List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
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; }//from ww w .ja va2s . c o m 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.BaseFhirResourceDao.java
private Set<Long> addPredicateReference(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { assert theParamName.contains(".") == false; Set<Long> pidsToRetain = thePids; if (theList == null || theList.isEmpty()) { return pidsToRetain; }// w w w . ja v a 2 s.co m CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceLink> from = cq.from(ResourceLink.class); cq.select(from.get("mySourceResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (params instanceof ReferenceParam) { ReferenceParam ref = (ReferenceParam) params; String resourceId = ref.getValueAsQueryToken(); if (resourceId.contains("/")) { IdDt dt = new IdDt(resourceId); resourceId = dt.getIdPart(); } if (isBlank(ref.getChain())) { Long targetPid = translateForcedIdToPid(new IdDt(resourceId)); ourLog.info("Searching for resource link with target PID: {}", targetPid); Predicate eq = builder.equal(from.get("myTargetResourcePid"), targetPid); codePredicates.add(eq); } else { String chain = getContext().getResourceDefinition(myResourceType).getSearchParam(theParamName) .getPath(); BaseRuntimeChildDefinition def = getContext().newTerser().getDefinition(myResourceType, chain); if (!(def instanceof RuntimeChildResourceDefinition)) { throw new ConfigurationException("Property " + chain + " of type " + myResourceName + " is not a resource: " + def.getClass()); } List<Class<? extends IBaseResource>> resourceTypes; if (isBlank(ref.getResourceType())) { RuntimeChildResourceDefinition resDef = (RuntimeChildResourceDefinition) def; resourceTypes = resDef.getResourceTypes(); } else { resourceTypes = new ArrayList<Class<? extends IBaseResource>>(); RuntimeResourceDefinition resDef = getContext() .getResourceDefinition(ref.getResourceType()); resourceTypes.add(resDef.getImplementingClass()); } for (Class<? extends IBaseResource> nextType : resourceTypes) { RuntimeResourceDefinition typeDef = getContext().getResourceDefinition(nextType); RuntimeSearchParam param = typeDef.getSearchParam(ref.getChain()); if (param == null) { ourLog.debug("Type {} doesn't have search param {}", nextType.getSimpleName(), param); continue; } IFhirResourceDao<?> dao = getDao(nextType); if (dao == null) { ourLog.debug("Don't have a DAO for type {}", nextType.getSimpleName(), param); continue; } IQueryParameterType chainValue = toParameterType(param, resourceId); Set<Long> pids = dao.searchForIds(ref.getChain(), chainValue); if (pids.isEmpty()) { continue; } Predicate eq = from.get("myTargetResourcePid").in(pids); codePredicates.add(eq); } } } else { throw new IllegalArgumentException("Invalid token type: " + params.getClass()); } } Predicate masterCodePredicate = builder.or(codePredicates.toArray(new Predicate[0])); RuntimeSearchParam param = getContext().getResourceDefinition(getResourceType()) .getSearchParam(theParamName); String path = param.getPath(); Predicate type = builder.equal(from.get("mySourcePath"), path); if (pidsToRetain.size() > 0) { Predicate inPids = (from.get("mySourceResourcePid").in(pidsToRetain)); cq.where(builder.and(type, masterCodePredicate, inPids)); } else { cq.where(builder.and(type, 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> addPredicateNumber(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }//from ww w .jav a 2 s .c o m CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceIndexedSearchParamNumber> from = cq.from(ResourceIndexedSearchParamNumber.class); cq.select(from.get("myResourcePid").as(Long.class)); List<Predicate> codePredicates = new ArrayList<Predicate>(); for (IQueryParameterType nextOr : theList) { IQueryParameterType params = nextOr; if (params instanceof NumberParam) { NumberParam param = (NumberParam) params; BigDecimal value = param.getValue(); if (value == null) { return thePids; } Path<Object> fromObj = from.get("myValue"); if (param.getComparator() == null) { double mul = value.doubleValue() * 1.01; double low = value.doubleValue() - mul; double high = value.doubleValue() + mul; Predicate lowPred = builder.ge(fromObj.as(Long.class), low); Predicate highPred = builder.le(fromObj.as(Long.class), high); codePredicates.add(builder.and(lowPred, highPred)); } else { switch (param.getComparator()) { case GREATERTHAN: codePredicates.add(builder.greaterThan(fromObj.as(BigDecimal.class), value)); break; case GREATERTHAN_OR_EQUALS: codePredicates.add(builder.ge(fromObj.as(BigDecimal.class), value)); break; case LESSTHAN: codePredicates.add(builder.lessThan(fromObj.as(BigDecimal.class), value)); break; case LESSTHAN_OR_EQUALS: codePredicates.add(builder.le(fromObj.as(BigDecimal.class), value)); break; } } } else { throw new IllegalArgumentException("Invalid token type: " + params.getClass()); } } 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:com.tasktop.c2c.server.profile.tests.service.BaseProfileServiceTest.java
private <T> int getEntityCount(Class<T> entityClass) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class); Root<T> root = query.from(entityClass); query.select(criteriaBuilder.count(root)); return entityManager.createQuery(query).getSingleResult().intValue(); }
From source file:ca.uhn.fhir.jpa.dao.BaseFhirResourceDao.java
private Set<Long> addPredicateQuantity(String theParamName, Set<Long> thePids, List<? extends IQueryParameterType> theList) { if (theList == null || theList.isEmpty()) { return thePids; }//from ww w. java 2 s. c om 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) { IQueryParameterType params = nextOr; String systemValue; String unitsValue; QuantityCompararatorEnum cmpValue; BigDecimal valueValue; boolean approx = false; if (params instanceof BaseQuantityDt) { BaseQuantityDt param = (BaseQuantityDt) params; systemValue = param.getSystemElement().getValueAsString(); unitsValue = param.getUnitsElement().getValueAsString(); cmpValue = QuantityCompararatorEnum.VALUESET_BINDER .fromCodeString(param.getComparatorElement().getValueAsString()); valueValue = param.getValueElement().getValue(); } else if (params instanceof QuantityParam) { QuantityParam param = (QuantityParam) params; systemValue = param.getSystem().getValueAsString(); unitsValue = param.getUnits(); cmpValue = param.getComparator(); valueValue = param.getValue().getValue(); approx = param.isApproximate(); } else { throw new IllegalArgumentException("Invalid quantity type: " + params.getClass()); } Predicate system = null; if (!isBlank(systemValue)) { system = builder.equal(from.get("mySystem"), systemValue); } Predicate code = null; if (!isBlank(unitsValue)) { code = builder.equal(from.get("myUnits"), unitsValue); } Predicate num; if (cmpValue == null) { BigDecimal mul = approx ? new BigDecimal(0.1) : new BigDecimal(0.01); BigDecimal low = valueValue.subtract(valueValue.multiply(mul)); BigDecimal high = valueValue.add(valueValue.multiply(mul)); Predicate lowPred = builder.gt(from.get("myValue").as(BigDecimal.class), low); Predicate highPred = builder.lt(from.get("myValue").as(BigDecimal.class), high); num = builder.and(lowPred, highPred); } else { switch (cmpValue) { case GREATERTHAN: Expression<Number> path = from.get("myValue"); num = builder.gt(path, valueValue); break; case GREATERTHAN_OR_EQUALS: path = from.get("myValue"); num = builder.ge(path, valueValue); break; case LESSTHAN: path = from.get("myValue"); num = builder.lt(path, valueValue); break; case LESSTHAN_OR_EQUALS: path = from.get("myValue"); num = builder.le(path, valueValue); break; default: throw new IllegalStateException(cmpValue.getCode()); } } if (system == null && code == null) { codePredicates.add(num); } else if (system == null) { Predicate singleCode = builder.and(code, num); codePredicates.add(singleCode); } else if (code == null) { Predicate singleCode = builder.and(system, num); codePredicates.add(singleCode); } else { Predicate singleCode = builder.and(system, code, num); 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:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java
public List<ServerTuple> getAppServers(String hostFilter, String appServerFilter, String runtimeFilter, String nodeFilter, String contextFilter) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class); //get Node ResType Root<ResourceEntity> node = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT); //get AppServer Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations", JoinType.LEFT);//from www . j av a 2 s . c o m Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT); //get Runtime of as Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations", JoinType.LEFT); Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT); //get Props between as and node Join<ConsumedResourceRelationEntity, ResourceRelationContextEntity> resRelCtx = nodeResRel.join("contexts", JoinType.LEFT); Join<ResourceRelationContextEntity, ContextEntity> asCtx = resRelCtx.join("context", JoinType.LEFT); Join<ContextEntity, ContextEntity> asDomain = asCtx.join("parent", JoinType.LEFT); Join<ResourceRelationContextEntity, PropertyEntity> asProp = resRelCtx.join("properties", JoinType.LEFT); //here an on clause should be added, so we don't get hostnames that are divined directly on the node multiple times (and descriptor.propertyName = 'hostName') //on support was added in jpa 2.1 which is part of JEE 7 Join<PropertyEntity, PropertyDescriptorEntity> asPropType = asProp.join("descriptor", JoinType.LEFT); q.select(cb.construct(ServerTuple.class, asProp.get("value"), appServer.get("name"), appServer.get("id"), asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"), nodeRelease.get("name"), asDomain.get("name"), asDomain.get("id"), asCtx.get("name"), asCtx.get("id"), cb.literal(0) //false )); Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()), cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()), cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME), cb.isNull(runtimeType.get("name"))), cb.isNotNull(asProp.get("value")), cb.equal(asPropType.get("propertyName"), "hostName")); p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter, asProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"), node.<String>get("name"), asCtx.<String>get("name")); q.where(p); TypedQuery<ServerTuple> query = entityManager.createQuery(q); List<ServerTuple> servers = query.getResultList(); return servers; }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void filterResourceIdsByLastUpdated(final DateRangeParam theLastUpdated) { CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); List<Predicate> lastUpdatedPredicates = createLastUpdatedPredicates(theLastUpdated, builder, from); createPredicateResourceId(builder, cq, lastUpdatedPredicates, from.get("myId").as(Long.class)); cq.where(SearchBuilder.toArray(lastUpdatedPredicates)); TypedQuery<Long> query = myEntityManager.createQuery(cq); List<Long> resultList = query.getResultList(); doSetPids(resultList);/*w ww . j a v a 2 s. c om*/ }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private TypedQuery<Long> createSearchAllByTypeQuery(DateRangeParam theLastUpdated) { CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(builder.isNull(from.get("myDeleted"))); if (theLastUpdated != null) { predicates.addAll(createLastUpdatedPredicates(theLastUpdated, builder, from)); }//from ww w . j a va 2 s .co m cq.where(toArray(predicates)); TypedQuery<Long> query = myEntityManager.createQuery(cq); return query; }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateId(Set<Long> thePids) { if (thePids == null || thePids.isEmpty()) { return;//from w w w. j a v a 2 s .c om } CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); predicates.add(from.get("myId").in(thePids)); createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class)); createPredicateLastUpdatedForResourceTable(builder, from, predicates); cq.where(toArray(predicates)); TypedQuery<Long> q = myEntityManager.createQuery(cq); doSetPids(q.getResultList()); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private void addPredicateParamMissingResourceLink(String joinName, String theParamName) { CriteriaBuilder builder = myEntityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = builder.createQuery(Long.class); Root<ResourceTable> from = cq.from(ResourceTable.class); cq.select(from.get("myId").as(Long.class)); Subquery<Long> subQ = cq.subquery(Long.class); Root<ResourceLink> subQfrom = subQ.from(ResourceLink.class); subQ.select(subQfrom.get("mySourceResourcePid").as(Long.class)); // subQ.where(builder.equal(subQfrom.get("myParamName"), theParamName)); Predicate path = createResourceLinkPathPredicate(theParamName, subQfrom); subQ.where(path);// w w w . j a va2s . c o m List<Predicate> predicates = new ArrayList<Predicate>(); createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class)); predicates.add(builder.not(builder.in(from.get("myId")).value(subQ))); predicates.add(builder.equal(from.get("myResourceType"), myResourceName)); cq.where(builder.and(toArray(predicates))); TypedQuery<Long> q = myEntityManager.createQuery(cq); List<Long> resultList = q.getResultList(); doSetPids(new HashSet<Long>(resultList)); }