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

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

http://datanucleus-appengine.googlecode.com/
Java | 53 lines | 42 code | 7 blank | 4 comment | 6 complexity | b500fe5abdc69282863d0e88986fc063 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 DeleteFlight extends HttpServlet {
16
17  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
18    String key = req.getParameter("key");
19    if (key == null) {
20      resp.getWriter().println("No key provided.");
21    }
22    if (PersistenceStandard.get() == PersistenceStandard.JPA) {
23      doPostJPA(Long.valueOf(key));
24    } else {
25      doPostJDO(Long.valueOf(key));
26    }
27    resp.sendRedirect("/");
28  }
29
30  private void doPostJDO(long key) {
31    PersistenceManager pm = PMF.get().getPersistenceManager();
32    try {
33      Flight f = pm.getObjectById(Flight.class, key);
34      pm.deletePersistent(f);
35    } finally {
36      if (pm.currentTransaction().isActive()) {
37        pm.currentTransaction().rollback();
38      }
39      pm.close();
40    }
41  }
42
43  private void doPostJPA(long key) {
44    EntityManager em = EMF.get().createEntityManager();
45    try {
46      Flight f = em.find(Flight.class, key);
47      em.remove(f);
48    } finally {
49      em.close();
50    }
51  }
52
53}