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.ncip.bo.OLENCIPConstants;
8 import org.kuali.ole.ncip.service.OLEAcceptItemService;
9 import org.kuali.ole.ncip.service.OLECirculationService;
10 import org.kuali.rice.core.api.config.property.ConfigContext;
11 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16
17
18
19
20
21
22
23
24 public class OLEAcceptItemServiceImpl implements OLEAcceptItemService {
25 private static final Logger LOG = Logger.getLogger(OLEAcceptItemServiceImpl.class);
26 private OLECirculationService oleCirculationService;
27 private OLECirculationHelperServiceImpl oleCirculationHelperService;
28
29 public OLECirculationService getOleCirculationService() {
30 if(null==oleCirculationService){
31 oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
32 }
33 return oleCirculationService;
34 }
35
36 public void setOleCirculationService(OLECirculationService oleCirculationService) {
37 this.oleCirculationService = oleCirculationService;
38 }
39
40 public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
41 if(null==oleCirculationHelperService){
42 oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
43 }
44 return oleCirculationHelperService;
45 }
46
47 public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
48 this.oleCirculationHelperService = oleCirculationHelperService;
49 }
50
51 @Override
52 public AcceptItemResponseData performService(AcceptItemInitiationData initData, ServiceContext serviceContext, RemoteServiceManager serviceManager) throws ServiceException {
53 AcceptItemResponseData responseData = new AcceptItemResponseData();
54 oleCirculationService = getOleCirculationService();
55 oleCirculationHelperService = getOleCirculationHelperService();
56 List<Problem> problems = new ArrayList<Problem>();
57 String responseString = null;
58 AgencyId agencyId=null;
59 if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getFromAgencyId()!=null && initData.getInitiationHeader().getFromAgencyId().getAgencyId()!=null){
60 agencyId= new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
61 }else if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getApplicationProfileType()!=null){
62 agencyId= new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
63 }else{
64 agencyId= new AgencyId(OLEConstants.AGENCY_ID);
65 }
66 String operatorId,itemType="";
67 HashMap<String,String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
68 itemType = agencyPropertyMap.get("itemType");
69 operatorId = agencyPropertyMap.get("operatorId");
70 String expiryDate = initData.getPickupExpiryDate()!=null?initData.getPickupExpiryDate().toString():"";
71 try{
72 responseString = oleCirculationService.acceptItem((initData.getUserId().getUserIdentifierValue()),
73 operatorId, initData.getItemId().getItemIdentifierValue(), initData.getRequestId().getRequestIdentifierValue(),
74 initData.getItemOptionalFields().getBibliographicDescription().getTitle(),
75 initData.getItemOptionalFields().getBibliographicDescription().getAuthor(),itemType,
76 initData.getPickupLocation().getValue(), expiryDate);
77 if(responseString!= null && responseString.equals(OLEConstants.ITEM_CREATED)){
78 RequestId requestId = new RequestId();
79 requestId = setRequestId(requestId,initData);
80 responseData.setRequestId(requestId);
81 ItemId itemId = new ItemId();
82 itemId = setItemId(itemId,initData,initData.getRequestId().getRequestIdentifierValue());
83 responseData.setItemId(itemId);
84 }else{
85 Problem problem = new Problem();
86 ProblemType problemType = new ProblemType("");
87 problem.setProblemDetail(responseString);
88 problem.setProblemElement("Item");
89 problem.setProblemType(problemType);
90 problem.setProblemValue("Item value:"+initData.getItemId().getItemIdentifierValue());
91 problems.add(problem);
92 responseData.setProblems(problems);
93 }
94 XStream xStream = new XStream();
95 LOG.info((xStream.toXML(responseData)));
96 }catch (Exception e){
97 throw new RuntimeException(e);
98 }
99 return responseData;
100 }
101
102 private RequestId setRequestId(RequestId requestId,AcceptItemInitiationData initData)throws Exception{
103 AgencyId agencyId=null;
104 if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getFromAgencyId()!=null && initData.getInitiationHeader().getFromAgencyId().getAgencyId()!=null)
105 agencyId= new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
106 else if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getApplicationProfileType()!=null)
107 agencyId= new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
108 else
109 agencyId= new AgencyId(OLEConstants.AGENCY_ID);
110 requestId.setAgencyId(agencyId);
111 RequestIdentifierType requestIdentifierType = new RequestIdentifierType("scheme","requestIdentifierType");
112 requestId.setRequestIdentifierType(requestIdentifierType);
113 requestId.setRequestIdentifierValue(initData.getRequestId().getRequestIdentifierValue());
114 return requestId;
115 }
116
117 private ItemId setItemId(ItemId itemId,AcceptItemInitiationData initData,String itemIdentifierValue)throws Exception{
118 AgencyId agencyId=null;
119 if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getFromAgencyId()!=null && initData.getInitiationHeader().getFromAgencyId().getAgencyId()!=null)
120 agencyId= new AgencyId(initData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
121 else if(initData.getInitiationHeader()!=null && initData.getInitiationHeader().getApplicationProfileType()!=null)
122 agencyId= new AgencyId(initData.getInitiationHeader().getApplicationProfileType().getValue());
123 else
124 agencyId= new AgencyId(OLEConstants.AGENCY_ID);
125 String identifierType;
126 HashMap<String,String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
127 identifierType = agencyPropertyMap.get("itemType");
128 itemId.setAgencyId(agencyId);
129 ItemIdentifierType itemIdentifierType = new ItemIdentifierType("Scheme",identifierType);
130 itemId.setItemIdentifierType(itemIdentifierType);
131 itemId.setItemIdentifierValue(itemIdentifierValue);
132 return itemId;
133 }
134
135
136
137
138
139 }