[PATCH] SCHEMA CHANGE and Importer: get time intervals of SQ-relations from metalines in sediment load files

Wald Commits scm-commit at wald.intevation.org
Fri Jul 18 18:01:53 CEST 2014


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1405699309 -7200
# Node ID bde5f5ec7c72611dcee58864f43c65be796b0bcb
# Parent  fbe3ba5a480ebc100a54106fba7809ceadef00e6
SCHEMA CHANGE and Importer: get time intervals of SQ-relations from metalines in sediment load files.

diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/doc/schema/oracle-minfo.sql
--- a/backend/doc/schema/oracle-minfo.sql	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/doc/schema/oracle-minfo.sql	Fri Jul 18 18:01:49 2014 +0200
@@ -275,6 +275,7 @@
     grain_fraction_id   NUMBER(38,0),
     unit_id             NUMBER(38,0) NOT NULL,
     time_interval_id    NUMBER(38,0) NOT NULL,
+    sq_time_interval_id NUMBER(38,0),
     description         VARCHAR(256),
     kind                NUMBER(38,0),
     PRIMARY KEY (id),
@@ -282,7 +283,9 @@
     CONSTRAINT fk_sy_kind_id FOREIGN KEY (kind) REFERENCES sediment_yield_kinds(id),
     CONSTRAINT fk_sy_grain_fraction_id FOREIGN KEY (grain_fraction_id) REFERENCES grain_fraction(id),
     CONSTRAINT fk_sy_unit_id FOREIGN KEY (unit_id) REFERENCES units(id),
-    CONSTRAINT fk_sy_time_interval_id FOREIGN KEY (time_interval_id) REFERENCES time_intervals(id)
+    CONSTRAINT fk_sy_time_interval_id FOREIGN KEY (time_interval_id) REFERENCES time_intervals(id),
+    CONSTRAINT fk_sy_sq_time_interval_id FOREIGN KEY (sq_time_interval_id)
+        REFERENCES time_intervals(id)
 );
 
 
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/doc/schema/postgresql-minfo.sql
--- a/backend/doc/schema/postgresql-minfo.sql	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/doc/schema/postgresql-minfo.sql	Fri Jul 18 18:01:49 2014 +0200
@@ -275,6 +275,7 @@
     grain_fraction_id   int,
     unit_id             int NOT NULL,
     time_interval_id    int NOT NULL,
+    sq_time_interval_id int,
     description         VARCHAR(256),
     kind                int,
     PRIMARY KEY (id),
@@ -282,7 +283,9 @@
     CONSTRAINT fk_sy_kind_id FOREIGN KEY (kind) REFERENCES sediment_yield_kinds(id),
     CONSTRAINT fk_sy_grain_fraction_id FOREIGN KEY (grain_fraction_id) REFERENCES grain_fraction(id),
     CONSTRAINT fk_sy_unit_id FOREIGN KEY (unit_id) REFERENCES units(id),
-    CONSTRAINT fk_sy_time_interval_id FOREIGN KEY (time_interval_id) REFERENCES time_intervals(id)
+    CONSTRAINT fk_sy_time_interval_id FOREIGN KEY (time_interval_id) REFERENCES time_intervals(id),
+    CONSTRAINT fk_sy_sq_time_interval_id FOREIGN KEY (sq_time_interval_id)
+        REFERENCES time_intervals(id)
 );
 
 
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoad.java	Fri Jul 18 18:01:49 2014 +0200
@@ -29,13 +29,15 @@
     public ImportSedimentLoad (
         ImportGrainFraction grainFraction,
         ImportTimeInterval  timeInterval,
+        ImportTimeInterval  sqTimeInterval,
         String              description,
         Integer             kind
     ) {
-        this.grainFraction = grainFraction;
-        this.timeInterval  = timeInterval;
-        this.description   = description;
-        this.kind          = kind;
+        this.grainFraction  = grainFraction;
+        this.timeInterval   = timeInterval;
+        this.sqTimeInterval = sqTimeInterval;
+        this.description    = description;
+        this.kind           = kind;
 
         this.values = new ArrayList<ImportSedimentLoadValue>();
     }
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLS.java
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLS.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportSedimentLoadLS.java	Fri Jul 18 18:01:49 2014 +0200
@@ -33,6 +33,8 @@
 
     private ImportTimeInterval timeInterval;
 
