001    /**
002     * Copyright 2005-2014 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     */
016    package edu.sampleu.demo.kitchensink;
017    
018    import java.util.Properties;
019    import java.util.Set;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    
024    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
025    import org.kuali.rice.krad.uif.UifConstants;
026    import org.kuali.rice.krad.uif.UifParameters;
027    import org.kuali.rice.krad.util.GlobalVariables;
028    import org.kuali.rice.krad.web.bind.RequestAccessible;
029    import org.kuali.rice.krad.web.controller.MethodAccessible;
030    import org.kuali.rice.krad.web.controller.UifControllerBase;
031    import org.kuali.rice.krad.web.form.UifFormBase;
032    import org.springframework.stereotype.Controller;
033    import org.springframework.validation.BindingResult;
034    import org.springframework.web.bind.annotation.ModelAttribute;
035    import org.springframework.web.bind.annotation.RequestMapping;
036    import org.springframework.web.bind.annotation.RequestMethod;
037    import org.springframework.web.servlet.ModelAndView;
038    
039    /**
040     * Controller for the Test UI Page
041     *
042     * @author Kuali Rice Team (rice.collab@kuali.org)
043     */
044    @Controller
045    @RequestMapping(value = "/uicomponents")
046    public class UifComponentsTestController extends UifControllerBase {
047    
048        /**
049         * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
050         */
051        @Override
052        protected UifComponentsTestForm createInitialForm(HttpServletRequest request) {
053            return new UifComponentsTestForm();
054        }
055    
056        @Override
057        @RequestMapping(params = "methodToCall=start")
058        public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
059                HttpServletResponse response) {
060            UifComponentsTestForm uiTestForm = (UifComponentsTestForm) form;
061            form.setState("state1");
062            //for generated view:
063            if (form.getView().getId().equals("UifGeneratedFields")) {
064                for (int i = 0; i < 100; i++) {
065                    ((UifComponentsTestForm) form).getList1generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
066                            "D" + i));
067                }
068                for (int i = 0; i < 100; i++) {
069                    ((UifComponentsTestForm) form).getList2generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
070                            "D" + i));
071                }
072                for (int i = 0; i < 10; i++) {
073                    ((UifComponentsTestForm) form).getList3generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
074                            "D" + i));
075                    for (int j = 0; j < 10; j++) {
076                        ((UifComponentsTestForm) form).getList3generated().get(i).getSubList().add(new UITestObject(
077                                "i" + i + "-" + j, "i" + i + "-" + j, "i" + i + "-" + j, "i" + i + "-" + j));
078                    }
079                }
080            }
081    
082            GlobalVariables.getMessageMap().addGrowlMessage("Welcome!", "kitchenSink.welcome");
083    
084            return super.start(uiTestForm, request, response);
085        }
086    
087        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=save")
088        public ModelAndView save(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm, BindingResult result,
089                HttpServletRequest request, HttpServletResponse response) {
090            KRADServiceLocatorWeb.getViewValidationService().validateView(uiTestForm);
091    
092            return getUIFModelAndView(uiTestForm);
093        }
094    
095        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
096        public ModelAndView close(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm, BindingResult result,
097                HttpServletRequest request, HttpServletResponse response) {
098    
099            return getUIFModelAndView(uiTestForm, "UifCompView-Page1");
100        }
101    
102        /**
103         * Handles menu navigation between view pages
104         */
105        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=navigate")
106        public ModelAndView navigate(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
107                HttpServletRequest request, HttpServletResponse response) {
108            String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
109    
110            if (pageId.equals("UifCompView-Page8")) {
111                GlobalVariables.getMessageMap().putError("gField1", "serverTestError");
112                GlobalVariables.getMessageMap().putError("gField1", "serverTestError2");
113                GlobalVariables.getMessageMap().putError("gField2", "serverTestError");
114                GlobalVariables.getMessageMap().putError("gField3", "serverTestError");
115                GlobalVariables.getMessageMap().putWarning("gField1", "serverTestWarning");
116                GlobalVariables.getMessageMap().putWarning("gField2", "serverTestWarning");
117                GlobalVariables.getMessageMap().putInfo("gField2", "serverTestInfo");
118                GlobalVariables.getMessageMap().putInfo("gField3", "serverTestInfo");
119            }
120    
121            return getUIFModelAndView(form, pageId);
122        }
123    
124        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProgGroup")
125        public ModelAndView refreshProgGroup(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
126                BindingResult result, HttpServletRequest request, HttpServletResponse response) {
127    
128            return getUIFModelAndView(uiTestForm);
129        }
130    
131        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshWithServerMessages")
132        public ModelAndView refreshWithServerMessages(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
133                BindingResult result, HttpServletRequest request, HttpServletResponse response) {
134            GlobalVariables.getMessageMap().putError("field45", "serverTestError");
135            GlobalVariables.getMessageMap().putWarning("field45", "serverTestWarning");
136            GlobalVariables.getMessageMap().putInfo("field45", "serverTestInfo");
137    
138            return getUIFModelAndView(uiTestForm);
139        }
140    
141        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=genCollectionServerMessages")
142        public ModelAndView genCollectionServerMessages(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
143                BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
144            GlobalVariables.getMessageMap().putError("list2[0].field1", "serverTestError");
145            GlobalVariables.getMessageMap().putWarning("list2[0].field1", "serverTestWarning");
146            GlobalVariables.getMessageMap().putInfo("list2[0].field1", "serverTestInfo");
147    
148            GlobalVariables.getMessageMap().putError("list3[0].field1", "serverTestError");
149            GlobalVariables.getMessageMap().putWarning("list3[0].field1", "serverTestWarning");
150            GlobalVariables.getMessageMap().putInfo("list3[0].field1", "serverTestInfo");
151    
152            GlobalVariables.getMessageMap().putError("list5[0].subList[0].field1", "serverTestError");
153            GlobalVariables.getMessageMap().putWarning("list5[0].subList[0].field1", "serverTestWarning");
154            GlobalVariables.getMessageMap().putInfo("list5[0].subList[0].field1", "serverTestInfo");
155            return refresh(uiTestForm, result, request, response);
156        }
157    
158        /**
159         * Adds errors to fields defined in the validationMessageFields array
160         */
161        @MethodAccessible
162        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrors")
163        public ModelAndView addErrors(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
164                HttpServletRequest request, HttpServletResponse response) {
165    
166            if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
167                GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section1", "errorSectionTest");
168                GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section2", "errorSectionTest");
169            } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
170                GlobalVariables.getMessageMap().putError("badKey", "unmatchedTest");
171            } else if (form.getPageId().equals("Demo-ValidationLayout-SubSectionsPage")) {
172                GlobalVariables.getMessageMap().putError("Uif-ValidationLayout-SubGroup", "errorSectionTest");
173            }
174    
175            if (form.getViewPostMetadata().getId().equals("RichMessagesView")) {
176                GlobalVariables.getMessageMap().putError("Demo-BasicMessagesSection", "richValidationMessageTest");
177                GlobalVariables.getMessageMap().putError("field5", "richValidationMessageTest2");
178            }
179    
180            Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
181            for (String id : inputFieldIds) {
182                if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
183                    String key = (String) form.getViewPostMetadata().getComponentPostData(id,
184                            UifConstants.PostMetadata.PATH);
185                    GlobalVariables.getMessageMap().putError(key, "error1Test");
186                }
187            }
188    
189            return getUIFModelAndView(form);
190        }
191    
192        /**
193         * Adds warnings to fields defined in the validationMessageFields array
194         */
195        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addWarnings")
196        public ModelAndView addWarnings(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
197                HttpServletRequest request, HttpServletResponse response) {
198    
199            if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
200                GlobalVariables.getMessageMap().putWarning("Demo-ValidationLayout-Section1", "warningSectionTest");
201                GlobalVariables.getMessageMap().putWarning("Demo-ValidationLayout-Section2", "warningSectionTest");
202            } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
203                GlobalVariables.getMessageMap().putWarning("badKey", "unmatchedTest");
204            }
205    
206            Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
207            for (String id : inputFieldIds) {
208                if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
209                    String key = (String) form.getViewPostMetadata().getComponentPostData(id,
210                            UifConstants.PostMetadata.PATH);
211                    GlobalVariables.getMessageMap().putWarning(key, "warning1Test");
212                }
213            }
214    
215            return getUIFModelAndView(form);
216        }
217    
218        /**
219         * Adds infos to fields defined in the validationMessageFields array
220         */
221        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addInfo")
222        public ModelAndView addInfo(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
223                HttpServletRequest request, HttpServletResponse response) {
224    
225            if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
226                GlobalVariables.getMessageMap().putInfo("Demo-ValidationLayout-Section1", "infoSectionTest");
227                GlobalVariables.getMessageMap().putInfo("Demo-ValidationLayout-Section2", "infoSectionTest");
228            } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
229                GlobalVariables.getMessageMap().putInfo("badKey", "unmatchedTest");
230            }
231    
232            Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
233            for (String id : inputFieldIds) {
234                if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
235                    String key = (String) form.getViewPostMetadata().getComponentPostData(id,
236                            UifConstants.PostMetadata.PATH);
237                    GlobalVariables.getMessageMap().putInfo(key, "info1Test");
238                }
239            }
240    
241            return getUIFModelAndView(form);
242        }
243    
244        /**
245         * Adds all message types to fields defined in the validationMessageFields array
246         */
247        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addAllMessages")
248        public ModelAndView addAllMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
249                HttpServletRequest request, HttpServletResponse response) {
250    
251            this.addErrors(form, result, request, response);
252            this.addWarnings(form, result, request, response);
253            this.addInfo(form, result, request, response);
254    
255            return getUIFModelAndView(form);
256        }
257    
258        /**
259         * Adds error and warning messages to fields defined in the validationMessageFields array
260         */
261        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrorWarnMessages")
262        public ModelAndView addErrorWarnMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
263                HttpServletRequest request, HttpServletResponse response) {
264    
265            this.addErrors(form, result, request, response);
266            this.addWarnings(form, result, request, response);
267    
268            return getUIFModelAndView(form);
269        }
270    
271        /**
272         * Adds error and info messages to fields defined in the validationMessageFields array
273         */
274        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrorInfoMessages")
275        public ModelAndView addErrorInfoMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
276                HttpServletRequest request, HttpServletResponse response) {
277    
278            this.addErrors(form, result, request, response);
279            this.addInfo(form, result, request, response);
280    
281            return getUIFModelAndView(form);
282        }
283    
284        /**
285         * Adds only 1 error message for testing
286         */
287        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addSingleErrorMessage")
288        public ModelAndView addSingleErrorMessage(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
289                HttpServletRequest request, HttpServletResponse response) {
290    
291            if (form.getPageId().equals("Demo-ValidationLayout-SubSectionsPage")) {
292                GlobalVariables.getMessageMap().putError("Uif-ValidationLayout-SubGroup", "errorSectionTest");
293            } else {
294                GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section1", "errorSectionTest");
295            }
296    
297            return getUIFModelAndView(form);
298        }
299    
300        /**
301         * Adds warning and info messages to fields defined in the validationMessageFields array
302         */
303        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addWarnInfoMessages")
304        public ModelAndView addWarnInfoMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
305                HttpServletRequest request, HttpServletResponse response) {
306    
307            this.addWarnings(form, result, request, response);
308            this.addInfo(form, result, request, response);
309    
310            return getUIFModelAndView(form);
311        }
312    
313        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState2")
314        public ModelAndView gotoState2(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
315                HttpServletRequest request, HttpServletResponse response) {
316    
317            KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state2");
318            if (!GlobalVariables.getMessageMap().hasErrors()) {
319                form.setState("state2");
320            }
321    
322            return getUIFModelAndView(form);
323        }
324    
325        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState3")
326        public ModelAndView gotoState3(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
327                HttpServletRequest request, HttpServletResponse response) {
328    
329            KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state3");
330            if (!GlobalVariables.getMessageMap().hasErrors()) {
331                form.setState("state3");
332            }
333    
334            return getUIFModelAndView(form);
335        }
336    
337        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState4")
338        public ModelAndView gotoState4(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
339                HttpServletRequest request, HttpServletResponse response) {
340    
341            KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state4");
342            if (!GlobalVariables.getMessageMap().hasErrors()) {
343                form.setState("state4");
344            }
345    
346            return getUIFModelAndView(form);
347        }
348    
349        /**
350         * Generates a fake incident report to test for errorCallback
351         *
352         * @param form
353         * @param result
354         * @param request
355         * @param response
356         * @return ModelAndView model and view
357         */
358        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=errorCheck")
359        public ModelAndView errorCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
360                HttpServletRequest request, HttpServletResponse response) {
361    
362            if (true) {
363                throw new RuntimeException("Generate fake incident report");
364            }
365    
366            return getUIFModelAndView(form);
367        }
368    
369        /**
370         * Test controller method to check for ajax redirect functionality. Redirects to the portal main page
371         *
372         * @param form
373         * @param result
374         * @param request
375         * @param response
376         * @return ModelAndView model and view
377         */
378        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=redirectCheck")
379        public ModelAndView redirectCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
380                HttpServletRequest request, HttpServletResponse response) {
381            Properties props = new Properties();
382            props.put(UifParameters.VIEW_ID, form.getViewId());
383            props.put(UifParameters.FORM_KEY, form.getFormKey());
384            return performRedirect(form, "http://localhost:8080/kr-dev", props);
385        }
386    
387        @Override
388        public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
389                HttpServletRequest request, HttpServletResponse response) {
390            GlobalVariables.getMessageMap().addGrowlMessage("Greetings!", "kitchenSink.welcome");
391            if (uifForm.getPageId().equals("Demo-ValidationLayout-CollectionsErrorPage")) {
392                GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-CollectionErrorSection",
393                        "errorSectionTest");
394                GlobalVariables.getMessageMap().putErrorForSectionId("Demo-ValidationLayout-CollectionErrorSection",
395                        "errorSectionTest");
396            }
397            return super.addLine(uifForm, result, request, response);
398        }
399    
400        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customLineAction")
401        public ModelAndView customLineAction(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
402                HttpServletRequest request, HttpServletResponse response) {
403    
404            String actionParm1 = uifForm.getActionParamaterValue("field1");
405            String actionParm2 = uifForm.getActionParamaterValue("field2");
406    
407            GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "actionParms.message", actionParm1,
408                    actionParm2);
409    
410            return super.deleteLine(uifForm, result, request, response);
411        }
412    
413        /**
414         * Performs custom line action for collection 4 in kitchen sink collection demo.
415         * Just puts out a growl message and returns.
416         *
417         * @param uifForm
418         * @param result
419         * @param request
420         * @param response
421         * @return
422         */
423        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=updateOfficial")
424        public ModelAndView updateOfficial(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
425                HttpServletRequest request, HttpServletResponse response) {
426    
427            String actionParm1 = uifForm.getActionParamaterValue("field1");
428    
429            GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "customLineAction.message", actionParm1);
430    
431            return getUIFModelAndView(uifForm);
432        }
433    
434        /**
435         * Changes the view to readOnly and returns.
436         *
437         * @param uifForm
438         * @param result
439         * @param request
440         * @param response
441         * @return readOnly View
442         */
443        @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeReadOnly")
444        public ModelAndView makeReadOnly(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
445                HttpServletRequest request, HttpServletResponse response) {
446            //set View to readOnly
447            uifForm.getView().setReadOnly(true);
448            return getUIFModelAndView(uifForm);
449        }
450    }