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

declare
  resume__case_id                alias for $1;  
  resume__user_id                alias for $2;  -- default null  
  resume__ip_address             alias for $3;  -- default null
  resume__msg                    alias for $4;  -- default null
  v_state                        varchar;   
  v_journal_id                   integer;        
begin
        select state into v_state
        from   wf_cases
        where  case_id = resume__case_id;

        if v_state != 'suspended' and v_state != 'canceled' then
            raise EXCEPTION '-20000: Only suspended or canceled cases can be resumed';
        end if;

        /* Add an entry to the journal */
        v_journal_id := journal_entry__new(
            null,
            resume__case_id,
            'case resume',
            'case resumed',
            now(),
            resume__user_id,
            resume__ip_address,
            resume__msg
        );

        update wf_cases
        set    state = 'active'
        where  case_id = resume__case_id;

        return 0; 
end;$$ language plpgsql;