View Javadoc
1   /**
2    * Copyright 2005-2014 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 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      *
206      * @param dialogId
207      * @param returnMethod
208      */
209     public void setDialogReturnMethod(String dialogId, String returnMethod){
210         DialogInfo dialogInfo = dialogs.get(dialogId);
211         dialogInfo.returnMethod = returnMethod;
212         dialogs.put(dialogId, dialogInfo);
213     }
214 
215     /**
216      * Creates a new DialogInfo record and adds it to the list of dialogs
217      * used in the view
218      *
219      * <p>
220      * New dialog entry is initialized to asked=false, answered=false.
221      * If the dialog already has a record, nothing is performed.
222      * </p>
223      *
224      * @param dialogId String name identifying the dialog
225      */
226     public void addDialog(String dialogId, String returnMethod){
227         DialogInfo dialogInfo = new DialogInfo(dialogId, returnMethod);
228         dialogInfo.asked = true;
229         dialogs.put(dialogId, dialogInfo);
230         setCurrentDialogId(dialogId);
231     }
232 
233     /**
234      * Removes a dialog from the list of dialogs used in this vew.
235      *
236      * <p>
237      * If the dialog is in the list, it is removed.
238      * If the dialog is not in the list, nothing is performed.
239      * </p>
240      *
241      * @param dialogId String identifying the dialog to be removed
242      */
243     public void removeDialog(String dialogId){
244         if (dialogs.containsKey(dialogId)){
245             dialogs.remove(dialogId);
246         }
247     }
248 
249     /**
250      * Removes all dialogs from the list of dialogs used in this vew.
251      *
252      */
253     public void removeAllDialogs(){
254         dialogs.clear();
255     }
256 
257     /**
258      * Sets the status of the dialog tracking record to indicate that this dialog
259      * has not yet been asked or answered.
260      *
261      * @param dialogId String identifier for the dialog
262      */
263     public void resetDialogStatus(String dialogId){
264         String returnMethod = getDialogReturnMethod(dialogId);
265         dialogs.put(dialogId, new DialogInfo(dialogId, returnMethod));
266     }
267 
268     /**
269      * Gets the Map used to track dialog interactions related to the view
270      *
271      * @return a Map of DialogInfo records
272      */
273     public Map<String, DialogInfo> getDialogs() {
274         return dialogs;
275     }
276 
277     /**
278      * Sets the Map of DialogInfo records used to track modal dialog interactions
279      * within a view
280      *
281      * @param dialogs a Map of DialogInfo records keyed by the dialog id
282      */
283     public void setDialogs(Map<String, DialogInfo> dialogs) {
284         this.dialogs = dialogs;
285     }
286 
287     /**
288      * Gets the name of the currently active dialog
289      *
290      * @return the name of the current dialog
291      */
292     public String getCurrentDialogId() {
293         return currentDialogId;
294     }
295 
296     /**
297      * Sets the name of the currently active dialog
298      *
299      * @param currentDialogId the name of the dialog
300      */
301     public void setCurrentDialogId(String currentDialogId) {
302         this.currentDialogId = currentDialogId;
303     }
304 
305 }