Search in sources :

Example 1 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class DAOTest method testDelete.

@Test
public void testDelete() throws Exception {
    rawSave(object1, object2, object3, object4);
    dao.delete(testObject1);
    dao.delete(testObject4);
    Collection<TransientObject> results = dao.get(testObject1Table, testObject1.getObjectKey(), testObject2.getObjectKey());
    assertNotEquals(null, results);
    assertEquals(1, results.size());
    assertEquals(ObjectUtils.get1stOrNull(results).getObjectKey(), testObject2.getObjectKey());
    Query q = new QueryBuilder().select().from(TestObject1.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 2, results.size());
    q = new QueryBuilder().select().from(TestObject2.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 0, results.size());
}
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 2 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class ObjectifyDAO method query.

@Override
public List<TransientObject> query(Query query) throws DAOException {
    logger.info("query: " + query);
    LoadType<?> filter = ofy().load().type(OfyObject.class);
    com.googlecode.objectify.cmd.Query<?> oFilter = filter.filter(TransientObject.META_DATA + "." + TransientObject.OBJECT_TYPE_KEY.KEY + " =", query.getFrom());
    for (Clause c : query.getWhere().values()) {
        oFilter = oFilter.filter(c.getBefore() + " " + // replace CONTAINS with ==
        (c.getOperand().equals(OPERAND.CONTAINS.toString()) ? OPERAND.EQ.toString() : c.getOperand()), c.getAfter());
    }
    if (query.getOffset() != null) {
        oFilter = oFilter.offset(query.getOffset());
    }
    if (query.getLimit() != null) {
        oFilter = oFilter.limit(query.getLimit());
    }
    if (query.getRandom() != null) {
        int count = count(query.getFrom());
        if (count < 1)
            return new ArrayList<TransientObject>();
        int random = RANDOM.nextInt(count);
        oFilter = oFilter.offset(random);
        oFilter = oFilter.limit(query.getLimit());
    }
    List list = null;
    switch(query.getAction()) {
        case SELECT:
            {
                if (query.getSelect() == null) {
                    list = oFilter.list();
                    List<TransientObject> toReturn = new ArrayList<TransientObject>(list.size());
                    try {
                        for (OfyObject oo : (List<OfyObject>) list) {
                            logger.info("Got: " + oo);
                            toReturn.add(BackendToOfy.getBack(oo));
                        }
                    } catch (Exception e) {
                        throw new DAOException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
                    }
                    list.clear();
                    list = toReturn;
                } else if (query.getSelect().equals(SelectOperation.COUNT)) {
                    int count = count(query.getFrom());
                    return Arrays.asList((TransientObject) new Count(count, query.getFrom()));
                }
            }
            break;
        case DELETE:
            {
                list = oFilter.keys().list();
                ofy().delete().keys(list);
                int count = list.size();
                list.clear();
                EmptyTO o = new EmptyTO();
                o.put("count", count);
                list.add(o);
            }
            break;
        case UPDATE:
            {
                throw new NotImplementedException();
            }
    }
    logger.info("Query Complete: " + list);
    return (List<TransientObject>) list;
}
Also used : NotImplementedException(sun.reflect.generics.reflectiveObjects.NotImplementedException) NotImplementedException(sun.reflect.generics.reflectiveObjects.NotImplementedException) TransientObject(io.divide.shared.transitory.TransientObject)

Example 3 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class ObjectifyDAO method delete.

@Override
public void delete(TransientObject... objects) throws DAOException {
    logger.info("delete: " + ObjectUtils.v2c(objects));
    try {
        for (TransientObject bo : objects) {
            ofy().delete().key(Key.create(OfyObject.class, bo.getObjectKey())).now();
        }
    } catch (Exception e) {
        throw new DAOException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
    logger.info("Delete Complete.");
}
Also used : TransientObject(io.divide.shared.transitory.TransientObject) NotImplementedException(sun.reflect.generics.reflectiveObjects.NotImplementedException)

Example 4 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class OrientDBDao method save.

@Override
public void save(TransientObject... objects) throws DAOException {
    System.out.println("save: " + Arrays.asList(objects));
    checkDb();
    OTransaction transaction = db.getTransaction();
    transaction.begin();
    for (TransientObject t : objects) {
        db.save(new ODocumentWrapper(t));
    }
    transaction.commit();
    transaction.close();
}
Also used : OTransaction(com.orientechnologies.orient.core.tx.OTransaction) TransientObject(io.divide.shared.transitory.TransientObject)

Example 5 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class OrientDBDao method get.

@Override
public Collection<TransientObject> get(String objectType, String... keys) throws DAOException {
    if (keys.length == 0)
        return Arrays.asList();
    //        String objectType = Query.safeTable(type);
    checkDb();
    OTransaction transaction = db.getTransaction();
    transaction.begin();
    //        System.err.println(db.browseCluster(objectType).next());
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT * FROM " + objectType + " WHERE ");
    for (int x = 0; x < keys.length; x++) {
        sb.append("meta_data.object_key = '" + keys[x] + "'");
        if (x + 1 < keys.length)
            sb.append(" OR ");
    }
    List<TransientObject> objects = new ArrayList<TransientObject>();
    System.err.println("Query: " + sb.toString());
    List<ODocument> list = db.query(new OSQLSynchQuery<ODocument>(sb.toString()));
    System.err.println("Found: " + list.size());
    for (ODocument w : list) {
        System.err.println("Get: " + w);
        TransientObject to = ODocumentWrapper.toObject(w, TransientObject.class);
        objects.add(to);
    }
    transaction.commit();
    transaction.close();
    return objects;
}
Also used : OTransaction(com.orientechnologies.orient.core.tx.OTransaction) ArrayList(java.util.ArrayList) TransientObject(io.divide.shared.transitory.TransientObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

TransientObject (io.divide.shared.transitory.TransientObject)21 QueryBuilder (io.divide.shared.transitory.query.QueryBuilder)10 Query (io.divide.shared.transitory.query.Query)9 Test (org.junit.Test)6 OTransaction (com.orientechnologies.orient.core.tx.OTransaction)3 NotImplementedException (sun.reflect.generics.reflectiveObjects.NotImplementedException)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ServerTest (io.divide.server.ServerTest)2 DAOException (io.divide.shared.server.DAO.DAOException)2 Credentials (io.divide.shared.transitory.Credentials)2 AuthTokenUtils (io.divide.shared.util.AuthTokenUtils)2 PublicKey (java.security.PublicKey)2 ArrayList (java.util.ArrayList)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeLiteral (com.google.inject.TypeLiteral)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 CommitExpection (iBoxDB.LocalServer.E.CommitExpection)1 MockKeyManager (io.divide.client.auth.MockKeyManager)1