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