/*
 *  $Revision$
 *  $Id$
 *
 */

CREATE_TRIGGER(fac_t_fac_functie_B_I)
BEFORE INSERT ON fac_functie
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_functie_key, fac_s_fac_functie_key);
   :new.fac_functie_volgnr :=
          CASE
             WHEN SUBSTR (:new.fac_functie_code, 8, 3) IN ('USE', 'FOF', 'BOF')
                  AND SUBSTR (:new.fac_functie_code, 5, 3) <> 'UUR'
             THEN 250
             WHEN SUBSTR (:new.fac_functie_code, 8, 3) IN ('BAC', 'MAN')
             THEN 500
             WHEN SUBSTR (:new.fac_functie_code, 8, 3) IN ('BO2', 'BO3', 'AFR', 'NOS', 'OAP')
             THEN 750
          END;
END;
/

CREATE_TRIGGER(fac_t_fac_groep_B_IU)
BEFORE INSERT OR UPDATE ON fac_groep
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_groep_key, fac_s_fac_groep_key);
    :new.fac_groep_upper := UPPER(:new.fac_groep_omschrijving);
END;
/

CREATE_TRIGGER(fac_t_fac_gebruikersgroep_B_IU)
BEFORE INSERT OR UPDATE ON fac_gebruikersgroep
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_gebruikersgroep_key, fac_s_fac_gebruikersgroep_key);
END;
/

AUDIT_BEGIN(fac_gebruikersgroep)
AUDIT_VALUE(fac_gebruikersgroep, fac_groep_key)
AUDIT_VALUE(fac_gebruikersgroep, prs_perslid_key)
AUDIT_END()

CREATE_TRIGGER(fac_t_fac_email_setting_B_IU)
BEFORE INSERT OR UPDATE ON fac_email_setting
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_email_setting_key, fac_s_fac_email_setting_key);
END;
/

AUDIT_BEGIN(fac_groeprechten)
AUDIT_VALUE(fac_groeprechten, fac_groep_key)
AUDIT_VALUE(fac_groeprechten, fac_functie_key)
AUDIT_VALUE(fac_groeprechten, ins_discipline_key)
AUDIT_VALUE(fac_groeprechten, fac_gebruiker_alg_level_read)
AUDIT_VALUE(fac_groeprechten, fac_gebruiker_prs_level_read)
AUDIT_VALUE(fac_groeprechten, fac_gebruiker_alg_level_write)
AUDIT_VALUE(fac_groeprechten, fac_gebruiker_prs_level_write)
AUDIT_END()

CREATE_TRIGGER(fac_t_fac_groeprechten_B_IU)
BEFORE INSERT OR UPDATE ON fac_groeprechten
FOR EACH ROW
DECLARE
  Dummy BOOLEAN;
  keepit NUMBER;
  funclevel NUMBER;
