Search in sources :

Example 1 with ModuleDto

use of org.motechproject.mots.dto.ModuleDto 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 ModuleDto

use of org.motechproject.mots.dto.ModuleDto in project mots by motech-implementations.

the class ModuleMapper method toDto.

/**
 * Create new DTO with Course Module data.
 * @param courseModule used to create DTO
 * @return DTO with Course Module data
 */
public ModuleDto toDto(CourseModule courseModule) {
    String treeId = "-v" + courseModule.getCourse().getVersion();
    ModuleDto moduleDto = toDto(courseModule.getModule());
    addTreeId(moduleDto, treeId);
    moduleDto.setIvrId(courseModule.getIvrId());
    moduleDto.setIvrName(courseModule.getIvrName());
    return moduleDto;
}
Also used : ModuleDto(org.motechproject.mots.dto.ModuleDto)

Example 3 with ModuleDto

use of org.motechproject.mots.dto.ModuleDto 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

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