comparison src/main/java/de/intevation/lada/rest/MessungService.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
29 import javax.ws.rs.core.UriInfo; 29 import javax.ws.rs.core.UriInfo;
30 30
31 import de.intevation.lada.lock.LockConfig; 31 import de.intevation.lada.lock.LockConfig;
32 import de.intevation.lada.lock.LockType; 32 import de.intevation.lada.lock.LockType;
33 import de.intevation.lada.lock.ObjectLocker; 33 import de.intevation.lada.lock.ObjectLocker;
34 import de.intevation.lada.model.land.LMessung; 34 import de.intevation.lada.model.land.Messung;
35 import de.intevation.lada.model.land.LProbe; 35 import de.intevation.lada.model.land.Probe;
36 import de.intevation.lada.model.land.LStatusProtokoll; 36 import de.intevation.lada.model.land.StatusProtokoll;
37 import de.intevation.lada.model.land.MessungTranslation;
38 import de.intevation.lada.query.QueryTools; 37 import de.intevation.lada.query.QueryTools;
39 import de.intevation.lada.util.annotation.AuthorizationConfig; 38 import de.intevation.lada.util.annotation.AuthorizationConfig;
40 import de.intevation.lada.util.annotation.RepositoryConfig; 39 import de.intevation.lada.util.annotation.RepositoryConfig;
41 import de.intevation.lada.util.auth.Authorization; 40 import de.intevation.lada.util.auth.Authorization;
42 import de.intevation.lada.util.auth.AuthorizationType; 41 import de.intevation.lada.util.auth.AuthorizationType;
138 @Context HttpServletRequest request 137 @Context HttpServletRequest request
139 ) { 138 ) {
140 MultivaluedMap<String, String> params = info.getQueryParameters(); 139 MultivaluedMap<String, String> params = info.getQueryParameters();
141 if (params.isEmpty() || 140 if (params.isEmpty() ||
142 (!params.containsKey("probeId") && !params.containsKey("qid"))) { 141 (!params.containsKey("probeId") && !params.containsKey("qid"))) {
143 return repository.getAll(LMessung.class, "land"); 142 return repository.getAll(Messung.class, "land");
144 } 143 }
145 if (params.containsKey("probeId")) { 144 if (params.containsKey("probeId")) {
146 String probeId = params.getFirst("probeId"); 145 String probeId = params.getFirst("probeId");
147 QueryBuilder<LMessung> builder = 146 QueryBuilder<Messung> builder =
148 new QueryBuilder<LMessung>( 147 new QueryBuilder<Messung>(
149 repository.entityManager("land"), 148 repository.entityManager("land"),
150 LMessung.class); 149 Messung.class);
151 builder.and("probeId", probeId); 150 builder.and("probeId", probeId);
152 return authorization.filter( 151 return authorization.filter(
153 request, 152 request,
154 repository.filter(builder.getQuery(), "land"), 153 repository.filter(builder.getQuery(), "land"),
155 LMessung.class); 154 Messung.class);
156 } 155 }
157 else if (params.containsKey("qid")) { 156 else if (params.containsKey("qid")) {
158 Integer id = null; 157 Integer id = null;
159 try { 158 try {
160 id = Integer.valueOf(params.getFirst("qid")); 159 id = Integer.valueOf(params.getFirst("qid"));
174 end = result.size(); 173 end = result.size();
175 } 174 }
176 result = result.subList(start, end); 175 result = result.subList(start, end);
177 } 176 }
178 177
179 QueryBuilder<LMessung> pBuilder = new QueryBuilder<LMessung>( 178 QueryBuilder<Messung> pBuilder = new QueryBuilder<Messung>(
180 repository.entityManager("land"), LMessung.class); 179 repository.entityManager("land"), Messung.class);
181 for (Map<String, Object> entry: result) { 180 for (Map<String, Object> entry: result) {
182 pBuilder.or("id", (Integer)entry.get("id")); 181 pBuilder.or("id", (Integer)entry.get("id"));
183 } 182 }
184 Response r = repository.filter(pBuilder.getQuery(), "land"); 183 Response r = repository.filter(pBuilder.getQuery(), "land");
185 r = authorization.filter(request, r, LMessung.class); 184 r = authorization.filter(request, r, Messung.class);
186 List<LMessung> messungen= (List<LMessung>)r.getData(); 185 List<Messung> messungen= (List<Messung>)r.getData();
187 for (Map<String, Object> entry: result) { 186 for (Map<String, Object> entry: result) {
188 Integer pId = Integer.valueOf(entry.get("id").toString()); 187 Integer pId = Integer.valueOf(entry.get("id").toString());
189 setAuthData(messungen, entry, pId); 188 setAuthData(messungen, entry, pId);
190 } 189 }
191 return new Response(true, 200, result, size); 190 return new Response(true, 200, result, size);
192 } 191 }
193 return new Response(false, 603, "No valid paramter given."); 192 return new Response(false, 603, "No valid paramter given.");
194 } 193 }
195 194
196 private void setAuthData( 195 private void setAuthData(
197 List<LMessung> messungen, 196 List<Messung> messungen,
198 Map<String, Object> entry, 197 Map<String, Object> entry,
199 Integer id 198 Integer id
200 ) { 199 ) {
201 for (int i = 0; i < messungen.size(); i++) { 200 for (int i = 0; i < messungen.size(); i++) {
202 if (id.equals(messungen.get(i).getId())) { 201 if (id.equals(messungen.get(i).getId())) {
225 @Context HttpHeaders headers, 224 @Context HttpHeaders headers,
226 @Context HttpServletRequest request, 225 @Context HttpServletRequest request,
227 @PathParam("id") String id 226 @PathParam("id") String id
228 ) { 227 ) {
229 Response response = 228 Response response =
230 repository.getById(LMessung.class, Integer.valueOf(id), "land"); 229 repository.getById(Messung.class, Integer.valueOf(id), "land");
231 LMessung messung = (LMessung)response.getData(); 230 Messung messung = (Messung)response.getData();
232 Violation violation = validator.validate(messung); 231 Violation violation = validator.validate(messung);
233 if (violation.hasErrors() || violation.hasWarnings()) { 232 if (violation.hasErrors() || violation.hasWarnings()) {
234 response.setErrors(violation.getErrors()); 233 response.setErrors(violation.getErrors());
235 response.setWarnings(violation.getWarnings()); 234 response.setWarnings(violation.getWarnings());
236 } 235 }
237 return authorization.filter( 236 return authorization.filter(
238 request, 237 request,
239 response, 238 response,
240 LMessung.class); 239 Messung.class);
241 } 240 }
242 241
243 /** 242 /**
244 * Create a Messung object. 243 * Create a Messung object.
245 * <p> 244 * <p>
270 @Path("/") 269 @Path("/")
271 @Produces(MediaType.APPLICATION_JSON) 270 @Produces(MediaType.APPLICATION_JSON)
272 public Response create( 271 public Response create(
273 @Context HttpHeaders headers, 272 @Context HttpHeaders headers,
274 @Context HttpServletRequest request, 273 @Context HttpServletRequest request,
275 LMessung messung 274 Messung messung
276 ) { 275 ) {
277 if (!authorization.isAuthorized( 276 if (!authorization.isAuthorized(
278 request, 277 request,
279 messung, 278 messung,
280 RequestMethod.POST, 279 RequestMethod.POST,
281 LMessung.class) 280 Messung.class)
282 ) { 281 ) {
283 return new Response(false, 699, null); 282 return new Response(false, 699, null);
284 } 283 }
285 284
286 Violation violation = validator.validate(messung); 285 Violation violation = validator.validate(messung);
291 return response; 290 return response;
292 } 291 }
293 292
294 /* Persist the new messung object*/ 293 /* Persist the new messung object*/
295 Response response = repository.create(messung, "land"); 294 Response response = repository.create(messung, "land");
296 LMessung ret = (LMessung)response.getData(); 295 Messung ret = (Messung)response.getData();
297 /* Create and persist a new probe translation object*/
298 MessungTranslation trans = new MessungTranslation();
299 trans.setMessungsId(ret);
300 repository.create(trans, "land");
301 /* Get and return the new probe object*/
302 Response created =
303 repository.getById(LMessung.class, ret.getId(), "land");
304 if(violation.hasWarnings()) { 296 if(violation.hasWarnings()) {
305 created.setWarnings(violation.getWarnings()); 297 response.setWarnings(violation.getWarnings());
306 } 298 }
307 299
308 LStatusProtokoll status = new LStatusProtokoll(); 300 StatusProtokoll status = new StatusProtokoll();
309 status.setDatum(new Timestamp(new Date().getTime())); 301 status.setDatum(new Timestamp(new Date().getTime()));
310 status.setMessungsId(((LMessung)created.getData()).getId()); 302 status.setMessungsId((ret.getId()));
311 LProbe probe = 303 Probe probe =
312 repository.getByIdPlain(LProbe.class, ret.getProbeId(), "land"); 304 repository.getByIdPlain(Probe.class, ret.getProbeId(), "land");
313 status.setErzeuger(probe.getMstId()); 305 status.setMstId(probe.getMstId());
314 status.setStatusStufe(1); 306 status.setStatusKombi(1);
315 status.setStatusWert(0);
316 repository.create(status, "land"); 307 repository.create(status, "land");
317 ret.setStatus(status.getId()); 308 ret.setStatus(status.getId());
318 repository.update(ret, "land"); 309 repository.update(ret, "land");
319 Response updated= 310 Response updated=
320 repository.getById(LMessung.class, ret.getId(), "land"); 311 repository.getById(Messung.class, ret.getId(), "land");
321 312
322 return authorization.filter( 313 return authorization.filter(
323 request, 314 request,
324 updated, 315 updated,
325 LMessung.class); 316 Messung.class);
326 } 317 }
327 318
328 /** 319 /**
329 * Update an existing Messung object. 320 * Update an existing Messung object.
330 * <p> 321 * <p>
355 @Path("/{id}") 346 @Path("/{id}")
356 @Produces(MediaType.APPLICATION_JSON) 347 @Produces(MediaType.APPLICATION_JSON)
357 public Response update( 348 public Response update(
358 @Context HttpHeaders headers, 349 @Context HttpHeaders headers,
359 @Context HttpServletRequest request, 350 @Context HttpServletRequest request,
360 LMessung messung 351 Messung messung
361 ) { 352 ) {
362 if (!authorization.isAuthorized( 353 if (!authorization.isAuthorized(
363 request, 354 request,
364 messung, 355 messung,
365 RequestMethod.PUT, 356 RequestMethod.PUT,
366 LMessung.class) 357 Messung.class)
367 ) { 358 ) {
368 return new Response(false, 699, null); 359 return new Response(false, 699, null);
369 } 360 }
370 if (lock.isLocked(messung)) { 361 if (lock.isLocked(messung)) {
371 return new Response(false, 697, null); 362 return new Response(false, 697, null);
380 Response response = repository.update(messung, "land"); 371 Response response = repository.update(messung, "land");
381 if (!response.getSuccess()) { 372 if (!response.getSuccess()) {
382 return response; 373 return response;
383 } 374 }
384 Response updated = repository.getById( 375 Response updated = repository.getById(
385 LMessung.class, 376 Messung.class,
386 ((LMessung)response.getData()).getId(), "land"); 377 ((Messung)response.getData()).getId(), "land");
387 if(violation.hasWarnings()) { 378 if(violation.hasWarnings()) {
388 updated.setWarnings(violation.getWarnings()); 379 updated.setWarnings(violation.getWarnings());
389 } 380 }
390 return authorization.filter( 381 return authorization.filter(
391 request, 382 request,
392 updated, 383 updated,
393 LMessung.class); 384 Messung.class);
394 } 385 }
395 386
396 /** 387 /**
397 * Delete an existing Messung object by id. 388 * Delete an existing Messung object by id.
398 * <p> 389 * <p>
410 @Context HttpServletRequest request, 401 @Context HttpServletRequest request,
411 @PathParam("id") String id 402 @PathParam("id") String id
412 ) { 403 ) {
413 /* Get the messung object by id*/ 404 /* Get the messung object by id*/
414 Response messung = 405 Response messung =
415 repository.getById(LMessung.class, Integer.valueOf(id), "land"); 406 repository.getById(Messung.class, Integer.valueOf(id), "land");
416 LMessung messungObj = (LMessung)messung.getData(); 407 Messung messungObj = (Messung)messung.getData();
417 if (!authorization.isAuthorized( 408 if (!authorization.isAuthorized(
418 request, 409 request,
419 messungObj, 410 messungObj,
420 RequestMethod.DELETE, 411 RequestMethod.DELETE,
421 LMessung.class) 412 Messung.class)
422 ) { 413 ) {
423 return new Response(false, 699, null); 414 return new Response(false, 699, null);
424 } 415 }
425 if (lock.isLocked(messung)) { 416 if (lock.isLocked(messung)) {
426 return new Response(false, 697, null); 417 return new Response(false, 697, null);
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)