use of org.motechproject.mots.domain.Facility 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;
}
use of org.motechproject.mots.domain.Facility in project mots by motech-implementations.
the class InchargeRepositoryIntegrationTest method getNewFacility.
private Facility getNewFacility() {
Facility newFacility = new FacilityDataBuilder().withChiefdom(chiefdom).buildAsNew();
facilityRepository.save(newFacility);
return newFacility;
}
use of org.motechproject.mots.domain.Facility in project mots by motech-implementations.
the class FacilityRepositoryIntegrationTest method shouldFindFacility.
@Test
public void shouldFindFacility() {
// when
Page<Facility> result = facilityRepository.search(facility1.getFacilityId(), facility1.getName(), facility1.getFacilityType().getDisplayName(), facility1.getInchargeFullName(), null, null, null);
// then
assertThat(result.getTotalElements(), is(1L));
Facility foundFacility = result.getContent().get(0);
assertThat(foundFacility.getId(), is(facility1.getId()));
}
use of org.motechproject.mots.domain.Facility in project mots by motech-implementations.
the class LocationController method createFacility.
/**
* Creates Facility.
* @param facilityCreationDto DTO of facility to be created
* @return created Facility
*/
@RequestMapping(value = "/facility", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public FacilityCreationDto createFacility(@RequestBody @Valid FacilityCreationDto facilityCreationDto, BindingResult bindingResult) {
checkBindingResult(bindingResult);
Facility facility = locationMapper.fromDtoToFacility(facilityCreationDto);
return locationMapper.toFacilityCreationDto(locationService.createFacility(facility));
}
use of org.motechproject.mots.domain.Facility in project mots by motech-implementations.
the class FacilityDataBuilder method build.
/**
* Builds instance of {@link Facility}.
*/
public Facility build() {
Facility facility = buildAsNew();
facility.setId(id);
return facility;
}
Aggregations