1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.extension;
21
22 import eu.ehri.extension.base.AbstractAccessibleResource;
23 import eu.ehri.extension.base.AbstractResource;
24 import eu.ehri.extension.base.DeleteResource;
25 import eu.ehri.extension.base.GetResource;
26 import eu.ehri.extension.base.ListResource;
27 import eu.ehri.extension.base.UpdateResource;
28 import eu.ehri.project.core.Tx;
29 import eu.ehri.project.definitions.Entities;
30 import eu.ehri.project.exceptions.AccessDenied;
31 import eu.ehri.project.exceptions.DeserializationError;
32 import eu.ehri.project.exceptions.ItemNotFound;
33 import eu.ehri.project.exceptions.PermissionDenied;
34 import eu.ehri.project.exceptions.SerializationError;
35 import eu.ehri.project.exceptions.ValidationError;
36 import eu.ehri.project.models.Annotation;
37 import eu.ehri.project.models.UserProfile;
38 import eu.ehri.project.persistence.Bundle;
39 import org.neo4j.graphdb.GraphDatabaseService;
40
41 import javax.ws.rs.Consumes;
42 import javax.ws.rs.DELETE;
43 import javax.ws.rs.GET;
44 import javax.ws.rs.POST;
45 import javax.ws.rs.PUT;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.QueryParam;
50 import javax.ws.rs.core.Context;
51 import javax.ws.rs.core.MediaType;
52 import javax.ws.rs.core.Response;
53 import java.util.List;
54
55
56
57
58 @Path(AbstractResource.RESOURCE_ENDPOINT_PREFIX + "/" + Entities.ANNOTATION)
59 public class AnnotationResource extends AbstractAccessibleResource<Annotation>
60 implements GetResource, ListResource, UpdateResource, DeleteResource {
61
62 public static final String TARGET_PARAM = "target";
63 public static final String BODY_PARAM = "body";
64
65 public AnnotationResource(@Context GraphDatabaseService database) {
66 super(database, Annotation.class);
67 }
68
69 @GET
70 @Produces(MediaType.APPLICATION_JSON)
71 @Path("{id:[^/]+}")
72 @Override
73 public Response get(@PathParam("id") String id) throws ItemNotFound {
74 return getItem(id);
75 }
76
77 @GET
78 @Produces(MediaType.APPLICATION_JSON)
79 @Override
80 public Response list() {
81 return listItems();
82 }
83
84
85
86
87
88
89
90
91
92
93 @POST
94 @Consumes(MediaType.APPLICATION_JSON)
95 @Produces(MediaType.APPLICATION_JSON)
96 public Response createAnnotation(
97 @QueryParam(TARGET_PARAM) String id,
98 @QueryParam(BODY_PARAM) String did,
99 @QueryParam(ACCESSOR_PARAM) List<String> accessors,
100 Bundle bundle)
101 throws PermissionDenied, AccessDenied, ValidationError, DeserializationError,
102 ItemNotFound, SerializationError {
103 try (final Tx tx = beginTx()) {
104 if (id == null) {
105 throw new DeserializationError("Target must be provided");
106 }
107 UserProfile user = getCurrentUser();
108 Annotation ann = api().createAnnotation(id, did == null ? id : did,
109 bundle, getAccessors(accessors, user), getLogMessage());
110 Response response = creationResponse(ann);
111 tx.success();
112 return response;
113 }
114 }
115
116 @PUT
117 @Path("{id:[^/]+}")
118 @Override
119 public Response update(@PathParam("id") String id, Bundle bundle)
120 throws PermissionDenied, ItemNotFound, ValidationError, DeserializationError {
121 try (final Tx tx = beginTx()) {
122 Response response = updateItem(id, bundle);
123 tx.success();
124 return response;
125 }
126 }
127
128 @DELETE
129 @Path("{id:[^/]+}")
130 @Override
131 public void delete(@PathParam("id") String id)
132 throws PermissionDenied, ItemNotFound, ValidationError {
133 try (final Tx tx = beginTx()) {
134 deleteItem(id);
135 tx.success();
136 }
137 }
138 }