try {
// disable the triggers to prevent unexpected extra db actions
// during import
TriggerHandler.getInstance().disable();
final StaxXMLEntityConverter xec = StaxXMLEntityConverter.newInstance();
xec.setOptionClientImport(true);
xec.setOptionImportAuditInfo(importAuditInfo);
xec.getEntityResolver().setOptionCreateReferencedIfNotFound(true);
xec.setEntityResolver(ClientImportEntityResolver.getInstance());
xec.setImportProcessor(importProcessor);
xec.process(reader);
ir.setLogMessages(xec.getLogMessages());
ir.setErrorMessages(xec.getErrorMessages());
ir.setWarningMessages(xec.getWarningMessages());
if (ir.hasErrorOccured()) {
OBDal.getInstance().rollbackAndClose();
rolledBack = true;
return ir;
}
if (importProcessor != null) {
try {
importProcessor.process(xec.getToInsert(), xec.getToUpdate());
} catch (final Exception e) {
// note on purpose caught and set in ImportResult
e.printStackTrace(System.err);
ir.setException(e);
ir.setErrorMessages(e.getMessage());
OBDal.getInstance().rollbackAndClose();
rolledBack = true;
return ir;
}
}
// validate the objects
for (BaseOBObject bob : xec.getToInsert()) {
validateObject(bob, ir);
}
for (BaseOBObject bob : xec.getToUpdate()) {
validateObject(bob, ir);
}
if (ir.hasErrorOccured()) {
OBDal.getInstance().rollbackAndClose();
rolledBack = true;
return ir;
}
// now save and update
// do inserts and updates in opposite order, this is important
// so that the objects on which other depend are inserted first
final List<BaseOBObject> toInsert = xec.getToInsert();
int done = 0;
final Set<BaseOBObject> inserted = new HashSet<BaseOBObject>();
for (int i = toInsert.size() - 1; i > -1; i--) {
final BaseOBObject ins = toInsert.get(i);
// for (final BaseOBObject ins : toInsert) {
insertObjectGraph(ins, inserted);
ir.getInsertedObjects().add(ins);
done++;
}
Check.isTrue(done == toInsert.size(),
"Not all objects have been inserted, check for loop: " + done + "/" + toInsert.size());
// flush to set the ids in the objects
OBDal.getInstance().flush();
// do the updates the other way around also
done = 0;
final List<BaseOBObject> toUpdate = xec.getToUpdate();
for (int i = toUpdate.size() - 1; i > -1; i--) {
final BaseOBObject upd = toUpdate.get(i);
OBDal.getInstance().save(upd);
ir.getUpdatedObjects().add(upd);
done++;
}
Check.isTrue(done == toUpdate.size(),
"Not all objects have been inserted, check for loop: " + done + "/" + toUpdate.size());
// flush to set the ids in the objects
OBDal.getInstance().flush();
// now walk through the objects to repair primitive reference id's
// note updates both the ir and changes the entityresolver
repairPrimitiveReferences(ir, xec.getEntityResolver());
if (ir.hasErrorOccured()) {
OBDal.getInstance().rollbackAndClose();
rolledBack = true;
return ir;