-- Copyright (C) 2001,2002 topIT Informationstechnologie GmbH
-- Copyright (C) 2003-2014 independIT Integrative Technologies GmbH

CREATE TABLE SCHEDULING_ENTITY (
    ID                             bigint NOT NULL
    , NAME                           varchar(64)     NOT NULL
    , FOLDER_ID                      bigint          NOT NULL
    , OWNER_ID                       bigint          NOT NULL
    , TYPE                           integer         NOT NULL
    , RUN_PROGRAM                    varchar(512)        NULL
    , RERUN_PROGRAM                  varchar(512)        NULL
    , KILL_PROGRAM                   varchar(512)        NULL
    , WORKDIR                        varchar(512)        NULL
    , LOGFILE                        varchar(512)        NULL
    , TRUNC_LOG                      integer             NULL
    , ERRLOGFILE                     varchar(512)        NULL
    , TRUNC_ERRLOG                   integer             NULL
    , EXPECTED_RUNTIME               integer             NULL
    , EXPECTED_FINALTIME             integer             NULL
    , GET_EXPECTED_RUNTIME           varchar(32)         NULL
    , PRIORITY                       integer         NOT NULL
    , MIN_PRIORITY                   integer             NULL
    , AGING_AMOUNT                   integer             NULL
    , AGING_BASE                     integer             NULL
    , SUBMIT_SUSPENDED               integer         NOT NULL
    , RESUME_AT                      varchar(20)         NULL
    , RESUME_IN                      integer             NULL
    , RESUME_BASE                    integer             NULL
    , MASTER_SUBMITTABLE             integer         NOT NULL
    , TIMEOUT_AMOUNT                 integer             NULL
    , TIMEOUT_BASE                   integer             NULL
    , TIMEOUT_STATE_ID               bigint              NULL
    , SAME_NODE                      integer             NULL
    , GANG_SCHEDULE                  integer             NULL
    , DEPENDENCY_OPERATION           integer         NOT NULL
    , ESMP_ID                        bigint              NULL
    , ESP_ID                         bigint              NULL
    , QA_ID                          bigint              NULL
    , NE_ID                          bigint              NULL
    , FP_ID                          bigint              NULL
    , CANCEL_LEAD_FLAG               integer         NOT NULL
    , CANCEL_APPROVAL                integer         NOT NULL
    , RERUN_LEAD_FLAG                integer         NOT NULL
    , RERUN_APPROVAL                 integer         NOT NULL
    , ENABLE_LEAD_FLAG               integer         NOT NULL
    , ENABLE_APPROVAL                integer         NOT NULL
    , SET_STATE_LEAD_FLAG            integer         NOT NULL
    , SET_STATE_APPROVAL             integer         NOT NULL
    , IGN_DEP_LEAD_FLAG              integer         NOT NULL
    , IGN_DEP_APPROVAL               integer         NOT NULL
    , IGN_RSS_LEAD_FLAG              integer         NOT NULL
    , IGN_RSS_APPROVAL               integer         NOT NULL
    , CLONE_LEAD_FLAG                integer         NOT NULL
    , CLONE_APPROVAL                 integer         NOT NULL
    , EDIT_PARM_LEAD_FLAG            integer         NOT NULL
    , EDIT_PARM_APPROVAL             integer         NOT NULL
    , KILL_LEAD_FLAG                 integer         NOT NULL
    , KILL_APPROVAL                  integer         NOT NULL
    , SET_JOB_STATE_LEAD_FLAG        integer         NOT NULL
    , SET_JOB_STATE_APPROVAL         integer         NOT NULL
    , INHERIT_PRIVS                  bigint          NOT NULL
    , CREATOR_U_ID                   bigint          NOT NULL
    , CREATE_TS                      bigint          NOT NULL
    , CHANGER_U_ID                   bigint          NOT NULL
    , CHANGE_TS                      bigint          NOT NULL
    , VALID_FROM                   bigint NOT NULL
    , VALID_TO                     bigint NOT NULL
);
CREATE INDEX PK_SCHEDULING_ENTITY
ON SCHEDULING_ENTITY(ID);
CREATE VIEW SCI_C_SCHEDULING_ENTITY AS
SELECT 
    ID 
    , NAME                           AS NAME
    , FOLDER_ID                      AS FOLDER_ID
    , OWNER_ID                       AS OWNER_ID
    , DECODE (TYPE, 1, 'JOB', 2, 'BATCH', 3, 'MILESTONE') AS TYPE
    , RUN_PROGRAM                    AS RUN_PROGRAM
    , RERUN_PROGRAM                  AS RERUN_PROGRAM
    , KILL_PROGRAM                   AS KILL_PROGRAM
    , WORKDIR                        AS WORKDIR
    , LOGFILE                        AS LOGFILE
    , DECODE (TRUNC_LOG, 0, 'NOTRUNC', 1, 'TRUNC') AS TRUNC_LOG
    , ERRLOGFILE                     AS ERRLOGFILE
    , DECODE (TRUNC_ERRLOG, 0, 'NOTRUNC', 1, 'TRUNC') AS TRUNC_ERRLOG
    , EXPECTED_RUNTIME               AS EXPECTED_RUNTIME
    , EXPECTED_FINALTIME             AS EXPECTED_FINALTIME
    , GET_EXPECTED_RUNTIME           AS GET_EXPECTED_RUNTIME
    , PRIORITY                       AS PRIORITY
    , MIN_PRIORITY                   AS MIN_PRIORITY
    , AGING_AMOUNT                   AS AGING_AMOUNT
    , DECODE (AGING_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS AGING_BASE
    , DECODE (SUBMIT_SUSPENDED, 1, 'SUSPEND', 0, 'NOSUSPEND') AS SUBMIT_SUSPENDED
    , RESUME_AT                      AS RESUME_AT
    , RESUME_IN                      AS RESUME_IN
    , DECODE (RESUME_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS RESUME_BASE
    , DECODE (MASTER_SUBMITTABLE, 1, 'MASTER', 0, 'NOMASTER') AS MASTER_SUBMITTABLE
    , TIMEOUT_AMOUNT                 AS TIMEOUT_AMOUNT
    , DECODE (TIMEOUT_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS TIMEOUT_BASE
    , TIMEOUT_STATE_ID               AS TIMEOUT_STATE_ID
    , DECODE (DEPENDENCY_OPERATION, 1, 'AND', 2, 'OR') AS DEPENDENCY_OPERATION
    , ESMP_ID                        AS ESMP_ID
    , ESP_ID                         AS ESP_ID
    , NE_ID                          AS NE_ID
    , FP_ID                          AS FP_ID
    , DECODE (CANCEL_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS CANCEL_LEAD_FLAG
    , DECODE (CANCEL_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS CANCEL_APPROVAL
    , DECODE (RERUN_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS RERUN_LEAD_FLAG
    , DECODE (RERUN_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS RERUN_APPROVAL
    , DECODE (ENABLE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS ENABLE_LEAD_FLAG
    , DECODE (ENABLE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS ENABLE_APPROVAL
    , DECODE (SET_STATE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS SET_STATE_LEAD_FLAG
    , DECODE (SET_STATE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS SET_STATE_APPROVAL
    , DECODE (IGN_DEP_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS IGN_DEP_LEAD_FLAG
    , DECODE (IGN_DEP_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS IGN_DEP_APPROVAL
    , DECODE (IGN_RSS_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS IGN_RSS_LEAD_FLAG
    , DECODE (IGN_RSS_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS IGN_RSS_APPROVAL
    , DECODE (CLONE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS CLONE_LEAD_FLAG
    , DECODE (CLONE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS CLONE_APPROVAL
    , DECODE (EDIT_PARM_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS EDIT_PARM_LEAD_FLAG
    , DECODE (EDIT_PARM_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS EDIT_PARM_APPROVAL
    , DECODE (KILL_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS KILL_LEAD_FLAG
    , DECODE (KILL_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS KILL_APPROVAL
    , DECODE (SET_JOB_STATE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS SET_JOB_STATE_LEAD_FLAG
    , DECODE (SET_JOB_STATE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS SET_JOB_STATE_APPROVAL
    , INHERIT_PRIVS                  AS INHERIT_PRIVS
    , CREATOR_U_ID                   AS CREATOR_U_ID
    , timestamp('1970-01-01-00.00.00') + int(mod(CREATE_TS, 1125899906842624)/1000) seconds AS CREATE_TS
    , CHANGER_U_ID                   AS CHANGER_U_ID
    , timestamp('1970-01-01-00.00.00') + int(mod(CHANGE_TS, 1125899906842624)/1000) seconds AS CHANGE_TS
  FROM SCHEDULING_ENTITY
 WHERE VALID_TO = 9223372036854775807;
CREATE VIEW SCI_V_SCHEDULING_ENTITY AS
SELECT 
    ID 
    , NAME                           AS NAME
    , FOLDER_ID                      AS FOLDER_ID
    , OWNER_ID                       AS OWNER_ID
    , DECODE (TYPE, 1, 'JOB', 2, 'BATCH', 3, 'MILESTONE') AS TYPE
    , RUN_PROGRAM                    AS RUN_PROGRAM
    , RERUN_PROGRAM                  AS RERUN_PROGRAM
    , KILL_PROGRAM                   AS KILL_PROGRAM
    , WORKDIR                        AS WORKDIR
    , LOGFILE                        AS LOGFILE
    , DECODE (TRUNC_LOG, 0, 'NOTRUNC', 1, 'TRUNC') AS TRUNC_LOG
    , ERRLOGFILE                     AS ERRLOGFILE
    , DECODE (TRUNC_ERRLOG, 0, 'NOTRUNC', 1, 'TRUNC') AS TRUNC_ERRLOG
    , EXPECTED_RUNTIME               AS EXPECTED_RUNTIME
    , EXPECTED_FINALTIME             AS EXPECTED_FINALTIME
    , GET_EXPECTED_RUNTIME           AS GET_EXPECTED_RUNTIME
    , PRIORITY                       AS PRIORITY
    , MIN_PRIORITY                   AS MIN_PRIORITY
    , AGING_AMOUNT                   AS AGING_AMOUNT
    , DECODE (AGING_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS AGING_BASE
    , DECODE (SUBMIT_SUSPENDED, 1, 'SUSPEND', 0, 'NOSUSPEND') AS SUBMIT_SUSPENDED
    , RESUME_AT                      AS RESUME_AT
    , RESUME_IN                      AS RESUME_IN
    , DECODE (RESUME_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS RESUME_BASE
    , DECODE (MASTER_SUBMITTABLE, 1, 'MASTER', 0, 'NOMASTER') AS MASTER_SUBMITTABLE
    , TIMEOUT_AMOUNT                 AS TIMEOUT_AMOUNT
    , DECODE (TIMEOUT_BASE, 0, 'MINUTE', 1, 'HOUR', 2, 'DAY', 3, 'WEEK', 4, 'MONTH', 5, 'YEAR') AS TIMEOUT_BASE
    , TIMEOUT_STATE_ID               AS TIMEOUT_STATE_ID
    , DECODE (DEPENDENCY_OPERATION, 1, 'AND', 2, 'OR') AS DEPENDENCY_OPERATION
    , ESMP_ID                        AS ESMP_ID
    , ESP_ID                         AS ESP_ID
    , NE_ID                          AS NE_ID
    , FP_ID                          AS FP_ID
    , DECODE (CANCEL_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS CANCEL_LEAD_FLAG
    , DECODE (CANCEL_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS CANCEL_APPROVAL
    , DECODE (RERUN_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS RERUN_LEAD_FLAG
    , DECODE (RERUN_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS RERUN_APPROVAL
    , DECODE (ENABLE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS ENABLE_LEAD_FLAG
    , DECODE (ENABLE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS ENABLE_APPROVAL
    , DECODE (SET_STATE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS SET_STATE_LEAD_FLAG
    , DECODE (SET_STATE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS SET_STATE_APPROVAL
    , DECODE (IGN_DEP_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS IGN_DEP_LEAD_FLAG
    , DECODE (IGN_DEP_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS IGN_DEP_APPROVAL
    , DECODE (IGN_RSS_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS IGN_RSS_LEAD_FLAG
    , DECODE (IGN_RSS_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS IGN_RSS_APPROVAL
    , DECODE (CLONE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS CLONE_LEAD_FLAG
    , DECODE (CLONE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS CLONE_APPROVAL
    , DECODE (EDIT_PARM_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS EDIT_PARM_LEAD_FLAG
    , DECODE (EDIT_PARM_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS EDIT_PARM_APPROVAL
    , DECODE (KILL_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS KILL_LEAD_FLAG
    , DECODE (KILL_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS KILL_APPROVAL
    , DECODE (SET_JOB_STATE_LEAD_FLAG, 1, 'TRUE', 0, 'FALSE') AS SET_JOB_STATE_LEAD_FLAG
    , DECODE (SET_JOB_STATE_APPROVAL, 0, 'DEFAULT', 1, 'PARENT', 2, 'NO', 5, 'APPROVE', 4, 'REVIEW') AS SET_JOB_STATE_APPROVAL
    , INHERIT_PRIVS                  AS INHERIT_PRIVS
    , CREATOR_U_ID                   AS CREATOR_U_ID
    , timestamp('1970-01-01-00.00.00') + int(mod(CREATE_TS, 1125899906842624)/1000) seconds AS CREATE_TS
    , CHANGER_U_ID                   AS CHANGER_U_ID
    , timestamp('1970-01-01-00.00.00') + int(mod(CHANGE_TS, 1125899906842624)/1000) seconds AS CHANGE_TS
    , VALID_FROM
    , VALID_TO
  FROM SCHEDULING_ENTITY;
