Example usage for javax.persistence.criteria Subquery from

List of usage examples for javax.persistence.criteria Subquery from

Introduction

In this page you can find the example usage for javax.persistence.criteria Subquery from.

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:org.jgrades.admin.accounts.UserSpecificationsImpl.java

private static Predicate getSearchPredicate(Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb,
        Class clazz) {//from  w  w  w .  j a v a2s .  c o  m
    Subquery subquery = cq.subquery(clazz);
    Root subRoot = subquery.from(clazz);
    subquery.select(subRoot);
    Predicate predicate = cb.equal(subRoot.get("id"), root.get("id"));
    subquery.where(predicate);
    return cb.exists(subquery);
}

From source file:org.kuali.rice.kew.rule.dao.impl.RuleDAOJpa.java

private Subquery<RuleResponsibilityBo> addResponsibilityCriteria(CriteriaQuery<RuleBaseValues> query,
        Collection<String> workgroupIds, String workflowId, Collection actionRequestCodes, Boolean searchUser,
        Boolean searchUserInWorkgroups) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    Subquery<RuleResponsibilityBo> subquery = query.subquery(RuleResponsibilityBo.class);
    Root fromResp = subquery.from(RuleResponsibilityBo.class);

    List<javax.persistence.criteria.Predicate> respPredicates = new ArrayList<javax.persistence.criteria.Predicate>();

    List<javax.persistence.criteria.Predicate> ruleRespNamePredicates = new ArrayList<javax.persistence.criteria.Predicate>();

    List<javax.persistence.criteria.Predicate> userNamePreds = new ArrayList<javax.persistence.criteria.Predicate>();

    List<javax.persistence.criteria.Predicate> workgroupPreds = new ArrayList<javax.persistence.criteria.Predicate>();

    if ((actionRequestCodes != null) && (!actionRequestCodes.isEmpty())) {
        Expression<String> exp = fromResp.get("actionRequestedCd");
        javax.persistence.criteria.Predicate actionRequestPredicate = exp.in(actionRequestCodes);

        respPredicates.add(actionRequestPredicate);
    }/*w  ww  . j a  v a2  s .c  o  m*/

    if (!org.apache.commons.lang.StringUtils.isEmpty(workflowId)) {
        // workflow user id exists
        if (searchUser != null && searchUser) {
            // searching user wishes to search for rules specific to user
            userNamePreds.add(cb.like(fromResp.get("ruleResponsibilityName"), workflowId));
            userNamePreds.add(cb.equal(fromResp.get("ruleResponsibilityType"),
                    KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID));

            javax.persistence.criteria.Predicate[] preds = userNamePreds
                    .toArray(new javax.persistence.criteria.Predicate[userNamePreds.size()]);
            ruleRespNamePredicates.add(cb.and(preds));

        }
        if ((searchUserInWorkgroups != null && searchUserInWorkgroups) && (workgroupIds != null)
                && (!workgroupIds.isEmpty())) {
            // at least one workgroup id exists and user wishes to search on workgroups

            Expression<String> exp = fromResp.get("ruleResponsibilityName");
            javax.persistence.criteria.Predicate groupIdPredicate = exp.in(workgroupIds);
            workgroupPreds.add(groupIdPredicate);
            workgroupPreds.add(cb.equal(fromResp.get("ruleResponsibilityType"),
                    KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID));
            javax.persistence.criteria.Predicate[] preds = workgroupPreds
                    .toArray(new javax.persistence.criteria.Predicate[workgroupPreds.size()]);
            ruleRespNamePredicates.add(cb.and(preds));
        }
    } else if ((workgroupIds != null) && (workgroupIds.size() == 1)) {
        // no user and one workgroup id
        workgroupPreds.add(cb.like(fromResp.get("ruleResponsibilityName"), workgroupIds.iterator().next()));
        workgroupPreds.add(
                cb.equal(fromResp.get("ruleResponsibilityType"), KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID));
        javax.persistence.criteria.Predicate[] preds = workgroupPreds
                .toArray(new javax.persistence.criteria.Predicate[workgroupPreds.size()]);
        ruleRespNamePredicates.add(cb.and(preds));

    } else if ((workgroupIds != null) && (workgroupIds.size() > 1)) {
        // no user and more than one workgroup id

        Expression<String> exp = fromResp.get("ruleResponsibilityName");
        javax.persistence.criteria.Predicate groupIdPredicate = exp.in(workgroupIds);
        workgroupPreds.add(
                cb.equal(fromResp.get("ruleResponsibilityType"), KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID));
        javax.persistence.criteria.Predicate[] preds = workgroupPreds
                .toArray(new javax.persistence.criteria.Predicate[workgroupPreds.size()]);
        ruleRespNamePredicates.add(cb.and(preds));
    }

    if (!ruleRespNamePredicates.isEmpty()) {
        javax.persistence.criteria.Predicate[] preds = ruleRespNamePredicates
                .toArray(new javax.persistence.criteria.Predicate[ruleRespNamePredicates.size()]);
        respPredicates.add(cb.or(preds));
    }

    if (!respPredicates.isEmpty()) {

        javax.persistence.criteria.Predicate[] preds = respPredicates
                .toArray(new javax.persistence.criteria.Predicate[respPredicates.size()]);
        subquery.where(preds);
        subquery.select(fromResp.get("ruleBaseValuesId"));
        return subquery;
    }
    return null;
}

