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;
}
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;
}
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);
}
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;
}
Aggregations