Example usage for javax.persistence.criteria CriteriaQuery select

List of usage examples for javax.persistence.criteria CriteriaQuery select

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery select.

Prototype

CriteriaQuery<T> select(Selection<? extends T> selection);

Source Link

Document

Specify the item that is to be returned in the query result.

Usage

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateParamMissing(String joinName, String theParamName,
        Class<? extends BaseResourceIndexedSearchParam> theParamTable) {
    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<? extends BaseResourceIndexedSearchParam> subQfrom = subQ.from(theParamTable);
    subQ.select(subQfrom.get("myResourcePid").as(Long.class));
    Predicate subQname = builder.equal(subQfrom.get("myParamName"), theParamName);
    Predicate subQtype = builder.equal(subQfrom.get("myResourceType"), myResourceName);
    subQ.where(builder.and(subQtype, subQname));

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(builder.not(builder.in(from.get("myId")).value(subQ)));
    predicates.add(builder.equal(from.get("myResourceType"), myResourceName));
    predicates.add(builder.isNull(from.get("myDeleted")));
    createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class));

    cq.where(builder.and(toArray(predicates)));

    ourLog.info("Adding :missing qualifier for parameter '{}'", theParamName);

    TypedQuery<Long> q = myEntityManager.createQuery(cq);
    doSetPids(q.getResultList());/* w  w  w.j  ava  2  s  .c o  m*/
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateComposite(RuntimeSearchParam theParamDef,
        List<? extends IQueryParameterType> theNextAnd) {
    // TODO: fail if missing is set for a composite query

    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));

    IQueryParameterType or = theNextAnd.get(0);
    if (!(or instanceof CompositeParam<?, ?>)) {
        throw new InvalidRequestException("Invalid type for composite param (must be "
                + CompositeParam.class.getSimpleName() + ": " + or.getClass());
    }/*from  w  w w  .ja v  a 2s  .c o m*/
    CompositeParam<?, ?> cp = (CompositeParam<?, ?>) or;

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(builder.equal(from.get("myResourceType"), myResourceName));

    RuntimeSearchParam left = theParamDef.getCompositeOf().get(0);
    IQueryParameterType leftValue = cp.getLeftValue();
    predicates.add(createCompositeParamPart(builder, from, left, leftValue));

    RuntimeSearchParam right = theParamDef.getCompositeOf().get(1);
    IQueryParameterType rightValue = cp.getRightValue();
    predicates.add(createCompositeParamPart(builder, from, right, rightValue));

    createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class));
    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 addPredicateLanguage(List<List<? extends IQueryParameterType>> theList) {
    for (List<? extends IQueryParameterType> nextList : theList) {

        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));

        Set<String> values = new HashSet<String>();
        for (IQueryParameterType next : nextList) {
            if (next instanceof StringParam) {
                String nextValue = ((StringParam) next).getValue();
                if (isBlank(nextValue)) {
                    continue;
                }/*  w  ww  .  j a v  a  2 s  .  c  o m*/
                values.add(nextValue);
            } else {
                throw new InternalErrorException(
                        "Lanugage parameter must be of type " + StringParam.class.getCanonicalName() + " - Got "
                                + next.getClass().getCanonicalName());
            }
        }

        if (values.isEmpty()) {
            continue;
        }

        List<Predicate> predicates = new ArrayList<Predicate>();
        predicates.add(builder.equal(from.get("myResourceType"), myResourceName));
        predicates.add(from.get("myLanguage").as(String.class).in(values));
        createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class));
        createPredicateLastUpdatedForResourceTable(builder, from, predicates);

        predicates.add(builder.isNull(from.get("myDeleted")));

        cq.where(toArray(predicates));

        TypedQuery<Long> q = myEntityManager.createQuery(cq);
        doSetPids(q.getResultList());
        if (doHaveNoResults()) {
            return;
        }
    }

    return;
}

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;/*w  w w .  j ava 2 s  .  co  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 addPredicateString(String theParamName, List<? extends IQueryParameterType> theList) {
    if (Boolean.TRUE.equals(theList.get(0).getMissing())) {
        addPredicateParamMissing("myParamsString", theParamName, ResourceIndexedSearchParamString.class);
        return;//  w ww .ja  v  a  2  s. c om
    }

    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;
        if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) {
            continue;
        }

        Predicate singleCode = createPredicateString(theParameter, theParamName, builder, from);
        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()));
}

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;//from   w  ww .j a v a2  s. com
    }

    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()));
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateHas(List<List<? extends IQueryParameterType>> theHasParameters,
        DateRangeParam theLastUpdated) {

    for (List<? extends IQueryParameterType> nextOrList : theHasParameters) {

        StringBuilder valueBuilder = new StringBuilder();
        String targetResourceType = null;
        String owningParameter = null;
        String parameterName = null;
        for (IQueryParameterType nextParam : nextOrList) {
            HasParam next = (HasParam) nextParam;
            if (valueBuilder.length() > 0) {
                valueBuilder.append(',');
            }/*w  w  w .j ava  2 s .  c  o m*/
            valueBuilder.append(UrlUtil.escape(next.getValueAsQueryToken(myContext)));
            targetResourceType = next.getTargetResourceType();
            owningParameter = next.getOwningFieldName();
            parameterName = next.getParameterName();
        }

        if (valueBuilder.length() == 0) {
            continue;
        }

        String matchUrl = targetResourceType + '?' + UrlUtil.escape(parameterName) + '='
                + valueBuilder.toString();
        RuntimeResourceDefinition targetResourceDefinition;
        try {
            targetResourceDefinition = myContext.getResourceDefinition(targetResourceType);
        } catch (DataFormatException e) {
            throw new InvalidRequestException("Invalid resource type: " + targetResourceType);
        }

        RuntimeSearchParam owningParameterDef = targetResourceDefinition
                .getSearchParam(parameterName.replaceAll("\\..*", ""));
        if (owningParameterDef == null) {
            throw new InvalidRequestException(
                    "Unknown parameter name: " + targetResourceType + ':' + parameterName);
        }

        owningParameterDef = targetResourceDefinition.getSearchParam(owningParameter);
        if (owningParameterDef == null) {
            throw new InvalidRequestException(
                    "Unknown parameter name: " + targetResourceType + ':' + owningParameter);
        }

        Class<? extends IBaseResource> resourceType = targetResourceDefinition.getImplementingClass();
        Set<Long> match = myCallingDao.processMatchUrl(matchUrl, resourceType);
        if (match.isEmpty()) {
            doSetPids(new ArrayList<Long>());
            return;
        }

        CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
        CriteriaQuery<Long> cq = builder.createQuery(Long.class);
        Root<ResourceLink> from = cq.from(ResourceLink.class);
        cq.select(from.get("myTargetResourcePid").as(Long.class));

        List<Predicate> predicates = new ArrayList<Predicate>();
        predicates.add(builder.equal(from.get("mySourceResourceType"), targetResourceType));
        predicates.add(from.get("mySourceResourcePid").in(match));
        predicates.add(createResourceLinkPathPredicate(myContext, owningParameter, from, resourceType));
        predicates.add(builder.equal(from.get("myTargetResourceType"), myResourceName));
        createPredicateResourceId(builder, cq, predicates, from.get("myId").as(Long.class));
        createPredicateLastUpdatedForResourceLink(builder, from, predicates);

        cq.where(toArray(predicates));

        TypedQuery<Long> q = myEntityManager.createQuery(cq);
        doSetPids(q.getResultList());
        if (doHaveNoResults()) {
            return;
        }

        return;
    }
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateToken(String theParamName, List<? extends IQueryParameterType> theList) {

    if (Boolean.TRUE.equals(theList.get(0).getMissing())) {
        addPredicateParamMissing("myParamsToken", theParamName, ResourceIndexedSearchParamToken.class);
        return;/*from w  w  w.j av a 2  s.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 (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) {
            continue;
        }

        if (nextOr instanceof TokenParam) {
            TokenParam id = (TokenParam) nextOr;
            if (id.isText()) {
                addPredicateString(theParamName, theList);
                continue;
            }
        }

        Predicate singleCode = createPredicateToken(nextOr, theParamName, builder, from);
        if (singleCode == null) {
            doSetPids(new ArrayList<Long>());
            return;
        }
        codePredicates.add(singleCode);
    }

    if (codePredicates.isEmpty()) {
        return;
    }

    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));

    cq.where(builder.and(toArray(predicates)));

    TypedQuery<Long> q = myEntityManager.createQuery(cq);
    doSetPids(new HashSet<Long>(q.getResultList()));
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateNumber(String theParamName, List<? extends IQueryParameterType> theList) {

    if (Boolean.TRUE.equals(theList.get(0).getMissing())) {
        addPredicateParamMissing("myParamsNumber", theParamName, ResourceIndexedSearchParamNumber.class);
        return;/*www .  jav  a 2s.  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 (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) {
            continue;
        }

        if (params instanceof NumberParam) {
            NumberParam param = (NumberParam) params;

            BigDecimal value = param.getValue();
            if (value == null) {
                continue;
            }

            final Expression<BigDecimal> fromObj = from.get("myValue");
            ParamPrefixEnum prefix = ObjectUtils.defaultIfNull(param.getPrefix(), ParamPrefixEnum.EQUAL);
            String invalidMessageName = "invalidNumberPrefix";
            String valueAsString = param.getValue().toPlainString();

            Predicate num = createPredicateNumeric(builder, params, prefix, value, fromObj, invalidMessageName,
                    valueAsString);
            codePredicates.add(num);

        } else {
            throw new IllegalArgumentException("Invalid token type: " + params.getClass());
        }

    }

    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(q.getResultList());
}

From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java

private void addPredicateUri(String theParamName, List<? extends IQueryParameterType> theList) {
    if (Boolean.TRUE.equals(theList.get(0).getMissing())) {
        addPredicateParamMissing("myParamsUri", theParamName, ResourceIndexedSearchParamUri.class);
        return;/* w w w . j  a  v a  2  s  .  co  m*/
    }

    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<ResourceIndexedSearchParamUri> from = cq.from(ResourceIndexedSearchParamUri.class);
    cq.select(from.get("myResourcePid").as(Long.class));

    List<Predicate> codePredicates = new ArrayList<Predicate>();
    for (IQueryParameterType nextOr : theList) {
        IQueryParameterType params = nextOr;

        if (addPredicateMissingFalseIfPresent(builder, theParamName, from, codePredicates, nextOr)) {
            continue;
        }

        if (params instanceof UriParam) {
            UriParam param = (UriParam) params;

            String value = param.getValue();
            if (value == null) {
                continue;
            }

            Path<Object> fromObj = from.get("myUri");
            Predicate predicate;
            if (param.getQualifier() == UriParamQualifierEnum.ABOVE) {

                /*
                 * :above is an inefficient query- It means that the user is supplying a more specific URL (say
                 * http://example.com/foo/bar/baz) and that we should match on any URLs that are less
                 * specific but otherwise the same. For example http://example.com and http://example.com/foo would both
                 * match.
                 * 
                 * We do this by querying the DB for all candidate URIs and then manually checking each one. This isn't
                 * very efficient, but this is also probably not a very common type of query to do.
                 * 
                 * If we ever need to make this more efficient, lucene could certainly be used as an optimization.
                 */
                ourLog.info("Searching for candidate URI:above parameters for Resource[{}] param[{}]",
                        myResourceName, theParamName);
                Collection<String> candidates = myResourceIndexedSearchParamUriDao
                        .findAllByResourceTypeAndParamName(myResourceName, theParamName);
                List<String> toFind = new ArrayList<String>();
                for (String next : candidates) {
                    if (value.length() >= next.length()) {
                        if (value.substring(0, next.length()).equals(next)) {
                            toFind.add(next);
                        }
                    }
                }

                if (toFind.isEmpty()) {
                    continue;
                }

                predicate = fromObj.as(String.class).in(toFind);

            } else if (param.getQualifier() == UriParamQualifierEnum.BELOW) {
                predicate = builder.like(fromObj.as(String.class), createLeftMatchLikeExpression(value));
            } else {
                predicate = builder.equal(fromObj.as(String.class), value);
            }
            codePredicates.add(predicate);
        } else {
            throw new IllegalArgumentException("Invalid URI type: " + params.getClass());
        }

    }

    if (codePredicates.isEmpty()) {
        doSetPids(new HashSet<Long>());
        return;
    }

    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));

    cq.where(builder.and(toArray(predicates)));

    TypedQuery<Long> q = myEntityManager.createQuery(cq);
    doSetPids(new HashSet<Long>(q.getResultList()));
}