DBA Data[Home] [Help]

PACKAGE BODY: APPS.FND_GLOBAL

Source


1 package body fnd_global as
2   /* $Header: AFSCGBLB.pls 120.64.12020000.7 2013/02/28 20:48:23 rarmaly ship $ */
3 
4   procedure dump_context;
5 
6   /* Static variables defined for Connection Tagging */
7 
8     FRM constant varchar2(3) := 'frm';
9     FWK constant varchar2(3) := 'fwk';
10     JTT constant varchar2(3) := 'jtt';
11     CP constant varchar2(2) := 'cp';
12     WF constant varchar2(2) := 'wf';
13     BES constant varchar2(3) := 'bes';
14     REPORT constant varchar2(3) := 'rpt';
15     ALERT constant varchar2(3) := 'alr';
16     ISG constant varchar2(3) := 'isg';
17     GSM constant varchar2(3) := 'gsm';
18     HELP constant varchar2(7) := 'hlp';
19     BINARY_PROGRAM constant varchar2(3) := 'bin';
20     EBS constant varchar2(1) := 'e';
21 
22   /*
23   * bug13831550
24   * Local GLOBAL variables for TAG-DB procedures and functions
25   *
26   */
27   gbl_session_module number := 0;
28   gbl_session_action number := 0;
29   gbl_read_sinfo     number := -1;  /* global read session Information */
30 
31   -- although the index is 2000, sys_context only support 30 character names
32   -- so the names will be truncated in the sys_context if they exceed 30.
33   type t_flags is table of boolean index by varchar2(2000);
34 
35   type t_wa is table of varchar2(2000) index by binary_integer;
36   type t_waf is table of boolean index by binary_integer;
37 
38   -- the context hash. these are all the real initialized name/values we track.
39   z_context fnd_const.t_hashtable;
40   z_context_names t_wa;
41   z_context_values t_wa;
42 
43   -- the backup context hash. see restore.
44   z_backup fnd_const.t_hashtable;
45   z_backup_names t_wa;
46   z_backup_values t_wa;
47 
48   -- initialization hash.
49   -- these are the name/values pairs passed in from the caller.
50   z_init t_flags;
51   z_init_names t_wa;
52   z_init_values t_waf;
53 
54   -- flags to indicate that the value changed so the profile needs reset.
55   z_init_profiles t_flags;
56   z_profile_names t_wa;
57   z_profile_values t_waf;
58 
59   -- for avoiding puts to profiles until after profile initialization.
60   z_allow_profile_puts boolean := false;
61 
62   -- flags to indicate that the value is to be set in sys_context.
63   z_syscontext t_flags;
64   z_syscontext_names t_wa;
65   z_syscontext_values t_waf;
66 
67   -- a map for fnd_product_initialization
68   -- @todo deprecated?
69   z_conditions_map fnd_const.t_hashtable;
70   z_conditions_names t_wa;
71   z_conditions_values t_wa;
72 
73   --
74   z_security_groups_enabled boolean := false;
75 
76   -- flag to indicate package instantiation
77   z_first_initialization boolean := true;
78 
79   --
80   z_context_change_flag boolean := null;
81 
82   --
83   site_context_change boolean := false;
84 
85   -- cached value indicating if any of the security context
86   -- related properties have changed.
87   z_security_context_change_flag boolean := null;
88 
89   -- can force database initialization of the entire contex
90   z_force_init boolean := false;
91 
92   AUDIT_TRAIL_PROFILE constant varchar2(19) := 'AUDITTRAIL:ACTIVATE';
93 
94   -- Turns on debugging.
95   -- This can be enabled for initialization by turning on core logging.
96   is_debugging boolean := false;
97 
98   -- Wildcard name for determining when to dump the stack when the
99   -- name's value is changed.
100   debug_trace_name fnd_profile_option_values.profile_option_value%type;
101 
102   -- Logging to fnd_core_log circumvents other logging.
103   -- That is, if this is enabled, the other two methods
104   -- will not be reached.
105   debug_to_core boolean := false;
106 
107   -- Debugs using dbms_output.put_line. Circumvents logging
108   -- using debug_to_table.
109   debug_to_console boolean := false;
110 
111   -- NOTE: This will attempt to create a database table
112   -- named fnd_global_debug_table. Don't enable this
113   -- unless it's okay to create that table. The contents
114   -- may be security sensitive.
115   debug_to_table boolean := false;
116 
117   -- used to order records when using debug_to_table mode
118   debug_counter integer := 0;
119 
120   -- used to determine whether an org context change was made by MO_GLOBAL
121   -- using fnd_profile.initialize(name, value)
122   -- Bug 7685798
123   MOAC_context_change_attempt boolean := false;
124 
125   -- used to determine whether FND_INIT_SQL is being executed.
126   -- Bug 8335361
127   in_fnd_init_sql boolean := false;
128 
129 
130   -- bug 12875860 - needed to add a way to remove a value in  the PUT cache
131   --
132   -- Constant string used to indicate a delete request in PUT cache.
133   FND_DELETE_VALUE VARCHAR2(30) := '**FND_DELETE_VALUE**';
134 
135   --
136   -- Enables logging to core logging for fnd_global if core logging is enabled.
137   -- It should be called from the primary public routines. For example,
138   -- initialize, and the set_nl* routines.
139   procedure check_logging
140   is
141     dest varchar2(30);
142   begin
143 
144     debug_to_core := fnd_core_log.enabled <> 'N';
145 
146     dest := upper(sys_context(FND_CONST.FND,'FND_GLOBAL_DEBUG_LOGGING'));
147     debug_to_console := dest like '%CONSOLE%';
148     debug_to_table := dest like '%TABLE%';
149 
150     -- enables debug output if a destination is enabled.
151     is_debugging := debug_to_core or debug_to_console or debug_to_table;
152 
153     if is_debugging then
154       if debug_trace_name is null then
155         begin
156           select fpov.profile_option_value
157             into debug_trace_name
158             from fnd_profile_option_values fpov, fnd_profile_options fpo
159            where fpo.profile_option_name = 'AFGLOBAL_TRACE_NAME'
160              and fpo.profile_option_id = fpov.profile_option_id
161              and fpov.level_id = 10001
162              and fpo.application_id = fpov.application_id;
163         exception
164           when no_data_found then
165             -- Don't track anything and stop the query from reexecuting.
166             debug_trace_name := '-NO TRACING-';
167         end;
168       end if;
169     else
170       -- so that next time debugging is enabled, it'll requery this.
171       debug_trace_name := null;
172     end if;
173 
174   end check_logging;
175 
176   -- General purpose debugger. Will direct debugging based on
177   -- the debug_to_* flags above.
178   -- DO NOT use any fnd_global routines within this routine.
179   -- DO NOT call anything outside fnd_global except fnd_core_log.put.
180   procedure debugger(text varchar2)
181   is
182      pragma autonomous_transaction;
183   begin
184 
185     if not is_debugging then return; end if;
186 
187     if debug_to_core then
188       fnd_core_log.put('FG.D:'||userenv('sessionid')||':'||text||newline);
189       return;
190     end if;
191 
192     if debug_to_console then
193       dbms_output.put_line(substr(text,1,250));
194     end if;
195 
196     if debug_to_table then
197 
198       debug_counter := debug_counter + 1;
199 
200       if debug_counter = 1 then
201         -- this is a bit of a waste to do at the start of every new
202         -- session but it's probably not much worse than having to
203         -- verify the existence each time either.
204         begin
205           execute immediate
206                   'create table fnd_global_debug_table (
207                           text varchar2(2000)
208                           ,counter integer
209                           ,when date
210                           ,who integer)';
211         exception
212           when others then null;
213         end;
214       end if;
215 
216       begin
217         execute immediate
218                 'insert into fnd_global_debug_table
219                  values (:text,:debug_counter,sysdate,userenv(''sessionid''))'
220                 using text,debug_counter;
221         commit;
222       exception
223         when others then
224           -- stop doing this if insert errored.
225           debug_to_table := false;
226       end;
227 
228     end if;
229 
230   exception
231     when others then
232       null;
233   end debugger;
234 
235   --
236   -- local_chr
237   --   Return specified character in current codeset
238   -- IN
239   --   ascii_chr - chr number in US7ASCII
240   --
241   function local_chr(ascii_chr in number) return varchar2 is
242   begin
243     return fnd_const.local_chr(ascii_chr);
244   end local_chr;
245 
246 
247   function newline return varchar2 is begin return fnd_const.newline; end;
248   function tab return varchar2 is begin return fnd_const.tab; end;
249 
250   --
251   -- log (Internal)
252   --
253   -- Set error message for unexpected sql errors
254   --
255   procedure log(routine in varchar2,
256                 errcode in number,
257                 errmsg in varchar2) is
258   begin
259     if is_debugging then
260       debugger('ROUTINE:'||routine);
261       debugger('ERRNO:'||errcode);
262       debugger('REASON:'||errmsg);
263     end if;
264     fnd_message.set_name(FND_CONST.FND, 'SQL_PLSQL_ERROR');
265     fnd_message.set_token('ROUTINE', routine);
266     fnd_message.set_token('ERRNO', errcode);
267     fnd_message.set_token('REASON', errmsg);
268   end;
269 
270   --
271   -- error (Internal)
272   --
273   -- Set error message and raise exception for unexpected sql errors
274   --
275   procedure throw(routine in varchar2,
276                   errcode in number,
277                   errmsg in varchar2) is
278   begin
279     log(routine,errcode,errmsg);
280     app_exception.raise_exception;
281   end;
282 
283 
284   -- get a value from z_init but only if it is different than
285   -- the value in z_context.
286   function is_new(name varchar2) return boolean
287   as
288   begin
289     if not z_init.exists(name) then
290       return false;
291     end if;
292     return z_init(name);
293   end is_new;
294 
295   -- set a value as new.
296   procedure set_new(name varchar2)
297   as
298   begin
299     z_init(name) := true;
300   end set_new;
301 
302   -- get a value from z_context
303   function get(name varchar2) return varchar2
304   is
305   begin
306     if not z_context.exists(name) then
307       return null;
308     end if;
309     return z_context(name);
310   end get;
311 
312   -- get an integer value from z_context
313   -- returns 'def' if null.
314   -- will throw value_error if not a number.
315   function get_i(name varchar2, def number) return number
316   as
317   begin
318     return nvl(to_number(get(name)),def);
319   end get_i;
320 
321   -- get an integer value from z_context
322   -- returns FND_CONST.UNDEFINED_I if null.
323   -- will throw value_error if not a number.
324   function get_i(name varchar2) return number
325   as
326   begin
327     return get_i(name,FND_CONST.UNDEFINED_I);
328   end get_i;
329 
330   -- determines if a value is defined, not -1 nor null.
331   function is_defined(name varchar2) return boolean
332   as
333   begin
334     return nvl(get(name),FND_CONST.UNDEFINED_S) <> FND_CONST.UNDEFINED_S;
335   end is_defined;
336 
337   -- determines if a value is undefined, -1 or null.
338   function is_undefined(name varchar2) return boolean
339   as
340   begin
341     return not is_defined(name);
342   end is_undefined;
343 
344   -- set the profile value if it has changed.
345   procedure initialize_profile_value(name varchar2, value varchar2)
346   as
347   begin
348     if z_allow_profile_puts and is_new(name) then
349       if is_debugging then
350         debugger('.  fnd_profile.put('||name||','||value||')');
351       end if;
352       fnd_profile.put(name,value);
353     end if;
354   end initialize_profile_value;
355 
356   -- set the profile value based on the cached value, if it has changed.
357   procedure initialize_profile_value(name varchar2)
358   as
359   begin
360     initialize_profile_value(name,get(name));
361   end initialize_profile_value;
362 
363   -- initializes all profile values by calling initialize_profile_value
364   -- for each profile in z_init_profiles.
365   procedure initialize_profile_values
366   as
367     c integer;
368     p varchar2(2000);
369   begin
370 
371     c := z_init_profiles.count;
372     p := z_init_profiles.first;
373     for i in 1..c loop
374       -- only allow profile puts for names in z_init_profiles that are 'true'.
375       if z_init_profiles(p) then
376         initialize_profile_value(p,get(p));
377       end if;
378       p := z_init_profiles.next(p);
379     end loop;
380 
381   end initialize_profile_values;
382 
383   -- returns true if the value passed is different than the cached value.
384   function has_changed(name varchar2, value varchar2) return boolean
385   as
386     currval varchar2(2000) := get(name);
387   begin
388 
389     --debugger('name '||name||' ['||currval||','||value||']');
390 
391     -- only set the value if the new and old values are different
392     return z_force_init
393       or value <> currval
394       or (currval is null and value is not null)
395       or (currval is not null and value is null);
396 
397   end has_changed;
398 
399   -- This exists so that routines that are restrict_references(WNDS)
400   -- don't break. It should only be called from the few routines
401   -- that are already using it. In all other cases, use put instead of this.
402   -- set a value in z_context but does not affect profiles nor sys_context
403   -- as does the standard put routine.
404   -- returns true if the value was set, false otherwise.
405   function put_nosys(name varchar2, value varchar2) return boolean
406   as
407   begin
408 
409     -- only set the value if the new and old values are different
410     if has_changed(name,value) then
411 
412       z_context(name) := value;
413       set_new(name);
414 
415       if is_debugging then
416         debugger('=* '||name||'='||value);
417         if name like debug_trace_name then
418           debugger(dbms_utility.format_call_stack);
419         end if;
420       end if;
421 
422       return true;
423 
424     end if;
425 
426     if is_debugging then
427       debugger('=  '||name||'='||value);
428     end if;
429 
430     return false;
431 
432   end put_nosys;
433 
434   -- set a value in z_context
435   -- returns true if the value was set, false otherwise.
436   function put(name varchar2,
437                value varchar2,
438                put_profile boolean default true) return boolean
439   as
440   begin
441 
442     if put_nosys(name,value) then
443 
444       -- only put profile values that exist in z_init_profiles.
445       -- it is irrelevant if its value in z_init_profiles is true or false.
446       if put_profile and z_init_profiles.exists(name) then
447         initialize_profile_value(name,value);
448       end if;
449 
450       -- only set syscontext values that exist in z_syscontext and are 'true'.
451       if z_syscontext.exists(name) and z_syscontext(name) then
452 
453         if is_debugging then
454           debugger('.  fnd_context.init');
455         end if;
456 
457         fnd_context.init(FND_CONST.FND, name, value);
458       end if;
459 
460       return true;
461 
462     end if;
463 
464     return false;
465 
466   exception
467     when others then
468       throw('fnd_global.put('||name||','||value||')',
469             sqlcode, dbms_utility.format_error_stack);
470   end put;
471 
472   -- same as put_nosys but disposes of the return value
473   procedure put_nosys(name varchar2, value varchar2)
474   as
475     dummy boolean;
476   begin
477     dummy := put_nosys(name,value);
478   end put_nosys;
479 
480   -- same as put but disposes of the return value
481   procedure put(name varchar2,
482                 value varchar2,
483                 put_profile boolean default true)
484   as
485     dummy boolean;
486   begin
487     dummy := put(name,value,put_profile);
488   end put;
489 
490   -- remove the value from z_context, setting it to null
491   procedure clear(name varchar2)
492   as
493   begin
494     if z_context.exists(name) and z_context(name) is not null then
495       put(name,null);
496     end if;
497   end clear;
498 
499   -- set a value in z_context
500   procedure put_i(name varchar2, value number)
501   as
502   begin
503     put(name,to_char(value));
504   end put_i;
505 
506   -- set the default integer value in z_context
507   procedure put_i(name varchar2)
508   as
509   begin
510     put(name,FND_CONST.UNDEFINED_S);
511   end put_i;
512 
513   -- set a value to -1.
514   procedure set_undefined(name varchar2)
515   as
516   begin
517     --debugger('undefining '||name);
518     put(name,FND_CONST.UNDEFINED_S);
519   end set_undefined;
520 
521   -- set a value on z_context from a profile value
522   -- if name isn't already defined
523   procedure put_from_profile(name varchar2)
524   as
525     value varchar2(2000);
526   begin
527     value := get(name);
528     if value is null or is_undefined(name) then
529       value := fnd_profile.value(name);
530       if is_debugging then
531         debugger('.  fnd_profile.value('||name||')='||value);
532       end if;
533       put(name,value,false);
534     end if;
535   end put_from_profile;
536 
537   -- Clear all the derived values that are cached as a result of lazy
538   -- initialization.
539   -- These are values that are never actually passed, but are derived from
540   -- other context values and cached for efficiency.  If the values from
541   -- which they were derived have changed, they need to be cleared to
542   -- to force re-derivation.
543   procedure clear_derived_values
544   as
545   begin
546 
547     if nls_context_change then
548 
549       if resp_context_change
550         or appl_context_change
551       then
552         clear(FND_CONST.APPLICATION_NAME);
553         clear(FND_CONST.RESP_NAME);
554       end if;
555 
556     end if;
557 
558   end clear_derived_values;
559 
560   --
561   -- flag to indicate if user_id, resp_id and/or resp_appl_id changed.
562   --
563   function user_resp_changed return boolean
564   is
565   begin
566     return user_context_change
567            or resp_context_change
568            or appl_context_change;
569   end;
570 
571   --
572   -- flag to indicate if a security context change occurred.
573   -- this means user_resp_changed or sec_context_change
574   --
575   function security_context_changed return boolean
576   is
577   begin
578     if z_security_context_change_flag is null then
579       z_security_context_change_flag
580         := user_resp_changed
581            or sec_context_change;
582     end if;
583     return z_security_context_change_flag;
584   end;
585 
586   --
587   -- flag to indicate if a context change occurred.
588   -- this means any of the follow are true:
589   --   security_context_changed
590   --   resp_context_change
591   --   appl_context_change
592   --   server_context_change
593   --   org_context_change
594   --   nls_context_change;
595   --
596   function context_changed return boolean
597   is
598   begin
599     if z_context_change_flag is null then
600       -- For R12, due to MOAC, the org context can change within a session and
601       -- is being handled separately from the other contexts.
602       if fnd_release.major_version >= 12 then
603         z_context_change_flag
604           := security_context_changed
605              or server_context_change
606              or nls_context_change;
607       else
608         z_context_change_flag
609           := security_context_changed
610              or server_context_change
611              or org_context_change
612              or nls_context_change;
613       end if;
614     end if;
615     return z_context_change_flag;
616   end;
617 
618   -- AOL-FORCE_INIT -
619   function force_init return boolean is
620   begin
621     return get('AOL:FORCE_INIT') is not null;
622   end force_init;
623 
624   --
625   -- AUDIT_ACTIVE - Return TRUE/FALSE (whether audit is turned on or off)
626   -- Added June, 1999, bug 879630 Jan Smith
627   --
628   function audit_active return boolean is
629     buffer varchar2(30); -- actual length should be 1, padding for security.
630   begin
631 
632     -- If this is the first time that the function has been invoked then
633     -- retrieve the value for the profile option.
634     buffer := substrb(get(AUDIT_TRAIL_PROFILE),1,1);
635     if buffer is null then
636       buffer := fnd_profile.value(AUDIT_TRAIL_PROFILE);
637       put_nosys(AUDIT_TRAIL_PROFILE,buffer);
638     end if;
639 
640     return buffer = 'Y';
641   end audit_active;
642 
643   -- APPLICATION_NAME -
644   function application_name return varchar2 is
645     buffer fnd_application_vl.application_name%type;
646     v_raid integer := resp_appl_id;
647   begin
648     if v_raid is null then
649       return null;
650     end if;
651 
652     -- no caching of APPLICATION_NAME
653     -- to avoid changing pragma in spec
654     --buffer := get(FND_CONST.APPLICATION_NAME);
655     --if buffer is null then
656 
657       -- Re-query every time in case of language change --
658       select a.application_name
659         into buffer
660         from fnd_application_vl a
661        where a.application_id = v_raid;
662 
663       -- no caching of APPLICATION_NAME
664       -- to avoid changing pragma in spec
665       --put_nosys(FND_CONST.APPLICATION_NAME,buffer);
666     --end if;
667 
668     return buffer;
669   exception
670     when no_data_found then
671       return null;
672   end application_name;
673 
674   -- APPLICATION_SHORT_NAME -
675   function application_short_name return varchar2 is
676   begin
677     return get(FND_CONST.APPLICATION_SHORT_NAME);
678   end application_short_name;
679 
680   -- BASE_LANGUAGE -
681   function base_language return varchar2 is
682     buffer fnd_languages.language_code%type := get(FND_CONST.BASE_LANGUAGE);
683   begin
684     if buffer is null then
685 
686       select language_code
687         into buffer
688         from fnd_languages
689        where installed_flag = 'B';
690 
691       put_nosys(FND_CONST.BASE_LANGUAGE,buffer);
692     end if;
693     return buffer;
694   exception
695     when no_data_found then
696       return null;
697   end base_language;
698 
699   -- CONC_LOGIN_ID -
700   function conc_login_id return number is
701   begin
702     return get_i(FND_CONST.CONC_LOGIN_ID);
703   end conc_login_id;
704 
705   -- CONC_PRIORITY_REQUEST -
706   function conc_priority_request return number is
707   begin
708     return get_i(FND_CONST.CONC_PRIORITY_REQUEST,null);
709   end conc_priority_request;
710 
711   -- CONC_PROGRAM_ID -
712   function conc_program_id return number is
713   begin
714     return get_i(FND_CONST.CONC_PROGRAM_ID);
715   end conc_program_id;
716 
717   -- CONC_PROCESS_ID -
718   function conc_process_id return number is
719   begin
720     return get_i(FND_CONST.CONC_PROCESS_ID);
721   end conc_process_id;
722 
723   -- CONC_QUEUE_ID -
724   function conc_queue_id return number is
725   begin
726     return get_i(FND_CONST.CONC_QUEUE_ID);
727   end conc_queue_id;
728 
729   -- CONC_REQUEST_ID -
730   function conc_request_id return number is
731   begin
732     return get_i(FND_CONST.CONC_REQUEST_ID);
733   end conc_request_id;
734 
735   -- CURRENT_LANGUAGE -
736   function current_language return varchar2 is
737   begin
738     return userenv('LANG');
739   end current_language;
740 
741   -- CUSTOMER_ID -
742   function customer_id return number is
743   begin
744     return get_i(FND_CONST.CUSTOMER_ID);
745   end customer_id;
746 
747   -- EMPLOYEE_ID -
748   function employee_id return number is
749   begin
750     return get_i(FND_CONST.EMPLOYEE_ID);
751   end employee_id;
752 
753   -- FORM_ID -
754   function form_id return number is
755   begin
756     return get_i(FND_CONST.FORM_ID);
757   end form_id;
758 
759   -- FORM_APPL_ID -
760   function form_appl_id return number is
761   begin
762     return get_i(FND_CONST.FORM_APPL_ID);
763   end form_appl_id;
764 
765   -- LANGUAGE_COUNT -
766   function language_count return number is
767     buffer number := get_i(FND_CONST.LANGUAGE_COUNT,null);
768   begin
769     if buffer is null then
770 
771       select count(1)
772         into buffer
773         from fnd_languages
774        where installed_flag in ('I', 'B');
775 
776       put_nosys(FND_CONST.LANGUAGE_COUNT,to_char(buffer));
777     end if;
778     return buffer;
779   exception
780     when no_data_found then
781       return 0;
782   end language_count;
783 
784   -- LOGIN_ID -
785   function login_id return number is
786   begin
787     return get_i(FND_CONST.LOGIN_ID);
788   end login_id;
789 
790   -- ORG_ID -
791   function org_id return number is
792   begin
793     return get_i(FND_CONST.ORG_ID);
794   end org_id;
795 
796   -- Fetches and caches ORG_NAME based on current ORG_ID
797   function org_name return varchar2 is
798     v_org_name varchar2(2000) := get(FND_CONST.ORG_NAME);
799     v_org_id integer;
800   begin
801     if v_org_name is null then
802       v_org_id := org_id;
803       if v_org_id > -1 then
804         select name into v_org_name
805         from hr_operating_units
806         where organization_id = v_org_id;
807 
808         put(FND_CONST.ORG_NAME,v_org_name);
809       end if;
810     end if;
811 
812     return(v_org_name);
813   exception
814     when others then
815       clear(FND_CONST.ORG_NAME);
816       return null;
817   end org_name;
818 
819   -- PARTY_ID -
820   function party_id return number is
821   begin
822     return get_i(FND_CONST.PARTY_ID);
823   end party_id;
824 
825   -- PER_BUSINESS_GROUP_ID -
826   function per_business_group_id return number is
827   begin
828     return get_i(FND_CONST.PER_BUSINESS_GROUP_ID);
829   end per_business_group_id;
830 
831   -- PER_SECURITY_PROFILE_ID -
832   function per_security_profile_id return number is
833   begin
834     return get_i(FND_CONST.PER_SECURITY_PROFILE_ID);
835   end per_security_profile_id;
836 
837   -- PROG_APPL_ID -
838   function prog_appl_id return number is
839   begin
840     return get_i(FND_CONST.PROG_APPL_ID);
841   end prog_appl_id;
842 
843   -- QUEUE_APPL_ID -
844   function queue_appl_id return number is
845   begin
846     return get_i(FND_CONST.QUEUE_APPL_ID);
847   end queue_appl_id;
848 
849   -- RESP_APPL_ID - Return responsibility application id
850   function resp_appl_id return number is
851   begin
852     return get_i(FND_CONST.RESP_APPL_ID);
853   end resp_appl_id;
854 
855   -- RESP_ID - Return responsibility id
856   function resp_id return number is
857   begin
858     return get_i(FND_CONST.RESP_ID);
859   end resp_id;
860 
861   -- RESP_NAME -
862   function resp_name return varchar2 is
863     buffer fnd_responsibility_vl.responsibility_name%type;
864     v_rid integer := resp_id;
865     v_raid integer := resp_appl_id;
866   begin
867     if v_rid is null or v_raid is null then
868       return null;
869     end if;
870 
871     -- no caching of RESP_NAME
872     -- to avoid changing pragma in spec
873     --buffer := get(FND_CONST.RESP_NAME);
874     --if buffer is null then
875 
876       -- Re-query every time in case of language change --
877       select r.responsibility_name
878         into buffer
879         from fnd_responsibility_vl r
880        where r.responsibility_id = v_rid
881          and r.application_id = v_raid;
882 
883       -- no caching of RESP_NAME
884       -- to avoid changing pragma in spec
885       --put_nosys(FND_CONST.RESP_NAME,buffer);
886     --end if;
887 
888     return buffer;
889   exception
890     when no_data_found then
891       -- no caching of RESP_NAME
892       -- to avoid changing pragma in spec
893       --clear(FND_CONST.RESP_NAME);
894       return null;
895   end resp_name;
896 
897   -- RESP_KEY -
898   -- returns responsibility_key of current context
899   function resp_key return varchar2 is
900     buffer varchar2(30);
901     v_rid  integer := resp_id;
902     v_raid integer := resp_appl_id;
903   begin
904     if v_rid is null or v_raid is null then
905       return null;
906     end if;
907 
908     select r.responsibility_key
909     into buffer
910     from fnd_responsibility_vl r
911     where r.responsibility_id = v_rid
912     and r.application_id      = v_raid;
913 
914     return buffer;
915   exception
916     when no_data_found then
917       return null;
918   end resp_key;
919 
920   -- RT_TEST_ID -
921   function rt_test_id return number is
922   begin
923     return get_i(FND_CONST.RT_TEST_ID);
924   end rt_test_id;
925 
926   -- Bug 12875860 - remove function security_groups_enabled - NOT used
927 
928   -- SECURITY_GROUP_ID - Return security group id
929   function security_group_id return number is
930   begin
931     return get_i(FND_CONST.SECURITY_GROUP_ID,0);
932   end security_group_id;
933 
934   -- SECURITY_GROUP_ID_POLICY - Return security group id
935   function security_group_id_policy(d1 varchar2, d2 varchar2) return varchar2 is
936   begin
937     if is_undefined(FND_CONST.SECURITY_GROUP_ID) then
938       return null;
939     end if;
940     return '(security_group_id = SYS_CONTEXT(''FND'',''SECURITY_GROUP_ID''))';
941   end security_group_id_policy;
942 
943   -- SERVER_ID -
944   function server_id return number is
945   begin
946     return get_i(FND_CONST.SERVER_ID);
947   end server_id;
948 
949   -- SESSION_ID - Return responsibility id
950   function session_id return number is
951   begin
952     return get_i(FND_CONST.SESSION_ID);
953   end session_id;
954 
955   -- SITE_ID -
956   function site_id return number is
957   begin
958     return get_i(FND_CONST.SITE_ID);
959   end site_id;
960 
961   -- SUPPLIER_ID -
962   function supplier_id return number is
963   begin
964     return get_i(FND_CONST.SUPPLIER_ID);
965   end supplier_id;
966 
967   -- USER_ID - Return user id
968   function user_id return number is
969   begin
970     return get_i(FND_CONST.USER_ID);
971   end user_id;
972 
973   -- USER_NAME -
974   function user_name return varchar2 is
975   begin
976     return get(FND_CONST.USER_NAME);
977   end user_name;
978 
979 
980   -- NLS functions
981   function nls_language return varchar2 is
982   begin
983     return get(FND_CONST.NLS_LANGUAGE);
984   end nls_language;
985 
986   function nls_numeric_characters return varchar2 is
987   begin
988     return get(FND_CONST.NLS_NUMERIC_CHARACTERS);
989   end nls_numeric_characters;
990 
991   function nls_date_format return varchar2 is
992   begin
993     return get(FND_CONST.NLS_DATE_FORMAT);
994   end;
995 
996   function nls_date_language return varchar2 is
997   begin
998     return get(FND_CONST.NLS_DATE_LANGUAGE);
999   end nls_date_language;
1000 
1001   function nls_territory return varchar2 is
1002   begin
1003     return get(FND_CONST.NLS_TERRITORY);
1004   end nls_territory;
1005 
1006   function nls_sort return varchar2 is
1007   begin
1008     return get(FND_CONST.NLS_SORT);
1009   end nls_sort;
1010 
1011   --   Get Security Group Id from which to retrieve lookup type.
1012   --   This will either be the current security group, or default to the
1013   --   STANDARD security group (id=0) if lookup type not defined
1014   --   in current security group.
1015   -- IN
1016   --   lookup_type
1017   --   view_application_id
1018   -- RETURNS
1019   --   Security_group_id of lookup type to use (current or STANDARD).
1020   -- NOTE
1021   --   This function is used by FND_LOOKUPS and related views to
1022   --   improve performance.
1023   function lookup_security_group(lookup_type in varchar2,
1024                                  view_application_id in number)
1025   return number
1026   is
1027     retval number;
1028   begin
1029     --
1030     -- execute this query only when security groups are enabled (1/2/01) jvc
1031     --
1032     if z_security_groups_enabled then
1033 
1034       select max(lt.security_group_id)
1035         into retval
1036         from fnd_lookup_types lt
1037        where lt.view_application_id = lookup_security_group.view_application_id
1038          and lt.lookup_type         = lookup_security_group.lookup_type
1039          and lt.security_group_id in (0,
1040                         to_number(decode(substrb(userenv('CLIENT_INFO'),55,1),
1041                                         ' ', '0',
1042                                         null, '0',
1043                                         substrb(userenv('CLIENT_INFO'),55,10))));
1044       return retval;
1045     else
1046       return 0;
1047     end if;
1048   exception
1049     when no_data_found then
1050       return null;
1051   end lookup_security_group;
1052 
1053   -- returns the number of times initialize has been called in this session
1054   function get_session_context
1055   return number
1056   is
1057   begin
1058     return(session_context);
1059   end get_session_context;
1060 
1061   -- returns true if the session_context is the same as context_id,
1062   -- otherwise returns false.
1063   function compare_session_context(context_id in number)
1064   return boolean
1065   is
1066   begin
1067     if (session_context <> context_id) then
1068       return false;
1069     else
1070       return true;
1071     end if;
1072   end compare_session_context;
1073 
1074   -- returns true if no_pool is null or equal to session_context,
1075   -- otherwise returns false.
1076   function assert_no_pool return boolean is
1077   begin
1078     if no_pool is null then
1079       no_pool := session_context;
1080       return true;
1081     else
1082       if no_pool <> session_context then
1083         return false;
1084       else
1085         return true;
1086       end if;
1087     end if;
1088   end assert_no_pool;
1089 
1090   --
1091   procedure save_hash(p_hash in out nocopy fnd_const.t_hashtable,
1092                       p_names in out nocopy t_wa,
1093                       p_values in out nocopy t_wa) is
1094     c integer;
1095     p varchar2(2000);
1096   begin
1097     c := p_hash.count;
1098     p := p_hash.first;
1099     for i in 1..c loop
1100       p_names(i) := p;
1101       p_values(i) := p_hash(p);
1102       p := p_hash.next(p);
1103     end loop;
1104     p_hash.delete;
1105 
1106   end save_hash;
1107 
1108   --
1109   procedure restore_hash(p_hash in out nocopy fnd_const.t_hashtable,
1110                          p_names in out nocopy t_wa,
1111                          p_values in out nocopy t_wa) is
1112     c integer;
1113     p varchar2(2000);
1114   begin
1115     c := p_names.count;
1116     p := p_names.first;
1117     for i in 1..c loop
1118       if not p_hash.exists(p_names(i)) then
1119         p_hash(p_names(i)) := p_values(i);
1120       end if;
1121       p := p_names.next(p);
1122     end loop;
1123     p_names.delete;
1124     p_values.delete;
1125 
1126   end restore_hash;
1127 
1128   --
1129   procedure save_flags(p_hash in out nocopy t_flags,
1130                        p_names in out nocopy t_wa,
1131                        p_values in out nocopy t_waf) is
1132     c integer;
1133     p varchar2(2000);
1134   begin
1135     c := p_hash.count;
1136     p := p_hash.first;
1137     for i in 1..c loop
1138       p_names(i) := p;
1139       p_values(i) := p_hash(p);
1140       p := p_hash.next(p);
1141     end loop;
1142     p_hash.delete;
1143 
1144   end save_flags;
1145 
1146   --
1147   procedure restore_flags(p_hash in out nocopy t_flags,
1148                           p_names in out nocopy t_wa,
1149                           p_values in out nocopy t_waf) is
1150     c integer;
1151     p varchar2(2000);
1152   begin
1153     c := p_names.count;
1154     p := p_names.first;
1155     for i in 1..c loop
1156       if not p_hash.exists(p_names(i)) then
1157         p_hash(p_names(i)) := p_values(i);
1158       end if;
1159       p := p_names.next(p);
1160     end loop;
1161     p_names.delete;
1162     p_values.delete;
1163 
1164   end restore_flags;
1165 
1166   --
1167   -- Prior to an NLS change, all string-keyed associative arrays must be
1168   -- deleted. This stores the values in integer indexed arrays so the
1169   -- associative array can be repopulated after the NLS change.
1170   --
1171   procedure pre_nls_change is begin
1172     save_hash(z_context,z_context_names,z_context_values);
1173     save_hash(z_backup,z_backup_names,z_backup_values);
1174     save_hash(z_conditions_map,z_conditions_names,z_conditions_values);
1175     save_flags(z_init,z_init_names,z_init_values);
1176     save_flags(z_init_profiles,z_profile_names,z_profile_values);
1177     save_flags(z_syscontext,z_syscontext_names,z_syscontext_values);
1178   end pre_nls_change;
1179 
1180   --
1181   -- This is called in set_nls *after* any nls context changes have occurred
1182   -- and the nls_context_change flag has been set, if there were any changes.
1183   -- Also, this API doesn't execute during fnd_global.initialize's call
1184   -- to set_nls so it'll only run during direct calls to set_nls.
1185   --
1186   procedure post_nls_change is begin
1187     restore_hash(z_context,z_context_names,z_context_values);
1188     restore_hash(z_backup,z_backup_names,z_backup_values);
1189     restore_hash(z_conditions_map,z_conditions_names,z_conditions_values);
1190     restore_flags(z_init,z_init_names,z_init_values);
1191     restore_flags(z_init_profiles,z_profile_names,z_profile_values);
1192     restore_flags(z_syscontext,z_syscontext_names,z_syscontext_values);
1193 
1194     -- Bug 9226640: the cached org_name value should be cleared if the NLS
1195     -- context changes, so that when org_name is called, the value is refreshed
1196     -- with the prevailing NLS session context.
1197     --
1198     clear(FND_CONST.ORG_NAME);
1199 
1200   end post_nls_change;
1201 
1202   --
1203   -- Bug 5032374
1204   -- Determine whether to override NLS_DATE_LANGUAGE.
1205   -- This routine depends on NLS_DATE_FORMAT being set. Either by defaulting
1206   -- based on its parent territory or on the user's specification.
1207   function override_nls_date_language(p_nls_date_language varchar2 default null)
1208   return varchar2 is
1209     -- Bug 6718678 and 5032384
1210     -- use nls_language instead of nls_date_language
1211     v_nls_date_language v$nls_parameters.value%type
1212       := nvl(p_nls_date_language,nls_language);
1213     nul_dl boolean := false;
1214     new_dl boolean := false;
1215     new_df boolean := false;
1216     chg_dl boolean := false;
1217     mon_lk boolean := false;
1218     mm_lk  boolean := false;
1219     v_nls_charset_name varchar2(30) := nls_charset_name(nls_charset_id('CHAR_CS'));
1220   begin
1221 
1222     nul_dl := p_nls_date_language is null;
1223     new_dl := is_new(FND_CONST.NLS_DATE_LANGUAGE);
1224     new_df := is_new(FND_CONST.NLS_DATE_FORMAT);
1225     chg_dl := has_changed(FND_CONST.NLS_DATE_LANGUAGE,p_nls_date_language);
1226     mon_lk := nls_date_format like '%MON%';
1227     mm_lk  := nls_date_format like '%MM%';
1228 
1229     if is_debugging then
1230       debugger(dbms_utility.format_call_stack);
1231       debugger('?  p_nls_date_language is null              :'
1232                ||fnd_const.bool(nul_dl));
1233       debugger('?  is_new(FND_CONST.NLS_DATE_LANGUAGE)      :'
1234                ||fnd_const.bool(new_dl));
1235       debugger('?  is_new(FND_CONST.NLS_DATE_FORMAT)        :'
1236                ||fnd_const.bool(new_df));
1237       debugger('?  has_changed(FND_CONST.NLS_DATE_LANGUAGE) :'
1238                ||fnd_const.bool(chg_dl));
1239       debugger('?= nls_date_format like ''%MON%''           :'
1240                ||fnd_const.bool(mon_lk));
1241       debugger('?= nls_date_format like ''%MM%''           :'
1242                ||fnd_const.bool(mm_lk));
1243       debugger('?= nul_dl and new_dl                        :'
1244                ||fnd_const.bool(nul_dl and new_dl));
1245       debugger('?= not nul_dl and chg_dl                    :'
1246                ||fnd_const.bool(not nul_dl and chg_dl));
1247       debugger('?= new_df or new_dl                         :'
1248                ||fnd_const.bool(new_df or new_dl));
1249     end if;
1250 
1251 
1252     -- All conditions depend on whether there's a 'MON' in
1253     -- the NLS_DATE_FORMAT. If not, don't need to override.
1254 
1255     -- If there's a new NLS_DATE_FORMAT or NLS_DATE_LANGUAGE,
1256     -- try the override.
1257 
1258     -- If the parameter is null and there's a new NLS_DATE_LANGUAGE,
1259     -- try the override. This occurs when called from query_nls,
1260     -- typically during first initialization or when a public set_nls*
1261     -- routine calls reset_nls.
1262 
1263     -- If the parameter isn't null and is different than the current
1264     -- NLS_DATE_LANGUAGE, try the override. This occurs when
1265     -- the caller has supplied their own NLS_DATE_LANGUAGE.
1266 
1267     if mon_lk or mm_lk
1268       and ((nul_dl and new_dl)
1269            or (not nul_dl and chg_dl)
1270            or (new_df or new_dl))
1271     then
1272 
1273       declare
1274         -- Bug 6718678 and 5032384
1275         -- use nls_language instead of nls_date_language
1276         t_nls_date_language v$nls_parameters.value%type
1277           := nvl(p_nls_date_language,nls_language);
1278       begin
1279         /*
1280         select nls_date_language
1281           into v_nls_date_language
1282           from (select utf8_date_language nls_date_language
1283                   from fnd_languages
1284                  where nls_charset_name(nls_charset_id('CHAR_CS'))
1285                        in ('UTF8', 'AL32UTF8')
1286                    and installed_flag <>'D'
1287                    and nls_language = t_nls_date_language
1288                  union
1289                 select local_date_language nls_date_language
1290                   from fnd_languages
1291                  where nls_charset_name(nls_charset_id('CHAR_CS'))
1292                        not in ('UTF8', 'AL32UTF8')
1293                    and installed_flag <>'D'
1294                    and nls_language = t_nls_date_language);
1295         */
1296 
1297         /* Bug 10057138: The following query was formulated by
1298            Enrique Miranda and was approved by Jinsoo Eo of the ATG
1299            Performance Team.
1300 
1301            The query above was raised by GSI as the cause of a high number
1302            of sessions waiting on library cache, which consequently lead to
1303            a high CPU and database crash.
1304          */
1305         select decode(v_nls_charset_name,
1306                'UTF8', fl.utf8_date_language,
1307                'AL32UTF8', fl.utf8_date_language,
1308                fl.local_date_language
1309                ) data_rtn
1310         into  v_nls_date_language
1311         from fnd_languages fl
1312         where fl.nls_language = t_nls_date_language
1313         and fl.installed_flag <>'D' ;
1314 
1315         if debug_to_core then
1316           debugger('^ Changing NLS_DATE_LANGUAGE');
1317           debugger('^  from: '||t_nls_date_language);
1318           debugger('^    to: '||v_nls_date_language);
1319         end if;
1320 
1321       exception
1322         when no_data_found then
1323           if debug_to_core then
1324             debugger('^ No data for FND_LANGUAGE: '||t_nls_date_language);
1325           end if;
1326       end;
1327 
1328     else
1329       if debug_to_core then
1330         debugger('^ No reason to change NLS_DATE_FORMAT: '||nls_date_format);
1331       end if;
1332     end if;
1333 
1334     -- Bug 8252659: shashimo
1335     -- There are two sets of common month names in the Arab World:
1336     -- Jordanian and Egyptian
1337     -- If the user preference language is Arabic and user preference
1338     -- territory is any of Jordan, Lebanon, Syrian Arab Republic, or Iraq,
1339     -- the Jordanian flavor of month name should be used.
1340     -- For other territories, Egyptian flavor of month name should be used.
1341     -- This does not apply to 11i.
1342     --
1343     -- Bug 10359373: Backport fix of 8252659 to 11i for bug 9685123 by
1344     -- removing the release version check
1345     --
1346     if v_nls_date_language = 'ARABIC' and
1347       nls_territory not in ('JORDAN', 'LEBANON', 'SYRIA', 'IRAQ') then
1348       if debug_to_core then
1349         debugger('^ Changing NLS_DATE_LANGUAGE from ARABIC to EGYPTIAN');
1350       end if;
1351       v_nls_date_language := 'EGYPTIAN';
1352     end if;
1353 
1354     return v_nls_date_language;
1355 
1356   end override_nls_date_language;
1357 
1358   -- See this routine's associated function.
1359   procedure override_nls_date_language
1360   is
1361     tmp v$nls_parameters.value%type;
1362   begin
1363     tmp := override_nls_date_language;
1364   end override_nls_date_language;
1365 
1366   --
1367   -- query NLS values
1368   --
1369   procedure query_nls is
1370   begin
1371     -- query to ensure the cache is accurate.
1372     -- not using FND_CONST in query to avoid SQL context switch.
1373     for nls in (select *
1374                   from v$nls_parameters
1375                  where parameter in (
1376                           'NLS_LANGUAGE',
1377                           'NLS_DATE_LANGUAGE',
1378                           'NLS_SORT',
1379                           'NLS_TERRITORY',
1380                           'NLS_DATE_FORMAT',
1381                           'NLS_NUMERIC_CHARACTERS',
1382                           'NLS_CHARACTERSET'
1383                       )) loop
1384         nls_context_change := put(nls.parameter,nls.value) or nls_context_change;
1385     end loop;
1386 
1387     if z_first_initialization then
1388       override_nls_date_language;
1389     end if;
1390 
1391   end query_nls;
1392 
1393   --
1394   -- Reset NLS initialization variables
1395   --
1396   procedure reset_nls is
1397   begin
1398 
1399     if z_first_initialization then
1400       query_nls;
1401       z_first_initialization := false;
1402     end if;
1403 
1404     z_init(FND_CONST.NLS_LANGUAGE) := false;
1405       z_init(FND_CONST.NLS_DATE_LANGUAGE) := false;
1406       z_init(FND_CONST.NLS_SORT) := false;
1407     z_init(FND_CONST.NLS_TERRITORY) := false;
1408       z_init(FND_CONST.NLS_DATE_FORMAT) := false;
1409       z_init(FND_CONST.NLS_NUMERIC_CHARACTERS) := false;
1410     z_init(FND_CONST.NLS_CHARACTERSET) := false;
1411 
1412   end reset_nls;
1413 
1414   --
1415   -- SET_NLS
1416   --
1417   -- This is the main NLS routine. All others call into this routine to set
1418   -- NLS values by calling dbms_session.set_nls (i.e. alter session) to set
1419   -- the following values in DB.
1420   --
1421   -- Notes:
1422   --       - Side effects of setting certain values
1423   --            - NLS_LANGUAGE
1424   --                 affects
1425   --                    NLS_DATE_LANGUAGE
1426   --                    NLS_SORT
1427   --            - NLS_TERRITORY
1428   --                 affects
1429   --                    NLS_DATE_FORMAT
1430   --                    NLS_NUMERIC_CHARACTERS
1431   --            - NLS_SORT affects no others
1432   --            - NLS_DATE_FORMAT affects no others
1433   --            - NLS_DATE_LANGUAGE affects no others
1434   --            - NLS_NUMERIC_CHARACTERS affects no others
1435 
1436   procedure set_nls(p_nls_language in varchar2 default null,
1437                     p_nls_date_language in varchar2 default null,
1438                     p_nls_sort in varchar2 default null,
1439                     p_nls_territory in varchar2 default null,
1440                     p_nls_date_format in varchar2 default null,
1441                     p_nls_numeric_characters in varchar2 default null
1442                     ) is
1443 
1444     -- The indenting below is intentional based on dependent NLS values.
1445     -- This is done throughout to indicate the ordering. That is, when
1446     -- the indentation isn't present, the order of the names does not
1447     -- follow this ordering. It was very confusing to me why the parameters
1448     -- to older NLS routines didn't follow a logical order. It doesn't even
1449     -- look like they were ordered based on most highly used.
1450 
1451     v_nls_language v$nls_parameters.value%type
1452         := upper(p_nls_language);
1453       v_nls_date_language v$nls_parameters.value%type
1454         := upper(p_nls_date_language);
1455       v_nls_sort v$nls_parameters.value%type
1456         := upper(p_nls_sort);
1457 
1458     v_nls_territory v$nls_parameters.value%type
1459         := upper(p_nls_territory);
1460       v_nls_date_format v$nls_parameters.value%type
1461         := upper(p_nls_date_format);
1462       v_nls_numeric_characters v$nls_parameters.value%type
1463         := p_nls_numeric_characters;
1464 
1465     v_nls_characterset v$nls_parameters.value%type
1466         := get(FND_CONST.NLS_CHARACTERSET);
1467 
1468     --
1469     -- calls dbms_session.set_nls
1470     function set_parameter(p_parameter varchar2, p_value in varchar2)
1471     return boolean is
1472     begin
1473 
1474       -- simply don't do anything if passed null.
1475       if p_value is null then
1476         return false;
1477       end if;
1478 
1479       if has_changed(p_parameter,p_value) then
1480         dbms_session.set_nls(p_parameter, '"'|| p_value ||'"');
1481         put(p_parameter,p_value);
1482         return true;
1483       end if;
1484 
1485       return false;
1486 
1487     exception
1488       when others then
1489         throw('fnd_global.set_nls.set_parameter('''||
1490               p_parameter||''','''||
1491               p_value||''')',
1492               sqlcode, dbms_utility.format_error_stack);
1493     end set_parameter;
1494 
1495     --
1496     -- calls dbms_session.set_nls
1497     procedure set_parameter(p_parameter varchar2, p_value in varchar2)
1498     is
1499       result boolean;
1500     begin
1501       result := set_parameter(p_parameter,p_value);
1502     end set_parameter;
1503 
1504   begin
1505 
1506     nls_context_change := false;
1507 
1508     if z_first_initialization then
1509 
1510       query_nls;
1511       z_first_initialization := false;
1512 
1513     else
1514 
1515       -- Although there is a performance improvement in 10.2.0.2 eliminating
1516       -- the need to check whether NLS value have changed before setting them,
1517       -- See bug 5080655 for the explanation, this file is still under
1518       -- dual-checkin with 11.5 which doesn't include the improvement.
1519 
1520       --    - NLS_LANGUAGE
1521       --         affects
1522       --            NLS_DATE_LANGUAGE
1523       --            NLS_SORT
1524       --    - NLS_TERRITORY
1525       --         affects
1526       --            NLS_DATE_FORMAT
1527       --            NLS_NUMERIC_CHARACTERS
1528 
1529       pre_nls_change;
1530 
1531       -- If NLS_LANGUAGE changed, clear the cached, derived values.
1532       -- This ensures that the passed, derived values are set if different than
1533       -- the default, derived value for this language.
1534       if set_parameter(FND_CONST.NLS_LANGUAGE, v_nls_language) then
1535         z_context(FND_CONST.NLS_DATE_LANGUAGE) := null;
1536         z_context(FND_CONST.NLS_SORT) := null;
1537       end if;
1538 
1539       -- if NLS_TERRITORY changed, clear the cached, derived values
1540       -- This ensures that the passed, derived values are set if different than
1541       -- the default, derived value for this territory.
1542       if set_parameter(FND_CONST.NLS_TERRITORY, v_nls_territory) then
1543         z_context(FND_CONST.NLS_DATE_FORMAT) := null;
1544         z_context(FND_CONST.NLS_NUMERIC_CHARACTERS) := null;
1545       end if;
1546 
1547       -- Requery derived values to avoid calling dbms_session.set_nls
1548       -- in case the derived value is the same as the passed parameter value.
1549       -- In other words, it ensures that the following set_parameter calls
1550       -- don't do anything if the value in the database is the same as the
1551       -- passed parameter due to the value passed already being the default
1552       -- for the specified language or territory.
1553       -- This is where bug 5080655 and the improvement to dbms_session.set_nls
1554       -- affects us since we have to retain the nls values in the cache since
1555       -- we have code the depends on knowing when nls context changes.
1556       if (v_nls_language is not null
1557          and (v_nls_date_language is null
1558               or v_nls_sort is null))
1559       or (v_nls_territory is not null
1560           and (v_nls_date_format is null
1561                or v_nls_numeric_characters is null)) then
1562         query_nls;
1563       end if;
1564 
1565       -- NOTE: NLS_DATE_FORMAT must come before NLS_DATE_LANGUAGE.
1566       -- Due to bug 5032374, we need to check the value of NLS_DATE_FORMAT
1567       -- to determine if NLS_DATE_LANGUAGE needs to be overridden.
1568       set_parameter(FND_CONST.NLS_DATE_FORMAT, v_nls_date_format);
1569       set_parameter(FND_CONST.NLS_NUMERIC_CHARACTERS, v_nls_numeric_characters);
1570 
1571       set_parameter(FND_CONST.NLS_SORT, v_nls_sort);
1572       -- Bug 6718678 and 5032384
1573       -- Instead of passing v_nls_date_language to override nls_date_language,
1574       -- pass the nls_language
1575       set_parameter(FND_CONST.NLS_DATE_LANGUAGE,
1576                     override_nls_date_language(v_nls_language));
1577 
1578       post_nls_change;
1579 
1580     end if;
1581 
1582   exception
1583     when others then
1584       throw('fnd_global.set_nls',
1585             sqlcode, dbms_utility.format_error_stack);
1586   end set_nls;
1587 
1588   -- legacy routine that simply calls through to set_nls.
1589   --
1590   procedure set_nls_context(p_nls_language in varchar2 default null,
1591                             p_nls_date_format in varchar2 default null,
1592                             p_nls_date_language in varchar2 default null,
1593                             p_nls_numeric_characters in varchar2 default null,
1594                             p_nls_sort in varchar2 default null,
1595                             p_nls_territory in varchar2 default null) is
1596   begin
1597 
1598     check_logging;
1599 
1600     if is_debugging then
1601       debugger('begin set_nls_context');
1602       debugger(dbms_utility.format_call_stack);
1603       dump_context;
1604     end if;
1605 
1606     reset_nls;
1607 
1608     -- NOTE: the parameter order is different to this call
1609     -- than the parent routine's parameters. set_nls is a new
1610     -- routine and the parameter order is based on value dependency
1611     -- rather than the apparent ad hoc order of the old routines.
1612     set_nls(p_nls_language,
1613               p_nls_date_language,
1614               p_nls_sort,
1615             p_nls_territory,
1616               p_nls_date_format,
1617               p_nls_numeric_characters);
1618 
1619     if is_debugging then
1620       dump_context;
1621       debugger('end set_nls_context');
1622     end if;
1623 
1624   end set_nls_context;
1625 
1626   -- simply calls through to set_nls then returns all the nls
1627   -- values in the respective out parameters.
1628   procedure set_nls(p_nls_language in varchar2 default null,
1629                     p_nls_date_format in varchar2 default null,
1630                     p_nls_date_language in varchar2 default null,
1631                     p_nls_numeric_characters in varchar2 default null,
1632                     p_nls_sort in varchar2 default null,
1633                     p_nls_territory in varchar2 default null,
1634                     p_db_nls_language out nocopy varchar2,
1635                     p_db_nls_date_format out nocopy varchar2,
1636                     p_db_nls_date_language out nocopy varchar2,
1637                     p_db_nls_numeric_characters out nocopy varchar2,
1638                     p_db_nls_sort out nocopy varchar2,
1639                     p_db_nls_territory out nocopy varchar2,
1640                     p_db_nls_charset out nocopy varchar2) is
1641   begin
1642 
1643     check_logging;
1644 
1645     if is_debugging then
1646       debugger('begin set_nls');
1647       debugger(dbms_utility.format_call_stack);
1648       dump_context;
1649     end if;
1650 
1651     reset_nls;
1652 
1653     -- NOTE: the parameter order is different to this call
1654     -- than the parent routine's parameters. set_nls is a new
1655     -- routine and the parameter order is based on value dependency
1656     -- rather than the apparent ad hoc order of the old routines.
1657     set_nls(p_nls_language,
1658               p_nls_date_language,
1659               p_nls_sort,
1660             p_nls_territory,
1661               p_nls_date_format,
1662               p_nls_numeric_characters);
1663 
1664     p_db_nls_language := nls_language;
1665     p_db_nls_date_format := nls_date_format;
1666     p_db_nls_date_language := nls_date_language;
1667     p_db_nls_numeric_characters := nls_numeric_characters;
1668     p_db_nls_sort := nls_sort;
1669     p_db_nls_territory := nls_territory;
1670     p_db_nls_charset := get(FND_CONST.NLS_CHARACTERSET);
1671 
1672     if is_debugging then
1673       dump_context;
1674       debugger('end set_nls');
1675     end if;
1676 
1677   exception
1678     when others then
1679       log('fnd_global.set_nls.13', sqlcode, dbms_utility.format_error_stack);
1680   end set_nls;
1681 
1682   --
1683   -- builds the conditions passed to fnd_product_initialization.
1684   -- deprecated
1685   --
1686   function build_conditions return varchar2
1687   is
1688     c integer;
1689     p varchar2(2000);
1690     conditions varchar2(80) := null;
1691     procedure build(name varchar2) is
1692     begin
1693       if is_new(name) or (name = 'NLS' and nls_context_change) then
1694         if conditions is not null then
1695           conditions := conditions || '_';
1696         end if;
1697         conditions := conditions || ''''||z_conditions_map(name)||'''';
1698       end if;
1699     end;
1700   begin
1701 
1702     c := z_conditions_map.count;
1703     p := z_conditions_map.first;
1704     for i in 1..c loop
1705       build(p);
1706       p := z_conditions_map.next(p);
1707     end loop;
1708 
1709     return conditions;
1710 
1711   end build_conditions;
1712 
1713   --
1714   -- backup z_context. see restore.
1715   --
1716   procedure backup_context
1717   is
1718     c integer;
1719     p varchar2(2000);
1720   begin
1721 
1722     c := z_context.count;
1723     p := z_context.first;
1724     for i in 1..c loop
1725       z_backup(p) := z_context(p);
1726       p := z_context.next(p);
1727     end loop;
1728 
1729   end backup_context;
1730 
1731   --
1732   --
1733   -- org context initialization routine.
1734   -- This routine was broken out of initialize to isolate changes related only
1735   -- to the org context. initialize will call this when it performs normal
1736   -- initialization. initialize(name, value) will call this when the org
1737   -- context is being changed for R12.
1738   --
1739   procedure initialize_org_context
1740   as
1741   begin
1742     --
1743     -- handle ORG context change
1744     --
1745     if is_new(FND_CONST.ORG_ID) then
1746 
1747       -- if passed an org context and it is invalid (null ORG_NAME), clear the
1748       -- org context. this will only ever occur when explicitly called with
1749       -- ORG_ID via initialize(varchar2,varchar2).
1750       if org_name = null then
1751         clear(FND_CONST.ORG_ID);
1752       end if;
1753 
1754     else
1755 
1756       --
1757       -- set ORG context if we didn't just set it and resp or appl context
1758       -- changed
1759       if resp_context_change or appl_context_change then
1760 
1761         declare
1762           defined boolean;
1763           v_org_id_s varchar2(30);
1764         begin
1765 
1766           -- For R12, MO supports Multiple Organization Access Control (MOAC)
1767           -- which allows access to multiple operating units during a session.
1768           -- FND still requires an org_id context to set for FND_PROFILE via
1769           -- FND_GLOBAL.ORG_ID. Per guidance from MO Team, there are 3
1770           -- profiles to determine the ORG_ID to set at initialization:
1771           -- MO: Security Profile
1772           -- MO: Default Operating Unit
1773           -- MO: Operating Unit
1774           if fnd_release.major_version >= 12 then
1775             declare
1776               v_sec_prof_s varchar2(240);
1777             begin
1778               -- Bug 7109984
1779               -- Check the value of MO: Security Profile using get_specific
1780               -- since a context has not been set yet.
1781               fnd_profile.get_specific('XLA_MO_SECURITY_PROFILE_LEVEL',
1782                                         user_id,
1783                                         resp_id,
1784                                         resp_appl_id,
1785                                         v_sec_prof_s,
1786                                         defined);
1787 
1788               -- Bug 7109984
1789               -- If MO: Security Profile is not NULL then check for the
1790               -- MO: Default Operating Unit using get_specific since a context
1791               -- has not been set yet.
1792               if defined and v_sec_prof_s is not NULL then
1793                 fnd_profile.get_specific('DEFAULT_ORG_ID',
1794                                         user_id,
1795                                         resp_id,
1796                                         resp_appl_id,
1797                                         v_org_id_s,
1798                                         defined);
1799 
1800                 -- Bug 7109984
1801                 -- If MO: Default Operating Unit returns a value, set it as
1802                 -- the initial ORG_ID, the organization context.
1803                 -- Note: this would make the return value of
1804                 -- FND_GLOBAL.ORG_ID not always equal to the return value of
1805                 -- FND_PROFILE.value('ORG_ID') since FND_GLOBAL.ORG_ID refers
1806                 -- to the org context while FND_PROFILE.value('ORG_ID')
1807                 -- refers to the value of the profile option MO: Operating
1808                 -- Unit which are not the same.
1809                 --
1810                 -- Bug 10177414
1811                 -- The IF-THEN Block:
1812                 --    if defined and (org_id <> v_org_id_s) then
1813                 --       ...
1814                 --    else
1815                 --       put_i(FND_CONST.ORG_ID,'-1');
1816                 --    end if;
1817                 -- introduced an issue where IF defined and org_id=v_org_id_s,
1818                 -- then the organization context was reset to -1. Thus, the
1819                 -- IF-THEN block needed to be broken down further to
1820                 -- distinguish that case.
1821                 if defined and v_org_id_s is not NULL then
1822                    if (org_id <> v_org_id_s) then
1823                       put_i(FND_CONST.ORG_ID,v_org_id_s);
1824                    end if;
1825                 else
1826                   -- if DEFAULT_ORG_ID is null, set -1 as the organization
1827                   -- context.
1828                   put_i(FND_CONST.ORG_ID,'-1');
1829                 end if;
1830               else
1831                 -- If MO: Security Profile is not set, then get the value of
1832                 -- ORG_ID profile option. This get specific call has to be
1833                 -- made to get the org_id because the context is not yet set.
1834                 fnd_profile.get_specific('ORG_ID',
1835                                          user_id,
1836                                          resp_id,
1837                                          resp_appl_id,
1838                                          v_org_id_s,
1839                                          defined);
1840                 -- Bug 10177414
1841                 -- The IF-THEN Block:
1842                 --    if defined and (org_id <> v_org_id_s) then
1843                 --       ...
1844                 --    else
1845                 --       put_i(FND_CONST.ORG_ID,'-1');
1846                 --    end if;
1847                 -- introduced an issue where IF defined and org_id=v_org_id_s,
1848                 -- then the organization context was reset to -1. Thus, the
1849                 -- IF-THEN block needed to be broken down further to
1850                 -- distinguish that case.
1851                 if defined and v_org_id_s is not NULL then
1852                   if (org_id <> v_org_id_s) then
1853                      put_i(FND_CONST.ORG_ID,v_org_id_s);
1854                   end if;
1855                 else
1856                  -- if ORG_ID is null, set -1 as the organization context.
1857                  --
1858                  put_i(FND_CONST.ORG_ID,'-1');
1859                 end if;
1860               end if;
1861             end;
1862           else
1863             -- This is for 11i.
1864             -- This get specific call has to be made to get the org_id because
1865             -- the context is not yet set.
1866             fnd_profile.get_specific('ORG_ID',
1867                                       user_id,
1868                                       resp_id,
1869                                       resp_appl_id,
1870                                       v_org_id_s,
1871                                       defined);
1872 
1873             -- Bug 10177414
1874             -- The IF-THEN Block:
1875             --    if defined and (org_id <> v_org_id_s) then
1876             --       ...
1877             --    else
1878             --       put_i(FND_CONST.ORG_ID,'-1');
1879             --    end if;
1880             -- introduced an issue where IF defined and org_id = v_org_id_s,
1881             -- then the organization context was reset to -1. Thus, the IF-THEN
1882             -- block needed to be broken down further to distinguish that
1883             -- case.
1884             if defined and v_org_id_s is not NULL then
1885                if (org_id <> v_org_id_s) then
1886                   put_i(FND_CONST.ORG_ID,v_org_id_s);
1887                end if;
1888             else
1889               -- if ORG_ID is null, set -1 as the organization context.
1890               put_i(FND_CONST.ORG_ID,'-1');
1891             end if;
1892           end if;
1893 
1894         end;
1895 
1896       end if;
1897 
1898     end if;
1899 
1900     org_context_change := is_new(FND_CONST.ORG_ID);
1901 
1902     if org_context_change then
1903 
1904       -- for consistency with prior versions, have to fetch org_name here
1905       -- so it can be set on sys_context
1906       clear(FND_CONST.ORG_NAME);
1907 
1908       declare
1909         v_org_name varchar2(2000);
1910       begin
1911         v_org_name := org_name;
1912       end;
1913       -- This synchronizes the org context with the client_info space such
1914       -- that FND_GLOBAL.ORG_ID = substrb(userenv('CLIENT_INFO'),1,10).
1915       fnd_client_info.set_org_context(org_id);
1916       -- Reset the transient profile option CURRENT_ORG_CONTEXT if the org
1917       -- context changes. Re-initialize the org context for FND_PROFILE.
1918       if fnd_release.major_version >= 12 then
1919         fnd_profile.put('CURRENT_ORG_CONTEXT', org_id);
1920         fnd_profile.initialize_org_context;
1921       end if;
1922     end if;
1923 
1924     -- Get_specific checks the cache but does not save to it.
1925     -- Due to a write-no-package-state pragma restriction in get_specific, it
1926     -- cannot be changed to cache any profile options as that would result in
1927     -- a pragma violation. So we are pre fetching these values to force them to
1928     -- be cached before any get_specific calls are made.
1929     -- This, according to ATGPERF, results in a performance improvement since it
1930     -- will eliminate trips to the DB.
1931     declare
1932       torg_id number;
1933       tsec_prof number;
1934       tdef_org_id number;
1935 
1936     begin
1937       -- For 11i, this call will cache ORG_ID and serve as the org context,
1938       -- as well. However, in R12, ORG_ID is not necessarily required if the
1939       -- 'MO: Security Profile' is not null and 'MO: Default Organization Unit'
1940       -- returns a value. It will only be useful if there is an actual profile
1941       -- option value fetch for the ORG_ID profile option.
1942       --
1943       -- if release >= 12
1944       if fnd_release.major_version >= 12 then
1945          -- get XLA_MO_SECURITY_PROFILE_LEVEL value
1946          fnd_profile.get('XLA_MO_SECURITY_PROFILE_LEVEL',tsec_prof);
1947          -- if XLA_MO_SECURITY_PROFILE_LEVEL is not null
1948          if tsec_prof is not null then
1949             -- get DEFAULT_ORG_ID value
1950             fnd_profile.get('DEFAULT_ORG_ID',tdef_org_id);
1951          else
1952             -- if XLA_MO_SECURITY_PROFILE_LEVEL is null
1953             -- get ORG_ID value
1954             fnd_profile.get(FND_CONST.ORG_ID,torg_id);
1955          end if;
1956       else
1957          -- If release is less than R12 then need to fall back on ORG_ID for
1958          -- the org context.
1959          fnd_profile.get(FND_CONST.ORG_ID,torg_id);
1960       end if;
1961 
1962     end;
1963 
1964   end initialize_org_context;
1965 
1966   --
1967   --
1968   -- main initialization routine.
1969   -- initialize from z_init setting z_context.
1970   -- order of initialization is probably importan
1971   --
1972   --
1973   procedure initialize
1974   as
1975     n varchar2(2000);
1976     v varchar2(2000);
1977     prd_init_pkg_called boolean := false;
1978     l_tab_user_name   fnd_user.user_name%TYPE;   --- added for bug 13654980
1979     l_user_id  fnd_user.user_id%TYPE := user_id;  --- added for bug 13654980
1980   begin
1981 
1982     check_logging;
1983 
1984     if is_debugging then
1985       debugger('begin initialize');
1986       debugger(dbms_utility.format_call_stack);
1987       dump_context;
1988     end if;
1989 
1990     -- Store away the argument values passed in case needed later for a
1991     -- a restore.
1992     if get('PERMISSION_CODE') is not null then
1993        backup_context;
1994        clear('PERMISSION_CODE');
1995     end if;
1996 
1997     -- Increment our context page id by 1.
1998     session_context := session_context + 1;
1999 
2000     if is_debugging then
2001       debugger('.  session_context: '||session_context);
2002     end if;
2003 
2004     z_context_change_flag := null;
2005     z_security_context_change_flag := null;
2006 
2007     user_context_change := is_new(FND_CONST.USER_ID);
2008     resp_context_change := is_new(FND_CONST.RESP_ID);
2009     appl_context_change := is_new(FND_CONST.RESP_APPL_ID);
2010     sec_context_change := is_new(FND_CONST.SECURITY_GROUP_ID);
2011     server_context_change := is_new(FND_CONST.SERVER_ID);
2012     site_context_change := is_new(FND_CONST.SITE_ID);
2013 
2014     --
2015     -- NLS initialization
2016     --
2017     set_nls(get(FND_CONST.NLS_LANGUAGE),
2018            get(FND_CONST.NLS_DATE_LANGUAGE),
2019            get(FND_CONST.NLS_SORT),
2020            get(FND_CONST.NLS_TERRITORY),
2021            get(FND_CONST.NLS_DATE_FORMAT),
2022            get(FND_CONST.NLS_NUMERIC_CHARACTERS));
2023 
2024     --
2025     -- SECURITY_GROUP_ID initialization
2026     --
2027     if sec_context_change then
2028       fnd_client_info.set_security_group_context(security_group_id);
2029     end if;
2030 
2031     --
2032     -- If necessary, check if this resp_id is accessible from the user_id
2033     --
2034     n := FND_CONST.PROG_APPL_ID;
2035     if is_new(n) and get(n) = '-999' then
2036 
2037       set_undefined(FND_CONST.PROG_APPL_ID);
2038 
2039       -- if any of the values are undefined, there will be no
2040       -- valid responsibility so clear resp_id/resp_appl_id
2041       if is_undefined(FND_CONST.USER_ID)
2042         or is_undefined(FND_CONST.SECURITY_GROUP_ID)
2043         or is_undefined(FND_CONST.RESP_ID)
2044         or is_undefined(FND_CONST.RESP_APPL_ID) then
2045 
2046         set_undefined(FND_CONST.RESP_ID);
2047         set_undefined(FND_CONST.RESP_APPL_ID);
2048 
2049       else
2050 
2051         declare
2052           v_uid integer := user_id;
2053           v_rid integer := resp_id;
2054           v_raid integer := resp_appl_id;
2055           v_sgid integer := security_group_id;
2056           v_count integer;
2057         begin
2058 
2059           select count(*)
2060             into v_count
2061             from fnd_user_resp_groups u
2062            where sysdate between u.start_date and nvl(u.end_date, sysdate)
2063              and u.security_group_id in (0, v_sgid)
2064              and u.user_id = v_uid
2065              and u.responsibility_id = v_rid
2066              and u.responsibility_application_id = v_raid;
2067 
2068           -- If there is a row, then all is well so just continue.
2069           -- Otherwise, no rows means this resp doesn't have access,
2070           -- so set resp_id/resp_appl_id as undefined.
2071           if 0 = v_count then
2072             set_undefined(FND_CONST.RESP_ID);
2073             set_undefined(FND_CONST.RESP_APPL_ID);
2074           end if;
2075 
2076         end;
2077       end if;
2078     end if;
2079 
2080     --
2081     -- Get session_id for return.
2082     -- This is done here to save a round trip.  The value is passed
2083     -- back to the client and used to set a client-side profile.
2084     -- Pl/sql should get this value directly.
2085     --
2086     put_i(FND_CONST.SESSION_ID,userenv('SESSIONID'));
2087 
2088     --
2089     -- query user information if user_id changed
2090     --
2091     if user_context_change then
2092 
2093       -- Would be nice to query these on-demand but the pragmas are all RNDS.
2094 
2095       -- Select name globals that were not directly passed.
2096       -- (Only untranslated ones are set here - translated names
2097       --  are re-selected every time in case of language change.)
2098       --
2099       declare
2100         v_user fnd_user%rowtype;
2101       begin
2102         v_user.user_id := user_id;
2103 
2104         select u.user_name,
2105                nvl(u.employee_id, FND_CONST.UNDEFINED_I) employee_id,
2106                nvl(u.customer_id, FND_CONST.UNDEFINED_I) customer_id,
2107                nvl(u.supplier_id, FND_CONST.UNDEFINED_I) supplier_id,
2108                nvl(u.person_party_id, FND_CONST.UNDEFINED_I) person_party_id
2109           into v_user.user_name,
2110                v_user.employee_id,
2111                v_user.customer_id,
2112                v_user.supplier_id,
2113                v_user.person_party_id
2114           from fnd_user u
2115          where u.user_id = v_user.user_id;
2116 
2117         put(FND_CONST.USER_NAME,v_user.user_name);
2118         put(FND_CONST.EMPLOYEE_ID,v_user.employee_id);
2119         put(FND_CONST.CUSTOMER_ID,v_user.customer_id);
2120         put(FND_CONST.SUPPLIER_ID,v_user.supplier_id);
2121         put(FND_CONST.PARTY_ID,v_user.person_party_id);
2122 
2123       exception
2124         when no_data_found then
2125           clear(FND_CONST.USER_NAME);
2126           clear(FND_CONST.EMPLOYEE_ID);
2127           clear(FND_CONST.CUSTOMER_ID);
2128           clear(FND_CONST.SUPPLIER_ID);
2129           clear(FND_CONST.PARTY_ID);
2130 
2131           -- I'd like to clear user_id too since it failed, it isn't valid,
2132           -- but that's not consistent with the old code.
2133           -- clear(FND_CONST.USER_ID);
2134 
2135       end;
2136          --  The setting of database client identifier session has been moved
2137          --  to tag_db_session call as part of Connection Tagging project. Whenever, this
2138          --  api will be called, the client identifier attribute will be set.
2139     else
2140         --- Else has been added for bug 13654980 - user_name not getting updated
2141         --- in the cache after it has been succesfully changed in fnd_user table.
2142         begin
2143         select       fu.user_name
2144         into         l_tab_user_name
2145         from         fnd_user fu
2146         where        fu.user_id = l_user_id;
2147         exception when others then
2148            l_tab_user_name := null;
2149         end;
2150 
2151         if l_tab_user_name is not null and l_tab_user_name <> user_name then
2152                put(FND_CONST.USER_NAME,l_tab_user_name);
2153         end if;
2154         --- code changes for bug 13654980 end.
2155     end if;
2156 
2157     -- Would be nice to query these on-demand but the pragmas are all RNDS.
2158 
2159     -- query fnd_application data if resp_appl_id changed
2160     --
2161     if appl_context_change then
2162 
2163       clear(FND_CONST.APPLICATION_SHORT_NAME);
2164 
2165       -- avoid executing the query if RESP_APPL_ID is -1
2166       -- and just clear the APPLICATION_SHORT_NAME instead
2167       if is_defined(FND_CONST.RESP_APPL_ID) then
2168 
2169         declare
2170           v_asn fnd_application.application_short_name%type;
2171           v_raid fnd_application.application_id%type := resp_appl_id;
2172         begin
2173 
2174           select a.application_short_name
2175             into v_asn
2176             from fnd_application a
2177            where a.application_id = v_raid;
2178 
2179           put(FND_CONST.APPLICATION_SHORT_NAME,v_asn);
2180 
2181         exception
2182           when no_data_found then
2183             null;
2184         end;
2185 
2186       end if;
2187 
2188     end if;
2189 
2190     --
2191     -- handle ORG context change
2192     --
2193     -- Bug 8335361: Broke off org context-specific initialization routines for
2194     -- MOAC so that it can be called without re-initializing everything else.
2195     initialize_org_context;
2196 
2197     --
2198     -- Core Logging
2199     --
2200     if debug_to_core then
2201       fnd_core_log.write('FG.I',
2202                          user_id,
2203                          resp_id,
2204                          resp_appl_id,
2205                          org_id,
2206                          server_id);
2207     end if;
2208 
2209     --
2210     -- LOGGING and PROFILES initialization
2211     --
2212     if context_changed then
2213 
2214       if is_debugging then
2215         debugger('before fnd_profile.initialize');
2216         dump_context;
2217       end if;
2218 
2219       --
2220       -- Initialize profile cache
2221       --
2222       fnd_profile.initialize(user_id,
2223                              resp_id,
2224                              resp_appl_id,
2225                              site_id);
2226 
2227     end if;
2228 
2229     -- this just enables values to be put the profiles now.
2230     z_allow_profile_puts := true;
2231 
2232     --
2233     -- Profile value initializations
2234     --
2235     initialize_profile_values;
2236 
2237     if user_resp_changed then
2238 
2239       -- This will start up debug logging.
2240       -- This call must occur after profiles have been initialized and
2241       -- when there is a user and resp.
2242       fnd_log_repository.init(NULL,user_id);
2243 
2244     end if;
2245 
2246     --
2247     -- get the ENABLE_SECURITY_GROUPS profile option value (1/2/01) jvc
2248     --
2249     -- bug 12875860 - mskees - HR is sensitive to any base security context change
2250     -- user, resp, app or sec_group, Mr. Buzaki has pointed out to me in other
2251     -- bugs that customers can and will change profile definitions so where we can
2252     -- we should accommodate this behavior so we should get this value accordingly.
2253     --
2254     if security_context_changed then
2255       z_security_groups_enabled := 'Y' = fnd_profile.value('ENABLE_SECURITY_GROUPS');
2256     end if;
2257 
2258 /*  NOTE: the logic below seems incorrect ... just because Security groups is
2259 NOT enabled we should NOT be reseting SECURITY_GROUP_ID or sec_context_change
2260 both of these were set correctly in the code above and further based on those
2261 settings we have set the client info record.  It is my opinion that this code
2262 should be removed, BUT there is always the "well it has been this way forever"
2263 attitude and is it safe to change it now?  So the odds are that SECURITY_GROUP_ID
2264 is always '0' when HR is not used so that is why this has always worked, in older
2265 releases we actually used security groups so it is possible this would hurt some
2266 customers who still have customer code set up in an alternate security group.
2267 i propose we remove this code in the next major testing cycle for the beginning of
2268 12.2.1 - so it will get tested by the division to see if there are any unknown side
2269 effects.
2270 This code was added by Nix in version 115.99 as part of fix for bug 5404199 and
2271 only as part of an attempt to make sure that the values are NOT NULL or '-1', so
2272 we need to make sure that the value is properly inited when we set the sec group id.
2273 */
2274     if not z_security_groups_enabled then
2275       -- bug 12875860 adding this debug to confirm hypothesis above.
2276       if is_debugging then
2277         debugger('.  Security groups NOT enabled. SGID='||security_group_id);
2278       end if;
2279       put_i(FND_CONST.SECURITY_GROUP_ID,0);
2280       z_init(FND_CONST.SECURITY_GROUP_ID) := false;
2281       sec_context_change := false;
2282     else
2283       if is_debugging then
2284         debugger('.  Security groups enabled. SGID='||security_group_id);
2285       end if;
2286     end if;
2287 
2288     -- Profiles must be properly initialized before fnd_number.initialize.
2289     --
2290     -- Bug 2489275 - Since ICX_DATE_FORMAT_MASK is not tied to NLS_DATE_FORMAT,
2291     -- and the following relies on FND_GLOBAL.set_nls() being called when
2292     -- people want to initialize FND_DATE and FND_NUMBER packages, FND_DATE has
2293     -- been removed from this conditional and we leave FND_NUMBER which should
2294     -- be NLS related.
2295     --
2296     if nls_context_change then
2297       fnd_number.initialize();
2298     end if;
2299 
2300     -- Get_specific checks the cache but does not save to it.
2301     -- Due to a write-no-package-state pragma restriction in get_specific, it
2302     -- cannot be changed to cache any profile options as that would result in
2303     -- a pragma violation. So we are pre fetching these values to force them to
2304     -- be cached before any get_specific calls are made.
2305     -- This, according to ATGPERF, results in a performance improvement since it
2306     -- will eliminate trips to the DB.
2307     declare
2308       mrc_reporting_sob_id number;
2309       icx_language v$nls_parameters.value%type;
2310     begin
2311 
2312       if fnd_release.major_version < 12 then
2313         -- MRC is no longer used beyond 11i.
2314         fnd_profile.get('MRC_REPORTING_SOB_ID',mrc_reporting_sob_id);
2315         put('MRC_REPORTING_SOB_ID',mrc_reporting_sob_id);
2316       end if;
2317 
2318       fnd_profile.get(FND_CONST.ICX_LANGUAGE,icx_language);
2319       put(FND_CONST.ICX_LANGUAGE,icx_language);
2320 
2321     end;
2322 
2323     --
2324     -- Call routine to load MultiOrg and Multi-Currency info into
2325     -- the RDBMS session-level global variable that we read when we
2326     -- call USERENV('CLIENT_INFO')
2327     --
2328     if security_context_changed then
2329       fnd_client_info.setup_client_info(resp_appl_id,
2330                                         resp_id,
2331                                         user_id,
2332                                         security_group_id,
2333                                         org_id);
2334     end if;
2335 
2336     -- @todo is this too broad? should it just be if security_context_changed?
2337     if context_changed then
2338 
2339       -- Bug 2489275, FND_DATE.initialize will only be called with the
2340       -- ICX_DATE_FORMAT_MASK profile value if the profile value is NOT NULL
2341       -- and either this is the first time into FND_GLOBAL.initialize or there
2342       -- has been a real context change.
2343       declare
2344         user_calendar varchar2(20);
2345         plsql_block varchar2(240) := 'begin fnd_date.initialize_with_calendar(:1, null, :2); end;';
2346         -- This is declared as 240 simply because that is the maximum length of
2347         -- a profile value
2348         icx_date_format varchar(240) := fnd_profile.value('ICX_DATE_FORMAT_MASK');
2349       begin
2350         -- this should never happen, but just in case ICX_DATE_FORMAT is null,
2351         -- we'll let fnd_date default to whatever it does.
2352         if icx_date_format is not null then
2353           -- Bug 9536949: non-Gregorian calendar support was introduced for the
2354           -- R12.FND.B (12.1) release.
2355           if fnd_release.major_version >= 12 and
2356             fnd_release.minor_version > 0 then
2357             -- Fetch the value of the FND_FORMS_USER_CALENDAR. This profile was
2358             -- introduced in R12.FND.B. This profile should only be fetched if
2359             -- instance is 12.1 or higher. This saves a profile fetch for EBS
2360             -- instances < 12.1.
2361             user_calendar := nvl(fnd_profile.value('FND_FORMS_USER_CALENDAR'),'GREGORIAN');
2362             execute immediate plsql_block using icx_date_format, user_calendar;
2363           else
2364             fnd_date.initialize(icx_date_format);
2365           end if;
2366         end if;
2367       end;
2368 
2369     end if;
2370 
2371     --
2372     -- Set the Resource Consumer Group based on the profile
2373     --
2374     declare
2375       pro_rcg varchar2(30);
2376       old_rcg varchar2(30);
2377     begin
2378       begin
2379         fnd_profile.get('FND_RESOURCE_CONSUMER_GROUP', pro_rcg);
2380         if (pro_rcg is not null) then -- bug 4466432
2381           dbms_session.switch_current_consumer_group(pro_rcg,old_rcg,FALSE);
2382         end if;
2383       end;
2384     exception
2385       when others then
2386         null;
2387     end;
2388 
2389 
2390    -- FND_PRODUCT_INITIALIZATION is no longer supported in FND_Global for R12++ see:
2391    -- http://files.oraclecorp.com/content/AllPublic/SharedFolders/ATG%20Requirements-Public/R12/Requirements%20Definition%20Document/Performance/session_initialization_callbacks.doc
2392    -- Also see bug 5263334 for follow on discussions about use of DiscoInit and
2393    -- the fnd_product_initialization table.
2394    --
2395    -- The ONLY exception to this for R12++ is HR (prod=PER)
2396    -- and PER-dependent applications which require HR Security init function
2397    -- from the HR_SIGNON PKG that is used to setup HR Security group profiles which are
2398    -- bundled very tightly with FND Global and Profiles ... mskees.
2399     declare
2400       doInit     boolean;
2401       conditions varchar2(80) := build_conditions;
2402     begin
2403       -- 15959817  R12 check added back with checks for HR ...
2404       if fnd_release.major_version >= 12 then
2405         -- 12875860 expand the conditions to include all security context changes
2406         -- 16196565 remove specific check for PER application
2407         -- to allow init sequence for PER-dependent applications
2408         if application_short_name is not null and z_security_groups_enabled then
2409           doInit := true;
2410           -- 16196565 mask all R12 init conditions as SEC which triggers
2411           -- only the PER init function for PER and PER dependencies
2412           conditions := '''SEC''';
2413         else
2414           doInit := false;
2415         end if;
2416       else
2417        -- 11i Only do the initialization callbacks for a valid application.
2418         if application_short_name is not null and conditions is not null then
2419           doInit := true;
2420         else
2421           doInit := false;
2422         end if;
2423       end if;
2424 
2425       if doInit then
2426         put('fnd_prod_init.conditions',conditions);
2427         if is_debugging then
2428           debugger('.  fnd_product_initialization_pkg:'||application_short_name);
2429           debugger('.  conditions:'||conditions);
2430         end if;
2431         fnd_product_initialization_pkg
2432           .execInitFunction(application_short_name,conditions);
2433         -- Set Prod INIT flag, this indicates whether the fnd init code was executed
2434         -- and will be used later to determine how local values and HR PUT values
2435         -- are set.  If the init code was NOT executed and the PUT cache was
2436         -- cleared this will help know to reset the HR PUT values
2437         prd_init_pkg_called := true;
2438       end if;
2439     exception
2440       when others then
2441         put('fnd_prod_init.error',sqlerrm);
2442     end;
2443 
2444 
2445     --
2446     -- Custom initialization profile
2447     -- FND_INIT_SQL data should be moved to FND_PRODUCT_INITIALIZATION table.
2448     --
2449     declare
2450       curs integer;
2451       sqlbuf varchar2(2000);
2452       tmpbuf varchar2(2000) := get(FND_CONST.FND_INIT_SQL);
2453       rows integer;
2454     begin
2455 
2456       -- Bug 8335361: With MO_GLOBAL now calling FND_GLOBAL.INITIALIZE and
2457       -- FND_INIT_SQL allowing MO_GLOBAL APIs to be used, there is a possibility
2458       -- that a recursive call might occur. So, need to check whether the code
2459       -- is already in this code block.
2460       if not in_fnd_init_sql then
2461 
2462         -- Set flag to indicate that code flow is currently in this code block.
2463         in_fnd_init_sql := TRUE;
2464 
2465         if is_debugging then
2466           debugger('in_fnd_init_sql is NOW TRUE');
2467         end if;
2468 
2469         -- Check if FND_INIT_SQL has a value
2470         fnd_profile.get(FND_CONST.FND_INIT_SQL, sqlbuf);
2471 
2472         if sqlbuf is not null then
2473           -- If FND_CONST.FND_INIT_SQL is null or if the profile option value
2474           -- is different from FND_CONST.FND_INIT_SQL
2475           if (tmpbuf is null or sqlbuf <> tmpbuf) then
2476             if is_debugging then
2477               debugger('.  fnd_init_sql:'||sqlbuf);
2478             end if;
2479             -- Change FND_CONST.FND_INIT_SQL to new profile option value
2480             put(FND_CONST.FND_INIT_SQL,sqlbuf);
2481           end if;
2482 
2483           -- FND_INIT_SQL needs to execute for each initialization
2484           curs := dbms_sql.open_cursor;
2485           dbms_sql.parse(curs, sqlbuf, dbms_sql.v7);
2486           rows := dbms_sql.execute(curs);
2487           dbms_sql.close_cursor(curs);
2488         end if;
2489 
2490         -- Set flag to indicate that the code is exiting this code block.
2491         in_fnd_init_sql := FALSE;
2492         if is_debugging then
2493           debugger('in_fnd_init_sql is now FALSE');
2494         end if;
2495       else
2496         if is_debugging then
2497           debugger('in_fnd_init_sql is still TRUE');
2498         end if;
2499       end if; -- in_fnd_init_sql
2500     exception
2501       when others then
2502         -- Just in case...
2503         if (dbms_sql.is_open(curs)) then
2504           dbms_sql.close_cursor(curs);
2505         end if;
2506         in_fnd_init_sql := FALSE;
2507         throw('fnd_global.initialize[fnd_init_sql]',
2508               sqlcode, dbms_utility.format_error_stack);
2509     end;
2510 
2511     -- Bug 12875860 - mskees - HR security group profile control
2512     --
2513     -- "ENABLE_SECURITY_GROUPS = 'N' -> use actual FND table values stored by
2514     -- standard FND profile forms and code, use normal FND profile hierarchy
2515     -- behavior returning value at site level unless set at one of the lower
2516     -- security/hierarchy levels RESP_ID, APP_ID or USER.
2517     --
2518     -- "ENABLE_SECURITY_GROUPS = 'Y' -> call HR_SIGNON.INITIALIZE_HR_SECURITY
2519     -- via fnd_product_initialization_pkg for any change in user_id,
2520     -- application_id, responsibility_id or security_group_id.  This will
2521     -- set/reset the Profile PUT cache values for these PER profiles, we will
2522     -- then set FND_GLOBAL local variables for both PER_SECURITY_PROFILE_ID and
2523     -- PER_BUSINESS_GROUP_ID."
2524     --
2525     -- But if INITIALIZE is called twice with the same or similar parameters
2526     -- we will reset the profile cache but since there has been no context
2527     -- change we will not call the product initialization code again, so we
2528     -- must make sure that these two values are re-cached from the Global CONST
2529     -- values.
2530 
2531     if z_security_groups_enabled then
2532     -- "ENABLE_SECURITY_GROUPS = 'Y' ->  HR controlled values
2533 
2534       if ( prd_init_pkg_called and security_context_changed ) then
2535       -- HR_SIGNON.INITIALIZE_HR_SECURITY was called they updated profile PUT's
2536       -- so we should load Local CONST values
2537 
2538         -- get value from PUT cache
2539         v := fnd_profile.value(FND_CONST.PER_BUSINESS_GROUP_ID);
2540         -- load local value
2541         put(FND_CONST.PER_BUSINESS_GROUP_ID,v,false);
2542 
2543         -- get value from PUT cache
2544         v := fnd_profile.value(FND_CONST.PER_SECURITY_PROFILE_ID);
2545         -- load local value
2546         put(FND_CONST.PER_SECURITY_PROFILE_ID,v,false);
2547 
2548         if is_debugging then
2549           debugger('HR context change');
2550           debugger('HR PER_BUSINESS_GROUP_ID: '||get(FND_CONST.PER_BUSINESS_GROUP_ID));
2551           debugger('HR PER_SECURITY_PROFILE_ID: '||get(FND_CONST.PER_SECURITY_PROFILE_ID));
2552         end if;
2553 
2554       else
2555       -- HR_SIGNON not called but we are in security group mode
2556       -- this could be a duplicated call to init with same values
2557       -- this could be a change in NLS, ORG or Server
2558 
2559         if FND_PROFILE.PUT_CACHE_CLEARED then
2560         -- profile PUT cache was cleared so we need to reset HR values from
2561         -- previous call to HR_SIGNON as stored in CONST local values
2562           fnd_profile.put(FND_CONST.PER_BUSINESS_GROUP_ID, get(FND_CONST.PER_BUSINESS_GROUP_ID));
2563           fnd_profile.put(FND_CONST.PER_SECURITY_PROFILE_ID, get(FND_CONST.PER_SECURITY_PROFILE_ID));
2564 
2565             if is_debugging then
2566               debugger('HR_SIGNON not called but PUT cleared');
2567               debugger('HR PER_BUSINESS_GROUP_ID: '||get(FND_CONST.PER_BUSINESS_GROUP_ID));
2568               debugger('HR PER_SECURITY_PROFILE_ID: '||get(FND_CONST.PER_SECURITY_PROFILE_ID));
2569             end if;
2570 
2571         end if;
2572         -- else profile PUT cache was not cleared so previous HR values and our
2573         -- local CONST values are still in place
2574 
2575       end if;
2576     else
2577     -- "ENABLE_SECURITY_GROUPS = 'N' -> use FND table values
2578     -- we clear PUT cache here to make sure these values come from DB on fetch
2579     -- historicly we have not, yet it worked,  i assume since according to HR
2580     -- once enabled it is permanent in the instance for HR ... but the
2581     -- ENABLE_SECURITY_GROUPS is also settable at app level so for other
2582     -- prods it may/should not be enabled but then they are not likely using
2583     -- PER profiles so we have never seen a bug.
2584 
2585       -- first clear PUT cache
2586       fnd_profile.put(FND_CONST.PER_BUSINESS_GROUP_ID, FND_DELETE_VALUE);
2587       fnd_profile.put(FND_CONST.PER_SECURITY_PROFILE_ID, FND_DELETE_VALUE);
2588 
2589       -- get real profile value
2590       v := fnd_profile.value(FND_CONST.PER_BUSINESS_GROUP_ID);
2591       -- load local value
2592       put(FND_CONST.PER_BUSINESS_GROUP_ID,v,false);
2593 
2594       -- get real profile value
2595       v := fnd_profile.value(FND_CONST.PER_SECURITY_PROFILE_ID);
2596       -- load local value
2597       put(FND_CONST.PER_SECURITY_PROFILE_ID,v,false);
2598 
2599       -- put debug out just for FYI
2600       if is_debugging then
2601         debugger('NOT HR security group');
2602         debugger('HR PER_BUSINESS_GROUP_ID: '||get(FND_CONST.PER_BUSINESS_GROUP_ID));
2603         debugger('HR PER_SECURITY_PROFILE_ID: '||get(FND_CONST.PER_SECURITY_PROFILE_ID));
2604       end if;
2605 
2606     end if;
2607 
2608 
2609     -- fetch all the logging profiles so that logging can
2610     -- be properly initialized
2611     put_from_profile(FND_CONST.AFLOG_ENABLED);
2612     put_from_profile(FND_CONST.AFLOG_MODULE);
2613     put_from_profile(FND_CONST.AFLOG_LEVEL);
2614     put_from_profile(FND_CONST.AFLOG_FILENAME);
2615 
2616     -- clear all the derived values that are cached as a
2617     -- result of lazy initialization.
2618     clear_derived_values;
2619 
2620     if is_debugging then
2621       dump_context;
2622       debugger('end initialize');
2623     end if;
2624 
2625     -- don't allow any puts to profiles until this flag is true,
2626     -- which will be the next time through initialize, at the appropriate point.
2627     z_allow_profile_puts := false;
2628 
2629   end initialize;
2630 
2631   --
2632   -- p_hashtable is a name/value pairs to initialize.
2633   procedure initialize(p_mode in varchar2,
2634                        p_nv in out nocopy fnd_const.t_hashtable)
2635   as
2636     c integer;
2637     p varchar2(2000);
2638   begin
2639 
2640     if p_mode = FND_CONST.MODE_IN or p_mode = FND_CONST.MODE_INOUT then
2641 
2642       z_init.delete;
2643 
2644       c := p_nv.count;
2645       p := p_nv.first;
2646       for i in 1..c loop
2647         put(upper(p),p_nv(p));
2648         p := p_nv.next(p);
2649       end loop;
2650 
2651       z_force_init := force_init;
2652       initialize;
2653 
2654     end if;
2655 
2656     if p_mode = FND_CONST.MODE_OUT or p_mode = FND_CONST.MODE_INOUT then
2657 
2658       c := z_context.count;
2659       p := z_context.first;
2660       for i in 1..c loop
2661         p_nv(p) := z_context(p);
2662         p := z_context.next(p);
2663       end loop;
2664 
2665     end if;
2666 
2667   end initialize;
2668 
2669   --
2670   -- p_nv is a name/value pairs to initialize.
2671   procedure initialize(p_nv in out nocopy fnd_const.t_hashtable)
2672   as
2673   begin
2674     initialize(FND_CONST.MODE_INOUT,p_nv);
2675   end initialize;
2676 
2677   --
2678   -- initialize a single attribute
2679   --
2680   procedure initialize(name varchar2, value varchar2)
2681   as
2682   begin
2683 
2684     z_init.delete;
2685 
2686     -- If initialize was called for an org_context change and the applications
2687     -- release is R12, then the code will just change the org context if the
2688     -- current org_context is not equal to the org_context passed in.
2689     if upper(name) = 'ORG_ID' and fnd_release.major_version >= 12 then
2690       if (FND_GLOBAL.ORG_ID <> to_number(value)) then
2691         if is_debugging then
2692           -- This will indicate the caller of this API when an attempt to
2693           -- change the org context is made.
2694           debugger(dbms_utility.format_call_stack);
2695           -- This indicates what the org context is being changed to, and what
2696           -- it is currently set to.
2697           debugger('Org Context change:New ORG_ID='||value||' FND_GLOBAL.ORG_ID='
2698             ||FND_GLOBAL.ORG_ID);
2699         end if;
2700         put(upper(name),value);
2701         initialize_org_context;
2702       else
2703         if is_debugging then
2704           debugger('Org Context unchanged: FND_GLOBAL.ORG_ID=ORG_ID');
2705         end if;
2706       end if;
2707     else
2708       -- Initialize normally
2709       put(upper(name),value);
2710       initialize;
2711     end if;
2712 
2713   end initialize;
2714 
2715   --
2716   -- SET_SECURITY_GROUP_ID_CONTEXT
2717   -- Set the FND.SECURITY_GROUP_ID for SYS_CONTEXT as used by SECURITY_GROUP_ID_POLICY
2718   -- INTERNAL AOL USE ONLY
2719   --
2720   procedure set_security_group_id_context(security_group_id in number) is
2721   begin
2722     initialize(FND_CONST.SECURITY_GROUP_ID, to_char(security_group_id));
2723   exception
2724     when others then
2725       log('fnd_global.set_security_group_id_context',
2726           sqlcode, dbms_utility.format_error_stack);
2727   end;
2728 
2729   --
2730   --
2731   --
2732   --
2733   procedure apps_initialize(user_id in number,
2734                             resp_id in number,
2735                             resp_appl_id in number,
2736                             security_group_id in number default 0,
2737                             server_id in number default -1)
2738   is
2739     session_id number := null;
2740   begin
2741 
2742    initialize(session_id, user_id, resp_id, resp_appl_id,
2743               security_group_id, -1, -1, -1, -1, -1, -1,
2744               null,null,null,null,null,null,server_id);
2745 
2746   end apps_initialize;
2747 
2748   --
2749   -- INITIALIZE
2750   -- Set new values for security globals when new login or responsibility
2751   -- INTERNAL AOL USE ONLY
2752   --
2753   procedure initialize(session_id in out nocopy number,
2754                        user_id               in number,
2755                        resp_id               in number,
2756                        resp_appl_id          in number,
2757                        security_group_id     in number,
2758                        site_id               in number,
2759                        login_id              in number,
2760                        conc_login_id         in number,
2761                        prog_appl_id          in number,
2762                        conc_program_id       in number,
2763                        conc_request_id       in number,
2764                        conc_priority_request in number,
2765                        form_id               in number default null,
2766                        form_appl_id          in number default null,
2767                        conc_process_id       in number default null,
2768                        conc_queue_id         in number default null,
2769                        queue_appl_id         in number default null,
2770                        server_id             in number default -1)
2771   is
2772     v_nv fnd_const.t_hashtable;
2773 
2774     -- set a value in v_nv (list of new name/value pairs)
2775     procedure put_nv(name varchar2, value varchar2)
2776     as
2777     begin
2778       if value is not null then
2779         v_nv(name) := value;
2780       end if;
2781     end put_nv;
2782 
2783   begin
2784 
2785     put_nv(FND_CONST.USER_ID,to_char(user_id));
2786     put_nv(FND_CONST.RESP_ID,to_char(resp_id));
2787     put_nv(FND_CONST.RESP_APPL_ID,to_char(resp_appl_id));
2788     put_nv(FND_CONST.SECURITY_GROUP_ID,to_char(nvl(security_group_id,0)));
2789     put_nv(FND_CONST.SITE_ID,to_char(site_id));
2790     put_nv(FND_CONST.LOGIN_ID,to_char(login_id));
2791     put_nv(FND_CONST.CONC_LOGIN_ID,to_char(conc_login_id));
2792     put_nv(FND_CONST.PROG_APPL_ID,to_char(prog_appl_id));
2793     put_nv(FND_CONST.CONC_PROGRAM_ID,to_char(conc_program_id));
2794     put_nv(FND_CONST.CONC_REQUEST_ID,to_char(conc_request_id));
2795     put_nv(FND_CONST.CONC_PRIORITY_REQUEST,to_char(conc_priority_request));
2796     put_nv(FND_CONST.FORM_ID,to_char(nvl(form_id,FND_CONST.UNDEFINED_I)));
2797     put_nv(FND_CONST.FORM_APPL_ID,to_char(nvl(form_appl_id,FND_CONST.UNDEFINED_I)));
2798     put_nv(FND_CONST.CONC_PROCESS_ID,to_char(nvl(conc_process_id,FND_CONST.UNDEFINED_I)));
2799     put_nv(FND_CONST.CONC_QUEUE_ID,to_char(nvl(conc_queue_id,FND_CONST.UNDEFINED_I)));
2800     put_nv(FND_CONST.QUEUE_APPL_ID,to_char(nvl(queue_appl_id,FND_CONST.UNDEFINED_I)));
2801     put_nv(FND_CONST.SERVER_ID,to_char(server_id));
2802 
2803     -- mode_in since the only out variable can be obtained from a function
2804     initialize(FND_CONST.MODE_IN,v_nv);
2805 
2806     session_id := fnd_global.session_id;
2807 
2808   end initialize;
2809 
2810   --
2811   -- UNINITIALIZE, setting to null, all context values.
2812   --
2813   procedure uninitialize is
2814     c integer;
2815     p varchar2(2000);
2816   begin
2817 
2818     c := z_context.count;
2819     p := z_context.first;
2820     for i in 1..c loop
2821       set_undefined(p);
2822       p := z_context.next(p);
2823     end loop;
2824 
2825    z_first_initialization := true;
2826    initialize;
2827 
2828   end uninitialize;
2829 
2830   --
2831   -- INITIALIZE RT
2832   -- Set rt test id
2833   -- INTERNAL AOL USE ONLY
2834   --
2835   procedure rt_initialize(rt_test_id in number) is
2836   begin
2837       --
2838       -- Set globals from parameters
2839       --
2840       put(FND_CONST.RT_TEST_ID,rt_test_id);
2841 
2842   end RT_INITIALIZE;
2843 
2844   --
2845   --   Only a few Oracle FND developers will ever call this routine.
2846   --   Because it is so rare that anyone would ever call this
2847   --   routine, we aren't going to document it so as not to
2848   --   confuse people.  All you need to know is that calling this
2849   --   routine incorrectly can easily cause showstopper problems
2850   --   even for code outside your product.  So just don't call it
2851   --   unless you have been told to do so by an Oracle FND
2852   --   development manager.
2853   --
2854   --   in argument:
2855   --      permission_code- if you have permission to call this
2856   --                       you will have been given a unique code
2857   --                       that only you are allowed to pass to
2858   --                       confirm that your call is permitted.
2859   --
2860   --   see the internal oracle document for more details:
2861   --   http://www-apps.us.oracle.com/atg/plans/r115x/contextintegrity.txt
2862   --
2863   procedure bless_next_init(permission_code in varchar2) is
2864   begin
2865     if    ((permission_code >= 'FND_PERMIT_0000')
2866        and (permission_code <= 'FND_PERMIT_0500'))
2867     then
2868       put('PERMISSION_CODE',substrb(permission_code, 1, 30));
2869     end if;
2870   end;
2871 
2872   --
2873   -- Restores the context to the last "approved" value saved away,
2874   -- and gives a warning if the value was not already the approved value.
2875   --
2876   procedure restore is
2877   begin
2878 
2879     if (fnd_profile.value('FND_DEVELOPER_MODE') = 'Y') then
2880       raise_application_error(-20009,
2881         'Developer error: FND_GLOBAL initialization potential side effects. '||
2882         'Remove the call that initialized the context to resp_id: '||
2883         resp_id||' resp_appl_id: '||resp_appl_id||' user_id: '||
2884         user_id || '. '||
2885         'This message indicates that the context value set with the last ' ||
2886         'FND_GLOBAL init call could unintentionally affect code running ' ||
2887         'later on in the session. '||
2888         'This message indicates a problem in a previous call to FND_GLOBAL '||
2889         'initialization routines.  It does not indicate any problem with the '||
2890         'FND_GLOBAL package itself. '||
2891         'Unset the FND_DEVELOPER_MODE profile if you are seeing this '||
2892         'message in a production environment.');
2893     end if;
2894 
2895     initialize(z_backup);
2896 
2897     if(fnd_log.LEVEL_EXCEPTION >= fnd_log.g_current_runtime_level) then
2898       fnd_log.string(FND_LOG.LEVEL_EXCEPTION,
2899        'oracle.apps.plsql.fnd_global.restore.changed ',
2900        'Developer error: FND_GLOBAL initialization potential side effects.  '||
2901        'Remove the call that initialized the context to resp_id: '||
2902        resp_id||' resp_appl_id: '||resp_appl_id||' user_id: '||
2903        user_id || '. '||
2904        'This message indicates that the context value set with the last ' ||
2905        'FND_GLOBAL init call could unintentionally affect code running ' ||
2906        'later on in the session. '||
2907        'This message indicates a problem in a previous call to FND_GLOBAL '||
2908        'initialization routines.  It does not indicate any problem with the '||
2909        'FND_GLOBAL package itself. '||
2910        'Unset the FND_DEVELOPER_MODE profile if you are seeing this '||
2911        'message in a production environment.');
2912     end if;
2913 
2914   end;
2915 
2916   --
2917   -- Debugging Routines
2918   --
2919   --
2920   --
2921   --
2922 
2923   -- DUMP_CONTEXT
2924   --
2925   -- example output formats with comments interlaced:
2926   --    # this means NAME1's value has changed to VALUE
2927   -- *  NAME1=(5)VALUE
2928   --    # this means NAME2's value is unchanged
2929   -- .  NAME2=(9)NEW VALUE
2930   --    # this means NAME3's value has changed to V and is different than profiles
2931   -- *p NAME3=(1)V:(5)VALUE
2932 
2933   procedure dump_context is
2934     -- oops. sorry, these are very poor names.
2935     c integer;
2936     p varchar2(2000);
2937     b varchar2(3);
2938     f varchar2(300);
2939     g varchar2(300);
2940   begin
2941     -- force some derived value gets.
2942     p := application_name;
2943     p := base_language;
2944     p := current_language;
2945     p := org_name;
2946     c := language_count;
2947 
2948     -- dump the context
2949     debugger('z_contex');
2950     debugger('---------');
2951     c := z_context.count;
2952     p := z_context.first;
2953     for i in 1..c loop
2954 
2955       -- context values that have changed will start with '*',
2956       -- otherwise they start with '.'.
2957       if is_new(p) then b := '*'; else b := '.'; end if;
2958 
2959       -- the general 'name=(length)value' text
2960       g := p||'=('||length(z_context(p))||')'||substr(z_context(p),1,100);
2961 
2962       -- check if the name has a corresponding profile value
2963       f := fnd_profile.value(p);
2964 
2965       -- if there is a profile value but it's different than the context,
2966       -- add a 'p' to the change/no-change symbol and concatenate the
2967       -- profile as ':(length)value'
2968       if nvl(f,z_context(p)) <> z_context(p) then
2969         b := b||'p';
2970         g := g||':('||length(f)||')'||substr(f,1,100);
2971       else
2972         -- profile is null or the same as context value
2973         b := b||' ';
2974       end if;
2975 
2976       debugger(b||' '||g);
2977 
2978       p := z_context.next(p);
2979     end loop;
2980     debugger('---------'||fnd_const.newline);
2981 
2982   end;
2983 
2984 /* The function validates the value for module_type.
2985    It returns true, if module_type is null or invalid
2986    else returns false;
2987 */
2988 function is_invalid_module_type(module_type varchar2)
2989 return boolean is
2990 begin
2991   if(module_type is null or
2992          module_type not in (FRM,FWK,JTT,CP,WF,BES,REPORT,ALERT,ISG,GSM,HELP,BINARY_PROGRAM)) then
2993     return true;
2994   else
2995     return false;
2996   end if;
2997 end;
2998 
2999 /* This returns the length of MODULE and ACTION column of v$session.
3000    This ensures the support on any release of DB
3001 */
3002 function getColumnLength(cname varchar2)
3003 return number is
3004   len number := 0;
3005 begin
3006   select data_length
3007   into len
3008   from all_tab_columns
3009   where owner = 'SYS' and table_name = 'V_$SESSION' and column_name = cname;
3010   return len;
3011 exception
3012   when others then
3013     return len;
3014 end;
3015 
3016 /*
3017 * bug13831550 - performance issue, solved by calling the function
3018 *               getColumnLength only once per plsql-session.
3019 */
3020 Procedure setSessionColumnLength is
3021 begin
3022   if ( gbl_read_sinfo = -1 ) then
3023        gbl_session_module := getColumnLength('MODULE');
3024        gbl_session_action := getColumnLength('ACTION');
3025        gbl_read_sinfo := 0;
3026   end if;
3027 end setSessionColumnLength;
3028 
3029 
3030 /*  Overloaded procedure of tag_db_session. This is a PRIVATE API currently.
3031 *   This api will be used by module_types which does not set any context using
3032 *   fnd_global.apps_initialize. Sets the module and action field of v$session.
3033 *
3034 *   module_type : type of program being called (always in lowercase)
3035 *   appname : application name
3036 *   module_name : The module name that performs the action
3037 *   resp_app : responsibility application name
3038 *   resp_key : responsibility application key
3039 *   username : name of the user performing the action
3040 */
3041 procedure tag_db_session(
3042       module_type IN VARCHAR2,
3043       appname     IN VARCHAR2,
3044       module_name IN VARCHAR2,
3045       p_resp_appl IN VARCHAR2,
3046       p_resp_key  IN VARCHAR2,
3047       username  IN VARCHAR2)
3048 is
3049    module        V$SESSION.MODULE%TYPE;
3050    action        V$SESSION.ACTION%TYPE;
3051    l_module_type VARCHAR2(10) := lower(module_type); -- always in lowercase
3052    isConnEnabled varchar2(10);
3053    l_module_len integer;
3054    l_module_col_len integer;
3055    l_action_len integer;
3056    l_action_col_len integer;
3057    l_module_name varchar2(1000);
3058    l_module_name_prefix varchar2(100);
3059 begin
3060    isConnEnabled := fnd_profile.value('FND_CONNECTION_TAGGING');
3061    if(isConnEnabled is null or isConnEnabled = 'DISABLED') then
3062      return;
3063    end if;
3064    if(is_invalid_module_type(module_type)) then
3065      return;
3066    end if;
3067    /* bug13831550 */
3068    setSessionColumnLength;
3069    l_module_col_len := gbl_session_module;
3070    l_action_col_len := gbl_session_action;
3071    if( l_module_col_len = 0 or l_action_col_len = 0) then
3072       return;
3073    end if;
3074    -- Constructing MODULE information for tagging v$SESSION.MODULE
3075    -- If module_type = help, then no need to determine module_name
3076    if(HELP = l_module_type) then
3077       l_module_name := NULL;
3078       l_module_name_prefix := EBS ||':'|| 'fnd' ||':'|| l_module_type;
3079    else
3080       l_module_name := module_name;
3081       l_module_name_prefix := EBS ||':'|| appname ||':'|| l_module_type || ':';
3082    end if;
3083 
3084    l_module_len := length(l_module_name_prefix || l_module_name);
3085 
3086    if l_module_len > l_module_col_len then
3087       if (l_module_name is NOT NULL) then
3088          if (instr(lower(l_module_name), 'oracle.apps') <> 0) then
3089             -- MODULE_NAME has information in pkg structure. So do left truncation on passed in module_name.
3090          -- retrieves l_module_col_len characters from end;
3091             l_module_name := substr(l_module_name,-(length(l_module_name)-(l_module_len-l_module_col_len)),length(l_module_name)-(l_module_len-l_module_col_len));
3092             module := l_module_name_prefix || l_module_name;
3093          else
3094             -- MODULE_NAME doesn't has information in pkg structure. So do right truncation.
3095             module := substr(l_module_name_prefix || l_module_name, 1, l_module_col_len);
3096          end if;
3097       end if;
3098    else
3099       module := l_module_name_prefix || l_module_name;
3100    end if;
3101 
3102    -- Constructing ACTION information for tagging v$SESSION.ACTION
3103    if(HELP = l_module_type) then
3104       action := NULL;
3105    elsif(BINARY_PROGRAM = l_module_type) then
3106          -- then action is set to SYSADMIN by default.
3107          action := 'SYSADMIN';
3108    else
3109          l_action_len := length(p_resp_appl || '/' || p_resp_key);
3110          if l_action_len > l_action_col_len then
3111             action := substr(p_resp_appl || '/' || p_resp_key, 1, l_action_col_len);
3112     else
3113        action := p_resp_appl || '/' || p_resp_key;
3114          end if;
3115    end if;
3116 
3117    dbms_session.set_identifier(username);
3118    -- Set the module and action field of v$session
3119    dbms_application_info.set_module(module,action);
3120 exception
3121    when others then
3122       return;
3123 end tag_db_session;
3124 
3125 
3126 /*  Sets the module and action field of v$session. This API is used when a
3127 *   context has been established via fnd_global.apps_initialize().
3128 *   module_type : type of program/function/action being called (always in lowercase)
3129 *   module_name : The module or code class name that performs the action.
3130 */
3131 procedure tag_db_session(
3132       module_type in varchar2,
3133       module_name in varchar2)
3134 is
3135   l_app_short_name varchar2(50) := application_short_name;
3136   l_user_name varchar2(100) := user_name;
3137   l_resp_key varchar2(30) := resp_key;
3138 
3139 begin
3140 
3141    -- call overloaded tag_db_session
3142    -- application_short_name, resp_key, and user_name are fnd_global functions
3143    tag_db_session( module_type=>module_type,
3144                    appname=>l_app_short_name,
3145                    module_name=>module_name,
3146                    p_resp_appl=>l_app_short_name,
3147                    p_resp_key=>l_resp_key,
3148                    username=>l_user_name);
3149 end tag_db_session;
3150 
3151 
3152 /*  Overloaded function for tag_db_session.
3153 *   This api sets client_identifier and action to SYSADMIN
3154 *   module_type : type of program/function/action being called (always in lowercase)
3155 *   module_name : The module or code class name that performs the action.
3156 *   application_name : application short name to which the program_name belongs to.
3157 */
3158 procedure tag_db_session(
3159       module_type      IN VARCHAR2,
3160       module_name      IN VARCHAR2,
3161       application_name IN VARCHAR2)
3162 is
3163   l_user_name varchar2(100) := 'SYSADMIN';
3164 begin
3165    -- call overloaded tag_db_session
3166     tag_db_session( module_type=>module_type,
3167                     appname=>application_name,
3168                     module_name=>module_name,
3169                     p_resp_appl=>null,
3170                     p_resp_key=>null,
3171                     username=>l_user_name);
3172 end tag_db_session;
3173 
3174 begin
3175 
3176   -- all these constants can't reference the FND_CONST package
3177   -- because of pragam issues with the spec for this package.
3178 
3179   -- true means to initialize these values after calling
3180   -- fnd_profile.initialize.
3181   -- false is reserved for the PER* in which we don't want
3182   -- to do initialization along with the others.
3183   z_init_profiles('CONC_LOGIN_ID')           := true;
3184   z_init_profiles('CONC_PRIORITY_REQUEST')   := true;
3185   z_init_profiles('CONC_PROGRAM_ID')         := true;
3186   z_init_profiles('CONC_REQUEST_ID')         := true;
3187   z_init_profiles('LOGIN_ID')                := true;
3188   z_init_profiles('PER_BUSINESS_GROUP_ID')   := false;
3189   z_init_profiles('PER_SECURITY_PROFILE_ID') := false;
3190   z_init_profiles('PROG_APPL_ID')            := true;
3191   z_init_profiles('RT_TEST_ID')              := true;
3192 
3193   z_conditions_map('NLS')                    := 'NLS';
3194   z_conditions_map('ORG_ID')                 := 'ORG';
3195   z_conditions_map('RESP_APPL_ID')           := 'APPL';
3196   z_conditions_map('RESP_ID')                := 'RESP';
3197   z_conditions_map('SECURITY_GROUP_ID')      := 'SEC';
3198   z_conditions_map('SERVER_ID')              := 'SERVER';
3199   z_conditions_map('USER_ID')                := 'USER';
3200 
3201   z_syscontext('APPLICATION_SHORT_NAME')     := true;
3202   z_syscontext('CONC_LOGIN_ID')              := true;
3203   z_syscontext('CONC_PRIORITY_REQUEST')      := true;
3204   z_syscontext('CONC_PROCESS_ID')            := true;
3205   z_syscontext('CONC_PROGRAM_ID')            := true;
3206   z_syscontext('CONC_QUEUE_ID')              := true;
3207   z_syscontext('CONC_REQUEST_ID')            := true;
3208   z_syscontext('CUSTOMER_ID')                := true;
3209   z_syscontext('EMPLOYEE_ID')                := true;
3210   z_syscontext('FORM_APPL_ID')               := true;
3211   z_syscontext('FORM_ID')                    := true;
3212   z_syscontext('LOGIN_ID')                   := true;
3213   z_syscontext('ORG_ID')                     := true;
3214   z_syscontext('ORG_NAME')                   := true;
3215   z_syscontext('PARTY_ID')                   := true;
3216   z_syscontext('PER_BUSINESS_GROUP_ID')      := true;
3217   z_syscontext('PER_SECURITY_PROFILE_ID')    := true;
3218   z_syscontext('PROG_APPL_ID')               := true;
3219   z_syscontext('QUEUE_APPL_ID')              := true;
3220   z_syscontext('RESP_APPL_ID')               := true;
3221   z_syscontext('RESP_ID')                    := true;
3222   z_syscontext('SECURITY_GROUP_ID')          := true;
3223   z_syscontext('SERVER_ID')                  := true;
3224   z_syscontext('SESSION_ID')                 := true;
3225   z_syscontext('SITE_ID')                    := true;
3226   z_syscontext('SUPPLIER_ID')                := true;
3227   z_syscontext('USER_ID')                    := true;
3228   z_syscontext('USER_NAME')                  := true;
3229 
3230 end fnd_global;