Search in sources :

Example 1 with EntityNotFoundException

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);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Mappings(org.mapstruct.Mappings) Mapping(org.mapstruct.Mapping) ChoiceDto(org.motechproject.mots.dto.ChoiceDto) CourseModule(org.motechproject.mots.domain.CourseModule) CallFlowElementType(org.motechproject.mots.domain.enums.CallFlowElementType) ArrayList(java.util.ArrayList) UnitDto(org.motechproject.mots.dto.UnitDto) BooleanUtils(org.apache.commons.lang.BooleanUtils) MessageDto(org.motechproject.mots.dto.MessageDto) MappingTarget(org.mapstruct.MappingTarget) Mapper(org.mapstruct.Mapper) MultipleChoiceQuestion(org.motechproject.mots.domain.MultipleChoiceQuestion) Module(org.motechproject.mots.domain.Module) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Mappers(org.mapstruct.factory.Mappers) MultipleChoiceQuestionDto(org.motechproject.mots.dto.MultipleChoiceQuestionDto) Choice(org.motechproject.mots.domain.Choice) Course(org.motechproject.mots.domain.Course) CallFlowElement(org.motechproject.mots.domain.CallFlowElement) ReportingPolicy(org.mapstruct.ReportingPolicy) ModuleDto(org.motechproject.mots.dto.ModuleDto) List(java.util.List) Message(org.motechproject.mots.domain.Message) Unit(org.motechproject.mots.domain.Unit) ModuleSimpleDto(org.motechproject.mots.dto.ModuleSimpleDto) Status(org.motechproject.mots.domain.enums.Status) CourseDto(org.motechproject.mots.dto.CourseDto) CallFlowElementDto(org.motechproject.mots.dto.CallFlowElementDto) ArrayList(java.util.ArrayList) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Unit(org.motechproject.mots.domain.Unit) UnitDto(org.motechproject.mots.dto.UnitDto)

Example 2 with EntityNotFoundException

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);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) JasperReportsMultiFormatView(org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView) JasperTemplate(org.motechproject.mots.domain.JasperTemplate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with EntityNotFoundException

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);
    }
}
Also used : AssignedModules(org.motechproject.mots.domain.AssignedModules) IvrException(org.motechproject.mots.exception.IvrException) User(org.motechproject.mots.domain.security.User) ModuleAssignmentException(org.motechproject.mots.exception.ModuleAssignmentException) CommunityHealthWorker(org.motechproject.mots.domain.CommunityHealthWorker) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) CourseModule(org.motechproject.mots.domain.CourseModule) Module(org.motechproject.mots.domain.Module) DistrictAssignmentLog(org.motechproject.mots.domain.DistrictAssignmentLog) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with EntityNotFoundException

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);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Mappings(org.mapstruct.Mappings) Mapping(org.mapstruct.Mapping) ChoiceDto(org.motechproject.mots.dto.ChoiceDto) CourseModule(org.motechproject.mots.domain.CourseModule) CallFlowElementType(org.motechproject.mots.domain.enums.CallFlowElementType) ArrayList(java.util.ArrayList) UnitDto(org.motechproject.mots.dto.UnitDto) BooleanUtils(org.apache.commons.lang.BooleanUtils) MessageDto(org.motechproject.mots.dto.MessageDto) MappingTarget(org.mapstruct.MappingTarget) Mapper(org.mapstruct.Mapper) MultipleChoiceQuestion(org.motechproject.mots.domain.MultipleChoiceQuestion) Module(org.motechproject.mots.domain.Module) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Mappers(org.mapstruct.factory.Mappers) MultipleChoiceQuestionDto(org.motechproject.mots.dto.MultipleChoiceQuestionDto) Choice(org.motechproject.mots.domain.Choice) Course(org.motechproject.mots.domain.Course) CallFlowElement(org.motechproject.mots.domain.CallFlowElement) ReportingPolicy(org.mapstruct.ReportingPolicy) ModuleDto(org.motechproject.mots.dto.ModuleDto) List(java.util.List) Message(org.motechproject.mots.domain.Message) Unit(org.motechproject.mots.domain.Unit) ModuleSimpleDto(org.motechproject.mots.dto.ModuleSimpleDto) Status(org.motechproject.mots.domain.enums.Status) CourseDto(org.motechproject.mots.dto.CourseDto) CallFlowElementDto(org.motechproject.mots.dto.CallFlowElementDto) CallFlowElementDto(org.motechproject.mots.dto.CallFlowElementDto) CallFlowElement(org.motechproject.mots.domain.CallFlowElement) ArrayList(java.util.ArrayList) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException)

Example 5 with EntityNotFoundException

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);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Mappings(org.mapstruct.Mappings) Mapping(org.mapstruct.Mapping) ChoiceDto(org.motechproject.mots.dto.ChoiceDto) CourseModule(org.motechproject.mots.domain.CourseModule) CallFlowElementType(org.motechproject.mots.domain.enums.CallFlowElementType) ArrayList(java.util.ArrayList) UnitDto(org.motechproject.mots.dto.UnitDto) BooleanUtils(org.apache.commons.lang.BooleanUtils) MessageDto(org.motechproject.mots.dto.MessageDto) MappingTarget(org.mapstruct.MappingTarget) Mapper(org.mapstruct.Mapper) MultipleChoiceQuestion(org.motechproject.mots.domain.MultipleChoiceQuestion) Module(org.motechproject.mots.domain.Module) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Mappers(org.mapstruct.factory.Mappers) MultipleChoiceQuestionDto(org.motechproject.mots.dto.MultipleChoiceQuestionDto) Choice(org.motechproject.mots.domain.Choice) Course(org.motechproject.mots.domain.Course) CallFlowElement(org.motechproject.mots.domain.CallFlowElement) ReportingPolicy(org.mapstruct.ReportingPolicy) ModuleDto(org.motechproject.mots.dto.ModuleDto) List(java.util.List) Message(org.motechproject.mots.domain.Message) Unit(org.motechproject.mots.domain.Unit) ModuleSimpleDto(org.motechproject.mots.dto.ModuleSimpleDto) Status(org.motechproject.mots.domain.enums.Status) CourseDto(org.motechproject.mots.dto.CourseDto) CallFlowElementDto(org.motechproject.mots.dto.CallFlowElementDto) CourseModule(org.motechproject.mots.domain.CourseModule) ArrayList(java.util.ArrayList) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) ModuleDto(org.motechproject.mots.dto.ModuleDto)

Aggregations

EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)5 CourseModule (org.motechproject.mots.domain.CourseModule)4 Module (org.motechproject.mots.domain.Module)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BooleanUtils (org.apache.commons.lang.BooleanUtils)3 StringUtils (org.apache.commons.lang.StringUtils)3 Mapper (org.mapstruct.Mapper)3 Mapping (org.mapstruct.Mapping)3 MappingTarget (org.mapstruct.MappingTarget)3 Mappings (org.mapstruct.Mappings)3 ReportingPolicy (org.mapstruct.ReportingPolicy)3 Mappers (org.mapstruct.factory.Mappers)3 CallFlowElement (org.motechproject.mots.domain.CallFlowElement)3 Choice (org.motechproject.mots.domain.Choice)3 Course (org.motechproject.mots.domain.Course)3 Message (org.motechproject.mots.domain.Message)3 MultipleChoiceQuestion (org.motechproject.mots.domain.MultipleChoiceQuestion)3 Unit (org.motechproject.mots.domain.Unit)3 CallFlowElementType (org.motechproject.mots.domain.enums.CallFlowElementType)3