1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
29
30
31
32
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
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
68
69 public DialogManager(){
70
71 dialogs = new HashMap<String, DialogInfo>();
72 }
73
74
75
76
77
78
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
89
90
91
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
102
103
104
105
106
107
108
109
110
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
121
122
123
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
136
137
138
139
140 public String getDialogExplanation(String dialogId){
141 return dialogs.get(dialogId).explanation;
142 }
143
144
145
146
147
148
149
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
161
162
163
164
165
166
167
168
169
170
171
172
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
186
187 return false;
188 }
189
190
191
192
193
194
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
205
206
207
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
217
218
219
220
221
222
223
224
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
235
236
237
238
239
240
241
242
243 public void removeDialog(String dialogId){
244 if (dialogs.containsKey(dialogId)){
245 dialogs.remove(dialogId);
246 }
247 }
248
249
250
251
252
253 public void removeAllDialogs(){
254 dialogs.clear();
255 }
256
257
258
259
260
261
262
263 public void resetDialogStatus(String dialogId){
264 String returnMethod = getDialogReturnMethod(dialogId);
265 dialogs.put(dialogId, new DialogInfo(dialogId, returnMethod));
266 }
267
268
269
270
271
272
273 public Map<String, DialogInfo> getDialogs() {
274 return dialogs;
275 }
276
277
278
279
280
281
282
283 public void setDialogs(Map<String, DialogInfo> dialogs) {
284 this.dialogs = dialogs;
285 }
286
287
288
289
290
291
292 public String getCurrentDialogId() {
293 return currentDialogId;
294 }
295
296
297
298
299
300
301 public void setCurrentDialogId(String currentDialogId) {
302 this.currentDialogId = currentDialogId;
303 }
304
305 }