[Lada-commits] [PATCH] Let the database generate id_alt and initial status for new messung objects

Wald Commits scm-commit at wald.intevation.org
Wed Nov 9 12:39:15 CET 2016


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1478691548 -3600
# Node ID 84ff77bba9b0a215fa3e495bf57420f29c702de6
# Parent  315df94c679b46d9302b9cd496438350e076d64f
Let the database generate id_alt and initial status for new messung objects.

diff -r 315df94c679b -r 84ff77bba9b0 db_schema/lada_schema.sql
--- a/db_schema/lada_schema.sql	Wed Nov 09 10:18:51 2016 +0100
+++ b/db_schema/lada_schema.sql	Wed Nov 09 12:39:08 2016 +0100
@@ -18,6 +18,36 @@
 
 SET search_path = land, pg_catalog;
 
+CREATE FUNCTION set_messung_id_alt() RETURNS trigger
+    LANGUAGE plpgsql
+    AS $$
+    BEGIN
+        IF NEW.id_alt IS NULL THEN
+            NEW.id_alt = (
+                SELECT count(*)
+                   FROM land.messung
+                   WHERE probe_id = NEW.probe_id) + 1;
+        END IF;
+        RETURN NEW;
+    END;
+$$;
+
+CREATE FUNCTION set_messung_status() RETURNS trigger
+    LANGUAGE plpgsql
+    AS $$
+    DECLARE status_id integer;
+    BEGIN
+        INSERT INTO land.status_protokoll
+            (mst_id, datum, text, messungs_id, status_kombi)
+        VALUES ((SELECT mst_id
+                     FROM land.probe
+                     WHERE id = NEW.probe_id),
+                now(), '', NEW.id, 1)
+        RETURNING id into status_id;
+        UPDATE land.messung SET status = status_id where id = NEW.id;
+        RETURN NEW;
+    END;
+$$;
 
 CREATE FUNCTION update_letzte_aenderung() RETURNS trigger
     LANGUAGE plpgsql
@@ -279,7 +309,7 @@
 
 CREATE TABLE messung (
     id serial PRIMARY KEY,
-    id_alt integer DEFAULT nextval('land.messung_messung_id_alt_seq'::regclass) NOT NULL,
+    id_alt integer NOT NULL,
     probe_id integer NOT NULL REFERENCES probe ON DELETE CASCADE,
     nebenproben_nr character varying(10),
     mmt_id character varying(2) NOT NULL REFERENCES stammdaten.mess_methode ON DELETE CASCADE,
@@ -293,7 +323,8 @@
 );
 CREATE TRIGGER letzte_aenderung_messung BEFORE UPDATE ON messung FOR EACH ROW EXECUTE PROCEDURE update_letzte_aenderung();
 CREATE TRIGGER tree_modified_messung BEFORE UPDATE ON messung FOR EACH ROW EXECUTE PROCEDURE update_tree_modified_messung();
-
+CREATE TRIGGER id_alt_messung BEFORE INSERT ON land.messung FOR EACH ROW EXECUTE PROCEDURE set_messung_id_alt();
+CREATE TRIGGER status_messung AFTER INSERT ON land.messung FOR EACH ROW EXECUTE PROCEDURE set_messung_status();
 
 --
 -- Name: kommentar_m; Type: TABLE; Schema: land; Owner: -; Tablespace:
diff -r 315df94c679b -r 84ff77bba9b0 src/main/java/de/intevation/lada/factory/ProbeFactory.java
--- a/src/main/java/de/intevation/lada/factory/ProbeFactory.java	Wed Nov 09 10:18:51 2016 +0100
+++ b/src/main/java/de/intevation/lada/factory/ProbeFactory.java	Wed Nov 09 12:39:08 2016 +0100
@@ -366,15 +366,6 @@
             messung.setProbeId(probe.getId());
             repository.create(messung, "land");
 
-            StatusProtokoll status = new StatusProtokoll();
-            status.setDatum(new Timestamp(new Date().getTime()));
-            status.setMessungsId(messung.getId());
-            status.setMstId(probe.getMstId());
-            status.setStatusKombi(1);
-            repository.create(status, "land");
-            messung.setStatus(status.getId());
-            repository.update(messung, "land");
-
             for (int mw : mmt.getMessgroessen()) {
                 Messwert wert = new Messwert();
                 wert.setMessgroesseId(mw);
diff -r 315df94c679b -r 84ff77bba9b0 src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java
--- a/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java	Wed Nov 09 10:18:51 2016 +0100
+++ b/src/main/java/de/intevation/lada/importer/laf/LafObjectMapper.java	Wed Nov 09 12:39:08 2016 +0100
@@ -316,14 +316,6 @@
                 newMessung = ((Messung)created.getData());
                 created = repository.getById(Messung.class, newMessung.getId(), "land");
                 newMessung = ((Messung)created.getData());
-                StatusProtokoll status = new StatusProtokoll();
-                status.setDatum(new Timestamp(new Date().getTime()));
-                status.setMessungsId(newMessung.getId());
-                status.setMstId(mstId);
-                status.setStatusKombi(1);
-                Response st = repository.create(status, "land");
-                newMessung.setStatus(((StatusProtokoll)st.getData()).getId());
-                repository.update(newMessung, "land");
                 if (object.getAttributes().containsKey("BEARBEITUNGSSTATUS")) {
                     createStatusProtokoll(object.getAttributes().get("BEARBEITUNGSSTATUS"), newMessung, mstId);
                 }
diff -r 315df94c679b -r 84ff77bba9b0 src/main/java/de/intevation/lada/rest/MessungService.java
--- a/src/main/java/de/intevation/lada/rest/MessungService.java	Wed Nov 09 10:18:51 2016 +0100
+++ b/src/main/java/de/intevation/lada/rest/MessungService.java	Wed Nov 09 12:39:08 2016 +0100
@@ -293,28 +293,13 @@
 
         /* Persist the new messung object*/
         Response response = repository.create(messung, "land");
-        Messung ret = (Messung)response.getData();
-        Messung refreshed = repository.getByIdPlain(Messung.class, ret.getId(), "land");
         if(violation.hasWarnings()) {
             response.setWarnings(violation.getWarnings());
         }
 
-        StatusProtokoll status = new StatusProtokoll();
-        status.setDatum(new Timestamp(new Date().getTime()));
-        status.setMessungsId((ret.getId()));
-        Probe probe =
-            repository.getByIdPlain(Probe.class, ret.getProbeId(), "land");
-        status.setMstId(probe.getMstId());
-        status.setStatusKombi(1);
-        repository.create(status, "land");
-        refreshed.setStatus(status.getId());
-        repository.update(refreshed, "land");
-        Response updated=
-            repository.getById(Messung.class, refreshed.getId(), "land");
-
         return authorization.filter(
             request,
-            updated,
+            response,
             Messung.class);
     }
 


More information about the Lada-commits mailing list