View Javadoc
1   package org.kuali.ole.docstore.engine.service.rest;
2   
3   import com.thoughtworks.xstream.converters.ConversionException;
4   import org.kuali.ole.DocumentUniqueIDPrefix;
5   import org.kuali.ole.docstore.common.document.*;
6   import org.kuali.ole.docstore.common.document.Item;
7   import org.kuali.ole.docstore.common.document.Items;
8   import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
9   import org.kuali.ole.docstore.common.document.content.bib.marc.OrderBibMarcRecord;
10  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
11  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
12  import org.kuali.ole.docstore.common.document.ids.BibId;
13  import org.kuali.ole.docstore.common.document.ids.BibIds;
14  import org.kuali.ole.docstore.common.document.ids.HoldingsId;
15  import org.kuali.ole.docstore.common.document.ids.ItemIds;
16  import org.kuali.ole.docstore.common.exception.*;
17  import org.kuali.ole.docstore.common.find.FindParams;
18  import org.kuali.ole.docstore.common.service.DocstoreService;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.ole.docstore.service.BeanLocator;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.web.bind.annotation.*;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  
30  /**
31   * Created with IntelliJ IDEA.
32   * User: jayabharathreddy
33   * Date: 12/18/13
34   * Time: 4:07 PM
35   * To change this template use File | Settings | File Templates.
36   */
37  @Controller
38  @RequestMapping("/item")
39  public class ItemsRestController extends AbstractRestService {
40  
41      private static final Logger LOG = LoggerFactory.getLogger(ItemsRestController.class);
42      private static String responseUrl = "documentrest/item/doc/";
43      private Logger logger = LoggerFactory.getLogger(ItemsRestController.class);
44      public static BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
45  
46      @Override
47      @RequestMapping(value = "/doc/", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
48      @ResponseBody
49      public String createItem(@RequestBody String requestBody) {
50          DocstoreService ds = BeanLocator.getDocstoreService();
51          ItemOleml item = new ItemOleml();
52          item = (ItemOleml) item.deserialize(requestBody);
53          try {
54              ds.createItem(item);
55          } catch (DocstoreException e) {
56              LOG.info("Exception :", e);
57              return DocstoreExceptionProcessor.toXml(e);
58          }
59          return responseUrl + item.getId();
60      }
61  
62  
63      @RequestMapping(value = "/doc", method = RequestMethod.GET, produces = "application/xml")
64      @ResponseBody
65      public String retrieveItems(@RequestParam("itemId") String[] itemsIds) {
66          DocstoreService ds = BeanLocator.getDocstoreService();
67          if(itemsIds.length == 1) {
68              Item item = null;
69              try {
70                  item = ds.retrieveItem(itemsIds[0]);
71              } catch (DocstoreException e) {
72                  LOG.info("Exception :", e);
73                  return DocstoreExceptionProcessor.toXml(e);
74              }
75              ItemOleml items = new ItemOleml();
76              return items.serialize(item);
77  
78          }
79          else {
80              List<String> itemsIdList = new ArrayList<>();
81              for (String id : itemsIds) {
82                  itemsIdList.add(id);
83              }
84              List<Item> items = null;
85              try {
86                  items = ds.retrieveItems(itemsIdList);
87              } catch (DocstoreException e) {
88                  LOG.info("Exception :", e);
89                  return DocstoreExceptionProcessor.toXml(e);
90              }
91              Items itemsObj = new Items();
92              itemsObj.getItems().addAll(items);
93              return Items.serialize(itemsObj);
94          }
95  
96      }
97  
98      @RequestMapping(value = "/doc/map", method = RequestMethod.GET, produces = "application/xml")
99      @ResponseBody
100     public String retrieveItemMap(@RequestParam("itemId") String[] itemsIds) {
101         DocstoreService ds = BeanLocator.getDocstoreService();
102             List<String> itemsIdList = new ArrayList<>();
103             for (String id : itemsIds) {
104                 itemsIdList.add(id);
105             }
106             HashMap<String,Item> items = null;
107             try {
108                 items = ds.retrieveItemMap(itemsIdList);
109             } catch (DocstoreException e) {
110                 LOG.info("Exception :", e);
111                 return DocstoreExceptionProcessor.toXml(e);
112             }
113             ItemMap itemsObj = new ItemMap();
114             itemsObj.getItemMap().putAll(items);
115             return ItemMap.serialize(itemsObj);
116     }
117 
118 
119     @Override
120     @RequestMapping(value = "/holdings", method = RequestMethod.GET, produces = "application/xml")
121     @ResponseBody
122     public String retrieveHoldingsByBarcode(@RequestParam("barcode") String[] barcodes) {
123         DocstoreService ds = BeanLocator.getDocstoreService();
124         StringBuilder holdingsBuilder = new StringBuilder();
125         holdingsBuilder.append("<holdings>").append("\n");
126         for (String barcode2 : barcodes) {
127             Item item = null;
128             try {
129                 item = ds.retrieveItemByBarcode(barcode2);
130             } catch (DocstoreException e) {
131                 LOG.info("Exception :", e);
132                 return DocstoreExceptionProcessor.toXml(e);
133             }
134             if (item != null && null != item.getHolding()) {
135                 String holdingsId = item.getHolding().getId();
136                 if (holdingsId != null) {
137                     Holdings holdings = ds.retrieveHoldings(holdingsId);
138                     holdingsBuilder.append(holdings.getContent()).append("\n");
139                 }
140             }
141         }
142         holdingsBuilder.append("</holdings>");
143         return holdingsBuilder.toString();
144     }
145 
146     @Override
147     @RequestMapping(method = RequestMethod.GET, produces = "application/xml")
148     @ResponseBody
149     public String retrieveItemByBarcode(@RequestParam("barcode") String barcode) {
150         DocstoreService ds = BeanLocator.getDocstoreService();
151         Item item = new Item();
152         if (StringUtils.isNotBlank(barcode)) {
153             try {
154                 item = ds.retrieveItemByBarcode(barcode);
155             } catch (DocstoreException e) {
156                 LOG.info("Exception :", e);
157                 return DocstoreExceptionProcessor.toXml(e);
158             }
159         }
160         return item.getContent();
161     }
162 
163     @Override
164     @RequestMapping(method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
165     @ResponseBody
166     public String updateItemByBarcodeWithContent(@RequestParam("barcode") String[] barcodes, @RequestBody String requestBody) {
167         Item existingItemDoc = null;
168         try {
169             DocstoreService ds = BeanLocator.getDocstoreService();
170             Item incomingItemDoc = new Item();
171             incomingItemDoc.setContent(requestBody);
172             for (String barcode : barcodes) {
173                 existingItemDoc = ds.retrieveItemByBarcode(barcode);
174                 existingItemDoc.setItem(incomingItemDoc);
175                 ds.updateItem(existingItemDoc);
176             }
177         } catch (DocstoreException e) {
178             LOG.info("Exception :", e);
179             return DocstoreExceptionProcessor.toXml(e);
180         } catch (ConversionException e) {
181             LOG.info("Exception :", e);
182             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
183         }
184         return existingItemDoc.serialize(existingItemDoc);
185     }
186 
187 
188     @Override
189     @RequestMapping(value = "/doc", method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
190     @ResponseBody
191     public String updateItemByBarcode(@RequestParam("barcode") String[] barcodes, @RequestBody String requestBody) {
192         Item existingItemDoc = null;
193         Item incomingItemDoc = new Item();
194         try {
195             DocstoreService ds = BeanLocator.getDocstoreService();
196             incomingItemDoc = (Item) incomingItemDoc.deserialize(requestBody);
197             for (String barcode : barcodes) {
198                 existingItemDoc = ds.retrieveItemByBarcode(barcode);
199                 existingItemDoc.setItem(incomingItemDoc);
200                 ds.updateItem(existingItemDoc);
201             }
202         } catch (DocstoreException e) {
203             LOG.info("Exception :", e);
204             return DocstoreExceptionProcessor.toXml(e);
205         } catch (ConversionException e) {
206             LOG.info("Exception :", e);
207             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
208         }
209         return existingItemDoc.serialize(existingItemDoc);
210     }
211 
212     @Override
213     @RequestMapping(value = "/doc/", method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
214     @ResponseBody
215     public String patchItem(@RequestBody String requestBody) {
216         Item existingItemDoc = null;
217         Item incomingItemDoc = new Item();
218         try {
219             DocstoreService ds = BeanLocator.getDocstoreService();
220             incomingItemDoc = (Item) incomingItemDoc.deserialize(requestBody);
221             existingItemDoc = ds.retrieveItem(incomingItemDoc.getId());
222             existingItemDoc.setItem(incomingItemDoc);
223             ds.updateItem(existingItemDoc);
224         } catch (DocstoreException e) {
225             LOG.info("Exception :", e);
226             return DocstoreExceptionProcessor.toXml(e);
227         } catch (ConversionException e) {
228             LOG.info("Exception :", e);
229             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
230         }
231         return incomingItemDoc.serialize(incomingItemDoc);
232     }
233 
234     @Override
235     @RequestMapping(value = "/", method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
236     @ResponseBody
237     public String patchItemWithContent(@RequestBody String requestBody) {
238         Item existingItemDoc = null;
239         Item incomingItemDoc = new Item();
240         try {
241             DocstoreService ds = BeanLocator.getDocstoreService();
242             incomingItemDoc.setContent(requestBody);
243             ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
244             org.kuali.ole.docstore.common.document.content.instance.Item incomingItemContent = itemOlemlRecordProcessor.fromXML(incomingItemDoc.getContent());
245             existingItemDoc = ds.retrieveItem(incomingItemContent.getItemIdentifier());
246             existingItemDoc.setItem(incomingItemDoc);
247             ds.updateItem(existingItemDoc);
248         } catch (DocstoreException e) {
249             LOG.info("Exception :", e);
250             return DocstoreExceptionProcessor.toXml(e);
251         } catch (ConversionException e) {
252             LOG.info("Exception :", e);
253             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
254         }
255         return incomingItemDoc.serialize(incomingItemDoc);
256     }
257 
258     @Override
259     @RequestMapping(value = "/doc/id", method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
260     @ResponseBody
261     public String patchItemById(@RequestParam("id") String[] itemsIds, @RequestBody String requestBody) {
262 
263         Item incomingItemDoc = null;
264         try {
265             DocstoreService ds = BeanLocator.getDocstoreService();
266             incomingItemDoc = new Item();
267             incomingItemDoc = (Item) incomingItemDoc.deserialize(requestBody);
268             for (String itemId : itemsIds) {
269                 Item existingItemDoc = ds.retrieveItem(itemId);
270                 existingItemDoc.setItem(incomingItemDoc);
271                 ds.updateItem(existingItemDoc);
272             }
273         } catch (DocstoreException e) {
274             LOG.info("Exception :", e);
275             return DocstoreExceptionProcessor.toXml(e);
276         } catch (ConversionException e) {
277             LOG.info("Exception :", e);
278             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
279         }
280         return incomingItemDoc.serialize(incomingItemDoc);
281     }
282 
283     @Override
284     @RequestMapping(value = "/id", method = RequestMethod.PATCH, consumes = "application/xml", produces = "application/xml")
285     @ResponseBody
286     public String patchItemByIdWithContent(@RequestParam("id") String[] itemsIds, @RequestBody String requestBody) {
287         Item existingItemDoc = null;
288         Item incomingItemDoc = null;
289         try {
290             DocstoreService ds = BeanLocator.getDocstoreService();
291             incomingItemDoc = new Item();
292             incomingItemDoc.setContent(requestBody);
293             for (String itemId : itemsIds) {
294                 existingItemDoc = ds.retrieveItem(itemId);
295                 existingItemDoc.setItem(incomingItemDoc);
296                 ds.updateItem(existingItemDoc);
297             }
298         } catch (DocstoreException e) {
299             LOG.info("Exception :", e);
300             return DocstoreExceptionProcessor.toXml(e);
301         } catch (ConversionException e) {
302             LOG.info("Exception :", e);
303             return DocstoreExceptionProcessor.toXml(new DocstoreValidationException(e.getMessage()));
304         }
305         return incomingItemDoc.serialize(incomingItemDoc);
306     }
307 
308     @Override
309     @RequestMapping(value = "/doc/", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/xml")
310     @ResponseBody
311     public String updateItem(@RequestBody String requestBody) {
312         DocstoreService ds = BeanLocator.getDocstoreService();
313         ItemOleml item = new ItemOleml();
314         item = (ItemOleml) item.deserialize(requestBody);
315         try {
316             ds.updateItem(item);
317         } catch (DocstoreException e) {
318             LOG.info("Exception :", e);
319             return DocstoreExceptionProcessor.toXml(e);
320         }
321         return responseUrl + item.getId();
322     }
323 
324 
325 
326     @Override
327     @RequestMapping(value = "/doc", method = RequestMethod.DELETE)
328     @ResponseBody
329     public String deleteItem(@RequestParam("itemId") String itemId) {
330         DocstoreService ds = BeanLocator.getDocstoreService();
331         try {
332             ds.deleteItem(itemId);
333         } catch (DocstoreException e) {
334             LOG.info("Exception :", e);
335             return DocstoreExceptionProcessor.toXml(e);
336         }
337         return "Success";
338     }
339 
340     @Override
341     @RequestMapping(value = "/docs", method = RequestMethod.DELETE)
342     @ResponseBody
343     public String deleteItems(@RequestParam("itemId") String itemIds) {
344         DocstoreService ds = BeanLocator.getDocstoreService();
345         String[] itemsIds = itemIds.split(",");
346         try {
347             for (String itemId : itemsIds) {
348                 ds.deleteItem(itemId);
349             }
350         } catch (DocstoreException e) {
351             LOG.info("Exception :", e);
352             return DocstoreExceptionProcessor.toXml(e);
353         }
354         return "Success";
355     }
356 
357     @Override
358     @RequestMapping(value = "/doc/find", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
359     @ResponseBody
360     public String findItems(@RequestBody String requestBody) {
361         DocstoreService ds = BeanLocator.getDocstoreService();
362         FindParams findParams = new FindParams();
363         findParams = (FindParams) findParams.deserialize(requestBody);
364         HashMap<String, String> hashMap = new HashMap();
365         List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
366         for (FindParams.Map.Entry entry : entries) {
367             hashMap.put(entry.getKey(), entry.getValue());
368         }
369         Item item = null;
370         try {
371             item = ds.findItem(hashMap);
372         } catch (DocstoreException e) {
373             LOG.info("Exception :", e);
374             return DocstoreExceptionProcessor.toXml(e);
375         }
376 
377         return item.serialize(item);
378     }
379 
380     @RequestMapping(value = "/doc/bulkUpdate", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/xml")
381     @ResponseBody
382     public String bulkUpdateItems(@RequestBody String requestBody) {
383         DocstoreService ds = BeanLocator.getDocstoreService();
384 
385         String[] bulkUpdateRequest = requestBody.split("\n", 3);
386 
387         String itemId = bulkUpdateRequest[0];
388         String canUpdateStaffOnlyFlag = bulkUpdateRequest[1];
389         requestBody = bulkUpdateRequest[2];
390 
391 
392         String[] itemSplitIds = itemId.split(",");
393         List<String> itemIds = new ArrayList<>();
394         for (String id : itemSplitIds) {
395             itemIds.add(id);
396         }
397         ItemOleml items = new ItemOleml();
398         items = (ItemOleml) items.deserialize(requestBody);
399         try {
400             ds.bulkUpdateItem(items, itemIds, canUpdateStaffOnlyFlag);
401         } catch (DocstoreException e) {
402             LOG.info("Exception :", e);
403             return DocstoreExceptionProcessor.toXml(e);
404         }
405         return items.serialize(items);
406     }
407 
408 
409 
410 }