CREATE OR REPLACE PACKAGE TRIGGER_FLAGS AS
  NAMESPACE CONSTANT VARCHAR2(30 BYTE) := 'TRIGGER_FLAGS';
  FUNCTION isEnabled(in_flag_name IN VARCHAR2) RETURN BOOLEAN;
  PROCEDURE enable(in_flag_name IN VARCHAR2);
  PROCEDURE disable(in_flag_name IN VARCHAR2);
  PROCEDURE register;
  PROCEDURE deregister;
END TRIGGER_FLAGS;
/

CREATE OR REPLACE PACKAGE BODY TRIGGER_FLAGS AS
  FUNCTION isEnabled(in_flag_name IN VARCHAR2) RETURN BOOLEAN AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN FALSE;
    END IF;

    RETURN SYS_CONTEXT(NAMESPACE, in_flag_name) = '1';
  END isEnabled;

  PROCEDURE enable(in_flag_name IN VARCHAR2) AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => in_flag_name,
      value => '1');
  END enable;

  PROCEDURE disable(in_flag_name IN VARCHAR2) AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => in_flag_name,
      value => '0');
  END disable;

  PROCEDURE register AS
  BEGIN
    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => 'STATE',
      value => 'REGISTERED');
  END register;

  PROCEDURE deregister AS
  BEGIN
    DBMS_SESSION.clear_all_context(
      namespace => NAMESPACE);
  END deregister;
END TRIGGER_FLAGS;
/

CREATE OR REPLACE PACKAGE CANCELLATION_TOKEN AS
  NAMESPACE CONSTANT VARCHAR2(30 BYTE) := 'CANCELLATION_TOKEN';
  FUNCTION isSignalled(in_token_name IN VARCHAR2) RETURN BOOLEAN;
  PROCEDURE wait_for(in_token_name IN VARCHAR2);
  PROCEDURE clear(in_token_name IN VARCHAR2);
  PROCEDURE signal(in_token_name IN VARCHAR2);
  PROCEDURE register;
  PROCEDURE deregister;
END CANCELLATION_TOKEN;
/

CREATE OR REPLACE PACKAGE BODY CANCELLATION_TOKEN AS
  FUNCTION isSignalled(in_token_name IN VARCHAR2) RETURN BOOLEAN AS
  BEGIN
    IF in_token_name = 'STATE' THEN
      RETURN FALSE;
    END IF;

    RETURN SYS_CONTEXT(NAMESPACE, in_token_name) = '1';
  END isSignalled;

  PROCEDURE wait_for(in_token_name IN VARCHAR2) AS
  BEGIN
    IF in_token_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => in_token_name,
      value => '0');
  END wait_for;

  PROCEDURE clear(in_token_name IN VARCHAR2) AS
  BEGIN
    IF in_token_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.clear_context(
      namespace => NAMESPACE,
      attribute => in_token_name);
  END clear;

  PROCEDURE signal(in_token_name IN VARCHAR2) AS
  BEGIN
    IF in_token_name = 'STATE' THEN
      RETURN;
    END IF;

    IF SYS_CONTEXT(NAMESPACE, in_token_name) = '0' THEN
      DBMS_SESSION.set_context(
        namespace => NAMESPACE,
        attribute => in_token_name,
        value => '1');
    END IF;
  END signal;

  PROCEDURE register AS
  BEGIN
    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => 'STATE',
      value => 'REGISTERED');
  END register;

  PROCEDURE deregister AS
  BEGIN
    DBMS_SESSION.clear_all_context(
      namespace => NAMESPACE);
  END deregister;
END CANCELLATION_TOKEN;
/

CREATE OR REPLACE CONTEXT TRIGGER_FLAGS USING TRIGGER_FLAGS;

CREATE OR REPLACE CONTEXT CANCELLATION_TOKEN USING CANCELLATION_TOKEN ACCESSED GLOBALLY;

