Search in sources :

Example 6 with Query

use of io.divide.shared.transitory.query.Query in project divide by HiddenStage.

the class MyActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DivideDrawer.attach(this, R.layout.main);
    //        setContentView(R.layout.main);
    ButterKnife.inject(this);
    getActionBar().setHomeButtonEnabled(true);
    //        au = AuthUtils.get(this, AuthManager.ACCOUNT_TYPE); TODO replace this
    app = (MyApplication) this.getApplication();
    BackendServices.addLoginListener(new LoginListener() {

        @Override
        public void onNext(BackendUser user) {
            System.out.println("loginListener: setUser: " + user);
            if (user != null) {
                setUser(user);
            }
        }
    });
    adapter = new BackendObjectAdaper(this, objectList);
    usersLV.setAdapter(adapter);
    usersLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            BackendObject o = adapter.getItem(position);
            Query q = new QueryBuilder().delete().from(BackendObject.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, o.getObjectKey()).build();
            BackendServices.remote().query(BackendObject.class, q).subscribe(new Action1<Collection<BackendObject>>() {

                @Override
                public void call(Collection<BackendObject> backendObjects) {
                    getObjects();
                }
            });
            BackendServices.local().delete(o);
        }
    });
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            if (BackendUser.isLoggedIn())
                getObjects();
            else
                logger.debug("Not signed in, not querying data.");
        }
    }, 1000);
    Storage s = new XmlStorage(new File(this.getFilesDir() + File.separator + "something.xml"), Storage.MODE_WORLD_WRITEABLE);
    String id = "something";
    System.out.println("Stored: " + s.getString(id, ""));
    System.out.println("Stored: " + s.contains(id));
    s.edit().putString(id, "something2").commit();
    s.edit().putInt("int", 55).commit();
}
Also used : BackendObject(io.divide.client.BackendObject) Action1(rx.functions.Action1) Query(io.divide.shared.transitory.query.Query) Handler(android.os.Handler) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) BackendUser(io.divide.client.BackendUser) InjectView(butterknife.InjectView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) XmlStorage(io.divide.shared.file.XmlStorage) LoginListener(io.divide.client.auth.LoginListener) Storage(io.divide.shared.file.Storage) XmlStorage(io.divide.shared.file.XmlStorage) Collection(java.util.Collection) AdapterView(android.widget.AdapterView) File(java.io.File)

Example 7 with Query

use of io.divide.shared.transitory.query.Query in project divide by HiddenStage.

the class DAOTest method testQueryEmbededCollections.

//    [TransientObject{
//        user_data={players_key=[2]}
//        , meta_data={object_type=com.jug6ernaut.tactics.client.web.WGame, getKey()=049d74c7-511c-49ef-a312-3a782f2f41e3, create_date_key=1384732193542, modified_date_key=1384732193543}
//    }]
//    [TestObject1{
//        user_data={players_key=[2]}
//        , meta_data={object_type=io.divide.authenticator.se, create_date_key=1384733366530, getKey()=e842d295-cee8-45da-83df-d235f5b1801f, modified_date_key=1384733366530}
//    }]
@Test
public void testQueryEmbededCollections() throws Exception {
    TestObject1 t1 = new TestObject1();
    List<String> l1 = Arrays.asList("2");
    t1.put("players_key", l1);
    System.out.println("embeded: " + Arrays.asList(t1));
    TestObject1 t2 = new TestObject1();
    List<String> l2 = Arrays.asList("3", "4", "5");
    t2.put("players_key", l2);
    dao.save(t1, t2);
    Query q = new QueryBuilder().select().from(TestObject1.class).where("players_key", OPERAND.CONTAINS, "2").build();
    List<TransientObject> results = dao.query(q);
    assertNotEquals(null, results);
    assertEquals(1, results.size());
    assertEquals(t1.getObjectKey(), ObjectUtils.get1stOrNull(results).getObjectKey());
    assertEquals(l1, ObjectUtils.get1stOrNull(results).get(l1.getClass(), "players_key"));
    q = new QueryBuilder().select().from(TestObject1.class).where("players_key", OPERAND.CONTAINS, "4").build();
    results = dao.query(q);
    assertNotEquals(null, results);
    assertEquals(1, results.size());
    assertEquals(t2.getObjectKey(), ObjectUtils.get1stOrNull(results).getObjectKey());
    assertEquals(l2, ObjectUtils.get1stOrNull(results).get(l2.getClass(), "players_key"));
    Gson g = new GsonBuilder().create();
    g.fromJson(g.toJson(t1), TransientObject.class);
}
Also used : Query(io.divide.shared.transitory.query.Query) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject) Test(org.junit.Test)

Example 8 with Query

use of io.divide.shared.transitory.query.Query in project divide by HiddenStage.

the class DAOTest method testQuery.

