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