DBA Data[Home] [Help]

PACKAGE BODY: APPS.FND_GLOBAL

Source


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