--MARS-770

set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;

column owner format a15
column object_name format a30
column spoolname new_value spoolname 

select 'install_package_MarsGeneralRelease_MARS770_triggers_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'


prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;

--set echo on
--=============================================================================================================================
-- start install --MARS-770 triggers
--=============================================================================================================================

CREATE OR REPLACE TRIGGER CT_MRDS.TRG_BIU_CHCK_TEMPLATE_TABLE_NAME
BEFORE INSERT OR UPDATE ON "CT_MRDS"."A_COLUMN_DATE_FORMAT" 
FOR EACH ROW
DECLARE
    vCount NUMBER;
BEGIN
  SELECT COUNT(*) 
    INTO vCount 
    FROM CT_MRDS.A_SOURCE_FILE_CONFIG 
   WHERE TEMPLATE_TABLE_NAME = :NEW.TEMPLATE_TABLE_NAME;
 
    IF vCount = 0 THEN
        RAISE_APPLICATION_ERROR(-20001, 'TEMPLATE_TABLE_NAME = '''||:NEW.TEMPLATE_TABLE_NAME||''' not exists in A_SOURCE_FILE_CONFIG.TEMPLATE_TABLE_NAME ');
    END IF;
END;
/

CREATE OR REPLACE TRIGGER CT_MRDS.TRG_BI_A_SOURCE_FILE_CONFIG_CHECK
BEFORE INSERT ON CT_MRDS.A_SOURCE_FILE_CONFIG
FOR EACH ROW
DECLARE
--   NO_CONTAINER_FOR_INPUT EXCEPTION;
   WRONG_CONTAINER_FOR_INPUT EXCEPTION;
   CONTAINER_ALREADY_EXISTS EXCEPTION;
   vCount PLS_INTEGER;
   vContainerFileKey   CT_MRDS.A_SOURCE_FILE_CONFIG.A_SOURCE_FILE_CONFIG_KEY%TYPE;
BEGIN
   
   --Check if there is CONTAINER entity in A_SOURCE_FILE_CONFIG table
   IF (:NEW.SOURCE_FILE_TYPE = 'INPUT' AND :NEW.CONTAINER_FILE_KEY IS NULL) THEN
      :NEW.CONTAINER_FILE_KEY := CT_MRDS.FILE_MANAGER.GET_CONTAINER_SOURCE_FILE_CONFIG_KEY(:NEW.SOURCE_FILE_ID);
--      IF :NEW.CONTAINER_FILE_KEY is null THEN
--         RAISE NO_CONTAINER_FOR_INPUT;
--      END IF;
   
   -- Check if provided CONTAINER_FILE_KEY is the one which exists in the A_SOURCE_FILE_CONFIG table
   ELSIF (:NEW.SOURCE_FILE_TYPE = 'INPUT' AND :NEW.CONTAINER_FILE_KEY IS NOT NULL) THEN
      vContainerFileKey := CT_MRDS.FILE_MANAGER.GET_CONTAINER_SOURCE_FILE_CONFIG_KEY(:NEW.SOURCE_FILE_ID);
      IF :NEW.CONTAINER_FILE_KEY <> vContainerFileKey THEN
         RAISE WRONG_CONTAINER_FOR_INPUT;
      END IF;
   
   -- Check if there is already a CONTAINER for provided SOURCE_FILE_ID
   ELSIF (:NEW.SOURCE_FILE_TYPE = 'CONTAINER') THEN
      SELECT count(*)
        INTO vCount
        FROM CT_MRDS.A_SOURCE_FILE_CONFIG
       WHERE :NEW.SOURCE_FILE_TYPE  = 'CONTAINER'
         AND SOURCE_FILE_ID         = :NEW.SOURCE_FILE_ID;
         
      IF vCount > 0 THEN
         RAISE CONTAINER_ALREADY_EXISTS;
      END IF;
   END IF;
   SELECT CT_MRDS.A_SOURCE_FILE_CONFIG_KEY_SEQ.NEXTVAL INTO :NEW.A_SOURCE_FILE_CONFIG_KEY FROM DUAL;
EXCEPTION
--   WHEN NO_CONTAINER_FOR_INPUT THEN
--      RAISE_APPLICATION_ERROR(-20011, 'Error while adding entity where SOURCE_FILE_TYPE = ''INPUT'''
--                  ||CHR(13)||CHR(10)||'There is no A_SOURCE_FILE_CONFIG entity where SOURCE_FILE_ID = '''||:NEW.SOURCE_FILE_ID||''' AND SOURCE_FILE_TYPE = ''CONTAINER'''
--                  ||CHR(13)||CHR(10)||'Add ''CONTAINER'' config entity before ''INPUT'' one');
   WHEN WRONG_CONTAINER_FOR_INPUT THEN
      RAISE_APPLICATION_ERROR(-20012, 'Provided CONTAINER_FILE_KEY='||:NEW.CONTAINER_FILE_KEY||' is wrong'
                  ||CHR(13)||CHR(10)||'There is A_SOURCE_FILE_CONFIG entity where SOURCE_FILE_ID = '''||:NEW.SOURCE_FILE_ID||''' AND SOURCE_FILE_TYPE = ''CONTAINER'''
                  ||CHR(13)||CHR(10)||'Existing CONTAINER_FILE_KEY='||vContainerFileKey);
   WHEN CONTAINER_ALREADY_EXISTS THEN
      RAISE_APPLICATION_ERROR(-20013, 'Such CONTAINER already exists!'
                  ||CHR(13)||CHR(10)||'There is A_SOURCE_FILE_CONFIG entity where SOURCE_FILE_ID = '''||:NEW.SOURCE_FILE_ID||''' AND SOURCE_FILE_TYPE = ''CONTAINER''');                           
END;


