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));
}
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);
}
}
}
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);
}
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);
}
}
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);
}
}
Aggregations