Author: ppoddar
Date: Tue Nov 23 07:30:28 2010
New Revision: 1038006

URL: http://svn.apache.org/viewvc?rev=1038006&view=rev
Log:
OPENJPA-1859: Separate Data Loading from SimpleApp.

Modified:
    openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/Movie.java
    
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/SimpleApp.java
    
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/resources/demo/index.html

Modified: 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/Movie.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/Movie.java?rev=1038006&r1=1038005&r2=1038006&view=diff
==============================================================================
--- openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/Movie.java 
(original)
+++ openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/Movie.java 
Tue Nov 23 07:30:28 2010
@@ -24,7 +24,6 @@ import java.util.Set;
 
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
-import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.persistence.OneToMany;
 
@@ -36,8 +35,7 @@ import javax.persistence.OneToMany;
 @Entity
 public class Movie {
     @Id
-    @GeneratedValue
-    private long id;
+    private String id;
     private String title;
     private int year;
     @OneToMany(fetch=FetchType.EAGER)
@@ -46,13 +44,15 @@ public class Movie {
     protected Movie() {
         
     }
-    public Movie(String title, int year) {
+    
+    public Movie(String id, String title, int year) {
         super();
+        this.id = id;
         this.title = title;
         this.year = year;
     }
     
-    public long getId() {
+    public String getId() {
         return id;
     }
     

Modified: 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/SimpleApp.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/SimpleApp.java?rev=1038006&r1=1038005&r2=1038006&view=diff
==============================================================================
--- 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/SimpleApp.java 
(original)
+++ 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/java/demo/SimpleApp.java 
Tue Nov 23 07:30:28 2010
@@ -49,17 +49,21 @@ import javax.servlet.http.HttpServletRes
 @SuppressWarnings("serial")
 public class SimpleApp extends HttpServlet {
     
+    private static String UNIT_NAME = "jestdemo";
     
     @Override 
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
-        String unit = config.getInitParameter("persistence.unit");
-        config.getServletContext().log("SimpleApp initializing with 
persistence unit [" + unit + "]");
-        Map<String,Object> props = new HashMap<String, Object>();
-        props.put("openjpa.EntityManagerFactoryPool", "true");
-        EntityManagerFactory emf = 
Persistence.createEntityManagerFactory(unit, props);
-        config.getServletContext().getContextPath();
-        populate(emf.createEntityManager(), config.getServletContext());
+        config.getServletContext().log("Initializing persistence unit [" + 
UNIT_NAME + "]");
+        try {
+            Map<String,Object> props = new HashMap<String, Object>();
+            props.put("openjpa.EntityManagerFactoryPool", "true");
+            EntityManagerFactory emf = 
Persistence.createEntityManagerFactory(UNIT_NAME, props);
+            new DataLoader().populate(emf.createEntityManager());
+        } catch (Exception e) {
+            throw new ServletException(e);
+        }
+        config.getServletContext().log("Initialized with persistence unit [" + 
UNIT_NAME + "]");
     }
     
     @Override
@@ -71,102 +75,4 @@ public class SimpleApp extends HttpServl
             out.write((char)c);
         }
     }
-    
-
-    public void populate(EntityManager em, ServletContext ctx)  {
-        Long count = em.createQuery("select count(m) from Movie m", 
Long.class).getSingleResult();
-        if (count != null && count.longValue() > 0) {
-            ctx.log("Found " + count + " Movie records in the database");
-            return;
-        }
-        ctx.log("Populating Movie database with " + MOVIE_DATA.length + " 
movies and " + ACTOR_DATA.length + " actors");
-        
-        List<Actor> actors = createActors();
-        List<Movie> movies = createMovies();
-        linkActorAndMovie(actors, movies);
-        makePartner(actors);
-        em.getTransaction().begin();
-        for (Actor a : actors) {
-            em.persist(a);
-        }
-        for (Movie m : movies) {
-            em.persist(m);
-        }
-        em.getTransaction().commit();
-    }
-    
-    List<Actor> createActors() {
-        List<Actor> actors = new ArrayList<Actor>();
-        for (Object[] a : ACTOR_DATA) {
-            Actor actor = new Actor((String)a[0], (String)a[1], (String)a[2], 
(Actor.Gender)a[3], (Date)a[4]);
-            actors.add(actor);
-        }
-        return actors;
-    }
-    
-    List<Movie> createMovies() {
-        List<Movie> movies = new ArrayList<Movie>();
-        for (Object[] m : MOVIE_DATA) {
-            Movie movie = new Movie((String)m[0], (Integer)m[1]);
-            movies.add(movie);
-        }
-        return movies;
-    }
-    
-    void linkActorAndMovie(List<Actor> actors, List<Movie> movies) {
-        for (Actor a : actors) {
-            int n = rng.nextInt(movies.size());
-            for (int i = 0; i < n; i++) {
-                Movie m = random(movies);
-                a.addMovie(m);
-                m.addActor(a);
-            }
-        }
-    }
-    
-    void makePartner(List<Actor> actors) {
-        for (Actor p : actors) {
-            if (p.getPartner() != null)
-                continue;
-            Actor f = random(actors);
-            if (f.getPartner() == null && p.getGender() != f.getGender()) {
-               p.setPartner(f);
-               f.setPartner(p);
-            }
-       }
-    }
-    
-    /**
-     * Select a random element from the given list. 
-     */
-    private <T> T random(List<T> list) {
-        return list.get(rng.nextInt(list.size()));
-    }
-    
-    private static Random rng = new Random();
-    
-    public static Object[][] MOVIE_DATA = {
-        new Object[] {"One flew over the cuckoo's nest", 1980},
-        new Object[] {"Everyone Says I Love You", 1980},
-        new Object[] {"Where Eagles Dare", 1980},
-        new Object[] {"Fight Club", 1980},
-        new Object[] {"Horse Whisperer", 1980},
-    };
-
-    @SuppressWarnings("deprecation")
-    public static Object[][] ACTOR_DATA = {
-        new Object[] {"m01", "Robert", "Redford", Actor.Gender.Male, new 
Date(1950, 1, 12)},
-        new Object[] {"m02", "Robert", "De Niro", Actor.Gender.Male, new 
Date(1940, 4, 14)},
-        new Object[] {"m03", "Al",     "Pacino",  Actor.Gender.Male, new 
Date(1950, 1, 12)},
-        new Object[] {"m04", "Brad",   "Pitt",    Actor.Gender.Male, new 
Date(1940, 4, 14)},
-        new Object[] {"m05", "Clint",  "Eastwood",Actor.Gender.Male, new 
Date(1950, 1, 12)},
-        
-        new Object[] {"f01", "Meryl",   "Streep",    Actor.Gender.Female, new 
Date(1940, 4, 14)},
-        new Object[] {"f02", "Anglina", "Jolie",     Actor.Gender.Female, new 
Date(1950, 1, 12)},
-        new Object[] {"f03", "Goldie",  "Hawn",      Actor.Gender.Female, new 
Date(1940, 4, 14)},
-        new Object[] {"f04", "Diane",   "Keaton",    Actor.Gender.Female, new 
Date(1950, 1, 12)},
-        new Object[] {"f05", "Catherine", "Hepburn", Actor.Gender.Female, new 
Date(1940, 4, 14)},
-    };
-    
-
 }

Modified: 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/resources/demo/index.html
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/jest/openjpa-examples/jest/src/main/resources/demo/index.html?rev=1038006&r1=1038005&r2=1038006&view=diff
==============================================================================
--- 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/resources/demo/index.html 
(original)
+++ 
openjpa/sandboxes/jest/openjpa-examples/jest/src/main/resources/demo/index.html 
Tue Nov 23 07:30:28 2010
@@ -17,17 +17,78 @@ specific language governing permissions 
 under the License.
 -->
 <html>
+<head>
+<style type="text/css">
+hr {color:sienna;}
+p {margin-left:20px;}
+body {background-image:url("images/back40.gif");
+.tag {
+  color:green;
+  font-weight:bold;
+}
+</style>
+</head>
 <body>
-  <h1>Do-Nothing Application</h1>  
+  <h1>DNA: Do-Nothing Application</h1>  
+  <hr>
+  This DNA application is deployed as a HTTP Servlet.<br>
+  
+  The servlet creates an OpenJPA persistence unit at initialization.<br>
+  
+  It does nothing else, other than serving this single web page you are 
reading now.
+  The fact that you are reading this page means the persistence unit has been 
initialized. 
+  <p>
+  
+  <span style="font-size:1.2em;color:green;font-weight:bold">DNA application 
is used to demonstrate </span> 
+  <A HREF="../jest">JEST</A>.
   <br>
-  This do-nothing application initializes a JPA persistence unit with OpenJPA 
as provider and its <em>only</em> 
-  response is the page you are reading now. 
-  <br>
-  This application is used to demonstrate JEST.
-  <br>
-  This application is deployed along with a JEST Servlet within the same 
module scope.
-  As long as JEST servlet knows the name of the persistence unit of this 
do-nothing
-  application, JEST Servlet can browse the domain model, execute query or find 
in
-  a generic fashion.
+  
+  <h2>Requirement for JEST</h2>
+  
+  The only requirements for an application to enable JEST are
+  
+  <p>
+  &#9658; JEST Servlet must be <A href="#web.xml">deployed</A> within the same 
module scope of the application.  <br>
+  &#9658; The unit name of the persistence unit used by the application must 
be known to JEST Servlet  <br>
+  &#9658; The persistence unit must be configured with <br>
+  &nbsp;&nbsp; <span 
style="color:green;font-weight:bold;text-align:center;background-color: 
#EEEEEE"><tt>openjpa.EntityManagerFactoryPool=true</tt></span> 
+  <p>
+  Once JEST servlet knows the name of the persistence unit, it can <br>
+  
+  &#9658; browse the domain model<br>
+  &#9658; execute query <br>
+  from any web browser in a <em>meta-data driven</em>, <em>generic</em> 
fashion 
+  i.e. without knowing anything further about he application. 
+  <p>
+  <p>
+  <hr>
+  <a name="web.xml"></a>
+  <h5>WEB-INF/web.xml : Deployment Descriptor for JEST-enabled Application</h5>
+  <div style="border:1px solid black; width:700px;background-color:#EEEEEE">
+  <pre>
+ <span style="color:gray;font-weight:bold;">&lt;servlet></span>
+   <span style="color:gray;font-weight:bold;">&lt;servlet-name></span><span 
style="color:blue;">demo</span><span 
style="color:gray;font-weight:bold;">&lt;/servlet-name>
+   &lt;servlet-class></span><span 
style="color:blue;">demo.SimpleApp</span><span 
style="color:gray;font-weight:bold;">&lt;/servlet-class>
+ &lt;/servlet>
+   &lt;servlet-mapping>
+   &lt;servlet-name></span><span style="color:blue;">demo</span><span 
style="color:gray;font-weight:bold;">&lt;/servlet-name>
+ &lt;url-pattern></span><span style="color:blue;">/app/*</span><span 
style="color:gray;font-weight:bold;">&lt;/url-pattern>
+ &lt;/servlet-mapping>
+  
+ <span style="color:green;font-weight:bold;">&lt;!-- Deployment descriptor for 
JESTServlet. --></span>
+ &lt;servlet>
+   &lt;servlet-name></span><span style="color:red;">jest</span><span 
style="color:gray;font-weight:bold;">&lt;/servlet-name>
+   &lt;servlet-class></span><span 
style="color:red;font-weight:bold;">org.apache.openjpa.persistence.jest.JESTServlet</span><span
 style="color:gray;font-weight:bold;">&lt;/servlet-class>
+   &lt;init-param>
+     &lt;param-name></span><span 
style="color:red;">persistence.unit</span><span 
style="color:gray;font-weight:bold;">&lt;/param-name>
+     &lt;param-value></span><span 
style="color:red;font-weight:bold;">jestdemo</span><span 
style="color:gray;font-weight:bold;">&lt;/param-value>
+   &lt;/init-param>
+ &lt;/servlet>
+ &lt;servlet-mapping>
+   &lt;servlet-name></span><span style="color:red;">jest</span><span 
style="color:gray;font-weight:bold;">&lt;/servlet-name>
+   &lt;url-pattern></span><span 
style="color:red;font-weight:bold;">/jest/*</span><span 
style="color:gray;font-weight:bold;">&lt;/url-pattern>
+ &lt;/servlet-mapping></span>
+</pre>
+  </div>
 </body>
 </html>
\ No newline at end of file


Reply via email to