List of usage examples for javax.persistence.criteria CriteriaBuilder locate
Expression<Integer> locate(Expression<String> x, String pattern);
From source file:org.niord.core.message.MessageTagService.java
/** * Searches for message tags matching the given search parameters * * @param params the search parameters/*from w w w. j a va2 s .co m*/ * @return the search result */ @SuppressWarnings("ResultOfMethodCallIgnored") public List<MessageTag> searchMessageTags(MessageTagSearchParams params) { User user = userService.currentUser(); Domain domain = domainService.currentDomain(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<MessageTag> query = cb.createQuery(MessageTag.class); Root<MessageTag> tagRoot = query.from(MessageTag.class); // Build the predicate CriteriaHelper<MessageTag> criteriaHelper = new CriteriaHelper<>(cb, query); // Name filtering criteriaHelper.like(tagRoot.get("name"), params.getName()); // Locked filtering criteriaHelper.equals(tagRoot.get("locked"), params.getLocked()); // Type filtering Set<MessageTagType> types = params.getTypes() != null ? params.getTypes() : new HashSet<>(); if (types.isEmpty()) { types.add(PUBLIC); types.add(DOMAIN); types.add(PRIVATE); } List<Predicate> typePredicates = new LinkedList<>(); if (types.contains(PUBLIC)) { typePredicates.add(cb.equal(tagRoot.get("type"), MessageTagType.PUBLIC)); } if (types.contains(DOMAIN) && domain != null) { Join<MessageTag, Domain> domains = tagRoot.join("domain", JoinType.LEFT); typePredicates.add(cb.and(cb.equal(tagRoot.get("type"), MessageTagType.DOMAIN), cb.equal(domains.get("id"), domain.getId()))); } if (types.contains(PRIVATE) && user != null) { Join<MessageTag, User> users = tagRoot.join("user", JoinType.LEFT); typePredicates.add(cb.and(cb.equal(tagRoot.get("type"), MessageTagType.PRIVATE), cb.equal(users.get("id"), user.getId()))); } if (types.contains(TEMP)) { typePredicates.add(cb.equal(tagRoot.get("type"), MessageTagType.TEMP)); } criteriaHelper.add(cb.or(typePredicates.toArray(new Predicate[typePredicates.size()]))); // Compute the sorting List<Order> sortOrders = new ArrayList<>(); Order nameAscSortOrder = cb.asc(cb.lower(tagRoot.get("name"))); if (params.sortByType()) { Expression sortBy = tagRoot.get("type"); sortOrders.add(params.getSortOrder() == DESC ? cb.desc(sortBy) : cb.asc(sortBy)); sortOrders.add(nameAscSortOrder); } else if (params.sortByCreated()) { Expression sortBy = tagRoot.get("created"); sortOrders.add(params.getSortOrder() == DESC ? cb.desc(sortBy) : cb.asc(sortBy)); sortOrders.add(nameAscSortOrder); } else if (params.sortByExpiryDate()) { Expression sortBy = tagRoot.get("expiryDate"); sortOrders.add(params.getSortOrder() == DESC ? cb.desc(sortBy) : cb.asc(sortBy)); sortOrders.add(nameAscSortOrder); } else if (params.sortByMessageCount()) { Expression sortBy = tagRoot.get("messageCount"); sortOrders.add(params.getSortOrder() == DESC ? cb.desc(sortBy) : cb.asc(sortBy)); sortOrders.add(nameAscSortOrder); } else { if (StringUtils.isNotBlank(params.getName())) { sortOrders.add(cb.asc(cb.locate(cb.lower(tagRoot.get("name")), params.getName().toLowerCase()))); } String name = StringUtils.defaultIfBlank(params.getName(), ""); Expression sortBy = cb.lower(tagRoot.get("name")); sortOrders.add(params.getSortOrder() == DESC ? cb.desc(sortBy) : cb.asc(sortBy)); } // Complete the query query.select(tagRoot).distinct(true).where(criteriaHelper.where()).orderBy(sortOrders); // Execute the query and update the search result return em.createQuery(query).setMaxResults(params.getMaxSize()).getResultList(); }