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