         "area_id_p" ISNULL AND
         "issue_id_p" NOTNULL
       THEN
+        SELECT INTO "issue_row" * FROM "issue" WHERE "id" = "issue_id_p";
+        IF "issue_row"."id" ISNULL THEN
+          RETURN;
+        END IF;
+        IF "issue_row"."closed" NOTNULL THEN
+          IF "simulate_trustee_id_p" NOTNULL THEN
+            RAISE EXCEPTION 'Tried to simulate delegation chain for closed issue.';
+          END IF;
+          FOR "output_row" IN
+            SELECT * FROM
+            "delegation_chain_for_closed_issue"("member_id_p", "issue_id_p")
+          LOOP
+            RETURN NEXT "output_row";
+          END LOOP;
+          RETURN;
+        END IF;
         "scope_v" := 'issue';
-        SELECT INTO "issue_row" * FROM "issue"
-          WHERE "id" = "issue_id_p";
         SELECT "area_id" INTO "area_id_v"
           FROM "issue" WHERE "id" = "issue_id_p";
         SELECT "unit_id" INTO "unit_id_v"




--------------------------------------------------
-- Set returning function for delegation chains --
--------------------------------------------------


CREATE TYPE "delegation_chain_loop_tag" AS ENUM

    "area"."id"%TYPE,
    "issue"."id"%TYPE,
    "member"."id"%TYPE )
  IS 'Shows a delegation chain for unit, area, or issue; See "delegation_chain_row" type for more information';



---------------------------------------------------------
-- Single row returning function for delegation chains --
---------------------------------------------------------


CREATE TYPE "delegation_info_loop_type" AS ENUM
  ('own', 'first', 'first_ellipsis', 'other', 'other_ellipsis');

CREATE TYPE "delegation_info_type" AS (
        "own_participation"           BOOLEAN,
        "own_delegation_scope"        "delegation_scope",
        "first_trustee_id"            INT4,
        "first_trustee_participation" BOOLEAN,
        "first_trustee_ellipsis"      BOOLEAN,
        "other_trustee_id"            INT4,
        "other_trustee_participation" BOOLEAN,
        "other_trustee_ellipsis"      BOOLEAN,
        "delegation_loop"             "delegation_info_loop_type");

CREATE FUNCTION "delegation_info"
  ( "member_id_p"           "member"."id"%TYPE,
    "unit_id_p"             "unit"."id"%TYPE,
    "area_id_p"             "area"."id"%TYPE,
    "issue_id_p"            "issue"."id"%TYPE,
    "simulate_trustee_id_p" "member"."id"%TYPE DEFAULT NULL )
  RETURNS "delegation_info_type"
  LANGUAGE 'plpgsql' STABLE AS $$
    DECLARE
      "current_row" "delegation_chain_row";
      "result"      "delegation_info_type";
    BEGIN
      "result"."own_participation" := FALSE;
      FOR "current_row" IN
        SELECT * FROM "delegation_chain"(
          "member_id_p",
          "unit_id_p", "area_id_p", "issue_id_p",
          "simulate_trustee_id_p")
      LOOP
        IF "current_row"."member_id" = "member_id_p" THEN
          "result"."own_participation"    := "current_row"."participation";
          "result"."own_delegation_scope" := "current_row"."scope_out";
          IF "current_row"."loop" = 'first' THEN
            "result"."delegation_loop" := 'own';
          END IF;
        ELSIF
          "current_row"."member_valid" AND
          ( "current_row"."loop" ISNULL OR
            "current_row"."loop" != 'repetition' )
        THEN
          IF "result"."first_trustee_id" ISNULL THEN
            "result"."first_trustee_id"            := "current_row"."member_id";
            "result"."first_trustee_participation" := "current_row"."participation";
            "result"."first_trustee_ellipsis"      := FALSE;
            IF "current_row"."loop" = 'first' THEN
              "result"."delegation_loop" := 'first';
            END IF;
          ELSIF "result"."other_trustee_id" ISNULL THEN
            IF "current_row"."participation" THEN
              "result"."other_trustee_id"            := "current_row"."member_id";
              "result"."other_trustee_participation" := TRUE;
              "result"."other_trustee_ellipsis"      := FALSE;
              IF "current_row"."loop" = 'first' THEN
                "result"."delegation_loop" := 'other';
              END IF;
            ELSE
              "result"."first_trustee_ellipsis" := TRUE;
              IF "current_row"."loop" = 'first' THEN
                "result"."delegation_loop" := 'first_ellipsis';
              END IF;
            END IF;
          ELSE
            "result"."other_trustee_ellipsis" := TRUE;
            IF "current_row"."loop" = 'first' THEN
              "result"."delegation_loop" := 'other_ellipsis';
            END IF;
          END IF;
        END IF;
      END LOOP;
      RETURN "result";
    END;
  $$;
