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.http.HttpStatus;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.annotation.*;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18
19
20
21
22
23
24
25
26
27 @Controller
28 @RequestMapping("/bibs1")
29
30 public class BibsRestController extends AbstractRestService {
31
32 private static final Logger LOG = LoggerFactory.getLogger(BibsRestController.class);
33 private static String responseUrl = "documentrest/bibs/";
34 private static String responseTreeUrl = "documentrest/bibs/tree/";
35 private Logger logger = LoggerFactory.getLogger(BibsRestController.class);
36
37 @Override
38 @RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/xml;charset=UTF-8", produces = "application/text")
39 @ResponseBody
40 public String createBib(@RequestBody String requestBody) {
41 DocstoreService ds = BeanLocator.getDocstoreService();
42 Bib bib = new BibMarc();
43 Bib bibObj = (Bib) bib.deserialize(requestBody.replaceAll("\n", ""));
44 try {
45 ds.createBib(bibObj);
46 } catch (DocstoreException e) {
47 LOG.info("Exception :", e);
48 return DocstoreExceptionProcessor.toXml(e);
49 }
50 return responseUrl + bibObj.getId();
51 }
52
53 @Override
54 @RequestMapping(value = "/", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/text")
55 @ResponseBody
56 public String updateBib(@RequestBody String requestBody) {
57 DocstoreService ds = BeanLocator.getDocstoreService();
58 Bib bib = new BibMarc();
59 Bib bibObj = (Bib) bib.deserialize(requestBody);
60 try {
61 ds.updateBib(bibObj);
62 } catch (DocstoreException e) {
63 LOG.info("Exception :", e);
64 return DocstoreExceptionProcessor.toXml(e);
65 }
66 return responseUrl + bibObj.getId();
67 }
68
69
70
71 @Override
72 @RequestMapping(value = "/{bibId}", method = RequestMethod.GET, produces = "application/xml;charset=UTF-8")
73 @ResponseBody
74 public String retrieveBib(@PathVariable("bibId") String bibId) {
75 DocstoreService ds = BeanLocator.getDocstoreService();
76 if (bibId.contains("bibIds")) {
77 String[] splitString = bibId.split("=");
78 String[] bibIds = splitString[1].split(",");
79 List<String> bibIdList = new ArrayList<>();
80 for (String id : bibIds) {
81 bibIdList.add(id);
82 }
83 List<Bib> bibs = null;
84 try {
85 bibs = ds.retrieveBibs(bibIdList);
86 } catch (DocstoreException e) {
87 LOG.info("Exception :", e);
88 return DocstoreExceptionProcessor.toXml(e);
89 }
90 Bibs bibsObj = new Bibs();
91 bibsObj.getBibs().addAll(bibs);
92 return Bibs.serialize(bibsObj);
93 } else {
94 Bib bib = null;
95 try {
96 bib = ds.retrieveBib(bibId);
97 } catch (DocstoreException e) {
98 LOG.info("Exception :", e);
99 return DocstoreExceptionProcessor.toXml(e);
100 }
101 return bib.serialize(bib);
102 }
103 }
104
105 @Override
106 @RequestMapping(value = "/{bibId}", method = RequestMethod.DELETE)
107 @ResponseBody
108 public String deleteBib(@PathVariable("bibId") String bibId) {
109 DocstoreService ds = BeanLocator.getDocstoreService();
110 if (bibId.contains("bibIds")) {
111 String[] splitString = bibId.split("=");
112 String[] bibIds = splitString[1].split(",");
113 List<String> bibIdList = new ArrayList<>();
114 for (String id : bibIds) {
115 bibIdList.add(id);
116 }
117 try {
118 ds.deleteBibs(bibIdList);
119 } catch (DocstoreException e) {
120 LOG.info("Exception :", e);
121 return DocstoreExceptionProcessor.toXml(e);
122 }
123
124 } else {
125 try {
126 ds.deleteBib(bibId);
127 } catch (DocstoreException e) {
128 LOG.info("Exception :", e);
129 return DocstoreExceptionProcessor.toXml(e);
130 }
131
132 }
133 return "Success";
134 }
135
136 @Override
137 @RequestMapping(value = "/tree", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
138 @ResponseBody
139 public String createBibTree(@RequestBody String requestBody) {
140 DocstoreService ds = BeanLocator.getDocstoreService();
141 BibTree bibTree = new BibTree();
142 bibTree = (BibTree) bibTree.deserialize(requestBody);
143 try {
144 ds.createBibTree(bibTree);
145 } catch (DocstoreException e) {
146 LOG.info("Exception :", e);
147 return DocstoreExceptionProcessor.toXml(e);
148 }
149 return responseTreeUrl + bibTree.getBib().getId();
150 }
151
152
153
154 @Override
155 @RequestMapping(value = "/tree/{bibId}", method = RequestMethod.GET, produces = "application/xml")
156 @ResponseBody
157 public String retrieveBibTree(@PathVariable("bibId") String bibId) {
158 DocstoreService ds = BeanLocator.getDocstoreService();
159 BibTree bibTree = null;
160 try {
161 bibTree = ds.retrieveBibTree(bibId);
162 } catch (DocstoreException e) {
163 LOG.info("Exception :", e);
164 return DocstoreExceptionProcessor.toXml(e);
165 }
166 return bibTree.serialize(bibTree);
167 }
168
169 @Override
170 @RequestMapping(value = "/find", method = RequestMethod.POST, consumes = "application/xml", produces = "application/xml")
171 @ResponseBody
172 public String findBibs(@RequestBody String requestBody) {
173 DocstoreService ds = BeanLocator.getDocstoreService();
174 FindParams findParams = new FindParams();
175 findParams = (FindParams) findParams.deserialize(requestBody);
176 HashMap<String, String> hashMap = new HashMap();
177 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
178 for (FindParams.Map.Entry entry : entries) {
179 hashMap.put(entry.getKey(), entry.getValue());
180 }
181 Bib bib = null;
182 try {
183 bib = ds.findBib(hashMap);
184 } catch (DocstoreException e) {
185 LOG.info("Exception :", e);
186 return DocstoreExceptionProcessor.toXml(e);
187 }
188
189 return bib.serialize(bib);
190 }
191
192 @Override
193 @RequestMapping(value = "/tree/find", method = RequestMethod.POST, consumes = "application/xml", produces = "application/xml")
194 @ResponseBody
195 public String findBibTree(@RequestBody String requestBody) {
196 DocstoreService ds = BeanLocator.getDocstoreService();
197 FindParams findParams = new FindParams();
198 findParams = (FindParams) findParams.deserialize(requestBody);
199 HashMap<String, String> hashMap = new HashMap();
200 List<FindParams.Map.Entry> entries = findParams.getMap().getEntry();
201 for (FindParams.Map.Entry entry : entries) {
202 hashMap.put(entry.getKey(), entry.getValue());
203 }
204 BibTree bibTree = null;
205 try {
206 bibTree = ds.findBibTree(hashMap);
207 } catch (DocstoreException e) {
208 LOG.info("Exception :", e);
209 return DocstoreExceptionProcessor.toXml(e);
210 }
211 return bibTree.serialize(bibTree);
212 }
213
214 @Override
215 @RequestMapping(value = "{bibId}/transfer/{holdingIds}", method = RequestMethod.POST, consumes = "application/xml", produces = "application/text")
216 @ResponseBody
217 public String transferHoldings(@PathVariable("bibId") String bibId, @PathVariable("holdingIds") String holdingIds) {
218 DocstoreService ds = BeanLocator.getDocstoreService();
219 String[] splitHoldingIds = holdingIds.split(",");
220 List<String> holdingsIds = new ArrayList<>();
221 for (String holdingsId : splitHoldingIds) {
222 holdingsIds.add(holdingsId);
223 }
224 try {
225 ds.transferHoldings(holdingsIds, bibId);
226 } catch (DocstoreException e) {
227 LOG.info("Exception :", e);
228 return DocstoreExceptionProcessor.toXml(e);
229 }
230 return "Success";
231 }
232
233
234 }