#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Command.View;
using System.Windows.Forms;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Change DateTimeColumn to DoubleColumn", 1, ColumnGroupState.DateTimeColumnSelected, "Time Series",
Position = 5, OptionsView = typeof(ChangeDateTimeCommandView))]
public class ChangeDateTimeColumnToDoubleColumn : ColumnGroupCommandWithAffectedColumnsBase {
private List newColumns;
private List oldColumns;
private List positions;
private ICollection oldSortedColumnIndexes;
private IEnumerable oldSortOrder;
private ChangeDateTimeColumnToDoubleColumn()
: base(null, string.Empty, null) {
newColumns = new List();
oldColumns = new List();
positions = new List();
}
public ChangeDateTimeColumnToDoubleColumn(DataSet dataSet, string columnGroupName, int[] affectedColumns)
: base(dataSet, columnGroupName, affectedColumns) {
newColumns = new List();
oldColumns = new List();
positions = new List();
}
public override string Description {
get { return "Changes the date values to double by substracting from a given date and converting the result to the given unit."; }
}
[Storable]
private DateTimeSpan frequency;
public DateTimeSpan Frequency {
get { return this.frequency; }
set { this.frequency = value; }
}
[Storable]
private DateTime startTime;
public DateTime StartTime {
get { return this.startTime; }
set { this.startTime = value; }
}
public override void Execute() {
base.Execute();
oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
oldSortedColumnIndexes = new List(ColumnGroup.SortedColumnIndexes);
DateTimeColumn oldColumn;
DoubleColumn newColumn;
DateTime? value;
double newValue;
for (int j = 0; j < AffectedColumns.Length; j++) {
oldColumn = (DateTimeColumn)ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
positions.Add(AffectedColumns[j]);
oldColumns.Add(oldColumn);
newColumn = new DoubleColumn(oldColumn.Name, oldColumn.TotalValuesCount);
for (int i = 0; i < oldColumn.TotalValuesCount; i++) {
value = (DateTime?)oldColumn.GetValue(i);
if (!value.HasValue)
newColumn.AddValue(null);
else {
TimeSpan elapsedTime = value.Value - startTime;
if (frequency.Seconds == 1)
newValue = elapsedTime.TotalSeconds;
else if (frequency.Minutes == 1)
newValue = elapsedTime.TotalMinutes;
else if (frequency.Hours == 1)
newValue = elapsedTime.TotalHours;
else if (frequency.Days == 1)
newValue = elapsedTime.TotalDays;
else
throw new CommandExecutionException("Unit for ChangeDateTimeColumnToDoubleColumn not correclty set.", this);
newColumn.AddValue(newValue);
}
}
newColumns.Add(newColumn);
}
for (int i = 0; i < positions.Count; i++)
ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
ColumnGroup.FireChanged();
}
public override void UndoExecute() {
base.UndoExecute();
for (int i = 0; i < positions.Count; i++)
ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
ColumnGroup.SortOrdersForColumns = oldSortOrder;
ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
oldSortedColumnIndexes = null;
oldSortOrder = null;
oldColumns.Clear();
newColumns.Clear();
positions.Clear();
ColumnGroup.FireChanged();
}
}
}