-- Licensed Materials - Property of IBM
-- 5724-I68
--
-- (C) Copyright IBM Corp. 2000, 2006
--
-- US Government Users Restricted Rights - Use, duplication, or
-- disclosure restricted by GSA ADP Schedule Contract with IBM Corp.



update command options using c off;

-- ===================================================================
--
-- Table twfl_std_step_def
--
-- step definition.
-- ===================================================================

drop table twfl_std_step_def;
create table twfl_std_step_def
( 
    std_cmp_id                    bigint,
    std_wfl_id                    bigint,
    std_id                        bigint,
    std_name                      varchar(200),
    std_desc                      varchar(3000),
    std_type                      varchar(100),
    std_script_path               varchar(1000),
    std_deadline_date             timestamp,
    std_deadline_duration         bigint,
    std_entry_notification        varchar(3000),
    std_deadline_notification     varchar(3000),
    std_allow_add_entries         char(1),
    std_recategorize              char(1),
    std_allow_reserve_entries     char(1),
    std_view_id                   bigint
) 

in users index in indx;


drop synonym std;
create synonym std for twfl_std_step_def;

drop sequence qwfl_std_id restrict;
create sequence qwfl_std_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_stl_step_loc_hierarchy
--
-- step location hierarchy
-- ===================================================================

drop table twfl_stl_step_loc_hierarchy;
create table twfl_stl_step_loc_hierarchy
( 
    stl_cmp_id                    bigint,
    stl_step_id                   bigint,
    stl_loc_hierarchy_id          bigint,
    stl_modify_availability       char(1)
) 

in users index in indx;


drop synonym stl;
create synonym stl for twfl_stl_step_loc_hierarchy;

-- ===================================================================
--
-- Table twfl_stp_step_performer
--
-- step performers
-- ===================================================================

drop table twfl_stp_step_performer;
create table twfl_stp_step_performer
( 
    stp_cmp_id                    bigint,
    stp_step_id                   bigint,
    stp_role_id                   bigint,
    stp_user_id                   bigint
) 

in users index in indx;


drop synonym stp;
create synonym stp for twfl_stp_step_performer;

-- ===================================================================
--
-- Table twfl_ste_step_exit_value
--
-- step exit values for each entry exiting a step.
-- ===================================================================

drop table twfl_ste_step_exit_value;
create table twfl_ste_step_exit_value
( 
    ste_cmp_id                    bigint,
    ste_step_id                   bigint,
    ste_exit_value                varchar(200)
) 

in users index in indx;


drop synonym ste;
create synonym ste for twfl_ste_step_exit_value;

-- ===================================================================
--
-- Table twfl_stg_step_attr_group
--
-- step attribute groups ids.
-- Attribute Groups on which a step is supposed to apply.
-- ===================================================================

drop table twfl_stg_step_attr_group;
create table twfl_stg_step_attr_group
( 
    stg_cmp_id                    bigint,
    stg_step_id                   bigint,
    stg_attr_group_id             bigint,
    stg_viewable_flag             char(1)
) 

in users index in indx;


drop synonym stg;
create synonym stg for twfl_stg_step_attr_group;

-- ===================================================================
--
-- Table twfl_wfd_workflow_def
--
-- workflow definition.
-- just basic info.
-- the real structure is in the workflow graph
-- gui_available tells you whether a gui is present for this wfl or not
-- T -> true
-- F -> false
-- ===================================================================

drop table twfl_wfd_workflow_def;
create table twfl_wfd_workflow_def
( 
    wfd_cmp_id                    bigint,
    wfd_id                        bigint,
    wfd_name                      varchar(200),
    wfd_desc                      varchar(3000),
    wfd_container_type            varchar(200),
    wfd_gui_available             char,
    wfd_object_group_id           bigint
) 

in users index in indx;


drop synonym wfd;
create synonym wfd for twfl_wfd_workflow_def;

drop sequence qwfl_wfd_id restrict;
create sequence qwfl_wfd_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_wfg_workflow_graph
--
-- workflow graph.
-- defines the destination step for each exit value of the source step,
-- ===================================================================

drop table twfl_wfg_workflow_graph;
create table twfl_wfg_workflow_graph
( 
    wfg_cmp_id                    bigint,
    wfg_wfl_id                    bigint,
    wfg_src_step_id               bigint,
    wfg_exit_value                varchar(200),
    wfg_dst_step_id               bigint
) 

