001package org.kuali.ole.ingest.function;
002
003//import org.kuali.incubator.SolrRequestReponseHandler;
004
005import org.apache.log4j.Logger;
006import org.kuali.ole.DataCarrierService;
007import org.kuali.ole.OLEConstants;
008
009import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
010import org.kuali.ole.docstore.common.document.content.enums.DocType;
011import org.kuali.ole.docstore.common.search.SearchParams;
012import org.kuali.ole.docstore.common.search.SearchResponse;
013import org.kuali.ole.docstore.common.search.SearchResult;
014import org.kuali.ole.docstore.common.search.SearchResultField;
015import org.kuali.ole.ingest.ISBNUtil;
016import org.kuali.ole.sys.context.SpringContext;
017import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
018import org.kuali.rice.krms.framework.engine.Function;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023
024/**
025 * ISBNFunction takes the request,normalize tha isbn and sets the bibRecordFromSOLRResponse into dataCarrierService
026 */
027public class ISBNFunction implements Function {
028
029    private DocstoreClientLocator docstoreClientLocator;
030
031    private static final Logger LOG = Logger.getLogger(ISBNFunction.class);
032
033    /**
034     *  This method takes the request and normalize the isbn and  sets the bibRecordFromSOLRResponse into dataCarrierService.
035     * @param arguments
036     * @return  Object
037     */
038    @Override
039    public Object invoke(Object... arguments) {
040        LOG.info(" -----------------> inside isbn function ------------> ");
041        DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
042        String existingDocstoreField = (String)(arguments[0]);
043        String isbn = (String)(arguments[1]);
044        String normalizedISBN = null;
045        try {
046            normalizedISBN = new ISBNUtil().normalizeISBN(isbn);
047        } catch (Exception e) {
048            System.out.println(e.getMessage());
049        }
050        //  List list = getDocstoreClientLocator().getResponseFromSOLR(existingDocstoreField, normalizedISBN);
051        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
052        SearchParams searchParams=new  SearchParams();
053        searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"ISBN",normalizedISBN), "AND"));
054        searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic", "bibIdentifier"));
055        try {
056            SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
057            List<SearchResult>  searchResults=searchResponse.getSearchResults();
058            LOG.info(" ---------------> list.size ------------> " + list.size());
059            if(searchResults.size() >=1){
060                LOG.info(" inside if condition of list --------------------> ");
061                for (SearchResult searchResult:searchResults){
062                    HashMap<String, Object> bibMap=new HashMap<>();
063                    for(SearchResultField searchResultField:searchResult.getSearchResultFields()){
064                        if(searchResultField.getFieldValue()!=null && !searchResultField.getFieldValue().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("bibidentifier") ){
065                        bibMap.put(OLEConstants.BIB_UNIQUE_ID,searchResultField.getFieldValue());
066                        }
067                    }
068                    list.add(bibMap);
069            }
070            dataCarrierService.addData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE, list);
071             return true;
072            }
073            return false;
074
075        }
076        catch(Exception ex){
077        throw new RuntimeException(ex);
078        }
079
080        }
081
082
083    public DocstoreClientLocator getDocstoreClientLocator() {
084        if (null == docstoreClientLocator) {
085        return  SpringContext.getBean(DocstoreClientLocator.class);
086       }
087        return docstoreClientLocator;
088    }
089
090    public void setDocstoreClientLocator(DocstoreClientLocator docstoreClientLocator) {
091                this.docstoreClientLocator = docstoreClientLocator;
092    }
093
094}