-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFile.java
More file actions
434 lines (377 loc) · 12.8 KB
/
InputFile.java
File metadata and controls
434 lines (377 loc) · 12.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package models;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import configuration.Consts;
import configuration.FileDefinitions;
import configuration.Filter;
import configuration.Utils;
import au.com.bytecode.opencsv.CSVReader;
import net.sf.flatpack.DataError;
import net.sf.flatpack.DataSet;
import net.sf.flatpack.brparse.BuffReaderDelimParser;
import net.sf.flatpack.brparse.BuffReaderFixedParser;
import net.sf.flatpack.brparse.BuffReaderParseFactory;
/**
* The Class InputFile.
* Represents one inputfile as defined in konfiguration.xml (tag datafile)
* uses flatpack for fixed length, i.e. Satzart-Files
* uses CSVReader for csv files
*
* returns value by column name
*/
public class InputFile {
protected final static Logger LOGGER = Logger.getLogger(InputFile.class.getName());
/** The data_id, e.g. STAMM */
private String datentyp;
/** The filetype, e.g. CSV */
private String filetype;
/** The path. */
private String path;
/** The fixparse file. */
private BuffReaderFixedParser fixparse = null;
/** The csvparse file. */
private BuffReaderDelimParser csvparse = null;
private DataSet flatpackDataset;
/** colnames **/
protected String[] colnames;
/** cached rows if needed */
protected ArrayList<LinkedHashMap<String,String>> rowcache = new ArrayList<LinkedHashMap<String,String>>();
/** points to last element in case that is to be used **/
protected int currentcachepointer=0;
private String currentCachedID = "";
protected boolean inWarpMode = false;
/** do I still have a row? */
protected boolean hasRow = false;
private IDfield[] idfields;
protected String currentID = "";
private boolean isLeader = false; //leadingtable: diese Tabelle wird um Features erweitert, dh. alle Zeilen bleiben erhalten
private boolean hasLeaderCols = false; //returns true if columns from leaderfile are to be added
private String[] leaderColnames;
private boolean hasLeaderNumfield = false; //returns true if there is a specific rowno from leaderdile
private String leaderNumfield;
private boolean upcaseData=false;
private HashMap<String,ArrayList<ColumnFilter>> colfilters = new HashMap<String,ArrayList<ColumnFilter>>();
/**
* Instantiates a new inputfiles file.
*
* @param data_id the data_id
* @param path the path
* @param filetype the filetype
* @throws Exception the exception
*/
public InputFile (String datentyp, String path, String filetype, String[] idfields, char separator, char quote, boolean upcaseData, ArrayList<Filter> filters) throws Exception {
//ToDo: Eigene Reader-Classe als Wrapper für z.B. flatpack, csvreader usw.
this.datentyp = datentyp;
this.filetype = filetype;
this.path = path;
this.idfields = new IDfield[idfields.length];
this.upcaseData=upcaseData;
for (int i=0;i<idfields.length;i++) this.idfields[i] = new IDfield(idfields[i]);
//open datafile, but not for ddi
if (!filetype.equals(Consts.ddiFlag)) {
//check encoding first
String encoding = Utils.checkEncoding(path);
if (encoding==null) encoding="UTF-8";
//new InputStreamReader(new FileInputStream(myFile), encoding)
//new FileReader(path)
if (filetype.equals(Consts.satzartFlag)) {
FileDefinitions filedef = new FileDefinitions();
String myDef = filedef.getDefinition(datentyp);
//old: new FileReader(path));
fixparse = (BuffReaderFixedParser) BuffReaderParseFactory.getInstance().newFixedLengthParser(new StringReader(myDef), new InputStreamReader(new FileInputStream(path), encoding));
fixparse.setIgnoreExtraColumns(true); //ignores extra characters in lines that are too long; lines that are too short are ignored (i.e. first line)
//fixparse.setStoreRawDataToDataSet(true); //for Testing only
flatpackDataset = fixparse.parse();
colnames=flatpackDataset.getColumns();
} else {
//Issue: Flatpack will not give back CSV column name correctly, when BuffReaderDelimParser is used (getValue works fine)
//Workaround: Open csv beforehand and read first line
CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(path), encoding), separator, quote);
String [] firstLine;
if ((firstLine = reader.readNext()) != null) {
colnames = firstLine;
}
reader.close();
//make Uppercase
for(int i=0; i<colnames.length; i++) {
colnames[i]=colnames[i].toUpperCase();
}
csvparse = (BuffReaderDelimParser) BuffReaderParseFactory.getInstance().newDelimitedParser(new InputStreamReader(new FileInputStream(path), encoding),separator,quote);
//csvparse.setStoreRawDataToDataSet(true);//for Testing only
flatpackDataset = csvparse.parse();
}
if (flatpackDataset.getErrors() != null && !flatpackDataset.getErrors().isEmpty()) {
LOGGER.log(Level.SEVERE,"Fehler gefunden beim Einlesen von " + path);
for (int i = 0; i < flatpackDataset.getErrors().size(); i++) {
final DataError de = (DataError) flatpackDataset.getErrors().get(i);
LOGGER.log(Level.SEVERE,"Fehler: " + de.getErrorDesc() + " Zeile: " + de.getLineNo());
}
}
}
if (filters!=null) {
ColumnFilter newcolfilter;
for (Filter nextfilter : filters) {
newcolfilter = new ColumnFilter(nextfilter.getField(),nextfilter.getInclusion(),nextfilter.getExclusion());
if (!colfilters.containsKey(newcolfilter.getColumn()))
colfilters.put(newcolfilter.getColumn(), new ArrayList<ColumnFilter>());
colfilters.get(newcolfilter.getColumn()).add(newcolfilter);
}
}
}
/**
* Checks for field.
*
* @param field the field
* @return true, if successful
*/
public boolean hasField (String field) {
return Arrays.asList(colnames).contains(field);
}
/**
* Next row.
*
* @return true, if successful
*/
public boolean nextRow(boolean checkID, boolean allowWarpBack) throws Exception {
if (this.inWarpMode) {
//i.e. use current cache only!
currentcachepointer++;
//checkID only if last entry
if (currentcachepointer==rowcache.size()) checkID=true;
//stop warp mode if overshooting
if (currentcachepointer>rowcache.size()) {
//stop warp
this.inWarpMode= false;
currentcachepointer--;
} else hasRow=true;
}
if (!this.inWarpMode) {
if (!allowWarpBack) this.clearcache(false);
//add current row to rowcache, only if not filtered out
boolean isallowed=false;
LinkedHashMap<String,String> nextrow = null;
while (!isallowed && (hasRow = flatpackDataset.next())) {
nextrow = new LinkedHashMap<String,String>();
for (int i=0; i<colnames.length; i++) {
if (upcaseData)
nextrow.put(colnames[i],flatpackDataset.getString(colnames[i]).toUpperCase());
else
nextrow.put(colnames[i],flatpackDataset.getString(colnames[i]));
//test filters
if (colfilters.containsKey(colnames[i])) {
for (ColumnFilter nextfilter : colfilters.get(colnames[i])) {
isallowed=nextfilter.isAllowed(nextfilter.getValueSubstring(nextrow.get(colnames[i])));
if (!isallowed) break;
}
} else isallowed=true;
if (!isallowed) break;
}
}
if (!hasRow) return false; //break herer as file ends
if (isallowed) {
rowcache.add(nextrow);
currentcachepointer++;
}
}
if (checkID) {
String newID = "";
try {
for (int i=0; i<idfields.length; i++)
newID += idfields[i].getFinalValue(this.getValue(idfields[i].getField()));
} catch (Exception e) {
newID="";
}
if (newID=="") {
hasRow = false;
throw new Exception("Keine ID in File " + this.datentyp + ", Zeile "+flatpackDataset.getRowNo()+". Zeile wird ignoriert.");
}
if (currentcachepointer>1) {
//refresh cache, if
// a) newID <> currentID <> currentCachedID
// b) newID == currentID && newID <> currentCachedID
// -> i.e. flush all but last entries
if (!currentCachedID.isEmpty() && !newID.equals(currentCachedID) && !currentID.equals(currentCachedID)) {
this.clearcache(true);
}
}
currentCachedID=currentID;
currentID=newID;
}
return hasRow;
}
public boolean nextRow() throws Exception {
return this.nextRow(false, false);
}
/**
* Warps back to beginning of last ID, to repeat readin
* alternatively warps forward until correct ID is reached
*
* @return true, if successful
*/
public void warpToCorrectID (String warpID) throws Exception {
if (warpID.equals(currentCachedID)) {
currentcachepointer=0;
inWarpMode=true;
currentID=currentCachedID;
//set pointer to correct row, no checkid required
this.nextRow(false,true);
} else {
//warp forward until ID is equal or bigger (i.e. later), check ID but do not cache
while (this.currentID.compareTo(warpID)<0 && this.nextRow(true,false)) {}
}
}
/**
* Gets the value.
*
* @param field the field
* @return the value
*/
public String getValue (String field) {
return rowcache.get(currentcachepointer-1).get(field);
}
public String getValueLastCached (String field) {
int cachepointer = currentcachepointer-2;
if (!this.hasRow || cachepointer<0) cachepointer=currentcachepointer-1;
return rowcache.get(cachepointer).get(field);
}
public String getID() {
return currentID;
}
//returns fields in one String, Komma-separated
public String getIDFields() {
String fields="";
for (int i=0; i<idfields.length; i++) {
if (i==0) fields=idfields[i].getField();
else fields+=","+idfields[i].getField();
}
return fields;
}
/**
* Checks if is data_id.
*
* @param data_id the data_id
* @return true, if is data_id
*/
public boolean isDatentyp (String datentyp) {
return this.datentyp.equals(datentyp);
}
/**
* Gets the data_id.
*
* @return the data_id
*/
public String getDatentyp () {
return this.datentyp;
}
/**
* Gets the path.
*
* @return the path
*/
public String getPath () {
return this.path;
}
/**
* Gets the colnames.
*
* @return the colnames
*/
public String[] getColnames () {
return this.colnames;
}
/**
* Checks for row.
*
* @return true, if successful
*/
public boolean hasRow () {
return hasRow;
}
public boolean isLeader() {
return isLeader;
}
public void setLeader(boolean isLeader) {
this.isLeader = isLeader;
this.hasLeaderCols = true;
this.leaderColnames = colnames;
this.hasLeaderNumfield = false;
}
public void setLeaderNumfield (String numfield) {
boolean exists = false;
numfield = numfield.toUpperCase();
for(int i=0; i<colnames.length; i++) {
if (colnames[i].equals(numfield)) { exists=true; break; }
}
if (exists) {
hasLeaderNumfield=true;
leaderNumfield=numfield;
}
}
public void setLeaderColnames (String[] cols) {
this.leaderColnames = null; //reset leadercolnames
this.hasLeaderCols=false;
if (cols!= null && cols.length > 0 && !cols[0].equals("")) {
ArrayList<String> addcols= new ArrayList<String>();
for(int i=0; i<cols.length; i++) {
cols[i]=cols[i].toUpperCase();
for(int j=0; j<this.colnames.length; j++) {
if (this.colnames[j].equals(cols[i])) { addcols.add(cols[i]); break; }
}
}
if (addcols.size()>0) {
this.leaderColnames = addcols.toArray(new String[addcols.size()]);
this.hasLeaderCols=true;
}
}
}
public boolean hasLeaderCols () {
return hasLeaderCols;
}
public String[] getLeaderColnames () {
return leaderColnames;
}
public boolean hasLeaderNumfield () {
return hasLeaderNumfield;
}
public String getLeaderNumfield () {
return leaderNumfield;
}
/*
* Clears rowcache; keeps last two entries if required
*/
public void clearcache (boolean keeplast) {
if (keeplast) {
ArrayList<LinkedHashMap<String,String>> last = new ArrayList<LinkedHashMap<String,String>>();
if (rowcache.size() >1) last.add(rowcache.get(rowcache.size()-2));
if (rowcache.size() >0) last.add(rowcache.get(rowcache.size()-1));
rowcache.clear();
rowcache.addAll(last);
currentcachepointer=last.size();
} else {
rowcache.clear();
currentcachepointer=0;
}
}
/**
* Close.
*
* @throws Exception the exception
*/
public void close () throws Exception {
if (filetype.equals(Consts.satzartFlag)) {
fixparse.close();
} else if (filetype.equals(Consts.csvFlag)) {
csvparse.close();
}
}
public boolean isPatient (String id) {
return this.getID().equals(id);
}
}