Search in sources :

Example 1 with EducationLevel

use of org.motechproject.mots.domain.enums.EducationLevel in project mots by motech-implementations.

the class CommunityHealthWorkerRepositoryImpl method prepareQuery.

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String chwId, String firstName, String secondName, String otherName, String phoneNumber, String educationLevel, String communityName, String facilityName, String chiefdomName, String districtName, String phuSupervisor, Boolean selected, boolean count, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<CommunityHealthWorker> root = query.from(CommunityHealthWorker.class);
    if (count) {
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }
    Predicate predicate = builder.conjunction();
    if (chwId != null) {
        predicate = builder.and(predicate, builder.like(root.get(CHW_ID), '%' + chwId + '%'));
    }
    if (firstName != null) {
        predicate = builder.and(predicate, builder.like(root.get(FIRST_NAME), '%' + firstName + '%'));
    }
    if (secondName != null) {
        predicate = builder.and(predicate, builder.like(root.get(SECOND_NAME), '%' + secondName + '%'));
    }
    if (otherName != null) {
        predicate = builder.and(predicate, builder.like(root.get(OTHER_NAME), '%' + otherName + '%'));
    }
    if (phoneNumber != null) {
        predicate = builder.and(predicate, builder.like(root.get(PHONE_NUMBER), '%' + phoneNumber + '%'));
    }
    if (educationLevel != null) {
        EducationLevel validLevel = EducationLevel.valueOf(educationLevel.toUpperCase());
        predicate = builder.and(predicate, builder.equal(root.get(EDUCATION_LEVEL), validLevel));
    }
    if (communityName != null) {
        predicate = builder.and(predicate, builder.like(root.get(COMMUNITY).get(NAME), '%' + communityName + '%'));
    }
    if (facilityName != null) {
        predicate = builder.and(predicate, builder.like(root.get(COMMUNITY).get(FACILITY).get(NAME), '%' + facilityName + '%'));
    }
    if (chiefdomName != null) {
        predicate = builder.and(predicate, builder.like(root.get(COMMUNITY).get(FACILITY).get(CHIEFDOM).get(NAME), '%' + chiefdomName + '%'));
    }
    if (districtName != null) {
        predicate = builder.and(predicate, builder.like(root.get(COMMUNITY).get(FACILITY).get(CHIEFDOM).get(DISTRICT).get(NAME), '%' + districtName + '%'));
    }
    if (phuSupervisor != null) {
        Predicate localPredicate = builder.like(root.get(COMMUNITY).get(FACILITY).get(INCHARGE).get(FIRST_NAME), '%' + phuSupervisor + '%');
        localPredicate = builder.or(localPredicate, builder.like(root.get(COMMUNITY).get(FACILITY).get(INCHARGE).get(SECOND_NAME), '%' + phuSupervisor + '%'));
        localPredicate = builder.or(localPredicate, builder.like(root.get(COMMUNITY).get(FACILITY).get(INCHARGE).get(OTHER_NAME), '%' + phuSupervisor + '%'));
        predicate = builder.and(predicate, localPredicate);
    }
    if (selected != null) {
        predicate = builder.and(predicate, builder.equal(root.get(SELECTED), selected));
    }
    query.where(predicate);
    if (!count && pageable != null && pageable.getSort() != null) {
        query = addSortProperties(query, root, pageable);
    }
    return query;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) EducationLevel(org.motechproject.mots.domain.enums.EducationLevel) Predicate(javax.persistence.criteria.Predicate)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)1 EducationLevel (org.motechproject.mots.domain.enums.EducationLevel)1