void loadAllSources() { if (isNoThreadedMode()) { RhoRuby.loadAllSyncSources(); } else { getNet().pushData(getNet().resolveUrl("/system/loadallsyncsources"), "", null); } m_sources.removeAllElements(); Vector <String> arPartNames = DBAdapter.getDBAllPartitionNames(); for (int i = 0; i < (int)arPartNames.size(); i++) { DBAdapter dbPart = DBAdapter.getDB((String)arPartNames.elementAt(i)); IDBResult res = dbPart.executeSQL("SELECT source_id,sync_type,name from sources ORDER BY sync_priority"); for ( ; !res.isEnd(); res.next()) { String strShouldSync = res.getStringByIdx(1); if (strShouldSync.compareTo("none") == 0) { continue; } String strName = res.getStringByIdx(2); m_sources.addElement(new SyncSource(res.getIntByIdx(0), strName, strShouldSync, dbPart, this)); } } checkSourceAssociations(); }
private boolean doRegister(SyncEngine oSync) { String session = oSync.loadSession(); if (session == null || session.length() == 0) { m_nPollInterval = POLL_INTERVAL_INFINITE; return(false); } m_nPollInterval = POLL_INTERVAL_SECONDS; String client_id = oSync.loadClientID(); if (client_id == null || client_id.length() == 0) { return(false); } IDBResult res = DBAdapter.getUserDB().executeSQL("SELECT token,token_sent from client_info"); if (!res.isEnd()) { String token = res.getStringByIdx(0); boolean token_sent = res.getIntByIdx(1) > 0 && !RHOCONF().getBool("register_push_at_startup"); if (m_strDevicePin.equals(token) && token_sent) { //token in db same as new one and it was already send to the server //so we do nothing return(true); } } String strBody = getRegisterBody(client_id); NetResponse resp = getNet().pushData(oSync.getProtocol().getClientRegisterUrl(), strBody, oSync); if (resp.isOK()) { try { DBAdapter.getUserDB().executeSQL("UPDATE client_info SET token_sent=?, token=?", 1, m_strDevicePin); } catch (Exception ex) { LOG.ERROR("Error saving token_sent to the DB...", ex); } LOG.INFO("Registered client sucessfully..."); return(true); } else { LOG.INFO("Network error POST-ing device pin to the server..."); } return(false); }
public String loadClientID() { String clientID = ""; lock ( m_mxLoadClientID ) { boolean bResetClient = false; { IDBResult res = getUserDB().executeSQL("SELECT client_id,reset from client_info"); if (!res.isEnd()) { clientID = res.getStringByIdx(0); bResetClient = res.getIntByIdx(1) > 0; } } if (clientID.length() == 0) { clientID = requestClientIDByNet(); IDBResult res = getUserDB().executeSQL("SELECT * FROM client_info"); if (!res.isEnd()) { getUserDB().executeSQL("UPDATE client_info SET client_id=?", clientID); } else { getUserDB().executeSQL("INSERT INTO client_info (client_id) values (?)", clientID); } if (ClientRegister.getInstance() != null) { ClientRegister.getInstance().startUp(); } } else if (bResetClient) { if (!resetClientIDByNet(clientID)) { stopSync(); } else { getUserDB().executeSQL("UPDATE client_info SET reset=? where client_id=?", 0, clientID); } } } return(clientID); }
static void loadAttrs(DBAdapter db, Hashtable <int, Hashtable <String, int> > mapAttrs, String strDBAttr, Hashtable <String, int> mapSrcNames) { mapAttrs.clear(); String strSql = "SELECT source_id,"; strSql += strDBAttr + ",name from sources"; IDBResult res = db.executeSQL(strSql); for ( ; !res.isEnd(); res.next()) { int nSrcID = res.getIntByIdx(0); String strAttribs = res.getStringByIdx(1); if (strAttribs.length() == 0) { continue; } Tokenizer oTokenizer = new Tokenizer(strAttribs, ","); Hashtable <String, int> mapAttr = new Hashtable <String, int>(); String strAttr = ""; while (oTokenizer.hasMoreTokens()) { String tok = oTokenizer.nextToken(); if (tok.length() == 0) { continue; } if (strAttr.length() > 0) { mapAttr.put(strAttr, int.Parse(tok)); strAttr = ""; } else { strAttr = tok; } } mapAttrs.put(nSrcID, mapAttr); if (mapSrcNames != null) { mapSrcNames.put(res.getStringByIdx(2).toUpperCase(), nSrcID); } } }
public void applyChangedValues(DBAdapter db) { IDBResult resSrc = db.executeSQL("SELECT DISTINCT(source_id) FROM changed_values"); for ( ; !resSrc.isEnd(); resSrc.next()) { int nSrcID = resSrc.getIntByIdx(0); IDBResult res = db.executeSQL("SELECT source_id,sync_type,name, partition from sources WHERE source_id=?", nSrcID); if (res.isEnd()) { continue; } SyncSource src = new SyncSource(res.getIntByIdx(0), res.getStringByIdx(2), "none", db, this); src.applyChangedValues(); } }
void copyChangedValues(DBAdapter db) { updateAllAttribChanges(); copyTable("changed_values", m_dbStorage, db.m_dbStorage); { Vector <int> arOldSrcs = new Vector <int>(); { IDBResult resSrc = db.executeSQL("SELECT DISTINCT(source_id) FROM changed_values"); for ( ; !resSrc.isEnd(); resSrc.next()) { arOldSrcs.addElement(resSrc.getIntByIdx(0)); } } for (int i = 0; i < arOldSrcs.size(); i++) { int nOldSrcID = arOldSrcs.elementAt(i); IDBResult res = executeSQL("SELECT name from sources WHERE source_id=?", nOldSrcID); if (!res.isEnd()) { String strSrcName = res.getStringByIdx(0); IDBResult res2 = db.executeSQL("SELECT source_id from sources WHERE name=?", strSrcName); if (!res2.isEnd()) { if (nOldSrcID != res2.getIntByIdx(0)) { db.executeSQL("UPDATE changed_values SET source_id=? WHERE source_id=?", res2.getIntByIdx(0), nOldSrcID); } continue; } } //source not exist in new partition, remove this changes db.executeSQL("DELETE FROM changed_values WHERE source_id=?", nOldSrcID); } } }
public void updateAllAttribChanges() { //Check for attrib = object IDBResult res = executeSQL("SELECT object, source_id, update_type " + "FROM changed_values where attrib = 'object' and sent=0"); if (res.isEnd()) { return; } startTransaction(); Vector <String> arObj = new Vector <String>(), arUpdateType = new Vector <String>(); Vector <int> arSrcID = new Vector <int>(); for ( ; !res.isEnd(); res.next()) { arObj.addElement(res.getStringByIdx(0)); arSrcID.addElement(res.getIntByIdx(1)); arUpdateType.addElement(res.getStringByIdx(2)); } for (int i = 0; i < (int)arObj.size(); i++) { IDBResult resSrc = executeSQL("SELECT name, schema FROM sources where source_id=?", arSrcID.elementAt(i)); boolean bSchemaSource = false; String strTableName = "object_values"; if (!resSrc.isEnd()) { bSchemaSource = resSrc.getStringByIdx(1).length() > 0; if (bSchemaSource) { strTableName = resSrc.getStringByIdx(0); } } if (bSchemaSource) { IDBResult res2 = executeSQL("SELECT * FROM " + strTableName + " where object=?", arObj.elementAt(i)); for (int j = 0; j < res2.getColCount(); j++) { String strAttrib = res2.getColName(j); String value = res2.getStringByIdx(j); String attribType = getAttrMgr().isBlobAttr(arSrcID.elementAt(i), strAttrib) ? "blob.file" : ""; executeSQLReportNonUnique("INSERT INTO changed_values (source_id,object,attrib,value,update_type,attrib_type,sent) VALUES(?,?,?,?,?,?,?)", arSrcID.elementAt(i), arObj.elementAt(i), strAttrib, value, arUpdateType.elementAt(i), attribType, 0); } } else { IDBResult res2 = executeSQL("SELECT attrib, value FROM " + strTableName + " where object=? and source_id=?", arObj.elementAt(i), arSrcID.elementAt(i)); for ( ; !res2.isEnd(); res2.next()) { String strAttrib = res2.getStringByIdx(0); String value = res2.getStringByIdx(1); String attribType = getAttrMgr().isBlobAttr(arSrcID.elementAt(i), strAttrib) ? "blob.file" : ""; executeSQLReportNonUnique("INSERT INTO changed_values (source_id,object,attrib,value,update_type,attrib_type,sent) VALUES(?,?,?,?,?,?,?)", arSrcID.elementAt(i), arObj.elementAt(i), strAttrib, value, arUpdateType.elementAt(i), attribType, 0); } } } executeSQL("DELETE FROM changed_values WHERE attrib='object'"); endTransaction(); }
private void processDelete(String tableName, IDBResult rows2Delete, int[] cols) { if (tableName.equalsIgnoreCase("changed_values") || tableName.equalsIgnoreCase("sources") || tableName.equalsIgnoreCase("client_info")) { return; } boolean bProcessTable = tableName.equalsIgnoreCase("object_values"); boolean bSchemaSrc = false; int nSrcID = 0; if (!bProcessTable) { nSrcID = m_db.getAttrMgr().getSrcIDHasBlobsByName(tableName); bProcessTable = nSrcID != 0; bSchemaSrc = bProcessTable; } if (!bProcessTable) { return; } if (!bSchemaSrc && !isChangedCol(cols, 3)) //value { return; } for ( ; !rows2Delete.isEnd(); rows2Delete.next()) { if (!bSchemaSrc) { nSrcID = rows2Delete.getIntByIdx(0); String attrib = rows2Delete.getStringByIdx(1); String value = rows2Delete.getStringByIdx(3); //if (cols == null) //delete // m_db.getAttrMgr().remove(nSrcID, attrib); if (m_db.getAttrMgr().isBlobAttr(nSrcID, attrib)) { processBlobDelete(nSrcID, attrib, value); } } else { Object[] data = rows2Delete.getCurData(); for (int i = 0; i < rows2Delete.getColCount(); i++) { if (!isChangedCol(cols, i)) { continue; } String attrib = rows2Delete.getColName(i); if (!(data[i] is String)) { continue; } String value = (String)data[i]; if (m_db.getAttrMgr().isBlobAttr(nSrcID, attrib)) { processBlobDelete(nSrcID, attrib, value); } } } } }
private void processDelete(String tableName, IDBResult rows2Delete, int[] cols) { if ( tableName.equalsIgnoreCase("changed_values") || tableName.equalsIgnoreCase("sources") || tableName.equalsIgnoreCase("client_info")) return; boolean bProcessTable = tableName.equalsIgnoreCase("object_values"); boolean bSchemaSrc = false; int nSrcID = 0; if ( !bProcessTable ) { nSrcID = m_db.getAttrMgr().getSrcIDHasBlobsByName(tableName); bProcessTable = nSrcID != 0; bSchemaSrc = bProcessTable; } if ( !bProcessTable) return; if ( !bSchemaSrc && !isChangedCol(cols, 3))//value return; for( ; !rows2Delete.isEnd(); rows2Delete.next() ) { if ( !bSchemaSrc ) { nSrcID = rows2Delete.getIntByIdx(0); String attrib = rows2Delete.getStringByIdx(1); String value = rows2Delete.getStringByIdx(3); //if (cols == null) //delete // m_db.getAttrMgr().remove(nSrcID, attrib); if ( m_db.getAttrMgr().isBlobAttr(nSrcID, attrib) ) processBlobDelete(nSrcID, attrib, value); }else { Object[] data = rows2Delete.getCurData(); for ( int i = 0; i < rows2Delete.getColCount(); i++ ) { if (!isChangedCol(cols, i)) continue; String attrib = rows2Delete.getColName(i); if ( !(data[i] is String ) ) continue; String value = (String)data[i]; if ( m_db.getAttrMgr().isBlobAttr(nSrcID, attrib) ) processBlobDelete(nSrcID, attrib, value); } } } }