--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_create_ODS_package_' || 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 create ODS package
--=============================================================================================================================

CREATE OR REPLACE EDITIONABLE PACKAGE "ODS"."FILE_MANAGER_ODS"
AUTHID DEFINER
AS
   /**
    * FILE_MANAGER_ODS - Wrapper package for CT_MRDS.FILE_MANAGER
    *
    * This package serves as a wrapper for CT_MRDS.FILE_MANAGER procedures.
    * The key difference is that this package uses AUTHID DEFINER instead of
    * AUTHID CURRENT_USER, which means all objects will be created in the ODS
    * schema regardless of which user executes the procedures.
    *
    * This solves the execution context issue where users need to connect as
    * ODS user to use CT_MRDS.FILE_MANAGER procedures.
    */

   /**
    * @name     CREATE_EXTERNAL_TABLE
    * @desc     Creates external tables that can read data from Oracle Cloud Storage.
    *           This is a wrapper for CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE
    *           but uses DEFINER rights to ensure objects are created in ODS schema.
    * @param    pTableName - Name of the external table to create
    * @param    pTemplateTableName - Template table defining the structure
    * @param    pPrefix - Storage path prefix in Oracle Cloud Storage
    * @param    pBucketUri - URI of the target bucket (default: ENV_MANAGER.gvInboxBucketUri)
    * @param    pFileName - Specific file name (optional)
    * @param    pDelimiter - Field delimiter (default: ',')
    * @example  EXEC ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
    *               'C2D_A_UC_DISSEM_METADATA_LOADS_INBOX',
    *               'CT_ET_TEMPLATES.C2D_A_UC_DISSEM_METADATA_LOADS',
    *               'INBOX/C2D/UC_DISSEM/A_UC_DISSEM_METADATA_LOADS',
    *               CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    *           );
    */
   PROCEDURE CREATE_EXTERNAL_TABLE (
      pTableName          IN VARCHAR2,
      pTemplateTableName  IN VARCHAR2,
      pPrefix             IN VARCHAR2,
      pBucketUri          IN VARCHAR2 DEFAULT CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
      pFileName           IN VARCHAR2 DEFAULT NULL,
      pDelimiter          IN VARCHAR2 DEFAULT ','
   );

END FILE_MANAGER_ODS;

/

CREATE OR REPLACE EDITIONABLE PACKAGE BODY "ODS"."FILE_MANAGER_ODS"
AS
 
   /**
    * CREATE_EXTERNAL_TABLE - Wrapper for CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE
    */
   PROCEDURE CREATE_EXTERNAL_TABLE (
      pTableName          IN VARCHAR2,
      pTemplateTableName  IN VARCHAR2,
      pPrefix             IN VARCHAR2,
      pBucketUri          IN VARCHAR2 DEFAULT CT_MRDS.ENV_MANAGER.gvInboxBucketUri,
      pFileName           IN VARCHAR2 DEFAULT NULL,
      pDelimiter          IN VARCHAR2 DEFAULT ','
   )
   IS
      vParameters CT_MRDS.A_PROCESS_LOG.PROCEDURE_PARAMETERS%TYPE;
   BEGIN
      -- Log the start of the procedure
      vParameters := CT_MRDS.ENV_MANAGER.FORMAT_PARAMETERS(
         SYS.ODCIVARCHAR2LIST(
            'pTableName => ''' || pTableName || '''',
            'pTemplateTableName => ''' || pTemplateTableName || '''',
            'pPrefix => ''' || pPrefix || '''',
            'pBucketUri => ''' || pBucketUri || '''',
            'pFileName => ''' || NVL(pFileName, 'NULL') || '''',
            'pDelimiter => ''' || pDelimiter || ''''
         )
      );

      CT_MRDS.ENV_MANAGER.LOG_PROCESS_EVENT('Start FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE', 'INFO', vParameters);

      -- Call the original CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE procedure
      -- This ensures all logic remains centralized in the original package
      -- and ODS wrapper simply delegates execution with DEFINER rights
      CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE(
         pTableName         => pTableName,
         pTemplateTableName => pTemplateTableName,
         pPrefix            => pPrefix,
         pBucketUri         => pBucketUri,
         pFileName          => pFileName,
         pDelimiter         => pDelimiter
      );

      CT_MRDS.ENV_MANAGER.LOG_PROCESS_EVENT('End FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE', 'INFO', vParameters);

   EXCEPTION
      WHEN OTHERS THEN
         CT_MRDS.ENV_MANAGER.LOG_PROCESS_EVENT(
            'Error in ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE: ' || SQLERRM,
            'ERROR',
            vParameters
         );
         RAISE;
   END CREATE_EXTERNAL_TABLE;

END FILE_MANAGER_ODS;

/
