--=============================================================================================================================
--Step 1: Create Template Table
--=============================================================================================================================
--Table  TOP_AGGREGATED_ALLOTMENT
 

CREATE TABLE "CT_ET_TEMPLATES"."TOP_AGGREGATED_ALLOTMENT" 
   (	
    "A_KEY" NUMBER(38,0), 
	"A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	"REF_NO" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"TIMESTAMP_SENDER" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ID" NUMBER(28,0), 
	"CHECKSUM" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"REVISION" NUMBER(28,0), 
	"TRANSACTION_TYPE" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"OPERATION_CURRENCY" VARCHAR2(3 CHAR) COLLATE "USING_NLS_COMP", 
	"OPERATION_NAME" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"OPERATION_CATEGORY" VARCHAR2(50 CHAR) COLLATE "USING_NLS_COMP", 
	"OPERATION_TYPE" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"PROCEDURE" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"AUCTION_TYPE" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"ALLOTMENT_METHOD" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"QUOTATION" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"ALLOTMENT_DATE" DATE, 
	"VALUE_DATE" DATE, 
	"MATURITY_DATE" DATE, 
	"DURATION_OF_OPERATION" NUMBER(10,0), 
	"REFERENCE_ENTITY" VARCHAR2(60 CHAR) COLLATE "USING_NLS_COMP", 
	"SPOT_RATE" NUMBER(20,9), 
	"REPAID_REF_NO" VARCHAR2(8 CHAR) COLLATE "USING_NLS_COMP", 
	"BUSINESS_VALUE_TYPE" VARCHAR2(20 CHAR) COLLATE "USING_NLS_COMP", 
	"BID_CURRENCY" VARCHAR2(3 CHAR) COLLATE "USING_NLS_COMP", 
	"OTHER_CURRENCY" VARCHAR2(3 CHAR) COLLATE "USING_NLS_COMP", 
	"TOTAL_NUMBER_BIDDERS" NUMBER(28,0), 
	"TOTAL_BID_AMOUNT" NUMBER(28,8), 
	"TOTAL_ALLOTTED_AMOUNT" NUMBER(28,8), 
	"NCB_SENDER_REFERENCE" VARCHAR2(3 CHAR) COLLATE "USING_NLS_COMP", 
	"NCB_NUMBER_BIDDERS" NUMBER(28,0), 
	"NCB_BID_AMOUNT" NUMBER(28,8), 
	"NCB_ALLOTTED_AMOUNT" NUMBER(28,8), 
	"BIDDER_ID" VARCHAR2(15 CHAR) COLLATE "USING_NLS_COMP", 
	"BIDDER_NAME" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"BIDDER_BID_AMOUNT" NUMBER(28,8), 
	"BIDDER_ALLOTTED_AMOUNT" NUMBER(28,8), 
	"BIDDER_ALLOTTED_BUSINESS_VALUE" NUMBER(28,8), 
	"BID_AMOUNT" NUMBER(28,8), 
	"BID_BUSINESS_VALUE" NUMBER(28,8), 
	"BID_ALLOTTED_AMOUNT" NUMBER(28,8), 
	"BID_ALLOTTED_BUSINESS_VALUE" NUMBER(28,8), 
	"BID_ALLOTTED_PERCENTAGE" NUMBER(5,2) 
	)
  TABLESPACE "DATA" ;


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

--Table TOP_AGGREGATED_ALLOTMENT

--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'TOP_AGGREGATED_ALLOTMENT_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT',
        pPrefix => 'INBOX/TOP/AggregatedAllotmentNCB/TOP_AGGREGATED_ALLOTMENT',
        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_AGGREGATED_ALLOTMENT_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT',
        pPrefix => 'ODS/TOP/TOP_AGGREGATED_ALLOTMENT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 

--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'TOP_AGGREGATED_ALLOTMENT_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT',
        pPrefix => 'ARCHIVE/TOP/TOP_AGGREGATED_ALLOTMENT',
        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_AGGREGATED_ALLOTMENT',   
	pColumnName => 'ALLOTMENT_DATE',    
	pDateFormat =>  'YYYY-MM-DD"T"HH24:MI:SS'
	); 
	
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT',    
	pColumnName => 'MATURITY_DATE',    
	pDateFormat => 'yyyy-mm-dd'
	); 
	
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT',    
	pColumnName => 'VALUE_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          => 'AggregatedAllotmentNCB'
        ,pSourceFileDesc        => 'TOP source'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'TOP_AGGREGATED_ALLOTMENT'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.TOP_AGGREGATED_ALLOTMENT' 
        );
	   	
		 