Search in sources :

Example 6 with Incharge

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

the class InchargeService method processInchageCsv.

/**
 *.
 * Processes CSV file which contains Incharge list and returns list of errors
 * @param inchargeCsvFile CSV file with Incharge list
 * @return map with row numbers as keys and errors as values.
 * @throws IOException in case of file issues
 */
@SuppressWarnings("PMD.CyclomaticComplexity")
@PreAuthorize(RoleNames.HAS_UPLOAD_CSV_ROLE)
public Map<Integer, String> processInchageCsv(MultipartFile inchargeCsvFile, Boolean selected) throws IOException {
    ICsvMapReader csvMapReader;
    csvMapReader = new CsvMapReader(new InputStreamReader(inchargeCsvFile.getInputStream()), CsvPreference.STANDARD_PREFERENCE);
    final String[] header = csvMapReader.getHeader(true);
    final CellProcessor[] processors = getProcessors();
    Map<String, Object> csvRow;
    Set<String> phoneNumberSet = new HashSet<>();
    Map<Integer, String> errorMap = new HashMap<>();
    while ((csvRow = csvMapReader.read(header, processors)) != null) {
        LOGGER.debug(String.format("lineNo=%s, rowNo=%s, chw=%s", csvMapReader.getLineNumber(), csvMapReader.getRowNumber(), csvRow));
        String facilityId = Objects.toString(csvRow.get("FACILITY_ID"), null);
        Optional<Facility> existingFacility = facilityRepository.findByFacilityId(facilityId);
        if (!existingFacility.isPresent()) {
            errorMap.put(csvMapReader.getLineNumber(), "Facility with this Facility Id does not exist");
            continue;
        }
        String phoneNumber = Objects.toString(csvRow.get("PHU in-charge number"), null);
        if (phoneNumberSet.contains(phoneNumber)) {
            errorMap.put(csvMapReader.getLineNumber(), "Phone number is duplicated in CSV");
            continue;
        }
        if (phoneNumber != null) {
            phoneNumberSet.add(phoneNumber);
        }
        String name = Objects.toString(csvRow.get("PHU in-charge name"), null);
        if (StringUtils.isBlank(name)) {
            errorMap.put(csvMapReader.getLineNumber(), "Incharge name is empty");
            continue;
        }
        String[] nameParts = name.split("([. ])");
        List<String> names = Arrays.stream(nameParts).map(part -> part.length() == 1 ? part + "." : part).collect(Collectors.toList());
        if (names.size() < MIN_NAME_PARTS_NUMBER) {
            errorMap.put(csvMapReader.getLineNumber(), "Incharge second name is empty");
            continue;
        }
        Facility facility = existingFacility.get();
        Optional<Incharge> existingIncharge = inchargeRepository.findByFacilityId(facility.getId());
        String otherName = null;
        if (names.size() > MIN_NAME_PARTS_NUMBER) {
            otherName = StringUtils.join(names.subList(1, names.size() - 1), " ");
        }
        String firstName = names.get(0);
        String secondName = names.get(names.size() - 1);
        if (existingIncharge.isPresent()) {
            Incharge incharge = existingIncharge.get();
            incharge.setFirstName(firstName);
            incharge.setSecondName(secondName);
            incharge.setOtherName(otherName);
            incharge.setPhoneNumber(phoneNumber);
            if (selected) {
                incharge.setSelected(true);
            }
            inchargeRepository.save(incharge);
            continue;
        }
        inchargeRepository.save(new Incharge(firstName, secondName, otherName, phoneNumber, null, facility, selected));
    }
    return errorMap;
}
Also used : ICsvMapReader(org.supercsv.io.ICsvMapReader) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) RoleNames(org.motechproject.mots.domain.security.UserPermission.RoleNames) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) Facility(org.motechproject.mots.domain.Facility) CsvPreference(org.supercsv.prefs.CsvPreference) FacilityRepository(org.motechproject.mots.repository.FacilityRepository) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) InchargeRepository(org.motechproject.mots.repository.InchargeRepository) Incharge(org.motechproject.mots.domain.Incharge) Service(org.springframework.stereotype.Service) Map(java.util.Map) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Pageable(org.springframework.data.domain.Pageable) Set(java.util.Set) IOException(java.io.IOException) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) CsvMapReader(org.supercsv.io.CsvMapReader) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) Incharge(org.motechproject.mots.domain.Incharge) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) Facility(org.motechproject.mots.domain.Facility) ICsvMapReader(org.supercsv.io.ICsvMapReader) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with Incharge

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

the class InchargeRepositoryIntegrationTest method shouldFindInchargeBySurName.

@Test
public void shouldFindInchargeBySurName() {
    // when
    Page<Incharge> result = inchargeRepository.searchIncharges(null, incharge1.getSecondName(), null, null, null, null, false, null);
    // then
    assertThat(result.getTotalElements(), is(1L));
    Incharge foundIncharge = result.getContent().get(0);
    assertThat(foundIncharge.getSecondName(), is(incharge1.getSecondName()));
}
Also used : Incharge(org.motechproject.mots.domain.Incharge) Test(org.junit.Test)

Example 8 with Incharge

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

the class InchargeDataBuilder method buildAsNew.

/**
 * Builds instance of {@link Incharge} without id.
 */
public Incharge buildAsNew() {
    Incharge incharge = new Incharge();
    incharge.setFirstName(firstName);
    incharge.setSecondName(secondName);
    incharge.setPhoneNumber(phoneNumber);
    incharge.setFacility(facility);
    return incharge;
}
Also used : Incharge(org.motechproject.mots.domain.Incharge)

Example 9 with Incharge

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

the class InchargeDtoFacilityUniquenessValidator method isValid.

@Override
public boolean isValid(InchargeDto inchargeDto, ConstraintValidatorContext context) {
    if (StringUtils.isNotEmpty(inchargeDto.getFacilityId()) && ValidationUtils.isValidUuidString(inchargeDto.getFacilityId())) {
        UUID facilityId = UUID.fromString(inchargeDto.getFacilityId());
        Optional<Incharge> existingIncharge = inchargeRepository.findByFacilityId(facilityId);
        if (existingIncharge.isPresent() && !StringUtils.equals(inchargeDto.getId(), existingIncharge.get().getId().toString())) {
            context.disableDefaultConstraintViolation();
            ValidationUtils.addDefaultViolationMessageToInnerField(context, FACILITY_ID);
            return false;
        }
    }
    return true;
}
Also used : Incharge(org.motechproject.mots.domain.Incharge) UUID(java.util.UUID)

Example 10 with Incharge

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

the class InchargeController method selectIncharge.

/**
 * Select Incharge.
 * @param id id of Incharge to select
 * @param inchargeDto DTO of Incharge to be selected
 * @return selected Incharge
 */
@RequestMapping(value = "/incharge/{id}/select", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public InchargeDto selectIncharge(@PathVariable("id") UUID id, @RequestBody @Valid InchargeDto inchargeDto, BindingResult bindingResult) {
    checkBindingResult(bindingResult);
    Incharge existingIncharge = inchargeService.getIncharge(id);
    if (existingIncharge.getSelected()) {
        throw new IllegalArgumentException("This incharge is already selected");
    }
    inchargeMapper.updateFromDto(inchargeDto, existingIncharge);
    existingIncharge.setSelected(true);
    return inchargeMapper.toDto(inchargeService.saveIncharge(existingIncharge));
}
Also used : Incharge(org.motechproject.mots.domain.Incharge) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Incharge (org.motechproject.mots.domain.Incharge)10 UUID (java.util.UUID)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Test (org.junit.Test)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 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1