Search in sources :

Example 11 with Facility

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

the class FacilityUniquenessValidator method isValid.

@Override
public boolean isValid(FacilityCreationDto facilityCreationDto, ConstraintValidatorContext context) {
    if (StringUtils.isNotEmpty(facilityCreationDto.getChiefdomId()) && ValidationUtils.isValidUuidString(facilityCreationDto.getChiefdomId()) && StringUtils.isNotEmpty(facilityCreationDto.getName())) {
        String name = facilityCreationDto.getName();
        UUID chiefdomId = UUID.fromString(facilityCreationDto.getChiefdomId());
        Chiefdom chiefdom = chiefdomRepository.findOne(chiefdomId);
        Optional<Facility> existing = facilityRepository.findByNameAndChiefdom(name, chiefdom);
        if (// when edit facility allows change
        existing.isPresent() && !existing.get().getId().toString().equals(facilityCreationDto.getId())) {
            String message = String.format(ValidationMessages.NOT_UNIQUE_FACILITY, existing.get().getName());
            context.disableDefaultConstraintViolation();
            ValidationUtils.addDefaultViolationMessageToInnerField(context, NAME, message);
            return false;
        }
    }
    return true;
}
Also used : Facility(org.motechproject.mots.domain.Facility) UUID(java.util.UUID) Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 12 with Facility

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

the class FacilityRepositoryImpl method prepareQuery.

private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String facilityId, String facilityName, String facilityType, String inchargeFullName, String parentChiefdom, String districtName, boolean count, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    Root<Facility> root = query.from(Facility.class);
    if (count) {
        CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
        query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
    }
    Predicate predicate = builder.conjunction();
    if (facilityId != null) {
        predicate = builder.and(predicate, builder.like(root.get(FACILITY_ID), '%' + facilityId + '%'));
    }
    if (facilityName != null) {
        predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + facilityName + '%'));
    }
    if (facilityType != null) {
        FacilityType validFacilityType = FacilityType.valueOf(facilityType.toUpperCase());
        predicate = builder.and(predicate, builder.equal(root.get(FACILITY_TYPE), validFacilityType));
    }
    if (inchargeFullName != null) {
        predicate = builder.and(predicate, builder.like(root.get(INCHARGE).get(FIRST_NAME), '%' + inchargeFullName + '%'));
        predicate = builder.or(predicate, builder.like(root.get(INCHARGE).get(SECOND_NAME), '%' + inchargeFullName + '%'));
        predicate = builder.or(predicate, builder.like(root.get(INCHARGE).get(OTHER_NAME), '%' + inchargeFullName + '%'));
    }
    if (parentChiefdom != null) {
        predicate = builder.and(predicate, builder.like(root.get(CHIEFDOM).get(NAME), '%' + parentChiefdom + '%'));
    }
    if (districtName != null) {
        predicate = builder.and(predicate, builder.like(root.get(CHIEFDOM).get(DISTRICT).get(NAME), '%' + districtName + '%'));
    }
    query.where(predicate);
    if (!count && pageable != null && pageable.getSort() != null) {
        query = addSortProperties(query, root, pageable);
    }
    return query;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Facility(org.motechproject.mots.domain.Facility) FacilityType(org.motechproject.mots.domain.enums.FacilityType) Predicate(javax.persistence.criteria.Predicate)

Example 13 with Facility

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

the class FacilityRepositoryImpl method search.

/**
 * Finds Facilities matching all of the provided parameters.
 * If there are no parameters, return all Facilities.
 */
@Override
public Page<Facility> search(String facilityId, String facilityName, String facilityType, String inchargeFullName, String parentChiefdom, String districtName, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Facility> query = builder.createQuery(Facility.class);
    query = prepareQuery(query, facilityId, facilityName, facilityType, inchargeFullName, parentChiefdom, districtName, false, pageable);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery = prepareQuery(countQuery, facilityId, facilityName, facilityType, inchargeFullName, parentChiefdom, districtName, true, pageable);
    Long count = entityManager.createQuery(countQuery).getSingleResult();
    int pageSize = getPageSize(pageable);
    int firstResult = getFirstResult(pageable, pageSize);
    List<Facility> incharges = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
    return new PageImpl<>(incharges, pageable, count);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) Facility(org.motechproject.mots.domain.Facility)

Example 14 with Facility

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

the class CommunityUniquenessValidator method isValid.

@Override
public boolean isValid(CommunityCreationDto communityCreationDto, ConstraintValidatorContext context) {
    if (StringUtils.isNotEmpty(communityCreationDto.getFacilityId()) && ValidationUtils.isValidUuidString(communityCreationDto.getFacilityId()) && StringUtils.isNotEmpty(communityCreationDto.getName())) {
        String name = communityCreationDto.getName();
        UUID facilityId = UUID.fromString(communityCreationDto.getFacilityId());
        Facility facility = facilityRepository.findOne(facilityId);
        Optional<Community> existing = communityRepository.findByNameAndFacility(name, facility);
        if (// when edit community allows change
        existing.isPresent() && !existing.get().getId().toString().equals(communityCreationDto.getId())) {
            String message = String.format(ValidationMessages.NOT_UNIQUE_COMMUNITY, existing.get().getName());
            context.disableDefaultConstraintViolation();
            ValidationUtils.addDefaultViolationMessageToInnerField(context, NAME, message);
            return false;
        }
    }
    return true;
}
Also used : Facility(org.motechproject.mots.domain.Facility) UUID(java.util.UUID) Community(org.motechproject.mots.domain.Community)

Aggregations

Facility (org.motechproject.mots.domain.Facility)14 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3 Iterator (java.util.Iterator)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 Test (org.junit.Test)2 Chiefdom (org.motechproject.mots.domain.Chiefdom)2 Community (org.motechproject.mots.domain.Community)2 FacilityType (org.motechproject.mots.domain.enums.FacilityType)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1