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 public List<HistoryEntry> getGeneratedBreadcrumbs() {
205 List<HistoryEntry> breadcrumbs = new ArrayList<HistoryEntry>();
206 for (int i = 0; i < historyEntries.size(); i++) {
207 if (i == 0) {
208 breadcrumbs.add(copyEntry(historyEntries.get(i)));
209 } else {
210 HistoryEntry breadcrumb = copyEntry(historyEntries.get(i));
211 String historyParam = "";
212 for (int j = 0; j < i; j++) {
213 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
214 }
215 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
216 try {
217 historyParam = URLEncoder.encode(historyParam, "UTF-8");
218 } catch (Exception e) {
219 LOG.error("Error encoding history param", e);
220 }
221
222 String url = "";
223 if (breadcrumb.getUrl().contains("?")) {
224 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
225 } else {
226 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
227 }
228
229 breadcrumb.setUrl(url);
230 breadcrumbs.add(breadcrumb);
231 }
232 }
233
234 return breadcrumbs;
235 }
236
237
238
239
240
241
242 public HistoryEntry getGeneratedCurrentBreadcrumb() {
243 if (current == null){
244 return new HistoryEntry();
245 }
246
247 HistoryEntry breadcrumb = copyEntry(current);
248 String historyParam = "";
249 for (int j = 0; j < historyEntries.size(); j++) {
250 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
251 }
252 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
253
254 try {
255 historyParam = URLEncoder.encode(historyParam, "UTF-8");
256 } catch (Exception e) {
257 LOG.error("Error encoding history param", e);
258 }
259
260 String url = "";
261 if (breadcrumb.getUrl().contains("?")) {
262 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
263 } else {
264 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
265 }
266 breadcrumb.setUrl(url);
267
268 return breadcrumb;
269 }
270
271
272
273
274
275
276
277 private HistoryEntry copyEntry(HistoryEntry e) {
278 return new HistoryEntry(e.getViewId(), e.getPageId(), e.getTitle(), e.getUrl(), e.getFormKey());
279 }
280
281
282
283
284
285
286
287
288
289
290
291 public void pushToHistory(String viewId, String pageId, String title, String url, String formKey) {
292 HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
293 historyEntries.add(entry);
294 }
295
296
297
298
299
300
301
302
303 public void setAppendHomewardPath(boolean appendHomewardPath) {
304 this.appendHomewardPath = appendHomewardPath;
305 }
306
307
308
309
310 public boolean isAppendHomewardPath() {
311 return appendHomewardPath;
312 }
313
314
315
316
317
318
319
320
321 public void setAppendPassedHistory(boolean appendPassedHistory) {
322 this.appendPassedHistory = appendPassedHistory;
323 }
324
325
326
327
328 public boolean isAppendPassedHistory() {
329 return appendPassedHistory;
330 }
331
332
333
334
335
336
337
338
339
340 @SuppressWarnings("unchecked")
341 public void setCurrent(UifFormBase form, HttpServletRequest request) {
342 if (!request.getMethod().equals("POST")) {
343 boolean showHomeValue = false;
344 boolean pageIdValue = false;
345 boolean formKeyValue = false;
346
347 String queryString = "";
348 String url = request.getRequestURL().toString();
349
350
351 Enumeration<String> params = request.getParameterNames();
352 while (params.hasMoreElements()) {
353 String key = params.nextElement();
354 if (!key.equals(UifConstants.UrlParams.HISTORY)) {
355 for (String value : request.getParameterValues(key)) {
356 queryString = queryString + "&" + key + "=" + value;
357 }
358 } else if (key.equals(UifConstants.UrlParams.PAGE_ID)) {
359 pageIdValue = true;
360 } else if (key.equals(UifConstants.UrlParams.SHOW_HOME)) {
361 showHomeValue = true;
362 } else if (key.equals(UifConstants.UrlParams.FORM_KEY)) {
363 formKeyValue = true;
364 }
365 }
366
367
368 if (StringUtils.isNotBlank(form.getFormKey()) && !formKeyValue) {
369 queryString = queryString + "&" + UifConstants.UrlParams.FORM_KEY + "=" + form.getFormKey();
370 }
371 if (StringUtils.isNotBlank(form.getPageId()) && !pageIdValue) {
372 queryString = queryString + "&" + UifConstants.UrlParams.PAGE_ID + "=" + form.getPageId();
373 }
374 if (!showHomeValue) {
375 queryString = queryString + "&" + UifConstants.UrlParams.SHOW_HOME + "=false";
376 }
377
378 queryString = queryString.replaceFirst("&", "");
379
380 if (StringUtils.isNotEmpty(queryString)) {
381 url = url + "?" + queryString;
382 }
383
384 this.setCurrent(form.getViewId(), form.getPageId(), buildViewTitle(form), url, form.getFormKey());
385 }
386 }
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 protected String buildViewTitle(UifFormBase form) {
403 View view = form.getView();
404 String title = view.getTitle();
405
406
407
408 String viewLabelPropertyName = view.getViewLabelFieldPropertyName();
409
410
411 if (StringUtils.isBlank(viewLabelPropertyName)) {
412 Class<?> dataObjectClass;
413 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
414 dataObjectClass = ObjectPropertyUtils.getPropertyType(form, view.getDefaultBindingObjectPath());
415 } else {
416 dataObjectClass = view.getFormClass();
417 }
418
419 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
420 if (dataObjectClass != null) {
421 viewLabelPropertyName = mds.getTitleAttribute(dataObjectClass);
422 }
423 }
424
425 String viewLabelPropertyPath = "";
426 if (StringUtils.isNotBlank(viewLabelPropertyName)) {
427
428 if (!viewLabelPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
429 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
430 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + viewLabelPropertyName;
431 }
432 } else {
433 viewLabelPropertyPath = StringUtils.removeStart(viewLabelPropertyName,
434 UifConstants.NO_BIND_ADJUST_PREFIX);
435 }
436 }
437 else {
438
439 Class<?> dataObjectClass;
440 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
441 dataObjectClass = ViewModelUtils.getObjectClassForMetadata(view, form,
442 view.getDefaultBindingObjectPath());
443 } else {
444 dataObjectClass = view.getFormClass();
445 }
446
447 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
448 if (dataObjectClass != null) {
449 String titleAttribute = mds.getTitleAttribute(dataObjectClass);
450 if (StringUtils.isNotBlank(titleAttribute)) {
451 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + titleAttribute;
452 }
453 }
454 }
455
456 Object viewLabelPropertyValue = null;
457 if (StringUtils.isNotBlank(viewLabelPropertyPath) && ObjectPropertyUtils. isReadableProperty(form, viewLabelPropertyPath)) {
458 viewLabelPropertyValue = ObjectPropertyUtils.getPropertyValue(form, viewLabelPropertyPath);
459 }
460
461 String titleAppend = "";
462 if (viewLabelPropertyValue != null) {
463 titleAppend = viewLabelPropertyValue.toString();
464 }
465
466 if (StringUtils.isNotBlank(titleAppend) && view.getAppendOption() != null) {
467 if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.DASH)) {
468 title = title + " - " + titleAppend;
469 } else if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.PARENTHESIS)) {
470 title = title + "(" + titleAppend + ")";
471 } else if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.REPLACE)) {
472 title = titleAppend;
473 }
474
475 }
476
477 return title;
478 }
479 }