From source file:org.kuali.rice.kew.rule.dao.impl.RuleDAOJpa.java

private List<javax.persistence.criteria.Predicate> getSearchCriteria(Root<RuleBaseValues> root,
        CriteriaQuery<RuleBaseValues> query, String docTypeName, String ruleTemplateId, String ruleDescription,
        Boolean delegateRule, Boolean activeInd, Map extensionValues) {
    List<javax.persistence.criteria.Predicate> predicates = new ArrayList<javax.persistence.criteria.Predicate>();
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();

    predicates.add(cb.equal(root.get("currentInd"), Boolean.TRUE));
    predicates.add(cb.equal(root.get("templateRuleInd"), Boolean.FALSE));
    if (activeInd != null) {
        predicates.add(cb.equal(root.get("active"), activeInd));
    }/*from  ww  w  . j av a2s .c  om*/
    if (docTypeName != null) {
        predicates.add(cb.like(cb.upper(root.<String>get("docTypeName")), docTypeName.toUpperCase()));
    }
    if (ruleDescription != null && !ruleDescription.trim().equals("")) {
        predicates.add(cb.like(cb.upper(root.<String>get("description")), ruleDescription.toUpperCase()));
    }
    if (ruleTemplateId != null) {
        predicates.add(cb.equal(root.get("ruleTemplateId"), ruleTemplateId));
    }
    if (delegateRule != null) {
        predicates.add(cb.equal(root.get("delegateRule"), delegateRule));
    }
    if (extensionValues != null && !extensionValues.isEmpty()) {
        for (Iterator iter2 = extensionValues.entrySet().iterator(); iter2.hasNext();) {
            Map.Entry entry = (Map.Entry) iter2.next();
            if (!StringUtils.isEmpty((String) entry.getValue())) {
                Subquery ruleExtSubQuery = query.subquery(RuleExtensionBo.class);
                Root<RuleExtensionBo> ruleExtRoot = ruleExtSubQuery.from(RuleExtensionBo.class);
                javax.persistence.criteria.Predicate predAnd = cb.and(
                        cb.equal(ruleExtRoot.get("extensionValues").get("key"), entry.getKey()),
                        cb.like(ruleExtRoot.get("extensionValues").<String>get("value"),
                                ("%" + (String) entry.getValue() + "%").toUpperCase()));
                ruleExtSubQuery.where(predAnd);
                ruleExtSubQuery.select(ruleExtRoot.get("ruleBaseValuesId"));

                predicates.add(cb.in(root.get("id")).value(ruleExtSubQuery));
            }
        }
    }
    return predicates;
}

From source file:org.kuali.rice.kew.rule.dao.impl.RuleDAOJpa.java

private Subquery<RuleResponsibilityBo> getRuleResponsibilitySubQuery(String ruleRespName,
        CriteriaQuery<RuleBaseValues> query) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    Subquery<RuleResponsibilityBo> subquery = query.subquery(RuleResponsibilityBo.class);
    Root fromResp = subquery.from(RuleResponsibilityBo.class);
    subquery.where(cb.equal(fromResp.get("ruleResponsibilityName"), ruleRespName));
    subquery.select(fromResp.get("ruleBaseValuesId"));

    return subquery;
}

From source file:org.seedstack.i18n.rest.internal.infrastructure.jpa.KeysQuery.java

private Subquery<String> getAllKeysWithTranslationFor(String locale) {
    Subquery<String> subQuery;
    if (selectQuery != null) {
        subQuery = selectQuery.subquery(String.class);
    } else {// w  w  w  . ja  v a 2 s  .co  m
        subQuery = countQuery.subquery(String.class);
    }
    Root<Key> fromKey = subQuery.from(Key.class);

    subQuery.select(fromKey.get(ENTITY_ID));

    Join join = fromKey.join(TRANSLATIONS, JoinType.LEFT);

    Predicate keyIdsAreEquals = criteriaBuilder.equal(join.get(ENTITY_ID).get(LOCALE), locale);
    Predicate translationIsPresent = criteriaBuilder.notEqual(join.get(TRANSLATION_VALUE), "");

    subQuery.where(criteriaBuilder.and(keyIdsAreEquals, translationIsPresent));
    return subQuery;
}

From source file:org.seedstack.i18n.rest.internal.KeyJpaFinder.java

/**
 * Extracts predicates from criteria.//from www. ja  va  2 s  . c  o m
 *
 * @param criteria criteria
 * @param cb       criteria builder
 * @param k        root key
 * @return list of predicate
 */
