use of org.motechproject.mots.domain.Community in project mots by motech-implementations.
the class LocationController method saveCommunity.
/**
* Update Community.
* @param id id of Community to update
* @param communityCreationDto DTO of Community to be updated
* @return updated Community
*/
@RequestMapping(value = "/community/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public CommunityCreationDto saveCommunity(@PathVariable("id") UUID id, @RequestBody @Valid CommunityCreationDto communityCreationDto, BindingResult bindingResult) {
checkBindingResult(bindingResult);
Community community = locationService.getCommunity(id);
locationMapper.updateCommunityFromDto(communityCreationDto, community);
return locationMapper.toCommunityCreationDto(locationService.saveCommunity(community));
}
use of org.motechproject.mots.domain.Community in project mots by motech-implementations.
the class CommunityDataBuilder method buildAsNew.
/**
* Builds instance of {@link Community} without id.
*/
public Community buildAsNew() {
Community community = new Community();
community.setName(name);
community.setFacility(facility);
return community;
}
use of org.motechproject.mots.domain.Community in project mots by motech-implementations.
the class CommunityDataBuilder method build.
/**
* Builds instance of {@link Community}.
*/
public Community build() {
Community community = buildAsNew();
community.setId(id);
return community;
}
use of org.motechproject.mots.domain.Community in project mots by motech-implementations.
the class CommunityRepositoryImpl method prepareQuery.
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String communityName, String parentFacility, String chiefdomName, String districtName, boolean count, Pageable pageable) throws IllegalArgumentException {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Root<Community> root = query.from(Community.class);
if (count) {
CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
}
Predicate predicate = builder.conjunction();
if (communityName != null) {
predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + communityName + '%'));
}
if (parentFacility != null) {
predicate = builder.and(predicate, builder.like(root.get(FACILITY).get(NAME), '%' + parentFacility + '%'));
}
if (chiefdomName != null) {
predicate = builder.and(predicate, builder.like(root.get(FACILITY).get(CHIEFDOM).get(NAME), '%' + chiefdomName + '%'));
}
if (districtName != null) {
predicate = builder.and(predicate, builder.like(root.get(FACILITY).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.Community 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