BEGIN
  TRIGGER_FLAGS.deregister;
  CANCELLATION_TOKEN.deregister;
END;
/

CREATE OR REPLACE TRIGGER DB_LOGON
AFTER LOGON ON DATABASE
BEGIN
  CANCELLATION_TOKEN.register;
  TRIGGER_FLAGS.register;
  
  TRIGGER_FLAGS.enable('FAST_SCHEMADBLINK_TRG');
  TRIGGER_FLAGS.enable('FAST_SCHEMAELEMENT_TRG');

  TRIGGER_FLAGS.enable('ADRES_AUDIT_AD');
  TRIGGER_FLAGS.enable('ADRES_AUDIT_AI');
  TRIGGER_FLAGS.enable('ADRES_AUDIT_AU');
  TRIGGER_FLAGS.enable('ADRES_BEFORE_UPDATE');
  TRIGGER_FLAGS.enable('ADRES_SETID');
  TRIGGER_FLAGS.enable('ADRES_SPEED_AIUD');
  TRIGGER_FLAGS.enable('ADRES_WEZEL_AD');
  TRIGGER_FLAGS.enable('ADRES_WEZEL_AI');
  TRIGGER_FLAGS.enable('PASSPORT_198_DETAILS_AIUD');
  
  TRIGGER_FLAGS.enable('WEZEL_AUDIT_AD');
  TRIGGER_FLAGS.enable('WEZEL_AUDIT_AI');
  TRIGGER_FLAGS.enable('WEZEL_AUDIT_AU');
EXCEPTION
  WHEN OTHERS THEN
    NULL;
END;
/

CREATE OR REPLACE PACKAGE TRIGGER_FLAGS AS
  NAMESPACE CONSTANT VARCHAR2(30 BYTE) := 'TRIGGER_FLAGS';
  FUNCTION isDisabled(in_flag_name IN VARCHAR2) RETURN BOOLEAN;
  PROCEDURE enable(in_flag_name IN VARCHAR2);
  PROCEDURE disable(in_flag_name IN VARCHAR2);
  PROCEDURE register;
  PROCEDURE deregister;
END TRIGGER_FLAGS;
/

CREATE OR REPLACE PACKAGE BODY TRIGGER_FLAGS AS
  FUNCTION isDisabled(in_flag_name IN VARCHAR2) RETURN BOOLEAN AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN FALSE;
    END IF;

    RETURN SYS_CONTEXT(NAMESPACE, in_flag_name) = '0';
  END isDisabled;

  PROCEDURE enable(in_flag_name IN VARCHAR2) AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => in_flag_name,
      value => '1');
  END enable;

  PROCEDURE disable(in_flag_name IN VARCHAR2) AS
  BEGIN
    IF in_flag_name = 'STATE' THEN
      RETURN;
    END IF;

    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => in_flag_name,
      value => '0');
  END disable;

  PROCEDURE register AS
  BEGIN
    DBMS_SESSION.set_context(
      namespace => NAMESPACE,
      attribute => 'STATE',
      value => 'REGISTERED');
  END register;

  PROCEDURE deregister AS
  BEGIN
    DBMS_SESSION.clear_all_context(
      namespace => NAMESPACE);
  END deregister;
END TRIGGER_FLAGS;
/


CREATE OR REPLACE TRIGGER PST_AFTER_LOGON
AFTER LOGON ON SCHEMA
BEGIN
  CANCELLATION_TOKEN.register;
  TRIGGER_FLAGS.register;
EXCEPTION
  WHEN OTHERS THEN
    NULL;
END;
/

ALTER TABLE FAST_SCHEMAELEMENT 
ADD (DELETED_BY VARCHAR2(100) );

ALTER TABLE FAST_SCHEMAELEMENT 
ADD (DELETED_FROM_HOST VARCHAR2(100) );

ALTER TABLE FAST_SCHEMAELEMENT 
ADD (DELETED_FROM_APP VARCHAR2(100) );

EXIT;
