Mercurial > lada > lada-server
view src/main/java/de/intevation/lada/rest/stamm/ProbenehmerService.java @ 1035:4d95cc7f0a43 schema-update
Updated status workflow.
* Using status_kombi instead of status_wert and status_stufe
* new service for status_kombi
* updated validators
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Wed, 31 Aug 2016 15:52:15 +0200 |
parents | 1c41c7b8f7c2 |
children | 3a5a9a0492d1 |
line wrap: on
line source
/* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz * Software engineering by Intevation GmbH * * This file is Free Software under the GNU GPL (v>=3) * and comes with ABSOLUTELY NO WARRANTY! Check out * the documentation coming with IMIS-Labordaten-Application for details. */ package de.intevation.lada.rest.stamm; import java.util.List; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; import de.intevation.lada.model.stammdaten.Filter; import de.intevation.lada.model.stammdaten.Probenehmer; import de.intevation.lada.util.annotation.AuthorizationConfig; import de.intevation.lada.util.annotation.RepositoryConfig; import de.intevation.lada.util.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; import de.intevation.lada.util.data.QueryBuilder; import de.intevation.lada.util.data.Repository; import de.intevation.lada.util.data.RepositoryType; import de.intevation.lada.util.rest.RequestMethod; import de.intevation.lada.util.rest.Response; /** * REST service for Probenehmer objects. * <p> * The services produce data in the application/json media type. * A typical response holds information about the action performed and the data. * <pre> * <code> * { * "success": [boolean]; * "message": [string], * "data":[{ * "id": [number], * "mstId": [string], * "netzbetreiberId": [string] * }], * "errors": [object], * "warnings": [object], * "readonly": [boolean], * "totalCount": [number] * } * </code> * </pre> * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @Path("rest/probenehmer") @RequestScoped public class ProbenehmerService { /** * The data repository granting read access. */ @Inject @RepositoryConfig(type=RepositoryType.RW) private Repository repository; @Inject @AuthorizationConfig(type=AuthorizationType.HEADER) private Authorization authorization; /** * Get all Probenehmer objects. * <p> * Example: http://example.com/probenehmer * * @return Response object containing all objects. */ @GET @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response get( @Context HttpServletRequest request, @Context UriInfo info ) { MultivaluedMap<String, String> params = info.getQueryParameters(); List<Probenehmer> nehmer = repository.getAllPlain(Probenehmer.class, "stamm"); if (params.containsKey("qid")) { Integer id = null; try { id = Integer.valueOf(params.getFirst("qid")); } catch (NumberFormatException e) { return new Response(false, 603, "Not a valid filter id"); } QueryBuilder<Filter> fBuilder = new QueryBuilder<Filter>( repository.entityManager("stamm"), Filter.class ); fBuilder.and("query", id); List<Filter> filters = repository.filterPlain(fBuilder.getQuery(), "stamm"); QueryBuilder<Probenehmer> builder = new QueryBuilder<Probenehmer>( repository.entityManager("stamm"), Probenehmer.class ); for (Filter filter: filters) { String param = params.get(filter.getDataIndex()).get(0); if (param == null || param.isEmpty()) { continue; } if (filter.getMultiselect()) { param = param.trim(); String[] parts = param.split(","); for (String part: parts) { builder.or(filter.getDataIndex(), part); } } else { builder.or(filter.getDataIndex(), param); } } nehmer = repository.filterPlain(builder.getQuery(), "stamm"); } int size = nehmer.size(); if (params.containsKey("start") && params.containsKey("limit")) { int start = Integer.valueOf(params.getFirst("start")); int limit = Integer.valueOf(params.getFirst("limit")); int end = limit + start; if (start + limit > nehmer.size()) { end = nehmer.size(); } nehmer = nehmer.subList(start, end); } for (Probenehmer p : nehmer) { p.setReadonly( !authorization.isAuthorized( request, p, RequestMethod.POST, Probenehmer.class)); } return new Response(true, 200, nehmer, size); } /** * Get a single Datenbasis object by id. * <p> * The id is appended to the URL as a path parameter. * <p> * Example: http://example.com/probenehmer/{id} * * @return Response object containing a single object. */ @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response getById( @Context HttpHeaders headers, @PathParam("id") String id ) { return repository.getById( Probenehmer.class, Integer.valueOf(id), "stamm"); } @POST @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response create( @Context HttpServletRequest request, Probenehmer probenehmer ) { if (!authorization.isAuthorized( request, probenehmer, RequestMethod.POST, Probenehmer.class) ) { return new Response(false, 699, probenehmer); } QueryBuilder<Probenehmer> builder = new QueryBuilder<Probenehmer>( repository.entityManager("stamm"), Probenehmer.class ); builder.and("prnId", probenehmer.getPrnId()); builder.and("netzbetreiberId", probenehmer.getNetzbetreiberId()); List<Probenehmer> nehmer= repository.filterPlain(builder.getQuery(), "stamm"); if (nehmer.isEmpty()) { return repository.create(probenehmer, "stamm"); } return new Response(false, 672, null); } @PUT @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response update( @Context HttpServletRequest request, Probenehmer probenehmer ) { if (!authorization.isAuthorized( request, probenehmer, RequestMethod.PUT, Probenehmer.class) ) { return new Response(false, 699, probenehmer); } return repository.update(probenehmer, "stamm"); } @DELETE @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response delete( @Context HttpServletRequest request, @PathParam("id") String id ) { Probenehmer probenehmer = repository.getByIdPlain( Probenehmer.class, Integer.valueOf(id), "stamm"); if (probenehmer == null || !authorization.isAuthorized( request, probenehmer, RequestMethod.DELETE, Probenehmer.class ) ) { return new Response(false, 699, null); } return repository.delete(probenehmer, "stamm"); } }