raimund@836: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@836: * Software engineering by Intevation GmbH raimund@836: * raimund@836: * This file is Free Software under the GNU GPL (v>=3) raimund@836: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@836: * the documentation coming with IMIS-Labordaten-Application for details. raimund@836: */ raimund@836: package de.intevation.lada.rest.stamm; raimund@836: raimund@843: import java.util.List; raimund@843: raimund@836: import javax.enterprise.context.RequestScoped; raimund@836: import javax.inject.Inject; raimund@836: import javax.servlet.http.HttpServletRequest; raimund@836: import javax.ws.rs.DELETE; raimund@836: import javax.ws.rs.GET; raimund@836: import javax.ws.rs.POST; raimund@836: import javax.ws.rs.PUT; raimund@836: import javax.ws.rs.Path; raimund@836: import javax.ws.rs.PathParam; raimund@836: import javax.ws.rs.Produces; raimund@836: import javax.ws.rs.core.Context; raimund@836: import javax.ws.rs.core.HttpHeaders; raimund@836: import javax.ws.rs.core.MediaType; raimund@843: import javax.ws.rs.core.MultivaluedMap; raimund@836: import javax.ws.rs.core.UriInfo; raimund@836: raimund@836: import de.intevation.lada.model.stamm.DatensatzErzeuger; raimund@836: import de.intevation.lada.util.annotation.AuthorizationConfig; raimund@836: import de.intevation.lada.util.annotation.RepositoryConfig; raimund@836: import de.intevation.lada.util.auth.Authorization; raimund@836: import de.intevation.lada.util.auth.AuthorizationType; raimund@836: import de.intevation.lada.util.data.Repository; raimund@836: import de.intevation.lada.util.data.RepositoryType; raimund@836: import de.intevation.lada.util.rest.RequestMethod; raimund@836: import de.intevation.lada.util.rest.Response; raimund@836: raimund@836: /** raimund@836: * REST service for DatensatzErzeuger objects. raimund@836: *
raimund@836: * The services produce data in the application/json media type. raimund@836: * A typical response holds information about the action performed and the data. raimund@836: *
raimund@836: *
raimund@836: * {
raimund@836: * "success": [boolean];
raimund@836: * "message": [string],
raimund@836: * "data":[{
raimund@836: * "id": [number],
raimund@836: * "bezeichnung": [string],
raimund@836: * "daErzeugerId": [string],
raimund@836: * "letzteAenderung": [timestamp],
raimund@836: * "mstId": [string],
raimund@836: * "netzbetreiberId": [string]
raimund@836: * }],
raimund@836: * "errors": [object],
raimund@836: * "warnings": [object],
raimund@836: * "readonly": [boolean],
raimund@836: * "totalCount": [number]
raimund@836: * }
raimund@836: *
raimund@836: *
raimund@836: *
raimund@836: * @author Raimund Renkert
raimund@836: */
raimund@836: @Path("rest/datensatzerzeuger")
raimund@836: @RequestScoped
raimund@836: public class DatensatzErzeugerService {
raimund@836:
raimund@836: /**
raimund@836: * The data repository granting read access.
raimund@836: */
raimund@836: @Inject
raimund@836: @RepositoryConfig(type=RepositoryType.RW)
raimund@836: private Repository repository;
raimund@836:
raimund@836: @Inject
raimund@836: @AuthorizationConfig(type=AuthorizationType.HEADER)
raimund@836: private Authorization authorization;
raimund@836:
raimund@836: /**
raimund@836: * Get all Datenbasis objects.
raimund@836: *
raimund@836: * Example: http://example.com/datenbasis
raimund@836: *
raimund@836: * @return Response object containing all Datenbasis objects.
raimund@836: */
raimund@836: @GET
raimund@836: @Path("/")
raimund@836: @Produces(MediaType.APPLICATION_JSON)
raimund@836: public Response get(
raimund@843: @Context HttpServletRequest request,
raimund@836: @Context UriInfo info
raimund@836: ) {
raimund@843: MultivaluedMap
raimund@836: * The id is appended to the URL as a path parameter.
raimund@836: *
raimund@836: * Example: http://example.com/datenbasis/{id}
raimund@836: *
raimund@836: * @return Response object containing a single Datenabasis.
raimund@836: */
raimund@836: @GET
raimund@836: @Path("/{id}")
raimund@836: @Produces(MediaType.APPLICATION_JSON)
raimund@836: public Response getById(
raimund@836: @Context HttpHeaders headers,
raimund@836: @PathParam("id") String id
raimund@836: ) {
raimund@836: return repository.getById(
raimund@836: DatensatzErzeuger.class,
raimund@836: Integer.valueOf(id),
raimund@836: "stamm");
raimund@836: }
raimund@836:
raimund@836: @POST
raimund@836: @Path("/")
raimund@836: @Produces(MediaType.APPLICATION_JSON)
raimund@836: public Response create(
raimund@836: @Context HttpServletRequest request,
raimund@836: DatensatzErzeuger datensatzerzeuger
raimund@836: ) {
raimund@836: if (!authorization.isAuthorized(
raimund@836: request,
raimund@836: datensatzerzeuger,
raimund@836: RequestMethod.POST,
raimund@836: DatensatzErzeuger.class)
raimund@836: ) {
raimund@836: return new Response(false, 699, datensatzerzeuger);
raimund@836: }
raimund@836:
raimund@836: return repository.create(datensatzerzeuger, "stamm");
raimund@836: }
raimund@836:
raimund@836: @PUT
raimund@836: @Path("/{id}")
raimund@836: @Produces(MediaType.APPLICATION_JSON)
raimund@836: public Response update(
raimund@836: @Context HttpServletRequest request,
raimund@836: DatensatzErzeuger datensatzerzeuger
raimund@836: ) {
raimund@836: if (!authorization.isAuthorized(
raimund@836: request,
raimund@836: datensatzerzeuger,
raimund@836: RequestMethod.PUT,
raimund@836: DatensatzErzeuger.class)
raimund@836: ) {
raimund@836: return new Response(false, 699, datensatzerzeuger);
raimund@836: }
raimund@836:
raimund@836: return repository.update(datensatzerzeuger, "stamm");
raimund@836: }
raimund@836:
raimund@836: @DELETE
raimund@836: @Path("/{id}")
raimund@836: @Produces(MediaType.APPLICATION_JSON)
raimund@836: public Response delete(
raimund@836: @Context HttpServletRequest request,
raimund@836: @PathParam("id") String id
raimund@836: ) {
raimund@836: DatensatzErzeuger datensatzerzeuger = repository.getByIdPlain(
raimund@836: DatensatzErzeuger.class, Integer.valueOf(id), "stamm");
raimund@836: if (datensatzerzeuger == null ||
raimund@836: !authorization.isAuthorized(
raimund@836: request,
raimund@836: datensatzerzeuger,
raimund@836: RequestMethod.DELETE,
raimund@836: DatensatzErzeuger.class
raimund@836: )
raimund@836: ) {
raimund@836: return new Response(false, 699, null);
raimund@836: }
raimund@836: return repository.delete(datensatzerzeuger, "stamm");
raimund@836: }
raimund@836: }