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