View Javadoc
1   package org.kuali.ole.ncip.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.extensiblecatalog.ncip.v2.service.*;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.processor.LoanProcessor;
7   import org.kuali.ole.ncip.bo.OLECheckInItem;
8   import org.kuali.ole.ncip.bo.OLENCIPConstants;
9   import org.kuali.ole.ncip.converter.OLECheckInItemConverter;
10  import org.kuali.ole.ncip.service.OLECheckInItemService;
11  import org.kuali.ole.ncip.service.OLECirculationService;
12  import org.kuali.ole.sys.context.SpringContext;
13  import org.kuali.rice.core.api.config.property.ConfigContext;
14  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
15  
16  import java.util.ArrayList;
17  import java.util.HashMap;
18  import java.util.List;
19  
20  /**
21   * Created with IntelliJ IDEA.
22   * User: maheswarang
23   * Date: 7/31/13
24   * Time: 1:27 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class OLECheckInItemServiceImpl implements OLECheckInItemService {
28      private static final Logger LOG = Logger.getLogger(OLECheckInItemServiceImpl.class);
29      private OLECirculationService oleCirculationService;
30      private OLECheckInItemConverter oleCheckInItemConverter;
31      private OLECirculationHelperServiceImpl oleCirculationHelperService;
32      private LoanProcessor loanProcessor;
33  
34      public LoanProcessor getLoanProcessor() {
35          if (loanProcessor == null) {
36              loanProcessor = SpringContext.getBean(LoanProcessor.class);
37          }
38          return loanProcessor;
39      }
40  
41      public OLECirculationService getOleCirculationService() {
42          if (null == oleCirculationService) {
43              oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
44          }
45          return oleCirculationService;
46      }
47  
48      public void setOleCirculationService(OLECirculationService oleCirculationService) {
49          this.oleCirculationService = oleCirculationService;
50      }
51  
52      public OLECheckInItemConverter getOleCheckInItemConverter() {
53          if (null == oleCheckInItemConverter) {
54              oleCheckInItemConverter = GlobalResourceLoader.getService(OLENCIPConstants.CHECKIN_ITEM_CONVERTER);
55          }
56          return oleCheckInItemConverter;
57      }
58  
59      public void setOleCheckInItemConverter(OLECheckInItemConverter oleCheckInItemConverter) {
60          this.oleCheckInItemConverter = oleCheckInItemConverter;
61      }
62  
63      public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
64          if (null == oleCirculationHelperService) {
65              oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
66          }
67          return oleCirculationHelperService;
68      }
69  
70      public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
71          this.oleCirculationHelperService = oleCirculationHelperService;
72      }
73  
74      @Override
75      public CheckInItemResponseData performService(CheckInItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException {
76          CheckInItemResponseData responseData = new CheckInItemResponseData();
77          oleCirculationService = getOleCirculationService();
78          oleCirculationHelperService = getOleCirculationHelperService();
79          oleCheckInItemConverter = getOleCheckInItemConverter();
80          OLECheckInItem oleCheckInItem = null;
81          Problem problem = new Problem();
82          ProblemType problemType = new ProblemType("");
83          List<Problem> problems = new ArrayList<Problem>();
84          String responseString = null;
85          String itemDeleteIndicator = null;
86          AgencyId agencyId = null;
87          String operatorId, itemType = "";
88          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())
89              agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
90          else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
91              agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
92          else
93              agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
94  
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                 itemDeleteIndicator=getLoanProcessor().getParameter(OLENCIPConstants.TEMP_ITEM_DELETE_INDICATOR);
101                 LOG.info("Inside Check in Item Service . Operator Id : "+operatorId + " Item Id : " + initData.getItemId().getItemIdentifierValue());
102                 responseString = oleCirculationService.checkInItem(OLENCIPConstants.PATRON_ID, operatorId, initData.getItemId().getItemIdentifierValue(), itemDeleteIndicator,false);
103                 oleCheckInItem = (OLECheckInItem) oleCheckInItemConverter.generateCheckInItemObject(responseString);
104                 if (oleCheckInItem != null && oleCheckInItem.getMessage() != null && oleCheckInItem.getMessage().equals(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.SUCCESSFULLEY_CHECKED_IN))) {
105                     ItemId itemId = new ItemId();
106                     itemId = setItemId(itemId, initData, oleCheckInItem);
107                     responseData.setItemId(itemId);
108                     UserId userId = new UserId();
109                     userId = setUserId(userId, initData, oleCheckInItem);
110                     responseData.setUserId(userId);
111                     ItemOptionalFields itemOptionalFields = new ItemOptionalFields();
112                     itemOptionalFields = setItemOptionalFields(itemOptionalFields, initData, oleCheckInItem);
113                     responseData.setItemOptionalFields(itemOptionalFields);
114                 } else {
115                     if (oleCheckInItem != null) {
116                         problem.setProblemDetail(oleCheckInItem.getMessage());
117                     } else {
118                         problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.CHECK_IN_FAIL));
119                     }
120                     problem.setProblemElement(OLENCIPConstants.ITEM);
121                     problem.setProblemType(problemType);
122                     problem.setProblemValue("Item value:" + initData.getItemId().getItemIdentifierValue());
123                     problems.add(problem);
124                     responseData.setProblems(problems);
125                 }
126             } else {
127                 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
128                 problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
129                 problem.setProblemType(problemType);
130                 problem.setProblemValue(agencyId.getValue());
131                 problems.add(problem);
132                 responseData.setProblems(problems);
133             }
134 
135 
136         } catch (Exception e) {
137             throw new RuntimeException(e);
138         }
139         return responseData;
140     }
141 
142     private ItemId setItemId(ItemId itemId, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
143         AgencyId agencyId = null;
144         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())
145             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
146         else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
147             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
148         else
149             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
150         String identifierType = "";
151         HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
152         identifierType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
153         itemId.setAgencyId(agencyId);
154         ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.SCHEME, identifierType);
155         itemId.setItemIdentifierType(itemIdentifierType);
156         itemId.setItemIdentifierValue(initData.getItemId().getItemIdentifierValue());
157         return itemId;
158     }
159 
160     private UserId setUserId(UserId userId, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
161         AgencyId agencyId = null;
162         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())
163             agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
164         else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
165             agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
166         else
167             agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
168         UserIdentifierType userIdentifierType = new UserIdentifierType(oleCheckInItem.getUserType(), oleCheckInItem.getUserType());
169         userId.setUserIdentifierType(userIdentifierType);
170         if (oleCheckInItem.getUserId() != null)
171             userId.setUserIdentifierValue(oleCheckInItem.getUserId());
172         else
173             userId.setUserIdentifierValue(" ");
174         return userId;
175     }
176 
177     private ItemOptionalFields setItemOptionalFields(ItemOptionalFields itemOptionalFields, CheckInItemInitiationData initData, OLECheckInItem oleCheckInItem) throws Exception {
178         BibliographicDescription bibliographicDescription = new BibliographicDescription();
179         bibliographicDescription.setAuthor(oleCheckInItem.getTitle());
180         bibliographicDescription.setTitle(oleCheckInItem.getAuthor());
181         itemOptionalFields.setBibliographicDescription(bibliographicDescription);
182         ItemDescription itemDescription = new ItemDescription();
183         itemDescription.setCallNumber(oleCheckInItem.getCallNumber());
184         itemOptionalFields.setItemDescription(itemDescription);
185         return itemOptionalFields;
186     }
187 
188 
189 }