Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/ProbeService.java @ 1028:1c41c7b8f7c2 schema-update
Updated server application to new database model. THIS IS STILL WIP!!!
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 08 Jul 2016 15:32:36 +0200 |
parents | 2c927b3edb97 |
children | 61354a9fa58d |
comparison
equal
deleted
inserted
replaced
1027:9971471d562c | 1028:1c41c7b8f7c2 |
---|---|
35 | 35 |
36 import de.intevation.lada.factory.ProbeFactory; | 36 import de.intevation.lada.factory.ProbeFactory; |
37 import de.intevation.lada.lock.LockConfig; | 37 import de.intevation.lada.lock.LockConfig; |
38 import de.intevation.lada.lock.LockType; | 38 import de.intevation.lada.lock.LockType; |
39 import de.intevation.lada.lock.ObjectLocker; | 39 import de.intevation.lada.lock.ObjectLocker; |
40 import de.intevation.lada.model.land.LProbe; | 40 import de.intevation.lada.model.land.Probe; |
41 import de.intevation.lada.model.land.ProbeTranslation; | |
42 import de.intevation.lada.query.QueryTools; | 41 import de.intevation.lada.query.QueryTools; |
43 import de.intevation.lada.util.annotation.AuthorizationConfig; | 42 import de.intevation.lada.util.annotation.AuthorizationConfig; |
44 import de.intevation.lada.util.annotation.RepositoryConfig; | 43 import de.intevation.lada.util.annotation.RepositoryConfig; |
45 import de.intevation.lada.util.auth.Authorization; | 44 import de.intevation.lada.util.auth.Authorization; |
46 import de.intevation.lada.util.auth.AuthorizationType; | 45 import de.intevation.lada.util.auth.AuthorizationType; |
179 @Context UriInfo info, | 178 @Context UriInfo info, |
180 @Context HttpServletRequest request | 179 @Context HttpServletRequest request |
181 ) { | 180 ) { |
182 MultivaluedMap<String, String> params = info.getQueryParameters(); | 181 MultivaluedMap<String, String> params = info.getQueryParameters(); |
183 if (params.isEmpty() || !params.containsKey("qid")) { | 182 if (params.isEmpty() || !params.containsKey("qid")) { |
184 return repository.getAll(LProbe.class, "land"); | 183 return repository.getAll(Probe.class, "land"); |
185 } | 184 } |
186 Integer id = null; | 185 Integer id = null; |
187 try { | 186 try { |
188 id = Integer.valueOf(params.getFirst("qid")); | 187 id = Integer.valueOf(params.getFirst("qid")); |
189 } | 188 } |
202 end = result.size(); | 201 end = result.size(); |
203 } | 202 } |
204 result = result.subList(start, end); | 203 result = result.subList(start, end); |
205 } | 204 } |
206 | 205 |
207 QueryBuilder<LProbe> pBuilder = new QueryBuilder<LProbe>( | 206 QueryBuilder<Probe> pBuilder = new QueryBuilder<Probe>( |
208 repository.entityManager("land"), LProbe.class); | 207 repository.entityManager("land"), Probe.class); |
209 for (Map<String, Object> entry: result) { | 208 for (Map<String, Object> entry: result) { |
210 pBuilder.or("id", (Integer)entry.get("id")); | 209 pBuilder.or("id", (Integer)entry.get("id")); |
211 } | 210 } |
212 Response r = repository.filter(pBuilder.getQuery(), "land"); | 211 Response r = repository.filter(pBuilder.getQuery(), "land"); |
213 r = authorization.filter(request, r, LProbe.class); | 212 r = authorization.filter(request, r, Probe.class); |
214 List<LProbe> proben = (List<LProbe>)r.getData(); | 213 List<Probe> proben = (List<Probe>)r.getData(); |
215 for (Map<String, Object> entry: result) { | 214 for (Map<String, Object> entry: result) { |
216 Integer pId = Integer.valueOf(entry.get("id").toString()); | 215 Integer pId = Integer.valueOf(entry.get("id").toString()); |
217 setAuthData(proben, entry, pId); | 216 setAuthData(proben, entry, pId); |
218 } | 217 } |
219 return new Response(true, 200, result, size); | 218 return new Response(true, 200, result, size); |
220 } | 219 } |
221 | 220 |
222 private void setAuthData( | 221 private void setAuthData( |
223 List<LProbe> proben, | 222 List<Probe> proben, |
224 Map<String, Object> entry, | 223 Map<String, Object> entry, |
225 Integer id | 224 Integer id |
226 ) { | 225 ) { |
227 for (int i = 0; i < proben.size(); i++) { | 226 for (int i = 0; i < proben.size(); i++) { |
228 if (id.equals(proben.get(i).getId())) { | 227 if (id.equals(proben.get(i).getId())) { |
229 entry.put("readonly", proben.get(i).getReadonly()); | 228 entry.put("readonly", proben.get(i).isReadonly()); |
230 entry.put("owner", proben.get(i).getOwner()); | 229 entry.put("owner", proben.get(i).isOwner()); |
231 return; | 230 return; |
232 } | 231 } |
233 } | 232 } |
234 } | 233 } |
235 | 234 |
249 @Context HttpHeaders headers, | 248 @Context HttpHeaders headers, |
250 @PathParam("id") String id, | 249 @PathParam("id") String id, |
251 @Context HttpServletRequest request | 250 @Context HttpServletRequest request |
252 ) { | 251 ) { |
253 Response response = | 252 Response response = |
254 repository.getById(LProbe.class, Integer.valueOf(id), "land"); | 253 repository.getById(Probe.class, Integer.valueOf(id), "land"); |
255 Violation violation = validator.validate(response.getData()); | 254 Violation violation = validator.validate(response.getData()); |
256 if (violation.hasWarnings()) { | 255 if (violation.hasWarnings()) { |
257 response.setWarnings(violation.getWarnings()); | 256 response.setWarnings(violation.getWarnings()); |
258 } | 257 } |
259 return this.authorization.filter(request, response, LProbe.class); | 258 return this.authorization.filter(request, response, Probe.class); |
260 } | 259 } |
261 | 260 |
262 /** | 261 /** |
263 * Create a new Probe object. | 262 * Create a new Probe object. |
264 * <p> | 263 * <p> |
300 @Path("/") | 299 @Path("/") |
301 @Produces(MediaType.APPLICATION_JSON) | 300 @Produces(MediaType.APPLICATION_JSON) |
302 public Response create( | 301 public Response create( |
303 @Context HttpHeaders headers, | 302 @Context HttpHeaders headers, |
304 @Context HttpServletRequest request, | 303 @Context HttpServletRequest request, |
305 LProbe probe | 304 Probe probe |
306 ) { | 305 ) { |
307 if (!authorization.isAuthorized( | 306 if (!authorization.isAuthorized( |
308 request, | 307 request, |
309 probe, | 308 probe, |
310 RequestMethod.POST, | 309 RequestMethod.POST, |
311 LProbe.class) | 310 Probe.class) |
312 ) { | 311 ) { |
313 return new Response(false, 699, null); | 312 return new Response(false, 699, null); |
314 } | 313 } |
315 Violation violation = validator.validate(probe); | 314 Violation violation = validator.validate(probe); |
316 if (violation.hasErrors()) { | 315 if (violation.hasErrors()) { |
323 probe = factory.findUmweltId(probe); | 322 probe = factory.findUmweltId(probe); |
324 } | 323 } |
325 probe = factory.findMediaDesk(probe); | 324 probe = factory.findMediaDesk(probe); |
326 /* Persist the new probe object*/ | 325 /* Persist the new probe object*/ |
327 Response newProbe = repository.create(probe, "land"); | 326 Response newProbe = repository.create(probe, "land"); |
328 LProbe ret = (LProbe)newProbe.getData(); | 327 Probe ret = (Probe)newProbe.getData(); |
329 /* Create and persist a new probe translation object*/ | 328 /* Create and persist a new probe translation object*/ |
330 ProbeTranslation trans = new ProbeTranslation(); | |
331 trans.setProbeId(ret); | |
332 repository.create(trans, "land"); | |
333 /* Get and return the new probe object*/ | |
334 Response response = | |
335 repository.getById(LProbe.class, ret.getId(), "land"); | |
336 if(violation.hasWarnings()) { | 329 if(violation.hasWarnings()) { |
337 response.setWarnings(violation.getWarnings()); | 330 newProbe.setWarnings(violation.getWarnings()); |
338 } | 331 } |
339 return authorization.filter( | 332 return authorization.filter( |
340 request, | 333 request, |
341 response, | 334 newProbe, |
342 LProbe.class); | 335 Probe.class); |
343 } | 336 } |
344 | 337 |
345 /** | 338 /** |
346 * Create new Probe objects from a messprogramm. | 339 * Create new Probe objects from a messprogramm. |
347 * <p> | 340 * <p> |
367 JsonObject object | 360 JsonObject object |
368 ) { | 361 ) { |
369 String id = object.get("id").toString(); | 362 String id = object.get("id").toString(); |
370 long start = object.getJsonNumber("start").longValue(); | 363 long start = object.getJsonNumber("start").longValue(); |
371 long end = object.getJsonNumber("end").longValue(); | 364 long end = object.getJsonNumber("end").longValue(); |
372 List<LProbe> proben = factory.create( | 365 List<Probe> proben = factory.create( |
373 id, | 366 id, |
374 start, | 367 start, |
375 end); | 368 end); |
376 return new Response(true, 200, proben); | 369 return new Response(true, 200, proben); |
377 } | 370 } |
417 @Path("/{id}") | 410 @Path("/{id}") |
418 @Produces(MediaType.APPLICATION_JSON) | 411 @Produces(MediaType.APPLICATION_JSON) |
419 public Response update( | 412 public Response update( |
420 @Context HttpHeaders headers, | 413 @Context HttpHeaders headers, |
421 @Context HttpServletRequest request, | 414 @Context HttpServletRequest request, |
422 LProbe probe | 415 Probe probe |
423 ) { | 416 ) { |
424 if (!authorization.isAuthorized( | 417 if (!authorization.isAuthorized( |
425 request, | 418 request, |
426 probe, | 419 probe, |
427 RequestMethod.PUT, | 420 RequestMethod.PUT, |
428 LProbe.class) | 421 Probe.class) |
429 ) { | 422 ) { |
430 return new Response(false, 699, null); | 423 return new Response(false, 699, null); |
431 } | 424 } |
432 if (lock.isLocked(probe)) { | 425 if (lock.isLocked(probe)) { |
433 return new Response(false, 697, null); | 426 return new Response(false, 697, null); |
448 Response response = repository.update(probe, "land"); | 441 Response response = repository.update(probe, "land"); |
449 if (!response.getSuccess()) { | 442 if (!response.getSuccess()) { |
450 return response; | 443 return response; |
451 } | 444 } |
452 Response updated = repository.getById( | 445 Response updated = repository.getById( |
453 LProbe.class, | 446 Probe.class, |
454 ((LProbe)response.getData()).getId(), "land"); | 447 ((Probe)response.getData()).getId(), "land"); |
455 if (violation.hasWarnings()) { | 448 if (violation.hasWarnings()) { |
456 updated.setWarnings(violation.getWarnings()); | 449 updated.setWarnings(violation.getWarnings()); |
457 } | 450 } |
458 return authorization.filter( | 451 return authorization.filter( |
459 request, | 452 request, |
460 updated, | 453 updated, |
461 LProbe.class); | 454 Probe.class); |
462 } | 455 } |
463 | 456 |
464 /** | 457 /** |
465 * Delete an existing Probe object by id. | 458 * Delete an existing Probe object by id. |
466 * <p> | 459 * <p> |
478 @Context HttpServletRequest request, | 471 @Context HttpServletRequest request, |
479 @PathParam("id") String id | 472 @PathParam("id") String id |
480 ) { | 473 ) { |
481 /* Get the probe object by id*/ | 474 /* Get the probe object by id*/ |
482 Response probe = | 475 Response probe = |
483 repository.getById(LProbe.class, Integer.valueOf(id), "land"); | 476 repository.getById(Probe.class, Integer.valueOf(id), "land"); |
484 LProbe probeObj = (LProbe)probe.getData(); | 477 Probe probeObj = (Probe)probe.getData(); |
485 if (!authorization.isAuthorized( | 478 if (!authorization.isAuthorized( |
486 request, | 479 request, |
487 probeObj, | 480 probeObj, |
488 RequestMethod.DELETE, | 481 RequestMethod.DELETE, |
489 LProbe.class) | 482 Probe.class) |
490 ) { | 483 ) { |
491 return new Response(false, 699, null); | 484 return new Response(false, 699, null); |
492 } | 485 } |
493 /* Delete the probe object*/ | 486 /* Delete the probe object*/ |
494 try { | 487 try { |