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