Example #1
0
  /**
   * Applies the criteria found in the {@link SearchCriteriaBean} to the JPA query.
   *
   * @param criteria
   * @param builder
   * @param query
   * @param from
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected <T> void applySearchCriteriaToQuery(
      SearchCriteriaBean criteria,
      CriteriaBuilder builder,
      CriteriaQuery<?> query,
      Root<T> from,
      boolean countOnly) {

    List<SearchCriteriaFilterBean> filters = criteria.getFilters();
    if (filters != null && !filters.isEmpty()) {
      List<Predicate> predicates = new ArrayList<>();
      for (SearchCriteriaFilterBean filter : filters) {
        if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
          Path<Object> path = from.get(filter.getName());
          Class<?> pathc = path.getJavaType();
          if (pathc.isAssignableFrom(String.class)) {
            predicates.add(builder.equal(path, filter.getValue()));
          } else if (pathc.isEnum()) {
            predicates.add(builder.equal(path, Enum.valueOf((Class) pathc, filter.getValue())));
          }
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
          predicates.add(
              builder.equal(
                  from.<Boolean>get(filter.getName()), Boolean.valueOf(filter.getValue())));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.gt) {
          predicates.add(
              builder.greaterThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.gte) {
          predicates.add(
              builder.greaterThanOrEqualTo(
                  from.<Long>get(filter.getName()), new Long(filter.getValue())));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.lt) {
          predicates.add(
              builder.lessThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.lte) {
          predicates.add(
              builder.lessThanOrEqualTo(
                  from.<Long>get(filter.getName()), new Long(filter.getValue())));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.neq) {
          predicates.add(builder.notEqual(from.get(filter.getName()), filter.getValue()));
        } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
          predicates.add(
              builder.like(
                  builder.upper(from.<String>get(filter.getName())),
                  filter.getValue().toUpperCase().replace('*', '%')));
        }
      }
      query.where(predicates.toArray(new Predicate[predicates.size()]));
    }
    OrderByBean orderBy = criteria.getOrderBy();
    if (orderBy != null && !countOnly) {
      if (orderBy.isAscending()) {
        query.orderBy(builder.asc(from.get(orderBy.getName())));
      } else {
        query.orderBy(builder.desc(from.get(orderBy.getName())));
      }
    }
  }
Example #2
0
  /**
   * Get a list of entities based on the provided criteria and entity type.
   *
   * @param criteria
   * @param type
   * @throws StorageException if a storage problem occurs while storing a bean
   */
  protected <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, Class<T> type)
      throws StorageException {
    SearchResultsBean<T> results = new SearchResultsBean<>();
    EntityManager entityManager = getActiveEntityManager();
    try {
      // Set some default in the case that paging information was not included in the request.
      PagingBean paging = criteria.getPaging();
      if (paging == null) {
        paging = new PagingBean();
        paging.setPage(1);
        paging.setPageSize(20);
      }
      int page = paging.getPage();
      int pageSize = paging.getPageSize();
      int start = (page - 1) * pageSize;

      CriteriaBuilder builder = entityManager.getCriteriaBuilder();
      CriteriaQuery<T> criteriaQuery = builder.createQuery(type);
      Root<T> from = criteriaQuery.from(type);
      applySearchCriteriaToQuery(criteria, builder, criteriaQuery, from, false);
      TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);
      typedQuery.setFirstResult(start);
      typedQuery.setMaxResults(pageSize + 1);
      boolean hasMore = false;

      // Now query for the actual results
      List<T> resultList = typedQuery.getResultList();

      // Check if we got back more than we actually needed.
      if (resultList.size() > pageSize) {
        resultList.remove(resultList.size() - 1);
        hasMore = true;
      }

      // If there are more results than we needed, then we will need to do another
      // query to determine how many rows there are in total
      int totalSize = start + resultList.size();
      if (hasMore) {
        totalSize = executeCountQuery(criteria, entityManager, type);
      }
      results.setTotalSize(totalSize);
      results.setBeans(resultList);
      return results;
    } catch (Throwable t) {
      logger.error(t.getMessage(), t);
      throw new StorageException(t);
    }
  }