@Test
public void testQuery() throws Exception {
    rawSave(object1, object2, object3, object4);
    Query q;
    List<TransientObject> results;
    String key = testObject1.getObjectKey();
    //where
    q = new QueryBuilder().select().from(TestObject1.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, key).build();
    results = dao.query(q);
    assertNotNull("1.", results);
    assertEquals("2.", 1, results.size());
    TransientObject to = ObjectUtils.get1stOrNull(results);
    assertEquals(results + "", to.getObjectKey(), testObject1.getObjectKey());
    q = new QueryBuilder().select().from(TestObject1.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 3, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.EQ, "1").build();
    results = dao.query(q);
    assertNotEquals("key1=", null, results);
    assertEquals("key1=", 2, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.GREATER_THAN, "1").build();
    results = dao.query(q);
    assertNotEquals("key1>", null, results);
    assertEquals("key1>", 1, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.GREATER_THAN_EQ, "1").build();
    results = dao.query(q);
    assertNotEquals("key1>=", null, results);
    assertEquals("key1>=", 3, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN, "3").build();
    results = dao.query(q);
    assertNotEquals("key1<", null, results);
    assertEquals("key1<", 2, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN_EQ, "3").build();
    results = dao.query(q);
    assertNotEquals("key1<=", null, results);
    assertEquals("key1<=", 3, results.size());
    q = new QueryBuilder().select().from(TestObject2.class).where("key1", OPERAND.EQ, "1").or("key2", OPERAND.EQ, "2").build();
    results = dao.query(q);
    assertNotEquals("2where", null, results);
    assertEquals("2where", 1, results.size());
    assertEquals("2where", testObject4.getObjectKey(), ObjectUtils.get1stOrNull(results).getObjectKey());
    //limit
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN_EQ, "3").limit(2).build();
    results = dao.query(q);
    assertNotEquals("limit test failed", null, results);
    assertEquals("limit test failed", 2, results.size());
//        q = new QueryBuilder().delete().from(TestObject1.class).where("key1",OPERAND.LESS_THAN_EQ,"3").build();
//        results = dao.query(q);
//        assertNotEquals("delete", null, results);
//        assertEquals("delete test failed", 1, results.size());
//        assertEquals("delete test failed", 3, (int)results.get(0).get(Integer.class,"count"));
//
//        q = new QueryBuilder().delete().from(TestObject1.class).build();
//        results = dao.query(q);
//        assertNotEquals("delete test failed", null, results);
//        assertEquals("delete test failed", 1, results.size());
//        assertEquals("delete test failed", 0, (int)results.get(0).get(Integer.class,"count"));
//offset
}
Also used : Query(io.divide.shared.transitory.query.Query) TransientObject(io.divide.shared.transitory.TransientObject) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) Test(org.junit.Test)

Example 9 with Query

use of io.divide.shared.transitory.query.Query in project divide by HiddenStage.

the class OrientDBDao method delete.

@Override
public void delete(TransientObject... objects) throws DAOException {
    checkDb();
    if (objects.length == 0)
        return;
    QueryBuilder.WhereMoreBuilder builder = new QueryBuilder().select().from(objects[0].getClass()).where(TransientObject.OBJECT_KEY, OPERAND.EQ, objects[0].getObjectKey());
    for (int x = 1; x < objects.length; x++) {
        builder.or(TransientObject.OBJECT_KEY, OPERAND.EQ, objects[x].getObjectKey());
    }
    Query q = builder.build();
    //        StringBuilder sb = new StringBuilder();
    //        sb.append("SELECT * FROM " + objects[0].getClass().getName() + " WHERE ");
    //        for(int x=0;x<objects.length;x++){
    //            sb.append("meta_data.object_key = '" + objects[x].getObjectKey() +"'");
    //            if(x+1<objects.length)
    //                sb.append( " OR ");
    //        }
    System.out.println("Delete: " + q.getSQL());
    List<ODocument> list = db.query(new OSQLSynchQuery<ODocument>(q.getSQL()));
    for (ODocument w : list) {
        System.out.println("Deleting: " + w);
        w.delete();
    }
}
Also used : Query(io.divide.shared.transitory.query.Query) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 10 with Query

use of io.divide.shared.transitory.query.Query in project divide by HiddenStage.

the class AuthServerLogic method getUserFromRecoveryToken.

public Credentials getUserFromRecoveryToken(String token) throws DAOException {
    Query q = new QueryBuilder().select().from(Credentials.class).where(Credentials.RECOVERY_TOKEN_KEY, OPERAND.EQ, token).build();
    TransientObject to = ObjectUtils.get1stOrNull(dao.query(q));
    if (to != null) {
        ServerCredentials sc = new ServerCredentials(to);
        sc.setAuthToken(AuthTokenUtils.getNewToken(keyManager.getSymmetricKey(), sc));
        sc.setRecoveryToken(AuthTokenUtils.getNewToken(keyManager.getSymmetricKey(), sc));
        dao.save(sc);
        return sc;
    } else {
        throw new DAOException(HttpStatus.SC_BAD_REQUEST, "invalid recovery token");
    }
}
Also used : DAOException(io.divide.shared.server.DAO.DAOException) Query(io.divide.shared.transitory.query.Query) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject)

Aggregations

Query (io.divide.shared.transitory.query.Query)12 QueryBuilder (io.divide.shared.transitory.query.QueryBuilder)12 TransientObject (io.divide.shared.transitory.TransientObject)9 Test (org.junit.Test)3 BackendObject (io.divide.client.BackendObject)2 DAOException (io.divide.shared.server.DAO.DAOException)2 AuthTokenUtils (io.divide.shared.util.AuthTokenUtils)2 Collection (java.util.Collection)2 Handler (android.os.Handler)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 InjectView (butterknife.InjectView)1 OnClick (butterknife.OnClick)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 BackendUser (io.divide.client.BackendUser)1