+    private ImportTimeInterval sqTimeInterval;
+
     private String description;
 
     private Integer kind;
@@ -50,6 +52,10 @@
         this.timeInterval = timeInterval;
     }
 
+    public void setSQTimeInterval(ImportTimeInterval sqTimeInterval) {
+        this.sqTimeInterval = sqTimeInterval;
+    }
+
     public void setUnit(ImportUnit unit) {
         this.unit = unit;
     }
@@ -92,6 +98,9 @@
         Unit u = unit != null ? unit.getPeer() : null;
 
         TimeInterval ti = timeInterval != null ? timeInterval.getPeer() : null;
+        TimeInterval sqti = sqTimeInterval != null
+                ? sqTimeInterval.getPeer()
+                : null;
 
         if (ti == null || u == null) {
             log.warn("Skip invalid SedimentLoadLS: time interval or unit null!");
@@ -101,23 +110,34 @@
         if (peer == null) {
             Session session = ImporterSession.getInstance()
                 .getDatabaseSession();
+
+            String sqtquery = sqTimeInterval == null ?
+                "sq_time_interval_id is null" :
+                "sqTimeInterval = :sqTimeInterval";
             Query query = session.createQuery("from SedimentLoadLS where "
                 + "   river=:river and "
-                + "   grainFraction=:grainFraction and " + "   unit=:unit and "
+                + "   grainFraction=:grainFraction and "
+                + "   unit=:unit and "
                 + "   timeInterval=:timeInterval and "
-                + "   description=:description");
+                + "   description=:description and "
+                + "   kind = :kind and " +
+                      sqtquery);
 
             query.setParameter("river", river);
             query.setParameter("grainFraction", gf);
             query.setParameter("unit", u);
             query.setParameter("timeInterval", ti);
+            if (sqti != null) {
+                query.setParameter("sqTimeInterval", sqti);
+            }
             query.setParameter("description", description);
+            query.setParameter("kind", kind);
 
             List<SedimentLoadLS> loads = query.list();
             if (loads.isEmpty()) {
                 log.debug("create new SedimentLoadLS");
 
-                peer = new SedimentLoadLS(river, u, ti, gf, description);
+                peer = new SedimentLoadLS(river, u, ti, sqti, gf, description);
                 peer.setKind(this.kind);
                 session.save(peer);
             }
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/importer/parsers/AbstractSedimentLoadParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/AbstractSedimentLoadParser.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/AbstractSedimentLoadParser.java	Fri Jul 18 18:01:49 2014 +0200
@@ -52,6 +52,9 @@
     public static final Pattern META_FRACTION_NAME =
         Pattern.compile("^Fraktionsname: (.*)");
 
+    public static final Pattern META_SQ_TIMEINTERVAL =
+        Pattern.compile("^S-Q-Beziehung: (.*)");
+
     public static final Pattern META_COLUMN_NAMES =
         Pattern.compile("^Fluss-km.*");
 
@@ -71,6 +74,8 @@
 
     protected ImportGrainFraction grainFraction;
 
+    protected ImportTimeInterval sqTimeInterval;
+
     protected String description;
 
     protected String[] columnNames;
@@ -164,6 +169,41 @@
     }
 
 
