View Javadoc
1   package org.kuali.ole.ncip.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.DataCarrierService;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.OleLoanDocumentsFromSolrBuilder;
8   import org.kuali.ole.deliver.bo.*;
9   import org.kuali.ole.deliver.processor.LoanProcessor;
10  import org.kuali.ole.deliver.service.CircDeskLocationResolver;
11  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
12  import org.kuali.ole.deliver.service.OleLoanDocumentDaoOjb;
13  import org.kuali.ole.deliver.service.impl.OleDeliverDaoJdbc;
14  import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
15  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
16  import org.kuali.ole.docstore.common.document.Bib;
17  import org.kuali.ole.docstore.common.document.Holdings;
18  import org.kuali.ole.docstore.common.document.ItemOleml;
19  import org.kuali.ole.docstore.common.document.content.enums.DocType;
20  import org.kuali.ole.docstore.common.document.content.instance.Item;
21  import org.kuali.ole.docstore.common.document.content.instance.Location;
22  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
23  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
24  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
25  import org.kuali.ole.docstore.common.search.SearchResponse;
26  import org.kuali.ole.docstore.common.search.SearchResult;
27  import org.kuali.ole.docstore.common.search.SearchResultField;
28  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
29  import org.kuali.ole.ncip.bo.*;
30  import org.kuali.ole.ncip.converter.*;
31  import org.kuali.ole.ncip.service.OLECirculationService;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.util.DocstoreUtil;
34  import org.kuali.rice.core.api.config.property.ConfigContext;
35  import org.kuali.rice.core.api.config.property.ConfigurationService;
36  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
37  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
38  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
39  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
40  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
41  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
42  import org.kuali.rice.krad.service.BusinessObjectService;
43  import org.kuali.rice.krad.service.KRADServiceLocator;
44  import org.kuali.rice.krad.util.GlobalVariables;
45  import org.kuali.rice.krms.api.engine.EngineResults;
46  import org.kuali.rice.krms.api.engine.ResultEvent;
47  
48  import java.sql.Timestamp;
49  import java.text.SimpleDateFormat;
50  import java.util.*;
51  
52  /**
53   * Created with IntelliJ IDEA.
54   * User: maheswarang
55   * Date: 7/21/13
56   * Time: 2:13 PM
57   * To change this template use File | Settings | File Templates.
58   */
59  public class OLECirculationServiceImpl implements OLECirculationService {
60      private static final Logger LOG = Logger.getLogger(OLECirculationServiceImpl.class);
61      private BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
62      private OLECirculationHelperServiceImpl oleCirculationHelperService = new OLECirculationHelperServiceImpl();
63      private OLELookupUserConverter oleLookupUserConverter = new OLELookupUserConverter();
64      private OLECheckInItemConverter oleCheckInItemConverter = new OLECheckInItemConverter();
65      private OLECheckOutItemConverter oleCheckOutItemConverter = new OLECheckOutItemConverter();
66      private OLEHoldsConverter oleHoldsConverter = new OLEHoldsConverter();
67      private OLEItemFineConverter oleItemFineConverter = new OLEItemFineConverter();
68      private OLECheckoutItemsConverter oleCheckoutItemsConverter = new OLECheckoutItemsConverter();
69      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
70      private LoanProcessor loanProcessor = new LoanProcessor();
71      private DocstoreUtil docstoreUtil;
72      private ConfigurationService kualiConfigurationService;
73      private Map<String,OleCirculationDesk> oleCirculationDeskMap = getAvailableCirculationDesks();
74      private Map<String,OleDeliverRequestType> oleDeliverRequestTypeMap = getAvailableRequestTypes();
75      private CircDeskLocationResolver circDeskLocationResolver;
76      private OleLoanDocumentsFromSolrBuilder oleLoanDocumentsFromSolrBuilder;
77  
78      private CircDeskLocationResolver getCircDeskLocationResolver() {
79          if (circDeskLocationResolver == null) {
80              circDeskLocationResolver = new CircDeskLocationResolver();
81          }
82          return circDeskLocationResolver;
83      }
84  
85      public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
86          this.circDeskLocationResolver = circDeskLocationResolver;
87      }
88  
89      public ConfigurationService getKualiConfigurationService() {
90          if (kualiConfigurationService == null) {
91              kualiConfigurationService = (ConfigurationService) SpringContext.getBean("kualiConfigurationService");
92          }
93          return kualiConfigurationService;
94      }
95  
96      public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) {
97          this.kualiConfigurationService = kualiConfigurationService;
98      }
99  
100     public DocstoreUtil getDocstoreUtil() {
101 
102         if (docstoreUtil == null) {
103             docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
104 
105         }
106         return docstoreUtil;
107     }
108 
109     private DocstoreClientLocator docstoreClientLocator;
110 
111     public DocstoreClientLocator getDocstoreClientLocator() {
112 
113         if (docstoreClientLocator == null) {
114             docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
115 
116         }
117         return docstoreClientLocator;
118     }
119 
120     public BusinessObjectService getBusinessObjectService() {
121         return businessObjectService;
122     }
123 
124     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
125         this.businessObjectService = businessObjectService;
126     }
127 
128     public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
129         return oleCirculationHelperService;
130     }
131 
132     public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
133         this.oleCirculationHelperService = oleCirculationHelperService;
134     }
135 
136     public OLELookupUserConverter getOleLookupUserConverter() {
137         return oleLookupUserConverter;
138     }
139 
140     public void setOleLookupUserConverter(OLELookupUserConverter oleLookupUserConverter) {
141         this.oleLookupUserConverter = oleLookupUserConverter;
142     }
143 
144     public OLECheckInItemConverter getOleCheckInItemConverter() {
145         return oleCheckInItemConverter;
146     }
147 
148     public void setOleCheckInItemConverter(OLECheckInItemConverter oleCheckInItemConverter) {
149         this.oleCheckInItemConverter = oleCheckInItemConverter;
150     }
151 
152     public OLECheckOutItemConverter getOleCheckOutItemConverter() {
153         return oleCheckOutItemConverter;
154     }
155 
156     public void setOleCheckOutItemConverter(OLECheckOutItemConverter oleCheckOutItemConverter) {
157         this.oleCheckOutItemConverter = oleCheckOutItemConverter;
158     }
159 
160     @Override
161     public String lookupUser(String patronBarcode, String operator, String agencyId, boolean isSIP2Request) {
162         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator + "agencyId : " + agencyId );
163         OLELookupUser lookupUser = new OLELookupUser();
164         if (!loanProcessor.hasCirculationDesk(operator)) {
165             lookupUser.setCode("001");
166             lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
167         } else {
168             Map<String, String> patronMap = new HashMap<String, String>();
169             patronMap.put(OLEConstants.BARCODE, patronBarcode);
170             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
171             if (olePatronDocumentList.size() > 0) {
172                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
173                 lookupUser = oleCirculationHelperService.initialiseLookupUser(olePatronDocument, agencyId);
174                 if (olePatronDocument.isGeneralBlock() || oleCirculationHelperService.isPatronExpired(olePatronDocument) || !olePatronDocument.isActiveIndicator() || oleCirculationHelperService.isPatronActivated(olePatronDocument)){
175                     lookupUser.setValidPatron(false);
176                 }else{
177                     lookupUser.setValidPatron(true);
178                 }
179                 try {
180                     List<OleLoanDocument> oleLoanDocumentList = getOleLoanDocumentsFromSolrBuilder()
181                             .getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId(), null);
182                     List<OLECheckedOutItem> oleCheckedOutItemList = getPatronCheckedOutItemList(oleLoanDocumentList,olePatronDocument.getOleBorrowerType().getBorrowerTypeCode(),agencyId!=null?false:true);
183                     //List<OLECheckedOutItem> checkedOutItemList = getPatronCheckedOutItemList(olePatronDocument.getOleLoanDocuments(),olePatronDocument.getOleBorrowerType().getBorrowerTypeCode());
184                     OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
185                     if (oleCheckedOutItemList != null && oleCheckedOutItemList.size() > 0) {
186                         oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
187                     }
188                     lookupUser.setOleCheckedOutItems(oleCheckedOutItems);
189                 } catch (Exception e) {
190                     LOG.info("Exception Occurred While Retrieving the checked out items");
191                     LOG.error(e);
192                 }
193                 try {
194                     List<OLEHold> oleHoldList = getHoldsList(olePatronDocument.getOleDeliverRequestBos());
195                     OLEHolds oleHolds = new OLEHolds();
196                     if (oleHoldList != null && oleHoldList.size() > 0) {
197                         oleHolds.setOleHoldList(oleHoldList);
198                     }
199                     lookupUser.setOleHolds(oleHolds);
200                 } catch (Exception e) {
201                     LOG.info("Exception Occurred While Retrieving the Hold items");
202                     LOG.error(e);
203                 }
204                 try {
205                     OLEItemFines oleItemFines = (OLEItemFines) oleItemFineConverter.generateCheckoutItemObject(getFine(patronBarcode, operator));
206                     lookupUser.setOleItemFines(oleItemFines);
207                 } catch (Exception e) {
208                     LOG.info("Exception Occurred While Retrieving Fine");
209                     LOG.error(e);
210                 }
211                 lookupUser.setCode("000");
212                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
213             } else {
214                 lookupUser.setCode("002");
215                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
216             }
217         }
218         String responseMessage = "";
219         if(isSIP2Request){
220             responseMessage = oleLookupUserConverter.generateLookupUserResponseXmlForSip2(lookupUser);
221         }else{
222             responseMessage = oleLookupUserConverter.generateLookupUserResponseXml(lookupUser);
223         }
224         return responseMessage;
225     }
226 
227     @Override
228     public String getCheckedOutItems(String patronBarcode, String operator) throws Exception {
229         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator  );
230         LOG.info("Start CHECK out " + System.currentTimeMillis());
231         OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
232         String patronType = "";
233         String checkoutItemString = "";
234         if (!loanProcessor.hasCirculationDesk(operator)) {
235             oleCheckedOutItems.setCode("001");
236             oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
237             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
238         } else {
239 
240             Map<String, String> patronMap = new HashMap<String, String>();
241             patronMap.put(OLEConstants.BARCODE, patronBarcode);
242             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
243             if (olePatronDocumentList.size() > 0) {
244                 Map<String, String> patronDocMap = new HashMap<String, String>();
245                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
246                 patronType = olePatronDocument.getBorrowerType();
247                 Map<String, String> borrowerTypeMap = new HashMap<String, String>();
248                 borrowerTypeMap.put(OLEConstants.BORROWER_TYPE_ID, patronType);
249                 List<OleBorrowerType> oleBorrowerTypeList = (List<OleBorrowerType>) businessObjectService.findMatching(OleBorrowerType.class, borrowerTypeMap);
250                 if (oleBorrowerTypeList.size() > 0) {
251                     patronType = oleBorrowerTypeList.get(0).getBorrowerTypeCode();
252                 }
253                 patronDocMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
254                 List<OleLoanDocument> oleLoanDocumentList = getOleLoanDocumentsFromSolrBuilder()
255                         .getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId(), null);
256                 if (oleLoanDocumentList != null && oleLoanDocumentList.size() > 0) {
257                     List<OLECheckedOutItem> oleCheckedOutItemList = getPatronCheckedOutItemList(oleLoanDocumentList,patronType,true);
258                     oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
259                     oleCheckedOutItems.setCode("000");
260                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
261                     oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
262                 } else {
263                     oleCheckedOutItems.setCode("004");
264                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_LOAN));
265                     oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_LOAN));
266                 }
267             } else {
268                 oleCheckedOutItems.setCode("002");
269                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
270                 oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
271 
272             }
273         }
274         checkoutItemString = oleCheckoutItemsConverter.generateCheckOutItemXml(oleCheckedOutItems);
275         LOG.info("END CHECK out " + System.currentTimeMillis());
276         return checkoutItemString;  //To change body of implemented methods use File | Settings | File Templates.
277     }
278 
279     private OleLoanDocumentsFromSolrBuilder getOleLoanDocumentsFromSolrBuilder() {
280         if (null == oleLoanDocumentsFromSolrBuilder) {
281             oleLoanDocumentsFromSolrBuilder = new OleLoanDocumentsFromSolrBuilder();
282         }
283         return oleLoanDocumentsFromSolrBuilder;
284     }
285 
286     public void setOleLoanDocumentsFromSolrBuilder(OleLoanDocumentsFromSolrBuilder oleLoanDocumentsFromSolrBuilder) {
287         this.oleLoanDocumentsFromSolrBuilder = oleLoanDocumentsFromSolrBuilder;
288     }
289 
290     @Override
291     public String placeRequest(String patronBarcode, String operatorId, String itemBarcode, String requestType, String pickUpLocation, String itemLocation,String bibId,String requestLevel,java.sql.Date requestExpiryDate) {
292         String responseMessage = oleDeliverRequestDocumentHelperService.placeRequest(patronBarcode, operatorId, itemBarcode, requestType, pickUpLocation, null, itemLocation, null, null, null, null, false,bibId,requestLevel,requestExpiryDate);
293         return responseMessage;  //To change body of implemented methods use File | Settings | File Templates.
294     }
295 
296     @Override
297     public String overridePlaceRequest(String patronBarcode, String operatorId, String itemBarcode, String requestType, String pickUpLocation, String itemLocation,String bibId,String requestLevel,java.sql.Date requestExpiryDate) {
298         String responseMessage = oleDeliverRequestDocumentHelperService.overridePlaceRequest(patronBarcode, operatorId, itemBarcode, requestType, pickUpLocation, null, itemLocation, null, null, null, null, false,bibId,requestLevel,requestExpiryDate);
299         return responseMessage;  //To change body of implemented methods use File | Settings | File Templates.
300     }
301 
302     @Override
303     public String cancelRequest(String operator, String patronBarcode, String itemBarcode) {
304         LOG.info("Inside cancel Request : Operator : " + operator + "patron Barcode : " + patronBarcode + "item barcode");
305         OLECancelRequest oleCancelRequest = new OLECancelRequest();
306         OlePatronDocument olePatronDocument = null;
307         if (!loanProcessor.hasCirculationDesk(operator)) {
308             oleCancelRequest.setCode("001");
309             oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
310             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
311         } else {
312             Map<String, String> queryMap = new HashMap<String, String>();
313             queryMap.put(OLEConstants.BARCODE, patronBarcode);
314             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, queryMap);
315             if (olePatronDocumentList.size() > 0) {
316                 olePatronDocument = olePatronDocumentList.get(0);
317             } else if (olePatronDocumentList.size() == 0) {
318                 oleCancelRequest.setCode("002");
319                 oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
320                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
321             }
322             if (olePatronDocument != null) {
323                 queryMap = new HashMap<String, String>();
324                 queryMap.put(OLEConstants.OleDeliverRequest.BORROWER_ID, olePatronDocument.getOlePatronId());
325                 queryMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
326                 List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) businessObjectService.findMatching(OleDeliverRequestBo.class, queryMap);
327                 if (oleDeliverRequestBoList != null && oleDeliverRequestBoList.size() > 0) {
328                     OleDeliverRequestBo oleDeliverRequestBo = oleDeliverRequestBoList.get(0);
329                     oleDeliverRequestBo.setOperatorCreateId(operator);
330                     oleDeliverRequestDocumentHelperService.cancelDocument(oleDeliverRequestBo);
331                     oleCancelRequest.setCode("007");
332                     oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.REQUEST_SUCCESSFULLEY_CANCELLED));
333                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.REQUEST_SUCCESSFULLEY_CANCELLED));
334                 } else {
335                     oleCancelRequest.setCode("008");
336                     oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_REQUEST_FOUND));
337                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_REQUEST_FOUND));
338                     return new OLECancelRequestConverter().generateCancelRequestXml(oleCancelRequest);
339                 }
340             }
341         }
342 
343         return new OLECancelRequestConverter().generateCancelRequestXml(oleCancelRequest);
344     }
345 
346 
347     @Override
348     public String cancelRequests(String operator, String requestId) {
349         LOG.info("Inside cancel request  : Operator : " + operator + "Request id : " + requestId);
350         OLECancelRequest oleCancelRequest = new OLECancelRequest();
351         if (!loanProcessor.hasCirculationDesk(operator)) {
352             oleCancelRequest.setCode("001");
353             oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
354             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
355         } else {
356             Map<String, String> requestMap = new HashMap<String, String>();
357             requestMap.put(OLEConstants.OleDeliverRequest.REQUEST_ID, requestId);
358             List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) businessObjectService.findMatching(OleDeliverRequestBo.class, requestMap);
359             if (oleDeliverRequestBoList != null && oleDeliverRequestBoList.size() > 0) {
360                 OleDeliverRequestBo oleDeliverRequestBo = oleDeliverRequestBoList.get(0);
361                 oleDeliverRequestBo.setOperatorCreateId(operator);
362                 oleDeliverRequestDocumentHelperService.cancelDocument(oleDeliverRequestBo);
363                 oleCancelRequest.setCode("007");
364                 oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.REQUEST_SUCCESSFULLEY_CANCELLED));
365                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.REQUEST_SUCCESSFULLEY_CANCELLED));
366             } else {
367                 oleCancelRequest.setCode("008");
368                 oleCancelRequest.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_REQUEST_FOUND_REQUEST_ID));
369                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_REQUEST_FOUND_REQUEST_ID));
370                 return new OLECancelRequestConverter().generateCancelRequestXml(oleCancelRequest);
371             }
372         }
373         return new OLECancelRequestConverter().generateCancelRequestXml(oleCancelRequest);
374     }
375 
376     @Override
377     public String renewItem(String patronBarcode, String operator, String itemBarcode, boolean isSIP2Request) {
378         LOG.info("Inside cancel request  : Operator : " + operator + "Patron Barcode : " + patronBarcode + "Item barcode : "+ itemBarcode);
379         String responseMessage = oleCirculationHelperService.renewItem(patronBarcode, operator, itemBarcode, isSIP2Request);
380         return responseMessage;  //To change body of implemented methods use File | Settings | File Templates.
381     }
382 
383     @Override
384     public String renewItemList(String patronBarcode, String operator, String itemBarcode, boolean isSIP2Request) {
385         LOG.info("Inside cancel request  : Operator : " + operator + "Patron Barcode : " + patronBarcode + "Item barcode : "+ itemBarcode);
386         String responseMessage = oleCirculationHelperService.renewItemList(patronBarcode, operator, itemBarcode,isSIP2Request);
387         return responseMessage;  //To change body of implemented methods use File | Settings | File Templates.
388     }
389 
390     @Override
391     public String acceptItem(String patronBarcode, String operator, String itemBarcode, String callNumber, String title, String author, String itemType, String itemLocation, String dateExpires, String requestType, String pickUpLocation) {
392         OLEAcceptItem oleAcceptItem = new OLEAcceptItem();
393         String itemIdentifier = null;
394         if (!loanProcessor.hasCirculationDesk(operator)) {
395             oleAcceptItem.setCode("026");
396             oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
397             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
398             return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
399         }  if(!vaildPatron(patronBarcode)){
400             oleAcceptItem.setCode("002");
401             oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
402             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
403             return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
404         }
405 
406         if (requestType == null || (requestType != null && requestType.trim().isEmpty())) {
407             oleAcceptItem.setCode("012");
408             oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_RQST_TYP));
409             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_RQST_TYP));
410             return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
411         }
412         if (requestType != null && !requestType.trim().isEmpty()) {
413             Map<String, String> requestTypeMap = new HashMap<String, String>();
414             requestTypeMap.put(OLEConstants.OleDeliverRequest.REQUEST_TYPE_CD, requestType);
415             List<OleDeliverRequestType> oleDeliverRequestTypeList = (List<OleDeliverRequestType>) getBusinessObjectService().findMatching(OleDeliverRequestType.class, requestTypeMap);
416             if (oleDeliverRequestTypeList != null && (oleDeliverRequestTypeList.size() == 0)) {
417                 oleAcceptItem.setCode("012");
418                 oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_RQST_TYP));
419                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_RQST_TYP));
420                 return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
421             }
422         }
423         if (pickUpLocation != null) {
424             Map<String, String> circulationDeskMap = new HashMap<String, String>();
425             circulationDeskMap.put(OLEConstants.OleCirculationDesk.OLE_CIRCULATION_DESK_CD, pickUpLocation);
426             List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) getBusinessObjectService().findMatching(OleCirculationDesk.class, circulationDeskMap);
427             if (oleCirculationDeskList != null && oleCirculationDeskList.size() == 0) {
428                 oleAcceptItem.setCode("013");
429                 oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_PK_UP_LOCN));
430                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_PK_UP_LOCN));
431                 return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
432             }
433         }
434         try {
435             itemIdentifier = oleCirculationHelperService.acceptItem(patronBarcode, operator, itemBarcode, callNumber, title, author, itemType, itemLocation, dateExpires, requestType, pickUpLocation);
436             if (null == itemIdentifier) {
437                 oleAcceptItem.setCode("031");
438                 oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_EXIST));
439                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_EXIST));
440                 return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
441             }
442             if ("".equals(itemIdentifier)) {
443                 itemIdentifier = null;
444             }
445                 String responseMessage = oleDeliverRequestDocumentHelperService.placeRequest(patronBarcode, operator, itemBarcode, requestType, pickUpLocation, itemIdentifier, itemLocation, itemType, title, author, callNumber, true,null,"Item Level",null);
446                 responseMessage = responseMessage.replaceAll("&lt;br/&gt;", "");
447                 responseMessage = responseMessage.replaceAll("<br/>", "");
448                 OLEPlaceRequestConverter olePlaceRequestConverter = new OLEPlaceRequestConverter();
449                 OLEPlaceRequest olePlaceRequest = (OLEPlaceRequest) olePlaceRequestConverter.generatePlaceRequestObject(responseMessage);
450             if(!olePlaceRequest.getMessage().contains(OLEConstants.RQST_SUCCESS)){
451                 org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(itemIdentifier);
452                 String bibId = item.getHolding().getBib().getId();
453 
454                 getDocstoreClientLocator().getDocstoreClient().deleteBib(bibId);
455             }
456                 oleAcceptItem.setMessage(olePlaceRequest.getMessage());
457                 oleAcceptItem.setCode(olePlaceRequest.getCode());
458                 return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
459 
460         } catch (Exception e) {
461             oleAcceptItem.setCode("033");
462             oleAcceptItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_NOT_CREATED));
463             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_NOT_CREATED));
464             return new OLEAcceptItemConverter().generateAcceptItemXml(oleAcceptItem);
465         }
466     }
467 
468     @Override
469     public String checkInItem(String patronBarcode, String operator, String itemBarcode, String deleteIndicator, boolean isSIP2Request) {
470         LOG.info( " Inside check in item : Patron Barcode :"  +patronBarcode + "operator : "+ operator + "item barcode : "+ itemBarcode);
471         if (!loanProcessor.hasCirculationDesk(operator)) {
472             OLECheckInItem oleCheckInItem = new OLECheckInItem();
473             oleCheckInItem.setCode("026");
474             oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
475             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
476             return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
477         }
478         String responseMessage = oleCirculationHelperService.checkInItem(patronBarcode, operator, itemBarcode, deleteIndicator,isSIP2Request);
479         return responseMessage;
480     }
481 
482     @Override
483     public String checkOutItem(String patronBarcode, String operator, String itemBarcode, boolean isSIP2Request) {
484         LOG.info( " Inside checkOutItem : Patron Barcode :"  +patronBarcode + "operator : "+ operator + "item barcode : "+ itemBarcode);
485         String responseMessage = oleCirculationHelperService.checkOutItem(patronBarcode, operator, itemBarcode,isSIP2Request);
486         return responseMessage;
487     }
488 
489 
490     public List<OLECheckedOutItem> getOleCheckOutItemList(List<OleLoanDocument> oleLoanDocumentList, String patronType) throws Exception {
491         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
492         List<OLECheckedOutItem> checkedOutItemList = new ArrayList<OLECheckedOutItem>();
493         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
494         for (OleLoanDocument oleLoanDocument : oleLoanDocumentList) {
495             Map<String, Object> detailMap = oleDeliverRequestDocumentHelperService.retrieveBIbItemHoldingData(oleLoanDocument.getItemUuid());
496             Bib bib = (Bib) detailMap.get(OLEConstants.BIB);
497             Item item = (Item) detailMap.get(OLEConstants.ITEM);
498             OleHoldings oleHoldings = (OleHoldings) detailMap.get(OLEConstants.HOLDING);
499             org.kuali.ole.docstore.common.document.Item item1 = (org.kuali.ole.docstore.common.document.Item) detailMap.get("documentItem");
500             String itemLocation = null;
501             if (item1.getLocation() == null || (item1.getLocation() != null && item1.getLocation().trim().isEmpty())) {
502                 itemLocation = getDocstoreUtil().getLocation(oleHoldings.getLocation(), new StringBuffer(""));
503             } else {
504                 itemLocation = item1.getLocation();
505             }
506             Map<String, String> locationMap = getCircDeskLocationResolver().getLocationMap(itemLocation);
507             oleLoanDocument.setItemInstitution(locationMap.get(OLEConstants.ITEM_INSTITUTION));
508             oleLoanDocument.setItemCampus(locationMap.get(OLEConstants.ITEM_CAMPUS));
509             oleLoanDocument.setItemCollection(locationMap.get(OLEConstants.ITEM_COLLECTION));
510             oleLoanDocument.setItemLibrary(locationMap.get(OLEConstants.ITEM_LIBRARY));
511             oleLoanDocument.setItemLocation(locationMap.get(OLEConstants.ITEM_SHELVING));
512             oleLoanDocument.setInstanceUuid(oleHoldings.getHoldingsIdentifier());
513             oleLoanDocument.setBibUuid(bib.getId());
514             oleLoanDocument.setAuthor(bib.getAuthor());
515             oleLoanDocument.setTitle(bib.getTitle());
516             OLECheckedOutItem oleCheckedOutItem = new OLECheckedOutItem();
517             if (item != null) {
518                 oleLoanDocument.setItemUuid(item.getItemIdentifier());
519                 if (item.getItemType() != null) {
520                     oleCheckedOutItem.setItemType(item.getItemType().getCodeValue());
521                 }
522                 if (item.getCallNumber() != null) {
523                     oleCheckedOutItem.setCallNumber(item.getCallNumber().getNumber());
524                 } else if (oleHoldings != null && oleHoldings.getCallNumber() != null) {
525                     oleCheckedOutItem.setCallNumber(oleHoldings.getCallNumber().getNumber());
526                 }
527                 if (item.getCopyNumber() != null && !item.getCopyNumber().isEmpty()) {
528                     oleCheckedOutItem.setCopyNumber(item.getCopyNumber());
529                 } else if (oleHoldings != null && oleHoldings.getCopyNumber() != null && !oleHoldings.getCopyNumber().isEmpty()) {
530                     oleCheckedOutItem.setCopyNumber(oleHoldings.getCopyNumber());
531                 }
532                 if(item.getEnumeration()!=null){
533                     oleCheckedOutItem.setVolumeNumber(item.getEnumeration());
534                 }else{
535                 oleCheckedOutItem.setVolumeNumber(item.getVolumeNumber());
536                 }
537             }
538             oleCheckedOutItem.setCatalogueId(bib.getId());
539             oleCheckedOutItem.setItemId(oleLoanDocument.getItemId());
540             oleCheckedOutItem.setLoanDate(new Timestamp(oleLoanDocument.getCreateDate().getTime()).toString());
541             if (oleLoanDocument.getLoanDueDate() != null) {
542                 oleCheckedOutItem.setDueDate(oleLoanDocument.getLoanDueDate().toString());
543                 if ((fmt.format(oleLoanDocument.getLoanDueDate())).compareTo(fmt.format(new Date(System.currentTimeMillis()))) > 0) {
544                     oleCheckedOutItem.setOverDue(false);
545                 }
546                 else{
547                     oleCheckedOutItem.setOverDue(true);
548                 }
549             } else {
550                 oleCheckedOutItem.setDueDate((new java.sql.Timestamp(new Date(2025, 1, 1).getTime()).toString()));
551             }
552 
553             if (oleLoanDocument.getRenewalLoanDueDate() != null){
554                 oleCheckedOutItem.setDateRenewed(oleLoanDocument.getRenewalLoanDueDate().toString());
555             }
556             else{
557                 oleCheckedOutItem.setDateRenewed("");
558             }
559 
560             int renewalDaysFromPolicy = getRenewalDays(oleCheckedOutItem.getItemType(), oleLoanDocument, patronType, oleLoanDocument.getNumberOfRenewals());
561 
562             oleCheckedOutItem.setNumberOfRenewals(String.valueOf(renewalDaysFromPolicy));
563             oleCheckedOutItem.setTitle(oleLoanDocument.getTitle());
564             oleCheckedOutItem.setAuthor(oleLoanDocument.getAuthor());
565             oleCheckedOutItem.setAcquiredFine("");
566             oleCheckedOutItem.setDateRecalled("");
567             if (oleLoanDocument.getNoOfOverdueNoticesSentForBorrower() != null) {
568                 oleCheckedOutItem.setNumberOfOverdueSent(oleLoanDocument.getNoOfOverdueNoticesSentForBorrower());
569             } else {
570                 oleCheckedOutItem.setNumberOfOverdueSent("1");
571             }
572             //oleCheckedOutItem.setAcquiredFine(oleLoanDocument.getFineRate().toString());
573             checkedOutItemList.add(oleCheckedOutItem);
574         }
575 
576         return checkedOutItemList;
577     }
578 
579     @Override
580     public String getFine(String patronBarcode, String operator) throws Exception {
581         LOG.info("Inside Get Fine : Patron Barcode : " + patronBarcode + "Operator : " + operator);
582         OLEItemFineConverter oleItemFineConverter = new OLEItemFineConverter();
583         OLEItemFines oleItemFines = new OLEItemFines();
584         String itemFineString = "";
585         if (!loanProcessor.hasCirculationDesk(operator)) {
586             oleItemFines.setCode("001");
587             oleItemFines.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
588         } else {
589             Map<String, String> patronMap = new HashMap<String, String>();
590             patronMap.put(OLEConstants.BARCODE, patronBarcode);
591             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
592             if (olePatronDocumentList.size() > 0) {
593                 Map<String, String> patronDocMap = new HashMap<String, String>();
594                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
595                 patronDocMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
596                 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) businessObjectService.findMatching(PatronBillPayment.class, patronDocMap);
597                 if (patronBillPaymentList.size() > 0) {
598                     List<OLEItemFine> oleItemFineList = getFineItemList(patronBillPaymentList);
599                     oleItemFines.setOleItemFineList(oleItemFineList);
600                     oleItemFines.setCode("000");
601                     oleItemFines.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
602                 } else {
603                /*itemFineString="<fine><patronId>"+patronId+"</patronId><operatorId>"+operatorId+"</operatorId><message>Not having any fine amount</message></fine>";*/
604                     oleItemFines.setCode("005");
605                     oleItemFines.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_FINE));
606                     itemFineString = itemFineString = oleItemFineConverter.generateCheckOutItemXml(oleItemFines);
607                 }
608 
609             } else {
610                 oleItemFines.setCode("002");
611                 oleItemFines.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
612             }
613         }
614         return oleItemFineConverter.generateCheckOutItemXml(oleItemFines);
615     }
616 
617     public List<OLEItemFine> getFineItemList(List<PatronBillPayment> olePatronBillDocumentList) throws Exception {
618         List<OLEItemFine> oleItemFineList = new ArrayList<OLEItemFine>();
619         //Fee
620         org.kuali.ole.docstore.common.document.Item item = null;
621         for (PatronBillPayment olePatronBillPayment : olePatronBillDocumentList) {
622             //to do
623             List<FeeType> feeTypeList = olePatronBillPayment.getFeeType();
624             for (FeeType feeType : feeTypeList) {
625                 OLEItemFine oleItemFine = new OLEItemFine();
626                 if (feeType.getItemUuid() != null) {
627                     item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(feeType.getItemUuid());
628                     oleItemFine.setCatalogueId(item.getHolding().getBib().getId());
629                     oleItemFine.setTitle(item.getHolding().getBib().getTitle());
630                     oleItemFine.setAuthor(item.getHolding().getBib().getAuthor());
631                 }
632                 oleItemFine.setPatronBillId(olePatronBillPayment.getBillNumber());
633                 oleItemFine.setAmount((feeType.getFeeAmount() != null ? feeType.getFeeAmount().bigDecimalValue() : OLEConstants.BIGDECIMAL_DEF_VALUE));
634                 oleItemFine.setBalance((feeType.getBalFeeAmount() != null ? feeType.getBalFeeAmount().bigDecimalValue() : OLEConstants.BIGDECIMAL_DEF_VALUE));
635                 oleItemFine.setBillDate(feeType.getBillDate().toString());
636                 int noOfPayment = feeType.getItemLevelBillPaymentList().size();
637                 oleItemFine.setNoOfPayments(new Integer(noOfPayment).toString());
638                 if (feeType.getOleFeeType() != null) {
639                     oleItemFine.setReason(feeType.getOleFeeType().getFeeTypeName());
640                     oleItemFine.setFeeType(feeType.getOleFeeType().getFeeTypeCode());
641                 } else {
642                     oleItemFine.setReason(feeType.getFeeType());
643                     oleItemFine.setFeeType(feeType.getFeeType());
644                 }
645                 oleItemFine.setDateCharged(feeType.getBillDate().toString());
646                 oleItemFineList.add(oleItemFine);
647             }
648         }
649 
650         return oleItemFineList;
651     }
652 
653     @Override
654     public String getHolds(String patronBarcode, String operator) throws Exception {
655         LOG.info("Inside Get Holds . Patron Barcode : " +patronBarcode + "Operator : "+ operator );
656         OLEHoldsConverter oleHoldConverter = new OLEHoldsConverter();
657         OLEHolds oleHolds = new OLEHolds();
658         String itemFineString = "";
659         if (!loanProcessor.hasCirculationDesk(operator)) {
660             oleHolds.setCode("001");
661             oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
662         } else {
663             Map<String, String> patronMap = new HashMap<String, String>();
664             patronMap.put(OLEConstants.BARCODE, patronBarcode);
665             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
666             if (olePatronDocumentList.size() > 0) {
667                 Map<String, String> patronDocMap = new HashMap<String, String>();
668                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
669                 patronDocMap.put(OLEConstants.OleDeliverRequest.BORROWER_ID, olePatronDocument.getOlePatronId());
670                 List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) businessObjectService.findMatching(OleDeliverRequestBo.class, patronDocMap);
671                 if (oleDeliverRequestBoList.size() > 0) {
672                     List<OLEHold> oleHoldsList = getHoldsList(oleDeliverRequestBoList);
673                     oleHolds.setOleHoldList(oleHoldsList);
674                     oleHolds.setCode("000");
675                     oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
676                 } else {
677                     oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_HOLD));
678                     oleHolds.setCode("006");
679                 }
680             } else {
681                 oleHolds.setCode("002");
682                 oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
683             }
684         }
685         return oleHoldConverter.generateHoldsmXml(oleHolds);
686     }
687 
688     public List<OLEHold> getHoldsList(List<OleDeliverRequestBo> oleDeliverRequestBoList) throws Exception {
689         //String availableDate = "";
690         //String callNumber="";
691         String copyNumber="";
692 
693         String[] availableDates;
694         List<OLEHold> oleHoldList = new ArrayList<OLEHold>();
695         for (OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList) {
696             OLEHold oleHold = new OLEHold();
697             oleHold.setItemId(oleDeliverRequestBo.getItemId());
698             Map<String, Object> detailMap = oleDeliverRequestDocumentHelperService.retrieveBIbItemHoldingData(oleDeliverRequestBo.getItemUuid());
699             Bib bib = (Bib) detailMap.get(OLEConstants.BIB);
700             Item item = (Item) detailMap.get(OLEConstants.ITEM);
701             OleHoldings oleHoldings  = (OleHoldings) detailMap.get(OLEConstants.HOLDING);
702             org.kuali.ole.docstore.common.document.Item item1 = (org.kuali.ole.docstore.common.document.Item) detailMap.get("documentItem");
703             oleHold.setCatalogueId(bib.getId());
704             oleHold.setRequestId(oleDeliverRequestBo.getRequestId());
705             if (item.getItemStatus() != null) {
706                 oleHold.setAvailableStatus(item.getItemStatus().getCodeValue());
707             }
708 
709             /*if(item.getCallNumber()!=null && !StringUtils.isEmpty(item.getCallNumber().getNumber())){
710                 callNumber = loanProcessor.getItemCallNumber(item.getCallNumber());
711             }else {
712                 callNumber = loanProcessor.getItemCallNumber(oleHoldings.getCallNumber());
713             }*/
714             if(item.getCopyNumber()!=null && !item.getCopyNumber().isEmpty()){
715                 copyNumber=item.getCopyNumber();
716             }
717             else{
718                 copyNumber=oleHoldings.getCopyNumber()!=null? oleHoldings.getCopyNumber():"";
719             }
720             oleHold.setTitle(bib.getTitle());
721             oleHold.setAuthor(bib.getAuthor());
722             oleHold.setVolumeNumber(item.getEnumeration()!=null ? item.getEnumeration():"");
723             oleHold.setCallNumber(loanProcessor.getItemCallNumber(item.getCallNumber(),oleHoldings.getCallNumber()));
724             oleHold.setCopyNumber(copyNumber);
725             if (item.getItemType() != null)
726                 oleHold.setItemType(item.getItemType().getCodeValue());
727             if (oleDeliverRequestBo.getRequestTypeId() != null && !oleDeliverRequestBo.getRequestTypeId().isEmpty()) {
728                 if (oleDeliverRequestBo.getRequestTypeId().equals("1") || oleDeliverRequestBo.getRequestTypeId().equals("2")) {
729                     oleHold.setRecallStatus(OLEConstants.YES);
730                 } else {
731                     oleHold.setRecallStatus(OLEConstants.NO);
732                 }
733             }
734             if (oleDeliverRequestBo.getRequestExpiryDate() != null) {
735                 oleHold.setExpiryDate(oleDeliverRequestBo.getRequestExpiryDate().toString());
736             }
737             if (oleDeliverRequestBo.getCreateDate() != null) {
738                 oleHold.setCreateDate(oleDeliverRequestBo.getCreateDate().toString());
739             }
740             if (oleDeliverRequestBo.getBorrowerQueuePosition() != null) {
741                 oleHold.setPriority(oleDeliverRequestBo.getBorrowerQueuePosition().toString());
742             }
743             oleHold.setPickupLocation(oleDeliverRequestBo.getPickUpLocationId());
744             if (oleDeliverRequestBo.getRecallDueDate() != null) {
745                 oleHold.setDateRecalled(oleDeliverRequestBo.getRecallDueDate().toString());
746             }
747             if (oleDeliverRequestBo.getRecallDueDate() != null) {
748                 oleHold.setDateRecalled(oleDeliverRequestBo.getRecallDueDate().toString());
749             }
750             if (oleDeliverRequestTypeMap!=null && oleDeliverRequestTypeMap.size() > 0) {
751                 if(oleDeliverRequestTypeMap.get(oleDeliverRequestBo.getRequestTypeId())!=null){
752                     oleHold.setRequestType(oleDeliverRequestTypeMap.get(oleDeliverRequestBo.getRequestTypeId()).getRequestTypeCode());
753                 }
754             }
755             Map<String, String> loanMap = new HashMap<String, String>();
756             loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, oleDeliverRequestBo.getItemId());
757             List<OleLoanDocument> oleLoanDocumentList = (List<OleLoanDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class, loanMap);
758             if (oleLoanDocumentList.size() > 0) {
759                 if (oleLoanDocumentList.get(0).getLoanDueDate() != null) {
760                     availableDates = oleLoanDocumentList.get(0).getLoanDueDate().toString().split(" ");
761                     if (availableDates != null && availableDates.length > 0) {
762                         oleHold.setAvailableDate(availableDates[0]);
763                     } else {
764                         oleHold.setAvailableDate(oleLoanDocumentList.get(0).getLoanDueDate().toString());
765                     }
766                     if (oleDeliverRequestBo.getPickUpLocationId() != null) {
767                         if (oleCirculationDeskMap.size()>0 && oleCirculationDeskMap.get(oleDeliverRequestBo.getPickUpLocationId())!=null) {
768                             oleHold.setDateAvailableExpires(addDate(new java.sql.Date(oleLoanDocumentList.get(0).getLoanDueDate().getTime()), Integer.parseInt(oleCirculationDeskMap.get(oleDeliverRequestBo.getPickUpLocationId()).getOnHoldDays())).toString());
769                         }
770                     }
771                 } else {
772                     oleHold.setAvailableDate(OLEConstants.INDEFINITE);
773                     oleHold.setDateAvailableExpires(OLEConstants.INDEFINITE);
774                 }
775             }
776             if (oleDeliverRequestBo.getRequestTypeId().equals("2") || oleDeliverRequestBo.getRequestTypeId().equals("4") || oleDeliverRequestBo.getRequestTypeId().equals("6")) {
777                 oleHold.setReserve(true);
778             } else {
779                 oleHold.setReserve(false);
780             }
781 
782             oleHoldList.add(oleHold);
783         }
784 
785         return oleHoldList;
786     }
787 
788     private java.sql.Date addDate(java.sql.Date in, int daysToAdd) {
789         if (in == null) {
790             return null;
791         }
792         GregorianCalendar cal = new GregorianCalendar();
793         cal.setTime(in);
794         cal.add(Calendar.DAY_OF_MONTH, daysToAdd);
795         return new java.sql.Date(cal.getTime().getTime());
796     }
797 
798     private int getRenewalDays(String itemType, OleLoanDocument oleLoanDocument, String borrowerType, String numberOfRenewals) {
799         String agendaName = OLEConstants.RENEWAL_AGENDA_NM;
800         int renewalCount = 1;
801         boolean renewalExceeds = false;
802         Map<String,String> requestMap = new HashMap<String,String>();
803         requestMap.put("itemUuid",oleLoanDocument.getItemUuid());
804         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
805         if(oleDeliverRequestBos != null && oleDeliverRequestBos.size() > 0){
806          renewalExceeds =true;
807         }   else{
808         HashMap<String, Object> termValues = new HashMap<String, Object>();
809         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
810         String patronId = oleLoanDocument.getPatronId()!=null ?  oleLoanDocument.getPatronId() : "";
811         String itemId = oleLoanDocument.getItemId()!=null ?  oleLoanDocument.getItemId() : "";
812         dataCarrierService.removeData(patronId+itemId);
813         termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
814         termValues.put(OLEConstants.ITEM_TYPE, itemType);
815         termValues.put(OLEConstants.ITEM_STATUS, oleLoanDocument.getItemLoanStatus());
816         termValues.put(OLEConstants.ITEM_SHELVING, oleLoanDocument.getItemLocation());
817         termValues.put(OLEConstants.ITEM_COLLECTION, oleLoanDocument.getItemCollection());
818         termValues.put(OLEConstants.ITEM_LIBRARY, oleLoanDocument.getItemLibrary());
819         termValues.put(OLEConstants.ITEM_CAMPUS, oleLoanDocument.getItemCampus());
820         termValues.put(OLEConstants.ITEM_INSTITUTION, oleLoanDocument.getItemInstitution());
821         Integer noOfRenewals = Integer.parseInt(oleLoanDocument.getNumberOfRenewals());
822         termValues.put(OLEConstants.NUM_RENEWALS, noOfRenewals);
823         termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
824         termValues.put(OLEConstants.ITEM_ID_POLICY, itemId);
825         try {
826             EngineResults engineResults = loanProcessor.getEngineResults(agendaName, termValues);
827             dataCarrierService.removeData(patronId+itemId);
828             List<ResultEvent> allResults = engineResults.getAllResults();
829             for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
830                 ResultEvent resultEvent = resultEventIterator.next();
831                 if (resultEvent.getType().equals(OLEConstants.RULE_EVALUATED) && resultEvent.getSource().equals(OLEConstants.RENEWAL_LIMIT)) {
832                     renewalExceeds = true;
833                 }
834             }
835             if (renewalExceeds) {
836                 renewalCount = 0;
837             } else {
838                 renewalCount = 1;
839             }
840         } catch (Exception e) {
841 
842         }
843 
844         }
845         return renewalCount;
846     }
847 
848     /**
849      * This method is used return the operator id from operator name
850      *
851      * @return operatorId
852      */
853     public String getOperatorId(String operatorName) {
854         Map<String, String> principalMap = new HashMap<String, String>();
855         String principalId = null;
856         principalMap.put("principalName", operatorName);
857         List<PrincipalBo> principalBos = (List<PrincipalBo>) businessObjectService.findMatching(PrincipalBo.class, principalMap);
858         if (principalBos != null && principalBos.size() > 0) {
859             principalId = principalBos.get(0).getPrincipalId();
860         }
861         return principalId;
862     }
863 
864     /**
865      * This method is used to check whether the barcode belongs to a patron or not
866      * @param patronBarcode
867      * @return
868      */
869     public boolean vaildPatron(String patronBarcode){
870         boolean valid= false;
871         Map<String,String> patronMap = new HashMap<String,String>();
872         patronMap.put("barcode",patronBarcode);
873         List<OlePatronDocument> olePatronDocuments =(List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class,patronMap);
874         if(olePatronDocuments!=null && olePatronDocuments.size()>0){
875             valid=true;
876         }
877         return valid;
878     }
879 
880     /**
881      * This method is used to generate the ole checked out item list form the loan document list
882      * @param oleLoanDocumentList
883      * @return
884      */
885     public List<OLECheckedOutItem> getPatronCheckedOutItemList(List<OleLoanDocument> oleLoanDocumentList,String patronType,boolean renewableNeeded){
886         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
887         String renewParameter = loanProcessor.getParameter(OLEConstants.RENEW_INFO_INDICATOR);
888         boolean renewInfoNeeded = true;
889         if(renewParameter!=null && !renewParameter.isEmpty()){
890             if(renewParameter.equalsIgnoreCase("N")){
891              renewInfoNeeded = false;
892             }
893         }
894         List<OLECheckedOutItem> oleCheckedOutItems = new ArrayList<OLECheckedOutItem>();
895         if(oleLoanDocumentList.size()>0){
896             OLECheckedOutItem oleCheckedOutItem = null;
897           for(OleLoanDocument oleLoanDocument : oleLoanDocumentList){
898              oleCheckedOutItem = new OLECheckedOutItem();
899               oleCheckedOutItem.setCopyNumber(oleLoanDocument.getItemCopyNumber());
900               oleCheckedOutItem.setVolumeNumber(oleLoanDocument.getEnumeration());
901               oleCheckedOutItem.setAcquiredFine("");
902               oleCheckedOutItem.setDateRecalled("");
903               oleCheckedOutItem.setTitle(oleLoanDocument.getTitle());
904               oleCheckedOutItem.setAuthor(oleLoanDocument.getAuthor());
905               oleCheckedOutItem.setCallNumber(oleLoanDocument.getItemCallNumber());
906               oleCheckedOutItem.setCatalogueId(oleLoanDocument.getBibUuid());
907               if (oleLoanDocument.getLoanDueDate() != null) {
908                   oleCheckedOutItem.setDueDate(oleLoanDocument.getLoanDueDate().toString());
909                   if ((fmt.format(oleLoanDocument.getLoanDueDate())).compareTo(fmt.format(new Date(System.currentTimeMillis()))) > 0) {
910                       oleCheckedOutItem.setOverDue(false);
911                   }
912                   else{
913                       oleCheckedOutItem.setOverDue(true);
914                   }
915               } else {
916                   oleCheckedOutItem.setDueDate((new java.sql.Timestamp(new Date(2025, 1, 1).getTime()).toString()));
917               }
918               if (oleLoanDocument.getRenewalLoanDueDate() != null){
919                   oleCheckedOutItem.setDateRenewed(oleLoanDocument.getRenewalLoanDueDate().toString());
920               }
921               else{
922                   oleCheckedOutItem.setDateRenewed("");
923               }
924               oleCheckedOutItem.setItemType(oleLoanDocument.getItemType());
925               if (null!= oleLoanDocument.getCreateDate()) {
926                   oleCheckedOutItem.setLoanDate(new Timestamp(oleLoanDocument.getCreateDate().getTime()).toString());
927               }
928               oleCheckedOutItem.setItemId(oleLoanDocument.getItemId());
929               if (oleLoanDocument.getNoOfOverdueNoticesSentForBorrower() != null) {
930                   oleCheckedOutItem.setNumberOfOverdueSent(oleLoanDocument.getNoOfOverdueNoticesSentForBorrower());
931               } else {
932                   oleCheckedOutItem.setNumberOfOverdueSent("1");
933               }
934               Map<String, String> locationMap = getCircDeskLocationResolver().getLocationMap(oleLoanDocument.getItemFullLocation());
935               oleLoanDocument.setItemInstitution(locationMap.get(OLEConstants.ITEM_INSTITUTION));
936               oleLoanDocument.setItemCampus(locationMap.get(OLEConstants.ITEM_CAMPUS));
937               oleLoanDocument.setItemCollection(locationMap.get(OLEConstants.ITEM_COLLECTION));
938               oleLoanDocument.setItemLibrary(locationMap.get(OLEConstants.ITEM_LIBRARY));
939               oleLoanDocument.setItemLocation(locationMap.get(OLEConstants.ITEM_SHELVING));
940               oleLoanDocument.setBorrowerTypeCode(patronType);
941               if(renewableNeeded && renewInfoNeeded){
942 
943               int renewalDaysFromPolicy = getRenewalDays(oleCheckedOutItem.getItemType(), oleLoanDocument, patronType, oleLoanDocument.getNumberOfRenewals());
944               oleCheckedOutItem.setNumberOfRenewals(String.valueOf(renewalDaysFromPolicy));
945               }
946             oleCheckedOutItems.add(oleCheckedOutItem);
947           }
948         }
949         return oleCheckedOutItems;
950     }
951 
952     public Map<String,OleCirculationDesk> getAvailableCirculationDesks(){
953         Map<String,OleCirculationDesk> circulationDeskMap = new HashMap<String,OleCirculationDesk>();
954         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)getBusinessObjectService().findAll(OleCirculationDesk.class);
955         if(oleCirculationDeskList!=null && oleCirculationDeskList.size()>0){
956             for(OleCirculationDesk oleCirculationDesk : oleCirculationDeskList){
957                 circulationDeskMap.put(oleCirculationDesk.getCirculationDeskId(),oleCirculationDesk);
958             }
959         }
960         return circulationDeskMap;
961     }
962 
963     public Map<String,OleDeliverRequestType> getAvailableRequestTypes(){
964         Map<String,OleDeliverRequestType> requestTypeMap = new HashMap<String,OleDeliverRequestType>();
965         List<OleDeliverRequestType> oleDeliverRequestTypeList = (List<OleDeliverRequestType>)getBusinessObjectService().findAll(OleDeliverRequestType.class);
966         if(oleDeliverRequestTypeList!=null && oleDeliverRequestTypeList.size()>0){
967             for(OleDeliverRequestType oleDeliverRequestType : oleDeliverRequestTypeList){
968                 requestTypeMap.put(oleDeliverRequestType.getRequestTypeId(),oleDeliverRequestType);
969             }
970         }
971         return requestTypeMap;
972     }
973 
974 
975     /**
976      * This method is used to set set the patron profile information for the given  patron barcode
977      * @param patronBarcode
978      * @param operator
979      * @param agencyId
980      * @return
981      */
982    @Override
983     public String lookupUserForNCIP(String patronBarcode, String operator, String agencyId) {
984        Long startTime =System.currentTimeMillis();
985         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator + "agencyId : " + agencyId );
986         OLELookupUser lookupUser = new OLELookupUser();
987         if (!loanProcessor.hasCirculationDesk(operator)) {
988             lookupUser.setCode("001");
989             lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
990         } else {
991             OleLoanDocumentDaoOjb oleLoanDocumentDaoOjb = (OleLoanDocumentDaoOjb)SpringContext.getBean("oleLoanDao");
992             OleDeliverDaoJdbc oleDeliverDaoJdbc = (OleDeliverDaoJdbc)SpringContext.getBean("oleDeliverDaoJdbc");
993            OlePatronDocument olePatronDocument =  oleDeliverDaoJdbc.getPatronDocument(patronBarcode);
994 
995             if (olePatronDocument!=null) {
996                 Long startTimeInit = System.currentTimeMillis();
997                 lookupUser = oleCirculationHelperService.initialiseLookupUser(olePatronDocument, agencyId);
998                 Long endTimeInit = System.currentTimeMillis();
999                 Long timeDiffInit = endTimeInit-startTimeInit;
1000                 LOG.info("Time taken to set the init details : " +timeDiffInit);
1001                 try {
1002                     Long startTimeLoan = System.currentTimeMillis();
1003                     //   List<OleLoanDocument> oleLoanDocumentList = loanProcessor.getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId());
1004                   List<OleLoanDocument> oleLoanDocumentList = oleLoanDocumentDaoOjb.getDeliverLoans(olePatronDocument.getOlePatronId());
1005                   if(oleLoanDocumentList!=null){
1006                    List<OLECheckedOutItem> oleCheckedOutItemList =  oleDeliverDaoJdbc.getCheckedOutItemsList(oleLoanDocumentList);
1007                     //List<OLECheckedOutItem> checkedOutItemList = getPatronCheckedOutItemList(olePatronDocument.getOleLoanDocuments(),olePatronDocument.getOleBorrowerType().getBorrowerTypeCode());
1008                     if(oleCheckedOutItemList!=null){
1009                     OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
1010                     if (oleCheckedOutItemList != null && oleCheckedOutItemList.size() > 0) {
1011                         oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
1012                     }
1013                     lookupUser.setOleCheckedOutItems(oleCheckedOutItems);
1014                     }
1015                     }
1016                     Long endTimeLoan = System.currentTimeMillis();
1017                     Long timeDiffLoan = endTimeLoan-startTimeLoan;
1018                     LOG.info("Time taken to set the loan details : " +timeDiffLoan);
1019                 } catch (Exception e) {
1020                     LOG.info("Exception Occurred While Retrieving the checked out items");
1021                     LOG.error(e);
1022                 }
1023                 try {
1024                     Long startTimeHold = System.currentTimeMillis();
1025                  /*   List<OLEHold> oleHoldList = getHoldRecordsList(olePatronDocument.getOleDeliverRequestBos());*/
1026 
1027                     List<OleDeliverRequestBo> oleDeliverRequestBoList = oleLoanDocumentDaoOjb.getDeliverRequests(patronBarcode);
1028 
1029                     if(oleDeliverRequestBoList!=null){
1030                     List<OLEHold> oleHoldList = oleDeliverDaoJdbc.getHoldRecordsList(oleDeliverRequestBoList);
1031                     if(oleHoldList!=null){
1032                     OLEHolds oleHolds = new OLEHolds();
1033                     if (oleHoldList != null && oleHoldList.size() > 0) {
1034                         oleHolds.setOleHoldList(oleHoldList);
1035                     }
1036                     lookupUser.setOleHolds(oleHolds);
1037                     }
1038                     }
1039                     Long endTimeHold = System.currentTimeMillis();
1040                     Long timeDiffHold = endTimeHold-startTimeHold;
1041                     LOG.info("Time taken to set the hold details : " +timeDiffHold);
1042                 } catch (Exception e) {
1043                     LOG.info("Exception Occurred While Retrieving the Hold items");
1044                     LOG.error(e);
1045                 }
1046                 try {
1047                     Long startTimeFine = System.currentTimeMillis();
1048                     List<PatronBillPayment> patronBillPaymentList = oleLoanDocumentDaoOjb.getPatronBillPayments(olePatronDocument.getOlePatronId());
1049                     if(patronBillPaymentList!=null){
1050                      List<OLEItemFine> oleItemFineList = oleDeliverDaoJdbc.getFineItemLists(patronBillPaymentList);
1051                         if(oleItemFineList!=null){
1052                      OLEItemFines oleItemFines = new OLEItemFines();
1053                      oleItemFines.setOleItemFineList(oleItemFineList);
1054                     lookupUser.setOleItemFines(oleItemFines);
1055                         }
1056                     }
1057                     Long endTimeFine = System.currentTimeMillis();
1058                     Long timeDiffFine = endTimeFine-startTimeFine;
1059                     LOG.info("Time taken to set the fine details : " +timeDiffFine);
1060                 } catch (Exception e) {
1061                     LOG.info("Exception Occurred While Retrieving Fine");
1062                     LOG.error(e);
1063                 }
1064                 lookupUser.setCode("000");
1065                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
1066             } else {
1067                 lookupUser.setCode("002");
1068                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
1069             }
1070         }
1071         String responseMessage = oleLookupUserConverter.generateLookupUserResponseXml(lookupUser);
1072        Long endTime = System.currentTimeMillis();
1073        Long timediff = endTime-startTime;
1074        LOG.info("Time taken to complete the lookup user service " + timediff);
1075        return responseMessage;
1076     }
1077 
1078 
1079 }
1080 
1081 
1082