--
-- workflow_case__begin_task_action/5
--
create or replace function workflow_case__begin_task_action(
  integer,
  character varying,
  character varying,
  integer,
  character varying
) returns int4 as $$

declare
  begin_task_action__task_id                alias for $1;  
  begin_task_action__action                 alias for $2;  
  begin_task_action__action_ip              alias for $3;  
  begin_task_action__user_id                alias for $4;  
  begin_task_action__msg                    alias for $5;  -- default null  
  v_state                                   varchar;
  v_journal_id                              integer;
  v_case_id                                 integer;
  v_transition_name                         varchar;
  v_num_rows                                integer;
begin
        select state into v_state
        from   wf_tasks
        where  task_id = begin_task_action__task_id;

        if begin_task_action__action = 'start' then
            if v_state != 'enabled' then
                raise EXCEPTION '-20000: Task is in state "%", but it must be in state "enabled" to be started.', v_state;
            end if;
        
            select case when count(*) = 0 then 0 else 1 end into v_num_rows
            from   wf_user_tasks
            where  task_id = begin_task_action__task_id
            and    user_id = begin_task_action__user_id;
            
            if v_num_rows = 0 then
                raise EXCEPTION '-20000: You are not assigned to this task.';
            end if;
        else if begin_task_action__action = 'finish' or begin_task_action__action = 'cancel' then

            if v_state = 'started' then
                /* Is this user the holding user? */
                select case when count(*) = 0 then 0 else 1 end into v_num_rows
                from   wf_tasks
                where  task_id = begin_task_action__task_id
                and    holding_user = begin_task_action__user_id;
                if v_num_rows = 0 then  
                    raise EXCEPTION '-20000: You are not the user currently working on this task.';
                end if;
            else if v_state = 'enabled' then
                if begin_task_action__action = 'cancel' then
                    raise EXCEPTION '-20000: You can only cancel a task in state "started", but this task is in state "%"', v_state;
                end if;

                /* Is this user assigned to this task? */
                select case when count(*) = 0 then 0 else 1 end into v_num_rows
                from   wf_user_tasks
                where  task_id = begin_task_action__task_id
                and    user_id = begin_task_action__user_id;
                if v_num_rows = 0 then  
                    raise EXCEPTION '-20000: You are not assigned to this task.';
                end if;

                /* This task is finished without an explicit start.
                 * Store the user as the holding_user */
                update wf_tasks 
                set    holding_user = begin_task_action__user_id 
                where  task_id = begin_task_action__task_id;
            else
                raise EXCEPTION '-20000: Task is in state "%", but it must be in state "enabled" or "started" to be finished', v_state;
            end if; end if;

        else if begin_task_action__action = 'comment' then
            -- We currently allow anyone to comment on a task
            -- (need this line because PL/SQL does not like empty if blocks)
            v_num_rows := 0;
        end if; end if; end if;

        select  t.case_id, tr.transition_name into v_case_id, v_transition_name
        from    wf_tasks t, 
                wf_transitions tr
        where   t.task_id = begin_task_action__task_id
        and     tr.workflow_key = t.workflow_key
        and     tr.transition_key = t.transition_key;

        /* Insert a journal entry */

        v_journal_id := journal_entry__new (
            null,
            v_case_id,
            'task ' || begin_task_action__task_id || ' ' || begin_task_action__action,
            v_transition_name || ' ' || begin_task_action__action,
            now(),
            begin_task_action__user_id,
            begin_task_action__action_ip,
            begin_task_action__msg
        );

        return v_journal_id;
     
end;$$ language plpgsql;