View Javadoc
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   * Created with IntelliJ IDEA.
26   * User: jayabharathreddy
27   * Date: 6/16/14
28   * Time: 12:43 PM
29   * To change this template use File | Settings | File Templates.
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("/analytic")) {
81                  String holdingsId = req.getPathInfo().split("/")[2];
82                  createAnalyticsRelation(holdingsId, req);
83              } else if (req.getPathInfo().contains("/breakAnalytic")) {
84                  String holdingsId = req.getPathInfo().split("/")[2];
85                  breakAnalyticsRelation(holdingsId, req);
86              } else if (req.getPathInfo().contains("/transfer")) {
87                  String holdingsId = req.getPathInfo().split("/")[2];
88                  result = transferItems(holdingsId, req);
89              }  else if (req.getPathInfo().startsWith("/doc/tree")) {
90                  result = createHoldingsTree(req);
91              } else if (req.getPathInfo().startsWith("/doc")) {
92                  result = createHoldings(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     protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
105         PrintWriter out = resp.getWriter();
106         resp.setContentType("application/xml;charset=UTF-8");
107         String result = "";
108         try {
109             if (req.getPathInfo().contains("/bulkUpdate")) {
110                 result = bulkUpdateHoldings(req);
111             } else if (req.getPathInfo().startsWith("/doc")) {
112                 result = updateHoldings(req);
113             }
114             out.print(result);
115         } catch (DocstoreException de) {
116             LOG.error("Exception :", de);
117             out.print(DocstoreExceptionProcessor.toXml(de));
118         } catch (Exception e) {
119             LOG.error("Exception :", e);
120             out.print(e);
121         }
122     }
123 
124     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
125         PrintWriter out = resp.getWriter();
126         resp.setContentType("application/xml;charset=UTF-8");
127         String result = "";
128         try {
129             result = deleteHoldings(req);
130             out.print(result);
131         } catch (DocstoreException de) {
132             LOG.error("Exception :", de);
133             out.print(DocstoreExceptionProcessor.toXml(de));
134         } catch (Exception e) {
135             LOG.error("Exception :", e);
136             out.print(e);
137         }
138     }
139 
140     /**
141      * Creating Holdings
142      *
143      * @param req
144      * @return
145      * @throws IOException
146      */
147     public String createHoldings(HttpServletRequest req) throws IOException {
148         DocstoreService ds = BeanLocator.getDocstoreService();
149         String requestBody = CharStreams.toString(req.getReader());
150         Holdings holdings = new Holdings();
151         try {
152             holdings = (Holdings) holdings.deserialize(requestBody);
153             if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
154                 holdings = new PHoldings();
155                 holdings = (PHoldings) holdings.deserialize(requestBody);
156             } else {
157                 holdings = new EHoldings();
158                 holdings = (EHoldings) holdings.deserialize(requestBody);
159             }
160 
161             ds.createHoldings(holdings);
162         } catch (DocstoreException e) {
163             LOG.error("Exception Occurred in createHoldings() :", e);
164             return DocstoreExceptionProcessor.toXml(e);
165         } catch (Exception exp) {
166             LOG.error("Exception Occurred in createHoldings() :", exp);
167         }
168         return responseUrl + holdings.getId();
169     }
170 
171 
172     /**
173      * Retrieve Holdings
174      *
175      * @param req
176      * @return
177      */
178     public String retrieveHoldings(HttpServletRequest req) {
179         DocstoreService ds = BeanLocator.getDocstoreService();
180         String holdingsId = req.getParameter(("holdingsId"));
181         Holdings holdings = null;
182         try {
183             holdings = ds.retrieveHoldings(holdingsId);
184         } catch (DocstoreException e) {
185             LOG.error("Exception occurred in retrieveHoldings() :", e);
186             return DocstoreExceptionProcessor.toXml(e);
187         }
188         if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
189             holdings = ds.retrieveHoldings(holdingsId);
190             Holdings pHoldings = new Holdings();
191             return pHoldings.serialize(holdings);
192         } else {
193             holdings = ds.retrieveHoldings(holdingsId);
194             Holdings eHoldings = new EHoldings();
195             return eHoldings.serialize(holdings);
196         }
197     }
198 
199     /**
200      * Update Holdings
201      *
202      * @param req
203      * @return
204      * @throws IOException
205      */
206 
207     public String updateHoldings(HttpServletRequest req) throws IOException {
208         DocstoreService ds = BeanLocator.getDocstoreService();
209         String requestBody = CharStreams.toString(req.getReader());
210         Holdings holdings = new Holdings();
211         holdings = (Holdings) holdings.deserialize(requestBody);
212         if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
213             holdings = new PHoldings();
214             holdings = (PHoldings) holdings.deserialize(requestBody);
215         } else {
216             holdings = new EHoldings();
217             holdings = (EHoldings) holdings.deserialize(requestBody);
218         }
219         try {
220             ds.updateHoldings((holdings));
221         } catch (DocstoreException e) {
222             LOG.error("Exception occurred in updateHoldings() :", e);
223             return DocstoreExceptionProcessor.toXml(e);
224         }
225         return responseUrl + holdings.getId();
226     }
227 
228 
229     /**
230      * Delete Holdings
231      *
232      * @param req
233      * @return
234      */
235     public String deleteHoldings(HttpServletRequest req) {
236         DocstoreService ds = BeanLocator.getDocstoreService();
237         String holdingsId = req.getParameter(("holdingsId"));
238         try {
239             ds.deleteHoldings(holdingsId);
240         } catch (DocstoreException e) {
241             LOG.error("Exception occurred in deleteHoldings() :", e);
242             return DocstoreExceptionProcessor.toXml(e);
243         }
244         return "Suceess";
245     }
246 
247 
248     /**
249      * Create Holdings doc Tree
250      *
251      * @param req
252      * @return
253      * @throws IOException
254      */
255     public String createHoldingsTree(HttpServletRequest req) throws IOException {
256         DocstoreService ds = BeanLocator.getDocstoreService();
257         String requestBody = CharStreams.toString(req.getReader());
258         HoldingsTree holdingsTree = new HoldingsTree();
259         holdingsTree = (HoldingsTree) holdingsTree.deserialize(requestBody);
260         try {
261             ds.createHoldingsTree(holdingsTree);
262         } catch (DocstoreException e) {
263             LOG.error("Exception occurred in createHoldingsTree()  :", e);
264             return DocstoreExceptionProcessor.toXml(e);
265         }
266         StringBuilder itemIds = new StringBuilder();
267         for (Item item : holdingsTree.getItems()) {
268             if (holdingsTree.getItems().get(holdingsTree.getItems().size() - 1) == item) {
269                 itemIds.append(item.getId());
270             } else {
271                 itemIds.append(item.getId() + "/");
272             }
273         }
274         return responseTreeUrl + holdingsTree.getHoldings().getId() + "/" + itemIds.toString();
275     }
276 
277 
278     /**
279      * Retrieve Holdings Tree
280      *
281      * @param req
282      * @return
283      * @throws IOException
284      */
285     public String retrieveHoldingsTree(HttpServletRequest req) throws IOException {
286         DocstoreService ds = BeanLocator.getDocstoreService();
287         String holdingsId = req.getParameter(("holdingsId"));
288 
289         HoldingsTree holdingsTree = null;
290         try {
291             holdingsTree = ds.retrieveHoldingsTree(holdingsId);
292         } catch (DocstoreException e) {
293             LOG.error("Exception occurred in retrieveHoldingsTree() :", e);
294             return DocstoreExceptionProcessor.toXml(e);
295         }
296         return holdingsTree.serialize(holdingsTree);
297     }
298 
299     /**
300      * Retrieve Holdings Trees
301      *
302      * @param req
303      * @return
304      * @throws IOException
305      */
306 
307     public String retrieveHoldingsTrees(HttpServletRequest req) throws IOException {
308         Map params = req.getParameterMap();
309         String[] bibIds = (String[]) params.get("bibId");
310         DocstoreService ds = BeanLocator.getDocstoreService();
311         StringBuilder holdingsTrees = new StringBuilder("<holdingsTrees>");
312         for (String bibId : bibIds) {
313             try {
314                 BibTree bibTree = ds.retrieveBibTree(bibId);
315                 if (bibTree != null && bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
316                     for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
317                         holdingsTrees.append("\n" + "<holdingsTree>").append("\n");
318                         holdingsTrees.append(holdingsTree.getHoldings().getContent());
319 
320                         holdingsTrees.append("<items>").append("\n");
321                         for (Item item : holdingsTree.getItems()) {
322                             holdingsTrees.append(item.getContent()).append("\n");
323                         }
324                         holdingsTrees.append("</items>").append("\n");
325                         holdingsTrees.append("</holdingsTree>").append("\n");
326                     }
327 
328                 }
329             } catch (DocstoreException e) {
330                 LOG.info("Exception occurred in retrieveHoldingsTrees :" + e.getErrorCode() + " , " + e.getErrorParams());
331                 holdingsTrees.append("<noBib><id>"+bibId+"</id></noBib>");
332              }
333         }
334         holdingsTrees.append("</holdingsTrees>");
335         return holdingsTrees.toString();
336     }
337 
338     /**
339      * Retrieve Holdings Doc Trees
340      *
341      * @param req
342      * @return
343      * @throws IOException
344      */
345 
346     public String retrieveHoldingsDocTrees(HttpServletRequest req) throws IOException {
347         Map params = req.getParameterMap();
348         String[] bibIds = (String[]) params.get("bibId");
349         DocstoreService ds = BeanLocator.getDocstoreService();
350         HoldingsTrees holdingsTrees = new HoldingsTrees();
351         for (String bibId : bibIds) {
352             try {
353                 BibTree bibTree = ds.retrieveBibTree(bibId);
354                 if (bibTree != null && bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
355                  holdingsTrees.getHoldingsTrees().addAll(bibTree.getHoldingsTrees());
356                 }
357             } catch (DocstoreException e) {
358                 LOG.error("Exception occurred in retrieveHoldingsDocTrees :", e);
359                 return DocstoreExceptionProcessor.toXml(e);
360             }
361         }
362         return HoldingsTrees.serialize(holdingsTrees);
363     }
364 
365 
366     /**
367      * @param req
368      * @return
369      */
370     public String findHoldings(HttpServletRequest req) throws IOException {
371         DocstoreService ds = BeanLocator.getDocstoreService();
372         String requestBody = CharStreams.toString(req.getReader());
373         FindParams findParams = new FindParams();
374         findParams = (FindParams) findParams.deserialize(requestBody);
375         HashMap<String, String> hashMap = new HashMap();
376         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
377         for (FindParams.Map.Entry entry : entries) {
378             hashMap.put(entry.getKey(), entry.getValue());
379         }
380         Holdings holdings = null;
381         try {
382             holdings = ds.findHoldings(hashMap);
383         } catch (DocstoreException e) {
384             LOG.error("Exception occurred in findHoldings() :", e);
385             return DocstoreExceptionProcessor.toXml(e);
386         }
387         return holdings.serialize(holdings);
388     }
389 
390     /**
391      * @param req
392      * @return
393      * @throws IOException
394      */
395     public String findHoldingsTree(HttpServletRequest req) throws IOException {
396         DocstoreService ds = BeanLocator.getDocstoreService();
397         String requestBody = CharStreams.toString(req.getReader());
398         FindParams findParams = new FindParams();
399         findParams = (FindParams) findParams.deserialize(requestBody);
400         HashMap<String, String> hashMap = new HashMap();
401         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
402         for (FindParams.Map.Entry entry : entries) {
403             hashMap.put(entry.getKey(), entry.getValue());
404         }
405         HoldingsTree holdings = null;
406         try {
407             holdings = ds.findHoldingsTree(hashMap);
408         } catch (DocstoreException e) {
409             LOG.error("Exception occurred in findHoldingsTree() :", e);
410             return DocstoreExceptionProcessor.toXml(e);
411         }
412         return holdings.serialize(holdings);
413     }
414 
415     /**
416      * @param holdingsId
417      * @param req
418      * @return
419      * @throws IOException
420      */
421     public String boundWithBibs(String holdingsId, HttpServletRequest req) throws IOException {
422         DocstoreService ds = BeanLocator.getDocstoreService();
423         String requestBody = CharStreams.toString(req.getReader());
424         if (requestBody.contains("[")) {
425             requestBody = requestBody.substring(1, requestBody.length() - 1);
426         }
427         String[] splitBibid = requestBody.split(",");
428         List<String> bibIds = new ArrayList<String>();
429         for (String bibId : splitBibid) {
430             bibIds.add(bibId);
431         }
432         try {
433             ds.boundHoldingsWithBibs(holdingsId, bibIds);
434         } catch (DocstoreException e) {
435             LOG.error("Exception occurred in boundWithBibs() :", e);
436             return DocstoreExceptionProcessor.toXml(e);
437         }
438         return responseUrl + holdingsId + bindUrl;
439     }
440 
441     /**
442      * @param seriesHoldingsId
443      * @param req
444      * @return
445      * @throws IOException
446      */
447     public String breakAnalyticsRelation(String seriesHoldingsId, HttpServletRequest req) throws IOException {
448         String requestBody = CharStreams.toString(req.getReader());
449         DocstoreService ds = BeanLocator.getDocstoreService();
450         if (requestBody.contains("[")) {
451             requestBody = requestBody.substring(1, requestBody.length() - 1);
452         }
453         String[] splitItemid = requestBody.split(",");
454         List<String> itemIds = new ArrayList<String>();
455         for (String itemId : splitItemid) {
456             itemIds.add(itemId);
457         }
458         try {
459             ds.breakAnalyticsRelation(seriesHoldingsId, itemIds);
460         } catch (DocstoreException e) {
461             LOG.error("Exception occurred in breakAnalyticsRelation() :", e);
462             return DocstoreExceptionProcessor.toXml(e);
463         }
464         return responseUrl + seriesHoldingsId + unbindUrl;
465     }
466 
467     /**
468      * @param seriesHoldingsId
469      * @param req
470      * @return
471      * @throws IOException
472      */
473     public String createAnalyticsRelation(String seriesHoldingsId, HttpServletRequest req) throws IOException {
474         String requestBody = CharStreams.toString(req.getReader());
475         DocstoreService ds = BeanLocator.getDocstoreService();
476         if (requestBody.contains("[")) {
477             requestBody = requestBody.substring(1, requestBody.length() - 1);
478         }
479         String[] splitItemid = requestBody.split(",");
480         List<String> itemIds = new ArrayList<String>();
481         for (String itemId : splitItemid) {
482             itemIds.add(itemId);
483         }
484         try {
485             ds.createAnalyticsRelation(seriesHoldingsId, itemIds);
486         } catch (DocstoreException e) {
487             LOG.error("Exception occurrred in createAnalyticsRelation() :", e);
488             return DocstoreExceptionProcessor.toXml(e);
489         }
490         return responseUrl + seriesHoldingsId + bindUrl;
491     }
492 
493 
494     /**
495      * @param holdingsId
496      * @param req
497      * @return
498      * @throws IOException
499      */
500     public String transferItems(String holdingsId, HttpServletRequest req) throws IOException {
501         DocstoreService ds = BeanLocator.getDocstoreService();
502         Map params = req.getParameterMap();
503         String[] itemIds = (String[]) params.get("itemId");
504         List<String> itemsIds = new ArrayList<String>();
505         for (String itemId : itemIds) {
506             itemsIds.add(itemId);
507         }
508         try {
509             ds.transferItems(itemsIds, holdingsId);
510         } catch (DocstoreException e) {
511             LOG.error("Exception occurred in transferItems() :", e);
512             return DocstoreExceptionProcessor.toXml(e);
513         }
514         return "Success";
515     }
516 
517     /**
518      * @param req
519      * @return
520      * @throws IOException
521      */
522     public String bulkUpdateHoldings(HttpServletRequest req) throws IOException {
523         DocstoreService ds = BeanLocator.getDocstoreService();
524         String requestBody = CharStreams.toString(req.getReader());
525         String[] bulkUpdateRequest = requestBody.split("\n", 3);
526 
527         String holdingsId = bulkUpdateRequest[0];
528         String canUpdateStaffOnlyFlag = bulkUpdateRequest[1];
529         requestBody = bulkUpdateRequest[2];
530 
531 
532         String[] holdingItemIds = holdingsId.split(",");
533         List<String> holdingIds = new ArrayList<String>();
534         for (String itemId : holdingItemIds) {
535             holdingIds.add(itemId);
536         }
537         Holdings holdings = new Holdings();
538         holdings = (Holdings) holdings.deserialize(requestBody);
539         if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
540             holdings = new PHoldings();
541             holdings = (PHoldings) holdings.deserialize(requestBody);
542         } else {
543             holdings = new EHoldings();
544             holdings = (EHoldings) holdings.deserialize(requestBody);
545         }
546         try {
547             ds.bulkUpdateHoldings(holdings, holdingIds, canUpdateStaffOnlyFlag);
548         } catch (DocstoreException e) {
549             LOG.error("Exception occurred in bulkUpdateHoldings() :", e);
550             return DocstoreExceptionProcessor.toXml(e);
551         }
552         return "Success";
553     }
554 
555 
556 }