001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.labs.registration.controller;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.io.SerializationUtils;
020import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationActivity;
021import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationCourse;
022import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationForm;
023import org.kuali.rice.krad.labs.registration.form.LabsAdminRegistrationIssue;
024import org.kuali.rice.krad.uif.UifParameters;
025import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
026import org.kuali.rice.krad.web.controller.UifControllerBase;
027import org.kuali.rice.krad.web.form.DialogResponse;
028import org.kuali.rice.krad.web.form.UifFormBase;
029import org.springframework.stereotype.Controller;
030import org.springframework.validation.BindingResult;
031import org.springframework.web.bind.annotation.ModelAttribute;
032import org.springframework.web.bind.annotation.RequestMapping;
033import org.springframework.web.bind.annotation.RequestMethod;
034import org.springframework.web.bind.annotation.ResponseBody;
035import org.springframework.web.servlet.ModelAndView;
036
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039import java.util.ArrayList;
040import java.util.Collection;
041import java.util.Date;
042import java.util.HashMap;
043import java.util.List;
044import java.util.Map;
045import java.util.Random;
046
047/**
048 * Class for KS Admin Registration Lab prototype
049 *
050 * @author Kuali Rice Team (rice.collab@kuali.org)
051 */
052@Controller
053@RequestMapping(value = "/ksworkshop")
054public class LabsAdminRegistrationController extends UifControllerBase {
055    private static String REG_COLL_ID = "KS-AdminRegistration-Registered";
056    private static String WAITLIST_COLL_ID = "KS-AdminRegistration-Waitlist";
057
058    @Override
059    protected LabsAdminRegistrationForm createInitialForm() {
060        return new LabsAdminRegistrationForm();
061    }
062
063    /**
064     * @see org.kuali.rice.krad.web.service.RefreshControllerService#refresh(org.kuali.rice.krad.web.form.UifFormBase)
065     */
066    @Override
067    @RequestMapping(params = "methodToCall=refresh")
068    public ModelAndView refresh(UifFormBase form) {
069        cancelEdits((LabsAdminRegistrationForm) form, form.getUpdateComponentId());
070        return getRefreshControllerService().refresh(form);
071    }
072
073    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=getStudentInfo")
074    public ModelAndView getStudentInfo(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
075            HttpServletRequest request, HttpServletResponse response) {
076        // Pretend service call to get student and populate
077        form.setStudentName("Allison Glass");
078        form.setStanding("Junior");
079        form.setProgram("Undergrad");
080        form.setDepartment("Arts and Humanites");
081        form.setMajor("Psychology");
082        form.setCredits("68");
083
084        return getModelAndView(form);
085    }
086
087    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=register")
088    public ModelAndView register(@ModelAttribute("KualiForm") LabsAdminRegistrationForm form, BindingResult result,
089            HttpServletRequest request, HttpServletResponse response) {
090        DialogResponse dialogResponse = form.getDialogResponse("KS-AdminRegistration-RegisterDialogResponse");
091        if (dialogResponse == null) {
092            for (LabsAdminRegistrationCourse course : form.getPendingCourses()) {
093                course.setCourseName("Some course name here");
094                course.setCredits(3);
095                course.setRegDate(new Date());
096                course.setRegOptions("reg");
097                course.setEffectiveDate(new Date());
098                List<LabsAdminRegistrationActivity> activities = new ArrayList<LabsAdminRegistrationActivity>();
099                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}