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