BEGIN
  -- We need a key, even if we decide to remove this record later
    UPDATE_PRIMARY_KEY(fac_groeprechten_key, fac_s_fac_groeprechten_key);

  -- Determine whether we really need to store these values
    /* see web_ini for meaning of this bitwise value */

  -- Note: the ASP-code does all the checking and fixing
  -- This is not very necessary anymore, and does not really help
  -- However: if it ain't broke, we don't fix it.
  SELECT fac_functie_min_level
    INTO funclevel
    FROM fac_functie
   WHERE fac_functie_key=:new.fac_functie_key;

  keepit:=0;
  --Keep if any used property has not-a-9 value; unused properties are discarded
  IF funclevel=0 AND  -- all values will be equal by definition here
        :new.fac_gebruiker_prs_level_read <> 9
     AND :new.fac_gebruiker_prs_level_write <> 9
     AND :new.fac_gebruiker_alg_level_read <> 9
     AND :new.fac_gebruiker_alg_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=1 AND -- read
         :new.fac_gebruiker_prs_level_read <> 9
     AND :new.fac_gebruiker_alg_level_read <> 9
        THEN keepit:=1;
  ELSIF funclevel=2 AND -- write
         :new.fac_gebruiker_prs_level_write <> 9
     AND :new.fac_gebruiker_alg_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=3 AND -- read or write
         ((:new.fac_gebruiker_prs_level_read <> 9
      AND :new.fac_gebruiker_alg_level_read <> 9) OR
         (:new.fac_gebruiker_prs_level_write <> 9
      AND :new.fac_gebruiker_alg_level_write <> 9))
        THEN keepit:=1;
  ELSIF funclevel=4 AND -- unused
         :new.fac_gebruiker_prs_level_read <> 9
     AND :new.fac_gebruiker_prs_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=5 AND -- prs read
         :new.fac_gebruiker_prs_level_read <> 9
        THEN keepit:=1;
  ELSIF funclevel=6 AND -- prs write, probably unused
         :new.fac_gebruiker_prs_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=7 AND -- prs read n write
         (:new.fac_gebruiker_prs_level_read <> 9 OR
         :new.fac_gebruiker_prs_level_write <> 9)
        THEN keepit:=1;
  ELSIF funclevel=8 AND -- unused
         :new.fac_gebruiker_alg_level_read <> 9
     AND :new.fac_gebruiker_alg_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=9 AND -- alg read
         :new.fac_gebruiker_alg_level_read <> 9
        THEN keepit:=1;
  ELSIF funclevel=10 AND -- alg write, probably unused
         :new.fac_gebruiker_alg_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=11 AND -- alg read n write
         (:new.fac_gebruiker_alg_level_read <> 9 OR
         :new.fac_gebruiker_alg_level_write <> 9)
        THEN keepit:=1;
  ELSIF funclevel=12 AND -- unused
        :new.fac_gebruiker_prs_level_read <> 9
     AND :new.fac_gebruiker_prs_level_write <> 9
     AND :new.fac_gebruiker_alg_level_read <> 9
     AND :new.fac_gebruiker_alg_level_write <> 9
        THEN keepit:=1;
  ELSIF funclevel=13 AND -- prs/alg read
        (:new.fac_gebruiker_prs_level_read <> 9
     OR :new.fac_gebruiker_alg_level_read <> 9)
        THEN keepit:=1;
  ELSIF funclevel=14 AND -- prs/alg write, probably unused
        (:new.fac_gebruiker_prs_level_write <> 9
     OR :new.fac_gebruiker_alg_level_write <> 9)
        THEN keepit:=1;
  ELSIF funclevel=15 AND -- prs/alg read n write
        (:new.fac_gebruiker_prs_level_read <> 9
     OR :new.fac_gebruiker_prs_level_write <> 9
     OR :new.fac_gebruiker_alg_level_read <> 9
     OR :new.fac_gebruiker_alg_level_write <> 9)
        THEN keepit:=1;
  END IF;
  -- Slotbetoog: als deze autorisatie niet minsten iets te lezen heeft, mag-ie weg.
  -- Omdat de ASP dit voorkomt, is dit een error als vangnet
  IF keepit = 1 AND (:new.fac_gebruiker_prs_level_read = 9 OR :new.fac_gebruiker_alg_level_read = 9)
  THEN
      raise_application_error (-20000, 'FAC_MGG1 this is insufficient authorisation for this right');
  END IF;
  IF keepit = 0 THEN
        Dummy :=  fac.remember_SavePrimaryKey('FAC_GROEPRECHTEN',:new.fac_groeprechten_key);
  END IF;
END;
/

-- SQLS heeft deze trigger niet nodig: de rechten worden in de 'before' al verwijderd
-- omdat bij SQLS de 'before' altijd een 'after' is.
CREATE_TRIGGER(fac_t_fac_groeprechten_A_IU)
AFTER INSERT OR UPDATE ON fac_groeprechten
DECLARE
  PrimKey NUMBER(10);
  Dummy BOOLEAN;
  IndexNr NUMBER;
BEGIN
  IndexNr   := 1;
  PrimKey :=  fac.remember_GetPrimaryKey('FAC_GROEPRECHTEN',IndexNr);
  WHILE PrimKey IS NOT NULL
  LOOP
    DELETE
    FROM   FAC_groeprechten
    WHERE  fac_groeprechten_key = PrimKey;
	IndexNr    := IndexNr + 1;
    PrimKey :=  fac.remember_GetPrimaryKey('FAC_GROEPRECHTEN',IndexNr);
  END LOOP;
  Dummy :=  fac.remember_ResetSelectie('FAC_GROEPRECHTEN');
END;
/

CREATE_TRIGGER(fac_t_fac_message_B_IU)
BEFORE INSERT OR UPDATE ON fac_message
FOR EACH ROW
BEGIN
   UPDATE_UPPER(fac_message_code, fac_message_code, fac_message);
END;
/

CREATE_TRIGGER(fac_t_fac_usrtab_B_I)
BEFORE INSERT ON fac_usrtab
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_usrtab_key, fac_s_fac_usrtab_key);
   UPDATE_AANMAAKDATUM(fac_usrtab, fac_usrtab_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_usrtab_B_IU)
BEFORE INSERT OR UPDATE ON fac_usrtab
FOR EACH ROW
BEGIN
   :new.fac_usrtab_object := 'USR_'||UPPER(:new.fac_usrtab_naam);
END;
/

CREATE_TRIGGER(fac_t_fac_usrdata_B_I)
BEFORE INSERT ON fac_usrdata
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_usrdata_key, fac_s_fac_usrdata_key);
   UPDATE_AANMAAKDATUM(fac_usrdata, fac_usrdata_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_usrdata_B_IU)
BEFORE INSERT OR UPDATE ON fac_usrdata
FOR EACH ROW
BEGIN
   :new.fac_usrdata_upper := UPPER(:new.fac_usrdata_code);
END;
/

