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