001package org.kuali.ole.ingest.function;
002
003import org.kuali.ole.DataCarrierService;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
006import org.kuali.ole.docstore.common.document.content.enums.DocType;
007import org.kuali.ole.docstore.common.search.SearchParams;
008import org.kuali.ole.docstore.common.search.SearchResponse;
009import org.kuali.ole.docstore.common.search.SearchResult;
010import org.kuali.ole.docstore.common.search.SearchResultField;
011import org.kuali.ole.ingest.util.OCLCUtil;
012import org.kuali.ole.sys.context.SpringContext;
013import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
014import org.kuali.rice.krms.framework.engine.Function;
015
016import java.util.ArrayList;
017import java.util.HashMap;
018import java.util.List;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: ?
023 * Date: 12/10/12
024 * Time: 1:59 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class OCLCFunction implements Function {
028
029    private DocstoreClientLocator docstoreClientLocator;
030
031    @Override
032    public Object invoke(Object... arguments) {
033        DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
034        String existingDocstoreField = (String)(arguments[0]);
035        String oclc = (String)(arguments[1]);
036        if(oclc != null){
037            String normalizedOclc = null;
038            try{
039                normalizedOclc = new OCLCUtil().normalizedOclc(oclc);
040            }
041            catch (Exception e) {
042                e.printStackTrace();
043            }
044          //  List list = getDiscoveryHelperService().getResponseFromSOLR(existingDocstoreField, normalizedOclc);
045
046            List list=new ArrayList<>();
047            SearchParams searchParams=new  SearchParams();
048            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"035a",normalizedOclc), "AND"));
049            searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic", "bibIdentifier"));
050            try {
051                SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
052                List<SearchResult>  searchResults=searchResponse.getSearchResults();
053                if(searchResults.size() >=1){
054                    for (SearchResult searchResult:searchResults){
055                        HashMap<String, Object> bibMap=new HashMap<>();
056                        for(SearchResultField searchResultField:searchResult.getSearchResultFields()){
057                            if(searchResultField.getFieldValue()!=null && !searchResultField.getFieldValue().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("bibIdentifier") ){
058                                bibMap.put(OLEConstants.BIB_UNIQUE_ID,searchResultField.getFieldValue());
059                            }
060                        }
061                        list.add(bibMap);
062                    }
063                }
064            }catch(Exception ex){
065                throw new RuntimeException(ex);
066
067            }
068            if(list.size() >= 1){
069                dataCarrierService.addData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE, list);
070                return true;
071            }
072        }
073        return false;
074    }
075
076
077    public DocstoreClientLocator getDocstoreClientLocator() {
078        if (null == docstoreClientLocator) {
079            return  SpringContext.getBean(DocstoreClientLocator.class);
080        }
081        return docstoreClientLocator;
082    }
083
084    public void setDocstoreClientLocator(DocstoreClientLocator docstoreClientLocator) {
085        this.docstoreClientLocator = docstoreClientLocator;
086    }
087
088}