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.ParentResource;
29 import eu.ehri.extension.base.UpdateResource;
30 import eu.ehri.project.core.Tx;
31 import eu.ehri.project.definitions.Entities;
32 import eu.ehri.project.exceptions.DeserializationError;
33 import eu.ehri.project.exceptions.ItemNotFound;
34 import eu.ehri.project.exceptions.PermissionDenied;
35 import eu.ehri.project.exceptions.ValidationError;
36 import eu.ehri.project.exporters.eag.Eag2012Exporter;
37 import eu.ehri.project.exporters.eag.EagExporter;
38 import eu.ehri.project.models.Country;
39 import eu.ehri.project.models.Repository;
40 import eu.ehri.project.persistence.Bundle;
41 import org.neo4j.graphdb.GraphDatabaseService;
42
43 import javax.ws.rs.Consumes;
44 import javax.ws.rs.DELETE;
45 import javax.ws.rs.DefaultValue;
46 import javax.ws.rs.GET;
47 import javax.ws.rs.POST;
48 import javax.ws.rs.PUT;
49 import javax.ws.rs.Path;
50 import javax.ws.rs.PathParam;
51 import javax.ws.rs.Produces;
52 import javax.ws.rs.QueryParam;
53 import javax.ws.rs.core.Context;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import java.io.IOException;
57 import java.util.List;
58
59
60
61
62
63 @Path(AbstractResource.RESOURCE_ENDPOINT_PREFIX + "/" + Entities.COUNTRY)
64 public class CountryResource
65 extends AbstractAccessibleResource<Country>
66 implements CreateResource, GetResource, ListResource, UpdateResource, ParentResource, DeleteResource {
67
68 public CountryResource(@Context GraphDatabaseService database) {
69 super(database, Country.class);
70 }
71
72 @GET
73 @Produces(MediaType.APPLICATION_JSON)
74 @Path("{id:[^/]+}")
75 @Override
76 public Response get(@PathParam("id") String id) throws ItemNotFound {
77 return getItem(id);
78 }
79
80 @GET
81 @Produces(MediaType.APPLICATION_JSON)
82 @Override
83 public Response list() {
84 return listItems();
85 }
86
87 @GET
88 @Produces(MediaType.APPLICATION_JSON)
89 @Path("{id:[^/]+}/list")
90 @Override
91 public Response listChildren(@PathParam("id") String id,
92 @QueryParam(ALL_PARAM) @DefaultValue("false") boolean all) throws ItemNotFound {
93 try (final Tx tx = beginTx()) {
94 Country country = api().detail(id, cls);
95 Response response = streamingPage(() -> getQuery()
96 .page(country.getRepositories(), Repository.class));
97 tx.success();
98 return response;
99 }
100 }
101
102 @POST
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
105 @Override
106 public Response create(Bundle bundle,
107 @QueryParam(ACCESSOR_PARAM) List<String> accessors)
108 throws PermissionDenied, ValidationError, DeserializationError {
109 try (Tx tx = beginTx()) {
110 Response item = createItem(bundle, accessors);
111 tx.success();
112 return item;
113 }
114 }
115
116 @PUT
117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
119 @Path("{id:[^/]+}")
120 @Override
121 public Response update(@PathParam("id") String id, Bundle bundle)
122 throws PermissionDenied, ValidationError,
123 DeserializationError, ItemNotFound {
124 try (Tx tx = beginTx()) {
125 Response item = updateItem(id, bundle);
126 tx.success();
127 return item;
128 }
129 }
130
131 @DELETE
132 @Path("{id:[^/]+}")
133 @Override
134 public void delete(@PathParam("id") String id)
135 throws PermissionDenied, ItemNotFound, ValidationError {
136 try (Tx tx = beginTx()) {
137 deleteItem(id);
138 tx.success();
139 }
140 }
141
142
143
144
145
146
147
148
149 @POST
150 @Consumes(MediaType.APPLICATION_JSON)
151 @Produces(MediaType.APPLICATION_JSON)
152 @Path("{id:[^/]+}")
153 @Override
154 public Response createChild(@PathParam("id") String id,
155 Bundle bundle, @QueryParam(ACCESSOR_PARAM) List<String> accessors)
156 throws PermissionDenied, ValidationError,
157 DeserializationError, ItemNotFound {
158 try (Tx tx = beginTx()) {
159 final Country country = api().detail(id, cls);
160 Response item = createItem(bundle, accessors,
161 repository -> repository.setCountry(country),
162 api().withScope(country), Repository.class);
163 tx.success();
164 return item;
165 }
166 }
167
168
169
170
171
172
173
174
175
176 @GET
177 @Path("{id:[^/]+}/eag")
178 @Produces("application/zip")
179 public Response exportEag(
180 @PathParam("id") String id,
181 final @QueryParam("lang") @DefaultValue("eng") String lang)
182 throws IOException, ItemNotFound {
183 try (final Tx tx = beginTx()) {
184 final Country country = api().detail(id, cls);
185 final EagExporter eagExporter = new Eag2012Exporter(api());
186 Response response = exportItemsAsZip(eagExporter, country.getRepositories(), lang);
187 tx.success();
188 return response;
189 }
190 }
191 }