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.select.OleSelectConstant;
20  import org.kuali.ole.select.businessobject.OleAcquisitionSearchResult;
21  import org.kuali.ole.select.document.service.OleAcquisitionSearchService;
22  import org.kuali.ole.select.lookup.DocData;
23  import org.kuali.ole.select.service.impl.OleDocStoreSearchService;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.core.api.util.RiceKeyConstants;
26  import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
27  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
28  import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
29  import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
30  import org.kuali.rice.kew.exception.WorkflowServiceError;
31  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
32  import org.kuali.rice.kew.service.KEWServiceLocator;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  
35  import java.util.*;
36  
37  
38  public class OleAcquisitionSearchServiceImpl implements OleAcquisitionSearchService {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleAcquisitionSearchServiceImpl.class);
40  
41      /**
42       * This method sets the docData details for searched results
43       * by querying to the docStore
44       *
45       * @param documentSearchResults
46       */
47      protected Collection<DocData> getDocData(List<DocumentSearchResult> documentSearchResults) {
48          List<Object> titleIds = new ArrayList<Object>();
49          for (DocumentSearchResult documentSearchResult : documentSearchResults) {
50              for (DocumentAttribute documentAttribute : documentSearchResult.getDocumentAttributes()) {
51                  if (documentAttribute.getName().equalsIgnoreCase(OleSelectConstant.AcquisitionsSearch.ITEM_TITLE_ID) &&
52                          documentAttribute.getValue() != null &&
53                          StringUtils.isNotBlank(documentAttribute.getValue().toString())) {
54                      titleIds.add(documentAttribute.getValue());
55                  }
56              }
57          }
58          if (!titleIds.isEmpty()) {
59              try {
60                  return SpringContext.getBean(OleDocStoreSearchService.class).getResult(DocData.class, "", titleIds);
61              } catch (Exception ex) {
62                  GlobalVariables.getMessageMap().putError(OleSelectConstant.AcquisitionsSearch.REQUISITIONS, "Error While connecting to DocStore",
63                          new String[]{});
64                  LOG.error("Exception access document store for lookup by titleIds: " + titleIds, ex);
65              }
66          }
67          return Collections.emptyList();
68      }
69  
70      /**
71       * This method populates the criteria for the fields in the map
72       * using searchable attributes
73       *
74       * @param criteria
75       * @param propertyFields
76       * @return criteria
77       */
78      private DocumentSearchCriteria addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder criteria, Map<String, List<String>> propertyFields) {
79          Map<String, List<String>> attributes = new HashMap<String, List<String>>();
80          if (criteria != null) {
81              if (!propertyFields.isEmpty()) {
82                  for (String propertyField : propertyFields.keySet()) {
83                      if (propertyFields.get(propertyField) != null) {
84                          attributes.put(propertyField, propertyFields.get(propertyField));
85                      }
86                  }
87              }
88          }
89          criteria.setDocumentAttributeValues(attributes);
90          return criteria.build();
91      }
92  
93      /**
94       * return a string array of titleIds from a List of docdata class
95       *
96       * @param docDataList
97       * @return titleId
98       */
99      @Override
100     public List<String> getTitleIds(List<DocData> docDataList) {
101         List<String> titleIds = new ArrayList<String>();
102         int i = 0;
103         for (DocData docData : docDataList) {
104             if (StringUtils.isNotEmpty(docData.getUniqueId())) {
105                 titleIds.add(docData.getUniqueId());
106             }
107         }
108         //String[] titleId = titleIds.toArray(new String[titleIds.size()]);
109         return titleIds;
110     }
111 
112     /**
113      * returns a collection of resultRow for the search type document
114      * from the documentsearchresult components
115      *
116      * @param components
117      * @return resultTable
118      */
119     private List<OleAcquisitionSearchResult> getFinalDocumentTypeResult(List<DocumentSearchResult> componentResults) {
120         List<OleAcquisitionSearchResult> docResult = new ArrayList<OleAcquisitionSearchResult>();
121         OleAcquisitionSearchResult acqSearchResult;
122         if (!componentResults.isEmpty()) {
123             for (DocumentSearchResult searchResult : componentResults) {
124                 acqSearchResult = new OleAcquisitionSearchResult();
125                 acqSearchResult.setResultDetails(false, searchResult, new ArrayList());
126                 if (acqSearchResult != null) {
127                     docResult.add(acqSearchResult);
128                 }
129             }
130         }
131         return docResult;
132 
133     }
134 
135     /**
136      * This method filters other search criteria's based on create from/to date.
137      *
138      * @param fixedParameters Map containing created date search criteria.
139      * @return Map containing document numbers of searched Results
140      */
141     @Override
142     public List<OleAcquisitionSearchResult> performDocumentSearch(DocumentSearchCriteria docSearchCriteria, Map<String, List<String>> fixedParameters) {
143         DocumentSearchCriteria docSearchCriteriaDTO = addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder.create(docSearchCriteria), fixedParameters);
144         LOG.debug("Inside filterWorkflowStatusDate of OleAcquisitionsSearchDocument");
145         List result = new ArrayList();
146         List<OleAcquisitionSearchResult> finalResult = new ArrayList<OleAcquisitionSearchResult>();
147         DocumentSearchResults components = null;
148         boolean isDateSpecified = true;
149         try {
150             components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(),
151                     docSearchCriteriaDTO);
152 
153             List<DocumentSearchResult> docSearchResults = components.getSearchResults();
154             if (!fixedParameters.containsKey("displayType")) {
155                 finalResult = getFinalDocumentTypeResult(docSearchResults);
156             } else {
157                 finalResult = getFinalBibTypeResult(docSearchResults);
158             }
159         } catch (WorkflowServiceErrorException wsee) {
160             for (WorkflowServiceError workflowServiceError : (List<WorkflowServiceError>) wsee.getServiceErrors()) {
161                 if (workflowServiceError.getMessageMap() != null && workflowServiceError.getMessageMap().hasErrors()) {
162                     GlobalVariables.getMessageMap().merge(workflowServiceError.getMessageMap());
163                 } else {
164                     GlobalVariables.getMessageMap().putError(workflowServiceError.getMessage(), RiceKeyConstants.ERROR_CUSTOM,
165                             workflowServiceError.getMessage());
166                 }
167             }
168             ;
169         }
170         return finalResult;
171     }
172 
173     /**
174      * returns a collection of resultRow for the search type bib
175      * from the documentsearchresult components
176      *
177      * @param components
178      * @return resultTable
179      */
180     protected List<OleAcquisitionSearchResult> getFinalBibTypeResult(List<DocumentSearchResult> componentResults) {
181         List<OleAcquisitionSearchResult> bibResult = new ArrayList<OleAcquisitionSearchResult>();
182         List<DocData> docDatas = (List<DocData>) getDocData(componentResults);
183         OleAcquisitionSearchResult acqSearchResult;
184         if (!componentResults.isEmpty()) {
185             for (DocumentSearchResult searchResult : componentResults) {
186                 acqSearchResult = new OleAcquisitionSearchResult();
187                 acqSearchResult.setResultDetails(true, searchResult, docDatas);
188                 if (acqSearchResult != null) {
189                     bibResult.add(acqSearchResult);
190                 }
191             }
192         }
193         return bibResult;
194     }
195 
196 }