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 | */ |
---|
18 | package eu.dasish.annotation.backend.rest; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.BackendConstants; |
---|
21 | import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; |
---|
22 | import eu.dasish.annotation.backend.identifiers.UserIdentifier; |
---|
23 | import eu.dasish.annotation.schema.Annotation; |
---|
24 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
25 | import java.sql.SQLException; |
---|
26 | import javax.servlet.http.HttpServletRequest; |
---|
27 | import javax.ws.rs.Consumes; |
---|
28 | import javax.ws.rs.DELETE; |
---|
29 | import javax.ws.rs.GET; |
---|
30 | import javax.ws.rs.POST; |
---|
31 | import javax.ws.rs.Path; |
---|
32 | import javax.ws.rs.PathParam; |
---|
33 | import javax.ws.rs.Produces; |
---|
34 | import javax.ws.rs.core.Context; |
---|
35 | import javax.ws.rs.core.MediaType; |
---|
36 | import javax.xml.bind.JAXBElement; |
---|
37 | import org.springframework.stereotype.Component; |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | * @author olhsha |
---|
42 | */ |
---|
43 | @Component |
---|
44 | @Path("/annotations") |
---|
45 | public class AnnotationResource { |
---|
46 | |
---|
47 | private DaoDispatcher requestor; |
---|
48 | @Context |
---|
49 | private HttpServletRequest httpServletRequest; |
---|
50 | |
---|
51 | public void setHttpRequest(HttpServletRequest request) { |
---|
52 | this.httpServletRequest = request; |
---|
53 | } |
---|
54 | |
---|
55 | public AnnotationResource() { |
---|
56 | } |
---|
57 | |
---|
58 | @GET |
---|
59 | @Produces(MediaType.TEXT_XML) |
---|
60 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") |
---|
61 | public JAXBElement<Annotation> getAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException { |
---|
62 | final Number annotationID = requestor.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier)); |
---|
63 | final Annotation annotation = requestor.getAnnotation(annotationID); |
---|
64 | return new ObjectFactory().createAnnotation(annotation); |
---|
65 | } |
---|
66 | |
---|
67 | /////////////////////////////////////////////////////// |
---|
68 | // TODO: return envelope: deleted or not deleted |
---|
69 | @DELETE |
---|
70 | @Path("{annotationid: " + BackendConstants.regExpIdentifier + "}") |
---|
71 | public String deleteAnnotation(@PathParam("annotationid") String annotationIdentifier) throws SQLException { |
---|
72 | final Number annotationID = requestor.getAnnotationInternalIdentifier(new AnnotationIdentifier(annotationIdentifier)); |
---|
73 | int[] resultDelete = requestor.deleteAnnotationWithSourcesAndPermissions(annotationID); |
---|
74 | String result = Integer.toString(resultDelete[0]); |
---|
75 | return result; |
---|
76 | } |
---|
77 | |
---|
78 | /////////////////////////////////////////////////////// |
---|
79 | // TODO: should be returning the envelope!!! |
---|
80 | @POST |
---|
81 | @Consumes(MediaType.APPLICATION_XML) |
---|
82 | @Produces(MediaType.APPLICATION_XML) |
---|
83 | @Path("") |
---|
84 | public JAXBElement<Annotation> createAnnotation(Annotation annotation) throws SQLException { |
---|
85 | String remoteUser = httpServletRequest.getRemoteUser(); |
---|
86 | Number userID = null; |
---|
87 | if (remoteUser != null) { |
---|
88 | userID = requestor.getUserInternalIdentifier(new UserIdentifier(remoteUser)); |
---|
89 | } |
---|
90 | Annotation newAnnotation = requestor.addAnnotationWithTargetSources(annotation, userID); |
---|
91 | return (new ObjectFactory().createAnnotation(newAnnotation)); |
---|
92 | } |
---|
93 | } |
---|