Search in sources :

Example 6 with Community

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));
}
Also used : Community(org.motechproject.mots.domain.Community) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with 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;
}
Also used : Community(org.motechproject.mots.domain.Community)

Example 8 with 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;
}
Also used : Community(org.motechproject.mots.domain.Community)

Example 9 with 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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Community(org.motechproject.mots.domain.Community) Predicate(javax.persistence.criteria.Predicate)

Example 10 with Community

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;
}
Also used : Facility(org.motechproject.mots.domain.Facility) UUID(java.util.UUID) Community(org.motechproject.mots.domain.Community)

Aggregations

Community (org.motechproject.mots.domain.Community)10 HashSet (java.util.HashSet)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Facility (org.motechproject.mots.domain.Facility)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 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 UUID (java.util.UUID)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)1 Test (org.junit.Test)1 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)1 PageImpl (org.springframework.data.domain.PageImpl)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 CellProcessor (org.supercsv.cellprocessor.ift.CellProcessor)1