Search in sources :

Example 26 with Entity

use of com.google.appengine.api.datastore.Entity in project appengine-guestbook-java by googlearchive.

the class SignGuestbookServletTest method testDoPost.

@Test
public void testDoPost() throws IOException, EntityNotFoundException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    String guestbookName = "TestGuestbook";
    String testContent = "Test Content";
    when(request.getParameter("guestbookName")).thenReturn(guestbookName);
    when(request.getParameter("content")).thenReturn(testContent);
    Date priorToRequest = new Date();
    signGuestbookServlet.doPost(request, response);
    Date afterRequest = new Date();
    verify(response).sendRedirect("/guestbook.jsp?guestbookName=TestGuestbook");
    User currentUser = UserServiceFactory.getUserService().getCurrentUser();
    Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity();
    assertEquals(guestbookName, greeting.getKey().getParent().getName());
    assertEquals(testContent, greeting.getProperty("content"));
    assertEquals(currentUser, greeting.getProperty("user"));
    Date date = (Date) greeting.getProperty("date");
    assertTrue("The date in the entity [" + date + "] is prior to the request being performed", priorToRequest.before(date) || priorToRequest.equals(date));
    assertTrue("The date in the entity [" + date + "] is after to the request completed", afterRequest.after(date) || afterRequest.equals(date));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Entity(com.google.appengine.api.datastore.Entity) User(com.google.appengine.api.users.User) Query(com.google.appengine.api.datastore.Query) HttpServletResponse(javax.servlet.http.HttpServletResponse) Date(java.util.Date) Test(org.junit.Test)

Example 27 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaePersistenceManager method fillAggregated.

protected <T> void fillAggregated(ClassInfo info, T ancestor, Key ancestorKey) {
    // now gets aggregated one2one (one2many are retrieved by ListQuery except with @Join)
    for (Field f : info.aggregatedFields) {
        Class<?> cClazz = f.getType();
        ClassInfo cInfo = ClassInfo.getClassInfo(cClazz);
        if (ClassInfo.isModel(cClazz)) {
            // creates a query for fieldname:child_tablename
            com.google.appengine.api.datastore.Query q = new com.google.appengine.api.datastore.Query(GaeMappingUtils.getKindWithAncestorField(cInfo, info, f));
            PreparedQuery pq = ds.prepare(q.setAncestor(ancestorKey));
            Entity cEntity = pq.asSingleEntity();
            Object cObj = Util.createObjectInstance(cClazz);
            GaeMappingUtils.fillModelAndKey(cObj, cEntity);
            Util.setField(ancestor, f, cObj);
        } else // todo manage joined one2many listquery
        if (ClassInfo.isMany(f)) {
            Many4PM<?> lq = (Many4PM<?>) Util.readField(ancestor, f);
            // sets the sync flag to false to tell that it should be fetched when the listquery is accessed!
            lq.setSync(false);
        }
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(siena.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Field(java.lang.reflect.Field) Many4PM(siena.core.Many4PM) ClassInfo(siena.ClassInfo)

Example 28 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaePersistenceManager method _insertMultiple.

private <T> int _insertMultiple(Iterable<T> objects) {
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        if (info.hasAggregator) {
            Relation rel = (Relation) Util.readField(obj, info.aggregator);
            if (rel != null && rel.mode == RelationMode.AGGREGATION) {
                ClassInfo parentInfo = ClassInfo.getClassInfo(rel.target.getClass());
                Key parentKey = GaeMappingUtils.makeKey(parentInfo, rel.target);
                _insertAddEntity(entities, obj, info, parentKey, parentInfo, (Field) rel.discriminator);
            } else {
                _insertAddEntity(entities, obj, info, null, null, null);
            }
        } else {
            _insertAddEntity(entities, obj, info, null, null, null);
        }
    }
    return _insertPutEntities(entities, objects);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Relation(siena.core.Relation) ArrayList(java.util.ArrayList) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Example 29 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaePersistenceManager method get.

public void get(Object obj) {
    Key key = GaeMappingUtils.getKey(obj);
    ClassInfo info = ClassInfo.getClassInfo(obj.getClass());
    try {
        Entity entity = ds.get(key);
        if (entity != null) {
            GaeMappingUtils.fillModel(obj, entity);
            // related fields (Many<T> management mainly)
            if (!info.ownedFields.isEmpty()) {
                mapOwned(obj);
            }
            // aggregated management
            if (!info.aggregatedFields.isEmpty()) {
                mapAggregated(obj);
            }
            // join management
            if (!info.joinFields.isEmpty()) {
                mapJoins(obj);
            }
        }
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) ClassInfo(siena.ClassInfo)

Example 30 with Entity

use of com.google.appengine.api.datastore.Entity in project siena by mandubian.

the class GaePersistenceManager method getByKey.

public <T> T getByKey(Class<T> clazz, Object key) {
    Key gKey = GaeMappingUtils.makeKeyFromId(clazz, key);
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    try {
        Entity entity = ds.get(gKey);
        T obj = null;
        if (entity != null) {
            obj = Util.createObjectInstance(clazz);
            GaeMappingUtils.fillModelAndKey(obj, entity);
            // related fields (Many<T> management mainly)
            if (!info.ownedFields.isEmpty()) {
                mapOwned(obj);
            }
            // aggregated management
            if (!info.aggregatedFields.isEmpty()) {
                mapAggregated(obj);
            }
            // join management
            if (!info.joinFields.isEmpty()) {
                mapJoins(obj);
            }
        }
        return obj;
    } catch (EntityNotFoundException e) {
        return null;
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) SienaException(siena.SienaException) Key(com.google.appengine.api.datastore.Key) SienaException(siena.SienaException) EntityNotFoundException(com.google.appengine.api.datastore.EntityNotFoundException) ClassInfo(siena.ClassInfo)

Aggregations

Entity (com.google.appengine.api.datastore.Entity)50 Key (com.google.appengine.api.datastore.Key)34 ArrayList (java.util.ArrayList)25 Field (java.lang.reflect.Field)22 ClassInfo (siena.ClassInfo)17 SienaException (siena.SienaException)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)9 QueryResultList (com.google.appengine.api.datastore.QueryResultList)9 HashMap (java.util.HashMap)9 List (java.util.List)9 SienaFutureContainer (siena.core.async.SienaFutureContainer)9 SienaFutureWrapper (siena.core.async.SienaFutureWrapper)9 Map (java.util.Map)8 SienaRestrictedApiException (siena.SienaRestrictedApiException)6 DatastoreService (com.google.appengine.api.datastore.DatastoreService)5 IOException (java.io.IOException)5 Date (java.util.Date)4 Query (com.google.appengine.api.datastore.Query)3 ShortBlob (com.google.appengine.api.datastore.ShortBlob)3 NotImplementedException (org.apache.commons.lang.NotImplementedException)3