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 */
016package edu.sampleu.demo.kitchensink;
017
018import java.util.Properties;
019import java.util.Set;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.servlet.http.HttpServletResponse;
023
024import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
025import org.kuali.rice.krad.uif.UifConstants;
026import org.kuali.rice.krad.uif.UifParameters;
027import org.kuali.rice.krad.util.GlobalVariables;
028import org.kuali.rice.krad.web.bind.RequestAccessible;
029import org.kuali.rice.krad.web.controller.MethodAccessible;
030import org.kuali.rice.krad.web.controller.UifControllerBase;
031import org.kuali.rice.krad.web.form.UifFormBase;
032import org.springframework.stereotype.Controller;
033import org.springframework.validation.BindingResult;
034import org.springframework.web.bind.annotation.ModelAttribute;
035import org.springframework.web.bind.annotation.RequestMapping;
036import org.springframework.web.bind.annotation.RequestMethod;
037import 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")
046public 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    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrors")
162    public ModelAndView addErrors(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
163            HttpServletRequest request, HttpServletResponse response) {
164
165        if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
166            GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section1", "errorSectionTest");
167            GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section2", "errorSectionTest");
168        } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
169            GlobalVariables.getMessageMap().putError("badKey", "unmatchedTest");
170        } else if (form.getPageId().equals("Demo-ValidationLayout-SubSectionsPage")) {
171            GlobalVariables.getMessageMap().putError("Uif-ValidationLayout-SubGroup", "errorSectionTest");
172        }
173
174        if (form.getViewPostMetadata().getId().equals("RichMessagesView")) {
175            GlobalVariables.getMessageMap().putError("Demo-BasicMessagesSection", "richValidationMessageTest");
176            GlobalVariables.getMessageMap().putError("field5", "richValidationMessageTest2");
177        }
178
179        Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
180        for (String id : inputFieldIds) {
181            if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
182                String key = (String) form.getViewPostMetadata().getComponentPostData(id,
183                        UifConstants.PostMetadata.PATH);
184                GlobalVariables.getMessageMap().putError(key, "error1Test");
185            }
186        }
187
188        return getUIFModelAndView(form);
189    }
190
191    /**
192     * Adds warnings to fields defined in the validationMessageFields array
193     */
194    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addWarnings")
195    public ModelAndView addWarnings(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
196            HttpServletRequest request, HttpServletResponse response) {
197
198        if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
199            GlobalVariables.getMessageMap().putWarning("Demo-ValidationLayout-Section1", "warningSectionTest");
200            GlobalVariables.getMessageMap().putWarning("Demo-ValidationLayout-Section2", "warningSectionTest");
201        } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
202            GlobalVariables.getMessageMap().putWarning("badKey", "unmatchedTest");
203        }
204
205        Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
206        for (String id : inputFieldIds) {
207            if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
208                String key = (String) form.getViewPostMetadata().getComponentPostData(id,
209                        UifConstants.PostMetadata.PATH);
210                GlobalVariables.getMessageMap().putWarning(key, "warning1Test");
211            }
212        }
213
214        return getUIFModelAndView(form);
215    }
216
217    /**
218     * Adds infos to fields defined in the validationMessageFields array
219     */
220    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addInfo")
221    public ModelAndView addInfo(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
222            HttpServletRequest request, HttpServletResponse response) {
223
224        if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageSectionMessages")) {
225            GlobalVariables.getMessageMap().putInfo("Demo-ValidationLayout-Section1", "infoSectionTest");
226            GlobalVariables.getMessageMap().putInfo("Demo-ValidationLayout-Section2", "infoSectionTest");
227        } else if (form.getPageId().equals("Demo-ValidationLayout-SectionsPageUnmatched")) {
228            GlobalVariables.getMessageMap().putInfo("badKey", "unmatchedTest");
229        }
230
231        Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
232        for (String id : inputFieldIds) {
233            if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
234                String key = (String) form.getViewPostMetadata().getComponentPostData(id,
235                        UifConstants.PostMetadata.PATH);
236                GlobalVariables.getMessageMap().putInfo(key, "info1Test");
237            }
238        }
239
240        return getUIFModelAndView(form);
241    }
242
243    /**
244     * Adds all message types to fields defined in the validationMessageFields array
245     */
246    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addAllMessages")
247    public ModelAndView addAllMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
248            HttpServletRequest request, HttpServletResponse response) {
249
250        this.addErrors(form, result, request, response);
251        this.addWarnings(form, result, request, response);
252        this.addInfo(form, result, request, response);
253
254        return getUIFModelAndView(form);
255    }
256
257    /**
258     * Adds error and warning messages to fields defined in the validationMessageFields array
259     */
260    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrorWarnMessages")
261    public ModelAndView addErrorWarnMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
262            HttpServletRequest request, HttpServletResponse response) {
263
264        this.addErrors(form, result, request, response);
265        this.addWarnings(form, result, request, response);
266
267        return getUIFModelAndView(form);
268    }
269
270    /**
271     * Adds error and info messages to fields defined in the validationMessageFields array
272     */
273    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addErrorInfoMessages")
274    public ModelAndView addErrorInfoMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
275            HttpServletRequest request, HttpServletResponse response) {
276
277        this.addErrors(form, result, request, response);
278        this.addInfo(form, result, request, response);
279
280        return getUIFModelAndView(form);
281    }
282
283    /**
284     * Adds only 1 error message for testing
285     */
286    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addSingleErrorMessage")
287    public ModelAndView addSingleErrorMessage(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
288            HttpServletRequest request, HttpServletResponse response) {
289
290        if (form.getPageId().equals("Demo-ValidationLayout-SubSectionsPage")) {
291            GlobalVariables.getMessageMap().putError("Uif-ValidationLayout-SubGroup", "errorSectionTest");
292        } else {
293            GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-Section1", "errorSectionTest");
294        }
295
296        return getUIFModelAndView(form);
297    }
298
299    /**
300     * Adds warning and info messages to fields defined in the validationMessageFields array
301     */
302    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addWarnInfoMessages")
303    public ModelAndView addWarnInfoMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
304            HttpServletRequest request, HttpServletResponse response) {
305
306        this.addWarnings(form, result, request, response);
307        this.addInfo(form, result, request, response);
308
309        return getUIFModelAndView(form);
310    }
311
312    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState2")
313    public ModelAndView gotoState2(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
314            HttpServletRequest request, HttpServletResponse response) {
315
316        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state2");
317        if (!GlobalVariables.getMessageMap().hasErrors()) {
318            form.setState("state2");
319        }
320
321        return getUIFModelAndView(form);
322    }
323
324    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState3")
325    public ModelAndView gotoState3(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
326            HttpServletRequest request, HttpServletResponse response) {
327
328        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state3");
329        if (!GlobalVariables.getMessageMap().hasErrors()) {
330            form.setState("state3");
331        }
332
333        return getUIFModelAndView(form);
334    }
335
336    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState4")
337    public ModelAndView gotoState4(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
338            HttpServletRequest request, HttpServletResponse response) {
339
340        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state4");
341        if (!GlobalVariables.getMessageMap().hasErrors()) {
342            form.setState("state4");
343        }
344
345        return getUIFModelAndView(form);
346    }
347
348    /**
349     * Generates a fake incident report to test for errorCallback
350     *
351     * @param form
352     * @param result
353     * @param request
354     * @param response
355     * @return ModelAndView model and view
356     */
357    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=errorCheck")
358    public ModelAndView errorCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
359            HttpServletRequest request, HttpServletResponse response) {
360
361        if (true) {
362            throw new RuntimeException("Generate fake incident report");
363        }
364
365        return getUIFModelAndView(form);
366    }
367
368    /**
369     * Test controller method to check for ajax redirect functionality. Redirects to the portal main page
370     *
371     * @param form
372     * @param result
373     * @param request
374     * @param response
375     * @return ModelAndView model and view
376     */
377    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=redirectCheck")
378    public ModelAndView redirectCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
379            HttpServletRequest request, HttpServletResponse response) {
380        Properties props = new Properties();
381        props.put(UifParameters.VIEW_ID, form.getViewId());
382        props.put(UifParameters.FORM_KEY, form.getFormKey());
383        return performRedirect(form, "http://localhost:8080/kr-dev", props);
384    }
385
386    @Override
387    public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
388            HttpServletRequest request, HttpServletResponse response) {
389        GlobalVariables.getMessageMap().addGrowlMessage("Greetings!", "kitchenSink.welcome");
390        if (uifForm.getPageId().equals("Demo-ValidationLayout-CollectionsErrorPage")) {
391            GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-CollectionErrorSection",
392                    "errorSectionTest");
393            GlobalVariables.getMessageMap().putErrorForSectionId("Demo-ValidationLayout-CollectionErrorSection",
394                    "errorSectionTest");
395        }
396        return super.addLine(uifForm, result, request, response);
397    }
398
399    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customLineAction")
400    public ModelAndView customLineAction(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
401            HttpServletRequest request, HttpServletResponse response) {
402
403        String actionParm1 = uifForm.getActionParamaterValue("field1");
404        String actionParm2 = uifForm.getActionParamaterValue("field2");
405
406        GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "actionParms.message", actionParm1,
407                actionParm2);
408
409        return super.deleteLine(uifForm, result, request, response);
410    }
411
412    /**
413     * Performs custom line action for collection 4 in kitchen sink collection demo.
414     * Just puts out a growl message and returns.
415     *
416     * @param uifForm
417     * @param result
418     * @param request
419     * @param response
420     * @return
421     */
422    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=updateOfficial")
423    public ModelAndView updateOfficial(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
424            HttpServletRequest request, HttpServletResponse response) {
425
426        String actionParm1 = uifForm.getActionParamaterValue("field1");
427
428        GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "customLineAction.message", actionParm1);
429
430        return getUIFModelAndView(uifForm);
431    }
432
433    /**
434     * Changes the view to readOnly and returns.
435     *
436     * @param uifForm
437     * @param result
438     * @param request
439     * @param response
440     * @return readOnly View
441     */
442    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeReadOnly")
443    public ModelAndView makeReadOnly(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
444            HttpServletRequest request, HttpServletResponse response) {
445        //set View to readOnly
446        uifForm.getView().setReadOnly(true);
447        return getUIFModelAndView(uifForm);
448    }
449}