View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.demo.uif.controller;
17  
18  import org.kuali.rice.krad.demo.uif.form.KradSampleAppForm;
19  import org.kuali.rice.krad.demo.uif.form.UITestObject;
20  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
21  import org.kuali.rice.krad.uif.UifConstants;
22  import org.kuali.rice.krad.uif.view.ViewTheme;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  import org.kuali.rice.krad.web.controller.UifControllerBase;
25  import org.kuali.rice.krad.web.form.UifFormBase;
26  import org.kuali.rice.krad.web.service.FileControllerService;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.beans.factory.annotation.Qualifier;
29  import org.springframework.beans.propertyeditors.CustomCollectionEditor;
30  import org.springframework.stereotype.Controller;
31  import org.springframework.validation.BindingResult;
32  import org.springframework.web.bind.WebDataBinder;
33  import org.springframework.web.bind.annotation.InitBinder;
34  import org.springframework.web.bind.annotation.ModelAttribute;
35  import org.springframework.web.bind.annotation.RequestMapping;
36  import org.springframework.web.bind.annotation.RequestMethod;
37  import org.springframework.web.servlet.ModelAndView;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import java.util.List;
42  import java.util.Set;
43  
44  /**
45   * Basic controller for the KRAD sample application
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @Controller
50  @RequestMapping(value = "/kradsampleapp")
51  public class KradSampleAppController extends UifControllerBase {
52  
53      @Override
54      protected KradSampleAppForm createInitialForm() {
55          return new KradSampleAppForm();
56      }
57  
58      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=changeTheme")
59      public ModelAndView changeTheme(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
60              HttpServletRequest request, HttpServletResponse response) {
61          changeTheme(form);
62          return getModelAndView(form);
63      }
64  
65      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=validateView")
66      public ModelAndView validateView(@ModelAttribute("KualiForm") UifFormBase uiTestForm, BindingResult result,
67              HttpServletRequest request, HttpServletResponse response) {
68          KRADServiceLocatorWeb.getViewValidationService().validateView(uiTestForm);
69          return getModelAndView(uiTestForm);
70      }
71  
72      @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addGrowl")
73      public ModelAndView addGrowl(@ModelAttribute("KualiForm") UifFormBase uiTestForm, BindingResult result,
74              HttpServletRequest request, HttpServletResponse response) {
75          String extraInfo = (String) request.getParameter("extraInfo");
76          if (extraInfo == null) {
77              extraInfo = "none";
78          }
79          GlobalVariables.getMessageMap().addGrowlMessage("Growl Message", "demo.fakeGrowl", extraInfo);
80          return getModelAndView(uiTestForm);
81      }
82  
83      private void changeTheme(UifFormBase form) {
84          String theme = ((KradSampleAppForm) form).getThemeName();
85          if (theme != null) {
86              ViewTheme newTheme = (ViewTheme) (KRADServiceLocatorWeb.getDataDictionaryService().getDictionaryBean(
87                      theme));
88              if (newTheme != null) {
89                  form.getView().setTheme(newTheme);
90              }
91          }
92      }
93  
94      /**
95       * Changes the view to readOnly and returns.
96       *
97       * @param uifForm
98       * @param result
99       * @param request
100      * @param response
101      * @return readOnly View
102      */
103     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=makeReadOnly")
104     public ModelAndView makeReadOnly(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
105             HttpServletRequest request, HttpServletResponse response) {
106         //set View to readOnly
107         uifForm.getView().setReadOnly(true);
108         return getModelAndView(uifForm);
109     }
110 
111     /**
112      * Adds errors to fields defined in the validationMessageFields array
113      */
114     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addStandardSectionsErrors")
115     public ModelAndView addStandardSectionsErrors(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
116             HttpServletRequest request, HttpServletResponse response) {
117         GlobalVariables.getMessageMap().putError("Demo-ValidationMessages-Section1", "errorSectionTest");
118         GlobalVariables.getMessageMap().putError("Demo-ValidationMessages-Section2", "errorSectionTest");
119 
120         Set<String> inputFieldIds = form.getViewPostMetadata().getInputFieldIds();
121         for (String id : inputFieldIds) {
122             if (form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH) != null) {
123                 String key = (String) form.getViewPostMetadata().getComponentPostData(id, UifConstants.PostMetadata.PATH);
124                 GlobalVariables.getMessageMap().putError(key, "error1Test");
125             }
126         }
127 
128         return getModelAndView(form);
129     }
130 
131     /**
132      * Add a message and refresh the group
133      *
134      * @param form
135      * @param result
136      * @param request
137      * @param response
138      * @return
139      */
140     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customEditLine")
141     public ModelAndView customEditLine(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
142             HttpServletRequest request, HttpServletResponse response) {
143         KradSampleAppForm kradSampleAppForm = (KradSampleAppForm) form;
144         List<UITestObject> testObjects = kradSampleAppForm.getCollection6();
145         int index = 1;
146         for(UITestObject testObject : testObjects) {
147             testObject.setField1("Custom Edit Line " + Integer.toString(index));
148             index++;
149         }
150         return getModelAndView(form);
151     }
152 
153     /**
154      * Refreshes the group
155      *
156      * @param form
157      * @param result
158      * @param request
159      * @param response
160      * @return
161      */
162     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshProgGroup")
163     public ModelAndView refreshProgGroup(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
164             HttpServletRequest request, HttpServletResponse response) {
165         return getModelAndView(form);
166     }
167 
168     /**
169      * Refresh and set server messages
170      *
171      * @param form
172      * @param result
173      * @param request
174      * @param response
175      * @return
176      */
177     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=refreshWithServerMessages")
178     public ModelAndView refreshWithServerMessages(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
179             HttpServletRequest request, HttpServletResponse response) {
180         GlobalVariables.getMessageMap().putError("inputField4", "serverTestError");
181         GlobalVariables.getMessageMap().putWarning("inputField4", "serverTestWarning");
182         GlobalVariables.getMessageMap().putInfo("inputField4", "serverTestInfo");
183 
184         return getModelAndView(form);
185     }
186 
187     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=customRefresh")
188     public ModelAndView customRefresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
189             HttpServletRequest request, HttpServletResponse response) {
190         GlobalVariables.getMessageMap().addGrowlMessage("Test", "serverTestInfo");
191 
192         return getModelAndView(form);
193     }
194 
195     @Autowired
196     @Qualifier("demoFileControllerService")
197     @Override
198     public void setFileControllerService(FileControllerService fileControllerService) {
199         super.setFileControllerService(fileControllerService);
200     }
201 
202     @InitBinder
203     protected void initBinder(WebDataBinder binder) throws Exception {
204         binder.registerCustomEditor(List.class, "names", new UITestObjectEditor());
205     }
206 
207     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=submitMultiSelect")
208     public ModelAndView submitMultiSelect(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
209             HttpServletRequest request, HttpServletResponse response) {
210         Object obj = request.getParameter("names");
211         return getModelAndView(form);
212     }
213 
214     protected class UITestObjectEditor extends CustomCollectionEditor {
215         public UITestObjectEditor() {
216             super(List.class);
217         }
218 
219         @Override
220         protected Object convertElement(Object element) {
221             // not a very good example but shows converting string selected to UITestObject
222             KradSampleAppForm form = new KradSampleAppForm();
223             for (UITestObject to : form.getNames()) {
224                 if (to.getInputField1().equals(element)) {
225                     return to;
226                 }
227             }
228 
229             return null;
230         }
231 
232         @Override
233         public String getAsText() {
234             Object obj = this.getValue();
235 
236             if (obj == null) {
237                 return null;
238             }
239 
240             StringBuffer buf = new StringBuffer();
241             List<UITestObject> l = (List)obj;
242             for (UITestObject to : l) {
243                 buf.append(to.getInputField1().charAt(0)).append(",");
244             }
245 
246             if (buf.toString().length() > 0) {
247                 return buf.toString().substring(0, buf.toString().length()-1);
248             }
249             return buf.toString();
250         }
251     }
252 }