View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * This method sets the docData details for searched results
48       * by querying to the docStore
49       *
50       * @param documentSearchResults
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     /* protected Collection<DocData> getDocData(List<DocumentSearchResult> documentSearchResults) {
61          List<Object> titleIds = new ArrayList<Object>();
62          for (DocumentSearchResult documentSearchResult : documentSearchResults) {
63              for (DocumentAttribute documentAttribute : documentSearchResult.getDocumentAttributes()) {
64                  if (documentAttribute.getName().equalsIgnoreCase(OleSelectConstant.AcquisitionsSearch.ITEM_TITLE_ID) &&
65                          documentAttribute.getValue() != null &&
66                          StringUtils.isNotBlank(documentAttribute.getValue().toString())) {
67                      titleIds.add(documentAttribute.getValue());
68                  }
69              }
70          }
71          if (!titleIds.isEmpty()) {
72              try {
73                  return SpringContext.getBean(OleDocStoreSearchService.class).getResult(DocData.class, "", titleIds);
74              } catch (Exception ex) {
75                  GlobalVariables.getMessageMap().putError(OleSelectConstant.AcquisitionsSearch.REQUISITIONS, "Error While connecting to DocStore",
76                          new String[]{});
77                  LOG.error("Exception access document store for lookup by titleIds: " + titleIds, ex);
78              }
79          }
80          return Collections.emptyList();
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                  //return SpringContext.getBean(OleDocStoreSearchService.class).getResult(DocData.class, "", titleIds);
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      * This method populates the criteria for the fields in the map
110      * using searchable attributes
111      *
112      * @param criteria
113      * @param propertyFields
114      * @return criteria
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      * return a string array of titleIds from a List of docdata class
133      *
134      * @param docDataList
135      * @return titleId
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         //String[] titleId = titleIds.toArray(new String[titleIds.size()]);
149         return titleIds;
150     }
151 
152     /**
153      * returns a collection of resultRow for the search type document
154      * from the documentsearchresult components
155      *
156      * @param components
157      * @return resultTable
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      * This method filters other search criteria's based on create from/to date.
177      *
178      * @param fixedParameters Map containing created date search criteria.
179      * @return Map containing document numbers of searched Results
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      * returns a collection of resultRow for the search type bib
215      * from the documentsearchresult components
216      *
217      * @param components
218      * @return resultTable
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 }