Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeValues/SearchWithMatchOperationCommandBase.cs @ 6133

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

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

File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.DataImporter.Data.CommandBase;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.DataImporter.Data.Model;
8using System.Drawing;
9
10namespace HeuristicLab.DataImporter.Command {
11  public enum MatchOperation {
12    Equal = 0,
13    NotEqual = 1,
14    Smaller = 2,
15    Larger = 3,
16  }
17
18  [StorableClass]
19  public abstract class SearchWithMatchOperationCommandBase : SearchCommandBase {
20    private SearchWithMatchOperationCommandBase()
21      : base(null, string.Empty, null) {
22    }
23
24    public SearchWithMatchOperationCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns) :
25      base(dataSet, columnGroupName, affectedColumns) {
26    }
27
28    [Storable]
29    private MatchOperation matchOperation;
30    public MatchOperation MatchOperation {
31      get { return this.matchOperation; }
32      set { this.matchOperation = value; }
33    }
34
35    protected Point[] GetAffectedCells() {
36      List<Point> affectedCells = new List<Point>();
37      Func<IComparable, IComparable, bool> compareFunction = this.GetCompareFunction(this.matchOperation);
38      ColumnBase column;
39      IComparable compareValue;
40      IComparable value;
41
42      for (int col = 0; col < AffectedColumns.Length; col++) {
43        column = ColumnGroup.GetColumn(AffectedColumns[col]);
44        compareValue = this.GetCompareValue(column, SearchValue);
45        for (int row = 0; row < column.TotalValuesCount; row++) {
46          value = column.GetValue(row);
47          if (compareFunction(compareValue, value))
48            affectedCells.Add(new Point(AffectedColumns[col], row));
49        }
50      }
51
52      return affectedCells.ToArray();
53    }
54
55    private IComparable GetCompareValue(ColumnBase column, string searchValue) {
56      if (string.IsNullOrEmpty(searchValue))
57        return null;
58
59      IComparable compareValue;
60      if (column is DoubleColumn) {
61        double v;
62        if (!double.TryParse(SearchValue, out v)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DoubleColumn.", this);
63        compareValue = v;
64      } else if (column is DateTimeColumn) {
65        DateTime d;
66        if (!DateTime.TryParse(SearchValue, out d)) throw new CommandExecutionException("Can't use value " + SearchValue + " to filter a DateTimeColumn.", this);
67        compareValue = d;
68      } else if (column is StringColumn) {
69        compareValue = SearchValue;
70      } else throw new CommandExecutionException("Column type " + column.GetType() + " is not supported.", this);
71      return compareValue;
72    }
73
74    private Func<IComparable, IComparable, bool> GetCompareFunction(MatchOperation matchOperation) {
75      Func<IComparable, IComparable, bool> compareFunction;
76      switch (matchOperation) {
77        case MatchOperation.Equal:
78          compareFunction = (left, right) => {
79            if (left == null && right == null) return true;
80            else if (left != null && right == null) return false;
81            else if (left == null && right != null) return false;
82            return left.CompareTo(right) == 0;
83          };
84          break;
85        case MatchOperation.NotEqual:
86          compareFunction = (left, right) => {
87            if (left == null && right == null) return false;
88            else if (left != null && right == null) return true;
89            else if (left == null && right != null) return true;
90            return left.CompareTo(right) != 0;
91          };
92          break;
93        case MatchOperation.Smaller:
94          compareFunction = (left, right) => {
95            if (left == null && right == null) return false;
96            else if (left != null && right == null) return false;
97            else if (left == null && right != null) return false;
98            return left.CompareTo(right) > 0;
99          };
100          break;
101        case MatchOperation.Larger:
102          compareFunction = (left, right) => {
103            if (left == null && right == null) return false;
104            else if (left != null && right == null) return false;
105            else if (left == null && right != null) return false;
106            return left.CompareTo(right) < 0; };
107          break;
108        default:
109          throw new ArgumentException("Undefined compare function.");
110      }
111      return compareFunction;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.