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

declare
  notification__task_id               alias for $1; 	
  notification__custom_arg            alias for $2;
  notification__party_to              alias for $3;
  notification__party_from            alias for $4;
  notification__subject               alias for $5;
  notification__body                  alias for $6;
  v_deadline_pretty                   varchar(400);
  v_object_name                       text;
  v_transition_name                   wf_transitions.transition_name%TYPE;
  v_name                              varchar(1000);
  v_subject                           text default '';
  v_body                              text default '';
  v_request_id                        integer;
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;

	v_subject := 'Assignment: ' || v_transition_name || ' ' || 
                     v_object_name;

	v_body := 'Dear ' || acs_object__name(notification__party_to) || '
' || '
Today, you have been assigned to a task.
' || '
Task    : ' || v_transition_name || '
Object  : ' || v_object_name || '
';

        if v_deadline_pretty != '' then
           v_body := v_body || 'Deadline: ' || v_deadline_pretty || '
';
        end if;

        -- NOTICE, NOTICE, NOTICE
        --
        -- Since postgresql does not support out parameters, this 
        -- function call has been moved from workflow_case.notify_assignee
        -- into the callback function.

        -- If you implement a new notification callback, make sure 
        -- that this function call is included at the end of the 
        -- callback routine just as we have done for this example code.
        --
        -- DanW (dcwickstrom@earthlink.net)

        v_request_id := acs_mail_nt__post_request (       
            notification__party_from,     -- party_from
            notification__party_to,       -- party_to
            'f',                        -- expand_group
            v_subject,                    -- subject
            v_body,                       -- message
            0                             -- max_retries
        );

        return null;
end;$$ language plpgsql;