1 package org.kuali.ole.ingest.function;
2
3
4
5 import org.apache.log4j.Logger;
6 import org.kuali.ole.DataCarrierService;
7 import org.kuali.ole.OLEConstants;
8
9 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10 import org.kuali.ole.docstore.common.document.content.enums.DocType;
11 import org.kuali.ole.docstore.common.search.SearchParams;
12 import org.kuali.ole.docstore.common.search.SearchResponse;
13 import org.kuali.ole.docstore.common.search.SearchResult;
14 import org.kuali.ole.docstore.common.search.SearchResultField;
15 import org.kuali.ole.ingest.ISBNUtil;
16 import org.kuali.ole.sys.context.SpringContext;
17 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
18 import org.kuali.rice.krms.framework.engine.Function;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23
24
25
26
27 public class ISBNFunction implements Function {
28
29 private DocstoreClientLocator docstoreClientLocator;
30
31 private static final Logger LOG = Logger.getLogger(ISBNFunction.class);
32
33
34
35
36
37
38 @Override
39 public Object invoke(Object... arguments) {
40 LOG.info(" -----------------> inside isbn function ------------> ");
41 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
42 String existingDocstoreField = (String)(arguments[0]);
43 String isbn = (String)(arguments[1]);
44 String normalizedISBN = null;
45 try {
46 normalizedISBN = new ISBNUtil().normalizeISBN(isbn);
47 } catch (Exception e) {
48 System.out.println(e.getMessage());
49 }
50
51 List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
52 SearchParams searchParams=new SearchParams();
53 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"ISBN",normalizedISBN), "AND"));
54 searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic", "bibIdentifier"));
55 try {
56 SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
57 List<SearchResult> searchResults=searchResponse.getSearchResults();
58 LOG.info(" ---------------> list.size ------------> " + list.size());
59 if(searchResults.size() >=1){
60 LOG.info(" inside if condition of list --------------------> ");
61 for (SearchResult searchResult:searchResults){
62 HashMap<String, Object> bibMap=new HashMap<>();
63 for(SearchResultField searchResultField:searchResult.getSearchResultFields()){
64 if(searchResultField.getFieldValue()!=null && !searchResultField.getFieldValue().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("bibidentifier") ){
65 bibMap.put(OLEConstants.BIB_UNIQUE_ID,searchResultField.getFieldValue());
66 }
67 }
68 list.add(bibMap);
69 }
70 dataCarrierService.addData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE, list);
71 return true;
72 }
73 return false;
74
75 }
76 catch(Exception ex){
77 throw new RuntimeException(ex);
78 }
79
80 }
81
82
83 public DocstoreClientLocator getDocstoreClientLocator() {
84 if (null == docstoreClientLocator) {
85 return SpringContext.getBean(DocstoreClientLocator.class);
86 }
87 return docstoreClientLocator;
88 }
89
90 public void setDocstoreClientLocator(DocstoreClientLocator docstoreClientLocator) {
91 this.docstoreClientLocator = docstoreClientLocator;
92 }
93
94 }