Skip to content

Commit

Permalink
fix(from prev commit): Various fix and improvemts from prev commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xtorret committed Dec 31, 2024
1 parent 6ce3714 commit 7f1b954
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 247 deletions.
100 changes: 9 additions & 91 deletions ud/ftrg/ud_gw_trg_edit_sector.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
45 changes: 41 additions & 4 deletions updates/36/36017/ud/ddlview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;


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;
38 changes: 33 additions & 5 deletions updates/36/36017/utils/ddlview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
68 changes: 58 additions & 10 deletions updates/36/36017/ws/ddlview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;



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;

7 changes: 0 additions & 7 deletions utils/fct/gw_fct_setselectors.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions utils/ftrg/gw_trg_edit_exploitation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand All @@ -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;

Expand Down
Loading

0 comments on commit 7f1b954

Please sign in to comment.