1 package org.kuali.student.enrollment.class2.appointment.controller;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
5 import org.kuali.rice.krad.uif.UifParameters;
6 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
7 import org.kuali.rice.krad.util.GlobalVariables;
8 import org.kuali.rice.krad.util.KRADConstants;
9 import org.kuali.rice.krad.web.controller.UifControllerBase;
10 import org.kuali.rice.krad.web.form.UifFormBase;
11 import org.kuali.student.r2.common.util.constants.AcademicCalendarServiceConstants;
12 import org.kuali.student.enrollment.acal.dto.KeyDateInfo;
13 import org.kuali.student.enrollment.acal.dto.TermInfo;
14 import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
15 import org.kuali.student.enrollment.class2.appointment.dto.AppointmentWindowWrapper;
16 import org.kuali.student.enrollment.class2.appointment.form.RegistrationWindowsManagementForm;
17 import org.kuali.student.enrollment.class2.appointment.service.AppointmentViewHelperService;
18 import org.kuali.student.enrollment.class2.appointment.util.AppointmentConstants;
19 import org.kuali.student.enrollment.class2.appointment.util.AppointmentSlotRuleTypeConversion;
20 import org.kuali.student.r2.common.dto.ContextInfo;
21 import org.kuali.student.r2.common.dto.StatusInfo;
22 import org.kuali.student.r2.common.exceptions.*;
23 import org.kuali.student.r2.common.util.ContextUtils;
24 import org.kuali.student.r2.core.constants.PopulationServiceConstants;
25 import org.kuali.student.r2.core.appointment.constants.AppointmentServiceConstants;
26 import org.kuali.student.r2.core.appointment.dto.AppointmentSlotInfo;
27 import org.kuali.student.r2.core.appointment.dto.AppointmentWindowInfo;
28 import org.kuali.student.r2.core.appointment.service.AppointmentService;
29 import org.kuali.student.r2.core.population.dto.PopulationInfo;
30 import org.kuali.student.r2.core.population.service.PopulationService;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.validation.BindingResult;
33 import org.springframework.web.bind.annotation.ModelAttribute;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.servlet.ModelAndView;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.xml.namespace.QName;
41 import java.text.DateFormat;
42 import java.text.SimpleDateFormat;
43 import java.util.*;
44
45
46
47
48
49
50
51 @Controller
52 @RequestMapping(value = "/registrationWindows")
53 public class RegistrationWindowsController extends UifControllerBase {
54
55 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RegistrationWindowsController.class);
56
57 private AppointmentViewHelperService viewHelperService;
58
59 private AcademicCalendarService acalService;
60
61 private AppointmentService appointmentService;
62
63 private PopulationService populationService;
64
65 @Override
66 protected UifFormBase createInitialForm(HttpServletRequest request) {
67 return new RegistrationWindowsManagementForm();
68 }
69
70 @Override
71 @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
72 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
73 HttpServletRequest request, HttpServletResponse response) {
74 RegistrationWindowsManagementForm theForm = (RegistrationWindowsManagementForm)form;
75 String termId = request.getParameter("termId");
76
77 if (StringUtils.isNotBlank(termId)){
78 try {
79 getViewHelperService(theForm).loadTermAndPeriods(termId, theForm);
80 return getUIFModelAndView(theForm);
81 } catch (Exception ex) {
82 throw new RuntimeException(ex);
83 }
84 }
85
86 return super.start(theForm, result, request, response);
87 }
88
89
90
91
92 @RequestMapping(params = "methodToCall=searchForTerm")
93 public ModelAndView searchForTerm(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm searchForm, BindingResult result,
94 HttpServletRequest request, HttpServletResponse response) throws Exception {
95 String termType = searchForm.getTermType();
96 String termYear = searchForm.getTermYear();
97
98 getViewHelperService(searchForm).searchForTerm(termType, termYear, searchForm);
99
100 if (GlobalVariables.getMessageMap().hasErrors()){
101 return getUIFModelAndView(searchForm, AppointmentConstants.SELECT_TERM_PAGE);
102 }
103
104 return getUIFModelAndView(searchForm, AppointmentConstants.REGISTRATION_WINDOWS_EDIT_PAGE);
105 }
106
107 @RequestMapping(params = "methodToCall=show")
108 public ModelAndView show(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, BindingResult result,
109 HttpServletRequest request, HttpServletResponse response) throws Exception {
110
111 if (!form.isShowAddWindows()){
112 form.setShowAddWindows(true);
113 }
114 String periodId = form.getPeriodId();
115 String periodInfoDetails = "";
116
117
118 form.getAppointmentWindows().clear();
119
120 if(periodId == null || periodId.isEmpty()) {
121 form.setPeriodInfoDetails(periodInfoDetails);
122 form.setShowAddWindows(false);
123 form.getAppointmentWindows().clear();
124 }
125 else if (!periodId.isEmpty() && !periodId.equals("all")) {
126
127
128 KeyDateInfo period = getAcalService().getKeyDate(periodId, ContextUtils.getContextInfo());
129
130
131 List<KeyDateInfo> periods = new ArrayList<KeyDateInfo>();
132 periods.add(period);
133 _loadWindowsInfoForm(periods, form);
134
135
136 AppointmentWindowWrapper addLine= (AppointmentWindowWrapper)form.getNewCollectionLines().get("appointmentWindows");
137 if (addLine == null){
138 addLine = new AppointmentWindowWrapper();
139 form.getNewCollectionLines().put("appointmentWindows", addLine);
140 }
141
142 if (period.getName() != null) {
143 periodInfoDetails = period.getName()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
144 + period.getName()+" End Date: "+_getSimpleDate(period.getEndDate());
145 form.setPeriodName(period.getName());
146 form.setPeriodId(period.getId());
147 addLine.setPeriodName(period.getName());
148 addLine.setPeriodKey(period.getId());
149 } else {
150 periodInfoDetails = period.getId()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
151 + period.getId()+" End Date: "+_getSimpleDate(period.getEndDate());
152 form.setPeriodName(period.getId());
153 form.setPeriodId(period.getId());
154 addLine.setPeriodName(period.getId());
155 addLine.setPeriodKey(period.getId());
156 }
157 form.setPeriodInfoDetails(periodInfoDetails);
158
159
160 }
161 else if (periodId.equals("all")) {
162 List<KeyDateInfo> periodMilestones = form.getPeriodMilestones();
163 if(periodMilestones.isEmpty()) {
164 TermInfo term = form.getTermInfo();
165 if (term.getId() != null && !term.getId().isEmpty()) {
166 getViewHelperService(form).loadPeriods(term.getId(), form);
167 periodMilestones = form.getPeriodMilestones();
168 }
169 }
170 for (KeyDateInfo period : periodMilestones){
171
172 if (period.getName() != null) {
173 periodInfoDetails = periodInfoDetails.concat(period.getName()+" Start Date: "+_getSimpleDate(period.getStartDate())+ "<br>"
174 + period.getName()+" End Date: "+_getSimpleDate(period.getEndDate())+"<br>");
175 } else {
176 periodInfoDetails = periodInfoDetails.concat(period.getId()+" Start Date: "+period.getStartDate()+ "<br>"
177 + period.getId()+" End Date: "+_getSimpleDate(period.getEndDate())+"<br>");
178 }
179 }
180 form.setPeriodInfoDetails(periodInfoDetails);
181 _loadWindowsInfoForm(periodMilestones, form);
182 }
183 return getUIFModelAndView(form);
184 }
185
186 @RequestMapping(params = "methodToCall=save")
187 public ModelAndView save(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm form, BindingResult result,
188 HttpServletRequest request, HttpServletResponse response) throws Exception {
189
190
191 getViewHelperService(form).saveWindows(form);
192
193 return getUIFModelAndView(form);
194 }
195
196 @RequestMapping(params = "methodToCall=assignStudents")
197 public ModelAndView assignStudents(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, BindingResult result,
198 HttpServletRequest request, HttpServletResponse response) throws Exception {
199
200
201 AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Assign Students");
202 boolean isValid = getViewHelperService(uifForm).validateApptWidnow(window);
203 if (isValid) {
204 boolean isSaved = getViewHelperService(uifForm).saveApptWindow(window);
205
206
207 if(window!=null && isSaved){
208
209 List<AppointmentSlotInfo> slots = getAppointmentService().generateAppointmentSlotsByWindow(window.getAppointmentWindowInfo().getId(), new ContextInfo());
210 StatusInfo status = getAppointmentService().generateAppointmentsByWindow(window.getAppointmentWindowInfo().getId(), window.getAppointmentWindowInfo().getTypeKey(), new ContextInfo());
211
212
213 if(status.getIsSuccess()){
214 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
215 AppointmentConstants.APPOINTMENT_MSG_INFO_ASSIGNED,window.getAppointmentWindowInfo().getName(), status.getMessage(), String.valueOf(slots.size()));
216
217 window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY);
218 }else{
219
220 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
221 AppointmentConstants.APPOINTMENT_MSG_ERROR_TOO_MANY_STUDENTS, status.getMessage());
222 }
223 }
224 }
225 return getUIFModelAndView(uifForm);
226 }
227
228 @RequestMapping(params = "methodToCall=breakAppointments")
229 public ModelAndView breakAppointments(@ModelAttribute("KualiForm") RegistrationWindowsManagementForm uifForm, BindingResult result,
230 HttpServletRequest request, HttpServletResponse response) throws Exception {
231
232 AppointmentWindowWrapper window = _getSelectedWindow(uifForm, "Break Appointments");
233 if(window!=null){
234
235 StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
236 if(status.getIsSuccess()){
237 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
238 AppointmentConstants.APPOINTMENT_MSG_INFO_BREAK_APPOINTMENTS_SUCCESS);
239
240
241 window.getAppointmentWindowInfo().setStateKey(AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_DRAFT_KEY);
242 }else{
243
244 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
245 AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE, status.getMessage());
246 }
247 }
248
249 return getUIFModelAndView(uifForm);
250 }
251
252 @Override
253 public ModelAndView deleteLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
254
255 RegistrationWindowsManagementForm theForm = (RegistrationWindowsManagementForm)uifForm;
256 try{
257 AppointmentWindowWrapper window = _getSelectedWindow(theForm, "Delete a Window");
258 if (window != null){
259 if (AppointmentServiceConstants.APPOINTMENT_WINDOW_STATE_ASSIGNED_KEY.equals(window.getAppointmentWindowInfo().getStateKey())){
260
261
262 StatusInfo status = getAppointmentService().deleteAppointmentSlotsByWindowCascading(window.getAppointmentWindowInfo().getId(), new ContextInfo());
263 if(status.getIsSuccess()){
264 getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
265 return super.deleteLine(uifForm, result, request, response);
266 }else{
267
268 GlobalVariables.getMessageMap().putInfo( KRADConstants.GLOBAL_MESSAGES,
269 AppointmentConstants.APPOINTMENT_MSG_ERROR_BREAK_APPOINTMENTS_FAILURE, status.getMessage());
270 return getUIFModelAndView(uifForm);
271 }
272 }else {
273 getAppointmentService().deleteAppointmentWindowCascading(window.getId(), new ContextInfo());
274 return super.deleteLine(uifForm, result, request, response);
275 }
276 }else {
277
278 return getUIFModelAndView(uifForm);
279 }
280 }catch (Exception e){
281
282 return getUIFModelAndView(uifForm);
283
284 }
285 }
286
287
288 private AppointmentWindowWrapper _getSelectedWindow(RegistrationWindowsManagementForm theForm, String actionLink){
289 String selectedCollectionPath = theForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
290 if (StringUtils.isBlank(selectedCollectionPath)) {
291 throw new RuntimeException("Selected collection was not set for " + actionLink);
292 }
293
294 int selectedLineIndex = -1;
295 String selectedLine = theForm.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
296 if (StringUtils.isNotBlank(selectedLine)) {
297 selectedLineIndex = Integer.parseInt(selectedLine);
298 }
299
300 if (selectedLineIndex == -1) {
301 throw new RuntimeException("Selected line index was not set");
302 }
303
304 Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(theForm, selectedCollectionPath);
305 Object window = ((List<Object>) collection).get(selectedLineIndex);
306
307 return (AppointmentWindowWrapper)window;
308 }
309
310
311 private String _getSimpleDate(Date date) {
312 if (date == null){
313 return "";
314 }
315 DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
316 return df.format(date);
317 }
318
319 private void _loadWindowsInfoForm(List<KeyDateInfo> periods, RegistrationWindowsManagementForm form) throws InvalidParameterException, MissingParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
320 for(KeyDateInfo period:periods){
321 List<AppointmentWindowInfo> windows = getAppointmentService().getAppointmentWindowsByPeriod(period.getId(), new ContextInfo());
322 if(windows!=null && windows.size()>0){
323 for(AppointmentWindowInfo window:windows){
324
325
326 PopulationInfo population = getPopulationService().getPopulation(window.getAssignedPopulationId(), new ContextInfo());
327
328 AppointmentWindowWrapper windowWrapper = new AppointmentWindowWrapper();
329
330 windowWrapper.setAppointmentWindowInfo(window);
331 windowWrapper.setId(window.getId());
332 windowWrapper.setWindowName(window.getName());
333 windowWrapper.setPeriodKey(window.getPeriodMilestoneId());
334 windowWrapper.setPeriodName(period.getName());
335 windowWrapper.setAssignedPopulationName(population.getName());
336 windowWrapper.setWindowTypeKey(window.getTypeKey());
337 if(!AppointmentServiceConstants.APPOINTMENT_WINDOW_TYPE_ONE_SLOT_KEY.equals(window.getTypeKey())) {
338 windowWrapper.setSlotRuleEnumType(AppointmentSlotRuleTypeConversion.convToAppointmentSlotRuleCode(window.getSlotRule()));
339 }
340 windowWrapper.setStartDate(window.getStartDate());
341 windowWrapper.setStartTime(_parseTime(window.getStartDate()));
342 windowWrapper.setStartTimeAmPm(_parseAmPm(window.getStartDate()));
343
344 windowWrapper.setEndDate(window.getEndDate());
345 windowWrapper.setEndTime(_parseTime(window.getEndDate()));
346 windowWrapper.setEndTimeAmPm(_parseAmPm(window.getEndDate()));
347
348 form.getAppointmentWindows().add(windowWrapper);
349 }
350 }
351 }
352 }
353
354 private String _parseAmPm(Date date) {
355 if(date==null){
356 return null;
357 }
358 DateFormat df = new SimpleDateFormat("a");
359 return df.format(date);
360 }
361
362 private String _parseTime(Date date) {
363 if(date==null){
364 return null;
365 }
366 DateFormat df = new SimpleDateFormat("hh:mm");
367 return df.format(date);
368 }
369
370 public AppointmentViewHelperService getViewHelperService(RegistrationWindowsManagementForm appointmentForm){
371 if (viewHelperService == null) {
372 if (appointmentForm.getView().getViewHelperServiceClass() != null){
373 viewHelperService = (AppointmentViewHelperService)appointmentForm.getView().getViewHelperService();
374 }else{
375 viewHelperService= (AppointmentViewHelperService)appointmentForm.getPostedView().getViewHelperService();
376 }
377 }
378 return viewHelperService;
379 }
380 public AcademicCalendarService getAcalService() {
381 if(acalService == null) {
382 acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
383 }
384 return this.acalService;
385 }
386
387
388 public AppointmentService getAppointmentService() {
389 if(appointmentService == null) {
390 appointmentService = (AppointmentService) GlobalResourceLoader.getService(new QName(AppointmentServiceConstants.NAMESPACE, AppointmentServiceConstants.SERVICE_NAME_LOCAL_PART));
391 }
392 return appointmentService;
393 }
394
395 public PopulationService getPopulationService() {
396 if(populationService == null) {
397 populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationMockService"));
398 }
399 return populationService;
400 }
401
402 }