/** * 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); } }
/** * 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); } } }
@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; }