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