private Predicate[] getPredicates(Map<String, Object> criteria, CriteriaQuery q, CriteriaBuilder cb,
        Root<Key> k) {
    List<Predicate> predicates = new ArrayList<Predicate>();
    if (criteria != null) {
        // extract criteria from the map
        Boolean isApprox = (Boolean) criteria.get(IS_APPROX);
        Boolean isMissing = (Boolean) criteria.get(IS_MISSING);
        Boolean isOutdated = (Boolean) criteria.get(IS_OUTDATED);
        String searchName = (String) criteria.get(SEARCH_NAME);

        // is the key LIKE searchName
        if (StringUtils.isNotBlank(searchName)) {
            predicates.add(cb.like(k.<String>get(ENTITY_ID), "%" + searchName + "%"));
        }
        // is the key outdated
        if (isOutdated != null) {
            predicates.add(cb.equal(k.<Boolean>get(OUTDATED), isOutdated));
        }

        // if a default translation is available
        String defaultLocale = localeService.getDefaultLocale();
        if (defaultLocale != null && isApprox != null) {
            // join translation table
            Join<Key, Translation> tln = k.join(TRANSLATIONS, JoinType.LEFT);
            // WHERE locale = default locale
            predicates.add(cb.equal(tln.get(ENTITY_ID).get(LOCALE), defaultLocale));

            // is the translation approximate
            predicates.add(cb.equal(tln.get(APPROXIMATE), isApprox));
        }

        // is the translation missing
        if (isMissing != null) {
            // SubQuery to find all the key which get a translation for the default locale
            //noinspection unchecked
            Subquery<String> subquery = q.subquery(String.class);
            Root<Key> fromKey = subquery.from(Key.class);
            subquery.select(fromKey.<String>get(ENTITY_ID));
            Join join = fromKey.join(TRANSLATIONS, JoinType.LEFT);
            subquery.where(cb.and(cb.equal(join.get(ENTITY_ID).get(LOCALE), defaultLocale),
                    cb.notEqual(join.get(VALUE), "")));
            // Find all keys not in the above subquery, ie. all the keys missing
            predicates.add(cb.not(cb.in(k.get(ENTITY_ID)).value(subquery)));
        }
    }
    return predicates.toArray(new Predicate[predicates.size()]);
}

From source file:org.seedstack.i18n.rest.internal.TranslationJpaFinder.java

/**
 * Extracts predicates from criteria.//  w ww .j  a  v a 2 s . c  o  m
 *
 * @param criteria criteria
 * @param cb       criteria builder
 * @param k        root key
 * @return list of predicate
 */
private Predicate[] getPredicates(Map<String, Object> criteria, CriteriaQuery q, CriteriaBuilder cb,
        Root<Key> k) {
    List<Predicate> predicates = new ArrayList<Predicate>();
    if (criteria != null) {
        // extract criteria from the map
        Boolean isApprox = (Boolean) criteria.get(IS_APPROX);
        Boolean isMissing = (Boolean) criteria.get(IS_MISSING);
        Boolean isOutdated = (Boolean) criteria.get(IS_OUTDATED);
        String searchName = (String) criteria.get(SEARCH_NAME);
        String locale = (String) criteria.get(LOCALE);

        // is the key LIKE searchName
        if (StringUtils.isNotBlank(searchName)) {
            predicates.add(cb.like(k.<String>get(ENTITY_ID), "%" + searchName + "%"));
        }

        // if a default translation is available
        if (isApprox != null || isOutdated != null) {
            // join translation table
            Join<Key, Translation> tln = k.join(TRANSLATIONS, JoinType.LEFT);
            // WHERE locale = default locale
            predicates.add(cb.equal(tln.get(ENTITY_ID).get(LOCALE), locale));

            // is the key outdated
            if (isOutdated != null) {
                predicates.add(cb.equal(tln.<Boolean>get(OUTDATED), isOutdated));
            }

            // is the translation approximate
            if (isApprox != null) {
                predicates.add(cb.equal(tln.<Boolean>get(APPROXIMATE), isApprox));
            }
        }
        // is the translation missing
        if (isMissing != null) {
            // SubQuery to find all the key which get a translation for the default locale
            //noinspection unchecked
            Subquery<String> subquery = q.subquery(String.class);
            Root<Key> fromKey = subquery.from(Key.class);
            subquery.select(fromKey.<String>get(ENTITY_ID));
            Join join = fromKey.join(TRANSLATIONS, JoinType.LEFT);
            subquery.where(cb.and(cb.equal(join.get(ENTITY_ID).get(LOCALE), locale),
                    cb.notEqual(join.get(VALUE), "")));
            // Find all keys not in the above subquery, ie. all the keys missing
            predicates.add(cb.not(cb.in(k.get(ENTITY_ID)).value(subquery)));
        }
    }
    return predicates.toArray(new Predicate[predicates.size()]);
}