1 package org.kuali.ole.docstore.engine.service.rest;
2
3 import com.google.common.io.CharStreams;
4 import org.kuali.ole.docstore.common.document.*;
5 import org.kuali.ole.docstore.common.exception.DocstoreException;
6 import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
7 import org.kuali.ole.docstore.common.find.FindParams;
8 import org.kuali.ole.docstore.common.service.DocstoreService;
9 import org.kuali.ole.docstore.service.BeanLocator;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24
25
26
27
28
29
30
31 public class HoldingsRestServlet extends HttpServlet {
32
33 private static final Logger LOG = LoggerFactory.getLogger(HoldingsRestServlet.class);
34 private static String responseUrl = "documentrest/holdings/doc/";
35 private static String responseTreeUrl = "documentrest/holdings/doc/tree/";
36 private static String bindUrl = "bind";
37 private static String unbindUrl = "unbind";
38 private Logger logger = LoggerFactory.getLogger(HoldingsRestController.class);
39
40 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
41 PrintWriter out = resp.getWriter();
42 resp.setContentType("application/xml;charset=UTF-8");
43 String result = "";
44 try {
45 if (req.getPathInfo() != null && req.getPathInfo().contains("doc")) {
46 if (req.getPathInfo().startsWith("/doc/trees")) {
47 result = retrieveHoldingsDocTrees(req);
48 } else if (req.getPathInfo().startsWith("/doc/tree")) {
49 result = retrieveHoldingsTree(req);
50 } else if (req.getPathInfo().startsWith("/doc")) {
51 result = retrieveHoldings(req);
52 }
53 } else {
54 if (req.getPathInfo().startsWith("/tree")) {
55 result = retrieveHoldingsTrees(req);
56 }
57 }
58 out.print(result);
59 } catch (DocstoreException de) {
60 LOG.error("Docstore Exception :", de);
61 out.print(DocstoreExceptionProcessor.toXml(de));
62 } catch (Exception e) {
63 LOG.error("Exception :", e);
64 out.print(e);
65 }
66 }
67
68 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
69 PrintWriter out = resp.getWriter();
70 resp.setContentType("application/xml;charset=UTF-8");
71 String result = "";
72 try {
73 if (req.getPathInfo().startsWith("/doc/tree/find")) {
74 result = findHoldingsTree(req);
75 } else if (req.getPathInfo().startsWith("/doc/find")) {
76 result = findHoldings(req);
77 } else if (req.getPathInfo().contains("/bound")) {
78 String holdingsId = req.getPathInfo().split("/")[2];
79 boundWithBibs(holdingsId, req);
80 }else if (req.getPathInfo().contains("/unbind/one")) {
81 String bibId = req.getPathInfo().split("/")[2];
82 unbindWithOneBib(bibId, req);
83 }else if (req.getPathInfo().contains("/unbind/all")) {
84 String bibId = req.getPathInfo().split("/")[2];
85 unbindWithAllBibs(bibId, req);
86 } else if (req.getPathInfo().contains("/analytic")) {
87 String holdingsId = req.getPathInfo().split("/")[2];
88 createAnalyticsRelation(holdingsId, req);
89 } else if (req.getPathInfo().contains("/breakAnalytic")) {
90 String holdingsId = req.getPathInfo().split("/")[2];
91 breakAnalyticsRelation(holdingsId, req);
92 } else if (req.getPathInfo().contains("/transfer")) {
93 String holdingsId = req.getPathInfo().split("/")[2];
94 result = transferItems(holdingsId, req);
95 } else if (req.getPathInfo().startsWith("/doc/tree")) {
96 result = createHoldingsTree(req);
97 } else if (req.getPathInfo().startsWith("/doc")) {
98 result = createHoldings(req);
99 }
100 out.print(result);
101 } catch (DocstoreException de) {
102 LOG.error("Exception :", de);
103 out.print(DocstoreExceptionProcessor.toXml(de));
104 } catch (Exception e) {
105 LOG.error("Exception :", e);
106 out.print(e);
107 }
108 }
109
110 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
111 PrintWriter out = resp.getWriter();
112 resp.setContentType("application/xml;charset=UTF-8");
113 String result = "";
114 try {
115 if (req.getPathInfo().contains("/bulkUpdate")) {
116 result = bulkUpdateHoldings(req);
117 } else if (req.getPathInfo().startsWith("/doc")) {
118 result = updateHoldings(req);
119 }
120 out.print(result);
121 } catch (DocstoreException de) {
122 LOG.error("Exception :", de);
123 out.print(DocstoreExceptionProcessor.toXml(de));
124 } catch (Exception e) {
125 LOG.error("Exception :", e);
126 out.print(e);
127 }
128 }
129
130 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
131 PrintWriter out = resp.getWriter();
132 resp.setContentType("application/xml;charset=UTF-8");
133 String result = "";
134 try {
135 result = deleteHoldings(req);
136 out.print(result);
137 } catch (DocstoreException de) {
138 LOG.error("Exception :", de);
139 out.print(DocstoreExceptionProcessor.toXml(de));
140 } catch (Exception e) {
141 LOG.error("Exception :", e);
142 out.print(e);
143 }
144 }
145
146
147
148
149
150
151
152
153 public String createHoldings(HttpServletRequest req) throws IOException {
154 DocstoreService ds = BeanLocator.getDocstoreService();
155 String requestBody = CharStreams.toString(req.getReader());
156 Holdings holdings = new Holdings();
157 try {
158 holdings = (Holdings) holdings.deserialize(requestBody);
159 if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
160 holdings = new PHoldings();
161 holdings = (PHoldings) holdings.deserialize(requestBody);
162 } else {
163 holdings = new EHoldings();
164 holdings = (EHoldings) holdings.deserialize(requestBody);
165 }
166
167 ds.createHoldings(holdings);
168 } catch (DocstoreException e) {
169 LOG.error("Exception Occurred in createHoldings() :", e);
170 return DocstoreExceptionProcessor.toXml(e);
171 } catch (Exception exp) {
172 LOG.error("Exception Occurred in createHoldings() :", exp);
173 }
174 return responseUrl + holdings.getId();
175 }
176
177
178
179
180
181
182
183
184 public String retrieveHoldings(HttpServletRequest req) {
185 DocstoreService ds = BeanLocator.getDocstoreService();
186 String holdingsId = req.getParameter(("holdingsId"));
187 Holdings holdings = null;
188 try {
189 holdings = ds.retrieveHoldings(holdingsId);
190 } catch (DocstoreException e) {
191 LOG.error("Exception occurred in retrieveHoldings() :", e);
192 return DocstoreExceptionProcessor.toXml(e);
193 }
194 if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
195 holdings = ds.retrieveHoldings(holdingsId);
196 Holdings pHoldings = new Holdings();
197 return pHoldings.serialize(holdings);
198 } else {
199 holdings = ds.retrieveHoldings(holdingsId);
200 Holdings eHoldings = new EHoldings();
201 return eHoldings.serialize(holdings);
202 }
203 }
204
205
206
207
208
209
210
211
212
213 public String updateHoldings(HttpServletRequest req) throws IOException {
214 DocstoreService ds = BeanLocator.getDocstoreService();
215 String requestBody = CharStreams.toString(req.getReader());
216 Holdings holdings = new Holdings();
217 holdings = (Holdings) holdings.deserialize(requestBody);
218 if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
219 holdings = new PHoldings();
220 holdings = (PHoldings) holdings.deserialize(requestBody);
221 } else {
222 holdings = new EHoldings();
223 holdings = (EHoldings) holdings.deserialize(requestBody);
224 }
225 try {
226 ds.updateHoldings((holdings));
227 } catch (DocstoreException e) {
228 LOG.error("Exception occurred in updateHoldings() :", e);
229 return DocstoreExceptionProcessor.toXml(e);
230 }
231 return responseUrl + holdings.getId();
232 }
233
234
235
236
237
238
239
240
241 public String deleteHoldings(HttpServletRequest req) {
242 DocstoreService ds = BeanLocator.getDocstoreService();
243 String holdingsId = req.getParameter(("holdingsId"));
244 try {
245 ds.deleteHoldings(holdingsId);
246 } catch (DocstoreException e) {
247 LOG.error("Exception occurred in deleteHoldings() :", e);
248 return DocstoreExceptionProcessor.toXml(e);
249 }
250 return "Suceess";
251 }
252
253
254
255
256
257
258
259
260
261 public String createHoldingsTree(HttpServletRequest req) throws IOException {
262 DocstoreService ds = BeanLocator.getDocstoreService();
263 String requestBody = CharStreams.toString(req.getReader());
264 HoldingsTree holdingsTree = new HoldingsTree();
265 holdingsTree = (HoldingsTree) holdingsTree.deserialize(requestBody);
266 try {
267 ds.createHoldingsTree(holdingsTree);
268 } catch (DocstoreException e) {
269 LOG.error("Exception occurred in createHoldingsTree() :", e);
270 return DocstoreExceptionProcessor.toXml(e);
271 }
272 StringBuilder itemIds = new StringBuilder();
273 for (Item item : holdingsTree.getItems()) {
274 if (holdingsTree.getItems().get(holdingsTree.getItems().size() - 1) == item) {
275 itemIds.append(item.getId());
276 } else {
277 itemIds.append(item.getId() + "/");
278 }
279 }
280 return responseTreeUrl + holdingsTree.getHoldings().getId() + "/" + itemIds.toString();
281 }
282
283
284
285
286
287
288
289
290
291 public String retrieveHoldingsTree(HttpServletRequest req) throws IOException {
292 DocstoreService ds = BeanLocator.getDocstoreService();
293 String holdingsId = req.getParameter(("holdingsId"));
294
295 HoldingsTree holdingsTree = null;
296 try {
297 holdingsTree = ds.retrieveHoldingsTree(holdingsId);
298 } catch (DocstoreException e) {
299 LOG.error("Exception occurred in retrieveHoldingsTree() :", e);
300 return DocstoreExceptionProcessor.toXml(e);
301 }
302 return holdingsTree.serialize(holdingsTree);
303 }
304
305
306
307
308
309
310
311
312
313 public String retrieveHoldingsTrees(HttpServletRequest req) throws IOException {
314 Map params = req.getParameterMap();
315 String[] bibIds = (String[]) params.get("bibId");
316 DocstoreService ds = BeanLocator.getDocstoreService();
317 StringBuilder holdingsTrees = new StringBuilder("<holdingsTrees>");
318 for (String bibId : bibIds) {
319 try {
320 BibTree bibTree = ds.retrieveBibTree(bibId);
321 if (bibTree != null && bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
322 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
323 holdingsTrees.append("\n" + "<holdingsTree>").append("\n");
324 holdingsTrees.append(holdingsTree.getHoldings().getContent());
325
326 holdingsTrees.append("<items>").append("\n");
327 for (Item item : holdingsTree.getItems()) {
328 holdingsTrees.append(item.getContent()).append("\n");
329 }
330 holdingsTrees.append("</items>").append("\n");
331 holdingsTrees.append("</holdingsTree>").append("\n");
332 }
333
334 }
335 } catch (DocstoreException e) {
336 LOG.error("Exception occurred in retrieveHoldingsTrees :", e);
337 return DocstoreExceptionProcessor.toXml(e);
338 }
339 }
340 holdingsTrees.append("</holdingsTrees>");
341 return holdingsTrees.toString();
342 }
343
344
345
346
347
348
349
350
351
352 public String retrieveHoldingsDocTrees(HttpServletRequest req) throws IOException {
353 Map params = req.getParameterMap();
354 String[] bibIds = (String[]) params.get("bibId");
355 DocstoreService ds = BeanLocator.getDocstoreService();
356 HoldingsTrees holdingsTrees = new HoldingsTrees();
357 for (String bibId : bibIds) {
358 try {
359 BibTree bibTree = ds.retrieveBibTree(bibId);
360 if (bibTree != null && bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
361 holdingsTrees.getHoldingsTrees().addAll(bibTree.getHoldingsTrees());
362 }
363 } catch (DocstoreException e) {
364 LOG.error("Exception occurred in retrieveHoldingsDocTrees :", e);
365 return DocstoreExceptionProcessor.toXml(e);
366 }
367 }
368 return HoldingsTrees.serialize(holdingsTrees);
369 }
370
371
372
373
374
375
376 public String findHoldings(HttpServletRequest req) throws IOException {
377 DocstoreService ds = BeanLocator.getDocstoreService();
378 String requestBody = CharStreams.toString(req.getReader());
379 FindParams findParams = new FindParams();
380 findParams = (FindParams) findParams.deserialize(requestBody);
381 HashMap<String, String> hashMap = new HashMap();
382 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
383 for (FindParams.Map.Entry entry : entries) {
384 hashMap.put(entry.getKey(), entry.getValue());
385 }
386 Holdings holdings = null;
387 try {
388 holdings = ds.findHoldings(hashMap);
389 } catch (DocstoreException e) {
390 LOG.error("Exception occurred in findHoldings() :", e);
391 return DocstoreExceptionProcessor.toXml(e);
392 }
393 return holdings.serialize(holdings);
394 }
395
396
397
398
399
400
401 public String findHoldingsTree(HttpServletRequest req) throws IOException {
402 DocstoreService ds = BeanLocator.getDocstoreService();
403 String requestBody = CharStreams.toString(req.getReader());
404 FindParams findParams = new FindParams();
405 findParams = (FindParams) findParams.deserialize(requestBody);
406 HashMap<String, String> hashMap = new HashMap();
407 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
408 for (FindParams.Map.Entry entry : entries) {
409 hashMap.put(entry.getKey(), entry.getValue());
410 }
411 HoldingsTree holdings = null;
412 try {
413 holdings = ds.findHoldingsTree(hashMap);
414 } catch (DocstoreException e) {
415 LOG.error("Exception occurred in findHoldingsTree() :", e);
416 return DocstoreExceptionProcessor.toXml(e);
417 }
418 return holdings.serialize(holdings);
419 }
420
421
422
423
424
425
426
427 public String boundWithBibs(String holdingsId, HttpServletRequest req) throws IOException {
428 DocstoreService ds = BeanLocator.getDocstoreService();
429 String requestBody = CharStreams.toString(req.getReader());
430 if (requestBody.contains("[")) {
431 requestBody = requestBody.substring(1, requestBody.length() - 1);
432 }
433 String[] splitBibid = requestBody.split(",");
434 List<String> bibIds = new ArrayList<String>();
435 for (String bibId : splitBibid) {
436 bibIds.add(bibId);
437 }
438 try {
439 ds.boundHoldingsWithBibs(holdingsId, bibIds);
440 } catch (DocstoreException e) {
441 LOG.error("Exception occurred in boundWithBibs() :", e);
442 return DocstoreExceptionProcessor.toXml(e);
443 }
444 return responseUrl + holdingsId + bindUrl;
445 }
446
447
448
449
450
451
452
453 public String breakAnalyticsRelation(String seriesHoldingsId, HttpServletRequest req) throws IOException {
454 String requestBody = CharStreams.toString(req.getReader());
455 DocstoreService ds = BeanLocator.getDocstoreService();
456 if (requestBody.contains("[")) {
457 requestBody = requestBody.substring(1, requestBody.length() - 1);
458 }
459 String[] splitItemid = requestBody.split(",");
460 List<String> itemIds = new ArrayList<String>();
461 for (String itemId : splitItemid) {
462 itemIds.add(itemId);
463 }
464 try {
465 ds.breakAnalyticsRelation(seriesHoldingsId, itemIds);
466 } catch (DocstoreException e) {
467 LOG.error("Exception occurred in breakAnalyticsRelation() :", e);
468 return DocstoreExceptionProcessor.toXml(e);
469 }
470 return responseUrl + seriesHoldingsId + unbindUrl;
471 }
472
473
474
475
476
477
478
479 public String createAnalyticsRelation(String seriesHoldingsId, HttpServletRequest req) throws IOException {
480 String requestBody = CharStreams.toString(req.getReader());
481 DocstoreService ds = BeanLocator.getDocstoreService();
482 if (requestBody.contains("[")) {
483 requestBody = requestBody.substring(1, requestBody.length() - 1);
484 }
485 String[] splitItemid = requestBody.split(",");
486 List<String> itemIds = new ArrayList<String>();
487 for (String itemId : splitItemid) {
488 itemIds.add(itemId);
489 }
490 try {
491 ds.createAnalyticsRelation(seriesHoldingsId, itemIds);
492 } catch (DocstoreException e) {
493 LOG.error("Exception occurrred in createAnalyticsRelation() :", e);
494 return DocstoreExceptionProcessor.toXml(e);
495 }
496 return responseUrl + seriesHoldingsId + bindUrl;
497 }
498
499
500
501
502
503
504
505
506 public String transferItems(String holdingsId, HttpServletRequest req) throws IOException {
507 DocstoreService ds = BeanLocator.getDocstoreService();
508 Map params = req.getParameterMap();
509 String[] itemIds = (String[]) params.get("itemId");
510 List<String> itemsIds = new ArrayList<String>();
511 for (String itemId : itemIds) {
512 itemsIds.add(itemId);
513 }
514 try {
515 ds.transferItems(itemsIds, holdingsId);
516 } catch (DocstoreException e) {
517 LOG.error("Exception occurred in transferItems() :", e);
518 return DocstoreExceptionProcessor.toXml(e);
519 }
520 return "Success";
521 }
522
523
524
525
526
527
528 public String bulkUpdateHoldings(HttpServletRequest req) throws IOException {
529 DocstoreService ds = BeanLocator.getDocstoreService();
530 String requestBody = CharStreams.toString(req.getReader());
531 String[] bulkUpdateRequest = requestBody.split("\n", 3);
532
533 String holdingsId = bulkUpdateRequest[0];
534 String canUpdateStaffOnlyFlag = bulkUpdateRequest[1];
535 requestBody = bulkUpdateRequest[2];
536
537
538 String[] holdingItemIds = holdingsId.split(",");
539 List<String> holdingIds = new ArrayList<String>();
540 for (String itemId : holdingItemIds) {
541 holdingIds.add(itemId);
542 }
543 Holdings holdings = new Holdings();
544 holdings = (Holdings) holdings.deserialize(requestBody);
545 if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
546 holdings = new PHoldings();
547 holdings = (PHoldings) holdings.deserialize(requestBody);
548 } else {
549 holdings = new EHoldings();
550 holdings = (EHoldings) holdings.deserialize(requestBody);
551 }
552 try {
553 ds.bulkUpdateHoldings(holdings, holdingIds, canUpdateStaffOnlyFlag);
554 } catch (DocstoreException e) {
555 LOG.error("Exception occurred in bulkUpdateHoldings() :", e);
556 return DocstoreExceptionProcessor.toXml(e);
557 }
558 return "Success";
559 }
560
561 public String unbindWithOneBib(String bibId, HttpServletRequest req) throws IOException {
562 DocstoreService ds = BeanLocator.getDocstoreService();
563 String requestBody = CharStreams.toString(req.getReader());
564 if (requestBody.contains("[")) {
565 requestBody = requestBody.substring(1, requestBody.length() - 1);
566 }
567 String[] splitHoldingsid = requestBody.split(",");
568 List<String> holdingsIds = new ArrayList<String>();
569 for (String holdingsId : splitHoldingsid) {
570 holdingsIds.add(holdingsId);
571 }
572 try {
573 ds.unbindWithOneBib(holdingsIds, bibId);
574 } catch (DocstoreException e) {
575 LOG.error("Exception occurred in unbindWithOneBib() :", e);
576 return DocstoreExceptionProcessor.toXml(e);
577 }
578 return responseUrl + holdingsIds + unbindUrl;
579 }
580
581 public String unbindWithAllBibs(String bibId, HttpServletRequest req) throws IOException {
582 DocstoreService ds = BeanLocator.getDocstoreService();
583 String requestBody = CharStreams.toString(req.getReader());
584 if (requestBody.contains("[")) {
585 requestBody = requestBody.substring(1, requestBody.length() - 1);
586 }
587 String[] splitHoldingsid = requestBody.split(",");
588 List<String> holdingsIds = new ArrayList<String>();
589 for (String holdingsId : splitHoldingsid) {
590 holdingsIds.add(holdingsId);
591 }
592 try {
593 ds.unbindWithAllBibs(holdingsIds, bibId);
594 } catch (DocstoreException e) {
595 LOG.error("Exception occurred in unbindWithAllBibs() :", e);
596 return DocstoreExceptionProcessor.toXml(e);
597 }
598 return responseUrl + holdingsIds + unbindUrl;
599 }
600
601
602 }