--=============================================================================================================================
--Step 1: Create Template Table
--=============================================================================================================================
--Table  CSDB_DEBT
  
  CREATE TABLE "CT_ET_TEMPLATES"."CSDB_DEBT" 
   ( 
    "A_KEY" NUMBER(38,0) NOT NULL ENABLE, 
    "A_WORKFLOW_HISTORY_KEY" NUMBER(38,0) NOT NULL ENABLE,
	"NEWUPDATED" DATE, 
	"IDLOADDATE_DIM" DATE, 
	"EXTERNALCODE_ISIN" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"EXTERNALCODETYPE_NC" VARCHAR2(124 CHAR) COLLATE "USING_NLS_COMP", 
	"EXTERNALCODE_NATIONAL" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRINSTRUMENT" NUMBER(28,0), 
	"SHORTNAME" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
    "VA_BONDDURATION" NUMBER(28,10), 
	--  "IDIRDEPOSITORY" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", --- missing in yaml file
	"IDIRDEBTTYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRASSETSECTYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_CFI" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAI" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAI_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCURRENCY_NOMINAL" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"AMOUNTISSUED" NUMBER(28,10), 
	"AMOUNTOUTSTANDING" NUMBER(28,10), 
	"AMOUNTOUTSTANDING_EUR" NUMBER(28,10), 
	"POOLFACTOR" NUMBER(28,10), 
	"ISSUEPRICE" NUMBER(28,10), 
	"IDISSUEDATE" DATE, 
	"IDIRCOUPONTYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCOUPONFREQUENCY" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCURRENCY_COUPON" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATE" NUMBER(28,10), 
	"COUPONDATE" DATE, 
	"IDIRREDEMPTIONTYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRREDEMPTIONFREQUENCY" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCURRENCY_REDEMPTION" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"REDEMPTIONPRICE" NUMBER(28,10), 
	"IDMATURITYDATE" DATE, 
	"IDIRORGANISATIONALIASTYPE_IS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUERSOURCECODE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUEREXTERNALCODE_MFI" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUEREXTERNALCODE_BIC" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUEREXTERNALCODE_BEI" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRORGANISATION_ISSUER" NUMBER(28,0), 
	"ISSUERNAME" VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCOUNTRY" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCOUNTRY_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAO" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAO_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_NACE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"PUBLICATIONPRICEDATE" DATE, 
	"PUBLICATIONPRICE" NUMBER(28,10), 
	"PUBLICATIONPRICETYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"PUBLICATIONPRICEQUOTATIONBASIS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"MONTHLYAVERAGEPRICE" NUMBER(28,10), 
	"ACCRUALSTARTDATE" DATE, 
	"DEBTACCRUALDEBTOR" NUMBER(28,10), 
	"DEBTACCRUALDEBTOR_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"DEBTACCRUALCREDITOR" NUMBER(28,10), 
	"DEBTACCRUALCREDITOR_TYP" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ACCRUEDINTEREST" NUMBER(28,10), 
	"YTMNONOPTIONADJUSTED" NUMBER(28,10), 
	"ESCB_ISSUER_IDENT" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_ESCBCODETYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDUDCMPPARTY" NUMBER(28,0), 
	"AMOUNTOUTSTANDINGTYPE" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"MARKETCAPITALISATION" NUMBER(28,10), 
	"MARKETCAPITALISATION_EUR" NUMBER(28,10), 
	"VA_SECURITYSTATUS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_INSTRSUPPLEMENTARYCLASS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_RESIDUALMATURITYCLASS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_ISINSEC" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_ISELIGIBLEFOREADB" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAI10" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_ESAO10" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRDEBTTYPE_N" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"SENIORITY" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUEREXTERNALCODE_LEI" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"INSTR_ESA2010_CLASS_VALUETYPE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ISS_ESA2010_CLASS_VALUETYPE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_SECURITYSTATUSDATE" DATE, 
	"GROUP_TYPE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"HASEMBEDDEDOPTION" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"VOLUMETRADED" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PRIMARYLISTINGNAME" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PRIMARYLISTINGCOUNTRY" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_INSTRPORTFLAGS" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
--	"VA_BONDDURATION" NUMBER(28,10), --- moved to other possition 
	"RESIDUALMATURITY" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ORIGINAL_MATURITY" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"IDIRCLASSIFICATIONCODE_CFIN" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONFIRSTPAYMENTDATE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONLASTPAYMENTDATE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATEUNDERLYINGCODE_ISIN" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATESPREAD" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATEMULTIPLIER" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATECAP" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"COUPONRATEFLOOR" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"IDISSUEDATE_TRANCHE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUEPRICE_TRANCHE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"VA_ISPRIVATEPLACEMENT" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"RIAD_CODE" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"RIAD_OUID" NUMBER(38,0), 
	"ESG1" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ESG2" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ESG3" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"STRIP" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"DEPOSITORY_RECEIPT" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"RULE_144A" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"REG_S" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"WARRANT" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"CSEC_RELEVANCE_STOCK" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"CSEC_RELEVANCE_GROSS_ISSUANCE" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"CSEC_RELEVANCE_REDEMPTION" VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP", 
	"ACCRUING_COUPON" NUMBER(28,10), 
	"ACCRUING_DISCOUNT" NUMBER(28,10), 
	"STEPID" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PROGRAMNAME" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PROGRAMCEILING" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PROGRAMSTATUS" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"ISSUERNACE21SECTOR" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"INSTRUMENTQUOTATIONBASIS" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER38" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER39" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER40" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER41" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER42" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER43" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER44" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER45" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER46" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER47" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER48" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER49" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP", 
	"PLACEHOLDER50" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP"
   ) ;
   
   
