use of org.motechproject.mots.exception.EntityNotFoundException in project mots by motech-implementations.
the class ModuleMapper method updateModuleFromDto.
/**
* Update Module using data from DTO.
* @param moduleDto DTO with new data
* @param module Module to be updated
*/
private void updateModuleFromDto(ModuleDto moduleDto, Module module) {
List<UnitDto> unitDtos = moduleDto.getChildren();
List<Unit> units = module.getUnits();
List<Unit> updatedUnits = new ArrayList<>();
if (units == null) {
units = new ArrayList<>();
}
if (unitDtos != null && !unitDtos.isEmpty()) {
for (int i = 0; i < unitDtos.size(); i++) {
UnitDto unitDto = unitDtos.get(i);
Unit unit;
if (StringUtils.isBlank(unitDto.getId())) {
unit = new Unit();
} else {
unit = units.stream().filter(u -> u.getId().toString().equals(unitDto.getId())).findAny().orElseThrow(() -> new EntityNotFoundException("Cannot update module, because error occurred during unit list update"));
}
updateUnitFromDto(unitDto, unit);
unit.setAllowReplay(BooleanUtils.isTrue(unit.getAllowReplay()));
unit.setListOrder(i);
updatedUnits.add(unit);
}
}
updateFromDto(moduleDto, module);
module.setUnits(updatedUnits);
}
use of org.motechproject.mots.exception.EntityNotFoundException in project mots by motech-implementations.
the class JasperTemplateController method generateReport.
/**
* Generate a report based on the template, the format and the request parameters.
*
* @param request request (to get the request parameters)
* @param templateId report template ID
* @param format report format to generate, default is PDF
* @return the generated report
*/
@RequestMapping(value = "/{id}/{format}", method = RequestMethod.GET)
@ResponseBody
public ModelAndView generateReport(HttpServletRequest request, @PathVariable("id") UUID templateId, @PathVariable("format") String format) throws JasperReportViewException {
JasperTemplate template = jasperTemplateRepository.findOne(templateId);
if (template == null) {
throw new EntityNotFoundException(ERROR_JASPER_TEMPLATE_NOT_FOUND, templateId);
}
Map<String, Object> map = jasperTemplateService.mapRequestParametersToTemplate(request, template);
map.put("format", format);
JasperReportsMultiFormatView jasperView = jasperReportsViewService.getJasperReportsView(template, request);
String fileName = template.getName().replaceAll("\\s+", "_");
String contentDisposition = "inline; filename=" + fileName + "." + format;
jasperView.getContentDispositionMappings().setProperty(format, contentDisposition.toLowerCase(Locale.ENGLISH));
return new ModelAndView(jasperView, map);
}
use of org.motechproject.mots.exception.EntityNotFoundException in project mots by motech-implementations.
the class ModuleAssignmentService method assignModulesToDistrict.
/**
* Creates DistrictAssignmentLog for district assignment,
* and assigns modules to each CHW from a district.
* @param assignmentDto dto with district id, list of modules assigned to it
* and start and end dates
*/
@Transactional
@PreAuthorize(RoleNames.HAS_ASSIGN_MODULES_ROLE)
public void assignModulesToDistrict(DistrictAssignmentDto assignmentDto) {
List<CommunityHealthWorker> communityHealthWorkers = communityHealthWorkerRepository.findByCommunityFacilityChiefdomDistrictIdAndSelected(UUID.fromString(assignmentDto.getDistrictId()), true);
Set<Module> newChwModules = new HashSet<>();
String userName = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User currentUser = userService.getUserByUserName(userName);
for (String moduleId : assignmentDto.getModules()) {
Module moduleToAssign = moduleRepository.findById(UUID.fromString(moduleId)).orElseThrow(() -> new EntityNotFoundException("No module found for Id: {0}", moduleId));
newChwModules.add(moduleToAssign);
assignmentLogRepository.save(new DistrictAssignmentLog(districtRepository.findOne(UUID.fromString(assignmentDto.getDistrictId())), LocalDate.parse(assignmentDto.getStartDate()), LocalDate.parse(assignmentDto.getEndDate()), moduleToAssign, currentUser));
}
for (CommunityHealthWorker chw : communityHealthWorkers) {
AssignedModules existingAssignedModules = getAssignedModules(chw.getId());
Set<Module> oldModules = existingAssignedModules.getModules();
Set<Module> modulesToAdd = getModulesToAdd(oldModules, newChwModules);
existingAssignedModules.getModules().addAll(modulesToAdd);
repository.save(existingAssignedModules);
entityManager.flush();
entityManager.refresh(existingAssignedModules);
String ivrId = existingAssignedModules.getHealthWorker().getIvrId();
if (ivrId == null) {
throw new ModuleAssignmentException("Could not assign module to CHW, because CHW has empty IVR id");
}
try {
ivrService.addSubscriberToGroups(ivrId, getIvrGroupsFromModules(modulesToAdd));
} catch (IvrException ex) {
String message = "Could not assign or delete module for CHW, " + "because of IVR module assignment error.\n\n" + ex.getClearVotoInfo();
throw new ModuleAssignmentException(message, ex);
}
moduleProgressService.createModuleProgresses(chw, modulesToAdd);
}
}
use of org.motechproject.mots.exception.EntityNotFoundException in project mots by motech-implementations.
the class ModuleMapper method updateUnitFromDto.
private void updateUnitFromDto(UnitDto unitDto, Unit unit) {
List<CallFlowElementDto> callFlowElementDtos = unitDto.getChildren();
List<CallFlowElement> callFlowElements = unit.getCallFlowElements();
List<CallFlowElement> updatedCallFlowElements = new ArrayList<>();
if (callFlowElements == null) {
callFlowElements = new ArrayList<>();
}
if (callFlowElementDtos != null && !callFlowElementDtos.isEmpty()) {
for (int i = 0; i < callFlowElementDtos.size(); i++) {
CallFlowElementDto callFlowElementDto = callFlowElementDtos.get(i);
CallFlowElement callFlowElement;
if (callFlowElementDto.getId() == null) {
callFlowElement = fromDto(callFlowElementDto);
} else {
callFlowElement = callFlowElements.stream().filter(cf -> cf.getId().toString().equals(callFlowElementDto.getId())).findAny().orElseThrow(() -> new EntityNotFoundException("Cannot update module, because error occurred during unit list update"));
updateFromDto(callFlowElementDto, callFlowElement);
}
callFlowElement.setListOrder(i);
updatedCallFlowElements.add(callFlowElement);
}
}
updateFromDto(unitDto, unit);
unit.setCallFlowElements(updatedCallFlowElements);
}
use of org.motechproject.mots.exception.EntityNotFoundException in project mots by motech-implementations.
the class ModuleMapper method updateCourseFromDto.
/**
* Update Course using data from DTO.
* @param courseDto DTO with new data
* @param course Course to be updated
*/
public void updateCourseFromDto(CourseDto courseDto, Course course) {
List<ModuleDto> moduleDtos = courseDto.getChildren();
List<CourseModule> courseModules = course.getCourseModules();
List<CourseModule> updatedCourseModules = new ArrayList<>();
if (courseModules == null) {
courseModules = new ArrayList<>();
}
if (moduleDtos != null && !moduleDtos.isEmpty()) {
for (int i = 0; i < moduleDtos.size(); i++) {
ModuleDto moduleDto = moduleDtos.get(i);
CourseModule courseModule;
if (StringUtils.isBlank(moduleDto.getId())) {
courseModule = new CourseModule(course, Module.initialize());
} else {
courseModule = courseModules.stream().filter(cm -> cm.getModule().getId().toString().equals(moduleDto.getId())).findAny().orElseThrow(() -> new EntityNotFoundException("Cannot update course, because error occurred during module list update"));
}
updateCourseModuleFromDto(moduleDto, courseModule);
courseModule.setListOrder(i);
updatedCourseModules.add(courseModule);
}
}
updateFromDto(courseDto, course);
course.setCourseModules(updatedCourseModules);
}
Aggregations