Esempio n. 1
0
        JSONData ToJSON(List <E> data)
        {
            var      props  = typeof(E).GetProperties();
            JSONData result = new JSONData();

            if (data != null && data.Any())
            {
                foreach (var row in data)
                {
                    JSONEntry entry = new JSONEntry();
                    foreach (var col in props)
                    {
                        JSONValue val = new JSONValue();
                        val.column = col.Name;
                        val.value  = col.GetValue(row);
                        entry.values.Add(val);
                    }
                    result.data.Add(entry);
                }
                return(result);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 2
0
        // Adds new JSON entry to database.

        public static void Create(string jsonString)
        {
            JSONEntry jsonEntry = new JSONEntry();

            jsonEntry.content = jsonString;
            db.Entries.Add(jsonEntry);
            db.SaveChangesAsync();
        }
Esempio n. 3
0
        void processSyncCommand(String strCmd, JSONEntry oCmdEntry, boolean bCheckUIRequest)
        {
            JSONStructIterator objIter = new JSONStructIterator(oCmdEntry);

            for ( ; !objIter.isEnd() && getSync().isContinueSync(); objIter.next())
            {
                String             strObject = objIter.getCurKey();
                JSONStructIterator attrIter  = new JSONStructIterator(objIter.getCurValue());

                try
                {
                    if (m_bSchemaSource)
                    {
                        processServerCmd_Ver3_Schema(strCmd, strObject, attrIter);
                    }
                    else
                    {
                        for ( ; !attrIter.isEnd() && getSync().isContinueSync(); attrIter.next())
                        {
                            String strAttrib = attrIter.getCurKey();
                            String strValue  = attrIter.getCurString();

                            processServerCmd_Ver3(strCmd, strObject, strAttrib, strValue);
                        }
                    }
                }catch (DBException exc)
                {
                    LOG.ERROR("Sync of server changes failed for " + getName() + ";object: " + strObject, exc);
                }

                if (getSyncType().compareTo("none") == 0)
                {
                    continue;
                }

                if (bCheckUIRequest)
                {
                    int nSyncObjectCount = getNotify().incLastSyncObjectCount(getID());
                    if (getProgressStep() > 0 && (nSyncObjectCount % getProgressStep() == 0))
                    {
                        getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, "");
                    }

                    if (getDB().isUIWaitDB())
                    {
                        LOG.INFO("Commit transaction because of UI request.");
                        getDB().endTransaction();
                        SyncThread.sleep(1000);
                        getDB().startTransaction();
                    }
                }
            }
        }
Esempio n. 4
0
        String requestClientIDByNet()
        {
            //String strBody = "";
            //TODO: send client register info in client create
            //        if ( ClientRegister.getInstance() != null )
            //            strBody += ClientRegister.getInstance().getRegisterBody();

            NetResponse resp = getNetClientID().pullData(getProtocol().getClientCreateUrl(), this);

            if (resp.isOK() && resp.getCharData() != null)
            {
                String szData = resp.getCharData();

                JSONEntry oJsonEntry = new JSONEntry(szData);

                //if (oJsonEntry.hasName("sources") )
                //    processServerSources(szData);

                JSONEntry oJsonObject = oJsonEntry.getEntry("client");
                if (!oJsonObject.isEmpty())
                {
                    return(oJsonObject.getString("client_id"));
                }
            }
            else
            {
                m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                if (m_nErrCode == RhoAppAdapter.ERR_NONE)
                {
                    m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                    m_strError = resp.getCharData();
                }
            }

            return("");
        }
Esempio n. 5
0
        public void applyChangedValues()
        {
            String strBody = makePushBody_Ver3("create", false);

            if (strBody != null && strBody.length() > 0)
            {
                JSONEntry oEntry = new JSONEntry(strBody);
                processSyncCommand("insert", oEntry, false);
            }

            strBody = makePushBody_Ver3("delete", false);
            if (strBody != null && strBody.length() > 0)
            {
                JSONEntry oEntry = new JSONEntry(strBody);
                processSyncCommand("delete", oEntry, false);
            }

            strBody = makePushBody_Ver3("update", false);
            if (strBody != null && strBody.length() > 0)
            {
                JSONEntry oEntry = new JSONEntry(strBody);
                processSyncCommand("insert", oEntry, false);
            }
        }
Esempio n. 6
0
        void loadBulkPartition(String strPartition)
        {
            DBAdapter dbPartition = getDB(strPartition);
            String    serverUrl = RhoConf.getInstance().getPath("syncserver");
            String    strUrl = serverUrl + "bulk_data";
            String    strQuery = "?client_id=" + m_clientID + "&partition=" + strPartition;
            String    strDataUrl = "", strCmd = "", strCryptKey = "";

            getNotify().fireBulkSyncNotification(false, "start", strPartition, RhoAppAdapter.ERR_NONE);

            while (strCmd.length() == 0 && isContinueSync())
            {
                NetResponse resp = getNet().pullData(strUrl + strQuery, this);
                if (!resp.isOK() || resp.getCharData() == null)
                {
                    LOG.ERROR("Bulk sync failed: server return an error.");
                    stopSync();
                    getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.getErrorFromResponse(resp));
                    return;
                }

                LOG.INFO("Bulk sync: got response from server: " + resp.getCharData());

                String    szData     = resp.getCharData();
                JSONEntry oJsonEntry = new JSONEntry(szData);
                strCmd = oJsonEntry.getString("result");
                if (oJsonEntry.hasName("url"))
                {
                    strDataUrl = oJsonEntry.getString("url");
                }

                if (strCmd.compareTo("wait") == 0)
                {
                    int nTimeout = RhoConf.getInstance().getInt("bulksync_timeout_sec");
                    if (nTimeout == 0)
                    {
                        nTimeout = 5;
                    }

                    SyncThread.getInstance().wait(nTimeout);
                    strCmd = "";
                }
            }

            if (strCmd.compareTo("nop") == 0)
            {
                LOG.INFO("Bulk sync return no data.");
                getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.ERR_NONE);
                return;
            }

            if (!isContinueSync())
            {
                return;
            }

            getNotify().fireBulkSyncNotification(false, "download", strPartition, RhoAppAdapter.ERR_NONE);

            String fDataName     = makeBulkDataFileName(strDataUrl, dbPartition.getDBPath(), "");
            String strZip        = ".rzip";
            String strSqlDataUrl = CFilePath.join(getHostFromUrl(serverUrl), strDataUrl) + strZip;

            LOG.INFO("Bulk sync: download data from server: " + strSqlDataUrl);
            {
                NetResponse resp1 = getNet().pullFile(strSqlDataUrl, fDataName + strZip, this, null);
                if (!resp1.isOK())
                {
                    LOG.ERROR("Bulk sync failed: cannot download database file.");
                    stopSync();
                    getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.getErrorFromResponse(resp1));
                    return;
                }
            }

            if (!isContinueSync())
            {
                return;
            }

            LOG.INFO("Bulk sync: unzip db");

            if (!RHODESAPP().unzip_file(fDataName + strZip))
            {
                CRhoFile.deleteFile(fDataName + strZip);
                LOG.ERROR("Bulk sync failed: cannot unzip database file.");
                stopSync();
                getNotify().fireBulkSyncNotification(true, "", strPartition, RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE);
                return;
            }
            CRhoFile.deleteFile(fDataName + strZip);

            LOG.INFO("Bulk sync: start change db");
            getNotify().fireBulkSyncNotification(false, "change_db", strPartition, RhoAppAdapter.ERR_NONE);

            dbPartition.setBulkSyncDB(fDataName, strCryptKey);
            processServerSources("{\"partition\":\"" + strPartition + "\"}");

            LOG.INFO("Bulk sync: end change db");
            getNotify().fireBulkSyncNotification(false, "", strPartition, RhoAppAdapter.ERR_NONE);
        }
