View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by bobhurt on 8/7/12
16   */
17  package org.kuali.student.common.kitchensink;
18  
19  import org.kuali.rice.krad.util.GlobalVariables;
20  import org.kuali.rice.krad.web.controller.UifControllerBase;
21  import org.kuali.rice.krad.web.form.UifFormBase;
22  import org.kuali.student.common.uif.util.GrowlIcon;
23  import org.kuali.student.common.uif.util.KSUifUtils;
24  import org.springframework.stereotype.Controller;
25  import org.springframework.validation.BindingResult;
26  import org.springframework.web.bind.annotation.ModelAttribute;
27  import org.springframework.web.bind.annotation.RequestMapping;
28  import org.springframework.web.bind.annotation.RequestMethod;
29  import org.springframework.web.bind.annotation.RequestParam;
30  import org.springframework.web.servlet.ModelAndView;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * This class //TODO ...
41   *
42   * @author Kuali Student Team
43   */
44  @Controller
45  @RequestMapping(value = "/kitchensink")
46  public class KitchenSinkController extends UifControllerBase {
47  
48      /**
49       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
50       */
51      @Override
52      protected KitchenSinkForm createInitialForm(HttpServletRequest request) {
53          return new KitchenSinkForm();
54      }
55  
56      @Override
57      @RequestMapping(params = "methodToCall=start")
58      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
59                                HttpServletRequest request, HttpServletResponse response) {
60          KitchenSinkForm uiTestForm = (KitchenSinkForm) form;
61  
62          // pre-select option key "3" in checkbox controls
63          uiTestForm.setCheckboxSelections(Arrays.asList("3"));
64          // pre-select option key "2" in radio button controls
65          uiTestForm.setRadioButtonSelection("2");
66          // pre-select option key "2" in dropdown controls
67          uiTestForm.setDropdownSelection("2");
68  
69          return getUIFModelAndView(uiTestForm);
70      }
71  
72      @RequestMapping(params = "methodToCall=collection")
73      public ModelAndView collection(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
74                                     HttpServletRequest request, HttpServletResponse response) {
75  
76          List<KitchenSinkFormCollection1> collectionList = new ArrayList<KitchenSinkFormCollection1>();
77          collectionList.add(new KitchenSinkFormCollection1("Item #1", "A primary item", "2001-01-01"));
78          collectionList.add(new KitchenSinkFormCollection1("John Adams", "A founding father", "1735-10-30"));
79          collectionList.add(new KitchenSinkFormCollection1("Big Bang Theory", "A funny show", "2007-09-24"));
80          collectionList.add(new KitchenSinkFormCollection1("Chainbreaker IPA", "A tasty beverage", "2011-06-09"));
81          form.setCollection(collectionList);
82  
83          // Collections.xml has been removed
84          // for Collections.xml; same collection property causes validation problems
85          //List<KitchenSinkFormCollection1> collectionList2 = KitchenSinkFormCollection1.clone(collectionList);
86          //form.setCollection2(KitchenSinkFormCollection1.clone(collectionList));
87  
88          return getUIFModelAndView(form);
89      }
90  
91      /* test method for CollectionTest sandbox/test view
92      @RequestMapping(params = "methodToCall=subcollection")
93      public ModelAndView subcollection(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
94                                      HttpServletRequest request, HttpServletResponse response) {
95  
96          List<KitchenSinkFormCollection2> subCollectionList = new ArrayList<KitchenSinkFormCollection2>();
97          subCollectionList.add(new KitchenSinkFormCollection2("A","W","02:00 PM","04:50 PM"));
98          subCollectionList.add(new KitchenSinkFormCollection2("B","Tu","05:00 PM","07:50 PM"));
99          subCollectionList.add(new KitchenSinkFormCollection2("C","MWF","09:00 AM","09:50 AM"));
100 
101         List<KitchenSinkFormCollection1> collectionList = new ArrayList<KitchenSinkFormCollection1>();
102         KitchenSinkFormCollection1 ksFormCollection =
103                 new KitchenSinkFormCollection1("ENGL102", "Writing from Sources III", "1999-12-31");
104         ksFormCollection.setList1(subCollectionList);
105         collectionList.add(ksFormCollection);
106 
107         collectionList.add(new KitchenSinkFormCollection1("ENGL103", "Writing from Sources IV", "1999-12-31"));
108         collectionList.add(new KitchenSinkFormCollection1("ENGL104", "English for International Teaching Assistants", "1999-12-31"));
109         form.setCollection(collectionList);
110 
111         return getUIFModelAndView(form);
112     } //*/
113 
114     @RequestMapping(params = "methodToCall=collectionOne")
115     public ModelAndView collectionOne(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
116                                    HttpServletRequest request, HttpServletResponse response) {
117 
118         List<KitchenSinkFormCollection1> collectionList = new ArrayList<KitchenSinkFormCollection1>();
119         collectionList.add(new KitchenSinkFormCollection1("A", "First letter of the alphabet", "1970-01-01"));
120         form.setCollection(collectionList);
121 
122         return getUIFModelAndView(form);
123     }
124 
125     @RequestMapping(params = "methodToCall=collectionTerm")
126     public ModelAndView collectionTerm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
127                                       HttpServletRequest request, HttpServletResponse response) {
128 
129         List<KitchenSinkFormCollection1> collectionList = new ArrayList<KitchenSinkFormCollection1>();
130         collectionList.add(new KitchenSinkFormCollection1("Fall 1997", "kuali.atp.type.Fall", "1997-09-01"));
131         collectionList.add(new KitchenSinkFormCollection1("Winter 2000", "kuali.atp.type.Winter", "2000-01-01"));
132         form.setCollection(collectionList);
133 
134         return getUIFModelAndView(form);
135     }
136 
137     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLineCollectionAsForm")
138     public ModelAndView addLineCollectionAsForm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
139                                 HttpServletRequest request, HttpServletResponse response) {
140         List<KitchenSinkFormCollection1> collectionList = form.getCollection();
141         Map<String, Object> newCollectionLines = form.getNewCollectionLines();
142         if (null != newCollectionLines && !newCollectionLines.isEmpty()) {
143             for (Map.Entry<String, Object> entry : newCollectionLines.entrySet()) {
144                 //
145                 // Code goes here to save new collection line to database...
146                 //
147                 // for this example, just assign a unique ID
148                 KitchenSinkFormCollection1 collection = (KitchenSinkFormCollection1)entry.getValue();
149                 collection.setId(KitchenSinkFormCollection1.assignId());
150             }
151             GlobalVariables.getMessageMap().addGrowlMessage("NOTE", "kitchensink.addLine");
152         }
153 
154         return super.addLine(form, result, request, response);
155     }
156 
157     @RequestMapping(params = "methodToCall=getActivities")
158     public ModelAndView getActivities(
159             @RequestParam(value = "actionParameters", required = false) Map<String, Integer> actionParameters,
160             @ModelAttribute("KualiForm") KitchenSinkForm form,
161             BindingResult result,
162             HttpServletRequest request,
163             HttpServletResponse response) throws Exception {
164 
165         List<KitchenSinkMockActivityData> activities = new ArrayList<KitchenSinkMockActivityData>();
166 
167         Integer index = actionParameters.get("index");
168 
169         if (displayScheduleList != null && !(displayScheduleList.size() < index.intValue())) {
170             activities = displayScheduleList.get(index.intValue()).getActivities();
171         }
172 
173         form.setActivityList(activities);
174         return getUIFModelAndView(form);
175     }
176 
177     @RequestMapping(params = "methodToCall=growl")
178     public ModelAndView growl(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
179                               HttpServletRequest request, HttpServletResponse response) {
180         GlobalVariables.getMessageMap().addGrowlMessage("", "kitchensink.custom", "This is an example of a growl with no icon.");
181         return getUIFModelAndView(form);
182     }
183 
184     @RequestMapping(params = "methodToCall=growlError")
185     public ModelAndView growlError(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
186                                    HttpServletRequest request, HttpServletResponse response) {
187         KSUifUtils.addGrowlMessageIcon(GrowlIcon.ERROR, "kitchensink.custom", "This is an example of an error growl.");
188         return getUIFModelAndView(form);
189     }
190 
191     @RequestMapping(params = "methodToCall=growlInfo")
192     public ModelAndView growlInfo(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
193                                   HttpServletRequest request, HttpServletResponse response) {
194         KSUifUtils.addGrowlMessageIcon(GrowlIcon.INFORMATION, "kitchensink.custom", "This is an example of an information growl.");
195         return getUIFModelAndView(form);
196     }
197 
198     @RequestMapping(params = "methodToCall=growlSuccess")
199     public ModelAndView growlSuccess(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
200                                      HttpServletRequest request, HttpServletResponse response) {
201         KSUifUtils.addGrowlMessageIcon(GrowlIcon.SUCCESS, "kitchensink.custom", "This is an example of a success growl.");
202         return getUIFModelAndView(form);
203     }
204 
205     @RequestMapping(params = "methodToCall=growlWarning")
206     public ModelAndView growlWarning(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
207                                      HttpServletRequest request, HttpServletResponse response) {
208         KSUifUtils.addGrowlMessageIcon(GrowlIcon.WARNING, "kitchensink.custom", "This is an example of a warning growl.");
209         return getUIFModelAndView(form);
210     }
211 
212     @RequestMapping(params = "methodToCall=dialogButtonConfirm")
213     public ModelAndView dialogButtonConfirm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
214             HttpServletRequest request, HttpServletResponse response) throws Exception {
215         String dialogId = "messageBoxDialog";
216 
217         if (!hasDialogBeenAnswered(dialogId, form)) {
218             return showDialog(dialogId, form, request, response);
219         }
220 
221         //boolean isDialogResponseTrue = getBooleanDialogResponse(dialogId, form, request, response);
222         String dialogResponse = getStringDialogResponse(dialogId, form, request, response);
223         if ("Y".equals(dialogResponse)) {
224             KSUifUtils.addGrowlMessageIcon(GrowlIcon.SUCCESS, "kitchensink.custom", "You clicked the button.");
225         }
226         else {
227             KSUifUtils.addGrowlMessageIcon(GrowlIcon.ERROR, "kitchensink.custom", "Be more careful next time.");
228         }
229 
230         // clear dialog history so user can press the button again
231         form.getDialogManager().resetDialogStatus(dialogId);
232 
233         return getUIFModelAndView(form);
234     }
235 
236 
237     @RequestMapping(params = "methodToCall=customDialog")
238     public ModelAndView customDialog(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
239                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
240         String dialogId = "lightboxDialog2";
241 
242         if (!hasDialogBeenAnswered(dialogId, form)) {
243             return showDialog(dialogId, form, request, response);
244         }
245 
246         // clear dialog history so user can press the button again
247         form.getDialogManager().resetDialogStatus(dialogId);
248         return getUIFModelAndView(form);
249     }
250 
251 
252     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=saveForm")
253     public ModelAndView saveForm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
254             HttpServletRequest request, HttpServletResponse response) {
255         //
256         // Code goes here to edit form and exit if any errors exist
257         //
258         // Code goes here to save form to database
259         //
260         GlobalVariables.getMessageMap().addGrowlMessage("NOTE", "kitchensink.saveForm");
261         return getUIFModelAndView(form);
262     }
263 
264     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=saveLightboxForm")
265     public ModelAndView saveLightboxForm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
266                                          HttpServletRequest request, HttpServletResponse response)
267             throws Exception {
268         String dialogId = "lightboxDialog1";
269 
270         // clear dialog history so user can press the button again
271         //form.getDialogManager().resetDialogStatus(dialogId);
272         form.setStringField1("Lightbox Form Saved");
273 
274         KSUifUtils.addGrowlMessageIcon(GrowlIcon.INFORMATION, "kitchensink.custom", "Lightbox form saved.");
275         // growl doesn't show because returnFromLightbox() executes performRedirect(), w/o growl params:
276         return returnFromLightbox(form, result, request, response); //also, hidden bean briefly shows
277         //return getUIFModelAndView(form);//shows growl,lightbox remains,hidden bean displays
278         //return refresh(form, result, request, response);//same as getUIFModelAndView
279     }
280 
281     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=processFormRowSelection")
282     public ModelAndView processFormRowSelection(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
283                                              HttpServletRequest request, HttpServletResponse response) {
284         //
285         // This method should do something with the selected rows, like send them to another page or maybe
286         // save them, or...  For this example the selected row name will just be added to a growl message.
287         //
288         StringBuilder sb = new StringBuilder();
289         for (KitchenSinkFormCollection1 collection : form.getCollection()) {
290             if (collection.getSelected()) {
291                 // do something with the selected rows
292                 if (sb.length() > 0) {
293                     sb.append(", ");
294                 }
295                 sb.append(collection.getName());
296             }
297         }
298         GlobalVariables.getMessageMap().addGrowlMessage("PROCESSED:", "kitchensink.custom", sb.toString());
299         return getUIFModelAndView(form);
300     }
301 
302     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=popoverMethodToCall")
303     public ModelAndView popoverMethodToCall(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
304                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
305         form.setStringField2(
306                 "Text set in server-side method before the popover is displayed.  "
307             +   form.getStringField2()
308             );
309         return getUIFModelAndView(form);
310     }
311 
312     private int popupCount = 0;
313     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=popoverRefreshBeforeDisplay")
314     public ModelAndView popoverRefreshBeforeDisplay(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
315                                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
316         form.setStringField3(Integer.toString(++popupCount));
317         return getUIFModelAndView(form);
318     }
319 
320     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=processPopoverForm")
321     public ModelAndView processPopoverForm(@ModelAttribute("KualiForm") KitchenSinkForm form, BindingResult result,
322                                            HttpServletRequest request, HttpServletResponse response) {
323         GlobalVariables.getMessageMap().addGrowlMessage("NOTE", "kitchensink.custom", "processPopoverForm");
324         if ("error".equals(form.getStringField1().toLowerCase())) {
325                 GlobalVariables.getMessageMap().putErrorForSectionId("stringField1","kitchensink.custom","Popover form with error is displayed automatically.");
326         }
327         return getUIFModelAndView(form);
328     }
329 
330     List<KitchenSinkMockDisplayScheduleData> displayScheduleList = KitchenSinkMockDisplayScheduleData.mockTestData();
331 
332 }