001package org.kuali.ole.service;
002
003import org.apache.commons.lang.StringUtils;
004
005import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
006import org.kuali.ole.docstore.common.search.*;
007import org.kuali.ole.select.bo.OleAgreementSearch;
008import org.kuali.ole.sys.context.SpringContext;
009
010import java.util.ArrayList;
011import java.util.Iterator;
012import java.util.List;
013import java.util.Map;
014
015/**
016 *OleAgreementSearchService performs search operation and return list of agreement related information.
017 */
018public class OleAgreementSearchService {
019    private final String queryString = "(DocType:license AND DocFormat:onixpl)";
020
021    private DocstoreClientLocator docstoreClientLocator;
022
023    public DocstoreClientLocator getDocstoreClientLocator() {
024        if (null == docstoreClientLocator) {
025            return SpringContext.getBean(DocstoreClientLocator.class);
026        }
027        return docstoreClientLocator;
028    }
029
030
031//    /**
032//     *  This method returns the responses from Solr.
033//     * @param queryField
034//     * @param value
035//     * @return  List
036//     */
037//    public List getResponseFromSOLR(String queryField, String value) {
038//        String queryString = queryField + ":" + value;
039//        return QueryServiceImpl.getInstance().retriveResults(queryString);
040//    }
041
042    /**
043     *  This method returns list of agreement information based on search criteria.
044     * @param searchCriteria
045     * @return   List<OleAgreementSearch>
046     */
047    public List<OleAgreementSearch> getAgreementInformation(Map searchCriteria) {
048        List<OleAgreementSearch> agreementSearchResults = new ArrayList<OleAgreementSearch>();
049        OleAgreementSearch agreemetSearch;
050        SearchResponse solrResponse = getSolrResponse(searchCriteria);
051
052        for(SearchResult searchResult : solrResponse.getSearchResults()) {
053            agreemetSearch = new OleAgreementSearch();
054            for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
055                if (searchResultField.getFieldName().equalsIgnoreCase("Title_search")) {
056                    agreemetSearch.setAgreementTitle(searchResultField.getFieldValue());
057                }
058                else if (searchResultField.getFieldName().equalsIgnoreCase("ContractNumber_search")) {
059                    agreemetSearch.setContractNumber(searchResultField.getFieldValue());
060                }
061                else if (searchResultField.getFieldName().equalsIgnoreCase("Method_search")) {
062                    agreemetSearch.setMethodName(searchResultField.getFieldValue());
063                }
064                else if (searchResultField.getFieldName().equalsIgnoreCase("Type_search")) {
065                    agreemetSearch.setType(searchResultField.getFieldValue());
066                }
067                else if (searchResultField.getFieldName().equalsIgnoreCase("Status_search")) {
068                    agreemetSearch.setStatus(searchResultField.getFieldValue());
069                }
070                else if (searchResultField.getFieldName().equalsIgnoreCase("id")) {
071                    agreemetSearch.setUniqueId(searchResultField.getFieldValue());
072                }
073            }
074
075            agreementSearchResults.add(agreemetSearch);
076
077        }
078        return agreementSearchResults;
079    }
080
081    /**
082     *  This method returns the SolrResponse as List based on searchCriteria.
083     * @param searchCriteria
084     * @return  List
085     */
086    private SearchResponse getSolrResponse(Map searchCriteria) {
087        String query = queryString;
088        String key = null;
089        String value = null;
090        SearchParams searchParams = new SearchParams();
091        if(!searchCriteria.isEmpty()) {
092            int count = 0;
093            if ((searchCriteria.containsKey("agreementTitle")) &&
094                    searchCriteria.get("agreementTitle") != null & !searchCriteria.get("agreementTitle").equals("")) {
095                SearchField searchField = searchParams.buildSearchField("license", "Title_search", (String) searchCriteria.get("agreementTitle"));
096                searchParams.getSearchConditions().add(searchParams.buildSearchCondition("OR", searchField, "AND"));
097//                query = query + " AND Title_search:" +searchCriteria.get("agreementTitle");
098
099            }
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}