1 package org.kuali.ole.loaders.deliver.service.impl;
2
3 import com.sun.jersey.api.core.HttpContext;
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.cxf.common.util.SortedArraySet;
6 import org.apache.log4j.Logger;
7 import org.codehaus.jettison.json.JSONArray;
8 import org.codehaus.jettison.json.JSONException;
9 import org.codehaus.jettison.json.JSONObject;
10 import org.kuali.ole.deliver.bo.OlePatronDocument;
11 import org.kuali.ole.ingest.pojo.OlePatron;
12 import org.kuali.ole.loaders.common.bo.OLELoaderImportResponseBo;
13 import org.kuali.ole.loaders.common.bo.OLELoaderResponseBo;
14 import org.kuali.ole.loaders.common.constants.OLELoaderConstants;
15 import org.kuali.ole.loaders.common.service.OLELoaderService;
16 import org.kuali.ole.loaders.common.service.impl.OLELoaderServiceImpl;
17 import org.kuali.ole.loaders.deliver.service.OLEPatronLoaderHelperService;
18 import org.kuali.ole.loaders.deliver.service.OLEPatronLoaderService;
19 import org.kuali.ole.service.OlePatronConverterService;
20 import org.kuali.ole.sys.context.SpringContext;
21 import org.kuali.rice.krad.service.BusinessObjectService;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23
24 import java.util.*;
25
26
27
28
29 public class OLEPatronLoaderServiceImpl implements OLEPatronLoaderService {
30
31 private static final Logger LOG = Logger.getLogger(OLEPatronLoaderServiceImpl.class);
32 private OLEPatronLoaderHelperService olePatronLoaderHelperService;
33 private OLELoaderService oleLoaderService;
34 private BusinessObjectService businessObjectService;
35 private OlePatronConverterService olePatronConverterService;
36
37 public OLEPatronLoaderHelperService getPatronDocumentLoaderHelperService() {
38 if(olePatronLoaderHelperService == null){
39 olePatronLoaderHelperService = new OLEPatronLoaderHelperServiceImpl();
40 }
41 return olePatronLoaderHelperService;
42 }
43
44 public void setPatronDocumentLoaderHelperService(OLEPatronLoaderHelperService olePatronLoaderHelperService) {
45 this.olePatronLoaderHelperService = olePatronLoaderHelperService;
46 }
47
48 public OLELoaderService getOleLoaderService() {
49 if(oleLoaderService == null ){
50 oleLoaderService = new OLELoaderServiceImpl();
51 }
52 return oleLoaderService;
53 }
54
55 public void setOleLoaderService(OLELoaderService oleLoaderService) {
56 this.oleLoaderService = oleLoaderService;
57 }
58
59 public BusinessObjectService getBusinessObjectService() {
60 if(businessObjectService == null){
61 businessObjectService = KRADServiceLocator.getBusinessObjectService();
62 }
63 return businessObjectService;
64 }
65
66 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
67 this.businessObjectService = businessObjectService;
68 }
69
70 public OlePatronConverterService getOlePatronConverterService() {
71 if(olePatronConverterService == null){
72 olePatronConverterService = (OlePatronConverterService) SpringContext.getService("olePatronConverterService");
73 }
74 return olePatronConverterService;
75 }
76
77 public void setOlePatronConverterService(OlePatronConverterService olePatronConverterService) {
78 this.olePatronConverterService = olePatronConverterService;
79 }
80
81 @Override
82 public Object importPatrons(String bodyContent, HttpContext context) {
83 LOG.info("Inside importPatrons method.");
84 OLELoaderImportResponseBo oleLoaderImportResponseBo = new OLELoaderImportResponseBo();
85 Set<Integer> rejectPatronList = new SortedArraySet<>();
86 List<JSONObject> createdPatronObject = new ArrayList<JSONObject>();
87 JSONObject requestJsonObject = getOleLoaderService().getJsonObjectFromString(bodyContent);
88 boolean validObject = false;
89 List<OlePatron> failureList = new ArrayList<>();
90 List<OlePatronDocument> createdList = new ArrayList<>();
91 Map<String,Integer> rejectedPatronBarcodeAndIndexMap = new HashMap<>();
92 Map<String,Integer> selectedPatronBarcodeAndIndexMap = new HashMap<>();
93 if(requestJsonObject != null) {
94 if (requestJsonObject.has("items")) {
95 String items = getOleLoaderService().getStringValueFromJsonObject(requestJsonObject, "items");
96 if (StringUtils.isNotBlank(items)) {
97 JSONArray patronJsonArray = getOleLoaderService().getJsonArrayFromString(items);
98 List<OlePatron> olePatronList = getPatronDocumentLoaderHelperService().formIngestOlePatrons(patronJsonArray,rejectedPatronBarcodeAndIndexMap,selectedPatronBarcodeAndIndexMap);
99 createdList = getOlePatronConverterService().createOlePatronDocument(olePatronList,failureList);
100 for(OlePatronDocument olePatronDocument : createdList){
101 createdPatronObject.add((JSONObject)getPatronDocumentLoaderHelperService().formPatronExportResponse(olePatronDocument,OLELoaderConstants.OLELoaderContext.PATRON, context.getRequest().getRequestUri().toASCIIString(), false));
102 }
103 for(OlePatron olePatron : failureList){
104 rejectPatronList.add(selectedPatronBarcodeAndIndexMap.get(olePatron.getBarcode()));
105 }
106 for (Iterator<String> patronBarcodeIndex= rejectedPatronBarcodeAndIndexMap.keySet().iterator(); patronBarcodeIndex.hasNext(); ) {
107 String barcode = patronBarcodeIndex.next();
108 rejectPatronList.add(rejectedPatronBarcodeAndIndexMap.get(barcode));
109 }
110 }else{
111 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
112 }
113
114 }
115 }else{
116 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.BAD_REQUEST, OLELoaderConstants.OLEloaderStatus.BAD_REQUEST);
117 }
118 oleLoaderImportResponseBo.setOleRejectedBos(new ArrayList<Integer>(rejectPatronList));
119 oleLoaderImportResponseBo.setOleCreatedBos(createdPatronObject);
120 return oleLoaderImportResponseBo;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 @Override
174 public Object exportPatronById(String patronId) {
175 LOG.info("Inside exportPatronById method.");
176 OlePatronDocument olePatron = getPatronDocumentLoaderHelperService().getPatronById(patronId);
177 if(olePatron != null){
178 return olePatron;
179 }else{
180 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PATRON_NOT_EXIST);
181 }
182 }
183
184 @Override
185 public Object exportPatronByCode(String patronCode) {
186 LOG.info("Inside exportPatronByCode method.");
187 OlePatronDocument olePatron = getPatronDocumentLoaderHelperService().getPatronByCode(patronCode);
188 if(olePatron != null){
189 return olePatron;
190 }else{
191 return getOleLoaderService().generateResponse(OLELoaderConstants.OLEloaderMessage.PATRON_NOT_EXIST, OLELoaderConstants.OLEloaderStatus.PATRON_NOT_EXIST);
192 }
193 }
194
195 @Override
196 public List<OlePatronDocument> exportAllPatrons() {
197 LOG.info("Inside exportAllPatrons method.");
198 List<OlePatronDocument> olePatrons = getPatronDocumentLoaderHelperService().getAllPatrons();
199 return olePatrons;
200 }
201 }