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 private void setCurrent(String viewId, String pageId, String title, String url, String formKey) {
113 HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
114 current = entry;
115 }
116
117
118
119
120 public void setCurrent(HistoryEntry current) {
121 this.current = current;
122 }
123
124
125
126
127
128
129
130
131
132 public void buildHistoryFromParameterString(String parameterString) {
133 if (StringUtils.isNotEmpty(parameterString)) {
134 try {
135 parameterString = URLDecoder.decode(parameterString, "UTF-8");
136 } catch (UnsupportedEncodingException e) {
137 LOG.error("Error decoding history param", e);
138 }
139
140 historyEntries = new ArrayList<HistoryEntry>();
141 if (appendPassedHistory) {
142 String[] historyTokens = parameterString.split("\\" + ENTRY_TOKEN);
143 for (String token : historyTokens) {
144 String[] params = token.split(VAR_TOKEN);
145 pushToHistory(params[0], params[1], params[2], params[3], params[4]);
146 }
147 }
148 }
149
150 if (appendHomewardPath) {
151 historyEntries.addAll(homewardPath);
152 }
153 }
154
155
156
157
158
159
160
161
162 public String getHistoryParameterString() {
163 String historyString = "";
164 for (HistoryEntry e : historyEntries) {
165 if (historyEntries.indexOf(e) == 0) {
166 historyString = historyString + e.toParam();
167 } else {
168 historyString = historyString + ENTRY_TOKEN + e.toParam();
169 }
170 }
171
172
173 if (current != null) {
174 if (historyString.equals("")) {
175 historyString = historyString + current.toParam();
176 } else {
177 historyString = historyString + ENTRY_TOKEN + current.toParam();
178 }
179 }
180
181 try {
182 historyString = URLEncoder.encode(historyString, "UTF-8");
183 } catch (Exception e) {
184 LOG.error("Error encoding history param", e);
185 }
186
187 return historyString;
188 }
189
190
191
192
193
194
195
196
197
198
199 public List<HistoryEntry> getGeneratedBreadcrumbs() {
200 List<HistoryEntry> breadcrumbs = new ArrayList<HistoryEntry>();
201 for (int i = 0; i < historyEntries.size(); i++) {
202 if (i == 0) {
203 breadcrumbs.add(copyEntry(historyEntries.get(i)));
204 } else {
205 HistoryEntry breadcrumb = copyEntry(historyEntries.get(i));
206 String historyParam = "";
207 for (int j = 0; j < i; j++) {
208 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
209 }
210 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
211 try {
212 historyParam = URLEncoder.encode(historyParam, "UTF-8");
213 } catch (Exception e) {
214 LOG.error("Error encoding history param", e);
215 }
216
217 String url = "";
218 if (breadcrumb.getUrl().contains("?")) {
219 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
220 } else {
221 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
222 }
223
224 breadcrumb.setUrl(url);
225 breadcrumbs.add(breadcrumb);
226 }
227 }
228
229 return breadcrumbs;
230 }
231
232
233
234
235
236
237 public HistoryEntry getGeneratedCurrentBreadcrumb() {
238 HistoryEntry breadcrumb = copyEntry(current);
239 String historyParam = "";
240 for (int j = 0; j < historyEntries.size(); j++) {
241 historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
242 }
243 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
244
245 try {
246 historyParam = URLEncoder.encode(historyParam, "UTF-8");
247 } catch (Exception e) {
248 LOG.error("Error encoding history param", e);
249 }
250
251 String url = "";
252 if (breadcrumb.getUrl().contains("?")) {
253 url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
254 } else {
255 url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
256 }
257 breadcrumb.setUrl(url);
258
259 return breadcrumb;
260 }
261
262
263
264
265
266
267
268 private HistoryEntry copyEntry(HistoryEntry e) {
269 return new HistoryEntry(e.getViewId(), e.getPageId(), e.getTitle(), e.getUrl(), e.getFormKey());
270 }
271
272
273
274
275
276
277
278
279
280
281
282 public void pushToHistory(String viewId, String pageId, String title, String url, String formKey) {
283 HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
284 historyEntries.add(entry);
285 }
286
287
288
289
290
291
292
293
294 public void setAppendHomewardPath(boolean appendHomewardPath) {
295 this.appendHomewardPath = appendHomewardPath;
296 }
297
298
299
300
301 public boolean isAppendHomewardPath() {
302 return appendHomewardPath;
303 }
304
305
306
307
308
309
310
311
312 public void setAppendPassedHistory(boolean appendPassedHistory) {
313 this.appendPassedHistory = appendPassedHistory;
314 }
315
316
317
318
319 public boolean isAppendPassedHistory() {
320 return appendPassedHistory;
321 }
322
323
324
325
326
327
328
329
330
331 @SuppressWarnings("unchecked")
332 public void setCurrent(UifFormBase form, HttpServletRequest request) {
333 if (!request.getMethod().equals("POST")) {
334 boolean showHomeValue = false;
335 boolean pageIdValue = false;
336 boolean formKeyValue = false;
337
338 String queryString = "";
339 String url = request.getRequestURL().toString();
340
341
342 Enumeration<String> params = request.getParameterNames();
343 while (params.hasMoreElements()) {
344 String key = params.nextElement();
345 if (!key.equals(UifConstants.UrlParams.HISTORY)) {
346 for (String value : request.getParameterValues(key)) {
347 queryString = queryString + "&" + key + "=" + value;
348 }
349 } else if (key.equals(UifConstants.UrlParams.PAGE_ID)) {
350 pageIdValue = true;
351 } else if (key.equals(UifConstants.UrlParams.SHOW_HOME)) {
352 showHomeValue = true;
353 } else if (key.equals(UifConstants.UrlParams.FORM_KEY)) {
354 formKeyValue = true;
355 }
356 }
357
358
359 if (StringUtils.isNotBlank(form.getFormKey()) && !formKeyValue) {
360 queryString = queryString + "&" + UifConstants.UrlParams.FORM_KEY + "=" + form.getFormKey();
361 }
362 if (StringUtils.isNotBlank(form.getPageId()) && !pageIdValue) {
363 queryString = queryString + "&" + UifConstants.UrlParams.PAGE_ID + "=" + form.getPageId();
364 }
365 if (!showHomeValue) {
366 queryString = queryString + "&" + UifConstants.UrlParams.SHOW_HOME + "=false";
367 }
368
369 queryString = queryString.replaceFirst("&", "");
370
371 if (StringUtils.isNotEmpty(queryString)) {
372 url = url + "?" + queryString;
373 }
374
375 this.setCurrent(form.getViewId(), form.getPageId(), buildViewTitle(form), url, form.getFormKey());
376 }
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 protected String buildViewTitle(UifFormBase form) {
394 View view = form.getView();
395 String title = view.getTitle();
396
397
398
399 String viewLabelPropertyName = view.getViewLabelFieldPropertyName();
400
401
402 if (StringUtils.isBlank(viewLabelPropertyName)) {
403 Class<?> dataObjectClass;
404 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
405 dataObjectClass = ObjectPropertyUtils.getPropertyType(form, view.getDefaultBindingObjectPath());
406 } else {
407 dataObjectClass = view.getFormClass();
408 }
409
410 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
411 if (dataObjectClass != null) {
412 viewLabelPropertyName = mds.getTitleAttribute(dataObjectClass);
413 }
414 }
415
416 String viewLabelPropertyPath = "";
417 if (StringUtils.isNotBlank(viewLabelPropertyName)) {
418
419 if (!viewLabelPropertyName.startsWith(UifConstants.NO_BIND_ADJUST_PREFIX)) {
420 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
421 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + viewLabelPropertyName;
422 }
423 } else {
424 viewLabelPropertyPath = StringUtils.removeStart(viewLabelPropertyName,
425 UifConstants.NO_BIND_ADJUST_PREFIX);
426 }
427 }
428 else {
429
430 Class<?> dataObjectClass;
431 if (StringUtils.isNotBlank(view.getDefaultBindingObjectPath())) {
432 dataObjectClass = ViewModelUtils.getObjectClassForMetadata(view, form,
433 view.getDefaultBindingObjectPath());
434 } else {
435 dataObjectClass = view.getFormClass();
436 }
437
438 DataObjectMetaDataService mds = KRADServiceLocatorWeb.getDataObjectMetaDataService();
439 if (dataObjectClass != null) {
440 String titleAttribute = mds.getTitleAttribute(dataObjectClass);
441 if (StringUtils.isNotBlank(titleAttribute)) {
442 viewLabelPropertyPath = view.getDefaultBindingObjectPath() + "." + titleAttribute;
443 }
444 }
445 }
446
447 Object viewLabelPropertyValue = null;
448 if (StringUtils.isNotBlank(viewLabelPropertyPath)) {
449 viewLabelPropertyValue = ObjectPropertyUtils.getPropertyValue(form, viewLabelPropertyPath);
450 }
451
452 String titleAppend = "";
453 if (viewLabelPropertyValue != null) {
454 titleAppend = viewLabelPropertyValue.toString();
455 }
456
457 if (StringUtils.isNotBlank(titleAppend) && view.getAppendOption() != null) {
458 if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.DASH)) {
459 title = title + " - " + titleAppend;
460 } else if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.PARENTHESIS)) {
461 title = title + "(" + titleAppend + ")";
462 } else if (view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.REPLACE)) {
463 title = titleAppend;
464 }
465
466 }
467
468 return title;
469 }
470 }