Parent: [a7a9ff] (diff)

Download this file

ValidationController.java    349 lines (292 with data), 11.0 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* 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.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.table.DefaultTableModel;
import squirrel.model.AnnotationFactory;
import squirrel.model.io.DataColumn;
import squirrel.model.io.DataSource;
import squirrel.model.io.DataColumn.Unit;
import squirrel.util.Range;
import squirrel.view.ExportView;
import squirrel.view.WarningView;
public abstract class ValidationController {
protected Map<ValidationType, ValidationResult> validationResults = new HashMap<>();
protected URL url = null;
protected boolean error = false;
protected int startColumns;
protected AnnotationFactory annotationFactory;
protected Unit[] customUnits;
protected DateFormat dateFormat;
/**
* The Enum ValidationType.
*/
public enum ValidationType {
COL_DESCS,
CSV_DATE,
CSV_IS_CUSTOM_DATE,
UNITS,
CSV_CUSTOM_SEPARATOR,
CSV_DATE_FORMATS,
CSV_ELEM_SEPARATOR,
CSV_LINE_SEPARATOR,
CSV_CUSTOM_DATE,
BINARY_DATE_FORMATS,
PREVIEW_RANGE,
BINARY_ENDIAN,
LOCATION,
NULL_MODEL,
NOT_IGNORE,
DB_COL_DESCS,
DB_LOCATION,
DB_Login,
VIDEO_LOCATION,
DB_URL,
DB_Connection,
DB_Statement,
DB_Vendor, DB_PREVIEW_RANGE, DB_UNITS, DB_DATE_FORMATS, DB_Date, ;
}
protected Range previewRange;
// private Map<Integer, ParsedTimestampFormat> binaryParsedTimeFormats;
// private SimpleDateFormat binaryDateFormat;
boolean export;
public ValidationController(boolean export) {
this.export = export;
}
// Only for Import
protected List<String> mediaPaths = new ArrayList<>();
public ValidationController(Map<ValidationType, ValidationResult> validationResults, URL url,
boolean error, int startColumns,
AnnotationFactory annotationFactory, boolean export) {
super();
this.validationResults = validationResults;
this.url = url;
this.error = error;
this.startColumns = startColumns;
this.annotationFactory = annotationFactory;
this.export = export;
}
public boolean isExport() {
return export;
}
public void setExport(boolean export) {
this.export = export;
}
public Map<ValidationType, ValidationResult> getValidationResults() {
return validationResults;
}
public AnnotationFactory getAnnotationFactory() {
return annotationFactory;
}
public Range getPreviewRange() {
return previewRange;
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public URL getURL() {
return url;
}
/**
* 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) {
File file = new File(loc.replaceAll("^file://", ""));
if (!export) {
boolean exists = file.exists();
boolean readable = file.canRead();
if (!exists) {
return createError(ValidationType.LOCATION, "\"" + loc + "\" does not exist.");
} else if (!readable) {
return createError(ValidationType.LOCATION, file + " is not readable.");
}
}
try {
url = new URL(loc);
url = file.toURI().toURL();
} catch (MalformedURLException e) {
return createError(ValidationType.LOCATION, "Not a valid url");
}
return createValid(ValidationType.LOCATION);
}
protected boolean nonEmpty(String s) {
return s != null && s.length() > 0;
}
public int getStartColumns() {
return startColumns;
}
public void setStartColumns(int startColumns) {
this.startColumns = startColumns;
}
public abstract DataSource<? extends DataColumn> createDataSource(WarningView view);
public List<ValidationResult> getErrorValidations() {
List<ValidationResult> ret = new LinkedList<>();
for (ValidationType key : validationResults.keySet()) {
ValidationResult vRes = validationResults.get(key);
if (vRes != null && vRes.type == ValidationResult.Type.ERROR) {
ret.add(vRes);
}
}
return ret;
}
protected ValidationResult createDummyError(ValidationType validType) {
ValidationResult res = ValidationResult.createDummyErrorResult();
validationResults.put(validType, res);
return res;
}
protected ValidationResult createError(ValidationType validType, String msg) {
ValidationResult res = ValidationResult.createErrorResult(msg);
validationResults.put(validType, res);
// setError(true);
return res;
}
protected ValidationResult createValid(ValidationType validType) {
ValidationResult res = ValidationResult.createValidResult();
validationResults.put(validType, res);
return res;
}
protected boolean validateDeleteColumn(int number) {
return startColumns < number;
}
protected void validateChangeColumn(ExportView view, MasterController masterController) {
DataSource ds = createDataSource(view);
if (ds != null) {
for (int i = 0; i < ds.getNumberOfColumns(); i++) {
if (masterController.getModel().getCurrentDataSource().getNumberOfColumns() >= i
&& ds.getDataColumn(i).getType() != masterController.getModel()
.getCurrentDataSource().getDataColumn(i).getType()
&& ds.getDataColumn(i).getType() != DataColumn.Type.IGNORE) {
createError(ValidationType.NOT_IGNORE, "You can only change to Ignore");
} else if (masterController.getModel().getCurrentDataSource().getNumberOfColumns() < i
&& (ds.getDataColumn(i).getType() == DataColumn.Type.IGNORE ||
ds.getDataColumn(i).getType() == DataColumn.Type.TIMESTAMP)) {
createError(ValidationType.NOT_IGNORE, "You can only add Timestamps");
}
}
}
}
public void clearValidations() {
this.validationResults.clear();
}
/**
* Validate video location.
*
* @param loc the loc
* @return the validation result
*/
public ValidationResult validateVideoLocation(String loc) {
URL url = null;
try {
url = new URL("file://" + loc);
} catch (MalformedURLException e) {
return createError(ValidationType.VIDEO_LOCATION, e.getLocalizedMessage());
}
File file = new File(url.getFile());
boolean exists = file.exists();
boolean readable = file.canRead();
if (loc == null) {
return createValid(ValidationType.VIDEO_LOCATION);
} else if (exists && readable) {
mediaPaths.add(loc);
return createValid(ValidationType.VIDEO_LOCATION);
} else if (!exists) {
return createError(ValidationType.VIDEO_LOCATION, "\"" + loc + "\" does not exist.");
} else if (!readable) {
return createError(ValidationType.VIDEO_LOCATION, file + " is not readable.");
}
return createDummyError(ValidationType.VIDEO_LOCATION);
}
/**
* Clear media paths.
*/
public void clearMediaPaths() {
mediaPaths.clear();
}
public ValidationResult validatePreviewRange(String from, String to) {
Long parsedFrom = null;
Long parsedTo = null;
try {
parsedFrom = Long.parseLong(from);
} catch (NumberFormatException nfe) {
return createError(ValidationType.PREVIEW_RANGE, "From: '" + from
+ "' is not a valid integer.");
}
try {
parsedTo = Long.parseLong(to);
} catch (NumberFormatException nfe) {
return createError(ValidationType.PREVIEW_RANGE, "To: '" + to
+ "' is not a valid integer.");
}
if (parsedFrom <= 0)
return createError(ValidationType.PREVIEW_RANGE, "From may not be negative.");
if (parsedTo < 0)
return createError(ValidationType.PREVIEW_RANGE, "To may not be negative.");
if (parsedFrom > parsedTo)
return createError(ValidationType.PREVIEW_RANGE,
"From may not be greater than To.");
this.previewRange = new Range(parsedFrom, parsedTo);
return createValid(ValidationType.PREVIEW_RANGE);
}
public ValidationResult validateUnit(int column, String[] timeUnits) {
if (timeUnits.length > 0) {
Unit[] units = new Unit[timeUnits.length];
for (int i = 0; i < timeUnits.length; i++) {
switch (timeUnits[i]) {
case "h":
units[i] = Unit.HOURS;
break;
case "m":
units[i] = Unit.MINUTES;
break;
case "s":
units[i] = Unit.SECONDS;
break;
case "hs":
units[i] = Unit.HUNDRETH_SECONDS;
break;
case "S":
units[i] = Unit.MILLISECONDS;
break;
default:
return createError(ValidationType.UNITS, "\"" + timeUnits[i]
+ "\" is not a valid unit.");
}
}
this.customUnits = units;
return createValid(ValidationType.UNITS);
}
return createError(ValidationType.UNITS, "Units may not be empty.");
}
}