Mercurial > lada > lada-server
diff src/main/java/de/intevation/lada/rest/stamm/DatensatzErzeugerService.java @ 836:313bd1d227f1
Added services for stammdaten and updated models.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 08 Jan 2016 12:10:09 +0100 |
parents | |
children | 670f58112734 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/intevation/lada/rest/stamm/DatensatzErzeugerService.java Fri Jan 08 12:10:09 2016 +0100 @@ -0,0 +1,176 @@ +/* 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 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.UriInfo; + +import de.intevation.lada.model.stamm.DatensatzErzeuger; +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.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 DatensatzErzeuger 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], + * "bezeichnung": [string], + * "daErzeugerId": [string], + * "letzteAenderung": [timestamp], + * "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/datensatzerzeuger") +@RequestScoped +public class DatensatzErzeugerService { + + /** + * The data repository granting read access. + */ + @Inject + @RepositoryConfig(type=RepositoryType.RW) + private Repository repository; + + @Inject + @AuthorizationConfig(type=AuthorizationType.HEADER) + private Authorization authorization; + + /** + * Get all Datenbasis objects. + * <p> + * Example: http://example.com/datenbasis + * + * @return Response object containing all Datenbasis objects. + */ + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Response get( + @Context HttpHeaders headers, + @Context UriInfo info + ) { + return repository.getAll(DatensatzErzeuger.class, "stamm"); + } + + /** + * Get a single Datenbasis object by id. + * <p> + * The id is appended to the URL as a path parameter. + * <p> + * Example: http://example.com/datenbasis/{id} + * + * @return Response object containing a single Datenabasis. + */ + @GET + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response getById( + @Context HttpHeaders headers, + @PathParam("id") String id + ) { + return repository.getById( + DatensatzErzeuger.class, + Integer.valueOf(id), + "stamm"); + } + + @POST + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Response create( + @Context HttpServletRequest request, + DatensatzErzeuger datensatzerzeuger + ) { + if (!authorization.isAuthorized( + request, + datensatzerzeuger, + RequestMethod.POST, + DatensatzErzeuger.class) + ) { + return new Response(false, 699, datensatzerzeuger); + } + + return repository.create(datensatzerzeuger, "stamm"); + } + + @PUT + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response update( + @Context HttpServletRequest request, + DatensatzErzeuger datensatzerzeuger + ) { + if (!authorization.isAuthorized( + request, + datensatzerzeuger, + RequestMethod.PUT, + DatensatzErzeuger.class) + ) { + return new Response(false, 699, datensatzerzeuger); + } + + return repository.update(datensatzerzeuger, "stamm"); + } + + @DELETE + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response delete( + @Context HttpServletRequest request, + @PathParam("id") String id + ) { + DatensatzErzeuger datensatzerzeuger = repository.getByIdPlain( + DatensatzErzeuger.class, Integer.valueOf(id), "stamm"); + if (datensatzerzeuger == null || + !authorization.isAuthorized( + request, + datensatzerzeuger, + RequestMethod.DELETE, + DatensatzErzeuger.class + ) + ) { + return new Response(false, 699, null); + } + return repository.delete(datensatzerzeuger, "stamm"); + } +}