#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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 HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfoAttribute("Delete Rows", 1, ColumnGroupState.Sorted, "Aggregation", Position = 5)]
public class DeleteRowsWithDuplicateKeyValuesCommand : ColumnGroupCommandBase {
private Dictionary deletedRows;
[StorableConstructor]
protected DeleteRowsWithDuplicateKeyValuesCommand(bool deserializing)
: base(deserializing) {
deletedRows = new Dictionary();
}
public DeleteRowsWithDuplicateKeyValuesCommand(DataSet dataSet, string columnGroupName)
: base(dataSet, columnGroupName) {
deletedRows = new Dictionary();
}
public override string Description {
get { return "Delete rows with duplicate key values"; }
}
public override void Execute() {
base.Execute();
if (!ColumnGroup.Columns.Any()) return;
if (!ColumnGroup.Sorted)
throw new CommandExecutionException("ColumnGroup must be sorted to delete rows with duplicate key.", this);
for (int i = 0; i < ColumnGroup.RowCount - 1; i++) {
if (ColumnGroup.KeyString(i) == ColumnGroup.KeyString(i + 1))
deletedRows[i + 1] = ColumnGroup.GetRow(i + 1);
}
foreach (int i in deletedRows.Keys.Reverse())
ColumnGroup.DeleteRow(i);
ColumnGroup.FireChanged();
}
public override void UndoExecute() {
base.UndoExecute();
foreach (KeyValuePair deletedRow in deletedRows)
ColumnGroup.InsertRow(deletedRow.Key, deletedRow.Value);
deletedRows.Clear();
ColumnGroup.FireChanged();
}
}
}