Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/stamm/OrtService.java @ 1227:a0968660a4c5
Use reusable validation rule instead of in-service implementation.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Fri, 09 Dec 2016 16:34:44 +0100 |
parents | 6e6134bf1d6d |
children | 32e27cb8cfef |
comparison
equal
deleted
inserted
replaced
1226:85a8221e957a | 1227:a0968660a4c5 |
---|---|
36 import de.intevation.lada.util.data.QueryBuilder; | 36 import de.intevation.lada.util.data.QueryBuilder; |
37 import de.intevation.lada.util.data.Repository; | 37 import de.intevation.lada.util.data.Repository; |
38 import de.intevation.lada.util.data.RepositoryType; | 38 import de.intevation.lada.util.data.RepositoryType; |
39 import de.intevation.lada.util.rest.RequestMethod; | 39 import de.intevation.lada.util.rest.RequestMethod; |
40 import de.intevation.lada.util.rest.Response; | 40 import de.intevation.lada.util.rest.Response; |
41 import de.intevation.lada.validation.Validator; | |
42 import de.intevation.lada.validation.Violation; | |
43 import de.intevation.lada.validation.annotation.ValidationConfig; | |
41 | 44 |
42 /** | 45 /** |
43 * REST service for SOrt objects. | 46 * REST service for SOrt objects. |
44 * <p> | 47 * <p> |
45 * The services produce data in the application/json media type. | 48 * The services produce data in the application/json media type. |
93 private Authorization authorization; | 96 private Authorization authorization; |
94 | 97 |
95 @Inject | 98 @Inject |
96 private OrtFactory ortFactory; | 99 private OrtFactory ortFactory; |
97 | 100 |
101 @Inject | |
102 @ValidationConfig(type="Ort") | |
103 private Validator validator; | |
104 | |
98 /** | 105 /** |
99 * Get all SOrt objects. | 106 * Get all SOrt objects. |
100 * <p> | 107 * <p> |
101 * The requested objects can be filtered using a URL parameter named | 108 * The requested objects can be filtered using a URL parameter named |
102 * ortId. | 109 * ortId. |
260 RequestMethod.POST, | 267 RequestMethod.POST, |
261 Ort.class) | 268 Ort.class) |
262 ) { | 269 ) { |
263 return new Response(false, 699, ort); | 270 return new Response(false, 699, ort); |
264 } | 271 } |
265 QueryBuilder<Ort> builder = | 272 |
266 new QueryBuilder<Ort>( | 273 Violation violation = validator.validate(ort); |
267 repository.entityManager("stamm"), | 274 if (violation.hasErrors()) { |
268 Ort.class | 275 Response response = new Response(false, 604, ort); |
269 ); | 276 response.setErrors(violation.getErrors()); |
270 builder.and("ortId", ort.getOrtId()); | 277 response.setWarnings(violation.getWarnings()); |
271 builder.and("netzbetreiberId", ort.getNetzbetreiberId()); | 278 return response; |
272 | 279 } |
273 List<Ort> orte = | 280 |
274 repository.filterPlain(builder.getQuery(), "stamm"); | 281 ortFactory.transformCoordinates(ort); |
275 if (orte.isEmpty() || | 282 Response response = repository.create(ort, "stamm"); |
276 orte.get(0).getId() == ort.getId()) { | 283 if(violation.hasWarnings()) { |
277 ortFactory.transformCoordinates(ort); | 284 response.setWarnings(violation.getWarnings()); |
278 return repository.create(ort, "stamm"); | 285 } |
279 } | 286 |
280 return new Response(false, 672, null); | 287 return response; |
281 } | 288 } |
282 | 289 |
283 /** | 290 /** |
284 * Update an existing SOrt object. | 291 * Update an existing SOrt object. |
285 * <p> | 292 * <p> |