PageRenderTime 120ms CodeModel.GetById 104ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/demos/helloorm/src/com/google/appengine/demos/helloorm/GetFlights.java

http://datanucleus-appengine.googlecode.com/
Java | 144 lines | 123 code | 17 blank | 4 comment | 8 complexity | abff1351d16268b11dd8e85438edb3cc MD5 | raw file
  1// Copyright 2008 Google Inc. All Rights Reserved.
  2package com.google.appengine.demos.helloorm;
  3
  4import java.io.IOException;
  5import java.util.Collection;
  6import java.util.List;
  7
  8import javax.jdo.PersistenceManager;
  9import javax.persistence.EntityManager;
 10import javax.servlet.http.HttpServlet;
 11import javax.servlet.http.HttpServletRequest;
 12import javax.servlet.http.HttpServletResponse;
 13
 14public class GetFlights extends HttpServlet {
 15
 16  private static final String DEFAULT_QUERY = "select f from " + Flight.class.getName() + " as f";
 17
 18  @Override
 19  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 20    doGet(req, resp);
 21  }
 22
 23  @Override
 24  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
 25    resp.setContentType("text/html");
 26    long start = System.currentTimeMillis();
 27    Collection<Flight> flights;
 28    String query = DEFAULT_QUERY;
 29    String customQuery = req.getParameter("q");
 30    if (customQuery != null) {
 31      query = customQuery;
 32    }
 33    if (PersistenceStandard.get() == PersistenceStandard.JPA) {
 34      flights = queryJPA(query);
 35    } else {
 36      flights = queryJDO(query);
 37    }
 38    printQueryFlightForm(query, resp);
 39
 40    resp.getWriter().println("<table>");
 41    printTableHeader(resp);
 42
 43    printFlights(resp, flights);
 44    resp.getWriter().println("</table>");
 45    printAddFlightForm(query, resp);
 46    resp.getWriter().println("Request time in millis: " + (System.currentTimeMillis() - start));
 47    resp.getWriter().println("<br>");
 48    printPersistenceStandardForm(query, resp);
 49  }
 50
 51  private Collection<Flight> queryJDO(String query) {
 52    PersistenceManager pm = PMF.get().getPersistenceManager();
 53    try {
 54      List<Flight> flights = (List<Flight>) pm.newQuery(query).execute();
 55      // Force all results to be pulled back before we close the entity manager.
 56      // We could have also called pm.detachCopyAll() 
 57      flights.size();
 58      return flights;
 59    } finally {
 60      pm.close();
 61    }
 62  }
 63
 64  private List<Flight> queryJPA(String query) {
 65    EntityManager em = EMF.get().createEntityManager();
 66    try {
 67      List<Flight> flights = em.createQuery(query).getResultList();
 68      // force all results to be pulled back before we close the entity manager
 69      flights.size();
 70      return flights;
 71    } finally {
 72      em.close();
 73    }
 74  }
 75
 76  private void printQueryFlightForm(String query, HttpServletResponse resp) throws IOException {
 77    PersistenceStandard ps = PersistenceStandard.get();
 78    resp.getWriter().println("<form action=\"/\" method=\"post\">");
 79    resp.getWriter().println(ps.name() + " Query for flights: "
 80        + "<input name=\"q\" type=\"text\" size=\"100\" "
 81        + "value=\"" + (query == null ? "" : query) + "\"/>");
 82    resp.getWriter().println("<input type=\"submit\" value=\"Run Query\">");
 83    resp.getWriter().println("</form>");
 84  }
 85
 86  private void printPersistenceStandardForm(String query, HttpServletResponse resp) throws IOException {
 87    PersistenceStandard ps = PersistenceStandard.get();
 88    resp.getWriter().println("Persistence standard is " + ps.name());
 89
 90    resp.getWriter().println("<form action=\"updatePersistenceStandard\" method=\"post\">");
 91    resp.getWriter().println("<input type=\"submit\" value=\"Switch to " + ps.getAlternate() + "\">");
 92    resp.getWriter().println(
 93        "<input name=\"persistenceStandard\" type=\"hidden\" value=\"" + ps.getAlternate() + "\"/>");
 94    resp.getWriter().println("<input name=\"q\" type=\"hidden\" value=\"" + query + "\"/>");
 95    resp.getWriter().println("</form>");
 96  }
 97
 98  private void printAddFlightForm(String query, HttpServletResponse resp) throws IOException {
 99    resp.getWriter().println("<form action=\"addFlight\" method=\"post\">");
100    resp.getWriter().println("<table>");
101    resp.getWriter().println("<tr>");
102    resp.getWriter().println("<th>Origin</th><td><input name=\"orig\" type=\"text\"/></td>");
103    resp.getWriter().println("<th>Destination</th><td><input name=\"dest\" type=\"text\"/></td>");
104    resp.getWriter().println("</tr>");
105    resp.getWriter().println("<tr><td><input type=\"submit\" value=\"Add Flight\"></td></tr>");
106    resp.getWriter().println("<input name=\"q\" type=\"hidden\" value=\"" + query + "\"/>");
107    resp.getWriter().println("</table>");
108    resp.getWriter().println("</form>");
109  }
110
111  private void printFlights(HttpServletResponse resp, Collection<Flight> flights) throws IOException {
112    if (flights.isEmpty()) {
113      resp.getWriter().println("<br>No Flights!");
114    }
115    int index = 1;
116    for (Flight f : flights) {
117      resp.getWriter().println("<tr>");
118      resp.getWriter().println("<td>");
119      resp.getWriter().println("<a href=\"getFlight?key=" + f.getId() + "\"> " + index++ + "</a>");
120      resp.getWriter().println("</td>");
121      resp.getWriter().println("<td>");
122      resp.getWriter().println(f.getOrig());
123      resp.getWriter().println("</td>");
124      resp.getWriter().println("<td>");
125      resp.getWriter().println(f.getDest());
126      resp.getWriter().println("</td>");
127      resp.getWriter().println("</tr>");
128    }
129  }
130
131  private void printTableHeader(HttpServletResponse resp) throws IOException {
132    resp.getWriter().println("<tr>");
133    resp.getWriter().println("<td>");
134    resp.getWriter().println("</td>");
135    resp.getWriter().println("<td>");
136    resp.getWriter().println("Origin");
137    resp.getWriter().println("</td>");
138    resp.getWriter().println("<td>");
139    resp.getWriter().println("Dest");
140    resp.getWriter().println("</td>");
141    resp.getWriter().println("</tr>");
142  }
143
144}