KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > TypeCheckingEntityManager


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.jpa;
20
21 import javax.persistence.EntityTransaction;
22 import javax.persistence.FlushModeType;
23 import javax.persistence.LockModeType;
24 import javax.persistence.Query;
25
26 import org.apache.cayenne.DataChannel;
27 import org.apache.cayenne.Persistent;
28
29 /**
30  * An EntityManager decorator that checks that only properly enhanced entities are passwed
31  * to the underlying EntityManager.
32  *
33  * @author Andrus Adamchik
34  */

35 // TODO: andrus, 2/18/2007 - in the future this wrapper can also enhance entities on the
36
// fly, for now it simply does the type checks before passing entities to the underlying
37
// EM.
38
public class TypeCheckingEntityManager implements CayenneEntityManager {
39
40     protected CayenneEntityManager entityManager;
41
42     public TypeCheckingEntityManager(CayenneEntityManager entityManager) {
43         this.entityManager = entityManager;
44     }
45
46     protected void checkEntityType(Class JavaDoc entityClass) throws IllegalArgumentException JavaDoc {
47         if (entityClass == null) {
48             throw new IllegalArgumentException JavaDoc("Null entity class");
49         }
50
51         if (!Persistent.class.isAssignableFrom(entityClass)) {
52             throw new IllegalArgumentException JavaDoc("Entity class must be Persistent, got: "
53                     + entityClass.getName());
54         }
55     }
56
57     protected void checkEntityType(Object JavaDoc entity) throws IllegalArgumentException JavaDoc {
58         if (entity == null) {
59             throw new IllegalArgumentException JavaDoc("Null entity");
60         }
61
62         if (!(entity instanceof Persistent)) {
63             String JavaDoc className = (entity != null) ? entity.getClass().getName() : "<null>";
64             throw new IllegalArgumentException JavaDoc("entity must be Persistent: " + className);
65         }
66     }
67
68     protected void enhance(Class JavaDoc entityClass) {
69
70     }
71
72     public void clear() {
73         entityManager.clear();
74     }
75
76     public void close() {
77         entityManager.close();
78     }
79
80     public boolean contains(Object JavaDoc entity) {
81         checkEntityType(entity);
82         return entityManager.contains(entity);
83     }
84
85     public Query createNamedQuery(String JavaDoc name) {
86         return entityManager.createNamedQuery(name);
87     }
88
89     public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass) {
90         checkEntityType(resultClass);
91         return entityManager.createNativeQuery(sqlString, resultClass);
92     }
93
94     public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping) {
95         return entityManager.createNativeQuery(sqlString, resultSetMapping);
96     }
97
98     public Query createNativeQuery(String JavaDoc sqlString) {
99         return entityManager.createNativeQuery(sqlString);
100     }
101
102     public Query createQuery(String JavaDoc ejbqlString) {
103         return entityManager.createQuery(ejbqlString);
104     }
105
106     public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
107         checkEntityType(entityClass);
108         return entityManager.find(entityClass, primaryKey);
109     }
110
111     public void flush() {
112         entityManager.flush();
113     }
114
115     public DataChannel getChannel() {
116         return entityManager.getChannel();
117     }
118
119     public Object JavaDoc getDelegate() {
120         return entityManager.getDelegate();
121     }
122
123     public FlushModeType getFlushMode() {
124         return entityManager.getFlushMode();
125     }
126
127     public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
128         return entityManager.getReference(entityClass, primaryKey);
129     }
130
131     public EntityTransaction getTransaction() {
132         return entityManager.getTransaction();
133     }
134
135     public boolean isOpen() {
136         return entityManager.isOpen();
137     }
138
139     public void joinTransaction() {
140         entityManager.joinTransaction();
141     }
142
143     public void lock(Object JavaDoc entity, LockModeType lockMode) {
144         entityManager.lock(entity, lockMode);
145     }
146
147     public <T> T merge(T entity) {
148         checkEntityType(entity);
149         return entityManager.merge(entity);
150     }
151
152     public void persist(Object JavaDoc entity) {
153         checkEntityType(entity);
154         entityManager.persist(entity);
155     }
156
157     public void refresh(Object JavaDoc entity) {
158         entityManager.refresh(entity);
159     }
160
161     public void remove(Object JavaDoc entity) {
162         checkEntityType(entity);
163         entityManager.remove(entity);
164     }
165
166     public void setFlushMode(FlushModeType flushMode) {
167         entityManager.setFlushMode(flushMode);
168     }
169 }
170
Popular Tags