Vector replaceTags = new Vector();
Vector deleteTags = new Vector();
//Vector statusTags = new Vector();
String msgId = null;
Record record = null;
boolean ret = false;
// Get message id
try {
msgId = getXMLTagValue(modifications, TAG_MSGID );
}
catch (StringIndexOutOfBoundsException e){
sdh.log("[processModification]Invalid message Id from server: " + modifications);
throw new SyncException("Invalid message from server.");
}
// Get message parts
xmlResponse.addElement(modifications);
bodyTags = getXMLTag(getXMLTag(xmlResponse, TAG_SYNCML), TAG_SYNCBODY );
addTags = getXMLTag(bodyTags, TAG_ADD );
replaceTags = getXMLTag(bodyTags, TAG_REPLACE );
deleteTags = getXMLTag(bodyTags, TAG_DELETE );
//statusTags = getXMLTag(bodyTags, TAG_STATUS );
if (modifications.indexOf("<" + TAG_SYNC + ">") != - 1) {
ret = true;
}
Hashtable cmd = null;
// ADD
for (int i=0, l = addTags.size(); i < l; i++) {
cmd = processModificationCommand( (String)addTags.elementAt(i) );
// If there is an error status,
if(cmd.get(TAG_STATUS) != null){
// Add the status command for this item to the list
addStatusCommand(TAG_ADD, msgId, cmd);
continue;
}
record = new Record( (String)cmd.get(TAG_LOC_URI), ' ',
(String)cmd.get(TAG_DATA));
String bbKey = "";
try {
bbKey = dataStore.setRecord(record, false).getKey();
// Okay, set status code 200
cmd.put(TAG_STATUS, String.valueOf(STATUS_CODE_OK));
}
catch (DataAccessException e) {
StaticDataHelper.log("[ERROR]Exception in SyncManagerImpl.processModifications(add) invoking setRecord(...): " + e.toString());
//e.printStackTrace();
// Set error status code for this item
cmd.put(TAG_STATUS, String.valueOf(STATUS_CODE_ERROR));
String msg = e.getMessage();
throw new SyncException(msg);//it should be enough to break the sync process
}
finally {
// Add the status command for this item to the list
addStatusCommand(TAG_ADD, msgId, cmd);
}
if (bbKey != null) {
ret = true;
mappings.put(bbKey, cmd.get(TAG_LOC_URI));
}
}
// REPLACE
for (int i = 0, l = replaceTags.size(); i < l; i++) {
cmd = processModificationCommand((String)replaceTags.elementAt(i));
// If there is an error status,
if(cmd.get(TAG_STATUS) != null){
// Add the status command for this item to the list
addStatusCommand(TAG_REPLACE, msgId, cmd);
continue;
}
record = new Record( (String)cmd.get(TAG_LOC_URI), ' ',
(String)cmd.get(TAG_DATA) );
try {
dataStore.setRecord(record, true);
// Okay, set status code
cmd.put(TAG_STATUS, String.valueOf(STATUS_CODE_OK));
}
catch(DataAccessException e) {
StaticDataHelper.log("[ERROR]Exception in SyncManagerImpl.processModifications(Replace) invoking setRecord(...): " + e.toString());
e.printStackTrace();
// Set error status code for this item
cmd.put(TAG_STATUS, String.valueOf(STATUS_CODE_ERROR));
}
finally {
// Add the status command for this item to the list
addStatusCommand(TAG_REPLACE, msgId, cmd);
}
ret = true;
}
// Delete
for (int i=0, l = deleteTags.size(); i < l; i++) {
String key = getXMLTagValue((String) deleteTags.elementAt(i), TAG_LOC_URI );
String cmdId = getXMLTagValue((String) deleteTags.elementAt(i), TAG_CMDID );
record = new Record();
record.setKey(key);
// Set cmd info for the status handling
cmd = new Hashtable();
cmd.put(TAG_CMDID, cmdId);
cmd.put(TAG_LOC_URI, key);