Commit 9d773bb1cacbb9dab46e591de2b8960f8f42b0a9
1 parent
1279fa34
Exists in
master
Tratamento do pull-request que resolveu problemas na paginação.
Showing
1 changed file
with
47 additions
and
45 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
@@ -94,7 +94,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -94,7 +94,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
94 | private Class<T> beanClass; | 94 | private Class<T> beanClass; |
95 | 95 | ||
96 | protected Class<T> getBeanClass() { | 96 | protected Class<T> getBeanClass() { |
97 | - | ||
98 | if (this.beanClass == null) { | 97 | if (this.beanClass == null) { |
99 | this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0); | 98 | this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0); |
100 | } | 99 | } |
@@ -115,6 +114,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -115,6 +114,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
115 | PaginationContext context = paginationContext.get(); | 114 | PaginationContext context = paginationContext.get(); |
116 | pagination = context.getPagination(getBeanClass()); | 115 | pagination = context.getPagination(getBeanClass()); |
117 | } | 116 | } |
117 | + | ||
118 | return pagination; | 118 | return pagination; |
119 | } | 119 | } |
120 | 120 | ||
@@ -130,6 +130,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -130,6 +130,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
130 | if (cause instanceof TransactionRequiredException) { | 130 | if (cause instanceof TransactionRequiredException) { |
131 | String message = bundle.get().getString("no-transaction-active", "frameworkdemoiselle.transaction.class", | 131 | String message = bundle.get().getString("no-transaction-active", "frameworkdemoiselle.transaction.class", |
132 | Configuration.DEFAULT_RESOURCE); | 132 | Configuration.DEFAULT_RESOURCE); |
133 | + | ||
133 | throw new DemoiselleException(message, cause); | 134 | throw new DemoiselleException(message, cause); |
134 | 135 | ||
135 | } else { | 136 | } else { |
@@ -168,75 +169,74 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -168,75 +169,74 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
168 | 169 | ||
169 | /** | 170 | /** |
170 | * Search JPQL integrated into the context of paging | 171 | * Search JPQL integrated into the context of paging |
171 | - * @param jpql - query in syntax JPQL | 172 | + * |
173 | + * @param jpql | ||
174 | + * - query in syntax JPQL | ||
172 | * @return a list of entities | 175 | * @return a list of entities |
173 | */ | 176 | */ |
174 | protected List<T> findByJPQL(String jpql) { | 177 | protected List<T> findByJPQL(String jpql) { |
175 | - pagination = getPagination(); | ||
176 | TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); | 178 | TypedQuery<T> listQuery = getEntityManager().createQuery(jpql, getBeanClass()); |
177 | - if (pagination != null) { | ||
178 | - String countQueryString = createCountQueryString(jpql); | ||
179 | - Query query = getEntityManager().createQuery(countQueryString); | ||
180 | - Number cResults = (Number) query.getSingleResult(); | ||
181 | - pagination.setTotalResults(cResults.intValue()); | ||
182 | - listQuery.setFirstResult(pagination.getFirstResult()); | ||
183 | - listQuery.setMaxResults(pagination.getPageSize()); | 179 | + |
180 | + if (getPagination() != null) { | ||
181 | + String countQuery = createCountQuery(jpql); | ||
182 | + Query query = getEntityManager().createQuery(countQuery); | ||
183 | + Number cResults = (Number) query.getSingleResult(); | ||
184 | + getPagination().setTotalResults(cResults.intValue()); | ||
185 | + listQuery.setFirstResult(getPagination().getFirstResult()); | ||
186 | + listQuery.setMaxResults(getPagination().getPageSize()); | ||
184 | } | 187 | } |
188 | + | ||
185 | return listQuery.getResultList(); | 189 | return listQuery.getResultList(); |
186 | } | 190 | } |
187 | 191 | ||
188 | /** | 192 | /** |
189 | * Search CriteriaQuery integrated into the context of paging | 193 | * Search CriteriaQuery integrated into the context of paging |
190 | - * @param criteriaQuery - structure CriteriaQuery | 194 | + * |
195 | + * @param criteriaQuery | ||
196 | + * - structure CriteriaQuery | ||
191 | * @return a list of entities | 197 | * @return a list of entities |
192 | */ | 198 | */ |
193 | public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery) { | 199 | public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery) { |
194 | - pagination = getPagination(); | ||
195 | TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); | 200 | TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); |
196 | - if (pagination != null) { | 201 | + |
202 | + if (getPagination() != null) { | ||
197 | CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); | 203 | CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); |
198 | CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); | 204 | CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); |
199 | countQuery.select(builder.count(countQuery.from(getBeanClass()))); | 205 | countQuery.select(builder.count(countQuery.from(getBeanClass()))); |
200 | countQuery.where(criteriaQuery.getRestriction()); | 206 | countQuery.where(criteriaQuery.getRestriction()); |
201 | getEntityManager().createQuery(countQuery); | 207 | getEntityManager().createQuery(countQuery); |
202 | - pagination.setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); | ||
203 | - listQuery.setFirstResult(pagination.getFirstResult()); | ||
204 | - listQuery.setMaxResults(pagination.getPageSize()); | 208 | + getPagination().setTotalResults((int) (getEntityManager().createQuery(countQuery).getSingleResult() + 0)); |
209 | + listQuery.setFirstResult(getPagination().getFirstResult()); | ||
210 | + listQuery.setMaxResults(getPagination().getPageSize()); | ||
205 | } | 211 | } |
212 | + | ||
206 | return listQuery.getResultList(); | 213 | return listQuery.getResultList(); |
207 | } | 214 | } |
208 | - | 215 | + |
209 | /** | 216 | /** |
210 | - * Converts JPA query to JPA count query | 217 | + * Converts a query into a count query |
218 | + * | ||
211 | * @param query | 219 | * @param query |
212 | * @return | 220 | * @return |
213 | */ | 221 | */ |
214 | - private String createCountQueryString(String query) { | ||
215 | - query = query.toUpperCase(); | ||
216 | - Matcher matcher = Pattern.compile("SELECT(.+)FROM").matcher(query); | ||
217 | - if (matcher.find()){ | ||
218 | - String group = matcher.group(1).trim(); | ||
219 | - query = query.replaceFirst(group, "COUNT(" + group + ")"); | ||
220 | - matcher = Pattern.compile("ORDER(.+)").matcher(query); | ||
221 | - if (matcher.find()){ | ||
222 | - group = matcher.group(1); | ||
223 | - query = query.replaceFirst("ORDER"+group, ""); | ||
224 | - } | ||
225 | - return query; | ||
226 | - }else{ | ||
227 | - throw new DemoiselleException(bundle.get().getString("malformed-jpql")); | ||
228 | - } | ||
229 | - } | 222 | + private String createCountQuery(String query) { |
223 | + Matcher matcher = Pattern.compile("[Ss][Ee][Ll][Ee][Cc][Tt](.+)[Ff][Rr][Oo][Mm]").matcher(query); | ||
230 | 224 | ||
231 | - /** | ||
232 | - * Retrieves the number of persisted objects for the current class type. | ||
233 | - * | ||
234 | - * @return the row count | ||
235 | - */ | ||
236 | - private Long countAll() { | ||
237 | - final Query query = getEntityManager().createQuery( | ||
238 | - "select count(this) from " + beanClass.getSimpleName() + " this"); | ||
239 | - return (Long) query.getSingleResult(); | 225 | + if (matcher.find()) { |
226 | + String group = matcher.group(1).trim(); | ||
227 | + query = query.replaceFirst(group, "COUNT(" + group + ")"); | ||
228 | + matcher = Pattern.compile("[Oo][Rr][Dd][Ee][Rr](.+)").matcher(query); | ||
229 | + | ||
230 | + if (matcher.find()) { | ||
231 | + group = matcher.group(0); | ||
232 | + query = query.replaceFirst(group, ""); | ||
233 | + } | ||
234 | + | ||
235 | + return query; | ||
236 | + | ||
237 | + } else { | ||
238 | + throw new DemoiselleException(bundle.get().getString("malformed-jpql")); | ||
239 | + } | ||
240 | } | 240 | } |
241 | 241 | ||
242 | /** | 242 | /** |
@@ -267,7 +267,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -267,7 +267,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
267 | * @return an instance of {@code CriteriaQuery} | 267 | * @return an instance of {@code CriteriaQuery} |
268 | */ | 268 | */ |
269 | private CriteriaQuery<T> createCriteriaByExample(final T example) { | 269 | private CriteriaQuery<T> createCriteriaByExample(final T example) { |
270 | - | ||
271 | final CriteriaBuilder builder = getCriteriaBuilder(); | 270 | final CriteriaBuilder builder = getCriteriaBuilder(); |
272 | final CriteriaQuery<T> query = builder.createQuery(getBeanClass()); | 271 | final CriteriaQuery<T> query = builder.createQuery(getBeanClass()); |
273 | final Root<T> entity = query.from(getBeanClass()); | 272 | final Root<T> entity = query.from(getBeanClass()); |
@@ -276,7 +275,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -276,7 +275,6 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
276 | final Field[] fields = example.getClass().getDeclaredFields(); | 275 | final Field[] fields = example.getClass().getDeclaredFields(); |
277 | 276 | ||
278 | for (Field field : fields) { | 277 | for (Field field : fields) { |
279 | - | ||
280 | if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Basic.class) | 278 | if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Basic.class) |
281 | && !field.isAnnotationPresent(Enumerated.class)) { | 279 | && !field.isAnnotationPresent(Enumerated.class)) { |
282 | continue; | 280 | continue; |
@@ -287,10 +285,13 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -287,10 +285,13 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
287 | try { | 285 | try { |
288 | field.setAccessible(true); | 286 | field.setAccessible(true); |
289 | value = field.get(example); | 287 | value = field.get(example); |
288 | + | ||
290 | } catch (IllegalArgumentException e) { | 289 | } catch (IllegalArgumentException e) { |
291 | continue; | 290 | continue; |
291 | + | ||
292 | } catch (IllegalAccessException e) { | 292 | } catch (IllegalAccessException e) { |
293 | continue; | 293 | continue; |
294 | + | ||
294 | } | 295 | } |
295 | 296 | ||
296 | if (value == null) { | 297 | if (value == null) { |
@@ -300,6 +301,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | @@ -300,6 +301,7 @@ public class JPACrud<T, I> implements Crud<T, I> { | ||
300 | final Predicate pred = builder.equal(entity.get(field.getName()), value); | 301 | final Predicate pred = builder.equal(entity.get(field.getName()), value); |
301 | predicates.add(pred); | 302 | predicates.add(pred); |
302 | } | 303 | } |
304 | + | ||
303 | return query.where(predicates.toArray(new Predicate[0])).select(entity); | 305 | return query.where(predicates.toArray(new Predicate[0])).select(entity); |
304 | } | 306 | } |
305 | 307 |