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;
 }
示例#2
0
  /**
   * Adds {@literal order by} clause to the JPQL query.
   *
   * @param query
   * @param sort
   * @param alias
   * @return
   */
  public static String applySorting(String query, Sort sort, String alias) {

    Assert.hasText(query);

    if (null == sort || !sort.iterator().hasNext()) {
      return query;
    }

    StringBuilder builder = new StringBuilder(query);

    if (!ORDER_BY.matcher(query).matches()) {
      builder.append(" order by ");
    } else {
      builder.append(", ");
    }

    Set<String> aliases = getOuterJoinAliases(query);

    for (Order order : sort) {
      builder.append(getOrderClause(aliases, alias, order)).append(", ");
    }

    builder.delete(builder.length() - 2, builder.length());

    return builder.toString();
  }
  @Override
  protected DerivedViewQuery complete(ViewQuery criteria, Sort sort) {
    boolean descending = false;

    if (sort != null) {
      int sortCount = 0;
      Iterator<Sort.Order> it = sort.iterator();
      while (it.hasNext()) {
        sortCount++;
        if (!it.next().isAscending()) {
          descending = true;
        }
      }
      if (sortCount > 1) {
        throw new IllegalArgumentException(
            "Detected " + sortCount + " sort instructions, maximum one supported");
      }
      query.descending(descending);
    }

    if (tree.isLimiting()) {
      query.limit(tree.getMaxResults());
    }

    boolean isCount = tree.isCountProjection() == Boolean.TRUE;
    boolean isExplicitReduce = viewAnnotation != null && viewAnnotation.reduce();
    if (isCount || isExplicitReduce) {
      query.reduce();
    }

    return new DerivedViewQuery(query, tree.isLimiting(), isCount || isExplicitReduce);
  }
  /**
   * Apply sorting for the given query.
   *
   * @param query the query
   * @param sort the sort
   * @return the string
   */
  public static String applySorting(String query, Sort sort) {
    Assert.hasText(query);

    if (null == sort || !sort.iterator().hasNext()) {
      return query;
    }

    throw new UnsupportedOperationException("Not implemented");
  }
 public Optional<Sort> build(List<RequestSort> requestSortList, FieldMapper fieldMapper) {
   Iterator<RequestSort> iterator = requestSortList.iterator();
   Sort sort = null;
   if (iterator.hasNext()) {
     RequestSort requestSort = iterator.next();
     sort =
         new Sort(
             SORT_MAPPING.get(requestSort.getType()), fieldMapper.get(requestSort.getFieldName()));
   }
   while (iterator.hasNext()) {
     RequestSort requestSort = iterator.next();
     sort =
         sort.and(
             new Sort(
                 SORT_MAPPING.get(requestSort.getType()),
                 fieldMapper.get(requestSort.getFieldName())));
   }
   return Optional.ofNullable(sort);
 }
  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();
  }
示例#7
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;
  }