blob: c23db528c4f831aec43518ff4fa02852ade54db6 [file] [log] [blame]
margarethad3c0fc92017-10-25 15:03:32 +02001package de.ids_mannheim.korap.web.controller;
Michael Hanle56bb892016-05-25 17:34:41 +02002
3import com.sun.jersey.spi.container.ResourceFilters;
4import de.ids_mannheim.korap.config.BeansFactory;
5import de.ids_mannheim.korap.exceptions.KustvaktException;
6import de.ids_mannheim.korap.handlers.DocumentDao;
7import de.ids_mannheim.korap.resources.Document;
margarethaf68daa62017-09-21 02:11:24 +02008import de.ids_mannheim.korap.server.KustvaktServer;
Michael Hanle56bb892016-05-25 17:34:41 +02009import de.ids_mannheim.korap.utils.JsonUtils;
10import de.ids_mannheim.korap.utils.KustvaktLogger;
Michael Hanle56bb892016-05-25 17:34:41 +020011import de.ids_mannheim.korap.web.filter.AdminFilter;
12import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler;
13import org.slf4j.Logger;
Michael Hanlcb2d3f92016-06-02 17:34:06 +020014import org.slf4j.LoggerFactory;
Michael Hanle56bb892016-05-25 17:34:41 +020015
16import javax.ws.rs.*;
17import javax.ws.rs.core.MediaType;
18import javax.ws.rs.core.Response;
19import java.util.List;
20
21/**
margarethad3c0fc92017-10-25 15:03:32 +020022 * EM: To Do: restructure codes regarding service and controller layers
23 *
Michael Hanle56bb892016-05-25 17:34:41 +020024 * @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")
margarethad3c0fc92017-10-25 15:03:32 +020030public class DocumentController {
Michael Hanle56bb892016-05-25 17:34:41 +020031
margarethad3c0fc92017-10-25 15:03:32 +020032 private static Logger jlog =
33 LoggerFactory.getLogger(DocumentController.class);
Michael Hanle56bb892016-05-25 17:34:41 +020034 private DocumentDao documentDao;
35
36
37 // todo: error handling
margarethad3c0fc92017-10-25 15:03:32 +020038 public DocumentController () {
39 this.documentDao = new DocumentDao(
40 BeansFactory.getKustvaktContext().getPersistenceClient());
Michael Hanle56bb892016-05-25 17:34:41 +020041 }
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) {
margarethad3c0fc92017-10-25 15:03:32 +020067 if (index == null) index = 1;
68 if (length == null) length = 25;
Michael Hanle56bb892016-05-25 17:34:41 +020069 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}