CREATE_TRIGGER(fac_t_fac_usrrap_B_IU)
BEFORE INSERT OR UPDATE ON fac_usrrap
FOR EACH ROW
DECLARE
BEGIN
   -- Geeft een 44002 als invalid
   :new.fac_usrrap_view_name := DBMS_ASSERT.SQL_OBJECT_NAME(:new.fac_usrrap_view_name);
   UPDATE_PRIMARY_KEY(fac_usrrap_key, fac_s_fac_usrrap_key);
   UPDATE_AANMAAKDATUM(fac_usrrap, fac_usrrap_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_usrrapcols_B_IU)
BEFORE INSERT OR UPDATE ON fac_usrrap_cols
FOR EACH ROW
DECLARE
BEGIN
   UPDATE_PRIMARY_KEY(fac_usrrap_cols_key, fac_s_fac_usrrap_cols_key);
END;
/

CREATE_TRIGGER(fac_t_fac_usrgraph_B_IU)
BEFORE INSERT OR UPDATE ON fac_usrgraph
FOR EACH ROW
DECLARE
BEGIN
   -- Geeft een 44002 als invalid
   :new.fac_usrgraph_view_name := DBMS_ASSERT.SQL_OBJECT_NAME(:new.fac_usrgraph_view_name);
   UPDATE_PRIMARY_KEY(fac_usrgraph_key, fac_s_fac_usrgraph_key);
   UPDATE_AANMAAKDATUM(fac_usrgraph, fac_usrgraph_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_mgtrap_B_IU)
BEFORE INSERT OR UPDATE ON fac_mgtrap
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_mgtrap_key, fac_s_fac_mgtrap_key);
   UPDATE_AANMAAKDATUM(fac_mgtrap, fac_mgtrap_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_imp_schedule_B_IU)
BEFORE INSERT OR UPDATE ON imp_schedule
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(imp_schedule_key, fac_s_imp_schedule_key);
   UPDATE_AANMAAKDATUM(imp_schedule, imp_schedule_aanmaak);
END;
/

AUDIT_BEGIN(fac_profiel)
AUDIT_VALUE(fac_profiel, fac_profiel_omschrijving)
AUDIT_VALUE(fac_profiel, fac_profiel_limiet)
AUDIT_END()

CREATE_TRIGGER(fac_t_fac_profiel_B_IU)
BEFORE INSERT OR UPDATE ON fac_profiel
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_profiel_key, fac_s_fac_profiel_key);
END;
/

AUDIT_BEGIN(fac_profielwaarde)
AUDIT_VALUE(fac_profielwaarde, fac_profiel_key)
AUDIT_VALUE(fac_profielwaarde, fac_profielwaarde_limiet)
AUDIT_VALUE(fac_profielwaarde, ins_discipline_key)
AUDIT_END()

CREATE_TRIGGER(fac_t_fac_profielwaarde_B_IU)
BEFORE INSERT OR UPDATE ON fac_profielwaarde
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_profielwaarde_key, fac_s_fac_profielwaarde_key);
END;
/

CREATE_TRIGGER(fac_t_fac_srtnotificatie_B_IU)
BEFORE INSERT OR UPDATE ON fac_srtnotificatie
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_srtnotificatie_key, fac_s_fac_srtnotificatie_key);
END;
/

CREATE_TRIGGER(fac_t_fac_notificatie_B_IU)
BEFORE INSERT OR UPDATE ON fac_notificatie
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_notificatie_key, fac_s_fac_notificatie_key);
END;
/

CREATE_TRIGGER(fac_t_fac_notificatie_B_I)
   BEFORE INSERT
   ON fac_notificatie
   FOR EACH ROW
DECLARE
    loms   web_user_messages.web_user_mess_dsc%TYPE;
    lxmlnode fac_srtnotificatie.fac_srtnotificatie_xmlnode%TYPE;
