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