+    public boolean handleMetaSQTimeInterval(String line) {
+        Matcher m = META_SQ_TIMEINTERVAL.matcher(line);
+
+        if (m.matches()) {
+            String interval = m.group(1);
+
+            try {
+                Matcher a = TIMEINTERVAL_EPOCH.matcher(interval);
+                if (a.matches()) {
+                    int yearA = nf.parse(a.group(1)).intValue();
+                    int yearB = nf.parse(a.group(2)).intValue();
+
+                    sqTimeInterval = new ImportTimeInterval(
+                        DateUtil.getStartDateFromYear(yearA),
+                        DateUtil.getEndDateFromYear(yearB)
+                    );
+                }
+                else {
+                    log.warn("ASLP: Unknown SQ-time string: '" + interval +
+                        "'. Ignored.");
+                }
+            }
+            catch (ParseException pe) {
+                log.error("ASLP: Could not parse SQ-time string: '" +
+                    interval + "'. Ignored.", pe);
+            }
+
+            return true;
+
+        }
+
+        return false;
+    }
+
+
     public boolean handleColumnNames(String line) throws LineParserException {
         Matcher m = META_COLUMN_NAMES.matcher(line);
 
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java	Fri Jul 18 18:01:49 2014 +0200
@@ -83,6 +83,9 @@
         if (handleMetaFractionName(line)) {
             return;
         }
+        if (handleMetaSQTimeInterval(line)) {
+            return;
+        }
         if (handleColumnNames(line)) {
             return;
         }
@@ -147,6 +150,7 @@
         for (int i = 0, n = columnNames.length; i < n-2; i++) {
             current[i] = new ImportSedimentLoadLS(this.description);
             current[i].setTimeInterval(getTimeInterval(columnNames[i+1]));
+            current[i].setSQTimeInterval(sqTimeInterval);
             current[i].setUnit(unit);
             current[i].setGrainFraction(grainFraction);
             current[i].setKind(kind);
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadParser.java
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadParser.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadParser.java	Fri Jul 18 18:01:49 2014 +0200
@@ -88,6 +88,9 @@
         if (handleMetaFractionName(line)) {
             return;
         }
+        if (handleMetaSQTimeInterval(line)) {
+            return;
+        }
         if (handleColumnNames(line)) {
             return;
         }
@@ -184,6 +187,7 @@
             current[i] = new ImportSedimentLoad(
                 grainFraction,
                 getTimeInterval(columnNames[i+1]),
+                sqTimeInterval,
                 description,
                 kind);
         }
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java
--- a/backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/SedimentLoad.java	Fri Jul 18 18:01:49 2014 +0200
@@ -96,11 +96,11 @@
 
     @OneToOne
     @JoinColumn(name = "sq_time_interval_id")
-    public TimeInterval getSQTimeInterval() {
+    public TimeInterval getSqTimeInterval() {
         return sqTimeInterval;
     }
 
-    public void setSQTimeInterval(TimeInterval sqTimeInterval) {
+    public void setSqTimeInterval(TimeInterval sqTimeInterval) {
         this.sqTimeInterval = sqTimeInterval;
     }
 
diff -r fbe3ba5a480e -r bde5f5ec7c72 backend/src/main/java/org/dive4elements/river/model/SedimentLoadLS.java
--- a/backend/src/main/java/org/dive4elements/river/model/SedimentLoadLS.java	Fri Jul 18 17:43:27 2014 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/SedimentLoadLS.java	Fri Jul 18 18:01:49 2014 +0200
@@ -40,6 +40,8 @@
 
     private TimeInterval timeInterval;
 
+    private TimeInterval sqTimeInterval;
+
     private String description;
 
     private List<SedimentLoadLSValue> values;
@@ -75,12 +77,14 @@
         River         river,
         Unit          unit,
         TimeInterval  timeInterval,
+        TimeInterval  sqTimeInterval,
         GrainFraction grainFraction,
         String        description
     ) {
         this(river, unit, timeInterval, grainFraction);
 
-        this.description = description;
+        this.sqTimeInterval = sqTimeInterval;
+        this.description    = description;
     }
 
     @Id
@@ -140,6 +144,16 @@
         this.timeInterval = timeInterval;
     }
 
+    @OneToOne
+    @JoinColumn(name = "sq_time_interval_id")
+    public TimeInterval getSqTimeInterval() {
+        return sqTimeInterval;
+    }
+
+    public void setSqTimeInterval(TimeInterval sqTimeInterval) {
+        this.sqTimeInterval = sqTimeInterval;
+    }
+
     @Column(name = "description")
     public String getDescription() {
         return description;


More information about the Dive4Elements-commits mailing list