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

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

making interface and bean for DaoDispatcher?. AnnotationResourceTest? passed with 3 methods: get, add and delete annotations. AnnotationsTest? still have problems.

File size: 3.8 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.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.beans.factory.annotation.Autowired;
39import org.springframework.stereotype.Component;
40
41/**
42 *
43 * @author olhsha
44 */
45@Component
46@Path("/annotations")
47public class AnnotationResource {
48   
49    @Autowired
50    private DaoDispatcher daoDispatcher;
51    @Context
52    private HttpServletRequest httpServletRequest;
53
54    public void setHttpRequest(HttpServletRequest request) {
55        this.httpServletRequest = request;
56    }
57
58    public AnnotationResource() {
59    }
60
61    @GET
62    @Produces(MediaType.TEXT_XML)
63    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
64    public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException {
65        final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier));
66        final Annotation annotation = daoDispatcher.getAnnotation(annotationID);
67        return new ObjectFactory().createAnnotation(annotation);
68    }
69
70    ///////////////////////////////////////////////////////
71    // TODO: return envelope: deleted or not deleted
72    @DELETE
73    @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}")
74    public String deleteAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException {
75        final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier));
76        int[] resultDelete = daoDispatcher.deleteAnnotation(annotationID);
77        String result = Integer.toString(resultDelete[0]);
78        return result;
79    }
80
81    ///////////////////////////////////////////////////////
82    // TODO: should be returning the envelope!!!
83    @POST
84    @Consumes(MediaType.APPLICATION_XML)
85    @Produces(MediaType.APPLICATION_XML)
86    @Path("")
87    public JAXBElement<Annotation> createAnnotation(Annotation annotation) throws SQLException {
88        String remoteUser = httpServletRequest.getRemoteUser();
89        Number userID = null;
90        if (remoteUser != null) {
91            userID = daoDispatcher.getUserInternalIdentifier(new UserIdentifier(remoteUser));
92        }
93        Number newAnnotationID =  daoDispatcher.addUsersAnnotation(annotation, userID);
94        Annotation newAnnotation = daoDispatcher.getAnnotation(newAnnotationID); 
95        return (new ObjectFactory().createAnnotation(newAnnotation));
96    }
97}
Note: See TracBrowser for help on using the repository browser.