/* This table will hold one row for each case using this workflow. */

begin;

create table wf_ticket_cases (
  case_id 		integer primary key
			constraint wf_ticket_cases_case_fk
			references wf_cases
);


select workflow__create_workflow(
	'ticket_wf', 			-- workflow_key => 'ticket_wf', 
	'Ticket Tracker Process', 	-- pretty_name => 'Ticket Tracker Process', 
	'Ticket Tracker Process', 	-- pretty_plural => 'Ticket Tracker Process', 
	'Workflow for processing a ticket in the ticket-tracker',
					-- description => 'Workflow for processing a ticket in the ticket-tracker',
	'wf_ticket_cases',		-- table_name => 'wf_ticket_cases'
	'case_id'			-- default value
    );




insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('start', 'ticket_wf', 'Needs to be fixed', 1);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('to_be_acknowledged', 'ticket_wf', 'Needs to be acknowledged', 2);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('to_be_clarified', 'ticket_wf', 'Needs to be clarified', 3);
insert into wf_places(place_key, workflow_key, place_name, sort_order) 
    values ('end', 'ticket_wf', 'End state', 4);


insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type) 
    values ('fix', 'Fix problem', 'ticket_wf', 1, 'user');
insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type) 
    values ('acknowledge', 'Acknowledge fix', 'ticket_wf', 2, 'user');
insert into wf_transitions(transition_key, transition_name, workflow_key, sort_order, trigger_type)
    values ('clarify', 'Clarify problem', 'ticket_wf', 3, 'user');


-- fix --
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('ticket_wf', 'fix', 'start', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_custom_arg, guard_description) 
    values ('ticket_wf', 'fix', 'to_be_acknowledged', 'out', 'wf_callback.guard_attribute_true', 'clear', 'Problem clear and fixed');
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_custom_arg, guard_description) 
    values ('ticket_wf', 'fix', 'to_be_clarified', 'out', '#', '', 'Problem not clear');


-- acknowledge
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('ticket_wf', 'acknowledge', 'to_be_acknowledged', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_custom_arg, guard_description) 
    values ('ticket_wf', 'acknowledge', 'end', 'out', 'wf_callback.guard_attribute_true', 'acknowledged', 'Fix okay');
insert into wf_arcs(workflow_key, transition_key, place_key, direction, guard_callback, guard_custom_arg, guard_description) 
    values ('ticket_wf', 'acknowledge', 'start', 'out', '#', '', 'Fix not okay');


-- clarify
-- in
insert into wf_arcs(workflow_key, transition_key, place_key, direction) 
    values ('ticket_wf', 'clarify', 'to_be_clarified', 'in');
-- out
insert into wf_arcs(workflow_key, transition_key, place_key, direction)
    values ('ticket_wf', 'clarify', 'start', 'out');



create function inline_0 ()
returns integer as '
declare
    v_attribute_id acs_attributes.attribute_id%TYPE;
begin
    v_attribute_id := workflow__create_attribute(
	''ticket_wf'',		-- workflow_key => ''ticket_wf'',
	''clear'',		-- attribute_name => ''clear'',
	''boolean'',		-- datatype => ''boolean'',
	''Problem is clear'',	-- pretty_name => ''Problem is clear'',
	null,			-- pretty_plural
	null,			-- table_name
	null,			-- column_name
	''t'', 			-- default_value => ''t''
	1,			-- min_n_values
	1,			-- max_n_values
	null,			-- sort_order
	''generic'',		-- storage
	''none''		-- wf_datatype
    );

    insert into wf_transition_attribute_map
        (workflow_key, transition_key, attribute_id, sort_order) 
    values
        (''ticket_wf'', ''fix'', v_attribute_id, 1);

    v_attribute_id := workflow__create_attribute(
	''ticket_wf'',		-- workflow_key => ''ticket_wf'',
	''acknowledged'',	-- attribute_name => ''acknowledged'',
	''boolean'',		-- datatype => ''boolean'',
	''Fix is okay'',	-- pretty_name => ''Fix is okay'',
	null,			-- pretty_plural
	null,			-- table_name
	null,			-- column_name
	''t'', 			-- default_value => ''t''
	1,			-- min_n_values
	1,			-- max_n_values
	null,			-- sort_order
	''generic'',		-- storage
	''none''		-- wf_datatype
    );

    insert into wf_transition_attribute_map
        (workflow_key, transition_key, attribute_id, sort_order) 
    values
        (''ticket_wf'', ''acknowledge'', v_attribute_id, 1);
     
    return 0;
end;' language 'plpgsql';
	 
select inline_0 ();
	 
drop function inline_0 ();


insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'fix', 1, 'Ticket', '/wf-ticket-tracker/panel-ticket');

insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'fix', 2, 'What To Do', '/wf-ticket-tracker/panel-fix-to-do');

insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'clarify', 1, 'Ticket', '/wf-ticket-tracker/panel-ticket');

insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'clarify', 2, 'What To Do', '/wf-ticket-tracker/panel-clarify-to-do');

insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'acknowledge', 1, 'Ticket', '/wf-ticket-tracker/panel-ticket');

insert into wf_context_task_panels(context_key, workflow_key, transition_key, sort_key, header, template_url)
values ('default', 'ticket_wf', 'acknowledge', 2, 'What To Do', '/wf-ticket-tracker/panel-acknowledge-to-do');


end;
