
jbe@244: BEGIN;
jbe@244: 
jbe@244: CREATE OR REPLACE VIEW "liquid_feedback_version" AS
jbe@244:   SELECT * FROM (VALUES ('2.0.9', 2, 0, 9))
jbe@244:   AS "subquery"("string", "major", "minor", "revision");
jbe@244: 
jbe@244: COMMENT ON TYPE "delegation_chain_row" IS 'Type of rows returned by "delegation_chain" function';
jbe@244: 
jbe@244: CREATE FUNCTION "delegation_chain_for_closed_issue"
jbe@244:   ( "member_id_p"           "member"."id"%TYPE,
jbe@244:     "issue_id_p"            "issue"."id"%TYPE )
jbe@244:   RETURNS SETOF "delegation_chain_row"
jbe@244:   LANGUAGE 'plpgsql' STABLE AS $$
jbe@244:     DECLARE
jbe@244:       "output_row"           "delegation_chain_row";
jbe@244:       "direct_voter_row"     "direct_voter"%ROWTYPE;
jbe@244:       "delegating_voter_row" "delegating_voter"%ROWTYPE;
jbe@244:     BEGIN
jbe@244:       "output_row"."index"         := 0;
jbe@244:       "output_row"."member_id"     := "member_id_p";
jbe@244:       "output_row"."member_valid"  := TRUE;
jbe@244:       "output_row"."participation" := FALSE;
jbe@244:       "output_row"."overridden"    := FALSE;
jbe@244:       "output_row"."disabled_out"  := FALSE;
jbe@244:       LOOP
jbe@244:         SELECT INTO "direct_voter_row" * FROM "direct_voter"
jbe@244:           WHERE "issue_id" = "issue_id_p"
jbe@244:           AND "member_id" = "output_row"."member_id";
jbe@244:         IF "direct_voter_row"."member_id" NOTNULL THEN
jbe@244:           "output_row"."participation" := TRUE;
jbe@244:           "output_row"."scope_out"     := NULL;
jbe@244:           "output_row"."disabled_out"  := NULL;
jbe@244:           RETURN NEXT "output_row";
jbe@244:           RETURN;
jbe@244:         END IF;
jbe@244:         SELECT INTO "delegating_voter_row" * FROM "delegating_voter"
jbe@244:           WHERE "issue_id" = "issue_id_p"
jbe@244:           AND "member_id" = "output_row"."member_id";
jbe@244:         IF "delegating_voter_row"."member_id" ISNULL THEN
jbe@244:           RETURN;
jbe@244:         END IF;
jbe@244:         "output_row"."scope_out" := "delegating_voter_row"."scope";
jbe@244:         RETURN NEXT "output_row";
jbe@244:         "output_row"."member_id" := "delegating_voter_row"."delegate_member_ids"[1];
jbe@244:         "output_row"."scope_in"  := "output_row"."scope_out";
jbe@244:       END LOOP;
jbe@244:     END;
jbe@244:   $$;
jbe@244: 
jbe@244: COMMENT ON FUNCTION "delegation_chain_for_closed_issue"
jbe@244:   ( "member"."id"%TYPE,
jbe@244:     "member"."id"%TYPE )
jbe@244:   IS 'Helper function for "delegation_chain" function, handling the special case of closed issues after voting';
jbe@244: 
jbe@244: DROP FUNCTION "delegation_chain"
jbe@244:   ( "member"."id"%TYPE,
jbe@244:     "unit"."id"%TYPE,
jbe@244:     "area"."id"%TYPE,
jbe@244:     "issue"."id"%TYPE );
jbe@244: 
jbe@244: DROP FUNCTION "delegation_chain"
jbe@244:   ( "member"."id"%TYPE,
jbe@244:     "unit"."id"%TYPE,
jbe@244:     "area"."id"%TYPE,
jbe@244:     "issue"."id"%TYPE,
jbe@244:     "member"."id"%TYPE );
jbe@244: 
jbe@244: CREATE FUNCTION "delegation_chain"
jbe@244:   ( "member_id_p"           "member"."id"%TYPE,
jbe@244:     "unit_id_p"             "unit"."id"%TYPE,
jbe@244:     "area_id_p"             "area"."id"%TYPE,
jbe@244:     "issue_id_p"            "issue"."id"%TYPE,
jbe@244:     "simulate_trustee_id_p" "member"."id"%TYPE DEFAULT NULL )
jbe@244:   RETURNS SETOF "delegation_chain_row"
jbe@244:   LANGUAGE 'plpgsql' STABLE AS $$
jbe@244:     DECLARE
jbe@244:       "scope_v"            "delegation_scope";
jbe@244:       "unit_id_v"          "unit"."id"%TYPE;
jbe@244:       "area_id_v"          "area"."id"%TYPE;
jbe@244:       "issue_row"          "issue"%ROWTYPE;
jbe@244:       "visited_member_ids" INT4[];  -- "member"."id"%TYPE[]
jbe@244:       "loop_member_id_v"   "member"."id"%TYPE;
jbe@244:       "output_row"         "delegation_chain_row";
jbe@244:       "output_rows"        "delegation_chain_row"[];
jbe@244:       "delegation_row"     "delegation"%ROWTYPE;
jbe@244:       "row_count"          INT4;
jbe@244:       "i"                  INT4;
jbe@244:       "loop_v"             BOOLEAN;
jbe@244:     BEGIN
jbe@244:       IF
jbe@244:         "unit_id_p" NOTNULL AND
jbe@244:         "area_id_p" ISNULL AND
jbe@244:         "issue_id_p" ISNULL
jbe@244:       THEN
jbe@244:         "scope_v" := 'unit';
jbe@244:         "unit_id_v" := "unit_id_p";
jbe@244:       ELSIF
jbe@244:         "unit_id_p" ISNULL AND
jbe@244:         "area_id_p" NOTNULL AND
jbe@244:         "issue_id_p" ISNULL
jbe@244:       THEN
jbe@244:         "scope_v" := 'area';
jbe@244:         "area_id_v" := "area_id_p";
jbe@244:         SELECT "unit_id" INTO "unit_id_v"
jbe@244:           FROM "area" WHERE "id" = "area_id_v";
jbe@244:       ELSIF
jbe@244:         "unit_id_p" ISNULL AND
jbe@244:         "area_id_p" ISNULL AND
jbe@244:         "issue_id_p" NOTNULL
jbe@244:       THEN
jbe@244:         SELECT INTO "issue_row" * FROM "issue" WHERE "id" = "issue_id_p";
jbe@244:         IF "issue_row"."id" ISNULL THEN
jbe@244:           RETURN;
jbe@244:         END IF;
jbe@244:         IF "issue_row"."closed" NOTNULL THEN
jbe@244:           IF "simulate_trustee_id_p" NOTNULL THEN
jbe@244:             RAISE EXCEPTION 'Tried to simulate delegation chain for closed issue.';
jbe@244:           END IF;
jbe@244:           FOR "output_row" IN
jbe@244:             SELECT * FROM
jbe@244:             "delegation_chain_for_closed_issue"("member_id_p", "issue_id_p")
jbe@244:           LOOP
jbe@244:             RETURN NEXT "output_row";
jbe@244:           END LOOP;
jbe@244:           RETURN;
jbe@244:         END IF;
jbe@244:         "scope_v" := 'issue';
jbe@244:         SELECT "area_id" INTO "area_id_v"
jbe@244:           FROM "issue" WHERE "id" = "issue_id_p";
jbe@244:         SELECT "unit_id" INTO "unit_id_v"
jbe@244:           FROM "area"  WHERE "id" = "area_id_v";
jbe@244:       ELSE
jbe@244:         RAISE EXCEPTION 'Exactly one of unit_id_p, area_id_p, or issue_id_p must be NOTNULL.';
jbe@244:       END IF;
jbe@244:       "visited_member_ids" := '{}';
jbe@244:       "loop_member_id_v"   := NULL;
jbe@244:       "output_rows"        := '{}';
jbe@244:       "output_row"."index"         := 0;
jbe@244:       "output_row"."member_id"     := "member_id_p";
jbe@244:       "output_row"."member_valid"  := TRUE;
jbe@244:       "output_row"."participation" := FALSE;
jbe@244:       "output_row"."overridden"    := FALSE;
jbe@244:       "output_row"."disabled_out"  := FALSE;
jbe@244:       "output_row"."scope_out"     := NULL;
jbe@244:       LOOP
jbe@244:         IF "visited_member_ids" @> ARRAY["output_row"."member_id"] THEN
jbe@244:           "loop_member_id_v" := "output_row"."member_id";
jbe@244:         ELSE
jbe@244:           "visited_member_ids" :=
jbe@244:             "visited_member_ids" || "output_row"."member_id";
jbe@244:         END IF;
jbe@244:         IF "output_row"."participation" ISNULL THEN
jbe@244:           "output_row"."overridden" := NULL;
jbe@244:         ELSIF "output_row"."participation" THEN
jbe@244:           "output_row"."overridden" := TRUE;
jbe@244:         END IF;
jbe@244:         "output_row"."scope_in" := "output_row"."scope_out";
jbe@244:         IF EXISTS (
jbe@244:           SELECT NULL FROM "member" JOIN "privilege"
jbe@244:           ON "privilege"."member_id" = "member"."id"
jbe@244:           AND "privilege"."unit_id" = "unit_id_v"
jbe@244:           WHERE "id" = "output_row"."member_id"
jbe@244:           AND "member"."active" AND "privilege"."voting_right"
jbe@244:         ) THEN
jbe@244:           IF "scope_v" = 'unit' THEN
jbe@244:             SELECT * INTO "delegation_row" FROM "delegation"
jbe@244:               WHERE "truster_id" = "output_row"."member_id"
jbe@244:               AND "unit_id" = "unit_id_v";
jbe@244:           ELSIF "scope_v" = 'area' THEN
jbe@244:             "output_row"."participation" := EXISTS (
jbe@244:               SELECT NULL FROM "membership"
jbe@244:               WHERE "area_id" = "area_id_p"
jbe@244:               AND "member_id" = "output_row"."member_id"
jbe@244:             );
jbe@244:             SELECT * INTO "delegation_row" FROM "delegation"
jbe@244:               WHERE "truster_id" = "output_row"."member_id"
jbe@244:               AND (
jbe@244:                 "unit_id" = "unit_id_v" OR
jbe@244:                 "area_id" = "area_id_v"
jbe@244:               )
jbe@244:               ORDER BY "scope" DESC;
jbe@244:           ELSIF "scope_v" = 'issue' THEN
jbe@244:             IF "issue_row"."fully_frozen" ISNULL THEN
jbe@244:               "output_row"."participation" := EXISTS (
jbe@244:                 SELECT NULL FROM "interest"
jbe@244:                 WHERE "issue_id" = "issue_id_p"
jbe@244:                 AND "member_id" = "output_row"."member_id"
jbe@244:               );
jbe@244:             ELSE
jbe@244:               IF "output_row"."member_id" = "member_id_p" THEN
jbe@244:                 "output_row"."participation" := EXISTS (
jbe@244:                   SELECT NULL FROM "direct_voter"
jbe@244:                   WHERE "issue_id" = "issue_id_p"
jbe@244:                   AND "member_id" = "output_row"."member_id"
jbe@244:                 );
jbe@244:               ELSE
jbe@244:                 "output_row"."participation" := NULL;
jbe@244:               END IF;
jbe@244:             END IF;
jbe@244:             SELECT * INTO "delegation_row" FROM "delegation"
jbe@244:               WHERE "truster_id" = "output_row"."member_id"
jbe@244:               AND (
jbe@244:                 "unit_id" = "unit_id_v" OR
jbe@244:                 "area_id" = "area_id_v" OR
jbe@244:                 "issue_id" = "issue_id_p"
jbe@244:               )
jbe@244:               ORDER BY "scope" DESC;
jbe@244:           END IF;
jbe@244:         ELSE
jbe@244:           "output_row"."member_valid"  := FALSE;
jbe@244:           "output_row"."participation" := FALSE;
jbe@244:           "output_row"."scope_out"     := NULL;
jbe@244:           "delegation_row" := ROW(NULL);
jbe@244:         END IF;
jbe@244:         IF
jbe@244:           "output_row"."member_id" = "member_id_p" AND
jbe@244:           "simulate_trustee_id_p" NOTNULL
jbe@244:         THEN
jbe@244:           "output_row"."scope_out" := "scope_v";
jbe@244:           "output_rows" := "output_rows" || "output_row";
jbe@244:           "output_row"."member_id" := "simulate_trustee_id_p";
jbe@244:         ELSIF "delegation_row"."trustee_id" NOTNULL THEN
jbe@244:           "output_row"."scope_out" := "delegation_row"."scope";
jbe@244:           "output_rows" := "output_rows" || "output_row";
jbe@244:           "output_row"."member_id" := "delegation_row"."trustee_id";
jbe@244:         ELSIF "delegation_row"."scope" NOTNULL THEN
jbe@244:           "output_row"."scope_out" := "delegation_row"."scope";
jbe@244:           "output_row"."disabled_out" := TRUE;
jbe@244:           "output_rows" := "output_rows" || "output_row";
jbe@244:           EXIT;
jbe@244:         ELSE
jbe@244:           "output_row"."scope_out" := NULL;
jbe@244:           "output_rows" := "output_rows" || "output_row";
jbe@244:           EXIT;
jbe@244:         END IF;
jbe@244:         EXIT WHEN "loop_member_id_v" NOTNULL;
jbe@244:         "output_row"."index" := "output_row"."index" + 1;
jbe@244:       END LOOP;
jbe@244:       "row_count" := array_upper("output_rows", 1);
jbe@244:       "i"      := 1;
jbe@244:       "loop_v" := FALSE;
jbe@244:       LOOP
jbe@244:         "output_row" := "output_rows"["i"];
jbe@244:         EXIT WHEN "output_row" ISNULL;  -- NOTE: ISNULL and NOT ... NOTNULL produce different results!
jbe@244:         IF "loop_v" THEN
jbe@244:           IF "i" + 1 = "row_count" THEN
jbe@244:             "output_row"."loop" := 'last';
jbe@244:           ELSIF "i" = "row_count" THEN
jbe@244:             "output_row"."loop" := 'repetition';
jbe@244:           ELSE
jbe@244:             "output_row"."loop" := 'intermediate';
jbe@244:           END IF;
jbe@244:         ELSIF "output_row"."member_id" = "loop_member_id_v" THEN
jbe@244:           "output_row"."loop" := 'first';
jbe@244:           "loop_v" := TRUE;
jbe@244:         END IF;
jbe@244:         IF "scope_v" = 'unit' THEN
jbe@244:           "output_row"."participation" := NULL;
jbe@244:         END IF;
jbe@244:         RETURN NEXT "output_row";
jbe@244:         "i" := "i" + 1;
jbe@244:       END LOOP;
jbe@244:       RETURN;
jbe@244:     END;
jbe@244:   $$;
jbe@244: 
jbe@244: COMMENT ON FUNCTION "delegation_chain"
jbe@244:   ( "member"."id"%TYPE,
jbe@244:     "unit"."id"%TYPE,
jbe@244:     "area"."id"%TYPE,
jbe@244:     "issue"."id"%TYPE,
jbe@244:     "member"."id"%TYPE )
jbe@244:   IS 'Shows a delegation chain for unit, area, or issue; See "delegation_chain_row" type for more information';
jbe@244: 
jbe@244: CREATE TYPE "delegation_info_loop_type" AS ENUM
jbe@244:   ('own', 'first', 'first_ellipsis', 'other', 'other_ellipsis');
jbe@244: 
jbe@244: COMMENT ON TYPE "delegation_info_loop_type" IS 'Type of "delegation_loop" in "delegation_info_type"; ''own'' means loop to self, ''first'' means loop to first trustee, ''first_ellipsis'' means loop to ellipsis after first trustee, ''other'' means loop to other trustee, ''other_ellipsis'' means loop to ellipsis after other trustee''';
jbe@244: 
jbe@244: CREATE TYPE "delegation_info_type" AS (
jbe@244:         "own_participation"           BOOLEAN,
jbe@244:         "own_delegation_scope"        "delegation_scope",
jbe@244:         "first_trustee_id"            INT4,
jbe@244:         "first_trustee_participation" BOOLEAN,
jbe@244:         "first_trustee_ellipsis"      BOOLEAN,
jbe@244:         "other_trustee_id"            INT4,
jbe@244:         "other_trustee_participation" BOOLEAN,
jbe@244:         "other_trustee_ellipsis"      BOOLEAN,
jbe@244:         "delegation_loop"             "delegation_info_loop_type");
jbe@244: 
jbe@244: COMMENT ON TYPE "delegation_info_type" IS 'Type of result returned by "delegation_info" function; For meaning of "participation" check comment on "delegation_chain_row" type';
jbe@244: 
jbe@244: COMMENT ON COLUMN "delegation_info_type"."own_participation"           IS 'Member is directly participating';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."own_delegation_scope"        IS 'Delegation scope of member';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."first_trustee_id"            IS 'Direct trustee of member';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."first_trustee_participation" IS 'Direct trustee of member is participating';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."first_trustee_ellipsis"      IS 'Ellipsis in delegation chain after "first_trustee"';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."other_trustee_id"            IS 'Another relevant trustee (due to participation)';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."other_trustee_participation" IS 'Another trustee is participating (redundant field: if "other_trustee_id" is set, then "other_trustee_participation" is always TRUE, else "other_trustee_participation" is NULL)';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."other_trustee_ellipsis"      IS 'Ellipsis in delegation chain after "other_trustee"';
jbe@244: COMMENT ON COLUMN "delegation_info_type"."delegation_loop"             IS 'Non-NULL value, if delegation chain contains a circle; See comment on "delegation_info_loop_type" for details';
jbe@244: 
jbe@244: CREATE FUNCTION "delegation_info"
jbe@244:   ( "member_id_p"           "member"."id"%TYPE,
jbe@244:     "unit_id_p"             "unit"."id"%TYPE,
jbe@244:     "area_id_p"             "area"."id"%TYPE,
jbe@244:     "issue_id_p"            "issue"."id"%TYPE,
jbe@244:     "simulate_trustee_id_p" "member"."id"%TYPE DEFAULT NULL )
jbe@244:   RETURNS "delegation_info_type"
jbe@244:   LANGUAGE 'plpgsql' STABLE AS $$
jbe@244:     DECLARE
jbe@244:       "current_row" "delegation_chain_row";
jbe@244:       "result"      "delegation_info_type";
jbe@244:     BEGIN
jbe@244:       "result"."own_participation" := FALSE;
jbe@244:       FOR "current_row" IN
jbe@244:         SELECT * FROM "delegation_chain"(
jbe@244:           "member_id_p",
jbe@244:           "unit_id_p", "area_id_p", "issue_id_p",
jbe@244:           "simulate_trustee_id_p")
jbe@244:       LOOP
jbe@244:         IF "current_row"."member_id" = "member_id_p" THEN
jbe@244:           "result"."own_participation"    := "current_row"."participation";
jbe@244:           "result"."own_delegation_scope" := "current_row"."scope_out";
jbe@244:           IF "current_row"."loop" = 'first' THEN
jbe@244:             "result"."delegation_loop" := 'own';
jbe@244:           END IF;
jbe@244:         ELSIF
jbe@244:           "current_row"."member_valid" AND
jbe@244:           ( "current_row"."loop" ISNULL OR
jbe@244:             "current_row"."loop" != 'repetition' )
jbe@244:         THEN
jbe@244:           IF "result"."first_trustee_id" ISNULL THEN
jbe@244:             "result"."first_trustee_id"            := "current_row"."member_id";
jbe@244:             "result"."first_trustee_participation" := "current_row"."participation";
jbe@244:             "result"."first_trustee_ellipsis"      := FALSE;
jbe@244:             IF "current_row"."loop" = 'first' THEN
jbe@244:               "result"."delegation_loop" := 'first';
jbe@244:             END IF;
jbe@244:           ELSIF "result"."other_trustee_id" ISNULL THEN
jbe@244:             IF "current_row"."participation" THEN
jbe@244:               "result"."other_trustee_id"            := "current_row"."member_id";
jbe@244:               "result"."other_trustee_participation" := TRUE;
jbe@244:               "result"."other_trustee_ellipsis"      := FALSE;
jbe@244:               IF "current_row"."loop" = 'first' THEN
jbe@244:                 "result"."delegation_loop" := 'other';
jbe@244:               END IF;
jbe@244:             ELSE
jbe@244:               "result"."first_trustee_ellipsis" := TRUE;
jbe@244:               IF "current_row"."loop" = 'first' THEN
jbe@244:                 "result"."delegation_loop" := 'first_ellipsis';
jbe@244:               END IF;
jbe@244:             END IF;
jbe@244:           ELSE
jbe@244:             "result"."other_trustee_ellipsis" := TRUE;
jbe@244:             IF "current_row"."loop" = 'first' THEN
jbe@244:               "result"."delegation_loop" := 'other_ellipsis';
jbe@244:             END IF;
jbe@244:           END IF;
jbe@244:         END IF;
jbe@244:       END LOOP;
jbe@244:       RETURN "result";
jbe@244:     END;
jbe@244:   $$;
jbe@244: 
jbe@244: COMMENT ON FUNCTION "delegation_info"
jbe@244:   ( "member"."id"%TYPE,
jbe@244:     "unit"."id"%TYPE,
jbe@244:     "area"."id"%TYPE,
jbe@244:     "issue"."id"%TYPE,
jbe@244:     "member"."id"%TYPE )
jbe@244:   IS 'Notable information about a delegation chain for unit, area, or issue; See "delegation_info_type" for more information';
jbe@244: 
jbe@244: COMMIT;




