From 8beacdc59122cf3564959a930a18b727b64dfb56 Mon Sep 17 00:00:00 2001 From: mguzman14 Date: Thu, 29 Aug 2024 17:32:03 +0200 Subject: [PATCH] feat(trg_edit): insert sector_id and muni_id automatically if null --- ud/ftrg/ud_gw_trg_edit_dma.sql | 18 +++++++++++++++--- ud/ftrg/ud_gw_trg_edit_drainzone.sql | 9 +++++++++ ud/ftrg/ud_gw_trg_edit_raingage.sql | 11 +++++++++++ updates/36/36012/ud/ddl.sql | 2 ++ updates/36/36012/ud/ddlview.sql | 1 + utils/ftrg/gw_trg_edit_dimensions.sql | 19 +++++++++++++++++++ utils/ftrg/gw_trg_edit_element.sql | 20 ++++++++++++++++++++ utils/ftrg/gw_trg_edit_samplepoint.sql | 23 ++++++++++++++++++++++- ws/ftrg/ws_gw_trg_edit_dma.sql | 9 +++++++++ ws/ftrg/ws_gw_trg_edit_dqa.sql | 10 ++++++++++ ws/ftrg/ws_gw_trg_edit_presszone.sql | 10 ++++++++++ ws/ftrg/ws_gw_trg_edit_unconnected.sql | 14 ++++++++++++-- 12 files changed, 140 insertions(+), 6 deletions(-) diff --git a/ud/ftrg/ud_gw_trg_edit_dma.sql b/ud/ftrg/ud_gw_trg_edit_dma.sql index ad9473a273..a011cd7887 100644 --- a/ud/ftrg/ud_gw_trg_edit_dma.sql +++ b/ud/ftrg/ud_gw_trg_edit_dma.sql @@ -28,14 +28,26 @@ BEGIN ST_DWithin(NEW.the_geom, exploitation.the_geom,0.001) LIMIT 1); END IF; END IF; + + + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- active IF NEW.active IS NULL THEN NEW.active = TRUE; END IF; - INSERT INTO dma (dma_id, name, descript, macrodma_id, the_geom, undelete, expl_id, link, active, stylesheet, expl_id2, sector_id) - VALUES (NEW.dma_id, NEW.name, NEW.descript, NEW.macrodma_id, NEW.the_geom, NEW.undelete, NEW.expl_id, NEW.link, NEW.active, NEW.stylesheet, NEW.expl_id2, NEW.sector_id); + INSERT INTO dma (dma_id, name, descript, macrodma_id, the_geom, undelete, expl_id, link, active, stylesheet, sector_id) + VALUES (NEW.dma_id, NEW.name, NEW.descript, NEW.macrodma_id, NEW.the_geom, NEW.undelete, NEW.expl_id, NEW.link, NEW.active, NEW.stylesheet, NEW.sector_id); RETURN NEW; @@ -44,7 +56,7 @@ BEGIN UPDATE dma SET dma_id=NEW.dma_id, name=NEW.name, descript=NEW.descript, the_geom=NEW.the_geom, undelete=NEW.undelete, expl_id=NEW.expl_id, link=NEW.link, active=NEW.active, lastupdate=now(), - lastupdate_user = current_user, macrodma_id = NEW.macrodma_id, stylesheet=NEW.stylesheet, expl_id2 = NEW.expl_id2, sector=NEW.sector_id + lastupdate_user = current_user, macrodma_id = NEW.macrodma_id, stylesheet=NEW.stylesheet, sector=NEW.sector_id WHERE dma_id=OLD.dma_id; RETURN NEW; diff --git a/ud/ftrg/ud_gw_trg_edit_drainzone.sql b/ud/ftrg/ud_gw_trg_edit_drainzone.sql index 07dc92cfc4..b290e0d6be 100644 --- a/ud/ftrg/ud_gw_trg_edit_drainzone.sql +++ b/ud/ftrg/ud_gw_trg_edit_drainzone.sql @@ -28,6 +28,15 @@ BEGIN END IF; END IF; + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + END IF; + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + -- active IF NEW.active IS NULL THEN NEW.active = TRUE; diff --git a/ud/ftrg/ud_gw_trg_edit_raingage.sql b/ud/ftrg/ud_gw_trg_edit_raingage.sql index 4e2739b482..2a27f8d0b7 100644 --- a/ud/ftrg/ud_gw_trg_edit_raingage.sql +++ b/ud/ftrg/ud_gw_trg_edit_raingage.sql @@ -30,6 +30,17 @@ BEGIN END IF; END IF; + -- Municipality + + -- getting value from geometry of mapzone + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := (SELECT muni_id FROM ext_municipality WHERE ST_intersects(NEW.the_geom, ext_municipality.the_geom) AND active IS TRUE limit 1); + END IF; + + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := 0 + END IF; + -- FEATURE INSERT INSERT INTO raingage (rg_id, form_type, intvl, scf, rgage_type, timser_id, fname, sta, units, the_geom, expl_id, muni_id) VALUES (NEW.rg_id, NEW.form_type, NEW.intvl, NEW.scf, NEW.rgage_type, NEW.timser_id, NEW.fname, NEW.sta, NEW.units, NEW.the_geom, NEW.expl_id, NEW.muni_id); diff --git a/updates/36/36012/ud/ddl.sql b/updates/36/36012/ud/ddl.sql index f12a21c476..f6be0cefd7 100644 --- a/updates/36/36012/ud/ddl.sql +++ b/updates/36/36012/ud/ddl.sql @@ -45,6 +45,8 @@ SELECT gw_fct_admin_manage_fields($${"data":{"action":"DROP","table":"dma", "col SELECT gw_fct_admin_manage_fields($${"data":{"action":"DROP","table":"dma", "column":"min"}}$$); SELECT gw_fct_admin_manage_fields($${"data":{"action":"DROP","table":"dma", "column":"max"}}$$); SELECT gw_fct_admin_manage_fields($${"data":{"action":"DROP","table":"dma", "column":"effec"}}$$); +SELECT gw_fct_admin_manage_fields($${"data":{"action":"DROP","table":"dma", "column":"expl_id2"}}$$); + DROP FUNCTION IF EXISTS gw_fct_graphanalytics_downstream_recursive; diff --git a/updates/36/36012/ud/ddlview.sql b/updates/36/36012/ud/ddlview.sql index e48f530a87..3994461951 100644 --- a/updates/36/36012/ud/ddlview.sql +++ b/updates/36/36012/ud/ddlview.sql @@ -78,6 +78,7 @@ AS SELECT dma.sector_id, s.name as sector_name, dma.descript, + dma.undelete, dma.link, dma.active, dma.stylesheet, diff --git a/utils/ftrg/gw_trg_edit_dimensions.sql b/utils/ftrg/gw_trg_edit_dimensions.sql index b71f76601e..a4b5746c49 100644 --- a/utils/ftrg/gw_trg_edit_dimensions.sql +++ b/utils/ftrg/gw_trg_edit_dimensions.sql @@ -31,6 +31,25 @@ BEGIN END IF; END IF; + + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- Municipality + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := (SELECT m.muni_id FROM sector, ext_municipality m WHERE ST_intersects(m.the_geom, sector.the_geom) AND sector.active IS TRUE limit 1); + + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := 0; + END IF; + END IF; + -- State IF (NEW.state IS NULL) THEN NEW.state := (SELECT "value" FROM config_param_user WHERE "parameter"='edit_state_vdefault' AND "cur_user"="current_user"()); diff --git a/utils/ftrg/gw_trg_edit_element.sql b/utils/ftrg/gw_trg_edit_element.sql index 5460040652..0abe3e0066 100644 --- a/utils/ftrg/gw_trg_edit_element.sql +++ b/utils/ftrg/gw_trg_edit_element.sql @@ -114,6 +114,26 @@ BEGIN END IF; END IF; END IF; + + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- Municipality + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := (SELECT m.muni_id FROM sector, ext_municipality m WHERE ST_intersects(m.the_geom, sector.the_geom) AND sector.active IS TRUE limit 1); + + + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := 0; + END IF; + END IF; + -- Enddate IF (NEW.state > 0) THEN diff --git a/utils/ftrg/gw_trg_edit_samplepoint.sql b/utils/ftrg/gw_trg_edit_samplepoint.sql index 6db96d416d..21f592ae26 100644 --- a/utils/ftrg/gw_trg_edit_samplepoint.sql +++ b/utils/ftrg/gw_trg_edit_samplepoint.sql @@ -64,7 +64,7 @@ BEGIN -- control error when no value IF (NEW.expl_id IS NULL) THEN EXECUTE 'SELECT gw_fct_getmessage($${"client":{"device":4, "infoType":1, "lang":"ES"},"feature":{}, - "data":{"message":"2012", "function":"1302","debug_msg":"'||NEW.arc_id::text||'"}}$$);'; + "data":{"message":"2012", "function":"1302","debug_msg":"'||NEW.sample_id::text||'"}}$$);'; END IF; END IF; @@ -79,6 +79,27 @@ BEGIN NEW.dma_id := (SELECT "value" FROM config_param_user WHERE "parameter"='edit_dma_vdefault' AND "cur_user"="current_user"()); END IF; END IF; + + + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- Municipality + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := (SELECT m.muni_id FROM sector, ext_municipality m WHERE ST_intersects(m.the_geom, sector.the_geom) AND sector.active IS TRUE limit 1); + + + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := 0; + END IF; + END IF; + IF v_projectype = 'WS' THEN INSERT INTO samplepoint (sample_id, code, lab_code, feature_id, featurecat_id, dma_id, presszone_id, "state", builtdate, enddate, diff --git a/ws/ftrg/ws_gw_trg_edit_dma.sql b/ws/ftrg/ws_gw_trg_edit_dma.sql index 36edb726e0..5c0938f7dd 100644 --- a/ws/ftrg/ws_gw_trg_edit_dma.sql +++ b/ws/ftrg/ws_gw_trg_edit_dma.sql @@ -35,6 +35,15 @@ BEGIN END IF; END IF; + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + -- active IF NEW.active IS NULL THEN NEW.active = TRUE; diff --git a/ws/ftrg/ws_gw_trg_edit_dqa.sql b/ws/ftrg/ws_gw_trg_edit_dqa.sql index c5d5f39d59..68676888d1 100644 --- a/ws/ftrg/ws_gw_trg_edit_dqa.sql +++ b/ws/ftrg/ws_gw_trg_edit_dqa.sql @@ -27,6 +27,16 @@ BEGIN END IF; END IF; + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- active IF NEW.active IS NULL THEN NEW.active = TRUE; diff --git a/ws/ftrg/ws_gw_trg_edit_presszone.sql b/ws/ftrg/ws_gw_trg_edit_presszone.sql index 10a9289a92..32159978d7 100644 --- a/ws/ftrg/ws_gw_trg_edit_presszone.sql +++ b/ws/ftrg/ws_gw_trg_edit_presszone.sql @@ -27,6 +27,16 @@ BEGIN END IF; END IF; + -- Sector + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := (SELECT sector_id FROM sector WHERE ST_intersects(NEW.the_geom, sector.the_geom) AND active IS TRUE limit 1); + + IF (NEW.sector_id IS NULL) THEN + NEW.sector_id := 0; + END IF; + END IF; + + -- active IF NEW.active IS NULL THEN NEW.active = TRUE; diff --git a/ws/ftrg/ws_gw_trg_edit_unconnected.sql b/ws/ftrg/ws_gw_trg_edit_unconnected.sql index 96a50091f8..ea4cb3e1dd 100644 --- a/ws/ftrg/ws_gw_trg_edit_unconnected.sql +++ b/ws/ftrg/ws_gw_trg_edit_unconnected.sql @@ -35,7 +35,16 @@ BEGIN IF (expl_id_int IS NULL) THEN expl_id_int := (SELECT "value" FROM config_param_user WHERE "parameter"='edit_exploitation_vdefault' AND "cur_user"="current_user"()); END IF; + + -- Municipality + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := (SELECT m.muni_id FROM ext_municipality m WHERE ST_intersects(NEW.the_geom, m.the_geom) AND active IS TRUE limit 1); + IF (NEW.muni_id IS NULL) THEN + NEW.muni_id := 0; + END IF; + END IF; + -- State IF (NEW.state IS NULL) THEN NEW.state := (SELECT "value" FROM config_param_user WHERE "parameter"='edit_state_vdefault' AND "cur_user"="current_user"()); @@ -52,14 +61,15 @@ BEGIN --"data":{"message":"1012", "function":"1330","debug_msg":null, "variables":null}}$$); RETURN NULL; END IF; - NEW.dma_id := (SELECT dma_id FROM dma WHERE ST_DWithin(NEW.the_geom, dma.the_geom,0.001) AND active IS TRUE LIMIT 1); + NEW.dma_id := (SELECT dma_id FROM dma WHERE ST_DWithin(NEW.the_geom, dma.the_geom,0.001) AND dma.active IS TRUE LIMIT 1); IF (NEW.dma_id IS NULL) THEN NEW.dma_id := (SELECT "value" FROM config_param_user WHERE "parameter"='edit_dma_vdefault' AND "cur_user"="current_user"()); END IF; IF (NEW.dma_id IS NULL) THEN --PERFORM gw_fct_getmessage($${"client":{"device":4, "infoType":1, "lang":"ES"},"feature":{}, --"data":{"message":"1014", "function":"1330","debug_msg":null, "variables":null}}$$); - RETURN NULL; + NEW.dma_id := 0; + END IF; END IF;