View Javadoc
1   package org.kuali.ole.docstore.engine.service.rest;
2   
3   import org.kuali.ole.docstore.common.document.*;
4   import org.kuali.ole.docstore.common.exception.DocstoreException;
5   import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
6   import org.kuali.ole.docstore.common.find.FindParams;
7   import org.kuali.ole.docstore.common.service.DocstoreService;
8   import org.kuali.ole.docstore.service.BeanLocator;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  import org.springframework.stereotype.Controller;
12  import org.springframework.web.bind.annotation.*;
13  
14  import java.util.ArrayList;
15  import java.util.HashMap;
16  import java.util.List;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: jayabharathreddy
21   * Date: 12/18/13
22   * Time: 4:06 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  @Controller
26  @RequestMapping("/holdings")
27  public class HoldingsRestController extends AbstractRestService {
28  
29      private static final Logger LOG = LoggerFactory.getLogger(HoldingsRestController.class);
30      private static String responseUrl = "documentrest/holdings/doc/";
31      private static String responseTreeUrl = "documentrest/holdings/doc/tree/";
32      private static String bindUrl = "bind";
33      private static String unbindUrl = "unbind";
34      private Logger logger = LoggerFactory.getLogger(HoldingsRestController.class);
35  
36      @Override
37      @RequestMapping(value = "/doc/", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
38      @ResponseBody
39      public String createHoldings(@RequestBody String requestBody) {
40          DocstoreService ds = BeanLocator.getDocstoreService();
41          Holdings holdings = new Holdings();
42          try {
43          holdings = (Holdings) holdings.deserialize(requestBody);
44          if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
45              holdings = new PHoldings();
46              holdings = (PHoldings) holdings.deserialize(requestBody);
47          } else {
48              holdings = new EHoldings();
49              holdings = (EHoldings) holdings.deserialize(requestBody);
50          }
51  
52              ds.createHoldings(holdings);
53          } catch (DocstoreException e) {
54              LOG.error("Exception Occurred in createHoldings() :", e);
55              return DocstoreExceptionProcessor.toXml(e);
56          } catch (Exception exp) {
57              LOG.error("Exception Occurred in createHoldings() :", exp);
58          }
59          return responseUrl + holdings.getId();
60      }
61  
62      @Override
63      @RequestMapping(value = "/doc", method = RequestMethod.GET, produces = "application/xml")
64      @ResponseBody
65      public String retrieveHoldings(@RequestParam("holdingsId") String holdingsId) {
66          DocstoreService ds = BeanLocator.getDocstoreService();
67          Holdings holdings = null;
68          try {
69              holdings = ds.retrieveHoldings(holdingsId);
70          } catch (DocstoreException e) {
71              LOG.error("Exception occurred in retrieveHoldings() :", e);
72              return DocstoreExceptionProcessor.toXml(e);
73          }
74          if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
75              holdings = ds.retrieveHoldings(holdingsId);
76              Holdings pHoldings = new Holdings();
77              return pHoldings.serialize(holdings);
78          } else {
79              holdings = ds.retrieveHoldings(holdingsId);
80              Holdings eHoldings = new EHoldings();
81              return eHoldings.serialize(holdings);
82          }
83      }
84  
85      @Override
86      @RequestMapping(value = "/doc/", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/xml")
87      @ResponseBody
88      public String updateHoldings(@RequestBody String requestBody) {
89          DocstoreService ds = BeanLocator.getDocstoreService();
90          Holdings holdings = new Holdings();
91          holdings = (Holdings) holdings.deserialize(requestBody);
92          if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
93              holdings = new PHoldings();
94              holdings = (PHoldings) holdings.deserialize(requestBody);
95          } else {
96              holdings = new EHoldings();
97              holdings = (EHoldings) holdings.deserialize(requestBody);
98          }
99          try {
100             ds.updateHoldings((holdings));
101         } catch (DocstoreException e) {
102             LOG.error("Exception occurred in updateHoldings() :", e);
103             return DocstoreExceptionProcessor.toXml(e);
104         }
105         return responseUrl + holdings.getId();
106     }
107 
108     @Override
109     @RequestMapping(value = "/doc", method = RequestMethod.DELETE)
110     @ResponseBody
111     public String deleteHoldings(@RequestParam("holdingsId") String holdingsId) {
112         DocstoreService ds = BeanLocator.getDocstoreService();
113         try {
114             ds.deleteHoldings(holdingsId);
115         } catch (DocstoreException e) {
116             LOG.error("Exception occurred in deleteHoldings() :", e);
117             return DocstoreExceptionProcessor.toXml(e);
118         }
119         return "Suceess";
120     }
121 
122     @Override
123     @RequestMapping(value = "/doc/tree", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
124     @ResponseBody
125     public String createHoldingsTree(@RequestBody String requestBody) {
126         DocstoreService ds = BeanLocator.getDocstoreService();
127         HoldingsTree holdingsTree = new HoldingsTree();
128         holdingsTree = (HoldingsTree) holdingsTree.deserialize(requestBody);
129         try {
130             ds.createHoldingsTree(holdingsTree);
131         } catch (DocstoreException e) {
132             LOG.error("Exception occurred in createHoldingsTree()  :", e);
133             return DocstoreExceptionProcessor.toXml(e);
134         }
135         StringBuilder itemIds = new StringBuilder();
136         for (Item item : holdingsTree.getItems()) {
137             if (holdingsTree.getItems().get(holdingsTree.getItems().size() - 1) == item) {
138                 itemIds.append(item.getId());
139             } else {
140                 itemIds.append(item.getId() + "/");
141             }
142         }
143         return responseTreeUrl + holdingsTree.getHoldings().getId() + "/" + itemIds.toString();
144 
145     }
146 
147 
148 
149 
150     @Override
151     @RequestMapping(value = "/doc/tree", method = RequestMethod.GET, produces = "application/xml")
152     @ResponseBody
153     public String retrieveHoldingsTree(@RequestParam("holdingsId") String holdingsId) {
154         DocstoreService ds = BeanLocator.getDocstoreService();
155         HoldingsTree holdingsTree = null;
156         try {
157             holdingsTree = ds.retrieveHoldingsTree(holdingsId);
158         } catch (DocstoreException e) {
159             LOG.error("Exception occurred in retrieveHoldingsTree() :", e);
160             return DocstoreExceptionProcessor.toXml(e);
161         }
162         return holdingsTree.serialize(holdingsTree);
163     }
164 
165     @Override
166     @RequestMapping(value = "/tree", method = RequestMethod.GET, produces = "application/xml")
167     @ResponseBody
168     public String retrieveHoldingsTrees(@RequestParam("bibId") String[] bibIds) {
169         DocstoreService ds = BeanLocator.getDocstoreService();
170         StringBuilder holdingsTrees = new StringBuilder("<holdingsTrees>");
171         for (String bibId : bibIds) {
172             try {
173                 BibTree bibTree = ds.retrieveBibTree(bibId);
174                 if (bibTree != null && bibTree.getHoldingsTrees() != null && bibTree.getHoldingsTrees().size() > 0) {
175                     holdingsTrees.append("\n" + "<holdingsTree>").append("\n");
176                     for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
177                         holdingsTrees.append(holdingsTree.getHoldings().getContent());
178 
179                         holdingsTrees.append("<items>").append("\n");
180                         for (Item item : holdingsTree.getItems()) {
181                             holdingsTrees.append(item.getContent()).append("\n");
182                         }
183                         holdingsTrees.append("</items>").append("\n");
184                         holdingsTrees.append("</holdingsTree>").append("\n");
185                     }
186                 }
187             } catch (DocstoreException e) {
188                 LOG.error("Exception occurred in retrieveHoldingsTrees :", e);
189                 return DocstoreExceptionProcessor.toXml(e);
190             }
191         }
192         holdingsTrees.append("</holdingsTrees>");
193         String result = holdingsTrees.toString();
194         result = result.replaceAll("<shelvingOrder/>", "<shelvingOrder></shelvingOrder>");
195         result = result.replaceAll("<uri/>", "<uri></uri>");
196         result = result.replaceAll("<statisticalSearchingCode/>", "<statisticalSearchingCode></statisticalSearchingCode>");
197         result = result.replaceAll("<location/>", "<location></location>");
198         result = result.replaceAll("<donorInfo/>", "<donorInfo></donorInfo>");
199         result = result.replaceAll("<highDensityStorage/>", "<highDensityStorage></highDensityStorage>");
200         return result;
201     }
202 
203 
204     @Override
205     @RequestMapping(value = "/doc/find", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
206     @ResponseBody
207     public String findHoldings(@RequestBody String requestBody) {
208         DocstoreService ds = BeanLocator.getDocstoreService();
209         FindParams findParams = new FindParams();
210         findParams = (FindParams) findParams.deserialize(requestBody);
211         HashMap<String, String> hashMap = new HashMap();
212         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
213         for (FindParams.Map.Entry entry : entries) {
214             hashMap.put(entry.getKey(), entry.getValue());
215         }
216         Holdings holdings = null;
217         try {
218             holdings = ds.findHoldings(hashMap);
219         } catch (DocstoreException e) {
220             LOG.error("Exception occurred in findHoldings() :", e);
221             return DocstoreExceptionProcessor.toXml(e);
222         }
223         return holdings.serialize(holdings);
224     }
225 
226     @Override
227     @RequestMapping(value = "/doc/tree/find", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
228     @ResponseBody
229     public String findHoldingsTree(@RequestBody String requestBody) {
230         DocstoreService ds = BeanLocator.getDocstoreService();
231         FindParams findParams = new FindParams();
232         findParams = (FindParams) findParams.deserialize(requestBody);
233         HashMap<String, String> hashMap = new HashMap();
234         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
235         for (FindParams.Map.Entry entry : entries) {
236             hashMap.put(entry.getKey(), entry.getValue());
237         }
238         HoldingsTree holdings = null;
239         try {
240             holdings = ds.findHoldingsTree(hashMap);
241         } catch (DocstoreException e) {
242             LOG.error("Exception occurred in findHoldingsTree() :", e);
243             return DocstoreExceptionProcessor.toXml(e);
244         }
245         return holdings.serialize(holdings);
246     }
247 
248     @Override
249     @RequestMapping(value = "/doc/{holdingsId}/bound", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
250     @ResponseBody
251     public String boundWithBibs(@PathVariable("holdingsId") String holdingsId, @RequestBody String requestBody) {
252         DocstoreService ds = BeanLocator.getDocstoreService();
253         if (requestBody.contains("[")) {
254             requestBody = requestBody.substring(1, requestBody.length() - 1);
255         }
256         String[] splitBibid = requestBody.split(",");
257         List<String> bibIds = new ArrayList<>();
258         for (String bibId : splitBibid) {
259             bibIds.add(bibId);
260         }
261         try {
262             ds.boundHoldingsWithBibs(holdingsId, bibIds);
263         } catch (DocstoreException e) {
264             LOG.error("Exception occurred in boundWithBibs() :", e);
265             return DocstoreExceptionProcessor.toXml(e);
266         }
267         return responseUrl + holdingsId + bindUrl;
268     }
269 
270     @RequestMapping(value = "/doc/{seriesHoldingsId}/breakAnalytic", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
271     @ResponseBody
272     public String breakAnalyticsRelation(@PathVariable("seriesHoldingsId") String seriesHoldingsId, @RequestBody String requestBody) {
273         DocstoreService ds = BeanLocator.getDocstoreService();
274         if (requestBody.contains("[")) {
275             requestBody = requestBody.substring(1, requestBody.length() - 1);
276         }
277         String[] splitItemid = requestBody.split(",");
278         List<String> itemIds = new ArrayList<>();
279         for (String itemId : splitItemid) {
280             itemIds.add(itemId);
281         }
282         try {
283             ds.breakAnalyticsRelation(seriesHoldingsId, itemIds);
284         } catch (DocstoreException e) {
285             LOG.error("Exception occurred in breakAnalyticsRelation() :", e);
286             return DocstoreExceptionProcessor.toXml(e);
287         }
288         return responseUrl + seriesHoldingsId + unbindUrl;
289     }
290 
291     @RequestMapping(value = "/doc/{seriesHoldingsId}/analytic", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
292     @ResponseBody
293     public String createAnalyticsRelation(@PathVariable("seriesHoldingsId") String seriesHoldingsId, @RequestBody String requestBody) {
294         DocstoreService ds = BeanLocator.getDocstoreService();
295         if (requestBody.contains("[")) {
296             requestBody = requestBody.substring(1, requestBody.length() - 1);
297         }
298         String[] splitItemid = requestBody.split(",");
299         List<String> itemIds = new ArrayList<>();
300         for (String itemId : splitItemid) {
301             itemIds.add(itemId);
302         }
303         try {
304             ds.createAnalyticsRelation(seriesHoldingsId, itemIds);
305         } catch (DocstoreException e) {
306             LOG.error("Exception occurrred in createAnalyticsRelation() :", e);
307             return DocstoreExceptionProcessor.toXml(e);
308         }
309         return responseUrl + seriesHoldingsId + bindUrl;
310     }
311 
312 
313     @RequestMapping(value = "/doc/{holdingsId}/transfer", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
314     @ResponseBody
315     public String transferItems(@PathVariable("holdingsId") String holdingsId,  @RequestParam("itemId") String[] itemIds) {
316         DocstoreService ds = BeanLocator.getDocstoreService();
317         List<String> itemsIds = new ArrayList<>();
318         for (String itemId : itemIds) {
319             itemsIds.add(itemId);
320         }
321         try {
322             ds.transferItems(itemsIds, holdingsId);
323         } catch (DocstoreException e) {
324             LOG.error("Exception occurred in transferItems() :", e);
325             return DocstoreExceptionProcessor.toXml(e);
326         }
327         return "Success";
328     }
329 
330     @RequestMapping(value = "/doc/bulkUpdate", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/xml")
331     @ResponseBody
332     public String bulkUpdateHoldings( @RequestBody String requestBody) {
333         DocstoreService ds = BeanLocator.getDocstoreService();
334 
335         String[] bulkUpdateRequest = requestBody.split("\n", 3);
336 
337         String holdingsId = bulkUpdateRequest[0];
338         String canUpdateStaffOnlyFlag = bulkUpdateRequest[1];
339         requestBody = bulkUpdateRequest[2];
340 
341 
342         String[] holdingItemIds = holdingsId.split(",");
343         List<String> holdingIds = new ArrayList<>();
344         for (String itemId : holdingItemIds) {
345             holdingIds.add(itemId);
346         }
347         Holdings holdings = new Holdings();
348         holdings = (Holdings) holdings.deserialize(requestBody);
349         if (holdings.getHoldingsType().equalsIgnoreCase("print")) {
350             holdings = new PHoldings();
351             holdings = (PHoldings) holdings.deserialize(requestBody);
352         } else {
353             holdings = new EHoldings();
354             holdings = (EHoldings) holdings.deserialize(requestBody);
355         }
356         try {
357             ds.bulkUpdateHoldings(holdings, holdingIds,canUpdateStaffOnlyFlag);
358         } catch (DocstoreException e) {
359             LOG.error("Exception occurred in bulkUpdateHoldings() :", e);
360             return DocstoreExceptionProcessor.toXml(e);
361         }
362         return "Success";
363     }
364 
365 
366 }