Beispiel #1
0
        boolean downloadBlob(CAttrValue value)
        {
            String fName  = makeFileName(value);
            String url    = value.m_strValue;
            int    nQuest = url.lastIndexOf('?');

            if (nQuest > 0)
            {
                url += "&";
            }
            else
            {
                url += "?";
            }
            url += "client_id=" + getSync().getClientID();

            try{
                NetResponse resp = getNet().pullFile(url, fName, getSync(), null);
                if (!resp.isOK())
                {
                    getSync().stopSync();
                    m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                    return(false);
                }
            }catch (Exception exc)
            {
                m_nErrCode = RhoAppAdapter.getNetErrorCode(exc);
                throw exc;
            }

            value.m_strValue = CFilePath.getRelativePath(fName, CRhodesApp.getRhoRootPath());

            return(true);
        }
Beispiel #2
0
        public void login(String name, String password, SyncNotify.SyncNotification oNotify)
        {
            try {
                /*
                 *                      processServerSources("{\"sources\":{ \"ProductEx\":{ "+
                 *              "\"sync_type\":\"incremental\", \"partition\":\"application\", \"source_id\":\"7\","+
                 *              " \"sync_priority\":\"0\", \"model_type\":\"fixed_schema\", "+
                 *              " \"schema\":{\"version\":\"1.1\", \"property\":{\"brand\":\"string\", \"price\":\"string\", \"quantity\":\"string\", \"name\":\"string\", "+
                 *              " \"image_url\":\"blob\", \"image_url_ex\":\"blob,overwrite\"}, "+
                 *              " \"index\":[{\"by_brand_price1\":\"brand,price\"}, {\"by_quantity1\":\"quantity\"}], \"unique_index\":[{\"by_name1\":\"name\"}]}, "+
                 *              " \"belongs_to\":{\"brand\":\"Customer\"}}}}");//, \"schema_version\":\"1.0\"
                 */
                NetResponse resp = null;
                m_bStopByUser = false;

                try{
                    resp = getNet().pullCookies(getProtocol().getLoginUrl(), getProtocol().getLoginBody(name, password), this);
                    int nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                    if (nErrCode != RhoAppAdapter.ERR_NONE)
                    {
                        getNotify().callLoginCallback(oNotify, nErrCode, resp.getCharData());
                        return;
                    }
                }catch (Exception exc)
                {
                    LOG.ERROR("Login failed.", exc);
                    getNotify().callLoginCallback(oNotify, RhoAppAdapter.getNetErrorCode(exc), "");
                    return;
                }

                String strSession = resp.getCharData();
                if (strSession == null || strSession.length() == 0)
                {
                    LOG.ERROR("Return empty session.");
                    getNotify().callLoginCallback(oNotify, RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE, "");
                    return;
                }

                if (isStoppedByUser())
                {
                    return;
                }

                IDBResult res = getUserDB().executeSQL("SELECT * FROM client_info");
                if (!res.isEnd())
                {
                    getUserDB().executeSQL("UPDATE client_info SET session=?", strSession);
                }
                else
                {
                    getUserDB().executeSQL("INSERT INTO client_info (session) values (?)", strSession);
                }

                if (RHOCONF().isExist("rho_sync_user"))
                {
                    String strOldUser = RHOCONF().getString("rho_sync_user");
                    if (name.compareTo(strOldUser) != 0)
                    {
                        if (isNoThreadedMode())
                        {
                            RhoRuby.resetDBOnSyncUserChanged();
                        }
                        else
                        {
                            NetResponse resp1 = getNet().pushData(getNet().resolveUrl("/system/resetDBOnSyncUserChanged"), "", null);
                        }
                    }
                }
                RHOCONF().setString("rho_sync_user", name, true);

                getNotify().callLoginCallback(oNotify, RhoAppAdapter.ERR_NONE, "");

                if (ClientRegister.getInstance() != null)
                {
                    getUserDB().executeSQL("UPDATE client_info SET token_sent=?", 0);
                    ClientRegister.getInstance().startUp();
                }
            }catch (Exception exc)
            {
                LOG.ERROR("Login failed.", exc);
                getNotify().callLoginCallback(oNotify, RhoAppAdapter.ERR_RUNTIME, "");
            }
        }
Beispiel #3
0
        void syncServerChanges()
        {
            LOG.INFO("Sync server changes source ID :" + getID());

            while (getSync().isContinueSync())
            {
                setCurPageCount(0);
                String strUrl   = getProtocol().getServerQueryUrl("");
                String strQuery = getProtocol().getServerQueryBody(getName(), getSync().getClientID(), getSync().getSyncPageSize());

                if (!m_bTokenFromDB && getToken() > 1)
                {
                    strQuery += "&token=" + getToken();
                }

                LOG.INFO("Pull changes from server. Url: " + (strUrl + strQuery));

                NetResponse resp = null;
                try{
                    PROF.START("Net");
                    resp = getNet().pullData(strUrl + strQuery, getSync());
                    PROF.STOP("Net");

                    if (!resp.isOK())
                    {
                        getSync().stopSync();
                        m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                        m_strError = resp.getCharData();
                        continue;
                    }
                }catch (Exception exc)
                {
                    m_nErrCode = RhoAppAdapter.getNetErrorCode(exc);
                    throw exc;
                }

                String szData      = null;
                String strTestResp = SyncEngine.getSourceOptions().getProperty(getID(), "rho_server_response");
                if (strTestResp != null && strTestResp.length() > 0)
                {
                    szData = strTestResp;
                }
                else
                {
                    szData = resp.getCharData();
                }

                PROF.START("Parse");
                JSONArrayIterator oJsonArr = new JSONArrayIterator(szData);
                PROF.STOP("Parse");

                processServerResponse_ver3(oJsonArr);

                if (SyncEngine.getSourceOptions().getBoolProperty(getID(), "pass_through"))
                {
                    processToken(0);
                }

                if (getToken() == 0)
                {
                    break;
                }
            }

            if (getSync().isSchemaChanged())
            {
                getSync().stopSync();
            }
        }
Beispiel #4
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();
        }