DBA Data[Home] [Help]

PACKAGE BODY: APPS.WF_RULE

Source


1 package body WF_RULE as
2 /* $Header: wfruleb.pls 120.5.12020000.3 2012/08/10 14:20:14 skandepu ship $ */
3 --------------------------------------------------------------------------
4 /*
5 ** get_sub_parameters (PRIVATE) - retrieve the value of the PARAMETERS
6 **                                column from the wf_event_subscriptions
7 **                                table for the specified guid
8 */
9 FUNCTION get_sub_parameters(p_subscription_guid in raw) return varchar2
10 is
11   parm varchar2(4000);
12 begin
13   select parameters into parm
14   from   wf_event_subscriptions
15   where  guid = p_subscription_guid;
16 
17   return parm;
18 exception
19   when no_data_found then
20     wf_core.context('Wf_Rule', 'get_sub_parameters', p_subscription_guid);
21     wf_core.raise('WFE_SUBSC_NOTEXIST');
22 end;
23 --------------------------------------------------------------------------
24 /*
25 ** log - <Described in wfrules.pls>
26 */
27 FUNCTION log(p_subscription_guid in     raw,
28              p_event             in out nocopy wf_event_t) return varchar2
29 is
30   i                     number;
31   parmlist              wf_parameter_list_t;
32   srctype               varchar2(100);
33   istmp                 number;
34   myclob                clob;
35   clob_len              number;
36   clob_bufsize constant number := 240; -- using 240 instead of larger number
37                                        -- so dbms_output.putline-version of
38                                        -- logging for standalone version
39                                        -- works ok
40   offset                number;
41 
42 begin
43   wf_log_pkg.string(wf_log_pkg.level_procedure, 'wf.plsql.wf_rule.log.begin',
44                     'Begin wf_rule.log rule function');
45 
46   if (p_event is null) then
47     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.event_null',
48                       'Event is null. Returning.');
49     return 'SUCCESS';
50   end if;
51 
52   -- Reimplementation with wf_log_pkg
53 
54   if (p_event.getFromAgent() is not null) then
55     -- Log message only
56     -- BINDVAR_SCAN_IGNORE[2]
57     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.from_agent',
58                       'From Agent Name: '||p_event.getFromAgent().getName());
59     -- BINDVAR_SCAN_IGNORE[2]
60     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.from_agent.from_agent_sys',
61                       'From Agent System: '||p_event.getFromAgent().getSystem());
62   end if;
63 
64   if (p_event.getToAgent() is not null) then
65     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.to_agent',
66                       'To Agent Name: '||p_event.getToAgent().getName());
67     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.to_agent_sys',
68                       'To Agent System: '||p_event.getToAgent().getSystem());
69   end if;
70 
71   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.priority',
72                    'Priority: '||p_event.getPriority());
73   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.send_date',
74                    'Send Date: '||to_char(p_event.getSendDate(), wf_core.canonical_date_mask));
75   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.receive_date',
76                    'Receive Date: '||to_char(p_event.getReceiveDate(), wf_core.canonical_date_mask));
77   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.corr_id',
78                    'Correlation ID: '||p_event.getCorrelationID());
79 
80   parmlist := p_event.getParameterList();
81   if (parmlist is not null) then
82     i := parmlist.FIRST;
83     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.param_list',
84                      'Begin Parameterlist');
85 
86     while (i <= parmlist.LAST) loop
87       wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.param',
88                        'Name: '||parmlist(i).getName()||' Value: '||parmlist(i).getValue());
89 
90       i := parmlist.NEXT(i);
91     end loop;
92     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.param_list',
93                      'End Parameterlist');
94   end if;
95 
96   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.event_name',
97                    'Event Name: '||p_event.getEventName());
98   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.event_key',
99                    'Event Key: '||p_event.getEventKey());
100 
101   select source_type into srctype from wf_event_subscriptions
102   where guid = p_subscription_guid;
103 
104   -- Need to check if clob is temporary instead of null
105   IsTmp := dbms_lob.istemporary(p_event.GetEventData());
106   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.istemp',
107                    'Event Data is Temp: '||IsTmp);
108   if ((IsTmp= 1 and srctype = 'LOCAL')
109   or (IsTmp= 0 and srctype in ('EXTERNAL','ERROR'))) then
110     wf_log_pkg.string(wf_log_pkg.level_statement,
111     			'wf.plsql.wf_rule.log.event_data', 'Begin EventData');
112     myclob := p_event.getEventData();
113     if myclob is not null then
114       offset := 1;
115       clob_len := dbms_lob.getlength(myclob);
116       while( offset <= clob_len ) loop
117         wf_log_pkg.string( wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.message',
118           dbms_lob.substr( myclob, clob_bufsize, offset ));
119         offset := offset + clob_bufsize;
120       end loop;
121     end if;
122     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.event_data', 'End EventData');
123   else
124     wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.event_data', 'Event Data is Empty');
125   end if;
126 
127   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.error_sub',
128                    'Error Subscription: '||p_event.getErrorSubscription());
129   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.error_msg',
130                    'Error Message: '||p_event.getErrorMessage());
131   wf_log_pkg.string(wf_log_pkg.level_statement, 'wf.plsql.wf_rule.log.error_stk',
132                    'Error Stack: '||p_event.getErrorStack());
133 
134   wf_log_pkg.string(wf_log_pkg.level_procedure, 'wf.plsql.wf_rule.log.end',
135                    'Completed wf_rule.log rule function');
136 
137   return 'SUCCESS';
138 exception
139   when others then
140     wf_log_pkg.string(wf_log_pkg.level_error, 'wf.plsql.wf_rule.log.end',
141                      'Error in wf_rule.log rule function');
142     wf_core.context('Wf_Event', 'Log', p_event.getEventName(), p_subscription_guid);
143     wf_event.setErrorInfo(p_event, 'ERROR');
144     return 'ERROR';
145 end;
146 ---------------------------------------------------------------------------
147 /*
148 ** error - <described in wfrules.pls>
149 */
150 FUNCTION error(p_subscription_guid in     raw,
151                p_event             in out nocopy wf_event_t) return varchar2
152 is
153   msg varchar2(4000);
154   l_parameters varchar2(4000);
155 begin
156   l_parameters := upper(wf_rule.get_sub_parameters(p_subscription_guid));
157   msg := WF_EVENT_FUNCTIONS_PKG.SUBSCRIPTIONPARAMETERS(l_parameters, 'ERROR_MESSAGE');
158   wf_event.setErrorInfo(p_event, 'ERROR');
159   p_event.setErrorMessage(wf_core.substitute('WFERR', msg));
160 
161   return 'ERROR';
162 end;
163 ---------------------------------------------------------------------------
164 /*
165 ** warning - <described in wfrules.pls>
166 */
167 FUNCTION warning(p_subscription_guid in     raw,
168                  p_event             in out nocopy wf_event_t) return varchar2
169 is
170   msg varchar2(4000);
171   l_parameters varchar2(4000);
172 begin
173   l_parameters := upper(wf_rule.get_sub_parameters(p_subscription_guid));
174   msg := WF_EVENT_FUNCTIONS_PKG.SUBSCRIPTIONPARAMETERS(l_parameters, 'ERROR_MESSAGE');
175   wf_event.setErrorInfo(p_event, 'WARNING');
176   p_event.setErrorMessage(wf_core.substitute('WFERR', msg));
177 
178   return 'WARNING';
179 end;
180 ---------------------------------------------------------------------------
181 /*
182 ** success - <described in wfrules.pls>
183 */
184 FUNCTION success(p_subscription_guid in     raw,
185                  p_event             in out nocopy wf_event_t) return varchar2
186 is
187 begin
188   return 'SUCCESS';
189 end;
190 ---------------------------------------------------------------------------
191 /*
192 ** default_rule - <described in wfrules.pls>
193 */
194 FUNCTION default_rule(p_subscription_guid in     raw,
195                       p_event             in out nocopy wf_event_t) return varchar2
196 is
197   out_guid  raw(16);
198   to_guid   raw(16);
199   wftype    varchar2(30);
200   wfname    varchar2(30);
201   res       varchar2(30);
202   pri       number;
203   ikey      varchar2(240);
204   lparamlist wf_parameter_list_t;
205   subparams varchar2(4000);
206   lcorrid   varchar2(240);
207 
208   --Define an exception to capture the resource_busy error
209   resource_busy exception;
210   pragma EXCEPTION_INIT(resource_busy,-00054);
211 
212 
213 begin
214   select out_agent_guid,to_agent_guid,wf_process_type,wf_process_name,priority
215          , parameters
216   into   out_guid, to_guid, wftype, wfname, pri, subparams
217   from   wf_event_subscriptions
218   where  guid = p_subscription_guid;
219 
220   -- Check if need to generate Unique Correlation Id
221   if subparams is not null then
222     if (wf_event_functions_pkg.subscriptionparameters(p_string=>subparams
223                         ,p_key=>'CORRELATION_ID') = 'UNIQUE') then
224       select wf_error_processes_s.nextval
225       into lcorrid
226       from dual;
227 
228       lcorrid := p_event.event_key||'-'||lcorrid;
229       p_event.SetCorrelationId(lcorrid);
230     end if;
231   end if;
232 
233   -- Workflow --
234   if (wftype is not null) then
235 
236     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
237        wf_log_pkg.string(wf_log_pkg.level_statement,
238                         'wf.plsql.WF_RULE.default_rule.event',
239                         'Calling wf_engine.event()');
240     end if;
241 
242     lparamlist := p_event.Parameter_List;
243     wf_event.AddParameterToList('SUB_GUID', p_subscription_guid,lparamlist);
244     p_event.Parameter_List := lparamlist;
245     --p_event.addParameterToList('SUB_GUID', p_subscription_guid);
246 
247     if (wftype = 'WFERROR') then
248       select to_char(WF_ERROR_PROCESSES_S.nextval) into ikey from dual;
249     else
250       ikey := nvl(p_event.Correlation_ID, p_event.Event_Key);
251     end if;
252 
253     --The resource busy exception will be raised by wf_engine.event
254     --and caught in the exception block.
255     wf_engine.event(
256        itemtype      => wftype,
257        itemkey       => ikey,
258        process_name  => wfname,
259        event_message => p_event);
260   end if;
261 
262   -- Route --
263   /** single consumer queues do not need a To Agent  **/
264   -- if (to_guid is not null) then
265   if (out_guid is not null) then
266     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
267        wf_log_pkg.string(wf_log_pkg.level_statement,
268                         'wf.plsql.WF_RULE.default_rule.route',
269                         'Routing...');
270     end if;
271 
272     p_event.From_Agent := wf_event.newAgent(out_guid);
273     p_event.To_Agent := wf_event.newAgent(to_guid);
274     p_event.Priority := pri;
275     p_event.Send_Date := nvl(p_event.getSendDate(),sysdate);
276 
277     --p_event.Address(wf_event.newAgent(out_guid), wf_event.newAgent(to_guid),
278     --                pri, nvl(p_event.getSendDate(),sysdate));
279 
280     wf_event.send(p_event);
281   end if;
282 
283   -- Debug --
284   if (wf_log_pkg.wf_debug_flag = TRUE) then
285     begin
286       res := wf_rule.log(p_subscription_guid, p_event);
287     exception
288       when others then null;
289     end;
290   end if;
291 
292   return 'SUCCESS';
293 exception
294   --This exception alone raise to caller
295   when resource_busy then
296     --Raise this exception to caller
297     raise;
298 
299   when others then
300     wf_core.context('Wf_Rule', 'Default_Rule', p_event.getEventName(),
301                                                 p_subscription_guid);
302     wf_event.setErrorInfo(p_event, 'ERROR');
303     return 'ERROR';
304 end;
305 
306 ---------------------------------------------------------------------------
307 /*
308 ** default_rule2 - Executes default_rule only if the subscription contains
309 **                 parameters that are in the event parameter list.
310 */
311 FUNCTION default_rule2(p_subscription_guid in     raw,
312                        p_event             in out nocopy wf_event_t)
313          return varchar2 is
314 begin
315   if (WF_EVENT_FUNCTIONS_PKG.SubParamInEvent(p_subscription_guid, p_event)) then
316     return (default_rule(p_subscription_guid, p_event));
317 
318   else
319     return 'SUCCESS';
320 
321   end if;
322 
323 end;
324 
325 ---------------------------------------------------------------------------
326 /*
327 ** workflow_protocol - <described in wfrules.pls>
328 */
329 FUNCTION workflow_protocol(p_subscription_guid in     raw,
330                       p_event             in out nocopy wf_event_t) return varchar2
331 is
332   wftype    varchar2(30);
333   wfname    varchar2(30);
334   param	    varchar2(4000);
335   res       varchar2(30);
336   pri       number;
337   ikey      varchar2(240);
338   ackreq    varchar2(1);
339   sndack    varchar2(1);
340 begin
341 
342   select wf_process_type,wf_process_name,priority,parameters
343   into   wftype, wfname, pri, param
344   from   wf_event_subscriptions
345   where  guid = p_subscription_guid;
346 
347   -- Workflow --
348   if (wftype is not null) then
349     if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
350        wf_log_pkg.string(wf_log_pkg.level_procedure,
351                         'wf.plsql.WF_RULE.workflow_protocol.event',
352                         'Calling wf_engine.event()');
353     end if;
354     --
355     -- Add Parameters to List
356     --
357     ackreq := p_event.GetValueForParameter('ACKREQ');
358     sndack := p_event.GetValueForParameter('SNDACK');
359 
360     if ackreq = 'Y' then
361       -- The Received Message will be waiting for acknowledgement
362       sndack := 'Y';
363     else
364       sndack := 'N';
365     end if;
366 
367     -- Does the new message require acknowledgement
368     ackreq := wf_event_functions_pkg.subscriptionparameters
369 					(param, 'ACKREQ');
370 
371     p_event.addParameterToList('ACKREQ', nvl(ackreq,'N'));
372     p_event.addParameterToList('SNDACK', nvl(sndack,'N'));
373     p_event.addParameterToList('SUB_GUID', p_subscription_guid);
374 
375     if (wftype = 'WFERROR') then
376       select to_char(WF_ERROR_PROCESSES_S.nextval) into ikey from dual;
377     else
378       ikey := nvl(p_event.getCorrelationID(), p_event.getEventKey());
379     end if;
380 
381     wf_engine.event(
382        itemtype      => wftype,
383        itemkey       => ikey,
384        process_name  => wfname,
385        event_message => p_event);
386   end if;
387 
388   -- Debug --
389   if (wf_log_pkg.wf_debug_flag = TRUE) then
390     res := wf_rule.log(p_subscription_guid, p_event);
391   end if;
392 
393   return 'SUCCESS';
394 exception
395   when others then
396     wf_core.context('Wf_Rule', 'Workflow_Protocol', p_event.getEventName(),
397                                                 p_subscription_guid);
398     wf_event.setErrorInfo(p_event, 'ERROR');
399     return 'ERROR';
400 end;
401 ---------------------------------------------------------------------------
402 /*
403 ** error_rule - <described in wfrules.pls>
404 */
405 FUNCTION error_rule(p_subscription_guid in     raw,
406                     p_event          in out nocopy wf_event_t) return varchar2
407 is
408   out_guid  raw(16);
409   to_guid   raw(16);
410   wftype    varchar2(30);
411   wfname    varchar2(30);
412   res       varchar2(30);
413   pri       number;
414   ikey      varchar2(240);
415 begin
416   select out_agent_guid,to_agent_guid,wf_process_type,wf_process_name,priority
417   into   out_guid, to_guid, wftype, wfname, pri
418   from   wf_event_subscriptions
419   where  guid = p_subscription_guid;
420 
421   -- Workflow --
422   if (wftype is not null) then
423     if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
424        wf_log_pkg.string(wf_log_pkg.level_procedure,
425                         'wf.plsql.WF_RULE.error_rule.event',
426                         'Calling wf_engine.event()');
427     end if;
428 
429     p_event.addParameterToList('SUB_GUID', p_subscription_guid);
430 
431     if (wftype = 'WFERROR') then
432       select to_char(WF_ERROR_PROCESSES_S.nextval) into ikey from dual;
433     else
434       ikey := nvl(p_event.getCorrelationID(), p_event.getEventKey());
435     end if;
436 
437     wf_engine.event(
438        itemtype      => wftype,
439        itemkey       => ikey,
440        process_name  => wfname,
441        event_message => p_event);
442   end if;
443 
444   -- Route --
445   /** single consumer queues do not need a To Agent  **/
446   -- if (to_guid is not null) then
447   if (out_guid is not null) then
448     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
449        wf_log_pkg.string(wf_log_pkg.level_statement,
450                         'wf.plsql.WF_RULE.error_rule.route',
451                         'Routing...');
452     end if;
453     p_event.From_Agent := wf_event.newAgent(out_guid);
454     p_event.To_Agent := wf_event.newAgent(to_guid);
455     p_event.Priority := pri;
456     p_event.Send_Date := nvl(p_event.getSendDate(),sysdate);
457 
458     --p_event.Address(wf_event.newAgent(out_guid), wf_event.newAgent(to_guid),
459     --                pri, nvl(p_event.getSendDate(),sysdate));
460 
461     wf_event.send(p_event);
462   end if;
463 
464 
465   -- Debug --
466   if (wf_log_pkg.wf_debug_flag = TRUE) then
467     begin
468       res := wf_rule.log(p_subscription_guid, p_event);
469     exception
470       when others then null;
471     end;
472   end if;
473 
474   return 'SUCCESS';
475 exception
476   when others then
477     wf_core.context('Wf_Rule', 'Error_Rule', p_event.getEventName(),
478                                                 p_subscription_guid);
479     raise;
480 end;
481 ----------------------------------------------------------------------------
482 /*
483 ** setParametersIntoParameterList - <described in wfrules.pls>
484 **
485 */
486 FUNCTION setParametersIntoParameterList(p_subscription_guid in     raw,
487                            p_event          in out nocopy wf_event_t) return varchar2
488 is
489   l_parameters    varchar2(4000);
490   l_start         integer := 0;
491   l_end           integer := 0;
492   l_endposition   number;
493   l_namevalue     varchar2(4000);
494   l_value         varchar2(4000);
495   l_name          varchar2(4000);
496   l_equalpos      number;
497 
498   CURSOR  c_parameters IS
499   SELECT  parameters
500   FROM    wf_event_subscriptions
501   WHERE   guid = p_subscription_guid;
502 
503 begin
504 
505   -- Get the Subscription Parameter String
506   OPEN c_parameters;
507   FETCH c_parameters INTO l_parameters;
508   CLOSE c_parameters;
509 
510   -- If not null then continue
511   if l_parameters is not null then
512     -- Replace New Line, tab and CR Characters
513     l_parameters := replace(l_parameters,wf_core.newline,' ');
514     l_parameters := replace(l_parameters,wf_core.tab,' ');
515     l_parameters := replace(l_parameters,wf_core.cr,'');
516     l_parameters := l_parameters||' ';
517 
518     -- Initialize Start and End
519     l_start:= 1;
520     l_end := length(l_parameters);
521     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
522        wf_log_pkg.string(wf_log_pkg.level_statement,
523                         'wf.plsql.WF_RULE.setparametersintoparameterlist.params',
524                         'Length of Parameters is '||l_end);
525     end if;
526 
527     while l_start < l_end loop
528       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
529          wf_log_pkg.string(wf_log_pkg.level_statement,
530                           'wf.plsql.WF_RULE.setparametersintoparameterlist.st_pos',
531                           'Start Position is:'||l_start);
532       end if;
533       -- Get End of Name=Value pair
534       l_endposition := instr(l_parameters, ' ', l_start, 1);
535 
536       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
537          wf_log_pkg.string(wf_log_pkg.level_statement,
538                           'wf.plsql.WF_RULE.setparametersintoparameterlist.end_pos',
539                           'EndPosition of Name=Value Pair: '||l_endposition);
540       end if;
541 
542       -- Extract Name=Value Pair
543       l_namevalue := rtrim(ltrim(substr(l_parameters, l_start, l_endposition-l_start)));
544 
545       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
546          wf_log_pkg.string(wf_log_pkg.level_statement,
547                           'wf.plsql.WF_RULE.setparametersintoparameterlist.namevalue',
548                           'Name=Value Pair is:'||l_namevalue);
549       end if;
550 
551       -- Extract Name part of Name=Value pair
552       l_equalpos := instr(l_namevalue, '=', 1, 1);
553       l_name := substr(l_namevalue,1,l_equalpos-1);
554       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
555          wf_log_pkg.string(wf_log_pkg.level_statement,
556                           'wf.plsql.WF_RULE.setparametersintoparameterlist.paramname',
557                           'Parameter Name='||l_name);
558       end if;
559 
560       -- Extract Value part of Name=Value pair
561       l_value := substr(l_namevalue,l_equalpos+1,length(l_namevalue));
562       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
563           wf_log_pkg.string(wf_log_pkg.level_statement,
564                            'wf.plsql.WF_RULE.setparametersintoparameterlist.paramvalue',
565                            'Parameter Value='||l_value);
566       end if;
567 
568       -- OK, Set the Value into the Parameter List
569       -- unless we are dealing with reserved words CORRELATION_ID, ITEMKEY
570       -- in which case we will set these values into the Event Header
571       -- properties
572       if (l_name in ('CORRELATION_ID','ITEMKEY')) then
573         p_event.SetCorrelationId(l_value);
574       else
575         p_event.AddParameterToList(l_name, l_value);
576       end if;
577 
578       -- Reset Starting Point
579       l_start := l_endposition+1;
580 
581     end loop;
582   end if;
583 
584   return 'SUCCESS';
585 exception
586   when others then
587     wf_core.context('Wf_Rule', 'SetParametersIntoParameterList',
588 		p_event.getEventName(),p_subscription_guid);
589     wf_event.setErrorInfo(p_event, 'ERROR');
590     return 'ERROR';
591 end;
592 
593 ---------------------------------------------------------------------------
594 --Bug 2193561
595 --To provide better access to worklist by non-workflow products
596 FUNCTION SendNotification (p_subscription_guid in    raw,
597                     p_event            in out nocopy wf_event_t)
598 return varchar2
599 is
600 
601   pos number := 1;
602   l_nid number;
603   l_message_type varchar2(8);
604   l_message_name varchar2(30);
605   l_recipient_role varchar2(320);
606   l_callback varchar2(240);
607   l_context varchar2(2000);
608   l_send_comment varchar2(2000);
609   l_priority number;
610   l_due_date date;
611 
612   l_parameter_list wf_parameter_list_t;
613   l_pname varchar2(30);
614   l_pvalue varchar2(2000);
615   l_result  varchar2(100);
616 
617 begin
618   --Bug 3520032
619   --Call setParametersIntoParameterList to set the subscription
620   --parameters into event parameterlist.
621   l_result := setParametersIntoParameterList(p_subscription_guid,p_event);
622 
623   --If l_result returned is ERROR also give it a try in this
624   --rule function assuming maybe it has an event parameterlist set
625 
626   l_parameter_list:= p_event.getParameterList();
627 
628   -- Get the WF_NOTIFICATION.SEND arguments
629   pos := l_parameter_list.LAST;
630   while(pos is not null) loop
631     if (l_parameter_list(pos).getName() = 'RECIPIENT_ROLE') then
632       l_recipient_role := l_parameter_list(pos).getValue();
633     elsif (l_parameter_list(pos).getName() = 'MESSAGE_TYPE') then
634       l_message_type := l_parameter_list(pos).getValue();
635     elsif (l_parameter_list(pos).getName() = 'MESSAGE_NAME') then
636       l_message_name := l_parameter_list(pos).getValue();
637     elsif (l_parameter_list(pos).getName() = 'CALLBACK') then
638       l_callback := l_parameter_list(pos).getValue();
639     elsif (l_parameter_list(pos).getName() = 'CONTEXT') then
640       l_context := l_parameter_list(pos).getValue();
641     elsif (l_parameter_list(pos).getName() = 'SEND_COMMENT') then
642       l_send_comment := l_parameter_list(pos).getValue();
643     elsif (l_parameter_list(pos).getName() = 'PRIORITY') then
644       l_priority := to_number(l_parameter_list(pos).getValue());
645     elsif (l_parameter_list(pos).getName() = 'DUE_DATE') then
646       l_due_date := to_date(l_parameter_list(pos).getValue(),'DD-MM-YYYY HH24:MI:SS');
647     end if;
648     pos := l_parameter_list.PRIOR(pos);
649   end loop;
650   l_nid:=wf_notification.send(
651     ROLE         => l_recipient_role,
652     MSG_TYPE     => l_message_type,
653     MSG_NAME     => l_message_name,
654     DUE_DATE     => l_due_date,
655     CALLBACK     => l_callback,
656     CONTEXT      => l_context,
657     SEND_COMMENT => l_send_comment,
658     PRIORITY     => l_priority);
659 
660   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
661      wf_log_pkg.string(wf_log_pkg.level_statement,
662                       'wf.plsql.WF_RULE.SendNotification.nid',
663                       'Nid:'||to_char(l_nid));
664   end if;
665 
666   --Get the Text Notification Attributes
667   pos := l_parameter_list.LAST;
668   while(pos is not null) loop
669     if (l_parameter_list(pos).getName() NOT IN
670         ('RECIPIENT_ROLE','MESSAGE_TYPE','MESSAGE_NAME','CALLBACK',
671         'CONTEXT','SEND_COMMENT','PRIORITY','DUE_DATE')) then
672         wf_notification.setAttrText ( NID=>l_nid ,
673             ANAME=>l_parameter_list(pos).getName(),
674             AVALUE=>l_parameter_list(pos).getValue());
675     end if;
676     pos := l_parameter_list.PRIOR(pos);
677   end loop;
678 
679   --Insert the notification id into the parameter list
680   p_event.Addparametertolist('#NID',l_nid);
681 
682   wf_notification.denormalize_notification(l_nid);
683   return 'SUCCESS';
684 exception
685   when others then
686     wf_core.context('Wf_Notification', 'Send_Rule', p_event.getEventName(),
687                                                 p_subscription_guid);
688     wf_event.setErrorInfo(p_event, 'ERROR');
689     return 'ERROR';
690 end;
691 
692 ---------------------------------------------------------------------
693 --Bug 2786192
694 /*
695 **
696 This rule function sets the parameterlist for the
697 the event from the subscription parameter list and subsequently
698 calls the default rule to execute the default processing
699 **
700 */
701 
702 
703 FUNCTION default_rule3(p_subscription_guid in     raw,
704                       p_event             in out nocopy wf_event_t)
705 return varchar2 is
706  l_result varchar2(30);
707  l_event_paramlist wf_parameter_list_t;
708  l_result_str varchar2(10);
709 
710 begin
711  -- Bug 10277369: Store the event parameters into a temo variable and set it back to event
712  -- after executing the default rule function
713  l_event_paramlist := p_event.getParameterList();
714  l_result := setParametersIntoParameterList(p_subscription_guid,p_event);
715  l_result_str := default_rule(p_subscription_guid, p_event);
716  p_event.setParameterList(l_event_paramlist);
717 
718  return l_result_str;
719 
720 end;
721 
722 /* Bug 2472743
723 This rule function can be used to restart multiple Workflow process waiting
724 for this event (or a null event) where the event activity has item attribute
725 named #BUSINESS_KEY which has the specified value.
726 */
727 
728 FUNCTION instance_default_rule(p_subscription_guid in    raw,
729                                p_event     in out nocopy wf_event_t)
730 return varchar2
731 is
732   out_guid  raw(16);
733   to_guid   raw(16);
734   wftype    varchar2(30);
735   wfname    varchar2(30);
736   res       varchar2(30);
737   pri       number;
738   ikey      varchar2(240);
739   lparamlist wf_parameter_list_t;
740   subparams varchar2(4000);
741   lcorrid   varchar2(240);
742   l_result varchar2(30);
743   --Define an exception to capture the resource_busy error
744   resource_busy exception;
745   pragma EXCEPTION_INIT(resource_busy,-00054);
746 
747 begin
748 
749   select out_agent_guid,to_agent_guid,wf_process_type,wf_process_name,priority
750          , parameters
751   into   out_guid, to_guid, wftype, wfname, pri, subparams
752   from   wf_event_subscriptions
753   where  guid = p_subscription_guid;
754 
755   l_result := setParametersIntoParameterList(p_subscription_guid,p_event);
756 
757   --The resource busy exception could be raise here
758   wf_engine.event2(event_message => p_event);
759 
760   -- Route --
761   /** single consumer queues do not need a To Agent  **/
762   if (out_guid is not null) then
763     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
764        wf_log_pkg.string(wf_log_pkg.level_statement,
765                         'wf.plsql.WF_RULE.instance_default_rule.route',
766                         'Routing...');
767     end if;
768     p_event.From_Agent := wf_event.newAgent(out_guid);
769     p_event.To_Agent := wf_event.newAgent(to_guid);
770     p_event.Priority := pri;
771     p_event.Send_Date := nvl(p_event.getSendDate(),sysdate);
772 
773     wf_event.send(p_event);
774   end if;
775 
776   -- Debug --
777   if (wf_log_pkg.wf_debug_flag = TRUE) then
778     begin
779       res := wf_rule.log(p_subscription_guid, p_event);
780     exception
781       when others then null;
782     end;
783   end if;
784 
785   return 'SUCCESS';
786 exception
787   when resource_busy then
788     --Raise the error to the caller to handle it
789     raise;
790   when others then
791     wf_core.context('Wf_Rule', 'Instance_Default_Rule', p_event.getEventName(),
792                                                 p_subscription_guid);
793     wf_event.setErrorInfo(p_event, 'ERROR');
794     return 'ERROR';
795 end;
796 
797 /*
798 ** default_rule_or - Executes default_rule only if the subscription contains
799 **                 a parameter that is in the event parameter list.
800 */
801 FUNCTION default_rule_or(p_subscription_guid in     raw,
802                        p_event             in out nocopy wf_event_t)
803          return varchar2 is
804 begin
805 
806   if (WF_EVENT_FUNCTIONS_PKG.SubParamInEvent(p_subscription_guid, p_event,'ANY')) then
807     return (default_rule(p_subscription_guid, p_event));
808 
809 
810   else
811     return 'SUCCESS';
812 
813   end if;
814 
815 end;
816 
817 /*
818 ** Default_Generate - This function is a generic event generation function
819 **                    that will generate an XML document based on the
820 **                    p_event_name, p_event_key and the p_parameter_list.
821 */
822 function Default_Generate(p_event_name in varchar2,
823                           p_event_key in varchar2,
824                           p_parameter_list in wf_parameter_list_t)
825    return clob
826 is
827    doc CLOB;
828    l_str varchar2(32000);
829    generateTime varchar2(200);
830 
831    timeMask varchar2(100) := 'mm-dd-rr hh:mi:ss';
832 
833 begin
834 
835    select to_char(sysdate, timeMask)
836    into generateTime
837    from sys.dual;
838 
839    dbms_lob.createTemporary(doc, true, DBMS_LOB.CALL);
840 
841    l_str := '<?xml version="1.0"?>'||wf_core.newLine;
842 
843    l_str := l_str||'<BUSINESSEVENT event-name="'||p_event_name||
844             '" key="'||p_event_key||'">'||wf_core.newline;
845 
846    l_str := l_str||'<GENERATETIME mask="'||timeMask||'">'||'<![CDATA['||
847             generateTime||']]></GENERATETIME>'||wf_core.newline;
848 
849    if p_parameter_list is not null and p_parameter_list.COUNT > 0 then
850       l_str := l_str||'<PARAMETERS count="'||
851                       trim(to_char(p_parameter_list.COUNT))||'">'||
852                       wf_core.newline;
853 
854       -- Write out the buffer to date since the next section could be
855       -- larger.
856       dbms_lob.writeAppend(lob_loc => doc,
857                            amount => length(l_str),
858                            buffer => l_str);
859 
860 
861       -- Loop through the parameter structure, outputting each parameter in turn.
862 
863       for p in 1..p_parameter_list.COUNT loop
864          l_str := '<PARAMETER parameter-name="'||p_parameter_list(p).getName()||
865                   '"><![CDATA['||p_parameter_list(p).getValue()||
866                   ']]></PARAMETER>';
867          dbms_lob.writeAppend(lob_loc => doc,
868                               amount => length(l_str),
869                               buffer => l_str);
870 
871       end loop;
872 
873       l_str := '</PARAMETERS>'||wf_core.newline;
874 
875    end if;
876 
877    l_str := l_str||'</BUSINESSEVENT>'||wf_core.newLine;
878 
879    dbms_lob.writeAppend(lob_loc => doc,
880                         amount => length(l_str),
881                         buffer => l_str);
882 
883    return doc;
884 
885 exception
886 when others then
887    wf_core.context('WF_RULE', 'Default_Generate', p_event_name, p_event_key);
888    raise;
889 end Default_Generate;
890 
891 
892 
893 end WF_RULE;