source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java @ 3408

Last change on this file since 3408 was 3408, checked in by olhsha, 11 years ago

refactored DAO-s tests are added, but not all working yet. Also: add 8 more tests to dispatcher class

File size: 3.7 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.rest;
19
20import eu.dasish.annotation.backend.BackendConstants;
21import eu.dasish.annotation.backend.dao.integration.DaoDispatcher;
22import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
23import eu.dasish.annotation.backend.identifiers.UserIdentifier;
24import eu.dasish.annotation.schema.Annotation;
25import eu.dasish.annotation.schema.ObjectFactory;
26import java.sql.SQLException;
27import javax.servlet.http.HttpServletRequest;
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.Context;
36import javax.ws.rs.core.MediaType;
37import javax.xml.bind.JAXBElement;
38import org.springframework.stereotype.Component;
39
40/**
41 *
42 * @author olhsha
43 */
44@Component
45@Path("/annotations")
46public class AnnotationResource {
47
48    private DaoDispatcher requestor;
49    @Context
50    private HttpServletRequest httpServletRequest;
51
52    public void setHttpRequest(HttpServletRequest request) {
53        this.httpServletRequest = request;
54    }
55
56    public AnnotationResource() {
57    }
58
59    @GET
60    @Produces(MediaType.TEXT_XML)
61    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
62    public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException {
63        final Number annotationID = requestor.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier));
64        final Annotation annotation = requestor.getAnnotation(annotationID);
65        return new ObjectFactory().createAnnotation(annotation);
66    }
67
68    ///////////////////////////////////////////////////////
69    // TODO: return envelope: deleted or not deleted
70    @DELETE
71    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
72    public String deleteAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException {
73        final Number annotationID = requestor.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier));
74        int[] resultDelete = requestor.deleteAnnotationWithSourcesAndPermissions(annotationID);
75        String result = Integer.toString(resultDelete[0]);
76        return result;
77    }
78
79    ///////////////////////////////////////////////////////
80    // TODO: should be returning the envelope!!!
81    @POST
82    @Consumes(MediaType.APPLICATION_XML)
83    @Produces(MediaType.APPLICATION_XML)
84    @Path("")
85    public JAXBElement<Annotation> createAnnotation(Annotation annotation) throws SQLException {
86        String remoteUser = httpServletRequest.getRemoteUser();
87        Number userID = null;
88        if (remoteUser != null) {
89            userID = requestor.getUserInternalIdentifier(new UserIdentifier(remoteUser));
90        }
91        Annotation newAnnotation =  requestor.addAnnotationWithTargetSources(annotation, userID);
92        return (new ObjectFactory().createAnnotation(newAnnotation));
93    }
94}
Note: See TracBrowser for help on using the repository browser.