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 org.kuali.rice.krad.uif.view;
17  
18  import org.springframework.util.StringUtils;
19  
20  import java.io.Serializable;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /**
25   * Manages the status of any modal dialogs that are used in the view.
26   *
27   * <p>
28   * Keeps track of which modal dialogs have been asked and/or answered
29   * during the life cycle of a view.
30   * </p>
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class DialogManager implements Serializable {
35      private static final long serialVersionUID = 4627667603510159528L;
36  
37      private static final String TRUE_VALUES = "/true/yes/y/on/1/";
38      private static final String FALSE_VALUES = "/false/no/n/off/0/";
39  
40      /**
41       * Status information record used to track dialog status
42       */
43      private static class DialogInfo implements Serializable{
44          private static final long serialVersionUID = 2779403853894669510L;
45  
46          private String dialogId;
47          private boolean asked;
48          private boolean answered;
49          private String answer;
50          private String explanation;
51          private String returnMethod;
52  
53          public DialogInfo(String dialogId, String returnMethod){
54              this.dialogId = dialogId;
55              this.asked = false;
56              this.answered = false;
57              this.answer = null;
58              this.explanation = null;
59              this.returnMethod = returnMethod;
60          }
61      }
62  
63      private String currentDialogId;
64      private Map<String, DialogInfo> dialogs;
65  
66      /**
67       * Constructs new instance
68       */
69      public DialogManager(){
70          // init dialogs
71          dialogs = new HashMap<String, DialogInfo>();
72      }
73  
74      /**
75       * Indicates whether the named dialog has already been presented to the user
76       *
77       * @param dialogId - the key identifying the specific dialog
78       * @return true if dialog has been displayed, false if not
79       */
80      public boolean hasDialogBeenDisplayed(String dialogId){
81          if (dialogs.containsKey(dialogId)){
82              return dialogs.get(dialogId).asked;
83          }
84          return false;
85      }
86  
87      /**
88       * Indicates whether the named dialog has alread been answered by the user
89       *
90       * @param dialogId - name of the dialog in questions
91       * @return - true if the dialog has been answered by the user
92       */
93      public boolean hasDialogBeenAnswered(String dialogId){
94          if (dialogs.containsKey(dialogId)){
95              return dialogs.get(dialogId).answered;
96          }
97          return false;
98      }
99  
100     /**
101      * Gets the answer previously entered by the user when responding to this dialog
102      *
103      * <p>
104      * Returns the key value of the option chosen by the user.
105      * Returns null if the dialog has not yet been asked, or if the user has not yet
106      * responded.
107      * </p>
108      *
109      * @param dialogId - a String identifying the dialog
110      * @return the key String of the option KeyValue chosen by the user
111      */
112     public String getDialogAnswer(String dialogId){
113         if (hasDialogBeenAnswered(dialogId)){
114             return dialogs.get(dialogId).answer;
115         }
116         return null;
117     }
118 
119     /**
120      * Sets the answer chosen by the user when responding to the dialog
121      *
122      * @param dialogId - id of the dialog
123      * @param answer - value chosen by the user
124      */
125     public void setDialogAnswer(String dialogId, String answer){
126         DialogInfo dialogInfo = dialogs.get(dialogId);
127         if (dialogInfo != null){
128             dialogInfo.answer = answer;
129             dialogInfo.answered = true;
130             dialogs.put(dialogId,dialogInfo);
131         }
132     }
133 
134     /**
135      * Gets the text String value of the explanation input field
136      *
137      * @param dialogId - dialog identifier
138      * @return String representing user text input entered into the dialog
139      */
140     public String getDialogExplanation(String dialogId){
141         return dialogs.get(dialogId).explanation;
142     }
143 
144     /**
145      * Sets the exlanation text String obtained from the explanation input field
146      * for the dialog
147      *
148      * @param dialogId - identifier of the dialog
149      * @param explanation - text String from input field
150      */
151     public void setDialogExplanation(String dialogId, String explanation){
152         DialogInfo dialogInfo = dialogs.get(dialogId);
153         if (dialogInfo != null){
154             dialogInfo.explanation = explanation;
155             dialogs.put(dialogId,dialogInfo);
156         }
157     }
158 
159     /**
160      * Indicates whethe the user answered affirmatively to the question
161      *
162      * <p>
163      * The answer string is the key used for the option key/value pair selected by the user.
164      * This assumes that the developer used one of the common keys used for yes/no questions.
165      * The answer is checked to see if it is one of the acceptable values for "Yes". If so,
166      * the method returns true. False if not.
167      * Also returns false, if the question has not even been asked of the user.
168      * </p>
169      *
170      * @param dialogId
171      * @return true if the user answered the modal dialog affirmatively, false if answered negatively.
172      *   also returns false if the questions hasn't yet been answered.
173      */
174     public boolean wasDialogAnswerAffirmative(String dialogId){
175         String answer = getDialogAnswer(dialogId);
176         if (answer != null){
177             StringBuilder builder = new StringBuilder();
178             builder.append("/").append(answer.toLowerCase()).append("/");
179             String input = builder.toString();
180             if(TRUE_VALUES.contains(builder.toString())) {
181                 return true;
182             }
183         }
184 
185         // TODO: Should we return false if question not even asked yet?
186         //       Or should we throw an exception?
187         return false;
188     }
189 
190     /**
191      * Retrieves the target method to redirect to when returning from a lightbox
192      *
193      * @param dialogId - identifies the dialog currently being handled
194      * @return String - controller method to call
195      */
196     public String getDialogReturnMethod(String dialogId){
197         if (hasDialogBeenAnswered(dialogId)){
198             return dialogs.get(dialogId).returnMethod;
199         }
200         return null;
201     }
202 
203     /**
204      * sets the return method to call after returning from dialog
205      * @param dialogId
206      * @param returnMethod
207      */
208     public void setDialogReturnMethod(String dialogId, String returnMethod){
209         DialogInfo dialogInfo = dialogs.get(dialogId);
210         dialogInfo.returnMethod = returnMethod;
211         dialogs.put(dialogId, dialogInfo);
212     }
213 
214     /**
215      * Creates a new DialogInfo record and adds it to the list of dialogs
216      * used in the view
217      *
218      * <p>
219      * New dialog entry is initialized to asked=false, answered=false.
220      * If the dialog already has a record, nothing is performed.
221      * </p>
222      *
223      * @param dialogId - String name identifying the dialog
224      */
225     public void addDialog(String dialogId, String returnMethod){
226         DialogInfo dialogInfo = new DialogInfo(dialogId, returnMethod);
227         dialogInfo.asked = true;
228         dialogs.put(dialogId, dialogInfo);
229         setCurrentDialogId(dialogId);
230     }
231 
232     /**
233      * Removes a dialog from the list of dialogs used in this vew.
234      *
235      * <p>
236      * If the dialog is in the list, it is removed.
237      * If the dialog is not in the list, nothing is performed.
238      * </p>
239      *
240      * @param dialogId - String identifying the dialog to be removed
241      */
242     public void removeDialog(String dialogId){
243         if (dialogs.containsKey(dialogId)){
244             dialogs.remove(dialogId);
245         }
246     }
247 
248     /**
249      * Removes all dialogs from the list of dialogs used in this vew.
250      *
251      */
252     public void removeAllDialogs(){
253         dialogs.clear();
254     }
255 
256     /**
257      * Sets the status of the dialog tracking record to indicate that this dialog
258      * has not yet been asked or answered.
259      *
260      * @param dialogId - String identifier for the dialog
261      */
262     public void resetDialogStatus(String dialogId){
263         String returnMethod = getDialogReturnMethod(dialogId);
264         dialogs.put(dialogId, new DialogInfo(dialogId, returnMethod));
265     }
266 
267     /**
268      * Gets the Map used to track dialog interactions related to the view
269      *
270      * @return a Map of DialogInfo records
271      */
272     public Map<String, DialogInfo> getDialogs() {
273         return dialogs;
274     }
275 
276     /**
277      * Sets the Map of DialogInfo records used to track modal dialog interactions
278      * within a view
279      *
280      * @param dialogs - a Map of DialogInfo records keyed by the dialog id.
281      */
282     public void setDialogs(Map<String, DialogInfo> dialogs) {
283         this.dialogs = dialogs;
284     }
285 
286     /**
287      * Gets the name of the currently active dialog
288      *
289      * @return - the name of the current dialog
290      */
291     public String getCurrentDialogId() {
292         return currentDialogId;
293     }
294 
295     /**
296      * Sets the name of the currently active dialog
297      *
298      * @param currentDialogId - the name of the dialog
299      */
300     public void setCurrentDialogId(String currentDialogId) {
301         this.currentDialogId = currentDialogId;
302     }
303 
304 }