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.bo.*;
8   import org.kuali.ole.deliver.processor.LoanProcessor;
9   import org.kuali.ole.deliver.service.CircDeskLocationResolver;
10  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
11  import org.kuali.ole.deliver.service.OleLoanDocumentDaoOjb;
12  import org.kuali.ole.deliver.service.impl.OleDeliverDaoJdbc;
13  import org.kuali.ole.describe.bo.InstanceEditorFormDataHandler;
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.Holdings;
17  import org.kuali.ole.docstore.common.document.ItemOleml;
18  import org.kuali.ole.docstore.common.document.content.enums.DocType;
19  import org.kuali.ole.docstore.common.document.content.instance.Item;
20  import org.kuali.ole.docstore.common.document.content.instance.Location;
21  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
22  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
23  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
24  import org.kuali.ole.docstore.common.search.SearchResponse;
25  import org.kuali.ole.docstore.common.search.SearchResult;
26  import org.kuali.ole.docstore.common.search.SearchResultField;
27  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
28  import org.kuali.ole.ncip.bo.*;
29  import org.kuali.ole.ncip.converter.*;
30  import org.kuali.ole.ncip.service.OLECirculationService;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.ole.util.DocstoreUtil;
33  import org.kuali.rice.core.api.config.property.ConfigContext;
34  import org.kuali.rice.core.api.config.property.ConfigurationService;
35  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
36  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
37  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
38  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
39  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
40  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
41  import org.kuali.rice.krad.service.BusinessObjectService;
42  import org.kuali.rice.krad.service.KRADServiceLocator;
43  import org.kuali.rice.krad.util.GlobalVariables;
44  import org.kuali.rice.krms.api.engine.EngineResults;
45  import org.kuali.rice.krms.api.engine.ResultEvent;
46  
47  import java.sql.Timestamp;
48  import java.text.SimpleDateFormat;
49  import java.util.*;
50  
51  /**
52   * Created with IntelliJ IDEA.
53   * User: maheswarang
54   * Date: 7/21/13
55   * Time: 2:13 PM
56   * To change this template use File | Settings | File Templates.
57   */
58  public class OLECirculationServiceImpl implements OLECirculationService {
59      private static final Logger LOG = Logger.getLogger(OLECirculationServiceImpl.class);
60      private BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
61      private OLECirculationHelperServiceImpl oleCirculationHelperService = new OLECirculationHelperServiceImpl();
62      private OLELookupUserConverter oleLookupUserConverter = new OLELookupUserConverter();
63      private OLECheckInItemConverter oleCheckInItemConverter = new OLECheckInItemConverter();
64      private OLECheckOutItemConverter oleCheckOutItemConverter = new OLECheckOutItemConverter();
65      private OLEHoldsConverter oleHoldsConverter = new OLEHoldsConverter();
66      private OLEItemFineConverter oleItemFineConverter = new OLEItemFineConverter();
67      private OLECheckoutItemsConverter oleCheckoutItemsConverter = new OLECheckoutItemsConverter();
68      private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
69      private LoanProcessor loanProcessor = new LoanProcessor();
70      private DocstoreUtil docstoreUtil;
71      private ConfigurationService kualiConfigurationService;
72      private Map<String,OleCirculationDesk> oleCirculationDeskMap = getAvailableCirculationDesks();
73      private Map<String,OleDeliverRequestType> oleDeliverRequestTypeMap = getAvailableRequestTypes();
74      private CircDeskLocationResolver circDeskLocationResolver;
75  
76      private CircDeskLocationResolver getCircDeskLocationResolver() {
77          if (circDeskLocationResolver == null) {
78              circDeskLocationResolver = new CircDeskLocationResolver();
79          }
80          return circDeskLocationResolver;
81      }
82  
83      public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
84          this.circDeskLocationResolver = circDeskLocationResolver;
85      }
86  
87      public ConfigurationService getKualiConfigurationService() {
88          if (kualiConfigurationService == null) {
89              kualiConfigurationService = (ConfigurationService) SpringContext.getBean("kualiConfigurationService");
90          }
91          return kualiConfigurationService;
92      }
93  
94      public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) {
95          this.kualiConfigurationService = kualiConfigurationService;
96      }
97  
98      public DocstoreUtil getDocstoreUtil() {
99  
100         if (docstoreUtil == null) {
101             docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
102 
103         }
104         return docstoreUtil;
105     }
106 
107     private DocstoreClientLocator docstoreClientLocator;
108 
109     public DocstoreClientLocator getDocstoreClientLocator() {
110 
111         if (docstoreClientLocator == null) {
112             docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
113 
114         }
115         return docstoreClientLocator;
116     }
117 
118     public BusinessObjectService getBusinessObjectService() {
119         return businessObjectService;
120     }
121 
122     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123         this.businessObjectService = businessObjectService;
124     }
125 
126     public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
127         return oleCirculationHelperService;
128     }
129 
130     public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
131         this.oleCirculationHelperService = oleCirculationHelperService;
132     }
133 
134     public OLELookupUserConverter getOleLookupUserConverter() {
135         return oleLookupUserConverter;
136     }
137 
138     public void setOleLookupUserConverter(OLELookupUserConverter oleLookupUserConverter) {
139         this.oleLookupUserConverter = oleLookupUserConverter;
140     }
141 
142     public OLECheckInItemConverter getOleCheckInItemConverter() {
143         return oleCheckInItemConverter;
144     }
145 
146     public void setOleCheckInItemConverter(OLECheckInItemConverter oleCheckInItemConverter) {
147         this.oleCheckInItemConverter = oleCheckInItemConverter;
148     }
149 
150     public OLECheckOutItemConverter getOleCheckOutItemConverter() {
151         return oleCheckOutItemConverter;
152     }
153 
154     public void setOleCheckOutItemConverter(OLECheckOutItemConverter oleCheckOutItemConverter) {
155         this.oleCheckOutItemConverter = oleCheckOutItemConverter;
156     }
157 
158     @Override
159     public String lookupUser(String patronBarcode, String operator, String agencyId, boolean isSIP2Request) {
160         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator + "agencyId : " + agencyId );
161         OLELookupUser lookupUser = new OLELookupUser();
162         if (!loanProcessor.hasCirculationDesk(operator)) {
163             lookupUser.setCode("001");
164             lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
165         } else {
166             Map<String, String> patronMap = new HashMap<String, String>();
167             patronMap.put(OLEConstants.BARCODE, patronBarcode);
168             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
169             if (olePatronDocumentList.size() > 0) {
170                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
171                 lookupUser = oleCirculationHelperService.initialiseLookupUser(olePatronDocument, agencyId);
172                 if (olePatronDocument.isGeneralBlock() || oleCirculationHelperService.isPatronExpired(olePatronDocument) || !olePatronDocument.isActiveIndicator() || oleCirculationHelperService.isPatronActivated(olePatronDocument)){
173                     lookupUser.setValidPatron(false);
174                 }else{
175                     lookupUser.setValidPatron(true);
176                 }
177                 try {
178                     List<OleLoanDocument> oleLoanDocumentList = loanProcessor.getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId());
179                     List<OLECheckedOutItem> oleCheckedOutItemList = getPatronCheckedOutItemList(oleLoanDocumentList,olePatronDocument.getOleBorrowerType().getBorrowerTypeCode(),agencyId!=null?false:true);
180                     //List<OLECheckedOutItem> checkedOutItemList = getPatronCheckedOutItemList(olePatronDocument.getOleLoanDocuments(),olePatronDocument.getOleBorrowerType().getBorrowerTypeCode());
181                     OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
182                     if (oleCheckedOutItemList != null && oleCheckedOutItemList.size() > 0) {
183                         oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
184                     }
185                     lookupUser.setOleCheckedOutItems(oleCheckedOutItems);
186                 } catch (Exception e) {
187                     LOG.info("Exception Occurred While Retrieving the checked out items");
188                     LOG.error(e);
189                 }
190                 try {
191                     List<OLEHold> oleHoldList = getHoldsList(olePatronDocument.getOleDeliverRequestBos());
192                     OLEHolds oleHolds = new OLEHolds();
193                     if (oleHoldList != null && oleHoldList.size() > 0) {
194                         oleHolds.setOleHoldList(oleHoldList);
195                     }
196                     lookupUser.setOleHolds(oleHolds);
197                 } catch (Exception e) {
198                     LOG.info("Exception Occurred While Retrieving the Hold items");
199                     LOG.error(e);
200                 }
201                 try {
202                     OLEItemFines oleItemFines = (OLEItemFines) oleItemFineConverter.generateCheckoutItemObject(getFine(patronBarcode, operator));
203                     lookupUser.setOleItemFines(oleItemFines);
204                 } catch (Exception e) {
205                     LOG.info("Exception Occurred While Retrieving Fine");
206                     LOG.error(e);
207                 }
208                 lookupUser.setCode("000");
209                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
210             } else {
211                 lookupUser.setCode("002");
212                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
213             }
214         }
215         String responseMessage = "";
216         if(isSIP2Request){
217             responseMessage = oleLookupUserConverter.generateLookupUserResponseXmlForSip2(lookupUser);
218         }else{
219             responseMessage = oleLookupUserConverter.generateLookupUserResponseXml(lookupUser);
220         }
221         return responseMessage;
222     }
223 
224     @Override
225     public String getCheckedOutItems(String patronBarcode, String operator) throws Exception {
226         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator  );
227         LOG.info("Start CHECK out " + System.currentTimeMillis());
228         OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
229         String patronType = "";
230         String checkoutItemString = "";
231         if (!loanProcessor.hasCirculationDesk(operator)) {
232             oleCheckedOutItems.setCode("001");
233             oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
234             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
235         } else {
236 
237             Map<String, String> patronMap = new HashMap<String, String>();
238             patronMap.put(OLEConstants.BARCODE, patronBarcode);
239             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
240             if (olePatronDocumentList.size() > 0) {
241                 Map<String, String> patronDocMap = new HashMap<String, String>();
242                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
243                 patronType = olePatronDocument.getBorrowerType();
244                 Map<String, String> borrowerTypeMap = new HashMap<String, String>();
245                 borrowerTypeMap.put(OLEConstants.BORROWER_TYPE_ID, patronType);
246                 List<OleBorrowerType> oleBorrowerTypeList = (List<OleBorrowerType>) businessObjectService.findMatching(OleBorrowerType.class, borrowerTypeMap);
247                 if (oleBorrowerTypeList.size() > 0) {
248                     patronType = oleBorrowerTypeList.get(0).getBorrowerTypeCode();
249                 }
250                 patronDocMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
251                 List<OleLoanDocument> oleLoanDocumentList = loanProcessor.getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId());
252                 if (oleLoanDocumentList != null && oleLoanDocumentList.size() > 0) {
253                     List<OLECheckedOutItem> oleCheckedOutItemList = getPatronCheckedOutItemList(oleLoanDocumentList,patronType,true);
254                     oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
255                     oleCheckedOutItems.setCode("000");
256                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
257                     oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
258                 } else {
259                     oleCheckedOutItems.setCode("004");
260                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_LOAN));
261                     oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_LOAN));
262                 }
263             } else {
264                 oleCheckedOutItems.setCode("002");
265                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
266                 oleCheckedOutItems.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
267 
268             }
269         }
270         checkoutItemString = oleCheckoutItemsConverter.generateCheckOutItemXml(oleCheckedOutItems);
271         LOG.info("END CHECK out " + System.currentTimeMillis());
272         return checkoutItemString;  //To change body of implemented methods use File | Settings | File Templates.
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(patronBarcode, operator, itemBarcode, callNumber, title, author, itemType, itemLocation, dateExpires, requestType, pickUpLocation);
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.setAmount((feeType.getFeeAmount() != null ? feeType.getFeeAmount().bigDecimalValue() : OLEConstants.BIGDECIMAL_DEF_VALUE));
621                 oleItemFine.setBalance((feeType.getBalFeeAmount() != null ? feeType.getBalFeeAmount().bigDecimalValue() : OLEConstants.BIGDECIMAL_DEF_VALUE));
622                 oleItemFine.setBillDate(feeType.getBillDate().toString());
623                 int noOfPayment = feeType.getItemLevelBillPaymentList().size();
624                 oleItemFine.setNoOfPayments(new Integer(noOfPayment).toString());
625                 if (feeType.getOleFeeType() != null) {
626                     oleItemFine.setReason(feeType.getOleFeeType().getFeeTypeName());
627                     oleItemFine.setFeeType(feeType.getOleFeeType().getFeeTypeCode());
628                 } else {
629                     oleItemFine.setReason(feeType.getFeeType());
630                     oleItemFine.setFeeType(feeType.getFeeType());
631                 }
632                 oleItemFine.setDateCharged(feeType.getBillDate().toString());
633                 oleItemFineList.add(oleItemFine);
634             }
635         }
636 
637         return oleItemFineList;
638     }
639 
640     @Override
641     public String getHolds(String patronBarcode, String operator) throws Exception {
642         LOG.info("Inside Get Holds . Patron Barcode : " +patronBarcode + "Operator : "+ operator );
643         OLEHoldsConverter oleHoldConverter = new OLEHoldsConverter();
644         OLEHolds oleHolds = new OLEHolds();
645         String itemFineString = "";
646         if (!loanProcessor.hasCirculationDesk(operator)) {
647             oleHolds.setCode("001");
648             oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
649         } else {
650             Map<String, String> patronMap = new HashMap<String, String>();
651             patronMap.put(OLEConstants.BARCODE, patronBarcode);
652             List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
653             if (olePatronDocumentList.size() > 0) {
654                 Map<String, String> patronDocMap = new HashMap<String, String>();
655                 OlePatronDocument olePatronDocument = olePatronDocumentList.get(0);
656                 patronDocMap.put(OLEConstants.OleDeliverRequest.BORROWER_ID, olePatronDocument.getOlePatronId());
657                 List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) businessObjectService.findMatching(OleDeliverRequestBo.class, patronDocMap);
658                 if (oleDeliverRequestBoList.size() > 0) {
659                     List<OLEHold> oleHoldsList = getHoldsList(oleDeliverRequestBoList);
660                     oleHolds.setOleHoldList(oleHoldsList);
661                     oleHolds.setCode("000");
662                     oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
663                 } else {
664                     oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_HOLD));
665                     oleHolds.setCode("006");
666                 }
667             } else {
668                 oleHolds.setCode("002");
669                 oleHolds.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
670             }
671         }
672         return oleHoldConverter.generateHoldsmXml(oleHolds);
673     }
674 
675     public List<OLEHold> getHoldsList(List<OleDeliverRequestBo> oleDeliverRequestBoList) throws Exception {
676         //String availableDate = "";
677         //String callNumber="";
678         String copyNumber="";
679 
680         String[] availableDates;
681         List<OLEHold> oleHoldList = new ArrayList<OLEHold>();
682         for (OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList) {
683             OLEHold oleHold = new OLEHold();
684             oleHold.setItemId(oleDeliverRequestBo.getItemId());
685             Map<String, Object> detailMap = oleDeliverRequestDocumentHelperService.retrieveBIbItemHoldingData(oleDeliverRequestBo.getItemUuid());
686             Bib bib = (Bib) detailMap.get(OLEConstants.BIB);
687             Item item = (Item) detailMap.get(OLEConstants.ITEM);
688             OleHoldings oleHoldings  = (OleHoldings) detailMap.get(OLEConstants.HOLDING);
689             org.kuali.ole.docstore.common.document.Item item1 = (org.kuali.ole.docstore.common.document.Item) detailMap.get("documentItem");
690             oleHold.setCatalogueId(bib.getId());
691             oleHold.setRequestId(oleDeliverRequestBo.getRequestId());
692             if (item.getItemStatus() != null) {
693                 oleHold.setAvailableStatus(item.getItemStatus().getCodeValue());
694             }
695 
696             /*if(item.getCallNumber()!=null && !StringUtils.isEmpty(item.getCallNumber().getNumber())){
697                 callNumber = loanProcessor.getItemCallNumber(item.getCallNumber());
698             }else {
699                 callNumber = loanProcessor.getItemCallNumber(oleHoldings.getCallNumber());
700             }*/
701             if(item.getCopyNumber()!=null && !item.getCopyNumber().isEmpty()){
702                 copyNumber=item.getCopyNumber();
703             }
704             else{
705                 copyNumber=oleHoldings.getCopyNumber()!=null? oleHoldings.getCopyNumber():"";
706             }
707             oleHold.setTitle(bib.getTitle());
708             oleHold.setAuthor(bib.getAuthor());
709             oleHold.setVolumeNumber(item.getEnumeration()!=null ? item.getEnumeration():"");
710             oleHold.setCallNumber(loanProcessor.getItemCallNumber(item.getCallNumber(),oleHoldings.getCallNumber()));
711             oleHold.setCopyNumber(copyNumber);
712             if (item.getItemType() != null)
713                 oleHold.setItemType(item.getItemType().getCodeValue());
714             if (oleDeliverRequestBo.getRequestTypeId() != null && !oleDeliverRequestBo.getRequestTypeId().isEmpty()) {
715                 if (oleDeliverRequestBo.getRequestTypeId().equals("1") || oleDeliverRequestBo.getRequestTypeId().equals("2")) {
716                     oleHold.setRecallStatus(OLEConstants.YES);
717                 } else {
718                     oleHold.setRecallStatus(OLEConstants.NO);
719                 }
720             }
721             if (oleDeliverRequestBo.getRequestExpiryDate() != null) {
722                 oleHold.setExpiryDate(oleDeliverRequestBo.getRequestExpiryDate().toString());
723             }
724             if (oleDeliverRequestBo.getCreateDate() != null) {
725                 oleHold.setCreateDate(oleDeliverRequestBo.getCreateDate().toString());
726             }
727             if (oleDeliverRequestBo.getBorrowerQueuePosition() != null) {
728                 oleHold.setPriority(oleDeliverRequestBo.getBorrowerQueuePosition().toString());
729             }
730             oleHold.setPickupLocation(oleDeliverRequestBo.getPickUpLocationId());
731             if (oleDeliverRequestBo.getRecallDueDate() != null) {
732                 oleHold.setDateRecalled(oleDeliverRequestBo.getRecallDueDate().toString());
733             }
734             if (oleDeliverRequestBo.getRecallDueDate() != null) {
735                 oleHold.setDateRecalled(oleDeliverRequestBo.getRecallDueDate().toString());
736             }
737             if (oleDeliverRequestTypeMap!=null && oleDeliverRequestTypeMap.size() > 0) {
738                 if(oleDeliverRequestTypeMap.get(oleDeliverRequestBo.getRequestTypeId())!=null){
739                     oleHold.setRequestType(oleDeliverRequestTypeMap.get(oleDeliverRequestBo.getRequestTypeId()).getRequestTypeCode());
740                 }
741             }
742             Map<String, String> loanMap = new HashMap<String, String>();
743             loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, oleDeliverRequestBo.getItemId());
744             List<OleLoanDocument> oleLoanDocumentList = (List<OleLoanDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class, loanMap);
745             if (oleLoanDocumentList.size() > 0) {
746                 if (oleLoanDocumentList.get(0).getLoanDueDate() != null) {
747                     availableDates = oleLoanDocumentList.get(0).getLoanDueDate().toString().split(" ");
748                     if (availableDates != null && availableDates.length > 0) {
749                         oleHold.setAvailableDate(availableDates[0]);
750                     } else {
751                         oleHold.setAvailableDate(oleLoanDocumentList.get(0).getLoanDueDate().toString());
752                     }
753                     if (oleDeliverRequestBo.getPickUpLocationId() != null) {
754                         if (oleCirculationDeskMap.size()>0 && oleCirculationDeskMap.get(oleDeliverRequestBo.getPickUpLocationId())!=null) {
755                             oleHold.setDateAvailableExpires(addDate(new java.sql.Date(oleLoanDocumentList.get(0).getLoanDueDate().getTime()), Integer.parseInt(oleCirculationDeskMap.get(oleDeliverRequestBo.getPickUpLocationId()).getOnHoldDays())).toString());
756                         }
757                     }
758                 } else {
759                     oleHold.setAvailableDate(OLEConstants.INDEFINITE);
760                     oleHold.setDateAvailableExpires(OLEConstants.INDEFINITE);
761                 }
762             }
763             if (oleDeliverRequestBo.getRequestTypeId().equals("2") || oleDeliverRequestBo.getRequestTypeId().equals("4") || oleDeliverRequestBo.getRequestTypeId().equals("6")) {
764                 oleHold.setReserve(true);
765             } else {
766                 oleHold.setReserve(false);
767             }
768 
769             oleHoldList.add(oleHold);
770         }
771 
772         return oleHoldList;
773     }
774 
775     private java.sql.Date addDate(java.sql.Date in, int daysToAdd) {
776         if (in == null) {
777             return null;
778         }
779         GregorianCalendar cal = new GregorianCalendar();
780         cal.setTime(in);
781         cal.add(Calendar.DAY_OF_MONTH, daysToAdd);
782         return new java.sql.Date(cal.getTime().getTime());
783     }
784 
785     private int getRenewalDays(String itemType, OleLoanDocument oleLoanDocument, String borrowerType, String numberOfRenewals) {
786         String agendaName = OLEConstants.RENEWAL_AGENDA_NM;
787         int renewalCount = 1;
788         boolean renewalExceeds = false;
789         Map<String,String> requestMap = new HashMap<String,String>();
790         requestMap.put("itemUuid",oleLoanDocument.getItemUuid());
791         List<OleDeliverRequestBo> oleDeliverRequestBos = (List<OleDeliverRequestBo>)businessObjectService.findMatching(OleDeliverRequestBo.class,requestMap);
792         if(oleDeliverRequestBos != null && oleDeliverRequestBos.size() > 0){
793          renewalExceeds =true;
794         }   else{
795         HashMap<String, Object> termValues = new HashMap<String, Object>();
796         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
797         String patronId = oleLoanDocument.getPatronId()!=null ?  oleLoanDocument.getPatronId() : "";
798         String itemId = oleLoanDocument.getItemId()!=null ?  oleLoanDocument.getItemId() : "";
799         dataCarrierService.removeData(patronId+itemId);
800         termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
801         termValues.put(OLEConstants.ITEM_TYPE, itemType);
802         termValues.put(OLEConstants.ITEM_STATUS, oleLoanDocument.getItemLoanStatus());
803         termValues.put(OLEConstants.ITEM_SHELVING, oleLoanDocument.getItemLocation());
804         termValues.put(OLEConstants.ITEM_COLLECTION, oleLoanDocument.getItemCollection());
805         termValues.put(OLEConstants.ITEM_LIBRARY, oleLoanDocument.getItemLibrary());
806         termValues.put(OLEConstants.ITEM_CAMPUS, oleLoanDocument.getItemCampus());
807         termValues.put(OLEConstants.ITEM_INSTITUTION, oleLoanDocument.getItemInstitution());
808         Integer noOfRenewals = Integer.parseInt(oleLoanDocument.getNumberOfRenewals());
809         termValues.put(OLEConstants.NUM_RENEWALS, noOfRenewals);
810         termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
811         termValues.put(OLEConstants.ITEM_ID_POLICY, itemId);
812         try {
813             EngineResults engineResults = loanProcessor.getEngineResults(agendaName, termValues);
814             dataCarrierService.removeData(patronId+itemId);
815             List<ResultEvent> allResults = engineResults.getAllResults();
816             for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
817                 ResultEvent resultEvent = resultEventIterator.next();
818                 if (resultEvent.getType().equals(OLEConstants.RULE_EVALUATED) && resultEvent.getSource().equals(OLEConstants.RENEWAL_LIMIT)) {
819                     renewalExceeds = true;
820                 }
821             }
822             if (renewalExceeds) {
823                 renewalCount = 0;
824             } else {
825                 renewalCount = 1;
826             }
827         } catch (Exception e) {
828 
829         }
830 
831         }
832         return renewalCount;
833     }
834 
835     /**
836      * This method is used return the operator id from operator name
837      *
838      * @return operatorId
839      */
840     public String getOperatorId(String operatorName) {
841         Map<String, String> principalMap = new HashMap<String, String>();
842         String principalId = null;
843         principalMap.put("principalName", operatorName);
844         List<PrincipalBo> principalBos = (List<PrincipalBo>) businessObjectService.findMatching(PrincipalBo.class, principalMap);
845         if (principalBos != null && principalBos.size() > 0) {
846             principalId = principalBos.get(0).getPrincipalId();
847         }
848         return principalId;
849     }
850 
851     /**
852      * This method is used to check whether the barcode belongs to a patron or not
853      * @param patronBarcode
854      * @return
855      */
856     public boolean vaildPatron(String patronBarcode){
857         boolean valid= false;
858         Map<String,String> patronMap = new HashMap<String,String>();
859         patronMap.put("barcode",patronBarcode);
860         List<OlePatronDocument> olePatronDocuments =(List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class,patronMap);
861         if(olePatronDocuments!=null && olePatronDocuments.size()>0){
862             valid=true;
863         }
864         return valid;
865     }
866 
867     /**
868      * This method is used to generate the ole checked out item list form the loan document list
869      * @param oleLoanDocumentList
870      * @return
871      */
872     public List<OLECheckedOutItem> getPatronCheckedOutItemList(List<OleLoanDocument> oleLoanDocumentList,String patronType,boolean renewableNeeded){
873         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
874         String renewParameter = loanProcessor.getParameter(OLEConstants.RENEW_INFO_INDICATOR);
875         boolean renewInfoNeeded = true;
876         if(renewParameter!=null && !renewParameter.isEmpty()){
877             if(renewParameter.equalsIgnoreCase("N")){
878              renewInfoNeeded = false;
879             }
880         }
881         List<OLECheckedOutItem> oleCheckedOutItems = new ArrayList<OLECheckedOutItem>();
882         if(oleLoanDocumentList.size()>0){
883             OLECheckedOutItem oleCheckedOutItem = null;
884           for(OleLoanDocument oleLoanDocument : oleLoanDocumentList){
885              oleCheckedOutItem = new OLECheckedOutItem();
886               oleCheckedOutItem.setCopyNumber(oleLoanDocument.getItemCopyNumber());
887               oleCheckedOutItem.setVolumeNumber(oleLoanDocument.getEnumeration());
888               oleCheckedOutItem.setAcquiredFine("");
889               oleCheckedOutItem.setDateRecalled("");
890               oleCheckedOutItem.setTitle(oleLoanDocument.getTitle());
891               oleCheckedOutItem.setAuthor(oleLoanDocument.getAuthor());
892               oleCheckedOutItem.setCallNumber(oleLoanDocument.getItemCallNumber());
893               oleCheckedOutItem.setCatalogueId(oleLoanDocument.getBibUuid());
894               if (oleLoanDocument.getLoanDueDate() != null) {
895                   oleCheckedOutItem.setDueDate(oleLoanDocument.getLoanDueDate().toString());
896                   if ((fmt.format(oleLoanDocument.getLoanDueDate())).compareTo(fmt.format(new Date(System.currentTimeMillis()))) > 0) {
897                       oleCheckedOutItem.setOverDue(false);
898                   }
899                   else{
900                       oleCheckedOutItem.setOverDue(true);
901                   }
902               } else {
903                   oleCheckedOutItem.setDueDate((new java.sql.Timestamp(new Date(2025, 1, 1).getTime()).toString()));
904               }
905               if (oleLoanDocument.getRenewalLoanDueDate() != null){
906                   oleCheckedOutItem.setDateRenewed(oleLoanDocument.getRenewalLoanDueDate().toString());
907               }
908               else{
909                   oleCheckedOutItem.setDateRenewed("");
910               }
911               oleCheckedOutItem.setItemType(oleLoanDocument.getItemType());
912               if (null!= oleLoanDocument.getCreateDate()) {
913                   oleCheckedOutItem.setLoanDate(new Timestamp(oleLoanDocument.getCreateDate().getTime()).toString());
914               }
915               oleCheckedOutItem.setItemId(oleLoanDocument.getItemId());
916               if (oleLoanDocument.getNoOfOverdueNoticesSentForBorrower() != null) {
917                   oleCheckedOutItem.setNumberOfOverdueSent(oleLoanDocument.getNoOfOverdueNoticesSentForBorrower());
918               } else {
919                   oleCheckedOutItem.setNumberOfOverdueSent("1");
920               }
921               Map<String, String> locationMap = getCircDeskLocationResolver().getLocationMap(oleLoanDocument.getItemFullLocation());
922               oleLoanDocument.setItemInstitution(locationMap.get(OLEConstants.ITEM_INSTITUTION));
923               oleLoanDocument.setItemCampus(locationMap.get(OLEConstants.ITEM_CAMPUS));
924               oleLoanDocument.setItemCollection(locationMap.get(OLEConstants.ITEM_COLLECTION));
925               oleLoanDocument.setItemLibrary(locationMap.get(OLEConstants.ITEM_LIBRARY));
926               oleLoanDocument.setItemLocation(locationMap.get(OLEConstants.ITEM_SHELVING));
927               oleLoanDocument.setBorrowerTypeCode(patronType);
928               if(renewableNeeded && renewInfoNeeded){
929 
930               int renewalDaysFromPolicy = getRenewalDays(oleCheckedOutItem.getItemType(), oleLoanDocument, patronType, oleLoanDocument.getNumberOfRenewals());
931               oleCheckedOutItem.setNumberOfRenewals(String.valueOf(renewalDaysFromPolicy));
932               }
933             oleCheckedOutItems.add(oleCheckedOutItem);
934           }
935         }
936         return oleCheckedOutItems;
937     }
938 
939     public Map<String,OleCirculationDesk> getAvailableCirculationDesks(){
940         Map<String,OleCirculationDesk> circulationDeskMap = new HashMap<String,OleCirculationDesk>();
941         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)getBusinessObjectService().findAll(OleCirculationDesk.class);
942         if(oleCirculationDeskList!=null && oleCirculationDeskList.size()>0){
943             for(OleCirculationDesk oleCirculationDesk : oleCirculationDeskList){
944                 circulationDeskMap.put(oleCirculationDesk.getCirculationDeskId(),oleCirculationDesk);
945             }
946         }
947         return circulationDeskMap;
948     }
949 
950     public Map<String,OleDeliverRequestType> getAvailableRequestTypes(){
951         Map<String,OleDeliverRequestType> requestTypeMap = new HashMap<String,OleDeliverRequestType>();
952         List<OleDeliverRequestType> oleDeliverRequestTypeList = (List<OleDeliverRequestType>)getBusinessObjectService().findAll(OleDeliverRequestType.class);
953         if(oleDeliverRequestTypeList!=null && oleDeliverRequestTypeList.size()>0){
954             for(OleDeliverRequestType oleDeliverRequestType : oleDeliverRequestTypeList){
955                 requestTypeMap.put(oleDeliverRequestType.getRequestTypeId(),oleDeliverRequestType);
956             }
957         }
958         return requestTypeMap;
959     }
960 
961 
962     /**
963      * This method is used to set set the patron profile information for the given  patron barcode
964      * @param patronBarcode
965      * @param operator
966      * @param agencyId
967      * @return
968      */
969    @Override
970     public String lookupUserForNCIP(String patronBarcode, String operator, String agencyId) {
971        Long startTime =System.currentTimeMillis();
972         LOG.info("Inside the look up user : patron Barcode : " + patronBarcode + "operator : "+ operator + "agencyId : " + agencyId );
973         OLELookupUser lookupUser = new OLELookupUser();
974         if (!loanProcessor.hasCirculationDesk(operator)) {
975             lookupUser.setCode("001");
976             lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
977         } else {
978             OleLoanDocumentDaoOjb oleLoanDocumentDaoOjb = (OleLoanDocumentDaoOjb)SpringContext.getBean("oleLoanDao");
979             OleDeliverDaoJdbc oleDeliverDaoJdbc = (OleDeliverDaoJdbc)SpringContext.getBean("oleDeliverDaoJdbc");
980            OlePatronDocument olePatronDocument =  oleDeliverDaoJdbc.getPatronDocument(patronBarcode);
981 
982             if (olePatronDocument!=null) {
983                 Long startTimeInit = System.currentTimeMillis();
984                 lookupUser = oleCirculationHelperService.initialiseLookupUser(olePatronDocument, agencyId);
985                 Long endTimeInit = System.currentTimeMillis();
986                 Long timeDiffInit = endTimeInit-startTimeInit;
987                 LOG.info("Time taken to set the init details : " +timeDiffInit);
988                 try {
989                     Long startTimeLoan = System.currentTimeMillis();
990                     //   List<OleLoanDocument> oleLoanDocumentList = loanProcessor.getPatronLoanedItemBySolr(olePatronDocument.getOlePatronId());
991                   List<OleLoanDocument> oleLoanDocumentList = oleLoanDocumentDaoOjb.getDeliverLoans(olePatronDocument.getOlePatronId());
992                   if(oleLoanDocumentList!=null){
993                    List<OLECheckedOutItem> oleCheckedOutItemList =  oleDeliverDaoJdbc.getCheckedOutItemsList(oleLoanDocumentList);
994                     //List<OLECheckedOutItem> checkedOutItemList = getPatronCheckedOutItemList(olePatronDocument.getOleLoanDocuments(),olePatronDocument.getOleBorrowerType().getBorrowerTypeCode());
995                     if(oleCheckedOutItemList!=null){
996                     OLECheckedOutItems oleCheckedOutItems = new OLECheckedOutItems();
997                     if (oleCheckedOutItemList != null && oleCheckedOutItemList.size() > 0) {
998                         oleCheckedOutItems.setCheckedOutItems(oleCheckedOutItemList);
999                     }
1000                     lookupUser.setOleCheckedOutItems(oleCheckedOutItems);
1001                     }
1002                     }
1003                     Long endTimeLoan = System.currentTimeMillis();
1004                     Long timeDiffLoan = endTimeLoan-startTimeLoan;
1005                     LOG.info("Time taken to set the loan details : " +timeDiffLoan);
1006                 } catch (Exception e) {
1007                     LOG.info("Exception Occurred While Retrieving the checked out items");
1008                     LOG.error(e);
1009                 }
1010                 try {
1011                     Long startTimeHold = System.currentTimeMillis();
1012                  /*   List<OLEHold> oleHoldList = getHoldRecordsList(olePatronDocument.getOleDeliverRequestBos());*/
1013 
1014                     List<OleDeliverRequestBo> oleDeliverRequestBoList = oleLoanDocumentDaoOjb.getDeliverRequests(patronBarcode);
1015 
1016                     if(oleDeliverRequestBoList!=null){
1017                     List<OLEHold> oleHoldList = oleDeliverDaoJdbc.getHoldRecordsList(oleDeliverRequestBoList);
1018                     if(oleHoldList!=null){
1019                     OLEHolds oleHolds = new OLEHolds();
1020                     if (oleHoldList != null && oleHoldList.size() > 0) {
1021                         oleHolds.setOleHoldList(oleHoldList);
1022                     }
1023                     lookupUser.setOleHolds(oleHolds);
1024                     }
1025                     }
1026                     Long endTimeHold = System.currentTimeMillis();
1027                     Long timeDiffHold = endTimeHold-startTimeHold;
1028                     LOG.info("Time taken to set the hold details : " +timeDiffHold);
1029                 } catch (Exception e) {
1030                     LOG.info("Exception Occurred While Retrieving the Hold items");
1031                     LOG.error(e);
1032                 }
1033                 try {
1034                     Long startTimeFine = System.currentTimeMillis();
1035                     List<PatronBillPayment> patronBillPaymentList = oleLoanDocumentDaoOjb.getPatronBillPayments(olePatronDocument.getOlePatronId());
1036                     if(patronBillPaymentList!=null){
1037                      List<OLEItemFine> oleItemFineList = oleDeliverDaoJdbc.getFineItemLists(patronBillPaymentList);
1038                         if(oleItemFineList!=null){
1039                      OLEItemFines oleItemFines = new OLEItemFines();
1040                      oleItemFines.setOleItemFineList(oleItemFineList);
1041                     lookupUser.setOleItemFines(oleItemFines);
1042                         }
1043                     }
1044                     Long endTimeFine = System.currentTimeMillis();
1045                     Long timeDiffFine = endTimeFine-startTimeFine;
1046                     LOG.info("Time taken to set the fine details : " +timeDiffFine);
1047                 } catch (Exception e) {
1048                     LOG.info("Exception Occurred While Retrieving Fine");
1049                     LOG.error(e);
1050                 }
1051                 lookupUser.setCode("000");
1052                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS));
1053             } else {
1054                 lookupUser.setCode("002");
1055                 lookupUser.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
1056             }
1057         }
1058         String responseMessage = oleLookupUserConverter.generateLookupUserResponseXml(lookupUser);
1059        Long endTime = System.currentTimeMillis();
1060        Long timediff = endTime-startTime;
1061        LOG.info("Time taken to complete the lookup user service " + timediff);
1062        return responseMessage;
1063     }
1064 
1065 
1066 }
1067 
1068 
1069