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

declare
  suspend__case_id                alias for $1;  
  suspend__user_id                alias for $2;  -- default null  
  suspend__ip_address             alias for $3;  -- default null
  suspend__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 = suspend__case_id;

        if v_state != 'active' then
            raise EXCEPTION '-20000: Only active cases can be suspended';
        end if;
        
        /* Add an entry to the journal */
        v_journal_id := journal_entry__new(
            null,
            suspend__case_id,
            'case suspend',
            'case suspended',
            now(),
            suspend__user_id,
            suspend__ip_address,
            suspend__msg
        );

        update wf_cases
        set    state = 'suspended'
        where  case_id = suspend__case_id;

        return 0; 
end;$$ language plpgsql;