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); }
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 void addRecord(DataRecord r) { try { if (versionized) { long myValidAt = getValidFrom(r); dataAccess.addValidAt(r, myValidAt); setCurrentWorkDone(++importCount); } else { dataAccess.add(r); setCurrentWorkDone(++importCount); } } catch (Exception e) { logImportFailed(r, e.toString(), e); } }
public void startElement(String uri, String localName, String qname, Attributes atts) { super.startElement(uri, localName, qname, atts); if (localName.equals(DataExport.FIELD_EXPORT)) { String type = atts.getValue(DataExport.EXPORT_TYPE); textImport = (type != null && type.equals(DataExport.EXPORT_TYPE_TEXT)); } else if (localName.equals(DataRecord.ENCODING_RECORD)) { // begin of record record = dataAccess.getPersistence().createNewRecord(); fieldsInImport = new ArrayList<String>(); return; } else { if (atts.getValue("key") != null && atts.getValue("key").equalsIgnoreCase("true")) { overrideKeyField = localName; } } }
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; }