1 -- @tag: requirement_spec_items_update_trigger_fix2
2 -- @description: Fixes für Update-Trigger bei Pflichtenheften
3 -- @depends: requirement_spec_delete_trigger_fix
5 -- Trigger for updating time_estimation of function blocks from their
6 -- children. item_id is the ID of the item that needs to be updated
7 -- (or NULL if the requirement spec itself must be updated/a section
9 CREATE OR REPLACE FUNCTION update_requirement_spec_item_time_estimation(item_id INTEGER, item_requirement_spec_id INTEGER) RETURNS BOOLEAN AS $$
16 FROM trigger_information
17 WHERE ((key = 'deleting_requirement_spec_item') AND (value = CAST(item_id AS TEXT)))
18 OR ((key = 'deleting_requirement_spec') AND (value = CAST(item_requirement_spec_id AS TEXT)))
21 RAISE DEBUG 'updateRSIE: item_id % or requirement_spec_id % is about to be deleted; do not update', item_id, requirement_spec_id;
25 -- item_id IS NULL means that a section has been updated. The
26 -- requirement spec itself must therefore be updated.
27 IF item_id IS NULL THEN
28 SELECT COALESCE(time_estimation, 0) AS time_estimation
30 FROM requirement_specs
31 WHERE id = item_requirement_spec_id;
33 SELECT COALESCE(SUM(time_estimation), 0) AS time_estimation
35 FROM requirement_spec_items
36 WHERE (parent_id IS NULL)
37 AND (requirement_spec_id = item_requirement_spec_id);
39 IF current_row.time_estimation <> new_row.time_estimation THEN
40 RAISE DEBUG 'updateRSIE: updating requirement_spec % itself: old estimation % new %.', item_requirement_spec_id, current_row.time_estimation, new_row.time_estimation;
42 UPDATE requirement_specs
43 SET time_estimation = new_row.time_estimation
44 WHERE id = item_requirement_spec_id;
50 -- If we're here it means that either a sub-function-block or a
51 -- function-block has been updated. item_id is the parent's ID of
52 -- the updated item -- meaning the ID of the item that needs to be
55 SELECT COALESCE(time_estimation, 0) AS time_estimation
57 FROM requirement_spec_items
60 SELECT COALESCE(SUM(time_estimation), 0) AS time_estimation
62 FROM requirement_spec_items
63 WHERE (parent_id = item_id);
65 IF current_row.time_estimation = new_row.time_estimation THEN
66 RAISE DEBUG 'updateRSIE: item %: nothing to do', item_id;
70 RAISE DEBUG 'updateRSIE: updating item %: old estimation % new %.', item_id, current_row.time_estimation, new_row.time_estimation;
72 UPDATE requirement_spec_items
73 SET time_estimation = new_row.time_estimation
80 CREATE OR REPLACE FUNCTION recalculate_spec_item_time_estimation(the_requirement_spec_id INTEGER) RETURNS BOOLEAN AS $$
85 SELECT DISTINCT parent_id
86 FROM requirement_spec_items
87 WHERE (requirement_spec_id = the_requirement_spec_id)
88 AND (item_type = 'sub-function-block')
90 RAISE DEBUG 'hmm function-block with sub: %', item.parent_id;
91 PERFORM update_requirement_spec_item_time_estimation(item.parent_id, the_requirement_spec_id);
95 SELECT DISTINCT parent_id
96 FROM requirement_spec_items
97 WHERE (requirement_spec_id = the_requirement_spec_id)
98 AND (item_type = 'function-block')
101 FROM requirement_spec_items
102 WHERE (requirement_spec_id = the_requirement_spec_id)
103 AND (item_type = 'sub-function-block')
106 RAISE DEBUG 'hmm section with function-block: %', item.parent_id;
107 PERFORM update_requirement_spec_item_time_estimation(item.parent_id, the_requirement_spec_id);
110 PERFORM update_requirement_spec_item_time_estimation(NULL, the_requirement_spec_id);
116 CREATE OR REPLACE FUNCTION recalculate_all_spec_item_time_estimations() RETURNS BOOLEAN AS $$
120 FOR rspec IN SELECT id FROM requirement_specs LOOP
121 PERFORM recalculate_spec_item_time_estimation(rspec.id);
128 SELECT recalculate_all_spec_item_time_estimations();