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.extensiblecatalog.ncip.v2.service.*;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.bo.OLECheckOutItem;
8   import org.kuali.ole.ncip.bo.OLENCIPConstants;
9   import org.kuali.ole.ncip.converter.OLECheckOutItemConverter;
10  import org.kuali.ole.ncip.service.NCIPCheckOutItemResponseBuilder;
11  import org.kuali.ole.ncip.service.OLECheckOutItemService;
12  import org.kuali.ole.ncip.util.OLENCIPUtil;
13  import org.kuali.ole.utility.OleStopWatch;
14  import org.kuali.rice.core.api.config.property.ConfigContext;
15  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
16  
17  import java.math.BigDecimal;
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * Created by chenchulakshmig on 8/17/15.
24   */
25  public class OLENCIPCheckOutItemServiceImpl extends OLENCIPUtil implements OLECheckOutItemService {
26  
27      private static final Logger LOG = Logger.getLogger(OLENCIPCheckOutItemServiceImpl.class);
28  
29      private OLECirculationHelperServiceImpl oleCirculationHelperService;
30  
31      public NCIPCheckOutItemResponseBuilder getNCIPCheckOutItemResponseBuilder() {
32          return new NCIPCheckOutItemResponseBuilder();
33      }
34  
35      public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
36          if (null == oleCirculationHelperService) {
37              oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
38          }
39          return oleCirculationHelperService;
40      }
41  
42      public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
43          this.oleCirculationHelperService = oleCirculationHelperService;
44      }
45  
46      @Override
47      public CheckOutItemResponseData performService(CheckOutItemInitiationData checkOutItemInitiationData, ServiceContext serviceContext, RemoteServiceManager remoteServiceManager) throws ServiceException, ValidationException {
48          OleStopWatch oleStopWatch = new OleStopWatch();
49          oleStopWatch.start();
50  
51          NCIPCheckOutItemResponseBuilder ncipCheckOutItemResponseBuilder = getNCIPCheckOutItemResponseBuilder();
52          CheckOutItemResponseData checkOutItemResponseData = new CheckOutItemResponseData();
53  
54          AgencyId agencyId = validateAgency(checkOutItemInitiationData.getInitiationHeader(), checkOutItemResponseData);
55          if (null == agencyId) return checkOutItemResponseData;
56  
57          boolean userValid = validateUser(checkOutItemInitiationData.getUserId(), checkOutItemResponseData);
58          if (!userValid) return checkOutItemResponseData;
59  
60          String itemBarcode = checkOutItemInitiationData.getItemId().getItemIdentifierValue();
61          String patronBarcode = checkOutItemInitiationData.getUserId().getUserIdentifierValue();
62          String operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
63  
64          Map checoutParameters = new HashMap();
65          checoutParameters.put("patronBarcode", patronBarcode);
66          checoutParameters.put("operatorId", operatorId);
67          checoutParameters.put("itemBarcode", itemBarcode);
68          checoutParameters.put("responseFormatType", "XML");
69  
70          String responseString = new NonSip2CheckoutItemService().checkoutItem(checoutParameters);
71          OLECheckOutItem oleCheckOutItem = (OLECheckOutItem) new OLECheckOutItemConverter().generateCheckoutItemObject(responseString);
72  
73          if (oleCheckOutItem != null && StringUtils.isNotBlank(oleCheckOutItem.getMessage())) {
74              if (oleCheckOutItem.getMessage() != null && oleCheckOutItem.getMessage().equals(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED))) {
75                  if (oleCheckOutItem.getDueDate() != null) {
76                      ncipCheckOutItemResponseBuilder.setDateDue(checkOutItemResponseData, getOleCirculationHelperService().getGregorianCalendarDate(oleCheckOutItem.getDueDate().toString()));
77                  } else {
78                      ncipCheckOutItemResponseBuilder.setDateDue(checkOutItemResponseData, getOleCirculationHelperService().getGregorianCalendarDate((new java.sql.Timestamp(new Date(2025, 1, 1).getTime()).toString())));
79                  }
80                  ncipCheckOutItemResponseBuilder.setRenewalCount(checkOutItemResponseData, new BigDecimal(oleCheckOutItem.getRenewalCount()));
81                  ncipCheckOutItemResponseBuilder.setItemId(checkOutItemResponseData, checkOutItemInitiationData, agencyId, agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE));
82                  ncipCheckOutItemResponseBuilder.setUserId(checkOutItemResponseData, checkOutItemInitiationData, agencyId, oleCheckOutItem.getUserType());
83              } else {
84                  String problemElement = OLENCIPConstants.ITEM;
85                  String problemValue = itemBarcode;
86  
87                  if (oleCheckOutItem.getCode().equals("002")) {
88                      problemElement = OLENCIPConstants.USER;
89                      problemValue = patronBarcode;
90                  } else if (oleCheckOutItem.getCode().equals("026")) {
91                      problemValue = operatorId;
92                  }
93                  processProblems(checkOutItemResponseData, problemValue, oleCheckOutItem.getMessage(), problemElement);
94              }
95          } else {
96              processProblems(checkOutItemResponseData, itemBarcode, ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.CHECK_OUT_FAIL), OLENCIPConstants.ITEM);
97          }
98          oleStopWatch.end();
99          LOG.info("Time taken to perform checkout item service : " + oleStopWatch.getTotalTime());
100         return checkOutItemResponseData;
101     }
102 
103 }