@Override
  @Transactional(readOnly = true)
  public Page<UserGroupCheck> findAllByPage(Pageable pageable, Long groupId, long total) {
    // TODO use setParameter
    String sql = USER_GROUP_CHECK_QUERY;
    String orderBy = "";
    String sort = "";
    Iterator<Order> i = pageable.getSort().iterator();
    while (i.hasNext()) {
      Order order = i.next();
      orderBy = order.getProperty();
      sort = order.getDirection().name();
    }
    sql = sql.replace(ORDER_BY, orderBy);
    sql = sql.replace(SORT, sort);
    Query query = entityManager.createNativeQuery(sql);
    query.setParameter(GROUP_ID, groupId);
    query.setParameter(LIMIT, pageable.getPageSize());
    query.setParameter(OFFSET, pageable.getOffset());

    List<UserGroupCheck> result = JpaUtil.getResultList(query, UserGroupCheck.class);
    Page<UserGroupCheck> page = new PageImpl<>(result, pageable, total);

    return page;
  }
  protected String orderByExpression(Sort sort) {
    StringBuilder sb = new StringBuilder();

    for (Iterator<Order> it = sort.iterator(); it.hasNext(); ) {
      Order order = it.next();
      sb.append(order.getProperty()).append(' ').append(order.getDirection());

      if (it.hasNext()) sb.append(COMMA);
    }
    return sb.toString();
  }
Esempio n. 3
0
  protected Map<String, Object> applySorting(Map<String, Object> parameter, Sort sort) {

    if (sort == null) return parameter;

    StringBuilder sb = new StringBuilder();
    for (Order order : sort) {
      sb.append("," + order.getProperty() + " " + order.getDirection());
    }
    parameter.put(ORDER_STRING, sb.substring(1));

    return parameter;
  }
 private String getOrderDesc(final Pageable pageable) {
   String result = null;
   Sort sort = pageable.getSort();
   if (sort == null) {
     return result;
   }
   final Iterator<Order> iterator = sort.iterator();
   while (iterator.hasNext()) {
     final Order order = iterator.next();
     order.getProperty();
   }
   return result;
 }
Esempio n. 5
0
  /**
   * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}.
   *
   * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order}
   * @param root the {@link Root} the {@link Order} expression is based on
   * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order}
   *     with
   * @return
   */
  @SuppressWarnings("unchecked")
  private static javax.persistence.criteria.Order toJpaOrder(
      Order order, Root<?> root, CriteriaBuilder cb) {

    PropertyPath property = PropertyPath.from(order.getProperty(), root.getJavaType());
    Expression<?> expression = toExpressionRecursively(root, property);

    if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) {
      Expression<String> lower = cb.lower((Expression<String>) expression);
      return order.isAscending() ? cb.asc(lower) : cb.desc(lower);
    } else {
      return order.isAscending() ? cb.asc(expression) : cb.desc(expression);
    }
  }
  /**
   * Converts the given {@link org.springframework.data.domain.Sort} into {@link SortField}s.
   *
   * @param sort the {@link org.springframework.data.domain.Sort} instance to be transformed into
   *     JOOQ {@link SortField}s.
   * @return the list of {@link SortField}s.
   */
  public static List<SortField<?>> toOrders(Sort sort) {
    if (sort == null) {
      return Collections.emptyList();
    }

    List<SortField<?>> orders = new ArrayList<SortField<?>>();

    for (Order order : sort) {
      orders.add(
          field(order.getProperty())
              .sort(order.getDirection() == Direction.ASC ? SortOrder.ASC : SortOrder.DESC));
    }

    return orders;
  }
  /**
   * Append sorting parameters to {@link SolrQuery}
   *
   * @param solrQuery
   * @param sort
   */
  @SuppressWarnings("deprecation")
  protected void appendSort(SolrQuery solrQuery, Sort sort) {
    if (sort == null) {
      return;
    }

    for (Order order : sort) {
      // addSort which is to be used instead of addSortField is not available in versions below
      // 4.2.0
      if (VersionUtil.isSolr420Available()) {
        solrQuery.addSort(order.getProperty(), order.isAscending() ? ORDER.asc : ORDER.desc);
      } else {
        solrQuery.addSortField(order.getProperty(), order.isAscending() ? ORDER.asc : ORDER.desc);
      }
    }
  }
Esempio n. 8
0
  /**
   * Returns the order clause for the given {@link Order}. Will prefix the clause with the given
   * alias if the referenced property refers to a join alias.
   *
   * @param joinAliases the join aliases of the original query.
   * @param alias the alias for the root entity.
   * @param order the order object to build the clause for.
   * @return
   */
  private static String getOrderClause(Set<String> joinAliases, String alias, Order order) {

    String property = order.getProperty();
    boolean qualifyReference = !property.contains("("); // ( indicates a function

    for (String joinAlias : joinAliases) {
      if (property.startsWith(joinAlias)) {
        qualifyReference = false;
        break;
      }
    }

    String reference = qualifyReference ? String.format("%s.%s", alias, property) : property;
    String wrapped = order.isIgnoreCase() ? String.format("lower(%s)", reference) : reference;

    return String.format("%s %s", wrapped, toJpaDirection(order));
  }
