ALTER TABLE ADR.ACTIVATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ACTIVATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.ACTIVATION
(
  ACTIVATION_ID             NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20),
  STD_MHDATASOURCE_ID       NUMBER(20),
  STD_NAMEDCONTINGENCY_ID   NUMBER(20),
  STD_TERMINATIONREASON_ID  NUMBER(20),
  STD_SPECIALOPERATION_ID   NUMBER(20),
  ACTIVATION_BEGIN_DATE     DATE,
  ACTIVATION_END_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(18)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ACTIVATION IS 'A period that a veteran was activated or mobilized to active duty.';

COMMENT ON COLUMN ADR.ACTIVATION.ACTIVATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ACTIVATION.STD_MHDATASOURCE_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.ACTIVATION.STD_NAMEDCONTINGENCY_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.ACTIVATION.STD_TERMINATIONREASON_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.ACTIVATION.STD_SPECIALOPERATION_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.ACTIVATION.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.ACTIVATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ACTIVATION.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.ACTIVATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ACTIVATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ACT_MHDS_FK_I ON ADR.ACTIVATION
(STD_MHDATASOURCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ACT_NC_FK_I ON ADR.ACTIVATION
(STD_NAMEDCONTINGENCY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ACT_PK ON ADR.ACTIVATION
(ACTIVATION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ACT_PRSN_FK_I ON ADR.ACTIVATION
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ACT_SO_FK_I ON ADR.ACTIVATION
(STD_SPECIALOPERATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ACT_TR_FK_I ON ADR.ACTIVATION
(STD_TERMINATIONREASON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ACTIVATION_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.ACTIVATION
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into ACTIVATION_H
        (
        ACTIVATION_H_ID,
        ACTIVATION_ID,
        PERSON_ID,
        STD_MHDATASOURCE_ID,
        STD_NAMEDCONTINGENCY_ID,
        STD_TERMINATIONREASON_ID,
        STD_SPECIALOPERATION_ID,
        ACTIVATION_BEGIN_DATE,
        ACTIVATION_END_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        ACTIVATION_H_S.nextval,
        :old.ACTIVATION_ID,
        :old.PERSON_ID,
        :old.STD_MHDATASOURCE_ID,
        :old.STD_NAMEDCONTINGENCY_ID,
        :old.STD_TERMINATIONREASON_ID,
        :old.STD_SPECIALOPERATION_ID,
        :old.ACTIVATION_BEGIN_DATE,
        :old.ACTIVATION_END_DATE,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into ACTIVATION_H
        (
        ACTIVATION_H_ID,
        ACTIVATION_ID,
        PERSON_ID,
        STD_MHDATASOURCE_ID,
        STD_NAMEDCONTINGENCY_ID,
        STD_TERMINATIONREASON_ID,
        STD_SPECIALOPERATION_ID,
        ACTIVATION_BEGIN_DATE,
        ACTIVATION_END_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        ACTIVATION_H_S.nextval,
        :new.ACTIVATION_ID,
        :new.PERSON_ID,
        :new.STD_MHDATASOURCE_ID,
        :new.STD_NAMEDCONTINGENCY_ID,
        :new.STD_TERMINATIONREASON_ID,
        :new.STD_SPECIALOPERATION_ID,
        :new.ACTIVATION_BEGIN_DATE,
        :new.ACTIVATION_END_DATE,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end ACTIVATION_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ACTIVATION FOR ADR.ACTIVATION;


ALTER TABLE ADR.ACTIVATION ADD (
  CONSTRAINT ACT_PK
  PRIMARY KEY
  (ACTIVATION_ID)
  USING INDEX ADR.ACT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ACTIVATION ADD (
  CONSTRAINT RELATIONSHIP43 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ACTIVATION TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.ACTIVATION TO ADR_RW;

GRANT SELECT ON ADR.ACTIVATION TO ES_R;

GRANT INSERT, SELECT ON ADR.ACTIVATION TO ES_RW;
ALTER TABLE ADR.ACTIVATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ACTIVATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ACTIVATION_H
(
  ACTIVATION_H_ID           NUMBER(20)          NOT NULL,
  ACTIVATION_ID             NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20),
  STD_SPECIALOPERATION_ID   NUMBER(20),
  STD_TERMINATIONREASON_ID  NUMBER(20),
  STD_NAMEDCONTINGENCY_ID   NUMBER(20),
  STD_MHDATASOURCE_ID       NUMBER(20),
  ACTIVATION_BEGIN_DATE     DATE,
  ACTIVATION_END_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(18)          NOT NULL,
  HISTORY_CREATED_DATE      DATE,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ACTIVATION_H IS 'A period that a veteran was activated or mobilized to active duty.';

COMMENT ON COLUMN ADR.ACTIVATION_H.ACTIVATION_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ACTIVATION_H.ACTIVATION_ID IS 'Unique identifier for an Activation';

COMMENT ON COLUMN ADR.ACTIVATION_H.PERSON_ID IS 'Person Identifier';

COMMENT ON COLUMN ADR.ACTIVATION_H.STD_SPECIALOPERATION_ID IS 'New SDS lookup';

COMMENT ON COLUMN ADR.ACTIVATION_H.STD_TERMINATIONREASON_ID IS 'New SDS lookup';

COMMENT ON COLUMN ADR.ACTIVATION_H.STD_NAMEDCONTINGENCY_ID IS 'New SDS lookup';

COMMENT ON COLUMN ADR.ACTIVATION_H.STD_MHDATASOURCE_ID IS 'New SDS lookup';

COMMENT ON COLUMN ADR.ACTIVATION_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.ACTIVATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ACTIVATION_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.ACTIVATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ACTIVATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.ACTIVATION_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.ACT_H_PERSON_ID ON ADR.ACTIVATION_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ACT_H_PK ON ADR.ACTIVATION_H
(ACTIVATION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ACTIVATION_H FOR ADR.ACTIVATION_H;


ALTER TABLE ADR.ACTIVATION_H ADD (
  CONSTRAINT AVCON_1252960469_TRANS_001
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT ACT_H_PK
  PRIMARY KEY
  (ACTIVATION_H_ID)
  USING INDEX ADR.ACT_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ACTIVATION_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.ACTIVATION_H TO ADR_RW;

GRANT SELECT ON ADR.ACTIVATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ACTIVATION_H TO ES_RW;
DROP TABLE ADR.ACTIVEAGG CASCADE CONSTRAINTS;

CREATE TABLE ADR.ACTIVEAGG
(
  PERID                 VARCHAR2(11 BYTE),
  ICN                   VARCHAR2(29 BYTE),
  EDIPI                 VARCHAR2(20 BYTE),
  SSN                   VARCHAR2(9 BYTE),
  LN                    VARCHAR2(35 BYTE),
  FN                    VARCHAR2(25 BYTE),
  MN                    VARCHAR2(25 BYTE),
  SUF                   VARCHAR2(10 BYTE),
  DOB                   VARCHAR2(8 BYTE),
  DOD                   VARCHAR2(14 BYTE),
  GENDER                VARCHAR2(1 BYTE),
  ENR_DTE               VARCHAR2(8 BYTE),
  CV_ELIG               VARCHAR2(8 BYTE),
  ELIG_VERIFIED         VARCHAR2(1 BYTE),
  ENR_CODE              VARCHAR2(2 BYTE),
  PRIORITY              VARCHAR2(2 BYTE),
  ENR_CRE_DTE           VARCHAR2(8 BYTE),
  ADD_TYPE              VARCHAR2(1 BYTE),
  BAI                   VARCHAR2(1 BYTE),
  ADDRESS_ID            VARCHAR2(11 BYTE),
  ST1                   VARCHAR2(70 BYTE),
  ST2                   VARCHAR2(70 BYTE),
  ST3                   VARCHAR2(70 BYTE),
  CITY                  VARCHAR2(60 BYTE),
  STATE                 VARCHAR2(5 BYTE),
  ZIP                   VARCHAR2(5 BYTE),
  ZIP4                  VARCHAR2(4 BYTE),
  COUNTRY               VARCHAR2(3 BYTE),
  ADT                   VARCHAR2(14 BYTE),
  PFA                   VARCHAR2(25 BYTE),
  PCF                   VARCHAR2(10 BYTE),
  HPN                   VARCHAR2(35 BYTE),
  CPN                   VARCHAR2(25 BYTE),
  WPN                   VARCHAR2(25 BYTE),
  APN                   VARCHAR2(25 BYTE),
  TSTART                VARCHAR2(8 BYTE),
  TEND                  VARCHAR2(8 BYTE),
  PF_NO                 VARCHAR2(6 BYTE),
  PF_NAME               VARCHAR2(80 BYTE),
  VCE                   VARCHAR2(3 BYTE),
  SCI                   VARCHAR2(1 BYTE),
  SC_PCT                VARCHAR2(3 BYTE),
  SAI                   VARCHAR2(1 BYTE),
  SALIST                VARCHAR2(25 BYTE),
  NSCI                  VARCHAR2(1 BYTE),
  NLE_ES                VARCHAR2(1 BYTE),
  STA_VISN              VARCHAR2(2 BYTE),
  STA_NO                VARCHAR2(6 BYTE),
  STA_NAME              VARCHAR2(75 BYTE),
  EE_FLAG               VARCHAR2(1 BYTE),
  DISTANCE              VARCHAR2(21 BYTE),
  OPP_FLAG              VARCHAR2(1 BYTE),
  ADR_LON               VARCHAR2(20 BYTE),
  ADR_LAT               VARCHAR2(20 BYTE),
  STA_ST1               VARCHAR2(70 BYTE),
  STA_ST2               VARCHAR2(50 BYTE),
  STA_ST3               VARCHAR2(50 BYTE),
  STA_CITY              VARCHAR2(25 BYTE),
  STA_STATE             VARCHAR2(2 BYTE),
  STA_ZIP               VARCHAR2(5 BYTE),
  STA_ZIP4              VARCHAR2(4 BYTE),
  STA_LON               VARCHAR2(20 BYTE),
  STA_LAT               VARCHAR2(20 BYTE),
  LOC_NAME              VARCHAR2(30 BYTE),
  GEO_DATE              VARCHAR2(8 BYTE),
  SOFTWARE_VER          VARCHAR2(50 BYTE),
  STATUS                VARCHAR2(1 BYTE),
  STATUS_DATE           VARCHAR2(1 BYTE),
  MAILING_EXCEPTION     VARCHAR2(1 BYTE),
  TIME_COHORT           VARCHAR2(1 BYTE),
  BASIC_ELIG            VARCHAR2(1 BYTE),
  GEO_TIME_FRAME_BEGIN  VARCHAR2(14 BYTE),
  GEO_TIME_FRAME_END    VARCHAR2(14 BYTE),
  WT_RUN_TIME           VARCHAR2(14 BYTE),
  VBA_DOD               VARCHAR2(8 BYTE),
  EVER_GEO_CODED        INTEGER,
  CALC_TYPE             VARCHAR2(2 BYTE),
  DD_BASIC_ELIG         VARCHAR2(1 BYTE),
  DD_NLE_ES             VARCHAR2(1 BYTE),
  VISN                  VARCHAR2(2 BYTE),
  URH                   VARCHAR2(1 BYTE),
  FIPS                  VARCHAR2(5 BYTE),
  MARKET                VARCHAR2(4 BYTE),
  SUBMARKET             VARCHAR2(6 BYTE),
  SECTOR                VARCHAR2(8 BYTE),
  STATE_FIPS            VARCHAR2(2 BYTE),
  S_CLASS_NAME          VARCHAR2(20 BYTE),
  OUTPATIENTRATING      VARCHAR2(25 BYTE),
  PAR_STA_NO            VARCHAR2(6 BYTE),
  PARENTSTATIONCL       VARCHAR2(80 BYTE),
  SH_PAR_STA            VARCHAR2(6 BYTE),
  VA_SITE               VARCHAR2(1 BYTE),
  ST                    VARCHAR2(2 BYTE),
  POD                   VARCHAR2(1 BYTE),
  SHAR                  VARCHAR2(1 BYTE),
  HCC                   NUMBER(3),
  MSCBOC                NUMBER(3),
  NEWVAMC               NUMBER(3),
  PCCBOC                NUMBER(3),
  COCCLASSIFICATIONID   NUMBER(3),
  VAH                   NUMBER(3),
  CD                    VARCHAR2(20 BYTE),
  CONT                  VARCHAR2(1 BYTE),
  PROV                  INTEGER,
  VACAA_LIST_DATE       DATE,
  NLE_ES_DATE           VARCHAR2(14 BYTE),
  COUNTY_NAME           VARCHAR2(50 BYTE),
  MARKET_NAME           VARCHAR2(50 BYTE),
  DATASOURCE            VARCHAR2(10 BYTE),
  DISTRICT              VARCHAR2(2 BYTE),
  STA_DISTRICT_VISN     VARCHAR2(5 BYTE),
  RECORD_CREATED_TS     DATE                    DEFAULT TRUNC (SYSDATE)       NOT NULL,
  WT_INFO_CONCATTED     CLOB
)
LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
  TABLESPACE  L_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  NOCACHE
  LOGGING)
NOCOMPRESS 
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOLOGGING
PARTITION BY RANGE (RECORD_CREATED_TS)
INTERVAL( NUMTODSINTERVAL (1, 'DAY'))
(  
  PARTITION P1 VALUES LESS THAN (TO_DATE(' 2016-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_01
    LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
      TABLESPACE  L_DATA_01
      ENABLE      STORAGE IN ROW
      CHUNK       8192
      RETENTION
      NOCACHE
      LOGGING)
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-27 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_01
    LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
      TABLESPACE  L_DATA_01
      ENABLE      STORAGE IN ROW
      CHUNK       8192
      RETENTION
      NOCACHE
      LOGGING
          STORAGE    (
                      INITIAL          128M
                      NEXT             128M
                      MINEXTENTS       1
                      MAXEXTENTS       UNLIMITED
                      PCTINCREASE      0
                      BUFFER_POOL      DEFAULT
                      FLASH_CACHE      DEFAULT
                      CELL_FLASH_CACHE DEFAULT
                     ))
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-31 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_01
    LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
      TABLESPACE  L_DATA_01
      ENABLE      STORAGE IN ROW
      CHUNK       8192
      RETENTION
      NOCACHE
      LOGGING
          STORAGE    (
                      INITIAL          128M
                      NEXT             128M
                      MINEXTENTS       1
                      MAXEXTENTS       UNLIMITED
                      PCTINCREASE      0
                      BUFFER_POOL      DEFAULT
                      FLASH_CACHE      DEFAULT
                      CELL_FLASH_CACHE DEFAULT
                     ))
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_01
    LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
      TABLESPACE  L_DATA_01
      ENABLE      STORAGE IN ROW
      CHUNK       8192
      RETENTION
      NOCACHE
      LOGGING
          STORAGE    (
                      INITIAL          128M
                      NEXT             128M
                      MINEXTENTS       1
                      MAXEXTENTS       UNLIMITED
                      PCTINCREASE      0
                      BUFFER_POOL      DEFAULT
                      FLASH_CACHE      DEFAULT
                      CELL_FLASH_CACHE DEFAULT
                     ))
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_01
    LOB (WT_INFO_CONCATTED) STORE AS SECUREFILE (
      TABLESPACE  L_DATA_01
      ENABLE      STORAGE IN ROW
      CHUNK       8192
      RETENTION
      NOCACHE
      LOGGING
          STORAGE    (
                      INITIAL          128M
                      NEXT             128M
                      MINEXTENTS       1
                      MAXEXTENTS       UNLIMITED
                      PCTINCREASE      0
                      BUFFER_POOL      DEFAULT
                      FLASH_CACHE      DEFAULT
                      CELL_FLASH_CACHE DEFAULT
                     ))
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;


CREATE INDEX ADR.ACTIVEAGG_PERID_NU ON ADR.ACTIVEAGG
(PERID)
  TABLESPACE M_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
NOLOGGING
LOCAL (  
  PARTITION P1
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          4M
                NEXT             4M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ACTIVEAGG FOR ADR.ACTIVEAGG;


GRANT SELECT ON ADR.ACTIVEAGG TO ADR_R;

GRANT DELETE, INDEX, INSERT, SELECT, UPDATE ON ADR.ACTIVEAGG TO EEUSR;

GRANT SELECT ON ADR.ACTIVEAGG TO VETCHOICEUSR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ACTIVEAGG TO VETCHOICEUSR_RW;
ALTER TABLE ADR.ACTIVEAGG_TRACKER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ACTIVEAGG_TRACKER CASCADE CONSTRAINTS;

CREATE TABLE ADR.ACTIVEAGG_TRACKER
(
  ACTIVEAGG_TRACKER_ID  NUMBER(20)              NOT NULL,
  LOADING_STATUS        VARCHAR2(20 BYTE)       DEFAULT 'NO',
  LOG_MESSAGE           VARCHAR2(500 BYTE),
  PREVIOUS_ACTIVE_DATE  DATE                    DEFAULT TRUNC (SYSDATE)       NOT NULL,
  CURRENT_ACTIVE_DATE   DATE                    DEFAULT TRUNC (SYSDATE)       NOT NULL,
  RECORD_CREATED_TS     DATE                    DEFAULT SYSDATE               NOT NULL,
  RECORD_MODIFIED_TS    DATE                    DEFAULT SYSDATE               NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ACTIVEAGG_TRACKER IS 'Stores all metadata required for the OPP extract such TEMP table names and keeps track what to execute and when along with loading times';

COMMENT ON COLUMN ADR.ACTIVEAGG_TRACKER.LOG_MESSAGE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ACTIVEAGG_TRACKER.PREVIOUS_ACTIVE_DATE IS 'Previous active day for which data is made available for users';

COMMENT ON COLUMN ADR.ACTIVEAGG_TRACKER.CURRENT_ACTIVE_DATE IS 'current active day for which data is loaded into table';

COMMENT ON COLUMN ADR.ACTIVEAGG_TRACKER.RECORD_CREATED_TS IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ACTIVEAGG_TRACKER.RECORD_MODIFIED_TS IS 'Date of this modification to the record.';


CREATE UNIQUE INDEX ADR.ACTIVEAGG_TRACKER_PK ON ADR.ACTIVEAGG_TRACKER
(ACTIVEAGG_TRACKER_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ACTIVEAGG_TRACKER_BUR_TRGR
   BEFORE UPDATE
   ON ADR.ACTIVEAGG_TRACKER
   REFERENCING NEW AS New OLD AS Old
   FOR EACH ROW
DECLARE
BEGIN

   :new.RECORD_MODIFIED_TS := SYSDATE;
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (SUBSTR (   DBMS_UTILITY.FORMAT_CALL_STACK
                                    || DBMS_UTILITY.format_error_stack
                                    || DBMS_UTILITY.format_error_backtrace,
                                    1,
                                    2000));
END ACTIVEAGG_TRACKER_BUR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM ACTIVEAGG_TRACKER FOR ADR.ACTIVEAGG_TRACKER;


ALTER TABLE ADR.ACTIVEAGG_TRACKER ADD (
  CONSTRAINT LOADING_STATUS_CHK
  CHECK (LOADING_STATUS IN ('YES','FAIL'))
  ENABLE VALIDATE,
  CONSTRAINT APP_LOG_PK
  PRIMARY KEY
  (ACTIVEAGG_TRACKER_ID)
  USING INDEX ADR.ACTIVEAGG_TRACKER_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ACTIVEAGG_TRACKER TO ADR_R;

GRANT SELECT, UPDATE ON ADR.ACTIVEAGG_TRACKER TO EEUSR;

GRANT SELECT ON ADR.ACTIVEAGG_TRACKER TO VETCHOICEUSR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ACTIVEAGG_TRACKER TO VETCHOICEUSR_RW;
ALTER TABLE ADR.ADDRESS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ADDRESS CASCADE CONSTRAINTS;

CREATE TABLE ADR.ADDRESS
(
  ADDRESS_ID                     NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INSURANCE_ID                   NUMBER(20),
  STD_ADDRESSTYPE_ID             NUMBER(20),
  ADDRESS_CHANGE_SRC_TYPE_ID     NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  ADDRESS_INVALID_TYPE_ID        NUMBER(20),
  NCOA_STATUS_TYPE_ID            NUMBER(20),
  NCOA_DELIVERY_TYPE_ID          NUMBER(20),
  NCOA_ZIP_MATCH_TYPE_ID         NUMBER(20),
  NCOA_ZIP_NO_MATCH_TYPE_ID      NUMBER(20),
  ADDRESS_LINE1                  VARCHAR2(100 BYTE),
  ADDRESS_LINE2                  VARCHAR2(100 BYTE),
  ADDRESS_LINE3                  VARCHAR2(100 BYTE),
  CITY                           VARCHAR2(60 BYTE),
  STATE_CODE                     VARCHAR2(50 BYTE),
  COUNTY_CODE                    VARCHAR2(50 BYTE),
  PROVINCE_CODE                  VARCHAR2(50 BYTE),
  ZIP_CODE                       VARCHAR2(20 BYTE),
  ZIP_PLUS_4                     VARCHAR2(10 BYTE),
  POSTAL_CODE                    VARCHAR2(20 BYTE),
  COUNTRY_CODE                   VARCHAR2(50 BYTE),
  ADDRESS_CHANGE_EFFECTIVE_DATE  DATE,
  ADDRESS_INVALIDATED_DATE       DATE,
  NCOA_ADDRESS_CHECK_DATE        DATE,
  ADDRESS_START_DATE_TXT         VARCHAR2(14 BYTE),
  ADDRESS_END_DATE_TXT           VARCHAR2(14 BYTE),
  ADDRESS_PHONE_NUMBER           VARCHAR2(30 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  RECORD_MODIFIED_SRC_NAME       VARCHAR2(30 BYTE),
  PERSON_RELATION_ID             NUMBER(20),
  CASSCERTIFIEDTYPE_ID           NUMBER(20),
  CASS_CERTIFIED_DATE            DATE,
  PURPOSE_OF_USE_TYPE_ID         NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ADDRESS IS 'The ADDRESS table contains the components of primary, temporary, and confidential addresses for persons and addresses for insurance companies. This table also contains data used in the checking of values with National Change of Address (NCOA). Maintains all types of addresses for Persons. Also stores Insurance Company address. Foreign Keys present in table: PERSON_ID INSURANCE_ID ADDRESS_TYPE_ID (Types of addresses applicable to persons - Residential, Business etc.) ADDRESS_CHANGE_SRC_TYPE_ID (Source of the address change such as HEC, NCOA etc.) STD_INSTITUTION_ID (Site making the address change) ADDRESS_INVALID_TYPE_ID (Why the address cannot be used - Homeless, Undeliverable etc.) NCOA_STATUS_TYPE_ID ( Sent to NCOA, Not sent or Checked by NCOA) NCOA_DELIVERY_TYPE_ID ( Address good No Address Change, Foreign move etc.) NCOA_ZIP_MATCH_TYPE_ID (Building/High-Rise, Rural Route etc.) NCOA_ZIP_NO_MATCH_TYPE_ID DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ADDRESS.PERSON_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.INSURANCE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.STD_ADDRESSTYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_CHANGE_SRC_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_INVALID_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.NCOA_STATUS_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.NCOA_DELIVERY_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.NCOA_ZIP_MATCH_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.NCOA_ZIP_NO_MATCH_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS.CITY IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ADDRESS.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5- character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied. State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.ADDRESS.COUNTY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ADDRESS.PROVINCE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ADDRESS.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS.ZIP_PLUS_4 IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS.POSTAL_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS.COUNTRY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_CHANGE_EFFECTIVE_DATE IS 'Date on which a change of address went into effect.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_INVALIDATED_DATE IS 'Date on which an address was determined to be invalid.';

COMMENT ON COLUMN ADR.ADDRESS.NCOA_ADDRESS_CHECK_DATE IS 'Date on which an address was checked against NCOA. Date address checked with NCOA is defined as a valid date that is set by the AAC once the NCOA check is completed.  For the records that are returned to ADR via the NCOA Address check file - this date will be the NCOA Date Checked contained on that file. For Records returned via an address change this date will be the date the Z05 (address source = NCOA) data was uploaded on the ADR.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_START_DATE_TXT IS 'Address start date is defined as the date an address became valid to use for a party. Address start date can be a future date. Address becomes active/ current on the start date.';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_END_DATE_TXT IS 'Address end date is defined as the date an address stopped being used for a party. Address end date cannot be before address start date. Address end date can be a future date. Address becomes inactive on the end date. ';

COMMENT ON COLUMN ADR.ADDRESS.ADDRESS_PHONE_NUMBER IS 'Telephone number associated with a temporary address.';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.ADDRESS.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.ADDRESS.PERSON_RELATION_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS.PURPOSE_OF_USE_TYPE_ID IS 'Foreign Key on ADR.PURPOSE_OF_USE_TYPE';


CREATE INDEX ADR.ADDR_INSURANCE_FK_I ON ADR.ADDRESS
(INSURANCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ADDR_PK ON ADR.ADDRESS
(ADDRESS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_PRSNREL_FK_I ON ADR.ADDRESS
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_PRSN_FK_I ON ADR.ADDRESS
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STDADDRTYPE_FK_I ON ADR.ADDRESS
(STD_ADDRESSTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STDINST_FK_I ON ADR.ADDRESS
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_ADDRCHGSRC_FK_I ON ADR.ADDRESS
(ADDRESS_CHANGE_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_ADDRDELFLR_FK_I ON ADR.ADDRESS
(ADDRESS_INVALID_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_NCOADELTYP_FK_I ON ADR.ADDRESS
(NCOA_DELIVERY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_NCOASTATYP_FK_I ON ADR.ADDRESS
(NCOA_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_NCOAZIPMCH_FK_I ON ADR.ADDRESS
(NCOA_ZIP_MATCH_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDR_STD_NCOAZIPNOMTCH_FK_I ON ADR.ADDRESS
(NCOA_ZIP_NO_MATCH_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ADDRESS_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON ADR.ADDRESS FOR EACH ROW
declare
    mDML char(1);
begin

  if DELETING then
    insert into ADDRESS_H
    (
    ADDRESS_H_ID,
    ADDRESS_ID  ,
    PERSON_ID   ,
    INSURANCE_ID,
    STD_ADDRESSTYPE_ID,
    ADDRESS_CHANGE_SRC_TYPE_ID,
    STD_INSTITUTION_ID        ,
    ADDRESS_INVALID_TYPE_ID   ,
    NCOA_STATUS_TYPE_ID       ,
    NCOA_DELIVERY_TYPE_ID     ,
    NCOA_ZIP_MATCH_TYPE_ID    ,
    ADDRESS_LINE1             ,
    ADDRESS_LINE2             ,
    ADDRESS_LINE3             ,
    CITY                      ,
    STATE_CODE                ,
    COUNTY_CODE               ,
    PROVINCE_CODE             ,
    POSTAL_CODE               ,
    ZIP_PLUS_4                ,
    COUNTRY_CODE              ,
    ADDRESS_CHANGE_EFFECTIVE_DATE,
    ADDRESS_INVALIDATED_DATE  ,
    NCOA_ADDRESS_CHECK_DATE   ,
    ADDRESS_START_DATE_TXT   ,
    ADDRESS_END_DATE_TXT     ,
    RECORD_CREATED_BY         ,
    RECORD_CREATED_DATE       ,
    RECORD_MODIFIED_BY        ,
    RECORD_MODIFIED_DATE      ,
    RECORD_MODIFIED_COUNT     ,
    ADDRESS_PHONE_NUMBER,
    ZIP_CODE                  ,
    NCOA_ZIP_NO_MATCH_TYPE_ID ,
    RECORD_MODIFIED_SRC_NAME,
    PERSON_RELATION_ID,
    CASSCERTIFIEDTYPE_ID,
    CASS_CERTIFIED_DATE,
    purpose_of_use_type_id,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    ADDRESS_h_s.nextval,
    :old.ADDRESS_ID  ,
    :old.PERSON_ID   ,
    :old.INSURANCE_ID,
    :old.STD_ADDRESSTYPE_ID,
    :old.ADDRESS_CHANGE_SRC_TYPE_ID,
    :old.STD_INSTITUTION_ID        ,
    :old.ADDRESS_INVALID_TYPE_ID   ,
    :old.NCOA_STATUS_TYPE_ID       ,
    :old.NCOA_DELIVERY_TYPE_ID     ,
    :old.NCOA_ZIP_MATCH_TYPE_ID    ,
    :old.ADDRESS_LINE1             ,
    :old.ADDRESS_LINE2             ,
    :old.ADDRESS_LINE3             ,
    :old.CITY                      ,
    :old.STATE_CODE                ,
    :old.COUNTY_CODE               ,
    :old.PROVINCE_CODE             ,
    :old.POSTAL_CODE               ,
    :old.ZIP_PLUS_4                ,
    :old.COUNTRY_CODE              ,
    :old.ADDRESS_CHANGE_EFFECTIVE_DATE,
    :old.ADDRESS_INVALIDATED_DATE  ,
    :old.NCOA_ADDRESS_CHECK_DATE   ,
    :old.ADDRESS_START_DATE_TXT   ,
    :old.ADDRESS_END_DATE_TXT     ,
    :old.RECORD_CREATED_BY         ,
    :old.RECORD_CREATED_DATE       ,
    :old.RECORD_MODIFIED_BY        ,
    :old.RECORD_MODIFIED_DATE      ,
    :old.RECORD_MODIFIED_COUNT     ,
    :old.ADDRESS_PHONE_NUMBER,
    :old.ZIP_CODE                  ,
    :old.NCOA_ZIP_NO_MATCH_TYPE_ID ,
    :old.RECORD_MODIFIED_SRC_NAME,
    :old.PERSON_RELATION_ID,
    :old.CASSCERTIFIEDTYPE_ID,
    :old.CASS_CERTIFIED_DATE,
    :old.purpose_of_use_type_id,
    'D'
    );
  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

    insert into ADDRESS_H
    (
    ADDRESS_H_ID,
    ADDRESS_ID  ,
    PERSON_ID   ,
    INSURANCE_ID,
    STD_ADDRESSTYPE_ID,
    ADDRESS_CHANGE_SRC_TYPE_ID,
    STD_INSTITUTION_ID        ,
    ADDRESS_INVALID_TYPE_ID   ,
    NCOA_STATUS_TYPE_ID       ,
    NCOA_DELIVERY_TYPE_ID     ,
    NCOA_ZIP_MATCH_TYPE_ID    ,
    ADDRESS_LINE1             ,
    ADDRESS_LINE2             ,
    ADDRESS_LINE3             ,
    CITY                      ,
    STATE_CODE                ,
    COUNTY_CODE               ,
    PROVINCE_CODE             ,
    POSTAL_CODE               ,
    ZIP_PLUS_4                ,
    COUNTRY_CODE              ,
    ADDRESS_CHANGE_EFFECTIVE_DATE,
    ADDRESS_INVALIDATED_DATE  ,
    NCOA_ADDRESS_CHECK_DATE   ,
    ADDRESS_START_DATE_TXT   ,
    ADDRESS_END_DATE_TXT     ,
    RECORD_CREATED_BY         ,
    RECORD_CREATED_DATE       ,
    RECORD_MODIFIED_BY        ,
    RECORD_MODIFIED_DATE      ,
    RECORD_MODIFIED_COUNT     ,
    ADDRESS_PHONE_NUMBER,
    ZIP_CODE                  ,
    NCOA_ZIP_NO_MATCH_TYPE_ID ,
    RECORD_MODIFIED_SRC_NAME,
    PERSON_RELATION_ID,
    CASSCERTIFIEDTYPE_ID,
    CASS_CERTIFIED_DATE,
    purpose_of_use_type_id,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    ADDRESS_h_s.nextval,
    :new.ADDRESS_ID  ,
    :new.PERSON_ID   ,
    :new.INSURANCE_ID,
    :new.STD_ADDRESSTYPE_ID,
    :new.ADDRESS_CHANGE_SRC_TYPE_ID,
    :new.STD_INSTITUTION_ID        ,
    :new.ADDRESS_INVALID_TYPE_ID   ,
    :new.NCOA_STATUS_TYPE_ID       ,
    :new.NCOA_DELIVERY_TYPE_ID     ,
    :new.NCOA_ZIP_MATCH_TYPE_ID    ,
    :new.ADDRESS_LINE1             ,
    :new.ADDRESS_LINE2             ,
    :new.ADDRESS_LINE3             ,
    :new.CITY                      ,
    :new.STATE_CODE                ,
    :new.COUNTY_CODE               ,
    :new.PROVINCE_CODE             ,
    :new.POSTAL_CODE               ,
    :new.ZIP_PLUS_4                ,
    :new.COUNTRY_CODE              ,
    :new.ADDRESS_CHANGE_EFFECTIVE_DATE,
    :new.ADDRESS_INVALIDATED_DATE  ,
    :new.NCOA_ADDRESS_CHECK_DATE   ,
    :new.ADDRESS_START_DATE_TXT   ,
    :new.ADDRESS_END_DATE_TXT     ,
    :new.RECORD_CREATED_BY         ,
    :new.RECORD_CREATED_DATE       ,
    :new.RECORD_MODIFIED_BY        ,
    :new.RECORD_MODIFIED_DATE      ,
    :new.RECORD_MODIFIED_COUNT     ,
    :new.ADDRESS_PHONE_NUMBER,
    :new.ZIP_CODE                  ,
    :new.NCOA_ZIP_NO_MATCH_TYPE_ID ,
    :new.RECORD_MODIFIED_SRC_NAME,
    :new.PERSON_RELATION_ID,
    :new.CASSCERTIFIEDTYPE_ID,
    :new.CASS_CERTIFIED_DATE,
    :new.purpose_of_use_type_id,
    mDML
    );
  end if;

end ADDRESS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ADDRESS FOR ADR.ADDRESS;


ALTER TABLE ADR.ADDRESS ADD (
  CONSTRAINT SYS_C0067422
  CHECK ("ADDRESS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067423
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067424
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067425
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067426
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067427
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_PK
  PRIMARY KEY
  (ADDRESS_ID)
  USING INDEX ADR.ADDR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ADDRESS ADD (
  CONSTRAINT ADDR_CASSCERTIFIEDTY_FK 
  FOREIGN KEY (CASSCERTIFIEDTYPE_ID) 
  REFERENCES ADR.CASSCERTIFIEDTYPE (CASSCERTIFIEDTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_INSURANCE_FK 
  FOREIGN KEY (INSURANCE_ID) 
  REFERENCES ADR.INSURANCE (INSURANCE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_PRSNREL_FK 
  FOREIGN KEY (PERSON_RELATION_ID) 
  REFERENCES ADR.PERSON_RELATION (PERSON_RELATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_STD_NCOADELTYP_FK 
  FOREIGN KEY (NCOA_DELIVERY_TYPE_ID) 
  REFERENCES ADRREF.STD_NCOADELIVERYTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_STD_NCOASTATYP_FK 
  FOREIGN KEY (NCOA_STATUS_TYPE_ID) 
  REFERENCES ADRREF.STD_NCOASTATUSTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_STD_NCOAZIPMCH_FK 
  FOREIGN KEY (NCOA_ZIP_MATCH_TYPE_ID) 
  REFERENCES ADRREF.STD_NCOAZIPMATCHTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT ADDR_STD_NCOAZIPNOMTCH_FK 
  FOREIGN KEY (NCOA_ZIP_NO_MATCH_TYPE_ID) 
  REFERENCES ADRREF.STD_NCOAZIPNOMATCHTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT PURPOSE_OF_USE_FK 
  FOREIGN KEY (PURPOSE_OF_USE_TYPE_ID) 
  REFERENCES ADR.PURPOSE_OF_USE_TYPE (PURPOSE_OF_USE_TYPE_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ADDRESS TO ADREXT;

GRANT SELECT ON ADR.ADDRESS TO ADRTMP;

GRANT SELECT ON ADR.ADDRESS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ADDRESS TO ADR_RW;

GRANT INSERT, SELECT, UPDATE ON ADR.ADDRESS TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.ADDRESS TO ES_R;

GRANT INSERT, SELECT ON ADR.ADDRESS TO ES_RW;

GRANT SELECT ON ADR.ADDRESS TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.ADDRESS TO PSIM_ADR_RW;
ALTER TABLE ADR.ADDRESS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ADDRESS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ADDRESS_H
(
  ADDRESS_H_ID                   NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  ADDRESS_ID                     NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INSURANCE_ID                   NUMBER(20),
  STD_ADDRESSTYPE_ID             NUMBER(20),
  ADDRESS_CHANGE_SRC_TYPE_ID     NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  ADDRESS_INVALID_TYPE_ID        NUMBER(20),
  NCOA_STATUS_TYPE_ID            NUMBER(20),
  NCOA_DELIVERY_TYPE_ID          NUMBER(20),
  NCOA_ZIP_MATCH_TYPE_ID         NUMBER(20),
  NCOA_ZIP_NO_MATCH_TYPE_ID      NUMBER(20),
  NCOA_ADDRESS_CHECK_DATE        DATE,
  ADDRESS_LINE1                  VARCHAR2(100 BYTE),
  ADDRESS_LINE2                  VARCHAR2(100 BYTE),
  ADDRESS_LINE3                  VARCHAR2(100 BYTE),
  CITY                           VARCHAR2(60 BYTE),
  STATE_CODE                     VARCHAR2(50 BYTE),
  COUNTY_CODE                    VARCHAR2(50 BYTE),
  PROVINCE_CODE                  VARCHAR2(50 BYTE),
  ZIP_CODE                       VARCHAR2(20 BYTE),
  ZIP_PLUS_4                     VARCHAR2(10 BYTE),
  POSTAL_CODE                    VARCHAR2(20 BYTE),
  COUNTRY_CODE                   VARCHAR2(50 BYTE),
  ADDRESS_CHANGE_EFFECTIVE_DATE  DATE,
  ADDRESS_INVALIDATED_DATE       DATE,
  ADDRESS_START_DATE_TXT         VARCHAR2(14 BYTE),
  ADDRESS_END_DATE_TXT           VARCHAR2(14 BYTE),
  ADDRESS_PHONE_NUMBER           VARCHAR2(30 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  RECORD_MODIFIED_SRC_NAME       VARCHAR2(30 BYTE),
  PERSON_RELATION_ID             NUMBER(20),
  CASSCERTIFIEDTYPE_ID           NUMBER(20),
  CASS_CERTIFIED_DATE            DATE,
  PURPOSE_OF_USE_TYPE_ID         NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ADDRESS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ADDRESS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.PERSON_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.INSURANCE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.STD_ADDRESSTYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_CHANGE_SRC_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.STD_INSTITUTION_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_INVALID_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.NCOA_STATUS_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.NCOA_DELIVERY_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.NCOA_ZIP_MATCH_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.NCOA_ZIP_NO_MATCH_TYPE_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';

COMMENT ON COLUMN ADR.ADDRESS_H.NCOA_ADDRESS_CHECK_DATE IS 'Date on which an address was checked against NCOA. Date address checked with NCOA is defined as a valid date that is set by the AAC once the NCOA check is completed.  For the records that are returned to ADR via the NCOA Address check file - this date will be the NCOA Date Checked contained on that file. For Records returned via an address change this date will be the date the Z05 (address source = NCOA) data was uploaded on the ADR.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.ADDRESS_H.CITY IS 'City is defined as the name of a city used for an address.';

COMMENT ON COLUMN ADR.ADDRESS_H.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5- character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied. State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.ADDRESS_H.COUNTY_CODE IS 'FIPS County CODE is defined as the last 3 characters of the FIPS state/county code.  The FIPS county code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied 5-DIGIT FIPS CODE is defined as the 2 digit state code + 3 digit county code. This 5 digit FIPS code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied';

COMMENT ON COLUMN ADR.ADDRESS_H.PROVINCE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ADDRESS_H.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS_H.ZIP_PLUS_4 IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS_H.POSTAL_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.ADDRESS_H.COUNTRY_CODE IS 'Country is defined as the unique identifier assigned by the International Organization of Standardization for a country.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_CHANGE_EFFECTIVE_DATE IS 'Date on which a change of address went into effect.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_INVALIDATED_DATE IS 'Date on which an address was determined to be invalid.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_START_DATE_TXT IS 'Address start date is defined as the date an address became valid to use for a party. Address start date can be a future date. Address becomes active/ current on the start date.';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_END_DATE_TXT IS 'Address end date is defined as the date an address stopped being used for a party. Address end date cannot be before address start date. Address end date can be a future date. Address becomes inactive on the end date. ';

COMMENT ON COLUMN ADR.ADDRESS_H.ADDRESS_PHONE_NUMBER IS 'Telephone number associated with a temporary address.';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n. history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ADDRESS_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.ADDRESS_H.PERSON_RELATION_ID IS 'The Foriegn key, generated by the database system in numeric form, used to uniquely identify a data record in the related table.';


CREATE INDEX ADR.ADDRESS_ID_NU_I ON ADR.ADDRESS_H
(ADDRESS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE UNIQUE INDEX ADR.ADDRH_PK ON ADR.ADDRESS_H
(ADDRESS_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDRH_PRSNID_NU_I ON ADR.ADDRESS_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ADDRESS_H FOR ADR.ADDRESS_H;


ALTER TABLE ADR.ADDRESS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_062
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067248
  CHECK ("ADDRESS_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067249
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067250
  CHECK ("ADDRESS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067251
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067252
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067253
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067254
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067255
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ADDRH_PK
  PRIMARY KEY
  (ADDRESS_H_ID)
  USING INDEX ADR.ADDRH_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ADDRESS_H ADD (
  CONSTRAINT ADDR_H_CASSCERTIFIEDTY_FK 
  FOREIGN KEY (CASSCERTIFIEDTYPE_ID) 
  REFERENCES ADR.CASSCERTIFIEDTYPE (CASSCERTIFIEDTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ADDRESS_H TO ADR_R;

GRANT SELECT ON ADR.ADDRESS_H TO ADR_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ADDRESS_H TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.ADDRESS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ADDRESS_H TO ES_RW;

GRANT SELECT ON ADR.ADDRESS_H TO PSIM_ADR_R;
ALTER TABLE ADR.ADDR_CONFIDENTIAL_DTL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ADDR_CONFIDENTIAL_DTL CASCADE CONSTRAINTS;

CREATE TABLE ADR.ADDR_CONFIDENTIAL_DTL
(
  ADDR_CONFIDENTIAL_DTL_ID       NUMBER(20)     NOT NULL,
  ADDRESS_ID                     NUMBER(20)     NOT NULL,
  STD_CONFIDENTIALADDRESSCAT_ID  NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ADDR_CONFIDENTIAL_DTL IS 'Person Service Demographics (PSD) concept of confidential address is different from ESR: PSD wants to track whether or not a Person  record is sensitive and has a confidential address vs. ESR just marks an address as being confidential';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.ADDR_CONFIDENTIAL_DTL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.STD_CONFIDENTIALADDRESSCAT_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ADDRCONF_ADDR_FK_I ON ADR.ADDR_CONFIDENTIAL_DTL
(ADDRESS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ADDRCONF_CNFADDRTYP_FK_I ON ADR.ADDR_CONFIDENTIAL_DTL
(STD_CONFIDENTIALADDRESSCAT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ADDRCONF_PK ON ADR.ADDR_CONFIDENTIAL_DTL
(ADDR_CONFIDENTIAL_DTL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ADDR_CONF_DTL_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.ADDR_CONFIDENTIAL_DTL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ADDR_CONFIDENTIAL_DTL_H
		(
                ADDR_CONFIDENTIAL_DTL_H_ID,
 		ADDR_CONFIDENTIAL_DTL_ID,
 		ADDRESS_ID,
 		STD_CONFIDENTIALADDRESSCAT_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
      		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ADDR_CONFIDENTIAL_DTL_H_s.nextval,
		:old.ADDR_CONFIDENTIAL_DTL_ID,
		:old.ADDRESS_ID,
		:old.STD_CONFIDENTIALADDRESSCAT_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY ,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;
		insert into ADDR_CONFIDENTIAL_DTL_H
		(
                ADDR_CONFIDENTIAL_DTL_H_ID,
 		ADDR_CONFIDENTIAL_DTL_ID,
 		ADDRESS_ID,
 		STD_CONFIDENTIALADDRESSCAT_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
      		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ADDR_CONFIDENTIAL_DTL_H_s.nextval,
		:new.ADDR_CONFIDENTIAL_DTL_ID,
		:new.ADDRESS_ID,
		:new.STD_CONFIDENTIALADDRESSCAT_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY ,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end ADDR_CONF_DTL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ADDR_CONFIDENTIAL_DTL FOR ADR.ADDR_CONFIDENTIAL_DTL;


ALTER TABLE ADR.ADDR_CONFIDENTIAL_DTL ADD (
  CONSTRAINT ADDRCONF_PK
  PRIMARY KEY
  (ADDR_CONFIDENTIAL_DTL_ID)
  USING INDEX ADR.ADDRCONF_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ADDR_CONFIDENTIAL_DTL ADD (
  CONSTRAINT ADDRCONF_ADDR_FK 
  FOREIGN KEY (ADDRESS_ID) 
  REFERENCES ADR.ADDRESS (ADDRESS_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ADDR_CONFIDENTIAL_DTL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ADDR_CONFIDENTIAL_DTL TO ADR_RW;

GRANT SELECT ON ADR.ADDR_CONFIDENTIAL_DTL TO ES_R;

GRANT INSERT, SELECT ON ADR.ADDR_CONFIDENTIAL_DTL TO ES_RW;
ALTER TABLE ADR.ADDR_CONFIDENTIAL_DTL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ADDR_CONFIDENTIAL_DTL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ADDR_CONFIDENTIAL_DTL_H
(
  ADDR_CONFIDENTIAL_DTL_H_ID     NUMBER(20)     NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  ADDR_CONFIDENTIAL_DTL_ID       NUMBER(20)     NOT NULL,
  ADDRESS_ID                     NUMBER(20)     NOT NULL,
  STD_CONFIDENTIALADDRESSCAT_ID  NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ADDR_CONFIDENTIAL_DTL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.ADDR_CONFIDENTIAL_DTL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ADDR_CONFIDENTIAL_DTL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ADDRCONFH_PK ON ADR.ADDR_CONFIDENTIAL_DTL_H
(ADDR_CONFIDENTIAL_DTL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ADDR_CONFIDENTIAL_DTL_H FOR ADR.ADDR_CONFIDENTIAL_DTL_H;


ALTER TABLE ADR.ADDR_CONFIDENTIAL_DTL_H ADD (
  CONSTRAINT ADDR_CONFIDENTIAL_DTL_H_CHK
  CHECK (TRANSACTION_TYPE_CODE IN ('''U''', '''I''', '''D'''))
  ENABLE VALIDATE,
  CONSTRAINT ADDRCONFH_PK
  PRIMARY KEY
  (ADDR_CONFIDENTIAL_DTL_H_ID)
  USING INDEX ADR.ADDRCONFH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ADDR_CONFIDENTIAL_DTL_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ADDR_CONFIDENTIAL_DTL_H TO ADR_RW;

GRANT SELECT ON ADR.ADDR_CONFIDENTIAL_DTL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ADDR_CONFIDENTIAL_DTL_H TO ES_RW;
ALTER TABLE ADR.ADR_DB_VER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ADR_DB_VER CASCADE CONSTRAINTS;

CREATE TABLE ADR.ADR_DB_VER
(
  DB_VERSION      VARCHAR2(20 BYTE),
  DB_CREATE_DATE  DATE                          NOT NULL,
  ADR_DB_VER_ID   NUMBER(20)                    NOT NULL,
  ADR_CR          VARCHAR2(20 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ADR_DB_VER IS 'LOOKUP TABLE';


CREATE UNIQUE INDEX ADR.ADR_DBVER_CR_UK ON ADR.ADR_DB_VER
(ADR_CR)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ADR_DBVER_VERSION_UK ON ADR.ADR_DB_VER
(DB_VERSION, ADR_CR)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ADR_DB_VER FOR ADR.ADR_DB_VER;


ALTER TABLE ADR.ADR_DB_VER ADD (
  CONSTRAINT ADR_DBVER_PK
  PRIMARY KEY
  (ADR_DB_VER_ID)
  DISABLE NOVALIDATE,
  CONSTRAINT ADR_DBVER_CR_UK
  UNIQUE (ADR_CR)
  USING INDEX ADR.ADR_DBVER_CR_UK
  ENABLE VALIDATE,
  CONSTRAINT ADR_DBVER_VERSION_UK
  UNIQUE (DB_VERSION, ADR_CR)
  USING INDEX ADR.ADR_DBVER_VERSION_UK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ADR_DB_VER TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.ADR_DB_VER TO ADR_RW;

GRANT SELECT ON ADR.ADR_DB_VER TO ES_R;

GRANT INSERT, SELECT ON ADR.ADR_DB_VER TO ES_RW;

GRANT SELECT ON ADR.ADR_DB_VER TO PUBLIC;
ALTER TABLE ADR.APP_PARAMETER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.APP_PARAMETER CASCADE CONSTRAINTS;

CREATE TABLE ADR.APP_PARAMETER
(
  APP_PARAMETER_ID       NUMBER(20)             NOT NULL,
  APP_PARAMETER_TYPE_ID  NUMBER(20)             NOT NULL,
  APP_PARAMETER_UNIT_ID  NUMBER(20),
  APPLICATION_NAME       VARCHAR2(80 BYTE),
  PARAMETER_NAME         VARCHAR2(50 BYTE)      NOT NULL,
  PARAMETER_DESCRIPTION  VARCHAR2(250 BYTE)     NOT NULL,
  PARAMETER_VALUE        VARCHAR2(1000 BYTE)    NOT NULL,
  PARAMETER_DATA_LENGTH  NUMBER(10),
  INACTIVE_FLAG          NUMBER(1)              DEFAULT 0                     NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.APP_PARAMETER IS 'This table stores the various parameters used by the application, in support of non-hardcoded programming best practices.';

COMMENT ON COLUMN ADR.APP_PARAMETER.APP_PARAMETER_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.APP_PARAMETER.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.APP_PARAMETER.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.APP_PARAMETER.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.APP_PARAMETER.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.APP_PARAMETER.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.APP_PARAMETER.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE INDEX ADR.APARAM_STD_APARAMTYP_FK_I ON ADR.APP_PARAMETER
(APP_PARAMETER_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.APARAM_STD_APARAMU_FK_I ON ADR.APP_PARAMETER
(APP_PARAMETER_UNIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.APPLPARAM_PK ON ADR.APP_PARAMETER
(APP_PARAMETER_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.APPLPARAM_U_CODE_UK ON ADR.APP_PARAMETER
(PARAMETER_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM APP_PARAMETER FOR ADR.APP_PARAMETER;


ALTER TABLE ADR.APP_PARAMETER ADD (
  CONSTRAINT AVCON_1172521913_INACT_114
  CHECK (INACTIVE_FLAG BETWEEN 0 AND 0 OR INACTIVE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT APPLPARAM_PK
  PRIMARY KEY
  (APP_PARAMETER_ID)
  USING INDEX ADR.APPLPARAM_PK
  ENABLE VALIDATE,
  CONSTRAINT APPLPARAM_U_CODE_UK
  UNIQUE (PARAMETER_NAME)
  USING INDEX ADR.APPLPARAM_U_CODE_UK
  ENABLE VALIDATE);

ALTER TABLE ADR.APP_PARAMETER ADD (
  CONSTRAINT APARAM_STD_APARAMTYP_FK 
  FOREIGN KEY (APP_PARAMETER_TYPE_ID) 
  REFERENCES ADRREF.STD_APPPARAMETERTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT APARAM_STD_APARAMU_FK 
  FOREIGN KEY (APP_PARAMETER_UNIT_ID) 
  REFERENCES ADRREF.STD_APPPARAMETERUNIT (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.APP_PARAMETER TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.APP_PARAMETER TO ADR_RW;

GRANT SELECT ON ADR.APP_PARAMETER TO ES_R;

GRANT INSERT, SELECT ON ADR.APP_PARAMETER TO ES_RW;
ALTER TABLE ADR.BATCH_REQUEST_FACILITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BATCH_REQUEST_FACILITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.BATCH_REQUEST_FACILITY
(
  BATCH_REQUEST_FACILITY_ID  NUMBER(20)         NOT NULL,
  STD_INSTITUTION_ID         NUMBER(20)         NOT NULL,
  HANDBOOK_BATCH_REQUEST_ID  NUMBER(20)         NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  PARENT_STD_INSTITUTION_ID  NUMBER(20)         NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BATCH_REQUEST_FACILITY IS 'A record of parent preferred facilities used to select veterans for a particular batch request.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.BATREQFAC_HNDBATREQ_FK_I ON ADR.BATCH_REQUEST_FACILITY
(HANDBOOK_BATCH_REQUEST_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.BATREQFAC_PK ON ADR.BATCH_REQUEST_FACILITY
(BATCH_REQUEST_FACILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BATCH_REQUEST_FAC_AUD_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.BATCH_REQUEST_FACILITY  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into BATCH_REQUEST_FACILITY_H
        (
        BATCH_REQUEST_FACILITY_H_ID,
        BATCH_REQUEST_FACILITY_ID,
        HANDBOOK_BATCH_REQUEST_ID,
        STD_INSTITUTION_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE,
        PARENT_STD_INSTITUTION_ID
        )
        values
        (
        BATCH_REQUEST_FACILITY_H_S.nextval,
        :old.BATCH_REQUEST_FACILITY_ID,
        :old.HANDBOOK_BATCH_REQUEST_ID,
       :old.STD_INSTITUTION_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D',
        :old.PARENT_STD_INSTITUTION_ID
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into BATCH_REQUEST_FACILITY_H
        (
        BATCH_REQUEST_FACILITY_H_ID,
        BATCH_REQUEST_FACILITY_ID,
    HANDBOOK_BATCH_REQUEST_ID,
    STD_INSTITUTION_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE,
        PARENT_STD_INSTITUTION_ID
        )
        values
        (
        BATCH_REQUEST_FACILITY_H_S.nextval,
        :new.BATCH_REQUEST_FACILITY_ID,
    :new.HANDBOOK_BATCH_REQUEST_ID,
    :new.STD_INSTITUTION_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML,
        :new.PARENT_STD_INSTITUTION_ID
        );
    end if;

end BATCH_REQUEST_FAC_AUD_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM BATCH_REQUEST_FACILITY FOR ADR.BATCH_REQUEST_FACILITY;


ALTER TABLE ADR.BATCH_REQUEST_FACILITY ADD (
  CONSTRAINT BATREQFAC_PK
  PRIMARY KEY
  (BATCH_REQUEST_FACILITY_ID)
  USING INDEX ADR.BATREQFAC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BATCH_REQUEST_FACILITY ADD (
  CONSTRAINT R_1061 
  FOREIGN KEY (HANDBOOK_BATCH_REQUEST_ID) 
  REFERENCES ADR.HANDBOOK_BATCH_REQUEST (HANDBOOK_BATCH_REQUEST_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BATCH_REQUEST_FACILITY TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.BATCH_REQUEST_FACILITY TO ADR_RW;

GRANT SELECT ON ADR.BATCH_REQUEST_FACILITY TO ES_R;

GRANT INSERT, SELECT ON ADR.BATCH_REQUEST_FACILITY TO ES_RW;
ALTER TABLE ADR.BATCH_REQUEST_FACILITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BATCH_REQUEST_FACILITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BATCH_REQUEST_FACILITY_H
(
  BATCH_REQUEST_FACILITY_ID    NUMBER(20)       NOT NULL,
  STD_INSTITUTION_ID           NUMBER(20)       NOT NULL,
  HANDBOOK_BATCH_REQUEST_ID    NUMBER(20)       NOT NULL,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  HISTORY_CREATED_DATE         DATE             NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  BATCH_REQUEST_FACILITY_H_ID  NUMBER(20)       NOT NULL,
  PARENT_STD_INSTITUTION_ID    NUMBER(20)       NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BATCH_REQUEST_FACILITY_H IS 'A record of parent preferred facilities used to select veterans for a particular batch request.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_FACILITY_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.BATREQFACH_PK ON ADR.BATCH_REQUEST_FACILITY_H
(BATCH_REQUEST_FACILITY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BATCH_REQUEST_FACILITY_H FOR ADR.BATCH_REQUEST_FACILITY_H;


ALTER TABLE ADR.BATCH_REQUEST_FACILITY_H ADD (
  CONSTRAINT BATREQFACH_PK
  PRIMARY KEY
  (BATCH_REQUEST_FACILITY_H_ID)
  USING INDEX ADR.BATREQFACH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BATCH_REQUEST_FACILITY_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.BATCH_REQUEST_FACILITY_H TO ADR_RW;

GRANT SELECT ON ADR.BATCH_REQUEST_FACILITY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BATCH_REQUEST_FACILITY_H TO ES_RW;
ALTER TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP CASCADE CONSTRAINTS;

CREATE TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP
(
  BATCH_REQUEST_PRIORITY_GRP_ID  NUMBER(20)     NOT NULL,
  STD_ENROLLMENTPRIORGRP_ID      NUMBER(20)     NOT NULL,
  STD_ENROLLMENTPRIORSGRP_ID     NUMBER(20),
  HANDBOOK_BATCH_REQUEST_ID      NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP IS 'A parent priority group used to select veterans for a particular batch request.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.BATREQPRRGRP_HNDBTCREQ_FK_I ON ADR.BATCH_REQUEST_PRIORITY_GROUP
(HANDBOOK_BATCH_REQUEST_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.BTCREQPRRGRP_PK ON ADR.BATCH_REQUEST_PRIORITY_GROUP
(BATCH_REQUEST_PRIORITY_GRP_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BATCH_REQ_PRI_GRP_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.BATCH_REQUEST_PRIORITY_GROUP  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into BATCH_REQUEST_PRIORITY_GROUP_H
        (
        BATCH_REQUEST_PRI_GRP_H_ID,
        BATCH_REQUEST_PRIORITY_GRP_ID,
    STD_ENROLLMENTPRIORSGRP_ID,
    STD_ENROLLMENTPRIORGRP_ID,
    HANDBOOK_BATCH_REQUEST_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        BATCH_REQUEST_PRI_GRP_H_ID_S.nextval,
        :old.BATCH_REQUEST_PRIORITY_GRP_ID,
    :old.STD_ENROLLMENTPRIORSGRP_ID,
    :old.STD_ENROLLMENTPRIORGRP_ID,
    :old.HANDBOOK_BATCH_REQUEST_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into BATCH_REQUEST_PRIORITY_GROUP_H
        (
        BATCH_REQUEST_PRI_GRP_H_ID,
        BATCH_REQUEST_PRIORITY_GRP_ID,
    STD_ENROLLMENTPRIORSGRP_ID,
    STD_ENROLLMENTPRIORGRP_ID,
    HANDBOOK_BATCH_REQUEST_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        BATCH_REQUEST_PRI_GRP_H_ID_S.nextval,
        :new.BATCH_REQUEST_PRIORITY_GRP_ID,
    :new.STD_ENROLLMENTPRIORSGRP_ID,
    :new.STD_ENROLLMENTPRIORGRP_ID,
    :new.HANDBOOK_BATCH_REQUEST_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end BATCH_REQ_PRI_GRP_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM BATCH_REQUEST_PRIORITY_GROUP FOR ADR.BATCH_REQUEST_PRIORITY_GROUP;


ALTER TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP ADD (
  CONSTRAINT BTCREQPRRGRP_PK
  PRIMARY KEY
  (BATCH_REQUEST_PRIORITY_GRP_ID)
  USING INDEX ADR.BTCREQPRRGRP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP ADD (
  CONSTRAINT R_1062 
  FOREIGN KEY (HANDBOOK_BATCH_REQUEST_ID) 
  REFERENCES ADR.HANDBOOK_BATCH_REQUEST (HANDBOOK_BATCH_REQUEST_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.BATCH_REQUEST_PRIORITY_GROUP TO ADR_RW;

GRANT SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP TO ES_R;

GRANT INSERT, SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP TO ES_RW;
ALTER TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP_H
(
  BATCH_REQUEST_PRIORITY_GRP_ID  NUMBER(20)     NOT NULL,
  STD_ENROLLMENTPRIORGRP_ID      NUMBER(20)     NOT NULL,
  STD_ENROLLMENTPRIORSGRP_ID     NUMBER(20),
  HANDBOOK_BATCH_REQUEST_ID      NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  BATCH_REQUEST_PRI_GRP_H_ID     NUMBER(20)     NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP_H IS 'A parent priority group used to select veterans for a particular batch request.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.BATCH_REQUEST_PRIORITY_GROUP_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.BATREQPRRGRPH_PK ON ADR.BATCH_REQUEST_PRIORITY_GROUP_H
(BATCH_REQUEST_PRI_GRP_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BATCH_REQUEST_PRIORITY_GROUP_H FOR ADR.BATCH_REQUEST_PRIORITY_GROUP_H;


ALTER TABLE ADR.BATCH_REQUEST_PRIORITY_GROUP_H ADD (
  CONSTRAINT BATREQPRRGRPH_PK
  PRIMARY KEY
  (BATCH_REQUEST_PRI_GRP_H_ID)
  USING INDEX ADR.BATREQPRRGRPH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.BATCH_REQUEST_PRIORITY_GROUP_H TO ADR_RW;

GRANT SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BATCH_REQUEST_PRIORITY_GROUP_H TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_MST
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_MST CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_MST
(
  BENEFICIARY_MST_ID     NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  MST_STATUS_TYPE_ID     NUMBER(20),
  STD_INSTITUTION_ID     NUMBER(20),
  MST_EVALUATION_DATE    DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_MST IS 'The BENEFICIARY_MST table shows that a person has been clinically determined to have suffered a Military Sexual Trauma, and includes the status of the determination, the site at which the determination was made. PERSON_ID Foreign key to the PERSON table. Indicates the particular person to whom the Military Sexual Trauma data applies. MST_STATUS_TYPE_ID Foreign key to the MST_STATUS_TYPE table, which indicates the status of a persons screening for MST. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table, which indicates the site at which MST status was determined. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.BENEFICIARY_MST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.MST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.MST_EVALUATION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYMST_PK ON ADR.BENEFICIARY_MST
(BENEFICIARY_MST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYMST_PRSN_FK_I ON ADR.BENEFICIARY_MST
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYMST_STDINST_FK_I ON ADR.BENEFICIARY_MST
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYMST_STD_MSTST_FK_I ON ADR.BENEFICIARY_MST
(MST_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BENEFICIARY_MST_AUDIR_TRGR
after Update or Delete or Insert
on ADR.BENEFICIARY_MST
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin
	if DELETING then
		insert into BENEFICIARY_MST_H
		(
		BENEFICIARY_MST_H_ID,
		BENEFICIARY_MST_ID  ,
		PERSON_ID           ,
		MST_STATUS_TYPE_ID ,
		STD_INSTITUTION_ID  ,
		MST_EVALUATION_DATE,
		RECORD_CREATED_BY   ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY  ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		beneficiary_mst_h_s.nextval,
		:old.BENEFICIARY_MST_ID,
		:old.PERSON_ID        ,
		:old.MST_STATUS_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.MST_EVALUATION_DATE,
		:old.RECORD_CREATED_BY ,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY ,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else

		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into BENEFICIARY_MST_H
		(
		BENEFICIARY_MST_H_ID,
		BENEFICIARY_MST_ID  ,
		PERSON_ID           ,
		MST_STATUS_TYPE_ID ,
		STD_INSTITUTION_ID ,
		MST_EVALUATION_DATE,
		RECORD_CREATED_BY  ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		beneficiary_mst_h_s.nextval,
		:new.BENEFICIARY_MST_ID,
		:new.PERSON_ID         ,
		:new.MST_STATUS_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.MST_EVALUATION_DATE,
		:new.RECORD_CREATED_BY  ,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end beneficiary_mst_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_MST FOR ADR.BENEFICIARY_MST;


ALTER TABLE ADR.BENEFICIARY_MST ADD (
  CONSTRAINT BNFYMST_PK
  PRIMARY KEY
  (BENEFICIARY_MST_ID)
  USING INDEX ADR.BNFYMST_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BENEFICIARY_MST ADD (
  CONSTRAINT BNFYMST_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_MST TO ADREXT;

GRANT SELECT ON ADR.BENEFICIARY_MST TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_MST TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_MST TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_MST TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_MST_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_MST_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_MST_H
(
  BENEFICIARY_MST_H_ID   NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  BENEFICIARY_MST_ID     NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  MST_STATUS_TYPE_ID     NUMBER(20),
  STD_INSTITUTION_ID     NUMBER(20),
  MST_EVALUATION_DATE    DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_MST_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.BENEFICIARY_MST_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.BENEFICIARY_MST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.MST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.MST_EVALUATION_DATE IS 'Date on which the MST screening was conducted. Either by medical record or by physical exam.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_MST_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYMSTH_PK ON ADR.BENEFICIARY_MST_H
(BENEFICIARY_MST_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYMSTH_PRSNID_NU_I ON ADR.BENEFICIARY_MST_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_MST_H FOR ADR.BENEFICIARY_MST_H;


ALTER TABLE ADR.BENEFICIARY_MST_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_001
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT BNFYMSTH_PK
  PRIMARY KEY
  (BENEFICIARY_MST_H_ID)
  USING INDEX ADR.BNFYMSTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_MST_H TO ADR_R;

GRANT SELECT ON ADR.BENEFICIARY_MST_H TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_MST_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_MST_H TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_NTR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_NTR CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_NTR
(
  BENEFICIARY_NTR_ID              NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  NTR_DIAGNOSIS_SITE_ID           NUMBER(20),
  NTR_VERIFICATION_SITE_ID        NUMBER(20),
  NTR_VERIFY_METHOD_TYPE_ID       NUMBER(20),
  VERIFICATION_DATE               DATE,
  AVIATOR_PRE1955_IND             VARCHAR2(1 BYTE),
  NAVY_SUB_TRAINEE_PRE_1965_IND   VARCHAR2(1 BYTE),
  RECEIVED_NTR_TREATMENT_IND      VARCHAR2(1 BYTE),
  DIAGNOSED_HEAD_NECK_CANCER_IND  VARCHAR2(1 BYTE),
  HEAD_NECK_CANCER_DIAGNOSIS_DT   DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_NTR IS 'The BENEFICIARY_NTR table shows that a person has been clinically determined to have suffered from Nose Throat Radium exposure. The table includes the site at which the determination was made, the verification date and method, and whether the person has been diagnosed and treated with cancer. PERSON_ID Foreign key to the PERSON table. Indicates the particular person to whom the Nose, Throat, Radium data applies. NTR_DIAGNOSIS_SITE_ID Foreign key to STD_INSTITUTION table NTR_VERIFICATION_SITE_ID Foreign key to STD_INSTITUTION table NTR_VERIFY_METHOD_TYPE_ID Foreign key to the NTR_VERIFY_METHOD_TYPE table. Indicates which technique was used to verify a persons NTR status (for example, Military Record, Qualifying Military Service). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.BENEFICIARY_NTR_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.NTR_DIAGNOSIS_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.NTR_VERIFICATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.NTR_VERIFY_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.VERIFICATION_DATE IS 'BR6805 (NTR) Date/Time Diagnosis Verified is defined as the date and time the Nose/Throat Radium Treatment diagnosis was verified.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.AVIATOR_PRE1955_IND IS 'Indicates whether or not a person was an aviator before 1955, when exposure was more likely to have happened. Aviatior Prior to 1955 is a question that is presented to the user if the veteran has answered YES to the NTR Indicator.  The question is Did you serve as an aviator in the Military before Jan 31, 1955?  The values include YES/NO.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.NAVY_SUB_TRAINEE_PRE_1965_IND IS 'Indicates whether or not a person was a Navy submarine trainee before 1955, when exposure was more likely to have happened. Submarine Trainee prior to 1965 is a question that is presented to the user if the veteran has answered YES to the NTR Indicator.  The question is Did you have submarine training in the military before Jan 1, 1965?';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECEIVED_NTR_TREATMENT_IND IS 'Received NTR Treatment defines the answer a veteran gave to the question: DID YOU RECEIVE NOSE OR THROAT RADIUM TREATMENTS IN THE MILITARY?   ';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.DIAGNOSED_HEAD_NECK_CANCER_IND IS 'Indicates whether a person has been diagnosed with head/neck cancer. Diagnosed with Cancer of Head Neck is a question that is presented to the user if the veteran has answered YES to the NTR Indicator.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYNTR_PK ON ADR.BENEFICIARY_NTR
(BENEFICIARY_NTR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYNTR_PRSN_FK_I ON ADR.BENEFICIARY_NTR
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYNTR_STDINST_FK_I ON ADR.BENEFICIARY_NTR
(NTR_VERIFICATION_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYNTR_STDINST_H_FK_I ON ADR.BENEFICIARY_NTR
(NTR_DIAGNOSIS_SITE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYNTR_STD_NTRVRFYMTD_FK_I ON ADR.BENEFICIARY_NTR
(NTR_VERIFY_METHOD_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BENEFICIARY_NTR_AUDIR_TRGR
after Update or Delete or Insert
on ADR.BENEFICIARY_NTR
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into BENEFICIARY_NTR_H
		(
		BENEFICIARY_NTR_H_ID ,
		BENEFICIARY_NTR_ID       ,
		PERSON_ID                      ,
		NTR_DIAGNOSIS_SITE_ID ,
		NTR_VERIFICATION_SITE_ID ,
		NTR_VERIFY_METHOD_TYPE_ID,
		VERIFICATION_DATE            ,
		AVIATOR_PRE1955_IND        ,
		NAVY_SUB_TRAINEE_PRE_1965_IND  ,
		RECEIVED_NTR_TREATMENT_IND     ,
		DIAGNOSED_HEAD_NECK_CANCER_IND,
		HEAD_NECK_CANCER_DIAGNOSIS_DT ,
		RECORD_CREATED_BY              ,
		RECORD_CREATED_DATE         ,
		RECORD_MODIFIED_BY             ,
		RECORD_MODIFIED_DATE        ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		beneficiary_ntr_h_s.nextval,
		:old.BENEFICIARY_NTR_ID       ,
		:old.PERSON_ID                      ,
		:old.NTR_DIAGNOSIS_SITE_ID ,
		:old.NTR_VERIFICATION_SITE_ID ,
		:old.NTR_VERIFY_METHOD_TYPE_ID,
		:old.VERIFICATION_DATE            ,
		:old.AVIATOR_PRE1955_IND        ,
		:old.NAVY_SUB_TRAINEE_PRE_1965_IND  ,
		:old.RECEIVED_NTR_TREATMENT_IND     ,
		:old.DIAGNOSED_HEAD_NECK_CANCER_IND,
		:old.HEAD_NECK_CANCER_DIAGNOSIS_DT ,
		:old.RECORD_CREATED_BY              ,
		:old.RECORD_CREATED_DATE         ,
		:old.RECORD_MODIFIED_BY             ,
		:old.RECORD_MODIFIED_DATE        ,
		:old.RECORD_MODIFIED_COUNT     ,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into BENEFICIARY_NTR_H
		(
		BENEFICIARY_NTR_H_ID ,
		BENEFICIARY_NTR_ID       ,
		PERSON_ID                      ,
		NTR_DIAGNOSIS_SITE_ID ,
		NTR_VERIFICATION_SITE_ID ,
		NTR_VERIFY_METHOD_TYPE_ID,
		VERIFICATION_DATE            ,
		AVIATOR_PRE1955_IND        ,
		NAVY_SUB_TRAINEE_PRE_1965_IND  ,
		RECEIVED_NTR_TREATMENT_IND     ,
		DIAGNOSED_HEAD_NECK_CANCER_IND,
		HEAD_NECK_CANCER_DIAGNOSIS_DT ,
		RECORD_CREATED_BY              ,
		RECORD_CREATED_DATE         ,
		RECORD_MODIFIED_BY             ,
		RECORD_MODIFIED_DATE        ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		beneficiary_ntr_h_s.nextval,
		:new.BENEFICIARY_NTR_ID       ,
		:new.PERSON_ID                      ,
		:new.NTR_DIAGNOSIS_SITE_ID ,
		:new.NTR_VERIFICATION_SITE_ID ,
		:new.NTR_VERIFY_METHOD_TYPE_ID,
		:new.VERIFICATION_DATE            ,
		:new.AVIATOR_PRE1955_IND        ,
		:new.NAVY_SUB_TRAINEE_PRE_1965_IND  ,
		:new.RECEIVED_NTR_TREATMENT_IND     ,
		:new.DIAGNOSED_HEAD_NECK_CANCER_IND,
		:new.HEAD_NECK_CANCER_DIAGNOSIS_DT ,
		:new.RECORD_CREATED_BY              ,
		:new.RECORD_CREATED_DATE         ,
		:new.RECORD_MODIFIED_BY             ,
		:new.RECORD_MODIFIED_DATE        ,
		:new.RECORD_MODIFIED_COUNT     ,
		mDML
		);

	end if;
end BENEFICIARY_NTR_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_NTR FOR ADR.BENEFICIARY_NTR;


ALTER TABLE ADR.BENEFICIARY_NTR ADD (
  CONSTRAINT BNFYNTR_PK
  PRIMARY KEY
  (BENEFICIARY_NTR_ID)
  USING INDEX ADR.BNFYNTR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BENEFICIARY_NTR ADD (
  CONSTRAINT BNFYNTR_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_NTR TO ADREXT;

GRANT SELECT ON ADR.BENEFICIARY_NTR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_NTR TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_NTR TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_NTR TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_NTR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_NTR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_NTR_H
(
  BENEFICIARY_NTR_H_ID            NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  BENEFICIARY_NTR_ID              NUMBER(20),
  PERSON_ID                       NUMBER(20),
  NTR_DIAGNOSIS_SITE_ID           NUMBER(20),
  NTR_VERIFICATION_SITE_ID        NUMBER(20),
  NTR_VERIFY_METHOD_TYPE_ID       NUMBER(20),
  VERIFICATION_DATE               DATE,
  AVIATOR_PRE1955_IND             VARCHAR2(1 BYTE),
  NAVY_SUB_TRAINEE_PRE_1965_IND   VARCHAR2(1 BYTE),
  RECEIVED_NTR_TREATMENT_IND      VARCHAR2(1 BYTE),
  DIAGNOSED_HEAD_NECK_CANCER_IND  VARCHAR2(1 BYTE),
  HEAD_NECK_CANCER_DIAGNOSIS_DT   DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_NTR_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.BENEFICIARY_NTR_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.BENEFICIARY_NTR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.NTR_DIAGNOSIS_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.NTR_VERIFICATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.NTR_VERIFY_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.VERIFICATION_DATE IS 'BR6805 (NTR) Date/Time Diagnosis Verified is defined as the date and time the Nose/Throat Radium Treatment diagnosis was verified.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.AVIATOR_PRE1955_IND IS 'Indicates whether or not a person was an aviator before 1955, when exposure was more likely to have happened. Aviatior Prior to 1955 is a question that is presented to the user if the veteran has answered YES to the NTR Indicator.  The question is Did you serve as an aviator in the Military before Jan 31, 1955?  The values include YES/NO.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.NAVY_SUB_TRAINEE_PRE_1965_IND IS 'Indicates whether or not a person was a Navy submarine trainee before 1955, when exposure was more likely to have happened. Submarine Trainee prior to 1965 is a question that is presented to the user if the veteran has answered YES to the NTR Indicator.  The question is Did you have submarine training in the military before Jan 1, 1965?';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECEIVED_NTR_TREATMENT_IND IS 'Received NTR Treatment defines the answer a veteran gave to the question: DID YOU RECEIVE NOSE OR THROAT RADIUM TREATMENTS IN THE MILITARY?   ';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.DIAGNOSED_HEAD_NECK_CANCER_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.HEAD_NECK_CANCER_DIAGNOSIS_DT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_NTR_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYNTRH_PK ON ADR.BENEFICIARY_NTR_H
(BENEFICIARY_NTR_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYNTRH_PRSNID_NU_I ON ADR.BENEFICIARY_NTR_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_NTR_H FOR ADR.BENEFICIARY_NTR_H;


ALTER TABLE ADR.BENEFICIARY_NTR_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_069
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067428
  CHECK ("BENEFICIARY_NTR_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067429
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067430
  CHECK ("BENEFICIARY_NTR_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067431
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067432
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067433
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067434
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067435
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067436
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT BNFYNTRH_PK
  PRIMARY KEY
  (BENEFICIARY_NTR_H_ID)
  USING INDEX ADR.BNFYNTRH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_NTR_H TO ADR_R;

GRANT SELECT ON ADR.BENEFICIARY_NTR_H TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_NTR_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_NTR_H TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_SCI
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_SCI CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_SCI
(
  BENEFICIARY_SCI_ID       NUMBER(20)           NOT NULL,
  PERSON_ID                NUMBER(20)           NOT NULL,
  STD_SPINALINJURYCODE_ID  NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_SCI IS 'The BENEFICIARY_SCI  table shows that a person has been clinically determined to have suffered a Spinal Cord Injury, and includes the status of the determination, the site at which the determination was made.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.BENEFICIARY_SCI_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYSCI_PK ON ADR.BENEFICIARY_SCI
(BENEFICIARY_SCI_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYSCI_PRSN_FK_I ON ADR.BENEFICIARY_SCI
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BNFYSCI_STDSPINAL_FK_I ON ADR.BENEFICIARY_SCI
(STD_SPINALINJURYCODE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BENEF_SCI_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.BENEFICIARY_SCI
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into BENEFICIARY_SCI_H
		(
		BENEFICIARY_SCI_H_ID,
		BENEFICIARY_SCI_ID,
		PERSON_ID,
		STD_SPINALINJURYCODE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
      		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		BENEFICIARY_SCI_H_s.nextval,
		:old.BENEFICIARY_SCI_ID,
		:old.PERSON_ID,
		:old.STD_SPINALINJURYCODE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY ,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;
		insert into BENEFICIARY_SCI_H
		(
		BENEFICIARY_SCI_H_ID,
		BENEFICIARY_SCI_ID,
		PERSON_ID,
		STD_SPINALINJURYCODE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
      		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		BENEFICIARY_SCI_H_s.nextval,
		:new.BENEFICIARY_SCI_ID,
		:new.PERSON_ID,
		:new.STD_SPINALINJURYCODE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY ,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end BENEF_SCI_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_SCI FOR ADR.BENEFICIARY_SCI;


ALTER TABLE ADR.BENEFICIARY_SCI ADD (
  CONSTRAINT BNFYSCI_PK
  PRIMARY KEY
  (BENEFICIARY_SCI_ID)
  USING INDEX ADR.BNFYSCI_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BENEFICIARY_SCI ADD (
  CONSTRAINT BNFYSCI_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_SCI TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_SCI TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_SCI TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_SCI TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_SCI_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_SCI_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_SCI_H
(
  BENEFICIARY_SCI_H_ID     NUMBER(20),
  TRANSACTION_TYPE_CODE    VARCHAR2(1 BYTE),
  BENEFICIARY_SCI_ID       NUMBER(20),
  PERSON_ID                NUMBER(20),
  STD_SPINALINJURYCODE_ID  NUMBER(20),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE      DATE,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE     DATE,
  RECORD_MODIFIED_COUNT    NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_SCI_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.BENEFICIARY_SCI_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_SCI_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BNFYSCIH_PK ON ADR.BENEFICIARY_SCI_H
(BENEFICIARY_SCI_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_SCI_H FOR ADR.BENEFICIARY_SCI_H;


ALTER TABLE ADR.BENEFICIARY_SCI_H ADD (
  CONSTRAINT BENEFICIARY_SCI_H_CHK
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106522
  CHECK ("BENEFICIARY_SCI_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106523
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106524
  CHECK ("BENEFICIARY_SCI_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106525
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106526
  CHECK ("STD_SPINALINJURYCODE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106527
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106528
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106529
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106530
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106531
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT BNFYSCIH_PK
  PRIMARY KEY
  (BENEFICIARY_SCI_H_ID)
  USING INDEX ADR.BNFYSCIH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_SCI_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_SCI_H TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_SCI_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_SCI_H TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_TRAVEL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_TRAVEL CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_TRAVEL
(
  BENEFICIARY_TRAVEL_ID    NUMBER(20)           NOT NULL,
  PERSON_ID                NUMBER(20)           NOT NULL,
  STD_INSTITUTION_ID       NUMBER(20),
  INCOME_YEAR              NUMBER(4),
  TRAVEL_ELIGIBILITY_IND   VARCHAR2(1 BYTE),
  TRAVEL_CLAIM_DATE        DATE,
  TRAVEL_CERTIFIED_DATE    DATE,
  TRAVEL_CERTIFIED_AMOUNT  NUMBER(10,2),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_TRAVEL IS 'Detail regarding travel amount certified for a beneficiary to visit a Site (hospital) The BENEFICIARY_TRAVEL table shows that a person has been authorized to be reimbursed for travel for treatment. It also includes the dates of travel and the amount approved for reimbursement. PERSON_ID Foreign key to the PERSON table. Indicates the particular person to whom the travel benefits data applies. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the VHA site where the travel benefit was certified. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.BENEFICIARY_TRAVEL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.TRAVEL_ELIGIBILITY_IND IS 'BR3553 Eligible Indicator is defined as a code to indicate if this person is required to perform a Beneficiary Travel test to receive benefits.  The acceptable values are Yes/No. This determination is done by VISTA and sent to the HEC .If a veteran is 30-100% service connected or in receipt of VA Pension he is entitled to Beneficiary Travel upon request without the need to supply financial information.  The below business rules are applied on VISTA. The veteran is required to supply financial information to determine if he/she is eligible to receive beneficiary travel. If he/she has one of the following primary or other eligibilities:  · Non - Service Connected · Service Connected 0% non-compensable  · Less than 30% service connected  · WWI or Mexican Border veteran  · Purple Heart  · POW OR the veteran is in receipt of Medicaid OR is in receipt of Military Retirement Disability The veteran is NOT required to supply financial information to determine if he/she is eligible to receive beneficiary travel benefits.   If he/she is/has one of the following:  · The veteran Is a Domiciliary Inpatient  · The veteran Is Deceased · The veteran has one of the following primary/other eligibilities  1. Receiving VA Pension   2. Service Connected 30 - 100% ';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.TRAVEL_CLAIM_DATE IS 'Date on which a travel benefits claim was submitted. Travel Claim Date/Time is defined as the Beneficiary Travel CLAIM date/timeDate must be precise.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.TRAVEL_CERTIFIED_DATE IS 'Date Certified is defined as the date of annual income certification used in the Beneficial Travel Claim process.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.TRAVEL_CERTIFIED_AMOUNT IS 'Amount Certified is defined as the patients annual income for the previous year as reported by the patient. If the veteran is required to do a beneficiary travel test this number will be compared to the VA Pension threshold based on # of dependents. This comparison is a manual comparison done on VISTA by the travel clerk.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BENTRVL_PK ON ADR.BENEFICIARY_TRAVEL
(BENEFICIARY_TRAVEL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENTRVL_PRSN_FK_I ON ADR.BENEFICIARY_TRAVEL
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENTRVL_STDINST_FK_I ON ADR.BENEFICIARY_TRAVEL
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BENEFICIARY_TRVL_audir_trgr
after Update or Delete or Insert
ON ADR.BENEFICIARY_TRAVEL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into BENEFICIARY_TRAVEL_H
		(
		BENEFICIARY_TRAVEL_H_ID,
		BENEFICIARY_TRAVEL_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		INCOME_YEAR,
		TRAVEL_CERTIFIED_AMOUNT,
		TRAVEL_CERTIFIED_DATE,
		TRAVEL_CLAIM_DATE,
		TRAVEL_ELIGIBILITY_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		BENEFICIARY_TRAVEL_H_S.nextval,
		:old.BENEFICIARY_TRAVEL_ID,
		:old.PERSON_ID,
		:old.STD_INSTITUTION_ID,
		:old.INCOME_YEAR,
		:old.TRAVEL_CERTIFIED_AMOUNT,
		:old.TRAVEL_CERTIFIED_DATE,
		:old.TRAVEL_CLAIM_DATE,
		:old.TRAVEL_ELIGIBILITY_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into BENEFICIARY_TRAVEL_H
		(
		BENEFICIARY_TRAVEL_H_ID,
		BENEFICIARY_TRAVEL_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		INCOME_YEAR,
		TRAVEL_CERTIFIED_AMOUNT,
		TRAVEL_CERTIFIED_DATE,
		TRAVEL_CLAIM_DATE,
		TRAVEL_ELIGIBILITY_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		beneficiary_travel_h_s.nextval,
		:new.BENEFICIARY_TRAVEL_ID,
		:new.PERSON_ID,
		:new.STD_INSTITUTION_ID,
		:new.INCOME_YEAR,
		:new.TRAVEL_CERTIFIED_AMOUNT,
		:new.TRAVEL_CERTIFIED_DATE,
		:new.TRAVEL_CLAIM_DATE,
		:new.TRAVEL_ELIGIBILITY_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;
end BENEFICIARY_TRVL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_TRAVEL FOR ADR.BENEFICIARY_TRAVEL;


ALTER TABLE ADR.BENEFICIARY_TRAVEL ADD (
  CONSTRAINT BENTRVL_PK
  PRIMARY KEY
  (BENEFICIARY_TRAVEL_ID)
  USING INDEX ADR.BENTRVL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BENEFICIARY_TRAVEL ADD (
  CONSTRAINT BENTRVL_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_TRAVEL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_TRAVEL TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_TRAVEL TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_TRAVEL TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_TRAVEL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_TRAVEL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_TRAVEL_H
(
  BENEFICIARY_TRAVEL_H_ID  NUMBER(20)           NOT NULL,
  TRANSACTION_TYPE_CODE    VARCHAR2(1 BYTE)     NOT NULL,
  BENEFICIARY_TRAVEL_ID    NUMBER(20)           NOT NULL,
  PERSON_ID                NUMBER(20)           NOT NULL,
  STD_INSTITUTION_ID       NUMBER(20),
  INCOME_YEAR              NUMBER(4),
  TRAVEL_ELIGIBILITY_IND   VARCHAR2(1 BYTE),
  TRAVEL_CLAIM_DATE        DATE,
  TRAVEL_CERTIFIED_DATE    DATE,
  TRAVEL_CERTIFIED_AMOUNT  NUMBER(10,2),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_TRAVEL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.BENEFICIARY_TRAVEL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.BENEFICIARY_TRAVEL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.TRAVEL_ELIGIBILITY_IND IS 'BR3553 Eligible Indicator is defined as a code to indicate if this person is required to perform a Beneficiary Travel test to receive benefits.  The acceptable values are Yes/No. This determination is done by VISTA and sent to the HEC .If a veteran is 30-100% service connected or in receipt of VA Pension he is entitled to Beneficiary Travel upon request without the need to supply financial information.  The below business rules are applied on VISTA. The veteran is required to supply financial information to determine if he/she is eligible to receive beneficiary travel. If he/she has one of the following primary or other eligibilities:  · Non - Service Connected · Service Connected 0% non-compensable  · Less than 30% service connected  · WWI or Mexican Border veteran  · Purple Heart  · POW OR the veteran is in receipt of Medicaid OR is in receipt of Military Retirement Disability The veteran is NOT required to supply financial information to determine if he/she is eligible to receive beneficiary travel benefits.   If he/she is/has one of the following:  · The veteran Is a Domiciliary Inpatient  · The veteran Is Deceased · The veteran has one of the following primary/other eligibilities  1. Receiving VA Pension   2. Service Connected 30 - 100% ';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.TRAVEL_CLAIM_DATE IS 'Date on which a travel benefits claim was submitted. Travel Claim Date/Time is defined as the Beneficiary Travel CLAIM date/timeDate must be precise.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.TRAVEL_CERTIFIED_DATE IS 'Date Certified is defined as the date of annual income certification used in the Beneficial Travel Claim process.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.TRAVEL_CERTIFIED_AMOUNT IS 'Amount Certified is defined as the patients annual income for the previous year as reported by the patient. If the veteran is required to do a beneficiary travel test this number will be compared to the VA Pension threshold based on # of dependents. This comparison is a manual comparison done on VISTA by the travel clerk.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_TRAVEL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BENTRVLH_PK ON ADR.BENEFICIARY_TRAVEL_H
(BENEFICIARY_TRAVEL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENTRVLH_PRSNID_NU_I ON ADR.BENEFICIARY_TRAVEL_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_TRAVEL_H FOR ADR.BENEFICIARY_TRAVEL_H;


ALTER TABLE ADR.BENEFICIARY_TRAVEL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_006
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT BENTRVLH_PK
  PRIMARY KEY
  (BENEFICIARY_TRAVEL_H_ID)
  USING INDEX ADR.BENTRVLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_TRAVEL_H TO ADR_R;

GRANT SELECT ON ADR.BENEFICIARY_TRAVEL_H TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_TRAVEL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_TRAVEL_H TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_VISIT_SUMMARY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_VISIT_SUMMARY CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_VISIT_SUMMARY
(
  BENEFICIARY_VISIT_SUMMARY_ID  NUMBER(20),
  PERSON_ID                     NUMBER(20),
  STD_INSTITUTION_ID            NUMBER(20),
  INCOME_YEAR                   NUMBER(4),
  INPATIENT_DAYS                VARCHAR2(20 BYTE),
  OUTPATIENT_VISITS_NUMBER      VARCHAR2(20 BYTE),
  LAST_VISIT_DATE               DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_VISIT_SUMMARY IS 'Detail concerning the Hospital visit summary information for a beneficiary during a calendar year. The BENEFICIARY_VISIT_SUMMARY table stores the number of inpatient and outpatient visits a patient has had, by income year. PERSON_ID Foreign key to the PERSON table. Indicates the particular person to whom the visit summary data applies. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site that the person visited for treatment ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.BENEFICIARY_VISIT_SUMMARY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.INCOME_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.INPATIENT_DAYS IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.LAST_VISIT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BENVISSUM_PK ON ADR.BENEFICIARY_VISIT_SUMMARY
(BENEFICIARY_VISIT_SUMMARY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENVISSUM_PRSN_FK_I ON ADR.BENEFICIARY_VISIT_SUMMARY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENVISSUM_STDINST_FK_I ON ADR.BENEFICIARY_VISIT_SUMMARY
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.BENEF_VISIT_SUMM_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."BENEFICIARY_VISIT_SUMMARY" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into BENEFICIARY_VISIT_SUMMARY_H
		(
		BENEFICIARY_VISIT_SUMMARY_H_ID,
		BENEFICIARY_VISIT_SUMMARY_ID   ,
		PERSON_ID                      ,
		STD_INSTITUTION_ID  ,
		INCOME_YEAR                  ,
		INPATIENT_DAYS             ,
		OUTPATIENT_VISITS_NUMBER ,
		LAST_VISIT_DATE               ,
		RECORD_CREATED_BY    ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE   ,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		BENEFICIARY_VISIT_SUMMARY_H_S.nextval,
		:old.BENEFICIARY_VISIT_SUMMARY_ID   ,
		:old.PERSON_ID                      ,
		:old.STD_INSTITUTION_ID  ,
		:old.INCOME_YEAR                  ,
		:old.INPATIENT_DAYS             ,
		:old.OUTPATIENT_VISITS_NUMBER ,
		:old.LAST_VISIT_DATE               ,
		:old.RECORD_CREATED_BY    ,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY        ,
		:old.RECORD_MODIFIED_DATE   ,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into BENEFICIARY_VISIT_SUMMARY_H
		(
		BENEFICIARY_VISIT_SUMMARY_H_ID,
		BENEFICIARY_VISIT_SUMMARY_ID   ,
		PERSON_ID                      ,
		STD_INSTITUTION_ID  ,
		INCOME_YEAR                  ,
		INPATIENT_DAYS             ,
		OUTPATIENT_VISITS_NUMBER ,
		LAST_VISIT_DATE               ,
		RECORD_CREATED_BY    ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE   ,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		BENEFICIARY_VISIT_SUMMARY_H_S.nextval,
		:new.BENEFICIARY_VISIT_SUMMARY_ID   ,
		:new.PERSON_ID                      ,
		:new.STD_INSTITUTION_ID  ,
		:new.INCOME_YEAR                  ,
		:new.INPATIENT_DAYS             ,
		:new.OUTPATIENT_VISITS_NUMBER ,
		:new.LAST_VISIT_DATE               ,
		:new.RECORD_CREATED_BY    ,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY        ,
		:new.RECORD_MODIFIED_DATE   ,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end BENEF_VISIT_SUMM_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_VISIT_SUMMARY FOR ADR.BENEFICIARY_VISIT_SUMMARY;


ALTER TABLE ADR.BENEFICIARY_VISIT_SUMMARY ADD (
  CONSTRAINT SYS_C0067052
  CHECK ("BENEFICIARY_VISIT_SUMMARY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067053
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067054
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067055
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067056
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067057
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067058
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT BENVISSUM_PK
  PRIMARY KEY
  (BENEFICIARY_VISIT_SUMMARY_ID)
  USING INDEX ADR.BENVISSUM_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BENEFICIARY_VISIT_SUMMARY ADD (
  CONSTRAINT BENVISSUM_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY TO ADREXT;

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BENEFICIARY_VISIT_SUMMARY TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY TO ES_RW;
ALTER TABLE ADR.BENEFICIARY_VISIT_SUMMARY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BENEFICIARY_VISIT_SUMMARY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.BENEFICIARY_VISIT_SUMMARY_H
(
  BENEFICIARY_VISIT_SUMMARY_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  BENEFICIARY_VISIT_SUMMARY_ID    NUMBER(20),
  PERSON_ID                       NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  INCOME_YEAR                     NUMBER(4),
  INPATIENT_DAYS                  VARCHAR2(20 BYTE),
  OUTPATIENT_VISITS_NUMBER        VARCHAR2(20 BYTE),
  LAST_VISIT_DATE                 DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BENEFICIARY_VISIT_SUMMARY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.BENEFICIARY_VISIT_SUMMARY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.BENEFICIARY_VISIT_SUMMARY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.INPATIENT_DAYS IS 'Number of days a person spent as an inpatient in a hospital. ';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.OUTPATIENT_VISITS_NUMBER IS 'Number of outpatient visits to a hospital. ';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BENEFICIARY_VISIT_SUMMARY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BENVISSUMH_PK ON ADR.BENEFICIARY_VISIT_SUMMARY_H
(BENEFICIARY_VISIT_SUMMARY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BENVISSUMH_PRSNID_NU_I ON ADR.BENEFICIARY_VISIT_SUMMARY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BENEFICIARY_VISIT_SUMMARY_H FOR ADR.BENEFICIARY_VISIT_SUMMARY_H;


ALTER TABLE ADR.BENEFICIARY_VISIT_SUMMARY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_055
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067000
  CHECK ("BENEFICIARY_VISIT_SUMMARY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067001
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067002
  CHECK ("BENEFICIARY_VISIT_SUMMARY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067003
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067004
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067005
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067006
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067007
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067008
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT BENVISSUMH_PK
  PRIMARY KEY
  (BENEFICIARY_VISIT_SUMMARY_H_ID)
  USING INDEX ADR.BENVISSUMH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY_H TO ADR_R;

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY_H TO ADR_RW;

GRANT SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.BENEFICIARY_VISIT_SUMMARY_H TO ES_RW;
ALTER TABLE ADR.BROKER_SERVICE_ERROR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BROKER_SERVICE_ERROR CASCADE CONSTRAINTS;

CREATE TABLE ADR.BROKER_SERVICE_ERROR
(
  BROKER_SERVICE_ERROR_ID  NUMBER(20)           NOT NULL,
  ERROR_TEXT               VARCHAR2(4000 BYTE),
  INTERNAL_ERROR_TEXT      VARCHAR2(500 BYTE),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(18)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BROKER_SERVICE_ERROR IS 'Retention: 30 days';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.BROKER_SERVICE_ERROR_ID IS 'The primary key, generated by the database system in numeric form. Used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_ERROR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BRSRVER_PK ON ADR.BROKER_SERVICE_ERROR
(BROKER_SERVICE_ERROR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BROKER_SERVICE_ERROR FOR ADR.BROKER_SERVICE_ERROR;


ALTER TABLE ADR.BROKER_SERVICE_ERROR ADD (
  CONSTRAINT BRSRVER_PK
  PRIMARY KEY
  (BROKER_SERVICE_ERROR_ID)
  USING INDEX ADR.BRSRVER_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BROKER_SERVICE_ERROR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BROKER_SERVICE_ERROR TO ADR_RW;

GRANT SELECT ON ADR.BROKER_SERVICE_ERROR TO ES_R;

GRANT INSERT, SELECT ON ADR.BROKER_SERVICE_ERROR TO ES_RW;
ALTER TABLE ADR.BROKER_SERVICE_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BROKER_SERVICE_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.BROKER_SERVICE_LOG
(
  BROKER_SERVICE_LOG_ID          NUMBER(20)     NOT NULL,
  BROKER_SERVICE_ERROR_ID        NUMBER(20),
  TRANSMISSION_TYPE_ID           NUMBER(20),
  PERSON_ID                      NUMBER(20)     NOT NULL,
  STD_MESSAGESTATUS_ID           NUMBER(20),
  STD_HL7ACKNOWLEDGEMENTCODE_ID  NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  REF_BROKER_SERVICE_LOG_ID      NUMBER(20),
  TRANSMISSION_ID                NUMBER(20)     NOT NULL,
  TRANSMISSION_DATE              DATE           NOT NULL,
  RETRANSMISSION_COUNT           NUMBER(10)     NOT NULL,
  RETRANSMISSION_TEXT            VARCHAR2(30 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(18)     NOT NULL
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION BROKER_SERVICE_LOG_P001 VALUES LESS THAN (TO_DATE(' 2016-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BROKER_SERVICE_LOG IS 'Transactions from VADIR thru ORUZ07 etc.

Retention: 30 days';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.BROKER_SERVICE_LOG_ID IS 'The primary key, generated by the database system in numeric form. Used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.REF_BROKER_SERVICE_LOG_ID IS 'Referenced Broker Server Log Identifier - Reference to a transmitted log record where it''s communication could not return all needed information. ';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_LOG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.BRSRVL1_PK ON ADR.BROKER_SERVICE_LOG
(BROKER_SERVICE_LOG_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE INDEX ADR.BRSRVLP_PERSMSGSTAT_I ON ADR.BROKER_SERVICE_LOG
(PERSON_ID, STD_MESSAGESTATUS_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.BRSRVL_TRN_FK_I ON ADR.BROKER_SERVICE_LOG
(TRANSMISSION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BROKER_SERVICE_LOG FOR ADR.BROKER_SERVICE_LOG;


ALTER TABLE ADR.BROKER_SERVICE_LOG ADD (
  CONSTRAINT BRSRVL1_PK
  PRIMARY KEY
  (BROKER_SERVICE_LOG_ID)
  USING INDEX ADR.BRSRVL1_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.BROKER_SERVICE_LOG ADD (
  CONSTRAINT BRSL_BRSE_FK 
  FOREIGN KEY (BROKER_SERVICE_ERROR_ID) 
  REFERENCES ADR.BROKER_SERVICE_ERROR (BROKER_SERVICE_ERROR_ID)
  ENABLE VALIDATE,
  CONSTRAINT BRSL_PER_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DISABLE NOVALIDATE,
  CONSTRAINT BRSL_TRANSTYP_FK 
  FOREIGN KEY (TRANSMISSION_TYPE_ID) 
  REFERENCES ADR.TRANSMISSION_TYPE (TRANSMISSION_TYPE_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BROKER_SERVICE_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BROKER_SERVICE_LOG TO ADR_RW;

GRANT SELECT ON ADR.BROKER_SERVICE_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.BROKER_SERVICE_LOG TO ES_RW;
ALTER TABLE ADR.BROKER_SERVICE_PAYLOAD
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.BROKER_SERVICE_PAYLOAD CASCADE CONSTRAINTS;

CREATE TABLE ADR.BROKER_SERVICE_PAYLOAD
(
  BROKER_SERVICE_PAYLOAD_ID  NUMBER(20)         NOT NULL,
  BROKER_SERVICE_LOG_ID      NUMBER(20)         NOT NULL,
  REQUEST_PAYLOAD            CLOB,
  RESPONSE_PAYLOAD           CLOB,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(18)         NOT NULL
)
LOB (REQUEST_PAYLOAD) STORE AS (
  TABLESPACE  M_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
LOB (RESPONSE_PAYLOAD) STORE AS (
  TABLESPACE  M_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.BROKER_SERVICE_PAYLOAD IS 'Retention: 30 days';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.BROKER_SERVICE_PAYLOAD_ID IS 'The primary key, generated by the database system in numeric form. Used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.REQUEST_PAYLOAD IS 'Intended to hold XML. Initially with no online viewing.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.BROKER_SERVICE_PAYLOAD.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.BRSRVPL_BRSRVL_FK_I ON ADR.BROKER_SERVICE_PAYLOAD
(BROKER_SERVICE_LOG_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.BRSRVPL_PK ON ADR.BROKER_SERVICE_PAYLOAD
(BROKER_SERVICE_PAYLOAD_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM BROKER_SERVICE_PAYLOAD FOR ADR.BROKER_SERVICE_PAYLOAD;


ALTER TABLE ADR.BROKER_SERVICE_PAYLOAD ADD (
  CONSTRAINT BRSRVPL_PK
  PRIMARY KEY
  (BROKER_SERVICE_PAYLOAD_ID)
  USING INDEX ADR.BRSRVPL_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.BROKER_SERVICE_PAYLOAD TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.BROKER_SERVICE_PAYLOAD TO ADR_RW;

GRANT SELECT ON ADR.BROKER_SERVICE_PAYLOAD TO ES_R;

GRANT INSERT, SELECT ON ADR.BROKER_SERVICE_PAYLOAD TO ES_RW;
ALTER TABLE ADR.CANCEL_DECLINE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CANCEL_DECLINE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CANCEL_DECLINE
(
  CANCEL_DECLINE_ID              NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  CANCEL_DECLINE_STATUS_TYPE_ID  NUMBER(20),
  CANCEL_DECLINE_REASON_TYPE_ID  NUMBER(20),
  CANCEL_DECLINE_IND             VARCHAR2(1 BYTE),
  CANCEL_DECLINE_EFFECTIVE_DATE  DATE,
  REMARKS                        VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CANCEL_DECLINE IS 'Stores details regarding the Beneficiarys cancellation from VA health care coverage. Also stores details in the event that enrolee has declined VA health care coverage. The CANCEL_DECLINE table stores data about a person who has cancelled or declined enrollment into the VHA system. It indicates the reason and effective date for the cancel/decline, as well as storing status and related remarks. PERSON_ID Foreign key to the PERSON table. Indicates the person who has cancelled or declined enrollment into the VHA healthcare system. CANCEL_DECLINE_STATUS_TYPE_ID Foreign key to the CANCEL_DECLINE_STATUS_TYPE table. Indicates the status of a persons request to cancel or decline enrollment (for example, pending completion, pending telephone calls). CANCEL_DECLINE_REASON_TYPE_ID Foreign key to the CANCEL_DECLINE_REASON_TYPE table. Indicates the reason the person cancelled or declined (for example, geographic access, other insurance). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.CANCEL_DECLINE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.CANCEL_DECLINE_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.CANCEL_DECLINE_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.CANCEL_DECLINE_IND IS 'Indicates whether a person has cancelled or declined enrollment with the VHA healthcare system. ';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.CANCEL_DECLINE_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CANDEC_PK ON ADR.CANCEL_DECLINE
(CANCEL_DECLINE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CANDEC_PRSN_FK_I ON ADR.CANCEL_DECLINE
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CANDEC_STD_CANDECRSN_FK_I ON ADR.CANCEL_DECLINE
(CANCEL_DECLINE_REASON_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CANDEC_STD_CANDEC_FK_I ON ADR.CANCEL_DECLINE
(CANCEL_DECLINE_STATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CANCEL_DECLINE_AUDIR_TRGR
after Update or Delete or Insert
on ADR.CANCEL_DECLINE
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into CANCEL_DECLINE_H
		(
		CANCEL_DECLINE_H_ID,
		CANCEL_DECLINE_ID,
		PERSON_ID,
		CANCEL_DECLINE_STATUS_TYPE_ID,
		CANCEL_DECLINE_REASON_TYPE_ID,
		CANCEL_DECLINE_IND,
		CANCEL_DECLINE_EFFECTIVE_DATE,
		REMARKS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CANCEL_DECLINE_H_S.nextval,
		:old.CANCEL_DECLINE_ID,
		:old.PERSON_ID,
		:old.CANCEL_DECLINE_STATUS_TYPE_ID,
		:old.CANCEL_DECLINE_REASON_TYPE_ID,
		:old.CANCEL_DECLINE_IND,
		:old.CANCEL_DECLINE_EFFECTIVE_DATE,
		:old.REMARKS,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into CANCEL_DECLINE_H
		(
		CANCEL_DECLINE_H_ID,
		CANCEL_DECLINE_ID,
		PERSON_ID,
		CANCEL_DECLINE_STATUS_TYPE_ID,
		CANCEL_DECLINE_REASON_TYPE_ID,
		CANCEL_DECLINE_IND,
		CANCEL_DECLINE_EFFECTIVE_DATE,
		REMARKS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CANCEL_DECLINE_H_S.nextval,
		:new.CANCEL_DECLINE_ID,
		:new.PERSON_ID,
		:new.CANCEL_DECLINE_STATUS_TYPE_ID,
		:new.CANCEL_DECLINE_REASON_TYPE_ID,
		:new.CANCEL_DECLINE_IND,
		:new.CANCEL_DECLINE_EFFECTIVE_DATE,
		:new.REMARKS,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  CANCEL_DECLINE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CANCEL_DECLINE FOR ADR.CANCEL_DECLINE;


ALTER TABLE ADR.CANCEL_DECLINE ADD (
  CONSTRAINT CANDEC_PK
  PRIMARY KEY
  (CANCEL_DECLINE_ID)
  USING INDEX ADR.CANDEC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CANCEL_DECLINE ADD (
  CONSTRAINT CANDEC_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.CANCEL_DECLINE TO ADREXT;

GRANT SELECT ON ADR.CANCEL_DECLINE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CANCEL_DECLINE TO ADR_RW;

GRANT SELECT ON ADR.CANCEL_DECLINE TO ES_R;

GRANT INSERT, SELECT ON ADR.CANCEL_DECLINE TO ES_RW;
ALTER TABLE ADR.CANCEL_DECLINE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CANCEL_DECLINE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CANCEL_DECLINE_H
(
  CANCEL_DECLINE_H_ID            NUMBER(20)     NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  CANCEL_DECLINE_ID              NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  CANCEL_DECLINE_STATUS_TYPE_ID  NUMBER(20),
  CANCEL_DECLINE_REASON_TYPE_ID  NUMBER(20),
  CANCEL_DECLINE_IND             VARCHAR2(1 BYTE),
  CANCEL_DECLINE_EFFECTIVE_DATE  DATE,
  REMARKS                        VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CANCEL_DECLINE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_IND IS 'Indicates whether a person has cancelled or declined enrollment with the VHA healthcare system. ';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.CANCEL_DECLINE_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CANCEL_DECLINE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CANDECH_PK ON ADR.CANCEL_DECLINE_H
(CANCEL_DECLINE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CANDECH_PRSNID_NU_I ON ADR.CANCEL_DECLINE_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CANCEL_DECLINE_H FOR ADR.CANCEL_DECLINE_H;


ALTER TABLE ADR.CANCEL_DECLINE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_057
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CANDECH_PK
  PRIMARY KEY
  (CANCEL_DECLINE_H_ID)
  USING INDEX ADR.CANDECH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CANCEL_DECLINE_H TO ADR_R;

GRANT SELECT ON ADR.CANCEL_DECLINE_H TO ADR_RW;

GRANT SELECT ON ADR.CANCEL_DECLINE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CANCEL_DECLINE_H TO ES_RW;
ALTER TABLE ADR.CASSCERTIFIEDTYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CASSCERTIFIEDTYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CASSCERTIFIEDTYPE
(
  CASSCERTIFIEDTYPE_ID  NUMBER(20)              NOT NULL,
  CODE                  VARCHAR2(3 BYTE)        NOT NULL,
  NAME                  VARCHAR2(30 BYTE)       NOT NULL,
  DESCRIPTION           VARCHAR2(250 BYTE),
  CREATED               DATE                    NOT NULL,
  UPDATED               DATE,
  CREATEDBY             VARCHAR2(32 BYTE)       NOT NULL,
  UPDATEDBY             VARCHAR2(32 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CASSCERTIFIEDTYPE IS 'Lookup table used by VetChoice';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.CASSCERTIFIEDTYPE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.CODE IS 'Shortened code';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.NAME IS 'User readable name';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.DESCRIPTION IS 'Description of the CASS status code';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.CREATED IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.UPDATED IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.CREATEDBY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CASSCERTIFIEDTYPE.UPDATEDBY IS 'Account/user ID for this modification to the record.';


CREATE UNIQUE INDEX ADR.CASSCERTIFIEDTYPE_PK ON ADR.CASSCERTIFIEDTYPE
(CASSCERTIFIEDTYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CASSCERTIFIEDTYPE FOR ADR.CASSCERTIFIEDTYPE;


ALTER TABLE ADR.CASSCERTIFIEDTYPE ADD (
  CONSTRAINT CASSCERTIFIEDTYPE_PK
  PRIMARY KEY
  (CASSCERTIFIEDTYPE_ID)
  USING INDEX ADR.CASSCERTIFIEDTYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CASSCERTIFIEDTYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CASSCERTIFIEDTYPE TO ADR_RW;

GRANT SELECT ON ADR.CASSCERTIFIEDTYPE TO EEUSR WITH GRANT OPTION;

GRANT SELECT ON ADR.CASSCERTIFIEDTYPE TO ES_R;

GRANT INSERT, SELECT ON ADR.CASSCERTIFIEDTYPE TO ES_RW;
ALTER TABLE ADR.CATASTROPHIC_DISABILITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CATASTROPHIC_DISABILITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.CATASTROPHIC_DISABILITY
(
  CATASTROPHIC_DISABILITY_ID      NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  CAT_DSBLTY_EVAL_MTHD_TYPE_ID    NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  CATASTROPHICALLY_DISABLED_IND   VARCHAR2(1 BYTE),
  CLINICAL_APPROVER_NAME          VARCHAR2(100 BYTE),
  CAT_DSBLTY_EVALUATION_DATE      DATE,
  DECISION_DATE                   DATE,
  CAT_DSBLTY_REVIEW_REQUESTED_DT  DATE,
  CAT_DSBLTY_REVIEW_INITIATED_DT  DATE,
  CAT_DSBLTY_REVIEW_NOTIFIED_DT   DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CATASTROPHIC_DISABILITY IS 'The CATASTROPHIC_DISABILITY table shows that a person has been clinically determined to have a disability severe enough to be called catastrophic. The table includes the site at which the determination was made, the verification date and method. It also includes information about any reviews of this determination have taken place. PERSON_ID Foreign key to the PERSON table. Indicates the person who has been clinically determined to have a catastrophic disability. CAT_DSBLTY_EVAL_MTHD_TYPE_ID Foreign key to the CAT_DSBLTY_EVAL_MTHD_TYPE table. Indicates how the evaluation was done (for example, automated record review, physical examination). STD_INSTITUTION_ID Site at which the catastrophic disability determination was made. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CAT_DSBLTY_EVAL_MTHD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CATASTROPHICALLY_DISABLED_IND IS 'NULL =  no CD data
  2  `0¿ = no letter sent
  3  `1¿ = letter sent	';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CLINICAL_APPROVER_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CAT_DSBLTY_EVALUATION_DATE IS 'Review date is defined as the date of the Catastrophically Disabled evaluation. Either by medical record or by physical exam.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.DECISION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CAT_DSBLTY_REVIEW_REQUESTED_DT IS 'Date on which a request to review a persons catastrophic disability determination was received';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CAT_DSBLTY_REVIEW_INITIATED_DT IS 'Date on which catastrophic disability review was initiated by the Chief of Staff or his/her designee.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.CAT_DSBLTY_REVIEW_NOTIFIED_DT IS 'Date on which the affected person was notified about the results of the review.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CATDSBLTY_PK ON ADR.CATASTROPHIC_DISABILITY
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLTY_PRSN_FK_I ON ADR.CATASTROPHIC_DISABILITY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLTY_STDINST_FK_I ON ADR.CATASTROPHIC_DISABILITY
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLTY_STD_EVALMTHD_FK_I ON ADR.CATASTROPHIC_DISABILITY
(CAT_DSBLTY_EVAL_MTHD_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CAT_DISABILITY_AUDIR_TRGR
  AFTER UPDATE OR DELETE OR INSERT
  ON ADR.CATASTROPHIC_DISABILITY   FOR EACH ROW
DECLARE

    Mdml                               CHAR(1);

BEGIN

  IF DELETING THEN

    INSERT
    INTO      Catastrophic_Disability_H
              (Catastrophic_disability_h_id,
               Catastrophic_disability_id,
               Person_id,
               Cat_dsblty_eval_mthd_type_id,
               Std_institution_id,
               Clinical_approver_name,
               Catastrophically_disabled_ind,
               Cat_dsblty_evaluation_date,
               Decision_date,
               Record_created_by,
               Record_created_date,
               Record_modified_by,
               Record_modified_date,
               Record_modified_count,
               Cat_dsblty_review_requested_dt,
               Cat_dsblty_review_initiated_dt,
               Cat_dsblty_review_notified_dt,
               Transaction_type_code)
    VALUES    (ADR.Catastrophic_disability_h_s.NEXTVAL,
               :OLD.Catastrophic_disability_id     ,
               :OLD.Person_id                      ,
               :OLD.Cat_dsblty_eval_mthd_type_id   ,
               :OLD.Std_institution_id             ,
               :OLD.Clinical_approver_name,
               :OLD.Catastrophically_disabled_ind  ,
               :OLD.Cat_dsblty_evaluation_date     ,
               :OLD.Decision_date                  ,
               :OLD.Record_created_by   ,
               :OLD.Record_created_date            ,
               :OLD.Record_modified_by             ,
               :OLD.Record_modified_date        ,
               :OLD.Record_modified_count     ,
               :OLD.Cat_dsblty_review_requested_dt,
               :OLD.Cat_dsblty_review_initiated_dt,
               :OLD.Cat_dsblty_review_notified_dt  ,
               'D');

  ELSE

    IF UPDATING THEN

      Mdml := 'U';

    ELSE

      Mdml := 'I';

    END IF;

    INSERT
    INTO      ADR.Catastrophic_Disability_H
              (Catastrophic_disability_h_id,
               Catastrophic_disability_id,
               Person_id,
               Cat_dsblty_eval_mthd_type_id,
               Std_institution_id,
               Clinical_approver_name,
               Catastrophically_disabled_ind,
               Cat_dsblty_evaluation_date,
               Decision_date,
               Record_created_by,
               Record_created_date,
               Record_modified_by,
               Record_modified_date,
               Record_modified_count,
               Cat_dsblty_review_requested_dt,
               Cat_dsblty_review_initiated_dt,
               Cat_dsblty_review_notified_dt,
               Transaction_type_code)
    VALUES     (ADR.Catastrophic_disability_h_s.NEXTVAL,
                :NEW.Catastrophic_disability_id,
                :NEW.Person_id,
                :NEW.Cat_dsblty_eval_mthd_type_id,
                :NEW.Std_institution_id,
                :NEW.Clinical_approver_name,
                :NEW.Catastrophically_disabled_ind,
                :NEW.Cat_dsblty_evaluation_date,
                :NEW.Decision_date,
                :NEW.Record_created_by,
                :NEW.Record_created_date,
                :NEW.Record_modified_by,
                :NEW.Record_modified_date,
                :NEW.Record_modified_count,
                :NEW.Cat_dsblty_review_requested_dt,
                :NEW.Cat_dsblty_review_initiated_dt,
                :NEW.Cat_dsblty_review_notified_dt,
                Mdml);

  END IF;

END  Cat_Disability_Audir_Trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CATASTROPHIC_DISABILITY FOR ADR.CATASTROPHIC_DISABILITY;


ALTER TABLE ADR.CATASTROPHIC_DISABILITY ADD (
  CONSTRAINT CATDSBLTY_PK
  PRIMARY KEY
  (CATASTROPHIC_DISABILITY_ID)
  USING INDEX ADR.CATDSBLTY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CATASTROPHIC_DISABILITY ADD (
  CONSTRAINT CATDSBLTY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY TO ADREXT;

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CATASTROPHIC_DISABILITY TO ADR_RW;

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY TO ES_R;

GRANT INSERT, SELECT ON ADR.CATASTROPHIC_DISABILITY TO ES_RW;
ALTER TABLE ADR.CATASTROPHIC_DISABILITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CATASTROPHIC_DISABILITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CATASTROPHIC_DISABILITY_H
(
  CATASTROPHIC_DISABILITY_H_ID    NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  CATASTROPHIC_DISABILITY_ID      NUMBER(20),
  PERSON_ID                       NUMBER(20),
  CAT_DSBLTY_EVAL_MTHD_TYPE_ID    NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  CLINICAL_APPROVER_NAME          VARCHAR2(100 BYTE),
  CATASTROPHICALLY_DISABLED_IND   VARCHAR2(1 BYTE),
  CAT_DSBLTY_EVALUATION_DATE      DATE,
  DECISION_DATE                   DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  CAT_DSBLTY_REVIEW_REQUESTED_DT  DATE,
  CAT_DSBLTY_REVIEW_INITIATED_DT  DATE,
  CAT_DSBLTY_REVIEW_NOTIFIED_DT   DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CATASTROPHIC_DISABILITY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CATASTROPHIC_DISABILITY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CAT_DSBLTY_EVAL_MTHD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CLINICAL_APPROVER_NAME IS 'Name of the person who made the catastrophic disability determination. Name of the Chief of Staff or Designee making the Catastrophically Disabled decision. Free text.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CATASTROPHICALLY_DISABLED_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CAT_DSBLTY_EVALUATION_DATE IS 'Review date is defined as the date of the Catastrophically Disabled evaluation. Either by medical record or by physical exam.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.DECISION_DATE IS 'Date of decision is defined as the date that the clinical determination decision was made by the Chief of Staff or his/her designee.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CAT_DSBLTY_REVIEW_REQUESTED_DT IS 'Date on which a request to review a persons catastrophic disability determination was received';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CAT_DSBLTY_REVIEW_INITIATED_DT IS 'Date on which catastrophic disability review was initiated by the Chief of Staff or his/her designee.';

COMMENT ON COLUMN ADR.CATASTROPHIC_DISABILITY_H.CAT_DSBLTY_REVIEW_NOTIFIED_DT IS 'Date on which the affected person was notified about the results of the review.';


CREATE UNIQUE INDEX ADR.CATDSBLTYH_PK ON ADR.CATASTROPHIC_DISABILITY_H
(CATASTROPHIC_DISABILITY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLTYH_PRSNID_NU_I ON ADR.CATASTROPHIC_DISABILITY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CATASTROPHIC_DISABILITY_H FOR ADR.CATASTROPHIC_DISABILITY_H;


ALTER TABLE ADR.CATASTROPHIC_DISABILITY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_014
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066167
  CHECK ("CATASTROPHIC_DISABILITY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066168
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066169
  CHECK ("CATASTROPHIC_DISABILITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066170
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066171
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066172
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066173
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066174
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066175
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT CATDSBLTYH_PK
  PRIMARY KEY
  (CATASTROPHIC_DISABILITY_H_ID)
  USING INDEX ADR.CATDSBLTYH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY_H TO ADR_R;

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY_H TO ADR_RW;

GRANT SELECT ON ADR.CATASTROPHIC_DISABILITY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CATASTROPHIC_DISABILITY_H TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_CONDITION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_CONDITION CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_CONDITION
(
  CAT_DSBLTY_CONDITION_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  CONDITION_TYPE_ID           NUMBER(20)        NOT NULL,
  PRMNT_DSBLTY_STDYESNO_ID    NUMBER(20),
  DISABILITY_SCORE            NUMBER(3),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  PRMNT_DSBLTY_STDPERM_ID     NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_CONDITION IS 'The CAT_DSBLTY_CONDITION table is a child table of CATASTROPHIC_DISABILITY that shows the assessment tool used to evaluate the persons disability. CATASTROPHIC_DISABILITY_ID Foreign key to the CATASTROPHIC_DISABILITY table. CONDITION_TYPE_ID Foreign key to the CONDITION_TYPE table. Indicates what kind of disability a person has, according to the assessment tool used (for example, KATZ, FOLS). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.CAT_DSBLTY_CONDITION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.CONDITION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.DISABILITY_SCORE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSCOND_CATDSBLTY_FK_I ON ADR.CAT_DSBLTY_CONDITION
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSCOND_PK ON ADR.CAT_DSBLTY_CONDITION
(CAT_DSBLTY_CONDITION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSCOND_STDPERM_FK_I ON ADR.CAT_DSBLTY_CONDITION
(PRMNT_DSBLTY_STDPERM_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSCOND_STDYESNO_FK_I ON ADR.CAT_DSBLTY_CONDITION
(PRMNT_DSBLTY_STDYESNO_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSCON_STD_CNDN_FK_I ON ADR.CAT_DSBLTY_CONDITION
(CONDITION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CAT_DSBLTY_COND_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.CAT_DSBLTY_CONDITION
for each row
declare
       mDML char(1);
begin

    if DELETING then
        insert into CAT_DSBLTY_CONDITION_H
        (
        CAT_DSBLTY_CONDITION_H_ID,
        CAT_DSBLTY_CONDITION_ID,
        CATASTROPHIC_DISABILITY_ID,
        CONDITION_TYPE_ID         ,
        RECORD_CREATED_BY         ,
        RECORD_CREATED_DATE       ,
        RECORD_MODIFIED_BY        ,
        RECORD_MODIFIED_DATE      ,
        RECORD_MODIFIED_COUNT     ,
        PRMNT_DSBLTY_STDYESNO_ID  ,
        DISABILITY_SCORE          ,
        TRANSACTION_TYPE_CODE     ,
        PRMNT_DSBLTY_STDPERM_ID
        )
        values
        (
        CAT_DSBLTY_CONDITION_H_s.nextval,
        :old.CAT_DSBLTY_CONDITION_ID,
        :old.CATASTROPHIC_DISABILITY_ID,
        :old.CONDITION_TYPE_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        :old.PRMNT_DSBLTY_STDYESNO_ID,
        :old.DISABILITY_SCORE,
        'D',
        :old.PRMNT_DSBLTY_STDPERM_ID
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into CAT_DSBLTY_CONDITION_H
        (
        CAT_DSBLTY_CONDITION_H_ID,
        CAT_DSBLTY_CONDITION_ID,
        CATASTROPHIC_DISABILITY_ID,
        CONDITION_TYPE_ID         ,
        RECORD_CREATED_BY         ,
        RECORD_CREATED_DATE       ,
        RECORD_MODIFIED_BY        ,
        RECORD_MODIFIED_DATE      ,
        RECORD_MODIFIED_COUNT     ,
        PRMNT_DSBLTY_STDYESNO_ID  ,
        DISABILITY_SCORE          ,
        TRANSACTION_TYPE_CODE     ,
        PRMNT_DSBLTY_STDPERM_ID
        )
        values
        (
        CAT_DSBLTY_CONDITION_H_s.nextval,
        :new.CAT_DSBLTY_CONDITION_ID,
        :new.CATASTROPHIC_DISABILITY_ID,
        :new.CONDITION_TYPE_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        :new.PRMNT_DSBLTY_STDYESNO_ID,
        :new.DISABILITY_SCORE,
        mDML,
        :new.PRMNT_DSBLTY_STDPERM_ID
        );
    end if;

end  CAT_DSBLTY_COND_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_CONDITION FOR ADR.CAT_DSBLTY_CONDITION;


ALTER TABLE ADR.CAT_DSBLTY_CONDITION ADD (
  CONSTRAINT CATDSCOND_PK
  PRIMARY KEY
  (CAT_DSBLTY_CONDITION_ID)
  USING INDEX ADR.CATDSCOND_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CAT_DSBLTY_CONDITION ADD (
  CONSTRAINT CATDSCOND_CATDSBLTY_FK 
  FOREIGN KEY (CATASTROPHIC_DISABILITY_ID) 
  REFERENCES ADR.CATASTROPHIC_DISABILITY (CATASTROPHIC_DISABILITY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_CONDITION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CAT_DSBLTY_CONDITION TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_CONDITION TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_CONDITION TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_CONDITION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_CONDITION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_CONDITION_H
(
  CAT_DSBLTY_CONDITION_H_ID   NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  CAT_DSBLTY_CONDITION_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  CONDITION_TYPE_ID           NUMBER(20)        NOT NULL,
  PRMNT_DSBLTY_STDYESNO_ID    NUMBER(20),
  DISABILITY_SCORE            NUMBER(3),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  PRMNT_DSBLTY_STDPERM_ID     NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_CONDITION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.CAT_DSBLTY_CONDITION_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.CAT_DSBLTY_CONDITION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.CONDITION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.DISABILITY_SCORE IS 'BR3034 Score is based on the following and contingent on which CD condition was selected (FOLS, FIM, GAF, or KATZ).  Each one will have a score associated with it.Folstein Mini- Mental State Examination (FOLS) is a number between 0 - 30, on the Folstein.Mini-Mental State Examination (MMSE).Functional Independence Measure:The FIM contains 18 measures in 6 domains.  The 13 MOTOR ITEMS are in 4 domains:- self-care,- sphincter control,- transfers, and- locomotion.The value in this field is based on the number of MOTOR items for which the veteran received a score of 2 or lower. FIMS Score is 0- 13.The Katz Index of Activities of Daily Living (ADLs) assigns a number from 1 to 3 in each of 6 activities.  1 is dependent, 2 is an intermediate limitation, and 3 is independent. The Katz Index may be used to determine whether a veteran is catastrophically disabled.This field must contain the number of permanent ADLs for which the veteran received a score of 1 (dependent). It is NOT the overall score on theKATZ test (which can range from 0 to 6.)  GAF score is a number between 1 and 100.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_CONDITION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSCONDH_CATDSBLTY_NU_I ON ADR.CAT_DSBLTY_CONDITION_H
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSCONDH_PK ON ADR.CAT_DSBLTY_CONDITION_H
(CAT_DSBLTY_CONDITION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_CONDITION_H FOR ADR.CAT_DSBLTY_CONDITION_H;


ALTER TABLE ADR.CAT_DSBLTY_CONDITION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_047
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CATDSCONDH_PK
  PRIMARY KEY
  (CAT_DSBLTY_CONDITION_H_ID)
  USING INDEX ADR.CATDSCONDH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_CONDITION_H TO ADR_R;

GRANT SELECT ON ADR.CAT_DSBLTY_CONDITION_H TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_CONDITION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_CONDITION_H TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_DESCRIPTOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_DESCRIPTOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_DESCRIPTOR
(
  CAT_DSBLTY_DESCRIPTOR_ID    NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  DESCRIPTOR_TYPE_ID          NUMBER(20)        NOT NULL,
  DETERMINATION_DATE          DATE,
  STD_INSTITUTION_ID          NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.CAT_DSBLTY_DESCRIPTOR.CAT_DSBLTY_DESCRIPTOR_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DESCRIPTOR.CATASTROPHIC_DISABILITY_ID IS 'FK to ADR.CATASTROPHIC_DISABILITY.CATASTROPHIC_DISABILITY_ID';


CREATE UNIQUE INDEX ADR.CAT_DSBLTY_DESCRIPTOR_PK ON ADR.CAT_DSBLTY_DESCRIPTOR
(CAT_DSBLTY_DESCRIPTOR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CAT_DSBL_CAT_DSBL_DESC_FK_I ON ADR.CAT_DSBLTY_DESCRIPTOR
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CAT_DSBLTY_DESC_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.CAT_DSBLTY_DESCRIPTOR
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO ADR.CAT_DSBLTY_DESCRIPTOR_H
        (CAT_DSBLTY_DESCRIPTOR_ID_H,
         CAT_DSBLTY_DESCRIPTOR_ID,
         CATASTROPHIC_DISABILITY_ID,
         DESCRIPTOR_TYPE_ID,
         DETERMINATION_DATE,
         STD_INSTITUTION_ID,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_COUNT,
         RECORD_MODIFIED_DATE,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (CAT_DSBLTY_DESCRIPTOR_H_S.NEXTVAL,
         :OLD.CAT_DSBLTY_DESCRIPTOR_ID,
         :OLD.CATASTROPHIC_DISABILITY_ID,
         :OLD.DESCRIPTOR_TYPE_ID,
         :OLD.DETERMINATION_DATE,
         :OLD.STD_INSTITUTION_ID,
         :OLD.Record_created_by,
         :OLD.Record_created_date,
         :OLD.Record_modified_by,
         :OLD.Record_modified_count,
         :OLD.Record_modified_date,
         SYSDATE,
         'D'
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      INSERT INTO ADR.CAT_DSBLTY_DESCRIPTOR_H
        (CAT_DSBLTY_DESCRIPTOR_ID_H,
         CAT_DSBLTY_DESCRIPTOR_ID,
         CATASTROPHIC_DISABILITY_ID,
         DESCRIPTOR_TYPE_ID,
         DETERMINATION_DATE,
         STD_INSTITUTION_ID,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_COUNT,
         RECORD_MODIFIED_DATE,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (CAT_DSBLTY_DESCRIPTOR_H_S.NEXTVAL,
         :NEW.CAT_DSBLTY_DESCRIPTOR_ID,
         :NEW.CATASTROPHIC_DISABILITY_ID,
         :NEW.DESCRIPTOR_TYPE_ID,
         :NEW.DETERMINATION_DATE,
         :NEW.STD_INSTITUTION_ID,
         :NEW.Record_created_by,
         :NEW.Record_created_date,
         :NEW.Record_modified_by,
         :NEW.Record_modified_count,
         :NEW.Record_modified_date,
         SYSDATE,
         mDML
        );

    END IF;

  END CAT_DSBLTY_DESC_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_DESCRIPTOR FOR ADR.CAT_DSBLTY_DESCRIPTOR;


ALTER TABLE ADR.CAT_DSBLTY_DESCRIPTOR ADD (
  CONSTRAINT CAT_DSBLTY_DESCRIPTOR_PK
  PRIMARY KEY
  (CAT_DSBLTY_DESCRIPTOR_ID)
  USING INDEX ADR.CAT_DSBLTY_DESCRIPTOR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CAT_DSBLTY_DESCRIPTOR ADD (
  CONSTRAINT CAT_DSBLTY_CAT_DSBLTY_DESC_FK 
  FOREIGN KEY (CATASTROPHIC_DISABILITY_ID) 
  REFERENCES ADR.CATASTROPHIC_DISABILITY (CATASTROPHIC_DISABILITY_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CAT_DSBLTY_DESCRIPTOR TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_DESCRIPTOR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_DESCRIPTOR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_DESCRIPTOR_H
(
  CAT_DSBLTY_DESCRIPTOR_ID_H  NUMBER(20)        NOT NULL,
  CAT_DSBLTY_DESCRIPTOR_ID    NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20),
  DESCRIPTOR_TYPE_ID          NUMBER(20),
  DETERMINATION_DATE          DATE,
  STD_INSTITUTION_ID          NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  HISTORY_CREATED_DATE        DATE              NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.CAT_DSBLTY_DESCRIPTOR_H.CAT_DSBLTY_DESCRIPTOR_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DESCRIPTOR_H.CATASTROPHIC_DISABILITY_ID IS 'FK to ADR.CATASTROPHIC_DISABILITY.CATASTROPHIC_DISABILITY_ID';


CREATE UNIQUE INDEX ADR.CAT_DSBLTY_DESCRIPTOR_H_PK ON ADR.CAT_DSBLTY_DESCRIPTOR_H
(CAT_DSBLTY_DESCRIPTOR_ID_H)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_DESCRIPTOR_H FOR ADR.CAT_DSBLTY_DESCRIPTOR_H;


ALTER TABLE ADR.CAT_DSBLTY_DESCRIPTOR_H ADD (
  CONSTRAINT CAT_DSBLTY_DESCRIPTOR_H_PK
  PRIMARY KEY
  (CAT_DSBLTY_DESCRIPTOR_ID_H)
  USING INDEX ADR.CAT_DSBLTY_DESCRIPTOR_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CAT_DSBLTY_DESCRIPTOR_H TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_DESCRIPTOR_H TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_DIAGNOSIS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_DIAGNOSIS CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_DIAGNOSIS
(
  CAT_DSBLTY_DIAGNOSIS_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  DIAGNOSIS_TYPE_ID           NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_DIAGNOSIS IS 'The CAT_DSBLTY_DIAGNOSIS table is a child table of CATASTROPHIC_DISABILITY that shows the specific disability or disabilities with which the person has been diagnosed. CATASTROPHIC_DISABILITY_ID Foreign key to the CATASTROPHIC_DISABILITY table. DIAGNOSIS_TYPE_ID Foreign key to the DIAGNOSIS_TYPE. Indicates the type of disability that was diagnosed for a person (for example, paraplegia, legal blindness). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.CAT_DSBLTY_DIAGNOSIS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.DIAGNOSIS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSDIAG_CATDSBLTY_FK_I ON ADR.CAT_DSBLTY_DIAGNOSIS
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSDIAG_PK ON ADR.CAT_DSBLTY_DIAGNOSIS
(CAT_DSBLTY_DIAGNOSIS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSDIAG_STD_DIAGCD_FK_I ON ADR.CAT_DSBLTY_DIAGNOSIS
(DIAGNOSIS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CAT_DSBLTY_DIAG_audir_trgr
after Update or Delete or Insert
ON ADR.CAT_DSBLTY_DIAGNOSIS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into CAT_DSBLTY_DIAGNOSIS_H
		(
		CAT_DSBLTY_DIAGNOSIS_H_ID,
		CAT_DSBLTY_DIAGNOSIS_ID,
		CATASTROPHIC_DISABILITY_ID,
		DIAGNOSIS_TYPE_ID         ,
		RECORD_CREATED_BY         ,
		RECORD_CREATED_DATE       ,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE      ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CAT_DSBLTY_DIAGNOSIS_H_S.nextval,
		:old.CAT_DSBLTY_DIAGNOSIS_ID,
		:old.CATASTROPHIC_DISABILITY_ID,
		:old.DIAGNOSIS_TYPE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into CAT_DSBLTY_DIAGNOSIS_H
		(
		CAT_DSBLTY_DIAGNOSIS_H_ID,
		CAT_DSBLTY_DIAGNOSIS_ID,
		CATASTROPHIC_DISABILITY_ID,
		DIAGNOSIS_TYPE_ID         ,
		RECORD_CREATED_BY         ,
		RECORD_CREATED_DATE       ,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE      ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CAT_DSBLTY_DIAGNOSIS_H_S.nextval,
		:new.CAT_DSBLTY_DIAGNOSIS_ID,
		:new.CATASTROPHIC_DISABILITY_ID,
		:new.DIAGNOSIS_TYPE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;


end  CAT_DSBLTY_DIAG_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_DIAGNOSIS FOR ADR.CAT_DSBLTY_DIAGNOSIS;


ALTER TABLE ADR.CAT_DSBLTY_DIAGNOSIS ADD (
  CONSTRAINT CATDSDIAG_PK
  PRIMARY KEY
  (CAT_DSBLTY_DIAGNOSIS_ID)
  USING INDEX ADR.CATDSDIAG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CAT_DSBLTY_DIAGNOSIS ADD (
  CONSTRAINT CATDSDIAG_CATDSBLTY_FK 
  FOREIGN KEY (CATASTROPHIC_DISABILITY_ID) 
  REFERENCES ADR.CATASTROPHIC_DISABILITY (CATASTROPHIC_DISABILITY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CAT_DSBLTY_DIAGNOSIS TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_DIAGNOSIS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_DIAGNOSIS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_DIAGNOSIS_H
(
  CAT_DSBLTY_DIAGNOSIS_H_ID   NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  CAT_DSBLTY_DIAGNOSIS_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  DIAGNOSIS_TYPE_ID           NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_DIAGNOSIS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.CAT_DSBLTY_DIAGNOSIS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.CAT_DSBLTY_DIAGNOSIS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.DIAGNOSIS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_DIAGNOSIS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSDIAGH_CATDSBLTY_NU_I ON ADR.CAT_DSBLTY_DIAGNOSIS_H
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSDIAGH_PK ON ADR.CAT_DSBLTY_DIAGNOSIS_H
(CAT_DSBLTY_DIAGNOSIS_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_DIAGNOSIS_H FOR ADR.CAT_DSBLTY_DIAGNOSIS_H;


ALTER TABLE ADR.CAT_DSBLTY_DIAGNOSIS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_004
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CATDSDIAGH_PK
  PRIMARY KEY
  (CAT_DSBLTY_DIAGNOSIS_H_ID)
  USING INDEX ADR.CATDSDIAGH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS_H TO ADR_R;

GRANT SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS_H TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_DIAGNOSIS_H TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_PROCEDURE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_PROCEDURE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_PROCEDURE
(
  CAT_DSBLTY_PROCEDURE_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  PROCEDURE_TYPE_ID           NUMBER(20)        NOT NULL,
  AFFECTED_EXTREMITY_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_PROCEDURE IS 'The CAT_DSBLTY_PROCEDURE table is a child table of CATASTROPHIC_DISABILITY that shows the specific procedure, usually an amputation or disarticulation was part of the cause of disability. CATASTROPHIC_DISABILITY_ID Foreign key to the CATASTROPHIC_DISABILITY table. PROCEDURE_TYPE_ID Foreign key for the PROCEDURE_TYPE table. Indicates a specific procedure that is part of the cause of the disability (for example, amputation or disarticulation). AFFECTED_EXTREMITY_TYPE_ID Foreign key to the AFFECTED_EXTREMITY_TYPE table. Indicates which of a persons extremities were affected by the procedure. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.CAT_DSBLTY_PROCEDURE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.PROCEDURE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.AFFECTED_EXTREMITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSBLPRC_CATDSBLTY_FK_I ON ADR.CAT_DSBLTY_PROCEDURE
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLPRC_DIAGXTRMTY_FK_I ON ADR.CAT_DSBLTY_PROCEDURE
(AFFECTED_EXTREMITY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSBLPRC_PK ON ADR.CAT_DSBLTY_PROCEDURE
(CAT_DSBLTY_PROCEDURE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CATDSBLPRC_STD_PROCCD_FK_I ON ADR.CAT_DSBLTY_PROCEDURE
(PROCEDURE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CAT_DSBLTY_PROC_audir_trgr
after Update or Delete or Insert
ON ADR.CAT_DSBLTY_PROCEDURE
for each row
declare
   	mDML char(1);
begin
	if DELETING then
		insert into CAT_DSBLTY_PROCEDURE_H
		(
		CAT_DSBLTY_PROCEDURE_H_ID,
		CAT_DSBLTY_PROCEDURE_ID  ,
		CATASTROPHIC_DISABILITY_ID,
		PROCEDURE_TYPE_ID        ,
		AFFECTED_EXTREMITY_TYPE_ID,
		RECORD_CREATED_BY        ,
		RECORD_CREATED_DATE      ,
		RECORD_MODIFIED_BY       ,
		RECORD_MODIFIED_DATE     ,
		RECORD_MODIFIED_COUNT    ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CAT_DSBLTY_PROCEDURE_H_s.nextval,
		:old.CAT_DSBLTY_PROCEDURE_ID,
		:old.CATASTROPHIC_DISABILITY_ID,
		:old.PROCEDURE_TYPE_ID,
		:old.AFFECTED_EXTREMITY_TYPE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else

		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into CAT_DSBLTY_PROCEDURE_H
		(
		CAT_DSBLTY_PROCEDURE_H_ID,
		CAT_DSBLTY_PROCEDURE_ID  ,
		CATASTROPHIC_DISABILITY_ID,
		PROCEDURE_TYPE_ID        ,
		AFFECTED_EXTREMITY_TYPE_ID,
		RECORD_CREATED_BY        ,
		RECORD_CREATED_DATE      ,
		RECORD_MODIFIED_BY       ,
		RECORD_MODIFIED_DATE     ,
		RECORD_MODIFIED_COUNT    ,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CAT_DSBLTY_PROCEDURE_H_s.nextval,
		:new.CAT_DSBLTY_PROCEDURE_ID,
		:new.CATASTROPHIC_DISABILITY_ID,
		:new.PROCEDURE_TYPE_ID,
		:new.AFFECTED_EXTREMITY_TYPE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  CAT_DSBLTY_PROC_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_PROCEDURE FOR ADR.CAT_DSBLTY_PROCEDURE;


ALTER TABLE ADR.CAT_DSBLTY_PROCEDURE ADD (
  CONSTRAINT CATDSBLPRC_PK
  PRIMARY KEY
  (CAT_DSBLTY_PROCEDURE_ID)
  USING INDEX ADR.CATDSBLPRC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CAT_DSBLTY_PROCEDURE ADD (
  CONSTRAINT CATDSBLPRC_CATDSBLTY_FK 
  FOREIGN KEY (CATASTROPHIC_DISABILITY_ID) 
  REFERENCES ADR.CATASTROPHIC_DISABILITY (CATASTROPHIC_DISABILITY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_PROCEDURE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CAT_DSBLTY_PROCEDURE TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_PROCEDURE TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_PROCEDURE TO ES_RW;
ALTER TABLE ADR.CAT_DSBLTY_PROCEDURE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CAT_DSBLTY_PROCEDURE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CAT_DSBLTY_PROCEDURE_H
(
  CAT_DSBLTY_PROCEDURE_H_ID   NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  CAT_DSBLTY_PROCEDURE_ID     NUMBER(20)        NOT NULL,
  CATASTROPHIC_DISABILITY_ID  NUMBER(20)        NOT NULL,
  PROCEDURE_TYPE_ID           NUMBER(20)        NOT NULL,
  AFFECTED_EXTREMITY_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CAT_DSBLTY_PROCEDURE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.CAT_DSBLTY_PROCEDURE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.CAT_DSBLTY_PROCEDURE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.CATASTROPHIC_DISABILITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.PROCEDURE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.AFFECTED_EXTREMITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CAT_DSBLTY_PROCEDURE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CATDSBLPRH_CATDSBLTY_NU_I ON ADR.CAT_DSBLTY_PROCEDURE_H
(CATASTROPHIC_DISABILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CATDSBLPRH_PK ON ADR.CAT_DSBLTY_PROCEDURE_H
(CAT_DSBLTY_PROCEDURE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CAT_DSBLTY_PROCEDURE_H FOR ADR.CAT_DSBLTY_PROCEDURE_H;


ALTER TABLE ADR.CAT_DSBLTY_PROCEDURE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_035
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CATDSBLPRH_PK
  PRIMARY KEY
  (CAT_DSBLTY_PROCEDURE_H_ID)
  USING INDEX ADR.CATDSBLPRH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CAT_DSBLTY_PROCEDURE_H TO ADR_R;

GRANT SELECT ON ADR.CAT_DSBLTY_PROCEDURE_H TO ADR_RW;

GRANT SELECT ON ADR.CAT_DSBLTY_PROCEDURE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CAT_DSBLTY_PROCEDURE_H TO ES_RW;
ALTER TABLE ADR.CCN_CONTRACTOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_CONTRACTOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_CONTRACTOR
(
  CCN_CONTRACTOR_ID      NUMBER(20)             NOT NULL,
  CONTRACTOR_NAME        VARCHAR2(100 BYTE)     NOT NULL,
  ISACTIVE               VARCHAR2(1 BYTE),
  START_DATE             DATE,
  END_DATE               DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  CCN_CODE               VARCHAR2(20 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_CONTRACTOR IS 'Table will be used to hold CCN Contractors name';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.CCN_CONTRACTOR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.CONTRACTOR_NAME IS 'CCN Contractor Name';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.ISACTIVE IS 'active flag of the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.START_DATE IS 'start date of the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.END_DATE IS 'end date of the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CCN_CONTRACTOR_PK_I ON ADR.CCN_CONTRACTOR
(CCN_CONTRACTOR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CCN_CONTRACTOR_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.CCN_CONTRACTOR
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO CCN_CONTRACTOR_H (CCN_CONTRACTOR_H_ID,
                                    CCN_CONTRACTOR_ID,
                                    CONTRACTOR_NAME,
                                    ISACTIVE,
                                    START_DATE,
                                    END_DATE,
                                    CCN_CODE,
                                    RECORD_CREATED_BY,
                                    RECORD_CREATED_DATE,
                                    RECORD_MODIFIED_BY,
                                    RECORD_MODIFIED_DATE,
                                    RECORD_MODIFIED_COUNT,
                                    HISTORY_CREATED_DATE,
                                    TRANSACTION_TYPE_CODE)
           VALUES (CCN_CONTRACTOR_H_S.NEXTVAL,
                   :old.CCN_CONTRACTOR_ID,
                   :old.CONTRACTOR_NAME,
                   :old.ISACTIVE,
                   :old.START_DATE,
                   :old.END_DATE,
                   :old.CCN_CODE,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO CCN_CONTRACTOR_H (CCN_CONTRACTOR_H_ID,
                                    CCN_CONTRACTOR_ID,
                                    CONTRACTOR_NAME,
                                    ISACTIVE,
                                    START_DATE,
                                    END_DATE,
                                    CCN_CODE,
                                    RECORD_CREATED_BY,
                                    RECORD_CREATED_DATE,
                                    RECORD_MODIFIED_BY,
                                    RECORD_MODIFIED_DATE,
                                    RECORD_MODIFIED_COUNT,
                                    HISTORY_CREATED_DATE,
                                    TRANSACTION_TYPE_CODE)
           VALUES (CCN_CONTRACTOR_H_S.NEXTVAL,
                   :new.CCN_CONTRACTOR_ID,
                   :new.CONTRACTOR_NAME,
                   :new.ISACTIVE,
                   :new.START_DATE,
                   :new.END_DATE,
                   :new.CCN_CODE,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   END IF;
END CCN_CONTRACTOR_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CCN_CONTRACTOR FOR ADR.CCN_CONTRACTOR;


ALTER TABLE ADR.CCN_CONTRACTOR ADD (
  CONSTRAINT CCN_CONTRACTOR_PK
  PRIMARY KEY
  (CCN_CONTRACTOR_ID)
  USING INDEX ADR.CCN_CONTRACTOR_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_CONTRACTOR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_CONTRACTOR TO ADR_RW;

GRANT SELECT ON ADR.CCN_CONTRACTOR TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_CONTRACTOR TO VETCHOICE_RW;
ALTER TABLE ADR.CCN_CONTRACTOR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_CONTRACTOR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_CONTRACTOR_H
(
  CCN_CONTRACTOR_H_ID    NUMBER(20)             NOT NULL,
  CCN_CONTRACTOR_ID      NUMBER(20)             NOT NULL,
  CONTRACTOR_NAME        VARCHAR2(100 BYTE)     NOT NULL,
  ISACTIVE               VARCHAR2(1 BYTE),
  START_DATE             DATE,
  END_DATE               DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  HISTORY_CREATED_DATE   DATE                   NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE),
  CCN_CODE               VARCHAR2(20 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_CONTRACTOR_H IS 'Table will be used to hold CCN Contractors history info';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_H.HISTORY_CREATED_DATE IS 'the date this history record is created';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';


CREATE UNIQUE INDEX ADR.CCN_CONTRACTOR_H_PK_I ON ADR.CCN_CONTRACTOR_H
(CCN_CONTRACTOR_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_CONTRACTOR_H FOR ADR.CCN_CONTRACTOR_H;


ALTER TABLE ADR.CCN_CONTRACTOR_H ADD (
  CONSTRAINT CCN_CONTRACTOR_H_PK
  PRIMARY KEY
  (CCN_CONTRACTOR_H_ID)
  USING INDEX ADR.CCN_CONTRACTOR_H_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_CONTRACTOR_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.CCN_CONTRACTOR_H TO ADR_RW;

GRANT SELECT ON ADR.CCN_CONTRACTOR_H TO VETCHOICE_R;
ALTER TABLE ADR.CCN_CONTRACTOR_REGION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_CONTRACTOR_REGION CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_CONTRACTOR_REGION
(
  CCN_CONTRACTOR_REGION_ID  NUMBER(20)          NOT NULL,
  CCN_CONTRACTOR_ID         NUMBER(20)          NOT NULL,
  CCN_REGION_ID             NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_CONTRACTOR_REGION IS 'Table (many to many between CCN_CONTRACTOR and CCN_REGION) will be used to hold CCN Contractors assigned to any Regions';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.CCN_CONTRACTOR_ID IS 'FK to CCN_CONTRACTOR';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.CCN_REGION_ID IS 'FK to CCN_REGION';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CCN_CONTRACTOR_ID_FK_I ON ADR.CCN_CONTRACTOR_REGION
(CCN_CONTRACTOR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CCN_CONTRACTOR_REGION_PK_I ON ADR.CCN_CONTRACTOR_REGION
(CCN_CONTRACTOR_REGION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCN_REGION_ID_FK_I ON ADR.CCN_CONTRACTOR_REGION
(CCN_REGION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CCN_CONTRA_REGION_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.CCN_CONTRACTOR_REGION
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO CCN_CONTRACTOR_REGION_H (CCN_CONTRACTOR_REGION_H_ID,
                                           CCN_CONTRACTOR_REGION_ID,
                                           CCN_CONTRACTOR_ID,
                                           CCN_REGION_ID,
                                           RECORD_CREATED_BY,
                                           RECORD_CREATED_DATE,
                                           RECORD_MODIFIED_BY,
                                           RECORD_MODIFIED_DATE,
                                           RECORD_MODIFIED_COUNT,
                                           HISTORY_CREATED_DATE,
                                           TRANSACTION_TYPE_CODE)
           VALUES (CCN_CONTRACTOR_REGION_H_S.NEXTVAL,
                   :old.CCN_CONTRACTOR_REGION_ID,
                   :old.CCN_CONTRACTOR_ID,
                   :old.CCN_REGION_ID,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO CCN_CONTRACTOR_REGION_H (CCN_CONTRACTOR_REGION_H_ID,
                                           CCN_CONTRACTOR_REGION_ID,
                                           CCN_CONTRACTOR_ID,
                                           CCN_REGION_ID,
                                           RECORD_CREATED_BY,
                                           RECORD_CREATED_DATE,
                                           RECORD_MODIFIED_BY,
                                           RECORD_MODIFIED_DATE,
                                           RECORD_MODIFIED_COUNT,
                                           HISTORY_CREATED_DATE,
                                           TRANSACTION_TYPE_CODE)
           VALUES (CCN_CONTRACTOR_REGION_H_S.NEXTVAL,
                   :new.CCN_CONTRACTOR_REGION_ID,
                   :new.CCN_CONTRACTOR_ID,
                   :new.CCN_REGION_ID,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   END IF;
END CCN_CONTRA_REGION_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CCN_CONTRACTOR_REGION FOR ADR.CCN_CONTRACTOR_REGION;


ALTER TABLE ADR.CCN_CONTRACTOR_REGION ADD (
  CONSTRAINT CCN_CONTRACTOR_REGION_PK
  PRIMARY KEY
  (CCN_CONTRACTOR_REGION_ID)
  USING INDEX ADR.CCN_CONTRACTOR_REGION_PK_I
  ENABLE VALIDATE);

ALTER TABLE ADR.CCN_CONTRACTOR_REGION ADD (
  CONSTRAINT CCN_CONTRACTOR_ID_FK 
  FOREIGN KEY (CCN_CONTRACTOR_ID) 
  REFERENCES ADR.CCN_CONTRACTOR (CCN_CONTRACTOR_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCN_REGION_ID_FK 
  FOREIGN KEY (CCN_REGION_ID) 
  REFERENCES ADR.CCN_REGION (CCN_REGION_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_CONTRACTOR_REGION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_CONTRACTOR_REGION TO ADR_RW;

GRANT SELECT ON ADR.CCN_CONTRACTOR_REGION TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_CONTRACTOR_REGION TO VETCHOICE_RW;
ALTER TABLE ADR.CCN_CONTRACTOR_REGION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_CONTRACTOR_REGION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_CONTRACTOR_REGION_H
(
  CCN_CONTRACTOR_REGION_H_ID  NUMBER(20)        NOT NULL,
  CCN_CONTRACTOR_REGION_ID    NUMBER(20)        NOT NULL,
  CCN_CONTRACTOR_ID           NUMBER(20)        NOT NULL,
  CCN_REGION_ID               NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  HISTORY_CREATED_DATE        DATE              NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_CONTRACTOR_REGION_H IS 'History table will be used to hold history for CCN_CONTRACTOR_REGION.';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION_H.HISTORY_CREATED_DATE IS 'the date this history record is created';

COMMENT ON COLUMN ADR.CCN_CONTRACTOR_REGION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';


CREATE UNIQUE INDEX ADR.CCN_CONTRACTOR_REGION_H_PK_I ON ADR.CCN_CONTRACTOR_REGION_H
(CCN_CONTRACTOR_REGION_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_CONTRACTOR_REGION_H FOR ADR.CCN_CONTRACTOR_REGION_H;


ALTER TABLE ADR.CCN_CONTRACTOR_REGION_H ADD (
  CONSTRAINT CCN_CONTRACTOR_REGION_H_PK
  PRIMARY KEY
  (CCN_CONTRACTOR_REGION_H_ID)
  USING INDEX ADR.CCN_CONTRACTOR_REGION_H_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_CONTRACTOR_REGION_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.CCN_CONTRACTOR_REGION_H TO ADR_RW;

GRANT SELECT ON ADR.CCN_CONTRACTOR_REGION_H TO VETCHOICE_R;
ALTER TABLE ADR.CCN_FILETRANSFER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_FILETRANSFER CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_FILETRANSFER
(
  CCN_FILETRANSFER_ID    NUMBER(20)             NOT NULL,
  TRANSMISSION_TYPE_ID   NUMBER(20)             NOT NULL,
  VCADMISTATUS_TYPE_ID   NUMBER(20)             NOT NULL,
  DOC_REF                VARCHAR2(50 BYTE),
  FILE_NAME              VARCHAR2(60 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_FILETRANSFER IS 'Table to hold CCN file name and transfer status';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.CCN_FILETRANSFER_ID IS 'The primary key, generated by the database system in numeric, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.TRANSMISSION_TYPE_ID IS 'TRANSMISSION_TYPE_ID referenced by TRANSMISSION_TYPE';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.VCADMISTATUS_TYPE_ID IS 'VCADMISTATUS_TYPE_ID referenced by VCADMISTATUS_TYPE';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.DOC_REF IS 'is document reference being used by the file transfered';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.FILE_NAME IS 'is the file name';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CCNFITRANSFER_TRNSTYPEID_FK_I ON ADR.CCN_FILETRANSFER
(TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNFITRANSF_VCADSTPIDD_FK_I ON ADR.CCN_FILETRANSFER
(VCADMISTATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CCN_FILETRANS_ID_PK_I ON ADR.CCN_FILETRANSFER
(CCN_FILETRANSFER_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CCNFILETRANS_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.CCN_FILETRANSFER
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO CCN_FILETRANSFER_H (CCN_FILETRANSFER_H_ID,
                                      CCN_FILETRANSFER_ID,
                                      TRANSMISSION_TYPE_ID,
                                      VCADMISTATUS_TYPE_ID,
                                      DOC_REF,
                                      FILE_NAME,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      HISTORY_CREATED_DATE,
                                      TRANSACTION_TYPE_CODE)
           VALUES (CCN_FILETRANSFER_H_S.NEXTVAL,
                   :old.CCN_FILETRANSFER_ID,
                   :old.TRANSMISSION_TYPE_ID,
                   :old.VCADMISTATUS_TYPE_ID,
                   :old.DOC_REF,
                   :old.FILE_NAME,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO CCN_FILETRANSFER_H (CCN_FILETRANSFER_H_ID,
                                      CCN_FILETRANSFER_ID,
                                      TRANSMISSION_TYPE_ID,
                                      VCADMISTATUS_TYPE_ID,
                                      DOC_REF,
                                      FILE_NAME,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      HISTORY_CREATED_DATE,
                                      TRANSACTION_TYPE_CODE)
           VALUES (CCN_FILETRANSFER_H_S.NEXTVAL,
                   :new.CCN_FILETRANSFER_ID,
                   :new.TRANSMISSION_TYPE_ID,
                   :new.VCADMISTATUS_TYPE_ID,
                   :new.DOC_REF,
                   :new.FILE_NAME,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,
                   SYSDATE,
                   mDML);
   END IF;
END CCNFILETRANS_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CCN_FILETRANSFER FOR ADR.CCN_FILETRANSFER;


ALTER TABLE ADR.CCN_FILETRANSFER ADD (
  CONSTRAINT CCN_FILETRANS_ID_PK
  PRIMARY KEY
  (CCN_FILETRANSFER_ID)
  USING INDEX ADR.CCN_FILETRANS_ID_PK_I
  ENABLE VALIDATE);

ALTER TABLE ADR.CCN_FILETRANSFER ADD (
  CONSTRAINT CCNFITRANSFER_TRNSTYPEID_FK 
  FOREIGN KEY (TRANSMISSION_TYPE_ID) 
  REFERENCES ADR.TRANSMISSION_TYPE (TRANSMISSION_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNFITRANSF_VCADSTPIDD_FK_I 
  FOREIGN KEY (VCADMISTATUS_TYPE_ID) 
  REFERENCES ADR.VCADMISTATUS_TYPE (VCADMISTATUS_TYPE_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_FILETRANSFER TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_FILETRANSFER TO ADR_RW;

GRANT SELECT ON ADR.CCN_FILETRANSFER TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_FILETRANSFER TO VETCHOICE_RW;
ALTER TABLE ADR.CCN_FILETRANSFER_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_FILETRANSFER_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_FILETRANSFER_H
(
  CCN_FILETRANSFER_H_ID  NUMBER(20)             NOT NULL,
  CCN_FILETRANSFER_ID    NUMBER(20),
  TRANSMISSION_TYPE_ID   NUMBER(20),
  VCADMISTATUS_TYPE_ID   NUMBER(20),
  DOC_REF                VARCHAR2(50 BYTE),
  FILE_NAME              VARCHAR2(60 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE    DATE,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE   DATE,
  RECORD_MODIFIED_COUNT  NUMBER(10),
  HISTORY_CREATED_DATE   DATE                   NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_FILETRANSFER_H IS 'The Standard History table name _H .Table to hold CCN file name and transfer status';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.CCN_FILETRANSFER_H_ID IS 'The primary key, generated by the database system in numeric, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.CCN_FILETRANSFER_ID IS 'The primary key of table CCN_FILETRANSFER';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.TRANSMISSION_TYPE_ID IS 'TRANSMISSION_TYPE_ID referenced by TRANSMISSION_TYPE';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.VCADMISTATUS_TYPE_ID IS 'VCADMISTATUS_TYPE_ID referenced by VCADMISTATUS_TYPE';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.DOC_REF IS 'is document reference being used by the file transfered';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.FILE_NAME IS 'is the file name';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.HISTORY_CREATED_DATE IS 'history record created by TRANSACTION_TYPE_CODE.';

COMMENT ON COLUMN ADR.CCN_FILETRANSFER_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';


CREATE UNIQUE INDEX ADR.CCNFILETRANSHID_PK_I ON ADR.CCN_FILETRANSFER_H
(CCN_FILETRANSFER_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_FILETRANSFER_H FOR ADR.CCN_FILETRANSFER_H;


ALTER TABLE ADR.CCN_FILETRANSFER_H ADD (
  CONSTRAINT CCNFILETRANSHID_PK
  PRIMARY KEY
  (CCN_FILETRANSFER_H_ID)
  USING INDEX ADR.CCNFILETRANSHID_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_FILETRANSFER_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.CCN_FILETRANSFER_H TO ADR_RW;

GRANT SELECT ON ADR.CCN_FILETRANSFER_H TO VETCHOICE_R;
ALTER TABLE ADR.CCN_RECEIVED_SERVICE_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_RECEIVED_SERVICE_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_RECEIVED_SERVICE_LOG
(
  CCN_RECEIVED_SERVICE_LOG_ID  NUMBER(20)       NOT NULL,
  CCN_SERVICE_LOG_ID           NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  CCN_CONTRACTOR_ID            NUMBER(20)       NOT NULL,
  VCADMINSTATUS_TYPE_ID        NUMBER(20),
  ACK_DATE                     DATE             NOT NULL,
  COMMENTS                     VARCHAR2(200 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION CCNRECVDSRVLOG_P1 VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_RECEIVED_SERVICE_LOG IS 'Log table will be used to log any message while receiving Veteran¿s information from CCN Contractors. the bridge tables for realization of the many to many relationship between CCN_SERVICE_LOG and CCN_CONTRACTOR tables';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.CCN_RECEIVED_SERVICE_LOG_ID IS 'The FK to the CCN_RECEIVED_SERVICE_LOG table';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.CCN_CONTRACTOR_ID IS 'FK to the CCN_CONTRACTOR table';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.COMMENTS IS 'comments to the acknowledgement';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CCN_RECEIVED_SERVICE_LOG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CCNRSERVLOG_CONTRACTORID_FK_I ON ADR.CCN_RECEIVED_SERVICE_LOG
(CCN_CONTRACTOR_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNRSERVLOG_PERSONID_FK_I ON ADR.CCN_RECEIVED_SERVICE_LOG
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNRSERVLOG_VCADMNSTATPID_FK_I ON ADR.CCN_RECEIVED_SERVICE_LOG
(VCADMINSTATUS_TYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CCN_RECEIVED_SERVICE_LOG_PK_I ON ADR.CCN_RECEIVED_SERVICE_LOG
(CCN_RECEIVED_SERVICE_LOG_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RCVDSRVLOG_SRVLOGID_FK_I ON ADR.CCN_RECEIVED_SERVICE_LOG
(CCN_SERVICE_LOG_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_RECEIVED_SERVICE_LOG FOR ADR.CCN_RECEIVED_SERVICE_LOG;


ALTER TABLE ADR.CCN_RECEIVED_SERVICE_LOG ADD (
  CONSTRAINT CCN_RECEIVED_SERVICE_LOG_PK
  PRIMARY KEY
  (CCN_RECEIVED_SERVICE_LOG_ID)
  USING INDEX ADR.CCN_RECEIVED_SERVICE_LOG_PK_I
  ENABLE VALIDATE);

ALTER TABLE ADR.CCN_RECEIVED_SERVICE_LOG ADD (
  CONSTRAINT CCNRSERVLOG_CONTRACTORID_FK 
  FOREIGN KEY (CCN_CONTRACTOR_ID) 
  REFERENCES ADR.CCN_CONTRACTOR (CCN_CONTRACTOR_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNRSERVLOG_PERSONID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNRSERVLOG_VCADMNSTATPID_FK 
  FOREIGN KEY (VCADMINSTATUS_TYPE_ID) 
  REFERENCES ADR.VCADMISTATUS_TYPE (VCADMISTATUS_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT RCVDSRVLOG_SRVLOGID_FK 
  FOREIGN KEY (CCN_SERVICE_LOG_ID) 
  REFERENCES ADR.CCN_SERVICE_LOG (CCN_SERVICE_LOG_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_RECEIVED_SERVICE_LOG TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.CCN_RECEIVED_SERVICE_LOG TO ADR_RW;

GRANT SELECT ON ADR.CCN_RECEIVED_SERVICE_LOG TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_RECEIVED_SERVICE_LOG TO VETCHOICE_RW;
ALTER TABLE ADR.CCN_REGION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_REGION CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_REGION
(
  CCN_REGION_ID          NUMBER(20)             NOT NULL,
  REGION_NAME            VARCHAR2(100 BYTE)     NOT NULL,
  ISACTIVE               VARCHAR2(1 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_REGION IS 'Table will be used to hold CCN Regions name';

COMMENT ON COLUMN ADR.CCN_REGION.CCN_REGION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_REGION.REGION_NAME IS 'CCN Region Name';

COMMENT ON COLUMN ADR.CCN_REGION.ISACTIVE IS 'active flag of the record';

COMMENT ON COLUMN ADR.CCN_REGION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_REGION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_REGION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_REGION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CCN_REGION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CCN_REGION_PK_I ON ADR.CCN_REGION
(CCN_REGION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_REGION FOR ADR.CCN_REGION;


ALTER TABLE ADR.CCN_REGION ADD (
  CONSTRAINT CCN_REGION_PK
  PRIMARY KEY
  (CCN_REGION_ID)
  USING INDEX ADR.CCN_REGION_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_REGION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_REGION TO ADR_RW;

GRANT SELECT ON ADR.CCN_REGION TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_REGION TO VETCHOICE_RW;
ALTER TABLE ADR.CCN_REGION_STATE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_REGION_STATE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_REGION_STATE
(
  CCN_REGION_STATE_ID    NUMBER(20)             NOT NULL,
  STATE_NAME             VARCHAR2(100 BYTE)     NOT NULL,
  CCN_REGION_ID          NUMBER(20)             NOT NULL,
  ISACTIVE               VARCHAR2(1 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_REGION_STATE IS 'Table will be used to hold CCN Region and State relation';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.CCN_REGION_STATE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.STATE_NAME IS 'state name for CCN';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.CCN_REGION_ID IS 'FK (many to 1) to CCN_REGION table  ';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.ISACTIVE IS 'active flag; 1 for active; 0 or others for inactive';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_REGION_STATE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CCN_REGION_STATE_PK_I ON ADR.CCN_REGION_STATE
(CCN_REGION_STATE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CCN_REGION_STATE_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.CCN_REGION_STATE
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO CCN_REGION_STATE_H (CCN_REGION_STATE_H_ID,
                                      CCN_REGION_STATE_ID,
                                      STATE_NAME,
                                      CCN_REGION_ID,
                                      ISACTIVE,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      HISTORY_CREATED_DATE,
                                      TRANSACTION_TYPE_CODE)
           VALUES (CCN_REGION_STATE_H_S.NEXTVAL,
                   :old.CCN_REGION_STATE_ID,
                   :old.STATE_NAME,
                   :old.CCN_REGION_ID,
                   :old.ISACTIVE,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,

                   SYSDATE,
                   mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO CCN_REGION_STATE_H (CCN_REGION_STATE_H_ID,
                                      CCN_REGION_STATE_ID,
                                      STATE_NAME,
                                      CCN_REGION_ID,
                                      ISACTIVE,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      HISTORY_CREATED_DATE,
                                      TRANSACTION_TYPE_CODE)
           VALUES (CCN_REGION_STATE_H_S.NEXTVAL,
                   :new.CCN_REGION_STATE_ID,
                   :new.STATE_NAME,
                   :new.CCN_REGION_ID,
                   :new.ISACTIVE,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,

                   SYSDATE,
                   mDML);
   END IF;
END CCN_REGION_STATE_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CCN_REGION_STATE FOR ADR.CCN_REGION_STATE;


ALTER TABLE ADR.CCN_REGION_STATE ADD (
  CONSTRAINT CCN_REGION_STATE_PK
  PRIMARY KEY
  (CCN_REGION_STATE_ID)
  USING INDEX ADR.CCN_REGION_STATE_PK_I
  ENABLE VALIDATE);

ALTER TABLE ADR.CCN_REGION_STATE ADD (
  CONSTRAINT CCNRSNSTA_REGION_ID_FK 
  FOREIGN KEY (CCN_REGION_ID) 
  REFERENCES ADR.CCN_REGION (CCN_REGION_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_REGION_STATE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_REGION_STATE TO ADR_RW;

GRANT SELECT ON ADR.CCN_REGION_STATE TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_REGION_STATE TO VETCHOICE_RW;
DROP TABLE ADR.CCN_REGION_STATE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_REGION_STATE_H
(
  CCN_REGION_STATE_H_ID  NUMBER(20)             NOT NULL,
  CCN_REGION_STATE_ID    NUMBER(20),
  STATE_NAME             VARCHAR2(100 BYTE),
  CCN_REGION_ID          NUMBER(20),
  ISACTIVE               VARCHAR2(1 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE    DATE,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE   DATE,
  RECORD_MODIFIED_COUNT  NUMBER(10),
  HISTORY_CREATED_DATE   DATE                   NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CCN_REGION_STATE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.CCN_REGION_STATE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.CCN_REGION_STATE_ID IS 'The primary key of CCN_REGION_STATE';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.STATE_NAME IS 'state name for CCN';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.CCN_REGION_ID IS 'FK (many to 1) to CCN_REGION table  ';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.ISACTIVE IS 'active flag; 1 for active; 0 or others for inactive';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.HISTORY_CREATED_DATE IS 'created date of this history record';

COMMENT ON COLUMN ADR.CCN_REGION_STATE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';


CREATE UNIQUE INDEX ADR.CCN_REGION_STATE_H_PK ON ADR.CCN_REGION_STATE_H
(CCN_REGION_STATE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_REGION_STATE_H FOR ADR.CCN_REGION_STATE_H;


GRANT SELECT ON ADR.CCN_REGION_STATE_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.CCN_REGION_STATE_H TO ADR_RW;

GRANT SELECT ON ADR.CCN_REGION_STATE_H TO VETCHOICE_R;
ALTER TABLE ADR.CCN_SERVICE_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CCN_SERVICE_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.CCN_SERVICE_LOG
(
  CCN_SERVICE_LOG_ID       NUMBER(20)           NOT NULL,
  PERSON_ID                NUMBER(20)           NOT NULL,
  EDIPI                    NUMBER(10),
  FIRST_NAME               VARCHAR2(35 BYTE),
  LAST_NAME                VARCHAR2(35 BYTE),
  MID_NAME                 VARCHAR2(25 BYTE),
  SUFFIX                   VARCHAR2(10 BYTE),
  DATE_OF_BIRTH            DATE,
  DATE_OF_DEATH            VARCHAR2(14 BYTE),
  BIRTH_SEX                VARCHAR2(1 BYTE),
  MAILING_ADDRESS_H_ID     NUMBER(20)           NOT NULL,
  EMAIL                    VARCHAR2(128 BYTE),
  VCELIGIBILITY_H_ID       NUMBER(20)           NOT NULL,
  HPHONE_NUM               VARCHAR2(50 BYTE),
  CPHONE_NUM               VARCHAR2(50 BYTE),
  TPHONE_NUM               VARCHAR2(50 BYTE),
  NOT_ENR_ES               VARCHAR2(1 BYTE),
  NOT_ENR_ES_DATE          DATE,
  VISN_GEO                 NUMBER(2),
  TRANSMISSION_TYPE_ID     NUMBER(20),
  TRANSMISSION_STTYPE_ID   NUMBER(20),
  TRANSMISSION_DATE        DATE                 NOT NULL,
  COMMENTS                 VARCHAR2(200 BYTE),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL,
  CCN_FILETRANSFER_ID      NUMBER(20),
  RESI_ADDRESS_H_ID        NUMBER(20),
  PREFERRED_FACILITY_H_ID  NUMBER(20),
  GEOCODE_STATION_H_ID     NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (TRANSMISSION_DATE)
INTERVAL( NUMTOYMINTERVAL(3,'MONTH'))
SUBPARTITION BY HASH (PERSON_ID)
SUBPARTITION TEMPLATE
  (SUBPARTITION SRLOG_SP1 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP2 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP3 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP4 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP5 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP6 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP7 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP8 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP9 TABLESPACE L_DATA_01,
   SUBPARTITION SRLOG_SP10 TABLESPACE L_DATA_01
  )
(  
  PARTITION SRLOG_TRANSID_P01 VALUES LESS THAN (TO_DATE(' 2017-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
  ( SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP1      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP2      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP3      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP4      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP5      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP6      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP7      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP8      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP9      TABLESPACE L_DATA_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP10      TABLESPACE L_DATA_01 ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    NOLOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
    SUBPARTITIONS 10 STORE IN (L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01,L_DATA_01)
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.CCN_SERVICE_LOG.GEOCODE_STATION_H_ID IS 'FK referenced to geocode_station_h table.';


CREATE UNIQUE INDEX ADR.CCNSERVLOG_CCNSERVLOG_ID_PK ON ADR.CCN_SERVICE_LOG
(CCN_SERVICE_LOG_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNSERVLOG_MAILADDRHID_FK_I ON ADR.CCN_SERVICE_LOG
(MAILING_ADDRESS_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNSERVLOG_PERSION_ID_NU ON ADR.CCN_SERVICE_LOG
(PERSON_ID)
  TABLESPACE M_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION SRLOG_TRANSID_P01
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
  ( SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP1      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP2      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP3      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP4      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP5      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP6      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP7      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP8      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP9      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP10      TABLESPACE M_INDEX_01 ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
    STORE IN (M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01)
)
NOPARALLEL;

CREATE INDEX ADR.CCNSERVLOG_TRANSMISSION_ID_NU ON ADR.CCN_SERVICE_LOG
(TRANSMISSION_DATE)
  TABLESPACE M_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION SRLOG_TRANSID_P01
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
  ( SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP1      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP2      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP3      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP4      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP5      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP6      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP7      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP8      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP9      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP10      TABLESPACE M_INDEX_01 ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
    STORE IN (M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01)
)
NOPARALLEL;

CREATE INDEX ADR.CCNSERVLOG_TRNSMNTYPEID_NU_I ON ADR.CCN_SERVICE_LOG
(TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CCNSERVLOG_VCELIGBTY_H_ID_FK_I ON ADR.CCN_SERVICE_LOG
(VCELIGIBILITY_H_ID)
  TABLESPACE M_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION SRLOG_TRANSID_P01
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
  ( SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP1      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP2      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP3      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP4      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP5      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP6      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP7      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP8      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP9      TABLESPACE M_INDEX_01,
    SUBPARTITION SRLOG_TRANSID_P01_SRLOG_SP10      TABLESPACE M_INDEX_01 ),  
  PARTITION
    NOLOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
    STORE IN (M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01,M_INDEX_01)
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CCN_SERVICE_LOG FOR ADR.CCN_SERVICE_LOG;


ALTER TABLE ADR.CCN_SERVICE_LOG ADD (
  CONSTRAINT CCNSERVLOG_CCNSERVLOG_ID_PK
  PRIMARY KEY
  (CCN_SERVICE_LOG_ID)
  USING INDEX ADR.CCNSERVLOG_CCNSERVLOG_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CCN_SERVICE_LOG ADD (
  CONSTRAINT CCNSERVLOG_GEOCODE_STATION_FK 
  FOREIGN KEY (GEOCODE_STATION_H_ID) 
  REFERENCES ADR.GEOCODE_STATION_H (GEOCODE_STATION_H_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_MAILADDRHID_FK 
  FOREIGN KEY (MAILING_ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_PERSON_ID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_PFFACHID_FK 
  FOREIGN KEY (PREFERRED_FACILITY_H_ID) 
  REFERENCES ADR.PREFERRED_FACILITY_H (PREFERRED_FACILITY_H_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_RESADRHID_FK 
  FOREIGN KEY (RESI_ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_TRANSTASTTYPEID_FK 
  FOREIGN KEY (TRANSMISSION_STTYPE_ID) 
  REFERENCES ADR.VCADMISTATUS_TYPE (VCADMISTATUS_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_TRANSTYPE_ID_FK 
  FOREIGN KEY (TRANSMISSION_TYPE_ID) 
  REFERENCES ADR.TRANSMISSION_TYPE (TRANSMISSION_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSERVLOG_VCELIGBTY_H_ID_FK 
  FOREIGN KEY (VCELIGIBILITY_H_ID) 
  REFERENCES ADR.VCELIGIBILITY_H (VCELIGIBILITY_H_ID)
  ENABLE VALIDATE,
  CONSTRAINT CCNSVCLOG_CCNFLTR_ID_FK 
  FOREIGN KEY (CCN_FILETRANSFER_ID) 
  REFERENCES ADR.CCN_FILETRANSFER (CCN_FILETRANSFER_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CCN_SERVICE_LOG TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.CCN_SERVICE_LOG TO ADR_RW;

GRANT SELECT ON ADR.CCN_SERVICE_LOG TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CCN_SERVICE_LOG TO VETCHOICE_RW;
ALTER TABLE ADR.CLOSE_APL_RSN_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CLOSE_APL_RSN_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CLOSE_APL_RSN_TYPE
(
  CLOSE_APL_RSN_TYPE_ID  NUMBER(20)             NOT NULL,
  CODE                   VARCHAR2(2 BYTE)       NOT NULL,
  NAME                   VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(100 BYTE),
  ISACTIVE               CHAR(1 BYTE)           NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CLOSE_APL_RSN_TYPE IS 'lookup table for Close Application reasons';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.CLOSE_APL_RSN_TYPE_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.CODE IS 'code value';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.NAME IS 'name of the code';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.DESCRIPTION IS 'DESCRIPTION';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CLOSE_APL_RSN_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CLOSE_APL_RSN_TYPE_CD_UK ON ADR.CLOSE_APL_RSN_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CLOSE_APL_RSN_TYPE_NM_UK ON ADR.CLOSE_APL_RSN_TYPE
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CLOSE_APL_RSN_TYPE_PK ON ADR.CLOSE_APL_RSN_TYPE
(CLOSE_APL_RSN_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CLOSE_APL_RSN_TYPE FOR ADR.CLOSE_APL_RSN_TYPE;


ALTER TABLE ADR.CLOSE_APL_RSN_TYPE ADD (
  CONSTRAINT CLOSE_APL_RSN_TYPE_PK
  PRIMARY KEY
  (CLOSE_APL_RSN_TYPE_ID)
  USING INDEX ADR.CLOSE_APL_RSN_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CLOSE_APL_RSN_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CLOSE_APL_RSN_TYPE TO ADR_RW;
ALTER TABLE ADR.CL_VERIFICATION_METHOD
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CL_VERIFICATION_METHOD CASCADE CONSTRAINTS;

CREATE TABLE ADR.CL_VERIFICATION_METHOD
(
  CL_VERIFICATION_METHOD_ID    NUMBER(20)       NOT NULL,
  SPECIAL_FACTOR_ID            NUMBER(20)       NOT NULL,
  CL_VERIFICATION_METHOD_CODE  NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CL_VERIFICATION_METHOD IS 'This table contains CAMP LEJEUNE eligibility verification method information.  A CAMP LEJEUNE special factor can have 0 to many different verification types.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.CL_VERIFICATION_METHOD_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.SPECIAL_FACTOR_ID IS 'FK to ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_ID';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.CL_VERIFICATION_METHOD_CODE IS 'FK to ADRREF.STD_CLVERIFICATIONTYPE.ID';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n';


CREATE UNIQUE INDEX ADR.CL_VERIFICATION_METHOD_PK ON ADR.CL_VERIFICATION_METHOD
(CL_VERIFICATION_METHOD_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.SP_FAC_ID_CL_VER_METH_CD_UI ON ADR.CL_VERIFICATION_METHOD
(SPECIAL_FACTOR_ID, CL_VERIFICATION_METHOD_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CL_VERIF_METHOD_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.CL_VERIFICATION_METHOD
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into CL_VERIFICATION_METHOD_H
        (
         CL_VERIFICATION_METHOD_H_ID,
         CL_VERIFICATION_METHOD_ID,
         SPECIAL_FACTOR_ID,
         CL_VERIFICATION_METHOD_CODE,
         RECORD_CREATED_BY ,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        values
        (
        CL_VERIFICATION_METHOD_H_S.nextval,
         :old.CL_VERIFICATION_METHOD_ID,
         :old.SPECIAL_FACTOR_ID,
         :old.CL_VERIFICATION_METHOD_CODE,
         :old.RECORD_CREATED_BY,
         :old.RECORD_CREATED_DATE,
         :old.RECORD_MODIFIED_BY,
         :old.RECORD_MODIFIED_DATE,
         :old.RECORD_MODIFIED_COUNT,
         sysdate,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;
        insert into CL_VERIFICATION_METHOD_H
        (
         CL_VERIFICATION_METHOD_H_ID,
         CL_VERIFICATION_METHOD_ID,
         SPECIAL_FACTOR_ID,
         CL_VERIFICATION_METHOD_CODE,
         RECORD_CREATED_BY ,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        values
        (
        CL_VERIFICATION_METHOD_H_S.nextval,
         :new.CL_VERIFICATION_METHOD_ID,
         :new.SPECIAL_FACTOR_ID,
         :new.CL_VERIFICATION_METHOD_CODE,
         :new.RECORD_CREATED_BY,
         :new.RECORD_CREATED_DATE,
         :new.RECORD_MODIFIED_BY,
         :new.RECORD_MODIFIED_DATE,
         :new.RECORD_MODIFIED_COUNT,
         sysdate,
        mDML
        );
    end if;

end CL_VERIF_METHOD_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CL_VERIFICATION_METHOD FOR ADR.CL_VERIFICATION_METHOD;


ALTER TABLE ADR.CL_VERIFICATION_METHOD ADD (
  CONSTRAINT CL_VERIFICATION_METHOD_PK
  PRIMARY KEY
  (CL_VERIFICATION_METHOD_ID)
  USING INDEX ADR.CL_VERIFICATION_METHOD_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CL_VERIFICATION_METHOD ADD (
  CONSTRAINT SPC_FAC_CL_VER_METH_FK 
  FOREIGN KEY (SPECIAL_FACTOR_ID) 
  REFERENCES ADR.SPECIAL_FACTOR (SPECIAL_FACTOR_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT STD_CLVERIFTYP_CLVERMETH_FK 
  FOREIGN KEY (CL_VERIFICATION_METHOD_CODE) 
  REFERENCES ADRREF.STD_CLVERIFICATIONTYPE (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CL_VERIFICATION_METHOD TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CL_VERIFICATION_METHOD TO ADR_RW;

GRANT SELECT ON ADR.CL_VERIFICATION_METHOD TO ES_R;

GRANT INSERT, SELECT ON ADR.CL_VERIFICATION_METHOD TO ES_RW;
ALTER TABLE ADR.CL_VERIFICATION_METHOD_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CL_VERIFICATION_METHOD_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CL_VERIFICATION_METHOD_H
(
  CL_VERIFICATION_METHOD_ID    NUMBER(20),
  SPECIAL_FACTOR_ID            NUMBER(20),
  CL_VERIFICATION_METHOD_CODE  NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  CL_VERIFICATION_METHOD_H_ID  NUMBER(20)       NOT NULL,
  HISTORY_CREATED_DATE         DATE             NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CL_VERIFICATION_METHOD_H IS 'This table contains CAMP LEJEUNE eligibility verification method information.  A CAMP LEJEUNE special factor can have 0 to many different verification types.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.CL_VERIFICATION_METHOD_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.SPECIAL_FACTOR_ID IS 'FK to ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_ID';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.CL_VERIFICATION_METHOD_CODE IS 'FK to ADRREF.STD_CLVERIFICATIONTYPE.ID';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CL_VERIFICATION_METHOD_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n';


CREATE UNIQUE INDEX ADR.CL_VERIFICATION_METHOD_H_PK ON ADR.CL_VERIFICATION_METHOD_H
(CL_VERIFICATION_METHOD_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CL_VERIFICATION_METHOD_H FOR ADR.CL_VERIFICATION_METHOD_H;


ALTER TABLE ADR.CL_VERIFICATION_METHOD_H ADD (
  CONSTRAINT CL_VERIFICATION_METHOD_H_PK
  PRIMARY KEY
  (CL_VERIFICATION_METHOD_H_ID)
  USING INDEX ADR.CL_VERIFICATION_METHOD_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CL_VERIFICATION_METHOD_H TO ADR_R;

GRANT SELECT ON ADR.CL_VERIFICATION_METHOD_H TO ADR_RW;

GRANT SELECT ON ADR.CL_VERIFICATION_METHOD_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CL_VERIFICATION_METHOD_H TO ES_RW;
ALTER TABLE ADR.COMBAT_EPISODE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COMBAT_EPISODE CASCADE CONSTRAINTS;

CREATE TABLE ADR.COMBAT_EPISODE
(
  COMBAT_EPISODE_ID      NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  OEFOIFSOURCE_ID        NUMBER(20),
  COMBATPAYTYPE_ID       NUMBER(20),
  COMBATLOCATIONTYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID     NUMBER(20),
  COMBAT_FROM_DATE_TXT   VARCHAR2(14 BYTE),
  COMBAT_TO_DATE_TXT     VARCHAR2(14 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COMBAT_EPISODE IS 'This holds the OEF / OIF functionality (enhancement to the Military Service determination, in the case of Operation Iraqi Freedom) This is like an upgraded Military Service / Military Service Episode / Conflict Detail structure';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.COMBAT_EPISODE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.OEFOIFSOURCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.COMBATPAYTYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.COMBATLOCATIONTYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CMBTEPSD_PK ON ADR.COMBAT_EPISODE
(COMBAT_EPISODE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSD_PRSN_FK_I ON ADR.COMBAT_EPISODE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSD_STDCBTPAY_FK_I ON ADR.COMBAT_EPISODE
(COMBATPAYTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSD_STDCMBTLOC_FK_I ON ADR.COMBAT_EPISODE
(COMBATLOCATIONTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSD_STDINST_FK_I ON ADR.COMBAT_EPISODE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSD_STDOEFSRC_FK_I ON ADR.COMBAT_EPISODE
(OEFOIFSOURCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.COMBAT_EPISODE_audir_trgr
after Update or Delete or Insert
ON ADR.COMBAT_EPISODE
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into COMBAT_EPISODE_H
		(
  		COMBAT_EPISODE_H_ID,
 		COMBAT_EPISODE_ID,
 		PERSON_ID,
 		OEFOIFSOURCE_ID,
 		COMBATPAYTYPE_ID,
 		COMBATLOCATIONTYPE_ID,
 		STD_INSTITUTION_ID,
 		COMBAT_FROM_DATE_TXT,
 		COMBAT_TO_DATE_TXT,
 		RECORD_CREATED_BY,
 		RECORD_CREATED_DATE,
 		RECORD_MODIFIED_BY,
 		RECORD_MODIFIED_DATE,
 		RECORD_MODIFIED_COUNT,
 		TRANSACTION_TYPE_CODE
		)
		values
		(
		COMBAT_EPISODE_H_S.nextval,
 		:OLD.COMBAT_EPISODE_ID,
 		:OLD.PERSON_ID,
 		:OLD.OEFOIFSOURCE_ID,
 		:OLD.COMBATPAYTYPE_ID,
 		:OLD.COMBATLOCATIONTYPE_ID,
 		:OLD.STD_INSTITUTION_ID,
 		:OLD.COMBAT_FROM_DATE_TXT,
 		:OLD.COMBAT_TO_DATE_TXT,
 		:OLD.RECORD_CREATED_BY,
 		:OLD.RECORD_CREATED_DATE,
 		:OLD.RECORD_MODIFIED_BY,
 		:OLD.RECORD_MODIFIED_DATE,
 		:OLD.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into COMBAT_EPISODE_H
		(
  		COMBAT_EPISODE_H_ID,
 		COMBAT_EPISODE_ID,
 		PERSON_ID,
 		OEFOIFSOURCE_ID,
 		COMBATPAYTYPE_ID,
 		COMBATLOCATIONTYPE_ID,
 		STD_INSTITUTION_ID,
 		COMBAT_FROM_DATE_TXT,
 		COMBAT_TO_DATE_TXT,
 		RECORD_CREATED_BY,
 		RECORD_CREATED_DATE,
 		RECORD_MODIFIED_BY,
 		RECORD_MODIFIED_DATE,
 		RECORD_MODIFIED_COUNT,
 		TRANSACTION_TYPE_CODE
		)
		values
		(
		COMBAT_EPISODE_H_S.nextval,
 		:NEW.COMBAT_EPISODE_ID,
 		:NEW.PERSON_ID,
 		:NEW.OEFOIFSOURCE_ID,
 		:NEW.COMBATPAYTYPE_ID,
 		:NEW.COMBATLOCATIONTYPE_ID,
		:NEW.STD_INSTITUTION_ID,
 		:NEW.COMBAT_FROM_DATE_TXT,
 		:NEW.COMBAT_TO_DATE_TXT,
 		:NEW.RECORD_CREATED_BY,
 		:NEW.RECORD_CREATED_DATE,
 		:NEW.RECORD_MODIFIED_BY,
 		:NEW.RECORD_MODIFIED_DATE,
 		:NEW.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end COMBAT_EPISODE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM COMBAT_EPISODE FOR ADR.COMBAT_EPISODE;


ALTER TABLE ADR.COMBAT_EPISODE ADD (
  CONSTRAINT CMBTEPSD_PK
  PRIMARY KEY
  (COMBAT_EPISODE_ID)
  USING INDEX ADR.CMBTEPSD_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COMBAT_EPISODE ADD (
  CONSTRAINT CMBTEPSD_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COMBAT_EPISODE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COMBAT_EPISODE TO ADR_RW;

GRANT SELECT ON ADR.COMBAT_EPISODE TO ES_R;

GRANT INSERT, SELECT ON ADR.COMBAT_EPISODE TO ES_RW;
ALTER TABLE ADR.COMBAT_EPISODE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COMBAT_EPISODE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.COMBAT_EPISODE_H
(
  COMBAT_EPISODE_H_ID    NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  COMBAT_EPISODE_ID      NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  OEFOIFSOURCE_ID        NUMBER(20),
  COMBATPAYTYPE_ID       NUMBER(20),
  COMBATLOCATIONTYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID     NUMBER(20),
  COMBAT_FROM_DATE_TXT   VARCHAR2(14 BYTE),
  COMBAT_TO_DATE_TXT     VARCHAR2(14 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COMBAT_EPISODE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.COMBAT_EPISODE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.COMBAT_EPISODE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.OEFOIFSOURCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.COMBATPAYTYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.COMBATLOCATIONTYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COMBAT_EPISODE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CMBTEPSDH_PK ON ADR.COMBAT_EPISODE_H
(COMBAT_EPISODE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTEPSDH_PRSNID_NU_I ON ADR.COMBAT_EPISODE_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COMBAT_EPISODE_H FOR ADR.COMBAT_EPISODE_H;


ALTER TABLE ADR.COMBAT_EPISODE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_002
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CMBTEPSDH_PK
  PRIMARY KEY
  (COMBAT_EPISODE_H_ID)
  USING INDEX ADR.CMBTEPSDH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COMBAT_EPISODE_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COMBAT_EPISODE_H TO ADR_RW;

GRANT SELECT ON ADR.COMBAT_EPISODE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.COMBAT_EPISODE_H TO ES_RW;
ALTER TABLE ADR.COMBAT_PAY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COMBAT_PAY CASCADE CONSTRAINTS;

CREATE TABLE ADR.COMBAT_PAY
(
  COMBAT_PAY_ID              NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  COMBATSERVICESOURCE_ID     NUMBER(20),
  COMBATSERVICELOCATION_ID   NUMBER(20),
  COMBAT_PAY_START_DATE_TXT  VARCHAR2(14 BYTE),
  COMBAT_PAY_END_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  STD_COMBATPAYTYPE_ID       NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COMBAT_PAY IS 'This holds the OEF / OIF functionality (enhancement to the Military Service determination, in the case of Operation Iraqi Freedom) This is like an upgraded Military Service / Military Service Episode / Conflict Detail structure';

COMMENT ON COLUMN ADR.COMBAT_PAY.COMBAT_PAY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COMBAT_PAY.COMBATSERVICESOURCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY.COMBATSERVICELOCATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY.COMBAT_PAY_START_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COMBAT_PAY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COMBAT_PAY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COMBAT_PAY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COMBAT_PAY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.COMBAT_PAY.STD_COMBATPAYTYPE_ID IS 'Identifier for existing	SDS Combat Pay Type';


CREATE UNIQUE INDEX ADR.CMBTPAY_PK ON ADR.COMBAT_PAY
(COMBAT_PAY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTPAY_PRSN_FK_I ON ADR.COMBAT_PAY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTPAY_STDCBTPTYP_FK_I ON ADR.COMBAT_PAY
(STD_COMBATPAYTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTPAY_STDCBTSLOC_FK_I ON ADR.COMBAT_PAY
(COMBATSERVICELOCATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTPAY_STDCBTSSRC_FK_I ON ADR.COMBAT_PAY
(COMBATSERVICESOURCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.COMBAT_PAY_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.COMBAT_PAY
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into COMBAT_PAY_H
		(
		 COMBAT_PAY_H_ID
 		,COMBAT_PAY_ID
 		,PERSON_ID
 		,COMBATSERVICESOURCE_ID
 		,COMBATSERVICELOCATION_ID
        ,STD_COMBATPAYTYPE_ID
 		,COMBAT_PAY_START_DATE_TXT
 		,COMBAT_PAY_END_DATE_TXT
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		COMBAT_PAY_H_S.nextval,
		:old.COMBAT_PAY_ID,
		:old.PERSON_ID ,
 		:old.COMBATSERVICESOURCE_ID,
 		:old.COMBATSERVICELOCATION_ID,
        :old.STD_COMBATPAYTYPE_ID,
 		:old.COMBAT_PAY_START_DATE_TXT,
 		:old.COMBAT_PAY_END_DATE_TXT,
 		:old.RECORD_CREATED_BY,
 		:old.RECORD_CREATED_DATE,
 		:old.RECORD_MODIFIED_BY,
 		:old.RECORD_MODIFIED_DATE,
 		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;
		insert into COMBAT_PAY_H
		(
		 COMBAT_PAY_H_ID
 		,COMBAT_PAY_ID
 		,PERSON_ID
 		,COMBATSERVICESOURCE_ID
 		,COMBATSERVICELOCATION_ID
        ,STD_COMBATPAYTYPE_ID
 		,COMBAT_PAY_START_DATE_TXT
 		,COMBAT_PAY_END_DATE_TXT
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		COMBAT_PAY_H_S.nextval,
		:new.COMBAT_PAY_ID,
		:new.PERSON_ID ,
 		:new.COMBATSERVICESOURCE_ID,
 		:new.COMBATSERVICELOCATION_ID,
        :new.STD_COMBATPAYTYPE_ID,
 		:new.COMBAT_PAY_START_DATE_TXT,
 		:new.COMBAT_PAY_END_DATE_TXT,
 		:new.RECORD_CREATED_BY,
 		:new.RECORD_CREATED_DATE,
 		:new.RECORD_MODIFIED_BY,
 		:new.RECORD_MODIFIED_DATE,
 		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end COMBAT_PAY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM COMBAT_PAY FOR ADR.COMBAT_PAY;


ALTER TABLE ADR.COMBAT_PAY ADD (
  CONSTRAINT CMBTPAY_PK
  PRIMARY KEY
  (COMBAT_PAY_ID)
  USING INDEX ADR.CMBTPAY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COMBAT_PAY ADD (
  CONSTRAINT CMBTPAY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COMBAT_PAY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COMBAT_PAY TO ADR_RW;

GRANT SELECT ON ADR.COMBAT_PAY TO ES_R;

GRANT INSERT, SELECT ON ADR.COMBAT_PAY TO ES_RW;
ALTER TABLE ADR.COMBAT_PAY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COMBAT_PAY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.COMBAT_PAY_H
(
  COMBAT_PAY_H_ID            NUMBER(20)         NOT NULL,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)   NOT NULL,
  COMBAT_PAY_ID              NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  COMBATSERVICESOURCE_ID     NUMBER(20),
  COMBATSERVICELOCATION_ID   NUMBER(20),
  COMBAT_PAY_START_DATE_TXT  VARCHAR2(14 BYTE),
  COMBAT_PAY_END_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  STD_COMBATPAYTYPE_ID       NUMBER(20),
  HISTORY_CREATED_DATE       DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COMBAT_PAY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.COMBAT_PAY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.COMBAT_PAY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.COMBATSERVICESOURCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.COMBATSERVICELOCATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.COMBAT_PAY_START_DATE_TXT IS 'Date when the Beneficiary started receiving Combat Pay';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.STD_COMBATPAYTYPE_ID IS 'Identifier for existing  SDS Combat Pay Type';

COMMENT ON COLUMN ADR.COMBAT_PAY_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.CMBTPAYH_PK ON ADR.COMBAT_PAY_H
(COMBAT_PAY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMBTPAYH_PRSNID_NU_I ON ADR.COMBAT_PAY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COMBAT_PAY_H FOR ADR.COMBAT_PAY_H;


ALTER TABLE ADR.COMBAT_PAY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_023
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CMBTPAYH_PK
  PRIMARY KEY
  (COMBAT_PAY_H_ID)
  USING INDEX ADR.CMBTPAYH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COMBAT_PAY_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COMBAT_PAY_H TO ADR_RW;

GRANT SELECT ON ADR.COMBAT_PAY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.COMBAT_PAY_H TO ES_RW;
ALTER TABLE ADR.COM_AAC_ERROR_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_AAC_ERROR_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_AAC_ERROR_DETAIL
(
  COM_AAC_ERROR_DETAIL_ID  NUMBER(20)           NOT NULL,
  COM_MAILING_ID           NUMBER(20)           NOT NULL,
  COM_AAC_ERROR_TYPE_ID    NUMBER(20)           NOT NULL,
  REJECT_REASON_TEXT       VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_AAC_ERROR_DETAIL IS 'Contains detail reasons as to why the letter was rejected at the HEC The COM_AAC_ERROR_DETAIL table contains error reasons from the AAC error file for a letter. COM_MAILING_ID Foreign key to the COM_MAILING table. COM_AAC_ERROR_TYPE_ID Foreign key to the COM_AAC_ERROR_TYPE table.';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.COM_AAC_ERROR_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.REJECT_REASON_TEXT IS 'Text field used to document the reason for the AAC to reject the mailing record';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_AAC_ERROR_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMAACERR_COMMAIL_FK_I ON ADR.COM_AAC_ERROR_DETAIL
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMAACERR_PK ON ADR.COM_AAC_ERROR_DETAIL
(COM_AAC_ERROR_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMAACERR_STD_COMERRTYP_FK_I ON ADR.COM_AAC_ERROR_DETAIL
(COM_AAC_ERROR_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_AAC_ERROR_DETAIL FOR ADR.COM_AAC_ERROR_DETAIL;


ALTER TABLE ADR.COM_AAC_ERROR_DETAIL ADD (
  CONSTRAINT COMAACERR_PK
  PRIMARY KEY
  (COM_AAC_ERROR_DETAIL_ID)
  USING INDEX ADR.COMAACERR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_AAC_ERROR_DETAIL ADD (
  CONSTRAINT COMAACERR_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT COMAACERR_STD_COMERRTYP_FK 
  FOREIGN KEY (COM_AAC_ERROR_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMERRORTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_AAC_ERROR_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_AAC_ERROR_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.COM_AAC_ERROR_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_AAC_ERROR_DETAIL TO ES_RW;
ALTER TABLE ADR.COM_CANCELED_REQUEST
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_CANCELED_REQUEST CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_CANCELED_REQUEST
(
  COM_CANCELED_REQUEST_ID  NUMBER(20)           NOT NULL,
  COM_MAILING_ID           NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_CANCELED_REQUEST IS 'The table stores data about cancelled export letter entries.';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.COM_CANCELED_REQUEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_CANCELED_REQUEST.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMCNLPR_COMMAIL_FK_I ON ADR.COM_CANCELED_REQUEST
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMCNLPR_PK ON ADR.COM_CANCELED_REQUEST
(COM_CANCELED_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_CANCELED_REQUEST FOR ADR.COM_CANCELED_REQUEST;


ALTER TABLE ADR.COM_CANCELED_REQUEST ADD (
  CONSTRAINT COMCNLPR_PK
  PRIMARY KEY
  (COM_CANCELED_REQUEST_ID)
  USING INDEX ADR.COMCNLPR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_CANCELED_REQUEST ADD (
  CONSTRAINT COMCNLPR_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COM_CANCELED_REQUEST TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_CANCELED_REQUEST TO ADR_RW;

GRANT SELECT ON ADR.COM_CANCELED_REQUEST TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_CANCELED_REQUEST TO ES_RW;
ALTER TABLE ADR.COM_EMAIL_TEMPLATE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_EMAIL_TEMPLATE CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_EMAIL_TEMPLATE
(
  COM_EMAIL_TEMPLATE_ID       NUMBER(20)        NOT NULL,
  EMAIL_TEMPLATE_NAME         VARCHAR2(100 BYTE),
  EMAIL_TEMPLATE_DESCRIPTION  VARCHAR2(250 BYTE),
  EMAIL_TEMPLATE_TEXT         VARCHAR2(4000 BYTE),
  EMAIL_RECIPIENT_LIST        VARCHAR2(4000 BYTE),
  EMAIL_SUBJECT_TITLE         VARCHAR2(250 BYTE),
  SITE_EMAIL_PREFIX           VARCHAR2(40 BYTE),
  SITE_EMAIL_TYPE_CD          VARCHAR2(1 BYTE),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_EMAIL_TEMPLATE IS 'The table contains data contained in the template used for email bulletins.';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.COM_EMAIL_TEMPLATE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_EMAIL_TEMPLATE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.COMEMLTPL_PK ON ADR.COM_EMAIL_TEMPLATE
(COM_EMAIL_TEMPLATE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_EMAIL_TEMPLATE FOR ADR.COM_EMAIL_TEMPLATE;


ALTER TABLE ADR.COM_EMAIL_TEMPLATE ADD (
  CONSTRAINT COMEMLTPL_PK
  PRIMARY KEY
  (COM_EMAIL_TEMPLATE_ID)
  USING INDEX ADR.COMEMLTPL_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_EMAIL_TEMPLATE TO ADRTMP;

GRANT SELECT ON ADR.COM_EMAIL_TEMPLATE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_EMAIL_TEMPLATE TO ADR_RW;

GRANT SELECT ON ADR.COM_EMAIL_TEMPLATE TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_EMAIL_TEMPLATE TO ES_RW;
ALTER TABLE ADR.COM_FORM_STATISTICS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_FORM_STATISTICS CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_FORM_STATISTICS
(
  COM_FORM_STATISTICS_ID          NUMBER(20)    NOT NULL,
  COM_TRANSMISSION_LOG_ID         NUMBER(20)    NOT NULL,
  FORM_NUMBER                     VARCHAR2(100 BYTE),
  FORM_NUMBER_ERROR_REASON_TEXT   VARCHAR2(4000 BYTE),
  FORM_NUMBER_REJECT_REASON_TEXT  VARCHAR2(4000 BYTE),
  FORM_NUMBER_RECORD_COUNT        NUMBER(10),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_FORM_STATISTICS IS 'Contains the statistics for letter mailings';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.COM_FORM_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.COM_TRANSMISSION_LOG_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_FORM_STATISTICS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMFRMSTAT_COMTRANLOG_FK_I ON ADR.COM_FORM_STATISTICS
(COM_TRANSMISSION_LOG_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMFRMSTAT_PK ON ADR.COM_FORM_STATISTICS
(COM_FORM_STATISTICS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_FORM_STATISTICS FOR ADR.COM_FORM_STATISTICS;


ALTER TABLE ADR.COM_FORM_STATISTICS ADD (
  CONSTRAINT COMFRMSTAT_PK
  PRIMARY KEY
  (COM_FORM_STATISTICS_ID)
  USING INDEX ADR.COMFRMSTAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_FORM_STATISTICS ADD (
  CONSTRAINT COMFRMSTAT_COMTRANLOG_FK 
  FOREIGN KEY (COM_TRANSMISSION_LOG_ID) 
  REFERENCES ADR.COM_TRANSMISSION_LOG (COM_TRANSMISSION_LOG_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_FORM_STATISTICS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_FORM_STATISTICS TO ADR_RW;

GRANT SELECT ON ADR.COM_FORM_STATISTICS TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_FORM_STATISTICS TO ES_RW;
ALTER TABLE ADR.COM_HEC_REJECT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_HEC_REJECT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_HEC_REJECT_DETAIL
(
  COM_HEC_REJECT_DETAIL_ID  NUMBER(20)          NOT NULL,
  COM_MAILING_ID            NUMBER(20)          NOT NULL,
  REJECT_REASON_TEXT        VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_HEC_REJECT_DETAIL IS 'Contains detail reasons as to why the letter was rejected at the HEC COM_MAILING_ID Foreign key to the COM_MAILING table.';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.COM_HEC_REJECT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_HEC_REJECT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMRJCTDTL_COMMAIL_FK_I ON ADR.COM_HEC_REJECT_DETAIL
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMRJCTDTL_PK ON ADR.COM_HEC_REJECT_DETAIL
(COM_HEC_REJECT_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_HEC_REJECT_DETAIL FOR ADR.COM_HEC_REJECT_DETAIL;


ALTER TABLE ADR.COM_HEC_REJECT_DETAIL ADD (
  CONSTRAINT COMRJCTDTL_PK
  PRIMARY KEY
  (COM_HEC_REJECT_DETAIL_ID)
  USING INDEX ADR.COMRJCTDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_HEC_REJECT_DETAIL ADD (
  CONSTRAINT COMRJCTDTL_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COM_HEC_REJECT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_HEC_REJECT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.COM_HEC_REJECT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_HEC_REJECT_DETAIL TO ES_RW;
ALTER TABLE ADR.COM_LETTER_TEMPLATE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_LETTER_TEMPLATE CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_LETTER_TEMPLATE
(
  COM_LETTER_TEMPLATE_ID        NUMBER(20)      NOT NULL,
  COM_LETTER_TYPE_ID            NUMBER(20)      NOT NULL,
  COM_LETTER_GENERATED_TYPE_ID  NUMBER(20),
  LETTER_TEMPLATE_NAME          VARCHAR2(100 BYTE),
  LETTER_TEMPLATE_DESCRIPTION   VARCHAR2(250 BYTE),
  LETTER_TEMPLATE_TEXT          VARCHAR2(4000 BYTE),
  LETTER_TEMPLATE_FORM_NUMBER   VARCHAR2(100 BYTE),
  LETTER_TEMPLATE_START_DATE    DATE,
  LETTER_TEMPLATE_END_DATE      DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_LETTER_TEMPLATE IS 'The table stores data for templates used to generate a particular type of letter. COM_LETTER_TYPE_ID Foreign key to the COM_LETTER_TYPE table. Indicates the general type of letter (for example, IVM, ENR). COM_LETTER_GENERATED_TYPE_ID Foreign key to the COM_LETTER_GENERATED_TYPE table. Indicates how a letter is generated (for example, manually or automatically).';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.COM_LETTER_TEMPLATE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.COM_LETTER_GENERATED_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_LETTER_TEMPLATE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.COMLTRTPL_PK ON ADR.COM_LETTER_TEMPLATE
(COM_LETTER_TEMPLATE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMLTRTPL_STD_COMLTRGTYPE_FK_I ON ADR.COM_LETTER_TEMPLATE
(COM_LETTER_GENERATED_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMLTRTPL_STD_COMLTRTYPE_FK_I ON ADR.COM_LETTER_TEMPLATE
(COM_LETTER_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_LETTER_TEMPLATE FOR ADR.COM_LETTER_TEMPLATE;


ALTER TABLE ADR.COM_LETTER_TEMPLATE ADD (
  CONSTRAINT COMLTRTPL_PK
  PRIMARY KEY
  (COM_LETTER_TEMPLATE_ID)
  USING INDEX ADR.COMLTRTPL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_LETTER_TEMPLATE ADD (
  CONSTRAINT COMLTRTPL_STD_COMLTRGTYPE_FK 
  FOREIGN KEY (COM_LETTER_GENERATED_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMLETTERGENTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMLTRTPL_STD_COMLTRTYPE_FK 
  FOREIGN KEY (COM_LETTER_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMLETTERTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_LETTER_TEMPLATE TO ADREXT;

GRANT SELECT ON ADR.COM_LETTER_TEMPLATE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_LETTER_TEMPLATE TO ADR_RW;

GRANT SELECT ON ADR.COM_LETTER_TEMPLATE TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_LETTER_TEMPLATE TO ES_RW;
ALTER TABLE ADR.COM_MAILING
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_MAILING CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_MAILING
(
  COM_MAILING_ID                 NUMBER(20),
  WKF_CASE_ID                    NUMBER(20),
  PERSON_ID                      NUMBER(20),
  ADDRESS_TYPE_ID                NUMBER(20),
  COM_LETTER_TYPE_ID             NUMBER(20),
  COM_LETTER_TEMPLATE_ID         NUMBER(20),
  COM_AAC_REJECT_REASON_TYPE_ID  NUMBER(20),
  COM_MAILING_TRIGGER_TYPE_ID    NUMBER(20),
  AAC_EXTRACT_NUMBER             VARCHAR2(30 BYTE),
  OVERRIDE_IND                   VARCHAR2(1 BYTE),
  SSN                            VARCHAR2(20 BYTE),
  PREFIX                         VARCHAR2(12 BYTE),
  FIRST_NAME                     VARCHAR2(30 BYTE),
  MIDDLE_NAME                    VARCHAR2(30 BYTE),
  LAST_NAME                      VARCHAR2(40 BYTE),
  SUFFIX                         VARCHAR2(12 BYTE),
  TITLE                          VARCHAR2(50 BYTE),
  ADDRESS_LINE1                  VARCHAR2(100 BYTE),
  ADDRESS_LINE2                  VARCHAR2(100 BYTE),
  ADDRESS_LINE3                  VARCHAR2(100 BYTE),
  CITY                           VARCHAR2(60 BYTE),
  STATE_CODE                     VARCHAR2(50 BYTE),
  COUNTY_CODE                    VARCHAR2(50 BYTE),
  PROVINCE_CODE                  VARCHAR2(50 BYTE),
  ZIP_CODE                       VARCHAR2(20 BYTE),
  ZIP_PLUS_4                     VARCHAR2(10 BYTE),
  POSTAL_CODE                    VARCHAR2(20 BYTE),
  COUNTRY_CODE                   VARCHAR2(50 BYTE),
  MAILING_SENT_DATE              DATE,
  RECIPIENT_TYPE_TEXT            VARCHAR2(100 BYTE),
  REMAIL_STATUS_CODE             VARCHAR2(30 BYTE),
  REMARKS                        VARCHAR2(4000 BYTE),
  COM_MAILING_XML                VARCHAR2(4000 BYTE),
  BARCODE                        VARCHAR2(28 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  IVM_CASE_NUMBER                VARCHAR2(14 BYTE),
  CASE_CLOSURE_DATE              DATE,
  SENT_TO_IVM                    VARCHAR2(1 BYTE),
  IVM_COMMS_LOG_NUMBER           NUMBER(21),
  IPN_DATE                       DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_MAILING IS 'The COM_MAILING table acts a log to keep track of every mailing sent out from the ESR Communications subsystem. A mailing is defined as everything going into an envelope. WKF_CASE_ID Foreign key to the WKF_CASE table. PERSON_ID Foreign key to the PERSON table. Indicates the person with whom the letter is associated. ADDRESS_ID Foreign key to the ADDRESS table. Indicates the address used for the letter. COM_LETTER_TYPE_ID Foreign key to the COM_LETTER_TYPE table. Indicates the general type of letter (for example, IVM, ENR). COM_LETTER_TEMPLATE_ID Foreign key to the COM_LETTER_TEMPLATE table. COM_AAC_REJECT_REASON_TYPE_ID Foreign key to the COM_AAC_REJECT_REASON_TYPE table. Indicates the reasons a letter was rejected by AAC (for example, ZIP code not found in database, street address not found in ZIP code).';

COMMENT ON COLUMN ADR.COM_MAILING.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_MAILING.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.COM_MAILING.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_MAILING.COM_LETTER_TEMPLATE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_MAILING.AAC_EXTRACT_NUMBER IS 'Batch identifier used by AAC. Used in ADR to associate the the mailing entry with a particular AAC extract batch job.';

COMMENT ON COLUMN ADR.COM_MAILING.OVERRIDE_IND IS 'BR7458 Enrollment Override Flag is defined as a flag that when set will initiate the re- calculation of a veterans enrollment priority.  It is used in those cases where a veteran may have been placed in a Rejected; Stop New Enrollments OR Rejected; Below Enrollment Group Threshold Enrollment statuses. ';

COMMENT ON COLUMN ADR.COM_MAILING.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.COM_MAILING.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.COM_MAILING.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.COM_MAILING.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.COM_MAILING.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.COM_MAILING.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.COM_MAILING.TITLE IS 'Name Title';

COMMENT ON COLUMN ADR.COM_MAILING.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.COM_MAILING.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.COM_MAILING.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.COM_MAILING.CITY IS 'BR9 City is defined as the name of a city used for an address.';

COMMENT ON COLUMN ADR.COM_MAILING.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5- character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied. State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.COM_MAILING.COUNTY_CODE IS 'BR5293 FIPS County CODE is defined as the last 3 characters of the FIPS state/county code.  The FIPS county code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied BR7383 5-DIGIT FIPS CODE is defined as the 2 digit state code + 3 digit county code. This 5 digit FIPS code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied';

COMMENT ON COLUMN ADR.COM_MAILING.PROVINCE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.COM_MAILING.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.COM_MAILING.ZIP_PLUS_4 IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.COM_MAILING.POSTAL_CODE IS 'BR17 Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.COM_MAILING.COUNTRY_CODE IS 'BR15 Country is defined as the unique identifier assigned by the International Organization of Standardization for a country.';

COMMENT ON COLUMN ADR.COM_MAILING.REMAIL_STATUS_CODE IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.COM_MAILING.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.COM_MAILING.COM_MAILING_XML IS 'Contains a copy of the letter in XML format.';

COMMENT ON COLUMN ADR.COM_MAILING.BARCODE IS 'BARCODE - Stores a unique barcode generated for each mailing';

COMMENT ON COLUMN ADR.COM_MAILING.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_MAILING.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_MAILING.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_MAILING.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_MAILING.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.COM_MAILING.IVM_COMMS_LOG_NUMBER IS 'External ID number from IVM';

COMMENT ON COLUMN ADR.COM_MAILING.IPN_DATE IS 'Initial Pending Notification Date.';


CREATE INDEX ADR.COMMAIL_BARCODE_NU_I ON ADR.COM_MAILING
(BARCODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_COMLETTYPE_FK_I ON ADR.COM_MAILING
(COM_LETTER_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_COMLTRTPL_FK_I ON ADR.COM_MAILING
(COM_LETTER_TEMPLATE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_FNAME_FB_I ON ADR.COM_MAILING
(UPPER("FIRST_NAME"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE INDEX ADR.COMMAIL_LNAME_FB_I ON ADR.COM_MAILING
(UPPER("LAST_NAME"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE INDEX ADR.COMMAIL_LSTNM_CITY_I ON ADR.COM_MAILING
(UPPER("LAST_NAME"), UPPER("CITY"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE INDEX ADR.COMMAIL_LSTNM_ZIP_I ON ADR.COM_MAILING
(UPPER("LAST_NAME"), UPPER("ZIP_CODE"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE INDEX ADR.COMMAIL_MNAME_FB_I ON ADR.COM_MAILING
(UPPER("MIDDLE_NAME"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE UNIQUE INDEX ADR.COMMAIL_PK ON ADR.COM_MAILING
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_PRSN_FK_I ON ADR.COM_MAILING
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_SSN_NU_I ON ADR.COM_MAILING
(SSN)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_STD_ADDRTYPE_FK_I ON ADR.COM_MAILING
(ADDRESS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_STD_COMMLTRG_FK_I ON ADR.COM_MAILING
(COM_MAILING_TRIGGER_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_STD_COMRJCTRSN_FK_I ON ADR.COM_MAILING
(COM_AAC_REJECT_REASON_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMAIL_WKFCASE_FK_I ON ADR.COM_MAILING
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_MAILING FOR ADR.COM_MAILING;


ALTER TABLE ADR.COM_MAILING ADD (
  CONSTRAINT AVCON_1172521913_REMAI_000
  CHECK (REMAIL_STATUS_CODE IN ('REMAILED', 'RESEND'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067231
  CHECK ("COM_MAILING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067232
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067233
  CHECK ("COM_LETTER_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067234
  CHECK ("COM_LETTER_TEMPLATE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067235
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067236
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067237
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067238
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067239
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT COMMAIL_PK
  PRIMARY KEY
  (COM_MAILING_ID)
  USING INDEX ADR.COMMAIL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_MAILING ADD (
  CONSTRAINT COMMAIL_COMLETTYPE_FK 
  FOREIGN KEY (COM_LETTER_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMLETTERTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMMAIL_COMLTRTPL_FK 
  FOREIGN KEY (COM_LETTER_TEMPLATE_ID) 
  REFERENCES ADR.COM_LETTER_TEMPLATE (COM_LETTER_TEMPLATE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMMAIL_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT COMMAIL_STD_COMMLTRG_FK 
  FOREIGN KEY (COM_MAILING_TRIGGER_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMMLTRIGGERTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMMAIL_STD_COMRJCTRSN_FK 
  FOREIGN KEY (COM_AAC_REJECT_REASON_TYPE_ID) 
  REFERENCES ADRREF.STD_COMREJECTREASONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMMAIL_WKFCASE_FK 
  FOREIGN KEY (WKF_CASE_ID) 
  REFERENCES ADR.WKF_CASE (WKF_CASE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_MAILING TO ADREXT;

GRANT SELECT ON ADR.COM_MAILING TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_MAILING TO ADR_RW;

GRANT SELECT ON ADR.COM_MAILING TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_MAILING TO ES_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_MAILING TO VETCHOICE_RW;
ALTER TABLE ADR.COM_MAILING_STATUS_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_MAILING_STATUS_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_MAILING_STATUS_DETAIL
(
  COM_MAILING_STATUS_DETAIL_ID  NUMBER(20),
  COM_MAILING_ID                NUMBER(20),
  COM_MAILING_STATUS_TYPE_ID    NUMBER(20),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_MAILING_STATUS_DETAIL IS 'The COM_MAILING_STATUS_DETAIL table stores status information for mailings sent out from the ESR subsystem. COM_MAILING_ID Foreign key to the COM_MAILING table. COM_MAILING_STATUS_TYPE_ID Foreign key to the COM_MAILING_STATUS_TYPE table.';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.COM_MAILING_STATUS_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_MAILING_STATUS_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMMLSTAT_COMMAIL_FK_I ON ADR.COM_MAILING_STATUS_DETAIL
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMMLSTAT_PK ON ADR.COM_MAILING_STATUS_DETAIL
(COM_MAILING_STATUS_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMMLSTAT_STD_COMAILSTAT_FK_I ON ADR.COM_MAILING_STATUS_DETAIL
(COM_MAILING_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_MAILING_STATUS_DETAIL FOR ADR.COM_MAILING_STATUS_DETAIL;


ALTER TABLE ADR.COM_MAILING_STATUS_DETAIL ADD (
  CONSTRAINT SYS_C0066319
  CHECK ("COM_MAILING_STATUS_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066320
  CHECK ("COM_MAILING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066321
  CHECK ("COM_MAILING_STATUS_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066322
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066323
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066324
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066325
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066326
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT COMMLSTAT_PK
  PRIMARY KEY
  (COM_MAILING_STATUS_DETAIL_ID)
  USING INDEX ADR.COMMLSTAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_MAILING_STATUS_DETAIL ADD (
  CONSTRAINT COMMLSTAT_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT COMMLSTAT_STD_COMAILSTAT_FK 
  FOREIGN KEY (COM_MAILING_STATUS_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMMAILINGSTTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_MAILING_STATUS_DETAIL TO ADREXT;

GRANT SELECT ON ADR.COM_MAILING_STATUS_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_MAILING_STATUS_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.COM_MAILING_STATUS_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_MAILING_STATUS_DETAIL TO ES_RW;
ALTER TABLE ADR.COM_PRINT_CHECK
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_PRINT_CHECK CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_PRINT_CHECK
(
  COM_PRINT_CHECK_ID       NUMBER(20)           NOT NULL,
  COM_MAILING_ID           NUMBER(20),
  COM_PRINT_REQUEST_ID     NUMBER(20),
  COM_CANCELED_REQUEST_ID  NUMBER(20),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_PRINT_CHECK IS 'The table contains snapshots of what portion of the batch of letters that has been processed. Used to determine where to pick up since the last execution of the batch job. COM_MAILING_ID Foreign key to the COM_MAILING table. COM_PRINT_REQUEST_ID Foreign key to the COM_PRINT_REQUEST table. COM_CANCELED_REQUEST_ID Foreign key to the COM_CANCELED_REQUEST table.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.COM_PRINT_CHECK_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.COM_PRINT_REQUEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.COM_CANCELED_REQUEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_PRINT_CHECK.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMPRTCHK_COMCNLPR_FK_I ON ADR.COM_PRINT_CHECK
(COM_CANCELED_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMPRTCHK_COMMAIL_FK_I ON ADR.COM_PRINT_CHECK
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMPRTCHK_COMPRTREQ_FK_I ON ADR.COM_PRINT_CHECK
(COM_PRINT_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMPRTCHK_PK ON ADR.COM_PRINT_CHECK
(COM_PRINT_CHECK_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_PRINT_CHECK FOR ADR.COM_PRINT_CHECK;


ALTER TABLE ADR.COM_PRINT_CHECK ADD (
  CONSTRAINT COMPRTCHK_PK
  PRIMARY KEY
  (COM_PRINT_CHECK_ID)
  USING INDEX ADR.COMPRTCHK_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.COM_PRINT_CHECK ADD (
  CONSTRAINT COMPRTCHK_COMCNLPR_FK 
  FOREIGN KEY (COM_CANCELED_REQUEST_ID) 
  REFERENCES ADR.COM_CANCELED_REQUEST (COM_CANCELED_REQUEST_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT COMPRTCHK_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT COMPRTCHK_COMPRTREQ_FK 
  FOREIGN KEY (COM_PRINT_REQUEST_ID) 
  REFERENCES ADR.COM_PRINT_REQUEST (COM_PRINT_REQUEST_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COM_PRINT_CHECK TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_PRINT_CHECK TO ADR_RW;

GRANT SELECT ON ADR.COM_PRINT_CHECK TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_PRINT_CHECK TO ES_RW;
ALTER TABLE ADR.COM_PRINT_REQUEST
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_PRINT_REQUEST CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_PRINT_REQUEST
(
  COM_PRINT_REQUEST_ID       NUMBER(20),
  COM_MAILING_ID             NUMBER(20),
  PRINT_REQUEST_UNIQUE_CODE  VARCHAR2(50 BYTE),
  TEMPLATE_FORM_NUMBER       VARCHAR2(100 BYTE),
  PRINT_REQUEST_XML          VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE       DATE,
  RECORD_MODIFIED_COUNT      NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_PRINT_REQUEST IS 'This table contains the batch of letters that will be sent to AAC for printing COM_MAILING_ID Foreign key to the COM_MAILING table.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.COM_PRINT_REQUEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.COM_MAILING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.PRINT_REQUEST_UNIQUE_CODE IS 'This field will be stamped with a combination of the PERSON_ID and the LETTER_TEMPLATE_ID. A unique constraint on this field will ensure that the same person does not get a duplicate letter as part of one batch.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_PRINT_REQUEST.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.COMPRTREQ_COMMAIL_FK_I ON ADR.COM_PRINT_REQUEST
(COM_MAILING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMPRTREQ_PK ON ADR.COM_PRINT_REQUEST
(COM_PRINT_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMPRTREQ_U_PRINT_DUPLICATE_UK ON ADR.COM_PRINT_REQUEST
(PRINT_REQUEST_UNIQUE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_PRINT_REQUEST FOR ADR.COM_PRINT_REQUEST;


ALTER TABLE ADR.COM_PRINT_REQUEST ADD (
  CONSTRAINT SYS_C0067224
  CHECK ("COM_PRINT_REQUEST_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067225
  CHECK ("COM_MAILING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067226
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067227
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067228
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067229
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067230
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT COMPRTREQ_PK
  PRIMARY KEY
  (COM_PRINT_REQUEST_ID)
  USING INDEX ADR.COMPRTREQ_PK
  ENABLE VALIDATE,
  CONSTRAINT COMPRTREQ_U_PRINT_DUPLICATE_UK
  UNIQUE (PRINT_REQUEST_UNIQUE_CODE)
  USING INDEX ADR.COMPRTREQ_U_PRINT_DUPLICATE_UK
  ENABLE NOVALIDATE);

ALTER TABLE ADR.COM_PRINT_REQUEST ADD (
  CONSTRAINT COMPRTREQ_COMMAIL_FK 
  FOREIGN KEY (COM_MAILING_ID) 
  REFERENCES ADR.COM_MAILING (COM_MAILING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.COM_PRINT_REQUEST TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_PRINT_REQUEST TO ADR_RW;

GRANT SELECT ON ADR.COM_PRINT_REQUEST TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_PRINT_REQUEST TO ES_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_PRINT_REQUEST TO VETCHOICE_RW;
ALTER TABLE ADR.COM_TRANSMISSION_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.COM_TRANSMISSION_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.COM_TRANSMISSION_LOG
(
  COM_TRANSMISSION_LOG_ID       NUMBER(20)      NOT NULL,
  MESSAGE_TRANSMISSION_TYPE_ID  NUMBER(20),
  FILE_NAME                     VARCHAR2(100 BYTE),
  FILE_RECORD_COUNT             NUMBER(18),
  FILE_RECEIVED_DATE            DATE,
  TRANSMISSION_START_DATE       DATE,
  TRANSMISSION_END_DATE         DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.COM_TRANSMISSION_LOG IS 'The COM_TRANSMISSION_LOG table records all inbound and outbound letters to and from the ESR communications subsystem. MESSAGE_TRANSMISSION_TYPE_ID Foreign key to the MESSAGE_TRANSMISSION_TYPE table.';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.COM_TRANSMISSION_LOG_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.MESSAGE_TRANSMISSION_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.COM_TRANSMISSION_LOG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.COMTRANLOG_PK ON ADR.COM_TRANSMISSION_LOG
(COM_TRANSMISSION_LOG_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COMTRANS_STD_MSGTYP_FK_I ON ADR.COM_TRANSMISSION_LOG
(MESSAGE_TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM COM_TRANSMISSION_LOG FOR ADR.COM_TRANSMISSION_LOG;


ALTER TABLE ADR.COM_TRANSMISSION_LOG ADD (
  CONSTRAINT COMTRANLOG_PK
  PRIMARY KEY
  (COM_TRANSMISSION_LOG_ID)
  USING INDEX ADR.COMTRANLOG_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.COM_TRANSMISSION_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.COM_TRANSMISSION_LOG TO ADR_RW;

GRANT SELECT ON ADR.COM_TRANSMISSION_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.COM_TRANSMISSION_LOG TO ES_RW;
ALTER TABLE ADR.CONFIDENTIAL_ADDR_CATGRY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CONFIDENTIAL_ADDR_CATGRY CASCADE CONSTRAINTS;

CREATE TABLE ADR.CONFIDENTIAL_ADDR_CATGRY
(
  CONFIDENTIAL_ADDR_CATGRY_ID  NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  CONFIDENTIAL_ADDR_TYPE_ID    NUMBER(20)       NOT NULL,
  INACTIVE_FLAG                NUMBER(1)        DEFAULT 0                     NOT NULL,
  INACTIVE_DATE                DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CONFIDENTIAL_ADDR_CATGRY IS 'The CONFIDENTIAL_ADDR_CATGRY table stores the type of confidential address specified for a person. Addresses can be designated confidential by the associated person, and that person can indicate the address to be confidential for one or more of these categories: eligibility/enrollment, appointment/scheduling, copayments/veteran billing, medical records, and all others. Owner: PSD PERSON_ID Foreign key to the PERSON table. Indicates the particular person to whom the address belongs. CONFIDENTIAL_ADDR_TYPE_ID Foreign key to the CONFIDENTIAL_ADDR_TYPE. Indicates which under what circumstances an address is considered confidential (for example, eligibility/enrollment, appointment/scheduling).';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.CONFIDENTIAL_ADDR_CATGRY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.CNFADDR_PK ON ADR.CONFIDENTIAL_ADDR_CATGRY
(CONFIDENTIAL_ADDR_CATGRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CNFADDR_PRSN_FK_I ON ADR.CONFIDENTIAL_ADDR_CATGRY
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CNFADDR_STDINST2_FK_I ON ADR.CONFIDENTIAL_ADDR_CATGRY
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CNFADDR_STD_CNFADDRCAT_FK_I ON ADR.CONFIDENTIAL_ADDR_CATGRY
(CONFIDENTIAL_ADDR_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CONF_ADDR_CATGRY_audir_trgr
after Update or Delete or Insert
ON ADR.CONFIDENTIAL_ADDR_CATGRY
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into CONFIDENTIAL_ADDR_CATGRY_H
		(
		CONFIDENTIAL_ADDR_CATGRY_H_ID,
		CONFIDENTIAL_ADDR_CATGRY_ID  ,
		PERSON_ID                   ,
		CONFIDENTIAL_ADDR_TYPE_ID    ,
		INACTIVE_FLAG                ,
		INACTIVE_DATE                ,
		RECORD_CREATED_BY            ,
		RECORD_CREATED_DATE          ,
		RECORD_MODIFIED_BY           ,
		RECORD_MODIFIED_DATE         ,
		RECORD_MODIFIED_COUNT        ,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CONFIDENTIAL_ADDR_CATGRY_H_S.nextval,
		:old.CONFIDENTIAL_ADDR_CATGRY_ID  ,
		:old.PERSON_ID                   ,
		:old.CONFIDENTIAL_ADDR_TYPE_ID    ,
		:old.INACTIVE_FLAG                ,
		:old.INACTIVE_DATE                ,
		:old.RECORD_CREATED_BY            ,
		:old.RECORD_CREATED_DATE          ,
		:old.RECORD_MODIFIED_BY           ,
		:old.RECORD_MODIFIED_DATE         ,
		:old.RECORD_MODIFIED_COUNT        ,
		:old.RECORD_MODIFIED_SITE_ID,
		:old.RECORD_MODIFIED_SRC_NAME,
		'D'
		);
	else

		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into CONFIDENTIAL_ADDR_CATGRY_H
		(
		CONFIDENTIAL_ADDR_CATGRY_H_ID,
		CONFIDENTIAL_ADDR_CATGRY_ID  ,
		PERSON_ID                   ,
		CONFIDENTIAL_ADDR_TYPE_ID    ,
		INACTIVE_FLAG                ,
		INACTIVE_DATE                ,
		RECORD_CREATED_BY            ,
		RECORD_CREATED_DATE          ,
		RECORD_MODIFIED_BY           ,
		RECORD_MODIFIED_DATE         ,
		RECORD_MODIFIED_COUNT        ,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		CONFIDENTIAL_ADDR_CATGRY_H_S.nextval,
		:new.CONFIDENTIAL_ADDR_CATGRY_ID  ,
		:new.PERSON_ID                   ,
		:new.CONFIDENTIAL_ADDR_TYPE_ID    ,
		:new.INACTIVE_FLAG                ,
		:new.INACTIVE_DATE                ,
		:new.RECORD_CREATED_BY            ,
		:new.RECORD_CREATED_DATE          ,
		:new.RECORD_MODIFIED_BY           ,
		:new.RECORD_MODIFIED_DATE         ,
		:new.RECORD_MODIFIED_COUNT        ,
		:new.RECORD_MODIFIED_SITE_ID,
		:new.RECORD_MODIFIED_SRC_NAME,
		mDML
		);
	end if;
end CONF_ADDR_CATGRY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CONFIDENTIAL_ADDR_CATGRY FOR ADR.CONFIDENTIAL_ADDR_CATGRY;


ALTER TABLE ADR.CONFIDENTIAL_ADDR_CATGRY ADD (
  CONSTRAINT AVCON_1172521913_INACT_046
  CHECK (INACTIVE_FLAG BETWEEN 0 AND 0 OR INACTIVE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT CNFADDR_PK
  PRIMARY KEY
  (CONFIDENTIAL_ADDR_CATGRY_ID)
  USING INDEX ADR.CNFADDR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CONFIDENTIAL_ADDR_CATGRY ADD (
  CONSTRAINT CNFADDR_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CONFIDENTIAL_ADDR_CATGRY TO ADR_RW;

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY TO ES_R;

GRANT INSERT, SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY TO ES_RW;

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.CONFIDENTIAL_ADDR_CATGRY TO PSIM_ADR_RW;
ALTER TABLE ADR.CONFIDENTIAL_ADDR_CATGRY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CONFIDENTIAL_ADDR_CATGRY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CONFIDENTIAL_ADDR_CATGRY_H
(
  CONFIDENTIAL_ADDR_CATGRY_H_ID  NUMBER(20)     NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  CONFIDENTIAL_ADDR_CATGRY_ID    NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  CONFIDENTIAL_ADDR_TYPE_ID      NUMBER(20)     NOT NULL,
  INACTIVE_FLAG                  NUMBER(1)      DEFAULT 0                     NOT NULL,
  INACTIVE_DATE                  DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  RECORD_MODIFIED_SITE_ID        NUMBER(20),
  RECORD_MODIFIED_SRC_NAME       VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CONFIDENTIAL_ADDR_CATGRY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.CONFIDENTIAL_ADDR_CATGRY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.CONFIDENTIAL_ADDR_CATGRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CONFIDENTIAL_ADDR_CATGRY_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.CNFADDRH_PK ON ADR.CONFIDENTIAL_ADDR_CATGRY_H
(CONFIDENTIAL_ADDR_CATGRY_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CNFADDRH_PRSNID_NU_I ON ADR.CONFIDENTIAL_ADDR_CATGRY_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CONFIDENTIAL_ADDR_CATGRY_H FOR ADR.CONFIDENTIAL_ADDR_CATGRY_H;


ALTER TABLE ADR.CONFIDENTIAL_ADDR_CATGRY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_010
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT CNFADDRH_PK
  PRIMARY KEY
  (CONFIDENTIAL_ADDR_CATGRY_H_ID)
  USING INDEX ADR.CNFADDRH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY_H TO ADR_R;

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY_H TO ADR_RW;

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY_H TO ES_RW;

GRANT SELECT ON ADR.CONFIDENTIAL_ADDR_CATGRY_H TO PSIM_ADR_R;
ALTER TABLE ADR.CONFLICT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CONFLICT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.CONFLICT_DETAIL
(
  CONFLICT_DETAIL_ID      NUMBER(20)            NOT NULL,
  MILITARY_SERVICE_ID     NUMBER(20)            NOT NULL,
  STD_COMBATLOCATION_ID   NUMBER(20),
  CONFLICT_FROM_DATE_TXT  VARCHAR2(14 BYTE),
  CONFLICT_TO_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CONFLICT_DETAIL IS 'The table stores information about conflicts the person has been a part of during their military service. MILITARY_SERVICE_ID Foreign key to the MILITARY_SERVICE table. CONFLICT_LOCATION_TYPE_ID Foreign key to the CONFLICT_LOCATION_TYPE table. Indicates the particular conflict in which a person participated as part of his or her military service (for example, Korea, Vietnam). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.CONFLICT_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.MILITARY_SERVICE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.CONFLICT_FROM_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.CONFLICT_TO_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CONFLDTL_MILSRVC_FK_I ON ADR.CONFLICT_DETAIL
(MILITARY_SERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CONFLDTL_PK ON ADR.CONFLICT_DETAIL
(CONFLICT_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CONFLDTL_STD_CMBTLOCN_FK_I ON ADR.CONFLICT_DETAIL
(STD_COMBATLOCATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CONFLICT_DETAIL_audir_trgr
after Update or Delete or Insert
ON ADR.CONFLICT_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into CONFLICT_DETAIL_H
		(
		CONFLICT_DETAIL_H_ID ,
		CONFLICT_DETAIL_ID   ,
		MILITARY_SERVICE_ID  ,
		STD_COMBATLOCATION_ID,
		CONFLICT_FROM_DATE_TXT  ,
		CONFLICT_TO_DATE_TXT    ,
		RECORD_CREATED_BY        ,
		RECORD_CREATED_DATE      ,
		RECORD_MODIFIED_BY       ,
		RECORD_MODIFIED_DATE     ,
		RECORD_MODIFIED_COUNT    ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		CONFLICT_DETAIL_H_S.nextval,
		:old.CONFLICT_DETAIL_ID,
		:old.MILITARY_SERVICE_ID,
		:old.STD_COMBATLOCATION_ID,
		:old.CONFLICT_FROM_DATE_TXT,
		:old.CONFLICT_TO_DATE_TXT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into CONFLICT_DETAIL_H
		(
		CONFLICT_DETAIL_H_ID ,
		CONFLICT_DETAIL_ID   ,
		MILITARY_SERVICE_ID  ,
		STD_COMBATLOCATION_ID,
		CONFLICT_FROM_DATE_TXT  ,
		CONFLICT_TO_DATE_TXT    ,
		RECORD_CREATED_BY        ,
		RECORD_CREATED_DATE      ,
		RECORD_MODIFIED_BY       ,
		RECORD_MODIFIED_DATE     ,
		RECORD_MODIFIED_COUNT    ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		CONFLICT_DETAIL_H_S.nextval,
		:new.CONFLICT_DETAIL_ID,
		:new.MILITARY_SERVICE_ID,
		:new.STD_COMBATLOCATION_ID,
		:new.CONFLICT_FROM_DATE_TXT,
		:new.CONFLICT_TO_DATE_TXT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  CONFLICT_DETAIL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM CONFLICT_DETAIL FOR ADR.CONFLICT_DETAIL;


ALTER TABLE ADR.CONFLICT_DETAIL ADD (
  CONSTRAINT CONFLDTL_PK
  PRIMARY KEY
  (CONFLICT_DETAIL_ID)
  USING INDEX ADR.CONFLDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CONFLICT_DETAIL ADD (
  CONSTRAINT CONFLDTL_MILSRVC_FK 
  FOREIGN KEY (MILITARY_SERVICE_ID) 
  REFERENCES ADR.MILITARY_SERVICE (MILITARY_SERVICE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.CONFLICT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CONFLICT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.CONFLICT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.CONFLICT_DETAIL TO ES_RW;
ALTER TABLE ADR.CONFLICT_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CONFLICT_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CONFLICT_DETAIL_H
(
  CONFLICT_DETAIL_H_ID    NUMBER(20),
  TRANSACTION_TYPE_CODE   VARCHAR2(1 BYTE),
  CONFLICT_DETAIL_ID      NUMBER(20),
  MILITARY_SERVICE_ID     NUMBER(20),
  STD_COMBATLOCATION_ID   NUMBER(20),
  CONFLICT_FROM_DATE_TXT  VARCHAR2(14 BYTE),
  CONFLICT_TO_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY       VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE     DATE,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE    DATE,
  RECORD_MODIFIED_COUNT   NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CONFLICT_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.CONFLICT_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.CONFLICT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.MILITARY_SERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.CONFLICT_FROM_DATE_TXT IS 'Date on which the persons participation in a particular conflict began. ';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.CONFLICT_TO_DATE_TXT IS 'Date on which the persons participation in a particular conflict ended.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CONFLICT_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.CONFLDTLH_MILSRVC_NU_I ON ADR.CONFLICT_DETAIL_H
(MILITARY_SERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CONFLDTLH_PK ON ADR.CONFLICT_DETAIL_H
(CONFLICT_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CONFLICT_DETAIL_H FOR ADR.CONFLICT_DETAIL_H;


ALTER TABLE ADR.CONFLICT_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_029
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066506
  CHECK ("CONFLICT_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066507
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066508
  CHECK ("CONFLICT_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066509
  CHECK ("MILITARY_SERVICE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066510
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066511
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066512
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066513
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066514
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT CONFLDTLH_PK
  PRIMARY KEY
  (CONFLICT_DETAIL_H_ID)
  USING INDEX ADR.CONFLDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CONFLICT_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.CONFLICT_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.CONFLICT_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CONFLICT_DETAIL_H TO ES_RW;
ALTER TABLE ADR.CORRESPONDENCE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CORRESPONDENCE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CORRESPONDENCE
(
  CORRESPONDENCE_ID              NUMBER(20),
  CORRESPONDENCE_TYPE_ID         NUMBER(20)     NOT NULL,
  CORRESPONDENCE_STATUS_ID       NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  CORRESPONDENCE_STATUS_DATE     DATE,
  CORRESPONDENCE_EFFECTIVE_DATE  DATE,
  CORRESPONDENCE_DOCUMENT_ID     NUMBER(20),
  COM_LETTER_TEMPLATE_ID         NUMBER(20),
  EMAIL_TEMPLATE_ID              NUMBER(20),
  EMAIL_H_ID                     NUMBER(20),
  ADDRESS_H_ID                   NUMBER(20),
  ERROR_DESCRIPTION              VARCHAR2(1000 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CORRESPONDENCE IS 'This table contains correspondence details for VHA initiated communications to persons via mail, email or phone.';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_ID IS 'PRIMARY KEY';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_TYPE_ID IS 'FK to ADR.CORRESPONDENCE_TYPE. CORRESPONDENCE_TYPE_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_STATUS_ID IS 'FK to ADR.CORRESPONDENCE_STATUS. CORRESPONDENCE_STATUS_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.PERSON_ID IS 'FK to ADR.PERSON.PERSON_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_STATUS_DATE IS 'THE DATE THE STATUS CHANGE WAS RECIEVED ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_EFFECTIVE_DATE IS 'THE DATE THE CORRESPONDENCE IS RETROACTIVELY EFFECTIVE ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.CORRESPONDENCE_DOCUMENT_ID IS 'FK to ADR.CORRESPONDENCE_DOCUMENT. CORRESPONDENCE_DOCUMENT_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.COM_LETTER_TEMPLATE_ID IS 'FK to ADR.COM_LETTER_TEMPLATE.COM_LETTER_TEMPLATE_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.EMAIL_TEMPLATE_ID IS 'FK to ADR.EMAIL_TEMPLATE.EMAIL_TEMPLATE_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.EMAIL_H_ID IS 'FK to ADR.EMAIL_H.EMAIL_H_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.ADDRESS_H_ID IS 'FK to ADR.ADDRESS_H.ADDRESS_H_ID ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.ERROR_DESCRIPTION IS 'REASON CORRESPONDENCE WAS NOT SUCCESSFUL ';

COMMENT ON COLUMN ADR.CORRESPONDENCE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CORRESPONDENCE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CORRESPONDENCE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ADDRESS_H_ID_FK_I ON ADR.CORRESPONDENCE
(ADDRESS_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.COM_LETTER_TEMPLATE_ID_FK_I ON ADR.CORRESPONDENCE
(COM_LETTER_TEMPLATE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CORRESPONDENCE_DOC_ID_FK_I ON ADR.CORRESPONDENCE
(CORRESPONDENCE_DOCUMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CORRESPONDENCE_PK ON ADR.CORRESPONDENCE
(CORRESPONDENCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CORRESPONDENCE_STATUS_ID_FK_I ON ADR.CORRESPONDENCE
(CORRESPONDENCE_STATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CORRESPONDENCE_TYPE_ID_FK_I ON ADR.CORRESPONDENCE
(CORRESPONDENCE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_H_ID_FK_I ON ADR.CORRESPONDENCE
(EMAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_TEMPLATE_ID_FK_I ON ADR.CORRESPONDENCE
(EMAIL_TEMPLATE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PERSON_ID_FK_I ON ADR.CORRESPONDENCE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.CORRESPONDENCE_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.CORRESPONDENCE
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO CORRESPONDENCE_H
        (CORRESPONDENCE_H_ID,
         CORRESPONDENCE_ID,
         CORRESPONDENCE_TYPE_ID,
         CORRESPONDENCE_STATUS_ID,
         PERSON_ID ,
         CORRESPONDENCE_STATUS_DATE,
         CORRESPONDENCE_EFFECTIVE_DATE,
         CORRESPONDENCE_DOCUMENT_ID ,
         COM_LETTER_TEMPLATE_ID,
         EMAIL_TEMPLATE_ID ,
         EMAIL_H_ID  ,
         ADDRESS_H_ID,
         ERROR_DESCRIPTION ,
         RECORD_CREATED_BY ,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY ,
         RECORD_MODIFIED_COUNT,
         RECORD_MODIFIED_DATE,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (CORRESPONDENCE_H_S.NEXTVAL,
         :OLD.CORRESPONDENCE_ID,
         :OLD.CORRESPONDENCE_TYPE_ID,
         :OLD.CORRESPONDENCE_STATUS_ID,
         :OLD.PERSON_ID ,
         :OLD.CORRESPONDENCE_STATUS_DATE,
         :OLD.CORRESPONDENCE_EFFECTIVE_DATE,
         :OLD.CORRESPONDENCE_DOCUMENT_ID ,
         :OLD.COM_LETTER_TEMPLATE_ID,
         :OLD.EMAIL_TEMPLATE_ID ,
         :OLD.EMAIL_H_ID  ,
         :OLD.ADDRESS_H_ID,
         :OLD.ERROR_DESCRIPTION ,
         :OLD.Record_created_by,
         :OLD.Record_created_date,
         :OLD.Record_modified_by,
         :OLD.Record_modified_count,
         :OLD.Record_modified_date,
         SYSDATE,
         'D'
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      INSERT INTO CORRESPONDENCE_H
        (CORRESPONDENCE_H_ID,
         CORRESPONDENCE_ID,
         CORRESPONDENCE_TYPE_ID,
         CORRESPONDENCE_STATUS_ID,
         PERSON_ID ,
         CORRESPONDENCE_STATUS_DATE,
         CORRESPONDENCE_EFFECTIVE_DATE,
         CORRESPONDENCE_DOCUMENT_ID ,
         COM_LETTER_TEMPLATE_ID,
         EMAIL_TEMPLATE_ID ,
         EMAIL_H_ID  ,
         ADDRESS_H_ID,
         ERROR_DESCRIPTION ,
         RECORD_CREATED_BY ,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY ,
         RECORD_MODIFIED_COUNT,
         RECORD_MODIFIED_DATE,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (CORRESPONDENCE_H_S.NEXTVAL,
         :NEW.CORRESPONDENCE_ID,
         :NEW.CORRESPONDENCE_TYPE_ID,
         :NEW.CORRESPONDENCE_STATUS_ID,
         :NEW.PERSON_ID ,
         :NEW.CORRESPONDENCE_STATUS_DATE,
         :NEW.CORRESPONDENCE_EFFECTIVE_DATE,
         :NEW.CORRESPONDENCE_DOCUMENT_ID ,
         :NEW.COM_LETTER_TEMPLATE_ID,
         :NEW.EMAIL_TEMPLATE_ID ,
         :NEW.EMAIL_H_ID  ,
         :NEW.ADDRESS_H_ID,
         :NEW.ERROR_DESCRIPTION ,
         :NEW.Record_created_by,
         :NEW.Record_created_date,
         :NEW.Record_modified_by,
         :NEW.Record_modified_count,
         :NEW.Record_modified_date,
         SYSDATE,
         mDML
        );

    END IF;

  END CORRESPONDENCE_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM CORRESPONDENCE FOR ADR.CORRESPONDENCE;


ALTER TABLE ADR.CORRESPONDENCE ADD (
  CONSTRAINT CORRESPONDENCE_PK
  PRIMARY KEY
  (CORRESPONDENCE_ID)
  USING INDEX ADR.CORRESPONDENCE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.CORRESPONDENCE ADD (
  CONSTRAINT CORRESPONDENCE_ADDR_H_FK 
  FOREIGN KEY (ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT CORRESPONDENCE_COM_LTR_TEMP_FK 
  FOREIGN KEY (COM_LETTER_TEMPLATE_ID) 
  REFERENCES ADR.COM_LETTER_TEMPLATE (COM_LETTER_TEMPLATE_ID)
  ENABLE VALIDATE,
  CONSTRAINT CORRESPONDENCE_EMAIL_H_FK 
  FOREIGN KEY (EMAIL_H_ID) 
  REFERENCES ADR.EMAIL_H (EMAIL_H_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT CORRESPONDENCE_EMAIL_TEMP_FK 
  FOREIGN KEY (EMAIL_TEMPLATE_ID) 
  REFERENCES ADR.COM_EMAIL_TEMPLATE (COM_EMAIL_TEMPLATE_ID)
  ENABLE VALIDATE,
  CONSTRAINT CORRESPONDENCE_PERSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT CORRPNDNC_CORRPNDNC_DOC_FK 
  FOREIGN KEY (CORRESPONDENCE_DOCUMENT_ID) 
  REFERENCES ADR.CORRESPONDENCE_DOCUMENT (CORRESPONDENCE_DOCUMENT_ID)
  ENABLE VALIDATE,
  CONSTRAINT CORRPNDNC_CORRPNDNC_STATUS_FK 
  FOREIGN KEY (CORRESPONDENCE_STATUS_ID) 
  REFERENCES ADR.CORRESPONDENCE_STATUS (CORRESPONDENCE_STATUS_ID)
  ENABLE VALIDATE,
  CONSTRAINT CORRPNDNC_CORRPNDNC_TYPE_FK 
  FOREIGN KEY (CORRESPONDENCE_TYPE_ID) 
  REFERENCES ADR.CORRESPONDENCE_TYPE (CORRESPONDENCE_TYPE_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CORRESPONDENCE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CORRESPONDENCE TO ADR_RW;

GRANT SELECT ON ADR.CORRESPONDENCE TO ES_R;

GRANT INSERT, SELECT ON ADR.CORRESPONDENCE TO ES_RW;
ALTER TABLE ADR.CORRESPONDENCE_DOCUMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CORRESPONDENCE_DOCUMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.CORRESPONDENCE_DOCUMENT
(
  CORRESPONDENCE_DOCUMENT_ID  NUMBER(20)        NOT NULL,
  DOCUMENT_FILENAME           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  DOCUMENT                    CLOB
)
LOB (DOCUMENT) STORE AS SECUREFILE (
  TABLESPACE  L_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  COMPRESS    MEDIUM
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CORRESPONDENCE_DOCUMENT IS 'This table contains documents received that are related to the correspondence communication.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.CORRESPONDENCE_DOCUMENT_ID IS 'PRIMARY KEY.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.DOCUMENT_FILENAME IS 'NAME OF THE FILE';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_DOCUMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CORRESPONDENCE_DOCUMENT_PK ON ADR.CORRESPONDENCE_DOCUMENT
(CORRESPONDENCE_DOCUMENT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CORRESPONDENCE_DOCUMENT FOR ADR.CORRESPONDENCE_DOCUMENT;


ALTER TABLE ADR.CORRESPONDENCE_DOCUMENT ADD (
  CONSTRAINT CORRESPONDENCE_DOCUMENT_PK
  PRIMARY KEY
  (CORRESPONDENCE_DOCUMENT_ID)
  USING INDEX ADR.CORRESPONDENCE_DOCUMENT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CORRESPONDENCE_DOCUMENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CORRESPONDENCE_DOCUMENT TO ADR_RW;

GRANT SELECT ON ADR.CORRESPONDENCE_DOCUMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.CORRESPONDENCE_DOCUMENT TO ES_RW;
ALTER TABLE ADR.CORRESPONDENCE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CORRESPONDENCE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.CORRESPONDENCE_H
(
  CORRESPONDENCE_H_ID            NUMBER(20)     NOT NULL,
  CORRESPONDENCE_ID              NUMBER(20)     NOT NULL,
  CORRESPONDENCE_TYPE_ID         NUMBER(20),
  CORRESPONDENCE_STATUS_ID       NUMBER(20),
  PERSON_ID                      NUMBER(20),
  CORRESPONDENCE_STATUS_DATE     DATE,
  CORRESPONDENCE_EFFECTIVE_DATE  DATE,
  CORRESPONDENCE_DOCUMENT_ID     NUMBER(20),
  COM_LETTER_TEMPLATE_ID         NUMBER(20),
  EMAIL_TEMPLATE_ID              NUMBER(20),
  EMAIL_H_ID                     NUMBER(20),
  ADDRESS_H_ID                   NUMBER(20),
  ERROR_DESCRIPTION              VARCHAR2(1000 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CORRESPONDENCE_H IS 'The Standard History table name appends _H to the end of the table name and adds three columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row and a History_Created_Date.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_H_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_ID IS 'Primary Key of base table';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_TYPE_ID IS 'ADR.Correspondence_Type ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_STATUS_ID IS 'ADR.Correspondence_STATUS ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.PERSON_ID IS 'ADR.PERSON ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_STATUS_DATE IS 'ADR.CORRESPONDENCE STATUS Date';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_EFFECTIVE_DATE IS 'ADR.CORRESPONDENCE EFFECTIVE Date';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.CORRESPONDENCE_DOCUMENT_ID IS 'ADR.CORRESPONDENCE_DOCUMENT ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.EMAIL_TEMPLATE_ID IS 'ADR.EMAIL.TEMPLATE ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.EMAIL_H_ID IS 'ADR.EMAIL_H ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.ADDRESS_H_ID IS ' ADDRESS_H ID';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.ERROR_DESCRIPTION IS 'ADR.CORRESPONDENCE ERROR_DESCRIPTION';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_H.HISTORY_CREATED_DATE IS 'Date History Record was Created';


CREATE UNIQUE INDEX ADR.CORRESPONDENCE_H_PK ON ADR.CORRESPONDENCE_H
(CORRESPONDENCE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CORRESPONDENCE_H FOR ADR.CORRESPONDENCE_H;


ALTER TABLE ADR.CORRESPONDENCE_H ADD (
  CONSTRAINT CORRESPONDENCE_H_PK
  PRIMARY KEY
  (CORRESPONDENCE_H_ID)
  USING INDEX ADR.CORRESPONDENCE_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CORRESPONDENCE_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CORRESPONDENCE_H TO ADR_RW;

GRANT SELECT ON ADR.CORRESPONDENCE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.CORRESPONDENCE_H TO ES_RW;
ALTER TABLE ADR.CORRESPONDENCE_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CORRESPONDENCE_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.CORRESPONDENCE_STATUS
(
  CORRESPONDENCE_STATUS_ID  NUMBER(20),
  CODE                      VARCHAR2(10 BYTE)   NOT NULL,
  NAME                      VARCHAR2(30 BYTE)   NOT NULL,
  DESCRIPTION               VARCHAR2(250 BYTE)  NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CORRESPONDENCE_STATUS IS 'This table contains reference lookup values to track the specific state of correspondence communications.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.CORRESPONDENCE_STATUS_ID IS 'PRIMARY KEY.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.CODE IS 'LOOKUP CODE SHORT NAME VALUE.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.NAME IS 'LOOKUP CODE LONG NAME VALUE.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.DESCRIPTION IS 'LOOKUP CODE DESCRIPTION.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CORRESPONDENCE_STATUS_CODE_UK ON ADR.CORRESPONDENCE_STATUS
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CORRESPONDENCE_STATUS_PK ON ADR.CORRESPONDENCE_STATUS
(CORRESPONDENCE_STATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CORRESPOND_STATUS_NAME_NU_I ON ADR.CORRESPONDENCE_STATUS
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CORRESPONDENCE_STATUS FOR ADR.CORRESPONDENCE_STATUS;


ALTER TABLE ADR.CORRESPONDENCE_STATUS ADD (
  CONSTRAINT CORRESPONDENCE_STATUS_PK
  PRIMARY KEY
  (CORRESPONDENCE_STATUS_ID)
  USING INDEX ADR.CORRESPONDENCE_STATUS_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CORRESPONDENCE_STATUS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CORRESPONDENCE_STATUS TO ADR_RW;

GRANT SELECT ON ADR.CORRESPONDENCE_STATUS TO ES_R;

GRANT INSERT, SELECT ON ADR.CORRESPONDENCE_STATUS TO ES_RW;
ALTER TABLE ADR.CORRESPONDENCE_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.CORRESPONDENCE_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.CORRESPONDENCE_TYPE
(
  CORRESPONDENCE_TYPE_ID  NUMBER(20),
  CODE                    VARCHAR2(10 BYTE)     NOT NULL,
  NAME                    VARCHAR2(30 BYTE)     NOT NULL,
  DESCRIPTION             VARCHAR2(250 BYTE)    NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.CORRESPONDENCE_TYPE IS 'This table contains reference lookup values to track the specific type of correspondence method.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.CORRESPONDENCE_TYPE_ID IS 'PRIMARY KEY.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.CODE IS 'LOOKUP CODE SHORT NAME VALUE.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.NAME IS 'LOOKUP CODE LONG NAME VALUE.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.DESCRIPTION IS 'LOOKUP CODE DESCRIPTION.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.CORRESPONDENCE_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CORRESPONDENCE_TYPE_CODE_UK ON ADR.CORRESPONDENCE_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CORRESPONDENCE_TYPE_NAME_NU_I ON ADR.CORRESPONDENCE_TYPE
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.CORRESPONDENCE_TYPE_PK ON ADR.CORRESPONDENCE_TYPE
(CORRESPONDENCE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM CORRESPONDENCE_TYPE FOR ADR.CORRESPONDENCE_TYPE;


ALTER TABLE ADR.CORRESPONDENCE_TYPE ADD (
  CONSTRAINT CORRESPONDENCE_TYPE_PK
  PRIMARY KEY
  (CORRESPONDENCE_TYPE_ID)
  USING INDEX ADR.CORRESPONDENCE_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.CORRESPONDENCE_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.CORRESPONDENCE_TYPE TO ADR_RW;

GRANT SELECT ON ADR.CORRESPONDENCE_TYPE TO ES_R;

GRANT INSERT, SELECT ON ADR.CORRESPONDENCE_TYPE TO ES_RW;
ALTER TABLE ADR.DELIVERY_PREFERENCE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DELIVERY_PREFERENCE CASCADE CONSTRAINTS;

CREATE TABLE ADR.DELIVERY_PREFERENCE
(
  DELIVERY_PREFERENCE_ID         NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  STD_DELIVERYPREFERENCETYPE_ID  NUMBER(20)     NOT NULL,
  STD_DELIVERYPREFSRCTYPE_ID     NUMBER(20)     NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DELIVERY_PREFERENCE IS 'Prefered method of delivery to the veteran.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.DELIVERY_PREFERENCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.STD_DELIVERYPREFERENCETYPE_ID IS 'ID of the veteran''s preference for delivery.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE.STD_DELIVERYPREFSRCTYPE_ID IS 'ID of the source application that specified the veteran''s preference for delivery.';


CREATE INDEX ADR.DLVPRF_DLVPRFSRCTYP_FK_I ON ADR.DELIVERY_PREFERENCE
(STD_DELIVERYPREFSRCTYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DLVPRF_DLVPRFTYP_FK_I ON ADR.DELIVERY_PREFERENCE
(STD_DELIVERYPREFERENCETYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.DLVPRF_PK ON ADR.DELIVERY_PREFERENCE
(DELIVERY_PREFERENCE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DLVPRF_PRSN_FK_I ON ADR.DELIVERY_PREFERENCE
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.DLVPRF_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.DELIVERY_PREFERENCE
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into DELIVERY_PREFERENCE_H
        (
        DELIVERY_PREFERENCE_H_ID,
        DELIVERY_PREFERENCE_ID,
        PERSON_ID,
        STD_DELIVERYPREFERENCETYPE_ID,
STD_DELIVERYPREFSRCTYPE_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DELIVERY_PREFERENCE_H_S.nextval,
        :old.DELIVERY_PREFERENCE_ID,
        :old.PERSON_ID,
        :old.STD_DELIVERYPREFERENCETYPE_ID,
:old.STD_DELIVERYPREFSRCTYPE_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into DELIVERY_PREFERENCE_H
        (
        DELIVERY_PREFERENCE_H_ID,
        DELIVERY_PREFERENCE_ID,
        PERSON_ID,
        STD_DELIVERYPREFERENCETYPE_ID,
STD_DELIVERYPREFSRCTYPE_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DELIVERY_PREFERENCE_H_S.nextval,
        :new.DELIVERY_PREFERENCE_ID,
        :new.PERSON_ID,
        :new.STD_DELIVERYPREFERENCETYPE_ID,
:new.STD_DELIVERYPREFSRCTYPE_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end DELIVERY_PREFERENCE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM DELIVERY_PREFERENCE FOR ADR.DELIVERY_PREFERENCE;


ALTER TABLE ADR.DELIVERY_PREFERENCE ADD (
  CONSTRAINT DLVPRF_PK
  PRIMARY KEY
  (DELIVERY_PREFERENCE_ID)
  USING INDEX ADR.DLVPRF_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.DELIVERY_PREFERENCE ADD (
  CONSTRAINT DLVPRF_PRS_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.DELIVERY_PREFERENCE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.DELIVERY_PREFERENCE TO ADR_RW;

GRANT SELECT ON ADR.DELIVERY_PREFERENCE TO ES_R;

GRANT INSERT, SELECT ON ADR.DELIVERY_PREFERENCE TO ES_RW;
ALTER TABLE ADR.DELIVERY_PREFERENCE_EMAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DELIVERY_PREFERENCE_EMAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.DELIVERY_PREFERENCE_EMAIL
(
  DELIVERY_PREFERENCE_EMAIL_ID  NUMBER(20)      NOT NULL,
  STD_EMAILADDRESSTYPE_ID       NUMBER(20)      NOT NULL,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL,
  DELIVERY_PREFERENCE_ID        NUMBER(20)      NOT NULL,
  EMAIL_ADDRESS                 VARCHAR2(128 BYTE) NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DELIVERY_PREFERENCE_EMAIL IS 'Delivery Preference email address for a person.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.DELIVERY_PREFERENCE_EMAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.STD_EMAILADDRESSTYPE_ID IS 'Type of email address for delivery';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL.DELIVERY_PREFERENCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';


CREATE INDEX ADR.DLVPRFEM_DLVPRF_FK_I ON ADR.DELIVERY_PREFERENCE_EMAIL
(DELIVERY_PREFERENCE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.DLVPRFEM_PK ON ADR.DELIVERY_PREFERENCE_EMAIL
(DELIVERY_PREFERENCE_EMAIL_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DLVPRFEM_STD_EMAILADDRTYP_FK_I ON ADR.DELIVERY_PREFERENCE_EMAIL
(STD_EMAILADDRESSTYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.DLVPRFEM_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.DELIVERY_PREFERENCE_EMAIL
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into DELIVERY_PREFERENCE_EMAIL_H
        (
        DELIVERY_PREFERENCE_EMAIL_H_ID,
        DELIVERY_PREFERENCE_EMAIL_ID,
DELIVERY_PREFERENCE_ID,
STD_EMAILADDRESSTYPE_ID,
EMAIL_ADDRESS,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DELIVERY_PREFERENCE_EMAIL_H_S.nextval,
        :old.DELIVERY_PREFERENCE_EMAIL_ID,
        :old.DELIVERY_PREFERENCE_ID,
        :old.STD_EMAILADDRESSTYPE_ID,
        :old.EMAIL_ADDRESS,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into DELIVERY_PREFERENCE_EMAIL_H
        (
        DELIVERY_PREFERENCE_EMAIL_H_ID,
        DELIVERY_PREFERENCE_EMAIL_ID,
 	DELIVERY_PREFERENCE_ID,
	STD_EMAILADDRESSTYPE_ID,
EMAIL_ADDRESS,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DELIVERY_PREFERENCE_EMAIL_H_S.nextval,
        :new.DELIVERY_PREFERENCE_EMAIL_ID,
        :new.DELIVERY_PREFERENCE_ID,
        :new.STD_EMAILADDRESSTYPE_ID,
        :new.EMAIL_ADDRESS,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end DLVPRFEM_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM DELIVERY_PREFERENCE_EMAIL FOR ADR.DELIVERY_PREFERENCE_EMAIL;


ALTER TABLE ADR.DELIVERY_PREFERENCE_EMAIL ADD (
  CONSTRAINT DLVPRFEM_PK
  PRIMARY KEY
  (DELIVERY_PREFERENCE_EMAIL_ID)
  USING INDEX ADR.DLVPRFEM_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.DELIVERY_PREFERENCE_EMAIL ADD (
  CONSTRAINT DLVPRFEM_DLVPRF_FK 
  FOREIGN KEY (DELIVERY_PREFERENCE_ID) 
  REFERENCES ADR.DELIVERY_PREFERENCE (DELIVERY_PREFERENCE_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.DELIVERY_PREFERENCE_EMAIL TO ADR_RW;

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL TO ES_RW;
ALTER TABLE ADR.DELIVERY_PREFERENCE_EMAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DELIVERY_PREFERENCE_EMAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.DELIVERY_PREFERENCE_EMAIL_H
(
  DELIVERY_PREFERENCE_EMAIL_ID    NUMBER(20)    NOT NULL,
  STD_EMAILADDRESSTYPE_ID         NUMBER(20)    NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  DELIVERY_PREFERENCE_ID          NUMBER(20)    NOT NULL,
  EMAIL_ADDRESS                   VARCHAR2(128 BYTE) NOT NULL,
  HISTORY_CREATED_DATE            DATE          NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  DELIVERY_PREFERENCE_EMAIL_H_ID  NUMBER(20)    NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DELIVERY_PREFERENCE_EMAIL_H IS 'Delivery Preference email address for a person.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.DELIVERY_PREFERENCE_EMAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.STD_EMAILADDRESSTYPE_ID IS 'Type of email address for delivery';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.DELIVERY_PREFERENCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.HISTORY_CREATED_DATE IS 'Date history record created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.TRANSACTION_TYPE_CODE IS 'Whether this row was written because of a Create, Update, or Delete.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_EMAIL_H.DELIVERY_PREFERENCE_EMAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';


CREATE UNIQUE INDEX ADR.DLVPRFEM_H_PK ON ADR.DELIVERY_PREFERENCE_EMAIL_H
(DELIVERY_PREFERENCE_EMAIL_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM DELIVERY_PREFERENCE_EMAIL_H FOR ADR.DELIVERY_PREFERENCE_EMAIL_H;


ALTER TABLE ADR.DELIVERY_PREFERENCE_EMAIL_H ADD (
  CONSTRAINT DLVPRFEM_H_PK
  PRIMARY KEY
  (DELIVERY_PREFERENCE_EMAIL_H_ID)
  USING INDEX ADR.DLVPRFEM_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.DELIVERY_PREFERENCE_EMAIL_H TO ADR_RW;

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.DELIVERY_PREFERENCE_EMAIL_H TO ES_RW;
ALTER TABLE ADR.DELIVERY_PREFERENCE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DELIVERY_PREFERENCE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.DELIVERY_PREFERENCE_H
(
  DELIVERY_PREFERENCE_ID         NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  DELIVERY_PREFERENCE_H_ID       NUMBER(20)     NOT NULL,
  STD_DELIVERYPREFERENCETYPE_ID  NUMBER(20)     NOT NULL,
  STD_DELIVERYPREFSRCTYPE_ID     NUMBER(20)     NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DELIVERY_PREFERENCE_H IS 'Prefered method of delivery to the veteran.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.DELIVERY_PREFERENCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.HISTORY_CREATED_DATE IS 'Date history record created.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.TRANSACTION_TYPE_CODE IS 'Whether this row was written because of a Create, Update, or Delete.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.DELIVERY_PREFERENCE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.STD_DELIVERYPREFERENCETYPE_ID IS 'ID of the veteran''s preference for delivery.';

COMMENT ON COLUMN ADR.DELIVERY_PREFERENCE_H.STD_DELIVERYPREFSRCTYPE_ID IS 'ID of the source application that specified the veteran''s preference for delivery.';


CREATE UNIQUE INDEX ADR.DLVPRF_H_PK ON ADR.DELIVERY_PREFERENCE_H
(DELIVERY_PREFERENCE_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM DELIVERY_PREFERENCE_H FOR ADR.DELIVERY_PREFERENCE_H;


ALTER TABLE ADR.DELIVERY_PREFERENCE_H ADD (
  CONSTRAINT DLVPRF_H_PK
  PRIMARY KEY
  (DELIVERY_PREFERENCE_H_ID)
  USING INDEX ADR.DLVPRF_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.DELIVERY_PREFERENCE_H TO ADR_RW;

GRANT SELECT ON ADR.DELIVERY_PREFERENCE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.DELIVERY_PREFERENCE_H TO ES_RW;
ALTER TABLE ADR.DEPLOYMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DEPLOYMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.DEPLOYMENT
(
  DEPLOYMENT_ID             NUMBER(20)          NOT NULL,
  ACTIVATION_ID             NUMBER(20)          NOT NULL,
  STD_TERMINATIONREASON_ID  NUMBER(20),
  STD_MHDATASOURCE_ID       NUMBER(20),
  STD_COUNTRY_ID            NUMBER(20),
  STD_SPECIALOPERATION_ID   NUMBER(20),
  DEPLOYMENT_BEGIN_DATE     DATE,
  DEPLOYMENT_END_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(18)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DEPLOYMENT IS 'Period that a veteran was deployed during an Activation';

COMMENT ON COLUMN ADR.DEPLOYMENT.DEPLOYMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DEPLOYMENT.ACTIVATION_ID IS 'Unique identifier for an Activation';

COMMENT ON COLUMN ADR.DEPLOYMENT.STD_TERMINATIONREASON_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.DEPLOYMENT.STD_MHDATASOURCE_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.DEPLOYMENT.STD_SPECIALOPERATION_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.DEPLOYMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.DEPLOYMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DEPLOYMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.DEPLOYMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DEPLOYMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.DEP_ACT_FK_I ON ADR.DEPLOYMENT
(ACTIVATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DEP_MHDS_FK_I ON ADR.DEPLOYMENT
(STD_MHDATASOURCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.DEP_PK ON ADR.DEPLOYMENT
(DEPLOYMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DEP_SC_FK_I ON ADR.DEPLOYMENT
(STD_COUNTRY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DEP_SO_FK_I ON ADR.DEPLOYMENT
(STD_SPECIALOPERATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.DEP_TR_FK_I ON ADR.DEPLOYMENT
(STD_TERMINATIONREASON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.DEPLOYMENT_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.DEPLOYMENT
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into DEPLOYMENT_H
        (
        DEPLOYMENT_H_ID,
        DEPLOYMENT_ID,
        ACTIVATION_ID,
        STD_TERMINATIONREASON_ID,
        STD_MHDATASOURCE_ID,
        STD_COUNTRY_ID,
        STD_SPECIALOPERATION_ID,
        DEPLOYMENT_BEGIN_DATE,
        DEPLOYMENT_END_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DEPLOYMENT_H_S.nextval,
        :old.DEPLOYMENT_ID,
        :old.ACTIVATION_ID,
        :old.STD_TERMINATIONREASON_ID,
        :old.STD_MHDATASOURCE_ID,
        :old.STD_COUNTRY_ID,
        :old.STD_SPECIALOPERATION_ID,
        :old.DEPLOYMENT_BEGIN_DATE,
        :old.DEPLOYMENT_END_DATE,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into DEPLOYMENT_H
        (
        DEPLOYMENT_H_ID,
        DEPLOYMENT_ID,
        ACTIVATION_ID,
        STD_TERMINATIONREASON_ID,
        STD_MHDATASOURCE_ID,
        STD_COUNTRY_ID,
        STD_SPECIALOPERATION_ID,
        DEPLOYMENT_BEGIN_DATE,
        DEPLOYMENT_END_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        DEPLOYMENT_H_S.nextval,
        :new.DEPLOYMENT_ID,
        :new.ACTIVATION_ID,
        :new.STD_TERMINATIONREASON_ID,
        :new.STD_MHDATASOURCE_ID,
        :new.STD_COUNTRY_ID,
        :new.STD_SPECIALOPERATION_ID,
        :new.DEPLOYMENT_BEGIN_DATE,
        :new.DEPLOYMENT_END_DATE,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end DEPLOYMENT_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM DEPLOYMENT FOR ADR.DEPLOYMENT;


ALTER TABLE ADR.DEPLOYMENT ADD (
  CONSTRAINT DEP_PK
  PRIMARY KEY
  (DEPLOYMENT_ID)
  USING INDEX ADR.DEP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.DEPLOYMENT ADD (
  CONSTRAINT DEP_ACT_FK 
  FOREIGN KEY (ACTIVATION_ID) 
  REFERENCES ADR.ACTIVATION (ACTIVATION_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.DEPLOYMENT TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.DEPLOYMENT TO ADR_RW;

GRANT SELECT ON ADR.DEPLOYMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.DEPLOYMENT TO ES_RW;
ALTER TABLE ADR.DEPLOYMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.DEPLOYMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.DEPLOYMENT_H
(
  DEPLOYMENT_H_ID           NUMBER(20)          NOT NULL,
  DEPLOYMENT_ID             NUMBER(20)          NOT NULL,
  ACTIVATION_ID             NUMBER(20)          NOT NULL,
  STD_TERMINATIONREASON_ID  NUMBER(20),
  STD_MHDATASOURCE_ID       NUMBER(20),
  STD_COUNTRY_ID            NUMBER(20),
  STD_SPECIALOPERATION_ID   NUMBER(20),
  DEPLOYMENT_BEGIN_DATE     DATE,
  DEPLOYMENT_END_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(18)          NOT NULL,
  HISTORY_CREATED_DATE      DATE                NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.DEPLOYMENT_H IS 'Period that a veteran was deployed during an Activation';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.DEPLOYMENT_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.DEPLOYMENT_ID IS 'Unique identifier for a  Deployment';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.ACTIVATION_ID IS 'FK to Activation';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.STD_COUNTRY_ID IS 'Existing Country';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.DEPLOYMENT_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.DEP_H_PK ON ADR.DEPLOYMENT_H
(DEPLOYMENT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM DEPLOYMENT_H FOR ADR.DEPLOYMENT_H;


ALTER TABLE ADR.DEPLOYMENT_H ADD (
  CONSTRAINT AVCON_1253310191_TRANS_000
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT DEP_H_PK
  PRIMARY KEY
  (DEPLOYMENT_H_ID)
  USING INDEX ADR.DEP_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.DEPLOYMENT_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.DEPLOYMENT_H TO ADR_RW;

GRANT SELECT ON ADR.DEPLOYMENT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.DEPLOYMENT_H TO ES_RW;
ALTER TABLE ADR.EGT_SETTING
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_SETTING CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_SETTING
(
  EGT_SETTING_ID              NUMBER(20)        NOT NULL,
  EGT_TYPE_ID                 NUMBER(20)        NOT NULL,
  PRIORITY_GROUP_TYPE_ID      NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID  NUMBER(20),
  REFERENCE_EGT_SETTING_ID    NUMBER(20),
  EGT_EFFECTIVE_DATE          DATE,
  FEDERAL_REGISTRY_DATE       DATE,
  CURRENT_EGT_FLAG            NUMBER(1)         DEFAULT 0                     NOT NULL,
  DISABLED_EGT_FLAG           NUMBER(1)         DEFAULT 0                     NOT NULL,
  REMARKS                     VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_SETTING IS 'Enrollment Global Threshold setting for beneficiaries The EGT_SETTING table stores a record for Enrollment Group Threshold (EGT) that is set. This threshold dictates what the cutoff point is for persons receiving care from the VHA. Any person in an enrollment priority group less than or equal to the EGT setting is eligible to enroll in the VHA system and receive care; any person in an enrollment priority group greater than the threshold will not be eligible for enrollment. EGT_TYPE_ID Foreign key to the EGT_TYPE table. Indicates the type of setting (for example, annual fiscal year, enrollment decision). PRIORITY_GROUP_TYPE_ID Foreign key to the PRIORITY_GROUP_TYPE table. Indicates the Enrollment Priority Group that has been set (Group 1 through Group 8). PRIORITY_SUB_GROUP_TYPE_ID Foreign key to the PRIORITY_SUB_GROUP_TYPE table. Indicates the Enrollment Priority Group that has been set (for example, a, c). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.EGT_SETTING.EGT_SETTING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.EGT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.REFERENCE_EGT_SETTING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.EGT_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.FEDERAL_REGISTRY_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING.CURRENT_EGT_FLAG IS 'Flag used to indicate that an EGT setting has been disabled. Disabled Indicator is used if this entry was entered in error, this field should be set.  If the EGT that this record represents needs to be disabled for any reason (wrong EGT Effect Date, etc was entered) then the EGT entry needs to be disabled.  This will prevent the processing of all the enrollment records from taking place.';

COMMENT ON COLUMN ADR.EGT_SETTING.DISABLED_EGT_FLAG IS 'Flag used to indicate that an EGT setting has been disabled. Disabled Indicator is used if this entry was entered in error, this field should be set.  If the EGT that this record represents needs to be disabled for any reason (wrong EGT Effect Date, etc was entered) then the EGT entry needs to be disabled.  This will prevent the processing of all the enrollment records from taking place.';

COMMENT ON COLUMN ADR.EGT_SETTING.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.EGT_SETTING.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_SETTING.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_SETTING.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_SETTING.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_SETTING.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.EGTSETTING_EGTSETTING_FK_I ON ADR.EGT_SETTING
(REFERENCE_EGT_SETTING_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.EGTSETTING_PK ON ADR.EGT_SETTING
(EGT_SETTING_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EGTSETTING_STD_EGTTYP_FK_I ON ADR.EGT_SETTING
(EGT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EGTSETTING_STD_ENRLPGRP_FK_I ON ADR.EGT_SETTING
(PRIORITY_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EGTSETTING_STD_ENRLPSGRP_FK_I ON ADR.EGT_SETTING
(PRIORITY_SUB_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EGT_SETTING_audir_trgr
after Update or Delete or Insert
ON ADR.EGT_SETTING
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into EGT_SETTING_H
		(
		EGT_SETTING_H_ID,
		EGT_SETTING_ID  ,
		EGT_TYPE_ID     ,
		PRIORITY_GROUP_TYPE_ID,
		PRIORITY_SUB_GROUP_TYPE_ID,
		EGT_EFFECTIVE_DATE        ,
		FEDERAL_REGISTRY_DATE     ,
		REMARKS                   ,
		DISABLED_EGT_FLAG         ,
		CURRENT_EGT_FLAG,
		REFERENCE_EGT_SETTING_ID,
		RECORD_CREATED_BY         ,
		RECORD_CREATED_DATE       ,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE      ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
      		)
		values
		(
		EGT_SETTING_H_s.nextval,
		:old.EGT_SETTING_ID,
		:old.EGT_TYPE_ID,
		:old.PRIORITY_GROUP_TYPE_ID,
		:old.PRIORITY_SUB_GROUP_TYPE_ID,
		:old.EGT_EFFECTIVE_DATE,
		:old.FEDERAL_REGISTRY_DATE,
		:old.REMARKS,
		:old.DISABLED_EGT_FLAG,
		:old.CURRENT_EGT_FLAG,
		:old.REFERENCE_EGT_SETTING_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into EGT_SETTING_H
		(
		EGT_SETTING_H_ID,
		EGT_SETTING_ID  ,
		EGT_TYPE_ID     ,
		PRIORITY_GROUP_TYPE_ID,
		PRIORITY_SUB_GROUP_TYPE_ID,
		EGT_EFFECTIVE_DATE        ,
		FEDERAL_REGISTRY_DATE     ,
		REMARKS                   ,
		DISABLED_EGT_FLAG         ,
		CURRENT_EGT_FLAG,
		REFERENCE_EGT_SETTING_ID,
		RECORD_CREATED_BY         ,
		RECORD_CREATED_DATE       ,
		RECORD_MODIFIED_BY        ,
		RECORD_MODIFIED_DATE      ,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		EGT_SETTING_H_s.nextval,
		:new.EGT_SETTING_ID,
		:new.EGT_TYPE_ID,
		:new.PRIORITY_GROUP_TYPE_ID,
		:new.PRIORITY_SUB_GROUP_TYPE_ID,
		:new.EGT_EFFECTIVE_DATE,
		:new.FEDERAL_REGISTRY_DATE,
		:new.REMARKS,
		:new.DISABLED_EGT_FLAG,
		:new.CURRENT_EGT_FLAG,
		:new.REFERENCE_EGT_SETTING_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  EGT_SETTING_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EGT_SETTING FOR ADR.EGT_SETTING;


ALTER TABLE ADR.EGT_SETTING ADD (
  CONSTRAINT AVCON_1172521913_CURRE_000
  CHECK (CURRENT_EGT_FLAG BETWEEN 0 AND 0 OR CURRENT_EGT_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT AVCON_1172521913_DISAB_000
  CHECK (DISABLED_EGT_FLAG BETWEEN 0
AND 0 OR DISABLED_EGT_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT EGTSETTING_PK
  PRIMARY KEY
  (EGT_SETTING_ID)
  USING INDEX ADR.EGTSETTING_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EGT_SETTING ADD (
  CONSTRAINT EGTSETTING_EGTSETTING_FK 
  FOREIGN KEY (REFERENCE_EGT_SETTING_ID) 
  REFERENCES ADR.EGT_SETTING (EGT_SETTING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_SETTING TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EGT_SETTING TO ADR_RW;

GRANT SELECT ON ADR.EGT_SETTING TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_SETTING TO ES_RW;
ALTER TABLE ADR.EGT_SETTING_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_SETTING_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_SETTING_H
(
  EGT_SETTING_H_ID            NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  EGT_SETTING_ID              NUMBER(20)        NOT NULL,
  EGT_TYPE_ID                 NUMBER(20)        NOT NULL,
  PRIORITY_GROUP_TYPE_ID      NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID  NUMBER(20),
  REFERENCE_EGT_SETTING_ID    NUMBER(20),
  EGT_EFFECTIVE_DATE          DATE,
  FEDERAL_REGISTRY_DATE       DATE,
  CURRENT_EGT_FLAG            NUMBER(1)         DEFAULT 0                     NOT NULL,
  DISABLED_EGT_FLAG           NUMBER(1)         DEFAULT 0                     NOT NULL,
  REMARKS                     VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_SETTING_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.EGT_SETTING_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.EGT_SETTING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.EGT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.REFERENCE_EGT_SETTING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.EGT_EFFECTIVE_DATE IS 'Date on which the EGT setting becomes effective. The EGT Effective Date is defined as the Effective Date for this EGT to go into effect. It is referenced by the determine enrollment business rules.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.FEDERAL_REGISTRY_DATE IS 'Federal Register Date holds the date that the EGT change was published in the Federal Register.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.CURRENT_EGT_FLAG IS 'Flag used to indicate that an EGT setting has been disabled. Disabled Indicator is used if this entry was entered in error, this field should be set.  If the EGT that this record represents needs to be disabled for any reason (wrong EGT Effect Date, etc was entered) then the EGT entry needs to be disabled.  This will prevent the processing of all the enrollment records from taking place.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.DISABLED_EGT_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.EGT_SETTING_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_SETTING_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_SETTING_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_SETTING_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EGTSETTINH_PK ON ADR.EGT_SETTING_H
(EGT_SETTING_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EGT_SETTING_H FOR ADR.EGT_SETTING_H;


ALTER TABLE ADR.EGT_SETTING_H ADD (
  CONSTRAINT AVCON_1172521913_CURRE_001
  CHECK (CURRENT_EGT_FLAG BETWEEN 0 AND 0 OR CURRENT_EGT_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT AVCON_1172521913_TRANS_045
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT EGTSETTINH_PK
  PRIMARY KEY
  (EGT_SETTING_H_ID)
  USING INDEX ADR.EGTSETTINH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_SETTING_H TO ADR_R;

GRANT SELECT ON ADR.EGT_SETTING_H TO ADR_RW;

GRANT SELECT ON ADR.EGT_SETTING_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_SETTING_H TO ES_RW;
ALTER TABLE ADR.EGT_SITE_STATISTICS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_SITE_STATISTICS CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_SITE_STATISTICS
(
  EGT_SITE_STATISTICS_ID          NUMBER(20)    NOT NULL,
  EGT_STATISTICS_ID               NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20),
  ENROLLED_CATGRY_COUNT           NUMBER(10),
  IN_PROCESS_CATGRY_COUNT         NUMBER(10),
  BENEFICIARY_PROCESSED_COUNT     NUMBER(10),
  BENEFICIARY_NOT_ENROLLED_COUNT  NUMBER(10),
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_SITE_STATISTICS IS 'Enrollment Global Threshold statistics per site The EGT_SITE_STATISTICS table contains information by site about persons who are not enrolled. EGT_SETTING_ID Foreign key to the EGT_SETTING table. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site to which the statistics apply. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.EGT_SITE_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.EGT_STATISTICS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.ENROLLED_CATGRY_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.BENEFICIARY_PROCESSED_COUNT IS 'BR2326 Total VETS In Process is the total number of veteran records that were set to the Enrollment Category of In Process as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.BENEFICIARY_NOT_ENROLLED_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.EGTSTSTIC_EGTSETTING_FK_I ON ADR.EGT_SITE_STATISTICS
(EGT_STATISTICS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.EGTSTSTIC_PK ON ADR.EGT_SITE_STATISTICS
(EGT_SITE_STATISTICS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EGTSTSTIC_STDINST_FK_I ON ADR.EGT_SITE_STATISTICS
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EGT_SITE_STATISTICS_audir_trgr
after Update or Delete or Insert
ON ADR.EGT_SITE_STATISTICS
for each row
declare
   	mDML char(1);
begin
	if DELETING then
		insert into EGT_SITE_STATISTICS_H
		(
		EGT_SITE_STATISTICS_H_ID,
		EGT_SITE_STATISTICS_ID,
		EGT_STATISTICS_ID,
		STD_INSTITUTION_ID,
		BENEFICIARY_NOT_ENROLLED_COUNT,
		IN_PROCESS_CATGRY_COUNT,
		ENROLLED_CATGRY_COUNT,
		BENEFICIARY_PROCESSED_COUNT,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      		)
		values
		(
		EGT_SITE_STATISTICS_H_s.nextval,
		:old.EGT_SITE_STATISTICS_ID,
		:old.EGT_STATISTICS_ID,
		:old.STD_INSTITUTION_ID,
		:old.BENEFICIARY_NOT_ENROLLED_COUNT,
		:old.IN_PROCESS_CATGRY_COUNT,
		:old.ENROLLED_CATGRY_COUNT,
		:old.BENEFICIARY_PROCESSED_COUNT,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into EGT_SITE_STATISTICS_H
		(
		EGT_SITE_STATISTICS_H_ID,
		EGT_SITE_STATISTICS_ID,
		EGT_STATISTICS_ID,
		STD_INSTITUTION_ID,
		BENEFICIARY_NOT_ENROLLED_COUNT,
		IN_PROCESS_CATGRY_COUNT,
		ENROLLED_CATGRY_COUNT,
		BENEFICIARY_PROCESSED_COUNT,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      		)
		values
		(
		EGT_SITE_STATISTICS_H_s.nextval,
		:new.EGT_SITE_STATISTICS_ID,
		:new.EGT_STATISTICS_ID,
		:new.STD_INSTITUTION_ID,
		:new.BENEFICIARY_NOT_ENROLLED_COUNT,
		:new.IN_PROCESS_CATGRY_COUNT,
		:new.ENROLLED_CATGRY_COUNT,
		:new.BENEFICIARY_PROCESSED_COUNT,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  EGT_SITE_STATISTICS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EGT_SITE_STATISTICS FOR ADR.EGT_SITE_STATISTICS;


ALTER TABLE ADR.EGT_SITE_STATISTICS ADD (
  CONSTRAINT EGTSTSTIC_PK
  PRIMARY KEY
  (EGT_SITE_STATISTICS_ID)
  USING INDEX ADR.EGTSTSTIC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EGT_SITE_STATISTICS ADD (
  CONSTRAINT EGTSTSTIC_EGTSTAT_FK 
  FOREIGN KEY (EGT_STATISTICS_ID) 
  REFERENCES ADR.EGT_STATISTICS (EGT_STATISTICS_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_SITE_STATISTICS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EGT_SITE_STATISTICS TO ADR_RW;

GRANT SELECT ON ADR.EGT_SITE_STATISTICS TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_SITE_STATISTICS TO ES_RW;
ALTER TABLE ADR.EGT_SITE_STATISTICS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_SITE_STATISTICS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_SITE_STATISTICS_H
(
  EGT_SITE_STATISTICS_H_ID        NUMBER(20)    NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  EGT_SITE_STATISTICS_ID          NUMBER(20)    NOT NULL,
  EGT_STATISTICS_ID               NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20),
  IN_PROCESS_CATGRY_COUNT         NUMBER(10),
  BENEFICIARY_PROCESSED_COUNT     NUMBER(10),
  BENEFICIARY_NOT_ENROLLED_COUNT  NUMBER(10),
  ENROLLED_CATGRY_COUNT           NUMBER(10),
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_SITE_STATISTICS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.EGT_SITE_STATISTICS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.EGT_SITE_STATISTICS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.EGT_STATISTICS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.BENEFICIARY_PROCESSED_COUNT IS 'BR2326 Total VETS In Process is the total number of veteran records that were set to the Enrollment Category of In Process as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.BENEFICIARY_NOT_ENROLLED_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.ENROLLED_CATGRY_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_SITE_STATISTICS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EGTSTSTICH_PK ON ADR.EGT_SITE_STATISTICS_H
(EGT_SITE_STATISTICS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EGT_SITE_STATISTICS_H FOR ADR.EGT_SITE_STATISTICS_H;


ALTER TABLE ADR.EGT_SITE_STATISTICS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_051
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT EGTSTSTICH_PK
  PRIMARY KEY
  (EGT_SITE_STATISTICS_H_ID)
  USING INDEX ADR.EGTSTSTICH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_SITE_STATISTICS_H TO ADR_R;

GRANT SELECT ON ADR.EGT_SITE_STATISTICS_H TO ADR_RW;

GRANT SELECT ON ADR.EGT_SITE_STATISTICS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_SITE_STATISTICS_H TO ES_RW;
ALTER TABLE ADR.EGT_STATISTICS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_STATISTICS CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_STATISTICS
(
  EGT_STATISTICS_ID               NUMBER(20)    NOT NULL,
  EGT_SETTING_ID                  NUMBER(20)    NOT NULL,
  EGT_STATUS_TYPE_ID              NUMBER(20),
  PROCESS_NUMBER                  VARCHAR2(20 BYTE),
  PROCESS_MESSAGE_DESCRIPTION     VARCHAR2(250 BYTE),
  PROCESS_START_DATE              DATE,
  PROCESS_END_DATE                DATE,
  IN_PROCESS_CATGRY_COUNT         NUMBER(10),
  ENROLLED_CATGRY_COUNT           NUMBER(10),
  BENEFICIARY_PROCESSED_COUNT     NUMBER(10),
  BENEFICIARY_NOT_ENROLLED_COUNT  NUMBER(10),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_STATISTICS IS 'Enrollment Global Threshold Statistics ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.EGT_STATISTICS.EGT_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.EGT_SETTING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.PROCESS_START_DATE IS 'BR2312 Process Started Date/Time: Holds the date/time that the process to recalculate the enrollment statuses based on the new EGT setting started.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.PROCESS_END_DATE IS 'BR2316 Process Ended Date/Time is the Date/Time that the process to recalculate the enrollment statuses based on the new EGT setting ended.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.ENROLLED_CATGRY_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.BENEFICIARY_PROCESSED_COUNT IS 'BR2326 Total VETS In Process is the total number of veteran records that were set to the Enrollment Category of In Process as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.BENEFICIARY_NOT_ENROLLED_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_STATISTICS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_STATISTICS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_STATISTICS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.EGTSTAT_EGTSETTING_FK_I ON ADR.EGT_STATISTICS
(EGT_SETTING_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.EGTSTAT_PK ON ADR.EGT_STATISTICS
(EGT_STATISTICS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EGTSTAT_STD_EGTPRCST_FK_I ON ADR.EGT_STATISTICS
(EGT_STATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EGT_STATISTICS_audir_trgr
after Update or Delete or Insert
ON ADR.EGT_STATISTICS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into EGT_STATISTICS_H
		(
		EGT_STATISTICS_H_ID,
		EGT_STATISTICS_ID,
		EGT_SETTING_ID,
		PROCESS_START_DATE,
		PROCESS_END_DATE,
		BENEFICIARY_PROCESSED_COUNT,
		BENEFICIARY_NOT_ENROLLED_COUNT,
		PROCESS_NUMBER,
		PROCESS_MESSAGE_DESCRIPTION,
		IN_PROCESS_CATGRY_COUNT,
		ENROLLED_CATGRY_COUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EGT_STATISTICS_H_S.nextval,
		:old.EGT_STATISTICS_ID,
		:old.EGT_SETTING_ID,
		:old.PROCESS_START_DATE,
		:old.PROCESS_END_DATE,
		:old.BENEFICIARY_PROCESSED_COUNT,
		:old.BENEFICIARY_NOT_ENROLLED_COUNT,
		:old.PROCESS_NUMBER,
		:old.PROCESS_MESSAGE_DESCRIPTION,
		:old.IN_PROCESS_CATGRY_COUNT,
		:old.ENROLLED_CATGRY_COUNT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into EGT_STATISTICS_H
		(
		EGT_STATISTICS_H_ID,
		EGT_STATISTICS_ID,
		EGT_SETTING_ID,
		PROCESS_START_DATE,
		PROCESS_END_DATE,
		BENEFICIARY_PROCESSED_COUNT,
		BENEFICIARY_NOT_ENROLLED_COUNT,
		PROCESS_NUMBER,
		PROCESS_MESSAGE_DESCRIPTION,
		IN_PROCESS_CATGRY_COUNT,
		ENROLLED_CATGRY_COUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EGT_STATISTICS_H_S.nextval,
		:new.EGT_STATISTICS_ID,
		:new.EGT_SETTING_ID,
		:new.PROCESS_START_DATE,
		:new.PROCESS_END_DATE,
		:new.BENEFICIARY_PROCESSED_COUNT,
		:new.BENEFICIARY_NOT_ENROLLED_COUNT,
		:new.PROCESS_NUMBER,
		:new.PROCESS_MESSAGE_DESCRIPTION,
		:new.IN_PROCESS_CATGRY_COUNT,
		:new.ENROLLED_CATGRY_COUNT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end EGT_STATISTICS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EGT_STATISTICS FOR ADR.EGT_STATISTICS;


ALTER TABLE ADR.EGT_STATISTICS ADD (
  CONSTRAINT EGTSTAT_PK
  PRIMARY KEY
  (EGT_STATISTICS_ID)
  USING INDEX ADR.EGTSTAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EGT_STATISTICS ADD (
  CONSTRAINT EGTSTAT_EGTSETTING_FK 
  FOREIGN KEY (EGT_SETTING_ID) 
  REFERENCES ADR.EGT_SETTING (EGT_SETTING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_STATISTICS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EGT_STATISTICS TO ADR_RW;

GRANT SELECT ON ADR.EGT_STATISTICS TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_STATISTICS TO ES_RW;
ALTER TABLE ADR.EGT_STATISTICS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EGT_STATISTICS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EGT_STATISTICS_H
(
  EGT_STATISTICS_H_ID             NUMBER(20)    NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  EGT_STATISTICS_ID               NUMBER(20)    NOT NULL,
  EGT_SETTING_ID                  NUMBER(20)    NOT NULL,
  EGT_STATUS_TYPE_ID              NUMBER(20),
  PROCESS_NUMBER                  VARCHAR2(20 BYTE),
  PROCESS_MESSAGE_DESCRIPTION     VARCHAR2(250 BYTE),
  PROCESS_START_DATE              DATE,
  PROCESS_END_DATE                DATE,
  IN_PROCESS_CATGRY_COUNT         NUMBER(10),
  ENROLLED_CATGRY_COUNT           NUMBER(10),
  BENEFICIARY_PROCESSED_COUNT     NUMBER(10),
  BENEFICIARY_NOT_ENROLLED_COUNT  NUMBER(10),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EGT_STATISTICS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.EGT_STATISTICS_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.EGT_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.EGT_SETTING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.PROCESS_START_DATE IS 'BR2312 Process Started Date/Time: Holds the date/time that the process to recalculate the enrollment statuses based on the new EGT setting started.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.PROCESS_END_DATE IS 'BR2316 Process Ended Date/Time is the Date/Time that the process to recalculate the enrollment statuses based on the new EGT setting ended.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.ENROLLED_CATGRY_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.BENEFICIARY_PROCESSED_COUNT IS 'BR2326 Total VETS In Process is the total number of veteran records that were set to the Enrollment Category of In Process as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.BENEFICIARY_NOT_ENROLLED_COUNT IS 'BR2324 Total VETS Not Enrolled is the total number of veteran records that were set to the Enrollment Category of NOT ENROLLED as a result of the recalculation of enrollment statuses based on a change to the EGT settings.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EGT_STATISTICS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EGTSTATH_PK ON ADR.EGT_STATISTICS_H
(EGT_STATISTICS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EGT_STATISTICS_H FOR ADR.EGT_STATISTICS_H;


ALTER TABLE ADR.EGT_STATISTICS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_013
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT EGTSTATH_PK
  PRIMARY KEY
  (EGT_STATISTICS_H_ID)
  USING INDEX ADR.EGTSTATH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EGT_STATISTICS_H TO ADR_R;

GRANT SELECT ON ADR.EGT_STATISTICS_H TO ADR_RW;

GRANT SELECT ON ADR.EGT_STATISTICS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EGT_STATISTICS_H TO ES_RW;
ALTER TABLE ADR.ELIGIBILITY_DETERMINATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ELIGIBILITY_DETERMINATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.ELIGIBILITY_DETERMINATION
(
  ELIGIBILITY_DETERMINATION_ID  NUMBER(20)      NOT NULL,
  ENROLLMENT_DETERMINATION_ID   NUMBER(20)      NOT NULL,
  ELIGIBILITY_TYPE_ID           NUMBER(20),
  STD_ELIGIBILITYFACTOR_ID      NUMBER(20),
  PRIMARY_SECONDARY_OR_OTHER    VARCHAR2(1 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ELIGIBILITY_DETERMINATION IS 'The ELIGIBILITY_DETERMINATION table contains information about the eligibility codes applicable to a persons enrollment in the VHA Healthcare system. ENROLLMENT_DETERMINATION_ID Foreign key to the ENROLLMENT_DETERMINATION table. ELIGIBILITY_TYPE_ID Foreign key to the ELIGIBILITY_TYPE table. Indicates the eligibility codes associated with a particular person (for example, Service Connected 50% to 100%, Purple Heart). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.ELIGIBILITY_DETERMINATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.ENROLLMENT_DETERMINATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.ELIGIBILITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.STD_ELIGIBILITYFACTOR_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.PRIMARY_SECONDARY_OR_OTHER IS 'Indicates whether an eligibility is a primary, secondary, or other eligibility.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ELGBDETER_ENRLMNTDET_FK_I ON ADR.ELIGIBILITY_DETERMINATION
(ENROLLMENT_DETERMINATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ELGBDETER_PK ON ADR.ELIGIBILITY_DETERMINATION
(ELIGIBILITY_DETERMINATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBDETER_STDELGFCTR_FK_I ON ADR.ELIGIBILITY_DETERMINATION
(STD_ELIGIBILITYFACTOR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBDETER_STD_ELGBTYCODE_FK_I ON ADR.ELIGIBILITY_DETERMINATION
(ELIGIBILITY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENROLLMENT_DETERM_MOD_DT_NDX ON ADR.ELIGIBILITY_DETERMINATION
(RECORD_MODIFIED_DATE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ELIGIBILITY_DET_audir_trgr
after Update or Delete or Insert
ON ADR.ELIGIBILITY_DETERMINATION
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ELIGIBILITY_DETERMINATION_H
		(
		ELIGIBILITY_DETERMINATION_H_ID,
		ELIGIBILITY_DETERMINATION_ID  ,
		ENROLLMENT_DETERMINATION_ID   ,
		ELIGIBILITY_TYPE_ID           ,
		PRIMARY_SECONDARY_OR_OTHER    ,
		STD_ELIGIBILITYFACTOR_ID	  ,
		RECORD_CREATED_BY             ,
		RECORD_CREATED_DATE           ,
		RECORD_MODIFIED_BY            ,
		RECORD_MODIFIED_DATE          ,
		RECORD_MODIFIED_COUNT         ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		ELIGIBILITY_DETERMINATION_H_s.nextval,
		:old.ELIGIBILITY_DETERMINATION_ID,
		:old.ENROLLMENT_DETERMINATION_ID,
		:old.ELIGIBILITY_TYPE_ID,
		:old.PRIMARY_SECONDARY_OR_OTHER,
		:old.STD_ELIGIBILITYFACTOR_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into ELIGIBILITY_DETERMINATION_H
		(
		ELIGIBILITY_DETERMINATION_H_ID,
		ELIGIBILITY_DETERMINATION_ID  ,
		ENROLLMENT_DETERMINATION_ID   ,
		ELIGIBILITY_TYPE_ID           ,
		PRIMARY_SECONDARY_OR_OTHER    ,
		STD_ELIGIBILITYFACTOR_ID,
		RECORD_CREATED_BY             ,
		RECORD_CREATED_DATE           ,
		RECORD_MODIFIED_BY            ,
		RECORD_MODIFIED_DATE          ,
		RECORD_MODIFIED_COUNT         ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		ELIGIBILITY_DETERMINATION_H_s.nextval,
		:new.ELIGIBILITY_DETERMINATION_ID,
		:new.ENROLLMENT_DETERMINATION_ID,
		:new.ELIGIBILITY_TYPE_ID,
		:new.PRIMARY_SECONDARY_OR_OTHER,
		:new.STD_ELIGIBILITYFACTOR_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  ELIGIBILITY_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ELIGIBILITY_DETERMINATION FOR ADR.ELIGIBILITY_DETERMINATION;


ALTER TABLE ADR.ELIGIBILITY_DETERMINATION ADD (
  CONSTRAINT AVCON_1172521913_PRIMA_001
  CHECK (PRIMARY_SECONDARY_OR_OTHER IN ('P', 'S', 'O'))
  ENABLE VALIDATE,
  CONSTRAINT ELGBDETER_PK
  PRIMARY KEY
  (ELIGIBILITY_DETERMINATION_ID)
  USING INDEX ADR.ELGBDETER_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ELIGIBILITY_DETERMINATION ADD (
  CONSTRAINT ELGBDETER_ENRLMNTDET_FK 
  FOREIGN KEY (ENROLLMENT_DETERMINATION_ID) 
  REFERENCES ADR.ENROLLMENT_DETERMINATION (ENROLLMENT_DETERMINATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ELIGIBILITY_DETERMINATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ELIGIBILITY_DETERMINATION TO ADR_RW;

GRANT SELECT ON ADR.ELIGIBILITY_DETERMINATION TO ES_R;

GRANT INSERT, SELECT ON ADR.ELIGIBILITY_DETERMINATION TO ES_RW;
ALTER TABLE ADR.ELIGIBILITY_DETERMINATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ELIGIBILITY_DETERMINATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ELIGIBILITY_DETERMINATION_H
(
  ELIGIBILITY_DETERMINATION_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  ELIGIBILITY_DETERMINATION_ID    NUMBER(20),
  ENROLLMENT_DETERMINATION_ID     NUMBER(20),
  ELIGIBILITY_TYPE_ID             NUMBER(20),
  STD_ELIGIBILITYFACTOR_ID        NUMBER(20),
  PRIMARY_SECONDARY_OR_OTHER      VARCHAR2(1 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ELIGIBILITY_DETERMINATION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.ELIGIBILITY_DETERMINATION_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.ELIGIBILITY_DETERMINATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.ENROLLMENT_DETERMINATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.ELIGIBILITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.STD_ELIGIBILITYFACTOR_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.PRIMARY_SECONDARY_OR_OTHER IS 'Indicates whether an eligibility is a primary, secondary, or other eligibility.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ELIGIBILITY_DETERMINATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ELGBDETERH_ENRLMNTDET_NU_I ON ADR.ELIGIBILITY_DETERMINATION_H
(ENROLLMENT_DETERMINATION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ELGBDETERH_PK ON ADR.ELIGIBILITY_DETERMINATION_H
(ELIGIBILITY_DETERMINATION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENROLLMENT_DETERM_H_MOD_DT_NDX ON ADR.ELIGIBILITY_DETERMINATION_H
(RECORD_MODIFIED_DATE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ELIGIBILITY_DETERMINATION_H FOR ADR.ELIGIBILITY_DETERMINATION_H;


ALTER TABLE ADR.ELIGIBILITY_DETERMINATION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_075
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067559
  CHECK ("ELIGIBILITY_DETERMINATION_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067560
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067561
  CHECK ("ELIGIBILITY_DETERMINATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067562
  CHECK ("ENROLLMENT_DETERMINATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067563
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067564
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067565
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067566
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067567
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ELGBDETERH_PK
  PRIMARY KEY
  (ELIGIBILITY_DETERMINATION_H_ID)
  USING INDEX ADR.ELGBDETERH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ELIGIBILITY_DETERMINATION_H TO ADR_R;

GRANT SELECT ON ADR.ELIGIBILITY_DETERMINATION_H TO ADR_RW;

GRANT SELECT ON ADR.ELIGIBILITY_DETERMINATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ELIGIBILITY_DETERMINATION_H TO ES_RW;
ALTER TABLE ADR.ELIGIBILITY_STATUS_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ELIGIBILITY_STATUS_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.ELIGIBILITY_STATUS_REASON
(
  ELIGIBILITY_STATUS_REASON_ID  NUMBER(20)      NOT NULL,
  CODE                          VARCHAR2(2 BYTE) NOT NULL,
  NAME                          VARCHAR2(50 BYTE) NOT NULL,
  DESCRIPTION                   VARCHAR2(100 BYTE),
  ISACTIVE                      CHAR(1 BYTE)    NOT NULL,
  INACTIVE_DATE                 DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
CACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ELIGIBILITY_STATUS_REASON IS 'Lookup table to hold eligibility status reason.';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.ELIGIBILITY_STATUS_REASON_ID IS 'The¿¿primary¿¿key, generated¿¿by¿¿the¿¿database system¿¿in¿¿numeric¿¿form, used¿¿to¿¿uniquely¿¿identify a¿¿data¿¿record¿¿in¿¿this table.';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ELIGIBILITY_STATUS_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ELGBTY_STATUS_REASON_CODE_UK ON ADR.ELIGIBILITY_STATUS_REASON
(CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ELGBTY_STATUS_REASON_NM_UK ON ADR.ELIGIBILITY_STATUS_REASON
(NAME)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ELIGIBILITY_STATUS_REASON_PK ON ADR.ELIGIBILITY_STATUS_REASON
(ELIGIBILITY_STATUS_REASON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ELIGIBILITY_STATUS_REASON FOR ADR.ELIGIBILITY_STATUS_REASON;


ALTER TABLE ADR.ELIGIBILITY_STATUS_REASON ADD (
  CONSTRAINT ELIGIBILITY_STATUS_REASON_PK
  PRIMARY KEY
  (ELIGIBILITY_STATUS_REASON_ID)
  USING INDEX ADR.ELIGIBILITY_STATUS_REASON_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ELIGIBILITY_STATUS_REASON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ELIGIBILITY_STATUS_REASON TO ADR_RW;

GRANT SELECT ON ADR.ELIGIBILITY_STATUS_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.ELIGIBILITY_STATUS_REASON TO ES_RW;
ALTER TABLE ADR.ELIGIBILITY_VERIFICATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ELIGIBILITY_VERIFICATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.ELIGIBILITY_VERIFICATION
(
  ELIGIBILITY_VERIFICATION_ID  NUMBER(20),
  PERSON_ID                    NUMBER(20),
  ELGBTY_STATUS_TYPE_ID        NUMBER(20),
  ELGBTY_VRFN_SOURCE_TYPE_ID   NUMBER(20),
  VBA_QUERY_STATUS_TYPE_ID     NUMBER(20),
  VBA_QUERY_STATUS_DATE        DATE,
  VBA_QUERY_SENT_DATE          DATE,
  STD_INSTITUTION_ID           NUMBER(20),
  VERIFICATION_METHOD_CODE     VARCHAR2(50 BYTE),
  ELIGIBILITY_STATUS_DATE_TXT  VARCHAR2(14 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  ELGBTY_STATUS_REASON_ID      NUMBER(20),
  ELGBTY_REASON_EXPLANATION    VARCHAR2(255 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ELIGIBILITY_VERIFICATION IS 'The table stores data about a persons eligibility status, including what method was used to verify the eligibility, what source was used to verify the eligibility (i.e. HINQ/VBA, Veteran Family Member, Other Source, Master Veteran Record, VAMC, or VIVA file from Austin) and at what site the verification was performed. PERSON_ID Foreign key to the PERSON table. ELGBTY_STATUS_TYPE_ID Foreign key to the ELGBTY_STATUS_TYPE table. Indicates the status of the verification (for example, Pending Verification, Verified). ELGBTY_VRFN_SOURCE_TYPE_ID Foreign key to the ELGBTY_VRFN_SOURCE_TYPE. Indicates the source from which verification data was received (for example, VBA, VistA). AAC_STATUS_TYPE_ID Foreign key to the AAC_STATUS_TYPE table. Indicates the status of the verification query to AAC (for example, Data Pending Review, AAC Data Rejected). STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site that initiated the verification. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.ELIGIBILITY_VERIFICATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.ELGBTY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.ELGBTY_VRFN_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.VBA_QUERY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.VBA_QUERY_STATUS_DATE IS 'Provides a migration target for DATE LAST UPDATED AT AAC (300.12/31)';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.VBA_QUERY_SENT_DATE IS 'Provides a migration target for Date query sent to AAC (300.12/33)';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.VERIFICATION_METHOD_CODE IS 'Code that describes method used to verify eligibility.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ELGBTYVRFY_PK ON ADR.ELIGIBILITY_VERIFICATION
(ELIGIBILITY_VERIFICATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFY_PRSN_FK_I ON ADR.ELIGIBILITY_VERIFICATION
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFY_STDINST_FK_I ON ADR.ELIGIBILITY_VERIFICATION
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFY_STD_ELGBTST_FK_I ON ADR.ELIGIBILITY_VERIFICATION
(ELGBTY_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFY_STD_ELGVRSRC_FK_I ON ADR.ELIGIBILITY_VERIFICATION
(ELGBTY_VRFN_SOURCE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFY_STD_VBAQURYST_FK_I ON ADR.ELIGIBILITY_VERIFICATION
(VBA_QUERY_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ELIGIBILITY_VER_AUDIR_TRGR
   AFTER UPDATE OR DELETE OR INSERT
   ON ADR.ELIGIBILITY_VERIFICATION
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      INSERT INTO ELIGIBILITY_VERIFICATION_H (ELIGIBILITY_VERIFICATION_H_ID,
                                              ELIGIBILITY_VERIFICATION_ID,
                                              PERSON_ID,
                                              ELGBTY_STATUS_TYPE_ID,
                                              ELGBTY_VRFN_SOURCE_TYPE_ID,
                                              VBA_QUERY_STATUS_TYPE_ID,
                                              STD_INSTITUTION_ID,
                                              VERIFICATION_METHOD_CODE,
                                              ELIGIBILITY_STATUS_DATE_TXT,
                                              VBA_QUERY_STATUS_DATE,
                                              VBA_QUERY_SENT_DATE,
                                              elgbty_Status_reason_id,
                                              elgbty_reason_explanation,
                                              RECORD_CREATED_BY,
                                              RECORD_CREATED_DATE,
                                              RECORD_MODIFIED_BY,
                                              RECORD_MODIFIED_DATE,
                                              RECORD_MODIFIED_COUNT,
                                              TRANSACTION_TYPE_CODE)
           VALUES (ELIGIBILITY_VERIFICATION_H_s.NEXTVAL,
                   :old.ELIGIBILITY_VERIFICATION_ID,
                   :old.PERSON_ID,
                   :old.ELGBTY_STATUS_TYPE_ID,
                   :old.ELGBTY_VRFN_SOURCE_TYPE_ID,
                   :old.VBA_QUERY_STATUS_TYPE_ID,
                   :old.STD_INSTITUTION_ID,
                   :old.VERIFICATION_METHOD_CODE,
                   :old.ELIGIBILITY_STATUS_DATE_TXT,
                   :old.VBA_QUERY_STATUS_DATE,
                   :old.VBA_QUERY_SENT_DATE,
                   :old.elgbty_Status_reason_id,
                   :old.elgbty_reason_explanation,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,
                   'D');
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO ELIGIBILITY_VERIFICATION_H (ELIGIBILITY_VERIFICATION_H_ID,
                                              ELIGIBILITY_VERIFICATION_ID,
                                              PERSON_ID,
                                              ELGBTY_STATUS_TYPE_ID,
                                              ELGBTY_VRFN_SOURCE_TYPE_ID,
                                              VBA_QUERY_STATUS_TYPE_ID,
                                              STD_INSTITUTION_ID,
                                              VERIFICATION_METHOD_CODE,
                                              ELIGIBILITY_STATUS_DATE_TXT,
                                              VBA_QUERY_STATUS_DATE,
                                              VBA_QUERY_SENT_DATE,
                                              elgbty_Status_reason_id,
                                              elgbty_reason_explanation,
                                              RECORD_CREATED_BY,
                                              RECORD_CREATED_DATE,
                                              RECORD_MODIFIED_BY,
                                              RECORD_MODIFIED_DATE,
                                              RECORD_MODIFIED_COUNT,
                                              TRANSACTION_TYPE_CODE)
           VALUES (ELIGIBILITY_VERIFICATION_H_s.NEXTVAL,
                   :new.ELIGIBILITY_VERIFICATION_ID,
                   :new.PERSON_ID,
                   :new.ELGBTY_STATUS_TYPE_ID,
                   :new.ELGBTY_VRFN_SOURCE_TYPE_ID,
                   :new.VBA_QUERY_STATUS_TYPE_ID,
                   :new.STD_INSTITUTION_ID,
                   :new.VERIFICATION_METHOD_CODE,
                   :new.ELIGIBILITY_STATUS_DATE_TXT,
                   :new.VBA_QUERY_STATUS_DATE,
                   :new.VBA_QUERY_SENT_DATE,
                   :new.elgbty_Status_reason_id,
                   :new.elgbty_reason_explanation,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,
                   mDML);
   END IF;
END ELIGIBILITY_VER_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ELIGIBILITY_VERIFICATION FOR ADR.ELIGIBILITY_VERIFICATION;


ALTER TABLE ADR.ELIGIBILITY_VERIFICATION ADD (
  CONSTRAINT SYS_C0067367
  CHECK ("ELIGIBILITY_VERIFICATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067368
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067369
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067370
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067371
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067372
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067373
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ELGBTYVRFY_PK
  PRIMARY KEY
  (ELIGIBILITY_VERIFICATION_ID)
  USING INDEX ADR.ELGBTYVRFY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ELIGIBILITY_VERIFICATION ADD (
  CONSTRAINT ELGBTYVERIFI_REASONID_FK 
  FOREIGN KEY (ELGBTY_STATUS_REASON_ID) 
  REFERENCES ADR.ELIGIBILITY_STATUS_REASON (ELIGIBILITY_STATUS_REASON_ID)
  ENABLE VALIDATE,
  CONSTRAINT ELGBTYVRFY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION TO ADREXT;

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ELIGIBILITY_VERIFICATION TO ADR_RW;

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION TO ES_R;

GRANT INSERT, SELECT ON ADR.ELIGIBILITY_VERIFICATION TO ES_RW;
ALTER TABLE ADR.ELIGIBILITY_VERIFICATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ELIGIBILITY_VERIFICATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ELIGIBILITY_VERIFICATION_H
(
  ELIGIBILITY_VERIFICATION_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  ELIGIBILITY_VERIFICATION_ID    NUMBER(20),
  PERSON_ID                      NUMBER(20),
  ELGBTY_STATUS_TYPE_ID          NUMBER(20),
  ELGBTY_VRFN_SOURCE_TYPE_ID     NUMBER(20),
  VBA_QUERY_STATUS_TYPE_ID       NUMBER(20),
  VBA_QUERY_STATUS_DATE          DATE,
  VBA_QUERY_SENT_DATE            DATE,
  STD_INSTITUTION_ID             NUMBER(20),
  VERIFICATION_METHOD_CODE       VARCHAR2(50 BYTE),
  ELIGIBILITY_STATUS_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  ELGBTY_STATUS_REASON_ID        NUMBER(20),
  ELGBTY_REASON_EXPLANATION      VARCHAR2(255 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ELIGIBILITY_VERIFICATION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.ELIGIBILITY_VERIFICATION_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.ELIGIBILITY_VERIFICATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.ELGBTY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.ELGBTY_VRFN_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.VBA_QUERY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.VBA_QUERY_STATUS_DATE IS 'Provides a migration target for DATE LAST UPDATED AT AAC (300.12/31)';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.VBA_QUERY_SENT_DATE IS 'Provides a migration target for Date query sent to AAC (300.12/33)';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.VERIFICATION_METHOD_CODE IS 'Code that describes method used to verify eligibility.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ELIGIBILITY_VERIFICATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ELGBTYVRFH_PK ON ADR.ELIGIBILITY_VERIFICATION_H
(ELIGIBILITY_VERIFICATION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ELGBTYVRFH_PRSNID_NU_I ON ADR.ELIGIBILITY_VERIFICATION_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ELIGIBILITY_VERIFICATION_H FOR ADR.ELIGIBILITY_VERIFICATION_H;


ALTER TABLE ADR.ELIGIBILITY_VERIFICATION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_031
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066537
  CHECK ("ELIGIBILITY_VERIFICATION_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066538
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066539
  CHECK ("ELIGIBILITY_VERIFICATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066540
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066541
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066542
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066543
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066544
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066545
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ELGBTYVRFH_PK
  PRIMARY KEY
  (ELIGIBILITY_VERIFICATION_H_ID)
  USING INDEX ADR.ELGBTYVRFH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION_H TO ADR_R;

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION_H TO ADR_RW;

GRANT SELECT ON ADR.ELIGIBILITY_VERIFICATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ELIGIBILITY_VERIFICATION_H TO ES_RW;
ALTER TABLE ADR.EMAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMAIL
(
  EMAIL_ID                     NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20),
  INSURANCE_ID                 NUMBER(20),
  EMAIL_TYPE_ID                NUMBER(20),
  EMAIL_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  EMAIL_ADDRESS                VARCHAR2(128 BYTE) NOT NULL,
  CHANGE_EFFECTIVE_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMAIL IS 'The table contains email address(es) for a Person and also for an Insurance company Maintains all types of email addresses for Persons. Also stores Insurance Company email address. PERSON_ID Foreign key to the PERSON table. INSURANCE_ID Foreign key to the INSURANCE table. EMAIL_TYPE_ID Foreign key to the EMAIL_TYPE table. Indicates the type of email address (for example, Private, Business). DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.EMAIL.EMAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMAIL.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL.EMAIL_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL.EMAIL_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL.EMAIL_ADDRESS IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of email address went into effect.';

COMMENT ON COLUMN ADR.EMAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.EMAIL.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE INDEX ADR.EMAIL_INSURANCE_FK_I ON ADR.EMAIL
(INSURANCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.EMAIL_PK ON ADR.EMAIL
(EMAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_PRSN_FK_I ON ADR.EMAIL
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_STDINST2_FK_I ON ADR.EMAIL
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_STDINST_FK_I ON ADR.EMAIL
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_STD_EMAILADDRTYP_FK_I ON ADR.EMAIL
(EMAIL_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAIL_STD_EMAILCHGSRC_FK_I ON ADR.EMAIL
(EMAIL_CHANGE_SOURCE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EMAIL_audir_trgr
after Update or Delete or Insert
ON ADR.EMAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into EMAIL_H
		(
		EMAIL_H_ID,
		EMAIL_ID,
		PERSON_ID,
		INSURANCE_ID,
		EMAIL_TYPE_ID,
		EMAIL_ADDRESS,
		CHANGE_EFFECTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		STD_INSTITUTION_ID,
		EMAIL_CHANGE_SOURCE_TYPE_ID,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EMAIL_H_S.nextval,
		:old.EMAIL_ID,
		:old.PERSON_ID,
		:old.INSURANCE_ID,
		:old.EMAIL_TYPE_ID,
		:old.EMAIL_ADDRESS,
		:old.CHANGE_EFFECTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		:old.STD_INSTITUTION_ID,
		:old.EMAIL_CHANGE_SOURCE_TYPE_ID,
		:old.RECORD_MODIFIED_SITE_ID,
		:old.RECORD_MODIFIED_SRC_NAME,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into EMAIL_H
		(
		EMAIL_H_ID,
		EMAIL_ID,
		PERSON_ID,
		INSURANCE_ID,
		EMAIL_TYPE_ID,
		EMAIL_ADDRESS,
		CHANGE_EFFECTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		STD_INSTITUTION_ID,
		EMAIL_CHANGE_SOURCE_TYPE_ID,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EMAIL_H_S.nextval,
		:new.EMAIL_ID,
		:new.PERSON_ID,
		:new.INSURANCE_ID,
		:new.EMAIL_TYPE_ID,
		:new.EMAIL_ADDRESS,
		:new.CHANGE_EFFECTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		:new.STD_INSTITUTION_ID,
		:new.EMAIL_CHANGE_SOURCE_TYPE_ID,
		:new.RECORD_MODIFIED_SITE_ID,
		:new.RECORD_MODIFIED_SRC_NAME,
		mDML
		);
	end if;

end EMAIL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EMAIL FOR ADR.EMAIL;


ALTER TABLE ADR.EMAIL ADD (
  CONSTRAINT EMAIL_PK
  PRIMARY KEY
  (EMAIL_ID)
  USING INDEX ADR.EMAIL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EMAIL ADD (
  CONSTRAINT EMAIL_INSURANCE_FK 
  FOREIGN KEY (INSURANCE_ID) 
  REFERENCES ADR.INSURANCE (INSURANCE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT EMAIL_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.EMAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EMAIL TO ADR_RW;

GRANT SELECT ON ADR.EMAIL TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.EMAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.EMAIL TO ES_RW;

GRANT SELECT ON ADR.EMAIL TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.EMAIL TO PSIM_ADR_RW;
ALTER TABLE ADR.EMAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMAIL_H
(
  EMAIL_H_ID                   NUMBER(20),
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  EMAIL_ID                     NUMBER(20),
  PERSON_ID                    NUMBER(20),
  INSURANCE_ID                 NUMBER(20),
  EMAIL_TYPE_ID                NUMBER(20),
  EMAIL_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  EMAIL_ADDRESS                VARCHAR2(128 BYTE),
  CHANGE_EFFECTIVE_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EMAIL_H.EMAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EMAIL_H.EMAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMAIL_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMAIL_H.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL_H.EMAIL_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL_H.EMAIL_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.EMAIL_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.EMAIL_H.EMAIL_ADDRESS IS 'The email address for the person or insurance company.';

COMMENT ON COLUMN ADR.EMAIL_H.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of email address went into effect.';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.EMAIL_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.EMAILH_PK ON ADR.EMAIL_H
(EMAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMAILH_PRSNID_NU_I ON ADR.EMAIL_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EMAIL_H FOR ADR.EMAIL_H;


ALTER TABLE ADR.EMAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_076
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067583
  CHECK ("EMAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067584
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067585
  CHECK ("EMAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067586
  CHECK ("EMAIL_ADDRESS" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067587
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067588
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067589
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067590
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067591
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT EMAILH_PK
  PRIMARY KEY
  (EMAIL_H_ID)
  USING INDEX ADR.EMAILH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EMAIL_H TO ADR_R;

GRANT SELECT ON ADR.EMAIL_H TO ADR_RW;

GRANT SELECT ON ADR.EMAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EMAIL_H TO ES_RW;

GRANT SELECT ON ADR.EMAIL_H TO PSIM_ADR_R;
ALTER TABLE ADR.EMERGENCY_RESPONSE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMERGENCY_RESPONSE CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMERGENCY_RESPONSE
(
  EMERGENCY_RESPONSE_ID     NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  STD_EMERGENCYRESPONSE_ID  NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMERGENCY_RESPONSE IS 'This feature instituted in the aftermath of Hurricane Katrina. Table lists details of any emergency response assistance provided to the Beneficiary.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.EMERGENCY_RESPONSE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.STD_EMERGENCYRESPONSE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EMERGRESP_PK ON ADR.EMERGENCY_RESPONSE
(EMERGENCY_RESPONSE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMERGRESP_PRSN_FK_I ON ADR.EMERGENCY_RESPONSE
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMERGRESP_STDEMRGNCY_FK_I ON ADR.EMERGENCY_RESPONSE
(STD_EMERGENCYRESPONSE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EMERG_RESPONSE_AUDIR_TRGR
after Update or Delete or Insert
on ADR.EMERGENCY_RESPONSE
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into EMERGENCY_RESPONSE_H
		(
		EMERGENCY_RESPONSE_H_ID,
 		EMERGENCY_RESPONSE_ID,
		PERSON_ID,
		STD_EMERGENCYRESPONSE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EMERGENCY_RESPONSE_H_S.nextval,
 		:old.EMERGENCY_RESPONSE_ID,
		:old.PERSON_ID,
		:old.STD_EMERGENCYRESPONSE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into EMERGENCY_RESPONSE_H
		(
		EMERGENCY_RESPONSE_H_ID,
 		EMERGENCY_RESPONSE_ID,
		PERSON_ID,
		STD_EMERGENCYRESPONSE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		EMERGENCY_RESPONSE_H_S.nextval,
 		:new.EMERGENCY_RESPONSE_ID,
		:new.PERSON_ID,
		:new.STD_EMERGENCYRESPONSE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end emerg_response_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EMERGENCY_RESPONSE FOR ADR.EMERGENCY_RESPONSE;


ALTER TABLE ADR.EMERGENCY_RESPONSE ADD (
  CONSTRAINT EMERGRESP_PK
  PRIMARY KEY
  (EMERGENCY_RESPONSE_ID)
  USING INDEX ADR.EMERGRESP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EMERGENCY_RESPONSE ADD (
  CONSTRAINT EMERGRESP_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.EMERGENCY_RESPONSE TO ADREXT;

GRANT SELECT ON ADR.EMERGENCY_RESPONSE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EMERGENCY_RESPONSE TO ADR_RW;

GRANT SELECT ON ADR.EMERGENCY_RESPONSE TO ES_R;

GRANT INSERT, SELECT ON ADR.EMERGENCY_RESPONSE TO ES_RW;
ALTER TABLE ADR.EMERGENCY_RESPONSE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMERGENCY_RESPONSE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMERGENCY_RESPONSE_H
(
  EMERGENCY_RESPONSE_H_ID   NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  EMERGENCY_RESPONSE_ID     NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  STD_EMERGENCYRESPONSE_ID  NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMERGENCY_RESPONSE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.EMERGENCY_RESPONSE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.EMERGENCY_RESPONSE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.STD_EMERGENCYRESPONSE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMERGENCY_RESPONSE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EMERGRESH_PK ON ADR.EMERGENCY_RESPONSE_H
(EMERGENCY_RESPONSE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMERGRESH_PRSNID_NU_I ON ADR.EMERGENCY_RESPONSE_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EMERGENCY_RESPONSE_H FOR ADR.EMERGENCY_RESPONSE_H;


ALTER TABLE ADR.EMERGENCY_RESPONSE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_025
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT EMERGRESH_PK
  PRIMARY KEY
  (EMERGENCY_RESPONSE_H_ID)
  USING INDEX ADR.EMERGRESH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EMERGENCY_RESPONSE_H TO ADR_R;

GRANT SELECT ON ADR.EMERGENCY_RESPONSE_H TO ADR_RW;

GRANT SELECT ON ADR.EMERGENCY_RESPONSE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EMERGENCY_RESPONSE_H TO ES_RW;
ALTER TABLE ADR.EMPLOYMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMPLOYMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMPLOYMENT
(
  EMPLOYMENT_ID                 NUMBER(20),
  PERSON_ID                     NUMBER(20),
  PERSON_RELATION_ID            NUMBER(20),
  STD_EMPLOYMENTSTATUS_ID       NUMBER(20),
  EMPLOYEE_OCCUPATION           VARCHAR2(50 BYTE),
  EMPLOYER_FULL_NAME            VARCHAR2(100 BYTE),
  PHONE_NUMBER                  VARCHAR2(128 BYTE),
  ADDRESS_LINE1                 VARCHAR2(100 BYTE),
  ADDRESS_LINE2                 VARCHAR2(100 BYTE),
  ADDRESS_LINE3                 VARCHAR2(100 BYTE),
  CITY                          VARCHAR2(60 BYTE),
  STATE_CODE                    VARCHAR2(50 BYTE),
  COUNTY_CODE                   VARCHAR2(50 BYTE),
  PROVINCE_CODE                 VARCHAR2(50 BYTE),
  ZIP_CODE                      VARCHAR2(20 BYTE),
  ZIP_PLUS_4                    VARCHAR2(10 BYTE),
  POSTAL_CODE                   VARCHAR2(20 BYTE),
  COUNTRY_CODE                  VARCHAR2(50 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10),
  EMPLOYEE_RETIREMENT_DATE_TXT  VARCHAR2(14 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMPLOYMENT IS 'Stores Employer name and address, and also the Employee details such as Employment status and occupation';

COMMENT ON COLUMN ADR.EMPLOYMENT.EMPLOYMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMPLOYMENT.STD_EMPLOYMENTSTATUS_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.EMPLOYMENT.EMPLOYEE_OCCUPATION IS 'Full Name';

COMMENT ON COLUMN ADR.EMPLOYMENT.EMPLOYER_FULL_NAME IS 'Full Name';

COMMENT ON COLUMN ADR.EMPLOYMENT.ADDRESS_LINE1 IS 'The free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT.ADDRESS_LINE2 IS 'The free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT.ADDRESS_LINE3 IS 'The free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT.CITY IS 'The name of a city used for an address.';

COMMENT ON COLUMN ADR.EMPLOYMENT.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5-character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied.
  2  State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.EMPLOYMENT.COUNTY_CODE IS 'FIPS County CODE is defined as the last 3 characters of the FIPS state/county code.  The FIPS county code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied
  2  5-DIGIT FIPS CODE is defined as the 2 digit state code + 3 digit county code. This 5 digit FIPS code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied';

COMMENT ON COLUMN ADR.EMPLOYMENT.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.
  2  Acceptable format is free Text 5 - 10 characters.';

COMMENT ON COLUMN ADR.EMPLOYMENT.POSTAL_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.
  2  Acceptable format is free Text 5 - 10 characters.';

COMMENT ON COLUMN ADR.EMPLOYMENT.COUNTRY_CODE IS 'Country is defined as the unique identifier assigned by the International Organization of Standardization for a country.';

COMMENT ON COLUMN ADR.EMPLOYMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMPLOYMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMPLOYMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMPLOYMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMPLOYMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.EMPLOY_EMPTSTAT_FK_I ON ADR.EMPLOYMENT
(STD_EMPLOYMENTSTATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.EMPLOY_PK ON ADR.EMPLOYMENT
(EMPLOYMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMPLOY_PRSNREL_FK_I ON ADR.EMPLOYMENT
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMPLOY_PRSN_FK_I ON ADR.EMPLOYMENT
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.EMPLOYMENT_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."EMPLOYMENT" FOR EACH ROW
declare
    mDML char(1);
begin

  if DELETING then
    insert into EMPLOYMENT_H
    (
    EMPLOYMENT_H_ID,
    EMPLOYMENT_ID,
    PERSON_ID,
    PERSON_RELATION_ID,
    STD_EMPLOYMENTSTATUS_ID,
    EMPLOYEE_OCCUPATION,
    EMPLOYER_FULL_NAME,
    PHONE_NUMBER,
    ADDRESS_LINE1,
    ADDRESS_LINE2,
    ADDRESS_LINE3,
    CITY,
    STATE_CODE,
    COUNTY_CODE,
    PROVINCE_CODE,
    ZIP_CODE,
    ZIP_PLUS_4,
    POSTAL_CODE,
    COUNTRY_CODE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    EMPLOYEE_RETIREMENT_DATE_TXT,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    EMPLOYMENT_h_s.nextval,
    :old.EMPLOYMENT_ID ,
    :old.PERSON_ID,
    :old.PERSON_RELATION_ID,
    :old.STD_EMPLOYMENTSTATUS_ID,
    :old.EMPLOYEE_OCCUPATION,
    :old.EMPLOYER_FULL_NAME,
    :old.PHONE_NUMBER,
    :old.ADDRESS_LINE1,
    :old.ADDRESS_LINE2,
    :old.ADDRESS_LINE3,
    :old.CITY,
    :old.STATE_CODE,
    :old.COUNTY_CODE,
    :old.PROVINCE_CODE,
    :old.ZIP_CODE,
    :old.ZIP_PLUS_4,
    :old.POSTAL_CODE,
    :old.COUNTRY_CODE,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY ,
    :old.RECORD_MODIFIED_DATE,
    :old.RECORD_MODIFIED_COUNT,
    :old.EMPLOYEE_RETIREMENT_DATE_TXT,
    'D'
    );


  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;
    insert into EMPLOYMENT_H
    (
    EMPLOYMENT_H_ID,
    EMPLOYMENT_ID,
    PERSON_ID,
    PERSON_RELATION_ID,
    STD_EMPLOYMENTSTATUS_ID,
    EMPLOYEE_OCCUPATION,
    EMPLOYER_FULL_NAME,
    PHONE_NUMBER,
    ADDRESS_LINE1,
    ADDRESS_LINE2,
    ADDRESS_LINE3,
    CITY,
    STATE_CODE,
    COUNTY_CODE,
    PROVINCE_CODE,
    ZIP_CODE,
    ZIP_PLUS_4,
    POSTAL_CODE,
    COUNTRY_CODE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    EMPLOYEE_RETIREMENT_DATE_TXT,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    EMPLOYMENT_h_s.nextval,
    :new.EMPLOYMENT_ID ,
    :new.PERSON_ID,
    :new.PERSON_RELATION_ID,
    :new.STD_EMPLOYMENTSTATUS_ID,
    :new.EMPLOYEE_OCCUPATION,
    :new.EMPLOYER_FULL_NAME,
    :new.PHONE_NUMBER,
    :new.ADDRESS_LINE1,
    :new.ADDRESS_LINE2,
    :new.ADDRESS_LINE3,
    :new.CITY,
    :new.STATE_CODE,
    :new.COUNTY_CODE,
    :new.PROVINCE_CODE,
    :new.ZIP_CODE,
    :new.ZIP_PLUS_4,
    :new.POSTAL_CODE,
    :new.COUNTRY_CODE,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY ,
    :new.RECORD_MODIFIED_DATE,
    :new.RECORD_MODIFIED_COUNT,
    :new.EMPLOYEE_RETIREMENT_DATE_TXT,
    mDML
    );



  end if;

end EMPLOYMENT_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM EMPLOYMENT FOR ADR.EMPLOYMENT;


ALTER TABLE ADR.EMPLOYMENT ADD (
  CONSTRAINT SYS_C00106532
  CHECK ("EMPLOYMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106534
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106535
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106536
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106537
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106538
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT EMPLOY_PK
  PRIMARY KEY
  (EMPLOYMENT_ID)
  USING INDEX ADR.EMPLOY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.EMPLOYMENT ADD (
  CONSTRAINT EMPLOY_PRSNREL_FK 
  FOREIGN KEY (PERSON_RELATION_ID) 
  REFERENCES ADR.PERSON_RELATION (PERSON_RELATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT EMPLOY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.EMPLOYMENT TO ADREXT;

GRANT SELECT ON ADR.EMPLOYMENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EMPLOYMENT TO ADR_RW;

GRANT SELECT ON ADR.EMPLOYMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.EMPLOYMENT TO ES_RW;
ALTER TABLE ADR.EMPLOYMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.EMPLOYMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.EMPLOYMENT_H
(
  EMPLOYMENT_H_ID               NUMBER(20),
  EMPLOYMENT_ID                 NUMBER(20),
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE),
  PERSON_ID                     NUMBER(20),
  PERSON_RELATION_ID            NUMBER(20),
  STD_EMPLOYMENTSTATUS_ID       NUMBER(20),
  EMPLOYEE_OCCUPATION           VARCHAR2(50 BYTE),
  EMPLOYER_FULL_NAME            VARCHAR2(100 BYTE),
  PHONE_NUMBER                  VARCHAR2(128 BYTE),
  ADDRESS_LINE1                 VARCHAR2(100 BYTE),
  ADDRESS_LINE2                 VARCHAR2(100 BYTE),
  ADDRESS_LINE3                 VARCHAR2(100 BYTE),
  CITY                          VARCHAR2(60 BYTE),
  STATE_CODE                    VARCHAR2(50 BYTE),
  COUNTY_CODE                   VARCHAR2(50 BYTE),
  PROVINCE_CODE                 VARCHAR2(50 BYTE),
  ZIP_CODE                      VARCHAR2(20 BYTE),
  ZIP_PLUS_4                    VARCHAR2(10 BYTE),
  POSTAL_CODE                   VARCHAR2(20 BYTE),
  COUNTRY_CODE                  VARCHAR2(50 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10),
  EMPLOYEE_RETIREMENT_DATE_TXT  VARCHAR2(14 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.EMPLOYMENT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.EMPLOYMENT_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.EMPLOYEE_OCCUPATION IS 'Full Name';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.EMPLOYER_FULL_NAME IS 'Full Name';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.ADDRESS_LINE1 IS 'The free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.ADDRESS_LINE2 IS 'The free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.ADDRESS_LINE3 IS 'The free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.CITY IS 'The name of a city used for an address.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5-character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied.
  2  State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.COUNTY_CODE IS 'FIPS County CODE is defined as the last 3 characters of the FIPS state/county code.  The FIPS county code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied
  2  5-DIGIT FIPS CODE is defined as the 2 digit state code + 3 digit county code. This 5 digit FIPS code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.
  2  Acceptable format is free Text 5 - 10 characters.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.POSTAL_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.
  2  Acceptable format is free Text 5 - 10 characters.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.COUNTRY_CODE IS 'Country is defined as the unique identifier assigned by the International Organization of Standardization for a country.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.EMPLOYMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.EMPLOYH_PK ON ADR.EMPLOYMENT_H
(EMPLOYMENT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMPLOY_H_PRSNREL_FK_I ON ADR.EMPLOYMENT_H
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.EMPLOY_H_PRSN_FK_I ON ADR.EMPLOYMENT_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM EMPLOYMENT_H FOR ADR.EMPLOYMENT_H;


ALTER TABLE ADR.EMPLOYMENT_H ADD (
  CONSTRAINT EMPLOYMENT_H_CHK
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106539
  CHECK ("EMPLOYMENT_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106540
  CHECK ("EMPLOYMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106541
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106542
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106543
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106544
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106545
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00106546
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT EMPLOYH_PK
  PRIMARY KEY
  (EMPLOYMENT_H_ID)
  USING INDEX ADR.EMPLOYH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.EMPLOYMENT_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.EMPLOYMENT_H TO ADR_RW;

GRANT SELECT ON ADR.EMPLOYMENT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.EMPLOYMENT_H TO ES_RW;
ALTER TABLE ADR.ENROLLMENT_DETERMINATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ENROLLMENT_DETERMINATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.ENROLLMENT_DETERMINATION
(
  ENROLLMENT_DETERMINATION_ID  NUMBER(20),
  PERSON_ID                    NUMBER(20),
  EGT_SETTING_ID               NUMBER(20),
  STD_ENROLLMENTSTATUS_ID      NUMBER(20),
  ENROLLMENT_DATA_SRC_TYPE_ID  NUMBER(20),
  PRIORITY_GROUP_TYPE_ID       NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID   NUMBER(20),
  APPLICATION_SITE_ID          NUMBER(20),
  ENROLLMENT_APPLICATION_DATE  DATE,
  ENROLLMENT_START_DATE        DATE,
  ENROLLMENT_END_DATE          DATE,
  CHANGE_EFFECTIVE_DATE        DATE,
  ENROLLMENT_OVERRIDDEN_FLAG   NUMBER(1)        DEFAULT 0,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  STD_ENROLLMENTPRIORGRP_ID    NUMBER(20),
  STD_ENROLLMENTPRIORSGRP_ID   NUMBER(20),
  CLOSE_APL_RSN_TYPE_ID        NUMBER(20),
  CLOSE_APP_REMARKS            VARCHAR2(4000 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ENROLLMENT_DETERMINATION IS 'The table stores data about a persons enrollment into the VHA Healthcare System, including information about when the application was submitted, when the enrollment started and ended, what priority group the person was in. There is also status and change information, as well as an indicator to show if the enrollment has been overridden. PERSON_ID Foreign key to the PERSON table. ENROLLMENT_STATUS_TYPE_ID Foreign key to the ENROLLMENT_STATUS_TYPE table. Indicates the status of a persons enrollment (for example, Verified, Rejected). ENROLLMENT_DATA_SRC_TYPE_ID Foreign key to the ENROLLMENT_DATA_SRC_TYPE table. Indicates the source of data for a persons enrollment (for example, HEC, VAMC). PRIORITY_GROUP_TYPE_ID Foreign key to the PRIORITY_GROUP_TYPE table. Indicates the Enrollment Priority Group the person falls into (Group 1 through Group 8). PRIORITY_SUB_GROUP_TYPE_ID Foreign key to the PRIORITY_SUB_GROUP_TYPE table. Indicates the Enrollment Priority Group the person falls into (for example, a, c). APPLICATION_SITE_ID Foreign key to the STD_INSTITUTION table. Indicates the site at which the application was initiated. EGT_SETTING_ID Foreign key to the EGT_SETTING table. Indicates the current EGT setting. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_DETERMINATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.EGT_SETTING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_DATA_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.APPLICATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_APPLICATION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_START_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of email address went into effect.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.ENROLLMENT_OVERRIDDEN_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION.CLOSE_APP_REMARKS IS 'Text including any additional information for the action on close application';


CREATE INDEX ADR.ENRLMNTDET_EGTSETTING_FK_I ON ADR.ENROLLMENT_DETERMINATION
(EGT_SETTING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ENRLMNTDET_PK ON ADR.ENROLLMENT_DETERMINATION
(ENROLLMENT_DETERMINATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_PRSN_FK_I ON ADR.ENROLLMENT_DETERMINATION
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_STDINST_FK_I ON ADR.ENROLLMENT_DETERMINATION
(APPLICATION_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_STD_ENRLPGRP_FK_I ON ADR.ENROLLMENT_DETERMINATION
(PRIORITY_GROUP_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_STD_ENRLPSGRP_FK_I ON ADR.ENROLLMENT_DETERMINATION
(PRIORITY_SUB_GROUP_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_STD_ENRLSRC_FK_I ON ADR.ENROLLMENT_DETERMINATION
(ENROLLMENT_DATA_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDET_STD_ENRLSTAT_FK_I ON ADR.ENROLLMENT_DETERMINATION
(STD_ENROLLMENTSTATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ENROLLMENT_DETER_AUDIR_TRGR
    AFTER UPDATE OR DELETE OR INSERT
    ON ADR.ENROLLMENT_DETERMINATION
    FOR EACH ROW
DECLARE
    Mdml   CHAR (1);
BEGIN
    IF DELETING
    THEN
        INSERT INTO ADR.Enrollment_Determination_H (Enrollment_determination_h_id,
                                                    Enrollment_determination_id,
                                                    Person_id,
                                                    Std_enrollmentstatus_id,
                                                    Enrollment_data_src_type_id,
                                                    Priority_group_type_id,
                                                    Priority_sub_group_type_id,
                                                    Application_site_id,
                                                    Egt_setting_id,
                                                    Enrollment_application_date,
                                                    Enrollment_start_date,
                                                    Enrollment_end_date,
                                                    Change_effective_date,
                                                    Enrollment_overridden_flag,
                                                    Record_created_by,
                                                    Record_created_date,
                                                    Record_modified_by,
                                                    Record_modified_date,
                                                    Record_modified_count,
                                                    Transaction_type_code,
                                                    Std_enrollmentpriorgrp_id,
                                                    Std_enrollmentpriorsgrp_id,
                                                    CLOSE_APL_RSN_TYPE_ID,
                                                         CLOSE_APP_REMARKS
)
             VALUES (ADR.Enrollment_determination_h_s.NEXTVAL,
                     :OLD.Enrollment_determination_id,
                     :OLD.Person_id,
                     :OLD.Std_enrollmentstatus_id,
                     :OLD.Enrollment_data_src_type_id,
                     :OLD.Priority_group_type_id,
                     :OLD.Priority_sub_group_type_id,
                     :OLD.Application_site_id,
                     :OLD.Egt_setting_id,
                     :OLD.Enrollment_application_date,
                     :OLD.Enrollment_start_date,
                     :OLD.Enrollment_end_date,
                     :OLD.Change_effective_date,
                     :OLD.Enrollment_overridden_flag,
                     :OLD.Record_created_by,
                     :OLD.Record_created_date,
                     :OLD.Record_modified_by,
                     :OLD.Record_modified_date,
                     :OLD.Record_modified_count,
                     'D',
                     :OLD.Std_enrollmentpriorgrp_id,
                     :OLD.Std_enrollmentpriorsgrp_id,
                     :OLD.CLOSE_APL_RSN_TYPE_ID,
                     :OLD.CLOSE_APP_REMARKS);
    ELSE
        IF UPDATING
        THEN
            Mdml := 'U';
        ELSE
            Mdml := 'I';
        END IF;

        INSERT INTO ADR.Enrollment_Determination_H (Enrollment_determination_h_id,
                                                    Enrollment_determination_id,
                                                    Person_id,
                                                    Std_enrollmentstatus_id,
                                                    Enrollment_data_src_type_id,
                                                    Priority_group_type_id,
                                                    Priority_sub_group_type_id,
                                                    Application_site_id,
                                                    Egt_setting_id,
                                                    Enrollment_application_date,
                                                    Enrollment_start_date,
                                                    Enrollment_end_date,
                                                    Change_effective_date,
                                                    Enrollment_overridden_flag,
                                                    Record_created_by,
                                                    Record_created_date,
                                                    Record_modified_by,
                                                    Record_modified_date,
                                                    Record_modified_count,
                                                    Transaction_type_code,
                                                    Std_enrollmentpriorgrp_id,
                                                    Std_enrollmentpriorsgrp_id,
                                                    CLOSE_APL_RSN_TYPE_ID,
                                                         CLOSE_APP_REMARKS)
             VALUES (ADR.Enrollment_determination_h_s.NEXTVAL,
                     :NEW.Enrollment_determination_id,
                     :NEW.Person_id,
                     :NEW.Std_enrollmentstatus_id,
                     :NEW.Enrollment_data_src_type_id,
                     :NEW.Priority_group_type_id,
                     :NEW.Priority_sub_group_type_id,
                     :NEW.Application_site_id,
                     :NEW.Egt_setting_id,
                     :NEW.Enrollment_application_date,
                     :NEW.Enrollment_start_date,
                     :NEW.Enrollment_end_date,
                     :NEW.Change_effective_date,
                     :NEW.Enrollment_overridden_flag,
                     :NEW.Record_created_by,
                     :NEW.Record_created_date,
                     :NEW.Record_modified_by,
                     :NEW.Record_modified_date,
                     :NEW.Record_modified_count,
                     Mdml,
                     :NEW.Std_enrollmentpriorgrp_id,
                     :NEW.Std_enrollmentpriorsgrp_id,
                     :NEW.CLOSE_APL_RSN_TYPE_ID,
                     :NEW.CLOSE_APP_REMARKS);
    END IF;
END ENROLLMENT_DETER_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM ENROLLMENT_DETERMINATION FOR ADR.ENROLLMENT_DETERMINATION;


ALTER TABLE ADR.ENROLLMENT_DETERMINATION ADD (
  CONSTRAINT AVCON_1172521913_ENROL_000
  CHECK (ENROLLMENT_OVERRIDDEN_FLAG BETWEEN 0
AND 0 OR ENROLLMENT_OVERRIDDEN_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067374
  CHECK ("ENROLLMENT_DETERMINATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067375
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067376
  CHECK ("EGT_SETTING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067377
  CHECK ("ENROLLMENT_OVERRIDDEN_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067378
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067379
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067380
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067381
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067382
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ENRLMNTDET_PK
  PRIMARY KEY
  (ENROLLMENT_DETERMINATION_ID)
  USING INDEX ADR.ENRLMNTDET_PK
  ENABLE VALIDATE,
  CONSTRAINT PERSON_ID_UK
  UNIQUE (PERSON_ID)
  USING INDEX ADR.ENRLMNTDET_PRSN_FK_I
  ENABLE NOVALIDATE);

ALTER TABLE ADR.ENROLLMENT_DETERMINATION ADD (
  CONSTRAINT CLOSE_APL_RSN_TYPE_ID_FK 
  FOREIGN KEY (CLOSE_APL_RSN_TYPE_ID) 
  REFERENCES ADR.CLOSE_APL_RSN_TYPE (CLOSE_APL_RSN_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT ENRLMNTDET_EGTSETTING_FK 
  FOREIGN KEY (EGT_SETTING_ID) 
  REFERENCES ADR.EGT_SETTING (EGT_SETTING_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT ENRLMNTDET_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO ADREXT;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO ADRTMP;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ENROLLMENT_DETERMINATION TO ADR_RW;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO BIZFLOW_R;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO ES_R;

GRANT INSERT, SELECT ON ADR.ENROLLMENT_DETERMINATION TO ES_RW;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION TO PSIM;
ALTER TABLE ADR.ENROLLMENT_DETERMINATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ENROLLMENT_DETERMINATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ENROLLMENT_DETERMINATION_H
(
  ENROLLMENT_DETERMINATION_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  ENROLLMENT_DETERMINATION_ID    NUMBER(20),
  PERSON_ID                      NUMBER(20),
  STD_ENROLLMENTSTATUS_ID        NUMBER(20),
  ENROLLMENT_DATA_SRC_TYPE_ID    NUMBER(20),
  PRIORITY_GROUP_TYPE_ID         NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID     NUMBER(20),
  APPLICATION_SITE_ID            NUMBER(20),
  EGT_SETTING_ID                 NUMBER(20),
  ENROLLMENT_APPLICATION_DATE    DATE,
  ENROLLMENT_START_DATE          DATE,
  ENROLLMENT_END_DATE            DATE,
  CHANGE_EFFECTIVE_DATE          DATE,
  ENROLLMENT_OVERRIDDEN_FLAG     NUMBER(1)      DEFAULT 0,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  STD_ENROLLMENTPRIORGRP_ID      NUMBER(20),
  STD_ENROLLMENTPRIORSGRP_ID     NUMBER(20),
  CLOSE_APL_RSN_TYPE_ID          NUMBER(20),
  CLOSE_APP_REMARKS              VARCHAR2(4000 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ENROLLMENT_DETERMINATION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_DETERMINATION_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_DETERMINATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_DATA_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.APPLICATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.EGT_SETTING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_APPLICATION_DATE IS 'BR2260 Enrollment Application Date is defined as the date of the application for enrollment.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_START_DATE IS 'BR2240 Enrollment Date is defined as he starting date for this enrollment.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_END_DATE IS 'BR7771 The enrollment end date is defined as the end date of this persons enrollment.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of email address went into effect.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.ENROLLMENT_OVERRIDDEN_FLAG IS 'BR7458 Enrollment Override Flag is defined as a flag that when set will initiate the re- calculation of a veterans enrollment priority.  It is used in those cases where a veteran may have been placed in a Rejected; Stop New Enrollments OR Rejected; Below Enrollment Group Threshold Enrollment statuses. ';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.ENROLLMENT_DETERMINATION_H.CLOSE_APP_REMARKS IS ' Text including any additional information for the action on close application';


CREATE INDEX ADR.ENRLMNTDEH_ENRLMNTDET_NU_I ON ADR.ENROLLMENT_DETERMINATION_H
(ENROLLMENT_DETERMINATION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ENRLMNTDEH_PK ON ADR.ENROLLMENT_DETERMINATION_H
(ENROLLMENT_DETERMINATION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENRLMNTDEH_PRSNID_NU_I ON ADR.ENROLLMENT_DETERMINATION_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ENROLLMENT_DETERMINATION_H FOR ADR.ENROLLMENT_DETERMINATION_H;


ALTER TABLE ADR.ENROLLMENT_DETERMINATION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_054
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066981
  CHECK ("ENROLLMENT_DETERMINATION_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066982
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066983
  CHECK ("ENROLLMENT_DETERMINATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066984
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066985
  CHECK ("EGT_SETTING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066986
  CHECK ("ENROLLMENT_OVERRIDDEN_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066987
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066988
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066989
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066990
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066991
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT ENRLMNTDEH_PK
  PRIMARY KEY
  (ENROLLMENT_DETERMINATION_H_ID)
  USING INDEX ADR.ENRLMNTDEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION_H TO ADREXT;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION_H TO ADR_R;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION_H TO ADR_RW;

GRANT SELECT ON ADR.ENROLLMENT_DETERMINATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ENROLLMENT_DETERMINATION_H TO ES_RW;
ALTER TABLE ADR.ENROLLMENT_OVERRIDE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ENROLLMENT_OVERRIDE CASCADE CONSTRAINTS;

CREATE TABLE ADR.ENROLLMENT_OVERRIDE
(
  ENROLLMENT_OVERRIDE_ID        NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  OVERRIDE_FLAG                 NUMBER(1)       DEFAULT 0,
  OVERRIDE_DATE                 DATE,
  STD_ENROLLMENTOVERRIDERSN_ID  NUMBER(20),
  REMARKS                       VARCHAR2(80 BYTE),
  OVERRIDE_CREATED_USER         VARCHAR2(30 BYTE),
  OVERRIDE_CREATED_DATE         DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ENROLLMENT_OVERRIDE IS 'The table contains a set of attributes stored in the database for the override instruction. The override instruction documents whether or not to override a persons enrollment. PERSON_ID Foreign key to the PERSON table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.ENROLLMENT_OVERRIDE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.OVERRIDE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.OVERRIDE_DATE IS 'Date on which a persons enrollment was overridden.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.OVERRIDE_CREATED_USER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.OVERRIDE_CREATED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ENROLOVER_PK ON ADR.ENROLLMENT_OVERRIDE
(ENROLLMENT_OVERRIDE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENROLOVER_PRSN_FK_I ON ADR.ENROLLMENT_OVERRIDE
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENROLOVER_STDENROVRD_FK_I ON ADR.ENROLLMENT_OVERRIDE
(STD_ENROLLMENTOVERRIDERSN_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ENROLLMENT_OVER_AUDIR_TRGR
after Update or Delete or Insert
on ADR.ENROLLMENT_OVERRIDE
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ENROLLMENT_OVERRIDE_H
		(
		ENROLLMENT_OVERRIDE_H_ID,
		ENROLLMENT_OVERRIDE_ID,
		PERSON_ID,
		OVERRIDE_FLAG,
		OVERRIDE_DATE,
		STD_ENROLLMENTOVERRIDERSN_ID,
		REMARKS,
		OVERRIDE_CREATED_USER,
		OVERRIDE_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		ENROLLMENT_OVERRIDE_H_S.nextval,
		:old.ENROLLMENT_OVERRIDE_ID,
		:old.PERSON_ID,
		:old.OVERRIDE_FLAG,
		:old.OVERRIDE_DATE,
		:old.STD_ENROLLMENTOVERRIDERSN_ID,
		:old.REMARKS,
		:old.OVERRIDE_CREATED_USER,
		:old.OVERRIDE_CREATED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into ENROLLMENT_OVERRIDE_H
		(
		ENROLLMENT_OVERRIDE_H_ID,
		ENROLLMENT_OVERRIDE_ID,
		PERSON_ID,
		OVERRIDE_FLAG,
		OVERRIDE_DATE,
		STD_ENROLLMENTOVERRIDERSN_ID,
		REMARKS,
		OVERRIDE_CREATED_USER,
		OVERRIDE_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		ENROLLMENT_OVERRIDE_H_S.nextval,
		:new.ENROLLMENT_OVERRIDE_ID,
		:new.PERSON_ID,
		:new.OVERRIDE_FLAG,
		:new.OVERRIDE_DATE,
		:new.STD_ENROLLMENTOVERRIDERSN_ID,
		:new.REMARKS,
		:new.OVERRIDE_CREATED_USER,
		:new.OVERRIDE_CREATED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  ENROLLMENT_OVER_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ENROLLMENT_OVERRIDE FOR ADR.ENROLLMENT_OVERRIDE;


ALTER TABLE ADR.ENROLLMENT_OVERRIDE ADD (
  CONSTRAINT AVCON_1172521913_OVERR_000
  CHECK (OVERRIDE_FLAG BETWEEN 0 AND 0 OR OVERRIDE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT ENROLOVER_PK
  PRIMARY KEY
  (ENROLLMENT_OVERRIDE_ID)
  USING INDEX ADR.ENROLOVER_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ENROLLMENT_OVERRIDE ADD (
  CONSTRAINT ENROLOVER_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE TO ADREXT;

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ENROLLMENT_OVERRIDE TO ADR_RW;

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE TO ES_R;

GRANT INSERT, SELECT ON ADR.ENROLLMENT_OVERRIDE TO ES_RW;
ALTER TABLE ADR.ENROLLMENT_OVERRIDE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ENROLLMENT_OVERRIDE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ENROLLMENT_OVERRIDE_H
(
  ENROLLMENT_OVERRIDE_H_ID      NUMBER(20)      NOT NULL,
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE) NOT NULL,
  ENROLLMENT_OVERRIDE_ID        NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  STD_ENROLLMENTOVERRIDERSN_ID  NUMBER(20),
  OVERRIDE_FLAG                 NUMBER(1)       DEFAULT 0,
  OVERRIDE_DATE                 DATE,
  REMARKS                       VARCHAR2(80 BYTE),
  OVERRIDE_CREATED_USER         VARCHAR2(30 BYTE),
  OVERRIDE_CREATED_DATE         DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ENROLLMENT_OVERRIDE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.ENROLLMENT_OVERRIDE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.OVERRIDE_FLAG IS 'Flag used to indicate that a persons enrollment has been overridden.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.OVERRIDE_DATE IS 'Date on which a persons enrollment was overridden.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.OVERRIDE_CREATED_USER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.OVERRIDE_CREATED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ENROLLMENT_OVERRIDE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ENROLOVERH_PK ON ADR.ENROLLMENT_OVERRIDE_H
(ENROLLMENT_OVERRIDE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ENROLOVERH_PRSNID_NU_I ON ADR.ENROLLMENT_OVERRIDE_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ENROLLMENT_OVERRIDE_H FOR ADR.ENROLLMENT_OVERRIDE_H;


ALTER TABLE ADR.ENROLLMENT_OVERRIDE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_039
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT ENROLOVERH_PK
  PRIMARY KEY
  (ENROLLMENT_OVERRIDE_H_ID)
  USING INDEX ADR.ENROLOVERH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE_H TO ADR_R;

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE_H TO ADR_RW;

GRANT SELECT ON ADR.ENROLLMENT_OVERRIDE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ENROLLMENT_OVERRIDE_H TO ES_RW;
ALTER TABLE ADR.FEE_BASIS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FEE_BASIS CASCADE CONSTRAINTS;

CREATE TABLE ADR.FEE_BASIS
(
  FEE_BASIS_ID                 NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  FEE_BASIS_PROGRAM_TYPE_ID    NUMBER(20),
  FEE_BASIS_TREATMENT_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  AUTHORIZATION_FROM_DATE      DATE,
  AUTHORIZATION_TO_DATE        DATE,
  FEE_BASIS_REPORT_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FEE_BASIS IS 'The table stores information about persons who use medical services outside the VHA, but paid for by the VHA because it is not feasible economically for care to be provided due to geographical inaccessibility. PERSON_ID Foreign key to the PERSON table. FEE_BASIS_PROGRAM_TYPE_ID Foreign key to the FEE_BASIS_PROGRAM_TYPE table. Indicates the type of fee basis program associated with the person (for example, Pharmacy, Outpatient). FEE_BASIS_TREATMENT_TYPE_ID Foreign key to the FEE_BASIS_TREATMENT_TYPE table. Indicates the applicable type of treatment for the fee basis program (for example, Short Term Fee Status, Home Nursing Services). STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FEE_BASIS.FEE_BASIS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FEE_BASIS.FEE_BASIS_PROGRAM_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.FEE_BASIS_TREATMENT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.FEE_BASIS.AUTHORIZATION_FROM_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.AUTHORIZATION_TO_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.FEE_BASIS_REPORT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FEE_BASIS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FEE_BASIS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FEE_BASIS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FEE_BASIS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FEEBASIS_PK ON ADR.FEE_BASIS
(FEE_BASIS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FEEBASIS_PRSN_FK_I ON ADR.FEE_BASIS
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FEEBASIS_STDINST_FK_I ON ADR.FEE_BASIS
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FEEBSIS_STD_FEEBSISPROG_FK_I ON ADR.FEE_BASIS
(FEE_BASIS_PROGRAM_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FEEBSIS_STD_FEEBSISTRTMT_FK_I ON ADR.FEE_BASIS
(FEE_BASIS_TREATMENT_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FEE_BASIS_audir_trgr
after Update or Delete or Insert
ON ADR.FEE_BASIS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into FEE_BASIS_H
		(
		FEE_BASIS_H_ID,
		FEE_BASIS_ID,
		PERSON_ID,
		FEE_BASIS_PROGRAM_TYPE_ID,
		FEE_BASIS_TREATMENT_TYPE_ID,
		STD_INSTITUTION_ID,
		AUTHORIZATION_FROM_DATE,
		AUTHORIZATION_TO_DATE,
		FEE_BASIS_REPORT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FEE_BASIS_H_S.nextval,
		:old.FEE_BASIS_ID,
		:old.PERSON_ID,
		:old.FEE_BASIS_PROGRAM_TYPE_ID,
		:old.FEE_BASIS_TREATMENT_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.AUTHORIZATION_FROM_DATE,
		:old.AUTHORIZATION_TO_DATE,
		:old.FEE_BASIS_REPORT_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into FEE_BASIS_H
		(
		FEE_BASIS_H_ID,
		FEE_BASIS_ID,
		PERSON_ID,
		FEE_BASIS_PROGRAM_TYPE_ID,
		FEE_BASIS_TREATMENT_TYPE_ID,
		STD_INSTITUTION_ID,
		AUTHORIZATION_FROM_DATE,
		AUTHORIZATION_TO_DATE,
		FEE_BASIS_REPORT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FEE_BASIS_H_S.nextval,
		:new.FEE_BASIS_ID,
		:new.PERSON_ID,
		:new.FEE_BASIS_PROGRAM_TYPE_ID,
		:new.FEE_BASIS_TREATMENT_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.AUTHORIZATION_FROM_DATE,
		:new.AUTHORIZATION_TO_DATE,
		:new.FEE_BASIS_REPORT_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  FEE_BASIS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FEE_BASIS FOR ADR.FEE_BASIS;


ALTER TABLE ADR.FEE_BASIS ADD (
  CONSTRAINT FEEBASIS_PK
  PRIMARY KEY
  (FEE_BASIS_ID)
  USING INDEX ADR.FEEBASIS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FEE_BASIS ADD (
  CONSTRAINT FEEBASIS_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FEE_BASIS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FEE_BASIS TO ADR_RW;

GRANT SELECT ON ADR.FEE_BASIS TO ES_R;

GRANT INSERT, SELECT ON ADR.FEE_BASIS TO ES_RW;
ALTER TABLE ADR.FEE_BASIS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FEE_BASIS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FEE_BASIS_H
(
  FEE_BASIS_H_ID               NUMBER(20),
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  FEE_BASIS_ID                 NUMBER(20),
  PERSON_ID                    NUMBER(20),
  FEE_BASIS_PROGRAM_TYPE_ID    NUMBER(20),
  FEE_BASIS_TREATMENT_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  AUTHORIZATION_FROM_DATE      DATE,
  AUTHORIZATION_TO_DATE        DATE,
  FEE_BASIS_REPORT_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FEE_BASIS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.FEE_BASIS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.FEE_BASIS_PROGRAM_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.FEE_BASIS_TREATMENT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.FEE_BASIS_H.AUTHORIZATION_FROM_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.AUTHORIZATION_TO_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FEE_BASIS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FEE_BASIS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FEE_BASIS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FEEBASISH_PK ON ADR.FEE_BASIS_H
(FEE_BASIS_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FEEBASISH_PRSNID_NU_I ON ADR.FEE_BASIS_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FEE_BASIS_H FOR ADR.FEE_BASIS_H;


ALTER TABLE ADR.FEE_BASIS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_030
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066528
  CHECK ("FEE_BASIS_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066529
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066530
  CHECK ("FEE_BASIS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066531
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066532
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066533
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066534
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066535
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066536
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FEEBASISH_PK
  PRIMARY KEY
  (FEE_BASIS_H_ID)
  USING INDEX ADR.FEEBASISH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FEE_BASIS_H TO ADR_R;

GRANT SELECT ON ADR.FEE_BASIS_H TO ADR_RW;

GRANT SELECT ON ADR.FEE_BASIS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FEE_BASIS_H TO ES_RW;
ALTER TABLE ADR.FINANCIAL_ASSET_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_ASSET_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_ASSET_DETAIL
(
  FINANCIAL_ASSET_DETAIL_ID  NUMBER(20),
  FINANCIAL_STATEMENT_ID     NUMBER(20),
  FINANCIAL_ASSET_TYPE_ID    NUMBER(20),
  GROSS_AMOUNT               NUMBER(10,2),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE       DATE,
  RECORD_MODIFIED_COUNT      NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_ASSET_DETAIL IS 'The FINANCIAL_ASSET_DETAIL table is a child table of the FINANCIAL_STATEMENT table that provides detailed amounts for different types of monetary assets a person has that would have an impact on their eligibility to enroll for VHA medical benefits. FINANCIAL_STATEMENT_ID Foreign key to the FINANCIAL_STATEMENT table. FINANCIAL_ASSET_TYPE_ID Foreign key to the FINANCIAL_ASSET_TYPE table. Indicates the kind of financial asset being documented (for example, Cash, Real Estate). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.FINANCIAL_ASSET_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.FINANCIAL_ASSET_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.GROSS_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.FINASTDTL_FINSTATE_FK_I ON ADR.FINANCIAL_ASSET_DETAIL
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FINASTDTL_PK ON ADR.FINANCIAL_ASSET_DETAIL
(FINANCIAL_ASSET_DETAIL_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINASTDTL_STD_ASSTTYP_FK_I ON ADR.FINANCIAL_ASSET_DETAIL
(FINANCIAL_ASSET_TYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FINAN_ASSET_DET_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."FINANCIAL_ASSET_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into FINANCIAL_ASSET_DETAIL_H
		(
		FINANCIAL_ASSET_DETAIL_H_ID,
		FINANCIAL_ASSET_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_ASSET_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_ASSET_DETAIL_H_S.nextval,
		:old.FINANCIAL_ASSET_DETAIL_ID,
		:old.FINANCIAL_STATEMENT_ID,
		:old.FINANCIAL_ASSET_TYPE_ID,
		:old.GROSS_AMOUNT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into FINANCIAL_ASSET_DETAIL_H
		(
		FINANCIAL_ASSET_DETAIL_H_ID,
		FINANCIAL_ASSET_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_ASSET_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_ASSET_DETAIL_H_S.nextval,
		:new.FINANCIAL_ASSET_DETAIL_ID,
		:new.FINANCIAL_STATEMENT_ID,
		:new.FINANCIAL_ASSET_TYPE_ID,
		:new.GROSS_AMOUNT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  FINAN_ASSET_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_ASSET_DETAIL FOR ADR.FINANCIAL_ASSET_DETAIL;


ALTER TABLE ADR.FINANCIAL_ASSET_DETAIL ADD (
  CONSTRAINT SYS_C0067151
  CHECK ("FINANCIAL_ASSET_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067152
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067153
  CHECK ("FINANCIAL_ASSET_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067154
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067155
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067156
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067157
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067158
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINASTDTL_PK
  PRIMARY KEY
  (FINANCIAL_ASSET_DETAIL_ID)
  USING INDEX ADR.FINASTDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FINANCIAL_ASSET_DETAIL ADD (
  CONSTRAINT FINASTDTL_FINSTATE_FK 
  FOREIGN KEY (FINANCIAL_STATEMENT_ID) 
  REFERENCES ADR.FINANCIAL_STATEMENT (FINANCIAL_STATEMENT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FINANCIAL_ASSET_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FINANCIAL_ASSET_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_ASSET_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_ASSET_DETAIL TO ES_RW;
ALTER TABLE ADR.FINANCIAL_ASSET_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_ASSET_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_ASSET_DETAIL_H
(
  FINANCIAL_ASSET_DETAIL_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  FINANCIAL_ASSET_DETAIL_ID    NUMBER(20),
  FINANCIAL_STATEMENT_ID       NUMBER(20),
  FINANCIAL_ASSET_TYPE_ID      NUMBER(20),
  GROSS_AMOUNT                 NUMBER(10,2),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_ASSET_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.FINANCIAL_ASSET_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.FINANCIAL_ASSET_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.FINANCIAL_ASSET_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.GROSS_AMOUNT IS 'Asset Amount is defined as the gross annual dollar amount associated with the specific asset type.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_ASSET_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FINASTDTLH_PK ON ADR.FINANCIAL_ASSET_DETAIL_H
(FINANCIAL_ASSET_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_ASSET_DETAIL_H FOR ADR.FINANCIAL_ASSET_DETAIL_H;


ALTER TABLE ADR.FINANCIAL_ASSET_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_061
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067192
  CHECK ("FINANCIAL_ASSET_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067193
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067194
  CHECK ("FINANCIAL_ASSET_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067195
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067196
  CHECK ("FINANCIAL_ASSET_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067197
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067198
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067199
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067200
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067201
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINASTDTLH_PK
  PRIMARY KEY
  (FINANCIAL_ASSET_DETAIL_H_ID)
  USING INDEX ADR.FINASTDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FINANCIAL_ASSET_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.FINANCIAL_ASSET_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_ASSET_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_ASSET_DETAIL_H TO ES_RW;
ALTER TABLE ADR.FINANCIAL_DEBT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_DEBT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_DEBT_DETAIL
(
  FINANCIAL_DEBT_DETAIL_ID  NUMBER(20)          NOT NULL,
  FINANCIAL_STATEMENT_ID    NUMBER(20)          NOT NULL,
  GROSS_AMOUNT              NUMBER(10,2),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_DEBT_DETAIL IS 'The table is a child table of the FINANCIAL_STATEMENT table that provides detailed amounts for different types of monetary debts a person is liable for that would have an impact on their eligibility to enroll for VHA medical benefits. FINANCIAL_STATEMENT_ID Foreign key to the FINANCIAL_STATEMENT table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.FINANCIAL_DEBT_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.GROSS_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.FINDBTDTL_FINSTATE_FK_I ON ADR.FINANCIAL_DEBT_DETAIL
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FINDBTDTL_PK ON ADR.FINANCIAL_DEBT_DETAIL
(FINANCIAL_DEBT_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FINAN_DEBT_DET_AUDIR_TRGR
after Update or Delete or Insert
on ADR.FINANCIAL_DEBT_DETAIL
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
	d_prsnid	number	:= 0;
begin

	if DELETING then
		insert into FINANCIAL_DEBT_DETAIL_H
		(
		FINANCIAL_DEBT_DETAIL_H_ID,
		FINANCIAL_DEBT_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_DEBT_DETAIL_H_S.nextval,
		:old.FINANCIAL_DEBT_DETAIL_ID,
		:old.FINANCIAL_STATEMENT_ID,
		:old.GROSS_AMOUNT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
		select
			fs.person_id
		into
			d_prsnid
	  	from
			ADR.FINANCIAL_STATEMENT fs
	 	where 	financial_statement_id = :OLD.financial_statement_id;

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into FINANCIAL_DEBT_DETAIL_H
		(
		FINANCIAL_DEBT_DETAIL_H_ID,
		FINANCIAL_DEBT_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_DEBT_DETAIL_H_S.nextval,
		:new.FINANCIAL_DEBT_DETAIL_ID,
		:new.FINANCIAL_STATEMENT_ID,
		:new.GROSS_AMOUNT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
		select
			fs.person_id
		into
			d_prsnid
	  	from
			ADR.FINANCIAL_STATEMENT fs
	 	where 	financial_statement_id = :NEW.financial_statement_id;

	end if;

end  FINAN_DEBT_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_DEBT_DETAIL FOR ADR.FINANCIAL_DEBT_DETAIL;


ALTER TABLE ADR.FINANCIAL_DEBT_DETAIL ADD (
  CONSTRAINT FINDBTDTL_PK
  PRIMARY KEY
  (FINANCIAL_DEBT_DETAIL_ID)
  USING INDEX ADR.FINDBTDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FINANCIAL_DEBT_DETAIL ADD (
  CONSTRAINT FINDBTDTL_FINSTATE_FK 
  FOREIGN KEY (FINANCIAL_STATEMENT_ID) 
  REFERENCES ADR.FINANCIAL_STATEMENT (FINANCIAL_STATEMENT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL TO ADREXT;

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FINANCIAL_DEBT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_DEBT_DETAIL TO ES_RW;
ALTER TABLE ADR.FINANCIAL_DEBT_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_DEBT_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_DEBT_DETAIL_H
(
  FINANCIAL_DEBT_DETAIL_H_ID  NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  FINANCIAL_DEBT_DETAIL_ID    NUMBER(20)        NOT NULL,
  FINANCIAL_STATEMENT_ID      NUMBER(20)        NOT NULL,
  GROSS_AMOUNT                NUMBER(10,2),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_DEBT_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.FINANCIAL_DEBT_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.FINANCIAL_DEBT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.GROSS_AMOUNT IS 'Asset Amount is defined as the gross annual dollar amount associated with the specific asset type.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_DEBT_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FINDBTDTLH_PK ON ADR.FINANCIAL_DEBT_DETAIL_H
(FINANCIAL_DEBT_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_DEBT_DETAIL_H FOR ADR.FINANCIAL_DEBT_DETAIL_H;


ALTER TABLE ADR.FINANCIAL_DEBT_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_074
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT FINDBTDTLH_PK
  PRIMARY KEY
  (FINANCIAL_DEBT_DETAIL_H_ID)
  USING INDEX ADR.FINDBTDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_DEBT_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_DEBT_DETAIL_H TO ES_RW;
ALTER TABLE ADR.FINANCIAL_EXPENSE_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_EXPENSE_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_EXPENSE_DETAIL
(
  FINANCIAL_EXPENSE_DETAIL_ID  NUMBER(20),
  FINANCIAL_STATEMENT_ID       NUMBER(20),
  FINANCIAL_EXPENSE_TYPE_ID    NUMBER(20),
  GROSS_AMOUNT                 NUMBER(10,2),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_EXPENSE_DETAIL IS 'The table is a child table of the FINANCIAL_STATEMENT table that provides detailed amounts for different types of monetary expenses a person has incurred that would have an impact on their eligibility to enroll for VHA medical benefits. FINANCIAL_STATEMENT_ID Foreign key to the FINANCIAL_STATEMENT table. FINANCIAL_EXPENSE_TYPE_ID Foreign key to the FINANCIAL_EXPENSE_TYPE table. Indicates the kind of financial expense being documented (for example, Adjusted Medical Expenses, Cost of Shelter). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.FINANCIAL_EXPENSE_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.FINANCIAL_EXPENSE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.GROSS_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.FINEXPDTL_FINSTATE_FK_I ON ADR.FINANCIAL_EXPENSE_DETAIL
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FINEXPDTL_PK ON ADR.FINANCIAL_EXPENSE_DETAIL
(FINANCIAL_EXPENSE_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINEXPDTL_STD_EXPNSTYPE_FK_I ON ADR.FINANCIAL_EXPENSE_DETAIL
(FINANCIAL_EXPENSE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FINAN_EXP_DET_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."FINANCIAL_EXPENSE_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into FINANCIAL_EXPENSE_DETAIL_H
		(
		FINANCIAL_EXPENSE_DETAIL_H_ID,
		FINANCIAL_EXPENSE_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_EXPENSE_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_EXPENSE_DETAIL_H_S.nextval,
		:old.FINANCIAL_EXPENSE_DETAIL_ID,
		:old.FINANCIAL_STATEMENT_ID,
		:old.FINANCIAL_EXPENSE_TYPE_ID,
		:old.GROSS_AMOUNT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into FINANCIAL_EXPENSE_DETAIL_H
		(
		FINANCIAL_EXPENSE_DETAIL_H_ID,
		FINANCIAL_EXPENSE_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_EXPENSE_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_EXPENSE_DETAIL_H_S.nextval,
		:new.FINANCIAL_EXPENSE_DETAIL_ID,
		:new.FINANCIAL_STATEMENT_ID,
		:new.FINANCIAL_EXPENSE_TYPE_ID,
		:new.GROSS_AMOUNT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  FINAN_EXP_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_EXPENSE_DETAIL FOR ADR.FINANCIAL_EXPENSE_DETAIL;


ALTER TABLE ADR.FINANCIAL_EXPENSE_DETAIL ADD (
  CONSTRAINT SYS_C0066410
  CHECK ("FINANCIAL_EXPENSE_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066411
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066412
  CHECK ("FINANCIAL_EXPENSE_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066413
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066414
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066415
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066416
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066417
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINEXPDTL_PK
  PRIMARY KEY
  (FINANCIAL_EXPENSE_DETAIL_ID)
  USING INDEX ADR.FINEXPDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FINANCIAL_EXPENSE_DETAIL ADD (
  CONSTRAINT FINEXPDTL_FINSTATE_FK 
  FOREIGN KEY (FINANCIAL_STATEMENT_ID) 
  REFERENCES ADR.FINANCIAL_STATEMENT (FINANCIAL_STATEMENT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FINANCIAL_EXPENSE_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL TO ES_RW;
ALTER TABLE ADR.FINANCIAL_EXPENSE_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_EXPENSE_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_EXPENSE_DETAIL_H
(
  FINANCIAL_EXPENSE_DETAIL_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  FINANCIAL_EXPENSE_DETAIL_ID    NUMBER(20),
  FINANCIAL_STATEMENT_ID         NUMBER(20),
  FINANCIAL_EXPENSE_TYPE_ID      NUMBER(20),
  GROSS_AMOUNT                   NUMBER(10,2),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_EXPENSE_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.FINANCIAL_EXPENSE_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.FINANCIAL_EXPENSE_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.FINANCIAL_EXPENSE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.GROSS_AMOUNT IS 'Asset Amount is defined as the gross annual dollar amount associated with the specific asset type.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_EXPENSE_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FINEXPDTLH_PK ON ADR.FINANCIAL_EXPENSE_DETAIL_H
(FINANCIAL_EXPENSE_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_EXPENSE_DETAIL_H FOR ADR.FINANCIAL_EXPENSE_DETAIL_H;


ALTER TABLE ADR.FINANCIAL_EXPENSE_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_017
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066241
  CHECK ("FINANCIAL_EXPENSE_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066242
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066243
  CHECK ("FINANCIAL_EXPENSE_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066244
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066245
  CHECK ("FINANCIAL_EXPENSE_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066246
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066247
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066248
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066249
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066250
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINEXPDTLH_PK
  PRIMARY KEY
  (FINANCIAL_EXPENSE_DETAIL_H_ID)
  USING INDEX ADR.FINEXPDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_EXPENSE_DETAIL_H TO ES_RW;
ALTER TABLE ADR.FINANCIAL_INCOME_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_INCOME_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_INCOME_DETAIL
(
  FINANCIAL_INCOME_DETAIL_ID  NUMBER(20),
  FINANCIAL_STATEMENT_ID      NUMBER(20),
  FINANCIAL_INCOME_TYPE_ID    NUMBER(20),
  GROSS_AMOUNT                NUMBER(10,2),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE         DATE,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE        DATE,
  RECORD_MODIFIED_COUNT       NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_INCOME_DETAIL IS 'The table is a child table of the FINANCIAL_STATEMENT table that provides detailed amounts for different types of monetary income a person earns that would have an impact on their eligibility to enroll for VHA medical benefits. FINANCIAL_STATEMENT_ID Foreign key to the FINANCIAL_STATEMENT table. FINANCIAL_INCOME_TYPE_ID Foreign key to the FINANCIAL_INCOME_TYPE table. Indicates the kind of financial income being documented (for example, Social Security, Military Retirement). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.FINANCIAL_INCOME_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.FINANCIAL_INCOME_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.GROSS_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.FININCDTL_STD_INCMTYP_FK_I ON ADR.FINANCIAL_INCOME_DETAIL
(FINANCIAL_INCOME_TYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FININCMDTL_FINSTATE_FK_I ON ADR.FINANCIAL_INCOME_DETAIL
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FININCMDTL_PK ON ADR.FINANCIAL_INCOME_DETAIL
(FINANCIAL_INCOME_DETAIL_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FINAN_INC_DET_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."FINANCIAL_INCOME_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into FINANCIAL_INCOME_DETAIL_H
		(
		FINANCIAL_INCOME_DETAIL_H_ID,
		FINANCIAL_INCOME_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_INCOME_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_INCOME_DETAIL_H_S.nextval,
		:old.FINANCIAL_INCOME_DETAIL_ID,
		:old.FINANCIAL_STATEMENT_ID,
		:old.FINANCIAL_INCOME_TYPE_ID,
		:old.GROSS_AMOUNT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into FINANCIAL_INCOME_DETAIL_H
		(
		FINANCIAL_INCOME_DETAIL_H_ID,
		FINANCIAL_INCOME_DETAIL_ID,
		FINANCIAL_STATEMENT_ID,
		FINANCIAL_INCOME_TYPE_ID,
		GROSS_AMOUNT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		FINANCIAL_INCOME_DETAIL_H_S.nextval,
		:new.FINANCIAL_INCOME_DETAIL_ID,
		:new.FINANCIAL_STATEMENT_ID,
		:new.FINANCIAL_INCOME_TYPE_ID,
		:new.GROSS_AMOUNT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  FINAN_INC_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_INCOME_DETAIL FOR ADR.FINANCIAL_INCOME_DETAIL;


ALTER TABLE ADR.FINANCIAL_INCOME_DETAIL ADD (
  CONSTRAINT SYS_C0067437
  CHECK ("FINANCIAL_INCOME_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067438
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067439
  CHECK ("FINANCIAL_INCOME_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067440
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067441
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067442
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067443
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067444
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FININCMDTL_PK
  PRIMARY KEY
  (FINANCIAL_INCOME_DETAIL_ID)
  USING INDEX ADR.FININCMDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FINANCIAL_INCOME_DETAIL ADD (
  CONSTRAINT FININCMDTL_FINSTATE_FK 
  FOREIGN KEY (FINANCIAL_STATEMENT_ID) 
  REFERENCES ADR.FINANCIAL_STATEMENT (FINANCIAL_STATEMENT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FINANCIAL_INCOME_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FINANCIAL_INCOME_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_INCOME_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_INCOME_DETAIL TO ES_RW;
ALTER TABLE ADR.FINANCIAL_INCOME_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_INCOME_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_INCOME_DETAIL_H
(
  FINANCIAL_INCOME_DETAIL_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE),
  FINANCIAL_INCOME_DETAIL_ID    NUMBER(20),
  FINANCIAL_STATEMENT_ID        NUMBER(20),
  FINANCIAL_INCOME_TYPE_ID      NUMBER(20),
  GROSS_AMOUNT                  NUMBER(10,2),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_INCOME_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.FINANCIAL_INCOME_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.FINANCIAL_INCOME_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.FINANCIAL_INCOME_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.GROSS_AMOUNT IS 'Asset Amount is defined as the gross annual dollar amount associated with the specific asset type.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_INCOME_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FININCMDTH_PK ON ADR.FINANCIAL_INCOME_DETAIL_H
(FINANCIAL_INCOME_DETAIL_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_INCOME_DETAIL_H FOR ADR.FINANCIAL_INCOME_DETAIL_H;


ALTER TABLE ADR.FINANCIAL_INCOME_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_005
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065951
  CHECK ("FINANCIAL_INCOME_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065952
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065953
  CHECK ("FINANCIAL_INCOME_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065954
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065955
  CHECK ("FINANCIAL_INCOME_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065956
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065957
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065958
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065959
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065960
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FININCMDTH_PK
  PRIMARY KEY
  (FINANCIAL_INCOME_DETAIL_H_ID)
  USING INDEX ADR.FININCMDTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FINANCIAL_INCOME_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.FINANCIAL_INCOME_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_INCOME_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_INCOME_DETAIL_H TO ES_RW;
ALTER TABLE ADR.FINANCIAL_STATEMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_STATEMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_STATEMENT
(
  FINANCIAL_STATEMENT_ID          NUMBER(20),
  PERSON_ID                       NUMBER(20),
  PERSON_RELATION_ID              NUMBER(20),
  REF_FINANCIAL_STATEMENT_ID      NUMBER(20),
  FIN_STMT_MAPPING_TYPE_CODE      VARCHAR2(6 BYTE),
  INCOME_YEAR                     NUMBER(4),
  CONTRIBUTION_TO_SPOUSE_AMT      NUMBER(10,2),
  CONTRIBUTION_TO_DEPENDENT_AMT   NUMBER(10,2),
  DISCLOSED_FINANCIAL_INFO_IND    VARCHAR2(1 BYTE),
  VALID_DEPENDENT_IND             VARCHAR2(1 BYTE),
  SPOUSE_HAS_INCOME_IND           VARCHAR2(1 BYTE),
  MARRIED_CURR_CALENDAR_YEAR_IND  VARCHAR2(1 BYTE),
  MARRIED_PREV_CALENDAR_YEAR_IND  VARCHAR2(1 BYTE),
  DEPENDENT_INCOME_AVAILABLE_IND  VARCHAR2(1 BYTE),
  DEPENDENT_HAS_INCOME_IND        VARCHAR2(1 BYTE),
  DEPENDENT_IN_SCHOOL_IND         VARCHAR2(1 BYTE),
  LIVED_WITH_VETERAN_IND          VARCHAR2(1 BYTE),
  IS_INCAPABLE_SELF_SUPPORT_IND   VARCHAR2(1 BYTE),
  DEPENDENT_CHILDREN_COUNT        NUMBER(3),
  CHILD_SUPPORT_IND               VARCHAR2(1 BYTE),
  VERIFIED_DATE                   DATE,
  REMARKS                         VARCHAR2(4000 BYTE),
  POST_2005_DISPLAY_FORMAT_FLAG   NUMBER(1)     DEFAULT 0,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  CONTRIBUTED_TO_SPOUSE_IND       VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_STATEMENT IS 'The table stores a record of a persons financial status. Expenses, income, assets, and debts are stored in the corresponding child detail tables, and the statement itself contains other financially relevant data, including whether the person has declined to provide income information, has dependent children, pays child support, and is incapable of self support. PERSON_ID Foreign key to the PERSON table. PERSON_RELATION_ID Foreign key to the PERSON_RELATION table. REF_FINANCIAL_STATEMENT_ID Foreign key to the FINANCIAL_STATEMENT table. Self referential constraint. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.REF_FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.FIN_STMT_MAPPING_TYPE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.INCOME_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.CONTRIBUTION_TO_SPOUSE_AMT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.CONTRIBUTION_TO_DEPENDENT_AMT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.DISCLOSED_FINANCIAL_INFO_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.VALID_DEPENDENT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.SPOUSE_HAS_INCOME_IND IS 'Indicator that a persons spouse has his or her own income.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.MARRIED_CURR_CALENDAR_YEAR_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.MARRIED_PREV_CALENDAR_YEAR_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.DEPENDENT_INCOME_AVAILABLE_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.DEPENDENT_HAS_INCOME_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.DEPENDENT_IN_SCHOOL_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.LIVED_WITH_VETERAN_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.IS_INCAPABLE_SELF_SUPPORT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.DEPENDENT_CHILDREN_COUNT IS 'BR597 Number of dependent children is defined as the number of qualifying dependent children this veteran has. This number is derived based on the dependents DOB.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.CHILD_SUPPORT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.VERIFIED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.POST_2005_DISPLAY_FORMAT_FLAG IS 'Flag to indicate that on the screen, the record should be formatted in the post-2005 style.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT.CONTRIBUTED_TO_SPOUSE_IND IS 'This is a Yes/No indicator to show whether or not the Veteran contributed to Spousal or Dependent support during the income year.';


CREATE INDEX ADR.FINSTATE_FINSTATE_FK_I ON ADR.FINANCIAL_STATEMENT
(REF_FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FINSTATE_PK ON ADR.FINANCIAL_STATEMENT
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINSTATE_PRSNREL_FK_I ON ADR.FINANCIAL_STATEMENT
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINSTATE_PRSN_FK_I ON ADR.FINANCIAL_STATEMENT
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FIN_STD_FINANCIALMAP_FK_I ON ADR.FINANCIAL_STATEMENT
(FIN_STMT_MAPPING_TYPE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.FINAN_STATEMENT_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."FINANCIAL_STATEMENT" FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into FINANCIAL_STATEMENT_H
        (
        FINANCIAL_STATEMENT_H_ID,
        FINANCIAL_STATEMENT_ID,
        PERSON_ID,
        PERSON_RELATION_ID,
        REF_FINANCIAL_STATEMENT_ID,
        INCOME_YEAR,
        FIN_STMT_MAPPING_TYPE_CODE,
        CONTRIBUTION_TO_SPOUSE_AMT,
        CONTRIBUTION_TO_DEPENDENT_AMT,
        DISCLOSED_FINANCIAL_INFO_IND,
        SPOUSE_HAS_INCOME_IND,
        MARRIED_CURR_CALENDAR_YEAR_IND,
        MARRIED_PREV_CALENDAR_YEAR_IND,
        DEPENDENT_INCOME_AVAILABLE_IND,
        DEPENDENT_HAS_INCOME_IND,
        DEPENDENT_IN_SCHOOL_IND,
        LIVED_WITH_VETERAN_IND,
        IS_INCAPABLE_SELF_SUPPORT_IND,
        DEPENDENT_CHILDREN_COUNT,
        CHILD_SUPPORT_IND,
        VERIFIED_DATE,
        REMARKS,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        VALID_DEPENDENT_IND,
        POST_2005_DISPLAY_FORMAT_FLAG,
        TRANSACTION_TYPE_CODE,
        CONTRIBUTED_TO_SPOUSE_IND
          )
        values
        (
        FINANCIAL_STATEMENT_H_S.nextval,
        :old.FINANCIAL_STATEMENT_ID,
        :old.PERSON_ID,
        :old.PERSON_RELATION_ID,
        :old.REF_FINANCIAL_STATEMENT_ID,
        :old.INCOME_YEAR,
        :old.FIN_STMT_MAPPING_TYPE_CODE,
        :old.CONTRIBUTION_TO_SPOUSE_AMT,
        :old.CONTRIBUTION_TO_DEPENDENT_AMT,
        :old.DISCLOSED_FINANCIAL_INFO_IND,
        :old.SPOUSE_HAS_INCOME_IND,
        :old.MARRIED_CURR_CALENDAR_YEAR_IND,
        :old.MARRIED_PREV_CALENDAR_YEAR_IND,
        :old.DEPENDENT_INCOME_AVAILABLE_IND,
        :old.DEPENDENT_HAS_INCOME_IND,
        :old.DEPENDENT_IN_SCHOOL_IND,
        :old.LIVED_WITH_VETERAN_IND,
        :old.IS_INCAPABLE_SELF_SUPPORT_IND,
        :old.DEPENDENT_CHILDREN_COUNT,
        :old.CHILD_SUPPORT_IND,
        :old.VERIFIED_DATE,
        :old.REMARKS,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        :old.VALID_DEPENDENT_IND,
        :old.POST_2005_DISPLAY_FORMAT_FLAG,
        'D',
        :old.CONTRIBUTED_TO_SPOUSE_IND
        );

    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into FINANCIAL_STATEMENT_H
        (
        FINANCIAL_STATEMENT_H_ID,
        FINANCIAL_STATEMENT_ID,
        PERSON_ID,
        PERSON_RELATION_ID,
        REF_FINANCIAL_STATEMENT_ID,
        INCOME_YEAR,
        FIN_STMT_MAPPING_TYPE_CODE,
        CONTRIBUTION_TO_SPOUSE_AMT,
        CONTRIBUTION_TO_DEPENDENT_AMT,
        DISCLOSED_FINANCIAL_INFO_IND,
        SPOUSE_HAS_INCOME_IND,
        MARRIED_CURR_CALENDAR_YEAR_IND,
        MARRIED_PREV_CALENDAR_YEAR_IND,
        DEPENDENT_INCOME_AVAILABLE_IND,
        DEPENDENT_HAS_INCOME_IND,
        DEPENDENT_IN_SCHOOL_IND,
        LIVED_WITH_VETERAN_IND,
        IS_INCAPABLE_SELF_SUPPORT_IND,
        DEPENDENT_CHILDREN_COUNT,
        CHILD_SUPPORT_IND,
        VERIFIED_DATE,
        REMARKS,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        VALID_DEPENDENT_IND,
        POST_2005_DISPLAY_FORMAT_FLAG,
        TRANSACTION_TYPE_CODE,
        CONTRIBUTED_TO_SPOUSE_IND
          )
        values
        (
        FINANCIAL_STATEMENT_H_S.nextval,
        :new.FINANCIAL_STATEMENT_ID,
        :new.PERSON_ID,
        :new.PERSON_RELATION_ID,
        :new.REF_FINANCIAL_STATEMENT_ID,
        :new.INCOME_YEAR,
        :new.FIN_STMT_MAPPING_TYPE_CODE,
        :new.CONTRIBUTION_TO_SPOUSE_AMT,
        :new.CONTRIBUTION_TO_DEPENDENT_AMT,
        :new.DISCLOSED_FINANCIAL_INFO_IND,
        :new.SPOUSE_HAS_INCOME_IND,
        :new.MARRIED_CURR_CALENDAR_YEAR_IND,
        :new.MARRIED_PREV_CALENDAR_YEAR_IND,
        :new.DEPENDENT_INCOME_AVAILABLE_IND,
        :new.DEPENDENT_HAS_INCOME_IND,
        :new.DEPENDENT_IN_SCHOOL_IND,
        :new.LIVED_WITH_VETERAN_IND,
        :new.IS_INCAPABLE_SELF_SUPPORT_IND,
        :new.DEPENDENT_CHILDREN_COUNT,
        :new.CHILD_SUPPORT_IND,
        :new.VERIFIED_DATE,
        :new.REMARKS,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        :new.VALID_DEPENDENT_IND,
        :new.POST_2005_DISPLAY_FORMAT_FLAG,
        mDML,
        :new.CONTRIBUTED_TO_SPOUSE_IND
        );

    end if;

end  FINAN_STATEMENT_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_STATEMENT FOR ADR.FINANCIAL_STATEMENT;


ALTER TABLE ADR.FINANCIAL_STATEMENT ADD (
  CONSTRAINT AVCON_1172521913_POST__000
  CHECK (POST_2005_DISPLAY_FORMAT_FLAG BETWEEN 0
AND 0 OR POST_2005_DISPLAY_FORMAT_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066079
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066080
  CHECK ("POST_2005_DISPLAY_FORMAT_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066081
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066082
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066083
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066084
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066085
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINSTATE_PK
  PRIMARY KEY
  (FINANCIAL_STATEMENT_ID)
  USING INDEX ADR.FINSTATE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.FINANCIAL_STATEMENT ADD (
  CONSTRAINT FINSTATE_FINSTATE_FK 
  FOREIGN KEY (REF_FINANCIAL_STATEMENT_ID) 
  REFERENCES ADR.FINANCIAL_STATEMENT (FINANCIAL_STATEMENT_ID)
  DEFERRABLE INITIALLY DEFERRED
  DISABLE NOVALIDATE,
  CONSTRAINT FINSTATE_PRSNREL_FK 
  FOREIGN KEY (PERSON_RELATION_ID) 
  REFERENCES ADR.PERSON_RELATION (PERSON_RELATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT FINSTATE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.FINANCIAL_STATEMENT TO ADREXT;

GRANT SELECT ON ADR.FINANCIAL_STATEMENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FINANCIAL_STATEMENT TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_STATEMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_STATEMENT TO ES_RW;
ALTER TABLE ADR.FINANCIAL_STATEMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FINANCIAL_STATEMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.FINANCIAL_STATEMENT_H
(
  FINANCIAL_STATEMENT_H_ID        NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  FINANCIAL_STATEMENT_ID          NUMBER(20),
  PERSON_ID                       NUMBER(20),
  PERSON_RELATION_ID              NUMBER(20),
  REF_FINANCIAL_STATEMENT_ID      NUMBER(20),
  INCOME_YEAR                     NUMBER(4),
  FIN_STMT_MAPPING_TYPE_CODE      VARCHAR2(6 BYTE),
  CONTRIBUTION_TO_SPOUSE_AMT      NUMBER(10,2),
  CONTRIBUTION_TO_DEPENDENT_AMT   NUMBER(10,2),
  DISCLOSED_FINANCIAL_INFO_IND    VARCHAR2(1 BYTE),
  SPOUSE_HAS_INCOME_IND           VARCHAR2(1 BYTE),
  MARRIED_CURR_CALENDAR_YEAR_IND  VARCHAR2(1 BYTE),
  MARRIED_PREV_CALENDAR_YEAR_IND  VARCHAR2(1 BYTE),
  VALID_DEPENDENT_IND             VARCHAR2(1 BYTE),
  DEPENDENT_CHILDREN_COUNT        NUMBER(3),
  DEPENDENT_INCOME_AVAILABLE_IND  VARCHAR2(1 BYTE),
  DEPENDENT_HAS_INCOME_IND        VARCHAR2(1 BYTE),
  DEPENDENT_IN_SCHOOL_IND         VARCHAR2(1 BYTE),
  LIVED_WITH_VETERAN_IND          VARCHAR2(1 BYTE),
  IS_INCAPABLE_SELF_SUPPORT_IND   VARCHAR2(1 BYTE),
  CHILD_SUPPORT_IND               VARCHAR2(1 BYTE),
  VERIFIED_DATE                   DATE,
  POST_2005_DISPLAY_FORMAT_FLAG   NUMBER(1),
  REMARKS                         VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  CONTRIBUTED_TO_SPOUSE_IND       VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FINANCIAL_STATEMENT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.FINANCIAL_STATEMENT_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.REF_FINANCIAL_STATEMENT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.FIN_STMT_MAPPING_TYPE_CODE IS 'Hibernate mapping: used to identify whether this is a person, spouse, or dependent statement.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.CONTRIBUTION_TO_SPOUSE_AMT IS 'Dollar amount a person contributes to support of a spouse';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.CONTRIBUTION_TO_DEPENDENT_AMT IS 'Dollar amount a person contributes to support of a dependent.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.DISCLOSED_FINANCIAL_INFO_IND IS 'Indicator that a person, when asked, declined to provide income information.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.SPOUSE_HAS_INCOME_IND IS 'Indicator that a persons spouse has his or her own income.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.MARRIED_CURR_CALENDAR_YEAR_IND IS 'Indicates that the person was married in the current calendar year.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.MARRIED_PREV_CALENDAR_YEAR_IND IS 'Married last calendar year is defined as an Indicator of whether veteran was married during last calendar year.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.VALID_DEPENDENT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.DEPENDENT_CHILDREN_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.DEPENDENT_INCOME_AVAILABLE_IND IS 'BR6803 Income available is defined, as an Indicator Yes/No the childs income was available to the veteran.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.DEPENDENT_HAS_INCOME_IND IS 'Child has income is defined as an Indicator Yes/No the child had income during last calendar year. ';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.DEPENDENT_IN_SCHOOL_IND IS 'Dependent child school indicator is defined as an indicator If the child is between the ages of 18 and 23 indicate Yes/No if they attended school last calendar year';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.LIVED_WITH_VETERAN_IND IS 'Lived with patient is defined as an Indicator whether spouse lived with patient during last calendar year. ';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.IS_INCAPABLE_SELF_SUPPORT_IND IS 'Dependent Child is Permanently Incapable Of Self-Support. Indicator is defined as an Indicator if the child is over the age of 18 and became permanently incapable of self support before reaching the age of 18.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.VERIFIED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.POST_2005_DISPLAY_FORMAT_FLAG IS 'Flag to indicate that on the screen, the record should be formatted in the post-2005 style.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.FINANCIAL_STATEMENT_H.CONTRIBUTED_TO_SPOUSE_IND IS 'This is a Yes/No indicator to show whether or not the Veteran contributed to Spousal or Dependent support during the income year.';


CREATE INDEX ADR.FINSTATEH_FINSTATID_NU_I ON ADR.FINANCIAL_STATEMENT_H
(FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FINSTATEH_PK ON ADR.FINANCIAL_STATEMENT_H
(FINANCIAL_STATEMENT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINSTATEH_PRSNID_NU_I ON ADR.FINANCIAL_STATEMENT_H
(PERSON_ID, FIN_STMT_MAPPING_TYPE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.FINSTATEH_REFSTATID_NU_I ON ADR.FINANCIAL_STATEMENT_H
(REF_FINANCIAL_STATEMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FINANCIAL_STATEMENT_H FOR ADR.FINANCIAL_STATEMENT_H;


ALTER TABLE ADR.FINANCIAL_STATEMENT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_070
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067445
  CHECK ("FINANCIAL_STATEMENT_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067446
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067447
  CHECK ("FINANCIAL_STATEMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067448
  CHECK ("POST_2005_DISPLAY_FORMAT_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067449
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067450
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067451
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067452
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067453
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT FINSTATEH_PK
  PRIMARY KEY
  (FINANCIAL_STATEMENT_H_ID)
  USING INDEX ADR.FINSTATEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FINANCIAL_STATEMENT_H TO ADR_R;

GRANT SELECT ON ADR.FINANCIAL_STATEMENT_H TO ADR_RW;

GRANT SELECT ON ADR.FINANCIAL_STATEMENT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.FINANCIAL_STATEMENT_H TO ES_RW;
ALTER TABLE ADR.FUTURE_DISCHARGE_SRC_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.FUTURE_DISCHARGE_SRC_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.FUTURE_DISCHARGE_SRC_TYPE
(
  FUTURE_DISCHARGE_SRC_TYPE_ID  NUMBER(20)      NOT NULL,
  CODE                          VARCHAR2(2 BYTE) NOT NULL,
  NAME                          VARCHAR2(50 BYTE) NOT NULL,
  DESCRIPTION                   VARCHAR2(100 BYTE),
  ISACTIVE                      CHAR(1 BYTE)    NOT NULL,
  INACTIVE_DATE                 DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.FUTURE_DISCHARGE_SRC_TYPE IS 'lookup table to hold future discharge date source';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.FUTURE_DISCHARGE_SRC_TYPE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.CODE IS 'Code for the source';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.NAME IS 'Hold name of the Source';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.DESCRIPTION IS 'Hold name of the Source';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.ISACTIVE IS 'Hold 1/0 value';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.INACTIVE_DATE IS 'Date field to indicate when the list is inactive';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.FUTURE_DISCHARGE_SRC_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.FUTURE_DISCHARGE_SRC_TYPE_PK ON ADR.FUTURE_DISCHARGE_SRC_TYPE
(FUTURE_DISCHARGE_SRC_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FUTURE_DIS_SRC_TYPE_CD_UK ON ADR.FUTURE_DISCHARGE_SRC_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.FUTURE_DIS_SRC_TYPE_NM_UK ON ADR.FUTURE_DISCHARGE_SRC_TYPE
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM FUTURE_DISCHARGE_SRC_TYPE FOR ADR.FUTURE_DISCHARGE_SRC_TYPE;


ALTER TABLE ADR.FUTURE_DISCHARGE_SRC_TYPE ADD (
  CONSTRAINT FUTURE_DISCHARGE_SRC_TYPE_PK
  PRIMARY KEY
  (FUTURE_DISCHARGE_SRC_TYPE_ID)
  USING INDEX ADR.FUTURE_DISCHARGE_SRC_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.FUTURE_DISCHARGE_SRC_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.FUTURE_DISCHARGE_SRC_TYPE TO ADR_RW;
ALTER TABLE ADR.GEOCODE_STAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_STAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_STAGE
(
  GEOCODE_STAGE_ID        NUMBER(20)            NOT NULL,
  GEOCODE_STATION_ID      NUMBER(20),
  PERSON_ID               NUMBER(20),
  ADDRESS_H_ID            NUMBER(20),
  ADDR_EXACT_MATCH_IND    NUMBER(1),
  ADDRESS_LINE1           VARCHAR2(100 BYTE),
  ADDRESS_LINE2           VARCHAR2(100 BYTE),
  CITY                    VARCHAR2(60 BYTE),
  STATE                   VARCHAR2(2 BYTE),
  ZIP                     VARCHAR2(5 BYTE),
  ZIP_PLUS4               VARCHAR2(4 BYTE),
  COUNTRY                 VARCHAR2(50 BYTE),
  FIPS                    VARCHAR2(5 BYTE),
  NEW_VISN                VARCHAR2(2 BYTE),
  MARKET                  VARCHAR2(4 BYTE),
  SUBMARKET               VARCHAR2(6 BYTE),
  SECTOR                  VARCHAR2(8 BYTE),
  DISTRICT                VARCHAR2(4 BYTE),
  STATE_FIPS              VARCHAR2(2 BYTE),
  COUNTY_NAME             VARCHAR2(255 BYTE),
  DISTRICT_NAME           VARCHAR2(255 BYTE),
  MARKET_NAME             VARCHAR2(255 BYTE),
  STATE_BY_COUNTY         VARCHAR2(2 BYTE),
  URH_CODE                VARCHAR2(1 BYTE),
  CONGRESSIONAL_DISTRICT  VARCHAR2(20 BYTE),
  ADDR_LONGITUDE          NUMBER(20),
  ADDR_LATITUDE           NUMBER(20),
  DRIVING_DISTANCE        NUMBER(20),
  ADDR_LOCATION_NAME      VARCHAR2(59 BYTE),
  ADDR_MATCH_TYPE         VARCHAR2(2 BYTE),
  MATCH_ADDRESS           VARCHAR2(120 BYTE),
  GEOCODING_FLAG          VARCHAR2(1 BYTE),
  GEOCODE_SW_VER          VARCHAR2(50 BYTE),
  GEOCODE_DATE            DATE,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION GEOCODE_STAGE_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_STAGE IS 'Log of records sent to geocode service and data received';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.GEOCODE_STAGE_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.GEOCODE_STATION_ID IS 'FK to ADR.GEOCODE_STATION table';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDRESS_H_ID IS 'FK to ADR.ADDRESS_H table';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDR_EXACT_MATCH_IND IS 'Boolean Yes/No indicator of did Geocode match exactly to the address ES sent';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDRESS_LINE1 IS 'Street Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDRESS_LINE2 IS 'Street Address line 2';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.CITY IS 'City';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.STATE IS 'State for US (Province for non-US)';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ZIP IS '5-digit ZIP code';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ZIP_PLUS4 IS 'Four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.COUNTRY IS 'Country';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.FIPS IS 'County Federal Information Processing Standard code (2 state + 3 county digits)';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.NEW_VISN IS 'NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.MARKET IS 'Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.SUBMARKET IS 'Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.SECTOR IS 'Sector Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.DISTRICT IS 'VA District Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.STATE_FIPS IS 'County Federal Information Processing Standard code (2 digit state)';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.COUNTY_NAME IS 'County Name';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.DISTRICT_NAME IS 'VA District Name';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.MARKET_NAME IS 'Market Name';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.STATE_BY_COUNTY IS 'State determined based on county data joined to file (see CountyName)';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.CONGRESSIONAL_DISTRICT IS 'Congressional District';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDR_LONGITUDE IS 'Longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDR_LATITUDE IS 'Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.DRIVING_DISTANCE IS 'Driving Distance to nearest  station';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDR_LOCATION_NAME IS 'The name of the participating locator used for matching the address';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.ADDR_MATCH_TYPE IS 'M=Matched; U=Unmatched; T=Tied';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.MATCH_ADDRESS IS 'The address where the matched location actually resides based on geocoded results';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.GEOCODING_FLAG IS 'Geocoding flag 0, 1, 2, 3, 4, 5, Null';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.GEOCODE_SW_VER IS 'Geocoder Software Version';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.GEOCODE_DATE IS 'Date file was geocoded. Format: CYYMMDD where C = Century: 1=1800, 2=1900, C=2000';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_STAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.GEOCODE_STAGE_PK ON ADR.GEOCODE_STAGE
(RECORD_CREATED_DATE, GEOCODE_STAGE_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION GEOCODE_STAGE_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.GS_PERSONID_IDX ON ADR.GEOCODE_STAGE
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION GS_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION GS_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION GS_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION GS_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_STAGE FOR ADR.GEOCODE_STAGE;


ALTER TABLE ADR.GEOCODE_STAGE ADD (
  CONSTRAINT GEOCODE_STAGE_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, GEOCODE_STAGE_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.GEOCODE_STAGE ADD (
  CONSTRAINT GEOCD_STAGE_ADDR_D_FK 
  FOREIGN KEY (ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT GEOCODE_STG_STATION_FK 
  FOREIGN KEY (GEOCODE_STATION_ID) 
  REFERENCES ADR.GEOCODE_STATION (GEOCODE_STATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT GS_TRNSMT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.GEOCODE_STAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_STAGE TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_STAGE TO ES_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_STAGE TO ES_RW;

GRANT SELECT ON ADR.GEOCODE_STAGE TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_STAGE TO VETCHOICE_RW;
ALTER TABLE ADR.GEOCODE_STATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_STATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_STATION
(
  GEOCODE_STATION_ID        NUMBER(20)          NOT NULL,
  STD_INSTITUTION_ID        NUMBER(20),
  STATION_NUMBER            VARCHAR2(7 BYTE),
  STATION_ADRR_LINE1        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE2        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE3        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE4        VARCHAR2(100 BYTE),
  STATION_CITY              VARCHAR2(50 BYTE),
  STATION_STATE             VARCHAR2(2 BYTE),
  STATION_ZIP               VARCHAR2(5 BYTE),
  STATION_ZIP4              VARCHAR2(5 BYTE),
  VA_SITE_IND               VARCHAR2(1 BYTE),
  CONTRACTED_IND            VARCHAR2(1 BYTE),
  NUMBER_OF_PROVIDERS       NUMBER(8),
  PARENT_STATION            VARCHAR2(6 BYTE),
  PARENT_STA_COMPLEXITY     VARCHAR2(80 BYTE),
  PLANNED_OR_OPERATIONAL    VARCHAR2(1 BYTE),
  STATION_PHONE             VARCHAR2(12 BYTE),
  STATION_FAX               VARCHAR2(12 BYTE),
  SHARED_IND                VARCHAR2(1 BYTE),
  SHARED_PARENT_STATION     VARCHAR2(6 BYTE),
  HEALTHCARE_CENTER         NUMBER(8),
  MSCBOC                    NUMBER(8),
  NEW_VAMC                  NUMBER(8),
  PCCBOC                    NUMBER(8),
  COC_CLASSIFICATION_CODE   NUMBER(16),
  VA_HOSPITAL               NUMBER(8),
  VHA_SITE_CATEGORY_CODE    VARCHAR2(6 BYTE),
  SITE_CLASSIFICATION_NAME  VARCHAR2(150 BYTE),
  PRIMARY_SVC_TYPE          VARCHAR2(6 BYTE),
  OP_COC_CLASSIFICATION     VARCHAR2(25 BYTE),
  STATION_LONGITUDE         NUMBER(20),
  STATION_LATITUDE          NUMBER(20),
  STATION_NEW_VISN          VARCHAR2(6 BYTE),
  STATION_MARKET            VARCHAR2(4 BYTE),
  STATION_SUBMARKET         VARCHAR2(6 BYTE),
  STATION_COUNTYNAME        VARCHAR2(255 BYTE),
  STATION_DISTRICT          VARCHAR2(4 BYTE),
  STATION_DISTRICT_NAME     VARCHAR2(100 BYTE),
  STATION_URH_CODE          VARCHAR2(1 BYTE),
  STATION_OPERATION_DATE    DATE,
  VAST_EXTRACT_DATE         DATE,
  ACTIVE_START_DT           CHAR(20 BYTE),
  ACTIVE_END_DT             CHAR(20 BYTE),
  ACTIVE_FAG                CHAR(20 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  STATION_NAME              VARCHAR2(100 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_STATION IS 'List of stations provided by geocode service';

COMMENT ON COLUMN ADR.GEOCODE_STATION.GEOCODE_STATION_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_NUMBER IS 'Unique site identifier - station identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ADRR_LINE1 IS 'Station Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ADRR_LINE2 IS 'Station Address line 2';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ADRR_LINE3 IS 'Station Address line 3';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ADRR_LINE4 IS 'Station Address line 4';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_CITY IS 'Station City';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_STATE IS 'Station State';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ZIP IS 'Station Zip';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_ZIP4 IS 'Station four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_STATION.VA_SITE_IND IS 'VA-owned site flag';

COMMENT ON COLUMN ADR.GEOCODE_STATION.CONTRACTED_IND IS 'V=VA; L=Leased; C=Contracted';

COMMENT ON COLUMN ADR.GEOCODE_STATION.NUMBER_OF_PROVIDERS IS 'Number of sites providing service under this station identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION.PARENT_STATION IS 'Administrative Parent Station';

COMMENT ON COLUMN ADR.GEOCODE_STATION.PARENT_STA_COMPLEXITY IS 'Administrative Parent Station Complexity Level';

COMMENT ON COLUMN ADR.GEOCODE_STATION.PLANNED_OR_OPERATIONAL IS 'Planned (P) or operational/activated (A)';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_PHONE IS 'Station phone number';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_FAX IS 'Station fax number';

COMMENT ON COLUMN ADR.GEOCODE_STATION.SHARED_IND IS 'Shared flag (indicates multiple station numbers operate at the same geographic location)';

COMMENT ON COLUMN ADR.GEOCODE_STATION.SHARED_PARENT_STATION IS 'First responsible site / parent of sharing site';

COMMENT ON COLUMN ADR.GEOCODE_STATION.HEALTHCARE_CENTER IS 'Health Care Center';

COMMENT ON COLUMN ADR.GEOCODE_STATION.MSCBOC IS 'Multi-Specialty Community-Based Outpatient Clinic';

COMMENT ON COLUMN ADR.GEOCODE_STATION.NEW_VAMC IS 'VA Medical Center';

COMMENT ON COLUMN ADR.GEOCODE_STATION.PCCBOC IS 'Primary Care Community-Based Outpatient Clinic';

COMMENT ON COLUMN ADR.GEOCODE_STATION.COC_CLASSIFICATION_CODE IS 'Station Continuum of Care (CoC) classification. It will be blank for stations permanently deactivated prior to 10/1/14, as those stations never received a CoC Classification.¿ It will also be blank for Vet Centers, Mobile Vet Centers, and Multicontract sites, as these don¿t receive CoC classifications.';

COMMENT ON COLUMN ADR.GEOCODE_STATION.VA_HOSPITAL IS 'VA Hospital';

COMMENT ON COLUMN ADR.GEOCODE_STATION.VHA_SITE_CATEGORY_CODE IS 'Internal indicator that identifies whether a facility is classified according to VHA Handbook 1006.02 ¿ VHA Site Classifications and Definitions';

COMMENT ON COLUMN ADR.GEOCODE_STATION.SITE_CLASSIFICATION_NAME IS 'Site of care classifications name ';

COMMENT ON COLUMN ADR.GEOCODE_STATION.PRIMARY_SVC_TYPE IS 'Primary service type';

COMMENT ON COLUMN ADR.GEOCODE_STATION.OP_COC_CLASSIFICATION IS 'Outpatient Detailed CoC Site Classification. Each point of service receives an outpatient care rating within the VHA site classifications based on the services provided at that location, including mental health, primary care, and specialty care.';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_LONGITUDE IS 'Station longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_LATITUDE IS 'Station Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_NEW_VISN IS 'Station NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_MARKET IS 'Station Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_SUBMARKET IS 'Station Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_COUNTYNAME IS 'Station County Name';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_DISTRICT IS 'ID assigned to fit into the five Department Level Districts that are part of the MyVA Realignment';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_DISTRICT_NAME IS 'Stations assigned VA Department Level District Name';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_OPERATION_DATE IS 'Operational date of site: (A) is date opened/saw first patient; (P) is date planned to open. Format: CYYMMDD';

COMMENT ON COLUMN ADR.GEOCODE_STATION.VAST_EXTRACT_DATE IS 'VAST extraction date. Format: CYYMMDD';

COMMENT ON COLUMN ADR.GEOCODE_STATION.ACTIVE_START_DT IS 'Date station became active';

COMMENT ON COLUMN ADR.GEOCODE_STATION.ACTIVE_END_DT IS 'Date station became inactive';

COMMENT ON COLUMN ADR.GEOCODE_STATION.ACTIVE_FAG IS 'Indicator of the station¿s active/inactive status';

COMMENT ON COLUMN ADR.GEOCODE_STATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_STATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_STATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_STATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_STATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.GEOCODE_STATION.STATION_NAME IS 'Name of the VACAA facility';


CREATE UNIQUE INDEX ADR.GEOCODE_STATION_PK ON ADR.GEOCODE_STATION
(GEOCODE_STATION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.GEOCODE_STATION_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.GEOCODE_STATION
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO GEOCODE_STATION_H (
         GEOCODE_STATION_H_ID  ,
         GEOCODE_STATION_ID,
         STD_INSTITUTION_ID,
         STATION_NUMBER,
         STATION_ADRR_LINE1,
         STATION_ADRR_LINE2,
         STATION_ADRR_LINE3 ,
         STATION_ADRR_LINE4,
         STATION_CITY ,
         STATION_STATE ,
         STATION_ZIP ,
         STATION_ZIP4,
         VA_SITE_IND ,
         CONTRACTED_IND ,
         NUMBER_OF_PROVIDERS,
         PARENT_STATION,
         PARENT_STA_COMPLEXITY,
         PLANNED_OR_OPERATIONAL,
         STATION_PHONE,
         STATION_FAX,
         SHARED_IND,
         SHARED_PARENT_STATION,
         HEALTHCARE_CENTER,
         MSCBOC,
         NEW_VAMC,
         PCCBOC,
         COC_CLASSIFICATION_CODE ,
         VA_HOSPITAL,
         VHA_SITE_CATEGORY_CODE,
         SITE_CLASSIFICATION_NAME,
         PRIMARY_SVC_TYPE ,
         OP_COC_CLASSIFICATION,
         STATION_LONGITUDE,
         STATION_LATITUDE,
         STATION_NEW_VISN,
         STATION_MARKET,
         STATION_SUBMARKET,
         STATION_COUNTYNAME,
         STATION_DISTRICT,
         STATION_DISTRICT_NAME,
         STATION_URH_CODE,
         STATION_OPERATION_DATE,
         VAST_EXTRACT_DATE ,
         ACTIVE_START_DT,
         ACTIVE_END_DT,
         ACTIVE_FAG,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         STATION_NAME ,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
         )
 VALUES (GEOCODE_TRANSMIT_H_S.NEXTVAL,
       :old.GEOCODE_STATION_ID,
       :old.STD_INSTITUTION_ID,
       :old.STATION_NUMBER,
       :old.STATION_ADRR_LINE1,
       :old.STATION_ADRR_LINE2,
       :old.STATION_ADRR_LINE3 ,
       :old.STATION_ADRR_LINE4,
       :old.STATION_CITY ,
       :old.STATION_STATE ,
       :old.STATION_ZIP ,
       :old.STATION_ZIP4,
       :old.VA_SITE_IND ,
       :old.CONTRACTED_IND ,
       :old.NUMBER_OF_PROVIDERS,
       :old.PARENT_STATION,
       :old.PARENT_STA_COMPLEXITY,
       :old.PLANNED_OR_OPERATIONAL,
       :old.STATION_PHONE,
       :old.STATION_FAX,
       :old.SHARED_IND,
       :old.SHARED_PARENT_STATION,
       :old.HEALTHCARE_CENTER,
       :old.MSCBOC,
       :old.NEW_VAMC,
       :old.PCCBOC,
       :old.COC_CLASSIFICATION_CODE ,
       :old.VA_HOSPITAL,
       :old.VHA_SITE_CATEGORY_CODE,
       :old.SITE_CLASSIFICATION_NAME,
       :old.PRIMARY_SVC_TYPE ,
       :old.OP_COC_CLASSIFICATION,
       :old.STATION_LONGITUDE,
       :old.STATION_LATITUDE,
       :old.STATION_NEW_VISN,
       :old.STATION_MARKET,
       :old.STATION_SUBMARKET,
       :old.STATION_COUNTYNAME,
       :old.STATION_DISTRICT,
       :old.STATION_DISTRICT_NAME,
       :old.STATION_URH_CODE,
       :old.STATION_OPERATION_DATE,
       :old.VAST_EXTRACT_DATE ,
       :old.ACTIVE_START_DT,
       :old.ACTIVE_END_DT,
       :old.ACTIVE_FAG,
       :old.RECORD_CREATED_BY,
       :old.RECORD_CREATED_DATE,
       :old.RECORD_MODIFIED_BY,
       :old.RECORD_MODIFIED_DATE,
       :old.RECORD_MODIFIED_COUNT,
       :old.STATION_NAME ,
       sysdate,
       mDML);
ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

       INSERT INTO GEOCODE_STATION_H (
         GEOCODE_STATION_H_ID  ,
         GEOCODE_STATION_ID,
         STD_INSTITUTION_ID,
         STATION_NUMBER,
         STATION_ADRR_LINE1,
         STATION_ADRR_LINE2,
         STATION_ADRR_LINE3 ,
         STATION_ADRR_LINE4,
         STATION_CITY ,
         STATION_STATE ,
         STATION_ZIP ,
         STATION_ZIP4,
         VA_SITE_IND ,
         CONTRACTED_IND ,
         NUMBER_OF_PROVIDERS,
         PARENT_STATION,
         PARENT_STA_COMPLEXITY,
         PLANNED_OR_OPERATIONAL,
         STATION_PHONE,
         STATION_FAX,
         SHARED_IND,
         SHARED_PARENT_STATION,
         HEALTHCARE_CENTER,
         MSCBOC,
         NEW_VAMC,
         PCCBOC,
         COC_CLASSIFICATION_CODE ,
         VA_HOSPITAL,
         VHA_SITE_CATEGORY_CODE,
         SITE_CLASSIFICATION_NAME,
         PRIMARY_SVC_TYPE ,
         OP_COC_CLASSIFICATION,
         STATION_LONGITUDE,
         STATION_LATITUDE,
         STATION_NEW_VISN,
         STATION_MARKET,
         STATION_SUBMARKET,
         STATION_COUNTYNAME,
         STATION_DISTRICT,
         STATION_DISTRICT_NAME,
         STATION_URH_CODE,
         STATION_OPERATION_DATE,
         VAST_EXTRACT_DATE ,
         ACTIVE_START_DT,
         ACTIVE_END_DT,
         ACTIVE_FAG,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         STATION_NAME ,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE)
 VALUES (GEOCODE_STATION_H_S.NEXTVAL  ,
        :new.GEOCODE_STATION_ID,
        :new.STD_INSTITUTION_ID,
        :new.STATION_NUMBER,
        :new.STATION_ADRR_LINE1,
        :new.STATION_ADRR_LINE2,
        :new.STATION_ADRR_LINE3 ,
        :new.STATION_ADRR_LINE4,
        :new.STATION_CITY ,
        :new.STATION_STATE ,
        :new.STATION_ZIP ,
        :new.STATION_ZIP4,
        :new.VA_SITE_IND ,
        :new.CONTRACTED_IND ,
        :new.NUMBER_OF_PROVIDERS,
        :new.PARENT_STATION,
        :new.PARENT_STA_COMPLEXITY,
        :new.PLANNED_OR_OPERATIONAL,
        :new.STATION_PHONE,
        :new.STATION_FAX,
        :new.SHARED_IND,
        :new.SHARED_PARENT_STATION,
        :new.HEALTHCARE_CENTER,
        :new.MSCBOC,
        :new.NEW_VAMC,
        :new.PCCBOC,
        :new.COC_CLASSIFICATION_CODE ,
        :new.VA_HOSPITAL,
        :new.VHA_SITE_CATEGORY_CODE,
        :new.SITE_CLASSIFICATION_NAME,
        :new.PRIMARY_SVC_TYPE ,
        :new.OP_COC_CLASSIFICATION,
        :new.STATION_LONGITUDE,
        :new.STATION_LATITUDE,
        :new.STATION_NEW_VISN,
        :new.STATION_MARKET,
        :new.STATION_SUBMARKET,
        :new.STATION_COUNTYNAME,
        :new.STATION_DISTRICT,
        :new.STATION_DISTRICT_NAME,
        :new.STATION_URH_CODE,
        :new.STATION_OPERATION_DATE,
        :new.VAST_EXTRACT_DATE ,
        :new.ACTIVE_START_DT,
        :new.ACTIVE_END_DT,
        :new.ACTIVE_FAG,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        :new.STATION_NAME ,
        sysdate,
        mDML);
   END IF;

 END GEOCODE_STATION_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_STATION FOR ADR.GEOCODE_STATION;


ALTER TABLE ADR.GEOCODE_STATION ADD (
  CONSTRAINT GEOCODE_STATION_PK
  PRIMARY KEY
  (GEOCODE_STATION_ID)
  USING INDEX ADR.GEOCODE_STATION_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.GEOCODE_STATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_STATION TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_STATION TO ES_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_STATION TO ES_RW;

GRANT SELECT ON ADR.GEOCODE_STATION TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_STATION TO VETCHOICE_RW;
ALTER TABLE ADR.GEOCODE_STATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_STATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_STATION_H
(
  GEOCODE_STATION_H_ID      NUMBER(20)          NOT NULL,
  GEOCODE_STATION_ID        NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  STATION_NUMBER            VARCHAR2(7 BYTE),
  STATION_ADRR_LINE1        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE2        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE3        VARCHAR2(100 BYTE),
  STATION_ADRR_LINE4        VARCHAR2(100 BYTE),
  STATION_CITY              VARCHAR2(50 BYTE),
  STATION_STATE             VARCHAR2(2 BYTE),
  STATION_ZIP               VARCHAR2(5 BYTE),
  STATION_ZIP4              VARCHAR2(5 BYTE),
  VA_SITE_IND               VARCHAR2(1 BYTE),
  CONTRACTED_IND            VARCHAR2(1 BYTE),
  NUMBER_OF_PROVIDERS       NUMBER(8),
  PARENT_STATION            VARCHAR2(6 BYTE),
  PARENT_STA_COMPLEXITY     VARCHAR2(80 BYTE),
  PLANNED_OR_OPERATIONAL    VARCHAR2(1 BYTE),
  STATION_PHONE             VARCHAR2(12 BYTE),
  STATION_FAX               VARCHAR2(12 BYTE),
  SHARED_IND                VARCHAR2(1 BYTE),
  SHARED_PARENT_STATION     VARCHAR2(6 BYTE),
  HEALTHCARE_CENTER         NUMBER(8),
  MSCBOC                    NUMBER(8),
  NEW_VAMC                  NUMBER(8),
  PCCBOC                    NUMBER(8),
  COC_CLASSIFICATION_CODE   NUMBER(16),
  VA_HOSPITAL               NUMBER(8),
  VHA_SITE_CATEGORY_CODE    VARCHAR2(6 BYTE),
  SITE_CLASSIFICATION_NAME  VARCHAR2(150 BYTE),
  PRIMARY_SVC_TYPE          VARCHAR2(6 BYTE),
  OP_COC_CLASSIFICATION     VARCHAR2(25 BYTE),
  STATION_LONGITUDE         NUMBER(20),
  STATION_LATITUDE          NUMBER(20),
  STATION_NEW_VISN          VARCHAR2(6 BYTE),
  STATION_MARKET            VARCHAR2(4 BYTE),
  STATION_SUBMARKET         VARCHAR2(6 BYTE),
  STATION_COUNTYNAME        VARCHAR2(255 BYTE),
  STATION_DISTRICT          VARCHAR2(4 BYTE),
  STATION_DISTRICT_NAME     VARCHAR2(100 BYTE),
  STATION_URH_CODE          VARCHAR2(1 BYTE),
  STATION_OPERATION_DATE    DATE,
  VAST_EXTRACT_DATE         DATE,
  ACTIVE_START_DT           CHAR(20 BYTE),
  ACTIVE_END_DT             CHAR(20 BYTE),
  ACTIVE_FAG                CHAR(20 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE       DATE,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE      DATE,
  RECORD_MODIFIED_COUNT     NUMBER(10),
  STATION_NAME              VARCHAR2(100 BYTE),
  HISTORY_CREATED_DATE      DATE                NOT NULL,
  TRANSACTION_TYPE_CODE     CHAR(1 BYTE)        NOT NULL
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION GEOCODESTATNH_RECRDT VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_STATION_H IS 'List of stations history provided by geocode service';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.GEOCODE_STATION_H_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.GEOCODE_STATION_ID IS 'FK to GEOCODE_STATION';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_NUMBER IS 'Unique site identifier - station identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ADRR_LINE1 IS 'Station Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ADRR_LINE2 IS 'Station Address line 2';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ADRR_LINE3 IS 'Station Address line 3';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ADRR_LINE4 IS 'Station Address line 4';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_CITY IS 'Station City';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_STATE IS 'Station State';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ZIP IS 'Station Zip';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_ZIP4 IS 'Station four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.VA_SITE_IND IS 'VA-owned site flag';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.CONTRACTED_IND IS 'V=VA; L=Leased; C=Contracted';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.NUMBER_OF_PROVIDERS IS 'Number of sites providing service under this station identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.PARENT_STATION IS 'Administrative Parent Station';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.PARENT_STA_COMPLEXITY IS 'Administrative Parent Station Complexity Level';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.PLANNED_OR_OPERATIONAL IS 'Planned (P) or operational/activated (A)';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_PHONE IS 'Station phone number';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_FAX IS 'Station fax number';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.SHARED_IND IS 'Shared flag (indicates multiple station numbers operate at the same geographic location)';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.SHARED_PARENT_STATION IS 'First responsible site / parent of sharing site';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.HEALTHCARE_CENTER IS 'Health Care Center';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.MSCBOC IS 'Multi-Specialty Community-Based Outpatient Clinic';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.NEW_VAMC IS 'VA Medical Center';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.PCCBOC IS 'Primary Care Community-Based Outpatient Clinic';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.COC_CLASSIFICATION_CODE IS 'Station Continuum of Care (CoC) classification. It will be blank for stations permanently deactivated prior to 10/1/14, as those stations never received a CoC Classification.  It will also be blank for Vet Centers, Mobile Vet Centers, and Multicontract sites, as these don¿t receive CoC classifications.';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.VA_HOSPITAL IS 'VA Hospital';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.VHA_SITE_CATEGORY_CODE IS 'Internal indicator that identifies whether a facility is classified according to VHA Handbook 1006.02 ¿ VHA Site Classifications and Definitions';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.SITE_CLASSIFICATION_NAME IS 'Site of care classifications name ';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.PRIMARY_SVC_TYPE IS 'Primary service type';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.OP_COC_CLASSIFICATION IS 'Outpatient Detailed CoC Site Classification. Each point of service receives an outpatient care rating within the VHA site classifications based on the services provided at that location, including mental health, primary care, and specialty care.';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_LONGITUDE IS 'Station longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_LATITUDE IS 'Station Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_NEW_VISN IS 'Station NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_MARKET IS 'Station Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_SUBMARKET IS 'Station Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_COUNTYNAME IS 'Station County Name';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_DISTRICT IS 'ID assigned to fit into the five Department Level Districts that are part of the MyVA Realignment';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_DISTRICT_NAME IS 'Stations assigned VA Department Level District Name';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_OPERATION_DATE IS 'Operational date of site: (A) is date opened/saw first patient; (P) is date planned to open. Format: CYYMMDD';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.VAST_EXTRACT_DATE IS 'VAST extraction date. Format: CYYMMDD';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.ACTIVE_START_DT IS 'Date station became active';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.ACTIVE_END_DT IS 'Date station became inactive';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.ACTIVE_FAG IS 'Indicator of the station¿s active/inactive status';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.GEOCODE_STATION_H.STATION_NAME IS 'Name of the VACAA facility';


CREATE UNIQUE INDEX ADR.GEOCODE_STATION_H_PK ON ADR.GEOCODE_STATION_H
(GEOCODE_STATION_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.GEOCODE_ST_ID_NU_I ON ADR.GEOCODE_STATION_H
(GEOCODE_STATION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_STATION_H FOR ADR.GEOCODE_STATION_H;


ALTER TABLE ADR.GEOCODE_STATION_H ADD (
  CONSTRAINT GEOCODE_STATION_H_PK
  PRIMARY KEY
  (GEOCODE_STATION_H_ID)
  USING INDEX ADR.GEOCODE_STATION_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.GEOCODE_STATION_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_STATION_H TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_STATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_STATION_H TO ES_RW;
ALTER TABLE ADR.GEOCODE_TRANSMIT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_TRANSMIT CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_TRANSMIT
(
  GEOCODE_TRANSMIT_ID     NUMBER(20)            NOT NULL,
  GEOCODE_STATION_ID      NUMBER(20),
  PERSON_ID               NUMBER(20),
  ADDRESS_H_ID            NUMBER(20),
  ADDR_EXACT_MATCH_IND    NUMBER(1),
  ADDRESS_LINE1           VARCHAR2(100 BYTE),
  ADDRESS_LINE2           VARCHAR2(100 BYTE),
  CITY                    VARCHAR2(60 BYTE),
  STATE                   VARCHAR2(2 BYTE),
  ZIP                     VARCHAR2(5 BYTE),
  ZIP_PLUS4               VARCHAR2(4 BYTE),
  COUNTRY                 VARCHAR2(50 BYTE),
  FIPS                    VARCHAR2(5 BYTE),
  NEW_VISN                VARCHAR2(2 BYTE),
  MARKET                  VARCHAR2(4 BYTE),
  SUBMARKET               VARCHAR2(6 BYTE),
  SECTOR                  VARCHAR2(8 BYTE),
  DISTRICT                VARCHAR2(4 BYTE),
  STATE_FIPS              VARCHAR2(2 BYTE),
  COUNTY_NAME             VARCHAR2(255 BYTE),
  DISTRICT_NAME           VARCHAR2(255 BYTE),
  MARKET_NAME             VARCHAR2(255 BYTE),
  STATE_BY_COUNTY         VARCHAR2(2 BYTE),
  URH_CODE                VARCHAR2(1 BYTE),
  CONGRESSIONAL_DISTRICT  VARCHAR2(20 BYTE),
  ADDR_LONGITUDE          NUMBER(20),
  ADDR_LATITUDE           NUMBER(20),
  DRIVING_DISTANCE        NUMBER(20),
  ADDR_LOCATION_NAME      VARCHAR2(59 BYTE),
  ADDR_MATCH_TYPE         VARCHAR2(2 BYTE),
  MATCH_ADDRESS           VARCHAR2(120 BYTE),
  GEOCODING_FLAG          VARCHAR2(1 BYTE),
  GEOCODE_SW_VER          VARCHAR2(200 BYTE),
  GEOCODE_DATE            DATE,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL,
  VCELIGIBILITY_ID        NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION GEOCODE_TRANSMIT_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_TRANSMIT IS 'Log of records sent to geocode service and data received';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.GEOCODE_TRANSMIT_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.GEOCODE_STATION_ID IS 'FK to ADR.GEOCODE_STATION table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDRESS_H_ID IS 'FK to ADR.ADDRESS_H table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDR_EXACT_MATCH_IND IS 'Boolean Yes/No indicator of did Geocode match exactly to the address ES sent';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDRESS_LINE1 IS 'Street Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDRESS_LINE2 IS 'Street Address line 2';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.CITY IS 'City';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.STATE IS 'State for US (Province for non-US)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ZIP IS '5-digit ZIP code';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ZIP_PLUS4 IS 'Four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.COUNTRY IS 'Country';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.FIPS IS 'County Federal Information Processing Standard code (2 state + 3 county digits)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.NEW_VISN IS 'NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.MARKET IS 'Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.SUBMARKET IS 'Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.SECTOR IS 'Sector Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.DISTRICT IS 'VA District Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.STATE_FIPS IS 'County Federal Information Processing Standard code (2 digit state)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.COUNTY_NAME IS 'County Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.DISTRICT_NAME IS 'VA District Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.MARKET_NAME IS 'Market Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.STATE_BY_COUNTY IS 'State determined based on county data joined to file (see CountyName)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.CONGRESSIONAL_DISTRICT IS 'Congressional District';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDR_LONGITUDE IS 'Longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDR_LATITUDE IS 'Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.DRIVING_DISTANCE IS 'Driving Distance to nearest  station';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDR_LOCATION_NAME IS 'The name of the participating locator used for matching the address';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.ADDR_MATCH_TYPE IS 'M=Matched; U=Unmatched; T=Tied';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.MATCH_ADDRESS IS 'The address where the matched location actually resides based on geocoded results';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.GEOCODING_FLAG IS 'Geocoding flag 0, 1, 2, 3, 4, 5, Null';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.GEOCODE_SW_VER IS 'Geocoder Software Version';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.GEOCODE_DATE IS 'Date file was geocoded. Format: CYYMMDD where C = Century: 1=1800, 2=1900, C=2000';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.GEOCODETRANS_VCELIG_FK_I ON ADR.GEOCODE_TRANSMIT
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.GEOCODE_TRANSMIT_PK ON ADR.GEOCODE_TRANSMIT
(RECORD_CREATED_DATE, GEOCODE_TRANSMIT_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION GEOCODE_TRANSMIT_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.GT_PERSONID_IDX ON ADR.GEOCODE_TRANSMIT
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION GT_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION GT_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION GT_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION GT_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.GEOCODE_TRANSMIT_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.GEOCODE_TRANSMIT
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO GEOCODE_TRANSMIT_H (GEOCODE_TRANSMIT_H_ID,
                                      GEOCODE_TRANSMIT_ID,
                                      GEOCODE_STATION_ID,
                                      PERSON_ID,
                                      ADDRESS_H_ID,
                                      ADDR_EXACT_MATCH_IND,
                                      ADDRESS_LINE1,
                                      ADDRESS_LINE2,
                                      CITY,
                                      STATE,
                                      ZIP,
                                      ZIP_PLUS4,
                                      COUNTRY,
                                      FIPS,
                                      NEW_VISN,
                                      MARKET,
                                      SUBMARKET,
                                      SECTOR,
                                      DISTRICT,
                                      STATE_FIPS,
                                      COUNTY_NAME,
                                      DISTRICT_NAME,
                                      MARKET_NAME,
                                      STATE_BY_COUNTY,
                                      URH_CODE,
                                      CONGRESSIONAL_DISTRICT,
                                      ADDR_LONGITUDE,
                                      ADDR_LATITUDE,
                                      DRIVING_DISTANCE,
                                      ADDR_LOCATION_NAME,
                                      ADDR_MATCH_TYPE,
                                      MATCH_ADDRESS,
                                      GEOCODING_FLAG,
                                      GEOCODE_SW_VER,
                                      GEOCODE_DATE,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      VCELIGIBILITY_ID,
                                      TRANSACTION_TYPE_CODE)
           VALUES (GEOCODE_TRANSMIT_H_S.NEXTVAL,
                   :old.GEOCODE_TRANSMIT_ID,
                   :old.GEOCODE_STATION_ID,
                   :old.PERSON_ID,
                   :old.ADDRESS_H_ID,
                   :old.ADDR_EXACT_MATCH_IND,
                   :old.ADDRESS_LINE1,
                   :old.ADDRESS_LINE2,
                   :old.CITY,
                   :old.STATE,
                   :old.ZIP,
                   :old.ZIP_PLUS4,
                   :old.COUNTRY,
                   :old.FIPS,
                   :old.NEW_VISN,
                   :old.MARKET,
                   :old.SUBMARKET,
                   :old.SECTOR,
                   :old.DISTRICT,
                   :old.STATE_FIPS,
                   :old.COUNTY_NAME,
                   :old.DISTRICT_NAME,
                   :old.MARKET_NAME,
                   :old.STATE_BY_COUNTY,
                   :old.URH_CODE,
                   :old.CONGRESSIONAL_DISTRICT,
                   :old.ADDR_LONGITUDE,
                   :old.ADDR_LATITUDE,
                   :old.DRIVING_DISTANCE,
                   :old.ADDR_LOCATION_NAME,
                   :old.ADDR_MATCH_TYPE,
                   :old.MATCH_ADDRESS,
                   :old.GEOCODING_FLAG,
                   :old.GEOCODE_SW_VER,
                   :old.GEOCODE_DATE,
                   :old.RECORD_CREATED_BY,
                   :old.RECORD_CREATED_DATE,
                   :old.RECORD_MODIFIED_BY,
                   :old.RECORD_MODIFIED_DATE,
                   :old.RECORD_MODIFIED_COUNT,
                   :old.VCELIGIBILITY_ID,
                   mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

      INSERT INTO GEOCODE_TRANSMIT_H (GEOCODE_TRANSMIT_H_ID,
                                      GEOCODE_TRANSMIT_ID,
                                      GEOCODE_STATION_ID,
                                      PERSON_ID,
                                      ADDRESS_H_ID,
                                      ADDR_EXACT_MATCH_IND,
                                      ADDRESS_LINE1,
                                      ADDRESS_LINE2,
                                      CITY,
                                      STATE,
                                      ZIP,
                                      ZIP_PLUS4,
                                      COUNTRY,
                                      FIPS,
                                      NEW_VISN,
                                      MARKET,
                                      SUBMARKET,
                                      SECTOR,
                                      DISTRICT,
                                      STATE_FIPS,
                                      COUNTY_NAME,
                                      DISTRICT_NAME,
                                      MARKET_NAME,
                                      STATE_BY_COUNTY,
                                      URH_CODE,
                                      CONGRESSIONAL_DISTRICT,
                                      ADDR_LONGITUDE,
                                      ADDR_LATITUDE,
                                      DRIVING_DISTANCE,
                                      ADDR_LOCATION_NAME,
                                      ADDR_MATCH_TYPE,
                                      MATCH_ADDRESS,
                                      GEOCODING_FLAG,
                                      GEOCODE_SW_VER,
                                      GEOCODE_DATE,
                                      RECORD_CREATED_BY,
                                      RECORD_CREATED_DATE,
                                      RECORD_MODIFIED_BY,
                                      RECORD_MODIFIED_DATE,
                                      RECORD_MODIFIED_COUNT,
                                      VCELIGIBILITY_ID,
                                      TRANSACTION_TYPE_CODE)
          VALUES (GEOCODE_TRANSMIT_H_S.NEXTVAL,
                   :new.GEOCODE_TRANSMIT_ID,
                   :new.GEOCODE_STATION_ID,
                   :new.PERSON_ID,
                   :new.ADDRESS_H_ID,
                   :new.ADDR_EXACT_MATCH_IND,
                   :new.ADDRESS_LINE1,
                   :new.ADDRESS_LINE2,
                   :new.CITY,
                   :new.STATE,
                   :new.ZIP,
                   :new.ZIP_PLUS4,
                   :new.COUNTRY,
                   :new.FIPS,
                   :new.NEW_VISN,
                   :new.MARKET,
                   :new.SUBMARKET,
                   :new.SECTOR,
                   :new.DISTRICT,
                   :new.STATE_FIPS,
                   :new.COUNTY_NAME,
                   :new.DISTRICT_NAME,
                   :new.MARKET_NAME,
                   :new.STATE_BY_COUNTY,
                   :new.URH_CODE,
                   :new.CONGRESSIONAL_DISTRICT,
                   :new.ADDR_LONGITUDE,
                   :new.ADDR_LATITUDE,
                   :new.DRIVING_DISTANCE,
                   :new.ADDR_LOCATION_NAME,
                   :new.ADDR_MATCH_TYPE,
                   :new.MATCH_ADDRESS,
                   :new.GEOCODING_FLAG,
                   :new.GEOCODE_SW_VER,
                   :new.GEOCODE_DATE,
                   :new.RECORD_CREATED_BY,
                   :new.RECORD_CREATED_DATE,
                   :new.RECORD_MODIFIED_BY,
                   :new.RECORD_MODIFIED_DATE,
                   :new.RECORD_MODIFIED_COUNT,
                   :new.VCELIGIBILITY_ID,
                   mDML);
   END IF;
END GEOCODE_TRANSMIT_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_TRANSMIT FOR ADR.GEOCODE_TRANSMIT;


ALTER TABLE ADR.GEOCODE_TRANSMIT ADD (
  CONSTRAINT GEOCODE_TRANSMIT_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, GEOCODE_TRANSMIT_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.GEOCODE_TRANSMIT ADD (
  CONSTRAINT GEOCD_TRNSMIT_ADDR_D_FK 
  FOREIGN KEY (ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT GEOCODETRANS_VCELIG_FK 
  FOREIGN KEY (VCELIGIBILITY_ID) 
  REFERENCES ADR.VCELIGIBILITY (VCELIGIBILITY_ID)
  ENABLE VALIDATE,
  CONSTRAINT GEOCODE_STATION_FK 
  FOREIGN KEY (GEOCODE_STATION_ID) 
  REFERENCES ADR.GEOCODE_STATION (GEOCODE_STATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT TRNSMT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.GEOCODE_TRANSMIT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_TRANSMIT TO ES_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_TRANSMIT TO ES_RW;

GRANT SELECT ON ADR.GEOCODE_TRANSMIT TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT TO VETCHOICE_RW;
ALTER TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE
(
  GEOCODE_TRANSMIT_ID     NUMBER(20)            NOT NULL,
  GEOCODE_STATION_ID      NUMBER(20),
  PERSON_ID               NUMBER(20),
  ADDRESS_H_ID            NUMBER(20),
  ADDR_EXACT_MATCH_IND    NUMBER(1),
  ADDRESS_LINE1           VARCHAR2(100 BYTE),
  ADDRESS_LINE2           VARCHAR2(100 BYTE),
  CITY                    VARCHAR2(60 BYTE),
  STATE                   VARCHAR2(2 BYTE),
  ZIP                     VARCHAR2(5 BYTE),
  ZIP_PLUS4               VARCHAR2(4 BYTE),
  COUNTRY                 VARCHAR2(50 BYTE),
  FIPS                    VARCHAR2(5 BYTE),
  NEW_VISN                VARCHAR2(2 BYTE),
  MARKET                  VARCHAR2(4 BYTE),
  SUBMARKET               VARCHAR2(6 BYTE),
  SECTOR                  VARCHAR2(8 BYTE),
  DISTRICT                VARCHAR2(4 BYTE),
  STATE_FIPS              VARCHAR2(2 BYTE),
  COUNTY_NAME             VARCHAR2(255 BYTE),
  DISTRICT_NAME           VARCHAR2(255 BYTE),
  MARKET_NAME             VARCHAR2(255 BYTE),
  STATE_BY_COUNTY         VARCHAR2(2 BYTE),
  URH_CODE                VARCHAR2(1 BYTE),
  CONGRESSIONAL_DISTRICT  VARCHAR2(20 BYTE),
  ADDR_LONGITUDE          NUMBER(20),
  ADDR_LATITUDE           NUMBER(20),
  DRIVING_DISTANCE        NUMBER(20),
  ADDR_LOCATION_NAME      VARCHAR2(59 BYTE),
  ADDR_MATCH_TYPE         VARCHAR2(2 BYTE),
  MATCH_ADDRESS           VARCHAR2(120 BYTE),
  GEOCODING_FLAG          VARCHAR2(1 BYTE),
  GEOCODE_SW_VER          VARCHAR2(50 BYTE),
  GEOCODE_DATE            DATE,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL,
  VCELIGIBILITY_ID        NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION GEOCODE_TRANSMIT_ARCHIVE_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE IS 'Log of records sent to geocode service and data received';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.GEOCODE_TRANSMIT_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.GEOCODE_STATION_ID IS 'FK to ADR.GEOCODE_STATION table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDRESS_H_ID IS 'FK to ADR.ADDRESS_H table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDR_EXACT_MATCH_IND IS 'Boolean Yes/No indicator of did Geocode match exactly to the address ES sent';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDRESS_LINE1 IS 'Street Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.CITY IS 'City';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.STATE IS 'State for US (Province for non-US)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ZIP IS '5-digit ZIP code';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ZIP_PLUS4 IS 'Four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.COUNTRY IS 'Country';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.FIPS IS 'County Federal Information Processing Standard code (2 state + 3 county digits)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.NEW_VISN IS 'NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.MARKET IS 'Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.SUBMARKET IS 'Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.SECTOR IS 'Sector Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.DISTRICT IS 'VA District Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.STATE_FIPS IS 'County Federal Information Processing Standard code (2 digit state)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.COUNTY_NAME IS 'County Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.DISTRICT_NAME IS 'VA District Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.MARKET_NAME IS 'Market Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.STATE_BY_COUNTY IS 'State determined based on county data joined to file (see CountyName)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.CONGRESSIONAL_DISTRICT IS 'Congressional District';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDR_LONGITUDE IS 'Longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDR_LATITUDE IS 'Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDR_LOCATION_NAME IS 'The name of the participating locator used for matching the address';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.ADDR_MATCH_TYPE IS 'M=Matched; U=Unmatched; T=Tied';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.MATCH_ADDRESS IS 'The address where the matched location actually resides based on geocoded results';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.GEOCODING_FLAG IS 'Geocoding flag 0, 1, 2, 3, 4, 5, Null';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.GEOCODE_SW_VER IS 'Geocoder Software Version';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.GEOCODE_DATE IS 'Date file was geocoded. Format: CYYMMDD where C = Century: 1=1800, 2=1900, C=2000';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_ARCHIVE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.GEOCDTRANSARCH_VCELIG_FK_I ON ADR.GEOCODE_TRANSMIT_ARCHIVE
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.GEOCODE_TRANSMIT_ARCHIVE_PK ON ADR.GEOCODE_TRANSMIT_ARCHIVE
(RECORD_CREATED_DATE, GEOCODE_TRANSMIT_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION GEOCODE_TRANSMIT_ARCHIVE_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.GTA_PERSONID_IDX ON ADR.GEOCODE_TRANSMIT_ARCHIVE
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION GTA_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION GTA_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION GTA_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION GTA_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_TRANSMIT_ARCHIVE FOR ADR.GEOCODE_TRANSMIT_ARCHIVE;


ALTER TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE ADD (
  CONSTRAINT GEOCODE_TRANSMIT_ARCHIVE_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, GEOCODE_TRANSMIT_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.GEOCODE_TRANSMIT_ARCHIVE ADD (
  CONSTRAINT GEOCDTRANSARCH_VCELIG_FK 
  FOREIGN KEY (VCELIGIBILITY_ID) 
  REFERENCES ADR.VCELIGIBILITY (VCELIGIBILITY_ID)
  ENABLE VALIDATE,
  CONSTRAINT GEOCD_TRNSMIT_ARCH_ADDR_D_FK 
  FOREIGN KEY (ADDRESS_H_ID) 
  REFERENCES ADR.ADDRESS_H (ADDRESS_H_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT GTA_GEOCODE_STATION_FK 
  FOREIGN KEY (GEOCODE_STATION_ID) 
  REFERENCES ADR.GEOCODE_STATION (GEOCODE_STATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT GTA_TRNSMT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO ES_R;

GRANT INSERT, SELECT ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO ES_RW;

GRANT SELECT ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT_ARCHIVE TO VETCHOICE_RW;
ALTER TABLE ADR.GEOCODE_TRANSMIT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.GEOCODE_TRANSMIT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.GEOCODE_TRANSMIT_H
(
  GEOCODE_TRANSMIT_H_ID   NUMBER(20)            NOT NULL,
  GEOCODE_TRANSMIT_ID     NUMBER(20)            NOT NULL,
  GEOCODE_STATION_ID      NUMBER(20),
  PERSON_ID               NUMBER(20),
  ADDRESS_H_ID            NUMBER(20),
  ADDR_EXACT_MATCH_IND    NUMBER(1),
  ADDRESS_LINE1           VARCHAR2(100 BYTE),
  ADDRESS_LINE2           VARCHAR2(100 BYTE),
  CITY                    VARCHAR2(60 BYTE),
  STATE                   VARCHAR2(2 BYTE),
  ZIP                     VARCHAR2(5 BYTE),
  ZIP_PLUS4               VARCHAR2(4 BYTE),
  COUNTRY                 VARCHAR2(50 BYTE),
  FIPS                    VARCHAR2(5 BYTE),
  NEW_VISN                VARCHAR2(2 BYTE),
  MARKET                  VARCHAR2(4 BYTE),
  SUBMARKET               VARCHAR2(6 BYTE),
  SECTOR                  VARCHAR2(8 BYTE),
  DISTRICT                VARCHAR2(4 BYTE),
  STATE_FIPS              VARCHAR2(2 BYTE),
  COUNTY_NAME             VARCHAR2(255 BYTE),
  DISTRICT_NAME           VARCHAR2(255 BYTE),
  MARKET_NAME             VARCHAR2(255 BYTE),
  STATE_BY_COUNTY         VARCHAR2(2 BYTE),
  URH_CODE                VARCHAR2(1 BYTE),
  CONGRESSIONAL_DISTRICT  VARCHAR2(20 BYTE),
  ADDR_LONGITUDE          NUMBER(20),
  ADDR_LATITUDE           NUMBER(20),
  DRIVING_DISTANCE        NUMBER(20),
  ADDR_LOCATION_NAME      VARCHAR2(59 BYTE),
  ADDR_MATCH_TYPE         VARCHAR2(2 BYTE),
  MATCH_ADDRESS           VARCHAR2(120 BYTE),
  GEOCODING_FLAG          VARCHAR2(1 BYTE),
  GEOCODE_SW_VER          VARCHAR2(200 BYTE),
  GEOCODE_DATE            DATE,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL,
  VCELIGIBILITY_ID        NUMBER(20),
  TRANSACTION_TYPE_CODE   CHAR(1 BYTE)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION GEOCDTRANS_P01 VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.GEOCODE_TRANSMIT_H IS 'Historical log of records sent to geocode service and data received';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODE_TRANSMIT_H_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODE_TRANSMIT_ID IS 'FK key index referenced from GEOCODE_TRANSMIT';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODE_STATION_ID IS 'FK to ADR.GEOCODE_STATION table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDRESS_H_ID IS 'FK to ADR.ADDRESS_H table';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDR_EXACT_MATCH_IND IS 'Boolean Yes/No indicator of did Geocode match exactly to the address ES sent';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDRESS_LINE1 IS 'Street Address line 1';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDRESS_LINE2 IS 'Street Address line 2';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.CITY IS 'City';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.STATE IS 'State for US (Province for non-US)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ZIP IS '5-digit ZIP code';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ZIP_PLUS4 IS 'Four-digit ZIP code extension';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.COUNTRY IS 'Country';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.FIPS IS 'County Federal Information Processing Standard code (2 state + 3 county digits)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.NEW_VISN IS 'NEW Veterans Integrated Service Network identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.MARKET IS 'Market Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.SUBMARKET IS 'Submarket Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.SECTOR IS 'Sector Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.DISTRICT IS 'VA District Identifier';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.STATE_FIPS IS 'County Federal Information Processing Standard code (2 digit state)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.COUNTY_NAME IS 'County Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.DISTRICT_NAME IS 'VA District Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.MARKET_NAME IS 'Market Name';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.STATE_BY_COUNTY IS 'State determined based on county data joined to file (see CountyName)';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.URH_CODE IS 'Rurality flag: U=Urban; R=Rural; H=Highly rural';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.CONGRESSIONAL_DISTRICT IS 'Congressional District';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDR_LONGITUDE IS 'Longitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDR_LATITUDE IS 'Latitude of site location, relative to WGS1984 datum';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.DRIVING_DISTANCE IS 'Driving Distance to nearest  station';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDR_LOCATION_NAME IS 'The name of the participating locator used for matching the address';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.ADDR_MATCH_TYPE IS 'M=Matched; U=Unmatched; T=Tied';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.MATCH_ADDRESS IS 'The address where the matched location actually resides based on geocoded results';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODING_FLAG IS 'Geocoding flag 0, 1, 2, 3, 4, 5, Null';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODE_SW_VER IS 'Geocoder Software Version';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.GEOCODE_DATE IS 'Date file was geocoded. Format: CYYMMDD where C = Century: 1=1800, 2=1900, C=2000';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.GEOCODE_TRANSMIT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.GEOCDTTNS_H_CRDTGEOTRNSID_NU_I ON ADR.GEOCODE_TRANSMIT_H
(RECORD_CREATED_DATE, GEOCODE_TRANSMIT_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION GEOCDTRANS_P01
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.GEOCDTTNS_H_PERSONID_NU_I ON ADR.GEOCODE_TRANSMIT_H
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION GEOCDTRNS_H_PERSONID_IDX_P1
    TABLESPACE L_INDEX_01,  
  PARTITION GEOCDTRNS_H_PERSONID_IDX_P2
    TABLESPACE L_INDEX_01,  
  PARTITION GEOCDTRNS_H_PERSONID_IDX_P3
    TABLESPACE L_INDEX_01,  
  PARTITION GEOCDTRNS_H_PERSONID_IDX_P4
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE INDEX ADR.GEOCDTTNS_H_VCELIG_NU_I ON ADR.GEOCODE_TRANSMIT_H
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.GEOCODE_TRANSMIT_H_PK_I ON ADR.GEOCODE_TRANSMIT_H
(GEOCODE_TRANSMIT_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM GEOCODE_TRANSMIT_H FOR ADR.GEOCODE_TRANSMIT_H;


ALTER TABLE ADR.GEOCODE_TRANSMIT_H ADD (
  CONSTRAINT GEOCODE_TRANSMIT_H_PK
  PRIMARY KEY
  (GEOCODE_TRANSMIT_H_ID)
  USING INDEX ADR.GEOCODE_TRANSMIT_H_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.GEOCODE_TRANSMIT_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT_H TO ADR_RW;

GRANT SELECT ON ADR.GEOCODE_TRANSMIT_H TO ES_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.GEOCODE_TRANSMIT_H TO ES_RW;
ALTER TABLE ADR.HANDBOOK_BATCH_REQUEST
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_BATCH_REQUEST CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_BATCH_REQUEST
(
  HANDBOOK_BATCH_REQUEST_ID       NUMBER(20)    NOT NULL,
  BATCH_RELEASE_DATE              DATE          NOT NULL,
  BATCH_CANCEL_DATE               DATE,
  MAX_PER_JOB_NUMBER              NUMBER(10),
  BATCH_RELEASE_SIZE              NUMBER(10),
  NOTE                            VARCHAR2(400 BYTE),
  STD_HANDBOOKRELEASECNTL_ID      NUMBER(20)    NOT NULL,
  STD_HANDBOOKBATCHREQUESTSTA_ID  NUMBER(20)    NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_BATCH_REQUEST IS 'A batch request for generation of documents';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.MAX_PER_JOB_NUMBER IS 'Maximum number of handbooks generated per request. Set by the user.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.BATCH_RELEASE_SIZE IS 'Actual number of veterans documents scheduled for this batch request';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.HNDBATREQ_PK ON ADR.HANDBOOK_BATCH_REQUEST
(HANDBOOK_BATCH_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDBATREQ_STDHDBRELCNT_FK_I ON ADR.HANDBOOK_BATCH_REQUEST
(STD_HANDBOOKRELEASECNTL_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDBATREQ_STDHNDREQSTA_FK_I ON ADR.HANDBOOK_BATCH_REQUEST
(STD_HANDBOOKBATCHREQUESTSTA_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HANDBOOK_BATCH_REQ_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.HANDBOOK_BATCH_REQUEST  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into HANDBOOK_BATCH_REQUEST_H
        (
        HANDBOOK_BATCH_REQUEST_H_ID,
        HANDBOOK_BATCH_REQUEST_ID,
    MAX_PER_JOB_NUMBER,
    BATCH_RELEASE_DATE,
    BATCH_RELEASE_SIZE,
    BATCH_CANCEL_DATE,
    NOTE,
    STD_HANDBOOKRELEASECNTL_ID,
    STD_HANDBOOKBATCHREQUESTSTA_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_BATCH_REQUEST_H_S.nextval,
        :old.HANDBOOK_BATCH_REQUEST_ID,
        :old.MAX_PER_JOB_NUMBER,
        :old.BATCH_RELEASE_DATE,
        :old.BATCH_RELEASE_SIZE,
        :old.BATCH_CANCEL_DATE,
        :old.NOTE,
        :old.STD_HANDBOOKRELEASECNTL_ID,
       :OLD.STD_HANDBOOKBATCHREQUESTSTA_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into HANDBOOK_BATCH_REQUEST_H
        (
        HANDBOOK_BATCH_REQUEST_H_ID,
        HANDBOOK_BATCH_REQUEST_ID,
    MAX_PER_JOB_NUMBER,
    BATCH_RELEASE_DATE,
    BATCH_RELEASE_SIZE,
    BATCH_CANCEL_DATE,
    NOTE,
    STD_HANDBOOKRELEASECNTL_ID,
  STD_HANDBOOKBATCHREQUESTSTA_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_BATCH_REQUEST_H_S.nextval,
        :new.HANDBOOK_BATCH_REQUEST_ID,
        :new.MAX_PER_JOB_NUMBER,
        :new.BATCH_RELEASE_DATE,
        :new.BATCH_RELEASE_SIZE,
        :new.BATCH_CANCEL_DATE,
        :new.NOTE,
        :new.STD_HANDBOOKRELEASECNTL_ID,
        :new.STD_HANDBOOKBATCHREQUESTSTA_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end HANDBOOK_BATCH_REQ_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_BATCH_REQUEST FOR ADR.HANDBOOK_BATCH_REQUEST;


ALTER TABLE ADR.HANDBOOK_BATCH_REQUEST ADD (
  CONSTRAINT HNDBATREQ_PK
  PRIMARY KEY
  (HANDBOOK_BATCH_REQUEST_ID)
  USING INDEX ADR.HNDBATREQ_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HANDBOOK_BATCH_REQUEST ADD (
  CONSTRAINT R_1051 
  FOREIGN KEY (STD_HANDBOOKRELEASECNTL_ID) 
  REFERENCES ADRREF.STD_HANDBOOKRELEASECONTROL (ID)
  ENABLE VALIDATE,
  CONSTRAINT R_1064 
  FOREIGN KEY (STD_HANDBOOKBATCHREQUESTSTA_ID) 
  REFERENCES ADRREF.STD_HANDBOOKBATCHREQUESTSTATUS (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_BATCH_REQUEST TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_BATCH_REQUEST TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_BATCH_REQUEST TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_BATCH_REQUEST TO ES_RW;
ALTER TABLE ADR.HANDBOOK_BATCH_REQUEST_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_BATCH_REQUEST_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_BATCH_REQUEST_H
(
  HANDBOOK_BATCH_REQUEST_ID       NUMBER(20)    NOT NULL,
  BATCH_RELEASE_DATE              DATE          NOT NULL,
  BATCH_CANCEL_DATE               DATE,
  MAX_PER_JOB_NUMBER              NUMBER(10),
  BATCH_RELEASE_SIZE              NUMBER(10),
  NOTE                            VARCHAR2(400 BYTE),
  STD_HANDBOOKRELEASECNTL_ID      NUMBER(20)    NOT NULL,
  STD_HANDBOOKBATCHREQUESTSTA_ID  NUMBER(20)    NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  HISTORY_CREATED_DATE            DATE          NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  HANDBOOK_BATCH_REQUEST_H_ID     NUMBER(20)    NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_BATCH_REQUEST_H IS 'A batch request for generation of documents';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.MAX_PER_JOB_NUMBER IS 'Maximum number of handbooks generated per request. Set by the user.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.BATCH_RELEASE_SIZE IS 'Actual number of veterans documents scheduled for this batch request';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HANDBOOK_BATCH_REQUEST_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.HNDBATREQH_PK ON ADR.HANDBOOK_BATCH_REQUEST_H
(HANDBOOK_BATCH_REQUEST_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_BATCH_REQUEST_H FOR ADR.HANDBOOK_BATCH_REQUEST_H;


ALTER TABLE ADR.HANDBOOK_BATCH_REQUEST_H ADD (
  CONSTRAINT HNDBATREQH_PK
  PRIMARY KEY
  (HANDBOOK_BATCH_REQUEST_H_ID)
  USING INDEX ADR.HNDBATREQH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_BATCH_REQUEST_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_BATCH_REQUEST_H TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_BATCH_REQUEST_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_BATCH_REQUEST_H TO ES_RW;
ALTER TABLE ADR.HANDBOOK_DOCUMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_DOCUMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_DOCUMENT
(
  HANDBOOK_DOCUMENT_ID     NUMBER(20),
  DOCUMENT_TEXT            CLOB,
  HANDBOOK_MAIL_STATUS_ID  NUMBER(20),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE      DATE,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE     DATE,
  RECORD_MODIFIED_COUNT    NUMBER(10)
)
LOB (DOCUMENT_TEXT) STORE AS (
  TABLESPACE  L_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_DOCUMENT IS 'Text of the Handbook sent to the Configuration Management System (CMS).';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.DOCUMENT_TEXT IS 'Contents of the Handbook as sent to the Content Management System (CMS)';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_DOCUMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HNDDOC_HNDMALSTA_FK_I ON ADR.HANDBOOK_DOCUMENT
(HANDBOOK_MAIL_STATUS_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDDOC_PK ON ADR.HANDBOOK_DOCUMENT
(HANDBOOK_DOCUMENT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_DOCUMENT FOR ADR.HANDBOOK_DOCUMENT;


ALTER TABLE ADR.HANDBOOK_DOCUMENT ADD (
  CONSTRAINT SYS_C00186897
  CHECK ("HANDBOOK_DOCUMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186898
  CHECK ("HANDBOOK_MAIL_STATUS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186899
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186900
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186901
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186902
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186903
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT HNDDOC_PK
  PRIMARY KEY
  (HANDBOOK_DOCUMENT_ID)
  USING INDEX ADR.HNDDOC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HANDBOOK_DOCUMENT ADD (
  CONSTRAINT R_1059 
  FOREIGN KEY (HANDBOOK_MAIL_STATUS_ID) 
  REFERENCES ADR.HANDBOOK_MAIL_STATUS (HANDBOOK_MAIL_STATUS_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.HANDBOOK_DOCUMENT TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_DOCUMENT TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_DOCUMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_DOCUMENT TO ES_RW;
ALTER TABLE ADR.HANDBOOK_MAIL_QUEUE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_MAIL_QUEUE CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_MAIL_QUEUE
(
  HANDBOOK_MAIL_QUEUE_ID      NUMBER(20),
  COM_LETTER_TEMPLATE_ID      NUMBER(20),
  REMARK                      VARCHAR2(400 BYTE),
  STD_HANDBOOKRELEASECNTL_ID  NUMBER(20),
  HANDBOOK_BATCH_REQUEST_ID   NUMBER(20),
  PERSON_ID                   NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE         DATE,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE        DATE,
  RECORD_MODIFIED_COUNT       NUMBER(10),
  ADDRESS_ID                  NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_MAIL_QUEUE IS 'Veterans Health Benefits Communication created for a specific Veteran.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.HANDBOOK_MAIL_QUEUE_ID IS 'The identifier associated with a document request.
For example, the CMS Communications ID / Receipt String associated with the document request from ESR.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.COM_LETTER_TEMPLATE_ID IS 'HandBook Form Number.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HNDMALQUE_HNDBATREQ_FK ON ADR.HANDBOOK_MAIL_QUEUE
(HANDBOOK_BATCH_REQUEST_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDMALQUE_PER_FK ON ADR.HANDBOOK_MAIL_QUEUE
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDMALQUE_PK ON ADR.HANDBOOK_MAIL_QUEUE
(HANDBOOK_MAIL_QUEUE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDMALQUE_STDHNDBKRELCNT_FK ON ADR.HANDBOOK_MAIL_QUEUE
(STD_HANDBOOKRELEASECNTL_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HANDBOOK_MAIL_QUEUE_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."HANDBOOK_MAIL_QUEUE" FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into HANDBOOK_MAIL_QUEUE_H
        (
        HANDBOOK_MAIL_QUEUE_H_ID,
        HANDBOOK_MAIL_QUEUE_ID,
    COM_LETTER_TEMPLATE_ID,
    REMARK,
    STD_HANDBOOKRELEASECNTL_ID,
    PERSON_ID,
    ADDRESS_ID,
    HANDBOOK_BATCH_REQUEST_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_MAIL_QUEUE_H_S.nextval,
        :old.HANDBOOK_MAIL_QUEUE_ID,
    :old.COM_LETTER_TEMPLATE_ID,
    :old.REMARK,
    :old.STD_HANDBOOKRELEASECNTL_ID,
    :old.PERSON_ID,
    :old.ADDRESS_ID,
    :old.HANDBOOK_BATCH_REQUEST_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into HANDBOOK_MAIL_QUEUE_H
        (
        HANDBOOK_MAIL_QUEUE_H_ID,
        HANDBOOK_MAIL_QUEUE_ID,
    COM_LETTER_TEMPLATE_ID,
    REMARK,
    STD_HANDBOOKRELEASECNTL_ID,
    PERSON_ID,
    ADDRESS_ID,
    HANDBOOK_BATCH_REQUEST_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_MAIL_QUEUE_H_S.nextval,
        :new.HANDBOOK_MAIL_QUEUE_ID,
    :new.COM_LETTER_TEMPLATE_ID,
    :new.REMARK,
    :new.STD_HANDBOOKRELEASECNTL_ID,
    :new.PERSON_ID,
    :new.ADDRESS_ID,
    :new.HANDBOOK_BATCH_REQUEST_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end HANDBOOK_MAIL_QUEUE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_MAIL_QUEUE FOR ADR.HANDBOOK_MAIL_QUEUE;


ALTER TABLE ADR.HANDBOOK_MAIL_QUEUE ADD (
  CONSTRAINT SYS_C00186904
  CHECK ("HANDBOOK_MAIL_QUEUE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186905
  CHECK ("COM_LETTER_TEMPLATE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186906
  CHECK ("STD_HANDBOOKRELEASECNTL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186907
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186908
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186909
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186910
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186911
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186912
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT COMLOG_PK
  PRIMARY KEY
  (HANDBOOK_MAIL_QUEUE_ID)
  USING INDEX ADR.HNDMALQUE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HANDBOOK_MAIL_QUEUE ADD (
  CONSTRAINT R_1050 
  FOREIGN KEY (STD_HANDBOOKRELEASECNTL_ID) 
  REFERENCES ADRREF.STD_HANDBOOKRELEASECONTROL (ID)
  ENABLE VALIDATE,
  CONSTRAINT R_1053 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT R_1065 
  FOREIGN KEY (HANDBOOK_BATCH_REQUEST_ID) 
  REFERENCES ADR.HANDBOOK_BATCH_REQUEST (HANDBOOK_BATCH_REQUEST_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_MAIL_QUEUE TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_MAIL_QUEUE TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_MAIL_QUEUE TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_MAIL_QUEUE TO ES_RW;
ALTER TABLE ADR.HANDBOOK_MAIL_QUEUE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_MAIL_QUEUE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_MAIL_QUEUE_H
(
  HANDBOOK_MAIL_QUEUE_ID      NUMBER(20),
  COM_LETTER_TEMPLATE_ID      NUMBER(20),
  REMARK                      VARCHAR2(400 BYTE),
  STD_HANDBOOKRELEASECNTL_ID  NUMBER(20),
  HANDBOOK_BATCH_REQUEST_ID   NUMBER(20),
  PERSON_ID                   NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE         DATE,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE        DATE,
  RECORD_MODIFIED_COUNT       NUMBER(10),
  HISTORY_CREATED_DATE        DATE,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE),
  HANDBOOK_MAIL_QUEUE_H_ID    NUMBER(20),
  ADDRESS_ID                  NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_MAIL_QUEUE_H IS 'Veterans Health Benefits Communication created for a specific Veteran.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.COM_LETTER_TEMPLATE_ID IS 'HandBook Form Number.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_QUEUE_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.HNDBK_MAIL_QUEID_NU ON ADR.HANDBOOK_MAIL_QUEUE_H
(HANDBOOK_MAIL_QUEUE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDMALQUEH_PK ON ADR.HANDBOOK_MAIL_QUEUE_H
(HANDBOOK_MAIL_QUEUE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_MAIL_QUEUE_H FOR ADR.HANDBOOK_MAIL_QUEUE_H;


ALTER TABLE ADR.HANDBOOK_MAIL_QUEUE_H ADD (
  CONSTRAINT SYS_C00186913
  CHECK ("HANDBOOK_MAIL_QUEUE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186914
  CHECK ("COM_LETTER_TEMPLATE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186915
  CHECK ("STD_HANDBOOKRELEASECNTL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186916
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186917
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186918
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186919
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186920
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186921
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186922
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186923
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186924
  CHECK ("HANDBOOK_MAIL_QUEUE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT HNDMALQUEH_PK
  PRIMARY KEY
  (HANDBOOK_MAIL_QUEUE_H_ID)
  USING INDEX ADR.HNDMALQUEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_MAIL_QUEUE_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_MAIL_QUEUE_H TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_MAIL_QUEUE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_MAIL_QUEUE_H TO ES_RW;
ALTER TABLE ADR.HANDBOOK_MAIL_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_MAIL_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_MAIL_STATUS
(
  HANDBOOK_MAIL_STATUS_ID      NUMBER(20),
  STD_COMREJECTREASONTYPE_ID   NUMBER(20),
  HANDBOOK_MAIL_QUEUE_ID       NUMBER(20),
  CMS_FILE_GENERATED_DATE      DATE,
  SENT_TO_USPS_DATE            DATE,
  RETURNED_FROM_USPS_DATE      DATE,
  ERROR_TEXT                   VARCHAR2(400 BYTE),
  STD_HANDBOOKMAILSTATTYPE_ID  NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_MAIL_STATUS IS 'Status of a handbook mailing.  HANDBOOK_MAIL_QUEUE table holds the base request and HANDBOOK_MAIL_STATUS holds the corresponding status and associated fields. Every time the status changes, the corresponding fields are reset in the HANDBOOK_MAIL_STATUS table. For example, if the status changes to resend, then set nulls to fields for errors, dates, etc.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.HANDBOOK_MAIL_QUEUE_ID IS 'The identifier associated with a document request.
For example, the CMS Communications ID / Receipt String associated with the document request from ESR.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.SENT_TO_USPS_DATE IS 'Mailing sent to USPS date.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HNDMALSTA_HNDMALQUE_FK ON ADR.HANDBOOK_MAIL_STATUS
(HANDBOOK_MAIL_QUEUE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDMALSTA_PK ON ADR.HANDBOOK_MAIL_STATUS
(HANDBOOK_MAIL_STATUS_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDMALSTA_STDCOMREJREATYP_FK ON ADR.HANDBOOK_MAIL_STATUS
(STD_COMREJECTREASONTYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDMALSTA_STDHNDBKMALSTATYP_FK ON ADR.HANDBOOK_MAIL_STATUS
(STD_HANDBOOKMAILSTATTYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HANDBOOK_MAIL_STATUS_AUD_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."HANDBOOK_MAIL_STATUS" FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into HANDBOOK_MAIL_STATUS_H
        (
        HANDBOOK_MAIL_STATUS_H_ID,
        HANDBOOK_MAIL_STATUS_ID,
    CMS_FILE_GENERATED_DATE,
    SENT_TO_USPS_DATE,
    HANDBOOK_MAIL_QUEUE_ID,
    STD_COMREJECTREASONTYPE_ID,
    STD_HANDBOOKMAILSTATTYPE_ID,
    ERROR_TEXT,
    RETURNED_FROM_USPS_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_MAIL_STATUS_H_S.nextval,
        :old.HANDBOOK_MAIL_STATUS_ID,
    :old.CMS_FILE_GENERATED_DATE,
    :old.SENT_TO_USPS_DATE,
    :old.HANDBOOK_MAIL_QUEUE_ID,
    :old.STD_COMREJECTREASONTYPE_ID,
    :old.STD_HANDBOOKMAILSTATTYPE_ID,
    :old.ERROR_TEXT,
    :old.RETURNED_FROM_USPS_DATE,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into HANDBOOK_MAIL_STATUS_H
        (
        HANDBOOK_MAIL_STATUS_H_ID,
        HANDBOOK_MAIL_STATUS_ID,
    CMS_FILE_GENERATED_DATE,
    SENT_TO_USPS_DATE,
    HANDBOOK_MAIL_QUEUE_ID,
    STD_COMREJECTREASONTYPE_ID,
    STD_HANDBOOKMAILSTATTYPE_ID,
    ERROR_TEXT,
    RETURNED_FROM_USPS_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HANDBOOK_MAIL_STATUS_H_S.nextval,
        :new.HANDBOOK_MAIL_STATUS_ID,
    :new.CMS_FILE_GENERATED_DATE,
    :new.SENT_TO_USPS_DATE,
    :new.HANDBOOK_MAIL_QUEUE_ID,
    :new.STD_COMREJECTREASONTYPE_ID,
    :new.STD_HANDBOOKMAILSTATTYPE_ID,
    :new.ERROR_TEXT,
    :new.RETURNED_FROM_USPS_DATE,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end HANDBOOK_MAIL_STATUS_AUD_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_MAIL_STATUS FOR ADR.HANDBOOK_MAIL_STATUS;


ALTER TABLE ADR.HANDBOOK_MAIL_STATUS ADD (
  CONSTRAINT SYS_C00186925
  CHECK ("HANDBOOK_MAIL_STATUS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186926
  CHECK ("HANDBOOK_MAIL_QUEUE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186927
  CHECK ("STD_HANDBOOKMAILSTATTYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186928
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186929
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186930
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186931
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186932
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT HNDMALSTA_PK
  PRIMARY KEY
  (HANDBOOK_MAIL_STATUS_ID)
  USING INDEX ADR.HNDMALSTA_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HANDBOOK_MAIL_STATUS ADD (
  CONSTRAINT R_1057 
  FOREIGN KEY (HANDBOOK_MAIL_QUEUE_ID) 
  REFERENCES ADR.HANDBOOK_MAIL_QUEUE (HANDBOOK_MAIL_QUEUE_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT R_1058 
  FOREIGN KEY (STD_COMREJECTREASONTYPE_ID) 
  REFERENCES ADRREF.STD_COMREJECTREASONTYPE (ID)
  ENABLE VALIDATE,
  CONSTRAINT R_1060 
  FOREIGN KEY (STD_HANDBOOKMAILSTATTYPE_ID) 
  REFERENCES ADRREF.STD_HANDBOOKMAILSTATTYPE (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_MAIL_STATUS TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_MAIL_STATUS TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_MAIL_STATUS TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_MAIL_STATUS TO ES_RW;
ALTER TABLE ADR.HANDBOOK_MAIL_STATUS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_MAIL_STATUS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_MAIL_STATUS_H
(
  HANDBOOK_MAIL_STATUS_ID      NUMBER(20),
  STD_COMREJECTREASONTYPE_ID   NUMBER(20),
  HANDBOOK_MAIL_QUEUE_ID       NUMBER(20),
  CMS_FILE_GENERATED_DATE      DATE,
  SENT_TO_USPS_DATE            DATE,
  RETURNED_FROM_USPS_DATE      DATE,
  ERROR_TEXT                   VARCHAR2(400 BYTE),
  STD_HANDBOOKMAILSTATTYPE_ID  NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  HISTORY_CREATED_DATE         DATE,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  HANDBOOK_MAIL_STATUS_H_ID    NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_MAIL_STATUS_H IS 'Status of a handbook mailing.  HANDBOOK_MAIL_QUEUE table holds the base request and HANDBOOK_MAIL_STATUS holds the corresponding status and associated fields. Every time the status changes, the corresponding fields are reset in the HANDBOOK_MAIL_STATUS table. For example, if the status changes to resend, then set nulls to fields for errors, dates, etc.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.SENT_TO_USPS_DATE IS 'Mailing sent to USPS date.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HANDBOOK_MAIL_STATUS_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.HNDMALSTAH_CMSFLGENDT_NU_I ON ADR.HANDBOOK_MAIL_STATUS_H
(CMS_FILE_GENERATED_DATE, STD_HANDBOOKMAILSTATTYPE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDMALSTAH_HNDMALQUE_NU_I ON ADR.HANDBOOK_MAIL_STATUS_H
(HANDBOOK_MAIL_QUEUE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDMALSTAH_PK ON ADR.HANDBOOK_MAIL_STATUS_H
(HANDBOOK_MAIL_STATUS_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_MAIL_STATUS_H FOR ADR.HANDBOOK_MAIL_STATUS_H;


ALTER TABLE ADR.HANDBOOK_MAIL_STATUS_H ADD (
  CONSTRAINT SYS_C00186933
  CHECK ("HANDBOOK_MAIL_STATUS_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186934
  CHECK ("HANDBOOK_MAIL_QUEUE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186935
  CHECK ("STD_HANDBOOKMAILSTATTYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186936
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186937
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186938
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186939
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186940
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186941
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186942
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00186943
  CHECK ("HANDBOOK_MAIL_STATUS_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT HNDMALSTAH_PK
  PRIMARY KEY
  (HANDBOOK_MAIL_STATUS_H_ID)
  USING INDEX ADR.HNDMALSTAH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_MAIL_STATUS_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_MAIL_STATUS_H TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_MAIL_STATUS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_MAIL_STATUS_H TO ES_RW;
ALTER TABLE ADR.HANDBOOK_REQUEST_VETERAN
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_REQUEST_VETERAN CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_REQUEST_VETERAN
(
  HANDBOOK_REQUEST_VETERAN_ID  NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  HANDBOOK_BATCH_REQUEST_ID    NUMBER(20)       NOT NULL,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_REQUEST_VETERAN IS 'A veteran included in a batch request for document generation.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HNDREQVET_HNDBCHREQ_FK_I ON ADR.HANDBOOK_REQUEST_VETERAN
(HANDBOOK_BATCH_REQUEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HNDREQVET_PK ON ADR.HANDBOOK_REQUEST_VETERAN
(HANDBOOK_REQUEST_VETERAN_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HNDREQVET_PRSN_FK_I ON ADR.HANDBOOK_REQUEST_VETERAN
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HANDBOOK_REQ_VET_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.HANDBOOK_REQUEST_VETERAN  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into HANDBOOK_REQUEST_VETERAN_H
        (
       HANDBOOK_REQUEST_VETERAN_H_ID,
       HANDBOOK_REQUEST_VETERAN_ID,
        HANDBOOK_BATCH_REQUEST_ID,
        PERSON_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
       HANDBOOK_REQUEST_VETERAN_H_S.nextval,
        :old.HANDBOOK_REQUEST_VETERAN_ID,
        :old.HANDBOOK_BATCH_REQUEST_ID,
        :old.PERSON_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into HANDBOOK_REQUEST_VETERAN_H
        (
       HANDBOOK_REQUEST_VETERAN_H_ID,
       HANDBOOK_REQUEST_VETERAN_ID,
        HANDBOOK_BATCH_REQUEST_ID,
        PERSON_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
       HANDBOOK_REQUEST_VETERAN_H_S.nextval,
        :new.HANDBOOK_REQUEST_VETERAN_ID,
        :new.HANDBOOK_BATCH_REQUEST_ID,
        :new.PERSON_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end HANDBOOK_REQ_VET_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_REQUEST_VETERAN FOR ADR.HANDBOOK_REQUEST_VETERAN;


ALTER TABLE ADR.HANDBOOK_REQUEST_VETERAN ADD (
  CONSTRAINT BATREQVET_PK
  PRIMARY KEY
  (HANDBOOK_REQUEST_VETERAN_ID)
  USING INDEX ADR.HNDREQVET_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HANDBOOK_REQUEST_VETERAN ADD (
  CONSTRAINT RELATIONSHIP69 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ON DELETE SET NULL
  ENABLE NOVALIDATE,
  CONSTRAINT R_1063 
  FOREIGN KEY (HANDBOOK_BATCH_REQUEST_ID) 
  REFERENCES ADR.HANDBOOK_BATCH_REQUEST (HANDBOOK_BATCH_REQUEST_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_REQUEST_VETERAN TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_REQUEST_VETERAN TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_REQUEST_VETERAN TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_REQUEST_VETERAN TO ES_RW;
ALTER TABLE ADR.HANDBOOK_REQUEST_VETERAN_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HANDBOOK_REQUEST_VETERAN_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HANDBOOK_REQUEST_VETERAN_H
(
  HANDBOOK_REQUEST_VETERAN_ID    NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  HANDBOOK_BATCH_REQUEST_ID      NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  HANDBOOK_REQUEST_VETERAN_H_ID  NUMBER(20)     NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HANDBOOK_REQUEST_VETERAN_H IS 'A veteran included in a batch request for document generation.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HANDBOOK_REQUEST_VETERAN_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.HNDREQVETH_PK ON ADR.HANDBOOK_REQUEST_VETERAN_H
(HANDBOOK_REQUEST_VETERAN_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP69 ON ADR.HANDBOOK_REQUEST_VETERAN_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HANDBOOK_REQUEST_VETERAN_H FOR ADR.HANDBOOK_REQUEST_VETERAN_H;


ALTER TABLE ADR.HANDBOOK_REQUEST_VETERAN_H ADD (
  CONSTRAINT HNDREQVETH_PK
  PRIMARY KEY
  (HANDBOOK_REQUEST_VETERAN_H_ID)
  USING INDEX ADR.HNDREQVETH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HANDBOOK_REQUEST_VETERAN_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HANDBOOK_REQUEST_VETERAN_H TO ADR_RW;

GRANT SELECT ON ADR.HANDBOOK_REQUEST_VETERAN_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HANDBOOK_REQUEST_VETERAN_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFITS_APPLICATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFITS_APPLICATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFITS_APPLICATION
(
  HEALTH_BENEFITS_APPLICATION_ID  NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20),
  APPLICATION_DATE                DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  STD_HEALTHBENEFITTYPE_ID        NUMBER(20),
  Z07_FROM_VISTA_IND              VARCHAR2(1 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFITS_APPLICATION IS 'Basic information about an application made for healthcare benefits. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.HEALTH_BENEFITS_APPLICATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.STD_INSTITUTION_ID IS 'Site at which the application was made.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.APPLICATION_DATE IS 'BR2260 Enrollment Application Date is defined as the date of the application for enrollment. Migrated and calculated for each veteran with an enrollment record.. ';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.STD_HEALTHBENEFITTYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION.Z07_FROM_VISTA_IND IS 'Indicates whether or not the HL7 message for this person has come from VISTA - this is actually a derived value that is persisted in the database in order to save repeated queries on the huge HL7 log table';


CREATE UNIQUE INDEX ADR.HEALTHAPP_PK ON ADR.HEALTH_BENEFITS_APPLICATION
(HEALTH_BENEFITS_APPLICATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HEALTHAPP_PRSN_FK_I ON ADR.HEALTH_BENEFITS_APPLICATION
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HEALTHAPP_STDHEALTH_FK_I ON ADR.HEALTH_BENEFITS_APPLICATION
(STD_HEALTHBENEFITTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HEALTHAPP_STDINST_FK_I ON ADR.HEALTH_BENEFITS_APPLICATION
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HEALTH_BENF_APPLN_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.HEALTH_BENEFITS_APPLICATION for each row
declare
    mDML char(1);
begin

  if DELETING then
    insert into HEALTH_BENEFITS_APPLICATION_H
    (
    HEALTH_BENEFITS_APPLN_H_ID,
    HEALTH_BENEFITS_APPLICATION_ID,
    PERSON_ID,
    STD_INSTITUTION_ID,
    APPLICATION_DATE,
    STD_HEALTHBENEFITTYPE_ID,
    Z07_FROM_VISTA_IND,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    TRANSACTION_TYPE_CODE
        )
    values
    (
    HEALTH_BENEFITS_APP_H_S.nextval,
    :old.HEALTH_BENEFITS_APPLICATION_ID,
    :old.PERSON_ID,
    :old.STD_INSTITUTION_ID,
    :old.APPLICATION_DATE,
    :old.STD_HEALTHBENEFITTYPE_ID,
    :old.Z07_FROM_VISTA_IND,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY,
    :old.RECORD_MODIFIED_DATE,
    :old.RECORD_MODIFIED_COUNT,
    'D'
    );

  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

    insert into HEALTH_BENEFITS_APPLICATION_H
    (
    HEALTH_BENEFITS_APPLN_H_ID,
    HEALTH_BENEFITS_APPLICATION_ID,
    PERSON_ID,
    STD_INSTITUTION_ID,
    APPLICATION_DATE,
    STD_HEALTHBENEFITTYPE_ID,
    Z07_FROM_VISTA_IND,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    TRANSACTION_TYPE_CODE
        )
    values
    (
    HEALTH_BENEFITS_APP_H_S.nextval,
    :new.HEALTH_BENEFITS_APPLICATION_ID,
    :new.PERSON_ID,
    :new.STD_INSTITUTION_ID,
    :new.APPLICATION_DATE,
    :new.STD_HEALTHBENEFITTYPE_ID,
    :new.Z07_FROM_VISTA_IND,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY,
    :new.RECORD_MODIFIED_DATE,
    :new.RECORD_MODIFIED_COUNT,
    mDML
    );

  end if;

end  HEALTH_BENF_APPLN_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFITS_APPLICATION FOR ADR.HEALTH_BENEFITS_APPLICATION;


ALTER TABLE ADR.HEALTH_BENEFITS_APPLICATION ADD (
  CONSTRAINT HEALTHAPP_PK
  PRIMARY KEY
  (HEALTH_BENEFITS_APPLICATION_ID)
  USING INDEX ADR.HEALTHAPP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFITS_APPLICATION ADD (
  CONSTRAINT HEALTHAPP_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION TO ADREXT;

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFITS_APPLICATION TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFITS_APPLICATION TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFITS_APPLICATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFITS_APPLICATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFITS_APPLICATION_H
(
  HEALTH_BENEFITS_APPLN_H_ID      NUMBER(20)    NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  HEALTH_BENEFITS_APPLICATION_ID  NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20),
  APPLICATION_DATE                DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  STD_HEALTHBENEFITTYPE_ID        NUMBER(20),
  Z07_FROM_VISTA_IND              VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFITS_APPLICATION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.HEALTH_BENEFITS_APPLN_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.HEALTH_BENEFITS_APPLICATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.APPLICATION_DATE IS 'BR2260 Enrollment Application Date is defined as the date of the application for enrollment.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_BENEFITS_APPLICATION_H.Z07_FROM_VISTA_IND IS 'Indicates whether or not the HL7 message for this person has come from VISTA - this is actually a derived value that is persisted in the database in order to save repeated queries on the huge HL7 log table';


CREATE UNIQUE INDEX ADR.HEALTHAPPH_PK ON ADR.HEALTH_BENEFITS_APPLICATION_H
(HEALTH_BENEFITS_APPLN_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HEALTHAPPH_PRSNID_NU_I ON ADR.HEALTH_BENEFITS_APPLICATION_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFITS_APPLICATION_H FOR ADR.HEALTH_BENEFITS_APPLICATION_H;


ALTER TABLE ADR.HEALTH_BENEFITS_APPLICATION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_018
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT HEALTHAPPH_PK
  PRIMARY KEY
  (HEALTH_BENEFITS_APPLN_H_ID)
  USING INDEX ADR.HEALTHAPPH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION_H TO ADREXT;

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION_H TO ADR_R;

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFITS_APPLICATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFITS_APPLICATION_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN
(
  HEALTH_BENEFIT_PLAN_ID       NUMBER(20)       NOT NULL,
  HEALTH_BENEFIT_PROFILE_ID    NUMBER(20)       NOT NULL,
  STD_HEALTHBENEFITPLAN_ID     NUMBER(20)       NOT NULL,
  HEALTH_BNFT_PLN_UPDATE_DATE  DATE,
  STD_DATACHANGESOURCE_ID      NUMBER(20)       NOT NULL,
  STD_INSTITUTION_ID           NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  OVERRIDE_FLAG                NUMBER(1),
  OVERRIDE_ASSIGNMENT          NUMBER(1),
  OVERRIDE_REASON              VARCHAR2(200 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFIT_PLAN IS 'A description of a type of benefit used to associate the authority for care to the provisioning of health care services for a Person.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.HEALTH_BENEFIT_PLAN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.HEALTH_BENEFIT_PROFILE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.STD_HEALTHBENEFITPLAN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.STD_DATACHANGESOURCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.STD_INSTITUTION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.RECORD_CREATED_BY IS 'Account or user ID that created the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HLTBNFPLN_HLTBNFPRF_FK_I ON ADR.HEALTH_BENEFIT_PLAN
(HEALTH_BENEFIT_PROFILE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPLN_PK ON ADR.HEALTH_BENEFIT_PLAN
(HEALTH_BENEFIT_PLAN_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HEALTH_BENEFIT_PLAN_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.Health_Benefit_Plan
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO Health_Benefit_Plan_H
        (Health_benefit_plan_h_id,
         Health_benefit_plan_id,
         Health_benefit_profile_id,
         Std_healthbenefitplan_id,
         Health_bnft_pln_update_date,
         Std_datachangesource_id,
         Std_institution_id,
         Record_created_by,
         Record_created_date,
         Record_modified_by,
         Record_modified_date,
         Record_modified_count,
         History_created_date,
         Override_flag,
         Override_assignment,
         Override_reason,
         Transaction_type_code
        )
        VALUES
        (Health_Benefit_Plan_H_S.NEXTVAL,
         :OLD.Health_benefit_plan_id,
         :OLD.Health_benefit_profile_id,
         :OLD.Std_healthbenefitplan_id,
         :OLD.Health_bnft_pln_update_date,
         :OLD.Std_datachangesource_id,
         :OLD.Std_institution_id,
         :OLD.Record_created_by,
         :OLD.Record_created_date,
         :OLD.Record_modified_by,
         :OLD.Record_modified_date,
         :OLD.Record_modified_count,
         SYSDATE,
         :OLD.Override_flag,
         :OLD.Override_assignment,
         :OLD.Override_reason,
         'D'
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      insert into HEALTH_BENEFIT_PLAN_H
        (Health_benefit_plan_h_id,
         Health_benefit_plan_id,
         Health_benefit_profile_id,
         Std_healthbenefitplan_id,
         Health_bnft_pln_update_date,
         Std_datachangesource_id,
         Std_institution_id,
         Record_created_by,
         Record_created_date,
         Record_modified_by,
         Record_modified_date,
         Record_modified_count,
         History_created_date,
         Override_flag,
         Override_assignment,
         Override_reason,
         Transaction_type_code
        )
        VALUES
        (Health_Benefit_Plan_H_S.NEXTVAL,
         :NEW.Health_benefit_plan_id,
         :NEW.Health_benefit_profile_id,
         :NEW.Std_healthbenefitplan_id,
         :NEW.Health_bnft_pln_update_date,
         :NEW.Std_datachangesource_id,
         :NEW.Std_institution_id,
         :NEW.Record_created_by,
         :NEW.Record_created_date,
         :NEW.Record_modified_by,
         :NEW.Record_modified_date,
         :NEW.Record_modified_count,
         SYSDATE,
         :NEW.Override_flag,
         :NEW.Override_assignment,
         :NEW.Override_reason,
         mDML
        );

    END IF;

  END Health_Benefit_Plan_Audir_Trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN FOR ADR.HEALTH_BENEFIT_PLAN;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN ADD (
  CONSTRAINT HLTHBENPLN_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PLAN_ID)
  USING INDEX ADR.HLTHBENPLN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFIT_PLAN ADD (
  CONSTRAINT HLTBNFPLN_HLTBNFPRF_FK 
  FOREIGN KEY (HEALTH_BENEFIT_PROFILE_ID) 
  REFERENCES ADR.HEALTH_BENEFIT_PROFILE (HEALTH_BENEFIT_PROFILE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN TO ES_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN TO VETCHOICE_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DESC
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_DESC CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_DESC
(
  HLTH_BEN_PLAN_DESC_ID          NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_DESCRIPTION      CLOB           NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER         NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  HEALTH_BENEFIT_PLAN_DETAIL_ID  NUMBER(20)     NOT NULL
)
LOB (HLTH_BEN_PLAN_DESCRIPTION) STORE AS (
  TABLESPACE  L_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_DESC.HLTH_BEN_PLAN_DESC_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_DESC.HLTH_BEN_PLAN_DESCRIPTION IS 'Health Benefit Plan Description';


CREATE UNIQUE INDEX ADR.HEALTH_BENEFIT_PLAN_DESC_PK ON ADR.HEALTH_BENEFIT_PLAN_DESC
(HLTH_BEN_PLAN_DESC_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HLTH_BEN_PLAN_DESC_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.HEALTH_BENEFIT_PLAN_DESC
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO ADR.HEALTH_BENEFIT_PLAN_DESC_H
        (HLTH_BEN_PLAN_DESC_H_ID,
         HLTH_BEN_PLAN_DESC_ID,
         HLTH_BEN_PLAN_DESCRIPTION,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE,
         HEALTH_BENEFIT_PLAN_DETAIL_ID
        )
        VALUES
        (HEALTH_BENEFIT_PLAN_DESC_H_S.NEXTVAL,
         :OLD.HLTH_BEN_PLAN_DESC_ID,
         :OLD.HLTH_BEN_PLAN_DESCRIPTION,
         :OLD.Record_created_by,
         :OLD.Record_created_date,
         :OLD.Record_modified_by,
         :OLD.Record_modified_date,
         :OLD.Record_modified_count,
         SYSDATE,
         'D',
         :OLD.HEALTH_BENEFIT_PLAN_DETAIL_ID
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      insert into ADR.HEALTH_BENEFIT_PLAN_DESC_H
        (HLTH_BEN_PLAN_DESC_H_ID,
         HLTH_BEN_PLAN_DESC_ID,
         HLTH_BEN_PLAN_DESCRIPTION,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE,
         HEALTH_BENEFIT_PLAN_DETAIL_ID
        )
        VALUES
        (HEALTH_BENEFIT_PLAN_DESC_H_S.NEXTVAL,
         :NEW.HLTH_BEN_PLAN_DESC_ID,
         :NEW.HLTH_BEN_PLAN_DESCRIPTION,
         :NEW.Record_created_by,
         :NEW.Record_created_date,
         :NEW.Record_modified_by,
         :NEW.Record_modified_date,
         :NEW.Record_modified_count,
         SYSDATE,
         mDML,
         :NEW.HEALTH_BENEFIT_PLAN_DETAIL_ID
        );

    END IF;

  END HLTH_BEN_PLAN_DESC_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_DESC FOR ADR.HEALTH_BENEFIT_PLAN_DESC;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DESC ADD (
  CONSTRAINT HEALTH_BENEFIT_PLAN_DESC_PK
  PRIMARY KEY
  (HLTH_BEN_PLAN_DESC_ID)
  USING INDEX ADR.HEALTH_BENEFIT_PLAN_DESC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DESC ADD (
  CONSTRAINT HLTHBENPLANDESC_FK 
  FOREIGN KEY (HEALTH_BENEFIT_PLAN_DETAIL_ID) 
  REFERENCES ADR.HEALTH_BENEFIT_PLAN_DETAIL (HEALTH_BENEFIT_PLAN_DETAIL_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_DESC TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DESC_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_DESC_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_DESC_H
(
  HLTH_BEN_PLAN_DESC_H_ID        NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_DESC_ID          NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_DESCRIPTION      CLOB,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER         NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  HEALTH_BENEFIT_PLAN_DETAIL_ID  NUMBER(20)     NOT NULL
)
LOB (HLTH_BEN_PLAN_DESCRIPTION) STORE AS (
  TABLESPACE  L_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_DESC_H.HLTH_BEN_PLAN_DESC_H_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_DESC_H.HLTH_BEN_PLAN_DESC_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_DESC_H.HLTH_BEN_PLAN_DESCRIPTION IS 'Health Benefit Plan Description';


CREATE UNIQUE INDEX ADR.HEALTH_BENEFIT_PLAN_DESC_H_PK ON ADR.HEALTH_BENEFIT_PLAN_DESC_H
(HLTH_BEN_PLAN_DESC_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_DESC_H FOR ADR.HEALTH_BENEFIT_PLAN_DESC_H;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DESC_H ADD (
  CONSTRAINT HEALTH_BENEFIT_PLAN_DESC_H_PK
  PRIMARY KEY
  (HLTH_BEN_PLAN_DESC_H_ID)
  USING INDEX ADR.HEALTH_BENEFIT_PLAN_DESC_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_DESC_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_DESC_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL
(
  HEALTH_BENEFIT_PLAN_DETAIL_ID  NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_NAME_ID          NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_CODE             VARCHAR2(20 BYTE),
  HLTH_BEN_PLAN_COV_CODE         VARCHAR2(20 BYTE),
  HLTH_BEN_PLAN_EFF_DATE         DATE           NOT NULL,
  HLTH_BEN_PLAN_EXP_DATE         DATE,
  HLTH_BEN_PLAN_AUTO_IND         VARCHAR2(1 BYTE) DEFAULT '0' NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.HLTHBENPLANDET_HBPCCD_UI ON ADR.HEALTH_BENEFIT_PLAN_DETAIL
(HLTH_BEN_PLAN_COV_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPLANDET_HBPCD_UI ON ADR.HEALTH_BENEFIT_PLAN_DETAIL
(HLTH_BEN_PLAN_COV_CODE, HLTH_BEN_PLAN_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPLANDET_PK ON ADR.HEALTH_BENEFIT_PLAN_DETAIL
(HEALTH_BENEFIT_PLAN_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HLTH_BEN_PLN_DTL_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.HEALTH_BENEFIT_PLAN_DETAIL
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO ADR.HEALTH_BENEFIT_PLAN_DETAIL_H
        (HEALTH_BENEFIT_PLAN_DET_H_ID,
         HEALTH_BENEFIT_PLAN_DETAIL_ID,
         HLTH_BEN_PLAN_NAME_ID,
         HLTH_BEN_PLAN_CODE,
      	 HLTH_BEN_PLAN_COV_CODE,
     	 HLTH_BEN_PLAN_EFF_DATE,
     	 HLTH_BEN_PLAN_EXP_DATE,
         HLTH_BEN_PLAN_AUTO_IND,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (HEALTH_BENEFIT_PLAN_DETAIL_H_S.NEXTVAL,
         :OLD.HEALTH_BENEFIT_PLAN_DETAIL_ID,
     	 :OLD.HLTH_BEN_PLAN_NAME_ID,
    	 :OLD.HLTH_BEN_PLAN_CODE,
     	 :OLD.HLTH_BEN_PLAN_COV_CODE,
     	 :OLD.HLTH_BEN_PLAN_EFF_DATE,
    	 :OLD.HLTH_BEN_PLAN_EXP_DATE,
     	 :OLD.HLTH_BEN_PLAN_AUTO_IND,
     	 :OLD.RECORD_CREATED_BY,
     	 :OLD.RECORD_CREATED_DATE,
     	 :OLD.RECORD_MODIFIED_BY,
     	 :OLD.RECORD_MODIFIED_DATE,
     	 :OLD.RECORD_MODIFIED_COUNT,
         SYSDATE,
         'D'
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      insert into ADR.HEALTH_BENEFIT_PLAN_DETAIL_H
      (HEALTH_BENEFIT_PLAN_DET_H_ID,
         HEALTH_BENEFIT_PLAN_DETAIL_ID,
         HLTH_BEN_PLAN_NAME_ID,
         HLTH_BEN_PLAN_CODE,
         HLTH_BEN_PLAN_COV_CODE,
         HLTH_BEN_PLAN_EFF_DATE,
         HLTH_BEN_PLAN_EXP_DATE,
         HLTH_BEN_PLAN_AUTO_IND,
         RECORD_CREATED_BY,
         RECORD_CREATED_DATE,
         RECORD_MODIFIED_BY,
         RECORD_MODIFIED_DATE,
         RECORD_MODIFIED_COUNT,
         HISTORY_CREATED_DATE,
         TRANSACTION_TYPE_CODE
        )
        VALUES
        (HEALTH_BENEFIT_PLAN_DETAIL_H_S.NEXTVAL,
           :NEW.HEALTH_BENEFIT_PLAN_DETAIL_ID,
           :NEW.HLTH_BEN_PLAN_NAME_ID,
           :NEW.HLTH_BEN_PLAN_CODE,
           :NEW.HLTH_BEN_PLAN_COV_CODE,
           :NEW.HLTH_BEN_PLAN_EFF_DATE,
           :NEW.HLTH_BEN_PLAN_EXP_DATE,
           :NEW.HLTH_BEN_PLAN_AUTO_IND,
           :NEW.RECORD_CREATED_BY,
           :NEW.RECORD_CREATED_DATE,
           :NEW.RECORD_MODIFIED_BY,
           :NEW.RECORD_MODIFIED_DATE,
           :NEW.RECORD_MODIFIED_COUNT,
         SYSDATE,
         mDML
      );

    END IF;

  END HLTH_BEN_PLAN_DESC_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_DETAIL FOR ADR.HEALTH_BENEFIT_PLAN_DETAIL;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL ADD (
  CONSTRAINT HLTHBENPLANDET_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PLAN_DETAIL_ID)
  USING INDEX ADR.HLTHBENPLANDET_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL ADD (
  CONSTRAINT HLTHBENPLNDTL_HLTHPLNNAMEID_FK 
  FOREIGN KEY (HLTH_BEN_PLAN_NAME_ID) 
  REFERENCES ADR.HEALTH_BENEFIT_PLAN_NAME (HLTH_BEN_PLAN_NAME_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL_H
(
  HEALTH_BENEFIT_PLAN_DET_H_ID   NUMBER(20)     NOT NULL,
  HEALTH_BENEFIT_PLAN_DETAIL_ID  NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_NAME_ID          NUMBER(20)     NOT NULL,
  HLTH_BEN_PLAN_CODE             VARCHAR2(20 BYTE),
  HLTH_BEN_PLAN_COV_CODE         VARCHAR2(20 BYTE),
  HLTH_BEN_PLAN_EFF_DATE         DATE           NOT NULL,
  HLTH_BEN_PLAN_EXP_DATE         DATE,
  HLTH_BEN_PLAN_AUTO_IND         VARCHAR2(1 BYTE) DEFAULT 'O' NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL,
  HISTORY_CREATED_DATE           DATE           NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.HLTH_BEN_PLAN_DETAIL_H_PK ON ADR.HEALTH_BENEFIT_PLAN_DETAIL_H
(HEALTH_BENEFIT_PLAN_DET_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_DETAIL_H FOR ADR.HEALTH_BENEFIT_PLAN_DETAIL_H;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_DETAIL_H ADD (
  CONSTRAINT HEALTH_BEN_PLAN_DETAIL_H_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PLAN_DET_H_ID)
  USING INDEX ADR.HLTH_BEN_PLAN_DETAIL_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_DETAIL_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_H
(
  HEALTH_BENEFIT_PLAN_H_ID     NUMBER(20)       NOT NULL,
  HEALTH_BENEFIT_PLAN_ID       NUMBER(20)       NOT NULL,
  HEALTH_BENEFIT_PROFILE_ID    NUMBER(20),
  STD_HEALTHBENEFITPLAN_ID     NUMBER(20),
  HEALTH_BNFT_PLN_UPDATE_DATE  DATE,
  STD_DATACHANGESOURCE_ID      NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  HISTORY_CREATED_DATE         DATE             NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  OVERRIDE_FLAG                NUMBER(1),
  OVERRIDE_ASSIGNMENT          NUMBER(1),
  OVERRIDE_REASON              VARCHAR2(200 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFIT_PLAN_H IS 'A description of a type of benefit used to associate the authority for care to the provisioning of health care services for a Person.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.HEALTH_BENEFIT_PLAN_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.HEALTH_BENEFIT_PLAN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.HEALTH_BENEFIT_PROFILE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.STD_HEALTHBENEFITPLAN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.STD_DATACHANGESOURCE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.STD_INSTITUTION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.RECORD_CREATED_BY IS 'Account or user ID that created the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PLAN_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.HLTHBENPLNH_BENPLANID_NU_I ON ADR.HEALTH_BENEFIT_PLAN_H
(HEALTH_BENEFIT_PLAN_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE INDEX ADR.HLTHBENPLNH_BENPROFID_NU_I ON ADR.HEALTH_BENEFIT_PLAN_H
(HEALTH_BENEFIT_PROFILE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE UNIQUE INDEX ADR.HLTHBENPLNH_PK ON ADR.HEALTH_BENEFIT_PLAN_H
(HEALTH_BENEFIT_PLAN_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.HLTHBENPLNH_SHBPID_NU_I ON ADR.HEALTH_BENEFIT_PLAN_H
(STD_HEALTHBENEFITPLAN_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_H FOR ADR.HEALTH_BENEFIT_PLAN_H;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_H ADD (
  CONSTRAINT HLTHBENPLNH_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PLAN_H_ID)
  USING INDEX ADR.HLTHBENPLNH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_NAME
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_NAME CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_NAME
(
  HLTH_BEN_PLAN_NAME_ID       NUMBER(20)        NOT NULL,
  HLTH_BEN_PLAN_NAME          VARCHAR2(300 BYTE) NOT NULL,
  STD_HLTHBENEFITPLANTYPE_ID  NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.HEALTH_BENEFIT_PLAN_NAME_PK ON ADR.HEALTH_BENEFIT_PLAN_NAME
(HLTH_BEN_PLAN_NAME_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPLNDEFNM_UI ON ADR.HEALTH_BENEFIT_PLAN_NAME
(HLTH_BEN_PLAN_NAME)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HLTH_BEN_PLN_NAM_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.HEALTH_BENEFIT_PLAN_NAME
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO ADR.HEALTH_BENEFIT_PLAN_NAME_H
          (HLTH_BEN_PLAN_NAME_H_ID ,
    	   HLTH_BEN_PLAN_NAME_ID,
   	   HLTH_BEN_PLAN_NAME,
    	   STD_HLTHBENEFITPLANTYPE_ID ,
           RECORD_CREATED_BY,
           RECORD_CREATED_DATE,
           RECORD_MODIFIED_BY,
           RECORD_MODIFIED_DATE,
           RECORD_MODIFIED_COUNT,
           HISTORY_CREATED_DATE ,
           TRANSACTION_TYPE_CODE
       )
      VALUES
        (HEALTH_BENEFIT_PLAN_NAME_H_S.NEXTVAL,
         :OLD.HLTH_BEN_PLAN_NAME_ID,
         :OLD.HLTH_BEN_PLAN_NAME,
         :OLD.STD_HLTHBENEFITPLANTYPE_ID,
         :OLD.RECORD_CREATED_BY,
         :OLD.RECORD_CREATED_DATE,
         :OLD.RECORD_MODIFIED_BY,
         :OLD.RECORD_MODIFIED_DATE,
         :OLD.RECORD_MODIFIED_COUNT,
         SYSDATE,
         'D'
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      INSERT INTO ADR.HEALTH_BENEFIT_PLAN_NAME_H
      (	HLTH_BEN_PLAN_NAME_H_ID ,
       	HLTH_BEN_PLAN_NAME_ID,
     	HLTH_BEN_PLAN_NAME,
     	STD_HLTHBENEFITPLANTYPE_ID ,
     	RECORD_CREATED_BY,
     	RECORD_CREATED_DATE,
     	RECORD_MODIFIED_BY,
     	RECORD_MODIFIED_DATE,
     	RECORD_MODIFIED_COUNT,
     	HISTORY_CREATED_DATE ,
     	TRANSACTION_TYPE_CODE
     )
     VALUES
     	(HEALTH_BENEFIT_PLAN_NAME_H_S.NEXTVAL,
     	:NEW.HLTH_BEN_PLAN_NAME_ID,
     	:NEW.HLTH_BEN_PLAN_NAME,
     	:NEW.STD_HLTHBENEFITPLANTYPE_ID,
     	:NEW.RECORD_CREATED_BY,
     	:NEW.RECORD_CREATED_DATE,
     	:NEW.RECORD_MODIFIED_BY,
     	:NEW.RECORD_MODIFIED_DATE,
     	:NEW.RECORD_MODIFIED_COUNT,
     	 SYSDATE,
         mDML
        );

    END IF;

  END HLTH_BEN_PLN_NAM_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_NAME FOR ADR.HEALTH_BENEFIT_PLAN_NAME;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_NAME ADD (
  CONSTRAINT HEALTH_BENEFIT_PLAN_NAME_PK
  PRIMARY KEY
  (HLTH_BEN_PLAN_NAME_ID)
  USING INDEX ADR.HEALTH_BENEFIT_PLAN_NAME_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_NAME ADD (
  CONSTRAINT STD_HLTHBENEFITPLANTYPEID_FK 
  FOREIGN KEY (STD_HLTHBENEFITPLANTYPE_ID) 
  REFERENCES ADRREF.STD_HLTHBENEFITPLANTYPE (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_NAME TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_NAME_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PLAN_NAME_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PLAN_NAME_H
(
  HLTH_BEN_PLAN_NAME_H_ID     NUMBER(20)        NOT NULL,
  HLTH_BEN_PLAN_NAME_ID       NUMBER(20)        NOT NULL,
  HLTH_BEN_PLAN_NAME          VARCHAR2(300 BYTE) NOT NULL,
  STD_HLTHBENEFITPLANTYPE_ID  NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  HISTORY_CREATED_DATE        DATE              NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.HEALTH_BENEFIT_PLAN_NAME_H_PK ON ADR.HEALTH_BENEFIT_PLAN_NAME_H
(HLTH_BEN_PLAN_NAME_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PLAN_NAME_H FOR ADR.HEALTH_BENEFIT_PLAN_NAME_H;


ALTER TABLE ADR.HEALTH_BENEFIT_PLAN_NAME_H ADD (
  CONSTRAINT HEALTH_BENEFIT_PLAN_NAME_H_PK
  PRIMARY KEY
  (HLTH_BEN_PLAN_NAME_H_ID)
  USING INDEX ADR.HEALTH_BENEFIT_PLAN_NAME_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PLAN_NAME_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PLAN_NAME_H TO ES_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PROFILE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PROFILE CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PROFILE
(
  HEALTH_BENEFIT_PROFILE_ID  NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFIT_PROFILE IS 'A collection of Health Benefit Plans associated to a Person.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.HEALTH_BENEFIT_PROFILE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.RECORD_CREATED_BY IS 'Account or user ID that created the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.HLTBNFPRF_PRSN_FK_I ON ADR.HEALTH_BENEFIT_PROFILE
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPRF_PK ON ADR.HEALTH_BENEFIT_PROFILE
(HEALTH_BENEFIT_PROFILE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HEALTH_BENEFIT_PROF_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
    ON ADR.Health_Benefit_Profile
  REFERENCING  OLD AS OLD NEW AS NEW
  FOR EACH ROW
DECLARE

    mDML char(1);

  BEGIN

    IF DELETING THEN

      INSERT INTO Health_Benefit_Profile_H
        (Health_benefit_profile_h_id,
         Health_benefit_profile_id,
         Person_id,
         Record_created_by,
         Record_created_date,
         Record_modified_by,
         Record_modified_date,
         Record_modified_count,
         History_created_date,
         Transaction_type_code
        )
        VALUES
        (Health_Benefit_Profile_H_S.NEXTVAL,
         :OLD.Health_benefit_profile_id,
         :OLD.Person_id,
         :OLD.Record_created_by,
         :OLD.Record_created_date,
         :OLD.Record_modified_by,
         :OLD.Record_modified_date,
         :OLD.Record_modified_count,
         SYSDATE,
         'D' 
        );

    ELSE

      IF UPDATING THEN

        mDML := 'U';

      ELSE

        mDML := 'I';

      END IF;

      INSERT INTO Health_Benefit_Profile_H
        (Health_benefit_profile_h_id,
         Health_benefit_profile_id,
         Person_id,
         Record_created_by,
         Record_created_date,
         Record_modified_by,
         Record_modified_date,
         Record_modified_count,
         History_created_date,
         Transaction_type_code
        )
        VALUES
        (Health_Benefit_Profile_H_S.NEXTVAL,
         :NEW.Health_benefit_profile_id,
         :NEW.Person_id,
         :NEW.Record_created_by,
         :NEW.Record_created_date,
         :NEW.Record_modified_by,
         :NEW.Record_modified_date,
         :NEW.Record_modified_count,
         SYSDATE,
         mDML
        );

    END IF;

  END Health_Benefit_Prof_Audir_Trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PROFILE FOR ADR.HEALTH_BENEFIT_PROFILE;


ALTER TABLE ADR.HEALTH_BENEFIT_PROFILE ADD (
  CONSTRAINT HLTHBENPRF_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PROFILE_ID)
  USING INDEX ADR.HLTHBENPRF_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HEALTH_BENEFIT_PROFILE ADD (
  CONSTRAINT HLTBNFPRF_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PROFILE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PROFILE TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PROFILE TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PROFILE TO ES_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PROFILE TO VETCHOICE_RW;
ALTER TABLE ADR.HEALTH_BENEFIT_PROFILE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_BENEFIT_PROFILE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_BENEFIT_PROFILE_H
(
  HEALTH_BENEFIT_PROFILE_H_ID  NUMBER(20),
  HEALTH_BENEFIT_PROFILE_ID    NUMBER(20),
  PERSON_ID                    NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  HISTORY_CREATED_DATE         DATE,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_BENEFIT_PROFILE_H IS 'A collection of Health Benefit Plans associated to a Person.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.HEALTH_BENEFIT_PROFILE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.HEALTH_BENEFIT_PROFILE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.RECORD_CREATED_BY IS 'Account or user ID that created the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_BENEFIT_PROFILE_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.HEALTHPROFH_PRSNID_NU_I ON ADR.HEALTH_BENEFIT_PROFILE_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.HLTHBENPRFH_PK ON ADR.HEALTH_BENEFIT_PROFILE_H
(HEALTH_BENEFIT_PROFILE_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_BENEFIT_PROFILE_H FOR ADR.HEALTH_BENEFIT_PROFILE_H;


ALTER TABLE ADR.HEALTH_BENEFIT_PROFILE_H ADD (
  CONSTRAINT SYS_C00297522
  CHECK ("HEALTH_BENEFIT_PROFILE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297523
  CHECK ("HEALTH_BENEFIT_PROFILE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297524
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297525
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297526
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297527
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297528
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297529
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00297530
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT HLTHBENPRFH_PK
  PRIMARY KEY
  (HEALTH_BENEFIT_PROFILE_H_ID)
  USING INDEX ADR.HLTHBENPRFH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_BENEFIT_PROFILE_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HEALTH_BENEFIT_PROFILE_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_BENEFIT_PROFILE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_BENEFIT_PROFILE_H TO ES_RW;
ALTER TABLE ADR.HEALTH_CARE_PROVIDER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_CARE_PROVIDER CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_CARE_PROVIDER
(
  HEALTH_CARE_PROVIDER_ID  NUMBER(20)           NOT NULL,
  LAST_NAME                VARCHAR2(40 BYTE),
  FIRST_NAME               VARCHAR2(30 BYTE),
  MIDDLE_NAME              VARCHAR2(30 BYTE),
  SUFFIX                   VARCHAR2(12 BYTE),
  PREFIX                   VARCHAR2(12 BYTE),
  EDUCATIONAL_DEGREE       VARCHAR2(30 BYTE),
  PRACTITIONER_ID          VARCHAR2(18 BYTE)    NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_CARE_PROVIDER IS 'Primary Care Provider (PCP) or Practitioner and the facility they are associated with.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.HEALTH_CARE_PROVIDER_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.PRACTITIONER_ID IS 'Uniquely identifies a HEALTH_CARE_PROVIDER at a single STD_INSTITUTION.

Format of this field includes the provider id and the institution id.

This field corresponds to 1.1 & 1.2 of Provider ID of A08 ZPC segment: Pointer to entry in NEW PERSON file (#200) & Facility Number.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE UNIQUE INDEX ADR.HCP_PK ON ADR.HEALTH_CARE_PROVIDER
(HEALTH_CARE_PROVIDER_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.HEALTH_CARE_PROV_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.HEALTH_CARE_PROVIDER
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into HEALTH_CARE_PROVIDER_H
        (
        HEALTH_CARE_PROVIDER_H_ID,
        HEALTH_CARE_PROVIDER_ID,
        LAST_NAME,
        FIRST_NAME,
        MIDDLE_NAME,
        SUFFIX,
        PREFIX,
        EDUCATIONAL_DEGREE,
        PRACTITIONER_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HEALTH_CARE_PROVIDER_H_S.nextval,
        :old.HEALTH_CARE_PROVIDER_ID,
        :old.LAST_NAME,
        :old.FIRST_NAME,
        :old.MIDDLE_NAME,
        :old.SUFFIX,
        :old.PREFIX,
        :old.EDUCATIONAL_DEGREE,
        :old.PRACTITIONER_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into HEALTH_CARE_PROVIDER_H
        (
        HEALTH_CARE_PROVIDER_H_ID,
        HEALTH_CARE_PROVIDER_ID,
        LAST_NAME,
        FIRST_NAME,
        MIDDLE_NAME,
        SUFFIX,
        PREFIX,
        EDUCATIONAL_DEGREE,
        PRACTITIONER_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        HEALTH_CARE_PROVIDER_H_S.nextval,
        :new.HEALTH_CARE_PROVIDER_ID,
        :new.LAST_NAME,
        :new.FIRST_NAME,
        :new.MIDDLE_NAME,
        :new.SUFFIX,
        :new.PREFIX,
        :new.EDUCATIONAL_DEGREE,
        :new.PRACTITIONER_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end HEALTH_CARE_PROV_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM HEALTH_CARE_PROVIDER FOR ADR.HEALTH_CARE_PROVIDER;


ALTER TABLE ADR.HEALTH_CARE_PROVIDER ADD (
  CONSTRAINT HCP_PK
  PRIMARY KEY
  (HEALTH_CARE_PROVIDER_ID)
  USING INDEX ADR.HCP_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_CARE_PROVIDER TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HEALTH_CARE_PROVIDER TO ADR_RW;

GRANT DELETE ON ADR.HEALTH_CARE_PROVIDER TO EEUSR;

GRANT SELECT ON ADR.HEALTH_CARE_PROVIDER TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_CARE_PROVIDER TO ES_RW;
ALTER TABLE ADR.HEALTH_CARE_PROVIDER_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HEALTH_CARE_PROVIDER_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.HEALTH_CARE_PROVIDER_H
(
  HEALTH_CARE_PROVIDER_H_ID  NUMBER(20),
  HEALTH_CARE_PROVIDER_ID    NUMBER(20),
  LAST_NAME                  VARCHAR2(40 BYTE),
  FIRST_NAME                 VARCHAR2(30 BYTE),
  MIDDLE_NAME                VARCHAR2(30 BYTE),
  SUFFIX                     VARCHAR2(12 BYTE),
  PREFIX                     VARCHAR2(12 BYTE),
  EDUCATIONAL_DEGREE         VARCHAR2(30 BYTE),
  PRACTITIONER_ID            VARCHAR2(18 BYTE),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_COUNT      NUMBER(10),
  RECORD_MODIFIED_DATE       DATE,
  HISTORY_CREATED_DATE       DATE,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HEALTH_CARE_PROVIDER_H IS 'Primary Care Provider (PCP) or Practitioner and the facility they are associated with.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.HEALTH_CARE_PROVIDER_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.HEALTH_CARE_PROVIDER_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.PRACTITIONER_ID IS 'Uniquely identifies a HEALTH_CARE_PROVIDER at a single STD_INSTITUTION.

Format of this field includes the provider id and the institution id.

This field corresponds to 1.1 & 1.2 of Provider ID of A08 ZPC segment: Pointer to entry in NEW PERSON file (#200) & Facility Number.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.HEALTH_CARE_PROVIDER_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.HCPH_PK ON ADR.HEALTH_CARE_PROVIDER_H
(HEALTH_CARE_PROVIDER_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HEALTH_CARE_PROVIDER_H FOR ADR.HEALTH_CARE_PROVIDER_H;


ALTER TABLE ADR.HEALTH_CARE_PROVIDER_H ADD (
  CONSTRAINT SYS_C00160881
  CHECK ("HEALTH_CARE_PROVIDER_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160882
  CHECK ("HEALTH_CARE_PROVIDER_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160883
  CHECK ("PRACTITIONER_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160884
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160885
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160886
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160887
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160888
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160889
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160890
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT HCPH_PK
  PRIMARY KEY
  (HEALTH_CARE_PROVIDER_H_ID)
  USING INDEX ADR.HCPH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HEALTH_CARE_PROVIDER_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.HEALTH_CARE_PROVIDER_H TO ADR_RW;

GRANT SELECT ON ADR.HEALTH_CARE_PROVIDER_H TO ES_R;

GRANT INSERT, SELECT ON ADR.HEALTH_CARE_PROVIDER_H TO ES_RW;
ALTER TABLE ADR.HL7_ARCHIVE_TRANSACTION_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HL7_ARCHIVE_TRANSACTION_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.HL7_ARCHIVE_TRANSACTION_LOG
(
  HL7_TRANSACTION_LOG_ID          NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20),
  MESSAGE_TRANSMISSION_TYPE_ID    NUMBER(20)    NOT NULL,
  HL7_MESSAGE_STATUS_TYPE_ID      NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  ACK_TYPE_ID                     NUMBER(20),
  MESSAGE_CONTROL_NUMBER          VARCHAR2(20 BYTE) NOT NULL,
  BATCH_CONTROL_NUMBER            VARCHAR2(20 BYTE),
  ACK_DATE                        DATE,
  TRANSMISSION_DATE               DATE,
  RETRANSMISSION_COUNT            NUMBER(10)    NOT NULL,
  RETRANSMISSION_INFO_TEXT        VARCHAR2(30 BYTE),
  ERROR_TEXT                      VARCHAR2(2000 BYTE),
  SITE_RECORD_ELGBTY_STATUS_CODE  VARCHAR2(30 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  MESSAGE_BODY                    CLOB,
  INTERNAL_ERROR_TEXT             CLOB,
  REF_HL7_TRANSACTION_LOG_ID      NUMBER(20)
)
LOB (MESSAGE_BODY) STORE AS MESSAGE_BODY_LOB (
  TABLESPACE  L_DATA_03
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  PCTVERSION  10
  NOCACHE
  NOLOGGING)
LOB (INTERNAL_ERROR_TEXT) STORE AS INTERNAL_ERROR_TEXT_LOB (
  TABLESPACE  L_DATA_03
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  PCTVERSION  10
  NOCACHE
  NOLOGGING)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_03
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY HASH (PERSON_ID)
(  
  PARTITION HL7_ARC_TRANSACTION_LOG_P01
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P01_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P01_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P02
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P02_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P02_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P03
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P03_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P03_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P04
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P04_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P04_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P05
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P05_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P05_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P06
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P06_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P06_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P07
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P07_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P07_I (TABLESPACE L_DATA_03),  
  PARTITION HL7_ARC_TRANSACTION_LOG_P08
    TABLESPACE L_DATA_03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_ARC_MSGBDY_LOB_P08_I (TABLESPACE L_DATA_03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ARC_ERRTXT_LOB_P08_I (TABLESPACE L_DATA_03)
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HL7_ARCHIVE_TRANSACTION_LOG IS 'Messages marked for archive, deletion.';


CREATE INDEX ADR.HL7ARCLOG_HL7TRNLG_NU_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(REF_HL7_TRANSACTION_LOG_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (REF_HL7_TRANSACTION_LOG_ID) (  
  PARTITION HL7LOG_REFTRNID_FK_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_REFTRNID_FK_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7ARCLOG_PERSONID_NU_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(PERSON_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION HL7_ARC_TRANSACTION_LOG_P01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARC_TRANSACTION_LOG_P08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7_ARCLOG_BTCHCNTLNO_NU_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(BATCH_CONTROL_NUMBER)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (BATCH_CONTROL_NUMBER) (  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_BTCHCNTLNO_NU_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7_ARCLOG_MSGCNTLNO_NU_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(MESSAGE_CONTROL_NUMBER)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (MESSAGE_CONTROL_NUMBER) (  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGCNTLNO_NU_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7_ARCLOG_MSGTYPID_FK_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(MESSAGE_TRANSMISSION_TYPE_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (MESSAGE_TRANSMISSION_TYPE_ID) (  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_MSGTYPID_FK_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7_ARCLOG_PK ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(HL7_TRANSACTION_LOG_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (HL7_TRANSACTION_LOG_ID) (  
  PARTITION HL7_ARCLOG_PK_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7_ARCLOG_PK_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE INDEX ADR.HL7_ARCLOG_TRNDT_TRNTYP_NU_I ON ADR.HL7_ARCHIVE_TRANSACTION_LOG
(TRANSMISSION_DATE, MESSAGE_TRANSMISSION_TYPE_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (TRANSMISSION_DATE, MESSAGE_TRANSMISSION_TYPE_ID) (  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I01
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I02
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I03
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I04
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I05
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I06
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I07
    TABLESPACE L_INDEX_03,  
  PARTITION HL7LOG_ARC_TRNDT_TRNTYP_NU_I08
    TABLESPACE L_INDEX_03
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HL7_ARCHIVE_TRANSACTION_LOG FOR ADR.HL7_ARCHIVE_TRANSACTION_LOG;


ALTER TABLE ADR.HL7_ARCHIVE_TRANSACTION_LOG ADD (
  CONSTRAINT HL7_ARCLOG_PK
  PRIMARY KEY
  (HL7_TRANSACTION_LOG_ID)
  USING INDEX ADR.HL7_ARCLOG_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.HL7_ARCHIVE_TRANSACTION_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HL7_ARCHIVE_TRANSACTION_LOG TO ADR_RW;

GRANT SELECT ON ADR.HL7_ARCHIVE_TRANSACTION_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.HL7_ARCHIVE_TRANSACTION_LOG TO ES_RW;
DROP TABLE ADR.HL7_STATS_MV CASCADE CONSTRAINTS;

CREATE TABLE ADR.HL7_STATS_MV
(
  MESSAGE_TRANSMISSION_TYPE_ID  NUMBER(20)      NOT NULL,
  STD_INSTITUTION_ID            NUMBER(20),
  RECORD_CREATED_DATE           DATE,
  RECORD_COUNT                  NUMBER
)
NOCOMPRESS 
TABLESPACE M_DATA_02_P01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION HL7STATSMV_P01 VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_02_P01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    NOCOMPRESS 
    TABLESPACE M_DATA_02_P01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;


CREATE INDEX ADR.HL7_STATS_MV_NU_I ON ADR.HL7_STATS_MV
(MESSAGE_TRANSMISSION_TYPE_ID, STD_INSTITUTION_ID, RECORD_CREATED_DATE)
  TABLESPACE M_INDEX_02_P01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION HL7STATSMV_P01
    LOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_02_P01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE M_INDEX_02_P01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HL7_STATS_MV FOR ADR.HL7_STATS_MV;


GRANT SELECT ON ADR.HL7_STATS_MV TO ADR_R;

GRANT SELECT ON ADR.HL7_STATS_MV TO ADR_RW;
ALTER TABLE ADR.HL7_TRANSACTION_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.HL7_TRANSACTION_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.HL7_TRANSACTION_LOG
(
  HL7_TRANSACTION_LOG_ID          NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20),
  MESSAGE_TRANSMISSION_TYPE_ID    NUMBER(20)    NOT NULL,
  HL7_MESSAGE_STATUS_TYPE_ID      NUMBER(20),
  REF_HL7_TRANSACTION_LOG_ID      NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  ACK_TYPE_ID                     NUMBER(20),
  MESSAGE_CONTROL_NUMBER          VARCHAR2(20 BYTE) NOT NULL,
  BATCH_CONTROL_NUMBER            VARCHAR2(20 BYTE),
  ACK_DATE                        DATE,
  TRANSMISSION_DATE               DATE,
  RETRANSMISSION_COUNT            NUMBER(10)    NOT NULL,
  RETRANSMISSION_INFO_TEXT        VARCHAR2(30 BYTE),
  ERROR_TEXT                      VARCHAR2(2000 BYTE),
  SITE_RECORD_ELGBTY_STATUS_CODE  VARCHAR2(30 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  MESSAGE_BODY                    CLOB,
  INTERNAL_ERROR_TEXT             CLOB
)
LOB (MESSAGE_BODY) STORE AS (
  TABLESPACE  M_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  PCTVERSION  10
  NOCACHE
  NOLOGGING)
LOB (INTERNAL_ERROR_TEXT) STORE AS (
  TABLESPACE  M_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  PCTVERSION  10
  NOCACHE
  NOLOGGING)
COMPRESS FOR OLTP 
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      RECYCLE
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY HASH (PERSON_ID)
(  
  PARTITION HL7_TRANSACTION_LOG_P01
    TABLESPACE L_DATA_02_P01
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P01_I (TABLESPACE L_INDEX_02_P01)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P01_I (TABLESPACE L_INDEX_02_P01),  
  PARTITION HL7_TRANSACTION_LOG_P02
    TABLESPACE L_DATA_02_P02
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P02_I (TABLESPACE L_INDEX_02_P02)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P02_I (TABLESPACE L_INDEX_02_P02),  
  PARTITION HL7_TRANSACTION_LOG_P03
    TABLESPACE L_DATA_02_P03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P03_I (TABLESPACE L_INDEX_02_P03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P03_I (TABLESPACE L_INDEX_02_P03),  
  PARTITION HL7_TRANSACTION_LOG_P04
    TABLESPACE L_DATA_02_P04
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P04_I (TABLESPACE L_INDEX_02_P04)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P04_I (TABLESPACE L_INDEX_02_P04),  
  PARTITION HL7_TRANSACTION_LOG_P05
    TABLESPACE L_DATA_02_P01
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P05_I (TABLESPACE L_INDEX_02_P01)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P05_I (TABLESPACE L_INDEX_02_P01),  
  PARTITION HL7_TRANSACTION_LOG_P06
    TABLESPACE L_DATA_02_P02
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P06_I (TABLESPACE L_INDEX_02_P02)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P06_I (TABLESPACE L_INDEX_02_P02),  
  PARTITION HL7_TRANSACTION_LOG_P07
    TABLESPACE L_DATA_02_P03
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P07_I (TABLESPACE L_INDEX_02_P03)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P07_I (TABLESPACE L_INDEX_02_P03),  
  PARTITION HL7_TRANSACTION_LOG_P08
    TABLESPACE L_DATA_02_P04
    LOB (MESSAGE_BODY) STORE AS HL7LOG_MSGBDY_LOB_P08_I (TABLESPACE L_INDEX_02_P04)
    LOB (INTERNAL_ERROR_TEXT) STORE AS HL7LOG_ERRTXT_LOB_P08_I (TABLESPACE L_INDEX_02_P04)
)
NOCACHE
NOPARALLEL
MONITORING;


CREATE INDEX ADR.HL7LOG_BTCHCNTLNO_NU_I ON ADR.HL7_TRANSACTION_LOG
(BATCH_CONTROL_NUMBER)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (BATCH_CONTROL_NUMBER) (  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_BTCHCNTLNO_NU_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_MSGCNTLNO_NU_I ON ADR.HL7_TRANSACTION_LOG
(MESSAGE_CONTROL_NUMBER)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (MESSAGE_CONTROL_NUMBER) (  
  PARTITION HL7LOG_MSGCNTLNO_NU_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_MSGCNTLNO_NU_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_MSGTYPID_FK_I ON ADR.HL7_TRANSACTION_LOG
(MESSAGE_TRANSMISSION_TYPE_ID)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (MESSAGE_TRANSMISSION_TYPE_ID) (  
  PARTITION HL7LOG_MSGTYPID_FK_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_MSGTYPID_FK_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_MSGTYPID_FK_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_MSGTYPID_FK_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_MSGTYPID_FK_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_MSGTYPID_FK_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_MSGTYPID_FK_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_MSGTYPID_FK_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_PERSONID_FK_I ON ADR.HL7_TRANSACTION_LOG
(PERSON_ID)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION HL7LOG_PERSONID_FK_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_PERSONID_FK_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_PERSONID_FK_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_PERSONID_FK_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_PERSONID_FK_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_PERSONID_FK_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_PERSONID_FK_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_PERSONID_FK_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_PK ON ADR.HL7_TRANSACTION_LOG
(HL7_TRANSACTION_LOG_ID)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (HL7_TRANSACTION_LOG_ID) (  
  PARTITION HL7LOG_PK_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_PK_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_PK_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_PK_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_PK_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_PK_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_PK_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_PK_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_REFTRNID_FK_I ON ADR.HL7_TRANSACTION_LOG
(REF_HL7_TRANSACTION_LOG_ID)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (REF_HL7_TRANSACTION_LOG_ID) (  
  PARTITION HL7LOG_REFTRNID_FK_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_REFTRNID_FK_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_REFTRNID_FK_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_REFTRNID_FK_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_REFTRNID_FK_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_REFTRNID_FK_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_REFTRNID_FK_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_REFTRNID_FK_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE INDEX ADR.HL7LOG_TRNDT_TRNTYP_NU_I ON ADR.HL7_TRANSACTION_LOG
(TRANSMISSION_DATE, MESSAGE_TRANSMISSION_TYPE_ID)
  TABLESPACE M_DATA_01
  PCTFREE    10
  INITRANS   10
  MAXTRANS   255
  STORAGE    (
              INITIAL          128M
              NEXT             128M
              MINEXTENTS       1
              PCTINCREASE      0
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (TRANSMISSION_DATE, MESSAGE_TRANSMISSION_TYPE_ID) (  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I01
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I02
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I03
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I04
    TABLESPACE L_INDEX_02_P04,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I05
    TABLESPACE L_INDEX_02_P01,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I06
    TABLESPACE L_INDEX_02_P02,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I07
    TABLESPACE L_INDEX_02_P03,  
  PARTITION HL7LOG_TRNDT_TRNTYP_NU_I08
    TABLESPACE L_INDEX_02_P04
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM HL7_TRANSACTION_LOG FOR ADR.HL7_TRANSACTION_LOG;


ALTER TABLE ADR.HL7_TRANSACTION_LOG ADD (
  CONSTRAINT HL7LOG_PK
  PRIMARY KEY
  (HL7_TRANSACTION_LOG_ID)
  USING INDEX ADR.HL7LOG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.HL7_TRANSACTION_LOG ADD (
  CONSTRAINT HL7_PERSONID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.HL7_TRANSACTION_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HL7_TRANSACTION_LOG TO ADR_RW;

GRANT SELECT ON ADR.HL7_TRANSACTION_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.HL7_TRANSACTION_LOG TO ES_RW;
DROP TABLE ADR.HL7_TRANSACTION_LOG_SAVE CASCADE CONSTRAINTS;

CREATE TABLE ADR.HL7_TRANSACTION_LOG_SAVE
(
  HL7_TRANSACTION_LOG_ID  NUMBER(20)            NOT NULL,
  MESSAGE_CONTROL_NUMBER  VARCHAR2(20 BYTE)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.HL7_TRANSACTION_LOG_SAVE IS 'The table stores a record for each HL7 transaction containing details about each transaction. PERSON_ID Foreign key to the PERSON table. MESSAGE_TRANSMISSION_TYPE_ID Foreign key to the MESSAGE_TRANSMISSION_TYPE table. HL7_MESSAGE_STATUS_TYPE_ID Foreign key to the HL7_MESSAGE_STATUS_TYPE table. REF_HL7_TRANSACTION_LOG_ID Foreign key to the HL7_TRANSACTION_LOG table. Self- referential constraint. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site that originated the transmission. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.HL7_TRANSACTION_LOG_SAVE.HL7_TRANSACTION_LOG_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';


CREATE OR REPLACE PUBLIC SYNONYM HL7_TRANSACTION_LOG_SAVE FOR ADR.HL7_TRANSACTION_LOG_SAVE;


GRANT SELECT ON ADR.HL7_TRANSACTION_LOG_SAVE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.HL7_TRANSACTION_LOG_SAVE TO ADR_RW;

GRANT SELECT ON ADR.HL7_TRANSACTION_LOG_SAVE TO ES_R;

GRANT INSERT, SELECT ON ADR.HL7_TRANSACTION_LOG_SAVE TO ES_RW;
ALTER TABLE ADR.INCOME_TEST
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOME_TEST CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOME_TEST
(
  INCOME_TEST_ID                 NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INCOME_TEST_TYPE_ID            NUMBER(20),
  INCOME_TEST_SRC_TYPE_ID        NUMBER(20),
  MEANS_TEST_STATUS_TYPE_ID      NUMBER(20),
  MT_NOT_REQD_RSN_TYPE_ID        NUMBER(20),
  INCOME_VERIFY_STATUS_TYPE_ID   NUMBER(20),
  HARDSHIP_REVIEW_SITE_ID        NUMBER(20),
  SITE_CONDUCTING_TEST_ID        NUMBER(20),
  INCOME_YEAR                    NUMBER(4),
  INCOME_TEST_EFFECTIVE_DATE     DATE,
  IVM_TEST_COMPLETION_DATE       DATE,
  PRIMARY_TEST_IND               VARCHAR2(1 BYTE),
  ADJUDICATION_SENT_DATE         DATE,
  ADJUDICATED_DATE               DATE,
  THRESHOLD_MT_EXEMPT_AMOUNT     NUMBER(10,2),
  THRESHOLD_GMT_AMOUNT           NUMBER(10,2),
  THRESHOLD_NETWORTH_AMOUNT      NUMBER(10,2),
  THRESHOLD_PENSION_AMOUNT       NUMBER(10,2),
  THRESHOLD_INCOME_AMOUNT        NUMBER(10,2),
  DEDUCTIBLE_EXPENSE_AMOUNT      NUMBER(10,2),
  INCOME_VERIFY_STATUS_DATE      DATE,
  FARM_RANCH_INCOME_AMOUNT       NUMBER(10,2),
  DISCLOSE_FINANCIAL_INCOME_IND  VARCHAR2(1 BYTE),
  AGREED_TO_PAY_DEDUCTIBLE_IND   VARCHAR2(1 BYTE),
  DEPENDENT_OTHER_COUNT          NUMBER(3),
  INCOME_TOTAL                   NUMBER,
  EXPENSE_TOTAL                  NUMBER,
  NET_WORTH_TOTAL                NUMBER,
  PREV_YEAR_THRESHOLD_IND        VARCHAR2(1 BYTE),
  MT_NOT_REQUIRED_DATE           DATE,
  REFUSED_TO_SIGN_IND            VARCHAR2(1 BYTE),
  INCOME_NET_TOTAL               NUMBER,
  FUTURE_TEST_FLAG               NUMBER(1)      DEFAULT 0,
  ASSIGNED_CONTACT_REP_USERNAME  VARCHAR2(30 BYTE),
  GMT_SITE_ADDRESS_IND           VARCHAR2(1 BYTE),
  GMT_ADDRESS_LINE1              VARCHAR2(100 BYTE),
  GMT_ADDRESS_LINE2              VARCHAR2(100 BYTE),
  GMT_ADDRESS_LINE3              VARCHAR2(100 BYTE),
  GMT_CITY                       VARCHAR2(60 BYTE),
  GMT_STATE_CODE                 VARCHAR2(50 BYTE),
  GMT_COUNTY_CODE                VARCHAR2(50 BYTE),
  GMT_COUNTRY_CODE               VARCHAR2(50 BYTE),
  GMT_POSTAL_CODE                VARCHAR2(20 BYTE),
  GMT_MSA_CODE                   VARCHAR2(20 BYTE),
  CAD_ADJUSTED_IND               VARCHAR2(1 BYTE),
  FINANCIAL_HARDSHIP_IND         VARCHAR2(1 BYTE),
  HARDSHIP_REVIEW_DATE           DATE,
  REMARKS                        VARCHAR2(4000 BYTE),
  HARDSHIP_EFFECTIVE_DATE        DATE,
  HARDSHIP_REASON_TEXT           VARCHAR2(250 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  PG8_RELAXATION_IND             VARCHAR2(1 BYTE),
  STD_WORKFLOWCASESTATUS_ID      NUMBER(20),
  IVM_CONVERSION_DATE            DATE,
  BT_FINANCIAL_IND               VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INCOME_TEST IS 'The table stores detailed data about a persons means test. This test is performed to determine a persons financial eligibility to receive VHA healthcare services. This table stores data about a persons hardship status, whether they have agreed to pay any deductibles, whether they contribute to the support of a spouse or dependent, as well as other financial indicators used to determine eligibility. PERSON_ID Foreign key to the PERSON table. INCOME_TEST_TYPE_ID Foreign key to the INCOME_TEST_TYPE table. Indicates the type of test being performed (for example, Means Test, Co-Pay Exemption Test). INCOME_TEST_SRC_TYPE_ID Foreign key to the INCOME_TEST_SRC_TYPE table. Indicates the source of the income test (for example VAMC, HEC). MEANS_TEST_STATUS_TYPE_ID Foreign key to the MEANS_TEST_STATUS_TYPE table. Indicates the status of the particular means test (for example, Exempt, Pending Adjudication). MT_NOT_REQD_RSN_TYPE_ID Foreign key to the MT_NOT_REQD_RSN_TYPE table. Indicates the reason a means test is not required (for example, Purple Heart, POW). STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site that is performing the income test. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_TEST_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_TEST_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_TEST_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.MEANS_TEST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.MT_NOT_REQD_RSN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_VERIFY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.HARDSHIP_REVIEW_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.SITE_CONDUCTING_TEST_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_TEST_EFFECTIVE_DATE IS 'BR3383 Effective Date of Test is defined as the effective date of the primary test for the income year. Date must be precise. This date + 364 days is the date that a new MT is required.';

COMMENT ON COLUMN ADR.INCOME_TEST.IVM_TEST_COMPLETION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.PRIMARY_TEST_IND IS 'BR6797 Primary MT indicator is defined as the indicator that is set by the system when a MT is determined to be primary.';

COMMENT ON COLUMN ADR.INCOME_TEST.ADJUDICATION_SENT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.ADJUDICATED_DATE IS 'BR3422 Date/Time of Adjudication is defined as the last date/time the financial test was adjudicated. Date must be precise.';

COMMENT ON COLUMN ADR.INCOME_TEST.THRESHOLD_MT_EXEMPT_AMOUNT IS 'BR6239 Threshold A is defined as the income threshold A as determined by the system for the veteran.  The threshold is determined by the system in the following manner: Cat A Vet Income + Cat A First Dependent+(Cat A Per Dependent*(Num Dependent- First Dependent)). The means test thresholds are released annually by VHA and include the veteran and dependent income thresholds for the year.';

COMMENT ON COLUMN ADR.INCOME_TEST.THRESHOLD_GMT_AMOUNT IS 'BR7446 GMT Threshold dollar amount is defined as the income GMT threshold as determined by the system for the veteran. This threshold is derived by a look up to the HUD indices table. The threshold amount is computed as FIPS code + MSA code for # of dependents.';

COMMENT ON COLUMN ADR.INCOME_TEST.THRESHOLD_NETWORTH_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.THRESHOLD_PENSION_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.DEDUCTIBLE_EXPENSE_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_VERIFY_STATUS_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.FARM_RANCH_INCOME_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.DISCLOSE_FINANCIAL_INCOME_IND IS 'BR529 Disclose Financial Information Indicator is defined as an indicator where the Veteran chooses Yes/No to disclose Financial Information.';

COMMENT ON COLUMN ADR.INCOME_TEST.AGREED_TO_PAY_DEDUCTIBLE_IND IS 'BR531 Agree to Pay Deductible Indicator is defined as an indicator where the Veteran chooses Yes or No to pay deductible. Veteran must answer yes or no if veteran has a MT status of non exempt. This indicator is set to YES when an IVM case is closed converted by the IVM process. IF veteran chooses No he is considered Ineligible.';

COMMENT ON COLUMN ADR.INCOME_TEST.DEPENDENT_OTHER_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.INCOME_TOTAL IS 'BR3414 Income is defined as a summation of each applicable family members income for the previous calendar year from date of test. If the veteran is married, the spouses income will only be collected and subsequently included in the test under certain conditions. The veteran and spouse must either live together or if they do not live together, the veteran must have contributed > $600 to the spouses support during the previous Calendar year. If the veteran has dependent children, the childrens income will only be collected and subsequently included in the test under certain conditions.  Only children who have income and whose income is available to the veteran need to provide income information.';

COMMENT ON COLUMN ADR.INCOME_TEST.EXPENSE_TOTAL IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.NET_WORTH_TOTAL IS 'BR3418 Net Worth is defined as a summation of the veteran and spouse (if applicable) net worth minus any debts for the previous calendar year from date of test. If the veteran is married, the spouses income will only be collected and subsequently included in the test under certain conditions. The veteran and spouse must either live together or if they do not live together, the veteran must have contributed > $600 to the spouses support during the previous Calendar year. When determining the means test status and subsequent category of care, the system will require net worth be entered for those veterans whos income is below the MT income threshold (require mandatory care). If the income plus net worth exceed the annual net worth threshold, the case can be sent to adjudication for the test. Calculation is as follows: Net worth =(Bank account amount + Stock and Bond amounts + Real Property Value + (Other property and assets) - Debts)';

COMMENT ON COLUMN ADR.INCOME_TEST.PREV_YEAR_THRESHOLD_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.MT_NOT_REQUIRED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.REFUSED_TO_SIGN_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST.GMT_ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.INCOME_TEST.GMT_ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.INCOME_TEST.GMT_ADDRESS_LINE3 IS 'BR5 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.INCOME_TEST.GMT_CITY IS 'BR9 City is defined as the name of a city used for an address.';

COMMENT ON COLUMN ADR.INCOME_TEST.CAD_ADJUSTED_IND IS 'Centralized Anniversary Date adjusted flag';

COMMENT ON COLUMN ADR.INCOME_TEST.FINANCIAL_HARDSHIP_IND IS 'BR6799 Hardship Indicator is defined as the indicator that identifies that the veteran has a hardship applied. ';

COMMENT ON COLUMN ADR.INCOME_TEST.HARDSHIP_REVIEW_DATE IS 'Hardship Review Date is defined as the date the hardship should be reviewed. Date must be precise.';

COMMENT ON COLUMN ADR.INCOME_TEST.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.INCOME_TEST.HARDSHIP_EFFECTIVE_DATE IS 'Hardship effective date is defined as the date when the hardship is effective. Date must be precise.';

COMMENT ON COLUMN ADR.INCOME_TEST.HARDSHIP_REASON_TEXT IS 'BR4179 Hardship reason is defined as a reason this person has been granted a hardship. This is a future requirement and the customer will need to define a set of acceptable reasons that can be chosen. ';

COMMENT ON COLUMN ADR.INCOME_TEST.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOME_TEST.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOME_TEST.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOME_TEST.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOME_TEST.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.INCOME_TEST.STD_WORKFLOWCASESTATUS_ID IS 'Uses the lookup table STD_WORKFLOWCASESTATUS for all IVM Case Status values';

COMMENT ON COLUMN ADR.INCOME_TEST.IVM_CONVERSION_DATE IS 'Indicates Conversion date associated with IVM case status of a Veteran';


CREATE UNIQUE INDEX ADR.INCMTST_PK ON ADR.INCOME_TEST
(INCOME_TEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_PRSN_FK_I ON ADR.INCOME_TEST
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STDINST_FK2_I ON ADR.INCOME_TEST
(HARDSHIP_REVIEW_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STDINST_FK_I ON ADR.INCOME_TEST
(SITE_CONDUCTING_TEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STD_INCMTSTSRC_FK_I ON ADR.INCOME_TEST
(INCOME_TEST_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STD_INCMTSTYP_FK_I ON ADR.INCOME_TEST
(INCOME_TEST_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STD_MNSTSTST_FK2_I ON ADR.INCOME_TEST
(INCOME_VERIFY_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STD_MNSTSTST_FK_I ON ADR.INCOME_TEST
(MEANS_TEST_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTST_STD_MTNOTREQRS_FK_I ON ADR.INCOME_TEST
(MT_NOT_REQD_RSN_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.INCOME_TEST_AUDIR_TRGR
after Update or Delete or Insert
on ADR.INCOME_TEST
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
       mDML char(1);
begin

    if DELETING then
        insert into INCOME_TEST_H
        (
        INCOME_TEST_H_ID,
        INCOME_TEST_ID,
        PERSON_ID,
        INCOME_TEST_TYPE_ID,
        INCOME_TEST_SRC_TYPE_ID,
        MEANS_TEST_STATUS_TYPE_ID,
        MT_NOT_REQD_RSN_TYPE_ID,
        SITE_CONDUCTING_TEST_ID,
        INCOME_YEAR,
        INCOME_TEST_EFFECTIVE_DATE,
        IVM_TEST_COMPLETION_DATE,
        PRIMARY_TEST_IND,
        ADJUDICATION_SENT_DATE,
        ADJUDICATED_DATE,
        THRESHOLD_MT_EXEMPT_AMOUNT,
        THRESHOLD_GMT_AMOUNT,
        THRESHOLD_NETWORTH_AMOUNT,
        THRESHOLD_PENSION_AMOUNT,
        THRESHOLD_INCOME_AMOUNT,
        DEDUCTIBLE_EXPENSE_AMOUNT,
        INCOME_VERIFY_STATUS_DATE,
        FARM_RANCH_INCOME_AMOUNT,
        DISCLOSE_FINANCIAL_INCOME_IND,
        AGREED_TO_PAY_DEDUCTIBLE_IND,
        DEPENDENT_OTHER_COUNT,
        INCOME_TOTAL,
        EXPENSE_TOTAL,
        NET_WORTH_TOTAL,
        PREV_YEAR_THRESHOLD_IND,
        MT_NOT_REQUIRED_DATE,
        REFUSED_TO_SIGN_IND,
        GMT_ADDRESS_LINE1,
        GMT_ADDRESS_LINE2,
        GMT_CITY,
        GMT_STATE_CODE,
        GMT_COUNTY_CODE,
        GMT_POSTAL_CODE,
        GMT_MSA_CODE,
        GMT_SITE_ADDRESS_IND,
        FINANCIAL_HARDSHIP_IND,
        HARDSHIP_REVIEW_DATE,
        HARDSHIP_EFFECTIVE_DATE,
        REMARKS,
        HARDSHIP_REASON_TEXT,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        CAD_ADJUSTED_IND,
        HARDSHIP_REVIEW_SITE_ID,
        INCOME_VERIFY_STATUS_TYPE_ID,
        GMT_ADDRESS_LINE3,
        GMT_COUNTRY_CODE,
        FUTURE_TEST_FLAG,
        INCOME_NET_TOTAL,
        ASSIGNED_CONTACT_REP_USERNAME,
        PG8_RELAXATION_IND,        --ccr 1802
        TRANSACTION_TYPE_CODE,
        STD_WORKFLOWCASESTATUS_ID,
        IVM_CONVERSION_DATE,
        BT_FINANCIAL_IND
          )
        values
        (
        INCOME_TEST_H_S.nextval,
        :old.INCOME_TEST_ID,
        :old.PERSON_ID,
        :old.INCOME_TEST_TYPE_ID,
        :old.INCOME_TEST_SRC_TYPE_ID,
        :old.MEANS_TEST_STATUS_TYPE_ID,
        :old.MT_NOT_REQD_RSN_TYPE_ID,
        :old.SITE_CONDUCTING_TEST_ID,
        :old.INCOME_YEAR,
        :old.INCOME_TEST_EFFECTIVE_DATE,
        :old.IVM_TEST_COMPLETION_DATE,
        :old.PRIMARY_TEST_IND,
        :old.ADJUDICATION_SENT_DATE,
        :old.ADJUDICATED_DATE,
        :old.THRESHOLD_MT_EXEMPT_AMOUNT,
        :old.THRESHOLD_GMT_AMOUNT,
        :old.THRESHOLD_NETWORTH_AMOUNT,
        :old.THRESHOLD_PENSION_AMOUNT,
        :old.THRESHOLD_INCOME_AMOUNT,
        :old.DEDUCTIBLE_EXPENSE_AMOUNT,
        :old.INCOME_VERIFY_STATUS_DATE,
        :old.FARM_RANCH_INCOME_AMOUNT,
        :old.DISCLOSE_FINANCIAL_INCOME_IND,
        :old.AGREED_TO_PAY_DEDUCTIBLE_IND,
        :old.DEPENDENT_OTHER_COUNT,
        :old.INCOME_TOTAL,
        :old.EXPENSE_TOTAL,
        :old.NET_WORTH_TOTAL,
        :old.PREV_YEAR_THRESHOLD_IND,
        :old.MT_NOT_REQUIRED_DATE,
        :old.REFUSED_TO_SIGN_IND,
        :old.GMT_ADDRESS_LINE1,
        :old.GMT_ADDRESS_LINE2,
        :old.GMT_CITY,
        :old.GMT_STATE_CODE,
        :old.GMT_COUNTY_CODE,
        :old.GMT_POSTAL_CODE,
        :old.GMT_MSA_CODE,
        :old.GMT_SITE_ADDRESS_IND,
        :old.FINANCIAL_HARDSHIP_IND,
        :old.HARDSHIP_REVIEW_DATE,
        :old.HARDSHIP_EFFECTIVE_DATE,
        :old.REMARKS,
        :old.HARDSHIP_REASON_TEXT,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        :old.CAD_ADJUSTED_IND,
        :old.HARDSHIP_REVIEW_SITE_ID,
        :old.INCOME_VERIFY_STATUS_TYPE_ID,
        :old.GMT_ADDRESS_LINE3,
        :old.GMT_COUNTRY_CODE,
        :old.FUTURE_TEST_FLAG,
        :old.INCOME_NET_TOTAL,
        :old.ASSIGNED_CONTACT_REP_USERNAME,
        :OLD.PG8_RELAXATION_IND,    -- ccr 1802
        'D',
        :old.STD_WORKFLOWCASESTATUS_ID,
        :old.IVM_CONVERSION_DATE,
        :old.BT_FINANCIAL_IND
        );

    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into INCOME_TEST_H
        (
        INCOME_TEST_H_ID,
        INCOME_TEST_ID,
        PERSON_ID,
        INCOME_TEST_TYPE_ID,
        INCOME_TEST_SRC_TYPE_ID,
        MEANS_TEST_STATUS_TYPE_ID,
        MT_NOT_REQD_RSN_TYPE_ID,
        SITE_CONDUCTING_TEST_ID,
        INCOME_YEAR,
        INCOME_TEST_EFFECTIVE_DATE,
        IVM_TEST_COMPLETION_DATE,
        PRIMARY_TEST_IND,
        ADJUDICATION_SENT_DATE,
        ADJUDICATED_DATE,
        THRESHOLD_MT_EXEMPT_AMOUNT,
        THRESHOLD_GMT_AMOUNT,
        THRESHOLD_NETWORTH_AMOUNT,
        THRESHOLD_PENSION_AMOUNT,
        THRESHOLD_INCOME_AMOUNT,
        DEDUCTIBLE_EXPENSE_AMOUNT,
        INCOME_VERIFY_STATUS_DATE,
        FARM_RANCH_INCOME_AMOUNT,
        DISCLOSE_FINANCIAL_INCOME_IND,
        AGREED_TO_PAY_DEDUCTIBLE_IND,
        DEPENDENT_OTHER_COUNT,
        INCOME_TOTAL,
        EXPENSE_TOTAL,
        NET_WORTH_TOTAL,
        PREV_YEAR_THRESHOLD_IND,
        MT_NOT_REQUIRED_DATE,
        REFUSED_TO_SIGN_IND,
        GMT_ADDRESS_LINE1,
        GMT_ADDRESS_LINE2,
        GMT_CITY,
        GMT_STATE_CODE,
        GMT_COUNTY_CODE,
        GMT_POSTAL_CODE,
        GMT_MSA_CODE,
        GMT_SITE_ADDRESS_IND,
        FINANCIAL_HARDSHIP_IND,
        HARDSHIP_REVIEW_DATE,
        HARDSHIP_EFFECTIVE_DATE,
        REMARKS,
        HARDSHIP_REASON_TEXT,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        CAD_ADJUSTED_IND,
        HARDSHIP_REVIEW_SITE_ID,
        INCOME_VERIFY_STATUS_TYPE_ID,
        GMT_ADDRESS_LINE3,
        GMT_COUNTRY_CODE,
        FUTURE_TEST_FLAG,
        INCOME_NET_TOTAL,
        ASSIGNED_CONTACT_REP_USERNAME,
        PG8_RELAXATION_IND,        --ccr 1802
        TRANSACTION_TYPE_CODE,
        STD_WORKFLOWCASESTATUS_ID,
        IVM_CONVERSION_DATE,
        BT_FINANCIAL_IND
          )
        values
        (
        INCOME_TEST_H_S.nextval,
        :new.INCOME_TEST_ID,
        :new.PERSON_ID,
        :new.INCOME_TEST_TYPE_ID,
        :new.INCOME_TEST_SRC_TYPE_ID,
        :new.MEANS_TEST_STATUS_TYPE_ID,
        :new.MT_NOT_REQD_RSN_TYPE_ID,
        :new.SITE_CONDUCTING_TEST_ID,
        :new.INCOME_YEAR,
        :new.INCOME_TEST_EFFECTIVE_DATE,
        :new.IVM_TEST_COMPLETION_DATE,
        :new.PRIMARY_TEST_IND,
        :new.ADJUDICATION_SENT_DATE,
        :new.ADJUDICATED_DATE,
        :new.THRESHOLD_MT_EXEMPT_AMOUNT,
        :new.THRESHOLD_GMT_AMOUNT,
        :new.THRESHOLD_NETWORTH_AMOUNT,
        :new.THRESHOLD_PENSION_AMOUNT,
        :new.THRESHOLD_INCOME_AMOUNT,
        :new.DEDUCTIBLE_EXPENSE_AMOUNT,
        :new.INCOME_VERIFY_STATUS_DATE,
        :new.FARM_RANCH_INCOME_AMOUNT,
        :new.DISCLOSE_FINANCIAL_INCOME_IND,
        :new.AGREED_TO_PAY_DEDUCTIBLE_IND,
        :new.DEPENDENT_OTHER_COUNT,
        :new.INCOME_TOTAL,
        :new.EXPENSE_TOTAL,
        :new.NET_WORTH_TOTAL,
        :new.PREV_YEAR_THRESHOLD_IND,
        :new.MT_NOT_REQUIRED_DATE,
        :new.REFUSED_TO_SIGN_IND,
        :new.GMT_ADDRESS_LINE1,
        :new.GMT_ADDRESS_LINE2,
        :new.GMT_CITY,
        :new.GMT_STATE_CODE,
        :new.GMT_COUNTY_CODE,
        :new.GMT_POSTAL_CODE,
        :new.GMT_MSA_CODE,
        :new.GMT_SITE_ADDRESS_IND,
        :new.FINANCIAL_HARDSHIP_IND,
        :new.HARDSHIP_REVIEW_DATE,
        :new.HARDSHIP_EFFECTIVE_DATE,
        :new.REMARKS,
        :new.HARDSHIP_REASON_TEXT,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        :new.CAD_ADJUSTED_IND,
        :new.HARDSHIP_REVIEW_SITE_ID,
        :new.INCOME_VERIFY_STATUS_TYPE_ID,
        :new.GMT_ADDRESS_LINE3,
        :new.GMT_COUNTRY_CODE,
        :new.FUTURE_TEST_FLAG,
        :new.INCOME_NET_TOTAL,
        :new.ASSIGNED_CONTACT_REP_USERNAME,
        :NEW.PG8_RELAXATION_IND,        --ccr 1802
        mDML,
         :new.STD_WORKFLOWCASESTATUS_ID,
         :new.IVM_CONVERSION_DATE,
         :new.BT_FINANCIAL_IND
        );

    end if;

end  INCOME_TEST_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM INCOME_TEST FOR ADR.INCOME_TEST;


ALTER TABLE ADR.INCOME_TEST ADD (
  CONSTRAINT AVCON_1172521913_FUTUR_001
  CHECK (FUTURE_TEST_FLAG BETWEEN 0 AND 0 OR FUTURE_TEST_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067023
  CHECK ("INCOME_TEST_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067024
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067025
  CHECK ("INCOME_TEST_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067026
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067027
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067028
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067029
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067030
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INCMTST_PK
  PRIMARY KEY
  (INCOME_TEST_ID)
  USING INDEX ADR.INCMTST_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.INCOME_TEST ADD (
  CONSTRAINT INCMTST_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.INCOME_TEST TO ADREXT;

GRANT SELECT ON ADR.INCOME_TEST TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.INCOME_TEST TO ADR_RW;

GRANT SELECT ON ADR.INCOME_TEST TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOME_TEST TO ES_RW;
ALTER TABLE ADR.INCOME_TEST_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOME_TEST_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOME_TEST_DETAIL
(
  INCOME_TEST_DETAIL_ID         NUMBER(20),
  INCOME_TEST_ID                NUMBER(20),
  INCOME_TEST_TYPE_ID           NUMBER(20),
  TEST_STATUS_TYPE_ID           NUMBER(20),
  TEST_DETMND_STATUS_TYPE_ID    NUMBER(20),
  INCOME_TEST_LAST_EDITED_DATE  DATE,
  INCOME_TEST_COMPLETED_DATE    DATE,
  INCOME_TEST_EXPIRATION_DATE   DATE,
  INCOME_TEST_EFFECTIVE_DATE    DATE,
  INCOME_TEST_SIGNED_DATE       DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INCOME_TEST_DETAIL IS 'The table stores information about a specific income test, such as the effective and expiration dates. INCOME_TEST_ID Foreign key to the INCOME_TEST table. INCOME_TEST_TYPE_ID Foreign key to the INCOME_TEST_TYPE table. Indicates the type of test being performed (for example, Means Test, Co-Pay Exemption Test). TEST_STATUS_TYPE_ID Foreign key to the MEANS_TEST_STATUS_TYPE table. TEST_DETMND_STATUS_TYPE_ID Foreign key to the MEANS_TEST_STATUS_TYPE table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.TEST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.TEST_DETMND_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_LAST_EDITED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_COMPLETED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.INCOME_TEST_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.INCMTSTDTL_INCMTST_FK_I ON ADR.INCOME_TEST_DETAIL
(INCOME_TEST_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTSTDTL_MTSTATTYP_FK_I ON ADR.INCOME_TEST_DETAIL
(TEST_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTSTDTL_MTSTATTYP_H_FK_I ON ADR.INCOME_TEST_DETAIL
(TEST_DETMND_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.INCMTSTDTL_PK ON ADR.INCOME_TEST_DETAIL
(INCOME_TEST_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTSTDTL_STD_INCMTSTYP_FK_I ON ADR.INCOME_TEST_DETAIL
(INCOME_TEST_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.INCOME_TEST_DET_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."INCOME_TEST_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into INCOME_TEST_DETAIL_H
		(
		INCOME_TEST_DETAIL_H_ID,
		INCOME_TEST_DETAIL_ID,
		INCOME_TEST_ID,
		INCOME_TEST_TYPE_ID,
		TEST_STATUS_TYPE_ID,
		TEST_DETMND_STATUS_TYPE_ID,
		INCOME_TEST_LAST_EDITED_DATE,
		INCOME_TEST_COMPLETED_DATE,
		INCOME_TEST_EXPIRATION_DATE,
		INCOME_TEST_EFFECTIVE_DATE,
		INCOME_TEST_SIGNED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INCOME_TEST_DETAIL_H_S.nextval,
		:old.INCOME_TEST_DETAIL_ID,
		:old.INCOME_TEST_ID,
		:old.INCOME_TEST_TYPE_ID,
		:old.TEST_STATUS_TYPE_ID,
		:old.TEST_DETMND_STATUS_TYPE_ID,
		:old.INCOME_TEST_LAST_EDITED_DATE,
		:old.INCOME_TEST_COMPLETED_DATE,
		:old.INCOME_TEST_EXPIRATION_DATE,
		:old.INCOME_TEST_EFFECTIVE_DATE,
		:old.INCOME_TEST_SIGNED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into INCOME_TEST_DETAIL_H
		(
		INCOME_TEST_DETAIL_H_ID,
		INCOME_TEST_DETAIL_ID,
		INCOME_TEST_ID,
		INCOME_TEST_TYPE_ID,
		TEST_STATUS_TYPE_ID,
		TEST_DETMND_STATUS_TYPE_ID,
		INCOME_TEST_LAST_EDITED_DATE,
		INCOME_TEST_COMPLETED_DATE,
		INCOME_TEST_EXPIRATION_DATE,
		INCOME_TEST_EFFECTIVE_DATE,
		INCOME_TEST_SIGNED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INCOME_TEST_DETAIL_H_S.nextval,
		:new.INCOME_TEST_DETAIL_ID,
		:new.INCOME_TEST_ID,
		:new.INCOME_TEST_TYPE_ID,
		:new.TEST_STATUS_TYPE_ID,
		:new.TEST_DETMND_STATUS_TYPE_ID,
		:new.INCOME_TEST_LAST_EDITED_DATE,
		:new.INCOME_TEST_COMPLETED_DATE,
		:new.INCOME_TEST_EXPIRATION_DATE,
		:new.INCOME_TEST_EFFECTIVE_DATE,
		:new.INCOME_TEST_SIGNED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  INCOME_TEST_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM INCOME_TEST_DETAIL FOR ADR.INCOME_TEST_DETAIL;


ALTER TABLE ADR.INCOME_TEST_DETAIL ADD (
  CONSTRAINT SYS_C0067031
  CHECK ("INCOME_TEST_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067032
  CHECK ("INCOME_TEST_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067033
  CHECK ("INCOME_TEST_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067034
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067035
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067036
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067037
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067038
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INCMTSTDTL_PK
  PRIMARY KEY
  (INCOME_TEST_DETAIL_ID)
  USING INDEX ADR.INCMTSTDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.INCOME_TEST_DETAIL ADD (
  CONSTRAINT INCMTSTDTL_INCMTST_FK 
  FOREIGN KEY (INCOME_TEST_ID) 
  REFERENCES ADR.INCOME_TEST (INCOME_TEST_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.INCOME_TEST_DETAIL TO ADREXT;

GRANT SELECT ON ADR.INCOME_TEST_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.INCOME_TEST_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.INCOME_TEST_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOME_TEST_DETAIL TO ES_RW;
ALTER TABLE ADR.INCOME_TEST_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOME_TEST_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOME_TEST_DETAIL_H
(
  INCOME_TEST_DETAIL_H_ID       NUMBER(20),
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE),
  INCOME_TEST_DETAIL_ID         NUMBER(20),
  INCOME_TEST_ID                NUMBER(20),
  INCOME_TEST_TYPE_ID           NUMBER(20),
  TEST_STATUS_TYPE_ID           NUMBER(20),
  TEST_DETMND_STATUS_TYPE_ID    NUMBER(20),
  INCOME_TEST_LAST_EDITED_DATE  DATE,
  INCOME_TEST_COMPLETED_DATE    DATE,
  INCOME_TEST_EXPIRATION_DATE   DATE,
  INCOME_TEST_EFFECTIVE_DATE    DATE,
  INCOME_TEST_SIGNED_DATE       DATE,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INCOME_TEST_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.TEST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.TEST_DETMND_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_LAST_EDITED_DATE IS 'BR3393 Date/Time last edited is defined as the date/time the test was last edited. This attribute is updated when a value associated with an income test is modified in any way. Date must be precise. Cannot be a Future Date.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.INCOME_TEST_COMPLETED_DATE IS 'BR3385 Date/time completed is defined as the date/time that the test was completed. This field is used to indicate when a veteran agreed to pay co-payment, and is used for billing purposes. Date must be precise. Cannot be a Future Date.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOME_TEST_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.INCMTSTDTH_INCMTSTID_NU_I ON ADR.INCOME_TEST_DETAIL_H
(INCOME_TEST_ID, TEST_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.INCMTSTDTH_PK ON ADR.INCOME_TEST_DETAIL_H
(INCOME_TEST_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM INCOME_TEST_DETAIL_H FOR ADR.INCOME_TEST_DETAIL_H;


ALTER TABLE ADR.INCOME_TEST_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_012
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066125
  CHECK ("INCOME_TEST_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066126
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066127
  CHECK ("INCOME_TEST_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066128
  CHECK ("INCOME_TEST_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066129
  CHECK ("INCOME_TEST_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066130
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066131
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066132
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066133
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066134
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INCMTSTDTH_PK
  PRIMARY KEY
  (INCOME_TEST_DETAIL_H_ID)
  USING INDEX ADR.INCMTSTDTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.INCOME_TEST_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.INCOME_TEST_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.INCOME_TEST_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOME_TEST_DETAIL_H TO ES_RW;
ALTER TABLE ADR.INCOME_TEST_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOME_TEST_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOME_TEST_H
(
  INCOME_TEST_H_ID               NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  INCOME_TEST_ID                 NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INCOME_TEST_TYPE_ID            NUMBER(20),
  INCOME_TEST_SRC_TYPE_ID        NUMBER(20),
  MEANS_TEST_STATUS_TYPE_ID      NUMBER(20),
  MT_NOT_REQD_RSN_TYPE_ID        NUMBER(20),
  HARDSHIP_REVIEW_SITE_ID        NUMBER(20),
  INCOME_VERIFY_STATUS_TYPE_ID   NUMBER(20),
  SITE_CONDUCTING_TEST_ID        NUMBER(20),
  INCOME_YEAR                    NUMBER(4),
  INCOME_TEST_EFFECTIVE_DATE     DATE,
  IVM_TEST_COMPLETION_DATE       DATE,
  PRIMARY_TEST_IND               VARCHAR2(1 BYTE),
  ADJUDICATION_SENT_DATE         DATE,
  ADJUDICATED_DATE               DATE,
  THRESHOLD_MT_EXEMPT_AMOUNT     NUMBER(10,2),
  THRESHOLD_GMT_AMOUNT           NUMBER(10,2),
  THRESHOLD_NETWORTH_AMOUNT      NUMBER(10,2),
  THRESHOLD_PENSION_AMOUNT       NUMBER(10,2),
  THRESHOLD_INCOME_AMOUNT        NUMBER(10,2),
  DEDUCTIBLE_EXPENSE_AMOUNT      NUMBER(10,2),
  INCOME_VERIFY_STATUS_DATE      DATE,
  FARM_RANCH_INCOME_AMOUNT       NUMBER(10,2),
  DISCLOSE_FINANCIAL_INCOME_IND  VARCHAR2(1 BYTE),
  AGREED_TO_PAY_DEDUCTIBLE_IND   VARCHAR2(1 BYTE),
  DEPENDENT_OTHER_COUNT          NUMBER(3),
  INCOME_TOTAL                   NUMBER,
  EXPENSE_TOTAL                  NUMBER,
  NET_WORTH_TOTAL                NUMBER,
  PREV_YEAR_THRESHOLD_IND        VARCHAR2(1 BYTE),
  MT_NOT_REQUIRED_DATE           DATE,
  REFUSED_TO_SIGN_IND            VARCHAR2(1 BYTE),
  FUTURE_TEST_FLAG               NUMBER(1)      DEFAULT 0,
  INCOME_NET_TOTAL               NUMBER,
  ASSIGNED_CONTACT_REP_USERNAME  VARCHAR2(30 BYTE),
  GMT_ADDRESS_LINE1              VARCHAR2(100 BYTE),
  GMT_ADDRESS_LINE2              VARCHAR2(100 BYTE),
  GMT_ADDRESS_LINE3              VARCHAR2(100 BYTE),
  GMT_CITY                       VARCHAR2(60 BYTE),
  GMT_STATE_CODE                 VARCHAR2(50 BYTE),
  GMT_COUNTY_CODE                VARCHAR2(50 BYTE),
  GMT_POSTAL_CODE                VARCHAR2(20 BYTE),
  GMT_COUNTRY_CODE               VARCHAR2(50 BYTE),
  GMT_MSA_CODE                   VARCHAR2(20 BYTE),
  GMT_SITE_ADDRESS_IND           VARCHAR2(1 BYTE),
  FINANCIAL_HARDSHIP_IND         VARCHAR2(1 BYTE),
  HARDSHIP_REVIEW_DATE           DATE,
  HARDSHIP_EFFECTIVE_DATE        DATE,
  HARDSHIP_REASON_TEXT           VARCHAR2(250 BYTE),
  CAD_ADJUSTED_IND               VARCHAR2(1 BYTE),
  REMARKS                        VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  PG8_RELAXATION_IND             VARCHAR2(1 BYTE),
  STD_WORKFLOWCASESTATUS_ID      NUMBER(20),
  IVM_CONVERSION_DATE            DATE,
  BT_FINANCIAL_IND               VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_TEST_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_TEST_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_TEST_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_TEST_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.MEANS_TEST_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.MT_NOT_REQD_RSN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.HARDSHIP_REVIEW_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_VERIFY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.SITE_CONDUCTING_TEST_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.PRIMARY_TEST_IND IS 'BR6797 Primary MT indicator is defined as the indicator that is set by the system when a MT is determined to be primary.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.ADJUDICATION_SENT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.ADJUDICATED_DATE IS 'BR3422 Date/Time of Adjudication is defined as the last date/time the financial test was adjudicated. Date must be precise.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.THRESHOLD_MT_EXEMPT_AMOUNT IS 'BR6239 Threshold A is defined as the income threshold A as determined by the system for the veteran.  The threshold is determined by the system in the following manner: Cat A Vet Income + Cat A First Dependent+(Cat A Per Dependent*(Num Dependent- First Dependent)). The means test thresholds are released annually by VHA and include the veteran and dependent income thresholds for the year.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.THRESHOLD_GMT_AMOUNT IS 'BR7446 GMT Threshold dollar amount is defined as the income GMT threshold as determined by the system for the veteran. This threshold is derived by a look up to the HUD indices table. The threshold amount is computed as FIPS code + MSA code for # of dependents.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.THRESHOLD_NETWORTH_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.THRESHOLD_PENSION_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.THRESHOLD_INCOME_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.DISCLOSE_FINANCIAL_INCOME_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.AGREED_TO_PAY_DEDUCTIBLE_IND IS 'BR531 Agree to Pay Deductible Indicator is defined as an indicator where the Veteran chooses Yes or No to pay deductible. Veteran must answer yes or no if veteran has a MT status of non exempt. This indicator is set to YES when an IVM case is closed converted by the IVM process. IF veteran chooses No he is considered Ineligible.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.INCOME_TOTAL IS 'BR3414 Income is defined as a summation of each applicable family members income for the previous calendar year from date of test. If the veteran is married, the spouses income will only be collected and subsequently included in the test under certain conditions. The veteran and spouse must either live together or if they do not live together, the veteran must have contributed > $600 to the spouses support during the previous Calendar year. If the veteran has dependent children, the childrens income will only be collected and subsequently included in the test under certain conditions.  Only children who have income and whose income is available to the veteran need to provide income information.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.EXPENSE_TOTAL IS 'BR3416 Deductible Expenses is defined as the summation of the veterans deductible expenses (medical, burial and educational).';

COMMENT ON COLUMN ADR.INCOME_TEST_H.NET_WORTH_TOTAL IS 'BR3418 Net Worth is defined as a summation of the veteran and spouse (if applicable) net worth minus any debts for the previous calendar year from date of test. If the veteran is married, the spouses income will only be collected and subsequently included in the test under certain conditions. The veteran and spouse must either live together or if they do not live together, the veteran must have contributed > $600 to the spouses support during the previous Calendar year. When determining the means test status and subsequent category of care, the system will require net worth be entered for those veterans whos income is below the MT income threshold (require mandatory care). If the income plus net worth exceed the annual net worth threshold, the case can be sent to adjudication for the test. Calculation is as follows: Net worth =(Bank account amount + Stock and Bond amounts + Real Property Value + (Other property and assets) - Debts)';

COMMENT ON COLUMN ADR.INCOME_TEST_H.PREV_YEAR_THRESHOLD_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.MT_NOT_REQUIRED_DATE IS 'BR5049 Date MT no longer required is defined as the date a MT is determined no longer required.  Date must be precise.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.REFUSED_TO_SIGN_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_ADDRESS_LINE1 IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_ADDRESS_LINE2 IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_ADDRESS_LINE3 IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_CITY IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_STATE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_COUNTY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_POSTAL_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_COUNTRY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_MSA_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.GMT_SITE_ADDRESS_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.FINANCIAL_HARDSHIP_IND IS 'BR6799 Hardship Indicator is defined as the indicator that identifies that the veteran has a hardship applied. ';

COMMENT ON COLUMN ADR.INCOME_TEST_H.HARDSHIP_REVIEW_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.HARDSHIP_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.HARDSHIP_REASON_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.CAD_ADJUSTED_IND IS 'Centralized Anniversary Date adjusted flag';

COMMENT ON COLUMN ADR.INCOME_TEST_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.INCOME_TEST_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOME_TEST_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOME_TEST_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.INCOME_TEST_H.STD_WORKFLOWCASESTATUS_ID IS 'Uses the lookup table STD_WORKFLOWCASESTATUS for all IVM Case Status values';

COMMENT ON COLUMN ADR.INCOME_TEST_H.IVM_CONVERSION_DATE IS 'Indicates Conversion date associated with IVM case status of a Veteran';


CREATE UNIQUE INDEX ADR.INCMTSTH_PK ON ADR.INCOME_TEST_H
(INCOME_TEST_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCMTSTH_PRSNID_NU_I ON ADR.INCOME_TEST_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM INCOME_TEST_H FOR ADR.INCOME_TEST_H;


ALTER TABLE ADR.INCOME_TEST_H ADD (
  CONSTRAINT AVCON_1172521913_FUTUR_000
  CHECK (FUTURE_TEST_FLAG BETWEEN 0 AND 0 OR FUTURE_TEST_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT AVCON_1172521913_TRANS_053
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066971
  CHECK ("INCOME_TEST_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066972
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066973
  CHECK ("INCOME_TEST_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066974
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066975
  CHECK ("INCOME_TEST_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066976
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066977
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066978
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066979
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066980
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INCMTSTH_PK
  PRIMARY KEY
  (INCOME_TEST_H_ID)
  USING INDEX ADR.INCMTSTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.INCOME_TEST_H TO ADR_R;

GRANT SELECT ON ADR.INCOME_TEST_H TO ADR_RW;

GRANT SELECT ON ADR.INCOME_TEST_H TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOME_TEST_H TO ES_RW;
ALTER TABLE ADR.INCOMPETENT_RULING
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOMPETENT_RULING CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOMPETENT_RULING
(
  INCOMPETENT_RULING_ID          NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  CIVIL_INCPTNT_RULING_DATE_TXT  VARCHAR2(14 BYTE),
  VA_INCPTNT_RULING_DATE_TXT     VARCHAR2(14 BYTE),
  RULED_INCOMPETENT_IND          VARCHAR2(1 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INCOMPETENT_RULING IS 'The INCOMPETENT_RULING table stores data about the ruling that deemed a person incompetent to manage his or her own affairs. PERSON_ID Foreign key to the PERSON table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.INCOMPETENT_RULING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.CIVIL_INCPTNT_RULING_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.VA_INCPTNT_RULING_DATE_TXT IS 'BR1621 Date Ruled Incompetent (VA) is defined as the Date the veteran was ruled incompetent to handle her/his VA Funds.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.INCOMPRULE_PK ON ADR.INCOMPETENT_RULING
(INCOMPETENT_RULING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCOMPRULE_PRSN_FK_I ON ADR.INCOMPETENT_RULING
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.INCOMPETENT_RUL_AUDIR_TRGR
after Update or Delete or Insert
on ADR.INCOMPETENT_RULING
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into INCOMPETENT_RULING_H
		(
		INCOMPETENT_RULING_H_ID,
		INCOMPETENT_RULING_ID,
		PERSON_ID,
		CIVIL_INCPTNT_RULING_DATE_TXT,
		VA_INCPTNT_RULING_DATE_TXT,
		RULED_INCOMPETENT_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INCOMPETENT_RULING_H_S.nextval,
		:old.INCOMPETENT_RULING_ID,
		:old.PERSON_ID,
		:old.CIVIL_INCPTNT_RULING_DATE_TXT,
		:old.VA_INCPTNT_RULING_DATE_TXT,
		:old.RULED_INCOMPETENT_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into INCOMPETENT_RULING_H
		(
		INCOMPETENT_RULING_H_ID,
		INCOMPETENT_RULING_ID,
		PERSON_ID,
		CIVIL_INCPTNT_RULING_DATE_TXT,
		VA_INCPTNT_RULING_DATE_TXT,
		RULED_INCOMPETENT_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INCOMPETENT_RULING_H_S.nextval,
		:new.INCOMPETENT_RULING_ID,
		:new.PERSON_ID,
		:new.CIVIL_INCPTNT_RULING_DATE_TXT,
		:new.VA_INCPTNT_RULING_DATE_TXT,
		:new.RULED_INCOMPETENT_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  INCOMPETENT_RUL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM INCOMPETENT_RULING FOR ADR.INCOMPETENT_RULING;


ALTER TABLE ADR.INCOMPETENT_RULING ADD (
  CONSTRAINT INCOMPRULE_PK
  PRIMARY KEY
  (INCOMPETENT_RULING_ID)
  USING INDEX ADR.INCOMPRULE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.INCOMPETENT_RULING ADD (
  CONSTRAINT INCOMPRULE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.INCOMPETENT_RULING TO ADREXT;

GRANT SELECT ON ADR.INCOMPETENT_RULING TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.INCOMPETENT_RULING TO ADR_RW;

GRANT SELECT ON ADR.INCOMPETENT_RULING TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOMPETENT_RULING TO ES_RW;
ALTER TABLE ADR.INCOMPETENT_RULING_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INCOMPETENT_RULING_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.INCOMPETENT_RULING_H
(
  INCOMPETENT_RULING_H_ID        NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  INCOMPETENT_RULING_ID          NUMBER(20),
  PERSON_ID                      NUMBER(20),
  CIVIL_INCPTNT_RULING_DATE_TXT  VARCHAR2(14 BYTE),
  VA_INCPTNT_RULING_DATE_TXT     VARCHAR2(14 BYTE),
  RULED_INCOMPETENT_IND          VARCHAR2(1 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INCOMPETENT_RULING_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.INCOMPETENT_RULING_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.CIVIL_INCPTNT_RULING_DATE_TXT IS 'BR1619 Date Ruled Incompetent (Civil) is defined as the Date the veteran was ruled incompetent to handle her/his personal funds.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.VA_INCPTNT_RULING_DATE_TXT IS 'BR1621 Date Ruled Incompetent (VA) is defined as the Date the veteran was ruled incompetent to handle her/his VA Funds.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INCOMPETENT_RULING_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.INCOMPRULH_PK ON ADR.INCOMPETENT_RULING_H
(INCOMPETENT_RULING_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INCOMPRULH_PRSNID_NU_I ON ADR.INCOMPETENT_RULING_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM INCOMPETENT_RULING_H FOR ADR.INCOMPETENT_RULING_H;


ALTER TABLE ADR.INCOMPETENT_RULING_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_007
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066014
  CHECK ("INCOMPETENT_RULING_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066015
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066016
  CHECK ("INCOMPETENT_RULING_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066017
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066018
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066019
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066020
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066021
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066022
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INCOMPRULH_PK
  PRIMARY KEY
  (INCOMPETENT_RULING_H_ID)
  USING INDEX ADR.INCOMPRULH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.INCOMPETENT_RULING_H TO ADR_R;

GRANT SELECT ON ADR.INCOMPETENT_RULING_H TO ADR_RW;

GRANT SELECT ON ADR.INCOMPETENT_RULING_H TO ES_R;

GRANT INSERT, SELECT ON ADR.INCOMPETENT_RULING_H TO ES_RW;
ALTER TABLE ADR.INELIGIBLE_RULING
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INELIGIBLE_RULING CASCADE CONSTRAINTS;

CREATE TABLE ADR.INELIGIBLE_RULING
(
  INELIGIBLE_RULING_ID    NUMBER(20)            NOT NULL,
  PERSON_ID               NUMBER(20)            NOT NULL,
  INELIGIBLE_DATE         DATE,
  INELIGIBLE_REASON_TEXT  VARCHAR2(250 BYTE),
  VARO_DECISION_TEXT      VARCHAR2(250 BYTE),
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL,
  STD_INSTITUTION_ID      NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INELIGIBLE_RULING IS 'The table stores data about the ruling that deemed a person ineligible to receive healthcare benefits from the VHA. PERSON_ID Foreign key to the PERSON table. STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. Indicates the site at which the ruling was made. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.INELIGIBLE_RULING_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.INELIGIBLE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.INELIGIBLE_REASON_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.VARO_DECISION_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';


CREATE UNIQUE INDEX ADR.INELIGBRLG_PK ON ADR.INELIGIBLE_RULING
(INELIGIBLE_RULING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INELIGBRLG_PRSN_FK_I ON ADR.INELIGIBLE_RULING
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INELIGBRLG_STDINST_FK_I ON ADR.INELIGIBLE_RULING
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.INELIGIBLE_RUL_AUDIR_TRGR
after Update or Delete or Insert
on ADR.INELIGIBLE_RULING
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into INELIGIBLE_RULING_H
		(
		INELIGIBLE_RULING_H_ID,
		INELIGIBLE_RULING_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		INELIGIBLE_DATE,
		INELIGIBLE_REASON_TEXT,
		VARO_DECISION_TEXT,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT     ,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INELIGIBLE_RULING_H_S.nextval,
		:old.INELIGIBLE_RULING_ID,
		:old.PERSON_ID,
		:old.STD_INSTITUTION_ID,
		:old.INELIGIBLE_DATE,
		:old.INELIGIBLE_REASON_TEXT,
		:old.VARO_DECISION_TEXT,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT     ,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into INELIGIBLE_RULING_H
		(
		INELIGIBLE_RULING_H_ID,
		INELIGIBLE_RULING_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		INELIGIBLE_DATE,
		INELIGIBLE_REASON_TEXT,
		VARO_DECISION_TEXT,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		INELIGIBLE_RULING_H_S.nextval,
		:new.INELIGIBLE_RULING_ID,
		:new.PERSON_ID,
		:new.STD_INSTITUTION_ID,
		:new.INELIGIBLE_DATE,
		:new.INELIGIBLE_REASON_TEXT,
		:new.VARO_DECISION_TEXT,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  INELIGIBLE_RUL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM INELIGIBLE_RULING FOR ADR.INELIGIBLE_RULING;


ALTER TABLE ADR.INELIGIBLE_RULING ADD (
  CONSTRAINT INELIGBRLG_PK
  PRIMARY KEY
  (INELIGIBLE_RULING_ID)
  USING INDEX ADR.INELIGBRLG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.INELIGIBLE_RULING ADD (
  CONSTRAINT INELIGBRLG_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.INELIGIBLE_RULING TO ADREXT;

GRANT SELECT ON ADR.INELIGIBLE_RULING TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.INELIGIBLE_RULING TO ADR_RW;

GRANT SELECT ON ADR.INELIGIBLE_RULING TO ES_R;

GRANT INSERT, SELECT ON ADR.INELIGIBLE_RULING TO ES_RW;
ALTER TABLE ADR.INELIGIBLE_RULING_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INELIGIBLE_RULING_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.INELIGIBLE_RULING_H
(
  INELIGIBLE_RULING_H_ID  NUMBER(20)            NOT NULL,
  TRANSACTION_TYPE_CODE   VARCHAR2(1 BYTE)      NOT NULL,
  INELIGIBLE_RULING_ID    NUMBER(20)            NOT NULL,
  PERSON_ID               NUMBER(20)            NOT NULL,
  STD_INSTITUTION_ID      NUMBER(20),
  INELIGIBLE_DATE         DATE,
  INELIGIBLE_REASON_TEXT  VARCHAR2(250 BYTE),
  VARO_DECISION_TEXT      VARCHAR2(250 BYTE),
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INELIGIBLE_RULING_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.INELIGIBLE_RULING_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.INELIGIBLE_RULING_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.VARO_DECISION_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INELIGIBLE_RULING_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.INELIGBRLH_PK ON ADR.INELIGIBLE_RULING_H
(INELIGIBLE_RULING_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INELIGBRLH_PRSNID_NU_I ON ADR.INELIGIBLE_RULING_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM INELIGIBLE_RULING_H FOR ADR.INELIGIBLE_RULING_H;


ALTER TABLE ADR.INELIGIBLE_RULING_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_038
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT INELIGBRLH_PK
  PRIMARY KEY
  (INELIGIBLE_RULING_H_ID)
  USING INDEX ADR.INELIGBRLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.INELIGIBLE_RULING_H TO ADR_R;

GRANT SELECT ON ADR.INELIGIBLE_RULING_H TO ADR_RW;

GRANT SELECT ON ADR.INELIGIBLE_RULING_H TO ES_R;

GRANT INSERT, SELECT ON ADR.INELIGIBLE_RULING_H TO ES_RW;
ALTER TABLE ADR.INSURANCE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INSURANCE CASCADE CONSTRAINTS;

CREATE TABLE ADR.INSURANCE
(
  INSURANCE_ID                   NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INSURANCE_RPT_SRC_TYPE_ID      NUMBER(20),
  POLICY_HOLDER_TYPE_ID          NUMBER(20),
  SITE_UPLOAD_STATUS_TYPE_ID     NUMBER(20),
  SITE_NO_UPLOAD_REASON_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  INSURANCE_MAPPING_TYPE_CODE    VARCHAR2(5 BYTE),
  INSURANCE_COMPANY_NAME         VARCHAR2(100 BYTE),
  INSURANCE_PLAN_TYPE_ID         NUMBER(20),
  POLICY_HOLDER_SSN              VARCHAR2(20 BYTE),
  POLICY_HOLDER_NAME             VARCHAR2(50 BYTE),
  POLICY_HOLDER_BIRTH_DATE       DATE,
  GROUP_NAME                     VARCHAR2(100 BYTE),
  GROUP_NUMBER                   VARCHAR2(30 BYTE),
  POLICY_NUMBER                  VARCHAR2(30 BYTE),
  POLICY_EFFECTIVE_DATE_TXT      VARCHAR2(14 BYTE),
  POLICY_EXPIRATION_DATE_TXT     VARCHAR2(14 BYTE),
  DEDUCTIBLE_AMOUNT              NUMBER(10,2),
  PERCENTAGE_PAYABLE             NUMBER(3),
  FILING_TIME_LIMIT              VARCHAR2(80 BYTE),
  PRECRTFN_REQD_IND              VARCHAR2(1 BYTE),
  PRE_CERTIFICATION_PENALTY_IND  VARCHAR2(1 BYTE),
  NAME_ON_MEDICARE_CARD          VARCHAR2(35 BYTE),
  MEDICARE_CLAIM_NUMBER          VARCHAR2(25 BYTE),
  MEDICARE_PART_A_IND            VARCHAR2(1 BYTE),
  MEDICARE_PART_B_IND            VARCHAR2(1 BYTE),
  INSURANCE_DATA_LAST_EDIT_USER  VARCHAR2(30 BYTE),
  INSURANCE_DATA_LAST_EDIT_DATE  DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  MEDICARE_PART_A_EFF_DATE_TXT   VARCHAR2(14 BYTE),
  MEDICARE_PART_B_EFF_DATE_TXT   VARCHAR2(14 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INSURANCE IS 'The table stores data about a persons private insurance and medicare policies. Note: This is the ESR version of the INSURANCE data, this is just the insurance factor that is used for performing Enrollment / Eligibility calculations PERSON_ID Foreign key to the PERSON table. INSURANCE_RPT_SRC_TYPE_ID Foreign key to the INSURANCE_RPT_SRC_TYPE table. Indicates the source of an insurance report (for example, interview, IVM). SCRB_PATNT_RLTN_TYPE_ID Foreign key to the SCRB_PATNT_RLTN_TYPE table. Indicates the relationship of the insurance subscriber to the patient (for example, spouse, natural child). STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. SITE_UPLOAD_STATUS_TYPE_ID Foreign key to the SITE_UPLOAD_STATUS_TYPE table. Indicates whether insurance data was uploaded from a site. SITE_NO_UPLOAD_REASON_TYPE_ID Foreign key to the SITE_NO_UPLOAD_REASON_TYPE table. Indicates the reason insurance data was not uploaded from a site (for example, Benefits not Available, Policy Coverage Expired). INSURANCE_PLAN_TYPE_ID Foreign key to the INSURANCE_PLAN_TYPE table. Indicates the type of insurance plan a person has (for example, Medicaid, Tricare). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_RPT_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_HOLDER_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.SITE_UPLOAD_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.SITE_NO_UPLOAD_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_MAPPING_TYPE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_COMPANY_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_PLAN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_HOLDER_SSN IS 'BR707 Insured SSN is defined as the policyholders (insureds) social security number. This only needs to be entered if it is different than the Subscriber Id number and the insured is not the patient.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_HOLDER_NAME IS 'BR703 Name of Insured is defined as the name of the individual for which this policy was issued. If the Patients Relationship to the Insured is Patient then this name will default to the patients name.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_HOLDER_BIRTH_DATE IS 'BR705 Insured DOB is defined as the policyholders (insureds) Date of Birth.  This is not needed if the patient is the policy holder since the patients DOB is stored elsewhere.';

COMMENT ON COLUMN ADR.INSURANCE.GROUP_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.GROUP_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_EFFECTIVE_DATE_TXT IS 'BR697 Effective Date is defined as the date this policy went into effect for this patient. This can be an imprecise date and the date can be a future date. Cannot be before DOB. Cannot be After DOD.';

COMMENT ON COLUMN ADR.INSURANCE.POLICY_EXPIRATION_DATE_TXT IS 'BR699 Expiration date is defined as the date this insurance policy coverage expires for this patient. This can be an imprecise date and the date can be a future date. This date must be after the effective date.';

COMMENT ON COLUMN ADR.INSURANCE.DEDUCTIBLE_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.PERCENTAGE_PAYABLE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.FILING_TIME_LIMIT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.PRECRTFN_REQD_IND IS 'Indicates that the persons insurance company requires precertification for certain procedures.';

COMMENT ON COLUMN ADR.INSURANCE.PRE_CERTIFICATION_PENALTY_IND IS 'Indicates that there is a penalty if a person does not have medical care pre-certified by the insurance company.';

COMMENT ON COLUMN ADR.INSURANCE.NAME_ON_MEDICARE_CARD IS 'BR721 Name on Medicare card is defined as the Name exactly as it appears on the Medicare Card.';

COMMENT ON COLUMN ADR.INSURANCE.MEDICARE_CLAIM_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.MEDICARE_PART_A_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.MEDICARE_PART_B_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.INSURANCE_DATA_LAST_EDIT_USER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INSURANCE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INSURANCE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INSURANCE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INSURANCE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.INSR_STD_INSRHLDRTYPE_FK_I ON ADR.INSURANCE
(POLICY_HOLDER_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_INSRPLANTYP_FK_I ON ADR.INSURANCE
(INSURANCE_PLAN_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_INSRREP_FK_I ON ADR.INSURANCE
(INSURANCE_RPT_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_INST_FK_I ON ADR.INSURANCE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_INSURANCEMAP_FK_I ON ADR.INSURANCE
(INSURANCE_MAPPING_TYPE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_NOSTUPLDRSN_FK_I ON ADR.INSURANCE
(SITE_NO_UPLOAD_REASON_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSR_STD_STUPLDSTATUS_FK_I ON ADR.INSURANCE
(SITE_UPLOAD_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.INSURANCE_PK ON ADR.INSURANCE
(INSURANCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSURANCE_PRSN_FK_I ON ADR.INSURANCE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.INSURANCE_AUDIR_TRGR
After Update or Delete or Insert
ON ADR.INSURANCE REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
       mDML char(1);
    mc_eff_d date;
begin

    if DELETING then
        insert into INSURANCE_H
        (
        INSURANCE_H_ID,
        INSURANCE_ID,
        PERSON_ID,
        INSURANCE_RPT_SRC_TYPE_ID,
        POLICY_HOLDER_TYPE_ID,
        INSURANCE_MAPPING_TYPE_CODE,
        INSURANCE_COMPANY_NAME,
        POLICY_HOLDER_SSN,
        POLICY_HOLDER_NAME,
        POLICY_HOLDER_BIRTH_DATE,
        POLICY_NUMBER,
        POLICY_EFFECTIVE_DATE_TXT,
        POLICY_EXPIRATION_DATE_TXT,
        GROUP_NAME,
        GROUP_NUMBER,
        NAME_ON_MEDICARE_CARD,
        MEDICARE_CLAIM_NUMBER,
        MEDICARE_PART_A_IND,
        MEDICARE_PART_B_IND,
        INSURANCE_DATA_LAST_EDIT_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        PRE_CERTIFICATION_PENALTY_IND,
        RECORD_MODIFIED_COUNT,
        INSURANCE_DATA_LAST_EDIT_USER,
        STD_INSTITUTION_ID,
        SITE_UPLOAD_STATUS_TYPE_ID,
        SITE_NO_UPLOAD_REASON_TYPE_ID,
        INSURANCE_PLAN_TYPE_ID,
        PRECRTFN_REQD_IND,
        DEDUCTIBLE_AMOUNT,
        PERCENTAGE_PAYABLE,
        FILING_TIME_LIMIT,
        MEDICARE_PART_A_EFF_DATE_TXT,
        MEDICARE_PART_B_EFF_DATE_TXT,
        TRANSACTION_TYPE_CODE
          )
        values
        (
        INSURANCE_H_S.nextval,
        :old.INSURANCE_ID,
        :old.PERSON_ID,
        :old.INSURANCE_RPT_SRC_TYPE_ID,
        :old.POLICY_HOLDER_TYPE_ID,
        :old.INSURANCE_MAPPING_TYPE_CODE,
        :old.INSURANCE_COMPANY_NAME,
        :old.POLICY_HOLDER_SSN,
        :old.POLICY_HOLDER_NAME,
        :old.POLICY_HOLDER_BIRTH_DATE,
        :old.POLICY_NUMBER,
        :old.POLICY_EFFECTIVE_DATE_TXT,
        :old.POLICY_EXPIRATION_DATE_TXT,
        :old.GROUP_NAME,
        :old.GROUP_NUMBER,
        :old.NAME_ON_MEDICARE_CARD,
        :old.MEDICARE_CLAIM_NUMBER,
        :old.MEDICARE_PART_A_IND,
        :old.MEDICARE_PART_B_IND,
        :old.INSURANCE_DATA_LAST_EDIT_DATE,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.PRE_CERTIFICATION_PENALTY_IND,
        :old.RECORD_MODIFIED_COUNT,
        :old.INSURANCE_DATA_LAST_EDIT_USER,
        :old.STD_INSTITUTION_ID,
        :old.SITE_UPLOAD_STATUS_TYPE_ID,
        :old.SITE_NO_UPLOAD_REASON_TYPE_ID,
        :old.INSURANCE_PLAN_TYPE_ID,
        :old.PRECRTFN_REQD_IND,
        :old.DEDUCTIBLE_AMOUNT,
        :old.PERCENTAGE_PAYABLE,
        :old.FILING_TIME_LIMIT,
        :old.MEDICARE_PART_A_EFF_DATE_TXT,
        :old.MEDICARE_PART_B_EFF_DATE_TXT,
        'D'
        );

    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;


        insert into INSURANCE_H
        (
        INSURANCE_H_ID,
        INSURANCE_ID,
        PERSON_ID,
        INSURANCE_RPT_SRC_TYPE_ID,
        POLICY_HOLDER_TYPE_ID,
        INSURANCE_MAPPING_TYPE_CODE,
        INSURANCE_COMPANY_NAME,
        POLICY_HOLDER_SSN,
        POLICY_HOLDER_NAME,
        POLICY_HOLDER_BIRTH_DATE,
        POLICY_NUMBER,
        POLICY_EFFECTIVE_DATE_TXT,
        POLICY_EXPIRATION_DATE_TXT,
        GROUP_NAME,
        GROUP_NUMBER,
        NAME_ON_MEDICARE_CARD,
        MEDICARE_CLAIM_NUMBER,
        MEDICARE_PART_A_IND,
        MEDICARE_PART_B_IND,
        INSURANCE_DATA_LAST_EDIT_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        PRE_CERTIFICATION_PENALTY_IND,
        RECORD_MODIFIED_COUNT,
        INSURANCE_DATA_LAST_EDIT_USER,
        STD_INSTITUTION_ID,
        SITE_UPLOAD_STATUS_TYPE_ID,
        SITE_NO_UPLOAD_REASON_TYPE_ID,
        INSURANCE_PLAN_TYPE_ID,
        PRECRTFN_REQD_IND,
        DEDUCTIBLE_AMOUNT,
        PERCENTAGE_PAYABLE,
        FILING_TIME_LIMIT,
        MEDICARE_PART_A_EFF_DATE_TXT,
        MEDICARE_PART_B_EFF_DATE_TXT,
        TRANSACTION_TYPE_CODE
          )
        values
        (
        INSURANCE_H_S.nextval,
        :new.INSURANCE_ID,
        :new.PERSON_ID,
        :new.INSURANCE_RPT_SRC_TYPE_ID,
        :new.POLICY_HOLDER_TYPE_ID,
        :new.INSURANCE_MAPPING_TYPE_CODE,
        :new.INSURANCE_COMPANY_NAME,
        :new.POLICY_HOLDER_SSN,
        :new.POLICY_HOLDER_NAME,
        :new.POLICY_HOLDER_BIRTH_DATE,
        :new.POLICY_NUMBER,
        :new.POLICY_EFFECTIVE_DATE_TXT,
        :new.POLICY_EXPIRATION_DATE_TXT,
        :new.GROUP_NAME,
        :new.GROUP_NUMBER,
        :new.NAME_ON_MEDICARE_CARD,
        :new.MEDICARE_CLAIM_NUMBER,
        :new.MEDICARE_PART_A_IND,
        :new.MEDICARE_PART_B_IND,
        :new.INSURANCE_DATA_LAST_EDIT_DATE,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.PRE_CERTIFICATION_PENALTY_IND,
        :new.RECORD_MODIFIED_COUNT,
        :new.INSURANCE_DATA_LAST_EDIT_USER,
        :new.STD_INSTITUTION_ID,
        :new.SITE_UPLOAD_STATUS_TYPE_ID,
        :new.SITE_NO_UPLOAD_REASON_TYPE_ID,
        :new.INSURANCE_PLAN_TYPE_ID,
        :new.PRECRTFN_REQD_IND,
        :new.DEDUCTIBLE_AMOUNT,
        :new.PERCENTAGE_PAYABLE,
        :new.FILING_TIME_LIMIT,
        :new.MEDICARE_PART_A_EFF_DATE_TXT,
        :new.MEDICARE_PART_B_EFF_DATE_TXT,
        mDML
        );


    end if;

end  INSURANCE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM INSURANCE FOR ADR.INSURANCE;


ALTER TABLE ADR.INSURANCE ADD (
  CONSTRAINT SYS_C0066176
  CHECK ("INSURANCE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066177
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066178
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066179
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066180
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066181
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066182
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INSURANCE_PK
  PRIMARY KEY
  (INSURANCE_ID)
  USING INDEX ADR.INSURANCE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.INSURANCE ADD (
  CONSTRAINT INSURANCE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.INSURANCE TO ADREXT;

GRANT SELECT ON ADR.INSURANCE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.INSURANCE TO ADR_RW;

GRANT SELECT ON ADR.INSURANCE TO ES_R;

GRANT INSERT, SELECT ON ADR.INSURANCE TO ES_RW;
ALTER TABLE ADR.INSURANCE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.INSURANCE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.INSURANCE_H
(
  INSURANCE_H_ID                 NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  INSURANCE_ID                   NUMBER(20),
  PERSON_ID                      NUMBER(20),
  INSURANCE_RPT_SRC_TYPE_ID      NUMBER(20),
  POLICY_HOLDER_TYPE_ID          NUMBER(20),
  INSURANCE_MAPPING_TYPE_CODE    VARCHAR2(5 BYTE),
  INSURANCE_COMPANY_NAME         VARCHAR2(100 BYTE),
  POLICY_HOLDER_SSN              VARCHAR2(20 BYTE),
  POLICY_HOLDER_NAME             VARCHAR2(50 BYTE),
  POLICY_HOLDER_BIRTH_DATE       DATE,
  POLICY_NUMBER                  VARCHAR2(30 BYTE),
  POLICY_EFFECTIVE_DATE_TXT      VARCHAR2(14 BYTE),
  POLICY_EXPIRATION_DATE_TXT     VARCHAR2(14 BYTE),
  GROUP_NAME                     VARCHAR2(100 BYTE),
  GROUP_NUMBER                   VARCHAR2(30 BYTE),
  NAME_ON_MEDICARE_CARD          VARCHAR2(35 BYTE),
  MEDICARE_CLAIM_NUMBER          VARCHAR2(25 BYTE),
  MEDICARE_PART_A_IND            VARCHAR2(1 BYTE),
  MEDICARE_PART_B_IND            VARCHAR2(1 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  PRE_CERTIFICATION_PENALTY_IND  VARCHAR2(1 BYTE),
  RECORD_MODIFIED_COUNT          NUMBER(10),
  INSURANCE_DATA_LAST_EDIT_USER  VARCHAR2(30 BYTE),
  INSURANCE_DATA_LAST_EDIT_DATE  DATE,
  STD_INSTITUTION_ID             NUMBER(20),
  SITE_UPLOAD_STATUS_TYPE_ID     NUMBER(20),
  SITE_NO_UPLOAD_REASON_TYPE_ID  NUMBER(20),
  INSURANCE_PLAN_TYPE_ID         NUMBER(20),
  PRECRTFN_REQD_IND              VARCHAR2(1 BYTE),
  DEDUCTIBLE_AMOUNT              NUMBER(10,2),
  PERCENTAGE_PAYABLE             NUMBER(3),
  FILING_TIME_LIMIT              VARCHAR2(80 BYTE),
  MEDICARE_PART_A_EFF_DATE_TXT   VARCHAR2(14 BYTE),
  MEDICARE_PART_B_EFF_DATE_TXT   VARCHAR2(14 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.INSURANCE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_RPT_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_HOLDER_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_MAPPING_TYPE_CODE IS 'Hibernate mapping: used to identify whether the persons insurance is private or Medicare. ';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_COMPANY_NAME IS 'BR657 Insurance Company name is defined, as the name of the insurance company.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_HOLDER_SSN IS 'BR707 Insured SSN is defined as the policyholders (insureds) social security number. This only needs to be entered if it is different than the Subscriber Id number and the insured is not the patient.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_HOLDER_NAME IS 'BR703 Name of Insured is defined as the name of the individual for which this policy was issued. If the Patients Relationship to the Insured is Patient then this name will default to the patients name.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_HOLDER_BIRTH_DATE IS 'BR705 Insured DOB is defined as the policyholders (insureds) Date of Birth.  This is not needed if the patient is the policy holder since the patients DOB is stored elsewhere.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_EFFECTIVE_DATE_TXT IS 'BR697 Effective Date is defined as the date this policy went into effect for this patient. This can be an imprecise date and the date can be a future date. Cannot be before DOB. Cannot be After DOD.';

COMMENT ON COLUMN ADR.INSURANCE_H.POLICY_EXPIRATION_DATE_TXT IS 'BR699 Expiration date is defined as the date this insurance policy coverage expires for this patient. This can be an imprecise date and the date can be a future date. This date must be after the effective date.';

COMMENT ON COLUMN ADR.INSURANCE_H.GROUP_NAME IS 'BR671 Group Name is defined as the name that the insurance company uses to identify this plan.';

COMMENT ON COLUMN ADR.INSURANCE_H.GROUP_NUMBER IS 'BR673 Group Number is defined as the number or code which the insurance company uses to identify this plan.';

COMMENT ON COLUMN ADR.INSURANCE_H.NAME_ON_MEDICARE_CARD IS 'BR721 Name on Medicare card is defined as the Name exactly as it appears on the Medicare Card.';

COMMENT ON COLUMN ADR.INSURANCE_H.MEDICARE_CLAIM_NUMBER IS 'Identifier for a claim that was made for coverage through Medicare.';

COMMENT ON COLUMN ADR.INSURANCE_H.MEDICARE_PART_A_IND IS 'Indicates that a person is covered by Medicare Part A for inpatient hospital services, skilled nursing facilities, home health services and hospice care. ';

COMMENT ON COLUMN ADR.INSURANCE_H.MEDICARE_PART_B_IND IS 'Indicates that a person is covered by Medicare Part B for the cost of physician services, outpatient hospital services, medical equipment and supplies and other health services and supplies. ';

COMMENT ON COLUMN ADR.INSURANCE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.INSURANCE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.INSURANCE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.INSURANCE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.INSURANCE_H.PRE_CERTIFICATION_PENALTY_IND IS 'Indicates that there is a penalty if a person does not have medical care pre-certified by the insurance company.';

COMMENT ON COLUMN ADR.INSURANCE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.INSURANCE_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.INSURANCE_H.SITE_UPLOAD_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.SITE_NO_UPLOAD_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.INSURANCE_PLAN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.INSURANCE_H.PRECRTFN_REQD_IND IS 'Indicates that the persons insurance company requires precertification for certain procedures.';

COMMENT ON COLUMN ADR.INSURANCE_H.DEDUCTIBLE_AMOUNT IS 'Dollar amount for the deductible the person is responsible for as part of his or her insurance plan.';


CREATE UNIQUE INDEX ADR.INSURANCEH_PK ON ADR.INSURANCE_H
(INSURANCE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INSURANCEH_PRSNID_NU_I ON ADR.INSURANCE_H
(PERSON_ID, INSURANCE_MAPPING_TYPE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM INSURANCE_H FOR ADR.INSURANCE_H;


ALTER TABLE ADR.INSURANCE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_043
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066781
  CHECK ("INSURANCE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066782
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066783
  CHECK ("INSURANCE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066784
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066785
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066786
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066787
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066788
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066789
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT INSURANCEH_PK
  PRIMARY KEY
  (INSURANCE_H_ID)
  USING INDEX ADR.INSURANCEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.INSURANCE_H TO ADR_R;

GRANT SELECT ON ADR.INSURANCE_H TO ADR_RW;

GRANT SELECT ON ADR.INSURANCE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.INSURANCE_H TO ES_RW;
ALTER TABLE ADR.IN_PROCESS_FINANCIALS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IN_PROCESS_FINANCIALS CASCADE CONSTRAINTS;

CREATE TABLE ADR.IN_PROCESS_FINANCIALS
(
  IN_PROCESS_FINANCIAL_ID  NUMBER(20)           NOT NULL,
  PERSON_ID                NUMBER(20)           NOT NULL,
  INCOME_YEAR              NUMBER(4),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL,
  FINANCIAL_DATA           CLOB
)
LOB (FINANCIAL_DATA) STORE AS (
  TABLESPACE  M_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IN_PROCESS_FINANCIALS IS 'This is a process related table. While a financial determination is in process, all data pertaining to this process is stored in this table in a CLOB field called FINANCIAL_DATA';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.INCOME_YEAR IS 'Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.IN_PROCESS_FINANCIALS.FINANCIAL_DATA IS 'This is a CLOB column that contains all the data related to a particular person, for a given Income Year.';


CREATE UNIQUE INDEX ADR.INPROCFNCL_PK ON ADR.IN_PROCESS_FINANCIALS
(IN_PROCESS_FINANCIAL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.INPROCFNCL_PRSN_FK_I ON ADR.IN_PROCESS_FINANCIALS
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IN_PROCESS_FINANCIALS FOR ADR.IN_PROCESS_FINANCIALS;


ALTER TABLE ADR.IN_PROCESS_FINANCIALS ADD (
  CONSTRAINT INPROCFNCL_PK
  PRIMARY KEY
  (IN_PROCESS_FINANCIAL_ID)
  USING INDEX ADR.INPROCFNCL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IN_PROCESS_FINANCIALS ADD (
  CONSTRAINT INPROCFNCL_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.IN_PROCESS_FINANCIALS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IN_PROCESS_FINANCIALS TO ADR_RW;

GRANT SELECT ON ADR.IN_PROCESS_FINANCIALS TO ES_R;

GRANT INSERT, SELECT ON ADR.IN_PROCESS_FINANCIALS TO ES_RW;
ALTER TABLE ADR.IRS_TRANSMISSION_DATA
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IRS_TRANSMISSION_DATA CASCADE CONSTRAINTS;

CREATE TABLE ADR.IRS_TRANSMISSION_DATA
(
  IRS_TRANSMISSION_DATA_ID  NUMBER(20)          NOT NULL,
  IRS_TRANSMISSION_LOG_ID   NUMBER(20)          NOT NULL,
  REQUEST_DATA              CLOB,
  RESPONSE_DATA             CLOB,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL
)
LOB (REQUEST_DATA) STORE AS SECUREFILE (
  TABLESPACE  L_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  COMPRESS    MEDIUM
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
LOB (RESPONSE_DATA) STORE AS SECUREFILE (
  TABLESPACE  L_DATA_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  COMPRESS    MEDIUM
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128M
                  NEXT             128M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IRS_TRANSMISSION_DATA IS 'This table contains request and response raw data attachments sent/received in IRS transmissions.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.IRS_TRANSMISSION_DATA_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.IRS_TRANSMISSION_LOG_ID IS 'FK to ADR.IRS_TRANSMISSION_LOG.IRS_TRANSMISSION_LOG_ID';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.REQUEST_DATA IS 'Batch request transmission data file attachment submitted to the IRS';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RESPONSE_DATA IS 'Response data file attachment containing errors received from IRS acknowledgment processing';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_DATA.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE INDEX ADR.IRSTRNSDAT_IRSTRNSLG_FK_I ON ADR.IRS_TRANSMISSION_DATA
(IRS_TRANSMISSION_LOG_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.IRS_TRANSMISSION_DATA_ID_PK ON ADR.IRS_TRANSMISSION_DATA
(IRS_TRANSMISSION_DATA_ID)
LOGGING
TABLESPACE M_DATA_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IRS_TRANSMISSION_DATA FOR ADR.IRS_TRANSMISSION_DATA;


ALTER TABLE ADR.IRS_TRANSMISSION_DATA ADD (
  CONSTRAINT IRS_TRANSMISSION_DATA_ID_PK
  PRIMARY KEY
  (IRS_TRANSMISSION_DATA_ID)
  USING INDEX ADR.IRS_TRANSMISSION_DATA_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IRS_TRANSMISSION_DATA ADD (
  CONSTRAINT IRSTRNSLOG_IRSTRNSDATA_FK 
  FOREIGN KEY (IRS_TRANSMISSION_LOG_ID) 
  REFERENCES ADR.IRS_TRANSMISSION_LOG (IRS_TRANSMISSION_LOG_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.IRS_TRANSMISSION_DATA TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IRS_TRANSMISSION_DATA TO ADR_RW;

GRANT SELECT ON ADR.IRS_TRANSMISSION_DATA TO ES_R;

GRANT INSERT, SELECT ON ADR.IRS_TRANSMISSION_DATA TO ES_RW;
ALTER TABLE ADR.IRS_TRANSMISSION_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IRS_TRANSMISSION_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.IRS_TRANSMISSION_LOG
(
  IRS_TRANSMISSION_LOG_ID  NUMBER(20)           NOT NULL,
  TRANSMISSION_STATUS_ID   NUMBER(20)           NOT NULL,
  TRANSMISSION_DATE        DATE                 NOT NULL,
  RECEIPT_ID               VARCHAR2(50 BYTE),
  BATCH_TYPE_IND           VARCHAR2(10 BYTE)    NOT NULL,
  YEAR                     NUMBER(4)            NOT NULL,
  CORRECTION_IND           VARCHAR2(10 BYTE)    NOT NULL,
  ACK_DATE                 DATE,
  ACK_ID                   VARCHAR2(50 BYTE),
  ERROR_DESCRIPTION        VARCHAR2(1000 BYTE),
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IRS_TRANSMISSION_LOG IS 'This table contains transmission details for submissions of ACA Minimum Essential Coverage data from ES to the IRS.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.IRS_TRANSMISSION_LOG_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.TRANSMISSION_STATUS_ID IS 'FK to ADR.TRANSMISSION_STATUS.TRANSMISSION_STATUS_ID ';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.TRANSMISSION_DATE IS 'Date of original batch request submission';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECEIPT_ID IS 'Hash value provided by IRS to confirm receipt of the batch submission';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.BATCH_TYPE_IND IS 'Indicates submission of type Veterans or Beneficiaries';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.YEAR IS 'Calendar year for which the coverage is being reported';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.CORRECTION_IND IS 'Indicates correction type of Original, Corrected, or Replacement submission';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.ACK_DATE IS 'Date of completed IRS acknowledgment of processing';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.ACK_ID IS 'Hash value provided by IRS in acknowledgement response data to confirm processing';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.ERROR_DESCRIPTION IS 'Error string provided by IRS for cases of Rejected batch submission';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE INDEX ADR.IRSTRNSLG_TRNSSTAT_FK_I ON ADR.IRS_TRANSMISSION_LOG
(TRANSMISSION_STATUS_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.IRS_TRANSMISSION_LOG_ID_PK ON ADR.IRS_TRANSMISSION_LOG
(IRS_TRANSMISSION_LOG_ID)
LOGGING
TABLESPACE M_DATA_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ITL_RECEIPT_ID_NU_I ON ADR.IRS_TRANSMISSION_LOG
(RECEIPT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ITL_YEAR_NU_I ON ADR.IRS_TRANSMISSION_LOG
(YEAR)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IRS_TRANSMISSION_LOG FOR ADR.IRS_TRANSMISSION_LOG;


ALTER TABLE ADR.IRS_TRANSMISSION_LOG ADD (
  CONSTRAINT IRS_TRANSMISSION_LOG_ID_PK
  PRIMARY KEY
  (IRS_TRANSMISSION_LOG_ID)
  USING INDEX ADR.IRS_TRANSMISSION_LOG_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IRS_TRANSMISSION_LOG ADD (
  CONSTRAINT TRNSSTAT_IRSTRNSLOG_FK 
  FOREIGN KEY (TRANSMISSION_STATUS_ID) 
  REFERENCES ADR.TRANSMISSION_STATUS (TRANSMISSION_STATUS_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.IRS_TRANSMISSION_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IRS_TRANSMISSION_LOG TO ADR_RW;

GRANT SELECT ON ADR.IRS_TRANSMISSION_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.IRS_TRANSMISSION_LOG TO ES_RW;
ALTER TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL
(
  IRS_TRANSMISSION_LOG_DETAIL_ID  NUMBER(20),
  IRS_TRANSMISSION_LOG_ID         NUMBER(20),
  PERSON_ID                       NUMBER(20),
  IEN                             VARCHAR2(30 BYTE),
  RECORD_SEQUENCE                 NUMBER(10),
  ERROR_DESCRIPTION               VARCHAR2(1000 BYTE),
  RECORD_MODIFIED_COUNT           NUMBER(10),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   3
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL IS 'This table contains details of each person transmitted in a batch submission to IRS.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.IRS_TRANSMISSION_LOG_DETAIL_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.IRS_TRANSMISSION_LOG_ID IS 'FK to ADR.IRS_TRANSMISSION_LOG.IRS_TRANSMISSION_LOG_ID';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.PERSON_ID IS 'FK to ADR.PERSON.PERSON_ID';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.IEN IS 'Unique ID of Beneficiary person record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_SEQUENCE IS 'Sequence number 1¿n of this records position in the batch';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.ERROR_DESCRIPTION IS 'Error string provided by IRS describing reject reason for this particular record data';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IRS_TRANSMISSION_LOG_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE INDEX ADR.IRSTRNSLGDET_IEN_NU_I ON ADR.IRS_TRANSMISSION_LOG_DETAIL
(IEN)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IRSTRNSLGDET_IRSTRNSLG_FK_I ON ADR.IRS_TRANSMISSION_LOG_DETAIL
(IRS_TRANSMISSION_LOG_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IRSTRNSLGDET_PERS_FK_I ON ADR.IRS_TRANSMISSION_LOG_DETAIL
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.IRS_TRANS_LOG_DET_ID_PK ON ADR.IRS_TRANSMISSION_LOG_DETAIL
(IRS_TRANSMISSION_LOG_DETAIL_ID)
LOGGING
TABLESPACE M_DATA_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IRS_TRANSMISSION_LOG_DETAIL FOR ADR.IRS_TRANSMISSION_LOG_DETAIL;


ALTER TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL ADD (
  CONSTRAINT SYS_C00392901
  CHECK ("IRS_TRANSMISSION_LOG_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392902
  CHECK ("IRS_TRANSMISSION_LOG_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392903
  CHECK ("RECORD_SEQUENCE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392904
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392905
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392906
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392907
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00392908
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT IRS_TRANS_LOG_DET_ID_PK
  PRIMARY KEY
  (IRS_TRANSMISSION_LOG_DETAIL_ID)
  USING INDEX ADR.IRS_TRANS_LOG_DET_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IRS_TRANSMISSION_LOG_DETAIL ADD (
  CONSTRAINT IRSTRNSLOG_IRSTRNSLGDET_FK 
  FOREIGN KEY (IRS_TRANSMISSION_LOG_ID) 
  REFERENCES ADR.IRS_TRANSMISSION_LOG (IRS_TRANSMISSION_LOG_ID)
  ENABLE VALIDATE,
  CONSTRAINT PERSN_IRSTRNSLGDET_PRSN_ID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.IRS_TRANSMISSION_LOG_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IRS_TRANSMISSION_LOG_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.IRS_TRANSMISSION_LOG_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.IRS_TRANSMISSION_LOG_DETAIL TO ES_RW;
ALTER TABLE ADR.IVM_MIGRATE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IVM_MIGRATE CASCADE CONSTRAINTS;

CREATE TABLE ADR.IVM_MIGRATE
(
  IVM_MIGRATE_ID            NUMBER(20),
  PERSON_ID                 NUMBER(20),
  MIGRATE_DATE              DATE,
  INCOME_YEAR               NUMBER(4),
  LEGACY_ICN                VARCHAR2(30 BYTE),
  LOAD_FAILURE_DESCRIPTION  VARCHAR2(2000 BYTE),
  MARKED_FLAG               NUMBER(1)           DEFAULT 0,
  REMIGRATE_FLAG            NUMBER(1)           DEFAULT 0,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE       DATE,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE      DATE,
  RECORD_MODIFIED_COUNT     NUMBER(10),
  STD_MESSAGESTATUS_ID      NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IVM_MIGRATE IS 'For a brief period just after Deployment, the ADR database to be running in parallel with an EDB database that contains the IVM data. During this period, any deltas that occur in the ADR system after initial migration, will be stored in this table. Hence, this is a one-time, IVM migration related table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.IVM_MIGRATE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.INCOME_YEAR IS 'Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IVM_MIGRATE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.IVMMGRT_LGCYICN_NU_I ON ADR.IVM_MIGRATE
(LEGACY_ICN)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.IVMMGRT_PK ON ADR.IVM_MIGRATE
(IVM_MIGRATE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IVMMGRT_PRSN_FK_I ON ADR.IVM_MIGRATE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IVMMGRT_PRSN_INCYR_REMIG_NU_I ON ADR.IVM_MIGRATE
(PERSON_ID, INCOME_YEAR, REMIGRATE_FLAG)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IVMMGRT_RCDCRTDT_NU_I ON ADR.IVM_MIGRATE
(RECORD_CREATED_DATE)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IVMMGRT_STDMSGSTA_NU_I ON ADR.IVM_MIGRATE
(STD_MESSAGESTATUS_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IVM_MIGRATE FOR ADR.IVM_MIGRATE;


ALTER TABLE ADR.IVM_MIGRATE ADD (
  CONSTRAINT AVCON_1172521913_MARKE_000
  CHECK (MARKED_FLAG BETWEEN 0 AND 0 OR MARKED_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT AVCON_1172521913_REMIG_000
  CHECK (REMIGRATE_FLAG BETWEEN 0 AND 0 OR REMIGRATE_FLAG BETWEEN 1 AND 1)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065967
  CHECK ("IVM_MIGRATE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065968
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065969
  CHECK ("MARKED_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065970
  CHECK ("REMIGRATE_FLAG" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065971
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065972
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065973
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065974
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065975
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT IVMMGRT_PK
  PRIMARY KEY
  (IVM_MIGRATE_ID)
  USING INDEX ADR.IVMMGRT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IVM_MIGRATE ADD (
  CONSTRAINT IVMMGRT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.IVM_MIGRATE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IVM_MIGRATE TO ADR_RW;

GRANT SELECT ON ADR.IVM_MIGRATE TO ES_R;

GRANT INSERT, SELECT ON ADR.IVM_MIGRATE TO ES_RW;
DROP TABLE ADR.IVM_MIGRATE_ARCHIVE CASCADE CONSTRAINTS;

CREATE TABLE ADR.IVM_MIGRATE_ARCHIVE
(
  IVM_MIGRATE_ID            NUMBER(20)          NOT NULL,
  IVM_MIGRATE_ARCHIVE_ID    NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  MIGRATE_DATE              DATE,
  INCOME_YEAR               NUMBER(4),
  LEGACY_ICN                VARCHAR2(30 BYTE),
  LOAD_FAILURE_DESCRIPTION  VARCHAR2(2000 BYTE),
  MARKED_FLAG               NUMBER(1)           DEFAULT 0                     NOT NULL,
  REMIGRATE_FLAG            NUMBER(1)           DEFAULT 0                     NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  STD_MESSAGESTATUS_ID      NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_03
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION IVMMGRTARC_P001 VALUES LESS THAN (TO_DATE(' 2014-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2014-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2015-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2016-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_03
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IVM_MIGRATE_ARCHIVE IS 'For a brief period just after Deployment, the ADR database to be running in parallel with an EDB database that contains the IVM data. During this period, any deltas that occur in the ADR system after initial migration, will be stored in this table. Hence, this is a one-time, IVM migration related table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.IVM_MIGRATE_ARCHIVE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.INCOME_YEAR IS 'Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_ARCHIVE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.IVMMGRTARC_PK ON ADR.IVM_MIGRATE_ARCHIVE
(IVM_MIGRATE_ARCHIVE_ID)
  TABLESPACE L_INDEX_03
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
GLOBAL PARTITION BY HASH (IVM_MIGRATE_ARCHIVE_ID) (  
  PARTITION IVMMGRTARC_IDX_P001
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P002
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P003
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P004
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P005
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P006
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P007
    TABLESPACE L_INDEX_03,  
  PARTITION IVMMGRTARC_IDX_P008
    TABLESPACE L_INDEX_03
)
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE OR REPLACE PUBLIC SYNONYM IVM_MIGRATE_ARCHIVE FOR ADR.IVM_MIGRATE_ARCHIVE;


GRANT SELECT ON ADR.IVM_MIGRATE_ARCHIVE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IVM_MIGRATE_ARCHIVE TO ADR_RW;

GRANT SELECT ON ADR.IVM_MIGRATE_ARCHIVE TO ES_R;

GRANT INSERT, SELECT ON ADR.IVM_MIGRATE_ARCHIVE TO ES_RW;
ALTER TABLE ADR.IVM_MIGRATE_EXTRACT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IVM_MIGRATE_EXTRACT CASCADE CONSTRAINTS;

CREATE TABLE ADR.IVM_MIGRATE_EXTRACT
(
  IVM_MIGRATE_EXTRACT_ID  NUMBER(20)            NOT NULL,
  IVM_MIGRATE_TYPE_ID     NUMBER(20)            NOT NULL,
  EXTRACT_FILE_NAME       VARCHAR2(100 BYTE),
  EXTRACT_START_DATE      DATE,
  EXTRACT_END_DATE        DATE,
  EXTRACT_RECORD_COUNT    NUMBER(10),
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IVM_MIGRATE_EXTRACT IS 'For a brief period just after Deployment, the ADR database to be running in parallel with an EDB database that contains the IVM data. During this period, any deltas that occur in the ADR system after initial migration, will be stored in this table. Hence, this is a one-time, IVM migration related table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.IVM_MIGRATE_EXTRACT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.IVMMGEX_PK ON ADR.IVM_MIGRATE_EXTRACT
(IVM_MIGRATE_EXTRACT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IVMMGEX_STD_IVMMGRTYP_FK_I ON ADR.IVM_MIGRATE_EXTRACT
(IVM_MIGRATE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IVM_MIGRATE_EXTRACT FOR ADR.IVM_MIGRATE_EXTRACT;


ALTER TABLE ADR.IVM_MIGRATE_EXTRACT ADD (
  CONSTRAINT IVMMGEX_PK
  PRIMARY KEY
  (IVM_MIGRATE_EXTRACT_ID)
  USING INDEX ADR.IVMMGEX_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.IVM_MIGRATE_EXTRACT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IVM_MIGRATE_EXTRACT TO ADR_RW;

GRANT SELECT ON ADR.IVM_MIGRATE_EXTRACT TO ES_R;

GRANT INSERT, SELECT ON ADR.IVM_MIGRATE_EXTRACT TO ES_RW;
ALTER TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL
(
  IVM_MIGRATE_EXTRACT_DETAIL_ID  NUMBER(20)     NOT NULL,
  IVM_MIGRATE_EXTRACT_ID         NUMBER(20)     NOT NULL,
  INCOME_YEAR                    NUMBER(4),
  LEGACY_ICN                     VARCHAR2(30 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL IS 'For a brief period just after Deployment, the ADR database to be running in parallel with an EDB database that contains the IVM data. During this period, any deltas that occur in the ADR system after initial migration, will be stored in this table. Hence, this is a one-time, IVM migration related table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.IVM_MIGRATE_EXTRACT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.IVM_MIGRATE_EXTRACT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_EXTRACT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.IVMMGEXDTL_IVMMGEX_FK_I ON ADR.IVM_MIGRATE_EXTRACT_DETAIL
(IVM_MIGRATE_EXTRACT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.IVMMGEXDTL_PK ON ADR.IVM_MIGRATE_EXTRACT_DETAIL
(IVM_MIGRATE_EXTRACT_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IVM_MIGRATE_EXTRACT_DETAIL FOR ADR.IVM_MIGRATE_EXTRACT_DETAIL;


ALTER TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL ADD (
  CONSTRAINT IVMMGEXDTL_PK
  PRIMARY KEY
  (IVM_MIGRATE_EXTRACT_DETAIL_ID)
  USING INDEX ADR.IVMMGEXDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.IVM_MIGRATE_EXTRACT_DETAIL ADD (
  CONSTRAINT IVMMGEXDTL_IVMMGEX_FK 
  FOREIGN KEY (IVM_MIGRATE_EXTRACT_ID) 
  REFERENCES ADR.IVM_MIGRATE_EXTRACT (IVM_MIGRATE_EXTRACT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.IVM_MIGRATE_EXTRACT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IVM_MIGRATE_EXTRACT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.IVM_MIGRATE_EXTRACT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.IVM_MIGRATE_EXTRACT_DETAIL TO ES_RW;
ALTER TABLE ADR.IVM_MIGRATE_STATISTICS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.IVM_MIGRATE_STATISTICS CASCADE CONSTRAINTS;

CREATE TABLE ADR.IVM_MIGRATE_STATISTICS
(
  IVM_MIGRATE_STATISTICS_ID  NUMBER(20)         NOT NULL,
  PROCESSED_COUNT            NUMBER(10),
  LOAD_FAIL_CLEAR_COUNT      NUMBER(10),
  NO_IVM_CRITERIA_COUNT      NUMBER(10),
  NO_MATCH_COUNT             NUMBER(10),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.IVM_MIGRATE_STATISTICS IS 'For a brief period just after Deployment, the ADR database to be running in parallel with an EDB database that contains the IVM data. During this period, any deltas that occur in the ADR system after initial migration, will be stored in this table. Hence, this is a one-time, IVM migration related table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.IVM_MIGRATE_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.IVM_MIGRATE_STATISTICS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.IVMMIGSTAT_PK ON ADR.IVM_MIGRATE_STATISTICS
(IVM_MIGRATE_STATISTICS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM IVM_MIGRATE_STATISTICS FOR ADR.IVM_MIGRATE_STATISTICS;


ALTER TABLE ADR.IVM_MIGRATE_STATISTICS ADD (
  CONSTRAINT IVMMIGSTAT_PK
  PRIMARY KEY
  (IVM_MIGRATE_STATISTICS_ID)
  USING INDEX ADR.IVMMIGSTAT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.IVM_MIGRATE_STATISTICS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.IVM_MIGRATE_STATISTICS TO ADR_RW;

GRANT SELECT ON ADR.IVM_MIGRATE_STATISTICS TO ES_R;

GRANT INSERT, SELECT ON ADR.IVM_MIGRATE_STATISTICS TO ES_RW;
ALTER TABLE ADR.JOB_CONFIG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.JOB_CONFIG CASCADE CONSTRAINTS;

CREATE TABLE ADR.JOB_CONFIG
(
  JOB_CONFIG_ID            NUMBER(20)           NOT NULL,
  JOB_NAME                 VARCHAR2(80 BYTE)    NOT NULL,
  JOB_GROUP_NAME           VARCHAR2(80 BYTE)    NOT NULL,
  EMAIL_DISTRIBUTION_LIST  VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.JOB_CONFIG.JOB_CONFIG_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.JOB_CONFIG.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.JOB_CONFIG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.JOB_CONFIG.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.JOB_CONFIG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.JOB_CONFIG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.JCONFIG_PK ON ADR.JOB_CONFIG
(JOB_CONFIG_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.JOB_CONFIG_GRPNAME_I ON ADR.JOB_CONFIG
(JOB_GROUP_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.JOB_CONFIG_NAME_I ON ADR.JOB_CONFIG
(JOB_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM JOB_CONFIG FOR ADR.JOB_CONFIG;


ALTER TABLE ADR.JOB_CONFIG ADD (
  CONSTRAINT JCONFIG_PK
  PRIMARY KEY
  (JOB_CONFIG_ID)
  USING INDEX ADR.JCONFIG_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.JOB_CONFIG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.JOB_CONFIG TO ADR_RW;

GRANT SELECT ON ADR.JOB_CONFIG TO ES_R;

GRANT INSERT, SELECT ON ADR.JOB_CONFIG TO ES_RW;
ALTER TABLE ADR.JOB_RESULT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.JOB_RESULT CASCADE CONSTRAINTS;

CREATE TABLE ADR.JOB_RESULT
(
  JOB_RESULT_ID          NUMBER(20)             NOT NULL,
  JOB_NAME               VARCHAR2(80 BYTE)      NOT NULL,
  JOB_GROUP_NAME         VARCHAR2(80 BYTE)      NOT NULL,
  JOB_STATUS_CODE        VARCHAR2(50 BYTE),
  JOB_CONTEXT            VARCHAR2(250 BYTE),
  JOB_STATISTICS         VARCHAR2(4000 BYTE),
  JOB_START_DATE         DATE,
  JOB_END_DATE           DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.JOB_RESULT.JOB_RESULT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.JOB_RESULT.JOB_STATUS_CODE IS 'BR5850 Consent/Authorization Image Index Number is defined as the unique image index number associated with the veteran that is assigned by the imaging system.';

COMMENT ON COLUMN ADR.JOB_RESULT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.JOB_RESULT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.JOB_RESULT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.JOB_RESULT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.JOB_RESULT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.JOB_RESULT_GRPNAME_I ON ADR.JOB_RESULT
(JOB_GROUP_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.JOB_RESULT_NAME_I ON ADR.JOB_RESULT
(JOB_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.JOB_RESULT_STATUS_I ON ADR.JOB_RESULT
(JOB_STATUS_CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.JRESULT_PK ON ADR.JOB_RESULT
(JOB_RESULT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM JOB_RESULT FOR ADR.JOB_RESULT;


ALTER TABLE ADR.JOB_RESULT ADD (
  CONSTRAINT JRESULT_PK
  PRIMARY KEY
  (JOB_RESULT_ID)
  USING INDEX ADR.JRESULT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.JOB_RESULT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.JOB_RESULT TO ADR_RW;

GRANT SELECT ON ADR.JOB_RESULT TO ES_R;

GRANT INSERT, SELECT ON ADR.JOB_RESULT TO ES_RW;
ALTER TABLE ADR.MANUAL_OVERRIDE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MANUAL_OVERRIDE CASCADE CONSTRAINTS;

CREATE TABLE ADR.MANUAL_OVERRIDE
(
  MANUAL_OVERRIDE_ID           NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20),
  OVERRIDE_IND                 CHAR(1 BYTE)     NOT NULL,
  MANUAL_OVERRIDE_RSN_TYPE_ID  NUMBER(20),
  MANUAL_RSN_COMMENTS          VARCHAR2(250 BYTE),
  VCESTATUSTYPE_ID             NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MANUAL_OVERRIDE IS 'Table  for Manual_Override at person level.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.MANUAL_OVERRIDE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.PERSON_ID IS 'Foreign Key to Person Table ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.OVERRIDE_IND IS 'The Code will be used by the U.I to determine which Radio Button the User Saved. On the first use, the Radio Buttons will be blank and the code will be Null or Blank. 0: Manual Override Radio Button Selected; 1: Remove Override Radio Button Selected, Null: No Option Selected for Radio Buttons ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.MANUAL_OVERRIDE_RSN_TYPE_ID IS 'FK to Manual_Override_Reason table.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.MANUAL_RSN_COMMENTS IS 'Free Form text for Override Reason ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE.VCESTATUSTYPE_ID IS 'FK to VCESTATUSTYPE table ''code'' field';


CREATE UNIQUE INDEX ADR.MANUAL_OVERRIDE_PK ON ADR.MANUAL_OVERRIDE
(MANUAL_OVERRIDE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MANUOVERRIDE_VCESTATTYPE_FK_I ON ADR.MANUAL_OVERRIDE
(VCESTATUSTYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MANUAL_OVERRIDE_AUD_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.MANUAL_OVERRIDE  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
        mDML := 'D';
      INSERT
        INTO ADR.MANUAL_OVERRIDE_H (MANUAL_OVERRIDE_H_ID,
                                             TRANSACTION_TYPE_CODE,
                                             MANUAL_OVERRIDE_ID,
                                             PERSON_ID,
                                             OVERRIDE_IND,
                                             MANUAL_OVERRIDE_RSN_TYPE_ID,
                                             MANUAL_RSN_COMMENTS,
                                             VCESTATUSTYPE_ID,
                                             RECORD_CREATED_BY,
                                             RECORD_CREATED_DATE,
                                             RECORD_MODIFIED_BY,
                                             RECORD_MODIFIED_DATE,
                                             RECORD_MODIFIED_COUNT,
                                             HISTORY_CREATED_DATE)
      VALUES (MANUAL_OVERRIDE_H_S.NEXTVAL,
             mDML,
             :old.MANUAL_OVERRIDE_ID,
             :old.PERSON_ID,
             :old.OVERRIDE_IND,
             :old.MANUAL_OVERRIDE_RSN_TYPE_ID,
             :old.MANUAL_RSN_COMMENTS,
             :old.VCESTATUSTYPE_ID,
             :old.RECORD_CREATED_BY,
             :old.RECORD_CREATED_DATE,
             :old.RECORD_MODIFIED_BY,
             :old.RECORD_MODIFIED_DATE,
             :old.RECORD_MODIFIED_COUNT,
             SYSDATE);
    ELSE

      IF UPDATING THEN
        mDML := 'U';
      ELSE
        mDML := 'I';
      END IF;

     INSERT INTO ADR.MANUAL_OVERRIDE_H (MANUAL_OVERRIDE_H_ID,
                                            TRANSACTION_TYPE_CODE,
                                            MANUAL_OVERRIDE_ID,
                                            PERSON_ID,
                                            OVERRIDE_IND,
                                            MANUAL_OVERRIDE_RSN_TYPE_ID,
                                            MANUAL_RSN_COMMENTS,
                                            VCESTATUSTYPE_ID,
                                            RECORD_CREATED_BY,
                                            RECORD_CREATED_DATE,
                                            RECORD_MODIFIED_BY,
                                            RECORD_MODIFIED_DATE,
                                            RECORD_MODIFIED_COUNT,
                                            HISTORY_CREATED_DATE)
     VALUES (MANUAL_OVERRIDE_H_S.NEXTVAL,
             mDML,
             :new.MANUAL_OVERRIDE_ID,
             :new.PERSON_ID,
             :new.OVERRIDE_IND,
             :new.MANUAL_OVERRIDE_RSN_TYPE_ID,
             :new.MANUAL_RSN_COMMENTS,
             :new.VCESTATUSTYPE_ID,
             :new.RECORD_CREATED_BY,
             :new.RECORD_CREATED_DATE,
             :new.RECORD_MODIFIED_BY,
             :new.RECORD_MODIFIED_DATE,
             :new.RECORD_MODIFIED_COUNT,
             SYSDATE);
    END IF;
END MANUAL_OVERRIDE_AUD_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM MANUAL_OVERRIDE FOR ADR.MANUAL_OVERRIDE;


ALTER TABLE ADR.MANUAL_OVERRIDE ADD (
  CONSTRAINT MANUAL_OVERRIDE_PK
  PRIMARY KEY
  (MANUAL_OVERRIDE_ID)
  USING INDEX ADR.MANUAL_OVERRIDE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MANUAL_OVERRIDE ADD (
  CONSTRAINT MANUAL_OVERRIDE_RSN_TYPE_ID_FK 
  FOREIGN KEY (MANUAL_OVERRIDE_RSN_TYPE_ID) 
  REFERENCES ADR.MANUAL_OVERRIDE_RSN_TYPE (MANUAL_OVERRIDE_RSN_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT PERSON_ID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MANUAL_OVERRIDE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MANUAL_OVERRIDE TO ADR_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MANUAL_OVERRIDE TO VETCHOICE_RW;
ALTER TABLE ADR.MANUAL_OVERRIDE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MANUAL_OVERRIDE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MANUAL_OVERRIDE_H
(
  MANUAL_OVERRIDE_H_ID         NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  MANUAL_OVERRIDE_ID           NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20),
  OVERRIDE_IND                 CHAR(1 BYTE)     NOT NULL,
  MANUAL_OVERRIDE_RSN_TYPE_ID  NUMBER(20),
  MANUAL_RSN_COMMENTS          VARCHAR2(250 BYTE),
  VCESTATUSTYPE_ID             NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  HISTORY_CREATED_DATE         DATE             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MANUAL_OVERRIDE_H IS 'History table for Manual_Override at person level.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.MANUAL_OVERRIDE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.MANUAL_OVERRIDE_ID IS 'Foreign key referenced to PK of MANUAL_OVERRIDE.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.PERSON_ID IS 'Foreign Key to Person Table ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.OVERRIDE_IND IS 'The Code will be used by the U.I to determine which Radio Button the User Saved. On the first use, the Radio Buttons will be blank and the code will be Null or Blank. 0: Manual Override Radio Button Selected; 1: Remove Override Radio Button Selected, Null: No Option Selected for Radio Buttons ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.MANUAL_OVERRIDE_RSN_TYPE_ID IS 'FK to Manual_Override_Reason table. ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.MANUAL_RSN_COMMENTS IS 'Free Form text for Override Reason ';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_H.VCESTATUSTYPE_ID IS 'FK to VCESTATUSTYPE table ''code'' field';


CREATE UNIQUE INDEX ADR.MANUAL_OVERRIDE_H_PK ON ADR.MANUAL_OVERRIDE_H
(MANUAL_OVERRIDE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MANUAL_OVERRIDE_H FOR ADR.MANUAL_OVERRIDE_H;


ALTER TABLE ADR.MANUAL_OVERRIDE_H ADD (
  CONSTRAINT MANUAL_OVERRIDE_H_PK
  PRIMARY KEY
  (MANUAL_OVERRIDE_H_ID)
  USING INDEX ADR.MANUAL_OVERRIDE_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MANUAL_OVERRIDE_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MANUAL_OVERRIDE_H TO ADR_RW;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MANUAL_OVERRIDE_H TO VETCHOICE_RW;
ALTER TABLE ADR.MANUAL_OVERRIDE_RSN_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MANUAL_OVERRIDE_RSN_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.MANUAL_OVERRIDE_RSN_TYPE
(
  MANUAL_OVERRIDE_RSN_TYPE_ID  NUMBER(20)       NOT NULL,
  CODE                         VARCHAR2(2 BYTE) NOT NULL,
  NAME                         VARCHAR2(50 BYTE) NOT NULL,
  DESCRIPTION                  VARCHAR2(50 BYTE),
  ISACTIVE                     CHAR(1 BYTE)     NOT NULL,
  INACTIVE_DATE                DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MANUAL_OVERRIDE_RSN_TYPE IS 'lookup table for Manual_Override_Reason.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.MANUAL_OVERRIDE_RSN_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.CODE IS '(CODE:NAME)0: Administrative Decision ES record; 1:Error; 2:Mileage; 3 Legislative Change';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.NAME IS 'Lookup code long name value.';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.DESCRIPTION IS 'Lookup code description';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.ISACTIVE IS 'FLAG TO SHOW ACTIVE/INACTIVE';

COMMENT ON COLUMN ADR.MANUAL_OVERRIDE_RSN_TYPE.INACTIVE_DATE IS 'Date field to indicate when the list is inactive';


CREATE UNIQUE INDEX ADR.MANUAL_OVERRIDE_RSN_TYPE_NM_UK ON ADR.MANUAL_OVERRIDE_RSN_TYPE
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MANUAL_OVERRIDE_RSN_TYPE_PK ON ADR.MANUAL_OVERRIDE_RSN_TYPE
(MANUAL_OVERRIDE_RSN_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MANUAL_OVERRIDE_RSN_TYPE_UK ON ADR.MANUAL_OVERRIDE_RSN_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MANUAL_OVERRIDE_RSN_TYPE FOR ADR.MANUAL_OVERRIDE_RSN_TYPE;


ALTER TABLE ADR.MANUAL_OVERRIDE_RSN_TYPE ADD (
  CONSTRAINT MANUAL_OVERRIDE_RSN_TYPE_PK
  PRIMARY KEY
  (MANUAL_OVERRIDE_RSN_TYPE_ID)
  USING INDEX ADR.MANUAL_OVERRIDE_RSN_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MANUAL_OVERRIDE_RSN_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MANUAL_OVERRIDE_RSN_TYPE TO ADR_RW;

GRANT SELECT ON ADR.MANUAL_OVERRIDE_RSN_TYPE TO EEUSR WITH GRANT OPTION;
ALTER TABLE ADR.MEC_PERIOD_LAST_HIST_NEW
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEC_PERIOD_LAST_HIST_NEW CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEC_PERIOD_LAST_HIST_NEW
(
  PERSON_ID                NUMBER(20),
  LAST_ENROLL_DETERM_H_ID  NUMBER(20),
  STATUS                   CHAR(1 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.PERSON_ID_MAX_PK ON ADR.MEC_PERIOD_LAST_HIST_NEW
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MEC_PERIOD_LAST_HIST FOR ADR.MEC_PERIOD_LAST_HIST_NEW;


CREATE OR REPLACE PUBLIC SYNONYM MEC_PERIOD_LAST_HIST_NEW FOR ADR.MEC_PERIOD_LAST_HIST_NEW;


ALTER TABLE ADR.MEC_PERIOD_LAST_HIST_NEW ADD (
  CONSTRAINT PERSON_ID_MAX_PK
  PRIMARY KEY
  (PERSON_ID)
  USING INDEX ADR.PERSON_ID_MAX_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MEC_PERIOD_LAST_HIST_NEW TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MEC_PERIOD_LAST_HIST_NEW TO ADR_RW;

GRANT SELECT ON ADR.MEC_PERIOD_LAST_HIST_NEW TO ES_R;

GRANT INSERT, SELECT ON ADR.MEC_PERIOD_LAST_HIST_NEW TO ES_RW;
ALTER TABLE ADR.MEC_PERIOD_NEW
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEC_PERIOD_NEW CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEC_PERIOD_NEW
(
  MEC_PERIOD_ID          NUMBER,
  PERSON_ID              NUMBER(20)             NOT NULL,
  MEC_START_DATE         DATE,
  MEC_END_DATE           DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.MEC_PERIOD_ID_PK ON ADR.MEC_PERIOD_NEW
(MEC_PERIOD_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MEC_PERIOD_N_END_DATE_NU_I ON ADR.MEC_PERIOD_NEW
(MEC_END_DATE)
NOLOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MEC_PERIOD_N_PERSON_FK_I ON ADR.MEC_PERIOD_NEW
(PERSON_ID)
NOLOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MEC_PERIOD FOR ADR.MEC_PERIOD_NEW;


CREATE OR REPLACE PUBLIC SYNONYM MEC_PERIOD_NEW FOR ADR.MEC_PERIOD_NEW;


ALTER TABLE ADR.MEC_PERIOD_NEW ADD (
  CONSTRAINT MEC_PERIOD_ID_PK
  PRIMARY KEY
  (MEC_PERIOD_ID)
  USING INDEX ADR.MEC_PERIOD_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MEC_PERIOD_NEW ADD (
  CONSTRAINT MEC_PERIOD_NEW_PERSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MEC_PERIOD_NEW TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.MEC_PERIOD_NEW TO ADR_RW;

GRANT SELECT ON ADR.MEC_PERIOD_NEW TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.MEC_PERIOD_NEW TO ES_R;

GRANT INSERT, SELECT ON ADR.MEC_PERIOD_NEW TO ES_RW;
ALTER TABLE ADR.MEDAL_OF_HONOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEDAL_OF_HONOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEDAL_OF_HONOR
(
  MEDAL_OF_HONOR_ID          NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  STD_DECORATIONSTATUS_ID    NUMBER(20),
  STD_DECORATIONDOCUMENT_ID  NUMBER(20),
  STD_MHDATASOURCE_ID        NUMBER(20),
  MH_INDICATOR               VARCHAR2(1 BYTE),
  MH_STATUS_UPDATE_DATE      DATE,
  DOCUMENT_RECEIPT_DATE      DATE,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(18)         NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.MEDAL_OF_HONOR_ID IS 'The primary key, generated by the database system in numeric form. Uniquely identifies a record in this table.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.MH_INDICATOR IS 'Indicates if person has a Medal Of Honor';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.MH_STATUS_UPDATE_DATE IS 'Last Edit Date';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.DOCUMENT_RECEIPT_DATE IS 'Date received';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.MOH_PERSON_FK_I ON ADR.MEDAL_OF_HONOR
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MOH_PK ON ADR.MEDAL_OF_HONOR
(MEDAL_OF_HONOR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MOH_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.MEDAL_OF_HONOR
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into MEDAL_OF_HONOR_H
        (
        MEDAL_OF_HONOR_H_ID,
        MEDAL_OF_HONOR_ID,
        PERSON_ID,
        MH_INDICATOR,
        MH_STATUS_UPDATE_DATE,
        STD_DECORATIONSTATUS_ID,
        DOCUMENT_RECEIPT_DATE,
        STD_DECORATIONDOCUMENT_ID,
        STD_MHDATASOURCE_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        MEDAL_OF_HONOR_H_S.nextval,
        :old.MEDAL_OF_HONOR_ID,
        :old.PERSON_ID,
        :old.MH_INDICATOR,
        :old.MH_STATUS_UPDATE_DATE,
        :old.STD_DECORATIONSTATUS_ID,
        :old.DOCUMENT_RECEIPT_DATE,
        :old.STD_DECORATIONDOCUMENT_ID,
        :old.STD_MHDATASOURCE_ID,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into MEDAL_OF_HONOR_H
        (
        MEDAL_OF_HONOR_H_ID,
        MEDAL_OF_HONOR_ID,
        PERSON_ID,
        MH_INDICATOR,
        MH_STATUS_UPDATE_DATE,
        STD_DECORATIONSTATUS_ID,
        DOCUMENT_RECEIPT_DATE,
        STD_DECORATIONDOCUMENT_ID,
        STD_MHDATASOURCE_ID,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        MEDAL_OF_HONOR_H_S.nextval,
        :new.MEDAL_OF_HONOR_ID,
        :new.PERSON_ID,
        :new.MH_INDICATOR,
        :new.MH_STATUS_UPDATE_DATE,
        :new.STD_DECORATIONSTATUS_ID,
        :new.DOCUMENT_RECEIPT_DATE,
        :new.STD_DECORATIONDOCUMENT_ID,
        :new.STD_MHDATASOURCE_ID,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end MOH_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM MEDAL_OF_HONOR FOR ADR.MEDAL_OF_HONOR;


ALTER TABLE ADR.MEDAL_OF_HONOR ADD (
  CONSTRAINT MOH_PK
  PRIMARY KEY
  (MEDAL_OF_HONOR_ID)
  USING INDEX ADR.MOH_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MEDAL_OF_HONOR ADD (
  CONSTRAINT RELATIONSHIP25 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MEDAL_OF_HONOR TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.MEDAL_OF_HONOR TO ADR_RW;

GRANT SELECT ON ADR.MEDAL_OF_HONOR TO ES_R;

GRANT INSERT, SELECT ON ADR.MEDAL_OF_HONOR TO ES_RW;
ALTER TABLE ADR.MEDAL_OF_HONOR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEDAL_OF_HONOR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEDAL_OF_HONOR_H
(
  MEDAL_OF_HONOR_H_ID        NUMBER(20)         NOT NULL,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)   NOT NULL,
  MEDAL_OF_HONOR_ID          NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20),
  MH_INDICATOR               VARCHAR2(1 BYTE),
  MH_STATUS_UPDATE_DATE      DATE,
  STD_DECORATIONSTATUS_ID    NUMBER(20),
  DOCUMENT_RECEIPT_DATE      DATE,
  STD_DECORATIONDOCUMENT_ID  NUMBER(20),
  STD_MHDATASOURCE_ID        NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(18)         NOT NULL,
  HISTORY_CREATED_DATE       DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.MEDAL_OF_HONOR_ID IS 'Unique identifier for a Medal  of Honor';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.PERSON_ID IS 'Person Identifier';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.MH_INDICATOR IS 'Indicates if person has MH';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.MH_STATUS_UPDATE_DATE IS 'Last Edit Date';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.STD_DECORATIONSTATUS_ID IS 'Use existing SDS lookup';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.DOCUMENT_RECEIPT_DATE IS 'Date received';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.STD_DECORATIONDOCUMENT_ID IS 'Use existing SDS lookup';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.STD_MHDATASOURCE_ID IS 'New lookup in SDS';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.MEDAL_OF_HONOR_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.MOH_H_PK ON ADR.MEDAL_OF_HONOR_H
(MEDAL_OF_HONOR_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MEDAL_OF_HONOR_H FOR ADR.MEDAL_OF_HONOR_H;


ALTER TABLE ADR.MEDAL_OF_HONOR_H ADD (
  CONSTRAINT AVCON_1252960469_TRANS_002
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT MOH_H_PK
  PRIMARY KEY
  (MEDAL_OF_HONOR_H_ID)
  USING INDEX ADR.MOH_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MEDAL_OF_HONOR_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.MEDAL_OF_HONOR_H TO ADR_RW;

GRANT SELECT ON ADR.MEDAL_OF_HONOR_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MEDAL_OF_HONOR_H TO ES_RW;
ALTER TABLE ADR.MEDICAID_FACTOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEDICAID_FACTOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEDICAID_FACTOR
(
  MEDICAID_FACTOR_ID        NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  MEDICAID_ELIGIBILITY_IND  VARCHAR2(1 BYTE)    NOT NULL,
  MEDICAID_LAST_EDIT_DATE   DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MEDICAID_FACTOR IS 'The table stores data about whether a person is eligible for Medicaid. PERSON_ID Foreign key to the PERSON table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.MEDICAID_FACTOR_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.MEDICAID_ELIGIBILITY_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.MEDICAID_LAST_EDIT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.MEDICAID_PK ON ADR.MEDICAID_FACTOR
(MEDICAID_FACTOR_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MEDICAID_PRSN_FK_I ON ADR.MEDICAID_FACTOR
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MEDICAID_FACTOR_AUDIR_TRGR
after Update or Delete or Insert
on ADR.MEDICAID_FACTOR
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into MEDICAID_FACTOR_H
		(
		MEDICAID_FACTOR_H_ID,
		MEDICAID_FACTOR_ID,
		PERSON_ID,
		MEDICAID_ELIGIBILITY_IND,
		MEDICAID_LAST_EDIT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MEDICAID_FACTOR_H_S.nextval,
		:old.MEDICAID_FACTOR_ID,
		:old.PERSON_ID,
		:old.MEDICAID_ELIGIBILITY_IND,
		:old.MEDICAID_LAST_EDIT_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into MEDICAID_FACTOR_H
		(
		MEDICAID_FACTOR_H_ID,
		MEDICAID_FACTOR_ID,
		PERSON_ID,
		MEDICAID_ELIGIBILITY_IND,
		MEDICAID_LAST_EDIT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MEDICAID_FACTOR_H_S.nextval,
		:new.MEDICAID_FACTOR_ID,
		:new.PERSON_ID,
		:new.MEDICAID_ELIGIBILITY_IND,
		:new.MEDICAID_LAST_EDIT_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  MEDICAID_FACTOR_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM MEDICAID_FACTOR FOR ADR.MEDICAID_FACTOR;


ALTER TABLE ADR.MEDICAID_FACTOR ADD (
  CONSTRAINT MEDICAID_PK
  PRIMARY KEY
  (MEDICAID_FACTOR_ID)
  USING INDEX ADR.MEDICAID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MEDICAID_FACTOR ADD (
  CONSTRAINT MEDICAID_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MEDICAID_FACTOR TO ADREXT;

GRANT SELECT ON ADR.MEDICAID_FACTOR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MEDICAID_FACTOR TO ADR_RW;

GRANT SELECT ON ADR.MEDICAID_FACTOR TO ES_R;

GRANT INSERT, SELECT ON ADR.MEDICAID_FACTOR TO ES_RW;
ALTER TABLE ADR.MEDICAID_FACTOR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MEDICAID_FACTOR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MEDICAID_FACTOR_H
(
  MEDICAID_FACTOR_H_ID      NUMBER(20),
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE),
  MEDICAID_FACTOR_ID        NUMBER(20),
  PERSON_ID                 NUMBER(20),
  MEDICAID_ELIGIBILITY_IND  VARCHAR2(1 BYTE),
  MEDICAID_LAST_EDIT_DATE   DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE       DATE,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE      DATE,
  RECORD_MODIFIED_COUNT     NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MEDICAID_FACTOR_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.MEDICAID_FACTOR_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.MEDICAID_FACTOR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.MEDICAID_ELIGIBILITY_IND IS 'BR709 Eligible for Medicaid Indicator is defined as a code to indicate if this patient is eligible to receive Medicaid coverage.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.MEDICAID_LAST_EDIT_DATE IS 'BR7665 Date Medicaid last asked is defined as the date that the Eligible for Medicaid was last asked.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MEDICAID_FACTOR_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.MEDICAIDH_PK ON ADR.MEDICAID_FACTOR_H
(MEDICAID_FACTOR_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MEDICAIDH_PRSNID_NU_I ON ADR.MEDICAID_FACTOR_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MEDICAID_FACTOR_H FOR ADR.MEDICAID_FACTOR_H;


ALTER TABLE ADR.MEDICAID_FACTOR_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_059
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067141
  CHECK ("MEDICAID_FACTOR_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067142
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067143
  CHECK ("MEDICAID_FACTOR_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067144
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067145
  CHECK ("MEDICAID_ELIGIBILITY_IND" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067146
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067147
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067148
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067149
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067150
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MEDICAIDH_PK
  PRIMARY KEY
  (MEDICAID_FACTOR_H_ID)
  USING INDEX ADR.MEDICAIDH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MEDICAID_FACTOR_H TO ADR_R;

GRANT SELECT ON ADR.MEDICAID_FACTOR_H TO ADR_RW;

GRANT SELECT ON ADR.MEDICAID_FACTOR_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MEDICAID_FACTOR_H TO ES_RW;
ALTER TABLE ADR.MILITARY_FUTURE_DISCHARGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_FUTURE_DISCHARGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_FUTURE_DISCHARGE
(
  MILITARY_FUTURE_DISCHARGE_ID  NUMBER(20)      NOT NULL,
  FUTURE_DISCHARGE_DATE         VARCHAR2(14 BYTE) NOT NULL,
  MILITARY_SERVICE_ID           NUMBER(20)      NOT NULL,
  FUTURE_DISCHARGE_SRC_TYPE_ID  NUMBER(20),
  SERVICE_BRANCH_TYPE_ID        NUMBER(20)      NOT NULL,
  SERVICE_ENTRY_DATE            VARCHAR2(14 BYTE) NOT NULL,
  OTHER_EXPLANATION             VARCHAR2(255 BYTE),
  SITE_OF_CHANGE                VARCHAR2(50 BYTE),
  SOURCE_ID                     NUMBER(20),
  MILITARY_SERVICE_NUMBER       VARCHAR2(40 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_FUTURE_DISCHARGE IS 'New table to hold future discharge date.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.MILITARY_FUTURE_DISCHARGE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.FUTURE_DISCHARGE_DATE IS 'Hold future discharge date¿in¿this table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.FUTURE_DISCHARGE_SRC_TYPE_ID IS 'The¿foreign to ¿key FUTURE_DISCHARGE_SRC_TYPE table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.SERVICE_BRANCH_TYPE_ID IS 'The¿foreign to ¿key STD_SERVICEBRANCH table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.OTHER_EXPLANATION IS 'Free text field populated when source of information is Other';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.SITE_OF_CHANGE IS 'Hold ESR site number or station id and station name';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.SOURCE_ID IS 'Hold system job name or user_id';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.MILITARY_SERVICE_NUMBER IS 'The¿foreign key to FUTURE_DISCHARGE_SRC_TYPE table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.FUTURE_DISCHARGE_DATE_NU_I ON ADR.MILITARY_FUTURE_DISCHARGE
(FUTURE_DISCHARGE_DATE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MILITARY_FUTURE_DIS_PK ON ADR.MILITARY_FUTURE_DISCHARGE
(MILITARY_FUTURE_DISCHARGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MILITARY_FUT_DIS_AUDIT_TRGR
    AFTER UPDATE OR DELETE OR INSERT OF FUTURE_DISCHARGE_DATE
    ON ADR.MILITARY_FUTURE_DISCHARGE
    FOR EACH ROW
DECLARE
    mDML   CHAR (1) := 'D';
BEGIN
    IF DELETING
    THEN
        INSERT
          INTO ADR.MILITARY_FUTURE_DISCHARGE_H (MILITARY_FUTURE_DISCHARGE_H_ID,
                                                MILITARY_FUTURE_DISCHARGE_ID,
                                                FUTURE_DISCHARGE_DATE,
                                                FUTURE_DISCHARGE_SRC_TYPE_ID,
                                                OTHER_EXPLANATION,
                                                SERVICE_BRANCH_TYPE_ID,
                                                SERVICE_ENTRY_DATE,
                                                SITE_OF_CHANGE,
                                                SOURCE_ID,
                                                MILITARY_SERVICE_NUMBER,
                                                TRANSACTION_TYPE_CODE,
                                                RECORD_CREATED_BY,
                                                RECORD_CREATED_DATE,
                                                RECORD_MODIFIED_BY,
                                                RECORD_MODIFIED_DATE,
                                                MILITARY_SERVICE_ID,
                                                RECORD_MODIFIED_COUNT)
        VALUES (ADR.MILITARY_FUTURE_DIS_H_S.NEXTVAL,
                :old.MILITARY_FUTURE_DISCHARGE_ID,
                :old.FUTURE_DISCHARGE_DATE,
                :old.FUTURE_DISCHARGE_SRC_TYPE_ID,
                :old.OTHER_EXPLANATION,
                :old.SERVICE_BRANCH_TYPE_ID,
                :old.SERVICE_ENTRY_DATE,
                :old.SITE_OF_CHANGE,
                :old.SOURCE_ID,
                :old.MILITARY_SERVICE_NUMBER,
                mDML,
                :old.RECORD_CREATED_BY,
                :old.RECORD_CREATED_DATE,
                :old.RECORD_MODIFIED_BY,
                :old.RECORD_MODIFIED_DATE,
                :old.MILITARY_SERVICE_ID,
                :old.RECORD_MODIFIED_COUNT);
    ELSE
        IF UPDATING
        THEN
            mDML := 'U';
        ELSE
            mDML := 'I';
        END IF;

        INSERT
          INTO ADR.MILITARY_FUTURE_DISCHARGE_H (MILITARY_FUTURE_DISCHARGE_H_ID,
                                                MILITARY_FUTURE_DISCHARGE_ID,
                                                FUTURE_DISCHARGE_DATE,
                                                FUTURE_DISCHARGE_SRC_TYPE_ID,
                                                OTHER_EXPLANATION,
                                                SERVICE_BRANCH_TYPE_ID,
                                                SERVICE_ENTRY_DATE,
                                                SITE_OF_CHANGE,
                                                SOURCE_ID,
                                                MILITARY_SERVICE_NUMBER,
                                                TRANSACTION_TYPE_CODE,
                                                RECORD_CREATED_BY,
                                                RECORD_CREATED_DATE,
                                                RECORD_MODIFIED_BY,
                                                RECORD_MODIFIED_DATE,
                                                MILITARY_SERVICE_ID,
                                                RECORD_MODIFIED_COUNT)
        VALUES (ADR.MILITARY_FUTURE_DIS_H_S.NEXTVAL,
                :new.MILITARY_FUTURE_DISCHARGE_ID,
                :new.FUTURE_DISCHARGE_DATE,
                :new.FUTURE_DISCHARGE_SRC_TYPE_ID,
                :new.OTHER_EXPLANATION,
                :new.SERVICE_BRANCH_TYPE_ID,
                :new.SERVICE_ENTRY_DATE,
                :new.SITE_OF_CHANGE,
                :new.SOURCE_ID,
                :new.MILITARY_SERVICE_NUMBER,
                mDML,
                :new.RECORD_CREATED_BY,
                :new.RECORD_CREATED_DATE,
                :new.RECORD_MODIFIED_BY,
                :new.RECORD_MODIFIED_DATE,
                :new.MILITARY_SERVICE_ID,
                :new.RECORD_MODIFIED_COUNT + 1);
    END IF;
END MILITARY_FUT_DIS_AUDIT_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM MILITARY_FUTURE_DISCHARGE FOR ADR.MILITARY_FUTURE_DISCHARGE;


ALTER TABLE ADR.MILITARY_FUTURE_DISCHARGE ADD (
  CONSTRAINT MILITARY_FUTURE_DIS_PK
  PRIMARY KEY
  (MILITARY_FUTURE_DISCHARGE_ID)
  USING INDEX ADR.MILITARY_FUTURE_DIS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MILITARY_FUTURE_DISCHARGE ADD (
  CONSTRAINT FUTURE_DIS_ID_FK 
  FOREIGN KEY (FUTURE_DISCHARGE_SRC_TYPE_ID) 
  REFERENCES ADR.FUTURE_DISCHARGE_SRC_TYPE (FUTURE_DISCHARGE_SRC_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT MILITARY_SERVICE_ID_FK 
  FOREIGN KEY (MILITARY_SERVICE_ID) 
  REFERENCES ADR.MILITARY_SERVICE (MILITARY_SERVICE_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MILITARY_FUTURE_DISCHARGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MILITARY_FUTURE_DISCHARGE TO ADR_RW;
ALTER TABLE ADR.MILITARY_FUTURE_DISCHARGE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_FUTURE_DISCHARGE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_FUTURE_DISCHARGE_H
(
  MILITARY_FUTURE_DISCHARGE_H_ID  NUMBER(20)    NOT NULL,
  MILITARY_FUTURE_DISCHARGE_ID    NUMBER(20)    NOT NULL,
  FUTURE_DISCHARGE_DATE           VARCHAR2(14 BYTE),
  FUTURE_DISCHARGE_SRC_TYPE_ID    NUMBER(20),
  OTHER_EXPLANATION               VARCHAR2(255 BYTE),
  SERVICE_BRANCH_TYPE_ID          NUMBER(20),
  SERVICE_ENTRY_DATE              VARCHAR2(14 BYTE),
  SITE_OF_CHANGE                  VARCHAR2(50 BYTE),
  SOURCE_ID                       NUMBER(20),
  MILITARY_SERVICE_NUMBER         VARCHAR2(40 BYTE),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  MILITARY_SERVICE_ID             NUMBER(20)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_FUTURE_DISCHARGE_H IS 'History Table of New table to hold future discharge date.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.MILITARY_FUTURE_DISCHARGE_H_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.MILITARY_FUTURE_DISCHARGE_ID IS 'The¿foreign key to MILITARY_FUTURE_DISCHARGE table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.FUTURE_DISCHARGE_DATE IS 'Hold future discharge date¿in¿this table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.FUTURE_DISCHARGE_SRC_TYPE_ID IS 'The¿foreign to ¿key FUTURE_DISCHARGE_SRC_TYPE table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.OTHER_EXPLANATION IS 'Free text field populated when source of information is Other';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.SERVICE_BRANCH_TYPE_ID IS 'The¿foreign to ¿key STD_SERVICEBRANCH table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.SITE_OF_CHANGE IS 'Hold ESR site number or station id and station name';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.SOURCE_ID IS 'Hold system job name or user_id';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.MILITARY_SERVICE_NUMBER IS 'The¿foreign key to FUTURE_DISCHARGE_SRC_TYPE table.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.TRANSACTION_TYPE_CODE IS 'Transaction type like I,U or D.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_FUTURE_DISCHARGE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.MILITARY_FUTURE_DIS_H_PK ON ADR.MILITARY_FUTURE_DISCHARGE_H
(MILITARY_FUTURE_DISCHARGE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MILITARY_FUTURE_DISCHARGE_H FOR ADR.MILITARY_FUTURE_DISCHARGE_H;


ALTER TABLE ADR.MILITARY_FUTURE_DISCHARGE_H ADD (
  CONSTRAINT MILITARY_FUTURE_DIS_H_PK
  PRIMARY KEY
  (MILITARY_FUTURE_DISCHARGE_H_ID)
  USING INDEX ADR.MILITARY_FUTURE_DIS_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MILITARY_FUTURE_DISCHARGE_H TO ADR_R;

GRANT INSERT, SELECT ON ADR.MILITARY_FUTURE_DISCHARGE_H TO ADR_RW;
ALTER TABLE ADR.MILITARY_SERVICE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_SERVICE CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_SERVICE
(
  MILITARY_SERVICE_ID       NUMBER(20),
  PERSON_ID                 NUMBER(20),
  STD_PERIODOFSERVICE_ID    NUMBER(20),
  FILIPINO_VET_DOC_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  RECORD_CREATED_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE      DATE,
  RECORD_MODIFIED_COUNT     NUMBER(10),
  ACTIVE_DUTY_IND           VARCHAR2(1 BYTE),
  ACTIVE_DUTY_SRC           VARCHAR2(10 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_SERVICE IS 'The table stores information about a persons military service. Individuation episodes of service are stored in the child MILITARY_SERVICE _EPISODE table. PERSON_ID Foreign key to the PERSON table. FILIPINO_VET_DOC_TYPE_ID Foreign key to the FILIPINO_VET_DOC_TYPE table. Indicates the documentation used to prove identity as a Filipino Veteran of the US Armed Services (defined as one whose branch of service is F. Commonwealth, F.Guerilla, or F.Scouts New) Examples of these documents are: US Passport or US Birth Certificate. STD_PERIODOFSERVICE_ID Foreign key to the STD_PERIODOFSERVICE table. Indicates the period in which the person performed his or her military service (for example, World War 1, World War II). STD_INSTITUTION_ID Foreign key to the STD_INSTITUTION table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.MILITARY_SERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.STD_PERIODOFSERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.FILIPINO_VET_DOC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.ACTIVE_DUTY_IND IS ' indicate whether vet is in active duty. It will set by EMIS or ES 0 for not active; 1 for active';

COMMENT ON COLUMN ADR.MILITARY_SERVICE.ACTIVE_DUTY_SRC IS ' source for Active duty Indicator 0 for not active; 1 for active';


CREATE UNIQUE INDEX ADR.MILSRVC_PK ON ADR.MILITARY_SERVICE
(MILITARY_SERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVC_PRSN_FK_I ON ADR.MILITARY_SERVICE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVC_STDINST_FK_I ON ADR.MILITARY_SERVICE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVC_STDPERSRVC_FK_I ON ADR.MILITARY_SERVICE
(STD_PERIODOFSERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVC_STD_FLPNOVTRN_FK_I ON ADR.MILITARY_SERVICE
(FILIPINO_VET_DOC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MILITARY_SERV_AUDIR_TRGR
AFTER INSERT OR UPDATE OR DELETE ON ADR.MILITARY_SERVICE FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into MILITARY_SERVICE_H
		(
		MILITARY_SERVICE_H_ID,
		MILITARY_SERVICE_ID,
		PERSON_ID,
		STD_PERIODOFSERVICE_ID,
		FILIPINO_VET_DOC_TYPE_ID,
		STD_INSTITUTION_ID,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE,
            ACTIVE_DUTY_IND,
            ACTIVE_DUTY_SRC
      	)
		values
		(
		MILITARY_SERVICE_H_S.nextval,
		:old.MILITARY_SERVICE_ID,
		:old.PERSON_ID,
		:old.STD_PERIODOFSERVICE_ID,
		:old.FILIPINO_VET_DOC_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D',
            :old.ACTIVE_DUTY_IND,
            :old.ACTIVE_DUTY_SRC
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into MILITARY_SERVICE_H
		(
		MILITARY_SERVICE_H_ID,
		MILITARY_SERVICE_ID,
		PERSON_ID,
		STD_PERIODOFSERVICE_ID,
		FILIPINO_VET_DOC_TYPE_ID,
		STD_INSTITUTION_ID,
		RECORD_CREATED_DATE,
		RECORD_CREATED_BY,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE,
            ACTIVE_DUTY_IND,
            ACTIVE_DUTY_SRC
      	)
		values
		(
		MILITARY_SERVICE_H_S.nextval,
		:new.MILITARY_SERVICE_ID,
		:new.PERSON_ID,
		:new.STD_PERIODOFSERVICE_ID,
		:new.FILIPINO_VET_DOC_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		 mDML,
            :new.ACTIVE_DUTY_IND,
            :new.ACTIVE_DUTY_SRC
		);
	end if;

end  MILITARY_SERV_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM MILITARY_SERVICE FOR ADR.MILITARY_SERVICE;


ALTER TABLE ADR.MILITARY_SERVICE ADD (
  CONSTRAINT SYS_C0067202
  CHECK ("MILITARY_SERVICE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067203
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067204
  CHECK ("STD_INSTITUTION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067205
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067206
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067207
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067208
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067209
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MILSRVC_PK
  PRIMARY KEY
  (MILITARY_SERVICE_ID)
  USING INDEX ADR.MILSRVC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MILITARY_SERVICE ADD (
  CONSTRAINT MILSRVC_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MILITARY_SERVICE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MILITARY_SERVICE TO ADR_RW;

GRANT SELECT ON ADR.MILITARY_SERVICE TO ES_R;

GRANT INSERT, SELECT ON ADR.MILITARY_SERVICE TO ES_RW;
ALTER TABLE ADR.MILITARY_SERVICE_EPISODE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_SERVICE_EPISODE CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_SERVICE_EPISODE
(
  MILITARY_SERVICE_EPISODE_ID  NUMBER(20),
  MILITARY_SERVICE_ID          NUMBER(20),
  SERVICE_BRANCH_TYPE_ID       NUMBER(20),
  SERVICE_DISCHARGE_TYPE_ID    NUMBER(20),
  SERVICECOMPONENTTYPE_ID      NUMBER(20),
  MILITARY_SERVICE_NUMBER      VARCHAR2(20 BYTE),
  SERVICE_ENTRY_DATE_TXT       VARCHAR2(14 BYTE),
  SERVICE_SEPARATION_DATE_TXT  VARCHAR2(14 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_SERVICE_EPISODE IS 'The table stores details about a persons individual episodes of service, including entry and separation dates, and discharge type. This is a child table of MILITARY_SERVICE table. MILITARY_SERVICE_ID Foreign key to the MILITARY_SERVICE table. SERVICE_BRANCH_TYPE_ID Foreign key to the SERVICE_BRANCH_TYPE table. Indicates the branch of the armed forces associated with the service episode (for example, Army, Navy). SERVICE_DISCHARGE_TYPE_ID Foreign key to the SERVICE_DISCHARGE_TYPE table. Indicates the classification of the discharge from the service (for example, Honorable, Dishonorable). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.MILITARY_SERVICE_EPISODE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.MILITARY_SERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.SERVICE_BRANCH_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.SERVICE_DISCHARGE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.MILITARY_SERVICE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.SERVICE_ENTRY_DATE_TXT IS 'BR3278 Service Entry Date is defined as the date the veteran commenced his/her episodes of military service. Service Entry Date is used to determine enrollment appropriately for 24 months of service computation.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.SERVICE_SEPARATION_DATE_TXT IS 'BR3281 Service Separation Date is defined as the date the veteran ended his/her episodes of military service. Service Separation Date is used to determine enrollment appropriately for 24 months of service computation.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.MILSRVCEPS_MILSRVC_FK_I ON ADR.MILITARY_SERVICE_EPISODE
(MILITARY_SERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVCEPS_MILSVCNBR_FB_I ON ADR.MILITARY_SERVICE_EPISODE
(UPPER("MILITARY_SERVICE_NUMBER"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE UNIQUE INDEX ADR.MILSRVCEPS_PK ON ADR.MILITARY_SERVICE_EPISODE
(MILITARY_SERVICE_EPISODE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVCEPS_STDMSVCMPT_FK_I ON ADR.MILITARY_SERVICE_EPISODE
(SERVICECOMPONENTTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVCEPS_STD_SVCBRNCH_FK_I ON ADR.MILITARY_SERVICE_EPISODE
(SERVICE_BRANCH_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVCEPS_STD_SVCDSCHRG_FK_I ON ADR.MILITARY_SERVICE_EPISODE
(SERVICE_DISCHARGE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MILITARY_SERV_EPIS_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."MILITARY_SERVICE_EPISODE" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into MILITARY_SERVICE_EPISODE_H
		(
		MILITARY_SERVICE_EPISODE_H_ID,
		MILITARY_SERVICE_EPISODE_ID,
		MILITARY_SERVICE_ID,
		SERVICE_BRANCH_TYPE_ID,
		SERVICE_DISCHARGE_TYPE_ID,
		SERVICECOMPONENTTYPE_ID,
		MILITARY_SERVICE_NUMBER,
		SERVICE_ENTRY_DATE_TXT,
		SERVICE_SEPARATION_DATE_TXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MILITARY_SERVICE_EPISODE_H_S.nextval,
		:old.MILITARY_SERVICE_EPISODE_ID,
		:old.MILITARY_SERVICE_ID,
		:old.SERVICE_BRANCH_TYPE_ID,
		:old.SERVICE_DISCHARGE_TYPE_ID,
		:old.SERVICECOMPONENTTYPE_ID,
		:old.MILITARY_SERVICE_NUMBER,
		:old.SERVICE_ENTRY_DATE_TXT,
		:old.SERVICE_SEPARATION_DATE_TXT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into MILITARY_SERVICE_EPISODE_H
		(
		MILITARY_SERVICE_EPISODE_H_ID,
		MILITARY_SERVICE_EPISODE_ID,
		MILITARY_SERVICE_ID,
		SERVICE_BRANCH_TYPE_ID,
		SERVICE_DISCHARGE_TYPE_ID,
		SERVICECOMPONENTTYPE_ID,
		MILITARY_SERVICE_NUMBER,
		SERVICE_ENTRY_DATE_TXT,
		SERVICE_SEPARATION_DATE_TXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MILITARY_SERVICE_EPISODE_H_S.nextval,
		:new.MILITARY_SERVICE_EPISODE_ID,
		:new.MILITARY_SERVICE_ID,
		:new.SERVICE_BRANCH_TYPE_ID,
		:new.SERVICE_DISCHARGE_TYPE_ID,
		:new.SERVICECOMPONENTTYPE_ID,
		:new.MILITARY_SERVICE_NUMBER,
		:new.SERVICE_ENTRY_DATE_TXT,
		:new.SERVICE_SEPARATION_DATE_TXT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  MILITARY_SERV_EPIS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM MILITARY_SERVICE_EPISODE FOR ADR.MILITARY_SERVICE_EPISODE;


ALTER TABLE ADR.MILITARY_SERVICE_EPISODE ADD (
  CONSTRAINT SYS_C0066734
  CHECK ("MILITARY_SERVICE_EPISODE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066735
  CHECK ("MILITARY_SERVICE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066736
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066737
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066738
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066739
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066740
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MILSRVCEPS_PK
  PRIMARY KEY
  (MILITARY_SERVICE_EPISODE_ID)
  USING INDEX ADR.MILSRVCEPS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MILITARY_SERVICE_EPISODE ADD (
  CONSTRAINT MILSRVCEPS_MILSRVC_FK 
  FOREIGN KEY (MILITARY_SERVICE_ID) 
  REFERENCES ADR.MILITARY_SERVICE (MILITARY_SERVICE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MILITARY_SERVICE_EPISODE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MILITARY_SERVICE_EPISODE TO ADR_RW;

GRANT SELECT ON ADR.MILITARY_SERVICE_EPISODE TO ES_R;

GRANT INSERT, SELECT ON ADR.MILITARY_SERVICE_EPISODE TO ES_RW;
ALTER TABLE ADR.MILITARY_SERVICE_EPISODE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_SERVICE_EPISODE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_SERVICE_EPISODE_H
(
  MILITARY_SERVICE_EPISODE_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  MILITARY_SERVICE_EPISODE_ID    NUMBER(20),
  MILITARY_SERVICE_ID            NUMBER(20),
  SERVICECOMPONENTTYPE_ID        NUMBER(20),
  SERVICE_BRANCH_TYPE_ID         NUMBER(20),
  SERVICE_DISCHARGE_TYPE_ID      NUMBER(20),
  MILITARY_SERVICE_NUMBER        VARCHAR2(20 BYTE),
  SERVICE_ENTRY_DATE_TXT         VARCHAR2(14 BYTE),
  SERVICE_SEPARATION_DATE_TXT    VARCHAR2(14 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_SERVICE_EPISODE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.MILITARY_SERVICE_EPISODE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.MILITARY_SERVICE_EPISODE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.MILITARY_SERVICE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.SERVICE_BRANCH_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.SERVICE_DISCHARGE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.MILITARY_SERVICE_NUMBER IS 'Military Service Number is a Unique identifier provided to a person by the branch of the military with which he or she served. It is defined as the service number assigned during the veterans episodes of military service. The social security number and service number can be the same. ';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.SERVICE_ENTRY_DATE_TXT IS 'BR3278 Service Entry Date is defined as the date the veteran commenced his/her episodes of military service. Service Entry Date is used to determine enrollment appropriately for 24 months of service computation.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.SERVICE_SEPARATION_DATE_TXT IS 'BR3281 Service Separation Date is defined as the date the veteran ended his/her episodes of military service. Service Separation Date is used to determine enrollment appropriately for 24 months of service computation.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_EPISODE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.MILSRVCEPH_MILSRVC_NU_I ON ADR.MILITARY_SERVICE_EPISODE_H
(MILITARY_SERVICE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MILSRVCEPH_PK ON ADR.MILITARY_SERVICE_EPISODE_H
(MILITARY_SERVICE_EPISODE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MILITARY_SERVICE_EPISODE_H FOR ADR.MILITARY_SERVICE_EPISODE_H;


ALTER TABLE ADR.MILITARY_SERVICE_EPISODE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_042
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066765
  CHECK ("MILITARY_SERVICE_EPISODE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066766
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066767
  CHECK ("MILITARY_SERVICE_EPISODE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066768
  CHECK ("MILITARY_SERVICE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066769
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066770
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066771
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066772
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066773
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MILSRVCEPH_PK
  PRIMARY KEY
  (MILITARY_SERVICE_EPISODE_H_ID)
  USING INDEX ADR.MILSRVCEPH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MILITARY_SERVICE_EPISODE_H TO ADR_R;

GRANT SELECT ON ADR.MILITARY_SERVICE_EPISODE_H TO ADR_RW;

GRANT SELECT ON ADR.MILITARY_SERVICE_EPISODE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MILITARY_SERVICE_EPISODE_H TO ES_RW;
ALTER TABLE ADR.MILITARY_SERVICE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MILITARY_SERVICE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MILITARY_SERVICE_H
(
  MILITARY_SERVICE_H_ID     NUMBER(20),
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE),
  MILITARY_SERVICE_ID       NUMBER(20),
  PERSON_ID                 NUMBER(20),
  STD_PERIODOFSERVICE_ID    NUMBER(20),
  FILIPINO_VET_DOC_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  RECORD_CREATED_DATE       DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE      DATE,
  RECORD_MODIFIED_COUNT     NUMBER(10),
  ACTIVE_DUTY_IND           VARCHAR2(1 BYTE),
  ACTIVE_DUTY_SRC           VARCHAR2(10 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MILITARY_SERVICE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.MILITARY_SERVICE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.MILITARY_SERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.STD_PERIODOFSERVICE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.FILIPINO_VET_DOC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.ACTIVE_DUTY_IND IS ' indicate whether vet is in active duty. It will set by EMIS or ES 0 for not active; 1 for active';

COMMENT ON COLUMN ADR.MILITARY_SERVICE_H.ACTIVE_DUTY_SRC IS ' source for Active duty Indicator 0 for not active; 1 for active';


CREATE UNIQUE INDEX ADR.MILSRVCH_PK ON ADR.MILITARY_SERVICE_H
(MILITARY_SERVICE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MILSRVCH_PRSNID_NU_I ON ADR.MILITARY_SERVICE_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MILITARY_SERVICE_H FOR ADR.MILITARY_SERVICE_H;


ALTER TABLE ADR.MILITARY_SERVICE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_033
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066583
  CHECK ("MILITARY_SERVICE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066584
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066585
  CHECK ("MILITARY_SERVICE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066586
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066587
  CHECK ("STD_INSTITUTION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066588
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066589
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066590
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066591
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066592
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MILSRVCH_PK
  PRIMARY KEY
  (MILITARY_SERVICE_H_ID)
  USING INDEX ADR.MILSRVCH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MILITARY_SERVICE_H TO ADR_R;

GRANT SELECT ON ADR.MILITARY_SERVICE_H TO ADR_RW;

GRANT SELECT ON ADR.MILITARY_SERVICE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MILITARY_SERVICE_H TO ES_RW;
DROP TABLE ADR.MLOG$_HL7_TRANSACTION_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.MLOG$_HL7_TRANSACTION_LOG
(
  HL7_TRANSACTION_LOG_ID        NUMBER(20),
  MESSAGE_TRANSMISSION_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID            NUMBER(20),
  RECORD_CREATED_DATE           DATE,
  TRANSMISSION_DATE             DATE,
  M_ROW$$                       VARCHAR2(255 BYTE),
  SEQUENCE$$                    NUMBER,
  SNAPTIME$$                    DATE,
  DMLTYPE$$                     VARCHAR2(1 BYTE),
  OLD_NEW$$                     VARCHAR2(1 BYTE),
  CHANGE_VECTOR$$               RAW(255),
  XID$$                         NUMBER
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MLOG$_HL7_TRANSACTION_LOG IS 'snapshot log for master table ADR.HL7_TRANSACTION_LOG';


CREATE INDEX ADR.I_MLOG$_HL7_TRANSACTION_LO ON ADR.MLOG$_HL7_TRANSACTION_LOG
(XID$$)
LOGGING
TABLESPACE M_DATA_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MLOG$_HL7LOG_ID_NU_I ON ADR.MLOG$_HL7_TRANSACTION_LOG
(HL7_TRANSACTION_LOG_ID)
LOGGING
TABLESPACE M_INDEX_02_P01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MLOG$_HL7LOG_SNAPTIME_NU_I ON ADR.MLOG$_HL7_TRANSACTION_LOG
(SNAPTIME$$)
LOGGING
TABLESPACE M_INDEX_02_P01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

GRANT SELECT ON ADR.MLOG$_HL7_TRANSACTION_LOG TO ADR_R;

GRANT SELECT ON ADR.MLOG$_HL7_TRANSACTION_LOG TO ADR_RW;
ALTER TABLE ADR.MONETARY_BENEFIT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MONETARY_BENEFIT CASCADE CONSTRAINTS;

CREATE TABLE ADR.MONETARY_BENEFIT
(
  MONETARY_BENEFIT_ID            NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  MONETARY_BENEFIT_AWARD_AMOUNT  NUMBER(10,2),
  VERIFIED_DATE                  DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MONETARY_BENEFIT IS 'The table stores information about a persons monetary awards as provided by the Veterans Benefits Administration (VBA). PERSON_ID Foreign key for the PERSON table. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.MONETARY_BENEFIT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.MONETARY_BENEFIT_AWARD_AMOUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.VERIFIED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.MONEYBEN_PK ON ADR.MONETARY_BENEFIT
(MONETARY_BENEFIT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MONEYBEN_PRSN_FK_I ON ADR.MONETARY_BENEFIT
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MONETARY_BENF_AUDIR_TRGR
after Update or Delete or Insert
on ADR.MONETARY_BENEFIT
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into MONETARY_BENEFIT_H
		(
		MONETARY_BENEFIT_H_ID,
		MONETARY_BENEFIT_ID,
		PERSON_ID,
		MONETARY_BENEFIT_AWARD_AMOUNT,
		VERIFIED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MONETARY_BENEFIT_H_S.nextval,
		:old.MONETARY_BENEFIT_ID,
		:old.PERSON_ID,
		:old.MONETARY_BENEFIT_AWARD_AMOUNT,
		:old.VERIFIED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into MONETARY_BENEFIT_H
		(
		MONETARY_BENEFIT_H_ID,
		MONETARY_BENEFIT_ID,
		PERSON_ID,
		MONETARY_BENEFIT_AWARD_AMOUNT,
		VERIFIED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MONETARY_BENEFIT_H_S.nextval,
		:new.MONETARY_BENEFIT_ID,
		:new.PERSON_ID,
		:new.MONETARY_BENEFIT_AWARD_AMOUNT,
		:new.VERIFIED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end  MONETARY_BENF_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM MONETARY_BENEFIT FOR ADR.MONETARY_BENEFIT;


ALTER TABLE ADR.MONETARY_BENEFIT ADD (
  CONSTRAINT MONEYBEN_PK
  PRIMARY KEY
  (MONETARY_BENEFIT_ID)
  USING INDEX ADR.MONEYBEN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MONETARY_BENEFIT ADD (
  CONSTRAINT MONEYBEN_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.MONETARY_BENEFIT TO ADREXT;

GRANT SELECT ON ADR.MONETARY_BENEFIT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MONETARY_BENEFIT TO ADR_RW;

GRANT SELECT ON ADR.MONETARY_BENEFIT TO ES_R;

GRANT INSERT, SELECT ON ADR.MONETARY_BENEFIT TO ES_RW;
ALTER TABLE ADR.MONETARY_BENEFIT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MONETARY_BENEFIT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.MONETARY_BENEFIT_DETAIL
(
  MONETARY_BENEFIT_DETAIL_ID    NUMBER(20),
  MONETARY_BENEFIT_ID           NUMBER(20),
  MONETARY_BENEFIT_TYPE_ID      NUMBER(20),
  MONETARY_BENEFIT_STDYESNO_ID  NUMBER(20),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MONETARY_BENEFIT_DETAIL IS 'The table stores details about a persons individual monetary awards as provided by the Veterans Benefits Administration (VBA). This is a child table of MONETARY_BENEFIT. MONETARY_BENEFIT_ID Foreign key for the MONETARY_BENEFIT table. MONETARY_BENEFIT_TYPE_ID Foreign key for the MONETARY_BENEFIT_TYPE table. Indicates the type of monetary benefit provided (for example Aid and Attendance, Housebound). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.MONETARY_BENEFIT_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.MONETARY_BENEFIT_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.MONETARY_BENEFIT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.MONEYBNDTL_MONEYBEN_FK_I ON ADR.MONETARY_BENEFIT_DETAIL
(MONETARY_BENEFIT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MONEYBNDTL_PK ON ADR.MONETARY_BENEFIT_DETAIL
(MONETARY_BENEFIT_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MONEYBNDTL_STDYESNO_FK_I ON ADR.MONETARY_BENEFIT_DETAIL
(MONETARY_BENEFIT_STDYESNO_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MONEYBNDTL_STD_MNTRYBNTYP_FK_I ON ADR.MONETARY_BENEFIT_DETAIL
(MONETARY_BENEFIT_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.MONETARY_BENF_DEF_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."MONETARY_BENEFIT_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into MONETARY_BENEFIT_DETAIL_H
		(
		MONETARY_BENEFIT_DETAIL_H_ID,
		MONETARY_BENEFIT_DETAIL_ID,
		MONETARY_BENEFIT_ID,
		MONETARY_BENEFIT_TYPE_ID,
		MONETARY_BENEFIT_STDYESNO_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MONETARY_BENEFIT_DETAIL_H_S.nextval,
		:old.MONETARY_BENEFIT_DETAIL_ID,
		:old.MONETARY_BENEFIT_ID,
		:old.MONETARY_BENEFIT_TYPE_ID,
		:old.MONETARY_BENEFIT_STDYESNO_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into MONETARY_BENEFIT_DETAIL_H
		(
		MONETARY_BENEFIT_DETAIL_H_ID,
		MONETARY_BENEFIT_DETAIL_ID,
		MONETARY_BENEFIT_ID,
		MONETARY_BENEFIT_TYPE_ID,
		MONETARY_BENEFIT_STDYESNO_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
      	)
		values
		(
		MONETARY_BENEFIT_DETAIL_H_S.nextval,
		:new.MONETARY_BENEFIT_DETAIL_ID,
		:new.MONETARY_BENEFIT_ID,
		:new.MONETARY_BENEFIT_TYPE_ID,
		:new.MONETARY_BENEFIT_STDYESNO_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  MONETARY_BENF_DEF_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM MONETARY_BENEFIT_DETAIL FOR ADR.MONETARY_BENEFIT_DETAIL;


ALTER TABLE ADR.MONETARY_BENEFIT_DETAIL ADD (
  CONSTRAINT SYS_C0066217
  CHECK ("MONETARY_BENEFIT_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066218
  CHECK ("MONETARY_BENEFIT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066219
  CHECK ("MONETARY_BENEFIT_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066220
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066221
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066222
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066223
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066224
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MONEYBNDTL_PK
  PRIMARY KEY
  (MONETARY_BENEFIT_DETAIL_ID)
  USING INDEX ADR.MONEYBNDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.MONETARY_BENEFIT_DETAIL ADD (
  CONSTRAINT MONEYBNDTL_MONEYBEN_FK 
  FOREIGN KEY (MONETARY_BENEFIT_ID) 
  REFERENCES ADR.MONETARY_BENEFIT (MONETARY_BENEFIT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL TO ADREXT;

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.MONETARY_BENEFIT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.MONETARY_BENEFIT_DETAIL TO ES_RW;
ALTER TABLE ADR.MONETARY_BENEFIT_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MONETARY_BENEFIT_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MONETARY_BENEFIT_DETAIL_H
(
  MONETARY_BENEFIT_DETAIL_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE),
  MONETARY_BENEFIT_DETAIL_ID    NUMBER(20),
  MONETARY_BENEFIT_ID           NUMBER(20),
  MONETARY_BENEFIT_TYPE_ID      NUMBER(20),
  MONETARY_BENEFIT_STDYESNO_ID  NUMBER(20),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MONETARY_BENEFIT_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.MONETARY_BENEFIT_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.MONETARY_BENEFIT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.MONETARY_BENEFIT_STDYESNO_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.MONEYBNDTLH_MONEYBEN_NU_I ON ADR.MONETARY_BENEFIT_DETAIL_H
(MONETARY_BENEFIT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.MONEYBNDTLH_PK ON ADR.MONETARY_BENEFIT_DETAIL_H
(MONETARY_BENEFIT_DETAIL_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MONETARY_BENEFIT_DETAIL_H FOR ADR.MONETARY_BENEFIT_DETAIL_H;


ALTER TABLE ADR.MONETARY_BENEFIT_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_021
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066309
  CHECK ("MONETARY_BENEFIT_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066310
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066311
  CHECK ("MONETARY_BENEFIT_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066312
  CHECK ("MONETARY_BENEFIT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066313
  CHECK ("MONETARY_BENEFIT_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066314
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066315
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066316
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066317
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066318
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MONEYBNDTLH_PK
  PRIMARY KEY
  (MONETARY_BENEFIT_DETAIL_H_ID)
  USING INDEX ADR.MONEYBNDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.MONETARY_BENEFIT_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MONETARY_BENEFIT_DETAIL_H TO ES_RW;
ALTER TABLE ADR.MONETARY_BENEFIT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.MONETARY_BENEFIT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.MONETARY_BENEFIT_H
(
  MONETARY_BENEFIT_H_ID          NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  MONETARY_BENEFIT_ID            NUMBER(20),
  PERSON_ID                      NUMBER(20),
  MONETARY_BENEFIT_AWARD_AMOUNT  NUMBER(10,2),
  VERIFIED_DATE                  DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.MONETARY_BENEFIT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.MONETARY_BENEFIT_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.VERIFIED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.MONETARY_BENEFIT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.MONEYBENH_PK ON ADR.MONETARY_BENEFIT_H
(MONETARY_BENEFIT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.MONEYBENH_PRSNID_NU_I ON ADR.MONETARY_BENEFIT_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM MONETARY_BENEFIT_H FOR ADR.MONETARY_BENEFIT_H;


ALTER TABLE ADR.MONETARY_BENEFIT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_046
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066811
  CHECK ("MONETARY_BENEFIT_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066812
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066813
  CHECK ("MONETARY_BENEFIT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066814
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066815
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066816
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066817
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066818
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066819
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT MONEYBENH_PK
  PRIMARY KEY
  (MONETARY_BENEFIT_H_ID)
  USING INDEX ADR.MONEYBENH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.MONETARY_BENEFIT_H TO ADR_R;

GRANT SELECT ON ADR.MONETARY_BENEFIT_H TO ADR_RW;

GRANT SELECT ON ADR.MONETARY_BENEFIT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.MONETARY_BENEFIT_H TO ES_RW;
ALTER TABLE ADR.NCOA_ADDRESS_VERIFY_SUMMARY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.NCOA_ADDRESS_VERIFY_SUMMARY CASCADE CONSTRAINTS;

CREATE TABLE ADR.NCOA_ADDRESS_VERIFY_SUMMARY
(
  NCOA_ADDRESS_VERIFY_SUMMARY_ID  NUMBER(20)    NOT NULL,
  SCREENED_ADDRESS_COUNT          NUMBER(10),
  EXCLUDED_ADDRESS_COUNT          NUMBER(10),
  DECEASED_ADDRESS_COUNT          NUMBER(10),
  SENT_TO_AAC_COUNT               NUMBER(10),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.NCOA_ADDRESS_VERIFY_SUMMARY IS 'Contains a summary of the address batch that was sent for NCOA processing';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.NCOA_ADDRESS_VERIFY_SUMMARY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.NCOA_ADDRESS_VERIFY_SUMMARY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.VERIFYSUM_PK ON ADR.NCOA_ADDRESS_VERIFY_SUMMARY
(NCOA_ADDRESS_VERIFY_SUMMARY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM NCOA_ADDRESS_VERIFY_SUMMARY FOR ADR.NCOA_ADDRESS_VERIFY_SUMMARY;


ALTER TABLE ADR.NCOA_ADDRESS_VERIFY_SUMMARY ADD (
  CONSTRAINT VERIFYSUM_PK
  PRIMARY KEY
  (NCOA_ADDRESS_VERIFY_SUMMARY_ID)
  USING INDEX ADR.VERIFYSUM_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.NCOA_ADDRESS_VERIFY_SUMMARY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.NCOA_ADDRESS_VERIFY_SUMMARY TO ADR_RW;

GRANT SELECT ON ADR.NCOA_ADDRESS_VERIFY_SUMMARY TO ES_R;

GRANT INSERT, SELECT ON ADR.NCOA_ADDRESS_VERIFY_SUMMARY TO ES_RW;
ALTER TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT
(
  PATIENT_PROVIDER_ASSIGNMENT_ID  NUMBER(20),
  HEALTH_CARE_PROVIDER_ID         NUMBER(20),
  PERSON_ID                       NUMBER(20),
  STD_TEAM_PURPOSE_ID             NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  TEAM_ID                         NUMBER(20),
  TEAM_NAME                       VARCHAR2(30 BYTE),
  DATE_ASSIGNED                   DATE,
  DATE_UNASSIGNED                 DATE,
  PATIENT_IEN                     VARCHAR2(20 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_COUNT           NUMBER(10),
  RECORD_MODIFIED_DATE            DATE
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT IS 'A current HEALTH CARE PROVIDER assigned to PATIRNT at a facility.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.DATE_ASSIGNED IS 'Date Provider Assigned to Team Position at Assigning Facility.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.DATE_UNASSIGNED IS 'Date Provider Un-Assigned from Team Position';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.PATIENT_IEN IS 'Uniquely tracks the PATIENT, PROVIDER, TEAM relationship.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE INDEX ADR.IX_RELATIONSHIP59 ON ADR.PATIENT_PROVIDER_ASSIGNMENT
(HEALTH_CARE_PROVIDER_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP65 ON ADR.PATIENT_PROVIDER_ASSIGNMENT
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP74 ON ADR.PATIENT_PROVIDER_ASSIGNMENT
(STD_TEAM_PURPOSE_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PATPRVASGN_PK ON ADR.PATIENT_PROVIDER_ASSIGNMENT
(PATIENT_PROVIDER_ASSIGNMENT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PPA_STDINS_FK_I ON ADR.PATIENT_PROVIDER_ASSIGNMENT
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PAT_PROV_ASSIGN_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."PATIENT_PROVIDER_ASSIGNMENT" FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into PATIENT_PROVIDER_ASSIGNMENT_H
        (
        PATIENT_PROVIDER_ASSIGN_H_ID,
        PATIENT_PROVIDER_ASSIGNMENT_ID,
        HEALTH_CARE_PROVIDER_ID,
        PERSON_ID,
        STD_TEAM_PURPOSE_ID,
        STD_INSTITUTION_ID,
        TEAM_ID,
        TEAM_NAME,
        DATE_ASSIGNED,
        DATE_UNASSIGNED,
        PATIENT_IEN,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PATIENT_PROV_ASSIGN_H_S.nextval,
        :old.PATIENT_PROVIDER_ASSIGNMENT_ID,
        :old.HEALTH_CARE_PROVIDER_ID,
        :old.PERSON_ID,
        :old.STD_TEAM_PURPOSE_ID,
        :old.STD_INSTITUTION_ID,
        :old.TEAM_ID,
        :old.TEAM_NAME,
        :old.DATE_ASSIGNED,
        :old.DATE_UNASSIGNED,
        :old.PATIENT_IEN,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into PATIENT_PROVIDER_ASSIGNMENT_H
        (
        PATIENT_PROVIDER_ASSIGN_H_ID,
        PATIENT_PROVIDER_ASSIGNMENT_ID,
        HEALTH_CARE_PROVIDER_ID,
        PERSON_ID,
        STD_TEAM_PURPOSE_ID,
        STD_INSTITUTION_ID,
        TEAM_ID,
        TEAM_NAME,
        DATE_ASSIGNED,
        DATE_UNASSIGNED,
        PATIENT_IEN,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PATIENT_PROV_ASSIGN_H_S.nextval,
        :new.PATIENT_PROVIDER_ASSIGNMENT_ID,
        :new.HEALTH_CARE_PROVIDER_ID,
        :new.PERSON_ID,
        :new.STD_TEAM_PURPOSE_ID,
        :new.STD_INSTITUTION_ID,
        :new.TEAM_ID,
        :new.TEAM_NAME,
        :new.DATE_ASSIGNED,
        :new.DATE_UNASSIGNED,
        :new.PATIENT_IEN,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end PAT_PROV_ASSIGN_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM PATIENT_PROVIDER_ASSIGNMENT FOR ADR.PATIENT_PROVIDER_ASSIGNMENT;


ALTER TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT ADD (
  CONSTRAINT SYS_C00160891
  CHECK ("PATIENT_PROVIDER_ASSIGNMENT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160892
  CHECK ("HEALTH_CARE_PROVIDER_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160893
  CHECK ("STD_INSTITUTION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160894
  CHECK ("DATE_ASSIGNED" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160895
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160896
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160897
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160898
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160899
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PATPRVASGN_PK
  PRIMARY KEY
  (PATIENT_PROVIDER_ASSIGNMENT_ID)
  USING INDEX ADR.PATPRVASGN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT ADD (
  CONSTRAINT RELATIONSHIP59 
  FOREIGN KEY (HEALTH_CARE_PROVIDER_ID) 
  REFERENCES ADR.HEALTH_CARE_PROVIDER (HEALTH_CARE_PROVIDER_ID)
  ENABLE VALIDATE,
  CONSTRAINT RELATIONSHIP65 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PATIENT_PROVIDER_ASSIGNMENT TO ADR_RW;

GRANT DELETE ON ADR.PATIENT_PROVIDER_ASSIGNMENT TO EEUSR;

GRANT SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT TO ES_RW;
ALTER TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT_H
(
  PATIENT_PROVIDER_ASSIGN_H_ID    NUMBER(20)    NOT NULL,
  PATIENT_PROVIDER_ASSIGNMENT_ID  NUMBER(20)    NOT NULL,
  HEALTH_CARE_PROVIDER_ID         NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20),
  STD_TEAM_PURPOSE_ID             NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20)    NOT NULL,
  TEAM_ID                         NUMBER(20),
  TEAM_NAME                       VARCHAR2(30 BYTE),
  DATE_ASSIGNED                   DATE          NOT NULL,
  DATE_UNASSIGNED                 DATE,
  PATIENT_IEN                     VARCHAR2(20 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  HISTORY_CREATED_DATE            DATE,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT_H IS 'A current HEALTH CARE PROVIDER assigned to PATIRNT at a facility.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.HEALTH_CARE_PROVIDER_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.DATE_ASSIGNED IS 'Date Provider Assigned to Team Position at Assigning Facility.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.DATE_UNASSIGNED IS 'Date Provider Un-Assigned from Team Position';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.PATIENT_IEN IS 'Uniquely tracks the PATIENT, PROVIDER, TEAM relationship.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PATIENT_PROVIDER_ASSIGNMENT_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE UNIQUE INDEX ADR.PATPRVASGNH_PK ON ADR.PATIENT_PROVIDER_ASSIGNMENT_H
(PATIENT_PROVIDER_ASSIGN_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PATIENT_PROVIDER_ASSIGNMENT_H FOR ADR.PATIENT_PROVIDER_ASSIGNMENT_H;


ALTER TABLE ADR.PATIENT_PROVIDER_ASSIGNMENT_H ADD (
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT PATPRVASGNH_PK
  PRIMARY KEY
  (PATIENT_PROVIDER_ASSIGN_H_ID)
  USING INDEX ADR.PATPRVASGNH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PATIENT_PROVIDER_ASSIGNMENT_H TO ADR_RW;

GRANT SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PATIENT_PROVIDER_ASSIGNMENT_H TO ES_RW;
ALTER TABLE ADR.PCP_STAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PCP_STAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PCP_STAGE
(
  PCP_STAGE_ID           NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20),
  PCP_FIRST_NAME         VARCHAR2(30 BYTE),
  PCP_LAST_NAME          VARCHAR2(40 BYTE),
  PCP_MID_NAME           VARCHAR2(30 BYTE),
  PCP_NPI                VARCHAR2(50 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PCP_STAGE IS 'Staging table will be used hold PCP Clinical Data, this table will be truncated after each send';

COMMENT ON COLUMN ADR.PCP_STAGE.PERSON_ID IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PCP_STAGE.PCP_FIRST_NAME IS 'is the PCP First name.';

COMMENT ON COLUMN ADR.PCP_STAGE.PCP_LAST_NAME IS 'is the PCP Last name.';

COMMENT ON COLUMN ADR.PCP_STAGE.PCP_MID_NAME IS 'is the PCP middle name';

COMMENT ON COLUMN ADR.PCP_STAGE.PCP_NPI IS 'is the PCP NPI';

COMMENT ON COLUMN ADR.PCP_STAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PCP_STAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PCP_STAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PCP_STAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PCP_STAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.PCPSTAGE_PERSONID_FK_I ON ADR.PCP_STAGE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PCP_STAGE_PK ON ADR.PCP_STAGE
(PCP_STAGE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PCP_STAGE FOR ADR.PCP_STAGE;


ALTER TABLE ADR.PCP_STAGE ADD (
  CONSTRAINT PCP_STAGE_PK
  PRIMARY KEY
  (PCP_STAGE_ID)
  USING INDEX ADR.PCP_STAGE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PCP_STAGE ADD (
  CONSTRAINT PCPSTAGE_PERSONID_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PCP_STAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PCP_STAGE TO ADR_RW;
ALTER TABLE ADR.PENSION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PENSION CASCADE CONSTRAINTS;

CREATE TABLE ADR.PENSION
(
  PENSION_ID                      NUMBER(20)    NOT NULL,
  MONETARY_BENEFIT_DETAIL_ID      NUMBER(20),
  PENSION_AWARD_REASON_ID         NUMBER(20),
  PENSION_AWARD_TERMINATION_ID1   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID2   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID3   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID4   NUMBER(20),
  PENSION_AWARD_EFFECTIVE_DATE    DATE,
  PENSION_AWARD_TERMINATION_DATE  DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.PENSION.PENSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.PENSION.PENSION_AWARD_EFFECTIVE_DATE IS 'Beginning of effective period.';

COMMENT ON COLUMN ADR.PENSION.PENSION_AWARD_TERMINATION_DATE IS 'End of effective period.';

COMMENT ON COLUMN ADR.PENSION.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.PENSION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PENSION.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.PENSION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PENSION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.IX_RELATIONSHIP51 ON ADR.PENSION
(PENSION_AWARD_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP52 ON ADR.PENSION
(PENSION_AWARD_TERMINATION_ID1)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP53 ON ADR.PENSION
(PENSION_AWARD_TERMINATION_ID2)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP54 ON ADR.PENSION
(PENSION_AWARD_TERMINATION_ID3)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RELATIONSHIP55 ON ADR.PENSION
(PENSION_AWARD_TERMINATION_ID4)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PNS_MNTBENDTL_FK_I ON ADR.PENSION
(MONETARY_BENEFIT_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PNS_PK ON ADR.PENSION
(PENSION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PENSION_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.PENSION
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into PENSION_H
        (
        PENSION_H_ID,
        PENSION_ID,
        MONETARY_BENEFIT_DETAIL_ID,
        PENSION_AWARD_REASON_ID,
        PENSION_AWARD_EFFECTIVE_DATE,
        PENSION_AWARD_TERMINATION_DATE,
        PENSION_AWARD_TERMINATION_ID1,
        PENSION_AWARD_TERMINATION_ID2,
        PENSION_AWARD_TERMINATION_ID3,
        PENSION_AWARD_TERMINATION_ID4,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PENSION_H_S.nextval,
        :old.PENSION_ID,
        :old.MONETARY_BENEFIT_DETAIL_ID,
        :old.PENSION_AWARD_REASON_ID,
        :old.PENSION_AWARD_EFFECTIVE_DATE,
        :old.PENSION_AWARD_TERMINATION_DATE,
        :old.PENSION_AWARD_TERMINATION_ID1,
        :old.PENSION_AWARD_TERMINATION_ID2,
        :old.PENSION_AWARD_TERMINATION_ID3,
        :old.PENSION_AWARD_TERMINATION_ID4,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into PENSION_H
        (
        PENSION_H_ID,
        PENSION_ID,
        MONETARY_BENEFIT_DETAIL_ID,
        PENSION_AWARD_REASON_ID,
        PENSION_AWARD_EFFECTIVE_DATE,
        PENSION_AWARD_TERMINATION_DATE,
        PENSION_AWARD_TERMINATION_ID1,
        PENSION_AWARD_TERMINATION_ID2,
        PENSION_AWARD_TERMINATION_ID3,
        PENSION_AWARD_TERMINATION_ID4,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PENSION_H_S.nextval,
        :new.PENSION_ID,
        :new.MONETARY_BENEFIT_DETAIL_ID,
        :new.PENSION_AWARD_REASON_ID,
        :new.PENSION_AWARD_EFFECTIVE_DATE,
        :new.PENSION_AWARD_TERMINATION_DATE,
        :new.PENSION_AWARD_TERMINATION_ID1,
        :new.PENSION_AWARD_TERMINATION_ID2,
        :new.PENSION_AWARD_TERMINATION_ID3,
        :new.PENSION_AWARD_TERMINATION_ID4,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end PENSION_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PENSION FOR ADR.PENSION;


ALTER TABLE ADR.PENSION ADD (
  CONSTRAINT PNS_PK
  PRIMARY KEY
  (PENSION_ID)
  USING INDEX ADR.PNS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PENSION ADD (
  CONSTRAINT RELATIONSHIP51 
  FOREIGN KEY (PENSION_AWARD_REASON_ID) 
  REFERENCES ADR.PENSION_REASON (PENSION_REASON_ID)
  ENABLE VALIDATE,
  CONSTRAINT RELATIONSHIP52 
  FOREIGN KEY (PENSION_AWARD_TERMINATION_ID1) 
  REFERENCES ADR.PENSION_REASON (PENSION_REASON_ID)
  ENABLE VALIDATE,
  CONSTRAINT RELATIONSHIP53 
  FOREIGN KEY (PENSION_AWARD_TERMINATION_ID2) 
  REFERENCES ADR.PENSION_REASON (PENSION_REASON_ID)
  ENABLE VALIDATE,
  CONSTRAINT RELATIONSHIP54 
  FOREIGN KEY (PENSION_AWARD_TERMINATION_ID3) 
  REFERENCES ADR.PENSION_REASON (PENSION_REASON_ID)
  ENABLE VALIDATE,
  CONSTRAINT RELATIONSHIP55 
  FOREIGN KEY (PENSION_AWARD_TERMINATION_ID4) 
  REFERENCES ADR.PENSION_REASON (PENSION_REASON_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PENSION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PENSION TO ADR_RW;

GRANT SELECT ON ADR.PENSION TO ES_R;

GRANT INSERT, SELECT ON ADR.PENSION TO ES_RW;
ALTER TABLE ADR.PENSION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PENSION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PENSION_H
(
  PENSION_H_ID                    NUMBER(20)    NOT NULL,
  PENSION_ID                      NUMBER(20)    NOT NULL,
  MONETARY_BENEFIT_DETAIL_ID      NUMBER(20),
  PENSION_AWARD_REASON_ID         NUMBER(20),
  PENSION_AWARD_TERMINATION_ID1   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID2   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID3   NUMBER(20),
  PENSION_AWARD_TERMINATION_ID4   NUMBER(20),
  PENSION_AWARD_EFFECTIVE_DATE    DATE,
  PENSION_AWARD_TERMINATION_DATE  DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  HISTORY_CREATED_DATE            DATE,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.PENSION_H.PENSION_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.PENSION_H.PENSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.PENSION_H.PENSION_AWARD_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.PENSION_H.PENSION_AWARD_EFFECTIVE_DATE IS 'Beginning of effective period.';

COMMENT ON COLUMN ADR.PENSION_H.PENSION_AWARD_TERMINATION_DATE IS 'End of effective period.';

COMMENT ON COLUMN ADR.PENSION_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.PENSION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PENSION_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.PENSION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PENSION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PENSION_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.PNSH_MNTBENDTL_FK_I ON ADR.PENSION_H
(MONETARY_BENEFIT_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PNSH_PK ON ADR.PENSION_H
(PENSION_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PENSION_H FOR ADR.PENSION_H;


ALTER TABLE ADR.PENSION_H ADD (
  CONSTRAINT PNSH_PK
  PRIMARY KEY
  (PENSION_H_ID)
  USING INDEX ADR.PNSH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PENSION_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PENSION_H TO ADR_RW;

GRANT SELECT ON ADR.PENSION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PENSION_H TO ES_RW;
ALTER TABLE ADR.PENSION_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PENSION_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.PENSION_REASON
(
  PENSION_REASON_ID      NUMBER(20)             NOT NULL,
  CODE                   VARCHAR2(12 BYTE)      NOT NULL,
  NAME                   VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(100 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PENSION_REASON IS 'Reason for awarding a pension or terminationg a pension award';

COMMENT ON COLUMN ADR.PENSION_REASON.PENSION_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.PENSION_REASON.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.PENSION_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PENSION_REASON.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.PENSION_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PENSION_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PNSRSN_PK ON ADR.PENSION_REASON
(PENSION_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PENSION_REASON FOR ADR.PENSION_REASON;


ALTER TABLE ADR.PENSION_REASON ADD (
  CONSTRAINT PNSRSN_PK
  PRIMARY KEY
  (PENSION_REASON_ID)
  USING INDEX ADR.PNSRSN_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PENSION_REASON TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PENSION_REASON TO ADR_RW;

GRANT SELECT ON ADR.PENSION_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.PENSION_REASON TO ES_RW;
ALTER TABLE ADR.PERMISSION_SET
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERMISSION_SET CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERMISSION_SET
(
  PERMISSION_SET_ID      NUMBER(20)             NOT NULL,
  NAME                   VARCHAR2(100 BYTE)     NOT NULL,
  DESCRIPTION            VARCHAR2(250 BYTE),
  ACTIVE_DATE            DATE,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERMISSION_SET IS 'LOOKUP TABLE - this contains the list of permissions present in the application: Administer Application Parameters Administer System Parameters Manage Address Manage All Eligibility & Enrollment Manage Communications Log Manage EGT Setting Manage Financial Information Manage Person Associations Manage Person Demographics Manage Person Insurance Manage Person Military Service Info Manage Person POW Information Manage Person Purple Heart Manage Registries Manage Select Eligibility & Enrollment Manage User Profile Manage Workload Perform S&D Merge Perform Veteran Merge Re-transmit Messages Schedule/Run Reports View Completed Reports View Registries';

COMMENT ON COLUMN ADR.PERMISSION_SET.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERMISSION_SET.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERMISSION_SET.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERMISSION_SET.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERMISSION_SET.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PERMSET_PK ON ADR.PERMISSION_SET
(PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PERMSET_U_CODE_UK ON ADR.PERMISSION_SET
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERMISSION_SET_audir_trgr
after Update or Delete or Insert
ON ADR.PERMISSION_SET
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PERMISSION_SET_H
		(
		PERMISSION_SET_H_ID,
		PERMISSION_SET_ID,
		NAME,
		DESCRIPTION,
		ACTIVE_DATE,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERMISSION_SET_H_S.nextval,
		:old.PERMISSION_SET_ID,
		:old.NAME,
		:old.DESCRIPTION,
		:old.ACTIVE_DATE,
		:old.INACTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PERMISSION_SET_H
		(
		PERMISSION_SET_H_ID,
		PERMISSION_SET_ID,
		NAME,
		DESCRIPTION,
		ACTIVE_DATE,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERMISSION_SET_H_S.nextval,
		:new.PERMISSION_SET_ID,
		:new.NAME,
		:new.DESCRIPTION,
		:new.ACTIVE_DATE,
		:new.INACTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end PERMISSION_SET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERMISSION_SET FOR ADR.PERMISSION_SET;


ALTER TABLE ADR.PERMISSION_SET ADD (
  CONSTRAINT PERMSET_PK
  PRIMARY KEY
  (PERMISSION_SET_ID)
  USING INDEX ADR.PERMSET_PK
  ENABLE VALIDATE,
  CONSTRAINT PERMSET_U_CODE_UK
  UNIQUE (NAME)
  USING INDEX ADR.PERMSET_U_CODE_UK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERMISSION_SET TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERMISSION_SET TO ADR_RW;

GRANT SELECT ON ADR.PERMISSION_SET TO CARUSR_R;

GRANT SELECT ON ADR.PERMISSION_SET TO ES_R;

GRANT INSERT, SELECT ON ADR.PERMISSION_SET TO ES_RW;
ALTER TABLE ADR.PERMISSION_SET_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERMISSION_SET_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERMISSION_SET_H
(
  PERMISSION_SET_H_ID    NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  PERMISSION_SET_ID      NUMBER(20)             NOT NULL,
  NAME                   VARCHAR2(100 BYTE)     NOT NULL,
  DESCRIPTION            VARCHAR2(250 BYTE),
  ACTIVE_DATE            DATE,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERMISSION_SET_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.PERMISSION_SET_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERMISSION_SET_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PERMSETH_PK ON ADR.PERMISSION_SET_H
(PERMISSION_SET_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERMISSION_SET_H FOR ADR.PERMISSION_SET_H;


ALTER TABLE ADR.PERMISSION_SET_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_036
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT PERMSETH_PK
  PRIMARY KEY
  (PERMISSION_SET_H_ID)
  USING INDEX ADR.PERMSETH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERMISSION_SET_H TO ADR_R;

GRANT SELECT ON ADR.PERMISSION_SET_H TO ADR_RW;

GRANT SELECT ON ADR.PERMISSION_SET_H TO CARUSR_R;

GRANT SELECT ON ADR.PERMISSION_SET_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERMISSION_SET_H TO ES_RW;
ALTER TABLE ADR.PERMISSION_SET_PERMISSIONS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERMISSION_SET_PERMISSIONS CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERMISSION_SET_PERMISSIONS
(
  PERM_SET_PERMISSIONS_ID  NUMBER(20)           NOT NULL,
  PERMISSION_TYPE_ID       NUMBER(20)           NOT NULL,
  PERMISSION_SET_ID        NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERMISSION_SET_PERMISSIONS IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.PERM_SET_PERMISSIONS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRMSTPRM_PK ON ADR.PERMISSION_SET_PERMISSIONS
(PERM_SET_PERMISSIONS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PSETPRM_PERMSET_FK_I ON ADR.PERMISSION_SET_PERMISSIONS
(PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PSETPRM_STD_PERM_FK_I ON ADR.PERMISSION_SET_PERMISSIONS
(PERMISSION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERM_SET_PERM_audir_trgr
after Update or Delete or Insert
ON ADR.PERMISSION_SET_PERMISSIONS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PERMISSION_SET_PERMISSIONS_H
		(
		PERM_SET_PERMISSIONS_H_ID,
		PERM_SET_PERMISSIONS_ID,
		PERMISSION_SET_ID,
		PERMISSION_TYPE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERMSSN_SET_PERMSSNS_H_S.nextval,
		:old.PERM_SET_PERMISSIONS_ID,
		:old.PERMISSION_SET_ID,
		:old.PERMISSION_TYPE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PERMISSION_SET_PERMISSIONS_H
		(
		PERM_SET_PERMISSIONS_H_ID,
		PERM_SET_PERMISSIONS_ID,
		PERMISSION_SET_ID,
		PERMISSION_TYPE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERMSSN_SET_PERMSSNS_H_S.nextval,
		:new.PERM_SET_PERMISSIONS_ID,
		:new.PERMISSION_SET_ID,
		:new.PERMISSION_TYPE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end PERM_SET_PERM_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERMISSION_SET_PERMISSIONS FOR ADR.PERMISSION_SET_PERMISSIONS;


ALTER TABLE ADR.PERMISSION_SET_PERMISSIONS ADD (
  CONSTRAINT PRMSTPRM_PK
  PRIMARY KEY
  (PERM_SET_PERMISSIONS_ID)
  USING INDEX ADR.PRMSTPRM_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERMISSION_SET_PERMISSIONS ADD (
  CONSTRAINT PSETPRM_PERMSET_FK 
  FOREIGN KEY (PERMISSION_SET_ID) 
  REFERENCES ADR.PERMISSION_SET (PERMISSION_SET_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT PSETPRM_STD_PERM_FK 
  FOREIGN KEY (PERMISSION_TYPE_ID) 
  REFERENCES ADRREF.STD_PERMISSIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERMISSION_SET_PERMISSIONS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERMISSION_SET_PERMISSIONS TO ADR_RW;

GRANT SELECT ON ADR.PERMISSION_SET_PERMISSIONS TO ES_R;

GRANT INSERT, SELECT ON ADR.PERMISSION_SET_PERMISSIONS TO ES_RW;
ALTER TABLE ADR.PERMISSION_SET_PERMISSIONS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERMISSION_SET_PERMISSIONS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERMISSION_SET_PERMISSIONS_H
(
  PERM_SET_PERMISSIONS_H_ID  NUMBER(20)         NOT NULL,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)   NOT NULL,
  PERM_SET_PERMISSIONS_ID    NUMBER(20)         NOT NULL,
  PERMISSION_SET_ID          NUMBER(20)         NOT NULL,
  PERMISSION_TYPE_ID         NUMBER(20)         NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERMISSION_SET_PERMISSIONS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.PERM_SET_PERMISSIONS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.PERM_SET_PERMISSIONS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERMISSION_SET_PERMISSIONS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRMSTPRMH_PK ON ADR.PERMISSION_SET_PERMISSIONS_H
(PERM_SET_PERMISSIONS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERMISSION_SET_PERMISSIONS_H FOR ADR.PERMISSION_SET_PERMISSIONS_H;


ALTER TABLE ADR.PERMISSION_SET_PERMISSIONS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_063
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT PRMSTPRMH_PK
  PRIMARY KEY
  (PERM_SET_PERMISSIONS_H_ID)
  USING INDEX ADR.PRMSTPRMH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERMISSION_SET_PERMISSIONS_H TO ADR_R;

GRANT SELECT ON ADR.PERMISSION_SET_PERMISSIONS_H TO ADR_RW;

GRANT SELECT ON ADR.PERMISSION_SET_PERMISSIONS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERMISSION_SET_PERMISSIONS_H TO ES_RW;
ALTER TABLE ADR.PERSON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON
(
  PERSON_ID                       NUMBER(20),
  VPID_ID                         NUMBER(20),
  VPID_VALUE                      VARCHAR2(30 BYTE),
  HEC_IEN                         VARCHAR2(20 BYTE),
  STD_PROGRAMOFSTUDY_ID           NUMBER(20),
  STD_RELIGION_ID                 NUMBER(20),
  STD_MARITALSTATUS_ID            NUMBER(20),
  USER_ENROLEE_SITE_ID            NUMBER(20),
  PREFERRED_SITE_ID               NUMBER(20),
  DEATH_REPORTING_SITE_ID         NUMBER(20),
  DEATH_NOTIFICATION_SRC_TYPE_ID  NUMBER(20),
  CLAIM_FOLDER_LOCATION_SITE_ID   NUMBER(20),
  CLAIM_FOLDER_NUMBER             VARCHAR2(20 BYTE),
  EDUCATIONAL_DEGREE              VARCHAR2(30 BYTE),
  REMARKS                         VARCHAR2(4000 BYTE),
  SENSITIVITY_FLAG_SET_SRC_ID     NUMBER(20),
  SENSITIVITY_FLAG_SET_SITE_ID    NUMBER(20),
  SENSITIVITY_IND                 VARCHAR2(1 BYTE) DEFAULT '0',
  SENSITIVITY_FLAG_SET_BY         VARCHAR2(30 BYTE),
  SENSITIVITY_FLAG_SET_DATE       DATE,
  VETERAN_IND                     VARCHAR2(1 BYTE) DEFAULT '0',
  WAS_PRISONER_OF_WAR_IND         VARCHAR2(1 BYTE),
  EE_MIGRATED_IND                 VARCHAR2(1 BYTE),
  DEATH_DATE_TXT                  VARCHAR2(14 BYTE),
  DEATH_REPORTED_DATE             DATE,
  LAZARUS_DATE_TXT                VARCHAR2(14 BYTE),
  USER_ENROLEE_THROUGH_YEAR       NUMBER(4),
  DISABILITY_DISCHARGE_IND        VARCHAR2(1 BYTE),
  MILITARY_DSBLTY_RETIREMENT_IND  VARCHAR2(1 BYTE),
  COMBAT_VET_ELIGIBILITY_END_DT   DATE,
  APPOINTMENT_WAS_REQUESTED_IND   VARCHAR2(1 BYTE),
  APPOINTMENT_REQUESTED_DATE      DATE,
  VA_ASSOCIATE_TITLE              VARCHAR2(50 BYTE),
  VA_SIGNATURE_BLOCK_PRINT_NAME   VARCHAR2(40 BYTE),
  VA_SIGNATURE_BLOCK_TITLE        VARCHAR2(50 BYTE),
  VA_LAST_TRAINING_YEAR           VARCHAR2(4 BYTE),
  VAMC_MAIL_CODE                  VARCHAR2(10 BYTE),
  VA_ROOM_NUMBER                  VARCHAR2(30 BYTE),
  VA_TERMINATION_REASON_TEXT      VARCHAR2(45 BYTE),
  VA_TERMINATION_DATE             DATE,
  DEMOGRAPHIC_UPDATE_EVENT_DATE   DATE,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  RECORD_MODIFIED_SITE_ID         NUMBER(20),
  RECORD_MODIFIED_SRC_NAME        VARCHAR2(30 BYTE),
  VOA_IND                         CHAR(1 BYTE),
  STD_MILITARYSVCQRYSTAT_ID       NUMBER(20),
  DENTAL_CLASS2_IND               VARCHAR2(1 BYTE),
  DENTAL_CLS2_APP_BEF_DUE_DATE    DATE,
  DOD_VERIFIED_IND                VARCHAR2(1 BYTE),
  DEATH_REPORTED_BY               VARCHAR2(30 BYTE),
  MAIL_STOP_REASON                VARCHAR2(200 BYTE),
  STOP_COM_IND                    VARCHAR2(1 BYTE),
  STOP_COM_RSN_TYPE_ID            NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON IS 'The PERSON table is the central table in the ADR database for any information connected to a person of import to the Veterans Health Administration. This table contains data for persons of the following categories: Patient/Beneficiary, IT User, VA Associates, and Practioners. This is the base table of the ADR person-centric model. A record is created in this table for each Person who has been enumerated and assigned a VPID through the Identity Management service. This record is appended with further information about a person, such as demographic or Enrollment and Eligibility attributes and details, by the respective Services. Foreign keys into this table include: VPID_ID Unique and Not Null Foreign Key from PS_PERSON_VPID table USER_ENROLEE_SITE_ID Site where the person is enrolled STD_PROGRAMOFSTUDY_ID Program of study for a VA Employee Nursing, Pharmacy etc. STD_MARITALSTATUS_ID Marital Status STD_RELIGION_ID Religion DEATH_REPORTING_SITE_ID Site Reporting the persons death information CLAIM_FOLDER_LOCATION_SITE_ID Site where the Claim folder is located DEATH_NOTIFICATION_SRC_TYPE_ID Source notifying the death of a person SSA, VBA, DoD etc. VA_ASSOCIATE_TYPE_ID Type of associate EMPLOYMENT_STATUS_TYPE_ID Employment Status PREFERRED_SITE_ID The preferred facility for health care IDENTITY MANAGEMENT / DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PERSON.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON.VPID_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VPID_VALUE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.HEC_IEN IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.STD_PROGRAMOFSTUDY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.STD_RELIGION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.STD_MARITALSTATUS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.USER_ENROLEE_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.PREFERRED_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.DEATH_REPORTING_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.DEATH_NOTIFICATION_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.CLAIM_FOLDER_LOCATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.CLAIM_FOLDER_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.EDUCATIONAL_DEGREE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.PERSON.SENSITIVITY_FLAG_SET_SRC_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.SENSITIVITY_FLAG_SET_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.SENSITIVITY_IND IS 'Patient sensitivity flag from AAC is defined as an indication that this veteran record is a sensitive record and only those with a business purpose should view the related information. ';

COMMENT ON COLUMN ADR.PERSON.SENSITIVITY_FLAG_SET_BY IS 'BR7316 Sensitivity assigned by is defined as the person who set the record to sensitive. ';

COMMENT ON COLUMN ADR.PERSON.SENSITIVITY_FLAG_SET_DATE IS 'BR7318 Date/Time sensitivity flag set is defined as the time stamp associated with the sensitivity flag.';

COMMENT ON COLUMN ADR.PERSON.VETERAN_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.WAS_PRISONER_OF_WAR_IND IS 'BR2376 PH Only Indicator is defined as the indicator that identifies the veterans in the PH Registry file who are not currently in the HEC EDB as enrollees with other eligibility/enrollment data receiving VHA services.  Once the veteran record is created in the EDB then this flag is removed ';

COMMENT ON COLUMN ADR.PERSON.DEATH_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.DEATH_REPORTED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.LAZARUS_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.USER_ENROLEE_THROUGH_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.DISABILITY_DISCHARGE_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.MILITARY_DSBLTY_RETIREMENT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.COMBAT_VET_ELIGIBILITY_END_DT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.APPOINTMENT_WAS_REQUESTED_IND IS 'Indicates if beneficiary was retired from the U.S. Armed Forces due to a disability incurred while serving.  ';

COMMENT ON COLUMN ADR.PERSON.APPOINTMENT_REQUESTED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VA_ASSOCIATE_TITLE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VA_SIGNATURE_BLOCK_PRINT_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VA_SIGNATURE_BLOCK_TITLE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VA_LAST_TRAINING_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VAMC_MAIL_CODE IS 'VA HOSPITAL SERVICE NUMBER, I.E. 116A FOR PSYCHIATRY, 116B FOR PSYCHOLOGY, ETC';

COMMENT ON COLUMN ADR.PERSON.VA_ROOM_NUMBER IS 'This is the room number assigned to the person.';

COMMENT ON COLUMN ADR.PERSON.VA_TERMINATION_REASON_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.VA_TERMINATION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.PERSON.VOA_IND IS 'Veterans Online Application indicator';

COMMENT ON COLUMN ADR.PERSON.DENTAL_CLASS2_IND IS 'Derived from the VBA DD214 Dental Indicator. The DD214 Dental Indicator specifies whether or not the Veteran received dental treatment at service separation. The Dental Indicator is translated from the DD214 Indicator to state whether or not the Veteran is eligible for dental services.
  2  Null means not initialized
  3  0 means `NO¿
  4  1 means `YES¿';

COMMENT ON COLUMN ADR.PERSON.DENTAL_CLS2_APP_BEF_DUE_DATE IS 'Null means not initialized
  2  0 means `NO¿
  3  1 means `YES¿';

COMMENT ON COLUMN ADR.PERSON.MAIL_STOP_REASON IS 'Indicates the reason to halt all USPS mail correspondence for the individual';


CREATE INDEX ADR.PRSN_CLMFLDRNO_NU_I ON ADR.PERSON
(CLAIM_FOLDER_NUMBER)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_ENRLEESITE_FK_I ON ADR.PERSON
(USER_ENROLEE_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PRSN_PK ON ADR.PERSON
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_PSPRSNVPID_FK_I ON ADR.PERSON
(VPID_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_SNSTCHGTYP_FK_I ON ADR.PERSON
(SENSITIVITY_FLAG_SET_SRC_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDINST2_FK_I ON ADR.PERSON
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDINST_FK_I ON ADR.PERSON
(DEATH_REPORTING_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDINST_H_FK_2_I ON ADR.PERSON
(SENSITIVITY_FLAG_SET_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDINST_H_FK_I ON ADR.PERSON
(CLAIM_FOLDER_LOCATION_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDINST_PREF_FAC_FK_I ON ADR.PERSON
(PREFERRED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDMAR_FK_I ON ADR.PERSON
(STD_MARITALSTATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDMSQS_FK_I ON ADR.PERSON
(STD_MILITARYSVCQRYSTAT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDPGMSTDY_FK_I ON ADR.PERSON
(STD_PROGRAMOFSTUDY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STDREL_FK_I ON ADR.PERSON
(STD_RELIGION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_STD_DEATHNOTIFY_FK_I ON ADR.PERSON
(DEATH_NOTIFICATION_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSN_VPIDVALUE_NU_I ON ADR.PERSON
(VPID_VALUE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERSON_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.PERSON
 REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
         insert into person_h
        (
        PERSON_H_ID,
        PERSON_ID,
        VPID_ID,
        HEC_IEN,
        CLAIM_FOLDER_NUMBER,
        EDUCATIONAL_DEGREE,
        REMARKS,
        SENSITIVITY_IND,
        SENSITIVITY_FLAG_SET_BY,
        SENSITIVITY_FLAG_SET_DATE,
        VETERAN_IND,
        DEATH_DATE_TXT,
        DEATH_REPORTED_DATE,
        LAZARUS_DATE_TXT,
        USER_ENROLEE_SITE_ID,
        USER_ENROLEE_THROUGH_YEAR,
        DISABILITY_DISCHARGE_IND,
        COMBAT_VET_ELIGIBILITY_END_DT,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        STD_PROGRAMOFSTUDY_ID,
        STD_MARITALSTATUS_ID,
        STD_RELIGION_ID,
        MILITARY_DSBLTY_RETIREMENT_IND,
        DEATH_REPORTING_SITE_ID,
        CLAIM_FOLDER_LOCATION_SITE_ID,
        DEATH_NOTIFICATION_SRC_TYPE_ID,
        VA_ASSOCIATE_TITLE,
        VA_SIGNATURE_BLOCK_PRINT_NAME,
        VA_SIGNATURE_BLOCK_TITLE,
        VA_LAST_TRAINING_YEAR,
        VAMC_MAIL_CODE,
        VA_ROOM_NUMBER,
        VA_TERMINATION_REASON_TEXT,
        VA_TERMINATION_DATE,
        RECORD_MODIFIED_COUNT,
        VPID_VALUE,
        PREFERRED_SITE_ID,
        WAS_PRISONER_OF_WAR_IND,
        APPOINTMENT_WAS_REQUESTED_IND,
        APPOINTMENT_REQUESTED_DATE,
        SENSITIVITY_FLAG_SET_SRC_ID,
        SENSITIVITY_FLAG_SET_SITE_ID,
        DEMOGRAPHIC_UPDATE_EVENT_DATE,
        EE_MIGRATED_IND,
        RECORD_MODIFIED_SITE_ID,
        RECORD_MODIFIED_SRC_NAME,
        TRANSACTION_TYPE_CODE,
        DENTAL_CLASS2_IND,
        DENTAL_CLS2_APP_BEF_DUE_DATE,
        HISTORY_CREATED_DATE,
        STD_MILITARYSVCQRYSTAT_ID,
        VOA_IND,
        DOD_VERIFIED_IND,
        DEATH_REPORTED_BY
        )
        values
        (
        person_h_s.nextval  ,
        :old.PERSON_ID,
        :old.VPID_ID,
        :old.HEC_IEN,
        :old.CLAIM_FOLDER_NUMBER,
        :old.EDUCATIONAL_DEGREE,
        :old.REMARKS,
        :old.SENSITIVITY_IND,
        :old.SENSITIVITY_FLAG_SET_BY,
        :old.SENSITIVITY_FLAG_SET_DATE,
        :old.VETERAN_IND,
        :old.DEATH_DATE_TXT,
        :old.DEATH_REPORTED_DATE,
        :old.LAZARUS_DATE_TXT,
        :old.USER_ENROLEE_SITE_ID,
        :old.USER_ENROLEE_THROUGH_YEAR,
        :old.DISABILITY_DISCHARGE_IND,
        :old.COMBAT_VET_ELIGIBILITY_END_DT,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.STD_PROGRAMOFSTUDY_ID,
        :old.STD_MARITALSTATUS_ID,
        :old.STD_RELIGION_ID,
        :old.MILITARY_DSBLTY_RETIREMENT_IND,
        :old.DEATH_REPORTING_SITE_ID,
        :old.CLAIM_FOLDER_LOCATION_SITE_ID,
        :old.DEATH_NOTIFICATION_SRC_TYPE_ID,
        :old.VA_ASSOCIATE_TITLE,
        :old.VA_SIGNATURE_BLOCK_PRINT_NAME,
        :old.VA_SIGNATURE_BLOCK_TITLE,
        :old.VA_LAST_TRAINING_YEAR,
        :old.VAMC_MAIL_CODE,
        :old.VA_ROOM_NUMBER,
        :old.VA_TERMINATION_REASON_TEXT,
        :old.VA_TERMINATION_DATE,
        :old.RECORD_MODIFIED_COUNT,
        :old.VPID_VALUE,
        :old.PREFERRED_SITE_ID,
        :old.WAS_PRISONER_OF_WAR_IND,
        :old.APPOINTMENT_WAS_REQUESTED_IND,
        :old.APPOINTMENT_REQUESTED_DATE,
        :old.SENSITIVITY_FLAG_SET_SRC_ID,
        :old.SENSITIVITY_FLAG_SET_SITE_ID,
        :old.DEMOGRAPHIC_UPDATE_EVENT_DATE,
        :old.EE_MIGRATED_IND,
        :old.RECORD_MODIFIED_SITE_ID,
        :old.RECORD_MODIFIED_SRC_NAME,
                'D',
        :old.DENTAL_CLASS2_IND,
        :old.DENTAL_CLS2_APP_BEF_DUE_DATE,
        SYSDATE,
        :old.STD_MILITARYSVCQRYSTAT_ID,
        :old.VOA_IND,
        :old.DOD_VERIFIED_IND,
        :old.DEATH_REPORTED_BY
        );

    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

         insert into person_h
        (
        PERSON_H_ID,
        PERSON_ID,
        VPID_ID,
        HEC_IEN,
        CLAIM_FOLDER_NUMBER,
        EDUCATIONAL_DEGREE,
        REMARKS,
        SENSITIVITY_IND,
        SENSITIVITY_FLAG_SET_BY,
        SENSITIVITY_FLAG_SET_DATE,
        VETERAN_IND,
        DEATH_DATE_TXT,
        DEATH_REPORTED_DATE,
        LAZARUS_DATE_TXT,
        USER_ENROLEE_SITE_ID,
        USER_ENROLEE_THROUGH_YEAR,
        DISABILITY_DISCHARGE_IND,
        COMBAT_VET_ELIGIBILITY_END_DT,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        STD_PROGRAMOFSTUDY_ID,
        STD_MARITALSTATUS_ID,
        STD_RELIGION_ID,
        MILITARY_DSBLTY_RETIREMENT_IND,
        DEATH_REPORTING_SITE_ID,
        CLAIM_FOLDER_LOCATION_SITE_ID,
        DEATH_NOTIFICATION_SRC_TYPE_ID,
        VA_ASSOCIATE_TITLE,
        VA_SIGNATURE_BLOCK_PRINT_NAME,
        VA_SIGNATURE_BLOCK_TITLE,
        VA_LAST_TRAINING_YEAR,
        VAMC_MAIL_CODE,
        VA_ROOM_NUMBER,
        VA_TERMINATION_REASON_TEXT,
        VA_TERMINATION_DATE,
        RECORD_MODIFIED_COUNT,
        VPID_VALUE,
        PREFERRED_SITE_ID,
        WAS_PRISONER_OF_WAR_IND,
        APPOINTMENT_WAS_REQUESTED_IND,
        APPOINTMENT_REQUESTED_DATE,
        SENSITIVITY_FLAG_SET_SRC_ID,
        SENSITIVITY_FLAG_SET_SITE_ID,
        DEMOGRAPHIC_UPDATE_EVENT_DATE,
        EE_MIGRATED_IND,
        RECORD_MODIFIED_SITE_ID,
        RECORD_MODIFIED_SRC_NAME,
        TRANSACTION_TYPE_CODE,
        DENTAL_CLASS2_IND,
        DENTAL_CLS2_APP_BEF_DUE_DATE,
        HISTORY_CREATED_DATE,
        STD_MILITARYSVCQRYSTAT_ID,
        VOA_IND,
        DOD_VERIFIED_IND,
        DEATH_REPORTED_BY
        )
        values
        (
        person_h_s.nextval  ,
        :new.PERSON_ID,
        :new.VPID_ID,
        :new.HEC_IEN,
        :new.CLAIM_FOLDER_NUMBER,
        :new.EDUCATIONAL_DEGREE,
        :new.REMARKS,
        :new.SENSITIVITY_IND,
        :new.SENSITIVITY_FLAG_SET_BY,
        :new.SENSITIVITY_FLAG_SET_DATE,
        :new.VETERAN_IND,
        :new.DEATH_DATE_TXT,
        :new.DEATH_REPORTED_DATE,
        :new.LAZARUS_DATE_TXT,
        :new.USER_ENROLEE_SITE_ID,
        :new.USER_ENROLEE_THROUGH_YEAR,
        :new.DISABILITY_DISCHARGE_IND,
        :new.COMBAT_VET_ELIGIBILITY_END_DT,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.STD_PROGRAMOFSTUDY_ID,
        :new.STD_MARITALSTATUS_ID,
        :new.STD_RELIGION_ID,
        :new.MILITARY_DSBLTY_RETIREMENT_IND,
        :new.DEATH_REPORTING_SITE_ID,
        :new.CLAIM_FOLDER_LOCATION_SITE_ID,
        :new.DEATH_NOTIFICATION_SRC_TYPE_ID,
        :new.VA_ASSOCIATE_TITLE,
        :new.VA_SIGNATURE_BLOCK_PRINT_NAME,
        :new.VA_SIGNATURE_BLOCK_TITLE,
        :new.VA_LAST_TRAINING_YEAR,
        :new.VAMC_MAIL_CODE,
        :new.VA_ROOM_NUMBER,
        :new.VA_TERMINATION_REASON_TEXT,
        :new.VA_TERMINATION_DATE,
        :new.RECORD_MODIFIED_COUNT,
        :new.VPID_VALUE,
        :new.PREFERRED_SITE_ID,
        :new.WAS_PRISONER_OF_WAR_IND,
        :new.APPOINTMENT_WAS_REQUESTED_IND,
        :new.APPOINTMENT_REQUESTED_DATE,
        :new.SENSITIVITY_FLAG_SET_SRC_ID,
        :new.SENSITIVITY_FLAG_SET_SITE_ID,
        :new.DEMOGRAPHIC_UPDATE_EVENT_DATE,
        :new.EE_MIGRATED_IND,
        :new.RECORD_MODIFIED_SITE_ID,
        :new.RECORD_MODIFIED_SRC_NAME,
                mDML,
        :new.DENTAL_CLASS2_IND,
        :new.DENTAL_CLS2_APP_BEF_DUE_DATE,
        SYSDATE,
        :new.STD_MILITARYSVCQRYSTAT_ID,
        :new.VOA_IND,
        :new.DOD_VERIFIED_IND,
        :new.DEATH_REPORTED_BY

        );

    end if;

End person_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON FOR ADR.PERSON;


ALTER TABLE ADR.PERSON ADD (
  CONSTRAINT SYS_C0066484
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066486
  CHECK ("VPID_VALUE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066487
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066488
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066489
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066490
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066491
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSN_PK
  PRIMARY KEY
  (PERSON_ID)
  USING INDEX ADR.PRSN_PK
  ENABLE VALIDATE,
  CONSTRAINT PRSN_U_VPID_UK
  UNIQUE (VPID_ID)
  USING INDEX ADR.PRSN_PSPRSNVPID_FK_I
  ENABLE NOVALIDATE,
  CONSTRAINT PRSN_VPIDVALUE_UK
  UNIQUE (VPID_VALUE)
  USING INDEX ADR.PRSN_VPIDVALUE_NU_I
  ENABLE NOVALIDATE);

ALTER TABLE ADR.PERSON ADD (
  CONSTRAINT STOP_COM_RSN_TYPE_ID_FK 
  FOREIGN KEY (STOP_COM_RSN_TYPE_ID) 
  REFERENCES ADR.STOP_COM_RSN_TYPE (STOP_COM_RSN_TYPE_ID)
  ENABLE VALIDATE);

GRANT REFERENCES ON ADR.PERSON TO ADRAAP;

GRANT SELECT ON ADR.PERSON TO ADREXT;

GRANT SELECT ON ADR.PERSON TO ADRTMP;

GRANT SELECT ON ADR.PERSON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON TO ADR_RW;

GRANT SELECT ON ADR.PERSON TO BIZFLOW_R;

GRANT SELECT ON ADR.PERSON TO CARUSR_R;

GRANT SELECT ON ADR.PERSON TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.PERSON TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON TO ES_RW;

GRANT SELECT ON ADR.PERSON TO PSIM;

GRANT SELECT ON ADR.PERSON TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PERSON TO PSIM_ADR_RW;

GRANT SELECT ON ADR.PERSON TO DNS   STALLP;

GRANT REFERENCES ON ADR.PERSON TO VOA;
ALTER TABLE ADR.PERSON_CHANGE_LOG
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_CHANGE_LOG CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_CHANGE_LOG
(
  PERSON_CHANGE_LOG_ID   NUMBER(20),
  PERSON_ID              NUMBER(20),
  INCOME_YEAR            NUMBER(4),
  SUBMITTER_DESCRIPTION  VARCHAR2(250 BYTE),
  SUBMITTED_DATE         DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE    DATE,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE   DATE,
  RECORD_MODIFIED_COUNT  NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_CHANGE_LOG IS 'This is a process related table - whenever there is merge or unmerge of a Beneficiary record, a log of all changes made to the record is stored here.';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.PERSON_CHANGE_LOG_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.SUBMITTER_DESCRIPTION IS 'Audit Field - User or Process creating the record in database';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.SUBMITTED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_CHANGE_LOG.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.PRSNCHGLOG_DESC_DT_NU_I ON ADR.PERSON_CHANGE_LOG
(SUBMITTER_DESCRIPTION, SUBMITTED_DATE, INCOME_YEAR, PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PRSNCHGLOG_PK ON ADR.PERSON_CHANGE_LOG
(PERSON_CHANGE_LOG_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNCHGLOG_PRSN_FK_I ON ADR.PERSON_CHANGE_LOG
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_CHANGE_LOG FOR ADR.PERSON_CHANGE_LOG;


ALTER TABLE ADR.PERSON_CHANGE_LOG ADD (
  CONSTRAINT SYS_C00140209
  CHECK ("PERSON_CHANGE_LOG_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140210
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140211
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140212
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140213
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140214
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00140215
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNCHGLOG_PK
  PRIMARY KEY
  (PERSON_CHANGE_LOG_ID)
  USING INDEX ADR.PRSNCHGLOG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_CHANGE_LOG ADD (
  CONSTRAINT PRSNCHGLOG_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_CHANGE_LOG TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_CHANGE_LOG TO ADR_RW;

GRANT SELECT ON ADR.PERSON_CHANGE_LOG TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_CHANGE_LOG TO ES_RW;
ALTER TABLE ADR.PERSON_CONTACT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_CONTACT CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_CONTACT
(
  PERSON_CONTACT_ID            NUMBER(20),
  PERSON_ID                    NUMBER(20),
  PERSON_CONTACT_TYPE_ID       NUMBER(20),
  CONTACT_RELATIONSHIP_TEXT    VARCHAR2(250 BYTE),
  ORGANIZATION_NAME            VARCHAR2(100 BYTE),
  PREFIX                       VARCHAR2(12 BYTE),
  FIRST_NAME                   VARCHAR2(30 BYTE),
  MIDDLE_NAME                  VARCHAR2(30 BYTE),
  LAST_NAME                    VARCHAR2(40 BYTE),
  SUFFIX                       VARCHAR2(12 BYTE),
  ADDRESS_LINE1                VARCHAR2(100 BYTE),
  ADDRESS_LINE2                VARCHAR2(100 BYTE),
  ADDRESS_LINE3                VARCHAR2(100 BYTE),
  CITY                         VARCHAR2(60 BYTE),
  STATE_CODE                   VARCHAR2(50 BYTE),
  COUNTY_CODE                  VARCHAR2(50 BYTE),
  PROVINCE_CODE                VARCHAR2(50 BYTE),
  ZIP_CODE                     VARCHAR2(20 BYTE),
  ZIP_PLUS_4                   VARCHAR2(10 BYTE),
  POSTAL_CODE                  VARCHAR2(20 BYTE),
  COUNTRY_CODE                 VARCHAR2(50 BYTE),
  HOME_PHONE                   VARCHAR2(30 BYTE),
  HOME_PHONE_UPDATE_DATE       DATE,
  WORK_PHONE                   VARCHAR2(30 BYTE),
  WORK_PHONE_UPDATE_DATE       DATE,
  EMAIL_ADDRESS                VARCHAR2(128 BYTE),
  CONTACT_DATA_LAST_EDIT_DATE  DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE),
  DEACTIVATION_DATE            DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_CONTACT IS 'The table stores information (address, phone, and email) about contacts associated with persons of import to the VHA. Types of contacts include Next of Kin, Guardian (VA and civilian), and Power of Attorney. These contact persons have not been enumerated through the Identity Management process. As a result they have no VPIDs assigned to their records. The address and telephone information for these contact persons is denormalized in this table. PERSON_ID Foreign key to the PERSON table. PERSON_CONTACT_TYPE_ID Foreign key to the PERSON_CONTACT_TYPE table. DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PERSON_CONTACT.PERSON_CONTACT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.PERSON_CONTACT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.CONTACT_RELATIONSHIP_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ORGANIZATION_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.FIRST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.MIDDLE_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.LAST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.CITY IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5- character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied. State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.PERSON_CONTACT.COUNTY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.PROVINCE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.ZIP_PLUS_4 IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.POSTAL_CODE IS 'BR17 Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.COUNTRY_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.HOME_PHONE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.HOME_PHONE_UPDATE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.WORK_PHONE IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.WORK_PHONE_UPDATE_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.PERSON_CONTACT.EMAIL_ADDRESS IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.CONTACT_DATA_LAST_EDIT_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_CONTACT.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNCTAC_PK ON ADR.PERSON_CONTACT
(PERSON_CONTACT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNCTAC_PRSN_FK_I ON ADR.PERSON_CONTACT
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNCTAC_STDINST2_FK_I ON ADR.PERSON_CONTACT
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNCTAC_STD_PRNSCTAC_FK_I ON ADR.PERSON_CONTACT
(PERSON_CONTACT_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERSON_CONTACT_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."PERSON_CONTACT" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PERSON_CONTACT_H
		(
		PERSON_CONTACT_H_ID,
		PERSON_CONTACT_ID,
		PERSON_ID,
		PERSON_CONTACT_TYPE_ID,
		CONTACT_RELATIONSHIP_TEXT,
		ORGANIZATION_NAME,
		PREFIX,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		SUFFIX,
		ADDRESS_LINE1,
		ADDRESS_LINE2,
		ADDRESS_LINE3,
		CITY,
		STATE_CODE,
		COUNTY_CODE,
		PROVINCE_CODE,
		POSTAL_CODE,
		ZIP_PLUS_4,
		COUNTRY_CODE,
		HOME_PHONE,
		WORK_PHONE,
		EMAIL_ADDRESS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		HOME_PHONE_UPDATE_DATE,
		WORK_PHONE_UPDATE_DATE,
		ZIP_CODE,
		CONTACT_DATA_LAST_EDIT_DATE,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		deactivation_date,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERSON_CONTACT_H_S.nextval,
		:old.PERSON_CONTACT_ID,
		:old.PERSON_ID,
		:old.PERSON_CONTACT_TYPE_ID,
		:old.CONTACT_RELATIONSHIP_TEXT,
		:old.ORGANIZATION_NAME,
		:old.PREFIX,
		:old.FIRST_NAME,
		:old.MIDDLE_NAME,
		:old.LAST_NAME,
		:old.SUFFIX,
		:old.ADDRESS_LINE1,
		:old.ADDRESS_LINE2,
		:old.ADDRESS_LINE3,
		:old.CITY,
		:old.STATE_CODE,
		:old.COUNTY_CODE,
		:old.PROVINCE_CODE,
		:old.POSTAL_CODE,
		:old.ZIP_PLUS_4,
		:old.COUNTRY_CODE,
		:old.HOME_PHONE,
		:old.WORK_PHONE,
		:old.EMAIL_ADDRESS,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		:old.HOME_PHONE_UPDATE_DATE,
		:old.WORK_PHONE_UPDATE_DATE,
		:old.ZIP_CODE,
		:old.CONTACT_DATA_LAST_EDIT_DATE,
		:old.RECORD_MODIFIED_SITE_ID,
		:old.RECORD_MODIFIED_SRC_NAME,
		:old.deactivation_date,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PERSON_CONTACT_H
		(
		PERSON_CONTACT_H_ID,
		PERSON_CONTACT_ID,
		PERSON_ID,
		PERSON_CONTACT_TYPE_ID,
		CONTACT_RELATIONSHIP_TEXT,
		ORGANIZATION_NAME,
		PREFIX,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		SUFFIX,
		ADDRESS_LINE1,
		ADDRESS_LINE2,
		ADDRESS_LINE3,
		CITY,
		STATE_CODE,
		COUNTY_CODE,
		PROVINCE_CODE,
		POSTAL_CODE,
		ZIP_PLUS_4,
		COUNTRY_CODE,
		HOME_PHONE,
		WORK_PHONE,
		EMAIL_ADDRESS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		HOME_PHONE_UPDATE_DATE,
		WORK_PHONE_UPDATE_DATE,
		ZIP_CODE,
		CONTACT_DATA_LAST_EDIT_DATE,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		deactivation_date,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERSON_CONTACT_H_S.nextval,
		:new.PERSON_CONTACT_ID,
		:new.PERSON_ID,
		:new.PERSON_CONTACT_TYPE_ID,
		:new.CONTACT_RELATIONSHIP_TEXT,
		:new.ORGANIZATION_NAME,
		:new.PREFIX,
		:new.FIRST_NAME,
		:new.MIDDLE_NAME,
		:new.LAST_NAME,
		:new.SUFFIX,
		:new.ADDRESS_LINE1,
		:new.ADDRESS_LINE2,
		:new.ADDRESS_LINE3,
		:new.CITY,
		:new.STATE_CODE,
		:new.COUNTY_CODE,
		:new.PROVINCE_CODE,
		:new.POSTAL_CODE,
		:new.ZIP_PLUS_4,
		:new.COUNTRY_CODE,
		:new.HOME_PHONE,
		:new.WORK_PHONE,
		:new.EMAIL_ADDRESS,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		:new.HOME_PHONE_UPDATE_DATE,
		:new.WORK_PHONE_UPDATE_DATE,
		:new.ZIP_CODE,
		:new.CONTACT_DATA_LAST_EDIT_DATE,
		:new.RECORD_MODIFIED_SITE_ID,
		:new.RECORD_MODIFIED_SRC_NAME,
		:new.deactivation_date,
		mDML
		);
	end if;

end PERSON_CONTACT_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON_CONTACT FOR ADR.PERSON_CONTACT;


ALTER TABLE ADR.PERSON_CONTACT ADD (
  CONSTRAINT SYS_C0067210
  CHECK ("PERSON_CONTACT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067211
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067212
  CHECK ("PERSON_CONTACT_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067213
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067214
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067215
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067216
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067217
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNCTAC_PK
  PRIMARY KEY
  (PERSON_CONTACT_ID)
  USING INDEX ADR.PRSNCTAC_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_CONTACT ADD (
  CONSTRAINT PRSNCTAC_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_CONTACT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_CONTACT TO ADR_RW;

GRANT SELECT ON ADR.PERSON_CONTACT TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_CONTACT TO ES_RW;

GRANT SELECT ON ADR.PERSON_CONTACT TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PERSON_CONTACT TO PSIM_ADR_RW;
ALTER TABLE ADR.PERSON_CONTACT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_CONTACT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_CONTACT_H
(
  PERSON_CONTACT_H_ID          NUMBER(20),
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  PERSON_CONTACT_ID            NUMBER(20),
  PERSON_ID                    NUMBER(20),
  PERSON_CONTACT_TYPE_ID       NUMBER(20),
  CONTACT_RELATIONSHIP_TEXT    VARCHAR2(250 BYTE),
  ORGANIZATION_NAME            VARCHAR2(100 BYTE),
  PREFIX                       VARCHAR2(12 BYTE),
  FIRST_NAME                   VARCHAR2(30 BYTE),
  MIDDLE_NAME                  VARCHAR2(30 BYTE),
  LAST_NAME                    VARCHAR2(40 BYTE),
  SUFFIX                       VARCHAR2(12 BYTE),
  ADDRESS_LINE1                VARCHAR2(100 BYTE),
  ADDRESS_LINE2                VARCHAR2(100 BYTE),
  ADDRESS_LINE3                VARCHAR2(100 BYTE),
  CITY                         VARCHAR2(60 BYTE),
  STATE_CODE                   VARCHAR2(50 BYTE),
  COUNTY_CODE                  VARCHAR2(50 BYTE),
  PROVINCE_CODE                VARCHAR2(50 BYTE),
  POSTAL_CODE                  VARCHAR2(20 BYTE),
  ZIP_CODE                     VARCHAR2(20 BYTE),
  ZIP_PLUS_4                   VARCHAR2(10 BYTE),
  COUNTRY_CODE                 VARCHAR2(50 BYTE),
  EMAIL_ADDRESS                VARCHAR2(128 BYTE),
  HOME_PHONE                   VARCHAR2(30 BYTE),
  HOME_PHONE_UPDATE_DATE       DATE,
  WORK_PHONE                   VARCHAR2(30 BYTE),
  WORK_PHONE_UPDATE_DATE       DATE,
  CONTACT_DATA_LAST_EDIT_DATE  DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE),
  DEACTIVATION_DATE            DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_CONTACT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PERSON_CONTACT_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PERSON_CONTACT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PERSON_CONTACT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.CONTACT_RELATIONSHIP_TEXT IS 'Stores a free text description of the nature of the association between the Person and the Contact';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ORGANIZATION_NAME IS 'BR947 Organization name is defined as the name of the organization.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.CITY IS 'City is defined as the name of a city used for an address.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.STATE_CODE IS 'FIPS STATE CODE is defined as the 2 characters FIPS State code. The FIPS State code is the first two characters of the 5- character FIPS code. The FIPS state code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is Required, it is associated with the Facility address in which the primary MT was supplied. State code is defines as the unique country code / state abbreviation combination found in the database that best identifies the state ';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.COUNTY_CODE IS 'FIPS County CODE is defined as the last 3 characters of the FIPS state/county code.  The FIPS county code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied 5-DIGIT FIPS CODE is defined as the 2 digit state code + 3 digit county code. This 5 digit FIPS code is associated with the veterans permanent mailing address, If no permanent mailing address is supplied, and a MT is required, it is associated with the Facility address  in which the primary MT was supplied';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.PROVINCE_CODE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.POSTAL_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ZIP_CODE IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.ZIP_PLUS_4 IS 'Postal code is defined as the mail code used for mail delivery to a country other than the USA only.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.COUNTRY_CODE IS 'Country is defined as the unique identifier assigned by the International Organization of Standardization for a country.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.EMAIL_ADDRESS IS 'The email address for the person or insurance company.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.HOME_PHONE IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.HOME_PHONE_UPDATE_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.WORK_PHONE IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.WORK_PHONE_UPDATE_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_CONTACT_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNCTACH_PK ON ADR.PERSON_CONTACT_H
(PERSON_CONTACT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNCTACH_PRSNID_NU_I ON ADR.PERSON_CONTACT_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_CONTACT_H FOR ADR.PERSON_CONTACT_H;


ALTER TABLE ADR.PERSON_CONTACT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_020
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066291
  CHECK ("PERSON_CONTACT_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066292
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066293
  CHECK ("PERSON_CONTACT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066294
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066295
  CHECK ("PERSON_CONTACT_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066296
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066297
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066298
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066299
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066300
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNCTACH_PK
  PRIMARY KEY
  (PERSON_CONTACT_H_ID)
  USING INDEX ADR.PRSNCTACH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_CONTACT_H TO ADR_R;

GRANT SELECT ON ADR.PERSON_CONTACT_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_CONTACT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_CONTACT_H TO ES_RW;

GRANT SELECT ON ADR.PERSON_CONTACT_H TO PSIM_ADR_R;
ALTER TABLE ADR.PERSON_ETHNICITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_ETHNICITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_ETHNICITY
(
  PERSON_ETHNICITY_ID        NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  STD_ETHNICITY_ID           NUMBER(20)         NOT NULL,
  COLLECTION_METHOD_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  RECORD_MODIFIED_SITE_ID    NUMBER(20),
  RECORD_MODIFIED_SRC_NAME   VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_ETHNICITY IS 'The table stores information a persons reported ethnic group and the method by which that information was reported. DEMOGRAPHICS';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.PERSON_ETHNICITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.STD_ETHNICITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.COLLECTION_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNETNCTY_PK ON ADR.PERSON_ETHNICITY
(PERSON_ETHNICITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNETNCTY_PRSN_FK_I ON ADR.PERSON_ETHNICITY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNETNCTY_STDETH_FK_I ON ADR.PERSON_ETHNICITY
(STD_ETHNICITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNETNCTY_STDINST2_FK_I ON ADR.PERSON_ETHNICITY
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNETNCTY_STD_CLLNMTHD_FK_I ON ADR.PERSON_ETHNICITY
(COLLECTION_METHOD_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERSON_ETHNICITY_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.PERSON_ETHNICITY for each row
declare
    mDML char(1);
begin

  if DELETING then
    insert into PERSON_ETHNICITY_H
    (
    PERSON_ETHNICITY_H_ID,
    PERSON_ETHNICITY_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    STD_ETHNICITY_ID,
    COLLECTION_METHOD_TYPE_ID,
    PERSON_ID,
    RECORD_MODIFIED_COUNT,
    RECORD_MODIFIED_SITE_ID,
    RECORD_MODIFIED_SRC_NAME,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PERSON_ETHNICITY_H_S.nextval,
    :old.PERSON_ETHNICITY_ID,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY,
    :old.RECORD_MODIFIED_DATE,
    :old.STD_ETHNICITY_ID,
    :old.COLLECTION_METHOD_TYPE_ID,
    :old.PERSON_ID,
    :old.RECORD_MODIFIED_COUNT,
    :old.RECORD_MODIFIED_SITE_ID,
    :old.RECORD_MODIFIED_SRC_NAME,
    'D'
    );

  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

    insert into PERSON_ETHNICITY_H
    (
    PERSON_ETHNICITY_H_ID,
    PERSON_ETHNICITY_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    STD_ETHNICITY_ID,
    COLLECTION_METHOD_TYPE_ID,
    PERSON_ID,
    RECORD_MODIFIED_COUNT,
    RECORD_MODIFIED_SITE_ID,
    RECORD_MODIFIED_SRC_NAME,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PERSON_ETHNICITY_H_S.nextval,
    :new.PERSON_ETHNICITY_ID,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY,
    :new.RECORD_MODIFIED_DATE,
    :new.STD_ETHNICITY_ID,
    :new.COLLECTION_METHOD_TYPE_ID,
    :new.PERSON_ID,
    :new.RECORD_MODIFIED_COUNT,
    :new.RECORD_MODIFIED_SITE_ID,
    :new.RECORD_MODIFIED_SRC_NAME,
    mDML
    );

  end if;

end PERSON_ETHNICITY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON_ETHNICITY FOR ADR.PERSON_ETHNICITY;


ALTER TABLE ADR.PERSON_ETHNICITY ADD (
  CONSTRAINT PRSNETNCTY_PK
  PRIMARY KEY
  (PERSON_ETHNICITY_ID)
  USING INDEX ADR.PRSNETNCTY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_ETHNICITY ADD (
  CONSTRAINT PRSNETNCTY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_ETHNICITY TO ADREXT;

GRANT SELECT ON ADR.PERSON_ETHNICITY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_ETHNICITY TO ADR_RW;

GRANT SELECT ON ADR.PERSON_ETHNICITY TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_ETHNICITY TO ES_RW;

GRANT SELECT ON ADR.PERSON_ETHNICITY TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PERSON_ETHNICITY TO PSIM_ADR_RW;
ALTER TABLE ADR.PERSON_ETHNICITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_ETHNICITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_ETHNICITY_H
(
  PERSON_ETHNICITY_H_ID      NUMBER(20),
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE),
  PERSON_ETHNICITY_ID        NUMBER(20),
  PERSON_ID                  NUMBER(20),
  STD_ETHNICITY_ID           NUMBER(20),
  COLLECTION_METHOD_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE       DATE,
  RECORD_MODIFIED_COUNT      NUMBER(10),
  RECORD_MODIFIED_SITE_ID    NUMBER(20),
  RECORD_MODIFIED_SRC_NAME   VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_ETHNICITY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.PERSON_ETHNICITY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.PERSON_ETHNICITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.STD_ETHNICITY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.COLLECTION_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_ETHNICITY_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNETNCTH_PK ON ADR.PERSON_ETHNICITY_H
(PERSON_ETHNICITY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNETNCTH_PRSNID_NU_I ON ADR.PERSON_ETHNICITY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_ETHNICITY_H FOR ADR.PERSON_ETHNICITY_H;


ALTER TABLE ADR.PERSON_ETHNICITY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_003
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065931
  CHECK ("PERSON_ETHNICITY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065932
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065933
  CHECK ("PERSON_ETHNICITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065934
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065935
  CHECK ("STD_ETHNICITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065936
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065937
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065938
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065939
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065940
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNETNCTH_PK
  PRIMARY KEY
  (PERSON_ETHNICITY_H_ID)
  USING INDEX ADR.PRSNETNCTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_ETHNICITY_H TO ADREXT;

GRANT SELECT ON ADR.PERSON_ETHNICITY_H TO ADR_R;

GRANT SELECT ON ADR.PERSON_ETHNICITY_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_ETHNICITY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_ETHNICITY_H TO ES_RW;

GRANT SELECT ON ADR.PERSON_ETHNICITY_H TO PSIM_ADR_R;
ALTER TABLE ADR.PERSON_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_H
(
  PERSON_H_ID                     NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  PERSON_ID                       NUMBER(20),
  VPID_ID                         NUMBER(20),
  VPID_VALUE                      VARCHAR2(30 BYTE),
  HEC_IEN                         VARCHAR2(20 BYTE),
  CLAIM_FOLDER_NUMBER             VARCHAR2(20 BYTE),
  EDUCATIONAL_DEGREE              VARCHAR2(30 BYTE),
  REMARKS                         VARCHAR2(4000 BYTE),
  SENSITIVITY_IND                 VARCHAR2(1 BYTE) DEFAULT '0',
  SENSITIVITY_FLAG_SET_BY         VARCHAR2(30 BYTE),
  SENSITIVITY_FLAG_SET_DATE       DATE,
  VETERAN_IND                     VARCHAR2(1 BYTE) DEFAULT '0',
  DEATH_DATE_TXT                  VARCHAR2(14 BYTE),
  DEATH_REPORTED_DATE             DATE,
  LAZARUS_DATE_TXT                VARCHAR2(14 BYTE),
  USER_ENROLEE_SITE_ID            NUMBER(20),
  USER_ENROLEE_THROUGH_YEAR       NUMBER(4),
  DISABILITY_DISCHARGE_IND        VARCHAR2(1 BYTE),
  COMBAT_VET_ELIGIBILITY_END_DT   DATE,
  STD_PROGRAMOFSTUDY_ID           NUMBER(20),
  STD_MARITALSTATUS_ID            NUMBER(20),
  STD_RELIGION_ID                 NUMBER(20),
  MILITARY_DSBLTY_RETIREMENT_IND  VARCHAR2(1 BYTE),
  DEATH_REPORTING_SITE_ID         NUMBER(20),
  CLAIM_FOLDER_LOCATION_SITE_ID   NUMBER(20),
  DEATH_NOTIFICATION_SRC_TYPE_ID  NUMBER(20),
  VA_ASSOCIATE_TITLE              VARCHAR2(50 BYTE),
  VA_SIGNATURE_BLOCK_PRINT_NAME   VARCHAR2(40 BYTE),
  VA_SIGNATURE_BLOCK_TITLE        VARCHAR2(50 BYTE),
  VA_LAST_TRAINING_YEAR           VARCHAR2(4 BYTE),
  VAMC_MAIL_CODE                  VARCHAR2(10 BYTE),
  VA_ROOM_NUMBER                  VARCHAR2(30 BYTE),
  VA_TERMINATION_REASON_TEXT      VARCHAR2(45 BYTE),
  VA_TERMINATION_DATE             DATE,
  DEMOGRAPHIC_UPDATE_EVENT_DATE   DATE,
  PREFERRED_SITE_ID               NUMBER(20),
  WAS_PRISONER_OF_WAR_IND         VARCHAR2(1 BYTE),
  EE_MIGRATED_IND                 VARCHAR2(1 BYTE),
  APPOINTMENT_WAS_REQUESTED_IND   VARCHAR2(1 BYTE),
  APPOINTMENT_REQUESTED_DATE      DATE,
  SENSITIVITY_FLAG_SET_SRC_ID     NUMBER(20),
  SENSITIVITY_FLAG_SET_SITE_ID    NUMBER(20),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  RECORD_MODIFIED_SITE_ID         NUMBER(20),
  RECORD_MODIFIED_SRC_NAME        VARCHAR2(30 BYTE),
  VOA_IND                         CHAR(1 BYTE),
  STD_MILITARYSVCQRYSTAT_ID       NUMBER(20),
  DENTAL_CLASS2_IND               VARCHAR2(1 BYTE),
  DENTAL_CLS2_APP_BEF_DUE_DATE    DATE,
  HISTORY_CREATED_DATE            DATE,
  DOD_VERIFIED_IND                VARCHAR2(1 BYTE),
  DEATH_REPORTED_BY               VARCHAR2(30 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.PERSON_H.PERSON_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERSON_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_H.VPID_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VPID_VALUE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.HEC_IEN IS 'Unique identifier used in the legacy HEC system. Records from the ADR, the HEC Legacy system and also the current IVM system can be tied together using this number.';

COMMENT ON COLUMN ADR.PERSON_H.CLAIM_FOLDER_NUMBER IS 'Claim folder number as supplied by VBA. Usually the same as the persons social security number.';

COMMENT ON COLUMN ADR.PERSON_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.PERSON_H.SENSITIVITY_IND IS 'Patient sensitivity flag from AAC is defined as an indication that this veteran record is a sensitive record and only those with a business purpose should view the related information. ';

COMMENT ON COLUMN ADR.PERSON_H.SENSITIVITY_FLAG_SET_BY IS 'BR7316 Sensitivity assigned by is defined as the person who set the record to sensitive. ';

COMMENT ON COLUMN ADR.PERSON_H.SENSITIVITY_FLAG_SET_DATE IS 'BR7318 Date/Time sensitivity flag set is defined as the time stamp associated with the sensitivity flag.';

COMMENT ON COLUMN ADR.PERSON_H.VETERAN_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.USER_ENROLEE_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.USER_ENROLEE_THROUGH_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.DISABILITY_DISCHARGE_IND IS 'Indicates if beneficiary was retired from the U.S. Armed Forces due to a disability incurred while serving.  ';

COMMENT ON COLUMN ADR.PERSON_H.STD_PROGRAMOFSTUDY_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.STD_MARITALSTATUS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.STD_RELIGION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.MILITARY_DSBLTY_RETIREMENT_IND IS 'Indicates if beneficiary is receiving Military Disability';

COMMENT ON COLUMN ADR.PERSON_H.DEATH_REPORTING_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.CLAIM_FOLDER_LOCATION_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.DEATH_NOTIFICATION_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VA_ASSOCIATE_TITLE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VA_SIGNATURE_BLOCK_PRINT_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VA_SIGNATURE_BLOCK_TITLE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VA_LAST_TRAINING_YEAR IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VAMC_MAIL_CODE IS 'VA HOSPITAL SERVICE NUMBER, I.E. 116A FOR PSYCHIATRY, 116B FOR PSYCHOLOGY, ETC';

COMMENT ON COLUMN ADR.PERSON_H.VA_ROOM_NUMBER IS 'This is the room number assigned to the person.';

COMMENT ON COLUMN ADR.PERSON_H.VA_TERMINATION_REASON_TEXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.VA_TERMINATION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.PREFERRED_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.WAS_PRISONER_OF_WAR_IND IS 'BR2376 PH Only Indicator is defined as the indicator that identifies the veterans in the PH Registry file who are not currently in the HEC EDB as enrollees with other eligibility/enrollment data receiving VHA services.  Once the veteran record is created in the EDB then this flag is removed ';

COMMENT ON COLUMN ADR.PERSON_H.APPOINTMENT_WAS_REQUESTED_IND IS 'Indicates if beneficiary was retired from the U.S. Armed Forces due to a disability incurred while serving.  ';

COMMENT ON COLUMN ADR.PERSON_H.APPOINTMENT_REQUESTED_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.SENSITIVITY_FLAG_SET_SRC_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.SENSITIVITY_FLAG_SET_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.PERSON_H.VOA_IND IS 'Veterans Online Application indicator';

COMMENT ON COLUMN ADR.PERSON_H.DENTAL_CLASS2_IND IS 'Derived from the VBA DD214 Dental Indicator. The DD214 Dental Indicator specifies whether or not the Veteran received dental treatment at service separation. The Dental Indicator is translated from the DD214 Indicator to state whether or not the Veteran is eligible for dental services.
  2  Null means not initialized
  3  0 means `NO¿
  4  1 means `YES¿';

COMMENT ON COLUMN ADR.PERSON_H.DENTAL_CLS2_APP_BEF_DUE_DATE IS 'Null means not initialized
  2  0 means `NO¿
  3  1 means `YES¿';


CREATE UNIQUE INDEX ADR.PRSNH_PK ON ADR.PERSON_H
(PERSON_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNH_PRSNID_NU_I ON ADR.PERSON_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_H FOR ADR.PERSON_H;


ALTER TABLE ADR.PERSON_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_009
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066039
  CHECK ("PERSON_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066040
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066041
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066043
  CHECK ("VPID_VALUE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066044
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066045
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066046
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066047
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066048
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNH_PK
  PRIMARY KEY
  (PERSON_H_ID)
  USING INDEX ADR.PRSNH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_H TO ADR_R;

GRANT SELECT ON ADR.PERSON_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_H TO CARUSR_R;

GRANT SELECT ON ADR.PERSON_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_H TO ES_RW;

GRANT SELECT ON ADR.PERSON_H TO PSIM_ADR_R;

GRANT SELECT ON ADR.PERSON_H TO DNS   STALLP;
ALTER TABLE ADR.PERSON_MERGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_MERGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_MERGE
(
  PERSON_MERGE_ID           NUMBER(20)          NOT NULL,
  PRIMARY_PERSON_ID         NUMBER(20),
  DEPRECATED_PERSON_ID      NUMBER(20),
  MERGE_RECORD_LOCKED_FLAG  NUMBER(1)           DEFAULT 0,
  MERGE_START_DATE          DATE,
  MERGE_END_DATE            DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  MERGE_DATA_TEXT           CLOB
)
LOB (MERGE_DATA_TEXT) STORE AS (
  TABLESPACE  M_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_MERGE IS 'This table supports the person merge process - in the case of a single identity sharing two or more records in the system, the identities are merged together into a single record.';

COMMENT ON COLUMN ADR.PERSON_MERGE.PERSON_MERGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_MERGE.PRIMARY_PERSON_ID IS 'Non-key ID - there is no foreign key enforcing this value. This field contains the ID value for the person record to be merged.';

COMMENT ON COLUMN ADR.PERSON_MERGE.DEPRECATED_PERSON_ID IS 'Non-key ID - there is no foreign key enforcing this value. This field contains the ID value for the duplicate person record.';

COMMENT ON COLUMN ADR.PERSON_MERGE.MERGE_RECORD_LOCKED_FLAG IS 'Neumeric FLAG record - if 1 indicates that the record is locked.';

COMMENT ON COLUMN ADR.PERSON_MERGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_MERGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_MERGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_MERGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_MERGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_MERGE.MERGE_DATA_TEXT IS 'Text value containing details of merged record';


CREATE INDEX ADR.PRSNMRG_DEPRECPRSNID_NU_I ON ADR.PERSON_MERGE
(DEPRECATED_PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PRSNMRG_PK ON ADR.PERSON_MERGE
(PERSON_MERGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNMRG_PRIMRYPRSNID_NU_I ON ADR.PERSON_MERGE
(PRIMARY_PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_MERGE FOR ADR.PERSON_MERGE;


ALTER TABLE ADR.PERSON_MERGE ADD (
  CONSTRAINT AVCON_1172521913_MERGE_000
  CHECK (MERGE_RECORD_LOCKED_FLAG BETWEEN 0
AND 0 OR MERGE_RECORD_LOCKED_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT PRSNMRG_PK
  PRIMARY KEY
  (PERSON_MERGE_ID)
  USING INDEX ADR.PRSNMRG_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_MERGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_MERGE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_MERGE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_MERGE TO ES_RW;
ALTER TABLE ADR.PERSON_PREFERRED_LANGUAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_PREFERRED_LANGUAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_PREFERRED_LANGUAGE
(
  PERSON_PREFERRED_LANGUAGE_ID  NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  STD_LANGUAGECODE_ID           NUMBER(20)      NOT NULL,
  LANGUAGE_ENTRY_DATE           DATE            NOT NULL,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.PERSON_PREFERRED_LANGUAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.PERSON_ID IS 'Person table primary key';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.STD_LANGUAGECODE_ID IS 'Language_code table primary key';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PERSON_PREFERRED_LANGUAGE_PK ON ADR.PERSON_PREFERRED_LANGUAGE
(PERSON_PREFERRED_LANGUAGE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PRSNPRFLNG_PRSN_ID_UI ON ADR.PERSON_PREFERRED_LANGUAGE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PRSN_PREF_LANG_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.PERSON_PREFERRED_LANGUAGE  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin
  if DELETING then
      insert into PERSON_PREFERRED_LANGUAGE_H
      (
      PERSON_PREFERRED_LANG_H_ID,
      PERSON_PREFERRED_LANGUAGE_ID,
      PERSON_ID,
      STD_LANGUAGECODE_ID,
      LANGUAGE_ENTRY_DATE,
      RECORD_CREATED_BY,
      RECORD_CREATED_DATE,
      RECORD_MODIFIED_BY,
      RECORD_MODIFIED_DATE,
      RECORD_MODIFIED_COUNT,
      HISTORY_CREATED_DATE,
      TRANSACTION_TYPE_CODE
      )
      values
      (
      PERSON_PREFERRED_LANGUAGE_H_S.nextval,
      :old.PERSON_PREFERRED_LANGUAGE_ID,
      :old.PERSON_ID,
      :old.STD_LANGUAGECODE_ID,
      :old.LANGUAGE_ENTRY_DATE,
      :old.RECORD_CREATED_BY,
      :old.RECORD_CREATED_DATE,
      :old.RECORD_MODIFIED_BY,
      :old.RECORD_MODIFIED_DATE,
      :old.RECORD_MODIFIED_COUNT,
      SYSDATE,
      'D'
      );

  else
      if UPDATING then
          mDML := 'U';
      else
          mDML := 'I';
      end if;

       insert into PERSON_PREFERRED_LANGUAGE_H
      (
      PERSON_PREFERRED_LANG_H_ID,
      PERSON_PREFERRED_LANGUAGE_ID,
      PERSON_ID,
      STD_LANGUAGECODE_ID,
      LANGUAGE_ENTRY_DATE,
      RECORD_CREATED_BY,
      RECORD_CREATED_DATE,
      RECORD_MODIFIED_BY,
      RECORD_MODIFIED_DATE,
      RECORD_MODIFIED_COUNT,
      HISTORY_CREATED_DATE,
      TRANSACTION_TYPE_CODE
      )
      values
      (
      PERSON_PREFERRED_LANGUAGE_H_S.nextval,
      :new.PERSON_PREFERRED_LANGUAGE_ID,
      :new.PERSON_ID,
      :new.STD_LANGUAGECODE_ID,
      :new.LANGUAGE_ENTRY_DATE,
      :new.RECORD_CREATED_BY,
      :new.RECORD_CREATED_DATE,
      :new.RECORD_MODIFIED_BY,
      :new.RECORD_MODIFIED_DATE,
      :new.RECORD_MODIFIED_COUNT,
      SYSDATE,
      mDML
      );

  end if;

end PRSN_PREF_LANG_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON_PREFERRED_LANGUAGE FOR ADR.PERSON_PREFERRED_LANGUAGE;


ALTER TABLE ADR.PERSON_PREFERRED_LANGUAGE ADD (
  CONSTRAINT PERSON_PREFERRED_LANGUAGE_PK
  PRIMARY KEY
  (PERSON_PREFERRED_LANGUAGE_ID)
  USING INDEX ADR.PERSON_PREFERRED_LANGUAGE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_PREFERRED_LANGUAGE ADD (
  CONSTRAINT PRSNPRSNPREFLANG_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE,
  CONSTRAINT STDLNGCD_PRSNPREFLNG_FK 
  FOREIGN KEY (STD_LANGUAGECODE_ID) 
  REFERENCES ADRREF.STD_LANGUAGECODE (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_PREFERRED_LANGUAGE TO ADREXT;

GRANT SELECT ON ADR.PERSON_PREFERRED_LANGUAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_PREFERRED_LANGUAGE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_PREFERRED_LANGUAGE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_PREFERRED_LANGUAGE TO ES_RW;
ALTER TABLE ADR.PERSON_PREFERRED_LANGUAGE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_PREFERRED_LANGUAGE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_PREFERRED_LANGUAGE_H
(
  PERSON_PREFERRED_LANG_H_ID    NUMBER(20)      NOT NULL,
  PERSON_PREFERRED_LANGUAGE_ID  NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  STD_LANGUAGECODE_ID           NUMBER(20)      NOT NULL,
  LANGUAGE_ENTRY_DATE           DATE            NOT NULL,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL,
  HISTORY_CREATED_DATE          DATE            NOT NULL,
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE) NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.PERSON_PREFERRED_LANG_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.PERSON_PREFERRED_LANGUAGE_ID IS 'Person_preferred_language table primary key';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.PERSON_ID IS 'person table primary key';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.STD_LANGUAGECODE_ID IS 'Language_code table primary key';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.HISTORY_CREATED_DATE IS 'Date history record created.';

COMMENT ON COLUMN ADR.PERSON_PREFERRED_LANGUAGE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). ¿I¿ for insert, ¿U¿ for update and ¿D¿ for delete.';


CREATE UNIQUE INDEX ADR.PERSON_PREFERRED_LANGUAGE_H_PK ON ADR.PERSON_PREFERRED_LANGUAGE_H
(PERSON_PREFERRED_LANG_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_PREFERRED_LANGUAGE_H FOR ADR.PERSON_PREFERRED_LANGUAGE_H;


ALTER TABLE ADR.PERSON_PREFERRED_LANGUAGE_H ADD (
  CONSTRAINT PERSON_PREFERRED_LANGUAGE_H_PK
  PRIMARY KEY
  (PERSON_PREFERRED_LANG_H_ID)
  USING INDEX ADR.PERSON_PREFERRED_LANGUAGE_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_PREFERRED_LANGUAGE_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PERSON_PREFERRED_LANGUAGE_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_PREFERRED_LANGUAGE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_PREFERRED_LANGUAGE_H TO ES_RW;
ALTER TABLE ADR.PERSON_RACE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RACE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RACE
(
  PERSON_RACE_ID             NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  STD_RACE_ID                NUMBER(20)         NOT NULL,
  COLLECTION_METHOD_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  RECORD_MODIFIED_SITE_ID    NUMBER(20),
  RECORD_MODIFIED_SRC_NAME   VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RACE IS 'The table stores information a persons reported race and the method by which that information was reported. DEMOGRAPHICS';

COMMENT ON COLUMN ADR.PERSON_RACE.PERSON_RACE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RACE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RACE.STD_RACE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RACE.COLLECTION_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_RACE.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNRACE_PK ON ADR.PERSON_RACE
(PERSON_RACE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRACE_PRSN_FK_I ON ADR.PERSON_RACE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRACE_STDINST2_FK_I ON ADR.PERSON_RACE
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRACE_STDRACE_FK_I ON ADR.PERSON_RACE
(STD_RACE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRACE_STD_CLLNMTHD_FK_I ON ADR.PERSON_RACE
(COLLECTION_METHOD_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERSON_RACE_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.PERSON_RACE for each row
declare
    mDML char(1);
begin

  if DELETING then
    insert into PERSON_RACE_H
    (
    PERSON_RACE_H_ID,
    PERSON_RACE_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    STD_RACE_ID,
    COLLECTION_METHOD_TYPE_ID,
    PERSON_ID,
    RECORD_MODIFIED_COUNT,
    RECORD_MODIFIED_SITE_ID,
    RECORD_MODIFIED_SRC_NAME,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PERSON_RACE_H_S.nextval,
    :old.PERSON_RACE_ID,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY,
    :old.RECORD_MODIFIED_DATE,
    :old.STD_RACE_ID,
    :old.COLLECTION_METHOD_TYPE_ID,
    :old.PERSON_ID,
    :old.RECORD_MODIFIED_COUNT,
    :old.RECORD_MODIFIED_SITE_ID,
    :old.RECORD_MODIFIED_SRC_NAME,
    'D'
    );



  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

    insert into PERSON_RACE_H
    (
    PERSON_RACE_H_ID,
    PERSON_RACE_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    STD_RACE_ID,
    COLLECTION_METHOD_TYPE_ID,
    PERSON_ID,
    RECORD_MODIFIED_COUNT,
    RECORD_MODIFIED_SITE_ID,
    RECORD_MODIFIED_SRC_NAME,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PERSON_RACE_H_S.nextval,
    :new.PERSON_RACE_ID,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY,
    :new.RECORD_MODIFIED_DATE,
    :new.STD_RACE_ID,
    :new.COLLECTION_METHOD_TYPE_ID,
    :new.PERSON_ID,
    :new.RECORD_MODIFIED_COUNT,
    :new.RECORD_MODIFIED_SITE_ID,
    :new.RECORD_MODIFIED_SRC_NAME,
    mDML
    );



  end if;

end PERSON_RACE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON_RACE FOR ADR.PERSON_RACE;


ALTER TABLE ADR.PERSON_RACE ADD (
  CONSTRAINT PRSNRACE_PK
  PRIMARY KEY
  (PERSON_RACE_ID)
  USING INDEX ADR.PRSNRACE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_RACE ADD (
  CONSTRAINT PRSNRACE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_RACE TO ADREXT;

GRANT SELECT ON ADR.PERSON_RACE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_RACE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RACE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RACE TO ES_RW;

GRANT SELECT ON ADR.PERSON_RACE TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PERSON_RACE TO PSIM_ADR_RW;
ALTER TABLE ADR.PERSON_RACE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RACE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RACE_H
(
  PERSON_RACE_H_ID           NUMBER(20)         NOT NULL,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)   NOT NULL,
  PERSON_RACE_ID             NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  STD_RACE_ID                NUMBER(20)         NOT NULL,
  COLLECTION_METHOD_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  RECORD_MODIFIED_SITE_ID    NUMBER(20),
  RECORD_MODIFIED_SRC_NAME   VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RACE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.PERSON_RACE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.PERSON_RACE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.STD_RACE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.COLLECTION_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PERSON_RACE_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.PRSNRACEH_PK ON ADR.PERSON_RACE_H
(PERSON_RACE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRACEH_PRSNID_NU_I ON ADR.PERSON_RACE_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_RACE_H FOR ADR.PERSON_RACE_H;


ALTER TABLE ADR.PERSON_RACE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_034
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT PRSNRACEH_PK
  PRIMARY KEY
  (PERSON_RACE_H_ID)
  USING INDEX ADR.PRSNRACEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_RACE_H TO ADREXT;

GRANT SELECT ON ADR.PERSON_RACE_H TO ADR_R;

GRANT SELECT ON ADR.PERSON_RACE_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RACE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RACE_H TO ES_RW;

GRANT SELECT ON ADR.PERSON_RACE_H TO PSIM_ADR_R;
ALTER TABLE ADR.PERSON_RELATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RELATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RELATION
(
  PERSON_RELATION_ID             NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  STD_GENDER_ID                  NUMBER(20),
  PERSON_RELATION_TYPE_ID        NUMBER(20),
  PRSN_RELATION_MAPPING_TYPE_CD  VARCHAR2(5 BYTE),
  HEC_IEN                        VARCHAR2(20 BYTE),
  PREFIX                         VARCHAR2(12 BYTE),
  FIRST_NAME                     VARCHAR2(30 BYTE),
  MIDDLE_NAME                    VARCHAR2(30 BYTE),
  LAST_NAME                      VARCHAR2(40 BYTE),
  MAIDEN_NAME                    VARCHAR2(40 BYTE),
  SUFFIX                         VARCHAR2(12 BYTE),
  BIRTH_DATE_TXT                 VARCHAR2(14 BYTE),
  EDUCATIONAL_DEGREE             VARCHAR2(30 BYTE),
  RELATION_EFFECTIVE_DATE_TXT    VARCHAR2(14 BYTE),
  RELATION_END_DATE              DATE,
  INCAPABLE_SELF_SUPPORT_IND     VARCHAR2(1 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   4
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RELATION IS 'The table stores information about the spouse and dependent(s) associated with a person, including their name, date of birth, gender, and type of relationship. Spouses and Dependents are not enumerated by the Identity Management service. They exist only as details to the Person record. PERSON_ID is a mandatory foreign key to this table. DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PERSON_RELATION.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION.STD_GENDER_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.PERSON_RELATION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.PRSN_RELATION_MAPPING_TYPE_CD IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.HEC_IEN IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION.FIRST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.MIDDLE_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.LAST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.MAIDEN_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION.BIRTH_DATE_TXT IS 'BR7731 The date of birth is defined as the birth date of the veteran or spouse associated with the communication to be mailed.';

COMMENT ON COLUMN ADR.PERSON_RELATION.EDUCATIONAL_DEGREE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.RELATION_EFFECTIVE_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.RELATION_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.INCAPABLE_SELF_SUPPORT_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RELATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RELATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RELATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RELATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRSNREL_PK ON ADR.PERSON_RELATION
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNREL_PRSN_FK_I ON ADR.PERSON_RELATION
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNREL_SG_FK_I ON ADR.PERSON_RELATION
(STD_GENDER_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNREL_STD_PRSNREL_FK_I ON ADR.PERSON_RELATION
(PRSN_RELATION_MAPPING_TYPE_CD)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNREL_STD_RELTYP_FK_I ON ADR.PERSON_RELATION
(PERSON_RELATION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PERSON_RELATION_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.PERSON_RELATION
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PERSON_RELATION_H
		(
		PERSON_RELATION_H_ID,
		PERSON_RELATION_ID,
		PERSON_ID,
		STD_GENDER_ID,
		PERSON_RELATION_TYPE_ID,
		PRSN_RELATION_MAPPING_TYPE_CD,
		HEC_IEN,
		PREFIX,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		MAIDEN_NAME,
		SUFFIX,
		BIRTH_DATE_TXT,
		EDUCATIONAL_DEGREE,
		RELATION_EFFECTIVE_DATE_TXT,
		RELATION_END_DATE,
		INCAPABLE_SELF_SUPPORT_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERSON_RELATION_H_S.nextval,
		:old.PERSON_RELATION_ID,
		:old.PERSON_ID,
		:old.STD_GENDER_ID,
		:old.PERSON_RELATION_TYPE_ID,
		:old.PRSN_RELATION_MAPPING_TYPE_CD,
		:old.HEC_IEN,
		:old.PREFIX,
		:old.FIRST_NAME,
		:old.MIDDLE_NAME,
		:old.LAST_NAME,
		:old.MAIDEN_NAME,
		:old.SUFFIX,
		:old.BIRTH_DATE_TXT,
		:old.EDUCATIONAL_DEGREE,
		:old.RELATION_EFFECTIVE_DATE_TXT,
		:old.RELATION_END_DATE,
		:old.INCAPABLE_SELF_SUPPORT_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PERSON_RELATION_H
		(
		PERSON_RELATION_H_ID,
		PERSON_RELATION_ID,
		PERSON_ID,
		STD_GENDER_ID,
		PERSON_RELATION_TYPE_ID,
		PRSN_RELATION_MAPPING_TYPE_CD,
		HEC_IEN,
		PREFIX,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		MAIDEN_NAME,
		SUFFIX,
		BIRTH_DATE_TXT,
		EDUCATIONAL_DEGREE,
		RELATION_EFFECTIVE_DATE_TXT,
		RELATION_END_DATE,
		INCAPABLE_SELF_SUPPORT_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PERSON_RELATION_H_S.nextval,
		:new.PERSON_RELATION_ID,
		:new.PERSON_ID,
		:new.STD_GENDER_ID,
		:new.PERSON_RELATION_TYPE_ID,
		:new.PRSN_RELATION_MAPPING_TYPE_CD,
		:new.HEC_IEN,
		:new.PREFIX,
		:new.FIRST_NAME,
		:new.MIDDLE_NAME,
		:new.LAST_NAME,
		:new.MAIDEN_NAME,
		:new.SUFFIX,
		:new.BIRTH_DATE_TXT,
		:new.EDUCATIONAL_DEGREE,
		:new.RELATION_EFFECTIVE_DATE_TXT,
		:new.RELATION_END_DATE,
		:new.INCAPABLE_SELF_SUPPORT_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end PERSON_RELATION_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PERSON_RELATION FOR ADR.PERSON_RELATION;


ALTER TABLE ADR.PERSON_RELATION ADD (
  CONSTRAINT PRSNREL_PK
  PRIMARY KEY
  (PERSON_RELATION_ID)
  USING INDEX ADR.PRSNREL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_RELATION ADD (
  CONSTRAINT PRSNREL_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_RELATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_RELATION TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RELATION TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RELATION TO ES_RW;

GRANT SELECT ON ADR.PERSON_RELATION TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PERSON_RELATION TO PSIM_ADR_RW;
ALTER TABLE ADR.PERSON_RELATION_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RELATION_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RELATION_H
(
  PERSON_RELATION_H_ID           NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  PERSON_RELATION_ID             NUMBER(20),
  PERSON_ID                      NUMBER(20),
  STD_GENDER_ID                  NUMBER(20),
  PERSON_RELATION_TYPE_ID        NUMBER(20),
  PRSN_RELATION_MAPPING_TYPE_CD  VARCHAR2(5 BYTE),
  HEC_IEN                        VARCHAR2(20 BYTE),
  PREFIX                         VARCHAR2(12 BYTE),
  FIRST_NAME                     VARCHAR2(30 BYTE),
  MIDDLE_NAME                    VARCHAR2(30 BYTE),
  LAST_NAME                      VARCHAR2(40 BYTE),
  MAIDEN_NAME                    VARCHAR2(40 BYTE),
  SUFFIX                         VARCHAR2(12 BYTE),
  BIRTH_DATE_TXT                 VARCHAR2(14 BYTE),
  EDUCATIONAL_DEGREE             VARCHAR2(30 BYTE),
  RELATION_EFFECTIVE_DATE_TXT    VARCHAR2(14 BYTE),
  RELATION_END_DATE              DATE,
  INCAPABLE_SELF_SUPPORT_IND     VARCHAR2(1 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RELATION_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PERSON_RELATION_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.STD_GENDER_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PERSON_RELATION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PRSN_RELATION_MAPPING_TYPE_CD IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.HEC_IEN IS 'Unique identifier used in the legacy HEC system. Records from the ADR, the HEC Legacy system and also the current IVM system can be tied together using this number.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.BIRTH_DATE_TXT IS 'BR7731 The date of birth is defined as the birth date of the veteran or spouse associated with the communication to be mailed.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RELATION_EFFECTIVE_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RELATION_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.INCAPABLE_SELF_SUPPORT_IND IS 'BR2084 Dependent Child is Permanently Incapable Of Self-Support. Indicator is defined as an Indicator if the child is over the age of 18 and became permanently incapable of self support before reaching the age of 18.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RELATION_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRSNRELH_PK ON ADR.PERSON_RELATION_H
(PERSON_RELATION_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRELH_PRSNID_NU_I ON ADR.PERSON_RELATION_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRELH_PRSNRELID_NU_I ON ADR.PERSON_RELATION_H
(PERSON_RELATION_ID, PRSN_RELATION_MAPPING_TYPE_CD)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_RELATION_H FOR ADR.PERSON_RELATION_H;


ALTER TABLE ADR.PERSON_RELATION_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_027
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066451
  CHECK ("PERSON_RELATION_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066452
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066453
  CHECK ("PERSON_RELATION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066454
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066455
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066456
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066457
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066458
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066459
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRSNRELH_PK
  PRIMARY KEY
  (PERSON_RELATION_H_ID)
  USING INDEX ADR.PRSNRELH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_RELATION_H TO ADR_R;

GRANT SELECT ON ADR.PERSON_RELATION_H TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RELATION_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RELATION_H TO ES_RW;

GRANT SELECT ON ADR.PERSON_RELATION_H TO PSIM_ADR_R;
ALTER TABLE ADR.PERSON_RELATION_MERGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RELATION_MERGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RELATION_MERGE
(
  PERSON_RELATION_MERGE_ID  NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  PERSON_RELATION_ID_1      NUMBER(20),
  PERSON_RELATION_ID_2      NUMBER(20),
  MERGE_IN_PROGRESS_FLAG    NUMBER(1)           DEFAULT 0,
  MERGE_RECORD_LOCKED_FLAG  NUMBER(1)           DEFAULT 0,
  MERGE_DATA_TEXT           VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RELATION_MERGE IS ' In the case that the identity of a person relation (such as a spouse or dependent) has been duplicated in the system, the duplicate records are merged together in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.PERSON_RELATION_MERGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.PERSON_RELATION_ID_1 IS 'Stores the ID of the spouse or dependent record.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.PERSON_RELATION_ID_2 IS 'Stores the ID (first duplicate) of the spouse or dependent.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.MERGE_IN_PROGRESS_FLAG IS 'Numeric FLAG record - if 1, indicates that the merge process is ongoing';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.MERGE_RECORD_LOCKED_FLAG IS 'FLAG value - indicates whether record is locked or not.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.MERGE_DATA_TEXT IS 'Stores all the related data generated during the merge process,  for this record';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RELATION_MERGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRSNRELMRG_PK ON ADR.PERSON_RELATION_MERGE
(PERSON_RELATION_MERGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRSNRELMRG_PRSN_FK_I ON ADR.PERSON_RELATION_MERGE
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_RELATION_MERGE FOR ADR.PERSON_RELATION_MERGE;


ALTER TABLE ADR.PERSON_RELATION_MERGE ADD (
  CONSTRAINT AVCON_1172521913_MERGE_001
  CHECK (MERGE_IN_PROGRESS_FLAG BETWEEN 0
AND 0 OR MERGE_IN_PROGRESS_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT AVCON_1172521913_MERGE_002
  CHECK (MERGE_RECORD_LOCKED_FLAG BETWEEN 0
AND 0 OR MERGE_RECORD_LOCKED_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT PRSNRELMRG_PK
  PRIMARY KEY
  (PERSON_RELATION_MERGE_ID)
  USING INDEX ADR.PRSNRELMRG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PERSON_RELATION_MERGE ADD (
  CONSTRAINT PRSNRELMRG_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PERSON_RELATION_MERGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_RELATION_MERGE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RELATION_MERGE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RELATION_MERGE TO ES_RW;
ALTER TABLE ADR.PERSON_RELATION_UNMERGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_RELATION_UNMERGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_RELATION_UNMERGE
(
  PERSON_RELATION_UNMERGE_ID  NUMBER(20)        NOT NULL,
  PERSON_RELATION_ID_2        NUMBER(20),
  PERSON_RELATION_ID_1        NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_RELATION_UNMERGE IS 'In the case that two distinct identities of person relations (such as spouses or dependents) have mistakenly been merged into a single record, the merged record will now have to be un-merged in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.PERSON_RELATION_UNMERGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.PERSON_RELATION_ID_2 IS 'Stores the ID (first duplicate) of the spouse or dependent.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.PERSON_RELATION_ID_1 IS 'Stores the ID of the spouse or dependent record.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_RELATION_UNMERGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRSNRELUNM_PK ON ADR.PERSON_RELATION_UNMERGE
(PERSON_RELATION_UNMERGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_RELATION_UNMERGE FOR ADR.PERSON_RELATION_UNMERGE;


ALTER TABLE ADR.PERSON_RELATION_UNMERGE ADD (
  CONSTRAINT PRSNRELUNM_PK
  PRIMARY KEY
  (PERSON_RELATION_UNMERGE_ID)
  USING INDEX ADR.PRSNRELUNM_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_RELATION_UNMERGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_RELATION_UNMERGE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_RELATION_UNMERGE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_RELATION_UNMERGE TO ES_RW;
ALTER TABLE ADR.PERSON_UNMERGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PERSON_UNMERGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PERSON_UNMERGE
(
  PERSON_UNMERGE_ID      NUMBER(20)             NOT NULL,
  PERSON_ID_1            NUMBER(20),
  PERSON_ID_2            NUMBER(20),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PERSON_UNMERGE IS 'In the case that two distinct identities of Persons have mistakenly been merged into a single record, the merged record will now have to be un-merged in this table.';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.PERSON_UNMERGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PERSON_UNMERGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRSNUNMRG_PK ON ADR.PERSON_UNMERGE
(PERSON_UNMERGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PERSON_UNMERGE FOR ADR.PERSON_UNMERGE;


ALTER TABLE ADR.PERSON_UNMERGE ADD (
  CONSTRAINT PRSNUNMRG_PK
  PRIMARY KEY
  (PERSON_UNMERGE_ID)
  USING INDEX ADR.PRSNUNMRG_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PERSON_UNMERGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PERSON_UNMERGE TO ADR_RW;

GRANT SELECT ON ADR.PERSON_UNMERGE TO ES_R;

GRANT INSERT, SELECT ON ADR.PERSON_UNMERGE TO ES_RW;
ALTER TABLE ADR.PHONE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PHONE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PHONE
(
  PHONE_ID                     NUMBER(20),
  PERSON_ID                    NUMBER(20),
  INSURANCE_ID                 NUMBER(20),
  PHONE_TYPE_ID                NUMBER(20),
  PHONE_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  PHONE_NUMBER                 VARCHAR2(128 BYTE),
  CHANGE_EFFECTIVE_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE),
  PERSON_RELATION_ID           NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PHONE IS 'The PHONE table stores phone numbers for persons and insurance companies. Maintains all types of phone information for Persons. Also stores Insurance Company telephone information. Foreign Keys present in table: PERSON_ID INSURANCE_ID PHONE_TYPE_ID (Types of telephones applicable to persons - Residential, Business etc.) DEMOGRAPHICS / ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PHONE.PHONE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PHONE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PHONE.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE.PHONE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE.PHONE_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE.PHONE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of phone went into effect.';

COMMENT ON COLUMN ADR.PHONE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PHONE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PHONE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PHONE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PHONE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PHONE.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.PHONE.PERSON_RELATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';


CREATE INDEX ADR.PHONE_INSURANCE_FK_I ON ADR.PHONE
(INSURANCE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PHONE_PK ON ADR.PHONE
(PHONE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONE_PRSNREL_FK_I ON ADR.PHONE
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONE_PRSN_FK_I ON ADR.PHONE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONE_STDINST_FK_I ON ADR.PHONE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONE_STD_PHCHGSRC_FK_I ON ADR.PHONE
(PHONE_CHANGE_SOURCE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONE_STD_PHONECTACTTYP_FK_I ON ADR.PHONE
(PHONE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PHONE_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."PHONE" FOR EACH ROW
declare
    mDML char(1);
begin

  if DELETING then
    insert into PHONE_H
    (
    PHONE_H_ID,
    PHONE_ID  ,
    PHONE_NUMBER,
    CHANGE_EFFECTIVE_DATE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY ,
    RECORD_MODIFIED_DATE,
    INSURANCE_ID        ,
    PHONE_TYPE_ID       ,
    PERSON_ID           ,
    RECORD_MODIFIED_COUNT,
    STD_INSTITUTION_ID  ,
    PHONE_CHANGE_SOURCE_TYPE_ID,
    RECORD_MODIFIED_SRC_NAME,
    PERSON_RELATION_ID,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PHONE_h_s.nextval,
    :old.PHONE_ID  ,
    :old.PHONE_NUMBER,
    :old.CHANGE_EFFECTIVE_DATE,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY ,
    :old.RECORD_MODIFIED_DATE,
    :old.INSURANCE_ID        ,
    :old.PHONE_TYPE_ID       ,
    :old.PERSON_ID           ,
    :old.RECORD_MODIFIED_COUNT,
    :old.STD_INSTITUTION_ID  ,
    :old.PHONE_CHANGE_SOURCE_TYPE_ID,
    :old.RECORD_MODIFIED_SRC_NAME,
    :old.PERSON_RELATION_ID,
    'D'
    );

  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;
    insert into PHONE_H
    (
    PHONE_H_ID,
    PHONE_ID  ,
    PHONE_NUMBER,
    CHANGE_EFFECTIVE_DATE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY ,
    RECORD_MODIFIED_DATE,
    INSURANCE_ID        ,
    PHONE_TYPE_ID       ,
    PERSON_ID           ,
    RECORD_MODIFIED_COUNT,
    STD_INSTITUTION_ID  ,
    PHONE_CHANGE_SOURCE_TYPE_ID,
    RECORD_MODIFIED_SRC_NAME,
    PERSON_RELATION_ID,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    PHONE_h_s.nextval,
    :new.PHONE_ID  ,
    :new.PHONE_NUMBER,
    :new.CHANGE_EFFECTIVE_DATE,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY ,
    :new.RECORD_MODIFIED_DATE,
    :new.INSURANCE_ID        ,
    :new.PHONE_TYPE_ID       ,
    :new.PERSON_ID           ,
    :new.RECORD_MODIFIED_COUNT,
    :new.STD_INSTITUTION_ID  ,
    :new.PHONE_CHANGE_SOURCE_TYPE_ID,
    :new.RECORD_MODIFIED_SRC_NAME,
    :new.PERSON_RELATION_ID,
    mDML
    );

  end if;

end PHONE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PHONE FOR ADR.PHONE;


ALTER TABLE ADR.PHONE ADD (
  CONSTRAINT SYS_C0066892
  CHECK ("PHONE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066893
  CHECK ("PHONE_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066894
  CHECK ("PHONE_NUMBER" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066895
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066896
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066897
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066898
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066899
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PHONE_PK
  PRIMARY KEY
  (PHONE_ID)
  USING INDEX ADR.PHONE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PHONE ADD (
  CONSTRAINT PHONE_INSURANCE_FK 
  FOREIGN KEY (INSURANCE_ID) 
  REFERENCES ADR.INSURANCE (INSURANCE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT PHONE_PRSNREL_FK 
  FOREIGN KEY (PERSON_RELATION_ID) 
  REFERENCES ADR.PERSON_RELATION (PERSON_RELATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT PHONE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PHONE TO ADREXT;

GRANT SELECT ON ADR.PHONE TO ADRTMP;

GRANT SELECT ON ADR.PHONE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PHONE TO ADR_RW;

GRANT SELECT ON ADR.PHONE TO ESCISSEEDINGUSR;

GRANT SELECT ON ADR.PHONE TO ES_R;

GRANT INSERT, SELECT ON ADR.PHONE TO ES_RW;

GRANT SELECT ON ADR.PHONE TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.PHONE TO PSIM_ADR_RW;
ALTER TABLE ADR.PHONE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PHONE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PHONE_H
(
  PHONE_H_ID                   NUMBER(20),
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE),
  PHONE_ID                     NUMBER(20),
  PERSON_ID                    NUMBER(20),
  INSURANCE_ID                 NUMBER(20),
  PHONE_TYPE_ID                NUMBER(20),
  PHONE_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID           NUMBER(20),
  PHONE_NUMBER                 VARCHAR2(128 BYTE),
  CHANGE_EFFECTIVE_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE),
  PERSON_RELATION_ID           NUMBER(20)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PHONE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PHONE_H.PHONE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PHONE_H.PHONE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PHONE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PHONE_H.INSURANCE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE_H.PHONE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE_H.PHONE_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.PHONE_H.PHONE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PHONE_H.CHANGE_EFFECTIVE_DATE IS 'Date on which a change of email address went into effect.';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.PHONE_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';

COMMENT ON COLUMN ADR.PHONE_H.PERSON_RELATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';


CREATE UNIQUE INDEX ADR.PHONEH_PK ON ADR.PHONE_H
(PHONE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHONEH_PRSNID_NU_I ON ADR.PHONE_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PHONE_H FOR ADR.PHONE_H;


ALTER TABLE ADR.PHONE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_064
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067287
  CHECK ("PHONE_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067288
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067289
  CHECK ("PHONE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067290
  CHECK ("PHONE_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067291
  CHECK ("PHONE_NUMBER" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067292
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067293
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067294
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067295
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067296
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PHONEH_PK
  PRIMARY KEY
  (PHONE_H_ID)
  USING INDEX ADR.PHONEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PHONE_H TO ADR_R;

GRANT SELECT ON ADR.PHONE_H TO ADR_RW;

GRANT SELECT ON ADR.PHONE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PHONE_H TO ES_RW;

GRANT SELECT ON ADR.PHONE_H TO PSIM_ADR_R;
ALTER TABLE ADR.PH_REGISTRY_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PH_REGISTRY_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.PH_REGISTRY_DETAIL
(
  PH_REGISTRY_DETAIL_ID        NUMBER(20)       NOT NULL,
  PURPLE_HEART_REGISTRY_ID     NUMBER(20)       NOT NULL,
  PH_DOCUMENT_TYPE_ID          NUMBER(20),
  PH_DATA_SOURCE_TYPE_ID       NUMBER(20),
  PH_DOCUMENT_RECEIVED_DATE    DATE,
  PH_DOCUMENT_LOGIN_DATE       DATE,
  PH_DOCUMENT_LOGIN_USERNAME   VARCHAR2(30 BYTE),
  PH_DOCUMENT_ACCEPTABLE_IND   VARCHAR2(1 BYTE),
  PH_DOC_UNACCEPTABLE_REMARKS  VARCHAR2(2000 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PH_REGISTRY_DETAIL IS 'Contains details about a Purple Heart Registry Such as when PH documentation was received, when and who logged it in to the system.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PURPLE_HEART_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOCUMENT_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DATA_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOCUMENT_RECEIVED_DATE IS 'BR2352 Date Document Received is defined as is the date the HEC received PH documentation from a site or a veteran or veteran representative. Acceptable entries are: the current days date or one in the past but NOT a future date.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOCUMENT_LOGIN_DATE IS 'Date the PH Documentation was entered into the system';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOCUMENT_LOGIN_USERNAME IS 'Name of user who keyed in PH Documentation into the system';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOCUMENT_ACCEPTABLE_IND IS 'BR2358 Document Acceptable Indicator is defined as the value of YES or NO if the document received from a site or the veteran has met the criteria defined in the VHA Directive.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.PH_DOC_UNACCEPTABLE_REMARKS IS 'BR2361 Unacceptable Remarks is defined as is a free text field that allows the user to comment on why a document was unacceptable after they have sent a PH 630A letter. ';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PHREGDTL_PK ON ADR.PH_REGISTRY_DETAIL
(PH_REGISTRY_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHREGDTL_PRPLHRTREG_FK_I ON ADR.PH_REGISTRY_DETAIL
(PURPLE_HEART_REGISTRY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHREGDTL_STD_DCRNDOCTYP_FK_I ON ADR.PH_REGISTRY_DETAIL
(PH_DOCUMENT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHREGDTL_STD_PHDATASRC_FK_I ON ADR.PH_REGISTRY_DETAIL
(PH_DATA_SOURCE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PH_REGISTRY_DET_audir_trgr
after Update or Delete or Insert
ON ADR.PH_REGISTRY_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PH_REGISTRY_DETAIL_H
		(
  		 PH_REGISTRY_DETAIL_H_ID,
 		 PH_REGISTRY_DETAIL_ID,
 		 PH_DOCUMENT_TYPE_ID,
		 PURPLE_HEART_REGISTRY_ID,
		 PH_DATA_SOURCE_TYPE_ID,
 		 PH_DOCUMENT_RECEIVED_DATE,
 		 PH_DOCUMENT_LOGIN_DATE,
 		 PH_DOCUMENT_LOGIN_USERNAME,
 		 PH_DOCUMENT_ACCEPTABLE_IND,
 		 PH_DOC_UNACCEPTABLE_REMARKS,
 		 RECORD_CREATED_BY,
 		 RECORD_CREATED_DATE,
 		 RECORD_MODIFIED_BY,
 		 RECORD_MODIFIED_DATE,
 		 RECORD_MODIFIED_COUNT,
		 TRANSACTION_TYPE_CODE
		)
		values
		(
		PH_REGISTRY_DETAIL_H_S.nextval,
		:old.PH_REGISTRY_DETAIL_ID,
		:old.PH_DOCUMENT_TYPE_ID,
		:old.PURPLE_HEART_REGISTRY_ID,
		:old.PH_DATA_SOURCE_TYPE_ID,
		:old.PH_DOCUMENT_RECEIVED_DATE,
		:old.PH_DOCUMENT_LOGIN_DATE,
		:old.PH_DOCUMENT_LOGIN_USERNAME,
		:old.PH_DOCUMENT_ACCEPTABLE_IND,
		:old.PH_DOC_UNACCEPTABLE_REMARKS,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PH_REGISTRY_DETAIL_H
		(
  		 PH_REGISTRY_DETAIL_H_ID,
 		 PH_REGISTRY_DETAIL_ID,
 		 PH_DOCUMENT_TYPE_ID,
		 PURPLE_HEART_REGISTRY_ID,
		 PH_DATA_SOURCE_TYPE_ID,
 		 PH_DOCUMENT_RECEIVED_DATE,
 		 PH_DOCUMENT_LOGIN_DATE,
 		 PH_DOCUMENT_LOGIN_USERNAME,
 		 PH_DOCUMENT_ACCEPTABLE_IND,
 		 PH_DOC_UNACCEPTABLE_REMARKS,
 		 RECORD_CREATED_BY,
 		 RECORD_CREATED_DATE,
 		 RECORD_MODIFIED_BY,
 		 RECORD_MODIFIED_DATE,
 		 RECORD_MODIFIED_COUNT,
		 TRANSACTION_TYPE_CODE
		)
		values
		(
		PH_REGISTRY_DETAIL_H_S.nextval,
		:new.PH_REGISTRY_DETAIL_ID,
		:new.PH_DOCUMENT_TYPE_ID,
		:new.PURPLE_HEART_REGISTRY_ID,
		:new.PH_DATA_SOURCE_TYPE_ID,
		:new.PH_DOCUMENT_RECEIVED_DATE,
		:new.PH_DOCUMENT_LOGIN_DATE,
		:new.PH_DOCUMENT_LOGIN_USERNAME,
		:new.PH_DOCUMENT_ACCEPTABLE_IND,
		:new.PH_DOC_UNACCEPTABLE_REMARKS,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end PH_REGISTRY_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PH_REGISTRY_DETAIL FOR ADR.PH_REGISTRY_DETAIL;


ALTER TABLE ADR.PH_REGISTRY_DETAIL ADD (
  CONSTRAINT PHREGDTL_PK
  PRIMARY KEY
  (PH_REGISTRY_DETAIL_ID)
  USING INDEX ADR.PHREGDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PH_REGISTRY_DETAIL ADD (
  CONSTRAINT PHREGDTL_PRPLHRTREG_FK 
  FOREIGN KEY (PURPLE_HEART_REGISTRY_ID) 
  REFERENCES ADR.PURPLE_HEART_REGISTRY (PURPLE_HEART_REGISTRY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PH_REGISTRY_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PH_REGISTRY_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.PH_REGISTRY_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.PH_REGISTRY_DETAIL TO ES_RW;
ALTER TABLE ADR.PH_REGISTRY_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PH_REGISTRY_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PH_REGISTRY_DETAIL_H
(
  PH_REGISTRY_DETAIL_H_ID      NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  PH_REGISTRY_DETAIL_ID        NUMBER(20)       NOT NULL,
  PURPLE_HEART_REGISTRY_ID     NUMBER(20)       NOT NULL,
  PH_DOCUMENT_TYPE_ID          NUMBER(20),
  PH_DATA_SOURCE_TYPE_ID       NUMBER(20),
  PH_DOCUMENT_RECEIVED_DATE    DATE,
  PH_DOCUMENT_LOGIN_DATE       DATE,
  PH_DOCUMENT_LOGIN_USERNAME   VARCHAR2(30 BYTE),
  PH_DOCUMENT_ACCEPTABLE_IND   VARCHAR2(1 BYTE),
  PH_DOC_UNACCEPTABLE_REMARKS  VARCHAR2(2000 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PH_REGISTRY_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_REGISTRY_DETAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PURPLE_HEART_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOCUMENT_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DATA_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOCUMENT_RECEIVED_DATE IS 'BR2352 Date Document Received is defined as is the date the HEC received PH documentation from a site or a veteran or veteran representative. Acceptable entries are: the current days date or one in the past but NOT a future date.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOCUMENT_LOGIN_DATE IS 'Date the PH Documentation was entered into the system';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOCUMENT_LOGIN_USERNAME IS 'Name of user who keyed in PH Documentation into the system';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOCUMENT_ACCEPTABLE_IND IS 'BR2358 Document Acceptable Indicator is defined as the value of YES or NO if the document received from a site or the veteran has met the criteria defined in the VHA Directive.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.PH_DOC_UNACCEPTABLE_REMARKS IS 'BR2361 Unacceptable Remarks is defined as is a free text field that allows the user to comment on why a document was unacceptable after they have sent a PH 630A letter. ';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PH_REGISTRY_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PHREGDTLH_PK ON ADR.PH_REGISTRY_DETAIL_H
(PH_REGISTRY_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PHREGDTLH_PRPLHRTREG_NU_I ON ADR.PH_REGISTRY_DETAIL_H
(PURPLE_HEART_REGISTRY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PH_REGISTRY_DETAIL_H FOR ADR.PH_REGISTRY_DETAIL_H;


ALTER TABLE ADR.PH_REGISTRY_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_071
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT PHREGDTLH_PK
  PRIMARY KEY
  (PH_REGISTRY_DETAIL_H_ID)
  USING INDEX ADR.PHREGDTLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PH_REGISTRY_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.PH_REGISTRY_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.PH_REGISTRY_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PH_REGISTRY_DETAIL_H TO ES_RW;
ALTER TABLE ADR.POW_REGISTRY_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.POW_REGISTRY_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.POW_REGISTRY_DETAIL
(
  POW_REGISTRY_DETAIL_ID       NUMBER(20)       NOT NULL,
  PRISONER_OF_WAR_REGISTRY_ID  NUMBER(20)       NOT NULL,
  POW_DATA_SOURCE_TYPE_ID      NUMBER(20),
  POW_DOCUMENT_TYPE_ID         NUMBER(20),
  POW_CONFINE_LOCN_TYPE_ID     NUMBER(20),
  STD_POWCASUALTYLOCATION_ID   NUMBER(20),
  POW_CAPTURE_DATE_TXT         VARCHAR2(14 BYTE),
  POW_RELEASE_DATE_TXT         VARCHAR2(14 BYTE),
  NUMBER_OF_DAYS_IN_CAPTIVITY  NUMBER(6),
  LEGACY_ENTERED_DATE          DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.POW_REGISTRY_DETAIL IS 'Detail table of Prisoner of War When Veteran was captured, released, how many days in captivity etc.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.POW_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.PRISONER_OF_WAR_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.POW_DATA_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.POW_DOCUMENT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.POW_CONFINE_LOCN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.STD_POWCASUALTYLOCATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.POWREGDTL_PK ON ADR.POW_REGISTRY_DETAIL
(POW_REGISTRY_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTL_POWCSLTLOC_FK_I ON ADR.POW_REGISTRY_DETAIL
(STD_POWCASUALTYLOCATION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTL_POW_FK_I ON ADR.POW_REGISTRY_DETAIL
(PRISONER_OF_WAR_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTL_STD_POWDATASRC_FK_I ON ADR.POW_REGISTRY_DETAIL
(POW_DATA_SOURCE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTL_STD_POWDOC_FK_I ON ADR.POW_REGISTRY_DETAIL
(POW_DOCUMENT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTL_STD_POWPRD_FK_I ON ADR.POW_REGISTRY_DETAIL
(POW_CONFINE_LOCN_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.pow_reg_det_audir_trgr
after Update or Delete or Insert
ON ADR.POW_REGISTRY_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into POW_REGISTRY_DETAIL_H
		(
		POW_REGISTRY_DETAIL_H_ID,
		POW_REGISTRY_DETAIL_ID,
		PRISONER_OF_WAR_REGISTRY_ID,
		POW_DATA_SOURCE_TYPE_ID,
		POW_DOCUMENT_TYPE_ID,
		POW_CONFINE_LOCN_TYPE_ID,
		STD_POWCASUALTYLOCATION_ID,
		POW_CAPTURE_DATE_TXT,
		POW_RELEASE_DATE_TXT,
		NUMBER_OF_DAYS_IN_CAPTIVITY,
		LEGACY_ENTERED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		POW_REGISTRY_DETAIL_H_S.nextval,
		:old.POW_REGISTRY_DETAIL_ID,
		:old.PRISONER_OF_WAR_REGISTRY_ID,
		:old.POW_DATA_SOURCE_TYPE_ID,
		:old.POW_DOCUMENT_TYPE_ID,
		:old.POW_CONFINE_LOCN_TYPE_ID,
		:old.STD_POWCASUALTYLOCATION_ID,
		:old.POW_CAPTURE_DATE_TXT,
		:old.POW_RELEASE_DATE_TXT,
		:old.NUMBER_OF_DAYS_IN_CAPTIVITY,
		:old.LEGACY_ENTERED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into POW_REGISTRY_DETAIL_H
		(
		POW_REGISTRY_DETAIL_H_ID,
		POW_REGISTRY_DETAIL_ID,
		PRISONER_OF_WAR_REGISTRY_ID,
		POW_DATA_SOURCE_TYPE_ID,
		POW_DOCUMENT_TYPE_ID,
		POW_CONFINE_LOCN_TYPE_ID,
		STD_POWCASUALTYLOCATION_ID,
		POW_CAPTURE_DATE_TXT,
		POW_RELEASE_DATE_TXT,
		NUMBER_OF_DAYS_IN_CAPTIVITY,
		LEGACY_ENTERED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		POW_REGISTRY_DETAIL_H_S.nextval,
		:new.POW_REGISTRY_DETAIL_ID,
		:new.PRISONER_OF_WAR_REGISTRY_ID,
		:new.POW_DATA_SOURCE_TYPE_ID,
		:new.POW_DOCUMENT_TYPE_ID,
		:new.POW_CONFINE_LOCN_TYPE_ID,
		:new.STD_POWCASUALTYLOCATION_ID,
		:new.POW_CAPTURE_DATE_TXT,
		:new.POW_RELEASE_DATE_TXT,
		:new.NUMBER_OF_DAYS_IN_CAPTIVITY,
		:new.LEGACY_ENTERED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end pow_reg_det_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM POW_REGISTRY_DETAIL FOR ADR.POW_REGISTRY_DETAIL;


ALTER TABLE ADR.POW_REGISTRY_DETAIL ADD (
  CONSTRAINT POWREGDTL_PK
  PRIMARY KEY
  (POW_REGISTRY_DETAIL_ID)
  USING INDEX ADR.POWREGDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.POW_REGISTRY_DETAIL ADD (
  CONSTRAINT POWREGDTL_POW_FK 
  FOREIGN KEY (PRISONER_OF_WAR_REGISTRY_ID) 
  REFERENCES ADR.PRISONER_OF_WAR_REGISTRY (PRISONER_OF_WAR_REGISTRY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.POW_REGISTRY_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.POW_REGISTRY_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.POW_REGISTRY_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.POW_REGISTRY_DETAIL TO ES_RW;
ALTER TABLE ADR.POW_REGISTRY_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.POW_REGISTRY_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.POW_REGISTRY_DETAIL_H
(
  POW_REGISTRY_DETAIL_H_ID     NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  POW_REGISTRY_DETAIL_ID       NUMBER(20)       NOT NULL,
  PRISONER_OF_WAR_REGISTRY_ID  NUMBER(20)       NOT NULL,
  POW_DATA_SOURCE_TYPE_ID      NUMBER(20),
  POW_DOCUMENT_TYPE_ID         NUMBER(20),
  STD_POWCASUALTYLOCATION_ID   NUMBER(20),
  POW_CONFINE_LOCN_TYPE_ID     NUMBER(20),
  POW_CAPTURE_DATE_TXT         VARCHAR2(14 BYTE),
  POW_RELEASE_DATE_TXT         VARCHAR2(14 BYTE),
  NUMBER_OF_DAYS_IN_CAPTIVITY  NUMBER(6),
  LEGACY_ENTERED_DATE          DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.POW_REGISTRY_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.POW_REGISTRY_DETAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.POW_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.PRISONER_OF_WAR_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.POW_DATA_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.POW_DOCUMENT_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.STD_POWCASUALTYLOCATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.POW_CONFINE_LOCN_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.POW_REGISTRY_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.POWREGDTH_PK ON ADR.POW_REGISTRY_DETAIL_H
(POW_REGISTRY_DETAIL_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWREGDTH_POWREGID_NU_I ON ADR.POW_REGISTRY_DETAIL_H
(PRISONER_OF_WAR_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM POW_REGISTRY_DETAIL_H FOR ADR.POW_REGISTRY_DETAIL_H;


ALTER TABLE ADR.POW_REGISTRY_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_022
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT POWREGDTH_PK
  PRIMARY KEY
  (POW_REGISTRY_DETAIL_H_ID)
  USING INDEX ADR.POWREGDTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.POW_REGISTRY_DETAIL_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.POW_REGISTRY_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.POW_REGISTRY_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.POW_REGISTRY_DETAIL_H TO ES_RW;
ALTER TABLE ADR.PREFERRED_FACILITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PREFERRED_FACILITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.PREFERRED_FACILITY
(
  PREFERRED_FACILITY_ID          NUMBER(20),
  PERSON_ID                      NUMBER(20),
  STD_PREFERRED_FACILITY_SRC_ID  NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  ASSIGNMENT_DATE                DATE,
  UNASSIGNMENT_DATE              DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PREFERRED_FACILITY IS 'A PREFERRED FACILITY assigned to the patient on an ASSIGNMENT_DATE.';

COMMENT ON COLUMN ADR.PREFERRED_FACILITY.ASSIGNMENT_DATE IS 'Date when this site was assigned as a PREFERRED FACILITY.';

COMMENT ON COLUMN ADR.PREFERRED_FACILITY.UNASSIGNMENT_DATE IS 'Date when this site was unassigned from being PREFERRED FACILITY.';


CREATE INDEX ADR.IX_RELATIONSHIP60 ON ADR.PREFERRED_FACILITY
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.PRFFCL_PK ON ADR.PREFERRED_FACILITY
(PREFERRED_FACILITY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRFFCL_STDINS_NU_I ON ADR.PREFERRED_FACILITY
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PREFERRED_FACILITY_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."PREFERRED_FACILITY" FOR EACH ROW
declare
       mDML char(1);
begin
    if DELETING then
        insert into PREFERRED_FACILITY_H
        (
        PREFERRED_FACILITY_H_ID,
        PREFERRED_FACILITY_ID,
        PERSON_ID,
        STD_PREFERRED_FACILITY_SRC_ID,
        STD_INSTITUTION_ID,
        ASSIGNMENT_DATE,
        UNASSIGNMENT_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PREFERRED_FACILITY_H_S.nextval,
        :old.PREFERRED_FACILITY_ID,
        :old.PERSON_ID,
        :old.STD_PREFERRED_FACILITY_SRC_ID,
        :old.STD_INSTITUTION_ID,
        :old.ASSIGNMENT_DATE,
        :old.UNASSIGNMENT_DATE,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into PREFERRED_FACILITY_H
        (
        PREFERRED_FACILITY_H_ID,
        PREFERRED_FACILITY_ID,
        PERSON_ID,
        STD_PREFERRED_FACILITY_SRC_ID,
        STD_INSTITUTION_ID,
        ASSIGNMENT_DATE,
        UNASSIGNMENT_DATE,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        PREFERRED_FACILITY_H_S.nextval,
        :new.PREFERRED_FACILITY_ID,
        :new.PERSON_ID,
        :new.STD_PREFERRED_FACILITY_SRC_ID,
        :new.STD_INSTITUTION_ID,
        :new.ASSIGNMENT_DATE,
        :new.UNASSIGNMENT_DATE,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end PREFERRED_FACILITY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PREFERRED_FACILITY FOR ADR.PREFERRED_FACILITY;


ALTER TABLE ADR.PREFERRED_FACILITY ADD (
  CONSTRAINT SYS_C00160851
  CHECK ("PREFERRED_FACILITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160852
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160853
  CHECK ("STD_PREFERRED_FACILITY_SRC_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160854
  CHECK ("STD_INSTITUTION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160855
  CHECK ("ASSIGNMENT_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160856
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160857
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160858
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160859
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160860
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRFFCL_PK
  PRIMARY KEY
  (PREFERRED_FACILITY_ID)
  USING INDEX ADR.PRFFCL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PREFERRED_FACILITY ADD (
  CONSTRAINT RELATIONSHIP60 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PREFERRED_FACILITY TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PREFERRED_FACILITY TO ADR_RW;

GRANT DELETE ON ADR.PREFERRED_FACILITY TO EEUSR;

GRANT SELECT ON ADR.PREFERRED_FACILITY TO ES_R;

GRANT INSERT, SELECT ON ADR.PREFERRED_FACILITY TO ES_RW;
ALTER TABLE ADR.PREFERRED_FACILITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PREFERRED_FACILITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PREFERRED_FACILITY_H
(
  PREFERRED_FACILITY_H_ID        NUMBER(20),
  PREFERRED_FACILITY_ID          NUMBER(20),
  PERSON_ID                      NUMBER(20),
  STD_PREFERRED_FACILITY_SRC_ID  NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  ASSIGNMENT_DATE                DATE,
  UNASSIGNMENT_DATE              DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10),
  HISTORY_CREATED_DATE           DATE,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PREFERRED_FACILITY_H IS 'A PREFERRED FACILITY assigned to the patient on an ASSIGNMENT_DATE.';

COMMENT ON COLUMN ADR.PREFERRED_FACILITY_H.ASSIGNMENT_DATE IS 'Date when this site was assigned as a PREFERRED FACILITY.';

COMMENT ON COLUMN ADR.PREFERRED_FACILITY_H.UNASSIGNMENT_DATE IS 'Date when this site was unassigned from being PREFERRED FACILITY.';

COMMENT ON COLUMN ADR.PREFERRED_FACILITY_H.HISTORY_CREATED_DATE IS 'Date history record created.';


CREATE INDEX ADR.PREFRRREDFAC_PER_ID_NU_I ON ADR.PREFERRED_FACILITY_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE INDEX ADR.PREFRRRED_FAC_ID_NU_I ON ADR.PREFERRED_FACILITY_H
(PREFERRED_FACILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE UNIQUE INDEX ADR.PRFFCLH_PK ON ADR.PREFERRED_FACILITY_H
(PREFERRED_FACILITY_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PREFERRED_FACILITY_H FOR ADR.PREFERRED_FACILITY_H;


ALTER TABLE ADR.PREFERRED_FACILITY_H ADD (
  CONSTRAINT SYS_C00160861
  CHECK ("PREFERRED_FACILITY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160862
  CHECK ("PREFERRED_FACILITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160863
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160864
  CHECK ("STD_PREFERRED_FACILITY_SRC_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160865
  CHECK ("STD_INSTITUTION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160866
  CHECK ("ASSIGNMENT_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160867
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160868
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160869
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160870
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160871
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160872
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C00160873
  CHECK ("HISTORY_CREATED_DATE" IS NOT NULL)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT PRFFCLH_PK
  PRIMARY KEY
  (PREFERRED_FACILITY_H_ID)
  USING INDEX ADR.PRFFCLH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PREFERRED_FACILITY_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PREFERRED_FACILITY_H TO ADR_RW;

GRANT SELECT ON ADR.PREFERRED_FACILITY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PREFERRED_FACILITY_H TO ES_RW;
ALTER TABLE ADR.PRISONER_OF_WAR_REGISTRY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PRISONER_OF_WAR_REGISTRY CASCADE CONSTRAINTS;

CREATE TABLE ADR.PRISONER_OF_WAR_REGISTRY
(
  PRISONER_OF_WAR_REGISTRY_ID  NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20),
  REGISTRY_TRAIT_ID            NUMBER(20),
  POW_STDYESNO_ID              NUMBER(20),
  LEGACY_ICN                   VARCHAR2(30 BYTE),
  LEGACY_USERNAME              VARCHAR2(30 BYTE),
  LEGACY_ENTERED_DATE          DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PRISONER_OF_WAR_REGISTRY IS 'The table contains data about persons who have been captured and imprisoned as prisoners of war, The data originates from the Department of Defense, Veterans Benefits Administration, and the National Archives. This table corresponds to the Prisoner of War Registry File. All persons in the POW registry file are enumerated and a record is created for them in this table with full details. The Enrollment application uses some of the information in this table as a factor in enrollment calculations. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.PRISONER_OF_WAR_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.LEGACY_ENTERED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.POW_LGCYICN_NU_I ON ADR.PRISONER_OF_WAR_REGISTRY
(LEGACY_ICN)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.POW_PK ON ADR.PRISONER_OF_WAR_REGISTRY
(PRISONER_OF_WAR_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POW_PRSN_FK_I ON ADR.PRISONER_OF_WAR_REGISTRY
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POW_REGTRAIT_FK_I ON ADR.PRISONER_OF_WAR_REGISTRY
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POW_STDYESNO_FK_I ON ADR.PRISONER_OF_WAR_REGISTRY
(POW_STDYESNO_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.POW_REGISTRY_AUDIR_TRGR
after Update or Delete or Insert
on ADR.PRISONER_OF_WAR_REGISTRY
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PRISONER_OF_WAR_REGISTRY_H
		(
		PRISONER_OF_WAR_REGISTRY_H_ID,
		PRISONER_OF_WAR_REGISTRY_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		LEGACY_ICN,
		LEGACY_USERNAME,
		LEGACY_ENTERED_DATE,
		POW_STDYESNO_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PRISONER_OF_WAR_REGISTRY_H_S.nextval,
		:old.PRISONER_OF_WAR_REGISTRY_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.PERSON_ID,
		:old.REGISTRY_TRAIT_ID,
		:old.LEGACY_ICN,
		:old.LEGACY_USERNAME,
		:old.LEGACY_ENTERED_DATE,
		:old.POW_STDYESNO_ID,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PRISONER_OF_WAR_REGISTRY_H
		(
		PRISONER_OF_WAR_REGISTRY_H_ID,
		PRISONER_OF_WAR_REGISTRY_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		LEGACY_ICN,
		LEGACY_USERNAME,
		LEGACY_ENTERED_DATE,
		POW_STDYESNO_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PRISONER_OF_WAR_REGISTRY_H_S.nextval,
		:new.PRISONER_OF_WAR_REGISTRY_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.PERSON_ID,
		:new.REGISTRY_TRAIT_ID,
		:new.LEGACY_ICN,
		:new.LEGACY_USERNAME,
		:new.LEGACY_ENTERED_DATE,
		:new.POW_STDYESNO_ID,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end POW_REGISTRY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PRISONER_OF_WAR_REGISTRY FOR ADR.PRISONER_OF_WAR_REGISTRY;


ALTER TABLE ADR.PRISONER_OF_WAR_REGISTRY ADD (
  CONSTRAINT POW_PK
  PRIMARY KEY
  (PRISONER_OF_WAR_REGISTRY_ID)
  USING INDEX ADR.POW_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PRISONER_OF_WAR_REGISTRY ADD (
  CONSTRAINT POW_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT POW_REGTRAIT_FK 
  FOREIGN KEY (REGISTRY_TRAIT_ID) 
  REFERENCES ADR.REGISTRY_TRAIT (REGISTRY_TRAIT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY TO ADREXT;

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PRISONER_OF_WAR_REGISTRY TO ADR_RW;

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY TO ES_R;

GRANT INSERT, SELECT ON ADR.PRISONER_OF_WAR_REGISTRY TO ES_RW;
ALTER TABLE ADR.PRISONER_OF_WAR_REGISTRY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PRISONER_OF_WAR_REGISTRY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PRISONER_OF_WAR_REGISTRY_H
(
  PRISONER_OF_WAR_REGISTRY_H_ID  NUMBER(20)     NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  PRISONER_OF_WAR_REGISTRY_ID    NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20),
  REGISTRY_TRAIT_ID              NUMBER(20),
  POW_STDYESNO_ID                NUMBER(20),
  LEGACY_ICN                     VARCHAR2(30 BYTE),
  LEGACY_USERNAME                VARCHAR2(30 BYTE),
  LEGACY_ENTERED_DATE            DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PRISONER_OF_WAR_REGISTRY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.PRISONER_OF_WAR_REGISTRY_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.PRISONER_OF_WAR_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PRISONER_OF_WAR_REGISTRY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.POWH_PK ON ADR.PRISONER_OF_WAR_REGISTRY_H
(PRISONER_OF_WAR_REGISTRY_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWH_POWREGID_NU_I ON ADR.PRISONER_OF_WAR_REGISTRY_H
(PRISONER_OF_WAR_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWH_PRSNID_NU_I ON ADR.PRISONER_OF_WAR_REGISTRY_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.POWH_REGTRAIT_NU_I ON ADR.PRISONER_OF_WAR_REGISTRY_H
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PRISONER_OF_WAR_REGISTRY_H FOR ADR.PRISONER_OF_WAR_REGISTRY_H;


ALTER TABLE ADR.PRISONER_OF_WAR_REGISTRY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_011
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT POWH_PK
  PRIMARY KEY
  (PRISONER_OF_WAR_REGISTRY_H_ID)
  USING INDEX ADR.POWH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY_H TO ADR_R;

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY_H TO ADR_RW;

GRANT SELECT ON ADR.PRISONER_OF_WAR_REGISTRY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PRISONER_OF_WAR_REGISTRY_H TO ES_RW;
ALTER TABLE ADR.PURPLE_HEART_REGISTRY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PURPLE_HEART_REGISTRY CASCADE CONSTRAINTS;

CREATE TABLE ADR.PURPLE_HEART_REGISTRY
(
  PURPLE_HEART_REGISTRY_ID       NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20),
  REGISTRY_TRAIT_ID              NUMBER(20),
  PH_STATUS_TYPE_ID              NUMBER(20),
  STD_REJECTEDREMARKS_ID         NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  PURPLE_HEART_IND               VARCHAR2(1 BYTE),
  PH_STATUS_LAST_EDIT_DATE       DATE,
  PH_STATUS_LAST_EDIT_USERNAME   VARCHAR2(30 BYTE),
  ASSIGNED_CONTACT_REP_USERNAME  VARCHAR2(30 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PURPLE_HEART_REGISTRY IS 'The table contains data about persons who received Purple Heart decorations. The data originates from the veteran, military documentation, VAMC, and VBA. This table corresponds to the Purple Heart Registry File. All persons in the POW registry file are enumerated and a record is created for them in this table with full details. The Enrollment application uses some of the information in this table as a factor in enrollment calculations. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.PURPLE_HEART_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.PH_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.STD_REJECTEDREMARKS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.PURPLE_HEART_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRPLHRTREG_PK ON ADR.PURPLE_HEART_REGISTRY
(PURPLE_HEART_REGISTRY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREG_PRSN_FK_I ON ADR.PURPLE_HEART_REGISTRY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREG_REGTRAIT_FK_I ON ADR.PURPLE_HEART_REGISTRY
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREG_STDINST_FK_I ON ADR.PURPLE_HEART_REGISTRY
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREG_STDREJRMRK_FK_I ON ADR.PURPLE_HEART_REGISTRY
(STD_REJECTEDREMARKS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREG_STD_DCRNSTAT_FK_I ON ADR.PURPLE_HEART_REGISTRY
(PH_STATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.PH_REGISTRY_AUDIR_TRGR
after Update or Delete or Insert
on ADR.PURPLE_HEART_REGISTRY
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into PURPLE_HEART_REGISTRY_H
		(
		PURPLE_HEART_REGISTRY_H_ID,
		PURPLE_HEART_REGISTRY_ID,
		PH_STATUS_TYPE_ID,
		STD_INSTITUTION_ID,
                STD_REJECTEDREMARKS_ID,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		PH_STATUS_LAST_EDIT_USERNAME,
		PH_STATUS_LAST_EDIT_DATE,
		ASSIGNED_CONTACT_REP_USERNAME,
		PURPLE_HEART_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PURPLE_HEART_REGISTRY_H_S.nextval,
		:old.PURPLE_HEART_REGISTRY_ID,
		:old.PH_STATUS_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.STD_REJECTEDREMARKS_ID,
		:old.PERSON_ID,
		:old.REGISTRY_TRAIT_ID,
		:old.PH_STATUS_LAST_EDIT_USERNAME,
		:old.PH_STATUS_LAST_EDIT_DATE,
		:old.ASSIGNED_CONTACT_REP_USERNAME,
		:old.PURPLE_HEART_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into PURPLE_HEART_REGISTRY_H
		(
		PURPLE_HEART_REGISTRY_H_ID,
		PURPLE_HEART_REGISTRY_ID,
		PH_STATUS_TYPE_ID,
		STD_INSTITUTION_ID,
		STD_REJECTEDREMARKS_ID,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		PH_STATUS_LAST_EDIT_USERNAME,
		PH_STATUS_LAST_EDIT_DATE,
		ASSIGNED_CONTACT_REP_USERNAME,
		PURPLE_HEART_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		PURPLE_HEART_REGISTRY_H_S.nextval,
		:new.PURPLE_HEART_REGISTRY_ID,
		:new.PH_STATUS_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.STD_REJECTEDREMARKS_ID,
		:new.PERSON_ID,
		:new.REGISTRY_TRAIT_ID,
		:new.PH_STATUS_LAST_EDIT_USERNAME,
		:new.PH_STATUS_LAST_EDIT_DATE,
		:new.ASSIGNED_CONTACT_REP_USERNAME,
		:new.PURPLE_HEART_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end PH_REGISTRY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM PURPLE_HEART_REGISTRY FOR ADR.PURPLE_HEART_REGISTRY;


ALTER TABLE ADR.PURPLE_HEART_REGISTRY ADD (
  CONSTRAINT PRPLHRTREG_PK
  PRIMARY KEY
  (PURPLE_HEART_REGISTRY_ID)
  USING INDEX ADR.PRPLHRTREG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.PURPLE_HEART_REGISTRY ADD (
  CONSTRAINT PRPLHRTREG_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT PRPLHRTREG_REGTRAIT_FK 
  FOREIGN KEY (REGISTRY_TRAIT_ID) 
  REFERENCES ADR.REGISTRY_TRAIT (REGISTRY_TRAIT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY TO ADREXT;

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.PURPLE_HEART_REGISTRY TO ADR_RW;

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY TO ES_R;

GRANT INSERT, SELECT ON ADR.PURPLE_HEART_REGISTRY TO ES_RW;
ALTER TABLE ADR.PURPLE_HEART_REGISTRY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PURPLE_HEART_REGISTRY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.PURPLE_HEART_REGISTRY_H
(
  PURPLE_HEART_REGISTRY_H_ID     NUMBER(20),
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE),
  PURPLE_HEART_REGISTRY_ID       NUMBER(20),
  PERSON_ID                      NUMBER(20),
  REGISTRY_TRAIT_ID              NUMBER(20),
  PH_STATUS_TYPE_ID              NUMBER(20),
  STD_REJECTEDREMARKS_ID         NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  PURPLE_HEART_IND               VARCHAR2(1 BYTE),
  ASSIGNED_CONTACT_REP_USERNAME  VARCHAR2(30 BYTE),
  PH_STATUS_LAST_EDIT_USERNAME   VARCHAR2(30 BYTE),
  PH_STATUS_LAST_EDIT_DATE       DATE,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE            DATE,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE           DATE,
  RECORD_MODIFIED_COUNT          NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PURPLE_HEART_REGISTRY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.PURPLE_HEART_REGISTRY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.PURPLE_HEART_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.PH_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.STD_REJECTEDREMARKS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.PURPLE_HEART_IND IS 'Retrofitted.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PURPLE_HEART_REGISTRY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PRPLHRTREH_PK ON ADR.PURPLE_HEART_REGISTRY_H
(PURPLE_HEART_REGISTRY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREH_PRPLHRTRE_NU_I ON ADR.PURPLE_HEART_REGISTRY_H
(PURPLE_HEART_REGISTRY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREH_PRSNID_NU_I ON ADR.PURPLE_HEART_REGISTRY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.PRPLHRTREH_REGTRAIT_NU_I ON ADR.PURPLE_HEART_REGISTRY_H
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PURPLE_HEART_REGISTRY_H FOR ADR.PURPLE_HEART_REGISTRY_H;


ALTER TABLE ADR.PURPLE_HEART_REGISTRY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_008
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066031
  CHECK ("PURPLE_HEART_REGISTRY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066032
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066033
  CHECK ("PURPLE_HEART_REGISTRY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066034
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066035
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066036
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066037
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066038
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT PRPLHRTREH_PK
  PRIMARY KEY
  (PURPLE_HEART_REGISTRY_H_ID)
  USING INDEX ADR.PRPLHRTREH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY_H TO ADR_R;

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY_H TO ADR_RW;

GRANT SELECT ON ADR.PURPLE_HEART_REGISTRY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.PURPLE_HEART_REGISTRY_H TO ES_RW;
ALTER TABLE ADR.PURPOSE_OF_USE_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.PURPOSE_OF_USE_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.PURPOSE_OF_USE_TYPE
(
  PURPOSE_OF_USE_TYPE_ID  NUMBER(20),
  CODE                    VARCHAR2(2 BYTE)      NOT NULL,
  NAME                    VARCHAR2(50 BYTE)     NOT NULL,
  DESCRIPTION             VARCHAR2(100 BYTE),
  ISACTIVE                CHAR(1 BYTE)          NOT NULL,
  INACTIVE_DATE           DATE,
  STD_ADDRESSTYPE_ID      NUMBER(20),
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.PURPOSE_OF_USE_TYPE IS 'lookup table to hold purpose of use list for different address type';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.PURPOSE_OF_USE_TYPE_ID IS 'PRIMARY KEY.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.CODE IS 'LOOKUP CODE SHORT NAME VALUE.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.NAME IS 'LOOKUP CODE LONG NAME VALUE.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.DESCRIPTION IS 'LOOKUP CODE DESCRIPTION.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.ISACTIVE IS 'FLAG TO SHOW ACTIVE/INACTIVE';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.INACTIVE_DATE IS 'INACTIVE DATE';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.STD_ADDRESSTYPE_ID IS 'ADDRESS TYPE ID';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.PURPOSE_OF_USE_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.PURPOSE_OF_USE_TYPE_PK ON ADR.PURPOSE_OF_USE_TYPE
(PURPOSE_OF_USE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM PURPOSE_OF_USE_TYPE FOR ADR.PURPOSE_OF_USE_TYPE;


ALTER TABLE ADR.PURPOSE_OF_USE_TYPE ADD (
  CONSTRAINT PURPOSE_OF_USE_TYPE_PK
  PRIMARY KEY
  (PURPOSE_OF_USE_TYPE_ID)
  USING INDEX ADR.PURPOSE_OF_USE_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.PURPOSE_OF_USE_TYPE TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.PURPOSE_OF_USE_TYPE TO ADR_RW;
ALTER TABLE ADR.QRTZ_BLOB_TRIGGERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_BLOB_TRIGGERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_BLOB_TRIGGERS
(
  TRIGGER_NAME   VARCHAR2(80 BYTE)              NOT NULL,
  TRIGGER_GROUP  VARCHAR2(80 BYTE)              NOT NULL,
  BLOB_DATA      BLOB
)
LOB (BLOB_DATA) STORE AS (
  TABLESPACE  S_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128K
                  NEXT             128K
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE UNIQUE INDEX ADR.QRTZ_BLOBTRGGR_PK ON ADR.QRTZ_BLOB_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_BLOB_TRIGGERS FOR ADR.QRTZ_BLOB_TRIGGERS;


ALTER TABLE ADR.QRTZ_BLOB_TRIGGERS ADD (
  CONSTRAINT QRTZ_BLOBTRGGR_PK
  PRIMARY KEY
  (TRIGGER_NAME, TRIGGER_GROUP)
  USING INDEX ADR.QRTZ_BLOBTRGGR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_BLOB_TRIGGERS ADD (
  CONSTRAINT QRTZ_BLOBTRGGR_TRGGR_FK 
  FOREIGN KEY (TRIGGER_NAME, TRIGGER_GROUP) 
  REFERENCES ADR.QRTZ_TRIGGERS (TRIGGER_NAME,TRIGGER_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_BLOB_TRIGGERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_BLOB_TRIGGERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_BLOB_TRIGGERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_BLOB_TRIGGERS TO ES_RW;
ALTER TABLE ADR.QRTZ_CALENDARS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_CALENDARS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_CALENDARS
(
  CALENDAR_NAME  VARCHAR2(80 BYTE)              NOT NULL,
  CALENDAR       BLOB
)
LOB (CALENDAR) STORE AS (
  TABLESPACE  S_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128K
                  NEXT             128K
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.QRTZ_CLNDR_PK ON ADR.QRTZ_CALENDARS
(CALENDAR_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_CALENDARS FOR ADR.QRTZ_CALENDARS;


ALTER TABLE ADR.QRTZ_CALENDARS ADD (
  CONSTRAINT QRTZ_CLNDR_PK
  PRIMARY KEY
  (CALENDAR_NAME)
  USING INDEX ADR.QRTZ_CLNDR_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_CALENDARS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_CALENDARS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_CALENDARS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_CALENDARS TO ES_RW;
ALTER TABLE ADR.QRTZ_CRON_TRIGGERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_CRON_TRIGGERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_CRON_TRIGGERS
(
  TRIGGER_NAME     VARCHAR2(80 BYTE)            NOT NULL,
  TRIGGER_GROUP    VARCHAR2(80 BYTE)            NOT NULL,
  CRON_EXPRESSION  VARCHAR2(80 BYTE)            NOT NULL,
  TIME_ZONE_ID     VARCHAR2(80 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE UNIQUE INDEX ADR.QRTZ_CRNTRGGR_PK ON ADR.QRTZ_CRON_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_CRON_TRIGGERS FOR ADR.QRTZ_CRON_TRIGGERS;


ALTER TABLE ADR.QRTZ_CRON_TRIGGERS ADD (
  CONSTRAINT QRTZ_CRNTRGGR_PK
  PRIMARY KEY
  (TRIGGER_NAME, TRIGGER_GROUP)
  USING INDEX ADR.QRTZ_CRNTRGGR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_CRON_TRIGGERS ADD (
  CONSTRAINT QRTZ_CRNTRGGR_TRGGR_FK 
  FOREIGN KEY (TRIGGER_NAME, TRIGGER_GROUP) 
  REFERENCES ADR.QRTZ_TRIGGERS (TRIGGER_NAME,TRIGGER_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_CRON_TRIGGERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_CRON_TRIGGERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_CRON_TRIGGERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_CRON_TRIGGERS TO ES_RW;
ALTER TABLE ADR.QRTZ_FIRED_TRIGGERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_FIRED_TRIGGERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_FIRED_TRIGGERS
(
  ENTRY_ID           VARCHAR2(95 BYTE)          NOT NULL,
  TRIGGER_NAME       VARCHAR2(80 BYTE)          NOT NULL,
  TRIGGER_GROUP      VARCHAR2(80 BYTE)          NOT NULL,
  IS_VOLATILE        VARCHAR2(1 BYTE)           NOT NULL,
  INSTANCE_NAME      VARCHAR2(80 BYTE)          NOT NULL,
  FIRED_TIME         NUMBER(13)                 NOT NULL,
  STATE              VARCHAR2(16 BYTE)          NOT NULL,
  JOB_NAME           VARCHAR2(80 BYTE),
  JOB_GROUP          VARCHAR2(80 BYTE),
  IS_STATEFUL        VARCHAR2(1 BYTE),
  REQUESTS_RECOVERY  VARCHAR2(1 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.QRTZ_FIRED_TRIGGERS.STATE IS 'BR9 City is defined as the name of a city used for an address.';


CREATE INDEX ADR.IDX_QRTZ_FT_JOB_GROUP ON ADR.QRTZ_FIRED_TRIGGERS
(JOB_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_JOB_NAME ON ADR.QRTZ_FIRED_TRIGGERS
(JOB_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_JOB_REQ_RECOVERY ON ADR.QRTZ_FIRED_TRIGGERS
(REQUESTS_RECOVERY)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_JOB_STATEFUL ON ADR.QRTZ_FIRED_TRIGGERS
(IS_STATEFUL)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_TRIG_GROUP ON ADR.QRTZ_FIRED_TRIGGERS
(TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_TRIG_INST_NAME ON ADR.QRTZ_FIRED_TRIGGERS
(INSTANCE_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_TRIG_NAME ON ADR.QRTZ_FIRED_TRIGGERS
(TRIGGER_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_TRIG_NM_GP ON ADR.QRTZ_FIRED_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_FT_TRIG_VOLATILE ON ADR.QRTZ_FIRED_TRIGGERS
(IS_VOLATILE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.QRTZ_FRDTRGGR_PK ON ADR.QRTZ_FIRED_TRIGGERS
(ENTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_FIRED_TRIGGERS FOR ADR.QRTZ_FIRED_TRIGGERS;


ALTER TABLE ADR.QRTZ_FIRED_TRIGGERS ADD (
  CONSTRAINT QRTZ_FRDTRGGR_PK
  PRIMARY KEY
  (ENTRY_ID)
  USING INDEX ADR.QRTZ_FRDTRGGR_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_FIRED_TRIGGERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_FIRED_TRIGGERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_FIRED_TRIGGERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_FIRED_TRIGGERS TO ES_RW;
ALTER TABLE ADR.QRTZ_JOB_DETAILS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_JOB_DETAILS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_JOB_DETAILS
(
  JOB_NAME           VARCHAR2(80 BYTE)          NOT NULL,
  JOB_GROUP          VARCHAR2(80 BYTE)          NOT NULL,
  DESCRIPTION        VARCHAR2(120 BYTE),
  JOB_CLASS_NAME     VARCHAR2(128 BYTE)         NOT NULL,
  IS_DURABLE         VARCHAR2(1 BYTE)           NOT NULL,
  IS_VOLATILE        VARCHAR2(1 BYTE)           NOT NULL,
  IS_STATEFUL        VARCHAR2(1 BYTE)           NOT NULL,
  REQUESTS_RECOVERY  VARCHAR2(1 BYTE)           NOT NULL,
  JOB_DATA           BLOB
)
LOB (JOB_DATA) STORE AS (
  TABLESPACE  S_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128K
                  NEXT             128K
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE INDEX ADR.IDX_QRTZ_J_REQ_RECOVERY ON ADR.QRTZ_JOB_DETAILS
(REQUESTS_RECOVERY)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.QRTZ_JBDTL_PK ON ADR.QRTZ_JOB_DETAILS
(JOB_NAME, JOB_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_JOB_DETAILS FOR ADR.QRTZ_JOB_DETAILS;


ALTER TABLE ADR.QRTZ_JOB_DETAILS ADD (
  CONSTRAINT QRTZ_JBDTL_PK
  PRIMARY KEY
  (JOB_NAME, JOB_GROUP)
  USING INDEX ADR.QRTZ_JBDTL_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_JOB_DETAILS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_JOB_DETAILS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_JOB_DETAILS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_JOB_DETAILS TO ES_RW;
ALTER TABLE ADR.QRTZ_JOB_LISTENERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_JOB_LISTENERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_JOB_LISTENERS
(
  JOB_NAME      VARCHAR2(80 BYTE)               NOT NULL,
  JOB_GROUP     VARCHAR2(80 BYTE)               NOT NULL,
  JOB_LISTENER  VARCHAR2(80 BYTE)               NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.QRTZ_JBLSTNR_PK ON ADR.QRTZ_JOB_LISTENERS
(JOB_NAME, JOB_GROUP, JOB_LISTENER)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_JOB_LISTENERS FOR ADR.QRTZ_JOB_LISTENERS;


ALTER TABLE ADR.QRTZ_JOB_LISTENERS ADD (
  CONSTRAINT QRTZ_JBLSTNR_PK
  PRIMARY KEY
  (JOB_NAME, JOB_GROUP, JOB_LISTENER)
  USING INDEX ADR.QRTZ_JBLSTNR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_JOB_LISTENERS ADD (
  CONSTRAINT QRTZ_JBLSTNR_JBDTL_FK 
  FOREIGN KEY (JOB_NAME, JOB_GROUP) 
  REFERENCES ADR.QRTZ_JOB_DETAILS (JOB_NAME,JOB_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_JOB_LISTENERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_JOB_LISTENERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_JOB_LISTENERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_JOB_LISTENERS TO ES_RW;
ALTER TABLE ADR.QRTZ_LOCKS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_LOCKS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_LOCKS
(
  LOCK_NAME  VARCHAR2(40 BYTE)                  NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.QRTZ_LOCKS_PK ON ADR.QRTZ_LOCKS
(LOCK_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_LOCKS FOR ADR.QRTZ_LOCKS;


ALTER TABLE ADR.QRTZ_LOCKS ADD (
  CONSTRAINT QRTZ_LOCKS_PK
  PRIMARY KEY
  (LOCK_NAME)
  USING INDEX ADR.QRTZ_LOCKS_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_LOCKS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_LOCKS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_LOCKS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_LOCKS TO ES_RW;
ALTER TABLE ADR.QRTZ_PAUSED_TRIGGER_GRPS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_PAUSED_TRIGGER_GRPS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_PAUSED_TRIGGER_GRPS
(
  TRIGGER_GROUP  VARCHAR2(80 BYTE)              NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.QRTZ_PUSDTRGGR_PK ON ADR.QRTZ_PAUSED_TRIGGER_GRPS
(TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_PAUSED_TRIGGER_GRPS FOR ADR.QRTZ_PAUSED_TRIGGER_GRPS;


ALTER TABLE ADR.QRTZ_PAUSED_TRIGGER_GRPS ADD (
  CONSTRAINT QRTZ_PUSDTRGGR_PK
  PRIMARY KEY
  (TRIGGER_GROUP)
  USING INDEX ADR.QRTZ_PUSDTRGGR_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_PAUSED_TRIGGER_GRPS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_PAUSED_TRIGGER_GRPS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_PAUSED_TRIGGER_GRPS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_PAUSED_TRIGGER_GRPS TO ES_RW;
ALTER TABLE ADR.QRTZ_SCHEDULER_STATE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_SCHEDULER_STATE CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_SCHEDULER_STATE
(
  INSTANCE_NAME      VARCHAR2(80 BYTE)          NOT NULL,
  LAST_CHECKIN_TIME  NUMBER(13)                 NOT NULL,
  CHECKIN_INTERVAL   NUMBER(13)                 NOT NULL,
  RECOVERER          VARCHAR2(80 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.QRTZ_SCHDST_PK ON ADR.QRTZ_SCHEDULER_STATE
(INSTANCE_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_SCHEDULER_STATE FOR ADR.QRTZ_SCHEDULER_STATE;


ALTER TABLE ADR.QRTZ_SCHEDULER_STATE ADD (
  CONSTRAINT QRTZ_SCHDST_PK
  PRIMARY KEY
  (INSTANCE_NAME)
  USING INDEX ADR.QRTZ_SCHDST_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_SCHEDULER_STATE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_SCHEDULER_STATE TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_SCHEDULER_STATE TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_SCHEDULER_STATE TO ES_RW;
ALTER TABLE ADR.QRTZ_SIMPLE_TRIGGERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_SIMPLE_TRIGGERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_SIMPLE_TRIGGERS
(
  TRIGGER_NAME     VARCHAR2(80 BYTE)            NOT NULL,
  TRIGGER_GROUP    VARCHAR2(80 BYTE)            NOT NULL,
  REPEAT_COUNT     NUMBER(7)                    NOT NULL,
  REPEAT_INTERVAL  NUMBER(12)                   NOT NULL,
  TIMES_TRIGGERED  NUMBER(7)                    NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE UNIQUE INDEX ADR.QRTZ_SMPTRGGR_PK ON ADR.QRTZ_SIMPLE_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_SIMPLE_TRIGGERS FOR ADR.QRTZ_SIMPLE_TRIGGERS;


ALTER TABLE ADR.QRTZ_SIMPLE_TRIGGERS ADD (
  CONSTRAINT QRTZ_SMPTRGGR_PK
  PRIMARY KEY
  (TRIGGER_NAME, TRIGGER_GROUP)
  USING INDEX ADR.QRTZ_SMPTRGGR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_SIMPLE_TRIGGERS ADD (
  CONSTRAINT QRTZ_SMPTRGGR_TRGGR_FK 
  FOREIGN KEY (TRIGGER_NAME, TRIGGER_GROUP) 
  REFERENCES ADR.QRTZ_TRIGGERS (TRIGGER_NAME,TRIGGER_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_SIMPLE_TRIGGERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_SIMPLE_TRIGGERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_SIMPLE_TRIGGERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_SIMPLE_TRIGGERS TO ES_RW;
ALTER TABLE ADR.QRTZ_TRIGGERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_TRIGGERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_TRIGGERS
(
  TRIGGER_NAME    VARCHAR2(80 BYTE)             NOT NULL,
  TRIGGER_GROUP   VARCHAR2(80 BYTE)             NOT NULL,
  JOB_NAME        VARCHAR2(80 BYTE)             NOT NULL,
  JOB_GROUP       VARCHAR2(80 BYTE)             NOT NULL,
  IS_VOLATILE     VARCHAR2(1 BYTE)              NOT NULL,
  DESCRIPTION     VARCHAR2(120 BYTE),
  NEXT_FIRE_TIME  NUMBER(13),
  PREV_FIRE_TIME  NUMBER(13),
  TRIGGER_STATE   VARCHAR2(16 BYTE)             NOT NULL,
  TRIGGER_TYPE    VARCHAR2(8 BYTE)              NOT NULL,
  START_TIME      NUMBER(13)                    NOT NULL,
  END_TIME        NUMBER(13),
  CALENDAR_NAME   VARCHAR2(80 BYTE),
  MISFIRE_INSTR   NUMBER(2),
  JOB_DATA        BLOB
)
LOB (JOB_DATA) STORE AS (
  TABLESPACE  S_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          128K
                  NEXT             128K
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE INDEX ADR.IDX_QRTZ_T_NEXT_FIRE_TIME ON ADR.QRTZ_TRIGGERS
(NEXT_FIRE_TIME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_T_NFT_ST ON ADR.QRTZ_TRIGGERS
(NEXT_FIRE_TIME, TRIGGER_STATE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_T_STATE ON ADR.QRTZ_TRIGGERS
(TRIGGER_STATE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IDX_QRTZ_T_VOLATILE ON ADR.QRTZ_TRIGGERS
(IS_VOLATILE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.QRTZ_TRGGR_PK ON ADR.QRTZ_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_TRIGGERS FOR ADR.QRTZ_TRIGGERS;


ALTER TABLE ADR.QRTZ_TRIGGERS ADD (
  CONSTRAINT QRTZ_TRGGR_PK
  PRIMARY KEY
  (TRIGGER_NAME, TRIGGER_GROUP)
  USING INDEX ADR.QRTZ_TRGGR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_TRIGGERS ADD (
  CONSTRAINT QRTZ_TRGGR_JBDTL_FK 
  FOREIGN KEY (JOB_NAME, JOB_GROUP) 
  REFERENCES ADR.QRTZ_JOB_DETAILS (JOB_NAME,JOB_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_TRIGGERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_TRIGGERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_TRIGGERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_TRIGGERS TO ES_RW;
ALTER TABLE ADR.QRTZ_TRIGGER_LISTENERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.QRTZ_TRIGGER_LISTENERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.QRTZ_TRIGGER_LISTENERS
(
  TRIGGER_NAME      VARCHAR2(80 BYTE)           NOT NULL,
  TRIGGER_GROUP     VARCHAR2(80 BYTE)           NOT NULL,
  TRIGGER_LISTENER  VARCHAR2(80 BYTE)           NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING
ENABLE ROW MOVEMENT;


CREATE UNIQUE INDEX ADR.QRTZ_TRGGRLSTNR_PK ON ADR.QRTZ_TRIGGER_LISTENERS
(TRIGGER_NAME, TRIGGER_GROUP, TRIGGER_LISTENER)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM QRTZ_TRIGGER_LISTENERS FOR ADR.QRTZ_TRIGGER_LISTENERS;


ALTER TABLE ADR.QRTZ_TRIGGER_LISTENERS ADD (
  CONSTRAINT QRTZ_TRGGRLSTNR_PK
  PRIMARY KEY
  (TRIGGER_NAME, TRIGGER_GROUP, TRIGGER_LISTENER)
  USING INDEX ADR.QRTZ_TRGGRLSTNR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.QRTZ_TRIGGER_LISTENERS ADD (
  CONSTRAINT QRTZ_TRGGRLSTNR_TRGGR_FK 
  FOREIGN KEY (TRIGGER_NAME, TRIGGER_GROUP) 
  REFERENCES ADR.QRTZ_TRIGGERS (TRIGGER_NAME,TRIGGER_GROUP)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.QRTZ_TRIGGER_LISTENERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.QRTZ_TRIGGER_LISTENERS TO ADR_RW;

GRANT SELECT ON ADR.QRTZ_TRIGGER_LISTENERS TO ES_R;

GRANT INSERT, SELECT ON ADR.QRTZ_TRIGGER_LISTENERS TO ES_RW;
ALTER TABLE ADR.RATED_DISABILITY_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RATED_DISABILITY_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.RATED_DISABILITY_DETAIL
(
  RATED_DISABILITY_DETAIL_ID    NUMBER(20),
  DIAGNOSTIC_EXTREMITY_TYPE_ID  NUMBER(20),
  STD_DISABILITYCONDITION_ID    NUMBER(20),
  SERVICE_CONNECTED_ID          NUMBER(20),
  RATED_DISABILITY_IS_SC_IND    VARCHAR2(3 BYTE),
  DISABILITY_PERCENT            NUMBER(3),
  DISABILITY_START_DATE         DATE,
  ORIG_EFCTV_RATING_DATE_TXT    VARCHAR2(14 BYTE),
  RATING_DATE_TXT               VARCHAR2(14 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RATED_DISABILITY_DETAIL IS 'The table stores information about a persons disability or disabilities, including whether the disability is service connected, the percentage disabled the person is, the effective dates, and the code associated with the disability. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RATED_DISABILITY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.DIAGNOSTIC_EXTREMITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.STD_DISABILITYCONDITION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.SERVICE_CONNECTED_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RATED_DISABILITY_IS_SC_IND IS 'BR7255 Rated disability SC indicator is defined as a yes/no code that identifies if the rated disability is service connected or not.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.DISABILITY_PERCENT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.DISABILITY_START_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RATING_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RATEDISDET_PK ON ADR.RATED_DISABILITY_DETAIL
(RATED_DISABILITY_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RATEDISDET_STDDSBLCND_FK_I ON ADR.RATED_DISABILITY_DETAIL
(STD_DISABILITYCONDITION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RATEDISDET_SVCCON_FK_I ON ADR.RATED_DISABILITY_DETAIL
(SERVICE_CONNECTED_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RATEDISDET_XTRMTY_FK_I ON ADR.RATED_DISABILITY_DETAIL
(DIAGNOSTIC_EXTREMITY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.RATED_DSBLTY_DET_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."RATED_DISABILITY_DETAIL" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into RATED_DISABILITY_DETAIL_H
		(
		RATED_DISABILITY_DETAIL_H_ID,
		RATED_DISABILITY_DETAIL_ID,
		RATED_DISABILITY_IS_SC_IND,
		DISABILITY_PERCENT,
		DISABILITY_START_DATE,
		ORIG_EFCTV_RATING_DATE_TXT,
		RATING_DATE_TXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_DISABILITYCONDITION_ID,
		SERVICE_CONNECTED_ID,
		RECORD_MODIFIED_COUNT,
		DIAGNOSTIC_EXTREMITY_TYPE_ID,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		RATED_DISABILITY_DETAIL_H_S.nextval,
		:old.RATED_DISABILITY_DETAIL_ID,
		:old.RATED_DISABILITY_IS_SC_IND,
		:old.DISABILITY_PERCENT,
		:old.DISABILITY_START_DATE,
		:old.ORIG_EFCTV_RATING_DATE_TXT,
		:old.RATING_DATE_TXT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.STD_DISABILITYCONDITION_ID,
		:old.SERVICE_CONNECTED_ID,
		:old.RECORD_MODIFIED_COUNT,
		:old.DIAGNOSTIC_EXTREMITY_TYPE_ID,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into RATED_DISABILITY_DETAIL_H
		(
		RATED_DISABILITY_DETAIL_H_ID,
		RATED_DISABILITY_DETAIL_ID,
		RATED_DISABILITY_IS_SC_IND,
		DISABILITY_PERCENT,
		DISABILITY_START_DATE,
		ORIG_EFCTV_RATING_DATE_TXT,
		RATING_DATE_TXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_DISABILITYCONDITION_ID,
		SERVICE_CONNECTED_ID,
		RECORD_MODIFIED_COUNT,
		DIAGNOSTIC_EXTREMITY_TYPE_ID,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		RATED_DISABILITY_DETAIL_H_S.nextval,
		:new.RATED_DISABILITY_DETAIL_ID,
		:new.RATED_DISABILITY_IS_SC_IND,
		:new.DISABILITY_PERCENT,
		:new.DISABILITY_START_DATE,
		:new.ORIG_EFCTV_RATING_DATE_TXT,
		:new.RATING_DATE_TXT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.STD_DISABILITYCONDITION_ID,
		:new.SERVICE_CONNECTED_ID,
		:new.RECORD_MODIFIED_COUNT,
		:new.DIAGNOSTIC_EXTREMITY_TYPE_ID,
		mDML
		);
	end if;

end RATED_DSBLTY_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM RATED_DISABILITY_DETAIL FOR ADR.RATED_DISABILITY_DETAIL;


ALTER TABLE ADR.RATED_DISABILITY_DETAIL ADD (
  CONSTRAINT SYS_C0066150
  CHECK ("RATED_DISABILITY_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066151
  CHECK ("STD_DISABILITYCONDITION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066152
  CHECK ("SERVICE_CONNECTED_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066153
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066154
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066155
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066156
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066157
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT RATEDISDET_PK
  PRIMARY KEY
  (RATED_DISABILITY_DETAIL_ID)
  USING INDEX ADR.RATEDISDET_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RATED_DISABILITY_DETAIL ADD (
  CONSTRAINT RATEDISDET_SVCCON_FK 
  FOREIGN KEY (SERVICE_CONNECTED_ID) 
  REFERENCES ADR.SERVICE_CONNECTED (SERVICE_CONNECTED_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RATED_DISABILITY_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RATED_DISABILITY_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.RATED_DISABILITY_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.RATED_DISABILITY_DETAIL TO ES_RW;
ALTER TABLE ADR.RATED_DISABILITY_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RATED_DISABILITY_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.RATED_DISABILITY_DETAIL_H
(
  RATED_DISABILITY_DETAIL_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE),
  RATED_DISABILITY_DETAIL_ID    NUMBER(20),
  DIAGNOSTIC_EXTREMITY_TYPE_ID  NUMBER(20),
  STD_DISABILITYCONDITION_ID    NUMBER(20),
  SERVICE_CONNECTED_ID          NUMBER(20),
  RATED_DISABILITY_IS_SC_IND    VARCHAR2(3 BYTE),
  DISABILITY_PERCENT            NUMBER(3),
  DISABILITY_START_DATE         DATE,
  ORIG_EFCTV_RATING_DATE_TXT    VARCHAR2(14 BYTE),
  RATING_DATE_TXT               VARCHAR2(14 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE           DATE,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE          DATE,
  RECORD_MODIFIED_COUNT         NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RATED_DISABILITY_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RATED_DISABILITY_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RATED_DISABILITY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.DIAGNOSTIC_EXTREMITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.STD_DISABILITYCONDITION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.SERVICE_CONNECTED_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RATED_DISABILITY_IS_SC_IND IS 'BR7255 Rated disability SC indicator is defined as a yes/no code that identifies if the rated disability is service connected or not.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.DISABILITY_PERCENT IS 'BR1633 % Disability is defined as the percentage at which the VA rated this disability for this patient.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RATING_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RATED_DISABILITY_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RATEDISDTH_PK ON ADR.RATED_DISABILITY_DETAIL_H
(RATED_DISABILITY_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RATEDISDTH_SVCCON_NU_I ON ADR.RATED_DISABILITY_DETAIL_H
(SERVICE_CONNECTED_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RATED_DISABILITY_DETAIL_H FOR ADR.RATED_DISABILITY_DETAIL_H;


ALTER TABLE ADR.RATED_DISABILITY_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_041
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066724
  CHECK ("RATED_DISABILITY_DETAIL_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066725
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066726
  CHECK ("RATED_DISABILITY_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066727
  CHECK ("STD_DISABILITYCONDITION_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066728
  CHECK ("SERVICE_CONNECTED_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066729
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066730
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066731
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066732
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066733
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT RATEDISDTH_PK
  PRIMARY KEY
  (RATED_DISABILITY_DETAIL_H_ID)
  USING INDEX ADR.RATEDISDTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RATED_DISABILITY_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.RATED_DISABILITY_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.RATED_DISABILITY_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.RATED_DISABILITY_DETAIL_H TO ES_RW;
ALTER TABLE ADR.REGISTRY_LOAD_EXCEPTION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_LOAD_EXCEPTION CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_LOAD_EXCEPTION
(
  REGISTRY_LOAD_EXCEPTION_ID   NUMBER(20)       NOT NULL,
  REGISTRY_LOAD_STATISTICS_ID  NUMBER(20),
  RPT_EXCEPTION_TYPE_ID        NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  EXCEPTION_TXT                CLOB
)
LOB (EXCEPTION_TXT) STORE AS (
  TABLESPACE  M_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_LOAD_EXCEPTION IS 'Stores statistics captured during migration of the Registry data from the HEC legacy system to ADR. Number of records processed, number inserted, pass count, match count etc.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.REGISTRY_LOAD_EXCEPTION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.REGISTRY_LOAD_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_EXCEPTION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RGSTRLODX_PK ON ADR.REGISTRY_LOAD_EXCEPTION
(REGISTRY_LOAD_EXCEPTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RGSTRLODX_RGSTRYSTAT_FK_I ON ADR.REGISTRY_LOAD_EXCEPTION
(REGISTRY_LOAD_STATISTICS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RGSTRLODX_STD_RPTEXCPT_FK_I ON ADR.REGISTRY_LOAD_EXCEPTION
(RPT_EXCEPTION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_LOAD_EXCEPTION FOR ADR.REGISTRY_LOAD_EXCEPTION;


ALTER TABLE ADR.REGISTRY_LOAD_EXCEPTION ADD (
  CONSTRAINT RGSTRLODX_PK
  PRIMARY KEY
  (REGISTRY_LOAD_EXCEPTION_ID)
  USING INDEX ADR.RGSTRLODX_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.REGISTRY_LOAD_EXCEPTION ADD (
  CONSTRAINT RGSTRLODX_RGSTRYSTAT_FK 
  FOREIGN KEY (REGISTRY_LOAD_STATISTICS_ID) 
  REFERENCES ADR.REGISTRY_LOAD_STATISTICS (REGISTRY_LOAD_STATISTICS_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RGSTRLODX_STD_RPTEXCPT_FK 
  FOREIGN KEY (RPT_EXCEPTION_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTEXCEPTIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.REGISTRY_LOAD_EXCEPTION TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.REGISTRY_LOAD_EXCEPTION TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_LOAD_EXCEPTION TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_LOAD_EXCEPTION TO ES_RW;
ALTER TABLE ADR.REGISTRY_LOAD_STATISTICS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_LOAD_STATISTICS CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_LOAD_STATISTICS
(
  REGISTRY_LOAD_STATISTICS_ID  NUMBER(20)       NOT NULL,
  REGISTRY_TYPE_ID             NUMBER(20)       NOT NULL,
  PROCESSED_COUNT              NUMBER(10),
  PASS_COUNT                   NUMBER(10),
  MATCH_COUNT                  NUMBER(10),
  NO_MATCH_COUNT               NUMBER(10),
  AMBIGUOUS_MATCH_COUNT        NUMBER(10),
  INSERT_COUNT                 NUMBER(10),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_LOAD_STATISTICS IS 'Stores statistics captured during migration of the Registry data from the HEC legacy system to ADR. Number of records processed, number inserted, pass count, match count etc.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.REGISTRY_LOAD_STATISTICS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.REGISTRY_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_LOAD_STATISTICS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.REGSTAT_STD_REGTYPE_FK_I ON ADR.REGISTRY_LOAD_STATISTICS
(REGISTRY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RGSTRYSTAT_PK ON ADR.REGISTRY_LOAD_STATISTICS
(REGISTRY_LOAD_STATISTICS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_LOAD_STATISTICS FOR ADR.REGISTRY_LOAD_STATISTICS;


ALTER TABLE ADR.REGISTRY_LOAD_STATISTICS ADD (
  CONSTRAINT RGSTRYSTAT_PK
  PRIMARY KEY
  (REGISTRY_LOAD_STATISTICS_ID)
  USING INDEX ADR.RGSTRYSTAT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.REGISTRY_LOAD_STATISTICS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.REGISTRY_LOAD_STATISTICS TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_LOAD_STATISTICS TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_LOAD_STATISTICS TO ES_RW;
ALTER TABLE ADR.REGISTRY_TRAIT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_TRAIT CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_TRAIT
(
  REGISTRY_TRAIT_ID      NUMBER(20),
  REGISTRY_TYPE_ID       NUMBER(20),
  SSN                    VARCHAR2(20 BYTE),
  PREFIX                 VARCHAR2(12 BYTE),
  FIRST_NAME             VARCHAR2(30 BYTE),
  MIDDLE_NAME            VARCHAR2(30 BYTE),
  LAST_NAME              VARCHAR2(40 BYTE),
  SUFFIX                 VARCHAR2(12 BYTE),
  EDUCATIONAL_DEGREE     VARCHAR2(30 BYTE),
  DEATH_DATE_TXT         VARCHAR2(14 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE    DATE,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE   DATE,
  RECORD_MODIFIED_COUNT  NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_TRAIT IS 'Used to facilitate quick searching of the Registry information. This table contains a foreign key to the Registry type table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.REGISTRY_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.REGTRAIT_FNAME_FB_I ON ADR.REGISTRY_TRAIT
(UPPER("FIRST_NAME"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE INDEX ADR.REGTRAIT_LNAME_FB_I ON ADR.REGISTRY_TRAIT
(UPPER("LAST_NAME"))
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE 4 INSTANCES 1 );

CREATE UNIQUE INDEX ADR.REGTRAIT_PK ON ADR.REGISTRY_TRAIT
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.REGTRAIT_SSN_NU_I ON ADR.REGISTRY_TRAIT
(SSN)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.REGTRAIT_STD_REGTYPE_FK_I ON ADR.REGISTRY_TRAIT
(REGISTRY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.REGISTRY_TRAIT_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."REGISTRY_TRAIT" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into REGISTRY_TRAIT_H
		(
 		 REGISTRY_TRAIT_H_ID
 		,REGISTRY_TRAIT_ID
 		,REGISTRY_TYPE_ID
 		,SSN
		,FIRST_NAME
 		,LAST_NAME
		,PREFIX
		,MIDDLE_NAME
		,SUFFIX
		,EDUCATIONAL_DEGREE
		,DEATH_DATE_TXT
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		REGISTRY_TRAIT_H_S.nextval,
		:old.REGISTRY_TRAIT_ID,
		:old.REGISTRY_TYPE_ID,
		:old.SSN,
		:old.FIRST_NAME,
		:old.LAST_NAME,
		:old.PREFIX,
		:old.MIDDLE_NAME,
		:old.SUFFIX,
		:old.EDUCATIONAL_DEGREE,
		:old.DEATH_DATE_TXT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into REGISTRY_TRAIT_H
		(
 		 REGISTRY_TRAIT_H_ID
 		,REGISTRY_TRAIT_ID
 		,REGISTRY_TYPE_ID
 		,SSN
		,FIRST_NAME
 		,LAST_NAME
		,PREFIX
		,MIDDLE_NAME
		,SUFFIX
		,EDUCATIONAL_DEGREE
		,DEATH_DATE_TXT
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		REGISTRY_TRAIT_H_S.nextval,
		:new.REGISTRY_TRAIT_ID,
		:new.REGISTRY_TYPE_ID,
		:new.SSN,
		:new.FIRST_NAME,
		:new.LAST_NAME,
		:new.PREFIX,
		:new.MIDDLE_NAME,
		:new.SUFFIX,
		:new.EDUCATIONAL_DEGREE,
		:new.DEATH_DATE_TXT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end REGISTRY_TRAIT_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_TRAIT FOR ADR.REGISTRY_TRAIT;


ALTER TABLE ADR.REGISTRY_TRAIT ADD (
  CONSTRAINT SYS_C0066600
  CHECK ("REGISTRY_TRAIT_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066601
  CHECK ("REGISTRY_TYPE_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066602
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066603
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066604
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066605
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066606
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT REGTRAIT_PK
  PRIMARY KEY
  (REGISTRY_TRAIT_ID)
  USING INDEX ADR.REGTRAIT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.REGISTRY_TRAIT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.REGISTRY_TRAIT TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_TRAIT TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_TRAIT TO ES_RW;
ALTER TABLE ADR.REGISTRY_TRAIT_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_TRAIT_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_TRAIT_DETAIL
(
  REGISTRY_TRAIT_DETAIL_ID  NUMBER(20)          NOT NULL,
  REGISTRY_TRAIT_ID         NUMBER(20)          NOT NULL,
  MILITARY_SERVICE_NUMBER   VARCHAR2(20 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_TRAIT_DETAIL IS 'Contains the Military Service Number - this is required in cases where the veteran has rejoined the military after having been discharger';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.REGISTRY_TRAIT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.MILITARY_SERVICE_NUMBER IS 'Military Service Number is a Unique identifier provided to a person by the branch of the military with which he or she served. It is defined as the service number assigned during the veterans episodes of military service. The social security number and service number can be the same. ';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.REGTRAITDTL_MILSVCNBR_FB_I ON ADR.REGISTRY_TRAIT_DETAIL
(UPPER("MILITARY_SERVICE_NUMBER"))
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.REGTRAITDT_PK ON ADR.REGISTRY_TRAIT_DETAIL
(REGISTRY_TRAIT_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.REGTRAITDT_REGTRAIT_FK_I ON ADR.REGISTRY_TRAIT_DETAIL
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.REGISTRY_TRAIT_DET_audir_trgr
after Update or Delete or Insert
ON ADR.REGISTRY_TRAIT_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into REGISTRY_TRAIT_DETAIL_H
		(
 		REGISTRY_TRAIT_DETAIL_H_ID
 		,REGISTRY_TRAIT_DETAIL_ID
 		,REGISTRY_TRAIT_ID
 		,MILITARY_SERVICE_NUMBER
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		REGISTRY_TRAIT_DETAIL_H_S.nextval,
		:old.REGISTRY_TRAIT_DETAIL_ID,
		:old.REGISTRY_TRAIT_ID,
		:old.MILITARY_SERVICE_NUMBER,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into REGISTRY_TRAIT_DETAIL_H
		(
 		 REGISTRY_TRAIT_DETAIL_H_ID
 		,REGISTRY_TRAIT_DETAIL_ID
 		,REGISTRY_TRAIT_ID
 		,MILITARY_SERVICE_NUMBER
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		REGISTRY_TRAIT_DETAIL_H_S.nextval,
		:new.REGISTRY_TRAIT_DETAIL_ID,
		:new.REGISTRY_TRAIT_ID,
		:new.MILITARY_SERVICE_NUMBER,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end  REGISTRY_TRAIT_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_TRAIT_DETAIL FOR ADR.REGISTRY_TRAIT_DETAIL;


ALTER TABLE ADR.REGISTRY_TRAIT_DETAIL ADD (
  CONSTRAINT REGTRAITDT_PK
  PRIMARY KEY
  (REGISTRY_TRAIT_DETAIL_ID)
  USING INDEX ADR.REGTRAITDT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.REGISTRY_TRAIT_DETAIL ADD (
  CONSTRAINT REGTRAITDT_REGTRAIT_FK 
  FOREIGN KEY (REGISTRY_TRAIT_ID) 
  REFERENCES ADR.REGISTRY_TRAIT (REGISTRY_TRAIT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.REGISTRY_TRAIT_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.REGISTRY_TRAIT_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_TRAIT_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_TRAIT_DETAIL TO ES_RW;
ALTER TABLE ADR.REGISTRY_TRAIT_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_TRAIT_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_TRAIT_DETAIL_H
(
  REGISTRY_TRAIT_DETAIL_H_ID  NUMBER(20)        NOT NULL,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL,
  REGISTRY_TRAIT_DETAIL_ID    NUMBER(20),
  REGISTRY_TRAIT_ID           NUMBER(20),
  MILITARY_SERVICE_NUMBER     VARCHAR2(20 BYTE),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_TRAIT_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.REGISTRY_TRAIT_DETAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.REGISTRY_TRAIT_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.MILITARY_SERVICE_NUMBER IS 'Military Service Number is a Unique identifier provided to a person by the branch of the military with which he or she served. It is defined as the service number assigned during the veterans episodes of military service. The social security number and service number can be the same. ';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.REGTRAIDTH_PK ON ADR.REGISTRY_TRAIT_DETAIL_H
(REGISTRY_TRAIT_DETAIL_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.REGTRAITDTH_REGTRAIT_NU_I ON ADR.REGISTRY_TRAIT_DETAIL_H
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_TRAIT_DETAIL_H FOR ADR.REGISTRY_TRAIT_DETAIL_H;


ALTER TABLE ADR.REGISTRY_TRAIT_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_073
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT REGTRAIDTH_PK
  PRIMARY KEY
  (REGISTRY_TRAIT_DETAIL_H_ID)
  USING INDEX ADR.REGTRAIDTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.REGISTRY_TRAIT_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.REGISTRY_TRAIT_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_TRAIT_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_TRAIT_DETAIL_H TO ES_RW;
ALTER TABLE ADR.REGISTRY_TRAIT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.REGISTRY_TRAIT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.REGISTRY_TRAIT_H
(
  REGISTRY_TRAIT_H_ID    NUMBER(20),
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE),
  REGISTRY_TRAIT_ID      NUMBER(20),
  REGISTRY_TYPE_ID       NUMBER(20),
  SSN                    VARCHAR2(20 BYTE),
  PREFIX                 VARCHAR2(12 BYTE),
  FIRST_NAME             VARCHAR2(30 BYTE),
  MIDDLE_NAME            VARCHAR2(30 BYTE),
  LAST_NAME              VARCHAR2(40 BYTE),
  SUFFIX                 VARCHAR2(12 BYTE),
  EDUCATIONAL_DEGREE     VARCHAR2(30 BYTE),
  DEATH_DATE_TXT         VARCHAR2(14 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE    DATE,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE   DATE,
  RECORD_MODIFIED_COUNT  NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.REGISTRY_TRAIT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.REGISTRY_TRAIT_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.REGISTRY_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.PREFIX IS 'BR7327 Representative prefix is defined as the prefix of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.SUFFIX IS 'BR7329 Representative suffix is defined as the suffix of the association type identified.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.REGISTRY_TRAIT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.REGTRAITH_PK ON ADR.REGISTRY_TRAIT_H
(REGISTRY_TRAIT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.REGTRAITH_REGTRAIT_NU_I ON ADR.REGISTRY_TRAIT_H
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM REGISTRY_TRAIT_H FOR ADR.REGISTRY_TRAIT_H;


ALTER TABLE ADR.REGISTRY_TRAIT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_040
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066710
  CHECK ("REGISTRY_TRAIT_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066711
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066712
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066713
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066714
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066715
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066716
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT REGTRAITH_PK
  PRIMARY KEY
  (REGISTRY_TRAIT_H_ID)
  USING INDEX ADR.REGTRAITH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.REGISTRY_TRAIT_H TO ADR_R;

GRANT SELECT ON ADR.REGISTRY_TRAIT_H TO ADR_RW;

GRANT SELECT ON ADR.REGISTRY_TRAIT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.REGISTRY_TRAIT_H TO ES_RW;
ALTER TABLE ADR.RELAXATION_PERCENTAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RELAXATION_PERCENTAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RELAXATION_PERCENTAGE
(
  RELAXATION_PERCENTAGE_ID  NUMBER(20)          NOT NULL,
  RELAXATION_PERCENTAGE     NUMBER              NOT NULL,
  START_DATE                DATE,
  END_DATE                  DATE,
  INCOME_YEAR               NUMBER(4)           DEFAULT 1,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RELAXATION_PERCENTAGE IS 'The percentage to relax veterans healthcare eligibility threshold for an income year. ';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RELAXATION_PERCENTAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RELAXATION_PERCENTAGE IS 'Percentage to relax the eligibility criteria.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.START_DATE IS 'Start of effective period.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.END_DATE IS 'End of effective period.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.INCOME_YEAR IS 'Applicatble income year';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RECORD_CREATED_DATE IS 'DATE the record was created.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RECORD_MODIFIED_DATE IS 'DATE of this modification to the record.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RLXINCYR_I ON ADR.RELAXATION_PERCENTAGE
(INCOME_YEAR)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RLXPCT_PK ON ADR.RELAXATION_PERCENTAGE
(RELAXATION_PERCENTAGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.RELAXATION_PCT_AUDIR_TRGR
  AFTER DELETE OR INSERT OR UPDATE
  ON ADR.RELAXATION_PERCENTAGE  REFERENCING  OLD AS OLD NEW AS NEW
 FOR EACH ROW
declare
       mDML char(1);
begin

    if DELETING then
        insert into RELAXATION_PERCENTAGE_H
        (
        RELAXATION_PERCENTAGE_H_ID,
        RELAXATION_PERCENTAGE_ID,
        RELAXATION_PERCENTAGE,
        START_DATE,
        END_DATE,
        INCOME_YEAR,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        RELAXATION_PERCENTAGE_H_S.nextval,
        :old.RELAXATION_PERCENTAGE_ID,
        :old.RELAXATION_PERCENTAGE,
        :old.START_DATE,
        :old.END_DATE,
        :old.INCOME_YEAR,
        :old.RECORD_CREATED_BY,
        :old.RECORD_CREATED_DATE,
        :old.RECORD_MODIFIED_BY,
        :old.RECORD_MODIFIED_DATE,
        :old.RECORD_MODIFIED_COUNT,
        SYSDATE,
        'D'
        );
    else
        if UPDATING then
            mDML := 'U';
        else
            mDML := 'I';
        end if;

        insert into RELAXATION_PERCENTAGE_H
        (
        RELAXATION_PERCENTAGE_H_ID,
        RELAXATION_PERCENTAGE_ID,
        RELAXATION_PERCENTAGE,
        START_DATE,
        END_DATE,
        INCOME_YEAR,
        RECORD_CREATED_BY,
        RECORD_CREATED_DATE,
        RECORD_MODIFIED_BY,
        RECORD_MODIFIED_DATE,
        RECORD_MODIFIED_COUNT,
        HISTORY_CREATED_DATE,
        TRANSACTION_TYPE_CODE
        )
        values
        (
        RELAXATION_PERCENTAGE_H_S.nextval,
        :new.RELAXATION_PERCENTAGE_ID,
        :new.RELAXATION_PERCENTAGE,
        :new.START_DATE,
        :new.END_DATE,
        :new.INCOME_YEAR,
        :new.RECORD_CREATED_BY,
        :new.RECORD_CREATED_DATE,
        :new.RECORD_MODIFIED_BY,
        :new.RECORD_MODIFIED_DATE,
        :new.RECORD_MODIFIED_COUNT,
        SYSDATE,
        mDML
        );
    end if;

end;
/


CREATE OR REPLACE PUBLIC SYNONYM RELAXATION_PERCENTAGE FOR ADR.RELAXATION_PERCENTAGE;


ALTER TABLE ADR.RELAXATION_PERCENTAGE ADD (
  CONSTRAINT RLXPCT_PK
  PRIMARY KEY
  (RELAXATION_PERCENTAGE_ID)
  USING INDEX ADR.RLXPCT_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RELAXATION_PERCENTAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RELAXATION_PERCENTAGE TO ADR_RW;

GRANT SELECT ON ADR.RELAXATION_PERCENTAGE TO ES_R;

GRANT INSERT, SELECT ON ADR.RELAXATION_PERCENTAGE TO ES_RW;
ALTER TABLE ADR.RELAXATION_PERCENTAGE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RELAXATION_PERCENTAGE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.RELAXATION_PERCENTAGE_H
(
  RELAXATION_PERCENTAGE_H_ID  NUMBER(20)        NOT NULL,
  RELAXATION_PERCENTAGE_ID    NUMBER(20)        NOT NULL,
  RELAXATION_PERCENTAGE       NUMBER            NOT NULL,
  START_DATE                  DATE,
  END_DATE                    DATE,
  INCOME_YEAR                 NUMBER(4)         DEFAULT 1,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL,
  HISTORY_CREATED_DATE        DATE,
  TRANSACTION_TYPE_CODE       VARCHAR2(1 BYTE)  NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RELAXATION_PERCENTAGE_H IS 'The percentage to relax veterans healthcare eligibility threshold for an income year. ';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RELAXATION_PERCENTAGE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RELAXATION_PERCENTAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identiry a data record in this table.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RELAXATION_PERCENTAGE IS 'Percentage to relax the eligibility criteria.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.START_DATE IS 'Start of effective period.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.END_DATE IS 'End of effective period.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.INCOME_YEAR IS 'Applicatble income year';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RECORD_CREATED_DATE IS 'DATE the record was created.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RECORD_MODIFIED_DATE IS 'DATE of this modification to the record.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.RELAXATION_PERCENTAGE_H.HISTORY_CREATED_DATE IS 'DATE history record created.';


CREATE INDEX ADR.RLXHINCYR_I ON ADR.RELAXATION_PERCENTAGE_H
(INCOME_YEAR)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RLXPCTH_PK ON ADR.RELAXATION_PERCENTAGE_H
(RELAXATION_PERCENTAGE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RELAXATION_PERCENTAGE_H FOR ADR.RELAXATION_PERCENTAGE_H;


ALTER TABLE ADR.RELAXATION_PERCENTAGE_H ADD (
  CONSTRAINT RLXPCTH_PK
  PRIMARY KEY
  (RELAXATION_PERCENTAGE_H_ID)
  USING INDEX ADR.RLXPCTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RELAXATION_PERCENTAGE_H TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.RELAXATION_PERCENTAGE_H TO ADR_RW;

GRANT SELECT ON ADR.RELAXATION_PERCENTAGE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.RELAXATION_PERCENTAGE_H TO ES_RW;
ALTER TABLE ADR.ROLES
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLES CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLES
(
  ROLE_ID                NUMBER(20)             NOT NULL,
  ROLE_NAME              VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(250 BYTE),
  SORT_ORDER             NUMBER(6)              DEFAULT 0,
  INACTIVE_FLAG          NUMBER(1)              DEFAULT 0                     NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  ACTIVE_DATE            DATE
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLES IS 'The table is a application administration table that is used to designate a role for a particular user.';

COMMENT ON COLUMN ADR.ROLES.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLES.ROLE_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES.DESCRIPTION IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES.SORT_ORDER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLES.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLES.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLES.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLES.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ROLE_PK ON ADR.ROLES
(ROLE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ROLE_U_CODE_UK ON ADR.ROLES
(ROLE_NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ROLES_audir_trgr
after Update or Delete or Insert
ON ADR.ROLES
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ROLES_H
		(
		ROLES_H_ID,
		ROLE_ID,
		ROLE_NAME,
		DESCRIPTION,
		SORT_ORDER,
		ACTIVE_DATE,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLES_H_S.nextval,
		:old.ROLE_ID,
		:old.ROLE_NAME,
		:old.DESCRIPTION,
		:old.SORT_ORDER,
		:old.ACTIVE_DATE,
		:old.INACTIVE_FLAG,
		:old.INACTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into ROLES_H
		(
		ROLES_H_ID,
		ROLE_ID,
		ROLE_NAME,
		DESCRIPTION,
		SORT_ORDER,
		ACTIVE_DATE,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLES_H_S.nextval,
		:new.ROLE_ID,
		:new.ROLE_NAME,
		:new.DESCRIPTION,
		:new.SORT_ORDER,
		:new.ACTIVE_DATE,
		:new.INACTIVE_FLAG,
		:new.INACTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end ROLES_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ROLES FOR ADR.ROLES;


ALTER TABLE ADR.ROLES ADD (
  CONSTRAINT AVCON_1172521913_INACT_063
  CHECK (INACTIVE_FLAG BETWEEN 0 AND 0 OR INACTIVE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT ROLE_PK
  PRIMARY KEY
  (ROLE_ID)
  USING INDEX ADR.ROLE_PK
  ENABLE VALIDATE,
  CONSTRAINT ROLE_U_CODE_UK
  UNIQUE (ROLE_NAME)
  USING INDEX ADR.ROLE_U_CODE_UK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLES TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ROLES TO ADR_RW;

GRANT SELECT ON ADR.ROLES TO CARUSR_R;

GRANT SELECT ON ADR.ROLES TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLES TO ES_RW;

GRANT SELECT ON ADR.ROLES TO IAM_R;

GRANT SELECT ON ADR.ROLES TO IAM_RW;
ALTER TABLE ADR.ROLES_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLES_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLES_H
(
  ROLES_H_ID             NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  ROLE_ID                NUMBER(20)             NOT NULL,
  ROLE_NAME              VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(250 BYTE),
  SORT_ORDER             NUMBER(6)              DEFAULT 0,
  INACTIVE_FLAG          NUMBER(1)              DEFAULT 0                     NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  ACTIVE_DATE            DATE
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLES_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ROLES_H.ROLES_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ROLES_H.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLES_H.ROLE_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES_H.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLES_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLES_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLES_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLES_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLES_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ROLESH_PK ON ADR.ROLES_H
(ROLES_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ROLES_H FOR ADR.ROLES_H;


ALTER TABLE ADR.ROLES_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_050
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT ROLESH_PK
  PRIMARY KEY
  (ROLES_H_ID)
  USING INDEX ADR.ROLESH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLES_H TO ADR_R;

GRANT SELECT ON ADR.ROLES_H TO ADR_RW;

GRANT SELECT ON ADR.ROLES_H TO CARUSR_R;

GRANT SELECT ON ADR.ROLES_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLES_H TO ES_RW;
ALTER TABLE ADR.ROLE_PERMISSIONS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLE_PERMISSIONS CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLE_PERMISSIONS
(
  ROLE_PERMISSION_ID     NUMBER(20)             NOT NULL,
  PERMISSION_TYPE_ID     NUMBER(20)             NOT NULL,
  ROLE_ID                NUMBER(20)             NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLE_PERMISSIONS IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.ROLE_PERMISSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ROLEPERMS_PK ON ADR.ROLE_PERMISSIONS
(ROLE_PERMISSION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ROLEPERMS_ROLE_FK_I ON ADR.ROLE_PERMISSIONS
(ROLE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ROLEPERMS_STD_PERM_FK_I ON ADR.ROLE_PERMISSIONS
(PERMISSION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ROLE_PERMSSN_audir_trgr
after Update or Delete or Insert
ON ADR.ROLE_PERMISSIONS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ROLE_PERMISSIONS_H
		(
		ROLE_PERMISSIONS_H_ID,
		ROLE_PERMISSION_ID,
		ROLE_ID,
		PERMISSION_TYPE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLE_PERMISSIONS_H_S.nextval,
		:old.ROLE_PERMISSION_ID,
		:old.ROLE_ID,
		:old.PERMISSION_TYPE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into ROLE_PERMISSIONS_H
		(
		ROLE_PERMISSIONS_H_ID,
		ROLE_PERMISSION_ID,
		ROLE_ID,
		PERMISSION_TYPE_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLE_PERMISSIONS_H_S.nextval,
		:new.ROLE_PERMISSION_ID,
		:new.ROLE_ID,
		:new.PERMISSION_TYPE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end ROLE_PERMSSN_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ROLE_PERMISSIONS FOR ADR.ROLE_PERMISSIONS;


ALTER TABLE ADR.ROLE_PERMISSIONS ADD (
  CONSTRAINT ROLEPERMS_PK
  PRIMARY KEY
  (ROLE_PERMISSION_ID)
  USING INDEX ADR.ROLEPERMS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ROLE_PERMISSIONS ADD (
  CONSTRAINT ROLEPERMS_ROLE_FK 
  FOREIGN KEY (ROLE_ID) 
  REFERENCES ADR.ROLES (ROLE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT ROLEPERMS_STD_PERM_FK 
  FOREIGN KEY (PERMISSION_TYPE_ID) 
  REFERENCES ADRREF.STD_PERMISSIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLE_PERMISSIONS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ROLE_PERMISSIONS TO ADR_RW;

GRANT SELECT ON ADR.ROLE_PERMISSIONS TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLE_PERMISSIONS TO ES_RW;
ALTER TABLE ADR.ROLE_PERMISSIONS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLE_PERMISSIONS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLE_PERMISSIONS_H
(
  ROLE_PERMISSIONS_H_ID  NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  ROLE_PERMISSION_ID     NUMBER(20)             NOT NULL,
  ROLE_ID                NUMBER(20)             NOT NULL,
  PERMISSION_TYPE_ID     NUMBER(20)             NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLE_PERMISSIONS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.ROLE_PERMISSIONS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.ROLE_PERMISSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLE_PERMISSIONS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ROLEPRMSH_PK ON ADR.ROLE_PERMISSIONS_H
(ROLE_PERMISSIONS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ROLE_PERMISSIONS_H FOR ADR.ROLE_PERMISSIONS_H;


ALTER TABLE ADR.ROLE_PERMISSIONS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_048
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT ROLEPRMSH_PK
  PRIMARY KEY
  (ROLE_PERMISSIONS_H_ID)
  USING INDEX ADR.ROLEPRMSH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLE_PERMISSIONS_H TO ADR_R;

GRANT SELECT ON ADR.ROLE_PERMISSIONS_H TO ADR_RW;

GRANT SELECT ON ADR.ROLE_PERMISSIONS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLE_PERMISSIONS_H TO ES_RW;
ALTER TABLE ADR.ROLE_PERMISSION_SETS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLE_PERMISSION_SETS CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLE_PERMISSION_SETS
(
  ROLE_PERMISSION_SET_ID  NUMBER(20)            NOT NULL,
  PERMISSION_SET_ID       NUMBER(20)            NOT NULL,
  ROLE_ID                 NUMBER(20)            NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLE_PERMISSION_SETS IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.ROLE_PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.ROLEPERMST_PERMSET_FK_I ON ADR.ROLE_PERMISSION_SETS
(PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.ROLEPERMST_PK ON ADR.ROLE_PERMISSION_SETS
(ROLE_PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.ROLEPERMST_ROLE_FK_I ON ADR.ROLE_PERMISSION_SETS
(ROLE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.ROLE_PERM_SET_audir_trgr
after Update or Delete or Insert
ON ADR.ROLE_PERMISSION_SETS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into ROLE_PERMISSION_SETS_H
		(
		ROLE_PERMISSION_SET_H_ID,
		ROLE_PERMISSION_SET_ID,
		ROLE_ID,
		PERMISSION_SET_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLE_PERMISSION_SETS_H_S.nextval,
		:old.ROLE_PERMISSION_SET_ID,
		:old.ROLE_ID,
		:old.PERMISSION_SET_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into ROLE_PERMISSION_SETS_H
		(
		ROLE_PERMISSION_SET_H_ID,
		ROLE_PERMISSION_SET_ID,
		ROLE_ID,
		PERMISSION_SET_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		ROLE_PERMISSION_SETS_H_S.nextval,
		:new.ROLE_PERMISSION_SET_ID,
		:new.ROLE_ID,
		:new.PERMISSION_SET_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end ROLE_PERM_SET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM ROLE_PERMISSION_SETS FOR ADR.ROLE_PERMISSION_SETS;


ALTER TABLE ADR.ROLE_PERMISSION_SETS ADD (
  CONSTRAINT ROLEPERMST_PK
  PRIMARY KEY
  (ROLE_PERMISSION_SET_ID)
  USING INDEX ADR.ROLEPERMST_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.ROLE_PERMISSION_SETS ADD (
  CONSTRAINT ROLEPERMST_PERMSET_FK 
  FOREIGN KEY (PERMISSION_SET_ID) 
  REFERENCES ADR.PERMISSION_SET (PERMISSION_SET_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT ROLEPERMST_ROLE_FK 
  FOREIGN KEY (ROLE_ID) 
  REFERENCES ADR.ROLES (ROLE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLE_PERMISSION_SETS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.ROLE_PERMISSION_SETS TO ADR_RW;

GRANT SELECT ON ADR.ROLE_PERMISSION_SETS TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLE_PERMISSION_SETS TO ES_RW;
ALTER TABLE ADR.ROLE_PERMISSION_SETS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.ROLE_PERMISSION_SETS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.ROLE_PERMISSION_SETS_H
(
  ROLE_PERMISSION_SET_H_ID  NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  ROLE_PERMISSION_SET_ID    NUMBER(20)          NOT NULL,
  ROLE_ID                   NUMBER(20)          NOT NULL,
  PERMISSION_SET_ID         NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.ROLE_PERMISSION_SETS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.ROLE_PERMISSION_SET_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.ROLE_PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.ROLE_PERMISSION_SETS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.ROLEPRMSTH_PK ON ADR.ROLE_PERMISSION_SETS_H
(ROLE_PERMISSION_SET_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM ROLE_PERMISSION_SETS_H FOR ADR.ROLE_PERMISSION_SETS_H;


ALTER TABLE ADR.ROLE_PERMISSION_SETS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_060
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT ROLEPRMSTH_PK
  PRIMARY KEY
  (ROLE_PERMISSION_SET_H_ID)
  USING INDEX ADR.ROLEPRMSTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.ROLE_PERMISSION_SETS_H TO ADR_R;

GRANT SELECT ON ADR.ROLE_PERMISSION_SETS_H TO ADR_RW;

GRANT SELECT ON ADR.ROLE_PERMISSION_SETS_H TO ES_R;

GRANT INSERT, SELECT ON ADR.ROLE_PERMISSION_SETS_H TO ES_RW;
ALTER TABLE ADR.RPT_CANCEL_DECLINE_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_CANCEL_DECLINE_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_CANCEL_DECLINE_REASON
(
  RPT_CANCEL_DECLINE_REASON_ID   NUMBER(20)     NOT NULL,
  RPT_SCHEDULED_REPORT_ID        NUMBER(20)     NOT NULL,
  CANCEL_DECLINE_REASON_TYPE_ID  NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_CANCEL_DECLINE_REASON IS 'ESR reporting table. Contains the RPT_SCHEDULED_REPORT_ID and the cancel / decline reason type id.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RPT_CANCEL_DECLINE_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.CANCEL_DECLINE_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_CANCEL_DECLINE_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTCANDECL_CANDCLNRSN_FK_I ON ADR.RPT_CANCEL_DECLINE_REASON
(CANCEL_DECLINE_REASON_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTCANDECL_PK ON ADR.RPT_CANCEL_DECLINE_REASON
(RPT_CANCEL_DECLINE_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTCANDECL_SCHEDRPT_FK_I ON ADR.RPT_CANCEL_DECLINE_REASON
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_CANCEL_DECLINE_REASON FOR ADR.RPT_CANCEL_DECLINE_REASON;


ALTER TABLE ADR.RPT_CANCEL_DECLINE_REASON ADD (
  CONSTRAINT RPTCANDECL_PK
  PRIMARY KEY
  (RPT_CANCEL_DECLINE_REASON_ID)
  USING INDEX ADR.RPTCANDECL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_CANCEL_DECLINE_REASON ADD (
  CONSTRAINT RPTCANDECL_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_CANCEL_DECLINE_REASON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_CANCEL_DECLINE_REASON TO ADR_RW;

GRANT SELECT ON ADR.RPT_CANCEL_DECLINE_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_CANCEL_DECLINE_REASON TO ES_RW;
ALTER TABLE ADR.RPT_COMPLETED_REPORTS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_COMPLETED_REPORTS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_COMPLETED_REPORTS
(
  RPT_COMPLETED_REPORT_ID  NUMBER(20)           NOT NULL,
  RPT_STANDARD_REPORT_ID   NUMBER(20),
  RPT_OUTPUT_FILE_TYPE_ID  NUMBER(20)           NOT NULL,
  USERID                   NUMBER(20),
  REPORT_COMPLETED_DATE    DATE,
  REPORT_PARAMETER_TXT     VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL,
  REPORT_OUTPUT_DATA       BLOB,
  INACTIVATED_DATE         DATE
)
LOB (REPORT_OUTPUT_DATA) STORE AS (
  TABLESPACE  M_INDEX_01
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          4M
                  NEXT             4M
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  BUFFER_POOL      DEFAULT
                  FLASH_CACHE      DEFAULT
                  CELL_FLASH_CACHE DEFAULT
                 ))
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_COMPLETED_REPORTS IS 'ESR reporting table. Contains the RPT_SCHEDULED_REPORT_ID as well as the date when report was completed';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RPT_COMPLETED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RPT_STANDARD_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.REPORT_COMPLETED_DATE IS 'Date that the report run was completed';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.REPORT_PARAMETER_TXT IS 'TEXT field that contains the parameters used for building the report';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.RPT_COMPLETED_REPORTS.REPORT_OUTPUT_DATA IS 'CLOB field that contains the output of the report';


CREATE INDEX ADR.CMPRPT_USER_1_FK_I ON ADR.RPT_COMPLETED_REPORTS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMRPT_STD_RPTOUTFILE_FK_I ON ADR.RPT_COMPLETED_REPORTS
(RPT_OUTPUT_FILE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.CMRPT_STD_RPTSTDRPT_FK_I ON ADR.RPT_COMPLETED_REPORTS
(RPT_STANDARD_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.COMPRPT_PK ON ADR.RPT_COMPLETED_REPORTS
(RPT_COMPLETED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_COMPLETED_REPORTS FOR ADR.RPT_COMPLETED_REPORTS;


ALTER TABLE ADR.RPT_COMPLETED_REPORTS ADD (
  CONSTRAINT COMPRPT_PK
  PRIMARY KEY
  (RPT_COMPLETED_REPORT_ID)
  USING INDEX ADR.COMPRPT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_COMPLETED_REPORTS ADD (
  CONSTRAINT CMPRPT_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT CMRPT_STD_RPTOUTFILE_FK 
  FOREIGN KEY (RPT_OUTPUT_FILE_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTOUTPUTFILETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT CMRPT_STD_RPTSTDRPT_FK 
  FOREIGN KEY (RPT_STANDARD_REPORT_ID) 
  REFERENCES ADRREF.STD_RPTSTANDARDREPORTS (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_COMPLETED_REPORTS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_COMPLETED_REPORTS TO ADR_RW;

GRANT SELECT ON ADR.RPT_COMPLETED_REPORTS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_COMPLETED_REPORTS TO ES_RW;
ALTER TABLE ADR.RPT_COM_ERROR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_COM_ERROR CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_COM_ERROR
(
  RPT_COM_ERROR_ID         NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  COM_AAC_ERROR_TYPE_ID    NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_COM_ERROR IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RPT_COM_ERROR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_COM_ERROR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTCOMERR_COMERRTYP_FK_I ON ADR.RPT_COM_ERROR
(COM_AAC_ERROR_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTCOMERR_PK ON ADR.RPT_COM_ERROR
(RPT_COM_ERROR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTCOMERR_SCHEDRPT_FK_I ON ADR.RPT_COM_ERROR
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_COM_ERROR FOR ADR.RPT_COM_ERROR;


ALTER TABLE ADR.RPT_COM_ERROR ADD (
  CONSTRAINT RPTCOMERR_PK
  PRIMARY KEY
  (RPT_COM_ERROR_ID)
  USING INDEX ADR.RPTCOMERR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_COM_ERROR ADD (
  CONSTRAINT RPTCOMERR_COMERRTYP_FK 
  FOREIGN KEY (COM_AAC_ERROR_TYPE_ID) 
  REFERENCES ADRREF.STD_COMMERRORTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTCOMERR_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_COM_ERROR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_COM_ERROR TO ADR_RW;

GRANT SELECT ON ADR.RPT_COM_ERROR TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_COM_ERROR TO ES_RW;
ALTER TABLE ADR.RPT_COM_REJECT_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_COM_REJECT_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_COM_REJECT_REASON
(
  RPT_COM_REJECT_REASON_ID       NUMBER(20)     NOT NULL,
  RPT_SCHEDULED_REPORT_ID        NUMBER(20)     NOT NULL,
  COM_AAC_REJECT_REASON_TYPE_ID  NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_COM_REJECT_REASON IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RPT_COM_REJECT_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_COM_REJECT_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTCOMRJCT_COMAACRJCT_FK_I ON ADR.RPT_COM_REJECT_REASON
(COM_AAC_REJECT_REASON_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTCOMRJCT_PK ON ADR.RPT_COM_REJECT_REASON
(RPT_COM_REJECT_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTCOMRJCT_SCHEDRPT_FK_I ON ADR.RPT_COM_REJECT_REASON
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_COM_REJECT_REASON FOR ADR.RPT_COM_REJECT_REASON;


ALTER TABLE ADR.RPT_COM_REJECT_REASON ADD (
  CONSTRAINT RPTCOMRJCT_PK
  PRIMARY KEY
  (RPT_COM_REJECT_REASON_ID)
  USING INDEX ADR.RPTCOMRJCT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_COM_REJECT_REASON ADD (
  CONSTRAINT RPTCOMRJCT_COMAACRJCT_FK 
  FOREIGN KEY (COM_AAC_REJECT_REASON_TYPE_ID) 
  REFERENCES ADRREF.STD_COMREJECTREASONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTCOMRJCT_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_COM_REJECT_REASON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_COM_REJECT_REASON TO ADR_RW;

GRANT SELECT ON ADR.RPT_COM_REJECT_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_COM_REJECT_REASON TO ES_RW;
ALTER TABLE ADR.RPT_DAY_OF_WEEK
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_DAY_OF_WEEK CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_DAY_OF_WEEK
(
  RPT_DAY_OF_WEEK_ID       NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  RPT_DAY_OF_WEEK_TYPE_ID  NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_DAY_OF_WEEK IS 'ESR reporting table. Contains the RPT_SCHEDULED_REPORT_ID as well as the day of week that the report is scheduled to run';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RPT_DAY_OF_WEEK_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_DAY_OF_WEEK.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTDAYOFWK_PK ON ADR.RPT_DAY_OF_WEEK
(RPT_DAY_OF_WEEK_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTDAYOFWK_SCHEDRPT_FK_I ON ADR.RPT_DAY_OF_WEEK
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTDOWK_STD_RPTDYWKTYP_FK_I ON ADR.RPT_DAY_OF_WEEK
(RPT_DAY_OF_WEEK_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_DAY_OF_WEEK FOR ADR.RPT_DAY_OF_WEEK;


ALTER TABLE ADR.RPT_DAY_OF_WEEK ADD (
  CONSTRAINT RPTDAYOFWK_PK
  PRIMARY KEY
  (RPT_DAY_OF_WEEK_ID)
  USING INDEX ADR.RPTDAYOFWK_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_DAY_OF_WEEK ADD (
  CONSTRAINT RPTDAYOFWK_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTDOWK_STD_RPTDYWKTYP_FK 
  FOREIGN KEY (RPT_DAY_OF_WEEK_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDAYOFWEEKTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_DAY_OF_WEEK TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_DAY_OF_WEEK TO ADR_RW;

GRANT SELECT ON ADR.RPT_DAY_OF_WEEK TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_DAY_OF_WEEK TO ES_RW;
ALTER TABLE ADR.RPT_EED_WEEKLY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_EED_WEEKLY CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_EED_WEEKLY
(
  RPT_EED_WEEKLY_ID        NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  RPT_EED_WEEKLY_TYPE_ID   NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_EED_WEEKLY IS 'ESR Reporting table, containing list of weekly reports Purple Heart Statistics Address Statistics from AAC Enrollment Letters from AAC';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RPT_EED_WEEKLY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_EED_WEEKLY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTEEDWKLY_PK ON ADR.RPT_EED_WEEKLY
(RPT_EED_WEEKLY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTEWK_SCHEDRPT_FK_I ON ADR.RPT_EED_WEEKLY
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTEWK_STD_EEDWKLYTYP_FK_I ON ADR.RPT_EED_WEEKLY
(RPT_EED_WEEKLY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_EED_WEEKLY FOR ADR.RPT_EED_WEEKLY;


ALTER TABLE ADR.RPT_EED_WEEKLY ADD (
  CONSTRAINT RPTEEDWKLY_PK
  PRIMARY KEY
  (RPT_EED_WEEKLY_ID)
  USING INDEX ADR.RPTEEDWKLY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_EED_WEEKLY ADD (
  CONSTRAINT RPTEWK_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTEWK_STD_EEDWKLYTYP_FK 
  FOREIGN KEY (RPT_EED_WEEKLY_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTEEDWEEKLYTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_EED_WEEKLY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_EED_WEEKLY TO ADR_RW;

GRANT SELECT ON ADR.RPT_EED_WEEKLY TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_EED_WEEKLY TO ES_RW;
ALTER TABLE ADR.RPT_ELIGIBILITY_FACTOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_ELIGIBILITY_FACTOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_ELIGIBILITY_FACTOR
(
  RPT_ELIGIBILITY_FACTOR_ID  NUMBER(20)         NOT NULL,
  RPT_SCHEDULED_REPORT_ID    NUMBER(20)         NOT NULL,
  STD_REPORTELIGFACTOR_ID    NUMBER(20)         NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_ELIGIBILITY_FACTOR IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RPT_ELIGIBILITY_FACTOR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_ELIGIBILITY_FACTOR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTELGFCTR_PK ON ADR.RPT_ELIGIBILITY_FACTOR
(RPT_ELIGIBILITY_FACTOR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTELGFCTR_SCHEDRPT_FK_I ON ADR.RPT_ELIGIBILITY_FACTOR
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTELGFCTR_STDRPTELGF_FK_I ON ADR.RPT_ELIGIBILITY_FACTOR
(STD_REPORTELIGFACTOR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_ELIGIBILITY_FACTOR FOR ADR.RPT_ELIGIBILITY_FACTOR;


ALTER TABLE ADR.RPT_ELIGIBILITY_FACTOR ADD (
  CONSTRAINT RPTELGFCTR_PK
  PRIMARY KEY
  (RPT_ELIGIBILITY_FACTOR_ID)
  USING INDEX ADR.RPTELGFCTR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_ELIGIBILITY_FACTOR ADD (
  CONSTRAINT RPTELGFCTR_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTELGFCTR_STDRPTELGF_FK 
  FOREIGN KEY (STD_REPORTELIGFACTOR_ID) 
  REFERENCES ADRREF.STD_REPORTELIGFACTOR (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_ELIGIBILITY_FACTOR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_ELIGIBILITY_FACTOR TO ADR_RW;

GRANT SELECT ON ADR.RPT_ELIGIBILITY_FACTOR TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_ELIGIBILITY_FACTOR TO ES_RW;
ALTER TABLE ADR.RPT_ENROLLMENT_CATEGORY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_ENROLLMENT_CATEGORY CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_ENROLLMENT_CATEGORY
(
  RPT_ENROLLMENT_CATEGORY_ID  NUMBER(20)        NOT NULL,
  RPT_SCHEDULED_REPORT_ID     NUMBER(20)        NOT NULL,
  STD_ENROLLMENTCATTYPE_ID    NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_ENROLLMENT_CATEGORY IS 'ESR Reporting table. Enrollment Category reports for category types such as: Enrolled In-Process Not Enrolled';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RPT_ENROLLMENT_CATEGORY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_CATEGORY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTENRLCAT_PK ON ADR.RPT_ENROLLMENT_CATEGORY
(RPT_ENROLLMENT_CATEGORY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTENRLCAT_SCHEDRPT_FK_I ON ADR.RPT_ENROLLMENT_CATEGORY
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTENRLCAT_STD_ENRLMNTCAT_FK_I ON ADR.RPT_ENROLLMENT_CATEGORY
(STD_ENROLLMENTCATTYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_ENROLLMENT_CATEGORY FOR ADR.RPT_ENROLLMENT_CATEGORY;


ALTER TABLE ADR.RPT_ENROLLMENT_CATEGORY ADD (
  CONSTRAINT RPTENRLCAT_PK
  PRIMARY KEY
  (RPT_ENROLLMENT_CATEGORY_ID)
  USING INDEX ADR.RPTENRLCAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_ENROLLMENT_CATEGORY ADD (
  CONSTRAINT RPTENRLCAT_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_ENROLLMENT_CATEGORY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_ENROLLMENT_CATEGORY TO ADR_RW;

GRANT SELECT ON ADR.RPT_ENROLLMENT_CATEGORY TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_ENROLLMENT_CATEGORY TO ES_RW;
ALTER TABLE ADR.RPT_ENROLLMENT_OVERRIDE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_ENROLLMENT_OVERRIDE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_ENROLLMENT_OVERRIDE
(
  RPT_ENROLLMENT_OVERRIDE_ID  NUMBER(20)        NOT NULL,
  RPT_SCHEDULED_REPORT_ID     NUMBER(20)        NOT NULL,
  STD_ENRLOVERRIDEREASON_ID   NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_ENROLLMENT_OVERRIDE IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RPT_ENROLLMENT_OVERRIDE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_OVERRIDE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTENROVER_PK ON ADR.RPT_ENROLLMENT_OVERRIDE
(RPT_ENROLLMENT_OVERRIDE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTENROVER_SCHEDRPT_FK_I ON ADR.RPT_ENROLLMENT_OVERRIDE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTENROVER_STDENROVRD_FK_I ON ADR.RPT_ENROLLMENT_OVERRIDE
(STD_ENRLOVERRIDEREASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_ENROLLMENT_OVERRIDE FOR ADR.RPT_ENROLLMENT_OVERRIDE;


ALTER TABLE ADR.RPT_ENROLLMENT_OVERRIDE ADD (
  CONSTRAINT RPTENROVER_PK
  PRIMARY KEY
  (RPT_ENROLLMENT_OVERRIDE_ID)
  USING INDEX ADR.RPTENROVER_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_ENROLLMENT_OVERRIDE ADD (
  CONSTRAINT RPTENROVER_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_ENROLLMENT_OVERRIDE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_ENROLLMENT_OVERRIDE TO ADR_RW;

GRANT SELECT ON ADR.RPT_ENROLLMENT_OVERRIDE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_ENROLLMENT_OVERRIDE TO ES_RW;
ALTER TABLE ADR.RPT_ENROLLMENT_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_ENROLLMENT_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_ENROLLMENT_STATUS
(
  RPT_ENROLLMENT_STATUS_ID  NUMBER(20)          NOT NULL,
  RPT_SCHEDULED_REPORT_ID   NUMBER(20)          NOT NULL,
  STD_ENROLLMENTSTATUS_ID   NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_ENROLLMENT_STATUS IS 'ESR Reporting table: Provides reporting based on the following enrollment status: Unverified Verified Deceased Cancelled/Declined Rejected; Initial Application by VAMC Pending; No Eligibility Code Pending; Means Test Required Pending; Eligibility Status is Unverified Pending; Other Not Eligible; Refused to Pay Copay Not Eligible; Ineligible Date';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RPT_ENROLLMENT_STATUS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_ENROLLMENT_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTENRSTAT_ENRLMTSTAT_FK_I ON ADR.RPT_ENROLLMENT_STATUS
(STD_ENROLLMENTSTATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTENRSTAT_PK ON ADR.RPT_ENROLLMENT_STATUS
(RPT_ENROLLMENT_STATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTENRSTAT_SCHEDRPT_FK_I ON ADR.RPT_ENROLLMENT_STATUS
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_ENROLLMENT_STATUS FOR ADR.RPT_ENROLLMENT_STATUS;


ALTER TABLE ADR.RPT_ENROLLMENT_STATUS ADD (
  CONSTRAINT RPTENRSTAT_PK
  PRIMARY KEY
  (RPT_ENROLLMENT_STATUS_ID)
  USING INDEX ADR.RPTENRSTAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_ENROLLMENT_STATUS ADD (
  CONSTRAINT RPTENRSTAT_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_ENROLLMENT_STATUS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_ENROLLMENT_STATUS TO ADR_RW;

GRANT SELECT ON ADR.RPT_ENROLLMENT_STATUS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_ENROLLMENT_STATUS TO ES_RW;
ALTER TABLE ADR.RPT_FORM_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_FORM_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_FORM_TYPE
(
  RPT_FORM_TYPE_ID         NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  ENROLLMENT_FORM_TYPE_ID  NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE INDEX ADR.ENROL_FORM_TYP_RPT_FORM_TYP_FK ON ADR.RPT_FORM_TYPE
(ENROLLMENT_FORM_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.IX_RPT_SCHED_REP_RPT_FORM_TYP ON ADR.RPT_FORM_TYPE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPT_FORM_TYPE_ID_PK ON ADR.RPT_FORM_TYPE
(RPT_FORM_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_FORM_TYPE FOR ADR.RPT_FORM_TYPE;


ALTER TABLE ADR.RPT_FORM_TYPE ADD (
  CONSTRAINT RPT_FORM_TYPE_ID_PK
  PRIMARY KEY
  (RPT_FORM_TYPE_ID)
  USING INDEX ADR.RPT_FORM_TYPE_ID_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_FORM_TYPE ADD (
  CONSTRAINT ENRL_FORM_TYP_RPT_FORM_TYP_FK 
  FOREIGN KEY (ENROLLMENT_FORM_TYPE_ID) 
  REFERENCES VOA.ENROLLMENT_FORM_TYPE (ENROLLMENT_FORM_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT RPT_SCHED_REP_RPT_FORM_TYP 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_FORM_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_FORM_TYPE TO ADR_RW;

GRANT SELECT ON ADR.RPT_FORM_TYPE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_FORM_TYPE TO ES_RW;
ALTER TABLE ADR.RPT_INVALID_ADDRESS_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_INVALID_ADDRESS_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_INVALID_ADDRESS_REASON
(
  RPT_INVALID_ADDRESS_REASON_ID  NUMBER(20)     NOT NULL,
  RPT_SCHEDULED_REPORT_ID        NUMBER(20)     NOT NULL,
  ADDRESS_INVALID_TYPE_ID        NUMBER(20)     NOT NULL,
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_INVALID_ADDRESS_REASON IS 'ESR reporting table. Provides for reporting on the basis of various invalid address reasons: Mail was returned or is otherwise known to be undeliverable Veteran has no known address';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RPT_INVALID_ADDRESS_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.ADDRESS_INVALID_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_INVALID_ADDRESS_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTINVADDR_ADDRINVTYP_FK_I ON ADR.RPT_INVALID_ADDRESS_REASON
(ADDRESS_INVALID_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTINVADDR_PK ON ADR.RPT_INVALID_ADDRESS_REASON
(RPT_INVALID_ADDRESS_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTINVADDR_SCHEDRPT_FK_I ON ADR.RPT_INVALID_ADDRESS_REASON
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_INVALID_ADDRESS_REASON FOR ADR.RPT_INVALID_ADDRESS_REASON;


ALTER TABLE ADR.RPT_INVALID_ADDRESS_REASON ADD (
  CONSTRAINT RPTINVADDR_PK
  PRIMARY KEY
  (RPT_INVALID_ADDRESS_REASON_ID)
  USING INDEX ADR.RPTINVADDR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_INVALID_ADDRESS_REASON ADD (
  CONSTRAINT RPTINVADDR_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_INVALID_ADDRESS_REASON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_INVALID_ADDRESS_REASON TO ADR_RW;

GRANT SELECT ON ADR.RPT_INVALID_ADDRESS_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_INVALID_ADDRESS_REASON TO ES_RW;
ALTER TABLE ADR.RPT_LETTER_FILE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_LETTER_FILE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_LETTER_FILE
(
  RPT_LETTER_FILE_ID       NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  RPT_LETTER_FILE_TYPE_ID  NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_LETTER_FILE IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RPT_LETTER_FILE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_LETTER_FILE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTLTRFLTP_PK ON ADR.RPT_LETTER_FILE
(RPT_LETTER_FILE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTLTRFLTP_RPTLTRFILE_FK_I ON ADR.RPT_LETTER_FILE
(RPT_LETTER_FILE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTLTRFLTP_SCHEDRPT_FK_I ON ADR.RPT_LETTER_FILE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_LETTER_FILE FOR ADR.RPT_LETTER_FILE;


ALTER TABLE ADR.RPT_LETTER_FILE ADD (
  CONSTRAINT RPTLTRFLTP_PK
  PRIMARY KEY
  (RPT_LETTER_FILE_ID)
  USING INDEX ADR.RPTLTRFLTP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_LETTER_FILE ADD (
  CONSTRAINT RPTLTRFLTP_RPTLTRFILE_FK 
  FOREIGN KEY (RPT_LETTER_FILE_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTLETTERFILETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTLTRFLTP_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_LETTER_FILE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_LETTER_FILE TO ADR_RW;

GRANT SELECT ON ADR.RPT_LETTER_FILE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_LETTER_FILE TO ES_RW;
ALTER TABLE ADR.RPT_MESSAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_MESSAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_MESSAGE
(
  RPT_MESSAGE_ID                NUMBER(20)      NOT NULL,
  RPT_SCHEDULED_REPORT_ID       NUMBER(20)      NOT NULL,
  MESSAGE_TRANSMISSION_TYPE_ID  NUMBER(20)      NOT NULL,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_MESSAGE IS 'ESR reporting table. Allows for reporting on the basis of various message transmission types, such as: AAC Communication Request to AAC EDB Financial Query to EDB EDB Communication Address Update to EDB EDB Communication Errors to EDB EDB Communication Rejects to EDB EDB Death to EDB EDB Demographic to EDB EDB Eligibility Solicited to EDB EDB Eligibility or Enrollment Query to EDB EDB Eligibility to EDB EDB Follow Up Insurance Data to EDB EDB Full Data Solicited to EDB EDB Full Data to EDB EDB IVM Billing or Collecting Data to EDB EDB Lazarus Death to EDB EDB MFA EGT Setting to EDB';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RPT_MESSAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_MESSAGE.MESSAGE_TRANSMISSION_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_MESSAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTMSG_HL7TRANTYP_FK_I ON ADR.RPT_MESSAGE
(MESSAGE_TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTMSG_PK ON ADR.RPT_MESSAGE
(RPT_MESSAGE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTMSG_SCHEDRPT_FK_I ON ADR.RPT_MESSAGE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_MESSAGE FOR ADR.RPT_MESSAGE;


ALTER TABLE ADR.RPT_MESSAGE ADD (
  CONSTRAINT RPTMSG_PK
  PRIMARY KEY
  (RPT_MESSAGE_ID)
  USING INDEX ADR.RPTMSG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_MESSAGE ADD (
  CONSTRAINT RPTMSG_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_MESSAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_MESSAGE TO ADR_RW;

GRANT SELECT ON ADR.RPT_MESSAGE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_MESSAGE TO ES_RW;
ALTER TABLE ADR.RPT_MESSAGE_ERROR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_MESSAGE_ERROR CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_MESSAGE_ERROR
(
  RPT_MESSAGE_ERROR_ID     NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  ACK_TYPE_ID              NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_MESSAGE_ERROR IS 'ESR reporting table. Allows reporting on the basis of various HL7 acknowledgement codes such as: Accept Commit Accept Commit Error Commit Reject Error Reject';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RPT_MESSAGE_ERROR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.ACK_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_MESSAGE_ERROR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTMSGERR_PK ON ADR.RPT_MESSAGE_ERROR
(RPT_MESSAGE_ERROR_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTMSGERR_SCHEDRPT_FK_I ON ADR.RPT_MESSAGE_ERROR
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTMSGERR_STD_MSGTYP_FK_I ON ADR.RPT_MESSAGE_ERROR
(ACK_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_MESSAGE_ERROR FOR ADR.RPT_MESSAGE_ERROR;


ALTER TABLE ADR.RPT_MESSAGE_ERROR ADD (
  CONSTRAINT RPTMSGERR_PK
  PRIMARY KEY
  (RPT_MESSAGE_ERROR_ID)
  USING INDEX ADR.RPTMSGERR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_MESSAGE_ERROR ADD (
  CONSTRAINT RPTMSGERR_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_MESSAGE_ERROR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_MESSAGE_ERROR TO ADR_RW;

GRANT SELECT ON ADR.RPT_MESSAGE_ERROR TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_MESSAGE_ERROR TO ES_RW;
ALTER TABLE ADR.RPT_PERMISSIONS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_PERMISSIONS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_PERMISSIONS
(
  RPT_PERMISSION_ID      NUMBER(20)             NOT NULL,
  RPT_TYPE_ID            NUMBER(20)             NOT NULL,
  PERMISSION_TYPE_ID     NUMBER(20)             NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_PERMISSIONS IS 'ESR Reporting table Allows the user to see reports based their assigned User Permissions such as: Add Address Information Add Capability Set Add Contact (Excluding Next of Kin) Add EGT Settings Edit Cancel/Decline Enrollment Status Edit Capability Set Edit Contact Information (Excluding Next of Kin) Edit Demographic Information Edit Duplicate Threshold Value';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RPT_PERMISSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_PERMISSIONS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTPERM_PK ON ADR.RPT_PERMISSIONS
(RPT_PERMISSION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPERM_STD_PERM_FK_I ON ADR.RPT_PERMISSIONS
(PERMISSION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPERM_STD_RPTTYPE_FK_I ON ADR.RPT_PERMISSIONS
(RPT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_PERMISSIONS FOR ADR.RPT_PERMISSIONS;


ALTER TABLE ADR.RPT_PERMISSIONS ADD (
  CONSTRAINT RPTPERM_PK
  PRIMARY KEY
  (RPT_PERMISSION_ID)
  USING INDEX ADR.RPTPERM_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_PERMISSIONS ADD (
  CONSTRAINT RPTPERM_STD_PERM_FK 
  FOREIGN KEY (PERMISSION_TYPE_ID) 
  REFERENCES ADRREF.STD_PERMISSIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTPERM_STD_RPTTYPE_FK 
  FOREIGN KEY (RPT_TYPE_ID) 
  REFERENCES ADRREF.STD_REPORTTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_PERMISSIONS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_PERMISSIONS TO ADR_RW;

GRANT SELECT ON ADR.RPT_PERMISSIONS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_PERMISSIONS TO ES_RW;
ALTER TABLE ADR.RPT_PERSON_MERGE_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_PERSON_MERGE_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_PERSON_MERGE_STATUS
(
  RPT_PERSON_MERGE_STATUS_ID  NUMBER(20)        NOT NULL,
  RPT_SCHEDULED_REPORT_ID     NUMBER(20)        NOT NULL,
  STD_PERSONMERGESTATUS_ID    NUMBER(20)        NOT NULL,
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_PERSON_MERGE_STATUS IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RPT_PERSON_MERGE_STATUS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_PERSON_MERGE_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTPRSNMRG_PK ON ADR.RPT_PERSON_MERGE_STATUS
(RPT_PERSON_MERGE_STATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPRSNMRG_SCHEDRPT_FK_I ON ADR.RPT_PERSON_MERGE_STATUS
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPRSNMRG_STDPRSNMRG_FK_I ON ADR.RPT_PERSON_MERGE_STATUS
(STD_PERSONMERGESTATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_PERSON_MERGE_STATUS FOR ADR.RPT_PERSON_MERGE_STATUS;


ALTER TABLE ADR.RPT_PERSON_MERGE_STATUS ADD (
  CONSTRAINT RPTPRSNMRG_PK
  PRIMARY KEY
  (RPT_PERSON_MERGE_STATUS_ID)
  USING INDEX ADR.RPTPRSNMRG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_PERSON_MERGE_STATUS ADD (
  CONSTRAINT RPTPRSNMRG_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_PERSON_MERGE_STATUS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_PERSON_MERGE_STATUS TO ADR_RW;

GRANT SELECT ON ADR.RPT_PERSON_MERGE_STATUS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_PERSON_MERGE_STATUS TO ES_RW;
ALTER TABLE ADR.RPT_PH_REJECT_REASON
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_PH_REJECT_REASON CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_PH_REJECT_REASON
(
  RPT_PH_REJECT_REASON_ID  NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  STD_REJECTEDREMARKS_ID   NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_PH_REJECT_REASON IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RPT_PH_REJECT_REASON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.STD_REJECTEDREMARKS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_PH_REJECT_REASON.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTPHRJCT_PK ON ADR.RPT_PH_REJECT_REASON
(RPT_PH_REJECT_REASON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPHRJCT_SCHEDRPT_FK_I ON ADR.RPT_PH_REJECT_REASON
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPHRJCT_STDREJRMRK_FK_I ON ADR.RPT_PH_REJECT_REASON
(STD_REJECTEDREMARKS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_PH_REJECT_REASON FOR ADR.RPT_PH_REJECT_REASON;


ALTER TABLE ADR.RPT_PH_REJECT_REASON ADD (
  CONSTRAINT RPTPHRJCT_PK
  PRIMARY KEY
  (RPT_PH_REJECT_REASON_ID)
  USING INDEX ADR.RPTPHRJCT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_PH_REJECT_REASON ADD (
  CONSTRAINT RPTPHRJCT_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_PH_REJECT_REASON TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_PH_REJECT_REASON TO ADR_RW;

GRANT SELECT ON ADR.RPT_PH_REJECT_REASON TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_PH_REJECT_REASON TO ES_RW;
ALTER TABLE ADR.RPT_PRIORITY_GROUP
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_PRIORITY_GROUP CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_PRIORITY_GROUP
(
  RPT_PRIORITY_GROUP_ID       NUMBER(20)        NOT NULL,
  RPT_SCHEDULED_REPORT_ID     NUMBER(20)        NOT NULL,
  PRIORITY_GROUP_TYPE_ID      NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID  NUMBER(20),
  RECORD_CREATED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE         DATE              NOT NULL,
  RECORD_MODIFIED_BY          VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE        DATE              NOT NULL,
  RECORD_MODIFIED_COUNT       NUMBER(10)        NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_PRIORITY_GROUP IS 'ESR reporting table. Allows for reporting to be performed on each of the priority groups and priority sub-groups.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RPT_PRIORITY_GROUP_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_PRIORITY_GROUP.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.RPTPGRP_STD_ENRLMNTPSGRP_FK_I ON ADR.RPT_PRIORITY_GROUP
(PRIORITY_SUB_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPTPRIOGRP_PK ON ADR.RPT_PRIORITY_GROUP
(RPT_PRIORITY_GROUP_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPRIOGRP_PRTYGRP_FK_I ON ADR.RPT_PRIORITY_GROUP
(PRIORITY_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTPRIOGRP_SCHEDRPT_FK_I ON ADR.RPT_PRIORITY_GROUP
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_PRIORITY_GROUP FOR ADR.RPT_PRIORITY_GROUP;


ALTER TABLE ADR.RPT_PRIORITY_GROUP ADD (
  CONSTRAINT RPTPRIOGRP_PK
  PRIMARY KEY
  (RPT_PRIORITY_GROUP_ID)
  USING INDEX ADR.RPTPRIOGRP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_PRIORITY_GROUP ADD (
  CONSTRAINT RPTPRIOGRP_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_PRIORITY_GROUP TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_PRIORITY_GROUP TO ADR_RW;

GRANT SELECT ON ADR.RPT_PRIORITY_GROUP TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_PRIORITY_GROUP TO ES_RW;
ALTER TABLE ADR.RPT_QUERY_SITE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_QUERY_SITE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_QUERY_SITE
(
  REPORT_QUERY_SITE_ID     NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  ID                       NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_QUERY_SITE IS 'ESR reporting table. Can be used to generate reports showing which site submitted the query';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.REPORT_QUERY_SITE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_QUERY_SITE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTQRYSITE_PK ON ADR.RPT_QUERY_SITE
(REPORT_QUERY_SITE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTQRYSITE_SCHEDRPT_FK_I ON ADR.RPT_QUERY_SITE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTQRYSITE_STDINST_FK_I ON ADR.RPT_QUERY_SITE
(ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_QUERY_SITE FOR ADR.RPT_QUERY_SITE;


ALTER TABLE ADR.RPT_QUERY_SITE ADD (
  CONSTRAINT RPTQRYSITE_PK
  PRIMARY KEY
  (REPORT_QUERY_SITE_ID)
  USING INDEX ADR.RPTQRYSITE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_QUERY_SITE ADD (
  CONSTRAINT RPTQRYSITE_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_QUERY_SITE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_QUERY_SITE TO ADR_RW;

GRANT SELECT ON ADR.RPT_QUERY_SITE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_QUERY_SITE TO ES_RW;
ALTER TABLE ADR.RPT_REGISTRY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_REGISTRY CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_REGISTRY
(
  RPT_REGISTRY_ID          NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  REGISTRY_TYPE_ID         NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_REGISTRY IS 'ESR reporting table. Allows for reporting to be performed on each of the Registries, namely; Prisoner of War Registry Purple Heart Registry SHAD Registry';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RPT_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_REGISTRY.REGISTRY_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_REGISTRY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTRGSTRY_PK ON ADR.RPT_REGISTRY
(RPT_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTRGSTRY_SCHEDRPT_FK_I ON ADR.RPT_REGISTRY
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTRGSTRY_STD_REGTYPE_FK_I ON ADR.RPT_REGISTRY
(REGISTRY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_REGISTRY FOR ADR.RPT_REGISTRY;


ALTER TABLE ADR.RPT_REGISTRY ADD (
  CONSTRAINT RPTRGSTRY_PK
  PRIMARY KEY
  (RPT_REGISTRY_ID)
  USING INDEX ADR.RPTRGSTRY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_REGISTRY ADD (
  CONSTRAINT RPTRGSTRY_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_REGISTRY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_REGISTRY TO ADR_RW;

GRANT SELECT ON ADR.RPT_REGISTRY TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_REGISTRY TO ES_RW;
ALTER TABLE ADR.RPT_SCHEDULED_REPORTS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_SCHEDULED_REPORTS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_SCHEDULED_REPORTS
(
  RPT_SCHEDULED_REPORT_ID         NUMBER(20)    NOT NULL,
  USERID                          NUMBER(20),
  RPT_STANDARD_REPORT_ID          NUMBER(20)    NOT NULL,
  RPT_DIVISION_TYPE_ID            NUMBER(20),
  RPT_SCHEDULE_TYPE_ID            NUMBER(20),
  RPT_MONTH_LIST_TYPE_ID          NUMBER(20),
  RPT_QUARTER_LIST_TYPE_ID        NUMBER(20),
  RPT_DAY_OF_QUARTER_TYPE_ID      NUMBER(20),
  RPT_DAY_OF_MONTH_TYPE_ID        NUMBER(20),
  RPT_DAY_OF_WEEK_TYPE_ID         NUMBER(20),
  RPT_WEEK_OF_MONTH_TYPE_ID       NUMBER(20),
  RPT_OUTPUT_FORMAT_TYPE_ID       NUMBER(20),
  RPT_OUTPUT_FILE_TYPE_ID         NUMBER(20),
  RPT_WORK_ITEM_STATUS_TYPE_ID    NUMBER(20),
  RPT_PERIOD_TYPE_ID              NUMBER(20),
  RPT_SITE_DISPLAY_TYPE_ID        NUMBER(20),
  RPT_DETAIL_BY_TYPE_ID           NUMBER(20),
  RPT_DETAIL_FOR_TYPE_ID          NUMBER(20),
  RPT_RUN_FREQUENCY_TYPE_ID       NUMBER(20),
  RPT_EXCEPTION_TYPE_ID           NUMBER(20),
  RPT_ENROLLPRSTAT_ID             NUMBER(20),
  REPORT_BATCH_NUMBER             VARCHAR2(20 BYTE),
  BEGIN_INCOME_YEAR               NUMBER(4),
  END_INCOME_YEAR                 NUMBER(4),
  REPORT_GENERATE_DATE            DATE,
  REPORT_FROM_DATE                DATE,
  REPORT_TO_DATE                  DATE,
  REPORT_AS_OF_DATE               DATE,
  REPORT_RECIPIENT_EMAIL          VARCHAR2(1000 BYTE),
  REPORTING_PERIOD_YEAR           NUMBER(4),
  REPORTING_PERIOD_FROM_DATE      DATE,
  REPORTING_PERIOD_TO_DATE        DATE,
  LETTER_FILE_RECEIVED_DATE       DATE,
  ENROLLMENT_CHANGE_EFCTV_DATE    DATE,
  ADDRESS_UPDATE_DAYS_COUNT       NUMBER(10),
  PURPLE_HEART_DAYS_COUNT         NUMBER(10),
  ENROLLMENT_STATUS_RECORD_COUNT  NUMBER(10),
  DAY_NUMBER                      NUMBER(4),
  HOUR_NUMBER                     NUMBER(4),
  MINUTE_NUMBER                   NUMBER(4),
  RANDOM_RECORDS_COUNT            NUMBER(10),
  INCLUDE_USERS_FLAG              NUMBER(1)     DEFAULT 0                     NOT NULL,
  RUN_REPORT_BY_USER_FLAG         NUMBER(1)     DEFAULT 0                     NOT NULL,
  OUTLIER_DAYS                    NUMBER(3),
  UPDATE_BETWEEN_DAYS             NUMBER(3),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  RPT_SORTBY_TYPE_ID              NUMBER(20),
  PRIOR_ENROLLMENT                VARCHAR2(5 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_SCHEDULED_REPORTS IS 'ESR Reporting table; This table stores all the reports that have been scheduled to run';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RPT_STANDARD_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.REPORT_BATCH_NUMBER IS 'Batch number that the report belongs to.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.BEGIN_INCOME_YEAR IS 'Income Year Specified for Report run';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.END_INCOME_YEAR IS 'Income Year Specified for Report run';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.REPORT_GENERATE_DATE IS 'Date the report was generated';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.ADDRESS_UPDATE_DAYS_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.PURPLE_HEART_DAYS_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.ENROLLMENT_STATUS_RECORD_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.INCLUDE_USERS_FLAG IS 'Numeric FLAG field - 1 indicates that Users are included in the report output email';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RUN_REPORT_BY_USER_FLAG IS 'Numeric FLAG field - 1 indicates that report is being run on a per user basis';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.RPT_SORTBY_TYPE_ID IS 'Foreign Key from  RPT_SORTBY_TYPE table';

COMMENT ON COLUMN ADR.RPT_SCHEDULED_REPORTS.PRIOR_ENROLLMENT IS 'Indicates prior enrollment for the report generated either with Prior enrollment, without prior enrollment or all records. Valid values: all, yes, and no';


CREATE UNIQUE INDEX ADR.SCHEDRPT_PK ON ADR.RPT_SCHEDULED_REPORTS
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SCHEDRPT_RPTDIVTYPE_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DIVISION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SCHEDRPT_RPTWRKITM_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_WORK_ITEM_STATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SCHEDRPT_USER_1_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTDAYQRTR_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DAY_OF_QUARTER_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTDTLBY_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DETAIL_BY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTDTLFOR_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DETAIL_FOR_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTDYWKTYP_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DAY_OF_WEEK_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTENRLPRSTAT_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_ENROLLPRSTAT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTEXCPT_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_EXCEPTION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTFORMAT_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_OUTPUT_FORMAT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTFREQTYP_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_RUN_FREQUENCY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTMNTHDAY_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_DAY_OF_MONTH_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTMTHLST_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_MONTH_LIST_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTOUTFILE_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_OUTPUT_FILE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTPERIOD_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_PERIOD_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTQRTRLST_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_QUARTER_LIST_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTSCHED_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_SCHEDULE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTSITEDSP_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_SITE_DISPLAY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTSTDRPT_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_STANDARD_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SRPT_STD_RPTWKMNTH_FK_I ON ADR.RPT_SCHEDULED_REPORTS
(RPT_WEEK_OF_MONTH_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_SCHEDULED_REPORTS FOR ADR.RPT_SCHEDULED_REPORTS;


ALTER TABLE ADR.RPT_SCHEDULED_REPORTS ADD (
  CONSTRAINT AVCON_1172521913_INCLU_000
  CHECK (INCLUDE_USERS_FLAG BETWEEN 0
AND 0 OR INCLUDE_USERS_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT AVCON_1172521913_RUN_R_000
  CHECK (RUN_REPORT_BY_USER_FLAG BETWEEN 0
AND 0 OR RUN_REPORT_BY_USER_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT PRIOR_ENROLLMENT_CK
  CHECK (PRIOR_ENROLLMENT IN ('all', 'yes', 'no'))
  ENABLE VALIDATE,
  CONSTRAINT SCHEDRPT_PK
  PRIMARY KEY
  (RPT_SCHEDULED_REPORT_ID)
  USING INDEX ADR.SCHEDRPT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_SCHEDULED_REPORTS ADD (
  CONSTRAINT RPT_SORTBY_TYPE_ID_FK 
  FOREIGN KEY (RPT_SORTBY_TYPE_ID) 
  REFERENCES ADR.RPT_SORTBY_TYPE (RPT_SORTBY_TYPE_ID)
  ENABLE VALIDATE,
  CONSTRAINT SCHEDRPT_RPTDIVTYPE_FK 
  FOREIGN KEY (RPT_DIVISION_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDIVISIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SCHEDRPT_RPTWRKITM_FK 
  FOREIGN KEY (RPT_WORK_ITEM_STATUS_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTWORKITEMSTTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SCHEDRPT_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTDAYQRTR_FK 
  FOREIGN KEY (RPT_DAY_OF_QUARTER_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDAYOFQUARTERTYP (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTDTLBY_FK 
  FOREIGN KEY (RPT_DETAIL_BY_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDETAILBYTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTDTLFOR_FK 
  FOREIGN KEY (RPT_DETAIL_FOR_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDETAILFORTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTDYWKTYP_FK 
  FOREIGN KEY (RPT_DAY_OF_WEEK_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDAYOFWEEKTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTENRLPRSTAT_FK 
  FOREIGN KEY (RPT_ENROLLPRSTAT_ID) 
  REFERENCES ADRREF.STD_REPORTENROLLPRSTAT (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTEXCPT_FK 
  FOREIGN KEY (RPT_EXCEPTION_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTEXCEPTIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTFORMAT_FK 
  FOREIGN KEY (RPT_OUTPUT_FORMAT_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTOUTFORMATTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTFREQTYP_FK 
  FOREIGN KEY (RPT_RUN_FREQUENCY_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTRUNFREQUENCYTYP (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTMNTHDAY_FK 
  FOREIGN KEY (RPT_DAY_OF_MONTH_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTDAYOFMONTHTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTMTHLST_FK 
  FOREIGN KEY (RPT_MONTH_LIST_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTMONTHLIST (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTOUTFILE_FK 
  FOREIGN KEY (RPT_OUTPUT_FILE_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTOUTPUTFILETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTPERIOD_FK 
  FOREIGN KEY (RPT_PERIOD_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTPERIODTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTQRTRLST_FK 
  FOREIGN KEY (RPT_QUARTER_LIST_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTQUARTERLIST (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTSCHED_FK 
  FOREIGN KEY (RPT_SCHEDULE_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTSCHEDULETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTSITEDSP_FK 
  FOREIGN KEY (RPT_SITE_DISPLAY_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTSITEDISPLAYTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTSTDRPT_FK 
  FOREIGN KEY (RPT_STANDARD_REPORT_ID) 
  REFERENCES ADRREF.STD_RPTSTANDARDREPORTS (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SRPT_STD_RPTWKMNTH_FK 
  FOREIGN KEY (RPT_WEEK_OF_MONTH_TYPE_ID) 
  REFERENCES ADRREF.STD_RPTWEEKOFMONTHTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_SCHEDULED_REPORTS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_SCHEDULED_REPORTS TO ADR_RW;

GRANT SELECT ON ADR.RPT_SCHEDULED_REPORTS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_SCHEDULED_REPORTS TO ES_RW;
ALTER TABLE ADR.RPT_SITE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_SITE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_SITE
(
  RPT_SITE_ID              NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  STD_INSTITUTION_ID       NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_SITE IS 'ESR reporting table. Allows for reports to be printed on a per-site basis.';

COMMENT ON COLUMN ADR.RPT_SITE.RPT_SITE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_SITE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_SITE.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.RPT_SITE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_SITE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_SITE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_SITE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_SITE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTSITE_PK ON ADR.RPT_SITE
(RPT_SITE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTSITE_SCHEDRPT_FK_I ON ADR.RPT_SITE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTSITE_STDINST_FK_I ON ADR.RPT_SITE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_SITE FOR ADR.RPT_SITE;


ALTER TABLE ADR.RPT_SITE ADD (
  CONSTRAINT RPTSITE_PK
  PRIMARY KEY
  (RPT_SITE_ID)
  USING INDEX ADR.RPTSITE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_SITE ADD (
  CONSTRAINT RPTSITE_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_SITE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_SITE TO ADR_RW;

GRANT SELECT ON ADR.RPT_SITE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_SITE TO ES_RW;
ALTER TABLE ADR.RPT_SORTBY_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_SORTBY_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_SORTBY_TYPE
(
  RPT_SORTBY_TYPE_ID     NUMBER(20)             NOT NULL,
  CODE                   VARCHAR2(2 BYTE)       NOT NULL,
  NAME                   VARCHAR2(50 BYTE),
  DESCRIPTION            VARCHAR2(100 BYTE)     NOT NULL,
  ISACTIVE               CHAR(1 BYTE)           NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_SORTBY_TYPE IS 'lookup table for Report sort by.';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RPT_SORTBY_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.CODE IS 'Lookup code short name';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.NAME IS 'LOOKUP CODE LONG NAME VALUE.';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.DESCRIPTION IS 'Description of the Report Sort By Code';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.ISACTIVE IS 'FLAG TO SHOW ACTIVE/INACTIVE';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_SORTBY_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPT_SORTBY_TYPE_CD_UK ON ADR.RPT_SORTBY_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPT_SORTBY_TYPE_DESC_UK ON ADR.RPT_SORTBY_TYPE
(DESCRIPTION)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.RPT_SORTBY_TYPE_PK ON ADR.RPT_SORTBY_TYPE
(RPT_SORTBY_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_SORTBY_TYPE FOR ADR.RPT_SORTBY_TYPE;


ALTER TABLE ADR.RPT_SORTBY_TYPE ADD (
  CONSTRAINT RPT_SORTBY_TYPE_PK
  PRIMARY KEY
  (RPT_SORTBY_TYPE_ID)
  USING INDEX ADR.RPT_SORTBY_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_SORTBY_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_SORTBY_TYPE TO ADR_RW;
ALTER TABLE ADR.RPT_STATUS_COMPONENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_STATUS_COMPONENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_STATUS_COMPONENT
(
  RPT_STATUS_COMPONENT_ID    NUMBER(20)         NOT NULL,
  RPT_SCHEDULED_REPORT_ID    NUMBER(20)         NOT NULL,
  STD_RPTSTATUSCOMPONENT_ID  NUMBER(20)         NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_STATUS_COMPONENT IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RPT_STATUS_COMPONENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_STATUS_COMPONENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTSTATCMP_PK ON ADR.RPT_STATUS_COMPONENT
(RPT_STATUS_COMPONENT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTSTATCMP_SCHEDRPT_FK_I ON ADR.RPT_STATUS_COMPONENT
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTSTATCMP_STDRPTSTCM_FK_I ON ADR.RPT_STATUS_COMPONENT
(STD_RPTSTATUSCOMPONENT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_STATUS_COMPONENT FOR ADR.RPT_STATUS_COMPONENT;


ALTER TABLE ADR.RPT_STATUS_COMPONENT ADD (
  CONSTRAINT RPTSTATCMP_PK
  PRIMARY KEY
  (RPT_STATUS_COMPONENT_ID)
  USING INDEX ADR.RPTSTATCMP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_STATUS_COMPONENT ADD (
  CONSTRAINT RPTSTATCMP_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTSTATCMP_STDRPTSTCM_FK 
  FOREIGN KEY (STD_RPTSTATUSCOMPONENT_ID) 
  REFERENCES ADRREF.STD_RPTSTATUSCOMPONENT (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_STATUS_COMPONENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_STATUS_COMPONENT TO ADR_RW;

GRANT SELECT ON ADR.RPT_STATUS_COMPONENT TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_STATUS_COMPONENT TO ES_RW;
ALTER TABLE ADR.RPT_USERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_USERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_USERS
(
  RPT_USER_ID              NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  USERID                   NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_USERS IS 'ESR reporting table: Shows which user scheduled a particular report for run.';

COMMENT ON COLUMN ADR.RPT_USERS.RPT_USER_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_USERS.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_USERS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_USERS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_USERS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_USERS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_USERS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_USERS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTUSERS_PK ON ADR.RPT_USERS
(RPT_USER_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTUSERS_SCHEDRPT_FK_I ON ADR.RPT_USERS
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTUSERS_USER_1_FK_I ON ADR.RPT_USERS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_USERS FOR ADR.RPT_USERS;


ALTER TABLE ADR.RPT_USERS ADD (
  CONSTRAINT RPTUSERS_PK
  PRIMARY KEY
  (RPT_USER_ID)
  USING INDEX ADR.RPTUSERS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_USERS ADD (
  CONSTRAINT RPTUSERS_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTUSERS_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_USERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_USERS TO ADR_RW;

GRANT SELECT ON ADR.RPT_USERS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_USERS TO ES_RW;
ALTER TABLE ADR.RPT_USER_GROUP
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_USER_GROUP CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_USER_GROUP
(
  RPT_USER_GROUP_ID        NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  USER_GROUP_TYPE_ID       NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_USER_GROUP IS 'ESR reporting table. allows for reporting based on which group of users performed the query';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RPT_USER_GROUP_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.USER_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_USER_GROUP.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTUSRGRP_PK ON ADR.RPT_USER_GROUP
(RPT_USER_GROUP_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTUSRGRP_SCHEDRPT_FK_I ON ADR.RPT_USER_GROUP
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTUSRGRP_USRGRPTYP_FK_I ON ADR.RPT_USER_GROUP
(USER_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_USER_GROUP FOR ADR.RPT_USER_GROUP;


ALTER TABLE ADR.RPT_USER_GROUP ADD (
  CONSTRAINT RPTUSRGRP_PK
  PRIMARY KEY
  (RPT_USER_GROUP_ID)
  USING INDEX ADR.RPTUSRGRP_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_USER_GROUP ADD (
  CONSTRAINT RPTUSRGRP_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_USER_GROUP TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_USER_GROUP TO ADR_RW;

GRANT SELECT ON ADR.RPT_USER_GROUP TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_USER_GROUP TO ES_RW;
ALTER TABLE ADR.RPT_VBA_QUERY_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_VBA_QUERY_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_VBA_QUERY_STATUS
(
  RPT_VBA_QUERY_STATUS_ID   NUMBER(20)          NOT NULL,
  RPT_SCHEDULED_REPORT_ID   NUMBER(20)          NOT NULL,
  VBA_QUERY_STATUS_TYPE_ID  NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_VBA_QUERY_STATUS IS 'ESR reporting table. Allows for reporting based on each of the VBA query status types, such as: No VBA Data on File VBA Data Auto-upload VBA Data Pending Rev VBA Data Manually Ac VBA Data Rejected VBA Query Initiated VBA Queried - Pending VBA Data Inconclusive Unsolicited Change O Unsolicited Eligible Unsolicited Lazarus';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RPT_VBA_QUERY_STATUS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.VBA_QUERY_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_VBA_QUERY_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTVBAQRY_PK ON ADR.RPT_VBA_QUERY_STATUS
(RPT_VBA_QUERY_STATUS_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTVBAQRY_SCHEDRPT_FK_I ON ADR.RPT_VBA_QUERY_STATUS
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTVBAQRY_STD_VBAQURYST_FK_I ON ADR.RPT_VBA_QUERY_STATUS
(VBA_QUERY_STATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_VBA_QUERY_STATUS FOR ADR.RPT_VBA_QUERY_STATUS;


ALTER TABLE ADR.RPT_VBA_QUERY_STATUS ADD (
  CONSTRAINT RPTVBAQRY_PK
  PRIMARY KEY
  (RPT_VBA_QUERY_STATUS_ID)
  USING INDEX ADR.RPTVBAQRY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_VBA_QUERY_STATUS ADD (
  CONSTRAINT RPTVBAQRY_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_VBA_QUERY_STATUS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_VBA_QUERY_STATUS TO ADR_RW;

GRANT SELECT ON ADR.RPT_VBA_QUERY_STATUS TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_VBA_QUERY_STATUS TO ES_RW;
ALTER TABLE ADR.RPT_WKF_CASE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.RPT_WKF_CASE CASCADE CONSTRAINTS;

CREATE TABLE ADR.RPT_WKF_CASE
(
  RPT_WKF_CASE_ID          NUMBER(20)           NOT NULL,
  RPT_SCHEDULED_REPORT_ID  NUMBER(20)           NOT NULL,
  WKF_CASE_TYPE_ID         NUMBER(20)           NOT NULL,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.RPT_WKF_CASE IS 'ESR reporting table. Reports can be run in order to track the various Purple Heart reject reasons such as: Entered In Error No Document Received Unacceptable Documentation Undeliverable Mail Unsupported Purple Heart VAMC';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RPT_WKF_CASE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RPT_SCHEDULED_REPORT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.RPT_WKF_CASE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.RPTWKFCASE_PK ON ADR.RPT_WKF_CASE
(RPT_WKF_CASE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTWKFCASE_SCHEDRPT_FK_I ON ADR.RPT_WKF_CASE
(RPT_SCHEDULED_REPORT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.RPTWKFCASE_WKFCASETYP_FK_I ON ADR.RPT_WKF_CASE
(WKF_CASE_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM RPT_WKF_CASE FOR ADR.RPT_WKF_CASE;


ALTER TABLE ADR.RPT_WKF_CASE ADD (
  CONSTRAINT RPTWKFCASE_PK
  PRIMARY KEY
  (RPT_WKF_CASE_ID)
  USING INDEX ADR.RPTWKFCASE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.RPT_WKF_CASE ADD (
  CONSTRAINT RPTWKFCASE_SCHEDRPT_FK 
  FOREIGN KEY (RPT_SCHEDULED_REPORT_ID) 
  REFERENCES ADR.RPT_SCHEDULED_REPORTS (RPT_SCHEDULED_REPORT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT RPTWKFCASE_WKFCASETYP_FK 
  FOREIGN KEY (WKF_CASE_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.RPT_WKF_CASE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.RPT_WKF_CASE TO ADR_RW;

GRANT SELECT ON ADR.RPT_WKF_CASE TO ES_R;

GRANT INSERT, SELECT ON ADR.RPT_WKF_CASE TO ES_RW;
DROP TABLE ADR.RUPD$_HL7_TRANSACTION_LOG CASCADE CONSTRAINTS;

CREATE GLOBAL TEMPORARY TABLE ADR.RUPD$_HL7_TRANSACTION_LOG
(
  HL7_TRANSACTION_LOG_ID  NUMBER(20),
  DMLTYPE$$               VARCHAR2(1 BYTE),
  SNAPID                  INTEGER,
  CHANGE_VECTOR$$         RAW(255)
)
ON COMMIT PRESERVE ROWS
RESULT_CACHE (MODE DEFAULT)
NOCACHE;

COMMENT ON TABLE ADR.RUPD$_HL7_TRANSACTION_LOG IS 'temporary updatable snapshot log';
ALTER TABLE ADR.SEEDING
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SEEDING CASCADE CONSTRAINTS;

CREATE TABLE ADR.SEEDING
(
  SEEDING_ID                NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20)          NOT NULL,
  STD_SEEDSTATUS_ID         NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  ORIGINATING_PROCESS_NAME  VARCHAR2(50 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(18)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.SEEDING.SEEDING_ID IS 'Unique identifier for Seeding Overview';

COMMENT ON COLUMN ADR.SEEDING.STD_SEEDSTATUS_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.SEEDING.STD_INSTITUTION_ID IS 'Unique Numeric Primary Key';

COMMENT ON COLUMN ADR.SEEDING.ORIGINATING_PROCESS_NAME IS 'Indicates if seeding process was initialized by batch or MSDS (part of Z07 process for on demand seeding).';

COMMENT ON COLUMN ADR.SEEDING.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.SEEDING.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SEEDING.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.SEEDING.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SEEDING.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SEEDING_PK ON ADR.SEEDING
(SEEDING_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SEED_PRSN_FK_I ON ADR.SEEDING
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SEED_SEEDSTAT_FK_I ON ADR.SEEDING
(STD_SEEDSTATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SEED_STDINST_FK_I ON ADR.SEEDING
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SEEDING FOR ADR.SEEDING;


ALTER TABLE ADR.SEEDING ADD (
  CONSTRAINT SEEDING_PK
  PRIMARY KEY
  (SEEDING_ID)
  USING INDEX ADR.SEEDING_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SEEDING ADD (
  CONSTRAINT RELATIONSHIP39 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SEEDING TO ADR_R;

GRANT INSERT, SELECT, UPDATE ON ADR.SEEDING TO ADR_RW;

GRANT SELECT ON ADR.SEEDING TO ES_R;

GRANT INSERT, SELECT ON ADR.SEEDING TO ES_RW;
ALTER TABLE ADR.SELF_IDENTIFIED_GENDER
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SELF_IDENTIFIED_GENDER CASCADE CONSTRAINTS;

CREATE TABLE ADR.SELF_IDENTIFIED_GENDER
(
  SELF_IDENTIFIED_GENDER_ID  NUMBER(20)         NOT NULL,
  CODE                       VARCHAR2(10 BYTE)  NOT NULL,
  NAME                       VARCHAR2(50 BYTE)  NOT NULL,
  DESCRIPTION                VARCHAR2(250 BYTE) NOT NULL,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.SELF_IDENTIFIED_GENDER_PK ON ADR.SELF_IDENTIFIED_GENDER
(SELF_IDENTIFIED_GENDER_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIG_CODE_NU_I ON ADR.SELF_IDENTIFIED_GENDER
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIG_NAME_NU_I ON ADR.SELF_IDENTIFIED_GENDER
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SELF_IDENTIFIED_GENDER FOR ADR.SELF_IDENTIFIED_GENDER;


ALTER TABLE ADR.SELF_IDENTIFIED_GENDER ADD (
  CONSTRAINT SELF_IDENTIFIED_GENDER_PK
  PRIMARY KEY
  (SELF_IDENTIFIED_GENDER_ID)
  USING INDEX ADR.SELF_IDENTIFIED_GENDER_PK
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SELF_IDENTIFIED_GENDER TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SELF_IDENTIFIED_GENDER TO ADR_RW;

GRANT SELECT ON ADR.SELF_IDENTIFIED_GENDER TO ES_R;

GRANT INSERT, SELECT ON ADR.SELF_IDENTIFIED_GENDER TO ES_RW;
ALTER TABLE ADR.SERVICE_CONNECTED
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SERVICE_CONNECTED CASCADE CONSTRAINTS;

CREATE TABLE ADR.SERVICE_CONNECTED
(
  SERVICE_CONNECTED_ID            NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  SC_TOTAL_AMOUNT                 NUMBER(10,2),
  SERVICE_AWARD_DATE_TXT          VARCHAR2(14 BYTE),
  SC_COMBINED_PERCENT             NUMBER(3),
  SC_COMBINED_PCT_EFCTV_DATE_TXT  VARCHAR2(14 BYTE),
  SC_PERMANENT_AND_TOTAL_IND      NUMBER(1)     DEFAULT 0,
  SC_PERMNT_AND_TOTAL_EFCTV_DT    DATE,
  UNEMPLOYABLE_INDICATOR          NUMBER(1)     DEFAULT 0,
  SERVICE_CONNECTED_IND           NUMBER(1),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SERVICE_CONNECTED IS 'The table stores information about a persons disabilities that are related to their time in the military, including the award amount, the associated dates, and whether the disability is permanent and total, and whether the person is unemployable. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SERVICE_CONNECTED_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SC_TOTAL_AMOUNT IS 'Service Connected - Total check amount';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SERVICE_AWARD_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SC_COMBINED_PERCENT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SC_COMBINED_PCT_EFCTV_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SC_PERMANENT_AND_TOTAL_IND IS 'BR1611 Permanent & Total Indicator is defined as a code that indicates if the patient is permanently and totally disabled determined by VARO due to a service- connected condition.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.SC_PERMNT_AND_TOTAL_EFCTV_DT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.UNEMPLOYABLE_INDICATOR IS 'BR1613 Unemployable Indicator is defined as a code that indicates if this patient is rated unemployable by the VARO due to a service connected condition.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SVCCON_PK ON ADR.SERVICE_CONNECTED
(SERVICE_CONNECTED_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SVCCON_PRSN_FK_I ON ADR.SERVICE_CONNECTED
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SERVICE_CONNECTED_AUDIR_TRGR
after Update or Delete or Insert
on ADR.SERVICE_CONNECTED
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SERVICE_CONNECTED_H
		(
		SERVICE_CONNECTED_H_ID,
		SERVICE_CONNECTED_ID,
		SC_TOTAL_AMOUNT,
		SERVICE_AWARD_DATE_TXT,
		SC_COMBINED_PERCENT,
		SC_COMBINED_PCT_EFCTV_DATE_TXT,
		SC_PERMANENT_AND_TOTAL_IND,
		SC_PERMNT_AND_TOTAL_EFCTV_DT,
		UNEMPLOYABLE_INDICATOR,
		SERVICE_CONNECTED_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SERVICE_CONNECTED_H_S.nextval,
		:old.SERVICE_CONNECTED_ID,
		:old.SC_TOTAL_AMOUNT,
		:old.SERVICE_AWARD_DATE_TXT,
		:old.SC_COMBINED_PERCENT,
		:old.SC_COMBINED_PCT_EFCTV_DATE_TXT,
		:old.SC_PERMANENT_AND_TOTAL_IND,
		:old.SC_PERMNT_AND_TOTAL_EFCTV_DT,
		:old.UNEMPLOYABLE_INDICATOR,
		:old.SERVICE_CONNECTED_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.PERSON_ID,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SERVICE_CONNECTED_H
		(
		SERVICE_CONNECTED_H_ID,
		SERVICE_CONNECTED_ID,
		SC_TOTAL_AMOUNT,
		SERVICE_AWARD_DATE_TXT,
		SC_COMBINED_PERCENT,
		SC_COMBINED_PCT_EFCTV_DATE_TXT,
		SC_PERMANENT_AND_TOTAL_IND,
		SC_PERMNT_AND_TOTAL_EFCTV_DT,
		UNEMPLOYABLE_INDICATOR,
		SERVICE_CONNECTED_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SERVICE_CONNECTED_H_S.nextval,
		:new.SERVICE_CONNECTED_ID,
		:new.SC_TOTAL_AMOUNT,
		:new.SERVICE_AWARD_DATE_TXT,
		:new.SC_COMBINED_PERCENT,
		:new.SC_COMBINED_PCT_EFCTV_DATE_TXT,
		:new.SC_PERMANENT_AND_TOTAL_IND,
		:new.SC_PERMNT_AND_TOTAL_EFCTV_DT,
		:new.UNEMPLOYABLE_INDICATOR,
		:new.SERVICE_CONNECTED_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.PERSON_ID,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end SERVICE_CONNECTED_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SERVICE_CONNECTED FOR ADR.SERVICE_CONNECTED;


ALTER TABLE ADR.SERVICE_CONNECTED ADD (
  CONSTRAINT SVCCON_PK
  PRIMARY KEY
  (SERVICE_CONNECTED_ID)
  USING INDEX ADR.SVCCON_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SERVICE_CONNECTED ADD (
  CONSTRAINT SVCCON_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SERVICE_CONNECTED TO ADREXT;

GRANT SELECT ON ADR.SERVICE_CONNECTED TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SERVICE_CONNECTED TO ADR_RW;

GRANT SELECT ON ADR.SERVICE_CONNECTED TO ES_R;

GRANT INSERT, SELECT ON ADR.SERVICE_CONNECTED TO ES_RW;
ALTER TABLE ADR.SERVICE_CONNECTED_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SERVICE_CONNECTED_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SERVICE_CONNECTED_H
(
  SERVICE_CONNECTED_H_ID          NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  SERVICE_CONNECTED_ID            NUMBER(20),
  PERSON_ID                       NUMBER(20),
  SC_TOTAL_AMOUNT                 NUMBER(10,2),
  SERVICE_AWARD_DATE_TXT          VARCHAR2(14 BYTE),
  SC_COMBINED_PERCENT             NUMBER(3),
  SC_COMBINED_PCT_EFCTV_DATE_TXT  VARCHAR2(14 BYTE),
  SC_PERMANENT_AND_TOTAL_IND      NUMBER(1)     DEFAULT 0,
  SC_PERMNT_AND_TOTAL_EFCTV_DT    DATE,
  UNEMPLOYABLE_INDICATOR          NUMBER(1)     DEFAULT 0,
  SERVICE_CONNECTED_IND           NUMBER(1),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10)
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SERVICE_CONNECTED_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SERVICE_CONNECTED_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SERVICE_CONNECTED_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SC_TOTAL_AMOUNT IS 'Service Connected - Total check amount';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SERVICE_AWARD_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SC_COMBINED_PERCENT IS 'Service Connected  - total combined percentage.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SC_COMBINED_PCT_EFCTV_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SC_PERMANENT_AND_TOTAL_IND IS 'BR1611 Permanent & Total Indicator is defined as a code that indicates if the patient is permanently and totally disabled determined by VARO due to a service- connected condition.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.SC_PERMNT_AND_TOTAL_EFCTV_DT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.UNEMPLOYABLE_INDICATOR IS 'BR1613 Unemployable Indicator is defined as a code that indicates if this patient is rated unemployable by the VARO due to a service connected condition.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SERVICE_CONNECTED_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SVCCONH_PK ON ADR.SERVICE_CONNECTED_H
(SERVICE_CONNECTED_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SVCCONH_PRSNID_NU_I ON ADR.SERVICE_CONNECTED_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SERVICE_CONNECTED_H FOR ADR.SERVICE_CONNECTED_H;


ALTER TABLE ADR.SERVICE_CONNECTED_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_077
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067608
  CHECK ("SERVICE_CONNECTED_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067609
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067610
  CHECK ("SERVICE_CONNECTED_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067611
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067612
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067613
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067614
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067615
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067616
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SVCCONH_PK
  PRIMARY KEY
  (SERVICE_CONNECTED_H_ID)
  USING INDEX ADR.SVCCONH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SERVICE_CONNECTED_H TO ADR_R;

GRANT SELECT ON ADR.SERVICE_CONNECTED_H TO ADR_RW;

GRANT SELECT ON ADR.SERVICE_CONNECTED_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SERVICE_CONNECTED_H TO ES_RW;
ALTER TABLE ADR.SHAD_REGISTRY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SHAD_REGISTRY CASCADE CONSTRAINTS;

CREATE TABLE ADR.SHAD_REGISTRY
(
  SHAD_REGISTRY_ID       NUMBER(20)             NOT NULL,
  REGISTRY_TRAIT_ID      NUMBER(20),
  PERSON_ID              NUMBER(20),
  SHAD_STDYESNO_ID       NUMBER(20),
  CLAIM_NUMBER           VARCHAR2(20 BYTE),
  LEGACY_USERNAME        VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SHAD_REGISTRY IS 'The contains data about persons who have been exposed to hazardous environments as part of their military service. The Shipboard Hazard and Defense (SHAD) data originates from the Veterans Benefits Administration (VBA). ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.SHAD_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SHADRGSTRY_PK ON ADR.SHAD_REGISTRY
(SHAD_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADRGSTRY_PRSN_FK_I ON ADR.SHAD_REGISTRY
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADRGSTRY_REGTRAIT_FK_I ON ADR.SHAD_REGISTRY
(REGISTRY_TRAIT_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADRGSTRY_STDYESNO_FK_I ON ADR.SHAD_REGISTRY
(SHAD_STDYESNO_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SHAD_REGISTRY_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.SHAD_REGISTRY
REFERENCING NEW AS NEW OLD AS OLD
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SHAD_REGISTRY_H
		(
		SHAD_REGISTRY_H_ID,
		SHAD_REGISTRY_ID,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		CLAIM_NUMBER,
		SHAD_STDYESNO_ID,
		LEGACY_USERNAME,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SHAD_REGISTRY_H_S.nextval,
		:old.SHAD_REGISTRY_ID,
		:old.PERSON_ID,
		:old.REGISTRY_TRAIT_ID,
		:old.CLAIM_NUMBER,
		:old.SHAD_STDYESNO_ID,
		:old.LEGACY_USERNAME,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);

	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SHAD_REGISTRY_H
		(
		SHAD_REGISTRY_H_ID,
		SHAD_REGISTRY_ID,
		PERSON_ID,
		REGISTRY_TRAIT_ID,
		CLAIM_NUMBER,
		SHAD_STDYESNO_ID,
		LEGACY_USERNAME,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SHAD_REGISTRY_H_S.nextval,
		:new.SHAD_REGISTRY_ID,
		:new.PERSON_ID,
		:new.REGISTRY_TRAIT_ID,
		:new.CLAIM_NUMBER,
		:new.SHAD_STDYESNO_ID,
		:new.LEGACY_USERNAME,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);

	end if;

end SHAD_REGISTRY_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SHAD_REGISTRY FOR ADR.SHAD_REGISTRY;


ALTER TABLE ADR.SHAD_REGISTRY ADD (
  CONSTRAINT SHADRGSTRY_PK
  PRIMARY KEY
  (SHAD_REGISTRY_ID)
  USING INDEX ADR.SHADRGSTRY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SHAD_REGISTRY ADD (
  CONSTRAINT SHADRGSTRY_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT SHADRGSTRY_REGTRAIT_FK 
  FOREIGN KEY (REGISTRY_TRAIT_ID) 
  REFERENCES ADR.REGISTRY_TRAIT (REGISTRY_TRAIT_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SHAD_REGISTRY TO ADREXT;

GRANT SELECT ON ADR.SHAD_REGISTRY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SHAD_REGISTRY TO ADR_RW;

GRANT SELECT ON ADR.SHAD_REGISTRY TO ES_R;

GRANT INSERT, SELECT ON ADR.SHAD_REGISTRY TO ES_RW;
ALTER TABLE ADR.SHAD_REGISTRY_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SHAD_REGISTRY_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.SHAD_REGISTRY_DETAIL
(
  SHAD_REGISTRY_DETAIL_ID  NUMBER(20)           NOT NULL,
  SHAD_REGISTRY_ID         NUMBER(20)           NOT NULL,
  SHAD_DOCUMENT_TYPE_ID    NUMBER(20)           NOT NULL,
  DOCUMENT_RECEIVED_DATE   DATE,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SHAD_REGISTRY_DETAIL IS 'Detail table for SHAD registry.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.SHAD_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.SHAD_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.SHAD_DOCUMENT_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.DOCUMENT_RECEIVED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SHADREGDTL_PK ON ADR.SHAD_REGISTRY_DETAIL
(SHAD_REGISTRY_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADREGDTL_SHADRGSTRY_FK_I ON ADR.SHAD_REGISTRY_DETAIL
(SHAD_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADREGDTL_STD_SHADOCTYP_FK_I ON ADR.SHAD_REGISTRY_DETAIL
(SHAD_DOCUMENT_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SHAD_REGISTRY_det_audir_trgr
after Update or Delete or Insert
ON ADR.SHAD_REGISTRY_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SHAD_REGISTRY_DETAIL_H
		(
 		SHAD_REGISTRY_DETAIL_H_ID
 		,SHAD_REGISTRY_DETAIL_ID
 		,SHAD_REGISTRY_ID
 		,SHAD_DOCUMENT_TYPE_ID
		,DOCUMENT_RECEIVED_DATE
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		SHAD_REGISTRY_DETAIL_H_S.nextval,
		:old.SHAD_REGISTRY_DETAIL_ID,
		:old.SHAD_REGISTRY_ID,
		:old.SHAD_DOCUMENT_TYPE_ID,
		:OLD.DOCUMENT_RECEIVED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SHAD_REGISTRY_DETAIL_H
		(
 		SHAD_REGISTRY_DETAIL_H_ID
 		,SHAD_REGISTRY_DETAIL_ID
 		,SHAD_REGISTRY_ID
 		,SHAD_DOCUMENT_TYPE_ID
		,DOCUMENT_RECEIVED_DATE
 		,RECORD_CREATED_BY
 		,RECORD_CREATED_DATE
 		,RECORD_MODIFIED_BY
 		,RECORD_MODIFIED_DATE
 		,RECORD_MODIFIED_COUNT
		,TRANSACTION_TYPE_CODE
		)
		values
		(
		SHAD_REGISTRY_DETAIL_H_S.nextval,
		:new.SHAD_REGISTRY_DETAIL_ID,
		:new.SHAD_REGISTRY_ID,
		:new.SHAD_DOCUMENT_TYPE_ID,
		:new.DOCUMENT_RECEIVED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end SHAD_REGISTRY_det_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SHAD_REGISTRY_DETAIL FOR ADR.SHAD_REGISTRY_DETAIL;


ALTER TABLE ADR.SHAD_REGISTRY_DETAIL ADD (
  CONSTRAINT SHADREGDTL_PK
  PRIMARY KEY
  (SHAD_REGISTRY_DETAIL_ID)
  USING INDEX ADR.SHADREGDTL_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SHAD_REGISTRY_DETAIL ADD (
  CONSTRAINT SHADREGDTL_SHADRGSTRY_FK 
  FOREIGN KEY (SHAD_REGISTRY_ID) 
  REFERENCES ADR.SHAD_REGISTRY (SHAD_REGISTRY_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SHAD_REGISTRY_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SHAD_REGISTRY_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.SHAD_REGISTRY_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.SHAD_REGISTRY_DETAIL TO ES_RW;
ALTER TABLE ADR.SHAD_REGISTRY_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SHAD_REGISTRY_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SHAD_REGISTRY_DETAIL_H
(
  SHAD_REGISTRY_DETAIL_H_ID  NUMBER(20)         NOT NULL,
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE)   NOT NULL,
  SHAD_REGISTRY_DETAIL_ID    NUMBER(20)         NOT NULL,
  SHAD_REGISTRY_ID           NUMBER(20)         NOT NULL,
  SHAD_DOCUMENT_TYPE_ID      NUMBER(20),
  DOCUMENT_RECEIVED_DATE     DATE,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SHAD_REGISTRY_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.SHAD_REGISTRY_DETAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.SHAD_REGISTRY_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.SHAD_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.SHAD_DOCUMENT_TYPE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.DOCUMENT_RECEIVED_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SHADREGDH_PK ON ADR.SHAD_REGISTRY_DETAIL_H
(SHAD_REGISTRY_DETAIL_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADREGDH_SHADRGSTRY_FK_I ON ADR.SHAD_REGISTRY_DETAIL_H
(SHAD_REGISTRY_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SHAD_REGISTRY_DETAIL_H FOR ADR.SHAD_REGISTRY_DETAIL_H;


ALTER TABLE ADR.SHAD_REGISTRY_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_026
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT SHADREGDH_PK
  PRIMARY KEY
  (SHAD_REGISTRY_DETAIL_H_ID)
  USING INDEX ADR.SHADREGDH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SHAD_REGISTRY_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.SHAD_REGISTRY_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.SHAD_REGISTRY_DETAIL_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SHAD_REGISTRY_DETAIL_H TO ES_RW;
ALTER TABLE ADR.SHAD_REGISTRY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SHAD_REGISTRY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SHAD_REGISTRY_H
(
  SHAD_REGISTRY_H_ID     NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  SHAD_REGISTRY_ID       NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20),
  REGISTRY_TRAIT_ID      NUMBER(20),
  SHAD_STDYESNO_ID       NUMBER(20),
  CLAIM_NUMBER           VARCHAR2(20 BYTE),
  LEGACY_USERNAME        VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SHAD_REGISTRY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.SHAD_REGISTRY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.SHAD_REGISTRY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.REGISTRY_TRAIT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SHAD_REGISTRY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SHADRGSTRH_PK ON ADR.SHAD_REGISTRY_H
(SHAD_REGISTRY_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SHADRGSTRH_PRSNID_NU_I ON ADR.SHAD_REGISTRY_H
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SHAD_REGISTRY_H FOR ADR.SHAD_REGISTRY_H;


ALTER TABLE ADR.SHAD_REGISTRY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_056
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT SHADRGSTRH_PK
  PRIMARY KEY
  (SHAD_REGISTRY_H_ID)
  USING INDEX ADR.SHADRGSTRH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SHAD_REGISTRY_H TO ADR_R;

GRANT SELECT ON ADR.SHAD_REGISTRY_H TO ADR_RW;

GRANT SELECT ON ADR.SHAD_REGISTRY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SHAD_REGISTRY_H TO ES_RW;
ALTER TABLE ADR.SIGNATURE_IMAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SIGNATURE_IMAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.SIGNATURE_IMAGE
(
  SIGNATURE_IMAGE_ID           NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  SIGNATURE_INDICATOR_TYPE_ID  NUMBER(20),
  SUBMITTING_SITE_ID           NUMBER(20),
  INCOME_YEAR                  NUMBER(4),
  IMAGE_NUMBER                 VARCHAR2(30 BYTE),
  INDEXED_DATE                 DATE,
  USERNAME                     VARCHAR2(30 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SIGNATURE_IMAGE IS 'The SIGNATURE_IMAGE table stores information about the image of a persons signature stored during the means test process. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.SIGNATURE_IMAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.SIGNATURE_INDICATOR_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.SUBMITTING_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.IMAGE_NUMBER IS 'BR5850 Consent/Authorization Image Index Number is defined as the unique image index number associated with the veteran that is assigned by the imaging system.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.USERNAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SIGNATURE_PK ON ADR.SIGNATURE_IMAGE
(SIGNATURE_IMAGE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIGNATURE_PRSN_FK_I ON ADR.SIGNATURE_IMAGE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIGNATURE_SIGNINDTYP_FK_I ON ADR.SIGNATURE_IMAGE
(SIGNATURE_INDICATOR_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIGNATURE_STDINST_FK_I ON ADR.SIGNATURE_IMAGE
(SUBMITTING_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SIGNATURE_IMAGE_audir_trgr
after Update or Delete or Insert
ON ADR.SIGNATURE_IMAGE
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SIGNATURE_IMAGE_H
		(
		SIGNATURE_IMAGE_H_ID,
		SIGNATURE_IMAGE_ID,
		SIGNATURE_INDICATOR_TYPE_ID,
		IMAGE_NUMBER,
		INCOME_YEAR,
		INDEXED_DATE,
		USERNAME,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		SUBMITTING_SITE_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SIGNATURE_IMAGE_H_S.nextval,
		:old.SIGNATURE_IMAGE_ID,
		:old.SIGNATURE_INDICATOR_TYPE_ID,
		:old.IMAGE_NUMBER,
		:old.INCOME_YEAR,
		:old.INDEXED_DATE,
		:old.USERNAME,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.SUBMITTING_SITE_ID,
		:old.PERSON_ID,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SIGNATURE_IMAGE_H
		(
		SIGNATURE_IMAGE_H_ID,
		SIGNATURE_IMAGE_ID,
		SIGNATURE_INDICATOR_TYPE_ID,
		IMAGE_NUMBER,
		INCOME_YEAR,
		INDEXED_DATE,
		USERNAME,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		SUBMITTING_SITE_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SIGNATURE_IMAGE_H_S.nextval,
		:new.SIGNATURE_IMAGE_ID,
		:new.SIGNATURE_INDICATOR_TYPE_ID,
		:new.IMAGE_NUMBER,
		:new.INCOME_YEAR,
		:new.INDEXED_DATE,
		:new.USERNAME,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.SUBMITTING_SITE_ID,
		:new.PERSON_ID,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end SIGNATURE_IMAGE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SIGNATURE_IMAGE FOR ADR.SIGNATURE_IMAGE;


ALTER TABLE ADR.SIGNATURE_IMAGE ADD (
  CONSTRAINT SIGNATURE_PK
  PRIMARY KEY
  (SIGNATURE_IMAGE_ID)
  USING INDEX ADR.SIGNATURE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SIGNATURE_IMAGE ADD (
  CONSTRAINT SIGNATURE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SIGNATURE_IMAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SIGNATURE_IMAGE TO ADR_RW;

GRANT SELECT ON ADR.SIGNATURE_IMAGE TO ES_R;

GRANT INSERT, SELECT ON ADR.SIGNATURE_IMAGE TO ES_RW;
ALTER TABLE ADR.SIGNATURE_IMAGE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SIGNATURE_IMAGE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SIGNATURE_IMAGE_H
(
  SIGNATURE_IMAGE_H_ID         NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  SIGNATURE_IMAGE_ID           NUMBER(20)       NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  SIGNATURE_INDICATOR_TYPE_ID  NUMBER(20),
  SUBMITTING_SITE_ID           NUMBER(20),
  INCOME_YEAR                  NUMBER(4),
  IMAGE_NUMBER                 VARCHAR2(30 BYTE),
  INDEXED_DATE                 DATE,
  USERNAME                     VARCHAR2(30 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SIGNATURE_IMAGE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.SIGNATURE_IMAGE_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.SIGNATURE_IMAGE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.SIGNATURE_INDICATOR_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.SUBMITTING_SITE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.INCOME_YEAR IS 'Income year for which the visit summary information is being collected. Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.IMAGE_NUMBER IS 'BR5850 Consent/Authorization Image Index Number is defined as the unique image index number associated with the veteran that is assigned by the imaging system.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.USERNAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SIGNATURE_IMAGE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SIGNATUREH_PK ON ADR.SIGNATURE_IMAGE_H
(SIGNATURE_IMAGE_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SIGNATUREH_PRSNID_NU_I ON ADR.SIGNATURE_IMAGE_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SIGNATURE_IMAGE_H FOR ADR.SIGNATURE_IMAGE_H;


ALTER TABLE ADR.SIGNATURE_IMAGE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_072
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT SIGNATUREH_PK
  PRIMARY KEY
  (SIGNATURE_IMAGE_H_ID)
  USING INDEX ADR.SIGNATUREH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SIGNATURE_IMAGE_H TO ADR_R;

GRANT SELECT ON ADR.SIGNATURE_IMAGE_H TO ADR_RW;

GRANT SELECT ON ADR.SIGNATURE_IMAGE_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SIGNATURE_IMAGE_H TO ES_RW;
ALTER TABLE ADR.SITE_RECEIVED_ELIGIBILITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SITE_RECEIVED_ELIGIBILITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.SITE_RECEIVED_ELIGIBILITY
(
  SITE_RECEIVED_ELIGIBILITY_ID    NUMBER(20),
  PERSON_ID                       NUMBER(20),
  ELIGIBILITY_TYPE_ID             NUMBER(20),
  ALLIED_COUNTRY_TYPE_ID          NUMBER(20),
  FEDERAL_AGENCY_TYPE_ID          NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  ELIGIBILITY_EFFECTIVE_DATE      DATE,
  ELIGIBILITY_EXPIRATION_DATE     DATE,
  ELIGIBILITY_STATUS_DATE         DATE,
  STATUS_DATE_TXT                 VARCHAR2(14 BYTE),
  PRIMARY_SECONDARY_OR_OTHER_IND  VARCHAR2(1 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SITE_RECEIVED_ELIGIBILITY IS 'The SITE_RECEIVED_ELIGIBILITY table stores data about the eligibility of a particular person, when that eligibility was received from a site, as opposed to being processed at the Health Eligibility Center (HEC).';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.SITE_RECEIVED_ELIGIBILITY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.ELIGIBILITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.ALLIED_COUNTRY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.FEDERAL_AGENCY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.ELIGIBILITY_EFFECTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.ELIGIBILITY_EXPIRATION_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.ELIGIBILITY_STATUS_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.STATUS_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.PRIMARY_SECONDARY_OR_OTHER_IND IS 'Primary Eligibility Code field is defined as the field that contains the veterans current eligibility code. The Primary Eligibility Code is the code that is assigned to the veteran based on benefit factors captured on the intake process.  This is the code that is assigned the highest precedence.  It is the primary factor that determines the veterans enrollment priority.   It can be used as a single entity or in conjunction with other eligibility codes in the calculation of the enrollment priority.  The Primary Eligibility code also contributes in determining if the veteran needs to complete a Means Test. Secondary Eligibility Codes is defined as the codes that are assigned to the veteran based on the benefit factors captured on the intake process.  They are considered additional factors and combined with the Primary Eligibility Code determine the Veterans Enrollment Priority.  They also contribute in determining if the veteran needs to complete a Means Test The veteran record will contain all the eligibilities under which this patient may receive care.  The primary eligibility as well as all other eligibilities he/she is entitled to is stored by the system.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SITERCVELG_PK ON ADR.SITE_RECEIVED_ELIGIBILITY
(SITE_RECEIVED_ELIGIBILITY_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELG_PRSN_FK_I ON ADR.SITE_RECEIVED_ELIGIBILITY
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELG_STDINST_FK_I ON ADR.SITE_RECEIVED_ELIGIBILITY
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELG_STD_ALIEDCTRY_FK_I ON ADR.SITE_RECEIVED_ELIGIBILITY
(ALLIED_COUNTRY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELG_STD_ELGBTYCODE_FK_I ON ADR.SITE_RECEIVED_ELIGIBILITY
(ELIGIBILITY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELG_STD_OTHRAGNCY_FK_I ON ADR.SITE_RECEIVED_ELIGIBILITY
(FEDERAL_AGENCY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SITE_RECEIVED_ELIG_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."SITE_RECEIVED_ELIGIBILITY" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SITE_RECEIVED_ELIGIBILITY_H
		(
		SITE_RECEIVED_ELIGIBILITY_H_ID,
		SITE_RECEIVED_ELIGIBILITY_ID,
		ELIGIBILITY_EFFECTIVE_DATE,
		ELIGIBILITY_EXPIRATION_DATE,
		ELIGIBILITY_STATUS_DATE,
		STATUS_DATE_TXT,
		PRIMARY_SECONDARY_OR_OTHER_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		STD_INSTITUTION_ID,
		ELIGIBILITY_TYPE_ID,
		RECORD_MODIFIED_COUNT,
		ALLIED_COUNTRY_TYPE_ID,
		FEDERAL_AGENCY_TYPE_ID,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SITE_RECEIVED_ELIGIBILITY_H_S.nextval,
		:old.SITE_RECEIVED_ELIGIBILITY_ID,
		:old.ELIGIBILITY_EFFECTIVE_DATE,
		:old.ELIGIBILITY_EXPIRATION_DATE,
		:old.ELIGIBILITY_STATUS_DATE,
		:old.STATUS_DATE_TXT,
		:old.PRIMARY_SECONDARY_OR_OTHER_IND,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.PERSON_ID,
		:old.STD_INSTITUTION_ID,
		:old.ELIGIBILITY_TYPE_ID,
		:old.RECORD_MODIFIED_COUNT,
		:old.ALLIED_COUNTRY_TYPE_ID,
		:old.FEDERAL_AGENCY_TYPE_ID,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SITE_RECEIVED_ELIGIBILITY_H
		(
		SITE_RECEIVED_ELIGIBILITY_H_ID,
		SITE_RECEIVED_ELIGIBILITY_ID,
		ELIGIBILITY_EFFECTIVE_DATE,
		ELIGIBILITY_EXPIRATION_DATE,
		ELIGIBILITY_STATUS_DATE,
		STATUS_DATE_TXT,
		PRIMARY_SECONDARY_OR_OTHER_IND,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		PERSON_ID,
		STD_INSTITUTION_ID,
		ELIGIBILITY_TYPE_ID,
		RECORD_MODIFIED_COUNT,
		ALLIED_COUNTRY_TYPE_ID,
		FEDERAL_AGENCY_TYPE_ID,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SITE_RECEIVED_ELIGIBILITY_H_S.nextval,
		:new.SITE_RECEIVED_ELIGIBILITY_ID,
		:new.ELIGIBILITY_EFFECTIVE_DATE,
		:new.ELIGIBILITY_EXPIRATION_DATE,
		:new.ELIGIBILITY_STATUS_DATE,
		:new.STATUS_DATE_TXT,
		:new.PRIMARY_SECONDARY_OR_OTHER_IND,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.PERSON_ID,
		:new.STD_INSTITUTION_ID,
		:new.ELIGIBILITY_TYPE_ID,
		:new.RECORD_MODIFIED_COUNT,
		:new.ALLIED_COUNTRY_TYPE_ID,
		:new.FEDERAL_AGENCY_TYPE_ID,
		mDML
		);
	end if;

end SITE_RECEIVED_ELIG_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SITE_RECEIVED_ELIGIBILITY FOR ADR.SITE_RECEIVED_ELIGIBILITY;


ALTER TABLE ADR.SITE_RECEIVED_ELIGIBILITY ADD (
  CONSTRAINT AVCON_1172521913_PRIMA_000
  CHECK (PRIMARY_SECONDARY_OR_OTHER_IND IN ('P', 'S', 'O'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066418
  CHECK ("SITE_RECEIVED_ELIGIBILITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066419
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066420
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066421
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066422
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066423
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066424
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SITERCVELG_PK
  PRIMARY KEY
  (SITE_RECEIVED_ELIGIBILITY_ID)
  USING INDEX ADR.SITERCVELG_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SITE_RECEIVED_ELIGIBILITY ADD (
  CONSTRAINT SITERCVELG_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SITE_RECEIVED_ELIGIBILITY TO ADR_RW;

GRANT SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY TO ES_R;

GRANT INSERT, SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY TO ES_RW;
ALTER TABLE ADR.SITE_RECEIVED_ELIGIBILITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SITE_RECEIVED_ELIGIBILITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SITE_RECEIVED_ELIGIBILITY_H
(
  SITE_RECEIVED_ELIGIBILITY_H_ID  NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  SITE_RECEIVED_ELIGIBILITY_ID    NUMBER(20),
  PERSON_ID                       NUMBER(20),
  STD_INSTITUTION_ID              NUMBER(20),
  ELIGIBILITY_TYPE_ID             NUMBER(20),
  RECORD_MODIFIED_COUNT           NUMBER(10),
  ALLIED_COUNTRY_TYPE_ID          NUMBER(20),
  FEDERAL_AGENCY_TYPE_ID          NUMBER(20),
  ELIGIBILITY_EFFECTIVE_DATE      DATE,
  ELIGIBILITY_EXPIRATION_DATE     DATE,
  ELIGIBILITY_STATUS_DATE         DATE,
  STATUS_DATE_TXT                 VARCHAR2(14 BYTE),
  PRIMARY_SECONDARY_OR_OTHER_IND  VARCHAR2(1 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SITE_RECEIVED_ELIGIBILITY_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.SITE_RECEIVED_ELIGIBILITY_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.SITE_RECEIVED_ELIGIBILITY_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.ELIGIBILITY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.ALLIED_COUNTRY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.FEDERAL_AGENCY_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.STATUS_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.PRIMARY_SECONDARY_OR_OTHER_IND IS 'Primary Eligibility Code field is defined as the field that contains the veterans current eligibility code. The Primary Eligibility Code is the code that is assigned to the veteran based on benefit factors captured on the intake process.  This is the code that is assigned the highest precedence.  It is the primary factor that determines the veterans enrollment priority.   It can be used as a single entity or in conjunction with other eligibility codes in the calculation of the enrollment priority.  The Primary Eligibility code also contributes in determining if the veteran needs to complete a Means Test. Secondary Eligibility Codes is defined as the codes that are assigned to the veteran based on the benefit factors captured on the intake process.  They are considered additional factors and combined with the Primary Eligibility Code determine the Veterans Enrollment Priority.  They also contribute in determining if the veteran needs to complete a Means Test The veteran record will contain all the eligibilities under which this patient may receive care.  The primary eligibility as well as all other eligibilities he/she is entitled to is stored by the system.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ELIGIBILITY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE UNIQUE INDEX ADR.SITERCVELH_PK ON ADR.SITE_RECEIVED_ELIGIBILITY_H
(SITE_RECEIVED_ELIGIBILITY_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERCVELH_PRSNID_NU_I ON ADR.SITE_RECEIVED_ELIGIBILITY_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SITE_RECEIVED_ELIGIBILITY_H FOR ADR.SITE_RECEIVED_ELIGIBILITY_H;


ALTER TABLE ADR.SITE_RECEIVED_ELIGIBILITY_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_058
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067126
  CHECK ("SITE_RECEIVED_ELIGIBILITY_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067127
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067128
  CHECK ("SITE_RECEIVED_ELIGIBILITY_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067129
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067130
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067131
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067132
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067133
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0067134
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SITERCVELH_PK
  PRIMARY KEY
  (SITE_RECEIVED_ELIGIBILITY_H_ID)
  USING INDEX ADR.SITERCVELH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SITE_RECEIVED_ELIGIBILITY_H TO ADR_RW;

GRANT SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SITE_RECEIVED_ELIGIBILITY_H TO ES_RW;
ALTER TABLE ADR.SITE_RECEIVED_ENROLLMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SITE_RECEIVED_ENROLLMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.SITE_RECEIVED_ENROLLMENT
(
  SITE_RECEIVED_ENROLLMENT_ID   NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  STD_ENROLLMENTSTATUS_ID       NUMBER(20),
  ENROLLMENT_DATA_SRC_TYPE_ID   NUMBER(20),
  PRIORITY_GROUP_TYPE_ID        NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID    NUMBER(20),
  STD_INSTITUTION_ID            NUMBER(20),
  ENROLLMENT_START_DATE         DATE,
  ENROLLMENT_END_DATE           DATE,
  ENROLLMENT_CHANGE_EFCTV_DATE  DATE,
  OVERRIDE_IND                  NUMBER(1),
  REMARKS                       VARCHAR2(200 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SITE_RECEIVED_ENROLLMENT IS 'The table stores data about the enrollment of a particular person, when that enrollment was received from a site, as opposed to being processed at the Health Eligibility Center (HEC).';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.SITE_RECEIVED_ENROLLMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.ENROLLMENT_DATA_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.ENROLLMENT_START_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.ENROLLMENT_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.ENROLLMENT_CHANGE_EFCTV_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.OVERRIDE_IND IS 'BR7458 Enrollment Override Flag is defined as a flag that when set will initiate the re- calculation of a veterans enrollment priority.  It is used in those cases where a veteran may have been placed in a Rejected; Stop New Enrollments OR Rejected; Below Enrollment Group Threshold Enrollment statuses. ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SITERECENR_PK ON ADR.SITE_RECEIVED_ENROLLMENT
(SITE_RECEIVED_ENROLLMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERECENR_PRSN_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERECENR_STDINST_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.STRCVDENR_STD_ENRLPGRP_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(PRIORITY_GROUP_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.STRCVDENR_STD_ENRLPSGRP_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(PRIORITY_SUB_GROUP_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.STRCVDENR_STD_ENRLSRC_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(ENROLLMENT_DATA_SRC_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.STREVDENR_STD_ENRLST_FK_I ON ADR.SITE_RECEIVED_ENROLLMENT
(STD_ENROLLMENTSTATUS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SITE_RECEIVED_ENROL_audir_trgr
after Update or Delete or Insert
ON ADR.SITE_RECEIVED_ENROLLMENT
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SITE_RECEIVED_ENROLLMENT_H
		(
		SITE_RECEIVED_ENROLLMENT_H_ID,
		SITE_RECEIVED_ENROLLMENT_ID,
		ENROLLMENT_START_DATE,
		ENROLLMENT_END_DATE,
		ENROLLMENT_CHANGE_EFCTV_DATE,
		OVERRIDE_IND,
		REMARKS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_ENROLLMENTSTATUS_ID,
		PRIORITY_GROUP_TYPE_ID,
		ENROLLMENT_DATA_SRC_TYPE_ID,
		STD_INSTITUTION_ID,
		PRIORITY_SUB_GROUP_TYPE_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SITE_RECEIVED_ENROLLMENT_H_S.nextval,
		:old.SITE_RECEIVED_ENROLLMENT_ID,
		:old.ENROLLMENT_START_DATE,
		:old.ENROLLMENT_END_DATE,
		:old.ENROLLMENT_CHANGE_EFCTV_DATE,
		:old.OVERRIDE_IND,
		:old.REMARKS,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.STD_ENROLLMENTSTATUS_ID,
		:old.PRIORITY_GROUP_TYPE_ID,
		:old.ENROLLMENT_DATA_SRC_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.PRIORITY_SUB_GROUP_TYPE_ID,
		:old.PERSON_ID,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SITE_RECEIVED_ENROLLMENT_H
		(
		SITE_RECEIVED_ENROLLMENT_H_ID,
		SITE_RECEIVED_ENROLLMENT_ID,
		ENROLLMENT_START_DATE,
		ENROLLMENT_END_DATE,
		ENROLLMENT_CHANGE_EFCTV_DATE,
		OVERRIDE_IND,
		REMARKS,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_ENROLLMENTSTATUS_ID,
		PRIORITY_GROUP_TYPE_ID,
		ENROLLMENT_DATA_SRC_TYPE_ID,
		STD_INSTITUTION_ID,
		PRIORITY_SUB_GROUP_TYPE_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SITE_RECEIVED_ENROLLMENT_H_S.nextval,
		:new.SITE_RECEIVED_ENROLLMENT_ID,
		:new.ENROLLMENT_START_DATE,
		:new.ENROLLMENT_END_DATE,
		:new.ENROLLMENT_CHANGE_EFCTV_DATE,
		:new.OVERRIDE_IND,
		:new.REMARKS,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.STD_ENROLLMENTSTATUS_ID,
		:new.PRIORITY_GROUP_TYPE_ID,
		:new.ENROLLMENT_DATA_SRC_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.PRIORITY_SUB_GROUP_TYPE_ID,
		:new.PERSON_ID,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end SITE_RECEIVED_ENROL_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SITE_RECEIVED_ENROLLMENT FOR ADR.SITE_RECEIVED_ENROLLMENT;


ALTER TABLE ADR.SITE_RECEIVED_ENROLLMENT ADD (
  CONSTRAINT SITERECENR_PK
  PRIMARY KEY
  (SITE_RECEIVED_ENROLLMENT_ID)
  USING INDEX ADR.SITERECENR_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SITE_RECEIVED_ENROLLMENT ADD (
  CONSTRAINT SITERECENR_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SITE_RECEIVED_ENROLLMENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SITE_RECEIVED_ENROLLMENT TO ADR_RW;

GRANT SELECT ON ADR.SITE_RECEIVED_ENROLLMENT TO ES_R;

GRANT INSERT, SELECT ON ADR.SITE_RECEIVED_ENROLLMENT TO ES_RW;
ALTER TABLE ADR.SITE_RECEIVED_ENROLLMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SITE_RECEIVED_ENROLLMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SITE_RECEIVED_ENROLLMENT_H
(
  SITE_RECEIVED_ENROLLMENT_H_ID  NUMBER(20)     NOT NULL,
  TRANSACTION_TYPE_CODE          VARCHAR2(1 BYTE) NOT NULL,
  SITE_RECEIVED_ENROLLMENT_ID    NUMBER(20)     NOT NULL,
  PERSON_ID                      NUMBER(20)     NOT NULL,
  STD_ENROLLMENTSTATUS_ID        NUMBER(20),
  ENROLLMENT_DATA_SRC_TYPE_ID    NUMBER(20),
  PRIORITY_GROUP_TYPE_ID         NUMBER(20),
  PRIORITY_SUB_GROUP_TYPE_ID     NUMBER(20),
  STD_INSTITUTION_ID             NUMBER(20),
  ENROLLMENT_START_DATE          DATE,
  ENROLLMENT_END_DATE            DATE,
  ENROLLMENT_CHANGE_EFCTV_DATE   DATE,
  OVERRIDE_IND                   NUMBER(1),
  REMARKS                        VARCHAR2(200 BYTE),
  RECORD_CREATED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE            DATE           NOT NULL,
  RECORD_MODIFIED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE           DATE           NOT NULL,
  RECORD_MODIFIED_COUNT          NUMBER(10)     NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SITE_RECEIVED_ENROLLMENT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.SITE_RECEIVED_ENROLLMENT_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.SITE_RECEIVED_ENROLLMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.ENROLLMENT_DATA_SRC_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.PRIORITY_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.PRIORITY_SUB_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.ENROLLMENT_START_DATE IS 'BR2240 Enrollment Date is defined as he starting date for this enrollment.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.ENROLLMENT_END_DATE IS 'BR7771 The enrollment end date is defined as the end date of this persons enrollment.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.OVERRIDE_IND IS 'When an actor manually Remails or Mails a letter and the conditions to send the letter as identified in the Letter Decision Tables and the Supplementary Specifications are not met the system displays this message to the actor       The letter has failed to meet the conditions to send the letter.  The condition that failed is insert Letter Criteria not met OR the HEC Reject Reason.  Do you want to override this condition and send the letter? ';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SITE_RECEIVED_ENROLLMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SITERECENH_PK ON ADR.SITE_RECEIVED_ENROLLMENT_H
(SITE_RECEIVED_ENROLLMENT_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SITERECENH_PRSNID_NU_I ON ADR.SITE_RECEIVED_ENROLLMENT_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SITE_RECEIVED_ENROLLMENT_H FOR ADR.SITE_RECEIVED_ENROLLMENT_H;


ALTER TABLE ADR.SITE_RECEIVED_ENROLLMENT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_052
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT SITERECENH_PK
  PRIMARY KEY
  (SITE_RECEIVED_ENROLLMENT_H_ID)
  USING INDEX ADR.SITERECENH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SITE_RECEIVED_ENROLLMENT_H TO ADR_R;

GRANT SELECT ON ADR.SITE_RECEIVED_ENROLLMENT_H TO ADR_RW;

GRANT SELECT ON ADR.SITE_RECEIVED_ENROLLMENT_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SITE_RECEIVED_ENROLLMENT_H TO ES_RW;
ALTER TABLE ADR.SPECIAL_FACTOR
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SPECIAL_FACTOR CASCADE CONSTRAINTS;

CREATE TABLE ADR.SPECIAL_FACTOR
(
  SPECIAL_FACTOR_ID               NUMBER(20),
  PERSON_ID                       NUMBER(20),
  AGENT_ORANGE_LOCATION_TYPE_ID   NUMBER(20),
  RADIATION_EXP_METHOD_TYPE_ID    NUMBER(20),
  SPECIAL_FACTOR_STDYESNO_ID      NUMBER(20),
  SPCL_FACTOR_MAPPING_TYPE_CODE   VARCHAR2(5 BYTE),
  SPECIAL_FACTOR_REGISTER_NUMBER  NUMBER(6),
  SPECIAL_FACTOR_REG_DATE_TXT     VARCHAR2(14 BYTE),
  SPECIAL_FACTOR_EXAM_DATE_TXT    VARCHAR2(14 BYTE),
  REMARKS                         VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  CL_VERIFICATION_METHOD_CODE     NUMBER(20),
  CL_CHANGE_SITE                  NUMBER(20),
  CL_COMMENT                      VARCHAR2(255 BYTE),
  CL_SOURCE_OF_CHANGE             NUMBER(20)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SPECIAL_FACTOR IS 'The table contains data about what the VHA considers to be special factors for eligibility: Radiation Exposure SHAD Agent Orange Exposure Environmental Contamination Exposure ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.AGENT_ORANGE_LOCATION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RADIATION_EXP_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.SPCL_FACTOR_MAPPING_TYPE_CODE IS 'BR607 Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_REGISTER_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_REG_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.SPECIAL_FACTOR_EXAM_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.CL_VERIFICATION_METHOD_CODE IS 'FK to ADRREF.STD_CLVERIFICATIONTYPE.ID';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.CL_CHANGE_SITE IS 'The site that made changes to the Camp LeJeune Indicator.  These values come from the SDSADM.STD_INSTITUTION table.  If source of change is VAMC or HEC then this field will contain the institution value.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.CL_COMMENT IS 'User entered comments';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR.CL_SOURCE_OF_CHANGE IS 'Contains the value from the ID column of the ADRREF.STD_CLSOURCEOFCHANGE table.  This represent the origin of the Camp LeJeune eligibility indicator';


CREATE INDEX ADR.SPCLFCTR_STD_AGNTORNG_FK_I ON ADR.SPECIAL_FACTOR
(AGENT_ORANGE_LOCATION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SPCLFCTR_STD_RADEXPMTHD_FK_I ON ADR.SPECIAL_FACTOR
(RADIATION_EXP_METHOD_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.SPECFACT_PK ON ADR.SPECIAL_FACTOR
(SPECIAL_FACTOR_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SPECFACT_PRSN_FK_I ON ADR.SPECIAL_FACTOR
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SPECFACT_STDYESNO_FK_I ON ADR.SPECIAL_FACTOR
(SPECIAL_FACTOR_STDYESNO_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SP_STD_SPECIALFACTORMAP_FK_I ON ADR.SPECIAL_FACTOR
(SPCL_FACTOR_MAPPING_TYPE_CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SPECIAL_FACTOR_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."SPECIAL_FACTOR" FOR EACH ROW
declare
    mDML char(1);
begin

  if DELETING then
    insert into SPECIAL_FACTOR_H
    (
    SPECIAL_FACTOR_H_ID,
    SPECIAL_FACTOR_ID,
    SPCL_FACTOR_MAPPING_TYPE_CODE,
    SPECIAL_FACTOR_REGISTER_NUMBER,
    SPECIAL_FACTOR_REG_DATE_TXT,
    SPECIAL_FACTOR_EXAM_DATE_TXT,
    REMARKS,
    SPECIAL_FACTOR_STDYESNO_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RADIATION_EXP_METHOD_TYPE_ID,
    PERSON_ID,
    AGENT_ORANGE_LOCATION_TYPE_ID,
    RECORD_MODIFIED_COUNT,
    CL_VERIFICATION_METHOD_CODE,
    CL_CHANGE_SITE,
    CL_COMMENT,
    CL_SOURCE_OF_CHANGE,
    HISTORY_CREATED_DATE,
    TRANSACTION_TYPE_CODE

    )
    values
    (
    SPECIAL_FACTOR_H_S.nextval,
    :old.SPECIAL_FACTOR_ID,
    :old.SPCL_FACTOR_MAPPING_TYPE_CODE,
    :old.SPECIAL_FACTOR_REGISTER_NUMBER,
    :old.SPECIAL_FACTOR_REG_DATE_TXT,
    :old.SPECIAL_FACTOR_EXAM_DATE_TXT,
    :old.REMARKS,
    :old.SPECIAL_FACTOR_STDYESNO_ID,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY,
    :old.RECORD_MODIFIED_DATE,
    :old.RADIATION_EXP_METHOD_TYPE_ID,
    :old.PERSON_ID,
    :old.AGENT_ORANGE_LOCATION_TYPE_ID,
    :old.RECORD_MODIFIED_COUNT,
    :old.CL_VERIFICATION_METHOD_CODE,
    :old.CL_CHANGE_SITE,
    :old.CL_COMMENT,
    :old.CL_SOURCE_OF_CHANGE,
    SYSDATE,
    'D'
    );

  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

    insert into SPECIAL_FACTOR_H
    (
    SPECIAL_FACTOR_H_ID,
    SPECIAL_FACTOR_ID,
    SPCL_FACTOR_MAPPING_TYPE_CODE,
    SPECIAL_FACTOR_REGISTER_NUMBER,
    SPECIAL_FACTOR_REG_DATE_TXT,
    SPECIAL_FACTOR_EXAM_DATE_TXT,
    REMARKS,
    SPECIAL_FACTOR_STDYESNO_ID,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RADIATION_EXP_METHOD_TYPE_ID,
    PERSON_ID,
    AGENT_ORANGE_LOCATION_TYPE_ID,
    RECORD_MODIFIED_COUNT,
    CL_VERIFICATION_METHOD_CODE,
    CL_CHANGE_SITE,
    CL_COMMENT,
    CL_SOURCE_OF_CHANGE,
    HISTORY_CREATED_DATE,
    TRANSACTION_TYPE_CODE
    )
    values
    (
    SPECIAL_FACTOR_H_S.nextval,
    :new.SPECIAL_FACTOR_ID,
    :new.SPCL_FACTOR_MAPPING_TYPE_CODE,
    :new.SPECIAL_FACTOR_REGISTER_NUMBER,
    :new.SPECIAL_FACTOR_REG_DATE_TXT,
    :new.SPECIAL_FACTOR_EXAM_DATE_TXT,
    :new.REMARKS,
    :new.SPECIAL_FACTOR_STDYESNO_ID,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY,
    :new.RECORD_MODIFIED_DATE,
    :new.RADIATION_EXP_METHOD_TYPE_ID,
    :new.PERSON_ID,
    :new.AGENT_ORANGE_LOCATION_TYPE_ID,
    :new.RECORD_MODIFIED_COUNT,
    :new.CL_VERIFICATION_METHOD_CODE,
    :new.CL_CHANGE_SITE,
    :new.CL_COMMENT,
    :new.CL_SOURCE_OF_CHANGE,
    SYSDATE,
    mDML
    );

  end if;

end SPECIAL_FACTOR_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SPECIAL_FACTOR FOR ADR.SPECIAL_FACTOR;


ALTER TABLE ADR.SPECIAL_FACTOR ADD (
  CONSTRAINT SYS_C0066593
  CHECK ("SPECIAL_FACTOR_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066594
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066595
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066596
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066597
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066598
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066599
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SPECFACT_PK
  PRIMARY KEY
  (SPECIAL_FACTOR_ID)
  USING INDEX ADR.SPECFACT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SPECIAL_FACTOR ADD (
  CONSTRAINT SPECFACT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT SP_FAC_STD_CLSRCOFCHNG_FK 
  FOREIGN KEY (CL_SOURCE_OF_CHANGE) 
  REFERENCES ADRREF.STD_CLSOURCEOFCHANGE (ID)
  ENABLE VALIDATE,
  CONSTRAINT SP_FAC_STD_CLVERIFTYP_FK 
  FOREIGN KEY (CL_VERIFICATION_METHOD_CODE) 
  REFERENCES ADRREF.STD_CLVERIFICATIONTYPE (ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SPECIAL_FACTOR TO ADREXT;

GRANT SELECT ON ADR.SPECIAL_FACTOR TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SPECIAL_FACTOR TO ADR_RW;

GRANT SELECT ON ADR.SPECIAL_FACTOR TO ES_R;

GRANT INSERT, SELECT ON ADR.SPECIAL_FACTOR TO ES_RW;
ALTER TABLE ADR.SPECIAL_FACTOR_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SPECIAL_FACTOR_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SPECIAL_FACTOR_H
(
  SPECIAL_FACTOR_H_ID             NUMBER(20),
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE),
  SPECIAL_FACTOR_ID               NUMBER(20),
  PERSON_ID                       NUMBER(20),
  RADIATION_EXP_METHOD_TYPE_ID    NUMBER(20),
  AGENT_ORANGE_LOCATION_TYPE_ID   NUMBER(20),
  SPCL_FACTOR_MAPPING_TYPE_CODE   VARCHAR2(5 BYTE),
  SPECIAL_FACTOR_STDYESNO_ID      NUMBER(20),
  SPECIAL_FACTOR_REGISTER_NUMBER  NUMBER(6),
  SPECIAL_FACTOR_REG_DATE_TXT     VARCHAR2(14 BYTE),
  SPECIAL_FACTOR_EXAM_DATE_TXT    VARCHAR2(14 BYTE),
  REMARKS                         VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY               VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE             DATE,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE            DATE,
  RECORD_MODIFIED_COUNT           NUMBER(10),
  CL_VERIFICATION_METHOD_CODE     NUMBER(20),
  CL_CHANGE_SITE                  NUMBER(20),
  CL_COMMENT                      VARCHAR2(255 BYTE),
  CL_SOURCE_OF_CHANGE             NUMBER(20),
  HISTORY_CREATED_DATE            DATE
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SPECIAL_FACTOR_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPECIAL_FACTOR_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPECIAL_FACTOR_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RADIATION_EXP_METHOD_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.AGENT_ORANGE_LOCATION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPCL_FACTOR_MAPPING_TYPE_CODE IS 'BR607 Income Year is defined as the Income Year that this financial information pertains to.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPECIAL_FACTOR_REGISTER_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPECIAL_FACTOR_REG_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.SPECIAL_FACTOR_EXAM_DATE_TXT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.REMARKS IS 'Text including any additional information for the action taken for the particular mailing record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.CL_VERIFICATION_METHOD_CODE IS 'FK to ADRREF.STD_CLVERIFICATIONTYPE.ID';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.CL_CHANGE_SITE IS 'The site that made changes to the Camp LeJeune Indicator.  These values come from the SDSADM.STD_INSTITUTION table.  If source of change is VAMC or HEC then this field will contain the institution value.';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.CL_COMMENT IS 'User entered comments';

COMMENT ON COLUMN ADR.SPECIAL_FACTOR_H.CL_SOURCE_OF_CHANGE IS 'Contains the value from the ID column of the ADRREF.STD_CLSOURCEOFCHANGE table.  This represent the origin of the Camp LeJeune eligibility indicator';


CREATE UNIQUE INDEX ADR.SPECFACTH_PK ON ADR.SPECIAL_FACTOR_H
(SPECIAL_FACTOR_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SPECFACTH_PRSNID_NU_I ON ADR.SPECIAL_FACTOR_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SPECIAL_FACTOR_H FOR ADR.SPECIAL_FACTOR_H;


ALTER TABLE ADR.SPECIAL_FACTOR_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_019
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066275
  CHECK ("SPECIAL_FACTOR_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066276
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066277
  CHECK ("SPECIAL_FACTOR_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066278
  CHECK ("PERSON_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066279
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066280
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066281
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066282
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066283
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SPECFACTH_PK
  PRIMARY KEY
  (SPECIAL_FACTOR_H_ID)
  USING INDEX ADR.SPECFACTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SPECIAL_FACTOR_H TO ADR_R;

GRANT SELECT ON ADR.SPECIAL_FACTOR_H TO ADR_RW;

GRANT SELECT ON ADR.SPECIAL_FACTOR_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SPECIAL_FACTOR_H TO ES_RW;
ALTER TABLE ADR.SSN
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SSN CASCADE CONSTRAINTS;

CREATE TABLE ADR.SSN
(
  SSN_ID                     NUMBER(20),
  PERSON_ID                  NUMBER(20),
  PERSON_RELATION_ID         NUMBER(20),
  STD_SSNTYPE_ID             NUMBER(20),
  PSEUDO_SSN_REASON_TYPE_ID  NUMBER(20),
  SSN_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  SSN_VRFN_STATUS_TYPE_ID    NUMBER(20),
  SSA_MESSAGE_TYPE_ID        NUMBER(20),
  SSN                        VARCHAR2(20 BYTE),
  SSA_VERIFIED_DATE          DATE,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE       DATE,
  RECORD_MODIFIED_COUNT      NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   11
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SSN IS 'The table supports the SSN Verification process for Spouse and Dependent records only. The un-verified SSN s are sent to the Social Security Administration via a batch process. The return batch is processed and the record status is updated according to the SSA message returned for each SSN. OWNER: This is an E&E Table ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.SSN.SSN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN.PSEUDO_SSN_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN.SSN_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN.SSN_VRFN_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN.SSA_MESSAGE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.SSN.SSA_VERIFIED_DATE IS 'BR1590 SSA Verification date is defined as the date the SSA SSN tape was read.';

COMMENT ON COLUMN ADR.SSN.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SSN.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SSN.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SSN.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SSN.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SSN_PK ON ADR.SSN
(SSN_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_PRSNREL_FK_I ON ADR.SSN
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_PRSN_FK_I ON ADR.SSN
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_SSNTYPE_FK_I ON ADR.SSN
(STD_SSNTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_STD_PSEUDOSSNRSN_FK_I ON ADR.SSN
(PSEUDO_SSN_REASON_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_STD_SSAVRFNCD_FK_I ON ADR.SSN
(SSA_MESSAGE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_STD_SSNCHGSRC_FK_I ON ADR.SSN
(SSN_CHANGE_SOURCE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSN_STD_SSNVRFNSTAT_FK_I ON ADR.SSN
(SSN_VRFN_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.SSN_AUDIR_TRGR AFTER UPDATE OR INSERT OR DELETE ON "ADR"."SSN" FOR EACH ROW
declare
   	mDML char(1);
begin

	if DELETING then
		insert into SSN_H
		(
		SSN_H_ID,
		SSN_ID,
		PERSON_ID,
		PERSON_RELATION_ID,
		STD_SSNTYPE_ID,
		PSEUDO_SSN_REASON_TYPE_ID,
		SSN_CHANGE_SOURCE_TYPE_ID,
		SSN_VRFN_STATUS_TYPE_ID,
		SSA_MESSAGE_TYPE_ID,
		SSN,
		SSA_VERIFIED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SSN_H_S.nextval,
		:old.SSN_ID,
		:old.PERSON_ID,
		:old.PERSON_RELATION_ID,
		:old.STD_SSNTYPE_ID,
		:old.PSEUDO_SSN_REASON_TYPE_ID,
		:old.SSN_CHANGE_SOURCE_TYPE_ID,
		:old.SSN_VRFN_STATUS_TYPE_ID,
		:old.SSA_MESSAGE_TYPE_ID,
		:old.SSN,
		:old.SSA_VERIFIED_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into SSN_H
		(
		SSN_H_ID,
		SSN_ID,
		PERSON_ID,
		PERSON_RELATION_ID,
		STD_SSNTYPE_ID,
		PSEUDO_SSN_REASON_TYPE_ID,
		SSN_CHANGE_SOURCE_TYPE_ID,
		SSN_VRFN_STATUS_TYPE_ID,
		SSA_MESSAGE_TYPE_ID,
		SSN,
		SSA_VERIFIED_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		SSN_H_S.nextval,
		:new.SSN_ID,
		:new.PERSON_ID,
		:new.PERSON_RELATION_ID,
		:new.STD_SSNTYPE_ID,
		:new.PSEUDO_SSN_REASON_TYPE_ID,
		:new.SSN_CHANGE_SOURCE_TYPE_ID,
		:new.SSN_VRFN_STATUS_TYPE_ID,
		:new.SSA_MESSAGE_TYPE_ID,
		:new.SSN,
		:new.SSA_VERIFIED_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end SSN_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM SSN FOR ADR.SSN;


ALTER TABLE ADR.SSN ADD (
  CONSTRAINT SYS_C0066930
  CHECK ("SSN_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066931
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066932
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066933
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066934
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066935
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SSN_PK
  PRIMARY KEY
  (SSN_ID)
  USING INDEX ADR.SSN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SSN ADD (
  CONSTRAINT SSN_PRSNREL_FK 
  FOREIGN KEY (PERSON_RELATION_ID) 
  REFERENCES ADR.PERSON_RELATION (PERSON_RELATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT SSN_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.SSN TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SSN TO ADR_RW;

GRANT SELECT ON ADR.SSN TO ES_R;

GRANT INSERT, SELECT ON ADR.SSN TO ES_RW;

GRANT SELECT ON ADR.SSN TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.SSN TO PSIM_ADR_RW;
ALTER TABLE ADR.SSN_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SSN_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.SSN_H
(
  SSN_H_ID                   NUMBER(20),
  TRANSACTION_TYPE_CODE      VARCHAR2(1 BYTE),
  SSN_ID                     NUMBER(20),
  PERSON_ID                  NUMBER(20),
  PERSON_RELATION_ID         NUMBER(20),
  STD_SSNTYPE_ID             NUMBER(20),
  PSEUDO_SSN_REASON_TYPE_ID  NUMBER(20),
  SSN_CHANGE_SOURCE_TYPE_ID  NUMBER(20),
  SSN_VRFN_STATUS_TYPE_ID    NUMBER(20),
  SSA_MESSAGE_TYPE_ID        NUMBER(20),
  SSN                        VARCHAR2(20 BYTE),
  SSA_VERIFIED_DATE          DATE,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE        DATE,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE       DATE,
  RECORD_MODIFIED_COUNT      NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.SSN_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.SSN_H.SSN_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.SSN_H.SSN_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN_H.PERSON_RELATION_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.PSEUDO_SSN_REASON_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.SSN_CHANGE_SOURCE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.SSN_VRFN_STATUS_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.SSA_MESSAGE_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_H.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.SSN_H.SSA_VERIFIED_DATE IS 'BR1590 SSA Verification date is defined as the date the SSA SSN tape was read.';

COMMENT ON COLUMN ADR.SSN_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SSN_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SSN_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SSN_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SSN_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SSNH_PK ON ADR.SSN_H
(SSN_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNH_PRSNID_NU_I ON ADR.SSN_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNH_PRSNRELID_NU_I ON ADR.SSN_H
(PERSON_RELATION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SSN_H FOR ADR.SSN_H;


ALTER TABLE ADR.SSN_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_016
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066233
  CHECK ("SSN_H_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066234
  CHECK ("TRANSACTION_TYPE_CODE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066235
  CHECK ("SSN_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066236
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066237
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066238
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066239
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0066240
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SSNH_PK
  PRIMARY KEY
  (SSN_H_ID)
  USING INDEX ADR.SSNH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SSN_H TO ADR_R;

GRANT SELECT ON ADR.SSN_H TO ADR_RW;

GRANT SELECT ON ADR.SSN_H TO ES_R;

GRANT INSERT, SELECT ON ADR.SSN_H TO ES_RW;

GRANT SELECT ON ADR.SSN_H TO PSIM_ADR_R;
ALTER TABLE ADR.SSN_VERIFICATION
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SSN_VERIFICATION CASCADE CONSTRAINTS;

CREATE TABLE ADR.SSN_VERIFICATION
(
  SSN_VERIFICATION_ID    NUMBER(20)             NOT NULL,
  SSA_SENT_DATE          DATE,
  SSN_COUNT              NUMBER(10),
  SSN_SENT_COUNT         NUMBER(10),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.SSN_VERIFICATION.SSN_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.SSN_SENT_COUNT IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SSNVERIFY_PK ON ADR.SSN_VERIFICATION
(SSN_VERIFICATION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SSN_VERIFICATION FOR ADR.SSN_VERIFICATION;


ALTER TABLE ADR.SSN_VERIFICATION ADD (
  CONSTRAINT SSNVERIFY_PK
  PRIMARY KEY
  (SSN_VERIFICATION_ID)
  USING INDEX ADR.SSNVERIFY_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SSN_VERIFICATION TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SSN_VERIFICATION TO ADR_RW;

GRANT SELECT ON ADR.SSN_VERIFICATION TO ES_R;

GRANT INSERT, SELECT ON ADR.SSN_VERIFICATION TO ES_RW;
ALTER TABLE ADR.SSN_VERIFICATION_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.SSN_VERIFICATION_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.SSN_VERIFICATION_DETAIL
(
  SSN_VERIFICATION_DETAIL_ID   NUMBER(20),
  SSN_VERIFICATION_ID          NUMBER(20),
  PERSON_ID                    NUMBER(20),
  SSN_ID                       NUMBER(20),
  SSN                          VARCHAR2(20 BYTE),
  VERIFICATION_REQUEST_NUMBER  VARCHAR2(20 BYTE),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE),
  RECORD_CREATED_DATE          DATE,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE),
  RECORD_MODIFIED_DATE         DATE,
  RECORD_MODIFIED_COUNT        NUMBER(10)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
COMPRESS FOR OLTP 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.SSN_VERIFICATION_DETAIL_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.SSN_VERIFICATION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.SSN_VERIFICATION_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.SSNVERFYDT_PK ON ADR.SSN_VERIFICATION_DETAIL
(SSN_VERIFICATION_DETAIL_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNVERFYDT_PRSN_FK_I ON ADR.SSN_VERIFICATION_DETAIL
(PERSON_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNVERFYDT_SSNVERIFY_FK_I ON ADR.SSN_VERIFICATION_DETAIL
(SSN_VERIFICATION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNVERFYDT_SSN_FK_I ON ADR.SSN_VERIFICATION_DETAIL
(SSN_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.SSNVERFYDT_VERFRQNO_NU_I ON ADR.SSN_VERIFICATION_DETAIL
(VERIFICATION_REQUEST_NUMBER)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM SSN_VERIFICATION_DETAIL FOR ADR.SSN_VERIFICATION_DETAIL;


ALTER TABLE ADR.SSN_VERIFICATION_DETAIL ADD (
  CONSTRAINT SYS_C0065899
  CHECK ("SSN_VERIFICATION_DETAIL_ID" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065900
  CHECK ("RECORD_CREATED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065901
  CHECK ("RECORD_CREATED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065902
  CHECK ("RECORD_MODIFIED_BY" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065903
  CHECK ("RECORD_MODIFIED_DATE" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SYS_C0065904
  CHECK ("RECORD_MODIFIED_COUNT" IS NOT NULL)
  ENABLE NOVALIDATE,
  CONSTRAINT SSNVERFYDT_PK
  PRIMARY KEY
  (SSN_VERIFICATION_DETAIL_ID)
  USING INDEX ADR.SSNVERFYDT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.SSN_VERIFICATION_DETAIL ADD (
  CONSTRAINT SSNVERFYDT_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT SSNVERFYDT_SSNVERIFY_FK 
  FOREIGN KEY (SSN_VERIFICATION_ID) 
  REFERENCES ADR.SSN_VERIFICATION (SSN_VERIFICATION_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.SSN_VERIFICATION_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.SSN_VERIFICATION_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.SSN_VERIFICATION_DETAIL TO ES_R;

GRANT INSERT, SELECT ON ADR.SSN_VERIFICATION_DETAIL TO ES_RW;
DROP TABLE ADR.SSN_VERIFICATION_EXT CASCADE CONSTRAINTS;

CREATE TABLE ADR.SSN_VERIFICATION_EXT
(
  SSN              VARCHAR2(9 BYTE),
  ENTRY_CODE       VARCHAR2(3 BYTE),
  PROCESSING_CODE  VARCHAR2(3 BYTE),
  LAST_NAME        VARCHAR2(13 BYTE),
  FIRST_NAME       VARCHAR2(10 BYTE),
  MIDDLE_NAME      VARCHAR2(7 BYTE),
  DOB              VARCHAR2(8 BYTE),
  SEX              VARCHAR2(1 BYTE),
  INCOME_YEAR      VARCHAR2(4 BYTE),
  HEC_VA_ID        VARCHAR2(15 BYTE),
  SPOUSE_CODE      VARCHAR2(1 BYTE),
  BLANK_1          VARCHAR2(29 BYTE),
  BLANK_2          VARCHAR2(20 BYTE),
  REQUEST_CODE     VARCHAR2(4 BYTE),
  BLANK_3          VARCHAR2(3 BYTE)
)
ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY BATCH_SSN_ADRTOPROCESS_DIR
     ACCESS PARAMETERS 
       ( RECORDS DELIMITED BY NEWLINE 
BADFILE BATCH_SSN_ERR_DIR:'SSA.r_%a_%p.err' 
LOGFILE BATCH_SSN_LOG_DIR:'SSA.r_%a_%p.log' 
FIELDS (
SSN          POSITION(1:9) CHAR(9),
ENTRY_CODE   POSITION(10:12) CHAR(3),
PROCESSING_CODE  POSITION(13:15) CHAR(3),
LAST_NAME    POSITION(16:28) CHAR(13),
FIRST_NAME   POSITION(29:38) CHAR(10),
MIDDLE_NAME  POSITION(39:45) CHAR(7),
DOB          POSITION(46:53) CHAR(8),
SEX          POSITION(54:54) CHAR(1),
INCOME_YEAR  POSITION(55:58) CHAR(4),
HEC_VA_ID    POSITION(59:73) CHAR(15),
SPOUSE_CODE  POSITION(74:74) CHAR(1),
BLANK_1      POSITION(75:103) CHAR(29),
BLANK_2      POSITION(104:123) CHAR(20),
REQUEST_CODE POSITION(124:127) CHAR(4),
BLANK_3      POSITION(128:130) CHAR(3)
)  )
     LOCATION (BATCH_SSN_ADRTOPROCESS_DIR:'SSA.r122491.txt')
  )
REJECT LIMIT UNLIMITED
PARALLEL ( DEGREE 4 INSTANCES 1 )
NOMONITORING;


CREATE OR REPLACE PUBLIC SYNONYM SSN_VERIFICATION_EXT FOR ADR.SSN_VERIFICATION_EXT;


GRANT SELECT ON ADR.SSN_VERIFICATION_EXT TO ADR_R;

GRANT SELECT ON ADR.SSN_VERIFICATION_EXT TO ES_R;
DROP TABLE ADR.STG_ADDRESS_SSN_CLEANUP CASCADE CONSTRAINTS;

CREATE GLOBAL TEMPORARY TABLE ADR.STG_ADDRESS_SSN_CLEANUP
(
  ADDRESS_ID     NUMBER(20),
  ADDRESS_LINE1  VARCHAR2(100 BYTE),
  ADDRESS_LINE2  VARCHAR2(100 BYTE),
  ADDRESS_LINE3  VARCHAR2(100 BYTE),
  CITY           VARCHAR2(60 BYTE),
  SSN            VARCHAR2(20 BYTE)
)
ON COMMIT PRESERVE ROWS
RESULT_CACHE (MODE DEFAULT)
NOCACHE;

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.ADDRESS_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.ADDRESS_LINE1 IS 'BR3 Address line #1 is defined as the free text form of the number and street or post office box of a mailing address.';

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.ADDRESS_LINE2 IS 'BR5 Address line #2 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.ADDRESS_LINE3 IS 'BR7 Address line #3 is defined as the free text supplemental to the number and street of a mailing address.';

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.CITY IS 'Retrofitted.';

COMMENT ON COLUMN ADR.STG_ADDRESS_SSN_CLEANUP.SSN IS 'The verified social security number that has been assigned by the Social Security Agency to the person.';


CREATE OR REPLACE PUBLIC SYNONYM STG_ADDRESS_SSN_CLEANUP FOR ADR.STG_ADDRESS_SSN_CLEANUP;


GRANT SELECT ON ADR.STG_ADDRESS_SSN_CLEANUP TO ADR_R;
DROP TABLE ADR.STG_MEC_PERIOD CASCADE CONSTRAINTS;

CREATE GLOBAL TEMPORARY TABLE ADR.STG_MEC_PERIOD
(
  MEC_PERIOD_ID         NUMBER,
  PERSON_ID             NUMBER(20),
  DERVD_MEC_START_DATE  DATE,
  DERVD_MEC_END_DATE    DATE
)
ON COMMIT PRESERVE ROWS
RESULT_CACHE (MODE DEFAULT)
NOCACHE;


CREATE OR REPLACE PUBLIC SYNONYM STG_MEC_PERIOD FOR ADR.STG_MEC_PERIOD;
DROP TABLE ADR.STG_MEC_PERIOD_ENRMNT_DTRM_H CASCADE CONSTRAINTS;

CREATE GLOBAL TEMPORARY TABLE ADR.STG_MEC_PERIOD_ENRMNT_DTRM_H
(
  ENROLLMENT_DETERMINATION_H_ID  NUMBER,
  PERSON_ID                      NUMBER(20),
  DERVD_MEC_START_DATE           DATE,
  DERVD_MEC_END_DATE             DATE,
  CURRENT_RANK                   NUMBER
)
ON COMMIT DELETE ROWS
RESULT_CACHE (MODE DEFAULT)
NOCACHE;


CREATE OR REPLACE PUBLIC SYNONYM STG_MEC_PERIOD_ENRMNT_DTRM_H FOR ADR.STG_MEC_PERIOD_ENRMNT_DTRM_H;
DROP TABLE ADR.STG_MEC_PERIOD_PERSON_ID CASCADE CONSTRAINTS;

CREATE GLOBAL TEMPORARY TABLE ADR.STG_MEC_PERIOD_PERSON_ID
(
  PERSON_ID  NUMBER(20)
)
ON COMMIT PRESERVE ROWS
RESULT_CACHE (MODE DEFAULT)
NOCACHE;


CREATE OR REPLACE PUBLIC SYNONYM STG_MEC_PERIOD_PERSON_ID FOR ADR.STG_MEC_PERIOD_PERSON_ID;
ALTER TABLE ADR.STOP_COM_RSN_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.STOP_COM_RSN_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.STOP_COM_RSN_TYPE
(
  STOP_COM_RSN_TYPE_ID   NUMBER(20)             NOT NULL,
  CODE                   VARCHAR2(1 BYTE)       NOT NULL,
  NAME                   VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(100 BYTE),
  ISACTIVE               CHAR(1 BYTE)           NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX ADR.STOPCOMRSNTYPE_CD_UK ON ADR.STOP_COM_RSN_TYPE
(CODE)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.STOPCOMRSNTYPE_NM_UK ON ADR.STOP_COM_RSN_TYPE
(NAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.STOP_COM_RSN_TYPE_PK ON ADR.STOP_COM_RSN_TYPE
(STOP_COM_RSN_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM STOP_COM_RSN_TYPE FOR ADR.STOP_COM_RSN_TYPE;


ALTER TABLE ADR.STOP_COM_RSN_TYPE ADD (
  CONSTRAINT STOP_COM_RSN_TYPE_PK
  PRIMARY KEY
  (STOP_COM_RSN_TYPE_ID)
  USING INDEX ADR.STOP_COM_RSN_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.STOP_COM_RSN_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.STOP_COM_RSN_TYPE TO ADR_RW;
ALTER TABLE ADR.TRANSMISSION_STATUS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.TRANSMISSION_STATUS CASCADE CONSTRAINTS;

CREATE TABLE ADR.TRANSMISSION_STATUS
(
  TRANSMISSION_STATUS_ID  NUMBER(20)            NOT NULL,
  CODE                    VARCHAR2(10 BYTE)     NOT NULL,
  NAME                    VARCHAR2(30 BYTE)     NOT NULL,
  DESCRIPTION             VARCHAR2(250 BYTE)    NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.TRANSMISSION_STATUS IS 'This table contains reference lookup values to track the state of IRS submissions';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.TRANSMISSION_STATUS_ID IS 'Primary Key';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.CODE IS 'Lookup code short name value';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.NAME IS 'Lookup code long name value';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.DESCRIPTION IS 'Lookup code description';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.TRANSMISSION_STATUS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';


CREATE UNIQUE INDEX ADR.TRANSMISSION_STATUS_ID_PK ON ADR.TRANSMISSION_STATUS
(TRANSMISSION_STATUS_ID)
LOGGING
TABLESPACE M_DATA_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.TRNS_STAT_CODE_NU_I ON ADR.TRANSMISSION_STATUS
(CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.TRNS_STAT_NAME_NU_I ON ADR.TRANSMISSION_STATUS
(NAME)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM TRANSMISSION_STATUS FOR ADR.TRANSMISSION_STATUS;


ALTER TABLE ADR.TRANSMISSION_STATUS ADD (
  CONSTRAINT TRANSMISSION_STATUS_ID_PK
  PRIMARY KEY
  (TRANSMISSION_STATUS_ID)
  USING INDEX ADR.TRANSMISSION_STATUS_ID_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.TRANSMISSION_STATUS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.TRANSMISSION_STATUS TO ADR_RW;
ALTER TABLE ADR.TRANSMISSION_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.TRANSMISSION_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.TRANSMISSION_TYPE
(
  TRANSMISSION_TYPE_ID   NUMBER(20)             NOT NULL,
  CODE                   VARCHAR2(2 BYTE)       NOT NULL,
  NAME                   VARCHAR2(50 BYTE)      NOT NULL,
  DESCRIPTION            VARCHAR2(100 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(18)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.TRANSMISSION_TYPE_ID IS 'The primary key, generated by the database system in numeric form. Used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.RECORD_CREATED_BY IS 'Account or user ID that created the record';

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.RECORD_MODIFIED_BY IS 'Account or user ID for this modification to the record';

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.TRANSMISSION_TYPE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.CODE_UK ON ADR.TRANSMISSION_TYPE
(CODE)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.NAME_UK ON ADR.TRANSMISSION_TYPE
(NAME)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.TRNTYP_PK ON ADR.TRANSMISSION_TYPE
(TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM TRANSMISSION_TYPE FOR ADR.TRANSMISSION_TYPE;


ALTER TABLE ADR.TRANSMISSION_TYPE ADD (
  CONSTRAINT TRNTYP_PK
  PRIMARY KEY
  (TRANSMISSION_TYPE_ID)
  USING INDEX ADR.TRNTYP_PK
  ENABLE VALIDATE,
  CONSTRAINT CODE_UK
  UNIQUE (CODE)
  USING INDEX ADR.CODE_UK
  ENABLE VALIDATE,
  CONSTRAINT NAME_UK
  UNIQUE (NAME)
  USING INDEX ADR.NAME_UK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.TRANSMISSION_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.TRANSMISSION_TYPE TO ADR_RW;
ALTER TABLE ADR.USERS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USERS CASCADE CONSTRAINTS;

CREATE TABLE ADR.USERS
(
  USERID                    NUMBER(20)          NOT NULL,
  USER_GROUP_TYPE_ID        NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  USERNAME                  VARCHAR2(30 BYTE)   NOT NULL,
  FULL_NAME                 VARCHAR2(100 BYTE),
  PASSWORD                  VARCHAR2(128 BYTE),
  PASSWORD_CREATE_DATE      DATE,
  PASSWORD_CHANGE_DATE      DATE                NOT NULL,
  PASSWORD_EXPIRE_DATE      DATE,
  ACCOUNT_EXPIRE_DATE       DATE,
  ACCOUNT_LOCK_DATE         DATE,
  INITIAL_LOGIN_DATE        DATE,
  NUMBER_OF_LOGIN_ATTEMPTS  NUMBER(3),
  FIRST_NAME                VARCHAR2(30 BYTE),
  MIDDLE_NAME               VARCHAR2(30 BYTE),
  LAST_NAME                 VARCHAR2(40 BYTE),
  MAIDEN_NAME               VARCHAR2(40 BYTE),
  EMPLOYEE_NUMBER           VARCHAR2(50 BYTE),
  JOB_TITLE                 VARCHAR2(80 BYTE),
  EMAIL_ADDRESS             VARCHAR2(128 BYTE),
  TELEPHONE_NUMBER          VARCHAR2(30 BYTE),
  AGREEMENT_SIGNATURE_CODE  VARCHAR2(6 BYTE),
  SIGNATURE_VERIFIED_IND    VARCHAR2(1 BYTE),
  FAX_NUMBER                VARCHAR2(30 BYTE),
  INACTIVE_FLAG             NUMBER(1)           DEFAULT 0                     NOT NULL,
  INACTIVE_DATE             DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USERS IS 'The table is an application administration table used to designate the authorized users of the database.';

COMMENT ON COLUMN ADR.USERS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.USER_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.USERS.USERNAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.FULL_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.PASSWORD IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.PASSWORD_CHANGE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.PASSWORD_EXPIRE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.ACCOUNT_EXPIRE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.ACCOUNT_LOCK_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.FIRST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.MIDDLE_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.LAST_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.MAIDEN_NAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.EMPLOYEE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.EMAIL_ADDRESS IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.TELEPHONE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.FAX_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USERS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USERS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USERS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USERS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.USER_1_STDINST_FK_I ON ADR.USERS
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.USER_1_UK2_UK ON ADR.USERS
(USERNAME)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USER_1_USRGRPTYP_FK_I ON ADR.USERS
(USER_GROUP_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.USER_PK ON ADR.USERS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.USERS_audir_trgr
after Update or Delete or Insert
ON ADR.USERS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into USERS_H
		(
		USERS_H_ID,
		USERID,
		STD_INSTITUTION_ID,
		USERNAME,
		FULL_NAME,
		PASSWORD,
		PASSWORD_CREATE_DATE,
		PASSWORD_CHANGE_DATE,
		PASSWORD_EXPIRE_DATE,
		USER_GROUP_TYPE_ID,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		MAIDEN_NAME,
		EMPLOYEE_NUMBER,
		EMAIL_ADDRESS,
		TELEPHONE_NUMBER,
		FAX_NUMBER,
		JOB_TITLE,
		AGREEMENT_SIGNATURE_CODE,
		SIGNATURE_VERIFIED_IND,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		ACCOUNT_EXPIRE_DATE,
		ACCOUNT_LOCK_DATE,
		NUMBER_OF_LOGIN_ATTEMPTS,
		INITIAL_LOGIN_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USERS_H_S.nextval,
		:old.USERID,
		:old.STD_INSTITUTION_ID,
		:old.USERNAME,
		:old.FULL_NAME,
		:old.PASSWORD,
		:old.PASSWORD_CREATE_DATE,
		:old.PASSWORD_CHANGE_DATE,
		:old.PASSWORD_EXPIRE_DATE,
		:old.USER_GROUP_TYPE_ID,
		:old.FIRST_NAME,
		:old.MIDDLE_NAME,
		:old.LAST_NAME,
		:old.MAIDEN_NAME,
		:old.EMPLOYEE_NUMBER,
		:old.EMAIL_ADDRESS,
		:old.TELEPHONE_NUMBER,
		:old.FAX_NUMBER,
		:old.JOB_TITLE,
		:old.AGREEMENT_SIGNATURE_CODE,
		:old.SIGNATURE_VERIFIED_IND,
		:old.INACTIVE_FLAG,
		:old.INACTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.ACCOUNT_EXPIRE_DATE,
		:old.ACCOUNT_LOCK_DATE,
		:old.NUMBER_OF_LOGIN_ATTEMPTS,
		:old.INITIAL_LOGIN_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into USERS_H
		(
		USERS_H_ID,
		USERID,
		STD_INSTITUTION_ID,
		USERNAME,
		FULL_NAME,
		PASSWORD,
		PASSWORD_CREATE_DATE,
		PASSWORD_CHANGE_DATE,
		PASSWORD_EXPIRE_DATE,
		USER_GROUP_TYPE_ID,
		FIRST_NAME,
		MIDDLE_NAME,
		LAST_NAME,
		MAIDEN_NAME,
		EMPLOYEE_NUMBER,
		EMAIL_ADDRESS,
		TELEPHONE_NUMBER,
		FAX_NUMBER,
		JOB_TITLE,
		AGREEMENT_SIGNATURE_CODE,
		SIGNATURE_VERIFIED_IND,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		ACCOUNT_EXPIRE_DATE,
		ACCOUNT_LOCK_DATE,
		NUMBER_OF_LOGIN_ATTEMPTS,
		INITIAL_LOGIN_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USERS_H_S.nextval,
		:new.USERID,
		:new.STD_INSTITUTION_ID,
		:new.USERNAME,
		:new.FULL_NAME,
		:new.PASSWORD,
		:new.PASSWORD_CREATE_DATE,
		:new.PASSWORD_CHANGE_DATE,
		:new.PASSWORD_EXPIRE_DATE,
		:new.USER_GROUP_TYPE_ID,
		:new.FIRST_NAME,
		:new.MIDDLE_NAME,
		:new.LAST_NAME,
		:new.MAIDEN_NAME,
		:new.EMPLOYEE_NUMBER,
		:new.EMAIL_ADDRESS,
		:new.TELEPHONE_NUMBER,
		:new.FAX_NUMBER,
		:new.JOB_TITLE,
		:new.AGREEMENT_SIGNATURE_CODE,
		:new.SIGNATURE_VERIFIED_IND,
		:new.INACTIVE_FLAG,
		:new.INACTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.ACCOUNT_EXPIRE_DATE,
		:new.ACCOUNT_LOCK_DATE,
		:new.NUMBER_OF_LOGIN_ATTEMPTS,
		:new.INITIAL_LOGIN_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end USERS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM USERS FOR ADR.USERS;


ALTER TABLE ADR.USERS ADD (
  CONSTRAINT AVCON_1172521913_INACT_042
  CHECK (INACTIVE_FLAG BETWEEN 0 AND 0 OR INACTIVE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT USER_PK
  PRIMARY KEY
  (USERID)
  USING INDEX ADR.USER_PK
  ENABLE VALIDATE,
  CONSTRAINT USER_1_UK2_UK
  UNIQUE (USERNAME)
  USING INDEX ADR.USER_1_UK2_UK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USERS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USERS TO ADR_RW;

GRANT SELECT ON ADR.USERS TO CARUSR_R;

GRANT SELECT ON ADR.USERS TO IAM_R;

GRANT INSERT, SELECT, UPDATE ON ADR.USERS TO IAM_RW;
ALTER TABLE ADR.USERS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USERS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.USERS_H
(
  USERS_H_ID                NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  USERID                    NUMBER(20)          NOT NULL,
  USER_GROUP_TYPE_ID        NUMBER(20),
  STD_INSTITUTION_ID        NUMBER(20),
  USERNAME                  VARCHAR2(30 BYTE)   NOT NULL,
  FULL_NAME                 VARCHAR2(100 BYTE),
  PASSWORD                  VARCHAR2(128 BYTE),
  PASSWORD_CREATE_DATE      DATE,
  PASSWORD_CHANGE_DATE      DATE,
  PASSWORD_EXPIRE_DATE      DATE,
  ACCOUNT_EXPIRE_DATE       DATE,
  ACCOUNT_LOCK_DATE         DATE,
  INITIAL_LOGIN_DATE        DATE,
  NUMBER_OF_LOGIN_ATTEMPTS  NUMBER(3),
  FIRST_NAME                VARCHAR2(30 BYTE),
  MIDDLE_NAME               VARCHAR2(30 BYTE),
  LAST_NAME                 VARCHAR2(40 BYTE),
  MAIDEN_NAME               VARCHAR2(40 BYTE),
  EMPLOYEE_NUMBER           VARCHAR2(50 BYTE),
  JOB_TITLE                 VARCHAR2(80 BYTE),
  EMAIL_ADDRESS             VARCHAR2(128 BYTE),
  TELEPHONE_NUMBER          VARCHAR2(30 BYTE),
  FAX_NUMBER                VARCHAR2(30 BYTE),
  AGREEMENT_SIGNATURE_CODE  VARCHAR2(6 BYTE),
  SIGNATURE_VERIFIED_IND    VARCHAR2(1 BYTE),
  INACTIVE_FLAG             NUMBER(1)           DEFAULT 0                     NOT NULL,
  INACTIVE_DATE             DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USERS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.USERS_H.USERS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.USERS_H.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.USER_GROUP_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.USERS_H.USERNAME IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.ACCOUNT_EXPIRE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.ACCOUNT_LOCK_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.NUMBER_OF_LOGIN_ATTEMPTS IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.FIRST_NAME IS 'BR7323 Representative First Name is defined as the given name of the association type identified.';

COMMENT ON COLUMN ADR.USERS_H.MIDDLE_NAME IS 'BR7325 Representative middle Name is defined as the middle name of the association type identified.';

COMMENT ON COLUMN ADR.USERS_H.LAST_NAME IS 'BR7321 Representative Last Name is defined as the family name of the association type identified.';

COMMENT ON COLUMN ADR.USERS_H.EMAIL_ADDRESS IS 'The email address for the person or insurance company.';

COMMENT ON COLUMN ADR.USERS_H.TELEPHONE_NUMBER IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USERS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USERS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USERS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USERS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USERS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERH_PK ON ADR.USERS_H
(USERS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM USERS_H FOR ADR.USERS_H;


ALTER TABLE ADR.USERS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_028
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT USERH_PK
  PRIMARY KEY
  (USERS_H_ID)
  USING INDEX ADR.USERH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USERS_H TO ADR_R;

GRANT SELECT ON ADR.USERS_H TO ADR_RW;

GRANT SELECT ON ADR.USERS_H TO CARUSR_R;

GRANT SELECT ON ADR.USERS_H TO IAM_R;

GRANT INSERT, SELECT ON ADR.USERS_H TO IAM_RW;
ALTER TABLE ADR.USER_LOGIN
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_LOGIN CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_LOGIN
(
  ID                       NUMBER(20)           NOT NULL,
  USERID                   NUMBER(20)           NOT NULL,
  LAST_LOGIN_SUCCESS_DATE  DATE,
  LAST_LOGIN_FAILURE_DATE  DATE,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_LOGIN IS 'The table is an application administration table that stores login history for database users.';

COMMENT ON COLUMN ADR.USER_LOGIN.ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_LOGIN.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_LOGIN.LAST_LOGIN_SUCCESS_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_LOGIN.LAST_LOGIN_FAILURE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_LOGIN.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_LOGIN.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_LOGIN.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_LOGIN.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_LOGIN.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERLOGIN_PK ON ADR.USER_LOGIN
(ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERLOGIN_USER_1_FK_I ON ADR.USER_LOGIN
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.USER_LOGIN_audir_trgr
after Update or Delete or Insert
ON ADR.USER_LOGIN
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into USER_LOGIN_H
		(
		USER_LOGIN_H_ID,
		ID,
		USERID,
		LAST_LOGIN_SUCCESS_DATE,
		LAST_LOGIN_FAILURE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_LOGIN_H_S.nextval,
		:old.ID,
		:old.USERID,
		:old.LAST_LOGIN_SUCCESS_DATE,
		:old.LAST_LOGIN_FAILURE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into USER_LOGIN_H
		(
		USER_LOGIN_H_ID,
		ID,
		USERID,
		LAST_LOGIN_SUCCESS_DATE,
		LAST_LOGIN_FAILURE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_LOGIN_H_S.nextval,
		:new.ID,
		:new.USERID,
		:new.LAST_LOGIN_SUCCESS_DATE,
		:new.LAST_LOGIN_FAILURE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end USER_LOGIN_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM USER_LOGIN FOR ADR.USER_LOGIN;


ALTER TABLE ADR.USER_LOGIN ADD (
  CONSTRAINT USERLOGIN_PK
  PRIMARY KEY
  (ID)
  USING INDEX ADR.USERLOGIN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.USER_LOGIN ADD (
  CONSTRAINT USERLOGIN_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_LOGIN TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_LOGIN TO ADR_RW;

GRANT SELECT ON ADR.USER_LOGIN TO CARUSR_R;
ALTER TABLE ADR.USER_LOGIN_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_LOGIN_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_LOGIN_H
(
  USER_LOGIN_H_ID          NUMBER(20)           NOT NULL,
  TRANSACTION_TYPE_CODE    VARCHAR2(1 BYTE)     NOT NULL,
  ID                       NUMBER(20)           NOT NULL,
  USERID                   NUMBER(20)           NOT NULL,
  LAST_LOGIN_SUCCESS_DATE  DATE,
  LAST_LOGIN_FAILURE_DATE  DATE,
  RECORD_CREATED_BY        VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_CREATED_DATE      DATE                 NOT NULL,
  RECORD_MODIFIED_BY       VARCHAR2(30 BYTE)    NOT NULL,
  RECORD_MODIFIED_DATE     DATE                 NOT NULL,
  RECORD_MODIFIED_COUNT    NUMBER(10)           NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_LOGIN_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.USER_LOGIN_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.LAST_LOGIN_SUCCESS_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.USER_LOGIN_H.LAST_LOGIN_FAILURE_DATE IS 'Audit Field - Date the record was created in database';

COMMENT ON COLUMN ADR.USER_LOGIN_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_LOGIN_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_LOGIN_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_LOGIN_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERLOGINH_PK ON ADR.USER_LOGIN_H
(USER_LOGIN_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM USER_LOGIN_H FOR ADR.USER_LOGIN_H;


ALTER TABLE ADR.USER_LOGIN_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_015
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT USERLOGINH_PK
  PRIMARY KEY
  (USER_LOGIN_H_ID)
  USING INDEX ADR.USERLOGINH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_LOGIN_H TO ADR_R;

GRANT SELECT ON ADR.USER_LOGIN_H TO ADR_RW;

GRANT SELECT ON ADR.USER_LOGIN_H TO CARUSR_R;
ALTER TABLE ADR.USER_PERMISSIONS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_PERMISSIONS CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_PERMISSIONS
(
  USER_PERMISSION_ID     NUMBER(20)             NOT NULL,
  PERMISSION_TYPE_ID     NUMBER(20)             NOT NULL,
  USERID                 NUMBER(20)             NOT NULL,
  ACTIVE_DATE            DATE,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_PERMISSIONS IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.USER_PERMISSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERPERM_PK ON ADR.USER_PERMISSIONS
(USER_PERMISSION_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERPERM_STD_PERM_FK_I ON ADR.USER_PERMISSIONS
(PERMISSION_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERPERM_USER_1_FK_I ON ADR.USER_PERMISSIONS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.USER_PERMISSIONS_audir_trgr
after Update or Delete or Insert
ON ADR.USER_PERMISSIONS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into USER_PERMISSIONS_H
		(
		USER_PERMISSIONS_H_ID,
		USER_PERMISSION_ID,
		USERID,
		PERMISSION_TYPE_ID,
		ACTIVE_DATE,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_PERMISSIONS_H_S.nextval,
		:old.USER_PERMISSION_ID,
		:old.USERID,
		:old.PERMISSION_TYPE_ID,
		:old.ACTIVE_DATE,
		:old.INACTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into USER_PERMISSIONS_H
		(
		USER_PERMISSIONS_H_ID,
		USER_PERMISSION_ID,
		USERID,
		PERMISSION_TYPE_ID,
		ACTIVE_DATE,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_PERMISSIONS_H_S.nextval,
		:new.USER_PERMISSION_ID,
		:new.USERID,
		:new.PERMISSION_TYPE_ID,
		:new.ACTIVE_DATE,
		:new.INACTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end USER_PERMISSIONS_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM USER_PERMISSIONS FOR ADR.USER_PERMISSIONS;


ALTER TABLE ADR.USER_PERMISSIONS ADD (
  CONSTRAINT USERPERM_PK
  PRIMARY KEY
  (USER_PERMISSION_ID)
  USING INDEX ADR.USERPERM_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.USER_PERMISSIONS ADD (
  CONSTRAINT USERPERM_STD_PERM_FK 
  FOREIGN KEY (PERMISSION_TYPE_ID) 
  REFERENCES ADRREF.STD_PERMISSIONTYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT USERPERM_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_PERMISSIONS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_PERMISSIONS TO ADR_RW;

GRANT SELECT ON ADR.USER_PERMISSIONS TO CARUSR_R;
ALTER TABLE ADR.USER_PERMISSIONS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_PERMISSIONS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_PERMISSIONS_H
(
  USER_PERMISSIONS_H_ID  NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  USER_PERMISSION_ID     NUMBER(20)             NOT NULL,
  USERID                 NUMBER(20)             NOT NULL,
  PERMISSION_TYPE_ID     NUMBER(20)             NOT NULL,
  ACTIVE_DATE            DATE,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_PERMISSIONS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.USER_PERMISSIONS_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.USER_PERMISSION_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.PERMISSION_TYPE_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_PERMISSIONS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USRPERMH_PK ON ADR.USER_PERMISSIONS_H
(USER_PERMISSIONS_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM USER_PERMISSIONS_H FOR ADR.USER_PERMISSIONS_H;


ALTER TABLE ADR.USER_PERMISSIONS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_044
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT USRPERMH_PK
  PRIMARY KEY
  (USER_PERMISSIONS_H_ID)
  USING INDEX ADR.USRPERMH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_PERMISSIONS_H TO ADR_R;

GRANT SELECT ON ADR.USER_PERMISSIONS_H TO ADR_RW;

GRANT SELECT ON ADR.USER_PERMISSIONS_H TO CARUSR_R;
ALTER TABLE ADR.USER_PERMISSION_SETS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_PERMISSION_SETS CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_PERMISSION_SETS
(
  USER_PERMISSION_SET_ID  NUMBER(20)            NOT NULL,
  PERMISSION_SET_ID       NUMBER(20)            NOT NULL,
  USERID                  NUMBER(20)            NOT NULL,
  RECORD_CREATED_BY       VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_CREATED_DATE     DATE                  NOT NULL,
  RECORD_MODIFIED_BY      VARCHAR2(30 BYTE)     NOT NULL,
  RECORD_MODIFIED_DATE    DATE                  NOT NULL,
  RECORD_MODIFIED_COUNT   NUMBER(10)            NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_PERMISSION_SETS IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.USER_PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.USERPERMST_PERMSET_FK_I ON ADR.USER_PERMISSION_SETS
(PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.USERPERMST_PK ON ADR.USER_PERMISSION_SETS
(USER_PERMISSION_SET_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERPERMST_USER_1_FK_I ON ADR.USER_PERMISSION_SETS
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.USER_PERM_SET_audir_trgr
after Update or Delete or Insert
ON ADR.USER_PERMISSION_SETS
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into USER_PERMISSION_SETS_H
		(
		USER_PERMISSION_SET_H_ID,
		USER_PERMISSION_SET_ID,
		USERID,
		PERMISSION_SET_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_PERMISSION_SETS_H_S.nextval,
		:old.USER_PERMISSION_SET_ID,
		:old.USERID,
		:old.PERMISSION_SET_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into USER_PERMISSION_SETS_H
		(
		USER_PERMISSION_SET_H_ID,
		USER_PERMISSION_SET_ID,
		USERID,
		PERMISSION_SET_ID,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_PERMISSION_SETS_H_S.nextval,
		:new.USER_PERMISSION_SET_ID,
		:new.USERID,
		:new.PERMISSION_SET_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end USER_PERM_SET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM USER_PERMISSION_SETS FOR ADR.USER_PERMISSION_SETS;


ALTER TABLE ADR.USER_PERMISSION_SETS ADD (
  CONSTRAINT USERPERMST_PK
  PRIMARY KEY
  (USER_PERMISSION_SET_ID)
  USING INDEX ADR.USERPERMST_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.USER_PERMISSION_SETS ADD (
  CONSTRAINT USERPERMST_PERMSET_FK 
  FOREIGN KEY (PERMISSION_SET_ID) 
  REFERENCES ADR.PERMISSION_SET (PERMISSION_SET_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT USERPERMST_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_PERMISSION_SETS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_PERMISSION_SETS TO ADR_RW;
ALTER TABLE ADR.USER_PERMISSION_SETS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_PERMISSION_SETS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_PERMISSION_SETS_H
(
  USER_PERMISSION_SET_H_ID  NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  USER_PERMISSION_SET_ID    NUMBER(20)          NOT NULL,
  USERID                    NUMBER(20)          NOT NULL,
  PERMISSION_SET_ID         NUMBER(20)          NOT NULL,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_PERMISSION_SETS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.USER_PERMISSION_SET_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.USER_PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.PERMISSION_SET_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_PERMISSION_SETS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USRPERMSTH_PK ON ADR.USER_PERMISSION_SETS_H
(USER_PERMISSION_SET_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM USER_PERMISSION_SETS_H FOR ADR.USER_PERMISSION_SETS_H;


ALTER TABLE ADR.USER_PERMISSION_SETS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_037
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT USRPERMSTH_PK
  PRIMARY KEY
  (USER_PERMISSION_SET_H_ID)
  USING INDEX ADR.USRPERMSTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_PERMISSION_SETS_H TO ADR_R;

GRANT SELECT ON ADR.USER_PERMISSION_SETS_H TO ADR_RW;
ALTER TABLE ADR.USER_ROLES
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_ROLES CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_ROLES
(
  USER_ROLE_ID           NUMBER(20)             NOT NULL,
  USERID                 NUMBER(20)             NOT NULL,
  ROLE_ID                NUMBER(20)             NOT NULL,
  INACTIVE_FLAG          NUMBER(1)              DEFAULT 0                     NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_ROLES IS 'Intersection table mapping users and roles The USER_ROLES table is an application administration table that stores the role that determines database access for users.';

COMMENT ON COLUMN ADR.USER_ROLES.USER_ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_ROLES.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_ROLES.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_ROLES.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_ROLES.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_ROLES.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_ROLES.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERROLE_PK ON ADR.USER_ROLES
(USER_ROLE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERROLE_ROLE_FK_I ON ADR.USER_ROLES
(ROLE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.USERROLE_USER_1_FK_I ON ADR.USER_ROLES
(USERID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.USER_ROLES_audir_trgr
after Update or Delete or Insert
ON ADR.USER_ROLES
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into USER_ROLES_H
		(
		USER_ROLES_H_ID,
		USER_ROLE_ID,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		USERID,
		ROLE_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_ROLES_H_S.nextval,
		:old.USER_ROLE_ID,
		:old.INACTIVE_FLAG,
		:old.INACTIVE_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.USERID,
		:old.ROLE_ID,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into USER_ROLES_H
		(
		USER_ROLES_H_ID,
		USER_ROLE_ID,
		INACTIVE_FLAG,
		INACTIVE_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		USERID,
		ROLE_ID,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		USER_ROLES_H_S.nextval,
		:new.USER_ROLE_ID,
		:new.INACTIVE_FLAG,
		:new.INACTIVE_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.USERID,
		:new.ROLE_ID,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

end USER_ROLES_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM USER_ROLES FOR ADR.USER_ROLES;


ALTER TABLE ADR.USER_ROLES ADD (
  CONSTRAINT AVCON_1172521913_INACT_116
  CHECK (INACTIVE_FLAG BETWEEN 0 AND 0 OR INACTIVE_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT USERROLE_PK
  PRIMARY KEY
  (USER_ROLE_ID)
  USING INDEX ADR.USERROLE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.USER_ROLES ADD (
  CONSTRAINT USERROLE_ROLE_FK 
  FOREIGN KEY (ROLE_ID) 
  REFERENCES ADR.ROLES (ROLE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT USERROLE_USER_1_FK 
  FOREIGN KEY (USERID) 
  REFERENCES ADR.USERS (USERID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_ROLES TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_ROLES TO ADR_RW;

GRANT SELECT ON ADR.USER_ROLES TO CARUSR_R;

GRANT SELECT ON ADR.USER_ROLES TO IAM_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_ROLES TO IAM_RW;
ALTER TABLE ADR.USER_ROLES_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.USER_ROLES_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.USER_ROLES_H
(
  USER_ROLES_H_ID        NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  USER_ROLE_ID           NUMBER(20)             NOT NULL,
  USERID                 NUMBER(20)             NOT NULL,
  ROLE_ID                NUMBER(20)             NOT NULL,
  INACTIVE_FLAG          NUMBER(1)              DEFAULT 0                     NOT NULL,
  INACTIVE_DATE          DATE,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.USER_ROLES_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.USER_ROLES_H.USER_ROLES_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.USER_ROLES_H.USER_ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_ROLES_H.USERID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES_H.ROLE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.USER_ROLES_H.INACTIVE_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES_H.INACTIVE_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.USER_ROLES_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.USER_ROLES_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.USER_ROLES_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.USER_ROLES_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.USER_ROLES_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.USERROLEH_PK ON ADR.USER_ROLES_H
(USER_ROLES_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM USER_ROLES_H FOR ADR.USER_ROLES_H;


ALTER TABLE ADR.USER_ROLES_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_024
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT USERROLEH_PK
  PRIMARY KEY
  (USER_ROLES_H_ID)
  USING INDEX ADR.USERROLEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.USER_ROLES_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.USER_ROLES_H TO ADR_RW;

GRANT SELECT ON ADR.USER_ROLES_H TO CARUSR_R;

GRANT SELECT ON ADR.USER_ROLES_H TO IAM_R;

GRANT INSERT, SELECT ON ADR.USER_ROLES_H TO IAM_RW;
ALTER TABLE ADR.VA_PERSON_CLASS_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VA_PERSON_CLASS_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.VA_PERSON_CLASS_DETAIL
(
  VA_PERSON_CLASS_DETAIL_ID  NUMBER(20)         NOT NULL,
  PERSON_ID                  NUMBER(20)         NOT NULL,
  STD_PERSONCLASS_ID         NUMBER(20),
  STD_INSTITUTION_ID         NUMBER(20),
  PERSON_CLASS_START_DATE    DATE,
  PERSON_CLASS_END_DATE      DATE,
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL,
  RECORD_MODIFIED_SITE_ID    NUMBER(20),
  RECORD_MODIFIED_SRC_NAME   VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VA_PERSON_CLASS_DETAIL IS 'The table stores data for persons designated as practitioners for the VHA. The Person Class designation shows what type of function(s) practitioners are authorized to perform (for example, audiologist, social worker, and chiropractor). DEMOGRAPHICS';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.VA_PERSON_CLASS_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.STD_PERSONCLASS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.PERSON_CLASS_START_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.PERSON_CLASS_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.VAPRSNCLAS_PK ON ADR.VA_PERSON_CLASS_DETAIL
(VA_PERSON_CLASS_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPRSNCLAS_PRSN_FK_I ON ADR.VA_PERSON_CLASS_DETAIL
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPRSNCLAS_STDINST2_FK_I ON ADR.VA_PERSON_CLASS_DETAIL
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPRSNCLAS_STDINST_FK_I ON ADR.VA_PERSON_CLASS_DETAIL
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPRSNCLAS_STDPRCLASS_FK_I ON ADR.VA_PERSON_CLASS_DETAIL
(STD_PERSONCLASS_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.VA_PERSON_CLASS_DET_audir_trgr
after Update or Delete or Insert
ON ADR.VA_PERSON_CLASS_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into VA_PERSON_CLASS_DETAIL_H
		(
		VA_PERSON_CLASS_DETAIL_H_ID,
		VA_PERSON_CLASS_DETAIL_ID,
		PERSON_CLASS_START_DATE,
		PERSON_CLASS_END_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_PERSONCLASS_ID,
		STD_INSTITUTION_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		VA_PERSON_CLASS_DETAIL_H_S.nextval,
		:old.VA_PERSON_CLASS_DETAIL_ID,
		:old.PERSON_CLASS_START_DATE,
		:old.PERSON_CLASS_END_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.STD_PERSONCLASS_ID,
		:old.STD_INSTITUTION_ID,
		:old.PERSON_ID,
		:old.RECORD_MODIFIED_COUNT,
		:old.RECORD_MODIFIED_SITE_ID,
		:old.RECORD_MODIFIED_SRC_NAME,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into VA_PERSON_CLASS_DETAIL_H
		(
		VA_PERSON_CLASS_DETAIL_H_ID,
		VA_PERSON_CLASS_DETAIL_ID,
		PERSON_CLASS_START_DATE,
		PERSON_CLASS_END_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		STD_PERSONCLASS_ID,
		STD_INSTITUTION_ID,
		PERSON_ID,
		RECORD_MODIFIED_COUNT,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		VA_PERSON_CLASS_DETAIL_H_S.nextval,
		:new.VA_PERSON_CLASS_DETAIL_ID,
		:new.PERSON_CLASS_START_DATE,
		:new.PERSON_CLASS_END_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.STD_PERSONCLASS_ID,
		:new.STD_INSTITUTION_ID,
		:new.PERSON_ID,
		:new.RECORD_MODIFIED_COUNT,
		:new.RECORD_MODIFIED_SITE_ID,
		:new.RECORD_MODIFIED_SRC_NAME,
		mDML
		);
	end if;

end VA_PERSON_CLASS_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM VA_PERSON_CLASS_DETAIL FOR ADR.VA_PERSON_CLASS_DETAIL;


ALTER TABLE ADR.VA_PERSON_CLASS_DETAIL ADD (
  CONSTRAINT VAPRSNCLAS_PK
  PRIMARY KEY
  (VA_PERSON_CLASS_DETAIL_ID)
  USING INDEX ADR.VAPRSNCLAS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.VA_PERSON_CLASS_DETAIL ADD (
  CONSTRAINT VAPRSNCLAS_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VA_PERSON_CLASS_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.VA_PERSON_CLASS_DETAIL TO PSIM_ADR_RW;

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL TO DNS   STALLP;
ALTER TABLE ADR.VA_PERSON_CLASS_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VA_PERSON_CLASS_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.VA_PERSON_CLASS_DETAIL_H
(
  VA_PERSON_CLASS_DETAIL_H_ID  NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  PERSON_ID                    NUMBER(20)       NOT NULL,
  STD_PERSONCLASS_ID           NUMBER(20),
  VA_PERSON_CLASS_DETAIL_ID    NUMBER(20)       NOT NULL,
  STD_INSTITUTION_ID           NUMBER(20),
  PERSON_CLASS_START_DATE      DATE,
  PERSON_CLASS_END_DATE        DATE,
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL,
  RECORD_MODIFIED_SITE_ID      NUMBER(20),
  RECORD_MODIFIED_SRC_NAME     VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VA_PERSON_CLASS_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.VA_PERSON_CLASS_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.STD_PERSONCLASS_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.VA_PERSON_CLASS_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.PERSON_CLASS_START_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.PERSON_CLASS_END_DATE IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.VA_PERSON_CLASS_DETAIL_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.VAPRSNCLAH_PK ON ADR.VA_PERSON_CLASS_DETAIL_H
(VA_PERSON_CLASS_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPRSNCLAH_PRSNID_NU_I ON ADR.VA_PERSON_CLASS_DETAIL_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VA_PERSON_CLASS_DETAIL_H FOR ADR.VA_PERSON_CLASS_DETAIL_H;


ALTER TABLE ADR.VA_PERSON_CLASS_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_065
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT VAPRSNCLAH_PK
  PRIMARY KEY
  (VA_PERSON_CLASS_DETAIL_H_ID)
  USING INDEX ADR.VAPRSNCLAH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL_H TO PSIM_ADR_R;

GRANT SELECT ON ADR.VA_PERSON_CLASS_DETAIL_H TO DNS   STALLP;
ALTER TABLE ADR.VA_PERSON_DIVISION_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VA_PERSON_DIVISION_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.VA_PERSON_DIVISION_DETAIL
(
  VA_PERSON_DIVISION_DETAIL_ID  NUMBER(20)      NOT NULL,
  PERSON_ID                     NUMBER(20)      NOT NULL,
  STD_INSTITUTION_ID            NUMBER(20),
  DEFAULT_DIVISION_FLAG         NUMBER(1)       DEFAULT 0                     NOT NULL,
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL,
  RECORD_MODIFIED_SITE_ID       NUMBER(20),
  RECORD_MODIFIED_SRC_NAME      VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VA_PERSON_DIVISION_DETAIL IS 'The table stores data for persons designated as VA Associates and with what division they are associated.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.VA_PERSON_DIVISION_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.DEFAULT_DIVISION_FLAG IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.VAPERDVSN_PK ON ADR.VA_PERSON_DIVISION_DETAIL
(VA_PERSON_DIVISION_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPERDVSN_PRSN_FK_I ON ADR.VA_PERSON_DIVISION_DETAIL
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPERDVSN_STDINST2_FK_I ON ADR.VA_PERSON_DIVISION_DETAIL
(RECORD_MODIFIED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   10
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPERDVSN_STDINST_FK_I ON ADR.VA_PERSON_DIVISION_DETAIL
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.VA_PERSON_DIV_DET_audir_trgr
after Update or Delete or Insert
ON ADR.VA_PERSON_DIVISION_DETAIL
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into VA_PERSON_DIVISION_DETAIL_H
		(
		VA_PERSON_DIVISION_DETAIL_H_ID,
		VA_PERSON_DIVISION_DETAIL_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		DEFAULT_DIVISION_FLAG,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		VA_PERSON_DIVISION_DETAIL_H_S.nextval,
		:old.VA_PERSON_DIVISION_DETAIL_ID,
		:old.PERSON_ID,
		:old.STD_INSTITUTION_ID,
		:old.DEFAULT_DIVISION_FLAG,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		:old.RECORD_MODIFIED_SITE_ID,
		:old.RECORD_MODIFIED_SRC_NAME,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into VA_PERSON_DIVISION_DETAIL_H
		(
		VA_PERSON_DIVISION_DETAIL_H_ID,
		VA_PERSON_DIVISION_DETAIL_ID,
		PERSON_ID,
		STD_INSTITUTION_ID,
		DEFAULT_DIVISION_FLAG,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		RECORD_MODIFIED_SITE_ID,
		RECORD_MODIFIED_SRC_NAME,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		VA_PERSON_DIVISION_DETAIL_H_S.nextval,
		:new.VA_PERSON_DIVISION_DETAIL_ID,
		:new.PERSON_ID,
		:new.STD_INSTITUTION_ID,
		:new.DEFAULT_DIVISION_FLAG,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		:new.RECORD_MODIFIED_SITE_ID,
		:new.RECORD_MODIFIED_SRC_NAME,
		mDML
		);
	end if;

end VA_PERSON_DIV_DET_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM VA_PERSON_DIVISION_DETAIL FOR ADR.VA_PERSON_DIVISION_DETAIL;


ALTER TABLE ADR.VA_PERSON_DIVISION_DETAIL ADD (
  CONSTRAINT AVCON_1172521913_DEFAU_000
  CHECK (DEFAULT_DIVISION_FLAG BETWEEN 0
AND 0 OR DEFAULT_DIVISION_FLAG BETWEEN 1 AND 1)
  ENABLE VALIDATE,
  CONSTRAINT VAPERDVSN_PK
  PRIMARY KEY
  (VA_PERSON_DIVISION_DETAIL_ID)
  USING INDEX ADR.VAPERDVSN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.VA_PERSON_DIVISION_DETAIL ADD (
  CONSTRAINT VAPERDVSN_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE);

GRANT SELECT ON ADR.VA_PERSON_DIVISION_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VA_PERSON_DIVISION_DETAIL TO ADR_RW;

GRANT SELECT ON ADR.VA_PERSON_DIVISION_DETAIL TO PSIM_ADR_R;

GRANT DELETE, INSERT, UPDATE ON ADR.VA_PERSON_DIVISION_DETAIL TO PSIM_ADR_RW;
ALTER TABLE ADR.VA_PERSON_DIVISION_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VA_PERSON_DIVISION_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.VA_PERSON_DIVISION_DETAIL_H
(
  VA_PERSON_DIVISION_DETAIL_H_ID  NUMBER(20)    NOT NULL,
  TRANSACTION_TYPE_CODE           VARCHAR2(1 BYTE) NOT NULL,
  VA_PERSON_DIVISION_DETAIL_ID    NUMBER(20)    NOT NULL,
  PERSON_ID                       NUMBER(20)    NOT NULL,
  STD_INSTITUTION_ID              NUMBER(20),
  DEFAULT_DIVISION_FLAG           NUMBER(1)     DEFAULT 0                     NOT NULL,
  RECORD_CREATED_BY               VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE             DATE          NOT NULL,
  RECORD_MODIFIED_BY              VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE            DATE          NOT NULL,
  RECORD_MODIFIED_COUNT           NUMBER(10)    NOT NULL,
  RECORD_MODIFIED_SITE_ID         NUMBER(20),
  RECORD_MODIFIED_SRC_NAME        VARCHAR2(30 BYTE)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VA_PERSON_DIVISION_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.VA_PERSON_DIVISION_DETAIL_H_ID IS 'Retrofitted.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.VA_PERSON_DIVISION_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.VA_PERSON_DIVISION_DETAIL_H.RECORD_MODIFIED_SRC_NAME IS 'Believed by ADR to be the record modified source name, but on March, 2012 the column was not populated in any of the 10 base and 10 history tables using the column in the ADR database.  As far as ADR can tell, this is not a standard ADR column that needs to be used in future tables. It is being replaced with the STD_DELIVERYPREFSRCTYPE  Standard Data Services (SDS) reference table.';


CREATE UNIQUE INDEX ADR.VAPERDVSNH_PK ON ADR.VA_PERSON_DIVISION_DETAIL_H
(VA_PERSON_DIVISION_DETAIL_H_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VAPERDVSNH_PRSNID_NU_I ON ADR.VA_PERSON_DIVISION_DETAIL_H
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VA_PERSON_DIVISION_DETAIL_H FOR ADR.VA_PERSON_DIVISION_DETAIL_H;


ALTER TABLE ADR.VA_PERSON_DIVISION_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_032
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT VAPERDVSNH_PK
  PRIMARY KEY
  (VA_PERSON_DIVISION_DETAIL_H_ID)
  USING INDEX ADR.VAPERDVSNH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VA_PERSON_DIVISION_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.VA_PERSON_DIVISION_DETAIL_H TO ADR_RW;

GRANT SELECT ON ADR.VA_PERSON_DIVISION_DETAIL_H TO PSIM_ADR_R;
ALTER TABLE ADR.VCADMIN
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCADMIN CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCADMIN
(
  VCADMIN_ID             NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  VCADMISTATUS_TYPE_ID   NUMBER(20)             NOT NULL,
  ERROR_TXT              VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCADMIN IS 'Log of records sent to VC Contractor';

COMMENT ON COLUMN ADR.VCADMIN.VCADMIN_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCADMIN.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.VCADMIN.VCADMISTATUS_TYPE_ID IS 'FK to ADR.VCADMISTATUS_TYPE table';

COMMENT ON COLUMN ADR.VCADMIN.ERROR_TXT IS 'Error message';

COMMENT ON COLUMN ADR.VCADMIN.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCADMIN.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCADMIN.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VCADMIN.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCADMIN.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.VCADMIN_PERSON_ID ON ADR.VCADMIN
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.VCADMIN_PK ON ADR.VCADMIN
(VCADMIN_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.VCADMIN_AUDIR_TRGR
    AFTER UPDATE OR DELETE OR INSERT
    ON ADR.VCADMIN
    FOR EACH ROW
DECLARE
    mDML   CHAR (1);
BEGIN
    IF DELETING
    THEN
        INSERT INTO ADR.VCADMIN_H (VCADMIN_H_ID,
                                   VCADMIN_ID,
                                   PERSON_ID,
                                   ERROR_TXT,
                                   VCADMISTATUS_TYPE_ID,
                                   TRANSACTION_TYPE_CODE,
                                   RECORD_CREATED_BY,
                                   RECORD_CREATED_DATE,
                                   RECORD_MODIFIED_BY,
                                   RECORD_MODIFIED_DATE,
                                   RECORD_MODIFIED_COUNT)
             VALUES (VCADMIN_H_S.NEXTVAL,
                     :old.VCADMIN_ID,
                     :old.PERSON_ID,
                     :old.ERROR_TXT,
                     :old.VCADMISTATUS_TYPE_ID,
                     'D',
                     :old.RECORD_CREATED_BY,
                     :old.RECORD_CREATED_DATE,
                     :old.RECORD_MODIFIED_BY,
                     :old.RECORD_MODIFIED_DATE,
                     :old.RECORD_MODIFIED_COUNT);
    ELSE
        IF UPDATING
        THEN
            mDML := 'U';
        ELSE
            mDML := 'I';
        END IF;

        INSERT INTO ADR.VCADMIN_H (VCADMIN_H_ID,
                                   VCADMIN_ID,
                                   PERSON_ID,
                                   ERROR_TXT,
                                   VCADMISTATUS_TYPE_ID,
                                   TRANSACTION_TYPE_CODE,
                                   RECORD_CREATED_BY,
                                   RECORD_CREATED_DATE,
                                   RECORD_MODIFIED_BY,
                                   RECORD_MODIFIED_DATE,
                                   RECORD_MODIFIED_COUNT)
             VALUES (VCADMIN_H_S.NEXTVAL,
                     :new.VCADMIN_ID,
                     :new.PERSON_ID,
                     :new.ERROR_TXT,
                     :new.VCADMISTATUS_TYPE_ID,
                     mDML,
                     :new.RECORD_CREATED_BY,
                     :new.RECORD_CREATED_DATE,
                     :new.RECORD_MODIFIED_BY,
                     :new.RECORD_MODIFIED_DATE,
                     :new.RECORD_MODIFIED_COUNT);
    END IF;
END VCADMIN_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM VCADMIN FOR ADR.VCADMIN;


ALTER TABLE ADR.VCADMIN ADD (
  CONSTRAINT VCADMIN_PK
  PRIMARY KEY
  (VCADMIN_ID)
  USING INDEX ADR.VCADMIN_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.VCADMIN ADD (
  CONSTRAINT VCADMIN_PRSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT VCADMIN_VCEADMINSTATS_FK 
  FOREIGN KEY (VCADMISTATUS_TYPE_ID) 
  REFERENCES ADR.VCADMISTATUS_TYPE (VCADMISTATUS_TYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCADMIN TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCADMIN TO ADR_RW;

GRANT SELECT ON ADR.VCADMIN TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCADMIN TO VETCHOICE_RW;
ALTER TABLE ADR.VCADMIN_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCADMIN_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCADMIN_H
(
  VCADMIN_H_ID           NUMBER(20)             NOT NULL,
  VCADMIN_ID             NUMBER(20),
  PERSON_ID              NUMBER(20)             NOT NULL,
  ERROR_TXT              VARCHAR2(30 BYTE),
  VCADMISTATUS_TYPE_ID   NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCADMIN_H IS 'History table of VC Contractor log';

COMMENT ON COLUMN ADR.VCADMIN_H.VCADMIN_H_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCADMIN_H.VCADMIN_ID IS 'FK to ADR.VCADMIN table';

COMMENT ON COLUMN ADR.VCADMIN_H.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.VCADMIN_H.ERROR_TXT IS 'Error message';

COMMENT ON COLUMN ADR.VCADMIN_H.VCADMISTATUS_TYPE_ID IS 'FK to ADR.VCADMISTATUS_TYPE table';

COMMENT ON COLUMN ADR.VCADMIN_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.VCADMIN_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCADMIN_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCADMIN_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VCADMIN_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCADMIN_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE INDEX ADR.VCADMIN_H_PERSON_ID_NU_I ON ADR.VCADMIN_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE UNIQUE INDEX ADR.VCADMIN_H_PK ON ADR.VCADMIN_H
(VCADMIN_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VCADMIN_H FOR ADR.VCADMIN_H;


ALTER TABLE ADR.VCADMIN_H ADD (
  CONSTRAINT VCADMIN_H_PK
  PRIMARY KEY
  (VCADMIN_H_ID)
  USING INDEX ADR.VCADMIN_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCADMIN_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCADMIN_H TO ADR_RW;

GRANT SELECT ON ADR.VCADMIN_H TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCADMIN_H TO VETCHOICE_RW;
ALTER TABLE ADR.VCADMISTATUS_TYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCADMISTATUS_TYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCADMISTATUS_TYPE
(
  VCADMISTATUS_TYPE_ID  NUMBER(20)              NOT NULL,
  CODE                  VARCHAR2(3 BYTE)        NOT NULL,
  NAME                  VARCHAR2(30 BYTE)       NOT NULL,
  DESCRIPTION           VARCHAR2(250 BYTE),
  CREATED               DATE                    NOT NULL,
  UPDATED               DATE,
  CREATEDBY             VARCHAR2(32 BYTE)       NOT NULL,
  UPDATEDBY             VARCHAR2(32 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCADMISTATUS_TYPE IS 'Lookup table used by VetChoice';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.VCADMISTATUS_TYPE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.CODE IS 'Shortened code';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.NAME IS 'User readable name';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.DESCRIPTION IS 'Description of the status';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.CREATED IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.UPDATED IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.CREATEDBY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCADMISTATUS_TYPE.UPDATEDBY IS 'Account/user ID for this modification to the record.';


CREATE UNIQUE INDEX ADR.VCADMISTATUS_TYPE_PK ON ADR.VCADMISTATUS_TYPE
(VCADMISTATUS_TYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VCADMISTATUS_TYPE FOR ADR.VCADMISTATUS_TYPE;


ALTER TABLE ADR.VCADMISTATUS_TYPE ADD (
  CONSTRAINT VCADMISTATUS_TYPE_PK
  PRIMARY KEY
  (VCADMISTATUS_TYPE_ID)
  USING INDEX ADR.VCADMISTATUS_TYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCADMISTATUS_TYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCADMISTATUS_TYPE TO ADR_RW;
ALTER TABLE ADR.VCELIGIBILITY
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCELIGIBILITY CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCELIGIBILITY
(
  VCELIGIBILITY_ID       NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  VCESTATUSTYPE_ID       NUMBER(20)             NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCELIGIBILITY IS 'Current VC Eligibility determinations';

COMMENT ON COLUMN ADR.VCELIGIBILITY.VCELIGIBILITY_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCELIGIBILITY.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.VCELIGIBILITY.VCESTATUSTYPE_ID IS 'FK to ADR.VCESTATUSTYPE table';

COMMENT ON COLUMN ADR.VCELIGIBILITY.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCELIGIBILITY.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCELIGIBILITY.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VCELIGIBILITY.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCELIGIBILITY.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.VCELIGIBILITY_PK ON ADR.VCELIGIBILITY
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VCELIGIBIL_PERSON_ID ON ADR.VCELIGIBILITY
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.VCELIGIB_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.VCELIGIBILITY for each row
declare
     mDML char(1);
begin

  if DELETING then

INSERT INTO ADR.VCELIGIBILITY_H (
   VCELIGIBILITY_H_ID,TRANSACTION_TYPE_CODE,VCELIGIBILITY_ID, PERSON_ID, VCESTATUSTYPE_ID,
   RECORD_CREATED_BY, RECORD_CREATED_DATE, RECORD_MODIFIED_BY,
   RECORD_MODIFIED_DATE, RECORD_MODIFIED_COUNT)
VALUES ( VCELIGIBILITY_H_S.nextval,
'D',
:old.VCELIGIBILITY_ID,
 :old.PERSON_ID ,
 :old.VCESTATUSTYPE_ID ,
 :old.RECORD_CREATED_BY ,
 :old.RECORD_CREATED_DATE ,
 :old.RECORD_MODIFIED_BY ,
 :old.RECORD_MODIFIED_DATE ,
 :old.RECORD_MODIFIED_COUNT  );

  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

  INSERT INTO ADR.VCELIGIBILITY_H (
   VCELIGIBILITY_H_ID,TRANSACTION_TYPE_CODE,VCELIGIBILITY_ID, PERSON_ID, VCESTATUSTYPE_ID,
   RECORD_CREATED_BY, RECORD_CREATED_DATE, RECORD_MODIFIED_BY,
   RECORD_MODIFIED_DATE, RECORD_MODIFIED_COUNT)
VALUES ( VCELIGIBILITY_H_S.nextval,
mDML,
:new.VCELIGIBILITY_ID,
 :new.PERSON_ID ,
 :new.VCESTATUSTYPE_ID ,
 :new.RECORD_CREATED_BY ,
 :new.RECORD_CREATED_DATE ,
 :new.RECORD_MODIFIED_BY ,
 :new.RECORD_MODIFIED_DATE ,
 :new.RECORD_MODIFIED_COUNT  );

  end if;

end VCELIGIB_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM VCELIGIBILITY FOR ADR.VCELIGIBILITY;


ALTER TABLE ADR.VCELIGIBILITY ADD (
  CONSTRAINT VCELIGIBILITY_PK
  PRIMARY KEY
  (VCELIGIBILITY_ID)
  USING INDEX ADR.VCELIGIBILITY_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.VCELIGIBILITY ADD (
  CONSTRAINT VCEELGB_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT VCEELGB_VCESTATS_FK 
  FOREIGN KEY (VCESTATUSTYPE_ID) 
  REFERENCES ADR.VCESTATUSTYPE (VCESTATUSTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCELIGIBILITY TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCELIGIBILITY TO ADR_RW;

GRANT SELECT ON ADR.VCELIGIBILITY TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCELIGIBILITY TO VETCHOICE_RW;
ALTER TABLE ADR.VCELIGIBILITY_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCELIGIBILITY_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCELIGIBILITY_H
(
  VCELIGIBILITY_H_ID     NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  VCELIGIBILITY_ID       NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  VCESTATUSTYPE_ID       NUMBER(20)             NOT NULL,
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCELIGIBILITY_H IS 'Historical VC Eligibility determinations';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.VCELIGIBILITY_H_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.VCELIGIBILITY_ID IS 'FK to ADR.VCELIGIBILITY table';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.VCESTATUSTYPE_ID IS 'FK to ADR.VCESTATUSTYPE table';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCELIGIBILITY_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.VCELIGIBILITY_H_PK ON ADR.VCELIGIBILITY_H
(VCELIGIBILITY_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.VCELIGIBILITY_ID_NU_I ON ADR.VCELIGIBILITY_H
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT );

CREATE INDEX ADR.VCELI_H_PERSON_ID_NU_I ON ADR.VCELIGIBILITY_H
(PERSON_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VCELIGIBILITY_H FOR ADR.VCELIGIBILITY_H;


ALTER TABLE ADR.VCELIGIBILITY_H ADD (
  CONSTRAINT VCELIGIBILITY_H_PK
  PRIMARY KEY
  (VCELIGIBILITY_H_ID)
  USING INDEX ADR.VCELIGIBILITY_H_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCELIGIBILITY_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCELIGIBILITY_H TO ADR_RW;

GRANT SELECT ON ADR.VCELIGIBILITY_H TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCELIGIBILITY_H TO VETCHOICE_RW;
ALTER TABLE ADR.VCESTATUSTYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.VCESTATUSTYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.VCESTATUSTYPE
(
  VCESTATUSTYPE_ID  NUMBER(20)                  NOT NULL,
  CODE              VARCHAR2(3 BYTE)            NOT NULL,
  NAME              VARCHAR2(30 BYTE)           NOT NULL,
  DESCRIPTION       VARCHAR2(250 BYTE),
  CREATED           DATE                        NOT NULL,
  UPDATED           DATE,
  CREATEDBY         VARCHAR2(32 BYTE)           NOT NULL,
  UPDATEDBY         VARCHAR2(32 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.VCESTATUSTYPE IS 'Lookup table used by VetChoice';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.VCESTATUSTYPE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.CODE IS 'Shortened code';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.NAME IS 'User readable name';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.DESCRIPTION IS 'Description of the status';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.CREATED IS 'Date the record was created.';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.UPDATED IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.CREATEDBY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.VCESTATUSTYPE.UPDATEDBY IS 'Account/user ID for this modification to the record.';


CREATE UNIQUE INDEX ADR.VCESTATUSTYPE_PK ON ADR.VCESTATUSTYPE
(VCESTATUSTYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM VCESTATUSTYPE FOR ADR.VCESTATUSTYPE;


ALTER TABLE ADR.VCESTATUSTYPE ADD (
  CONSTRAINT VCESTATUSTYPE_PK
  PRIMARY KEY
  (VCESTATUSTYPE_ID)
  USING INDEX ADR.VCESTATUSTYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.VCESTATUSTYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.VCESTATUSTYPE TO ADR_RW;
ALTER TABLE ADR.WAITTIME
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WAITTIME CASCADE CONSTRAINTS;

CREATE TABLE ADR.WAITTIME
(
  WAITTIME_ID            NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  WAITTYPE_ID            NUMBER(20)             NOT NULL,
  STD_INSTITUTION_ID     NUMBER(20)             NOT NULL,
  CLINIC_STOPCODE        VARCHAR2(6 BYTE),
  CLINIC_STOPCODE_NAME   VARCHAR2(30 BYTE),
  WAITLIST_START_DATE    DATE                   NOT NULL,
  DESIRED_APPT_DATE      DATE,
  DAYS_ELAPSED           NUMBER(4),
  RECORD_RESOLVED_IND    NUMBER(1),
  CLINIC_NAME            VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  VCELIGIBILITY_ID       NUMBER(20),
  IS_ACTIVE              CHAR(1 BYTE)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION WAITTIME_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WAITTIME IS 'Current Wait-Time data';

COMMENT ON COLUMN ADR.WAITTIME.WAITTIME_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WAITTIME.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.WAITTIME.WAITTYPE_ID IS 'FK to ADR.WAITTYPE table';

COMMENT ON COLUMN ADR.WAITTIME.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.WAITTIME.CLINIC_STOPCODE IS 'Clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME.CLINIC_STOPCODE_NAME IS 'Name of the clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME.WAITLIST_START_DATE IS 'Date Veteran was put on waitlist';

COMMENT ON COLUMN ADR.WAITTIME.DESIRED_APPT_DATE IS 'Veteran¿s desired appointment date';

COMMENT ON COLUMN ADR.WAITTIME.DAYS_ELAPSED IS 'Number of days Veteran has been waiting for appointment';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_RESOLVED_IND IS 'Indicator of whether or not the wait-time record was resolved';

COMMENT ON COLUMN ADR.WAITTIME.CLINIC_NAME IS 'The name of the clinic where the patient will be seen.';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WAITTIME.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.WAITTIME.IS_ACTIVE IS 'indicator that WAITTIME record has been solved/completed';


CREATE UNIQUE INDEX ADR.WAITTIME_PK ON ADR.WAITTIME
(RECORD_CREATED_DATE, WAITTIME_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION WAITTIME_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.WT_PERSONID_IDX ON ADR.WAITTIME
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION WT_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION WT_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION WT_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION WT_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE INDEX ADR.WT_VCELIGID_FK ON ADR.WAITTIME
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.WAITTIME_AUDIR_TRGR
   AFTER UPDATE OR INSERT OR DELETE
   ON ADR.WAITTIME
   FOR EACH ROW
DECLARE
   mDML   CHAR (1);
BEGIN
   IF DELETING
   THEN
      mDML := 'D';

      INSERT INTO WAITTIME_H (
      		WAITTIME_H_ID,
  		WAITTIME_ID,
  		PERSON_ID ,
  		WAITTYPE_ID,
  		STD_INSTITUTION_ID,
  		CLINIC_STOPCODE ,
  		CLINIC_STOPCODE_NAME,
  		WAITLIST_START_DATE,
  		DESIRED_APPT_DATE,
  		DAYS_ELAPSED,
  		RECORD_RESOLVED_IND,
  		CLINIC_NAME  ,
  		RECORD_CREATED_BY ,
  		RECORD_CREATED_DATE,
  		RECORD_MODIFIED_BY,
  		RECORD_MODIFIED_DATE,
  		RECORD_MODIFIED_COUNT,
  		VCELIGIBILITY_ID,
  		IS_ACTIVE,
  		TRANSACTION_TYPE_CODE
  )
    VALUES (WAITTIME_H_S.NEXTVAL,
	      :old.WAITTIME_ID,
	      :old.PERSON_ID ,
	      :old.WAITTYPE_ID,
	      :old.STD_INSTITUTION_ID,
	      :old.CLINIC_STOPCODE ,
	      :old.CLINIC_STOPCODE_NAME,
	      :old.WAITLIST_START_DATE,
	      :old.DESIRED_APPT_DATE,
	      :old.DAYS_ELAPSED,
	      :old.RECORD_RESOLVED_IND,
	      :old.CLINIC_NAME  ,
	      :old.RECORD_CREATED_BY ,
	      :old.RECORD_CREATED_DATE,
	      :old.RECORD_MODIFIED_BY,
	      :old.RECORD_MODIFIED_DATE,
	      :old.RECORD_MODIFIED_COUNT,
	      :old.VCELIGIBILITY_ID,
	      :old.IS_ACTIVE,
  		mDML);
   ELSE
      IF UPDATING
      THEN
         mDML := 'U';
      ELSE
         mDML := 'I';
      END IF;

       INSERT INTO WAITTIME_H (
      		  WAITTIME_H_ID,
  		  WAITTIME_ID,
  		  PERSON_ID ,
  		  WAITTYPE_ID,
  		  STD_INSTITUTION_ID,
  		  CLINIC_STOPCODE ,
  		  CLINIC_STOPCODE_NAME,
  		  WAITLIST_START_DATE,
  		  DESIRED_APPT_DATE,
  		  DAYS_ELAPSED,
  		  RECORD_RESOLVED_IND,
  		  CLINIC_NAME  ,
  		  RECORD_CREATED_BY ,
  		  RECORD_CREATED_DATE,
  		  RECORD_MODIFIED_BY,
  		  RECORD_MODIFIED_DATE,
  		  RECORD_MODIFIED_COUNT,
  		  VCELIGIBILITY_ID,
  		  IS_ACTIVE,
  		  TRANSACTION_TYPE_CODE
  	    )
          VALUES (WAITTIME_H_S.NEXTVAL,
		   :new.WAITTIME_ID,
		   :new.PERSON_ID ,
		   :new.WAITTYPE_ID,
		   :new.STD_INSTITUTION_ID,
		   :new.CLINIC_STOPCODE ,
		   :new.CLINIC_STOPCODE_NAME,
		   :new.WAITLIST_START_DATE,
		   :new.DESIRED_APPT_DATE,
		   :new.DAYS_ELAPSED,
		   :new.RECORD_RESOLVED_IND,
		   :new.CLINIC_NAME  ,
		   :new.RECORD_CREATED_BY ,
		   :new.RECORD_CREATED_DATE,
		   :new.RECORD_MODIFIED_BY,
		   :new.RECORD_MODIFIED_DATE,
		   :new.RECORD_MODIFIED_COUNT,
		   :new.VCELIGIBILITY_ID,
		   :new.IS_ACTIVE,
 		 mDML);
   END IF;
END WAITTIME_AUDIR_TRGR;
/


CREATE OR REPLACE PUBLIC SYNONYM WAITTIME FOR ADR.WAITTIME;


ALTER TABLE ADR.WAITTIME ADD (
  CONSTRAINT WAITTIME_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, WAITTIME_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.WAITTIME ADD (
  CONSTRAINT VCELIG_WAITTME_FK 
  FOREIGN KEY (VCELIGIBILITY_ID) 
  REFERENCES ADR.VCELIGIBILITY (VCELIGIBILITY_ID)
  ENABLE VALIDATE,
  CONSTRAINT WAITTIME_PERSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT WAITTIME_WAITTYPE_FK 
  FOREIGN KEY (WAITTYPE_ID) 
  REFERENCES ADR.WAITTYPE (WAITTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WAITTIME TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME TO ADR_RW;

GRANT SELECT ON ADR.WAITTIME TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME TO VETCHOICE_RW;
ALTER TABLE ADR.WAITTIME_ARCHIVE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WAITTIME_ARCHIVE CASCADE CONSTRAINTS;

CREATE TABLE ADR.WAITTIME_ARCHIVE
(
  WAITTIME_ID            NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  WAITTYPE_ID            NUMBER(20)             NOT NULL,
  STD_INSTITUTION_ID     NUMBER(20)             NOT NULL,
  CLINIC_STOPCODE        VARCHAR2(6 BYTE),
  CLINIC_STOPCODE_NAME   VARCHAR2(30 BYTE),
  WAITLIST_START_DATE    DATE                   NOT NULL,
  DESIRED_APPT_DATE      DATE,
  DAYS_ELAPSED           NUMBER(4),
  RECORD_RESOLVED_IND    NUMBER(1),
  CLINIC_NAME            VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  VCELIGIBILITY_ID       NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION WAITTIME_ARCHIVE_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WAITTIME_ARCHIVE IS 'Current Wait-Time data';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.WAITTIME_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.WAITTYPE_ID IS 'FK to ADR.WAITTYPE table';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.CLINIC_STOPCODE IS 'Clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.CLINIC_STOPCODE_NAME IS 'Name of the clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.WAITLIST_START_DATE IS 'Date Veteran was put on waitlist';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.DESIRED_APPT_DATE IS 'Veteran¿s desired appointment date';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.DAYS_ELAPSED IS 'Number of days Veteran has been waiting for appointment';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_RESOLVED_IND IS 'Indicator of whether or not the wait-time record was resolved';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.CLINIC_NAME IS 'The name of the clinic where the patient will be seen.';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WAITTIME_ARCHIVE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WAITTIME_ARCHIVE_PK ON ADR.WAITTIME_ARCHIVE
(RECORD_CREATED_DATE, WAITTIME_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION WAITTIME_ARCHIVE_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.WTARCH_VCELIG_FK ON ADR.WAITTIME_ARCHIVE
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WTA_PERSONID_IDX ON ADR.WAITTIME_ARCHIVE
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION WTA_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION WTA_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION WTA_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION WTA_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WAITTIME_ARCHIVE FOR ADR.WAITTIME_ARCHIVE;


ALTER TABLE ADR.WAITTIME_ARCHIVE ADD (
  CONSTRAINT WAITTIME_ARCHIVE_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, WAITTIME_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.WAITTIME_ARCHIVE ADD (
  CONSTRAINT WAITTIME_ARCHIVE_PERSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT WAITTIME_ARCHIVE_WAITTYPE_FK 
  FOREIGN KEY (WAITTYPE_ID) 
  REFERENCES ADR.WAITTYPE (WAITTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WTARCH_VCELIG_FK 
  FOREIGN KEY (VCELIGIBILITY_ID) 
  REFERENCES ADR.VCELIGIBILITY (VCELIGIBILITY_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WAITTIME_ARCHIVE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_ARCHIVE TO ADR_RW;

GRANT SELECT ON ADR.WAITTIME_ARCHIVE TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_ARCHIVE TO VETCHOICE_RW;
ALTER TABLE ADR.WAITTIME_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WAITTIME_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WAITTIME_H
(
  WAITTIME_H_ID          NUMBER(20)             NOT NULL,
  WAITTIME_ID            NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  WAITTYPE_ID            NUMBER(20)             NOT NULL,
  STD_INSTITUTION_ID     NUMBER(20)             NOT NULL,
  CLINIC_STOPCODE        VARCHAR2(6 BYTE),
  CLINIC_STOPCODE_NAME   VARCHAR2(30 BYTE),
  WAITLIST_START_DATE    DATE                   NOT NULL,
  DESIRED_APPT_DATE      DATE,
  DAYS_ELAPSED           NUMBER(4),
  RECORD_RESOLVED_IND    NUMBER(1),
  CLINIC_NAME            VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  VCELIGIBILITY_ID       NUMBER(20),
  IS_ACTIVE              CHAR(1 BYTE),
  TRANSACTION_TYPE_CODE  CHAR(1 BYTE)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION WT_RECREATDT_P1 VALUES LESS THAN (TO_DATE(' 2017-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION VALUES LESS THAN (TO_DATE(' 2017-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WAITTIME_H IS 'Current Wait-Time data';

COMMENT ON COLUMN ADR.WAITTIME_H.WAITTIME_H_ID IS 'The¿primary¿key, generated by the database system¿in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WAITTIME_H.WAITTIME_ID IS 'FK index referenced by waittime.waittime_id.';

COMMENT ON COLUMN ADR.WAITTIME_H.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.WAITTIME_H.WAITTYPE_ID IS 'FK to ADR.WAITTYPE table';

COMMENT ON COLUMN ADR.WAITTIME_H.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.WAITTIME_H.CLINIC_STOPCODE IS 'Clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_H.CLINIC_STOPCODE_NAME IS 'Name of the clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_H.WAITLIST_START_DATE IS 'Date Veteran was put on waitlist';

COMMENT ON COLUMN ADR.WAITTIME_H.DESIRED_APPT_DATE IS 'Veteran¿s desired appointment date';

COMMENT ON COLUMN ADR.WAITTIME_H.DAYS_ELAPSED IS 'Number of days Veteran has been waiting for appointment';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_RESOLVED_IND IS 'Indicator of whether or not the wait-time record was resolved';

COMMENT ON COLUMN ADR.WAITTIME_H.CLINIC_NAME IS 'The name of the clinic where the patient will be seen.';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WAITTIME_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';

COMMENT ON COLUMN ADR.WAITTIME_H.IS_ACTIVE IS 'indicator that WAITTIME record has been solved/completed';


CREATE INDEX ADR.WAITTIME_H_PERSONID_NU_I ON ADR.WAITTIME_H
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION WAITTIME_H_PERSONID_IDX_P1
    TABLESPACE L_INDEX_01,  
  PARTITION WAITTIME_H_PERSONID_IDX_P2
    TABLESPACE L_INDEX_01,  
  PARTITION WAITTIME_H_PERSONID_IDX_P3
    TABLESPACE L_INDEX_01,  
  PARTITION WAITTIME_H_PERSONID_IDX_P4
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE UNIQUE INDEX ADR.WAITTIME_H_PK_I ON ADR.WAITTIME_H
(WAITTIME_H_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WAITTIME_H_RECDTWTTIME_NU_I ON ADR.WAITTIME_H
(RECORD_CREATED_DATE, WAITTIME_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOCAL (  
  PARTITION WT_RECREATDT_P1
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               ),  
  PARTITION
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          128M
                NEXT             128M
                MAXSIZE          UNLIMITED
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.WAITTIME_H_VCELIGID_NU_I ON ADR.WAITTIME_H
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             128M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WAITTIME_H FOR ADR.WAITTIME_H;


ALTER TABLE ADR.WAITTIME_H ADD (
  CONSTRAINT WAITTIME_H_PK
  PRIMARY KEY
  (WAITTIME_H_ID)
  USING INDEX ADR.WAITTIME_H_PK_I
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WAITTIME_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_H TO ADR_RW;

GRANT SELECT ON ADR.WAITTIME_H TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_H TO VETCHOICE_RW;
ALTER TABLE ADR.WAITTIME_STAGE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WAITTIME_STAGE CASCADE CONSTRAINTS;

CREATE TABLE ADR.WAITTIME_STAGE
(
  WAITTIME_STAGE_ID      NUMBER(20)             NOT NULL,
  PERSON_ID              NUMBER(20)             NOT NULL,
  WAITTYPE_ID            NUMBER(20)             NOT NULL,
  STD_INSTITUTION_ID     NUMBER(20)             NOT NULL,
  CLINIC_STOPCODE        VARCHAR2(6 BYTE),
  CLINIC_STOPCODE_NAME   VARCHAR2(30 BYTE),
  WAITLIST_START_DATE    DATE                   NOT NULL,
  DESIRED_APPT_DATE      DATE,
  DAYS_ELAPSED           NUMBER(4),
  RECORD_RESOLVED_IND    NUMBER(1),
  CLINIC_NAME            VARCHAR2(30 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL,
  VCELIGIBILITY_ID       NUMBER(20)
)
COMPRESS FOR OLTP 
TABLESPACE L_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
PARTITION BY RANGE (RECORD_CREATED_DATE)
INTERVAL( NUMTOYMINTERVAL(1,'MONTH'))
(  
  PARTITION WAITTIME_STAGE_P001 VALUES LESS THAN (TO_DATE(' 2016-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
    LOGGING
    COMPRESS FOR OLTP 
    TABLESPACE L_DATA_01
    PCTFREE    10
    INITRANS   1
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WAITTIME_STAGE IS 'Current Wait-Time data';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.WAITTIME_STAGE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.PERSON_ID IS 'FK to ADR.PERSON table';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.WAITTYPE_ID IS 'FK to ADR.WAITTYPE table';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.STD_INSTITUTION_ID IS 'FK to ADR.STD_INSTITUTION table';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.CLINIC_STOPCODE IS 'Clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.CLINIC_STOPCODE_NAME IS 'Name of the clinic stopcode';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.WAITLIST_START_DATE IS 'Date Veteran was put on waitlist';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.DESIRED_APPT_DATE IS 'Veteran¿s desired appointment date';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.DAYS_ELAPSED IS 'Number of days Veteran has been waiting for appointment';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_RESOLVED_IND IS 'Indicator of whether or not the wait-time record was resolved';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.CLINIC_NAME IS 'The name of the clinic where the patient will be seen.';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WAITTIME_STAGE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WAITTIME_STAGE_PK ON ADR.WAITTIME_STAGE
(RECORD_CREATED_DATE, WAITTIME_STAGE_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
LOGGING
LOCAL (  
  PARTITION WAITTIME_STAGE_P001
    LOGGING
    NOCOMPRESS 
    TABLESPACE L_INDEX_01
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                MAXSIZE          UNLIMITED
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
               )
)
NOPARALLEL;

CREATE INDEX ADR.WTSTG_VCELIG_FK ON ADR.WAITTIME_STAGE
(VCELIGIBILITY_ID)
LOGGING
TABLESPACE L_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            MAXSIZE          UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WTS_PERSONID_IDX ON ADR.WAITTIME_STAGE
(PERSON_ID)
  TABLESPACE L_INDEX_01
  PCTFREE    10
  INITRANS   2
  MAXTRANS   255
  STORAGE    (
              BUFFER_POOL      DEFAULT
              FLASH_CACHE      DEFAULT
              CELL_FLASH_CACHE DEFAULT
             )
GLOBAL PARTITION BY HASH (PERSON_ID) (  
  PARTITION WTS_PERSONID_IDX_P001
    TABLESPACE L_INDEX_01,  
  PARTITION WTS_PERSONID_IDX_P002
    TABLESPACE L_INDEX_01,  
  PARTITION WTS_PERSONID_IDX_P003
    TABLESPACE L_INDEX_01,  
  PARTITION WTS_PERSONID_IDX_P004
    TABLESPACE L_INDEX_01
)
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WAITTIME_STAGE FOR ADR.WAITTIME_STAGE;


ALTER TABLE ADR.WAITTIME_STAGE ADD (
  CONSTRAINT WAITTIME_STAGE_PK
  PRIMARY KEY
  (RECORD_CREATED_DATE, WAITTIME_STAGE_ID)
  USING INDEX LOCAL
  ENABLE VALIDATE);

ALTER TABLE ADR.WAITTIME_STAGE ADD (
  CONSTRAINT WAITTIME_STAGE_PERSON_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT WAITTIME_STAGE_WAITTYPE_FK 
  FOREIGN KEY (WAITTYPE_ID) 
  REFERENCES ADR.WAITTYPE (WAITTYPE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WT_STAGE_FK 
  FOREIGN KEY (VCELIGIBILITY_ID) 
  REFERENCES ADR.VCELIGIBILITY (VCELIGIBILITY_ID)
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WAITTIME_STAGE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_STAGE TO ADR_RW;

GRANT SELECT ON ADR.WAITTIME_STAGE TO VETCHOICE_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTIME_STAGE TO VETCHOICE_RW;
ALTER TABLE ADR.WAITTYPE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WAITTYPE CASCADE CONSTRAINTS;

CREATE TABLE ADR.WAITTYPE
(
  WAITTYPE_ID  NUMBER(20)                       NOT NULL,
  CODE         VARCHAR2(3 BYTE)                 NOT NULL,
  NAME         VARCHAR2(30 BYTE)                NOT NULL,
  DESCRIPTION  VARCHAR2(250 BYTE),
  CREATED      DATE                             NOT NULL,
  UPDATED      DATE,
  CREATEDBY    VARCHAR2(32 BYTE)                NOT NULL,
  UPDATEDBY    VARCHAR2(32 BYTE)
)
TABLESPACE S_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WAITTYPE IS 'Lookup table used by VetChoice';

COMMENT ON COLUMN ADR.WAITTYPE.WAITTYPE_ID IS 'The¿primary¿key, generated¿by¿the¿database system¿in¿numeric¿form, used¿to¿uniquely¿identify a¿data¿record¿in¿this table.';

COMMENT ON COLUMN ADR.WAITTYPE.CODE IS 'Shortened code';

COMMENT ON COLUMN ADR.WAITTYPE.NAME IS 'User readable name';

COMMENT ON COLUMN ADR.WAITTYPE.DESCRIPTION IS 'Description of the wait type code';

COMMENT ON COLUMN ADR.WAITTYPE.CREATED IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WAITTYPE.UPDATED IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WAITTYPE.CREATEDBY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WAITTYPE.UPDATEDBY IS 'Account/user ID for this modification to the record.';


CREATE UNIQUE INDEX ADR.WAITTYPE_PK ON ADR.WAITTYPE
(WAITTYPE_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WAITTYPE FOR ADR.WAITTYPE;


ALTER TABLE ADR.WAITTYPE ADD (
  CONSTRAINT WAITTYPE_PK
  PRIMARY KEY
  (WAITTYPE_ID)
  USING INDEX ADR.WAITTYPE_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WAITTYPE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WAITTYPE TO ADR_RW;

GRANT SELECT ON ADR.WAITTYPE TO EEUSR WITH GRANT OPTION;

GRANT SELECT ON ADR.WAITTYPE TO VETCHOICE_R;
ALTER TABLE ADR.WKF_CASE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE
(
  WKF_CASE_ID               NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20),
  WKF_CASE_TYPE_ID          NUMBER(20)          NOT NULL,
  WKF_CASE_REQUEST_TYPE_ID  NUMBER(20),
  WKF_CASE_ISSUE_TYPE_ID    NUMBER(20),
  REQUEST_RECEIVED_SITE_ID  NUMBER(20),
  STD_USERGROUPTYPE_ID      NUMBER(20),
  REQUEST_RECEIVED_DATE     DATE,
  CASE_DUE_DATE             DATE,
  ERROR_MESSAGE_TEXT        VARCHAR2(250 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  BF_PROCESSED              NUMBER(1)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE IS 'Contains all the workflow records that are manual cases i.e. a business event that requires staff to perform work. Workflow Cases may be handled by the system automatically or may become a case that a staff member will work. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.WKF_CASE.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCASE_PK ON ADR.WKF_CASE
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_PRSN_FK_I ON ADR.WKF_CASE
(PERSON_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_STDINST_FK_I ON ADR.WKF_CASE
(REQUEST_RECEIVED_SITE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_STD_USRGRPTYP_FK_I ON ADR.WKF_CASE
(STD_USERGROUPTYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_STD_WKFCSTYPE_FK_I ON ADR.WKF_CASE
(WKF_CASE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_STD_WKFRQST_FK_I ON ADR.WKF_CASE
(WKF_CASE_REQUEST_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCASE_WKFISUTYPE_FK_I ON ADR.WKF_CASE
(WKF_CASE_ISSUE_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.WKF_CASE_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.WKF_CASE for each row
declare
    mDML char(1);
begin

  if DELETING then
       insert into WKF_CASE_H
    (

    WKF_CASE_H_ID,
    WKF_CASE_ID,
    PERSON_ID,
    WKF_CASE_TYPE_ID,
    WKF_CASE_REQUEST_TYPE_ID,
    WKF_CASE_ISSUE_TYPE_ID,
    REQUEST_RECEIVED_SITE_ID,
    REQUEST_RECEIVED_DATE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    TRANSACTION_TYPE_CODE,
    Bf_Processed
    )
    values
    (
    WKF_CASE_H_S.nextval,
    :old.WKF_CASE_ID,
    :old.PERSON_ID,
    :old.WKF_CASE_TYPE_ID,
    :old.WKF_CASE_REQUEST_TYPE_ID,
    :old.WKF_CASE_ISSUE_TYPE_ID,
    :old.REQUEST_RECEIVED_SITE_ID,
    :old.REQUEST_RECEIVED_DATE,
    :old.RECORD_CREATED_BY,
    :old.RECORD_CREATED_DATE,
    :old.RECORD_MODIFIED_BY,
    :old.RECORD_MODIFIED_DATE,
    :old.RECORD_MODIFIED_COUNT,
    'D',
    :old.Bf_Processed
    );
  else
    if UPDATING then
      mDML := 'U';
    else
      mDML := 'I';
    end if;

       insert into WKF_CASE_H
    (
    WKF_CASE_H_ID,
    WKF_CASE_ID,
    PERSON_ID,
    WKF_CASE_TYPE_ID,
    WKF_CASE_REQUEST_TYPE_ID,
    WKF_CASE_ISSUE_TYPE_ID,
    REQUEST_RECEIVED_SITE_ID,
    REQUEST_RECEIVED_DATE,
    RECORD_CREATED_BY,
    RECORD_CREATED_DATE,
    RECORD_MODIFIED_BY,
    RECORD_MODIFIED_DATE,
    RECORD_MODIFIED_COUNT,
    TRANSACTION_TYPE_CODE,
    Bf_Processed
    )
    values
    (
    WKF_CASE_H_S.nextval,
    :new.WKF_CASE_ID,
    :new.PERSON_ID,
    :new.WKF_CASE_TYPE_ID,
    :new.WKF_CASE_REQUEST_TYPE_ID,
    :new.WKF_CASE_ISSUE_TYPE_ID,
    :new.REQUEST_RECEIVED_SITE_ID,
    :new.REQUEST_RECEIVED_DATE,
    :new.RECORD_CREATED_BY,
    :new.RECORD_CREATED_DATE,
    :new.RECORD_MODIFIED_BY,
    :new.RECORD_MODIFIED_DATE,
    :new.RECORD_MODIFIED_COUNT,
    mDML,
    :new.Bf_Processed
    );
  end if;

End WKF_CASE_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE FOR ADR.WKF_CASE;


ALTER TABLE ADR.WKF_CASE ADD (
  CONSTRAINT WKFCASE_PK
  PRIMARY KEY
  (WKF_CASE_ID)
  USING INDEX ADR.WKFCASE_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.WKF_CASE ADD (
  CONSTRAINT WKFCASE_PRSN_FK 
  FOREIGN KEY (PERSON_ID) 
  REFERENCES ADR.PERSON (PERSON_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE NOVALIDATE,
  CONSTRAINT WKFCASE_STD_WKFCSTYPE_FK 
  FOREIGN KEY (WKF_CASE_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WKFCASE_STD_WKFRQST_FK 
  FOREIGN KEY (WKF_CASE_REQUEST_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASEREQUEST (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WKFCASE_WKFISUTYPE_FK 
  FOREIGN KEY (WKF_CASE_ISSUE_TYPE_ID) 
  REFERENCES ADRREF.STD_WKFISSUETYPE (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_CASE TO ADR_RW;

GRANT INSERT, SELECT, UPDATE ON ADR.WKF_CASE TO BIZFLOW_RW;
ALTER TABLE ADR.WKF_CASE_ASSIGNMENT
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_ASSIGNMENT CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_ASSIGNMENT
(
  WKF_CASE_ASSIGNMENT_ID    NUMBER(20)          NOT NULL,
  WKF_CASE_ID               NUMBER(20),
  WKF_CASE_ASSIGNED_BY      VARCHAR2(30 BYTE),
  WKF_CASE_ASSIGNED_TO      VARCHAR2(30 BYTE),
  WKF_CASE_ASSIGNMENT_DATE  DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_ASSIGNMENT IS 'The table stores data about the user assigned to a particular workflow case. Each Workflow case can be assigned or re-assigned to users in the system. This table keeps a record of all case assignments. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.WKF_CASE_ASSIGNMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCSASGMT_PK ON ADR.WKF_CASE_ASSIGNMENT
(WKF_CASE_ASSIGNMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCSASGMT_WKFCASE_FK_I ON ADR.WKF_CASE_ASSIGNMENT
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.WKF_CASE_ASSGN_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.WKF_CASE_ASSIGNMENT for each row
declare
   	mDML char(1);
begin

	if DELETING then
	     insert into WKF_CASE_ASSIGNMENT_H
		(
		WKF_CASE_ASSIGNMENT_H_ID,
		WKF_CASE_ASSIGNMENT_ID,
		WKF_CASE_ID,
		WKF_CASE_ASSIGNED_BY,
		WKF_CASE_ASSIGNED_TO,
		WKF_CASE_ASSIGNMENT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_ASSIGNMENT_H_S.nextval  ,
		:old.WKF_CASE_ASSIGNMENT_ID,
		:old.WKF_CASE_ID,
		:old.WKF_CASE_ASSIGNED_BY,
		:old.WKF_CASE_ASSIGNED_TO,
		:old.WKF_CASE_ASSIGNMENT_DATE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

	     insert into WKF_CASE_ASSIGNMENT_H
		(
		WKF_CASE_ASSIGNMENT_H_ID,
		WKF_CASE_ASSIGNMENT_ID,
		WKF_CASE_ID,
		WKF_CASE_ASSIGNED_BY,
		WKF_CASE_ASSIGNED_TO,
		WKF_CASE_ASSIGNMENT_DATE,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_ASSIGNMENT_H_S.nextval  ,
		:new.WKF_CASE_ASSIGNMENT_ID,
		:new.WKF_CASE_ID,
		:new.WKF_CASE_ASSIGNED_BY,
		:new.WKF_CASE_ASSIGNED_TO,
		:new.WKF_CASE_ASSIGNMENT_DATE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

End WKF_CASE_ASSGN_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_ASSIGNMENT FOR ADR.WKF_CASE_ASSIGNMENT;


ALTER TABLE ADR.WKF_CASE_ASSIGNMENT ADD (
  CONSTRAINT WKFCSASGMT_PK
  PRIMARY KEY
  (WKF_CASE_ASSIGNMENT_ID)
  USING INDEX ADR.WKFCSASGMT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.WKF_CASE_ASSIGNMENT ADD (
  CONSTRAINT WKFCSASGMT_WKFCASE_FK 
  FOREIGN KEY (WKF_CASE_ID) 
  REFERENCES ADR.WKF_CASE (WKF_CASE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_ASSIGNMENT TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_CASE_ASSIGNMENT TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_ASSIGNMENT_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_ASSIGNMENT_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_ASSIGNMENT_H
(
  WKF_CASE_ASSIGNMENT_H_ID  NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  WKF_CASE_ASSIGNMENT_ID    NUMBER(20)          NOT NULL,
  WKF_CASE_ID               NUMBER(20),
  WKF_CASE_ASSIGNED_BY      VARCHAR2(30 BYTE),
  WKF_CASE_ASSIGNED_TO      VARCHAR2(30 BYTE),
  WKF_CASE_ASSIGNMENT_DATE  DATE,
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_ASSIGNMENT_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.	All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.WKF_CASE_ASSIGNMENT_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.WKF_CASE_ASSIGNMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_ASSIGNMENT_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCSASGH_PK ON ADR.WKF_CASE_ASSIGNMENT_H
(WKF_CASE_ASSIGNMENT_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKF_CASE_ASSIGNMENT_H_FK_I ON ADR.WKF_CASE_ASSIGNMENT_H
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_ASSIGNMENT_H FOR ADR.WKF_CASE_ASSIGNMENT_H;


ALTER TABLE ADR.WKF_CASE_ASSIGNMENT_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_049
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT WKFCSASGH_PK
  PRIMARY KEY
  (WKF_CASE_ASSIGNMENT_H_ID)
  USING INDEX ADR.WKFCSASGH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_ASSIGNMENT_H TO ADR_R;

GRANT SELECT ON ADR.WKF_CASE_ASSIGNMENT_H TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_COMMENTS
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_COMMENTS CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_COMMENTS
(
  WKF_CASE_COMMENT_ID    NUMBER(20)             NOT NULL,
  WKF_CASE_ID            NUMBER(20),
  COMMENT_VALUE          VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_COMMENTS IS 'The table stores user remarks pertinent to a particular workflow case. Contains all the comment records for a case Freeform text which may be entered by the user to document pertinent information regarding the case. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.WKF_CASE_COMMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.COMMENT_VALUE IS 'Additional text info associated with the message event ';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCMTS_PK ON ADR.WKF_CASE_COMMENTS
(WKF_CASE_COMMENT_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCMTS_WKFCASE_FK_I ON ADR.WKF_CASE_COMMENTS
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.WKF_CASE_COMM_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.WKF_CASE_COMMENTS for each row
declare
   	mDML char(1);
begin

	if DELETING then
	     insert into WKF_CASE_Comments_H
		(
		WKF_CASE_COMMENT_H_ID,
		WKF_CASE_ID,
		WKF_CASE_COMMENT_ID,
		COMMENT_VALUE ,
		RECORD_CREATED_BY ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_COMMENTS_H_S.nextval  ,
		:old.WKF_CASE_ID,
		:old.WKF_CASE_COMMENT_ID,
		:old.COMMENT_VALUE,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

	     insert into WKF_CASE_Comments_H
		(
		WKF_CASE_COMMENT_H_ID,
		WKF_CASE_ID,
		WKF_CASE_COMMENT_ID,
		COMMENT_VALUE ,
		RECORD_CREATED_BY ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_COMMENTS_H_S.nextval  ,
		:new.WKF_CASE_ID,
		:new.WKF_CASE_COMMENT_ID,
		:new.COMMENT_VALUE,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

End WKF_CASE_COMM_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_COMMENTS FOR ADR.WKF_CASE_COMMENTS;


ALTER TABLE ADR.WKF_CASE_COMMENTS ADD (
  CONSTRAINT WKFCMTS_PK
  PRIMARY KEY
  (WKF_CASE_COMMENT_ID)
  USING INDEX ADR.WKFCMTS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.WKF_CASE_COMMENTS ADD (
  CONSTRAINT WKFCMTS_WKFCASE_FK 
  FOREIGN KEY (WKF_CASE_ID) 
  REFERENCES ADR.WKF_CASE (WKF_CASE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_COMMENTS TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_CASE_COMMENTS TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_COMMENTS_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_COMMENTS_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_COMMENTS_H
(
  WKF_CASE_COMMENT_H_ID  NUMBER(20)             NOT NULL,
  TRANSACTION_TYPE_CODE  VARCHAR2(1 BYTE)       NOT NULL,
  WKF_CASE_ID            NUMBER(20)             NOT NULL,
  WKF_CASE_COMMENT_ID    NUMBER(20)             NOT NULL,
  COMMENT_VALUE          VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY      VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_CREATED_DATE    DATE                   NOT NULL,
  RECORD_MODIFIED_BY     VARCHAR2(30 BYTE)      NOT NULL,
  RECORD_MODIFIED_DATE   DATE                   NOT NULL,
  RECORD_MODIFIED_COUNT  NUMBER(10)             NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_COMMENTS_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.WKF_CASE_COMMENT_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.WKF_CASE_COMMENT_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.COMMENT_VALUE IS 'Additional text info associated with the message event ';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_COMMENTS_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCMTSH_PK ON ADR.WKF_CASE_COMMENTS_H
(WKF_CASE_COMMENT_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKF_CASE_COMMENTS_H_FK_I ON ADR.WKF_CASE_COMMENTS_H
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_COMMENTS_H FOR ADR.WKF_CASE_COMMENTS_H;


ALTER TABLE ADR.WKF_CASE_COMMENTS_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_067
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT WKFCMTSH_PK
  PRIMARY KEY
  (WKF_CASE_COMMENT_H_ID)
  USING INDEX ADR.WKFCMTSH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_COMMENTS_H TO ADR_R;

GRANT SELECT ON ADR.WKF_CASE_COMMENTS_H TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_H
(
  WKF_CASE_H_ID             NUMBER(20)          NOT NULL,
  TRANSACTION_TYPE_CODE     VARCHAR2(1 BYTE)    NOT NULL,
  WKF_CASE_ID               NUMBER(20)          NOT NULL,
  PERSON_ID                 NUMBER(20),
  WKF_CASE_TYPE_ID          NUMBER(20),
  WKF_CASE_REQUEST_TYPE_ID  NUMBER(20),
  WKF_CASE_ISSUE_TYPE_ID    NUMBER(20),
  REQUEST_RECEIVED_SITE_ID  NUMBER(20),
  STD_USERGROUPTYPE_ID      NUMBER(20),
  REQUEST_RECEIVED_DATE     DATE,
  CASE_DUE_DATE             DATE,
  ERROR_MESSAGE_TEXT        VARCHAR2(250 BYTE),
  RECORD_CREATED_BY         VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_CREATED_DATE       DATE                NOT NULL,
  RECORD_MODIFIED_BY        VARCHAR2(30 BYTE)   NOT NULL,
  RECORD_MODIFIED_DATE      DATE                NOT NULL,
  RECORD_MODIFIED_COUNT     NUMBER(10)          NOT NULL,
  BF_PROCESSED              NUMBER(1)
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.WKF_CASE_H.WKF_CASE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.WKF_CASE_H.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_H.PERSON_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCASEH_PK ON ADR.WKF_CASE_H
(WKF_CASE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_H FOR ADR.WKF_CASE_H;


ALTER TABLE ADR.WKF_CASE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_068
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT WKFCASEH_PK
  PRIMARY KEY
  (WKF_CASE_H_ID)
  USING INDEX ADR.WKFCASEH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_H TO ADR_R;

GRANT SELECT ON ADR.WKF_CASE_H TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_STATUS_DETAIL
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_STATUS_DETAIL CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_STATUS_DETAIL
(
  WKF_CASE_STATUS_DETAIL_ID  NUMBER(20)         NOT NULL,
  WKF_CASE_ID                NUMBER(20),
  WKF_CASE_ACTIVITY_TYPE_ID  NUMBER(20),
  WKF_CASE_REQUEST_TYPE_ID   NUMBER(20),
  WKF_CASE_STATUS_TYPE_ID    NUMBER(20),
  RECORD_CREATED_BY          VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_CREATED_DATE        DATE               NOT NULL,
  RECORD_MODIFIED_BY         VARCHAR2(30 BYTE)  NOT NULL,
  RECORD_MODIFIED_DATE       DATE               NOT NULL,
  RECORD_MODIFIED_COUNT      NUMBER(10)         NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_STATUS_DETAIL IS 'The table stores status information used to mark progress toward completion for a particular workflow case. The status is the current phase of the case. The status value acts as a progress indicator as the case activities are completed. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.WKF_CASE_STATUS_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCSESTAT_PK ON ADR.WKF_CASE_STATUS_DETAIL
(WKF_CASE_STATUS_DETAIL_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCSESTAT_STD_WKFACTVTY_FK_I ON ADR.WKF_CASE_STATUS_DETAIL
(WKF_CASE_ACTIVITY_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCSESTAT_STD_WKFCSST_FK_I ON ADR.WKF_CASE_STATUS_DETAIL
(WKF_CASE_STATUS_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCSESTAT_STD_WKFRQST_FK_I ON ADR.WKF_CASE_STATUS_DETAIL
(WKF_CASE_REQUEST_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFCSESTAT_WKFCASE_FK_I ON ADR.WKF_CASE_STATUS_DETAIL
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.WKF_CASE_STA_DET_AUDIR_TRGR
after Update or Delete or Insert
ON ADR.WKF_CASE_STATUS_DETAIL for each row
declare
   	mDML char(1);
begin

	if DELETING then
	     insert into WKF_CASE_STATUS_DETAIL_H
		(
		WKF_CASE_STATUS_DETAIL_H_ID,
		WKF_CASE_STATUS_DETAIL_ID,
		WKF_CASE_ID,
		WKF_CASE_REQUEST_TYPE_ID ,
		WKF_CASE_STATUS_TYPE_ID ,
		WKF_CASE_ACTIVITY_TYPE_ID,
		RECORD_CREATED_BY ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_STATUS_DETAIL_H_S.nextval  ,
		:old.WKF_CASE_STATUS_DETAIL_ID,
		:old.WKF_CASE_ID,
		:old.WKF_CASE_REQUEST_TYPE_ID,
		:old.WKF_CASE_STATUS_TYPE_ID ,
		:old.WKF_CASE_ACTIVITY_TYPE_ID,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

	     insert into WKF_CASE_STATUS_DETAIL_H
		(
		WKF_CASE_STATUS_DETAIL_H_ID,
		WKF_CASE_STATUS_DETAIL_ID,
		WKF_CASE_ID,
		WKF_CASE_REQUEST_TYPE_ID ,
		WKF_CASE_STATUS_TYPE_ID ,
		WKF_CASE_ACTIVITY_TYPE_ID,
		RECORD_CREATED_BY ,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY ,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_CASE_STATUS_DETAIL_H_S.nextval  ,
		:new.WKF_CASE_STATUS_DETAIL_ID,
		:new.WKF_CASE_ID,
		:new.WKF_CASE_REQUEST_TYPE_ID,
		:new.WKF_CASE_STATUS_TYPE_ID ,
		:new.WKF_CASE_ACTIVITY_TYPE_ID,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;

End WKF_CASE_STA_det_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_STATUS_DETAIL FOR ADR.WKF_CASE_STATUS_DETAIL;


ALTER TABLE ADR.WKF_CASE_STATUS_DETAIL ADD (
  CONSTRAINT WKFCSESTAT_PK
  PRIMARY KEY
  (WKF_CASE_STATUS_DETAIL_ID)
  USING INDEX ADR.WKFCSESTAT_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.WKF_CASE_STATUS_DETAIL ADD (
  CONSTRAINT WKFCSESTAT_STD_WKFACTVTY_FK 
  FOREIGN KEY (WKF_CASE_ACTIVITY_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASEACTIVITY (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WKFCSESTAT_STD_WKFCSST_FK 
  FOREIGN KEY (WKF_CASE_STATUS_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASESTATUS (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WKFCSESTAT_STD_WKFRQST_FK 
  FOREIGN KEY (WKF_CASE_REQUEST_TYPE_ID) 
  REFERENCES ADRREF.STD_WORKFLOWCASEREQUEST (ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE,
  CONSTRAINT WKFCSESTAT_WKFCASE_FK 
  FOREIGN KEY (WKF_CASE_ID) 
  REFERENCES ADR.WKF_CASE (WKF_CASE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_STATUS_DETAIL TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_CASE_STATUS_DETAIL TO ADR_RW;
ALTER TABLE ADR.WKF_CASE_STATUS_DETAIL_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_CASE_STATUS_DETAIL_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_CASE_STATUS_DETAIL_H
(
  WKF_CASE_STATUS_DETAIL_H_ID  NUMBER(20)       NOT NULL,
  TRANSACTION_TYPE_CODE        VARCHAR2(1 BYTE) NOT NULL,
  WKF_CASE_STATUS_DETAIL_ID    NUMBER(20)       NOT NULL,
  WKF_CASE_ID                  NUMBER(20)       NOT NULL,
  WKF_CASE_REQUEST_TYPE_ID     NUMBER(20),
  WKF_CASE_STATUS_TYPE_ID      NUMBER(20),
  WKF_CASE_ACTIVITY_TYPE_ID    NUMBER(20),
  RECORD_CREATED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE          DATE             NOT NULL,
  RECORD_MODIFIED_BY           VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE         DATE             NOT NULL,
  RECORD_MODIFIED_COUNT        NUMBER(10)       NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_CASE_STATUS_DETAIL_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.WKF_CASE_STATUS_DETAIL_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.WKF_CASE_STATUS_DETAIL_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_CASE_STATUS_DETAIL_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFCSESTH_PK ON ADR.WKF_CASE_STATUS_DETAIL_H
(WKF_CASE_STATUS_DETAIL_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKF_CASE_STATUS_DETAIL_H_FK_I ON ADR.WKF_CASE_STATUS_DETAIL_H
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WKF_CASE_STATUS_DETAIL_H FOR ADR.WKF_CASE_STATUS_DETAIL_H;


ALTER TABLE ADR.WKF_CASE_STATUS_DETAIL_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_066
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT WKFCSESTH_PK
  PRIMARY KEY
  (WKF_CASE_STATUS_DETAIL_H_ID)
  USING INDEX ADR.WKFCSESTH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_CASE_STATUS_DETAIL_H TO ADR_R;

GRANT SELECT ON ADR.WKF_CASE_STATUS_DETAIL_H TO ADR_RW;
ALTER TABLE ADR.WKF_MESSAGING_CASE
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_MESSAGING_CASE CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_MESSAGING_CASE
(
  WKF_MESSAGING_CASE_ID         NUMBER(20)      NOT NULL,
  WKF_CASE_ID                   NUMBER(20)      NOT NULL,
  ACK_TYPE_ID                   NUMBER(20),
  HL7_MESSAGE_ID                VARCHAR2(50 BYTE),
  MESSAGE_TRANSMISSION_TYPE_ID  NUMBER(20),
  STD_INSTITUTION_ID            NUMBER(20),
  TRANSMISSION_TYPE_CODE        VARCHAR2(50 BYTE),
  TRANSMISSION_DATE             DATE,
  ACK_DATE                      DATE,
  ERROR_MESSAGE_TEXT            VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_MESSAGING_CASE IS 'Stores information for a case associated with or initiated by an HL7 message. This requires permanent retention (due to potential future adjudication) of information critical to the case. ENROLLMENT ELIGIBILITY';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.WKF_MESSAGING_CASE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.ACK_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.HL7_MESSAGE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.MESSAGE_TRANSMISSION_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.TRANSMISSION_TYPE_CODE IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.TRANSMISSION_DATE IS 'BR2216 The date/time the transmission was made to this site or received from this site.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.ACK_DATE IS 'BR2228 The ACK date/time is defined as the date and time HEC received the ACK on outbound messages or the date/time HEC sent the ack on inbound messages.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.ERROR_MESSAGE_TEXT IS 'Additional text info associated with the message event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFMSGCS_PK ON ADR.WKF_MESSAGING_CASE
(WKF_MESSAGING_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFMSGCS_STDINST_FK_I ON ADR.WKF_MESSAGING_CASE
(STD_INSTITUTION_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFMSGCS_STD_MSGTYP_FK_2_I ON ADR.WKF_MESSAGING_CASE
(ACK_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFMSGCS_STD_MSGTYP_FK_I ON ADR.WKF_MESSAGING_CASE
(MESSAGE_TRANSMISSION_TYPE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKFMSGCS_WKFCASE_FK_I ON ADR.WKF_MESSAGING_CASE
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE TRIGGER ADR.wkf_msg_case_audir_trgr
after Update or Delete or Insert
ON ADR.WKF_MESSAGING_CASE
for each row
declare
   	mDML char(1);
begin

	if DELETING then
		insert into WKF_MESSAGING_CASE_H
		(
		WKF_MESSAGING_CASE_H_ID,
		WKF_MESSAGING_CASE_ID,
		HL7_MESSAGE_ID,
		WKF_CASE_ID,
		MESSAGE_TRANSMISSION_TYPE_ID,
		ACK_TYPE_ID,
		STD_INSTITUTION_ID,
		TRANSMISSION_TYPE_CODE,
		TRANSMISSION_DATE,
		ACK_DATE,
		ERROR_MESSAGE_TEXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_MESSAGING_CASE_H_S.nextval,
		:old.WKF_MESSAGING_CASE_ID,
		:old.HL7_MESSAGE_ID,
		:old.WKF_CASE_ID,
		:old.MESSAGE_TRANSMISSION_TYPE_ID,
		:old.ACK_TYPE_ID,
		:old.STD_INSTITUTION_ID,
		:old.TRANSMISSION_TYPE_CODE,
		:old.TRANSMISSION_DATE,
		:old.ACK_DATE,
		:old.ERROR_MESSAGE_TEXT,
		:old.RECORD_CREATED_BY,
		:old.RECORD_CREATED_DATE,
		:old.RECORD_MODIFIED_BY,
		:old.RECORD_MODIFIED_DATE,
		:old.RECORD_MODIFIED_COUNT,
		'D'
		);
	else
		if UPDATING then
			mDML := 'U';
		else
			mDML := 'I';
		end if;

		insert into WKF_MESSAGING_CASE_H
		(
		WKF_MESSAGING_CASE_H_ID,
		WKF_MESSAGING_CASE_ID,
		HL7_MESSAGE_ID,
		WKF_CASE_ID,
		MESSAGE_TRANSMISSION_TYPE_ID,
		ACK_TYPE_ID,
		STD_INSTITUTION_ID,
		TRANSMISSION_TYPE_CODE,
		TRANSMISSION_DATE,
		ACK_DATE,
		ERROR_MESSAGE_TEXT,
		RECORD_CREATED_BY,
		RECORD_CREATED_DATE,
		RECORD_MODIFIED_BY,
		RECORD_MODIFIED_DATE,
		RECORD_MODIFIED_COUNT,
		TRANSACTION_TYPE_CODE
		)
		values
		(
		WKF_MESSAGING_CASE_H_S.nextval,
		:new.WKF_MESSAGING_CASE_ID,
		:new.HL7_MESSAGE_ID,
		:new.WKF_CASE_ID,
		:new.MESSAGE_TRANSMISSION_TYPE_ID,
		:new.ACK_TYPE_ID,
		:new.STD_INSTITUTION_ID,
		:new.TRANSMISSION_TYPE_CODE,
		:new.TRANSMISSION_DATE,
		:new.ACK_DATE,
		:new.ERROR_MESSAGE_TEXT,
		:new.RECORD_CREATED_BY,
		:new.RECORD_CREATED_DATE,
		:new.RECORD_MODIFIED_BY,
		:new.RECORD_MODIFIED_DATE,
		:new.RECORD_MODIFIED_COUNT,
		mDML
		);
	end if;
end wkf_msg_case_audir_trgr;
/


CREATE OR REPLACE PUBLIC SYNONYM WKF_MESSAGING_CASE FOR ADR.WKF_MESSAGING_CASE;


ALTER TABLE ADR.WKF_MESSAGING_CASE ADD (
  CONSTRAINT WKFMSGCS_PK
  PRIMARY KEY
  (WKF_MESSAGING_CASE_ID)
  USING INDEX ADR.WKFMSGCS_PK
  ENABLE VALIDATE);

ALTER TABLE ADR.WKF_MESSAGING_CASE ADD (
  CONSTRAINT WKFMSGCS_WKFCASE_FK 
  FOREIGN KEY (WKF_CASE_ID) 
  REFERENCES ADR.WKF_CASE (WKF_CASE_ID)
  DEFERRABLE INITIALLY DEFERRED
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_MESSAGING_CASE TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_MESSAGING_CASE TO ADR_RW;
ALTER TABLE ADR.WKF_MESSAGING_CASE_H
 DROP PRIMARY KEY CASCADE;

DROP TABLE ADR.WKF_MESSAGING_CASE_H CASCADE CONSTRAINTS;

CREATE TABLE ADR.WKF_MESSAGING_CASE_H
(
  WKF_MESSAGING_CASE_H_ID       NUMBER(20)      NOT NULL,
  TRANSACTION_TYPE_CODE         VARCHAR2(1 BYTE) NOT NULL,
  WKF_MESSAGING_CASE_ID         NUMBER(20)      NOT NULL,
  HL7_MESSAGE_ID                VARCHAR2(50 BYTE),
  WKF_CASE_ID                   VARCHAR2(50 BYTE),
  MESSAGE_TRANSMISSION_TYPE_ID  VARCHAR2(50 BYTE),
  ACK_TYPE_ID                   VARCHAR2(50 BYTE),
  STD_INSTITUTION_ID            VARCHAR2(50 BYTE),
  TRANSMISSION_TYPE_CODE        VARCHAR2(50 BYTE),
  TRANSMISSION_DATE             DATE,
  ACK_DATE                      DATE,
  ERROR_MESSAGE_TEXT            VARCHAR2(4000 BYTE),
  RECORD_CREATED_BY             VARCHAR2(30 BYTE) NOT NULL,
  RECORD_CREATED_DATE           DATE            NOT NULL,
  RECORD_MODIFIED_BY            VARCHAR2(30 BYTE) NOT NULL,
  RECORD_MODIFIED_DATE          DATE            NOT NULL,
  RECORD_MODIFIED_COUNT         NUMBER(10)      NOT NULL
)
TABLESPACE M_DATA_01
RESULT_CACHE (MODE DEFAULT)
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;

COMMENT ON TABLE ADR.WKF_MESSAGING_CASE_H IS 'The Standard History table name appends _H to the end of the table name and adds two columns, the primary key column appends _H_ID to the table name and a column named TRANSACTION_TYPE_CODE containing a code defining what was done to the row.  All other columns contain a copy of the data row before the change.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.WKF_MESSAGING_CASE_H_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.TRANSACTION_TYPE_CODE IS 'Code for the action performed on the record in the base table (followed by it being copied into this history table). I for insert, U for update and D for delete.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.WKF_MESSAGING_CASE_ID IS 'The primary key, generated by the database system in numeric form, used to uniquely identify a data record in this table.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.HL7_MESSAGE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.WKF_CASE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.MESSAGE_TRANSMISSION_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.ACK_TYPE_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.STD_INSTITUTION_ID IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.TRANSMISSION_TYPE_CODE IS 'Acknowledgement message type associated with the event ';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.TRANSMISSION_DATE IS 'BR2216 The date/time the transmission was made to this site or received from this site.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.ACK_DATE IS 'BR2228 The ACK date/time is defined as the date and time HEC received the ACK on outbound messages or the date/time HEC sent the ack on inbound messages.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.RECORD_CREATED_BY IS 'Account/user ID that created the record';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.RECORD_CREATED_DATE IS 'Date the record was created.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.RECORD_MODIFIED_BY IS 'Account/user ID for this modification to the record';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.RECORD_MODIFIED_DATE IS 'Date of this modification to the record.';

COMMENT ON COLUMN ADR.WKF_MESSAGING_CASE_H.RECORD_MODIFIED_COUNT IS 'Number of times this record has been modified with zero indicating the initial creation, 1...n.';


CREATE UNIQUE INDEX ADR.WKFMSGCSH_PK ON ADR.WKF_MESSAGING_CASE_H
(WKF_MESSAGING_CASE_H_ID)
LOGGING
TABLESPACE S_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128K
            NEXT             128K
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE INDEX ADR.WKF_MESSAGING_CASE_H_FK_I ON ADR.WKF_MESSAGING_CASE_H
(WKF_CASE_ID)
LOGGING
TABLESPACE M_INDEX_01
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          4M
            NEXT             4M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

CREATE OR REPLACE PUBLIC SYNONYM WKF_MESSAGING_CASE_H FOR ADR.WKF_MESSAGING_CASE_H;


ALTER TABLE ADR.WKF_MESSAGING_CASE_H ADD (
  CONSTRAINT AVCON_1172521913_TRANS_000
  CHECK (TRANSACTION_TYPE_CODE IN ('U', 'I', 'D'))
  ENABLE VALIDATE,
  CONSTRAINT WKFMSGCSH_PK
  PRIMARY KEY
  (WKF_MESSAGING_CASE_H_ID)
  USING INDEX ADR.WKFMSGCSH_PK
  ENABLE VALIDATE);

GRANT SELECT ON ADR.WKF_MESSAGING_CASE_H TO ADR_R;

GRANT DELETE, INSERT, SELECT, UPDATE ON ADR.WKF_MESSAGING_CASE_H TO ADR_RW;
