Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9760 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

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