Commit 054ab3700ccd0d38bdaf1067ce4230fa4c79b00c
Exists in
master
Merge branch '2.4.0' of git@github.com:demoiselle/framework.git into 2.4.0
Showing
3 changed files
with
95 additions
and
21 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/template/Crud.java
... | ... | @@ -39,16 +39,53 @@ package br.gov.frameworkdemoiselle.template; |
39 | 39 | import java.io.Serializable; |
40 | 40 | import java.util.List; |
41 | 41 | |
42 | +/** | |
43 | + * Interface containing basic methods for creating, updating and deleting entities (the CRUD design pattern). | |
44 | + * | |
45 | + * @author serpro | |
46 | + * | |
47 | + * @param <T> Type of the entity | |
48 | + * @param <I> Type of the identification attribute of the entity | |
49 | + */ | |
42 | 50 | public interface Crud<T, I> extends Serializable { |
43 | 51 | |
52 | + /** | |
53 | + * Find an entity by it's identification attribute and make it transient. | |
54 | + * | |
55 | + * @param id The unique identification to find the entity to be deleted. | |
56 | + */ | |
44 | 57 | void delete(I id); |
45 | 58 | |
59 | + /** | |
60 | + * List all instances of the given entity. | |
61 | + * | |
62 | + * @return The (possibly empty) list of all instances of the given entity. | |
63 | + */ | |
46 | 64 | List<T> findAll(); |
47 | 65 | |
48 | - void insert(T bean); | |
66 | + /** | |
67 | + * Insert an entity and return the inserted instance of the entity | |
68 | + * | |
69 | + * @param bean The bean to be inserted | |
70 | + * @return The inserted instance of the entity | |
71 | + */ | |
72 | + T insert(T bean); | |
49 | 73 | |
74 | + /** | |
75 | + * Find an entity's instance by it's identification attribute and return it | |
76 | + * @param id Value of the identification attribute of the desired entity's instance | |
77 | + * @return The entity's instance whose identification attribute's value matches | |
78 | + * the one passed as argument. | |
79 | + */ | |
50 | 80 | T load(I id); |
51 | 81 | |
52 | - void update(T bean); | |
82 | + /** | |
83 | + * Update an instance of the entity with the bean's attributes as new values. | |
84 | + * | |
85 | + * @param bean The bean instance containing both the identification value to find the old | |
86 | + * instance and the new values for the instance's attributes. | |
87 | + * @return The updated entity's instance | |
88 | + */ | |
89 | + T update(T bean); | |
53 | 90 | |
54 | 91 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/template/DelegateCrud.java
... | ... | @@ -46,6 +46,19 @@ import br.gov.frameworkdemoiselle.transaction.Transactional; |
46 | 46 | import br.gov.frameworkdemoiselle.util.Beans; |
47 | 47 | import br.gov.frameworkdemoiselle.util.Reflections; |
48 | 48 | |
49 | +/** | |
50 | + * An implementation of the {@link Crud} interface that delegates it's operations | |
51 | + * to another {@link Crud} implementation. | |
52 | + * | |
53 | + * @author serpro | |
54 | + * | |
55 | + * @param <T> | |
56 | + * bean object type | |
57 | + * @param <I> | |
58 | + * bean id type | |
59 | + * @param <C> | |
60 | + * type of {@link Crud} implementation this class will delegate to | |
61 | + */ | |
49 | 62 | public class DelegateCrud<T, I, C extends Crud<T, I>> implements Crud<T, I> { |
50 | 63 | |
51 | 64 | private static final long serialVersionUID = 1L; |
... | ... | @@ -137,21 +150,21 @@ public class DelegateCrud<T, I, C extends Crud<T, I>> implements Crud<T, I> { |
137 | 150 | * A entity to be inserted by the delegate |
138 | 151 | */ |
139 | 152 | @Override |
140 | - public void insert(final T bean) { | |
153 | + public T insert(final T bean) { | |
141 | 154 | if (isRunningTransactionalOperations()) { |
142 | - transactionalInsert(bean); | |
155 | + return transactionalInsert(bean); | |
143 | 156 | } else { |
144 | - nonTransactionalInsert(bean); | |
157 | + return nonTransactionalInsert(bean); | |
145 | 158 | } |
146 | 159 | } |
147 | 160 | |
148 | 161 | @Transactional |
149 | - private void transactionalInsert(final T bean) { | |
150 | - nonTransactionalInsert(bean); | |
162 | + private T transactionalInsert(final T bean) { | |
163 | + return nonTransactionalInsert(bean); | |
151 | 164 | } |
152 | 165 | |
153 | - private void nonTransactionalInsert(final T bean) { | |
154 | - getDelegate().insert(bean); | |
166 | + private T nonTransactionalInsert(final T bean) { | |
167 | + return getDelegate().insert(bean); | |
155 | 168 | } |
156 | 169 | |
157 | 170 | /** |
... | ... | @@ -171,21 +184,21 @@ public class DelegateCrud<T, I, C extends Crud<T, I>> implements Crud<T, I> { |
171 | 184 | * The instance containing the updated state. |
172 | 185 | */ |
173 | 186 | @Override |
174 | - public void update(final T bean) { | |
187 | + public T update(final T bean) { | |
175 | 188 | if (isRunningTransactionalOperations()) { |
176 | - transactionalUpdate(bean); | |
189 | + return transactionalUpdate(bean); | |
177 | 190 | } else { |
178 | - nonTransactionalUpdate(bean); | |
191 | + return nonTransactionalUpdate(bean); | |
179 | 192 | } |
180 | 193 | } |
181 | 194 | |
182 | 195 | @Transactional |
183 | - private void transactionalUpdate(final T bean) { | |
184 | - nonTransactionalUpdate(bean); | |
196 | + private T transactionalUpdate(final T bean) { | |
197 | + return nonTransactionalUpdate(bean); | |
185 | 198 | } |
186 | 199 | |
187 | - private void nonTransactionalUpdate(final T bean) { | |
188 | - getDelegate().update(bean); | |
200 | + private T nonTransactionalUpdate(final T bean) { | |
201 | + return getDelegate().update(bean); | |
189 | 202 | } |
190 | 203 | |
191 | 204 | private boolean isRunningTransactionalOperations() { | ... | ... |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
... | ... | @@ -47,6 +47,7 @@ import javax.enterprise.inject.Instance; |
47 | 47 | import javax.inject.Inject; |
48 | 48 | import javax.persistence.Basic; |
49 | 49 | import javax.persistence.Column; |
50 | +import javax.persistence.EntityExistsException; | |
50 | 51 | import javax.persistence.EntityManager; |
51 | 52 | import javax.persistence.Enumerated; |
52 | 53 | import javax.persistence.Query; |
... | ... | @@ -145,25 +146,48 @@ public class JPACrud<T, I> implements Crud<T, I> { |
145 | 146 | } |
146 | 147 | } |
147 | 148 | |
149 | + /** | |
150 | + * Insert a new instance of the entity in the database. After insertion the entity becomes "managed" | |
151 | + * as stated by the JPA specification. | |
152 | + * | |
153 | + * @param entity A non-detached instance of an entity. If this instance is not managed, it will be after this method returns. | |
154 | + * @return The same instance passed as argument. | |
155 | + * @throws EntityExistsException if <code>entity</code> is an unmanaged instance | |
156 | + * and the persistence provider already has a persisted instance that matches the <code>entity</code>'s primary key. | |
157 | + * @throws IllegalArgumentException if the instance is not an entity | |
158 | + * @see EntityManager#persist(Object entity) | |
159 | + */ | |
148 | 160 | @Override |
149 | 161 | @Transactional |
150 | - public void insert(final T entity) { | |
162 | + public T insert(final T entity) { | |
151 | 163 | getEntityManager().persist(entity); |
164 | + return entity; | |
152 | 165 | } |
153 | 166 | |
167 | + /** | |
168 | + * Finds an instance of this entity by it's primary ID and asks to the | |
169 | + * persistence provider to remove this entity instance from the persistence | |
170 | + * context. | |
171 | + * | |
172 | + * @see EntityManager#remove(Object entity) | |
173 | + */ | |
154 | 174 | @Override |
155 | 175 | @Transactional |
156 | 176 | public void delete(final I id) { |
157 | 177 | T entity = getEntityManager().getReference(getBeanClass(), id); |
158 | 178 | getEntityManager().remove(entity); |
159 | 179 | } |
160 | - | |
180 | + | |
181 | + /** | |
182 | + * Merge all changes made to the passed entity to a managed entity. The passed instance is not | |
183 | + * modified nor becomes managed, instead the managed entity is returned by this method. | |
184 | + */ | |
161 | 185 | @Override |
162 | 186 | @Transactional |
163 | - public void update(final T entity) { | |
164 | - getEntityManager().merge(entity); | |
187 | + public T update(T entity) { | |
188 | + return getEntityManager().merge(entity); | |
165 | 189 | } |
166 | - | |
190 | + | |
167 | 191 | @Override |
168 | 192 | public T load(final I id) { |
169 | 193 | return getEntityManager().find(getBeanClass(), id); | ... | ... |