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.DeserializationError;
31 import eu.ehri.project.exceptions.ItemNotFound;
32 import eu.ehri.project.exceptions.PermissionDenied;
33 import eu.ehri.project.exceptions.ValidationError;
34 import eu.ehri.project.models.Link;
35 import eu.ehri.project.models.UserProfile;
36 import eu.ehri.project.persistence.Bundle;
37 import org.neo4j.graphdb.GraphDatabaseService;
38
39 import javax.ws.rs.Consumes;
40 import javax.ws.rs.DELETE;
41 import javax.ws.rs.GET;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.PUT;
44 import javax.ws.rs.Path;
45 import javax.ws.rs.PathParam;
46 import javax.ws.rs.Produces;
47 import javax.ws.rs.QueryParam;
48 import javax.ws.rs.core.Context;
49 import javax.ws.rs.core.MediaType;
50 import javax.ws.rs.core.Response;
51 import java.util.List;
52
53
54
55
56 @Path(AbstractResource.RESOURCE_ENDPOINT_PREFIX + "/" + Entities.LINK)
57 public class LinkResource extends AbstractAccessibleResource<Link>
58 implements GetResource, ListResource, DeleteResource, UpdateResource {
59
60 public static final String TARGET_PARAM = "target";
61 public static final String SOURCE_PARAM = "source";
62 public static final String BODY_PARAM = "body";
63
64 public LinkResource(@Context GraphDatabaseService database) {
65 super(database, Link.class);
66 }
67
68 @GET
69 @Produces(MediaType.APPLICATION_JSON)
70 @Path("{id:[^/]+}")
71 @Override
72 public Response get(@PathParam("id") String id) throws ItemNotFound {
73 return getItem(id);
74 }
75
76 @GET
77 @Produces(MediaType.APPLICATION_JSON)
78 public Response list() {
79 return listItems();
80 }
81
82 @PUT
83 @Path("{id:[^/]+}")
84 @Override
85 public Response update(@PathParam("id") String id, Bundle bundle)
86 throws PermissionDenied, ItemNotFound, ValidationError, DeserializationError {
87 try (final Tx tx = beginTx()) {
88 Response item = updateItem(id, bundle);
89 tx.success();
90 return item;
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 @POST
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces(MediaType.APPLICATION_JSON)
107 public Response create(
108 Bundle bundle,
109 @QueryParam(SOURCE_PARAM) String source,
110 @QueryParam(TARGET_PARAM) String target,
111 @QueryParam(BODY_PARAM) List<String> bodies,
112 @QueryParam(ACCESSOR_PARAM) List<String> accessors)
113 throws PermissionDenied, ValidationError,
114 DeserializationError, ItemNotFound {
115 try (final Tx tx = beginTx()) {
116 if (source == null || target == null) {
117 throw new DeserializationError("Both source and target must be provided");
118 }
119 UserProfile user = getCurrentUser();
120 Link link = api().createLink(target,
121 source, bodies, bundle, getAccessors(accessors, user), getLogMessage());
122 Response response = creationResponse(link);
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 }