Mercurial > lada > lada-server
view src/main/java/de/intevation/lada/rest/stamm/LocationService.java @ 630:44d7ff0461f2
Code documentation.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Thu, 23 Apr 2015 10:40:36 +0200 |
parents | b3eee6414310 |
children | e0d3459d5914 |
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 javax.enterprise.context.RequestScoped; import javax.inject.Inject; 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.stamm.SOrt; import de.intevation.lada.util.annotation.RepositoryConfig; 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.Response; /** * REST service for SOrt 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], * "beschreibung": [string], * "bezeichnung": [string], * "hoeheLand": [number], * "koordXExtern": [string], * "koordYExtern": [string], * "latitude": [number], * "letzteAenderung": [timestamp], * "longitude": [number], * "nutsCode": [string], * "unscharf": [string], * "koordinatenartId": [number], * "netzbetreiberId": [number], * "staatId": [number], * "verwaltungseinheitId": [string], * "otyp": [string] * }], * "errors": [object], * "warnings": [object], * "readonly": [boolean], * "totalCount": [number] * } * </code> * </pre> * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @Path("location") @RequestScoped public class LocationService { /** * The data repository granting read/write access. */ @Inject @RepositoryConfig(type=RepositoryType.RW) private Repository defaultRepo; /** * Get all SOrt objects. * <p> * The requested objects can be filtered using a URL parameter named * ortId. * <p> * Example: http://example.com/location?ortId=[ID] * * @return Response object containing all (filtered) SOrt objects. */ @GET @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response get( @Context HttpHeaders headers, @Context UriInfo info ) { MultivaluedMap<String, String> params = info.getQueryParameters(); if (params.isEmpty() || !params.containsKey("ortId")) { return defaultRepo.getAll(SOrt.class, "stamm"); } String probeId = params.getFirst("ortId"); QueryBuilder<SOrt> builder = new QueryBuilder<SOrt>( defaultRepo.entityManager("land"), SOrt.class); builder.and("probeId", probeId); return defaultRepo.filter(builder.getQuery(), "stamm"); } /** * Get a single SOrt object by id. * <p> * The id is appended to the URL as a path parameter. * <p> * Example: http://example.com/location/{id} * * @return Response object containing a single SOrt. */ @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response getById( @Context HttpHeaders headers, @PathParam("id") String id ) { return defaultRepo.getById( SOrt.class, Integer.valueOf(id), "stamm"); } /** * Create a SOrt object. * <p> * The new object is embedded in the post data as JSON formatted string. * <p> * <pre> * <code> * { * "bezeichnung": [string], * "beschreibung": [string], * "unscharf": [string], * "nutsCode": [string], * "koordXExtern": [string], * "koordYExtern": [string], * "hoeheLand": [string], * "longitude": [number], * "latitude": [number], * "staatId": [number], * "verwaltungseinheitId": [string], * "otyp": [string], * "letzteAenderung": [date] * } * </code> * </pre> * @return A response object containing the created SOrt. */ @POST @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response create( @Context HttpHeaders headers, SOrt ort ) { /* Persist the new object*/ return defaultRepo.create(ort, "stamm"); } /** * Update an existing SOrt object. * <p> * The object to update should come as JSON formatted string. * <pre> * <code> * { * "id": [number], * "bezeichnung": [string], * "beschreibung": [string], * "unscharf": [string], * "nutsCode": [string], * "koordXExtern": [string], * "koordYExtern": [string], * "hoeheLand": [number], * "longitude": [number], * "latitude": [number], * "staatId": [number], * "verwaltungseinheitId": [string], * "otyp": [string], * "letzteAenderung": [date] * } * </code> * </pre> * * @return Response object containing the updated SOrt object. */ @PUT @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response update(@Context HttpHeaders headers, SOrt ort) { Response response = defaultRepo.update(ort, "stamm"); Response updated = defaultRepo.getById( SOrt.class, ((SOrt)response.getData()).getId(), "stamm"); return updated; } /** * Delete an existing SOrt object by id. * <p> * The id is appended to the URL as a path parameter. * <p> * Example: http://example.com/location/{id} * * @return Response object. */ @DELETE @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Response delete( @Context HttpHeaders headers, @PathParam("id") String id ) { /* Get the object by id*/ Response object = defaultRepo.getById(SOrt.class, Integer.valueOf(id), "stamm"); SOrt ortObj = (SOrt)object.getData(); /* Delete the object*/ return defaultRepo.delete(ortObj, "stamm"); } }