Mercurial > lada > lada-server
diff src/main/java/de/intevation/lada/rest/stamm/MessprogrammKategorieService.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/MessprogrammKategorieService.java Fri Jan 08 12:10:09 2016 +0100 @@ -0,0 +1,177 @@ +/* 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.model.stamm.MessprogrammKategorie; +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/messprogrammkategorie") +@RequestScoped +public class MessprogrammKategorieService { + + /** + * 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/messprogrammkategorie + * + * @return Response object containing all objects. + */ + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Response get( + @Context HttpHeaders headers, + @Context UriInfo info + ) { + return repository.getAll(MessprogrammKategorie.class, "stamm"); + } + + /** + * Get a single object by id. + * <p> + * The id is appended to the URL as a path parameter. + * <p> + * Example: http://example.com/messprogrammkategorie/{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( + MessprogrammKategorie.class, + Integer.valueOf(id), + "stamm"); + } + + @POST + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Response create( + @Context HttpServletRequest request, + MessprogrammKategorie kategorie + ) { + if (!authorization.isAuthorized( + request, + kategorie, + RequestMethod.POST, + MessprogrammKategorie.class) + ) { + return new Response(false, 699, kategorie); + } + + return repository.create(kategorie, "stamm"); + } + + @PUT + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response update( + @Context HttpServletRequest request, + MessprogrammKategorie kategorie + ) { + if (!authorization.isAuthorized( + request, + kategorie, + RequestMethod.PUT, + MessprogrammKategorie.class) + ) { + return new Response(false, 699, kategorie); + } + + return repository.update(kategorie, "stamm"); + } + + @DELETE + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response delete( + @Context HttpServletRequest request, + @PathParam("id") String id + ) { + MessprogrammKategorie kategorie = repository.getByIdPlain( + MessprogrammKategorie.class, Integer.valueOf(id), "stamm"); + if (kategorie == null || + !authorization.isAuthorized( + request, + kategorie, + RequestMethod.DELETE, + MessprogrammKategorie.class + ) + ) { + return new Response(false, 699, null); + } + return repository.delete(kategorie, "stamm"); + } +}