private CriteriaQuery<SearchRedirect> buildFindSearchRedirectBySearchTermCriteria(String searchTerm) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<SearchRedirect> criteria = builder.createQuery(SearchRedirect.class); Root<SearchRedirectImpl> redirect = criteria.from(SearchRedirectImpl.class); List<Predicate> restrictions = new ArrayList<>(); restrictions.add(builder.equal(builder.upper(redirect.<String>get("searchTerm")), searchTerm.toUpperCase())); // Add the active start/end date restrictions Date currentDate = getCurrentDateAfterFactoringInDateResolution(); if (isNullActiveStartDateActive) { restrictions.add(builder.or(builder.isNull(redirect.get("activeStartDate")), builder.lessThanOrEqualTo(redirect.get("activeStartDate").as(Date.class), currentDate))); } else { restrictions.add(builder.and(builder.isNotNull(redirect.get("activeStartDate")), builder.lessThanOrEqualTo(redirect.get("activeStartDate").as(Date.class), currentDate))); } restrictions.add(builder.or(builder.isNull(redirect.get("activeEndDate")), builder.greaterThan(redirect.get("activeEndDate").as(Date.class), currentDate))); // Add the restrictions to the criteria query criteria.select(redirect); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); return criteria.orderBy(builder.asc(redirect.get("searchPriority"))); }
/** * Applies an {@code UPPERCASE} conversion to the given {@link Expression} in case the underlying {@link Part} * requires ignoring case. * * @param expression must not be {@literal null}. * @return */ private <T> Expression<T> upperIfIgnoreCase(Expression<? extends T> expression) { switch (part.shouldIgnoreCase()) { case ALWAYS: Assert.state(canUpperCase(expression), "Unable to ignore case of " + expression.getJavaType().getName() + " types, the property '" + part.getProperty().getSegment() + "' must reference a String"); return (Expression<T>) builder.upper((Expression<String>) expression); case WHEN_POSSIBLE: if (canUpperCase(expression)) { return (Expression<T>) builder.upper((Expression<String>) expression); } case NEVER: default: return (Expression<T>) expression; } }
@Override public NotificationRegistrationStatusEntity getNotificationRegistrationStatus(String code) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<NotificationRegistrationStatusEntity> criteriaQuery = criteriaBuilder.createQuery(NotificationRegistrationStatusEntity.class); Root<NotificationRegistrationStatusEntity> notificationRegistrationStatus = criteriaQuery.from(NotificationRegistrationStatusEntity.class); return executeSingleResultQuery(criteriaQuery.select(notificationRegistrationStatus).where(criteriaBuilder.equal(criteriaBuilder.upper( notificationRegistrationStatus.get(NotificationRegistrationStatusEntity_.code)), code.toUpperCase())), "More than 1 notification registration status was found with code \"" + code + "\""); } }
@Override public NotificationRegistrationStatusEntity getNotificationRegistrationStatus(String code) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<NotificationRegistrationStatusEntity> criteriaQuery = criteriaBuilder.createQuery(NotificationRegistrationStatusEntity.class); Root<NotificationRegistrationStatusEntity> notificationRegistrationStatus = criteriaQuery.from(NotificationRegistrationStatusEntity.class); return executeSingleResultQuery(criteriaQuery.select(notificationRegistrationStatus).where(criteriaBuilder.equal(criteriaBuilder.upper( notificationRegistrationStatus.get(NotificationRegistrationStatusEntity_.code)), code.toUpperCase())), "More than 1 notification registration status was found with code \"" + code + "\""); } }
@Override public NamespaceEntity getNamespaceByCd(String namespaceCode) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<NamespaceEntity> criteria = builder.createQuery(NamespaceEntity.class); // The criteria root is the namespace. Root<NamespaceEntity> namespaceEntity = criteria.from(NamespaceEntity.class); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(namespaceEntity.get(NamespaceEntity_.code)), namespaceCode.toUpperCase()); criteria.select(namespaceEntity).where(queryRestriction); return executeSingleResultQuery(criteria, String.format("Found more than one namespace with namespaceCode=\"%s\".", namespaceCode)); }
@Override public NotificationRegistrationEntity getNotificationRegistration(String namespace, String name) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<NotificationRegistrationEntity> criteriaQuery = criteriaBuilder.createQuery(NotificationRegistrationEntity.class); Root<NotificationRegistrationEntity> notificationRegistration = criteriaQuery.from(NotificationRegistrationEntity.class); Path<NamespaceEntity> namespacePath = notificationRegistration.get(NotificationRegistrationEntity_.namespace); return executeSingleResultQuery(criteriaQuery.select(notificationRegistration).where(criteriaBuilder.equal(criteriaBuilder.upper(namespacePath.get( NamespaceEntity_.code)), namespace.toUpperCase()), criteriaBuilder.equal(criteriaBuilder.upper(notificationRegistration.get( NotificationRegistrationEntity_.name)), name.toUpperCase())), "More than 1 notification registration was found with namespace \"" + namespace + "\" and name \"" + name + "\""); } }
@Override public DataProviderEntity getDataProviderByName(String dataProviderName) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<DataProviderEntity> criteria = builder.createQuery(DataProviderEntity.class); // The criteria root is the data provider. Root<DataProviderEntity> dataProviderEntity = criteria.from(DataProviderEntity.class); // Create the standard restrictions (i.e. the standard where clauses). Predicate queryRestriction = builder.equal(builder.upper(dataProviderEntity.get(DataProviderEntity_.name)), dataProviderName.toUpperCase()); criteria.select(dataProviderEntity).where(queryRestriction); return executeSingleResultQuery(criteria, String.format("Found more than one data provider with name=\"%s\".", dataProviderName)); }
/** * {@inheritDoc} */ @Override protected void addEqualToIgnoreCase(TranslationContext criteria, String propertyPath, String value) { criteria.addPredicate(criteria.builder.equal(criteria.builder.upper(criteria.attr(propertyPath)), value.toUpperCase())); }
CriteriaBuilder critBuilder = em.getCriteriaBuilder(); CriteriaQuery<CtfLibrary> critQ = critBuilder.createQuery(Users.class); Root<CtfLibrary> root = critQ.from(Users.class); Expression<String> path = root.get("lastName"); Expression<String> upper =critBuilder.upper(path); Predicate ctfPredicate = critBuilder.like(upper,"%stringToFind%") critQ.where(critBuilder.and(ctfPredicate)); em.createQuery(critQ.select(root)).getResultList();
@Override public List<Predicate> build(CriteriaBuilder builder, Path<E> path) { return Arrays.asList(builder.equal(builder.upper(path.<String>get(getAtt())), getValue().toUpperCase())); }
@Override public <R> Selection<String> toSelection(CriteriaQuery<R> query, CriteriaBuilder builder, Path<? extends P> path) { return builder.upper(path.get(getAttribute())); }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { if (path(root).getJavaType().equals(String.class)) { return cb.equal(cb.upper(this.<String>path(root)), expectedValue.toUpperCase()); } return super.toPredicate(root, query, cb); } }
/** * {@inheritDoc} */ @Override protected void addNotEqualToIgnoreCase(TranslationContext criteria, String propertyPath, String value) { criteria.addPredicate(criteria.builder.notEqual(criteria.builder.upper(criteria.attr(propertyPath)), value.toUpperCase())); }
private Predicate getOutPredicate(final List<Object> transformedValues, final Path<Object> fieldPath) { final List<String> outParams = new ArrayList<>(); for (final Object param : transformedValues) { if (param instanceof String) { outParams.add(((String) param).toUpperCase()); } } if (!outParams.isEmpty()) { return cb.not(cb.upper(pathOfString(fieldPath)).in(outParams)); } else { return cb.not(fieldPath.in(transformedValues)); } }
@Override public List<Predicate> build(CriteriaBuilder builder, Path<E> path) { return Arrays.asList(builder.notEqual(builder.upper(path.<String>get(getAtt())), getValue().toUpperCase())); }
/** * {@inheritDoc} */ @Override protected void addNotLikeIgnoreCase(TranslationContext criteria, String propertyPath, String value) { criteria.addPredicate(criteria.builder.notLike(criteria.builder.upper(criteria.attr(propertyPath)), fixSearchPattern(value.toUpperCase()))); }
/** * {@inheritDoc} */ @Override protected void addLikeIgnoreCase(TranslationContext criteria, String propertyPath, String value){ criteria.addPredicate(criteria.builder.like(criteria.builder.upper(criteria.attr(propertyPath)), fixSearchPattern(value.toUpperCase()))); }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { return path(root).getJavaType().equals(String.class) ? cb.notEqual(cb.upper(this.<String>path(root)), expectedValue.toUpperCase()) : super.toPredicate(root, query, cb); }
@Override public List<Predicate> build(CriteriaBuilder builder, Path<E> path) { return Arrays.asList(builder.like( caseInsensitive ? builder.upper(path.<String>get(getAtt())) : path.get(getAtt()), caseInsensitive ? getValue().toUpperCase() : getValue())); }
@Override public List<Predicate> build(CriteriaBuilder builder, Path<E> path) { return Arrays.asList(builder.notLike( caseInsensitive ? builder.upper(path.<String>get(getAtt())) : path.get(getAtt()), caseInsensitive ? getValue().toUpperCase() : getValue())); }