diff --git a/ud/ftrg/ud_gw_trg_edit_sector.sql b/ud/ftrg/ud_gw_trg_edit_sector.sql index 0b5bb7e8e..5e7075baf 100644 --- a/ud/ftrg/ud_gw_trg_edit_sector.sql +++ b/ud/ftrg/ud_gw_trg_edit_sector.sql @@ -10,10 +10,6 @@ CREATE OR REPLACE FUNCTION "SCHEMA_NAME".gw_trg_edit_sector() RETURNS trigger AS $BODY$ DECLARE -v_querytext text; -v_row record; -v_sector_id integer[]; -v_user text; BEGIN @@ -26,6 +22,10 @@ BEGIN INSERT INTO sector (sector_id, name, descript, macrosector_id, the_geom, undelete, active, parent_id, stylesheet, sector_type, graphconfig) VALUES (NEW.sector_id, NEW.name, NEW.descript, NEW.macrosector_id, NEW.the_geom, NEW.undelete, NEW.active, NEW.parent_id, NEW.stylesheet, NEW.sector_type, NEW.graphconfig::json); + + INSERT INTO selector_sector VALUES (NEW.sector_id, current_user); + + RETURN NEW; ELSIF TG_OP = 'UPDATE' THEN @@ -34,99 +34,17 @@ BEGIN undelete=NEW.undelete, active=NEW.active, lastupdate=now(), lastupdate_user = current_user, stylesheet=NEW.stylesheet, sector_type=NEW.sector_type, graphconfig=NEW.graphconfig::json WHERE sector_id=OLD.sector_id; + + RETURN NEW; ELSIF TG_OP = 'DELETE' THEN DELETE FROM sector WHERE sector_id = OLD.sector_id; + + RETURN NULL; + END IF; - -- manage admin_exploitation_x_user - IF (SELECT value::boolean FROM config_param_system WHERE parameter = 'admin_exploitation_x_user') THEN - - IF TG_OP = 'INSERT' THEN - - IF NEW.parent_id IS NULL THEN - - IF (SELECT count(*) FROM selector_expl WHERE cur_user = current_user) = 1 THEN - NEW.parent_id = (SELECT expl_id FROM selector_expl WHERE cur_user = current_user LIMIT 1) ; - END IF; - - -- profilactic control in case of parent_id null - IF NEW.parent_id IS NULL THEN - INSERT INTO config_user_x_sector VALUES (NEW.sector_id, v_user); - INSERT INTO selector_sector VALUES (NEW.sector_id, v_user); - END IF; - END IF; - - IF NEW.parent_id IS NOT NULL THEN - - UPDATE sector SET parent_id = NEW.parent_id WHERE sector_id = NEW.sector_id; - - -- manage cat_manager - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a - WHERE sector_id = '||NEW.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - -- update array of sectors for specific row - v_sector_id = array_append (v_row.sector_id, NEW.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - - -- insert new sector on selector for all those user allowed on specific row - FOR v_user IN SELECT unnest(username) FROM cat_manager WHERE id = v_row.id - LOOP - INSERT INTO selector_sector VALUES (NEW.sector_id, v_user); - END LOOP; - END LOOP; - END IF; - - RETURN NEW; - - ELSIF TG_OP = 'UPDATE' THEN - - IF ((NEW.parent_id <> OLD.parent_id) OR (OLD.parent_id IS NULL AND NEW.parent_id IS NOT NULL)) AND (SELECT parent_id FROM sector WHERE parent_id = NEW.sector_id) IS NULL THEN - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||OLD.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - raise notice 'v_row1 %', v_row; - v_sector_id = array_remove (v_row.sector_id, OLD.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||NEW.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - raise notice 'v_row2 %', v_row; - v_sector_id = array_append (v_row.sector_id, NEW.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - END IF; - - RETURN NEW; - - ELSIF TG_OP = 'DELETE' THEN - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||OLD.sector_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - v_sector_id = array_remove (v_row.sector_id, OLD.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - RETURN NULL; - END IF; - ELSE - IF TG_OP ='INSERT'THEN - INSERT INTO selector_sector VALUES (NEW.sector_id, current_user); - RETURN NEW; - - ELSIF TG_OP ='UPDATE' THEN - RETURN NEW; - ELSE - RETURN NULL; - END IF; - END IF; END; $BODY$ LANGUAGE plpgsql VOLATILE diff --git a/updates/36/36017/ud/ddlview.sql b/updates/36/36017/ud/ddlview.sql index 6a28a5642..7f6a67e7b 100644 --- a/updates/36/36017/ud/ddlview.sql +++ b/updates/36/36017/ud/ddlview.sql @@ -1014,7 +1014,44 @@ SELECT distinct on (sector_id) s.sector_id, JOIN config_user_x_expl USING (expl_id) LEFT JOIN macrosector m USING (macrosector_id) LEFT JOIN edit_typevalue et ON et.id::text = s.sector_type::text AND et.typevalue::text = 'sector_type'::text - where username = current_user and s.active and sector_id > 0 - ORDER BY 1; - - \ No newline at end of file + where username = current_user and sector_id > 0 + union + SELECT distinct on (sector_id) s.sector_id, + s.name, + s.macrosector_id, + m.name AS macrosector_name, + et.idval, + s.descript, + s.parent_id, + s.graphconfig::text AS graphconfig, + s.stylesheet::text AS stylesheet, + s.link, + s.active, + s.undelete, + s.tstamp, + s.insert_user, + s.lastupdate, + s.lastupdate_user, + s.the_geom + FROM sector s + LEFT JOIN ( SELECT DISTINCT node.sector_id, node.expl_id FROM node WHERE node.state > 0) a USING (sector_id) + LEFT JOIN macrosector m USING (macrosector_id) + LEFT JOIN edit_typevalue et ON et.id::text = s.sector_type::text AND et.typevalue::text = 'sector_type'::text + where s.sector_id > 0 and a.sector_id is null + ORDER BY 1; + + +CREATE OR REPLACE VIEW v_edit_sector +AS SELECT sector.sector_id, + sector.name, + sector.descript, + sector.macrosector_id, + sector.sector_type, + sector.the_geom, + sector.undelete, + sector.active, + sector.parent_id, + sector.graphconfig::text AS graphconfig, + sector.stylesheet + FROM selector_sector, sector + WHERE sector.sector_id = selector_sector.sector_id AND selector_sector.cur_user = "current_user"()::text and active is true; \ No newline at end of file diff --git a/updates/36/36017/utils/ddlview.sql b/updates/36/36017/utils/ddlview.sql index fe02fe71f..0ef5fde5c 100644 --- a/updates/36/36017/utils/ddlview.sql +++ b/updates/36/36017/utils/ddlview.sql @@ -13,14 +13,20 @@ SET search_path = SCHEMA_NAME, public, pg_catalog; CREATE OR REPLACE VIEW vu_exploitation as select e.* from exploitation e JOIN config_user_x_expl USING (expl_id) -where username = current_user and e.active and expl_id > 0 +where username = current_user and expl_id > 0 order by 1; +select * from config_user_x_expl + CREATE OR REPLACE VIEW vu_macroexploitation as select distinct on (macroexpl_id) m.* from macroexploitation m join exploitation using (macroexpl_id) JOIN config_user_x_expl USING (expl_id) -where username = current_user and m.active and macroexpl_id > 0 +where username = current_user and macroexpl_id > 0 +UNION +select distinct on (macroexpl_id) m.* from macroexploitation m +LEFT join exploitation using (macroexpl_id) +where expl_id IS NULL and macroexpl_id > 0 order by 1; CREATE OR REPLACE VIEW vu_macrosector as @@ -29,12 +35,34 @@ join sector using (macrosector_id) JOIN (SELECT DISTINCT sector_id, expl_id FROM node WHERE state > 0) a USING (sector_id) join exploitation using (expl_id) JOIN config_user_x_expl USING (expl_id) -where username = current_user and m.active and macrosector_id > 0 +where username = current_user and macrosector_id > 0 +UNION +select distinct on (macrosector_id) m.* from macrosector m +LEFT join sector using (macrosector_id) +where sector_id IS NULL and macrosector_id > 0 order by 1; CREATE OR REPLACE VIEW vu_ext_municipality as select m.* from ext_municipality m join (SELECT DISTINCT muni_id, expl_id FROM node WHERE state > 0) a USING (muni_id) JOIN config_user_x_expl USING (expl_id) -where username = current_user and m.active and muni_id > 0; -------- +where username = current_user and muni_id > 0 +UNION +select m.* from ext_municipality m +LEFT join (SELECT DISTINCT muni_id, expl_id FROM node WHERE state > 0) a USING (muni_id) +where a.muni_id IS NULL and muni_id > 0 +order by 1; + +CREATE OR REPLACE VIEW v_edit_exploitation +AS SELECT exploitation.expl_id, + exploitation.name, + exploitation.macroexpl_id, + exploitation.descript, + exploitation.undelete, + exploitation.the_geom, + exploitation.tstamp, + exploitation.active + FROM selector_expl, + exploitation + WHERE exploitation.expl_id = selector_expl.expl_id AND selector_expl.cur_user = "current_user"()::text + and active is true; \ No newline at end of file diff --git a/updates/36/36017/ws/ddlview.sql b/updates/36/36017/ws/ddlview.sql index 9763dd6d3..927d39760 100644 --- a/updates/36/36017/ws/ddlview.sql +++ b/updates/36/36017/ws/ddlview.sql @@ -866,10 +866,35 @@ select distinct on (m.id) m.* from om_mincut m JOIN config_user_x_expl USING (expl_id) where username = current_user and m.id > 0; - CREATE OR REPLACE VIEW vu_sector as -SELECT distinct on (sector_id) s.sector_id, - s.name, +SELECT DISTINCT ON (s.sector_id) s.sector_id, + s.name, + s.macrosector_id, + m.name AS macrosector_name, + et.idval, + s.descript, + s.parent_id, + s.pattern_id, + s.graphconfig::text AS graphconfig, + s.stylesheet::text AS stylesheet, + s.link, + s.avg_press, + s.active, + s.undelete, + s.tstamp, + s.insert_user, + s.lastupdate, + s.lastupdate_user, + s.the_geom + FROM sector s + JOIN ( SELECT DISTINCT node.sector_id, node.expl_id FROM node WHERE node.state > 0) a USING (sector_id) + JOIN config_user_x_expl USING (expl_id) + LEFT JOIN macrosector m USING (macrosector_id) + LEFT JOIN edit_typevalue et ON et.id::text = s.sector_type::text AND et.typevalue::text = 'sector_type'::text + WHERE config_user_x_expl.username::text = CURRENT_USER AND s.sector_id > 0 + UNION + select s.sector_id, + s.name, s.macrosector_id, m.name AS macrosector_name, et.idval, @@ -888,12 +913,35 @@ SELECT distinct on (sector_id) s.sector_id, s.lastupdate_user, s.the_geom FROM sector s - JOIN (SELECT DISTINCT sector_id, expl_id FROM node WHERE state > 0) a USING (sector_id) - JOIN config_user_x_expl USING (expl_id) + LEFT JOIN ( SELECT DISTINCT node.sector_id, node.expl_id FROM node WHERE node.state > 0) a USING (sector_id) LEFT JOIN macrosector m USING (macrosector_id) LEFT JOIN edit_typevalue et ON et.id::text = s.sector_type::text AND et.typevalue::text = 'sector_type'::text - where username = current_user and s.active and sector_id > 0 - ORDER BY 1; - - - \ No newline at end of file + where s.sector_id > 0 and a.sector_id is null + ORDER BY 1; + + + CREATE OR REPLACE VIEW v_edit_sector +AS SELECT vu_sector.sector_id, + vu_sector.name, + vu_sector.macrosector_id, + vu_sector.macrosector_name, + vu_sector.idval, + vu_sector.descript, + vu_sector.parent_id, + vu_sector.pattern_id, + vu_sector.graphconfig, + vu_sector.stylesheet, + vu_sector.link, + vu_sector.avg_press, + vu_sector.active, + vu_sector.undelete, + vu_sector.tstamp, + vu_sector.insert_user, + vu_sector.lastupdate, + vu_sector.lastupdate_user, + vu_sector.the_geom + FROM vu_sector, + selector_sector + WHERE vu_sector.sector_id = selector_sector.sector_id AND selector_sector.cur_user = "current_user"()::text + and active is true; + \ No newline at end of file diff --git a/utils/fct/gw_fct_setselectors.sql b/utils/fct/gw_fct_setselectors.sql index 40bc1cdd5..161745ce6 100644 --- a/utils/fct/gw_fct_setselectors.sql +++ b/utils/fct/gw_fct_setselectors.sql @@ -161,13 +161,7 @@ BEGIN ON CONFLICT (',v_columnname,', cur_user) DO NOTHING;'); END IF; - ELSIF v_checkall IS FALSE THEN - EXECUTE 'DELETE FROM ' || v_tablename || ' WHERE cur_user = current_user'; - ELSE - EXECUTE 'DELETE FROM ' || v_tablename || ' WHERE cur_user = current_user'; - - -- manage isalone IF v_isalone THEN EXECUTE 'DELETE FROM ' || v_tablename || ' WHERE cur_user = current_user'; END IF; @@ -182,7 +176,6 @@ BEGIN ELSE EXECUTE 'DELETE FROM ' || v_tablename || ' WHERE ' || v_columnname || '::text = '''|| v_id ||''' AND cur_user = current_user'; END IF; - END IF; -- manage parents diff --git a/utils/ftrg/gw_trg_edit_exploitation.sql b/utils/ftrg/gw_trg_edit_exploitation.sql index a4cbae909..bde5eb537 100644 --- a/utils/ftrg/gw_trg_edit_exploitation.sql +++ b/utils/ftrg/gw_trg_edit_exploitation.sql @@ -6,8 +6,6 @@ This version of Giswater is provided by Giswater Association -- FUNCTION CODE: 2756 --- DROP FUNCTION "SCHEMA_NAME".gw_trg_edit_exploitation(); - CREATE OR REPLACE FUNCTION "SCHEMA_NAME".gw_trg_edit_exploitation() RETURNS trigger AS $BODY$ @@ -20,7 +18,7 @@ BEGIN IF TG_OP = 'INSERT' THEN INSERT INTO exploitation (expl_id, name, descript, macroexpl_id, the_geom, undelete, active) - VALUES (NEW.expl_id, NEW.name, NEW.descript, NEW.macroexpl_id, NEW.the_geom, NEW.undelete,NEW.active); + VALUES (NEW.expl_id, NEW.name, NEW.descript, NEW.macroexpl_id, NEW.the_geom, NEW.undelete, true); INSERT INTO selector_expl (expl_id, cur_user) VALUES (NEW.expl_id, current_user); RETURN NEW; diff --git a/utils/ftrg/gw_trg_exploitation.sql b/utils/ftrg/gw_trg_exploitation.sql index 06efb68d9..e4ae36016 100644 --- a/utils/ftrg/gw_trg_exploitation.sql +++ b/utils/ftrg/gw_trg_exploitation.sql @@ -24,35 +24,19 @@ BEGIN IF v_expl_x_user IS FALSE THEN IF TG_OP = 'INSERT' THEN + + -- for all users + INSERT INTO config_user_x_expl SELECT distinct ON (username) NEW.expl_id, username + from config_user_x_expl ON CONFLICT (expl_id, username) DO NOTHING; - IF NEW.active IS TRUE THEN - - -- for all users - INSERT INTO config_user_x_expl SELECT distinct ON (username) NEW.expl_id, username - from config_user_x_expl ON CONFLICT (expl_id, username) DO NOTHING; - - -- for this user (in case of does not exists) - INSERT INTO config_user_x_expl VALUES (NEW.expl_id, current_user) - ON CONFLICT (expl_id, username) DO NOTHING; - - END IF; + -- for this user (in case of does not exists) + INSERT INTO config_user_x_expl VALUES (NEW.expl_id, current_user) + ON CONFLICT (expl_id, username) DO NOTHING; RETURN NEW; ELSIF TG_OP = 'UPDATE' THEN - - IF NEW.active IS TRUE THEN - -- for this user (in case not exists) - INSERT INTO config_user_x_expl VALUES (NEW.expl_id, current_user) - ON CONFLICT (expl_id, username) DO NOTHING; - - ELSE - -- delete from config_user_x_expl - DELETE FROM config_user_x_expl WHERE expl_id = NEW.expl_id; - - END IF; - RETURN NEW; ELSIF TG_OP = 'DELETE' THEN diff --git a/ws/ftrg/ws_gw_trg_edit_sector.sql b/ws/ftrg/ws_gw_trg_edit_sector.sql index bf453c733..c24a78114 100644 --- a/ws/ftrg/ws_gw_trg_edit_sector.sql +++ b/ws/ftrg/ws_gw_trg_edit_sector.sql @@ -9,12 +9,8 @@ This version of Giswater is provided by Giswater Association CREATE OR REPLACE FUNCTION "SCHEMA_NAME".gw_trg_edit_sector() RETURNS trigger AS $BODY$ -DECLARE -v_querytext text; -v_row record; -v_sector_id integer[]; -v_user text; +DECLARE BEGIN @@ -22,112 +18,24 @@ BEGIN IF TG_OP = 'INSERT' THEN - NEW.active = TRUE; - - INSERT INTO sector (sector_id, name, muni_id, expl_id, descript, macrosector_id, the_geom, undelete, graphconfig, stylesheet, active, parent_id, pattern_id, avg_press) - VALUES (NEW.sector_id, NEW.name, NEW.muni_id, NEW.expl_id, NEW.descript, NEW.macrosector_id, NEW.the_geom, NEW.undelete, - NEW.graphconfig::json, NEW.stylesheet::json, NEW.active, NEW.parent_id, NEW.pattern_id, NEW.avg_press); + INSERT INTO sector (sector_id, name, descript, macrosector_id, the_geom, undelete, graphconfig, stylesheet, active, parent_id, pattern_id, avg_press) + VALUES (NEW.sector_id, NEW.name, NEW.descript, NEW.macrosector_id, NEW.the_geom, NEW.undelete, + NEW.graphconfig::json, NEW.stylesheet::json, true, NEW.parent_id, NEW.pattern_id, NEW.avg_press); + + INSERT INTO selector_sector VALUES (NEW.sector_id, current_user); + RETURN NEW; + ELSIF TG_OP = 'UPDATE' THEN - UPDATE sector - SET sector_id=NEW.sector_id, name=NEW.name, muni_id=NEW.muni_id, expl_id=NEW.expl_id, descript=NEW.descript, macrosector_id=NEW.macrosector_id, the_geom=NEW.the_geom, - undelete=NEW.undelete, graphconfig=NEW.graphconfig::json, stylesheet = NEW.stylesheet::json, active = NEW.active, parent_id = NEW.parent_id, pattern_id = NEW.pattern_id, - lastupdate=now(), lastupdate_user = current_user, avg_press = NEW.avg_press - WHERE sector_id=OLD.sector_id; - - ELSIF TG_OP = 'DELETE' THEN + RETURN NEW; + + ELSIF TG_OP = 'DELETE' THEN DELETE FROM sector WHERE sector_id = OLD.sector_id; - END IF; - - -- manage admin_exploitation_x_user - IF (SELECT value::boolean FROM config_param_system WHERE parameter = 'admin_exploitation_x_user') THEN - - IF TG_OP = 'INSERT' THEN - - IF NEW.parent_id IS NULL THEN - - IF (SELECT count(*) FROM selector_expl WHERE cur_user = current_user) = 1 THEN - NEW.parent_id = (SELECT expl_id FROM selector_expl WHERE cur_user = current_user LIMIT 1) ; - END IF; - - -- profilactic control in case of parent_id null - IF NEW.parent_id IS NULL THEN - INSERT INTO config_user_x_sector VALUES (NEW.sector_id, v_user); - INSERT INTO selector_sector VALUES (NEW.sector_id, v_user); - END IF; - END IF; - - IF NEW.parent_id IS NOT NULL THEN - - UPDATE sector SET parent_id = NEW.parent_id WHERE sector_id = NEW.sector_id; - - -- manage cat_manager - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a - WHERE sector_id = '||NEW.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - -- update array of sectors for specific row - v_sector_id = array_append (v_row.sector_id, NEW.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - - -- insert new sector on selector for all those user allowed on specific row - FOR v_user IN SELECT unnest(username) FROM cat_manager WHERE id = v_row.id - LOOP - INSERT INTO selector_sector VALUES (NEW.sector_id, v_user); - END LOOP; - END LOOP; - END IF; - - RETURN NEW; - - ELSIF TG_OP = 'UPDATE' THEN - - IF ((NEW.parent_id <> OLD.parent_id) OR (OLD.parent_id IS NULL AND NEW.parent_id IS NOT NULL)) AND (SELECT parent_id FROM sector WHERE parent_id = NEW.sector_id LIMIT 1) IS NULL THEN - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||OLD.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - raise notice 'v_row1 %', v_row; - v_sector_id = array_remove (v_row.sector_id, OLD.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||NEW.parent_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - raise notice 'v_row2 %', v_row; - v_sector_id = array_append (v_row.sector_id, NEW.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - END IF; - - RETURN NEW; - - ELSIF TG_OP = 'DELETE' THEN - - v_querytext = 'SELECT * FROM cat_manager WHERE id IN (SELECT id FROM (SELECT id, unnest(sector_id) sector_id FROM cat_manager)a WHERE sector_id = '||OLD.sector_id||')'; - FOR v_row IN EXECUTE v_querytext - LOOP - v_sector_id = array_remove (v_row.sector_id, OLD.sector_id); - UPDATE cat_manager SET sector_id = v_sector_id WHERE id = v_row.id; - END LOOP; - - RETURN NULL; - END IF; - ELSE - IF TG_OP ='INSERT'THEN - INSERT INTO selector_sector VALUES (NEW.sector_id, current_user); - RETURN NEW; + RETURN NEW; - ELSIF TG_OP ='UPDATE' THEN - RETURN NEW; - ELSE - RETURN NULL; - END IF; END IF; END; $BODY$