Search in sources :

Example 1 with CommunityHealthWorker

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

the class CommunityHealthWorkerRepositoryIntegrationTest method shouldFindChw.

@Test
public void shouldFindChw() {
    // when
    Page<CommunityHealthWorker> result = repository.searchCommunityHealthWorkers(chw1.getChwId(), chw1.getFirstName(), chw1.getSecondName(), chw1.getOtherName(), chw1.getPhoneNumber(), chw1.getEducationLevel().toString(), community.getName(), facility.getName(), chiefdom.getName(), district.getName(), null, false, null);
    // then
    assertThat(result.getTotalElements(), is(1L));
    CommunityHealthWorker foundWorker = result.getContent().get(0);
    assertThat(foundWorker.getId(), is(chw1.getId()));
}
Also used : CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) Test(org.junit.Test)

Example 2 with CommunityHealthWorker

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

the class ModuleAssignmentServiceTest method assignModulesShouldThrowIfIvrIdIsNotSet.

@Test(expected = ModuleAssignmentException.class)
public void assignModulesShouldThrowIfIvrIdIsNotSet() {
    final CommunityHealthWorker chw = new CommunityHealthWorkerDataBuilder().withIvrId(null).build();
    final AssignedModules assignedModules = new AssignedModulesDataBuilder().withChw(chw).build();
    when(assignedModulesRepository.findByHealthWorkerId(eq(chw.getId()))).thenReturn(Optional.of(assignedModules));
    moduleAssignmentService.assignModules(assignedModules);
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) AssignedModulesDataBuilder(org.motechproject.mots.testbuilder.AssignedModulesDataBuilder) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) CommunityHealthWorkerDataBuilder(org.motechproject.mots.testbuilder.CommunityHealthWorkerDataBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with CommunityHealthWorker

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

the class CommunityHealthWorkerDataBuilder method buildAsNew.

/**
 * Builds instance of {@link CommunityHealthWorker} without id.
 */
public CommunityHealthWorker buildAsNew() {
    CommunityHealthWorker chw = new CommunityHealthWorker();
    chw.setChwId(chwId);
    chw.setFirstName(firstName);
    chw.setSecondName(secondName);
    chw.setGender(gender);
    chw.setPhoneNumber(phoneNumber);
    chw.setCommunity(community);
    chw.setPreferredLanguage(preferredLanguage);
    chw.setEducationLevel(educationLevel);
    chw.setIvrId(ivrId);
    return chw;
}
Also used : CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker)

Example 4 with CommunityHealthWorker

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

the class CommunityHealthWorkerRepositoryImpl method searchCommunityHealthWorkers.

/**
 * Finds CommunityHealthWorkers matching all of the provided parameters.
 * If there are no parameters, return all CommunityHealthWorkers.
 */
@Override
public Page<CommunityHealthWorker> searchCommunityHealthWorkers(String chwId, String firstName, String secondName, String otherName, String phoneNumber, String educationLevel, String communityName, String facilityName, String chiefdomName, String districtName, String phuSupervisor, Boolean selected, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CommunityHealthWorker> query = builder.createQuery(CommunityHealthWorker.class);
    query = prepareQuery(query, chwId, firstName, secondName, otherName, phoneNumber, educationLevel, communityName, facilityName, chiefdomName, districtName, phuSupervisor, selected, false, pageable);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery = prepareQuery(countQuery, chwId, firstName, secondName, otherName, phoneNumber, educationLevel, communityName, facilityName, chiefdomName, districtName, phuSupervisor, selected, true, pageable);
    Long count = entityManager.createQuery(countQuery).getSingleResult();
    int pageSize = getPageSize(pageable);
    int firstResult = getFirstResult(pageable, pageSize);
    List<CommunityHealthWorker> communityHealthWorkers = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
    return new PageImpl<>(communityHealthWorkers, pageable, count);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker)

Example 5 with CommunityHealthWorker

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

the class CommunityHealthWorkerService method processChwCsv.