--=============================================================================================================================
--Step 2: Create External Tables
--=============================================================================================================================

--Table CSDB_DEBT

--INBOX -For incoming files awaiting processing
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_INBOX',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'INBOX/CSDB/CentralizedSecuritiesDissemination/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvInboxBucketUri
    );
END;

--ODS - for processed files in operational data store
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_ODS',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'ODS/CSDB/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvDataBucketUri
    );
END; 
 
--ARCHIVE - For historical/archived files
BEGIN
    ODS.FILE_MANAGER_ODS.CREATE_EXTERNAL_TABLE(
        pTableName => 'CSDB_DEBT_ARCHIVE',
        pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
        pPrefix => 'ARCHIVE/CSDB/CSDB_DEBT',
        pBucketUri => CT_MRDS.ENV_MANAGER.gvArchiveBucketUri
    );
END;

--=============================================================================================================================
--Step 3: Date format adjustment
--=============================================================================================================================

--Table CSDB_DEBT
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'NEWUPDATED',    
	pDateFormat => 'dd/mm/yyyy'
	); 

CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'IDLOADDATE_DIM',    
	pDateFormat => 'dd/mm/yyyy'
	); 
	
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'IDISSUEDATE',    
	pDateFormat => 'dd/mm/yyyy'
	); 


CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'IDMATURITYDATE',    
	pDateFormat => 'dd/mm/yyyy'
	); 


CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'PUBLICATIONPRICEDATE',    
	pDateFormat => 'dd/mm/yyyy'
	); 
	
-- not data , set using other column model
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'ACCRUALSTARTDATE',    
	pDateFormat => 'dd/mm/yyyy'
	);

CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT(
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',    
	pColumnName => 'VA_SECURITYSTATUSDATE',    
	pDateFormat => 'dd/mm/yyyy'
	); 	
	
CALL CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT( 
	pTemplateTableName => 'CT_ET_TEMPLATES.CSDB_DEBT',
    pColumnName => 'COUPONDATE',
    pDateFormat => 'dd/mm/yyyy'
);
	
--=============================================================================================================================
--Step 4: Configure file type for processing
--=============================================================================================================================
   
 call CT_MRDS.FILE_MANAGER.ADD_SOURCE_FILE_CONFIG(
         pSourceKey             => 'CSDB'
        ,pSourceFileType        => 'INPUT'
        ,pSourceFileId          => 'CentralizedSecuritiesDissemination'
        ,pSourceFileDesc        => 'Table covers the monthly debt information (amount issued, outstanding amount, price, debt, coupon, maturity, etc) for an instrument.'
        ,pSourceFileNamePattern => '.*.csv'
        ,pTableId               => 'CSDB_DEBT'
        ,pTemplateTableName     => 'CT_ET_TEMPLATES.CSDB_DEBT' 
        );