View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.labs.registration.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.io.SerializationUtils;
20  import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationActivity;
21  import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationCourse;
22  import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationForm;
23  import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationIssue;
24  import org.kuali.rice.krad.uif.UifParameters;
25  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
26  import org.kuali.rice.krad.web.controller.UifControllerBase;
27  import org.kuali.rice.krad.web.form.DialogResponse;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  import org.springframework.stereotype.Controller;
30  import org.springframework.validation.BindingResult;
31  import org.springframework.web.bind.annotation.ModelAttribute;
32  import org.springframework.web.bind.annotation.RequestMapping;
33  import org.springframework.web.bind.annotation.RequestMethod;
34  import org.springframework.web.bind.annotation.ResponseBody;
35  import org.springframework.web.servlet.ModelAndView;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.Date;
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Map;
45  import java.util.Random;
46  
47  /**
48   * Class for KS Admin Registration Lab prototype
49   *
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  @Controller
53  @RequestMapping(value = "/ksworkshop")
54  public class LabsAdminRegistrationController extends UifControllerBase {
55      private static String REG_COLL_ID = "KS-AdminRegistration-Registered";
56      private static String WAITLIST_COLL_ID = "KS-AdminRegistration-Waitlist";
57  
58      @Override
59      protected LabsAdminRegistrationForm createInitialForm() {
60          return new LabsAdminRegistrationForm();
61      }
62  
63      /**
64       * @see org.kuali.rice.krad.web.service.RefreshControllerService#refresh(org.kuali.rice.krad.web.form.UifFormBase)
65       */
66      @Override
67      @RequestMapping(params = "methodToCall=refresh")
68      public ModelAndView refresh(UifFormBase form) {
69          cancelEdits((LabsAdminRegistrationForm) form, form.getUpdateComponentId());
70          return getRefreshControllerService().refresh(form);
71      }
72  
73      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=getStudentInfo")
74      public ModelAndView getStudentInfo(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
75              HttpServletRequest request, HttpServletResponse response) {
76          // Pretend service call to get student and populate
77          form.setStudentName("Allison Glass");
78          form.setStanding("Junior");
79          form.setProgram("Undergrad");
80          form.setDepartment("Arts and Humanites");
81          form.setMajor("Psychology");
82          form.setCredits("68");
83  
84          return getModelAndView(form);
85      }
86  
87      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=register")
88      public ModelAndView register(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
89              HttpServletRequest request, HttpServletResponse response) {
90          DialogResponse dialogResponse = form.getDialogResponse("KS-AdminRegistration-RegisterDialogResponse");
91          if (dialogResponse == null) {
92              for (LabsAdminRegistrationCourse course : form.getPendingCourses()) {
93                  course.setCourseName("Some course name here");
94                  course.setCredits(3);
95                  course.setRegDate(new Date());
96                  course.setRegOptions("reg");
97                  course.setEffectiveDate(new Date());
98                  List<LabsAdminRegistrationActivity> activities = new ArrayList<LabsAdminRegistrationActivity>();
99                  activities.add(new LabsAdminRegistrationActivity("Lec", "MWF 01:00pm - 02:30pm", "Steve Capriani", "PTX 2391"));
100                 activities.add(new LabsAdminRegistrationActivity("Lab", "MWF 02:30pm - 03:30pm", "Steve Capriani", "PTX 2391"));
101                 course.setActivities(activities);
102             }
103 
104             return showDialog("KS-AdminRegistration-RegisterDialogResponse", true, form);
105         }
106 
107         // continue with registration
108         form.getCoursesInProcess().addAll(form.getPendingCourses());
109         form.setPendingCourses(new ArrayList<LabsAdminRegistrationCourse>());
110         form.getPendingCourses().add(new LabsAdminRegistrationCourse());
111 
112         return getModelAndView(form);
113     }
114 
115     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=dropCourse")
116     public ModelAndView dropCourse(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
117             HttpServletRequest request, HttpServletResponse response) {
118         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_PATH);
119         if (StringUtils.isBlank(selectedCollectionPath)) {
120             throw new RuntimeException("Selected collection path was not set for collection action");
121         }
122 
123         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
124 
125         String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
126         int selectedLineIndex = -1;
127         if (StringUtils.isNotBlank(selectedLine)) {
128             selectedLineIndex = Integer.parseInt(selectedLine);
129         }
130 
131         DialogResponse dialogResponse = form.getDialogResponse("KS-AdminRegistration-DropRegisteredDialog");
132 
133         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(form, selectedCollectionPath);
134         Object item = ((List) collection).get(selectedLineIndex);
135 
136         if (dialogResponse == null) {
137             // Create temp object with the info we need about the course
138             LabsAdminRegistrationCourse pendingDropCourse = new LabsAdminRegistrationCourse();
139             pendingDropCourse.setCode(((LabsAdminRegistrationCourse) item).getCode());
140             pendingDropCourse.setSection(((LabsAdminRegistrationCourse) item).getSection());
141             pendingDropCourse.setDropDate(new Date());
142             form.setPendingDropCourse(pendingDropCourse);
143 
144             return showDialog("KS-AdminRegistration-DropRegisteredDialog", true, form);
145         }
146 
147         // TODO you would do the actual drop call here
148         ((LabsAdminRegistrationCourse)item).setDropDate(form.getPendingDropCourse().getDropDate());
149 
150         cancelEdits(form, selectedCollectionId);
151 
152         return deleteLine(form);
153     }
154 
155     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=removeWaitlistCourse")
156     public ModelAndView removeWaitlistCourse(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
157             HttpServletRequest request, HttpServletResponse response) {
158         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
159         cancelEdits(form, selectedCollectionId);
160 
161         return deleteLine(form);
162     }
163 
164     @RequestMapping(method = RequestMethod.GET, params = "methodToCall=regUpdateQuery")
165     @ResponseBody
166     public Map regUpdateQuery(LabsAdminRegistrationForm form) {
167         Map<String, Object> result = new HashMap<String, Object>();
168         List<String> updateIds = new ArrayList<String>();
169 
170         if (form.getCoursesInProcess().isEmpty()) {
171             result.put("stop", true);
172             return result;
173         }
174 
175         synchronized (form) {
176             Random generator = new Random();
177 
178             int i = generator.nextInt(5);
179             if (i == 0 && !form.getCoursesInProcess().isEmpty()) {
180                 // faking a registration complete
181                 form.getRegisteredCourses().add(form.getCoursesInProcess().get(0));
182                 form.getCoursesInProcess().remove(0);
183 
184                 updateIds.add(REG_COLL_ID);
185             }
186 
187             i = generator.nextInt(5);
188             if (i == 0 && !form.getCoursesInProcess().isEmpty()) {
189                 // faking a waitlist complete
190                 form.getWaitlistedCourses().add(form.getCoursesInProcess().get(0));
191                 form.getCoursesInProcess().remove(0);
192 
193                 updateIds.add(WAITLIST_COLL_ID);
194             }
195 
196             i = generator.nextInt(5);
197             if (i == 0 && !form.getCoursesInProcess().isEmpty()) {
198                 // faking an issue found
199                 LabsAdminRegistrationIssue regIssue = new LabsAdminRegistrationIssue();
200                 regIssue.setCourse(form.getCoursesInProcess().get(0));
201                 regIssue.getMessages().add("Some Problem");
202                 regIssue.getMessages().add("Some Other Problem");
203                 form.getRegistrationIssues().add(regIssue);
204                 form.getCoursesInProcess().remove(0);
205 
206                 updateIds.add("KS-AdminRegistration-Issues");
207             }
208         }
209 
210         if (form.getCoursesInProcess().isEmpty()) {
211             result.put("stop", true);
212         }
213 
214         result.put("updateIds", updateIds);
215 
216         return result;
217     }
218 
219     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=editCourse")
220     public ModelAndView editCourse(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
221             HttpServletRequest request, HttpServletResponse response) {
222         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_PATH);
223         if (StringUtils.isBlank(selectedCollectionPath)) {
224             throw new RuntimeException("Selected collection path was not set for collection action");
225         }
226 
227         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
228 
229         String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
230         int selectedLineIndex = -1;
231         if (StringUtils.isNotBlank(selectedLine)) {
232             selectedLineIndex = Integer.parseInt(selectedLine);
233         }
234 
235         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(form, selectedCollectionPath);
236         Object item = ((List) collection).get(selectedLineIndex);
237 
238         cancelEdits(form, selectedCollectionId);
239 
240         // TODO May want to write your own copy/clone method or alternatively re-retrieve value from db on cancel
241         LabsAdminRegistrationCourse
242                 tempCourse = (LabsAdminRegistrationCourse) (SerializationUtils.clone((LabsAdminRegistrationCourse) item));
243 
244         if (selectedCollectionId.equals(REG_COLL_ID)) {
245             form.setEditRegisteredIndex(selectedLineIndex);
246             form.setTempRegCourseEdit(tempCourse);
247         } else if (selectedCollectionId.equals(WAITLIST_COLL_ID)) {
248             form.setEditWaitlistedIndex(selectedLineIndex);
249             form.setTempWaitlistCourseEdit(tempCourse);
250         }
251 
252         return getRefreshControllerService().refresh(form);
253     }
254 
255     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=saveEdit")
256     public ModelAndView saveEdit(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
257             HttpServletRequest request, HttpServletResponse response) {
258         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
259         if (selectedCollectionId.equals(REG_COLL_ID)) {
260             form.setEditRegisteredIndex(-1);
261             form.setTempRegCourseEdit(null);
262         } else if (selectedCollectionId.equals(WAITLIST_COLL_ID)) {
263             form.setEditWaitlistedIndex(-1);
264             form.setTempWaitlistCourseEdit(null);
265         }
266 
267         // TODO perform actual save on item in the backend
268 
269         return refresh(form);
270     }
271 
272     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=cancelEdit")
273     public ModelAndView cancelEdit(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
274             HttpServletRequest request, HttpServletResponse response) {
275         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
276         cancelEdits(form, selectedCollectionId);
277 
278         return refresh(form);
279     }
280 
281     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=allowCourse")
282     public ModelAndView allowCourse(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
283             HttpServletRequest request, HttpServletResponse response) {
284         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_PATH);
285         if (StringUtils.isBlank(selectedCollectionPath)) {
286             throw new RuntimeException("Selected collection path was not set for collection action");
287         }
288 
289         String selectedCollectionId = form.getActionParamaterValue(UifParameters.SELECTED_COLLECTION_ID);
290 
291         String selectedLine = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
292         int selectedLineIndex = -1;
293         if (StringUtils.isNotBlank(selectedLine)) {
294             selectedLineIndex = Integer.parseInt(selectedLine);
295         }
296 
297         // TODO would Force registration here
298         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(form, selectedCollectionPath);
299         Object item = ((List) collection).get(selectedLineIndex);
300         form.getRegisteredCourses().add(((LabsAdminRegistrationIssue) item).getCourse());
301         ((List) collection).remove(selectedLineIndex);
302 
303         return getModelAndView(form);
304     }
305 
306     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=denyCourse")
307     public ModelAndView denyCourse(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
308             HttpServletRequest request, HttpServletResponse response) {
309         // TODO would deny registration request here
310         return deleteLine(form);
311     }
312 
313     private void cancelEdits(LabsAdminRegistrationForm form, String collectionId) {
314         if (collectionId == null) {
315             return;
316         }
317 
318         // Cancel other edit if one is open
319         if (form.getEditRegisteredIndex() > -1 && collectionId.equals(REG_COLL_ID)) {
320             Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(form, "registeredCourses");
321             // TODO using temp here but could retrieve original from db
322             ((List) collection).set(form.getEditRegisteredIndex(), form.getTempRegCourseEdit());
323             form.setEditRegisteredIndex(-1);
324         } else if (form.getEditWaitlistedIndex() > -1 && collectionId.equals(WAITLIST_COLL_ID)) {
325             Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(form, "waitlistedCourses");
326             // TODO using temp here but could retrieve original from db
327             ((List) collection).set(form.getEditWaitlistedIndex(), form.getTempWaitlistCourseEdit());
328             form.setEditRegisteredIndex(-1);
329         }
330     }
331 }