/**
 *.
 * Processes CSV file which contains CHW list and returns list of errors
 * @param chwCsvFile CSV file with CHW 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> processChwCsv(MultipartFile chwCsvFile, Boolean selected) throws IOException {
    ICsvMapReader csvMapReader;
    csvMapReader = new CsvMapReader(new InputStreamReader(chwCsvFile.getInputStream()), CsvPreference.STANDARD_PREFERENCE);
    final String[] header = csvMapReader.getHeader(true);
    final CellProcessor[] processors = getProcessors();
    Map<String, Object> csvRow;
    Set<String> phoneNumberSet = new HashSet<>();
    Set<String> chwIdSet = 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 phoneNumber = Objects.toString(csvRow.get("Mobile"), null);
        String chwId = Objects.toString(csvRow.get("CHW ID"), null);
        // Validate
        if (phoneNumberSet.contains(phoneNumber)) {
            errorMap.put(csvMapReader.getLineNumber(), "Phone number is duplicated in CSV");
            continue;
        }
        if (chwIdSet.contains(chwId)) {
            errorMap.put(csvMapReader.getLineNumber(), "CHW ID is duplicated in CSV");
            continue;
        }
        if (validateBlankFieldsInCsv(csvMapReader.getLineNumber(), csvRow, errorMap)) {
            continue;
        }
        // Add to collections
        if (phoneNumber != null) {
            phoneNumberSet.add(phoneNumber);
        }
        if (chwId != null) {
            chwIdSet.add(chwId);
        }
        String community = Objects.toString(csvRow.get("Community"), null);
        String facility = Objects.toString(csvRow.get("PHU"), null);
        Community chwCommunity = communityRepository.findByNameAndFacilityName(community, facility);
        if (chwCommunity == null) {
            errorMap.put(csvMapReader.getLineNumber(), String.format("There is no community %s in facility %s in MOTS", community, facility));
            continue;
        }
        Optional<CommunityHealthWorker> existingHealthWorker = healthWorkerRepository.findByChwId(csvRow.get("CHW ID").toString());
        CommunityHealthWorker communityHealthWorker;
        if (existingHealthWorker.isPresent()) {
            communityHealthWorker = existingHealthWorker.get();
        } else {
            communityHealthWorker = new CommunityHealthWorker();
            communityHealthWorker.setPreferredLanguage(Language.ENGLISH);
            communityHealthWorker.setSelected(false);
        }
        if ((selected || communityHealthWorker.getSelected()) && StringUtils.isBlank(phoneNumber)) {
            errorMap.put(csvMapReader.getLineNumber(), "Phone number is empty");
            continue;
        }
        communityHealthWorker.setChwId(csvRow.get("CHW ID").toString());
        communityHealthWorker.setFirstName(csvRow.get("First_Name").toString());
        communityHealthWorker.setSecondName(csvRow.get("Second_Name").toString());
        communityHealthWorker.setOtherName(Objects.toString(csvRow.get("Other_Name"), null));
        communityHealthWorker.setYearOfBirth(csvRow.get("Age") != null ? LocalDate.now().getYear() - Integer.valueOf(Objects.toString(csvRow.get("Age"), null)) : null);
        communityHealthWorker.setGender(Gender.getByDisplayName(csvRow.get("Gender").toString()));
        communityHealthWorker.setLiteracy(Literacy.getByDisplayName(csvRow.get("Read_Write").toString()));
        communityHealthWorker.setEducationLevel(EducationLevel.getByDisplayName(csvRow.get("Education").toString()));
        communityHealthWorker.setPhoneNumber(phoneNumber);
        communityHealthWorker.setCommunity(chwCommunity);
        communityHealthWorker.setHasPeerSupervisor(csvRow.get("Peer_Supervisor").equals("Yes"));
        communityHealthWorker.setWorking(csvRow.get("Working").equals("Yes"));
        if (selected && !communityHealthWorker.getSelected()) {
            selectHealthWorker(communityHealthWorker);
        } else {
            healthWorkerRepository.save(communityHealthWorker);
        }
    }
    return errorMap;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) Community(org.motechproject.mots.domain.Community) ICsvMapReader(org.supercsv.io.ICsvMapReader) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)13 Test (org.junit.Test)4 AssignedModules (org.motechproject.mots.domain.AssignedModules)4 HashSet (java.util.HashSet)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 CourseModule (org.motechproject.mots.domain.CourseModule)2 Module (org.motechproject.mots.domain.Module)2 IvrException (org.motechproject.mots.exception.IvrException)2 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)2 AssignedModulesDataBuilder (org.motechproject.mots.testbuilder.AssignedModulesDataBuilder)2 CommunityHealthWorkerDataBuilder (org.motechproject.mots.testbuilder.CommunityHealthWorkerDataBuilder)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Transactional (org.springframework.transaction.annotation.Transactional)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 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1