1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.DocumentUniqueIDPrefix;
20 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
21 import org.kuali.ole.docstore.common.document.Bib;
22 import org.kuali.ole.docstore.common.search.SearchResult;
23 import org.kuali.ole.docstore.common.search.SearchResultField;
24 import org.kuali.ole.select.OleSelectConstant;
25 import org.kuali.ole.select.businessobject.OleAcquisitionSearchResult;
26 import org.kuali.ole.select.document.service.OleAcquisitionSearchService;
27 import org.kuali.ole.select.lookup.DocData;
28 import org.kuali.ole.select.service.impl.OleDocStoreSearchService;
29 import org.kuali.ole.sys.context.SpringContext;
30 import org.kuali.rice.core.api.util.RiceKeyConstants;
31 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
32 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
33 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
34 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
35 import org.kuali.rice.kew.exception.WorkflowServiceError;
36 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
37 import org.kuali.rice.kew.service.KEWServiceLocator;
38 import org.kuali.rice.krad.util.GlobalVariables;
39
40 import java.util.*;
41
42
43 public class OleAcquisitionSearchServiceImpl implements OleAcquisitionSearchService {
44 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleAcquisitionSearchServiceImpl.class);
45
46
47
48
49
50
51
52 private DocstoreClientLocator docstoreClientLocator;
53
54 public DocstoreClientLocator getDocstoreClientLocator() {
55 if (docstoreClientLocator == null) {
56 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
57 }
58 return docstoreClientLocator;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 protected Collection<Bib> getBib(List<DocumentSearchResult> documentSearchResults) {
83 List<String> titleIds = new ArrayList<>();
84 for (DocumentSearchResult documentSearchResult : documentSearchResults) {
85 for (DocumentAttribute documentAttribute : documentSearchResult.getDocumentAttributes()) {
86 if (documentAttribute.getName().equalsIgnoreCase(OleSelectConstant.AcquisitionsSearch.ITEM_TITLE_ID) &&
87 documentAttribute.getValue() != null &&
88 StringUtils.isNotBlank(documentAttribute.getValue().toString())) {
89 titleIds.add((String)documentAttribute.getValue());
90 }
91 }
92 }
93 if (!titleIds.isEmpty()) {
94 try {
95 List<Bib> bibs=new ArrayList<>();
96 bibs.addAll(getDocstoreClientLocator().getDocstoreClient().acquisitionSearchRetrieveBibs(titleIds));
97 return bibs;
98
99 } catch (Exception ex) {
100 GlobalVariables.getMessageMap().putError(OleSelectConstant.AcquisitionsSearch.REQUISITIONS, "Error While connecting to DocStore",
101 new String[]{});
102 LOG.error("Exception access document store for lookup by titleIds: " + titleIds, ex);
103 }
104 }
105 return Collections.emptyList();
106 }
107
108
109
110
111
112
113
114
115
116 private DocumentSearchCriteria addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder criteria, Map<String, List<String>> propertyFields) {
117 Map<String, List<String>> attributes = new HashMap<String, List<String>>();
118 if (criteria != null) {
119 if (!propertyFields.isEmpty()) {
120 for (String propertyField : propertyFields.keySet()) {
121 if (propertyFields.get(propertyField) != null) {
122 attributes.put(propertyField, propertyFields.get(propertyField));
123 }
124 }
125 }
126 }
127 criteria.setDocumentAttributeValues(attributes);
128 return criteria.build();
129 }
130
131
132
133
134
135
136
137 @Override
138 public List<String> getTitleIds(List<SearchResult> searchResults) {
139 List<String> titleIds = new ArrayList<String>();
140 int i = 0;
141 for (SearchResult searchResult : searchResults) {
142 for(SearchResultField searchResultField:searchResult.getSearchResultFields()){
143 if(searchResultField.getFieldName().equalsIgnoreCase("id")){
144 titleIds.add(searchResultField.getFieldValue());
145 }
146 }
147 }
148
149 return titleIds;
150 }
151
152
153
154
155
156
157
158
159 private List<OleAcquisitionSearchResult> getFinalDocumentTypeResult(List<DocumentSearchResult> componentResults) {
160 List<OleAcquisitionSearchResult> docResult = new ArrayList<OleAcquisitionSearchResult>();
161 OleAcquisitionSearchResult acqSearchResult;
162 if (!componentResults.isEmpty()) {
163 for (DocumentSearchResult searchResult : componentResults) {
164 acqSearchResult = new OleAcquisitionSearchResult();
165 acqSearchResult.setResultDetails(false, searchResult, new ArrayList());
166 if (acqSearchResult != null) {
167 docResult.add(acqSearchResult);
168 }
169 }
170 }
171 return docResult;
172
173 }
174
175
176
177
178
179
180
181 @Override
182 public List<OleAcquisitionSearchResult> performDocumentSearch(DocumentSearchCriteria docSearchCriteria, Map<String, List<String>> fixedParameters) {
183 DocumentSearchCriteria docSearchCriteriaDTO = addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder.create(docSearchCriteria), fixedParameters);
184 LOG.debug("Inside filterWorkflowStatusDate of OleAcquisitionsSearchDocument");
185 List result = new ArrayList();
186 List<OleAcquisitionSearchResult> finalResult = new ArrayList<OleAcquisitionSearchResult>();
187 DocumentSearchResults components = null;
188 boolean isDateSpecified = true;
189 try {
190 components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(),
191 docSearchCriteriaDTO);
192
193 List<DocumentSearchResult> docSearchResults = components.getSearchResults();
194 if (!fixedParameters.containsKey("displayType")) {
195 finalResult = getFinalDocumentTypeResult(docSearchResults);
196 } else {
197 finalResult = getFinalBibTypeResult(docSearchResults);
198 }
199 } catch (WorkflowServiceErrorException wsee) {
200 for (WorkflowServiceError workflowServiceError : (List<WorkflowServiceError>) wsee.getServiceErrors()) {
201 if (workflowServiceError.getMessageMap() != null && workflowServiceError.getMessageMap().hasErrors()) {
202 GlobalVariables.getMessageMap().merge(workflowServiceError.getMessageMap());
203 } else {
204 GlobalVariables.getMessageMap().putError(workflowServiceError.getMessage(), RiceKeyConstants.ERROR_CUSTOM,
205 workflowServiceError.getMessage());
206 }
207 }
208 ;
209 }
210 return finalResult;
211 }
212
213
214
215
216
217
218
219
220 protected List<OleAcquisitionSearchResult> getFinalBibTypeResult(List<DocumentSearchResult> componentResults) {
221 List<OleAcquisitionSearchResult> bibResult = new ArrayList<OleAcquisitionSearchResult>();
222 List<Bib> bibs= (List<Bib>) getBib(componentResults);
223 OleAcquisitionSearchResult acqSearchResult;
224 if (!componentResults.isEmpty()) {
225 for (DocumentSearchResult searchResult : componentResults) {
226 acqSearchResult = new OleAcquisitionSearchResult();
227 acqSearchResult.setResultDetails(true, searchResult, bibs);
228 if (acqSearchResult != null & StringUtils.isNotBlank(acqSearchResult.getLocalIdentifier())) {
229 bibResult.add(acqSearchResult);
230 }
231 }
232 }
233 return bibResult;
234 }
235
236 }