View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.persistence.jpa.helper;
18  
19  import javax.persistence.EntityManager;
20  import javax.persistence.EntityNotFoundException;
21  import javax.persistence.PersistenceContext;
22  
23  import org.slf4j.LoggerFactory;
24  import org.springframework.transaction.annotation.Transactional;
25  
26  import ch.elca.el4j.services.persistence.generic.dto.AbstractIntOptimisticLockingDto;
27  import ch.elca.el4j.services.persistence.generic.dto.PrimaryKeyObject;
28  import ch.elca.el4j.services.persistence.jpa.util.JpaQuery;
29  import ch.elca.el4j.services.persistence.jpa.util.QueryException;
30  
31  /**
32   * Implementation of DataService.
33   *
34   * @svnLink $Revision: 4112 $;$Date: 2010-08-05 11:00:36 +0200 (Do, 05. Aug 2010) $;$Author: swrelca $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/jpa/helper/JpaHelperImpl.java $
35   *
36   * @author David Bernhard (dab)
37   */
38  public class JpaHelperImpl implements JpaHelper {
39  
40  	/**
41  	 * The entity manager.
42  	 */
43  	@PersistenceContext
44  	private EntityManager em;
45  	
46  	@Override 
47  	public <T> JpaQuery<T> selectFrom(Class<T> cls) {
48  		return new JpaQuery<T>(cls, this);
49  	}
50  
51  	/**
52  	 * Check if an instance is new. 
53  	 * @param instance The instance.
54  	 * @return true if it is new.
55  	 * @throws IllegalArgumentException If the instance is not an entity.
56  	 */
57  	private boolean isNew(Object instance) throws IllegalArgumentException {
58  		// TODO We won't need this once we've changed to a clean JPA
59  		// programming model.
60  		if (instance instanceof PrimaryKeyObject) {
61  			PrimaryKeyObject dto
62  				= (PrimaryKeyObject) instance;
63  			return dto.isKeyNew();
64  		} else {
65  			throw new IllegalArgumentException("Cannot persist " + instance
66  				+ " : Not an entity, class is "
67  				+ instance.getClass().getName());
68  		}
69  	}
70  
71  	@Override 
72  	@Transactional 
73  	public <T> Boolean contains(T instance) {
74  		T t = instance;
75  		return em.contains(t);
76  	}
77  	
78  	@Override 
79  	@Transactional 
80  	public <T> T merge(T instance) {
81  		T t = instance;
82  		if (em.contains(t)) {
83  			LoggerFactory.getLogger(this.getClass())
84  				.warn("merge is called on a MANAGED entity instead on a DETACHED one.");
85  		}
86  		t = em.merge(t);
87  		return t;
88  	}
89  	
90  	@Override 
91  	@Transactional 
92  	public <T> void detach(T instance) {
93  		T t = instance;
94  		if (!em.contains(t)) {
95  			LoggerFactory.getLogger(this.getClass())
96  				.warn("merge is not called on a MANAGED entity.");
97  		}
98  		em.detach(t);
99  	}
100 	
101 	@Override 
102 	@Transactional 
103 	public <T> void persist(T instance) {
104 		T t = instance;
105 		if (em.contains(t)) {
106 			LoggerFactory.getLogger(this.getClass())
107 				.warn("persist is called on a MANAGED entity instead on a NEW one.");
108 		}
109 		em.persist(t);
110 	}
111 
112 	@Override 
113 	@Transactional 
114 	public <T> void remove(T instance) {
115 		T t = instance;
116 		if (!em.contains(t)) {
117 			LoggerFactory.getLogger(this.getClass()).warn("remove is not called on a MANAGED entity.");
118 		}
119 		em.remove(t);
120 	}
121 
122 	@Override 
123 	@Transactional 
124 	public <T> void refresh(T instance) {
125 		T t = instance;
126 		if (!em.contains(t)) {
127 			LoggerFactory.getLogger(this.getClass()).warn("refresh is not called on a MANAGED entity.");
128 		}
129 		em.refresh(t);
130 	}
131 
132 	@Override
133 	@Transactional
134 	public <T> T findByKey(Class<T> clazz, Integer key) {
135 		T t = em.find(clazz, key);
136 		return t;
137 	}
138 	
139 	
140 	@Override 
141 	public void clear() {
142 		em.clear();
143 	}
144 
145 	@Override 
146 	@Transactional 
147 	public void flush() {
148 		em.flush();
149 	}
150 	
151 	/**
152 	 * Getter for entity manager.
153 	 */
154 	public EntityManager getEntityManager() {
155 		return em;
156 	}
157 
158 	/**
159 	 * Set the entity manager.
160 	 * @param em The entity manager.
161 	 */
162 	public void setEntityManager(EntityManager em) {
163 		this.em = em;
164 	}
165 	
166 	@Override 
167 	@Transactional 
168 	public void doInTransaction(Runnable r) {
169 		r.run();
170 	}
171 }