// TODO Check to make sure properties are all set up before running
Session session = coll.getAncestorSession();
Database targetDb = session.getDatabase(getTargetServer(), getTargetFilepath());
View targetView = targetDb.getView(getTargetLookupView());
Strategy strategy = getStrategy();
DatabaseTransaction txn = null;
if (getTransactionRule() == TransactionRule.COMMIT_AT_END) {
txn = targetDb.startTransaction();
}
for (Document source : coll) {
if (getTransactionRule() == TransactionRule.COMMIT_EVERY_SOURCE) {
txn = targetDb.startTransaction();
}
DateTime sourceLastMod = source.getLastModified();
// Object lookupKey = Factory.wrappedEvaluate(session, getSourceKeyFormula(), source);
Object lookupKey = getSourceKeyFormula().getValue(source);
DocumentCollection targetColl = targetView.getAllDocumentsByKey(lookupKey, true);
for (Document target : targetColl) {
// boolean targetDirty = false;
for (Map.Entry<Formula, String> entry : getSyncMap().entrySet()) {
String targetItemName = entry.getValue();
java.util.Vector<?> sourceValue = entry.getKey().getValue(source);
// Factory.wrappedEvaluate(session, entry.getKey(), source);
if (strategy == Strategy.CREATE_AND_REPLACE) {
target.replaceItemValue(targetItemName, sourceValue);
// targetDirty = true;
} else {
Item targetItem = target.getFirstItem(targetItemName);
if (strategy == Strategy.REPLACE_IF_NEWER) {
DateTime itemLastMod = targetItem.getLastModified();
if (sourceLastMod.isAfter(itemLastMod)) {
targetItem.setValues(sourceValue);
// targetDirty = true;
}
} else if (strategy == Strategy.REPLACE_ONLY) {
if (targetItem != null) {
targetItem.setValues(sourceValue);
// targetDirty = true;
}
}
}
}
if (getTransactionRule() == TransactionRule.NO_TRANSACTION || txn == null) {
target.save();
}
}
if (getTransactionRule() == TransactionRule.COMMIT_EVERY_SOURCE && txn != null) {
txn.commit();
txn = null;
}
}
if (getTransactionRule() == TransactionRule.COMMIT_AT_END && txn != null) {
txn.commit();
txn = null;
}
}