--
-- acs-workflow/sql/sample-article-create.sql
--
-- Creates a sample article-authoring workflow to play with
--
-- @author Kevin Scaldeferri (kevin@theory.caltech.edu)
--
-- @creation-date 2000-05-18
--
-- @cvs-id $Id: sample-article-create.sql,v 1.1.1.1 2001/03/13 22:59:27 ben Exp $
--

/* This table will hold one row for each case using this workflow. */
create table wf_article_cases (
  case_id 		integer primary key
			constraint wf_article_cases_case_fk
			references wf_cases on delete cascade
);


declare
    v_workflow_key varchar2(40);
begin
    v_workflow_key := workflow.create_workflow(
	workflow_key => 'article_wf', 
	pretty_name => 'Article Publication', 
	pretty_plural => 'Article Publications', 
	description => 'Workflow for managing the publication of an article',
	table_name => 'wf_article_cases'
    );
end;
/
show errors

insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('start', 'article_wf', 'Initial state', 1);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('to_be_written', 'article_wf', 'Needs to be written', 2);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('to_be_reviewed', 'article_wf', 'Needs review', 3);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('to_go_to_press', 'article_wf', 'Ready to go to press', 4);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('end', 'article_wf', 'End state', 5);


insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type) 
    values ('specification', 'Description and assignment by editor', 'article_wf', 1, 'user');
insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type) 
    values ('writing', 'Writing by author', 'article_wf', 2, 'user');
insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type)
    values ('review', 'Approval by reviewer', 'article_wf', 3, 'user');
insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type) 
    values ('press', 'Publication of article', 'article_wf', 4, 'automatic');


-- specification --
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'specification', 'start', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'specification', 'to_be_written', 'out');


-- writing
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'writing', 'to_be_written', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'writing', 'to_be_reviewed', 'out');


-- review (or-split)
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'review', 'to_be_reviewed', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_custom_arg, guard_description) 
    values ('article_wf', 'review', 'to_go_to_press', 'out', 'wf_callback.guard_attribute_true', 'reviewer_ok', 'Reviewer approved article');
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_description)
    values ('article_wf', 'review', 'to_be_written', 'out', '#',  'Reviewer disapproved article');


-- press
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'press', 'to_go_to_press', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('article_wf', 'press', 'end', 'out');


declare
    v_attribute_id acs_attributes.attribute_id%TYPE;
begin
    v_attribute_id := workflow.create_attribute(
	workflow_key => 'article_wf',
	attribute_name => 'reviewer_ok',
	datatype => 'boolean',
	pretty_name => 'Reviewer Approval',
	default_value => 'f'
    );

    insert into wf_transition_attribute_map
        (workflow_key, transition_key, attribute_id, sort_order) 
    values
        ('article_wf', 'review', v_attribute_id, 1);

end;
/
show errors;


-- assignment as part of workflow
insert into wf_transition_assignment_map 
    (workflow_key, transition_key, assign_transition_key)
values
    ('article_wf', 'specification', 'writing');

insert into wf_transition_assignment_map 
    (workflow_key, transition_key, assign_transition_key)
values
    ('article_wf', 'specification', 'review');


/* Context stuff */


insert into wf_context_transition_info (
    context_key, workflow_key, transition_key, hold_timeout_callback, hold_timeout_custom_arg
) values (
    'default', 'article_wf', 'specification', 'wf_callback.time_sysdate_plus_x', 1/24
);

commit;


create or replace package wf_article_callback
is
    procedure notification(
        task_id 	in number, 
            custom_arg 	in varchar2, 
            party_to 	in integer, 
            party_from 	in out integer, 
            subject 	in out varchar2, 
            body 	in out varchar2
    );

end wf_article_callback;
/
show errors


create or replace package body wf_article_callback
is

    procedure notification(
        task_id 	in number, 
        custom_arg 	in varchar2, 
        party_to 	in integer, 
        party_from 	in out integer, 
        subject 	in out varchar2, 
        body	 	in out varchar2
    )
    is
	v_deadline_pretty varchar2(400);
        v_object_name varchar2(4000);
        v_transition_name wf_transitions.transition_name%TYPE;
        v_name varchar2(1000);
    begin
        select to_char(ta.deadline,'Mon fmDDfm, YYYY HH24:MI:SS'),
               acs_object.name(c.object_id),
               tr.transition_name
        into   v_deadline_pretty,
               v_object_name, 
	       v_transition_name
	from   wf_tasks ta, wf_transitions tr, wf_cases c
	where  ta.task_id = notification.task_id
        and    c.case_id = ta.case_id
        and    tr.workflow_key = c.workflow_key
        and    tr.transition_key = ta.transition_key;

	subject := 'Assignment: '||v_transition_name||' '||v_object_name;
	body := 'Dear '||acs_object.name(party_to)||'
'||'
Today, you have been assigned to a task.
'||'
Task    : '||v_transition_name||'
Object  : '||v_object_name||'
';

        if v_deadline_pretty != '' then
            body := body ||'Deadline: '||v_deadline_pretty||'
';
        end if;
    end notification;

end wf_article_callback;
/
show errors




update wf_context_transition_info
set notification_callback = 'wf_article_callback.notification'
where workflow_key = 'article_wf'
and   context_key = 'default'
and   transition_key = 'specification';

commit;