in users index in indx;


drop synonym wfg;
create synonym wfg for twfl_wfg_workflow_graph;

-- ===================================================================
--
-- Table twfl_spt_step_path
--
-- Step path and workflow hierarchy information.
-- For all pairs (R, W) of workflows R and W such that W is nested at some depth within R,
-- there is one row in this table for every top-level step of W.
-- In other words, for each such pair (R, W), and for each step S of W,
-- there is a row (R, W, S) in this table
-- Since we may say any workflow is nested at depth zero within itself,
-- the set of all (R, W) includes the pair (W, W) for every workflow W.
-- In a table row (R, W, S), root_wfl_id is the id of R, leaf_parent_step_id is the step id
-- of W as a top-level (NestedWorkflowStep) step of its parent workflow P that is
-- nested within R, if any (-1 when W = R; positive value when W < P <= R),
-- leaf_wfl_id is the id of W,
-- leaf_step_id is the step id of S in W, leaf_step_path is the full numerical path of S for
-- this particular nesting of W within R, and leaf_step_text_path is the text path of
-- this particular nesting of W within R.
-- For each (and every) instance of one workflow being nested at any depth within another,
-- there is one row in this table for each step of the nested workflow,
-- whose root_wfl_id is the id of the enclosing workflow.
-- Note that for the purposes of populating this table, we consider every workflow to "be nested within itself",
-- and so to generate one row in this table for each of its steps, in which its id is both
-- root_wfl_id and leaf_wfl_id.
-- cmp_id is company id.
-- id is an arbitrary sequential id for the rows of this table.
-- root_wfl_id is the workflow id of the enclosing workflow, for this particular pair of (enclosing, nested) workflows.
-- leaf_parent_step_id is the step id of the NestedWorkflowStep that represents the nested workflow
-- as a top-level step of the immediately enclosing workflow
-- (note that the "immediately enclosing workflow" in general is itself a nested workflow within the
-- workflow that is root workflow here).
-- leaf_wfl_id is the id of the nested workflow that is the subject of this particular table row.
-- leaf_step_id is the id of the particular step (as a top level step) of the nested workflow
-- that this table row corresponds to.
-- leaf_step_path is the "full (numerical) path" /WFLID1.STEPID1/WFLID2.STEPID2/.../WFLIDN.STEPIDN
-- of the step (as a top level step of the nested workflow) whose (step) id is leaf_step_id.
-- Note that root_wfl_id, leaf_parent_step_id, leaf_wfl_id, and leaf_step_id will all occur
-- within this path.
-- leaf_step_text_path is the text representation of the path.
-- ===================================================================

drop table twfl_spt_step_path;
create table twfl_spt_step_path
( 
    spt_cmp_id                    bigint,
    spt_id                        bigint,
    spt_root_wfl_id               bigint,
    spt_leaf_parent_step_id       bigint,
    spt_leaf_wfl_id               bigint,
    spt_leaf_step_id              bigint,
    spt_leaf_step_path            varchar(400),
    spt_leaf_step_text_path       varchar(1000)
) 

in users index in indx;


drop synonym spt;
create synonym spt for twfl_spt_step_path;

drop sequence qwfl_spt_id restrict;
create sequence qwfl_spt_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_coa_collaboration_area
--
-- Collaboration area is an object similar to a container, but used for temporary
-- manipulation of the entries through a workflow, before the entries are
-- checked back in to the source container.
-- cmp_id is company id.
-- id is collaboration area id.
-- name is collaboration area name.
-- desc is collaboration area description.
-- wfl_id is workflow id that the collaboration area uses.
-- src_container_id is the id of the source container for which the current collaboration
-- area is created.
-- dst_container_id is the id of the destination container, which is created for the
-- manipulation of the temporary entries of the current collaboration area.
-- deadline_date is the deadline for the col. area stored as a date.
-- deadline_duration is the deadline for the col. area stored as a number
-- specifying the duration. At any time, either a deadline_date or
-- a deadline_duration are set but not both.
-- is_locked is a flag recording whether the col. area is locked (flag = 'Y'),
-- i.e. col. area is in an non-editable state,
-- versus being in unlocked state and thus unavailable for editing (flag = 'N').
-- delete_on_empty is a flag recording whether the col. area should be deleted
-- after the last entry which enters it leaves (flag = 'Y'),
-- versus the col. area should be not deleted automatically (flag = 'N').
-- ===================================================================

