public DataImport( StorageObject storageObject, String filename, String encoding, char csvSeparator, char csvQuotes, String importMode, String updMode, String logbookEntryNoHandling, long validAt) { super(); this.storageObject = storageObject; this.dataAccess = storageObject.data(); this.versionized = storageObject.data().getMetaData().isVersionized(); this.fields = dataAccess.getFieldNames(); this.keyFields = dataAccess.getKeyFieldNames(); this.filename = filename; this.encoding = encoding; this.csvSeparator = csvSeparator; this.csvQuotes = csvQuotes; this.importMode = importMode; this.logbookEntryNoHandling = logbookEntryNoHandling; this.validAt = validAt; this.updMode = updMode; this.isLogbook = storageObject.data().getStorageObjectType().equals(Logbook.DATATYPE); }
public int runCsvImport() { int count = 0; try { int linecnt = 0; String[] header = null; ArrayList<String> fieldsInImport = new ArrayList<String>(); BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding)); String s; DataRecord dummyRecord = storageObject.createNewRecord(); while ((s = f.readLine()) != null) { s = s.trim(); if (s.length() == 0) { continue; } Vector<String> fields = splitFields(s); if (fields.size() > 0) { if (linecnt == 0) { // header header = new String[fields.size()]; for (int i = 0; i < fields.size(); i++) { header[i] = fields.get(i); if (header[i].startsWith("#") && header[i].endsWith("#") && header.length > 2) { header[i] = header[i].substring(1, header[i].length() - 1).trim(); overrideKeyField = header[i]; } String[] equivFields = dummyRecord.getEquivalentFields(header[i]); for (String ef : equivFields) { fieldsInImport.add(ef); } } } else { // fields DataRecord r = storageObject.createNewRecord(); for (int i = 0; i < header.length; i++) { String value = (fields.size() > i ? fields.get(i) : null); if (value != null && value.length() > 0) { try { if (!r.setFromText(header[i], value.trim())) { logImportWarning( r, "Value '" + value + "' for Field '" + header[i] + "' corrected to '" + r.getAsText(header[i]) + "'"); } } catch (Exception esetvalue) { logImportWarning( r, "Cannot set value '" + value + "' for Field '" + header[i] + "': " + esetvalue.toString()); } } } if (importRecord(r, fieldsInImport)) { count++; } } } linecnt++; } f.close(); } catch (Exception e) { logInfo(e.toString()); errorCount++; Logger.log(e); if (Daten.isGuiAppl()) { Dialog.error(e.toString()); } } return count; }