DBA Data[Home] [Help]

PACKAGE BODY: APPS.ECX_RULE

Source


1 package body ECX_RULE as
2 -- $Header: ECXRULEB.pls 120.4.12020000.2 2013/03/21 11:20:38 jmaddila ship $
3 --
4 --
5 -- rule (PUBLIC)
6 --   Standard XML Gateway Subscription rule function
7 -- IN:
8 --   p_subscription_guid - GUID of Subscription to be processed
9 --   p_event             - Event to be processes
10 -- NOTE:
11 
12 saved_fnd_runtime_debug  pls_integer;
13 
14 function outbound_rule(
15                         p_subscription_guid in	   raw,
16                         p_event		   in out nocopy wf_event_t
17                       ) return varchar2
18 is
19   transaction_type     	varchar2(240);
20   transaction_subtype   varchar2(240);
21   party_id	      	varchar2(240);
22   party_site_id	      	varchar2(240);
23   party_type            varchar2(200); --Bug #2183619
24   document_number       varchar2(240);
25   resultout             boolean;
26   retcode		pls_integer;
27   errmsg		varchar2(2000);
28   debug_level           varchar2(2000);
29   i_debug_level         pls_integer;
30   parameterList         varchar2(200);
31   ecx_exception_type    varchar2(200);
32   l_module              varchar2(2000);
33 
34   cursor c_debug_level
35   is select parameters
36        from wf_event_subscriptions
37       where guid = p_subscription_guid;
38 
39 
40 begin
41   -- initialize declared variables
42   l_module := 'ecx.plsql.ecx_rule.outbound_rule';
43 
44   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
45     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
46        'outbound_rule');
47   end if;
48   ecx_exception_type := null;
49 
50   transaction_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
51   transaction_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
52   party_id := p_event.getValueForParameter('ECX_PARTY_ID');
53   party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
54   document_number := p_event.getValueForParameter('ECX_DOCUMENT_ID');
55   -- Bug #2183619
56   party_type := p_event.getValueForParameter('ECX_PARTY_TYPE');
57   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
58     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
59        'Guid: ' ||  p_subscription_guid);
60     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
61        'Transaction Type ' || transaction_type);
62     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
63        'Transaction Subtype ' || transaction_subtype);
64     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
65        'Party Id ' || party_id);
66     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
67        'Party Site Id ' || party_site_id);
68     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
69        'Document Id ' || document_number);
70   --Bug #2183619
71     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
72        'party_type' || party_type);
73   end if;
74 
75   open c_debug_level;
76   fetch c_debug_level into debug_level;
77   close c_debug_level;
78 
79   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
80     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
81        'Debug Level ' || debug_level);
82   end if;
83 
84   i_debug_level := wf_event_functions_pkg.subscriptionparameters(debug_level, 'ECX_DEBUG_LEVEL');
85 
86   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
87     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
88        'Debug Level from subscription' || i_debug_level);
89   end if;
90 
91   begin
92       ecx_document.isDeliveryRequired
93       (
94          transaction_type,
95 	 transaction_subtype,
96 	 party_id,
97 	 party_site_id,
98 	 party_type, --Bug #2183619
99          resultout,
100          retcode,
101          errmsg
102       );
103 
104       parameterList := wf_rule.setParametersIntoParameterList(p_subscription_guid, p_event);
105 
106 
107       --Return status of Default rule
108       if (resultout) then
109           return(wf_rule.default_rule(p_subscription_guid,p_event));
110       else
111           wf_event.setErrorInfo(p_event,'WARNING');
112           -- MLS
113           p_event.setErrorMessage(retcode||':'||ecx_debug.getMessage(errmsg,
114                                                 ecx_utils.i_errparams));
115           if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
116             wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
117                'Resultout is FALSE- no delivery required.');
118           end if;
119           return 'WARNING';
120       end if;
121   exception
122     when ecx_document.ecx_no_party_setup then
123         if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
124           wf_log_pkg.string(wf_log_pkg.level_error, l_module,'No Party Setup');
125         end if;
126         ecx_exception_type := 'ecx_no_party_setup';
127         wf_core.token('ECX_PARTY_ID', party_id);
128         wf_core.token('ECX_PARTY_SITE_ID', party_site_id);
129         wf_core.token('ECX_TRANSACTION_TYPE', transaction_type);
130         wf_core.token('ECX_TRANSACTION_SUBTYPE', transaction_subtype);
131         wf_core.raise('ECX_NO_PARTY_SETUP');
132     when ecx_document.ecx_delivery_setup_error then
133         if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
134           wf_log_pkg.string(wf_log_pkg.level_error, l_module,'Delivery Setup Error');
135         end if;
136         ecx_exception_type := 'ecx_delivery_setup_error';
137         wf_core.token('ECX_PARTY_ID', party_id);
138         wf_core.token('ECX_PARTY_SITE_ID', party_site_id);
139         wf_core.token('ECX_TRANSACTION_TYPE', transaction_type);
140         wf_core.token('ECX_TRANSACTION_SUBTYPE', transaction_subtype);
141         wf_core.raise('ECX_DELIVERY_SETUP_ERROR');
142     when ecx_utils.program_exit then
143         if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
144           wf_log_pkg.string(wf_log_pkg.level_error, l_module,'Program Exit');
145         end if;
146         ecx_exception_type := 'program_exit';
147         -- Get the MLS message
148         wf_core.token('ECX_ERRMSG',ecx_debug.getMessage(ecx_utils.i_errbuf,
149                                                         ecx_utils.i_errparams));
150         wf_core.raise('ECX_PROGRAM_EXIT');
151     when others then
152         wf_core.raise('ECX_EVENT_ERROR');
153   end;
154 
155 exception
156   when others then
157     	Wf_Core.Context('ECX_RULE', 'OUTBOUND_RULE', p_event.getEventName(), p_subscription_guid);
158         if(ecx_exception_type = 'ecx_no_party_setup') OR
159           (ecx_exception_type = 'ecx_delivery_setup_error') OR
160           (ecx_exception_type = 'program_exit') then
161                  wf_event.setErrorInfo(p_event,'WARNING');
162                  return 'WARNING';
163         end if;
164 	wf_event.setErrorInfo(p_event,'ERROR');
165         if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
166           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
167              'ERRMSG ' || errmsg);
168           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
169              'Unexpected Error');
170         end if;
171 	return 'ERROR';
172 end outbound_rule;
173 
174 
175 -- Inbound_Rule (PUBLIC)
176 --   Standard XML Gateway Subscription rule function
177 -- IN:
178 --   p_subscription_guid - GUID of Subscription to be processed
179 --   p_event             - Event to be processes
180 -- NOTE: Determines the Inbound Transaction Queue
181 --
182 -- Standard inbound_rule function
183 
184 function inbound_rule(
185 		       p_subscription_guid  in      raw,
186 	               p_event	            in out nocopy wf_event_t
187 		     ) return varchar2
188 is
189 
190   l_transaction_type    varchar2(240);
191   l_transaction_subtype varchar2(240);
192   l_standard_code       varchar2(2000);
193   l_standard_type       varchar2(2000);
194   i_queue_name		varchar2(2000);
195   v_ect_inengobj        system.ecx_inengobj;
196   v_enqueueoptions      dbms_aq.enqueue_options_t;
197   v_messageproperties   dbms_aq.message_properties_t;
198   v_msgid               raw(16);
199   v_msgid_out           raw(16);
200   i_trigger_id          number;
201   debug_level           pls_integer;
202   l_party_site_id       varchar2(200);   --Bug #2183619
203   invalid_tp_setup      exception;
204   l_tp_header_id        number;
205   r_myparams 			varchar2(4000);
206   r_transaction_type 		varchar2(4000);
207   r_transaction_subtype 	varchar2(4000);
208   r_party_site_id		varchar2(4000);
209   r_dbg 			pls_integer;
210   r_debug 			varchar2(2000);
211   l_module                      varchar2(2000);
212 
213 begin
214   -- initialize declared variables
215   l_module := 'ecx.plsql.ecx_rule.inbound_rule';
216   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
217     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
218        'inbound_rule');
219   end if;
220   debug_level := 0;
221   r_dbg :=0;
222 
223   v_msgid := p_event.getValueForParameter('ECX_MSGID');
224   i_trigger_id := p_event.getValueForParameter('ECX_TRIGGER_ID');
225 
226   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
227     wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'MsgId '|| v_msgid);
228     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
229        'TriggerId '|| i_trigger_id);
230   end if;
231 
232   ecx_debug.setErrorInfo(10,10, 'ECX_PROCESSING_RULE');
233   ecx_errorlog.inbound_trigger(
234                                  i_trigger_id,
235                                  v_msgid,
236                                  null,
237                                  ecx_utils.i_ret_code,
238                                  ecx_utils.i_errbuf
239                                );
240 
241 
242   -- Get the data from the Event
243   l_transaction_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
244   l_transaction_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
245   l_standard_code := p_event.getValueForParameter('ECX_MESSAGE_STANDARD');
246   l_standard_type := p_event.getValueForParameter('ECX_MESSAGE_TYPE');
247   -- we should pickup from event subscription and not from p_event VS
248   --debug_level := p_event.getValueForParameter('ECX_DEBUG_LEVEL');
249   --Party_site_id added for  Bug #2183619
250   l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
251   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
252     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
253        'Transaction Type ' || l_transaction_type);
254     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
255        'Transaction Subtype '||l_transaction_subtype);
256     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
257        'Standard Code ' || l_standard_code);
258     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
259        'Debug Mode ' || debug_level);
260   --Party_site_id added for Bug #2183619
261     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
262        'party_site_id  ' || l_party_site_id );
263   end if;
264 
265   /*start of changes for Bug#2183619*/
266   BEGIN
267 
268      select  queue_name ,
269              tp_header_id
270      into    i_queue_name,
271              l_tp_header_id
272      from    ecx_ext_processes eep,
273              ecx_standards es,
274              ecx_tp_details etd
275      where   eep.ext_type       = l_transaction_type
276      and     eep.ext_subtype    = l_transaction_subtype
277      and     eep.direction      = 'IN'
278      and     eep.standard_id    = es.standard_id
279      and     es.standard_code   = l_standard_code
280      and     es.standard_type   = l_standard_type
281      and     etd.ext_process_id = eep.ext_process_id
282      and     etd.source_tp_location_code  = l_party_site_id;
283    Exception
284    WHEN NO_DATA_FOUND THEN
285 		raise invalid_tp_setup;
286    END;
287    /*End of changes for bug #2183619*/
288 
289    if i_queue_name is not null then
290      if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
291        wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
292           'Queue name '||i_queue_name);
293      end if;
294 
295      -- Enqueue the Event on the Inbound Engine
296      -- Determine the Debug Mode from the Subscription parameter for the transaction processing
297    	begin
298   		-- Get Params
299 		select 	parameters
300 		into 	r_myparams
301 		from 	wf_event_subscriptions
302 		where 	guid = p_subscription_guid;
303 	exception
304 	when others then
305                 if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
306 		  wf_log_pkg.string(wf_log_pkg.level_error, l_module,
307                      'Error in selecting parameters.');
308                 end if;
309 		ecx_debug.setErrorInfo(1,30,'ECX_PARAM_SELECT_ERROR',
310 		                            'p_guid',
311                                              p_subscription_guid);
312 		wf_event.setErrorInfo(p_event,'ERROR');
313 		p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
314                                                      ecx_utils.i_errparams));
315 		p_event.event_key := v_msgid;
316                 p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
317                 p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
318                 p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
319                 p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
320                 p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
321                 p_event.addParameterToList('ECX_DIRECTION','IN');
322 		return 'ERROR';
323 	end;
324 
325         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
326   	  wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
327              'Parameters from Subscription are:'||r_myparams);
328         end if;
329 	begin
330 		-- Get debug level, and default if not found
331 		if r_myparams is not null
332 		then
333                    r_dbg := wf_event_functions_pkg.subscriptionparameters(r_myparams,'ECX_DEBUG_LEVEL');
334                    if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
335                        wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
336                           'ECX_DEBUG_LEVEL:'||r_dbg||'XX');
337                    end if;
338 
339                    if r_dbg is null
340                    then
341                       debug_level := 0;
342                    else
343                       debug_level := r_dbg;
344                    end if;
345 		end if;
346 	exception
347 	when others then
348            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
349 		wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
350                    'Warning in getting DEBUG Parameters.'||SQLERRM);
351            end if;
352 	end;
353 
354 	begin
355 		-- Get TRANSACTION_TYPE
356 		if r_myparams is not null
357 		then
358                   r_transaction_type := wf_event_functions_pkg.subscriptionparameters(r_myparams,'ECX_TRANSACTION_TYPE');
359                    if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
360                      wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
361                         'ECX_TRANSACTION_TYPE:'||r_transaction_type);
362                    end if;
363 		end if;
364 	exception
365 	when others then
366             if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
367 		wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
368                    'Warning in getting TRANSACTION_TYPE Parameters.');
369             end if;
370 	end;
371 
372 	begin
373 		-- Get TRANSACTION_SUBTYPE
374 		if r_myparams is not null
375 		then
376                   r_transaction_subtype :=
377                     wf_event_functions_pkg.subscriptionparameters(r_myparams,'ECX_TRANSACTION_SUBTYPE');
378                   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
379                     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
380                        'ECX_TRANSACTION_SUBTYPE:'||r_transaction_subtype);
381                   end if;
382 		end if;
383 	exception
384 	when others then
385            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
386 		wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
387                    'Warning in getting TRANSACTION_SUBTYPE Parameters.');
388            end if;
389 	end;
390 
391 	begin
392 		-- Get PARTY_SITE_ID
393           if r_myparams is not null then
394             r_party_site_id 	:= wf_event_functions_pkg.subscriptionparameters(r_myparams,'ECX_PARTY_SITE_ID');
395             if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
396               wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
397                  'ECX_PARTY_SITE_ID:'||r_party_site_id);
398             end if;
399           end if;
400 	exception
401 	when others then
402           if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
403             wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
404                'Warning in getting PARTY_SITE_ID Parameter.');
405           end if;
406 	end;
407 
408 	if r_transaction_type is not null
409 	then
410            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
411 		wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
412                    'r_transaction_type'||r_transaction_type||'XX'||l_transaction_type);
413 		wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
414                    'r_dbg'||r_dbg);
415            end if;
416            if r_transaction_type = l_transaction_type
417            then
418 		debug_level := r_dbg;
419            end if;
420            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
421                wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
422                   'debug_level'||debug_level);
423 
424                -- Check for transaction_subtype also
425                wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
426                   'r_transaction_subtype'||r_transaction_subtype||
427                   'XX'|| l_transaction_subtype);
428                wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
429                   'r_dbg'||r_dbg);
430            end if;
431            if r_transaction_subtype is not null
432            then
433 		if r_transaction_subtype = l_transaction_subtype
434 		then
435 	 	  debug_level := r_dbg;
436 		end if;
437            end if;
438            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
439              wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
440                 'debug_level'||debug_level);
441 
442              -- Check for party_site_id also
443              wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
444                 'r_party_site_id'||r_party_site_id);
445              wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
446                 'l_party_site_id'||l_party_site_id);
447              wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
448                 'r_dbg'||r_dbg);
449            end if;
450            if r_party_site_id is not null
451            then
452               if r_party_site_id = l_party_site_id
453               then
454                  debug_level := r_dbg;
455               end if;
456            end if;
457            if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
458              wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
459                 'debug_level'||debug_level);
460            end if;
461 	end if;
462 
463      v_ect_inengobj := system.ecx_inengobj(v_msgid,debug_level);
464 
465      begin
466 	dbms_aq.enqueue(queue_name         => i_queue_name,
467 		        enqueue_options    => v_enqueueoptions,
468 		        message_properties => v_messageproperties,
469 		        payload            => v_ect_inengobj,
470 		        msgid              => v_msgid_out );
471 
472          ecx_debug.setErrorInfo(10,10, 'ECX_PROCESSING_MESSAGE');
473          ecx_errorlog.inbound_trigger(
474                                  i_trigger_id,
475                                  v_msgid,
476                                  v_msgid_out,
477                                  ecx_utils.i_ret_code,
478                                  ecx_utils.i_errbuf
479                                );
480         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
481           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
482              'Processed successfully.');
483         end if;
484 	return 'SUCCESS';
485       exception
486 	  when others then
487                 ecx_debug.setErrorInfo(1,30,'ECX_PROCESSING_ENQ_ERROR',
488                                             'p_queue_name',
489                                              i_queue_name);
490                 ecx_errorlog.inbound_trigger(
491                                  i_trigger_id,
492                                  v_msgid,
493                                  v_msgid_out,
494                                  ecx_utils.i_ret_code,
495                                  ecx_utils.i_errbuf,
496                                  ecx_utils.i_errparams);
497 
498               if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
499 	        wf_log_pkg.string(wf_log_pkg.level_error, l_module,
500                    'Error enqueuing to processing engine');
501               end if;
502               wf_event.setErrorInfo(p_event,'ERROR');
503               --p_event.setErrorMessage('Error enqueuing to processing engine: ' || i_queue_name);
504               -- MLS
505               p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
506                                                         ecx_utils.i_errparams));
507               p_event.event_key := v_msgid;
508               p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
509               p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
510               p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
511               p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
512               p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
513               p_event.addParameterToList('ECX_DIRECTION','IN');
514               return 'ERROR';
515       end;
516    else
517         ecx_debug.setErrorInfo(1,30,'ECX_NO_PROCESSING_QUEUE');
518         ecx_errorlog.inbound_trigger(
519                                  i_trigger_id,
520                                  v_msgid,
521                                  v_msgid_out,
522                                  ecx_utils.i_ret_code,
523                                  ecx_utils.i_errbuf
524                                     );
525         if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
526           wf_log_pkg.string(wf_log_pkg.level_error, l_module,
527              'Queue name not found');
528         end if;
529         wf_event.setErrorInfo(p_event,'ERROR');
530         -- MLS
531         --p_event.setErrorMessage('Unable to determine processing engine queue.');
532         p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
533                                                      ecx_utils.i_errparams));
534         p_event.event_key := v_msgid;
535         p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
536         p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
537         p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
538         p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
539         p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
540         p_event.addParameterToList('ECX_DIRECTION','IN');
541 
542         return 'ERROR';
543    end if;
544 exception
545 /* start of changes for bug #2183619*/
546 when too_many_rows then
547 
548         ecx_debug.setErrorInfo(2,30, 'ECX_MANY_PROCESSING_QUEUES');
549         ecx_errorlog.inbound_trigger(
550                                  i_trigger_id,
551                                  v_msgid,
552                                  v_msgid_out,
553                                  ecx_utils.i_ret_code,
554                                  ecx_utils.i_errbuf
555                                );
556         if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
557           wf_log_pkg.string(wf_log_pkg.level_error, l_module,
558              'More than one row resulted while querying the queue name.');
559         end if;
560         wf_event.setErrorInfo(p_event,'ERROR');
561         -- MLS
562         --p_event.setErrorMessage('More than one row resulted while querying the Queue Name.');
563         p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
564                                                      ecx_utils.i_errparams));
565         p_event.event_key := v_msgid;
566         p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
567         p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
568         p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
569         p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
570         p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
571         p_event.addParameterToList('ECX_DIRECTION','IN');
572 
573         return 'ERROR';
574 when invalid_tp_setup then
575 
576         ecx_debug.setErrorInfo(2,30,'ECX_RULE_INVALID_TP_SETUP',
577                                     'p_standard_code',
578                                      l_standard_code,
579                                      'p_transaction_type',
580                                      l_transaction_type,
581                                      'p_transaction_subtype',
582                                      l_transaction_subtype,
583                                      'p_party_site_id',
584                                      l_party_site_id);
585 
586 
587         ecx_errorlog.inbound_trigger(
588                                  i_trigger_id,
589                                  v_msgid,
590                                  v_msgid_out,
591                                  ecx_utils.i_ret_code,
592                                  ecx_utils.i_errbuf,
593                                  ecx_utils.i_errparams);
594 
595       if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
596         wf_log_pkg.string(wf_log_pkg.level_error, l_module,
597                           'The Standard:'||l_standard_code||
598                           ' Transaction Type:'||l_transaction_type||
599                           ' SubType:'||l_transaction_subtype||
600                           ' Location Code'||l_party_site_id||
601                           ' is not enabled in the XML Gateway Server. Pls check your Setup');
602         end if;
603         wf_event.setErrorInfo(p_event,'ERROR');
604         -- MLS
605         p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
606                                                      ecx_utils.i_errparams));
607         p_event.event_key := v_msgid;
608         p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
609         p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
610         p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
611         p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
612         p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
613         p_event.addParameterToList('ECX_DIRECTION','IN');
614 
615         return 'ERROR';
616 /* end of changes for bug #2183619*/
617 when others then
618       ecx_debug.setErrorInfo(2,30, 'ECX_IN_RULE_PROCESING_ERROR');
619       ecx_errorlog.inbound_trigger(
620                                  i_trigger_id,
621                                  v_msgid,
622                                  v_msgid_out,
623                                  ecx_utils.i_ret_code,
624                                  ecx_utils.i_errbuf
625                                );
626       if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
627         wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
628            'Error in processing inbound rule.');
629       end if;
630       wf_event.setErrorInfo(p_event,'ERROR');
631       p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
632                                                    ecx_utils.i_errparams));
633       p_event.event_key := v_msgid;
634       p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
635       p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
636       p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
637       p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
638       p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
639       p_event.addParameterToList('ECX_DIRECTION','IN');
640 
641       return 'ERROR';
642 end inbound_rule;
643 
644 -- Inbound_Rule2 (PUBLIC)
645 --   Another XML Gateway Subscription rule function (does no validation)
646 --	quick and dirty, useful for a2a.
647 -- IN:
648 --   p_subscription_guid - GUID of Subscription to be processed
649 --   p_event             - Event to be processes
650 --
651 -- Another inbound_rule function
652 
653 function inbound_rule2 (p_subscription_guid  in      raw,
654                p_event in out nocopy wf_event_t) return varchar2 is
655 
656   myparams varchar2(4000);
657   dbg pls_integer;
658   mapcode Varchar2(240);
659   l_module  Varchar2(2000);
660 
661 begin
662   l_module := 'ecx.plsql.ecx_rule.inbound_rule2';
663   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
664     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
665        'inbound_rule2');
666   end if;
667   begin
668     -- Get Params
669     select parameters
670       into myparams
671       from wf_event_subscriptions
672      where guid = p_subscription_guid;
673   exception
674     when others then
675       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
676        wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
677           'Error in selecting parameters.');
678       end if;
679        wf_event.setErrorInfo(p_event,'ERROR');
680        return 'ERROR';
681   end;
682 
683   begin
684     -- Get debug level, and default if not found
685     dbg := wf_event_functions_pkg.
686 		subscriptionparameters(myparams,'ECX_DEBUG_LEVEL');
687 
688     if dbg is null then
689 	dbg := 0;
690     end if;
691   exception
692     when others then
693        if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
694          wf_log_pkg.string(wf_log_pkg.level_error, l_module,
695             'Error in getting ECX_DEBUG_LEVEL.');
696        end if;
697        wf_event.setErrorInfo(p_event,'ERROR');
698        return 'ERROR';
699   end;
700 
701   begin
702     -- get map code
703     mapcode := wf_event_functions_pkg.
704 		subscriptionparameters(myparams,'ECX_MAP_CODE');
705   exception
706     when others then
707        if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
708          wf_log_pkg.string(wf_log_pkg.level_error, l_module,
709                            'Error in getting ECX_MAP_CODE.');
710        end if;
711        wf_event.setErrorInfo(p_event,'ERROR');
712        return 'ERROR';
713   end;
714 
715   -- process
716   ECX_STANDARD.ProcessXmlCover(i_map_code=> mapcode,
717      			       i_inpayload => p_event.GetEventData(),
718      			       i_debug_level => dbg);
719 
720   return 'SUCCESS';
721 
722 exception
723     when others then
724        if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
725          wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
726             'Error in processing inbound rule2.');
727        end if;
728        wf_event.setErrorInfo(p_event,'ERROR');
729        return 'ERROR';
730 end inbound_rule2;
731 
732 
733 function exec_wf (p_subscription_guid in     raw,
734                   p_event             in out nocopy wf_event_t,
735                   p_wftype            in     varchar2,
736                   p_wfname            in     varchar2
737                   ) return varchar2
738 is
739   l_out_guid   raw(16);
740   l_to_guid    raw(16);
741   l_wftype     varchar2(30);
742   l_wfname     varchar2(30);
743   l_res        varchar2(30);
744   l_pri        number;
745   l_ikey       varchar2(240);
746   l_paramlist  wf_parameter_list_t;
747   l_subparams  varchar2(4000);
748   l_lcorrid    varchar2(240);
749   l_map_code   varchar2(30);
750   l_std_type   varchar2(30);
751   l_std_code   varchar2(30);
752   l_module     varchar2(2000);
753 
754 begin
755   l_module := 'ecx.plsql.ecx_rule.CreateTPMessage.exec_wf';
756   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
757     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
758        'exec_wf');
759   end if;
760 
761   select out_agent_guid, to_agent_guid, wf_process_type, wf_process_name,
762          priority, parameters, map_code, standard_type, standard_code
763   into   l_out_guid, l_to_guid, l_wftype, l_wfname, l_pri, l_subparams,
764          l_map_code, l_std_type, l_std_code
765   from   wf_event_subscriptions
766   where  guid = p_subscription_guid;
767 
768   if (p_wftype is not null) then
769     l_wftype := p_wftype;
770     l_wfname := p_wfname;
771   end if;
772 
773   -- Workflow --
774   if (l_wftype is not null) then
775 
776     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
777        wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
778                         'Calling wf_engine.event()');
779     end if;
780 
781     l_paramlist := p_event.Parameter_List;
782     wf_event.AddParameterToList('SUB_GUID',  p_subscription_guid, l_paramlist);
783 
784     if p_event.getValueForParameter('ECX_MAP_CODE') is null then
785        if l_map_code is not null then
786           wf_event.AddParameterToList('ECX_MAP_CODE', l_map_code, l_paramlist);
787        end if;
788     end if;
789 
790     if p_event.getValueForParameter('ECX_MESSAGE_STANDARD') is null then
791        if l_std_code is not null then
792           wf_event.AddParameterToList('ECX_MESSAGE_STANDARD', l_std_code, l_paramlist);
793        end if;
794     end if;
795 
796     if p_event.getValueForParameter('ECX_MESSAGE_TYPE') is null then
797        if l_std_type is not null then
798           wf_event.AddParameterToList('ECX_MESSAGE_TYPE', l_std_type, l_paramlist);
799        end if;
800     end if;
801 
802     p_event.parameter_List := l_paramlist;
803 
804     if (l_wftype = 'WFERROR') then
805       select to_char(WF_ERROR_PROCESSES_S.nextval) into l_ikey from dual;
806     else
807       l_ikey := nvl(p_event.Correlation_ID, p_event.Event_Key);
808     end if;
809 
810     wf_engine.event(
811        itemtype      => l_wftype,
812        itemkey       => l_ikey,
813        process_name  => l_wfname,
814        event_message => p_event);
815   end if;
816 
817   -- Route --
818   /** single consumer queues do not need a To Agent  **/
819   if (l_out_guid is not null) then
820     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
821        wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
822                         'Routing...' || l_out_guid);
823     end if;
824 
825     p_event.From_Agent := wf_event.newAgent(l_out_guid);
826     p_event.To_Agent   := wf_event.newAgent(l_to_guid);
827     p_event.Priority   := l_pri;
828     p_event.Send_Date  := nvl(p_event.getSendDate(),sysdate);
829 
830     wf_event.send(p_event);
831   end if;
832 
833   -- Debug --
834   if (wf_log_pkg.wf_debug_flag = TRUE) then
835     begin
836       l_res := wf_rule.log(p_subscription_guid, p_event);
837     exception
838       when others then null;
839     end;
840   end if;
841 
842   return 'SUCCESS';
843 exception
844   when others then
845     wf_core.context('ECX_RULE', 'Exec_WF', p_event.getEventName(),
846                                            p_subscription_guid);
847     wf_event.setErrorInfo(p_event, 'ERROR');
848     return 'ERROR';
849 end exec_wf;
850 
851 
852 procedure setEventParam(
853   p_msgid                   in raw,
854   p_transaction_type        in Varchar2,
855   p_transaction_subtype     in Varchar2,
856   p_message_code            in Varchar2,
857   p_message_type            in Varchar2,
858   p_party_id                in Varchar2,
859   p_party_site_id           in Varchar2,
860   p_protocol_type           in Varchar2,
861   p_protocol_address        in Varchar2,
862   p_username                in Varchar2,
863   p_password                in Varchar2,
864   p_attribute1              in Varchar2,
865   p_attribute2              in Varchar2,
866   p_attribute3              in Varchar2,
867   p_attribute4              in Varchar2,
868   p_attribute5              in Varchar2,
869   p_internal_control_number in pls_integer,
870   p_debug_mode              in varchar2,
871   p_logfile                 in varchar2,
872   p_status                  in varchar2,
873   p_time_stamp              in varchar2,
874   p_document_number         in varchar2,
875   p_event                   in out nocopy wf_event_t)
876 is
877 
878   l_party_type          Varchar2(20);
879   l_module              Varchar2(2000);
880 begin
881 
882   if (p_event is null or
883       p_event.event_name is null) then
884       return;
885   end if;
886 
887   l_module := 'ecx.plsql.ecx_rule.ReceiveTPMessage.setEventParam';
888   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
889     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
890        'setEventParam');
891   end if;
892 
893   if ecx_utils.g_snd_tp_id is not null then
894   begin
895     select party_type
896     into l_party_type
897     from ecx_tp_headers
898     where tp_header_id=ecx_utils.g_snd_tp_id;
899   exception
900     when others then
901       if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
902         wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
903                           'Unable to determine party_type:' || l_party_type);
904       end if;
905     end;
906   end if;
907 
908   p_event.addParameterToList('ECX_DEBUG_LEVEL', p_debug_mode);
909   p_event.addParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
910   p_event.addParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
911   p_event.addParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
912   p_event.addParameterToList('ECX_ERROR_TYPE',ecx_utils.error_type);
913   p_event.addParameterToList('ECX_DIRECTION', ecx_utils.g_direction);
914   p_event.addParameterToList('ECX_TRANSACTION_TYPE', ecx_utils.g_transaction_type);
915   p_event.addParameterToList('ECX_TRANSACTION_SUBTYPE', ecx_utils.g_transaction_subtype);
916   p_event.addParameterToList('ECX_TP_HEADER_ID', ecx_utils.g_snd_tp_id);
917   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
918     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
919        'Tp Header Id: '|| ecx_utils.g_snd_tp_id);
920     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
921        'Mesage Type from doclogs: '|| p_message_type);
922   end if;
923   p_event.addParameterToList('ECX_MSGID', p_msgid);
924   p_event.addParameterToList('ECX_MESSAGE_TYPE', p_message_type);
925   p_event.addParameterToList('ECX_MESSAGE_STANDARD', p_message_code);
926   p_event.addParameterToList('ECX_DOCUMENT_ID', p_document_number);
927   p_event.addParameterToList('ECX_PARTY_ID', p_party_id);
928   p_event.addParameterToList('ECX_PARTY_SITE_ID', p_party_site_id);
929   p_event.addParameterToList('ECX_PARTY_TYPE', l_party_type);
930   p_event.addParameterToList('ECX_PROTOCOL_TYPE', p_protocol_type);
931   p_event.addParameterToList('ECX_PROTOCOL_ADDRESS', p_protocol_address);
932   p_event.addParameterToList('ECX_USERNAME', p_username);
933   p_event.addParameterToList('ECX_PASSWORD', p_password);
934   p_event.addParameterToList('ECX_ATTRIBUTE1', p_attribute1);
935   p_event.addParameterToList('ECX_ATTRIBUTE2', p_attribute2);
936   p_event.addParameterToList('ECX_ATTRIBUTE3', p_attribute3);
937   p_event.addParameterToList('ECX_ATTRIBUTE4', p_attribute4);
938   p_event.addParameterToList('ECX_ATTRIBUTE5', p_attribute5);
939   p_event.addParameterToList('ECX_LOGFILE', p_logfile);
940   p_event.addParameterToList('ECX_ICN', p_internal_control_number);
941   p_event.addParameterToList('ECX_STATUS', p_status);
942   p_event.addParameterToList('ECX_TIME_STAMP', p_time_stamp);
943 
944 exception
945   when others then
946     raise;
947 
948 end setEventParam;
949 
950 
951 procedure processTPMessage (
952   p_msgid               in raw,
953   p_debug_mode          in varchar2,
954   p_process_id          in varchar2)
955 is
956 
957   l_module    Varchar2(2000);
958 
959 begin
960 
961   l_module := 'ecx.plsql.ecx_rule.ReceiveTPMessage.processTPMessage';
962   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
963     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
964        'Starting inbound processing.');
965   end if;
966   savepoint before_processing;
967 
968   ecx_inbound_trig.wrap_validate_message
969   (
970     p_msgid,
971     p_debug_mode
972   );
973   /*bug 16442475:New Project Inbound Messages processing at java*/
974   if ECX_UTIL_API.isInJavaProcessEnabled then
975     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
976       wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
977          'Event raised for Inbound messgae processing at middle tier(i.e. java).');
978     end if;
979     /*ecx_debug.setErrorInfo has been called at ecx_inbound_trig.raiseEvent(to the the current message)
980     as documnet number is not available here*/
981   else
982     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
983     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
984        'Completed inbound processing.');
985     end if;
986 
987     if(ecx_utils.g_ret_code = 1 ) then
988       ecx_debug.setErrorInfo(1, 10, 'ECX_MESSAGE_PROCESSED_WARNING');
989     else
990       ecx_debug.setErrorInfo(0, 10, 'ECX_MESSAGE_PROCESSED');
991     end if;
992   end if;
993 
994   ecx_errorlog.inbound_engine(p_process_id,
995                               ecx_utils.i_ret_code,
996                               ecx_utils.i_errbuf);
997 
998 exception
999   when others then
1000     rollback to before_processing;
1001     if(ecx_utils.i_ret_code = 0) then
1002        ecx_utils.i_ret_code := 2;
1003        ecx_utils.i_errbuf := SQLERRM;
1004     end if;
1005 
1006     if (ecx_utils.i_errbuf is null) then
1007         ecx_utils.i_errbuf := SQLERRM;
1008     end if;
1009 
1010     if (ecx_utils.error_type is null)
1011        -- OR
1012        --(ecx_utils.error_type = 10 )
1013     then
1014         ecx_utils.error_type := 30;
1015     end if;
1016 
1017     begin
1018       ecx_errorlog.inbound_engine(p_process_id,
1019                                   ecx_utils.i_ret_code,
1020                                   ecx_utils.i_errbuf);
1021     exception
1022       when others then
1023         if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
1024           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1025              'Error While Saving the Log: ' ||p_msgid);
1026           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1027              'Logging Error Message: '|| substr(SQLERRM,1,200));
1028         end if;
1029     end;
1030 
1031     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
1032       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1033          'Exception in inbound processing.');
1034       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1035          'Error Type: '|| ecx_utils.error_type);
1036       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1037          'Return Code: '|| ecx_utils.i_ret_code);
1038       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1039          'Error Message: '|| ecx_utils.i_errbuf);
1040     end if;
1041     raise;
1042 end processTPMessage;
1043 
1044 
1045 function isTPEnabled (
1046   p_transaction_type     in varchar2,
1047   p_transaction_subtype  in varchar2,
1048   p_standard_code        in varchar2,
1049   p_standard_type        in varchar2,
1050   p_party_site_id        in varchar2,
1051   x_queue_name           out nocopy varchar2,
1052   x_tp_header_id         out nocopy number)
1053 
1054 return boolean is
1055 
1056 begin
1057   x_queue_name := null;
1058   x_tp_header_id := -1;
1059 
1060   select  queue_name ,
1061           tp_header_id
1062   into    x_queue_name,
1063           x_tp_header_id
1064   from    ecx_ext_processes eep,
1065           ecx_standards es,
1066           ecx_tp_details etd
1067   where   eep.ext_type       = p_transaction_type
1068   and     eep.ext_subtype    = p_transaction_subtype
1069   and     eep.direction      = 'IN'
1070   and     eep.standard_id    = es.standard_id
1071   and     es.standard_code   = p_standard_code
1072   and     es.standard_type   = p_standard_type
1073   and     etd.ext_process_id = eep.ext_process_id
1074   and     etd.source_tp_location_code  = p_party_site_id;
1075 
1076   return true;
1077 Exception
1078   when others then
1079      return false;
1080 end isTPEnabled;
1081 
1082 
1083 procedure enqueue_msg (
1084   p_event        in  wf_event_t,
1085   p_queue_name   in  Varchar2,
1086   p_msgid_in     in  raw,
1087   x_msgid_out    out nocopy raw)
1088 is
1089 
1090   l_ecx_inengobj       system.ecx_inengobj;
1091   l_debug_mode         pls_integer;
1092   l_enqueueoptions      dbms_aq.enqueue_options_t;
1093   l_messageproperties   dbms_aq.message_properties_t;
1094   l_msgid               raw(16);
1095   l_module              Varchar2(2000);
1096 
1097 begin
1098   if (p_queue_name is null) then
1099     return;
1100   end if;
1101 
1102   l_module := 'ecx.plsq.ecx_rule.ReceiveTPMessage.enqueue_msg';
1103   l_debug_mode := p_event.getValueForParameter('ECX_DEBUG_LEVEL');
1104 
1105   l_ecx_inengobj := system.ecx_inengobj(p_msgid_in, l_debug_mode);
1106 
1107   dbms_aq.enqueue(queue_name         => p_queue_name,
1108                   enqueue_options    => l_enqueueoptions,
1109                   message_properties => l_messageproperties,
1110                   payload            => l_ecx_inengobj,
1111                   msgid              => x_msgid_out );
1112 
1113   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1114     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1115       'Enqueued to '||p_queue_name|| ' successfully.');
1116   end if;
1117 
1118 exception
1119   when others then
1120     if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
1121       wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1122          'Error enqueuing to processing engine');
1123     end if;
1124     ecx_debug.setErrorInfo(1,30,'ECX_PROCESSING_ENQ_ERROR',
1125                            'p_queue_name', p_queue_name);
1126     raise;
1127 end enqueue_msg;
1128 
1129 
1130 procedure convertEcxToFndDebug (
1131   p_debug_mode      in  pls_integer)
1132 is
1133 
1134 begin
1135 
1136   if (p_debug_mode = 0) or
1137      (p_debug_mode = 1) then
1138     fnd_log.g_current_runtime_level := fnd_log.level_unexpected;
1139   elsif (p_debug_mode = 2) then
1140     fnd_log.g_current_runtime_level := fnd_log.level_procedure;
1141   elsif (p_debug_mode = 3) then
1142     fnd_log.g_current_runtime_level := fnd_log.level_statement;
1143   end if;
1144 
1145 end convertEcxToFndDebug;
1146 
1147 
1148 procedure setDebugMode (
1149   p_subscription_guid   in raw,
1150   p_transaction_type    in varchar2,
1151   p_transaction_subtype in varchar2,
1152   p_party_site_id       in varchar2,
1153   p_debug_mode          in pls_integer)
1154 is
1155 
1156   l_debug_level         varchar2(2);
1157   l_transaction_type    varchar2(2000);
1158   l_transaction_subtype varchar2(2000);
1159   l_party_site_id       varchar2(2000);
1160   l_sub_param           varchar2(4000);
1161 
1162   cursor c_sub_param is
1163   select parameters
1164   from wf_event_subscriptions
1165   where guid = p_subscription_guid;
1166 
1167 begin
1168   -- always takes event debug mode over the subscription debug mode.
1169   if p_debug_mode is not null then
1170      convertEcxToFndDebug(p_debug_mode);
1171      return;
1172   end if;
1173 
1174   -- get the debug level from subscription.
1175   open c_sub_param;
1176   fetch c_sub_param into l_sub_param;
1177   close c_sub_param;
1178 
1179   -- nothing is set to the subscription, so set the default debug mode.
1180   if (l_sub_param is null) then
1181     return;
1182   end if;
1183 
1184   l_debug_level := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1185                    'ECX_DEBUG_LEVEL');
1186 
1187   -- if no ecx_debug_level is specified, then it should use the profile option.
1188   if l_debug_level is null then
1189     return;
1190   end if;
1191 
1192   -- At this point, there is some debug level is set at subscription.
1193   l_transaction_type := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1194                         'ECX_TRANSACTION_TYPE');
1195 
1196   l_transaction_subtype := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1197                           'ECX_TRANSACTION_SUBTYPE');
1198 
1199   l_party_site_id := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1200                      'ECX_PARTY_SITE_ID');
1201 
1202   if (l_transaction_type is null) and  (l_party_site_id is null) and
1203      (l_transaction_subtype is null) then
1204       convertEcxToFndDebug(l_debug_level);
1205       return;
1206   end if;
1207 
1208   if (l_transaction_type is not null) and
1209      (l_transaction_type <> p_transaction_type) then
1210       return;
1211   end if;
1212 
1213   if (l_transaction_subtype is not null) and
1214      (l_transaction_subtype <> p_transaction_subtype) then
1215       return;
1216   end if;
1217 
1218   if (l_party_site_id is not null) and
1219      (l_party_site_id <> p_party_site_id) then
1220       return;
1221   end if;
1222 
1223   convertEcxToFndDebug(l_debug_level);
1224 
1225 exception
1226   when others then
1227     raise;
1228 end setDebugMode;
1229 
1230 --
1231 -- TPPreProcessing
1232 -- Standard XML Gateway Subscription to perform User to TP and TP to
1233 -- transaction validation and also to perform the initial logging
1234 -- IN:
1235 --   p_subscription_guid - GUID of Subscription to be processed
1236 --   p_event             - Event to be processed
1237 --
1238 
1239 function TPPreProcessing(
1240   p_subscription_guid  in      raw,
1241   p_event              in out nocopy wf_event_t
1242 ) return varchar2
1243 is
1244   l_msgid                   RAW(16);
1245   l_process_id              RAW(16);
1246   l_tran_type               varchar2(240);
1247   l_tran_subtype            varchar2(240);
1248   l_std_code                varchar2(2000);
1249   l_std_type                varchar2(2000);
1250   l_party_id                varchar2(256);
1251   l_party_site_id           varchar2(256);
1252   l_party_type              varchar2(50);
1253   l_protocol_type           varchar2(500);
1254   l_protocol_address       ecx_tp_details.protocol_address%TYPE;
1255   l_username                varchar2(500);
1256   l_password                varchar2(500);
1257   l_attribute1              varchar2(500);
1258   l_attribute2              varchar2(500);
1259   l_attribute3              varchar2(500);
1260   l_attribute4              varchar2(500);
1261   l_attribute5              varchar2(500);
1262   l_internal_control_number pls_integer;
1263   l_trigger_id              number;
1264   l_doc_number              varchar2(256);
1265   l_tp_user            varchar2(500);
1266   p_tp_header_id            varchar2(500);
1267 
1268   p_ret_code                pls_integer;
1269   p_errmsg                  varchar2(200);
1270   l_debug_mode              pls_integer;
1271   l_payload                 clob;
1272   is_valid		    varchar2(20);
1273   l_module		    varchar2(200);
1274   rule_exception            exception;
1275 
1276   cursor c_ecx_trigger_id is
1277   select ecx_trigger_id_s.NEXTVAL
1278   from dual;
1279 
1280 begin
1281 
1282   l_module := 'ecx.plsql.ecx_rule.TPPreProcessing';
1283   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level)
1284   then
1285     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1286        'Starting TPPreProcessing rule function.');
1287   end if;
1288 
1289   -- validate the inbound request
1290   l_msgid := p_event.getValueForParameter('#MSG_ID');
1291   if l_msgid is null
1292   then
1293     l_msgid := p_event.getValueForParameter('ECX_MSGID');
1294      if l_msgid is null
1295      then
1296        return 'ERROR';
1297      end if;
1298   end if;
1299 
1300   open c_ecx_trigger_id;
1301   fetch c_ecx_trigger_id into l_trigger_id;
1302   close c_ecx_trigger_id;
1303   p_event.AddParameterToList('ECX_TRIGGER_ID', l_trigger_id);
1304 
1305   l_process_id := p_event.getValueForParameter('ECX_PROCESS_ID');
1306   if(l_process_id is null)
1307   then
1308     l_process_id := l_msgid;
1309   end if;
1310 
1311   l_tran_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
1312   l_tran_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
1313   l_std_type := p_event.getValueForParameter('ECX_MESSAGE_TYPE');
1314   l_std_code := p_event.getValueForParameter('ECX_MESSAGE_STANDARD');
1315   l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1316   l_tp_user := p_event.getValueForParameter('ECX_TP_USER');
1317 
1318   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1319     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1320        'l_tran_type: ' || l_tran_type);
1321     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1322        'l_tran_subtype: ' || l_tran_subtype);
1323     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1324        'l_std_type: ' || l_std_type);
1325     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1326        'l_std_code: ' || l_std_code);
1327     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1328        'l_party_site_id: ' || l_party_site_id);
1329     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1330        'l_tp_user: ' || l_tp_user);
1331     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1332        'l_trigger_id: ' || l_trigger_id);
1333     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1334        'l_process_id: ' || l_process_id);
1335   end if;
1336 
1337   is_valid := ecx_trading_partner_pvt.validateTPUser(
1338   	p_transaction_type     => l_tran_type,
1339 	p_transaction_subtype  => l_tran_subtype,
1340 	p_standard_code        => l_std_code,
1341 	p_standard_type        => l_std_type,
1342 	p_party_site_id        => l_party_site_id,
1343 	p_user_name            => l_tp_user,
1344 	x_tp_header_id         => p_tp_header_id,
1345 	retcode                => p_ret_code,
1346 	errmsg                 => p_errmsg);
1347 
1348   if(is_valid = 'N')
1349   then
1350     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1351     then
1352       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1353                         ecx_debug.getMessage(ecx_utils.i_errbuf,
1354                                              ecx_utils.i_errparams));
1355     end if;
1356     raise rule_exception;
1357   end if;
1358 
1359   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level)
1360   then
1361     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1362                       'Validated User and TP Setup.');
1363   end if;
1364 
1365   -- retrieve the event parameters and perform the logging.
1366   l_doc_number := p_event.getValueForParameter('ECX_DOCUMENT_NUMBER');
1367   if(l_doc_number is null)
1368   then
1369     l_doc_number := l_trigger_id;
1370   end if;
1371 
1372   l_party_type := p_event.getValueForParameter('ECX_PARTY_TYPE');
1373   l_protocol_type := p_event.getValueForParameter('ECX_PROTOCOL_TYPE');
1374   l_protocol_address := p_event.getValueForParameter('ECX_PROTOCOL_ADDRESS');
1375   l_attribute1 := p_event.getValueForParameter('ECX_ATTRIBUTE1');
1376   l_attribute2 := p_event.getValueForParameter('ECX_ATTRIBUTE2');
1377   l_attribute3 := p_event.getValueForParameter('ECX_ATTRIBUTE3');
1378   l_attribute4 := p_event.getValueForParameter('ECX_ATTRIBUTE4');
1379   l_attribute5 := p_event.getValueForParameter('ECX_ATTRIBUTE5');
1380 
1381   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level)
1382   then
1383     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1384        'l_party_type: ' || l_party_type);
1385     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1386        'l_protocol_type: ' || l_protocol_type);
1387     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1388        'l_protocol_address: ' || l_protocol_address);
1389     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1390        'l_attribute1: ' || l_attribute1);
1391     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1392        'l_attribute2: ' || l_attribute2);
1393     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1394        'l_attribute3: ' || l_attribute3);
1395     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1396        'l_attribute4: ' || l_attribute4);
1397     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1398        'l_attribute5: ' || l_attribute5);
1399   end if;
1400 
1401   -- this to make sure that the payload has something before
1402   -- insert into the doclog table.
1403   l_payload := p_event.getEventData();
1404   if (l_payload is null) or
1405      (dbms_lob.getlength(l_payload) = 0) then
1406     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1407     then
1408       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1409                         'Payload is null');
1410      end if;
1411      return 'ERROR';
1412   end if;
1413 
1414   ecx_errorlog.log_receivemessage (
1415   	caller			=> 'JMS Queue',
1416 	status_text		=> 'SUCCESS',
1417 	err_msg 		=> null,
1418 	receipt_msgid 		=> l_msgid,
1419 	trigger_id 		=> l_trigger_id,
1420 	message_type 		=> l_std_type,
1421 	message_standard 	=> l_std_code,
1422 	transaction_type 	=> l_tran_type,
1423 	transaction_subtype 	=> l_tran_subtype,
1424 	document_number 	=> l_doc_number,
1425 	partyid 		=> l_party_id,
1426 	party_site_id 		=> l_party_site_id,
1427 	party_type 		=> l_party_type,
1428 	protocol_type 		=> l_protocol_type,
1429 	protocol_address 	=> l_protocol_address,
1430 	username 		=> l_username,
1431 	encrypt_password 	=> l_password,
1432 	attribute1 		=> l_attribute1,
1433 	attribute2 		=> l_attribute2,
1434 	attribute3 		=> l_attribute3,
1435 	attribute4 		=> l_attribute4,
1436 	attribute5 		=> l_attribute5,
1437 	payload 		=> l_payload,
1438         returnval 		=> p_errmsg);
1439 
1440   if(p_errmsg <> 'SUCCESS')
1441   then
1442     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1443     then
1444       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1445                         ecx_debug.getMessage(ecx_utils.i_errbuf,
1446                                              ecx_utils.i_errparams));
1447     end if;
1448     raise rule_exception;
1449   end if;
1450 
1451 return 'SUCCESS';
1452 exception
1453   when others then
1454     ecx_errorlog.inbound_trigger(
1455     	l_trigger_id,
1456         l_msgid,
1457         l_process_id,
1458         ecx_utils.i_ret_code,
1459         ecx_utils.i_errbuf,
1460         ecx_utils.i_errparams
1461     );
1462 
1463     wf_event.setErrorInfo(p_event,'ERROR');
1464     p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
1465                                                  ecx_utils.i_errparams));
1466     p_event.event_key := l_msgid;
1467     p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
1468     p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
1469     p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
1470     p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
1471     p_event.AddParameterToList('ECX_TP_HEADER_ID', p_tp_header_id);
1472     p_event.AddParameterToList('ECX_TRANSACTION_TYPE',l_tran_type);
1473     p_event.AddParameterToList('ECX_TRANSACTION_SUBTYPE',l_tran_subtype);
1474     p_event.AddParameterToList('ECX_PARTY_ID',l_party_id);
1475     p_event.AddParameterToList('ECX_PARTY_SITE_ID',l_party_site_id);
1476     p_event.AddParameterToList('ECX_PARTY_TYPE',l_party_type);
1477     p_event.addParameterToList('ECX_DIRECTION','IN');
1478     p_event.addParameterToList('ECX_MESSAGE_STANDARD',l_std_code);
1479     p_event.addParameterToList('ECX_MESSAGE_TYPE', l_std_type);
1480     p_event.addParameterToList('ECX_DOCUMENT_NUMBER', l_doc_number);
1481     return 'ERROR';
1482 end TPPreProcessing;
1483 
1484 
1485 
1486 -- ReceiveTPMessage
1487 --   Standard XML Gateway Subscription rule function for inbound B2B
1488 -- IN:
1489 --   p_subscription_guid - GUID of Subscription to be processed
1490 --   p_event             - Event to be processes
1491 -- NOTE: Determines the Inbound Transaction Queue
1492 --
1493 -- Standard B2B inbound function
1494 
1495 function ReceiveTPMessage(
1496   p_subscription_guid  in      raw,
1497   p_event              in out nocopy wf_event_t
1498 ) return varchar2
1499 is
1500   l_queue_name              varchar2(80);
1501   l_agent_name              varchar2(30);
1502   l_process_id              varchar2(200);
1503   l_msgid                   RAW(16);
1504   l_transaction_type        varchar2(240);
1505   l_transaction_subtype     varchar2(240);
1506   l_message_code            varchar2(2000);
1507   l_message_type            varchar2(2000);
1508   l_party_id                varchar2(256);
1509   l_party_site_id           varchar2(256);
1510   l_party_type              varchar2(50);
1511   l_protocol_type           varchar2(500);
1512   l_protocol_address       ecx_tp_details.protocol_address%TYPE;
1513   l_username                varchar2(500);
1514   l_password                varchar2(500);
1515   l_attribute1              varchar2(500);
1516   l_attribute2              varchar2(500);
1517   l_attribute3              varchar2(500);
1518   l_attribute4              varchar2(500);
1519   l_attribute5              varchar2(500);
1520   l_internal_control_number pls_integer;
1521   l_trigger_id              number;
1522   l_retcode                 pls_integer;
1523   l_retmsg                  varchar2(200);
1524   l_debug_mode              pls_integer;
1525   l_document_number         varchar2(256);
1526   l_payload                 clob;
1527   l_logfile                 Varchar2(80);
1528   l_msgid_out               raw(16);
1529   l_status                  Varchar2(256);
1530   l_time_stamp              date;
1531   l_tp_header_id            number;
1532   l_direction               Varchar2(20);
1533   l_param_list              wf_parameter_list_t;
1534   l_module                  Varchar2(2000);
1535   rule_exception            exception;
1536   isSavedToDocLogs          boolean;
1537   isTPCheck                 boolean;
1538   err_msg                   varchar2(500);
1539   cursor c_ecx_trigger_id is
1540   select ecx_trigger_id_s.NEXTVAL
1541   from dual;
1542 
1543 begin
1544   l_module := 'ecx.plsql.ecx_rule.ReceiveTPMessage';
1545   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
1546     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1547        'Starting ReceiveTPMessage rule function.');
1548   end if;
1549 
1550   -- initialize declared variables
1551   l_msgid := null;
1552   l_retcode := 0;
1553   l_retmsg := null;
1554 
1555   saved_fnd_runtime_debug := fnd_log.g_current_runtime_level;
1556 
1557   -- Get the data from the Event
1558   l_msgid := p_event.getValueForParameter('#MSG_ID');
1559   if l_msgid is null then
1560     l_msgid := p_event.getValueForParameter('ECX_MSGID');
1561   end if;
1562   l_debug_mode := p_event.getValueForParameter('ECX_DEBUG_LEVEL');
1563   l_trigger_id := p_event.getValueForParameter('ECX_TRIGGER_ID');
1564   l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1565   l_process_id := p_event.getValueForParameter('ECX_PROCESS_ID');
1566 
1567   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1568     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1569        'l_process_id: ' || l_process_id);
1570   end if;
1571 
1572   -- The first time to call the rule function should have
1573   -- a party_site_id and the second time won't have the party_id info.
1574   if (l_process_id is null) then
1575     l_process_id := l_msgid;
1576     begin
1577       select trigger_id into l_trigger_id
1578       from ecx_inbound_logs
1579       where msgid = l_msgid;
1580       if (l_trigger_id is null) then
1581 	 raise null_trigger_id;
1582       end if;
1583     exception
1584       when others then
1585         open c_ecx_trigger_id;
1586         fetch c_ecx_trigger_id into l_trigger_id;
1587         close c_ecx_trigger_id;
1588     end;
1589     p_event.AddParameterToList('ECX_TRIGGER_ID', l_trigger_id);
1590     ecx_debug.setErrorInfo(10, 10,'ECX_DEQUEUED_LOGGED');
1591     isTPCheck := false;
1592 
1593   else
1594     ecx_debug.setErrorInfo(10,10, 'ECX_PROCESSING_RULE');
1595     isTPCheck := true;
1596     if l_trigger_id is null then
1597       begin
1598         select max(trigger_id) into l_trigger_id
1599         from ecx_inbound_logs
1600         where msgid = l_msgid;
1601       exception
1602         when others then
1603             open c_ecx_trigger_id;
1604             fetch c_ecx_trigger_id into l_trigger_id;
1605             close c_ecx_trigger_id;
1606       end;
1607     end if;
1608   end if;
1609 
1610   ecx_errorlog.inbound_trigger(
1611       l_trigger_id,
1612       l_msgid,
1613       l_process_id,
1614       ecx_utils.i_ret_code,
1615       ecx_utils.i_errbuf
1616   );
1617 
1618   l_queue_name := null;
1619 
1620   -- Debug for parameter
1621   l_param_list := p_event.getParameterList();
1622 
1623   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1624     for i in l_param_list.first..l_param_list.last loop
1625       wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1626          l_param_list(i).GetName || ':' || l_param_list(i).GetValue);
1627     end loop;
1628   end if;
1629 
1630   -- need to check if the msgid has already exists in ecx_doclogs,
1631   -- if yes, then it is the second queue calling this subscription.
1632   -- Otherwise, it is the first queue, then it need to insert an entry to doclogs.
1633   ecx_errorlog.getDocLogDetails
1634     (
1635      l_msgid,
1636      l_message_type,
1637      l_message_code,
1638      l_transaction_type,
1639      l_transaction_subtype,
1640      l_document_number,
1641      l_party_id,
1642      l_party_site_id,
1643      l_protocol_type,
1644      l_protocol_address,
1645      l_username,
1646      l_password,
1647      l_attribute1,
1648      l_attribute2,
1649      l_attribute3,
1650      l_attribute4,
1651      l_attribute5,
1652      l_logfile,
1653      l_internal_control_number,
1654      l_status,
1655      l_time_stamp,
1656      l_direction,
1657      l_retcode,
1658      l_retmsg
1659      );
1660 
1661   if (l_retcode <> 0) then
1662     isSavedToDocLogs := false;
1663   else
1664     isSavedToDocLogs := true;
1665   end if;
1666 
1667   if not isSavedToDocLogs then
1668 
1669     -- this to make sure that the payload has something before
1670     -- insert into the doclog table.
1671     l_payload := p_event.getEventData();
1672     if (l_payload is null) or
1673        (dbms_lob.getlength(l_payload) = 0) then
1674        ecx_utils.i_ret_code := l_retcode;
1675        ecx_utils.i_errbuf  := l_retmsg;
1676        raise rule_exception;
1677     end if;
1678 
1679     l_transaction_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
1680     l_transaction_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
1681     l_message_code := nvl(p_event.getValueForParameter('ECX_MESSAGE_STANDARD'), 'OAG');
1682     l_message_type := nvl(p_event.getValueForParameter('ECX_MESSAGE_TYPE'), 'XML');
1683     l_document_number := p_event.getValueForParameter('ECX_DOCUMENT_NUMBER');
1684     if(l_document_number is null) then
1685       l_document_number := l_trigger_id;
1686     end if;
1687     l_party_id := p_event.getValueForParameter('ECX_PARTY_ID');
1688     l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1689     l_party_type := p_event.getValueForParameter('ECX_PARTY_TYPE');
1690     l_protocol_type := p_event.getValueForParameter('ECX_PROTOCOL_TYPE');
1691     l_protocol_address := p_event.getValueForParameter('ECX_PROTOCOL_ADDRESS');
1692     l_username := p_event.getValueForParameter('ECX_USERNAME');
1693     l_password := p_event.getValueForParameter('ECX_PASSWORD');
1694     l_attribute1 := p_event.getValueForParameter('ECX_ATTRIBUTE1');
1695     l_attribute2 := p_event.getValueForParameter('ECX_ATTRIBUTE2');
1696     l_attribute3 := p_event.getValueForParameter('ECX_ATTRIBUTE3');
1697     l_attribute4 := p_event.getValueForParameter('ECX_ATTRIBUTE4');
1698     l_attribute5 := p_event.getValueForParameter('ECX_ATTRIBUTE5');
1699     l_internal_control_number := p_event.getValueForParameter('ECX_ICN');
1700 
1701     if l_internal_control_number is null then
1702       select ecx_inlstn_s.nextval into l_internal_control_number from dual;
1703       p_event.AddParameterToList('ECX_ICN', l_internal_control_number);
1704     end if;
1705 
1706     -- Save the Copy of the Document
1707     begin
1708       ecx_errorlog.log_document(
1709         l_retcode,
1710         l_retmsg,
1711         l_msgid,
1712         l_message_type,
1713         l_message_code,
1714         l_transaction_type,
1715         l_transaction_subtype,
1716         l_document_number,
1717         l_party_id,
1718         l_party_site_id,
1719         l_party_type,
1720         l_protocol_type,
1721         l_protocol_address,
1722         l_username,
1723         l_password,
1724         l_attribute1,
1725         l_attribute2,
1726         l_attribute3,
1727         l_attribute4,
1728         l_attribute5,
1729         l_payload,
1730         l_internal_control_number,
1731         'Message received.',
1732         'IN',
1733         null
1734       );
1735 
1736       if (l_retcode = 0) then
1737         isSavedToDocLogs := true;
1738 
1739         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1740           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1741              'Saved Message to DocLogs');
1742         end if;
1743       end if;
1744 
1745       if (l_retcode = 1) then
1746         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1747           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1748              'Return Message: '|| l_retmsg);
1749         end if;
1750       elsif (l_retcode >= 2) then
1751         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1752           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1753                ecx_debug.getMessage(ecx_utils.i_errbuf, ecx_utils.i_errparams));
1754         end if;
1755         raise rule_exception;
1756       end if;
1757     end;  -- save to doc_logs
1758   end if;
1759 
1760 --  if not (isTPCheck) then
1761 -- The tp_header_id is derived now in case of both Inbound and Transaction queues.
1762 -- Hence removed the check on isTPCheck.
1763     if not isTPEnabled(l_transaction_type, l_transaction_subtype,
1764                      l_message_code, l_message_type, l_party_site_id,
1765                      l_queue_name, l_tp_header_id) then
1766 
1767       if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
1768         wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1769                         'The Standard: '||l_message_code|| ',' ||
1770                         'Transaction Type: '||l_transaction_type|| ',' ||
1771                         'SubType:'||l_transaction_subtype|| ',' ||
1772                         'Location Code'||l_party_site_id||
1773                         ' is not enabled in the XML Gateway Server. '||
1774                         ' Pls check your Setup');
1775       end if;
1776 
1777       ecx_debug.setErrorInfo(2,30,'ECX_RULE_INVALID_TP_SETUP',
1778                              'p_standard_code', l_message_code,
1779                              'p_transaction_type', l_transaction_type,
1780                              'p_transaction_subtype', l_transaction_subtype,
1781                              'p_party_site_id', l_party_site_id);
1782 
1783        raise rule_exception;
1784     end if;
1785     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1786       wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Validated TP Setup.');
1787     end if;
1788   -- end if;
1789 -- In case the message is in Transaction queue, the message should be processed.
1790 -- Hence the queue name should be null.
1791 
1792     if (isTPCheck) then
1793        l_queue_name := null;
1794     end if;
1795 
1796   -- start to do the inbound processing and then dispatch the product
1797   -- team specified event.
1798   if (l_queue_name is null) then
1799     ecx_utils.g_direction := 'IN';
1800     ecx_utils.g_transaction_type := l_transaction_type;
1801     ecx_utils.g_transaction_subtype := l_transaction_subtype;
1802 
1803     setDebugMode (
1804       p_subscription_guid   => p_subscription_guid,
1805       p_transaction_type    => l_transaction_type,
1806       p_transaction_subtype => l_transaction_subtype,
1807       p_party_site_id       => l_party_site_id,
1808       p_debug_mode          => l_debug_mode);
1809 
1810     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1811       wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1812                       'FND Debug level is set to '
1813                       || fnd_log.g_current_runtime_level );
1814     end if;
1815     processTPMessage(l_msgid, l_debug_mode, l_process_id);
1816     if (ecx_utils.i_ret_code <> 2) then
1817        if (ecx_utils.g_event is not null and
1818           ecx_utils.g_event.event_name is not null) then
1819           p_event := ecx_utils.g_event;
1820 
1821           setEventParam(
1822           p_msgid                   => l_msgid,
1823           p_transaction_type        => l_transaction_type,
1824           p_transaction_subtype     => l_transaction_subtype,
1825           p_message_code            => l_message_code,
1826           p_message_type            => l_message_type,
1827           p_party_id                => l_party_id,
1828           p_party_site_id           => l_party_site_id,
1829           p_protocol_type           => l_protocol_type,
1830           p_protocol_address        => l_protocol_address,
1831           p_username                => l_username,
1832           p_password                => l_password,
1833           p_attribute1              => l_attribute1,
1834           p_attribute2              => l_attribute2,
1835           p_attribute3              => l_attribute3,
1836           p_attribute4              => l_attribute4,
1837           p_attribute5              => l_attribute5,
1838           p_internal_control_number => l_internal_control_number,
1839           p_debug_mode              => l_debug_mode,
1840           p_logfile                 => l_logfile,
1841           p_status                  => l_status,
1842           p_time_stamp              => l_time_stamp,
1843           p_document_number         => l_document_number,
1844           p_event                   => p_event);
1845 
1846           if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1847             wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1848                             'Dispatch ' ||p_event.event_name);
1849           end if;
1850           wf_event.dispatch('EXTERNAL', null, p_event);
1851       end if;
1852     end if;
1853   else
1854     -- enqueue to the second queue.
1855     enqueue_msg(p_event, l_queue_name, l_msgid, l_process_id);
1856     p_event.setCorrelationId(l_process_id);
1857     --p_event.setEventKey(l_process_id);
1858 
1859     ecx_debug.setErrorInfo(10, 10, 'ECX_PROCESSING_MESSAGE');
1860     ecx_errorlog.inbound_trigger(
1861                  l_trigger_id,
1862                  l_msgid,
1863                  l_process_id,
1864                  ecx_utils.i_ret_code,
1865                  ecx_utils.i_errbuf
1866                  );
1867   end if;
1868 
1869   fnd_log.g_current_runtime_level := saved_fnd_runtime_debug;
1870 
1871   if (ecx_utils.i_ret_code = 2) then
1872      if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1873         wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Finished with Error.');
1874      end if;
1875      ecx_debug.setErrorInfo(2,30, 'ECX_IN_RULE_PROCESING_ERROR');
1876      raise rule_exception;
1877   end if;
1878 
1879   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1880     wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Finished with Success.');
1881   end if;
1882   return 'SUCCESS';
1883 
1884 exception
1885   when others then
1886     fnd_log.g_current_runtime_level := saved_fnd_runtime_debug;
1887     ecx_errorlog.inbound_trigger(
1888                   l_trigger_id,
1889                   l_msgid,
1890                   l_process_id,
1891                   ecx_utils.i_ret_code,
1892                   ecx_utils.i_errbuf,
1893                   ecx_utils.i_errparams
1894                   );
1895 
1896     wf_event.setErrorInfo(p_event,'ERROR');
1897     p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
1898                                                  ecx_utils.i_errparams));
1899     if (p_event.event_key is null)
1900     then
1901       p_event.event_key := l_msgid;
1902     end if;
1903     p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
1904     p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
1905     p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
1906     p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
1907     p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
1908     p_event.AddParameterToList('ECX_TRANSACTION_TYPE',l_transaction_type);
1909     p_event.AddParameterToList('ECX_TRANSACTION_SUBTYPE',l_transaction_subtype);
1910     p_event.AddParameterToList('ECX_PARTY_ID',l_party_id);
1911     p_event.AddParameterToList('ECX_PARTY_SITE_ID',l_party_site_id);
1912     p_event.AddParameterToList('ECX_PARTY_TYPE',l_party_type);
1913     p_event.addParameterToList('ECX_DIRECTION','IN');
1914     p_event.addParameterToList('ECX_MESSAGE_STANDARD',l_message_code);
1915     p_event.addParameterToList('ECX_MESSAGE_TYPE', l_message_type);
1916     p_event.addParameterToList('ECX_DOCUMENT_ID', l_document_number);
1917 
1918     return 'ERROR';
1919 
1920 end ReceiveTPMessage;
1921 
1922 
1923 function CreateTPMessage (
1924   p_subscription_guid  in            raw,
1925   p_event              in out nocopy wf_event_t
1926 ) return varchar2
1927 is
1928 
1929   l_module   varchar2(2000);
1930 
1931 begin
1932   l_module := 'ecx.plsql.ecx_rule.CreateTPMessage';
1933   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
1934     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1935        'Starting CreateTPMessage rule function.');
1936   end if;
1937   return (exec_wf(p_subscription_guid, p_event, 'ECXSTD', 'OUTBOUND_B2B'));
1938 
1939 end CreateTPMessage;
1940 
1941 
1942 end ecx_rule;