OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 |
| 3 package com.google.api.client.sample.bigquery.appengine.dashboard; |
| 4 |
| 5 import com.google.appengine.api.datastore.Entity; |
| 6 import com.google.appengine.api.users.UserServiceFactory; |
| 7 import com.google.gson.stream.JsonWriter; |
| 8 |
| 9 import java.io.IOException; |
| 10 import java.util.List; |
| 11 |
| 12 import javax.servlet.http.HttpServlet; |
| 13 import javax.servlet.http.HttpServletRequest; |
| 14 import javax.servlet.http.HttpServletResponse; |
| 15 |
| 16 /** |
| 17 * This servlet responds to a post request with the data in the datastore for th
e |
| 18 * user in the form of json parseable by a DataTable constructor. Also returns |
| 19 * the stored message and whether their query failed. |
| 20 * |
| 21 * @author lparkinson@google.com (Laura Parkinson) |
| 22 */ |
| 23 public class DataServlet extends HttpServlet { |
| 24 |
| 25 // It's important that the first column be a string and the second a number. |
| 26 // Also, it is expected that these are the same length. |
| 27 private final String[] labels = new String[] |
| 28 {"State", "Year", "Average Mother Age", "Average Father Age", "U.S. Census
Region"}; |
| 29 private final String[] properties = new String[] |
| 30 {"state", "year", "average_mother_age", "average_father_age", "region"}; |
| 31 private final String[] types = new String[] {"string", "number", "number", "nu
mber", "string"}; |
| 32 |
| 33 /** |
| 34 * Attempts to retrieve results for the logged-in user. If the datastore cont
ains |
| 35 * results, they are written into the response as JSON. |
| 36 */ |
| 37 @Override |
| 38 protected void doPost(HttpServletRequest request, HttpServletResponse response
) |
| 39 throws IOException { |
| 40 JsonWriter jsonWriter = new JsonWriter(response.getWriter()).beginObject(); |
| 41 String userId = UserServiceFactory.getUserService().getCurrentUser().getUser
Id(); |
| 42 DatastoreUtils datastoreUtils = new DatastoreUtils(userId); |
| 43 |
| 44 String jobStatus = datastoreUtils.getUserJobStatus(); |
| 45 |
| 46 if (("DONE").equalsIgnoreCase(jobStatus)) { |
| 47 List<Entity> results = datastoreUtils.getResults(); |
| 48 if (!results.isEmpty()) { |
| 49 writeResultsToMotionChartJson(jsonWriter, results); |
| 50 } |
| 51 } |
| 52 |
| 53 jsonWriter.name("failed").value(datastoreUtils.hasUserQueryFailed()); |
| 54 jsonWriter.name("message").value(datastoreUtils.getUserMessage()); |
| 55 jsonWriter.name("lastRun").value(datastoreUtils.getUserLastRunMessage()); |
| 56 |
| 57 jsonWriter.endObject().close(); |
| 58 } |
| 59 |
| 60 /** |
| 61 * Converts the query results retrieved from the datastore to json parsable by
javascript |
| 62 * into a DataTable object for use with a motion chart. |
| 63 */ |
| 64 private void writeResultsToMotionChartJson(JsonWriter jsonWriter, Iterable<Ent
ity> results) |
| 65 throws IOException { |
| 66 jsonWriter.name("data").beginObject(); |
| 67 |
| 68 // Write the header. |
| 69 jsonWriter.name("cols").beginArray(); |
| 70 for (int i = 0; i < properties.length; i++) { |
| 71 jsonWriter.beginObject() |
| 72 .name("id").value(properties[i]) |
| 73 .name("label").value(labels[i]) |
| 74 .name("type").value(types[i]) |
| 75 .endObject(); |
| 76 } |
| 77 jsonWriter.endArray(); |
| 78 |
| 79 // Write the data. |
| 80 jsonWriter.name("rows").beginArray(); |
| 81 for (Entity entity : results) { |
| 82 jsonWriter.beginObject().name("c").beginArray(); |
| 83 for (int i = 0; i < properties.length; i++) { |
| 84 String value = ""; |
| 85 if (entity.getProperty(properties[i]) != null) { |
| 86 value = String.valueOf(entity.getProperty(properties[i])); |
| 87 } |
| 88 |
| 89 jsonWriter.beginObject().name("v").value(value).endObject(); |
| 90 } |
| 91 jsonWriter.endArray().endObject(); |
| 92 } |
| 93 jsonWriter.endArray(); |
| 94 |
| 95 jsonWriter.endObject(); |
| 96 } |
| 97 } |
OLD | NEW |