Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/TimeSeries/SetPartOfDateTimeCommand.cs @ 7267

Last change on this file since 7267 was 7267, checked in by gkronber, 12 years ago

#1734 updated copyright year in all files of the DataImporter branch

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Windows.Forms;
27using System.Xml;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30using HeuristicLab.DataImporter.Data;
31using HeuristicLab.DataImporter.Command.View;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.DataImporter.Command {
35  [StorableClass]
36  [ViewableCommandInfoAttribute("Set Part of DateTime", 1, ColumnGroupState.DateTimeColumnSelected, "Time Series",
37Position = 2, SelectedColumns = 1, OptionsView = typeof(TimeBasedWithoutOffsetCommandView))]
38  public class SetPartOfDateTimeCommand : ColumnGroupCommandWithAffectedColumnsBase {
39    private DateTimeColumn oldColumn;
40    private ICollection<int> oldSortedColumnIndices;
41    private IEnumerable<SortOrder> oldSortOrder;
42
43    private SetPartOfDateTimeCommand()
44      : base(null, string.Empty, null) {
45    }
46
47    public SetPartOfDateTimeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns)
48      : base(dataSet, columnGroupName, affectedColumns) {
49    }
50
51    [Storable]
52    private DateTimeSpan dateTimePart;
53    public DateTimeSpan DateTimePart {
54      get { return this.dateTimePart; }
55      set { this.dateTimePart = value; }
56    }
57
58    public override string Description {
59      get { return "Set specific part of datetime"; }
60    }
61
62    public override void Execute() {
63      base.Execute();
64      oldColumn = (DateTimeColumn)ColumnGroup.GetColumn(AffectedColumns[0]);
65      DateTimeColumn newColumn = (DateTimeColumn)oldColumn.CreateCopyOfColumnWithoutValues();
66      DateTime? value;
67      DateTimeSpan newValue;
68      oldSortedColumnIndices = new List<int>(ColumnGroup.SortedColumnIndexes);
69      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
70
71      for (int i = 0; i < oldColumn.TotalValuesCount; i++) {
72        newValue = (DateTimeSpan)dateTimePart.Clone();
73        value = (DateTime?)oldColumn.GetValue(i);
74        if (value != null) {
75          if (newValue.Seconds == -1)
76            newValue.Seconds = value.Value.Second;
77          if (newValue.Minutes == -1)
78            newValue.Minutes = value.Value.Minute;
79          if (newValue.Hours == -1)
80            newValue.Hours = value.Value.Hour;
81          if (newValue.Days == -1)
82            newValue.Days = value.Value.Day;
83          if (newValue.Months == -1)
84            newValue.Months = value.Value.Month;
85          if (newValue.Years == -1)
86            newValue.Years = value.Value.Year;
87
88          newColumn.AddValue(new DateTime(newValue.Years, newValue.Months, newValue.Days, newValue.Hours, newValue.Minutes, newValue.Seconds));
89        } else
90          newColumn.AddValue(null);
91      }
92      ColumnGroup.ReplaceColumn(AffectedColumns[0], newColumn);
93      ColumnGroup.FireChanged();
94      this.ColumnGroup = null;
95    }
96
97    public override void UndoExecute() {
98      base.UndoExecute();
99      ColumnGroup.ReplaceColumn(AffectedColumns[0], oldColumn);
100      ColumnGroup.SortOrdersForColumns = oldSortOrder;
101      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices;
102      oldSortedColumnIndices = null;
103      oldSortOrder = null;
104      oldColumn = null;
105      ColumnGroup.FireChanged();
106      this.ColumnGroup = null;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.