raimund@488: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@488: * Software engineering by Intevation GmbH raimund@488: * raimund@488: * This file is Free Software under the GNU GPL (v>=3) raimund@488: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@488: * the documentation coming with IMIS-Labordaten-Application for details. raimund@488: */ raimund@488: package de.intevation.lada.rest; raimund@488: raimund@488: import javax.enterprise.context.RequestScoped; raimund@488: import javax.inject.Inject; raimund@582: import javax.servlet.http.HttpServletRequest; raimund@488: import javax.ws.rs.DELETE; raimund@488: import javax.ws.rs.GET; raimund@488: import javax.ws.rs.POST; raimund@488: import javax.ws.rs.PUT; raimund@488: import javax.ws.rs.Path; raimund@488: import javax.ws.rs.PathParam; raimund@488: import javax.ws.rs.Produces; raimund@488: import javax.ws.rs.core.Context; raimund@488: import javax.ws.rs.core.HttpHeaders; raimund@488: import javax.ws.rs.core.MediaType; raimund@488: import javax.ws.rs.core.MultivaluedMap; raimund@488: import javax.ws.rs.core.UriInfo; raimund@488: raimund@488: import de.intevation.lada.model.land.LStatus; raimund@488: import de.intevation.lada.util.annotation.AuthorizationConfig; raimund@488: import de.intevation.lada.util.annotation.RepositoryConfig; raimund@488: import de.intevation.lada.util.auth.Authorization; raimund@488: import de.intevation.lada.util.auth.AuthorizationType; raimund@488: import de.intevation.lada.util.data.QueryBuilder; raimund@488: import de.intevation.lada.util.data.Repository; raimund@488: import de.intevation.lada.util.data.RepositoryType; raimund@582: import de.intevation.lada.util.rest.RequestMethod; raimund@488: import de.intevation.lada.util.rest.Response; raimund@488: raimund@488: @Path("status") raimund@488: @RequestScoped raimund@488: public class StatusService { raimund@488: raimund@488: /* The data repository granting read/write access.*/ raimund@488: @Inject raimund@488: @RepositoryConfig(type=RepositoryType.RW) raimund@488: private Repository defaultRepo; raimund@488: raimund@488: /* The authorization module.*/ raimund@488: @Inject raimund@582: @AuthorizationConfig(type=AuthorizationType.OPEN_ID) raimund@488: private Authorization authorization; raimund@488: raimund@488: /** raimund@488: * Get all objects. raimund@488: * raimund@488: * @return Response object containing all messung objects. raimund@488: */ raimund@488: @GET raimund@488: @Path("/") raimund@488: @Produces(MediaType.APPLICATION_JSON) raimund@488: public Response get( raimund@488: @Context HttpHeaders headers, raimund@582: @Context UriInfo info, raimund@582: @Context HttpServletRequest request raimund@488: ) { raimund@488: MultivaluedMap params = info.getQueryParameters(); raimund@556: if (params.isEmpty() || !params.containsKey("messungsId")) { raimund@488: return defaultRepo.getAll(LStatus.class, "land"); raimund@488: } raimund@556: String messungId = params.getFirst("messungsId"); raimund@488: QueryBuilder builder = raimund@488: new QueryBuilder( raimund@488: defaultRepo.entityManager("land"), raimund@488: LStatus.class); raimund@488: builder.and("messungsId", messungId); raimund@582: return authorization.filter( raimund@582: request, raimund@582: defaultRepo.filter(builder.getQuery(), "land"), raimund@582: LStatus.class); raimund@488: } raimund@488: raimund@488: /** raimund@488: * Get an object by id. raimund@488: * raimund@488: * @return Response object containing a single messung. raimund@488: */ raimund@488: @GET raimund@488: @Path("/{id}") raimund@488: @Produces(MediaType.APPLICATION_JSON) raimund@488: public Response getById( raimund@488: @Context HttpHeaders headers, raimund@582: @Context HttpServletRequest request, raimund@488: @PathParam("id") String id raimund@488: ) { raimund@582: return authorization.filter( raimund@582: request, raimund@582: defaultRepo.getById(LStatus.class, Integer.valueOf(id), "land"), raimund@582: LStatus.class); raimund@488: } raimund@488: raimund@488: @POST raimund@488: @Path("/") raimund@488: @Produces(MediaType.APPLICATION_JSON) raimund@488: public Response create( raimund@488: @Context HttpHeaders headers, raimund@582: @Context HttpServletRequest request, raimund@488: LStatus status raimund@488: ) { raimund@582: if (!authorization.isAuthorized( raimund@582: request, raimund@582: status, raimund@582: RequestMethod.POST, raimund@582: LStatus.class) raimund@582: ) { raimund@488: return new Response(false, 699, null); raimund@488: } raimund@488: /* Persist the new object*/ raimund@488: return defaultRepo.create(status, "land"); raimund@488: } raimund@488: raimund@488: /** raimund@488: * Update an existing object. raimund@488: * raimund@488: * @return Response object containing the updated probe object. raimund@488: */ raimund@488: @PUT raimund@557: @Path("/{id}") raimund@488: @Produces(MediaType.APPLICATION_JSON) raimund@582: public Response update( raimund@582: @Context HttpHeaders headers, raimund@582: @Context HttpServletRequest request, raimund@582: LStatus status raimund@582: ) { raimund@582: if (!authorization.isAuthorized( raimund@582: request, raimund@582: status, raimund@582: RequestMethod.PUT, raimund@582: LStatus.class) raimund@582: ) { raimund@488: return new Response(false, 699, null); raimund@488: } raimund@488: Response response = defaultRepo.update(status, "land"); raimund@488: Response updated = defaultRepo.getById( raimund@488: LStatus.class, raimund@488: ((LStatus)response.getData()).getId(), "land"); raimund@488: return updated; raimund@488: } raimund@488: raimund@488: /** raimund@488: * Delete an existing object by id. raimund@488: * raimund@488: * @return Response object. raimund@488: */ raimund@488: @DELETE raimund@488: @Path("/{id}") raimund@488: @Produces(MediaType.APPLICATION_JSON) raimund@488: public Response delete( raimund@488: @Context HttpHeaders headers, raimund@582: @Context HttpServletRequest request, raimund@488: @PathParam("id") String id raimund@488: ) { raimund@488: /* Get the object by id*/ raimund@488: Response object = raimund@488: defaultRepo.getById(LStatus.class, Integer.valueOf(id), "land"); raimund@488: LStatus obj = (LStatus)object.getData(); raimund@582: if (!authorization.isAuthorized( raimund@582: request, raimund@582: obj, raimund@582: RequestMethod.DELETE, raimund@582: LStatus.class) raimund@582: ) { raimund@582: return new Response(false, 699, null); raimund@582: } raimund@488: /* Delete the object*/ raimund@488: return defaultRepo.delete(obj, "land"); raimund@488: } raimund@488: }