List of usage examples for javax.persistence.criteria CriteriaBuilder greaterThan
<Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Y y);
From source file:com.mxep.web.common.persistence.DynamicSpecifications.java
private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, // ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); }//from w ww . ja v a 2 s. c o m /*????*/ if (expression.getJavaType().equals(Integer.class) && !filter.operator.equals(SearchFilter.Operator.IN)) { filter.value = Integer.parseInt(filter.value.toString()); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case NEQ: predicates.add(builder.notEqual(expression, filter.value)); break; case IN: In in = builder.in(expression); String value = filter.value.toString(); String[] values = StringUtils.split(value, ","); for (int i = 0; i < values.length; i++) { /*????*/ if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class) || expression.getJavaType().equals(byte.class)) { in.value(Integer.valueOf(values[i])); } else { in.value(values[i]); } } predicates.add(in); break; case NULL: predicates.add(builder.isNull(expression)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; } } return predicates; }
From source file:cn.imethan.common.repository.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/* w w w. ja v a 2 s . c o m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (filters != null && !filters.isEmpty()) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); try { for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } } catch (Exception e) { e.printStackTrace(); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case IN: In in = builder.in(expression); String[] valueStrings = StringUtils.split(filter.value.toString(), ","); List<Long> list = new ArrayList<Long>(); for (String value : valueStrings) { list.add(Long.valueOf(value.trim())); } in.value(list); predicates.add(in); break; case NOTIN: In in1 = builder.in(expression); String[] valueStrings1 = StringUtils.split(filter.value.toString(), ","); List<Long> list1 = new ArrayList<Long>(); for (String value : valueStrings1) { list1.add(Long.valueOf(value.trim())); } in1.value(list1); predicates.add(builder.not(in1)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } // //hibernate // //org.hibernate.ejb.criteria.CriteriaQueryImpl // if(query instanceof org.hibernate.ejb.QueryImpl) { // @SuppressWarnings("rawtypes") // org.hibernate.ejb.QueryImpl hibernateQuery = (org.hibernate.ejb.QueryImpl)query; // org.hibernate.Query hQuery = hibernateQuery.getHibernateQuery(); // hQuery.setCacheable(true); // } return builder.conjunction(); } }; }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}// w w w.ja v a2 s . co m */ public List<Transaction> findTransactionsByStatementDateAndCreditCardId(Date startDate, Date endDate, long creditCardId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); Predicate date = builder.greaterThan(root.<Date>get(FIELD_CONSTANT_TRANSACTION_DATE), startDate); Predicate isCreditCardId = builder.equal(root.<String>get(FIELD_CONSTANT_CREDIT_CARD_ID), creditCardId); query.where(builder.and(date, isCreditCardId)); return super.find(query); }
From source file:com.shz.foundation.service.dynamic.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/*from ww w . j av a 2 s. c o m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // logic operator switch (filter.operator) { case EQ: //?? Object value = filter.value; if (isBoolean(value)) { value = Boolean.parseBoolean(value.toString()); } predicates.add(builder.equal(expression, value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } private boolean isBoolean(Object value) { String valueString = value.toString().toLowerCase(); if (valueString.equals("true") || valueString.equals("false")) return true; return false; } }; }
From source file:cc.sion.core.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/* w w w.j a v a2 s . c om*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // if(log.isDebugEnabled()){ // log.debug(" {}-{}-{}",names[0],filter.operator,filter.value); // } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case NE: predicates.add(builder.notEqual(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a predicate based on a "Compare" filter * /*from w ww.j a va2 s .co m*/ * @param builder * @param root * @param filter * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) private static Predicate createComparePredicate(CriteriaBuilder builder, Root<?> root, Filter filter) { Compare compare = (Compare) filter; Expression<Comparable> property = (Expression) getPropertyPath(root, compare.getPropertyId()); Object value = compare.getValue(); // number representation may contain locale specific separators. // Here, we remove // those and make sure a period is used in all cases if (value instanceof String) { // strip out any "%" sign from decimal fields value = ((String) value).replace('%', ' ').trim(); String str = (String) value; if (str != null && org.apache.commons.lang.StringUtils .isNumeric(str.replaceAll("\\.", "").replaceAll(",", ""))) { // first remove all periods (which may be used as // thousand // separators), then replace comma by period str = str.replaceAll("\\.", "").replace(',', '.'); value = str; } } switch (compare.getOperation()) { case EQUAL: return builder.equal(property, value); case GREATER: return builder.greaterThan(property, (Comparable) value); case GREATER_OR_EQUAL: return builder.greaterThanOrEqualTo(property, (Comparable) value); case LESS: return builder.lessThan(property, (Comparable) value); case LESS_OR_EQUAL: return builder.lessThanOrEqualTo(property, (Comparable) value); default: return null; } }
From source file:aode.lx.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//w w w . ja va 2 s.c om public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); List<Predicate> ORpredicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } Object value = filter.value; if (expression.getJavaType().isEnum()) { value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString()); predicates.add(builder.equal(expression, value)); } else { // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case NEQ: predicates.add(builder.notEqual(expression, filter.value)); break; case NOTNULL: predicates.add(builder.isNotNull(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case OREQ: ORpredicates.add(builder.equal(expression, filter.value)); break; case ORLIKE: ORpredicates.add(builder.like(expression, "%" + filter.value + "%")); break; case ORGT: ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case ORLT: ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case ORGTE: ORpredicates .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLTE: ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORNEQ: ORpredicates.add(builder.notEqual(expression, filter.value)); break; case ORNOTNULL: ORpredicates.add(builder.isNotNull(expression)); break; case ORISNULL: ORpredicates.add(builder.isNull(expression)); break; } } } // ? and ??? if (!predicates.isEmpty() && !ORpredicates.isEmpty()) { return builder.and(builder.and(predicates.toArray(new Predicate[predicates.size()])), builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()]))); } if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } if (!ORpredicates.isEmpty()) { return builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()])); } } return builder.conjunction(); } }; }
From source file:com.faceye.feature.repository.jpa.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override// ww w . j av a 2s .c o m public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (CollectionUtils.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // logic operator switch (filter.operator) { case EQ: if (filter.value instanceof Number) { predicates.add(builder.equal(expression, (Number) filter.value)); } else if (filter.value instanceof String) { if (StringUtils.isNotEmpty(filter.value.toString())) { predicates.add(builder.equal(expression, filter.value)); } } else { predicates.add(builder.equal(expression, filter.value)); } break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ISTRUE: predicates.add(builder.isTrue(expression)); break; case ISFALSE: predicates.add(builder.isFalse(expression)); break; case ISEMPTY: predicates.add(builder.isEmpty(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case NE: predicates.add(builder.notEqual(expression, filter.value)); } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }
From source file:net.groupbuy.dao.impl.PromotionDaoImpl.java
public List<Promotion> findList(Boolean hasBegun, Boolean hasEnded, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Promotion> criteriaQuery = criteriaBuilder.createQuery(Promotion.class); Root<Promotion> root = criteriaQuery.from(Promotion.class); criteriaQuery.select(root);/* ww w. j a v a 2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(root.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, root.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(root.<Date>get("beginDate"), new Date())); } } if (hasEnded != null) { if (hasEnded) { restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(), criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(), criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date()))); } } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:com.orange.clara.tool.controllers.AbstractDefaultController.java
protected List<WatchedResource> getFilteredWatchedResources(User user, boolean isAdmin, String isPublic, String tags, String types, Date afterDate) { Specification<WatchedResource> specification = new Specification<WatchedResource>() { @Override/*from w w w.j a v a 2 s.c o m*/ public Predicate toPredicate(Root<WatchedResource> root, CriteriaQuery<?> cq, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<>(); if (tags != null) { ListJoin<WatchedResource, Tag> tagJoin = root.joinList("tags"); predicates.add(cb.isTrue(tagJoin.get("name").in(Tag.namesFromStringList(tags)))); } if (types != null) { predicates.add(cb.isTrue(root.get("type").in(ResourceType.fromStringList(types)))); } if (isPublic != null) { predicates.add(cb.isTrue(root.get("isPublic"))); } if (afterDate != null) { predicates.add(cb.greaterThan(root.get("updatedResourceAt"), afterDate)); } if (!isAdmin) { ListJoin<WatchedResource, User> usersJoin = root.joinList("users"); predicates.add(cb.equal(usersJoin.get("uuid"), user.getUuid())); } Predicate finalPredicate = cb.and(predicates.toArray(new Predicate[] {})); if (isPublic != null) { finalPredicate = cb.or(finalPredicate, cb.isTrue(root.get("isPublic"))); } return finalPredicate; } }; return this.watchedResourceRepo.findAll(specification); }