DBA Data[Home] [Help]

PACKAGE BODY: APPS.EDW_OWB_COLLECTION_UTIL

Source


1 package BODY EDW_OWB_COLLECTION_UTIL AS
2 /*$Header: EDWCOLUB.pls 120.7 2007/08/20 10:27:45 karthmoh ship $*/
3 
4 l_mapping_id number;
5 l_mapping_name varchar2(200);
6 l_dim_usage_id number;
7 l_dim_id number;
8 
9 --primary src
10 l_primary_src varchar2(300);
11 l_primary_src_ru number;
12 
13 l_child_level varcharTableType;
14 l_child_level_fk varcharTableType;
15 l_parent_level varcharTableType;
16 l_parent_level_pk varcharTableType;
17 l_parent_map   numberTableType;
18 l_parent_primary_src numberTableType;
19 l_parent_primary_target numberTableType;
20 l_number_children number:=1;
21 
22 --these hold the final ranks
23 l_hier		varcharTableType;
24 l_number_levels_hier numberTableType;
25 l_relation_level varcharTableType;
26 l_relation_level_status varcharTableType;
27 l_relation_number_of_children numberTableType;
28 l_relation_map  numberTableType;--the mapping id
29 l_running_count number;
30 l_relation_child_levels varcharTableType;
31 l_relation_child_fks varcharTableType;
32 l_relation_parent_pks   varcharTableType;
33 l_relation_primary_src numberTableType;
34 l_relation_primary_target numberTableType;
35 l_relation_number_of_levels number;
36 
37 --the mapping attributes
38 l_dim_col  varcharTableType;
39 l_level_name varcharTableType;
40 l_level_col varcharTableType;
41 l_number_mapping integer;
42 
43 
44 
45 
46 --not checked
47 cursor c0(p_dim_name varchar2) is
48 select
49 	b.mapping_id,
50 	c.target_usage_id,
51 	a.dim_id,
52     ltc.name,
53     ltc.elementid
54 from
55     edw_dimensions_md_v a,
56     edw_tables_md_v ltc,
57     edw_pvt_map_properties_md_v b,
58     edw_pvt_map_targets_md_v c
59 where
60     a.dim_name =p_dim_name
61 and b.primary_target=a.dim_id
62 and c.mapping_id=b.mapping_id
63 and c.target_id=a.dim_id
64 and ltc.elementid=b.primary_source;
65 --not checked
66 cursor c2II(p_dim_id number) is
67 select distinct
68     child_relation.name,
69     parent_relation.name,
70     child_fk_item.column_name,
71     parent_pk_item.column_name,
72     nvl(parent_map.mapping_id,0),
73     nvl(parent_map.primary_source,0),
74     nvl(parent_map.primary_target,0)
75 from
76     edw_levels_md_v lvl_child,
77     edw_levels_md_v lvl_parent,
78     edw_foreign_keys_md_v child_fk,
79     edw_pvt_key_columns_md_v child_fk_set_usage,
80     edw_pvt_columns_md_v     child_fk_item,
81     edw_unique_keys_md_v  parent_pk,
82     edw_pvt_key_columns_md_v parent_pk_set_usage,
83     edw_pvt_columns_md_v           parent_pk_item,
84     edw_tables_md_v child_relation,
85     edw_tables_md_v parent_relation,
86     edw_pvt_level_relation_md_v lvl_rel,
87     edw_hierarchies_md_v hier,
88     edw_pvt_map_properties_md_v parent_map
89 where
90     hier.dim_id=p_dim_id
91 and lvl_rel.hierarchy_id=hier.hier_id
92 and lvl_child.level_id=lvl_rel.child_level_id
93 and lvl_parent.level_id=lvl_rel.parent_level_id
94 and child_fk.entity_id=child_relation.elementid
95 and child_fk.key_id=parent_pk.key_id
96 and child_fk_set_usage.key_id=child_fk.foreign_key_id
97 and child_fk_item.column_id=child_fk_set_usage.column_id
98 and child_fk_item.parent_object_id=child_relation.elementid
99 and parent_pk.entity_id=parent_relation.elementid
100 and parent_pk_set_usage.key_id=parent_pk.key_id
101 and parent_pk_item.column_id=parent_pk_set_usage.column_id
102 and parent_pk_item.parent_object_id=parent_relation.elementid
103 and parent_relation.name=lvl_parent.level_name||'_LTC'
104 and child_relation.name=lvl_child.level_name||'_LTC'
105 and parent_map.primary_target(+)=parent_relation.elementid;
106 --not checked
107 --for single level dims
108 cursor c2II_I(p_dim_id number) is
109 select distinct
110     child_relation.name,
111     null,
112     null,
113     parent_pk_item.column_name,--actually child pk
114     parent_map.mapping_id, --actually child map
115     nvl(parent_map.primary_source,0),
116     nvl(parent_map.primary_target,0)
117 from
118     edw_levels_md_v lvl_child,
119     edw_unique_keys_md_v  parent_pk,
120     edw_pvt_key_columns_md_v parent_pk_set_usage,
121     edw_pvt_columns_md_v           parent_pk_item,
122     edw_tables_md_v child_relation,
123     edw_pvt_level_relation_md_v lvl_rel,
124     edw_hierarchies_md_v hier,
125     edw_pvt_map_properties_md_v parent_map
126 where
127     hier.dim_id=p_dim_id
128 and lvl_rel.hierarchy_id=hier.hier_id
129 and lvl_child.level_id=lvl_rel.child_level_id
130 and parent_pk.entity_id=child_relation.elementid
131 and parent_pk_set_usage.key_id=parent_pk.key_id
132 and parent_pk_item.column_id=parent_pk_set_usage.column_id
133 and parent_pk_item.parent_object_id=child_relation.elementid
134 and child_relation.name=lvl_child.level_name||'_LTC'
135 and parent_map.primary_target(+)=child_relation.elementid;
136 
137 --not checked
138 --get the attribute mapping
139 cursor c3(p_dimUsageId number, p_mapping_id number) is
140 select
141     upper(dim_item.column_name),
142     upper(relation.name),
143     upper(level_item.column_name)
144 from
145     edw_pvt_map_properties_md_v map_properties,
146     edw_pvt_map_sources_md_v map_sources,
147     (select * from edw_pvt_map_columns_md_v where mapping_id=p_mapping_id) map_columns,
148     edw_tables_md_v relation,
149     edw_pvt_columns_md_v	dim_item,
150     edw_pvt_columns_md_v	level_item
151 where
152   map_properties.mapping_id=p_mapping_id
153   and map_sources.mapping_id=map_properties.mapping_id
154   and map_columns.Target_column_id=dim_item.column_id
155   and map_columns.source_column_id=level_item.column_id
156   and relation.elementid=map_sources.source_id
157   and map_columns.Source_usage_id=map_sources.Source_usage_id;
158 
159 --get the rowid mappings
160 cursor c4(p_dim_id number) is
161 select
162     lvl.level_prefix||'_'||item.column_name||'_ROWID',
163     lvl.level_name||'_LTC',
164     'ROWID'
165 from
166     edw_levels_md_v lvl,
167     edw_pvt_level_columns_md_v  item
168 where
169     lvl.dim_id=p_dim_id
170 and item.level_id=lvl.level_id
171 and item.column_name like '%_PK';
172 
173 --not checked
174 cursor c5(p_target_relation varchar2) IS
175 select
176     	map.mapping_id,
177         nvl(map.primary_source,0),
178         nvl(map.primary_target,0)
179 from
180 	edw_pvt_map_properties_md_v map,
181 	edw_relations_md_v	relation
182 where
183 	map.primary_target=relation.relation_id
184 and	relation.relation_name=p_target_relation;
185 
186 --misc veriables
187 t_parent_level varchar2(300);
188 l_top_level varchar2(300);
189 l_top_index number;
190 l_found boolean:=TRUE;
191 l_child_true boolean;
192 l_child_found boolean:=false;
193 l_one_level_dim boolean:=false;
194 l_count integer;
195 l_start integer;
196 l_end integer;
197 
198 
199 
200 
201 PROCEDURE set_up(p_dimension_name in varchar2) IS
202 Begin
203 l_found:=true;
204 l_child_found:=false;
205 l_one_level_dim:=false;
206 l_dim_id:=null;
207 l_dim_usage_id:=null;
208 l_mapping_id:=null;
209 open c0(p_dimension_name);
210 fetch c0 into l_mapping_id, l_dim_usage_id, l_dim_id,
211     g_lowest_level, g_lowest_level_id;
212 close c0;
213 l_number_children:=1;
214 l_relation_number_of_levels:=1;
215 Exception when others then
216   g_status_message:=sqlerrm;
217   write_to_log_file_n('Error in set_up '||sqlerrm||get_time);
218 end;--procedure set_up(p_dimension_name in varchar2) IS
219 
220 procedure get_lowest_level(
221  p_level out NOCOPY varchar2,
222  p_level_id out NOCOPY number) is
223 begin
224   p_level:=g_lowest_level;
225   p_level_id:=g_lowest_level_id;
226 Exception when others then
227   g_status_message:=sqlerrm;
228   write_to_log_file_n('Error in get_lowest_level '||sqlerrm||get_time);
229 end;--procedure get_lowest_level
230 
231 
232 procedure Get_Level_Relations(
233 	p_levels out NOCOPY varcharTableType,
234 	p_level_status out NOCOPY varcharTableType,
235 	p_child_level_number out NOCOPY numberTableType,
236 	p_child_levels out NOCOPY varcharTableType,
237 	p_child_fk out NOCOPY varcharTableType,
238         p_parent_pk out NOCOPY varcharTableType,
239         p_number_levels out NOCOPY integer) IS
240 begin
241 l_found:=true;
242 l_child_found:=false;
243 l_one_level_dim:=false;
244 p_number_levels:=0;
245 if l_dim_id is null OR l_dim_usage_id is null OR  l_mapping_id is null then
246  return;
247 end if;
248 if g_debug then
249   write_to_log_file_n('Opening c2II using '||l_dim_id);
250 end if;
251 open c2II(l_dim_id);
252 loop
253   fetch c2II into
254     l_child_level(l_number_children),
255     l_parent_level(l_number_children),
256     l_child_level_fk(l_number_children),
257     l_parent_level_pk(l_number_children),
258     l_parent_map(l_number_children),
259     l_parent_primary_src(l_number_children),
260     l_parent_primary_target(l_number_children);
261   exit when c2II%NOTFOUND;
262   l_number_children:=l_number_children+1;
263 end loop;
264 l_number_children:=l_number_children-1;
265 close c2II;
266 if g_debug then
267   write_to_log_file_n('Processed c2II');
268 end if;
269 if g_debug then
270   write_to_log_file_n('The results of cursor c2II, number of children is '||l_number_children);
271   write_to_log_file('l_child_level  l_parent_level   l_child_level_fk  l_parent_level_pk '||
272                 ' l_parent_map  l_parent_primary_src  l_parent_primary_target ');
273   for i in 1..l_number_children loop
274     write_to_log_file(l_child_level(i)||'   '||l_parent_level(i)||'  '||l_child_level_fk(i)||'   '||
275         l_parent_level_pk(i)||'   '||l_parent_map(i)||'   '||l_parent_primary_src(i)||'  '||
276         l_parent_primary_target(i));
277   end loop;
278 end if;
279 if l_number_children=0 then --this could be a single level dim
280   write_to_log_file_n('This is a single level dimension');
281   l_number_children:=1;
282   l_one_level_dim:=true;
283   open c2II_I(l_dim_id);--for single level dim
284   loop
285     fetch c2II_I into
286     l_child_level(l_number_children),
287     l_parent_level(l_number_children),
288     l_child_level_fk(l_number_children),
289     l_parent_level_pk(l_number_children),
290     l_parent_map(l_number_children),
291     l_parent_primary_src(l_number_children),
292     l_parent_primary_target(l_number_children);
293     exit when c2II_I%NOTFOUND;
294     l_number_children:=l_number_children+1;
295   end loop;
296   l_number_children:=l_number_children-1;
297   close c2II_I;
298   l_relation_map(1):=l_parent_map(l_number_children);
299   l_relation_primary_src(1):=l_parent_primary_src(l_number_children);
300   l_relation_primary_target(1):=l_parent_primary_target(l_number_children);
301   if g_debug then
302     write_to_log_file_n('The results of cursor c2II_I, number of children is '||l_number_children);
303     write_to_log_file('l_child_level  l_parent_level   l_child_level_fk  l_parent_level_pk '||
304                   ' l_parent_map  l_parent_primary_src  l_parent_primary_target ');
305     for i in 1..l_number_children loop
306       write_to_log_file(l_child_level(i)||'   '||l_parent_level(i)||'  '||l_child_level_fk(i)||'   '||
307           l_parent_level_pk(i)||'   '||l_parent_map(i)||'   '||l_parent_primary_src(i)||'  '||
308           l_parent_primary_target(i));
309     end loop;
310   end if;
311 end if;--if l_number_children=0
312 
313 if l_number_children=0 then -- we have a problem here
314   if g_debug then
315     write_to_log_file_n('l_number_children=0. this is a fatal issue. No levels found');
316   end if;
317   p_levels:=l_relation_level;
318   p_level_status:=l_relation_level_status;
319   p_child_level_number:=l_relation_number_of_children;
320   p_child_levels:=l_relation_child_levels;
321   p_child_fk:=l_relation_child_fks;
322   p_parent_pk:=l_relation_parent_pks;
323   p_number_levels:=0;
324   return ;
325 end if;
326 
327 if l_one_level_dim = false then
328   --logic to give the rank
329   --find the parent level
330   for i in 1..l_number_children loop
331       --find out NOCOPY the child that is never a parent
332       t_parent_level:=l_parent_level(i);
333       l_found:=FALSE;
334       for j in 1..l_number_children loop
335           if t_parent_level=l_child_level(j) then
336               l_found:=true;
337               exit;
338           end if;
339       end loop;
340       if l_found=false then
341           l_top_level:=t_parent_level;
342           l_top_index:=i;
343           exit;
344       end if;
345   end loop;
346 
347   l_count:=1;
348   l_start:=1;
349   l_end:=1;
350   l_relation_level(1):=l_parent_level(l_top_index);
351   l_relation_map(1):=l_parent_map(l_top_index);
352   l_relation_primary_src(1):=l_parent_primary_src(l_top_index);
353   l_relation_primary_target(1):=l_parent_primary_target(l_top_index);
354   l_running_count:=0;
355   while true loop
356   l_relation_number_of_children(l_start):=0;
357 
358   if l_start=1 then
359       l_relation_level_status(l_start):='P';
360   else
361       l_relation_level_status(l_start):='I';
362   end if;
363 
364   l_child_true:=true;--first assume that this is a child
365   for i in 1..l_number_children loop
366       if l_parent_level(i)=l_relation_level(l_start) then
367 	  l_child_true:=false;
368           -- add the child
369           l_relation_map(l_start):=l_parent_map(i);
370           l_relation_primary_src(l_start):=l_parent_primary_src(i);
371     	  l_relation_primary_target(l_start):=l_parent_primary_target(i);
372           l_relation_number_of_children(l_start):=l_relation_number_of_children(l_start)+1;
373           l_running_count:=l_running_count+1;
374           l_relation_child_levels(l_running_count):=l_child_level(i);
375           l_relation_child_fks(l_running_count):=l_child_level_fk(i);
376           l_relation_parent_pks(l_running_count):=l_parent_level_pk(i);
377         --if it exists, do not add it
378         l_found:=false;
379         for j in 1..l_end loop
380             if l_relation_level(j)=l_child_level(i) then
381                 l_found:=true;
382                 exit;
383             end if;
384        end loop;
385        if l_found=false then
386         l_end:=l_end+1;
387         l_relation_level(l_end):=l_child_level(i);
388         l_count:=l_count+1;
389        end if;
390     end if;
391    end loop;--for i in 1..l_number_children loop
392   if l_child_true=true AND l_child_found=false then
393      l_child_found:=true;
394      --this is the child
395       l_relation_number_of_children(l_start):=0;
396       l_relation_level_status(l_start):='C';
397       --l_running_count:=l_running_count+1;
398       --l_relation_child_levels(l_running_count):=' ';
399       --l_relation_child_fks(l_running_count):=' ';
400       --l_relation_parent_pks(l_running_count):=' ';
401       open c5(l_relation_level(l_start));
402       fetch c5 into
403     	l_relation_map(l_start),
404     	l_relation_primary_src(l_start),
405     	l_relation_primary_target(l_start);
406       close c5;
407   end if;--if l_child_true=true then
408 
409   if l_start>=l_end AND l_child_found=true then
410     exit;--all set
411   end if;
412   l_start:=l_start+1;
413 
414  end loop;--while true loop
415 
416  l_relation_number_of_levels:=l_count;
417 
418 else --if l_number_children=1
419  l_relation_level:=l_child_level;
420  l_relation_level_status(1):='P';
421  l_relation_number_of_children(1):=0;
422  l_relation_child_levels(1):='';
423  l_relation_child_fks(1):='';
424  l_relation_parent_pks:=l_parent_level_pk;
425  l_relation_number_of_levels:=1;
426 
427 end if;
428 
429 --now assign the outputs
430 
431 p_levels:=l_relation_level;
432 p_level_status:=l_relation_level_status;
433 p_child_level_number:=l_relation_number_of_children;
434 p_child_levels:=l_relation_child_levels;
435 p_child_fk:=l_relation_child_fks;
436 p_parent_pk:=l_relation_parent_pks;
437 p_number_levels:=l_relation_number_of_levels;
438 
439 Exception when others then
440  g_status_message:=sqlerrm;
441  write_to_log_file_n('Error in Get_Level_Relations '||sqlerrm);
442 end; --THE procedure
443 
444 --get the mapping ids
445 PROCEDURE Get_mapping_ids(
446 	p_level_map_id out NOCOPY numberTableType,
447 	p_level_primary_src out NOCOPY numberTableType,
448 	p_level_primary_target out NOCOPY numberTableType) IS
449 Begin
450 
451 p_level_map_id:=l_relation_map;
452 p_level_primary_src:=l_relation_primary_src;
453 p_level_primary_target:=l_relation_primary_target;
454 
455 End;--PROCEDURE Get_mapping_ids
456 
457 --get the mapping
458 PROCEDURE Get_lvl_dim_mapping(
459     p_dim_col out NOCOPY varcharTableType,
460     p_level_name out NOCOPY varcharTableType,
461     p_level_col out NOCOPY varcharTableType,
462     p_number_mapping out NOCOPY integer,
463     p_flag in integer) IS
464 Begin
465 --if p_flag is 0, find out NOCOPY the rowid also.
466 --if p_flag is 1 then dont find the rowid
467 open c3(l_dim_usage_id, l_mapping_id);
468 l_number_mapping:=1;
469 loop
470     fetch c3 into
471     l_dim_col(l_number_mapping),
472     l_level_name(l_number_mapping),
473     l_level_col(l_number_mapping);
474     exit when c3%NOTFOUND;
475     l_number_mapping:=l_number_mapping+1;
476 end loop;
477 close c3;
478 l_number_mapping:=l_number_mapping-1;
479 if p_flag = 0 then
480   l_number_mapping:=l_number_mapping+1;--this is imp
481   --find all the rowid mapping
482   open c4(l_dim_id);
483   loop
484     fetch c4 into
485 	l_dim_col(l_number_mapping),
486 	l_level_name(l_number_mapping),
487 	l_level_col(l_number_mapping);
488     exit when c4%NOTFOUND;
489     l_number_mapping:=l_number_mapping+1;
490   end loop;
491   close c4;
492   l_number_mapping:=l_number_mapping-1;
493 end if;
494 p_dim_col:=l_dim_col;
495 p_level_name:=l_level_name;
496 p_level_col:=l_level_col;
497 p_number_mapping:=l_number_mapping;
498 Exception when others then
499   g_status_message:=sqlerrm;
500   write_to_log_file_n('Error in Util.Get_lvl_dim_mapping '||sqlerrm||get_time);
501 End;-- PROCEDURE Get_lvl_dim_mapping
502 
503 PROCEDURE Get_Fact_Ids(
504 	p_fact_name in varchar2,
505 	p_fact_map_id out NOCOPY number,
506 	p_fact_src out NOCOPY number,
507 	p_fact_target out NOCOPY number) IS
508 
509 Begin
510 --not checked
511 select
512   fact_map.mapping_id,
513   fact_map.primary_source,
514   fact_map.primary_target
515   into
516   p_fact_map_id,
517   p_fact_src,
518   p_fact_target
519 from
520   edw_facts_md_v fact,
521   edw_pvt_map_properties_md_v fact_map
522 where
523     fact.fact_name=p_fact_name
524 and fact_map.primary_target=fact.fact_id;
525 
526 Exception when others then
527   g_status_message:=sqlerrm;
528   write_to_log_file_n('Error in Get_Fact_Ids '||sqlerrm||get_time);
529 End;--PROCEDURE Get_Fact_Ids(p_fact_name in varchar2)
530 
531 function get_log_for_table(p_table varchar2, p_log varchar2) return varchar2 is
532 l_stmt varchar2(5000);
533 TYPE CurTyp IS REF CURSOR;
534 cv   CurTyp;
535 l_log_desc varchar2(400);
536 l_log_table varchar2(400);
537 Begin
538   if g_debug then
539     write_to_log_file_n('In get_log_for_table. param1='||p_table||' and param2='||p_log);
540   end if;
541   if p_log='SNAPSHOT-LOG' then
542     write_to_log_file_n('Going to call get_table_snapshot_log');
543     return get_table_snapshot_log(p_table);
544   end if;
545   l_log_desc :=p_log;
546   l_log_table:=null;
547   --p_table is the fact name
548   l_stmt:='select rel.name from edw_tables_md_v rel, edw_facts_md_v fact, edw_foreign_keys_md_v fk, '||
549   'edw_unique_keys_md_v pk where fact.fact_name=:a and fk.entity_id=rel.elementid '||
550   'and pk.entity_id=fact.fact_id and fk.key_id=pk.key_id  '||
551   'and fk.foreign_key_name like ''%_DLOG''';
552   open cv for l_stmt using p_table;
553   fetch cv into l_log_table;
554   close cv;
555   if l_log_table is null then
556     l_stmt:='select rel.RELATION_NAME from edw_relations_md_v rel, edw_facts_md_v fact, edw_foreign_keys_md_v fk, '||
557     'edw_unique_keys_md_v pk where fact.fact_name=:a and fk.entity_id=rel.RELATION_ID '||
558     'and pk.entity_id=fact.fact_id and fk.key_id=pk.key_id  '||
559     'and rel.description=:b';
560     open cv for l_stmt using p_table,l_log_desc;
561     fetch cv into l_log_table;
562     close cv;
563   end if;
564   return l_log_table;
565 Exception when others then
566   g_status_message:=sqlerrm;
567   write_to_log_file_n('Exception in get_ilog_for_table '||sqlerrm);
568   return null;
569 End;
570 
571 function get_columns_for_table(
572     p_table varchar2,
573     p_columns out NOCOPY varcharTableType,
574     p_number_columns out NOCOPY number) return boolean is
575 l_stmt varchar2(5000);
576 TYPE CurTyp IS REF CURSOR;
577 cv   CurTyp;
578 Begin
579   if g_debug then
580     write_to_log_file_n('In get_columns_for_table, Input params is '||p_table);
581   end if;
582   p_number_columns:=1;
583   l_stmt:='select col.column_name from edw_pvt_columns_md_v col, edw_relations_md_v rel where rel.relation_name=:s and '||
584             ' col.parent_object_id=rel.relation_id ';
585   open cv for l_stmt using p_table;
586   loop
587     fetch cv into p_columns(p_number_columns);
588     exit when cv%notfound;
589     p_number_columns:=p_number_columns+1;
590   end loop;
591   close cv;
592   p_number_columns:=p_number_columns-1;
593   return true;
594 Exception when others then
595   g_status_message:=sqlerrm;
596   write_to_log_file_n(g_status_message);
597   p_number_columns:=0;
598   return false;
599 End;
600 
601 function get_db_columns_for_table(
602     p_table varchar2,
603     p_columns out NOCOPY varcharTableType,
604     p_number_columns out NOCOPY number,
605     p_owner varchar2) return boolean is
606 l_data_type varcharTableType;
607 l_data_length varcharTableType;
608 l_num_distinct numberTableType;
609 l_num_nulls numberTableType;
610 l_avg_col_length numberTableType;
611 Begin
612   return get_db_columns_for_table(p_table,p_columns,l_data_type,l_data_length,l_num_distinct,
613   l_num_nulls,l_avg_col_length,p_number_columns,p_owner);
614 Exception when others then
615   g_status_message:=sqlerrm;
616   write_to_log_file_n(g_status_message);
617   p_number_columns:=0;
618   return false;
619 End;
620 
621 function get_db_columns_for_table(
622     p_table varchar2,
623     p_columns out NOCOPY varcharTableType,
624     p_data_type out NOCOPY varcharTableType,
625     p_number_columns out NOCOPY number,
626     p_owner varchar2) return boolean is
627 l_data_length varcharTableType;
628 l_num_distinct numberTableType;
629 l_num_nulls numberTableType;
630 l_avg_col_length numberTableType;
631 Begin
632   return get_db_columns_for_table(p_table,p_columns,p_data_type,l_data_length,l_num_distinct,
633   l_num_nulls,l_avg_col_length,p_number_columns,p_owner);
634 Exception when others then
635   g_status_message:=sqlerrm;
636   write_to_log_file_n(g_status_message);
637   p_number_columns:=0;
638   return false;
639 End;
640 
641 function get_db_columns_for_table(
642     p_table varchar2,
643     p_columns out NOCOPY varcharTableType,
644     p_data_type out NOCOPY varcharTableType,
645     p_data_length out NOCOPY varcharTableType,
646     p_num_distinct out NOCOPY numberTableType,
647     p_num_nulls out NOCOPY numberTableType,
648     p_avg_col_length out NOCOPY numberTableType,
649     p_number_columns out NOCOPY number,
650     p_owner varchar2) return boolean is
651 cursor cv(p_table varchar2,p_owner varchar2) is
652 select column_name,data_type,data_length,num_distinct,num_nulls,avg_col_len from all_tab_columns
653 where table_name=p_table and owner=p_owner;
654 l_table varchar2(80);
655 l_owner varchar2(80);
656 Begin
657   if g_debug then
658     write_to_log_file_n('In get_db_columns_for_table, Input params is '||p_table||
659     ' and owner '||p_owner);
660   end if;
661   l_owner:=p_owner;
662   if instr(p_table,'.')<>0 then
663     l_table:=substr(p_table,instr(p_table,'.')+1,length(p_table));
664   else
665     l_table:=p_table;
666   end if;
667   if l_owner is null then
668     l_owner:=get_table_owner(l_table);
669   end if;
670   p_number_columns:=1;
671   if g_debug then
672     write_to_log_file_n('select column_name,data_type,data_length,num_distinct,num_nulls,'||
673     'avg_col_len from all_tab_columns where table_name='||l_table||' and owner='||l_owner);
674   end if;
675   open cv(l_table,l_owner);
676   loop
677     fetch cv into p_columns(p_number_columns),
678     p_data_type(p_number_columns),
679     p_data_length(p_number_columns),
680     p_num_distinct(p_number_columns),
681     p_num_nulls(p_number_columns),
682     p_avg_col_length(p_number_columns);
683     exit when cv%notfound;
684     p_number_columns:=p_number_columns+1;
685   end loop;
686   p_number_columns:=p_number_columns-1;
687   close cv;
688   if g_debug then
689     write_to_log_file_n('Result');
690     for i in 1..p_number_columns loop
691       write_to_log_file(p_columns(i)||' '||p_data_type(i)||' '||p_data_length(i)||' '||
692       p_num_distinct(i)||' '||p_num_nulls(i)||' '||p_avg_col_length(i));
693     end loop;
694   end if;
695   return true;
696 Exception when others then
697   g_status_message:=sqlerrm;
698   write_to_log_file_n(g_status_message);
699   p_number_columns:=0;
700   return false;
701 End;
702 
703 function get_fks_for_table(
704     p_table varchar2,
705     p_fks out NOCOPY varcharTableType,
706     p_number_fks out NOCOPY number) return boolean is
707 l_stmt varchar2(5000);
708 TYPE CurTyp IS REF CURSOR;
709 cv   CurTyp;
710 Begin
711   if g_debug then
712     write_to_log_file_n('In get_fks_for_table, Input params is '||p_table);
713   end if;
714   p_number_fks:=1;
715   l_stmt:='select col.column_name from edw_pvt_columns_md_v col, edw_relations_md_v rel, edw_foreign_keys_md_v fk, '||
716   'edw_pvt_key_columns_md_v fku where rel.relation_name=:a and fk.entity_id=rel.relation_id and  '||
717   'fku.key_id=fk.foreign_key_id and col.column_id=fku.column_id and col.parent_object_id=rel.relation_id';
718   open cv for l_stmt using p_table;
719   loop
720     fetch cv into p_fks(p_number_fks);
721     exit when cv%notfound;
722     p_number_fks:=p_number_fks+1;
723   end loop;
724   close cv;
725   p_number_fks:=p_number_fks-1;
726   return true;
727 Exception when others then
728   g_status_message:=sqlerrm;
729   write_to_log_file_n('Exception in get_fks_for_table '||sqlerrm);
730   return false;
731 End;
732 
733 function get_fks_for_table(
734     p_table varchar2,
735     p_parent_table out NOCOPY varcharTableType,
736     p_fks out NOCOPY varcharTableType,
737     p_number_fks out NOCOPY number) return boolean is
738 l_stmt varchar2(5000);
739 TYPE CurTyp IS REF CURSOR;
740 cv   CurTyp;
741 Begin
742   if g_debug then
743     write_to_log_file_n('In get_fks_for_table, Input params is '||p_table);
744   end if;
745   p_number_fks:=1;
746   l_stmt:='select fk_item.column_name,p_rel.relation_name '||
747   'from  '||
748   'edw_relations_md_v rel,  '||
749   'edw_foreign_keys_md_v fk,  '||
750   'edw_pvt_key_columns_md_v fkisu,  '||
751   'edw_pvt_columns_md_v fk_item,  '||
752   'edw_relations_md_v p_rel,  '||
753   'edw_unique_keys_md_v pk  '||
754   'where  '||
755   'rel.relation_name=:a  '||
756   'and fk.entity_id=rel.relation_id '||
757   'and fkisu.key_id=fk.foreign_key_id '||
758   'and fk_item.column_id=fkisu.column_id '||
759   'and fk_item.parent_object_id=rel.relation_id '||
760   'and pk.key_id=fk.key_id '||
761   'and p_rel.relation_id=pk.entity_id';
762   open cv for l_stmt using p_table;
763   loop
764     fetch cv into p_fks(p_number_fks),p_parent_table(p_number_fks);
765     exit when cv%notfound;
766     p_number_fks:=p_number_fks+1;
767   end loop;
768   close cv;
769   p_number_fks:=p_number_fks-1;
770   return true;
771 Exception when others then
772   g_status_message:=sqlerrm;
773   write_to_log_file_n(g_status_message);
774   return false;
775 End;
776 
777 
778 function get_table_snapshot_log(p_table varchar2) return varchar2 is
779 l_stmt varchar2(5000);
780 TYPE CurTyp IS REF CURSOR;
781 cv   CurTyp;
782 l_var varchar2(400);
783 l_table_owner varchar2(30);
784 begin
785  if g_debug then
786    write_to_log_file_n('In  get_table_snapshot_log, input param is '||p_table);
787  end if;
788  --rkumar:bug#4905343
789  l_stmt:='select table_owner from user_synonyms where table_name=:a';
790  open cv for l_stmt using p_table;
791  fetch cv into l_table_owner;
792  close cv;
793 
794  l_stmt:='select log_table from all_snapshot_logs where master=:a and log_owner=:b'; --rkumar:bug#4905343
795 
796  open cv for l_stmt using p_table, l_table_owner;
797 
798  fetch cv into  l_var;
799  close cv;
800  if g_debug then
801    write_to_log_file_n('The snapshot log found for '||p_table||' is '||l_var);
802  end if;
803  return l_var;
804 Exception when others then
805   g_status_message:=sqlerrm;
806   write_to_log_file_n('Exception in get_table_snapshot_log '||sqlerrm);
807   return null;
808 End;
809 
810 function delete_table(p_table varchar2) return boolean is
811 l_stmt varchar2(1000);
812 begin
813   l_stmt:='delete '||p_table;
814   if g_debug then
815     write_to_log_file_n('Going to execute '||l_stmt);
816   end if;
817   execute immediate l_stmt;
818   if g_debug then
819     write_to_log_file_n('Deleted '||sql%rowcount||' records from '||p_table);
820   end if;
821   return true;
822 Exception when others then
823   g_status_message:=sqlerrm;
824   write_to_log_file_n('Exception in delete_table '||sqlerrm);
825   return false;
826 End;
827 
828 function truncate_table(p_table varchar2) return boolean is
829 Begin
830   return truncate_table(p_table,null);
831   return true;
832 Exception when others then
833   g_status_message:=sqlerrm;
834   write_to_log_file_n('Exception in truncate_table '||sqlerrm);
835   return false;
836 End;
837 
838 function truncate_table(p_table varchar2, p_owner varchar2) return boolean is
839 l_stmt varchar2(1000);
840 l_owner varchar2(200);
841 Begin
842   if g_debug then
843     write_to_log_file_n('In Util.truncate_table, p_table='||p_table||', p_owner='||p_owner);
844   end if;
845   if p_owner is null or instr(p_table,'.')<>0 then
846   --if p_owner is null then
847     if instr(p_table,'.')<>0 then
848       l_stmt:='truncate table '||p_table;
849     else
850       l_owner:=get_table_owner(p_table);
851       l_stmt:='truncate table '||l_owner||'.'||p_table;
852     end if;
853   else
854     l_stmt:='truncate table '||p_owner||'.'||p_table;
855   end if;
856   execute immediate l_stmt;
857   return true;
858 Exception when others then
859   g_status_message:=sqlerrm;
860   write_to_log_file_n('Exception in truncate_table '||sqlerrm);
861   return false;
862 End;
863 
864 
865 function value_in_table(
866     p_table varcharTableType,
867     l_number_table number,
868     p_value varchar2) return boolean is
869 Begin
870   if p_value is null or l_number_table <=0 then
871     return false;
872   end if;
873   for i in 1..l_number_table loop
874     if p_table(i)=p_value then
875       return true;
876     end if;
877   end loop;
878   return false;
879 Exception when others then
880   g_status_message:=sqlerrm;
881   write_to_log_file_n('Exception in value_in_table '||sqlerrm);
882   return false;
883 End;
884 
885 function value_in_table(
886     p_table numberTableType,
887     l_number_table number,
888     p_value number) return boolean is
889 Begin
890   if p_value is null or l_number_table <=0 then
891     return false;
892   end if;
893   for i in 1..l_number_table loop
894     if p_table(i)=p_value then
895       return true;
896     end if;
897   end loop;
898   return false;
899 Exception when others then
900   g_status_message:=sqlerrm;
901   write_to_log_file_n('Exception in value_in_table '||sqlerrm);
902   return false;
903 End;
904 
905 function value_in_table(
906     p_table1 varcharTableType,
907     p_table2 varcharTableType,
908     l_number_table number,
909     p_value1 varchar2,
910     p_value2 varchar2) return boolean is
911 Begin
912   if l_number_table <=0 then
913     return false;
914   end if;
915   for i in 1..l_number_table loop
916     if p_table1(i)=p_value1 and p_table2(i)=p_value2 then
917       return true;
918     end if;
919   end loop;
920   return false;
921 Exception when others then
922   g_status_message:=sqlerrm;
923   write_to_log_file_n('Exception in value_in_table '||sqlerrm);
924   return false;
925 End;
926 
927 function index_in_table(
928     p_table varcharTableType,
929     l_number_table number,
930     p_value varchar2) return number is
931 Begin
932   if p_value is null or l_number_table <=0 then
933     return 0;
934   end if;
935   for i in 1..l_number_table loop
936     if p_table(i)=p_value then
937       return i;
938     end if;
939   end loop;
940   return 0;
941 Exception when others then
942   g_status_message:=sqlerrm;
943   write_to_log_file_n('Exception in index_in_table '||sqlerrm);
944   return -1;
945 End;
946 
947 function index_in_table(
948     p_table1 varcharTableType,
949     p_table2 varcharTableType,
950     l_number_table number,
951     p_value1 varchar2,
952     p_value2 varchar2) return number is
953 Begin
954   if l_number_table <=0 then
955     return 0;
956   end if;
957   for i in 1..l_number_table loop
958     if p_table1(i)=p_value1 and p_table2(i)=p_value2 then
959       return i;
960     end if;
961   end loop;
962   return 0;
963 Exception when others then
964   g_status_message:=sqlerrm;
965   write_to_log_file_n('Exception in index_in_table '||sqlerrm);
966   return -1;
967 End;
968 
969 procedure setup_conc_program_log is
970 begin
971   g_conc_log_index:=0;
972   g_file:=null;
973   g_file_flag:=false;
974   g_status:=true;
975 End;--procedure setup_conc_program_log is
976 
977 procedure setup_conc_program_log(p_object_name varchar2) is
978 l_dir varchar2(300);
979 begin
980   g_conc_program_id:=FND_GLOBAL.Conc_request_id;--my conc id
981   l_dir:=null;
982   l_dir := fnd_profile.value('UTL_FILE_LOG');
983   if l_dir is  null  then
984     l_dir := fnd_profile.value('EDW_LOGFILE_DIR');
985     if l_dir is  null  then
986       l_dir:='/sqlcom/log';
987     end if;
988   end if;
989   g_conc_log_index:=0;
990   g_status:=true;
991   --REMOVE comments
992   FND_FILE.PUT_NAMES(p_object_name||'.log',p_object_name||'.out',l_dir);
993   g_file_flag:=true;
994   if g_version_GT_1159 is null then
995     g_version_GT_1159:=is_oracle_apps_GT_1159;
996   end if;
997 Exception when others then
998 g_status_message:=sqlerrm;
999 g_file:=null;
1000 g_file_flag:=false;
1001 End;--procedure setup_conc_program_log is
1002 
1003 procedure set_conc_program_id(p_conc_program_id number) is
1004 Begin
1005   g_conc_program_id:=p_conc_program_id;
1006 Exception when others then
1007 g_status_message:=sqlerrm;
1008 g_file:=null;
1009 g_file_flag:=false;
1010 End;
1011 PROCEDURE Write_to_conc_prog_log(
1012 		p_conc_id number,
1013 		p_conc_name varchar2,
1014 		p_object_type varchar2,
1015 		p_conc_status varchar2,
1016 		p_conc_message varchar2) IS
1017 Begin
1018   if g_file_flag then
1019     FND_FILE.PUT_LINE(FND_FILE.LOG,p_conc_status||'   '||p_conc_message);
1020   end if;
1021 Exception when others then
1022   g_status_message:=sqlerrm;
1023   null;
1024 End;
1025 
1026 procedure commit_conc_program_log is
1027 begin
1028  commit;
1029  if g_debug then
1030    write_to_log_file_n('commit');
1031  end if;
1032 Exception when others then
1033   g_status_message:=sqlerrm;
1034   null;
1035 End;--procedure commit_conc_program_log is
1036 
1037 --only writes to the conc request log file
1038 procedure write_to_conc_log_file(p_message varchar2) is
1039 l_len number;
1040 l_start number:=1;
1041 l_end number:=1;
1042 last_reached boolean:=false;
1043 Begin
1044   if p_message is null or p_message='' then
1045     return;
1046   end if;
1047   l_len:=nvl(length(p_message),0);
1048   if l_len <=0 then
1049     return;
1050   end if;
1051   while true loop
1052     l_end:=l_start+250;
1053     if l_end >= l_len then
1054      l_end:=l_len;
1055      last_reached:=true;
1056     end if;
1057     if g_file_flag then
1058       FND_FILE.PUT_LINE(FND_FILE.LOG,substr(p_message, l_start, 250));
1059     end if;
1060     l_start:=l_start+250;
1061     if last_reached then
1062       exit;
1063     end if;
1064   end loop;
1065 Exception when others then
1066   null;
1067 End;
1068 
1069 procedure write_to_log_file(p_message varchar2) is
1070 l_severity number;
1071 Begin
1072   write_to_conc_log_file(p_message);
1073   l_severity:=FND_LOG.LEVEL_STATEMENT;
1074   if l_severity>=FND_LOG.G_CURRENT_RUNTIME_LEVEL and g_version_GT_1159 then --this is for perf
1075     write_to_fnd_log(p_message,l_severity);
1076   end if;
1077 Exception when others then
1078   null;
1079 End;--procedure write_to_log_file(p_message varchar2) is
1080 
1081 procedure write_to_log_file(p_message varchar2,p_severity number) is
1082 Begin
1083   write_to_conc_log_file(p_message);
1084   if p_severity>=FND_LOG.G_CURRENT_RUNTIME_LEVEL and g_version_GT_1159 then --this is for perf
1085     write_to_fnd_log(p_message,p_severity);
1086   end if;
1087 Exception when others then
1088   null;
1089 End;--procedure write_to_log_file(p_message varchar2) is
1090 
1091 procedure write_to_log_file_n(p_message varchar2) is
1092 begin
1093   write_to_log_file('  ');
1094   write_to_log_file(p_message);
1095 Exception when others then
1096   null;
1097 end;
1098 procedure write_to_log_file_n(p_message varchar2,p_severity number) is
1099 begin
1100   write_to_log_file('  ',p_severity);
1101   write_to_log_file(p_message,p_severity);
1102 Exception when others then
1103   null;
1104 end;
1105 
1106 procedure write_to_out_file(p_message varchar2) is
1107  l_len number;
1108  l_start number:=1;
1109  l_end number:=1;
1110  last_reached boolean:=false;
1111 Begin
1112   if p_message is null or p_message='' then
1113     return;
1114   end if;
1115   l_len:=nvl(length(p_message),0);
1116   if l_len <=0 then
1117     return;
1118   end if;
1119   while true loop
1120     l_end:=l_start+250;
1121     if l_end >= l_len then
1122       --l_end:=l_len;
1123       last_reached:=true;
1124     end if;
1125     if g_file_flag then
1126       FND_FILE.PUT_LINE(FND_FILE.OUTPUT,substr(p_message, l_start, 250));
1127     end if;
1128     l_start:=l_start+250;
1129     if last_reached then
1130       exit;
1131      end if;
1132   end loop;
1133 Exception when others then
1134   null;
1135 End;--procedure write_to_out_file(p_message varchar2) is
1136 
1137 /*
1138 for 11.5.10. log messages must be written to fnd log
1139 */
1140 procedure write_to_fnd_log(
1141 p_message varchar2,
1142 p_severity number) is
1143 l_len number;
1144 l_start number:=1;
1145 l_end number:=1;
1146 last_reached boolean:=false;
1147 Begin
1148   if p_message is null or p_message='' then
1149     return;
1150   end if;
1151   if g_fnd_log_module is null then
1152     g_fnd_log_module:='BIS';--default
1153     g_fnd_log_module:=g_fnd_log_module||'.EDW';
1154   end if;
1155   l_len:=nvl(length(p_message),0);
1156   if l_len <=0 then
1157     return;
1158   end if;
1159   while true loop
1160     l_end:=l_start+3990;
1161     if l_end>=l_len then
1162       --l_end:=l_len;
1163       last_reached:=true;
1164     end if;
1165     if p_severity>=FND_LOG.G_CURRENT_RUNTIME_LEVEL then --this is for perf
1166       FND_LOG.STRING(p_severity,g_fnd_log_module,substr(p_message, l_start,3990));
1167     end if;
1168     l_start:=l_start+3990;
1169     if last_reached then
1170       exit;
1171     end if;
1172   end loop;
1173 Exception when others then
1174   write_to_conc_log_file('Error in write_to_fnd_log '||g_status_message);
1175 End;
1176 
1177 procedure print_stmt(l_stmt in varchar2) IS
1178 
1179 l_len number;
1180 l_start number:=1;
1181 l_end number:=1;
1182 last_reached boolean:=false;
1183 begin
1184 
1185 l_len:=length(l_stmt);
1186 --dbms_output.put_line(l_len);
1187 
1188 while true loop
1189 
1190 l_end:=l_start+250;
1191 
1192 if l_end >= l_len then
1193 	l_end:=l_len;
1194 	last_reached:=true;
1195 end if;
1196 
1197 --dbms_output.put_line(substr(l_stmt, l_start, 250));
1198 null;
1199 
1200 l_start:=l_start+250;
1201 
1202 if last_reached then
1203 	exit;
1204 end if;
1205 
1206 end loop;
1207 
1208 end;
1209 
1210 procedure set_debug(p_debug boolean) is
1211 begin
1212  g_debug:=p_debug;
1213 End;
1214 
1215 function get_time return varchar2 is
1216 begin
1217   return ' '||to_char(sysdate,'MM/DD/YYYY HH24:MI:SS');
1218 Exception when others then
1219   null;
1220 End;
1221 
1222 FUNCTION get_wh_language return VARCHAR2 IS
1223 l_lang VARCHAR2(240);
1224 BEGIN
1225     SELECT edw_language_code INTO l_lang
1226     FROM edw_system_parameters;
1227     RETURN l_lang;
1228 EXCEPTION WHEN OTHERS THEN
1229   return 'US';
1230 END;
1231 
1232 FUNCTION get_wh_lookup_value(p_lookup_type IN VARCHAR2,
1233                                 p_lookup_code in varchar2) return VARCHAR2 IS
1234 l_meaning VARCHAR2(100);
1235 l_lang          VARCHAR2(240);
1236 BEGIN
1237 
1238    return p_lookup_code;
1239    /*
1240      the below code was there when we thought that we need to translate before inserting into the
1241      log tables. now its decided that the conversion needs to happen in the form. so the below portion is
1242      not needed.
1243    */
1244 
1245    l_lang := get_wh_language;
1246   SELECT
1247     meaning
1248   INTO
1249     l_meaning
1250   FROM fnd_lookup_values
1251   WHERE lookup_code= p_lookup_code
1252     AND lookup_type= p_lookup_type
1253     AND language = l_lang;
1254  return l_meaning;
1255 EXCEPTION WHEN OTHERS THEN
1256    return null;
1257 END;
1258 
1259 
1260 /*
1261      NEW LOGGING collection log and collection detail log
1262 */
1263 
1264 function write_to_collection_log(
1265         p_object varchar2,
1266         p_object_id number,
1267         p_object_type varchar2,
1268         p_conc_program_id number,
1269         p_start_date date,
1270         p_end_date date,
1271         p_rows_ready number,
1272         p_rows_processed number,
1273         p_rows_collected number,/*not same as p_rows_processed when duplicate collect is yes*/
1274         p_number_insert number,
1275         p_number_update number,
1276         p_number_delete number,
1277         p_collection_message varchar2,
1278         p_status varchar2,
1279         p_load_pk number) return boolean is
1280 l_stmt varchar2(10000);
1281 l_collection_status varchar2(400);
1282 l_insert_flag boolean;
1283 TYPE CurTyp IS REF CURSOR;
1284 cv   CurTyp;
1285 l_num number:=null;
1286 Begin
1287   if g_debug then
1288     write_to_log_file_n('In write_to_collection_log');
1289   end if;
1290   l_collection_status:=p_status;
1291   l_stmt:='select 1 from edw_collection_detail_log where load_pk=:a';
1292   open cv for l_stmt using p_load_pk;
1293   fetch cv into l_num;
1294   close cv;
1295   if l_num is null then
1296     l_insert_flag:=true;
1297   else
1298     l_insert_flag:=false;
1299   end if;
1300   if l_insert_flag then
1301     l_stmt:='insert into edw_collection_detail_log(load_pk,object_name, object_id,object_type,COLLECTION_CONCURRENT_ID, '||
1302           'COLLECTION_START_DATE,COLLECTION_END_DATE, '||
1303           ' NO_ACTUALLY_COLLECTED,NO_OF_READY_RECORDS,NO_OF_SUCC_PROCESSED_RECORDS, NUMBER_INSERT,'||
1304           ' NUMBER_UPDATE,NUMBER_DELETE,COLLECTION_STATUS, COLLECTION_EXCEPTION_MESSAGE,CREATION_DATE,'||
1305           'CREATED_BY,LAST_UPDATE_DATE,LAST_UPDATE_LOGIN,LAST_UPDATE_BY) '||
1306           ' values (:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9,:a10,:a11,:a12,:a13,:a14,:a15,:a16,:a17,:a18,:a19,:a20) ';
1307     if g_debug then
1308       write_to_log_file_n('Going to execute '||l_stmt);
1309       write_to_log_file(p_load_pk||','||p_object||','||p_object_id||','||p_object_type||','||p_conc_program_id||','||p_start_date||
1310        ','||p_end_date||','||p_rows_collected||','||p_rows_ready||','||
1311        p_rows_processed||','||p_number_insert||','||p_number_update||','||p_number_delete||','||
1312        l_collection_status||','||p_collection_message||','||sysdate||','||
1313        p_conc_program_id||','||sysdate||','||p_conc_program_id||','||p_conc_program_id);
1314     end if;
1315     execute immediate l_stmt using  p_load_pk,p_object,p_object_id,p_object_type,p_conc_program_id,p_start_date,p_end_date,
1316     p_rows_collected,p_rows_ready,p_rows_processed,p_number_insert,p_number_update,p_number_delete,
1317     l_collection_status,p_collection_message,sysdate,
1318     p_conc_program_id,sysdate,p_conc_program_id,p_conc_program_id;
1319     write_to_log_file_n('Inserted '||sql%rowcount||' record into edw_collection_detail_log');
1320   else
1321     l_stmt:='update edw_collection_detail_log set COLLECTION_END_DATE=:a1, '||
1322           ' NO_ACTUALLY_COLLECTED=:a2,NO_OF_READY_RECORDS=:a3,NO_OF_SUCC_PROCESSED_RECORDS=:a4, '||
1323           'NUMBER_INSERT=:a5,NUMBER_UPDATE=:a6,NUMBER_DELETE=:a7, '||
1324           'COLLECTION_STATUS=:a8, COLLECTION_EXCEPTION_MESSAGE=:a9,LAST_UPDATE_DATE=:a10,'||
1325           'LAST_UPDATE_LOGIN=:a11,LAST_UPDATE_BY=:a12 where load_pk=:a13';
1326     if g_debug then
1327       write_to_log_file_n('Going to execute '||l_stmt);
1328       write_to_log_file(p_end_date||','||p_rows_collected||','||p_rows_ready||','||
1329        p_rows_processed||','||p_number_insert||','||p_number_update||','||p_number_delete||','||
1330        l_collection_status||','||p_collection_message||','||sysdate||','||
1331        p_conc_program_id||','||p_conc_program_id||','||p_load_pk);
1332     end if;
1333     execute immediate l_stmt using  p_end_date,p_rows_collected,p_rows_ready,p_rows_processed,
1334     p_number_insert,p_number_update,p_number_delete,
1335     l_collection_status,p_collection_message,sysdate,p_conc_program_id,p_conc_program_id,p_load_pk;
1336     write_to_log_file_n('Updated '||sql%rowcount||' record in edw_collection_detail_log');
1337   end if;
1338    return true;
1339 Exception when others then
1340   g_status_message:=sqlerrm;
1341   write_to_log_file_n(g_status_message);
1342   return false;
1343 End;
1344 
1345 function write_to_general_log
1346         (OBJECT_NAME     VARCHAR2,
1347          OBJECT_TYPE    VARCHAR2,
1348          LOG_TYPE  VARCHAR2,
1349          CONCURRENT_ID   NUMBER,
1350          START_DATE DATE,
1351          END_DATE DATE,
1352          MESSAGE   VARCHAR2,
1353          STATUS   VARCHAR2) return boolean is
1354 l_stmt varchar2(4000);
1355 Begin
1356     if g_debug then
1357       write_to_log_file_n('In UTIL.write_to_general_log');
1358       write_to_log_file(OBJECT_NAME||'     '||OBJECT_TYPE||'     '||LOG_TYPE||'     '||
1359         CONCURRENT_ID||'     '||START_DATE||'     '||END_DATE||'     '||
1360         MESSAGE||'     '||STATUS);
1361     end if;
1362     l_stmt:='insert into edw_general_log (OBJECT_NAME,OBJECT_TYPE,LOG_TYPE,CONCURRENT_ID,START_DATE, '||
1363     ' END_DATE,MESSAGE,STATUS )'||
1364              ' values (:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8) ';
1365     execute immediate l_stmt using OBJECT_NAME,OBJECT_TYPE,LOG_TYPE,CONCURRENT_ID,START_DATE,END_DATE,
1366         MESSAGE,STATUS;
1367     write_to_log_file_n('Inserted '||sql%rowcount||' records into edw_general_log');
1368     return true;
1369 Exception when others then
1370   g_status_message:=sqlerrm;
1371   write_to_log_file_n('Error in write_to_general_log '||sqlerrm||get_time);
1372   return false;
1373 End;
1374 
1375 function write_to_error_log
1376         (OBJECT_NAME     VARCHAR2,
1377          OBJECT_TYPE    VARCHAR2,
1378          ERROR_TYPE  VARCHAR2,
1379          CONCURRENT_ID   NUMBER,
1380          START_DATE DATE,
1381          END_DATE DATE,
1382          MESSAGE   VARCHAR2,
1383          STATUS   VARCHAR2,
1384          RESP_ID NUMBER) return boolean is
1385 l_stmt varchar2(4000);
1386 Begin
1387     if g_debug then
1388       write_to_log_file_n('In UTIL.write_to_error_log');
1389       write_to_log_file(OBJECT_NAME||'     '||OBJECT_TYPE||'     '||ERROR_TYPE||'     '||
1390         CONCURRENT_ID||'     '||START_DATE||'     '||END_DATE||'     '||
1391         MESSAGE||'     '||STATUS);
1392     end if;
1393     l_stmt:='insert into edw_error_log (OBJECT_NAME,OBJECT_TYPE,ERROR_TYPE,CONCURRENT_ID,START_DATE, '||
1394     ' END_DATE,MESSAGE,STATUS ,LAST_UPDATE_DATE,LAST_UPDATED_BY,CREATION_DATE,CREATED_BY, '||
1395     ' LAST_UPDATE_LOGIN,RESP_ID)'||
1396              ' values (:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9,:a10,:a11,:a12,:a13,:a14) ';
1397     execute immediate l_stmt using OBJECT_NAME,OBJECT_TYPE,ERROR_TYPE,CONCURRENT_ID,START_DATE,END_DATE,
1398         MESSAGE,STATUS,sysdate,CONCURRENT_ID,sysdate,CONCURRENT_ID,CONCURRENT_ID,RESP_ID;
1399     write_to_log_file_n('Inserted '||sql%rowcount||' records into edw_error_log');
1400     return true;
1401 Exception when others then
1402   g_status_message:=sqlerrm;
1403   write_to_log_file_n('Error in write_to_error_log '||sqlerrm||get_time);
1404   return false;
1405 End;
1406 
1407 /*
1408    the temp logging reqd so that we can have commit after each level collection
1409    the function should be called only for the lowest level
1410 */
1411 function insert_temp_log_table(
1412         p_object_name varchar2,
1413         p_object_type varchar2,
1414         p_concurrent_req_id number,
1415         p_ins_instance_name varcharTableType,
1416         p_ins_request_id_table numberTableType,
1417         p_ins_rows_ready numberTableType,
1418         p_ins_rows_processed numberTableType,
1419         p_ins_rows_collected numberTableType,
1420         p_ins_rows_dangling numberTableType,
1421         p_ins_rows_duplicate numberTableType,
1422         p_ins_rows_error numberTableType,
1423         p_number_ready number,
1424         p_number_insert number,
1425         p_number_update number,
1426         p_number_delete number,
1427         p_number_ins_req_coll number) return boolean is
1428 l_stmt varchar2(10000);
1429 Begin
1430   if g_debug then
1431     write_to_log_file_n('In EDW_OWB_COLLECTION_UTIL.insert_temp_log_table');
1432     write_to_log_file('Object '||p_object_name||', object type '||p_object_type||' req '||p_concurrent_req_id);
1433     write_to_log_file('instance   reqid  ready  processed  collected  dangling  duplicate error number_ready '||
1434     'number_insert number_update number_delete');
1435     for i in 1..p_number_ins_req_coll loop
1436       write_to_log_file(p_ins_instance_name(i)||'   '||p_ins_request_id_table(i)||'   '||p_ins_rows_ready(i)
1437         ||'   '||p_ins_rows_processed(i)||'   '||p_ins_rows_collected(i)||'   '||p_ins_rows_dangling(i)
1438         ||'   '||p_ins_rows_duplicate(i)||'    '||p_ins_rows_error(i)||' '||p_number_ready||' '||p_number_insert
1439         ||' '||p_number_update||' '||p_number_delete);
1440     end loop;
1441   end if;
1442   for i in 1..p_number_ins_req_coll loop
1443     l_stmt:='insert into edw_temp_collection_log(object_name, object_type, concurrent_id,instance, request_id, rows_ready, '||
1444           ' rows_processed, rows_collected, rows_dangling, rows_duplicate, rows_error, status,number_ready,'||
1445           'number_insert,number_update,number_delete) values '||
1446           '(:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9,:a10,:a11,:a12,:a13,:a14,:a15,:a16)';
1447     if g_debug then
1448       write_to_log_file_n('Going to execute parameter number '||i);
1449     end if;
1450     execute immediate l_stmt using p_object_name,p_object_type,p_concurrent_req_id,p_ins_instance_name(i),
1451         p_ins_request_id_table(i),p_ins_rows_ready(i),
1452         p_ins_rows_processed(i),p_ins_rows_collected(i),p_ins_rows_dangling(i),
1453         p_ins_rows_duplicate(i),p_ins_rows_error(i),'OPEN',p_number_ready,
1454         p_number_insert,p_number_update,p_number_delete;
1455   end loop;
1456   return true;
1457 Exception when others then
1458   g_status_message:=sqlerrm;
1459   write_to_log_file_n('Error in insert_temp_log_table '||sqlerrm||get_time);
1460   return false;
1461 End;
1462 
1463 /*
1464 does the table or view exist in the database?
1465 called when EDW_ALL_COLLECT is making sure that all objects exist in the database
1466 */
1467 function check_table(p_table varchar2) return boolean is
1468 Begin
1469   return check_table(p_table,null);
1470 Exception when others then
1471   g_status_message:=sqlerrm;
1472   write_to_log_file_n('Exception in  check_table '||sqlerrm||' '||get_time);
1473   return false;
1474 End;
1475 
1476 function check_table(p_table varchar2, p_owner varchar2) return boolean is
1477 l_stmt varchar2(10000);
1478 TYPE CurTyp IS REF CURSOR;
1479 cv   CurTyp;
1480 Begin
1481   if g_debug then
1482     write_to_log_file_n('In UTIL.check_table for '||p_table||' and owner '||p_owner);
1483   end if;
1484   begin
1485     if p_owner is null then
1486       l_stmt:='select 1 from '||p_table||' where rownum=1';
1487     else
1488       l_stmt:='select 1 from '||p_owner||'.'||p_table||' where rownum=1';
1489     end if;
1490     open cv for l_stmt;
1491     close cv;
1492     if g_debug then
1493       write_to_log_file('Table found');
1494     end if;
1495     return true;
1496   exception when others then
1497     g_status_message:=sqlerrm;
1498     if g_debug then
1499       write_to_log_file('Table NOT found');
1500     end if;
1501     return false;
1502   end;
1503 Exception when others then
1504   g_status_message:=sqlerrm;
1505   write_to_log_file_n('Exception in  check_table '||sqlerrm||' '||get_time);
1506   return false;
1507 End;
1508 
1509 /*
1510  given an IV, make sure that the trigger is in valid state
1511  again called from EDW_ALL_COLLECT as a part of the initial check
1512 */
1513 function check_iv_trigger(p_iv varchar2) return boolean is
1514 l_stmt varchar2(2000);
1515 TYPE CurTyp IS REF CURSOR;
1516 cv   CurTyp;
1517 l_str varchar2(400);
1518 l_trigger varchar2(400);
1519 l_table_owner varchar2(30); --bug#4905343
1520 begin
1521   if g_debug then
1522     write_to_log_file_n('In check_iv_trigger for '||p_iv);
1523   end if;
1524   l_str:=null;
1525 
1526   l_stmt:='select table_owner from user_synonyms where table_name=:a';
1527   open cv for l_stmt using p_iv;
1528      fetch cv into l_table_owner;
1529   close cv;
1530 
1531   l_stmt:='select trg.trigger_name, trg.status from all_triggers trg where trg.table_name=:a '||
1532   'and trg.owner=:b'; --rkumar:4905343
1533      open cv for l_stmt using p_iv, l_table_owner;
1534   fetch cv into l_trigger, l_str;
1535   close cv;
1536   if l_str is null then
1537     write_to_log_file_n('The instead of view Trigger does not exist for the view '||p_iv);
1538     return false;
1539   elsif l_str<> 'ENABLED' then
1540     write_to_log_file_n('The instead of view Trigger '||l_trigger||' for the view '||p_iv||' is not ENABLED');
1541     return false;
1542   else
1543    if g_debug then
1544      write_to_log_file_n('Instead of view Trigger '||l_trigger||' is valid ');
1545    end if;
1546   end if;
1547   return true;
1548 Exception when others then
1549   g_status_message:=sqlerrm;
1550   write_to_log_file_n('Exception in  check_iv_trigger '||sqlerrm||' '||get_time);
1551   return false;
1552 End;
1553 
1554 /****************************************************************************
1555     FOR DERIVED FACTS
1556 ****************************************************************************/
1557 
1558 /*
1559 is this base fact a source for the derived fact with inc refresh implemented
1560 */
1561 function is_source_for_inc_derived_fact(p_fact varchar2) return number is
1562 l_stmt varchar2(10000);
1563 TYPE CurTyp IS REF CURSOR;
1564 cv   CurTyp;
1565 l_var number:=null;
1566 l_is_name varchar2(40);
1567 l_derv_facts numberTableType;
1568 l_number_derv_facts number;
1569 Begin
1570   if g_debug then
1571     write_to_log_file_n('In is_source_for_inc_derived_fact, input param='||p_fact);
1572   end if;
1573   if g_read_cfig_options then
1574     --not checked
1575     l_stmt:='select df.fact_id from edw_facts_md_v df, edw_facts_md_v fact, '||
1576     'edw_pvt_map_properties_md_v map  '||
1577     'where fact.fact_name=:a and map.primary_source=fact.fact_id and df.fact_id=map.primary_target ';
1578     if g_debug then
1579       write_to_log_file_n(l_stmt||' '||p_fact);
1580     end if;
1581     l_number_derv_facts:=1;
1582     open cv for l_stmt using p_fact;
1583     loop
1584       fetch cv into l_derv_facts(l_number_derv_facts);
1585       exit when cv%notfound;
1586       l_number_derv_facts:=l_number_derv_facts+1;
1587     end loop;
1588     l_number_derv_facts:=l_number_derv_facts-1;
1589     close cv;
1590     if l_number_derv_facts>0 then
1591       for i in 1..l_number_derv_facts loop
1592         l_is_name:=null;
1593         if edw_option.get_warehouse_option(null,l_derv_facts(i),'INCREMENTAL',l_is_name)=false then
1594           null;
1595         end if;
1596         if l_is_name='Y' then
1597           l_var:=1;
1598           exit;
1599         end if;
1600       end loop;
1601     else
1602       l_var:=0;
1603     end if;
1604   else
1605     --not checked
1606     l_stmt:='select 1 from edw_facts_md_v df, edw_facts_md_v fact, '||
1607     'edw_pvt_map_properties_md_v map, edw_attribute_sets_md_v sis  '||
1608     'where fact.fact_name=:a and map.primary_source=fact.fact_id and df.fact_id=map.primary_target '||
1609     'and sis.entity_id=df.fact_id and sis.attribute_group_name=''EDW_INC_REFRESH''';
1610     if g_debug then
1611       write_to_log_file_n(l_stmt||' '||p_fact);
1612     end if;
1613     open cv for l_stmt using p_fact;
1614     fetch cv into l_var;
1615     close cv;
1616   end if;
1617   if l_var = 1 then
1618     return 1;
1619   else
1620     return 0;
1621   end if;
1622 Exception when others then
1623   g_status_message:=sqlerrm;
1624   write_to_log_file_n('Exception in  is_source_for_inc_derived_fact '||sqlerrm||' '||get_time);
1625   return -1;
1626 End;
1627 
1628 function get_all_derived_facts(
1629     p_object varchar2,
1630     p_derived_facts out NOCOPY varcharTableType,
1631     p_derived_fact_ids out NOCOPY numberTableType,
1632     p_map_id  out NOCOPY numberTableType,
1633     p_number_derived_facts out NOCOPY number) return boolean is
1634 l_stmt varchar2(10000);
1635 TYPE CurTyp IS REF CURSOR;
1636 cv   CurTyp;
1637 Begin
1638   if g_debug then
1639     write_to_log_file_n('In get_all_derived_facts, input param='||p_object);
1640   end if;
1641   --not checked
1642   l_stmt:='select df.fact_name, df.fact_id, map.mapping_id  from edw_facts_md_v df, edw_facts_md_v fact, '||
1643   'edw_pvt_map_properties_md_v map  '||
1644   'where fact.fact_name=:a and map.primary_source=fact.fact_id and df.fact_id=map.primary_target';
1645   if g_debug then
1646     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object);
1647   end if;
1648   p_number_derived_facts:=1;
1649   open cv for l_stmt using p_object;
1650   loop
1651     fetch cv into
1652         p_derived_facts(p_number_derived_facts),
1653         p_derived_fact_ids(p_number_derived_facts),
1654         p_map_id(p_number_derived_facts);
1655     exit when cv%notfound;
1656     p_number_derived_facts:=p_number_derived_facts+1;
1657   end loop;
1658   p_number_derived_facts:=p_number_derived_facts-1;
1659   close cv;
1660   if g_debug then
1661     write_to_log_file('Query resulted in '||p_number_derived_facts||' rows');
1662     for i in 1..p_number_derived_facts loop
1663       write_to_log_file(p_derived_facts(i)||' '||p_derived_fact_ids(i)||' '||p_map_id(i));
1664     end loop;
1665   end if;
1666 
1667   return true;
1668 Exception when others then
1669   g_status_message:=sqlerrm;
1670   write_to_log_file_n('Exception in  get_all_derived_facts '||sqlerrm||' '||get_time);
1671   return false;
1672 End;
1673 
1674 /*
1675 function to create a table when passed the table name, cols etc
1676 */
1677 function create_table(
1678     p_table_name varchar2,
1679     p_table_owner varchar2,
1680     p_table_cols varcharTableType,
1681     p_table_data_type varcharTableType,
1682     p_number_table_cols number,
1683     p_table_storage varchar2) return boolean is
1684 l_stmt varchar2(30000);
1685 Begin
1686   if g_debug then
1687     write_to_log_file_n('In Util.create_table');
1688     write_to_log_file('Input parameters:');
1689     write_to_log_file('p_table_name='||p_table_name);
1690     write_to_log_file('p_table_owner='||p_table_owner);
1691     write_to_log_file('p_table_storage='||p_table_storage);
1692     write_to_log_file('p_number_table_cols='||p_number_table_cols);
1693     write_to_log_file('Table columns');
1694     for i in 1..p_number_table_cols loop
1695       write_to_log_file(p_table_cols(i)||'  '||p_table_data_type(i));
1696     end loop;
1697   end if;
1698   if p_table_owner is null then
1699     l_stmt:='create table '||p_table_name||'(';
1700   else
1701     l_stmt:='create table '||p_table_owner||'.'||p_table_name||'(';
1702   end if;
1703   for i in 1..p_number_table_cols loop
1704     l_stmt:=l_stmt||p_table_cols(i)||' '||p_table_data_type(i)||',';
1705   end loop;
1706   l_stmt:=substr(l_stmt,1,length(l_stmt)-1);
1707   l_stmt:=l_stmt||')';
1708   if p_table_storage is not null then
1709     l_stmt:=l_stmt||' '||p_table_storage;
1710   end if;
1711   if g_debug then
1712     write_to_log_file_n('Going to execute '||l_stmt);
1713   end if;
1714   execute immediate l_stmt;
1715   return  true;
1716 Exception when others then
1717   g_status_message:=sqlerrm;
1718   write_to_log_file_n('Error in Util.create_table '||sqlerrm);
1719   return false;
1720 End;
1721 
1722 function drop_table(p_table_name varchar2) return boolean is
1723 Begin
1724   return drop_table(p_table_name,null);
1725 Exception when others then
1726   g_status_message:=sqlerrm;
1727   write_to_log_file_n('Error in drop_table '||sqlerrm);
1728   return false;
1729 End;
1730 
1731 function drop_table (
1732     p_table_name varchar2,
1733     p_owner varchar2) return boolean is
1734 l_stmt varchar2(4000);
1735 l_owner varchar2(400);
1736 Begin
1737   if p_owner is null then
1738     if instr(p_table_name,'.')<>0 then
1739       l_stmt:='drop table '||p_table_name;
1740       execute immediate l_stmt;
1741     else
1742       l_owner:=get_table_owner(p_table_name);
1743       l_stmt:='drop table '||l_owner||'.'||p_table_name;
1744       execute immediate l_stmt;
1745       l_stmt:='drop synonym '||p_table_name;
1746       begin
1747         execute immediate l_stmt;
1748       exception when others then
1749         null;
1750       end;
1751     end if;
1752   else
1753     l_stmt:='drop table '||p_owner||'.'||p_table_name;
1754     execute immediate l_stmt;
1755   end if;
1756   return  true;
1757 Exception when others then
1758   g_status_message:=sqlerrm;
1759   g_sqlcode:=sqlcode;
1760   if g_debug then
1761     write_to_log_file_n('Error in Util.drop_table '||p_table_name||' '||sqlerrm);
1762   end if;
1763   return false;
1764 End;
1765 
1766 /*
1767 given the product, returns the schema
1768 */
1769 function get_db_user(p_product varchar2) return varchar2 is
1770 l_dummy1 varchar2(2000);
1771 l_dummy2 varchar2(2000);
1772 l_schema varchar2(400);
1773 Begin
1774   if g_debug then
1775     write_to_log_file_n('In Util.get_db_user, param is '||p_product);
1776   end if;
1777   if FND_INSTALLATION.GET_APP_INFO(p_product,l_dummy1, l_dummy2,l_schema) = false then
1778     write_to_log_file_n('FND_INSTALLATION.GET_APP_INFO returned with error');
1779     return null;
1780   end if;
1781   return l_schema;
1782 Exception when others then
1783   g_status_message:=sqlerrm;
1784   write_to_log_file_n('Error in get_db_user '||sqlerrm);
1785   return null;
1786 End;
1787 
1788 function get_table_owner(p_table varchar2) return varchar2 is
1789 l_owner varchar2(400);
1790 l_stmt  varchar2(4000);
1791 TYPE CurTyp IS REF CURSOR;
1792 cv   CurTyp;
1793 Begin
1794   if g_debug then
1795     write_to_log_file_n('In get_table_owner');
1796   end if;
1797   if instr(p_table,'.')<>0 then
1798     l_owner:=substr(p_table,1,instr(p_table,'.')-1);
1799     return l_owner;
1800   end if;
1801   l_stmt:='select table_owner from user_synonyms where synonym_name=:a';
1802   open cv for l_stmt using p_table;
1803   fetch cv into l_owner;
1804   close cv;
1805 
1806   if l_owner is null then
1807     l_stmt:='select table_owner from user_synonyms where table_name=:a'; --rkumar:bug#4905343
1808     open cv for l_stmt using p_table;
1809        fetch cv into l_owner;
1810     close cv;
1811     l_stmt:='select owner from all_tables where table_name=:a '||
1812             ' and owner=:b'; --rkumar:bug#4905343
1813     open cv for l_stmt using p_table,l_owner;
1814         fetch cv into l_owner;
1815     close cv;
1816   end if;
1817   if g_debug then
1818     write_to_log_file_n('Owner for '||p_table||' is '||l_owner);
1819   end if;
1820   return l_owner;
1821 Exception when others then
1822   g_status_message:=sqlerrm;
1823   write_to_log_file_n('Error in get_table_owner '||sqlerrm);
1824   return null;
1825 End;
1826 
1827 procedure analyze_table_stats(p_table varchar2) is
1828 Begin
1829   analyze_table_stats(p_table,null,null);
1830 Exception when others then
1831   g_status_message:=sqlerrm;
1832   write_to_log_file_n('Error in analyze_table_stats '||sqlerrm);
1833 End;
1834 procedure analyze_table_stats(p_table varchar2, p_owner varchar2) is
1835 Begin
1836   analyze_table_stats(p_table,p_owner,null);
1837 Exception when others then
1838   g_status_message:=sqlerrm;
1839   write_to_log_file_n('Error in analyze_table_stats '||sqlerrm);
1840 End;
1841 procedure analyze_table_stats(p_table varchar2, p_owner varchar2,p_percentage number) is
1842 errbuf varchar2(2000);
1843 retcode varchar2(200);
1844 l_owner varchar2(400);
1845 l_percentage number;
1846 l_table varchar2(400);
1847 Begin
1848   l_percentage:=p_percentage;
1849   if l_percentage is null then
1850     l_percentage:=1; --default 1%
1851   end if;
1852   if instr(p_table,'.')<>0 then
1853     l_table:=substr(p_table,instr(p_table,'.')+1,length(p_table));
1854   else
1855     l_table:=p_table;
1856   end if;
1857   l_owner:=p_owner;
1858   if l_owner is null then
1859     l_owner:=get_table_owner(p_table);
1860     if g_debug then
1861       write_to_log_file_n('l_owner is '||l_owner);
1862     end if;
1863     if l_owner is null then
1864       write_to_log_file_n('Owner for table '||p_table||' not found');
1865       return;
1866     end if;
1867   end if;
1868   if g_debug then
1869     write_to_log_file_n('In analyze_table_stats. table is '||l_table||' and p_owner is '||
1870     l_owner||' '||p_percentage||get_time);
1871   end if;
1872   if g_parallel is null then
1873     dbms_stats.gather_table_stats(ownname=>l_owner,tabname=>l_table,estimate_percent=>l_percentage,
1874     degree=>1);
1875     --FND_STATS.GATHER_TABLE_STATS (errbuf, retcode, l_owner, l_table,l_percentage,1);
1876   else
1877     dbms_stats.gather_table_stats(ownname=>l_owner,tabname=>l_table,estimate_percent=>l_percentage,
1878     degree=>g_parallel);
1879     --FND_STATS.GATHER_TABLE_STATS (errbuf, retcode, l_owner, l_table,l_percentage,g_parallel);
1880   end if;
1881   if g_debug then
1882     write_to_log_file('Done '||get_time);
1883   end if;
1884   /*if retcode <> '0' then
1885     write_to_log_file_n('FND_STATS.GATHER_TABLE_STATS status message is '||errbuf);
1886   end if;*/
1887 Exception when others then
1888   g_status_message:=sqlerrm;
1889   write_to_log_file_n('Error in analyze_table_stats '||sqlerrm);
1890 End;
1891 
1892 function record_coll_progress(
1893     p_object_name varchar2,
1894     p_object_type varchar2,
1895     p_number_rows_processed number,
1896     p_status varchar2,
1897     p_action varchar2) return boolean is
1898 l_stmt varchar2(4000);
1899 Begin
1900   if g_debug then
1901     write_to_log_file_n('In record_coll_progress ');
1902     write_to_log_file('p_object_name='||p_object_name);
1903     write_to_log_file('p_object_type='||p_object_type);
1904     write_to_log_file('p_number_rows_processed='||p_number_rows_processed);
1905     write_to_log_file('p_status='||p_status);
1906     write_to_log_file('p_action='||p_action);
1907   end if;
1908   if p_action='INSERT' then
1909     l_stmt:='insert into edw_coll_progress_log(object_name,object_type,status,number_processed, '||
1910         'last_update_date,last_update_login,creation_date,created_by,last_update_by ) values ( '||
1911         ' :a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9) ';
1912     if g_debug then
1913       write_to_log_file_n('Going to execute '||l_stmt);
1914     end if;
1915     execute immediate l_stmt using p_object_name, p_object_type,p_status,p_number_rows_processed,
1916         sysdate,g_conc_program_id,sysdate,g_conc_program_id,g_conc_program_id;
1917   elsif p_action='UPDATE' then
1918     l_stmt:='update edw_coll_progress_log set status=:c,number_processed=:d, '||
1919         'last_update_date=:e,last_update_login=:f,last_update_by=:g where object_name=:a and '||
1920         ' object_type=:b ';
1921     if g_debug then
1922       write_to_log_file_n('Going to execute '||l_stmt);
1923     end if;
1924     execute immediate l_stmt using p_status,p_number_rows_processed,sysdate,
1925         g_conc_program_id,g_conc_program_id,p_object_name,p_object_type;
1926   elsif p_action='DELETE' then
1927     l_stmt:='delete edw_coll_progress_log where object_name=:a and object_type=:b ';
1928     if g_debug then
1929       write_to_log_file_n('Going to execute '||l_stmt);
1930     end if;
1931     execute immediate l_stmt using p_object_name,p_object_type;
1932   end if;
1933   if g_debug then
1934     write_to_log_file_n('Processed '||sql%rowcount||' records in edw_coll_progress_log');
1935   end if;
1936   return true;
1937 Exception when others then
1938   g_status_message:=sqlerrm;
1939   write_to_log_file_n('Error in record_coll_progress '||sqlerrm);
1940   return false;
1941 End;
1942 
1943 /*
1944 is_another_coll_running
1945 return values:
1946 0: function error
1947 1: still running
1948 2: not running
1949 */
1950 function is_another_coll_running(p_object_name varchar2, p_object_type varchar2) return number is
1951 TYPE CurTyp IS REF CURSOR;
1952 cv   CurTyp;
1953 l_stmt varchar2(4000);
1954 l_var number:=null;
1955 l_phase varchar2(400);
1956 l_status varchar2(400);
1957 l_dev_phase  varchar2(400);
1958 l_dev_status varchar2(400);
1959 l_message varchar2(4000);
1960 
1961 Begin
1962   if g_debug then
1963     write_to_log_file_n('In Util.is_another_coll_running');
1964     write_to_log_file('p_object_name='||p_object_name);
1965     write_to_log_file('p_object_type='||p_object_type);
1966   end if;
1967   l_stmt:='select last_update_by from edw_coll_progress_log where object_name=:a and object_type=:b';
1968   open cv for l_stmt using p_object_name, p_object_type;
1969   fetch cv into l_var;
1970   close cv;
1971   if g_debug then
1972     write_to_log_file_n('last update by found from edw_coll_progress_log is '||l_var);
1973   end if;
1974   if l_var is null then
1975     --first insert a row
1976     if record_coll_progress(p_object_name,p_object_type,0,'PROCESSING','INSERT') = false then
1977       return 0;
1978     end if;
1979     return 2;--this is where it must return most of the time
1980   end if;
1981   --if yes, check fnd status
1982   if FND_CONCURRENT.get_request_status(l_var,null,null,l_phase,l_status,
1983     l_dev_phase,l_dev_status,l_message)=false then
1984     write_to_log_file_n('FND_CONCURRENT.get_request_status returned with false');
1985     write_to_log_file(FND_MESSAGE.get);
1986     return 2;
1987     /*if record_coll_progress(p_object_name,p_object_type,0,'PROCESSING','INSERT') = false then
1988       return 0;
1989     end if;
1990     return 2;*/
1991   end if;
1992   if g_debug then
1993     write_to_log_file_n('The results found from fnd get req status are ');
1994     write_to_log_file('l_phase='||l_phase);
1995     write_to_log_file('l_status='||l_status);
1996     write_to_log_file('l_dev_phase='||l_dev_phase);
1997     write_to_log_file('l_dev_status='||l_dev_status);
1998     write_to_log_file('l_message='||l_message);
1999   end if;
2000 
2001   /*if l_phase <> 'Completed' then*/
2002   if l_dev_phase is not null and l_dev_phase <> 'COMPLETE' then
2003     return 1;
2004   else
2005     if record_coll_progress(p_object_name,p_object_type,null,null,'DELETE') = false then
2006       return 0;
2007     end if;
2008     if record_coll_progress(p_object_name,p_object_type,0,'PROCESSING','INSERT') = false then
2009       return 0;
2010     end if;
2011     commit;
2012     return 2;
2013   end if;
2014 Exception when others then
2015   g_status_message:=sqlerrm;
2016   write_to_log_file_n('Error in is_another_coll_running '||sqlerrm);
2017   return 0;
2018 End;
2019 
2020 procedure alter_session(p_param varchar2) is
2021 Begin
2022   alter_session(p_param,null);
2023 Exception when others then
2024   g_status_message:=sqlerrm;
2025   write_to_log_file_n('Error in alter_session '||sqlerrm);
2026 End;
2027 procedure alter_session(p_param varchar2,p_value varchar2) is
2028 l_stmt varchar2(2000);
2029 Begin
2030   if p_param ='PARALLEL' then
2031     l_stmt:='alter session enable parallel dml';
2032     execute immediate l_stmt;
2033     write_to_log_file_n('Session made parallel dml enabled');
2034   elsif p_param ='NO-PARALLEL' then
2035     l_stmt:='alter session disable parallel dml';
2036     execute immediate l_stmt;
2037     write_to_log_file_n('Session made parallel dml disabled');
2038   elsif p_param ='HASH_AREA_SIZE' then
2039     l_stmt:='alter session set hash_area_size='||p_value;
2040     write_to_log_file_n(l_stmt);
2041     execute immediate l_stmt;
2042   elsif p_param ='SORT_AREA_SIZE' then
2043     l_stmt:='alter session set sort_area_size='||p_value;
2044     write_to_log_file_n(l_stmt);
2045     execute immediate l_stmt;
2046   elsif p_param='TRACE' then
2047     execute immediate 'alter session set tracefile_identifier=LOADER_ENGINE';
2048     execute immediate 'alter session set sql_trace=true';
2049     execute immediate 'alter session set events=''10046 trace name context forever, level 12''';
2050     write_to_log_file_n('alter session with trace file identifier as LOADER_ENGINE, level 12');
2051   end if;
2052   commit;
2053 Exception when others then
2054   g_status_message:=sqlerrm;
2055   write_to_log_file_n('Error in alter_session '||sqlerrm);
2056 End;
2057 
2058 /*
2059 called when being run as a job
2060 */
2061 function set_session_parameters(
2062 p_hash_area_size number,
2063 p_sort_area_size number,
2064 p_trace boolean,
2065 p_parallel number
2066  )return boolean is
2067 Begin
2068   if p_hash_area_size>0 then
2069     alter_session('HASH_AREA_SIZE',p_hash_area_size);
2070   end if;
2071   if p_sort_area_size>0 then
2072     alter_session('SORT_AREA_SIZE',p_sort_area_size);
2073   end if;
2074   if p_trace then
2075     alter_session('TRACE');
2076   end if;
2077   if p_parallel is not null then
2078     alter_session('PARALLEL');
2079   end if;
2080   return true;
2081 Exception when others then
2082   g_status_message:=sqlerrm;
2083   write_to_log_file_n('Error in set_session_parameters '||g_status_message);
2084   return false;
2085 End;
2086 
2087 
2088 /*
2089 does_table_have_data :
2090 0 : Error
2091 1: no data
2092 2: data present
2093 */
2094 function does_table_have_data(p_table varchar2) return number is
2095 Begin
2096   return does_table_have_data(p_table,null);
2097 Exception when others then
2098   g_status_message:=sqlerrm;
2099   write_to_log_file_n('Error in does_table_have_data '||sqlerrm);
2100   return 0;
2101 End;
2102 function does_table_have_data(p_table varchar2, p_where varchar2) return number is
2103 l_stmt varchar2(2000);
2104 TYPE CurTyp IS REF CURSOR;
2105 cv   CurTyp;
2106 l_res number:=null;
2107 Begin
2108   if g_debug then
2109     write_to_log_file_n('In does_table_have_data , table is '||p_table||' and where clause is '||p_where);
2110   end if;
2111   if p_where is null then
2112     l_stmt:='select 1 from '||p_table||' where rownum=1';
2113   else
2114     l_stmt:='select 1 from '||p_table||' where '||p_where||' and rownum=1';
2115   end if;
2116   open cv for l_stmt;
2117   fetch cv into l_res;
2118   close cv;
2119   if l_res is null then
2120     if g_debug then
2121       write_to_log_file('No');
2122     end if;
2123     return 1;
2124   end if;
2125   if g_debug then
2126     write_to_log_file('Yes');
2127   end if;
2128   return 2;
2129 Exception when others then
2130   g_status_message:=sqlerrm;
2131   write_to_log_file_n('Error in does_table_have_data '||sqlerrm);
2132   return 0;
2133 End;
2134 
2135 function is_object_a_source(p_object_name varchar2)  return boolean is
2136 l_stmt varchar2(2000);
2137 TYPE CurTyp IS REF CURSOR;
2138 cv   CurTyp;
2139 l_res number:=null;
2140 Begin
2141   if g_debug then
2142     write_to_log_file_n('In Util.is_object_a_source, p_object_name='||p_object_name);
2143   end if;
2144   --not checked
2145   l_stmt:='select 1 from edw_pvt_map_properties_md_v map, edw_relations_md_v rel where rel.relation_name=:a and '||
2146           ' map.primary_source=rel.relation_id and rownum=1';
2147   if g_debug then
2148     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_name);
2149   end if;
2150   open cv for l_stmt using p_object_name;
2151   fetch cv into l_res;
2152   close cv;
2153   if l_res is null then
2154     return false;
2155   else
2156     return true;
2157   end if;
2158   return true;
2159 Exception when others then
2160   g_status_message:=sqlerrm;
2161   write_to_log_file_n(g_status_message);
2162   g_status:=false;
2163   return false;
2164 End;
2165 
2166 function create_dim_key_lookup(p_dim_name varchar2, p_dim_user_pk varchar2, p_dim_pk varchar2,
2167           p_lookup_table varchar2, p_parallel number,p_mode varchar2,p_op_table_space varchar2)
2168           return boolean is
2169 l_stmt varchar2(4000);
2170 Begin
2171   if g_debug then
2172     write_to_log_file_n('In create_dim_key_lookup');
2173     write_to_log_file('p_dim_name='||p_dim_name||',p_dim_user_pk='||p_dim_user_pk||',p_dim_pk='||
2174                       p_dim_pk||',p_lookup_table='||p_lookup_table||', p_parallel='||p_parallel);
2175   end if;
2176   if drop_table(p_lookup_table)=false then
2177     null;
2178   end if;
2179   l_stmt:='create table '||p_lookup_table;
2180   if p_op_table_space is not null then
2181     l_stmt:=l_stmt||' tablespace '||p_op_table_space;
2182   end if;
2183   if p_parallel is not null then
2184     l_stmt:=l_stmt||' parallel (degree '||p_parallel||') ';
2185   end if;
2186   l_stmt:=l_stmt||' as select ';
2187   if p_parallel is not null then
2188     l_stmt:=l_stmt||'/*+PARALLEL ('||p_dim_name||','||p_parallel||')*/ ';
2189   end if;
2190   if p_mode='MAX' then
2191     l_stmt:=l_stmt||p_dim_user_pk||',max('||p_dim_pk||') '||p_dim_pk||' from '||p_dim_name||
2192       ' group by '||p_dim_user_pk;
2193   else
2194     l_stmt:=l_stmt||p_dim_user_pk||','||p_dim_pk||' from '||p_dim_name;
2195   end if;
2196   if g_debug then
2197     write_to_log_file_n('Going to execute '||l_stmt);
2198   end if;
2199   execute immediate l_stmt;
2200   if g_debug then
2201     write_to_log_file_n(p_lookup_table||' created with '||sql%rowcount||' rows');
2202   end if;
2203   return true;
2204 Exception when others then
2205   g_status_message:=sqlerrm;
2206   write_to_log_file_n(g_status_message);
2207   g_status:=false;
2208   return false;
2209 End;
2210 
2211 procedure set_g_read_cfig_options(p_read_cfig_options boolean) is
2212 Begin
2213   g_read_cfig_options:=p_read_cfig_options;
2214 Exception when others then
2215   g_status_message:=sqlerrm;
2216   write_to_log_file_n(g_status_message);
2217 End;
2218 
2219 function is_slow_change_implemented(p_dim_name varchar2) return boolean is
2220 Begin
2221   return is_slow_change_implemented(p_dim_name,null);
2222 Exception when others then
2223   g_status_message:=sqlerrm;
2224   write_to_log_file_n(g_status_message);
2225   g_status:=false;
2226   return false;
2227 End;
2228 function is_slow_change_implemented(p_dim_name varchar2,p_is_name varchar2) return boolean is
2229 l_is_name varchar2(40);
2230 l_dim_id number;
2231 l_stmt varchar2(4000);
2232 TYPE CurTyp IS REF CURSOR;
2233 cv   CurTyp;
2234 l_res number:=null;
2235 Begin
2236   if g_debug then
2237     write_to_log_file_n('In is_slow_change_implemented, p_dim_name is '||p_dim_name);
2238   end if;
2239   l_is_name:=null;
2240   l_dim_id:=get_dim_id(p_dim_name);
2241   if g_read_cfig_options then
2242     if edw_option.get_warehouse_option(null,l_dim_id,'SLOWDIM',l_is_name)=false then
2243       null;
2244     end if;
2245   else
2246     l_stmt:='select 1 from edw_dimensions_md_v dim,edw_attribute_sets_md_v sis where sis.attribute_group_name=:a '||
2247     ' and sis.entity_id=dim.dim_id and dim.dim_name=:b';
2248     l_is_name:=p_is_name;
2249     if l_is_name is null then
2250       l_is_name:='DIMENSION_HISTORY';
2251     end if;
2252     if g_debug then
2253       write_to_log_file_n('Going to execute '||l_stmt||' using '||l_is_name||','||p_dim_name);
2254     end if;
2255     open cv for l_stmt using l_is_name,p_dim_name;
2256     fetch cv into l_res;
2257     close cv;
2258     if g_debug then
2259       write_to_log_file('l_res='||l_res);
2260     end if;
2261     if l_res=1 then
2262       l_is_name:='Y';
2263     else
2264       l_is_name:='N';
2265     end if;
2266   end if;
2267   if l_is_name='Y' then
2268     return true;
2269   else
2270     return false;
2271   end if;
2272 Exception when others then
2273   g_status_message:=sqlerrm;
2274   write_to_log_file_n(g_status_message);
2275   g_status:=false;
2276   return false;
2277 End;
2278 
2279 function get_dims_slow_change(p_dim_names varcharTableType,p_number_dims number,
2280           p_dim_list out NOCOPY varcharTableType ,p_number_dim_list out NOCOPY number)
2281           return boolean is
2282 Begin
2283   return get_dims_slow_change(p_dim_names,p_number_dims,p_dim_list,p_number_dim_list,null);
2284 Exception when others then
2285   g_status_message:=sqlerrm;
2286   write_to_log_file_n(g_status_message);
2287   g_status:=false;
2288   return false;
2289 End;
2290 function get_dims_slow_change(p_dim_names varcharTableType,p_number_dims number,
2291           p_dim_list out NOCOPY varcharTableType ,p_number_dim_list out NOCOPY number,
2292           p_is_name varchar2) return boolean is
2293 l_is_name varchar2(40);
2294 l_dim_id number;
2295 l_stmt varchar2(20000);
2296 l_in_clause varchar2(10000);
2297 TYPE CurTyp IS REF CURSOR;
2298 cv   CurTyp;
2299 Begin
2300   if g_debug then
2301     write_to_log_file_n('In get_dims_slow_change');
2302   end if;
2303   p_number_dim_list:=0;
2304   if p_number_dims=0 then
2305     return true;
2306   end if;
2307   if g_read_cfig_options then
2308     for i in 1..p_number_dims loop
2309       l_is_name:=null;
2310       l_dim_id:=get_dim_id(p_dim_names(i));
2311       if edw_option.get_warehouse_option(null,l_dim_id,'SLOWDIM',l_is_name)=false then
2312         null;
2313       end if;
2314       if l_is_name='Y' then
2315         p_number_dim_list:=p_number_dim_list+1;
2316         p_dim_list(p_number_dim_list):=p_dim_names(i);
2317       end if;
2318     end loop;
2319   else
2320     l_in_clause:=null;
2321     for i in 1..p_number_dims loop
2322       l_in_clause:=l_in_clause||''''||p_dim_names(i)||''',';
2323     end loop;
2324     l_in_clause:=substr(l_in_clause,1,length(l_in_clause)-1);
2325     if g_debug then
2326       write_to_log_file_n('in clause is '||l_in_clause);
2327     end if;
2328     l_is_name:=p_is_name;
2329     if l_is_name is null then
2330       l_is_name:='DIMENSION_HISTORY';
2331     end if;
2332     l_stmt:='select dim.dim_name from edw_dimensions_md_v dim,edw_attribute_sets_md_v sis where sis.attribute_group_name=:a '||
2333     ' and sis.entity_id=dim.dim_id and dim.dim_name in ('||l_in_clause||')';
2334     if g_debug then
2335       write_to_log_file_n('Going to execute '||l_stmt||' using '||l_is_name);
2336     end if;
2337     open cv for l_stmt using l_is_name;
2338     p_number_dim_list:=1;
2339     loop
2340       fetch cv into p_dim_list(p_number_dim_list);
2341       exit when cv%notfound;
2342       p_number_dim_list:=p_number_dim_list+1;
2343     end loop;
2344     close cv;
2345     p_number_dim_list:=p_number_dim_list-1;
2346   end if;
2347   if g_debug then
2348     write_to_log_file_n('The number of dime with slow change '||p_number_dim_list);
2349     for i in 1..p_number_dim_list loop
2350       write_to_log_file(p_dim_list(i));
2351     end loop;
2352   end if;
2353   return true;
2354 Exception when others then
2355   g_status_message:=sqlerrm;
2356   write_to_log_file_n(g_status_message);
2357   g_status:=false;
2358   return false;
2359 End;
2360 
2361 function execute_stmt(p_stmt varchar2) return boolean is
2362 Begin
2363   if g_debug then
2364     write_to_log_file_n('In execute_stmt. Going to execute ');
2365     write_to_log_file(p_stmt||' '||get_time);
2366   end if;
2367   execute immediate p_stmt;
2368   if g_debug then
2369     write_to_log_file_n('Executed with '||sql%rowcount||' row creation'||get_time);
2370   end if;
2371   return true;
2372 Exception when others then
2373   g_status_message:=sqlerrm;
2374   write_to_log_file_n(g_status_message);
2375   write_to_log_file('Problem stmt '||p_stmt);
2376   g_status:=false;
2377   return false;
2378 End;
2379 
2380 /*
2381 0: error
2382 1: more or less than n row
2383 2: only n row
2384 */
2385 function does_table_have_only_n_row(p_table varchar2,p_row_count number) return number is
2386 Begin
2387   return does_table_have_only_n_row(p_table,null,p_row_count);
2388 Exception when others then
2389   g_status_message:=sqlerrm;
2390   write_to_log_file_n(g_status_message);
2391   g_status:=false;
2392   return 0;
2393 End;
2394 function does_table_have_only_n_row(p_table varchar2,p_where varchar2,p_row_count number) return number is
2395 l_stmt varchar2(2000);
2396 TYPE CurTyp IS REF CURSOR;
2397 cv   CurTyp;
2398 l_res number:=null;
2399 l_count number:=0;
2400 Begin
2401   if g_debug then
2402     write_to_log_file_n('In does_table_have_only_n_row '||p_table||' '||p_where||' '||p_row_count);
2403   end if;
2404   l_stmt:='select 1 from '||p_table;
2405   if p_where is not null then
2406     l_stmt:=l_stmt||' where '||p_where;
2407   end if;
2408   if g_debug then
2409     write_to_log_file_n('Going to execute '||l_stmt);
2410   end if;
2411   open cv for l_stmt;
2412   loop
2413     fetch cv into l_res;
2414     exit when cv%notfound;
2415     l_count:=l_count+1;
2416     if l_count>p_row_count then
2417       exit;
2418     end if;
2419   end loop;
2420   close cv;
2421   if l_count=p_row_count then
2422     if g_debug then
2423       write_to_log_file_n('Yes');
2424     end if;
2425     return 2;
2426   else
2427     if g_debug then
2428       write_to_log_file_n('No');
2429     end if;
2430     return 1;
2431   end if;
2432 Exception when others then
2433   g_status_message:=sqlerrm;
2434   write_to_log_file_n(g_status_message);
2435   g_status:=false;
2436   return 0;
2437 End;
2438 
2439 function get_table_surr_pk(p_table varchar2, p_pk out NOCOPY varchar2) return boolean is
2440 l_stmt varchar2(4000);
2441 TYPE CurTyp IS REF CURSOR;
2442 cv   CurTyp;
2443 Begin
2444   if g_debug then
2445     write_to_log_file_n('In get_table_surr_pk');
2446     write_to_log_file('p_table='||p_table);
2447   end if;
2448   l_stmt:='select pk_item.column_name from edw_unique_keys_md_v pk,edw_pvt_columns_md_v pk_item,edw_relations_md_v rel, '||
2449   'edw_pvt_key_columns_md_v isu where rel.relation_name=:a and pk.entity_id=rel.relation_id '||
2450   'and isu.key_id=pk.key_id and pk_item.column_id=isu.column_id and pk_item.column_name like ''%_KEY'' '||
2451   'and pk_item.parent_object_id=rel.relation_id';
2452   open cv for l_stmt using p_table;
2453   fetch cv into p_pk;
2454   close cv;
2455   return true;
2456 Exception when others then
2457   g_status_message:=sqlerrm;
2458   write_to_log_file_n(g_status_message);
2459   g_status:=false;
2460   return false;
2461 End;
2462 
2463 function get_user_key(p_key varchar2) return varchar2 is
2464 Begin
2465   return substr(p_key,1,instr(p_key,'_KEY')-1);
2466 Exception when others then
2467   g_status_message:=sqlerrm;
2468   write_to_log_file_n(g_status_message);
2469   g_status:=false;
2470   return null;
2471 End;
2472 
2473 function is_inc_refresh_implemented(p_fact varchar2) return boolean is
2474 l_stmt varchar2(4000);
2475 TYPE CurTyp IS REF CURSOR;
2476 cv   CurTyp;
2477 l_res number:=null;
2478 l_is_name varchar2(40):=null;
2479 Begin
2480   if g_debug then
2481     write_to_log_file_n('In is_inc_refresh_implemented '||p_fact);
2482   end if;
2483   if g_read_cfig_options then
2484     if edw_option.get_warehouse_option(p_fact,null,'INCREMENTAL',l_is_name)=false then
2485       null;
2486     end if;
2487     if l_is_name='Y' then
2488       l_res:=1;
2489     else
2490       l_res:=0;
2491     end if;
2492   else
2493     l_stmt:='select 1 from edw_facts_md_v fact, edw_attribute_sets_md_v sis '||
2494      ' where fact.fact_name=:a and sis.entity_id=fact.fact_id and sis.attribute_group_name=''EDW_INC_REFRESH''';
2495     if g_debug then
2496       write_to_log_file_n('Going to execute '||l_stmt);
2497     end if;
2498     open cv for l_stmt using p_fact;
2499     fetch cv into l_res;
2500     close cv;
2501   end if;
2502   if l_res =1 then
2503     if g_debug then
2504       write_to_log_file('Yes');
2505     end if;
2506     return true;
2507   else
2508     if g_debug then
2509       write_to_log_file('No');
2510     end if;
2511     return false;
2512   end if;
2513   return false;
2514 Exception when others then
2515   g_status_message:=sqlerrm;
2516   write_to_log_file_n(g_status_message);
2517   g_status:=false;
2518   return false;
2519 End;
2520 
2521 
2522 function is_push_down_implemented(p_dim varchar2) return boolean is
2523 l_stmt varchar2(10000);
2524 TYPE CurTyp IS REF CURSOR;
2525 cv   CurTyp;
2526 l_res number:=null;
2527 Begin
2528   if g_debug then
2529     write_to_log_file_n('In is_push_down_implemented '||p_dim);
2530   end if;
2531   l_stmt:='select 1 from edw_dimensions_md_v dim, edw_attribute_sets_md_v sis '||
2532      ' where dim.dim_name=:a and sis.entity_id=dim.dim_id '||
2533      'and sis.attribute_group_name=''EDW_LEVEL_PUSH_DOWN''';
2534   if g_debug then
2535     write_to_log_file_n('Going to execute '||l_stmt);
2536   end if;
2537   open cv for l_stmt using p_dim;
2538   fetch cv into l_res;
2539   close cv;
2540   if l_res =1 then
2541     if g_debug then
2542       write_to_log_file('Yes');
2543     end if;
2544     return true;
2545   else
2546     if g_debug then
2547       write_to_log_file('No');
2548     end if;
2549     return false;
2550   end if;
2551   return false;
2552 Exception when others then
2553   g_status_message:=sqlerrm;
2554   write_to_log_file_n(g_status_message);
2555   g_status:=false;
2556   return false;
2557 End;
2558 
2559 procedure set_parallel(p_parallel number) is
2560 begin
2561   g_parallel:=p_parallel;
2562 Exception when others then
2563   g_status_message:=sqlerrm;
2564   write_to_log_file_n(g_status_message);
2565   g_status:=false;
2566 End;
2567 
2568 function is_delete_trigger_imp(p_object varchar2, p_owner varchar2) return boolean is
2569 l_stmt varchar2(10000);
2570 TYPE CurTyp IS REF CURSOR;
2571 cv   CurTyp;
2572 l_type varchar2(400):=null;
2573 Begin
2574   if g_debug then
2575     write_to_log_file_n('In is_delete_trigger_imp');
2576     write_to_log_file('p_object='||p_object);
2577     write_to_log_file('p_owner='||p_owner);
2578   end if;
2579   l_stmt:='select triggering_event from all_triggers where table_name =:a and table_owner=:b';
2580   if g_debug then
2581     write_to_log_file_n('Going to execute '||l_stmt);
2582   end if;
2583   open cv for l_stmt using p_object,p_owner;
2584   fetch cv into l_type;
2585   close cv;
2586   if l_type='DELETE' then
2587     return true;
2588   else
2589     return false;
2590   end if;
2591 Exception when others then
2592   g_status_message:=sqlerrm;
2593   write_to_log_file_n(g_status_message);
2594   g_status:=false;
2595   return true;--for safety
2596 End;
2597 
2598 function insert_into_coll_progress(
2599   OBJECT_NAME                              VARCHAR2
2600 , OBJECT_TYPE                              VARCHAR2
2601 , STATUS                                   VARCHAR2
2602 , NUMBER_PROCESSED                         NUMBER ) return boolean is
2603 l_stmt varchar2(4000);
2604 Begin
2605 --g_conc_program_id
2606   if g_debug then
2607     write_to_log_file_n('In insert_into_coll_progress');
2608   end if;
2609   l_stmt:='insert into edw_coll_progress_log(OBJECT_NAME,OBJECT_TYPE,STATUS, '||
2610           ' NUMBER_PROCESSED,LAST_UPDATE_DATE,LAST_UPDATE_LOGIN,CREATION_DATE, '||
2611           ' CREATED_BY,LAST_UPDATE_BY) values (:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9)';
2612   if g_debug then
2613     write_to_log_file_n('Going to execute '||l_stmt);
2614     write_to_log_file('Parameter '||OBJECT_NAME||','||OBJECT_TYPE||','||STATUS||','||NUMBER_PROCESSED||','||
2615     sysdate||','||g_conc_program_id||','||sysdate||','||g_conc_program_id||','||g_conc_program_id);
2616   end if;
2617   execute immediate l_stmt using OBJECT_NAME,OBJECT_TYPE,STATUS,NUMBER_PROCESSED,
2618     sysdate,g_conc_program_id,sysdate,g_conc_program_id,g_conc_program_id;
2619   return true;
2620 Exception when others then
2621   g_status_message:=sqlerrm;
2622   write_to_log_file_n(g_status_message);
2623   g_status:=false;
2624   return false;
2625 End;
2626 
2627 /* API that returns the sec sources and fk-pk constraints for the sec sources given a mapping id */
2628 function get_sec_source_info(
2629     p_map_id number,
2630     p_sec_source out NOCOPY varcharTableType,
2631     p_sec_source_id out NOCOPY numberTableType,
2632     p_sec_source_child out NOCOPY varcharTableType,
2633     p_sec_source_child_id out NOCOPY numberTableType,
2634     p_pk  out NOCOPY varcharTableType,
2635     p_fk  out NOCOPY varcharTableType,
2636     p_sec_source_usage out NOCOPY numberTableType,
2637     p_sec_source_usage_name out NOCOPY varcharTableType,
2638     p_sec_source_child_usage out NOCOPY numberTableType,
2639     p_sec_source_number out NOCOPY number) return boolean is
2640 l_stmt varchar2(10000);
2641 TYPE CurTyp IS REF CURSOR;
2642 cv   CurTyp;
2643 Begin
2644   if g_debug then
2645     write_to_log_file_n('In Util.get_sec_source_info');
2646   end if;
2647   p_sec_source_number:=0;
2648   l_stmt:=' select '||
2649   'sec_relation.relation_name,  '||
2650   'sec_relation.relation_id,  '||
2651   'sec_relation_child.relation_name, '||
2652   'sec_relation_child.relation_id,  '||
2653   'pk_item.column_name,  '||
2654   'fk_item.column_name,  '||
2655   'sec_relation_usage.source_usage_id,  '||
2656   'sec_relation_usage.source_alias,  '||
2657   'sec_relation_child_usage.source_usage_id  '||
2658   'from  '||
2659   'edw_pvt_map_sources_md_v        sec_relation_usage,  '||
2660   'edw_relations_md_v               sec_relation,  '||
2661   'edw_pvt_map_sources_md_v        sec_relation_child_usage,  '||
2662   'edw_relations_md_v               sec_relation_child,  '||
2663   'edw_pvt_map_key_usages_md_v      fk_usage,  '||
2664   'edw_pvt_key_columns_md_v         fk_isu,  '||
2665   'edw_pvt_columns_md_v                   fk_item,  '||
2666   'edw_pvt_key_columns_md_v         pk_isu,  '||
2667   'edw_pvt_columns_md_v                   pk_item  '||
2668   'where  '||
2669   ' sec_relation_usage.mapping_id=:a '||
2670   'and sec_relation.relation_id=sec_relation_usage.source_id '||
2671   'and sec_relation_child_usage.mapping_id=sec_relation_usage.mapping_id '||
2672   'and sec_relation_child.relation_id=sec_relation_child_usage.source_id '||
2673   'and fk_usage.source_usage_id=sec_relation_child_usage.source_usage_id '||
2674   'and fk_usage.Parent_table_usage_id=sec_relation_usage.source_usage_id '||
2675   'and fk_usage.mapping_id=sec_relation_usage.mapping_id '||
2676   'and fk_isu.key_id=fk_usage.foreign_key_id '||
2677   'and fk_item.column_id=fk_isu.column_id '||
2678   'and fk_item.parent_object_id=sec_relation_child.relation_id '||
2679   'and pk_isu.key_id=fk_usage.unique_key_id '||
2680   'and pk_item.column_id=pk_isu.column_id '||
2681   'and pk_item.parent_object_id=sec_relation.relation_id '||
2682   'order by sec_relation_usage.source_usage_id';
2683   if g_debug then
2684     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_map_id);
2685   end if;
2686   open cv for l_stmt using p_map_id;
2687   p_sec_source_number:=1;
2688   loop
2689     fetch cv into
2690       p_sec_source(p_sec_source_number),
2691       p_sec_source_id(p_sec_source_number),
2692       p_sec_source_child(p_sec_source_number),
2693       p_sec_source_child_id(p_sec_source_number),
2694       p_pk(p_sec_source_number),
2695       p_fk(p_sec_source_number),
2696       p_sec_source_usage(p_sec_source_number),
2697       p_sec_source_usage_name(p_sec_source_number),
2698       p_sec_source_child_usage(p_sec_source_number);
2699     exit when cv%notfound;
2700     p_sec_source_number:=p_sec_source_number+1;
2701   end loop;
2702   close cv;
2703   p_sec_source_number:=p_sec_source_number-1;
2704   if g_debug then
2705     write_to_log_file('Results');
2706     for i in 1..p_sec_source_number loop
2707       write_to_log_file(p_sec_source(i)||' '||p_sec_source_id(i)||' '||p_sec_source_child(i)||' '||
2708         p_sec_source_child_id(i)||' '||p_pk(i)||' '||p_fk(i)||' '||p_sec_source_usage(i)||' '||
2709         p_sec_source_usage_name(i)||' '||p_sec_source_child_usage(i));
2710     end loop;
2711   end if;
2712   return true;
2713 Exception when others then
2714   g_status_message:=sqlerrm;
2715   write_to_log_file_n(g_status_message);
2716   g_status:=false;
2717   return false;
2718 End;
2719 
2720 function bubble_sort(p_input numberTableType,p_input_number number, p_output out NOCOPY numberTableType)
2721  return boolean is
2722 l_temp number;
2723 Begin
2724   if p_input_number<=0 then
2725     return true;
2726   end if;
2727   if p_input_number=1 then
2728     p_output(1):=p_input(1);
2729   end if;
2730   for i in 1..p_input_number loop
2731     p_output(i):=p_input(i);
2732   end loop;
2733   for i in 1..p_input_number-1 loop
2734     for j in 1..p_input_number-i loop
2735       if p_output(j+1)<p_output(j) then
2736         l_temp:=p_output(j);
2737         p_output(j):=p_output(j+1);
2738         p_output(j+1):=l_temp;
2739       end if;
2740     end loop;
2741   end loop;
2742   if g_debug then
2743     write_to_log_file_n('Before and after sort');
2744     for i in 1..p_input_number loop
2745       write_to_log_file(p_input(i)||'   '||p_output(i));
2746     end loop;
2747   end if;
2748   return true;
2749 Exception when others then
2750   g_status_message:=sqlerrm;
2751   write_to_log_file_n(g_status_message);
2752   g_status:=false;
2753   return false;
2754 End;
2755 
2756 /*
2757 make_transforms_rec is a recursive function that will find out NOCOPY the nested transforms, construct the
2758 statement and return it.
2759 this is called for any transform. what is passed first is the derived fact id
2760 this is called only for 2 cases. first, if there is a transform on the target, the target calls it first
2761 then its called only for nested transformations
2762 */
2763 /*
2764 logic for aggregation
2765 if this level has aggregation then
2766   if not a transform or constant apply aggregation
2767   if hild level reports no aggregation then apply aggregation
2768 if this level has no aggregation then
2769   if the child level reports aggregation then
2770     apply aggregation to columns.
2771     if any transform reports no aggregation, apply aggregation
2772 */
2773 function make_transforms_rec(
2774   p_hold_func varcharTableType,
2775   p_hold_func_category varcharTableType,
2776   p_hold_item varcharTableType,
2777   p_hold_item_id numberTableType,
2778   p_hold_item_is_fk booleanTableType,
2779   p_hold_relation numberTableType,
2780   p_hold_relation_usage numberTableType,
2781   p_hold_item_usage numberTableType,
2782   p_hold_aggregatefunction varcharTableType,
2783   p_hold_is_distinct numberTableType,
2784   p_hold_relation_name varcharTableType,
2785   p_hold_func_usage numberTableType,
2786   p_hold_func_position numberTableType,
2787   p_hold_func_dvalue varcharTableType,
2788   p_sec_sources  varcharTableType,
2789   p_number_sec_sources number,
2790   p_target_id number,
2791   p_src_object varchar2,
2792   p_hold_number number,
2793   p_index  number,
2794   p_agg_flag out NOCOPY varchar2
2795 ) return varchar2 is
2796 l_func_usage number;
2797 l_index number:=null;
2798 l_transform varchar2(20000);
2799 l_ind_transform varcharTableType; --individual transforms
2800 l_ind_transform_position numberTableType;
2801 l_ind_agg  varcharTableType; --whether there is agg or not
2802 l_ind_type varcharTableType; --type, col or constant or transform etc
2803 l_ind_transform_number number;
2804 l_agg_flag boolean;
2805 l_aggregation_value varchar2(200); --used for p_agg_flag
2806 l_is_groupby_flag boolean:=false;
2807 Begin
2808   if g_debug then
2809     write_to_log_file_n('In make_transforms_rec');
2810     write_to_log_file('p_index='||p_index);
2811   end if;
2812   p_agg_flag:='NO-AGGREGATION';
2813   l_aggregation_value:=null;
2814   l_agg_flag:=false;
2815   --see if the object is the target table (ex derived fact)
2816   if p_hold_relation(p_index)=p_target_id then
2817     l_func_usage:=p_hold_func_usage(p_index);
2818     l_index:=p_index;
2819   else
2820     --search for the same occurance of the item
2821     declare
2822       l_found boolean;
2823       l_max number;
2824     begin
2825       for i in 1..p_hold_number loop
2826         if i <> p_index and p_hold_item_id(p_index)=p_hold_item_id(i) then
2827           /*
2828             we are looking for p_hold_item_id(p_index) being the output and not the input so
2829             logic is
2830             look at the position. if its 0, this is the output
2831           */
2832           if p_hold_func_position(i)=0 then
2833             l_index:=i;
2834             l_func_usage:=p_hold_func_usage(l_index);
2835             exit;
2836           end if;
2837         end if;
2838       end loop;
2839     exception when others then
2840       g_status_message:=sqlerrm;
2841       g_status:=false;
2842       write_to_log_file_n(g_status_message);
2843       write_to_log_file('Failed for '||p_hold_item_id(p_index));
2844       return null;
2845     end;
2846   end if;
2847   if g_debug then
2848     write_to_log_file_n('l_index='||l_index);
2849     write_to_log_file('l_func_usage='||l_func_usage);
2850   end if;
2851   if l_index is null then
2852     return null;
2853   end if;
2854   --build the transform
2855   if lower(p_hold_func_category(l_index)) <> 'basic' and
2856      lower(p_hold_func_category(l_index)) <> 'character' and
2857      lower(p_hold_func_category(l_index)) <> 'conversion' and
2858      lower(p_hold_func_category(l_index)) <> 'date' and
2859      lower(p_hold_func_category(l_index)) <> 'numeric' and
2860      upper(p_hold_func_category(l_index)) not like 'EDW_STAND_ALONE%' and
2861      upper(p_hold_func_category(l_index)) <> 'EDW_STANDARD_TRANSFORMS' then
2862     if p_hold_func(l_index)='GROUP_BY' then
2863       l_is_groupby_flag:=true;
2864       l_transform:='';
2865       p_agg_flag:='AGGREGATION';
2866     else
2867       l_transform:=p_hold_func_category(l_index)||'.'||p_hold_func(l_index)||'(';
2868     end if;
2869   else
2870     if p_hold_func(l_index)='GROUP_BY' then
2871       l_is_groupby_flag:=true;
2872       l_transform:='';
2873       p_agg_flag:='AGGREGATION';
2874     else
2875       if p_hold_func(l_index)='COUNT' then
2876         l_transform:=p_hold_func(l_index)||'(';
2877         p_agg_flag:='AGGREGATION';
2878       else
2879         if instr(p_hold_func(l_index),'_')=length(p_hold_func(l_index)) then
2880           l_transform:=substr(p_hold_func(l_index),1,length(p_hold_func(l_index))-1)||'(';
2881         else
2882           l_transform:=p_hold_func(l_index)||'(';
2883         end if;
2884       end if;
2885     end if;
2886   end if;
2887   --first see if there is any aggregation
2888   if l_agg_flag=false then
2889     for i in 1..p_hold_number loop
2890       if p_hold_func_usage(l_index)=p_hold_func_usage(i) then
2891         if p_hold_aggregatefunction(i) is not null then
2892           l_agg_flag:=true;
2893           p_agg_flag:='AGGREGATION';
2894           exit;
2895         end if;
2896       end if;
2897     end loop;
2898   end if;
2899   if g_debug then
2900     if l_agg_flag then
2901       write_to_log_file_n('aggregate function implemented');
2902     else
2903       write_to_log_file_n('aggregate function NOT implemented');
2904     end if;
2905   end if;
2906   l_ind_transform_number:=0;
2907   for i in 1..p_hold_number loop
2908     if i <> l_index then
2909       if p_hold_func_usage(i) = l_func_usage then
2910         --see if the object is the base fact or a sec source
2911         if p_hold_relation_name(i)=p_src_object or value_in_table(p_sec_sources,
2912            p_number_sec_sources,p_hold_relation_name(i)) then
2913           if g_debug then
2914             write_to_log_file_n('src object or sec source ,p_hold_item(i)='||p_hold_item(i)||',relation='||
2915             p_hold_relation_name(i));
2916           end if;
2917           l_ind_transform_number:=l_ind_transform_number+1;
2918           l_ind_type(l_ind_transform_number):='COLUMN';
2919           --make the transform
2920           if l_agg_flag then
2921             l_ind_agg(l_ind_transform_number):='AGGREGATION';
2922             if p_hold_aggregatefunction(i) is not null then
2923               if p_hold_is_distinct(i)=1 then
2924                 l_ind_transform(l_ind_transform_number):=p_hold_aggregatefunction(i)||'(DISTINCT('||
2925                 p_hold_item(i)||'))';
2926               else
2927                 l_ind_transform(l_ind_transform_number):=p_hold_aggregatefunction(i)||'('||p_hold_item(i)||')';
2928               end if;
2929               l_ind_transform_position(l_ind_transform_number):=p_hold_func_position(i);
2930             else
2931               l_ind_transform(l_ind_transform_number):='SUM('||p_hold_item(i)||')';
2932               l_ind_transform_position(l_ind_transform_number):=p_hold_func_position(i);
2933             end if;
2934           else --there is no aggregation
2935             l_ind_agg(l_ind_transform_number):='NO-AGGREGATION';
2936             l_ind_transform(l_ind_transform_number):=p_hold_item(i);
2937             l_ind_transform_position(l_ind_transform_number):=p_hold_func_position(i);
2938           end if;
2939         elsif p_hold_func_dvalue(i) is not null then --constant parameters
2940           if g_debug then
2941             write_to_log_file_n('constant parameter ,p_hold_item(i)='||p_hold_item(i));
2942           end if;
2943           l_ind_transform_number:=l_ind_transform_number+1;
2944           l_ind_type(l_ind_transform_number):='CONSTANT';
2945           l_ind_agg(l_ind_transform_number):='NO-AGGREGATION';
2946           l_ind_transform(l_ind_transform_number):=p_hold_func_dvalue(i);
2947           l_ind_transform_position(l_ind_transform_number):=p_hold_func_position(i);
2948         else
2949           --recursion needed here
2950           if g_debug then
2951             write_to_log_file_n('recursion needed ,p_hold_item(i)='||p_hold_item(i));
2952           end if;
2953           l_ind_transform_number:=l_ind_transform_number+1;
2954           l_ind_type(l_ind_transform_number):='TRANSFORMATION';
2955           l_ind_transform_position(l_ind_transform_number):=p_hold_func_position(i);
2956           l_ind_transform(l_ind_transform_number):=make_transforms_rec(
2957             p_hold_func,
2958             p_hold_func_category,
2959             p_hold_item,
2960             p_hold_item_id,
2961             p_hold_item_is_fk,
2962             p_hold_relation,
2963             p_hold_relation_usage,
2964             p_hold_item_usage,
2965             p_hold_aggregatefunction,
2966             p_hold_is_distinct,
2967             p_hold_relation_name,
2968             p_hold_func_usage,
2969             p_hold_func_position,
2970             p_hold_func_dvalue,
2971             p_sec_sources,
2972             p_number_sec_sources,
2973             p_target_id,
2974             p_src_object,
2975             p_hold_number,
2976             i,
2977             l_aggregation_value);
2978           if l_ind_transform(l_ind_transform_number) is null then
2979             write_to_log_file_n('l_ind_transform('||l_ind_transform_number||') is null. Error');
2980             return null;
2981           end if;
2982           l_ind_agg(l_ind_transform_number):=l_aggregation_value;
2983           if l_agg_flag then
2984             if l_aggregation_value='AGGREGATION' then
2985               null;
2986             else
2987               l_ind_transform(l_ind_transform_number):='SUM('||l_ind_transform(l_ind_transform_number)||')';
2988             end if;
2989           else--this level has no agg
2990             null;
2991           end if;
2992         end if;
2993       end if;
2994     end if;
2995   end loop;
2996   if l_agg_flag=false then --this level has no agg
2997     for i in 1..l_ind_transform_number loop
2998       if l_ind_type(i)='TRANSFORMATION' and l_ind_agg(i)='AGGREGATION' then
2999         p_agg_flag:='AGGREGATION';
3000         for j in 1..l_ind_transform_number loop
3001           if l_ind_type(i)='COLUMN' then
3002             l_ind_transform(l_ind_transform_number):='SUM('||l_ind_transform(l_ind_transform_number)||')';
3003           elsif l_ind_type(i)='TRANSFORMATION' and l_ind_agg(i)='NO-AGGREGATION' then
3004             l_ind_transform(l_ind_transform_number):='SUM('||l_ind_transform(l_ind_transform_number)||')';
3005           end if;
3006         end loop;
3007         exit;
3008       end if;
3009     end loop;
3010   end if;
3011 
3012   if g_debug then
3013     write_to_log_file_n('The func inputs and position and type and aggregation');
3014     for i in 1..l_ind_transform_number loop
3015       write_to_log_file(l_ind_transform(i)||'  '||l_ind_transform_position(i)||' '||
3016         l_ind_type(i)||' '||l_ind_agg(i));
3017     end loop;
3018   end if;
3019   declare
3020     l_pos number:=0;
3021     l_pos_flag boolean:=false;
3022     l_output_pos  numberTableType;
3023     l_sign varchar2(100); --this is the multiply sign *, divide sign / etc
3024   begin
3025     --make the transformation
3026     --first see if position is implemented
3027     for i in 1..l_ind_transform_number loop
3028       if l_ind_transform_position(i) is not null then
3029         l_pos_flag:=true;
3030         --if there are nulls, make them all 0
3031         for j in 1..l_ind_transform_number loop
3032           if l_ind_transform_position(j) is null then
3033             --l_ind_transform_position(j):=0;
3034             l_pos:=l_pos+1;
3035           end if;
3036         end loop;
3037         if l_pos>0 then
3038           for j in 1..l_ind_transform_number loop
3039             l_ind_transform_position(j):=nvl(l_ind_transform_position(j),0)+l_pos;
3040           end loop;
3041         end if;
3042         exit;
3043       end if;
3044     end loop;
3045     --l_transform
3046     if l_pos_flag then
3047       --get a sort of the positions
3048       if bubble_sort(l_ind_transform_position,
3049         l_ind_transform_number,l_output_pos)=false then
3050         g_status_message:=sqlerrm;
3051         write_to_log_file_n(g_status_message);
3052         g_status:=false;
3053         return null;
3054       end if;
3055     end if;
3056     --l_output_pos has the ascending order of ranks
3057     --see if the function is a standard function implementation
3058     if upper(p_hold_func_category(l_index))='EDW_STANDARD_TRANSFORMS' or
3059     upper(p_hold_func_category(l_index)) like 'EDW_STAND_ALONE%' then
3060       --these are MULTIPLY, DIVIDE, ADD, SUBTRACT etc
3061       if p_hold_func(l_index)='MULTIPLY' then
3062         l_sign:='*';
3063       elsif p_hold_func(l_index)='DIVIDE' then
3064         l_sign:='/';
3065       elsif p_hold_func(l_index)='ADD' or p_hold_func(l_index)='SUM' then
3066         l_sign:='+';
3067       elsif p_hold_func(l_index)='SUBTRACT' then
3068         l_sign:='-';
3069       else
3070         l_sign:=',';
3071       end if;
3072       --if l_sign is a ',' then its a function
3073       if l_sign<> ',' then
3074         l_transform:='(';
3075       end if;
3076       if l_pos_flag = false then
3077         for i in 1..l_ind_transform_number loop
3078           l_transform:=l_transform||l_ind_transform(i)||l_sign;
3079         end loop;
3080       else
3081         for i in 1..l_ind_transform_number loop
3082           for j in 1..l_ind_transform_number loop
3083             if l_ind_transform_position(j)=l_output_pos(i) then
3084               l_transform:=l_transform||l_ind_transform(j)||l_sign;
3085             end if;
3086           end loop;
3087         end loop;
3088       end if;
3089     else
3090       if l_pos_flag = false then
3091         for i in 1..l_ind_transform_number loop
3092           l_transform:=l_transform||l_ind_transform(i)||',';
3093         end loop;
3094       else
3095         for i in 1..l_ind_transform_number loop
3096           for j in 1..l_ind_transform_number loop
3097             if l_ind_transform_position(j)=l_output_pos(i) then
3098               l_transform:=l_transform||l_ind_transform(j)||',';
3099             end if;
3100           end loop;
3101         end loop;
3102       end if;
3103     end if;
3104     if l_is_groupby_flag then
3105       l_transform:=substr(l_transform,1,length(l_transform)-1);
3106     else
3107       l_transform:=substr(l_transform,1,length(l_transform)-1)||')';
3108     end if;
3109   exception when others then
3110     g_status_message:=sqlerrm;
3111     g_status:=false;
3112     write_to_log_file_n(g_status_message);
3113     return null;
3114   end;
3115   return l_transform;
3116 Exception when others then
3117   g_status_message:=sqlerrm;
3118   write_to_log_file_n(g_status_message);
3119   g_status:=false;
3120   return null;
3121 End;
3122 
3123 procedure insert_into_load_progress(p_load_fk number,p_object_name varchar2,p_object_id number,
3124   p_load_progress varchar2,p_start_date date,p_end_date date,p_category varchar2, p_operation varchar2,
3125   p_seq_id varchar2,p_flag varchar2,p_obj_id number) is
3126 l_stmt varchar2(2000);
3127 l_sysdate date;
3128 Begin
3129   l_sysdate:=sysdate;
3130   if p_flag='U' then
3131     --update
3132     l_stmt:='update edw_load_progress_log set end_date=:a1,last_update_date=:a2 '||
3133     'where load_fk=:a3 and seq_id=:a4 and obj_id=:a5';
3134     execute immediate l_stmt using p_end_date,l_sysdate,p_load_fk,p_seq_id,p_obj_id;
3135   else
3136     l_stmt:='insert into edw_load_progress_log(load_fk,seq_id,obj_id,object_name,object_id,load_progress,start_date,end_date,'||
3137     'category,operation,creation_date,last_update_date,created_by,last_update_by,last_update_login) '||
3138     'values(:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9,:a10,:a11,:a12,:a13,:a14,:a15)';
3139     execute immediate l_stmt using p_load_fk,p_seq_id,p_obj_id,p_object_name,p_object_id,p_load_progress,
3140     p_start_date,p_end_date,p_category,p_operation,l_sysdate,l_sysdate,g_conc_program_id,g_conc_program_id,
3141     g_conc_program_id;
3142   end if;
3143 Exception when others then
3144   g_status_message:=sqlerrm;
3145   write_to_log_file_n(g_status_message);
3146 End;
3147 
3148 function get_item_set_cols(p_cols out NOCOPY varcharTableType, p_number_cols out NOCOPY number,
3149 p_object varchar2,p_item_set varchar2) return boolean is
3150 l_stmt varchar2(5000);
3151 TYPE CurTyp IS REF CURSOR;
3152 cv   CurTyp;
3153 l_count number;
3154 Begin
3155   if g_debug then
3156     write_to_log_file_n('In get_item_set_cols');
3157   end if;
3158   l_stmt:='select item.column_name from edw_attribute_sets_md_v sis, edw_attribute_set_columns_md_v isu, '||
3159   'edw_pvt_columns_md_v item, '||
3160   'edw_relations_md_v rel where rel.relation_name=:a and sis.entity_id=rel.relation_id '||
3161   'and sis.attribute_group_name=:b and isu.attribute_group_id=sis.attribute_group_id and '||
3162   'item.column_id=isu.column_id and item.parent_object_id=rel.relation_id';
3163   p_number_cols:=0;
3164   l_count:=1;
3165   if g_debug then
3166     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object||','||p_item_set);
3167   end if;
3168   open cv for l_stmt using p_object,p_item_set;
3169   loop
3170     fetch cv into p_cols(l_count);
3171     exit when cv%notfound;
3172     l_count:=l_count+1;
3173   end loop;
3174   close cv;
3175   l_count:=l_count-1;
3176   p_number_cols:=l_count;
3177   return true;
3178 Exception when others then
3179   g_status_message:=sqlerrm;
3180   write_to_log_file_n('Error in get_item_set_cols '||sqlerrm);
3181   return false;
3182 End;
3183 
3184 
3185 function get_level_prefix(p_level varchar2) return varchar2 is
3186 l_prefix varchar2(40);
3187 l_stmt varchar2(1000);
3188 TYPE CurTyp IS REF CURSOR;
3189 cv   CurTyp;
3190 Begin
3191   l_stmt:='select level_prefix from edw_levels_md_v where level_name=:a';
3192   if g_debug then
3193     write_to_log_file_n(l_stmt||' '||p_level);
3194   end if;
3195   open cv for l_stmt using p_level;
3196   fetch cv into l_prefix;
3197   close cv;
3198   return l_prefix;
3199 Exception when others then
3200   g_status_message:=sqlerrm;
3201   write_to_log_file_n('Error in get_level_prefix '||sqlerrm);
3202   return null;
3203 End;
3204 
3205 --only for COPY mapping
3206 function get_obj_obj_map_details(p_src_object varchar2,p_tgt_object varchar2,p_map_name varchar2,
3207 p_src_cols out NOCOPY varcharTableType, p_tgt_cols out NOCOPY varcharTableType, p_number_cols out NOCOPY number) return boolean is
3208 l_stmt varchar2(5000);
3209 TYPE CurTyp IS REF CURSOR;
3210 cv   CurTyp;
3211 l_number_cols number;
3212 l_mapping_id number;
3213 Begin
3214   if g_debug then
3215     write_to_log_file_n('In EDW_OWB_COLLECTION_UTIL.get_obj_obj_map_details');
3216     write_to_log_file('p_src_object='||p_src_object||',p_tgt_object='||p_tgt_object||',p_map_name='||p_map_name);
3217   end if;
3218   p_number_cols:=0;
3219   l_mapping_id:=get_target_map(null,p_tgt_object);
3220   if p_map_name is null then
3221     l_stmt:='select src_it.column_name,tgt_it.column_name '||
3222     'from '||
3223     'edw_relations_md_v src_rel, '||
3224     'edw_relations_md_v tgt_rel, '||
3225     'edw_pvt_map_sources_md_v map_sources, '||
3226     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns, '||
3227     'edw_pvt_map_properties_md_v map, '||
3228     'edw_pvt_columns_md_v tgt_it, '||
3229     'edw_pvt_columns_md_v src_it '||
3230     'where '||
3231     'tgt_rel.relation_name=:a '||
3232     'and src_rel.relation_name=:b '||
3233     'and map.primary_target=tgt_rel.relation_id '||
3234     'and map_sources.mapping_id=map.mapping_id '||
3235     'and map_sources.source_id=src_rel.relation_id '||
3236     'and map_columns.source_usage_id=map_sources.source_usage_id '||
3237     'and tgt_it.column_id=map_columns.Target_column_id '||
3238     'and src_it.column_id=map_columns.source_column_id '||
3239     'and tgt_it.parent_object_id=tgt_rel.relation_id '||
3240     'and src_it.parent_object_id=src_rel.relation_id ';
3241     if g_debug then
3242       write_to_log_file_n('Going to execute '||l_stmt||' using '||l_mapping_id||','||
3243       p_tgt_object||','||p_src_object);
3244     end if;
3245     open cv for l_stmt using l_mapping_id,p_tgt_object,p_src_object;
3246   else
3247     l_stmt:='select src_it.column_name,tgt_it.column_name '||
3248     'from '||
3249     'edw_relations_md_v src_rel, '||
3250     'edw_relations_md_v tgt_rel, '||
3251     'edw_pvt_map_sources_md_v map_sources, '||
3252     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns, '||
3253     'edw_pvt_map_properties_md_v map, '||
3254     'edw_pvt_columns_md_v tgt_it, '||
3255     'edw_pvt_columns_md_v src_it, '||
3256     'edw_pvt_mappings_md_v model '||
3257     'where '||
3258     'tgt_rel.relation_name=:a '||
3259     'and src_rel.relation_name=:b '||
3260     'and model.mapping_name=:c '||
3261     'and map.primary_target=tgt_rel.relation_id '||
3262     'and model.mapping_id=map.mapping_id '||
3263     'and map_sources.mapping_id=map.mapping_id '||
3264     'and map_sources.source_id=src_rel.relation_id '||
3265     'and map_columns.source_usage_id=map_sources.source_usage_id '||
3266     'and tgt_it.column_id=map_columns.Target_column_id '||
3267     'and src_it.column_id=map_columns.source_column_id '||
3268     'and tgt_it.parent_object_id=tgt_rel.relation_id '||
3269     'and src_it.parent_object_id=src_rel.relation_id ';
3270     if g_debug then
3271       write_to_log_file_n('Going to execute '||l_stmt||' using '||l_mapping_id||','||
3272       p_tgt_object||','||p_src_object||','||
3273       p_map_name);
3274     end if;
3275     open cv for l_stmt using l_mapping_id,p_tgt_object,p_src_object,p_map_name;
3276   end if;
3277   l_number_cols:=1;
3278   loop
3279     fetch cv into p_src_cols(l_number_cols),p_tgt_cols(l_number_cols);
3280     exit when cv%notfound;
3281     l_number_cols:=l_number_cols+1;
3282   end loop;
3283   close cv;
3284   l_number_cols:=l_number_cols-1;
3285   p_number_cols:=l_number_cols;
3286   return true;
3287 Exception when others then
3288   g_status_message:=sqlerrm;
3289   write_to_log_file_n('Error in get_obj_obj_map_details '||sqlerrm);
3290   return false;
3291 End;
3292 
3293 
3294 /************************************************************
3295      for VBH
3296 ************************************************************/
3297 function get_vbh_mapping(
3298 		p_src_name varchar2,
3299         p_tgt_name varchar2,
3300 		p_map varchar2,
3301 		p_src_table out NOCOPY varcharTableType,
3302 		p_src_col out NOCOPY varcharTableType,
3303 		p_tgt_table out NOCOPY varcharTableType,
3304 		p_tgt_col out NOCOPY varcharTableType,
3305 		p_number_maps out NOCOPY number,
3306         p_debug boolean) return boolean is
3307 TYPE CurTyp IS REF CURSOR;
3308 cv   CurTyp;
3309 l_stmt varchar2(30000);
3310 l_map_id number;
3311 l_tgt_id number;
3312 l_src_id number;
3313 Begin
3314   p_number_maps:=0;
3315   l_stmt:='select map.mapping_id,map.primary_target, map.primary_source '||
3316   'from  edw_pvt_map_properties_md_v map, edw_relations_md_v src, edw_relations_md_v tgt, '||
3317   'edw_pvt_mappings_md_v model where map.primary_target=tgt.relation_id and '||
3318   'map.primary_source=src.relation_id and tgt.relation_name=:a and src.relation_name=:b and '||
3319   'model.mapping_name=:c and model.mapping_id=map.mapping_id';
3320   if p_debug then
3321     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_tgt_name||'  '||
3322 	p_src_name||'   '||p_map);
3323   end if;
3324   open cv for l_stmt using p_tgt_name, p_src_name, p_map;
3325   fetch cv into l_map_id, l_tgt_id,l_src_id;
3326   close cv;
3327   l_stmt:='select '||
3328 	'src.relation_name, '||
3329 	'src_col.column_name, '||
3330 	'tgt.relation_name, '||
3331 	'tgt_col.column_name '||
3332 	'from '||
3333     'edw_relations_md_v src, '||
3334     'edw_relations_md_v tgt, '||
3335     'edw_pvt_columns_md_v src_col, '||
3336     'edw_pvt_columns_md_v tgt_col, '||
3337     'edw_pvt_map_properties_md_v map, '||
3338     'edw_pvt_map_sources_md_v map_source, '||
3339     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns '||
3340     'where '||
3341     'map.mapping_id=:a '||
3342     'and map_source.mapping_id=map.mapping_id '||
3343     'and map_source.source_id=src.relation_id '||
3344     'and map_columns.source_usage_id=map_source.source_usage_id '||
3345     'and map.primary_target=tgt.relation_id '||
3346     'and map.primary_source=src.relation_id '||
3347     'and src_col.column_id=map_columns.Source_column_id '||
3348     'and tgt_col.column_id=map_columns.target_column_id '||
3349     'and tgt_col.parent_object_id=tgt.relation_id '||
3350     'and src_col.parent_object_id=src.relation_id ';
3351   open cv for l_stmt using l_map_id,l_map_id;
3352   p_number_maps:=1;
3353   loop
3354    fetch cv into
3355      p_src_table(p_number_maps),p_src_col(p_number_maps),
3356      p_tgt_table(p_number_maps),p_tgt_col(p_number_maps);
3357      exit when cv%notfound;
3358      p_number_maps:=p_number_maps+1;
3359   end loop;
3360   close cv;
3361   p_number_maps:=p_number_maps-1;
3362   write_to_log_file('The number of detailed mappings found for dim '||p_tgt_name||'= '||p_number_maps);
3363   if p_debug then
3364     write_to_log_file('  ');
3365     write_to_log_file('Src Table    Src column    Tgt Table    Tgt Column');
3366     write_to_log_file('  ');
3367     for i in 1..p_number_maps loop
3368       write_to_log_file(p_src_table(i)||'   '||p_src_col(i)||'    '||
3369         p_tgt_table(i)||'   '||p_tgt_col(i));
3370     end loop;
3371   end if;
3372   return true;
3373 Exception when others then
3374   g_status_message:=sqlerrm;
3375   write_to_log_file_n('Error in get_vbh_mapping '||sqlerrm);
3376   return false;
3377 End;
3378 
3379 /**************************************************************/
3380 
3381 
3382 /******************************************************************
3383         FOR VIKAS
3384 *******************************************************************/
3385 function is_slowly_changing_dimension(p_dim_name varchar2) return varchar2 is
3386 TYPE CurTyp IS REF CURSOR;
3387 cv   CurTyp;
3388 l_stmt varchar2(5000);
3389 l_sis_name varchar2(200);
3390 l_var number;
3391 begin
3392   if g_debug then
3393     write_to_log_file_n('In is_slowly_changing_dimension , dim is '||p_dim_name);
3394   end if;
3395   l_sis_name:='DIMENSION_HISTORY';
3396   l_var:=null;
3397   l_stmt:='select 1 from edw_dimensions_md_v dim, edw_attribute_sets_md_v sis '||
3398         ' where dim.dim_name=:a and sis.entity_id=dim.dim_id and sis.attribute_group_name=:b ';
3399   open cv for l_stmt using p_dim_name, l_sis_name;
3400   fetch cv into l_var;
3401   close cv;
3402   if l_var is null then
3403     return 'FALSE';
3404   else
3405     return 'TRUE';
3406   end if;
3407 Exception when others then
3408   g_status_message:=sqlerrm;
3409   write_to_log_file_n('Error in is_slowly_changing_dimension for '||p_dim_name||'  '||sqlerrm);
3410   return 'FALSE';
3411 End;
3412 
3413 /**********************************************************************/
3414 function get_table_index_col(
3415 p_table varchar2,
3416 p_owner varchar2,
3417 p_index out NOCOPY varcharTableType,
3418 p_ind_col out NOCOPY varcharTableType,
3419 p_ind_col_pos out NOCOPY numberTableType,
3420 p_number_index out NOCOPY number) return boolean is
3421 l_stmt varchar2(2000);
3422 TYPE CurTyp IS REF CURSOR;
3423 cv   CurTyp;
3424 Begin
3425   p_number_index:=0;
3426   l_stmt:='select index_name, column_name,column_position from all_ind_columns where table_name=:a and TABLE_OWNER=:b';
3427   if g_debug then
3428     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table||','||p_owner);
3429   end if;
3430   p_number_index:=1;
3431   open cv for l_stmt using p_table,p_owner;
3432   loop
3433     fetch cv into p_index(p_number_index),p_ind_col(p_number_index),p_ind_col_pos(p_number_index);
3434     exit when cv%notfound;
3435     p_number_index:=p_number_index+1;
3436   end loop;
3437   close cv;
3438   p_number_index:=p_number_index-1;
3439   return true;
3440 Exception when others then
3441   g_status_message:=sqlerrm;
3442   write_to_log_file_n(g_status_message);
3443   return false;
3444 End;
3445 
3446 /*
3447 is there an index with just this column?
3448 */
3449 function check_index_on_column(
3450 p_table varchar2,
3451 p_owner varchar2,
3452 p_column varchar2
3453 )return boolean is
3454 l_index varcharTableType;
3455 l_ind_col varcharTableType;
3456 l_ind_col_pos numberTableType;
3457 l_number_index number;
3458 l_found boolean;
3459 Begin
3460   if g_debug then
3461     write_to_log_file_n('In check_index_on_column '||p_table||' '||p_owner||' '||p_column);
3462   end if;
3463   if get_table_index_col(
3464     p_table,
3465     p_owner,
3466     l_index,
3467     l_ind_col,
3468     l_ind_col_pos,
3469     l_number_index)=false then
3470     return false;
3471   end if;
3472   l_found:=false;
3473   for i in 1..l_number_index loop
3474     if l_ind_col(i)=p_column then
3475       l_found:=true;
3476       for j in 1..l_number_index loop
3477         if i<>j and l_index(i)=l_index(j) then
3478           l_found:=false;
3479           exit;
3480         end if;
3481       end loop;
3482       if l_found then
3483         if g_debug then
3484           write_to_log_file_n('Index found '||l_index(i)||' with only column '||p_column);
3485         end if;
3486         exit;
3487       end if;
3488     end if;
3489   end loop;
3490   return l_found;
3491 Exception when others then
3492   g_status_message:=sqlerrm;
3493   write_to_log_file_n(g_status_message);
3494   return false;
3495 End;
3496 
3497 
3498 function get_object_id(p_object varchar2) return number is
3499 l_stmt varchar2(2000);
3500 TYPE CurTyp IS REF CURSOR;
3501 cv   CurTyp;
3502 l_obj_id number;
3503 Begin
3504   l_stmt:='select relation_id from edw_relations_md_v where relation_name=:a';
3505   open cv for l_stmt using p_object;
3506   fetch cv into l_obj_id;
3507   close cv;
3508   return l_obj_id;
3509 Exception when others then
3510   g_status_message:=sqlerrm;
3511   write_to_log_file_n(g_status_message);
3512   return -1;
3513 End;
3514 
3515 function get_dim_id(p_object varchar2) return number is
3516 l_stmt varchar2(2000);
3517 TYPE CurTyp IS REF CURSOR;
3518 cv   CurTyp;
3519 l_obj_id number;
3520 Begin
3521   l_stmt:='select dim_id from edw_dimensions_md_v where dim_name=:a';
3522   open cv for l_stmt using p_object;
3523   fetch cv into l_obj_id;
3524   close cv;
3525   return l_obj_id;
3526 Exception when others then
3527   g_status_message:=sqlerrm;
3528   write_to_log_file_n(g_status_message);
3529   return -1;
3530 End;
3531 
3532 function last_analyzed_date(p_table varchar2) return date is
3533 Begin
3534   return last_analyzed_date(p_table,null);
3535 Exception when others then
3536   g_status_message:=sqlerrm;
3537   write_to_log_file_n(g_status_message);
3538   return null;
3539 End;
3540 
3541 function last_analyzed_date(p_table varchar2,p_owner varchar2) return date is
3542 l_stmt varchar2(2000);
3543 l_table_owner varchar2(30);
3544 TYPE CurTyp IS REF CURSOR;
3545 cv   CurTyp;
3546 l_var date;
3547 Begin
3548   if p_owner is not null then
3549     l_stmt:='select last_analyzed from all_tables where table_name=:a and owner=:b';
3550     open cv for l_stmt using p_table,p_owner;
3551   else
3552       l_stmt:='select table_owner from user_synonyms where table_name=:a';
3553       open cv for l_stmt using p_table;
3554       	     fetch cv into l_table_owner;
3555       close cv;
3556      l_stmt:='select last_analyzed from all_tables where table_name=:a and owner=:b';--rkumar:bug#4905343
3557     open cv for l_stmt using p_table, l_table_owner;
3558   end if;
3559   fetch cv into l_var;
3560   close cv;  --bug#4905343
3561   return l_var;
3562 Exception when others then
3563   g_status_message:=sqlerrm;
3564   write_to_log_file_n(g_status_message);
3565   return null;
3566 End;
3567 
3568 function get_table_count(p_table varchar2) return number is
3569 Begin
3570   return get_table_count(p_table,null);
3571 Exception when others then
3572   g_status_message:=sqlerrm;
3573   write_to_log_file_n(g_status_message);
3574   return -1;
3575 End;
3576 function get_table_count(p_table varchar2,p_where varchar2) return number is
3577 l_stmt varchar2(2000);
3578 TYPE CurTyp IS REF CURSOR;
3579 cv   CurTyp;
3580 l_count number;
3581 Begin
3582   l_stmt:='select ';
3583   if g_parallel is not null then
3584     l_stmt:=l_stmt||' /*+PARALLEL('||p_table||','||g_parallel||')*/ ';
3585   end if;
3586   if p_where is null then
3587     l_stmt:=l_stmt||' count(*) from '||p_table;
3588   else
3589     l_stmt:=l_stmt||' count(*) from '||p_table||' where '||p_where;
3590   end if;
3591   if g_debug then
3592     write_to_log_file_n('Going to execute '||l_stmt||get_time);
3593   end if;
3594   open cv for l_stmt;
3595   fetch cv into l_count;
3596   close cv;
3597   return l_count;
3598 Exception when others then
3599   g_status_message:=sqlerrm;
3600   write_to_log_file_n(g_status_message);
3601   return -1;
3602 End;
3603 
3604 function get_table_avg_row_len(p_table varchar2,p_owner varchar2,p_avg_row_len out NOCOPY number)
3605 return boolean is
3606 l_table_space varchar2(400);
3607 l_initial_extent number;
3608 l_next_extent number;
3609 l_pct_free number;
3610 l_pct_used number;
3611 l_pct_increase number;
3612 l_max_extents number;
3613 Begin
3614   return get_table_storage(p_table,p_owner,l_table_space,l_initial_extent,l_next_extent,l_pct_free,l_pct_used,
3615   l_pct_increase,l_max_extents,p_avg_row_len);
3616 Exception when others then
3617   write_to_log_file_n(sqlerrm);
3618   return false;
3619 End;
3620 
3621 
3622 function get_table_storage(p_table varchar2,p_owner varchar2,p_table_space out NOCOPY varchar2,
3623 p_initial_extent out NOCOPY number,p_next_extent out NOCOPY number,p_pct_free out NOCOPY number,p_pct_used out NOCOPY number,
3624 p_pct_increase out NOCOPY number, p_max_extents out NOCOPY number,p_avg_row_len out NOCOPY number
3625 ) return boolean is
3626 l_stmt varchar2(2000);
3627 TYPE CurTyp IS REF CURSOR;
3628 cv   CurTyp;
3629 Begin
3630   l_stmt:='select tablespace_name,initial_extent,next_extent,pct_free,pct_used,pct_increase,max_extents,'||
3631   'avg_row_len from '||
3632   'all_tables where table_name=:a and owner=:b';
3633   if g_debug then
3634     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table||','||p_owner);
3635   end if;
3636   open cv for l_stmt using p_table,p_owner;
3637   fetch cv into p_table_space,p_initial_extent,p_next_extent,p_pct_free,p_pct_used,p_pct_increase,p_max_extents,
3638   p_avg_row_len;
3639   close cv;
3640   return true;
3641 Exception when others then
3642   g_status_message:=sqlerrm;
3643   write_to_log_file_n(g_status_message);
3644   return false;
3645 End;
3646 
3647 function get_table_next_extent(p_table varchar2,p_owner varchar2,p_next_extent out NOCOPY number) return boolean is
3648 l_stmt varchar2(2000);
3649 TYPE CurTyp IS REF CURSOR;
3650 cv   CurTyp;
3651 Begin
3652   l_stmt:='select next_extent from all_tables where table_name=:a and owner=:b';
3653   if g_debug then
3654     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table||','||p_owner);
3655   end if;
3656   open cv for l_stmt using p_table,p_owner;
3657   fetch cv into p_next_extent;
3658   close cv;
3659   return true;
3660 Exception when others then
3661   g_status_message:=sqlerrm;
3662   write_to_log_file_n(g_status_message);
3663   return false;
3664 End;
3665 
3666 function get_user_pk(p_table varchar2) return varchar2 is
3667 l_stmt varchar2(2000);
3668 TYPE CurTyp IS REF CURSOR;
3669 cv   CurTyp;
3670 l_pk varchar2(400);
3671 Begin
3672   l_stmt:='select '||
3673   'pk_item.column_name  '||
3674   'from '||
3675   'edw_relations_md_v rel ,  '||
3676   'edw_unique_keys_md_v pk,  '||
3677   'edw_pvt_key_columns_md_v isu,   '||
3678   'edw_pvt_columns_md_v pk_item '||
3679   'where  '||
3680   'rel.relation_name=:a '||
3681   'and pk.entity_id=rel.relation_id '||
3682   'and pk.primarykey <> 1  '||
3683   'and isu.key_id=pk.key_id '||
3684   'and pk_item.column_id=isu.column_id '||
3685   'and pk_item.parent_object_id=rel.relation_id '||
3686   'and pk_item.data_type<>''NUMBER''';
3687   if g_debug then
3688     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table);
3689   end if;
3690   open cv for l_stmt using p_table;
3691   fetch cv into l_pk;
3692   close cv;
3693   return l_pk;
3694 Exception when others then
3695   g_status_message:=sqlerrm;
3696   write_to_log_file_n(g_status_message);
3697   return null;
3698 End;
3699 
3700 function is_column_in_table(p_table varchar2,p_column varchar2) return boolean is
3701 Begin
3702   return is_column_in_table(p_table,p_column,null);
3703 Exception when others then
3704   g_status_message:=sqlerrm;
3705   write_to_log_file_n(g_status_message);
3706   return false;
3707 End;
3708 function is_column_in_table(p_table varchar2,p_column varchar2,p_owner varchar2) return boolean is
3709 l_stmt varchar2(2000);
3710 TYPE CurTyp IS REF CURSOR;
3711 cv   CurTyp;
3712 l_res number;
3713 l_table varchar2(200);
3714 l_owner varchar2(200);
3715 Begin
3716   l_table:=upper(p_table);
3717   l_owner:=upper(p_owner);
3718   if instr(p_table,'.')>0 then
3719     l_table:=substr(p_table,instr(p_table,'.')+1,length(p_table));
3720     if l_owner is null then
3721       l_owner:=substr(p_table,1,instr(p_table,'.')-1);
3722     end if;
3723   end if;
3724   if l_owner is null then
3725     l_owner:=get_table_owner(l_table);
3726   end if;
3727   if l_owner is not null then
3728     l_stmt:='select 1 from all_tab_columns where table_name=:a and column_name=:b and owner=:c';
3729     if g_debug then
3730       write_to_log_file_n(l_stmt||' '||l_table||' '||upper(p_column)||' '||l_owner);
3731     end if;
3732     open cv for l_stmt using l_table,upper(p_column),l_owner;
3733   else
3734     l_stmt:='select 1 from all_tab_columns,user_synonyms syn where all_tab_columns.table_name=:a '||
3735     'and column_name=:b and syn.table_name=all_tab_columns.table_name and syn.table_owner=all_tab_columns.owner';
3736     if g_debug then
3737       write_to_log_file_n(l_stmt||' '||l_table||' '||upper(p_column));
3738     end if;
3739     open cv for l_stmt using l_table,upper(p_column);
3740   end if;
3741   fetch cv into l_res;
3742   if l_res is null then
3743     return false;
3744   else
3745     return true;
3746   end if;
3747 Exception when others then
3748   g_status_message:=sqlerrm;
3749   write_to_log_file_n(g_status_message);
3750   return false;
3751 End;
3752 
3753 function get_table_space(p_owner varchar2) return varchar2 is
3754 l_stmt varchar2(2000);
3755 TYPE CurTyp IS REF CURSOR;
3756 cv   CurTyp;
3757 l_ts varchar2(200);
3758 Begin
3759   l_stmt:='select default_tablespace from dba_users where username=:a';
3760   if g_debug then
3761     write_to_log_file_n(l_stmt||' using '||p_owner);
3762   end if;
3763   open cv for l_stmt using p_owner;
3764   fetch cv into l_ts;
3765   close cv;
3766   return l_ts;
3767 Exception when others then
3768   g_status_message:=sqlerrm;
3769   write_to_log_file_n(g_status_message);
3770   return null;
3771 End;
3772 
3773 function index_present(p_table varchar2,p_owner varchar2,p_key varchar2,p_type varchar2) return boolean is
3774 l_stmt varchar2(4000);
3775 l_owner varchar2(400);
3776 TYPE CurTyp IS REF CURSOR;
3777 cv   CurTyp;
3778 l_var number;
3779 Begin
3780   if g_debug then
3781     write_to_log_file_n('In index_present p_table='||p_table||',p_owner='||p_owner||',p_key='||
3782     p_key||',p_type='||p_type);
3783   end if;
3784   if p_owner is null then
3785     l_owner:=get_table_owner(p_table);
3786   else
3787     l_owner:=p_owner;
3788   end if;
3789   if p_type='UNIQUE' then
3790     l_stmt:='select 1 '||
3791     'from all_indexes a,all_ind_columns b '||
3792     'where '||
3793     'a.index_name=b.index_name '||
3794     'and a.owner=b.index_owner '||
3795     'and a.uniqueness=''UNIQUE'' '||
3796     'and a.table_name=:a '||
3797     'and a.table_owner=:b '||
3798     'and a.index_name in  '||
3799     '(select c.index_name from all_ind_columns c '||
3800     'where a.index_name=c.index_name '||
3801     'and a.owner=c.index_owner '||
3802     'and c.column_name=:c) '||
3803     'having count(*)=1 '||
3804     'group by b.index_name ';
3805   else
3806     l_stmt:='select 1 from all_ind_columns where table_name=:a and table_owner=:b and column_name=:c';
3807   end if;
3808   if g_debug then
3809     write_to_log_file_n(l_stmt||' using '||p_table||' '||l_owner||' '||p_key);
3810   end if;
3811   open cv for l_stmt using p_table,l_owner,p_key;
3812   fetch cv into l_var;
3813   close cv;
3814   if l_var is not null then
3815     return true;
3816   else
3817     return false;
3818   end if;
3819 Exception when others then
3820   g_status_message:=sqlerrm;
3821   write_to_log_file_n(g_status_message);
3822   return false;
3823 End;
3824 
3825 function get_mapping_details(
3826  p_mapping_id number
3827 ,p_hold_func out NOCOPY varcharTableType
3828 ,p_hold_func_category out NOCOPY varcharTableType
3829 ,p_hold_item out NOCOPY varcharTableType
3830 ,p_hold_item_id out NOCOPY numberTableType
3831 ,p_hold_item_usage out NOCOPY numberTableType
3832 ,p_hold_aggregatefunction out NOCOPY varcharTableType
3833 ,p_hold_is_distinct out NOCOPY numberTableType
3834 ,p_hold_relation out NOCOPY numberTableType
3835 ,p_hold_relation_name out NOCOPY varcharTableType
3836 ,p_hold_relation_usage out NOCOPY numberTableType
3837 ,p_hold_relation_type out NOCOPY varcharTableType
3838 ,p_hold_func_usage out NOCOPY numberTableType
3839 ,p_hold_func_position out NOCOPY numberTableType
3840 ,p_hold_func_dvalue out NOCOPY varcharTableType
3841 ,p_hold_number out NOCOPY number
3842 ,p_metedata_version varchar2
3843 ) return boolean is
3844 l_stmt varchar2(10000);
3845 TYPE CurTyp IS REF CURSOR;
3846 cv   CurTyp;
3847 l_metedata_version varchar2(80);
3848 Begin
3849   l_metedata_version:=p_metedata_version;
3850   if l_metedata_version is null then
3851     l_metedata_version:=find_metadata_version;
3852   end if;
3853   if l_metedata_version='OWB 2.1.1' or l_metedata_version='OWB 3i' then
3854     l_stmt:='select '||
3855     'func_name,'||
3856     'category_name,'||
3857     'column_name,'||
3858     'column_id,'||
3859     'column_usage_id ,'||
3860     'aggregation,'||
3861     'is_distinct,'||
3862     'relation_id,'||
3863     'relation_name,'||
3864     'relation_usage_id,'||
3865     'relation_type,'||
3866     'func_usage_id,'||
3867     'attribute_position,'||
3868     'func_default_value '||
3869     'from   '||
3870     'edw_pvt_map_func_md_v map '||
3871     'where '||
3872     'map.mapping_id=:a ';
3873   else
3874     write_to_log_file_n('could not find right metadata type');
3875     return false;
3876   end if;
3877   if g_debug then
3878     write_to_log_file_n('Going to execute '||l_stmt);
3879   end if;
3880   p_hold_number:=1;
3881   open cv for l_stmt using p_mapping_id;
3882   loop
3883     fetch cv into
3884         p_hold_func(p_hold_number),
3885         p_hold_func_category(p_hold_number),
3886         p_hold_item(p_hold_number),
3887         p_hold_item_id(p_hold_number),
3888         p_hold_item_usage(p_hold_number),
3889         p_hold_aggregatefunction(p_hold_number),
3890         p_hold_is_distinct(p_hold_number),
3891         p_hold_relation(p_hold_number),
3892         p_hold_relation_name(p_hold_number),
3893         p_hold_relation_usage(p_hold_number),
3894         p_hold_relation_type(p_hold_number),
3895         p_hold_func_usage(p_hold_number),
3896         p_hold_func_position(p_hold_number),
3897         p_hold_func_dvalue(p_hold_number);
3898     exit when cv%notfound;
3899     p_hold_number:=p_hold_number+1;
3900   end loop;
3901   p_hold_number:=p_hold_number-1;
3902   close cv;
3903   --for owb 9i, we have perf issues with edw_pvt_map_func_md_v. edw_pvt_map_func_md_v only contains
3904   --functions that are not copy.
3905   if l_metedata_version='OWB 3i' then
3906     l_stmt:='select '||
3907     '''COPY'' func_name, '||
3908     '''Basic'' category_name, '||
3909     'col.column_name column_name, '||
3910     'col.column_id, '||
3911     'map_col.target_column_usage_id column_usage_id , '||
3912     'null, '||
3913     'null, '||
3914     'rel.relation_id, '||
3915     'rel.relation_name, '||
3916     'map_col.target_usage_id relation_usage_id, '||
3917     'rel.relation_type, '||
3918     'map_col.func_usage_id, '||
3919     '1 attribute_position, '||
3920     'null '||
3921     'from '||
3922     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_col, '||
3923     'edw_relations_md rel, '||
3924     'edw_pvt_columns_md col, '||
3925     'edw_pvt_map_targets_md tgt '||
3926     'where '||
3927     'rel.relation_id=tgt.target_id '||
3928     'and tgt.mapping_id=map_col.mapping_id '||
3929     'and tgt.target_usage_id=map_col.target_usage_id '||
3930     'and col.column_id=map_col.target_column_id ';
3931     if g_debug then
3932       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_mapping_id||get_time);
3933     end if;
3934     p_hold_number:=p_hold_number+1;
3935     open cv for l_stmt using p_mapping_id;
3936     loop
3937       fetch cv into
3938         p_hold_func(p_hold_number),
3939         p_hold_func_category(p_hold_number),
3940         p_hold_item(p_hold_number),
3941         p_hold_item_id(p_hold_number),
3942         p_hold_item_usage(p_hold_number),
3943         p_hold_aggregatefunction(p_hold_number),
3944         p_hold_is_distinct(p_hold_number),
3945         p_hold_relation(p_hold_number),
3946         p_hold_relation_name(p_hold_number),
3947         p_hold_relation_usage(p_hold_number),
3948         p_hold_relation_type(p_hold_number),
3949         p_hold_func_usage(p_hold_number),
3950         p_hold_func_position(p_hold_number),
3951         p_hold_func_dvalue(p_hold_number);
3952       exit when cv%notfound;
3953       p_hold_number:=p_hold_number+1;
3954     end loop;
3955     p_hold_number:=p_hold_number-1;
3956     close cv;
3957     l_stmt:='select '||
3958     '''COPY'' func_name, '||
3959     '''Basic'' category_name, '||
3960     'nvl(col.column_name,''NEXTVAL'') column_name, '||
3961     'col.column_id, '||
3962     'map_col.source_column_usage_id column_usage_id , '||
3963     'null, '||
3964     'null, '||
3965     'rel.relation_id, '||
3966     'rel.relation_name, '||
3967     'map_col.source_usage_id relation_usage_id, '||
3968     'rel.relation_type, '||
3969     'map_col.func_usage_id, '||
3970     '1 attribute_position, '||
3971     'null '||
3972     'from '||
3973     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_col, '||
3974     'edw_relations_md rel, '||
3975     'edw_pvt_columns_md col, '||
3976     'edw_pvt_map_sources_md src '||
3977     'where '||
3978     'rel.relation_id=src.source_id '||
3979     'and src.mapping_id=map_col.mapping_id '||
3980     'and src.source_usage_id=map_col.source_usage_id '||
3981     'and col.column_id(+)=map_col.source_column_id ';
3982     if g_debug then
3983       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_mapping_id||get_time);
3984     end if;
3985     p_hold_number:=p_hold_number+1;
3986     open cv for l_stmt using p_mapping_id;
3987     loop
3988       fetch cv into
3989         p_hold_func(p_hold_number),
3990         p_hold_func_category(p_hold_number),
3991         p_hold_item(p_hold_number),
3992         p_hold_item_id(p_hold_number),
3993         p_hold_item_usage(p_hold_number),
3994         p_hold_aggregatefunction(p_hold_number),
3995         p_hold_is_distinct(p_hold_number),
3996         p_hold_relation(p_hold_number),
3997         p_hold_relation_name(p_hold_number),
3998         p_hold_relation_usage(p_hold_number),
3999         p_hold_relation_type(p_hold_number),
4000         p_hold_func_usage(p_hold_number),
4001         p_hold_func_position(p_hold_number),
4002         p_hold_func_dvalue(p_hold_number);
4003       exit when cv%notfound;
4004       p_hold_number:=p_hold_number+1;
4005     end loop;
4006     p_hold_number:=p_hold_number-1;
4007     close cv;
4008   end if;
4009   if g_debug then
4010     write_to_log_file_n('Dump from get_mapping_details');
4011     for i in 1..p_hold_number loop
4012       write_to_log_file(p_hold_func(i)||' '||p_hold_func_category(i)||' '||p_hold_item(i)||' '||
4013       p_hold_item_id(i)||' '||p_hold_item_usage(i)||' '||p_hold_aggregatefunction(i)||' '||
4014       p_hold_is_distinct(i)||' '||p_hold_relation(i)||' '||p_hold_relation_name(i)||' '||
4015       p_hold_relation_usage(i)||' '||p_hold_relation_type(i)||' '||p_hold_func_usage(i)||' '||
4016       p_hold_func_position(i)||' '||p_hold_func_dvalue(i));
4017     end loop;
4018   end if;
4019   return true;
4020 Exception when others then
4021   g_status_message:=sqlerrm;
4022   write_to_log_file_n(g_status_message);
4023   return false;
4024 End;
4025 
4026 function get_mapid_dim_in_derv_map(
4027 p_dim varchar2,
4028 p_mapid out NOCOPY numberTableType,
4029 p_derv_fact_id out NOCOPY numberTableType,
4030 p_src_fact_id out NOCOPY numberTableType,
4031 p_number_mapid out NOCOPY number,
4032 p_type varchar2) return boolean is
4033 l_mapid  numberTableType;
4034 l_derv_fact_id  numberTableType;
4035 l_src_fact_id  numberTableType;
4036 l_number_mapid  number;
4037 l_stmt varchar2(1000);
4038 TYPE CurTyp IS REF CURSOR;
4039 cv   CurTyp;
4040 l_is_name varchar2(40);
4041 Begin
4042   p_number_mapid:=0;
4043   if p_type='INC' and g_read_cfig_options=false then
4044     l_stmt:='select '||
4045     'distinct map.mapping_id,  '||
4046     'tgt.fact_id,  '||
4047     'src.fact_id  '||
4048     'from  '||
4049     'edw_pvt_map_sources_md_v ru,  '||
4050     'edw_dimensions_md_v rel,  '||
4051     'edw_pvt_map_properties_md_v map,  '||
4052     'edw_facts_md_v src,  '||
4053     'edw_facts_md_v tgt,  '||
4054     'edw_attribute_sets_md_v sis  '||
4055     'where  '||
4056     'ru.mapping_id=map.mapping_id '||
4057     'and rel.dim_id=ru.source_id '||
4058     'and rel.dim_name=:a '||
4059     'and map.primary_source=src.fact_id '||
4060     'and map.primary_target=tgt.fact_id '||
4061     'and sis.attribute_group_name=''EDW_INC_REFRESH'' '||
4062     'and sis.entity_id=tgt.fact_id';
4063   else
4064     l_stmt:='select '||
4065     'distinct map.mapping_id,  '||
4066     'tgt.fact_id,  '||
4067     'src.fact_id  '||
4068     'from  '||
4069     'edw_pvt_map_sources_md_v ru,  '||
4070     'edw_dimensions_md_v rel,  '||
4071     'edw_pvt_map_properties_md_v map,  '||
4072     'edw_facts_md_v src,  '||
4073     'edw_facts_md_v tgt '||
4074     'where  '||
4075     'ru.mapping_id=map.mapping_id '||
4076     'and rel.dim_id=ru.source_id '||
4077     'and rel.dim_name=:a '||
4078     'and map.primary_source=src.fact_id '||
4079     'and map.primary_target=tgt.fact_id ';
4080   end if;
4081   l_number_mapid:=1;
4082   if g_debug then
4083     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_dim);
4084   end if;
4085   open cv for l_stmt using p_dim;
4086   loop
4087     fetch cv into l_mapid(l_number_mapid),l_derv_fact_id(l_number_mapid),l_src_fact_id(l_number_mapid);
4088     exit when cv%notfound;
4089     l_number_mapid:=l_number_mapid+1;
4090   end loop;
4091   l_number_mapid:=l_number_mapid-1;
4092   close cv;
4093   if p_type='INC' and g_read_cfig_options then
4094     for i in 1..l_number_mapid loop
4095       l_is_name:=null;
4096       if edw_option.get_warehouse_option(null,l_derv_fact_id(i),'INCREMENTAL',l_is_name)=false then
4097         null;
4098       end if;
4099       if l_is_name='Y' then
4100         p_number_mapid:=p_number_mapid+1;
4101         p_mapid(p_number_mapid):=l_mapid(i);
4102         p_derv_fact_id(p_number_mapid):=l_derv_fact_id(i);
4103         p_src_fact_id(p_number_mapid):=l_src_fact_id(i);
4104       end if;
4105     end loop;
4106   else
4107     p_mapid:=l_mapid;
4108     p_derv_fact_id:=l_derv_fact_id;
4109     p_src_fact_id:=l_src_fact_id;
4110     p_number_mapid:=l_number_mapid;
4111   end if;
4112   return true;
4113 Exception when others then
4114   g_status_message:=sqlerrm;
4115   write_to_log_file_n(g_status_message);
4116   return false;
4117 End;
4118 
4119 /*
4120 for a sumamry fact and dim, look at the fk and if its pointing to a higher level, get the pk_key of that level
4121 from the dim table
4122 */
4123 function get_dim_fk_summary_fact(
4124 p_fact_id number,
4125 p_dim_id number,
4126 p_dim_fk out NOCOPY varcharTableType,
4127 p_number_dim_fk out NOCOPY number
4128 ) return boolean is
4129 TYPE CurTyp IS REF CURSOR;
4130 cv   CurTyp;
4131 l_stmt varchar2(4000);
4132 Begin
4133   l_stmt:='select lvl.level_prefix||''_''||item.column_name '||
4134   'from  '||
4135   'edw_foreign_keys_md_v fk,  '||
4136   'edw_unique_keys_md_v pk,  '||
4137   'edw_dimensions_md_v dim,  '||
4138   'edw_levels_md_v lvl,  '||
4139   'edw_tables_md_v ltc,  '||
4140   'edw_unique_keys_md_v ltc_pk,  '||
4141   'edw_pvt_columns_md_v item,  '||
4142   'edw_pvt_key_columns_md_v isu '||
4143   'where  '||
4144   'fk.entity_id=:a  '||
4145   'and fk.key_id=pk.key_id '||
4146   'and dim.dim_id=pk.entity_id '||
4147   'and dim.dim_id=:b  '||
4148   'and lvl.dim_id=dim.dim_id '||
4149   'and lvl.level_prefix=substr(fk.foreign_key_name,instr(fk.foreign_key_name,''_'',-1)+1,length(fk.foreign_key_name)) '||
4150   'and ltc.name=lvl.level_name||''_LTC'' '||
4151   'and ltc_pk.entity_id=ltc.elementid '||
4152   'and isu.key_id=ltc_pk.key_id '||
4153   'and isu.column_id=item.column_id '||
4154   'and item.parent_object_id=ltc.elementid '||
4155   'and item.data_type=''NUMBER''';
4156   if g_debug then
4157     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_fact_id||','||p_dim_id);
4158   end if;
4159   p_number_dim_fk:=1;
4160   open cv for l_stmt using p_fact_id,p_dim_id;
4161   loop
4162     fetch cv into p_dim_fk(p_number_dim_fk);
4163     exit when cv%notfound;
4164     p_number_dim_fk:=p_number_dim_fk+1;
4165   end loop;
4166   p_number_dim_fk:=p_number_dim_fk-1;
4167   close cv;
4168   return true;
4169 Exception when others then
4170   g_status_message:=sqlerrm;
4171   write_to_log_file_n(g_status_message);
4172   return false;
4173 End;
4174 
4175 function get_object_name(p_object_id number) return varchar2 is
4176 TYPE CurTyp IS REF CURSOR;
4177 cv   CurTyp;
4178 l_stmt varchar2(1000);
4179 l_name varchar2(400);
4180 Begin
4181   l_stmt:='select relation_name from edw_relations_md_v where relation_id=:a';
4182   open cv for l_stmt using p_object_id;
4183   fetch cv into l_name;
4184   close cv;
4185   return l_name;
4186 Exception when others then
4187   g_status_message:=sqlerrm;
4188   write_to_log_file_n(g_status_message);
4189   return null;
4190 End;
4191 
4192 function inc_g_load_pk return number is
4193 l_stmt varchar2(400);
4194 TYPE CurTyp IS REF CURSOR;
4195 cv   CurTyp;
4196 l_load_pk number;
4197 Begin
4198   l_stmt:='select edw_load_s.nextval from dual';
4199   open cv for l_stmt;
4200   fetch cv into l_load_pk;
4201   close cv;
4202   return l_load_pk;
4203 Exception when others then
4204   g_status_message:=sqlerrm;
4205   write_to_log_file_n(g_status_message);
4206   return null;
4207 End;
4208 
4209 function get_column_stats(
4210 p_owner varchar2,
4211 p_object varchar2,
4212 p_fk varchar2,
4213 p_distcnt out NOCOPY number,
4214 p_density out NOCOPY number,
4215 p_nullcnt out NOCOPY number,
4216 p_srec out NOCOPY DBMS_STATS.StatRec,
4217 p_avgclen out NOCOPY number
4218 ) return boolean is
4219 Begin
4220   DBMS_STATS.GET_COLUMN_STATS (
4221     p_owner,
4222     p_object,
4223     p_fk,
4224     NULL,
4225     NULL,
4226     NULL,
4227     p_distcnt,
4228     p_density,
4229     p_nullcnt,
4230     p_srec,
4231     p_avgclen,
4232     NULL);
4233   return true;
4234 Exception when others then
4235   g_status_message:=sqlerrm;
4236   write_to_log_file_n(g_status_message);
4237   return false;
4238 End;
4239 
4240 function get_table_stats(
4241 p_owner varchar2,
4242 p_object varchar2,
4243 p_numrows out NOCOPY number,
4244 p_numblks out NOCOPY number,
4245 p_avgrlen out NOCOPY number
4246 ) return boolean is
4247 Begin
4248  if g_debug then
4249    write_to_log_file_n('call DBMS_STATS.GET_TABLE_STATS with '||p_owner||','||p_object);
4250  end if;
4251  DBMS_STATS.GET_TABLE_STATS(
4252    p_owner,
4253    p_object,
4254    NULL,
4255    NULL,
4256    NULL,
4257    p_numrows,
4258    p_numblks,
4259    p_avgrlen,
4260    NULL);
4261   return true;
4262 Exception when others then
4263   g_status_message:=sqlerrm;
4264   write_to_log_file_n(g_status_message);
4265   return false;
4266 End;
4267 
4268 function get_fk_pk(p_child number,p_parent number,p_map_id number,
4269 p_fk out NOCOPY varcharTableType,p_pk out NOCOPY varcharTableType,
4270 p_number_fk out NOCOPY number) return boolean is
4271 TYPE CurTyp IS REF CURSOR;
4272 cv   CurTyp;
4273 l_stmt varchar2(8000);
4274 Begin
4275   if p_map_id is not null then
4276     l_stmt:='select pk_item.column_name, fk_item.column_name '||
4277     'from  '||
4278     'edw_pvt_map_properties_md_v map, '||
4279     'edw_pvt_map_sources_md_v map_sources_dim, '||
4280     'edw_pvt_map_sources_md_v map_sources_fact, '||
4281     'edw_pvt_map_key_usages_md_v map_key_usage, '||
4282     'edw_pvt_key_columns_md_v fk_key_column, '||
4283     'edw_pvt_columns_md_v fk_item, '||
4284     'edw_pvt_key_columns_md_v pk_key_column, '||
4285     'edw_pvt_columns_md_v pk_item '||
4286     'where '||
4287     'map_sources_dim.source_id=:a '||
4288     'and map_sources_fact.source_id=:b '||
4289     'and map.mapping_id=:c     '||
4290     'and map_sources_dim.mapping_id=map.mapping_id '||
4291     'and map_sources_fact.mapping_id=map.mapping_id '||
4292     'and map_key_usage.source_usage_id=map_sources_fact.source_usage_id '||
4293     'and map_key_usage.mapping_id=map.mapping_id '||
4294     'and fk_key_column.key_id=map_key_usage.foreign_key_id '||
4295     'and fk_item.column_id=fk_key_column.column_id '||
4296     'and fk_item.parent_object_id=map_sources_fact.source_id '||
4297     'and pk_key_column.key_id=map_key_usage.unique_key_id '||
4298     'and pk_item.column_id=pk_key_column.column_id '||
4299     'and pk_item.parent_object_id=map_sources_dim.source_id ';
4300     if g_debug then
4301       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_parent||','||p_child||','||p_map_id);
4302     end if;
4303     open cv for l_stmt using p_parent,p_child,p_map_id;
4304   else
4305     l_stmt:='select pk_item.column_name,fk_item.column_name '||
4306     'from  '||
4307     'edw_foreign_keys_md_v fk, '||
4308     'edw_unique_keys_md_v pk, '||
4309     'edw_pvt_key_columns_md_v fk_use, '||
4310     'edw_pvt_key_columns_md_v pk_use, '||
4311     'edw_pvt_columns_md_v fk_item, '||
4312     'edw_pvt_columns_md_v pk_item '||
4313     'where  '||
4314     '    pk.entity_id=:a  '||
4315     'and fk.entity_id=:b  '||
4316     'and fk_use.key_id=fk.foreign_key_id '||
4317     'and fk_item.column_id=fk_use.column_id '||
4318     'and fk_item.parent_object_id=fk.entity_id '||
4319     'and pk.key_id=fk.key_id '||
4320     'and pk_use.key_id=pk.key_id '||
4321     'and pk_item.column_id=pk_use.column_id '||
4322     'and pk_item.parent_object_id=pk.entity_id ';
4323     if g_debug then
4324       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_parent||','||p_child);
4325     end if;
4326     open cv for l_stmt using p_parent,p_child;
4327   end if;
4328   p_number_fk:=1;
4329   loop
4330     fetch cv into p_pk(p_number_fk),p_fk(p_number_fk);
4331     exit when cv%notfound;
4332     p_number_fk:=p_number_fk+1;
4333   end loop;
4334   p_number_fk:=p_number_fk-1;
4335   close cv;
4336   return true;
4337 Exception when others then
4338   g_status_message:=sqlerrm;
4339   write_to_log_file_n(g_status_message);
4340   p_number_fk:=0;
4341   return false;
4342 End;
4343 
4344 function create_prot_table(p_table varchar2,p_op_table_space varchar2) return boolean is
4345 l_stmt varchar2(1000);
4346 Begin
4347   if drop_table(p_table)=false then
4348     null;
4349   end if;
4350   if p_op_table_space is not null then
4351     l_stmt:='create table '||p_table||'(row_id rowid) tablespace '||p_op_table_space;
4352   else
4353     l_stmt:='create table '||p_table||'(row_id rowid)';
4354   end if;
4355   if g_debug then
4356     write_to_log_file_n('Going to execute '||l_stmt);
4357   end if;
4358   execute immediate l_stmt;
4359   return true;
4360 Exception when others then
4361   g_status_message:=sqlerrm;
4362   write_to_log_file_n(g_status_message);
4363   g_status:=false;
4364   return false;
4365 End;
4366 
4367 function drop_prot_table(p_table varchar2) return boolean is
4368 l_stmt varchar2(1000);
4369 Begin
4370   if drop_table(p_table)=false then
4371     null;
4372   end if;
4373   return true;
4374 Exception when others then
4375   g_status_message:=sqlerrm;
4376   write_to_log_file_n(g_status_message);
4377   g_status:=false;
4378   return false;
4379 End;
4380 
4381 function get_all_maps_for_tgt(p_object_id number,p_maps out NOCOPY numberTableType,p_number_maps out NOCOPY number)
4382 return boolean is
4383 TYPE CurTyp IS REF CURSOR;
4384 cv   CurTyp;
4385 l_stmt varchar2(1000);
4386 Begin
4387   l_stmt:='select mapping_id from edw_pvt_map_properties_md_v where primary_target=:a';
4388   open cv for l_stmt using p_object_id;
4389   p_number_maps:=1;
4390   loop
4391     fetch cv into p_maps(p_number_maps);
4392     exit when cv%notfound;
4393     p_number_maps:=p_number_maps+1;
4394   end loop;
4395   close cv;
4396   p_number_maps:=p_number_maps-1;
4397   return true;
4398 Exception when others then
4399   g_status_message:=sqlerrm;
4400   write_to_log_file_n(g_status_message);
4401   g_status:=false;
4402   return false;
4403 End;
4404 
4405 function create_table(p_table varchar2,p_stmt varchar2,p_count out NOCOPY number) return boolean is
4406 Begin
4407   p_count:=0;
4408   if drop_table(p_table) = false then
4409     null;
4410   end if;
4411   if g_debug then
4412     write_to_log_file_n('Going to execute '||p_stmt||get_time);
4413   end if;
4414   execute immediate p_stmt;
4415   p_count:=sql%rowcount;
4416   if g_debug then
4417     write_to_log_file_n('Created '||p_table||' with '||p_count||' rows '||get_time);
4418   end if;
4419   analyze_table_stats(substr(p_table,instr(p_table,'.')+1,
4420   length(p_table)),substr(p_table,1,instr(p_table,'.')-1));
4421   return true;
4422 Exception when others then
4423   g_status_message:=sqlerrm;
4424   write_to_log_file_n(g_status_message);
4425   return false;
4426 End;
4427 
4428 function is_itemset_implemented(p_object_name varchar2,
4429 p_item_set varchar2) return varchar2 is
4430 Begin
4431   return is_itemset_implemented(p_object_name,p_item_set,null);
4432 Exception when others then
4433   g_status_message:=sqlerrm;
4434   write_to_log_file_n(g_status_message);
4435   g_status:=false;
4436   return null;
4437 End;
4438 function is_itemset_implemented(p_object_name varchar2,p_item_set varchar2,p_object_id number) return varchar2 is
4439 l_stmt varchar2(2000);
4440 TYPE CurTyp IS REF CURSOR;
4441 cv   CurTyp;
4442 l_res number:=null;
4443 Begin
4444   if g_debug then
4445     write_to_log_file_n('In is_itemset_implemented '||p_object_name||' '||p_item_set||' '||p_object_id);
4446   end if;
4447   if p_object_id is null then
4448     l_stmt:='select 1 from edw_relations_md_v rel,edw_attribute_sets_md_v sis where sis.attribute_group_name=:a '||
4449           ' and sis.entity_id=rel.relation_id and rel.relation_name=:b';
4450   else
4451     l_stmt:='select 1 from edw_attribute_sets_md_v sis where sis.attribute_group_name=:a and sis.entity_id=:b';
4452   end if;
4453   --if g_debug then
4454     --write_to_log_file_n('Going to execute '||l_stmt);
4455   --end if;
4456   if p_object_id is null then
4457     open cv for l_stmt using p_item_set,p_object_name;
4458   else
4459     open cv for l_stmt using p_item_set,p_object_id;
4460   end if;
4461   fetch cv into l_res;
4462   close cv;
4463   if l_res is null then
4464     if g_debug then
4465       write_to_log_file('Itemset not implemented');
4466     end if;
4467     return 'N';
4468   else
4469     if g_debug then
4470       write_to_log_file('Itemset implemented');
4471     end if;
4472     return 'Y';
4473   end if;
4474   return 'N';
4475 Exception when others then
4476   g_status_message:=sqlerrm;
4477   write_to_log_file_n(g_status_message);
4478   g_status:=false;
4479   return null;
4480 End;
4481 
4482 function get_DA_table(p_object varchar2) return varchar2 is
4483 Begin
4484   return get_DA_table(p_object,null);
4485 Exception when others then
4486   g_status_message:=sqlerrm;
4487   write_to_log_file_n(g_status_message);
4488   return null;
4489 End;
4490 function get_DA_table(p_object varchar2,p_owner varchar2) return varchar2 is
4491 l_table varchar2(200);
4492 Begin
4493   l_table:=p_object||'DA';
4494   if check_table(l_table)=false then
4495     if p_owner is null then
4496       l_table:=get_table_owner(p_object)||'.'||p_object||'DA';
4497     else
4498       l_table:=p_owner||'.'||p_object||'DA';
4499     end if;
4500   end if;
4501   return l_table;
4502 Exception when others then
4503   g_status_message:=sqlerrm;
4504   write_to_log_file_n(g_status_message);
4505   return null;
4506 End;
4507 
4508 function get_PP_table(p_object varchar2) return varchar2 is
4509 Begin
4510   return get_PP_table(p_object,null);
4511 Exception when others then
4512   g_status_message:=sqlerrm;
4513   write_to_log_file_n(g_status_message);
4514   return null;
4515 End;
4516 function get_PP_table(p_object varchar2,p_owner varchar2) return varchar2 is
4517 l_table varchar2(200);
4518 Begin
4519   l_table:=p_object||'PP';
4520   if check_table(l_table)=false then
4521     if p_owner is null then
4522       l_table:=get_table_owner(p_object)||'.'||p_object||'PP';
4523     else
4524       l_table:=p_owner||'.'||p_object||'PP';
4525     end if;
4526   end if;
4527   return l_table;
4528 Exception when others then
4529   g_status_message:=sqlerrm;
4530   write_to_log_file_n(g_status_message);
4531   return null;
4532 End;
4533 
4534 function get_master_instance(p_object_name varchar2) return varchar2 is
4535 l_master varchar2(400):=null;
4536 l_stmt  varchar2(2000);
4537 TYPE CurTyp IS REF CURSOR;
4538 cv   CurTyp;
4539 l_res number:=null;
4540 Begin
4541   if g_read_cfig_options then
4542     if edw_option.get_warehouse_option(p_object_name,null,'MASTER_INSTANCE',l_master)=false then
4543       null;
4544     end if;
4545   else
4546     l_stmt:='select instr(lower(description),''master instance'') from edw_relations_md_v where relation_name=:a';
4547     if g_debug then
4548       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_name||get_time);
4549     end if;
4550     open cv for l_stmt using p_object_name;
4551     fetch cv into l_res;
4552     close cv;
4553     if l_res =0 then
4554       return null;
4555     end if;
4556     l_stmt:='select rtrim(ltrim(substr(upper(description),instr(lower(description),''master instance'')+16,'||
4557     'length(description)))) from edw_relations_md_v where relation_name=:a';
4558     if g_debug then
4559       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_name||get_time);
4560     end if;
4561     open cv for l_stmt using p_object_name;
4562     fetch cv into l_master;
4563     close cv;
4564   end if;
4565   if g_debug then
4566     write_to_log_file('Result '||l_master);
4567   end if;
4568   return l_master;
4569 Exception when others then
4570   g_status_message:=sqlerrm;
4571   write_to_log_file_n(g_status_message);
4572   return null;
4573 End;
4574 
4575 function log_into_cdi_results_table(
4576 p_object varchar2
4577 ,p_object_type  varchar2
4578 ,p_object_id number
4579 ,p_interface_table varchar2
4580 ,p_interface_table_id number
4581 ,p_interface_table_pk varchar2
4582 ,p_interface_table_pk_id number
4583 ,p_interface_table_fk varchar2
4584 ,p_interface_table_fk_id number
4585 ,p_parent_table varchar2
4586 ,p_parent_table_id number
4587 ,p_parent_table_pk varchar2
4588 ,p_parent_table_pk_id number
4589 ,p_number_dangling number
4590 ,p_number_duplicate number
4591 ,p_number_error number
4592 ,p_total_records number
4593 ,p_error_type varchar2) return boolean is
4594 l_stmt varchar2(4000);
4595 Begin
4596   l_stmt:='insert into EDW_CDI_RESULTS('||
4597     'OBJECT_NAME '||
4598    ',OBJECT_ID   '||
4599    ',OBJECT_TYPE '||
4600    ',INTERFACE_TABLE '||
4601    ',INTERFACE_TABLE_ID '||
4602    ',INTERFACE_TABLE_PK '||
4603    ',INTERFACE_TABLE_PK_ID '||
4604    ',INTERFACE_TABLE_FK '||
4605    ',INTERFACE_TABLE_FK_ID '||
4606    ',PARENT_TABLE  '||
4607    ',PARENT_TABLE_ID '||
4608    ',PARENT_TABLE_PK '||
4609    ',PARENT_TABLE_PK_ID '||
4610    ',NUMBER_DANGLING '||
4611    ',NUMBER_DUPLICATE '||
4612    ',NUMBER_ERROR   '||
4613    ',TOTAL_RECORDS   '||
4614    ',ERROR_TYPE      '||
4615    ',CREATED_BY      '||
4616    ',LAST_UPDATE_BY  '||
4617    ',LAST_UPDATE_LOGIN '||
4618    ',CREATION_DATE     '||
4619    ',LAST_UPDATE_DATE) '||
4620    'values (:a1,:a2,:a3,:a4,:a5,:a6,:a7,:a8,:a9,:a10,:a11,:a12,:a13,:a14,:a15,:a16,:a17,:a18,:a19,:a20,'||
4621    ':a21,:a22,:a23)';
4622    if g_debug then
4623      write_to_log_file_n('Going to execute '||l_stmt||' Using ');
4624      write_to_log_file(p_object||','||p_object_id||','||p_object_type||','||p_interface_table||','||
4625      p_interface_table_id||','||p_interface_table_pk||','||p_interface_table_pk_id||','||
4626      p_interface_table_fk||','||p_interface_table_fk_id||','||p_parent_table||','||
4627      p_parent_table_id||','||p_parent_table_pk||','||p_parent_table_pk_id||','||p_number_dangling||','||
4628      p_number_duplicate||','||p_number_error||','||p_total_records||','||p_error_type||','||g_conc_program_id||','||
4629      g_conc_program_id||','||g_conc_program_id||','||sysdate||','||sysdate);
4630    end if;
4631    execute immediate l_stmt using p_object,p_object_id,p_object_type,p_interface_table,
4632    p_interface_table_id,p_interface_table_pk,p_interface_table_pk_id,p_interface_table_fk,p_interface_table_fk_id,
4633    p_parent_table,p_parent_table_id,p_parent_table_pk,p_parent_table_pk_id,p_number_dangling,p_number_duplicate,
4634    p_number_error,p_total_records,p_error_type,g_conc_program_id,g_conc_program_id,g_conc_program_id,sysdate,sysdate;
4635    commit;
4636   return true;
4637 Exception when others then
4638  write_to_log_file_n('Error in log_into_cdi_results_table '||sqlerrm);
4639  return false;
4640 End;
4641 
4642 function log_into_cdi_dang_table(p_key_id number,p_table_id number,p_parent_table_id number,
4643 p_key_value varchar2,p_number_key_value number,p_instance varchar2,p_bad_key varchar2) return boolean is
4644 l_stmt varchar2(4000);
4645 Begin
4646   l_stmt:='insert into edw_cdi_key_values (key_id,table_id,parent_table_id,instance,key_value,number_key_value,'||
4647   'bad_key) values(:a1,:a2,:a3,:a4,:a5,:a6,:a7)';
4648   execute immediate l_stmt using p_key_id,p_table_id,p_parent_table_id,p_instance,p_key_value,p_number_key_value,
4649   p_bad_key;
4650   return true;
4651 Exception when others then
4652  write_to_log_file_n('Error in log_into_cdi_dang_table '||sqlerrm);
4653  return false;
4654 End;
4655 
4656 function get_column_id(p_column varchar2,p_table varchar2) return number is
4657 l_stmt  varchar2(2000);
4658 TYPE CurTyp IS REF CURSOR;
4659 cv   CurTyp;
4660 l_res number:=null;
4661 Begin
4662   l_stmt:='select item.column_id from edw_pvt_columns_md_v item, edw_relations_md_v rel where rel.relation_name=:a '||
4663   'and item.column_name=:b and item.parent_object_id=rel.relation_id and item.parent_object_id=rel.relation_id';
4664   open cv for l_stmt using p_table,p_column;
4665   fetch cv into l_res;
4666   close cv;
4667   return l_res;
4668 Exception when others then
4669  write_to_log_file_n('Error in get_column_id '||sqlerrm);
4670  return null;
4671 End;
4672 
4673 function get_instance_col(p_table varchar2) return varchar2 is
4674 l_stmt  varchar2(2000);
4675 TYPE CurTyp IS REF CURSOR;
4676 cv   CurTyp;
4677 l_res varchar2(200);
4678 Begin
4679   l_stmt:='select fk_item.column_name from  edw_relations_md_v rel, edw_foreign_keys_md_v fk, '||
4680   'edw_pvt_key_columns_md_v fkisu,edw_pvt_columns_md_v fk_item,edw_dimensions_md_v p_rel,edw_unique_keys_md_v pk '||
4681   'where rel.relation_name=:a and fk.entity_id=rel.relation_id and fkisu.key_id=fk.foreign_key_id '||
4682   'and fk_item.column_id=fkisu.column_id and fk_item.parent_object_id=rel.relation_id and pk.key_id=fk.key_id and '||
4683   'p_rel.dim_id=pk.entity_id and p_rel.dim_name=''EDW_INSTANCE_M''';
4684   if g_debug then
4685     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table);
4686   end if;
4687   open cv for l_stmt using p_table;
4688   fetch cv into l_res;
4689   close cv;
4690   if l_res is null then
4691     l_stmt:='select item.column_name from edw_pvt_columns_md_v item, edw_relations_md_v rel where '||
4692     'item.parent_object_id=rel.relation_id and rel.relation_name=:a and item.column_name '||
4693     'in (''INSTANCE'',''INSTANCE_CODE'') and item.parent_object_id=rel.relation_id';
4694     if g_debug then
4695       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table);
4696     end if;
4697     open cv for l_stmt using p_table;
4698     fetch cv into l_res;
4699     close cv;
4700   end if;
4701   return l_res;
4702 Exception when others then
4703  write_to_log_file_n('Error in get_instance_col '||sqlerrm);
4704  return null;
4705 End;
4706 
4707 function create_synonym(p_synonym varchar2,p_table varchar2) return boolean is
4708 l_stmt varchar2(4000);
4709 Begin
4710   l_stmt:='drop synonym '||p_synonym;
4711   begin
4712     if g_debug then
4713       write_to_log_file_n('Going to execute '||l_stmt);
4714     end if;
4715     execute immediate l_stmt;
4716   exception when others then
4717     write_to_log_file_n('Synonym '||p_synonym||' does not exist');
4718   end;
4719   l_stmt:='create synonym '||p_synonym||' for '||p_table;
4720   if g_debug then
4721     write_to_log_file_n('Going to execute '||l_stmt);
4722   end if;
4723   execute immediate l_stmt;
4724   return true;
4725 Exception when others then
4726  write_to_log_file_n('Error in create_synonym '||sqlerrm);
4727  return false;
4728 End;
4729 
4730 procedure create_bad_key_table(p_table varchar2,p_op_table_space varchar2,p_wh_parallel number) is
4731 l_stmt varchar2(4000);
4732 Begin
4733   l_stmt:='create table '||p_table||'(key_value varchar2(800)) '; --tablespace '||p_op_table_space;
4734   if p_wh_parallel is not null then
4735     l_stmt:=l_stmt||' parallel degree('||p_wh_parallel||') ';
4736   end if;
4737   l_stmt:=l_stmt||' tablespace '||p_op_table_space;
4738   if drop_table(p_table)=false then
4739     null;
4740   end if;
4741   g_status_message:=sqlerrm;
4742   execute immediate l_stmt;
4743 Exception when others then
4744   g_status:=false;
4745   g_status_message:=sqlerrm;
4746   write_to_log_file_n(g_status_message);
4747 End;
4748 
4749 function is_src_of_custom_inc_derv_fact(p_fact varchar2) return number is
4750 l_stmt varchar2(10000);
4751 TYPE CurTyp IS REF CURSOR;
4752 cv   CurTyp;
4753 l_var number:=null;
4754 Begin
4755   if g_debug then
4756     write_to_log_file_n('In is_src_of_custom_inc_derv_fact, input param='||p_fact);
4757   end if;
4758   l_stmt:='select 1 from edw_facts_md_v df, edw_facts_md_v fact, '||
4759            'edw_pvt_map_properties_md_v map, edw_attribute_sets_md_v sis  '||
4760            'where fact.fact_name=:a and map.primary_source=fact.fact_id and df.fact_id=map.primary_target '||
4761            'and sis.entity_id=df.fact_id and sis.attribute_group_name=''EDW_CUSTOM_INC_REFRESH''';
4762   open cv for l_stmt using p_fact;
4763   fetch cv into l_var;
4764   close cv;
4765   if l_var = 1 then
4766     return 1;
4767   else
4768     return 0;
4769   end if;
4770 Exception when others then
4771   g_status_message:=sqlerrm;
4772   write_to_log_file_n('Exception in  is_src_of_custom_inc_derv_fact '||sqlerrm||' '||get_time);
4773   return -1;
4774 End;
4775 
4776 function get_pk_view(p_dim varchar2,p_db_link varchar2) return varchar2 is
4777 l_view varcharTableType;
4778 l_number_view number;
4779 l_stmt varchar2(4000);
4780 TYPE CurTyp IS REF CURSOR;
4781 cv   CurTyp;
4782 l_res number;
4783 l_pkg varchar2(1000);
4784 Begin
4785   l_stmt:='select vw.view_name '||
4786   'from  '||
4787   'edw_pvt_md_views_v vw,  '||
4788   'edw_dimensions_md_v dim,  '||
4789   'edw_foreign_keys_md_v fk,  '||
4790   'edw_unique_keys_md_v pk  '||
4791   'where fk.entity_id=vw.view_id '||
4792   'and pk.entity_id=dim.dim_id '||
4793   'and fk.key_id=pk.key_id '||
4794   'and instr(fk.foreign_key_name,''_PK_VIEW'')<>0  '||
4795   'and dim.dim_name=:a ';
4796   if g_debug then
4797     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_dim);
4798   end if;
4799   l_number_view:=1;
4800   open cv for l_stmt using p_dim;
4801   loop
4802     fetch cv into l_view(l_number_view);
4803     exit when cv%notfound;
4804     l_number_view:=l_number_view+1;
4805   end loop;
4806   close cv;
4807   l_number_view:=l_number_view-1;
4808   if g_debug then
4809     write_to_log_file_n('Result');
4810     for i in 1..l_number_view loop
4811       write_to_log_file(l_view(i));
4812     end loop;
4813   end if;
4814   --see if a package exists
4815   /*for i in 1..l_number_view loop
4816     l_pkg:=l_view(i)||'_PKG.CREATE_PK_VIEW@'||p_db_link;
4817     l_stmt:='select '||l_pkg||' from dual';
4818     begin
4819       if g_debug then
4820         write_to_log_file_n('Going to execute '||l_stmt);
4821       end if;
4822       open cv for l_stmt;
4823       fetch cv into l_res;
4824       close cv;
4825       if l_res=-1 then
4826         g_status_message:=sqlerrm;
4827         g_status:=false;
4828         write_to_log_file_n(g_status_message);
4829         return null;
4830       end if;
4831       return l_view(i);
4832     exception when others then
4833       if g_debug then
4834         write_to_log_file_n('Package not found');
4835       end if;
4836     end;
4837   end loop;*/
4838   for i in 1..l_number_view loop
4839     l_stmt:='select 1 from '||l_view(i)||'@'||p_db_link||' where rownum=1';
4840     begin
4841       if g_debug then
4842         write_to_log_file_n('Going to execute '||l_stmt);
4843       end if;
4844       execute immediate l_stmt;
4845       return l_view(i);
4846     exception when others then
4847       if g_debug then
4848         write_to_log_file_n('View not found');
4849       end if;
4850     end;
4851   end loop;
4852   return null;
4853 Exception when others then
4854   g_status:=false;
4855   g_status_message:=sqlerrm;
4856   write_to_log_file_n(g_status_message);
4857   return null;
4858 End;
4859 
4860 function get_logical_name(p_obj_id number) return varchar2 is
4861 l_name varchar2(400);
4862 l_stmt varchar2(2000);
4863 TYPE CurTyp IS REF CURSOR;
4864 cv   CurTyp;
4865 Begin
4866   l_stmt:='select relation_long_name from edw_relations_md_v where relation_id=:a';
4867   open cv for l_stmt using p_obj_id;
4868   fetch cv into l_name;
4869   close cv;
4870   return l_name;
4871 Exception when others then
4872   g_status:=false;
4873   g_status_message:=sqlerrm;
4874   write_to_log_file_n(g_status_message);
4875   return null;
4876 End;
4877 
4878 function parse_names(p_list varchar2,p_names out NOCOPY varcharTableType,p_number_names out NOCOPY number)
4879 return boolean is
4880 Begin
4881   if parse_names(p_list,',',p_names,p_number_names)=false then
4882     return false;
4883   end if;
4884   return true;
4885 Exception when others then
4886  write_to_conc_log_file('Error in parse_names '||sqlerrm);
4887  return false;
4888 End;
4889 
4890 function parse_names(
4891 p_list varchar2,
4892 p_separator varchar2,
4893 p_names out NOCOPY varcharTableType,
4894 p_number_names out NOCOPY number)
4895 return boolean is
4896 l_start number;
4897 l_end number;
4898 l_len number;
4899 Begin
4900   p_number_names:=0;
4901   if p_list is null then
4902     return true;
4903   end if;
4904   l_len:=length(p_list);
4905   if l_len<=0 then
4906     return true;
4907   end if;
4908   if instr(p_list,p_separator)=0 then
4909     p_number_names:=1;
4910     p_names(p_number_names):=ltrim(rtrim(p_list));
4911     return true;
4912   end if;
4913   l_start:=1;
4914   loop
4915     l_end:=instr(p_list,p_separator,l_start);
4916     if l_end=0 then
4917       l_end:=l_len+1;
4918     end if;
4919     p_number_names:=p_number_names+1;
4920     p_names(p_number_names):=ltrim(rtrim(substr(p_list,l_start,(l_end-l_start))));
4921     l_start:=l_end+1;
4922     if l_end>=l_len then
4923       exit;
4924     end if;
4925   end loop;
4926   return true;
4927 Exception when others then
4928  write_to_conc_log_file('Error in parse_names '||sqlerrm);
4929  return false;
4930 End;
4931 
4932 function get_status_message return varchar2 is
4933 Begin
4934   return g_status_message;
4935 Exception when others then
4936  write_to_log_file_n('Error in get_status_message '||sqlerrm);
4937  return null;
4938 End;
4939 
4940 procedure set_rollback(p_rollback varchar2) is
4941 Begin
4942   if p_rollback is not null then
4943     execute immediate 'SET TRANSACTION USE ROLLBACK SEGMENT '||p_rollback;
4944   end if;
4945 Exception when others then
4946   write_to_log_file_n('Error in set_rollback '||sqlerrm);
4947 End;
4948 
4949 function get_ltc_fact_unique_key(p_object_id number,p_object_name varchar2,
4950 p_unique_key out NOCOPY varchar2,p_pk_key out NOCOPY varchar2) return boolean is
4951 l_stmt varchar2(10000);
4952 TYPE CurTyp IS REF CURSOR;
4953 cv   CurTyp;
4954 l_object_id number;
4955 l_uk varchar2(400);
4956 l_pk varchar2(400);
4957 l_mapping_id number;
4958 Begin
4959   l_object_id:=p_object_id;
4960   if l_object_id is null then
4961     l_object_id:=get_object_id(p_object_name);
4962   end if;
4963   l_mapping_id:=get_target_map(l_object_id,null);
4964   l_stmt:='select '||
4965   'tgt_column.column_name '||
4966   'from '||
4967   'edw_pvt_map_properties_md_v map_properties, '||
4968   'edw_pvt_map_sources_md_v map_sources, '||
4969   'edw_pvt_map_targets_md_v map_targets, '||
4970   'edw_tables_md_v src_table, '||
4971   'edw_relations_md_v tgt_relation, '||
4972   'edw_unique_keys_md_v pk, '||
4973   'edw_pvt_key_columns_md_v key_usage, '||
4974   'edw_pvt_columns_md_v pk_col, '||
4975   '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns, '||
4976   'edw_pvt_columns_md_v tgt_column '||
4977   'where '||
4978   '  map_sources.source_id=map_properties.primary_source '||
4979   'and map_targets.target_id=map_properties.primary_target '||
4980   'and map_properties.mapping_id=:a '||
4981   'and map_sources.mapping_id=map_properties.mapping_id '||
4982   'and src_table.elementid=map_sources.source_id '||
4983   'and map_targets.mapping_id=map_properties.mapping_id '||
4984   'and tgt_relation.relation_id=map_targets.target_id '||
4985   'and pk.entity_id=map_properties.primary_source '||
4986   'and key_usage.key_id=pk.key_id '||
4987   'and pk_col.column_id=key_usage.column_id '||
4988   'and pk_col.parent_object_id=src_table.elementid '||
4989   'and map_columns.mapping_id=map_properties.mapping_id '||
4990   'and map_columns.Source_usage_id=map_sources.source_usage_id '||
4991   'and map_columns.Source_column_id=pk_col.column_id '||
4992   'and map_columns.Target_column_id=tgt_column.column_id '||
4993   'and tgt_column.parent_object_id=tgt_relation.relation_id ';
4994   if g_debug then
4995     write_to_log_file_n('Going to execute '||l_stmt||' using '||l_mapping_id);
4996   end if;
4997   open cv for l_stmt using l_mapping_id,l_mapping_id;
4998   fetch cv into l_uk;
4999   close cv;
5000   l_pk:=l_uk||'_KEY';
5001   p_unique_key:=l_uk;
5002   p_pk_key:=l_pk;
5003   return true;
5004 Exception when others then
5005   g_status_message:=sqlerrm;
5006   write_to_log_file_n(g_status_message);
5007   g_status:=false;
5008   return false;
5009 End;
5010 
5011 function get_col_col_in_map(
5012 p_map_id number,
5013 p_object varchar2,
5014 p_src_tables out NOCOPY varcharTableType,
5015 p_src_cols out NOCOPY varcharTableType,
5016 p_tgt_tables out NOCOPY varcharTableType,
5017 p_tgt_cols out NOCOPY varcharTableType,
5018 p_number_cols out NOCOPY number) return boolean is
5019 l_stmt varchar2(10000);
5020 TYPE CurTyp IS REF CURSOR;
5021 cv   CurTyp;
5022 l_map_id number;
5023 Begin
5024   l_map_id:=p_map_id;
5025   if p_map_id is null then
5026     l_stmt:='select map.mapping_id '||
5027     'from edw_pvt_map_properties_md_v map, '||
5028     'edw_relations_md_v rel '||
5029     'where rel.relation_name=:a '||
5030     'and map.primary_target=rel.relation_id ';
5031     if g_debug then
5032       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object);
5033     end if;
5034     open cv for l_stmt using p_object;
5035     fetch cv into l_map_id;
5036     close cv;
5037   end if;
5038   l_stmt:='select '||
5039 	'src.relation_name, '||
5040 	'src_col.column_name, '||
5041 	'tgt.relation_name, '||
5042 	'tgt_col.column_name '||
5043 	'from '||
5044     'edw_relations_md_v src, '||
5045     'edw_relations_md_v tgt, '||
5046     'edw_pvt_columns_md_v src_col, '||
5047     'edw_pvt_columns_md_v tgt_col, '||
5048     'edw_pvt_map_properties_md_v map, '||
5049     'edw_pvt_map_sources_md_v map_source, '||
5050     '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns '||
5051     'where '||
5052     'map.mapping_id=:a '||
5053     'and map_source.mapping_id=map.mapping_id '||
5054     'and map_columns.mapping_id=map.mapping_id '||
5055     'and map_source.source_id=src.relation_id '||
5056     'and map_columns.source_usage_id=map_source.source_usage_id '||
5057     'and map.primary_target=tgt.relation_id '||
5058     'and src_col.column_id=map_columns.Source_column_id '||
5059     'and tgt_col.column_id=map_columns.target_column_id '||
5060     'and src_col.parent_object_id=src.relation_id '||
5061     'and tgt_col.parent_object_id=tgt.relation_id ';
5062   if g_debug then
5063     write_to_log_file_n('Going to execute '||l_stmt||' using '||l_map_id);
5064   end if;
5065   p_number_cols:=1;
5066   open cv for l_stmt using l_map_id,l_map_id;
5067   loop
5068     fetch cv into p_src_tables(p_number_cols),p_src_cols(p_number_cols),
5069     p_tgt_tables(p_number_cols),p_tgt_cols(p_number_cols);
5070     exit when cv%notfound;
5071     p_number_cols:=p_number_cols+1;
5072   end loop;
5073   p_number_cols:=p_number_cols-1;
5074   return true;
5075 Exception when others then
5076   g_status_message:=sqlerrm;
5077   write_to_log_file_n(g_status_message);
5078   g_status:=false;
5079   return false;
5080 End;
5081 
5082 --given a tgt, what are the fks used in the map
5083 function get_fks_in_map(p_tgt varchar2,p_src out NOCOPY varcharTableType,
5084 p_fk out NOCOPY varcharTableType,p_number_src out NOCOPY number) return boolean is
5085 l_stmt varchar2(4000);
5086 TYPE CurTyp IS REF CURSOR;
5087 cv   CurTyp;
5088 Begin
5089   l_stmt:='select '||
5090   'ltc.relation_name, '||
5091   'fk_item.column_name  '||
5092   'from  '||
5093   'edw_pvt_map_sources_md_v        ltc_ru,  '||
5094   'edw_relations_md_v               ltc,  '||
5095   'edw_pvt_map_key_usages_md_v      fk_usage, '||
5096   'edw_pvt_key_columns_md_v         fk_isu,  '||
5097   'edw_pvt_columns_md_v                   fk_item,  '||
5098   'edw_pvt_map_properties_md_v        map,  '||
5099   'edw_relations_md_v               tgt  '||
5100   'where  '||
5101   'tgt.relation_name=:a '||
5102   'and map.primary_target=tgt.relation_id '||
5103   'and ltc_ru.mapping_id=map.mapping_id '||
5104   'and ltc.relation_id=ltc_ru.source_id  '||
5105   'and fk_usage.source_usage_id=ltc_ru.source_usage_id '||
5106   'and fk_usage.mapping_id=map.mapping_id '||
5107   'and fk_isu.key_id=fk_usage.foreign_key_id '||
5108   'and fk_item.column_id=fk_isu.column_id '||
5109   'and fk_item.parent_object_id=ltc_ru.source_id';
5110   if g_debug then
5111     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_tgt);
5112   end if;
5113   p_number_src:=1;
5114   open cv for l_stmt using p_tgt;
5115   loop
5116     fetch cv into p_src(p_number_src),p_fk(p_number_src);
5117     exit when cv%notfound;
5118     p_number_src:=p_number_src+1;
5119   end loop;
5120   p_number_src:=p_number_src-1;
5121   close cv;
5122   return true;
5123 Exception when others then
5124   g_status_message:=sqlerrm;
5125   write_to_log_file_n(g_status_message);
5126   g_status:=false;
5127   return false;
5128 End;
5129 
5130 --given a tgt, what are the fks used in the dim
5131 function get_fks_in_dim(p_tgt varchar2,p_src out NOCOPY varcharTableType,
5132 p_fk out NOCOPY varcharTableType,p_number_src out NOCOPY number) return boolean is
5133 l_stmt varchar2(4000);
5134 TYPE CurTyp IS REF CURSOR;
5135 cv   CurTyp;
5136 Begin
5137   l_stmt:='select '||
5138   'rel.name,  '||
5139   'fk_item.column_name  '||
5140   'from  '||
5141   'edw_tables_md_v rel,  '||
5142   'edw_foreign_keys_md_v fk, '||
5143   'edw_pvt_key_columns_md_v fkisu,  '||
5144   'edw_pvt_columns_md_v fk_item,  '||
5145   'edw_dimensions_md_v dim,  '||
5146   'edw_levels_md_v lvl  '||
5147   'where  '||
5148   'dim.dim_name=:a  '||
5149   'and lvl.dim_id=dim.dim_id '||
5150   'and rel.name=lvl.level_name||''_LTC'' '||
5151   'and fk.entity_id=rel.elementid '||
5152   'and fkisu.key_id=fk.foreign_key_id '||
5153   'and fk_item.column_id=fkisu.column_id '||
5154   'and fk_item.parent_object_id=rel.elementid';
5155   if g_debug then
5156     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_tgt);
5157   end if;
5158   p_number_src:=1;
5159   open cv for l_stmt using p_tgt;
5160   loop
5161     fetch cv into p_src(p_number_src),p_fk(p_number_src);
5162     exit when cv%notfound;
5163     p_number_src:=p_number_src+1;
5164   end loop;
5165   p_number_src:=p_number_src-1;
5166   close cv;
5167   return true;
5168 Exception when others then
5169   g_status_message:=sqlerrm;
5170   write_to_log_file_n(g_status_message);
5171   g_status:=false;
5172   return false;
5173 End;
5174 
5175 function get_app_version(p_instance varchar2) return varchar2 is
5176 Begin
5177   return get_app_version(p_instance,null);
5178 Exception when others then
5179   g_status_message:=sqlerrm;
5180   write_to_conc_log_file(g_status_message);
5181   g_status:=false;
5182   return null;
5183 End;
5184 function get_app_version(p_instance varchar2,p_db_link varchar2) return varchar2 is
5185 l_stmt varchar2(4000);
5186 l_db_link varchar2(1000);
5187 l_release_name varchar2(200);
5188 l_version_name varchar2(200);
5189 TYPE CurTyp IS REF CURSOR;
5190 cv   CurTyp;
5191 Begin
5192   l_db_link:=p_db_link;
5193   if l_db_link is null and p_instance is not null then
5194     l_db_link:=get_db_link_for_instance(p_instance);
5195   end if;
5196   if l_db_link is null then
5197     l_stmt:='select release_name from fnd_product_groups';
5198   else
5199     l_stmt:='select release_name from fnd_product_groups@'||l_db_link;
5200   end if;
5201   if g_debug then
5202     write_to_conc_log_file(l_stmt);
5203   end if;
5204   open cv for l_stmt;
5205   fetch cv into l_release_name;
5206   close cv;
5207   l_version_name:=substr(l_release_name,1,instr(l_release_name,'.')+1);
5208   g_oracle_apps_version:=l_release_name;
5209   write_to_conc_log_file('l_release_name='||l_release_name);
5210   write_to_conc_log_file('l_version_name='||l_version_name);
5211   return l_version_name;
5212 Exception when others then
5213   write_to_conc_log_file(g_status_message);
5214   return null;
5215 End;
5216 
5217 function check_table_column(p_table varchar2,p_col varchar2) return boolean is
5218 l_stmt varchar2(1000);
5219 TYPE CurTyp IS REF CURSOR;
5220 cv   CurTyp;
5221 Begin
5222   if g_debug then
5223     write_to_log_file_n('Check column '||p_col||' in '||p_table);
5224   end if;
5225   begin
5226     l_stmt:='select '||p_col||' from '||p_table||' where rownum=1';
5227     open cv for l_stmt;
5228   exception when others then
5229     if g_debug then
5230       write_to_log_file('Not Found');
5231     end if;
5232     return false;
5233   end;
5234   if g_debug then
5235     write_to_log_file('Yes Found');
5236   end if;
5237   return true;
5238 Exception when others then
5239   g_status_message:=sqlerrm;
5240   write_to_log_file_n(g_status_message);
5241   g_status:=false;
5242   return false;
5243 End;
5244 
5245 function check_table_column(p_table varchar2,p_owner varchar2,p_col varchar2) return boolean is
5246 TYPE CurTyp IS REF CURSOR;
5247 cv   CurTyp;
5248 l_var number;
5249 l_owner varchar2(80);
5250 Begin
5251   if g_debug then
5252     write_to_log_file_n('Check column '||p_col||' in '||p_table);
5253   end if;
5254   l_owner:=p_owner;
5255   if l_owner is null then
5256     l_owner:=get_table_owner(p_table);
5257   end if;
5258   g_stmt:='select 1 from all_tab_columns where table_name=:1 and owner=:2 and column_name=:3';
5259   if g_debug then
5260     write_to_log_file_n(g_stmt||' '||p_table||' '||l_owner||' '||p_col);
5261   end if;
5262   open cv for g_stmt using p_table,l_owner,p_col;
5263   fetch cv into l_var;
5264   close cv;
5265   if l_var=1 then
5266     return true;
5267   else
5268     return false;
5269   end if;
5270   return true;
5271 Exception when others then
5272   g_status_message:=sqlerrm;
5273   write_to_log_file_n(g_status_message);
5274   g_status:=false;
5275   return false;
5276 End;
5277 
5278 function add_column_to_table(p_table varchar2,p_owner varchar2,p_col varchar2,p_datatype varchar2) return boolean is
5279 l_stmt varchar2(1000);
5280 l_owner varchar2(400);
5281 Begin
5282   l_owner:=p_owner;
5283   if l_owner is null then
5284     l_owner:=get_table_owner(p_table);
5285   end if;
5286   l_stmt:='alter table '||l_owner||'.'||p_table||' add('||p_col||' '||p_datatype||')';
5287   if g_debug then
5288     write_to_log_file_n(l_stmt||get_time);
5289   end if;
5290   execute immediate l_stmt;
5291   return true;
5292 Exception when others then
5293   g_status_message:=sqlerrm;
5294   write_to_log_file_n(g_status_message);
5295   g_status:=false;
5296   return false;
5297 End;
5298 
5299 function check_pk_pkkey_index(p_table varchar2,p_owner varchar2,p_pk varchar2,p_pk_key varchar2) return boolean is
5300 l_owner varchar2(200);
5301 index_found boolean:=false;
5302 l_index varcharTableType;
5303 l_ind_col varcharTableType;
5304 l_ind_col_pos numberTableType;
5305 l_number_index number;
5306 Begin
5307   l_owner:=p_owner;
5308   if l_owner is null then
5309     l_owner:=get_table_owner(p_table);
5310   end if;
5311   l_number_index:=0;
5312   index_found:=false;
5313   if g_debug then
5314     write_to_log_file_n('Check index for '||l_owner||'.'||p_table||'('||p_pk||','||p_pk_key||')');
5315   end if;
5316   if get_table_index_col(p_table,l_owner,l_index,l_ind_col,l_ind_col_pos,l_number_index)=true then
5317     for j in 1..l_number_index loop
5318       if l_ind_col(j)=p_pk and l_ind_col_pos(j)=1 then
5319         for k in 1..l_number_index loop
5320           if j<>k and l_index(k)=l_index(j) and l_ind_col(k)=p_pk_key then
5321             index_found:=true;
5322             exit;
5323           end if;
5324         end loop;
5325       end if;
5326       if index_found then
5327         exit;
5328       end if;
5329     end loop;
5330   end if;
5331   return index_found;
5332 Exception when others then
5333   g_status_message:=sqlerrm;
5334   write_to_log_file_n('Error in check_pk_pkkey_index '||g_status_message);
5335   return false;
5336 End;
5337 
5338 function check_load_status(p_object varchar2) return boolean is
5339 l_stmt varchar2(1000);
5340 TYPE CurTyp IS REF CURSOR;
5341 cv   CurTyp;
5342 l_date1 date;
5343 l_date2 date;
5344 Begin
5345   l_stmt:='select max(last_update_date) from edw_collection_detail_log where object_name=:a';
5346   if g_debug then
5347     write_to_log_file_n(l_stmt||' using '||p_object||get_time);
5348   end if;
5349   open cv for l_stmt using p_object;
5350   fetch cv into l_date1;
5351   close cv;
5352   l_stmt:='select max(last_update_date) from edw_collection_detail_log where object_name=:a '||
5353   'and collection_status=''ERROR''';
5354   if g_debug then
5355     write_to_log_file_n(l_stmt||' using '||p_object||get_time);
5356   end if;
5357   open cv for l_stmt using p_object;
5358   fetch cv into l_date2;
5359   close cv;
5360   if l_date1=l_date2 then
5361     return false;
5362   else
5363     return true;
5364   end if;
5365 Exception when others then
5366   g_status_message:=sqlerrm;
5367   write_to_log_file_n('Error in check_load_status '||g_status_message);
5368   return false;
5369 End;
5370 
5371 function get_message(p_message  varchar2) return varchar2 is
5372 Begin
5373   return get_message(p_message,null);
5374 Exception when others then
5375   g_status_message:=sqlerrm;
5376   write_to_log_file_n('Error in get_message '||g_status_message);
5377   return null;
5378 End;
5379 function get_message(p_message  varchar2,p_product varchar2) return varchar2 is
5380 l_product varchar2(200);
5381 Begin
5382   l_product:=p_product;
5383   if l_product is null then
5384     l_product:='BIS';
5385   end if;
5386   FND_MESSAGE.SET_NAME(l_product,p_message);
5387    return FND_MESSAGE.GET;
5388 Exception when others then
5389   g_status_message:=sqlerrm;
5390   write_to_log_file_n('Error in get_message '||g_status_message);
5391   return null;
5392 End;
5393 
5394 
5395 function get_object_type(p_object_name varchar2) return varchar2 is
5396 l_stmt varchar2(1000);
5397 TYPE CurTyp IS REF CURSOR;
5398 cv   CurTyp;
5399 l_res number;
5400 Begin
5401   l_stmt:='select 1 from edw_dimensions_md_v where dim_name=:a';
5402   open cv for l_stmt using p_object_name;
5403   fetch cv into l_res;
5404   close cv;
5405   if l_res=1 then
5406     return 'DIMENSION';
5407   end if;
5408   l_stmt:='select 1 from edw_facts_md_v where fact_name=:a';
5409   open cv for l_stmt using p_object_name;
5410   fetch cv into l_res;
5411   close cv;
5412   if l_res=1 then
5413     return 'FACT';
5414   end if;
5415   l_stmt:='select 1 from edw_tables_md_v where name=:a';
5416   open cv for l_stmt using p_object_name;
5417   fetch cv into l_res;
5418   close cv;
5419   if l_res=1 then
5420     return 'TABLE';
5421   end if;
5422   return 'UNKNOWN';
5423 Exception when others then
5424   g_status_message:=sqlerrm;
5425   write_to_log_file_n('Error in get_object_type '||g_status_message);
5426   return null;
5427 End;
5428 
5429 function get_ltc_lstg(p_object_name varchar2,p_lstg out NOCOPY varcharTableType,
5430 p_ltc out NOCOPY varcharTableType,p_number_ltc out NOCOPY number) return boolean is
5431 l_stmt varchar2(3000);
5432 TYPE CurTyp IS REF CURSOR;
5433 cv   CurTyp;
5434 Begin
5435   l_stmt:='select ltc.name,lstg.name '||
5436   'from  '||
5437   'edw_tables_md_v ltc,  '||
5438   'edw_tables_md_v lstg,  '||
5439   'edw_pvt_map_properties_md_v map,  '||
5440   'edw_dimensions_md_v dim,  '||
5441   'edw_levels_md_v lvl  '||
5442   'where dim.dim_name=:a  '||
5443   'and lvl.dim_id=dim.dim_id '||
5444   'and ltc.name=lvl.level_name||''_LTC'' '||
5445   'and map.primary_target(+)=ltc.elementid '||
5446   'and lstg.elementid(+)=map.primary_source';
5447   if g_debug then
5448     write_to_log_file_n(l_stmt||' using '||p_object_name||get_time);
5449   end if;
5450   p_number_ltc:=1;
5451   open cv for l_stmt using p_object_name;
5452   loop
5453     fetch cv into p_ltc(p_number_ltc),p_lstg(p_number_ltc);
5454     exit when cv%notfound;
5455     p_number_ltc:=p_number_ltc+1;
5456   end loop;
5457   p_number_ltc:=p_number_ltc-1;
5458   return true;
5459 Exception when others then
5460   g_status_message:=sqlerrm;
5461   write_to_log_file_n('Error in get_ltc_lstg '||g_status_message);
5462   return false;
5463 End;
5464 
5465 function is_auto_dang_implemented(p_dim_name varchar2) return boolean is
5466 l_dim_id number;
5467 l_is_name varchar2(40);
5468 Begin
5469   if g_read_cfig_options then
5470     l_dim_id:=get_dim_id(p_dim_name);
5471     if edw_option.get_warehouse_option(null,l_dim_id,'AUTODANG',l_is_name)=false then
5472       null;
5473     end if;
5474     if l_is_name='Y' then
5475       return true;
5476     end if;
5477   else
5478     if fnd_profile.value('EDW_AUTO_DANG_RECOVERY')='Y' then
5479       return true;
5480     end if;
5481   end if;
5482   return false;
5483 Exception when others then
5484   g_status_message:=sqlerrm;
5485   write_to_log_file_n('Error in is_auto_dang_implemented '||g_status_message);
5486   return false;
5487 End;
5488 
5489 function create_auto_dang_table(p_dim_auto_dang_table varchar2,
5490 p_pk_cols varcharTableType,p_number_pk_cols number) return boolean is
5491 l_stmt varchar2(3000);
5492 Begin
5493   l_stmt:='create table '||p_dim_auto_dang_table||'(level_table number,value varchar2(800)';
5494   for i in 1..p_number_pk_cols loop
5495     if p_pk_cols(i)<>'INST' then
5496       l_stmt:=l_stmt||','||p_pk_cols(i)||' varchar2(800) ';
5497     end if;
5498   end loop;
5499   l_stmt:=l_stmt||')';
5500   if g_debug then
5501     write_to_log_file_n(l_stmt||get_time);
5502   end if;
5503   execute immediate l_stmt;
5504   if instr(p_dim_auto_dang_table,'.')<>0 then
5505     --2881055
5506     begin
5507       l_stmt:='drop view '||substr(p_dim_auto_dang_table,instr(p_dim_auto_dang_table,'.')+1);
5508       if g_debug then
5509         write_to_log_file_n(l_stmt);
5510       end if;
5511       execute immediate l_stmt;
5512     exception when others then
5513       null;
5514     end;
5515     if create_synonym(substr(p_dim_auto_dang_table,instr(p_dim_auto_dang_table,'.')+1),
5516       p_dim_auto_dang_table)=false then
5517       null;
5518     end if;
5519   end if;
5520   return true;
5521 Exception when others then
5522   if sqlcode=-00955 then
5523     if g_debug then
5524       write_to_log_file_n('Table '||p_dim_auto_dang_table||' exists. Returning TRUE');
5525     end if;
5526     return true;
5527   end if;
5528   g_status_message:=sqlerrm;
5529   write_to_log_file_n('Error in create_auto_dang_table '||g_status_message);
5530   return false;
5531 End;
5532 
5533 function get_lowest_level_table(p_dim varchar2,p_lowest_level_table out NOCOPY varchar2,
5534 p_lowest_level_table_id out NOCOPY number) return boolean is
5535 l_stmt varchar2(3000);
5536 TYPE CurTyp IS REF CURSOR;
5537 cv   CurTyp;
5538 Begin
5539   l_stmt:='select ltc.name,ltc.elementid '||
5540   'from  '||
5541   'edw_levels_md_v lvl,  '||
5542   'edw_tables_md_v ltc,  '||
5543   'edw_dimensions_md_v dim  '||
5544   'where lvl.dim_id=dim.dim_id '||
5545   'and ltc.name=lvl.level_name||''_LTC'' '||
5546   'and dim.dim_name=:a '||
5547   'and not exists(  '||
5548   'select 1  from  '||
5549   'edw_pvt_level_relation_md_v lvl_rel,  '||
5550   'edw_hierarchies_md_v hier  '||
5551   'where  '||
5552   'lvl_rel.hierarchy_id=hier.hier_id '||
5553   'and hier.dim_id=dim.dim_id '||
5554   'and lvl_rel.parent_level_id=lvl.level_id) ';
5555   if g_debug then
5556     write_to_log_file_n(l_stmt||get_time);
5557   end if;
5558   open cv for l_stmt using p_dim;
5559   fetch cv into p_lowest_level_table,p_lowest_level_table_id;
5560   close cv;
5561   if g_debug then
5562     write_to_log_file_n('Result '||p_lowest_level_table||','||p_lowest_level_table_id);
5563   end if;
5564   return true;
5565 Exception when others then
5566   g_status_message:=sqlerrm;
5567   write_to_log_file_n('Error in get_lowest_level_table '||g_status_message);
5568   return false;
5569 End;
5570 
5571 function get_lowest_level_table(p_dim varchar2) return varchar2 is
5572 Begin
5573   return get_lowest_level_table(p_dim,null);
5574 Exception when others then
5575   g_status_message:=sqlerrm;
5576   write_to_log_file_n('Error in get_lowest_level_table '||g_status_message);
5577   return null;
5578 End;
5579 function get_lowest_level_table(p_dim varchar2,p_dim_id number) return varchar2 is
5580 l_stmt varchar2(3000);
5581 TYPE CurTyp IS REF CURSOR;
5582 cv   CurTyp;
5583 l_lowest_level varchar2(400);
5584 l_dim_id number;
5585 Begin
5586   l_dim_id:=p_dim_id;
5587   if l_dim_id is null then
5588     l_dim_id:=get_dim_id(p_dim);
5589   end if;
5590   l_stmt:='select ltc.name '||
5591   'from  '||
5592   'edw_levels_md_v lvl,  '||
5593   'edw_tables_md_v ltc,  '||
5594   'edw_dimensions_md_v dim  '||
5595   'where lvl.dim_id=dim.dim_id '||
5596   'and ltc.name=lvl.level_name||''_LTC'' '||
5597   'and dim.dim_name=:a '||
5598   'and not exists(  '||
5599   'select 1  from  '||
5600   'edw_pvt_level_relation_md_v lvl_rel,  '||
5601   'edw_hierarchies_md_v hier  '||
5602   'where  '||
5603   'lvl_rel.hierarchy_id=hier.hier_id '||
5604   'and hier.dim_id=dim.dim_id '||
5605   'and lvl_rel.parent_level_id=lvl.level_id) ';
5606   open cv for l_stmt using l_dim_id;
5607   fetch cv into l_lowest_level;
5608   close cv;
5609   return l_lowest_level;
5610 Exception when others then
5611   g_status_message:=sqlerrm;
5612   write_to_log_file_n('Error in get_lowest_level_table '||g_status_message);
5613   return null;
5614 End;
5615 
5616 function get_all_lowest_level_tables(
5617 p_dim_in varchar2,
5618 p_dim out NOCOPY varcharTableType,
5619 p_level_table out NOCOPY varcharTableType,
5620 p_level_table_id out NOCOPY numberTableType,
5621 p_number_dim out NOCOPY number) return boolean is
5622 l_stmt varchar2(3000);
5623 TYPE CurTyp IS REF CURSOR;
5624 cv   CurTyp;
5625 Begin
5626   p_number_dim:=0;
5627   l_stmt:='select dim.dim_name,ltc.elementid,ltc.name '||
5628   'from  '||
5629   'edw_levels_md_v lvl,  '||
5630   'edw_tables_md_v ltc,  '||
5631   'edw_dimensions_md_v dim  '||
5632   'where lvl.dim_id=dim.dim_id  '||
5633   'and ltc.name=lvl.level_name||''_LTC'' ';
5634   if p_dim_in is not null then
5635     l_stmt:=l_stmt||'and dim.dim_name in ('||p_dim_in||') ';
5636   end if;
5637   l_stmt:=l_stmt||'and not exists( '||
5638   'select 1  from  '||
5639   'edw_pvt_level_relation_md_v lvl_rel,  '||
5640   'edw_hierarchies_md_v hier  '||
5641   'where  '||
5642   'lvl_rel.hierarchy_id=hier.hier_id  '||
5643   'and hier.dim_id=dim.dim_id  '||
5644   'and lvl_rel.parent_level_id=lvl.level_id) ';
5645   if g_debug then
5646     write_to_log_file_n(l_stmt||get_time);
5647   end if;
5648   p_number_dim:=1;
5649   open cv for l_stmt;
5650   loop
5651     fetch cv into p_dim(p_number_dim),p_level_table_id(p_number_dim),p_level_table(p_number_dim);
5652     exit when cv%notfound;
5653     p_number_dim:=p_number_dim+1;
5654   end loop;
5655   close cv;
5656   p_number_dim:=p_number_dim-1;
5657   if g_debug then
5658     write_to_log_file_n('Result');
5659     for i in 1..p_number_dim loop
5660       write_to_log_file(p_dim(i)||','||p_level_table(i)||'('||p_level_table_id(i)||')');
5661     end loop;
5662   end if;
5663   return true;
5664 Exception when others then
5665   g_status_message:=sqlerrm;
5666   write_to_log_file_n('Error in get_all_lowest_level_tables '||g_status_message);
5667   return false;
5668 End;
5669 
5670 function get_dim_pk_structure(p_parent_table_name varchar2,p_instance varchar2,
5671 p_dim_pk_structure out NOCOPY varcharTableType,p_number_dim_pk_structure out NOCOPY number) return boolean is
5672 l_db_link varchar2(300);
5673 l_db_link_stmt varchar2(300);
5674 l_pk_structure varchar2(800);
5675 l_stmt varchar2(3000);
5676 TYPE CurTyp IS REF CURSOR;
5677 cv   CurTyp;
5678 l_profile_options varcharTableType;
5679 l_number_profile_options number;
5680 Begin
5681   p_number_dim_pk_structure:=0;
5682   l_db_link:=get_db_link_for_instance(p_instance);
5683   if l_db_link is not null then
5684     l_db_link_stmt:='@'||l_db_link;
5685     if test_db_link(l_db_link)=false then
5686       if g_debug then
5687         write_to_log_file_n(l_db_link||' not valid db link. Cannot parse PK');
5688         return true;
5689       end if;
5690     end if;
5691   else
5692     l_db_link_stmt:=null;
5693   end if;
5694   l_number_profile_options:=1;
5695   l_stmt:='select profile_option_name from fnd_profile_options'||l_db_link_stmt||' where profile_option_name '||
5696   ' like '''||p_parent_table_name||'_PS%''';
5697   if g_debug then
5698     write_to_log_file_n(l_stmt||get_time);
5699   end if;
5700   open cv for l_stmt;
5701   loop
5702     fetch cv into l_profile_options(l_number_profile_options);
5703     exit when cv%notfound;
5704     l_number_profile_options:=l_number_profile_options+1;
5705   end loop;
5706   close cv;
5707   l_number_profile_options:=l_number_profile_options-1;
5708   if g_debug then
5709     write_to_log_file_n('The profile options found'||get_time);
5710     for i in 1..l_number_profile_options loop
5711       write_to_log_file(l_profile_options(i));
5712     end loop;
5713   end if;
5714   for i in 1..l_number_profile_options loop
5715     l_pk_structure:=null;
5716     begin
5717       l_stmt:='select fnd_profile.value'||l_db_link_stmt||'('''||l_profile_options(i)||''') from dual';
5718       if g_debug then
5719         write_to_log_file_n(l_stmt);
5720       end if;
5721       open cv for l_stmt;
5722       fetch cv into l_pk_structure;
5723       close cv;
5724     exception when others then
5725       if g_debug then
5726         write_to_log_file_n(sqlerrm);
5727       end if;
5728       l_pk_structure:=null;
5729     end;
5730     if l_pk_structure is null then
5731       l_stmt:='select B.profile_option_value from fnd_profile_options'||l_db_link_stmt||' A, '||
5732       'fnd_profile_option_values'||l_db_link_stmt||' B '||
5733       'where A.profile_option_id=B.profile_option_id '||
5734       'and A.profile_option_name=:a';
5735       if g_debug then
5736         write_to_log_file_n(l_stmt);
5737       end if;
5738       open cv for l_stmt using l_profile_options(i);
5739       fetch cv into l_pk_structure;
5740       close cv;
5741     end if;
5742     p_number_dim_pk_structure:=p_number_dim_pk_structure+1;
5743     p_dim_pk_structure(p_number_dim_pk_structure):=l_pk_structure;
5744   end loop;
5745   return true;
5746 Exception when others then
5747   g_status_message:=sqlerrm;
5748   write_to_log_file_n('Error in get_dim_pk_structure '||g_status_message);
5749   return false;
5750 End;
5751 
5752 function get_db_link_for_instance(p_instance varchar2) return varchar2 is
5753 l_db_link varchar2(300);
5754 l_stmt varchar2(3000);
5755 TYPE CurTyp IS REF CURSOR;
5756 cv   CurTyp;
5757 Begin
5758   l_stmt:='select warehouse_to_instance_link from edw_source_instances where instance_code=:a';
5759   if g_debug then
5760     write_to_conc_log_file(l_stmt||get_time);
5761   end if;
5762   open cv for l_stmt using p_instance;
5763   fetch cv into l_db_link;
5764   close cv;
5765   return l_db_link;
5766 Exception when others then
5767   g_status_message:=sqlerrm;
5768   write_to_conc_log_file('Error in get_db_link_for_instance '||g_status_message);
5769   return null;
5770 End;
5771 
5772 function test_db_link(p_db_link varchar2) return boolean is
5773 l_stmt varchar2(3000);
5774 l_date date;
5775 TYPE CurTyp IS REF CURSOR;
5776 cv   CurTyp;
5777 Begin
5778   if g_debug then
5779     write_to_log_file_n('Check '||p_db_link);
5780   end if;
5781   l_stmt:='select sysdate from dual@'||p_db_link;
5782   if g_debug then
5783     write_to_log_file_n(l_stmt||get_time);
5784   end if;
5785   open cv for l_stmt;
5786   fetch cv into l_date;
5787   close cv;
5788   return true;
5789 Exception when others then
5790   write_to_log_file_n('Error in test_db_link '||sqlerrm);
5791   return false;
5792 End;
5793 
5794 
5795 function parse_pk_structure(p_dim_pk_structure varchar2,p_pk_cols out NOCOPY varcharTableType,
5796 p_number_pk_cols out NOCOPY number) return boolean is
5797 l_start number;
5798 l_end number;
5799 l_length number;
5800 l_col varchar2(200);
5801 Begin
5802   p_number_pk_cols:=0;
5803   if p_dim_pk_structure is null then
5804     return true;
5805   end if;
5806   l_start:=1;
5807   l_end:=1;
5808   l_length:=length(p_dim_pk_structure);
5809   loop
5810     l_end:=instr(p_dim_pk_structure,'-',l_start);
5811     if l_end=0 then
5812       l_end:=l_length+1;
5813     end if;
5814     l_col:=substr(p_dim_pk_structure,l_start,(l_end-l_start));
5815     p_number_pk_cols:=p_number_pk_cols+1;
5816     p_pk_cols(p_number_pk_cols):=l_col;
5817     if l_end>l_length then
5818       exit;
5819     end if;
5820     l_start:=l_end+1;
5821   end loop;
5822   if g_debug then
5823     write_to_log_file_n('The columns paresed from pk structure '||p_dim_pk_structure);
5824     for i in 1..p_number_pk_cols loop
5825       write_to_log_file(p_pk_cols(i));
5826     end loop;
5827   end if;
5828   return true;
5829 Exception when others then
5830   g_status_message:=sqlerrm;
5831   write_to_log_file_n('Error in parse_pk_structure '||g_status_message);
5832   return false;
5833 End;
5834 
5835 function get_dim_pk(p_dim_name varchar2) return varchar2 is
5836 Begin
5837   return get_dim_pk(p_dim_name,null);
5838 Exception when others then
5839   g_status_message:=sqlerrm;
5840   write_to_log_file_n('Error in get_dim_pk '||g_status_message);
5841   return null;
5842 End;
5843 function get_dim_pk(p_dim_name varchar2,p_dim_id number) return varchar2 is
5844 l_dim_id number;
5845 l_stmt varchar2(3000);
5846 TYPE CurTyp IS REF CURSOR;
5847 cv   CurTyp;
5848 l_pk varchar2(200);
5849 Begin
5850   l_dim_id:=p_dim_id;
5851   if l_dim_id is null then
5852     l_dim_id:=get_dim_id(p_dim_name);
5853   end if;
5854   l_stmt:='select pk_item.column_name '||
5855   'from '||
5856   'edw_unique_keys_md_v pk, '||
5857   'edw_pvt_key_columns_md_v isu, '||
5858   'edw_pvt_columns_md_v pk_item '||
5859   'where '||
5860   'pk.entity_id=:a '||
5861   'and pk.primarykey=1 '||
5862   'and isu.key_id=pk.key_id '||
5863   'and pk_item.column_id=isu.column_id '||
5864   'and pk_item.parent_object_id=pk.entity_id';
5865   if g_debug then
5866     write_to_log_file_n(l_stmt||' using '||l_dim_id);
5867   end if;
5868   open cv for l_stmt using l_dim_id;
5869   fetch cv into l_pk;
5870   close cv;
5871   return l_pk;
5872 Exception when others then
5873   g_status_message:=sqlerrm;
5874   write_to_log_file_n('Error in get_dim_pk '||g_status_message);
5875   return null;
5876 End;
5877 
5878 procedure truncate_table(p_table varchar2) is
5879 l_stmt varchar2(1000);
5880 l_owner varchar2(200);
5881 Begin
5882   l_owner:=get_table_owner(p_table);
5883   l_stmt:='truncate table '||l_owner||'.'||p_table;
5884   execute immediate l_stmt;
5885 Exception when others then
5886   write_to_log_file_n('Exception in truncate_table '||sqlerrm);
5887 End;
5888 
5889 function get_dim_lvl_pk_keys(p_dim_name varchar2,p_dim_id number,
5890 p_pk_key out NOCOPY varcharTableType,
5891 p_number_pk_key out NOCOPY number) return boolean is
5892 l_stmt varchar2(3000);
5893 TYPE CurTyp IS REF CURSOR;
5894 cv   CurTyp;
5895 l_dim_id number;
5896 Begin
5897   l_dim_id:=p_dim_id;
5898   p_number_pk_key:=0;
5899   if l_dim_id is null then
5900     l_dim_id:=get_dim_id(p_dim_name);
5901   end if;
5902   l_stmt:='select '||
5903   'lvl.level_prefix||''_''||pk_item.column_name '||
5904   'from  '||
5905   'edw_tables_md_v rel,  '||
5906   'edw_unique_keys_md_v pk,  '||
5907   'edw_pvt_key_columns_md_v isu,  '||
5908   'edw_pvt_columns_md_v pk_item,  '||
5909   'edw_levels_md_v lvl '||
5910   'where  '||
5911   ' lvl.dim_id=:a '||
5912   'and rel.name=lvl.level_name||''_LTC'' '||
5913   'and pk.entity_id=rel.elementid '||
5914   'and isu.key_id=pk.key_id '||
5915   'and pk_item.column_id=isu.column_id '||
5916   'and pk_item.data_type=''NUMBER'' '||
5917   'and pk_item.parent_object_id=rel.elementid';
5918   if g_debug then
5919     write_to_log_file_n(l_stmt||' using '||l_dim_id);
5920   end if;
5921   p_number_pk_key:=1;
5922   open cv for l_stmt using l_dim_id;
5923   loop
5924     fetch cv into p_pk_key(p_number_pk_key);
5925     exit when cv%notfound;
5926     p_number_pk_key:=p_number_pk_key+1;
5927   end loop;
5928   p_number_pk_key:=p_number_pk_key-1;
5929   close cv;
5930   return true;
5931 Exception when others then
5932   g_status_message:=sqlerrm;
5933   write_to_log_file_n('Error in get_dim_lvl_pk_keys '||g_status_message);
5934   return false;
5935 End;
5936 
5937 function get_dim_lvl_name_cols(p_dim_name varchar2,p_dim_id number,
5938 p_name out NOCOPY varcharTableType,
5939 p_number_name out NOCOPY number) return boolean is
5940 l_stmt varchar2(3000);
5941 TYPE CurTyp IS REF CURSOR;
5942 cv   CurTyp;
5943 l_dim_id number;
5944 Begin
5945   l_dim_id:=p_dim_id;
5946   p_number_name:=0;
5947   if l_dim_id is null then
5948     l_dim_id:=get_dim_id(p_dim_name);
5949   end if;
5950   l_stmt:='select '||
5951   'lvl.level_prefix||''_NAME'' '||
5952   'from '||
5953   'edw_levels_md_v lvl '||
5954   'where '||
5955   ' lvl.dim_id=:a ';
5956   if g_debug then
5957     write_to_log_file_n(l_stmt||' using '||l_dim_id);
5958   end if;
5959   p_number_name:=1;
5960   open cv for l_stmt using l_dim_id;
5961   loop
5962     fetch cv into p_name(p_number_name);
5963     exit when cv%notfound;
5964     p_number_name:=p_number_name+1;
5965   end loop;
5966   p_number_name:=p_number_name-1;
5967   close cv;
5968   return true;
5969 Exception when others then
5970   g_status_message:=sqlerrm;
5971   write_to_log_file_n('Error in get_dim_lvl_name_cols '||g_status_message);
5972   return false;
5973 End;
5974 
5975 function get_table_seq(p_table varchar2,p_table_id number) return varchar2 is
5976 l_seq varchar2(200);
5977 l_stmt varchar2(3000);
5978 TYPE CurTyp IS REF CURSOR;
5979 cv   CurTyp;
5980 l_table_id number;
5981 Begin
5982   l_table_id:=p_table_id;
5983   if l_table_id is null then
5984     l_table_id:=get_object_id(p_table);
5985   end if;
5986   l_stmt:='select '||
5987   'sec_relation.sequence_name  '||
5988   'from   '||
5989   'edw_pvt_map_sources_md_v  sec_relation_usage,   '||
5990   'edw_pvt_sequences_md_v  sec_relation,   '||
5991   'edw_pvt_map_properties_md_v map  '||
5992   'where   '||
5993   'map.primary_target=:a '||
5994   'and  sec_relation_usage.mapping_id=map.mapping_id '||
5995   'and sec_relation.sequence_id=sec_relation_usage.source_id';
5996   if g_debug then
5997     write_to_log_file_n(l_stmt||' using '||l_table_id);
5998   end if;
5999   open cv for l_stmt using l_table_id;
6000   fetch cv into l_seq;
6001   close cv;
6002   return l_seq;
6003 Exception when others then
6004   g_status_message:=sqlerrm;
6005   write_to_log_file_n('Error in get_table_seq '||g_status_message);
6006   return null;
6007 End;
6008 
6009 function get_lookup_code(p_lookup_type varchar2,p_lookup_code out NOCOPY varcharTableType,
6010 p_number_lookup_code out NOCOPY number) return boolean is
6011 l_stmt varchar2(4000);
6012 TYPE CurTyp IS REF CURSOR;
6013 cv   CurTyp;
6014 Begin
6015   l_stmt:='select lookup_code from fnd_common_lookups where lookup_type=:a';
6016   if g_debug then
6017     write_to_log_file_n(l_stmt||' using '||p_lookup_type);
6018   end if;
6019   p_number_lookup_code:=1;
6020   open cv for l_stmt using p_lookup_type;
6021   loop
6022     fetch cv into p_lookup_code(p_number_lookup_code);
6023     exit when cv%notfound;
6024     p_number_lookup_code:=p_number_lookup_code+1;
6025   end loop;
6026   p_number_lookup_code:=p_number_lookup_code-1;
6027   close cv;
6028   return true;
6029 Exception when others then
6030   g_status:=false;
6031   g_status_message:=sqlerrm;
6032   write_to_log_file_n(g_status_message);
6033   return false;
6034 End;
6035 
6036 function get_object_unique_key(p_object varchar2,p_object_id number,
6037 p_pk out NOCOPY varcharTableType,p_number_pk out NOCOPY number) return boolean is
6038 l_stmt varchar2(4000);
6039 TYPE CurTyp IS REF CURSOR;
6040 cv   CurTyp;
6041 l_object_id number;
6042 Begin
6043   l_object_id:=p_object_id;
6044   if l_object_id is null then
6045     l_object_id:=get_object_id(p_object);
6046   end if;
6047   l_stmt:='select pk_item.column_name '||
6048   'from '||
6049   'edw_unique_keys_md_v pk, '||
6050   'edw_pvt_key_columns_md_v isu, '||
6051   'edw_pvt_columns_md_v pk_item '||
6052   'where '||
6053   'pk.entity_id=:a '||
6054   'and isu.key_id=pk.key_id '||
6055   'and pk_item.column_id=isu.column_id '||
6056   'and pk_item.parent_object_id=pk.entity_id';
6057   if g_debug then
6058     write_to_log_file_n(l_stmt);
6059   end if;
6060   p_number_pk:=1;
6061   open cv for l_stmt using l_object_id;
6062   loop
6063     fetch cv into p_pk(p_number_pk);
6064     exit when cv%notfound;
6065     p_number_pk:=p_number_pk+1;
6066   end loop;
6067   p_number_pk:=p_number_pk-1;
6068   close cv;
6069   return true;
6070 Exception when others then
6071   g_status:=false;
6072   g_status_message:=sqlerrm;
6073   write_to_log_file_n(g_status_message);
6074   return false;
6075 End;
6076 
6077 function get_table_count_stats(p_table varchar2,p_owner varchar2) return number is
6078 l_stmt varchar2(4000);
6079 TYPE CurTyp IS REF CURSOR;
6080 cv   CurTyp;
6081 l_owner varchar2(200);
6082 l_res number;
6083 l_table varchar2(200);
6084 Begin
6085   l_owner:=upper(p_owner);
6086   l_table:=upper(p_table);
6087   if instr(l_table,'.')>0 then
6088     l_table:=substr(l_table,instr(l_table,'.')+1,length(l_table));
6089   end if;
6090   if l_owner is null then
6091     l_owner:=get_table_owner(l_table);
6092   end if;
6093   l_stmt:='select round(nvl(num_rows,0)) from all_tables where table_name=:a and owner=:b';
6094   open cv for l_stmt using l_table,l_owner;
6095   fetch cv into l_res;
6096   close cv;
6097   return l_res;
6098 Exception when others then
6099   write_to_log_file_n(sqlerrm);
6100   return -1;
6101 End;
6102 
6103 /*
6104 lstg to ltc mapping or
6105 fstg to fact mapping ONLY!!!
6106 this is the main function.
6107 this function does not consider the fk
6108 */
6109 function get_src_tgt_map_details(
6110 p_mapping_id number,
6111 p_primary_target number,
6112 p_primary_src number,
6113 p_factPKNameKey varchar2,
6114 p_dimTableName varcharTableType,
6115 p_numberOfDimTables number,
6116 p_fact_mapping_columns out NOCOPY varcharTableType,
6117 p_fstg_mapping_columns out NOCOPY varcharTableType,
6118 p_num_ff_map_cols out NOCOPY number,
6119 p_groupby_cols out NOCOPY varcharTableType,
6120 p_number_groupby_cols out NOCOPY number,
6121 p_instance_column out NOCOPY varchar2,
6122 p_groupby_on out NOCOPY boolean,
6123 p_pk_key_seq_pos out NOCOPY number,
6124 p_pk_key_seq out NOCOPY varchar2) return boolean is
6125 Begin
6126   if g_metedata_version is null then
6127     g_metedata_version:=find_metadata_version;
6128   end if;
6129   if g_metedata_version='EDW' then
6130     if get_src_tgt_map_details_edw(
6131       p_mapping_id,
6132       p_primary_target,
6133       p_primary_src,
6134       p_factPKNameKey ,
6135       p_dimTableName,
6136       p_numberOfDimTables,
6137       p_fact_mapping_columns,
6138       p_fstg_mapping_columns,
6139       p_num_ff_map_cols,
6140       p_groupby_cols,
6141       p_number_groupby_cols,
6142       p_instance_column,
6143       p_groupby_on,
6144       p_pk_key_seq_pos,
6145       p_pk_key_seq)=false then
6146       return false;
6147     end if;
6148   elsif g_metedata_version='OWB 2.1.1' or g_metedata_version='OWB 3i' then
6149     if get_src_tgt_map_details_owb(
6150       p_mapping_id,
6151       p_primary_target,
6152       p_primary_src,
6153       p_factPKNameKey ,
6154       p_dimTableName,
6155       p_numberOfDimTables,
6156       p_fact_mapping_columns,
6157       p_fstg_mapping_columns,
6158       p_num_ff_map_cols,
6159       p_groupby_cols,
6160       p_number_groupby_cols,
6161       p_instance_column,
6162       p_groupby_on,
6163       p_pk_key_seq_pos,
6164       p_pk_key_seq,
6165       g_metedata_version)=false then
6166       return false;
6167     end if;
6168   else
6169     write_to_log_file_n('Could not get metadata version');
6170     return false;
6171   end if;
6172   return true;
6173 Exception when others then
6174   g_status:=false;
6175   g_status_message:=sqlerrm;
6176   write_to_log_file_n(g_status_message);
6177   return false;
6178 End;
6179 
6180 /*
6181 lstg to ltc mapping or
6182 fstg to fact mapping
6183 for 2.1.1 owb metadata or 3i owb metadata
6184 */
6185 function get_src_tgt_map_details_owb(
6186 p_mapping_id number,
6187 p_primary_target number,
6188 p_primary_src number,
6189 p_factPKNameKey varchar2,
6190 p_dimTableName varcharTableType,
6191 p_numberOfDimTables number,
6192 p_fact_mapping_columns out NOCOPY varcharTableType,
6193 p_fstg_mapping_columns out NOCOPY varcharTableType,
6194 p_num_ff_map_cols out NOCOPY number,
6195 p_groupby_cols out NOCOPY varcharTableType,
6196 p_number_groupby_cols out NOCOPY number,
6197 p_instance_column out NOCOPY varchar2,
6198 p_groupby_on out NOCOPY boolean,
6199 p_pk_key_seq_pos out NOCOPY number,
6200 p_pk_key_seq out NOCOPY varchar2,
6201 p_metedata_version varchar2) return boolean is
6202 l_hold_func varcharTableType;
6203 l_hold_func_category varcharTableType;
6204 l_hold_item varcharTableType; --this is table.item
6205 l_hold_item_org varcharTableType; --this one is the pure col name
6206 l_hold_item_id numberTableType;
6207 l_hold_relation numberTableType;
6208 l_hold_item_usage numberTableType;
6209 l_hold_aggregatefunction varcharTableType;
6210 l_hold_relation_name varcharTableType;
6211 l_hold_relation_type varcharTableType;
6212 l_hold_relation_usage numberTableType;
6213 l_hold_func_usage numberTableType;
6214 l_hold_is_distinct numberTableType;
6215 l_hold_func_position numberTableType;
6216 l_hold_func_dvalue varcharTableType;
6217 l_count number:=1;
6218 l_func_id number;
6219 l_func_name varchar2(400);
6220 l_src_col_count numberTableType;
6221 l_found boolean:=false;
6222 Begin
6223   if g_debug then
6224     write_to_log_file_n('In get_src_tgt_map_details_owb');
6225   end if;
6226   p_num_ff_map_cols:=0;
6227   p_number_groupby_cols:=0;
6228   p_pk_key_seq_pos:=0;
6229   p_pk_key_seq:=null;
6230   p_instance_column:=null;
6231   p_groupby_on:=false;
6232   if get_mapping_details(
6233      p_mapping_id
6234     ,l_hold_func
6235     ,l_hold_func_category
6236     ,l_hold_item
6237     ,l_hold_item_id
6238     ,l_hold_item_usage
6239     ,l_hold_aggregatefunction
6240     ,l_hold_is_distinct
6241     ,l_hold_relation
6242     ,l_hold_relation_name
6243     ,l_hold_relation_usage
6244     ,l_hold_relation_type
6245     ,l_hold_func_usage
6246     ,l_hold_func_position
6247     ,l_hold_func_dvalue
6248     ,l_count
6249     ,p_metedata_version)=false then
6250     return false;
6251   end if;
6252   for i in 1..l_count loop
6253     l_hold_item_org(i):=l_hold_item(i);
6254   end loop;
6255   for i in 1..l_count loop
6256     if l_hold_relation(i)<>p_primary_target then
6257       if l_hold_aggregatefunction(i) is not null then
6258         p_groupby_on:=true;
6259         l_hold_item(i):=l_hold_relation_name(i)||'.'||l_hold_item(i);
6260         l_hold_item(i):=l_hold_aggregatefunction(i)||'('||l_hold_item(i)||')';
6261       else
6262         if value_in_table(p_dimTableName,p_numberOfDimTables,l_hold_relation_name(i))=false then
6263           p_number_groupby_cols:=p_number_groupby_cols+1;
6264           p_groupby_cols(p_number_groupby_cols):=l_hold_item(i);
6265           /*
6266             p_groupby_cols and p_groupby_stmt should be used only when p_groupby_on =true!!
6267           */
6268           l_hold_item(i):=l_hold_relation_name(i)||'.'||l_hold_item(i);
6269         end if;
6270       end if;
6271       --find the instance col
6272       if p_instance_column is null then
6273         if l_hold_relation(i)=p_primary_src then
6274           if l_hold_item_org(i)='INSTANCE' OR l_hold_item_org(i)='INSTANCE_CODE' then
6275             p_instance_column:= l_hold_item_org(i);
6276           end if;
6277         end if;
6278       end if;
6279     end if;
6280   end loop;
6281   if p_groupby_on then
6282     write_to_log_file_n('Group by on, the columns are ');
6283     for i in 1..p_number_groupby_cols loop
6284       write_to_log_file(p_groupby_cols(i));
6285     end loop;
6286   end if;
6287   p_pk_key_seq_pos:=0;
6288   p_pk_key_seq:=null;
6289   for i in 1..l_count loop
6290     if l_hold_relation(i)=p_primary_target then --this is the fact
6291       --if this is from the dim, skip this
6292       l_found:=false; --needed to see if this is a dim table. then skip...
6293       l_func_id:=l_hold_func_usage(i);--need to hold the func usage
6294       for j in 1..l_count loop
6295         if l_func_id=l_hold_func_usage(j) and l_hold_relation(j)<>p_primary_target then
6296           --if this is a secondary source, goto loopend\
6297           --this is imp here because the secondary sources are only considered for
6298           --key translation
6299           if value_in_table(p_dimTableName,p_numberOfDimTables,l_hold_relation_name(j)) then
6300             goto loopend;
6301           end if;
6302         end if;
6303       end loop;
6304       --now look at the proper cols
6305       p_num_ff_map_cols:=p_num_ff_map_cols+1;
6306       p_fact_mapping_columns(p_num_ff_map_cols):=l_hold_item(i);
6307       --now get the src
6308       l_func_id:=l_hold_func_usage(i);--need to hold the func usage
6309       l_func_name:=l_hold_func(i);
6310       l_src_col_count(p_num_ff_map_cols):=0;--how many src cols there are for this
6311       p_fstg_mapping_columns(p_num_ff_map_cols):=null; --was ''
6312       if l_func_name <> 'COPY' then
6313         if l_hold_func_category(i) <> 'Basic' and
6314            l_hold_func_category(i) <> 'Character' and
6315            l_hold_func_category(i) <> 'Conversion' and
6316            l_hold_func_category(i) <> 'Date' and
6317            l_hold_func_category(i) <> 'Numeric' and
6318            l_hold_func_category(i) <> 'EDW_STAND_ALONE' then
6319            p_fstg_mapping_columns(p_num_ff_map_cols):=l_hold_func_category(i)||'.'||l_func_name||'(';
6320         else
6321           p_fstg_mapping_columns(p_num_ff_map_cols):=l_func_name||'(';
6322         end if;
6323       end if;
6324       for j in 1..l_count loop
6325         --first get the mapping for the src table
6326         --if l_hold_func_usage(j)=l_func_id AND l_hold_relation(j)= p_primary_src then
6327         if l_hold_func_usage(j)=l_func_id AND l_hold_relation(j)<> p_primary_target then
6328           if p_fact_mapping_columns(p_num_ff_map_cols)=p_factPKNameKey then
6329             if l_hold_relation_type(j)='CMPWBSequence' or l_hold_relation_type(j)='SEQUENCE' then
6330               p_pk_key_seq_pos:=p_num_ff_map_cols;
6331               p_pk_key_seq:=l_hold_relation_name(j);
6332             end if;
6333           end if;
6334           l_src_col_count(p_num_ff_map_cols):=l_src_col_count(p_num_ff_map_cols)+1;
6335           if l_src_col_count(p_num_ff_map_cols)=1 then
6336             p_fstg_mapping_columns(p_num_ff_map_cols):=p_fstg_mapping_columns(p_num_ff_map_cols)||' '||
6337             l_hold_item(j);
6338           else
6339             if l_func_name <> 'COPY' then
6340               p_fstg_mapping_columns(p_num_ff_map_cols):=p_fstg_mapping_columns(p_num_ff_map_cols)||', '||l_hold_item(j);
6341             end if;
6342           end if;
6343         end if;
6344       end loop;
6345       if l_func_name <> 'COPY' then
6346         p_fstg_mapping_columns(p_num_ff_map_cols):=p_fstg_mapping_columns(p_num_ff_map_cols)||')';
6347       end if;
6348     end if;
6349     <<loopend>>
6350     null;
6351   end loop;
6352   return true;
6353 Exception when others then
6354   g_status:=false;
6355   g_status_message:=sqlerrm;
6356   write_to_log_file_n(g_status_message);
6357   return false;
6358 End;
6359 
6360 /*
6361 lstg to ltc mapping or
6362 fstg to fact mapping
6363 for EDW metadata
6364 */
6365 function get_src_tgt_map_details_edw(
6366 p_mapping_id number,
6367 p_primary_target number,
6368 p_primary_src number,
6369 p_factPKNameKey varchar2,
6370 p_dimTableName varcharTableType,
6371 p_numberOfDimTables number,
6372 p_fact_mapping_columns out NOCOPY varcharTableType,
6373 p_fstg_mapping_columns out NOCOPY varcharTableType,
6374 p_num_ff_map_cols out NOCOPY number,
6375 p_groupby_cols out NOCOPY varcharTableType,
6376 p_number_groupby_cols out NOCOPY number,
6377 p_instance_column out NOCOPY varchar2,
6378 p_groupby_on out NOCOPY boolean,
6379 p_pk_key_seq_pos out NOCOPY number,
6380 p_pk_key_seq out NOCOPY varchar2) return boolean is
6381 Begin
6382   if g_debug then
6383     write_to_log_file_n('In get_src_tgt_map_details_edw');
6384   end if;
6385   return true;
6386 Exception when others then
6387   g_status:=false;
6388   g_status_message:=sqlerrm;
6389   write_to_log_file_n(g_status_message);
6390   return false;
6391 End;
6392 
6393 function find_metadata_version return varchar2 is
6394 l_stmt varchar2(1000);
6395 TYPE CurTyp IS REF CURSOR;
6396 cv   CurTyp;
6397 l_view_text varchar2(10000);
6398 l_version varchar2(100);
6399 Begin
6400   l_stmt:='select text from user_views where view_name=:a';
6401   if g_debug then
6402     write_to_log_file_n(l_stmt);
6403   end if;
6404   open cv for l_stmt using 'EDW_DIMENSIONS_MD_V';
6405   fetch cv into l_view_text;
6406   close cv;
6407   l_view_text:=lower(l_view_text);
6408   if g_debug then
6409     write_to_log_file_n(l_view_text);
6410   end if;
6411   if instr(l_view_text,'cmpwbdimension_v')<>0 then
6412     l_version:='OWB 2.1.1';
6413   else
6414     l_version:='OWB 3i';
6415   end if;
6416   if g_debug then
6417     write_to_log_file_n('metadata version '||l_version);
6418   end if;
6419   return l_version;
6420 Exception when others then
6421   write_to_log_file_n(sqlerrm);
6422   return null;
6423 End;
6424 
6425 function get_metadata_version return varchar2 is
6426 Begin
6427   if g_metedata_version is null then
6428     g_metedata_version:=find_metadata_version;
6429   end if;
6430   return g_metedata_version;
6431 Exception when others then
6432   write_to_log_file_n(sqlerrm);
6433   return null;
6434 End;
6435 
6436 
6437 /*
6438 called from derived fact loading.
6439 */
6440 function get_derv_mapping_details(
6441 p_mapping_id number,
6442 p_src_object_id number,
6443 p_number_skip_cols number,
6444 p_skip_cols varcharTableType,
6445 p_fact_fks varcharTableType,
6446 p_number_fact_fks number,
6447 p_src_fks varcharTableType,
6448 p_number_src_fks number,
6449 p_fact_id number,
6450 p_src_object varchar2,
6451 p_temp_fact_name_temp in out NOCOPY varchar2,
6452 p_number_sec_sources out NOCOPY number,
6453 p_sec_sources out NOCOPY varcharTableType,
6454 p_sec_sources_alias out NOCOPY varcharTableType,
6455 p_number_sec_key out NOCOPY number,
6456 p_sec_sources_pk out NOCOPY varcharTableType,
6457 p_sec_sources_fk out NOCOPY varcharTableType,
6458 p_groupby_stmt out NOCOPY varchar2,
6459 p_hold_number out NOCOPY number,
6460 p_number_group_by_cols out NOCOPY number,
6461 p_hold_relation out NOCOPY varcharTableType,
6462 p_hold_item out NOCOPY varcharTableType,
6463 p_group_by_cols out NOCOPY varcharTableType,
6464 p_output_group_by_cols out NOCOPY varcharTableType,
6465 p_number_input_params out NOCOPY number,
6466 p_output_params out NOCOPY varcharTableType,
6467 p_input_params out NOCOPY varcharTableType,
6468 p_filter_stmt out NOCOPY varchar2
6469 ) return boolean is
6470 Begin
6471   if g_metedata_version is null then
6472     g_metedata_version:=find_metadata_version;
6473   end if;
6474   if g_metedata_version='EDW' then
6475     if get_derv_mapping_details_edw(
6476       p_mapping_id,
6477       p_src_object_id,
6478       p_number_skip_cols,
6479       p_skip_cols,
6480       p_fact_fks,
6481       p_number_fact_fks,
6482       p_src_fks,
6483       p_number_src_fks,
6484       p_fact_id,
6485       p_src_object,
6486       p_temp_fact_name_temp,
6487       p_number_sec_sources,
6488       p_sec_sources,
6489       p_sec_sources_alias,
6490       p_number_sec_key,
6491       p_sec_sources_pk,
6492       p_sec_sources_fk,
6493       p_groupby_stmt,
6494       p_hold_number,
6495       p_number_group_by_cols,
6496       p_hold_relation,
6497       p_hold_item,
6498       p_group_by_cols,
6499       p_output_group_by_cols,
6500       p_number_input_params,
6501       p_output_params,
6502       p_input_params,
6503       p_filter_stmt)=false then
6504       return false;
6505     end if;
6506   elsif g_metedata_version='OWB 2.1.1' or g_metedata_version='OWB 3i' then
6507     if get_derv_mapping_details_owb(
6508       p_mapping_id,
6509       p_src_object_id,
6510       p_number_skip_cols,
6511       p_skip_cols,
6512       p_fact_fks,
6513       p_number_fact_fks,
6514       p_src_fks,
6515       p_number_src_fks,
6516       p_fact_id,
6517       p_src_object,
6518       p_temp_fact_name_temp,
6519       p_number_sec_sources,
6520       p_sec_sources,
6521       p_sec_sources_alias,
6522       p_number_sec_key,
6523       p_sec_sources_pk,
6524       p_sec_sources_fk,
6525       p_groupby_stmt,
6526       p_hold_number,
6527       p_number_group_by_cols,
6528       p_hold_relation,
6529       p_hold_item,
6530       p_group_by_cols,
6531       p_output_group_by_cols,
6532       p_number_input_params,
6533       p_output_params,
6534       p_input_params,
6535       p_filter_stmt,
6536       g_metedata_version)=false then
6537       return false;
6538     end if;
6539   else
6540     write_to_log_file_n('Could not get metadata version');
6541     return false;
6542   end if;
6543   return true;
6544 Exception when others then
6545   g_status_message:=sqlerrm;
6546   write_to_log_file_n('Error in get_mapping_details(main) '||sqlerrm||' '||get_time);
6547   return false;
6548 End;
6549 
6550 function get_derv_mapping_details_owb(
6551 p_mapping_id number,
6552 p_src_object_id number,
6553 p_number_skip_cols number,
6554 p_skip_cols varcharTableType,
6555 p_fact_fks varcharTableType,
6556 p_number_fact_fks number,
6557 p_src_fks varcharTableType,
6558 p_number_src_fks number,
6559 p_fact_id number,
6560 p_src_object varchar2,
6561 p_temp_fact_name_temp in out NOCOPY varchar2,
6562 p_number_sec_sources out NOCOPY number,
6563 p_sec_sources out NOCOPY varcharTableType,
6564 p_sec_sources_alias out NOCOPY varcharTableType,
6565 p_number_sec_key out NOCOPY number,
6566 p_sec_sources_pk out NOCOPY varcharTableType,
6567 p_sec_sources_fk out NOCOPY varcharTableType,
6568 p_groupby_stmt out NOCOPY varchar2,
6569 p_hold_number out NOCOPY number,
6570 p_number_group_by_cols out NOCOPY number,
6571 p_hold_relation out NOCOPY varcharTableType,
6572 p_hold_item out NOCOPY varcharTableType,
6573 p_group_by_cols out NOCOPY varcharTableType,
6574 p_output_group_by_cols out NOCOPY varcharTableType,
6575 p_number_input_params out NOCOPY number,
6576 p_output_params out NOCOPY varcharTableType,
6577 p_input_params out NOCOPY varcharTableType,
6578 p_filter_stmt out NOCOPY varchar2,
6579 p_metedata_version varchar2
6580 ) return boolean is
6581 TYPE CurTyp IS REF CURSOR;
6582 cv   CurTyp;
6583 l_stmt varchar2(20000);
6584 l_hold_func varcharTableType;
6585 l_hold_func_category varcharTableType;
6586 l_hold_item varcharTableType;
6587 l_hold_item_id numberTableType;
6588 l_hold_item_is_fk booleanTableType;
6589 l_hold_relation numberTableType;
6590 l_hold_relation_usage numberTableType;
6591 l_hold_item_usage numberTableType;
6592 l_hold_aggregatefunction varcharTableType;
6593 l_hold_is_distinct numberTableType;
6594 l_hold_relation_name varcharTableType;
6595 l_hold_relation_type varcharTableType;
6596 l_hold_func_usage numberTableType;
6597 l_hold_func_position numberTableType;
6598 l_hold_func_dvalue varcharTableType;
6599 l_hold_number number;
6600 l_ip_aggregate  varcharTableType;
6601 l_ip_previous varchar2(400);
6602 l_ip_param_agg boolean:=false;
6603 l_ip_agg_func varchar2(400);
6604 l_sec_source  varcharTableType;
6605 l_sec_source_id  numberTableType;
6606 l_sec_source_child  varcharTableType;
6607 l_sec_source_child_id  numberTableType;
6608 l_pk   varcharTableType;
6609 l_fk   varcharTableType;
6610 l_sec_source_usage  numberTableType;
6611 l_sec_source_usage_name  varcharTableType;
6612 l_sec_source_child_usage  numberTableType;
6613 l_sec_source_number  number;
6614 l_sec_sources_child_alias  varcharTableType;
6615 l_sec_source_name_index  varcharTableType;
6616 l_sec_source_index  numberTableType;
6617 l_sec_source_index_number number;
6618 l_ind number;
6619 l_aggregation_value varchar2(400);
6620 --skipping items
6621 l_output_params varcharTableType;
6622 l_input_params varcharTableType;
6623 l_number_input_params number;
6624 begin
6625   if g_debug then
6626     write_to_log_file_n('In util.get_mapping_details_owb');
6627   end if;
6628   p_number_sec_sources:=0;
6629   p_number_sec_key:=0;
6630   p_groupby_stmt:=null;
6631   p_hold_number:=0;
6632   p_number_group_by_cols:=0;
6633   p_number_input_params:=0;
6634   p_filter_stmt:=null;
6635   l_hold_number:=1;
6636   if get_mapping_details(
6637      p_mapping_id
6638     ,l_hold_func
6639     ,l_hold_func_category
6640     ,l_hold_item
6641     ,l_hold_item_id
6642     ,l_hold_item_usage
6643     ,l_hold_aggregatefunction
6644     ,l_hold_is_distinct
6645     ,l_hold_relation
6646     ,l_hold_relation_name
6647     ,l_hold_relation_usage
6648     ,l_hold_relation_type
6649     ,l_hold_func_usage
6650     ,l_hold_func_position
6651     ,l_hold_func_dvalue
6652     ,l_hold_number
6653     ,p_metedata_version)=false then
6654     g_status_message:=g_status_message;
6655     return false;
6656   end if;
6657   if g_debug  then
6658     write_to_log_file_n('The result of get_mapping_details, l_hold_number is '||l_hold_number);
6659     write_to_log_file_n('l_hold_func l_hold_item l_hold_item_id l_hold_relation l_hold_item_usage
6660         l_hold_aggregatefunction l_hold_is_distinct l_hold_relation_name l_hold_func_usage');
6661     for i in 1..l_hold_number loop
6662       write_to_log_file(l_hold_func(i)||' '||l_hold_func_category(i)||' '||l_hold_item(i)||' '||
6663       l_hold_item_id(i)||' '||l_hold_relation(i)||' '||
6664       l_hold_item_usage(i)||' '||l_hold_aggregatefunction(i)||' '||l_hold_is_distinct(i)||' '||
6665       l_hold_relation_name(i)||' '||l_hold_relation_usage(i)||'  '||l_hold_func_usage(i)||' '||
6666       l_hold_func_position(i)||' '||l_hold_func_dvalue(i));
6667     end loop;
6668   end if;
6669   --first do this only for the base fact columns. for the sec sources, we will need to
6670   --find the aliases first
6671   for i in 1..l_hold_number loop
6672     l_hold_item_is_fk(i):=false;
6673     if l_hold_relation(i)=p_src_object_id then
6674       --we cannot have l_hold_relation(i)<>g_fact_id because secondary sources have aliasing issue
6675       l_hold_item(i):=l_hold_relation_name(i)||'.'||l_hold_item(i);
6676     end if;
6677   end loop;
6678   if get_sec_source_info(
6679     p_mapping_id,
6680     l_sec_source,
6681     l_sec_source_id ,
6682     l_sec_source_child ,
6683     l_sec_source_child_id,
6684     l_pk,
6685     l_fk,
6686     l_sec_source_usage,
6687     l_sec_source_usage_name,
6688     l_sec_source_child_usage,
6689     l_sec_source_number) = false then
6690     g_status_message:=g_status_message;
6691     return false;
6692   end if;
6693   p_number_sec_sources:=0;
6694   if l_sec_source_number>0 then
6695     --first generate the alias
6696     if g_debug then
6697       write_to_log_file_n('Going to generate alias');
6698     end if;
6699     for i in 1..l_sec_source_number loop
6700       p_number_sec_sources :=p_number_sec_sources +1;
6701       p_sec_sources(p_number_sec_sources):=l_sec_source(i);
6702       p_sec_sources_alias(p_number_sec_sources):=l_sec_source_usage_name(i);
6703     end loop;
6704     declare
6705       l_sec_number  varcharTableType;
6706       l_last_num number;
6707     begin
6708       for i in 1..l_sec_source_number loop
6709         l_sec_number(i):=-1;
6710       end loop;
6711       --logic to derive the name of the sec source
6712       for i in 1..l_sec_source_number loop
6713         if l_sec_number(i)=-1 then
6714           l_sec_number(i):=0;
6715         end if;
6716         l_last_num:=0;
6717         for j in 1..l_sec_source_number loop
6718           if i<>j and l_sec_source(i)=l_sec_source(j) and l_sec_source(i)=l_sec_source_usage_name(j)  then
6719             if l_sec_number(j)=-1 then
6720               l_last_num:=l_last_num+1;
6721               l_sec_number(j):=l_last_num;
6722             end if;
6723           end if;
6724         end loop;
6725       end loop;
6726       for i in 1..l_sec_source_number loop
6727         if l_sec_number(i) > 0 then
6728           p_sec_sources_alias(i):=p_sec_sources_alias(i)||'$'||l_sec_number(i);
6729         end if;
6730       end loop;
6731     exception when others then
6732       g_status_message:=sqlerrm;
6733       write_to_log_file_n(g_status_message);
6734       return false;
6735     end;
6736     --l_sec_sources_child_alias
6737     for i in 1..l_sec_source_number loop
6738       l_sec_sources_child_alias(i):=l_sec_source_child(i);
6739       for j in 1..l_sec_source_number loop
6740         if l_sec_source_usage(j)=l_sec_source_child_usage(i) then
6741           l_sec_sources_child_alias(i):=p_sec_sources_alias(j);
6742           exit;
6743         end if;
6744       end loop;
6745     end loop;
6746     if g_debug then
6747       write_to_log_file_n('The sec sources , its alias, and child alias');
6748       for i in 1..p_number_sec_sources loop
6749         write_to_log_file(p_sec_sources(i)||' '||p_sec_sources_alias(i)||' '||l_sec_sources_child_alias(i));
6750       end loop;
6751     end if;
6752     --assign the keys
6753     /*g_number_sec_key is actually=g_number_sec_sources. but keeping them separate for now*/
6754     p_number_sec_key:=0;
6755     for i in 1..l_sec_source_number loop
6756       p_number_sec_key:=p_number_sec_key+1;
6757       p_sec_sources_pk(p_number_sec_key):=p_sec_sources_alias(i)||'.'||l_pk(i);
6758       p_sec_sources_fk(p_number_sec_key):=l_sec_sources_child_alias(i)||'.'||l_fk(i);
6759     end loop;
6760     if g_debug then
6761       write_to_log_file_n('The secondary sources keys');
6762       for i in 1..p_number_sec_key loop
6763         write_to_log_file(p_sec_sources_pk(i)||' '||p_sec_sources_fk(i));
6764       end loop;
6765     end if;
6766     --assign the secondary source column maps
6767     for i in 1..l_hold_number loop
6768       l_ind:=0;
6769       if l_hold_relation(i)<> p_src_object_id and l_hold_relation(i)<> p_fact_id then
6770         for j in 1..p_number_sec_sources loop
6771           if l_sec_source_usage(j)=l_hold_relation_usage(i) then
6772             l_ind:=j;
6773             exit;
6774           end if;
6775         end loop;
6776         if l_ind>0 then
6777           l_hold_item(i):=p_sec_sources_alias(l_ind)||'.'||l_hold_item(i);
6778         else
6779           l_hold_item(i):=l_hold_relation_name(i)||'.'||l_hold_item(i);
6780         end if;
6781       end if;
6782     end loop;
6783     if g_debug then
6784       write_to_log_file_n('The secondary source col maps');
6785       for i in 1..l_hold_number loop
6786         if l_hold_relation(i)<> p_src_object_id and l_hold_relation(i)<> p_fact_id then
6787           write_to_log_file(l_hold_item(i));
6788         end if;
6789       end loop;
6790     end if;
6791   else
6792     p_temp_fact_name_temp:=p_src_object;
6793     if g_debug then
6794       write_to_log_file_n('No secondary sources');
6795     end if;
6796   end if;
6797   p_groupby_stmt:=null;
6798   p_hold_number:=l_hold_number;
6799   p_number_group_by_cols:=0;--no group by to start with unless common keys found
6800   --find the fks that are directly mapped from the base to the derived
6801   for i in 1..l_hold_number loop
6802     p_hold_relation(i):=l_hold_relation(i);
6803     p_hold_item(i):=l_hold_item(i);
6804     if l_hold_relation(i)=p_fact_id and l_hold_func(i) ='COPY' then
6805       for j in 1..l_hold_number loop
6806         if l_hold_func_usage(j) = l_hold_func_usage(i) AND l_hold_relation(j) <> p_fact_id then
6807           if is_src_fk(l_hold_item(j),p_src_fks,p_number_src_fks)= true then
6808             l_hold_item_is_fk(j):=true;
6809             p_number_group_by_cols:=p_number_group_by_cols+1;
6810             p_group_by_cols(p_number_group_by_cols):=l_hold_item(j);
6811           end if;
6812         end if;
6813       end loop;
6814     end if;
6815   end loop;
6816   for i in 1..p_number_group_by_cols loop
6817     for j in 1..l_hold_number loop
6818       if l_hold_item(j)=p_group_by_cols(i) and l_hold_relation(j)<>p_fact_id and l_hold_func(j) ='COPY' then
6819         for k in 1..l_hold_number loop
6820           if l_hold_func_usage(j) = l_hold_func_usage(k) AND l_hold_relation(k) = p_fact_id then
6821             p_output_group_by_cols(i):=l_hold_item(k);
6822             exit;
6823           end if;
6824         end loop;
6825         exit;
6826       end if;
6827     end loop;
6828   end loop;
6829   --make the funcs
6830   p_number_input_params:=0;
6831   for i in 1..l_hold_number loop
6832     if l_hold_relation(i)=p_fact_id then
6833       p_number_input_params:=p_number_input_params+1;
6834       p_output_params(p_number_input_params):=l_hold_item(i);
6835       --p_input_params_is_fk(p_number_input_params):=false; --make the input param not key by default
6836       if l_hold_func(i) = 'COPY' then
6837         for j in 1..l_hold_number loop
6838           if l_hold_func_usage(j) = l_hold_func_usage(i) AND l_hold_relation(j) <> p_fact_id then
6839             if l_hold_aggregatefunction(j) is not null then
6840               if l_hold_is_distinct(j)=1 then
6841                 l_hold_item(j):=l_hold_aggregatefunction(j)||'(DISTINCT('||l_hold_item(j)||'))';
6842               else
6843                 l_hold_item(j):=l_hold_aggregatefunction(j)||'('||l_hold_item(j)||')';
6844               end if;
6845             else
6846                --if this is a group by, do not sum
6847                if l_hold_item_is_fk(j)=false and value_in_table(p_group_by_cols,p_number_group_by_cols,
6848                  l_hold_item(j))=false then
6849                  l_hold_item(j):='SUM('||l_hold_item(j)||')';
6850                end if;
6851             end if;
6852             p_input_params(p_number_input_params):=l_hold_item(j);
6853             --if the cols is a fk, then we need group by this col
6854             exit;
6855           end if;
6856         end loop;
6857       else
6858         p_input_params(p_number_input_params):=make_transforms_rec(
6859             l_hold_func,
6860             l_hold_func_category,
6861             l_hold_item,
6862             l_hold_item_id,
6863             l_hold_item_is_fk,
6864             l_hold_relation,
6865             l_hold_relation_usage,
6866             l_hold_item_usage,
6867             l_hold_aggregatefunction,
6868             l_hold_is_distinct,
6869             l_hold_relation_name,
6870             l_hold_func_usage,
6871             l_hold_func_position,
6872             l_hold_func_dvalue,
6873             p_sec_sources,
6874             p_number_sec_sources,
6875             p_fact_id,
6876             p_src_object,
6877             l_hold_number,
6878             i,
6879             l_aggregation_value);
6880         if l_aggregation_value='AGGREGATION' then
6881           null;
6882         else
6883           p_input_params(p_number_input_params):='SUM('||p_input_params(p_number_input_params)||')';
6884         end if;
6885       end if;
6886     end if;
6887   end loop;
6888   --p_output_group_by_cols
6889   --i need to know what are the derived fact col corresponding to the base fact group by cols
6890   --p_output_group_by_cols
6891   --i need to know what are the derived fact col corresponding to the base fact group by cols
6892   --group by of cols is implemented as a transformation GROUP_BY .
6893   --bug 2564723. added or l_hold_func_category(i) = 'OPI_EDW_TRANSFORM_PKG') then
6894   for i in 1..l_hold_number loop
6895     if l_hold_relation(i)=p_fact_id and l_hold_func(i) = 'GROUP_BY' and
6896       (l_hold_func_category(i) like 'EDW_STAND_ALONE%'
6897       or l_hold_func_category(i) = 'OPI_EDW_TRANSFORM_PKG') then
6898       for j in 1..p_number_input_params loop
6899         if p_output_params(j)=l_hold_item(i) then
6900           p_number_group_by_cols:=p_number_group_by_cols+1;
6901           p_group_by_cols(p_number_group_by_cols):=p_input_params(j);
6902           p_output_group_by_cols(p_number_group_by_cols):=p_output_params(j);
6903           exit;
6904         end if;
6905       end loop;
6906     end if;
6907   end loop;
6908   if g_debug then
6909     write_to_log_file_n('The input and output groupby columns');
6910     for i in 1..p_number_group_by_cols loop
6911       write_to_log_file(p_group_by_cols(i)||'   '||p_output_group_by_cols(i));
6912     end loop;
6913   end if;
6914   if p_number_skip_cols>0 then
6915     if g_debug then
6916       write_to_log_file_n('The input and output params BEFORE SKIPPING');
6917       for i in 1..p_number_input_params loop
6918         write_to_log_file(p_input_params(i)||'         '||p_output_params(i));
6919       end loop;
6920     end if;
6921     --skip items
6922     l_number_input_params:=0;
6923     for i in 1..p_number_input_params loop
6924       if value_in_table(p_skip_cols,p_number_skip_cols,p_output_params(i))=false or
6925         value_in_table(p_fact_fks,p_number_fact_fks,p_output_params(i)) then
6926         l_number_input_params:=l_number_input_params+1;
6927         l_output_params(l_number_input_params):=p_output_params(i);
6928         l_input_params(l_number_input_params):=p_input_params(i);
6929       end if;
6930     end loop;
6931     p_number_input_params:=l_number_input_params;
6932     p_input_params:=l_input_params;
6933     p_output_params:=l_output_params;
6934   end if;
6935   if g_debug then
6936     write_to_log_file_n('The final input and output params');
6937     for i in 1..p_number_input_params loop
6938       write_to_log_file(p_input_params(i)||'         '||p_output_params(i));
6939     end loop;
6940   end if;
6941   --get filter
6942   --p_filter_stmt
6943   p_filter_stmt:=null;
6944   l_stmt:='select text from edw_pvt_map_properties_md_v where mapping_id=:a and text_type=''Filter''';
6945   if g_debug then
6946     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_mapping_id);
6947   end if;
6948   open cv for l_stmt using p_mapping_id;
6949   fetch cv into p_filter_stmt;
6950   close cv;
6951   if g_debug then
6952     write_to_log_file_n('p_filter_stmt='||p_filter_stmt);
6953   end if;
6954   return true;
6955 Exception when others then
6956   g_status_message:=sqlerrm;
6957   write_to_log_file_n('Error in get_mapping_details_owb '||sqlerrm||' '||get_time);
6958   return false;
6959 End;
6960 
6961 function get_derv_mapping_details_edw(
6962 p_mapping_id number,
6963 p_src_object_id number,
6964 p_number_skip_cols number,
6965 p_skip_cols varcharTableType,
6966 p_fact_fks varcharTableType,
6967 p_number_fact_fks number,
6968 p_src_fks varcharTableType,
6969 p_number_src_fks number,
6970 p_fact_id number,
6971 p_src_object varchar2,
6972 p_temp_fact_name_temp in out NOCOPY varchar2,
6973 p_number_sec_sources out NOCOPY number,
6974 p_sec_sources out NOCOPY varcharTableType,
6975 p_sec_sources_alias out NOCOPY varcharTableType,
6976 p_number_sec_key out NOCOPY number,
6977 p_sec_sources_pk out NOCOPY varcharTableType,
6978 p_sec_sources_fk out NOCOPY varcharTableType,
6979 p_groupby_stmt out NOCOPY varchar2,
6980 p_hold_number out NOCOPY number,
6981 p_number_group_by_cols out NOCOPY number,
6982 p_hold_relation out NOCOPY varcharTableType,
6983 p_hold_item out NOCOPY varcharTableType,
6984 p_group_by_cols out NOCOPY varcharTableType,
6985 p_output_group_by_cols out NOCOPY varcharTableType,
6986 p_number_input_params out NOCOPY number,
6987 p_output_params out NOCOPY varcharTableType,
6988 p_input_params out NOCOPY varcharTableType,
6989 p_filter_stmt out NOCOPY varchar2
6990 ) return boolean is
6991 Begin
6992   return true;
6993 Exception when others then
6994   g_status_message:=sqlerrm;
6995   write_to_log_file_n('Error in get_mapping_details_edw '||sqlerrm||' '||get_time);
6996   return false;
6997 End;
6998 
6999 function is_src_fk(p_fk varchar2,p_src_fks varcharTableType,p_number_src_fks number) return boolean is
7000 l_fk varchar2(400);
7001 begin
7002   if g_debug then
7003     write_to_log_file_n('in is_src_fk,p_fk='||p_fk);
7004   end if;
7005   --if the fk is abc.xyz then parse the xyz out
7006   if instr(p_fk,'.') <> 0 then
7007     l_fk:=substr(p_fk,instr(p_fk,'.')+1,length(p_fk));
7008   else
7009     l_fk:=p_fk;
7010   end if;
7011   if g_debug then
7012     write_to_log_file('l_fk='||l_fk);
7013   end if;
7014   for i in 1..p_number_src_fks loop
7015     if l_fk=p_src_fks(i) then
7016       return true;
7017     end if;
7018   end loop;
7019   return false;
7020 Exception when others then
7021   g_status_message:=sqlerrm;
7022   write_to_log_file_n('Error in is_src_fk for '||p_fk||' '||sqlerrm||get_time);
7023   return false;
7024 End;
7025 
7026 function get_dim_hier_levels(p_dim_name varchar2,
7027 p_hier out NOCOPY varcharTableType,
7028 p_parent_ltc out NOCOPY varcharTableType,
7029 p_parent_ltc_id out NOCOPY numberTableType,
7030 p_child_ltc out NOCOPY varcharTableType,
7031 p_child_ltc_id out NOCOPY numberTableType,
7032 p_number_hier out NOCOPY number) return boolean is
7033 l_stmt varchar2(4000);
7034 TYPE CurTyp IS REF CURSOR;
7035 cv   CurTyp;
7036 Begin
7037   p_number_hier:=1;
7038   l_stmt:='select '||
7039   'parent_ltc.name,  '||
7040   'parent_ltc.elementid, '||
7041   'child_ltc.name,  '||
7042   'child_ltc.elementid, '||
7043   'hier.HIER_NAME '||
7044   'from  '||
7045   'edw_pvt_level_relation_md_v lvl_rel,  '||
7046   'edw_hierarchies_md_v hier,  '||
7047   'edw_dimensions_md_v dim,  '||
7048   'edw_levels_md_v child_level,  '||
7049   'edw_levels_md_v parent_level,  '||
7050   'edw_tables_md_v parent_ltc,  '||
7051   'edw_tables_md_v child_ltc  '||
7052   'where  '||
7053   'dim.dim_name=:a '||
7054   'and hier.DIM_ID=dim.DIM_ID  '||
7055   'and lvl_rel.HIERARCHY_ID=hier.HIER_ID '||
7056   'and child_level.LEVEL_ID=lvl_rel.CHILD_LEVEL_ID  '||
7057   'and parent_level.LEVEL_ID=lvl_rel.PARENT_LEVEL_ID  '||
7058   'and parent_ltc.name=parent_level.LEVEL_NAME||''_LTC'' '||
7059   'and child_ltc.name=child_level.LEVEL_NAME||''_LTC'' ';
7060   if g_debug then
7061     write_to_log_file_n(l_stmt);
7062   end if;
7063   open cv for l_stmt using p_dim_name;
7064   loop
7065     fetch cv into p_parent_ltc(p_number_hier),p_parent_ltc_id(p_number_hier),
7066     p_child_ltc(p_number_hier),p_child_ltc_id(p_number_hier),p_hier(p_number_hier);
7067     exit when cv%notfound;
7068     p_number_hier:=p_number_hier+1;
7069   end loop;
7070   p_number_hier:=p_number_hier-1;
7071   close cv;
7072   return true;
7073 Exception when others then
7074   g_status:=false;
7075   g_status_message:=sqlerrm;
7076   write_to_log_file_n(g_status_message);
7077   return false;
7078 End;
7079 
7080 function get_target_map(p_object_id number,p_object_name varchar2) return number is
7081 l_stmt varchar2(4000);
7082 TYPE CurTyp IS REF CURSOR;
7083 cv   CurTyp;
7084 l_mapping_id number;
7085 Begin
7086   if p_object_name is null then
7087     l_stmt:='select mapping_id from edw_pvt_map_properties_md_v where primary_target=:a';
7088     if g_debug then
7089       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_id);
7090     end if;
7091     open cv for l_stmt using p_object_id;
7092     fetch cv into l_mapping_id;
7093     close cv;
7094   else
7095     l_stmt:='select map.mapping_id from edw_pvt_map_properties_md_v map,edw_relations_md_v rel where '||
7096     'rel.relation_name=:a and map.primary_target=rel.relation_id ';
7097     if g_debug then
7098       write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_name);
7099     end if;
7100     open cv for l_stmt using p_object_name;
7101     fetch cv into l_mapping_id;
7102     close cv;
7103   end if;
7104   if g_debug then
7105     write_to_log_file_n('l_mapping_id='||l_mapping_id);
7106   end if;
7107   return l_mapping_id;
7108 Exception when others then
7109   g_status_message:=sqlerrm;
7110   write_to_log_file_n(g_status_message);
7111   return -1;
7112 End;
7113 
7114 function get_last_analyzed_date(p_table varchar2) return date is
7115 Begin
7116   return get_last_analyzed_date(p_table,null);
7117 Exception when others then
7118   g_status_message:=sqlerrm;
7119   write_to_log_file_n(g_status_message);
7120   return null;
7121 End;
7122 function get_last_analyzed_date(p_table varchar2, p_owner varchar2) return date is
7123 l_stmt varchar2(1000);
7124 TYPE CurTyp IS REF CURSOR;
7125 cv   CurTyp;
7126 l_owner varchar2(200);
7127 l_date date;
7128 Begin
7129   l_owner:=p_owner;
7130   if l_owner is null then
7131     l_owner:=get_table_owner(p_table);
7132   end if;
7133   l_stmt:='select last_analyzed from all_tables where table_name=:1 and owner=:2';
7134   if g_debug then
7135     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_table||','||l_owner);
7136   end if;
7137   open cv for l_stmt using p_table,l_owner;
7138   fetch cv into l_date;
7139   close cv;
7140   return l_date;
7141 Exception when others then
7142   g_status_message:=sqlerrm;
7143   write_to_log_file_n(g_status_message);
7144   return null;
7145 End;
7146 
7147 function create_load_input_table(
7148   p_table_name varchar2,
7149   p_object_name varchar2,
7150   p_mapping_id number,
7151   p_map_type varchar2,
7152   p_primary_src number,
7153   p_primary_target number,
7154   p_primary_target_name varchar2,
7155   p_object_type varchar2,
7156   p_conc_id number,
7157   p_conc_program_name varchar2,
7158   p_fact_audit boolean,
7159   p_net_change boolean,
7160   p_fact_audit_name varchar2,
7161   p_net_change_name varchar2,
7162   p_fact_audit_is_name varchar2,
7163   p_net_change_is_name varchar2,
7164   p_debug boolean,
7165   p_duplicate_collect boolean,
7166   p_execute_flag boolean,
7167   p_request_id number,
7168   p_collection_size number,
7169   p_parallel number,
7170   p_table_owner varchar2,
7171   p_bis_owner  varchar2,
7172   p_temp_log boolean,
7173   p_forall_size number,
7174   p_update_type varchar2,
7175   p_mode varchar2,
7176   p_explain_plan_check boolean,
7177   p_fact_dlog varchar2,
7178   p_key_set number,
7179   p_instance_type varchar2,
7180   p_load_pk number,
7181   p_skip_cols EDW_OWB_COLLECTION_UTIL.varcharTableType,
7182   p_number_skip_cols number,
7183   p_fresh_restart boolean,
7184   p_op_table_space varchar2,
7185   p_da_cols EDW_OWB_COLLECTION_UTIL.varcharTableType,
7186   p_number_da_cols number,
7187   p_da_table varchar2,
7188   p_pp_table varchar2,
7189   p_master_instance varchar2,
7190   p_rollback varchar2,
7191   p_skip_levels EDW_OWB_COLLECTION_UTIL.varcharTableType,
7192   p_number_skip_levels number,
7193   p_smart_update boolean,
7194   p_fk_use_nl number,
7195   p_fact_smart_update number,
7196   p_auto_dang_table_extn varchar2,
7197   p_log_dang_keys boolean,
7198   p_create_parent_table_records boolean,
7199   p_smart_update_cols EDW_OWB_COLLECTION_UTIL.varcharTableType,
7200   p_number_smart_update_cols number,
7201   p_check_fk_change boolean,
7202   p_stg_join_nl_percentage number,
7203   p_ok_switch_update number,
7204   p_stg_make_copy_percentage number,
7205   p_ok_table varchar2,
7206   p_hash_area_size number,
7207   p_sort_area_size number,
7208   p_trace boolean,
7209   p_read_cfig_options boolean,
7210   p_job_status_table varchar2,
7211   p_max_round number,
7212   p_update_dlog_lookup_table varchar2,
7213   p_dlog_has_data boolean,
7214   p_sleep_time number,
7215   p_parallel_drill_down boolean
7216   ) return boolean is
7217 l_fact_audit varchar2(2);
7218 l_net_change varchar2(2);
7219 l_debug varchar2(2);
7220 l_duplicate_collect varchar2(2);
7221 l_execute_flag varchar2(2);
7222 l_temp_log varchar2(2);
7223 l_explain_plan_check varchar2(2);
7224 l_fresh_restart varchar2(2);
7225 l_smart_update varchar2(2);
7226 l_log_dang_keys varchar2(2);
7227 l_create_parent_table_records varchar2(2);
7228 l_check_fk_change varchar2(2);
7229 l_dlog_has_data varchar2(2);
7230 l_skip_cols_table varchar2(80);
7231 l_skip_levels_table varchar2(80);
7232 l_smart_update_table varchar2(80);
7233 l_trace varchar2(2);
7234 l_read_cfig_options varchar2(2);
7235 l_da_cols_table varchar2(80);
7236 l_parallel_drill_down varchar2(10);
7237 Begin
7238   if g_debug then
7239     write_to_log_file_n('In create_load_input_table ');
7240   end if;
7241   l_skip_cols_table:=p_table_name||'_SC';
7242   l_skip_levels_table:=p_table_name||'_SL';
7243   l_smart_update_table:=p_table_name||'_SU';
7244   l_da_cols_table:=p_table_name||'_DC';
7245   if drop_table(p_table_name)=false then
7246     null;
7247   end if;
7248   if drop_table(l_skip_cols_table)=false then
7249     null;
7250   end if;
7251   if drop_table(l_skip_levels_table)=false then
7252     null;
7253   end if;
7254   if drop_table(l_smart_update_table)=false then
7255     null;
7256   end if;
7257   if drop_table(l_da_cols_table)=false then
7258     null;
7259   end if;
7260   g_stmt:='create table '||p_table_name||'('||
7261   'object_name varchar2(80),'||
7262   'mapping_id number,'||
7263   'map_type varchar2(80),'||
7264   'primary_src number,'||
7265   'primary_target number,'||
7266   'primary_target_name varchar2(80),'||
7267   'object_type varchar2(80),'||
7268   'conc_id number,'||
7269   'conc_program_name varchar2(80),'||
7270   'fact_audit varchar2(2),'||
7271   'net_change varchar2(2),'||
7272   'fact_audit_name varchar2(80),'||
7273   'net_change_name varchar2(80),'||
7274   'fact_audit_is_name varchar2(80),'||
7275   'net_change_is_name varchar2(80),'||
7276   'debug varchar2(2),'||
7277   'duplicate_collect varchar2(2),'||
7278   'execute_flag varchar2(2),'||
7279   'request_id number,'||
7280   'collection_size number,'||
7281   'parallel number,'||
7282   'table_owner varchar2(80),'||
7283   'bis_owner  varchar2(80),'||
7284   'temp_log varchar2(2),'||
7285   'forall_size number,'||
7286   'update_type varchar2(80),'||
7287   'p_mode varchar2(80),'||
7288   'explain_plan_check varchar2(2),'||
7289   'fact_dlog varchar2(80),'||
7290   'key_set number,'||
7291   'instance_type varchar2(80),'||
7292   'load_pk number,'||
7293   'fresh_restart varchar2(2),'||
7294   'op_table_space varchar2(80),'||
7295   'da_table varchar2(80),'||
7296   'pp_table varchar2(80),'||
7297   'master_instance varchar2(80),'||
7298   'rollback varchar2(80),'||
7299   'smart_update varchar2(2),'||
7300   'fk_use_nl number,'||
7301   'fact_smart_update number,'||
7302   'auto_dang_table_extn varchar2(80),'||
7303   'log_dang_keys varchar2(2),'||
7304   'create_parent_table_records varchar2(2),'||
7305   'check_fk_change varchar2(2),'||
7306   'stg_join_nl_percentage number,'||
7307   'ok_switch_update number,'||
7308   'stg_make_copy_percentage number,'||
7309   'ok_table varchar2(80),'||
7310   'hash_area_size number,'||
7311   'sort_area_size number,'||
7312   'trace_mode varchar2(2),'||
7313   'read_cfig_options varchar2(2),'||
7314   'job_status_table varchar2(80),'||
7315   'max_round number,'||
7316   'update_dlog_lookup_table varchar2(80),'||
7317   'dlog_has_data varchar2(2),'||
7318   'total_records number,'||
7319   'stg_copy_table_flag varchar2(10),'||
7320   'sleep_time number,'||
7321   'parallel_drill_down varchar2(10)'||
7322   ') tablespace '||p_op_table_space;
7323   if g_debug then
7324     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7325   end if;
7326   execute immediate g_stmt;
7327   l_fact_audit:='N';
7328   l_net_change:='N';
7329   l_debug:='N';
7330   l_duplicate_collect:='N';
7331   l_execute_flag:='N';
7332   l_temp_log:='N';
7333   l_explain_plan_check:='N';
7334   l_fresh_restart:='N';
7335   l_smart_update:='N';
7336   l_log_dang_keys:='N';
7337   l_create_parent_table_records:='N';
7338   l_check_fk_change:='N';
7339   l_trace:='N';
7340   l_read_cfig_options:='N';
7341   l_dlog_has_data:='N';
7342   l_parallel_drill_down:='N';
7343   if p_fact_audit then
7344     l_fact_audit:='Y';
7345   end if;
7346   if p_net_change then
7347     l_net_change:='Y';
7348   end if;
7349   if p_debug then
7350     l_debug:='Y';
7351   end if;
7352   if p_duplicate_collect then
7353     l_duplicate_collect:='Y';
7354   end if;
7355   if p_execute_flag then
7356     l_execute_flag:='Y';
7357   end if;
7358   if p_temp_log then
7359     l_temp_log:='Y';
7360   end if;
7361   if p_explain_plan_check then
7362     l_explain_plan_check:='Y';
7363   end if;
7364   if p_fresh_restart then
7365     l_fresh_restart:='Y';
7366   end if;
7367   if p_smart_update then
7368     l_smart_update:='Y';
7369   end if;
7370   if p_log_dang_keys then
7371     l_log_dang_keys:='Y';
7372   end if;
7373   if p_create_parent_table_records then
7374     l_create_parent_table_records:='Y';
7375   end if;
7376   if p_check_fk_change then
7377     l_check_fk_change:='Y';
7378   end if;
7379   if p_trace then
7380     l_trace:='Y';
7381   end if;
7382   if p_read_cfig_options then
7383     l_read_cfig_options:='Y';
7384   end if;
7385   if p_dlog_has_data then
7386     l_dlog_has_data:='Y';
7387   end if;
7388   if p_parallel_drill_down then
7389     l_parallel_drill_down:='Y';
7390   end if;
7391   g_stmt:='insert into '||p_table_name||'('||
7392   'object_name,'||
7393   'mapping_id,'||
7394   'map_type,'||
7395   'primary_src ,'||
7396   'primary_target ,'||
7397   'primary_target_name,'||
7398   'object_type,'||
7399   'conc_id ,'||
7400   'conc_program_name,'||
7401   'fact_audit,'||
7402   'net_change,'||
7403   'fact_audit_name,'||
7404   'net_change_name,'||
7405   'fact_audit_is_name,'||
7406   'net_change_is_name,'||
7407   'debug,'||
7408   'duplicate_collect,'||
7409   'execute_flag,'||
7410   'request_id ,'||
7411   'collection_size ,'||
7412   'parallel ,'||
7413   'table_owner,'||
7414   'bis_owner ,'||
7415   'temp_log,'||
7416   'forall_size ,'||
7417   'update_type,'||
7418   'p_mode,'||
7419   'explain_plan_check,'||
7420   'fact_dlog,'||
7421   'key_set ,'||
7422   'instance_type,'||
7423   'load_pk ,'||
7424   'fresh_restart,'||
7425   'op_table_space,'||
7426   'da_table,'||
7427   'pp_table,'||
7428   'master_instance,'||
7429   'rollback,'||
7430   'smart_update,'||
7431   'fk_use_nl ,'||
7432   'fact_smart_update ,'||
7433   'auto_dang_table_extn,'||
7434   'log_dang_keys,'||
7435   'create_parent_table_records,'||
7436   'check_fk_change,'||
7437   'stg_join_nl_percentage ,'||
7438   'ok_switch_update ,'||
7439   'stg_make_copy_percentage,'||
7440   'ok_table, '||
7441   'hash_area_size,'||
7442   'sort_area_size,'||
7443   'trace_mode,'||
7444   'read_cfig_options, '||
7445   'job_status_table, '||
7446   'max_round,'||
7447   'update_dlog_lookup_table,'||
7448   'dlog_has_data,'||
7449   'sleep_time,'||
7450   'parallel_drill_down'||
7451   ') values ('||
7452   ':1,:2,:3,:4,:5,:6,:7,:8,:9,:10,'||
7453   ':11,:12,:13,:14,:15,:16,:17,:18,:19,:20,'||
7454   ':21,:22,:23,:24,:25,:26,:27,:28,:29,:30,'||
7455   ':31,:32,:33,:34,:35,:36,:37,:38,:39,:40,'||
7456   ':41,:42,:43,:44,:45,:46,:47,:48,:49,:50,'||
7457   ':51,:52,:53,:54,:55,:56,:57,:58,:59)';
7458   if g_debug then
7459     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7460   end if;
7461   execute immediate g_stmt using
7462   p_object_name ,
7463   p_mapping_id ,
7464   p_map_type ,
7465   p_primary_src ,
7466   p_primary_target ,
7467   p_primary_target_name ,
7468   p_object_type ,
7469   p_conc_id ,
7470   p_conc_program_name ,
7471   l_fact_audit ,
7472   l_net_change ,
7473   p_fact_audit_name ,
7474   p_net_change_name ,
7475   p_fact_audit_is_name ,
7476   p_net_change_is_name ,
7477   l_debug ,
7478   l_duplicate_collect ,
7479   l_execute_flag ,
7480   p_request_id ,
7481   p_collection_size ,
7482   p_parallel ,
7483   p_table_owner ,
7484   p_bis_owner  ,
7485   l_temp_log ,
7486   p_forall_size ,
7487   p_update_type ,
7488   p_mode ,
7489   l_explain_plan_check ,
7490   p_fact_dlog ,
7491   p_key_set ,
7492   p_instance_type ,
7493   p_load_pk ,
7494   l_fresh_restart ,
7495   p_op_table_space ,
7496   p_da_table ,
7497   p_pp_table ,
7498   p_master_instance ,
7499   p_rollback ,
7500   l_smart_update ,
7501   p_fk_use_nl ,
7502   p_fact_smart_update ,
7503   p_auto_dang_table_extn ,
7504   l_log_dang_keys ,
7505   l_create_parent_table_records ,
7506   l_check_fk_change ,
7507   p_stg_join_nl_percentage ,
7508   p_ok_switch_update ,
7509   p_stg_make_copy_percentage,
7510   p_ok_table,
7511   p_hash_area_size,
7512   p_sort_area_size,
7513   l_trace,
7514   l_read_cfig_options,
7515   p_job_status_table,
7516   p_max_round,
7517   p_update_dlog_lookup_table,
7518   l_dlog_has_data,
7519   p_sleep_time,
7520   l_parallel_drill_down;
7521   commit;
7522   g_stmt:='create table '||l_skip_cols_table||'('||
7523   'col_name varchar2(80)) tablespace '||p_op_table_space;
7524   if g_debug then
7525     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7526   end if;
7527   execute immediate g_stmt;
7528   if p_number_skip_cols>0 then
7529     g_stmt:='insert into '||l_skip_cols_table||'(col_name) values(:1)';
7530     if g_debug then
7531       write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7532     end if;
7533     for i in 1..p_number_skip_cols loop
7534       execute immediate g_stmt using p_skip_cols(i);
7535     end loop;
7536   end if;
7537   commit;
7538   g_stmt:='create table '||l_skip_levels_table||'('||
7539   'col_name varchar2(80)) tablespace '||p_op_table_space;
7540   if g_debug then
7541     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7542   end if;
7543   execute immediate g_stmt;
7544   if p_number_skip_levels>0 then
7545     g_stmt:='insert into '||l_skip_levels_table||'(col_name) values(:1)';
7546     if g_debug then
7547       write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7548     end if;
7549     for i in 1..p_number_skip_levels loop
7550       execute immediate g_stmt using p_skip_levels(i);
7551     end loop;
7552   end if;
7553   commit;
7554   g_stmt:='create table '||l_smart_update_table||'('||
7555   'col_name varchar2(80)) tablespace '||p_op_table_space;
7556   if g_debug then
7557     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7558   end if;
7559   execute immediate g_stmt;
7560   if p_number_smart_update_cols>0 then
7561     g_stmt:='insert into '||l_smart_update_table||'(col_name) values(:1)';
7562     if g_debug then
7563       write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7564     end if;
7565     for i in 1..p_number_smart_update_cols loop
7566       execute immediate g_stmt using p_smart_update_cols(i);
7567     end loop;
7568   end if;
7569   commit;
7570   g_stmt:='create table '||l_da_cols_table||'('||
7571   'col_name varchar2(80),stg_col_name varchar2(80)) tablespace '||p_op_table_space;
7572   if g_debug then
7573     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7574   end if;
7575   execute immediate g_stmt;
7576   if p_number_da_cols>0 then
7577     g_stmt:='insert into '||l_da_cols_table||'(col_name) values(:1)';
7578     if g_debug then
7579       write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7580     end if;
7581     for i in 1..p_number_da_cols loop
7582       execute immediate g_stmt using p_da_cols(i);
7583     end loop;
7584   end if;
7585   commit;
7586   return true;
7587 Exception when others then
7588   g_status_message:=sqlerrm;
7589   write_to_log_file_n('Error in create_load_input_table '||g_status_message);
7590   return false;
7591 End;
7592 
7593 function create_da_load_input_table(
7594 p_da_cols_table varchar2,
7595 p_op_table_space varchar2,
7596 p_da_cols EDW_OWB_COLLECTION_UTIL.varcharTableType,
7597 p_stg_da_cols EDW_OWB_COLLECTION_UTIL.varcharTableType,
7598 p_number_da_cols number
7599 ) return boolean is
7600 Begin
7601   if g_debug then
7602     write_to_log_file_n('In create_da_load_input_table ');
7603   end if;
7604   if drop_table(p_da_cols_table)=false then
7605     null;
7606   end if;
7607   g_stmt:='create table '||p_da_cols_table||'('||
7608   'col_name varchar2(80),stg_col_name varchar2(80)) tablespace '||p_op_table_space;
7609   if g_debug then
7610     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7611   end if;
7612   execute immediate g_stmt;
7613   if p_number_da_cols>0 then
7614     g_stmt:='insert into '||p_da_cols_table||'(col_name,stg_col_name) values(:1,:2)';
7615     if g_debug then
7616       write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
7617     end if;
7618     for i in 1..p_number_da_cols loop
7619       execute immediate g_stmt using p_da_cols(i),p_stg_da_cols(i);
7620     end loop;
7621   end if;
7622   commit;
7623   return true;
7624 Exception when others then
7625   g_status_message:=sqlerrm;
7626   write_to_log_file_n('Error in create_load_input_table '||g_status_message);
7627   return false;
7628 End;
7629 
7630 function update_load_input_table(
7631   p_table_name varchar2,
7632   p_ok_table varchar2,
7633   p_max_round number,
7634   p_update_dlog_lookup_table varchar2,
7635   p_dlog_has_data boolean,
7636   p_total_records number,
7637   p_stg_copy_table_flag boolean
7638 )return boolean is
7639 l_dlog_has_data varchar2(10);
7640 l_stg_copy_table_flag varchar2(10);
7641 Begin
7642   if g_debug then
7643     write_to_log_file_n('In update_load_input_table ');
7644   end if;
7645   l_dlog_has_data:='N';
7646   l_stg_copy_table_flag:='N';
7647   if p_dlog_has_data then
7648     l_dlog_has_data:='Y';
7649   end if;
7650   if p_stg_copy_table_flag then
7651     l_stg_copy_table_flag:='Y';
7652   end if;
7653   g_stmt:='update '||p_table_name||' set ok_table=:1,max_round=:2,update_dlog_lookup_table=:3,dlog_has_data=:4,'||
7654   'total_records=:5,stg_copy_table_flag=:6';
7655   if g_debug then
7656     write_to_log_file_n('Going to execute '||g_stmt||' using '||p_ok_table||' '||p_max_round||' '||
7657     p_update_dlog_lookup_table||' '||l_dlog_has_data||' '||p_total_records||' '||l_stg_copy_table_flag);
7658   end if;
7659   execute immediate g_stmt using p_ok_table,p_max_round,p_update_dlog_lookup_table,l_dlog_has_data,
7660   p_total_records,l_stg_copy_table_flag;
7661   commit;
7662   return true;
7663 Exception when others then
7664   g_status_message:=sqlerrm;
7665   write_to_log_file_n('Error in update_load_input_table '||g_status_message);
7666   return false;
7667 End;
7668 
7669 function check_job_status(p_job_id number) return varchar2 is
7670 TYPE CurTyp IS REF CURSOR;
7671 cv   CurTyp;
7672 l_var number;
7673 Begin
7674   g_stmt:='select 1 from all_jobs where job=:1';
7675   open cv for g_stmt using p_job_id;
7676   fetch cv into l_var;
7677   close cv;
7678   if l_var=1 then
7679     return 'Y';--job running
7680   else
7681     return 'N';
7682   end if;
7683 Exception when others then
7684   g_status_message:=sqlerrm;
7685   write_to_log_file_n('Error in check_job_status '||g_status_message);
7686   return null;
7687 End;
7688 
7689 function get_job_execute_time(p_job_id number) return varchar2 is
7690 TYPE CurTyp IS REF CURSOR;
7691 cv   CurTyp;
7692 l_var number;
7693 Begin
7694   g_stmt:='select total_time from all_jobs where job=:1';
7695   open cv for g_stmt using p_job_id;
7696   fetch cv into l_var;
7697   close cv;
7698   return l_var;
7699 Exception when others then
7700   g_status_message:=sqlerrm;
7701   write_to_log_file_n('Error in get_job_execute_time '||g_status_message);
7702   return null;
7703 End;
7704 
7705 function wait_on_jobs(
7706 p_job_id number,
7707 p_sleep_time number,
7708 p_thread_type varchar2
7709 ) return boolean is
7710 --
7711 l_job_id numberTableType;
7712 l_number_jobs number;
7713 --
7714 Begin
7715   l_number_jobs:=1;
7716   l_job_id(1):=p_job_id;
7717   if wait_on_jobs(l_job_id,l_number_jobs,p_sleep_time,p_thread_type)=false then
7718     return false;
7719   end if;
7720   return true;
7721 Exception when others then
7722   g_status_message:=sqlerrm;
7723   write_to_log_file_n('Error in wait_on_jobs '||g_status_message);
7724   return false;
7725 End;
7726 function wait_on_jobs(
7727 p_job_id numberTableType,
7728 p_number_jobs number,
7729 p_sleep_time number,
7730 p_thread_type varchar2
7731 ) return boolean is
7732 l_found boolean;
7733 l_status varchar2(2);
7734 l_job_status varcharTableType;
7735 Begin
7736   if g_debug then
7737     write_to_log_file_n('In wait_on_jobs sleep time='||p_sleep_time||' theread type='||p_thread_type);
7738   end if;
7739   if p_number_jobs<=0 then
7740     return true;
7741   end if;
7742   for i in 1..p_number_jobs loop
7743     l_job_status(i):='Y';
7744   end loop;
7745   loop
7746     l_found:=false;
7747     DBMS_LOCK.SLEEP(p_sleep_time);
7748     for i in 1..p_number_jobs loop
7749       if l_job_status(i)='Y' then
7750         if p_thread_type='JOB' then
7751           l_status:=check_job_status(p_job_id(i));
7752         else
7753           l_status:=check_conc_process_status(p_job_id(i));
7754         end if;
7755       end if;
7756       if l_status is null then
7757         return false;
7758       elsif l_status='Y' then
7759         l_found:=true;
7760       else
7761         if l_job_status(i)='Y' then
7762           if g_debug then
7763             write_to_log_file_n('Job '||p_job_id(i)||' has terminated '||get_time);
7764           end if;
7765           l_job_status(i):='N';
7766         end if;
7767       end if;
7768     end loop;
7769     if l_found=false then
7770       exit;
7771     end if;
7772   end loop;
7773   return true;
7774 Exception when others then
7775   g_status_message:=sqlerrm;
7776   write_to_log_file_n('Error in wait_on_jobs '||g_status_message);
7777   return false;
7778 End;
7779 
7780 function terminate_jobs(
7781 p_job_id numberTableType,
7782 p_number_jobs number
7783 )return boolean is
7784 l_sid number;
7785 l_serial number;
7786 Begin
7787   if p_number_jobs<=0 then
7788     return true;
7789   end if;
7790   for i in 1..p_number_jobs loop
7791     if terminate_job(p_job_id(i))=false then
7792       return false;
7793     end if;
7794   end loop;
7795   return true;
7796 Exception when others then
7797   g_status_message:=sqlerrm;
7798   write_to_log_file_n('Error in terminate_jobs '||g_status_message);
7799   return false;
7800 End;
7801 
7802 function terminate_job(p_job_id number) return boolean is
7803 l_sid number;
7804 l_serial number;
7805 Begin
7806   if p_job_id is not null then
7807     l_sid:=get_sid_for_job(p_job_id);
7808     if l_sid is not null then
7809       if get_session_parameters(l_sid,l_serial) then
7810         if kill_session(l_sid,l_serial)=false then
7811           if g_debug then
7812             write_to_log_file_n('Could not kill job='||p_job_id||' sid='||l_sid||' serial='||l_serial);
7813           end if;
7814         end if;
7815       end if;
7816     end if;
7817     if remove_job(p_job_id)=false then
7818       if g_debug then
7819         write_to_log_file_n('Could not remove job '||p_job_id);
7820       end if;
7821     end if;
7822     if g_debug then
7823       write_to_log_file_n('Job '||p_job_id||' terminate '||get_time);
7824     end if;
7825   end if;
7826   return true;
7827 Exception when others then
7828   g_status_message:=sqlerrm;
7829   write_to_log_file_n('Error in terminate_jobs '||g_status_message);
7830   return false;
7831 End;
7832 
7833 function get_sid_for_job(p_job_id number) return number is
7834 l_sid number;
7835 TYPE CurTyp IS REF CURSOR;
7836 cv   CurTyp;
7837 Begin
7838   g_stmt:='SELECT lock_table.SID FROM V$LOCK lock_table WHERE lock_table.TYPE=''JQ'' and lock_table.ID2=:1';
7839   if g_debug then
7840     write_to_log_file_n(g_stmt||' '||p_job_id);
7841   end if;
7842   open cv for g_stmt using p_job_id;
7843   fetch cv into l_sid;
7844   close cv;
7845   return l_sid;
7846 Exception when others then
7847   g_status_message:=sqlerrm;
7848   write_to_log_file_n('Error in get_sid_for_job '||g_status_message);
7849   return null;
7850 End;
7851 
7852 function get_session_parameters(
7853 p_sid number,
7854 p_serial out nocopy number
7855 ) return boolean is
7856 TYPE CurTyp IS REF CURSOR;
7857 cv   CurTyp;
7858 Begin
7859   g_stmt:='select ses.SERIAL# from v$session ses where ses.SID=:1';
7860   if g_debug then
7861     write_to_log_file_n(g_stmt||' '||p_sid);
7862   end if;
7863   open cv for g_stmt using p_sid;
7864   fetch cv into p_serial;
7865   close cv;
7866   return true;
7867 Exception when others then
7868   g_status_message:=sqlerrm;
7869   write_to_log_file_n('Error in get_session_parameters '||g_status_message);
7870   return false;
7871 End;
7872 
7873 function kill_session(
7874 p_sid number,
7875 p_serial number
7876 )return boolean is
7877 Begin
7878   g_stmt:='alter system kill session '''||p_sid||','||p_serial||'''';
7879   if g_debug then
7880     write_to_log_file_n(g_stmt);
7881   end if;
7882   execute immediate g_stmt;
7883   commit;
7884   return true;
7885 Exception when others then
7886   g_status_message:=sqlerrm;
7887   write_to_log_file_n('Error in kill_session '||g_status_message);
7888   return false;
7889 End;
7890 
7891 function remove_job(p_job_id number) return boolean is
7892 Begin
7893   DBMS_JOB.REMOVE(p_job_id);
7894   commit;
7895   return true;
7896 Exception when others then
7897   g_status_message:=sqlerrm;
7898   write_to_log_file_n('Error in remove_job '||g_status_message);
7899   return false;
7900 End;
7901 
7902 procedure dummy_proc is
7903 Begin
7904   null;
7905 End;
7906 
7907 function find_ok_distribution(
7908 p_ok_table varchar2,
7909 p_table_owner varchar2,
7910 p_max_threads number,
7911 p_min_job_load_size number,
7912 p_ok_low_end out NOCOPY EDW_OWB_COLLECTION_UTIL.numberTableType,
7913 p_ok_high_end out NOCOPY EDW_OWB_COLLECTION_UTIL.numberTableType,
7914 p_ok_end_count out NOCOPY integer
7915 ) return boolean is
7916 l_ok_size number;
7917 l_chunk_size integer;
7918 l_running_count integer;
7919 Begin
7920   if g_debug then
7921     write_to_log_file_n('In find_ok_distribution');
7922   end if;
7923   --bug 3472891. we have to change get_table_count_stats to get_table_count
7924   l_ok_size:=get_table_count(p_ok_table);
7925   if g_debug then
7926     write_to_log_file_n('l_ok_size='||l_ok_size);
7927   end if;
7928   p_ok_end_count:=0;
7929   if l_ok_size<=p_min_job_load_size then
7930     p_ok_end_count:=1;
7931     p_ok_low_end(p_ok_end_count):=1;
7932     p_ok_high_end(p_ok_end_count):=l_ok_size;
7933   else
7934     if (p_min_job_load_size*p_max_threads) > l_ok_size then
7935       p_ok_end_count:=trunc(l_ok_size/p_min_job_load_size);
7936       l_chunk_size:=p_min_job_load_size;
7937     else
7938       p_ok_end_count:=p_max_threads;
7939       l_chunk_size:=trunc(l_ok_size/p_max_threads);
7940     end if;
7941     if g_debug then
7942       write_to_log_file_n('p_ok_end_count='||p_ok_end_count);
7943       write_to_log_file_n('l_chunk_size='||l_chunk_size);
7944     end if;
7945     l_running_count:=0;
7946     for i in 1..p_ok_end_count loop
7947       if i=1 then
7948         p_ok_low_end(i):=1;
7949       else
7950         p_ok_low_end(i):=p_ok_high_end(i-1)+1;
7951       end if;
7952       if i=p_ok_end_count then
7953         p_ok_high_end(i):=l_ok_size;
7954       else
7955         p_ok_high_end(i):=i*l_chunk_size;
7956       end if;
7957     end loop;
7958     if g_debug then
7959       write_to_log_file_n('The low and high ends');
7960       for i in 1..p_ok_end_count loop
7961         write_to_log_file(p_ok_low_end(i)||' '||p_ok_high_end(i));
7962       end loop;
7963     end if;
7964   end if;
7965   return true;
7966 Exception when others then
7967   g_status_message:=sqlerrm;
7968   write_to_log_file_n(g_status_message);
7969   g_status:=false;
7970   return false;
7971 End;
7972 
7973 function create_job_status_table(
7974 p_table varchar2,
7975 p_op_table_space varchar2
7976 ) return boolean is
7977 Begin
7978   g_stmt:='create table '||p_table||'(object_name varchar2(100),id number,job_id number,status varchar2(40),'||
7979   'message varchar2(4000),measure1 number,measure2 number,measure3 number,measure4 number,measure5 number) '||
7980   'tablespace '||p_op_table_space;
7981   if g_debug then
7982     write_to_log_file_n(g_stmt);
7983   end if;
7984   if drop_table(p_table)=false then
7985     null;
7986   end if;
7987   execute immediate g_stmt;
7988   if g_debug then
7989     write_to_log_file_n('Table created');
7990   end if;
7991   return true;
7992 Exception when others then
7993   g_status_message:=sqlerrm;
7994   write_to_log_file_n(g_status_message);
7995   g_status:=false;
7996   return false;
7997 End;
7998 
7999 function get_join_nl(
8000 p_load_size number,
8001 p_total_records number,
8002 p_cut_off_percentage number
8003 ) return boolean is
8004 l_percentage number;
8005 l_stg_join_nl boolean;
8006 Begin
8007   if g_debug then
8008     write_to_log_file_n('In get_join_nl '||p_load_size||' '||p_total_records||' '||p_cut_off_percentage);
8009   end if;
8010   l_stg_join_nl:=true;
8011   if p_cut_off_percentage=0 then
8012     l_stg_join_nl:=false;
8013     if g_debug then
8014       write_to_log_file_n('l_stg_join_nl made FALSE');
8015     end if;
8016   elsif p_cut_off_percentage=100 then
8017     l_stg_join_nl:=true;
8018     if g_debug then
8019       write_to_log_file_n('l_stg_join_nl made TRUE');
8020     end if;
8021   else
8022     if p_load_size>0 then
8023       if p_total_records>0 then
8024         l_percentage:=round(100*(p_load_size/p_total_records));
8025         if g_debug then
8026           write_to_log_file_n('l_percentage='||l_percentage);
8027         end if;
8028         if l_percentage<=p_cut_off_percentage then
8029           l_stg_join_nl:=true;
8030           if g_debug then
8031             write_to_log_file_n('l_stg_join_nl made TRUE');
8032           end if;
8033         else
8034           l_stg_join_nl:=false;
8035           if g_debug then
8036             write_to_log_file_n('l_stg_join_nl made FALSE');
8037           end if;
8038         end if;
8039       end if;
8040     end if;
8041   end if;
8042   return l_stg_join_nl;
8043 Exception when others then
8044   g_status_message:=sqlerrm;
8045   write_to_log_file_n('Error in get_join_nl '||g_status_message);
8046   return false;
8047 End;
8048 
8049 function log_into_job_status_table(
8050 p_table varchar2,
8051 p_object_name varchar2,
8052 p_id number,
8053 p_status varchar2,
8054 p_message varchar2
8055 )return boolean is
8056 Begin
8057   if g_debug then
8058     write_to_log_file_n('In log_into_job_status_table '||p_table);
8059   end if;
8060   if log_into_job_status_table(p_table,p_object_name,p_id,p_status,p_message,null,null,null,null,null)=false then
8061     return false;
8062   end if;
8063   return true;
8064 Exception when others then
8065   g_status_message:=sqlerrm;
8066   write_to_log_file_n('Error in log_into_job_status_table '||g_status_message);
8067   return false;
8068 End;
8069 
8070 function log_into_job_status_table(
8071 p_table varchar2,
8072 p_object_name varchar2,
8073 p_id number,
8074 p_status varchar2,
8075 p_message varchar2,
8076 p_measure1 number,
8077 p_measure2 number,
8078 p_measure3 number,
8079 p_measure4 number,
8080 p_measure5 number
8081 )return boolean is
8082 Begin
8083   g_stmt:='insert into '||p_table||'(object_name,id,status,message,measure1,measure2,measure3,measure4,measure5) '||
8084   'values(:1,:2,:3,:4,:5,:6,:7,:8,:9)';
8085   if g_debug then
8086     write_to_log_file_n(g_stmt||' '||p_object_name||' '||p_id||' '||p_status||' '||p_message||' '||
8087     p_measure1||' '||p_measure2||' '||p_measure3||' '||p_measure4||' '||p_measure5);
8088   end if;
8089   execute immediate g_stmt using p_object_name,p_id,p_status,p_message,p_measure1,p_measure2,p_measure3,
8090   p_measure4,p_measure5;
8091   commit;
8092   return true;
8093 Exception when others then
8094   g_status_message:=sqlerrm;
8095   write_to_log_file_n('Error in log_into_job_status_table '||g_status_message);
8096   return false;
8097 End;
8098 
8099 function check_all_child_jobs(
8100 p_job_status_table varchar2,
8101 p_job_id numberTableType,
8102 p_number_jobs number,
8103 p_object_name varchar2
8104 ) return boolean is
8105 l_id numberTableType;
8106 l_job_id numberTableType;
8107 l_status varcharTableType;
8108 l_message varcharTableType;
8109 l_number_jobs number;
8110 Begin
8111   if get_child_job_status(
8112     p_job_status_table,
8113     p_object_name,
8114     l_id,
8115     l_job_id,
8116     l_status,
8117     l_message,
8118     l_number_jobs)=false then
8119     return false;
8120   end if;
8121   for i in 1..l_number_jobs loop
8122     if l_status(i)='ERROR' then
8123       return false;
8124     end if;
8125   end loop;
8126   return true;
8127 Exception when others then
8128   g_status_message:=sqlerrm;
8129   write_to_log_file_n('Error in check_all_child_jobs '||g_status_message);
8130   return false;
8131 End;
8132 
8133 function get_child_job_status(
8134 p_job_status_table varchar2,
8135 p_object_name varchar2,
8136 p_id out nocopy numberTableType,
8137 p_job_id out nocopy numberTableType,
8138 p_status out nocopy varcharTableType,
8139 p_message out nocopy varcharTableType,
8140 p_number_jobs out nocopy number
8141 )return boolean is
8142 TYPE CurTyp IS REF CURSOR;
8143 cv   CurTyp;
8144 Begin
8145   if p_object_name is null then
8146     g_stmt:='select id,job_id,status,message from '||p_job_status_table;
8147   else
8148     g_stmt:='select id,job_id,status,message from '||p_job_status_table||' where object_name=:1';
8149   end if;
8150   p_number_jobs:=1;
8151   if g_debug then
8152     write_to_log_file_n(g_stmt||' '||p_object_name);
8153   end if;
8154   if p_object_name is null then
8155     open cv for g_stmt;
8156   else
8157     open cv for g_stmt using p_object_name;
8158   end if;
8159   loop
8160     fetch cv into p_id(p_number_jobs),p_job_id(p_number_jobs),
8161     p_status(p_number_jobs),p_message(p_number_jobs);
8162     exit when cv%notfound;
8163     p_number_jobs:=p_number_jobs+1;
8164   end loop;
8165   close cv;
8166   p_number_jobs:=p_number_jobs-1;
8167   if g_debug then
8168     write_to_log_file_n('The job status');
8169     for i in 1..p_number_jobs loop
8170       write_to_log_file(p_id(i)||' '||p_job_id(i)||' '||p_status(i)||' '||p_message(i));
8171     end loop;
8172   end if;
8173   return true;
8174 Exception when others then
8175   g_status_message:=sqlerrm;
8176   write_to_log_file_n('Error in get_child_job_status '||g_status_message);
8177   return false;
8178 End;
8179 
8180 function create_dim_load_input_table(
8181     p_dim_name varchar2,
8182     p_table varchar2,
8183     p_conc_id number,
8184     p_conc_name varchar2,
8185     p_levels varcharTableType,
8186     p_child_level_number numberTableType,
8187     p_child_levels varcharTableType,
8188     p_child_fk varcharTableType,
8189     p_parent_pk varcharTableType,
8190     p_level_snapshot_logs varcharTableType,
8191     p_number_levels number,
8192     p_debug boolean,
8193     p_exec_flag boolean,
8194     p_bis_owner varchar2,
8195     p_parallel number,
8196     p_collection_size number,
8197     p_table_owner varchar2,
8198     p_forall_size number,
8199     p_update_type varchar2,
8200     p_level_order varcharTableType,
8201     p_skip_cols varcharTableType,
8202     p_number_skip_cols number,
8203     p_load_pk number,
8204     p_fresh_restart boolean,
8205     p_op_table_space varchar2,
8206     p_rollback varchar2,
8207     p_ltc_merge_use_nl boolean,
8208     p_dim_inc_refresh_derv boolean,
8209     p_check_fk_change boolean,
8210     p_ok_switch_update number,
8211     p_join_nl_percentage number,
8212     p_max_threads number,
8213     p_min_job_load_size number,
8214     p_sleep_time number,
8215     p_job_status_table varchar2,
8216     p_hash_area_size number,
8217     p_sort_area_size number,
8218     p_trace boolean,
8219     p_read_cfig_options boolean,
8220     p_max_fk_density number
8221     ) return boolean is
8222 l_debug varchar2(2);
8223 l_exec_flag varchar2(2);
8224 l_fresh_restart varchar2(2);
8225 l_ltc_merge_use_nl varchar2(2);
8226 l_dim_inc_refresh_derv varchar2(2);
8227 l_check_fk_change varchar2(2);
8228 l_trace varchar2(2);
8229 l_read_cfig_options varchar2(2);
8230 l_level_table varchar2(80);
8231 l_level_child_table varchar2(80);
8232 l_skip_table varchar2(80);
8233 l_run number;
8234 Begin
8235   if g_debug then
8236     write_to_log_file_n('In create_dim_load_input_table ');
8237   end if;
8238   l_level_table:=p_table||'_LT';
8239   l_level_child_table:=p_table||'_CT';
8240   l_skip_table:=p_table||'_SK';
8241   g_stmt:='create table '||p_table||'('||
8242   'conc_id number,'||
8243   'conc_name varchar2(200),'||
8244   'debug varchar2(2),'||
8245   'exec_flag varchar2(2),'||
8246   'bis_owner varchar2(200),'||
8247   'parallel number,'||
8248   'collection_size number,'||
8249   'table_owner varchar2(200),'||
8250   'forall_size number,'||
8251   'update_type varchar2(200),'||
8252   'load_pk number,'||
8253   'fresh_restart varchar2(2),'||
8254   'op_table_space varchar2(200),'||
8255   'rollback varchar2(200),'||
8256   'ltc_merge_use_nl varchar2(2),'||
8257   'dim_inc_refresh_derv varchar2(2),'||
8258   'check_fk_change varchar2(2),'||
8259   'ok_switch_update number,'||
8260   'join_nl_percentage number,'||
8261   'max_threads number,'||
8262   'min_job_load_size number,'||
8263   'sleep_time number,'||
8264   'job_status_table varchar2(200),'||
8265   'hash_area_size number,'||
8266   'sort_area_size number,'||
8267   'trace varchar2(2),'||
8268   'read_cfig_options varchar2(2),'||
8269   'ilog_table  varchar2(80),'||
8270   'skip_ilog_update varchar2(2),'||
8271   'level_change varchar2(2),'||
8272   'dim_empty_flag varchar2(2),'||
8273   'before_update_table_final varchar2(80),'||
8274   'error_rec_flag varchar2(2),'||
8275   'max_fk_density number'||
8276   ') tablespace '||p_op_table_space;
8277   if drop_table(p_table)=false then
8278     null;
8279   end if;
8280   if g_debug then
8281     write_to_log_file_n(g_stmt);
8282   end if;
8283   execute immediate g_stmt;
8284   g_stmt:='create table '||l_level_table||'('||
8285   'level_number number,'||
8286   'levels varchar2(200),'||
8287   'child_level_number number,'||
8288   'level_snapshot_logs varchar2(200),'||
8289   'level_order varchar2(200),'||
8290   'consider_snapshot varchar2(2),'||
8291   'levels_I varchar2(80),'||
8292   'use_ltc_ilog varchar2(2)'||
8293   ') tablespace '||p_op_table_space;
8294   if drop_table(l_level_table)=false then
8295     null;
8296   end if;
8297   if g_debug then
8298     write_to_log_file_n(g_stmt);
8299   end if;
8300   execute immediate g_stmt;
8301   g_stmt:='create table '||l_level_child_table||'('||
8302   'run_number number,'||
8303   'child_levels varchar2(200),'||
8304   'child_fk varchar2(200),'||
8305   'parent_pk varchar2(200)'||
8306   ') tablespace '||p_op_table_space;
8307   if drop_table(l_level_child_table)=false then
8308     null;
8309   end if;
8310   if g_debug then
8311     write_to_log_file_n(g_stmt);
8312   end if;
8313   execute immediate g_stmt;
8314   g_stmt:='create table '||l_skip_table||'('||
8315   'skip_cols varchar2(200)'||
8316   ') tablespace '||p_op_table_space;
8317   if drop_table(l_skip_table)=false then
8318     null;
8319   end if;
8320   if g_debug then
8321     write_to_log_file_n(g_stmt);
8322   end if;
8323   execute immediate g_stmt;
8324   l_debug:='N';
8325   l_exec_flag:='N';
8326   l_fresh_restart:='N';
8327   l_ltc_merge_use_nl:='N';
8328   l_dim_inc_refresh_derv:='N';
8329   l_check_fk_change:='N';
8330   l_trace:='N';
8331   l_read_cfig_options:='N';
8332   if g_debug then
8333     l_debug:='Y';
8334   end if;
8335   if p_exec_flag then
8336     l_exec_flag:='Y';
8337   end if;
8338   if p_fresh_restart then
8339     l_fresh_restart:='Y';
8340   end if;
8341   if p_ltc_merge_use_nl then
8342     l_ltc_merge_use_nl:='Y';
8343   end if;
8344   if p_dim_inc_refresh_derv then
8345     l_dim_inc_refresh_derv:='Y';
8346   end if;
8347   if p_check_fk_change then
8348     l_check_fk_change:='Y';
8349   end if;
8350   if p_trace then
8351     l_trace:='Y';
8352   end if;
8353   if p_read_cfig_options then
8354     l_read_cfig_options:='Y';
8355   end if;
8356   g_stmt:='insert into '||p_table||'('||
8357   'conc_id, '||
8358   'conc_name,'||
8359   'debug,'||
8360   'exec_flag,'||
8361   'bis_owner,'||
8362   'parallel,'||
8363   'collection_size,'||
8364   'table_owner,'||
8365   'forall_size,'||
8366   'update_type,'||
8367   'load_pk,'||
8368   'fresh_restart,'||
8369   'op_table_space,'||
8370   'rollback,'||
8371   'ltc_merge_use_nl,'||
8372   'dim_inc_refresh_derv,'||
8373   'check_fk_change,'||
8374   'ok_switch_update,'||
8375   'join_nl_percentage,'||
8376   'max_threads,'||
8377   'min_job_load_size,'||
8378   'sleep_time,'||
8379   'job_status_table,'||
8380   'hash_area_size,'||
8381   'sort_area_size,'||
8382   'trace,'||
8383   'read_cfig_options,'||
8384   'max_fk_density'||
8385   ') values ('||
8386   ':1,:2,:3,:4,:5,:6,:7,:8,:9,:10,'||
8387   ':11,:12,:13,:14,:15,:16,:17,:18,:19,:20,'||
8388   ':21,:22,:23,:24,:25,:26,:27,:28'||
8389   ')';
8390   if g_debug then
8391     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
8392   end if;
8393   /*
8394   write_to_log_file('p_conc_id='||p_conc_id);
8395   write_to_log_file('p_conc_name='||p_conc_name);
8396   write_to_log_file('l_debug='||l_debug);
8397   write_to_log_file('l_exec_flag='||l_exec_flag);
8398   write_to_log_file('p_bis_owner='||p_bis_owner);
8399   write_to_log_file('p_parallel='||p_parallel);
8400   write_to_log_file('p_collection_size='||p_collection_size);
8401   write_to_log_file('p_table_owner='||p_table_owner);
8402   write_to_log_file('p_forall_size='||p_forall_size);
8403   write_to_log_file('p_update_type='||p_update_type);
8404   write_to_log_file('p_load_pk='||p_load_pk);
8405   write_to_log_file('l_fresh_restart='||l_fresh_restart);
8406   write_to_log_file('p_op_table_space='||p_op_table_space);
8407   write_to_log_file('p_rollback='||p_rollback);
8408   write_to_log_file('l_ltc_merge_use_nl='||l_ltc_merge_use_nl);
8409   write_to_log_file('l_dim_inc_refresh_derv='||l_dim_inc_refresh_derv);
8410   write_to_log_file('l_check_fk_change='||l_check_fk_change);
8411   write_to_log_file('p_ok_switch_update='||p_ok_switch_update);
8412   write_to_log_file('p_join_nl_percentage='||p_join_nl_percentage);
8413   write_to_log_file('p_max_threads='||p_max_threads);
8414   write_to_log_file('p_min_job_load_size='||p_min_job_load_size);
8415   write_to_log_file('p_sleep_time='||p_sleep_time);
8416   write_to_log_file('p_job_status_table='||p_job_status_table);
8417   write_to_log_file('p_hash_area_size='||p_hash_area_size);
8418   write_to_log_file('p_sort_area_size='||p_sort_area_size);
8419   write_to_log_file('l_trace='||l_trace);
8420   write_to_log_file('l_read_cfig_options='||l_read_cfig_options);*/
8421   execute immediate g_stmt using
8422   p_conc_id,
8423   p_conc_name,
8424   l_debug,
8425   l_exec_flag,
8426   p_bis_owner,
8427   p_parallel,
8428   p_collection_size,
8429   p_table_owner,
8430   p_forall_size,
8431   p_update_type,
8432   p_load_pk,
8433   l_fresh_restart,
8434   p_op_table_space,
8435   p_rollback,
8436   l_ltc_merge_use_nl,
8437   l_dim_inc_refresh_derv,
8438   l_check_fk_change,
8439   p_ok_switch_update,
8440   p_join_nl_percentage,
8441   p_max_threads,
8442   p_min_job_load_size,
8443   p_sleep_time,
8444   p_job_status_table,
8445   p_hash_area_size,
8446   p_sort_area_size,
8447   l_trace,
8448   l_read_cfig_options,
8449   p_max_fk_density;
8450   commit;
8451   g_stmt:='insert into '||l_level_table||'('||
8452   'level_number,'||
8453   'levels,'||
8454   'child_level_number,'||
8455   'level_snapshot_logs,'||
8456   'level_order'||
8457   ') values('||
8458   ':1,:2,:3,:4,:5'||
8459   ')';
8460   if g_debug then
8461     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
8462   end if;
8463   for i in 1..p_number_levels loop
8464     execute immediate g_stmt using i,p_levels(i),p_child_level_number(i),
8465     p_level_snapshot_logs(i),p_level_order(i);
8466   end loop;
8467   commit;
8468   g_stmt:='insert into '||l_level_child_table||'('||
8469   'run_number,'||
8470   'child_levels,'||
8471   'child_fk,'||
8472   'parent_pk'||
8473   ') values('||
8474   ':1,:2,:3,:4'||
8475   ')';
8476   if g_debug then
8477     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
8478   end if;
8479   l_run:=0;
8480   for i in 1..p_number_levels loop
8481     for j in 1..p_child_level_number(i) loop
8482       l_run:=l_run+1;
8483       execute immediate g_stmt using l_run,p_child_levels(l_run),p_child_fk(l_run),p_parent_pk(l_run);
8484     end loop;
8485   end loop;
8486   commit;
8487   g_stmt:='insert into '||l_skip_table||'('||
8488   'skip_cols'||
8489   ') values('||
8490   ':1'||
8491   ')';
8492   if g_debug then
8493     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
8494   end if;
8495   for i in 1..p_number_skip_cols loop
8496     execute immediate g_stmt using p_skip_cols(i);
8497   end loop;
8498   commit;
8499   return true;
8500 Exception when others then
8501   g_status_message:=sqlerrm;
8502   write_to_log_file_n('Error in create_dim_load_input_table '||g_status_message);
8503   return false;
8504 End;
8505 
8506 function update_dim_load_input_table(
8507 p_input_table varchar2,
8508 p_ilog_table varchar2,
8509 p_skip_ilog_update boolean,
8510 p_level_change boolean,
8511 p_dim_empty_flag boolean,
8512 p_before_update_table_final varchar2,
8513 p_error_rec_flag boolean,
8514 p_consider_snapshot booleanTableType,
8515 p_levels_I varcharTableType,
8516 p_use_ltc_ilog booleanTableType,
8517 p_number_levels number
8518 )return boolean is
8519 l_skip_ilog_update varchar2(2);
8520 l_level_change varchar2(2);
8521 l_dim_empty_flag varchar2(2);
8522 l_error_rec_flag varchar2(2);
8523 l_use_ltc_ilog varchar2(2);
8524 l_level_table varchar2(80);
8525 Begin
8526   if g_debug then
8527     write_to_log_file_n('In update_dim_load_input_table');
8528   end if;
8529   l_level_table:=p_input_table||'_LT';
8530   l_skip_ilog_update:='N';
8531   l_level_change:='N';
8532   l_dim_empty_flag:='N';
8533   l_error_rec_flag:='N';
8534   if p_skip_ilog_update then
8535     l_skip_ilog_update:='Y';
8536   end if;
8537   if p_level_change then
8538     l_level_change:='Y';
8539   end if;
8540   if p_dim_empty_flag then
8541     l_dim_empty_flag:='Y';
8542   end if;
8543   if p_error_rec_flag then
8544     l_error_rec_flag:='Y';
8545   end if;
8546   g_stmt:='update '||p_input_table||' set ilog_table=:1,skip_ilog_update=:2,level_change=:3,'||
8547   'dim_empty_flag=:4,before_update_table_final=:5,error_rec_flag=:6';
8548   if g_debug then
8549     write_to_log_file_n(g_stmt);
8550   end if;
8551   execute immediate g_stmt using p_ilog_table,l_skip_ilog_update,l_level_change,l_dim_empty_flag,
8552   p_before_update_table_final,l_error_rec_flag;
8553   commit;
8554   g_stmt:='update '||l_level_table||' set consider_snapshot=:1,levels_I=:2,use_ltc_ilog=:3 where level_number=:4';
8555   if g_debug then
8556     write_to_log_file_n(g_stmt);
8557   end if;
8558   for i in  1..p_number_levels loop
8559     if p_use_ltc_ilog(i) then
8560       l_use_ltc_ilog:='Y';
8561     else
8562       l_use_ltc_ilog:='N';
8563     end if;
8564     if p_consider_snapshot(i) then
8565       execute immediate g_stmt using 'Y',p_levels_I(i),l_use_ltc_ilog,i;
8566     else
8567       execute immediate g_stmt using 'N',p_levels_I(i),l_use_ltc_ilog,i;
8568     end if;
8569   end loop;
8570   commit;
8571   return true;
8572 Exception when others then
8573   g_status_message:=sqlerrm;
8574   write_to_log_file_n('Error in update_dim_load_input_table '||g_status_message);
8575   return false;
8576 End;
8577 
8578 function create_derv_fact_inp_table(
8579 p_fact_name varchar2,
8580 p_input_table varchar2,
8581 p_fact_id number,
8582 p_mapping_id number,
8583 p_src_object varchar2,
8584 p_src_object_id number,
8585 p_fact_fks varcharTableType,
8586 p_higher_level booleanTableType,
8587 p_parent_dim varcharTableType,
8588 p_parent_level varcharTableType,
8589 p_level_prefix varcharTableType,
8590 p_level_pk varcharTableType,
8591 p_level_pk_key varcharTableType,
8592 p_dim_pk_key varcharTableType,
8593 p_number_fact_fks number,
8594 p_conc_id number,
8595 p_conc_program_name varchar2,
8596 p_debug boolean,
8597 p_collection_size number,
8598 p_parallel number,
8599 p_bis_owner varchar2,
8600 p_table_owner  varchar2,
8601 p_full_refresh boolean,
8602 p_forall_size number,
8603 p_update_type varchar2,
8604 p_fact_dlog varchar2,
8605 p_skip_cols varcharTableType,
8606 p_number_skip_cols number,
8607 p_load_fk number,
8608 p_fresh_restart boolean,
8609 p_op_table_space varchar2,
8610 p_bu_tables varcharTableType,--before update tables.prop dim change to derv
8611 p_bu_dimensions varcharTableType,
8612 p_number_bu_tables number,
8613 p_bu_src_fact varchar2,--what table to look at as the src fact. if null, scan full the src fact
8614 p_load_mode varchar2,
8615 p_rollback varchar2,
8616 p_src_join_nl_percentage number,
8617 p_max_threads number,
8618 p_min_job_load_size number,
8619 p_sleep_time number,
8620 p_job_status_table varchar2,
8621 p_hash_area_size number,
8622 p_sort_area_size number,
8623 p_trace boolean,
8624 p_read_cfig_options boolean
8625 ) return boolean is
8626 l_fk_table varchar2(80);
8627 l_skip_table varchar2(80);
8628 l_bu_table varchar2(80);
8629 l_debug varchar2(2);
8630 l_full_refresh varchar2(2);
8631 l_fresh_restart varchar2(2);
8632 l_trace varchar2(2);
8633 l_read_cfig_options varchar2(2);
8634 l_higher_level varchar2(2);
8635 Begin
8636   if g_debug then
8637     write_to_log_file_n('In create_derv_fact_inp_table ');
8638   end if;
8639   l_fk_table:=p_input_table||'_FK';
8640   l_skip_table:=p_input_table||'_SK';
8641   l_bu_table:=p_input_table||'_BU';
8642   if drop_table(p_input_table)=false then
8643     null;
8644   end if;
8645   if drop_table(l_fk_table)=false then
8646     null;
8647   end if;
8648   if drop_table(l_skip_table)=false then
8649     null;
8650   end if;
8651   if drop_table(l_bu_table)=false then
8652     null;
8653   end if;
8654   g_stmt:='create table '||p_input_table||'('||
8655   'fact_id number,'||
8656   'mapping_id number,'||
8657   'src_object varchar2(80),'||
8658   'src_object_id number,'||
8659   'conc_id number,'||
8660   'conc_program_name varchar2(80),'||
8661   'debug varchar2(2),'||
8662   'collection_size number,'||
8663   'parallel number,'||
8664   'bis_owner varchar2(80),'||
8665   'table_owner  varchar2(80),'||
8666   'full_refresh varchar2(2),'||
8667   'forall_size number,'||
8668   'update_type varchar2(80),'||
8669   'fact_dlog varchar2(80),'||
8670   'load_fk number,'||
8671   'fresh_restart varchar2(2),'||
8672   'op_table_space varchar2(80),'||
8673   'bu_src_fact varchar2(80),'||
8674   'load_mode varchar2(80),'||
8675   'rollback varchar2(80),'||
8676   'src_join_nl_percentage number,'||
8677   'max_threads number,'||
8678   'min_job_load_size number,'||
8679   'sleep_time number,'||
8680   'job_status_table varchar2(80),'||
8681   'hash_area_size number,'||
8682   'sort_area_size number,'||
8683   'trace varchar2(2),'||
8684   'read_cfig_options varchar2(2),'||
8685   'ilog_table varchar2(80),'||
8686   'dlog_table varchar2(80),'||
8687   'skip_ilog_update varchar2(2),'||
8688   'skip_dlog_update varchar2(2),'||
8689   'skip_ilog varchar2(2),'||
8690   'src_object_ilog varchar2(80),'||
8691   'src_object_dlog varchar2(80),'||
8692   'src_snplog_has_pk varchar2(2),'||
8693   'err_rec_flag varchar2(2),'||
8694   'err_rec_flag_d varchar2(2),'||
8695   'dbms_job_id number '||
8696   ') tablespace '||p_op_table_space;
8697   if g_debug then
8698     write_to_log_file_n(g_stmt);
8699   end if;
8700   execute immediate g_stmt;
8701   l_debug:='N';
8702   l_full_refresh:='N';
8703   l_fresh_restart:='N';
8704   l_trace:='N';
8705   l_read_cfig_options:='N';
8706   if p_debug then
8707     l_debug:='Y';
8708   end if;
8709   if p_full_refresh then
8710     l_full_refresh:='Y';
8711   end if;
8712   if p_fresh_restart then
8713     l_fresh_restart:='Y';
8714   end if;
8715   if p_trace then
8716     l_trace:='Y';
8717   end if;
8718   if p_read_cfig_options then
8719     l_read_cfig_options:='Y';
8720   end if;
8721   g_stmt:='insert into '||p_input_table||'('||
8722   'fact_id,'||
8723   'mapping_id,'||
8724   'src_object,'||
8725   'src_object_id,'||
8726   'conc_id,'||
8727   'conc_program_name,'||
8728   'debug,'||
8729   'collection_size,'||
8730   'parallel,'||
8731   'bis_owner,'||
8732   'table_owner ,'||
8733   'full_refresh,'||
8734   'forall_size,'||
8735   'update_type,'||
8736   'fact_dlog,'||
8737   'load_fk,'||
8738   'fresh_restart,'||
8739   'op_table_space,'||
8740   'bu_src_fact,'||
8741   'load_mode,'||
8742   'rollback,'||
8743   'src_join_nl_percentage,'||
8744   'max_threads,'||
8745   'min_job_load_size,'||
8746   'sleep_time,'||
8747   'job_status_table,'||
8748   'hash_area_size,'||
8749   'sort_area_size,'||
8750   'trace,'||
8751   'read_cfig_options'||
8752   ') values('||
8753   ':1,:2,:3,:4,:5,:6,:7,:8,:9,:10,'||
8754   ':11,:12,:13,:14,:15,:16,:17,:18,:19,:20,'||
8755   ':21,:22,:23,:24,:25,:26,:27,:28,:29,:30 '||
8756   ')';
8757   if g_debug then
8758     write_to_log_file_n(g_stmt);
8759   end if;
8760   execute immediate g_stmt using
8761   p_fact_id,
8762   p_mapping_id,
8763   p_src_object,
8764   p_src_object_id,
8765   p_conc_id,
8766   p_conc_program_name,
8767   l_debug,
8768   p_collection_size,
8769   p_parallel,
8770   p_bis_owner,
8771   p_table_owner ,
8772   l_full_refresh,
8773   p_forall_size,
8774   p_update_type,
8775   p_fact_dlog,
8776   p_load_fk,
8777   l_fresh_restart,
8778   p_op_table_space,
8779   p_bu_src_fact,
8780   p_load_mode,
8781   p_rollback,
8782   p_src_join_nl_percentage,
8783   p_max_threads,
8784   p_min_job_load_size,
8785   p_sleep_time,
8786   p_job_status_table,
8787   p_hash_area_size,
8788   p_sort_area_size,
8789   l_trace,
8790   l_read_cfig_options;
8791   commit;
8792   g_stmt:='create table '||l_fk_table||'('||
8793   'fact_fks varchar2(200),'||
8794   'higher_level varchar2(2),'||
8795   'parent_dim varchar2(200),'||
8796   'parent_level varchar2(200),'||
8797   'level_prefix varchar2(200),'||
8798   'level_pk varchar2(200),'||
8799   'level_pk_key varchar2(200),'||
8800   'dim_pk_key varchar2(200)'||
8801   ') tablespace '||p_op_table_space;
8802   if g_debug then
8803     write_to_log_file_n(g_stmt);
8804   end if;
8805   execute immediate g_stmt;
8806   g_stmt:='insert into '||l_fk_table||'('||
8807   'fact_fks,'||
8808   'higher_level,'||
8809   'parent_dim,'||
8810   'parent_level,'||
8811   'level_prefix,'||
8812   'level_pk,'||
8813   'level_pk_key,'||
8814   'dim_pk_key'||
8815   ') values('||
8816   ':1,:2,:3,:4,:5,:6,:7,:8'||
8817   ')';
8818   for i in 1..p_number_fact_fks loop
8819     if p_higher_level(i) then
8820       l_higher_level:='Y';
8821     else
8822       l_higher_level:='N';
8823     end if;
8824     execute immediate g_stmt using
8825     p_fact_fks(i),
8826     l_higher_level,
8827     p_parent_dim(i),
8828     p_parent_level(i),
8829     p_level_prefix(i),
8830     p_level_pk(i),
8831     p_level_pk_key(i),
8832     p_dim_pk_key(i);
8833   end loop;
8834   commit;
8835   g_stmt:='create table '||l_skip_table||'('||
8836   'skip_cols varchar2(80)'||
8837   ') tablespace '||p_op_table_space;
8838   if g_debug then
8839     write_to_log_file_n(g_stmt);
8840   end if;
8841   execute immediate g_stmt;
8842   g_stmt:='insert into '||l_skip_table||'('||
8843   'skip_cols'||
8844   ') values('||
8845   ':1'||
8846   ')';
8847   if g_debug then
8848     write_to_log_file_n(g_stmt);
8849   end if;
8850   for i in 1..p_number_skip_cols loop
8851     execute immediate g_stmt using p_skip_cols(i);
8852   end loop;
8853   commit;
8854   g_stmt:='create table '||l_bu_table||'('||
8855   'bu_tables varchar2(80),'||
8856   'bu_dimensions varchar2(80)'||
8857   ') tablespace '||p_op_table_space;
8858   if g_debug then
8859     write_to_log_file_n(g_stmt);
8860   end if;
8861   execute immediate g_stmt;
8862   g_stmt:='insert into '||l_bu_table||'('||
8863   'bu_tables,'||
8864   'bu_dimensions'||
8865   ') values('||
8866   ':1,:2'||
8867   ')';
8868   if g_debug then
8869     write_to_log_file_n(g_stmt);
8870   end if;
8871   for i in 1..p_number_bu_tables loop
8872     execute immediate g_stmt using p_bu_tables(i),p_bu_dimensions(i);
8873   end loop;
8874   commit;
8875   return true;
8876 Exception when others then
8877   g_status_message:=sqlerrm;
8878   write_to_log_file_n('Error in create_derv_fact_inp_table '||g_status_message);
8879   return false;
8880 End;
8881 
8882 function update_derv_fact_input_table(
8883 p_input_table varchar2,
8884 p_ilog_table varchar2,
8885 p_dlog_table varchar2,
8886 p_skip_ilog_update boolean,
8887 p_skip_dlog_update boolean,
8888 p_skip_ilog boolean,
8889 p_load_mode varchar2,
8890 p_full_refresh boolean,
8891 p_src_object_ilog varchar2,
8892 p_src_object_dlog varchar2,
8893 p_src_snplog_has_pk boolean,
8894 p_err_rec_flag boolean,
8895 p_err_rec_flag_d boolean
8896 ) return boolean is
8897 l_skip_ilog_update varchar2(2);
8898 l_skip_dlog_update varchar2(2);
8899 l_skip_ilog varchar2(2);
8900 l_full_refresh varchar2(2);
8901 l_src_snplog_has_pk varchar2(2);
8902 l_err_rec_flag varchar2(2);
8903 l_err_rec_flag_d varchar2(2);
8904 Begin
8905   if g_debug then
8906     write_to_log_file_n('In update_derv_fact_input_table');
8907   end if;
8908   l_skip_ilog_update:='N';
8909   l_skip_dlog_update:='N';
8910   l_skip_ilog:='N';
8911   l_full_refresh:='N';
8912   l_src_snplog_has_pk:='N';
8913   l_err_rec_flag:='N';
8914   l_err_rec_flag_d:='N';
8915   if p_skip_ilog_update then
8916     l_skip_ilog_update:='Y';
8917   end if;
8918   if p_skip_dlog_update then
8919     l_skip_dlog_update:='Y';
8920   end if;
8921   if p_skip_ilog then
8922     l_skip_ilog:='Y';
8923   end if;
8924   if p_full_refresh then
8925     l_full_refresh:='Y';
8926   end if;
8927   if p_src_snplog_has_pk then
8928     l_src_snplog_has_pk:='Y';
8929   end if;
8930   if p_err_rec_flag then
8931     l_err_rec_flag:='Y';
8932   end if;
8933   if p_err_rec_flag_d then
8934     l_err_rec_flag_d:='Y';
8935   end if;
8936   g_stmt:='update '||p_input_table||' set '||
8937   'ilog_table=:1,'||
8938   'dlog_table=:2,'||
8939   'skip_ilog_update=:3,'||
8940   'skip_dlog_update=:4,'||
8941   'skip_ilog=:5,'||
8942   'load_mode=:6,'||
8943   'full_refresh=:7,'||
8944   'src_object_ilog=:8,'||
8945   'src_object_dlog=:9,'||
8946   'src_snplog_has_pk=:10,'||
8947   'err_rec_flag=:11,'||
8948   'err_rec_flag_d=:12';
8949   if g_debug then
8950     write_to_log_file_n(g_stmt);
8951   end if;
8952   execute immediate g_stmt using
8953   p_ilog_table,
8954   p_dlog_table,
8955   l_skip_ilog_update,
8956   l_skip_dlog_update,
8957   l_skip_ilog,
8958   p_load_mode,
8959   l_full_refresh,
8960   p_src_object_ilog,
8961   p_src_object_dlog,
8962   l_src_snplog_has_pk,
8963   l_err_rec_flag,
8964   l_err_rec_flag_d;
8965   commit;
8966   return true;
8967 Exception when others then
8968   g_status_message:=sqlerrm;
8969   write_to_log_file_n('Error in update_derv_fact_input_table '||g_status_message);
8970   return false;
8971 End;
8972 
8973 function merge_all_prot_tables(
8974 p_prot_table varchar2,
8975 p_prot_table_extn varchar2,
8976 p_op_table_space varchar2,
8977 p_bis_owner varchar2,
8978 p_parallel number
8979 )return boolean is
8980 Begin
8981   if g_debug then
8982     write_to_log_file_n('In merge_all_prot_tables p_prot_table '||p_prot_table||', p_prot_table_extn '||
8983     p_prot_table_extn);
8984   end if;
8985   return merge_all_ilog_tables(p_prot_table,p_prot_table,null,p_prot_table_extn,p_op_table_space,p_bis_owner,
8986   p_parallel);
8987   return true;
8988 Exception when others then
8989   g_status_message:=sqlerrm;
8990   write_to_log_file_n('Error in merge_all_prot_tables '||g_status_message);
8991   return false;
8992 End;
8993 
8994 function merge_all_ilog_tables (
8995 p_ilog_pattern varchar2,
8996 p_ilog_table varchar2,
8997 p_ilog_table2 varchar2,
8998 p_ilog_table_extn varchar2,
8999 p_op_table_space varchar2,
9000 p_bis_owner varchar2,
9001 p_parallel number
9002 )return boolean is
9003 l_ilog_tables EDW_OWB_COLLECTION_UTIL.varcharTableType;
9004 l_number_ilog_tables number;
9005 Begin
9006   if g_debug then
9007     write_to_log_file_n('In merge_all_ilog_tables p_ilog_pattern '||p_ilog_pattern||
9008     ' p_ilog_table '||p_ilog_table||', p_ilog_table_extn '||p_ilog_table_extn);
9009   end if;
9010   if get_ilog_tables_from_db(p_ilog_pattern,p_ilog_table_extn,p_bis_owner,l_ilog_tables,
9011     l_number_ilog_tables)=false then
9012     return false;
9013   end if;
9014   if l_number_ilog_tables>0 then --only if the prev run was  multi threaded
9015     /*
9016     if these small ok tables exist, then g_ok_rowid_table cannot exist
9017     */
9018     if EDW_OWB_COLLECTION_UTIL.drop_table(p_ilog_table)=false then
9019       null;
9020     end if;
9021     --just for safety
9022     if p_ilog_table2 is not null then
9023       if EDW_OWB_COLLECTION_UTIL.drop_table(p_ilog_table2)=false then
9024         null;
9025       end if;
9026     end if;
9027     g_stmt:='create table '||p_ilog_table||' tablespace '||p_op_table_space;
9028     g_stmt:=g_stmt||' storage (initial 4M next 4M pctincrease 0) ';
9029     if p_parallel is not null then
9030       g_stmt:=g_stmt||' parallel (degree '||p_parallel||') ';
9031     end if;
9032     g_stmt:=g_stmt||' as ';
9033     for i in 1..l_number_ilog_tables loop
9034       g_stmt:=g_stmt||' select * from '||l_ilog_tables(i)||' union all ';
9035     end loop;
9036     g_stmt:=substr(g_stmt,1,length(g_stmt)-10);
9037     if g_debug then
9038       write_to_log_file_n(g_stmt||get_time);
9039     end if;
9040     execute immediate g_stmt;
9041     if g_debug then
9042       write_to_log_file_n('Created with '||sql%rowcount||' rows '||get_time);
9043     end if;
9044     for i in 1..l_number_ilog_tables loop
9045       if drop_table(l_ilog_tables(i))=false then
9046         null;
9047       end if;
9048     end loop;
9049   end if;
9050   return true;
9051 Exception when others then
9052   g_status_message:=sqlerrm;
9053   write_to_log_file_n('Error in merge_all_ilog_tables '||g_status_message);
9054   return false;
9055 End;
9056 
9057 function drop_prot_tables(
9058 p_prot_table varchar2,
9059 p_prot_table_extn varchar2,
9060 p_bis_owner varchar2
9061 ) return boolean is
9062 Begin
9063   return drop_ilog_tables(p_prot_table,p_prot_table_extn,p_bis_owner);
9064   return true;
9065 Exception when others then
9066   g_status_message:=sqlerrm;
9067   write_to_log_file_n('Error in drop_prot_tables '||g_status_message);
9068   return false;
9069 End;
9070 
9071 function drop_ilog_tables(
9072 p_ilog_table varchar2,
9073 p_ilog_table_extn varchar2,
9074 p_bis_owner varchar2
9075 ) return boolean is
9076 l_ilog_tables EDW_OWB_COLLECTION_UTIL.varcharTableType;
9077 l_number_ilog_tables number;
9078 Begin
9079   if g_debug then
9080     write_to_log_file_n('In drop_ilog_tables');
9081   end if;
9082   if get_ilog_tables_from_db(p_ilog_table,p_ilog_table_extn,p_bis_owner,l_ilog_tables,
9083     l_number_ilog_tables)=false then
9084     return false;
9085   end if;
9086   if l_number_ilog_tables>0 then
9087     for i in 1..l_number_ilog_tables loop
9088       if drop_table(l_ilog_tables(i))=false then
9089         null;
9090       end if;
9091     end loop;
9092   end if;
9093   return true;
9094 Exception when others then
9095   g_status_message:=sqlerrm;
9096   write_to_log_file_n('Error in drop_ilog_tables '||g_status_message);
9097   return false;
9098 End;
9099 
9100 function get_ilog_tables_from_db(
9101 p_ilog_table varchar2,
9102 p_ilog_table_extn varchar2,
9103 p_bis_owner varchar2,
9104 p_ilog_tables out nocopy EDW_OWB_COLLECTION_UTIL.varcharTableType,
9105 p_number_ilog_tables out nocopy number
9106 )return boolean is
9107 TYPE CurTyp IS REF CURSOR;
9108 cv   CurTyp;
9109 l_search_string varchar2(80);
9110 l_ilog_table_extn varchar2(30);
9111 l_ilog_table varchar2(80);
9112 Begin
9113   if g_debug then
9114     write_to_log_file_n('In get_ilog_tables_from_db');
9115   end if;
9116   l_ilog_table:=p_ilog_table;
9117   if instr(l_ilog_table,'.')>0 then
9118     l_ilog_table:=substr(p_ilog_table,instr(p_ilog_table,'.')+1,length(p_ilog_table));
9119   end if;
9120   if p_ilog_table_extn is null then
9121     l_ilog_table_extn:=substr(l_ilog_table,instr(l_ilog_table,'_',-1)+1,length(l_ilog_table));
9122   else
9123     l_ilog_table_extn:=p_ilog_table_extn;
9124   end if;
9125   l_search_string:=substr(l_ilog_table,1,instr(l_ilog_table,'_'||l_ilog_table_extn)-1);
9126   g_stmt:='select table_name from all_tables where (table_name like '''||l_search_string||'_%_'||
9127   l_ilog_table_extn||''' or table_name like '''||l_search_string||'_%_'||l_ilog_table_extn||'A'') '||
9128   'and owner='''||p_bis_owner||'''';
9129   if g_debug then
9130     write_to_log_file_n(g_stmt);
9131   end if;
9132   p_number_ilog_tables:=1;
9133   open cv for g_stmt;
9134   loop
9135     fetch cv into p_ilog_tables(p_number_ilog_tables);
9136     exit when cv%notfound;
9137     p_number_ilog_tables:=p_number_ilog_tables+1;
9138   end loop;
9139   close cv;
9140   p_number_ilog_tables:=p_number_ilog_tables-1;
9141   for i in 1..p_number_ilog_tables loop
9142     p_ilog_tables(i):=p_bis_owner||'.'||p_ilog_tables(i);
9143   end loop;
9144   if g_debug then
9145     write_to_log_file_n('The tables');
9146     for i in 1..p_number_ilog_tables loop
9147       write_to_log_file(p_ilog_tables(i));
9148     end loop;
9149   end if;
9150   return true;
9151 Exception when others then
9152   g_status_message:=sqlerrm;
9153   write_to_log_file_n('Error in get_ilog_tables_from_db '||g_status_message);
9154   return false;
9155 End;
9156 
9157 function put_rownum_in_ilog_table(
9158 p_ilog varchar2,
9159 p_ilog_old varchar2,
9160 p_op_table_space varchar2,
9161 p_parallel number
9162 )return boolean is
9163 Begin
9164   if g_debug then
9165     write_to_log_file_n('In put_rownum_in_ilog_table p_ilog='||p_ilog||',p_ilog_old='||p_ilog_old);
9166   end if;
9167   if EDW_OWB_COLLECTION_UTIL.drop_table(p_ilog)=false then
9168     null;
9169   end if;
9170   g_stmt:='create table '||p_ilog||' tablespace '||p_op_table_space;
9171   g_stmt:=g_stmt||' storage (initial 4M next 4M pctincrease 0) ';
9172   if p_parallel is not null then
9173     g_stmt:=g_stmt||' parallel (degree '||p_parallel||') ';
9174   end if;
9175   g_stmt:=g_stmt||' as select '||p_ilog_old||'.*, rownum row_num from '||p_ilog_old;
9176   if g_debug then
9177     write_to_log_file_n(g_stmt||get_time);
9178   end if;
9179   execute immediate g_stmt;
9180   if g_debug then
9181     write_to_log_file_n('Created with '||sql%rowcount||' rows '||get_time);
9182   end if;
9183   create_rownum_index_ilog(p_ilog,p_op_table_space,p_parallel);
9184   if EDW_OWB_COLLECTION_UTIL.drop_table(p_ilog_old)=false then
9185     null;
9186   end if;
9187   EDW_OWB_COLLECTION_UTIL.analyze_table_stats(substr(p_ilog,instr(p_ilog,'.')+1,
9188   length(p_ilog)),substr(p_ilog,1,instr(p_ilog,'.')-1));
9189   return true;
9190 Exception when others then
9191   g_status_message:=sqlerrm;
9192   write_to_log_file_n('Error in put_rownum_in_ilog_table '||g_status_message);
9193   return false;
9194 End;
9195 
9196 procedure create_rownum_index_ilog(
9197 p_ilog varchar2,
9198 p_op_table_space varchar2,
9199 p_parallel number
9200 ) is
9201 Begin
9202   --create the index
9203   g_stmt:='create unique index '||p_ilog||'u on '||p_ilog||'(row_num) '||
9204   'tablespace '||p_op_table_space;
9205   if p_parallel is not null then
9206     g_stmt:=g_stmt||' parallel '||p_parallel;
9207   end if;
9208   if g_debug then
9209     write_to_log_file_n(g_stmt||get_time);
9210   end if;
9211   execute immediate g_stmt;
9212   if g_debug then
9213     write_to_log_file_n('Created index '||get_time);
9214   end if;
9215 Exception when others then
9216   write_to_log_file_n('Error in create_rownum_index_ilog '||sqlerrm);
9217 End;
9218 
9219 function check_conc_process_status(p_conc_id number) return varchar2 is
9220 l_phase varchar2(400);
9221 l_status varchar2(400);
9222 l_dev_phase  varchar2(400);
9223 l_dev_status varchar2(400);
9224 l_message varchar2(4000);
9225 l_conc_id number;
9226 Begin
9227   l_conc_id:=p_conc_id;
9228   if FND_CONCURRENT.get_request_status(l_conc_id,null,null,l_phase,l_status,
9229     l_dev_phase,l_dev_status,l_message)=false then
9230     write_to_log_file_n('FND_CONCURRENT.get_request_status returned with false');
9231     write_to_log_file(FND_MESSAGE.get);
9232     return 'N';
9233   end if;
9234   if l_dev_phase is null or l_dev_phase='COMPLETE' then
9235     return 'N';--there is no more this process
9236   else
9237     return 'Y';--still running
9238   end if;
9239 Exception when others then
9240   g_status_message:=sqlerrm;
9241   write_to_log_file_n('Error in check_conc_process_status '||g_status_message);
9242   return null;
9243 End;
9244 
9245 function update_inp_table_jobid(
9246 p_input_table varchar2,
9247 p_job_id number
9248 )return boolean is
9249 Begin
9250   g_stmt:='update '||p_input_table||' set dbms_job_id=:1';
9251   if g_debug then
9252     write_to_log_file_n(g_stmt||' '||p_job_id);
9253   end if;
9254   execute immediate g_stmt using p_job_id;
9255   return true;
9256 Exception when others then
9257   g_status_message:=sqlerrm;
9258   write_to_log_file_n('Error in update_inp_table_jobid '||sqlerrm||' '||get_time);
9259   return false;
9260 End;
9261 
9262 function update_inp_table_concid(
9263 p_input_table varchar2,
9264 p_conc_id number
9265 )return boolean is
9266 Begin
9267   g_stmt:='update '||p_input_table||' set conc_id=:1';
9268   if g_debug then
9269     write_to_log_file_n(g_stmt||' '||p_conc_id);
9270   end if;
9271   execute immediate g_stmt using p_conc_id;
9272   return true;
9273 Exception when others then
9274   g_status_message:=sqlerrm;
9275   write_to_log_file_n('Error in update_inp_table_concid '||sqlerrm||' '||get_time);
9276   return false;
9277 End;
9278 
9279 function find_skip_attributes(
9280 p_object_name varchar2,
9281 p_object_type varchar2,
9282 p_skip_cols out NOCOPY varcharTableType,
9283 p_number_skip_cols out NOCOPY number
9284 ) return boolean is
9285 l_nc_columns varcharTableType;
9286 l_number_nc_columns number;
9287 l_ltc_in_map varcharTableType;
9288 l_ltc_col_in_map varcharTableType;
9289 l_tgt_in_map varcharTableType;--dummy
9290 l_tgt_col_in_map varcharTableType;
9291 l_number_ltc_in_map number;
9292 l_ltc_used varcharTableType;
9293 l_ltc_col_used varcharTableType;
9294 l_number_ltc_used number;
9295 l_index number;
9296 l_distinct_ltc_used varcharTableType;
9297 l_number_distinct_ltc_used number;
9298 l_pk varcharTableType;
9299 l_number_pk number;
9300 l_fk varcharTableType;
9301 l_parent_table varcharTableType;
9302 l_number_fk number;
9303 l_columns varcharTableType;
9304 l_number_columns number;
9305 l_prefix varcharTableType;
9306 l_col_to_add varchar2(200);
9307 Begin
9308   p_number_skip_cols:=0;
9309   l_number_nc_columns:=0;
9310   l_number_ltc_in_map:=0;
9311   l_number_ltc_used:=0;
9312   l_number_distinct_ltc_used:=0;
9313   if get_lookup_code('LC_'||p_object_name,l_nc_columns,l_number_nc_columns)=false then
9314     return false;
9315   end if;
9316   if g_debug then
9317     write_to_log_file_n('The columns that need to be loaded from lookup type LC_'||p_object_name);
9318     for i in 1..l_number_nc_columns loop
9319       write_to_log_file(l_nc_columns(i));
9320     end loop;
9321   end if;
9322   if l_number_nc_columns=0 then
9323     if g_debug then
9324       write_to_log_file_n('No columns defined in lookup type. So getting skipped columns from metadata');
9325     end if;
9326     if get_item_set_cols(p_skip_cols,p_number_skip_cols,p_object_name,'SKIP_LOAD_SET')=false then
9327       return false;
9328     end if;
9329   else
9330     if g_debug then
9331       write_to_log_file_n('Finding the skipped columns');
9332     end if;
9333     if p_object_type='DIMENSION' then
9334       if get_col_col_in_map(null,p_object_name,l_ltc_in_map,l_ltc_col_in_map,l_tgt_in_map,l_tgt_col_in_map,
9335         l_number_ltc_in_map)=false then
9336         return false;
9337       end if;
9338       for i in 1..l_number_nc_columns loop
9339         l_index:=index_in_table(l_tgt_col_in_map,l_number_ltc_in_map,l_nc_columns(i));
9340         if l_index>0 then
9341           l_number_ltc_used:=l_number_ltc_used+1;
9342           l_ltc_used(l_number_ltc_used):=l_ltc_in_map(l_index);
9343           l_ltc_col_used(l_number_ltc_used):=l_ltc_col_in_map(l_index);
9344         end if;
9345       end loop;
9346       if g_debug then
9347         write_to_log_file_n('LTC and columns needed');
9348         for i in 1..l_number_ltc_used loop
9349           write_to_log_file(l_ltc_used(i)||'('||l_ltc_col_used(i)||')');
9350         end loop;
9351       end if;
9352       for i in 1..l_number_ltc_used loop
9353         if value_in_table(l_distinct_ltc_used,l_number_distinct_ltc_used,l_ltc_used(i))=false then
9354           l_number_distinct_ltc_used:=l_number_distinct_ltc_used+1;
9355           l_distinct_ltc_used(l_number_distinct_ltc_used):=l_ltc_used(i);
9356         end if;
9357       end loop;
9358       for i in 1..l_number_distinct_ltc_used loop
9359         l_prefix(i):=get_level_prefix(substr(l_distinct_ltc_used(i),1,
9360         instr(l_distinct_ltc_used(i),'_LTC',-1)-1));
9361       end loop;
9362       if g_debug then
9363         write_to_log_file_n('Distinct LTC');
9364         for i in 1..l_number_distinct_ltc_used loop
9365           write_to_log_file(l_distinct_ltc_used(i)||'('||l_prefix(i)||')');
9366         end loop;
9367       end if;
9368       if get_columns_for_table(p_object_name,l_columns,l_number_columns)=false then
9369         return false;
9370       end if;
9371       for i in 1..l_number_distinct_ltc_used loop
9372         l_number_pk:=0;
9373         l_number_fk:=0;
9374         if get_object_unique_key(l_distinct_ltc_used(i),null,
9375         l_pk,l_number_pk)=false then
9376           return false;
9377         end if;
9378         if g_debug then
9379           write_to_log_file_n('PK for '||l_distinct_ltc_used(i));
9380           for j in 1..l_number_pk loop
9381             write_to_log_file(l_pk(j));
9382           end loop;
9383         end if;
9384         for j in 1..l_number_pk loop
9385           l_index:=index_in_table(l_ltc_in_map,l_ltc_col_in_map,l_number_ltc_in_map,
9386           l_distinct_ltc_used(i),l_pk(j));
9387           if l_index<=0 then
9388             l_index:=index_in_table(l_columns,l_number_columns,l_prefix(i)||'_'||l_pk(j));
9389             if l_index>0 then
9390               l_col_to_add:=l_columns(l_index);
9391             end if;
9392           else
9393             l_col_to_add:=l_tgt_col_in_map(l_index);
9394           end if;
9395           if l_index>0 then
9396             if value_in_table(l_nc_columns,l_number_nc_columns,l_col_to_add)=false then
9397               if g_debug then
9398                 write_to_log_file_n('Column '||l_pk(j)||' of '||l_distinct_ltc_used(i)||
9399                 ' missing in needec col list');
9400                 write_to_log_file('Adding '||l_col_to_add);
9401               end if;
9402               l_number_nc_columns:=l_number_nc_columns+1;
9403               l_nc_columns(l_number_nc_columns):=l_col_to_add;
9404             end if;
9405           end if;
9406         end loop;
9407         if get_fks_for_table(l_distinct_ltc_used(i),l_parent_table,l_fk,l_number_fk)=false then
9408           return false;
9409         end if;
9410         if g_debug then
9411           write_to_log_file_n('FK for '||l_distinct_ltc_used(i));
9412           for j in 1..l_number_fk loop
9413             write_to_log_file(l_fk(j)||'('||l_parent_table(j)||')');
9414           end loop;
9415         end if;
9416         for j in 1..l_number_fk loop
9417           if value_in_table(l_distinct_ltc_used,l_number_distinct_ltc_used,l_parent_table(j)) then
9418             l_index:=index_in_table(l_ltc_in_map,l_ltc_col_in_map,l_number_ltc_in_map,
9419             l_distinct_ltc_used(i),l_fk(j));
9420             if l_index<=0 then
9421               l_index:=index_in_table(l_columns,l_number_columns,l_prefix(i)||'_'||l_fk(j));
9422               if l_index>0 then
9423                 l_col_to_add:=l_columns(l_index);
9424               end if;
9425             else
9426               l_col_to_add:=l_tgt_col_in_map(l_index);
9427             end if;
9428             if l_index>0 then
9429               if value_in_table(l_nc_columns,l_number_nc_columns,l_col_to_add)=false then
9430                 if g_debug then
9431                   write_to_log_file_n('Column '||l_fk(j)||' of '||l_distinct_ltc_used(i)||
9432                   ' missing in needec col list');
9433                   write_to_log_file('Adding '||l_col_to_add);
9434                 end if;
9435                 l_number_nc_columns:=l_number_nc_columns+1;
9436                 l_nc_columns(l_number_nc_columns):=l_col_to_add;
9437               end if;
9438             end if;
9439           end if;
9440         end loop;
9441       end loop;
9442       if g_debug then
9443         write_to_log_file_n('All the needed columns of the dimension');
9444         for i in 1..l_number_nc_columns loop
9445           write_to_log_file(l_nc_columns(i));
9446         end loop;
9447       end if;
9448       for i in 1..l_number_ltc_in_map loop
9449         if value_in_table(l_nc_columns,l_number_nc_columns,l_tgt_col_in_map(i))=false then
9450           p_number_skip_cols:=p_number_skip_cols+1;
9451           p_skip_cols(p_number_skip_cols):=l_tgt_col_in_map(i);
9452         end if;
9453       end loop;
9454     else --this is a fact
9455       l_number_pk:=0;
9456       if get_object_unique_key(p_object_name,null,l_pk,l_number_pk)=false then
9457         return false;
9458       end if;
9459       if g_debug then
9460         write_to_log_file_n('PK for '||p_object_name);
9461         for j in 1..l_number_pk loop
9462           write_to_log_file(l_pk(j));
9463         end loop;
9464       end if;
9465       for i in 1..l_number_pk loop
9466         if value_in_table(l_nc_columns,l_number_nc_columns,l_pk(i))=false then
9467           l_number_nc_columns:=l_number_nc_columns+1;
9468           l_nc_columns(l_number_nc_columns):=l_pk(i);
9469         end if;
9470       end loop;
9471       if g_debug then
9472         write_to_log_file_n('All the needed columns of the fact');
9473         for i in 1..l_number_nc_columns loop
9474           write_to_log_file(l_nc_columns(i));
9475         end loop;
9476       end if;
9477       if get_columns_for_table(p_object_name,l_columns,l_number_columns)=false then
9478         return false;
9479       end if;
9480       for i in 1..l_number_columns loop
9481         if value_in_table(l_nc_columns,l_number_nc_columns,l_columns(i))=false then
9482           p_number_skip_cols:=p_number_skip_cols+1;
9483           p_skip_cols(p_number_skip_cols):=l_columns(i);
9484         end if;
9485       end loop;
9486     end if;
9487   end if;
9488   if g_debug then
9489     write_to_log_file_n('The skipped columns('||p_number_skip_cols||')');
9490     for i in 1..p_number_skip_cols loop
9491       write_to_log_file(p_skip_cols(i));
9492     end loop;
9493   end if;
9494   return true;
9495 Exception when others then
9496   g_status_message:='Error in find_skip_attributes_fk '||sqlerrm||get_time;
9497   write_to_log_file_n(g_status_message);
9498   g_status:=false;
9499   return false;
9500 End;
9501 
9502 function log_collection_start(
9503 p_object_name varchar2,
9504 p_object_id number,
9505 p_object_type varchar2,
9506 p_start_date date,
9507 p_conc_program_id number,
9508 p_load_pk number
9509 ) return boolean is
9510 l_stmt varchar2(2000);
9511 TYPE CurTyp IS REF CURSOR;
9512 cv   CurTyp;
9513 l_message varchar2(2000);
9514 l_load_pk number;
9515 Begin
9516   --first, if there are any with status of processing, make it 'ERROR'
9517   l_load_pk:=null;
9518   l_stmt:='select load_pk from edw_collection_detail_log where COLLECTION_STATUS=:a and object_name=:b '||
9519   'and object_type=:c';
9520   if g_debug then
9521     write_to_log_file_n('Going to execute '||l_stmt||' using ''PROCESSING'','||p_object_name||','||p_object_type);
9522   end if;
9523   open cv for l_stmt using 'PROCESSING',p_object_name,p_object_type;
9524   fetch cv into l_load_pk;
9525   close cv;
9526   if l_load_pk is not null then
9527     if g_debug then
9528       write_to_log_file_n('Found '||l_load_pk||' with PROCESSING, making it ERROR');
9529     end if;
9530     l_message:='Terminated';
9531     if write_to_collection_log(
9532       p_object_name,
9533       p_object_id,
9534       p_object_type,
9535       p_conc_program_id,
9536       p_start_date,
9537       null,
9538       null,
9539       null,
9540       null,
9541       null,
9542       null,
9543       null,
9544       l_message,
9545       'ERROR',
9546       l_load_pk)=false then
9547       write_to_log_file_n('write_to_collection_log returned with error');
9548       return false;
9549     end if;
9550   end if;
9551   l_load_pk:=p_load_pk;
9552   write_to_log_file_n('Load PK='||l_load_pk);
9553   if write_to_collection_log(
9554         p_object_name,
9555         p_object_id,
9556         p_object_type,
9557         p_conc_program_id,
9558         p_start_date,
9559         null,
9560         null,
9561         null,
9562         null,
9563         null,
9564         null,
9565         null,
9566         null,
9567         'PROCESSING',
9568         l_load_pk)=false then
9569     write_to_log_file_n('write_to_collection_log returned with error');
9570     return false;
9571   end if;
9572   commit;
9573   return true;
9574 Exception when others then
9575   g_status_message:=sqlerrm;
9576   g_status:=false;
9577   write_to_log_file_n(g_status_message);
9578   return false;
9579 End;
9580 
9581 function get_temp_log_data(
9582 p_object_name varchar2,
9583 p_object_type varchar2,
9584 p_load_pk number,
9585 p_ins_rows_ready out nocopy number,
9586 p_ins_rows_processed out nocopy number,
9587 p_ins_rows_collected out nocopy number,
9588 p_ins_rows_dangling out nocopy number,
9589 p_ins_rows_duplicate out nocopy number,
9590 p_ins_rows_error out nocopy number,
9591 p_ins_rows_insert out nocopy number,
9592 p_ins_rows_update out nocopy number,
9593 p_ins_rows_delete out nocopy number,
9594 p_ins_instance_name out nocopy varchar2,
9595 p_ins_request_id_table out nocopy varchar2
9596 ) return boolean is
9597 l_stmt varchar2(10000);
9598 TYPE CurTyp IS REF CURSOR;
9599 cv   CurTyp;
9600 l_object_type varchar2(400);
9601 Begin
9602   if g_debug then
9603     write_to_log_file_n('In get_temp_log_data '||p_object_name||'   '||p_object_type);
9604   end if;
9605   if p_load_pk is null then
9606     l_stmt:='select nvl(sum(nvl(rows_ready,0)),0), '||
9607     ' nvl(sum(nvl(rows_collected,0)),0), nvl(sum(nvl(rows_dangling,0)),0), '||
9608     'nvl(sum(nvl(rows_duplicate,0)),0), nvl(sum(nvl(rows_error,0)),0),nvl(sum(nvl(number_insert,0)),0),'||
9609     'nvl(sum(nvl(number_update,0)),0),nvl(sum(nvl(number_delete,0)),0) '||
9610     ' from edw_temp_collection_log where object_name=:a and object_type=:b and status=:c ';
9611      open cv for l_stmt using p_object_name,p_object_type,'OPEN';
9612   else
9613     l_stmt:='select nvl(sum(nvl(rows_ready,0)),0), '||
9614     ' nvl(sum(nvl(rows_collected,0)),0), nvl(sum(nvl(rows_dangling,0)),0), '||
9615     'nvl(sum(nvl(rows_duplicate,0)),0), nvl(sum(nvl(rows_error,0)),0),nvl(sum(nvl(number_insert,0)),0),'||
9616     'nvl(sum(nvl(number_update,0)),0),nvl(sum(nvl(number_delete,0)),0) '||
9617     ' from edw_temp_collection_log where request_id=:1';
9618     open cv for l_stmt using p_load_pk;
9619   end if;
9620   fetch cv into
9621     p_ins_rows_processed,p_ins_rows_collected,p_ins_rows_dangling,p_ins_rows_duplicate,
9622     p_ins_rows_error,p_ins_rows_insert,p_ins_rows_update,p_ins_rows_delete;
9623   close cv;
9624   p_ins_rows_ready:=null;
9625   l_stmt:='select max(NUMBER_READY) from edw_temp_collection_log where object_name=:a and object_type=:b '||
9626   'and status=:c';
9627   open cv for l_stmt using p_object_name,p_object_type,'OPEN';
9628   fetch cv into p_ins_rows_ready;
9629   close cv;
9630   if p_ins_rows_ready is null then
9631     p_ins_rows_ready:=p_ins_rows_processed; --for derived facts
9632   end if;
9633   p_ins_instance_name:=null;
9634   p_ins_request_id_table:=null;
9635   if g_debug then
9636     write_to_log_file_n('The results of query of the table edw_temp_collection_log');
9637     write_to_log_file('Instance   request id   ready  processed    collected  dangling  duplicate  error '||
9638     'insert update delete');
9639     write_to_log_file(p_ins_instance_name||' '||p_ins_request_id_table||' '||
9640     p_ins_rows_ready||' '||p_ins_rows_processed||' '||
9641     p_ins_rows_collected||' '||p_ins_rows_dangling||' '||
9642     p_ins_rows_duplicate||' '||p_ins_rows_error||' '||p_ins_rows_insert||' '||p_ins_rows_update
9643     ||' '||p_ins_rows_delete);
9644   end if;
9645   l_stmt:='delete edw_temp_collection_log where object_name=:a and object_type=:b and status=:c';
9646   if g_debug then
9647     write_to_log_file_n('Going to execute '||l_stmt||' using '||p_object_name||'  '||p_object_type||' OPEN');
9648   end if;
9649   execute immediate l_stmt using p_object_name,p_object_type,'OPEN';
9650   return true;
9651 EXCEPTION when others then
9652   g_status:=false;
9653   g_status_message:=sqlerrm;
9654   write_to_log_file_n(g_status_message);
9655   return false;
9656 End;
9657 
9658 function get_job_queue_processes return number is
9659 Begin
9660   return get_parameter_value('job_queue_processes');
9661 EXCEPTION when others then
9662   write_to_log_file_n(g_status_message);
9663   return null;
9664 End;
9665 
9666 function get_app_long_name(
9667 p_app_name varchar2,
9668 p_app_long_name out nocopy varchar2
9669 ) return boolean is
9670 TYPE CurTyp IS REF CURSOR;
9671 cv   CurTyp;
9672 Begin
9673   g_stmt:='select application_name from fnd_application_vl where application_short_name=:1';
9674   if g_debug then
9675     write_to_log_file_n(g_stmt||' '||p_app_name);
9676   end if;
9677   open cv for g_stmt using p_app_name;
9678   fetch cv into p_app_long_name;
9679   close cv;
9680   if g_debug then
9681     write_to_log_file('p_app_long_name='||p_app_long_name);
9682   end if;
9683   return true;
9684 EXCEPTION when others then
9685   write_to_log_file_n('Error in get_app_long_name '||sqlerrm);
9686   return false;
9687 End;
9688 
9689 function get_max_in_array(p_array numberTableType,p_number_array number,
9690 p_index out nocopy number) return number is
9691 l_max number;
9692 Begin
9693   for i in 1..p_number_array loop
9694     if l_max is null or (p_array(i)>l_max) then
9695       l_max:=p_array(i);
9696       p_index:=i;
9697     end if;
9698   end loop;
9699   if g_debug then
9700     write_to_log_file_n('l_max='||l_max||', p_index='||p_index);
9701   end if;
9702   return l_max;
9703 EXCEPTION when others then
9704   write_to_log_file_n('Error in get_max_in_array '||sqlerrm);
9705   return null;
9706 End;
9707 
9708 function get_min_in_array(p_array numberTableType,p_number_array number,
9709 p_index out nocopy number) return number is
9710 l_min number;
9711 Begin
9712   for i in 1..p_number_array loop
9713     if l_min is null or (p_array(i)<l_min) then
9714       l_min:=p_array(i);
9715       p_index:=i;
9716     end if;
9717   end loop;
9718   if g_debug then
9719     write_to_log_file_n('l_min='||l_min||', p_index='||p_index);
9720   end if;
9721   return l_min;
9722 EXCEPTION when others then
9723   write_to_log_file_n('Error in get_max_in_array '||sqlerrm);
9724   return null;
9725 End;
9726 
9727 function create_input_table_push_down(
9728   p_input_table varchar2,
9729   p_dim_name varchar2,
9730   p_dim_id number,
9731   p_levels varcharTableType,
9732   p_child_level_number numberTableType,
9733   p_child_levels varcharTableType,
9734   p_child_fk varcharTableType,
9735   p_parent_pk varcharTableType,
9736   p_number_levels number,
9737   p_level_order varcharTableType,
9738   p_level_snapshot_logs varcharTableType,
9739   p_level_ilog varcharTableType,
9740   p_level_consider booleanTableType,
9741   p_level_full_insert booleanTableType,
9742   p_debug boolean,
9743   p_parallel number,
9744   p_collection_size number,
9745   p_bis_owner  varchar2,
9746   p_table_owner varchar2,
9747   p_full_refresh boolean,
9748   p_forall_size number,
9749   p_update_type varchar2,
9750   p_load_pk number,
9751   p_op_table_space varchar2,
9752   p_rollback varchar2,
9753   p_max_threads number,
9754   p_min_job_load_size number,
9755   p_sleep_time number,
9756   p_hash_area_size number,
9757   p_sort_area_size number,
9758   p_trace boolean,
9759   p_read_cfig_options boolean,
9760   p_join_nl_percentage number
9761 ) return boolean is
9762 l_level_table varchar2(80);
9763 l_level_child_table varchar2(80);
9764 l_level_consider varchar2(10);
9765 l_level_full_insert varchar2(10);
9766 l_debug varchar2(10);
9767 l_full_refresh varchar2(10);
9768 l_trace varchar2(10);
9769 l_read_cfig_options varchar2(10);
9770 l_run number;
9771 Begin
9772   if g_debug then
9773     write_to_log_file_n('In create_input_table_push_down');
9774   end if;
9775   l_level_table:=p_input_table||'_LT';
9776   l_level_child_table:=p_input_table||'_LC';
9777   g_stmt:='create table '||p_input_table||'('||
9778   'dim_name varchar2(200),'||
9779   'dim_id number,'||
9780   'debug varchar2(10),'||
9781   'parallel number,'||
9782   'collection_size number,'||
9783   'bis_owner  varchar2(200),'||
9784   'table_owner varchar2(200),'||
9785   'full_refresh varchar2(10),'||
9786   'forall_size number,'||
9787   'update_type varchar2(200),'||
9788   'load_pk number,'||
9789   'op_table_space varchar2(200),'||
9790   'rollback varchar2(200),'||
9791   'max_threads number,'||
9792   'min_job_load_size number,'||
9793   'sleep_time number,'||
9794   'hash_area_size number,'||
9795   'sort_area_size number,'||
9796   'trace varchar2(10),'||
9797   'read_cfig_options varchar2(10),'||
9798   'join_nl_percentage number'||
9799   ') tablespace '||p_op_table_space;
9800   if drop_table(p_input_table)=false then
9801     null;
9802   end if;
9803   if g_debug then
9804     write_to_log_file_n(g_stmt);
9805   end if;
9806   execute immediate g_stmt;
9807   l_debug:='N';
9808   l_full_refresh:='N';
9809   l_trace:='N';
9810   l_read_cfig_options:='N';
9811   if p_debug then
9812     l_debug:='Y';
9813   end if;
9814   if p_full_refresh then
9815     l_full_refresh:='Y';
9816   end if;
9817   if p_trace then
9818     l_trace:='Y';
9819   end if;
9820   if p_read_cfig_options then
9821     l_read_cfig_options:='Y';
9822   end if;
9823   g_stmt:='insert into '||p_input_table||'('||
9824   'dim_name'||
9825   ',dim_id'||
9826   ',debug'||
9827   ',parallel'||
9828   ',collection_size'||
9829   ',bis_owner'||
9830   ',table_owner'||
9831   ',full_refresh'||
9832   ',forall_size'||
9833   ',update_type'||
9834   ',load_pk'||
9835   ',op_table_space'||
9836   ',rollback'||
9837   ',max_threads'||
9838   ',min_job_load_size'||
9839   ',sleep_time'||
9840   ',hash_area_size'||
9841   ',sort_area_size'||
9842   ',trace'||
9843   ',read_cfig_options'||
9844   ',join_nl_percentage'||
9845   ') values('||
9846   ':1,:2,:3,:4,:5,:6,:7,:8,:9,:10,'||
9847   ':11,:12,:13,:14,:15,:16,:17,:18,:19,:20,'||
9848   ':21'||
9849   ')';
9850   if g_debug then
9851     write_to_log_file_n(g_stmt);
9852   end if;
9853   execute immediate g_stmt using
9854   p_dim_name,
9855   p_dim_id,
9856   l_debug,
9857   p_parallel,
9858   p_collection_size,
9859   p_bis_owner,
9860   p_table_owner,
9861   l_full_refresh,
9862   p_forall_size,
9863   p_update_type,
9864   p_load_pk,
9865   p_op_table_space,
9866   p_rollback,
9867   p_max_threads,
9868   p_min_job_load_size,
9869   p_sleep_time,
9870   p_hash_area_size,
9871   p_sort_area_size,
9872   l_trace,
9873   l_read_cfig_options,
9874   p_join_nl_percentage;
9875   commit;
9876   g_stmt:='create table '||l_level_table||'('||
9877   'level_number number,'||
9878   'levels varchar2(200),'||
9879   'child_level_number number,'||
9880   'child_levels varchar2(200),'||
9881   'child_fk varchar2(200),'||
9882   'parent_pk varchar2(200),'||
9883   'level_order varchar2(200),'||
9884   'level_snapshot_logs varchar2(200),'||
9885   'level_ilog varchar2(200),'||
9886   'level_consider varchar2(10),'||
9887   'level_full_insert varchar2(10)'||
9888   ') tablespace '||p_op_table_space;
9889   if drop_table(l_level_table)=false then
9890     null;
9891   end if;
9892   if g_debug then
9893     write_to_log_file_n(g_stmt);
9894   end if;
9895   execute immediate g_stmt;
9896   g_stmt:='insert into '||l_level_table||'('||
9897   'level_number'||
9898   ',levels'||
9899   ',child_level_number'||
9900   ',level_order'||
9901   ',level_snapshot_logs'||
9902   ',level_ilog'||
9903   ',level_consider'||
9904   ',level_full_insert'||
9905   ') values ('||
9906   ':1,:2,:3,:4,:5,:6,:7,:8'||
9907   ')';
9908   if g_debug then
9909     write_to_log_file_n(g_stmt);
9910   end if;
9911   for i in 1..p_number_levels loop
9912     l_level_consider:='N';
9913     if p_level_consider(i) then
9914       l_level_consider:='Y';
9915     end if;
9916     l_level_full_insert:='N';
9917     if p_level_full_insert(i) then
9918       l_level_full_insert:='Y';
9919     end if;
9920     execute immediate g_stmt using
9921     i
9922     ,p_levels(i)
9923     ,p_child_level_number(i)
9924     ,p_level_order(i)
9925     ,p_level_snapshot_logs(i)
9926     ,p_level_ilog(i)
9927     ,l_level_consider
9928     ,l_level_full_insert;
9929   end loop;
9930   commit;
9931   g_stmt:='create table '||l_level_child_table||'('||
9932   'run_number number,'||
9933   'child_levels varchar2(200),'||
9934   'child_fk varchar2(200),'||
9935   'parent_pk varchar2(200)'||
9936   ') tablespace '||p_op_table_space;
9937   if drop_table(l_level_child_table)=false then
9938     null;
9939   end if;
9940   if g_debug then
9941     write_to_log_file_n(g_stmt);
9942   end if;
9943   execute immediate g_stmt;
9944   g_stmt:='insert into '||l_level_child_table||'('||
9945   'run_number,'||
9946   'child_levels,'||
9947   'child_fk,'||
9948   'parent_pk'||
9949   ') values('||
9950   ':1,:2,:3,:4'||
9951   ')';
9952   if g_debug then
9953     write_to_log_file_n('Going to execute '||g_stmt||' '||get_time);
9954   end if;
9955   l_run:=0;
9956   for i in 1..p_number_levels loop
9957     for j in 1..p_child_level_number(i) loop
9958       l_run:=l_run+1;
9959       execute immediate g_stmt using l_run,p_child_levels(l_run),p_child_fk(l_run),p_parent_pk(l_run);
9960     end loop;
9961   end loop;
9962   commit;
9963   return true;
9964 EXCEPTION when others then
9965   g_status_message:=sqlerrm;
9966   write_to_log_file_n('Error in get_app_long_name '||sqlerrm);
9967   return false;
9968 End;
9969 
9970 function make_ilog_from_main_ilog(
9971 p_ilog_rowid_table varchar2,
9972 p_ilog_table varchar2,
9973 p_low_end number,
9974 p_high_end number,
9975 p_op_table_space varchar2,
9976 p_bis_owner varchar2,
9977 p_parallel number,
9978 p_ilog_rowid_number out nocopy number
9979 ) return boolean is
9980 l_cols varcharTableType;
9981 l_number_cols number;
9982 l_table varchar2(80);
9983 Begin
9984   if g_debug then
9985     write_to_log_file_n('In make_ilog_from_main_ilog '||p_ilog_table||' '||p_low_end||' '||p_high_end);
9986   end if;
9987   if get_db_columns_for_table(
9988     p_ilog_table,
9989     l_cols,
9990     l_number_cols,
9991     p_bis_owner)=false then
9992     return false;
9993   end if;
9994   p_ilog_rowid_number:=0;
9995   if g_debug then
9996     write_to_log_file('The ILOG columns');
9997     for i in 1..l_number_cols loop
9998       write_to_log_file(l_cols(i));
9999     end loop;
10000   end if;
10001   g_stmt:='create table '||p_ilog_rowid_table||' tablespace '||p_op_table_space;
10002   g_stmt:=g_stmt||' storage (initial 4M next 4M pctincrease 0) ';
10003   if g_parallel is not null then
10004     g_stmt:=g_stmt||' parallel (degree '||p_parallel||') ';
10005   end if;
10006   g_stmt:=g_stmt||' as select ';
10007   for i in 1..l_number_cols loop
10008     if l_cols(i)<>'ROW_NUM' then
10009       g_stmt:=g_stmt||l_cols(i)||',';
10010     end if;
10011   end loop;
10012   g_stmt:=substr(g_stmt,1,length(g_stmt)-1);
10013   g_stmt:=g_stmt||' from '||p_ilog_table||' where row_num between '||
10014   p_low_end||' and '||p_high_end;
10015   if EDW_OWB_COLLECTION_UTIL.drop_table(p_ilog_rowid_table)=false then
10016     null;
10017   end if;
10018   if g_debug then
10019     write_to_log_file_n(g_stmt||get_time);
10020   end if;
10021   execute immediate g_stmt;
10022   p_ilog_rowid_number:=sql%rowcount;
10023   if g_debug then
10024     write_to_log_file_n('Created with '||p_ilog_rowid_number||' rows '||get_time);
10025   end if;
10026   EDW_OWB_COLLECTION_UTIL.analyze_table_stats(substr(p_ilog_rowid_table,instr(p_ilog_rowid_table,'.')+1,
10027   length(p_ilog_rowid_table)),substr(p_ilog_rowid_table,1,instr(p_ilog_rowid_table,'.')-1));
10028   return true;
10029 Exception when others then
10030   g_status_message:=sqlerrm;
10031   write_to_log_file_n('Error in make_ilog_from_main_ilog '||g_status_message);
10032   return false;
10033 End;
10034 
10035 procedure create_status_table(p_table varchar2,p_op_table_space varchar2,
10036 p_status varchar2,p_count number) is
10037 Begin
10038   g_stmt:='create table '||p_table||' tablespace '||p_op_table_space||
10039   ' as select '''||p_status||''' status,'||p_count||' count from dual';
10040   if drop_table(p_table)=false then
10041     null;
10042   end if;
10043   if g_debug then
10044     write_to_log_file_n(g_stmt);
10045   end if;
10046   execute immediate g_stmt;
10047 Exception when others then
10048   g_status_message:=sqlerrm;
10049   write_to_log_file_n('Error in create_status_table '||g_status_message);
10050 End;
10051 
10052 function create_sequence(
10053 p_seq varchar2,
10054 p_owner varchar2,
10055 p_start_with number,
10056 p_flag varchar2
10057 ) return boolean is
10058 l_owner varchar2(80);
10059 l_num number;
10060 l_seq_flag boolean;--create or not to create the seq
10061 Begin
10062   if g_debug then
10063     write_to_log_file_n('In create_sequence '||p_seq||' '||p_owner||' '||p_start_with||get_time);
10064   end if;
10065   l_owner:=p_owner;
10066   l_seq_flag:=true;
10067   <<start_seq_creation>>
10068   if l_owner is null then
10069     l_owner:=get_table_owner(p_seq);
10070   end if;
10071   if p_flag<>'FORCE' then
10072     begin
10073       l_num:=get_seq_nextval(p_seq);
10074       if l_num is not null then
10075         if l_num<p_start_with then
10076           if drop_seq(p_seq,l_owner)=false then
10077             null;
10078           end if;
10079         else
10080           l_seq_flag:=false;
10081         end if;
10082       end if;
10083     exception when others then
10084       write_to_log_file_n('This error mesg '||sqlerrm);
10085     end;
10086   end if;
10087   if l_seq_flag then
10088     if l_owner is null then
10089       g_stmt:='create sequence '||p_seq||' start with '||(p_start_with+1);
10090     else
10091       g_stmt:='create sequence '||l_owner||'.'||p_seq||' start with '||(p_start_with+1);
10092     end if;
10093     begin
10094       if g_debug then
10095         write_to_log_file_n(g_stmt);
10096       end if;
10097       execute immediate g_stmt;
10098     exception when others then
10099       if p_flag<>'FORCE' then
10100         if sqlcode=-00955 then
10101           if g_debug then
10102             write_to_log_file_n('Sequence already created!');
10103           end if;
10104           goto start_seq_creation;
10105         end if;
10106       end if;
10107     end;
10108   end if;
10109   return true;
10110 Exception when others then
10111   g_status_message:=sqlerrm;
10112   write_to_log_file_n('Error in create_sequence '||g_status_message);
10113   return false;
10114 End;
10115 
10116 function drop_seq(p_seq varchar2,p_owner varchar2) return boolean is
10117 l_owner varchar2(80);
10118 Begin
10119   l_owner:=p_owner;
10120   if l_owner is null then
10121     l_owner:=get_table_owner(p_seq);
10122   end if;
10123   if l_owner is not null then
10124     g_stmt:='drop sequence '||l_owner||'.'||p_seq;
10125   else
10126     g_stmt:='drop sequence '||p_seq;
10127   end if;
10128   if g_debug then
10129     write_to_log_file_n(g_stmt);
10130   end if;
10131   execute immediate g_stmt;
10132   return true;
10133 Exception when others then
10134   write_to_log_file_n('Error in drop_seq '||g_status_message);
10135   return false;
10136 End;
10137 
10138 function get_seq_nextval(p_seq varchar2) return number is
10139 l_num number;
10140 l_stmt varchar2(200);
10141 TYPE CurTyp IS REF CURSOR;
10142 cv   CurTyp;
10143 Begin
10144   l_stmt:='select '||p_seq||'.NEXTVAL from dual';
10145   if g_debug then
10146     write_to_log_file_n(l_stmt);
10147   end if;
10148   open cv for l_stmt;
10149   fetch cv into l_num;
10150   close cv;
10151   if g_debug then
10152     write_to_log_file(l_num);
10153   end if;
10154   return l_num;
10155 Exception when others then
10156   write_to_log_file_n('Error in get_seq_nextval '||g_status_message);
10157   return null;
10158 End;
10159 
10160 function get_max_value(p_table varchar2,p_col varchar2) return number is
10161 TYPE CurTyp IS REF CURSOR;
10162 cv   CurTyp;
10163 l_max number;
10164 Begin
10165   g_stmt:='select max('||p_col||') from '||p_table;
10166   if g_debug then
10167     write_to_log_file_n(g_stmt||get_time);
10168   end if;
10169   open cv for g_stmt;
10170   fetch cv into l_max;
10171   if g_debug then
10172     write_to_log_file_n('l_max='||l_max||get_time);
10173   end if;
10174   return l_max;
10175 Exception when others then
10176   write_to_log_file_n('Error in get_max_value '||g_status_message);
10177   return null;
10178 End;
10179 
10180 --FSTG or LSTG map fk details ONLY
10181 function get_stg_map_fk_details(
10182 p_fstg_usage_id number,
10183 p_fstg_id number,
10184 p_mapping_id number,
10185 p_job_id number,
10186 p_op_tablespace varchar2,
10187 p_bis_owner varchar2,
10188 p_dimTableName out nocopy varcharTableType,
10189 p_dim_row_count out nocopy numberTableType,
10190 p_dimTableId out nocopy numberTableType,
10191 p_dimUserPKName out nocopy varcharTableType,
10192 p_fstgUserFKName out nocopy varcharTableType,
10193 p_factFKName out nocopy varcharTableType,
10194 p_numberOfDimTables out nocopy number
10195 )return boolean is
10196 ------------------------------------------------------------
10197 --Added for bug#6129040
10198 
10199 CURSOR getRows(p_mapping_id NUMBER,p_fstg_usage_id NUMBER) is
10200 select
10201     dim.relation_name,
10202     dim.relation_id,
10203     dim_pk_item.column_name dim_col,
10204     fstg_item.column_name fstg_col,
10205     fact_item.column_name fact_col
10206     from
10207     (select Source_usage_id,Target_column_id from edw_pvt_map_columns_md_v
10208     where mapping_id=p_mapping_id) map_columns,
10209     (select edw_pvt_map_key_usages_md_v.Source_usage_id,
10210     edw_pvt_map_key_usages_md_v.Unique_key_id,
10211     edw_pvt_map_key_usages_md_v.foreign_key_id,
10212     edw_pvt_map_key_usages_md_v.Parent_table_usage_id
10213     from
10214     edw_pvt_map_key_usages_md_v,edw_pvt_map_sources_md_v
10215     where edw_pvt_map_sources_md_v.source_usage_id=edw_pvt_map_key_usages_md_v.parent_table_usage_id
10216     and edw_pvt_map_key_usages_md_v.Source_usage_id=p_fstg_usage_id
10217     and edw_pvt_map_key_usages_md_v.mapping_id=p_mapping_id
10218     and edw_pvt_map_sources_md_v.mapping_id=p_mapping_id
10219     ) key_usage,
10220     edw_pvt_key_columns_md_v key_col,
10221     edw_pvt_key_columns_md_v fk_key_col,
10222     edw_unique_keys_md_v dim_pk,
10223     edw_relations_md_v dim,
10224     edw_pvt_columns_md_v dim_pk_item,
10225     edw_pvt_columns_md_v fstg_item,
10226     edw_pvt_columns_md_v fact_item
10227     where
10228     dim_pk.key_id=key_usage.Unique_key_id
10229     and dim.relation_id=dim_pk.entity_id
10230     and key_col.key_id=dim_pk.key_id
10231     and dim_pk_item.column_id=key_col.column_id
10232     and fk_key_col.key_id=key_usage.foreign_key_id
10233     and fstg_item.column_id=fk_key_col.column_id
10234     and map_columns.Source_usage_id=key_usage.Parent_table_usage_id
10235     and map_columns.Target_column_id=fact_item.column_id ;
10236 ------------------------------------------------------------
10237 l_rows_with_problem booleanTableType;
10238 l_rows_with_problem_found boolean;
10239 ------------------------------------------------------------
10240 l_stmt VARCHAR2(200);
10241 
10242 rowCount NUMBER;
10243 l_relation_name VARCHAR2(255);
10244 l_relation_id NUMBER(9);
10245 l_dim_col VARCHAR2(255);
10246 l_fstg_col VARCHAR2(255);
10247 l_fact_col VARCHAR2(255);
10248 
10249 
10250 ------------------------------------------------------------
10251 TYPE CurTyp IS REF CURSOR;
10252 cv   CurTyp;
10253 l_found boolean;
10254 ------------------------------------------------------------
10255 l_stg_fk varcharTableType;
10256 l_stg_dim varcharTableType;
10257 l_number_stg number;
10258 ------------------------------------------------------------
10259 l_table1 varchar2(200);
10260 l_table2 varchar2(200);
10261 l_table3 varchar2(200);
10262 l_table4 varchar2(200);
10263 l_table5 varchar2(200);
10264 ------------------------------------------------------------
10265 Begin
10266   if g_debug then
10267     write_to_log_file_n('In get_stg_map_fk_details '||p_fstg_usage_id||' '||p_fstg_id||' '||
10268     p_mapping_id);
10269   end if;
10270   --l_table1:=p_bis_owner||'.TAB_'||p_mapping_id||'_'||p_job_id||'_GET_FK_1';
10271   --l_table2:=p_bis_owner||'.TAB_'||p_mapping_id||'_'||p_job_id||'_GET_FK_2';
10272   --l_table3:=p_bis_owner||'.TAB_'||p_mapping_id||'_'||p_job_id||'_GET_FK_3';
10273   --l_table4:=p_bis_owner||'.TAB_'||p_mapping_id||'_'||p_job_id||'_GET_FK_4';
10274   --l_table5:=p_bis_owner||'.TAB_'||p_mapping_id||'_'||p_job_id||'_GET_FK_5';
10275   l_table1:=p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_1';
10276   l_table2:=p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_2';
10277   l_table3:=p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_3';
10278   l_table4:=p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_4';
10279   l_table5:=p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_5';--dependence on drop_stg_map_fk_details
10280   if p_job_id is null then --this is the main process
10281     if drop_table(l_table1)=false then
10282       null;
10283     end if;
10284     if drop_table(l_table2)=false then
10285       null;
10286     end if;
10287     if drop_table(l_table3)=false then
10288       null;
10289     end if;
10290     if drop_table(l_table4)=false then
10291       null;
10292     end if;
10293     if drop_table(l_table5)=false then
10294       null;
10295     end if;
10296     --Added for 6129040, Instead of inserting directly into the table using a select statement, here a cursor is used and inserted row by row
10297    -- into the table to avoid multiple inserts into the table in parallel.
10298     g_stmt:='create table  '||l_table1||
10299     ' (relation_name VARCHAR(255),'||
10300     ' relation_id NUMBER(9),'||
10301     ' dim_col VARCHAR(255),'||
10302     ' fstg_col VARCHAR(255),'||
10303     ' fact_col VARCHAR(255)) tablespace '||p_op_tablespace;
10304 
10305     if g_debug then
10306       write_to_log_file_n(g_stmt||get_time);
10307     end if;
10308     execute immediate g_stmt;
10309       write_to_log_file_n('Executed '||g_stmt||' '||get_time);
10310     l_stmt := 'INSERT INTO '||l_table1||' VALUES(:1,:2,:3,:4,:5)';
10311     rowCount := 0;
10312     write_to_log_file('Mapping id : '||p_mapping_id||' Fstg Usage Id:'||p_fstg_usage_id);
10313     OPEN getRows(p_mapping_id,p_fstg_usage_id);
10314     LOOP
10315       FETCH getRows INTO l_relation_name,l_relation_id,l_dim_col,l_fstg_col,l_fact_col;
10316       EXIT when getRows%NOTFOUND;
10317       EXECUTE IMMEDIATE l_stmt USING l_relation_name,l_relation_id,l_dim_col,l_fstg_col,l_fact_col;
10318       rowCount := rowCount + 1;
10319     END LOOP;
10320     CLOSE getRows;
10321     if g_debug then
10322       write_to_log_file_n('Created using cursors->'||rowCount||' <-rows '||get_time);
10323     end if;
10324     g_stmt:='create table '||l_table2||' tablespace '||p_op_tablespace||
10325     ' as select distinct relation_name from '||l_table1;
10326     if g_debug then
10327       write_to_log_file_n(g_stmt||get_time);
10328     end if;
10329     execute immediate g_stmt;
10330     if g_debug then
10331       write_to_log_file_n('Created with '||sql%rowcount||' rows '||get_time);
10332     end if;
10333     g_stmt:='create table '||l_table3||' tablespace '||p_op_tablespace||
10334     ' as select syn.table_owner,tab.relation_name from user_synonyms syn,'||l_table2||' tab '||
10335     'where tab.relation_name=syn.synonym_name(+)';
10336     if g_debug then
10337       write_to_log_file_n(g_stmt||get_time);
10338     end if;
10339     execute immediate g_stmt;
10340     if g_debug then
10341       write_to_log_file_n('Created with '||sql%rowcount||' rows '||get_time);
10342     end if;
10343     g_stmt:='create table '||l_table4||' tablespace '||p_op_tablespace||
10344     ' as select syn.table_owner,tab.* from '||l_table3||' syn,'||l_table1||' tab '||
10345     'where tab.relation_name=syn.relation_name';
10346     if g_debug then
10347       write_to_log_file_n(g_stmt||get_time);
10348     end if;
10349     execute immediate g_stmt;
10350     if g_debug then
10351       write_to_log_file_n('Created table4 with  '||sql%rowcount||' rows '||get_time);
10352     end if;
10353     g_stmt:='create table '||l_table5||' tablespace '||p_op_tablespace||
10354     ' as select all_tab.NUM_ROWS,tab.table_owner,tab.relation_name,tab.relation_id,'||
10355     'tab.dim_col,tab.fstg_col,tab.fact_col from '||l_table4||' tab, all_tables all_tab '||
10356     'where all_tab.table_name=tab.relation_name and all_tab.owner=tab.table_owner '||
10357     'and tab.table_owner is not null '||
10358     'union all '||
10359     'select 0,tab.table_owner,tab.relation_name,tab.relation_id,'||
10360     'tab.dim_col,tab.fstg_col,tab.fact_col from '||l_table4||' tab where tab.table_owner is null ';
10361     execute immediate g_stmt;
10362     if g_debug then
10363       write_to_log_file_n('Created with '||sql%rowcount||' rows '||get_time);
10364     end if;
10365   end if;
10366 
10367   g_stmt:='select relation_name,num_rows,relation_id,dim_col,fstg_col,fact_col from '||l_table5||
10368   ' order by num_rows,relation_name';
10369   p_numberOfDimTables:=1;
10370   if g_debug then
10371     write_to_log_file_n(g_stmt||get_time);
10372   end if;
10373   open cv for g_stmt;
10374   loop
10375     fetch cv into
10376     p_dimTableName(p_numberOfDimTables),
10377     p_dim_row_count(p_numberOfDimTables),
10378     p_dimTableId(p_numberOfDimTables),
10379     p_dimUserPKName(p_numberOfDimTables),
10380     p_fstgUserFKName(p_numberOfDimTables),
10381     p_factFKName(p_numberOfDimTables);
10382     exit when cv%notfound;
10383     l_rows_with_problem(p_numberOfDimTables):=false;
10384     p_numberOfDimTables:=p_numberOfDimTables+1;
10385   end loop;
10386   p_numberOfDimTables:=p_numberOfDimTables-1;
10387   if g_debug then
10388     write_to_log_file_n('The results of the FK read '||get_time);
10389     for i in 1..p_numberOfDimTables loop
10390       write_to_log_file(p_fstgUserFKName(i)||' '||p_dimTableName(i)||'('||p_dim_row_count(i)||') '||
10391       p_dimUserPKName(i)||' '||p_factFKName(i));
10392     end loop;
10393   end if;
10394   if p_job_id is null then
10395     if drop_table(l_table1)=false then
10396       null;
10397     end if;
10398     if drop_table(l_table2)=false then
10399       null;
10400     end if;
10401     if drop_table(l_table3)=false then
10402       null;
10403     end if;
10404     if drop_table(l_table4)=false then
10405       null;
10406     end if;
10407     --table preserved for child processes
10408     --if drop_table(l_table5)=false then
10409       --null;
10410     --end if;
10411   end if;
10412   --see if any correction is reqd for this metadata read.
10413   --this comes from bug 2739489
10414   --sometimes input group names clash in the src and tgt rep. during import the metadata gets messed up
10415   --go through the fact names and fstg names. if there is a descripency, then go into the stg table and
10416   --see the fk names
10417   if g_debug then
10418     write_to_log_file_n('Scan the FK to see if there is any problem');
10419   end if;
10420   l_rows_with_problem_found:=false;
10421   for i in 1..p_numberOfDimTables loop
10422     if instr(p_factFKName(i),p_fstgUserFKName(i))=0 then --there is no match in name
10423       if g_debug then
10424         write_to_log_file('Problem for '||p_factFKName(i)||' '||p_fstgUserFKName(i));
10425       end if;
10426       l_rows_with_problem(i):=true;
10427       l_rows_with_problem_found:=true;
10428     end if;
10429   end loop;
10430   if l_rows_with_problem_found then
10431     g_stmt:='select '||
10432     'fk_col.column_name, '||
10433     'p_table.relation_name '||
10434     'from '||
10435     'edw_relations_md_v fstg, '||
10436     'edw_foreign_keys_md_v fk, '||
10437     'EDW_PVT_KEY_COLUMNS_MD_V fku, '||
10438     'edw_pvt_columns_md_v fk_col, '||
10439     'edw_relations_md_v p_table, '||
10440     'edw_unique_keys_md_v pk '||
10441     'where '||
10442     'fstg.relation_id=fk.entity_id '||
10443     'and fk.foreign_key_id=fku.key_id '||
10444     'and fk_col.column_id=fku.column_id '||
10445     'and fk_col.parent_object_id=fstg.relation_id '||
10446     'and pk.key_id=fk.key_id '||
10447     'and pk.entity_id=p_table.relation_id '||
10448     'and fstg.relation_id=:1 ';
10449     l_number_stg:=1;
10450     if g_debug then
10451       write_to_log_file_n(g_stmt||' '||p_fstg_id||' '||get_time);
10452     end if;
10453     open cv for g_stmt using p_fstg_id;
10454     loop
10455       fetch cv into l_stg_fk(l_number_stg),l_stg_dim(l_number_stg);
10456       exit when cv%notfound;
10457       l_number_stg:=l_number_stg+1;
10458     end loop;
10459     l_number_stg:=l_number_stg-1;
10460     if g_debug then
10461       write_to_log_file_n('Staging fk');
10462       for i in 1..l_number_stg loop
10463         write_to_log_file(l_stg_fk(i)||' '||l_stg_dim(i));
10464       end loop;
10465     end if;
10466     for i in 1..p_numberOfDimTables loop
10467       if l_rows_with_problem(i) then
10468         l_found:=false;
10469         for j in 1..l_number_stg loop
10470           if l_stg_dim(j)=p_dimTableName(i) then
10471             if instr(p_factFKName(i),l_stg_fk(j))=1 then --found a match
10472               p_fstgUserFKName(i):=l_stg_fk(j);
10473               l_found:=true;
10474               exit;
10475             end if;
10476           end if;
10477         end loop;
10478         if l_found=false then
10479           if g_debug then
10480             write_to_log_file_n('For key '||p_factFKName(i)||' could not get a corresponding fk from '||
10481             'staging table');
10482           end if;
10483         end if;
10484       end if;
10485     end loop;
10486     if g_debug then
10487       write_to_log_file_n('After Corrections the FK Info '||get_time);
10488       for i in 1..p_numberOfDimTables loop
10489         write_to_log_file(p_fstgUserFKName(i)||' '||p_dimTableName(i)||'('||p_dim_row_count(i)||') '||
10490         p_dimUserPKName(i)||' '||p_factFKName(i));
10491       end loop;
10492     end if;
10493   end if;
10494   return true;
10495 Exception when others then
10496   g_status_message:=sqlerrm;
10497   write_to_log_file_n('Error in get_stg_map_fk_details '||g_status_message);
10498   return false;
10499 End;
10500 
10501 function drop_stg_map_fk_details(
10502 p_bis_owner varchar2,
10503 p_mapping_id number
10504 )return boolean is
10505 Begin
10506   if drop_table(p_bis_owner||'.TAB_'||p_mapping_id||'__GET_FK_5')=false then
10507     null;
10508   end if;
10509   return true;
10510 Exception when others then
10511   g_status_message:=sqlerrm;
10512   write_to_log_file_n('Error in drop_stg_map_fk_details '||g_status_message);
10513   return false;
10514 End;
10515 
10516 function get_stg_map_pk_params(
10517 p_mapping_id number,
10518 p_fstgTableUsageId out nocopy number,
10519 p_fstgTableId out nocopy number,
10520 p_fstgTableName out nocopy varchar2,
10521 p_factTableUsageId out nocopy number,
10522 p_factTableId out nocopy number,
10523 p_factTableName out nocopy varchar2,
10524 p_fstgPKName out nocopy varchar2,
10525 p_factPKName out nocopy varchar2
10526 ) return boolean is
10527 TYPE CurTyp IS REF CURSOR;
10528 cv   CurTyp;
10529 Begin
10530   if g_debug then
10531     write_to_log_file_n('In get_stg_map_pk_params '||p_mapping_id);
10532   end if;
10533   g_stmt:='select '||
10534   'map_sources.source_usage_id, '||
10535   'map_sources.source_id, '||
10536   'src_table.name, '||
10537   'map_targets.target_usage_id, '||
10538   'map_targets.target_id, '||
10539   'tgt_relation.relation_name, '||
10540   'pk_col.column_name, '||
10541   'tgt_column.column_name '||
10542   'from '||
10543   'edw_pvt_map_properties_md_v map_properties, '||
10544   'edw_pvt_map_sources_md_v map_sources, '||
10545   'edw_pvt_map_targets_md_v map_targets, '||
10546   'edw_tables_md_v src_table, '||
10547   'edw_relations_md_v tgt_relation, '||
10548   'edw_unique_keys_md_v pk, '||
10549   'edw_pvt_key_columns_md_v key_usage, '||
10550   'edw_pvt_columns_md_v pk_col, '||
10551   '(select * from edw_pvt_map_columns_md_v where mapping_id=:1) map_columns, '||
10552   'edw_pvt_columns_md_v tgt_column '||
10553   'where '||
10554   'map_properties.mapping_id=:2 '||
10555   'and map_sources.source_id=map_properties.primary_source '||
10556   'and map_targets.target_id=map_properties.primary_target '||
10557   'and map_sources.mapping_id=map_properties.mapping_id '||
10558   'and src_table.elementid=map_sources.source_id '||
10559   'and map_targets.mapping_id=map_properties.mapping_id '||
10560   'and tgt_relation.relation_id=map_targets.target_id '||
10561   'and pk.entity_id=map_properties.primary_source '||
10562   'and key_usage.key_id=pk.key_id '||
10563   'and pk_col.column_id=key_usage.column_id '||
10564   'and map_columns.Source_usage_id=map_sources.source_usage_id '||
10565   'and map_columns.Source_column_id=pk_col.column_id '||
10566   'and map_columns.Target_column_id=tgt_column.column_id ';
10567   if g_debug then
10568     write_to_log_file_n(g_stmt||' '||p_mapping_id||' '||get_time);
10569   end if;
10570   open cv for g_stmt using p_mapping_id,p_mapping_id;
10571   fetch cv into
10572   p_fstgTableUsageId,
10573   p_fstgTableId,
10574   p_fstgTableName,
10575   p_factTableUsageId,
10576   p_factTableId,
10577   p_factTableName,
10578   p_fstgPKName,
10579   p_factPKName;
10580   close cv;
10581   return true;
10582 Exception when others then
10583   g_status_message:=sqlerrm;
10584   write_to_log_file_n('Error in get_stg_map_pk_params '||sqlerrm);
10585   return false;
10586 End;
10587 
10588 function create_conc_program(
10589 p_conc_name varchar2,
10590 p_conc_short_name varchar2,
10591 p_exe_name varchar2,
10592 p_exe_file_name varchar2,
10593 p_bis_short_name varchar2,
10594 p_parameter varcharTableType,
10595 p_parameter_value_set varcharTableType,
10596 p_number_parameters number
10597 ) return boolean is
10598 l_bis_long_name varchar2(240);
10599 l_parameter EDW_OWB_COLLECTION_UTIL.varcharTableType;
10600 l_parameter_value_set EDW_OWB_COLLECTION_UTIL.varcharTableType;
10601 l_number_parameters number;
10602 Begin
10603   if g_debug then
10604     write_to_log_file_n('In create_conc_program '||get_time);
10605     write_to_log_file(p_conc_name||' '||p_conc_short_name ||' '||p_exe_name||' '||
10606     p_exe_file_name||' '||p_bis_short_name);
10607     write_to_log_file('Parameters');
10608     for i in 1..p_number_parameters loop
10609       write_to_log_file(p_parameter(i)||' '||p_parameter_value_set(i));
10610     end loop;
10611   end if;
10612   if get_app_long_name(p_bis_short_name,l_bis_long_name)=false then
10613     g_status_message:=EDW_OWB_COLLECTION_UTIL.g_status_message;
10614     return false;
10615   end if;
10616   if delete_conc_program(p_conc_short_name,p_exe_name,l_bis_long_name,'LONG')=false then
10617     null;
10618   end if;
10619   FND_PROGRAM.executable(
10620     executable=>p_exe_name,
10621     application=>l_bis_long_name,
10622     short_name=>p_exe_name,
10623     description=>p_exe_name,
10624     execution_method=>'PL/SQL Stored Procedure',
10625     execution_file_name=>p_exe_file_name
10626     );
10627   if g_debug then
10628     write_to_log_file_n('Created executable '||p_exe_name);
10629   end if;
10630   FND_PROGRAM.REGISTER(
10631     program=>p_conc_name,
10632     application=>l_bis_long_name,
10633     enabled=>'Y',
10634     short_name=>p_conc_short_name,
10635     description=>p_conc_name,
10636     executable_short_name=>p_exe_name,
10637     executable_application=>l_bis_long_name,
10638     use_in_srs=>'Y',
10639     allow_disabled_values=>'Y'
10640     );
10641   if g_debug then
10642     write_to_log_file_n('Created program '||p_conc_name);
10643   end if;
10644   for i in 1..p_number_parameters loop
10645     FND_PROGRAM.PARAMETER(
10646     program_short_name=>p_conc_short_name,
10647     application=>l_bis_long_name,
10648     sequence=>i,
10649     parameter=>p_parameter(i),
10650     description=>p_parameter(i),
10651     enabled=>'Y',
10652     value_set=>p_parameter_value_set(i),
10653     default_type=>null,
10654     default_value=>null,
10655     required=>'Y',
10656     enable_security=>'N',
10657     range=>null,
10658     display=>'N',
10659     display_size=>10,
10660     description_size=>10,
10661     concatenated_description_size=>10,
10662     prompt=>p_parameter(i)
10663     );
10664   end loop;
10665   if g_debug then
10666     write_to_log_file_n('Created Parameters Complete!!');
10667   end if;
10668   return true;
10669 Exception when others then
10670   g_status_message:=sqlerrm;
10671   write_to_log_file_n('Error in create_conc_program '||g_status_message);
10672   write_to_log_file_n('FND_PROGRAM.MESSAGE='||FND_PROGRAM.MESSAGE);
10673   return false;
10674 End;
10675 
10676 function delete_conc_program(
10677 p_conc_name varchar2,
10678 p_exe_name varchar2,
10679 p_bis_name varchar2,
10680 p_name_type varchar2
10681 ) return boolean is
10682 l_bis_long_name varchar2(400);
10683 Begin
10684   if g_debug then
10685     write_to_log_file_n('In delete_conc_program');
10686   end if;
10687   if p_name_type='LONG' then
10688     l_bis_long_name:=p_bis_name;
10689   else
10690     if get_app_long_name(p_bis_name,l_bis_long_name)=false then
10691       null;
10692     end if;
10693   end if;
10694   FND_PROGRAM.DELETE_PROGRAM(p_conc_name,l_bis_long_name);
10695   FND_PROGRAM.DELETE_EXECUTABLE(p_exe_name,l_bis_long_name);
10696   if g_debug then
10697     write_to_log_file_n('Deleted '||p_conc_name||' '||p_exe_name);
10698   end if;
10699   return true;
10700 Exception when others then
10701   g_status_message:=sqlerrm;
10702   write_to_log_file_n('Error in delete_conc_program '||g_status_message);
10703   write_to_log_file_n('FND_PROGRAM.MESSAGE='||FND_PROGRAM.MESSAGE);
10704   return false;
10705 End;
10706 
10707 ---------------------------------------------
10708 function is_oracle_apps_GT_1159 return boolean is
10709 l_list varcharTableType;
10710 l_number_list number;
10711 l_version_GT_1159 boolean;
10712 Begin
10713   if get_app_version(null,null)=null then --this sets the global variable g_oracle_apps_version
10714     return false;
10715   end if;
10716   if parse_names(g_oracle_apps_version,'.',l_list,l_number_list)=false then
10717     return false;
10718   end if;
10719   if to_number(l_list(1))>11 then
10720     l_version_GT_1159:=true;
10721   elsif to_number(l_list(2))>5 then
10722     l_version_GT_1159:=true;
10723   elsif to_number(l_list(3))>9 then
10724     l_version_GT_1159:=true;
10725   else
10726     l_version_GT_1159:=false;
10727   end if;
10728   if l_version_GT_1159 then
10729     write_to_conc_log_file('Oracle Apps version > 11.5.9');
10730   else
10731     write_to_conc_log_file('Oracle Apps version NOT > 11.5.9');
10732   end if;
10733   return l_version_GT_1159;
10734 Exception when others then
10735   g_status_message:=sqlerrm;
10736   write_to_conc_log_file('Error in is_oracle_apps_GT_1159 '||g_status_message);
10737   return false;
10738 End;
10739 
10740 procedure init_all(
10741 p_object_name varchar2,
10742 p_debug boolean,
10743 p_fnd_log_module varchar2
10744 )is
10745 Begin
10746   if p_object_name is not null then
10747     setup_conc_program_log(p_object_name);
10748   end if;
10749   if p_debug is not null then
10750     set_debug(p_debug);
10751   end if;
10752   if p_fnd_log_module is not null then
10753     g_fnd_log_module:=p_fnd_log_module;
10754   end if;
10755 Exception when others then
10756   null;
10757 End;
10758 
10759 function get_parameter_value(
10760 p_name varchar2
10761 )return varchar2 is
10762 l_var varchar2(200);
10763 cursor cv(p_name varchar2) is select param.value from v$parameter param where param.name=p_name;
10764 Begin
10765   if g_debug then
10766     write_to_log_file_n('select param.value from v$parameter param where param.name=:1 '||p_name);
10767   end if;
10768   open cv(p_name);
10769   fetch cv into l_var;
10770   close cv;
10771   if g_debug then
10772     write_to_log_file('value='||l_var);
10773   end if;
10774   return l_var;
10775 EXCEPTION when others then
10776   write_to_log_file_n(g_status_message);
10777   return null;
10778 End;
10779 
10780 function get_db_version return varchar2 is
10781 l_compatibility varchar2(40);
10782 Begin
10783   if g_db_version is null then
10784     DBMS_UTILITY.DB_VERSION(g_db_version,l_compatibility);
10785   end if;
10786   if g_debug then
10787     write_to_log_file_n('DB version '||g_db_version);
10788   end if;
10789   return g_db_version;
10790 EXCEPTION when others then
10791   write_to_log_file_n(g_status_message);
10792   return null;
10793 End;
10794 
10795 function is_db_version_gt(p_db_version varchar2,p_version varchar2) return boolean is
10796 l_db_version varcharTableType;
10797 l_num_db_version number;
10798 l_version varcharTableType;
10799 l_num_version number;
10800 l_min number;
10801 Begin
10802   if g_debug then
10803     write_to_log_file_n('is_db_version_gt, is '||p_db_version||' > '||p_version||'?');
10804   end if;
10805   if parse_names(p_db_version,'.',l_db_version,l_num_db_version)=false then
10806     return false;
10807   end if;
10808   if parse_names(p_version,'.',l_version,l_num_version)=false then
10809     return false;
10810   end if;
10811   if l_num_version>l_num_db_version then
10812     l_min:=l_num_db_version;
10813   else
10814     l_min:=l_num_version;
10815   end if;
10816   for i in 1..l_min loop
10817     if to_number(l_db_version(i))>to_number(l_version(i)) then
10818       if g_debug then
10819         write_to_log_file('Yes');
10820       end if;
10821       return true;
10822     end if;
10823     if to_number(l_db_version(i))<to_number(l_version(i)) then
10824       if g_debug then
10825         write_to_log_file('No');
10826       end if;
10827       return false;
10828     end if;
10829   end loop;
10830   if g_debug then
10831     write_to_log_file('Yes');
10832   end if;
10833   return true;
10834 EXCEPTION when others then
10835   write_to_log_file_n(g_status_message);
10836   return false;
10837 End;
10838 
10839 --a generic table query api
10840 function query_table_cols(
10841 p_table varchar2,
10842 p_col varchar2,
10843 p_where varchar2,
10844 p_output out nocopy varcharTableType,
10845 p_num_output out nocopy number
10846 ) return boolean is
10847 l_stmt varchar2(4000);
10848 TYPE CurTyp IS REF CURSOR;
10849 cv   CurTyp;
10850 Begin
10851   p_num_output:=1;
10852   l_stmt:='select '||p_col||' from '||p_table||' '||p_where;
10853   if g_debug then
10854     write_to_log_file_n(l_stmt||get_time);
10855   end if;
10856   open cv for l_stmt;
10857   loop
10858     fetch cv into p_output(p_num_output);
10859     exit when cv%notfound;
10860     p_num_output:=p_num_output+1;
10861   end loop;
10862   p_num_output:=p_num_output-1;
10863   if g_debug then
10864     write_to_log_file('Result');
10865     for i in 1..p_num_output loop
10866       write_to_log_file(p_output(i));
10867     end loop;
10868   end if;
10869   return true;
10870 EXCEPTION when others then
10871   write_to_log_file_n(sqlerrm);
10872   return false;
10873 End;
10874 
10875 procedure dump_mem_stats is
10876 l_stmt varchar2(2000);
10877 TYPE CurTyp IS REF CURSOR;
10878 cv   CurTyp;
10879 ---
10880 l_sid number;
10881 l_name varchar2(200);
10882 l_mem number;
10883 ---
10884 Begin
10885   if g_session_id is null then
10886     g_session_id:=USERENV('SESSIONID');
10887   end if;
10888   --for dbms jobs g_session_id=0
10889   if g_session_id>0 then
10890     l_stmt:='select ses.sid,sy.name,round(ss.value/1048576,2) from v$sesstat ss,v$sysstat sy,v$session ses '||
10891     'where sy.statistic#=ss.statistic# and ss.sid = ses.sid and ses.audsid=:1 and '||
10892     'sy.name in (''session pga memory'',''session pga memory max'',''session uga memory'',''session uga memory max'')';
10893     if g_debug then
10894       write_to_log_file_n('Mem stats '||get_time);
10895     end if;
10896     open cv for l_stmt using g_session_id;
10897     loop
10898       fetch cv into l_sid,l_name,l_mem;
10899       exit when cv%notfound;
10900       write_to_log_file(l_sid||' '||l_name||' '||l_mem);
10901     end loop;
10902     close cv;
10903   end if;
10904 EXCEPTION when others then
10905   write_to_log_file_n('Error in dump_mem_stats '||sqlerrm);
10906 End;
10907 
10908 procedure dump_parallel_stats is
10909 l_stmt varchar2(2000);
10910 TYPE CurTyp IS REF CURSOR;
10911 cv   CurTyp;
10912 ---
10913 l_name varchar2(300);
10914 l_query number;
10915 l_total number;
10916 ---
10917 Begin
10918   l_stmt:='select pq.statistic,pq.last_query,pq.session_total from v$pq_sesstat pq';
10919   if g_debug then
10920     write_to_log_file_n('Parellel stats '||get_time);
10921   end if;
10922   open cv for l_stmt;
10923   loop
10924     fetch cv into l_name,l_query,l_total;
10925     exit when cv%notfound;
10926     write_to_log_file(l_name||' '||l_query||' '||l_total);
10927   end loop;
10928 EXCEPTION when others then
10929   write_to_log_file_n('Error in dump_parallel_stats '||sqlerrm);
10930 End;
10931 
10932 function check_and_wait_for_job(
10933 p_job_id number,
10934 p_status_table varchar2,
10935 p_where varchar2,
10936 p_sleep_time number,
10937 p_status out nocopy varchar2,
10938 p_message out nocopy varchar2
10939 )return boolean is
10940 ---
10941 l_status varcharTableType;
10942 l_num_status number;
10943 l_job_id numberTableType;
10944 ---
10945 Begin
10946   if g_debug then
10947     write_to_log_file_n('In check_and_wait_for_job job_id='||p_job_id||',status_table='||p_status_table||
10948     'where='||p_where||get_time);
10949   end if;
10950   if p_job_id is null then
10951     p_status:='NO JOB';
10952     return true;
10953   end if;
10954   if query_table_cols(p_status_table,'status||''+++''||message',p_where,l_status,l_num_status)=false then
10955     --this means that the job did not start, kill the job
10956     if edw_owb_collection_util.terminate_job(p_job_id)=false then
10957       return false;
10958     end if;
10959     p_status:='NO JOB';
10960     return true;
10961   else
10962     if g_debug then
10963       write_to_log_file_n('No rows in '||p_status_table||', process dead or running');
10964     end if;
10965     if l_num_status=0 then
10966       --this means that there are no rows in the status table. process still running or process crashed
10967       if check_job_status(p_job_id)='Y' then
10968         --wait on job
10969         l_job_id(1):=p_job_id;
10970         if wait_on_jobs(
10971           l_job_id,
10972           1,
10973           p_sleep_time,
10974           'JOB')=false then
10975           return false;
10976         end if;
10977         --read the status again
10978         if query_table_cols(p_status_table,'status||''+++''||message',p_where,
10979           l_status,l_num_status)=false then
10980           return false;
10981         end if;
10982       else
10983         --process has crashed. we have to redo the steps.
10984         if g_debug then
10985           write_to_log_file_n('Child Process dead');
10986         end if;
10987         p_status:='NO JOB';
10988         return true;
10989       end if;
10990     end if;
10991   end if;
10992   p_status:=substr(l_status(1),1,instr(l_status(1),'+++')-1);
10993   p_message:=substr(l_status(1),instr(l_status(1),'+++')+3);
10994   return true;
10995 EXCEPTION when others then
10996   write_to_log_file_n('Error in check_and_wait_for_job '||sqlerrm);
10997   return false;
10998 End;
10999 
11000 function get_tables_matching_pattern(
11001 p_pattern varchar2,
11002 p_owner varchar2,
11003 p_table in out nocopy varcharTableType,
11004 p_num_table in out nocopy number
11005 )return boolean is
11006 --
11007 cursor c1(p_pattern varchar2,p_owner varchar2) is
11008 select table_name from all_tables where table_name like p_pattern and owner=p_owner;
11009 --
11010 Begin
11011   if p_num_table is null then
11012     p_num_table:=0;
11013   end if;
11014   if g_debug then
11015     write_to_log_file_n('select table_name from all_tables where table_name like '||p_pattern||
11016     ' and owner='||p_owner);
11017   end if;
11018   open c1(p_pattern,p_owner);
11019   loop
11020     fetch c1 into p_table(p_num_table+1);
11021     exit when c1%notfound;
11022     p_num_table:=p_num_table+1;
11023   end loop;
11024   close c1;
11025   if g_debug then
11026     for i in 1..p_num_table loop
11027       write_to_log_file(p_table(i));
11028     end loop;
11029   end if;
11030   return true;
11031 EXCEPTION when others then
11032   write_to_log_file_n('Error in get_tables_matching_pattern '||sqlerrm);
11033   return false;
11034 End;
11035 
11036 function update_status_table(
11037 p_table varchar2,
11038 p_col varchar2,
11039 p_value varchar2,
11040 p_where varchar2
11041 ) return boolean is
11042 --
11043 l_stmt varchar2(4000);
11044 --
11045 Begin
11046   l_stmt:='update '||p_table||' set '||p_col||'=:1 '||p_where;
11047   if g_debug then
11048     write_to_log_file_n(l_stmt||' '||p_value);
11049   end if;
11050   execute immediate l_stmt using p_value;
11051   return true;
11052 EXCEPTION when others then
11053   write_to_log_file_n('Error in update_status_table '||sqlerrm);
11054   return false;
11055 End;
11056 
11057 function create_dd_status_table(
11058 p_table varchar2,
11059 p_level_order varcharTableType,
11060 p_number_levels number
11061 ) return boolean is
11062 --
11063 cursor c1(p_ltc varchar2) is
11064 select distinct
11065 lvl.LEVEL_TABLE_ID,
11066 rel.CHIL_LVLTBL_NAME,
11067 rel.CHILD_LVLTBL_ID
11068 from
11069 edw_levels_md_v lvl,
11070 edw_level_relations_md_v rel
11071 where
11072 lvl.LEVEL_TABLE_NAME=p_ltc
11073 and rel.PARENT_LVL_ID(+)=lvl.level_id;
11074 --
11075 l_stmt varchar2(2000);
11076 --
11077 l_parent_id numberTableType;
11078 l_ltc varcharTableType;
11079 l_ltc_id numberTableType;
11080 l_num_ltc number;
11081 l_child_count number;
11082 l_count number;
11083 --
11084 TYPE CurTyp IS REF CURSOR;
11085 cv   CurTyp;
11086 --
11087 Begin
11088   if g_debug then
11089     write_to_log_file_n('In create_dd_status_table p_number_levels='||p_number_levels);
11090   end if;
11091   l_stmt:='create table '||p_table||'(level_order number,parent_ltc varchar2(200),'||
11092   'parent_ltc_id number,child_ltc varchar2(200),child_ltc_id number,job_id number,status varchar2(4000),'||
11093   'message varchar2(4000))';
11094   if g_debug then
11095     write_to_log_file_n(l_stmt);
11096   end if;
11097   if drop_table(p_table)=false then
11098     null;
11099   end if;
11100   execute immediate l_stmt;
11101   --
11102   if p_number_levels is not null then
11103     l_count:=0;
11104     for i in 1..p_number_levels loop
11105       if g_debug then
11106         write_to_log_file_n('select distinct rel.PARENT_LVLTBL_ID,rel.CHIL_LVLTBL_NAME,rel.CHILD_LVLTBL_ID '||
11107         'from edw_levels_md_v lvl,edw_level_relations_md_v rel where lvl.LEVEL_TABLE_NAME='||p_level_order(i)||
11108         ' and rel.PARENT_LVL_ID=lvl.level_id');
11109       end if;
11110       l_num_ltc:=1;
11111       open c1(p_level_order(i));
11112       loop
11113         fetch c1 into l_parent_id(l_num_ltc),l_ltc(l_num_ltc),l_ltc_id(l_num_ltc);
11114         exit when c1%notfound;
11115         l_num_ltc:=l_num_ltc+1;
11116       end loop;
11117       l_num_ltc:=l_num_ltc-1;
11118       close c1;
11119       if g_debug then
11120         for j in 1..l_num_ltc loop
11121           write_to_log_file(l_parent_id(j)||' '||l_ltc(j)||' '||l_ltc_id(j));
11122         end loop;
11123       end if;
11124       if l_num_ltc=1 and l_ltc_id(1) is null then
11125         --lowest level
11126         l_count:=l_count+1;
11127         l_stmt:='insert into '||p_table||'(level_order,parent_ltc,parent_ltc_id)'||
11128         ' values(:1,:2,:3)';
11129         execute immediate l_stmt using l_count,p_level_order(i),l_parent_id(1);
11130       else --parent levels
11131         l_child_count:=0;
11132         for j in 1..p_number_levels loop
11133           for k in 1..l_num_ltc loop
11134             if l_ltc(k)=p_level_order(j) then
11135               l_count:=l_count+1;
11136               l_child_count:=l_child_count+1;
11137               l_stmt:='insert into '||p_table||'(level_order,parent_ltc,parent_ltc_id,child_ltc,child_ltc_id)'||
11138               ' values(:1,:2,:3,:4,:5)';
11139               execute immediate l_stmt using l_count,p_level_order(i),l_parent_id(k),l_ltc(k),l_ltc_id(k);
11140             end if;
11141           end loop;
11142         end loop;
11143       end if;
11144     end loop;
11145   end if;
11146   commit;
11147   return true;
11148 EXCEPTION when others then
11149   write_to_log_file_n('Error in create_dd_status_table '||sqlerrm);
11150   return false;
11151 End;
11152 
11153 function is_table_partitioned(p_table varchar2,p_owner varchar2) return varchar2 is
11154 --
11155 cursor c1(p_table varchar2,p_owner varchar2) is
11156 select PARTITIONED from all_tables where table_name=p_table and owner=p_owner;
11157 --
11158 l_res varchar2(40);
11159 Begin
11160   if g_debug then
11161     write_to_log_file_n('select PARTITIONED from all_tables where table_name='||p_table||' and owner='||
11162     p_owner);
11163   end if;
11164   open c1(p_table,p_owner);
11165   fetch c1 into l_res;
11166   close c1;
11167   if g_debug then
11168     write_to_log_file(l_res);
11169   end if;
11170   return l_res;
11171 EXCEPTION when others then
11172   write_to_log_file_n('Error in is_table_partitioned '||sqlerrm);
11173   return null;
11174 End;
11175 
11176 function drop_level_UL_tables(
11177 p_dim_id number,
11178 p_bis_owner varchar2
11179 ) return boolean is
11180 --
11181 cursor c1(p_dim_id number) is
11182 select LEVEL_TABLE_ID from edw_levels_md_v where dim_id=p_dim_id;
11183 --
11184 l_ltc_id number;
11185 --
11186 Begin
11187   open c1(p_dim_id);
11188   loop
11189     fetch c1 into l_ltc_id;
11190     exit when c1%notfound;
11191     if drop_table(p_bis_owner||'.TAB_'||l_ltc_id||'_UL')=false then
11192       null;
11193     end if;
11194   end loop;
11195   return true;
11196 EXCEPTION when others then
11197   write_to_log_file_n('Error in drop_level_UL_tables '||sqlerrm);
11198   return false;
11199 End;
11200 
11201 function is_source_for_fast_refresh_mv(
11202 p_object varchar2,
11203 p_owner varchar2
11204 ) return number is
11205 --
11206 cursor c1(p_object varchar2,p_owner varchar2) is
11207 select 1
11208 from
11209 ALL_MVIEWS mv,
11210 ALL_MVIEW_DETAIL_RELATIONS rel
11211 where
11212 rel.mview_name=mv.mview_name
11213 and mv.owner=rel.owner
11214 and mv.fast_refreshable<>'NO'
11215 and rel.detailobj_name=upper(p_object)
11216 and rel.detailobj_owner=upper(p_owner)
11217 and rownum=1;
11218 --
11219 l_res number;
11220 --
11221 Begin
11222   if g_debug then
11223     write_to_log_file_n('In is_source_for_fast_refresh_mv');
11224     write_to_log_file('select 1 from ALL_MVIEWS mv, ALL_MVIEW_DETAIL_RELATIONS rel '||
11225     ' where rel.mview_name=mv.mview_name and mv.owner=rel.owner and mv.fast_refreshable<>''NO'''||
11226     ' and rel.detailobj_name='||p_object||' and rel.detailobj_owner='||p_owner);
11227   end if;
11228   open c1(p_object,p_owner);
11229   fetch c1 into l_res;
11230   close c1;
11231   if l_res=1 then
11232     if g_debug then
11233       write_to_log_file('YES');
11234     end if;
11235   else
11236     if g_debug then
11237       write_to_log_file('NO');
11238     end if;
11239   end if;
11240   return l_res;
11241 Exception when others then
11242   g_status_message:=sqlerrm;
11243   write_to_log_file_n('Error in is_source_for_fast_refresh_mv '||sqlerrm);
11244   return -1;
11245 End;
11246 
11247 function drop_tables_like(p_string varchar2,p_owner varchar2)return boolean is
11248 --
11249 cursor c1(p_string varchar2,p_owner varchar2) is
11250 select table_name from all_tables where table_name like p_string and owner=p_owner;
11251 --
11252 l_table varchar2(100);
11253 l_string varchar2(4000);
11254 --
11255 Begin
11256   l_string:=p_string;
11257   if instr(l_string,'.')>0 then
11258     l_string:=substr(l_string,instr(l_string,'.')+1);
11259   end if;
11260   if g_debug then
11261     write_to_log_file_n('In drop_tables_like, select table_name from all_tables where '||
11262     'table_name like '||l_string||' and owner='||p_owner);
11263   end if;
11264   open c1(l_string,p_owner);
11265   loop
11266     fetch c1 into l_table;
11267     exit when c1%notfound;
11268     if g_debug then
11269       write_to_log_file('Drop '||p_owner||'.'||l_table);
11270     end if;
11271     if drop_table(l_table,p_owner)=false then
11272       null;
11273     end if;
11274   end loop;
11275   return true;
11276 EXCEPTION when others then
11277   write_to_log_file_n('Error in drop_tables_like '||sqlerrm);
11278   return false;
11279 End;
11280 
11281 --3529591
11282 function get_all_derived_facts_inc(
11283 p_object varchar2,
11284 p_derived_facts out NOCOPY varcharTableType,
11285 p_derived_fact_ids out NOCOPY numberTableType,
11286 p_map_id  out NOCOPY numberTableType,
11287 p_number_derived_facts out NOCOPY number) return boolean is
11288 --
11289 l_derived_facts varcharTableType;
11290 l_derived_fact_ids numberTableType;
11291 l_map_ids numberTableType;
11292 l_number_derived_facts number;
11293 --
11294 Begin
11295   if g_debug then
11296     write_to_log_file_n('In get_all_derived_facts_inc ');
11297   end if;
11298   if get_all_derived_facts(p_object,l_derived_facts,l_derived_fact_ids,l_map_ids,l_number_derived_facts)=false then
11299     return false;
11300   end if;
11301   p_number_derived_facts:=0;
11302   for i in 1..l_number_derived_facts loop
11303     if is_inc_refresh_implemented(l_derived_facts(i))=true then
11304       p_number_derived_facts:=p_number_derived_facts+1;
11305       p_derived_facts(p_number_derived_facts):=l_derived_facts(i);
11306       p_derived_fact_ids(p_number_derived_facts):=l_derived_fact_ids(i);
11307       p_map_id(p_number_derived_facts):=l_map_ids(i);
11308     end if;
11309   end loop;
11310   if g_debug then
11311     write_to_log_file_n('INC Derived facts');
11312     for i in 1..p_number_derived_facts loop
11313       write_to_log_file(p_derived_facts(i)||' '||p_derived_fact_ids(i)||' '||p_map_id(i));
11314     end loop;
11315   end if;
11316   return true;
11317 Exception when others then
11318   g_status_message:=sqlerrm;
11319   write_to_log_file_n('Exception in  get_all_derived_facts_inc '||sqlerrm||' '||get_time);
11320   return false;
11321 End;
11322 
11323 --3529591
11324 function get_fact_dfact_ilog(
11325 p_bis_owner varchar2,
11326 p_src_fact_id number,
11327 p_derived_fact_id number) return varchar2 is
11328 Begin
11329   return p_bis_owner||'.I_'||p_src_fact_id||'_'||p_derived_fact_id;
11330 Exception when others then
11331   write_to_log_file_n('Exception in  get_fact_dfact_ilog '||sqlerrm||' '||get_time);
11332   return null;
11333 End;
11334 
11335 --3529591
11336 function get_fact_dfact_dlog(
11337 p_bis_owner varchar2,
11338 p_src_fact_id number,
11339 p_derived_fact_id number) return varchar2 is
11340 Begin
11341   return p_bis_owner||'.D_'||p_src_fact_id||'_'||p_derived_fact_id;
11342 Exception when others then
11343   write_to_log_file_n('Exception in  get_fact_dfact_dlog '||sqlerrm||' '||get_time);
11344   return null;
11345 End;
11346 
11347 /*
11348 3529591
11349 for a base fact that has a fast refresh mv on it.
11350 after we refresh derived facts, we cannot clean the mv log and the ilog/dlog.
11351 we can clean them only in the next round of load if the mv log is truncated.
11352 mv log is truncated if mv refresh did run.
11353 */
11354 function clean_ilog_dlog_base_fact(
11355 p_fact varchar2,
11356 p_owner varchar2,
11357 p_bis_owner varchar2,
11358 p_fact_id number,
11359 p_fact_dlog varchar2
11360 )return boolean is
11361 --
11362 l_derived_facts varcharTableType;
11363 l_derived_fact_ids numberTableType;
11364 l_map_id  numberTableType;
11365 l_number_derived_facts number;
11366 l_snplog varchar2(200);
11367 l_ilog varchar2(200);
11368 l_dlog varchar2(200);
11369 --
11370 Begin
11371   if g_debug then
11372     write_to_log_file_n('In clean_ilog_dlog_base_fact '||get_time);
11373   end if;
11374   if is_source_for_inc_derived_fact(p_fact)=1 and is_source_for_fast_refresh_mv(p_fact,p_owner)=1 then
11375     l_snplog:=get_table_snapshot_log(p_fact);
11376     if l_snplog is not null then
11377       if does_table_have_data(p_owner||'.'||l_snplog)=1 then
11378         --the mv log is empty. clean up the ilogs and dlogs and -DLOG table
11379         if instr(p_fact_dlog,'.')<>0 then
11380           if drop_table(p_fact_dlog)=false then
11381             return false;
11382           end if;
11383         else
11384           if truncate_table(p_fact_dlog,p_owner)=false then
11385             null;
11386           end if;
11387         end if;
11388         if get_all_derived_facts_inc(p_fact,l_derived_facts,l_derived_fact_ids,
11389           l_map_id,l_number_derived_facts)=false then
11390           g_status_message:=g_status_message;
11391           return false;
11392         end if;
11393         if l_number_derived_facts>0 then
11394           for i in 1..l_number_derived_facts loop
11395             l_ilog:=get_fact_dfact_ilog(p_bis_owner,p_fact_id,l_derived_fact_ids(i));
11396             l_dlog:=get_fact_dfact_dlog(p_bis_owner,p_fact_id,l_derived_fact_ids(i));
11397             if drop_table(l_ilog)=false then
11398               null;
11399             end if;
11400             if drop_table(l_ilog||'A')=false then
11401               null;
11402             end if;
11403             if drop_table(l_dlog)=false then
11404               null;
11405             end if;
11406             if drop_table(l_dlog||'A')=false then
11407               null;
11408             end if;
11409             if drop_ilog_tables(l_ilog||'_IL',null,p_bis_owner)=false then
11410               null;
11411             end if;
11412             if drop_ilog_tables(l_dlog||'_DL',null,p_bis_owner)=false then
11413               null;
11414             end if;
11415           end loop;
11416         end if;
11417       end if;
11418     end if;
11419   end if;
11420   return true;
11421 Exception when others then
11422   g_status_message:=sqlerrm;
11423   write_to_log_file_n('Error in clean_ilog_dlog_base_fact '||g_status_message);
11424   return false;
11425 End;
11426 
11427 --4161164 : remove IOT , replace with ordinary table and index
11428 procedure create_iot_index(
11429 p_table varchar2,
11430 p_column varchar2,
11431 p_tablespace varchar2,
11432 p_parallel number) is
11433 --
11434 l_stmt varchar2(20000);
11435 Begin
11436   l_stmt:='create unique index '||p_table||'u on '||p_table||'('||p_column||') tablespace '||p_tablespace;
11437   if p_parallel is not null then
11438     l_stmt:=l_stmt||' parallel '||p_parallel;
11439   end if;
11440   if g_debug then
11441     write_to_log_file_n('Going to execute '||l_stmt||get_time);
11442   end if;
11443   execute immediate l_stmt;
11444   if g_debug then
11445     write_to_log_file('Created '||get_time);
11446   end if;
11447 Exception when others then
11448   g_status_message:=sqlerrm;
11449   write_to_log_file_n('Error in create_iot_index '||g_status_message);
11450   raise;
11451 End;
11452 
11453 FUNCTION get_apps_schema_name RETURN VARCHAR2 IS
11454   l_apps_schema_name VARCHAR2(30);
11455   CURSOR c_apps_schema_name IS
11456   SELECT oracle_username
11457   FROM fnd_oracle_userid WHERE oracle_id
11458   BETWEEN 900 AND 999 AND read_only_flag = 'U';
11459 BEGIN
11460   OPEN c_apps_schema_name;
11461   FETCH c_apps_schema_name INTO l_apps_schema_name;
11462   CLOSE c_apps_schema_name;
11463   RETURN l_apps_schema_name;
11464 EXCEPTION
11465   WHEN OTHERS THEN
11466   RETURN NULL;
11467 END get_apps_schema_name;
11468 
11469 END EDW_OWB_COLLECTION_UTIL;