[Lada-commits] [PATCH 7 of 7] Added json export

Wald Commits scm-commit at wald.intevation.org
Mon Sep 5 10:20:08 CEST 2016


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1473063563 -7200
# Branch schema-update
# Node ID 1b9516c60a5ab1444fc78975545c50d5158b3061
# Parent  3a5a9a0492d10ae67f33600fa260ff71989df0c9
Added json export.

diff -r 3a5a9a0492d1 -r 1b9516c60a5a pom.xml
--- a/pom.xml	Mon Sep 05 10:18:38 2016 +0200
+++ b/pom.xml	Mon Sep 05 10:19:23 2016 +0200
@@ -70,6 +70,16 @@
             <version>2.5.1</version>
         </dependency>
         <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.5.1</version>
+        </dependency>
+        <dependency>
+    		<groupId>commons-io</groupId>
+    		<artifactId>commons-io</artifactId>
+    		<version>2.5</version>
+		</dependency>
+        <dependency>
             <groupId>org.opensaml</groupId>
             <artifactId>opensaml</artifactId>
             <version>2.6.4</version>
diff -r 3a5a9a0492d1 -r 1b9516c60a5a src/main/java/de/intevation/lada/exporter/ExportFormat.java
--- a/src/main/java/de/intevation/lada/exporter/ExportFormat.java	Mon Sep 05 10:18:38 2016 +0200
+++ b/src/main/java/de/intevation/lada/exporter/ExportFormat.java	Mon Sep 05 10:19:23 2016 +0200
@@ -13,5 +13,6 @@
  * @author <a href="mailto:rrenkert at intevation.de">Raimund Renkert</a>
  */
 public enum ExportFormat {
-    LAF
+    LAF,
+    JSON
 }
