List of usage examples for javax.persistence.criteria CriteriaBuilder or
Predicate or(Predicate... restrictions);
From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java
@Override public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng, EventType... eventTypes) {//from www.j a v a2s .c om CriteriaBuilder criteriaBuilder = getCriteriaBuilder(); CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class); Root<EventEntity> root = criteriaQuery.from(EventEntity.class); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat)); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat)); predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng)); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng)); if (ArrayUtils.isNotEmpty(eventTypes)) { List<Predicate> eventTypePredicates = new ArrayList<Predicate>(); for (EventType et : eventTypes) { eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et)); } predicates.add(criteriaBuilder.and( criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()])))); } criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])); return getAllByCriteria(criteriaQuery); }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
protected Predicate newFullTextPredicate(CriteriaBuilder cb, Root<T> p, String terms) { LinkedList<Predicate> predicatesOr = Lists.newLinkedList(); Iterable<Path<String>> fullTexts = settings.getFullTexts(p, persistentClass); if (fullTexts != null) { for (Path<String> x : fullTexts) { StringTokenizer tokenizer = new StringTokenizer(terms, " \t\n\r\f,.;:/"); LinkedList<Predicate> predicatesAnd = Lists.newLinkedList(); while (tokenizer.hasMoreTokens()) { String token = "%" + tokenizer.nextToken() + "%"; predicatesAnd.add(cb.like(cb.lower(x), StringUtils.lowerCase(token))); }/*from w w w. j a v a2 s. c o m*/ predicatesOr.add(cb.and(predicatesAnd.toArray(new Predicate[predicatesAnd.size()]))); } } final Predicate[] restrictions = predicatesOr.toArray(new Predicate[predicatesOr.size()]); return cb.or(restrictions); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * count entity by ParamInfo/*from ww w.j a v a 2s. c om*/ * * @param paramInfo * @return */ public int count(ParamInfo paramInfo) { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityClass); Root<T> userRoot = cq.from(entityClass); cq.select(cb.count(userRoot)); //build query for paramInfo if (paramInfo != null) { Set<Predicate> andCriteria = new HashSet(); Set<Predicate> orCriteria = new HashSet(); for (ParamItem item : paramInfo.getParamItems()) { Predicate predicate; if (item.getValue() instanceof String) { //fuzy search for string String regExp = "%" + item.getValue() + "%"; predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp); } else { predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue()); } switch (item.getOperator()) { case AND: andCriteria.add(predicate); break; case OR: orCriteria.add(predicate); break; } } if (orCriteria.size() > 0) { Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()])); andCriteria.add(or); } if (andCriteria.size() > 0) { Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()])); cq.where(and); } } TypedQuery<Long> query = em.createQuery(cq); Long result = query.getSingleResult(); return result == null ? 0 : result.intValue(); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * list entity by CriteriaInfo/* w w w . j a v a 2 s.co m*/ * * @param criteriaInfo * @return PagedResult */ public PagedResult<T> list(CriteriaInfo criteriaInfo) { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(entityClass); Root<T> userRoot = cq.from(entityClass); cq.select(userRoot); ParamInfo paramInfo = criteriaInfo.getParamInfo(); PageInfo pageInfo = criteriaInfo.getPageInfo(); SortInfo sortInfo = criteriaInfo.getSortInfo(); //build query for paramInfo if (paramInfo != null) { Set<Predicate> andCriteria = new HashSet(); Set<Predicate> orCriteria = new HashSet(); for (ParamItem item : paramInfo.getParamItems()) { Predicate predicate; if (item.getValue() instanceof String) { //fuzy search for string String regExp = "%" + item.getValue() + "%"; predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp); } else { predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue()); } switch (item.getOperator()) { case AND: andCriteria.add(predicate); break; case OR: orCriteria.add(predicate); break; } } if (orCriteria.size() > 0) { Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()])); andCriteria.add(or); } if (andCriteria.size() > 0) { Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()])); cq.where(and); } } //build query for sortInfo Set<Order> orderPredicate = new HashSet<>(); if (sortInfo != null) { for (SortItem item : sortInfo.getSortItems()) { if (item.isDescending()) { orderPredicate.add(cb.desc(userRoot.get(item.getFieldName()))); } else { orderPredicate.add(cb.asc(userRoot.get(item.getFieldName()))); } } } if (orderPredicate.size() > 0) { cq.orderBy(orderPredicate.toArray(new Order[orderPredicate.size()])); } TypedQuery<T> query = em.createQuery(cq); //set result range if (pageInfo != null) { query.setFirstResult(pageInfo.getOffset()); query.setMaxResults(pageInfo.getSize()); } int totalSize; if (paramInfo != null && paramInfo.getParamItems().size() > 0) { totalSize = count(paramInfo); } else { totalSize = count(); } return new PagedResult(query.getResultList(), totalSize); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateDate(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }/* ww w . j av a 2s. com*/ CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamDate> from = cq.from(ResourceIndexedSearchParamDate.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; Predicate p = createPredicateDate(builder, from, params); codePredicates.add(p); } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateString(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }// ww w .ja v a 2 s . c o m CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamString> from = cq.from(ResourceIndexedSearchParamString.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType theParameter = nextOr; Predicate singleCode = createPredicateString(theParameter, theParamName, builder, from); codePredicates.add(singleCode); } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateToken(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }// www . ja va2s .com CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamToken> from = cq.from(ResourceIndexedSearchParamToken.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (nextOr instanceof TokenParam) { TokenParam id = (TokenParam) nextOr; if (id.isText()) { return addPredicateString(theParamName, thePids, theList); } } Predicate singleCode = createPredicateToken(nextOr, theParamName, builder, from); codePredicates.add(singleCode); } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); Predicate type = builder.equal(from.get("myResourceType"), myResourceName); Predicate name = builder.equal(from.get("myParamName"), theParamName); if (thePids.size() > 0) { Predicate inPids = (from.get("myResourcePid").in(thePids)); cq.where(builder.and(type, name, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, name, masterCodePredicate)); } TypedQuery<Long> q = myEntityManager.createQuery(cq); return new HashSet<Long>(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateDate(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsDate", theParamName, ResourceIndexedSearchParamDate.class); return;//from w w w . ja v a2s. c o m } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamDate> from = cq.from(ResourceIndexedSearchParamDate.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } IQueryParameterType params = nextOr; Predicate p = createPredicateDate(builder, from, params); codePredicates.add(p); } Predicate masterCodePredicate = builder.or(toArray(codePredicates)); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); createPredicateLastUpdatedForIndexedSearchParam(builder, from, predicates); predicates.add(masterCodePredicate); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateQuantity(String theParamName, List<? extends IQueryParameterType> theList) { if (Boolean.TRUE.equals(theList.get(0).getMissing())) { addPredicateParamMissing("myParamsQuantity", theParamName, ResourceIndexedSearchParamQuantity.class); return;// w w w. j a va2s.co m } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamQuantity> from = cq.from(ResourceIndexedSearchParamQuantity.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) { continue; } Predicate singleCode = createPredicateQuantity(builder, from, nextOr); codePredicates.add(singleCode); } List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.equal(from.get("myParamName"), theParamName)); predicates.add(builder.or(toArray(codePredicates))); createPredicateResourceId(builder, cq, predicates, from.get("myResourcePid").as(Long.class)); createPredicateLastUpdatedForIndexedSearchParam(builder, from, predicates); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(new HashSet<Long>(q.getResultList())); }