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.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.krad.service.DataObjectMetaDataService;
21 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22 import org.kuali.rice.krad.uif.UifConstants;
23 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
24 import org.kuali.rice.krad.uif.util.ViewModelUtils;
25 import org.kuali.rice.krad.web.form.UifFormBase;
26
27 import javax.servlet.http.HttpServletRequest;
28 import java.io.Serializable;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLDecoder;
31 import java.net.URLEncoder;
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.List;
35
36
37
38
39
40
41
42 public class History implements Serializable {
43 private static final long serialVersionUID = -8279297694371557335L;
44 private static final Logger LOG = Logger.getLogger(History.class);
45
46 public static final String ENTRY_TOKEN = "$";
47 public static final String VAR_TOKEN = ",";
48
49 private boolean appendHomewardPath;
50 private boolean appendPassedHistory;
51
52 private HistoryEntry current;
53
54 private List<HistoryEntry> homewardPath;
55 private List<HistoryEntry> historyEntries;
56
57 public History() {
58 historyEntries = new ArrayList<HistoryEntry>();
59 }
60
61
62
63
64
65
66
67 public List<HistoryEntry> getHomewardPath() {
68 return this.homewardPath;
69 }
70
71
72
73
74 public void setHomewardPath(List<HistoryEntry> homewardPath) {
75 this.homewardPath = homewardPath;
76 }
77
78
79
80
81
82
83
84
85
86 public List<HistoryEntry> getHistoryEntries() {
87 return this.historyEntries;
88 }
89
90
91
92
93 public void setHistoryEntries(List<HistoryEntry> history) {
94 this.historyEntries = history;
95 }
96
97
98
99
100
101
102
103
104
105 public HistoryEntry getCurrent() {
106 return this.current;
107 }
108
109
110
111
112
113
114
115
116
117 private void setCurrent(String viewId, String pageId, String title, String url, String formKey) {
118 HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
119 current = entry;
120 }
121
122
123
124
125 public void setCurrent(HistoryEntry current) {
126 this.current = current;
127 }
128
129
130
131
132
133
134
135
136
137 public void buildHistoryFromParameterString(String parameterString) {
138 if (StringUtils.isNotEmpty(parameterString)) {
139 try {
140 parameterString = URLDecoder.decode(parameterString, "UTF-8");
141 } catch (UnsupportedEncodingException e) {
142 LOG.error("Error decoding history param", e);
143 }
144
145 historyEntries = new ArrayList<HistoryEntry>();
146 if (appendPassedHistory) {
147 String[] historyTokens = parameterString.split("\\" + ENTRY_TOKEN);
148 for (String token : historyTokens) {
149 String[] params = token.split(VAR_TOKEN);
150 pushToHistory(params[0], params[1], params[2], params[3], params[4]);
151 }
152 }
153 }
154
155 if (appendHomewardPath) {
156 historyEntries.addAll(homewardPath);
157 }
158 }
159
160
161
162
163
164
165
166
167 public String getHistoryParameterString() {
168 String historyString = "";
169 for (HistoryEntry e : historyEntries) {
170 if (historyEntries.indexOf(e) == 0) {
171 historyString = historyString + e.toParam();
172 } else {
173 historyString = historyString + ENTRY_TOKEN + e.toParam();
174 }
175 }
176
177
178 if (current != null) {
179 if (historyString.equals("")) {
180 historyString = historyString + current.toParam();
181 } else {
182 historyString = historyString + ENTRY_TOKEN + current.toParam();
183 }
184 }
185
186 try {
187 historyString = URLEncoder.encode(historyString, "UTF-8");
188 } catch (Exception e) {
189 LOG.error("Error encoding history param", e);
190 }
191
192 return historyString;
193 }
194
195
196
197
198
199
200
201
202
203
204
205 public List<HistoryEntry> getGeneratedBreadcrumbs() {
206 List<HistoryEntry> breadcrumbs = new ArrayList<HistoryEntry>();
207 for (int i = 0; i < historyEntries.size(); i++) {
208 if (i == 0) {
209 breadcrumbs.add(copyEntry(historyEntries.get(i)));
210 } else {
211 HistoryEntry breadcrumb = copyEntry(historyEntries.get(i));
212 String historyParam = "";
213 for (int j = 0; j < i; j++) {
214 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
215 }
216 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
217 try {
218 historyParam = URLEncoder.encode(historyParam, "UTF-8");
219 } catch (Exception e) {
220 LOG.error("Error encoding history param", e);
221 }
222
223 String url = "";
224 if (breadcrumb.getUrl().contains("?")) {
225 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam
226 + "&" + UifConstants.UrlParams.LAST_FORM_KEY + "=" + current.getFormKey();
227 } else {
228 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam
229 + "&" + UifConstants.UrlParams.LAST_FORM_KEY + "=" + current.getFormKey();
230 }
231
232 breadcrumb.setUrl(url);
233 breadcrumbs.add(breadcrumb);
234 }
235 }
236
237 return breadcrumbs;
238 }
239
240
241
242
243
244
245 public HistoryEntry getGeneratedCurrentBreadcrumb() {
246 if (current == null){
247 return new HistoryEntry();
248 }
249
250 HistoryEntry breadcrumb = copyEntry(current);
251 String historyParam = "";
252 for (int j = 0; j < historyEntries.size(); j++) {
253 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
254 }
255 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
256
257 try {
258 historyParam = URLEncoder.encode(historyParam, "UTF-8");
259 } catch (Exception e) {
260 LOG.error("Error encoding history param", e);
261 }
262
263 String url = "";
264 if (breadcrumb.getUrl().contains("?")) {
265 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
266 } else {
267 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
268 }
269 breadcrumb.setUrl(url);
270
271 return breadcrumb;
272 }
273
274
275
276
277
278
279
280 private HistoryEntry copyEntry(HistoryEntry e) {
281 return new HistoryEntry(e.getViewId(), e.getPageId(), e.getTitle(), e.getUrl(), e.getFormKey());
282 }
283
284
285
286
287
288
289
290
291
292
293
294 public void pushToHistory(String viewId, String pageId, String title, String url, String formKey) {
295 HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
296 historyEntries.add(entry);
297 }
298
299
300
301
302
303
304
305
306 public void setAppendHomewardPath(boolean appendHomewardPath) {
307 this.appendHomewardPath = appendHomewardPath;
308 }
309
310
311
312
313 public boolean isAppendHomewardPath() {
314 return appendHomewardPath;
315 }
316
317
318
319
320
321
322
323
324 public void setAppendPassedHistory(boolean appendPassedHistory) {
325 this.appendPassedHistory = appendPassedHistory;
326 }
327
328
329
330
331 public boolean isAppendPassedHistory() {
332 return appendPassedHistory;
333 }
334
335
336
337
338
339
340
341
342
343 @SuppressWarnings("unchecked")
344 public void setCurrent(UifFormBase form, HttpServletRequest request) {
345 if (!request.getMethod().equals("POST")) {
346 boolean showHomeValue = false;
347 boolean pageIdValue = false;
348 boolean formKeyValue = false;
349
350 String queryString = "";
351 String url = request.getRequestURL().toString();
352
353
354 Enumeration<String> params = request.getParameterNames();
355 while (params.hasMoreElements()) {
356 String key = params.nextElement();
357 if (!key.equals(UifConstants.UrlParams.HISTORY)) {
358 for (String value : request.getParameterValues(key)) {
359 queryString = queryString + "&" + key + "=" + value;
360 }
361 } else if (key.equals(UifConstants.UrlParams.PAGE_ID)) {
362 pageIdValue = true;
363 } else if (key.equals(UifConstants.UrlParams.SHOW_HOME)) {
364 showHomeValue = true;
365 } else if (key.equals(UifConstants.UrlParams.FORM_KEY)) {
366 formKeyValue = true;
367 }
368 }
369
370
371 if (StringUtils.isNotBlank(form.getFormKey()) && !formKeyValue) {
372 queryString = queryString + "&" + UifConstants.UrlParams.FORM_KEY + "=" + form.getFormKey();
373 }
374 if (StringUtils.isNotBlank(form.getPageId()) && !pageIdValue) {
375 queryString = queryString + "&" + UifConstants.UrlParams.PAGE_ID + "=" + form.getPageId();
376 }
377 if (!showHomeValue) {
378 queryString = queryString + "&" + UifConstants.UrlParams.SHOW_HOME + "=false";
379 }
380
381 queryString = queryString.replaceFirst("&", "");
382
383 if (StringUtils.isNotEmpty(queryString)) {
384 url = url + "?" + queryString;
385 }
386
387 this.setCurrent(form.getViewId(), form.getPageId(), buildViewTitle(form), url, form.getFormKey());
388 }
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 protected String buildViewTitle(UifFormBase form) {
406 View view = form.getView();
407 String title = view.getHeaderText();
408
409
410
411 String viewLabelPropertyName = view.getBreadcrumbTitlePropertyName();
412
413
414 if (StringUtils.isBlank(viewLabelPropertyName)) {
415 Class<?> dataObjectClass;
416 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
417 dataObjectClass = ObjectPropertyUtils.getPropertyType(form, view.getDefaultBindingObjectPath());
418 } else {
419 dataObjectClass = view.getFormClass();
420 }
421
422 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
423 if (dataObjectClass != null) {
424 viewLabelPropertyName = mds.getTitleAttribute(dataObjectClass);
425 }
426 }
427
428 String viewLabelPropertyPath = "";
429 if (StringUtils.isNotBlank(viewLabelPropertyName)) {
430
431 if (!viewLabelPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
432 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
433 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + viewLabelPropertyName;
434 }
435 } else {
436 viewLabelPropertyPath = StringUtils.removeStart(viewLabelPropertyName,
437 UifConstants.NO_BIND_ADJUST_PREFIX);
438 }
439 }
440 else {
441
442 Class<?> dataObjectClass;
443 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
444 dataObjectClass = ViewModelUtils.getObjectClassForMetadata(view, form,
445 view.getDefaultBindingObjectPath());
446 } else {
447 dataObjectClass = view.getFormClass();
448 }
449
450 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
451 if (dataObjectClass != null) {
452 String titleAttribute = mds.getTitleAttribute(dataObjectClass);
453 if (StringUtils.isNotBlank(titleAttribute)) {
454 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + titleAttribute;
455 }
456 }
457 }
458
459 Object viewLabelPropertyValue = null;
460 if (StringUtils.isNotBlank(viewLabelPropertyPath) && ObjectPropertyUtils. isReadableProperty(form, viewLabelPropertyPath)) {
461 viewLabelPropertyValue = ObjectPropertyUtils.getPropertyValue(form, viewLabelPropertyPath);
462 }
463
464 String titleAppend = "";
465 if (viewLabelPropertyValue != null) {
466 titleAppend = viewLabelPropertyValue.toString();
467 }
468
469 if (StringUtils.isNotBlank(titleAppend) && view.getBreadcrumbTitleDisplayOption() != null) {
470 if (view.getBreadcrumbTitleDisplayOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.DASH)) {
471 title = title + " - " + titleAppend;
472 } else if (view.getBreadcrumbTitleDisplayOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.PARENTHESIS)) {
473 title = title + "(" + titleAppend + ")";
474 } else if (view.getBreadcrumbTitleDisplayOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.REPLACE)) {
475 title = titleAppend;
476 }
477
478 }
479
480 return title;
481 }
482 }