1 package org.kuali.ole.docstore.engine.service.rest;
2
3 import com.google.common.io.CharStreams;
4 import org.apache.commons.lang.StringUtils;
5 import org.kuali.ole.docstore.common.document.*;
6 import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
7 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
8 import org.kuali.ole.docstore.common.document.content.bib.marc.OrderBibMarcRecord;
9 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
10 import org.kuali.ole.docstore.common.document.ids.BibId;
11 import org.kuali.ole.docstore.common.document.ids.BibIds;
12 import org.kuali.ole.docstore.common.document.ids.HoldingsId;
13 import org.kuali.ole.docstore.common.exception.*;
14 import org.kuali.ole.docstore.common.find.FindParams;
15 import org.kuali.ole.docstore.common.service.DocstoreService;
16 import org.kuali.ole.docstore.service.BeanLocator;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import java.io.*;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31
32
33
34
35
36
37
38 public class BibRestServlet extends HttpServlet {
39 private static final Logger LOG = LoggerFactory.getLogger(BibRestServlet.class);
40 private static String responseUrl = "documentrest/bib/doc/";
41 private static String responseTreeUrl = "documentrest/bib/doc/tree/";
42 public static BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
43 @Override
44 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45 PrintWriter out = resp.getWriter();
46 resp.setContentType("application/xml;charset=UTF-8");
47 String result = "";
48 try {
49 if (req.getPathInfo() != null && req.getPathInfo().startsWith("/reload")) {
50 DocumentSearchConfig.getDocumentSearchConfig();
51 DocumentSearchConfig.reloadDocumentConfig();
52 } else if (req.getPathInfo() != null && req.getPathInfo().contains("doc")) {
53 if (req.getPathInfo().startsWith("/doc/trees")) {
54 result = retrieveBibTrees(req);
55 } else if (req.getPathInfo().startsWith("/doc/tree")) {
56 result = retrieveBibTree(req);
57 } else {
58 result = retrieveBib(req);
59 }
60 } else {
61 result = retrieveBibContent(req);
62 }
63 out.print(result);
64 } catch (DocstoreException de) {
65 LOG.error("Docstore Exception :", de);
66 out.print(DocstoreExceptionProcessor.toXml(de));
67 } catch (Exception e) {
68 LOG.error("Exception :", e);
69 out.print(e);
70 }
71 }
72
73 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
74
75 PrintWriter out = resp.getWriter();
76 resp.setContentType("application/xml;charset=UTF-8");
77 String result = "";
78 try {
79 if (req.getPathInfo().startsWith("/tree/doc/find")) {
80 result = findBibTree(req);
81 } else if (req.getPathInfo().startsWith("/doc/find")) {
82 result = findBib(req);
83 } else if (req.getPathInfo().startsWith("/doc/tree")) {
84 result = createBibTree(req);
85 } else if (req.getPathInfo().contains("/transfer")) {
86 result = transferHoldings(req);
87 } else if (req.getPathInfo().contains("/process")) {
88 result = processBibTrees(req);
89 }else if (req.getPathInfo().contains("/searchAcquistion")) {
90 result = retrieveBib(req);
91 } else {
92 result = createBib(req);
93 }
94 out.print(result);
95 } catch (DocstoreException de) {
96 LOG.error("Exception :", de);
97 out.print(DocstoreExceptionProcessor.toXml(de));
98 } catch (Exception e) {
99 LOG.error("Exception :", e);
100 out.print(e);
101 }
102 }
103
104
105 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
106
107 PrintWriter out = resp.getWriter();
108 resp.setContentType("application/xml;charset=UTF-8");
109 String result = "";
110 try {
111 if (req.getPathInfo().startsWith("/doc/trees")) {
112
113 } else {
114 result = updateBib(req);
115 }
116 out.print(result);
117 } catch (DocstoreException de) {
118 LOG.error("Exception :", de);
119 out.print(DocstoreExceptionProcessor.toXml(de));
120 } catch (Exception e) {
121 LOG.error("Exception :", e);
122 out.print(e);
123 }
124 }
125
126 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
127 PrintWriter out = resp.getWriter();
128 resp.setContentType("application/xml;charset=UTF-8");
129 String result = "";
130
131 try {
132 result = deleteBibs(req);
133 out.print(result);
134 } catch (DocstoreException de) {
135 LOG.error("Exception :", de);
136 out.print(DocstoreExceptionProcessor.toXml(de));
137 } catch (Exception e) {
138 LOG.error("Exception :", e);
139 out.print(e);
140 }
141 }
142
143 private String deleteBib(HttpServletRequest req) {
144 DocstoreService ds = BeanLocator.getDocstoreService();
145 String bibId = getIdFromPathInfo(req.getPathInfo());
146 ds.deleteBib(bibId);
147 return "Success";
148 }
149
150 private String deleteBibs(HttpServletRequest req) {
151 DocstoreService ds = BeanLocator.getDocstoreService();
152 Map params = req.getParameterMap();
153 String[] bibIds = (String[]) params.get("bibId");
154 List<String> bibIdList = new ArrayList<String>();
155 for (String bibId : bibIds) {
156 bibIdList.add(bibId);
157 }
158 ds.deleteBibs(bibIdList);
159 return "Success";
160 }
161
162 private String getIdFromPathInfo(String pathInfo) {
163 String id = "";
164 if (StringUtils.isNotEmpty(pathInfo)) {
165 int length = pathInfo.length();
166 if (pathInfo.endsWith("/")) {
167 pathInfo = pathInfo.substring(0, length - 1);
168 }
169 id = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
170 }
171 return id;
172 }
173
174 private String findBibTree(HttpServletRequest req) throws IOException {
175 DocstoreService ds = BeanLocator.getDocstoreService();
176 String requestBody = CharStreams.toString(req.getReader());
177 BibTree bibTree = new BibTree();
178 FindParams findParams = new FindParams();
179 findParams = (FindParams) findParams.deserialize(requestBody);
180 HashMap<String, String> hashMap = new HashMap();
181 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
182 for (FindParams.Map.Entry entry : entries) {
183 hashMap.put(entry.getKey(), entry.getValue());
184 }
185 bibTree = ds.findBibTree(hashMap);
186 return bibTree.serialize(bibTree);
187
188 }
189
190 private String findBib(HttpServletRequest req) throws IOException {
191 DocstoreService ds = BeanLocator.getDocstoreService();
192 String requestBody = CharStreams.toString(req.getReader());
193 Bib bib = new BibMarc();
194 FindParams findParams = new FindParams();
195 findParams = (FindParams) findParams.deserialize(requestBody);
196 HashMap<String, String> hashMap = new HashMap();
197 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
198 for (FindParams.Map.Entry entry : entries) {
199 hashMap.put(entry.getKey(), entry.getValue());
200 }
201 bib = ds.findBib(hashMap);
202 return (bib.serialize(bib));
203 }
204
205 private String createBib(HttpServletRequest req) throws IOException {
206 DocstoreService ds = BeanLocator.getDocstoreService();
207 String requestBody = CharStreams.toString(req.getReader());
208 Bib bib = new BibMarc();
209 Bib bibObj = (Bib) bib.deserialize(requestBody);
210 ds.createBib(bibObj);
211 String responseStr = responseUrl + bibObj.getId();
212 return responseStr;
213 }
214
215 private String updateBib(HttpServletRequest req) throws IOException {
216 DocstoreService ds = BeanLocator.getDocstoreService();
217 String requestBody = CharStreams.toString(req.getReader());
218
219 Bib bib = new BibMarc();
220 Bib bibObj = (Bib) bib.deserialize(requestBody);
221 ds.updateBib(bibObj);
222 return responseUrl + bibObj.getId();
223 }
224
225
226 private String retrieveBib(HttpServletRequest req) {
227 DocstoreService ds = BeanLocator.getDocstoreService();
228 Map params = req.getParameterMap();
229 String[] bibIds = (String[]) params.get("bibId");
230 List<String> bibIdList = new ArrayList<String>();
231
232 for (String bibId : bibIds) {
233 bibIdList.add(bibId);
234 }
235 List<Bib> bibs = null;
236 bibs = ds.retrieveBibs(bibIdList);
237 if (bibs.size() == 1) {
238 Bib bib = new Bib();
239 return bib.serialize(bibs.get(0));
240 }
241 Bibs bibsObj = new Bibs();
242 bibsObj.getBibs().addAll(bibs);
243 return Bibs.serialize(bibsObj);
244 }
245
246 private String retrieveBibContent(HttpServletRequest req) {
247 DocstoreService ds = BeanLocator.getDocstoreService();
248 StringBuffer contentBuffer = new StringBuffer();
249 BibMarcRecords bibMarcRecords = new BibMarcRecords();
250 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
251 List<String> bibIdList = new ArrayList<String>();
252 Map params = req.getParameterMap();
253 String[] queryVariables = (String[]) params.get("bibId");
254 if (queryVariables.length > 0) {
255 for (String bibId : queryVariables) {
256 bibIdList.add(bibId);
257 }
258 }
259 if (bibIdList != null && bibIdList.size() > 1) {
260 List<Bib> bibs = ds.retrieveBibs(bibIdList);
261 for (Bib bibRecord : bibs) {
262 bibMarcRecords.getRecords().addAll(bibMarcRecordProcessor.fromXML(bibRecord.getContent()).getRecords());
263 }
264 if (bibMarcRecords != null) {
265 contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
266 }
267 } else if (bibIdList != null && bibIdList.size() == 1) {
268 Bib bib = ds.retrieveBib(bibIdList.get(0));
269 bibMarcRecords = bibMarcRecordProcessor.fromXML(bib.getContent());
270 contentBuffer.append(bibMarcRecordProcessor.generateXML(bibMarcRecords.getRecords()));
271 }
272 return contentBuffer.toString();
273 }
274
275 private String retrieveBibTree(HttpServletRequest req) {
276 DocstoreService ds = BeanLocator.getDocstoreService();
277 String id = req.getParameter("bibId");
278 BibTree bibTree = ds.retrieveBibTree(id);
279 return bibTree.serialize(bibTree);
280 }
281
282 private String createBibTree(HttpServletRequest req) throws IOException {
283 DocstoreService ds = BeanLocator.getDocstoreService();
284 String requestBody = CharStreams.toString(req.getReader());
285 BibTree bibTree = new BibTree();
286 bibTree = (BibTree) bibTree.deserialize(requestBody);
287 ds.createBibTree(bibTree);
288 return responseTreeUrl + bibTree.getBib().getId();
289 }
290
291
292 private String processBibTrees(HttpServletRequest req) throws IOException {
293 DocstoreService ds = BeanLocator.getDocstoreService();
294 String requestBody = CharStreams.toString(req.getReader());
295 BibTrees bibTrees = new BibTrees();
296 bibTrees = (BibTrees) bibTrees.deserialize(requestBody);
297 bibTrees = ds.processBibTrees(bibTrees);
298 return bibTrees.serialize(bibTrees);
299 }
300
301 private String transferHoldings(HttpServletRequest req) {
302 DocstoreService ds = BeanLocator.getDocstoreService();
303
304 String bibId = req.getPathInfo().split("/")[2];
305 Map params = req.getParameterMap();
306 String[] splitHoldingIds = (String[]) params.get("holdingsId");
307 List<String> holdingsIds = new ArrayList<String>();
308 for (String holdingsId : splitHoldingIds) {
309 holdingsIds.add(holdingsId);
310 }
311 ds.transferHoldings(holdingsIds, bibId);
312 return "Success";
313 }
314
315 private String retrieveBibTrees(HttpServletRequest req) {
316 DocstoreService ds = BeanLocator.getDocstoreService();
317 List<String> bibIdList = new ArrayList<String>();
318 Map params = req.getParameterMap();
319 String[] bibIds = (String[]) params.get("bibId");
320 for (String bibId : bibIds) {
321 bibIdList.add(bibId);
322 }
323 return BibTrees.serialize(ds.retrieveBibTrees(bibIdList));
324 }
325
326 }