Example usage for javax.persistence.criteria CriteriaBuilder concat

List of usage examples for javax.persistence.criteria CriteriaBuilder concat

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder concat.

Prototype

Expression<String> concat(String x, Expression<String> y);

Source Link

Document

Create an expression for string concatenation.

Usage

From source file:eu.uqasar.service.user.UserService.java

public User getByFullName(final String fullName) {
    logger.infof("loading User with fullName %s ...", fullName);
    User entity = null;/*from w  w  w  . j a  v a  2 s  .  co  m*/
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.equal(cb.lower(cb.concat(root.get(User_.firstName), root.get(User_.lastName))),
            fullName.toLowerCase()));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;

}

From source file:eu.uqasar.service.user.UserService.java

public User getByFullNameWithUsername(final String fullNameWithUsername) {
    logger.infof("loading User with fullName and username %s ...", fullNameWithUsername);
    User entity = null;//from  w w w.  j  av  a2  s .c om
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    Expression<String> fullName = cb.concat(cb.concat(root.get(User_.firstName), " "),
            root.get(User_.lastName));
    Expression<String> userName = cb.concat(" (", cb.concat(root.get(User_.userName), ")"));
    query.where(cb.equal(cb.lower(cb.concat(fullName, userName)), fullNameWithUsername.toLowerCase()));
    List<User> resultList = em.createQuery(query).setMaxResults(1).getResultList();
    if (!resultList.isEmpty()) {
        return resultList.get(0);
    }
    return entity;
}

From source file:utils.jpa.EntityResource.java

private Query createCommonQuery(CriteriaBuilder cb, CriteriaQuery cq, Root root, TableSearchQuery tb,
        List<String> fieldsSearchBy) {

    List<Predicate> orPredicates = new ArrayList<>();
    List<Predicate> andPredicates = new ArrayList<>();

    //setting where conditions      
    if (fieldsSearchBy != null && !fieldsSearchBy.isEmpty()) {
        for (String field : fieldsSearchBy) {
            if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field), "::text")),
                        "%" + tb.getFilter().toLowerCase() + "%"));
            }/*  www.  ja v  a 2s.  co  m*/
        }
    } else {
        List<Field> filterColumns = SearchFields.getAll(entityClass);
        for (Field field : filterColumns) {
            EntityFilter entityFilter = tb.getFilterParameters().get(field.getName());

            if ((Collection.class.isAssignableFrom(field.getType())
                    || List.class.isAssignableFrom(field.getType()))
                    && (entityFilter == null
                            || !entityFilter.getEntityFilterType().equals(EntityFilterType.EMPTY))) {
                continue;
            }

            if (entityFilter == null) {
                if (tb.getFilter() != null && !tb.getFilter().isEmpty()) {
                    orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                            "%" + tb.getFilter().toLowerCase() + "%"));
                }
                continue;
            }

            switch (entityFilter.getEntityFilterType()) {
            case WILDCARD:
                andPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")),
                        "%" + entityFilter.getValue().toLowerCase() + "%"));
                break;
            case EMPTY:
                if (Collection.class.isAssignableFrom(field.getType())
                        || List.class.isAssignableFrom(field.getType())) {
                    andPredicates.add(entityFilter.getValue().equals("hide")
                            ? cb.isEmpty(root.<Collection>get(field.getName()))
                            : cb.isNotEmpty(root.<Collection>get(field.getName())));
                } else {
                    andPredicates.add(
                            entityFilter.getValue().equals("hide") ? root.<Object>get(field.getName()).isNull()
                                    : root.<Object>get(field.getName()).isNotNull());
                }

                break;
            case MIN_MAX:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(0)))));
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValues().get(1)))));
                break;
            case MIN:
                andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            case MAX:
                andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()),
                        new Date(Long.valueOf(entityFilter.getValue()))));
                break;
            }

        }
    }
    if (orPredicates.size() > 0 && andPredicates.size() > 0) {
        cq.where(cb.or(cb.or(orPredicates.toArray(new Predicate[0])),
                cb.and(andPredicates.toArray(new Predicate[0]))));
    } else if (orPredicates.size() > 0) {
        cq.where(cb.or(orPredicates.toArray(new Predicate[0])));
    } else if (andPredicates.size() > 0) {
        cq.where(cb.and(andPredicates.toArray(new Predicate[0])));
    }

    return em.createQuery(cq);

}

From source file:eu.uqasar.service.user.UserService.java

private List<Predicate> getFilterPredicates(final UserFilterStructure filter, CriteriaBuilder cb,
        Root<User> from) {/*from ww w  .  j  a v  a  2s  . c  o m*/
    List<Predicate> predicates = new ArrayList<>();
    if (filter == null) {
        return predicates;
    }

    if (filter.getRole() != null) {
        predicates.add(cb.equal(from.get(User_.userRole), filter.getRole()));
    }
    if (filter.getSource() != null) {
        predicates.add(cb.equal(from.get(User_.source), filter.getSource()));
    }
    if (filter.getStatus() != null) {
        predicates.add(cb.equal(from.get(User_.registrationStatus), filter.getStatus()));
    }
    if (!StringUtils.isEmpty(filter.getName())) {
        Predicate firstName = cb.like(cb.lower(from.get(User_.lastName)),
                LIKE_WILDCARD + filter.getName().toLowerCase() + LIKE_WILDCARD);
        Predicate lastName = cb.like(from.get(User_.firstName),
                LIKE_WILDCARD + filter.getName() + LIKE_WILDCARD);
        Predicate userName = cb.like(from.get(User_.userName),
                LIKE_WILDCARD + filter.getName() + LIKE_WILDCARD);
        Expression<String> fullName = cb.concat(cb.concat(from.get(User_.firstName), " "),
                from.get(User_.lastName));
        Expression<String> un = cb.concat(" (", cb.concat(from.get(User_.userName), ")"));
        Predicate fn = cb.like(fullName, LIKE_WILDCARD + filter.getName() + LIKE_WILDCARD);
        Expression<String> fnu = cb.concat(fullName, un);
        Predicate ff1 = cb.like(fnu, LIKE_WILDCARD + filter.getName() + LIKE_WILDCARD);

        predicates.add(cb.or(firstName, lastName, userName, fn, ff1));

    }
    return predicates;
}