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.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   * Web service interface for creating annotations.
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       * Create an annotation for a particular item.
86       *
87       * @param id        the ID of the item being annotation
88       * @param did       the (optional) ID of the sub-item target, e.g. a description
89       * @param bundle    the JSON representation of the annotation
90       * @param accessors user IDs who can access the annotation
91       * @return the annotation
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 }