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