001    /**
002     * Copyright 2005-2012 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     */
016    package edu.sampleu.demo.kitchensink;
017    
018    import org.kuali.rice.krad.uif.view.DialogManager;
019    import org.kuali.rice.krad.web.controller.UifControllerBase;
020    import org.kuali.rice.krad.web.form.UifFormBase;
021    import org.springframework.stereotype.Controller;
022    import org.springframework.validation.BindingResult;
023    import org.springframework.web.bind.annotation.ModelAttribute;
024    import org.springframework.web.bind.annotation.RequestMapping;
025    import org.springframework.web.servlet.ModelAndView;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * a controller for the configuration test view
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    @Controller
036    @RequestMapping(value = "/dialog-configuration-test")
037    public class DialogTestViewUifController extends UifControllerBase {
038    
039        @Override
040        protected UifFormBase createInitialForm(HttpServletRequest request) {
041            return new UifDialogTestForm();
042        }
043    
044        /**
045         * Displays page for testing dialogs
046         */
047        @Override
048        @RequestMapping(params = "methodToCall=start")
049        public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
050                HttpServletRequest request, HttpServletResponse response) {
051            return super.start(form, result, request, response);
052        }
053    
054        /**
055         * Exercises the Dialog framework.
056         *
057         * <p>
058         * Asks a series of questions of the user while processing a client request. Demonstrates the ability to go back
059         * to the client bring up a Lightbox modal dialog.
060         * <br>
061         * Displays a few dialogs back to the user. First a yes/no dialog asking the user to pick an author.
062         * Depending on which author was chosen, the user is asked to select a book.
063         * The user is then given a chance to start the selection process over or continue on.
064         * </p>
065         *
066         * @param form - test form
067         * @param result - Spring form binding result
068         * @param request - http request
069         * @param response - http response
070         * @return
071         * @throws Exception
072         */
073        @RequestMapping(params = "methodToCall=save")
074        public ModelAndView save(@ModelAttribute("KualiForm") UifDialogTestForm form, BindingResult result,
075                HttpServletRequest request, HttpServletResponse response) throws Exception {
076            ModelAndView mv;
077    
078            // dialog names
079            String dialog1 = "chooseAuthorDialog";
080            String dialog2a = "chooseEastmanBookDialog";
081            String dialog2b = "chooseSeussBookDialog";
082            String dialog3 = "myRestart";
083    
084            // local copies of dialog answers
085            boolean whichAuthor = false;
086            String chosenBook;
087            boolean doRestart = false;
088    
089            // choose which author
090            if (!hasDialogBeenAnswered(dialog1, form)){
091                // redirect back to client to display lightbox
092                return showDialog(dialog1, form, request, response);
093            }
094            whichAuthor = getBooleanDialogResponse(dialog1, form, request, response);
095    
096            // continue on here if they answered the the first question
097            if (whichAuthor){
098                form.setField1("You Selected: P.D. Eastman");
099    
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    }