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

CREATE TABLE `RESOURCE_ALLOCATION` (
    `ID`                           decimal(20) NOT NULL
    , `R_ID`                         decimal(20)     NOT NULL
    , `SME_ID`                       decimal(20)     NOT NULL
    , `NR_ID`                        decimal(20)     NOT NULL
    , `AMOUNT`                       integer             NULL
    , `ORIG_AMOUNT`                  integer             NULL
    , `KEEP_MODE`                    integer         NOT NULL
    , `IS_STICKY`                    integer         NOT NULL
    , `STICKY_NAME`                  varchar(64)         NULL
    , `STICKY_PARENT`                decimal(20)         NULL
    , `ALLOCATION_TYPE`              integer         NOT NULL
    , `RSMP_ID`                      decimal(20)         NULL
    , `LOCKMODE`                     integer             NULL
    , `REFCOUNT`                     integer         NOT NULL
    , `CREATOR_U_ID`                 decimal(20)     NOT NULL
    , `CREATE_TS`                    decimal(20)     NOT NULL
    , `CHANGER_U_ID`                 decimal(20)     NOT NULL
    , `CHANGE_TS`                    decimal(20)     NOT NULL
) ENGINE = INNODB;
CREATE UNIQUE INDEX PK_RESOURCE_ALLOCATION
ON `RESOURCE_ALLOCATION`(`ID`);
CREATE VIEW SCI_RESOURCE_ALLOCATION AS
SELECT 
    ID 
    , `R_ID`                         AS `R_ID`
    , `SME_ID`                       AS `SME_ID`
    , `NR_ID`                        AS `NR_ID`
    , `AMOUNT`                       AS `AMOUNT`
    , `ORIG_AMOUNT`                  AS `ORIG_AMOUNT`
    , CASE `KEEP_MODE` WHEN 0 THEN 'NOKEEP' WHEN 1 THEN 'KEEP' WHEN 2 THEN 'KEEP_FINAL' END AS `KEEP_MODE`
    , CASE `IS_STICKY` WHEN 1 THEN 'TRUE' WHEN 0 THEN 'FALSE' END AS `IS_STICKY`
    , `STICKY_NAME`                  AS `STICKY_NAME`
    , `STICKY_PARENT`                AS `STICKY_PARENT`
    , CASE `ALLOCATION_TYPE` WHEN 1 THEN 'REQUEST' WHEN 6 THEN 'MASTER_REQUEST' WHEN 2 THEN 'RESERVATION' WHEN 3 THEN 'MASTER_RESERVATION' WHEN 4 THEN 'ALLOCATION' WHEN 5 THEN 'IGNORE' END AS `ALLOCATION_TYPE`
    , `RSMP_ID`                      AS `RSMP_ID`
    , CASE `LOCKMODE` WHEN 255 THEN 'N' WHEN 0 THEN 'X' WHEN 2 THEN 'SX' WHEN 4 THEN 'S' WHEN 6 THEN 'SC' END AS `LOCKMODE`
    , `REFCOUNT`                     AS `REFCOUNT`
    , `CREATOR_U_ID`                 AS `CREATOR_U_ID`
    , from_unixtime((`CREATE_TS` & ~1125899906842624)/1000) AS `CREATE_TS`
    , `CHANGER_U_ID`                 AS `CHANGER_U_ID`
    , from_unixtime((`CHANGE_TS` & ~1125899906842624)/1000) AS `CHANGE_TS`
  FROM `RESOURCE_ALLOCATION`;
