View Javadoc

1   /**
2    * Copyright 2005-2015 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 edu.sampleu.demo.kitchensink;
17  
18  import org.kuali.rice.krad.uif.UifConstants;
19  import org.kuali.rice.krad.uif.view.DialogManager;
20  import org.kuali.rice.krad.web.controller.UifControllerBase;
21  import org.kuali.rice.krad.web.form.UifFormBase;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.validation.BindingResult;
24  import org.springframework.web.bind.annotation.ModelAttribute;
25  import org.springframework.web.bind.annotation.RequestMapping;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  /**
32   * a controller for the configuration test view
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  @Controller
37  @RequestMapping(value = "/dialog-configuration-test")
38  public class DialogTestViewUifController extends UifControllerBase {
39  
40      @Override
41      protected UifFormBase createInitialForm(HttpServletRequest request) {
42          return new UifDialogTestForm();
43      }
44  
45      /**
46       * Displays page for testing dialogs
47       */
48      @Override
49      @RequestMapping(params = "methodToCall=start")
50      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
51              HttpServletRequest request, HttpServletResponse response) {
52          return super.start(form, result, request, response);
53      }
54  
55      /**
56       * Exercises the Dialog framework.
57       *
58       * <p>
59       * Asks a series of questions of the user while processing a client request. Demonstrates the ability to go back
60       * to the client bring up a Lightbox modal dialog.
61       * <br>
62       * Displays a few dialogs back to the user. First a yes/no dialog asking the user to pick an author.
63       * Depending on which author was chosen, the user is asked to select a book.
64       * The user is then given a chance to start the selection process over or continue on.
65       * </p>
66       *
67       * @param form - test form
68       * @param result - Spring form binding result
69       * @param request - http request
70       * @param response - http response
71       * @return
72       * @throws Exception
73       */
74      @RequestMapping(params = "methodToCall=save")
75      public ModelAndView save(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
76              HttpServletRequest request, HttpServletResponse response) throws Exception {
77          ModelAndView mv;
78  
79          // dialog names
80          String dialog1 = "chooseAuthorDialog";
81          String dialog2a = "chooseEastmanBookDialog";
82          String dialog2b = "chooseSeussBookDialog";
83          String dialog3 = "myRestart";
84  
85          // local copies of dialog answers
86          boolean whichAuthor = false;
87          String chosenBook;
88          boolean doRestart = false;
89  
90          // choose which author
91          if (!hasDialogBeenAnswered(dialog1, form)){
92              // redirect back to client to display lightbox
93              return showDialog(dialog1, form, request, response);
94          }
95          whichAuthor = getBooleanDialogResponse(dialog1, form, request, response);
96  
97          // continue on here if they answered the the first question
98          if (whichAuthor){
99              form.setField1("You Selected: P.D. Eastman");
100 
101             // popup a 2nd consecutive dialog
102             if (!hasDialogBeenAnswered(dialog2a,form)){
103                 // redirect back to client to display lightbox
104                 return showDialog(dialog2a, form, request, response);
105             }
106             chosenBook = form.getDialogManager().getDialogExplanation(dialog2a);
107 
108             // return back to the client displaying the choices
109             form.setField1("You selected: "+chosenBook+" by P.D. Eastman. Here we go...");
110         } else {
111             form.setField1("You Selected: Dr. Seuss");
112             // in this case, return to client and wait for them to submit again before showing next dialog
113             // In the above case, the 1st and 2nd dialog are displayed consecutively before returning to the client.
114             // But in this example, we return to the client and wait for them to hit the submit button again
115             if (!hasDialogBeenDisplayed(dialog2b, form)){
116                 form.getDialogManager().addDialog(dialog2b, form.getMethodToCall());
117                 return getUIFModelAndView(form, "DialogView-Page1");
118             }
119 
120             // now display the dialog to choose which book
121             if (!hasDialogBeenAnswered(dialog2b, form)){
122                 return showDialog(dialog2b, form, request, response);
123             }
124             chosenBook = form.getDialogManager().getDialogExplanation(dialog2b);
125 
126             // display which story the user has selected
127             form.setField1("You selected: "+chosenBook+"  by Dr. Seuss. Here we go...");
128         }
129 
130         // Wait at the client page for another page submit
131         if (!hasDialogBeenDisplayed(dialog3, form)) {
132                 form.getDialogManager().addDialog(dialog3, form.getMethodToCall());
133                 return getUIFModelAndView(form, "DialogView-Page1");
134         };
135 
136         // Ask them if they want to start over
137         if (!hasDialogBeenAnswered(dialog3,form)){
138             return showDialog(dialog3, form, request, response);
139         }
140         doRestart = getBooleanDialogResponse(dialog3, form, request, response);
141 
142         // clear the dialog manager entries, so when we come back, we'll ask all the questions again
143         if (doRestart){
144             form.getDialogManager().removeAllDialogs();
145             form.setField1("Ok, let's start over.");
146             return getUIFModelAndView(form, "DialogView-Page1");
147         }
148 
149         // we're done, go to the next page
150         return getUIFModelAndView(form, "DialogView-Page2");
151     }
152 
153     /**
154      * not used at this time
155      *
156      * @param form - test form
157      * @param result - Spring form binding result
158      * @param request - http request
159      * @param response - http response
160      * @return
161      */
162     @RequestMapping(params = "methodToCall=goBack")
163     public ModelAndView goBack(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
164             HttpServletRequest request, HttpServletResponse response) {
165 
166 //      TODO: Put "Are Your Sure?" dialog here
167         form.getDialogManager().removeAllDialogs();
168         form.setField1("Ok, let's start over.");
169         return getUIFModelAndView(form, "DialogView-Page1");
170     }
171 
172     /**
173      * Test method for a controller that invokes a dialog lightbox.
174      *
175      * @param form - test form
176      * @param result - Spring form binding result
177      * @param request - http request
178      * @param response - http response
179      * @return
180      * @throws Exception
181      */
182     @RequestMapping(params = "methodToCall=" + "doRadioDialogExample")
183     public ModelAndView doSomething(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
184             HttpServletRequest request, HttpServletResponse response) throws Exception {
185         String dialog1 = "sampleRadioButtonDialog";
186         if (!hasDialogBeenAnswered(dialog1, form)){
187             // redirect back to client to display lightbox
188             return showDialog(dialog1, form, request, response);
189         }
190         // Get value from chosen radio button
191         String choice = form.getDialogManager().getDialogExplanation(dialog1);
192         if (choice == null){
193             form.setField1("You didn't select one of the radio buttons");
194         } else {
195             form.setField1("You chose Radio Option "+choice);
196         }
197 
198         // clear dialog history so they can press the button again
199         form.getDialogManager().removeDialog(dialog1);
200         // reload page1
201         return getUIFModelAndView(form, "DialogView-Page1");
202     }
203 
204     /**
205      * Test method for a controller that invokes a dialog lightbox.
206      *
207      * @param form - test form
208      * @param result - Spring form binding result
209      * @param request - http request
210      * @param response - http response
211      * @return
212      * @throws Exception
213      */
214     @RequestMapping(params = "methodToCall=" + "doOkCancelExample")
215     public ModelAndView doOKCancelExample(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
216             HttpServletRequest request, HttpServletResponse response) throws Exception {
217         String dialog1 = "preDefinedDialogOkCancel";
218         if (!hasDialogBeenAnswered(dialog1, form)){
219             // redirect back to client to display lightbox
220             return showDialog(dialog1, form, request, response);
221         }
222         // Get user choice
223         boolean choice = getBooleanDialogResponse(dialog1, form, request, response);
224         StringBuilder sb = new StringBuilder("You selected ");
225         sb.append((choice)?"OK":"Cancel");
226         form.setField1(sb.toString());
227 
228         // clear dialog history so they can press the button again
229         form.getDialogManager().removeDialog(dialog1);
230         // reload page1
231         return getUIFModelAndView(form, "DialogView-Page1");
232     }
233 
234     /**
235      * Test method for a controller that invokes a dialog lightbox.
236      *
237      * @param form - test form
238      * @param result - Spring form binding result
239      * @param request - http request
240      * @param response - http response
241      * @return
242      * @throws Exception
243      */
244     @RequestMapping(params = "methodToCall=" + "doRegularGroupAsDialog")
245     public ModelAndView doRegularGroupAsDialog(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
246             HttpServletRequest request, HttpServletResponse response) throws Exception {
247         String dialog1 = "myRegularGroup";
248         if (!hasDialogBeenAnswered(dialog1, form)){
249             // redirect back to client to display lightbox
250             return showDialog(dialog1, form, request, response);
251         }
252         // Get value from chosen radio button
253         boolean choice = getBooleanDialogResponse(dialog1, form, request, response);
254         StringBuilder sb = new StringBuilder("You selected ");
255         sb.append((choice)?"Yes":"No");
256         form.setField1(sb.toString());
257 
258         // clear dialog history so they can press the button again
259         form.getDialogManager().removeDialog(dialog1);
260         // reload page1
261         return getUIFModelAndView(form, "DialogView-Page1");
262     }
263 
264     /**
265      * Test method for a controller that invokes a dialog to test expression evaluation.
266      *
267      * @param form - test form
268      * @param result - Spring form binding result
269      * @param request - http request
270      * @param response - http response
271      * @return
272      * @throws Exception
273      */
274     @RequestMapping(params = "methodToCall=" + "testExpressionDialog")
275     public ModelAndView testExpressionDialog(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
276             HttpServletRequest request, HttpServletResponse response) throws Exception {
277         String dialog4 = "schedulingConfirmDialog";
278         if (!hasDialogBeenAnswered(dialog4, form)) {
279             // set the value for field3 which will be evaluated
280             form.setField3("TestVal");
281 
282             // redirect back to client to display lightbox
283             return showDialog(dialog4, form, request, response);
284         }
285 
286         // clear dialog history so they can press the button again
287         form.getDialogManager().removeDialog(dialog4);
288 
289         // reload page1
290         return getUIFModelAndView(form, "DialogView-Page1");
291     }
292 
293     /**
294      * Test method for a controller that invokes a dialog lightbox.
295      *
296      * @param form - test form
297      * @param result - Spring form binding result
298      * @param request - http request
299      * @param response - http response
300      * @return
301      * @throws Exception
302      */
303     @RequestMapping(params = "methodToCall=" + "doExtendedDialog")
304     public ModelAndView doExtendedDialog(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
305             HttpServletRequest request, HttpServletResponse response) throws Exception {
306         String dialog1 = "extendedDialogGroup";
307         if (!hasDialogBeenAnswered(dialog1, form)){
308             // redirect back to client to display lightbox
309             return showDialog(dialog1, form, request, response);
310         }
311         // Get value from chosen radio button
312         boolean choice = getBooleanDialogResponse(dialog1, form, request, response);
313         StringBuilder sb = new StringBuilder("You selected ");
314         sb.append((choice)?"Yes":"No");
315         form.setField1(sb.toString());
316 
317         // clear dialog history so they can press the button again
318         form.getDialogManager().removeDialog(dialog1);
319         // reload page1
320         return getUIFModelAndView(form, "DialogView-Page1");
321     }
322     /**
323          * Test method for a controller that displays the response in a ightbox.
324          *
325          * @param form - test form
326          * @param result - Spring form binding result
327          * @param request - http request
328          * @param response - http response
329          * @return
330          * @throws Exception
331          */
332         @RequestMapping(params = "methodToCall=" + "doResponseInLightBox")
333         public ModelAndView doResponseInLightBox(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
334                 HttpServletRequest request, HttpServletResponse response) throws Exception {
335             String dialog1 = "myRegularGroup";
336             if (!hasDialogBeenAnswered(dialog1, form)){
337                 // redirect back to client to display lightbox
338                 return showDialog(dialog1, form, request, response);
339             }
340             // Get value from chosen radio button
341             boolean choice = getBooleanDialogResponse(dialog1, form, request, response);
342             StringBuilder sb = new StringBuilder("You selected ");
343             sb.append((choice)?"Yes":"No");
344             form.setField1(sb.toString());
345 
346             // clear dialog history so they can press the button again
347             form.getDialogManager().removeDialog(dialog1);
348             // reload page1
349             return getUIFModelAndView(form, "DialogView-Page1");
350         }
351 
352 }