BEGIN
   -- Kopieer direct naar statusinformatie op de portal indien van toepassing
   -- Daarbij worden alle oude berichten (eerst) verwijderd
   IF BITAND (:NEW.fac_notificatie_status, 1) = 1  AND :new.prs_perslid_key_receiver IS NOT NULL
   THEN
      -- strip zodat we alleen de eerste regel overhouden
      loms := SUBSTR(:NEW.fac_notificatie_oms, 1, 512);
      IF INSTR (loms, chr(13)) > 0 THEN
        loms := SUBSTR (loms, 1, INSTR (loms, chr(13)) -1);
      END IF;
      IF INSTR (loms, chr(10)) > 0 THEN
        loms := SUBSTR (loms, 1, INSTR (loms, chr(10)) -1);
      END IF;
      -- Verwijder oudere (achterhaalde) portalberichten over hetzelfde
      IF :NEW.fac_srtnotificatie_key IS NOT NULL
      THEN
          SELECT fac_srtnotificatie_xmlnode
            INTO lxmlnode
            FROM fac_srtnotificatie
           WHERE fac_srtnotificatie_key = :NEW.fac_srtnotificatie_key;

          DELETE FROM web_user_messages
                WHERE fac_srtnotificatie_key IN
                      (SELECT fac_srtnotificatie_key
                         FROM fac_srtnotificatie
                        WHERE fac_srtnotificatie_xmlnode = lxmlnode)
                  AND prs_perslid_key_receiver = :NEW.prs_perslid_key_receiver
                  AND web_user_mess_action_params = COALESCE (:NEW.fac_notificatie_extrakey, :NEW.fac_notificatie_refkey);
      END IF;
      -- En voeg de nieuwe toe. Let op: als een extrakey bekend is, wordt die als parameter gebruikt, anders gewoon refkey
      INSERT INTO web_user_messages
                  (prs_perslid_key_sender, prs_perslid_key_receiver, web_user_mess_dsc, web_user_mess_action_status,
                   web_user_mess_action_params, fac_srtnotificatie_key, web_user_mess_prioriteit
                  )
           VALUES (:NEW.prs_perslid_key_sender, :NEW.prs_perslid_key_receiver, loms, 1,
                   COALESCE (:NEW.fac_notificatie_extrakey, :NEW.fac_notificatie_refkey), :NEW.fac_srtnotificatie_key,
                   :NEW.fac_notificatie_prioriteit
                  );

   END IF;
   -- Always clear the notify_to_statinfo bit, even when no :new.prs_perslid_key_receiver
   :NEW.fac_notificatie_status := BITAND (:NEW.fac_notificatie_status, 255 - 1);
END;
/

CREATE_TRIGGER(fac_t_fac_notificatie_A_U)
AFTER UPDATE ON fac_notificatie
BEGIN
  -- Ruim alle totaal verwerkte notificaties op.
  DELETE FROM fac_notificatie WHERE fac_notificatie_status=0 AND fac_notificatie_systeemadres IS NULL;
END;
/

CREATE_TRIGGER(fac_t_fac_notificatie_job_B_IU)
BEFORE INSERT OR UPDATE ON fac_notificatie_job
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_notificatie_job_key, fac_s_fac_notificatie_job_key);
    :new.fac_notificatie_job_view:=UPPER(:new.fac_notificatie_job_view);
END;
/

CREATE_TRIGGER(fac_t_fac_export_app_B_IU)
BEFORE INSERT OR UPDATE ON fac_export_app
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_export_app_key, fac_s_fac_export_app_key);
END;
/

CREATE_TRIGGER(fac_t_fac_import_app_B_IU)
BEFORE INSERT OR UPDATE ON fac_import_app
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_import_app_key, fac_s_fac_import_app_key);
END;
/

CREATE_TRIGGER(fac_t_fac_import_B_I)
BEFORE INSERT ON fac_import
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_import_key, fac_s_fac_import_key);
END;
/

CREATE_TRIGGER(fac_t_imp_log_B_I)
BEFORE INSERT ON imp_log
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(imp_log_key, imp_s_imp_log_key);
END;
/

CREATE_TRIGGER(imp_t_notificatie_imp_log)
   AFTER INSERT
   ON imp_log
   FOR EACH ROW
BEGIN
   DECLARE
      scode   fac_srtnotificatie.fac_srtnotificatie_code%TYPE;
      smode   fac_srtnotificatie.fac_srtnotificatie_mode%TYPE;
      sappl   imp_log.imp_log_applicatie%TYPE;
   BEGIN
      IF :NEW.imp_log_status = 'F'
      THEN
         -- fatal error; systeembeheerder moet aan de bak
         scode := 'IMPFAT';
      ELSIF :NEW.imp_log_status = 'E'
      THEN
         -- error; applicatiebeheerder moet aan de bak
         scode := 'IMPERR';
      END IF;

      IF scode IS NOT NULL
      THEN
         BEGIN
            SELECT sn.fac_srtnotificatie_mode
              INTO smode
              FROM fac_srtnotificatie sn
             WHERE sn.fac_srtnotificatie_code = scode AND sn.fac_srtnotificatie_mode > 0;
         EXCEPTION
            WHEN NO_DATA_FOUND
            THEN
               RETURN;
         END;
         sappl := :NEW.imp_log_applicatie;
         IF sappl IS NULL THEN
            -- imp_log_applicatie *of* fac_import_key is verplicht
            SELECT fac_import_app_code
              INTO sappl
              FROM fac_import_app fia, fac_import fi
             WHERE fia.fac_import_app_key = fi.fac_import_app_key
               AND fi.fac_import_key = :NEW.fac_import_key;
         END IF;

         fac.putsystemnotification (sappl||': '||:NEW.imp_log_omschrijving, smode);
      END IF;
   END;
END;
/


CREATE_TRIGGER(fac_t_fac_activiteit_B_IU)
BEFORE INSERT OR UPDATE ON fac_activiteit
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_activiteit_key, fac_s_fac_activiteit_key);
END;
/