diff -r 3a5a9a0492d1 -r 1b9516c60a5a src/main/java/de/intevation/lada/exporter/json/JsonExporter.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/exporter/json/JsonExporter.java	Mon Sep 05 10:19:23 2016 +0200
@@ -0,0 +1,328 @@
+/* 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.exporter.json;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import de.intevation.lada.exporter.ExportConfig;
+import de.intevation.lada.exporter.ExportFormat;
+import de.intevation.lada.exporter.Exporter;
+import de.intevation.lada.model.land.KommentarM;
+import de.intevation.lada.model.land.KommentarP;
+import de.intevation.lada.model.land.Messung;
+import de.intevation.lada.model.land.Messwert;
+import de.intevation.lada.model.land.Ortszuordnung;
+import de.intevation.lada.model.land.Probe;
+import de.intevation.lada.model.land.StatusProtokoll;
+import de.intevation.lada.model.land.ZusatzWert;
+import de.intevation.lada.model.stammdaten.Deskriptoren;
+import de.intevation.lada.model.stammdaten.MessStelle;
+import de.intevation.lada.model.stammdaten.Ort;
+import de.intevation.lada.model.stammdaten.StatusKombi;
+import de.intevation.lada.util.annotation.RepositoryConfig;
+import de.intevation.lada.util.auth.UserInfo;
+import de.intevation.lada.util.data.QueryBuilder;
+import de.intevation.lada.util.data.Repository;
+import de.intevation.lada.util.data.RepositoryType;
+
+ at ExportConfig(format=ExportFormat.JSON)
+public class JsonExporter implements Exporter {
+
+    @Inject private Logger logger;
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    private Repository repository;
+
+    @Override
+    public InputStream export(List<Integer> proben, UserInfo userInfo) {
+        logger.debug("export json");
+        String json = "";
+
+        //Create json.
+        json = createJsonString(proben, userInfo);
+        if (json == null) {
+            return null;
+        }
+
+        InputStream in = new ByteArrayInputStream(json.getBytes());
+        try {
+            in.close();
+        }
+        catch (IOException e) {
+            logger.debug("Error while closing Stream.");
+            return null;
+        }
+        return in;
+    }
+
+    private String createJsonString(List<Integer> probeIds, UserInfo userInfo) {
+        QueryBuilder<Probe> builder = new QueryBuilder<Probe>(
+                repository.entityManager("land"),
+                Probe.class
+            );
+        for (Integer id : probeIds) {
+            builder.or("id", id);
+        }
+        List<Probe> proben = repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(proben);
+            JsonNode nodes = mapper.readTree(tmp);
+            addSubObjects(nodes);
+            return mapper.writeValueAsString(nodes);
+        } catch (IOException e) {
+            logger.debug("Error parsing object structure.");
+            return null;
+        }
+    }
+
+    private JsonNode addSubObjects(JsonNode proben) {
+        for (int i = 0; i < proben.size(); i++) {
+            addMessungen(proben.get(i));
+            addKommentare(proben.get(i));
+            addZusatzwerte(proben.get(i));
+            addDeskriptoren(proben.get(i));
+            addOrtszuordung(proben.get(i));
+            addMessstelle(proben.get(i));
+        }
+        return proben;
+    }
+
+    private void addMessstelle(JsonNode node) {
+        MessStelle messstelle = repository.getByIdPlain(
+            MessStelle.class,
+            node.get("mstId").asText(),
+            "stamm");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(messstelle);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)node).set("messstelle", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Messstelle for Probe "
+                + node.get("idAlt").asText());
+        }
+    }
+
+    private void addMessungen(JsonNode probe) {
+        QueryBuilder<Messung> builder = new QueryBuilder<Messung>(
+                repository.entityManager("land"),
+                Messung.class
+            );
+        builder.and("probeId", probe.get("id").asInt());
+        List<Messung> messungen = repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(messungen);
+            JsonNode nodes = mapper.readTree(tmp);
+            for (int i = 0; i < nodes.size(); i++) {
+                addMesswerte(nodes.get(i));
+                addMessungsKommentare(nodes.get(i));
+                addStatusProtokoll(nodes.get(i));
+            }
+            ((ObjectNode)probe).set("messungen", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Messungen for Probe "
+                + probe.get("idAlt").asText());
+        }
+    }
+
+    private void addKommentare(JsonNode probe) {
+        QueryBuilder<KommentarP> builder = new QueryBuilder<KommentarP>(
+                repository.entityManager("land"),
+                KommentarP.class
+            );
+        builder.and("probeId", probe.get("id").asInt());
+        List<KommentarP> kommentare =
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(kommentare);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)probe).set("kommentare", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Kommentare for Probe "
+                + probe.get("idAlt").asText());
+        }
+    }
+
+    private void addZusatzwerte(JsonNode probe) {
+        QueryBuilder<ZusatzWert> builder = new QueryBuilder<ZusatzWert>(
+                repository.entityManager("land"),
+                ZusatzWert.class
+            );
+        builder.and("probeId", probe.get("id").asInt());
+        List<ZusatzWert> zusatzwerte=
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(zusatzwerte);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)probe).set("zusatzwerte", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Zusatzwerte for Probe "
+                + probe.get("idAlt").asText());
+        }
+    }
+
+    private void addDeskriptoren(JsonNode probe) {
+        String desk = probe.get("mediaDesk").asText();
+        String[] parts = desk.split(" ");
+
+        QueryBuilder<Deskriptoren> builder = new QueryBuilder<Deskriptoren>(
+                repository.entityManager("stamm"),
+                Deskriptoren.class
+            );
+        int vorgaenger = 0;
+        ObjectNode node = new ObjectNode(JsonNodeFactory.instance);
+        for (int i = 0; i < parts.length - 1; i++) {
+            logger.debug("ebene: "  + i);
+            logger.debug("sn: " + Integer.parseInt(parts[i+1]));
+            String beschreibung = "";
+            if (Integer.parseInt(parts[i+1]) != 0) {
+                builder.and("ebene", i);
+                builder.and("sn", Integer.parseInt(parts[i+1]));
+                logger.debug(vorgaenger);
+                if (vorgaenger != 0) {
+                    builder.and("vorgaenger", vorgaenger);
+                }
+                List<Deskriptoren> found = repository.filterPlain(builder.getQuery(), "stamm");
+                if (found.size() > 0) {
+                    beschreibung = found.get(0).getBeschreibung();
+                    vorgaenger = found.get(0).getId();
+                }
+            }
+            node.put("S" + i, beschreibung);
+            builder = builder.getEmptyBuilder();
+        }
+        ((ObjectNode)probe).set("deskriptoren", node);
+    }
+
+    private void addMesswerte(JsonNode node) {
+        QueryBuilder<Messwert> builder = new QueryBuilder<Messwert>(
+                repository.entityManager("land"),
+                Messwert.class
+            );
+        builder.and("messungsId", node.get("id").asInt());
+        List<Messwert> messwerte =
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(messwerte);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)node).set("messwerte", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Messwerte for Messung "
+                + node.get("nebenprobenNr").asText());
+        }
+    }
+
+    private void addMessungsKommentare(JsonNode node) {
+        QueryBuilder<KommentarM> builder = new QueryBuilder<KommentarM>(
+                repository.entityManager("land"),
+                KommentarM.class
+            );
+        builder.and("messungsId", node.get("id").asInt());
+        List<KommentarM> kommentare =
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(kommentare);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)node).set("kommentare", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Kommentare for Messung "
+                + node.get("nebenprobenNr").asText());
+        }
+    }
+
+    private void addStatusProtokoll(JsonNode node) {
+        QueryBuilder<StatusProtokoll> builder = new QueryBuilder<StatusProtokoll>(
+                repository.entityManager("land"),
+                StatusProtokoll.class
+            );
+        builder.and("messungsId", node.get("id").asInt());
+        List<StatusProtokoll> status =
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(status);
+            JsonNode nodes = mapper.readTree(tmp);
+            for (int i = 0; i < nodes.size(); i++) {
+                StatusKombi kombi = repository.getByIdPlain(
+                    StatusKombi.class,
+                    nodes.get(i).get("statusKombi").asInt(),
+                    "stamm");
+                ((ObjectNode)nodes.get(i)).put(
+                    "statusStufe",
+                    kombi.getStatusStufe().getStufe());
+                ((ObjectNode)nodes.get(i)).put(
+                    "statusWert",
+                    kombi.getStatusWert().getWert());
+            }
+            ((ObjectNode)node).set("statusprotokoll", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Statusprotokoll for Messung "
+                + node.get("nebenprobenNr").asText());
+        }
+    }
+
+    private void addOrtszuordung(JsonNode node) {
+        QueryBuilder<Ortszuordnung> builder = new QueryBuilder<Ortszuordnung>(
+                repository.entityManager("land"),
+                Ortszuordnung.class
+            );
+        builder.and("probeId", node.get("id").asInt());
+        List<Ortszuordnung> ortszuordnung =
+            repository.filterPlain(builder.getQuery(), "land");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(ortszuordnung);
+            JsonNode nodes = mapper.readTree(tmp);
+            for (int i = 0; i < nodes.size(); i++) {
+                addOrt(nodes.get(i));
+            }
+            ((ObjectNode)node).set("ortszuordnung", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Ortszuordnugen for Probe "
+                + node.get("idAlt").asText());
+        }
+    }
+
+    private void addOrt(JsonNode node) {
+        QueryBuilder<Ort> builder = new QueryBuilder<Ort>(
+                repository.entityManager("stamm"),
+                Ort.class
+            );
+        builder.and("id", node.get("ortId").asInt());
+        List<Ort> ort=
+            repository.filterPlain(builder.getQuery(), "stamm");
+        final ObjectMapper mapper = new ObjectMapper();
+        try {
+            String tmp = mapper.writeValueAsString(ort);
+            JsonNode nodes = mapper.readTree(tmp);
+            ((ObjectNode)node).set("ort", nodes);
+        } catch (IOException e) {
+            logger.debug("Could not export Ort for Ortszuordnung "
+                + node.get("id").asText());
+        }
+    }
+}
diff -r 3a5a9a0492d1 -r 1b9516c60a5a src/main/java/de/intevation/lada/rest/exporter/JsonExportService.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/rest/exporter/JsonExportService.java	Mon Sep 05 10:19:23 2016 +0200
@@ -0,0 +1,107 @@
+/* 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.IOException;
+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.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 org.apache.commons.io.IOUtils;
+
+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.auth.Authorization;
+import de.intevation.lada.util.auth.AuthorizationType;
+import de.intevation.lada.util.auth.UserInfo;
+import de.intevation.lada.util.rest.Response;
+
+/**
+ * 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 at intevation.de">Raimund Renkert</a>
+ */
+ at Path("data/export")
+ at RequestScoped
+public class JsonExportService {
+
+    /**
+     * The exporter.
+     */
+    @Inject
+    @ExportConfig(format=ExportFormat.JSON)
+    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("/json")
+    @Consumes("application/json")
+    @Produces("application/json")
+    public String download(
+        JsonObject proben,
+        @Context HttpServletRequest request
+    ) {
+        JsonArray array = proben.getJsonArray("proben");
+        List<Integer> probeIds = new ArrayList<Integer>();
+        UserInfo userInfo = authorization.getInfo(request);
+        for (int i = 0; i < array.size(); i++) {
+            Integer probeId = array.getInt(i);
+            probeIds.add(probeId);
+        }
+        InputStream exported = exporter.export(probeIds, userInfo);
+        if (exported == null) {
+            return new Response(false, 600, null).toString();
+        }
+        try {
+            return IOUtils.toString(exported, "utf-8");
+        } catch (IOException e) {
+            return new Response(false, 600, null).toString();
+        }
+    }
+}
+
diff -r 3a5a9a0492d1 -r 1b9516c60a5a src/main/java/de/intevation/lada/rest/exporter/LafExportService.java
--- a/src/main/java/de/intevation/lada/rest/exporter/LafExportService.java	Mon Sep 05 10:18:38 2016 +0200
+++ b/src/main/java/de/intevation/lada/rest/exporter/LafExportService.java	Mon Sep 05 10:19:23 2016 +0200
@@ -47,7 +47,7 @@
  *
  * @author <a href="mailto:rrenkert at intevation.de">Raimund Renkert</a>
  */
- at Path("data/laf/export")
+ at Path("data/export")
 @RequestScoped
 public class LafExportService {
 


More information about the Lada-commits mailing list