Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6133 was 6133, checked in by gkronber, 13 years ago

#1471: imported generic parts of DataImporter from private code base

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