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()));
}
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));
}
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;
}
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);
}
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);
}
});
}
Aggregations