View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
6   import org.kuali.ole.docstore.common.search.*;
7   import org.kuali.ole.select.bo.OleAgreementSearch;
8   import org.kuali.ole.sys.context.SpringContext;
9   
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   *OleAgreementSearchService performs search operation and return list of agreement related information.
17   */
18  public class OleAgreementSearchService {
19      private final String queryString = "(DocType:license AND DocFormat:onixpl)";
20  
21      private DocstoreClientLocator docstoreClientLocator;
22  
23      public DocstoreClientLocator getDocstoreClientLocator() {
24          if (null == docstoreClientLocator) {
25              return SpringContext.getBean(DocstoreClientLocator.class);
26          }
27          return docstoreClientLocator;
28      }
29  
30  
31  //    /**
32  //     *  This method returns the responses from Solr.
33  //     * @param queryField
34  //     * @param value
35  //     * @return  List
36  //     */
37  //    public List getResponseFromSOLR(String queryField, String value) {
38  //        String queryString = queryField + ":" + value;
39  //        return QueryServiceImpl.getInstance().retriveResults(queryString);
40  //    }
41  
42      /**
43       *  This method returns list of agreement information based on search criteria.
44       * @param searchCriteria
45       * @return   List<OleAgreementSearch>
46       */
47      public List<OleAgreementSearch> getAgreementInformation(Map searchCriteria) {
48          List<OleAgreementSearch> agreementSearchResults = new ArrayList<OleAgreementSearch>();
49          OleAgreementSearch agreemetSearch;
50          SearchResponse solrResponse = getSolrResponse(searchCriteria);
51  
52          for(SearchResult searchResult : solrResponse.getSearchResults()) {
53              agreemetSearch = new OleAgreementSearch();
54              for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
55                  if (searchResultField.getFieldName().equalsIgnoreCase("Title_search")) {
56                      agreemetSearch.setAgreementTitle(searchResultField.getFieldValue());
57                  }
58                  else if (searchResultField.getFieldName().equalsIgnoreCase("ContractNumber_search")) {
59                      agreemetSearch.setContractNumber(searchResultField.getFieldValue());
60                  }
61                  else if (searchResultField.getFieldName().equalsIgnoreCase("Method_search")) {
62                      agreemetSearch.setMethodName(searchResultField.getFieldValue());
63                  }
64                  else if (searchResultField.getFieldName().equalsIgnoreCase("Type_search")) {
65                      agreemetSearch.setType(searchResultField.getFieldValue());
66                  }
67                  else if (searchResultField.getFieldName().equalsIgnoreCase("Status_search")) {
68                      agreemetSearch.setStatus(searchResultField.getFieldValue());
69                  }
70                  else if (searchResultField.getFieldName().equalsIgnoreCase("id")) {
71                      agreemetSearch.setUniqueId(searchResultField.getFieldValue());
72                  }
73              }
74  
75              agreementSearchResults.add(agreemetSearch);
76  
77          }
78          return agreementSearchResults;
79      }
80  
81      /**
82       *  This method returns the SolrResponse as List based on searchCriteria.
83       * @param searchCriteria
84       * @return  List
85       */
86      private SearchResponse getSolrResponse(Map searchCriteria) {
87          String query = queryString;
88          String key = null;
89          String value = null;
90          SearchParams searchParams = new SearchParams();
91          if(!searchCriteria.isEmpty()) {
92              int count = 0;
93              if ((searchCriteria.containsKey("agreementTitle")) &&
94                      searchCriteria.get("agreementTitle") != null & !searchCriteria.get("agreementTitle").equals("")) {
95                  SearchField searchField = searchParams.buildSearchField("license", "Title_search", (String) searchCriteria.get("agreementTitle"));
96                  searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR", searchField, "AND"));
97  //                query = query + " AND Title_search:" +searchCriteria.get("agreementTitle");
98  
99              }
100             else {
101                 count ++;
102             }
103             if ((searchCriteria.containsKey("contractNumber")) &&
104                         searchCriteria.get("contractNumber") != null & !searchCriteria.get("contractNumber").equals("")) {
105                 SearchField searchField = searchParams.buildSearchField("license", "ContractNumber_search", (String) searchCriteria.get("contractNumber"));
106                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR", searchField, "AND"));
107 //                query = query + " AND ContractNumber_search:" +searchCriteria.get("contractNumber");
108             }
109             else {
110                 count ++;
111             }
112 
113             /*if ((searchCriteria.containsKey("licensee")) &&
114                     searchCriteria.get("licensee") != null & !searchCriteria.get("licensee").equals("")){
115                 query = query + " AND Licensee_search:" +searchCriteria.get("licensee");
116             }
117             if ((searchCriteria.containsKey("licensor")) &&
118                     searchCriteria.get("licensor") != null & !searchCriteria.get("licensor").equals("")) {
119                 query = query + " AND Licensor_search:" +searchCriteria.get("licensor");
120             }*/
121             //query = query+"&fl=id,Title_search,ContractNumber_search,Licensee_search,Licensor_search";
122             if(searchCriteria.containsKey("uuid")) {
123                 SearchField searchField = searchParams.buildSearchField("license", "id", (String) searchCriteria.get("uuid"));
124                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR", searchField, "AND"));
125 
126 //                query = query + "id:"+ searchCriteria.get("uuid");
127             }
128             else {
129                 count ++;
130             }
131             if( count > 0) {
132                 SearchField searchField = searchParams.buildSearchField("license", "DocFormat", "onixpl");
133                 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR", searchField , "AND"));
134             }
135         }
136 
137         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "Title_search"));
138         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "ContractNumber_search"));
139         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "Method_search"));
140         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "Type_search"));
141         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "Status_search"));
142         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("license", "id"));
143         SearchResponse searchResponse = null;
144 
145         try {
146             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
147         } catch (Exception e) {
148             e.printStackTrace();
149         }
150 
151         return searchResponse;
152     }
153 
154 
155 }