1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.enrollment.class2.courseoffering.controller;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.kim.api.identity.Person;
20 import org.kuali.rice.krad.util.GlobalVariables;
21 import org.kuali.rice.krad.util.KRADConstants;
22 import org.kuali.rice.krad.web.form.DocumentFormBase;
23 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
24 import org.kuali.rice.krad.web.form.UifFormBase;
25 import org.kuali.student.common.uif.util.KSControllerHelper;
26 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingCreateWrapper;
27 import org.kuali.student.enrollment.class2.courseoffering.dto.CourseOfferingEditWrapper;
28 import org.kuali.student.enrollment.class2.courseoffering.dto.JointCourseWrapper;
29 import org.kuali.student.enrollment.class2.courseoffering.service.impl.CourseOfferingCreateMaintainableImpl;
30 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingConstants;
31 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingManagementUtil;
32 import org.kuali.student.enrollment.class2.courseoffering.util.ManageSocConstants;
33 import org.kuali.student.enrollment.common.util.EnrollConstants;
34 import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
35 import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
36 import org.kuali.student.enrollment.courseofferingset.dto.SocRolloverResultItemInfo;
37 import org.kuali.student.r2.common.dto.ContextInfo;
38 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
39 import org.kuali.student.r2.common.util.ContextUtils;
40 import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
41 import org.kuali.student.r2.common.util.constants.LuServiceConstants;
42 import org.kuali.student.r2.core.acal.dto.TermInfo;
43 import org.kuali.student.r2.lum.course.dto.CourseInfo;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.validation.BindingResult;
46 import org.springframework.web.bind.annotation.ModelAttribute;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.servlet.ModelAndView;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import java.util.Collections;
53 import java.util.Comparator;
54 import java.util.List;
55 import java.util.Properties;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @Controller
74 @RequestMapping(value = "/courseOfferingCreate")
75 public class CourseOfferingCreateController extends CourseOfferingBaseController {
76
77
78
79
80
81
82 @Override
83 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
84 HttpServletRequest request, HttpServletResponse response) {
85 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) form;
86 setupMaintenance(maintenanceForm, request, KRADConstants.MAINTENANCE_NEW_ACTION);
87
88 if (form.getView() != null) {
89 String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
90
91
92 String createCO = request.getParameter(CourseOfferingConstants.CREATE_COURSEOFFERING);
93 if (createCO != null && createCO.equals("true")) {
94 CourseOfferingControllerPopulateUIForm.populateCreateCourseOfferingForm(maintenanceForm, request);
95 }
96
97
98 String pageId = request.getParameter("pageId");
99 if (pageId != null && pageId.equals("courseOfferingCopyPage")){
100 Object selectedObject = maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
101 if (selectedObject instanceof CourseOfferingCreateWrapper) {
102 CourseOfferingCreateWrapper coCreateWrapper = (CourseOfferingCreateWrapper)selectedObject;
103 String targetTermCode = request.getParameter("targetTermCode");
104 String catalogCourseCode = request.getParameter("catalogCourseCode");
105 String coId = request.getParameter("courseOfferingId");
106
107 CourseOfferingControllerPopulateUIForm.copyCourseOfferingInfo(coCreateWrapper, targetTermCode, catalogCourseCode, coId);
108 }
109 }
110 checkViewAuthorization(form, methodToCall);
111 }
112
113 return getUIFModelAndView(maintenanceForm);
114 }
115
116 @Override
117 @RequestMapping(params = "methodToCall=route")
118 public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
119 HttpServletRequest request, HttpServletResponse response) {
120
121 super.route(form, result, request, response);
122
123 if( GlobalVariables.getMessageMap().hasErrors() ) {
124 return handleRouteForErrors( form );
125 }
126
127 return handleRouteForCoCreate( form );
128 }
129
130
131 private ModelAndView handleRouteForCoCreate( DocumentFormBase form ) {
132
133 Properties urlParameters = new Properties();
134
135 CourseOfferingEditWrapper dataObject = (CourseOfferingEditWrapper)((MaintenanceDocumentForm)form).getDocument().getNewMaintainableObject().getDataObject();
136
137
138 if (StringUtils.equals(dataObject.getFinalExamDriver(), LuServiceConstants.LU_EXAM_DRIVER_CO_KEY)) {
139 if (StringUtils.isEmpty(dataObject.getExamPeriodId())) {
140 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS_WITH_MISSING_EXAMPERIOD);
141 } else if (!StringUtils.equals(dataObject.getCourseOfferingInfo().getFinalExamType(), CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_STANDARD)) {
142 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS);
143 } else {
144 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS_WITH_EXAMOFFERING_GENERATED);
145 }
146 } else {
147 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS);
148 }
149
150 urlParameters.put(EnrollConstants.GROWL_MESSAGE_PARAMS, dataObject.getCourseOfferingCode() + dataObject.getCourseOfferingInfo().getCourseNumberSuffix());
151
152 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "show");
153 urlParameters.put("termCode", dataObject.getTerm().getCode());
154 if (dataObject.getCourseOfferingInfo().getCourseNumberSuffix() != null && !StringUtils.isBlank(dataObject.getCourseOfferingInfo().getCourseNumberSuffix())) {
155 urlParameters.put("inputCode", dataObject.getCourseOfferingInfo().getCourseOfferingCode().concat(dataObject.getCourseOfferingInfo().getCourseNumberSuffix()));
156 } else {
157 urlParameters.put("inputCode", dataObject.getCourseOfferingInfo().getCourseOfferingCode());
158 }
159 urlParameters.put("viewId", CourseOfferingConstants.MANAGE_CO_VIEW_ID);
160 urlParameters.put("pageId", CourseOfferingConstants.MANAGE_THE_CO_PAGE);
161 urlParameters.put("withinPortal", "false");
162
163 return super.performRedirect(form, CourseOfferingConstants.MANAGE_CO_CONTROLLER_PATH, urlParameters);
164 }
165
166
167
168
169
170
171 @RequestMapping(params = "methodToCall=showCreateFormatSection")
172 public ModelAndView showCreateFormatSection(@ModelAttribute("KualiForm") MaintenanceDocumentForm form) throws Exception {
173
174 CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
175 wrapper.setShowCreateFormatSection(true);
176 wrapper.setShowCopyFormatSection(false);
177
178 return getUIFModelAndView(form);
179 }
180
181
182
183
184
185
186 @RequestMapping(params = "methodToCall=showCopyFromJointOffering")
187 public ModelAndView showCopyFromJointOffering(@ModelAttribute("KualiForm") MaintenanceDocumentForm form) throws Exception {
188
189 CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
190 wrapper.setShowCreateFormatSection(false);
191 wrapper.setShowCopyFormatSection(true);
192
193 return getUIFModelAndView(form);
194 }
195
196
197
198
199
200
201
202
203 @RequestMapping(params = "methodToCall=markCourseForJointOffering")
204 public ModelAndView markCourseForJointOffering(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, @SuppressWarnings("unused") BindingResult result,
205 HttpServletRequest request, HttpServletResponse response) throws Exception {
206
207 CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
208 int index = wrapper.getSelectedJointCourseIndex();
209 if(form.getActionParameters().size() > 1) {
210 String lineIndex = form.getActionParameters().get("selectedLineIndex");
211 index = Integer.parseInt(lineIndex);
212 wrapper.setSelectedJointCourseIndex(index);
213 }
214 JointCourseWrapper joint = wrapper.getJointCourses().get(index);
215
216 if (joint.isSelectedToJointlyOfferred()){
217 String dialogName = CourseOfferingConstants.JOINT_COURSE_FORMATS_DELETE_DIALOG;
218
219 if (!hasDialogBeenAnswered(dialogName, form)) {
220 wrapper.setSelectedJointCourseCode(joint.getCourseCode());
221 wrapper.setDialogFormatOfferingWrapperList(joint.getFormatOfferingWrappers());
222 return showDialog(dialogName, form, request, response);
223 }
224
225 boolean dialogAnswer = getBooleanDialogResponse(dialogName, form, request, response);
226 form.getDialogManager().resetDialogStatus(dialogName);
227
228 if (dialogAnswer) {
229 joint.setSelectedToJointlyOfferred(false);
230 String jointCodes = StringUtils.remove(wrapper.getJointCourseCodes(), ", " + joint.getCourseCode());
231 wrapper.setJointCourseCodes(jointCodes);
232 wrapper.getFormatOfferingWrappers().removeAll(joint.getFormatOfferingWrappers());
233 }
234
235 } else {
236 wrapper.setJointCourseCodes(wrapper.getJointCourseCodes() + ", " + joint.getCourseCode());
237 joint.setSelectedToJointlyOfferred(true);
238 }
239
240 return getUIFModelAndView(form);
241 }
242
243
244
245
246
247
248 @RequestMapping(params = "methodToCall=addFormat")
249 public ModelAndView addFormat(@ModelAttribute("KualiForm") MaintenanceDocumentForm form) throws Exception {
250
251 CourseOfferingCreateWrapper wrapper = (CourseOfferingCreateWrapper)form.getDocument().getNewMaintainableObject().getDataObject();
252 CourseOfferingCreateMaintainableImpl maintainable = (CourseOfferingCreateMaintainableImpl)KSControllerHelper.getViewHelperService(form);
253
254
255 if (wrapper.isShowCreateFormatSection()){
256 maintainable.addFormatOffering(wrapper);
257 } else {
258 maintainable.copyJointFormatOfferings(wrapper);
259 }
260
261 return getUIFModelAndView(form);
262 }
263
264 @RequestMapping(params = "methodToCall=continueFromCreate")
265 public ModelAndView continueFromCreate(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, @SuppressWarnings("unused") BindingResult result,
266 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
267
268 CourseOfferingCreateWrapper coWrapper = ((CourseOfferingCreateWrapper) form.getDocument().getNewMaintainableObject().getDataObject());
269 String courseCode = coWrapper.getCatalogCourseCode();
270 String termCode = coWrapper.getTargetTermCode();
271
272
273 if( StringUtils.isBlank(termCode) ) {
274
275
276 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_PARAMETER_IS_REQUIRED, "Term");
277 }
278 if( StringUtils.isBlank(courseCode) ) {
279
280
281 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_PARAMETER_IS_REQUIRED, "Course Code");
282 }
283 if (GlobalVariables.getMessageMap().getErrorCount() > 0) {
284 return getUIFModelAndView(form);
285 }
286
287 TermInfo term = CourseOfferingManagementUtil.getTerm(termCode);
288 if (term == null) {
289 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_TERM_INVALID, termCode);
290 return getUIFModelAndView(form);
291 }
292
293 coWrapper.setTerm(term);
294
295 List<CourseInfo> matchingCourses = CourseOfferingManagementUtil.retrieveMatchingCourses(courseCode, term);
296 coWrapper.clear();
297 int firstMatchingCourse = 0;
298 if (matchingCourses.size() == 1) {
299 CourseInfo course = matchingCourses.get(firstMatchingCourse);
300
301
302 List<String> orgIds = course.getUnitsContentOwner();
303 if(orgIds != null && !orgIds.isEmpty()){
304 StringBuilder orgIDs = new StringBuilder();
305 for (String orgId : orgIds) {
306 orgIDs.append(orgId + ",");
307 }
308 if (orgIDs.length() > 0) {
309 coWrapper.setAdminOrg(orgIDs.substring(0, orgIDs.length()-1));
310 }
311 }
312 Person user = GlobalVariables.getUserSession().getPerson();
313 boolean canOpenView = form.getView().getAuthorizer().canOpenView(form.getView(), form, user);
314
315 if (!canOpenView) {
316
317
318 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_COURSE_RESTRICTED, courseCode);
319 coWrapper.setAdminOrg(null);
320 coWrapper.setCourse(null);
321
322 return getUIFModelAndView(form);
323 } else {
324
325 ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
326 List<String> socIds = CourseOfferingManagementUtil.getCourseOfferingSetService().getSocIdsByTerm(term.getId(), contextInfo);
327 int firstSocID = 0;
328 if (socIds != null && !socIds.isEmpty()){
329
330 SocInfo soc = CourseOfferingManagementUtil.getCourseOfferingSetService().getSoc(socIds.get(firstSocID), contextInfo);
331 coWrapper.setSocInfo(soc);
332 boolean canOpenViewSoc = form.getView().getAuthorizer().canOpenView(form.getView(), form, user);
333
334 if(!canOpenViewSoc) {
335
336
337 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_TERM_RESTRICTED);
338 coWrapper.setSocInfo(null);
339
340 return getUIFModelAndView(form);
341 } else {
342 if (coWrapper.isCreateFromCatalog()) {
343 Properties urlParameters = CourseOfferingManagementUtil._buildCOURLParameters(course.getId(), term.getId(), soc.getId(), KRADConstants.Maintenance.METHOD_TO_CALL_NEW);
344 return super.performRedirect(form, CourseOfferingConstants.CONTROLLER_PATH_COURSEOFFERING_CREATE_MAINTENANCE, urlParameters);
345 } else {
346
347 CourseOfferingControllerPopulateUIForm.continueFromCreateCopyCourseOfferingInfo(coWrapper, course, term);
348
349
350 Collections.sort(coWrapper.getExistingTermOfferings(), new Comparator<CourseOfferingEditWrapper>() {
351 @Override
352 public int compare(CourseOfferingEditWrapper co1, CourseOfferingEditWrapper co2) {
353 return co2.getTerm().getCode().compareTo(co1.getTerm().getCode());
354 }
355 });
356
357 CourseOfferingCreateMaintainableImpl maintainable = (CourseOfferingCreateMaintainableImpl)KSControllerHelper.getViewHelperService(form);
358 maintainable.loadCourseJointInfos(coWrapper, form.getViewId());
359
360 coWrapper.setEnableCreateButton(true);
361
362 return getUIFModelAndView(form, CourseOfferingConstants.COPY_COURSEOFFERING_PAGE);
363 }
364 }
365 } else {
366 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, ManageSocConstants.MessageKeys.ERROR_SOC_NOT_EXISTS);
367 return getUIFModelAndView(form);
368 }
369 }
370 } else {
371 if (matchingCourses.size() > 1) {
372
373
374 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_MULTIPLE_COURSE_MATCHES, courseCode);
375 } else if (matchingCourses.isEmpty()) {
376 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, CourseOfferingConstants.ERROR_INVALID_CLU_VERSION, courseCode, termCode);
377 }
378
379 return getUIFModelAndView(form);
380 }
381 }
382
383 @RequestMapping(params = "methodToCall=createFromCopy")
384 public ModelAndView createFromCopy(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, @SuppressWarnings("unused") BindingResult result,
385 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
386
387 CourseOfferingCreateWrapper createWrapper = (CourseOfferingCreateWrapper) form.getDocument().getNewMaintainableObject().getDataObject();
388 CourseOfferingInfo existingCO = null;
389 String examPeriodId = null;
390
391
392 for(CourseOfferingEditWrapper eco : createWrapper.getExistingTermOfferings()){
393 if(eco.getIsChecked() || createWrapper.getExistingTermOfferings().size() == 1){
394 existingCO = eco.getCourseOfferingInfo();
395 break;
396 }
397 }
398
399 if (existingCO == null){
400 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS,CourseOfferingConstants.COURSEOFFERING_CREATE_ERROR_PARAMETER_IS_REQUIRED, "Selected Course");
401 return getUIFModelAndView(form);
402 }
403
404 List<String> optionKeys = CourseOfferingControllerPopulateUIForm.getOptionKeys(createWrapper, existingCO);
405 optionKeys.add(CourseOfferingSetServiceConstants.CONTINUE_WITHOUT_EXAM_OFFERINGS_OPTION_KEY);
406 ContextInfo contextInfo = ContextUtils.createDefaultContextInfo();
407 SocRolloverResultItemInfo item = CourseOfferingManagementUtil.getCourseOfferingService().rolloverCourseOffering(existingCO.getId(),
408 createWrapper.getTerm().getId(),
409 optionKeys,
410 contextInfo);
411 CourseOfferingInfo courseOfferingInfo = CourseOfferingManagementUtil.getCourseOfferingService().getCourseOffering(item.getTargetCourseOfferingId(), contextInfo);
412
413
414 try {
415 examPeriodId = CourseOfferingManagementUtil.getExamOfferingServiceFacade().getExamPeriodId(courseOfferingInfo.getTermId(), contextInfo);
416 } catch (DoesNotExistException e) {
417 LOG.warn("The Term " + courseOfferingInfo.getTermId() + " that the course offering " + courseOfferingInfo.getCourseOfferingCode() + " is attached to doesn't have an exam period to create exam offerings.");
418 }
419
420 Properties urlParameters;
421 urlParameters = new Properties();
422
423
424 if (StringUtils.isEmpty(examPeriodId)) {
425 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS_WITH_MISSING_EXAMPERIOD);
426 } else if (!StringUtils.equals(courseOfferingInfo.getFinalExamType(), CourseOfferingConstants.COURSEOFFERING_FINAL_EXAM_TYPE_STANDARD)) {
427 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS);
428 } else {
429 urlParameters.put(EnrollConstants.GROWL_MESSAGE, CourseOfferingConstants.COURSE_OFFERING_CREATE_SUCCESS_WITH_EXAMOFFERING_GENERATED);
430 }
431 urlParameters.put(EnrollConstants.GROWL_MESSAGE_PARAMS, courseOfferingInfo.getCourseOfferingCode());
432
433
434 urlParameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "show");
435 urlParameters.put("termCode",createWrapper.getTargetTermCode());
436 urlParameters.put("inputCode",courseOfferingInfo.getCourseOfferingCode());
437 urlParameters.put("viewId",CourseOfferingConstants.MANAGE_CO_VIEW_ID);
438 urlParameters.put("pageId",CourseOfferingConstants.MANAGE_THE_CO_PAGE);
439 urlParameters.put("withinPortal","false");
440
441 return super.performRedirect(form, CourseOfferingConstants.MANAGE_CO_CONTROLLER_PATH, urlParameters);
442 }
443
444 }