PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 66 lines | 53 code | 9 blank | 4 comment | 9 complexity | 8dda36d88136d0ba3214c08c07f0ffa8 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 UpdateFlight extends HttpServlet {
16
17  @Override
18  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
19    String key = req.getParameter("key");
20    String orig = req.getParameter("orig");
21    String dest = req.getParameter("dest");
22    if (key == null) {
23      resp.getWriter().println("No key provided.");
24      return;
25    }
26
27    if (orig == null) {
28      resp.getWriter().println("No origin provided.");
29      return;
30    }
31
32    if (dest == null) {
33      resp.getWriter().println("No destination provided.");
34      return;
35    }
36    if (PersistenceStandard.get() == PersistenceStandard.JPA) {
37      doPostJPA(Long.valueOf(key), orig, dest);
38    } else {
39      doPostJDO(Long.valueOf(key), orig, dest);
40    }
41    resp.sendRedirect("/");
42  }
43
44  private void doPostJDO(long key, String orig, String dest) {
45    PersistenceManager pm = PMF.get().getPersistenceManager();
46    try {
47      Flight f = pm.getObjectById(Flight.class, key);
48      f.setOrig(orig);
49      f.setDest(dest);
50    } finally {
51      pm.close();
52    }
53  }
54
55  private void doPostJPA(long key, String orig, String dest) {
56    EntityManager em = EMF.get().createEntityManager();
57    try {
58      Flight f = em.find(Flight.class, key);
59      f.setOrig(orig);
60      f.setDest(dest);
61    } finally {
62      em.close();
63    }
64  }
65
66}