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 | 94 | private Class<T> beanClass; |
| 95 | 95 | |
| 96 | 96 | protected Class<T> getBeanClass() { |
| 97 | - | |
| 98 | 97 | if (this.beanClass == null) { |
| 99 | 98 | this.beanClass = Reflections.getGenericTypeArgument(this.getClass(), 0); |
| 100 | 99 | } |
| ... | ... | @@ -115,6 +114,7 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 115 | 114 | PaginationContext context = paginationContext.get(); |
| 116 | 115 | pagination = context.getPagination(getBeanClass()); |
| 117 | 116 | } |
| 117 | + | |
| 118 | 118 | return pagination; |
| 119 | 119 | } |
| 120 | 120 | |
| ... | ... | @@ -130,6 +130,7 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 130 | 130 | if (cause instanceof TransactionRequiredException) { |
| 131 | 131 | String message = bundle.get().getString("no-transaction-active", "frameworkdemoiselle.transaction.class", |
| 132 | 132 | Configuration.DEFAULT_RESOURCE); |
| 133 | + | |
| 133 | 134 | throw new DemoiselleException(message, cause); |
| 134 | 135 | |
| 135 | 136 | } else { |
| ... | ... | @@ -168,75 +169,74 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 168 | 169 | |
| 169 | 170 | /** |
| 170 | 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 | 175 | * @return a list of entities |
| 173 | 176 | */ |
| 174 | 177 | protected List<T> findByJPQL(String jpql) { |
| 175 | - pagination = getPagination(); | |
| 176 | 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 | 189 | return listQuery.getResultList(); |
| 186 | 190 | } |
| 187 | 191 | |
| 188 | 192 | /** |
| 189 | 193 | * Search CriteriaQuery integrated into the context of paging |
| 190 | - * @param criteriaQuery - structure CriteriaQuery | |
| 194 | + * | |
| 195 | + * @param criteriaQuery | |
| 196 | + * - structure CriteriaQuery | |
| 191 | 197 | * @return a list of entities |
| 192 | 198 | */ |
| 193 | 199 | public List<T> findByCriteriaQuery(CriteriaQuery<T> criteriaQuery) { |
| 194 | - pagination = getPagination(); | |
| 195 | 200 | TypedQuery<T> listQuery = getEntityManager().createQuery(criteriaQuery); |
| 196 | - if (pagination != null) { | |
| 201 | + | |
| 202 | + if (getPagination() != null) { | |
| 197 | 203 | CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); |
| 198 | 204 | CriteriaQuery<Long> countQuery = builder.createQuery(Long.class); |
| 199 | 205 | countQuery.select(builder.count(countQuery.from(getBeanClass()))); |
| 200 | 206 | countQuery.where(criteriaQuery.getRestriction()); |
| 201 | 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 | 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 | 219 | * @param query |
| 212 | 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 | 267 | * @return an instance of {@code CriteriaQuery} |
| 268 | 268 | */ |
| 269 | 269 | private CriteriaQuery<T> createCriteriaByExample(final T example) { |
| 270 | - | |
| 271 | 270 | final CriteriaBuilder builder = getCriteriaBuilder(); |
| 272 | 271 | final CriteriaQuery<T> query = builder.createQuery(getBeanClass()); |
| 273 | 272 | final Root<T> entity = query.from(getBeanClass()); |
| ... | ... | @@ -276,7 +275,6 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 276 | 275 | final Field[] fields = example.getClass().getDeclaredFields(); |
| 277 | 276 | |
| 278 | 277 | for (Field field : fields) { |
| 279 | - | |
| 280 | 278 | if (!field.isAnnotationPresent(Column.class) && !field.isAnnotationPresent(Basic.class) |
| 281 | 279 | && !field.isAnnotationPresent(Enumerated.class)) { |
| 282 | 280 | continue; |
| ... | ... | @@ -287,10 +285,13 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 287 | 285 | try { |
| 288 | 286 | field.setAccessible(true); |
| 289 | 287 | value = field.get(example); |
| 288 | + | |
| 290 | 289 | } catch (IllegalArgumentException e) { |
| 291 | 290 | continue; |
| 291 | + | |
| 292 | 292 | } catch (IllegalAccessException e) { |
| 293 | 293 | continue; |
| 294 | + | |
| 294 | 295 | } |
| 295 | 296 | |
| 296 | 297 | if (value == null) { |
| ... | ... | @@ -300,6 +301,7 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 300 | 301 | final Predicate pred = builder.equal(entity.get(field.getName()), value); |
| 301 | 302 | predicates.add(pred); |
| 302 | 303 | } |
| 304 | + | |
| 303 | 305 | return query.where(predicates.toArray(new Predicate[0])).select(entity); |
| 304 | 306 | } |
| 305 | 307 | ... | ... |