Exemple #1
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;
	    }
Exemple #2
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);
	    }