001package org.kuali.ole.docstore.engine.service.rest;
002
003import org.kuali.ole.docstore.common.document.*;
004import org.kuali.ole.docstore.common.exception.DocstoreException;
005import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
006import org.kuali.ole.docstore.common.find.FindParams;
007import org.kuali.ole.docstore.common.service.DocstoreService;
008import org.kuali.ole.docstore.service.BeanLocator;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011import org.springframework.http.HttpStatus;
012import org.springframework.stereotype.Controller;
013import org.springframework.web.bind.annotation.*;
014
015import java.util.ArrayList;
016import java.util.HashMap;
017import java.util.List;
018
019/**
020 * Created with IntelliJ IDEA.
021 * User: jayabharathreddy
022 * Date: 12/17/13
023 * Time: 1:14 PM
024 * To change this template use File | Settings | File Templates.
025 */
026
027@Controller
028@RequestMapping("/bibs1")
029
030public class BibsRestController extends AbstractRestService {
031
032    private static final Logger LOG = LoggerFactory.getLogger(BibsRestController.class);
033    private static String responseUrl = "documentrest/bibs/";
034    private static String responseTreeUrl = "documentrest/bibs/tree/";
035    private Logger logger = LoggerFactory.getLogger(BibsRestController.class);
036
037    @Override
038    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/xml;charset=UTF-8", produces = "application/text")
039    @ResponseBody
040    public String createBib(@RequestBody String requestBody) {
041        DocstoreService ds = BeanLocator.getDocstoreService();
042        Bib bib = new BibMarc();
043        Bib bibObj = (Bib) bib.deserialize(requestBody.replaceAll("\n", ""));
044        try {
045            ds.createBib(bibObj);
046        } catch (DocstoreException e) {
047            LOG.info("Exception :", e);
048            return DocstoreExceptionProcessor.toXml(e);
049        }
050        return responseUrl + bibObj.getId();
051    }
052
053    @Override
054    @RequestMapping(value = "/", method = RequestMethod.PUT, consumes = "application/xml", produces = "application/text")
055    @ResponseBody
056    public String updateBib(@RequestBody String requestBody) {
057        DocstoreService ds = BeanLocator.getDocstoreService();
058        Bib bib = new BibMarc();
059        Bib bibObj = (Bib) bib.deserialize(requestBody);
060        try {
061            ds.updateBib(bibObj);
062        } catch (DocstoreException e) {
063            LOG.info("Exception :", e);
064            return DocstoreExceptionProcessor.toXml(e);
065        }
066        return responseUrl + bibObj.getId();
067    }
068
069
070
071    @Override
072    @RequestMapping(value = "/{bibId}", method = RequestMethod.GET, produces = "application/xml;charset=UTF-8")
073    @ResponseBody
074    public String retrieveBib(@PathVariable("bibId") String bibId) {
075        DocstoreService ds = BeanLocator.getDocstoreService();
076        if (bibId.contains("bibIds")) {
077            String[] splitString = bibId.split("=");
078            String[] bibIds = splitString[1].split(",");
079            List<String> bibIdList = new ArrayList<>();
080            for (String id : bibIds) {
081                bibIdList.add(id);
082            }
083            List<Bib> bibs = null;
084            try {
085                bibs = ds.retrieveBibs(bibIdList);
086            } catch (DocstoreException e) {
087                LOG.info("Exception :", e);
088                return DocstoreExceptionProcessor.toXml(e);
089            }
090            Bibs bibsObj = new Bibs();
091            bibsObj.getBibs().addAll(bibs);
092            return Bibs.serialize(bibsObj);
093        } else {
094            Bib bib = null;
095            try {
096                bib = ds.retrieveBib(bibId);
097            } catch (DocstoreException e) {
098                LOG.info("Exception :", e);
099                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}