#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 System.Windows.Forms; using System.Xml; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.DataImporter.Data; using HeuristicLab.DataImporter.Command.View; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] [ViewableCommandInfoAttribute("Set Part of DateTime", 1, ColumnGroupState.DateTimeColumnSelected, "Time Series", Position = 2, SelectedColumns = 1, OptionsView = typeof(TimeBasedWithoutOffsetCommandView))] public class SetPartOfDateTimeCommand : ColumnGroupCommandWithAffectedColumnsBase { private DateTimeColumn oldColumn; private ICollection oldSortedColumnIndices; private IEnumerable oldSortOrder; private SetPartOfDateTimeCommand() : base(null, string.Empty, null) { } public SetPartOfDateTimeCommand(DataSet dataSet, string columnGroupName, int[] affectedColumns) : base(dataSet, columnGroupName, affectedColumns) { } [Storable] private DateTimeSpan dateTimePart; public DateTimeSpan DateTimePart { get { return this.dateTimePart; } set { this.dateTimePart = value; } } public override string Description { get { return "Set specific part of datetime"; } } public override void Execute() { base.Execute(); oldColumn = (DateTimeColumn)ColumnGroup.GetColumn(AffectedColumns[0]); DateTimeColumn newColumn = (DateTimeColumn)oldColumn.CreateCopyOfColumnWithoutValues(); DateTime? value; DateTimeSpan newValue; oldSortedColumnIndices = new List(ColumnGroup.SortedColumnIndexes); oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList(); for (int i = 0; i < oldColumn.TotalValuesCount; i++) { newValue = (DateTimeSpan)dateTimePart.Clone(); value = (DateTime?)oldColumn.GetValue(i); if (value != null) { if (newValue.Seconds == -1) newValue.Seconds = value.Value.Second; if (newValue.Minutes == -1) newValue.Minutes = value.Value.Minute; if (newValue.Hours == -1) newValue.Hours = value.Value.Hour; if (newValue.Days == -1) newValue.Days = value.Value.Day; if (newValue.Months == -1) newValue.Months = value.Value.Month; if (newValue.Years == -1) newValue.Years = value.Value.Year; newColumn.AddValue(new DateTime(newValue.Years, newValue.Months, newValue.Days, newValue.Hours, newValue.Minutes, newValue.Seconds)); } else newColumn.AddValue(null); } ColumnGroup.ReplaceColumn(AffectedColumns[0], newColumn); ColumnGroup.FireChanged(); this.ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); ColumnGroup.ReplaceColumn(AffectedColumns[0], oldColumn); ColumnGroup.SortOrdersForColumns = oldSortOrder; ColumnGroup.SortedColumnIndexes = oldSortedColumnIndices; oldSortedColumnIndices = null; oldSortOrder = null; oldColumn = null; ColumnGroup.FireChanged(); this.ColumnGroup = null; } } }