Search in sources :

Example 1 with Chiefdom

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

the class ChiefdomDataBuilder method build.

/**
 * Builds instance of {@link Chiefdom}.
 */
public Chiefdom build() {
    Chiefdom chiefdom = buildAsNew();
    chiefdom.setId(id);
    return chiefdom;
}
Also used : Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 2 with Chiefdom

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

the class LocationImporter method parseFacilities.

private void parseFacilities(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Facility> newFacilitySet = new HashSet<>();
    DataFormatter fmt = new DataFormatter();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(FACILITY_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(FACILITY_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        String facilityId = fmt.formatCellValue(row.getCell(ID_FACILITY_COL_NUMBER));
        FacilityType facilityType = FacilityType.getByDisplayName(row.getCell(NAME_FACILITY_COL_NUMBER).getStringCellValue());
        Facility facility = new Facility(cellText, facilityType, facilityId);
        String parentChiefdomName = row.getCell(CHIEFDOM_COL_NUMBER).getStringCellValue();
        String parentDistrictName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        Chiefdom parent = currentChiefdomList.stream().filter(chiefdom -> chiefdom.getName().equals(parentChiefdomName) && chiefdom.getDistrict().getName().equals(parentDistrictName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Facility parent " + "is not defined properly in spreadsheet", facility.getName())));
        facility.setChiefdom(parent);
        newFacilitySet.add(facility);
    }
    newFacilitySet.forEach(newFacility -> {
        if (!currentFacilityList.contains(newFacility)) {
            locationService.createImportedFacility(newFacility);
        }
    });
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Iterator(java.util.Iterator) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Facility(org.motechproject.mots.domain.Facility) FacilityType(org.motechproject.mots.domain.enums.FacilityType) HashSet(java.util.HashSet) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 3 with Chiefdom

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

the class ChiefdomDataBuilder method buildAsNew.

/**
 * Builds instance of {@link Chiefdom} without id.
 */
public Chiefdom buildAsNew() {
    Chiefdom chiefdom = new Chiefdom();
    chiefdom.setName(name);
    chiefdom.setDistrict(district);
    return chiefdom;
}
Also used : Chiefdom(org.motechproject.mots.domain.Chiefdom)

Example 4 with Chiefdom

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

the class ChiefdomRepositoryIntegrationTest method shouldFindChiefdomByName.

@Test
public void shouldFindChiefdomByName() {
    // when
    Page<Chiefdom> result = chiefdomRepository.search(chiefdom1.getName(), null, null);
    // then
    assertThat(result.getTotalElements(), is(1L));
    Chiefdom foundChiefdom = result.getContent().get(0);
    assertThat(foundChiefdom.getName(), is(chiefdom1.getName()));
}
Also used : Chiefdom(org.motechproject.mots.domain.Chiefdom) Test(org.junit.Test)

Example 5 with Chiefdom

use of org.motechproject.mots.domain.Chiefdom 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)

Aggregations

Chiefdom (org.motechproject.mots.domain.Chiefdom)8 HashSet (java.util.HashSet)2 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 Facility (org.motechproject.mots.domain.Facility)2 UUID (java.util.UUID)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 DataFormatter (org.apache.poi.ss.usermodel.DataFormatter)1 Test (org.junit.Test)1 District (org.motechproject.mots.domain.District)1 FacilityType (org.motechproject.mots.domain.enums.FacilityType)1 PageImpl (org.springframework.data.domain.PageImpl)1