AUDIT_BEGIN(fac_setting)
AUDIT_VALUE(fac_setting, fac_setting_pvalue)
AUDIT_END()

CREATE_TRIGGER(fac_t_fac_setting_B_IU)
BEFORE INSERT or UPDATE ON fac_setting
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_setting_key, fac_s_fac_setting_key);
    IF :new.fac_setting_datum IS NULL OR
        COALESCE(:OLD.fac_setting_pvalue,'undefined') <> COALESCE(:NEW.fac_setting_pvalue,'undefined') OR
        COALESCE(:OLD.fac_setting_default,'undefined') <> COALESCE(:NEW.fac_setting_default,'undefined')
    THEN
        -- Datum bijwerken is belangrijk omdat webserver op basis hiervan
        -- eventueel versneld zijn settings-cache refreshed
        :new.fac_setting_datum := SYSDATE;
    END IF;

    :new.fac_setting_module := UPPER(:new.fac_setting_module);
    :new.fac_setting_name := LOWER(:new.fac_setting_name);
    -- Het kan geen kwaad om wijzigingen te tracken
    IF :new.fac_setting_datum <> :old.fac_setting_datum
    THEN
        INSERT INTO adm_tracking (adm_tracking_name, adm_tracking_revision, adm_tracking_osuser)
           VALUES   (SUBSTR('S('||:new.fac_setting_name||')', 1, 60),
                     SUBSTR(:new.fac_setting_pvalue, 1, 60),
                     :new.prs_perslid_key   -- we gaan de naam er niet bijzoeken hoor
                    );
    END IF;
END;
/

CREATE_TRIGGER(fac_t_fac_qlikview_b_iu)
BEFORE INSERT OR UPDATE ON fac_qlikview
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_qlikview_key, fac_s_fac_qlikview_key);
  UPDATE_AANMAAKDATUM(fac_qlikview, fac_qlikview_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_qvw_ticket_B_I)
BEFORE INSERT ON fac_qvw_ticket
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_qvw_ticket_key, fac_s_fac_qvw_ticket_key);
END;
/


CREATE_TRIGGER(fac_t_fac_tracking_b_i)
BEFORE INSERT ON fac_tracking
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_tracking_key, fac_s_fac_tracking_key);
  -- dit event moet mogelijk ook actief worden genotificeerd,
  -- dat kan expliciet worden onderdrukt door de omschrijving te prefixen met een #
  IF SUBSTR(:new.fac_tracking_oms, 1, 1) = '#'
  THEN
    -- bedoeld om te onderdrukken, maar moeten we wel weer verwijderen
    :new.fac_tracking_oms := SUBSTR(:new.fac_tracking_oms, 2);
    -- Moet ik nou nog iets doen om NULL te maken als dit alleen de # was?
    -- En nu dus niet tracken
  ELSE
    fac.notifytracking(:new.fac_srtnotificatie_key, :new.prs_perslid_key, :new.fac_tracking_oms, :new.fac_tracking_refkey);
  END IF;
END;
/

CREATE_TRIGGER(fac_t_fac_menuitems__b_iu)
BEFORE INSERT OR UPDATE ON fac_menuitems
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_menuitems_key, fac_s_fac_menuitems_key);
END;
/

CREATE_TRIGGER(fac_t_fac_menu__b_iu)
BEFORE INSERT OR UPDATE ON fac_menu
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_menu_key, fac_s_fac_menu_key);
END;
/
CREATE_TRIGGER(fac_t_fac_nieuws__b_iu)
BEFORE INSERT OR UPDATE ON fac_nieuws
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_nieuws_key, fac_s_fac_nieuws_key);
END;
/
CREATE_TRIGGER(fac_t_fac_faq__b_iu)
BEFORE INSERT OR UPDATE ON fac_faq
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_faq_key, fac_s_fac_faq_key);
END;
/
CREATE_TRIGGER(fac_t_fac_locale_b_iu)
BEFORE INSERT OR UPDATE ON fac_locale
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_locale_key, fac_s_fac_locale_key);
   :new.fac_locale_kolomnaam := UPPER (:new.fac_locale_kolomnaam);
   :new.fac_locale_lang := UPPER (:new.fac_locale_lang);
END;
/
CREATE_TRIGGER(fac_t_fac_localeitems_b_iu)
BEFORE INSERT OR UPDATE ON fac_localeitems
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_localeitems_key, fac_s_fac_localeitems_key);
   :new.fac_localeitems_dialect_id := UPPER (:new.fac_localeitems_dialect_id);
END;
/
CREATE_TRIGGER(fac_t_fac_locale_xsl_b_iu)
BEFORE INSERT OR UPDATE ON fac_locale_xsl
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_locale_xsl_key, fac_s_fac_locale_xsl_key);
   :new.fac_locale_xsl_lang := UPPER (:new.fac_locale_xsl_lang);
END;
/

