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.*;
9 import org.kuali.ole.ncip.converter.OLELookupUserConverter;
10 import org.kuali.ole.ncip.service.OLECirculationService;
11 import org.kuali.ole.ncip.service.OLELookupUserService;
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.GregorianCalendar;
19 import java.util.HashMap;
20 import java.util.List;
21
22
23
24
25
26
27
28
29 public class OLELookupUserServiceImpl implements OLELookupUserService {
30 private static final Logger LOG = Logger.getLogger(OLELookupUserServiceImpl.class);
31 private OLECirculationService oleCirculationService = getOleCirculationService();
32 private OLECirculationHelperServiceImpl oleCirculationHelperService= getOleCirculationHelperService();
33 private LoanProcessor loanProcessor;
34 private OLELookupUserConverter oleLookupUserConverter = getOleLookupUserConverter();
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 OLECirculationHelperServiceImpl getOleCirculationHelperService() {
55 if (null == oleCirculationHelperService) {
56 oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
57 }
58 return oleCirculationHelperService;
59 }
60
61 public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
62 this.oleCirculationHelperService = oleCirculationHelperService;
63 }
64
65 public OLELookupUserConverter getOleLookupUserConverter() {
66 if(null == oleLookupUserConverter ){
67 oleLookupUserConverter = GlobalResourceLoader.getService(OLENCIPConstants.LOOKUP_USER_CONVERTER);
68 }
69 return oleLookupUserConverter;
70 }
71
72 public void setOleLookupUserConverter(OLELookupUserConverter oleLookupUserConverter) {
73 this.oleLookupUserConverter = oleLookupUserConverter;
74 }
75
76 @Override
77 public LookupUserResponseData performService(LookupUserInitiationData initData,
78 ServiceContext serviceContext,
79 RemoteServiceManager serviceManager) {
80
81 LookupUserResponseData responseData = new LookupUserResponseData();
82 oleCirculationService = getOleCirculationService();
83 oleCirculationHelperService = getOleCirculationHelperService();
84 OLELookupUserConverter OLELookupUserConverter = new org.kuali.ole.ncip.converter.OLELookupUserConverter();
85
86 String lookUpUserResponseXml = null;
87 OLELookupUser oleLookupUser = null;
88 List<Problem> problems = new ArrayList<Problem>();
89 AgencyId agencyId = null;
90 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())
91 agencyId = new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
92 else if (initData.getInitiationHeader() != null && initData.getInitiationHeader().getApplicationProfileType() != null && initData.getInitiationHeader().getApplicationProfileType().getValue() != null && !initData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty())
93 agencyId = new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
94 else
95 agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
96
97 String operatorId, itemType = "";
98 HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
99 itemType = agencyPropertyMap.get(OLENCIPConstants.ITEM_TYPE);
100 operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
101 Problem problem = new Problem();
102 ProblemType problemType = new ProblemType("");
103 if (agencyPropertyMap.size() > 0) {
104 try {
105 LOG.info("User Id : "+initData.getUserId().getUserIdentifierValue() + " Operator Id : " + operatorId + " Agency Id "+agencyId.getValue());
106 lookUpUserResponseXml = oleCirculationService.lookupUserForNCIP(initData.getUserId().getUserIdentifierValue(), operatorId, agencyId.getValue());
107 oleLookupUser = (OLELookupUser) oleLookupUserConverter.getLookupUser(lookUpUserResponseXml);
108 if (oleLookupUser.getMessage() != null) {
109 if (oleLookupUser.getMessage().equalsIgnoreCase(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RTRVD_SUCCESS))) {
110 responseData = generateResponseDataFromLookupUser(oleLookupUser, initData, agencyId);
111 responseData.setUserId(initData.getUserId());
112 } else if (oleLookupUser.getMessage().equalsIgnoreCase(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.NO_PATRON_INFO))) {
113 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.USER_UN_AVAILABLE));
114 problem.setProblemElement(OLENCIPConstants.USER);
115 problem.setProblemType(problemType);
116 problem.setProblemValue(initData.getUserId().getUserIdentifierValue());
117 problems.add(problem);
118 responseData.setProblems(problems);
119 } else if (oleLookupUser.getMessage().equalsIgnoreCase(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.INVALID_OPRTR_ID))) {
120 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.OPERATOR_UN_AVAILABLE));
121 problem.setProblemElement(OLENCIPConstants.OPERATOR);
122 problem.setProblemType(problemType);
123 problem.setProblemValue(initData.getUserId().getUserIdentifierValue());
124 problems.add(problem);
125 responseData.setProblems(problems);
126 }
127 }
128
129 } catch (Exception e) {
130 LOG.error(e);
131 }
132 } else {
133 problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
134 problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
135 problem.setProblemType(problemType);
136 problem.setProblemValue(agencyId.getValue());
137 problems.add(problem);
138 responseData.setProblems(problems);
139 }
140
141 return responseData;
142 }
143
144 public LookupUserResponseData generateResponseDataFromLookupUser(OLELookupUser oleLookupUser, LookupUserInitiationData initData, AgencyId agencyId) {
145 LookupUserResponseData lookupUserResponseData = new LookupUserResponseData();
146 if (initData.getUserFiscalAccountDesired())
147 lookupUserResponseData.setUserFiscalAccounts(getUserFiscalAccounts(oleLookupUser.getOleItemFines(), agencyId));
148 if (initData.getLoanedItemsDesired())
149 lookupUserResponseData.setLoanedItems(getLoanedItems(oleLookupUser.getOleCheckedOutItems(), agencyId));
150 if (initData.getRequestedItemsDesired())
151 lookupUserResponseData.setRequestedItems(getRequestedItems(oleLookupUser.getOleHolds(), agencyId));
152 UserOptionalFields userOptionalFields = new UserOptionalFields();
153 PersonalNameInformation pni = new PersonalNameInformation();
154 String patronName = null;
155 String firstName = null;
156 String lastName = null;
157 String middleName = null;
158 if (oleLookupUser.getPatronName() != null) {
159 if (oleLookupUser.getPatronName().getFirstName() != null) {
160 firstName = oleLookupUser.getPatronName().getFirstName();
161 patronName = firstName;
162 }
163 if (oleLookupUser.getPatronName().getMiddleName() != null) {
164 middleName = oleLookupUser.getPatronName().getMiddleName();
165 if(patronName!=null){
166 patronName = patronName + " " + middleName;
167 }else{
168 patronName = middleName;
169 }
170 }
171 if (oleLookupUser.getPatronName().getLastName() != null) {
172 lastName = oleLookupUser.getPatronName().getLastName();
173 if(patronName!=null){
174 patronName = patronName + " " + lastName;
175 }else{
176 patronName = lastName;
177 }
178 }
179 StructuredPersonalUserName structuredPersonalUserName = new StructuredPersonalUserName();
180 if (firstName != null) {
181 structuredPersonalUserName.setGivenName(firstName);
182 }
183 if (lastName != null) {
184 structuredPersonalUserName.setSurname(lastName);
185 }
186 pni.setStructuredPersonalUserName(structuredPersonalUserName);
187 pni.setUnstructuredPersonalUserName(patronName);
188 }
189
190 NameInformation ni = new NameInformation();
191 ni.setPersonalNameInformation(pni);
192
193
194 List<OLEUserPrivilege> oleUserPrivilegeList = oleLookupUser.getOleUserPrivileges();
195 List<UserPrivilege> userPrivilegeList = new ArrayList<UserPrivilege>();
196 UserPrivilege userPrivilege;
197 UserPrivilegeStatus userPrivilegeStatus;
198 UserPrivilegeStatusType userPrivilegeStatusType;
199 for (OLEUserPrivilege oleUserPrivilege : oleUserPrivilegeList) {
200 userPrivilege = new UserPrivilege();
201 userPrivilegeStatus = new UserPrivilegeStatus();
202 AgencyUserPrivilegeType agencyUserPrivilegeType = new AgencyUserPrivilegeType("", oleUserPrivilege.getUserPrivilegeType());
203 userPrivilegeStatusType = new UserPrivilegeStatusType("", oleUserPrivilege.getUserPrivilegeStatus());
204 userPrivilegeStatus.setUserPrivilegeStatusType(userPrivilegeStatusType);
205 userPrivilege.setAgencyUserPrivilegeType(agencyUserPrivilegeType);
206 userPrivilege.setUserPrivilegeDescription(oleUserPrivilege.getUserPrivilegeDescription());
207 userPrivilege.setAgencyId(agencyId);
208 userPrivilege.setUserPrivilegeStatus(userPrivilegeStatus);
209 userPrivilegeList.add(userPrivilege);
210 }
211 if (lookupUserResponseData.getUserOptionalFields() == null) {
212 lookupUserResponseData.setUserOptionalFields(new UserOptionalFields());
213 }
214 if (initData.getUserAddressInformationDesired()) {
215 userOptionalFields.setUserAddressInformations(retrieveAddress(oleLookupUser));
216 }
217 if (initData.getUserPrivilegeDesired()) {
218 userOptionalFields.setUserPrivileges(userPrivilegeList);
219 }
220 if (initData.getNameInformationDesired()) {
221 userOptionalFields.setNameInformation(ni);
222 }
223 lookupUserResponseData.setUserOptionalFields(userOptionalFields);
224 return lookupUserResponseData;
225 }
226
227 private ArrayList<UserAddressInformation> retrieveAddress(OLELookupUser oleLookupUser) {
228 ArrayList<UserAddressInformation> userAddressInformationList = new ArrayList<UserAddressInformation>();
229 if (oleLookupUser.getPatronAddress() != null) {
230 userAddressInformationList.add(retrievePhysicalAddress(oleLookupUser));
231 }
232 if (oleLookupUser.getPatronEmail() != null) {
233 userAddressInformationList.add(retrieveElectronicAddress(oleLookupUser));
234 }
235 if (oleLookupUser.getPatronPhone() != null) {
236 userAddressInformationList.add(retrieveTelephoneNumber(oleLookupUser));
237 }
238 return userAddressInformationList;
239 }
240
241 private UserAddressInformation retrievePhysicalAddress(OLELookupUser oleLookupUser) {
242 UserAddressInformation userAddressInformation = new UserAddressInformation();
243 PhysicalAddress physicalAddress = new PhysicalAddress();
244 StructuredAddress structuredAddress = new StructuredAddress();
245 structuredAddress.setLine1(oleLookupUser.getPatronAddress().getLine1());
246 structuredAddress.setLine2(oleLookupUser.getPatronAddress().getLine2());
247 structuredAddress.setPostalCode(oleLookupUser.getPatronAddress().getPostalCode());
248 structuredAddress.setCountry(oleLookupUser.getPatronAddress().getCountryCode());
249 structuredAddress.setRegion(oleLookupUser.getPatronAddress().getStateProvinceCode());
250 structuredAddress.setLocality(oleLookupUser.getPatronAddress().getCity());
251 physicalAddress.setStructuredAddress(structuredAddress);
252 UserAddressRoleType userAddressRoleType = new UserAddressRoleType(oleLookupUser.getPatronAddress().getAddressTypeCode());
253 userAddressInformation.setUserAddressRoleType(userAddressRoleType);
254 PhysicalAddressType physicalAddressType = new PhysicalAddressType(OLENCIPConstants.ADDRESS_TYPE_SCHEME, oleLookupUser.getPatronAddress().getAddressTypeCode());
255 physicalAddress.setPhysicalAddressType(physicalAddressType);
256 userAddressInformation.setPhysicalAddress(physicalAddress);
257 return userAddressInformation;
258 }
259
260 private UserAddressInformation retrieveElectronicAddress(OLELookupUser oleLookupUser) {
261 ElectronicAddress electronicAddress = new ElectronicAddress();
262 electronicAddress.setElectronicAddressData(oleLookupUser.getPatronEmail().getEmailAddress());
263 ElectronicAddressType electronicAddressType = new ElectronicAddressType(OLENCIPConstants.EMAIL);
264 electronicAddress.setElectronicAddressType(electronicAddressType);
265 UserAddressInformation userAddressInformation = new UserAddressInformation();
266 UserAddressRoleType userAddressRoleType = new UserAddressRoleType(oleLookupUser.getPatronEmail().getEmailTypeCode());
267 userAddressInformation.setUserAddressRoleType(userAddressRoleType);
268 userAddressInformation.setElectronicAddress(electronicAddress);
269 return userAddressInformation;
270 }
271
272 private UserAddressInformation retrieveTelephoneNumber(OLELookupUser oleLookupUser) {
273 if (oleLookupUser.getPatronPhone() != null) {
274 ElectronicAddress phone = new ElectronicAddress();
275 phone.setElectronicAddressData(oleLookupUser.getPatronPhone().getPhoneNumber());
276 phone.setElectronicAddressType(new ElectronicAddressType(OLENCIPConstants.TELEPHONE_CODE));
277 UserAddressInformation uai = new UserAddressInformation();
278 uai.setUserAddressRoleType(new UserAddressRoleType(oleLookupUser.getPatronPhone().getPhoneTypeCode()));
279 uai.setElectronicAddress(phone);
280 return uai;
281 } else return null;
282 }
283
284 private List<LoanedItem> getLoanedItems(OLECheckedOutItems oleCheckedOutItems, AgencyId agencyId) {
285 LoanedItem loanedItem;
286 List<LoanedItem> loanedItems = new ArrayList<LoanedItem>();
287 if (oleCheckedOutItems != null && oleCheckedOutItems.getCheckedOutItems() != null) {
288 for (OLECheckedOutItem oleCheckedOutItems1 : oleCheckedOutItems.getCheckedOutItems()) {
289 loanedItem = new LoanedItem();
290 ItemId itemId = new ItemId();
291 itemId.setAgencyId(agencyId);
292 ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.IDENTIFIER_TYPE, oleCheckedOutItems1.getItemType());
293 itemId.setItemIdentifierType(itemIdentifierType);
294 itemId.setItemIdentifierValue(oleCheckedOutItems1.getItemId());
295 loanedItem.setItemId(itemId);
296 loanedItem.setReminderLevel(new BigDecimal(oleCheckedOutItems1.getNumberOfOverdueSent()));
297 Amount amount = new Amount();
298 CurrencyCode currencyCode = new CurrencyCode(OLENCIPConstants.USD, 1);
299 amount.setCurrencyCode(currencyCode);
300 amount.setMonetaryValue(new BigDecimal(0));
301 loanedItem.setAmount(amount);
302 List<BibliographicId> bibliographicIds = new ArrayList<BibliographicId>();
303 BibliographicId bibliographicId = new BibliographicId();
304 BibliographicItemId bibliographicItemId = new BibliographicItemId();
305 bibliographicItemId.setBibliographicItemIdentifier(oleCheckedOutItems1.getItemId());
306 BibliographicRecordId bibliographicRecordId = new BibliographicRecordId();
307 bibliographicRecordId.setAgencyId(agencyId);
308 bibliographicRecordId.setBibliographicRecordIdentifier(oleCheckedOutItems1.getCatalogueId());
309 bibliographicId.setBibliographicRecordId(bibliographicRecordId);
310 bibliographicId.setBibliographicItemId(bibliographicItemId);
311 bibliographicIds.add(bibliographicId);
312 loanedItem.setBibliographicIds(bibliographicIds);
313 loanedItem.setDateDue(oleCirculationHelperService.getGregorianCalendarDate(oleCheckedOutItems1.getDueDate()));
314 loanedItem.setTitle(oleCheckedOutItems1.getTitle());
315 MediumType mediumType = new MediumType(OLENCIPConstants.MEDIUM_TYPE, oleCheckedOutItems1.getItemType());
316 loanedItem.setMediumType(mediumType);
317
318 loanedItem.setDateCheckedOut(oleCirculationHelperService.getGregorianCalendarDate(oleCheckedOutItems1.getLoanDate()));
319 loanedItems.add(loanedItem);
320 }
321 }
322 return loanedItems;
323 }
324
325
326 private List<RequestedItem> getRequestedItems(OLEHolds oleHolds, AgencyId agencyId) {
327 RequestedItem requestedItem;
328 List<RequestedItem> requestedItems = new ArrayList<RequestedItem>();
329 if (oleHolds != null && oleHolds.getOleHoldList() != null) {
330 for (OLEHold oleHold : oleHolds.getOleHoldList()) {
331 requestedItem = new RequestedItem();
332 requestedItem.setHoldQueuePosition(new BigDecimal(oleHold.getPriority()));
333 ItemId itemId = new ItemId();
334 itemId.setItemIdentifierValue(oleHold.getItemId());
335 ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.IDENTIFIER_TYPE, oleHold.getItemType());
336 itemId.setItemIdentifierType(itemIdentifierType);
337 requestedItem.setItemId(itemId);
338 requestedItem.setItemId(itemId);
339 RequestId requestId = new RequestId();
340 requestId.setAgencyId(agencyId);
341 requestId.setRequestIdentifierValue(oleHold.getRequestId());
342 requestedItem.setRequestId(requestId);
343 RequestType requestType = new RequestType(OLENCIPConstants.REQUEST_TYPES, oleHold.getRequestType());
344 requestedItem.setRequestType(requestType);
345 RequestStatusType requestStatusType = new RequestStatusType("");
346 requestedItem.setRequestStatusType(requestStatusType);
347 requestedItem.setDatePlaced(oleCirculationHelperService.getGregorianCalendarDate(oleHold.getCreateDate()));
348 requestedItem.setPickupDate(oleCirculationHelperService.getGregorianCalendarDate(oleHold.getAvailableDate()));
349 PickupLocation pickupLocation = new PickupLocation(oleHold.getPickupLocation());
350 requestedItem.setPickupLocation(pickupLocation);
351 requestedItem.setTitle(oleHold.getTitle());
352 requestedItems.add(requestedItem);
353 }
354
355 }
356 return requestedItems;
357 }
358
359 private List<UserFiscalAccount> getUserFiscalAccounts(OLEItemFines oleItemFines, AgencyId agencyId) {
360 UserFiscalAccount userFiscalAccount;
361 List<UserFiscalAccount> userFiscalAccounts = new ArrayList<UserFiscalAccount>();
362
363 if (oleItemFines != null && oleItemFines.getOleItemFineList() != null) {
364 for (OLEItemFine oleItemFine : oleItemFines.getOleItemFineList()) {
365 userFiscalAccount = new UserFiscalAccount();
366 AccountBalance accountBalance = new AccountBalance();
367 CurrencyCode currencyCode = new CurrencyCode(OLENCIPConstants.USD, 1);
368 accountBalance.setCurrencyCode(currencyCode);
369 accountBalance.setMonetaryValue(new BigDecimal(oleItemFine.getBalance().intValue()));
370 userFiscalAccount.setAccountBalance(accountBalance);
371 List<AccountDetails> accountDetailsList = new ArrayList<AccountDetails>();
372 AccountDetails accountDetails = new AccountDetails();
373 accountDetails.setAccrualDate(oleCirculationHelperService.getGregorianCalendarDate(oleItemFine.getDateCharged()));
374 FiscalTransactionInformation fiscalTransactionInformation = new FiscalTransactionInformation();
375 Amount amount = new Amount();
376 amount.setCurrencyCode(currencyCode);
377 amount.setMonetaryValue(new BigDecimal(oleItemFine.getAmount().intValue()));
378 fiscalTransactionInformation.setAmount(amount);
379 PaymentMethodType paymentMethodType = new PaymentMethodType(OLENCIPConstants.PAYMENT_METHOD_TYPE, OLENCIPConstants.CASH);
380 fiscalTransactionInformation.setPaymentMethodType(paymentMethodType);
381 FiscalActionType fiscalActionType = new FiscalActionType(OLENCIPConstants.FISCAL_ACTION_TYPE, OLENCIPConstants.FINES);
382 fiscalTransactionInformation.setFiscalActionType(fiscalActionType);
383 FiscalTransactionType fiscalTransactionType = new FiscalTransactionType(OLENCIPConstants.FISCAL_TRANSACTION_TYPE, oleItemFine.getReason());
384 fiscalTransactionInformation.setFiscalTransactionType(fiscalTransactionType);
385 ItemDetails itemDetails = new ItemDetails();
386 ItemId itemId = new ItemId();
387 if (oleItemFine.getItemId() != null)
388 itemId.setItemIdentifierValue(oleItemFine.getItemId());
389 else
390 itemId.setItemIdentifierValue("");
391 itemId.setAgencyId(agencyId);
392 ItemIdentifierType itemIdentifierType = new ItemIdentifierType(OLENCIPConstants.IDENTIFIER_TYPE, OLENCIPConstants.ITEM_BARCODES);
393 itemId.setItemIdentifierType(itemIdentifierType);
394 itemDetails.setItemId(itemId);
395 BibliographicDescription bibliographicDescription = new BibliographicDescription();
396 bibliographicDescription.setTitle(oleItemFine.getTitle());
397 bibliographicDescription.setAuthor(oleItemFine.getAuthor());
398 itemDetails.setBibliographicDescription(bibliographicDescription);
399 fiscalTransactionInformation.setItemDetails(itemDetails);
400 accountDetails.setFiscalTransactionInformation(fiscalTransactionInformation);
401 accountDetailsList.add(accountDetails);
402 userFiscalAccount.setAccountDetails(accountDetailsList);
403 userFiscalAccounts.add(userFiscalAccount);
404 }
405 }
406 return userFiscalAccounts;
407 }
408
409
410
411
412 }