drop table twfl_coa_collaboration_area;
create table twfl_coa_collaboration_area
( 
    coa_cmp_id                    bigint,
    coa_id                        bigint,
    coa_name                      varchar(200),
    coa_desc                      varchar(3000),
    coa_container_type            varchar(200),
    coa_wfl_id                    bigint,
    coa_src_container_id          bigint,
    coa_dst_container_id          bigint,
    coa_admin_users               varchar(3000),
    coa_admin_roles               varchar(3000),
    coa_deadline_date             timestamp,
    coa_deadline_duration         bigint,
    coa_is_locked                 char,
    coa_delete_on_empty           char,
    coa_object_group_id           bigint
) 

in users index in indx;


drop synonym coa;
create synonym coa for twfl_coa_collaboration_area;

drop sequence qwfl_coa_id restrict;
create sequence qwfl_coa_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_cae_coll_area_entry
--
-- Collaboration area entry, i.e. an entry having entered a particular
-- step (having a particular full step path)
-- of a particular workflow that is bound to a collaboration area.
-- cmp_id is company id.
-- ca_id is collaboration area id.
-- entry_id is entry id in collaboration area container.
-- step_path_id is a foreign key to "full path" /WFLID1.STEPID1/WFLID2.STEPID2/.../WFLIDN.STEPIDN
-- of the step that this entry is in, as a row of twfl_wfh_workflow_hierarchy
-- deadline is the absolute value of the deadline for this entry in this step.
-- deadline_step_path_id is either a foreign key to the "full path" /WFLID1.STEPID1/WFLID2.STEPID2/.../WFLIDN.STEPIDN
-- of the "effective" deadline step, i.e. the workflow step that has determined the effective deadline
-- for this CAE entry, or Checks.PATH_ID_DEFAULT_VALUE, to indicate that the effective deadline
-- has been determined by the collaboration area's deadline.
-- data is an encoded string capturing the state of approvals so far for this entry
-- in this step (only used by "and approval" steps).
-- 
-- available is a flag recording whether the entry is stable (flag = 'Y'),
-- i.e. stably in' a particular state and thus available to the GUI,
-- versus being in transition between states and thus unavailable to the GUI (flag = 'N').
-- lock_id specifies the id of the entry in the collaboration entry lock
-- table (cel) for the current entry of the collaboration area.
-- ===================================================================

drop table twfl_cae_coll_area_entry;
create table twfl_cae_coll_area_entry
( 
    cae_cmp_id                    bigint,
    cae_ca_id                     bigint,
    cae_entry_id                  bigint not null,
    cae_step_path_id              bigint not null,
    cae_deadline_step_path_id     bigint,
    cae_deadline                  timestamp,
    cae_data                      varchar(3000),
    cae_available                 char,
    cae_lock_id                   bigint,
    constraint cae_0_uk
        unique (cae_entry_id, cae_step_path_id)
) 

in users index in indx;


drop synonym cae;
create synonym cae for twfl_cae_coll_area_entry;

-- ===================================================================
--
-- Table twfl_csj_coll_area_step_job
--
-- Collaboration area step jobs.  These are jobs started by the "CollaborationArea::runStepJob" script operation.
-- Each invocation of that script operation generates one or more rows in this table.
-- cmp_id is company id.
-- ca_id is collaboration area id.
-- entry_id is the id of an entry in collaboration area container.  The relationship between such
-- entries and the started jobs is arbitrary, i.e. up to the invoker of the runStepJob operation.
-- step_path_id is the id of a row in the SPT table.
-- job_id is the id of the referenced job in the SCJ table.
-- job_name is the name of the feed or syndication as it appears in the respective console,
-- i.e. the value of FEE_NAME or SYN_NAME column in TCTG_FEE_FEED or TCTG_SYN_SYNDICATION.
-- job_type is a string which identifies the job type, such as "IMPORT" or "EXPORT".  The allowable
-- values of such strings are defined by the definition and implementation of the runStepJob operation.
-- job_date is the absolute value of the date at which the job should be run.
-- schedule_id is the id in the SCH table of the run-once schedule created to run this job (once) at job_date.
-- ===================================================================

