PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 64 lines | 54 code | 6 blank | 4 comment | 5 complexity | 34fad32bdad012b8c8ecffffed319994 MD5 | raw file
 1// Copyright 2008 Google Inc. All Rights Reserved.
 2package com.google.appengine.demos.helloorm;
 3
 4import java.io.IOException;
 5
 6import javax.jdo.PersistenceManager;
 7import javax.persistence.EntityManager;
 8import javax.servlet.http.HttpServlet;
 9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12/**
13 * @author Max Ross <maxr@google.com>
14 */
15public class GetFlight extends HttpServlet {
16
17  @Override
18  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
19    resp.setContentType("text/html");
20    String key = req.getParameter("key");
21    if (key == null) {
22      resp.getWriter().println("No key provided.");
23      return;
24    }
25    Flight f;
26    if (PersistenceStandard.get() == PersistenceStandard.JPA) {
27      f = findJPA(Long.valueOf(key));
28    } else {
29      f = findJDO(Long.valueOf(key));
30    }
31    resp.getWriter().println("<form action=\"updateFlight\" method=\"post\">");
32    resp.getWriter().println("<input name=\"key\" type=\"hidden\" value=\"" + key + "\"/>");
33    resp.getWriter().println("<table>");
34    resp.getWriter().println("<tr>");
35    resp.getWriter().println("<th>Origin</th><td><input name=\"orig\" type=\"text\" value=\"" + f.getOrig() + "\"/></td>");
36    resp.getWriter().println("<th>Destination</th><td><input name=\"dest\" type=\"text\" value=\"" + f.getDest() + "\"/></td>");
37    resp.getWriter().println("</tr>");
38    resp.getWriter().println("<tr><td><input type=\"submit\" value=\"Update Flight\"></td></tr>");
39    resp.getWriter().println("</table>");
40    resp.getWriter().println("</form>");
41    resp.getWriter().println("<form action=\"deleteFlight\" method=\"post\">");
42    resp.getWriter().println("<input name=\"key\" type=\"hidden\" value=\"" + key + "\"/>");
43    resp.getWriter().println("<input type=\"submit\" value=\"Delete Flight\"></td></tr>");
44    resp.getWriter().println("</form>");
45  }
46
47  private Flight findJPA(long key) {
48    EntityManager em = EMF.get().createEntityManager();
49    try {
50      return em.find(Flight.class, key);
51    } finally {
52      em.close();
53    }
54  }
55
56  private Flight findJDO(long key) {
57    PersistenceManager pm = PMF.get().getPersistenceManager();
58    try {
59      return pm.getObjectById(Flight.class, key);
60    } finally {
61      pm.close();
62    }
63  }
64}