[PATCH 02 of 15] Added model, parser and importer for porosities

Wald Commits scm-commit at wald.intevation.org
Wed Apr 30 15:30:05 CEST 2014


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1398859889 -7200
# Node ID 02711de579ccabd25b7fb7bccc37796d01b3ab1a
# Parent  3f6b9fae16371bf6cb416d92cb051809f9721eb9
Added model, parser and importer for porosities.

diff -r 3f6b9fae1637 -r 02711de579cc backend/src/main/java/org/dive4elements/river/importer/ImportPorosity.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportPorosity.java	Wed Apr 30 14:11:29 2014 +0200
@@ -0,0 +1,112 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.importer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.Porosity;
+import org.dive4elements.river.model.TimeInterval;
+
+
+public class ImportPorosity {
+
+    private static Logger log = Logger.getLogger(ImportPorosity.class);
+
+    protected Porosity peer;
+
+    protected ImportDepth depth;
+
+    protected String description;
+
+    protected ImportTimeInterval timeInterval;
+
+    protected List<ImportPorosityValue> values;
+
+    public ImportPorosity(String description) {
+        this.description = description;
+        this.values = new ArrayList<ImportPorosityValue>();
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDepth(ImportDepth depth) {
+        this.depth = depth;
+    }
+
+    public void setTimeInterval(ImportTimeInterval importTimeInterval) {
+        this.timeInterval = importTimeInterval;
+    }
+
+    public void addValue(ImportPorosityValue value) {
+        values.add(value);
+    }
+
+    public void storeDependencies(River river) {
+        log.info("store dependencies");
+
+        if (depth != null) {
+            depth.storeDependencies();
+        }
+
+        Porosity peer = getPeer(river);
+
+        if (peer != null) {
+            log.info("store porosity values.");
+            for (ImportPorosityValue value : values) {
+                value.storeDependencies(peer);
+            }
+        }
+    }
+
+    public Porosity getPeer(River river) {
+        log.info("get peer");
+
+        if (depth == null) {
+            log.warn("cannot store porosity '" + description
+                + "': no depth");
+            return null;
+        }
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance()
+                .getDatabaseSession();
+
+            Query query = session.createQuery("from Porosity where "
+                + "   river=:river and " + "   depth=:depth");
+
+            query.setParameter("river", river);
+            query.setParameter("depth", depth.getPeer());
+
+            List<Porosity> porosity = query.list();
+
+            if (porosity.isEmpty()) {
+                log.debug("Create new Porosity DB instance.");
+
+                peer = new Porosity(river, depth.getPeer(),
+                    description, timeInterval.getPeer());
+
+                session.save(peer);
+            }
+            else {
+                peer = porosity.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 3f6b9fae1637 -r 02711de579cc backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportPorosityValue.java	Wed Apr 30 14:11:29 2014 +0200
@@ -0,0 +1,102 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.importer;
+
+import java.math.BigDecimal;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+import org.hibernate.Query;
+
+import org.dive4elements.river.model.Porosity;
+import org.dive4elements.river.model.PorosityValue;
+
+
+public class ImportPorosityValue {
+
+    private static final Logger log =
+        Logger.getLogger(ImportPorosityValue.class);
+
+
+    protected PorosityValue peer;
+
+    protected BigDecimal station;
+
+    protected BigDecimal shoreOffset;
+
+    protected BigDecimal porosity;
+
+    protected String description;
+
+
+    public ImportPorosityValue(
+        BigDecimal station,
+        BigDecimal shoreOffset,
+        BigDecimal porosity,
+        String     description
+    ) {
+        this.station     = station;
+        this.shoreOffset = shoreOffset;
+        this.porosity    = porosity;
+        this.description = description;
+    }
+
+
+    public void storeDependencies(Porosity porosity) {
+        log.info("store dependencies");
+
+        getPeer(porosity);
+    }
+
+
+    public PorosityValue getPeer(Porosity porosity) {
+        log.info("get peer");
+
+        if (peer == null) {
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+
+            Query query = session.createQuery(
+                "from PorosityValue where " +
+                "   porosity=:porosity and " +
+                "   station=:station and " +
+                "   shoreOffset=:shoreOffset and " +
+                "   porosityValue=:poros and " +
+                "   description=:description");
+
+            query.setParameter("porosity", porosity);
+            query.setParameter("station", station);
+            query.setParameter("shoreOffset", shoreOffset);
+            query.setParameter("poros", this.porosity);
+            query.setParameter("description", description);
+
+            List<PorosityValue> values = query.list();
+            if (values.isEmpty()) {
+                log.debug("Create new PorosityValue DB instance.");
+
+                peer = new PorosityValue(
+                    porosity,
+                    station,
+                    shoreOffset,
+                    this.porosity,
+                    description);
+
+                session.save(peer);
+            }
+            else {
+                peer = values.get(0);
+            }
+        }
+
+        return peer;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 3f6b9fae1637 -r 02711de579cc backend/src/main/java/org/dive4elements/river/importer/parsers/PorosityParser.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/PorosityParser.java	Wed Apr 30 14:11:29 2014 +0200
@@ -0,0 +1,201 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.importer.parsers;
+
+import org.dive4elements.river.importer.ImportDepth;
+import org.dive4elements.river.importer.ImportPorosity;
+import org.dive4elements.river.importer.ImportPorosityValue;
+import org.dive4elements.river.importer.ImportSedimentDensity;
+import org.dive4elements.river.importer.ImportSedimentDensityValue;
+import org.dive4elements.river.importer.ImportTimeInterval;
+
+import java.io.File;
+import java.io.IOException;
+
+import java.math.BigDecimal;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.log4j.Logger;
+
+public class PorosityParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(PorosityParser.class);
+
+    public static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+    public static final Pattern META_DEPTH =
+        Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
+
+    public static final Pattern META_TIMEINTERVAL =
+        Pattern.compile("^Zeitraum: (\\d{4})-(\\d{4}).*");
+
+    protected List<ImportPorosity> porosities;
+
+    protected ImportPorosity current;
+
+    protected String currentDescription;
+
+    public PorosityParser() {
+        porosities = new ArrayList<ImportPorosity>();
+    }
+
+
+    @Override
+    public void parse(File file) throws IOException {
+        currentDescription = file.getName();
+
+        super.parse(file);
+    }
+
+
+    @Override
+    protected void reset() {
+        current = new ImportPorosity(currentDescription);
+    }
+
+
+    @Override
+    protected void finish() {
+        if (current != null) {
+            porosities.add(current);
+        }
+    }
+
+
+    @Override
+    protected void handleLine(int lineNum, String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    protected void handleMetaLine(String line) {
+        if (handleMetaDepth(line)) {
+            return;
+        }
+        if (handleMetaTimeInterval(line)) {
+            return;
+        }
+        log.warn("Unknown meta line: '" + line + "'");
+    }
+
+    protected boolean handleMetaTimeInterval(String line) {
+        Matcher m = META_TIMEINTERVAL.matcher(line);
+
+        if (m.matches()) {
+            String lo = m.group(1);
+            String up = m.group(2);
+
+            log.debug("Found time interval: " + lo + " - " + up);
+
+            try {
+                int lower = Integer.valueOf(lo);
+                int upper = Integer.valueOf(up);
+
+                Date fromYear = LineParser.getStartDateFromYear(lower);
+                Date toYear   = LineParser.getEndDateFromYear(upper);
+
+                current.setTimeInterval(new ImportTimeInterval(fromYear, toYear));
+            }
+            catch (NumberFormatException e) {
+                log.warn("PP: could not parse timeinterval", e);
+            }
+
+            return true;
+        }
+
+        return false;
+    }
+
+    protected boolean handleMetaDepth(String line) {
+        Matcher m = META_DEPTH.matcher(line);
+
+        if (m.matches()) {
+            String lo   = m.group(1);
+            String up   = m.group(2);
+
+            log.info("Found porosity depth: " + lo + " - " + up + " cm");
+
+            try {
+                ImportDepth depth = new ImportDepth(
+                    new BigDecimal(nf.parse(lo).doubleValue()),
+                    new BigDecimal(nf.parse(up).doubleValue())
+                );
+
+                current.setDepth(depth);
+
+                return true;
+            }
+            catch (ParseException pe) {
+                log.warn("Unparseable numbers in: '" + line + "'");
+            }
+        }
+        else {
+            log.debug("Meta line doesn't contain depth information: " + line);
+        }
+
+        return false;
+    }
+
+    protected void handleDataLine(String line) {
+        String[] vals = line.split(SEPERATOR_CHAR);
+        log.debug("handle line: " + line);
+
+        if (vals == null || vals.length < 3) {
+            log.warn("skip invalid data line: '" + line + "'");
+            return;
+        }
+
+        BigDecimal km = null;
+        BigDecimal shoreOffset = null;
+        BigDecimal porosity = null;
+        try {
+            km          = new BigDecimal(nf.parse(vals[0]).doubleValue());
+            porosity     = new BigDecimal(nf.parse(vals[2]).doubleValue());
+            if (!vals[1].isEmpty()) {
+                shoreOffset = new BigDecimal(nf.parse(vals[1]).doubleValue());
+            }
+        }
+        catch (ParseException pe) {
+            log.warn("Unparseable numbers in '" + line + "'");
+        }
+
+        if (km == null || porosity == null) {
+            log.warn("PP: No km nor porosity given. Skip line");
+            return;
+        }
+        log.debug("add new value.");
+        current.addValue(new ImportPorosityValue(
+            km,
+            shoreOffset,
+            porosity,
+            currentDescription));
+    }
+
+
+    public List<ImportPorosity> getPorosities() {
+        return porosities;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 3f6b9fae1637 -r 02711de579cc backend/src/main/java/org/dive4elements/river/model/Porosity.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/model/Porosity.java	Wed Apr 30 14:11:29 2014 +0200
@@ -0,0 +1,128 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.model;
+
+import java.io.Serializable;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.GenerationType;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToOne;
+import javax.persistence.OneToMany;
+
+
+ at Entity
+ at Table(name = "porosity")
+public class Porosity implements Serializable {
+
+    private Integer id;
+
+    private River river;
+
+    private Depth depth;
+
+    private List<PorosityValue> values;
+
+    private String description;
+
+    private TimeInterval timeInterval;
+
+    public Porosity() {
+    }
+
+
+    public Porosity(
+        River river,
+        Depth depth,
+        String desc,
+        TimeInterval timeInterval
+    ) {
+        this.river       = river;
+        this.depth       = depth;
+        this.description = desc;
+        this.timeInterval = timeInterval;
+    }
+
+    @Id
+    @SequenceGenerator(
+        name           = "SEQUENCE_POROSITY_ID_SEQ",
+        sequenceName   = "POROSITY_ID_SEQ",
+        allocationSize = 1)
+    @GeneratedValue(
+        strategy  = GenerationType.SEQUENCE,
+        generator = "SEQUENCE_POROSITY_ID_SEQ")
+    @Column(name = "id")
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "river_id" )
+    public River getRiver() {
+        return river;
+    }
+
+    public void setRiver(River river) {
+        this.river = river;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "depth_id")
+    public Depth getDepth() {
+        return depth;
+    }
+
+    public void setDepth(Depth depth) {
+        this.depth = depth;
+    }
+
+    @Column(name = "description")
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "time_interval_id")
+    public TimeInterval getTimeInterval() {
+        return timeInterval;
+    }
+
+    public void setTimeInterval(TimeInterval timeInterval) {
+        this.timeInterval = timeInterval;
+    }
+
+    @OneToMany
+    @JoinColumn(name="porosity_id")
+    public List<PorosityValue> getValues() {
+        return values;
+    }
+
+    public void setValues(List<PorosityValue> values) {
+        this.values = values;
+    }
+
+    public void addValue(PorosityValue value) {
+        this.values.add(value);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 3f6b9fae1637 -r 02711de579cc backend/src/main/java/org/dive4elements/river/model/PorosityValue.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backend/src/main/java/org/dive4elements/river/model/PorosityValue.java	Wed Apr 30 14:11:29 2014 +0200
@@ -0,0 +1,121 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+
+package org.dive4elements.river.model;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Column;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.GenerationType;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToOne;
+
+
+ at Entity
+ at Table(name = "porosity_values")
+public class PorosityValue implements Serializable {
+
+    private Integer id;
+
+    private Porosity porosity;
+
+    private BigDecimal station;
+    private BigDecimal shoreOffset;
+    private BigDecimal porosityValue;
+
+    private String description;
+
+
+    public PorosityValue() {
+    }
+
+
+    public PorosityValue(
+        Porosity        porosity,
+        BigDecimal      station,
+        BigDecimal      shoreOffset,
+        BigDecimal      porosityValue,
+        String          desc
+    ) {
+        this.porosity        = porosity;
+        this.station         = station;
+        this.shoreOffset     = shoreOffset;
+        this.porosityValue   = porosityValue;
+        this.description     = desc;
+    }
+
+    @Id
+    @SequenceGenerator(
+        name           = "SEQUENCE_POROSITY_VALUES_ID_SEQ",
+        sequenceName   = "POROSITY_VALUES_ID_SEQ",
+        allocationSize = 1)
+    @GeneratedValue(
+        strategy  = GenerationType.SEQUENCE,
+        generator = "SEQUENCE_POROSITY_VALUES_ID_SEQ")
+    @Column(name = "id")
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    @OneToOne
+    @JoinColumn(name = "porosity_id")
+    public Porosity getPorosity() {
+        return porosity;
+    }
+
+    public void setPorosity(Porosity porosity) {
+        this.porosity = porosity;
+    }
+
+    @Column(name = "station")
+    public BigDecimal getStation() {
+        return station;
+    }
+
+    public void setStation(BigDecimal station) {
+        this.station = station;
+    }
+
+    @Column(name = "shore_offset")
+    public BigDecimal getShoreOffset() {
+        return shoreOffset;
+    }
+
+    public void setShoreOffset(BigDecimal shoreOffset) {
+        this.shoreOffset = shoreOffset;
+    }
+
+    @Column(name = "porosity")
+    public BigDecimal getPorosityValue() {
+        return porosityValue;
+    }
+
+    public void setPorosityValue(BigDecimal porosityValue) {
+        this.porosityValue = porosityValue;
+    }
+
+    @Column(name = "description")
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list