View Javadoc
1   package org.kuali.ole.ncip.service.impl;
2   
3   import com.thoughtworks.xstream.XStream;
4   import org.apache.log4j.Logger;
5   import org.extensiblecatalog.ncip.v2.service.*;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.processor.LoanProcessor;
8   import org.kuali.ole.ncip.bo.OLECheckOutItem;
9   import org.kuali.ole.ncip.bo.OLENCIPConstants;
10  import org.kuali.ole.ncip.converter.OLECheckOutItemConverter;
11  import org.kuali.ole.ncip.service.OLECheckOutItemService;
12  import org.kuali.ole.ncip.service.OLECirculationService;
13  import org.kuali.ole.sys.context.SpringContext;
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.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  
22  /**
23   * Created with IntelliJ IDEA.
24   * User: maheswarang
25   * Date: 7/31/13
26   * Time: 1:29 PM
27   * To change this template use File | Settings | File Templates.
28   */
29  public class OLECheckOutItemServiceImpl implements OLECheckOutItemService {
30      private static final Logger LOG = Logger.getLogger(OLECheckOutItemServiceImpl.class);
31      private OLECirculationService oleCirculationService;
32      private OLECheckOutItemConverter oleCheckOutItemConverter;
33      private OLECirculationHelperServiceImpl oleCirculationHelperService;
34      private LoanProcessor loanProcessor;
35  
36      public LoanProcessor getLoanProcessor() {
37          if (loanProcessor == null) {
38              loanProcessor = SpringContext.getBean(LoanProcessor.class);
39          }
40          return loanProcessor;
41      }
42  
43      public OLECirculationService getOleCirculationService() {
44          if (null == oleCirculationService) {
45              oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
46          }
47          return oleCirculationService;
48      }
49  
50      public void setOleCirculationService(OLECirculationService oleCirculationService) {
51          this.oleCirculationService = oleCirculationService;
52      }
53  
54      public OLECheckOutItemConverter getOleCheckOutItemConverter() {
55          if (null == oleCheckOutItemConverter) {
56              oleCheckOutItemConverter = GlobalResourceLoader.getService(OLENCIPConstants.CHECKOUT_ITEM_CONVERTER);
57          }
58          return oleCheckOutItemConverter;
59      }
60  
61      public void setOleCheckOutItemConverter(OLECheckOutItemConverter oleCheckOutItemConverter) {
62          this.oleCheckOutItemConverter = oleCheckOutItemConverter;
63      }
64  
65      public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
66          if (null == oleCirculationHelperService) {
67              oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
68          }
69          return oleCirculationHelperService;
70      }
71  
72      public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
73          this.oleCirculationHelperService = oleCirculationHelperService;
74      }
75  
76      @Override
77      public CheckOutItemResponseData performService(CheckOutItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException, ValidationException {
78          CheckOutItemResponseData responseData = new CheckOutItemResponseData();
79          oleCirculationService = getOleCirculationService();
80          oleCirculationHelperService = getOleCirculationHelperService();
81          oleCheckOutItemConverter = getOleCheckOutItemConverter();
82          OLECheckOutItem oleCheckOutItem = null;
83          Problem problem = new Problem();
84          ProblemType problemType = new ProblemType("");
85          List<Problem> problems = new ArrayList<Problem>();
86          String responseString = null;
87          AgencyId agencyId = null;
88          String operatorId, itemType = "";
89          if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
90              agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
91          else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
92              agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
93          else
94              agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
95          HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
96          try {
97              if (agencyPropertyMap.size() > 0) {
98                  itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
99                  operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
100                 LOG.info("Inside Check Out Item Service . Patron Barcode : " + initData.getUserId().getUserIdentifierValue() + " Operator Id : "+ operatorId + " Item Barcode : "+ initData.getItemId().getItemIdentifierValue() );
101                 responseString = oleCirculationService.checkOutItem(initData.getUserId().getUserIdentifierValue(), operatorId, initData.getItemId().getItemIdentifierValue());
102                 oleCheckOutItem = (OLECheckOutItem) oleCheckOutItemConverter.generateCheckoutItemObject(responseString);
103                 if (oleCheckOutItem != null && oleCheckOutItem.getMessage() != null && oleCheckOutItem.getMessage().equals(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_LOANED))) {
104                     ItemId itemId = new ItemId();
105                     itemId = setItemId(itemId, initData, oleCheckOutItem);
106                     responseData.setRenewalCount(new BigDecimal(oleCheckOutItem.getRenewalCount()));
107                     responseData.setDateDue(oleCirculationHelperService.getGregorianCalendarDate(oleCheckOutItem.getDueDate()));
108                     responseData.setItemId(itemId);
109                     UserId userId = new UserId();
110                     userId = setUserId(userId, initData, oleCheckOutItem);
111                     responseData.setUserId(userId);
112                 } else {
113                     if (oleCheckOutItem != null) {
114                         problem.setProblemDetail(oleCheckOutItem.getMessage());
115                     } else {
116                         problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.CHECK_OUT_FAIL));
117                     }
118                     problem.setProblemElement(OLENCIPConstants.ITEM);
119                     problem.setProblemType(problemType);
120                     problem.setProblemValue(initData.getItemId().getItemIdentifierValue());
121                     problems.add(problem);
122                     responseData.setProblems(problems);
123                 }
124             } else {
125                 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
126                 problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
127                 problem.setProblemType(problemType);
128                 problem.setProblemValue("Agency Id:" + agencyId.getValue());
129                 problems.add(problem);
130                 responseData.setProblems(problems);
131             }
132 
133 
134         } catch (Exception e) {
135             throw new RuntimeException(e);
136         }
137         return responseData;
138     }
139 
140     private ItemId setItemId(ItemId itemId, CheckOutItemInitiationData initData, OLECheckOutItem oleCheckOutItem) throws Exception {
141         AgencyId agencyId = null;
142         if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
143             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
144         else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
145             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
146         else
147             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
148         String identifierType = "";
149         HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
150         identifierType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
151         itemId.setAgencyId(agencyId);
152         ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.SCHEME, identifierType);
153         itemId.setItemIdentifierType(itemIdentifierType);
154         itemId.setItemIdentifierValue(initData.getItemId().getItemIdentifierValue());
155         return itemId;
156     }
157 
158     private UserId setUserId(UserId userId, CheckOutItemInitiationData initData, OLECheckOutItem oleCheckOutItem) throws Exception {
159         AgencyId agencyId = null;
160         if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getFromAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty())
161             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
162         else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
163             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
164         else
165             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
166         userId.setAgencyId(agencyId);
167         UserIdentifierType userIdentifierType = new UserIdentifierType(oleCheckOutItem.getUserType(), oleCheckOutItem.getUserType());
168         userId.setUserIdentifierValue(initData.getUserId().getUserIdentifierValue());
169         userId.setUserIdentifierType(userIdentifierType);
170         return userId;
171     }
172 
173 
174 }