| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
package org.kuali.student.enrollment.class2.courseoffering.service.impl; |
| 6 | |
|
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Arrays; |
| 9 | |
import java.util.Collections; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
| 13 | |
import org.kuali.student.enrollment.acal.service.AcademicCalendarService; |
| 14 | |
import org.kuali.student.enrollment.class2.courseoffering.service.decorators.R1CourseServiceHelper; |
| 15 | |
import org.kuali.student.enrollment.class2.courseoffering.service.transformer.CourseOfferingTransformer; |
| 16 | |
import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo; |
| 17 | |
import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo; |
| 18 | |
import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo; |
| 19 | |
import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo; |
| 20 | |
import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService; |
| 21 | |
import org.kuali.student.enrollment.courseoffering.service.CourseOfferingServiceBusinessLogic; |
| 22 | |
import org.kuali.student.lum.course.dto.CourseInfo; |
| 23 | |
import org.kuali.student.lum.course.service.CourseService; |
| 24 | |
import org.kuali.student.r2.common.dto.AttributeInfo; |
| 25 | |
import org.kuali.student.r2.common.dto.ContextInfo; |
| 26 | |
import org.kuali.student.r2.common.dto.ValidationResultInfo; |
| 27 | |
import org.kuali.student.r2.common.exceptions.AlreadyExistsException; |
| 28 | |
import org.kuali.student.r2.common.exceptions.DataValidationErrorException; |
| 29 | |
import org.kuali.student.r2.common.exceptions.DoesNotExistException; |
| 30 | |
import org.kuali.student.r2.common.exceptions.InvalidParameterException; |
| 31 | |
import org.kuali.student.r2.common.exceptions.MissingParameterException; |
| 32 | |
import org.kuali.student.r2.common.exceptions.OperationFailedException; |
| 33 | |
import org.kuali.student.r2.common.exceptions.PermissionDeniedException; |
| 34 | |
import org.kuali.student.r2.common.exceptions.ReadOnlyException; |
| 35 | |
import org.kuali.student.r2.common.exceptions.VersionMismatchException; |
| 36 | |
import org.kuali.student.r2.common.infc.ValidationResult.ErrorLevel; |
| 37 | |
import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants; |
| 38 | |
import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants; |
| 39 | |
|
| 40 | |
import javax.xml.namespace.QName; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public class CourseOfferingServiceBusinessLogicImpl implements CourseOfferingServiceBusinessLogic { |
| 47 | |
|
| 48 | |
private CourseService courseService; |
| 49 | |
private AcademicCalendarService acalService; |
| 50 | |
private CourseOfferingService coService; |
| 51 | |
|
| 52 | |
public CourseOfferingService getCoService() { |
| 53 | 0 | return coService; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public void setCoService(CourseOfferingService coService) { |
| 57 | 0 | this.coService = coService; |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public AcademicCalendarService getAcalService() { |
| 61 | 0 | return acalService; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public void setAcalService(AcademicCalendarService acalService) { |
| 65 | 0 | this.acalService = acalService; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public CourseService getCourseService() { |
| 69 | 0 | return courseService; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public void setCourseService(CourseService courseService) { |
| 73 | 0 | this.courseService = courseService; |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
private CourseOfferingService _getCoService() { |
| 77 | 0 | if (coService == null) { |
| 78 | 0 | coService = (CourseOfferingService) GlobalResourceLoader.getService(new QName(CourseOfferingServiceConstants.NAMESPACE, |
| 79 | |
CourseOfferingServiceConstants.SERVICE_NAME_LOCAL_PART)); |
| 80 | |
} |
| 81 | 0 | return coService; |
| 82 | |
} |
| 83 | |
|
| 84 | |
@Override |
| 85 | |
public CourseOfferingInfo rolloverCourseOffering(String sourceCoId, |
| 86 | |
String targetTermId, |
| 87 | |
List<String> optionKeys, |
| 88 | |
ContextInfo context) throws AlreadyExistsException, |
| 89 | |
DataValidationErrorException, DoesNotExistException, DataValidationErrorException, InvalidParameterException, |
| 90 | |
MissingParameterException, OperationFailedException, PermissionDeniedException, ReadOnlyException { |
| 91 | |
|
| 92 | 0 | CourseOfferingInfo sourceCo = this._getCoService().getCourseOffering(sourceCoId, context); |
| 93 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.IGNORE_CANCELLED_OPTION_KEY)) { |
| 94 | 0 | throw new DataValidationErrorException("Skipped because course offering was cancelled in source term"); |
| 95 | |
} |
| 96 | 0 | R1CourseServiceHelper helper = new R1CourseServiceHelper(courseService, acalService); |
| 97 | |
|
| 98 | 0 | CourseInfo sourceCourse = helper.getCourse(sourceCo.getCourseId()); |
| 99 | 0 | String sourceCourseId = sourceCourse.getId(); |
| 100 | 0 | List<CourseInfo> targetCourses = helper.getCoursesForTerm(sourceCourseId, targetTermId, context); |
| 101 | 0 | if (targetCourses.isEmpty()) { |
| 102 | 0 | throw new InvalidParameterException("Skipped because there is no valid version of the course in the target term"); |
| 103 | |
} |
| 104 | 0 | if (targetCourses.size() > 1) { |
| 105 | 0 | throw new InvalidParameterException( |
| 106 | |
"Skipped because there are more than one valid versions of the course in the target term"); |
| 107 | |
} |
| 108 | 0 | CourseInfo targetCourse = targetCourses.get(0); |
| 109 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.SKIP_IF_ALREADY_EXISTS_OPTION_KEY)) { |
| 110 | 0 | String existingCoId = this.findFirstExistingCourseOfferingIdInTargetTerm(targetCourse.getId(), targetTermId, context); |
| 111 | 0 | if (existingCoId != null) { |
| 112 | 0 | throw new AlreadyExistsException("Skipped because course offering already exists in target term"); |
| 113 | |
} |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.STILL_OFFERABLE_OPTION_KEY)) { |
| 118 | 0 | if (!targetCourse.getState().equals("Active")) { |
| 119 | 0 | throw new DataValidationErrorException("skipped because canonical course is no longer active"); |
| 120 | |
} |
| 121 | |
} |
| 122 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.IF_NO_NEW_VERSION_OPTION_KEY)) { |
| 123 | 0 | if (!sourceCourse.getId().equals(targetCourse.getId())) { |
| 124 | 0 | throw new DataValidationErrorException("skipped because there is a new version of the canonical course"); |
| 125 | |
} |
| 126 | |
} |
| 127 | 0 | CourseOfferingInfo targetCo = new CourseOfferingInfo(sourceCo); |
| 128 | 0 | targetCo.setId(null); |
| 129 | |
|
| 130 | 0 | for (OfferingInstructorInfo instr : targetCo.getInstructors()) { |
| 131 | 0 | instr.setId(null); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | 0 | for (AttributeInfo attr : targetCo.getAttributes()) { |
| 140 | 0 | attr.setId(null); |
| 141 | |
} |
| 142 | 0 | targetCo.setTermId(targetTermId); |
| 143 | 0 | targetCo.setMeta(null); |
| 144 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.NO_INSTRUCTORS_OPTION_KEY)) { |
| 145 | 0 | targetCo.getInstructors().clear(); |
| 146 | |
} |
| 147 | 0 | targetCo.setCourseId(targetCourse.getId()); |
| 148 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.USE_CANNONICAL_OPTION_KEY)) { |
| 149 | |
|
| 150 | 0 | CourseOfferingTransformer coTransformer = new CourseOfferingTransformer(); |
| 151 | 0 | coTransformer.copyFromCanonical(targetCourse, targetCo, optionKeys); |
| 152 | |
} |
| 153 | 0 | targetCo = this._getCoService().createCourseOffering(targetCo.getCourseId(), targetCo.getTermId(), targetCo.getTypeKey(), |
| 154 | |
targetCo, optionKeys, context); |
| 155 | 0 | for (FormatOfferingInfo sourceFo : this._getCoService().getFormatOfferingsByCourseOffering(sourceCo.getId(), context)) { |
| 156 | 0 | FormatOfferingInfo targetFo = new FormatOfferingInfo(sourceFo); |
| 157 | 0 | targetFo.setId(null); |
| 158 | |
|
| 159 | 0 | for (AttributeInfo attr : targetFo.getAttributes()) { |
| 160 | 0 | attr.setId(null); |
| 161 | |
} |
| 162 | 0 | targetFo.setCourseOfferingId(targetCo.getId()); |
| 163 | 0 | targetFo.setTermId(targetTermId); |
| 164 | 0 | targetFo.setMeta(null); |
| 165 | 0 | CourseOfferingService locoService = this.getCoService(); |
| 166 | 0 | targetFo = locoService.createFormatOffering(targetFo.getCourseOfferingId(), targetFo.getFormatId(), |
| 167 | |
targetFo.getTypeKey(), targetFo, context); |
| 168 | 0 | for (ActivityOfferingInfo sourceAo : locoService.getActivityOfferingsByFormatOffering(sourceFo.getId(), context)) { |
| 169 | 0 | ActivityOfferingInfo targetAo = new ActivityOfferingInfo(sourceAo); |
| 170 | 0 | targetAo.setId(null); |
| 171 | |
|
| 172 | 0 | for (AttributeInfo attr : targetAo.getAttributes()) { |
| 173 | 0 | attr.setId(null); |
| 174 | |
} |
| 175 | 0 | for (OfferingInstructorInfo instr : targetAo.getInstructors()) { |
| 176 | 0 | instr.setId(null); |
| 177 | |
} |
| 178 | 0 | targetAo.setFormatOfferingId(targetFo.getId()); |
| 179 | 0 | targetAo.setTermId(targetTermId); |
| 180 | 0 | targetAo.setMeta(null); |
| 181 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.NO_SCHEDULE_OPTION_KEY)) { |
| 182 | 0 | targetAo.setScheduleId(null); |
| 183 | |
|
| 184 | |
} |
| 185 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.NO_INSTRUCTORS_OPTION_KEY)) { |
| 186 | 0 | targetAo.getInstructors().clear(); |
| 187 | |
} |
| 188 | 0 | targetAo = this._getCoService().createActivityOffering(targetAo.getFormatOfferingId(), targetAo.getActivityId(), |
| 189 | |
targetAo.getTypeKey(), targetAo, context); |
| 190 | 0 | } |
| 191 | 0 | } |
| 192 | 0 | return targetCo; |
| 193 | |
} |
| 194 | |
|
| 195 | |
@Override |
| 196 | |
public CourseOfferingInfo updateCourseOfferingFromCanonical(String courseOfferingId, List<String> optionKeys, ContextInfo context) |
| 197 | |
throws DataValidationErrorException, |
| 198 | |
DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, |
| 199 | |
PermissionDeniedException, VersionMismatchException { |
| 200 | 0 | CourseOfferingInfo co = this._getCoService().getCourseOffering(courseOfferingId, context); |
| 201 | 0 | CourseInfo course = new R1CourseServiceHelper(courseService, acalService).getCourse(co.getCourseId()); |
| 202 | |
|
| 203 | 0 | CourseOfferingTransformer coTransformer = new CourseOfferingTransformer(); |
| 204 | 0 | coTransformer.copyFromCanonical(course, co, optionKeys); |
| 205 | |
try { |
| 206 | 0 | return this._getCoService().updateCourseOffering(courseOfferingId, co, context); |
| 207 | 0 | } catch (ReadOnlyException ex) { |
| 208 | 0 | throw new OperationFailedException("unexpected", ex); |
| 209 | |
} |
| 210 | |
|
| 211 | |
} |
| 212 | |
|
| 213 | |
@Override |
| 214 | |
public List<ValidationResultInfo> validateCourseOfferingFromCanonical(CourseOfferingInfo courseOfferingInfo, |
| 215 | |
List<String> optionKeys, ContextInfo context) throws DoesNotExistException, |
| 216 | |
InvalidParameterException, MissingParameterException, OperationFailedException { |
| 217 | 0 | List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); |
| 218 | 0 | CourseInfo course = new R1CourseServiceHelper(courseService, acalService).getCourse(courseOfferingInfo.getCourseId()); |
| 219 | 0 | if (!optionKeys.contains(CourseOfferingSetServiceConstants.NOT_COURSE_TITLE_OPTION_KEY)) { |
| 220 | 0 | results.addAll(this.compare("CourseTitle", course.getCourseTitle(), courseOfferingInfo.getCourseOfferingTitle(), null)); |
| 221 | |
} |
| 222 | 0 | results.addAll(this.compare("Code", course.getCode(), courseOfferingInfo.getCourseOfferingCode(), null)); |
| 223 | |
|
| 224 | 0 | if (optionKeys.contains(CourseOfferingSetServiceConstants.CREDITS_MATCH_SCHEDULED_HOURS_OPTION_KEY)) { |
| 225 | 0 | results.addAll(compareCreditsToSchedule(course, courseOfferingInfo)); |
| 226 | |
} |
| 227 | 0 | return results; |
| 228 | |
} |
| 229 | |
|
| 230 | |
private List<ValidationResultInfo> compare(String element, String courseValue, String coValue, String message) { |
| 231 | 0 | if (courseValue == null && coValue == null) { |
| 232 | 0 | return Collections.EMPTY_LIST; |
| 233 | |
} |
| 234 | 0 | if (courseValue.equals(coValue)) { |
| 235 | 0 | return Collections.EMPTY_LIST; |
| 236 | |
} |
| 237 | 0 | ValidationResultInfo result = new ValidationResultInfo(); |
| 238 | 0 | result.setElement(element); |
| 239 | 0 | result.setLevel(ErrorLevel.ERROR); |
| 240 | 0 | result.setMessage(message); |
| 241 | 0 | return Arrays.asList(result); |
| 242 | |
} |
| 243 | |
|
| 244 | |
protected List<ValidationResultInfo> compareCreditsToSchedule(CourseInfo course, CourseOfferingInfo co) { |
| 245 | |
|
| 246 | |
|
| 247 | 0 | return Collections.EMPTY_LIST; |
| 248 | |
} |
| 249 | |
|
| 250 | |
private String findFirstExistingCourseOfferingIdInTargetTerm(String targetCourseId, String targetTermId, ContextInfo context) |
| 251 | |
throws DoesNotExistException, OperationFailedException { |
| 252 | |
List<CourseOfferingInfo> list; |
| 253 | |
try { |
| 254 | 0 | list = this._getCoService().getCourseOfferingsByCourseAndTerm(targetCourseId, targetTermId, context); |
| 255 | 0 | } catch (InvalidParameterException ex) { |
| 256 | 0 | throw new OperationFailedException("unexpected", ex); |
| 257 | 0 | } catch (MissingParameterException ex) { |
| 258 | 0 | throw new OperationFailedException("unexpected", ex); |
| 259 | 0 | } catch (PermissionDeniedException ex) { |
| 260 | 0 | throw new OperationFailedException("unexpected", ex); |
| 261 | 0 | } |
| 262 | 0 | if (list.isEmpty()) { |
| 263 | 0 | return null; |
| 264 | |
} |
| 265 | 0 | return list.get(0).getId(); |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
} |