Esempio n. 1
0
        public void doSearch(Vector <String> arSources, String strParams, String strAction, boolean bSearchSyncChanges, int nProgressStep)
        {
            try
            {
                prepareSync(esSearch, null);
                if (!isContinueSync())
                {
                    if (getState() != esExit)
                    {
                        setState(esNone);
                    }

                    return;
                }

                TimeInterval startTime = TimeInterval.getCurrentTime();

                if (bSearchSyncChanges)
                {
                    for (int i = 0; i < (int)arSources.size(); i++)
                    {
                        SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                        if (pSrc != null)
                        {
                            pSrc.syncClientChanges();
                        }
                    }
                }

                while (isContinueSync())
                {
                    int    nSearchCount = 0;
                    String strUrl       = getProtocol().getServerQueryUrl(strAction);
                    String strQuery     = getProtocol().getServerQueryBody("", getClientID(), getSyncPageSize());

                    if (strParams.length() > 0)
                    {
                        strQuery += strParams;
                    }

                    String strTestResp = "";
                    for (int i = 0; i < (int)arSources.size(); i++)
                    {
                        SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                        if (pSrc != null)
                        {
                            strQuery += "&sources[][name]=" + pSrc.getName();

                            if (!pSrc.isTokenFromDB() && pSrc.getToken() > 1)
                            {
                                strQuery += "&sources[][token]=" + pSrc.getToken();
                            }

                            strTestResp = getSourceOptions().getProperty(pSrc.getID(), "rho_server_response");
                        }
                    }

                    LOG.INFO("Call search on server. Url: " + (strUrl + strQuery));
                    NetResponse resp = getNet().pullData(strUrl + strQuery, this);

                    if (!resp.isOK())
                    {
                        stopSync();
                        m_nErrCode = RhoAppAdapter.getErrorFromResponse(resp);
                        m_strError = resp.getCharData();
                        continue;
                    }

                    String szData = null;
                    if (strTestResp != null && strTestResp.length() > 0)
                    {
                        szData = strTestResp;
                    }
                    else
                    {
                        szData = resp.getCharData();
                    }

                    JSONArrayIterator oJsonArr = new JSONArrayIterator(szData);

                    for ( ; !oJsonArr.isEnd() && isContinueSync(); oJsonArr.next())
                    {
                        JSONArrayIterator oSrcArr = oJsonArr.getCurArrayIter();        //new JSONArrayIterator(oJsonArr.getCurItem());
                        if (oSrcArr.isEnd())
                        {
                            break;
                        }

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

                        if (nVersion != getProtocol().getVersion())
                        {
                            LOG.ERROR("Sync server send search data with incompatible version. Client version: " + getProtocol().getVersion() +
                                      "; Server response version: " + nVersion);
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_SYNCVERSION;
                            continue;
                        }

                        if (!oSrcArr.isEnd() && oSrcArr.getCurItem().hasName("token"))
                        {
                            oSrcArr.next();
                        }

                        if (!oSrcArr.getCurItem().hasName("source"))
                        {
                            LOG.ERROR("Sync server send search data without source name.");
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                            m_strError = szData;
                            continue;
                        }

                        String     strSrcName = oSrcArr.getCurItem().getString("source");
                        SyncSource pSrc       = findSourceByName(strSrcName);
                        if (pSrc == null)
                        {
                            LOG.ERROR("Sync server send search data for unknown source name:" + strSrcName);
                            stopSync();
                            m_nErrCode = RhoAppAdapter.ERR_UNEXPECTEDSERVERRESPONSE;
                            m_strError = szData;
                            continue;
                        }

                        oSrcArr.reset(0);
                        pSrc.setProgressStep(nProgressStep);
                        pSrc.processServerResponse_ver3(oSrcArr);

                        nSearchCount += pSrc.getCurPageCount();

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

                            m_strServerError += pSrc.getServerError();
                            m_nErrCode        = pSrc.getErrorCode();
                        }
                    }

                    if (nSearchCount == 0)
                    {
                        for (int i = 0; i < (int)arSources.size(); i++)
                        {
                            SyncSource pSrc = findSourceByName((String)arSources.elementAt(i));
                            if (pSrc != null)
                            {
                                pSrc.processToken(0);
                            }
                        }
                        break;
                    }
                }

                getNotify().fireAllSyncNotifications(true, m_nErrCode, m_strError, m_strServerError);

                //update db info
                TimeInterval endTime = TimeInterval.getCurrentTime();
                //unsigned long timeUpdated = CLocalTime().toULong();
                for (int i = 0; i < (int)arSources.size(); i++)
                {
                    SyncSource oSrc = findSourceByName((String)arSources.elementAt(i));
                    if (oSrc == null)
                    {
                        continue;
                    }
                    oSrc.getDB().executeSQL("UPDATE sources set last_updated=?,last_inserted_size=?,last_deleted_size=?, " +
                                            "last_sync_duration=?,last_sync_success=?, backend_refresh_time=? WHERE source_id=?",
                                            endTime.toULong() / 1000, oSrc.getInsertedCount(), oSrc.getDeletedCount(),
                                            endTime.minus(startTime).toULong(), oSrc.getGetAtLeastOnePage()?1:0,
                                            oSrc.getRefreshTime(), oSrc.getID());
                }
                //

                getNotify().cleanCreateObjectErrors();
                if (getState() != esExit)
                {
                    setState(esNone);
                }
            } catch (Exception exc) {
                LOG.ERROR("Search failed.", exc);

                getNotify().fireAllSyncNotifications(true, RhoAppAdapter.ERR_RUNTIME, "", "");
            }
        }