//{"source_name":"SampleAdapter","client_id":1,"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"update":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"delete":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"delete":{"3":{"brand":"HTC","name":"Fuze","price":"299.99"}},"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}},"update":{"2":{"brand":"Android","name":"G2","price":"99.99"}}} String makePushBody_Ver3(String strUpdateType, boolean isSync) { String strBody = ""; getDB().Lock(); if (isSync) { getDB().updateAllAttribChanges(); } IDBResult res = getDB().executeSQL("SELECT attrib, object, value, attrib_type " + "FROM changed_values where source_id=? and update_type =? and sent<=1 ORDER BY object", getID(), strUpdateType); if (res.isEnd()) { res.close(); getDB().Unlock(); return(strBody); } String strCurObject = ""; boolean bFirst = true; for ( ; !res.isEnd(); res.next()) { String strAttrib = res.getStringByIdx(0); String strObject = res.getStringByIdx(1); String value = res.getStringByIdx(2); String attribType = res.getStringByIdx(3); if (attribType.compareTo("blob.file") == 0) { NetRequest.MultipartItem oItem = new NetRequest.MultipartItem(); oItem.m_strFilePath = RHODESAPP().resolveDBFilesPath(value); oItem.m_strContentType = "application/octet-stream"; oItem.m_strName = strAttrib + "-" + strObject; m_arBlobAttrs.addElement(strAttrib); m_arMultipartItems.addElement(oItem); } if (strBody.length() == 0) { if (!isSync) { strBody += "{"; } else { strBody += "\"" + strUpdateType + "\":{"; } } if (strObject.compareTo(strCurObject) != 0) { if (strCurObject.length() > 0) { if (!bFirst) { strBody += "}"; } strBody += ","; } bFirst = true; strBody += JSONEntry.quoteValue(strObject); strCurObject = strObject; } if (!bFirst) { strBody += ","; } if (strAttrib.length() > 0) { if (bFirst) { strBody += ":{"; } strBody += JSONEntry.quoteValue(strAttrib) + ":" + JSONEntry.quoteValue(value); bFirst = false; } } if (strBody.length() > 0) { if (!bFirst) { strBody += "}"; } strBody += "}"; } if (isSync) { getDB().executeSQL("UPDATE changed_values SET sent=1 WHERE source_id=? and update_type=? and sent=0", getID(), strUpdateType); } getDB().Unlock(); return(strBody); }
void doSyncClientChanges() { String[] arUpdateTypes = { "create", "update", "delete" }; boolean[] arUpdateSent = { false, false, false }; m_arMultipartItems.removeAllElements(); m_arBlobAttrs.removeAllElements(); String strBody = "{\"source_name\":" + JSONEntry.quoteValue(getName()) + ",\"client_id\":" + JSONEntry.quoteValue(getSync().getClientID()); boolean bSend = false; int i = 0; for (i = 0; i < 3 && getSync().isContinueSync(); i++) { String strBody1; strBody1 = makePushBody_Ver3(arUpdateTypes[i], true); if (strBody1.length() > 0) { strBody += "," + strBody1; String strBlobAttrs = ""; for (int j = 0; j < (int)m_arBlobAttrs.size(); j++) { if (strBlobAttrs.length() > 0) { strBlobAttrs += ","; } strBlobAttrs += JSONEntry.quoteValue((String)m_arBlobAttrs.elementAt(j)); } if (strBlobAttrs.length() > 0) { strBody += ",\"blob_fields\":[" + strBlobAttrs + "]"; } arUpdateSent[i] = true; bSend = true; } } strBody += "}"; if (bSend) { LOG.INFO("Push client changes to server. Source: " + getName() + "Size :" + strBody.length()); LOG.TRACE("Push body: " + strBody); try{ if (m_arMultipartItems.size() > 0) { NetRequest.MultipartItem oItem = new NetRequest.MultipartItem(); oItem.m_strBody = strBody; //oItem.m_strContentType = getProtocol().getContentType(); oItem.m_strName = "cud"; m_arMultipartItems.addElement(oItem); NetResponse resp = getNet().pushMultipartData(getProtocol().getClientChangesUrl(), m_arMultipartItems, getSync(), null); if (!resp.isOK()) { getSync().setState(SyncEngine.esStop); m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER; m_strError = resp.getCharData(); } } else { NetResponse resp = getNet().pushData(getProtocol().getClientChangesUrl(), strBody, getSync()); if (!resp.isOK()) { getSync().setState(SyncEngine.esStop); m_nErrCode = RhoAppAdapter.ERR_REMOTESERVER; m_strError = resp.getCharData(); } } }catch (Exception exc) { m_nErrCode = RhoAppAdapter.getNetErrorCode(exc); throw exc; } } for (i = 0; i < 3 && getSync().isContinueSync(); i++) { if (arUpdateSent[i]) { //oo conflicts if (i < 1) //create { getDB().executeSQL("UPDATE changed_values SET sent=2 WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i]); } else { // getDB().executeSQL("DELETE FROM changed_values WHERE source_id=? and update_type=? and sent=1", getID(), arUpdateTypes[i]); } } } m_arMultipartItems.removeAllElements(); m_arBlobAttrs.removeAllElements(); }