CREATE_TRIGGER(fac_t_fac_locale_xref_b_iu)
BEFORE INSERT OR UPDATE ON fac_locale_xref
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_locale_xref_key, fac_s_fac_locale_xref_key);
   :new.fac_locale_xref_filepath := LOWER (:new.fac_locale_xref_filepath);
END;
/

CREATE_TRIGGER(fac_t_fac_kenmerkdomein_B_IU)
BEFORE INSERT OR UPDATE ON fac_kenmerkdomein
FOR EACH ROW
DECLARE
   objecttype user_objects.object_type%TYPE;
BEGIN
   UPDATE_PRIMARY_KEY(fac_kenmerkdomein_key, fac_s_fac_kenmerkdomein_key);
   :new.fac_kenmerkdomein_upper := UPPER(:new.fac_kenmerkdomein_omschrijving);
   :new.fac_kenmerkdomein_module := UPPER(:new.fac_kenmerkdomein_module);
   :new.fac_kenmerkdomein_xmlnode := LOWER(:new.fac_kenmerkdomein_xmlnode);
   BEGIN
      SELECT   object_type
        INTO   objecttype
        FROM   user_objects
       WHERE   UPPER(object_name) = UPPER(DBMS_ASSERT.SQL_OBJECT_NAME(:new.fac_kenmerkdomein_objectnaam));
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         objecttype := NULL;
   END;

   IF objecttype IS NULL
   THEN
      raise_application_error (-20000, 'FAC_MXX3 Invalid object');
   ELSIF objecttype IN ('TABLE', 'VIEW')
   THEN
      DECLARE
         dummy VARCHAR2 (30);
      BEGIN
         SELECT   column_name
           INTO   dummy
           FROM   user_tab_columns
          WHERE       table_name = :new.fac_kenmerkdomein_objectnaam
                  AND column_name = :new.fac_kenmerkdomein_kolomnaam
                  AND data_type = 'NUMBER';
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            raise_application_error (-20000, 'ALG_M236');
      END;

      DECLARE
         dummy VARCHAR2 (30);
      BEGIN
         SELECT   column_name
           INTO   dummy
           FROM   user_tab_columns
          WHERE       table_name = :new.fac_kenmerkdomein_objectnaam
                  AND column_name = :new.fac_kenmerkdomein_kolomtxt
                  AND data_type LIKE '%CHAR%';
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            raise_application_error (-20000, 'ALG_M237');
      END;
   ELSIF objecttype <> 'SYNONYM'
   THEN
      raise_application_error (-20000, 'ALG_M235');
   END IF;

END;
/

CREATE_TRIGGER(fac_t_fac_kenmwaarden_B_IU)
BEFORE INSERT OR UPDATE ON fac_kenmwaarden
FOR EACH ROW
BEGIN
  UPDATE_PRIMARY_KEY(fac_kenmwaarden_key, fac_s_fac_kenmwaarden_key);
END;
/

CREATE_TRIGGER(fac_t_fac_widget_B_IU)
BEFORE INSERT OR UPDATE ON fac_widget
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_widget_key, fac_s_fac_widget_key);
   UPDATE_AANMAAKDATUM(fac_widget, fac_widget_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_code2label_B_IU)
BEFORE INSERT OR UPDATE ON fac_code2label
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_code2label_key, fac_s_fac_code2label_key);
END;
/

CREATE_TRIGGER(fac_t_fac_api_b_iu)
BEFORE INSERT OR UPDATE ON fac_api
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_api_key, fac_s_fac_api_key);
END;
/

CREATE_TRIGGER(fac_t_fac_audit_b_i)
BEFORE INSERT ON fac_audit
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_audit_key, fac_s_fac_audit_key);
END;
/

CREATE_TRIGGER(fac_t_fac_auditfail_b_i)
BEFORE INSERT ON fac_auditfail
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_auditfail_key, fac_s_fac_auditfail_key);
END;
/

