1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.IO;
|
---|
23 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
24 | using HeuristicLab.DataImporter.Data.Model;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.DataImporter.Backend.Csv {
|
---|
28 | [StorableClass]
|
---|
29 | public class CsvImportCommand : DataSetCommandBase {
|
---|
30 | private ColumnGroup newColumnGroup;
|
---|
31 |
|
---|
32 | [StorableConstructor]
|
---|
33 | protected CsvImportCommand(bool deserializing) : base(deserializing) { }
|
---|
34 |
|
---|
35 | public CsvImportCommand(DataSet dataSet)
|
---|
36 | : base(dataSet) {
|
---|
37 | }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private string fileName;
|
---|
41 | public string FileName {
|
---|
42 | get { return fileName; }
|
---|
43 | set { this.fileName = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [Storable]
|
---|
47 | private char[] delimiter;
|
---|
48 | public char[] Delimiter {
|
---|
49 | get { return this.delimiter; }
|
---|
50 | set { this.delimiter = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | private bool columnNamesInFirstRow;
|
---|
55 | public bool ColumnNamesInFirstRow {
|
---|
56 | get { return this.columnNamesInFirstRow; }
|
---|
57 | set { this.columnNamesInFirstRow = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override string Description {
|
---|
61 | get { return "Import CSV file"; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override void Execute() {
|
---|
65 | base.Execute();
|
---|
66 | newColumnGroup = new ColumnGroup(fileName.Substring(fileName.LastIndexOf('\\') + 1));
|
---|
67 | StreamReader reader = new StreamReader(fileName);
|
---|
68 | string firstLine = reader.ReadLine();
|
---|
69 |
|
---|
70 | int i = 1;
|
---|
71 | string[] values = firstLine.Split(delimiter);
|
---|
72 | foreach (string value in values) {
|
---|
73 | if (columnNamesInFirstRow)
|
---|
74 | newColumnGroup.AddColumn(new StringColumn(value));
|
---|
75 | else {
|
---|
76 | newColumnGroup.AddColumn(new StringColumn("Column " + i));
|
---|
77 | }
|
---|
78 | i++;
|
---|
79 | }
|
---|
80 | if (!columnNamesInFirstRow)
|
---|
81 | newColumnGroup.AddRow(values);
|
---|
82 | while (!reader.EndOfStream) {
|
---|
83 | newColumnGroup.AddRow(reader.ReadLine().Split(delimiter));
|
---|
84 | }
|
---|
85 | reader.Close();
|
---|
86 | DataSet.AddColumnGroup(newColumnGroup);
|
---|
87 | DataSet.FireChanged();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void UndoExecute() {
|
---|
91 | base.UndoExecute();
|
---|
92 | DataSet.RemoveColumnGroup(newColumnGroup);
|
---|
93 | newColumnGroup = null;
|
---|
94 | DataSet.FireChanged();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|