1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.docsearch.service.impl; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
|
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.rice.kew.docsearch.DocSearchUtils; |
25 | |
import org.kuali.rice.kew.docsearch.SearchableAttribute; |
26 | |
import org.kuali.rice.kew.docsearch.SearchableAttributeValue; |
27 | |
import org.kuali.rice.kew.docsearch.service.SearchableAttributeProcessingService; |
28 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
29 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
30 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent; |
31 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class SearchableAttributeProcessor implements SearchableAttributeProcessingService { |
40 | |
|
41 | 0 | private static Logger LOG = Logger.getLogger(SearchableAttributeProcessor.class); |
42 | |
|
43 | |
public void indexDocument(Long documentId) { |
44 | 0 | indexDocument(documentId, true); |
45 | 0 | } |
46 | |
|
47 | |
public void indexDocument(Long documentId, boolean useMostRecentDocType) { |
48 | 0 | long t1 = System.currentTimeMillis(); |
49 | 0 | LOG.info("Indexing document " + documentId + " for document search..."); |
50 | |
try { |
51 | 0 | DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByDocumentId(documentId); |
52 | 0 | DocumentRouteHeaderValueContent docContent = KEWServiceLocator.getRouteHeaderService().getContent(documentId); |
53 | 0 | List<SearchableAttributeValue> attributes = buildSearchableAttributeValues(documentType, documentId, docContent.getDocumentContent(), useMostRecentDocType); |
54 | 0 | KEWServiceLocator.getRouteHeaderService().updateRouteHeaderSearchValues(documentId, attributes); |
55 | 0 | } catch (Exception e) { |
56 | 0 | String errorMsg = "Encountered an error when attempting to index searchable attributes, requeuing."; |
57 | 0 | LOG.error(errorMsg, e); |
58 | 0 | throw new WorkflowRuntimeException(errorMsg,e); |
59 | 0 | } |
60 | 0 | long t2 = System.currentTimeMillis(); |
61 | 0 | LOG.info("...finished indexing document " + documentId + " for document search, total time = " + (t2-t1) + " ms."); |
62 | 0 | } |
63 | |
|
64 | |
private List<SearchableAttributeValue> buildSearchableAttributeValues(DocumentType docType, Long documentId, String docContent, boolean useMostRecentDocType) { |
65 | 0 | if (useMostRecentDocType) { |
66 | 0 | docType = KEWServiceLocator.getDocumentTypeService().findByName(docType.getName()); |
67 | |
} |
68 | 0 | List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>(); |
69 | |
|
70 | 0 | for (Iterator iterator = docType.getSearchableAttributes().iterator(); iterator.hasNext();) { |
71 | 0 | SearchableAttribute searchableAttribute = (SearchableAttribute) iterator.next(); |
72 | 0 | List searchStorageValues = searchableAttribute.getSearchStorageValues( |
73 | |
DocSearchUtils.getDocumentSearchContext(documentId.toString(), docType.getName(), docContent)); |
74 | 0 | if (searchStorageValues != null) { |
75 | 0 | for (Iterator iterator2 = searchStorageValues.iterator(); iterator2.hasNext();) { |
76 | 0 | SearchableAttributeValue searchableAttributeValue = (SearchableAttributeValue) iterator2.next(); |
77 | 0 | searchableAttributeValue.setRouteHeaderId(documentId); |
78 | 0 | searchableAttributeValues.add(searchableAttributeValue); |
79 | 0 | searchableAttributeValue.setRouteHeader(null); |
80 | 0 | } |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | 0 | return searchableAttributeValues; |
85 | |
} |
86 | |
|
87 | |
} |