View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // EntityManagerAdapter.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;
22  
23  import java.lang.reflect.*;
24  
25  import javax.persistence.*;
26  
27  import org.perfectjpattern.core.api.structural.adapter.*;
28  import org.perfectjpattern.core.structural.adapter.*;
29  import org.perfectjpattern.jee.api.integration.dao.*;
30  
31  /**
32   * Adapts JPA's {@link EntityManager} to the JPA implementation-free 
33   * PerfectJPattern's {@link ISession} definition 
34   * 
35   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36   * @version $Revision: 1.0 $Date: Feb 10, 2009 8:26:06 PM $
37   */ 
38  public final 
39  class EntityManagerAdapter
40  extends Adapter<ISession, EntityManager>
41  {
42      //------------------------------------------------------------------------
43      // public
44      //------------------------------------------------------------------------
45      /**
46       * Constructs a {@link EntityManagerAdapter} from the Adaptee 
47       * {@link EntityManager} instance
48       * 
49       * @param anAdaptee The Adaptee {@link EntityManager}
50       * @throws IllegalArgumentException 'anAdaptee' must not be null
51       */
52      public 
53      EntityManagerAdapter(EntityManager anAdaptee)
54      throws IllegalArgumentException
55      {
56          super(ISession.class, anAdaptee);
57      }
58  
59      //------------------------------------------------------------------------
60      // protected
61      //------------------------------------------------------------------------
62      /** 
63       * {@inheritDoc}
64       */
65      @Override
66      protected Object 
67      invokeUnderlying(Method aMethod, Object[] anArguments)
68      throws Throwable
69      {
70          try
71          {
72              return super.invokeUnderlying(aMethod, anArguments);
73          }
74          // CHECKSTYLE:OFF
75          catch (Throwable anException)
76          // CHECKSTYLE:ON
77          {
78              // make sure that Exceptions are properly chained
79              throw new DaoException(anException);
80          }
81      }
82      
83      //------------------------------------------------------------------------
84      public void
85      update(Object anObject)
86      {
87          // approximation to update in JPA
88          getUnderlying().merge(anObject);
89      }
90      
91      //------------------------------------------------------------------------
92      public IQuery
93      createQuery(String aSqlString)
94      {
95          // adapts the Query
96          IQuery myQuery = new JpaQueryAdapter(getUnderlying().createQuery(
97              aSqlString)).getTarget();
98          
99          return myQuery;
100     }        
101     
102     //------------------------------------------------------------------------
103     public IQuery
104     createNativeQuery(String aSqlString, Class<?> aPersistentClass)
105     {
106         // adapts the Query
107         IQuery myQuery = new JpaQueryAdapter(getUnderlying().
108             createNativeQuery(aSqlString, aPersistentClass)).getTarget();
109         
110         return myQuery;
111     }        
112 
113     //------------------------------------------------------------------------
114     public ITransaction
115     getTransaction()
116     {
117         // adapts the EntityTransaction
118         EntityTransaction myAdaptee = getUnderlying().getTransaction();
119 
120         if (theTransactionAdapter == null || theTransactionAdapter.
121             getAdaptee() != myAdaptee)
122         {
123             theTransactionAdapter = new EntityTransactionAdapter(myAdaptee);
124         }
125         
126         assert theTransactionAdapter != null : 
127             "Transaction Adapter must not be null";
128         
129         return theTransactionAdapter.getTarget();
130     }    
131     
132     //------------------------------------------------------------------------
133     // members
134     //------------------------------------------------------------------------
135     private IAdapter<ITransaction, EntityTransaction> theTransactionAdapter;
136 }