Parent: [a7a9ff] (diff)

Download this file

ImportController.java    283 lines (242 with data), 8.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET 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.
*
* TACET 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 TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.controller;
import java.awt.Frame;
import java.util.List;
import java.util.Map;
import javax.swing.table.DefaultTableModel;
import squirrel.model.io.CSVDataSource;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.model.io.IOFactory;
import squirrel.view.ImportWizard.ImportWizard;
public class ImportController{
private ValidationController validControll;
private ImportWizard view;
private MasterController masterController;
private boolean debugMode = true;
/**
* Instantiates a new import controller.
*
* @param masterController the master controller
*/
public ImportController(MasterController masterController) {
this.masterController = masterController;
}
public void setValidationController(DataSource.Type type){
if(type==DataSource.Type.CSV){
validControll = new ValidationControllerCSV(false);
}
}
/**
* Sets the import view.
*
* @param view the new import view
*/
public void setImportView(ImportWizard view) {
this.view = view;
}
/**
* Sets the master controller.
*
* @param master the new master controller
*/
public void setMasterController(MasterController master) {
this.masterController = master;
}
private int sourceType;
public DefaultTableModel analyseLine(boolean header){
return ((ValidationControllerCSV) validControll).anlyseLine(header);
}
public String getFirstLine(){
return ((ValidationControllerCSV) validControll).getFirstLine();
}
public String getElemSeperator(){
return ((ValidationControllerCSV) validControll).getCsvElemSeparator();
}
/**
* Validates the location and readability of the csv file. Creates error if
* the file does not exist or is not readable.
*
* @param loc the location of the csv file
* @return the validation result
*/
public ValidationResult validateLocation(String loc) {
return validControll.validateLocation(loc);
}
/**
* Validates the element separator of the csv file. Creates error if the
* element separator is not 1 character long.
*
* @param elemSep the element separator
* @return the validation result
*/
public ValidationResult validateCSVElemSep(String elemSep) {
return ((ValidationControllerCSV) validControll).validateCSVElemSep(elemSep);
}
/**
* Validates the line separator of the csv file. Creates error if there is
* no line separator given.
*
* @param lineSep the line separator
* @return the validation result
*/
public ValidationResult validateCSVLineSep(String lineSep) {
return ((ValidationControllerCSV) validControll).validateCSVLineSep(lineSep);
}
/**
* Validates the column descriptions of the csv file. Creates error if the
* description of any column is missing or the description is malconfigured.
*
* @param tblModel the tbl model
* @return the validation result
*/
public ValidationResult validateCSVColumnDescriptions(DefaultTableModel tblModel) {
return ((ValidationControllerCSV) validControll).validateCSVColumnDescriptions(tblModel);
}
/**
* Validate csv date.
*
* @param date the date
* @return the validation result
*/
public ValidationResult validateCSVDate(int column, String date) {
return ((ValidationControllerCSV) validControll).validateCSVDate(column, date);
}
/**
* Validate csv custom units.
*
* @param customUnits the custom units
* @return the validation result
*/
public ValidationResult validateCSVCustomUnits(int column, String customUnits) {
return ((ValidationControllerCSV) validControll).validateCSVCustomUnits(column, customUnits);
}
/**
* Validate csv custom separator.
*
* @param customSeparator the custom separator
* @return the validation result
*/
public ValidationResult validateCSVCustomSeparator(int column, String customSeparator) {
return ((ValidationControllerCSV) validControll).validateCSVCustomSeparator(column, customSeparator);
}
public ValidationResult validateCSVTimestampFormats(Map<Integer, TimestampFormat> formats) {
return ((ValidationControllerCSV) validControll).validateCSVTimestampFormats(formats);
}
public String getGuestTimestampFormat(){
return ((ValidationControllerCSV) validControll).getGuestTimeStampFormat();
}
/**
* Validate csv range.
*
* @param from the from
* @param to the to
* @return the validation result
*/
public ValidationResult validatePreviewRange(String from, String to) {
return validControll.validatePreviewRange(from, to);
}
/**
* Do refresh.
*/
public void doRefresh() {
switch (this.getSelectedSourceType()) {
case CSV:
CSVDataSource csvDataSource = (CSVDataSource) createDataSource();
if (csvDataSource != null) {
PreviewTableModelAdapter adapter =
new PreviewTableModelAdapter(validControll.getPreviewRange(),
IOFactory.createCSVReader(csvDataSource), csvDataSource);
adapter.readData();
}
break;
}
}
/**
* Do import.
*/
public void doImport() {
DataSource<? extends DataColumn> dataSource = createDataSource();
if (!validControll.isError()) {
dataSource.setType(this.getSelectedSourceType());
masterController.onImportFinish(dataSource);
}
validControll.setError(false);
}
private DataSource<? extends DataColumn> createDataSource() {
DataSource<? extends DataColumn> ds = validControll.createDataSource(view);
if(!validControll.isError()){
ds.setType(this.getSelectedSourceType());
}
return ds;
}
/**
* Present dialog.
*
* @param owner the owner
*/
public void presentDialog(Frame owner) {
// if (view == null)
this.view = new ImportWizard(this);
}
/**
* Validate video location.
*
* @param loc the loc
* @return the validation result
*/
public ValidationResult validateVideoLocation(String loc) {
return validControll.validateVideoLocation(loc);
}
/**
* Clear media paths.
*/
public void clearMediaPaths() {
validControll.clearMediaPaths();
}
public void emptyValidationList() {
validControll.clearValidations();
}
/**
* Get Selected Source type
* @return type of source file
*/
public DataSource.Type getSelectedSourceType() {
switch (this.sourceType) {
case 0:
return DataSource.Type.CSV;
default:
throw new IllegalStateException("The selected tab/source is not supported.");
}
}
public void setSourceType(int type) {
this.sourceType = type;
}
public boolean getDebug(){
return this.debugMode;
}
public List<ValidationResult> getErrorValidations() {
return validControll.getErrorValidations();
}
public void clearErrors() {
// TODO Auto-generated method stub
emptyValidationList();
}
}