Mercurial > lada > lada-server
view src/main/java/de/intevation/lada/rest/exporter/LafExportService.java @ 1345:883ab3a6f525 tip
changed version to 2.7-SNAPSHOT after release2.6.2 for default branch
author | Marco Lechner, Bundesamt fuer Strahlenschutz, SW 2.1 <mlechner@bfs.de> |
---|---|
date | Fri, 07 Apr 2017 11:14:37 +0200 |
parents | f252a92aae1c |
children |
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.exporter; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.json.JsonArray; import javax.json.JsonValue; import javax.json.JsonNumber; import javax.json.JsonObject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import de.intevation.lada.model.land.Probe; import de.intevation.lada.exporter.ExportConfig; import de.intevation.lada.exporter.ExportFormat; import de.intevation.lada.exporter.Exporter; import de.intevation.lada.util.annotation.AuthorizationConfig; 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.auth.Authorization; import de.intevation.lada.util.auth.AuthorizationType; import de.intevation.lada.util.auth.UserInfo; /** * REST service to export probe objects and the child objects associated with * the selected Probe objects. * <p> * To request objects post a JSON formatted string with an array of probe ids. * <pre> * <code> * { * "proben": [[number], [number], ...] * } * </code> * </pre> * * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> */ @Path("data/export") @RequestScoped public class LafExportService { /** * The data repository granting read-only access. */ @Inject @RepositoryConfig(type=RepositoryType.RO) private Repository repository; /** * The exporter. */ @Inject @ExportConfig(format=ExportFormat.LAF) private Exporter exporter; /** * The authorization module. */ @Inject @AuthorizationConfig(type=AuthorizationType.HEADER) private Authorization authorization; /** * Export Probe objects. * * The service takes JSON formatted POST data containing probe ids and * exports the Probe objects filtered by these ids. * * @param proben JSON formatted string with an array of probe ids. * @param header The HTTP header containing authorization information. * @return The LAF file to export. */ @POST @Path("/laf") @Consumes("application/json") @Produces("text/plain") public Response download( JsonObject proben, @Context HttpServletRequest request ) { List<Integer> providedIds = new ArrayList<Integer>(); for (JsonValue id : proben.getJsonArray("proben")) { if (id instanceof JsonNumber) { providedIds.add(((JsonNumber)id).intValue()); } } QueryBuilder<Probe> pBuilder = new QueryBuilder<Probe>( repository.entityManager("land"), Probe.class); pBuilder.andIn("id", providedIds); List<Probe> pObjects = repository.filterPlain( pBuilder.getQuery(), "land"); if (pObjects.isEmpty()) { return Response.status(Response.Status.NOT_FOUND).build(); } List<Integer> probeIds = new ArrayList<Integer>(); for (Probe p : pObjects) { probeIds.add(p.getId()); } UserInfo userInfo = authorization.getInfo(request); InputStream exported = exporter.export(probeIds, userInfo); ResponseBuilder response = Response.ok((Object)exported); response.header( "Content-Disposition", "attachment; filename=\"export.laf\""); return response.build(); } }