Search in sources :

Example 1 with Community

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

the class CommunityRepositoryIntegrationTest method shouldFindCommunityByName.

@Test
public void shouldFindCommunityByName() {
    // when
    Page<Community> result = communityRepository.search(community1.getName(), null, null, null, null);
    // then
    assertThat(result.getTotalElements(), is(1L));
    Community foundCommunity = result.getContent().get(0);
    assertThat(foundCommunity.getName(), is(community1.getName()));
}
Also used : Community(org.motechproject.mots.domain.Community) Test(org.junit.Test)

Example 2 with Community

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

the class LocationController method createCommunity.

/**
 * Creates Community.
 * @param communityCreationDto DTO of community to be created
 * @return created Community
 */
@RequestMapping(value = "/community", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public CommunityCreationDto createCommunity(@RequestBody @Valid CommunityCreationDto communityCreationDto, BindingResult bindingResult) {
    checkBindingResult(bindingResult);
    Community community = locationMapper.fromDtoToCommunity(communityCreationDto);
    return locationMapper.toCommunityCreationDto(locationService.createCommunity(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 3 with Community

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

Example 4 with Community

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

the class CommunityRepositoryImpl method search.

/**
 * Finds Communities matching all of the provided parameters.
 * If there are no parameters, return all Communities.
 */
@Override
public Page<Community> search(String communityName, String parentFacility, String chiefdomName, String districtName, Pageable pageable) throws IllegalArgumentException {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Community> query = builder.createQuery(Community.class);
    query = prepareQuery(query, communityName, parentFacility, chiefdomName, districtName, false, pageable);
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery = prepareQuery(countQuery, communityName, parentFacility, chiefdomName, districtName, true, pageable);
    Long count = entityManager.createQuery(countQuery).getSingleResult();
    int pageSize = getPageSize(pageable);
    int firstResult = getFirstResult(pageable, pageSize);
    List<Community> incharges = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
    return new PageImpl<>(incharges, pageable, count);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) Community(org.motechproject.mots.domain.Community)

Example 5 with Community

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

the class LocationImporter method parseCommunities.

private void parseCommunities(XSSFSheet sheet) {
    XSSFRow row;
    XSSFCell cell;
    Iterator rows = sheet.rowIterator();
    HashSet<Community> newCommunitySet = new HashSet<>();
    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        cell = row.getCell(COMMUNITY_COL_NUMBER);
        if (cell == null) {
            continue;
        }
        String cellText = cell.getStringCellValue();
        if (cellText.equals(COMMUNITY_HEADER) || StringUtils.isEmpty(cellText)) {
            continue;
        }
        Community community = new Community(cellText);
        String parentFacilityName = row.getCell(FACILITY_COL_NUMBER).getStringCellValue();
        String parentChiefdomName = row.getCell(CHIEFDOM_COL_NUMBER).getStringCellValue();
        String parentDistrictName = row.getCell(DISTRICT_COL_NUMBER).getStringCellValue();
        Facility parent = currentFacilityList.stream().filter(facility -> facility.getName().equals(parentFacilityName) && facility.getChiefdom().getName().equals(parentChiefdomName) && facility.getChiefdom().getDistrict().getName().equals(parentDistrictName)).findFirst().orElseThrow(() -> new RuntimeException(String.format("'%s' Community parent " + "is not defined properly in spreadsheet", community.getName())));
        community.setFacility(parent);
        newCommunitySet.add(community);
    }
    newCommunitySet.forEach(newCommunity -> {
        if (!currentCommunityList.contains(newCommunity)) {
            locationService.createImportedCommunity(newCommunity);
        }
    });
}
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) Community(org.motechproject.mots.domain.Community) HashSet(java.util.HashSet)

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