001/**
002 * Copyright 2005-2015 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 org.kuali.rice.krad.service.KRADServiceLocatorWeb;
019import org.kuali.rice.krad.uif.UifConstants;
020import org.kuali.rice.krad.uif.UifParameters;
021import org.kuali.rice.krad.util.AuditCluster;
022import org.kuali.rice.krad.util.AuditError;
023import org.kuali.rice.krad.util.GlobalVariables;
024import org.kuali.rice.krad.util.KRADConstants;
025import org.kuali.rice.krad.web.controller.UifControllerBase;
026import org.kuali.rice.krad.web.form.UifFormBase;
027import org.springframework.stereotype.Controller;
028import org.springframework.validation.BindingResult;
029import org.springframework.web.bind.annotation.ModelAttribute;
030import org.springframework.web.bind.annotation.RequestMapping;
031import org.springframework.web.bind.annotation.RequestMethod;
032import org.springframework.web.servlet.ModelAndView;
033
034import javax.servlet.http.HttpServletRequest;
035import javax.servlet.http.HttpServletResponse;
036import java.util.ArrayList;
037import java.util.List;
038import java.util.Properties;
039import java.util.Set;
040
041/**
042 * Controller for the Test UI Page
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046@Controller
047@RequestMapping(value = "/uicomponents")
048public class UifComponentsTestController extends UifControllerBase {
049
050    /**
051     * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
052     */
053    @Override
054    protected UifComponentsTestForm createInitialForm() {
055        return new UifComponentsTestForm();
056    }
057
058    @Override
059    @RequestMapping(params = "methodToCall=start")
060    public ModelAndView start(UifFormBase form) {
061        UifComponentsTestForm uiTestForm = (UifComponentsTestForm) form;
062        form.setState("state1");
063        //for generated view:
064        if (form.getView().getId().equals("UifGeneratedFields")) {
065            for (int i = 0; i < 100; i++) {
066                ((UifComponentsTestForm) form).getList1generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
067                        "D" + i));
068            }
069            for (int i = 0; i < 100; i++) {
070                ((UifComponentsTestForm) form).getList2generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
071                        "D" + i));
072            }
073            for (int i = 0; i < 10; i++) {
074                ((UifComponentsTestForm) form).getList3generated().add(new UITestObject("A" + i, "B" + i, "C" + i,
075                        "D" + i));
076                for (int j = 0; j < 10; j++) {
077                    ((UifComponentsTestForm) form).getList3generated().get(i).getSubList().add(new UITestObject(
078                            "i" + i + "-" + j, "i" + i + "-" + j, "i" + i + "-" + j, "i" + i + "-" + j));
079                }
080            }
081        }
082
083        GlobalVariables.getMessageMap().addGrowlMessage("Welcome!", "kitchenSink.welcome");
084
085        return super.start(uiTestForm);
086    }
087
088    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=save")
089    public ModelAndView save(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm, BindingResult result,
090            HttpServletRequest request, HttpServletResponse response) {
091        KRADServiceLocatorWeb.getViewValidationService().validateView(uiTestForm);
092
093        return getModelAndView(uiTestForm);
094    }
095
096    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=close")
097    public ModelAndView close(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm, BindingResult result,
098            HttpServletRequest request, HttpServletResponse response) {
099
100        return getModelAndView(uiTestForm, "UifCompView-Page1");
101    }
102
103    /**
104     * Handles menu navigation between view pages
105     */
106    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=navigate")
107    public ModelAndView navigate(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
108            HttpServletRequest request, HttpServletResponse response) {
109        String pageId = form.getActionParamaterValue(UifParameters.NAVIGATE_TO_PAGE_ID);
110
111        if (pageId.equals("UifCompView-Page8")) {
112            GlobalVariables.getMessageMap().putError("gField1", "serverTestError");
113            GlobalVariables.getMessageMap().putError("gField1", "serverTestError2");
114            GlobalVariables.getMessageMap().putError("gField2", "serverTestError");
115            GlobalVariables.getMessageMap().putError("gField3", "serverTestError");
116            GlobalVariables.getMessageMap().putWarning("gField1", "serverTestWarning");
117            GlobalVariables.getMessageMap().putWarning("gField2", "serverTestWarning");
118            GlobalVariables.getMessageMap().putInfo("gField2", "serverTestInfo");
119            GlobalVariables.getMessageMap().putInfo("gField3", "serverTestInfo");
120        }
121
122        return getModelAndView(form, pageId);
123    }
124
125    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProgGroup")
126    public ModelAndView refreshProgGroup(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
127            BindingResult result, HttpServletRequest request, HttpServletResponse response) {
128
129        return getModelAndView(uiTestForm);
130    }
131
132    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshWithServerMessages")
133    public ModelAndView refreshWithServerMessages(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
134            BindingResult result, HttpServletRequest request, HttpServletResponse response) {
135        GlobalVariables.getMessageMap().putError("field45", "serverTestError");
136        GlobalVariables.getMessageMap().putWarning("field45", "serverTestWarning");
137        GlobalVariables.getMessageMap().putInfo("field45", "serverTestInfo");
138
139        return getModelAndView(uiTestForm);
140    }
141
142    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=genCollectionServerMessages")
143    public ModelAndView genCollectionServerMessages(@ModelAttribute("KualiForm") UifComponentsTestForm uiTestForm,
144            BindingResult result, HttpServletRequest request, HttpServletResponse response) throws Exception {
145        GlobalVariables.getMessageMap().putError("list2[0].field1", "serverTestError");
146        GlobalVariables.getMessageMap().putWarning("list2[0].field1", "serverTestWarning");
147        GlobalVariables.getMessageMap().putInfo("list2[0].field1", "serverTestInfo");
148
149        GlobalVariables.getMessageMap().putError("list3[0].field1", "serverTestError");
150        GlobalVariables.getMessageMap().putWarning("list3[0].field1", "serverTestWarning");
151        GlobalVariables.getMessageMap().putInfo("list3[0].field1", "serverTestInfo");
152
153        GlobalVariables.getMessageMap().putError("list5[0].subList[0].field1", "serverTestError");
154        GlobalVariables.getMessageMap().putWarning("list5[0].subList[0].field1", "serverTestWarning");
155        GlobalVariables.getMessageMap().putInfo("list5[0].subList[0].field1", "serverTestInfo");
156        return refresh(uiTestForm);
157    }
158
159    /**
160     * Adds errors to fields defined in the validationMessageFields array
161     */
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 getModelAndView(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 getModelAndView(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 getModelAndView(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 getModelAndView(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 getModelAndView(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 getModelAndView(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 getModelAndView(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 getModelAndView(form);
311    }
312
313    /**
314     * Adds warning and info messages to fields defined in the validationMessageFields array
315     */
316    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addAuditErrors")
317    public ModelAndView addAuditErrors(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
318            HttpServletRequest request, HttpServletResponse response) {
319
320        List<AuditError> auditErrors = new ArrayList<AuditError>();
321        List<AuditError> auditWarnings = new ArrayList<AuditError>();
322        Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
323        for (String id : inputFieldIds) {
324            if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
325                String key = (String) form.getViewPostMetadata().getComponentPostData(id,
326                        UifConstants.PostMetadata.PATH);
327                auditErrors.add(new AuditError(key, "error1Test", "link"));
328                auditWarnings.add(new AuditError(key, "warning1Test", "link"));
329            }
330        }
331        auditErrors.add(new AuditError("Demo-ValidationLayout-Section1", "errorSectionTest", "link"));
332
333        GlobalVariables.getAuditErrorMap().put("A", new AuditCluster("A", auditErrors,
334                KRADConstants.Audit.AUDIT_ERRORS));
335        GlobalVariables.getAuditErrorMap().put("B", new AuditCluster("B", auditWarnings,
336                KRADConstants.Audit.AUDIT_WARNINGS));
337
338        return getModelAndView(form);
339    }
340
341    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState2")
342    public ModelAndView gotoState2(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
343            HttpServletRequest request, HttpServletResponse response) {
344
345        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state2");
346        if (!GlobalVariables.getMessageMap().hasErrors()) {
347            form.setState("state2");
348        }
349
350        return getModelAndView(form);
351    }
352
353    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState3")
354    public ModelAndView gotoState3(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
355            HttpServletRequest request, HttpServletResponse response) {
356
357        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state3");
358        if (!GlobalVariables.getMessageMap().hasErrors()) {
359            form.setState("state3");
360        }
361
362        return getModelAndView(form);
363    }
364
365    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=gotoState4")
366    public ModelAndView gotoState4(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
367            HttpServletRequest request, HttpServletResponse response) {
368
369        KRADServiceLocatorWeb.getViewValidationService().validateView(form, "state4");
370        if (!GlobalVariables.getMessageMap().hasErrors()) {
371            form.setState("state4");
372        }
373
374        return getModelAndView(form);
375    }
376
377    /**
378     * Generates a fake incident report to test for errorCallback
379     *
380     * @return ModelAndView model and view
381     */
382    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=errorCheck")
383    public ModelAndView errorCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
384            HttpServletRequest request, HttpServletResponse response) {
385
386        if (true) {
387            throw new RuntimeException("Generate fake incident report");
388        }
389
390        return getModelAndView(form);
391    }
392
393    /**
394     * Test controller method to check for ajax redirect functionality. Redirects to the portal main page
395     *
396     * @return ModelAndView model and view
397     */
398    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=redirectCheck")
399    public ModelAndView redirectCheck(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
400            HttpServletRequest request, HttpServletResponse response) {
401        Properties props = new Properties();
402        props.put(UifParameters.VIEW_ID, form.getViewId());
403        props.put(UifParameters.FORM_KEY, form.getFormKey());
404        return performRedirect(form, "http://localhost:8080/kr-dev", props);
405    }
406
407    @Override
408    public ModelAndView addLine(UifFormBase uifForm) {
409        GlobalVariables.getMessageMap().addGrowlMessage("Greetings!", "kitchenSink.welcome");
410        if (uifForm.getPageId().equals("Demo-ValidationLayout-CollectionsErrorPage")) {
411            GlobalVariables.getMessageMap().putError("Demo-ValidationLayout-CollectionErrorSection",
412                    "errorSectionTest");
413            GlobalVariables.getMessageMap().putErrorForSectionId("Demo-ValidationLayout-CollectionErrorSection",
414                    "errorSectionTest");
415        }
416        return super.addLine(uifForm);
417    }
418
419    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customLineAction")
420    public ModelAndView customLineAction(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
421            HttpServletRequest request, HttpServletResponse response) {
422
423        String actionParm1 = uifForm.getActionParamaterValue("field1");
424        String actionParm2 = uifForm.getActionParamaterValue("field2");
425
426        GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "actionParms.message", actionParm1,
427                actionParm2);
428
429        return super.deleteLine(uifForm);
430    }
431
432    /**
433     * Performs custom line action for collection 4 in kitchen sink collection demo.
434     * Just puts out a growl message and returns.
435     */
436    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=updateOfficial")
437    public ModelAndView updateOfficial(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
438            HttpServletRequest request, HttpServletResponse response) {
439
440        String actionParm1 = uifForm.getActionParamaterValue("field1");
441
442        GlobalVariables.getMessageMap().addGrowlMessage("Action Parameters", "customLineAction.message", actionParm1);
443
444        return getModelAndView(uifForm);
445    }
446
447    /**
448     * Changes the view to readOnly and returns.
449     *
450     * @return readOnly View
451     */
452    @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeReadOnly")
453    public ModelAndView makeReadOnly(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
454            HttpServletRequest request, HttpServletResponse response) {
455        //set View to readOnly
456        uifForm.getView().setReadOnly(true);
457        return getModelAndView(uifForm);
458    }
459}