View Javadoc
1   package org.kuali.ole.ncip.service.impl;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.extensiblecatalog.ncip.v2.service.AgencyId;
7   import org.extensiblecatalog.ncip.v2.service.InitiationHeader;
8   import org.kuali.ole.DataCarrierService;
9   import org.kuali.ole.OLEConstants;
10  import org.kuali.ole.bo.*;
11  import org.kuali.ole.bo.OLECheckOutItem;
12  import org.kuali.ole.bo.OLERenewItem;
13  import org.kuali.ole.deliver.bo.*;
14  import org.kuali.ole.deliver.processor.LoanProcessor;
15  import org.kuali.ole.deliver.service.CircDeskLocationResolver;
16  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
17  import org.kuali.ole.describe.bo.OleLocation;
18  import org.kuali.ole.describe.bo.OleLocationLevel;
19  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
20  import org.kuali.ole.docstore.common.document.*;
21  import org.kuali.ole.docstore.common.document.HoldingsTree;
22  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
23  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
24  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
25  import org.kuali.ole.docstore.common.document.content.instance.*;
26  import org.kuali.ole.docstore.common.document.content.instance.Item;
27  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
28  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
29  import org.kuali.ole.ncip.bo.*;
30  import org.kuali.ole.converter.OLECheckInItemConverter;
31  import org.kuali.ole.converter.OLECheckOutItemConverter;
32  import org.kuali.ole.converter.OLERenewItemConverter;
33  import org.kuali.ole.ncip.service.OLESIAPIHelperService;
34  import org.kuali.ole.service.OleCirculationPolicyService;
35  import org.kuali.ole.service.OleCirculationPolicyServiceImpl;
36  import org.kuali.ole.sys.context.SpringContext;
37  import org.kuali.ole.util.DocstoreUtil;
38  import org.kuali.rice.core.api.config.property.ConfigContext;
39  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
40  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
41  import org.kuali.rice.coreservice.api.parameter.Parameter;
42  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
43  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
44  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
45  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
46  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
47  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
48  import org.kuali.rice.krad.service.BusinessObjectService;
49  import org.kuali.rice.krad.service.KRADServiceLocator;
50  import org.kuali.rice.krms.api.engine.EngineResults;
51  import org.kuali.rice.krms.api.engine.ResultEvent;
52  import org.kuali.rice.krms.framework.engine.BasicRule;
53  
54  import java.sql.Timestamp;
55  import java.text.ParseException;
56  import java.text.SimpleDateFormat;
57  import java.util.*;
58  
59  /**
60   * Created with IntelliJ IDEA.
61   * User: maheswarang
62   * Date: 7/21/13
63   * Time: 3:47 PM
64   * To change this template use File | Settings | File Templates.
65   */
66  public class OLECirculationHelperServiceImpl {
67      private static final Logger LOG = Logger.getLogger(OLECirculationHelperServiceImpl.class);
68      private static final String DOCSTORE_URL = "docstore.url";
69      private final String CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING = "docAction=ingestContent&stringContent=";
70      private BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
71      private LoanProcessor loanProcessor;
72      private OLECheckInItemConverter oleCheckInItemConverter = new OLECheckInItemConverter();
73      private OLECheckOutItemConverter oleCheckOutItemConverter = new OLECheckOutItemConverter();
74      private OLESIAPIHelperService oleSIAPIHelperService;
75      private OleCirculationPolicyService oleCirculationPolicyService = getOleCirculationPolicyService();
76      private DocstoreClientLocator docstoreClientLocator;
77      private CircDeskLocationResolver circDeskLocationResolver;
78      private CircDeskLocationResolver getCircDeskLocationResolver() {
79          if (circDeskLocationResolver == null) {
80              circDeskLocationResolver = new CircDeskLocationResolver();
81          }
82          return circDeskLocationResolver;
83      }
84  
85      public void setCircDeskLocationResolver(CircDeskLocationResolver circDeskLocationResolver) {
86          this.circDeskLocationResolver = circDeskLocationResolver;
87      }
88      public OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
89      DocstoreUtil docstoreUtil = new DocstoreUtil();
90      private Map<String,OleBorrowerType> oleBorrowerTypeMap = getAvailableBorrowerTypes();
91  
92      public DocstoreClientLocator getDocstoreClientLocator() {
93  
94          if (docstoreClientLocator == null) {
95              docstoreClientLocator = (DocstoreClientLocator) SpringContext.getService("docstoreClientLocator");
96  
97          }
98          return docstoreClientLocator;
99      }
100 
101     public LoanProcessor getLoanProcessor(){
102         if (loanProcessor == null) {
103             loanProcessor = (LoanProcessor) SpringContext.getService("loanProcessor");
104 
105         }
106         return loanProcessor;
107     }
108 
109 
110     public OleCirculationPolicyService getOleCirculationPolicyService() {
111         if (null == oleCirculationPolicyService) {
112             oleCirculationPolicyService = new OleCirculationPolicyServiceImpl();
113         }
114         return oleCirculationPolicyService;
115     }
116 
117     public BusinessObjectService getBusinessObjectService() {
118         return businessObjectService;
119     }
120 
121     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
122         this.businessObjectService = businessObjectService;
123     }
124 
125     public OLESIAPIHelperService getOleSIAPIHelperService() {
126         if (oleSIAPIHelperService == null) {
127             oleSIAPIHelperService = (OLESIAPIHelperService) SpringContext.getService("oleSIAPIHelperService");
128         }
129         return oleSIAPIHelperService;
130     }
131 
132     public void setOleSIAPIHelperService(OLESIAPIHelperService oleSIAPIHelperService) {
133         this.oleSIAPIHelperService = oleSIAPIHelperService;
134     }
135 
136     public Map<String,OleBorrowerType> getAvailableBorrowerTypes(){
137         Map<String,OleBorrowerType> borrowerTypeMap = new HashMap<String,OleBorrowerType>();
138         List<OleBorrowerType> oleBorrowerTypeList = (List<OleBorrowerType>)businessObjectService.findAll(OleBorrowerType.class);
139         if(oleBorrowerTypeList!=null && oleBorrowerTypeList.size()>0){
140             for(OleBorrowerType oleBorrowerType : oleBorrowerTypeList){
141                 borrowerTypeMap.put(oleBorrowerType.getBorrowerTypeId(),oleBorrowerType);
142             }
143         }
144        return borrowerTypeMap;
145     }
146 
147     public OLELookupUser initialiseLookupUser(OlePatronDocument olePatronDocument, String agencyId) {
148         OLELookupUser oleLookupUser = new OLELookupUser();
149         oleLookupUser.setPatronId(olePatronDocument.getBarcode());
150         OlePatronEmailBo olePatronEmailBo = getDefaultEmailBo(olePatronDocument.getOlePatronId());
151         if (olePatronEmailBo != null) {
152             oleLookupUser.setPatronEmail(olePatronEmailBo);
153         }
154         OlePatronAddressBo olePatronAddressBo = getDefaultAddressBo(olePatronDocument.getOlePatronId());
155         if (olePatronAddressBo != null) {
156             oleLookupUser.setPatronAddress(olePatronAddressBo);
157         }
158         OlePatronPhoneBo olePatronPhoneBo = getDefaultPhoneBo(olePatronDocument.getOlePatronId());
159         if (olePatronPhoneBo != null) {
160             oleLookupUser.setPatronPhone(olePatronPhoneBo);
161         }
162         List<OLEUserPrivilege> oleUserPrivilegeList = getPatronPrivilege(olePatronDocument, agencyId);
163         if ( oleUserPrivilegeList!= null) {
164             oleLookupUser.setOleUserPrivileges(oleUserPrivilegeList);
165         }
166         OlePatronNameBo olePatronNameBo = getEntityNameBo(olePatronDocument.getOlePatronId());
167         if (olePatronNameBo != null) {
168             oleLookupUser.setPatronName(olePatronNameBo);
169         }
170         return oleLookupUser;
171     }
172 
173     public EntityBo getEntity(String entityId) {
174         LOG.info("Inside getEntity : Entity Id : " + entityId);
175         Map<String, String> entityMap = new HashMap<>();
176         entityMap.put("id", entityId);
177         List<EntityBo> entityBoList = (List<EntityBo>) businessObjectService.findMatching(EntityBo.class, entityMap);
178         if (entityBoList.size() > 0)
179             return entityBoList.get(0);
180         return null;
181     }
182 
183     public OlePatronEmailBo getDefaultEmailBo(String entityId) {
184         LOG.info("Inside getDefaultEmailBo : Entity Id : " + entityId);
185         EntityBo entityBo = getEntity(entityId);
186         OlePatronEmailBo olePatronEmailBo = null;
187         if (entityBo != null) {
188             if (entityBo.getEntityTypeContactInfos() != null && entityBo.getEntityTypeContactInfos().size() > 0)
189                 if (entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses() != null && entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses().size() > 0) {
190                     for (EntityEmailBo entityEmailBo : entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses()) {
191                         if (entityEmailBo.getDefaultValue()) {
192                             olePatronEmailBo = new OlePatronEmailBo();
193                             olePatronEmailBo.setEmailTypeCode(entityEmailBo.getEmailTypeCode());
194                             olePatronEmailBo.setEmailAddress(entityEmailBo.getEmailAddress());
195                             return olePatronEmailBo;
196                         }
197                     }
198                 }
199         }
200         return null;
201     }
202 
203     public OlePatronAddressBo getDefaultAddressBo(String entityId) {
204         LOG.info("Inside getDefaultAddressBo : Entity Id : " + entityId);
205         EntityBo entityBo = getEntity(entityId);
206         EntityAddressBo entityAddressBo = null;
207         OlePatronAddressBo olePatronAddressBo = null;
208         if (entityBo != null) {
209             if (entityBo.getEntityTypeContactInfos() != null && entityBo.getEntityTypeContactInfos().size() > 0)
210                 if (entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress() != null) {
211                     entityAddressBo = entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress();
212                     olePatronAddressBo = new OlePatronAddressBo();
213                     olePatronAddressBo.setAddressTypeCode(entityAddressBo.getAddressTypeCode());
214                     olePatronAddressBo.setCity(entityAddressBo.getCity());
215                     olePatronAddressBo.setCountryCode(entityAddressBo.getCountryCode());
216                     olePatronAddressBo.setLine1(entityAddressBo.getLine1());
217                     olePatronAddressBo.setLine2(entityAddressBo.getLine2());
218                     olePatronAddressBo.setLine3(entityAddressBo.getLine3());
219                     olePatronAddressBo.setPostalCode(entityAddressBo.getPostalCode());
220                     olePatronAddressBo.setStateProvinceCode(entityAddressBo.getStateProvinceCode());
221                     return olePatronAddressBo;
222                 }
223         }
224         return null;
225     }
226 
227     public OlePatronNameBo getEntityNameBo(String entityId) {
228         LOG.info("Inside getEntityNameBo : Entity Id : " + entityId);
229         EntityBo entityBo = getEntity(entityId);
230         EntityNameBo entityNameBo = null;
231         OlePatronNameBo olePatronNameBo = null;
232         if (entityBo != null) {
233             if (entityBo.getNames() != null && entityBo.getNames().size() > 0) {
234                 entityNameBo = entityBo.getNames().get(0);
235                 olePatronNameBo = new OlePatronNameBo();
236                 olePatronNameBo.setFirstName(entityNameBo.getFirstName());
237                 olePatronNameBo.setMiddleName(entityNameBo.getMiddleName());
238                 olePatronNameBo.setLastName(entityNameBo.getLastName());
239                 return olePatronNameBo;
240             }
241         }
242         return null;
243     }
244 
245     public OlePatronPhoneBo getDefaultPhoneBo(String entityId) {
246         LOG.info("Inside getDefaultPhoneBo : Entity Id : " + entityId);
247         EntityBo entityBo = getEntity(entityId);
248         EntityPhoneBo entityPhoneBo = null;
249         OlePatronPhoneBo olePatronPhoneBo = null;
250         if (entityBo != null) {
251             if (entityBo.getEntityTypeContactInfos().get(0) != null && entityBo.getEntityTypeContactInfos().size() > 0) {
252                 if (entityBo.getEntityTypeContactInfos().get(0).getDefaultPhoneNumber() != null) {
253                     entityPhoneBo = entityBo.getEntityTypeContactInfos().get(0).getDefaultPhoneNumber();
254                     olePatronPhoneBo = new OlePatronPhoneBo();
255                     olePatronPhoneBo.setPhoneTypeCode(entityPhoneBo.getPhoneTypeCode());
256                     olePatronPhoneBo.setPhoneNumber(entityPhoneBo.getPhoneNumber());
257                     return olePatronPhoneBo;
258                 }
259             }
260         }
261         return null;
262     }
263 
264     public List<OLEUserPrivilege> getPatronPrivilege(OlePatronDocument olePatronDocument, String agencyId) {
265         List<OLEUserPrivilege> userPrivilegeList;
266         if (olePatronDocument != null) {
267             userPrivilegeList = new ArrayList<OLEUserPrivilege>();
268             OLEUserPrivilege courtesyPrivilege = new OLEUserPrivilege();
269             courtesyPrivilege.setUserPrivilegeType(OLEConstants.COURTESY_NOTICE);
270             courtesyPrivilege.setUserPrivilegeDescription(OLEConstants.COURTESY_DESCRIPTION);
271             if (olePatronDocument.isCourtesyNotice()) {
272                 courtesyPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
273             } else {
274                 courtesyPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
275             }
276             userPrivilegeList.add(courtesyPrivilege);
277             OLEUserPrivilege deliverPrivilege = new OLEUserPrivilege();
278             deliverPrivilege.setUserPrivilegeType(OLEConstants.DELIVERY);
279             deliverPrivilege.setUserPrivilegeDescription(OLEConstants.DELIVERY_DESCRIPTION);
280             if (olePatronDocument.isDeliveryPrivilege()){
281                 deliverPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
282             }else{
283             deliverPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
284             }
285                 userPrivilegeList.add(deliverPrivilege);
286             OLEUserPrivilege pagingPrivilege = new OLEUserPrivilege();
287             pagingPrivilege.setUserPrivilegeType(OLEConstants.PAGING);
288             pagingPrivilege.setUserPrivilegeDescription(OLEConstants.PAGING_DESCRIPTION);
289             if (olePatronDocument.isPagingPrivilege()) {
290                 pagingPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
291             }else{
292             pagingPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
293             }
294             userPrivilegeList.add(pagingPrivilege);
295             if (oleBorrowerTypeMap.get(olePatronDocument.getBorrowerType())!=null) {
296                 olePatronDocument.setOleBorrowerType(oleBorrowerTypeMap.get(olePatronDocument.getBorrowerType()));
297                 olePatronDocument.setBorrowerTypeCode(oleBorrowerTypeMap.get(olePatronDocument.getBorrowerType()).getBorrowerTypeCode());
298                 OLEUserPrivilege oleProfilePrivilege = new OLEUserPrivilege();
299                 oleProfilePrivilege.setUserPrivilegeType(OLEConstants.PROFILE);
300                 oleProfilePrivilege.setUserPrivilegeStatus(oleBorrowerTypeMap.get(olePatronDocument.getBorrowerType()).getBorrowerTypeName());
301                 oleProfilePrivilege.setUserPrivilegeDescription(OLEConstants.PROFILE_DESCRIPTION);
302                 userPrivilegeList.add(oleProfilePrivilege);
303             }
304             if (agencyId != null) {
305                 String itemType, itemLocation = "";
306                 HashMap<String, String> agencyPropertyMap = getAgencyPropertyMap(agencyId);
307                 itemType = agencyPropertyMap.get(OLEConstants.ITEM_TYPE);
308                 itemLocation = agencyPropertyMap.get(OLEConstants.ITEM_LOCATION);
309                 OLEUserPrivilege oleUserPrivilege = new OLEUserPrivilege();
310                 oleUserPrivilege.setUserPrivilegeType(OLEConstants.STATUS);
311                 oleUserPrivilege.setUserPrivilegeDescription(OLEConstants.STATUS_DESCRIPTION);
312                 oleUserPrivilege.setUserPrivilegeStatus(OLEConstants.OK);
313                 if (olePatronDocument.isGeneralBlock() || isPatronExpired(olePatronDocument) || !olePatronDocument.isActiveIndicator() || isPatronActivated(olePatronDocument) || !isAbleToCheckOut(olePatronDocument.getOlePatronId(), olePatronDocument.getBorrowerTypeCode(), itemType, itemLocation))
314                     oleUserPrivilege.setUserPrivilegeStatus(OLEConstants.BLOCKED);
315                 userPrivilegeList.add(oleUserPrivilege);
316             }
317             return userPrivilegeList;
318         }
319         return null;
320     }
321 
322     public boolean isPatronExpired(OlePatronDocument olePatronDocument) {
323 
324         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
325         Date expirationDate = olePatronDocument.getExpirationDate();
326         if (expirationDate != null) {
327             if ((fmt.format(expirationDate)).compareTo(fmt.format(new Date(System.currentTimeMillis()))) > 0) {
328                 return false;
329             } else {
330                 return true;
331             }
332         }else{
333             return false;
334         }
335     }
336 
337     public boolean isPatronActivated(OlePatronDocument olePatronDocument) {
338 
339         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
340         Date activationDate = olePatronDocument.getActivationDate();
341         if (activationDate != null) {
342             if ((fmt.format(activationDate)).compareTo(fmt.format(new Date(System.currentTimeMillis()))) <= 0) {
343                 return false;
344             } else {
345                 return true;
346             }
347         } else {
348             return false;
349         }
350     }
351 
352     public boolean isAbleToCheckOut(String patronId, String borrowerType, String itemType, String itemLocation) {
353         LOG.info("Inside isAbleToCheckOut method . Patron Id : " +patronId + " Borrower Type : " + borrowerType + " ItemType : "+itemType + " Item Location : "  + itemLocation);
354         boolean allowed = true;
355         Long startTime = System.currentTimeMillis();
356         String agendaName = OLEConstants.CHECK_OUT_AGENDA_NM;
357         HashMap<String, Object> termValues = new HashMap<String, Object>();
358         List<FeeType> feeTypeList = oleCirculationPolicyService.getPatronBillPayment(patronId);
359         Integer overdueFineAmt = 0;
360         Integer replacementFeeAmt = 0;
361         for (FeeType feeType : feeTypeList) {
362             overdueFineAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.OVERDUE_FINE) ? feeType.getFeeAmount().intValue() : 0;
363             replacementFeeAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.REPLACEMENT_FEE) ? feeType.getFeeAmount().intValue() : 0;
364         }
365         String[] locationArray = itemLocation.split("['/']");
366         List<String> locationList = Arrays.asList(locationArray);
367         for (String value : locationList) {
368             Map<String, String> requestMap = new HashMap<>();
369             requestMap.put(OLEConstants.LOCATION_CODE, value);
370             List<OleLocation> oleLocations = (List<OleLocation>) businessObjectService.findMatching(OleLocation.class, requestMap);
371             if (oleLocations != null && oleLocations.size() > 0) {
372                 String locationLevelId = oleLocations.get(0).getLevelId();
373                 requestMap.clear();
374                 requestMap.put(OLEConstants.LEVEL_ID, locationLevelId);
375                 List<OleLocationLevel> oleLocationLevels = (List<OleLocationLevel>) businessObjectService.findMatching(OleLocationLevel.class, requestMap);
376                 if (oleLocationLevels != null && oleLocationLevels.size() > 0) {
377                     OleLocationLevel oleLocationLevel = new OleLocationLevel();
378                     oleLocationLevel = oleLocationLevels.get(0);
379                     if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_CAMPUS)) {
380                         termValues.put(OLEConstants.ITEM_CAMPUS, value);
381                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_INSTITUTION)) {
382                         termValues.put(OLEConstants.ITEM_INSTITUTION, value);
383                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_COLLECTION)) {
384                         termValues.put(OLEConstants.ITEM_COLLECTION, value);
385                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_LIBRARY)) {
386                         termValues.put(OLEConstants.ITEM_LIBRARY, value);
387                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_SHELVING)) {
388                         termValues.put(OLEConstants.ITEM_SHELVING, value);
389                     }
390                 }
391             }
392         }
393         termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
394         termValues.put(OLEConstants.ITEM_TYPE, itemType);
395         //termValues.put(OLEConstants.ITEM_SHELVING, itemLocation);
396         termValues.put(OLEConstants.OVERDUE_FINE_AMT, overdueFineAmt);
397         termValues.put(OLEConstants.REPLACEMENT_FEE_AMT, replacementFeeAmt);
398         termValues.put(OLEConstants.ALL_CHARGES, overdueFineAmt + replacementFeeAmt);
399         /*termValues.put(OLEConstants.ITEM_STATUS, "AVAILABLE");
400         termValues.put("isCirculationPolicyNotFound","false");*/
401         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
402         String itemId = "";
403         dataCarrierService.removeData(patronId+itemId);
404         dataCarrierService.addData(OLEConstants.GROUP_ID, "100");
405         HashMap keyLoanMap=new HashMap();
406         keyLoanMap=oleCirculationPolicyService.getLoanedKeyMap(patronId,false);
407         List<Integer> listOfOverDueDays =(List<Integer>)keyLoanMap.get(OLEConstants.LIST_OF_OVERDUE_DAYS);
408         dataCarrierService.addData(OLEConstants.LIST_OVERDUE_DAYS, listOfOverDueDays);
409 
410         dataCarrierService.addData(OLEConstants.LIST_RECALLED_OVERDUE_DAYS, (List<Integer>) keyLoanMap.get(OLEConstants.LIST_RECALLED_OVERDUE_DAYS));
411         termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
412         termValues.put(OLEConstants.ITEM_ID_POLICY, "");
413         try {
414             EngineResults engineResults = getLoanProcessor().getEngineResults(agendaName, termValues);
415             dataCarrierService.removeData(patronId+itemId);
416             List<ResultEvent> allResults = engineResults.getAllResults();
417             for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
418                 ResultEvent resultEvent = resultEventIterator.next();
419                 if (resultEvent.getSource() instanceof BasicRule) {
420                     BasicRule basicRule = (BasicRule) resultEvent.getSource();
421                     if (resultEvent.getType().equals(OLEConstants.RULE_EVALUATED) && ((basicRule.getName().equals(OLENCIPConstants.CHECK_REPLACEMENT_FEE_AMT) && resultEvent.getResult())
422                             || (basicRule.getName().equals(OLENCIPConstants.CHECK_ALL_OVERDUE_FINE_AMT) && resultEvent.getResult()) || (basicRule.getName().equals(OLENCIPConstants.CHECK_OVERALL_CHARGES) && resultEvent.getResult()))) {
423                         // renewalExceeds=true;  Check all Overdue fine amount
424                         allowed = false;
425                         break;
426                     }
427                 }
428             }
429             List<String> errorMessage = (List<String>) engineResults.getAttribute(OLEConstants.ERROR_ACTION);
430             if (errorMessage != null && errorMessage.size() > 0) {
431                 allowed = false;
432             }
433         } catch (Exception e) {
434             LOG.info("Exception Occured while evaluating the KRMS rules");
435             LOG.error(e, e);
436         }
437         Long endTime =System.currentTimeMillis();
438         Long timeDifference = endTime-startTime;
439         LOG.info("Time taken for the krms fro getting the patron status " + timeDifference);
440         return allowed;
441     }
442 
443     public String checkOutItem(String patronId, String operatorId, String itemBarcode, boolean isSIP2Request) {
444         LOG.info("In  Check Out Item . Patron Barcode : "+patronId + " OperatorId : " +operatorId + "Item Barcode : "+itemBarcode);
445         OlePatronDocument olePatronDocument = null;
446         if(itemBarcode == null ||( itemBarcode!=null && itemBarcode.trim().isEmpty())){
447             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
448             oleCheckOutItem.setCode("900");
449             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_REQUIRED));
450             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_REQUIRED));
451             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
452         }
453        /*  Map<String, String> patronMap = new HashMap<String, String>();
454         patronMap.put(OLEConstants.BARCODE, patronId);
455         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
456         if (olePatronDocumentList.size() > 0) {
457             olePatronDocument = olePatronDocumentList.get(0);
458         } else {
459             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
460             oleCheckOutItem.setCode("002");
461             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
462             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
463             if(isSIP2Request){
464                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
465             }else{
466             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
467             }
468         }*/
469         OleLoanDocument oleLoanDocument;
470         try {
471             try{
472                 oleLoanDocument = getLoanProcessor().getLoanDocument(patronId, null, true, false);
473                 olePatronDocument = oleLoanDocument.getOlePatron();
474             }catch (Exception e){
475                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
476                 oleCheckOutItem.setCode("002");
477                 oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
478                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
479                 if(isSIP2Request){
480                     return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
481                 }else{
482                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
483                 }
484             }
485             long t66 = System.currentTimeMillis();
486             oleLoanDocument.setLoanOperatorId(operatorId);
487             Map<String, String> circulationDeskDetailMaps = new HashMap<String, String>();
488             circulationDeskDetailMaps.put(OLENCIPConstants.OPERATOR_ID, operatorId);
489             circulationDeskDetailMaps.put("defaultLocation", "Y");
490             List<OleCirculationDeskDetail> oleCirculationDeskDetailList = (List<OleCirculationDeskDetail>) businessObjectService.findMatching(OleCirculationDeskDetail.class, circulationDeskDetailMaps);
491             if (oleCirculationDeskDetailList != null && oleCirculationDeskDetailList.size() > 0) {
492                 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList) {
493                     if (oleCirculationDeskDetail.isDefaultLocation()) {
494                         String circulationDeskId = oleCirculationDeskDetail.getCirculationDeskId();
495                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
496                         if(oleCirculationDeskDetail.getOleCirculationDesk()!=null){
497                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskDetail.getOleCirculationDesk());
498                         }else{
499                             Map<String, String> circulationMap = new HashMap<String, String>();
500                             circulationMap.put(OLEConstants.CIRCULATION_DESK_ID, circulationDeskId);
501                             List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) businessObjectService.findMatching(OleCirculationDesk.class, circulationMap);
502                             if (oleCirculationDeskList.size() > 0){
503                                 oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
504                                 break;
505                             }
506                         }
507                     }
508                 }
509             } else {
510                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
511                 oleCheckOutItem.setCode("026");
512                 oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
513                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
514                 if(isSIP2Request){
515                     return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
516                 }else{
517                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
518                 }
519             }
520             if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
521                 if (olePatronDocument != null) {
522                     oleLoanDocument.setLoanOperatorId(operatorId);
523                     oleLoanDocument = getLoanProcessor().addLoan(olePatronDocument.getBarcode(), itemBarcode, oleLoanDocument, operatorId);
524                     if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
525                         OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
526                         oleCheckOutItem.setDueDate(oleLoanDocument.getLoanDueDate() != null ? oleLoanDocument.getLoanDueDate().toString() : "");
527                         oleCheckOutItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
528                         oleCheckOutItem.setUserType(oleLoanDocument.getBorrowerTypeName());
529                         oleCheckOutItem.setBarcode(oleLoanDocument.getItemId());
530                         oleCheckOutItem.setPatronId(oleLoanDocument.getPatronId());
531                         oleCheckOutItem.setPatronBarcode(patronId);
532                         if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemType() != null) {
533                             oleCheckOutItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
534                         }
535                         oleCheckOutItem.setCode("030");
536                         oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED));
537                         oleCheckOutItem.setItemProperties("Author : " + oleLoanDocument.getAuthor() + " , Status : " + oleLoanDocument.getItemStatus());
538                         oleCheckOutItem.setItemType(oleLoanDocument.getItemType());
539                         LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED));
540                         if(isSIP2Request){
541                             return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
542                         }else{
543                             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
544                         }
545 
546                     } else {
547                         if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getLocation() == null) {
548                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
549                             oleCheckOutItem.setCode("028");
550                             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC));
551                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC));
552                             if(isSIP2Request){
553                                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
554                             }else{
555                                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
556                             }
557                         } else if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemStatus() != null &&
558                                 oleLoanDocument.getOleItem().getItemStatus().getCodeValue() != null && oleLoanDocument.getOleItem().getItemStatus().getCodeValue().equalsIgnoreCase("LOANED")) {
559 
560                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
561                             oleCheckOutItem.setCode("100");
562                             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_IN_LOAN));
563                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_IN_LOAN));
564                             if(isSIP2Request){
565                                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
566                             }else{
567                                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
568                             }
569                         } else if(oleLoanDocument != null && oleLoanDocument.getErrorMessage() !=null && oleLoanDocument.getErrorMessage().equalsIgnoreCase(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS))){
570                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
571                             oleCheckOutItem.setCode("014");
572                             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
573                             LOG.info(oleLoanDocument.getErrorMessage());
574                             if(isSIP2Request){
575                                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
576                             }else{
577                                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
578                             }
579                         }else {
580                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
581                             oleCheckOutItem.setCode("500");
582                             oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
583                             LOG.info(oleLoanDocument.getErrorMessage());
584                             if(isSIP2Request){
585                                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
586                             }else{
587                                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
588                             }
589                         }
590                     }
591                 } else {
592                     OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
593                     oleCheckOutItem.setCode("500");
594                     oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
595                     LOG.info(oleLoanDocument.getErrorMessage());
596                     if(isSIP2Request){
597                         return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
598                     }else{
599                         return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
600                     }
601                 }
602             } else {
603                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
604                 oleCheckOutItem.setCode("500");
605                 oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
606                 LOG.info(oleLoanDocument.getErrorMessage());
607                 if(isSIP2Request){
608                     return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
609                 }else{
610                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
611                 }
612             }
613         } catch (Exception e) {
614             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
615             if(e.getCause()!= null && (e.getCause().getMessage()).contains("Duplicate entry")){
616                 oleCheckOutItem.setCode("100");
617                 oleCheckOutItem.setMessage("Item is already Loaned by a patron.");
618             }else if(e.getLocalizedMessage() == null){
619                 oleCheckOutItem.setCode("500");
620                 oleCheckOutItem.setMessage("Internal error");
621             } else {
622                 oleCheckOutItem.setCode("014");
623                 oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
624             }
625             LOG.info(oleCheckOutItem.getMessage());
626             LOG.error(e, e);
627             if(isSIP2Request){
628                 return oleCheckOutItemConverter.generateCheckOutItemXmlForSIP2(oleCheckOutItem);
629             }else{
630                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
631             }
632         }
633     }
634 
635     public String checkInItem(String patronBarcode, String operatorId, String itemBarcode, String deleteIndicator, boolean isSIP2Request) {
636         LOG.info("Inside checkInItem method .Patron barcode : " + patronBarcode + " Operator Id : " +operatorId + " Item Barcode : " + itemBarcode );
637 
638         OleLoanDocument oleLoanDocument = null;
639         OLECheckInItem oleCheckInItem = new OLECheckInItem();
640         try {
641             // oleLoanDocument= loanProcessor.returnLoan(oleLoanDocument);
642 
643             if (itemBarcode != null) {
644                 oleLoanDocument = getLoanProcessor().getOleLoanDocumentUsingItemBarcode(itemBarcode);
645             } else {
646                 oleLoanDocument = getLoanProcessor().getOleLoanDocumentUsingItemUUID(itemBarcode);
647             }
648             if (oleLoanDocument == null) {
649                 oleLoanDocument = new OleLoanDocument();
650             }
651             Map<String, String> circulationDeskDetailMap = new HashMap<String, String>();
652             circulationDeskDetailMap.put(OLENCIPConstants.OPERATOR_ID, operatorId);
653             circulationDeskDetailMap.put("defaultLocation", "Y");
654             List<OleCirculationDeskDetail> oleCirculationDeskDetailList = (List<OleCirculationDeskDetail>) businessObjectService.findMatching(OleCirculationDeskDetail.class, circulationDeskDetailMap);
655             if (oleCirculationDeskDetailList != null && oleCirculationDeskDetailList.size() > 0) {
656                 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList) {
657                     if (oleCirculationDeskDetail.isDefaultLocation()) {
658                         String circulationDeskId = oleCirculationDeskDetail.getCirculationDeskId();
659                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
660                         if(oleCirculationDeskDetail.getOleCirculationDesk()!=null){
661                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskDetail.getOleCirculationDesk());
662                         }else{
663                             Map<String, String> circulationMap = new HashMap<String, String>();
664                             circulationMap.put(OLEConstants.CIRCULATION_DESK_ID, circulationDeskId);
665                             List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) businessObjectService.findMatching(OleCirculationDesk.class, circulationMap);
666                             if (oleCirculationDeskList.size() > 0){
667                                 oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
668                             break;
669                             }
670                         }
671                     }
672                 }
673                 oleLoanDocument = getLoanProcessor().returnLoan(itemBarcode, oleLoanDocument);
674                 oleCheckInItem.setAuthor(oleLoanDocument.getAuthor());
675                 oleCheckInItem.setTitle(oleLoanDocument.getTitle());
676                 oleCheckInItem.setCallNumber(oleLoanDocument.getItemCallNumber());
677                 oleCheckInItem.setBarcode(oleLoanDocument.getPatronBarcode());
678                 oleCheckInItem.setUserId(oleLoanDocument.getPatronBarcode());
679                 oleCheckInItem.setUserType(oleLoanDocument.getBorrowerTypeName());
680                 if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemType() != null) {
681                     oleCheckInItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
682                 }
683                 if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
684                     oleCheckInItem.setCode("024");
685                     oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN));
686                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN));
687                     if (deleteIndicator!=null && deleteIndicator.equalsIgnoreCase("y")) {
688 
689                         org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(oleLoanDocument.getItemUuid());
690                         String bibId = item.getHolding().getBib().getId();
691 
692                         getDocstoreClientLocator().getDocstoreClient().deleteBib(bibId);
693                     }
694                     if(isSIP2Request){
695                         return oleCheckInItemConverter.generateCheckInItemXmlForSIP2(oleCheckInItem);
696                     }else{
697                         return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
698                     }
699 
700                 } else {
701                     oleCheckInItem.setCode("500");
702                     oleCheckInItem.setMessage(oleLoanDocument.getErrorMessage());
703                     LOG.info(oleLoanDocument.getErrorMessage());
704                     if(isSIP2Request){
705                         return oleCheckInItemConverter.generateCheckInItemXmlForSIP2(oleCheckInItem);
706                     }else{
707                         return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
708                     }
709                 }
710             } else {
711                 oleCheckInItem.setCode("025");
712                 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
713                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
714                 if(isSIP2Request){
715                     return oleCheckInItemConverter.generateCheckInItemXmlForSIP2(oleCheckInItem);
716                 }else{
717                     return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
718                 }
719             }
720         } catch (Exception e) {
721             if(e.getMessage()!=null && (e.getMessage().equals(OLEConstants.ITM_BARCD_NT_AVAL_DOC)||e.getMessage().equals(OLEConstants.INVAL_ITEM) ||e.getMessage().equals(OLEConstants.ITM_STS_NT_AVAL)||e.getMessage().equals(OLEConstants.NO_LOC_CIR_DESK))){
722                 oleCheckInItem.setCode("014");
723                 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED) + "." + e.getMessage());
724                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(e.getMessage()));
725                 LOG.error(e,e);
726                 if(isSIP2Request){
727                     return oleCheckInItemConverter.generateCheckInItemXmlForSIP2(oleCheckInItem);
728                 }else{
729                     return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
730                 }
731             }
732 
733             oleCheckInItem.setCode("025");
734             oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
735             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
736             if(isSIP2Request){
737                 return oleCheckInItemConverter.generateCheckInItemXmlForSIP2(oleCheckInItem);
738             }else{
739                 return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
740             }
741         }
742     }
743 
744     public String acceptItem(String itemBarcode, String callNumber, String title, String author, String itemType, String itemLocation) throws Exception {
745         LOG.info("Inside Accept Item . Item barcode :" + itemBarcode + " Call Number : "+callNumber + "Title : "+title + " Author : " +author + "Item Type : "+ itemType + "Item Location : "+itemLocation);
746         String itemIdentifier = null;
747         if (docstoreUtil.isItemAvailableInDocStore(itemBarcode)) {
748             return itemIdentifier;
749         }
750         BibTrees bibTrees = createItem(itemBarcode, callNumber, title, author, itemType, itemLocation);
751         if(bibTrees!=null &&  bibTrees.getBibTrees()!=null && bibTrees.getBibTrees().size()>0  &&bibTrees.getBibTrees().get(0).getHoldingsTrees()!=null  && bibTrees.getBibTrees().get(0).getHoldingsTrees().size()>0
752                 && bibTrees.getBibTrees().get(0).getHoldingsTrees().get(0).getItems() != null && bibTrees.getBibTrees().get(0).getHoldingsTrees().get(0).getItems().size()>0 ){
753             itemIdentifier= bibTrees.getBibTrees().get(0).getHoldingsTrees().get(0).getItems().get(0).getId();
754         }else{
755             itemIdentifier="";
756         }
757         LOG.info("Item Created with identifier : " + itemIdentifier);
758         return itemIdentifier;
759     }
760 
761     public BibTrees createItem(String itemBarcode, String callNumber, String title, String author, String itemType, String itemLocation) throws Exception {
762         BibMarcRecord bibMarcRecord = getLoanProcessor().getBibMarcRecord(title, author);
763 
764         List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
765         bibMarcRecordList.add(bibMarcRecord);
766 
767         BibMarcRecords bibMarcRecords = new BibMarcRecords();
768         bibMarcRecords.setRecords(bibMarcRecordList);
769         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
770 
771 
772         Bib bib = new BibMarc();
773         bib.setStaffOnly(true);
774         bib.setCategory(org.kuali.ole.docstore.common.document.content.enums.DocCategory.WORK.getCode());
775         bib.setType(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode());
776         bib.setFormat(org.kuali.ole.docstore.common.document.content.enums.DocFormat.MARC.getCode());
777         bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
778         bib.setOperation(DocstoreDocument.OperationType.CREATE);
779 
780 
781         OleHoldings oleHoldings = new OleHoldings();
782         LocationLevel locationLevel = new LocationLevel();
783         locationLevel = getCircDeskLocationResolver().createLocationLevel(itemLocation, locationLevel);
784         Location holdingsLocation = new Location();
785         holdingsLocation.setPrimary(OLEConstants.TRUE);
786         holdingsLocation.setStatus(OLEConstants.PERMANENT);
787         holdingsLocation.setLocationLevel(locationLevel);
788         oleHoldings.setLocation(holdingsLocation);
789         oleHoldings.setStaffOnlyFlag(true);
790         Item item = new Item();
791 
792         AccessInformation accessInformation = new AccessInformation();
793         accessInformation.setBarcode(itemBarcode);
794         item.setAccessInformation(accessInformation);
795         item.setStaffOnlyFlag(true);
796         ItemStatus itemStatus = new ItemStatus();
797         itemStatus.setCodeValue(OLEConstants.AVAILABLE);
798         item.setItemStatus(itemStatus);
799         ItemType type = new ItemType();
800         type.setCodeValue(itemType);
801         item.setItemType(type);
802         CallNumber itemCallNumber = new CallNumber();
803         itemCallNumber.setNumber(callNumber);
804         item.setCallNumber(itemCallNumber);
805         ShelvingScheme shelvingScheme = new ShelvingScheme();
806         shelvingScheme.setCodeValue(OLEConstants.LCC);
807         itemCallNumber.setShelvingScheme(shelvingScheme);
808         //item.setExtension(extension);
809         item.setLocation(holdingsLocation);
810         ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
811         org.kuali.ole.docstore.common.document.Item documentItem = new ItemOleml();
812         documentItem.setContent(itemOlemlRecordProcessor.toXML(item));
813         documentItem.setStaffOnly(true);
814         documentItem.setOperation(DocstoreDocument.OperationType.CREATE);
815         Holdings holdings = new PHoldings();
816         holdings.setStaffOnly(true);
817         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
818         holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
819         holdings.setOperation(DocstoreDocument.OperationType.CREATE);
820         HoldingsTree holdingsTree = new HoldingsTree();
821         holdingsTree.setHoldings(holdings);
822         holdingsTree.getItems().add(documentItem);
823         BibTree bibTree = new BibTree();
824         bibTree.setBib(bib);
825         bibTree.getHoldingsTrees().add(holdingsTree);
826         BibTrees bibTrees = new BibTrees();
827         bibTrees.getBibTrees().add(bibTree);
828         bibTrees=getDocstoreClientLocator().getDocstoreClient().processBibTrees(bibTrees);
829         return bibTrees;
830     }
831 
832     public HashMap<String, String> getAgencyPropertyMap(String agencyId) {
833         HashMap<String, String> agencyPropertyMap = new HashMap<String, String>();
834         agencyPropertyMap = getOleSIAPIHelperService().getAgencyPropertyMap(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLENCIPConstants.NCIPAPI_PARAMETER_NAME, agencyId, agencyPropertyMap);
835         return agencyPropertyMap;
836     }
837 
838     public String renewItem(String patronBarcode, String operatorId, String itemBarcode, boolean isSIP2Request) {
839         LOG.info("Inside Renew Item . Patron Barcode :  " + patronBarcode + "Operator Id : "+ operatorId + " Item Barcode : " +itemBarcode);
840         OLERenewItem oleRenewItem = new OLERenewItem();
841         OLERenewItemConverter oleRenewItemConverter = new OLERenewItemConverter();
842         OlePatronDocument olePatronDocument = null;
843         Map<String, String> patronMap = new HashMap<String, String>();
844         patronMap.put(OLEConstants.BARCODE, patronBarcode);
845         List<OlePatronDocument> patronDocuments = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
846         if (patronDocuments.size() > 0) {
847             olePatronDocument = patronDocuments.get(0);
848         } else {
849             oleRenewItem.setCode("002");
850             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
851             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
852             if(isSIP2Request){
853                 return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
854             }else{
855                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
856             }
857         }
858         if (!getLoanProcessor().hasCirculationDesk(operatorId)) {
859             oleRenewItem.setCode("001");
860             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
861             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
862             if(isSIP2Request){
863                 return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
864             }else{
865                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
866             }
867         }
868         Map<String, String> loanMap = new HashMap<String, String>();
869         loanMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
870         loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
871         List<OleLoanDocument> loanDocuments = (List<OleLoanDocument>) businessObjectService.findMatching(OleLoanDocument.class, loanMap);
872         if (loanDocuments.size() > 0) {
873             OleLoanDocument oleLoanDocument = loanDocuments.get(0);
874 
875             if (patronDocuments.size() > 0) {
876                 oleLoanDocument.setOlePatron(olePatronDocument);
877                 oleLoanDocument.setBorrowerTypeCode(olePatronDocument.getBorrowerTypeCode());
878                 oleLoanDocument.setBorrowerTypeId(olePatronDocument.getBorrowerType());
879                 oleLoanDocument.setOleBorrowerType(olePatronDocument.getOleBorrowerType());
880                 oleLoanDocument.setBorrowerTypeName(olePatronDocument.getBorrowerTypeName());
881             }
882             oleLoanDocument.setRenewalItemFlag(true);
883             oleLoanDocument.setErrorMessage(null);
884             if (getLoanProcessor().canOverrideLoan(operatorId)) {
885                 if (!getLoanProcessor().checkPendingRequestforItem(oleLoanDocument.getItemUuid())) {
886                     Timestamp currentDate = new Timestamp(System.currentTimeMillis());
887                     try {
888                         oleLoanDocument = getLoanProcessor().addLoan(oleLoanDocument.getPatronBarcode(), oleLoanDocument.getItemId(), oleLoanDocument, operatorId);
889                         if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().trim().isEmpty())) {
890                             oleRenewItem.setCode("003");
891                             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS));
892                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS));
893                             oleRenewItem.setPastDueDate(oleLoanDocument.getPastDueDate().toString());
894                             oleRenewItem.setNewDueDate(oleLoanDocument.getLoanDueDate() != null ? oleLoanDocument.getLoanDueDate().toString() : "");
895                             oleRenewItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
896                             oleRenewItem.setPatronBarcode(olePatronDocument.getBarcode());
897                             oleRenewItem.setItemBarcode(oleLoanDocument.getItemId());
898                             oleRenewItem.setTitleIdentifier(oleLoanDocument.getTitle());
899                             /*oleRenewItem.setFeeType();
900                             oleRenewItem.setFeeAmount();*/
901                             oleRenewItem.setItemProperties("Author="+oleLoanDocument.getAuthor());
902                             oleRenewItem.setMediaType(oleLoanDocument.getItemType());
903                             if(isSIP2Request){
904                                 return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
905                             }else{
906                                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
907                             }
908                         } else {
909                             oleRenewItem.setCode("500");
910                             oleRenewItem.setMessage(oleLoanDocument.getErrorMessage());
911                             LOG.info(oleLoanDocument.getErrorMessage());
912                             if(isSIP2Request){
913                                 return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
914                             }else{
915                                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
916                             }
917 
918                         }
919                     } catch (Exception e) {
920                         LOG.error(e,e);
921                         return "Exception occurred while renewing an item";
922                     }
923 
924                 } else {
925                     oleRenewItem.setCode("009");
926                     oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RQST_PNDNG));
927                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RQST_PNDNG));
928                     if(isSIP2Request){
929                         return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
930                     }else{
931                         return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
932                     }
933                 }
934             } else {
935                 oleRenewItem.setCode("010");
936                 oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_RENEW));
937                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_RENEW));
938                 if(isSIP2Request){
939                     return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
940                 }else{
941                     return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
942                 }
943             }
944         } else {
945             oleRenewItem.setCode("011");
946             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN));
947             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN));
948             if(isSIP2Request){
949                 return  oleRenewItemConverter.generateRenewItemXmlForSIP2(oleRenewItem);
950             }else{
951                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
952             }
953         }
954     }
955 
956 
957     public String renewItemList(String patronBarcode, String operatorId, String itemBarcodeList, boolean isSIP2Request) {
958         LOG.info("Inside Renew Item . Patron Barcode :  " + patronBarcode + "Operator Id : "+ operatorId + " Item Barcode : " +itemBarcodeList);
959         OLERenewItem oleRenewItemPatron = new OLERenewItem();
960         OLERenewItemList oleRenewItemList = new OLERenewItemList();
961         OLERenewItemConverter oleRenewItemConverter = new OLERenewItemConverter();
962         List<OLERenewItem> oleRenewItems = new ArrayList<>();
963         OlePatronDocument olePatronDocument = null;
964         StringBuffer errorMessage = new StringBuffer();
965         Map<String, String> patronMap = new HashMap<String, String>();
966         patronMap.put(OLEConstants.BARCODE, patronBarcode);
967         Long beginLocation = System.currentTimeMillis();
968         List<OlePatronDocument> patronDocuments = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
969         Long endLocation = System.currentTimeMillis();
970         Long timeTakenLocation = endLocation-beginLocation;
971         LOG.info("The Time Taken for Patron call using vufind"+timeTakenLocation);
972         if (patronDocuments.size() > 0) {
973             olePatronDocument = patronDocuments.get(0);
974         } else {
975             oleRenewItems = errorCode002(oleRenewItemPatron, oleRenewItems);
976             oleRenewItemList.setRenewItemList(oleRenewItems);
977             if(isSIP2Request){
978                 return oleRenewItemConverter.generateRenewItemListXmlForSip2(oleRenewItemList);
979             }else{
980 
981                 return oleRenewItemConverter.generateRenewItemListXml(oleRenewItemList);
982             }
983         }
984         if (!getLoanProcessor().hasCirculationDesk(operatorId)) {
985             oleRenewItems = errorCode001(oleRenewItemPatron,oleRenewItems);
986             oleRenewItemList.setRenewItemList(oleRenewItems);
987             if(isSIP2Request){
988                 return oleRenewItemConverter.generateRenewItemListXmlForSip2(oleRenewItemList);
989             }else{
990 
991                 return oleRenewItemConverter.generateRenewItemListXml(oleRenewItemList);
992             }
993         }
994         String[] itemBarcode = itemBarcodeList.split(",");
995         if(itemBarcode != null && itemBarcode.length>1){
996         List renewalItemList = new ArrayList();
997         for(int j=0;j<=itemBarcode.length-1;j++){
998             renewalItemList.add(itemBarcode[j]);
999         }
1000         Long beginItem = System.currentTimeMillis();
1001         List<OleLoanDocument> loanDocuments = (List<OleLoanDocument>) getLoanProcessor().getLoanObjectFromDAOForRenewal(renewalItemList,olePatronDocument.getOlePatronId());
1002         Long endItem = System.currentTimeMillis();
1003         Long timeTakenItem = endItem-beginItem;
1004         LOG.info("The Time Taken to fetch item from loan table using vufind"+timeTakenItem);
1005         if(loanDocuments != null && loanDocuments.size() > 0  ){
1006             for(int j=0;j<=itemBarcode.length-1;j++){
1007                 OLERenewItem oleRenewItem = new OLERenewItem();
1008                 boolean barcodeFlag = true;
1009                 nextItem:{
1010                 for(int i=0;i<loanDocuments.size();i++){
1011                    if(itemBarcode[j].equals(loanDocuments.get(i).getItemId())){
1012                        OleLoanDocument oleLoanDocument = loanDocuments.get(i);
1013                        barcodeFlag = false;
1014                        oleLoanDocument = setPatronInformation(patronDocuments,olePatronDocument,oleLoanDocument);
1015                        if (getLoanProcessor().canOverrideLoan(operatorId)) {
1016                            if (!getLoanProcessor().checkPendingRequestforItem(oleLoanDocument.getItemUuid())) {
1017                                try {
1018                                    oleLoanDocument.setVuFindFlag(true);
1019                                    Long beginItemRenewal = System.currentTimeMillis();
1020                                    oleLoanDocument = getLoanProcessor().addLoan(oleLoanDocument.getPatronBarcode(), oleLoanDocument.getItemId(), oleLoanDocument, operatorId);
1021                                    Long endItemRenewal = System.currentTimeMillis();
1022                                    Long timeTakenRenewal = endItemRenewal-beginItemRenewal;
1023                                    LOG.info("The Time Taken for item renewal using vufind"+timeTakenRenewal);
1024                                    if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().trim().isEmpty())) {
1025                                        oleRenewItem = errorCode003(oleRenewItem,oleLoanDocument,itemBarcode[j]);
1026                                        oleRenewItem = populateRenewItemForSip2(olePatronDocument,oleLoanDocument,oleRenewItem);
1027                                        oleRenewItems.add(oleRenewItem);
1028                                        break nextItem;
1029                                    } else {
1030                                        oleRenewItem = errorCode500(oleRenewItem,oleLoanDocument,itemBarcode[j]);
1031                                        oleRenewItems.add(oleRenewItem);
1032                                        //errorMessage.append(oleRenewItemConverter.generateRenewItemXml(oleRenewItem));
1033                                        break nextItem;
1034                                    }
1035                                } catch (Exception e) {
1036                                    LOG.error(e,e);
1037                                    return "Exception occured while renewing an item";
1038                                }
1039                            } else {
1040                                oleRenewItem = errorCode009(oleRenewItem,itemBarcode[j]);
1041                                oleRenewItems.add(oleRenewItem);
1042                                break nextItem;
1043                            }
1044                        } else {
1045                            oleRenewItem = errorCode010(oleRenewItem,itemBarcode[j]);
1046                            oleRenewItems.add(oleRenewItem);
1047                            break nextItem;
1048                        }
1049                    }
1050                }
1051             }
1052                 if(barcodeFlag){
1053                     oleRenewItem = errorCode011(oleRenewItem, itemBarcode[j]);
1054                     oleRenewItems.add(oleRenewItem);
1055                 }
1056             }
1057          }
1058        } else {
1059 
1060             Map<String, String> loanMap = new HashMap<String, String>();
1061             loanMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
1062             loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode[0]);
1063             List<OleLoanDocument> loanDocuments = (List<OleLoanDocument>) businessObjectService.findMatching(OleLoanDocument.class, loanMap);
1064             OLERenewItem oleRenewItem = new OLERenewItem();
1065             if (loanDocuments.size() > 0) {
1066                 OleLoanDocument oleLoanDocument = loanDocuments.get(0);
1067                 oleLoanDocument = setPatronInformation(patronDocuments,olePatronDocument,oleLoanDocument);
1068                 if (getLoanProcessor().canOverrideLoan(operatorId)) {
1069                     if (!getLoanProcessor().checkPendingRequestforItem(oleLoanDocument.getItemUuid())) {
1070                         try {
1071                             oleLoanDocument.setVuFindFlag(true);
1072                             oleLoanDocument = getLoanProcessor().addLoan(oleLoanDocument.getPatronBarcode(), oleLoanDocument.getItemId(), oleLoanDocument, operatorId);
1073                             if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().trim().isEmpty())) {
1074                                 oleRenewItem = errorCode003(oleRenewItem,oleLoanDocument,itemBarcode[0]);
1075                                 oleRenewItem = populateRenewItemForSip2(olePatronDocument,oleLoanDocument,oleRenewItem);
1076                                 oleRenewItems.add(oleRenewItem);
1077                             } else {
1078                                 oleRenewItem = errorCode500(oleRenewItem, oleLoanDocument, itemBarcode[0]);
1079                                 oleRenewItems.add(oleRenewItem);
1080                             }
1081                         } catch (Exception e) {
1082                             LOG.error(e,e);
1083                             return "Exception occured while renewing an item";
1084                         }
1085 
1086                     } else {
1087                         oleRenewItem = errorCode009(oleRenewItem, itemBarcode[0]);
1088                         oleRenewItems.add(oleRenewItem);
1089                     }
1090                 } else {
1091                     oleRenewItem = errorCode010(oleRenewItem, itemBarcode[0]);
1092                     oleRenewItems.add(oleRenewItem);
1093                 }
1094             } else {
1095                 oleRenewItem = errorCode011(oleRenewItem, itemBarcode[0]);
1096                 oleRenewItems.add(oleRenewItem);
1097             }
1098        }
1099         oleRenewItemList.setRenewItemList(oleRenewItems);
1100         if(isSIP2Request){
1101             return oleRenewItemConverter.generateRenewItemListXmlForSip2(oleRenewItemList);
1102         }else{
1103 
1104             return oleRenewItemConverter.generateRenewItemListXml(oleRenewItemList);
1105         }
1106     }
1107 
1108     private List<OLERenewItem> errorCode001(OLERenewItem oleRenewItemPatron,List<OLERenewItem> oleRenewItems){
1109         oleRenewItemPatron.setCode("001");
1110         oleRenewItemPatron.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
1111         LOG.info(oleRenewItemPatron.getMessage());
1112         oleRenewItems.add(oleRenewItemPatron);
1113         return oleRenewItems;
1114     }
1115 
1116     private List<OLERenewItem> errorCode002(OLERenewItem oleRenewItemPatron,List<OLERenewItem> oleRenewItems){
1117         oleRenewItemPatron.setCode("002");
1118         oleRenewItemPatron.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
1119         LOG.info(oleRenewItemPatron.getMessage());
1120         oleRenewItems.add(oleRenewItemPatron);
1121         return oleRenewItems;
1122     }
1123 
1124     private OLERenewItem errorCode003(OLERenewItem oleRenewItem,OleLoanDocument oleLoanDocument,String itemBarcode){
1125         oleRenewItem.setCode("003");
1126         oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS)+" - Item Barcode("+itemBarcode+")");
1127         LOG.info(oleRenewItem.getMessage());
1128         if(oleLoanDocument.getPastDueDate() != null)
1129             oleRenewItem.setPastDueDate(oleLoanDocument.getPastDueDate().toString());
1130         oleRenewItem.setNewDueDate(oleLoanDocument.getLoanDueDate() != null ? oleLoanDocument.getLoanDueDate().toString() : "");
1131         oleRenewItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
1132         return oleRenewItem;
1133     }
1134 
1135     private OLERenewItem errorCode500(OLERenewItem oleRenewItem,OleLoanDocument oleLoanDocument,String itemBarcode){
1136         oleRenewItem.setCode("500");
1137         oleRenewItem.setMessage(oleLoanDocument.getErrorMessage()+" - Item Barcode("+itemBarcode+")");
1138         LOG.info(oleLoanDocument.getErrorMessage());
1139         return oleRenewItem;
1140     }
1141 
1142     private OLERenewItem errorCode009(OLERenewItem oleRenewItem,String itemBarcode){
1143         oleRenewItem.setCode("009");
1144         oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RQST_PNDNG)+" - Item Barcode("+itemBarcode+")");
1145         LOG.info(oleRenewItem.getMessage());
1146         return oleRenewItem;
1147     }
1148 
1149     private OLERenewItem errorCode010(OLERenewItem oleRenewItem,String itemBarcode){
1150         oleRenewItem.setCode("010");
1151         oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_RENEW)+" - Item Barcode("+itemBarcode+")");
1152         LOG.info(oleRenewItem.getMessage());
1153         return oleRenewItem;
1154     }
1155 
1156     private OLERenewItem errorCode011(OLERenewItem oleRenewItem,String itemBarcode){
1157         oleRenewItem.setCode("011");
1158         oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN) +" - Item Barcode("+itemBarcode+")");
1159         LOG.info(oleRenewItem.getMessage());
1160         return oleRenewItem;
1161     }
1162 
1163 
1164     private OleLoanDocument setPatronInformation(List<OlePatronDocument> patronDocuments,OlePatronDocument olePatronDocument,OleLoanDocument oleLoanDocument){
1165         if (patronDocuments.size() > 0) {
1166             oleLoanDocument.setOlePatron(olePatronDocument);
1167             oleLoanDocument.setBorrowerTypeCode(olePatronDocument.getBorrowerTypeCode());
1168             oleLoanDocument.setBorrowerTypeId(olePatronDocument.getBorrowerType());
1169             oleLoanDocument.setOleBorrowerType(olePatronDocument.getOleBorrowerType());
1170             oleLoanDocument.setBorrowerTypeName(olePatronDocument.getBorrowerTypeName());
1171         }
1172         oleLoanDocument.setRenewalItemFlag(true);
1173         oleLoanDocument.setErrorMessage(null);
1174         return oleLoanDocument;
1175     }
1176 
1177 
1178 
1179 
1180     public boolean validPatron(String patronId) {
1181         boolean valid = false;
1182         Map<String, String> patronMap = new HashMap<String, String>();
1183         patronMap.put(OLEConstants.BARCODE, patronId);
1184         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
1185         if (olePatronDocumentList.size() > 0) {
1186             valid = true;
1187         }
1188         return valid;
1189     }
1190 
1191 
1192     private Map<String, String> getLocationMap(String itemLocation) {
1193         Map<String, String> locationMap = new HashMap<String, String>();
1194         String[] locationArray = itemLocation.split("['/']");
1195         List<String> locationList = Arrays.asList(locationArray);
1196         for (String value : locationList) {
1197             Map<String, String> requestMap = new HashMap<>();
1198             requestMap.put(OLEConstants.LOCATION_CODE, value);
1199             List<OleLocation> oleLocations = (List<OleLocation>) businessObjectService.findMatching(OleLocation.class, requestMap);
1200             if (oleLocations != null && oleLocations.size() > 0) {
1201                 String locationLevelId = oleLocations.get(0).getLevelId();
1202                 requestMap.clear();
1203                 requestMap.put(OLEConstants.LEVEL_ID, locationLevelId);
1204                 List<OleLocationLevel> oleLocationLevels = (List<OleLocationLevel>) businessObjectService.findMatching(OleLocationLevel.class, requestMap);
1205                 if (oleLocationLevels != null && oleLocationLevels.size() > 0) {
1206                     OleLocationLevel oleLocationLevel = new OleLocationLevel();
1207                     oleLocationLevel = oleLocationLevels.get(0);
1208                     if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_CAMPUS)) {
1209                         locationMap.put(OLEConstants.ITEM_CAMPUS, value);
1210                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_INSTITUTION)) {
1211                         locationMap.put(OLEConstants.ITEM_INSTITUTION, value);
1212                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_COLLECTION)) {
1213                         locationMap.put(OLEConstants.ITEM_COLLECTION, value);
1214                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_LIBRARY)) {
1215                         locationMap.put(OLEConstants.ITEM_LIBRARY, value);
1216                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_SHELVING)) {
1217                         locationMap.put(OLEConstants.ITEM_SHELVING, value);
1218                     }
1219                 }
1220             }
1221         }
1222         return locationMap;
1223     }
1224 
1225     /**
1226      * This method is used to convert the date in String format to gregorian calendar format
1227      * @param date
1228      * @return
1229      */
1230     public GregorianCalendar getGregorianCalendarDate(String date) {
1231         if (date != null) {
1232             if(date.equals("")){
1233                 return new GregorianCalendar(2025,1,1);
1234             }
1235 
1236             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
1237             Date parsedDate = null;
1238             try{
1239                 try {
1240                     parsedDate = simpleDateFormat.parse(date);
1241                 } catch (ParseException e) {
1242                     simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
1243                     parsedDate = simpleDateFormat.parse(date);
1244                 }
1245             }catch (ParseException e){
1246                 LOG.info("Exception occured while parsing the date : " + date);
1247             }
1248             Calendar cal = Calendar.getInstance();
1249             cal.setTime(parsedDate);
1250             int year = cal.get(Calendar.YEAR);
1251             int month = cal.get(Calendar.MONTH);
1252             int day = cal.get(Calendar.DAY_OF_MONTH);
1253             return new GregorianCalendar(year, month, day);
1254         }
1255         return null;
1256     }
1257 
1258     public OLERenewItem populateRenewItemForSip2(OlePatronDocument olePatronDocument , OleLoanDocument oleLoanDocument,OLERenewItem oleRenewItem){
1259         oleRenewItem.setPatronBarcode(olePatronDocument.getBarcode());
1260         oleRenewItem.setItemBarcode(oleLoanDocument.getItemId());
1261         oleRenewItem.setTitleIdentifier(oleLoanDocument.getTitle());
1262         /*oleRenewItem.setFeeType();
1263         oleRenewItem.setFeeAmount();*/
1264         oleRenewItem.setItemProperties("Author="+oleLoanDocument.getAuthor());
1265         oleRenewItem.setMediaType(oleLoanDocument.getItemType());
1266         return oleRenewItem;
1267     }
1268 
1269     public AgencyId getAgencyId(InitiationHeader initiationHeader) {
1270         AgencyId agencyId = null;
1271         if (initiationHeader != null && initiationHeader.getFromAgencyId() != null && initiationHeader.getFromAgencyId().getAgencyId() != null
1272                 && StringUtils.isNotBlank(initiationHeader.getFromAgencyId().getAgencyId().getValue())) {
1273             agencyId = new AgencyId(initiationHeader.getFromAgencyId().getAgencyId().getValue());
1274         } else if (initiationHeader != null && initiationHeader.getApplicationProfileType() != null
1275                 && StringUtils.isNotBlank(initiationHeader.getApplicationProfileType().getValue())) {
1276             agencyId = new AgencyId(initiationHeader.getApplicationProfileType().getValue());
1277         } else {
1278             agencyId = new AgencyId(getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
1279         }
1280         return agencyId;
1281     }
1282 
1283     public OlePatronDocument getPatronRecordByBarcode(String patronBarcode) {
1284         Map barcodeMap = new HashMap();
1285         barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronBarcode);
1286         List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
1287         if (CollectionUtils.isNotEmpty(olePatronDocuments)) {
1288             return olePatronDocuments.get(0);
1289         }
1290         return null;
1291     }
1292 
1293     public String getParameter(String name) {
1294         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
1295         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
1296         if(parameter==null){
1297             parameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
1298             parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
1299         }
1300         return parameter!=null?parameter.getValue():null;
1301     }
1302 }
1303 
1304 
1305 
1306