CREATE_TRIGGER(faq_t_faq_kenmerkwaarde_b_iu)
BEFORE INSERT OR UPDATE ON faq_kenmerkwaarde
FOR EACH ROW
BEGIN
   UPDATE_AANMAAKDATUM(faq_kenmerkwaarde, faq_kenmerkwaarde_aanmaak);
   /*
   ** Controleer eerst of de ingevulde waarde goed is.
   */
   IF :new.faq_kenmerkwaarde_waarde <> :old.faq_kenmerkwaarde_waarde
   OR :old.faq_kenmerkwaarde_waarde IS NULL
   OR :new.faq_kenmerkwaarde_waarde IS NULL
   THEN
      DECLARE
         KenmerkOmschr   faq_kenmerk.faq_kenmerk_omschrijving%TYPE;
         KenmerkType     faq_kenmerk.faq_kenmerk_kenmerktype%TYPE;
         KenmerkLengte   faq_kenmerk.faq_kenmerk_lengte%TYPE;
         KenmerkDec      faq_kenmerk.faq_kenmerk_dec%TYPE;
         KenmerkNMin     faq_kenmerk.faq_kenmerk_nmin%TYPE;
         KenmerkNMax     faq_kenmerk.faq_kenmerk_nmax%TYPE;
         NumberWaarde    NUMBER(25,5);
         FormatMask      VARCHAR2(60);
         KenmerkWaarde   faq_kenmerkwaarde.faq_kenmerkwaarde_waarde%TYPE;
      BEGIN
         KenmerkWaarde := :new.faq_kenmerkwaarde_waarde;
         BEGIN
            DECLARE
               dummy VARCHAR2(1);
            BEGIN
            SELECT faq_kenmerk_omschrijving
            ,      faq_kenmerk_kenmerktype
            ,      faq_kenmerk_lengte
            ,      faq_kenmerk_dec
            ,      faq_kenmerk_nmin
            ,      faq_kenmerk_nmax
            INTO   KenmerkOmschr
            ,      KenmerkType
            ,      KenmerkLengte
            ,      KenmerkDec
            ,      KenmerkNMin
            ,      KenmerkNMax
            FROM   faq_kenmerk
            WHERE  faq_kenmerk_key = :new.faq_kenmerk_key;
         EXCEPTION
            WHEN NO_DATA_FOUND
            THEN APPLICATION_ERROR_GOTO(-20000,'INS_R_INS_KENMERK_KEY1');
         END;

         /* Kijk welk KenmerkType gebruikt wordt.
         ** Aan de hand daarvan moet een controle uitgevoerd worden.
         **/
         IF   KenmerkType = 'C'
         /*
         ** Char
         */
         THEN
            /*
            ** Controleer of de lengte te groot of goed is
            */
            IF LENGTH(KenmerkWaarde) > KenmerkLengte
            THEN
               APPLICATION_ERROR(-20004, 'faq_m232,'|| KenmerkOmschr ||','
                                                    || TO_CHAR(KenmerkLengte));
            END IF;
         ELSIF KenmerkType IN ('N', 'B')
         /*
         ** Number
         */
         THEN
            /*
            ** Controleer of er een numerieke waarde ingevuld is
            */
            IF KenmerkDec IS NULL OR KenmerkDec = 0
            THEN
               FormatMask := RPAD('9', KenmerkLengte, '9');
            ELSE
               FormatMask := RPAD('9', KenmerkLengte - KenmerkDec, '9')
                                ||'D'||RPAD('9', KenmerkDec, '9');
            END IF;
            /*
            ** Controleer of er een numerieke waarde ingevuld is
            */
            BEGIN
               NumberWaarde := TO_NUMBER(KenmerkWaarde);
            EXCEPTION
               WHEN OTHERS
               THEN FormatMask := REPLACE(FormatMask, '9', '#');
                    FormatMask := REPLACE(FormatMask, '0', '#');
                    FormatMask := REPLACE(FormatMask, 'D', '.');
                    RAISE_APPLICATION_ERROR(-20004,'faq_m233,'|| KenmerkOmschr ||','
                                                              || FormatMask);
            END;
            /*
            ** Controleer of de waarde binnen NMin en NMax ligt
            */
            IF (   (KenmerkNMin IS NOT NULL AND NumberWaarde < KenmerkNMin)
                OR (KenmerkNMax IS NOT NULL AND NumberWaarde > KenmerkNMax))
            THEN
               APPLICATION_ERROR_GOTO(-20004, 'faq_m234,'|| KenmerkOmschr       ||','
                                                    || TO_CHAR(KenmerkNMin)||','
                                                    || TO_CHAR(KenmerkNMax));
            ELSE
               /*
               **  Controleer of de waarde aan het formaat voldoet
               */
               KenmerkWaarde := LTRIM(NUMBER_TO_CHAR(NumberWaarde, FormatMask));
               IF INSTR2(KenmerkWaarde, '#') <> 0
               THEN
                  FormatMask := REPLACE(FormatMask, '9', '#');
                  FormatMask := REPLACE(FormatMask, '0', '#');
                  FormatMask := REPLACE(FormatMask, 'D', '.');
                  APPLICATION_ERROR_GOTO(-20004,'faq_m233,'|| KenmerkOmschr ||','
                                                      || FormatMask);
               END IF;
            END IF;
            /*
            ** Zet de geconverteerde waarde terug
            */
            :new.faq_kenmerkwaarde_waarde := KenmerkWaarde;
         ELSIF KenmerkType = 'D'
         /*
         ** Datum
         */
         THEN
            KenmerkWaarde := DATE_TO_CHAR(_TO_DATE2(KenmerkWaarde,'DD-MM-YY'),'DD-MM-YYYY');
            /*
            ** Zet de geconverteerde waarde terug
            */
            :new.faq_kenmerkwaarde_waarde := KenmerkWaarde;
         END IF;
      END;
    END;
    UPDATE_PRIMARY_KEY(faq_kenmerkwaarde_key, faq_s_faq_kenmerkwaarde_key);
   END IF;
