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.ParentResource;
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.ead.Ead2002Exporter;
36 import eu.ehri.project.models.DocumentaryUnit;
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.DOCUMENTARY_UNIT)
63 public class DocumentaryUnitResource
64 extends AbstractAccessibleResource<DocumentaryUnit>
65 implements GetResource, ListResource, UpdateResource, ParentResource, DeleteResource {
66
67 public DocumentaryUnitResource(@Context GraphDatabaseService database) {
68 super(database, DocumentaryUnit.class);
69 }
70
71 @GET
72 @Produces(MediaType.APPLICATION_JSON)
73 @Path("{id:[^/]+}")
74 @Override
75 public Response get(@PathParam("id") String id) throws ItemNotFound {
76 return getItem(id);
77 }
78
79 @GET
80 @Produces(MediaType.APPLICATION_JSON)
81 @Override
82 public Response list() {
83 return listItems();
84 }
85
86 @GET
87 @Produces(MediaType.APPLICATION_JSON)
88 @Path("{id:[^/]+}/list")
89 @Override
90 public Response listChildren(
91 @PathParam("id") String id,
92 @QueryParam(ALL_PARAM) @DefaultValue("false") boolean all) throws ItemNotFound {
93 try (final Tx tx = beginTx()) {
94 DocumentaryUnit parent = manager.getEntity(id, DocumentaryUnit.class);
95 Response response = streamingPage(() -> {
96 Iterable<DocumentaryUnit> units = all
97 ? parent.getAllChildren()
98 : parent.getChildren();
99 return getQuery().page(units, cls);
100 });
101 tx.success();
102 return response;
103 }
104 }
105
106 @PUT
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 @Path("{id:[^/]+}")
110 @Override
111 public Response update(@PathParam("id") String id,
112 Bundle bundle) throws PermissionDenied,
113 ValidationError, DeserializationError, ItemNotFound {
114 try (final Tx tx = beginTx()) {
115 Response response = updateItem(id, bundle);
116 tx.success();
117 return response;
118 }
119 }
120
121 @DELETE
122 @Path("{id:[^/]+}")
123 @Override
124 public void delete(@PathParam("id") String id)
125 throws PermissionDenied, ItemNotFound, ValidationError {
126 try (final Tx tx = beginTx()) {
127 deleteItem(id);
128 tx.success();
129 }
130 }
131
132 @POST
133 @Consumes(MediaType.APPLICATION_JSON)
134 @Produces(MediaType.APPLICATION_JSON)
135 @Path("{id:[^/]+}")
136 @Override
137 public Response createChild(@PathParam("id") String id,
138 Bundle bundle, @QueryParam(ACCESSOR_PARAM) List<String> accessors)
139 throws PermissionDenied, ValidationError,
140 DeserializationError, ItemNotFound {
141 try (final Tx tx = beginTx()) {
142 final DocumentaryUnit parent = api().detail(id, cls);
143 Response resource = createItem(bundle, accessors,
144 parent::addChild,
145 api().withScope(parent), cls);
146 tx.success();
147 return resource;
148 }
149 }
150
151
152
153
154
155
156
157
158 @GET
159 @Path("{id:[^/]+}/ead")
160 @Produces(MediaType.TEXT_XML)
161 public Response exportEad(@PathParam("id") String id,
162 final @QueryParam("lang") @DefaultValue("eng") String lang)
163 throws IOException, ItemNotFound {
164 try (final Tx tx = beginTx()) {
165 DocumentaryUnit unit = api().detail(id, cls);
166 tx.success();
167 return Response.ok((StreamingOutput) outputStream -> {
168 try (final Tx tx2 = beginTx()) {
169 new Ead2002Exporter(api()).export(unit, outputStream, lang);
170 tx2.success();
171 } catch (TransformerException e) {
172 throw new WebApplicationException(e);
173 }
174 }).type(MediaType.TEXT_XML + "; charset=utf-8").build();
175 }
176 }
177 }