1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.bo.lookup; |
18 | |
|
19 | |
import org.apache.commons.beanutils.PropertyUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.kuali.rice.core.api.datetime.DateTimeService; |
22 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
23 | |
import org.kuali.rice.core.web.format.BooleanFormatter; |
24 | |
import org.kuali.rice.core.web.format.CollectionFormatter; |
25 | |
import org.kuali.rice.core.web.format.DateFormatter; |
26 | |
import org.kuali.rice.core.web.format.Formatter; |
27 | |
import org.kuali.rice.kew.docsearch.DocSearchCriteriaDTO; |
28 | |
import org.kuali.rice.kew.docsearch.DocumentLookupCriteriaProcessor; |
29 | |
import org.kuali.rice.kew.docsearch.DocumentLookupCriteriaProcessorKEWAdapter; |
30 | |
import org.kuali.rice.kew.docsearch.DocumentSearchResult; |
31 | |
import org.kuali.rice.kew.docsearch.DocumentSearchResultComponents; |
32 | |
import org.kuali.rice.kew.docsearch.StandardDocumentSearchCriteriaProcessor; |
33 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
34 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
36 | |
import org.kuali.rice.kew.util.KEWConstants; |
37 | |
import org.kuali.rice.kew.util.KEWPropertyConstants; |
38 | |
import org.kuali.rice.kew.web.KeyValueSort; |
39 | |
import org.kuali.rice.kim.bo.Person; |
40 | |
import org.kuali.rice.kns.lookup.HtmlData; |
41 | |
import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl; |
42 | |
import org.kuali.rice.kns.web.comparator.CellComparatorHelper; |
43 | |
import org.kuali.rice.kns.web.struts.form.LookupForm; |
44 | |
import org.kuali.rice.kns.web.ui.Column; |
45 | |
import org.kuali.rice.kns.web.ui.Field; |
46 | |
import org.kuali.rice.kns.web.ui.ResultRow; |
47 | |
import org.kuali.rice.kns.web.ui.Row; |
48 | |
import org.kuali.rice.krad.bo.BusinessObject; |
49 | |
import org.kuali.rice.krad.util.GlobalVariables; |
50 | |
import org.kuali.rice.krad.util.KRADConstants; |
51 | |
|
52 | |
import java.lang.reflect.InvocationTargetException; |
53 | |
import java.sql.Date; |
54 | |
import java.util.ArrayList; |
55 | |
import java.util.Collection; |
56 | |
import java.util.HashMap; |
57 | |
import java.util.List; |
58 | |
import java.util.Map; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public class DocumentRouteHeaderValueLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl { |
67 | |
|
68 | |
private static final long serialVersionUID = -5162419674659967408L; |
69 | |
DateTimeService dateTimeService; |
70 | |
DocumentLookupCriteriaProcessor processor; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void setDateTimeService(DateTimeService dateTimeService) { |
79 | 0 | this.dateTimeService = dateTimeService; |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Override |
89 | |
public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, |
90 | |
List pkNames) { |
91 | |
|
92 | 0 | return super.getCustomActionUrls(businessObject, pkNames); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public Collection performLookup(LookupForm lookupForm, |
108 | |
Collection resultTable, boolean bounded) { |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 0 | Map<String,String> fieldsForLookup = lookupForm.getFieldsForLookup(); |
114 | 0 | DocSearchCriteriaDTO criteria = constructCriteria(fieldsForLookup); |
115 | |
|
116 | |
|
117 | 0 | Collection displayList=null; |
118 | 0 | DocumentSearchResultComponents components = KEWServiceLocator.getDocumentSearchService().getList(GlobalVariables.getUserSession().getPrincipalId(), criteria); |
119 | 0 | List<DocumentSearchResult> result = components.getSearchResults(); |
120 | |
|
121 | 0 | displayList = result; |
122 | |
|
123 | |
|
124 | |
|
125 | 0 | setBackLocation((String) lookupForm.getFieldsForLookup().get(KRADConstants.BACK_LOCATION)); |
126 | 0 | setDocFormKey((String) lookupForm.getFieldsForLookup().get(KRADConstants.DOC_FORM_KEY)); |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 0 | HashMap<String,Class> propertyTypes = new HashMap<String, Class>(); |
140 | |
|
141 | 0 | boolean hasReturnableRow = false; |
142 | |
|
143 | 0 | List returnKeys = getReturnKeys(); |
144 | 0 | List pkNames = getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(getBusinessObjectClass()); |
145 | 0 | Person user = GlobalVariables.getUserSession().getPerson(); |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | 0 | for (DocumentSearchResult aResult : result) |
162 | |
{ |
163 | |
|
164 | 0 | HtmlData returnUrl = new HtmlData.AnchorHtmlData(); |
165 | 0 | String actionUrls = ""; |
166 | |
|
167 | |
|
168 | |
|
169 | 0 | List<? extends Column> columns = components.getColumns(); |
170 | 0 | List<KeyValueSort> keyValues = aResult.getResultContainers(); |
171 | 0 | for (int i = 0; i < columns.size(); i++) |
172 | |
{ |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 0 | Column col = (Column) columns.get(i); |
179 | 0 | KeyValueSort keyValue = keyValues.get(i); |
180 | |
|
181 | 0 | col.setPropertyValue(keyValue.getUserDisplayValue()); |
182 | |
|
183 | 0 | Formatter formatter = col.getFormatter(); |
184 | |
|
185 | |
|
186 | 0 | String propValue = KRADConstants.EMPTY_STRING; |
187 | |
|
188 | |
|
189 | 0 | Object prop = col.getPropertyValue(); |
190 | |
|
191 | |
|
192 | 0 | Class propClass = propertyTypes.get(col.getPropertyName()); |
193 | 0 | if (propClass == null) |
194 | |
{ |
195 | |
try |
196 | |
{ |
197 | |
|
198 | |
|
199 | |
} catch (Exception e) |
200 | |
{ |
201 | |
|
202 | 0 | } |
203 | |
} |
204 | |
|
205 | |
|
206 | 0 | if (prop != null) |
207 | |
{ |
208 | |
|
209 | 0 | if (prop instanceof Boolean) |
210 | |
{ |
211 | 0 | formatter = new BooleanFormatter(); |
212 | |
} |
213 | |
|
214 | |
|
215 | 0 | if (prop instanceof Date) |
216 | |
{ |
217 | 0 | formatter = new DateFormatter(); |
218 | |
} |
219 | |
|
220 | |
|
221 | 0 | if (prop instanceof Collection && formatter == null) |
222 | |
{ |
223 | 0 | formatter = new CollectionFormatter(); |
224 | |
} |
225 | |
|
226 | 0 | if (formatter != null) |
227 | |
{ |
228 | 0 | propValue = (String) formatter.format(prop); |
229 | |
} else |
230 | |
{ |
231 | 0 | propValue = prop.toString(); |
232 | |
} |
233 | |
} |
234 | |
|
235 | |
|
236 | 0 | col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass)); |
237 | 0 | col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass)); |
238 | |
|
239 | |
|
240 | |
|
241 | 0 | col.setPropertyValue(propValue); |
242 | |
|
243 | 0 | if (StringUtils.isNotBlank(propValue)) |
244 | |
{ |
245 | |
|
246 | |
|
247 | 0 | HtmlData.AnchorHtmlData anchor = new HtmlData.AnchorHtmlData(KRADConstants.EMPTY_STRING, KRADConstants.EMPTY_STRING); |
248 | |
|
249 | 0 | if (StringUtils.isNotEmpty(keyValue.getValue()) && StringUtils.equals("documentId", keyValue.getKey())) |
250 | |
{ |
251 | 0 | anchor.setHref(StringUtils.substringBetween(keyValue.getValue(), "<a href=\"", "docId=") + "docId=" + keyValue.getUserDisplayValue()); |
252 | 0 | col.setMaxLength(100); |
253 | |
} |
254 | |
|
255 | 0 | col.setColumnAnchor(anchor); |
256 | |
} |
257 | |
} |
258 | |
|
259 | 0 | ResultRow row = new ResultRow((List<Column>) columns, returnUrl.constructCompleteHtmlTag(), actionUrls); |
260 | 0 | row.setRowId(returnUrl.getName()); |
261 | |
|
262 | |
|
263 | 0 | if (getBusinessObjectDictionaryService().isExportable(getBusinessObjectClass())) |
264 | |
{ |
265 | |
|
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | 0 | boolean rowReturnable = true; |
271 | 0 | row.setRowReturnable(rowReturnable); |
272 | 0 | if (rowReturnable) |
273 | |
{ |
274 | 0 | hasReturnableRow = true; |
275 | |
} |
276 | 0 | resultTable.add(row); |
277 | 0 | } |
278 | |
|
279 | 0 | lookupForm.setHasReturnableRow(hasReturnableRow); |
280 | |
|
281 | 0 | return displayList; |
282 | |
|
283 | |
} |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
private DocSearchCriteriaDTO constructCriteria(Map<String,String> fieldsForLookup) { |
293 | |
|
294 | 0 | DocSearchCriteriaDTO criteria = new DocSearchCriteriaDTO(); |
295 | 0 | Map<String,String> fieldsToSet = new HashMap<String,String>(); |
296 | 0 | for (String formKey : fieldsForLookup.keySet()) { |
297 | 0 | if(!(formKey.equalsIgnoreCase(KRADConstants.BACK_LOCATION) || |
298 | |
formKey.equalsIgnoreCase(KRADConstants.DOC_FORM_KEY)) && StringUtils.isNotEmpty(fieldsForLookup.get(formKey))) { |
299 | 0 | fieldsToSet.put(formKey, fieldsForLookup.get(formKey)); |
300 | |
} |
301 | |
} |
302 | |
|
303 | 0 | for (String fieldToSet : fieldsToSet.keySet()) { |
304 | |
|
305 | 0 | String valueToSet = fieldsToSet.get(fieldToSet); |
306 | |
try { |
307 | |
|
308 | 0 | if(fieldToSet.equals("documentType.name")) { |
309 | 0 | fieldToSet = "docTypeFullName"; |
310 | |
} |
311 | 0 | PropertyUtils.setNestedProperty(criteria, fieldToSet, valueToSet); |
312 | 0 | } catch (IllegalAccessException e) { |
313 | 0 | e.printStackTrace(); |
314 | 0 | } catch (InvocationTargetException e) { |
315 | 0 | e.printStackTrace(); |
316 | 0 | } catch (NoSuchMethodException e) { |
317 | 0 | e.printStackTrace(); |
318 | 0 | } |
319 | 0 | } |
320 | 0 | return criteria; |
321 | |
} |
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
@Override |
330 | |
public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) { |
331 | |
|
332 | 0 | if(KEWPropertyConstants.DOC_SEARCH_RESULT_PROPERTY_NAME_DOCUMENT_ID.equals(propertyName)) { |
333 | |
|
334 | 0 | HtmlData.AnchorHtmlData link = new HtmlData.AnchorHtmlData(); |
335 | 0 | DocumentRouteHeaderValue doc = (DocumentRouteHeaderValue)bo; |
336 | |
|
337 | 0 | String documentId = doc.getDocumentId(); |
338 | 0 | link.setDisplayText(documentId+""); |
339 | |
|
340 | 0 | String href = ConfigContext.getCurrentContextConfig().getKRBaseURL()+"/"+ |
341 | |
KEWConstants.DOC_HANDLER_REDIRECT_PAGE + "?" + KEWConstants.COMMAND_PARAMETER + "=" + |
342 | |
KEWConstants.DOCSEARCH_COMMAND + "&" + KEWConstants.DOCUMENT_ID_PARAMETER + "=" + documentId; |
343 | 0 | link.setHref(href); |
344 | |
|
345 | 0 | return link; |
346 | |
} |
347 | |
|
348 | 0 | return super.getInquiryUrl(bo, propertyName); |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
@Override |
357 | |
protected void setRows() { |
358 | 0 | super.setRows(); |
359 | 0 | List<Row> lookupRows = new ArrayList<Row>(super.getRows()); |
360 | |
|
361 | 0 | super.getRows().clear(); |
362 | 0 | processor = new DocumentLookupCriteriaProcessorKEWAdapter(); |
363 | |
|
364 | 0 | List<Row> rows = processor.getRows(null, lookupRows, false, false); |
365 | 0 | super.getRows().addAll(rows); |
366 | 0 | } |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
@Override |
375 | |
public void performClear(LookupForm lookupForm) { |
376 | 0 | Map<String,String> fieldsToClear = new HashMap<String,String>(); |
377 | |
|
378 | 0 | for (Row row : this.getRows()) { |
379 | 0 | for (Field field : row.getFields()) { |
380 | 0 | fieldsToClear.put(field.getPropertyName(), field.getPropertyValue()); |
381 | |
} |
382 | |
} |
383 | |
|
384 | 0 | if(this.processor==null) { |
385 | 0 | super.performClear(lookupForm); |
386 | |
} else { |
387 | 0 | DocSearchCriteriaDTO docCriteria = constructCriteria(fieldsToClear); |
388 | 0 | if(StringUtils.isNotEmpty("documentType.name")) { |
389 | |
|
390 | 0 | getValidDocumentType(fieldsToClear.get("documentType.name")).getDocumentSearchGenerator().clearSearch(docCriteria);; |
391 | |
} |
392 | |
} |
393 | 0 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
private static DocumentType getValidDocumentType(String docTypeName) { |
402 | 0 | DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName); |
403 | 0 | if (documentType == null) { |
404 | 0 | throw new RuntimeException("Document Type invalid : " + docTypeName); |
405 | |
} |
406 | 0 | return documentType; |
407 | |
} |
408 | |
} |