View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
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   * Provides a web service interface for creating/reading item links.
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       * Create a link between two items.
96       *
97       * @param bundle    The link body data
98       * @param source    The link source
99       * @param target    The link target
100      * @param bodies    optional list of entities to provide the body
101      * @param accessors The IDs of accessors who can see this link
102      * @return the created link item
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 }