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