platform/bb/RubyVM/src/com/rho/sync/SyncSource.java in rhodes-2.2.3 vs platform/bb/RubyVM/src/com/rho/sync/SyncSource.java in rhodes-2.2.4.beta.1

- old
+ new

@@ -284,11 +284,11 @@ boolean bSend = false; int i = 0; for( i = 0; i < 3 && getSync().isContinueSync(); i++ ) { String strBody1; - strBody1 = makePushBody_Ver3(arUpdateTypes[i]); + strBody1 = makePushBody_Ver3(arUpdateTypes[i], true); if (strBody1.length() > 0) { strBody += "," + strBody1; String strBlobAttrs = ""; @@ -358,11 +358,11 @@ //{"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)throws DBException + String makePushBody_Ver3( String strUpdateType, boolean isSync)throws DBException { String strBody = ""; getDB().Lock(); 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 ); @@ -392,11 +392,16 @@ m_arBlobAttrs.addElement(strAttrib); m_arMultipartItems.addElement(oItem); } if ( strBody.length() == 0 ) - strBody += "\"" + strUpdateType + "\":{"; + { + if ( !isSync ) + strBody += "{"; + else + strBody += "\"" + strUpdateType + "\":{"; + } if ( strObject.compareTo(strCurObject) != 0 ) { if ( strCurObject.length() > 0 ) { @@ -429,16 +434,42 @@ strBody += "}"; strBody += "}"; } - getDB().executeSQL("UPDATE changed_values SET sent=1 WHERE source_id=? and update_type=? and sent=0", getID(), strUpdateType ); + 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 applyChangedValues()throws Exception + { + String strBody = makePushBody_Ver3("create", false); + if ( strBody != null && strBody.length() > 0 ) + { + JSONEntry oEntry = new JSONEntry(strBody); + processSyncCommand("insert", oEntry ); + } + + strBody = makePushBody_Ver3("delete", false); + if ( strBody != null && strBody.length() > 0 ) + { + JSONEntry oEntry = new JSONEntry(strBody); + processSyncCommand("delete", oEntry ); + } + + strBody = makePushBody_Ver3("update", false); + if ( strBody != null && strBody.length() > 0 ) + { + JSONEntry oEntry = new JSONEntry(strBody); + processSyncCommand("insert", oEntry ); + } + } + void syncServerChanges()throws Exception { LOG.INFO("Sync server changes source ID :" + getID() ); while( getSync().isContinueSync() ) @@ -567,30 +598,37 @@ if ( !oJsonArr.isEnd() && getSync().isContinueSync() ) { JSONEntry oCmds = oJsonArr.getCurItem(); PROF.START("Data"); - getDB().startTransaction(); - if ( oCmds.hasName("metadata") && getSync().isContinueSync() ) + if ( oCmds.hasName("schema-changed") ) { - String strMetadata = oCmds.getString("metadata"); - getDB().executeSQL("UPDATE sources SET metadata=? WHERE source_id=?", strMetadata, getID() ); + getSync().stopSync(); + getSync().setSchemaChanged(true); + }else + { + getDB().startTransaction(); + if ( oCmds.hasName("metadata") && getSync().isContinueSync() ) + { + String strMetadata = oCmds.getString("metadata"); + getDB().executeSQL("UPDATE sources SET metadata=? WHERE source_id=?", strMetadata, getID() ); + } + if ( oCmds.hasName("links") && getSync().isContinueSync() ) + processSyncCommand("links", oCmds.getEntry("links") ); + if ( oCmds.hasName("delete") && getSync().isContinueSync() ) + processSyncCommand("delete", oCmds.getEntry("delete") ); + if ( oCmds.hasName("insert") && getSync().isContinueSync() ) + processSyncCommand("insert", oCmds.getEntry("insert") ); + + PROF.STOP("Data"); + + PROF.START("DB"); + getDB().endTransaction(); + PROF.STOP("DB"); + + getNotify().fireObjectsNotification(); } - if ( oCmds.hasName("links") && getSync().isContinueSync() ) - processSyncCommand("links", oCmds.getEntry("links") ); - if ( oCmds.hasName("delete") && getSync().isContinueSync() ) - processSyncCommand("delete", oCmds.getEntry("delete") ); - if ( oCmds.hasName("insert") && getSync().isContinueSync() ) - processSyncCommand("insert", oCmds.getEntry("insert") ); - - PROF.STOP("Data"); - - PROF.START("DB"); - getDB().endTransaction(); - PROF.STOP("DB"); - - getNotify().fireObjectsNotification(); } PROF.START("Data1"); if ( getCurPageCount() > 0 ) getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, ""); @@ -616,10 +654,13 @@ processServerCmd_Ver3(strCmd,strObject,strAttrib,strValue); } } + if ( getSyncType().compareTo("none") == 0 ) + continue; + int nSyncObjectCount = getNotify().incLastSyncObjectCount(getID()); if ( getProgressStep() > 0 && (nSyncObjectCount%getProgressStep() == 0) ) getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, ""); if ( getDB().isUIWaitDB() ) @@ -705,20 +746,25 @@ { String strSqlUpdate = "UPDATE "; strSqlUpdate += getName() + " SET " + strSet + " WHERE object=?"; getDB().executeSQLEx(strSqlUpdate, vecValues); - // oo conflicts - for( int i = 0; i < (int)vecAttrs.size(); i++ ) + if ( getSyncType().compareTo("none") != 0 ) { - getDB().executeSQL("UPDATE changed_values SET sent=4 where object=? and attrib=? and source_id=? and sent>1", - strObject, vecAttrs.elementAt(i), getID() ); + // oo conflicts + for( int i = 0; i < (int)vecAttrs.size(); i++ ) + { + getDB().executeSQL("UPDATE changed_values SET sent=4 where object=? and attrib=? and source_id=? and sent>1", + strObject, vecAttrs.elementAt(i), getID() ); + } + // } - // } - getNotify().onObjectChanged(getID(),strObject, SyncNotify.enUpdate); + if ( getSyncType().compareTo("none") != 0 ) + getNotify().onObjectChanged(getID(),strObject, SyncNotify.enUpdate); + m_nInserted++; }else if (strCmd.compareTo("delete") == 0) { Vector/*<String>*/ vecAttrs = new Vector(); String strSet = ""; @@ -760,19 +806,22 @@ String strDelete = "DELETE FROM " + getName() + " WHERE object=?"; getDB().executeSQL( strDelete, strObject); } } - getNotify().onObjectChanged(getID(), strObject, SyncNotify.enDelete); - // oo conflicts - for( int i = 0; i < (int)vecAttrs.size(); i++ ) + if ( getSyncType().compareTo("none") != 0 ) { - getDB().executeSQL("UPDATE changed_values SET sent=3 where object=? and attrib=? and source_id=?", - strObject, vecAttrs.elementAt(i), getID() ); + getNotify().onObjectChanged(getID(), strObject, SyncNotify.enDelete); + // oo conflicts + for( int i = 0; i < (int)vecAttrs.size(); i++ ) + { + getDB().executeSQL("UPDATE changed_values SET sent=3 where object=? and attrib=? and source_id=?", + strObject, vecAttrs.elementAt(i), getID() ); + } + // } - // - + m_nDeleted++; }else if ( strCmd.compareTo("links") == 0 ) { String strValue = attrIter.getCurString(); processAssociations(strObject, strValue); @@ -858,26 +907,34 @@ { getDB().executeSQL("UPDATE object_values " + "SET value=? WHERE object=? and attrib=? and source_id=?", oAttrValue.m_strValue, strObject, oAttrValue.m_strAttrib, getID() ); - // oo conflicts - getDB().executeSQL("UPDATE changed_values SET sent=4 where object=? and attrib=? and source_id=? and sent>1", - strObject, oAttrValue.m_strAttrib, getID() ); - // + if ( getSyncType().compareTo("none") != 0 ) + { + // oo conflicts + getDB().executeSQL("UPDATE changed_values SET sent=4 where object=? and attrib=? and source_id=? and sent>1", + strObject, oAttrValue.m_strAttrib, getID() ); + // + } } - getNotify().onObjectChanged(getID(),strObject, SyncNotify.enUpdate); + if ( getSyncType().compareTo("none") != 0 ) + getNotify().onObjectChanged(getID(),strObject, SyncNotify.enUpdate); + m_nInserted++; }else if (strCmd.compareTo("delete") == 0) { getDB().executeSQL("DELETE FROM object_values where object=? and attrib=? and source_id=?", strObject, oAttrValue.m_strAttrib, getID() ); - getNotify().onObjectChanged(getID(), strObject, SyncNotify.enDelete); - // oo conflicts - getDB().executeSQL("UPDATE changed_values SET sent=3 where object=? and attrib=? and source_id=?", strObject, oAttrValue.m_strAttrib, getID() ); - // - + + if ( getSyncType().compareTo("none") != 0 ) + { + getNotify().onObjectChanged(getID(), strObject, SyncNotify.enDelete); + // oo conflicts + getDB().executeSQL("UPDATE changed_values SET sent=3 where object=? and attrib=? and source_id=?", strObject, oAttrValue.m_strAttrib, getID() ); + // + } m_nDeleted++; }else if ( strCmd.compareTo("links") == 0 ) { processAssociations(strObject, oAttrValue.m_strValue); @@ -889,10 +946,46 @@ } private String makeFileName(CAttrValue value)throws Exception { + String strExt = ""; + + URI uri = new URI(value.m_strValue); + String strQuest = uri.getQueryString(); + + if (strQuest != null && strQuest.length() > 0) + { + int nExt = strQuest.indexOf("extension="); + if ( nExt >= 0 ) + { + int nExtEnd = strQuest.indexOf("&", nExt); + if (nExtEnd < 0 ) + nExtEnd = strQuest.length(); + + strExt = strQuest.substring(nExt+10, nExtEnd); + } + } + + if ( strExt.length() == 0 ) + { + String strFileName = uri.getLastNamePart(); + int nExt = strFileName != null ? strFileName.lastIndexOf('.') : -1; + if ( nExt >= 0 ) + strExt = strFileName.substring(nExt); + } + + if ( strExt.length() == 0 ) + strExt = ".bin"; + else if ( strExt.charAt(0) != '.' ) + strExt = "." + strExt; + + String fName = RhodesApp.getInstance().getBlobsDirPath() + "/id_" + TimeInterval.getCurrentTime().toULong() + strExt; + + return fName; + +/* String strExt = ".bin"; URI uri = new URI(value.m_strValue); int nDot = uri.getPath().lastIndexOf('.'); if ( nDot >= 0 ) strExt = uri.getPath().substring(nDot); @@ -907,10 +1000,10 @@ } } String fName = RhodesApp.getInstance().getBlobsDirPath() + "/id_" + TimeInterval.getCurrentTime().toULong() + strExt; - return fName; + return fName;*/ } boolean downloadBlob(CAttrValue value)throws Exception { String fName = makeFileName( value );