List of usage examples for javax.persistence.criteria CriteriaBuilder and
Predicate and(Predicate... restrictions);
From source file:org.finra.herd.dao.impl.TagDaoImpl.java
@Override public TagEntity getTagByTagTypeAndDisplayName(String tagTypeCode, String displayName) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<TagEntity> criteria = builder.createQuery(TagEntity.class); // The criteria root is the tag entity. Root<TagEntity> tagEntityRoot = criteria.from(TagEntity.class); // Join on the other tables we can filter on. Join<TagEntity, TagTypeEntity> tagTypeEntityJoin = tagEntityRoot.join(TagEntity_.tagType); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(builder.upper(tagTypeEntityJoin.get(TagTypeEntity_.code)), tagTypeCode.toUpperCase())); predicates.add(/*from ww w. j a va 2 s . c om*/ builder.equal(builder.upper(tagEntityRoot.get(TagEntity_.displayName)), displayName.toUpperCase())); // Add all clauses to the query. criteria.select(tagEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))); return executeSingleResultQuery(criteria, String.format("Found more than one tag with parameters {tagType=\"%s\", displayName=\"%s\"}.", tagTypeCode, displayName)); }
From source file:org.finra.herd.dao.impl.TagDaoImpl.java
@Override public List<TagChild> getTagsByTagTypeAndParentTagCode(String tagType, String parentTagCode) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<TagEntity> criteria = builder.createQuery(TagEntity.class); // The criteria root is the tag entity. Root<TagEntity> tagEntityRoot = criteria.from(TagEntity.class); // Join to the other tables we can filter on. Join<TagEntity, TagTypeEntity> tagTypeEntityJoin = tagEntityRoot.join(TagEntity_.tagType); // Get the columns. Path<String> displayNameColumn = tagEntityRoot.get(TagEntity_.displayName); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(// www . java2 s . c o m builder.equal(builder.upper(tagTypeEntityJoin.get(TagTypeEntity_.code)), tagType.toUpperCase())); if (parentTagCode == null) { // Parent tag code is not specified, then return all tags with no parents, i.e. root tags. predicates.add(builder.isNull(tagEntityRoot.get(TagEntity_.parentTagEntity))); } else { // Add a restriction for the parent tag code. predicates.add(builder.equal( builder.upper(tagEntityRoot.get(TagEntity_.parentTagEntity).get(TagEntity_.tagCode)), parentTagCode.toUpperCase())); } // Add all clauses to the query. criteria.select(tagEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))) .orderBy(builder.asc(displayNameColumn)); // Run the query to get a list of tag entities back. List<TagEntity> tagEntities = entityManager.createQuery(criteria).getResultList(); // Populate tag child objects from the returned tag entities. List<TagChild> tagChildren = new ArrayList<>(); for (TagEntity tagEntity : tagEntities) { boolean hasChildren = !tagEntity.getChildrenTagEntities().isEmpty(); tagChildren.add(new TagChild(new TagKey(tagEntity.getTagType().getCode(), tagEntity.getTagCode()), hasChildren)); } return tagChildren; }
From source file:org.finra.herd.dao.impl.TagDaoImpl.java
@Override public List<TagEntity> getTagsByTagTypeEntityAndParentTagCode(TagTypeEntity tagTypeEntity, String parentTagCode, Boolean isParentTagNull) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<TagEntity> criteria = builder.createQuery(TagEntity.class); // The criteria root is the tag entity. Root<TagEntity> tagEntityRoot = criteria.from(TagEntity.class); // Get the columns. Path<String> displayNameColumn = tagEntityRoot.get(TagEntity_.displayName); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(tagEntityRoot.get(TagEntity_.tagType), tagTypeEntity)); if (StringUtils.isNotBlank(parentTagCode)) { // Return all tags that are immediate children of the specified parent tag. predicates.add(builder.equal(//w w w . j a va 2 s . co m builder.upper(tagEntityRoot.get(TagEntity_.parentTagEntity).get(TagEntity_.tagCode)), parentTagCode.toUpperCase())); } else if (BooleanUtils.isTrue(isParentTagNull)) { // The flag is non-null and true, return all tags with no parents, i.e. root tags. predicates.add(builder.isNull(tagEntityRoot.get(TagEntity_.parentTagEntity))); } else if (BooleanUtils.isFalse(isParentTagNull)) { // The flag is non-null and false, return all tags with parents. predicates.add(builder.isNotNull(tagEntityRoot.get(TagEntity_.parentTagEntity))); } // Add all clauses to the query. criteria.select(tagEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))) .orderBy(builder.asc(displayNameColumn)); // Run the query to get the results. return entityManager.createQuery(criteria).getResultList(); }
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); }//from ww w.ja va2 s . c om 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.opencastproject.messages.MailService.java
public List<MessageTemplate> findMessageTemplates(TemplateMessageQuery query) { EntityManager em = null;//from w ww. j a v a 2 s . c om List<MessageTemplate> messageTemplates = new ArrayList<MessageTemplate>(); try { em = emf.createEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<MessageTemplateDto> q = cb.createQuery(MessageTemplateDto.class); Root<MessageTemplateDto> messageTemplateRoot = q.from(MessageTemplateDto.class); List<Predicate> predicates = new ArrayList<Predicate>(); q.select(messageTemplateRoot); String orgId = securityService.getOrganization().getId(); predicates.add(cb.equal(messageTemplateRoot.get("organization"), orgId)); if (!query.isIncludeHidden()) predicates.add(cb.isFalse(messageTemplateRoot.get("hidden").as(Boolean.class))); if (StringUtils.isNotEmpty(query.getName())) predicates.add(cb.equal(messageTemplateRoot.get("name"), query.getName())); if (StringUtils.isNotEmpty(query.getCreator())) predicates.add(cb.equal(messageTemplateRoot.get("creator"), query.getCreator())); if (query.getType() != null) predicates.add( cb.equal(messageTemplateRoot.get("type").as(TemplateType.Type.class), query.getType())); if (StringUtils.isNotEmpty(query.getFullText())) { List<Predicate> fullTextPredicates = new ArrayList<Predicate>(); fullTextPredicates .add(cb.like(messageTemplateRoot.<String>get("name"), "%" + query.getFullText() + "%")); fullTextPredicates .add(cb.like(messageTemplateRoot.<String>get("subject"), "%" + query.getFullText() + "%")); fullTextPredicates .add(cb.like(messageTemplateRoot.<String>get("body"), "%" + query.getFullText() + "%")); predicates.add(cb.or(fullTextPredicates.toArray(new Predicate[fullTextPredicates.size()]))); } q.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); TypedQuery<MessageTemplateDto> typedQuery = em.createQuery(q); List<MessageTemplateDto> messageTemplatesDto = typedQuery.getResultList(); for (MessageTemplateDto mt : messageTemplatesDto) { messageTemplates.add(mt.toMessageTemplate(userDirectoryService)); } return messageTemplates; } finally { if (em != null) em.close(); } }
From source file:org.seedstack.i18n.rest.internal.KeyJpaFinder.java
@Override protected List<KeyRepresentation> computeResultList(Range range, Map<String, Object> criteria) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Key> q = cb.createQuery(Key.class); Root<Key> k = q.from(Key.class); Predicate[] predicates = getPredicates(criteria, q, cb, k); q.select(k);//from w w w. jav a2s .c o m if (predicates.length > 0) { q.where(cb.and(predicates)); } List<Key> keys; // Get all the keys with their default translation if (range != null) { keys = entityManager.createQuery(q).setFirstResult((int) range.getOffset()) .setMaxResults((int) range.getSize()).getResultList(); } else { keys = entityManager.createQuery(q).getResultList(); } return assembleRepresentationsFromEntities(keys); }
From source file:org.seedstack.i18n.rest.internal.KeyJpaFinder.java
@Override protected long computeFullRequestSize(Map<String, Object> criteria) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> q = cb.createQuery(Long.class); Root<Key> k = q.from(Key.class); q.select(cb.count(k));/*w w w.j ava 2 s. com*/ if (criteria != null) { q.where(cb.and(getPredicates(criteria, q, cb, k))); } return entityManager.createQuery(q).getSingleResult(); }
From source file:org.seedstack.i18n.rest.internal.TranslationJpaFinder.java
@Override protected List<TranslationRepresentation> computeResultList(Range range, Map<String, Object> criteria) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Key> q = cb.createQuery(Key.class); Root<Key> k = q.from(Key.class); Predicate[] predicates = getPredicates(criteria, q, cb, k); q.select(k);/*ww w . j a v a 2 s .c o m*/ if (predicates.length > 0) { q.where(cb.and(predicates)); } List<Key> keys; // Get all the keys with their default translation if (range != null) { keys = entityManager.createQuery(q).setFirstResult((int) range.getOffset()) .setMaxResults((int) range.getSize()).getResultList(); } else { keys = entityManager.createQuery(q).getResultList(); } List<TranslationRepresentation> translationRepresentations = new ArrayList<TranslationRepresentation>( keys.size()); String defaultLocale = localeService.getDefaultLocale(); for (Key key : keys) { translationRepresentations.add(translationAssembler .assembleDtoFromAggregate(key.subKey(defaultLocale, (String) criteria.get(LOCALE)))); } return translationRepresentations; }
From source file:org.xiaoqiaotq.util.persistence.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//from w ww . ja va 2 s. co 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]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case NEQ: 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(); } }; }