END;
/

CREATE_TRIGGER(faq_t_faq_kenmerk_b_iu)
BEFORE INSERT OR UPDATE ON faq_kenmerk
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(faq_kenmerk_key, faq_s_faq_kenmerk_key);
   UPDATE_UPPER(faq_kenmerk_omschrijving, faq_kenmerk_upper,faq_kenmerk);
   IF (:new.faq_kenmerk_verwijder IS NOT NULL AND :old.faq_kenmerk_verwijder IS NULL AND
       :new.faq_kenmerk_systeem = 1)
   THEN
      APPLICATION_ERROR(-20003, 'faq_m238');
   END IF;
   IF :new.faq_kenmerk_kenmerktype IN ('C','N', 'B') AND :new.faq_kenmerk_lengte IS NULL
   THEN
      APPLICATION_ERROR(-20000, 'FAQ_C_FAQ_KENMERK_LENGTE');
   END IF;
   IF :new.faq_kenmerk_kenmerktype IN ('N', 'B') AND :new.faq_kenmerk_lengte > 20
   THEN
      :new.faq_kenmerk_lengte := 20;
   END IF;
   IF :new.faq_kenmerk_kenmerktype IN ('N', 'B') AND :old.faq_kenmerk_key IS NOT NULL
   THEN
     IF :old.faq_kenmerk_nmin <> :new.faq_kenmerk_nmin OR
       :old.faq_kenmerk_nmin IS NULL AND :new.faq_kenmerk_nmin IS NOT NULL
     THEN
       DECLARE
         Dummy CHAR;
       BEGIN
         SELECT 'X'
           INTO Dummy
           FROM faq_kenmerkwaarde
          WHERE faq_kenmerk_key = :old.faq_kenmerk_key
            AND faq_kenmerk_key IN
              (SELECT faq_kenmerk_key FROM faq_kenmerkwaarde
                WHERE faq_kenmerk_key = :old.faq_kenmerk_key
                  AND FAC.SAFE_TO_NUMBER(faq_kenmerkwaarde_waarde) < :new.faq_kenmerk_nmin
                  AND rownum=1);
               APPLICATION_ERROR(-20000, 'FAQ_C_FAQ_KENMERK_MIN');
       EXCEPTION
         WHEN NO_DATA_FOUND
           THEN NULL;
         WHEN TOO_MANY_ROWS THEN
               APPLICATION_ERROR(-20000, 'FAQ_C_FAQ_KENMERK_MIN');
       END;
     END IF;
     IF :old.faq_kenmerk_nmax <> :new.faq_kenmerk_nmax OR
       :old.faq_kenmerk_nmax IS NULL AND :new.faq_kenmerk_nmax IS NOT NULL
     THEN
       DECLARE
         Dummy CHAR;
       BEGIN
         SELECT 'X'
           INTO Dummy
           FROM faq_kenmerkwaarde
          WHERE faq_kenmerk_key = :old.faq_kenmerk_key
            AND faq_kenmerk_key IN
              (SELECT  faq_kenmerk_key FROM faq_kenmerkwaarde
                WHERE faq_kenmerk_key = :old.faq_kenmerk_key
                  AND FAC.SAFE_TO_NUMBER(faq_kenmerkwaarde_waarde) > :new.faq_kenmerk_nmax
                  AND rownum=1);
               APPLICATION_ERROR(-20000, 'FAQ_C_FAQ_KENMERK_MAX');
       EXCEPTION
         WHEN NO_DATA_FOUND
           THEN NULL;
         WHEN TOO_MANY_ROWS THEN
           BEGIN
               APPLICATION_ERROR(-20000, 'FAQ_C_FAQ_KENMERK_MAX');
           END;
       END;
     END IF;
   END IF;
   UPDATE_AANMAAKDATUM(faq_kenmerk, faq_kenmerk_aanmaak);
END;
/

CREATE_TRIGGER(fac_t_fac_gui_counter_b_i)
BEFORE INSERT ON fac_gui_counter
FOR EACH ROW
BEGIN
   UPDATE_PRIMARY_KEY(fac_gui_counter_key, faq_s_fac_gui_counter_key);
END;
/

CREATE_TRIGGER(fac_t_fac_bookmark_B_I)
BEFORE INSERT ON fac_bookmark
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_bookmark_key, fac_s_fac_bookmark_key);
END;
/

CREATE_TRIGGER(fac_t_fac_session_B_I)
BEFORE INSERT ON fac_session
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_session_key, fac_s_fac_session_key);
END;
/

CREATE_TRIGGER(fac_t_fac_sequence_B_I)
BEFORE INSERT ON fac_sequence
FOR EACH ROW
BEGIN
    UPDATE_PRIMARY_KEY(fac_sequence_key, fac_s_fac_sequence_key);
END;
/

REGISTERRUN('$Id$')