Esempio n. 9
0
 private static String toJpaDirection(Order order) {
   return order.getDirection().name().toLowerCase(Locale.US);
 }
Esempio n. 10
0
  @Override
  public Page<DBObject> findPage(
      String collectionName,
      GroupPropertyFilter groupPropertyFilter,
      Pageable pageable,
      DBObject fields) {

    DB db = mongoClient.getDB(mongoDB);
    DBCollection dbColl = db.getCollection(collectionName);

    BasicDBObject query = new BasicDBObject();
    List<BasicDBObject> andQueries = Lists.newArrayList();
    List<PropertyFilter> filters = groupPropertyFilter.convertToPropertyFilters();
    if (CollectionUtils.isNotEmpty(filters)) {
      // Query and Projection Operators: https://docs.mongodb.org/manual/reference/operator/query/
      for (PropertyFilter filter : filters) {
        Object matchValue = filter.getMatchValue();
        if (matchValue == null) {
          continue;
        }

        String[] propertyNames = filter.getConvertedPropertyNames();
        List<BasicDBObject> orQueries = Lists.newArrayList();
        for (String propertyName : propertyNames) {
          BasicDBObject queryItem = new BasicDBObject();
          orQueries.add(queryItem);
          switch (filter.getMatchType()) {
            case EQ:
              queryItem.put(propertyName, matchValue);
              break;
            case NE:
              queryItem.put(propertyName, new BasicDBObject("$ne", matchValue));
              break;
            case BK:
              List<BasicDBObject> orList = Lists.newArrayList();
              orList.add(new BasicDBObject(propertyName, 0));
              orList.add(new BasicDBObject(propertyName, ""));
              queryItem.put("$or", orList);
              break;
            case NB:
              queryItem.put(propertyName, new BasicDBObject("$regex", ".{1,}"));
              break;
            case NU:
              queryItem.put(propertyName, 0);
              break;
            case NN:
              queryItem.put(propertyName, 1);
              break;
            case CN:
              queryItem.put(
                  propertyName,
                  new BasicDBObject("$regex", ".*" + matchValue + ".*").append("$options", "i"));
              break;
            case NC:
              queryItem.put(
                  propertyName,
                  new BasicDBObject(
                      "$not",
                      new BasicDBObject("$regex", ".*" + matchValue + ".*")
                          .append("$options", "i")));
              break;
            case BW:
              queryItem.put(
                  propertyName,
                  new BasicDBObject("$regex", "^" + matchValue).append("$options", "i"));
              break;
            case BN:
              queryItem.put(
                  propertyName,
                  new BasicDBObject(
                      "$not",
                      new BasicDBObject("$regex", "^" + matchValue).append("$options", "i")));
              break;
            case EW:
              queryItem.put(
                  propertyName,
                  new BasicDBObject("$regex", matchValue + "$").append("$options", "i"));
              break;
            case EN:
              queryItem.put(
                  propertyName,
                  new BasicDBObject(
                      "$not",
                      new BasicDBObject("$regex", matchValue + "$").append("$options", "i")));
              break;
            case BT:
              Assert.isTrue(matchValue.getClass().isArray(), "Match value must be array");
              Object[] matchValues = (Object[]) matchValue;
              Assert.isTrue(matchValues.length == 2, "Match value must have two value");
              List<BasicDBObject> andList = Lists.newArrayList();
              andList.add(new BasicDBObject(propertyName, new BasicDBObject("$gte", matchValue)));
              andList.add(new BasicDBObject(propertyName, new BasicDBObject("$gle", matchValue)));
              queryItem.put("$and", andList);
              break;
            case GT:
              queryItem.put(propertyName, new BasicDBObject("$gt", matchValue));
              break;
            case GE:
              queryItem.put(propertyName, new BasicDBObject("$ge", matchValue));
              break;
            case LT:
              queryItem.put(propertyName, new BasicDBObject("$lt", matchValue));
              break;
            case LE:
              queryItem.put(propertyName, new BasicDBObject("$le", matchValue));
              break;
            case IN:
              queryItem.put(propertyName, new BasicDBObject("$in", matchValue));
              break;
            default:
              throw new UnsupportedOperationException(
                  "Undefined PropertyFilter MatchType: " + filter.getMatchType());
          }
        }
        if (orQueries.size() > 1) {
          andQueries.add(new BasicDBObject("$or", orQueries));
        } else {
          andQueries.add(orQueries.get(0));
        }
      }
      query = new BasicDBObject("$and", andQueries);
    }

    BasicDBObject sort = new BasicDBObject();
    Sort pageSort = pageable.getSort();
    if (pageSort != null) {
      Iterator<Order> orders = pageSort.iterator();
      while (orders.hasNext()) {
        Order order = orders.next();
        String prop = order.getProperty();
        if (order.isAscending()) {
          sort.put(prop, 1);
        } else {
          sort.put(prop, -1);
        }
      }
    }

    DBCursor cur =
        dbColl
            .find(query, fields)
            .sort(sort)
            .skip(pageable.getOffset())
            .limit(pageable.getPageSize());
    List<DBObject> rows = Lists.newArrayList();
    while (cur.hasNext()) {
      DBObject obj = cur.next();
      rows.add(obj);
    }
    Page<DBObject> page = new PageImpl<DBObject>(rows, pageable, dbColl.count(query));
    return page;
  }