Esempio n. 7
0
        public void processServerResponse_ver3(JSONArrayIterator oJsonArr)
        {
            PROF.START("Data1");

            int nVersion = 0;

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("version"))
            {
                nVersion = oJsonArr.getCurItem().getInt("version");
                oJsonArr.next();
            }

            if (nVersion != getProtocol().getVersion())
            {
                LOG.ERROR("Sync server send data with incompatible version. Client version: " + getProtocol().getVersion() +
                          "; Server response version: " + nVersion + ". Source name: " + getName());
                getSync().stopSync();
                m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                return;
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("token"))
            {
                processToken(oJsonArr.getCurItem().getUInt64("token"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("source"))
            {
                //skip it. it uses in search only
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("count"))
            {
                setCurPageCount(oJsonArr.getCurItem().getInt("count"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("refresh_time"))
            {
                setRefreshTime(oJsonArr.getCurItem().getInt("refresh_time"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("progress_count"))
            {
                //TODO: progress_count
                //setTotalCount(oJsonArr.getCurItem().getInt("progress_count"));
                oJsonArr.next();
            }

            if (!oJsonArr.isEnd() && oJsonArr.getCurItem().hasName("total_count"))
            {
                setTotalCount(oJsonArr.getCurItem().getInt("total_count"));
                oJsonArr.next();
            }

            //if ( getServerObjectsCount() == 0 )
            //    getNotify().fireSyncNotification(this, false, RhoAppAdapter.ERR_NONE, "");

            if (getToken() == 0)
            {
                //oo conflicts
                getDB().executeSQL("DELETE FROM changed_values where source_id=? and sent>=3", getID());
                //
            }

            LOG.INFO("Got " + getCurPageCount() + "(Processed: " + getServerObjectsCount() + ") records of " + getTotalCount() + " from server. Source: " + getName()
                     + ". Version: " + nVersion);

            PROF.STOP("Data1");
            if (!oJsonArr.isEnd() && getSync().isContinueSync())
            {
                JSONEntry oCmds = oJsonArr.getCurItem();
                PROF.START("Data");

                if (oCmds.hasName("schema-changed"))
                {
                    getSync().setSchemaChanged(true);
                }
                else if (!processServerErrors(oCmds))
                {
                    getDB().startTransaction();

                    if (SyncEngine.getSourceOptions().getBoolProperty(getID(), "pass_through"))
                    {
                        if (m_bSchemaSource)
                        {
                            getDB().executeSQL("DELETE FROM " + getName());
                        }
                        else
                        {
                            getDB().executeSQL("DELETE FROM object_values WHERE source_id=?", getID());
                        }
                    }

                    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"), true);
                    }
                    if (oCmds.hasName("delete") && getSync().isContinueSync())
                    {
                        processSyncCommand("delete", oCmds.getEntry("delete"), true);
                    }
                    if (oCmds.hasName("insert") && getSync().isContinueSync())
                    {
                        processSyncCommand("insert", oCmds.getEntry("insert"), true);
                    }

                    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, "");
            }
            PROF.STOP("Data1");
        }
Esempio n. 8
0
        //{"create-error":{"0_broken_object_id":{"name":"wrongname","an_attribute":"error create"},"0_broken_object_id-error":{"message":"error create"}}}
        boolean processServerErrors(JSONEntry oCmds)
        {
            String[] arErrTypes = { "source-error", "search-error", "create-error", "update-error", "delete-error", null };
            boolean  bRes       = false;

            for (int i = 0; ; i++)
            {
                if (arErrTypes[i] == null)
                {
                    break;
                }
                if (!oCmds.hasName(arErrTypes[i]))
                {
                    continue;
                }

                bRes       = true;
                m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;

                JSONEntry          errSrc  = oCmds.getEntry(arErrTypes[i]);
                JSONStructIterator errIter = new JSONStructIterator(errSrc);
                for ( ; !errIter.isEnd(); errIter.next())
                {
                    String strKey = errIter.getCurKey();

                    if (i == 0 || i == 1)      //"source-error", "search-error"
                    {
                        if (errIter.getCurValue().hasName("message"))
                        {
                            if (m_strServerError.length() > 0)
                            {
                                m_strServerError += "&";
                            }

                            m_strServerError += "server_errors[" + URI.urlEncode(strKey) + "][message]=" + URI.urlEncode(errIter.getCurValue().getString("message"));
                        }
                    }
                    else
                    {
                        //"create-error", "update-error", "delete-error"
                        String strObject = strKey;

                        if (strObject.endsWith("-error"))
                        {
                            strObject = strObject.substring(0, strKey.length() - 6);
                            if (m_strServerError.length() > 0)
                            {
                                m_strServerError += "&";
                            }

                            m_strServerError += "server_errors[" + arErrTypes[i] + "][" + URI.urlEncode(strObject) + "][message]=" + URI.urlEncode(errIter.getCurValue().getString("message"));
                        }
                        else
                        {
                            JSONStructIterator attrIter = new JSONStructIterator(errIter.getCurValue());
                            for ( ; !attrIter.isEnd(); attrIter.next())
                            {
                                String strAttrName  = attrIter.getCurKey();
                                String strAttrValue = attrIter.getCurString();

                                if (m_strServerError.length() > 0)
                                {
                                    m_strServerError += "&";
                                }

                                m_strServerError += "server_errors[" + arErrTypes[i] + "][" + URI.urlEncode(strObject) + "][attributes][" + URI.urlEncode(strAttrName) + "]=" + URI.urlEncode(strAttrValue);
                            }
                        }
                    }
                }
            }

            return(bRes);
        }
Esempio n. 9
0
        //{"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);
        }
Esempio n. 10
0
        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();
        }