// Check if doc exists in database
JSONObject currentTargetDoc = null;
boolean creationRequired = false;
boolean modified = false;
UpdateSpecifier updateSpecifier = null;
try {
if( false == couchDb.documentExists(docId) ) {
creationRequired = true;
updateSpecifier = UpdateSpecifier.computeUpdateSpecifier(sourceDoc, dd, null, schedule);
} else {
// Get document from database
currentTargetDoc = couchDb.getDocument(docId);
// Check if the document in database was modified
modified = DocumentManifest.hasDocumentBeenModified(currentTargetDoc);
// Figure out the differences between the source document and
// the target document
updateSpecifier = UpdateSpecifier.computeUpdateSpecifier(sourceDoc, dd, currentTargetDoc, schedule);
}
} catch(Exception e) {
throw new Exception("Unable to access current document", e);
}
// Upload, if required
if( creationRequired ) {
logger.debug(sourceDoc.getId()+" creation required");
listener.updatingDocument(Phase.BEFORE, sourceDoc);
updateDocument(sourceDoc, dd, null, updateSpecifier);
listener.updatingDocument(Phase.AFTER, sourceDoc);
} else if( schedule == Schedule.UPDATE_FORCED ) {
logger.debug(sourceDoc.getId()+" forced update");
// Forced update always take place
listener.updatingDocument(Phase.BEFORE, sourceDoc);
updateDocument(sourceDoc, dd, currentTargetDoc, updateSpecifier);
listener.updatingDocument(Phase.AFTER, sourceDoc);
} else if( false == updateSpecifier.isUpdateRequired() ) {
logger.debug(sourceDoc.getId()+" update not required");
// Document on database is same as the one requested. No
// update required
listener.documentSkippedBecauseUnchanged(sourceDoc);