drop table twfl_csj_coll_area_step_job;
create table twfl_csj_coll_area_step_job
( 
    csj_cmp_id                    bigint,
    csj_ca_id                     bigint,
    csj_entry_id                  bigint,
    csj_step_path_id              bigint,
    csj_job_id                    bigint,
    csj_job_name                  varchar(200),
    csj_job_type                  varchar(200),
    csj_job_date                  timestamp,
    csj_schedule_id               bigint
) 

in users index in indx;


drop synonym csj;
create synonym csj for twfl_csj_coll_area_step_job;

-- ===================================================================
--
-- Table twfl_cem_coll_entry_mapping
--
-- Keeps the mapping between the collaboration area entry and the entry in the source catalog
-- cmp_id is company id.
-- ca_id is collaboration area id.
-- ca_entry_id is entry id in collaboration area catalog.
-- src_ctg_id is catalog id of source catalog.
-- src_entry_id is entry id in source container.
-- ===================================================================

drop table twfl_cem_coll_entry_mapping;
create table twfl_cem_coll_entry_mapping
( 
    cem_cmp_id                    bigint,
    cem_ca_id                     bigint,
    cem_ca_entry_id               bigint,
    cem_src_ctg_id                bigint,
    cem_src_entry_id              bigint
) 

in users index in indx;


drop synonym cem;
create synonym cem for twfl_cem_coll_entry_mapping;

-- ===================================================================
--
-- Table twfl_ceh_coll_entry_history
--
-- Collaboration entry history.
-- entry_key is intended to be a database-independent, human-readable key for the entry,
-- for example SKU for items.  Ideally it is nevertheless an actual unique database key
-- for the entry.
-- event should be a relatively short, human-readable string that specifies what
-- sort of event has occurred in the life history of the entry.
-- step_path is the path of the step "at" which, or "in the neighborhood" of which, this event occurred.
-- data is flattened attributes.
-- ===================================================================

drop table twfl_ceh_coll_entry_history;
create table twfl_ceh_coll_entry_history
( 
    ceh_cmp_id                    bigint,
    ceh_id                        bigint,
    ceh_date                      timestamp,
    ceh_entry_key                 varchar(300),
    ceh_ca_name                   varchar(100),
    ceh_wfl_name                  varchar(100),
    ceh_user_name                 varchar(20),
    ceh_step_path                 varchar(3000),
    ceh_event                     varchar(100),
    ceh_data                      varchar(900),
    ceh_row_order                 bigint
) 

in users index in indx;


drop synonym ceh;
create synonym ceh for twfl_ceh_coll_entry_history;

drop sequence qwfl_ceh_id restrict;
create sequence qwfl_ceh_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_cel_coll_entry_lock
--
-- Collaboration entry lock. A lock for an entry (specified by
-- the column entry_id) from a collaboration area.
-- Later on, entries from main containers (catalogs, categoryTrees) might be
-- locked too! Hence the column container_type.
-- A lock is acquired by an user (specified by an user_id) at a particular
-- time (specified by the time_acquired).
-- A lock might expire as specified by the column time_of_expiry,
-- after it has been acquired.
-- The column step_path_id specifies the id of the step path at which the
-- lock has been acquired for a given entry of a collaboration area.
-- ===================================================================

drop table twfl_cel_coll_entry_lock;
create table twfl_cel_coll_entry_lock
( 
    cel_cmp_id                    bigint,
    cel_id                        bigint,
    cel_user_id                   bigint,
    cel_time_acquired             timestamp,
    cel_time_of_expiry            timestamp,
    cel_container_id              bigint,
    cel_container_type            varchar(200),
    cel_entry_id                  bigint not null,
    cel_step_path_id              bigint not null,
    constraint cel_0_uk
        unique (cel_entry_id, cel_step_path_id),
  constraint cel_fk_1
    foreign key (cel_entry_id, cel_step_path_id)
    references twfl_cae_coll_area_entry (cae_entry_id, cae_step_path_id)
) 

in users index in indx;


drop synonym cel;
create synonym cel for twfl_cel_coll_entry_lock;

