DBA Data[Home] [Help]

PACKAGE BODY: APPS.ECX_RULE

Source


1 package body ECX_RULE as
2 -- $Header: ECXRULEB.pls 120.2.12010000.1 2008/07/25 07:32:08 appldev 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 
974   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
975     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
976        'Completed inbound processing.');
977   end if;
978 
979   if(ecx_utils.g_ret_code = 1 )
980   then
981 	ecx_debug.setErrorInfo(1, 10, 'ECX_MESSAGE_PROCESSED_WARNING');
982   else
983 	ecx_debug.setErrorInfo(0, 10, 'ECX_MESSAGE_PROCESSED');
984   end if;
985 
986   ecx_errorlog.inbound_engine(p_process_id,
987                               ecx_utils.i_ret_code,
988                               ecx_utils.i_errbuf);
989 
990 exception
991   when others then
992     rollback to before_processing;
993     if(ecx_utils.i_ret_code = 0) then
994        ecx_utils.i_ret_code := 2;
995        ecx_utils.i_errbuf := SQLERRM;
996     end if;
997 
998     if (ecx_utils.i_errbuf is null) then
999         ecx_utils.i_errbuf := SQLERRM;
1000     end if;
1001 
1002     if (ecx_utils.error_type is null)
1003        -- OR
1004        --(ecx_utils.error_type = 10 )
1005     then
1006         ecx_utils.error_type := 30;
1007     end if;
1008 
1009     begin
1010       ecx_errorlog.inbound_engine(p_process_id,
1011                                   ecx_utils.i_ret_code,
1012                                   ecx_utils.i_errbuf);
1013     exception
1014       when others then
1015         if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
1016           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1017              'Error While Saving the Log: ' ||p_msgid);
1018           wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1019              'Logging Error Message: '|| substr(SQLERRM,1,200));
1020         end if;
1021     end;
1022 
1023     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
1024       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1025          'Exception in inbound processing.');
1026       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1027          'Error Type: '|| ecx_utils.error_type);
1028       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1029          'Return Code: '|| ecx_utils.i_ret_code);
1030       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1031          'Error Message: '|| ecx_utils.i_errbuf);
1032     end if;
1033     raise;
1034 end processTPMessage;
1035 
1036 
1037 function isTPEnabled (
1038   p_transaction_type     in varchar2,
1039   p_transaction_subtype  in varchar2,
1040   p_standard_code        in varchar2,
1041   p_standard_type        in varchar2,
1042   p_party_site_id        in varchar2,
1043   x_queue_name           out nocopy varchar2,
1044   x_tp_header_id         out nocopy number)
1045 
1046 return boolean is
1047 
1048 begin
1049   x_queue_name := null;
1050   x_tp_header_id := -1;
1051 
1052   select  queue_name ,
1053           tp_header_id
1054   into    x_queue_name,
1055           x_tp_header_id
1056   from    ecx_ext_processes eep,
1057           ecx_standards es,
1058           ecx_tp_details etd
1059   where   eep.ext_type       = p_transaction_type
1060   and     eep.ext_subtype    = p_transaction_subtype
1061   and     eep.direction      = 'IN'
1062   and     eep.standard_id    = es.standard_id
1063   and     es.standard_code   = p_standard_code
1064   and     es.standard_type   = p_standard_type
1065   and     etd.ext_process_id = eep.ext_process_id
1066   and     etd.source_tp_location_code  = p_party_site_id;
1067 
1068   return true;
1069 Exception
1070   when others then
1071      return false;
1072 end isTPEnabled;
1073 
1074 
1075 procedure enqueue_msg (
1076   p_event        in  wf_event_t,
1077   p_queue_name   in  Varchar2,
1078   p_msgid_in     in  raw,
1079   x_msgid_out    out nocopy raw)
1080 is
1081 
1082   l_ecx_inengobj       system.ecx_inengobj;
1083   l_debug_mode         pls_integer;
1084   l_enqueueoptions      dbms_aq.enqueue_options_t;
1085   l_messageproperties   dbms_aq.message_properties_t;
1086   l_msgid               raw(16);
1087   l_module              Varchar2(2000);
1088 
1089 begin
1090   if (p_queue_name is null) then
1091     return;
1092   end if;
1093 
1094   l_module := 'ecx.plsq.ecx_rule.ReceiveTPMessage.enqueue_msg';
1095   l_debug_mode := p_event.getValueForParameter('ECX_DEBUG_LEVEL');
1096 
1097   l_ecx_inengobj := system.ecx_inengobj(p_msgid_in, l_debug_mode);
1098 
1099   dbms_aq.enqueue(queue_name         => p_queue_name,
1100                   enqueue_options    => l_enqueueoptions,
1101                   message_properties => l_messageproperties,
1102                   payload            => l_ecx_inengobj,
1103                   msgid              => x_msgid_out );
1104 
1105   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1106     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1107       'Enqueued to '||p_queue_name|| ' successfully.');
1108   end if;
1109 
1110 exception
1111   when others then
1112     if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
1113       wf_log_pkg.string(wf_log_pkg.level_unexpected, l_module,
1114          'Error enqueuing to processing engine');
1115     end if;
1116     ecx_debug.setErrorInfo(1,30,'ECX_PROCESSING_ENQ_ERROR',
1117                            'p_queue_name', p_queue_name);
1118     raise;
1119 end enqueue_msg;
1120 
1121 
1122 procedure convertEcxToFndDebug (
1123   p_debug_mode      in  pls_integer)
1124 is
1125 
1126 begin
1127 
1128   if (p_debug_mode = 0) or
1129      (p_debug_mode = 1) then
1130     fnd_log.g_current_runtime_level := fnd_log.level_unexpected;
1131   elsif (p_debug_mode = 2) then
1132     fnd_log.g_current_runtime_level := fnd_log.level_procedure;
1133   elsif (p_debug_mode = 3) then
1134     fnd_log.g_current_runtime_level := fnd_log.level_statement;
1135   end if;
1136 
1137 end convertEcxToFndDebug;
1138 
1139 
1140 procedure setDebugMode (
1141   p_subscription_guid   in raw,
1142   p_transaction_type    in varchar2,
1143   p_transaction_subtype in varchar2,
1144   p_party_site_id       in varchar2,
1145   p_debug_mode          in pls_integer)
1146 is
1147 
1148   l_debug_level         varchar2(2);
1149   l_transaction_type    varchar2(2000);
1150   l_transaction_subtype varchar2(2000);
1151   l_party_site_id       varchar2(2000);
1152   l_sub_param           varchar2(4000);
1153 
1154   cursor c_sub_param is
1155   select parameters
1156   from wf_event_subscriptions
1157   where guid = p_subscription_guid;
1158 
1159 begin
1160   -- always takes event debug mode over the subscription debug mode.
1161   if p_debug_mode is not null then
1162      convertEcxToFndDebug(p_debug_mode);
1163      return;
1164   end if;
1165 
1166   -- get the debug level from subscription.
1167   open c_sub_param;
1168   fetch c_sub_param into l_sub_param;
1169   close c_sub_param;
1170 
1171   -- nothing is set to the subscription, so set the default debug mode.
1172   if (l_sub_param is null) then
1173     return;
1174   end if;
1175 
1176   l_debug_level := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1177                    'ECX_DEBUG_LEVEL');
1178 
1179   -- if no ecx_debug_level is specified, then it should use the profile option.
1180   if l_debug_level is null then
1181     return;
1182   end if;
1183 
1184   -- At this point, there is some debug level is set at subscription.
1185   l_transaction_type := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1186                         'ECX_TRANSACTION_TYPE');
1187 
1188   l_transaction_subtype := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1189                           'ECX_TRANSACTION_SUBTYPE');
1190 
1191   l_party_site_id := wf_event_functions_pkg.subscriptionparameters(l_sub_param,
1192                      'ECX_PARTY_SITE_ID');
1193 
1194   if (l_transaction_type is null) and  (l_party_site_id is null) and
1195      (l_transaction_subtype is null) then
1196       convertEcxToFndDebug(l_debug_level);
1197       return;
1198   end if;
1199 
1200   if (l_transaction_type is not null) and
1201      (l_transaction_type <> p_transaction_type) then
1202       return;
1203   end if;
1204 
1205   if (l_transaction_subtype is not null) and
1206      (l_transaction_subtype <> p_transaction_subtype) then
1207       return;
1208   end if;
1209 
1210   if (l_party_site_id is not null) and
1211      (l_party_site_id <> p_party_site_id) then
1212       return;
1213   end if;
1214 
1215   convertEcxToFndDebug(l_debug_level);
1216 
1217 exception
1218   when others then
1219     raise;
1220 end setDebugMode;
1221 
1222 --
1223 -- TPPreProcessing
1224 -- Standard XML Gateway Subscription to perform User to TP and TP to
1225 -- transaction validation and also to perform the initial logging
1226 -- IN:
1227 --   p_subscription_guid - GUID of Subscription to be processed
1228 --   p_event             - Event to be processed
1229 --
1230 
1231 function TPPreProcessing(
1232   p_subscription_guid  in      raw,
1233   p_event              in out nocopy wf_event_t
1234 ) return varchar2
1235 is
1236   l_msgid                   RAW(16);
1237   l_process_id              RAW(16);
1238   l_tran_type               varchar2(240);
1239   l_tran_subtype            varchar2(240);
1240   l_std_code                varchar2(2000);
1241   l_std_type                varchar2(2000);
1242   l_party_id                varchar2(256);
1243   l_party_site_id           varchar2(256);
1244   l_party_type              varchar2(50);
1245   l_protocol_type           varchar2(500);
1246   l_protocol_address        varchar2(2000);
1247   l_username                varchar2(500);
1248   l_password                varchar2(500);
1249   l_attribute1              varchar2(500);
1250   l_attribute2              varchar2(500);
1251   l_attribute3              varchar2(500);
1252   l_attribute4              varchar2(500);
1253   l_attribute5              varchar2(500);
1254   l_internal_control_number pls_integer;
1255   l_trigger_id              number;
1256   l_doc_number              varchar2(256);
1257   l_tp_user            varchar2(500);
1258   p_tp_header_id            varchar2(500);
1259 
1260   p_ret_code                pls_integer;
1261   p_errmsg                  varchar2(200);
1262   l_debug_mode              pls_integer;
1263   l_payload                 clob;
1264   is_valid		    varchar2(20);
1265   l_module		    varchar2(200);
1266   rule_exception            exception;
1267 
1268   cursor c_ecx_trigger_id is
1269   select ecx_trigger_id_s.NEXTVAL
1270   from dual;
1271 
1272 begin
1273 
1274   l_module := 'ecx.plsql.ecx_rule.TPPreProcessing';
1275   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level)
1276   then
1277     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1278        'Starting TPPreProcessing rule function.');
1279   end if;
1280 
1281   -- validate the inbound request
1282   l_msgid := p_event.getValueForParameter('#MSG_ID');
1283   if l_msgid is null
1284   then
1285     l_msgid := p_event.getValueForParameter('ECX_MSGID');
1286      if l_msgid is null
1287      then
1288        return 'ERROR';
1289      end if;
1290   end if;
1291 
1292   open c_ecx_trigger_id;
1293   fetch c_ecx_trigger_id into l_trigger_id;
1294   close c_ecx_trigger_id;
1295   p_event.AddParameterToList('ECX_TRIGGER_ID', l_trigger_id);
1296 
1297   l_process_id := p_event.getValueForParameter('ECX_PROCESS_ID');
1298   if(l_process_id is null)
1299   then
1300     l_process_id := l_msgid;
1301   end if;
1302 
1303   l_tran_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
1304   l_tran_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
1305   l_std_type := p_event.getValueForParameter('ECX_MESSAGE_TYPE');
1306   l_std_code := p_event.getValueForParameter('ECX_MESSAGE_STANDARD');
1307   l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1308   l_tp_user := p_event.getValueForParameter('ECX_TP_USER');
1309 
1310   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1311     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1312        'l_tran_type: ' || l_tran_type);
1313     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1314        'l_tran_subtype: ' || l_tran_subtype);
1315     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1316        'l_std_type: ' || l_std_type);
1317     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1318        'l_std_code: ' || l_std_code);
1319     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1320        'l_party_site_id: ' || l_party_site_id);
1321     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1322        'l_tp_user: ' || l_tp_user);
1323     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1324        'l_trigger_id: ' || l_trigger_id);
1325     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1326        'l_process_id: ' || l_process_id);
1327   end if;
1328 
1329   is_valid := ecx_trading_partner_pvt.validateTPUser(
1330   	p_transaction_type     => l_tran_type,
1331 	p_transaction_subtype  => l_tran_subtype,
1332 	p_standard_code        => l_std_code,
1333 	p_standard_type        => l_std_type,
1334 	p_party_site_id        => l_party_site_id,
1335 	p_user_name            => l_tp_user,
1336 	x_tp_header_id         => p_tp_header_id,
1337 	retcode                => p_ret_code,
1338 	errmsg                 => p_errmsg);
1339 
1340   if(is_valid = 'N')
1341   then
1342     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1343     then
1344       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1345                         ecx_debug.getMessage(ecx_utils.i_errbuf,
1346                                              ecx_utils.i_errparams));
1347     end if;
1348     raise rule_exception;
1349   end if;
1350 
1351   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level)
1352   then
1353     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1354                       'Validated User and TP Setup.');
1355   end if;
1356 
1357   -- retrieve the event parameters and perform the logging.
1358   l_doc_number := p_event.getValueForParameter('ECX_DOCUMENT_NUMBER');
1359   if(l_doc_number is null)
1360   then
1361     l_doc_number := l_trigger_id;
1362   end if;
1363 
1364   l_party_type := p_event.getValueForParameter('ECX_PARTY_TYPE');
1365   l_protocol_type := p_event.getValueForParameter('ECX_PROTOCOL_TYPE');
1366   l_protocol_address := p_event.getValueForParameter('ECX_PROTOCOL_ADDRESS');
1367   l_attribute1 := p_event.getValueForParameter('ECX_ATTRIBUTE1');
1368   l_attribute2 := p_event.getValueForParameter('ECX_ATTRIBUTE2');
1369   l_attribute3 := p_event.getValueForParameter('ECX_ATTRIBUTE3');
1370   l_attribute4 := p_event.getValueForParameter('ECX_ATTRIBUTE4');
1371   l_attribute5 := p_event.getValueForParameter('ECX_ATTRIBUTE5');
1372 
1373   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level)
1374   then
1375     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1376        'l_party_type: ' || l_party_type);
1377     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1378        'l_protocol_type: ' || l_protocol_type);
1379     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1380        'l_protocol_address: ' || l_protocol_address);
1381     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1382        'l_attribute1: ' || l_attribute1);
1383     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1384        'l_attribute2: ' || l_attribute2);
1385     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1386        'l_attribute3: ' || l_attribute3);
1387     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1388        'l_attribute4: ' || l_attribute4);
1389     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1390        'l_attribute5: ' || l_attribute5);
1391   end if;
1392 
1393   -- this to make sure that the payload has something before
1394   -- insert into the doclog table.
1395   l_payload := p_event.getEventData();
1396   if (l_payload is null) or
1397      (dbms_lob.getlength(l_payload) = 0) then
1398     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1399     then
1400       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1401                         'Payload is null');
1402      end if;
1403      return 'ERROR';
1404   end if;
1405 
1406   ecx_errorlog.log_receivemessage (
1407   	caller			=> 'JMS Queue',
1408 	status_text		=> 'SUCCESS',
1409 	err_msg 		=> null,
1410 	receipt_msgid 		=> l_msgid,
1411 	trigger_id 		=> l_trigger_id,
1412 	message_type 		=> l_std_type,
1413 	message_standard 	=> l_std_code,
1414 	transaction_type 	=> l_tran_type,
1415 	transaction_subtype 	=> l_tran_subtype,
1416 	document_number 	=> l_doc_number,
1417 	partyid 		=> l_party_id,
1418 	party_site_id 		=> l_party_site_id,
1419 	party_type 		=> l_party_type,
1420 	protocol_type 		=> l_protocol_type,
1421 	protocol_address 	=> l_protocol_address,
1422 	username 		=> l_username,
1423 	encrypt_password 	=> l_password,
1424 	attribute1 		=> l_attribute1,
1425 	attribute2 		=> l_attribute2,
1426 	attribute3 		=> l_attribute3,
1427 	attribute4 		=> l_attribute4,
1428 	attribute5 		=> l_attribute5,
1429 	payload 		=> l_payload,
1430         returnval 		=> p_errmsg);
1431 
1432   if(p_errmsg <> 'SUCCESS')
1433   then
1434     if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level)
1435     then
1436       wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1437                         ecx_debug.getMessage(ecx_utils.i_errbuf,
1438                                              ecx_utils.i_errparams));
1439     end if;
1440     raise rule_exception;
1441   end if;
1442 
1443 return 'SUCCESS';
1444 exception
1445   when others then
1446     ecx_errorlog.inbound_trigger(
1447     	l_trigger_id,
1448         l_msgid,
1449         l_process_id,
1450         ecx_utils.i_ret_code,
1451         ecx_utils.i_errbuf,
1452         ecx_utils.i_errparams
1453     );
1454 
1455     wf_event.setErrorInfo(p_event,'ERROR');
1456     p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
1457                                                  ecx_utils.i_errparams));
1458     p_event.event_key := l_msgid;
1459     p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
1460     p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
1461     p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
1462     p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
1463     p_event.AddParameterToList('ECX_TP_HEADER_ID', p_tp_header_id);
1464     p_event.AddParameterToList('ECX_TRANSACTION_TYPE',l_tran_type);
1465     p_event.AddParameterToList('ECX_TRANSACTION_SUBTYPE',l_tran_subtype);
1466     p_event.AddParameterToList('ECX_PARTY_ID',l_party_id);
1467     p_event.AddParameterToList('ECX_PARTY_SITE_ID',l_party_site_id);
1468     p_event.AddParameterToList('ECX_PARTY_TYPE',l_party_type);
1469     p_event.addParameterToList('ECX_DIRECTION','IN');
1470     p_event.addParameterToList('ECX_MESSAGE_STANDARD',l_std_code);
1471     p_event.addParameterToList('ECX_MESSAGE_TYPE', l_std_type);
1472     p_event.addParameterToList('ECX_DOCUMENT_NUMBER', l_doc_number);
1473     return 'ERROR';
1474 end TPPreProcessing;
1475 
1476 
1477 
1478 -- ReceiveTPMessage
1479 --   Standard XML Gateway Subscription rule function for inbound B2B
1480 -- IN:
1481 --   p_subscription_guid - GUID of Subscription to be processed
1482 --   p_event             - Event to be processes
1483 -- NOTE: Determines the Inbound Transaction Queue
1484 --
1485 -- Standard B2B inbound function
1486 
1487 function ReceiveTPMessage(
1488   p_subscription_guid  in      raw,
1489   p_event              in out nocopy wf_event_t
1490 ) return varchar2
1491 is
1492   l_queue_name              varchar2(80);
1493   l_agent_name              varchar2(30);
1494   l_process_id              varchar2(200);
1495   l_msgid                   RAW(16);
1496   l_transaction_type        varchar2(240);
1497   l_transaction_subtype     varchar2(240);
1498   l_message_code            varchar2(2000);
1499   l_message_type            varchar2(2000);
1500   l_party_id                varchar2(256);
1501   l_party_site_id           varchar2(256);
1502   l_party_type              varchar2(50);
1503   l_protocol_type           varchar2(500);
1504   l_protocol_address        varchar2(2000);
1505   l_username                varchar2(500);
1506   l_password                varchar2(500);
1507   l_attribute1              varchar2(500);
1508   l_attribute2              varchar2(500);
1509   l_attribute3              varchar2(500);
1510   l_attribute4              varchar2(500);
1511   l_attribute5              varchar2(500);
1512   l_internal_control_number pls_integer;
1513   l_trigger_id              number;
1514   l_retcode                 pls_integer;
1515   l_retmsg                  varchar2(200);
1516   l_debug_mode              pls_integer;
1517   l_document_number         varchar2(256);
1518   l_payload                 clob;
1519   l_logfile                 Varchar2(80);
1520   l_msgid_out               raw(16);
1521   l_status                  Varchar2(256);
1522   l_time_stamp              date;
1523   l_tp_header_id            number;
1524   l_direction               Varchar2(20);
1525   l_param_list              wf_parameter_list_t;
1526   l_module                  Varchar2(2000);
1527   rule_exception            exception;
1528   isSavedToDocLogs          boolean;
1529   isTPCheck                 boolean;
1530   err_msg                   varchar2(500);
1531   cursor c_ecx_trigger_id is
1532   select ecx_trigger_id_s.NEXTVAL
1533   from dual;
1534 
1535 begin
1536   l_module := 'ecx.plsql.ecx_rule.ReceiveTPMessage';
1537   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
1538     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1539        'Starting ReceiveTPMessage rule function.');
1540   end if;
1541 
1542   -- initialize declared variables
1543   l_msgid := null;
1544   l_retcode := 0;
1545   l_retmsg := null;
1546 
1547   saved_fnd_runtime_debug := fnd_log.g_current_runtime_level;
1548 
1549   -- Get the data from the Event
1550   l_msgid := p_event.getValueForParameter('#MSG_ID');
1551   if l_msgid is null then
1552     l_msgid := p_event.getValueForParameter('ECX_MSGID');
1553   end if;
1554   l_debug_mode := p_event.getValueForParameter('ECX_DEBUG_LEVEL');
1555   l_trigger_id := p_event.getValueForParameter('ECX_TRIGGER_ID');
1556   l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1557   l_process_id := p_event.getValueForParameter('ECX_PROCESS_ID');
1558 
1559   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1560     wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1561        'l_process_id: ' || l_process_id);
1562   end if;
1563 
1564   -- The first time to call the rule function should have
1565   -- a party_site_id and the second time won't have the party_id info.
1566   if (l_process_id is null) then
1567     l_process_id := l_msgid;
1568     begin
1569       select trigger_id into l_trigger_id
1570       from ecx_inbound_logs
1571       where msgid = l_msgid;
1572       if (l_trigger_id is null) then
1573 	 raise null_trigger_id;
1574       end if;
1575     exception
1576       when others then
1577         open c_ecx_trigger_id;
1578         fetch c_ecx_trigger_id into l_trigger_id;
1579         close c_ecx_trigger_id;
1580     end;
1581     p_event.AddParameterToList('ECX_TRIGGER_ID', l_trigger_id);
1582     ecx_debug.setErrorInfo(10, 10,'ECX_DEQUEUED_LOGGED');
1583     isTPCheck := false;
1584 
1585   else
1586     ecx_debug.setErrorInfo(10,10, 'ECX_PROCESSING_RULE');
1587     isTPCheck := true;
1588     if l_trigger_id is null then
1589       begin
1590         select max(trigger_id) into l_trigger_id
1591         from ecx_inbound_logs
1592         where msgid = l_msgid;
1593       exception
1594         when others then
1595             open c_ecx_trigger_id;
1596             fetch c_ecx_trigger_id into l_trigger_id;
1597             close c_ecx_trigger_id;
1598       end;
1599     end if;
1600   end if;
1601 
1602   ecx_errorlog.inbound_trigger(
1603       l_trigger_id,
1604       l_msgid,
1605       l_process_id,
1606       ecx_utils.i_ret_code,
1607       ecx_utils.i_errbuf
1608   );
1609 
1610   l_queue_name := null;
1611 
1612   -- Debug for parameter
1613   l_param_list := p_event.getParameterList();
1614 
1615   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1616     for i in l_param_list.first..l_param_list.last loop
1617       wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1618          l_param_list(i).GetName || ':' || l_param_list(i).GetValue);
1619     end loop;
1620   end if;
1621 
1622   -- need to check if the msgid has already exists in ecx_doclogs,
1623   -- if yes, then it is the second queue calling this subscription.
1624   -- Otherwise, it is the first queue, then it need to insert an entry to doclogs.
1625   ecx_errorlog.getDocLogDetails
1626     (
1627      l_msgid,
1628      l_message_type,
1629      l_message_code,
1630      l_transaction_type,
1631      l_transaction_subtype,
1632      l_document_number,
1633      l_party_id,
1634      l_party_site_id,
1635      l_protocol_type,
1636      l_protocol_address,
1637      l_username,
1638      l_password,
1639      l_attribute1,
1640      l_attribute2,
1641      l_attribute3,
1642      l_attribute4,
1643      l_attribute5,
1644      l_logfile,
1645      l_internal_control_number,
1646      l_status,
1647      l_time_stamp,
1648      l_direction,
1649      l_retcode,
1650      l_retmsg
1651      );
1652 
1653   if (l_retcode <> 0) then
1654     isSavedToDocLogs := false;
1655   else
1656     isSavedToDocLogs := true;
1657   end if;
1658 
1659   if not isSavedToDocLogs then
1660 
1661     -- this to make sure that the payload has something before
1662     -- insert into the doclog table.
1663     l_payload := p_event.getEventData();
1664     if (l_payload is null) or
1665        (dbms_lob.getlength(l_payload) = 0) then
1666        ecx_utils.i_ret_code := l_retcode;
1667        ecx_utils.i_errbuf  := l_retmsg;
1668        raise rule_exception;
1669     end if;
1670 
1671     l_transaction_type := p_event.getValueForParameter('ECX_TRANSACTION_TYPE');
1672     l_transaction_subtype := p_event.getValueForParameter('ECX_TRANSACTION_SUBTYPE');
1673     l_message_code := nvl(p_event.getValueForParameter('ECX_MESSAGE_STANDARD'), 'OAG');
1674     l_message_type := nvl(p_event.getValueForParameter('ECX_MESSAGE_TYPE'), 'XML');
1675     l_document_number := p_event.getValueForParameter('ECX_DOCUMENT_NUMBER');
1676     if(l_document_number is null) then
1677       l_document_number := l_trigger_id;
1678     end if;
1679     l_party_id := p_event.getValueForParameter('ECX_PARTY_ID');
1680     l_party_site_id := p_event.getValueForParameter('ECX_PARTY_SITE_ID');
1681     l_party_type := p_event.getValueForParameter('ECX_PARTY_TYPE');
1682     l_protocol_type := p_event.getValueForParameter('ECX_PROTOCOL_TYPE');
1683     l_protocol_address := p_event.getValueForParameter('ECX_PROTOCOL_ADDRESS');
1684     l_username := p_event.getValueForParameter('ECX_USERNAME');
1685     l_password := p_event.getValueForParameter('ECX_PASSWORD');
1686     l_attribute1 := p_event.getValueForParameter('ECX_ATTRIBUTE1');
1687     l_attribute2 := p_event.getValueForParameter('ECX_ATTRIBUTE2');
1688     l_attribute3 := p_event.getValueForParameter('ECX_ATTRIBUTE3');
1689     l_attribute4 := p_event.getValueForParameter('ECX_ATTRIBUTE4');
1690     l_attribute5 := p_event.getValueForParameter('ECX_ATTRIBUTE5');
1691     l_internal_control_number := p_event.getValueForParameter('ECX_ICN');
1692 
1693     if l_internal_control_number is null then
1694       select ecx_inlstn_s.nextval into l_internal_control_number from dual;
1695       p_event.AddParameterToList('ECX_ICN', l_internal_control_number);
1696     end if;
1697 
1698     -- Save the Copy of the Document
1699     begin
1700       ecx_errorlog.log_document(
1701         l_retcode,
1702         l_retmsg,
1703         l_msgid,
1704         l_message_type,
1705         l_message_code,
1706         l_transaction_type,
1707         l_transaction_subtype,
1708         l_document_number,
1709         l_party_id,
1710         l_party_site_id,
1711         l_party_type,
1712         l_protocol_type,
1713         l_protocol_address,
1714         l_username,
1715         l_password,
1716         l_attribute1,
1717         l_attribute2,
1718         l_attribute3,
1719         l_attribute4,
1720         l_attribute5,
1721         l_payload,
1722         l_internal_control_number,
1723         'Message received.',
1724         'IN',
1725         null
1726       );
1727 
1728       if (l_retcode = 0) then
1729         isSavedToDocLogs := true;
1730 
1731         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1732           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1733              'Saved Message to DocLogs');
1734         end if;
1735       end if;
1736 
1737       if (l_retcode = 1) then
1738         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1739           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1740              'Return Message: '|| l_retmsg);
1741         end if;
1742       elsif (l_retcode >= 2) then
1743         if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1744           wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1745                ecx_debug.getMessage(ecx_utils.i_errbuf, ecx_utils.i_errparams));
1746         end if;
1747         raise rule_exception;
1748       end if;
1749     end;  -- save to doc_logs
1750   end if;
1751 
1752 --  if not (isTPCheck) then
1753 -- The tp_header_id is derived now in case of both Inbound and Transaction queues.
1754 -- Hence removed the check on isTPCheck.
1755     if not isTPEnabled(l_transaction_type, l_transaction_subtype,
1756                      l_message_code, l_message_type, l_party_site_id,
1757                      l_queue_name, l_tp_header_id) then
1758 
1759       if (wf_log_pkg.level_error >= fnd_log.g_current_runtime_level) then
1760         wf_log_pkg.string(wf_log_pkg.level_error, l_module,
1761                         'The Standard: '||l_message_code|| ',' ||
1762                         'Transaction Type: '||l_transaction_type|| ',' ||
1763                         'SubType:'||l_transaction_subtype|| ',' ||
1764                         'Location Code'||l_party_site_id||
1765                         ' is not enabled in the XML Gateway Server. '||
1766                         ' Pls check your Setup');
1767       end if;
1768 
1769       ecx_debug.setErrorInfo(2,30,'ECX_RULE_INVALID_TP_SETUP',
1770                              'p_standard_code', l_message_code,
1771                              'p_transaction_type', l_transaction_type,
1772                              'p_transaction_subtype', l_transaction_subtype,
1773                              'p_party_site_id', l_party_site_id);
1774 
1775        raise rule_exception;
1776     end if;
1777     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1778       wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Validated TP Setup.');
1779     end if;
1780   -- end if;
1781 -- In case the message is in Transaction queue, the message should be processed.
1782 -- Hence the queue name should be null.
1783 
1784     if (isTPCheck) then
1785        l_queue_name := null;
1786     end if;
1787 
1788   -- start to do the inbound processing and then dispatch the product
1789   -- team specified event.
1790   if (l_queue_name is null) then
1791     ecx_utils.g_direction := 'IN';
1792     ecx_utils.g_transaction_type := l_transaction_type;
1793     ecx_utils.g_transaction_subtype := l_transaction_subtype;
1794 
1795     setDebugMode (
1796       p_subscription_guid   => p_subscription_guid,
1797       p_transaction_type    => l_transaction_type,
1798       p_transaction_subtype => l_transaction_subtype,
1799       p_party_site_id       => l_party_site_id,
1800       p_debug_mode          => l_debug_mode);
1801 
1802     if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1803       wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1804                       'FND Debug level is set to '
1805                       || fnd_log.g_current_runtime_level );
1806     end if;
1807     processTPMessage(l_msgid, l_debug_mode, l_process_id);
1808     if (ecx_utils.i_ret_code <> 2) then
1809        if (ecx_utils.g_event is not null and
1810           ecx_utils.g_event.event_name is not null) then
1811           p_event := ecx_utils.g_event;
1812 
1813           setEventParam(
1814           p_msgid                   => l_msgid,
1815           p_transaction_type        => l_transaction_type,
1816           p_transaction_subtype     => l_transaction_subtype,
1817           p_message_code            => l_message_code,
1818           p_message_type            => l_message_type,
1819           p_party_id                => l_party_id,
1820           p_party_site_id           => l_party_site_id,
1821           p_protocol_type           => l_protocol_type,
1822           p_protocol_address        => l_protocol_address,
1823           p_username                => l_username,
1824           p_password                => l_password,
1825           p_attribute1              => l_attribute1,
1826           p_attribute2              => l_attribute2,
1827           p_attribute3              => l_attribute3,
1828           p_attribute4              => l_attribute4,
1829           p_attribute5              => l_attribute5,
1830           p_internal_control_number => l_internal_control_number,
1831           p_debug_mode              => l_debug_mode,
1832           p_logfile                 => l_logfile,
1833           p_status                  => l_status,
1834           p_time_stamp              => l_time_stamp,
1835           p_document_number         => l_document_number,
1836           p_event                   => p_event);
1837 
1838           if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1839             wf_log_pkg.string(wf_log_pkg.level_statement, l_module,
1840                             'Dispatch ' ||p_event.event_name);
1841           end if;
1842           wf_event.dispatch('EXTERNAL', null, p_event);
1843       end if;
1844     end if;
1845   else
1846     -- enqueue to the second queue.
1847     enqueue_msg(p_event, l_queue_name, l_msgid, l_process_id);
1848     p_event.setCorrelationId(l_process_id);
1849     --p_event.setEventKey(l_process_id);
1850 
1851     ecx_debug.setErrorInfo(10, 10, 'ECX_PROCESSING_MESSAGE');
1852     ecx_errorlog.inbound_trigger(
1853                  l_trigger_id,
1854                  l_msgid,
1855                  l_process_id,
1856                  ecx_utils.i_ret_code,
1857                  ecx_utils.i_errbuf
1858                  );
1859   end if;
1860 
1861   fnd_log.g_current_runtime_level := saved_fnd_runtime_debug;
1862 
1863   if (ecx_utils.i_ret_code = 2) then
1864      if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1865         wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Finished with Error.');
1866      end if;
1867      ecx_debug.setErrorInfo(2,30, 'ECX_IN_RULE_PROCESING_ERROR');
1868      raise rule_exception;
1869   end if;
1870 
1871   if (wf_log_pkg.level_statement >= fnd_log.g_current_runtime_level) then
1872     wf_log_pkg.string(wf_log_pkg.level_statement, l_module, 'Finished with Success.');
1873   end if;
1874   return 'SUCCESS';
1875 
1876 exception
1877   when others then
1878     fnd_log.g_current_runtime_level := saved_fnd_runtime_debug;
1879     ecx_errorlog.inbound_trigger(
1880                   l_trigger_id,
1881                   l_msgid,
1882                   l_process_id,
1883                   ecx_utils.i_ret_code,
1884                   ecx_utils.i_errbuf,
1885                   ecx_utils.i_errparams
1886                   );
1887 
1888     wf_event.setErrorInfo(p_event,'ERROR');
1889     p_event.setErrorMessage(ecx_debug.getMessage(ecx_utils.i_errbuf,
1890                                                  ecx_utils.i_errparams));
1891     if (p_event.event_key is null)
1892     then
1893       p_event.event_key := l_msgid;
1894     end if;
1895     p_event.AddParameterToList('ECX_RETURN_CODE', ecx_utils.i_ret_code);
1896     p_event.AddParameterToList('ECX_ERROR_MSG',ecx_utils.i_errbuf);
1897     p_event.AddParameterToList('ECX_ERROR_PARAMS',ecx_utils.i_errparams);
1898     p_event.AddParameterToList('ECX_ERROR_TYPE', ecx_utils.error_type);
1899     p_event.AddParameterToList('ECX_TP_HEADER_ID', l_tp_header_id);
1900     p_event.AddParameterToList('ECX_TRANSACTION_TYPE',l_transaction_type);
1901     p_event.AddParameterToList('ECX_TRANSACTION_SUBTYPE',l_transaction_subtype);
1902     p_event.AddParameterToList('ECX_PARTY_ID',l_party_id);
1903     p_event.AddParameterToList('ECX_PARTY_SITE_ID',l_party_site_id);
1904     p_event.AddParameterToList('ECX_PARTY_TYPE',l_party_type);
1905     p_event.addParameterToList('ECX_DIRECTION','IN');
1906     p_event.addParameterToList('ECX_MESSAGE_STANDARD',l_message_code);
1907     p_event.addParameterToList('ECX_MESSAGE_TYPE', l_message_type);
1908     p_event.addParameterToList('ECX_DOCUMENT_ID', l_document_number);
1909 
1910     return 'ERROR';
1911 
1912 end ReceiveTPMessage;
1913 
1914 
1915 function CreateTPMessage (
1916   p_subscription_guid  in            raw,
1917   p_event              in out nocopy wf_event_t
1918 ) return varchar2
1919 is
1920 
1921   l_module   varchar2(2000);
1922 
1923 begin
1924   l_module := 'ecx.plsql.ecx_rule.CreateTPMessage';
1925   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
1926     wf_log_pkg.string(wf_log_pkg.level_procedure, l_module ||'.begin',
1927        'Starting CreateTPMessage rule function.');
1928   end if;
1929   return (exec_wf(p_subscription_guid, p_event, 'ECXSTD', 'OUTBOUND_B2B'));
1930 
1931 end CreateTPMessage;
1932 
1933 
1934 end ecx_rule;