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.OLEParameterConstants;
8   import org.kuali.ole.deliver.bo.*;
9   import org.kuali.ole.deliver.calendar.bo.OleCalendar;
10  import org.kuali.ole.deliver.calendar.service.OleCalendarService;
11  import org.kuali.ole.deliver.calendar.service.impl.OleCalendarServiceImpl;
12  import org.kuali.ole.deliver.processor.LoanProcessor;
13  
14  import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
15  import org.kuali.ole.describe.bo.OleInstanceItemType;
16  import org.kuali.ole.describe.bo.OleLocation;
17  import org.kuali.ole.describe.bo.OleLocationLevel;
18  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
19  import org.kuali.ole.docstore.common.document.*;
20  import org.kuali.ole.docstore.common.document.HoldingsTree;
21  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
22  import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
23  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
24  import org.kuali.ole.docstore.common.document.content.instance.*;
25  import org.kuali.ole.docstore.common.document.content.instance.Item;
26  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
27  
28  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
29  import org.kuali.ole.docstore.common.exception.BibImportResponse;
30  import org.kuali.ole.docstore.common.search.SearchResponse;
31  import org.kuali.ole.docstore.common.search.SearchResult;
32  import org.kuali.ole.docstore.common.search.SearchResultField;
33  
34  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
35  
36  import org.kuali.ole.ncip.bo.*;
37  
38  import org.kuali.ole.ncip.converter.OLECheckInItemConverter;
39  import org.kuali.ole.ncip.converter.OLECheckOutItemConverter;
40  import org.kuali.ole.ncip.converter.OLERenewItemConverter;
41  import org.kuali.ole.ncip.service.OLESIAPIHelperService;
42  import org.kuali.ole.service.OleCirculationPolicyService;
43  import org.kuali.ole.service.OleCirculationPolicyServiceImpl;
44  import org.kuali.ole.sys.context.SpringContext;
45  import org.kuali.ole.util.DocstoreUtil;
46  import org.kuali.rice.core.api.config.property.ConfigContext;
47  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
48  import org.kuali.rice.kim.api.permission.PermissionService;
49  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
50  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
51  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
52  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
53  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
54  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
55  import org.kuali.rice.krad.service.BusinessObjectService;
56  import org.kuali.ole.docstore.common.document.content.instance.Location;
57  import org.kuali.rice.krad.service.KRADServiceLocator;
58  import org.kuali.rice.krad.util.GlobalVariables;
59  import org.kuali.rice.krms.api.engine.EngineResults;
60  import org.kuali.rice.krms.api.engine.ResultEvent;
61  import org.kuali.rice.krms.framework.engine.BasicRule;
62  
63  
64  import java.math.BigDecimal;
65  import java.sql.Timestamp;
66  import java.text.DateFormat;
67  import java.text.ParseException;
68  import java.text.SimpleDateFormat;
69  import java.util.*;
70  
71  /**
72   * Created with IntelliJ IDEA.
73   * User: maheswarang
74   * Date: 7/21/13
75   * Time: 3:47 PM
76   * To change this template use File | Settings | File Templates.
77   */
78  public class OLECirculationHelperServiceImpl {
79      private static final Logger LOG = Logger.getLogger(OLECirculationHelperServiceImpl.class);
80      private static final String DOCSTORE_URL = "docstore.url";
81      private final String CREATE_NEW_DOCSTORE_RECORD_QUERY_STRING = "docAction=ingestContent&stringContent=";
82      private BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
83      private LoanProcessor loanProcessor = new LoanProcessor();
84      private OLECheckInItemConverter oleCheckInItemConverter = new OLECheckInItemConverter();
85      private OLECheckOutItemConverter oleCheckOutItemConverter = new OLECheckOutItemConverter();
86      private OLESIAPIHelperService oleSIAPIHelperService;
87      private OleCirculationPolicyService oleCirculationPolicyService = getOleCirculationPolicyService();
88      private DocstoreClientLocator docstoreClientLocator;
89      public OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
90      DocstoreUtil docstoreUtil = new DocstoreUtil();
91  
92      public DocstoreClientLocator getDocstoreClientLocator() {
93  
94          if (docstoreClientLocator == null) {
95              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
96  
97          }
98          return docstoreClientLocator;
99      }
100 
101 
102     public OleCirculationPolicyService getOleCirculationPolicyService() {
103         if (null == oleCirculationPolicyService) {
104             oleCirculationPolicyService = new OleCirculationPolicyServiceImpl();
105         }
106         return oleCirculationPolicyService;
107     }
108 
109     public BusinessObjectService getBusinessObjectService() {
110         return businessObjectService;
111     }
112 
113     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
114         this.businessObjectService = businessObjectService;
115     }
116 
117     public OLESIAPIHelperService getOleSIAPIHelperService() {
118         if (oleSIAPIHelperService == null) {
119             oleSIAPIHelperService = SpringContext.getBean(OLESIAPIHelperService.class);
120         }
121         return oleSIAPIHelperService;
122     }
123 
124     public void setOleSIAPIHelperService(OLESIAPIHelperService oleSIAPIHelperService) {
125         this.oleSIAPIHelperService = oleSIAPIHelperService;
126     }
127 
128     public OLELookupUser initialiseLookupUser(OlePatronDocument olePatronDocument, String agencyId) {
129         OLELookupUser oleLookupUser = new OLELookupUser();
130         oleLookupUser.setPatronId(olePatronDocument.getBarcode());
131         OlePatronEmailBo olePatronEmailBo = getDefaultEmailBo(olePatronDocument.getOlePatronId());
132         if (olePatronEmailBo != null) {
133             oleLookupUser.setPatronEmail(olePatronEmailBo);
134         }
135         OlePatronAddressBo olePatronAddressBo = getDefaultAddressBo(olePatronDocument.getOlePatronId());
136         if (olePatronAddressBo != null) {
137             oleLookupUser.setPatronAddress(olePatronAddressBo);
138         }
139         OlePatronPhoneBo olePatronPhoneBo = getDefaultPhoneBo(olePatronDocument.getOlePatronId());
140         if (olePatronPhoneBo != null) {
141             oleLookupUser.setPatronPhone(olePatronPhoneBo);
142         }
143         if (getPatronPrivilege(olePatronDocument, agencyId) != null) {
144             oleLookupUser.setOleUserPrivileges(getPatronPrivilege(olePatronDocument, agencyId));
145         }
146         OlePatronNameBo olePatronNameBo = getEntityNameBo(olePatronDocument.getOlePatronId());
147         if (olePatronNameBo != null) {
148             oleLookupUser.setPatronName(olePatronNameBo);
149         }
150         return oleLookupUser;
151     }
152 
153     public EntityBo getEntity(String entityId) {
154         LOG.info("Inside getEntity : Entity Id : " + entityId);
155         Map<String, String> entityMap = new HashMap<>();
156         entityMap.put("id", entityId);
157         List<EntityBo> entityBoList = (List<EntityBo>) businessObjectService.findMatching(EntityBo.class, entityMap);
158         if (entityBoList.size() > 0)
159             return entityBoList.get(0);
160         return null;
161     }
162 
163     public OlePatronEmailBo getDefaultEmailBo(String entityId) {
164         LOG.info("Inside getDefaultEmailBo : Entity Id : " + entityId);
165         EntityBo entityBo = getEntity(entityId);
166         OlePatronEmailBo olePatronEmailBo = null;
167         if (entityBo != null) {
168             if (entityBo.getEntityTypeContactInfos() != null && entityBo.getEntityTypeContactInfos().size() > 0)
169                 if (entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses() != null && entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses().size() > 0) {
170                     for (EntityEmailBo entityEmailBo : entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses()) {
171                         if (entityEmailBo.getDefaultValue()) {
172                             olePatronEmailBo = new OlePatronEmailBo();
173                             olePatronEmailBo.setEmailTypeCode(entityEmailBo.getEmailTypeCode());
174                             olePatronEmailBo.setEmailAddress(entityEmailBo.getEmailAddress());
175                             return olePatronEmailBo;
176                         }
177                     }
178                 }
179         }
180         return null;
181     }
182 
183     public OlePatronAddressBo getDefaultAddressBo(String entityId) {
184         LOG.info("Inside getDefaultAddressBo : Entity Id : " + entityId);
185         EntityBo entityBo = getEntity(entityId);
186         EntityAddressBo entityAddressBo = null;
187         OlePatronAddressBo olePatronAddressBo = null;
188         if (entityBo != null) {
189             if (entityBo.getEntityTypeContactInfos() != null && entityBo.getEntityTypeContactInfos().size() > 0)
190                 if (entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress() != null) {
191                     entityAddressBo = entityBo.getEntityTypeContactInfos().get(0).getDefaultAddress();
192                     olePatronAddressBo = new OlePatronAddressBo();
193                     olePatronAddressBo.setAddressTypeCode(entityAddressBo.getAddressTypeCode());
194                     olePatronAddressBo.setCity(entityAddressBo.getCity());
195                     olePatronAddressBo.setCountryCode(entityAddressBo.getCountryCode());
196                     olePatronAddressBo.setLine1(entityAddressBo.getLine1());
197                     olePatronAddressBo.setLine2(entityAddressBo.getLine2());
198                     olePatronAddressBo.setLine3(entityAddressBo.getLine3());
199                     olePatronAddressBo.setPostalCode(entityAddressBo.getPostalCode());
200                     olePatronAddressBo.setStateProvinceCode(entityAddressBo.getStateProvinceCode());
201                     return olePatronAddressBo;
202                 }
203         }
204         return null;
205     }
206 
207     public OlePatronNameBo getEntityNameBo(String entityId) {
208         LOG.info("Inside getEntityNameBo : Entity Id : " + entityId);
209         EntityBo entityBo = getEntity(entityId);
210         EntityNameBo entityNameBo = null;
211         OlePatronNameBo olePatronNameBo = null;
212         if (entityBo != null) {
213             if (entityBo.getNames() != null && entityBo.getNames().size() > 0) {
214                 entityNameBo = entityBo.getNames().get(0);
215                 olePatronNameBo = new OlePatronNameBo();
216                 olePatronNameBo.setFirstName(entityNameBo.getFirstName());
217                 olePatronNameBo.setMiddleName(entityNameBo.getMiddleName());
218                 olePatronNameBo.setLastName(entityNameBo.getLastName());
219                 return olePatronNameBo;
220             }
221         }
222         return null;
223     }
224 
225     public OlePatronPhoneBo getDefaultPhoneBo(String entityId) {
226         LOG.info("Inside getDefaultPhoneBo : Entity Id : " + entityId);
227         EntityBo entityBo = getEntity(entityId);
228         EntityPhoneBo entityPhoneBo = null;
229         OlePatronPhoneBo olePatronPhoneBo = null;
230         if (entityBo != null) {
231             if (entityBo.getEntityTypeContactInfos().get(0) != null && entityBo.getEntityTypeContactInfos().size() > 0) {
232                 if (entityBo.getEntityTypeContactInfos().get(0).getDefaultPhoneNumber() != null) {
233                     entityPhoneBo = entityBo.getEntityTypeContactInfos().get(0).getDefaultPhoneNumber();
234                     olePatronPhoneBo = new OlePatronPhoneBo();
235                     olePatronPhoneBo.setPhoneTypeCode(entityPhoneBo.getPhoneTypeCode());
236                     olePatronPhoneBo.setPhoneNumber(entityPhoneBo.getPhoneNumber());
237                     return olePatronPhoneBo;
238                 }
239             }
240         }
241         return null;
242     }
243 
244     public List<OLEUserPrivilege> getPatronPrivilege(OlePatronDocument olePatronDocument, String agencyId) {
245         List<OLEUserPrivilege> userPrivilegeList;
246         if (olePatronDocument != null) {
247             userPrivilegeList = new ArrayList<OLEUserPrivilege>();
248             OLEUserPrivilege courtesyPrivilege = new OLEUserPrivilege();
249             courtesyPrivilege.setUserPrivilegeType(OLEConstants.COURTESY_NOTICE);
250             courtesyPrivilege.setUserPrivilegeDescription(OLEConstants.COURTESY_DESCRIPTION);
251             if (olePatronDocument.isCourtesyNotice()) {
252                 courtesyPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
253             } else {
254                 courtesyPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
255             }
256             userPrivilegeList.add(courtesyPrivilege);
257             OLEUserPrivilege deliverPrivilege = new OLEUserPrivilege();
258             deliverPrivilege.setUserPrivilegeType(OLEConstants.DELIVERY);
259             deliverPrivilege.setUserPrivilegeDescription(OLEConstants.DELIVERY_DESCRIPTION);
260             if (olePatronDocument.isDeliveryPrivilege()){
261                 deliverPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
262             }else{
263             deliverPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
264             }
265                 userPrivilegeList.add(deliverPrivilege);
266             OLEUserPrivilege pagingPrivilege = new OLEUserPrivilege();
267             pagingPrivilege.setUserPrivilegeType(OLEConstants.PAGING);
268             pagingPrivilege.setUserPrivilegeDescription(OLEConstants.PAGING_DESCRIPTION);
269             if (olePatronDocument.isPagingPrivilege()) {
270                 pagingPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.TRUE));
271             }else{
272             pagingPrivilege.setUserPrivilegeStatus(String.valueOf(Boolean.FALSE));
273             }
274             userPrivilegeList.add(pagingPrivilege);
275             String borrowerTypeCode = olePatronDocument.getBorrowerTypeCode();
276             Map<String, String> borrowerTypeMap = new HashMap<String, String>();
277             borrowerTypeMap.put(OLEConstants.BORROWER_TYPE_CODE, borrowerTypeCode);
278             List<OleBorrowerType> oleBorrowerTypeList = (List<OleBorrowerType>) businessObjectService.findMatching(OleBorrowerType.class, borrowerTypeMap);
279             if (oleBorrowerTypeList.size() > 0) {
280                 OLEUserPrivilege oleProfilePrivilege = new OLEUserPrivilege();
281                 oleProfilePrivilege.setUserPrivilegeType(OLEConstants.PROFILE);
282                 oleProfilePrivilege.setUserPrivilegeStatus(oleBorrowerTypeList.get(0).getBorrowerTypeName());
283                 oleProfilePrivilege.setUserPrivilegeDescription(OLEConstants.PROFILE_DESCRIPTION);
284                 userPrivilegeList.add(oleProfilePrivilege);
285             }
286             if (agencyId != null) {
287                 String itemType, itemLocation = "";
288                 HashMap<String, String> agencyPropertyMap = getAgencyPropertyMap(agencyId);
289                 itemType = agencyPropertyMap.get(OLEConstants.ITEM_TYPE);
290                 itemLocation = agencyPropertyMap.get(OLEConstants.ITEM_LOCATION);
291                 OLEUserPrivilege oleUserPrivilege = new OLEUserPrivilege();
292                 oleUserPrivilege.setUserPrivilegeType(OLEConstants.STATUS);
293                 oleUserPrivilege.setUserPrivilegeDescription(OLEConstants.STATUS_DESCRIPTION);
294                 oleUserPrivilege.setUserPrivilegeStatus(OLEConstants.OK);
295                 if (olePatronDocument.isGeneralBlock() || isPatronExpired(olePatronDocument) || !olePatronDocument.isActiveIndicator() || isPatronActivated(olePatronDocument) || !isAbleToCheckOut(olePatronDocument.getOlePatronId(), borrowerTypeCode, itemType, itemLocation))
296                     oleUserPrivilege.setUserPrivilegeStatus(OLEConstants.BLOCKED);
297                 userPrivilegeList.add(oleUserPrivilege);
298             }
299             return userPrivilegeList;
300         }
301         return null;
302     }
303 
304     public boolean isPatronExpired(OlePatronDocument olePatronDocument) {
305 
306         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
307         Date expirationDate = olePatronDocument.getExpirationDate();
308         if (expirationDate != null) {
309             if ((fmt.format(expirationDate)).compareTo(fmt.format(new Date(System.currentTimeMillis()))) > 0) {
310                 return false;
311             } else {
312                 return true;
313             }
314         }else{
315             return false;
316         }
317     }
318 
319     public boolean isPatronActivated(OlePatronDocument olePatronDocument) {
320 
321         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OleDeliverRequest.DATE_FORMAT);
322         Date activationDate = olePatronDocument.getActivationDate();
323         if (activationDate != null) {
324             if ((fmt.format(activationDate)).compareTo(fmt.format(new Date(System.currentTimeMillis()))) <= 0) {
325                 return false;
326             } else {
327                 return true;
328             }
329         } else {
330             return false;
331         }
332     }
333 
334     public boolean isAbleToCheckOut(String patronId, String borrowerType, String itemType, String itemLocation) {
335         LOG.info("Inside isAbleToCheckOut method . Patron Id : " +patronId + " Borrower Type : " + borrowerType + " ItemType : "+itemType + " Item Location : "  + itemLocation);
336         boolean allowed = true;
337         String agendaName = OLEConstants.CHECK_OUT_AGENDA_NM;
338         HashMap<String, Object> termValues = new HashMap<String, Object>();
339         List<FeeType> feeTypeList = oleCirculationPolicyService.getPatronBillPayment(patronId);
340         Integer overdueFineAmt = 0;
341         Integer replacementFeeAmt = 0;
342         for (FeeType feeType : feeTypeList) {
343             overdueFineAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.OVERDUE_FINE) ? feeType.getFeeAmount().intValue() : 0;
344             replacementFeeAmt += feeType.getOleFeeType().getFeeTypeName().equalsIgnoreCase(OLEConstants.REPLACEMENT_FEE) ? feeType.getFeeAmount().intValue() : 0;
345         }
346         String[] locationArray = itemLocation.split("['/']");
347         List<String> locationList = Arrays.asList(locationArray);
348         for (String value : locationList) {
349             Map<String, String> requestMap = new HashMap<>();
350             requestMap.put(OLEConstants.LOCATION_CODE, value);
351             List<OleLocation> oleLocations = (List<OleLocation>) businessObjectService.findMatching(OleLocation.class, requestMap);
352             if (oleLocations != null && oleLocations.size() > 0) {
353                 String locationLevelId = oleLocations.get(0).getLevelId();
354                 requestMap.clear();
355                 requestMap.put(OLEConstants.LEVEL_ID, locationLevelId);
356                 List<OleLocationLevel> oleLocationLevels = (List<OleLocationLevel>) businessObjectService.findMatching(OleLocationLevel.class, requestMap);
357                 if (oleLocationLevels != null && oleLocationLevels.size() > 0) {
358                     OleLocationLevel oleLocationLevel = new OleLocationLevel();
359                     oleLocationLevel = oleLocationLevels.get(0);
360                     if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_CAMPUS)) {
361                         termValues.put(OLEConstants.ITEM_CAMPUS, value);
362                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_INSTITUTION)) {
363                         termValues.put(OLEConstants.ITEM_INSTITUTION, value);
364                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_COLLECTION)) {
365                         termValues.put(OLEConstants.ITEM_COLLECTION, value);
366                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_LIBRARY)) {
367                         termValues.put(OLEConstants.ITEM_LIBRARY, value);
368                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_SHELVING)) {
369                         termValues.put(OLEConstants.ITEM_SHELVING, value);
370                     }
371                 }
372             }
373         }
374         termValues.put(OLEConstants.BORROWER_TYPE, borrowerType);
375         termValues.put(OLEConstants.ITEM_TYPE, itemType);
376         //termValues.put(OLEConstants.ITEM_SHELVING, itemLocation);
377         termValues.put(OLEConstants.OVERDUE_FINE_AMT, overdueFineAmt);
378         termValues.put(OLEConstants.REPLACEMENT_FEE_AMT, replacementFeeAmt);
379         termValues.put(OLEConstants.ALL_CHARGES, overdueFineAmt + replacementFeeAmt);
380         /*termValues.put(OLEConstants.ITEM_STATUS, "AVAILABLE");
381         termValues.put("isCirculationPolicyNotFound","false");*/
382         DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
383         String itemId = "";
384         dataCarrierService.removeData(patronId+itemId);
385         dataCarrierService.addData(OLEConstants.GROUP_ID, "100");
386         List<Integer> listOfOverDueDays = oleCirculationPolicyService.getNumberOfOverdueDays(patronId);
387         dataCarrierService.addData(OLEConstants.LIST_OVERDUE_DAYS, listOfOverDueDays);
388         HashMap overdueItems = new HashMap();
389         HashMap recallOverdueItem = new HashMap();
390         overdueItems = oleCirculationPolicyService.getNumberOfOverdueItemsCheckedOut(patronId);
391         recallOverdueItem = oleCirculationPolicyService.getRecalledOverdueItemsCheckedOut((List<OleLoanDocument>) overdueItems.get(OLEConstants.OLE_LOAN_DOCUMENT_LIST));
392         dataCarrierService.addData(OLEConstants.LIST_RECALLED_OVERDUE_DAYS, (List<Integer>) recallOverdueItem.get(OLEConstants.LIST_RECALLED_OVERDUE_DAYS));
393         termValues.put(OLEConstants.PATRON_ID_POLICY, patronId);
394         termValues.put(OLEConstants.ITEM_ID_POLICY, "");
395         try {
396             EngineResults engineResults = loanProcessor.getEngineResults(agendaName, termValues);
397             dataCarrierService.removeData(patronId+itemId);
398             List<ResultEvent> allResults = engineResults.getAllResults();
399             for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
400                 ResultEvent resultEvent = resultEventIterator.next();
401                 if (resultEvent.getSource() instanceof BasicRule) {
402                     BasicRule basicRule = (BasicRule) resultEvent.getSource();
403                     if (resultEvent.getType().equals(OLEConstants.RULE_EVALUATED) && ((basicRule.getName().equals(OLENCIPConstants.CHECK_REPLACEMENT_FEE_AMT) && resultEvent.getResult())
404                             || (basicRule.getName().equals(OLENCIPConstants.CHECK_ALL_OVERDUE_FINE_AMT) && resultEvent.getResult()) || (basicRule.getName().equals(OLENCIPConstants.CHECK_OVERALL_CHARGES) && resultEvent.getResult()))) {
405                         // renewalExceeds=true;  Check all Overdue fine amount
406                         allowed = false;
407                         break;
408                     }
409                 }
410             }
411             List<String> errorMessage = (List<String>) engineResults.getAttribute(OLEConstants.ERROR_ACTION);
412             if (errorMessage != null && errorMessage.size() > 0) {
413                 allowed = false;
414             }
415         } catch (Exception e) {
416             LOG.info("Exception Occured while evaluating the KRMS rules");
417             LOG.error(e, e);
418         }
419 
420         return allowed;
421     }
422 
423     public String checkOutItem(String patronId, String operatorId, String itemBarcode) {
424         LOG.info("In  Check Out Item . Patron Barcode : "+patronId + " OperatorId : " +operatorId + "Item Barcode : "+itemBarcode);
425         OlePatronDocument olePatronDocument = null;
426         Map<String, String> patronMap = new HashMap<String, String>();
427         patronMap.put(OLEConstants.BARCODE, patronId);
428         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
429         if (olePatronDocumentList.size() > 0) {
430             olePatronDocument = olePatronDocumentList.get(0);
431         } else {
432             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
433             oleCheckOutItem.setCode("002");
434             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
435             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
436             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
437         }
438         OleLoanDocument oleLoanDocument;
439         try {
440             oleLoanDocument = loanProcessor.getLoanDocument(olePatronDocument.getBarcode(), null, true, false);
441             oleLoanDocument.setLoanOperatorId(operatorId);
442             Map<String, String> circulationDeskDetailMaps = new HashMap<String, String>();
443             circulationDeskDetailMaps.put(OLENCIPConstants.OPERATOR_ID, operatorId);
444             List<OleCirculationDeskDetail> oleCirculationDeskDetailLists = (List<OleCirculationDeskDetail>) businessObjectService.findMatching(OleCirculationDeskDetail.class, circulationDeskDetailMaps);
445             if (oleCirculationDeskDetailLists != null && oleCirculationDeskDetailLists.size() > 0) {
446                 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailLists) {
447                     if (oleCirculationDeskDetail.isDefaultLocation()) {
448                         String circulationDeskId = oleCirculationDeskDetail.getCirculationDeskId();
449                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
450                         Map<String, String> circulationMap = new HashMap<String, String>();
451                         circulationMap.put(OLEConstants.CIRCULATION_DESK_ID, circulationDeskId);
452                         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) businessObjectService.findMatching(OleCirculationDesk.class, circulationMap);
453                         if (oleCirculationDeskList.size() > 0)
454                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
455                         break;
456                     }
457                 }
458             } else {
459                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
460                 oleCheckOutItem.setCode("026");
461                 oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
462                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CIRCULATION_DESK_NOT_MAPPED_OPERATOR));
463                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
464             }
465             if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
466                 if (olePatronDocument != null) {
467                     oleLoanDocument.setLoanOperatorId(operatorId);
468                     oleLoanDocument = loanProcessor.addLoan(olePatronDocument.getBarcode(), itemBarcode, oleLoanDocument, loanProcessor.getPatronLoanedItem(oleLoanDocument.getPatronId()), operatorId);
469                     if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
470                         OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
471                         oleCheckOutItem.setDueDate(oleLoanDocument.getLoanDueDate() != null ? oleLoanDocument.getLoanDueDate().toString() : "");
472                         oleCheckOutItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
473                         oleCheckOutItem.setUserType(oleLoanDocument.getBorrowerTypeName());
474                         oleCheckOutItem.setBarcode(oleLoanDocument.getItemId());
475                         oleCheckOutItem.setPatronId(oleLoanDocument.getPatronId());
476                         oleCheckOutItem.setPatronBarcode(patronId);
477                         if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemType() != null) {
478                             oleCheckOutItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
479                         }
480                         oleCheckOutItem.setCode("030");
481                         oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED));
482                         LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED));
483                         return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
484 
485                     } else {
486                         if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getLocation() == null) {
487                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
488                             oleCheckOutItem.setCode("028");
489                             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC));
490                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVAL_LOC));
491                             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
492                         } else if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemStatus() != null &&
493                                 oleLoanDocument.getOleItem().getItemStatus().getCodeValue() != null && oleLoanDocument.getOleItem().getItemStatus().getCodeValue().equalsIgnoreCase("LOANED")) {
494 
495                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
496                             oleCheckOutItem.setCode("100");
497                             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_IN_LOAN));
498                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_IN_LOAN));
499                             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
500                         } else {
501                             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
502                             oleCheckOutItem.setCode("500");
503                             oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
504                             LOG.info(oleLoanDocument.getErrorMessage());
505                             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
506                         }
507                     }
508                 } else {
509                     OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
510                     oleCheckOutItem.setCode("500");
511                     oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
512                     LOG.info(oleLoanDocument.getErrorMessage());
513                     return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
514                 }
515             } else {
516                 OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
517                 oleCheckOutItem.setCode("500");
518                 oleCheckOutItem.setMessage(oleLoanDocument.getErrorMessage());
519                 LOG.info(oleLoanDocument.getErrorMessage());
520                 return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
521             }
522         } catch (Exception e) {
523             OLECheckOutItem oleCheckOutItem = new OLECheckOutItem();
524             oleCheckOutItem.setCode("014");
525             oleCheckOutItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
526             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
527             LOG.error(e,e);
528             return oleCheckOutItemConverter.generateCheckOutItemXml(oleCheckOutItem);
529         }
530     }
531 
532     public String checkInItem(String patronBarcode, String operatorId, String itemBarcode, String deleteIndicator) {
533         LOG.info("Inside checkInItem method .Patron barcode : " + patronBarcode + " Operator Id : " +operatorId + " Item Barcode : " + itemBarcode );
534 
535         OleLoanDocument oleLoanDocument = new OleLoanDocument();
536         OLECheckInItem oleCheckInItem = new OLECheckInItem();
537         try {
538             // oleLoanDocument= loanProcessor.returnLoan(oleLoanDocument);
539 
540             Map<String, String> loanMap = new HashMap<String, String>();
541             loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
542             List<OleLoanDocument> oleLoanList = (List<OleLoanDocument>) businessObjectService.findMatching(OleLoanDocument.class, loanMap);
543             if (oleLoanList.size() > 0) {
544                 oleLoanDocument = oleLoanList.get(0);
545                 String olePatronId = oleLoanList.get(0).getPatronId();
546                 Map<String, String> patronMap = new HashMap<String, String>();
547                 patronMap.put(OLEConstants.OleDeliverRequest.PATRON_ID, olePatronId);
548                 List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
549                 if (olePatronDocumentList.size() > 0) {
550                     oleLoanDocument.setOlePatron(olePatronDocumentList.get(0));
551                     oleLoanDocument.setPatronBarcode(olePatronDocumentList.get(0).getBarcode());
552                     if(olePatronDocumentList.get(0).getOleBorrowerType()!=null){
553                     oleLoanDocument.setBorrowerTypeCode(olePatronDocumentList.get(0).getOleBorrowerType().getBorrowerTypeCode());
554                     oleLoanDocument.setBorrowerTypeName(olePatronDocumentList.get(0).getOleBorrowerType().getBorrowerTypeName());
555                     }
556                 }
557             }
558             Map<String, String> circulationDeskDetailMap = new HashMap<String, String>();
559             circulationDeskDetailMap.put(OLENCIPConstants.OPERATOR_ID, operatorId);
560             List<OleCirculationDeskDetail> oleCirculationDeskDetailList = (List<OleCirculationDeskDetail>) businessObjectService.findMatching(OleCirculationDeskDetail.class, circulationDeskDetailMap);
561             if (oleCirculationDeskDetailList != null && oleCirculationDeskDetailList.size() > 0) {
562                 for (OleCirculationDeskDetail oleCirculationDeskDetail : oleCirculationDeskDetailList) {
563                     if (oleCirculationDeskDetail.isDefaultLocation()) {
564                         String circulationDeskId = oleCirculationDeskDetail.getCirculationDeskId();
565                         oleLoanDocument.setCirculationLocationId(circulationDeskId);
566                         Map<String, String> circulationMap = new HashMap<String, String>();
567                         circulationMap.put(OLEConstants.CIRCULATION_DESK_ID, circulationDeskId);
568                         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) businessObjectService.findMatching(OleCirculationDesk.class, circulationMap);
569                         if (oleCirculationDeskList.size() > 0)
570                             oleLoanDocument.setOleCirculationDesk(oleCirculationDeskList.get(0));
571                         break;
572                     }
573                 }
574                 oleLoanDocument = loanProcessor.returnLoan(itemBarcode, oleLoanDocument);
575                 oleCheckInItem.setAuthor(oleLoanDocument.getAuthor());
576                 oleCheckInItem.setTitle(oleLoanDocument.getTitle());
577                 oleCheckInItem.setCallNumber(oleLoanDocument.getItemCallNumber());
578                 oleCheckInItem.setBarcode(oleLoanDocument.getPatronBarcode());
579                 oleCheckInItem.setUserId(oleLoanDocument.getPatronBarcode());
580                 oleCheckInItem.setUserType(oleLoanDocument.getBorrowerTypeName());
581                 if (oleLoanDocument.getOleItem() != null && oleLoanDocument.getOleItem().getItemType() != null) {
582                     oleCheckInItem.setItemType(oleLoanDocument.getOleItem().getItemType().getCodeValue());
583                 }
584                 if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().isEmpty())) {
585                     oleCheckInItem.setCode("024");
586                     oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN));
587                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN));
588                     if (deleteIndicator.equalsIgnoreCase("y")) {
589 
590                         org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(oleLoanDocument.getItemUuid());
591                         String bibId = item.getHolding().getBib().getId();
592 
593                         getDocstoreClientLocator().getDocstoreClient().deleteBib(bibId);
594                     }
595                     return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
596                 } else {
597                     oleCheckInItem.setCode("500");
598                     oleCheckInItem.setMessage(oleLoanDocument.getErrorMessage());
599                     LOG.info(oleLoanDocument.getErrorMessage());
600                     return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
601                 }
602             } else {
603                 oleCheckInItem.setCode("025");
604                 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
605                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
606                 return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
607             }
608         } catch (Exception e) {
609             if(e.getMessage()!=null && e.getMessage().equals("")){
610                 oleCheckInItem.setCode("014");
611                 oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
612                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITEM_BARCODE_DOESNOT_EXISTS));
613                 LOG.error(e,e);
614                 return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
615             }
616 
617             oleCheckInItem.setCode("025");
618             oleCheckInItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
619             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.CHECK_IN_FAILED));
620             return oleCheckInItemConverter.generateCheckInItemXml(oleCheckInItem);
621         }
622     }
623 
624     public String acceptItem(String patronBarcode, String operatorId, String itemBarcode, String callNumber, String title, String author, String itemType, String itemLocation, String dateExpires, String requestType, String pickUpLocation) throws Exception {
625         LOG.info("Inside Accept Item . Patron Barcode " + patronBarcode + "Operator Id : " +operatorId + "Item barcode :" + itemBarcode + " Call Number : "+callNumber + "Title : "+title + " Author : " +author + "Item Type : "+ itemType + "Item Location : "+itemLocation + "Request Type :" + requestType + "Pick up Location : " + pickUpLocation );
626         OLEAcceptItem oleAcceptItem = new OLEAcceptItem();
627         String itemIdentifier = null;
628         if (docstoreUtil.isItemAvailableInDocStore(itemBarcode)) {
629             return itemIdentifier;
630         }
631         BibMarcRecord bibMarcRecord = loanProcessor.getBibMarcRecord(title, author);
632 
633         List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
634         bibMarcRecordList.add(bibMarcRecord);
635 
636         BibMarcRecords bibMarcRecords = new BibMarcRecords();
637         bibMarcRecords.setRecords(bibMarcRecordList);
638         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
639 
640 
641         Bib bib = new BibMarc();
642         bib.setStaffOnly(true);
643         bib.setCategory(org.kuali.ole.docstore.common.document.content.enums.DocCategory.WORK.getCode());
644         bib.setType(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode());
645         bib.setFormat(org.kuali.ole.docstore.common.document.content.enums.DocFormat.MARC.getCode());
646         bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
647 
648 
649         OleHoldings oleHoldings = new OleHoldings();
650         LocationLevel locationLevel = new LocationLevel();
651         locationLevel = loanProcessor.createLocationLevel(itemLocation, locationLevel);
652         Location holdingsLocation = new Location();
653         holdingsLocation.setPrimary(OLEConstants.TRUE);
654         holdingsLocation.setStatus(OLEConstants.PERMANENT);
655         holdingsLocation.setLocationLevel(locationLevel);
656         oleHoldings.setLocation(holdingsLocation);
657         oleHoldings.setStaffOnlyFlag(true);
658         Item item = new Item();
659         AccessInformation accessInformation = new AccessInformation();
660         accessInformation.setBarcode(itemBarcode);
661         item.setAccessInformation(accessInformation);
662         item.setStaffOnlyFlag(true);
663         ItemStatus itemStatus = new ItemStatus();
664         itemStatus.setCodeValue(OLEConstants.AVAILABLE);
665         item.setItemStatus(itemStatus);
666         ItemType type = new ItemType();
667         type.setCodeValue(itemType);
668         item.setItemType(type);
669         CallNumber itemCallNumber = new CallNumber();
670         itemCallNumber.setNumber(callNumber);
671         item.setCallNumber(itemCallNumber);
672         ShelvingScheme shelvingScheme = new ShelvingScheme();
673         shelvingScheme.setCodeValue(OLEConstants.LCC);
674         itemCallNumber.setShelvingScheme(shelvingScheme);
675         //item.setExtension(extension);
676         item.setLocation(holdingsLocation);
677         ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
678         org.kuali.ole.docstore.common.document.Item itemXml = new ItemOleml();
679         itemXml.setContent(itemOlemlRecordProcessor.toXML(item));
680         itemXml.setStaffOnly(true);
681         Holdings holdings = new PHoldings();
682         holdings.setStaffOnly(true);
683         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
684         holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
685         HoldingsTree holdingsTree = new HoldingsTree();
686         holdingsTree.setHoldings(holdings);
687         holdingsTree.getItems().add(itemXml);
688         BibTree bibTree = new BibTree();
689         bibTree.setBib(bib);
690         bibTree.getHoldingsTrees().add(holdingsTree);
691         BibTrees bibTrees = new BibTrees();
692         bibTrees.getBibTrees().add(bibTree);
693         BibImportResponse bibImportResponse = getDocstoreClientLocator().getDocstoreClient().createBibTrees(bibTrees);
694         Thread.sleep(200);
695         if(bibImportResponse!=null && bibImportResponse.getBibImportResults()!=null && bibImportResponse.getBibImportResults().get(0)!=null && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord()!=null
696                 && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId()!=null && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds()!=null
697                 && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds().size()>0 && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds().get(0)!=null
698                 && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds().get(0).getItems()!=null && bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds().get(0).getItems().get(0)!=null){
699             itemIdentifier= bibImportResponse.getBibImportResults().get(0).getOrderBibMarcRecord().getBibId().getHoldingsIds().get(0).getItems().get(0);
700         }else{
701             itemIdentifier="";
702         }
703         LOG.info("Item Created with identifier : " + itemIdentifier);
704         return itemIdentifier;
705     }
706 
707     public HashMap<String, String> getAgencyPropertyMap(String agencyId) {
708         HashMap<String, String> agencyPropertyMap = new HashMap<String, String>();
709         agencyPropertyMap = getOleSIAPIHelperService().getAgencyPropertyMap(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLENCIPConstants.NCIPAPI_PARAMETER_NAME, agencyId, agencyPropertyMap);
710         return agencyPropertyMap;
711     }
712 
713     public String renewItem(String patronBarcode, String operatorId, String itemBarcode) {
714         LOG.info("Inside Renew Item . Patron Barcode :  " + patronBarcode + "Operator Id : "+ operatorId + " Item Barcode : " +itemBarcode);
715         OLERenewItem oleRenewItem = new OLERenewItem();
716         OLERenewItemConverter oleRenewItemConverter = new OLERenewItemConverter();
717         OlePatronDocument olePatronDocument = null;
718         Map<String, String> patronMap = new HashMap<String, String>();
719         patronMap.put(OLEConstants.BARCODE, patronBarcode);
720         List<OlePatronDocument> patronDocuments = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
721         if (patronDocuments.size() > 0) {
722             olePatronDocument = patronDocuments.get(0);
723         } else {
724             oleRenewItem.setCode("002");
725             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
726             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO));
727             return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
728         }
729         if (!loanProcessor.hasCirculationDesk(operatorId)) {
730             oleRenewItem.setCode("001");
731             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
732             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID));
733             return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
734         }
735         Map<String, String> loanMap = new HashMap<String, String>();
736         loanMap.put(OLEConstants.PATRON_ID, olePatronDocument.getOlePatronId());
737         loanMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
738         List<OleLoanDocument> loanDocuments = (List<OleLoanDocument>) businessObjectService.findMatching(OleLoanDocument.class, loanMap);
739         if (loanDocuments.size() > 0) {
740             OleLoanDocument oleLoanDocument = loanDocuments.get(0);
741 
742             if (patronDocuments.size() > 0) {
743                 oleLoanDocument.setOlePatron(olePatronDocument);
744                 oleLoanDocument.setBorrowerTypeCode(olePatronDocument.getBorrowerTypeCode());
745                 oleLoanDocument.setBorrowerTypeId(olePatronDocument.getBorrowerType());
746                 oleLoanDocument.setOleBorrowerType(olePatronDocument.getOleBorrowerType());
747                 oleLoanDocument.setBorrowerTypeName(olePatronDocument.getBorrowerTypeName());
748             }
749             oleLoanDocument.setRenewalItemFlag(true);
750             oleLoanDocument.setErrorMessage(null);
751             if (loanProcessor.canOverrideLoan(operatorId)) {
752                 if (!loanProcessor.checkPendingRequestforItem(oleLoanDocument.getItemUuid())) {
753                     Timestamp currentDate = new Timestamp(System.currentTimeMillis());
754                     try {
755                         oleLoanDocument = loanProcessor.addLoan(oleLoanDocument.getPatronBarcode(), oleLoanDocument.getItemId(), oleLoanDocument, loanProcessor.getPatronLoanedItem(olePatronDocument.getOlePatronId()), operatorId);
756                         if (oleLoanDocument.getErrorMessage() == null || (oleLoanDocument.getErrorMessage() != null && oleLoanDocument.getErrorMessage().trim().isEmpty())) {
757                             oleRenewItem.setCode("003");
758                             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS));
759                             LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS));
760                             oleRenewItem.setPastDueDate(oleLoanDocument.getPastDueDate().toString());
761                             oleRenewItem.setNewDueDate(oleLoanDocument.getLoanDueDate() != null ? oleLoanDocument.getLoanDueDate().toString() : "");
762                             oleRenewItem.setRenewalCount(oleLoanDocument.getNumberOfRenewals());
763                             return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
764                         } else {
765                             oleRenewItem.setCode("500");
766                             oleRenewItem.setMessage(oleLoanDocument.getErrorMessage());
767                             LOG.info(oleLoanDocument.getErrorMessage());
768                             return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
769 
770                         }
771                     } catch (Exception e) {
772                         LOG.error(e,e);
773                         return "Exception occured while renewing an item";
774                     }
775 
776                 } else {
777                     oleRenewItem.setCode("009");
778                     oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RQST_PNDNG));
779                     LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RQST_PNDNG));
780                     return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
781                 }
782             } else {
783                 oleRenewItem.setCode("010");
784                 oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_RENEW));
785                 LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_RENEW));
786                 return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
787             }
788         } else {
789             oleRenewItem.setCode("011");
790             oleRenewItem.setMessage(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN));
791            LOG.info(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.ITM_NT_LOAN));
792             return oleRenewItemConverter.generateRenewItemXml(oleRenewItem);
793         }
794     }
795 
796 
797     public boolean validPatron(String patronId) {
798         boolean valid = false;
799         Map<String, String> patronMap = new HashMap<String, String>();
800         patronMap.put(OLEConstants.BARCODE, patronId);
801         List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) businessObjectService.findMatching(OlePatronDocument.class, patronMap);
802         if (olePatronDocumentList.size() > 0) {
803             valid = true;
804         }
805         return valid;
806     }
807 
808 
809     private Map<String, String> getLocationMap(String itemLocation) {
810         Map<String, String> locationMap = new HashMap<String, String>();
811         String[] locationArray = itemLocation.split("['/']");
812         List<String> locationList = Arrays.asList(locationArray);
813         for (String value : locationList) {
814             Map<String, String> requestMap = new HashMap<>();
815             requestMap.put(OLEConstants.LOCATION_CODE, value);
816             List<OleLocation> oleLocations = (List<OleLocation>) businessObjectService.findMatching(OleLocation.class, requestMap);
817             if (oleLocations != null && oleLocations.size() > 0) {
818                 String locationLevelId = oleLocations.get(0).getLevelId();
819                 requestMap.clear();
820                 requestMap.put(OLEConstants.LEVEL_ID, locationLevelId);
821                 List<OleLocationLevel> oleLocationLevels = (List<OleLocationLevel>) businessObjectService.findMatching(OleLocationLevel.class, requestMap);
822                 if (oleLocationLevels != null && oleLocationLevels.size() > 0) {
823                     OleLocationLevel oleLocationLevel = new OleLocationLevel();
824                     oleLocationLevel = oleLocationLevels.get(0);
825                     if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_CAMPUS)) {
826                         locationMap.put(OLEConstants.ITEM_CAMPUS, value);
827                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_INSTITUTION)) {
828                         locationMap.put(OLEConstants.ITEM_INSTITUTION, value);
829                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_COLLECTION)) {
830                         locationMap.put(OLEConstants.ITEM_COLLECTION, value);
831                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_LIBRARY)) {
832                         locationMap.put(OLEConstants.ITEM_LIBRARY, value);
833                     } else if (oleLocationLevel.getLevelCode().equals(OLEConstants.OLEBatchProcess.LOCATION_LEVEL_SHELVING)) {
834                         locationMap.put(OLEConstants.ITEM_SHELVING, value);
835                     }
836                 }
837             }
838         }
839         return locationMap;
840     }
841 
842     /**
843      * This method is used to convert the date in String format to gregorian calendar format
844      * @param date
845      * @return
846      */
847     public GregorianCalendar getGregorianCalendarDate(String date) {
848         if (date != null) {
849              if(date.equals("")){
850                  return new GregorianCalendar(2025,1,1);
851              }
852             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
853             Date parsedDate = null;
854             try {
855                 parsedDate = simpleDateFormat.parse(date);
856             } catch (ParseException e) {
857                 e.printStackTrace();
858             }
859             Calendar cal = Calendar.getInstance();
860             cal.setTime(parsedDate);
861             int year = cal.get(Calendar.YEAR);
862             int month = cal.get(Calendar.MONTH);
863             int day = cal.get(Calendar.DAY_OF_MONTH);
864             return new GregorianCalendar(year, month, day);
865         }
866         return null;
867     }
868 }
869 
870 
871 
872