1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.DataImporter.Command.View;
|
---|
27 | using HeuristicLab.DataImporter.Data;
|
---|
28 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
29 | using HeuristicLab.DataImporter.Data.Model;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HEAL.Attic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.DataImporter.Command {
|
---|
34 | [StorableType("93774205-CCAE-425D-97C2-74A00CEBC5CC")]
|
---|
35 | [ViewableCommandInfoAttribute("Set Part of DateTime", 1, ColumnGroupState.DateTimeColumnSelected, "Time Series",
|
---|
36 | Position = 2, SelectedColumns = 1, OptionsView = typeof(TimeBasedWithoutOffsetCommandView))]
|
---|
37 | public class SetPartOfDateTimeCommand : ColumnGroupCommandWithAffectedColumnsBase {
|
---|
38 | private DateTimeColumn oldColumn;
|
---|
39 | private ICollection<int> oldSortedColumnIndices;
|
---|
40 | private IEnumerable<SortOrder> oldSortOrder;
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | protected SetPartOfDateTimeCommand(StorableConstructorFlag _) : base(_) { }
|
---|
44 | public SetPartOfDateTimeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
|
---|
45 | : base(dataSet, columnGroupName, affectedColumns) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private DateTimeSpan dateTimePart;
|
---|
50 | public DateTimeSpan DateTimePart {
|
---|
51 | get { return this.dateTimePart; }
|
---|
52 | set { this.dateTimePart = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override string Description {
|
---|
56 | get { return "Set specific part of datetime"; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override void Execute() {
|
---|
60 | base.Execute();
|
---|
61 | oldColumn = (DateTimeColumn)ColumnGroup.GetColumn(AffectedColumns[0]);
|
---|
62 | DateTimeColumn newColumn = (DateTimeColumn)oldColumn.CreateCopyOfColumnWithoutValues();
|
---|
63 | DateTime? value;
|
---|
64 | DateTimeSpan newValue;
|
---|
65 | oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
|
---|
66 | oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
|
---|
67 |
|
---|
68 | for (int i = 0; i < oldColumn.TotalValuesCount; i++) {
|
---|
69 | newValue = (DateTimeSpan)dateTimePart.Clone();
|
---|
70 | value = (DateTime?)oldColumn.GetValue(i);
|
---|
71 | if (value != null) {
|
---|
72 | if (newValue.Seconds == -1)
|
---|
73 | newValue.Seconds = value.Value.Second;
|
---|
74 | if (newValue.Minutes == -1)
|
---|
75 | newValue.Minutes = value.Value.Minute;
|
---|
76 | if (newValue.Hours == -1)
|
---|
77 | newValue.Hours = value.Value.Hour;
|
---|
78 | if (newValue.Days == -1)
|
---|
79 | newValue.Days = value.Value.Day;
|
---|
80 | if (newValue.Months == -1)
|
---|
81 | newValue.Months = value.Value.Month;
|
---|
82 | if (newValue.Years == -1)
|
---|
83 | newValue.Years = value.Value.Year;
|
---|
84 |
|
---|
85 | newColumn.AddValue(new DateTime(newValue.Years, newValue.Months, newValue.Days, newValue.Hours, newValue.Minutes, newValue.Seconds));
|
---|
86 | } else
|
---|
87 | newColumn.AddValue(null);
|
---|
88 | }
|
---|
89 | ColumnGroup.ReplaceColumn(AffectedColumns[0], newColumn);
|
---|
90 | ColumnGroup.FireChanged();
|
---|
91 | this.ColumnGroup = null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override void UndoExecute() {
|
---|
95 | base.UndoExecute();
|
---|
96 | ColumnGroup.ReplaceColumn(AffectedColumns[0], oldColumn);
|
---|
97 | ColumnGroup.SortOrdersForColumns = oldSortOrder;
|
---|
98 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
|
---|
99 | oldSortedColumnIndices = null;
|
---|
100 | oldSortOrder = null;
|
---|
101 | oldColumn = null;
|
---|
102 | ColumnGroup.FireChanged();
|
---|
103 | this.ColumnGroup = null;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|