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  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.jee.integration.dao.spring;
22  
23  import java.text.*;
24  import java.util.*;
25  
26  import org.perfectjpattern.example.datamodel.*;
27  import org.perfectjpattern.example.datamodel.visitor.*;
28  import org.perfectjpattern.jee.api.integration.dao.*;
29  import org.slf4j.*;
30  import org.springframework.context.support.*;
31  
32  /**
33   * Startup Main for the Hibernate-Spring Generic DAO Pattern Example code
34   * 
35   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36   * @version $ $Date: Dec 5, 2008 2:06:29 AM $
37   */
38  //CHECKSTYLE:OFF
39  public final
40  class Example
41  // CHECKSTYLE:ON
42  {
43      //------------------------------------------------------------------------
44      // public
45      //------------------------------------------------------------------------
46      public static void 
47      main(String[] anArguments)
48      throws ParseException
49      {
50          // ToStringVisitor needs loading a ResourceBundle
51          Locale.setDefault(new Locale("en", "US"));        
52  
53          // initialize IoC Spring container
54          new ClassPathXmlApplicationContext(
55              new String[] 
56              {
57                  "genericDao-applicationContext.xml",
58                  "example-applicationContext.xml"
59              });
60          
61          try
62          {
63              //---------------------------------------------------------------
64              // Create fixture/ business model
65              //---------------------------------------------------------------
66              
67              // available products
68              Product myProduct1 = new Product("Nikon D700", 3200.0);
69              Product myProduct2 = new Product("Nikon D300", 2000.0);
70              Product myProduct3 = new Product("Nikon D40x", 350.0);
71              Product myProduct4 = new Product("Nikon 80-400mm", 1500.0);
72              Product myProduct5 = new Product("Nikon 70-200mm", 1800.0);        
73              Product myProduct6 = new Product("Nikon 17-55mm", 1200.0);        
74              
75              // new customer
76              Customer myCustomer1 = new Customer("Ernesto");        
77              
78              // add customer orders
79              Date myDate = DATE_FORMAT.parse("14.03.2007 08:00:00");
80              Set<Product> myProducts = new HashSet<Product>();
81              myProducts.addAll(Arrays.asList(new Product[] {myProduct1, 
82                  myProduct4, myProduct5 }));
83              Order myOrder1 = new Order(myCustomer1, myDate, myProducts);
84              myCustomer1.getOrders().add(myOrder1);
85  
86              //-----------------------------------------------------------
87              // Accesses the AbstractFactory singleton LocalDaoFactory and 
88              // persists Customer model in storage. Note how the 
89              // LocalDaoFactory hides the framework-specific complexities. 
90              // LocalDaoFactory gets constructed from the Spring 
91              // configuration to:
92              //
93              //   - HibernateCurrentSessionStrategy: 
94              //      a) Receives a new SessionFactory via IoC defined in 
95              //         the Spring configuration
96              //      b) Uses the getCurrentSession Hibernate API that 
97              //         looks into the "current_session_context_class" 
98              //         configuration, for this example is set to "thread"
99              //
100             //   - HibernateConfiguredTransactionStrategy:
101             //      Simply accesses the Session Strategy and calls 
102             //      getTransaction, that will load whatever is configured 
103             //      in "hibernate.transaction_factory" configuration
104             //-----------------------------------------------------------
105             ICustomerDao myCustomerDao = LocalDaoFactory.
106                 getInstance().createCustomerDao();
107             myCustomerDao.create(myCustomer1);
108             
109             //-----------------------------------------------------------
110             // Commit changes. Note how accessing whatever Transaction 
111             // implementation is transparent to the client
112             //-----------------------------------------------------------
113             myCustomerDao.getTransaction().commit();
114 
115             //-----------------------------------------------------------
116             // Extend fixture/ business model
117             //-----------------------------------------------------------
118             Customer myCustomer2 = new Customer("Giovanni");        
119             
120             // add customer orders
121             myDate = DATE_FORMAT.parse("17.11.2007 08:00:00");
122             myProducts = new HashSet<Product>();
123             myProducts.addAll(Arrays.asList(new Product[] {myProduct1, 
124                 myProduct2, myProduct3 }));
125             myOrder1 = new Order(myCustomer2, myDate, myProducts);
126             myCustomer2.getOrders().add(myOrder1);
127             
128             myDate = DATE_FORMAT.parse("15.01.2008 17:35:00");
129             myProducts = new HashSet<Product>();
130             myProducts.addAll(Arrays.asList(new Product[] {myProduct4, 
131                 myProduct5, myProduct6 }));
132             Order myOrder2 = new Order(myCustomer2, myDate, myProducts);
133             myCustomer2.getOrders().add(myOrder2);
134             
135             //-----------------------------------------------------------
136             // Persist Customer models in storage
137             //-----------------------------------------------------------
138             myCustomerDao.create(myCustomer2);
139             
140             //-----------------------------------------------------------
141             // Commit changes. 
142             //-----------------------------------------------------------
143             myCustomerDao.getTransaction().commit();
144 
145             //-----------------------------------------------------------
146             // Now we can try our new search capabilities
147             //-----------------------------------------------------------
148             long myMinimum = 2;
149             Date myBegin = DATE_FORMAT.parse("01.01.2007 00:00:00");
150             Date myEnd = DATE_FORMAT.parse("01.01.2009 00:00:00");
151             List<Customer> myCustomers = myCustomerDao.
152                 findByNumberOfOrdersBetween(myBegin, myEnd, myMinimum);
153             
154             ToStringVisitor myVisitor = new ToStringVisitor();
155             for (Customer myCustomer : myCustomers)
156             {
157                 myVisitor.visit(myCustomer);         
158                 String myResult = myVisitor.getResult();
159                 
160                 theLogger.debug(myResult);                
161             }            
162         }
163         finally
164         {
165             //-----------------------------------------------------------
166             // Final cleanup
167             //-----------------------------------------------------------
168             LocalDaoFactory.getInstance().shutdown();
169         }                
170     }
171 
172     //------------------------------------------------------------------------
173     // protected
174     //------------------------------------------------------------------------
175     protected static void
176     setLogger(Logger aLogger)
177     {
178         theLogger = aLogger;
179     }    
180 
181     //------------------------------------------------------------------------
182     // members
183     //------------------------------------------------------------------------
184     /**
185      * Provides logging facilities for this class 
186      */
187     private static Logger theLogger = LoggerFactory.getLogger(Example.class);
188     private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
189         "dd.MM.yyyy HH:mm:ss");    
190 }