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.OLEAcceptItem;
9   import org.kuali.ole.ncip.bo.OLENCIPConstants;
10  import org.kuali.ole.ncip.converter.OLEAcceptItemConverter;
11  import org.kuali.ole.ncip.service.OLEAcceptItemService;
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.util.ArrayList;
18  import java.util.HashMap;
19  import java.util.List;
20  
21  /**
22   * Created with IntelliJ IDEA.
23   * User: maheswarang
24   * Date: 7/31/13
25   * Time: 1:13 PM
26   * To change this template use File | Settings | File Templates.
27   */
28  public class OLEAcceptItemServiceImpl implements OLEAcceptItemService {
29      private static final Logger LOG = Logger.getLogger(OLEAcceptItemServiceImpl.class);
30      private OLECirculationService oleCirculationService;
31      private OLECirculationHelperServiceImpl oleCirculationHelperService;
32      private LoanProcessor loanProcessor;
33      public LoanProcessor getLoanProcessor() {
34          if (loanProcessor == null) {
35              loanProcessor = SpringContext.getBean(LoanProcessor.class);
36          }
37          return loanProcessor;
38      }
39  
40      public OLECirculationService getOleCirculationService() {
41          if (null == oleCirculationService) {
42              oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
43          }
44          return oleCirculationService;
45      }
46  
47      public void setOleCirculationService(OLECirculationService oleCirculationService) {
48          this.oleCirculationService = oleCirculationService;
49      }
50  
51      public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
52          if (null == oleCirculationHelperService) {
53              oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
54          }
55          return oleCirculationHelperService;
56      }
57  
58      public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
59          this.oleCirculationHelperService = oleCirculationHelperService;
60      }
61  
62      @Override
63      public AcceptItemResponseData performService(AcceptItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException {
64          AcceptItemResponseData responseData = new AcceptItemResponseData();
65          oleCirculationService = getOleCirculationService();
66          oleCirculationHelperService = getOleCirculationHelperService();
67          OLEAcceptItemConverter oleAcceptItemConverter = new OLEAcceptItemConverter();
68          List<Problem> problems = new ArrayList<Problem>();
69          String responseString = null;
70          AgencyId agencyId = null;
71          String operatorId, itemType, requestType, itemLocation;
72          Problem problem = new Problem();
73          ProblemType problemType = new ProblemType("");
74          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()) {
75              agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
76          } else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty()) {
77              agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
78          } else {
79              agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
80          }
81  
82          HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
83          try {
84              if (agencyPropertyMap.size() > 0) {
85                  itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
86                  operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
87                  requestType = agencyPropertyMap.get(OLENCIPConstants.REQUEST_TYPE);
88                  itemLocation = agencyPropertyMap.get(OLENCIPConstants.ITEM_LOCATION);
89                  String expiryDate = initData.getPickupExpiryDate() != null ? initData.getPickupExpiryDate().toString() : "";
90                  String title = initData.getItemOptionalFields() != null && initData.getItemOptionalFields().getBibliographicDescription() != null
91                          && initData.getItemOptionalFields().getBibliographicDescription().getTitle() != null ? initData.getItemOptionalFields().getBibliographicDescription().getTitle() : "";
92                  String author = initData.getItemOptionalFields() != null && initData.getItemOptionalFields().getBibliographicDescription() != null
93                          && initData.getItemOptionalFields().getBibliographicDescription().getAuthor() != null ? initData.getItemOptionalFields().getBibliographicDescription().getAuthor() : "";
94                  String pickUpLocation = initData.getPickupLocation() != null && initData.getPickupLocation().getValue() != null ? initData.getPickupLocation().getValue() : "";
95                  String callNumber = initData.getItemOptionalFields() != null && initData.getItemOptionalFields().getItemDescription() != null && initData.getItemOptionalFields().getItemDescription().getCallNumber() != null ? initData.getItemOptionalFields().getItemDescription().getCallNumber() : "";
96                  LOG.info("Inside Accept Item . Patron Barcode " + initData.getUserId().getUserIdentifierValue() + "Operator Id : " +operatorId + "Item barcode :" +  initData.getItemId().getItemIdentifierValue() + " Call Number : "+callNumber + "Title : "+title + " Author : " +author + "Item Type : "+ itemType + "Item Location : "+itemLocation + "Request Type :" + requestType + "Pick up Location : " + pickUpLocation + " Agency Id : " +agencyId.getValue());
97                  responseString = oleCirculationService.acceptItem((initData.getUserId().getUserIdentifierValue()),
98                          operatorId, initData.getItemId().getItemIdentifierValue(), callNumber,
99                          title, author, itemType, itemLocation, expiryDate, requestType, pickUpLocation);
100                 OLEAcceptItem oleAcceptItem = (OLEAcceptItem) oleAcceptItemConverter.generateAcceptItemObject(responseString);
101                 if (oleAcceptItem != null && oleAcceptItem.getMessage() != null && oleAcceptItem.getMessage().contains(OLEConstants.RQST_SUCCESS)) {
102                     RequestId requestId = new RequestId();
103                     requestId = setRequestId(requestId, initData);
104                     responseData.setRequestId(requestId);
105                     ItemId itemId = new ItemId();
106                     itemId = setItemId(itemId, initData, initData.getItemId().getItemIdentifierValue());
107                     responseData.setItemId(itemId);
108                 } else {
109                     if (oleAcceptItem != null) {
110                         problem.setProblemDetail(oleAcceptItem.getMessage());
111                     } else {
112                         problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.ACCEPT_ITEM_FAIL));
113                     }
114                     problem.setProblemElement(OLENCIPConstants.ITEM);
115                     problem.setProblemType(problemType);
116                     problem.setProblemValue(initData.getItemId().getItemIdentifierValue());
117                     problems.add(problem);
118                     responseData.setProblems(problems);
119                 }
120             } else {
121                 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
122                 problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
123                 problem.setProblemType(problemType);
124                 problem.setProblemValue(agencyId.getValue());
125                 problems.add(problem);
126                 responseData.setProblems(problems);
127             }
128         } catch (Exception e) {
129             throw new RuntimeException(e);
130         }
131         return responseData;
132     }
133 
134     private RequestId setRequestId(RequestId requestId, AcceptItemInitiationData initData) throws Exception {
135         AgencyId agencyId = null;
136         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()) {
137             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
138         } else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty()) {
139             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
140         } else {
141             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
142         }
143         requestId.setAgencyId(agencyId);
144         RequestIdentifierType requestIdentifierType = new RequestIdentifierType(OLENCIPConstants.SCHEME, OLENCIPConstants.REQUEST_IDS);
145         requestId.setRequestIdentifierType(requestIdentifierType);
146         requestId.setRequestIdentifierValue(initData.getRequestId().getRequestIdentifierValue());
147         return requestId;
148     }
149 
150     private ItemId setItemId(ItemId itemId, AcceptItemInitiationData initData, String itemIdentifierValue) throws Exception {
151         AgencyId agencyId = null;
152         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()) {
153             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
154         } else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty()) {
155             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
156         } else {
157             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
158         }
159         String identifierType;
160         HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
161         identifierType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
162         itemId.setAgencyId(agencyId);
163         ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.SCHEME, OLENCIPConstants.ITEM_BARCODES);
164         itemId.setItemIdentifierType(itemIdentifierType);
165         itemId.setItemIdentifierValue(itemIdentifierValue);
166         return itemId;
167     }
168 
169 
170 }