View Javadoc

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