Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/rest/StatusService.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 | ad69878b7280 |
children | 4d95cc7f0a43 |
comparison
equal
deleted
inserted
replaced
1027:9971471d562c | 1028:1c41c7b8f7c2 |
---|---|
30 import org.apache.log4j.Logger; | 30 import org.apache.log4j.Logger; |
31 | 31 |
32 import de.intevation.lada.lock.LockConfig; | 32 import de.intevation.lada.lock.LockConfig; |
33 import de.intevation.lada.lock.LockType; | 33 import de.intevation.lada.lock.LockType; |
34 import de.intevation.lada.lock.ObjectLocker; | 34 import de.intevation.lada.lock.ObjectLocker; |
35 import de.intevation.lada.model.land.LMessung; | 35 import de.intevation.lada.model.land.Messung; |
36 import de.intevation.lada.model.land.LProbe; | 36 import de.intevation.lada.model.land.Probe; |
37 import de.intevation.lada.model.land.LStatusProtokoll; | 37 import de.intevation.lada.model.land.StatusProtokoll; |
38 import de.intevation.lada.model.stamm.MessStelle; | 38 import de.intevation.lada.model.stammdaten.MessStelle; |
39 import de.intevation.lada.model.stammdaten.StatusKombi; | |
40 import de.intevation.lada.model.stammdaten.StatusReihenfolge; | |
39 import de.intevation.lada.util.annotation.AuthorizationConfig; | 41 import de.intevation.lada.util.annotation.AuthorizationConfig; |
40 import de.intevation.lada.util.annotation.RepositoryConfig; | 42 import de.intevation.lada.util.annotation.RepositoryConfig; |
41 import de.intevation.lada.util.auth.Authorization; | 43 import de.intevation.lada.util.auth.Authorization; |
42 import de.intevation.lada.util.auth.AuthorizationType; | 44 import de.intevation.lada.util.auth.AuthorizationType; |
43 import de.intevation.lada.util.auth.UserInfo; | 45 import de.intevation.lada.util.auth.UserInfo; |
147 } | 149 } |
148 catch(NumberFormatException nfe) { | 150 catch(NumberFormatException nfe) { |
149 return new Response(false, 698, null); | 151 return new Response(false, 698, null); |
150 } | 152 } |
151 | 153 |
152 QueryBuilder<LStatusProtokoll> builder = | 154 QueryBuilder<StatusProtokoll> builder = |
153 new QueryBuilder<LStatusProtokoll>( | 155 new QueryBuilder<StatusProtokoll>( |
154 defaultRepo.entityManager("land"), | 156 defaultRepo.entityManager("land"), |
155 LStatusProtokoll.class); | 157 StatusProtokoll.class); |
156 builder.and("messungsId", id); | 158 builder.and("messungsId", id); |
157 return authorization.filter( | 159 return authorization.filter( |
158 request, | 160 request, |
159 defaultRepo.filter(builder.getQuery(), "land"), | 161 defaultRepo.filter(builder.getQuery(), "land"), |
160 LStatusProtokoll.class); | 162 StatusProtokoll.class); |
161 } | 163 } |
162 | 164 |
163 /** | 165 /** |
164 * Get a single Status object by id. | 166 * Get a single Status object by id. |
165 * <p> | 167 * <p> |
176 @Context HttpHeaders headers, | 178 @Context HttpHeaders headers, |
177 @Context HttpServletRequest request, | 179 @Context HttpServletRequest request, |
178 @PathParam("id") String id | 180 @PathParam("id") String id |
179 ) { | 181 ) { |
180 Response response = defaultRepo.getById( | 182 Response response = defaultRepo.getById( |
181 LStatusProtokoll.class, | 183 StatusProtokoll.class, |
182 Integer.valueOf(id), | 184 Integer.valueOf(id), |
183 "land"); | 185 "land"); |
184 LStatusProtokoll status = (LStatusProtokoll)response.getData(); | 186 StatusProtokoll status = (StatusProtokoll)response.getData(); |
185 Violation violation = validator.validate(status); | 187 Violation violation = validator.validate(status); |
186 if (violation.hasErrors() || violation.hasWarnings()) { | 188 if (violation.hasErrors() || violation.hasWarnings()) { |
187 response.setErrors(violation.getErrors()); | 189 response.setErrors(violation.getErrors()); |
188 response.setWarnings(violation.getWarnings()); | 190 response.setWarnings(violation.getWarnings()); |
189 } | 191 } |
190 | 192 |
191 return authorization.filter( | 193 return authorization.filter( |
192 request, | 194 request, |
193 response, | 195 response, |
194 LStatusProtokoll.class); | 196 StatusProtokoll.class); |
195 } | 197 } |
196 | 198 |
197 /** | 199 /** |
198 * Create a Status object. | 200 * Create a Status object. |
199 * <p> | 201 * <p> |
220 @Path("/") | 222 @Path("/") |
221 @Produces(MediaType.APPLICATION_JSON) | 223 @Produces(MediaType.APPLICATION_JSON) |
222 public Response create( | 224 public Response create( |
223 @Context HttpHeaders headers, | 225 @Context HttpHeaders headers, |
224 @Context HttpServletRequest request, | 226 @Context HttpServletRequest request, |
225 LStatusProtokoll status | 227 StatusProtokoll status |
226 ) { | 228 ) { |
227 if (status.getMessungsId() == null | 229 if (status.getMessungsId() == null |
228 || status.getErzeuger() == null | 230 || status.getMstId() == null |
229 || status.getStatusWert() == null | |
230 ) { | 231 ) { |
231 return new Response(false, 631, null); | 232 return new Response(false, 631, null); |
232 } | 233 } |
233 | 234 |
234 UserInfo userInfo = authorization.getInfo(request); | 235 UserInfo userInfo = authorization.getInfo(request); |
235 LMessung messung = defaultRepo.getByIdPlain( | 236 Messung messung = defaultRepo.getByIdPlain( |
236 LMessung.class, status.getMessungsId(), "land"); | 237 Messung.class, status.getMessungsId(), "land"); |
237 if (lock.isLocked(messung)) { | 238 if (lock.isLocked(messung)) { |
238 return new Response(false, 697, null); | 239 return new Response(false, 697, null); |
239 } | 240 } |
240 | 241 |
241 // Is user authorized to edit status at all? | 242 // Is user authorized to edit status at all? |
242 Response r = authorization.filter( | 243 Response r = authorization.filter( |
243 request, | 244 request, |
244 new Response(true, 200, messung), | 245 new Response(true, 200, messung), |
245 LMessung.class); | 246 Messung.class); |
246 LMessung filteredMessung = (LMessung)r.getData(); | 247 Messung filteredMessung = (Messung)r.getData(); |
247 if (filteredMessung.getStatusEdit() == false) { | 248 if (filteredMessung.getStatusEdit() == false) { |
248 return new Response(false, 699, null); | 249 return new Response(false, 699, null); |
249 } | 250 } |
250 | 251 |
251 if (messung.getStatus() == null) { | 252 if (messung.getStatus() == null) { |
252 status.setStatusStufe(1); | 253 // set the first status as default |
254 status.setStatusKombi(1); | |
253 } | 255 } |
254 else { | 256 else { |
255 LStatusProtokoll currentStatus = defaultRepo.getByIdPlain( | 257 StatusProtokoll currentStatus = defaultRepo.getByIdPlain( |
256 LStatusProtokoll.class, messung.getStatus(), "land"); | 258 StatusProtokoll.class, messung.getStatus(), "land"); |
257 | 259 |
258 String probeMstId = defaultRepo.getByIdPlain( | 260 String probeMstId = defaultRepo.getByIdPlain( |
259 LProbe.class, | 261 Probe.class, |
260 messung.getProbeId(), | 262 messung.getProbeId(), |
261 "land").getMstId(); | 263 "land").getMstId(); |
262 | 264 |
263 if (currentStatus.getStatusWert() == 4) { | 265 StatusKombi kombiCurrent = defaultRepo.getByIdPlain(StatusKombi.class, currentStatus.getStatusKombi(), "stamm"); |
264 if (status.getStatusWert() == 4 | 266 StatusKombi kombiStatus = defaultRepo.getByIdPlain(StatusKombi.class, status.getStatusKombi(), "stamm"); |
265 && userInfo.getMessstellen().contains( | 267 if (kombiCurrent.getStatusWert().getId() == 4) { |
266 currentStatus.getErzeuger()) | 268 if (userInfo.getMessstellen().contains( |
267 && status.getErzeuger().equals( | 269 currentStatus.getMstId()) |
268 currentStatus.getErzeuger()) | 270 && status.getMstId().equals( |
271 currentStatus.getMstId()) | |
269 ) { | 272 ) { |
270 // 'edit' currentStatus | 273 // 'edit' currentStatus |
271 status.setStatusStufe(currentStatus.getStatusStufe()); | 274 status.setStatusKombi(kombiCurrent.getId()); |
272 } | 275 } |
273 else if ( | 276 else if ( |
274 userInfo.getFunktionenForMst(probeMstId) | 277 userInfo.getFunktionenForMst(probeMstId) |
275 .contains(1) | 278 .contains(1) |
276 && probeMstId.equals(status.getErzeuger()) | 279 && probeMstId.equals(status.getMstId()) |
277 ) { | 280 ) { |
278 status.setStatusStufe(1); | 281 status.setStatusKombi(1); |
279 } | 282 } |
280 else { | 283 else { |
281 return new Response(false, 699, null); | 284 return new Response(false, 699, null); |
282 } | 285 } |
283 } | 286 } |
294 // one function for the matching Messstelle. | 297 // one function for the matching Messstelle. |
295 | 298 |
296 // XXX: It's assumed here, that an 'Erzeuger' is an instance | 299 // XXX: It's assumed here, that an 'Erzeuger' is an instance |
297 // of 'Messstelle', but the model does not enforce it! | 300 // of 'Messstelle', but the model does not enforce it! |
298 for (Integer function : | 301 for (Integer function : |
299 userInfo.getFunktionenForMst(status.getErzeuger()) | 302 userInfo.getFunktionenForMst(status.getMstId()) |
300 ) { | 303 ) { |
301 if (function.equals(currentStatus.getStatusStufe() + 1) | 304 if (function.equals(kombiCurrent.getStatusStufe().getId() + 1) |
302 && currentStatus.getStatusWert() != 0) { | 305 && kombiCurrent.getStatusWert().getId() != 0) { |
303 next = true; | 306 next = true; |
304 } | 307 } |
305 else if (function == currentStatus.getStatusStufe()) { | 308 else if (function == kombiCurrent.getStatusStufe().getId()) { |
306 if (currentStatus.getStatusStufe() == 1 | 309 if (kombiCurrent.getStatusStufe().getId() == 1 |
307 && !status.getErzeuger().equals(probeMstId)) { | 310 && !status.getMstId().equals(probeMstId)) { |
308 logger.debug( | 311 logger.debug( |
309 "Messstelle does not match for change"); | 312 "Messstelle does not match for change"); |
310 return new Response(false, 699, null); | 313 return new Response(false, 699, null); |
311 } | 314 } |
312 | 315 |
313 String pNetzbetreiber = defaultRepo.getByIdPlain( | 316 String pNetzbetreiber = defaultRepo.getByIdPlain( |
314 LProbe.class, | 317 MessStelle.class, |
315 messung.getProbeId(), | 318 probeMstId, |
316 "land").getNetzbetreiberId(); | 319 "land").getNetzbetreiberId(); |
317 String sNetzbetreiber = defaultRepo.getByIdPlain( | 320 String sNetzbetreiber = defaultRepo.getByIdPlain( |
318 MessStelle.class, | 321 MessStelle.class, |
319 status.getErzeuger(), | 322 status.getMstId(), |
320 "stamm").getNetzbetreiberId(); | 323 "stamm").getNetzbetreiberId(); |
321 if (currentStatus.getStatusStufe() == 2 | 324 if (kombiCurrent.getStatusStufe().getId() == 2 |
322 && !pNetzbetreiber.equals(sNetzbetreiber)){ | 325 && !pNetzbetreiber.equals(sNetzbetreiber)){ |
323 logger.debug( | 326 logger.debug( |
324 "Netzbetreiber does not match for change"); | 327 "Netzbetreiber does not match for change"); |
325 return new Response(false, 699, null); | 328 return new Response(false, 699, null); |
326 } | 329 } |
327 change = true; | 330 change = true; |
328 } | 331 } |
329 } | 332 } |
330 | 333 |
334 QueryBuilder<StatusKombi> builder = | |
335 new QueryBuilder<StatusKombi>(defaultRepo.entityManager("stamm"), StatusKombi.class); | |
331 if (change && | 336 if (change && |
332 status.getStatusWert() == 4 && | 337 kombiStatus.getStatusWert().getId() == 4 && |
333 status.getStatusStufe() > 1 | 338 kombiStatus.getStatusStufe().getId() > 1 |
334 ) { | 339 ) { |
335 status.setStatusStufe(currentStatus.getStatusStufe()); | 340 builder.and("StatusStufe", kombiCurrent.getStatusStufe()) |
336 } | 341 .and("statusWert", 4); |
337 else if (change && status.getStatusWert() == 8) { | 342 status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId()); |
343 // status.setStatusStufe(currentStatus.getStatusStufe()); | |
344 } | |
345 else if (change && kombiStatus.getStatusWert().getId() == 8) { | |
338 return authorization.filter( | 346 return authorization.filter( |
339 request, | 347 request, |
340 resetStatus(status, currentStatus, messung), | 348 resetStatus(status, currentStatus, messung), |
341 LStatusProtokoll.class); | 349 StatusProtokoll.class); |
342 } | 350 } |
343 else if (change && status.getStatusWert() != 0) { | 351 else if (change && kombiStatus.getStatusWert().getId() != 0) { |
344 status.setStatusStufe(currentStatus.getStatusStufe()); | 352 builder.and("StatusStufe", kombiCurrent.getStatusStufe()) |
353 .and("statusWert", kombiStatus.getStatusStufe()); | |
354 status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId()); | |
345 } | 355 } |
346 else if (next && | 356 else if (next && |
347 (status.getStatusWert() > 0 && | 357 (kombiStatus.getStatusWert().getId() > 0 && |
348 status.getStatusWert() <= 4 || | 358 kombiStatus.getStatusWert().getId() <= 4 || |
349 status.getStatusWert() == 7)) { | 359 kombiStatus.getStatusWert().getId() == 7)) { |
350 status.setStatusStufe(currentStatus.getStatusStufe() + 1); | 360 builder.and("StatusStufe", kombiCurrent.getStatusStufe().getId() + 1) |
361 .and("statusWert", kombiStatus.getStatusStufe()); | |
362 status.setStatusKombi(defaultRepo.filterPlain(builder.getQuery(),"stamm").get(0).getId()); | |
351 } | 363 } |
352 else { | 364 else { |
353 return new Response(false, 699, null); | 365 return new Response(false, 699, null); |
354 } | 366 } |
355 } | 367 } |
356 | 368 |
357 // auto-set 'fertig'-flag | 369 // auto-set 'fertig'-flag |
358 if (status.getStatusStufe() == 1) { | 370 if (kombiStatus.getStatusStufe().getId() == 1) { |
359 messung.setFertig(true); | 371 messung.setFertig(true); |
360 } | 372 } |
361 else if (status.getStatusWert() == 4) { | 373 else if (kombiStatus.getStatusWert().getId() == 4) { |
362 messung.setFertig(false); | 374 messung.setFertig(false); |
363 } | 375 } |
364 } | 376 } |
365 Violation violation = validator.validate(status); | 377 Violation violation = validator.validate(status); |
366 if (violation.hasErrors()) { | 378 if (violation.hasErrors()) { |
368 response.setErrors(violation.getErrors()); | 380 response.setErrors(violation.getErrors()); |
369 response.setWarnings(violation.getWarnings()); | 381 response.setWarnings(violation.getWarnings()); |
370 return response; | 382 return response; |
371 } | 383 } |
372 Response response = defaultRepo.create(status, "land"); | 384 Response response = defaultRepo.create(status, "land"); |
373 LStatusProtokoll created = (LStatusProtokoll)response.getData(); | 385 StatusProtokoll created = (StatusProtokoll)response.getData(); |
374 messung.setStatus(created.getId()); | 386 messung.setStatus(created.getId()); |
375 defaultRepo.update(messung, "land"); | 387 defaultRepo.update(messung, "land"); |
376 /* Persist the new object*/ | 388 /* Persist the new object*/ |
377 return authorization.filter( | 389 return authorization.filter( |
378 request, | 390 request, |
379 response, | 391 response, |
380 LStatusProtokoll.class); | 392 StatusProtokoll.class); |
381 } | 393 } |
382 | 394 |
383 /** | 395 /** |
384 * Update an existing Status object. | 396 * Update an existing Status object. |
385 * <p> | 397 * <p> |
406 @Path("/{id}") | 418 @Path("/{id}") |
407 @Produces(MediaType.APPLICATION_JSON) | 419 @Produces(MediaType.APPLICATION_JSON) |
408 public Response update( | 420 public Response update( |
409 @Context HttpHeaders headers, | 421 @Context HttpHeaders headers, |
410 @Context HttpServletRequest request, | 422 @Context HttpServletRequest request, |
411 LStatusProtokoll status | 423 StatusProtokoll status |
412 ) { | 424 ) { |
413 return new Response(false, 699, null); | 425 return new Response(false, 699, null); |
414 } | 426 } |
415 | 427 |
416 /** | 428 /** |
430 @Context HttpServletRequest request, | 442 @Context HttpServletRequest request, |
431 @PathParam("id") String id | 443 @PathParam("id") String id |
432 ) { | 444 ) { |
433 /* Get the object by id*/ | 445 /* Get the object by id*/ |
434 Response object = | 446 Response object = |
435 defaultRepo.getById(LStatusProtokoll.class, Integer.valueOf(id), "land"); | 447 defaultRepo.getById(StatusProtokoll.class, Integer.valueOf(id), "land"); |
436 LStatusProtokoll obj = (LStatusProtokoll)object.getData(); | 448 StatusProtokoll obj = (StatusProtokoll)object.getData(); |
437 if (!authorization.isAuthorized( | 449 if (!authorization.isAuthorized( |
438 request, | 450 request, |
439 obj, | 451 obj, |
440 RequestMethod.DELETE, | 452 RequestMethod.DELETE, |
441 LStatusProtokoll.class) | 453 StatusProtokoll.class) |
442 ) { | 454 ) { |
443 return new Response(false, 699, null); | 455 return new Response(false, 699, null); |
444 } | 456 } |
445 if (lock.isLocked(obj)) { | 457 if (lock.isLocked(obj)) { |
446 return new Response(false, 697, null); | 458 return new Response(false, 697, null); |
448 /* Delete the object*/ | 460 /* Delete the object*/ |
449 return defaultRepo.delete(obj, "land"); | 461 return defaultRepo.delete(obj, "land"); |
450 } | 462 } |
451 | 463 |
452 private Response resetStatus( | 464 private Response resetStatus( |
453 LStatusProtokoll status, | 465 StatusProtokoll status, |
454 LStatusProtokoll currentStatus, | 466 StatusProtokoll currentStatus, |
455 LMessung messung | 467 Messung messung |
456 ) { | 468 ) { |
457 // Create a new Status with value = 8. | 469 // Create a new Status with value = 8. |
458 LStatusProtokoll statusNew = new LStatusProtokoll(); | 470 StatusProtokoll statusNew = new StatusProtokoll(); |
459 statusNew.setDatum(new Timestamp(new Date().getTime())); | 471 statusNew.setDatum(new Timestamp(new Date().getTime())); |
460 statusNew.setErzeuger(status.getErzeuger()); | 472 statusNew.setMstId(status.getMstId()); |
461 statusNew.setMessungsId(status.getMessungsId()); | 473 statusNew.setMessungsId(status.getMessungsId()); |
462 statusNew.setStatusStufe(currentStatus.getStatusStufe()); | 474 // statusNew.setStatusKombi(currentStatus.getStatusStufe()); |
463 statusNew.setStatusWert(8); | 475 // statusNew.setStatusWert(8); |
464 statusNew.setText("Reset"); | 476 // statusNew.setText("Reset"); |
465 | 477 |
466 defaultRepo.create(statusNew, "land"); | 478 defaultRepo.create(statusNew, "land"); |
467 | 479 |
468 Response retValue; | 480 Response retValue; |
469 if (currentStatus.getStatusStufe() == 1) { | 481 StatusKombi kombi = defaultRepo.getByIdPlain( |
470 LStatusProtokoll nV = new LStatusProtokoll(); | 482 StatusKombi.class, |
483 currentStatus.getStatusKombi(), | |
484 "stamm"); | |
485 if (kombi.getStatusStufe().getId() == 1) { | |
486 StatusProtokoll nV = new StatusProtokoll(); | |
471 nV.setDatum(new Timestamp(new Date().getTime())); | 487 nV.setDatum(new Timestamp(new Date().getTime())); |
472 nV.setErzeuger(status.getErzeuger()); | 488 nV.setMstId(status.getMstId()); |
473 nV.setMessungsId(status.getMessungsId()); | 489 nV.setMessungsId(status.getMessungsId()); |
474 nV.setStatusStufe(1); | 490 nV.setStatusKombi(1); |
475 nV.setStatusWert(0); | |
476 nV.setText(""); | 491 nV.setText(""); |
477 retValue = defaultRepo.create(nV, "land"); | 492 retValue = defaultRepo.create(nV, "land"); |
478 messung.setStatus(((LStatusProtokoll)retValue.getData()).getId()); | 493 messung.setStatus(((StatusProtokoll)retValue.getData()).getId()); |
479 } | 494 } |
480 else { | 495 else { |
481 QueryBuilder<LStatusProtokoll> lastFilter = | 496 QueryBuilder<StatusProtokoll> lastFilter = |
482 new QueryBuilder<LStatusProtokoll>( | 497 new QueryBuilder<StatusProtokoll>( |
483 defaultRepo.entityManager("land"), | 498 defaultRepo.entityManager("land"), |
484 LStatusProtokoll.class); | 499 StatusProtokoll.class); |
485 lastFilter.and("messungsId", status.getMessungsId()); | 500 lastFilter.and("messungsId", status.getMessungsId()); |
486 lastFilter.and("statusStufe", currentStatus.getStatusStufe() - 1); | 501 // CHECK THIS! lastFilter.and("statusKombi", currentStatus.getStatusKombi() - 1); |
487 lastFilter.orderBy("datum", true); | 502 lastFilter.orderBy("datum", true); |
488 List<LStatusProtokoll> proto = | 503 List<StatusProtokoll> proto = |
489 defaultRepo.filterPlain(lastFilter.getQuery(), "land"); | 504 defaultRepo.filterPlain(lastFilter.getQuery(), "land"); |
490 LStatusProtokoll copy = new LStatusProtokoll(); | 505 StatusProtokoll copy = new StatusProtokoll(); |
491 LStatusProtokoll orig = proto.get(proto.size() - 1); | 506 StatusProtokoll orig = proto.get(proto.size() - 1); |
492 copy.setDatum(new Timestamp(new Date().getTime())); | 507 copy.setDatum(new Timestamp(new Date().getTime())); |
493 copy.setErzeuger(orig.getErzeuger()); | 508 copy.setMstId(orig.getMstId()); |
494 copy.setMessungsId(orig.getMessungsId()); | 509 copy.setMessungsId(orig.getMessungsId()); |
495 copy.setStatusStufe(orig.getStatusStufe()); | 510 copy.setStatusKombi(orig.getStatusKombi()); |
496 copy.setStatusWert(orig.getStatusWert()); | |
497 copy.setText(""); | 511 copy.setText(""); |
498 retValue = defaultRepo.create(copy, "land"); | 512 retValue = defaultRepo.create(copy, "land"); |
499 LStatusProtokoll createdCopy = (LStatusProtokoll)retValue.getData(); | 513 StatusProtokoll createdCopy = (StatusProtokoll)retValue.getData(); |
500 messung.setStatus(createdCopy.getId()); | 514 messung.setStatus(createdCopy.getId()); |
501 } | 515 } |
502 defaultRepo.update(messung, "land"); | 516 defaultRepo.update(messung, "land"); |
503 return retValue; | 517 return retValue; |
504 } | 518 } |