View Javadoc
1   package org.kuali.ole.ncip.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.extensiblecatalog.ncip.v2.service.*;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.bo.OleCirculationDesk;
8   import org.kuali.ole.deliver.bo.OleItemSearch;
9   import org.kuali.ole.deliver.bo.OlePatronDocument;
10  import org.kuali.ole.deliver.controller.checkout.CircUtilController;
11  import org.kuali.ole.deliver.service.CircDeskLocationResolver;
12  import org.kuali.ole.deliver.util.DroolsResponse;
13  import org.kuali.ole.deliver.util.OlePatronRecordUtil;
14  import org.kuali.ole.docstore.common.search.SearchResponse;
15  import org.kuali.ole.docstore.common.search.SearchResult;
16  import org.kuali.ole.docstore.common.search.SearchResultField;
17  import org.kuali.ole.ncip.bo.OLENCIPConstants;
18  import org.kuali.ole.ncip.service.OLESIAPIHelperService;
19  import org.kuali.ole.ncip.service.impl.OLECirculationHelperServiceImpl;
20  import org.kuali.ole.select.document.service.OleDocstoreHelperService;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.util.DocstoreUtil;
23  import org.kuali.ole.utility.OleStopWatch;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
27  import org.kuali.rice.coreservice.api.parameter.Parameter;
28  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  
34  /**
35   * Created by pvsubrah on 8/18/15.
36   */
37  public class OLENCIPUtil {
38  
39      private static final Logger LOG = Logger.getLogger(OLENCIPUtil.class);
40  
41      private OlePatronRecordUtil olePatronRecordUtil;
42      private OLESIAPIHelperService oleSIAPIHelperService;
43      private OLECirculationHelperServiceImpl oleCirculationHelperService;
44      private CircDeskLocationResolver circDeskLocationResolver;
45      private OLENCIPAcceptItemUtil olencipAcceptItemUtil;
46      private OleDocstoreHelperService oleDocstoreHelperService;
47      private DocstoreUtil docstoreUtil;
48  
49      public HashMap<String, String> agencyPropertyMap = new HashMap<>();
50  
51      private OlePatronRecordUtil getOlePatronRecordUtil() {
52          if (null == olePatronRecordUtil) {
53              olePatronRecordUtil = (OlePatronRecordUtil) SpringContext.getBean("olePatronRecordUtil");
54          }
55          return olePatronRecordUtil;
56      }
57  
58      public void setOlePatronRecordUtil(OlePatronRecordUtil olePatronRecordUtil) {
59          this.olePatronRecordUtil = olePatronRecordUtil;
60      }
61  
62      public OLESIAPIHelperService getOleSIAPIHelperService() {
63          if (oleSIAPIHelperService == null) {
64              oleSIAPIHelperService = (OLESIAPIHelperService) SpringContext.getService("oleSIAPIHelperService");
65          }
66          return oleSIAPIHelperService;
67      }
68  
69      public void setOleSIAPIHelperService(OLESIAPIHelperService oleSIAPIHelperService) {
70          this.oleSIAPIHelperService = oleSIAPIHelperService;
71      }
72  
73      public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
74          if (null == oleCirculationHelperService) {
75              oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
76          }
77          return oleCirculationHelperService;
78      }
79  
80      public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
81          this.oleCirculationHelperService = oleCirculationHelperService;
82      }
83  
84      private CircDeskLocationResolver getCircDeskLocationResolver() {
85          if (circDeskLocationResolver == null) {
86              circDeskLocationResolver = new CircDeskLocationResolver();
87          }
88          return circDeskLocationResolver;
89      }
90  
91      public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
92          this.circDeskLocationResolver = circDeskLocationResolver;
93      }
94  
95      public OLENCIPAcceptItemUtil getOlencipAcceptItemUtil() {
96          if (null == olencipAcceptItemUtil) {
97              olencipAcceptItemUtil = new OLENCIPAcceptItemUtil();
98          }
99          return olencipAcceptItemUtil;
100     }
101 
102     public void setOlencipAcceptItemUtil(OLENCIPAcceptItemUtil olencipAcceptItemUtil) {
103         this.olencipAcceptItemUtil = olencipAcceptItemUtil;
104     }
105 
106     public OleDocstoreHelperService getOleDocstoreHelperService() {
107         if (oleDocstoreHelperService == null) {
108             oleDocstoreHelperService = SpringContext.getBean(OleDocstoreHelperService.class);
109         }
110         return oleDocstoreHelperService;
111     }
112 
113     public void setOleDocstoreHelperService(OleDocstoreHelperService oleDocstoreHelperService) {
114         this.oleDocstoreHelperService = oleDocstoreHelperService;
115     }
116 
117     public DocstoreUtil getDocstoreUtil() {
118         if (null == docstoreUtil) {
119             docstoreUtil = new DocstoreUtil();
120         }
121         return docstoreUtil;
122     }
123 
124     public void setDocstoreUtil(DocstoreUtil docstoreUtil) {
125         this.docstoreUtil = docstoreUtil;
126     }
127 
128 
129     public AgencyId validateAgency(InitiationHeader initiationHeader, NCIPResponseData ncipResponseData) {
130 
131         AgencyId agencyId = getAgencyId(initiationHeader);
132 
133         agencyPropertyMap = getAgencyPropertyMap(agencyId);
134 
135         if (agencyPropertyMap.size() == 0) {
136             processProblems(ncipResponseData, agencyId.getValue(), ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID), OLENCIPConstants.AGENCY_ID);
137             return null;
138         }
139         return agencyId;
140     }
141 
142     public void processProblems(NCIPResponseData ncipResponseData, String problemValue, String problemDetail, String problemElement) {
143         List<Problem> problems = new ArrayList<Problem>();
144         Problem problem = new Problem();
145         ProblemType problemType = new ProblemType("");
146         problem.setProblemDetail(problemDetail);
147         problem.setProblemElement(problemElement);
148         problem.setProblemType(problemType);
149         problem.setProblemValue(problemValue);
150         problems.add(problem);
151         setProblems(ncipResponseData, problems);
152     }
153 
154     private void setProblems(NCIPResponseData ncipResponseData, List<Problem> problems) {
155         if (ncipResponseData instanceof AcceptItemResponseData) {
156             AcceptItemResponseData acceptItemResponseData = (AcceptItemResponseData) ncipResponseData;
157             acceptItemResponseData.setProblems(problems);
158         } else if (ncipResponseData instanceof LookupUserResponseData) {
159             LookupUserResponseData lookupUserResponseData = (LookupUserResponseData) ncipResponseData;
160             lookupUserResponseData.setProblems(problems);
161         } else if (ncipResponseData instanceof CheckOutItemResponseData) {
162             CheckOutItemResponseData checkOutItemResponseData = (CheckOutItemResponseData) ncipResponseData;
163             checkOutItemResponseData.setProblems(problems);
164         } else if (ncipResponseData instanceof CheckInItemResponseData) {
165             CheckInItemResponseData checkInItemResponseData = (CheckInItemResponseData) ncipResponseData;
166             checkInItemResponseData.setProblems(problems);
167         }
168     }
169 
170     public HashMap<String, String> getAgencyPropertyMap(AgencyId agencyId) {
171         agencyPropertyMap = getOleSIAPIHelperService().getAgencyPropertyMap(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLENCIPConstants.NCIPAPI_PARAMETER_NAME, agencyId.getValue(), agencyPropertyMap);
172         return agencyPropertyMap;
173     }
174 
175     public boolean validateUser(UserId userId, NCIPResponseData ncipResponseData) {
176         if (userId == null) {
177             processProblems(ncipResponseData, "", ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.USER_DOES_NOT_EXIST), OLENCIPConstants.USER);
178             return false;
179         }
180         return true;
181     }
182 
183     public OlePatronDocument validPatron(UserId userId, NCIPResponseData ncipResponseData) {
184         OlePatronDocument olePatronDocument = null;
185         String patronBarcode = userId.getUserIdentifierValue();
186 
187         if (StringUtils.isBlank(patronBarcode)) {
188             processProblems(ncipResponseData, "", ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.USER_IDENTIFIER_VALUE_DOES_NOT_EXIST), OLENCIPConstants.USER);
189             return null;
190         }
191         try {
192             olePatronDocument = getOlePatronRecordUtil().getPatronRecordByBarcode(patronBarcode);
193         } catch (Exception e) {
194             LOG.error("Exception " + e);
195         }
196 
197         if (olePatronDocument == null) {
198             processProblems(ncipResponseData, patronBarcode, ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO), OLENCIPConstants.USER);
199             return null;
200         }
201 
202         String errorMessage = fireLookupUserRules(olePatronDocument);
203         if (StringUtils.isNotBlank(errorMessage)) {
204             processProblems(ncipResponseData, patronBarcode, errorMessage, OLENCIPConstants.USER);
205             return null;
206         }
207 
208         return olePatronDocument;
209     }
210 
211     public OleCirculationDesk validOperator(NCIPResponseData ncipResponseData, String operatorId) {
212         OleCirculationDesk oleCirculationDesk = null;
213         if (StringUtils.isNotBlank(operatorId)) {
214             oleCirculationDesk = getCircDeskLocationResolver().getCircDeskForOpertorId(operatorId);
215         }
216         if (oleCirculationDesk == null) {
217             processProblems(ncipResponseData, operatorId, ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR), OLENCIPConstants.ITEM);
218         }
219         return oleCirculationDesk;
220     }
221 
222     public String validItemType(NCIPResponseData ncipResponseData) {
223         String itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
224 
225         if (!getOlencipAcceptItemUtil().isValidItemType(itemType)) {
226             processProblems(ncipResponseData, itemType, ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_ITM_TYP), OLENCIPConstants.ITEM);
227             return null;
228         }
229         return itemType;
230     }
231 
232     public boolean validLocation(NCIPResponseData ncipResponseData, String location) {
233         boolean validLocation = false;
234         if (StringUtils.isNotBlank(location)) {
235             OleStopWatch oleStopWatch = new OleStopWatch();
236             oleStopWatch.start();
237             validLocation = getOleDocstoreHelperService().isValidLocation(location);
238             oleStopWatch.end();
239             LOG.info("Time taken to validate location : " + oleStopWatch.getTotalTime());
240         }
241         if (!validLocation) {
242             processProblems(ncipResponseData, location, ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC), OLENCIPConstants.ITEM);
243         }
244         return validLocation;
245     }
246 
247     public AgencyId getAgencyId(InitiationHeader initiationHeader) {
248         AgencyId agencyId = null;
249         if (initiationHeader != null && initiationHeader.getFromAgencyId() != null && initiationHeader.getFromAgencyId().getAgencyId() != null
250                 && StringUtils.isNotBlank(initiationHeader.getFromAgencyId().getAgencyId().getValue())) {
251             agencyId = new AgencyId(initiationHeader.getFromAgencyId().getAgencyId().getValue());
252         } else if (initiationHeader != null && initiationHeader.getApplicationProfileType() != null
253                 && StringUtils.isNotBlank(initiationHeader.getApplicationProfileType().getValue())) {
254             agencyId = new AgencyId(initiationHeader.getApplicationProfileType().getValue());
255         } else {
256             agencyId = new AgencyId(getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
257         }
258         return agencyId;
259     }
260 
261     public String fireLookupUserRules(OlePatronDocument olePatronDocument) {
262         OleStopWatch oleStopWatch = new OleStopWatch();
263         oleStopWatch.start();
264         ArrayList<Object> facts = new ArrayList<>();
265         facts.add(olePatronDocument);
266         DroolsResponse droolsResponse = new DroolsResponse();
267         facts.add(droolsResponse);
268         new CircUtilController().fireRules(facts, null, "lookup-user-ncip");
269         oleStopWatch.end();
270         LOG.info("Time taken for fire rules to validate patron : " + oleStopWatch.getTotalTime());
271         return droolsResponse.getErrorMessage().getErrorMessage();
272     }
273 
274     public OleItemSearch getOleItemSearch(String itemBarcode) {
275         OleStopWatch oleStopWatch = new OleStopWatch();
276         oleStopWatch.start();
277         OleItemSearch oleItemSearch = new OleItemSearch();
278         try {
279             org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
280             search_Params.getSearchConditions().add(search_Params.buildSearchCondition("phrase", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "ITEMBARCODE", itemBarcode), ""));
281             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), OLEConstants.ID));
282             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), OLEConstants.TITLE));
283             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), OLEConstants.AUTHOR));
284             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), OLEConstants.ITEM_TYPE));
285             search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), OLEConstants.BIB_IDENTIFIER));
286             SearchResponse searchResponse = getDocstoreUtil().getDocstoreClientLocator().getDocstoreClient().search(search_Params);
287             if (searchResponse.getSearchResults() != null && searchResponse.getSearchResults().size() > 0) {
288                 for (SearchResult searchResult : searchResponse.getSearchResults()) {
289                     for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
290                         if (searchResultField.getFieldName().equalsIgnoreCase(OLEConstants.TITLE)) {
291                             oleItemSearch.setTitle(searchResultField.getFieldValue());
292                         } else if (searchResultField.getFieldName().equalsIgnoreCase(OLEConstants.AUTHOR)) {
293                             oleItemSearch.setAuthor(searchResultField.getFieldValue());
294                         } else if (searchResultField.getFieldName().equalsIgnoreCase(OLEConstants.ID)) {
295                             oleItemSearch.setItemUUID(searchResultField.getFieldValue());
296                         } else if (searchResultField.getFieldName().equalsIgnoreCase(OLEConstants.ITEM_TYPE)) {
297                             oleItemSearch.setItemType(searchResultField.getFieldValue());
298                         } else if (searchResultField.getFieldName().equalsIgnoreCase(OLEConstants.BIB_IDENTIFIER)) {
299                             oleItemSearch.setBibUUID(searchResultField.getFieldValue());
300                         }
301                     }
302                 }
303             }
304         } catch (Exception e) {
305             LOG.error("Exception " + e);
306         }
307         oleStopWatch.end();
308         LOG.info("Time taken to getOleItemSearch : " + oleStopWatch.getTotalTime());
309         return oleItemSearch;
310     }
311 
312     public String getParameter(String name) {
313         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
314         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
315         if(parameter==null){
316             parameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
317             parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
318         }
319         return parameter!=null?parameter.getValue():null;
320     }
321 
322 }