List of usage examples for javax.persistence.criteria CriteriaBuilder lower
Expression<String> lower(Expression<String> x);
From source file:org.kisoonlineapp.uc.veranstaltung.SearchPredicatesForVeranstaltungFilterForm.java
List<Predicate> createSearchPredicates(VeranstaltungFilterForm veranstaltungFilterForm, Root<Veranstaltung> root) { Veranstaltung example = veranstaltungFilterForm.getExample(); CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); List<Predicate> predicatesList = new ArrayList<Predicate>(); {// ww w .j a v a 2 s .c o m final String code = example.getCode(); if (isNotEmpty(code)) { predicatesList.add(builder.like(builder.lower(root.<String>get("code")), lowerWildcards(code))); } } { final String name = example.getName(); if (isNotEmpty(name)) { predicatesList.add(builder.like(builder.lower(root.<String>get("name")), lowerWildcards(name))); } } { final Veranstalter veranstalter = example.getVeranstalter(); if (veranstalter != null) { predicatesList.add(builder.equal(root.get("veranstalter"), veranstalter)); } } { final String beschreibung = example.getBeschreibung(); if (isNotEmpty(beschreibung)) { predicatesList.add(builder.like(builder.lower(root.<String>get("beschreibung")), lowerWildcards(beschreibung))); } } { final Gemeinde gemeinde = example.getGemeinde(); if (gemeinde != null) { final Path<Gemeinde> gemeindePath = root.<Gemeinde>get("gemeinde"); { { final String gemeindeName = gemeinde.getName(); if (isNotEmpty(gemeindeName)) { predicatesList.add(builder.like(builder.lower(gemeindePath.<String>get("name")), lowerWildcards(gemeindeName))); } } { final String gemeindePlz = gemeinde.getPlz(); if (isNotEmpty(gemeindePlz)) { predicatesList.add(builder.like(builder.lower(gemeindePath.<String>get("plz")), lowerWildcards(gemeindePlz))); } } } } } return Collections.unmodifiableList(predicatesList); }
From source file:org.niord.core.message.MessageTagService.java
/** * Searches for message tags matching the given search parameters * * @param params the search parameters/* w w w . j av a 2s . c o 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(); }
From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java
protected List<Product> readFilteredActiveProductsByQueryInternal(String query, Date currentDate, ProductSearchCriteria searchCriteria) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); // The root of our search is Product since we are searching Root<ProductImpl> product = criteria.from(ProductImpl.class); // We also want to filter on attributes from sku and productAttributes Join<Product, Sku> sku = product.join("defaultSku"); // Product objects are what we want back criteria.select(product);/*from w w w. ja v a 2s . c o m*/ // We only want results that match the search query List<Predicate> restrictions = new ArrayList<Predicate>(); String lq = query.toLowerCase(); restrictions.add(builder.or(builder.like(builder.lower(sku.get("name").as(String.class)), '%' + lq + '%'), builder.like(builder.lower(sku.get("longDescription").as(String.class)), '%' + lq + '%'))); attachProductSearchCriteria(searchCriteria, product, sku, restrictions); attachActiveRestriction(currentDate, product, sku, restrictions); attachOrderBy(searchCriteria, product, sku, criteria); // Execute the query with the restrictions criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Product> typedQuery = em.createQuery(criteria); //don't cache - not really practical for open ended search return typedQuery.getResultList(); }