001: package example;
002:
003: import java.io.PrintWriter;
004: import java.io.IOException;
005:
006: import java.util.List;
007:
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import javax.servlet.ServletException;
013:
014: import javax.persistence.*;
015:
016: /**
017: * A client to illustrate the services of the CourseBean CMP bean.
018: */
019: public class CreateServlet extends HttpServlet {
020: @PersistenceContext(name="example")
021: private EntityManager _entityManager;
022:
023: public void init() {
024: Course course = null;
025:
026: try {
027: course = _entityManager.find(Course.class, new Integer(1));
028: } catch (Throwable e) {
029: }
030:
031: if (course == null) {
032: _entityManager.getTransaction().begin();
033:
034: try {
035: course = new Course("Potions", "Severus Snape");
036: _entityManager.persist(course);
037:
038: course = new Course("Transfiguration",
039: "Minerva McGonagall");
040: _entityManager.persist(course);
041:
042: course = new Course("Defense Against the Dark Arts",
043: "Remus Lupin");
044: _entityManager.persist(course);
045: } finally {
046: _entityManager.getTransaction().commit();
047: }
048: }
049: }
050:
051: public void service(HttpServletRequest req, HttpServletResponse res)
052: throws java.io.IOException, ServletException {
053: PrintWriter out = res.getWriter();
054:
055: res.setContentType("text/html");
056:
057: Query courseQuery = _entityManager
058: .createQuery("SELECT o FROM Course o");
059:
060: out.println("<h3>Initial Classes</h3>");
061:
062: displayCourses(out, courseQuery);
063:
064: Course divination = null;
065: Course creatures = null;
066:
067: _entityManager.getTransaction().begin();
068:
069: try {
070: divination = new Course("Divination", "Sybil Trelawney");
071:
072: // creates the divination course
073: _entityManager.persist(divination);
074:
075: creatures = new Course("Care of Magical Creatures",
076: "Rubeus Hagrid");
077:
078: // creates the creatures course
079: _entityManager.persist(creatures);
080:
081: out.println("<h3>Adding some classes</h3>");
082: displayCourses(out, courseQuery);
083: } finally {
084: // remove the courses
085: String sql = "SELECT o FROM Course o WHERE o.course=?1";
086:
087: Query findQuery = _entityManager.createQuery(sql);
088:
089: findQuery.setParameter(1, "Divination");
090: divination = (Course) findQuery.getSingleResult();
091:
092: if (divination != null)
093: _entityManager.remove(divination);
094:
095: findQuery.setParameter(1, "Care of Magical Creatures");
096: creatures = (Course) findQuery.getSingleResult();
097:
098: if (creatures != null)
099: _entityManager.remove(creatures);
100:
101: _entityManager.getTransaction().commit();
102: }
103:
104: out.println("<h3>Removing the new classes</h3>");
105: displayCourses(out, courseQuery);
106: }
107:
108: private void displayCourses(PrintWriter out, Query query)
109: throws IOException {
110: List list = query.getResultList();
111:
112: for (int i = 0; i < list.size(); i++) {
113: Course course = (Course) list.get(i);
114:
115: out.println(course.getCourse() + " is taught by "
116: + course.getTeacher() + "<br>");
117: }
118: }
119: }
|