View Javadoc

1   //----------------------------------------------------------------------
2   
3   //PerfectJPattern: "Design patterns are good but components are better!" 
4   //Example.java Copyright (c) 2009 Giovanni Azua Garcia
5   //bravegag@hotmail.com
6   
7   //This program is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU General Public License
9   //as published by the Free Software Foundation; either version 3
10  //of the License, or (at your option) any later version.
11  
12  //This program is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  //GNU General Public License for more details.
16  
17  //You should have received a copy of the GNU General Public License
18  //along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
20  //MA 02110-1301, USA.
21  
22  //----------------------------------------------------------------------
23  package org.perfectjpattern.jee.integration.dao.jpa;
24  
25  import java.text.*;
26  import java.util.*;
27  
28  import org.perfectjpattern.example.datamodel.*;
29  import org.perfectjpattern.example.datamodel.visitor.*;
30  import org.perfectjpattern.jee.api.business.servicelocator.*;
31  import org.perfectjpattern.jee.business.servicelocator.*;
32  import org.slf4j.*;
33  
34  /**
35   * Startup Main for the JPA Generic Dao example code
36   * 
37   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
38   * @version $ $Date: Dec 5, 2008 2:04:37 AM $
39   */
40  //CHECKSTYLE:OFF
41  public final
42  class Example
43  // CHECKSTYLE:ON
44  {
45      //------------------------------------------------------------------------
46      // public
47      //------------------------------------------------------------------------
48      public static void 
49      main(String[] anArguments)
50      throws ParseException
51      {
52          // tweaking OpenEJB logging
53          System.setProperty("log4j.category.OpenEJB", "debug"); 
54  
55          //---------------------------------------------------------------
56          // use ServiceLocator to load the Local IGenericDao EJB
57          //---------------------------------------------------------------
58          IServiceLocator myServiceLocator = ServiceLocator.getInstance();
59          IMovieBaseDao myMovieBaseDao = myServiceLocator.locate(
60              "IMovieBaseDaoLocal");
61          
62          //---------------------------------------------------------------
63          // call IBaseDao methods
64          //---------------------------------------------------------------
65          myMovieBaseDao.create(new Movie("Fresa y chocolate", 
66              "Tomas Gutierrez Alea", 1994));        
67          myMovieBaseDao.create(new Movie("Vita e bella, La", "Roberto Benigni",
68              1997));
69          myMovieBaseDao.create(new Movie("Taken", "Pierre Morel", 2008));
70          
71          //---------------------------------------------------------------
72          // clear cache: demonstrates accessing whatever Session lies behind 
73          //---------------------------------------------------------------
74          myMovieBaseDao.getSession().clear();
75          
76          //---------------------------------------------------------------
77          // find all Movies
78          //---------------------------------------------------------------
79          StringBuilder myBuilder = new StringBuilder();
80          List<Movie> myMovies = myMovieBaseDao.findAll();
81          ToStringVisitor myVisitor = new ToStringVisitor();
82          for (Movie myMovie : myMovies)
83          {
84              myVisitor.visit(myMovie);         
85              String myResult = myVisitor.getResult();            
86              myBuilder.append(myResult);
87          }        
88          
89          //---------------------------------------------------------------
90          // use ServiceLocator to load the Local IGenericDao EJB
91          //---------------------------------------------------------------
92          IMovieGenericDao myMovieGenericDao = myServiceLocator.locate(
93              "IMovieGenericDaoLocal");
94  
95          //---------------------------------------------------------------
96          // find all Movies produced in 2008
97          //---------------------------------------------------------------
98          int myYear = 2008;
99          myMovies = myMovieGenericDao.findByYear(myYear);
100         for (Movie myMovie : myMovies)
101         {
102             myVisitor.visit(myMovie);         
103             String myResult = myVisitor.getResult();            
104             myBuilder.append(myResult);
105         }        
106 
107         theLogger.debug(myBuilder.toString());
108         
109         myServiceLocator.shutdown();        
110     }
111 
112     //------------------------------------------------------------------------
113     // protected
114     //------------------------------------------------------------------------
115     protected static void
116     setLogger(Logger aLogger)
117     {
118         theLogger = aLogger;
119     }    
120 
121     //------------------------------------------------------------------------
122     // members
123     //------------------------------------------------------------------------
124     /**
125      * Provides logging facilities for this class 
126      */
127     private static Logger theLogger = LoggerFactory.getLogger(Example.class);
128 }