Datastore

827 views
782 views

Published on

Small explanation for Peno 3. How to use the concept of foreign keys in Google App engine and unidexed properties. How to pass objects from a servlet to a jsp .

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Datastore

  1. 1. Data Store(Google App Engine) José Luis Santos joseluis.santos@cs.kuleuven.be
  2. 2. ServletDatastoreServicedatastore = DatastoreServiceFactory.getDatastoreService();Entity person = new Entity("Person", "test");person.setUnindexedProperty("name", "TestUser"); //Unindexed properties for these properties that you willnot filter onEntity weddingPhoto = new Entity("Photo", person.getKey()); // We link this Entity to another (Foreign key)weddingPhoto.setProperty("imageUrl", "http://scm-l3.technorati.com/11/02/05/25749/marriage.jpg");weddingPhoto.setProperty("date", new Date());Entity weddingVideo = new Entity("Video", person.getKey()); // We link this Entity to another (Foreign key)weddingVideo.setProperty("videoUrl", "http://www.criticallayouts.com/images/rsgallery/original/just-married-cans-ag1.gif");weddingVideo.setUnindexedProperty("date", new Date());datastore.put(person);datastore.put(weddingPhoto);datastore.put(weddingVideo);req.setAttribute("Key", person.getKey()); //Sending an object to a jsp fileRequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/store.jsp");dispatcher.forward(req,resp);
  3. 3. JSPDatastoreServicedatastore = DatastoreServiceFactory.getDatastoreService();Key key = (Key)request.getAttribute("Key"); //We get an object not only an Stringif (key != null){Query userMediaQuery = new Query();userMediaQuery.setAncestor(key);// Ancestor queries return ancestors by default. This filter excludes the ancestor from query results.userMediaQuery.addFilter(Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN, key);// Returns both weddingPhoto and weddingVideo even though they are different entity kinds.List<Entity> results = datastore.prepare(userMediaQuery).asList( FetchOptions.Builder.withDefaults());for (inti=0;i<results.size();i++){if (results.get(i).getKind().compareTo("Video")==0){%>This is a photo:<br/><imgsrc="<%=results.get(i).getProperty("videoUrl")%>"></img><br/><% }else if (results.get(i).getKind().compareTo("Photo")==0){%>This is a video:<br/><imgsrc="<%=results.get(i).getProperty("imageUrl")%>"></img><br/><% }%>

×