| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 2 | |
| 3 | import com.sun.jersey.spi.container.ResourceFilters; |
| 4 | import de.ids_mannheim.korap.config.BeansFactory; |
| 5 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 6 | import de.ids_mannheim.korap.handlers.DocumentDao; |
| 7 | import de.ids_mannheim.korap.resources.Document; |
| margaretha | f68daa6 | 2017-09-21 02:11:24 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.server.KustvaktServer; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 9 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 10 | import de.ids_mannheim.korap.utils.KustvaktLogger; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 11 | import de.ids_mannheim.korap.web.filter.AdminFilter; |
| 12 | import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler; |
| 13 | import org.slf4j.Logger; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 14 | import org.slf4j.LoggerFactory; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 15 | |
| 16 | import javax.ws.rs.*; |
| 17 | import javax.ws.rs.core.MediaType; |
| 18 | import javax.ws.rs.core.Response; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 22 | * EM: To Do: restructure codes regarding service and controller layers |
| 23 | * |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 24 | * @author hanl |
| 25 | * @date 19/11/2014 |
| 26 | */ |
| 27 | @Path(KustvaktServer.API_VERSION + "/doc") |
| 28 | @ResourceFilters({ AdminFilter.class }) |
| 29 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 30 | public class DocumentController { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 31 | |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 32 | private static Logger jlog = |
| 33 | LoggerFactory.getLogger(DocumentController.class); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 34 | private DocumentDao documentDao; |
| 35 | |
| 36 | |
| 37 | // todo: error handling |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 38 | public DocumentController () { |
| 39 | this.documentDao = new DocumentDao( |
| 40 | BeansFactory.getKustvaktContext().getPersistenceClient()); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | @POST |
| 45 | @Path("{doc}") |
| 46 | public Response store (@PathParam("doc") String docid, |
| 47 | @QueryParam("disabled") Boolean disabled) { |
| 48 | Document doc = new Document(docid); |
| 49 | doc.setDisabled(disabled); |
| 50 | try { |
| 51 | this.documentDao.storeResource(doc, null); |
| 52 | } |
| 53 | catch (KustvaktException e) { |
| 54 | throw KustvaktResponseHandler.throwit(e); |
| 55 | } |
| 56 | return Response.ok().build(); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | //todo: pipe add document to index endpoint |
| 61 | |
| 62 | @GET |
| 63 | @Path("{corpus}") |
| 64 | public Response get (@PathParam("corpus") String corpus, |
| 65 | @QueryParam("index") Integer index, |
| 66 | @QueryParam("offset") Integer length) { |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 67 | if (index == null) index = 1; |
| 68 | if (length == null) length = 25; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 69 | try { |
| 70 | List docs = this.documentDao.findbyCorpus(corpus, length, index); |
| 71 | //todo: serialize to document json |
| 72 | return Response.ok(JsonUtils.toJSON(docs)).build(); |
| 73 | } |
| 74 | catch (KustvaktException e) { |
| 75 | throw KustvaktResponseHandler.throwit(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } |