Ejemplo n.º 1
0
jobject rho_cast_helper<jobject, CJSONEntry>::operator ()(JNIEnv *env, const CJSONEntry& jsonEntry)
{
    if(jsonEntry.isEmpty() || jsonEntry.isNull()) {
        return 0;
    }
    if (!initConvertor(env))
    {
        return 0;
    }
    if(jsonEntry.isString())
    {
        RAWTRACE("Convert to String object");
        return rho_cast<jstring>(env, jsonEntry.getString());
    }
    if(jsonEntry.isArray())
    {
        RAWTRACE("Convert to Collection object");
        return convertJsonEntryToJavaCollection(jsonEntry);
    }
    if(jsonEntry.isObject())
    {
        RAWTRACE("Convert to Map object");
        return convertJsonEntryToJavaMap(jsonEntry);
    }
    if(jsonEntry.isBoolean())
    {
        RAWTRACE("Convert to Boolean object");
        return RhoJniConvertor::getBooleanObject(jsonEntry.getBoolean());
    }
    if(jsonEntry.isInteger())
    {
        RAWTRACE("Convert to Integer object");
        return RhoJniConvertor::getIntegerObject(jsonEntry.getInt());
    }
    if(jsonEntry.isFloat())
    {
        RAWTRACE("Convert to Double object");
        return RhoJniConvertor::getDoubleObject(jsonEntry.getDouble());
    }

    RAWTRACE("Convert to null");
    return 0;
}
Ejemplo n.º 2
0
String CSyncEngine::requestClientIDByNet()
{
    String serverUrl = RHOCONF().getString("syncserver");
    String strUrl = serverUrl + "clientcreate";
    String strQuery = SYNC_SOURCE_FORMAT();

    NetResponse(resp,getNet().pullData(strUrl+strQuery));
    if ( resp.isOK() && resp.getCharData() != null )
    {
        const char* szData = resp.getCharData();
        CJSONEntry oJsonEntry(szData);

        CJSONEntry oJsonObject = oJsonEntry.getEntry("client");
        if ( !oJsonObject.isEmpty() )
            return oJsonObject.getString("client_id");
    }

    return "";
}
Ejemplo n.º 3
0
boolean CSyncSource::processSyncObject_ver1(CJSONEntry oJsonObject, int nSrcID)//throws Exception
{
    const char* strOldObject = oJsonObject.getString("oo");
    if ( isDeleteObjectsPass() != (nSrcID < 0) )
        return true;

    if ( oJsonObject.hasName("e") )
    {
        const char* strError = oJsonObject.getString("e");
        getNotify().addCreateObjectError(nSrcID,strOldObject,strError);
        return true;
    }

	const char* strObject = oJsonObject.getString("o");
	CJSONArrayIterator oJsonArr(oJsonObject, "av");
    //oo conflicts
    boolean bUpdatedOO = false;
    //
    for( ; !oJsonArr.isEnd() && getSync().isContinueSync(); oJsonArr.next() )
	{
		CJSONEntry oJsonEntry = oJsonArr.getCurItem();
        if ( oJsonEntry.isEmpty() )
        	continue;

        if ( nSrcID >= 0 ) //insert
        {
    	    CValue value(oJsonEntry,1);
    	    if ( !downloadBlob(value) )
    		    return false;

            String strAttrib = oJsonEntry.getString("a");
            //oo conflicts
            if ( strOldObject != null && !bUpdatedOO )
            {
                getDB().executeSQL("UPDATE object_values SET object=? where object=? and source_id=?", strObject, strOldObject, nSrcID );
                getDB().executeSQL("UPDATE changed_values SET object=? where object=? and source_id=?", strObject, strOldObject, nSrcID );

                getNotify().onObjectChanged(nSrcID,strOldObject, CSyncNotify::enCreate);

                bUpdatedOO = true;
            }

            DBResult(resInsert, getDB().executeSQLReportNonUnique("INSERT INTO object_values \
                (id, attrib, source_id, object, value, attrib_type) VALUES(?,?,?,?,?,?)", 
                value.m_nID, strAttrib, nSrcID, strObject,
                value.m_strValue, value.m_strAttrType ) );
            if ( resInsert.isNonUnique() )
            {
                getDB().executeSQL("UPDATE object_values \
                    SET id=?, value=?, attrib_type=? WHERE object=? and attrib=? and source_id=?", 
                    value.m_nID, value.m_strValue, value.m_strAttrType,
                    strObject, strAttrib, nSrcID );

                // oo conflicts
                getDB().executeSQL("UPDATE changed_values SET main_id=? where object=? and attrib=? and source_id=? and sent<=1", value.m_nID, strObject, strAttrib, nSrcID );
                getDB().executeSQL("UPDATE changed_values SET sent=4 where object=? and attrib=? and source_id=? and sent>1", strObject, strAttrib, nSrcID );
                //
            }

            getNotify().onObjectChanged(nSrcID,strObject, CSyncNotify::enUpdate);

            m_nInserted++;
        }else
Ejemplo n.º 4
0
rho::String js_SQLite3_execute(const rho::String& strObjID, CJSONArray& argv, const rho::String&, const rho::String&, const rho::String& )
{
    rho::database::ISQLite3* pObj = rho::database::CSQLite3FactoryBase::getInstance()->getModuleByID(strObjID);
    void* pDB =  static_cast<rho::database::CSQLite3Impl*>(pObj)->getDb();

    sqlite3 * db = NULL;
    void **ppDB = &pDB;
    sqlite3_stmt *statement = NULL;
    const char* sql = NULL;
    Vector< rho::Hashtable<rho::String, rho::String> > arRes;
    rho::apiGenerator::CMethodResult oResult;

    int nRes = 0;
    char * szErrMsg = 0;
    int is_batch = 0;

    db = (sqlite3 *)rho_db_get_handle(*ppDB);
    sql = argv[0].getString();
    is_batch = argv[1].getBoolean();

    RAWTRACE1("db_execute: %s", sql);

    PROF_START_CREATED("SQLITE");
    if ( is_batch )
    {
        PROF_START_CREATED("SQLITE_EXEC");

        rho_db_lock(*ppDB);
        nRes = sqlite3_exec(db, sql,  NULL, NULL, &szErrMsg);
        rho_db_unlock(*ppDB);

        PROF_STOP("SQLITE_EXEC");
    }
    else
    {
        rho_db_lock(*ppDB);
        PROF_START_CREATED("SQLITE_PREPARE");
        nRes = rho_db_prepare_statement(*ppDB, sql, -1, &statement);
        PROF_STOP("SQLITE_PREPARE");
        if ( nRes != SQLITE_OK)
        {
            szErrMsg = (char *)sqlite3_errmsg(db);
            rho_db_unlock(*ppDB);

            oResult.setArgError( String("could not prepare statement: ") + convertToStringA(nRes) + "; Message: " + (szErrMsg?szErrMsg:""));
            return oResult.toJSON();
        }

        if ( argv.getSize() > 2 && argv[2].isArray() )
        {
            int i = 0;
            CJSONArray args( argv[2] );

            for( ; i < args.getSize(); i++ )
            {
                CJSONEntry arg = args[i];
                if ( arg.isNull() )
                {
                    sqlite3_bind_null(statement, i+1);
                    continue;
                }

                if ( arg.isString() )
                    sqlite3_bind_text(statement, i+1, arg.getString(), strlen(arg.getString()), SQLITE_TRANSIENT);
                else if (arg.isFloat())
                    sqlite3_bind_double(statement, i+1, arg.getDouble() );
                else if (arg.isInteger())
                    sqlite3_bind_int64(statement, i+1, arg.getUInt64() );
                else if (arg.isBoolean())
                    sqlite3_bind_int(statement, i+1, arg.getBoolean() ? 1 : 0 );
                else
                {
                    sqlite3_reset(statement);

                    oResult.setArgError( String("Unsupported argument type. Argument number: ") + convertToStringA(i));
                    return oResult.toJSON();
                }
            }
        }

        PROF_START_CREATED("SQLITE_EXEC");
        nRes = sqlite3_step(statement);
        PROF_STOP("SQLITE_EXEC");

        while( nRes== SQLITE_ROW )
        {
            int nCount = sqlite3_data_count(statement);
            int nCol = 0;
            rho::Hashtable<rho::String, rho::String> hashRec;

            for(; nCol<nCount; nCol++)
            {
                int nColType = sqlite3_column_type(statement,nCol);
                const char* szColName = sqlite3_column_name(statement,nCol);
                String colValue;

                switch(nColType)
                {
                case SQLITE_NULL:
                    break;
                case SQLITE_FLOAT:
                {
                    double dVal = sqlite3_column_double(statement, nCol);
                    colValue = convertToStringA(dVal);
                    break;
                }
                case SQLITE_INTEGER:
                {
                    sqlite_int64 nVal = sqlite3_column_int64(statement, nCol);
                    colValue = convertToStringA(nVal);
                    break;
                }
                default: {
                    sqlite3_value * sqlValue = sqlite3_column_value(statement, nCol);
                    int nLen = sqlite3_value_bytes(sqlValue);
                    const char*  szValue = (const char *)sqlite3_value_text(sqlValue);
                    colValue = String(szValue, nLen);
                    break;
                }
                }

                hashRec[szColName] = colValue;
            }

            arRes.addElement( hashRec );

            PROF_START_CREATED("SQLITE_EXEC");
            nRes = sqlite3_step(statement);
            PROF_STOP("SQLITE_EXEC");

        }

        rho_db_unlock(*ppDB);

    }

    if ( statement )
        sqlite3_reset(statement);

    PROF_STOP("SQLITE");

    if ( nRes != SQLITE_OK && nRes != SQLITE_ROW && nRes != SQLITE_DONE )
    {
        if ( !szErrMsg )
            szErrMsg = (char*)sqlite3_errmsg(db);

        oResult.setError( String("could not execute statement: ") + convertToStringA(nRes) + "; Message: " + (szErrMsg?szErrMsg:""));
        return oResult.toJSON();
    }

    oResult.set(arRes);

    return oResult.toJSON();
}
Ejemplo n.º 5
0
void CSyncSource::processServerResponse_ver3(CJSONArrayIterator& 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: " + convertToStringA(getProtocol().getVersion()) +
            "; Server response version: " + convertToStringA(nVersion) + ". Source name: " + getName();
        getSync().stopSync();
        m_nErrCode = RhoAppAdapter.ERR_SYNCVERSION;
        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() )
    {
        CJSONEntry oCmds = oJsonArr.getCurItem();
        PROF_START("Data");
        //TODO: use isUIWaitDB inside processSyncCommand
        //    if ( getDB().isUIWaitDB() )
        //    {
		//        LOG(INFO) + "Commit transaction because of UI request.";
        //        getDB().endTransaction();
        //        getDB().startTransaction();
        //    }

        if ( oCmds.hasName("schema-changed") )
        {
            getSync().setSchemaChanged(true);
        }else if ( oCmds.hasName("source-error") )
        {
            CJSONEntry errSrc = oCmds.getEntry("source-error");
            CJSONStructIterator errIter(errSrc);
            for( ; !errIter.isEnd(); errIter.next() )
            {
                m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;
                m_strError = errIter.getCurValue().getString("message");
                m_strErrorType = errIter.getCurKey();
            }
        }else if ( oCmds.hasName("search-error") )
        {
            CJSONEntry errSrc = oCmds.getEntry("search-error");
            CJSONStructIterator errIter(errSrc);
            for( ; !errIter.isEnd(); errIter.next() )
            {
                m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;
                m_strError = errIter.getCurValue().getString("message");
                m_strErrorType = errIter.getCurKey();
            }
        }else if ( oCmds.hasName("create-error") )
        {
            m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;
            m_strErrorType = "create-error";
        }else if ( oCmds.hasName("update-error") )
        {
            m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;
            m_strErrorType = "update-error";
        }else if ( oCmds.hasName("delete-error") )
        {
            m_nErrCode = RhoAppAdapter.ERR_CUSTOMSYNCSERVER;
            m_strErrorType = "delete-error";
        }else
        {
            getDB().startTransaction();

            if (getSync().getSourceOptions().getBoolProperty(getID(), "pass_through"))
            {
                if ( m_bSchemaSource )
                    getDB().executeSQL( (String("DELETE FROM ") + getName()).c_str() );
                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") );
            if ( oCmds.hasName("delete") && getSync().isContinueSync() )
                processSyncCommand("delete", oCmds.getEntry("delete") );
            if ( oCmds.hasName("insert") && getSync().isContinueSync() )
                processSyncCommand("insert", oCmds.getEntry("insert") );

            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");
}
Ejemplo n.º 6
0
jstring rho_cast_helper<jstring, CJSONEntry>::operator ()(JNIEnv *env, const CJSONEntry& jsonEntry)
{
    return rho_cast<jstring>(env, jsonEntry.getString());
}