@Test public void testMarshall_Embedded() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); ChildEntity embeddedObject = new ChildEntity("Old Test City"); embeddedObject.setId(1L); embeddedObject.setType("EmbeddedType"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity rootObject.setEmbeddedEntity(embeddedObject); // not included, @Embedded IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); EmbeddedEntity ee = (EmbeddedEntity) rootObjectEntity.getProperty("embeddedEntity"); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertEquals(EmbeddedEntity.class, ee.getClass()); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); assertEquals(1L, ee.getProperties().get("id")); assertEquals("EmbeddedType", ee.getProperties().get("type")); }
public Serializable getCacheableObject(String feedUri) throws Exception { JSONObject json = new JSONObject(); PreparedQuery pq = datastoreService.prepare(new Query("feeds")); for (Entity entity : pq.asIterable()) { JSONObject item = new JSONObject(); item.put("uri", "/addama/feeds/" + entity.getKey().getName()); item.put("creator", entity.getProperty("creator").toString()); json.append("items", item); } return json.toString(); }
@Test public void test() { Entity parent = new Entity("Parent"); // set parent properties... List<EmbeddedEntity> children = new ArrayList<EmbeddedEntity>(); for (int i = 0; i < 5; i++) { EmbeddedEntity child = new EmbeddedEntity(); // set child properties... children.add(child); } parent.setUnindexedProperty("children", children); parent.setUnindexedProperty("children2", list("tag1", "tag2", "tag3")); DatastoreServiceFactory.getDatastoreService().put(parent); try { Entity e = DatastoreServiceFactory.getDatastoreService().get(parent.getKey()); e.getKey(); Object children2 = e.getProperty("children2"); assertTrue(children2 instanceof List); } catch (EntityNotFoundException e1) { e1.printStackTrace(); } }
private String valueToHtml(final Entity entity, final Entity point, final Value value) { final StringBuilder sb = new StringBuilder(SIZE); if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) { sb.append("<img style=\"float:left\" src=\"") .append(ServerInfoImpl.getFullServerURL(this.getThreadLocalRequest())); switch (value.getAlertState()) { case LowAlert: sb.append("/resources/images/point_low.png\">"); break; case HighAlert: sb.append("/resources/images/point_high.png\">"); break; case IdleAlert: sb.append("/resources/images/point_idle.png\">"); break; case OK: sb.append("/resources/images/point_ok.png\">"); break; } } if (entity != null && point != null) { sb.append(" "); if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) { sb.append("Alert Status:").append(value.getAlertState().name()); sb.append("<br>Value:").append(value.getDoubleValue()); } if (!Utils.isEmptyString(value.getNote())) { sb.append("<br>Note:").append(value.getNote()); } sb.append("<a href=\"#\" onclick=\"window.open('report.html?uuid=") .append(point.getKey()) .append("', 'Report',") .append("'height=800,width=800,toolbar=0,status=0,location=0' );\" >") .append(" [more]</a>"); } return sb.toString(); }
@Test public void testMarshall_Child() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); }
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.getWriter().println("<html><body>"); String keyname = req.getParameter("keyname"); String value = req.getParameter("value"); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); // Using the synchronous cache. MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); // display every element of kind TaskData for /datastore if (req.getParameterMap().isEmpty()) { // querying from datastore resp.getWriter().println("<h3>Datastore results:</h3>"); List<String> listOfKeys = new ArrayList<String>(); Query q = new Query("TaskData"); PreparedQuery pq = datastore.prepare(q); for (Entity result : pq.asIterable()) { String datastore_key = result.getKey().getName(); String taskData_value = (String) result.getProperty("value"); Date taskData_date = (Date) result.getProperty("date"); resp.getWriter() .println( "<p>keyname = " + datastore_key + " value = " + taskData_value + " date = " + taskData_date.toString() + "</p>"); listOfKeys.add(datastore_key); } // check which of the keys exist in memcache String memcache_value; resp.getWriter().println("<h3>Memcache results:</h3>"); for (String datastore_key : listOfKeys) { memcache_value = (String) syncCache.get(datastore_key); if (memcache_value != null) { // String decoded = new String(memcache_value, "UTF-8"); resp.getWriter() .println("<p>keyname = " + datastore_key + " value = " + memcache_value + "</p>"); } } } // display element of kind TaskData with key=keyname else if (keyname != null && value == null) { // first check in the cache String memcache_value = (String) syncCache.get(keyname); // Read from cache. // Get value from datastore Key task_key = KeyFactory.createKey("TaskData", keyname); try { Entity tne = datastore.get(task_key); if (memcache_value == null) { resp.getWriter().println("<h2>Datastore</h2>"); } else { resp.getWriter().println("<h2>Both</h2>"); } } catch (EntityNotFoundException e) { resp.getWriter().println("<h2>Neither</h2>"); } } // store element of kind TaskData with key=keyname and value=value else if (keyname != null && value != null) { Entity tne = new Entity("TaskData", keyname); tne.setProperty("value", value); tne.setProperty("date", new Date()); datastore.put(tne); syncCache.put(keyname, value); // Populate cache. resp.getWriter() .println("<h2>Stored " + keyname + " and " + value + " in Datastore and Memcache</h2>"); } else { resp.getWriter().println("<h2>You entered wrong query parameters</h2>"); } /* Entity tne = new Entity("TaskData", "Person"); alice.setProperty("gender", "female"); alice.setProperty("age", 20); */ resp.getWriter().println("</body></html>"); }