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.CreateResource;
25 import eu.ehri.extension.base.DeleteResource;
26 import eu.ehri.extension.base.GetResource;
27 import eu.ehri.extension.base.ListResource;
28 import eu.ehri.extension.base.UpdateResource;
29 import eu.ehri.project.core.Tx;
30 import eu.ehri.project.definitions.Entities;
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.ValidationError;
35 import eu.ehri.project.exporters.eac.Eac2010Exporter;
36 import eu.ehri.project.models.HistoricalAgent;
37 import eu.ehri.project.persistence.Bundle;
38 import org.neo4j.graphdb.GraphDatabaseService;
39
40 import javax.ws.rs.Consumes;
41 import javax.ws.rs.DELETE;
42 import javax.ws.rs.DefaultValue;
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.WebApplicationException;
51 import javax.ws.rs.core.Context;
52 import javax.ws.rs.core.MediaType;
53 import javax.ws.rs.core.Response;
54 import javax.ws.rs.core.StreamingOutput;
55 import javax.xml.transform.TransformerException;
56 import java.io.IOException;
57 import java.util.List;
58
59
60
61
62 @Path(AbstractResource.RESOURCE_ENDPOINT_PREFIX + "/" + Entities.HISTORICAL_AGENT)
63 public class HistoricalAgentResource extends AbstractAccessibleResource<HistoricalAgent>
64 implements GetResource, ListResource, CreateResource, UpdateResource, DeleteResource {
65
66 public HistoricalAgentResource(@Context GraphDatabaseService database) {
67 super(database, HistoricalAgent.class);
68 }
69
70 @GET
71 @Produces(MediaType.APPLICATION_JSON)
72 @Path("{id:[^/]+}")
73 @Override
74 public Response get(@PathParam("id") String id) throws ItemNotFound {
75 return getItem(id);
76
77 }
78
79 @GET
80 @Produces(MediaType.APPLICATION_JSON)
81 @Override
82 public Response list() {
83 return listItems();
84 }
85
86 @POST
87 @Consumes(MediaType.APPLICATION_JSON)
88 @Produces(MediaType.APPLICATION_JSON)
89 @Override
90 public Response create(Bundle bundle,
91 @QueryParam(ACCESSOR_PARAM) List<String> accessors)
92 throws PermissionDenied, ValidationError, DeserializationError {
93 try (final Tx tx = beginTx()) {
94 Response item = createItem(bundle, accessors);
95 tx.success();
96 return item;
97 }
98 }
99
100 @PUT
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 @Path("{id:[^/]+}")
104 @Override
105 public Response update(@PathParam("id") String id, Bundle bundle)
106 throws PermissionDenied, ValidationError,
107 DeserializationError, ItemNotFound {
108 try (final Tx tx = beginTx()) {
109 Response response = updateItem(id, bundle);
110 tx.success();
111 return response;
112 }
113 }
114
115 @DELETE
116 @Path("{id:[^/]+}")
117 @Override
118 public void delete(@PathParam("id") String id)
119 throws PermissionDenied, ItemNotFound, ValidationError {
120 try (final Tx tx = beginTx()) {
121 deleteItem(id);
122 tx.success();
123 }
124 }
125
126
127
128
129
130
131
132
133 @GET
134 @Path("{id:[^/]+}/eac")
135 @Produces(MediaType.TEXT_XML)
136 public Response exportEac(@PathParam("id") String id,
137 final @QueryParam("lang") @DefaultValue("eng") String lang)
138 throws IOException, ItemNotFound {
139 try (final Tx tx = beginTx()) {
140 HistoricalAgent agent = api().detail(id, HistoricalAgent.class);
141 tx.success();
142 return Response.ok((StreamingOutput) outputStream -> {
143 try (final Tx tx2 = beginTx()) {
144 new Eac2010Exporter(api()).export(agent, outputStream, lang);
145 tx2.success();
146 } catch (TransformerException e) {
147 throw new WebApplicationException(e);
148 }
149 }).type(MediaType.TEXT_XML + "; charset=utf-8").build();
150 }
151 }
152 }