public void endElement(String uri, String localName, String qname) { super.endElement(uri, localName, qname); if (record != null && localName.equals(DataRecord.ENCODING_RECORD)) { // end of record if (dataImport.importRecord(record, fieldsInImport)) { count++; } record = null; fieldsInImport = null; } String fieldValue = getFieldValue(); if (record != null && fieldValue != null) { // end of field try { if (textImport) { if (!record.setFromText(fieldName, fieldValue.trim())) { dataImport.logImportWarning( record, "Value '" + fieldValue + "' for Field '" + fieldName + "' corrected to '" + record.getAsText(fieldName) + "'"); } } else { record.set(fieldName, fieldValue.trim()); } String[] equivFields = record.getEquivalentFields(fieldName); for (String f : equivFields) { fieldsInImport.add(f); } } catch (Exception esetvalue) { dataImport.logImportWarning( record, "Cannot set value '" + fieldValue + "' for Field '" + fieldName + "': " + esetvalue.toString()); } } }
private void logImportFailed(DataRecord r, String msg, Exception e) { if (e != null) { Logger.logdebug(e); } logInfo( "\nERROR: " + LogString.operationFailed( International.getMessage("Import von Datensatz {record}", r.toString()), msg)); errorCount++; }
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; }
private boolean importRecord(DataRecord r, ArrayList<String> fieldsInInport) { try { if (Logger.isDebugLogging()) { Logger.log(Logger.DEBUG, Logger.MSG_DEBUG_DATA, "importing " + r.toString()); } DataRecord[] otherVersions = null; if (importMode.equals(IMPORTMODE_ADD) && logbookEntryNoHandling != null && logbookEntryNoHandling.equals(ENTRYNO_ALWAYS_ADDEND)) { // determine new EntryId for logbook r.set(keyFields[0], ((Logbook) storageObject).getNextEntryNo()); } if (isLogbook && (importMode.equals(IMPORTMODE_ADD) || importMode.equals(IMPORTMODE_ADDUPD))) { LogbookRecord lr = ((LogbookRecord) r); if (lr.getEntryId() == null || !lr.getEntryId().isSet() || lr.getEntryId().toString().length() == 0) { r.set(keyFields[0], ((Logbook) storageObject).getNextEntryNo()); } } DataKey key = r.getKey(); if (key.getKeyPart1() == null || overrideKeyField != null) { // first key field is *not* set, or we're overriding the default key field DataKey[] keys = null; if (overrideKeyField == null) { // -> search for record by QualifiedName keys = dataAccess.getByFields( r.getQualifiedNameFields(), r.getQualifiedNameValues(r.getQualifiedName()), (versionized ? validAt : -1)); } else { // -> search for record by user-specified key field keys = dataAccess.getByFields( new String[] {overrideKeyField}, new String[] {r.getAsString(overrideKeyField)}, (versionized ? validAt : -1)); } if (keys != null && keys.length > 0) { for (int i = 0; i < keyFields.length; i++) { if (!keyFields[i].equals(DataRecord.VALIDFROM)) { r.set(keyFields[i], keys[0].getKeyPart(i)); } } } else { for (int i = 0; i < keyFields.length; i++) { if (!keyFields[i].equals(DataRecord.VALIDFROM) && r.get(keyFields[i]) == null) { if (dataAccess.getMetaData().getFieldType(keyFields[i]) == IDataAccess.DATA_UUID) { r.set(keyFields[i], UUID.randomUUID()); } else { logImportFailed(r, "KeyField(s) not set", null); return false; } } } } } key = r.getKey(); if (versionized) { otherVersions = dataAccess.getValidAny(key); } else { DataRecord r1 = dataAccess.get(key); otherVersions = (r1 != null ? new DataRecord[] {r1} : null); } if (importMode.equals(IMPORTMODE_ADD) && otherVersions != null && otherVersions.length > 0 && logbookEntryNoHandling != null && logbookEntryNoHandling.equals(ENTRYNO_DUPLICATE_ADDEND)) { r.set(keyFields[0], ((Logbook) storageObject).getNextEntryNo()); otherVersions = null; } if (importMode.equals(IMPORTMODE_ADD)) { if (otherVersions != null && otherVersions.length > 0) { logImportFailed(r, International.getString("Datensatz existiert bereits"), null); return false; } else { addRecord(r); return true; } } if (importMode.equals(IMPORTMODE_UPD)) { if (otherVersions == null || otherVersions.length == 0) { logImportFailed(r, International.getString("Datensatz nicht gefunden"), null); return false; } else { updateRecord(r, fieldsInInport); return true; } } if (importMode.equals(IMPORTMODE_ADDUPD)) { if (otherVersions != null && otherVersions.length > 0) { updateRecord(r, fieldsInInport); } else { addRecord(r); } return true; } } catch (Exception e) { logImportFailed(r, e.getMessage(), e); } return false; }
private void updateRecord(DataRecord r, ArrayList<String> fieldsInInport) { try { DataRecord rorig = (versionized ? dataAccess.getValidAt(r.getKey(), validAt) : dataAccess.get(r.getKey())); if (rorig == null) { logImportFailed( r, International.getString("Keine gültige Version des Datensatzes gefunden."), null); return; } // has the import record an InvalidFrom field? long invalidFrom = (versionized ? getInvalidFrom(r) : -1); if (invalidFrom <= rorig.getValidFrom()) { invalidFrom = -1; } boolean changed = false; for (int i = 0; i < fields.length; i++) { Object o = r.get(fields[i]); if ((o != null || fieldsInInport.contains(fields[i])) && !r.isKeyField(fields[i]) && !fields[i].equals(DataRecord.LASTMODIFIED) && !fields[i].equals(DataRecord.VALIDFROM) && !fields[i].equals(DataRecord.INVALIDFROM) && !fields[i].equals(DataRecord.INVISIBLE) && !fields[i].equals(DataRecord.DELETED)) { Object obefore = rorig.get(fields[i]); rorig.set(fields[i], o); if ((o != null && !o.equals(obefore)) || (o == null && obefore != null)) { changed = true; } } } if (invalidFrom <= 0) { long myValidAt = getValidFrom(r); if (!versionized || updMode.equals(UPDMODE_UPDATEVALIDVERSION) || rorig.getValidFrom() == myValidAt) { if (changed) { dataAccess.update(rorig); } setCurrentWorkDone(++importCount); } if (versionized && updMode.equals(UPPMODE_CREATENEWVERSION) && rorig.getValidFrom() != myValidAt) { if (changed) { dataAccess.addValidAt(rorig, myValidAt); } setCurrentWorkDone(++importCount); } } else { dataAccess.changeValidity(rorig, rorig.getValidFrom(), invalidFrom); setCurrentWorkDone(++importCount); } } catch (Exception e) { logImportFailed(r, e.toString(), e); } }
private long getInvalidFrom(DataRecord r) { long rv = r.getInvalidFrom(); return (rv > 0 && rv < Long.MAX_VALUE ? rv : -1); }
private long getValidFrom(DataRecord r) { long rv = r.getValidFrom(); return (rv > 0 ? rv : validAt); }
private void logImportWarning(DataRecord r, String msg) { logInfo("\nWARNING: " + msg + ": " + r.toString()); warningCount++; }