See More

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> rowcache = new ArrayList>(); /** 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> colfilters = new HashMap>(); /** * 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 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()); 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 nextrow = null; while (!isallowed && (hasRow = flatpackDataset.next())) { nextrow = new LinkedHashMap(); for (int i=0; i1) { //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 0 && !cols[0].equals("")) { ArrayList addcols= new ArrayList(); for(int i=0; i0) { 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> last = new ArrayList>(); 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); } }