
--=============================================================================================================================
--Step 1: Create Template Table
--=============================================================================================================================
--Table  TOP_ALLOTMENT_MODIFICATION_AGGREGATED
 
CREATE TABLE "CT_ET_TEMPLATES"."TOP_ALLOTMENT_MODIFICATION_AGGREGATED" 
   (
   "A_KEY" NUMBER(38,0), 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0), 
	"CHECKSUM" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ID" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"OPERATION_CURRENCY" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"REF_NO" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"REVISION" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"TIMESTAMP_SENDER" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"AMOUNT_ITEM" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"BIDDER_ID" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"FREE_TEXT" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"SENDER_REFERENCE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"VALUE_DATE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"NEW_BIDDER" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP"
   ) 
	DEFAULT COLLATION "USING_NLS_COMP" SEGMENT CREATION DEFERRED 
	PCTFREE 10 PCTUSED 40 INITRANS 10 MAXTRANS 255 
	COLUMN STORE COMPRESS FOR QUERY HIGH ROW LEVEL LOCKING LOGGING
	TABLESPACE "DATA" ;

--=============================================================================================================================
--Step 2: Create External Tables
--=============================================================================================================================

--Table TOP_ALLOTMENT_MODIFICATION_AGGREGATED

--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'TOP_ALLOTMENT_MODIFICATION_AGGREGATED_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pPrefix => 'INBOX/TOP/AggregatedAllotmentModification/TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    );
END;

--ODS - for processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'TOP_ALLOTMENT_MODIFICATION_AGGREGATED_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pPrefix => 'ODS/TOP/TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 


--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'TOP_ALLOTMENT_MODIFICATION_AGGREGATED_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pPrefix => 'ARCHIVE/TOP/TOP_ALLOTMENT_MODIFICATION_AGGREGATED',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END;
 
--=============================================================================================================================
--Step 3: Date format adjustment
--=============================================================================================================================

CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',    
	pColumnName => 'ALLOTMENT_DATE',    
	pDateFormat => 'yyyy-mm-dd'
	); 

CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',    
	pColumnName => 'VALUE_DATE',    
	pDateFormat => 'yyyy-mm-dd'
	);

CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED',    
	pColumnName => 'MATURITY_DATE',    
	pDateFormat => 'yyyy-mm-dd'
	);	
	
--=============================================================================================================================
--Step 4: Configure file type for processing
--=============================================================================================================================

 call CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'TOP'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'AggregatedAllotmentModification'
        ,pSourceFileDesc        => 'TOP source'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'TOP_ALLOTMENT_MODIFICATION_AGGREGATED'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.TOP_ALLOTMENT_MODIFICATION_AGGREGATED' 
        );
	   
	   