drop sequence qwfl_cel_id restrict;
create sequence qwfl_cel_id start with 1 increment by 200 nocache;

-- ===================================================================
--
-- Table twfl_cah_coll_area_history
--
-- Collaboration area history. A row for each signficant event in
-- the life of the collaboration area.
-- Note that twfl_ceh_coll_entry_history is a separate history table
-- for the individual ITAs in the collaboration area. ca_name is the name of the collaboration area.
-- the action_type is defined in ActionTypeEnum as:
-- - CREATION for collaboration area creation
-- - CHECKOUT for initiation of workflow execution via ITA checkouts
-- - AGGREGATION for starting an aggregation into this collaboration area
-- - ADD for adding new entries into this collaboration area
-- - DELETION for deletion of this collaboration area
-- - CHECKIN for completion of the executing workflow by checking in some or all of its entries
-- - DROP for completion of the executing workflow by checking in some or all of its entries
-- user_name is the name of the performer or perpetrator of this event.
-- date is the timestamp.
-- data is any relevant data that characterizes this event.
-- ===================================================================

drop table twfl_cah_coll_area_history;
create table twfl_cah_coll_area_history
( 
    cah_ca_name                   varchar(3000),
    cah_action_type               char,
    cah_user_name                 varchar(3000),
    cah_date                      timestamp,
    cah_data                      varchar(3000)
) 

in users index in indx;


drop synonym cah;
create synonym cah for twfl_cah_coll_area_history;

-- ===================================================================
--
-- Table twfl_wfe_workflow_event
--
-- The WFE table allows for persisting of the previously in-memory workflow events.
-- Unique sequential ID will be used to uniquely identify events as well as to identify
-- the next event for FIFO processing.
-- event types are the classes located in src/java/com.ibm.ccd.workflow/common/events
-- The child WEA records hold the attributes for the various event types.
-- Status vales are: NEW, PROCESSING, DONE, ERROR
-- start_time -- time event started processing
-- finish_time -- time event finished processing
-- Following are from AbstractWorkflowEvent
-- acquire_lock -- boolean
-- collaboration_area_id
-- 
-- If the status is ERROR, then the  error_message column stores messages and stack traces from failed event processing (in addition to logging the error)
-- Here is the query for getting the next event row for processing
-- select * from wfe where event_id  = (select min(event_id) from wfe where status='NEW') for update;
-- The row retrieved gets updated with the status to PROCESSING so that the row
-- cannot be picked up by another JVM. It is then immediately then committed.
-- ===================================================================

drop table twfl_wfe_workflow_event;
create table twfl_wfe_workflow_event
( 
    wfe_cmp_id                    bigint,
    wfe_id                        bigint,
    wfe_event_type                varchar(50),
    wfe_event_status              varchar(50),
    wfe_start_time                timestamp,
    wfe_finish_time               timestamp,
    wfe_acquire_lock              char,
    wfe_collaboration_area_id     bigint,
    wfe_comment                   varchar(3000),
    wfe_source_step_at_path_id    bigint,
    wfe_dest_step_at_path_ids     varchar(900),
    wfe_prior_step_at_path_id     bigint,
    wfe_deadline_step_at_path_id  bigint,
    wfe_exit_value                varchar(3000),
    wfe_user_id                   bigint,
    wfe_error_message             varchar(3000),
    wfe_row_order                 bigint
) 

in users index in indx;


drop synonym wfe;
create synonym wfe for twfl_wfe_workflow_event;

drop sequence qwfl_wfe_id restrict;
create sequence qwfl_wfe_id start with 1 increment by 1 nocache;

-- ===================================================================
--
-- Table twfl_wee_workflow_event_entry
--
-- The WEE table allows for persisting the mapping of entries to events, and for storing the
-- status of the processing of entries for an event. This table will be queried by the appserver
-- UI code to refresh the UI status after doing a checkout.
-- ===================================================================

drop table twfl_wee_workflow_event_entry;
create table twfl_wee_workflow_event_entry
( 
    wee_event_id                  bigint,
    wee_entry_id                  bigint,
    wee_event_entry_status        varchar(50)
) 

in users index in indx;


drop synonym wee;
create synonym wee for twfl_wee_workflow_event_entry;



commit;

update command options using c on;

terminate;