DBA Data[Home] [Help]

PACKAGE BODY: APPS.BSC_BSC_ADAPTER

Source


1 package body BSC_BSC_ADAPTER AS
2 /*$Header: BSCBSCMB.pls 120.31 2006/09/07 13:32:37 rkumar ship $*/
3 
4 
5 TYPE cLevelInfoMap IS RECORD(
6 level_table_name varchar2(100),
7 short_name       varchar2(100)
8 );
9 TYPE tab_cLevelInfoMap is table of cLevelInfoMap index by VARCHAR2(300);
10 
11 g_level_info tab_cLevelInfoMap;
12 g_level_info_cached boolean := false;
13 
14 procedure cache_level_info is
15 l_count number := 1;
16 cursor cLevelInfo is
17 SELECT LEVEL_TABLE_NAME,SHORT_NAME, upper(level_pk_col) level_pk_col
18 FROM
19  BSC_SYS_DIM_LEVELS_B WHERE level_table_name in
20 (select level_table_name
21    from bsc_kpi_dim_levels_b kpi,
22         bsc_tmp_opt_ui_kpis gdb
23   where gdb.indicator=kpi.indicator
24     and gdb.prototype_flag <> 2
25     and gdb.process_id = bsc_metadata_optimizer_pkg.g_ProcessId);
26 begin
27   for i in cLevelInfo loop
28     g_level_info(i.level_pk_col).level_table_name := i.level_table_name;
29     g_level_info(i.level_pk_col).short_name := i.short_name;
30   end loop;
31 end;
32 
33 procedure cache_calendar_as_loaded(p_calendar_id number) is
34 begin
35   if g_calendars_refreshed.exists(p_calendar_id) then
36     null;
37   else
38     g_calendars_refreshed(p_calendar_id).calendar_id := p_calendar_id;
39   end if;
40 end;
41 
42 function calendar_already_refreshed(p_calendar_id number) return boolean is
43 begin
44   if g_calendars_refreshed.exists(p_calendar_id) then
45     return true;
46   else
47     return false;
48   end if;
49 end;
50 
51 procedure drop_and_add_partition(p_calendar_id number, p_rpt_calendar_owner varchar2) is
52 PRAGMA AUTONOMOUS_TRANSACTION;
53 l_stmt varchar2(1000);
54 begin
55   l_stmt := 'alter table '||p_rpt_calendar_owner||'.bsc_reporting_calendar drop partition p_'||p_calendar_id;
56   --bug 4636259, performance issue, so partitioning;
57   begin
58     if g_debug then
59       write_to_log_file_n(l_stmt);
60     end if;
61     execute immediate l_stmt;
62     exception when others then
63       null;
64   end;
65   l_stmt := 'alter table '||p_rpt_calendar_owner||'.bsc_reporting_calendar add partition p_'||
66             p_calendar_id||' values('||p_calendar_id||')';
67   if g_debug then
68       write_to_log_file_n(l_stmt);
69   end if;
70   execute immediate l_stmt;
71   COMMIT;
72 end;
73 
74 function get_b_table_name_for_prj(p_table_name varchar2) return varchar2 is
75 
76 begin
77   if (p_table_name not like 'BSC_B_%PRJ%') then
78     return p_table_name;
79   end if;
80   return substr(p_table_name, 1, instr(p_table_name, '_', -1)-1);
81 end;
82 
83 function get_b_prj_table_name(p_b_table_name varchar2) return varchar2 is
84 begin
85   return bsc_dbgen_metadata_reader.get_table_properties(
86                            p_b_table_name,
87                            bsc_dbgen_std_metadata.bsc_b_prj_table);
88 end;
89 
90 function get_measures_by_table(
91 p_table_name varchar2,
92 p_measures BSC_IM_UTILS.varchar_tabletype,
93 p_number_measures number
94 ) return varchar2 is
95 cursor c1 is select column_name from bsc_db_tables_cols where table_name = p_table_name;
96 l_column_name VARCHAR2(100);
97 l_ordered_columns varchar2(32000);
98 begin
99   if p_number_measures= 0 then
100     return null;
101   end if;
102   for i in 1..p_number_measures loop
103     open c1;
104     loop
105       fetch c1 into l_column_name;
106       if c1%notfound then
107         l_ordered_columns := l_ordered_columns || ' NULL '||upper(p_measures(i))||',';
108         exit;
109       end if;
110       if (upper(l_column_name) = upper( p_measures(i))) THEN
111         l_ordered_columns := l_ordered_columns || upper(l_column_name)||',';
112         exit;
113       end if;
114     end loop;
115     close c1;
116   end loop;
117   l_ordered_columns:=substr(l_ordered_columns,1,length(l_ordered_columns)-1);
118   if g_debug then
119     write_to_log_file('get_measures_by_table for '|| p_table_name||' returning ... '||l_ordered_columns);
120   end if;
121   return l_ordered_columns;
122 end;
123 
124 function get_prj_union_clause(p_b_table_name varchar2, p_add_alias boolean default true) return varchar2 is
125 cursor c1 is select column_name from bsc_db_tables_cols where table_name=p_b_table_name
126 order by column_type desc;
127 l_stmt varchar2(32000);
128 l_prj_table varchar2(100);
129 begin
130   l_prj_table := get_b_prj_table_name(p_b_table_name);
131   if l_prj_table is null then
132     write_to_log_file('get_prj_union_clause for '|| p_b_table_name||' returning ...'||p_b_table_name);
133     return p_b_table_name;
134   end if;
135   l_stmt := 'select ';
136   for i in c1 loop
137     l_stmt := l_stmt || i.column_name||',';
138   end loop;
139   l_stmt := substr(l_stmt, 1, length(l_stmt)-1)||',year, period, type, periodicity_id from ';
140   l_stmt := l_stmt || p_b_table_name|| ' union all '||l_stmt ||l_prj_table;
141   l_stmt := '('||l_stmt||')';
142   if (p_add_alias) then
143     l_stmt := l_stmt||' '||p_b_table_name;
144   end if;
145    if g_debug then
146     write_to_log_file('get_prj_union_clause for '|| p_b_table_name||' returning ... '|| l_stmt);
147   end if;
148   return l_stmt;
149 end;
150 
151 
152 function load_metadata_for_indicators(
153 p_indicator varchar2,
154 p_options BSC_IM_UTILS.varchar_tabletype,
155 p_number_options number
156 )return boolean is
157 l_indicators BSC_IM_UTILS.number_tabletype;
158 l_number_indicators number;
159 l_final_dimensions BSC_IM_UTILS.varchar_tabletype;
160 l_number_final_dimensions number;
161 Begin
162   g_options:=p_options;
163   g_number_options:=p_number_options;
164   if init_all=false then
165     return false;
166   end if;
167   l_number_indicators:=1;
168   l_indicators(l_number_indicators):=p_indicator;
169   if read_metadata(l_indicators,l_number_indicators,l_final_dimensions,
170     l_number_final_dimensions)=false then
171     return false;
172   end if;
173   return true;
174 Exception when others then
175   BSC_IM_UTILS.g_status_message:=sqlerrm;
176   g_status_message:=sqlerrm;
177   write_to_log_file_n('Exception in load_metadata_for_indicators '||sqlerrm);
178   return false;
179 End;
180 
181 function read_metadata(
182 p_indicators BSC_IM_UTILS.number_tabletype,
183 p_number_indicators number,
184 p_final_dimensions out nocopy BSC_IM_UTILS.varchar_tabletype,
185 p_number_final_dimensions out nocopy number
186 ) return boolean is
187 l_indicator_ids BSC_IM_UTILS.number_tabletype;
188 l_indicators BSC_IM_UTILS.varchar_tabletype;
189 Begin
190   for i in 1..p_number_indicators loop
191     l_indicator_ids(i):=p_indicators(i);
192     l_indicators(i):=p_indicators(i);
193     write_to_debug_n('Id for '||l_indicators(i)||'='||l_indicator_ids(i));
194   end loop;
195   /*
196   after talking to mauricio 4/24/03 we have decided to turn off the dim read part for now.
197   the dim metadata had circular relations. this started causing infinite loops etc.
198   right now, we go for simple approach.
199   */
200   /*
201   if read_levels_required(l_indicator_ids,p_number_indicators,p_final_dimensions,
202     p_number_final_dimensions)=false then
203     return false;
204   end if;
205   */
206   if read_kpi_required(l_indicator_ids,l_indicators,p_number_indicators)=false then
207     return false;
208   end if;
209   return true;
210 Exception when others then
211   BSC_IM_UTILS.g_status_message:=sqlerrm;
212   g_status_message:=sqlerrm;
213   write_to_log_file_n('Exception in read_metadata '||sqlerrm);
214   return false;
215 End;
216 
217 function read_kpi_required(
218 p_indicators BSC_IM_UTILS.number_tabletype,
219 p_indicator_names BSC_IM_UTILS.varchar_tabletype,
220 p_number_indicators number
221 ) return boolean is
222 -----------------------------------------------------------------
223 l_stmt varchar2(5000);
224 l_periodicity BSC_IM_UTILS.number_tabletype;
225 l_number_periodicity number;
226 -----------------------------------------------------------------
227 Begin
228   write_to_debug_n('In read_kpi_required '||get_time);
229   for i in 1..p_number_indicators loop
230     l_number_periodicity:=1;
231     if get_kpi_periodicity(p_indicators(i),l_periodicity,l_number_periodicity)=false then
232       return false;
233     end if;
234     if read_kpi_map_info(
235       p_indicators(i),
236       l_periodicity,
237       l_number_periodicity
238       )=false then
239       return false;
240     end if;
241   end loop;
242   return true;
243 Exception when others then
244   BSC_IM_UTILS.g_status_message:=sqlerrm;
245   g_status_message:=sqlerrm;
246   write_to_log_file_n('Exception in read_kpi_required '||sqlerrm);
247   return false;
248 End;
249 
250 /*
251 this api for XTD supported MV
252 */
253 function read_kpi_map_info(
254 p_indicator number,
255 p_periodicity BSC_IM_UTILS.number_tabletype,
256 p_number_periodicity number
257 )return boolean is
258 -----------------------------------------------------------------
259 l_map_name varchar2(200);
260 l_zero_code_map_name varchar2(200);
261 l_lang varchar2(200);
262 -----------------------------------------------------------------
263 l_mv_name varchar2(200);
264 l_zero_code_mv_name varchar2(200);
265 -----------------------------------------------------------------
266 l_s_tables BSC_IM_UTILS.varchar_tabletype;
267 l_periodicity BSC_IM_UTILS.number_tabletype;
268 l_number_s_tables number;
269 -----------------------------------------------------------------
270 l_distinct_list BSC_IM_UTILS.varchar_tabletype;
271 l_number_distinct_list number;
272 -----------------------------------------------------------------
273 l_temp_var BSC_IM_UTILS.varchar_tabletype;
274 l_number_temp_var number;
275 -----------------------------------------------------------------
276 ll_s_tables BSC_IM_UTILS.varchar_tabletype;
277 ll_number_s_tables number;
278 -----------------------------------------------------------------
279 
280 -- added 02/13/2006 by Arun
281 -- P1 5034426
282 cursor cReportingCalCheck(p_calendar_id number) is
283 select 1 from bsc_sys_periodicities
284 where
285 calendar_id = p_calendar_id
286 and (period_type_id is null or record_type_id is null )
287   --ignoring xtd pattern due to PMD bug 4503527 not having an upgrade script yet
288   --or xtd_pattern is null )
289 and periodicity_type not in (11,12);
290 l_dummy number;
291 p_options BSC_IM_UTILS.varchar_tabletype;
292 l_calendar_id number;
293 
294 Begin
295   if g_debug then
296     write_to_log_file_n('In read_kpi_map_info '||get_time);
297   end if;
298 
299   -- need this to fix bug 5034426
300   -- mv creation fails if period_type_ids are not populated
301   select calendar_id into l_calendar_id from bsc_kpis_vl where indicator=p_indicator;
302   if g_debug then
303      write_to_log_file_n('Checking if any of the periodicities of objective '||p_indicator||
304      ' , calendar = '||l_calendar_id||' are missing period_type_id, record_type_id or xtd_pattern '||get_time  );
305   end if;
306   open cReportingCalCheck(l_calendar_id);
307   fetch cReportingCalCheck into l_dummy;
308   if cReportingCalCheck%FOUND then -- call load_reporting_calendar for this calendar
309     if NOT calendar_already_refreshed(l_calendar_id) then
310       if g_debug then
311         write_to_log_file_n('Need to load reporting calendar for calendar_id='||l_calendar_id);
312       end if;
313       p_options.delete;
314       if g_debug then
315         p_options(1) := 'DEBUG LOG';
316       end if;
317       if load_reporting_calendar(l_calendar_id, p_options, p_options.count) then
318         null;
319       end if;
320       cache_calendar_as_loaded(l_calendar_id);
321     end if;
322   end if;
323   close cReportingCalCheck;
324 
325   /*
326     look at
327     1. 0 code calculations, type=4
328     2. merging columns (tables_rels, relation_type=0)
329     3. merging rows (2 inp tables, same columns, month and week for example)
330     4. targets at different levels (tables_rels, relation_type=5)
331   */
332   --we need to loop over each set of summary data
333   --l_base_tables are the B tables
334   --for each S table, find the set (all periodicities)
335   --SB tables are considered in the same league as the S tables
336   --MV are created for the SB tables.
337   l_lang:=BSC_IM_UTILS.get_lang;
338   ----------------------
339   --create the metadata for KPI
340   if BSC_IM_INT_MD.create_cube(p_indicator,p_indicator,0,'BSC',p_indicator,null)=false then
341     return false;
342   end if;
343   ----------------------
344   if get_s_sb_tables(p_indicator,l_s_tables,l_periodicity,l_number_s_tables)=false then
345     return false;
346   end if;
347   l_number_distinct_list:=0;
348   for i in 1..l_number_s_tables loop
349     if BSC_IM_UTILS.in_array(l_distinct_list,l_number_distinct_list,substr(l_s_tables(i),1,
350       instr(l_s_tables(i),'_',-1)-1))=false then
351       l_number_distinct_list:=l_number_distinct_list+1;
352       l_distinct_list(l_number_distinct_list):=substr(l_s_tables(i),1,instr(l_s_tables(i),'_',-1)-1);
353     end if;
354   end loop;
355   if g_debug then
356     write_to_log_file_n('The distinct list of summary data');
357     for i in 1..l_number_distinct_list loop
358       write_to_log_file(l_distinct_list(i));
359     end loop;
360   end if;
361   --for each of this distinct list, create the maps
362   for i in 1..l_number_distinct_list loop
363     l_map_name:=l_distinct_list(i)||'_MAP';
364     l_mv_name:=l_distinct_list(i)||'_MV';
365     l_zero_code_mv_name:=l_distinct_list(i)||'_ZMV';
366     l_zero_code_map_name:=l_distinct_list(i)||'_ZMAP';
367     --get all the summary tables for l_distinct_list(i)
368     ll_number_s_tables:=0;
369     for j in 1..l_number_s_tables loop
370       if substr(l_s_tables(j),1,instr(l_s_tables(j),'_',-1)-1)=l_distinct_list(i) then
371         ll_number_s_tables:=ll_number_s_tables+1;
372         ll_s_tables(ll_number_s_tables):=l_s_tables(j);
373       end if;
374     end loop;
375     if g_debug then
376       write_to_log_file_n('*********************************************');
377       write_to_log_file('Going to look at ');
378       write_to_log_file('*********************************************');
379       for j in 1..ll_number_s_tables loop
380         write_to_log_file(ll_s_tables(j));
381       end loop;
382     end if;
383     if g_debug then
384         write_to_log_file_n('In read_kpi_map_info--Going to look at s tables '||get_time);
385     end if;
386     if ll_number_s_tables>0 then
387       --------------------------------------------
388       --int metadata for the mv for this kpi
389       if BSC_IM_INT_MD.create_object(
390         l_mv_name,
391         'SUMMARY MV',
392         'BSC',
393         p_indicator,
394         null,
395         'SUMMARY MV')=false then
399       if create_kpi_map_info(
396         return false;
397       end if;
398       --------------------------------------------
400         p_indicator,
401         l_map_name,
402         l_mv_name,
403         l_zero_code_mv_name,
404         l_zero_code_map_name,
405         ll_s_tables,
406         ll_number_s_tables)=false then
407         return false;
408       end if;
409     else
410       if g_debug then
411         write_to_log_file_n('Number of S tables <=0. Cannot process '||l_mv_name);
412       end if;
413     end if;
414     if g_debug then
415            write_to_log_file_n('In read_kpi_map_info--After look look at s tables '||get_time);
416     end if;
417   end loop;--for each set of S tables for this KPI
418   if g_debug then
419       write_to_log_file_n('Returning from read_kpi_map_info '||get_time);
420   end if;
421   return true;
422 Exception when others then
423   BSC_IM_UTILS.g_status_message:=sqlerrm;
424   g_status_message:=sqlerrm;
425   write_to_log_file_n('Exception in read_kpi_map_info '||sqlerrm);
426   return false;
427 End;
428 
429 --bug 3867313
430 function get_distinct_list(p_parameter IN BSC_IM_UTILS.varchar_tabletype, l_number_parameters IN NUMBER) return number is
431 l_final_list BSC_IM_UTILS.varchar_tabletype;
432 l_final_counter number := 0;
433 bFound boolean := false;
434 begin
435   for i in 1..l_number_parameters loop
436     bFound := false;
437     for j in 1..l_final_counter loop
438       if (p_parameter(i) = l_final_list(j)) then
439         bFound := true;
440         exit;
441       end if;
442     end loop;
443     if (NOT bFound) then
444       l_final_counter := l_final_counter + 1;
445       l_final_list(l_final_counter) := p_parameter(i);
446     end if;
447   end loop;
448   return l_final_counter;
449 end;
450 
451 function create_kpi_map_info(
452 p_indicator number,
453 p_map_name varchar2,
454 p_mv_name varchar2,
455 p_zero_code_mv_name varchar2,
456 p_zero_code_map_name varchar2,
457 p_s_tables BSC_IM_UTILS.varchar_tabletype,
458 p_number_s_tables number
459 )return boolean is
460 ---------------s table info-------------------------------------
461 l_fk BSC_IM_UTILS.varchar_tabletype;
462 l_number_fk number;
463 l_measures BSC_IM_UTILS.varchar_tabletype;
464 l_number_measures number;
465 ---------------src table info-------------------------------------
466 l_tables BSC_IM_UTILS.varchar_tabletype;
467 l_source_tables BSC_IM_UTILS.varchar_tabletype;
468 l_relation_type BSC_IM_UTILS.varchar_tabletype;
469 l_source_sql BSC_IM_UTILS.varchar_tabletype;
470 l_table_periodicity BSC_IM_UTILS.number_tabletype;
471 l_table_period_type_id BSC_IM_UTILS.number_tabletype;
472 l_periodicity_id_stmt varchar2(20000);
473 l_eliminate BSC_IM_UTILS.varchar_tabletype;--these are for S tables with SB as src, to have not exists(..)
474 l_group BSC_IM_UTILS.number_tabletype;
475 l_number_source_tables number;
476 ---------------group column info---------------------------------
477 --used in the algo to come up with the min number of union all statements
478 l_group_number BSC_IM_UTILS.number_tabletype;--grp number
479 l_group_column_formula BSC_IM_UTILS.varchar_tabletype;
480 l_group_column BSC_IM_UTILS.varchar_tabletype;
481 l_number_group_column number;
482 -----------S table columns--------------------------------------
483 l_col_table BSC_IM_UTILS.varchar_tabletype;
484 l_cols BSC_IM_UTILS.varchar_tabletype;
485 l_col_type BSC_IM_UTILS.varchar_tabletype;
486 l_col_formula BSC_IM_UTILS.varchar_tabletype;
487 l_col_source BSC_IM_UTILS.varchar_tabletype;
488 l_number_cols number;
489 ------------for 0 code ------------------------------------------
490 l_calculation_table BSC_IM_UTILS.varchar_tabletype;
491 l_calculation_type BSC_IM_UTILS.varchar_tabletype;
492 l_parameter1 BSC_IM_UTILS.varchar_tabletype;
493 l_parameter2 BSC_IM_UTILS.varchar_tabletype;
494 l_parameter3 BSC_IM_UTILS.varchar_tabletype;
495 l_parameter4 BSC_IM_UTILS.varchar_tabletype;
496 l_parameter5 BSC_IM_UTILS.varchar_tabletype;
497 l_number_parameters number;
498 -----------------------------------------------------------------
499 l_select_sql varchar2(32000);
500 l_select_sql_inc varchar2(32000);--for incremental mv sql
501 l_eliminate_sql varchar2(32000);
502 l_from_sql varchar2(32000);
503 l_where_sql varchar2(32000);
504 l_group_by_sql varchar2(32000);
505 l_hint_sql varchar2(32000);
506 l_select_basic varchar2(32000);
507 l_select_no_aggregation varchar2(32000);
508 b_no_agg boolean;
509 -----------------------------------------------------------------
510 l_filter_from BSC_IM_UTILS.varchar_tabletype;
511 l_filter_where BSC_IM_UTILS.varchar_tabletype;
512 l_number_filter number;
513 l_filter_first_level BSC_IM_UTILS.varchar_tabletype;
514 l_filter_first_level_alias BSC_IM_UTILS.varchar_tabletype;
515 l_filter_first_level_fk BSC_IM_UTILS.varchar_tabletype;
516 l_num_filter_first_level number;
517 -----------------------------------------------------------------
518 --for zero code
519 --there may be more than 1 fk for rollup. we need to have a union for each
520 --of the fks with rollup and then one more union with zero code for all the keys
521 --combined
522 l_rollup_select_sql BSC_IM_UTILS.varchar_tabletype;
523 l_rollup_from_sql BSC_IM_UTILS.varchar_tabletype;
524 l_rollup_where_sql BSC_IM_UTILS.varchar_tabletype;
528 --l_rollup... will contain stmts with union all. these are used to create fast refresh mv
525 l_rollup_group_by_sql BSC_IM_UTILS.varchar_tabletype;
526 l_number_rollup_sql number;
527 ----------
529 -- but in the case where we cannot have fast refresh mv, we need to go for full refresh mv.
530 --in the case of full refresh mv, we dont want union all, but we prefer rollup(...)
531 l_rollup_full_select_sql varchar2(32000);
532 l_rollup_full_from_sql varchar2(32000);
533 l_rollup_full_where_sql varchar2(32000);
534 l_rollup_full_group_by_sql varchar2(32000);
535 l_rollup_full_having_sql varchar2(32000);
536 -----------------------------------------------------------------
537 l_db_version varchar2(200);
538 -----------------------------------------------------------------
539 --used for dummy MV creation.
540 --dummy MV are created for snapshot log maintenance
541 l_b_tables BSC_IM_UTILS.varchar_tabletype;
542 l_number_b_tables number;
543 l_dim_level_tables BSC_IM_UTILS.varchar_tabletype;
544 l_number_dim_level_tables number;
545 -----------------------------------------------------------------
546 l_base_table_stmt varchar2(20000);--this is the stmt that will be used for snapshot log creation
547 l_dim_level_stmt varchar2(20000);--this will be used to create snapshot log on the dim level
548 -----------------------------------------------------------------
549 l_full_zero_code_map_name varchar2(200);
550 -----------------------------------------------------------------
551 l_lowest_s_table varchar2(200);
552 l_bt_tables BSC_IM_UTILS.varchar_tabletype;
553 l_number_bt_tables number;
554 -----------------------------------------------------------------
555 l_return_var varchar2(10);
556 l_number_keys number :=0;
557 
558 
559 l_b_prj_table varchar2(100);
560 
561 Begin
562   if g_debug then
563     write_to_log_file_n('In create_kpi_map_info '||p_map_name||' '||p_mv_name);
564     write_to_log_file_n('In Create_kpi_map_info'||' '||get_time);
565   end if;
566   l_number_source_tables:=0;
567   l_db_version:=BSC_IM_UTILS.get_db_version;
568   l_number_parameters:=0;
569   l_full_zero_code_map_name:=p_zero_code_map_name||'_FULL';
570   --get the fk of the summary tables
571   --if get_table_fks(p_s_tables,p_number_s_tables,l_fk,l_number_fk)=false then
572   --for perf, we look at only 1 of the S tables
573   if get_table_fks(p_s_tables(1),l_fk,l_number_fk)=false then
574     return false;
575   end if;
576   --if get_table_measures(p_s_tables,p_number_s_tables,l_measures,l_number_measures)=false then
577   if get_table_measures(p_s_tables(1),l_measures,l_number_measures)=false then
578     return false;
579   end if;
580   -----------------------------------------
581   --add int metadata for the fk and measures. useful for snapshot log creation
582   if g_debug then
583       write_to_log_file_n('In Create_kpi_map_info--Before Loop Create_Fks'||' '||get_time);
584   end if;
585   for i in 1..l_number_fk loop
586     if BSC_IM_INT_MD.create_fk(
587       l_fk(i),
588       'SUMMARY MV',
589       p_mv_name,
590       null,
591       null,
592       null,
593       'BSC',
594       'SUMMARY MV')=false then
595       return false;
596     end if;
597   end loop;
598   ------------------
599   --int metadata for the measures
600   for i in 1..l_number_measures loop
601     if BSC_IM_INT_MD.create_column(
602       l_measures(i),
603       'A',
604       null,
605       'BSC',
606       null,
607       null,
608       null,
609       p_mv_name,
610       null)=false then
611       return false;
612     end if;
613   end loop;
614 
615   -----------------------------------------
616 
617   for i in 1..p_number_s_tables loop
618 
619     if get_table_relations(
620       p_s_tables(i),
621       l_tables,
622       l_source_tables,
623       l_relation_type,
624       l_number_source_tables)=false then
625       return false;
626     end if;
627     --get the column information
628     if get_table_cols(
629       p_s_tables(i),
630       l_col_table,
631       l_cols,
632       l_col_type,
633       l_col_source,
634       l_col_formula,
635       l_number_cols)=false then
636       return false;
637     end if;
638     --get 0 code calculations
639     l_return_var := BSC_IM_UTILS.get_option_value(g_options,g_number_options,'NO ROLLUP');
640 
641     if l_return_var='Y' then
642       if g_debug then
643         write_to_log_file_n('No rollup specified');
644       end if;
645     else
646       if get_db_calculation(
647         p_indicator,
648         p_s_tables(i),
649         4,--zero code
650         l_calculation_table,
651         l_calculation_type,
652         l_parameter1,
653         l_parameter2,
654         l_parameter3,
655         l_parameter4,
656         l_parameter5,
657         l_number_parameters)=false then
658         return false;
659       end if;
660       -- bug 3835059, to support any number of keys and not hang while creating the mv
661       if (l_return_var <> 'N') then
662       	-- bug 3867313, l_number_parameters is incremented per periodicity and hence we shd
663         l_number_keys := get_distinct_list(l_parameter1, l_number_parameters);
664         if g_debug then
668       	  l_number_parameters :=0;
665           write_to_log_file_n('l_number_keys = '||l_number_keys||' max allowed = '||l_return_var );
666     	end if;
667       	if (l_number_keys > to_number(l_return_var) ) then
669       	end if;
670       end if;
671     end if;
672   end loop;
673 
674   --------------------------------------------------------------------------
675   l_number_b_tables:=0;
676   l_number_dim_level_tables:=0;
677   for i in 1..l_number_source_tables loop
678     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_B_') then
679       if BSC_IM_UTILS.in_array(l_b_tables,l_number_b_tables,l_source_tables(i))=false then
680         l_number_b_tables:=l_number_b_tables+1;
681         l_b_tables(l_number_b_tables):=l_source_tables(i);
682       end if;
683     end if;
684   end loop;
685   if g_debug then
686     write_to_log_file_n('Process the T tables');
687   end if;
688   l_periodicity_id_stmt:=' periodicity_id in (';
689   for i in 1..l_number_source_tables loop
690     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_T_') then --this is a T table
691       --construct the sql
692       --we need to get the level tables also here because snp logs need to be created on level tables too
693       --do we need to get the filter to the base tables for T?
694       if get_table_sql(l_source_tables(i),l_source_sql(i),l_b_tables,l_number_b_tables,
695         l_dim_level_tables,l_number_dim_level_tables)=false then
696         return false;
697       end if;
698     else
699       l_source_sql(i):=null;
700     end if;
701     --assign the table periodicity
702     l_table_periodicity(i):=get_table_periodicity(l_tables(i));
703     --l_table_period_type_id(i):=get_period_type_id_for_period(l_table_periodicity(i));
704     --period type id assigned further down
705 
706     -- added by arun, if periodicity id already exists, ignore duplicate
707     if (instr(l_periodicity_id_stmt, '('||l_table_periodicity(i)||',' )>0
708         OR instr(l_periodicity_id_stmt, ','||l_table_periodicity(i)||',') > 0) then
709       null;
710     else
711       l_periodicity_id_stmt:=l_periodicity_id_stmt||l_table_periodicity(i)||',';
712     end if;
713   end loop;
714   l_periodicity_id_stmt:=substr(l_periodicity_id_stmt,1,length(l_periodicity_id_stmt)-1)||')';
715   if g_debug then
716     write_to_log_file_n('periodicity_id stmt='||l_periodicity_id_stmt);
717     write_to_log_file_n('ALL the B tables');
718     for i in 1..l_number_b_tables loop
719       write_to_log_file(l_b_tables(i));
720     end loop;
721   end if;
722   -----------------------------------
723   --load the base table column info also into int metadata for snapshot log
724   declare
725     ll_fk BSC_IM_UTILS.varchar_tabletype;
726     ll_number_fk number;
727     ll_measures BSC_IM_UTILS.varchar_tabletype;
728     ll_number_measures number;
729   begin
730     for i in 1..l_number_b_tables loop
731       if get_table_fks(l_b_tables(i),ll_fk,ll_number_fk)=false then
732         return false;
733       end if;
734       if get_table_measures(l_b_tables(i),ll_measures,ll_number_measures)=false then
735         return false;
736       end if;
737       --insert into int metadata
738       for j in 1..ll_number_fk loop
739         if BSC_IM_INT_MD.create_fk(
740           ll_fk(j),
741           'BASE TABLE',
742           l_b_tables(i),
743           null,
744           null,
745           null,
746           'BSC',
747           'BASE TABLE')=false then
748           return false;
749         end if;
750       end loop;
751       for j in 1..ll_number_measures loop
752         if BSC_IM_INT_MD.create_column(
753           ll_measures(j),
754           'A',
755           null,
756           'BSC',
757           null,
758           null,
759           null,
760           l_b_tables(i),
761           null)=false then
762           return false;
763         end if;
764       end loop;
765     end loop;
766   end;
767   ----------------------------------------------------
768   --we construct the base table stmt to later put into "property for mapping. BISMVLDB will use this
769   --info to create snapshot logs
770   if l_number_b_tables>0 then
771     l_base_table_stmt:='BASE TABLES=';
772     for i in 1..l_number_b_tables loop
773       l_base_table_stmt:=l_base_table_stmt||l_b_tables(i)||'+';
774     end loop;
775     l_base_table_stmt:=substr(l_base_table_stmt,1,length(l_base_table_stmt)-1);
776   else
777     l_base_table_stmt:=null;
778   end if;
779   l_dim_level_stmt:='DIM LEVELS=';
780   if g_debug then
781     write_to_log_file_n('Base table stmt '||l_base_table_stmt);
782   end if;
783   --------------------------------------------------------------------------
784   /*
785   talked with mauricio 6/30/03
786   if this set is loaded from B tables, we need to make sure that for all higher levels in periodicity,
787   we use the same formula as  the lowest S table.
788   */
789   declare
790     -----------------------------------
791     ll_col BSC_IM_UTILS.varchar_tabletype;
792     ll_col_formula BSC_IM_UTILS.varchar_tabletype;
793     ll_number_col number;
794     ll_index number;
795     -----------------------------------
799     ll_calculation_type BSC_IM_UTILS.varchar_tabletype;
796     ll_rollup_found boolean;
797     ll_table BSC_IM_UTILS.varchar_tabletype;
798     ll_number_table number;
800     ll_parameter1 BSC_IM_UTILS.varchar_tabletype;
801     ll_parameter2 BSC_IM_UTILS.varchar_tabletype;
802     ll_parameter3 BSC_IM_UTILS.varchar_tabletype;
803     ll_parameter4 BSC_IM_UTILS.varchar_tabletype;
804     ll_parameter5 BSC_IM_UTILS.varchar_tabletype;
805     ll_number_parameters number;
806     -----------------------------------
807   begin
808     for i in 1..l_number_source_tables loop
809       if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_T_') or
810         BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_B_') then --this is a T or B table
811         l_lowest_s_table:=l_tables(i);
812         exit;
813       end if;
814     end loop;
815     if l_lowest_s_table is not null then
816       if g_debug then
817         write_to_log_file_n('This set of S tables is loaded from B or T tables, need to set the higher level '||
818         'formulas as the lowest level S table');
819         write_to_log_file('Lowest S table='||l_lowest_s_table);
820       end if;
821       ------------------------------
822       --get the measures for the lowest level
823       ll_number_col:=0;
824       for i in 1..l_number_cols loop
825         if l_col_table(i)=l_lowest_s_table then
826           ll_number_col:=ll_number_col+1;
827           ll_col(ll_number_col):=l_cols(i);
828           ll_col_formula(ll_number_col):=l_col_formula(i);
829         end if;
830       end loop;
831       --set the higher levels
832       for i in  1..l_number_cols loop
833         if l_col_table(i) <> l_lowest_s_table then
834           ll_index:=0;
835           ll_index:=BSC_IM_UTILS.get_index(ll_col,ll_number_col,l_cols(i));
836           if ll_index>0 then
837             if l_col_formula(i) <> ll_col_formula(ll_index) then
838               if g_debug then
839                 write_to_log_file('For '||l_col_table(i)||', changing '||l_col_formula(i)||' to '||
840                 ll_col_formula(ll_index));
841               end if;
842               l_col_formula(i):=ll_col_formula(ll_index);
843             end if;
844           end if;
845         end if;
846       end loop;
847       if g_debug then
848         write_to_log_file_n('After re-assigning the formulas for the columns');
849         for i in 1..l_number_cols loop
850           write_to_log_file(l_col_table(i)||' '||l_cols(i)||' '||l_col_type(i)||' '||l_col_source(i)||' '||
851           l_col_formula(i));
852         end loop;
853       end if;
854       --------------------------------------------
855       --do the same for rollups
856       --if the higher levels have no rollups and the lowest level has them, create it for higher levels
857       --if the higher levels have rollups and the formula is different, make it same as lowest level
858       ll_number_parameters:=0;
859       if l_number_parameters>0 then
860         --------------------------------
861         --for the lowest level, change the rollup formula to the formula for the column
862         if g_debug then
863           write_to_log_file_n('For the lowest level, make sure that the rollup formula is same as col formula');
864         end if;
865         for i in 1..l_number_parameters loop
866           if l_calculation_table(i)=l_lowest_s_table then
867             ll_index:=0;
868             ll_index:=BSC_IM_UTILS.get_index(l_cols,l_number_cols,l_parameter3(i));
869             if ll_index>0 then
870               if g_debug then
871                 write_to_log_file('Changing '||l_parameter5(i)||' to '||l_col_formula(ll_index));
872               end if;
873               l_parameter5(i):=l_col_formula(ll_index);
874             end if;
875           end if;
876         end loop;
877         if g_debug then
878           write_to_log_file_n('-------------------------------------------');
879         end if;
880         --------------------------------
881         if g_debug then
882           write_to_log_file_n('For zero code, if zero code does not exist for higher level, create it');
883           write_to_log_file('If zero code does exist, make sure the formulas match that of the lowest level');
884         end if;
885         for i in 1..l_number_parameters loop
886           if l_calculation_table(i)=l_lowest_s_table then
887             ll_number_parameters:=ll_number_parameters+1;
888             ll_calculation_type(ll_number_parameters):=l_calculation_type(i);
889             ll_parameter1(ll_number_parameters):=l_parameter1(i);
890             ll_parameter2(ll_number_parameters):=l_parameter2(i);
891             ll_parameter3(ll_number_parameters):=l_parameter3(i);
892             ll_parameter4(ll_number_parameters):=l_parameter4(i);
893             ll_parameter5(ll_number_parameters):=l_parameter5(i);
894           end if;
895         end loop;
896         --for all the s tables that are not the lowest level, if rollup does not exist, create one
897         ll_number_table:=0;--for which tables we need to create zero code rollups
898         for i in 1..p_number_s_tables loop
899           if p_s_tables(i) <> l_lowest_s_table then
900             ll_rollup_found:=false;
901             for j in 1..l_number_parameters loop
902               if l_calculation_table(j)=p_s_tables(i) then
903                 ll_rollup_found:=true;
904                 ll_index:=0;
908                     if g_debug then
905                 ll_index:=BSC_IM_UTILS.get_index(ll_parameter3,ll_number_parameters,l_parameter3(j));
906                 if ll_index>0 then
907                   if l_parameter5(j)<>ll_parameter5(ll_index) then
909                       write_to_log_file('For '||l_calculation_table(j)||', changing '||l_parameter5(j)||
910                       'to '||ll_parameter5(ll_index));
911                     end if;
912                     l_parameter5(j):=ll_parameter5(ll_index);
913                   end if;
914                 end if;
915               end if;
916             end loop;
917             if ll_rollup_found=false then
918               ll_number_table:=ll_number_table+1;
919               ll_table(ll_number_table):=p_s_tables(i);
920               if g_debug then
921                 write_to_log_file_n('For table '||ll_table(ll_number_table)||', zero code rollup not defined '||
922                 'in metadata');
923               end if;
924             end if;
925           end if;--if p_s_tables(i) <> l_lowest_s_table then
926         end loop;--for i in 1..l_number_parameters loop
927         if ll_number_table>0 then
928           for i in 1..ll_number_table loop
929             if g_debug then
930               write_to_log_file_n('Going to create zero code rollups for '||ll_table(i));
931             end if;
932             for j in 1..ll_number_parameters loop
933               l_number_parameters:=l_number_parameters+1;
934               l_calculation_table(l_number_parameters):=ll_table(i);
935               l_calculation_type(l_number_parameters):=l_calculation_type(j);
936               l_parameter1(l_number_parameters):=l_parameter1(j);
937               l_parameter2(l_number_parameters):=l_parameter2(j);
938               l_parameter3(l_number_parameters):=l_parameter3(j);
939               l_parameter4(l_number_parameters):=l_parameter4(j);
940               l_parameter5(l_number_parameters):=l_parameter5(j);
941             end loop;
942           end loop;
943         end if;
944         if g_debug then
945           write_to_log_file_n('After re-assigning the zero code rollups');
946           for i in 1..l_number_parameters loop
947             write_to_log_file(l_calculation_table(i)||' '||l_calculation_type(i)||' '||l_parameter1(i)||' '||
948             l_parameter2(i)||' '||l_parameter3(i)||' '||l_parameter4(i)||' '||l_parameter5(i));
949           end loop;
950         end if;
951       end if;--if l_number_parameters>0 then
952       ------------------------------
953     end if;--if l_lowest_s_table is not null then
954   end;
955   -----------------------------------------------------------------------
956   if g_debug then
957     write_to_log_file_n('Going to process this info to create the maps');
958   end if;
959   --if there are more than 1 b table or t table then we need to consolidate the b and t and
960   --make one source.
961   --see if we need to resolve the source. if the relation is B1,B2-> 24_5 -> 24_3 -> 24_1
962   --then we need to make B1,B2-> 24_5 B1,B2-> 24_3 B1,B2-> 24_1
963   declare
964     ll_tables BSC_IM_UTILS.varchar_tabletype;
965     ll_source_tables BSC_IM_UTILS.varchar_tabletype;
966     ll_relation_type BSC_IM_UTILS.varchar_tabletype;
967     ll_source_sql BSC_IM_UTILS.varchar_tabletype;
968     ll_table_periodicity BSC_IM_UTILS.number_tabletype;
969     ll_number_source_tables number;
970     ------------------------------------------
971     ll_list BSC_IM_UTILS.varchar_tabletype;
972     ll_list_2 BSC_IM_UTILS.varchar_tabletype;
973     ll_number_list number;
974     ll_found boolean;
975     ll_count integer;
976     -----------------------------------------
977     ll_bt_measures BSC_IM_UTILS.varchar_tabletype;
978     ll_number_bt_measures number;
979     ll_column_merge_group BSC_IM_UTILS.varchar_tabletype;
980     ll_column_merge_group_sql BSC_IM_UTILS.varchar_tabletype;
981     ll_number_column_merge_group number;
982     ll_column_merge_sql BSC_IM_UTILS.varchar_tabletype; --uses p_number_s_tables
983     ll_merge_sql varchar2(32000);
984 
985     ll_bt_measures_by_table VARCHAR2(32000);
986     -----------------------------------------
987     ll_prj_table_name varchar2(100);
988   begin
989     ll_number_source_tables:=0;
990     l_number_bt_tables:=0;
991     --first we need to consolidate the B tables and the T tables
992     --the B tables and T tables only feed the first level summary table
993     ll_number_column_merge_group:=0;
994     --there is a difference between l_number_bt_tables and l_number_b_tables
995     --l_number_bt_tables sees how many B and T tables feed this base the first level summary
996     --l_number_b_tables goes recursively down and sees all the B tables involved.
997     for i in 1..l_number_source_tables loop
998       if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_B_')
999         or BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_T_') then
1000         l_number_bt_tables:=l_number_bt_tables+1;
1001         l_bt_tables(l_number_bt_tables):=l_source_tables(i);
1002       end if;
1003     end loop;
1004     if g_debug then
1005       write_to_log_file_n('The list of B tables and T tables that are sources');
1006       for i in 1..l_number_bt_tables loop
1007         write_to_log_file(l_bt_tables(i));
1008       end loop;
1009     end if;
1010     if g_debug then
1011       write_to_log_file_n('Process B and T tables which are sources');
1012     end if;
1016     --correctly filtered.
1013     --get the filter stmt.
1014     --had a discussion with mauricio and deb. it was decided that we will go for filter only in the case
1015     --for the lowest level of summary. when this data rolls to higher levels, the data is automatically
1017     --if there are more than 1 B tables then the filter stmt must be inside the union stmt
1018     --if there is only 1 base table, then the filter is a part of the normal from clause
1019     l_number_filter:=0;
1020     if g_debug then
1021       write_to_log_file_n('In Create_kpi_map_info--before get_filter_stmt'||' '||get_time);
1022     end if;
1023     if l_number_bt_tables>0 then
1024       if get_filter_stmt(
1025         p_indicator,
1026         l_lowest_s_table,
1027         l_filter_from,
1028         l_filter_where,
1029         l_number_filter,
1030         l_dim_level_tables,
1031         l_number_dim_level_tables,
1032         l_filter_first_level,
1033         l_filter_first_level_alias,
1034         l_filter_first_level_fk,
1035         l_num_filter_first_level
1036         )=false then
1037         return false;
1038       end if;
1039     end if;
1040     if l_number_bt_tables>1 then
1041       if g_debug then
1042         write_to_log_file_n('Going to merge the B tables and T tables together');
1043       end if;
1044       --the merging can be union or a column merge or both
1045       for i in 1..p_number_s_tables loop
1046         ll_number_list:=0;
1047         ll_column_merge_sql(i):=null;
1048         ll_number_column_merge_group:=0;
1049         for j in 1..l_number_source_tables loop
1050           if l_tables(j)=p_s_tables(i) and (
1051             BSC_IM_UTILS.is_like(l_source_tables(j),'BSC_B_')
1052             or BSC_IM_UTILS.is_like(l_source_tables(j),'BSC_T_')) then
1053             ll_number_list:=ll_number_list+1;
1054             ll_list(ll_number_list):=l_source_tables(j);
1055             ll_list_2(ll_number_list):=l_source_sql(j);
1056           end if;
1057         end loop;
1058         if ll_number_list>0 then
1059           for j in 1..ll_number_list loop
1060             ll_number_column_merge_group:=ll_number_column_merge_group+1;
1061             ll_column_merge_group(ll_number_column_merge_group):=ll_list(j);
1062             ll_column_merge_group_sql(ll_number_column_merge_group):=ll_list_2(j);
1063           end loop;
1064         end if;
1065 
1066         if g_debug then
1067           write_to_log_file_n('The B and T tables for column merge');
1068           for j in 1..ll_number_column_merge_group loop
1069             write_to_log_file(ll_column_merge_group(j)||' '||ll_column_merge_group_sql(j));
1070           end loop;
1071         end if;
1072         if ll_number_column_merge_group>0 then
1073           ll_column_merge_sql(i):='select ';
1074           for j in 1..l_number_fk loop
1075             if ll_number_column_merge_group=1 then
1076               ll_column_merge_sql(i):=ll_column_merge_sql(i)||l_fk(j)||',';
1077             else
1078               ll_column_merge_sql(i):=ll_column_merge_sql(i)||'prim.'||l_fk(j)||',';
1079             end if;
1080           end loop;
1081           ll_number_list:=0;
1082           ll_number_bt_measures:=0;
1083           --need to take common columns
1084           if get_table_measures(ll_column_merge_group,ll_number_column_merge_group,ll_list,
1085             ll_number_list)=false then
1086             return false;
1087           end if;
1088           for j in 1..ll_number_list loop
1089             if BSC_IM_UTILS.in_array(l_measures,l_number_measures,ll_list(j)) then
1090               ll_number_bt_measures:=ll_number_bt_measures+1;
1091               ll_bt_measures(ll_number_bt_measures):=ll_list(j);
1092             end if;
1093           end loop;
1094           for j in 1..ll_number_bt_measures loop
1095             ll_column_merge_sql(i):=ll_column_merge_sql(i)||ll_bt_measures(j)||',';
1096           end loop;
1097           ll_column_merge_sql(i):=substr(ll_column_merge_sql(i),1,length(ll_column_merge_sql(i))-1)||' from ';
1098 
1099           if ll_number_column_merge_group=1 then
1100             --if there is only one base table then we dont need to find prim etc
1101             if ll_column_merge_group_sql(1) is null then
1102               --ll_column_merge_sql(i):=ll_column_merge_sql(i)||ll_column_merge_group(1);
1103               -- if B table, then union with PRJ
1104               if (instr(ll_column_merge_group(1), 'BSC_B_')=1) then
1105                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||get_prj_union_clause(ll_column_merge_group(1));
1106               end if;
1107             else
1108               ll_column_merge_sql(i):=ll_column_merge_sql(i)||'('||ll_column_merge_group_sql(1)||') '||ll_column_merge_group(1);
1109             end if;
1110             if l_number_filter>0 then
1111               for j in 1..l_number_filter loop
1112                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||','||l_filter_from(j);
1113               end loop;
1114               ll_column_merge_sql(i):=ll_column_merge_sql(i)||' where '||l_periodicity_id_stmt;
1115               for j in 1..l_num_filter_first_level loop
1116                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||' and '||ll_column_merge_group(1)||'.'||
1117                 l_filter_first_level_fk(j)||'='||l_filter_first_level_alias(j)||'.code ';
1118                 --3613094
1119                 if bsc_im_utils.is_column_in_object(l_filter_first_level(j),'LANGUAGE') then
1120                   ll_column_merge_sql(i):=ll_column_merge_sql(i)||' and '||
1124               for j in 1..l_number_filter loop
1121                   l_filter_first_level_alias(j)||'.language='''||BSC_IM_UTILS.get_lang||'''';
1122                 end if;
1123               end loop;
1125                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||' '||l_filter_where(j);
1126               end loop;
1127             else
1128               ll_column_merge_sql(i):=ll_column_merge_sql(i)||' where '||l_periodicity_id_stmt;
1129             end if;
1130           else
1131             --ll_column_merge_sql(i):=ll_column_merge_sql(i)||'(';
1132             ll_column_merge_sql(i):='(';
1133             for j in 1..ll_number_column_merge_group loop
1134               --ll_column_merge_sql(i):=ll_column_merge_sql(i)||'select ';
1135               ll_column_merge_sql(i):=ll_column_merge_sql(i)||'select /*+ full('||ll_column_merge_group(j)||') */';
1136               -- periodicity_id, year, period, type are returned as fks
1137               for k in 1..l_number_fk loop
1138                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||l_fk(k)||',';
1139               end loop;
1140               ll_bt_measures_by_table := get_measures_by_table(ll_column_merge_group(j), ll_bt_measures, ll_number_bt_measures);
1141               if ( ll_bt_measures_by_table is not null) then
1142               	ll_column_merge_sql(i):=ll_column_merge_sql(i)||ll_bt_measures_by_table||',';
1143               end if;
1144               ll_column_merge_sql(i):=substr(ll_column_merge_sql(i),1,length(ll_column_merge_sql(i))-1);
1145               if ll_column_merge_group_sql(j) is null then
1146                 -- Handle B_PRJ
1147                 ll_prj_table_name := get_b_prj_table_name(ll_column_merge_group(j));
1148                 if (ll_prj_table_name is null) then
1149                   ll_column_merge_sql(i):=ll_column_merge_sql(i)||' from '||ll_column_merge_group(j);
1150                 else
1151                   -- ll_column_merge_sql(i):=ll_column_merge_sql(i)||', year, type, period from ';
1152                   -- ll_column_merge_sql(i):='('||ll_column_merge_sql(i)||ll_column_merge_group(j)||
1153                     --                       ' union all '||ll_column_merge_sql(i)|| ll_prj_table_name||') '||ll_column_merge_group(j);*/
1154                     ll_column_merge_sql(i):=ll_column_merge_sql(i)||' from '||get_prj_union_clause( ll_column_merge_group(j));
1155                 end if;
1156               else
1157                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||' from ('||
1158                                          ll_column_merge_group_sql(j)||') '||ll_column_merge_group(j);
1159               end if;
1160               if l_number_filter>0 then
1161                 for k in 1..l_number_filter loop
1162                   ll_column_merge_sql(i):=ll_column_merge_sql(i)||','||l_filter_from(k);
1163                 end loop;
1164                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||' where '||l_periodicity_id_stmt;
1165                 for k in 1..l_num_filter_first_level loop
1166                   ll_column_merge_sql(i):=ll_column_merge_sql(i)||' and '||ll_column_merge_group(j)||'.'||
1167                   l_filter_first_level_fk(k)||'='||l_filter_first_level_alias(k)||'.code ';
1168                   --3613094
1169                   if bsc_im_utils.is_column_in_object(l_filter_first_level(k),'LANGUAGE') then
1170                     ll_column_merge_sql(i):=ll_column_merge_sql(i)||' and '||
1171                     l_filter_first_level_alias(k)||'.language='''||BSC_IM_UTILS.get_lang||''' ';
1172                   end if;
1173                 end loop;
1174                 for k in 1..l_number_filter loop
1175                   ll_column_merge_sql(i):=ll_column_merge_sql(i)||' '||l_filter_where(k);
1176                 end loop;
1177               else
1178                 ll_column_merge_sql(i):=ll_column_merge_sql(i)||' where '||l_periodicity_id_stmt;
1179               end if;
1180 
1181               ll_column_merge_sql(i):=ll_column_merge_sql(i)||' union all ';
1182               if g_debug then
1183                 write_to_log_file('ll_column_merge_sql('||i||')='||ll_column_merge_sql(i));
1184               end if;
1185             end loop;
1186             ll_column_merge_sql(i):=substr(ll_column_merge_sql(i),1,length(ll_column_merge_sql(i))-10);
1187             --ll_column_merge_sql(i):=ll_column_merge_sql(i)||') prim ';
1188             ll_column_merge_sql(i):=ll_column_merge_sql(i)||')  ';
1189           end if;
1190         end if;
1191         if g_debug then
1192           write_to_log_file_n('Column merge sql 1 '||ll_column_merge_sql(i));
1193         end if;
1194       end loop;--for i in 1..p_number_s_tables loop
1195       ll_merge_sql:=null;
1196       for i in 1..p_number_s_tables loop
1197         if ll_column_merge_sql(i) is not null then
1198           ll_merge_sql:=ll_merge_sql||ll_column_merge_sql(i)||' union all ';
1199         end if;
1200       end loop;
1201       ll_merge_sql:=substr(ll_merge_sql,1,length(ll_merge_sql)-10);
1202       if g_debug then
1203         write_to_log_file_n('The merge sql '||ll_merge_sql);
1204       end if;
1205       ll_merge_sql := ' (<SELECT DELIMITER> from ('||ll_merge_sql||') bsc_b <GROUP BY DELIMITER>)';
1206       b_no_agg := true;
1207       --now correct the pl/sql records for src info
1208       for i in 1..l_number_source_tables loop
1209         if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_B_')
1210           or BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_T_') then
1211           l_source_tables(i):='BSC_B';
1212           l_source_sql(i):=ll_merge_sql;
1213         end if;
1214       end loop;
1215       --take distinct combination of target and source
1219       for i in 1..l_number_source_tables loop
1216       --we need this because there could have been multiple B and T tables. they all become the
1217       --consolidated src, BSC_B.
1218       ll_number_source_tables:=0;
1220         if BSC_IM_UTILS.in_array(ll_tables,ll_source_tables,ll_number_source_tables,
1221           l_tables(i),l_source_tables(i))=false then
1222           ll_number_source_tables:=ll_number_source_tables+1;
1223           ll_tables(ll_number_source_tables):=l_tables(i);
1224           ll_source_tables(ll_number_source_tables):=l_source_tables(i);
1225           ll_relation_type(ll_number_source_tables):=l_relation_type(i);
1226           ll_source_sql(ll_number_source_tables):=l_source_sql(i);
1227           ll_table_periodicity(ll_number_source_tables):=l_table_periodicity(i);
1228         end if;
1229       end loop;
1230       l_tables:=ll_tables;
1231       l_source_tables:=ll_source_tables;
1232       l_relation_type:=ll_relation_type;
1233       l_source_sql:=ll_source_sql;
1234       l_table_periodicity:=ll_table_periodicity;
1235       l_number_source_tables:=ll_number_source_tables;
1236       if g_debug then
1237         write_to_log_file_n('After substituting the B tables and T tables, the pl/sql records TSRSP');
1238         for i in 1..l_number_source_tables loop
1239           write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1240           l_source_sql(i)||' '||l_table_periodicity(i));
1241         end loop;
1242       end if;
1243     end if;--if l_number_bt_tables>1 then
1244     --if we have something like S_5->S_3->S_1, then the src for S_3 will be S_5. we need to change that to the
1245     --src for S_5. if BSC_B is the src for S_5, we need to make the src for S_3 BSC_B or BSC_B->S_5
1246     ll_count:=0;
1247     if g_debug then
1248       write_to_log_file_n('Modify the sources for higher periodicity table if the src is lower periodicity table');
1249     end if;
1250     if g_debug then
1251       write_to_log_file_n('The table of records');
1252       for i in 1..l_number_source_tables loop
1253         write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1254         l_source_sql(i)||' '||l_table_periodicity(i));
1255       end loop;
1256     end if;
1257     loop
1258       ll_count:=ll_count+1;
1259       if g_debug then
1260         write_to_log_file_n('Pass '||ll_count);
1261       end if;
1262       ll_found:=false;
1263       ll_number_source_tables:=0;
1264       for i in 1..l_number_source_tables loop
1265         ll_number_list:=0;
1266         for j in 1..l_number_source_tables loop
1267           if l_source_tables(i)=l_tables(j) then
1268             ll_found:=true;
1269             ll_number_list:=ll_number_list+1;
1270             ll_list(ll_number_list):=l_source_tables(j);
1271             ll_list_2(ll_number_list):=l_source_sql(j);
1272           end if;
1273         end loop;
1274         if ll_number_list=0 then
1275           ll_number_source_tables:=ll_number_source_tables+1;
1276           ll_tables(ll_number_source_tables):=l_tables(i);
1277           ll_source_tables(ll_number_source_tables):=l_source_tables(i);
1278           ll_relation_type(ll_number_source_tables):=l_relation_type(i);
1279           ll_source_sql(ll_number_source_tables):=l_source_sql(i);
1280           ll_table_periodicity(ll_number_source_tables):=l_table_periodicity(i);
1281         else
1282           for j in 1..ll_number_list loop
1283             ll_number_source_tables:=ll_number_source_tables+1;
1284             ll_tables(ll_number_source_tables):=l_tables(i);
1285             ll_source_tables(ll_number_source_tables):=ll_list(j);
1286             ll_relation_type(ll_number_source_tables):=l_relation_type(i);
1287             ll_source_sql(ll_number_source_tables):=ll_list_2(j);
1288             ll_table_periodicity(ll_number_source_tables):=l_table_periodicity(i);
1289           end loop;
1290         end if;
1291       end loop;
1292       l_tables:=ll_tables;
1293       l_source_tables:=ll_source_tables;
1294       l_relation_type:=ll_relation_type;
1295       l_source_sql:=ll_source_sql;
1296       l_table_periodicity:=ll_table_periodicity;
1297       l_number_source_tables:=ll_number_source_tables;
1298       if g_debug then
1299         write_to_log_file_n('After reassigning the sources, the table of records pass '||ll_count);
1300         for i in 1..l_number_source_tables loop
1301           write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1302           l_source_sql(i)||' '||l_table_periodicity(i));
1303         end loop;
1304       end if;
1305       if ll_found=false then
1306         exit;
1307       end if;
1308     end loop;
1309   end;
1310   --check to see if we can make a single sql or we need a union all
1311   --update the src table to MV
1312   for i in 1..l_number_source_tables loop
1313     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_S_')
1314       or BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_SB_') then
1315       l_source_tables(i):=substr(l_source_tables(i),1,instr(l_source_tables(i),'_',-1)-1)||'_MV';
1316     end if;
1317   end loop;
1318   if g_debug then
1319     write_to_log_file_n('After assigning the MV as sources, the table of records');
1320     for i in 1..l_number_source_tables loop
1321       write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1322       l_source_sql(i)||' '||l_table_periodicity(i));
1323     end loop;
1327   end loop;
1324   end if;
1325   for i in 1..l_number_source_tables loop
1326     l_eliminate(i):=null;
1328   --if S tables have SB as source, we need to have not exists clause to remove the target info from
1329   --non SB source
1330   for i in 1..l_number_source_tables loop
1331     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_SB_') then
1332       for j in 1..l_number_source_tables loop
1333         if l_tables(j)=l_tables(i) and not(BSC_IM_UTILS.is_like(l_source_tables(j),'BSC_SB_')) then
1334           l_eliminate(j):=l_source_tables(i);
1335         end if;
1336       end loop;
1337     end if;
1338   end loop;
1339   if g_debug then
1340     write_to_log_file_n('After assigning the MV as sources, the table of records, eliminate is the last column');
1341     for i in 1..l_number_source_tables loop
1342       write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1343       l_source_sql(i)||' '||l_table_periodicity(i)||' '||l_eliminate(i));
1344     end loop;
1345   end if;
1346   -------------------------------------------------------
1347   --if there is a SB table that is feeding say periodicity 3,1 but not feeding say periodicity 5,
1348   --then we need to make sure that we put SB in the eliminate for 5
1349   declare
1350     ll_eliminate varchar2(200);
1351   begin
1352     for i in 1..l_number_source_tables loop
1353       if not(BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_SB_')) and l_eliminate(i) is not null then
1354         ll_eliminate:=l_eliminate(i);
1355         for j in 1..l_number_source_tables loop
1356           if i<>j then
1357             if l_source_tables(j)=l_source_tables(i) and l_eliminate(j) is null then
1358               l_eliminate(j):=ll_eliminate;
1359             end if;
1360           end if;
1361         end loop;
1362       end if;
1363     end loop;
1364   end;
1365   if g_debug then
1366     write_to_log_file_n('After correcting eliminate info, the table of records, eliminate is the last column');
1367     for i in 1..l_number_source_tables loop
1368       write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1369       l_source_sql(i)||' '||l_table_periodicity(i)||' '||l_eliminate(i));
1370     end loop;
1371   end if;
1372   -------------------------------------------------------
1373   --add int metadata for MV relations, useful for summary mv reresh to decide the order
1374   for i in 1..l_number_source_tables loop
1375     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_S_')
1376       or BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_SB_') then
1377       if g_debug then
1378       	  write_to_log_file_n('In Create_kpi_map_info--Before BSC_IM_INT_MD.create_object'||' '||get_time);
1379       end if;
1380       if BSC_IM_INT_MD.create_object(
1381         l_source_tables(i),
1382         'SUMMARY MV',
1383         'BSC',
1384         p_mv_name,
1385         null,
1386         'MV DEPENDENCY')=false then
1387         if g_debug then
1388 	  	  write_to_log_file_n('In Create_kpi_map_info--After BSC_IM_INT_MD.create_object'||' '||get_time);
1389         end if;
1390         return false;
1391       end if;
1392       if g_debug then
1393           write_to_log_file_n('In Create_kpi_map_info--After BSC_IM_INT_MD.create_object'||' '||get_time);
1394       end if;
1395     end if;
1396   end loop;
1397   -------------------------------------------------------
1398   /*make groups of the tables. we do this to make the sql efficient.
1399   we look at
1400   1. column source formula
1401   2. rollup formula
1402   3. is eliminate there?
1403   4. is the src table different?
1404   */
1405   declare
1406     -------------------------------------
1407     ll_groups BSC_IM_UTILS.varchar_tabletype;
1408     ll_number_groups number;
1409     -------------------------------------
1410     ll_table_grp BSC_IM_UTILS.varchar_tabletype;
1411     ll_table BSC_IM_UTILS.varchar_tabletype;
1412     ll_source BSC_IM_UTILS.varchar_tabletype;
1413     ll_number_table_grp number;
1414     -------------------------------------
1415     ll_column_grp BSC_IM_UTILS.varchar_tabletype;
1416     ll_column_formula BSC_IM_UTILS.varchar_tabletype;
1417     ll_column BSC_IM_UTILS.varchar_tabletype;
1418     ll_number_column_grp number;
1419     -------------------------------------
1420     ll_rollup_grp BSC_IM_UTILS.varchar_tabletype;
1421     ll_rollup_formula BSC_IM_UTILS.varchar_tabletype;
1422     ll_rollup_column BSC_IM_UTILS.varchar_tabletype;
1423     ll_number_rollup_grp number;
1424     -------------------------------------
1425     ll_eliminate_grp BSC_IM_UTILS.varchar_tabletype;
1426     ll_eliminate BSC_IM_UTILS.varchar_tabletype;
1427     ll_number_eliminate_grp number;
1428     -------------------------------------
1429     ll_new_reqd boolean;
1430     -------------------------------------
1431     ll_measure BSC_IM_UTILS.varchar_tabletype;
1432     ll_measure_formula BSC_IM_UTILS.varchar_tabletype;
1433     ll_number_measure number;
1434     -------------------------------------
1435   begin
1436     ll_number_groups:=0;
1437     ll_number_table_grp:=0;
1438     ll_number_column_grp:=0;
1439     ll_number_rollup_grp:=0;
1440     ll_number_eliminate_grp:=0;
1441     --go through the S tables
1442     for i in 1..l_number_source_tables loop
1443       ll_new_reqd:=false;
1444       if ll_number_groups=0 then
1445         ll_new_reqd:=true;
1446       else
1450           ---------------------------------------------
1447         --see if new is required / or find the group to put this in
1448         for j in 1..ll_number_groups loop
1449           ll_new_reqd:=false;
1451           --check the source
1452           if ll_new_reqd=false then
1453             for k in 1..ll_number_table_grp loop
1454               if ll_table_grp(k)=ll_groups(j) and ll_source(k)<>l_source_tables(i) then
1455                 ll_new_reqd:=true;
1456                 exit;
1457               end if;
1458             end loop;
1459           end if;
1460           ---------------------------------------------
1461           --check eliminate
1462           if ll_new_reqd=false then
1463             for k in 1..ll_number_eliminate_grp loop
1464               if ll_eliminate_grp(k)=ll_groups(j) and nvl(ll_eliminate(k),'abc')<>nvl(l_eliminate(i),'abc') then
1465                 ll_new_reqd:=true;
1466                 exit;
1467               end if;
1468             end loop;
1469           end if;
1470           ---------------------------------------------
1471           --check column formula
1472           if ll_new_reqd=false then
1473             ll_number_measure:=0;
1474             for k in 1..l_number_cols loop
1475               if l_col_table(k)=l_tables(i) and l_col_formula(k) is not null then
1476                 ll_number_measure:=ll_number_measure+1;
1477                 ll_measure(ll_number_measure):=l_cols(k);
1478                 ll_measure_formula(ll_number_measure):=l_col_formula(k);
1479               end if;
1480             end loop;
1481             for k in 1..ll_number_column_grp loop
1482               if ll_column_grp(k)=ll_groups(j) then
1483                 for m in 1..ll_number_measure loop
1484                   if ll_measure(m)=ll_column(k) and ll_measure_formula(m)<>ll_column_formula(k) then
1485                     ll_new_reqd:=true;
1486                     exit;
1487                   end if;
1488                 end loop;
1489               end if;
1490               if ll_new_reqd then
1491                 exit;
1492               end if;
1493             end loop;
1494           end if;
1495           ---------------------------------------------
1496           --check the rollup info
1497           if ll_new_reqd=false then
1498             ll_number_measure:=0;
1499             for k in 1..l_number_parameters loop
1500               if l_calculation_table(k)=l_tables(i) then
1501                 ll_number_measure:=ll_number_measure+1;
1502                 ll_measure(ll_number_measure):=l_parameter3(k);
1503                 ll_measure_formula(ll_number_measure):=l_parameter5(k);
1504               end if;
1505             end loop;
1506             for k in 1..ll_number_rollup_grp loop
1507               if ll_rollup_grp(k)=ll_groups(j) then
1508                 for m in 1..ll_number_measure loop
1509                   if ll_measure(m)=ll_rollup_column(k) and ll_measure_formula(m)<>ll_rollup_formula(k) then
1510                     ll_new_reqd:=true;
1511                     exit;
1512                   end if;
1513                 end loop;
1514               end if;
1515               if ll_new_reqd then
1516                 exit;
1517               end if;
1518             end loop;
1519           end if;
1520           ---------------------------------------------
1521           if ll_new_reqd=false then
1522             --matching group found
1523             l_group(i):=ll_groups(j);
1524             exit;
1525           end if;
1526         end loop;
1527       end if;
1528       if ll_new_reqd then
1529         --add the new group and the table, column,eliminate and rollup info
1530         ll_number_groups:=ll_number_groups+1;
1531         ll_groups(ll_number_groups):=ll_number_groups;
1532         ---------------------------------------------
1533         ll_number_table_grp:=ll_number_table_grp+1;
1534         ll_table_grp(ll_number_table_grp):=ll_groups(ll_number_groups);
1535         ll_table(ll_number_table_grp):=l_tables(i);
1536         ll_source(ll_number_table_grp):=l_source_tables(i);
1537         l_group(i):=ll_groups(ll_number_groups);
1538         ---------------------------------------------
1539         for j in 1..l_number_cols loop
1540           --no need to add the fk. just add the measures
1541           if l_col_table(j)=l_tables(i) and l_col_formula(j) is not null then
1542             ll_number_column_grp:=ll_number_column_grp+1;
1543             ll_column_grp(ll_number_column_grp):=ll_groups(ll_number_groups);
1544             ll_column_formula(ll_number_column_grp):=l_col_formula(j);
1545             ll_column(ll_number_column_grp):=l_cols(j);
1546           end if;
1547         end loop;
1548         ---------------------------------------------
1549         --add rollup agg
1550         for j in 1..l_number_parameters loop
1551           if l_calculation_table(j)=l_tables(i) then
1552             ll_number_rollup_grp:=ll_number_rollup_grp+1;
1553             ll_rollup_grp(ll_number_rollup_grp):=ll_groups(ll_number_groups);
1554             ll_rollup_formula(ll_number_rollup_grp):=l_parameter5(j);
1555             ll_rollup_column(ll_number_rollup_grp):=l_parameter3(j);
1556           end if;
1557         end loop;
1558         ---------------------------------------------
1559         --eliminate info
1560         ll_number_eliminate_grp:=ll_number_eliminate_grp+1;
1561         ll_eliminate_grp(ll_number_eliminate_grp):=ll_groups(ll_number_groups);
1565     end loop;
1562         ll_eliminate(ll_number_eliminate_grp):=l_eliminate(i);
1563         ---------------------------------------------
1564       end if;
1566   end;
1567   if g_debug then
1568     write_to_log_file_n('After assigning the Groups, the table of records, group is the last column');
1569     for i in 1..l_number_source_tables loop
1570       write_to_log_file(l_tables(i)||' '||l_source_tables(i)||' '||l_relation_type(i)||' '||
1571       l_source_sql(i)||' '||l_table_periodicity(i)||' '||l_eliminate(i)||' '||l_group(i));
1572     end loop;
1573   end if;
1574   -------------------------------------------------------
1575   --now generate the sql for each group. SB is last
1576   l_select_sql:=null;
1577   l_eliminate_sql:=null;
1578   l_select_sql_inc:=null;
1579   l_from_sql:=null;
1580   l_where_sql:=null;
1581   l_group_by_sql:=null;
1582   l_hint_sql := null;
1583   declare
1584     ------------------------------------------------------
1585     ll_distinct_groups BSC_IM_UTILS.varchar_tabletype;
1586     ll_number_distinct_groups number;
1587     ------------------------------------------------------
1588     ll_index number;
1589     ll_fk_index number;
1590     ll_col_index number;
1591     ll_temp varchar2(8000);
1592     ll_temp_alias varchar2(200);
1593     ll_dim_src_object varchar2(20000);
1594     ll_dim_src_object_type varchar2(100);
1595     ll_rec_dim boolean;
1596     ll_rec_dim_key varchar2(100);
1597     ------------------------------------------------------
1598     ll_zero_separate boolean;
1599     ------------------------------------------------------
1600     ll_rollup_fk BSC_IM_UTILS.varchar_tabletype;
1601     ll_rollup_fk_value BSC_IM_UTILS.varchar_tabletype;
1602     ll_number_rollup_fk number;
1603     ------------------------------------------------------
1604     ll_periodicity BSC_IM_UTILS.number_tabletype;
1605     ll_number_periodicity number;
1606     ------------------------------------------------------
1607      ll_keys_stmt varchar2(10000);
1608      ll_prj_table_name varchar2(100);
1609   begin
1610     --get the distinct groups
1611     ll_number_distinct_groups:=0;
1612     for i in 1..l_number_source_tables loop
1613       if BSC_IM_UTILS.in_array(ll_distinct_groups,ll_number_distinct_groups,l_group(i))=false then
1614         ll_number_distinct_groups:=ll_number_distinct_groups+1;
1615         ll_distinct_groups(ll_number_distinct_groups):=l_group(i);
1616       end if;
1617     end loop;
1618     if g_debug then
1619       write_to_log_file_n('Going to look at these distinct groups');
1620       for i in 1..ll_number_distinct_groups loop
1621         write_to_log_file(ll_distinct_groups(i));
1622       end loop;
1623     end if;
1624     for i in 1..ll_number_distinct_groups loop
1625       ll_index:=0;
1626       for j in 1..l_number_source_tables loop
1627         if l_group(j)=ll_distinct_groups(i) then
1628           ll_index:=j;
1629           exit;
1630         end if;
1631       end loop;
1632       ll_number_periodicity:=0;
1633       --assign the period type id
1634       for j in 1..l_number_source_tables loop
1635         l_table_period_type_id(j):=get_period_type_id_for_period(l_table_periodicity(j));
1636       end loop;
1637       for j in 1..l_number_source_tables loop
1638         if l_group(j)=ll_distinct_groups(i) then
1639           ll_number_periodicity:=ll_number_periodicity+1;
1640           ll_periodicity(ll_number_periodicity):=l_table_periodicity(j);
1641         end if;
1642       end loop;
1643       --we are sure of the following in a group
1644       --same col formula, same source and same rollup and same filter
1645       --make the select clause
1646       if ll_index>0 then
1647         l_select_sql:='select ';
1648         l_from_sql:=' from ';
1649         l_where_sql:=' where '||l_source_tables(ll_index)||'.periodicity_id in (';
1650         l_group_by_sql:=null;
1651         l_select_sql_inc:=null;
1652         l_hint_sql := '/*+ use_hash(';
1653 		--l_hint_sql := l_hint_sql||')*/';
1654         for j in 1..ll_number_periodicity loop
1655           l_where_sql:=l_where_sql||ll_periodicity(j)||',';
1656         end loop;
1657         l_where_sql:=substr(l_where_sql,1,length(l_where_sql)-1)||')';
1658         if l_source_sql(ll_index) is not null then
1659           l_from_sql:=l_from_sql||' ('||l_source_sql(ll_index)||') ';
1660           l_from_sql:=l_from_sql||l_source_tables(ll_index)||' ';
1661           --here, l_source_tables(ll_index) will be BSC_B
1662           l_hint_sql := l_hint_sql||l_source_tables(ll_index)||' ';
1663         else
1664           if l_source_tables(ll_index)<>'BSC_B' then
1665             -- handle b_prj, P1 5214589
1666             if BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_B_') then
1667               ll_prj_table_name := get_b_prj_table_name(l_source_tables(ll_index));
1668               if (ll_prj_table_name is not null) then
1669                 l_from_sql:=l_from_sql||get_prj_union_clause(l_source_tables(ll_index));
1670               else
1671                 l_from_sql:=l_from_sql||bsc_im_utils.get_table_owner(l_source_tables(ll_index))
1672                   ||'.'||l_source_tables(ll_index)||
1673                   ' '||l_source_tables(ll_index);
1674               end if;
1675             else
1676               l_from_sql:=l_from_sql||bsc_im_utils.get_table_owner(l_source_tables(ll_index))
1677                   ||'.'||l_source_tables(ll_index)||
1681             --for safety. code must not come here
1678                   ' '||l_source_tables(ll_index);
1679             end if;
1680           else
1682             l_from_sql:=l_from_sql||l_source_tables(ll_index)||' ';
1683           end if;
1684           l_hint_sql := l_hint_sql||l_source_tables(ll_index)||' ';
1685         end if;
1686         ----------------------------------------------------------
1687         --0 code we need to calculate this early to properly form group_by and select
1688         ll_number_rollup_fk:=0;
1689         if l_db_version='8i' then
1690           ll_zero_separate:=true;
1691         else
1692           ll_zero_separate:=false;
1693         end if;
1694         -------------------------------
1695         --change for zero code. after discussion with deb it was decided to use
1696         --union all for the zero code mv. this way we can get fast refresh mv.
1697         --so ll_zero_separate is true even for 9i
1698         --in 9i, if there is partial rollup, we dont get fast refresh
1699         ll_zero_separate:=true;
1700         -------------------------------
1701         if not(BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_SB_')) then
1702           if ll_zero_separate=false then --this is 9i
1703             --see if the 0 code aggregations are different from the normal aggregations
1704             --if the target is S and src table is SB, no zero code
1705             if BSC_IM_UTILS.is_like(l_tables(ll_index),'BSC_S_')
1706               and BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_SB_') then
1707               if g_debug then
1708                 write_to_log_file_n('No zero code when target is S and src is SB');
1709               end if;
1710             else
1711               for j in 1..l_number_parameters loop
1712                 if l_calculation_table(j)=l_tables(ll_index) then
1713                   ll_col_index:=0;
1714                   ll_col_index:=BSC_IM_UTILS.get_index(l_col_table,l_cols,l_number_cols,l_tables(ll_index),
1715                   l_parameter3(j));
1716                   if ll_col_index>0 then
1717                     if l_parameter5(j)<>l_col_formula(ll_col_index) then
1718                       ll_zero_separate:=true;
1719                       exit;
1720                     end if;
1721                   else
1722                     ll_zero_separate:=true;
1723                     exit;
1724                   end if;
1725                 end if;
1726               end loop;
1727             end if;
1728           end if;
1729         end if;--if l_source_tables(ll_index) not like 'BSC_SB_%' then
1730         --------------------------------------------------------
1731         ll_keys_stmt := null;
1732         write_to_log_file('Now fks');
1733         --then the fks
1734         for j in 1..l_number_fk loop
1735           ll_fk_index:=BSC_IM_UTILS.get_index(l_col_table,l_cols,l_number_cols,l_tables(ll_index),
1736           l_fk(j));
1737           if ll_fk_index>0 then
1738             --S B ok --rollup to higher level in the dimension
1739             --S S ok --rollup to higher level in the dimension
1740             --SB SB ok --rollup to higher level in the dimension
1741             --S SB not ok
1742             ll_keys_stmt := ll_keys_stmt||l_cols(ll_fk_index)||',';
1743             if BSC_IM_UTILS.is_like(l_tables(ll_index),'BSC_S_')
1744               and BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_SB_') then
1745               l_select_sql:=l_select_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||' '||
1746               l_cols(ll_fk_index)||',';
1747               l_group_by_sql:=l_group_by_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||',';
1748               if l_eliminate(ll_index) is not null then
1749                 l_eliminate_sql:=l_eliminate_sql||l_eliminate(ll_index)||'.'||l_fk(j)||'='||
1750                 l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||' and ';
1751               end if;
1752             else
1753               if upper(l_cols(ll_fk_index))<>upper(l_col_source(ll_fk_index)) then
1754                 /*
1755                 ll_dim_src_object is the name of the materialized dim table  from dbi. this table belongs
1756                 to the bsc schema. this table is required to get over the issue of complex views and to have
1757                 fast refresh MV
1758                 */
1759                 --ll_temp is the level alias
1760                 --ll_dim_src_object is the table or inline sql
1761                 if get_level_for_pk(l_col_source(ll_fk_index),ll_temp,ll_dim_src_object,ll_dim_src_object_type,
1762                   ll_rec_dim,ll_rec_dim_key)=false then
1763                   return false;
1764                 end if;
1765                 ---------------------
1766                 /*
1767                 BSC 5.2 E2E
1768                 support for complex views
1769                 to support complex views and to get fast refresh mv , we had to materialize the dbi dimension views.
1770                 BSCDDIMB.pls has the static class weith info on the table name etc
1771                 we must make the MV join with the table we have created.
1772                 we assume that the table is a reflection of the dim level view. the table has the code column and
1773                 higher level codes. if its date tracked, it can have code and parent code
1774                 currently (7/13/04), none of the dim levels have parent levels. they are all single level dim, including
1778                 --l_dim_level_stmt:=l_dim_level_stmt||ll_temp||'+';
1775                 the date tracked dim levels
1776                 */
1777                 ----------
1779                 ll_temp_alias:=substr(ll_temp,1,30-length(j))||j;
1780                 if ll_dim_src_object_type='none' then
1781                   if BSC_IM_UTILS.in_array(l_dim_level_tables,l_number_dim_level_tables,ll_dim_src_object)=false then
1782                     l_number_dim_level_tables:=l_number_dim_level_tables+1;
1783                     l_dim_level_tables(l_number_dim_level_tables):=ll_dim_src_object;
1784                   end if;
1785                 end if;
1786                 ----------
1787                 if ll_rec_dim then
1788                   -- ONLY FIRST LEVEL OF RECURSIVE DIMENSION SHD BE DIFFERENT
1789                   if  BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_S_') then -- higher level rec dim
1790                     l_select_sql:=l_select_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||' '||l_cols(ll_fk_index)||',';
1791                     l_group_by_sql:=l_group_by_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||',';
1792                   else
1793                     l_select_sql:=l_select_sql||'nvl('||ll_temp_alias||'.'||ll_rec_dim_key||','||
1794                     l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||') '||l_cols(ll_fk_index)||',';
1795                     l_group_by_sql:=l_group_by_sql||'nvl('||ll_temp_alias||'.'||ll_rec_dim_key||','||
1796                     l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||'),';
1797                   end if;
1798                 else
1799                   l_select_sql:=l_select_sql||ll_temp_alias||'.'||l_cols(ll_fk_index)||' '||l_cols(ll_fk_index)||',';
1800                   l_group_by_sql:=l_group_by_sql||ll_temp_alias||'.'||l_cols(ll_fk_index)||',';
1801                 end if;
1802                 if ll_dim_src_object_type='inline' then
1803                   l_from_sql:=l_from_sql||','||ll_dim_src_object||' '||ll_temp_alias;
1804                 else
1805                   if ll_dim_src_object_type ='recursive' and  BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_S_') then
1806                     null;
1807                   else
1808                   l_from_sql:=l_from_sql||','||bsc_im_utils.get_table_owner(ll_dim_src_object)||'.'||ll_dim_src_object||' '||ll_temp_alias;
1809                   end if;
1810                 end if;
1811                 l_hint_sql := l_hint_sql||ll_temp_alias||' ';
1812                 if ll_rec_dim and ll_dim_src_object_type<>'recursive' then
1813                   l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.CODE(+)='||l_source_tables(ll_index)||'.'||
1814                   l_col_source(ll_fk_index)||' ';
1815                 else
1816                   l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.CODE='||l_source_tables(ll_index)||'.'||
1817                   l_col_source(ll_fk_index)||' ';
1818                 end if;
1819                 --3613094
1820                 if bsc_im_utils.is_column_in_object(ll_dim_src_object,'LANGUAGE') then
1821                   l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.LANGUAGE='''||BSC_IM_UTILS.get_lang||''' ';
1822                 end if;
1823                 if l_eliminate(ll_index) is not null then
1824                   l_eliminate_sql:=l_eliminate_sql||l_eliminate(ll_index)||'.'||l_fk(j)||'='||
1825                   ll_temp_alias||'.'||l_cols(ll_fk_index)||' and ';
1826                 end if;
1827                 ------------------------------------------
1828               else --if l_cols(ll_fk_index)<>l_col_source(ll_fk_index) then  its the same level here
1829                 /*
1830                 for 5.2 we need to make a change where we join to the dim level no matter what
1831                 this is because by joining to the dim level like DBI mv, we can automatically handle the
1832                 changes to the dim levels like dim deletes, dim updates etc
1833                 these dim levels are also added to l_dim_level_tables so mv logs can be created on them
1834                 here, l_cols(ll_fk_index)=l_col_source(ll_fk_index)
1835                 */
1836                 /*
1837                 please see the above section on E2E kpi and need for materializing the dbi views
1838                 */
1839                 --ll_dim_src_object does not have the language column.
1840                 --it has code, maybe parent code and effective dates etc
1841                 if get_level_for_pk(l_cols(ll_fk_index),ll_temp,ll_dim_src_object,ll_dim_src_object_type,
1842                   ll_rec_dim,ll_rec_dim_key)=false then
1843                   return false;
1844                 end if;
1845                 --l_dim_level_stmt:=l_dim_level_stmt||ll_temp||'+';
1846                 ----------
1847                 ll_temp_alias:=substr(ll_temp,1,30-length(j))||j;
1848                 if ll_dim_src_object_type='none' then
1849                   if BSC_IM_UTILS.in_array(l_dim_level_tables,l_number_dim_level_tables,ll_dim_src_object)=false then
1850                     l_number_dim_level_tables:=l_number_dim_level_tables+1;
1851                     l_dim_level_tables(l_number_dim_level_tables):=ll_dim_src_object;
1852                   end if;
1853                 end if;
1854                 if ll_rec_dim and NOT BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_S_') then
1855                   if ll_dim_src_object_type<>'recursive' then -- DBI recursive
1856                     l_select_sql:=l_select_sql||'nvl('||ll_temp_alias||'.'||ll_rec_dim_key||','||
1857                       l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||') '||l_cols(ll_fk_index)||',';
1861                     l_select_sql:=l_select_sql||ll_temp_alias||'.'||ll_rec_dim_key ||' '||l_cols(ll_fk_index)||',';
1858                     l_group_by_sql:=l_group_by_sql||'nvl('||ll_temp_alias||'.'||ll_rec_dim_key||','||
1859                       l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||'),';
1860                   else  -- BSC Recursive
1862                     l_group_by_sql:=l_group_by_sql||ll_temp_alias||'.'||ll_rec_dim_key||',';
1863                   end if;
1864                 else
1865                   l_select_sql:=l_select_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||' '||
1866                   l_cols(ll_fk_index)||',';
1867                   l_group_by_sql:=l_group_by_sql||l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||',';
1868                 end if;
1869                 if ll_dim_src_object_type='inline' then
1870                   l_from_sql:=l_from_sql||','||ll_dim_src_object||' '||ll_temp_alias;
1871                 else
1872                   if ll_dim_src_object_type='recursive' and  BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_S_') then
1873                     null;
1874                   else
1875                     l_from_sql:=l_from_sql||','||bsc_im_utils.get_table_owner(ll_dim_src_object)||'.'||ll_dim_src_object||' '||ll_temp_alias;
1876                   end if;
1877                 end if;
1878                 l_hint_sql := l_hint_sql||ll_temp_alias||' ';
1879                 if ll_rec_dim and ll_dim_src_object_type<>'recursive'  then
1880                   l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.CODE(+)='||l_source_tables(ll_index)||'.'||
1881                   l_cols(ll_fk_index)||' ';
1882                 else
1883                   -- for recursive dim. higher levels, we dont include the dimension
1884                   if ll_rec_dim and BSC_IM_UTILS.is_like(l_source_tables(ll_index),'BSC_S_') then --
1885                     null;
1886                   else
1887                     l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.CODE='||l_source_tables(ll_index)||'.'||
1888                     l_cols(ll_fk_index)||' ';
1889                   end if;
1890                 end if;
1891                 --3613094
1892                 if bsc_im_utils.is_column_in_object(ll_dim_src_object,'LANGUAGE') then
1893                   l_where_sql:=l_where_sql||' and '||ll_temp_alias||'.LANGUAGE='''||BSC_IM_UTILS.get_lang||''' ';
1894                 end if;
1895                 if l_eliminate(ll_index) is not null then
1896                   l_eliminate_sql:=l_eliminate_sql||l_eliminate(ll_index)||'.'||l_fk(j)||'='||
1897                   l_source_tables(ll_index)||'.'||l_cols(ll_fk_index)||' and ';
1898                 end if;
1899               end if;
1900             end if;
1901           else
1902             --PERIOD,YEAR,TYPE
1903             l_select_sql:=l_select_sql||l_source_tables(ll_index)||'.'||l_fk(j)||' '||l_fk(j)||',';
1904             l_group_by_sql:=l_group_by_sql||l_source_tables(ll_index)||'.'||l_fk(j)||',';
1905             if l_eliminate(ll_index) is not null then
1906               l_eliminate_sql:=l_eliminate_sql||l_eliminate(ll_index)||'.'||l_fk(j)||'='||
1907               l_source_tables(ll_index)||'.'||l_fk(j)||' and ';
1908             end if;
1909           end if;
1910         end loop;
1911         ----------------------------------------------------------
1912         write_to_log_file('now measures');
1913         --second the measures
1914         declare
1915           lll_agg_columns BSC_IM_UTILS.varchar_tabletype;
1916           lll_number_agg_columns number;
1917           lll_list BSC_IM_UTILS.varchar_tabletype;
1918           lll_number_list number;
1919           lll_fk_index number;
1920         begin
1921           lll_number_agg_columns:=0;
1922           l_select_no_aggregation := l_select_sql;
1923           for j in 1..l_number_cols loop
1924             if l_col_table(j)=l_tables(ll_index) and l_col_type(j)='A' then
1925               if b_no_agg then
1926                 l_select_no_aggregation := l_select_no_aggregation||' '||l_cols(j)||',';
1927                 write_to_log_file(' adding to no agg select :'|| l_select_no_aggregation);
1928               end if;
1929               l_select_sql:=l_select_sql||l_col_formula(j)||' '||l_cols(j)||',';
1930               --find_aggregation_columns, needed for inv refresh MV
1931               lll_number_list:=0;
1932               if BSC_IM_UTILS.find_aggregation_columns(l_col_formula(j),lll_list,
1933                 lll_number_list)=false then
1934                 lll_number_list:=0;
1935               end if;
1936               for k in 1..lll_number_list loop
1937                 if BSC_IM_UTILS.in_array(lll_agg_columns,lll_number_agg_columns,lll_list(k))=false then
1938                   lll_number_agg_columns:=lll_number_agg_columns+1;
1939                   lll_agg_columns(lll_number_agg_columns):=lll_list(k);
1940                 end if;
1941               end loop;
1942             end if;
1943           end loop;
1944           for j in 1..lll_number_agg_columns loop
1945             l_select_sql_inc:=l_select_sql_inc||',count('||lll_agg_columns(j)||') '||
1946             substr('cnt_'||lll_agg_columns(j),1,27)||'_'||j;
1947           end loop;
1948           l_select_sql_inc:=l_select_sql_inc||',count(*) count_all';
1949         end;
1950         -------------------------------------------------------
1951         if b_no_agg then
1952           l_select_no_aggregation := substr(l_select_no_aggregation,1, length(l_select_no_aggregation)-1);
1953           l_select_no_aggregation := l_select_no_aggregation||',decode('||l_source_tables(ll_index)||'.periodicity_id,';
1957         l_select_sql:=l_select_sql||',decode('||l_source_tables(ll_index)||'.periodicity_id,';
1954         end if;
1955          l_select_sql:=substr(l_select_sql,1,length(l_select_sql)-1);
1956         --period_type_id for XTD
1958         for j in 1..l_number_source_tables loop
1959           l_select_sql:=l_select_sql||l_table_periodicity(j)||','||l_table_period_type_id(j)||',';
1960           l_select_no_aggregation := l_select_no_aggregation|| l_table_periodicity(j)||','||l_table_period_type_id(j)||',';
1961         end loop;
1962         l_select_sql:=l_select_sql||'null) period_type_id';
1963         l_select_no_aggregation := l_select_no_aggregation||'null) period_type_id';
1964         --l_select_sql:=l_select_sql||',decode('||l_source_tables(ll_index)||'.periodicity_id,9,1,7,16,5,32,4,2048,'||
1965         --'3,64,2,4096,1,128,null) period_type_id';
1966         l_group_by_sql:=substr(l_group_by_sql,1,length(l_group_by_sql)-1);
1967         if l_eliminate(ll_index) is not null then
1968           l_eliminate_sql:=substr(l_eliminate_sql,1,length(l_eliminate_sql)-4);
1969         end if;
1970         l_select_basic := l_select_sql;
1971         ----------------------------------------------------------
1972         --process filter
1973         --if there is only 1 base table then the filter is a part of the normal from clause
1974         if l_number_bt_tables=1 then
1975           if l_number_filter>0 then
1976             for j in 1..l_number_filter loop
1977               l_from_sql:=l_from_sql||','||l_filter_from(j);
1978               l_hint_sql := l_hint_sql||l_filter_from(j)||' ';
1979             end loop;
1980             for j in 1..l_num_filter_first_level loop
1981               l_where_sql:=l_where_sql||' and '||l_source_tables(ll_index)||'.'||
1982               l_filter_first_level_fk(j)||'='||l_filter_first_level_alias(j)||'.code ';
1983               --3613094
1984               if bsc_im_utils.is_column_in_object(l_filter_first_level(j),'LANGUAGE') then
1985                 l_where_sql:=l_where_sql||' and '||l_filter_first_level_alias(j)||'.language='''||BSC_IM_UTILS.get_lang||''' ';
1986               end if;
1987             end loop;
1988             for j in 1..l_number_filter loop
1989               l_where_sql:=l_where_sql||' '||l_filter_where(j);
1990             end loop;
1991           end if;
1992         end if;
1993         ----------------------------------------------------------
1994         --process eliminate
1995         --write_to_log_file('process eliminate');
1996         if l_eliminate(ll_index) is not null then
1997           l_where_sql:=l_where_sql||' and not exists (select 1 from '||l_eliminate(ll_index)||' where '||
1998           l_eliminate_sql||')';
1999         end if;--if l_eliminate(ll_index) is not null then
2000         ----------------------------------------------------------
2001         --correct the stmts if required
2002         if l_where_sql=' where 1=1' then
2003           l_where_sql:=null;
2004         end if;
2005         if l_group_by_sql is not null then
2006           l_group_by_sql:=' group by '||l_group_by_sql;
2007         end if;
2008         ----------------------------------------------------------
2009         if b_no_agg then
2010           l_from_sql := replace(l_from_sql, '<SELECT DELIMITER>', l_select_basic);
2011           l_from_sql := replace(l_from_sql, '<GROUP BY DELIMITER>', l_group_by_sql);
2012         end if;
2013         if g_debug then
2014           write_to_log_file_n('select no agg = '||l_select_no_aggregation);
2015           write_to_log_file_n('select sql='||l_select_sql);
2016           write_to_log_file_n('select sql inc='||l_select_sql_inc);
2017           write_to_log_file_n('from sql='||l_from_sql);
2018           write_to_log_file_n('where sql='||l_where_sql);
2019           write_to_log_file_n('group by sql='||l_group_by_sql);
2020           write_to_log_file_n('hint sql='||l_hint_sql);
2021           write_to_log_file_n('keys='||ll_keys_stmt);
2022         end if;
2023         ---------------------------------------------------------------------
2024         --+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2025         --LOAD Intermediate metadata
2026 
2027         -- Add l_hint_sql to l_select_sql;
2028         l_select_sql := 'select '||l_hint_sql||')*/ '||substr(l_select_sql, 7, length(l_select_sql));
2029         if (b_no_agg) then
2030           l_select_sql := 'select '||l_hint_sql||')*/ '||substr(l_select_no_aggregation, 7, length(l_select_no_aggregation));
2031           l_group_by_sql := null;
2032           --if (g_debug) then
2033             --write_to_log_file_n('select sql after no agg changes='||l_select_sql);
2034           --end if;
2035         end if;
2036 
2037         if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',l_select_sql,'SELECT',null)=false then
2038           return false;
2039         end if;
2040         if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',l_select_sql_inc,'SELECT INC',null)=false then
2041           return false;
2042         end if;
2043         if (ll_keys_stmt is not null) then
2044           ll_keys_stmt := substr(ll_keys_stmt, 1, length(ll_keys_stmt)-1);
2045           if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',ll_keys_stmt,'KEYS',null)=false then
2046             return false;
2047           end if;
2048         end if;
2049         if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',l_from_sql,'FROM',null)=false then
2050           return false;
2051         end if;
2052         if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',l_where_sql,'WHERE',null)=false then
2053           return false;
2054         end if;
2058 
2055         if BSC_IM_INT_MD.create_mapping_detail(p_map_name,'BSC',l_group_by_sql,'GROUP BY',null)=false then
2056           return false;
2057         end if;
2059         ---------------------------------------------------------------------
2060       else
2061         if g_debug then
2062           write_to_log_file_n('ll_index=0. Some problem. ID 1');
2063         end if;
2064       end if;--if ll_index>0 then
2065     end loop;--for i in 1..ll_number_distinct_groups loop
2066     ---------------------------------------------------------------
2067     ---------------------------------------------------------------
2068     -------------  ZERO CODE CALCULATIONS -------------------------
2069     ---------------------------------------------------------------
2070     declare
2071       ----------------------
2072       ll_union_creator BSC_IM_UTILS.number_tabletype;--does 3C1+3C2+3C3 etc
2073       ll_start number;
2074       ll_run number;
2075       ll_pointer number;
2076       ----------------------
2077       --what union has what keys
2078       ll_union_table BSC_IM_UTILS.number_tabletype;
2079       ll_union_keys BSC_IM_UTILS.varchar_tabletype;
2080       ll_union_key_values BSC_IM_UTILS.varchar_tabletype;
2081       ll_number_union_table number;
2082       ll_union_number number;
2083       ----------------------
2084       ll_count number;
2085       ----------------------
2086       ll_max_rollup_keys_for_union number;
2087       ll_use_union_for_rollup boolean;
2088       ----------------------
2089       ll_keys varchar2(10000);
2090     begin
2091       ll_number_rollup_fk:=0;
2092       ll_use_union_for_rollup:=true;
2093       ll_max_rollup_keys_for_union:=3;--hardcoding it to 3 for now.
2094       if g_debug then
2095         write_to_log_file_n('-------------------------------------------');
2096         write_to_log_file('PROCESSING ZERO CODE');
2097         write_to_log_file('-------------------------------------------');
2098         write_to_log_file('no. of parameters='||l_number_parameters);
2099       end if;
2100       for j in 1..l_number_parameters loop
2101         if l_calculation_table(j)=l_tables(ll_index) then
2102           if BSC_IM_UTILS.in_array(ll_rollup_fk,ll_number_rollup_fk,l_parameter1(j))=false then
2103             ll_number_rollup_fk:=ll_number_rollup_fk+1;
2104             ll_rollup_fk(ll_number_rollup_fk):=l_parameter1(j);
2105             ll_rollup_fk_value(ll_number_rollup_fk):=l_parameter2(j);
2106           end if;
2107         end if;
2108       end loop;
2109       if g_debug then
2110         write_to_log_file_n('The FK for zero code and value');
2111         for j in 1..ll_number_rollup_fk loop
2112           write_to_log_file(ll_rollup_fk(j)||' '||ll_rollup_fk_value(j));
2113         end loop;
2114       end if;
2115       if ll_number_rollup_fk>0 then
2116         --see if we want to use union all or we want to only go for cube
2117         if ll_number_rollup_fk>ll_max_rollup_keys_for_union then
2118           ll_use_union_for_rollup:=false;
2119           if g_debug then
2120             write_to_log_file_n('Not using union all as number of rollup keys > '||ll_max_rollup_keys_for_union);
2121           end if;
2122         else
2123           ll_use_union_for_rollup:=true;
2124           if g_debug then
2125             write_to_log_file_n('Using union for zero code mv');
2126           end if;
2127         end if;
2128         --first make the entries for the unions
2129         --we need to have a array as
2130         /*
2131         The union array and the keys
2132         1 CODE_CSO
2133         2 CODE_DIVISION
2134         3 CODE_WAREHOUSE
2135         4 CODE_CSO
2136         4 CODE_DIVISION
2137         5 CODE_CSO
2138         5 CODE_WAREHOUSE
2139         6 CODE_DIVISION
2140         6 CODE_WAREHOUSE
2141         7 CODE_CSO
2142         7 CODE_DIVISION
2143         7 CODE_WAREHOUSE
2144 
2145         1,2,3 = 1 at a time
2146         4,5,6 = 2 at a time
2147         7     = 3 at a time
2148         */
2149         ll_number_union_table:=0;
2150         ll_union_number:=0;
2151         for j in 1..ll_number_rollup_fk loop --keys at a time looking at
2152           for k in 1..j loop
2153             ll_union_creator(k):=k;
2154           end loop;
2155           ll_pointer:=j;--the last element
2156           -----------
2157           --add the keys into the union pl/sql table
2158           --ll_union_number BSC_IM_UTILS.number_tabletype;
2159           --ll_union_keys BSC_IM_UTILS.varchar_tabletype;
2160           --ll_number_union_table number;
2161           loop
2162             ll_union_number:=ll_union_number+1;
2163             for k in 1..j loop
2164               ll_number_union_table:=ll_number_union_table+1;
2165               ll_union_table(ll_number_union_table):=ll_union_number;
2166               ll_union_keys(ll_number_union_table):=ll_rollup_fk(ll_union_creator(k));
2167               ll_union_key_values(ll_number_union_table):=ll_rollup_fk_value(ll_union_creator(k));
2168             end loop;
2169             if ll_union_creator(ll_pointer)+1 > ll_number_rollup_fk then
2170               <<pointer_start>>
2171               ll_pointer:=ll_pointer-1;
2172               if ll_pointer<1 then
2173                 exit;
2174               else
2175                 loop
2176                   ll_union_creator(ll_pointer):=ll_union_creator(ll_pointer)+1;
2177                   ll_count:=0;
2178                   for m in ll_pointer+1..j loop
2182                   if ll_union_creator(j)>ll_number_rollup_fk then
2179                     ll_count:=ll_count+1;
2180                     ll_union_creator(m):=ll_union_creator(ll_pointer)+ll_count;
2181                   end loop;
2183                     goto pointer_start;
2184                   else
2185                     ll_pointer:=j;
2186                     exit;--from the inner loop
2187                   end if;
2188                 end loop;
2189               end if;
2190             else
2191               ll_union_creator(ll_pointer):=ll_union_creator(ll_pointer)+1;
2192             end if;
2193           end loop;
2194           -----------
2195         end loop;
2196         if g_debug then
2197           write_to_log_file_n('The union array and the keys');
2198           for j in 1..ll_number_union_table loop
2199             write_to_log_file(ll_union_table(j)||' '||ll_union_keys(j)||' '||ll_union_key_values(j));
2200           end loop;
2201         end if;
2202         -------------------------------------------
2203         --at this point, we have the info on union for 1 at a time, 2 at a time etc
2204         l_rollup_full_select_sql:=' select ';
2205         l_rollup_full_from_sql:=' from '||bsc_im_utils.get_table_owner(p_mv_name)||'.'||p_mv_name||' '||p_mv_name;
2206         l_rollup_full_where_sql:=null;
2207         l_rollup_full_group_by_sql:=' group by ';
2208         --loop for each union
2209         for j in 1..ll_union_number loop
2210           l_rollup_select_sql(j):=' select ';
2211           l_rollup_from_sql(j):=' from '||p_mv_name;
2212           l_rollup_where_sql(j):=null;
2213           l_rollup_group_by_sql(j):=' group by ';
2214         end loop;
2215         -------first the FKs--------------------------
2216         if g_debug then
2217           write_to_log_file_n('going to process the keys');
2218         end if;
2219         declare
2220           ll_index number;
2221           ll_fk_cube BSC_IM_UTILS.varchar_tabletype;
2222           ll_number_fk_cube number;
2223         begin
2224           if ll_use_union_for_rollup then
2225             for j in 1..ll_union_number loop
2226               for k in 1..l_number_fk loop
2227                 ll_index:=0;
2228                 ll_index:=BSC_IM_UTILS.get_index(ll_union_keys,ll_union_table,ll_number_union_table,l_fk(k),j);
2229                 if ll_index>0 then --this key is in the rollup
2230                   l_rollup_select_sql(j):=l_rollup_select_sql(j)||ll_union_key_values(ll_index)||' '||
2231                   ll_union_keys(ll_index)||',';
2232                 else
2233                   l_rollup_select_sql(j):=l_rollup_select_sql(j)||l_fk(k)||',';
2234                   l_rollup_group_by_sql(j):=l_rollup_group_by_sql(j)||l_fk(k)||',';
2235                 end if;
2236               end loop;
2237             end loop;
2238           end if;
2239           --the full refresh sql------------------------
2240           ----------------------------------------------
2241           if g_debug then
2242             write_to_log_file_n('going to process full refresh keys');
2243           end if;
2244           ll_number_fk_cube:=0;
2245           for k in 1..l_number_fk loop
2246             ll_index:=0;
2247             ll_index:=BSC_IM_UTILS.get_index(ll_rollup_fk,ll_number_rollup_fk,l_fk(k));
2248             if ll_index>0 then --this key is in the rollup
2249               l_rollup_full_select_sql:=l_rollup_full_select_sql||'decode(grouping('||ll_rollup_fk(ll_index)||'),'||
2250               '1,'||ll_rollup_fk_value(ll_index)||','||ll_rollup_fk(ll_index)||') '||ll_rollup_fk(ll_index)||',';
2251               ll_number_fk_cube:=ll_number_fk_cube+1;
2252               ll_fk_cube(ll_number_fk_cube):=l_fk(k);
2253             else
2254               l_rollup_full_select_sql:=l_rollup_full_select_sql||l_fk(k)||',';
2255               l_rollup_full_group_by_sql:=l_rollup_full_group_by_sql||l_fk(k)||',';
2256             end if;
2257             ll_keys := ll_keys||l_fk(k)||',';
2258           end loop;
2259           l_rollup_full_group_by_sql:=l_rollup_full_group_by_sql||' CUBE(';
2260           for k in 1..ll_number_fk_cube loop
2261             l_rollup_full_group_by_sql:=l_rollup_full_group_by_sql||ll_fk_cube(k)||',';
2262           end loop;
2263           l_rollup_full_group_by_sql:=substr(l_rollup_full_group_by_sql,1,length(l_rollup_full_group_by_sql)-1)||') ';
2264           --having...
2265           l_rollup_full_group_by_sql:=l_rollup_full_group_by_sql||' having (';
2266           for k in 1..ll_number_fk_cube loop
2267             l_rollup_full_group_by_sql:=l_rollup_full_group_by_sql||' grouping('||ll_fk_cube(k)||')=1 or';
2268           end loop;
2269           l_rollup_full_group_by_sql:=substr(l_rollup_full_group_by_sql,1,length(l_rollup_full_group_by_sql)-2)||
2270           ')';
2271         end;
2272         ----------------------------------------
2273         --second the measures------------------------
2274         if g_debug then
2275           write_to_log_file_n('going to process the measures');
2276         end if;
2277         declare
2278           ll_looked_at BSC_IM_UTILS.varchar_tabletype;
2279           ll_number_looked_at number;
2280         begin
2281           if ll_use_union_for_rollup then
2282             for j in 1..ll_union_number loop
2283               ll_number_looked_at:=0;
2284               for k in 1..l_number_cols loop
2285                 if l_col_type(k)='A' then
2286                   for m in 1..l_number_parameters loop
2290                         l_parameter5(m)||' '||l_parameter3(m)||',';
2287                     if lower(l_parameter3(m))=lower(l_cols(k)) then
2288                       if BSC_IM_UTILS.in_array(ll_looked_at,ll_number_looked_at,l_parameter3(m))=false then
2289                         l_rollup_select_sql(j):=l_rollup_select_sql(j)||
2291                         l_rollup_select_sql(j):=l_rollup_select_sql(j)||'count('||l_parameter3(m)||') '||
2292                         substr('cnt_'||l_parameter3(m),1,30)||',';
2293                         ll_number_looked_at:=ll_number_looked_at+1;
2294                         ll_looked_at(ll_number_looked_at):=l_parameter3(m);
2295                       end if;
2296                       exit;
2297                     end if;
2298                   end loop;--for m in 1..l_number_parameters loop
2299                 end if;
2300               end loop;--for k in 1..l_number_cols loop
2301             end loop;--for j in 1..ll_union_number loop
2302           end if;
2303           --the full refresh sql------------------------
2304           ----------------------------------------------
2305           if g_debug then
2306             write_to_log_file_n('going to process rollup measures');
2307           end if;
2308           ll_number_looked_at:=0;
2309           for k in 1..l_number_cols loop
2310             if l_col_type(k)='A' then
2311               for m in 1..l_number_parameters loop
2312                 if lower(l_parameter3(m))=lower(l_cols(k)) then
2313                   if BSC_IM_UTILS.in_array(ll_looked_at,ll_number_looked_at,l_parameter3(m))=false then
2314                     l_rollup_full_select_sql:=l_rollup_full_select_sql||
2315                     l_parameter5(m)||' '||l_parameter3(m)||',';
2316                     ll_number_looked_at:=ll_number_looked_at+1;
2317                     ll_looked_at(ll_number_looked_at):=l_parameter3(m);
2318                   end if;
2319                   exit;
2320                 end if;
2321               end loop;--for m in 1..l_number_parameters loop
2322             end if;
2323           end loop;--for k in 1..l_number_cols loop
2324         end;
2325         ----------------------------------------------------
2326         if ll_use_union_for_rollup then
2327           for j in 1..ll_union_number loop
2328             l_rollup_select_sql(j):=l_rollup_select_sql(j)||' count(*) count_all,'||j||' u_marker,';
2329             l_rollup_select_sql(j):=l_rollup_select_sql(j)||'decode(periodicity_id,';
2330             for k in 1..l_number_source_tables loop
2331               l_rollup_select_sql(j):=l_rollup_select_sql(j)||l_table_periodicity(k)||','||
2332               l_table_period_type_id(k)||',';
2333             end loop;
2334             l_rollup_select_sql(j):=l_rollup_select_sql(j)||'null) period_type_id';
2335             --'decode(periodicity_id,9,1,7,16,5,32,4,2048,3,64,2,4096,1,128,null) period_type_id';
2336             l_rollup_group_by_sql(j):=substr(l_rollup_group_by_sql(j),1,length(l_rollup_group_by_sql(j))-1);
2337           end loop;
2338         end if;
2339         l_rollup_full_select_sql:=l_rollup_full_select_sql||'decode(periodicity_id,';
2340         for j in 1..l_number_source_tables loop
2341           l_rollup_full_select_sql:=l_rollup_full_select_sql||l_table_periodicity(j)||','||
2342           l_table_period_type_id(j)||',';
2343         end loop;
2344         l_rollup_full_select_sql:=l_rollup_full_select_sql||'null) period_type_id';
2345         --l_rollup_full_select_sql:=l_rollup_full_select_sql||
2346         --'decode(periodicity_id,9,1,7,16,5,32,4,2048,3,64,2,4096,1,128,null) period_type_id';
2347         ---------------------------------------------
2348         if g_debug then
2349           write_to_log_file_n('The rollup select from where and group by');
2350           if ll_use_union_for_rollup then
2351             for j in 1..ll_union_number loop
2352               write_to_log_file('Union '||j);
2353               write_to_log_file('select - '||l_rollup_select_sql(j));
2354               write_to_log_file('from - '||l_rollup_from_sql(j));
2355               write_to_log_file('where - '||l_rollup_where_sql(j));
2356               write_to_log_file('group by - '||l_rollup_group_by_sql(j));
2357             end loop;
2358           end if;
2359           write_to_log_file_n('The FULL rollup select from where and group by');
2360           write_to_log_file('FULL select - '||l_rollup_full_select_sql);
2361           write_to_log_file('FULL from - '||l_rollup_full_from_sql);
2362           write_to_log_file('FULL where - '||l_rollup_full_where_sql);
2363           write_to_log_file('FULL group by - '||l_rollup_full_group_by_sql);
2364           write_to_log_file('keys= '||ll_keys);
2365         end if;
2366         ------------------------------------------------------------
2367         ---------create INT metadata entry for rollups
2368         ------------------------------------------------------------
2369         if (ll_keys_stmt like '%,') then
2370           ll_keys_stmt := substr(ll_keys_stmt, 1, length(ll_keys_stmt)-1);
2371         end if;
2372         if ll_use_union_for_rollup then
2373           --only if union all is allowed for zero code mv do we write this to the int metadata
2374           --if there are too many keys will rollup, then we want to go over cube(...)
2375           for j in 1..ll_union_number loop
2379             if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_from_sql(j),'FROM',null)=false then
2376             if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_select_sql(j),'SELECT',null)=false then
2377               return false;
2378             end if;
2380               return false;
2381             end if;
2382             if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_where_sql(j),'WHERE',null)=false then
2383               return false;
2384             end if;
2385             if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_group_by_sql(j),'GROUP BY',null)=false then
2386               return false;
2387             end if;
2388           end loop;
2389           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',ll_keys_stmt,'KEYS',null)=false then
2390             return false;
2391           end if;
2392         else
2393           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_full_select_sql,
2394             'SELECT',null)=false then
2395             return false;
2396           end if;
2397           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_full_from_sql,
2398             'FROM',null)=false then
2399             return false;
2400           end if;
2401           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_full_where_sql,
2402             'WHERE',null)=false then
2403             return false;
2404           end if;
2405           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',l_rollup_full_group_by_sql,
2406             'GROUP BY',null)=false then
2407             return false;
2408           end if;
2409           if BSC_IM_INT_MD.create_mapping_detail(p_zero_code_map_name,'BSC',ll_keys_stmt,'KEYS',null)=false then
2410             return false;
2411           end if;
2412         end if;
2413         if BSC_IM_INT_MD.create_mapping_detail(l_full_zero_code_map_name,'BSC',l_rollup_full_select_sql,
2414           'SELECT',null)=false then
2415           return false;
2416         end if;
2417         if BSC_IM_INT_MD.create_mapping_detail(l_full_zero_code_map_name,'BSC',l_rollup_full_from_sql,
2418           'FROM',null)=false then
2419           return false;
2420         end if;
2421         if BSC_IM_INT_MD.create_mapping_detail(l_full_zero_code_map_name,'BSC',l_rollup_full_where_sql,
2422           'WHERE',null)=false then
2423           return false;
2424         end if;
2425         if BSC_IM_INT_MD.create_mapping_detail(l_full_zero_code_map_name,'BSC',l_rollup_full_group_by_sql,
2426           'GROUP BY',null)=false then
2427           return false;
2428         end if;
2429         if BSC_IM_INT_MD.create_mapping_detail(l_full_zero_code_map_name,'BSC',ll_keys_stmt,'KEYS',null)=false then
2430           return false;
2431         end if;
2432 
2433       end if;--if ll_number_rollup_fk>0 then
2434     end;
2435     --create INT Metadata for mapping
2436     --for zero code mv
2437     declare
2438     begin
2439       if g_debug then
2440         write_to_log_file_n('In Create_kpi_map_info--Before Create_object ZEro Code MVs'||' '||get_time);
2441         write_to_log_file_n('ll_number_rollup_fk='||ll_number_rollup_fk);
2442       end if;
2443       if ll_number_rollup_fk>0 and ll_zero_separate then
2444         if BSC_IM_INT_MD.create_object(
2445           p_zero_code_mv_name,
2446           'ZERO CODE MV',
2447           'BSC',
2448           p_indicator,
2449           null,
2450           'ZERO CODE MV')=false then
2451           return false;
2452         end if;
2453         if BSC_IM_INT_MD.create_mapping(p_zero_code_map_name,'BSC','ZERO CODE MV',p_zero_code_mv_name,
2454           'FAST REFRESH')=false then
2455           return false;
2456         end if;
2457         if BSC_IM_INT_MD.create_mapping(l_full_zero_code_map_name,'BSC','ZERO CODE MV',p_zero_code_mv_name,
2458           'FULL REFRESH')=false then
2459           return false;
2460         end if;
2461         --create the fk, this is reqd for index creation
2462         for i in 1..l_number_fk loop
2463           if BSC_IM_INT_MD.create_fk(
2464             l_fk(i),
2465             'ZERO CODE MV',
2466             p_zero_code_mv_name,
2467             null,
2468             null,
2469             null,
2470             'BSC',
2471             'ZERO CODE MV')=false then
2472             return false;
2473           end if;
2474         end loop;
2475         --create the mv dependency. this is for snapshot log creation
2476         if BSC_IM_INT_MD.create_object(
2477           p_mv_name,
2478           'ZERO CODE MV',
2479           'BSC',
2480           p_zero_code_mv_name,
2481           null,
2482           'MV DEPENDENCY')=false then
2483           return false;
2484         end if;
2485         if g_debug then
2486 	      write_to_log_file_n('In Create_kpi_map_info--Before Create_object ZEro Code MVs'||' '||get_time);
2487         end if;
2488       end if;
2489     end;
2490     -----------------------------------------------------
2491     --create the fk and columns for dim levels. this info will be used for snapshot log creation.
2492     --we need snapshot logs also on the dim level tables for inc refresh
2493     --l_number_dim_level_tables
2494     declare
2495       ll_level_columns BSC_IM_UTILS.varchar_tabletype;
2496       ll_level_column_type BSC_IM_UTILS.varchar_tabletype;
2497       ll_number_level_columns number;
2498     begin
2499       for i in 1..l_number_dim_level_tables loop
2500         if get_dim_level_cols(
2501           l_dim_level_tables(i),
2505           return false;
2502           ll_level_columns,
2503           ll_level_column_type,
2504           ll_number_level_columns)=false then
2506         end if;
2507         if ll_number_level_columns>0 then
2508           if BSC_IM_INT_MD.create_fk('CODE','LEVEL TABLE',l_dim_level_tables(i),null,null,null,'BSC',
2509             'LEVEL TABLE')=false then
2510             return false;
2511           end if;
2512           --3613094
2513           if bsc_im_utils.is_column_in_object(l_dim_level_tables(i),'LANGUAGE') then
2514             if BSC_IM_INT_MD.create_fk('LANGUAGE','LEVEL TABLE',l_dim_level_tables(i),null,null,null,'BSC',
2515               'LEVEL TABLE')=false then
2516               return false;
2517             end if;
2518           end if;
2519           for j in 1..ll_number_level_columns loop
2520             if upper(ll_level_columns(j))<>'CODE' and upper(ll_level_columns(j))<>'LANGUAGE' then
2521               if BSC_IM_INT_MD.create_column(ll_level_columns(j),'A',null,'BSC',null,null,null,l_dim_level_tables(i),
2522                 null)=false then
2523                 return false;
2524               end if;
2525             end if;
2526           end loop;
2527         end if;
2528       end loop;
2529       --if there is filter, make an entry also for the filter table
2530       if l_number_filter>0 then
2531         if BSC_IM_INT_MD.create_fk('SOURCE_TYPE','LEVEL TABLE','BSC_SYS_FILTERS',null,null,null,'BSC',
2532           'LEVEL TABLE')=false then
2533           return false;
2534         end if;
2535         if BSC_IM_INT_MD.create_fk('SOURCE_CODE','LEVEL TABLE','BSC_SYS_FILTERS',null,null,null,'BSC',
2536           'LEVEL TABLE')=false then
2537           return false;
2538         end if;
2539         if BSC_IM_INT_MD.create_fk('DIM_LEVEL_ID','LEVEL TABLE','BSC_SYS_FILTERS',null,null,null,'BSC',
2540           'LEVEL TABLE')=false then
2541           return false;
2542         end if;
2543         if BSC_IM_INT_MD.create_fk('DIM_LEVEL_VALUE','LEVEL TABLE','BSC_SYS_FILTERS',null,null,null,'BSC',
2544           'LEVEL TABLE')=false then
2545           return false;
2546         end if;
2547         if BSC_IM_UTILS.in_array(l_dim_level_tables,l_number_dim_level_tables,'BSC_SYS_FILTERS')=false then
2548           l_number_dim_level_tables:=l_number_dim_level_tables+1;
2549           l_dim_level_tables(l_number_dim_level_tables):='BSC_SYS_FILTERS';
2550         end if;
2551       end if;
2552     end;
2553     ----------------------------------------------------
2554     declare
2555       ll_property varchar2(10000);
2556     begin
2557       for i in 1..l_number_dim_level_tables loop
2558         l_dim_level_stmt:=l_dim_level_stmt||l_dim_level_tables(i)||'+';
2559       end loop;
2560       if g_debug then
2561         write_to_log_file_n('dim level stmt '||l_dim_level_stmt);
2562       end if;
2563       if l_dim_level_stmt='DIM LEVELS=' then --these are used to create snp logs on the dim levels
2564         l_dim_level_stmt:=null;
2565       else
2566         l_dim_level_stmt:=substr(l_dim_level_stmt,1,length(l_dim_level_stmt)-1);
2567       end if;
2568       if l_base_table_stmt is not null and l_dim_level_stmt is not null then
2569         ll_property:=l_base_table_stmt||','||l_dim_level_stmt;
2570       else
2571         ll_property:=l_base_table_stmt||l_dim_level_stmt;
2572       end if;
2573       if BSC_IM_INT_MD.create_mapping(p_map_name,'BSC','SUMMARY MV',p_mv_name,ll_property)=false then
2574         return false;
2575       end if;
2576     end;
2577     -----------------------------------------------------
2578   end;
2579   -------------------------------------------------------
2580   if g_debug then
2581          write_to_log_file_n('Returning from Create_kpi_map_info'||' '||get_time);
2582   end if;
2583   return true;
2584 Exception when others then
2585   BSC_IM_UTILS.g_status_message:=sqlerrm;
2586   g_status_message:=sqlerrm;
2587   write_to_log_file_n('Exception in create_kpi_map_info '||sqlerrm);
2588   return false;
2589 End;
2590 
2591 --used for T table. will go recursively all the way back to the B table and generate the sql.
2592 function get_table_sql(
2593 p_table varchar2,
2594 p_table_sql out nocopy varchar2,
2595 p_b_tables in out nocopy BSC_IM_UTILS.varchar_tabletype,
2596 p_number_b_tables in out nocopy number,
2597 p_dim_level_tables in out nocopy BSC_IM_UTILS.varchar_tabletype,
2598 p_number_dim_level_tables in out nocopy number
2599 )return boolean is
2600 -----------------------------------------------------------------
2601 l_tables BSC_IM_UTILS.varchar_tabletype;
2602 l_source_tables BSC_IM_UTILS.varchar_tabletype;
2603 l_relation_type BSC_IM_UTILS.varchar_tabletype;
2604 l_source_sql BSC_IM_UTILS.varchar_tabletype;
2605 l_number_source_tables number;
2606 -----------T table columns--------------------------------------
2607 l_col_table BSC_IM_UTILS.varchar_tabletype;
2608 l_cols BSC_IM_UTILS.varchar_tabletype;
2609 l_col_type BSC_IM_UTILS.varchar_tabletype;
2610 l_col_formula BSC_IM_UTILS.varchar_tabletype;
2611 l_col_source BSC_IM_UTILS.varchar_tabletype;
2612 l_number_cols number;
2613 -----------------------------------------------------------------
2614 l_fk BSC_IM_UTILS.varchar_tabletype;
2615 l_fk_source BSC_IM_UTILS.varchar_tabletype;
2616 l_number_fk number;
2617 l_measures BSC_IM_UTILS.varchar_tabletype;
2618 l_number_measures number;
2619 -----------------------------------------------------------------
2620 l_column_merge_sql varchar2(32000);
2621 l_column_merge_group BSC_IM_UTILS.varchar_tabletype;
2622 l_column_merge_group_sql BSC_IM_UTILS.varchar_tabletype;
2623 l_number_column_merge_group number;
2627 l_number_table_measures number;
2624 l_merge_sql varchar2(32000);
2625 -----------------------------------------------------------------
2626 l_table_measures BSC_IM_UTILS.varchar_tabletype;
2628 l_index number;
2629 -----------------------------------------------------------------
2630 l_from_sql varchar2(32000);
2631 l_where_sql varchar2(32000);
2632 l_group_by_sql varchar2(32000);
2633 -----------------------------------------------------------------
2634 l_temp varchar2(4000);
2635 l_temp_alias varchar2(200);
2636 l_dim_src_object varchar2(20000);
2637 l_dim_src_object_type varchar2(100);
2638 l_rec_dim boolean;
2639 l_rec_dim_key varchar2(100);
2640 -----------------------------------------------------------------
2641 Begin
2642   if g_debug then
2643     write_to_log_file_n('In get_table_sql '||p_table);
2644   end if;
2645   if get_table_fks(p_table,l_fk,l_number_fk)=false then
2646     return false;
2647   end if;
2648   if get_table_measures(p_table,l_measures,l_number_measures)=false then
2649     return false;
2650   end if;
2651   l_number_source_tables:=null;
2652   if get_table_relations(
2653     p_table,
2654     l_tables,
2655     l_source_tables,
2656     l_relation_type,
2657     l_number_source_tables)=false then
2658     return false;
2659   end if;
2660   for i in 1..l_number_source_tables loop
2661     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_T_') then --this is T table
2662       --construct the sql. recursive call
2663       if get_table_sql(l_source_tables(i),l_source_sql(i),p_b_tables,p_number_b_tables,
2664         p_dim_level_tables,p_number_dim_level_tables)=false then
2665         return false;
2666       end if;
2667     else
2668       l_source_sql(i):=null;
2669     end if;
2670   end loop;
2671   for i in 1..l_number_source_tables loop
2672     if BSC_IM_UTILS.is_like(l_source_tables(i),'BSC_B_') and BSC_IM_UTILS.in_array(p_b_tables,p_number_b_tables,
2673       l_source_tables(i))=false then
2674       p_number_b_tables:=p_number_b_tables+1;
2675       p_b_tables(p_number_b_tables):=l_source_tables(i);
2676     end if;
2677   end loop;
2678   --get the column information
2679   l_number_cols:=null;
2680   if get_table_cols(
2681     p_table,
2682     l_col_table,
2683     l_cols,
2684     l_col_type,
2685     l_col_source,
2686     l_col_formula,
2687     l_number_cols)=false then
2688     return false;
2689   end if;
2690   /*P1 3649545
2691   when T table rolls up, we cannot use l_fk() in l_column_merge_sql. l_fk is the fk
2692   of the higher level. we need to get the source of l_fk and use it
2693   */
2694   for i in 1..l_number_fk loop
2695     l_index:=BSC_IM_UTILS.get_index(l_cols,l_number_cols,l_fk(i));
2696     if l_index>0 then
2697       l_fk_source(i):=l_col_source(l_index);
2698     else
2699       l_fk_source(i):=l_fk(i);
2700     end if;
2701   end loop;
2702   if g_debug then
2703     write_to_log_file_n('The fk source');
2704     for i in 1..l_number_fk loop
2705       write_to_log_file(l_fk(i)||' '||l_fk_source(i));
2706     end loop;
2707   end if;
2708   ----------------------
2709   l_column_merge_sql:=null;
2710   l_merge_sql:=null;
2711   l_number_column_merge_group:=0;
2712   l_from_sql:=null;
2713   l_where_sql:=null;
2714   l_group_by_sql:=null;
2715   --for T tables, there is only column merge!!
2716   if l_number_source_tables>1 then
2717     for i in 1..l_number_source_tables loop
2718       l_number_column_merge_group:=l_number_column_merge_group+1;
2719       l_column_merge_group(l_number_column_merge_group):=l_source_tables(i);
2720       l_column_merge_group_sql(l_number_column_merge_group):=l_source_sql(i);
2721     end loop;
2722     if g_debug then
2723       write_to_log_file_n('The column merge tables');
2724       for i in 1..l_number_column_merge_group loop
2725         write_to_log_file(l_column_merge_group(i)||' '||l_column_merge_group_sql(i));
2726       end loop;
2727     end if;
2728     if l_number_column_merge_group>0 then
2729       l_column_merge_sql:='select ';
2730       for i in 1..l_number_fk loop
2731         --l_column_merge_sql:=l_column_merge_sql||'prim.'||l_fk_source(i)||',';
2732         l_column_merge_sql:=l_column_merge_sql||'prim.'||l_fk(i)||',';
2733       end loop;
2734       for i in 1..l_number_measures loop
2735         l_column_merge_sql:=l_column_merge_sql||l_measures(i)||',';
2736       end loop;
2737       l_column_merge_sql:=substr(l_column_merge_sql,1,length(l_column_merge_sql)-1)||' from (';
2738       for i in 1..l_number_column_merge_group loop
2739         l_column_merge_sql:=l_column_merge_sql||'select ';
2740         for j in 1..l_number_fk loop
2741           --l_column_merge_sql:=l_column_merge_sql||l_fk_source(j)||',';
2742           l_column_merge_sql:=l_column_merge_sql||l_fk_source(j)||' '||l_fk(j)||',';
2743         end loop;
2744         l_column_merge_sql:=substr(l_column_merge_sql,1,length(l_column_merge_sql)-1);
2745         if l_column_merge_group_sql(i) is null then
2746           l_column_merge_sql:=l_column_merge_sql||' from '||l_column_merge_group(i)||' union ';
2747         else
2748           l_column_merge_sql:=l_column_merge_sql||' from ('||
2749           l_column_merge_group_sql(i)||') '||l_column_merge_group(i)||' union ';
2750         end if;
2751       end loop;
2752       l_column_merge_sql:=substr(l_column_merge_sql,1,length(l_column_merge_sql)-6);
2753       l_column_merge_sql:=l_column_merge_sql||') prim ';
2754       for i in 1..l_number_column_merge_group loop
2758           l_column_merge_sql:=l_column_merge_sql||',('||l_column_merge_group_sql(i)||') '||
2755         if l_column_merge_group_sql(i) is null then
2756           l_column_merge_sql:=l_column_merge_sql||','||l_column_merge_group(i);
2757         else
2759           l_column_merge_group(i);
2760         end if;
2761       end loop;
2762       l_column_merge_sql:=l_column_merge_sql||' where ';
2763       for i in 1..l_number_column_merge_group loop
2764         for j in 1..l_number_fk loop
2765           --l_column_merge_sql:=l_column_merge_sql||'prim.'||l_fk_source(j)||'='||l_column_merge_group(i)||'.'||
2766           --l_fk_source(j)||'(+) and ';
2767           l_column_merge_sql:=l_column_merge_sql||'prim.'||l_fk(j)||'='||l_column_merge_group(i)||'.'||
2768           l_fk_source(j)||'(+) and ';
2769         end loop;
2770       end loop;
2771       l_column_merge_sql:=substr(l_column_merge_sql,1,length(l_column_merge_sql)-4);
2772     end if;
2773     if g_debug then
2774       write_to_log_file_n('Column merge sql 2 '||l_column_merge_sql);
2775     end if;
2776     l_merge_sql:=l_column_merge_sql;
2777     if g_debug then
2778      write_to_log_file_n('The merge sql '||l_merge_sql);
2779     end if;
2780     l_from_sql:=' from ('||l_merge_sql||') prim_table';
2781   else --if l_number_source_tables>1 then
2782     if l_source_sql(l_number_source_tables) is null then
2783       l_from_sql:=' from ';
2784       if (instr(l_source_tables(l_number_source_tables), 'BSC_B_')=1) then
2785         -- Bug 5073442
2786         -- get prj union clause will return (B union B_PRJ) aliased as B
2787         -- for T tables, we should remove this alias as we're going to use the prim_table alias
2788         l_from_sql:= l_from_sql||get_prj_union_clause(l_source_tables(l_number_source_tables), false);
2789       else
2790         l_from_sql := l_from_sql||bsc_im_utils.get_table_owner(l_source_tables(l_number_source_tables))
2791                                 ||'.'||l_source_tables(l_number_source_tables);
2792       end if;
2793       l_from_sql := l_from_sql||' prim_table';
2794     else
2795       l_from_sql:=' from ('||l_source_sql(l_number_source_tables)||') prim_table';
2796     end if;
2797   end if;--if l_number_source_tables>1 then
2798   p_table_sql:='select ';
2799   l_where_sql:=' where 1=1';
2800   for i in 1..l_number_fk loop
2801     if l_fk_source(i)<>l_fk(i) then --there is a rollup
2802       --search for get_level_for_pk. you can see an explanation for why we need l_dim_src_object
2803       --l_dim_src_object is the name of the materialized dim table
2804       if get_level_for_pk(l_fk_source(i),l_temp,l_dim_src_object,l_dim_src_object_type,
2805         l_rec_dim,l_rec_dim_key)=false then
2806         return false;
2807       end if;
2808       l_temp_alias:=substr(l_temp,1,30-length(i))||i;
2809       p_table_sql:=p_table_sql||l_temp_alias||'.'||l_fk(i)||',';
2810       if l_dim_src_object_type='inline' then
2811         l_from_sql:=l_from_sql||','||l_dim_src_object||' '||l_temp_alias;
2812       else
2813         l_from_sql:=l_from_sql||','||bsc_im_utils.get_table_owner(l_dim_src_object)||'.'||l_dim_src_object||' '||l_temp_alias;
2814       end if;
2815       if l_number_source_tables=1 then
2816         l_where_sql:=l_where_sql||' and '||l_temp_alias||'.CODE=prim_table.'||l_fk_source(i);
2817       else
2818         l_where_sql:=l_where_sql||' and '||l_temp_alias||'.CODE=prim_table.'||l_fk(i);
2819       end if;
2820       --bug 3344807
2821       --when the dimension has multiple languages, we must filter by language
2822       --3613094
2823       if bsc_im_utils.is_column_in_object(l_dim_src_object,'LANGUAGE') then
2824         l_where_sql:=l_where_sql||' and '||l_temp_alias||'.LANGUAGE='''||BSC_IM_UTILS.get_lang||''' ';
2825       end if;
2826       l_group_by_sql:=l_group_by_sql||l_temp_alias||'.'||l_fk(i)||',';--l_fk(i)=l_cols(l_index)
2827       if l_dim_src_object_type='none' then
2828         if BSC_IM_UTILS.in_array(p_dim_level_tables,p_number_dim_level_tables,l_dim_src_object)=false then
2829           p_number_dim_level_tables:=p_number_dim_level_tables+1;
2830           p_dim_level_tables(p_number_dim_level_tables):=l_dim_src_object;
2831         end if;
2832       end if;
2833     else
2834       p_table_sql:=p_table_sql||'prim_table.'||l_fk(i)||',';
2835       l_group_by_sql:=l_group_by_sql||'prim_table.'||l_fk(i)||',';
2836     end if;
2837   end loop;--for i in 1..l_number_fk loop
2838   l_group_by_sql:=substr(l_group_by_sql,1,length(l_group_by_sql)-1);
2839   for i in 1..l_number_measures loop
2840     l_index:=0;
2841     l_index:=BSC_IM_UTILS.get_index(l_cols,l_number_cols,l_measures(i));
2842     if l_index>0 then
2843       p_table_sql:=p_table_sql||l_col_formula(l_index)||' '||l_measures(i)||',';
2844     end if;
2845   end loop;
2846   p_table_sql:=substr(p_table_sql,1,length(p_table_sql)-1);
2847   -------------------------------------------------------
2848   --correct stmts if reqd
2849   if l_where_sql=' where 1=1' then
2850     l_where_sql:=null;
2851   end if;
2852   -------------------------------------------------------
2853   if g_debug then
2854     write_to_log_file_n('p_table_sql='||p_table_sql);
2855     write_to_log_file_n('l_from_sql='||l_from_sql);
2856     write_to_log_file_n('l_where_sql='||l_where_sql);
2857     write_to_log_file_n('l_group_by_sql='||l_group_by_sql);
2858   end if;
2859   p_table_sql:=p_table_sql||l_from_sql||l_where_sql||' group by '||l_group_by_sql;
2863   g_status_message:=sqlerrm;
2860   return true;
2861 Exception when others then
2862   BSC_IM_UTILS.g_status_message:=sqlerrm;
2864   write_to_log_file_n('Exception in get_table_sql '||sqlerrm);
2865   return false;
2866 End;
2867 
2868 function get_filter_stmt(
2869 p_indicator number,
2870 p_table varchar2,
2871 p_filter_from out nocopy BSC_IM_UTILS.varchar_tabletype,
2872 p_filter_where out nocopy BSC_IM_UTILS.varchar_tabletype,
2873 p_number_filter out nocopy number,
2874 p_dim_level_tables in out nocopy BSC_IM_UTILS.varchar_tabletype,
2875 p_number_dim_level_tables in out nocopy number,
2876 p_filter_first_level out nocopy BSC_IM_UTILS.varchar_tabletype,
2877 p_filter_first_level_alias out nocopy BSC_IM_UTILS.varchar_tabletype, --this will be the alias L1
2878 p_filter_first_level_fk out nocopy BSC_IM_UTILS.varchar_tabletype,
2879 p_num_filter_first_level out nocopy number
2880 ) return boolean is
2881 --------------------------
2882 l_stmt varchar2(5000);
2883 --------------------------
2884 l_dim_set_id number;
2885 l_indicator_id number;
2886 l_pk_col BSC_IM_UTILS.varchar_tabletype;
2887 l_level_view BSC_IM_UTILS.varchar_tabletype;
2888 l_number_pk_col number;
2889 l_view_owner varchar2(200);
2890 --------------------------
2891 cursor c1(p_indicator number,p_dim_set_id number,p_status number,p_table_name varchar2,p_column_type varchar2) is
2892 SELECT d.level_pk_col, d.level_view_name FROM bsc_kpi_dim_levels_b d, bsc_db_tables_cols c
2893 WHERE d.indicator = p_indicator AND d.dim_set_id = p_dim_set_id AND d.status = p_status AND d.level_view_name <> (
2894 SELECT level_view_name FROM bsc_sys_dim_levels_b s WHERE d.level_pk_col = s.level_pk_col)
2895 AND c.table_name = p_table_name AND c.column_name = d.level_pk_col AND
2896 c.column_type = p_column_type;
2897 
2898 cursor c2(p_indicator number,p_set_id number) is
2899 select level_table_name,level_pk_col,parent_level_rel from bsc_kpi_dim_levels_b
2900 where indicator=p_indicator and dim_set_id=p_set_id;
2901 --------------------------
2902 Begin
2903   if g_debug then
2904     write_to_log_file_n('In get_filter_stmt '||p_table);
2905   end if;
2906   p_number_filter:=0;
2907   p_num_filter_first_level:=0;
2908   --logic taken from BSC_UPDATE_CALC.apply_filters(x_table_name); 115.21 2003/03/25
2909   begin
2910     SELECT indicator, dim_set_id into l_indicator_id,l_dim_set_id
2911     FROM bsc_kpi_data_tables_v WHERE (table_name = p_table OR
2912     table_name = (SELECT DISTINCT table_name FROM bsc_db_calculations WHERE
2913     parameter1 = p_table AND calculation_type = 5)) and rownum=1;
2914   exception when NO_DATA_FOUND then
2915     l_indicator_id:=null;
2916     l_dim_set_id:=null;
2917   end;
2918   if g_debug then
2919     write_to_log_file('Result '||l_indicator_id||' '||l_dim_set_id);
2920   end if;
2921   if l_indicator_id is not null and l_dim_set_id is not null then
2922     if g_debug then
2923       l_stmt:='SELECT d.level_pk_col, d.level_view_name FROM bsc_kpi_dim_levels_b d, bsc_db_tables_cols c '||
2924       'WHERE d.indicator = :1 AND d.dim_set_id = :2 AND d.status = :3 AND d.level_view_name <> ( '||
2925       'SELECT level_view_name FROM bsc_sys_dim_levels_b s WHERE d.level_pk_col = s.level_pk_col) '||
2926       'AND c.table_name = :4 AND c.column_name = d.level_pk_col AND '||
2927       'c.column_type = :5';
2928       write_to_log_file_n(l_stmt||' '||l_indicator_id||' '||l_dim_set_id||' 2 '||p_table||' P');
2929     end if;
2930     open c1(l_indicator_id,l_dim_set_id,2,p_table,'P');
2931     l_number_pk_col:=1;
2932     loop
2933       fetch c1 into l_pk_col(l_number_pk_col),l_level_view(l_number_pk_col);
2934       exit when c1%notfound;
2935       l_number_pk_col:=l_number_pk_col+1;
2936     end loop;
2937     close c1;
2938     l_number_pk_col:=l_number_pk_col-1;
2939     if g_debug then
2940       write_to_log_file('Results');
2941       for i in 1..l_number_pk_col loop
2942         write_to_log_file(l_pk_col(i)||' '||l_level_view(i));
2943       end loop;
2944     end if;
2945     if l_number_pk_col>0 then
2946       declare
2947         ll_level_table_name BSC_IM_UTILS.varchar_tabletype;
2948         ll_level_pk_col BSC_IM_UTILS.varchar_tabletype;
2949         ll_parent_level_pk_col BSC_IM_UTILS.varchar_tabletype;
2950         ll_number_level number;
2951         ---------------------
2952         ll_index number;
2953         ---------------------
2954         ll_pk_col varchar2(200);
2955         ll_from_stmt varchar2(20000);
2956         ll_where_stmt varchar2(20000);
2957         ll_count number;
2958         ll_level_count number;
2959         ---------------------
2960       begin
2961         if g_debug then
2962           l_stmt:='select level_table_name,level_pk_col,parent_level_rel'||
2963           ' from bsc_kpi_dim_levels_b where indicator=:1 and dim_set_id=:2';
2964           write_to_log_file_n(l_stmt||' '||l_indicator_id||' '||l_dim_set_id);
2965         end if;
2966         ll_number_level:=1;
2967         open c2(l_indicator_id,l_dim_set_id);
2968         loop
2972           ll_number_level:=ll_number_level+1;
2969           fetch c2 into ll_level_table_name(ll_number_level),ll_level_pk_col(ll_number_level),
2970           ll_parent_level_pk_col(ll_number_level);
2971           exit when c2%notfound;
2973         end loop;
2974         close c2;
2975         ll_number_level:=ll_number_level-1;
2976         if g_debug then
2977           write_to_log_file_n('Results');
2978           for i in 1..ll_number_level loop
2979             write_to_log_file(ll_level_table_name(i)||' '||ll_level_pk_col(i)||' '||
2980             ll_parent_level_pk_col(i));
2981           end loop;
2982         end if;
2983         ll_from_stmt:=null;
2984         ll_where_stmt:=null;
2985         g_rec_count:=0;
2986         ll_count:=0;
2987         ll_level_count:=0;
2988         for i in 1..l_number_pk_col loop
2989           ll_pk_col:=l_pk_col(i);
2990           if ll_pk_col is not null then
2991             ll_index:=0;
2992             ll_index:=BSC_IM_UTILS.get_index(ll_level_pk_col,ll_number_level,ll_pk_col);
2993             if ll_index>0 then
2994               p_num_filter_first_level:=p_num_filter_first_level+1;
2995               ll_level_count:=ll_level_count+1;
2996               p_filter_first_level(p_num_filter_first_level):=ll_level_table_name(ll_index);
2997               p_filter_first_level_alias(p_num_filter_first_level):='L'||ll_level_count;
2998               p_filter_first_level_fk(p_num_filter_first_level):=ll_pk_col;
2999               if get_filter_stmt_rec(p_indicator, ll_level_table_name(ll_index),ll_pk_col,ll_count,ll_level_count,
3000                 ll_from_stmt,ll_where_stmt, l_pk_col)=false then
3001                 return false;
3002               end if;
3003             end if;
3004           end if;
3005         end loop;--for i in 1..l_number_pk_col loop
3006         ll_from_stmt:=substr(ll_from_stmt,1,length(ll_from_stmt)-1);
3007         if g_debug then
3008           write_to_log_file_n('The filter from '||ll_from_stmt);
3009           write_to_log_file_n('The filter where '||ll_where_stmt);
3010         end if;
3011         p_number_filter:=p_number_filter+1;
3012         p_filter_from(p_number_filter):=ll_from_stmt;
3013         p_filter_where(p_number_filter):=ll_where_stmt;
3014       end;
3015     end if;--if l_number_pk_col>0 then
3016   end if;--if l_indicator_id is not null and l_dim_set_id is not null then
3017   return true;
3018 Exception when others then
3019   BSC_IM_UTILS.g_status_message:=sqlerrm;
3020   g_status_message:=sqlerrm;
3021   write_to_log_file_n('Exception in get_filter_stmt '||sqlerrm);
3022   return false;
3023 End;
3024 
3025 --bug 3394315
3026 function get_filter_stmt_rec(
3027 p_indicator number,
3028 p_child_level varchar,
3029 p_child_level_pk varchar,
3030 p_count in out nocopy number,
3031 p_level_count in out nocopy number,
3032 p_from_stmt in out nocopy varchar2,
3033 p_where_stmt in out nocopy varchar2,
3034 --added by arun
3035 p_pk_cols BSC_IM_UTILS.varchar_tabletype
3036 ) return boolean is
3037 --------------------------------
3038 l_level_fk BSC_IM_UTILS.varchar_tabletype;
3039 l_parent_level BSC_IM_UTILS.varchar_tabletype;
3040 l_num_parent_levels number;
3041 --------------------------------
3042 l_source_type number;
3043 l_source_code number;
3044 l_dim_level_id number;
3045 --------------------------------
3046 l_res number;
3047 l_parent_level_count number;
3048 --------------------------------
3049 Begin
3050   g_rec_count:=g_rec_count+1;
3051   if g_debug then
3052     write_to_log_file_n('In get_filter_stmt_rec '||p_child_level||' '||p_child_level_pk||' '||g_rec_count);
3053   end if;
3054   if g_rec_count>10000 then --some issue, prevent infinite loop
3055     return false;
3056   end if;
3057   l_res:=get_filter_view_params(p_indicator, p_child_level,l_source_type,l_source_code,l_dim_level_id);
3058   if l_res=1 then
3059     --there is a filter at this level. use it
3060     p_count:=p_count+1;
3061     p_from_stmt:=p_from_stmt||bsc_im_utils.get_table_owner(p_child_level)||'.'||p_child_level||' L'||p_level_count||
3062     ','||bsc_im_utils.get_table_owner('BSC_SYS_FILTERS')||'.bsc_sys_filters f'||p_count||',';
3063     p_where_stmt:=p_where_stmt||' and f'||p_count||'.source_code='||l_source_code||
3064     ' and f'||p_count||'.source_type='||l_source_type||
3065     ' and f'||p_count||'.dim_level_id='||l_dim_level_id||
3066     ' and f'||p_count||'.dim_level_value=L'||p_level_count||'.code ';
3067     --bug 3404374
3068     --' and '||p_child_level||'.language='''||BSC_IM_UTILS.get_lang||'''';
3069   else
3070     if get_parent_level(p_child_level,l_level_fk,l_parent_level,l_num_parent_levels)=false then
3071       return false;
3072     end if;
3073     if l_num_parent_levels>0 then
3074       for i in 1..l_num_parent_levels loop
3075         l_res:=get_filter_view_params(p_indicator, l_parent_level(i),l_source_type,l_source_code,l_dim_level_id);
3076         if l_source_type is not null and
3077            bsc_im_utils.get_index(p_pk_cols, p_pk_cols.count, l_parent_level(i)) <1 then
3078           --there is a filter view on this level
3079           l_parent_level_count:=p_level_count+1;
3080           p_from_stmt:=p_from_stmt||bsc_im_utils.get_table_owner(p_child_level)||'.'||p_child_level||
3081           ' L'||p_level_count||',';
3082           p_where_stmt:=p_where_stmt||' and L'||p_level_count||'.'||l_level_fk(i)||'=L'||l_parent_level_count||
3083           '.code';
3084           --3613094
3085           if bsc_im_utils.is_column_in_object(l_parent_level(i),'LANGUAGE') then
3089           if get_filter_stmt_rec(p_indicator, l_parent_level(i),l_level_fk(i),p_count,p_level_count,
3086             p_where_stmt:=p_where_stmt||' and L'||l_parent_level_count||'.language='''||BSC_IM_UTILS.get_lang||'''';
3087           end if;
3088           p_level_count:=p_level_count+1;
3090             p_from_stmt,p_where_stmt, p_pk_cols)=false then
3091             return false;
3092           end if;
3093         end if;
3094       end loop;
3095     end if;
3096   end if;
3097   return true;
3098 Exception when others then
3099   BSC_IM_UTILS.g_status_message:=sqlerrm;
3100   g_status_message:=sqlerrm;
3101   write_to_log_file_n('Exception in get_filter_stmt_rec '||sqlerrm);
3102   return false;
3103 End;
3104 
3105 function get_filter_view_params(
3106 p_indicator number,
3107 p_level varchar2,
3108 p_source_type out nocopy number,
3109 p_source_code out nocopy number,
3110 p_dim_level_id out nocopy number
3111 ) return number is
3112 ----
3113 cursor c3(p_ind number, p_table_name varchar2) is
3114 select source_type,source_code,dim_level_id from bsc_sys_filters_views where
3115 level_table_name=p_table_name
3116 and source_code in (select tab_id from bsc_tab_indicators where indicator=p_ind);
3117 ----
3118 cursor c4(p_type number,p_code number,p_id number) is
3119 select 1 from bsc_sys_filters where source_type=p_type and source_code=p_code
3120 and dim_level_id=p_id and rownum=1;
3121 ----
3122 l_res number;
3123 l_stmt varchar2(8000);
3124 --------------------------------
3125 Begin
3126   if g_debug then
3127     l_stmt:='select source_type,source_code,dim_level_id from bsc_sys_filters_views where '||
3128     'level_table_name=:1 and source_code in (select tab_id from bsc_tab_indicators where indicator=:2';
3129     write_to_log_file_n(l_stmt||' '||p_level||' '||p_indicator);
3130   end if;
3131   open c3(p_indicator, p_level);
3132   fetch c3 into p_source_type,p_source_code,p_dim_level_id;
3133   close c3;
3134   if g_debug then
3135     write_to_log_file(p_source_type||' '||p_source_code||' '||p_dim_level_id);
3136   end if;
3137   if g_debug then
3138     l_stmt:='select 1 from bsc_sys_filters where source_type=:1 and source_code=:2 '||
3139     'and dim_level_id=:3 and rownum=1';
3140     write_to_log_file_n(l_stmt||' '||p_source_type||' '||p_source_code||' '||p_dim_level_id);
3141   end if;
3142   l_res:=null;
3143   open c4(p_source_type,p_source_code,p_dim_level_id);
3144   fetch c4 into l_res;
3145   close c4;
3146   if g_debug then
3147     write_to_log_file('l_res='||l_res);
3148   end if;
3149   return l_res;
3150 Exception when others then
3151   BSC_IM_UTILS.g_status_message:=sqlerrm;
3152   g_status_message:=sqlerrm;
3153   write_to_log_file_n('Exception in get_filter_view_params '||sqlerrm);
3154   return -1;
3155 End;
3156 
3157 function get_parent_level(
3158 p_child_level varchar2,
3159 p_level_fk out nocopy BSC_IM_UTILS.varchar_tabletype,
3160 p_parent_level out nocopy BSC_IM_UTILS.varchar_tabletype,
3161 p_num_parent_levels out nocopy number
3162 ) return boolean is
3163 -------------
3164 cursor c1(p_level varchar2) is
3165 select
3166 par_lvl.level_table_name,
3167 rel.relation_col
3168 from
3169 bsc_sys_dim_levels_b lvl,
3170 bsc_sys_dim_levels_b par_lvl,
3171 bsc_sys_dim_level_rels rel
3172 where
3173 lvl.level_table_name=p_level
3174 and lvl.dim_level_id=rel.dim_level_id
3175 and rel.relation_type=1
3176 and par_lvl.dim_level_id=rel.parent_dim_level_id;
3177 -------------
3178 Begin
3179   if g_debug then
3180     write_to_log_file_n('select par_lvl.level_table_name,rel.relation_col from bsc_sys_dim_levels_b lvl,'||
3181     'bsc_sys_dim_levels_b par_lvl,bsc_sys_dim_level_rels rel where lvl.level_table_name='''||p_child_level||''''||
3182     'and lvl.dim_level_id=rel.dim_level_id and rel.relation_type=1 and par_lvl.dim_level_id=rel.parent_dim_level_id');
3183   end if;
3184   p_num_parent_levels:=1;
3185   open c1(p_child_level);
3186   loop
3187     fetch c1 into p_parent_level(p_num_parent_levels),p_level_fk(p_num_parent_levels);
3188     exit when c1%notfound;
3189     p_num_parent_levels:=p_num_parent_levels+1;
3190   end loop;
3191   p_num_parent_levels:=p_num_parent_levels-1;
3192   close c1;
3193   if g_debug then
3194     for i in 1..p_num_parent_levels loop
3195       write_to_log_file(p_parent_level(i)||' '||p_level_fk(i));
3196     end loop;
3197   end if;
3198   return true;
3199 Exception when others then
3200   BSC_IM_UTILS.g_status_message:=sqlerrm;
3201   g_status_message:=sqlerrm;
3202   write_to_log_file_n('Exception in get_parent_level '||sqlerrm);
3203   return false;
3204 End;
3205 
3206 function is_recursive_dim(p_child_level in varchar2, p_result OUT NOCOPY boolean) return boolean is
3207 l_level_fk BSC_IM_UTILS.varchar_tabletype;
3208 l_parent_level BSC_IM_UTILS.varchar_tabletype;
3209 l_num_parent_levels number;
3210 
3211 begin
3212   p_result := false;
3213   if get_parent_level(p_child_level,l_level_fk,l_parent_level,l_num_parent_levels)=false then
3214      return false;
3215   end if;
3216   if (l_parent_level.count=0) then
3217     p_result := false;
3218   elsif (p_child_level=l_parent_level(l_parent_level.first)) then
3219     p_result := true;
3220   end if;
3221   return true;
3222 end;
3223 
3224 function create_denorm_table(p_dim_level varchar2, p_level_pk varchar2) return varchar2 is
3228 l_data_type varchar2(100);
3225 l_denorm_table varchar2(100);
3226 l_stmt varchar2(1000);
3227 l_owner varchar2(100);
3229 l_reverse varchar2(1000);
3230 
3231 CURSOR cDataType(p_owner in varchar2) is
3232 select decode(data_length, null, data_type, data_type||'('||data_length||')') data_type
3233 from all_tab_columns
3234 where owner=p_owner
3235 and table_name=p_dim_level
3236 and column_name = p_level_pk;
3237 
3238 cursor c_short_name is
3239 select short_name from bsc_sys_dim_levels_b
3240 where level_table_name=p_dim_level;
3241 l_short_name varchar2(300);
3242 
3243 begin
3244   open c_short_name;
3245   fetch c_short_name into l_short_name;
3246   close c_short_name;
3247   l_denorm_table := BSC_DBGEN_METADATA_READER.get_denorm_dimension_table(l_short_name);
3248   l_owner := bsc_im_utils.get_table_owner(p_dim_level);
3249 
3250   open cDataType(l_owner);
3251   fetch cDataType into l_data_type;
3252   close cDataType;
3253 
3254   l_stmt := 'create table '||l_denorm_table||'(parent_code '||l_data_type||', code '||l_data_type||', child_level number, parent_level number)';
3255 
3256   begin
3257   BSC_APPS.Do_DDL(l_stmt, AD_DDL.CREATE_TABLE, l_denorm_table);
3258   l_stmt := 'create index '||l_denorm_table||'_N1 on '||l_denorm_table||'(CODE)';
3259 
3260   BSC_APPS.Do_DDL(l_stmt, AD_DDL.CREATE_INDEX, l_denorm_table||'_N1');
3261   exception when others then
3262    if sqlcode = -955 then -- already exists
3263      null;
3264    else
3265      raise;
3266    end if;
3267   end;
3268 
3269   l_stmt := 'create materialized view log on '||g_bsc_owner||'.'||l_denorm_table||' with sequence,rowid(code, parent_code, 	 child_level, parent_level) including new values';
3270   begin
3271   execute immediate l_stmt;
3272   exception when others then
3273     if sqlcode=-12000 then -- snapshot log already exists
3274       null;
3275     else
3276       raise;
3277     end if;
3278   end;
3279   return l_denorm_table;
3280 
3281 Exception when others then
3282   BSC_IM_UTILS.g_status_message:=sqlerrm;
3283   g_status_message:=sqlerrm;
3284   write_to_log_file_n('Exception in create_denorm_table: p_dim_level='||p_dim_level||', pk='||p_level_pk||', stmt='||l_stmt||', error='||sqlerrm);
3285   if (l_data_type is null) then
3286     write_to_log_file('PK Column '||p_level_pk||' specified in bsc_sys_dim_levels_b does not exist in '||p_dim_level);
3287   end if;
3288   raise;
3289 end;
3290 
3291 function get_level_for_pk(
3292 p_level_pk varchar2,
3293 p_level out nocopy varchar2,
3294 p_src_object out nocopy varchar2, --only populated if this is a DBI dimension
3295 p_special_dim out nocopy varchar2,
3296 p_rec_dim out nocopy boolean,
3297 p_rec_dim_key out nocopy varchar2
3298 )return boolean is
3299 l_stmt varchar2(5000);
3300 --------------------------------------------
3301 cursor c1(p_col varchar2) is select level_table_name,short_name from bsc_sys_dim_levels_b where upper(level_pk_col)=upper(p_col);
3302 --------------------------------------------
3303 l_name varchar2(200);
3304 l_dbi_dim_data BSC_UPDATE_DIM.t_dbi_dim_data;
3305 
3306 Begin
3307   if NOT g_level_info_cached then
3308     cache_level_info;
3309     g_level_info_cached := true;
3310   end if;
3311   if g_debug then
3312     write_to_log_file_n('In get_level_for_pk'||' '||get_time);
3313     l_stmt:='select level_table_name,short_name from bsc_sys_dim_levels_b where level_pk_col=:1';
3314     write_to_log_file_n(l_stmt||' '||p_level_pk);
3315   end if;
3316 
3317   if g_level_info.exists(upper(p_level_pk)) then
3318     p_level := g_level_info(upper(p_level_pk)).level_table_name;
3319     l_name := g_level_info(upper(p_level_pk)).short_name;
3320   else
3321     open c1(p_level_pk);
3322     fetch c1 into p_level,l_name;
3323     close c1;
3324   end if;
3325   if g_debug then
3326     write_to_log_file('Result : '||p_level||','||l_name);
3327   end if;
3328   p_special_dim:='none';
3329   p_src_object:=p_level;
3330   p_rec_dim:=false;
3331   p_rec_dim_key:=null;
3332 
3333   --BSC E2E : see if this is a dbi dimension and see if there is table associated with this level
3334 
3335   BSC_UPDATE_DIM.Get_Dbi_Dim_Data(l_name,l_dbi_dim_data);
3336   if  l_dbi_dim_data.table_name is null then
3337     -- Check if this is a non-DBI recursive dimension
3338     if is_recursive_dim(p_level, p_rec_dim)=false then
3339       return false;
3340     end if;
3341     if (p_rec_dim) then
3342       write_to_log_file_n(p_level||' is a recursive BSC Dimension');
3343     else
3344        write_to_log_file_n(p_level||' is not recursive');
3345     end if;
3346     if (p_rec_dim=true) then
3347       p_src_object := create_denorm_table(p_level, p_level_pk);
3348       p_rec_dim_key := 'PARENT_CODE';
3349       p_special_dim := 'recursive';
3350       return true;
3351     end if;
3352   else
3353   --  if l_dbi_dim_data.table_name is not null then
3354     if create_dbi_dim_tables=false then --try to create the dim tables(e2e) tables the first time they are encountered
3355       return false;
3356     end if;
3357     if g_debug then
3358       write_to_log_file_n('Finished create_dbi_dim_tables');
3359       write_to_log_file('short name='||l_dbi_dim_data.short_name||', table name='||
3360       l_dbi_dim_data.table_name);
3364     if l_dbi_dim_data.top_n_levels_in_mv>0 then --this is a rec dim
3361       write_to_log_file('code='||l_dbi_dim_data.code_col||', from '||l_dbi_dim_data.from_clause||', '||
3362       'where '||l_dbi_dim_data.where_clause);
3363     end if;
3365       --ENI_ITEM_VBH_CAT ENI_ITEM_ITM_CAT HRI_PER_USRDR_H PJI_ORGANIZATIONS
3366       if g_debug then
3367         write_to_log_file_n('Recursive dim');
3368       end if;
3369       p_src_object:='(Select '||l_dbi_dim_data.child_col||' code,'||l_dbi_dim_data.parent_col||' parent_code from '||
3370       l_dbi_dim_data.denorm_table||' where ('||l_dbi_dim_data.parent_level_col||'<='||
3371       l_dbi_dim_data.top_n_levels_in_mv||') ';
3372       --3948748
3373       p_src_object:=p_src_object||' or ('||l_dbi_dim_data.parent_col||'='||l_dbi_dim_data.child_col||' and '||
3374       l_dbi_dim_data.parent_level_col||'>'||l_dbi_dim_data.top_n_levels_in_mv||')';
3375       p_src_object:=p_src_object||')';
3376       p_rec_dim:=true;
3377       p_rec_dim_key:='parent_code';
3378       p_special_dim:='inline';
3379     elsif l_dbi_dim_data.short_name='ENI_ITEM_VBH_CAT' then
3380       p_src_object:='(Select '||l_dbi_dim_data.code_col||' CODE from '||l_dbi_dim_data.from_clause||
3381       l_dbi_dim_data.where_clause||') ';
3382       p_special_dim:='inline';
3383     elsif l_dbi_dim_data.short_name='ENI_ITEM_ORG' then
3384       p_src_object:='(Select '||l_dbi_dim_data.code_col||' CODE from '||l_dbi_dim_data.from_clause||
3385       l_dbi_dim_data.where_clause||') ';
3386       p_special_dim:='inline';
3387     else
3388       p_src_object:=l_dbi_dim_data.table_name;
3389     end if;
3390   end if;
3391   if g_debug then
3392     write_to_log_file_n('src object='||p_src_object||' type='||p_special_dim);
3393     write_to_log_file_n('Returning from get_level_for_pk'||' '||get_time);
3394   end if;
3395   ---
3396   return true;
3397 Exception when others then
3398   BSC_IM_UTILS.g_status_message:=sqlerrm;
3399   g_status_message:=sqlerrm;
3400   write_to_log_file_n('Exception in get_level_for_pk '||sqlerrm);
3401   return false;
3402 End;
3403 
3404 function get_table_cols(
3405 p_table_name varchar2,
3406 p_col_table in out nocopy BSC_IM_UTILS.varchar_tabletype,
3407 p_cols in out nocopy BSC_IM_UTILS.varchar_tabletype,
3408 p_col_type in out nocopy BSC_IM_UTILS.varchar_tabletype,
3409 p_source_column in out nocopy BSC_IM_UTILS.varchar_tabletype,
3410 p_source_formula in out nocopy BSC_IM_UTILS.varchar_tabletype,
3411 p_number_cols in out nocopy number
3412 ) return boolean is
3413 l_stmt varchar2(5000);
3414 ---------------------------------------------
3415 cursor c1(p_table varchar2) is
3416 select table_name,upper(column_name),column_type,source_formula,source_column
3417 from bsc_db_tables_cols where table_name=p_table
3418 order by column_name;
3419 ---------------------------------------------
3420 l_table_name varchar2(100);
3421 Begin
3422   if p_table_name like 'BSC_B_%PRJ%' then
3423     l_table_name := get_b_table_name_for_prj(p_table_name);
3424   else
3425     l_table_name := p_table_name;
3426   end if;
3427   if g_debug then
3428     l_stmt:='select table_name,upper(column_name),column_type,source_formula,upper(source_column) '||
3429     'from bsc_db_tables_cols where table_name=:1';
3430     write_to_log_file_n(l_stmt||' '||l_table_name);
3431   end if;
3432   if p_number_cols is null then
3433     p_number_cols:=1;
3434   else
3435     p_number_cols:=p_number_cols+1;
3436   end if;
3437   open c1(l_table_name);
3438   loop
3439     fetch c1 into p_col_table(p_number_cols),p_cols(p_number_cols),p_col_type(p_number_cols),
3440     p_source_formula(p_number_cols),p_source_column(p_number_cols);
3441     exit when c1%notfound;
3442     p_number_cols:=p_number_cols+1;
3443   end loop;
3444   close c1;
3445   p_number_cols:=p_number_cols-1;
3446   if g_debug then
3447     write_to_log_file_n('Results');
3448     for i in 1..p_number_cols loop
3449       write_to_log_file(p_col_table(i)||' '||p_cols(i)||' '||p_col_type(i)||' '||p_source_formula(i)||' '||
3450       p_source_column(i));
3451     end loop;
3452   end if;
3453   return true;
3454 Exception when others then
3455   BSC_IM_UTILS.g_status_message:=sqlerrm;
3456   g_status_message:=sqlerrm;
3457   write_to_log_file_n('Exception in get_table_cols '||sqlerrm);
3458   return false;
3459 End;
3460 
3461 function get_table_periodicity(p_table_name varchar2) return number is
3462 l_stmt varchar2(5000);
3463 l_id number;
3464 ---------------------------------------
3465 cursor c1 (p_table varchar2)
3466 is select periodicity_id from bsc_db_tables where table_name=p_table;
3467 ---------------------------------------
3468 l_table_name varchar2(100);
3469 Begin
3470   if (p_table_name like 'BSC_B%PRJ%') then
3471     l_table_name := get_b_table_name_for_prj(p_table_name);
3472   else
3473     l_table_name := p_table_name;
3474   end if;
3475   if g_debug then
3476     l_stmt:='select periodicity_id from bsc_db_tables where table_name=:1';
3477     write_to_log_file_n(l_stmt||' '||l_table_name);
3478   end if;
3479   open c1(l_table_name);
3480   fetch c1 into l_id;
3481   close c1;
3482   if g_debug then
3483     write_to_log_file('Result: '||l_id);
3484   end if;
3485   return l_id;
3486 Exception when others then
3487   BSC_IM_UTILS.g_status_message:=sqlerrm;
3488   g_status_message:=sqlerrm;
3489   write_to_log_file_n('Exception in get_table_periodicity '||sqlerrm);
3493 function init_all return boolean is
3490   return -1;
3491 End;
3492 
3494 Begin
3495   g_status:=true;
3496   if BSC_IM_UTILS.get_db_user('APPS',g_apps_owner)=false then
3497     return null;
3498   end if;
3499   if g_apps_owner is null then
3500     return null;
3501   end if;
3502   g_prod_owner:='BSC';
3503   if BSC_IM_UTILS.get_db_user(g_prod_owner,g_bsc_owner)=false then
3504     return null;
3505   end if;
3506   if g_debug then
3507     write_to_log_file_n('Apps DB User is '||g_apps_owner||', '||g_prod_owner||' user is '||g_bsc_owner||'  '||get_time);
3508   end if;
3509   return true;
3510 Exception when others then
3511   BSC_IM_UTILS.g_status_message:=sqlerrm;
3512   g_status_message:=sqlerrm;
3513   write_to_log_file_n('Exception in init_all '||sqlerrm);
3514   return false;
3515 End;
3516 
3517 procedure write_to_log_file(p_message varchar2) is
3518 Begin
3519   BSC_IM_UTILS.write_to_log_file(p_message);
3520 Exception when others then
3521   BSC_IM_UTILS.g_status_message:=sqlerrm;
3522   null;
3523 End;
3524 
3525 procedure write_to_log_file_n(p_message varchar2) is
3526 begin
3527   write_to_log_file('  ');
3528   write_to_log_file(p_message);
3529 Exception when others then
3530   BSC_IM_UTILS.g_status_message:=sqlerrm;
3531   null;
3532 end;
3533 
3534 procedure write_to_debug_n(p_message varchar2) is
3535 begin
3536   if g_debug then
3537     write_to_log_file_n(p_message);
3538   end if;
3539 Exception when others then
3540   BSC_IM_UTILS.g_status_message:=sqlerrm;
3541   null;
3542 end;
3543 
3544 procedure write_to_debug(p_message varchar2) is
3545 begin
3546   if g_debug then
3547     write_to_log_file(p_message);
3548   end if;
3549 Exception when others then
3550   BSC_IM_UTILS.g_status_message:=sqlerrm;
3551   null;
3552 end;
3553 
3554 function get_time return varchar2 is
3555 begin
3556   return BSC_IM_UTILS.get_time;
3557 Exception when others then
3558   BSC_IM_UTILS.g_status_message:=sqlerrm;
3559   null;
3560 End;
3561 
3562 procedure set_globals(p_debug boolean) is
3563 Begin
3564   g_debug:=p_debug;
3565   BSC_IM_UTILS.set_globals(g_debug);
3566   BSC_IM_INT_MD.set_globals(g_debug);
3567 Exception when others then
3568   BSC_IM_UTILS.g_status_message:=sqlerrm;
3569   null;
3570 End;
3571 
3572 function get_table_fks(
3573 p_s_tables BSC_IM_UTILS.varchar_tabletype,
3574 p_number_s_tables number,
3575 p_fk out nocopy BSC_IM_UTILS.varchar_tabletype,
3576 p_number_fk out nocopy number
3577 ) return boolean is
3578 l_fk BSC_IM_UTILS.varchar_tabletype;
3579 l_number_fk number;
3580 Begin
3581   p_number_fk:=0;
3582   for i in 1..p_number_s_tables loop
3583     if get_table_fks(p_s_tables(i),l_fk,l_number_fk)=false then
3584       return false;
3585     end if;
3586     for j in 1..l_number_fk loop
3587       if BSC_IM_UTILS.in_array(p_fk,p_number_fk,l_fk(j))=false then
3588         p_number_fk:=p_number_fk+1;
3589         p_fk(p_number_fk):=l_fk(j);
3590       end if;
3591     end loop;
3592   end loop;
3593   if g_debug then
3594     write_to_log_file_n('Results');
3595     for i in 1..p_number_fk loop
3596       write_to_log_file(p_fk(i));
3597     end loop;
3598   end if;
3599   return true;
3600 Exception when others then
3601   BSC_IM_UTILS.g_status_message:=sqlerrm;
3602   g_status_message:=sqlerrm;
3603   write_to_log_file_n('Exception in get_table_fks '||sqlerrm);
3604   return false;
3605 End;
3606 
3607 function get_table_fks(
3608 p_table varchar2,
3609 p_fk out nocopy BSC_IM_UTILS.varchar_tabletype,
3610 p_number_fk out nocopy number
3611 ) return boolean is
3612 l_stmt varchar2(5000);
3613 --------------------------------------------------
3614 --for S tables
3615 cursor c0 (p_table varchar2)
3616 is select distinct bsc_db_tables_cols.column_name,bsc_kpi_dim_levels_b.dim_level_index from
3617 bsc_kpi_data_tables,
3618 bsc_db_tables_cols,
3619 bsc_kpi_dim_levels_b
3620 where
3621 bsc_kpi_data_tables.table_name=bsc_db_tables_cols.table_name
3622 and bsc_db_tables_cols.table_name =p_table
3623 and bsc_db_tables_cols.column_type='P'
3624 and bsc_kpi_data_tables.indicator=bsc_kpi_dim_levels_b.indicator
3625 and bsc_db_tables_cols.column_name=bsc_kpi_dim_levels_b.level_pk_col
3626 and bsc_kpi_data_tables.dim_set_id=bsc_kpi_dim_levels_b.dim_set_id
3627 order by bsc_kpi_dim_levels_b.dim_level_index;
3628 --
3629 --for the B tables
3630 cursor c1 (p_table varchar2,p_owner varchar2)
3631 is select distinct bsc_db_tables_cols.column_name,all_tab_columns.column_id
3632 from
3633 bsc_db_tables_cols ,
3634 all_tab_columns
3635 where bsc_db_tables_cols.table_name =p_table
3636 and bsc_db_tables_cols.column_type='P'
3637 and all_tab_columns.table_name(+)=p_table
3638 and all_tab_columns.column_name(+)=bsc_db_tables_cols.column_name
3639 and all_tab_columns.owner(+)=p_owner
3640 order by all_tab_columns.column_id;
3641 ----
3642 l_owner varchar2(200);
3643 l_order BSC_IM_UTILS.number_tabletype;
3644 --------------------------------------------------
3645 l_table_name varchar2(100);
3646 Begin
3647   p_number_fk:=1;
3648   if g_debug then
3649     write_to_log_file_n('distinct bsc_db_tables_cols.column_name,bsc_kpi_dim_levels_b.dim_level_index fr...');
3650   end if;
3654     l_table_name := p_table;
3651   if (p_table like 'BSC_B_%PRJ%') then
3652     l_table_name := get_b_table_name_for_prj(p_table);
3653   else
3655   end if;
3656   open c0(l_table_name);
3657   loop
3658     fetch c0 into p_fk(p_number_fk),l_order(p_number_fk);
3659     exit when c0%notfound;
3660     p_number_fk:=p_number_fk+1;
3661   end loop;
3662   -- Fix bug#3899842 : close cursor
3663   close c0;
3664   p_number_fk:=p_number_fk-1;
3665   --
3666   if p_number_fk=0 then
3667     --either there are no fk or this could be a table without entries in bsc_kpi_data_tables...like B tables
3668     if g_debug then
3669       write_to_log_file_n('is select distinct bsc_db_tables_cols.column_name,all_tab_columns.column_id...');
3670     end if;
3671     l_owner:=bsc_im_utils.get_table_owner(l_table_name);
3672     p_number_fk:=p_number_fk+1;
3673     open c1(l_table_name,l_owner);
3674     loop
3675       fetch c1 into p_fk(p_number_fk),l_order(p_number_fk);
3676       exit when c1%notfound;
3677       p_number_fk:=p_number_fk+1;
3678     end loop;
3679     close c1;
3680     p_number_fk:=p_number_fk-1;
3681   end if;
3682   for i in 1..BSC_IM_UTILS.g_number_global_dimension loop
3683     if BSC_IM_UTILS.g_global_dimension(i)<>'PERIOD' and BSC_IM_UTILS.g_global_dimension(i)<>'TYPE' then
3684       p_number_fk:=p_number_fk+1;
3685       p_fk(p_number_fk):=BSC_IM_UTILS.g_global_dimension(i);
3686     end if;
3687   end loop;
3688   for i in 1..BSC_IM_UTILS.g_number_global_dimension loop
3689     if BSC_IM_UTILS.g_global_dimension(i)='PERIOD' or BSC_IM_UTILS.g_global_dimension(i)='TYPE' then
3690       p_number_fk:=p_number_fk+1;
3691       p_fk(p_number_fk):=BSC_IM_UTILS.g_global_dimension(i);
3692     end if;
3693   end loop;
3694   if g_debug then
3695     write_to_log_file_n('Results');
3696     for i in 1..p_number_fk loop
3697       write_to_log_file(p_fk(i));
3698     end loop;
3699   end if;
3700   return true;
3701 Exception when others then
3702   BSC_IM_UTILS.g_status_message:=sqlerrm;
3703   g_status_message:=sqlerrm;
3704   write_to_log_file_n('Exception in get_table_fks '||sqlerrm);
3705   return false;
3706 End;
3707 
3708 function get_table_measures(
3709 p_s_tables BSC_IM_UTILS.varchar_tabletype,
3710 p_number_s_tables number,
3711 p_measures out nocopy BSC_IM_UTILS.varchar_tabletype,
3712 p_number_measures out nocopy number
3713 ) return boolean is
3714 l_measures BSC_IM_UTILS.varchar_tabletype;
3715 l_number_measures number;
3716 Begin
3717   p_number_measures:=0;
3718   for i in 1..p_number_s_tables loop
3719     l_number_measures:=0;
3720     if get_table_measures(p_s_tables(i),l_measures,l_number_measures)=false then
3721       return false;
3722     end if;
3723     for j in 1..l_number_measures loop
3724       if BSC_IM_UTILS.in_array(p_measures,p_number_measures,l_measures(j))=false then
3725         p_number_measures:=p_number_measures+1;
3726         p_measures(p_number_measures):=l_measures(j);
3727       end if;
3728     end loop;
3729   end loop;
3730   if g_debug then
3731     write_to_log_file_n('Results');
3732     for i in 1..p_number_measures loop
3733       write_to_log_file(p_measures(i));
3734     end loop;
3735   end if;
3736   return true;
3737 Exception when others then
3738   BSC_IM_UTILS.g_status_message:=sqlerrm;
3739   g_status_message:=sqlerrm;
3740   write_to_log_file_n('Exception in get_s_table_measures '||sqlerrm);
3741   return false;
3742 End;
3743 
3744 function get_table_measures(
3745 p_table varchar2,
3746 p_measures out nocopy BSC_IM_UTILS.varchar_tabletype,
3747 p_number_measures out nocopy number
3748 ) return boolean is
3749 l_stmt varchar2(5000);
3750 -----------------------------------------
3751 cursor c1 (p_table_name varchar2)
3752 is select distinct column_name from bsc_db_tables_cols where table_name =p_table_name and
3753 column_type='A' order by column_name;
3754 -----------------------------------------
3755 l_table_name varchar2(100);
3756 Begin
3757   if (p_table like 'BSC_B_%PRJ%') then
3758     l_table_name := get_b_table_name_for_prj(p_table);
3759   else
3760     l_table_name := p_table;
3761   end if;
3762   if g_debug then
3763     l_stmt:='select distinct column_name from bsc_db_tables_cols where table_name =:1 and '||
3764     'column_type=''A''';
3765     write_to_log_file_n(l_stmt||' '||p_table);
3766   end if;
3767   p_number_measures:=1;
3768   open c1(l_table_name);
3769   loop
3770     fetch c1 into p_measures(p_number_measures);
3771     exit when c1%notfound;
3772     p_number_measures:=p_number_measures+1;
3773   end loop;
3774   close c1;
3775   p_number_measures:=p_number_measures-1;
3776   if g_debug then
3777     write_to_log_file_n('Results');
3778     for i in 1..p_number_measures loop
3779       write_to_log_file(p_measures(i));
3780     end loop;
3781   end if;
3782   return true;
3783 Exception when others then
3784   BSC_IM_UTILS.g_status_message:=sqlerrm;
3785   g_status_message:=sqlerrm;
3786   write_to_log_file_n('Exception in get_s_table_measures '||sqlerrm);
3787   return false;
3788 End;
3789 
3790 function get_kpi_periodicity(
3791 p_indicator_id number,
3792 p_periodicity out nocopy BSC_IM_UTILS.number_tabletype,
3793 p_number_periodicity out nocopy number
3794 )return boolean is
3795 l_stmt varchar2(5000);
3796 -------------------------------------------
3800 Begin
3797 cursor c1 (p_indicator number)
3798 is select periodicity_id from bsc_kpi_periodicities where  indicator=p_indicator;
3799 -------------------------------------------
3801   if g_debug then
3802     l_stmt:='select periodicity_id from bsc_kpi_periodicities where  indicator=:1';
3803     write_to_log_file_n(l_stmt||' '||p_indicator_id||'    '||get_time);
3804   end if;
3805   p_number_periodicity:=1;
3806   open c1(p_indicator_id);
3807   loop
3808     fetch c1 into p_periodicity(p_number_periodicity);
3809     exit when c1%notfound;
3810     p_number_periodicity:=p_number_periodicity+1;
3811   end loop;
3812   close c1;
3813   p_number_periodicity:=p_number_periodicity-1;
3814   if g_debug then
3815     write_to_debug_n('Results');
3816     for i in 1..p_number_periodicity loop
3817       write_to_debug(p_periodicity(i));
3818     end loop;
3819   end if;
3820   return true;
3821 Exception when others then
3822   BSC_IM_UTILS.g_status_message:=sqlerrm;
3823   g_status_message:=sqlerrm;
3824   write_to_log_file_n('Exception in get_kpi_periodicity '||sqlerrm);
3825   return false;
3826 End;
3827 
3828 function get_s_sb_tables(
3829 p_indicator_id number,
3830 p_s_tables out nocopy BSC_IM_UTILS.varchar_tabletype,
3831 p_s_periodicity out nocopy BSC_IM_UTILS.number_tabletype,
3832 p_number_s_tables out nocopy number
3833 )return boolean is
3834 l_stmt varchar2(5000);
3835 l_mv_object_string varchar2(5000);
3836 l_mv_object BSC_IM_UTILS.varchar_tabletype;
3837 l_number_mv_object number;
3838 ---------------------------------------------------
3839 
3840 -- overcome issue with large data in bsc_kpi_data_tables, use db_tables_rels instead
3841 --cursor c1 (p_indicator number)
3842 --is select distinct upper(table_name),periodicity_id from bsc_kpi_data_tables where indicator=p_indicator
3843 --and table_name is not null;
3844 
3845 cursor c1 (p_indicator number)is
3846 select distinct upper(table_name), substr(table_name, instr(table_name, '_', -1, 1)+1) periodicity_id  from bsc_db_tables_rels
3847 where instr(table_name, 'BSC_S_'||p_indicator||'_') =1 or instr(table_name, 'BSC_SB_'||p_indicator||'_') =1;
3848 
3849 /*
3850 5009697
3851 c2 will add duplicate data to c1
3852 cursor c2 (p_indicator number)
3853 is select distinct rel.source_table_name, tab.periodicity_id
3854 from bsc_db_tables_rels rel, bsc_db_tables tab
3855 where rel.table_name in
3856 (select distinct upper(table_name) from bsc_kpi_data_tables where indicator=p_indicator)
3857 and rel.source_table_name like 'BSC_SB_%'
3858 and rel.source_table_name=tab.table_name ;
3859 */
3860 ---------------------------------------------------
3861 Begin
3862   if g_debug then
3863     l_stmt:='select distinct upper(table_name),periodicity_id from bsc_kpi_data_tables where indicator=:1 '||
3864     'and table_name is not null';
3865     write_to_log_file_n(l_stmt||' '||p_indicator_id);
3866   end if;
3867   p_number_s_tables:=1;
3868   open c1(p_indicator_id);
3869   loop
3870     fetch c1 into p_s_tables(p_number_s_tables),p_s_periodicity(p_number_s_tables);
3871     exit when c1%notfound;
3872     p_number_s_tables:=p_number_s_tables+1;
3873   end loop;
3874   close c1;
3875   /*
3876   5009697
3877   if g_debug then
3878     l_stmt:='select distinct rel.source_table_name, tab.periodicity_id '||
3879     'from bsc_db_tables_rels rel, bsc_db_tables tab '||
3880     'where rel.table_name in  '||
3881     '(select distinct upper(table_name) from bsc_kpi_data_tables where indicator=:1) '||
3882     'and rel.source_table_name like ''BSC_SB_%'' '||
3883     'and rel.source_table_name=tab.table_name ';
3884     write_to_log_file_n(l_stmt||' '||p_indicator_id);
3885   end if;
3886 
3887   open c2(p_indicator_id);
3888   loop
3889     fetch c2 into p_s_tables(p_number_s_tables),p_s_periodicity(p_number_s_tables);
3890     exit when c2%notfound;
3891     p_number_s_tables:=p_number_s_tables+1;
3892   end loop;
3893   close c2;
3894   */
3895   p_number_s_tables:=p_number_s_tables-1;
3896   if g_debug then
3897     write_to_debug_n('S tables and SB from bsc_kpi_data_tables are ');
3898     for i in 1..p_number_s_tables loop
3899       write_to_debug(p_s_tables(i)||' '||p_s_periodicity(i));
3900     end loop;
3901   end if;
3902   return true;
3903 Exception when others then
3904   BSC_IM_UTILS.g_status_message:=sqlerrm;
3905   g_status_message:=sqlerrm;
3906   write_to_log_file_n('Exception in get_s_sb_tables '||sqlerrm);
3907   return false;
3908 End;
3909 
3910 function get_db_calculation(
3911 p_indicator number,
3912 p_s_table varchar2,
3913 p_type number,
3914 p_calculation_table in out nocopy BSC_IM_UTILS.varchar_tabletype,
3915 p_calculation_type in out nocopy BSC_IM_UTILS.varchar_tabletype,
3916 p_parameter1 in out nocopy BSC_IM_UTILS.varchar_tabletype,
3917 p_parameter2 in out nocopy BSC_IM_UTILS.varchar_tabletype,
3918 p_parameter3 in out nocopy BSC_IM_UTILS.varchar_tabletype,
3919 p_parameter4 in out nocopy BSC_IM_UTILS.varchar_tabletype,
3920 p_parameter5 in out nocopy BSC_IM_UTILS.varchar_tabletype,
3921 p_number_parameters in out nocopy number
3922 ) return boolean is
3923 l_stmt varchar2(4000);
3924 l_agg_func BSC_IM_UTILS.varchar_tabletype;
3925 ----------------------------------------------------------
3926 cursor c1 (p_table varchar2)
3927 is select table_name,calculation_type,parameter1,'''0''',upper(parameter3),parameter4,parameter5
3928 from bsc_db_calculations where table_name=p_table
3929 order by upper(parameter3),parameter1;
3933 from bsc_db_calculations where table_name=p_table
3930 
3931 cursor c2 (p_table varchar2,p_type number)
3932 is select table_name,calculation_type,parameter1,'''0''',upper(parameter3),parameter4,parameter5
3934 and calculation_type=p_type
3935 order by upper(parameter3),parameter1;
3936 
3937 --  bug 3866554, dont assume that the datatype in the above sql is always VARCHAR2
3938 cursor c3 (p_column varchar2)
3939 is select cols.data_type
3940 from all_tab_columns cols,
3941 bsc_kpi_dim_levels_b dim
3942 where cols.table_name = dim.level_table_name
3943 and cols.column_name = 'CODE'
3944 and dim.indicator = p_indicator
3945 and dim.level_pk_col = p_column
3946 and cols.owner = bsc_im_utils.get_table_owner(dim.level_table_name) ;
3947 
3948 ----------------------------------------------------------
3949 l_calculation_table BSC_IM_UTILS.varchar_tabletype;
3950 l_calculation_type BSC_IM_UTILS.varchar_tabletype;
3951 l_parameter1 BSC_IM_UTILS.varchar_tabletype;
3952 l_parameter2 BSC_IM_UTILS.varchar_tabletype;
3953 l_parameter3 BSC_IM_UTILS.varchar_tabletype;
3954 l_parameter4 BSC_IM_UTILS.varchar_tabletype;
3955 l_parameter5 BSC_IM_UTILS.varchar_tabletype;
3956 l_number_parameters number;
3957 l_fk_datatype varchar2(100);
3958 TYPE CurTyp IS REF CURSOR;
3959 cv   CurTyp;
3960 ----------------------------------------------------------
3961 Begin
3962   if g_debug then
3963     write_to_log_file_n('IN get_db_calculation'||' '||get_time);
3964     l_stmt:='select table_name,calculation_type,parameter1,0,upper(parameter3),parameter4,parameter5 '||
3965     'from bsc_db_calculations where table_name=:1 ';
3966     if p_type is not null then
3967       l_stmt:=l_stmt||' and calculation_type='||p_type;
3968     end if;
3969     l_stmt:=l_stmt||' order by upper(parameter3),parameter1';
3970     write_to_log_file_n(l_stmt||' '||p_s_table);
3971   end if;
3972   --
3973   if p_number_parameters is null then
3974     p_number_parameters:=1;
3975   else
3976     p_number_parameters:=p_number_parameters+1;
3977   end if;
3978   --
3979  l_stmt:=' select cols.data_type from '||bsc_olap_main.g_col_type_table_name||' cols,
3980             bsc_kpi_dim_levels_b dim where dim.indicator = :1
3981             and dim.level_pk_col = :2 and cols.level_table_name = dim.level_table_name';
3982 
3983   l_number_parameters:=1;
3984   if p_type is not null then
3985     open c2(p_s_table,p_type);
3986     fetch c2 bulk collect into l_calculation_table, l_calculation_type, l_parameter1, l_parameter2,
3987                                l_parameter3, l_parameter4, l_parameter5;
3988     close c2;
3989     l_number_parameters := l_number_parameters + l_calculation_table.count-1;
3990     for i in 1..l_number_parameters loop
3991       -- bug 3866554, dont assume that the datatype for the FK is always VARCHAR2,
3992       -- instead get it from the level
3993       -- table name's CODE column
3994       --Bug 3878968 use the tmp table to get the datatype
3995       open cv for l_stmt using p_indicator,l_parameter1(i);
3996       fetch cv into l_fk_datatype ;
3997       close cv;
3998       if (l_fk_datatype = 'NUMBER') then
3999   	l_parameter2(i) := '0'; --rkumar: bugfix5458512
4000       end if;
4001       --l_number_parameters:=l_number_parameters+1;
4002     end loop;
4003   else
4004     open c1(p_s_table);
4005     loop
4006       fetch c1 into l_calculation_table(l_number_parameters),l_calculation_type(l_number_parameters),
4007       l_parameter1(l_number_parameters),l_parameter2(l_number_parameters),l_parameter3(l_number_parameters),
4008       l_parameter4(l_number_parameters),l_parameter5(l_number_parameters);
4009       exit when c1%notfound;
4010       --  bug 3866554, dont assume that the datatype for the FK is always VARCHAR2, instead get it from the level
4011       -- table name's CODE column
4012       open c3(l_parameter1(l_number_parameters));
4013       fetch c3 into l_fk_datatype;
4014       close c3;
4015       if (l_fk_datatype = 'NUMBER') then
4016         l_parameter2(l_number_parameters) := '0';
4017       end if;
4018       l_number_parameters:=l_number_parameters+1;
4019     end loop;
4020     close c1;
4021     l_number_parameters:=l_number_parameters-1;
4022   end if;
4023   if g_debug then
4024     write_to_log_file_n('Cursor Results');
4025     for i in 1..l_number_parameters loop
4026       write_to_log_file(l_calculation_table(i)||' '||l_calculation_type(i)||' '||l_parameter1(i)||' '||
4027       l_parameter2(i)||' '||l_parameter3(i)||' '||l_parameter4(i)||' '||l_parameter5(i));
4028     end loop;
4029   end if;
4030   -----------------------
4031   --for E2E, for recursive dimensions,we need to turn off rec dim keys from zero code calculations
4032   --for rec dim, the data at the top levels are aggregated data. so if we take a parent level
4033   --whose level=1, i.e top node, the value we see is the total or zero code value from the base MV
4034   if p_type=4 then
4035     declare
4036       l_level varchar2(100);
4037       l_src_object varchar2(20000);
4038       l_special_dim varchar2(100);
4039       l_rec_dim boolean;
4040       l_rec_dim_key varchar2(100);
4041     begin
4042       for i in 1..l_number_parameters loop
4043         if get_level_for_pk(l_parameter1(i),l_level,l_src_object,l_special_dim,
4044           l_rec_dim,l_rec_dim_key)=false then
4045           return false;
4046         end if;
4047         --if l_rec_dim=false then
4048           write_to_log_file_n(l_level||' is not a recursive dim, so add parameters');
4052           p_parameter2(p_number_parameters):=l_parameter2(i);
4049           p_calculation_table(p_number_parameters):=l_calculation_table(i);
4050           p_calculation_type(p_number_parameters):=l_calculation_type(i);
4051           p_parameter1(p_number_parameters):=l_parameter1(i);
4053           p_parameter3(p_number_parameters):=l_parameter3(i);
4054           p_parameter4(p_number_parameters):=l_parameter4(i);
4055           p_parameter5(p_number_parameters):=l_parameter5(i);
4056           p_number_parameters:=p_number_parameters+1;
4057         --end if;
4058       end loop;
4059       p_number_parameters:=p_number_parameters-1;
4060     end;
4061   else
4062     for i in 1..l_number_parameters loop
4063       p_calculation_table(p_number_parameters):=l_calculation_table(i);
4064       p_calculation_type(p_number_parameters):=l_calculation_type(i);
4065       p_parameter1(p_number_parameters):=l_parameter1(i);
4066       p_parameter2(p_number_parameters):=l_parameter2(i);
4067       p_parameter3(p_number_parameters):=l_parameter3(i);
4068       p_parameter4(p_number_parameters):=l_parameter4(i);
4069       p_parameter5(p_number_parameters):=l_parameter5(i);
4070       p_number_parameters:=p_number_parameters+1;
4071     end loop;
4072     p_number_parameters:=p_number_parameters-1;
4073   end if;
4074   if g_debug then
4075     write_to_log_file_n('In End of get_db_calculation'||' '||get_time);
4076     write_to_log_file_n('After Reassigning, Results');
4077     for i in 1..p_number_parameters loop
4078       write_to_log_file(p_calculation_table(i)||' '||p_calculation_type(i)||' '||p_parameter1(i)||' '||
4079       p_parameter2(i)||' '||p_parameter3(i)||' '||p_parameter4(i)||' '||p_parameter5(i));
4080     end loop;
4081   end if;
4082   -----------------------
4083   return true;
4084 Exception when others then
4085   BSC_IM_UTILS.g_status_message:=sqlerrm;
4086   g_status_message:=sqlerrm;
4087   write_to_log_file_n('Exception in get_db_calculation '||sqlerrm);
4088   return false;
4089 End;
4090 
4091 function get_table_relations(
4092 p_table varchar2,
4093 p_tables in out nocopy BSC_IM_UTILS.varchar_tabletype,
4094 p_source_tables in out nocopy BSC_IM_UTILS.varchar_tabletype,
4095 p_relation_type in out nocopy BSC_IM_UTILS.varchar_tabletype,
4096 p_number_tables in out nocopy number
4097 ) return boolean is
4098 l_stmt varchar2(32000);
4099 ------------------------------------------------
4100 cursor c1 (p_table_name varchar2)
4101 is select table_name,source_table_name,relation_type from bsc_db_tables_rels where
4102 table_name=p_table_name;
4103 ------------------------------------------------
4104 l_table_name varchar2(100);
4105 Begin
4106   if (p_table like 'BSC_B_%PRJ%') then
4107     l_table_name := get_b_table_name_for_prj(p_table);
4108   else
4109     l_table_name := p_table;
4110   end if;
4111   if g_debug then
4112     write_to_log_file_n('In get_table_relations');
4113   end if;
4114   if p_number_tables is null then
4115     p_number_tables:=1;
4116   else
4117     p_number_tables:=p_number_tables+1;
4118   end if;
4119   if g_debug then
4120     l_stmt:='select table_name,source_table_name,relation_type from bsc_db_tables_rels where '||
4121     'table_name=:1';
4122     write_to_log_file_n(l_stmt||' '||l_table_name);
4123   end if;
4124   open c1(l_table_name);
4125   loop
4126     fetch c1 into p_tables(p_number_tables),p_source_tables(p_number_tables),
4127     p_relation_type(p_number_tables);
4128     exit when c1%notfound;
4129     p_number_tables:=p_number_tables+1;
4130   end loop;
4131   p_number_tables:=p_number_tables-1;
4132   close c1;
4133   if g_debug then
4134     write_to_log_file_n('Results');
4135     for i in 1..p_number_tables loop
4136       write_to_log_file(p_tables(i)||' '||p_source_tables(i)||' '||p_relation_type(i));
4137     end loop;
4138   end if;
4139   return true;
4140 Exception when others then
4141   BSC_IM_UTILS.g_status_message:=sqlerrm;
4142   g_status_message:=sqlerrm;
4143   write_to_log_file_n('Exception in get_table_relations '||sqlerrm);
4144   return false;
4145 End;
4146 
4147 function get_calendar_for_periodicity(p_periodicity number) return number is
4148 l_cal number;
4149 l_stmt varchar2(1000);
4150 ---------------------------------------------
4151 cursor c1 (p_id number)
4152 is select calendar_id from bsc_sys_periodicities where periodicity_id=p_id;
4153 ---------------------------------------------
4154 Begin
4155   if g_debug then
4156     l_stmt:='select calendar_id from bsc_sys_periodicities where periodicity_id=:1';
4157     write_to_log_file_n(l_stmt||' '||p_periodicity);
4158   end if;
4159   open c1(p_periodicity);
4160   fetch c1 into l_cal;
4161   close c1;
4162   return l_cal;
4163 Exception when others then
4164   BSC_IM_UTILS.g_status_message:=sqlerrm;
4165   g_status_message:=sqlerrm;
4166   write_to_log_file_n('Exception in get_calendar_for_periodicity '||sqlerrm);
4167   return null;
4168 End;
4169 
4170 function get_summarize_calendar(
4171 p_periodicity number,
4172 p_calendar out nocopy varchar2,
4173 p_calendar_tables out nocopy varchar2,
4174 p_calendar_alias out nocopy varchar2,
4175 p_calendar_join_1 out nocopy varchar2,
4176 p_calendar_join_2 out nocopy varchar2
4177 )return boolean is
4181 l_db_column_name BSC_IM_UTILS.varchar_tabletype;
4178 -------------------------------------------------------
4179 l_periodicity_id BSC_IM_UTILS.number_tabletype;
4180 l_source BSC_IM_UTILS.varchar_tabletype;
4182 l_number_periodicity number;
4183 -------------------------------------------------------
4184 l_values BSC_IM_UTILS.varchar_tabletype;
4185 l_number_values number;
4186 l_index number;
4187 -------------------------------------------------------
4188 l_cal_column BSC_IM_UTILS.varchar_tabletype;
4189 l_cal_periodicity BSC_IM_UTILS.number_tabletype;
4190 l_number_cal_column number;
4191 l_calendar_id number;
4192 -------------------------------------------------------
4193 cursor c1 is select periodicity_id,source,db_column_name from bsc_sys_periodicities;
4194 -------------------------------------------------------
4195 Begin
4196 
4197   l_number_periodicity:=1;
4198   if g_debug then
4199     g_stmt:='select periodicity_id,source,db_column_name from bsc_sys_periodicities';
4200     write_to_log_file_n(g_stmt);
4201   end if;
4202   open c1;
4203   loop
4204     fetch c1 into l_periodicity_id(l_number_periodicity),l_source(l_number_periodicity),
4205     l_db_column_name(l_number_periodicity);
4206     exit when c1%notfound;
4207     l_number_periodicity:=l_number_periodicity+1;
4208   end loop;
4209   l_number_periodicity:=l_number_periodicity-1;
4210   close c1;
4211   if g_debug then
4212     write_to_log_file('Result');
4213     for i in 1..l_number_periodicity loop
4214       write_to_log_file(l_periodicity_id(i)||' '||l_source(i)||' '||l_db_column_name(i));
4215     end loop;
4216   end if;
4217   l_calendar_id:=get_calendar_for_periodicity(p_periodicity);
4218   if g_debug then
4219     write_to_log_file_n('The calendar id for periodicity '||p_periodicity||'='||l_calendar_id);
4220   end if;
4221   l_number_cal_column:=1;
4222   l_cal_periodicity(l_number_cal_column):=p_periodicity;
4223   l_cal_column(l_number_cal_column):=null;
4224   for i in 1..l_number_periodicity loop
4225     if l_periodicity_id(i)=p_periodicity then
4226       l_cal_column(l_number_cal_column):=l_db_column_name(i);
4227       exit;
4228     end if;
4229   end loop;
4230   for i in 1..l_number_periodicity loop
4231     l_number_values:=0;
4232     if BSC_IM_UTILS.parse_values(l_source(i),',',l_values,l_number_values)=false then
4233       return false;
4234     end if;
4235     for j in 1..l_number_values loop
4236       if l_values(j)=p_periodicity then
4237         if BSC_IM_UTILS.in_array(l_cal_column,l_number_cal_column,l_db_column_name(i))=false then
4238           l_number_cal_column:=l_number_cal_column+1;
4239           l_cal_periodicity(l_number_cal_column):=l_periodicity_id(i);
4240           l_cal_column(l_number_cal_column):=l_db_column_name(i);
4241         end if;
4242       end if;
4243     end loop;
4244   end loop;
4245   if g_debug then
4246     write_to_log_file_n('The cal columns');
4247     for i in 1..l_number_cal_column loop
4248       write_to_log_file(l_cal_column(i)||' '||l_cal_periodicity(i));
4249     end loop;
4250   end if;
4251   p_calendar:='(select distinct ';
4252   for i in 1..l_number_cal_column loop
4253     if l_cal_column(i) is not null then
4254       p_calendar:=p_calendar||l_cal_column(i)||',';
4255     end if;
4256   end loop;
4257   p_calendar:=substr(p_calendar,1,length(p_calendar)-1);
4258   p_calendar:=p_calendar||' from BSC_DB_CALENDAR ';
4259   if l_calendar_id is not null then
4260     p_calendar:=p_calendar||'where calendar_id='||l_calendar_id;
4261   end if;
4262   p_calendar:=p_calendar||')';
4263   p_calendar_tables:='BSC_DB_CALENDAR';
4264   p_calendar_alias:='BSC_DB_CALENDAR';
4265   p_calendar_join_1:=l_cal_column(1);
4266   p_calendar_join_2:='YEAR';
4267   return true;
4268 Exception when others then
4269   BSC_IM_UTILS.g_status_message:=sqlerrm;
4270   g_status_message:=sqlerrm;
4271   write_to_log_file_n('Exception in get_summarize_calendar '||sqlerrm);
4272   return false;
4273 End;
4274 
4275 function get_columns_in_formula(
4276 p_expression varchar2,
4277 p_measure BSC_IM_UTILS.varchar_tabletype,
4278 p_number_measure number,
4279 p_table out nocopy BSC_IM_UTILS.varchar_tabletype,
4280 p_number_table out nocopy number
4281 )return boolean is
4282 Begin
4283   p_number_table:=0;
4284   for i in 1..p_number_measure loop
4285     if instr(upper(p_expression),upper(p_measure(i)))>0 then
4286       if BSC_IM_UTILS.in_array(p_table,p_number_table,p_measure(i))=false then
4287         p_number_table:=p_number_table+1;
4288         p_table(p_number_table):=p_measure(i);
4289       end if;
4290     end if;
4291   end loop;
4292   if g_debug then
4293     write_to_log_file_n('Columns parsed out of '||p_expression||' :');
4294     for i in 1..p_number_table loop
4295       write_to_log_file(p_table(i));
4296     end loop;
4297   end if;
4298   return true;
4299 Exception when others then
4300   BSC_IM_UTILS.g_status_message:=sqlerrm;
4301   g_status_message:=sqlerrm;
4302   write_to_log_file_n('Exception in get_columns_in_formula '||sqlerrm);
4303   return false;
4304 End;
4305 
4306 function get_period_type_id(p_level varchar2) return number is
4307 l_period_type_id number;
4308 Begin
4309   if p_level='DAY' or p_level='DAY365' then
4310     l_period_type_id:=1;
4311   elsif p_level='WEEK' or p_level='WEEK52' then
4312     l_period_type_id:=16;
4313   elsif p_level='MONTH' then
4314     l_period_type_id:=32;
4318     l_period_type_id:=128;
4315   elsif p_level='QUARTER' then
4316     l_period_type_id:=64;
4317   elsif p_level='YEAR' then
4319   end if;
4320   return l_period_type_id;
4321 Exception when others then
4322   BSC_IM_UTILS.g_status_message:=sqlerrm;
4323   g_status_message:=sqlerrm;
4324   write_to_log_file_n('Exception in get_period_type_id '||sqlerrm);
4325   return null;
4326 End;
4327 
4328 function find_xtd_levels(
4329 p_periodicity number,
4330 p_xtd_levels out nocopy BSC_IM_UTILS.varchar_tabletype,
4331 p_number_xtd_levels out nocopy number
4332 )return boolean is
4333 l_stmt varchar2(2000);
4334 l_period_col_name varchar2(200);
4335 l_level varchar2(200);
4336 --------------------------------------
4337 cursor c1 (p_id number)
4338 is select db_column_name,period_col_name from bsc_sys_periodicities where periodicity_id=p_id;
4339 --------------------------------------
4340 Begin
4341   p_number_xtd_levels:=0;
4342   if g_debug then
4343     l_stmt:='select db_column_name,period_col_name from bsc_sys_periodicities where periodicity_id=:1';
4344     write_to_log_file_n(l_stmt||' '||p_periodicity);
4345   end if;
4346   open c1(p_periodicity);
4347   fetch c1 into l_level,l_period_col_name;
4348   close c1;
4349   if g_debug then
4350     write_to_log_file(l_level||' '||l_period_col_name);
4351   end if;
4352   if l_level='DAY' or l_level='DAY365' then
4353     p_xtd_levels(1):='WEEK52';
4354     p_xtd_levels(2):='MONTH';
4355     p_xtd_levels(3):='QUARTER';
4356     p_xtd_levels(4):='YEAR';
4357     p_number_xtd_levels:=4;
4358   elsif l_level='WEEK' or l_level='WEEK52' then
4359     p_xtd_levels(1):='YEAR';
4360     p_number_xtd_levels:=1;
4361   elsif l_level='MONTH' then
4362     p_xtd_levels(1):='QUARTER';
4363     p_xtd_levels(2):='YEAR';
4364     p_number_xtd_levels:=2;
4365   elsif l_level='QUARTER' then
4366     p_xtd_levels(1):='YEAR';
4367     p_number_xtd_levels:=1;
4368   elsif l_level='YEAR' then
4369     null;
4370   end if;
4371   return true;
4372 Exception when others then
4373   BSC_IM_UTILS.g_status_message:=sqlerrm;
4374   g_status_message:=sqlerrm;
4375   write_to_log_file_n('Exception in find_xtd_levels '||sqlerrm);
4376   return false;
4377 End;
4378 
4379 function load_reporting_calendar(
4380 p_options BSC_IM_UTILS.varchar_tabletype,
4381 p_number_options number
4382 )return boolean is
4383 ----------------
4384 --1001 is DBI ent calendar
4385 cursor c1 is select calendar_id,decode(edw_calendar_type_id,null,0,1,decode(edw_calendar_id,1001,2,1))
4386 from bsc_sys_calendars_b;
4387 ---------------
4388 l_calendar_id BSC_IM_UTILS.number_tabletype;
4389 l_calendar_type BSC_IM_UTILS.number_tabletype;
4390 l_number_calendar_id number;
4391 ---------------
4392 l_calendar_data cal_record_table;
4393 l_number_calendar_data number;
4394 l_periodicity_data cal_periodicity_table;
4395 l_number_periodicity_data number;
4396 l_hier BSC_IM_UTILS.varchar_tabletype;
4397 l_hier_type BSC_IM_UTILS.varchar_tabletype;
4398 l_number_hier number;
4399 ---------------
4400 l_rpt_calendar varchar2(200);
4401 l_rpt_calendar_owner varchar2(200);
4402 ---------------
4403 Begin
4404   if g_debug then
4405     write_to_log_file_n('In load_reporting_calendar '||get_time);
4406   end if;
4407   --Bug#3973335
4408   if BSC_IM_UTILS.get_option_value(g_options,g_number_options,'DEBUG LOG')='Y' then
4409     g_debug:=true;
4410   end if;
4411   l_rpt_calendar:='BSC_REPORTING_CALENDAR';
4412   l_rpt_calendar_owner:=BSC_IM_UTILS.get_table_owner(l_rpt_calendar);
4413   /*if BSC_IM_UTILS.truncate_table(l_rpt_calendar,l_rpt_calendar_owner)=false then
4414     null;
4415   end if;*/
4416   --get the list of calendars
4417   if g_debug then
4418     write_to_log_file_n('select calendar_id from bsc_sys_calendars_b');
4419   end if;
4420   l_number_calendar_id:=1;
4421   open c1;
4422   loop
4423     fetch c1 into l_calendar_id(l_number_calendar_id),l_calendar_type(l_number_calendar_id);
4424     exit when c1%notfound;
4425     drop_and_add_partition(l_calendar_id(l_number_calendar_id), l_rpt_calendar_owner);
4426     l_number_calendar_id:=l_number_calendar_id+1;
4427   end loop;
4428   close c1;
4429   l_number_calendar_id:=l_number_calendar_id-1;
4430   if g_debug then
4431     for i in 1..l_number_calendar_id loop
4432       write_to_log_file(l_calendar_id(i)||' '||l_calendar_type(i));
4433     end loop;
4434   end if;
4435   --------------create the temp table-----
4436   /*l_temp_table:='BSC_CAL_TEMP';
4437   l_stmt:='create global temporary table '||l_temp_table||'(record_type_id number,'||
4438   'period_type_id number,period number,year number) on commit preserve rows';
4439   BSC_APPS.Do_DDL(l_stmt, AD_DDL.CREATE_TABLE, l_temp_table);*/
4440   ----------------------------------------
4441   ------------------------
4442   /*for each calendar, get the periodicities to fetch from bsc_db_calendar
4443   find out the relations between the periodicities, see if they are the std
4444   periodicities. if yes, assign the period_type_id etc. if custom, generate
4445   the period_type_id etc. then come up with the hierarchies to load into the
4446   reporting calendar.
4447   then for each of the hier, load the rpt cal
4448   */
4449   for i in 1..l_number_calendar_id loop
4450     if g_debug then
4454       return false;
4451       write_to_log_file_n('Processing calendar '||l_calendar_id(i));
4452     end if;
4453     if get_calendar_data(l_calendar_id(i),l_calendar_data,l_number_calendar_data)=false then
4455     end if;
4456     if get_periodicity_data(l_calendar_id(i),l_calendar_type(i),l_periodicity_data,l_number_periodicity_data)=false then
4457       return false;
4458     end if;
4459     if built_hier(l_calendar_id(i),l_calendar_type(i),l_periodicity_data,l_number_periodicity_data,l_hier,l_hier_type,
4460       l_number_hier)=false then
4461       return false;
4462     end if;
4463     if l_calendar_type(i)=0 then
4464       if set_xtd_pattern(l_calendar_id(i),l_periodicity_data,l_number_periodicity_data,l_hier,l_hier_type,
4465         l_number_hier)=false then
4466         return false;
4467       end if;
4468     end if;
4469     -------------------------
4470     for j in 1..l_number_hier loop
4471       if l_hier_type(j)='Std' or l_hier_type(j)='Custom' then
4472         if load_reporting_calendar(
4473           l_calendar_id(i),
4474           null,
4475           l_hier(j),
4476           l_hier_type(j),
4477           l_periodicity_data,
4478           l_number_periodicity_data)=false then
4479           return false;
4480         end if;
4481       elsif l_hier_type(j)='DBI' then
4482         if load_reporting_calendar_DBI(
4483           l_calendar_id(i),
4484           null,
4485           l_hier(j),
4486           l_hier_type(j),
4487           l_calendar_data,
4488           l_number_calendar_data,
4489           l_periodicity_data,
4490           l_number_periodicity_data)=false then
4491           return false;
4492         end if;
4493         if load_rpt_cal_DBI_rolling(
4494           l_calendar_id(i),
4495           null,
4496           l_hier(j),
4497           l_hier_type(j),
4498           l_calendar_data,
4499           l_number_calendar_data,
4500           l_periodicity_data,
4501           l_number_periodicity_data)=false then
4502           return false;
4503         end if;
4504       end if;
4505     end loop;
4506     -------------------------
4507   end loop;
4508   if BSC_IM_UTILS.get_option_value(p_options,p_number_options,'ANALYZE')='Y' then
4509     BSC_IM_UTILS.analyze_object(l_rpt_calendar,l_rpt_calendar_owner,null,null,null);
4510   end if;
4511 
4512   return true;
4513 Exception when others then
4514   BSC_IM_UTILS.g_status_message:=sqlerrm;
4515   g_status_message:=sqlerrm;
4516   write_to_log_file_n('Exception in load_reporting_calendar '||sqlerrm);
4517   return false;
4518 End;
4519 
4520 --Fix bug#4027813 This function created to load reporting calendar only for the specified
4521 --calendar id
4522 function load_reporting_calendar(
4523 p_calendar_id number,
4524 p_options BSC_IM_UTILS.varchar_tabletype,
4525 p_number_options number
4526 )return boolean is
4527 ----------------
4528 --1001 is DBI ent calendar
4529 cursor c1 is select decode(edw_calendar_type_id,null,0,1,decode(edw_calendar_id,1001,2,1))
4530 from bsc_sys_calendars_b
4531 where calendar_id = p_calendar_id;
4532 ---------------
4533 l_calendar_type number;
4534 ---------------
4535 l_calendar_data cal_record_table;
4536 l_number_calendar_data number;
4537 l_periodicity_data cal_periodicity_table;
4538 l_number_periodicity_data number;
4539 l_hier BSC_IM_UTILS.varchar_tabletype;
4540 l_hier_type BSC_IM_UTILS.varchar_tabletype;
4541 l_number_hier number;
4542 ---------------
4543 l_rpt_calendar varchar2(200);
4544 l_rpt_calendar_owner varchar2(200);
4545 ---------------
4546 l_stmt varchar2(1000);
4547 Begin
4548   if g_debug then
4549     write_to_log_file_n('In load_reporting_calendar '||get_time);
4550   end if;
4551   if calendar_already_refreshed(p_calendar_id) then
4552     return true;
4553   end if;
4554   --Bug#3973335
4555   if BSC_IM_UTILS.get_option_value(g_options,g_number_options,'DEBUG LOG')='Y' then
4556     g_debug:=true;
4557   end if;
4558   l_rpt_calendar:='BSC_REPORTING_CALENDAR';
4559   l_rpt_calendar_owner:=BSC_IM_UTILS.get_table_owner(l_rpt_calendar);
4560   drop_and_add_partition(p_calendar_id, l_rpt_calendar_owner);
4561   --bug 4636259, performance issue, so partitioning;
4562 
4563   if g_debug then
4564     write_to_log_file_n('dropping and adding calendars for '||p_calendar_id);
4565   end if;
4566   --get the calendar type
4567   if g_debug then
4568     write_to_log_file_n('select decode(edw_calendar_type_id,null,0,1,decode(edw_calendar_id,1001,2,1))'||
4569                         ' from bsc_sys_calendars_b where calendar_id=p_calendar_id');
4570   end if;
4571   open c1;
4572   fetch c1 into l_calendar_type;
4573   close c1;
4574   --------------create the temp table-----
4575   /*l_temp_table:='BSC_CAL_TEMP';
4576   l_stmt:='create global temporary table '||l_temp_table||'(record_type_id number,'||
4577   'period_type_id number,period number,year number) on commit preserve rows';
4578   BSC_APPS.Do_DDL(l_stmt, AD_DDL.CREATE_TABLE, l_temp_table);*/
4579   ----------------------------------------
4580   ------------------------
4581   /*for this calendar, get the periodicities to fetch from bsc_db_calendar
4582   find out the relations between the periodicities, see if they are the std
4583   periodicities. if yes, assign the period_type_id etc. if custom, generate
4584   the period_type_id etc. then come up with the hierarchies to load into the
4585   reporting calendar.
4586   then for each of the hier, load the rpt cal
4587   */
4588   if g_debug then
4589     write_to_log_file_n('Processing calendar '||p_calendar_id);
4590   end if;
4591   if get_calendar_data(p_calendar_id,l_calendar_data,l_number_calendar_data)=false then
4592     return false;
4593   end if;
4594   if get_periodicity_data(p_calendar_id,l_calendar_type,l_periodicity_data,l_number_periodicity_data)=false then
4595     return false;
4596   end if;
4597   if built_hier(p_calendar_id,l_calendar_type,l_periodicity_data,l_number_periodicity_data,l_hier,l_hier_type,
4598     l_number_hier)=false then
4599     return false;
4600   end if;
4601   if l_calendar_type=0 then
4602     if set_xtd_pattern(p_calendar_id,l_periodicity_data,l_number_periodicity_data,l_hier,l_hier_type,
4603       l_number_hier)=false then
4604       return false;
4605     end if;
4606   end if;
4607   -------------------------
4608   for j in 1..l_number_hier loop
4612         null,
4609     if l_hier_type(j)='Std' or l_hier_type(j)='Custom' then
4610       if load_reporting_calendar(
4611         p_calendar_id,
4613         l_hier(j),
4614         l_hier_type(j),
4615         l_periodicity_data,
4616         l_number_periodicity_data)=false then
4617         return false;
4618       end if;
4619     elsif l_hier_type(j)='DBI' then
4620       if load_reporting_calendar_DBI(
4621         p_calendar_id,
4622         null,
4623         l_hier(j),
4624         l_hier_type(j),
4625         l_calendar_data,
4626         l_number_calendar_data,
4627         l_periodicity_data,
4628         l_number_periodicity_data)=false then
4629         return false;
4630       end if;
4631       if load_rpt_cal_DBI_rolling(
4632         p_calendar_id,
4633         null,
4634         l_hier(j),
4635         l_hier_type(j),
4636         l_calendar_data,
4637         l_number_calendar_data,
4638         l_periodicity_data,
4639         l_number_periodicity_data)=false then
4640         return false;
4641       end if;
4642     end if;
4643   end loop;
4644   -------------------------
4645   if BSC_IM_UTILS.get_option_value(p_options,p_number_options,'ANALYZE')='Y' then
4646     BSC_IM_UTILS.analyze_object(l_rpt_calendar,l_rpt_calendar_owner,null,null,'p_'||p_calendar_id);
4647   end if;
4648   cache_calendar_as_loaded(p_calendar_id);
4649   if g_debug then
4650     write_to_log_file_n('Completed load_reporting_calendar '||get_time);
4651     write_to_log_file_n('--------------------------------------------');
4652   end if;
4653   return true;
4654 Exception when others then
4655   BSC_IM_UTILS.g_status_message:=sqlerrm;
4656   g_status_message:=sqlerrm;
4657   write_to_log_file_n('Exception in load_reporting_calendar '||sqlerrm);
4658   return false;
4659 End;
4660 
4661 function get_calendar_data(
4662 p_calendar_id number,
4663 p_calendar_data out nocopy cal_record_table,
4664 p_number_calendar_data out nocopy number
4665 ) return boolean is
4666 cursor c1 (p_calendar_id number)
4667 is select calendar_year,calendar_month,calendar_day,year,semester,bimester,quarter,month,
4668 week52,day365,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7,
4669 custom_8,custom_9,custom_10,custom_11,custom_12,custom_13,custom_14,custom_15,
4670 custom_16,custom_17,custom_18,custom_19,custom_20 from bsc_db_calendar where calendar_id=p_calendar_id
4671 order by calendar_year,calendar_month,calendar_day;
4672 Begin
4673   if g_debug then
4674     write_to_log_file_n('select calendar_year,calendar_month,calendar_day,year,semester,bimester,quarter,month, '||
4675     'week52,day365,custom_1,custom_2,custom_3,custom_4,custom_5,custom_6,custom_7, '||
4676     'custom_8,custom_9,custom_10,custom_11,custom_12,custom_13,custom_14,custom_15, '||
4677     'custom_16,custom_17,custom_18,custom_19,custom_20 from bsc_db_calendar where calendar_id='||p_calendar_id||
4678     'order by calendar_year,calendar_month,calendar_day');
4679   end if;
4680   p_number_calendar_data:=1;
4681   p_calendar_data:=cal_record_table();
4682   p_calendar_data.extend;
4683   open c1(p_calendar_id);
4684   loop
4685     fetch c1 into
4686     p_calendar_data(p_number_calendar_data).calendar_year,
4687     p_calendar_data(p_number_calendar_data).calendar_month,
4688     p_calendar_data(p_number_calendar_data).calendar_day,
4689     p_calendar_data(p_number_calendar_data).year,
4690     p_calendar_data(p_number_calendar_data).semester,
4691     p_calendar_data(p_number_calendar_data).bimester,
4692     p_calendar_data(p_number_calendar_data).quarter,
4693     p_calendar_data(p_number_calendar_data).month,
4694     p_calendar_data(p_number_calendar_data).week52,
4695     p_calendar_data(p_number_calendar_data).day365,
4696     p_calendar_data(p_number_calendar_data).custom_1,
4697     p_calendar_data(p_number_calendar_data).custom_2,
4698     p_calendar_data(p_number_calendar_data).custom_3,
4699     p_calendar_data(p_number_calendar_data).custom_4,
4700     p_calendar_data(p_number_calendar_data).custom_5,
4701     p_calendar_data(p_number_calendar_data).custom_6,
4702     p_calendar_data(p_number_calendar_data).custom_7,
4703     p_calendar_data(p_number_calendar_data).custom_8,
4704     p_calendar_data(p_number_calendar_data).custom_9,
4705     p_calendar_data(p_number_calendar_data).custom_10,
4706     p_calendar_data(p_number_calendar_data).custom_11,
4707     p_calendar_data(p_number_calendar_data).custom_12,
4708     p_calendar_data(p_number_calendar_data).custom_13,
4709     p_calendar_data(p_number_calendar_data).custom_14,
4710     p_calendar_data(p_number_calendar_data).custom_15,
4711     p_calendar_data(p_number_calendar_data).custom_16,
4712     p_calendar_data(p_number_calendar_data).custom_17,
4713     p_calendar_data(p_number_calendar_data).custom_18,
4714     p_calendar_data(p_number_calendar_data).custom_19,
4715     p_calendar_data(p_number_calendar_data).custom_20;
4716     exit when c1%notfound;
4717     p_number_calendar_data:=p_number_calendar_data+1;
4718     p_calendar_data.extend;
4719   end loop;
4720   p_number_calendar_data:=p_number_calendar_data-1;
4721   close c1;
4722   return true;
4723 Exception when others then
4724   BSC_IM_UTILS.g_status_message:=sqlerrm;
4725   g_status_message:=sqlerrm;
4726   write_to_log_file_n('Exception in get_calendar_data '||sqlerrm);
4727   return false;
4728 End;
4729 
4730 function get_periodicity_data(
4731 p_calendar_id number,
4732 p_calendar_type number,
4733 p_periodicity_data out nocopy cal_periodicity_table,
4734 p_number_periodicity_data out nocopy number
4735 )return boolean is
4736 --------------------------
4737 cursor c1(p_calendar_id number) is
4738 select periodicity_id,source,db_column_name,periodicity_type,
4739 period_type_id,record_type_id,xtd_pattern from bsc_sys_periodicities
4740 where calendar_id=p_calendar_id and periodicity_type not in (11,12);
4741 --------------------------
4745 Begin
4742 l_custom_count number;
4743 --------------------------
4744 
4746 
4747   if g_debug then
4748     write_to_log_file_n('select periodicity_id,source,db_column_name,periodicity_type,'||
4749     'period_type_id,record_type_id,xtd_pattern from bsc_sys_periodicities '||
4750     'where calendar_id='||p_calendar_id||' and periodicity_type not in (11,12)');
4751   end if;
4752   l_custom_count:=0;
4753   open c1(p_calendar_id);
4754   p_number_periodicity_data:=1;
4755   p_periodicity_data:=cal_periodicity_table();
4756   p_periodicity_data.extend;
4757   loop
4758     fetch c1 into
4759     p_periodicity_data(p_number_periodicity_data).periodicity_id,
4760     p_periodicity_data(p_number_periodicity_data).source,
4761     p_periodicity_data(p_number_periodicity_data).db_column_name,
4762     p_periodicity_data(p_number_periodicity_data).periodicity_type,
4763     p_periodicity_data(p_number_periodicity_data).period_type_id,
4764     p_periodicity_data(p_number_periodicity_data).record_type_id,
4765     p_periodicity_data(p_number_periodicity_data).xtd_pattern;
4766     exit when c1%notfound;
4767     p_number_periodicity_data:=p_number_periodicity_data+1;
4768     p_periodicity_data.extend;
4769   end loop;
4770   p_number_periodicity_data:=p_number_periodicity_data-1;
4771   close c1;
4772   g_periodicity_id_for_type(1):=get_periodicity_for_type(1,p_periodicity_data,p_number_periodicity_data);
4773   g_periodicity_id_for_type(2):=get_periodicity_for_type(2,p_periodicity_data,p_number_periodicity_data);
4774   g_periodicity_id_for_type(3):=get_periodicity_for_type(3,p_periodicity_data,p_number_periodicity_data);
4775   g_periodicity_id_for_type(4):=get_periodicity_for_type(4,p_periodicity_data,p_number_periodicity_data);
4776   g_periodicity_id_for_type(5):=get_periodicity_for_type(5,p_periodicity_data,p_number_periodicity_data);
4777   g_periodicity_id_for_type(7):=get_periodicity_for_type(7,p_periodicity_data,p_number_periodicity_data);
4778   g_periodicity_id_for_type(9):=get_periodicity_for_type(9,p_periodicity_data,p_number_periodicity_data);
4779   ---------------------------------------------------------------------
4780   if p_calendar_type=0 then --this is BSC calendar
4781     for i in 1..p_number_periodicity_data loop
4782       if p_periodicity_data(i).periodicity_type=1 then
4783         p_periodicity_data(i).period_type_id:=128;
4784         p_periodicity_data(i).record_type_id:=1024;
4785         p_periodicity_data(i).xtd_pattern:=null;
4786       elsif p_periodicity_data(i).periodicity_type=2 then
4787         p_periodicity_data(i).period_type_id:=8192;
4788         p_periodicity_data(i).record_type_id:=8192;
4789         p_periodicity_data(i).xtd_pattern:=null;
4790       elsif p_periodicity_data(i).periodicity_type=3 then
4791         p_periodicity_data(i).period_type_id:=64;
4792         p_periodicity_data(i).record_type_id:=64;
4793         p_periodicity_data(i).xtd_pattern:=null;
4794       elsif p_periodicity_data(i).periodicity_type=4 then
4795         p_periodicity_data(i).period_type_id:=4096;
4796         p_periodicity_data(i).record_type_id:=4096;
4797         p_periodicity_data(i).xtd_pattern:=null;
4798       elsif p_periodicity_data(i).periodicity_type=5 then
4799         p_periodicity_data(i).period_type_id:=32;
4800         p_periodicity_data(i).record_type_id:=32;
4801         p_periodicity_data(i).xtd_pattern:=null;
4802       elsif p_periodicity_data(i).periodicity_type=7 then
4803         p_periodicity_data(i).period_type_id:=16;
4804         p_periodicity_data(i).record_type_id:=16;
4805         p_periodicity_data(i).xtd_pattern:=null;
4806       elsif p_periodicity_data(i).periodicity_type=9 then
4807         p_periodicity_data(i).period_type_id:=1;
4808         p_periodicity_data(i).record_type_id:=1;
4809         p_periodicity_data(i).xtd_pattern:=null;
4810       else --custom periodicities
4811         --algo to find the record type and pattern
4812         l_custom_count:=l_custom_count+1;
4813         p_periodicity_data(i).period_type_id:=16384*l_custom_count;
4814         p_periodicity_data(i).record_type_id:=p_periodicity_data(i).period_type_id;
4815         p_periodicity_data(i).xtd_pattern:=null;
4816       end if;
4817     end loop;
4818   end if;
4819   if g_debug then
4820     for i in 1..p_number_periodicity_data loop
4821       write_to_log_file(p_periodicity_data(i).periodicity_id||' '||p_periodicity_data(i).source||' '||
4822       p_periodicity_data(i).db_column_name||' '||p_periodicity_data(i).periodicity_type||' '||
4823       p_periodicity_data(i).period_type_id||' '||p_periodicity_data(i).record_type_id||' '||
4824       p_periodicity_data(i).xtd_pattern);
4825     end loop;
4826   end if;
4827   return true;
4828 Exception when others then
4829   BSC_IM_UTILS.g_status_message:=sqlerrm;
4830   g_status_message:=sqlerrm;
4831   write_to_log_file_n('Exception in get_periodicity_data '||sqlerrm);
4832   return false;
4833 End;
4834 
4835 function set_xtd_pattern(
4836 p_calendar_id number,
4837 p_periodicity_data in out nocopy cal_periodicity_table,
4838 p_number_periodicity_data number,
4839 p_hier BSC_IM_UTILS.varchar_tabletype,
4840 p_hier_type BSC_IM_UTILS.varchar_tabletype,
4841 p_number_hier number
4842 )return boolean is
4843 l_pattern BSC_IM_UTILS.varchar_tabletype;
4844 l_dbi_hier varchar2(400);
4845 l_pattern_sum number;
4846 Begin
4847   if g_debug then
4848     write_to_log_file_n('In set_xtd_pattern');
4849   end if;
4850   --first: set the pattern for each std and custom periodicity
4851   for i in 1..p_number_periodicity_data loop
4852     l_pattern(i):=null;
4853     for j in 1..p_number_hier loop
4854       if p_hier_type(j)='Std' or p_hier_type(j)='Custom' then
4855         l_pattern_sum:=get_xtd_pattern(p_periodicity_data(i).periodicity_id,p_hier(j),
4856         p_periodicity_data,p_number_periodicity_data);
4857         if l_pattern_sum is not null and l_pattern_sum>0 then
4858           l_pattern(i):=l_pattern(i)||','||p_hier(j)||',:'||l_pattern_sum||';';
4859         end if;
4860       end if;
4864     if p_hier_type(i)='DBI' then
4861     end loop;
4862   end loop;
4863   for i in 1..p_number_hier loop
4865       l_dbi_hier:=','||p_hier(i)||',';
4866       exit;
4867     end if;
4868   end loop;
4869   --DBI hier goes last
4870   --there is no entry for semester or bimester
4871   for i in 1..p_number_periodicity_data loop
4872     if p_periodicity_data(i).periodicity_type=1 then
4873       l_pattern(i):=l_pattern(i)||l_dbi_hier||':119;';
4874     elsif p_periodicity_data(i).periodicity_type=3 then
4875       l_pattern(i):=l_pattern(i)||l_dbi_hier||':55;';
4876     elsif p_periodicity_data(i).periodicity_type=5 then
4877       l_pattern(i):=l_pattern(i)||l_dbi_hier||':23;';
4878     elsif p_periodicity_data(i).periodicity_type=7 then
4879       l_pattern(i):=l_pattern(i)||l_dbi_hier||':11;';
4880     elsif p_periodicity_data(i).periodicity_type=9 then
4881       l_pattern(i):=l_pattern(i)||l_dbi_hier||':1143;';
4882     end if;
4883   end loop;
4884   for i in 1..p_number_periodicity_data loop
4885     p_periodicity_data(i).xtd_pattern:=l_pattern(i);
4886   end loop;
4887   --update bsc_sys_periodicities table
4888   for i in 1..p_number_periodicity_data loop
4889     execute immediate 'update bsc_sys_periodicities set period_type_id=:1,'||
4890     'record_type_id=:2,xtd_pattern=:3 '||
4891     'where calendar_id=:4 and periodicity_id=:5' using p_periodicity_data(i).period_type_id,
4892     p_periodicity_data(i).record_type_id,p_periodicity_data(i).xtd_pattern,p_calendar_id,
4893     p_periodicity_data(i).periodicity_id;
4894   end loop;
4895   commit;
4896   return true;
4897 Exception when others then
4898   BSC_IM_UTILS.g_status_message:=sqlerrm;
4899   g_status_message:=sqlerrm;
4900   write_to_log_file_n('Exception in set_xtd_pattern '||sqlerrm);
4901   return false;
4902 End;
4903 
4904 function get_xtd_pattern(
4905 p_periodicity_id number,
4906 p_hier varchar2,
4907 p_periodicity_data cal_periodicity_table,
4908 p_number_periodicity_data number
4909 ) return number is
4910 l_pattern_sum number;
4911 l_periodicity BSC_IM_UTILS.number_tabletype;
4912 l_number_periodicity number;
4913 --
4914 l_start number;
4915 Begin
4916 
4917   --see if the periodicity is in the hier, then look at the periodicities below and calculate the pattern
4918   l_pattern_sum:=0;
4919   if BSC_IM_UTILS.parse_values(p_hier,',',l_periodicity,l_number_periodicity)=false then
4920     return null;
4921   end if;
4922   for i in 1..l_number_periodicity loop
4923     if l_periodicity(i)=p_periodicity_id then
4924       if i=l_number_periodicity then --this is day, we need inception to date
4925         l_start:=1;
4926       else
4927         l_start:=i+1;
4928       end if;
4929       for j in l_start..l_number_periodicity loop
4930         for k in 1..p_number_periodicity_data loop
4931           if p_periodicity_data(k).periodicity_id=l_periodicity(j) then
4932             l_pattern_sum:=l_pattern_sum+p_periodicity_data(k).record_type_id;
4933             exit;
4934           end if;
4935         end loop;
4936       end loop;
4937       exit;
4938     end if;
4939   end loop;
4940   return l_pattern_sum;
4941 Exception when others then
4942   BSC_IM_UTILS.g_status_message:=sqlerrm;
4943   g_status_message:=sqlerrm;
4944   write_to_log_file_n('In get_xtd_pattern, p_periodicity_id='||p_periodicity_id||',p_hier='||p_hier);
4945   write_to_log_file_n('Exception in get_xtd_pattern '||sqlerrm);
4946   return null;
4947 End;
4948 
4949 function built_hier(
4950 p_calendar_id number,
4951 p_calendar_type number,
4952 p_periodicity_data cal_periodicity_table,
4953 p_number_periodicity_data number,
4954 p_hier out nocopy BSC_IM_UTILS.varchar_tabletype,
4955 p_hier_type out nocopy BSC_IM_UTILS.varchar_tabletype,
4956 p_number_hier out nocopy number
4957 )return boolean is
4958 --------------------------
4959 l_parent_hier BSC_IM_UTILS.number_tabletype;
4960 l_child_hier BSC_IM_UTILS.number_tabletype;
4961 l_parent_type BSC_IM_UTILS.varchar_tabletype;
4962 l_looked_at BSC_IM_UTILS.boolean_tabletype;
4963 l_number_hier number;
4964 --------------------------
4965 l_num_table BSC_IM_UTILS.number_tabletype;
4966 l_number_num_table number;
4967 --------------------------
4968 l_custom_periodicity BSC_IM_UTILS.number_tabletype;
4969 l_number_custom_periodicity number;
4970 l_custom_parent BSC_IM_UTILS.number_tabletype;
4971 l_custom_child BSC_IM_UTILS.number_tabletype;
4972 l_custom_flag BSC_IM_UTILS.boolean_tabletype;
4973 l_number_custom_hier number;
4974 --------------------------
4975 l_seed number;
4976 --------------------------
4977 Begin
4978   ---------------------------------------------------------------------
4979   --for all these periodicities, construct the hierarchies
4980   p_number_hier:=0;
4981   l_number_hier:=0;
4982   --make the std parent child relations
4983   --p_calendar_type=1 means DBI time calendar, 0 means BSC
4984   if p_calendar_type=0 then
4985     l_parent_hier(1):=g_periodicity_id_for_type(1);
4986     l_child_hier(1):=g_periodicity_id_for_type(3);
4987     l_parent_type(1):='Std';
4988     l_parent_hier(2):=g_periodicity_id_for_type(1);
4989     l_child_hier(2):=g_periodicity_id_for_type(2);
4990     l_parent_type(2):='Std';
4991     l_parent_hier(3):=g_periodicity_id_for_type(1);
4992     l_child_hier(3):=g_periodicity_id_for_type(7);
4993     l_parent_type(3):='Std';
4994     l_parent_hier(4):=g_periodicity_id_for_type(2);
4995     l_child_hier(4):=g_periodicity_id_for_type(3);
4996     l_parent_type(4):='Std';
4997     l_parent_hier(5):=g_periodicity_id_for_type(2);
4998     l_child_hier(5):=g_periodicity_id_for_type(4);
4999     l_parent_type(5):='Std';
5000     l_parent_hier(6):=g_periodicity_id_for_type(3);
5001     l_child_hier(6):=g_periodicity_id_for_type(5);
5002     l_parent_type(6):='Std';
5006     l_parent_hier(8):=g_periodicity_id_for_type(5);
5003     l_parent_hier(7):=g_periodicity_id_for_type(4);
5004     l_child_hier(7):=g_periodicity_id_for_type(5);
5005     l_parent_type(7):='Std';
5007     l_child_hier(8):=g_periodicity_id_for_type(9);
5008     l_parent_type(8):='Std';
5009     l_parent_hier(9):=g_periodicity_id_for_type(7);
5010     l_child_hier(9):=g_periodicity_id_for_type(9);
5011     l_parent_type(9):='Std';
5012     l_number_hier:=9;
5013   end if;
5014   l_number_custom_periodicity:=0;
5015   --p_calendar_type=2 is DBI ent calendar
5016   for i in 1..p_number_periodicity_data loop
5017     if (p_calendar_type=0 and p_periodicity_data(i).periodicity_type=0) or p_calendar_type in(1,2) then --custom periodicity
5018       l_number_hier:=l_number_hier+1;
5019       l_parent_hier(l_number_hier):=p_periodicity_data(i).periodicity_id;
5020       l_child_hier(l_number_hier):=p_periodicity_data(i).source;
5021       l_parent_type(l_number_hier):='Custom';
5022       l_number_custom_periodicity:=l_number_custom_periodicity+1;
5023       l_custom_periodicity(l_number_custom_periodicity):=p_periodicity_data(i).periodicity_id;
5024     end if;
5025   end loop;
5026   if g_debug then
5027     write_to_log_file_n('The parent child periodicities before bringing in full custom relations');
5028     for i in 1..l_number_hier loop
5029       write_to_log_file(l_parent_hier(i)||'('||l_parent_type(i)||') '||l_child_hier(i));
5030     end loop;
5031   end if;
5032   --for custom periodicities, we need to set the parent correctly
5033   l_number_custom_hier:=0;
5034   for i in 1..p_number_periodicity_data loop
5035     if p_periodicity_data(i).periodicity_type<>0 then
5036       for j in 1..l_number_custom_periodicity loop
5037         if BSC_IM_UTILS.parse_and_find(p_periodicity_data(i).source,',',l_custom_periodicity(j)) then
5038           l_number_custom_hier:=l_number_custom_hier+1;
5039           l_custom_parent(l_number_custom_hier):=p_periodicity_data(i).periodicity_id;
5040           l_custom_child(l_number_custom_hier):=l_custom_periodicity(j);
5041           l_custom_flag(l_number_custom_hier):=true;
5042         end if;
5043       end loop;
5044     end if;
5045   end loop;
5046   if g_debug then
5047     write_to_log_file_n('The custom parent child');
5048     for i in 1..l_number_custom_hier loop
5049       write_to_log_file(l_custom_parent(i)||' '||l_custom_child(i));
5050     end loop;
5051   end if;
5052   --now see if we can eliminate some
5053   for i in 1..l_number_custom_hier loop
5054     if BSC_IM_UTILS.in_array(l_child_hier,l_number_hier,l_custom_child(i)) then
5055       l_custom_flag(i):=false;
5056     else
5057       for j in 1..l_number_custom_hier loop
5058         if i<>j and l_custom_child(i)=l_custom_child(j) and l_custom_parent(i)<l_custom_parent(j) then
5059           l_custom_flag(i):=false;
5060         end if;
5061       end loop;
5062     end if;
5063   end loop;
5064   if g_debug then
5065     write_to_log_file_n('After eliminate, the custom parent child');
5066     for i in 1..l_number_custom_hier loop
5067       if l_custom_flag(i) then
5068         write_to_log_file(l_custom_parent(i)||' '||l_custom_child(i));
5069       end if;
5070     end loop;
5071   end if;
5072   for i in 1..l_number_custom_hier loop
5073     if l_custom_flag(i) then
5074       l_number_hier:=l_number_hier+1;
5075       l_parent_hier(l_number_hier):=l_custom_parent(i);
5076       l_child_hier(l_number_hier):=l_custom_child(i);
5077       l_parent_type(l_number_hier):='Std';
5078     end if;
5079   end loop;
5080   --build the hier
5081   --first get the seed
5082   l_seed:=0;
5083   if p_calendar_type=0 then
5084     l_seed:=l_parent_hier(1);
5085   else
5086     for i in 1..p_number_periodicity_data loop
5087       if p_periodicity_data(i).periodicity_type<>0 then
5088         if p_periodicity_data(i).periodicity_id>l_seed then
5089           l_seed:=p_periodicity_data(i).periodicity_id;
5090         end if;
5091       end if;
5092     end loop;
5093   end if;
5094   if g_debug then
5095     write_to_log_file_n('seed='||l_seed);
5096   end if;
5097   if built_hier_rec(
5098     l_seed,
5099     l_parent_hier,
5100     l_child_hier,
5101     l_parent_type,
5102     l_number_hier,
5103     p_hier,
5104     p_hier_type,
5105     p_number_hier)=false then
5106     return false;
5107   end if;
5108   if g_debug then
5109     write_to_log_file_n('The hierarchies generated recursively');
5110     for i in 1..p_number_hier loop
5111       write_to_log_file(p_hier(i)||' '||p_hier_type(i));
5112     end loop;
5113   end if;
5114   if p_calendar_type=0 then
5115     p_number_hier:=p_number_hier+1;
5116     p_hier_type(p_number_hier):='DBI';
5117     p_hier(p_number_hier):=g_periodicity_id_for_type(1)||','||
5118     g_periodicity_id_for_type(3)||','||g_periodicity_id_for_type(5)||','||g_periodicity_id_for_type(7)||','||
5119     g_periodicity_id_for_type(9);
5120   elsif p_calendar_type=2 then --DBI ent calendar
5121     p_number_hier:=p_number_hier+1;
5122     p_hier_type(p_number_hier):='DBI';
5123     p_hier(p_number_hier):=g_periodicity_id_for_type(1)||','||
5124     g_periodicity_id_for_type(3)||','||g_periodicity_id_for_type(5)||','||g_periodicity_id_for_type(7)||','||
5125     g_periodicity_id_for_type(9);
5126   end if;
5127   if g_debug then
5128     write_to_log_file_n('The Final list of hierarchies');
5129     for i in 1..p_number_hier loop
5130       write_to_log_file(p_hier(i)||' '||p_hier_type(i));
5131     end loop;
5132   end if;
5133   return true;
5134 Exception when others then
5135   BSC_IM_UTILS.g_status_message:=sqlerrm;
5136   g_status_message:=sqlerrm;
5137   write_to_log_file_n('Exception in built_hier '||sqlerrm);
5138   return false;
5139 End;
5140 
5141 function built_hier_rec(
5142 p_parent number,
5146 p_number_rel number,
5143 p_parent_hier BSC_IM_UTILS.number_tabletype,
5144 p_child_hier BSC_IM_UTILS.number_tabletype,
5145 p_parent_type BSC_IM_UTILS.varchar_tabletype,
5147 p_hier out nocopy BSC_IM_UTILS.varchar_tabletype,
5148 p_hier_type out nocopy BSC_IM_UTILS.varchar_tabletype,
5149 p_number_hier out nocopy number
5150 )return boolean is
5151 -------
5152 l_hier BSC_IM_UTILS.varchar_tabletype;
5153 l_hier_type BSC_IM_UTILS.varchar_tabletype;
5154 l_number_hier number;
5155 ----
5156 l_found boolean;
5157 ----
5158 Begin
5159   p_number_hier:=0;
5160   l_found:=false;
5161   for i in 1..p_number_rel loop
5162     if p_parent_hier(i)=p_parent then
5163       l_found:=true;
5164       if built_hier_rec(p_child_hier(i),p_parent_hier,p_child_hier,p_parent_type,p_number_rel,l_hier,l_hier_type,
5165         l_number_hier)=false then
5166         return false;
5167       end if;
5168       for j in 1..l_number_hier loop
5169         p_number_hier:=p_number_hier+1;
5170         if l_hier(j) is not null then
5171           p_hier(p_number_hier):=p_parent||','||l_hier(j);
5172         else
5173           p_hier(p_number_hier):=p_parent;
5174         end if;
5175         if p_parent_type(i)='Custom' or l_hier_type(j)='Custom' then
5176           p_hier_type(p_number_hier):='Custom';
5177         else
5178           p_hier_type(p_number_hier):='Std';
5179         end if;
5180       end loop;
5181     end if;
5182   end loop;
5183   if l_found=false then --for day level
5184     p_number_hier:=p_number_hier+1;
5185     p_hier(p_number_hier):=p_parent;
5186     p_hier_type(p_number_hier):='Std';
5187   end if;
5188   return true;
5189 Exception when others then
5190   BSC_IM_UTILS.g_status_message:=sqlerrm;
5191   g_status_message:=sqlerrm;
5192   write_to_log_file_n('Exception in built_hier_rec '||sqlerrm);
5193   return false;
5194 End;
5195 
5196 
5197 function get_periodicity_for_type(
5198 p_periodicity_type number,
5199 p_periodicity_data cal_periodicity_table,
5200 p_number_periodicity_data number
5201 )return number is
5202 Begin
5203   for i in 1..p_number_periodicity_data loop
5204     if p_periodicity_data(i).periodicity_type=p_periodicity_type then
5205       return p_periodicity_data(i).periodicity_id;
5206     end if;
5207   end loop;
5208   return null;
5209 Exception when others then
5210   BSC_IM_UTILS.g_status_message:=sqlerrm;
5211   g_status_message:=sqlerrm;
5212   write_to_log_file_n('Exception in get_periodicity_for_type '||sqlerrm);
5213   return null;
5214 End;
5215 
5216 function get_period_type_id_for_period(
5217 p_periodicity_id number,
5218 p_periodicity_data cal_periodicity_table,
5219 p_number_periodicity_data number
5220 )return number is
5221 Begin
5222   for i in 1..p_number_periodicity_data loop
5223     if p_periodicity_data(i).periodicity_id=p_periodicity_id then
5224       return p_periodicity_data(i).period_type_id;
5225     end if;
5226   end loop;
5227   return null;
5228 Exception when others then
5229   BSC_IM_UTILS.g_status_message:=sqlerrm;
5230   g_status_message:=sqlerrm;
5231   write_to_log_file_n('Exception in get_period_type_id_for_period '||sqlerrm);
5232   return null;
5233 End;
5234 
5235 function get_period_type_id_for_period(
5236 p_periodicity_id number
5237 )return number is
5238 cursor c1(p_periodicity number) is select period_type_id from bsc_sys_periodicities where
5239 periodicity_id=p_periodicity;
5240 l_period_type_id number;
5241 Begin
5242   if g_debug then
5243     write_to_log_file_n('select period_type_id from bsc_sys_periodicities where periodicity_id='||
5244     p_periodicity_id);
5245   end if;
5246   open c1(p_periodicity_id);
5247   fetch c1 into l_period_type_id;
5248   close c1;
5249   return l_period_type_id;
5250 Exception when others then
5251   BSC_IM_UTILS.g_status_message:=sqlerrm;
5252   g_status_message:=sqlerrm;
5253   write_to_log_file_n('Exception in get_period_type_id_for_period '||sqlerrm);
5254   return null;
5255 End;
5256 
5257 
5258 
5259 /*
5260 This is for all hierarchies that rollup.
5261 Cannot be used for Y Q M W D. W does not rollup to M.
5262 */
5263 function load_reporting_calendar(
5264 p_calendar_id number,
5265 p_calendar_type varchar2,
5266 p_hierarchy varchar2,
5267 p_hierarchy_type varchar2,
5268 p_periodicity_data cal_periodicity_table,
5269 p_number_periodicity_data number
5270 )return boolean is
5271 -----------------------------
5272 l_periods BSC_IM_UTILS.number_tabletype;
5273 l_period_record_type BSC_IM_UTILS.number_tabletype;
5274 l_period_period_type BSC_IM_UTILS.number_tabletype;
5275 l_period_periodicity_id BSC_IM_UTILS.number_tabletype;
5276 l_period_column BSC_IM_UTILS.varchar_tabletype;
5277 l_number_periods number;
5278 -----------------------------
5279 l_stmt varchar2(32750);
5280 randomString varchar2(20);
5281 l_count number;
5282 newline varchar2(10);
5283 -----------------------------
5284 Begin
5285   newline:='
5286 ';
5287   if g_debug then
5288     write_to_log_file_n('In load_reporting_calendar, p_calendar_id='||p_calendar_id||','||
5289     'p_calendar_type='||p_calendar_type||',p_hierarchy='||p_hierarchy||',p_hierarchy_type='||p_hierarchy_type||
5290     get_time);
5291   end if;
5292   if BSC_im_utils.parse_values(p_hierarchy,',',l_periods,l_number_periods)=false then
5293     return false;
5294   end if;
5295   --for these periods get the record_type_id
5296   --for each periodicity, there is one record type id which is the same as
5297   --periodicity_id
5298   for i in 1..l_number_periods loop
5299     for j in 1..p_number_periodicity_data loop
5300       if p_periodicity_data(j).periodicity_id=l_periods(i) then
5304         l_period_periodicity_id(i):=p_periodicity_data(j).periodicity_id;
5301         l_period_column(i):=p_periodicity_data(j).db_column_name;
5302         l_period_record_type(i):=p_periodicity_data(j).record_type_id;
5303         l_period_period_type(i):=p_periodicity_data(j).period_type_id;
5305         exit;
5306       end if;
5307     end loop;
5308   end loop;
5309   if g_debug then
5310     write_to_log_file_n('periods record_type and db_column_name');
5311     for i in 1..l_number_periods loop
5312       write_to_log_file(l_periods(i)||' '||l_period_record_type(i)||' '||l_period_column(i));
5313     end loop;
5314   end if;
5315   -------------------------
5316   --we need to create a function dynamically and execute it.
5317   select to_char(systimestamp, 'HHMISSFF') into randomString from dual; --Bug 4027813
5318   l_stmt:='create or replace procedure ld_rpt_cal_'||randomString||' as
5319   type cal_record is record(calendar_year number,calendar_month number,calendar_day number'||newline;
5320   for i in 1..l_number_periods loop
5321     l_stmt:=l_stmt||','||l_period_column(i)||' number';
5322   end loop;
5323   l_stmt:=l_stmt||');'||newline||
5324   'type cal_record_table is table of cal_record;'||newline||'l_cal_records cal_record_table;'||newline||
5325   'l_number_cal_records number;'||newline||
5326   'cursor c1 is select calendar_year,calendar_month,calendar_day'||newline;
5327   for i in 1..l_number_periods loop
5328     l_stmt:=l_stmt||','||l_period_column(i);
5329   end loop;
5330   l_stmt:=l_stmt||'
5331   from bsc_db_calendar where calendar_id='||p_calendar_id||' order by calendar_year,calendar_month,calendar_day;'||
5332   newline;
5333   --build the variables that will be something like quarter for YTD, month for YTD and day for YTD etc
5334   for i in 1..l_number_periods loop
5335     l_stmt:=l_stmt||'l_'||l_period_column(1)||'_'||l_period_column(i)||' BSC_IM_UTILS.number_tabletype;'||newline;
5336     l_stmt:=l_stmt||'l_num_'||l_period_column(1)||'_'||l_period_column(i)||' number:=0;'||newline;
5337     l_stmt:=l_stmt||'l_prev_'||l_period_column(1)||'_'||l_period_column(i)||' number:=null;'||newline;
5338     l_stmt:=l_stmt||'l_'||l_period_column(i)||'_period_count BSC_IM_UTILS.number_tabletype;'||newline;
5339   end loop;
5340   l_stmt:=l_stmt||'l_final_report_date BSC_IM_UTILS.date_tabletype;
5341   l_final_period BSC_IM_UTILS.number_tabletype;
5342   l_final_year BSC_IM_UTILS.number_tabletype;
5343   l_final_period_type_id BSC_IM_UTILS.number_tabletype;
5344   l_final_periodicity_id BSC_IM_UTILS.number_tabletype;
5345   l_final_period_flag BSC_IM_UTILS.number_tabletype;
5346   l_final_period_count BSC_IM_UTILS.number_tabletype;
5347   l_num_final number:=0;
5348   -------------------
5349   begin
5350   l_number_cal_records:=1;
5351   l_cal_records:=cal_record_table();
5352   l_cal_records.extend;
5353   open c1;
5354   loop
5355   fetch c1 into l_cal_records(l_number_cal_records).calendar_year,
5356   l_cal_records(l_number_cal_records).calendar_month,l_cal_records(l_number_cal_records).calendar_day'||newline;
5357   for i in 1..l_number_periods loop
5358     l_stmt:=l_stmt||',l_cal_records(l_number_cal_records).'||l_period_column(i);
5359   end loop;
5360   l_stmt:=l_stmt||';
5361   exit when c1%notfound;
5362   l_number_cal_records:=l_number_cal_records+1;
5363   l_cal_records.extend;
5364   end loop;
5365   l_number_cal_records:=l_number_cal_records-1;
5366   close c1;
5367   l_num_final:=0; '||newline;
5368   for i in 1..l_number_periods loop
5369     l_stmt:=l_stmt||'l_'||l_period_column(i)||'_period_count(1):=0;'||newline;
5370   end loop;
5371   l_stmt:=l_stmt||'for i in 1..l_number_cal_records loop '||newline;
5372   l_count:=0;
5373   /*
5374   because these periodicities rollup, we need to do all the calculations only for YTD.
5375   QTD, MTD etc are already a part of it and all we need is the correct pattern to get
5376   the XTD value
5377   */
5378   for i in 1..l_number_periods-1 loop
5379     --for each of : Q of YTD, M of YTD
5380     if l_count=0 then
5381       l_count:=l_count+1;
5382       l_stmt:=l_stmt||'if ';
5383     else
5384       l_stmt:=l_stmt||'elsif ';
5385     end if;
5386     l_stmt:=l_stmt||'l_prev_'||l_period_column(1)||'_'||l_period_column(i)||' is not null and '||
5387     'l_prev_'||l_period_column(1)||'_'||l_period_column(i)||'<>l_cal_records(i).'||l_period_column(i)||' then'||newline;
5388     --add the new periodicity to the contribution
5389     l_stmt:=l_stmt||'l_num_'||l_period_column(1)||'_'||l_period_column(i)||':='||
5390     'l_num_'||l_period_column(1)||'_'||l_period_column(i)||'+1;'||newline||
5391     'l_'||l_period_column(1)||'_'||l_period_column(i)||'('||'l_num_'||l_period_column(1)||'_'||l_period_column(i)||
5392     '):=l_prev_'||l_period_column(1)||'_'||l_period_column(i)||';'||newline;
5393     l_stmt:=l_stmt||'l_'||l_period_column(i)||'_period_count(l_num_'||l_period_column(1)||'_'||l_period_column(i)||
5394     '+1):=0;'||newline;
5395     for j in i+1..l_number_periods loop
5396       --if Q is not the same for YTD, reset M for YTD
5397       l_stmt:=l_stmt||'l_'||l_period_column(1)||'_'||l_period_column(j)||'.delete;'||newline;
5398       l_stmt:=l_stmt||'l_num_'||l_period_column(1)||'_'||l_period_column(j)||':=0;'||newline;
5399       l_stmt:=l_stmt||'l_'||l_period_column(j)||'_period_count.delete;'||newline;
5400       l_stmt:=l_stmt||'l_'||l_period_column(j)||'_period_count(1):=0;'||newline;
5401     end loop;
5402   end loop;----for each of the periodicity say in Y Q M D
5403   if l_count>0 then
5404     l_stmt:=l_stmt||'end if;'||newline;
5405   end if;
5406   --increment the day
5407   l_stmt:=l_stmt||'--add period day counts'||newline;
5408   for i in 1..l_number_periods-1 loop
5409     l_stmt:=l_stmt||'l_'||l_period_column(i)||'_period_count('||
5410     'l_num_'||l_period_column(1)||'_'||l_period_column(i)||'+1):='||
5411     'l_'||l_period_column(i)||'_period_count('||
5412     'l_num_'||l_period_column(1)||'_'||l_period_column(i)||'+1)+1;'||newline;
5413   end loop;
5414   --for day, day count is always 1
5418   l_stmt:=l_stmt||'l_num_'||l_period_column(1)||'_'||l_period_column(l_number_periods)||':='||
5415   l_stmt:=l_stmt||'l_'||l_period_column(l_number_periods)||'_period_count('||
5416     'l_num_'||l_period_column(1)||'_'||l_period_column(l_number_periods)||'+1):=1;'||newline;
5417   l_stmt:=l_stmt||'--------------------'||newline;
5419   'l_num_'||l_period_column(1)||'_'||l_period_column(l_number_periods)||'+1;'||newline||
5420   'l_'||l_period_column(1)||'_'||l_period_column(l_number_periods)||'('||
5421   'l_num_'||l_period_column(1)||'_'||l_period_column(l_number_periods)||'):=l_cal_records(i).'||
5422   l_period_column(l_number_periods)||';'||newline;
5423   l_stmt:=l_stmt||'--------------------'||newline;
5424   --add the contribution of each period to the XTD
5425   --i starts from 2. 1 is year. dont add year contribution.
5426   for i in 2..l_number_periods loop
5427     l_stmt:=l_stmt||'for j in 1..l_num_'||l_period_column(1)||'_'||l_period_column(i)||' loop '||newline||
5428     'l_num_final:=l_num_final+1;'||newline;
5429     l_stmt:=l_stmt||'l_final_report_date(l_num_final):=to_date('||
5430     'l_cal_records(i).calendar_month||''/''||l_cal_records(i).calendar_day||''/''||'||
5431     'l_cal_records(i).calendar_year,'''||
5432     'MM/DD/YYYY'');'||newline;
5433     l_stmt:=l_stmt||'l_final_period(l_num_final):=l_'||l_period_column(1)||'_'||l_period_column(i)||
5434     '(j);'||newline;
5435     l_stmt:=l_stmt||'l_final_year(l_num_final):=l_cal_records(i).year;'||newline;
5436     l_stmt:=l_stmt||'l_final_period_type_id(l_num_final):='||l_period_period_type(i)||';'||newline;
5437     l_stmt:=l_stmt||'l_final_periodicity_id(l_num_final):='||l_period_periodicity_id(i)||';'||newline;
5438     l_stmt:=l_stmt||'l_final_period_count(l_num_final):=l_'||l_period_column(i)||'_period_count(j);'||newline;
5439     if i=l_number_periods then
5440       l_stmt:=l_stmt||'if j=l_num_'||l_period_column(1)||'_'||l_period_column(i)||' then '||newline;
5441       l_stmt:=l_stmt||'  l_final_period_flag(l_num_final):=1;'||newline;
5442       l_stmt:=l_stmt||'else '||newline||' l_final_period_flag(l_num_final):=0;'||newline;
5443       l_stmt:=l_stmt||'end if;'||newline;
5444     else
5445       l_stmt:=l_stmt||'l_final_period_flag(l_num_final):=0;'||newline;
5446     end if;
5447     l_stmt:=l_stmt||'end loop;'||newline;
5448     l_stmt:=l_stmt||'---------'||newline;
5449   end loop;
5450   --set the prev periods
5451   for i in 1..l_number_periods loop
5452     l_stmt:=l_stmt||'l_prev_'||l_period_column(1)||'_'||l_period_column(i)||':='||
5453     'l_cal_records(i).'||l_period_column(i)||';'||newline;
5454   end loop;
5455   l_stmt:=l_stmt||'---========================'||newline;
5456   l_stmt:=l_stmt||'end loop;--across each cal day'||newline;
5457   --insert into the reporting calendar table
5458   l_stmt:=l_stmt||'-------------------------'||newline||'---Insert into Reporting Calendar Table'||newline;
5459   --'record_type_id,periodicity_id,period_flag,period_day_count,hierarchy,created_by,last_update_by,'||
5460   l_stmt:=l_stmt||'forall i in 1..l_num_final'||newline||
5461   'insert into BSC_REPORTING_CALENDAR(calendar_id,calendar_type,report_date,period,year,period_type_id,'||newline||
5462   'record_type_id,periodicity_id,hierarchy,day_count,rolling_flag,created_by,last_update_by,'||newline||
5463   'last_update_login,creation_date,'||
5464   'last_update_date) values ('||newline||
5465   p_calendar_id||',''BSC'',l_final_report_date(i),l_final_period(i),l_final_year(i),'||
5466   'l_final_period_type_id(i),l_final_period_type_id(i),l_final_periodicity_id(i),'||
5467   ''','||p_hierarchy||','',l_final_period_count(i),''N'','||
5468   '0,0,0,sysdate,sysdate);'||newline;
5469   l_stmt:=l_stmt||'END;'||newline;
5470   if g_debug then
5471     write_to_log_file_n(l_stmt);
5472     write_to_log_file_n('length='||length(l_stmt));
5473   end if;
5474   execute immediate l_stmt;
5475   if g_debug then
5476     write_to_log_file_n('Load Reporting Calendar for calendar '||p_calendar_id||' and Hierarchy '||p_hierarchy||
5477     get_time);
5478   end if;
5479   begin
5480     l_stmt:='begin ld_rpt_cal_'||randomString||';end;';
5481     if g_debug then
5482       write_to_log_file_n(l_stmt);
5483     end if;
5484     execute immediate l_stmt;
5485     if g_debug then
5486       write_to_log_file_n('Loaded '||get_time);
5487     end if;
5488     commit;
5489 
5490   exception when others then
5491     BSC_IM_UTILS.g_status_message:=sqlerrm;
5492     g_status_message:=sqlerrm;
5493     write_to_log_file_n('Exception in load_reporting_calendar '||sqlerrm);
5494     return false;
5495   end;
5496   --drop the procedure
5497   l_stmt:='drop procedure ld_rpt_cal_'||randomString;
5498   if g_debug then
5499     write_to_log_file_n(l_stmt);
5500   end if;
5501   execute immediate l_stmt;
5502   return true;
5503 Exception when others then
5504   BSC_IM_UTILS.g_status_message:=sqlerrm;
5505   g_status_message:=sqlerrm;
5506   write_to_log_file_n('Exception in load_reporting_calendar '||sqlerrm);
5507   return false;
5508 End;
5509 
5510 /*
5511 This is for the DBI style reporting calendar hier. here we have Y Q M W D
5512 */
5513 function load_reporting_calendar_DBI(
5514 p_calendar_id number,
5515 p_calendar_type varchar2,
5516 p_hierarchy varchar2,
5517 p_hierarchy_type varchar2,
5518 p_calendar_data cal_record_table,
5519 p_number_calendar_data number,
5520 p_periodicity_data cal_periodicity_table,
5521 p_number_periodicity_data number
5522 )return boolean is
5523 --------------------
5524 l_year_year BSC_IM_UTILS.number_tabletype;
5525 l_year_day_count BSC_IM_UTILS.number_tabletype;--number of days in year
5526 l_prev_year_year number;
5527 l_num_year_year number:=0;
5528 l_year_year_PTD number;
5529 l_year_year_PID number;
5530 l_year_year_RTD number;
5531 --
5532 l_year_quarter BSC_IM_UTILS.number_tabletype;
5533 l_quarter_day_count BSC_IM_UTILS.number_tabletype;
5534 l_prev_year_quarter number;
5535 l_num_year_quarter number:=0;
5536 l_year_quarter_PTD number;
5540 l_year_month BSC_IM_UTILS.number_tabletype;
5537 l_year_quarter_PID number;
5538 l_year_quarter_RTD number;
5539 --
5541 l_month_day_count BSC_IM_UTILS.number_tabletype;
5542 l_prev_year_month number;
5543 l_num_year_month number:=0;
5544 l_year_month_PTD number;
5545 l_year_month_PID number;
5546 l_year_month_RTD number;
5547 --
5548 l_year_week BSC_IM_UTILS.number_tabletype;
5549 l_week_day_count BSC_IM_UTILS.number_tabletype;
5550 l_prev_year_week number;
5551 l_num_year_week number:=0;
5552 l_year_week_PTD number;
5553 l_year_week_PID number;
5554 l_year_week_RTD number;
5555 --
5556 l_year_day BSC_IM_UTILS.number_tabletype;
5557 l_prev_year_day number;
5558 l_num_year_day number:=0;
5559 l_year_day_PTD number;
5560 l_year_day_PID number;
5561 l_year_day_RTD number;
5562 --
5563 --for week to date
5564 l_week_day BSC_IM_UTILS.number_tabletype;
5565 l_prev_week_day number;
5566 l_num_week_day number:=0;
5567 l_week_day_PTD number;
5568 l_week_day_PID number;
5569 l_week_day_RTD number;
5570 -----
5571 l_month_day BSC_IM_UTILS.number_tabletype; --carries record type id of 4 i.e mtd only
5572 l_num_month_day number:=0;
5573 -----
5574 l_final_report_date BSC_IM_UTILS.date_tabletype;
5575 l_final_period BSC_IM_UTILS.number_tabletype;
5576 l_final_year BSC_IM_UTILS.number_tabletype;
5577 l_final_period_type_id BSC_IM_UTILS.number_tabletype;
5578 l_final_periodicity_id BSC_IM_UTILS.number_tabletype;
5579 l_final_record_type_id BSC_IM_UTILS.number_tabletype;
5580 l_final_day_count BSC_IM_UTILS.number_tabletype;
5581 l_final_period_flag BSC_IM_UTILS.number_tabletype;
5582 l_num_final number:=0;
5583 --
5584 Begin
5585   if g_debug then
5586     write_to_log_file_n('In load_reporting_calendar_DBI, p_calendar_id='||p_calendar_id||','||
5587     'p_calendar_type='||p_calendar_type||',p_hierarchy='||p_hierarchy||',p_hierarchy_type='||p_hierarchy_type||
5588     get_time);
5589   end if;
5590   --set the PTD and RTD
5591   for i in 1..p_number_periodicity_data loop
5592     if p_periodicity_data(i).periodicity_type=1 then
5593       l_year_year_PTD:=p_periodicity_data(i).period_type_id;
5594       l_year_year_PID:=p_periodicity_data(i).periodicity_id;
5595       l_year_year_RTD:=p_periodicity_data(i).record_type_id;
5596     elsif p_periodicity_data(i).periodicity_type=3 then
5597       l_year_quarter_PTD:=p_periodicity_data(i).period_type_id;
5598       l_year_quarter_PID:=p_periodicity_data(i).periodicity_id;
5599       l_year_quarter_RTD:=p_periodicity_data(i).record_type_id;
5600     elsif p_periodicity_data(i).periodicity_type=5 then
5601       l_year_month_PTD:=p_periodicity_data(i).period_type_id;
5602       l_year_month_PID:=p_periodicity_data(i).periodicity_id;
5603       l_year_month_RTD:=p_periodicity_data(i).record_type_id;
5604     elsif p_periodicity_data(i).periodicity_type=7 then
5605       l_year_week_PTD:=p_periodicity_data(i).period_type_id;
5606       l_year_week_PID:=p_periodicity_data(i).periodicity_id;
5607       l_year_week_RTD:=p_periodicity_data(i).record_type_id;
5608     elsif p_periodicity_data(i).periodicity_type=9 then
5609       l_year_day_PTD:=p_periodicity_data(i).period_type_id;
5610       l_year_day_PID:=p_periodicity_data(i).periodicity_id;
5611       l_year_day_RTD:=p_periodicity_data(i).record_type_id;
5612     end if;
5613   end loop;
5614   l_year_day_count(1):=0;
5615   l_quarter_day_count(1):=0;
5616   l_month_day_count(1):=0;
5617   l_week_day_count(1):=0;
5618   for i in 1..p_number_calendar_data loop
5619     if l_prev_year_year is not null and l_prev_year_year<>p_calendar_data(i).year then
5620       l_num_year_year:=l_num_year_year+1;
5621       l_year_year(l_num_year_year):=l_prev_year_year;
5622       l_year_day_count(l_num_year_year+1):=0;
5623       l_year_quarter.delete;
5624       l_quarter_day_count.delete;
5625       l_quarter_day_count(1):=0;
5626       l_num_year_quarter:=0;
5627       l_year_month.delete;
5628       l_month_day_count.delete;
5629       l_month_day_count(1):=0;
5630       l_num_year_month:=0;
5631       l_year_week.delete;
5632       l_week_day_count.delete;
5633       l_week_day_count(1):=0;
5634       l_num_year_week:=0;
5635       l_year_day.delete;
5636       l_num_year_day:=0;
5637       l_month_day.delete;
5638       l_num_month_day:=0;
5639     elsif l_prev_year_quarter is not null and l_prev_year_quarter<>p_calendar_data(i).quarter then
5640       l_num_year_quarter:=l_num_year_quarter+1;
5641       l_year_quarter(l_num_year_quarter):=l_prev_year_quarter;
5642       l_quarter_day_count(l_num_year_quarter+1):=0;
5643       l_year_month.delete;
5644       l_month_day_count.delete;
5645       l_month_day_count(1):=0;
5646       l_num_year_month:=0;
5647       l_year_week.delete;
5648       l_week_day_count.delete;
5649       l_week_day_count(1):=0;
5650       l_num_year_week:=0;
5651       l_year_day.delete;
5652       l_num_year_day:=0;
5653       l_month_day.delete;
5654       l_num_month_day:=0;
5655     elsif l_prev_year_month is not null and l_prev_year_month<>p_calendar_data(i).month then
5656       l_num_year_month:=l_num_year_month+1;
5657       l_year_month(l_num_year_month):=l_prev_year_month;
5658       l_month_day_count(l_num_year_month+1):=0;
5659       l_year_week.delete;
5660       l_week_day_count.delete;
5661       l_week_day_count(1):=0;
5662       l_num_year_week:=0;
5663       l_year_day.delete;
5664       l_num_year_day:=0;
5665       l_month_day.delete;
5666       l_num_month_day:=0;
5667     elsif l_prev_year_week is not null and l_prev_year_week<>p_calendar_data(i).week52 then
5668       --when week changes we dont reset l_year_day
5669       --we can include this week's contribution only if this week has all 7 days in it.
5670       if l_num_year_day>=7 then
5671         l_num_year_week:=l_num_year_week+1;
5672         l_year_week(l_num_year_week):=l_prev_year_week;
5676       else
5673         l_week_day_count(l_num_year_week+1):=0;
5674         l_year_day.delete;
5675         l_num_year_day:=0;
5677         l_num_month_day:=l_num_year_day;--these will carry record type of 4
5678         l_month_day:=l_year_day;
5679         l_year_day.delete;
5680         l_num_year_day:=0;
5681       end if;
5682     end if;
5683     if l_prev_year_week is not null and l_prev_year_week<>p_calendar_data(i).week52 then
5684       l_week_day.delete;
5685       l_num_week_day:=0;
5686       l_week_day_count(l_num_year_week+1):=0;
5687     end if;
5688     --load the previous years. this will actually work for all hierarchies
5689     for j in 1..l_num_year_year loop
5690       l_num_final:=l_num_final+1;
5691       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5692       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5693       l_final_period(l_num_final):=l_year_year(j);
5694       l_final_year(l_num_final):=l_year_year(j);
5695       l_final_period_type_id(l_num_final):=l_year_year_PTD;
5696       l_final_periodicity_id(l_num_final):=l_year_year_PID;
5697       l_final_record_type_id(l_num_final):=l_year_year_RTD;
5698       l_final_period_flag(l_num_final):=0;
5699       l_final_day_count(l_num_final):=l_year_day_count(j);
5700     end loop;
5701     for j in 1..l_num_year_quarter loop
5702       l_num_final:=l_num_final+1;
5703       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5704       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5705       l_final_period(l_num_final):=l_year_quarter(j);
5706       l_final_year(l_num_final):=p_calendar_data(i).year;
5707       l_final_period_type_id(l_num_final):=l_year_quarter_PTD;
5708       l_final_periodicity_id(l_num_final):=l_year_quarter_PID;
5709       l_final_record_type_id(l_num_final):=l_year_quarter_RTD;
5710       l_final_period_flag(l_num_final):=0;
5711       l_final_day_count(l_num_final):=l_quarter_day_count(j);
5712     end loop;
5713     for j in 1..l_num_year_month loop
5714       l_num_final:=l_num_final+1;
5715       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5716       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5717       l_final_period(l_num_final):=l_year_month(j);
5718       l_final_year(l_num_final):=p_calendar_data(i).year;
5719       l_final_period_type_id(l_num_final):=l_year_month_PTD;
5720       l_final_periodicity_id(l_num_final):=l_year_month_PID;
5721       l_final_record_type_id(l_num_final):=l_year_month_RTD;
5722       l_final_period_flag(l_num_final):=0;
5723       l_final_day_count(l_num_final):=l_month_day_count(j);
5724     end loop;
5725     for j in 1..l_num_year_week loop
5726       l_num_final:=l_num_final+1;
5727       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5728       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5729       l_final_period(l_num_final):=l_year_week(j);
5730       l_final_year(l_num_final):=p_calendar_data(i).year;
5731       l_final_period_type_id(l_num_final):=l_year_week_PTD;
5732       l_final_periodicity_id(l_num_final):=l_year_week_PID;
5733       l_final_record_type_id(l_num_final):=l_year_week_RTD;
5734       l_final_period_flag(l_num_final):=0;
5735       l_final_day_count(l_num_final):=l_week_day_count(j);
5736     end loop;
5737     /*
5738     29 M
5739     30 Tue
5740     31 Wed
5741     1 Th
5742     2 Fri
5743     3 Sat
5744     4 Sun
5745     5 Mon
5746     6 Tue
5747     in this case, when we are at 1, Th, 29,30,31,1 will contribute to wtd, 1 will contribute to wtd and mtd.
5748     when we are at 6, tue, 6 will contribute to wtd while 1,2,3,4,5 and 6 will contribute to mtd
5749     */
5750     for j in 1..l_num_year_day loop
5751       l_num_final:=l_num_final+1;
5752       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5753       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5754       l_final_period(l_num_final):=l_year_day(j);
5755       l_final_year(l_num_final):=p_calendar_data(i).year;
5756       l_final_period_type_id(l_num_final):=l_year_day_PTD;
5757       l_final_periodicity_id(l_num_final):=l_year_day_PID;
5758       l_final_record_type_id(l_num_final):=2;
5759       l_final_period_flag(l_num_final):=0;
5760       l_final_day_count(l_num_final):=1;
5761     end loop;
5762     --now the week and month contributions
5763     for j in 1..l_num_week_day loop
5764       if BSC_IM_UTILS.in_array(l_year_day,l_num_year_day,l_week_day(j))=false then
5765         l_num_final:=l_num_final+1;
5766         l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5767         p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5768         l_final_period(l_num_final):=l_week_day(j);
5769         l_final_year(l_num_final):=p_calendar_data(i).year;
5770         l_final_period_type_id(l_num_final):=l_year_day_PTD;
5771         l_final_periodicity_id(l_num_final):=l_year_day_PID;
5772         l_final_record_type_id(l_num_final):=8;--only contribute to WTD
5773         l_final_period_flag(l_num_final):=0;
5774         l_final_day_count(l_num_final):=1;
5775       end if;
5776     end loop;
5777     for j in 1..l_num_month_day loop
5778       l_num_final:=l_num_final+1;
5779       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5780       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5781       l_final_period(l_num_final):=l_month_day(j);
5782       l_final_year(l_num_final):=p_calendar_data(i).year;
5783       l_final_period_type_id(l_num_final):=l_year_day_PTD;
5784       l_final_periodicity_id(l_num_final):=l_year_day_PID;
5785       l_final_record_type_id(l_num_final):=4;--only contribute to MTD
5786       l_final_period_flag(l_num_final):=0;
5790     l_year_day_count(l_num_year_year+1):=l_year_day_count(l_num_year_year+1)+1;
5787       l_final_day_count(l_num_final):=1;
5788     end loop;
5789     --add the day's contribution to year, qtr etc
5791     l_quarter_day_count(l_num_year_quarter+1):=l_quarter_day_count(l_num_year_quarter+1)+1;
5792     l_month_day_count(l_num_year_month+1):=l_month_day_count(l_num_year_month+1)+1;
5793     l_week_day_count(l_num_year_week+1):=l_week_day_count(l_num_year_week+1)+1;
5794     -----
5795     --add this day
5796     l_num_year_day:=l_num_year_day+1;
5797     l_year_day(l_num_year_day):=p_calendar_data(i).day365;
5798     ------
5799     l_num_final:=l_num_final+1;
5800     l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
5801     p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
5802     l_final_period(l_num_final):=l_year_day(l_num_year_day);
5803     l_final_year(l_num_final):=p_calendar_data(i).year;
5804     l_final_period_type_id(l_num_final):=l_year_day_PTD;
5805     l_final_periodicity_id(l_num_final):=l_year_day_PID;
5806     l_final_record_type_id(l_num_final):=l_year_day_RTD;
5807     l_final_period_flag(l_num_final):=1;
5808     l_final_day_count(l_num_final):=1;
5809     ---------------------
5810     l_prev_year_year:=p_calendar_data(i).year;
5811     l_prev_year_quarter:=p_calendar_data(i).quarter;
5812     l_prev_year_month:=p_calendar_data(i).month;
5813     l_prev_year_week:=p_calendar_data(i).week52;
5814     l_prev_year_day:=p_calendar_data(i).day365;
5815     --
5816     --set the wtd parameters
5817     l_num_week_day:=l_num_week_day+1;
5818     l_week_day(l_num_week_day):=p_calendar_data(i).day365;
5819     --insert into the reporting calendar
5820   end loop;
5821   --dynamic sql to be compatible with 5.0.2
5822   --we need to make compatible with 8i.so have to have static sql. no need to
5823   --worry about 5.0.2
5824   forall i in 1..l_num_final
5825     execute immediate
5826     'insert into BSC_REPORTING_CALENDAR(calendar_id,calendar_type,report_date,
5827     period,year,period_type_id,record_type_id,periodicity_id,hierarchy,rolling_flag,day_count,
5828     created_by,last_update_by,last_update_login,creation_date,last_update_date) values(
5829     :1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16)'
5830     using  p_calendar_id,'BSC',l_final_report_date(i),l_final_period(i),l_final_year(i),
5831     l_final_period_type_id(i),l_final_record_type_id(i),l_final_periodicity_id(i),
5832     ','||p_hierarchy||',','N',l_final_day_count(i),0,0,0,sysdate,sysdate;
5833   commit;
5834 
5835   return true;
5836 Exception when others then
5837   BSC_IM_UTILS.g_status_message:=sqlerrm;
5838   g_status_message:=sqlerrm;
5839   write_to_log_file_n('Exception in load_reporting_calendar_DBI '||sqlerrm);
5840   return false;
5841 End;
5842 
5843 /*
5844 This for supporting rolling XTD
5845 Logic:
5846 */
5847 function load_rpt_cal_DBI_rolling(
5848 p_calendar_id number,
5849 p_calendar_type varchar2,
5850 p_hierarchy varchar2,
5851 p_hierarchy_type varchar2,
5852 p_calendar_data cal_record_table,
5853 p_number_calendar_data number,
5854 p_periodicity_data cal_periodicity_table,
5855 p_number_periodicity_data number
5856 )return boolean is
5857 --------------------
5858 l_rolling_flag varchar2(40);
5859 --------------------
5860 l_year_year BSC_IM_UTILS.number_tabletype;
5861 l_year_day_count BSC_IM_UTILS.number_tabletype;--number of days in year
5862 l_prev_year_year number;
5863 l_num_year_year number:=0;
5864 l_year_year_PTD number;
5865 l_year_year_PID number;
5866 l_year_year_RTD number;
5867 --
5868 l_year_quarter BSC_IM_UTILS.number_tabletype;
5869 l_quarter_day_count BSC_IM_UTILS.number_tabletype;
5870 l_prev_year_quarter number;
5871 l_num_year_quarter number:=0;
5872 l_year_quarter_PTD number;
5873 l_year_quarter_PID number;
5874 l_year_quarter_RTD number;
5875 --
5876 l_year_month BSC_IM_UTILS.number_tabletype;
5877 l_month_day_count BSC_IM_UTILS.number_tabletype;
5878 l_prev_year_month number;
5879 l_num_year_month number:=0;
5880 l_year_month_PTD number;
5881 l_year_month_PID number;
5882 l_year_month_RTD number;
5883 --
5884 l_year_week BSC_IM_UTILS.number_tabletype;
5885 l_week_day_count BSC_IM_UTILS.number_tabletype;
5886 l_prev_year_week number;
5887 l_num_year_week number:=0;
5888 l_year_week_PTD number;
5889 l_year_week_PID number;
5890 l_year_week_RTD number;
5891 --
5892 l_year_day BSC_IM_UTILS.number_tabletype;
5893 l_prev_year_day number;
5894 l_num_year_day number:=0;
5895 l_year_day_PTD number;
5896 l_year_day_PID number;
5897 l_year_day_RTD number;
5898 --
5899 --for week to date
5900 l_week_day BSC_IM_UTILS.number_tabletype;
5901 l_prev_week_day number;
5902 l_num_week_day number:=0;
5903 l_week_day_PTD number;
5904 l_week_day_PID number;
5905 l_week_day_RTD number;
5906 -----
5907 l_month_day BSC_IM_UTILS.number_tabletype; --carries record type id of 4 i.e mtd only
5908 l_num_month_day number:=0;
5909 -----
5910 l_final_report_date BSC_IM_UTILS.date_tabletype;
5911 l_final_period BSC_IM_UTILS.number_tabletype;
5912 l_final_year BSC_IM_UTILS.number_tabletype;
5913 l_final_period_type_id BSC_IM_UTILS.number_tabletype;
5914 l_final_periodicity_id BSC_IM_UTILS.number_tabletype;
5915 l_final_record_type_id BSC_IM_UTILS.number_tabletype;
5916 l_final_day_count BSC_IM_UTILS.number_tabletype;
5917 l_final_period_flag BSC_IM_UTILS.number_tabletype;
5918 l_num_final number:=0;
5919 --
5920 Begin
5921   if g_debug then
5922     write_to_log_file_n('In load_rpt_cal_DBI_rolling, p_calendar_id='||p_calendar_id||','||
5923     'p_calendar_type='||p_calendar_type||',p_hierarchy='||p_hierarchy||',p_hierarchy_type='||p_hierarchy_type||
5924     get_time);
5925   end if;
5926   l_rolling_flag:='Y';
5927   --set the PTD and RTD
5931       l_year_year_PID:=p_periodicity_data(i).periodicity_id;
5928   for i in 1..p_number_periodicity_data loop
5929     if p_periodicity_data(i).periodicity_type=1 then
5930       l_year_year_PTD:=p_periodicity_data(i).period_type_id;
5932       l_year_year_RTD:=p_periodicity_data(i).record_type_id;
5933     elsif p_periodicity_data(i).periodicity_type=3 then
5934       l_year_quarter_PTD:=p_periodicity_data(i).period_type_id;
5935       l_year_quarter_PID:=p_periodicity_data(i).periodicity_id;
5936       l_year_quarter_RTD:=p_periodicity_data(i).record_type_id;
5937     elsif p_periodicity_data(i).periodicity_type=5 then
5938       l_year_month_PTD:=p_periodicity_data(i).period_type_id;
5939       l_year_month_PID:=p_periodicity_data(i).periodicity_id;
5940       l_year_month_RTD:=p_periodicity_data(i).record_type_id;
5941     elsif p_periodicity_data(i).periodicity_type=7 then
5942       l_year_week_PTD:=p_periodicity_data(i).period_type_id;
5943       l_year_week_PID:=p_periodicity_data(i).periodicity_id;
5944       l_year_week_RTD:=p_periodicity_data(i).record_type_id;
5945     elsif p_periodicity_data(i).periodicity_type=9 then
5946       l_year_day_PTD:=p_periodicity_data(i).period_type_id;
5947       l_year_day_PID:=p_periodicity_data(i).periodicity_id;
5948       l_year_day_RTD:=p_periodicity_data(i).record_type_id;
5949     end if;
5950   end loop;
5951   l_year_day_count(1):=0;
5952   l_quarter_day_count(1):=0;
5953   l_month_day_count(1):=0;
5954   l_week_day_count(1):=0;
5955   --start from the last cal day and go to the first
5956   for i in reverse 1..p_number_calendar_data loop
5957     if l_prev_year_year is not null and l_prev_year_year<>p_calendar_data(i).year then
5958       l_num_year_year:=l_num_year_year+1;
5959       l_year_year(l_num_year_year):=l_prev_year_year;
5960       l_year_day_count(l_num_year_year+1):=0;
5961       l_year_quarter.delete;
5962       l_quarter_day_count.delete;
5963       l_quarter_day_count(1):=0;
5964       l_num_year_quarter:=0;
5965       l_year_month.delete;
5966       l_month_day_count.delete;
5967       l_month_day_count(1):=0;
5968       l_num_year_month:=0;
5969       l_year_week.delete;
5970       l_week_day_count.delete;
5971       l_week_day_count(1):=0;
5972       l_num_year_week:=0;
5973       l_year_day.delete;
5974       l_num_year_day:=0;
5975       l_month_day.delete;
5976       l_num_month_day:=0;
5977     elsif l_prev_year_quarter is not null and l_prev_year_quarter<>p_calendar_data(i).quarter then
5978       l_num_year_quarter:=l_num_year_quarter+1;
5979       l_year_quarter(l_num_year_quarter):=l_prev_year_quarter;
5980       l_quarter_day_count(l_num_year_quarter+1):=0;
5981       l_year_month.delete;
5982       l_month_day_count.delete;
5983       l_month_day_count(1):=0;
5984       l_num_year_month:=0;
5985       l_year_week.delete;
5986       l_week_day_count.delete;
5987       l_week_day_count(1):=0;
5988       l_num_year_week:=0;
5989       l_year_day.delete;
5990       l_num_year_day:=0;
5991       l_month_day.delete;
5992       l_num_month_day:=0;
5993     elsif l_prev_year_month is not null and l_prev_year_month<>p_calendar_data(i).month then
5994       l_num_year_month:=l_num_year_month+1;
5995       l_year_month(l_num_year_month):=l_prev_year_month;
5996       l_month_day_count(l_num_year_month+1):=0;
5997       l_year_week.delete;
5998       l_week_day_count.delete;
5999       l_week_day_count(1):=0;
6000       l_num_year_week:=0;
6001       l_year_day.delete;
6002       l_num_year_day:=0;
6003       l_month_day.delete;
6004       l_num_month_day:=0;
6005     elsif l_prev_year_week is not null and l_prev_year_week<>p_calendar_data(i).week52 then
6006       --when week changes we dont reset l_year_day
6007       --we can include this week's contribution only if this week has all 7 days in it.
6008       if l_num_year_day>=7 then
6009         l_num_year_week:=l_num_year_week+1;
6010         l_year_week(l_num_year_week):=l_prev_year_week;
6011         l_week_day_count(l_num_year_week+1):=0;
6012         l_year_day.delete;
6013         l_num_year_day:=0;
6014       else
6015         l_num_month_day:=l_num_year_day;--these will carry record type of 4
6016         l_month_day:=l_year_day;
6017         l_year_day.delete;
6018         l_num_year_day:=0;
6019       end if;
6020     end if;
6021     if l_prev_year_week is not null and l_prev_year_week<>p_calendar_data(i).week52 then
6022       l_week_day.delete;
6023       l_num_week_day:=0;
6024       l_week_day_count(l_num_year_week+1):=0;
6025     end if;
6026     for j in 1..l_num_year_quarter loop
6027       l_num_final:=l_num_final+1;
6028       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6029       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6030       l_final_period(l_num_final):=l_year_quarter(j);
6031       l_final_year(l_num_final):=p_calendar_data(i).year;
6032       l_final_period_type_id(l_num_final):=l_year_quarter_PTD;
6033       l_final_periodicity_id(l_num_final):=l_year_quarter_PID;
6034       l_final_record_type_id(l_num_final):=l_year_quarter_RTD;
6035       l_final_period_flag(l_num_final):=0;
6036       l_final_day_count(l_num_final):=l_quarter_day_count(j);
6037     end loop;
6038     for j in 1..l_num_year_month loop
6039       l_num_final:=l_num_final+1;
6040       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6041       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6042       l_final_period(l_num_final):=l_year_month(j);
6043       l_final_year(l_num_final):=p_calendar_data(i).year;
6044       l_final_period_type_id(l_num_final):=l_year_month_PTD;
6045       l_final_periodicity_id(l_num_final):=l_year_month_PID;
6046       l_final_record_type_id(l_num_final):=l_year_month_RTD;
6047       l_final_period_flag(l_num_final):=0;
6048       l_final_day_count(l_num_final):=l_month_day_count(j);
6049     end loop;
6053       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6050     for j in 1..l_num_year_week loop
6051       l_num_final:=l_num_final+1;
6052       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6054       l_final_period(l_num_final):=l_year_week(j);
6055       l_final_year(l_num_final):=p_calendar_data(i).year;
6056       l_final_period_type_id(l_num_final):=l_year_week_PTD;
6057       l_final_periodicity_id(l_num_final):=l_year_week_PID;
6058       l_final_record_type_id(l_num_final):=l_year_week_RTD;
6059       l_final_period_flag(l_num_final):=0;
6060       l_final_day_count(l_num_final):=l_week_day_count(j);
6061     end loop;
6062     /*
6063     29 M
6064     30 Tue
6065     31 Wed
6066     1 Th
6067     2 Fri
6068     3 Sat
6069     4 Sun
6070     5 Mon
6071     6 Tue
6072     in this case, when we are at 1, Th, 29,30,31,1 will contribute to wtd, 1 will contribute to wtd and mtd.
6073     when we are at 6, tue, 6 will contribute to wtd while 1,2,3,4,5 and 6 will contribute to mtd
6074     */
6075     for j in 1..l_num_year_day loop
6076       l_num_final:=l_num_final+1;
6077       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6078       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6079       l_final_period(l_num_final):=l_year_day(j);
6080       l_final_year(l_num_final):=p_calendar_data(i).year;
6081       l_final_period_type_id(l_num_final):=l_year_day_PTD;
6082       l_final_periodicity_id(l_num_final):=l_year_day_PID;
6083       l_final_record_type_id(l_num_final):=2;
6084       l_final_period_flag(l_num_final):=0;
6085       l_final_day_count(l_num_final):=1;
6086     end loop;
6087     --now the week and month contributions
6088     for j in 1..l_num_week_day loop
6089       if BSC_IM_UTILS.in_array(l_year_day,l_num_year_day,l_week_day(j))=false then
6090         l_num_final:=l_num_final+1;
6091         l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6092         p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6093         l_final_period(l_num_final):=l_week_day(j);
6094         l_final_year(l_num_final):=p_calendar_data(i).year;
6095         l_final_period_type_id(l_num_final):=l_year_day_PTD;
6096         l_final_periodicity_id(l_num_final):=l_year_day_PID;
6097         l_final_record_type_id(l_num_final):=8;--only contribute to WTD
6098         l_final_period_flag(l_num_final):=0;
6099         l_final_day_count(l_num_final):=1;
6100       end if;
6101     end loop;
6102     for j in 1..l_num_month_day loop
6103       l_num_final:=l_num_final+1;
6104       l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6105       p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6106       l_final_period(l_num_final):=l_month_day(j);
6107       l_final_year(l_num_final):=p_calendar_data(i).year;
6108       l_final_period_type_id(l_num_final):=l_year_day_PTD;
6109       l_final_periodicity_id(l_num_final):=l_year_day_PID;
6110       l_final_record_type_id(l_num_final):=4;--only contribute to MTD
6111       l_final_period_flag(l_num_final):=0;
6112       l_final_day_count(l_num_final):=1;
6113     end loop;
6114     --add the day's contribution to year, qtr etc
6115     l_year_day_count(l_num_year_year+1):=l_year_day_count(l_num_year_year+1)+1;
6116     l_quarter_day_count(l_num_year_quarter+1):=l_quarter_day_count(l_num_year_quarter+1)+1;
6117     l_month_day_count(l_num_year_month+1):=l_month_day_count(l_num_year_month+1)+1;
6118     l_week_day_count(l_num_year_week+1):=l_week_day_count(l_num_year_week+1)+1;
6119     -----
6120     --add this day
6121     l_num_year_day:=l_num_year_day+1;
6122     l_year_day(l_num_year_day):=p_calendar_data(i).day365;
6123     ------
6124     l_num_final:=l_num_final+1;
6125     l_final_report_date(l_num_final):=to_date(p_calendar_data(i).calendar_month||'/'||
6126     p_calendar_data(i).calendar_day||'/'||p_calendar_data(i).calendar_year,'MM/DD/YYYY');
6127     l_final_period(l_num_final):=l_year_day(l_num_year_day);
6128     l_final_year(l_num_final):=p_calendar_data(i).year;
6129     l_final_period_type_id(l_num_final):=l_year_day_PTD;
6130     l_final_periodicity_id(l_num_final):=l_year_day_PID;
6131     l_final_record_type_id(l_num_final):=l_year_day_RTD;
6132     l_final_period_flag(l_num_final):=1;
6133     l_final_day_count(l_num_final):=1;
6134     ---------------------
6135     l_prev_year_year:=p_calendar_data(i).year;
6136     l_prev_year_quarter:=p_calendar_data(i).quarter;
6137     l_prev_year_month:=p_calendar_data(i).month;
6138     l_prev_year_week:=p_calendar_data(i).week52;
6139     l_prev_year_day:=p_calendar_data(i).day365;
6140     --
6141     --set the wtd parameters
6142     l_num_week_day:=l_num_week_day+1;
6143     l_week_day(l_num_week_day):=p_calendar_data(i).day365;
6144     --insert into the reporting calendar
6145   end loop;
6146   --dynamic sql to be compatible with 5.0.2
6147   --we need to make compatible with 8i.so have to have static sql. no need to
6148   --worry about 5.0.2
6149   forall i in 1..l_num_final
6150     execute immediate
6151     'insert into BSC_REPORTING_CALENDAR(calendar_id,calendar_type,report_date,
6152     period,year,period_type_id,record_type_id,periodicity_id,hierarchy,rolling_flag,day_count,
6153     created_by,last_update_by,last_update_login,creation_date,last_update_date) values(
6154     :1, :2, :3, :4, :5, :6, :7, :8, :9, :10, :11, :12, :13, :14, :15, :16)'
6155     using
6156     p_calendar_id,'BSC',l_final_report_date(i),l_final_period(i),l_final_year(i),
6157     l_final_period_type_id(i),l_final_record_type_id(i),l_final_periodicity_id(i),
6158     ','||p_hierarchy||',',l_rolling_flag,l_final_day_count(i),0,0,0,sysdate,sysdate;
6159   commit;
6160 
6161   return true;
6162 Exception when others then
6163   BSC_IM_UTILS.g_status_message:=sqlerrm;
6164   g_status_message:=sqlerrm;
6168 
6165   write_to_log_file_n('Exception in load_rpt_cal_DBI_rolling '||sqlerrm);
6166   return false;
6167 End;
6169 function get_reporting_calendar_name return varchar2 is
6170 Begin
6171   return upper('bsc_db_report_struct');
6172 Exception when others then
6173   BSC_IM_UTILS.g_status_message:=sqlerrm;
6174   g_status_message:=sqlerrm;
6175   write_to_log_file_n('Exception in get_reporting_calendar_name '||sqlerrm);
6176   return null;
6177 End;
6178 
6179 function get_dim_level_cols(
6180 p_level varchar2,
6181 p_columns out nocopy BSC_IM_UTILS.varchar_tabletype,
6182 p_column_type out nocopy BSC_IM_UTILS.varchar_tabletype,
6183 p_number_columns out nocopy number
6184 )return boolean is
6185 l_stmt varchar2(5000);
6186 ---------------------------------------------
6187 cursor c1 (p_level_table_name varchar2)
6188 is select column_name,column_type
6189 from bsc_sys_dim_level_cols ,bsc_sys_dim_levels_b
6190 where level_table_name=p_level_table_name
6191 and bsc_sys_dim_level_cols.dim_level_id=bsc_sys_dim_levels_b.dim_level_id;
6192 ---------------------------------------------
6193 Begin
6194   if g_debug then
6195     l_stmt:='select column_name,column_type '||
6196     'from bsc_sys_dim_level_cols ,bsc_sys_dim_levels_b '||
6197     'where level_table_name=:1 '||
6198     'and bsc_sys_dim_level_cols.dim_level_id=bsc_sys_dim_levels_b.dim_level_id ';
6199     write_to_log_file_n(l_stmt||' '||p_level);
6200   end if;
6201   p_number_columns:=1;
6202   open c1(p_level);
6203   loop
6204     fetch c1 into p_columns(p_number_columns),p_column_type(p_number_columns);
6205     exit when c1%notfound;
6206     p_number_columns:=p_number_columns+1;
6207   end loop;
6208   p_number_columns:=p_number_columns-1;
6209   close c1;
6210   if g_debug then
6211     write_to_log_file_n('Result');
6212     for i in 1..p_number_columns loop
6213       write_to_log_file(p_columns(i)||' '||p_column_type(i));
6214     end loop;
6215   end if;
6216   return true;
6217 Exception when others then
6218   BSC_IM_UTILS.g_status_message:=sqlerrm;
6219   g_status_message:=sqlerrm;
6220   write_to_log_file_n('Exception in get_dim_level_cols '||sqlerrm);
6221   return false;
6222 End;
6223 
6224 function get_s_tables_for_mv(
6225 p_mv varchar2,
6226 p_s_tables out nocopy BSC_IM_UTILS.varchar_tabletype,
6227 p_number_s_tables out nocopy number
6228 )return boolean is
6229 l_stmt varchar2(5000);
6230 TYPE CurTyp IS REF CURSOR;
6231 cv   CurTyp;
6232 -----------------------------------
6233 --Fix bug#3899842: use distinct
6234 --cursor c1 (p_mv_name varchar2)
6235 --is select distinct table_name from bsc_kpi_data_tables where mv_name=p_mv_name;
6236 cursor c1 (p_mv_name_pattern varchar2)
6237 is select distinct table_name from bsc_db_tables_rels
6238 where substr(table_name, 1, length(p_mv_name_pattern))= p_mv_name_pattern;
6239 -----------------------------------
6240 l_pattern varchar2(100);
6241 Begin
6242   p_number_s_tables:=1;
6243   if g_debug then
6244     l_stmt:='select table_name from bsc_kpi_data_tables where mv_name=:1';
6245     write_to_log_file_n(l_stmt||' using '||p_mv);
6246   end if;
6247   --open c1(p_mv);
6248   l_pattern := substr(p_mv, 1, instr(p_mv, '_', -1, 1));
6249   open c1(l_pattern);
6250   loop
6251     fetch c1 into p_s_tables(p_number_s_tables);
6252     exit when c1%notfound;
6253     p_number_s_tables:=p_number_s_tables+1;
6254   end loop;
6255   p_number_s_tables:=p_number_s_tables-1;
6256   close c1;
6257   if g_debug then
6258     write_to_log_file_n('Results');
6259     for i in 1..p_number_s_tables loop
6260       write_to_log_file(p_s_tables(i));
6261     end loop;
6262   end if;
6263   if p_number_s_tables=0 then
6264     p_number_s_tables:=1;
6265     l_stmt:='select table_name from bsc_db_tables where table_name like substr(:1,1,
6266     length(:2)-3)||''%''';
6267     if g_debug then
6268       write_to_log_file_n(l_stmt||' using '||p_mv||','||p_mv);
6269     end if;
6270     open cv for l_stmt using p_mv,p_mv;
6271     loop
6272       fetch cv into p_s_tables(p_number_s_tables);
6273       exit when cv%notfound;
6274       p_number_s_tables:=p_number_s_tables+1;
6275     end loop;
6276     p_number_s_tables:=p_number_s_tables-1;
6277     close cv;
6278     if g_debug then
6279       write_to_log_file_n('Results');
6280       for i in 1..p_number_s_tables loop
6281         write_to_log_file(p_s_tables(i));
6282       end loop;
6283     end if;
6284   end if;
6285   return true;
6286 Exception when others then
6287   BSC_IM_UTILS.g_status_message:=sqlerrm;
6288   g_status_message:=sqlerrm;
6289   write_to_log_file_n('Exception in get_s_tables_for_mv '||sqlerrm);
6290   return false;
6291 End;
6292 
6293 --given the table name, get the short name of the level
6294 function get_level_short_name(p_table_name varchar2) return varchar2 is
6295 --
6296 cursor c1(p_table varchar2) is select short_name from bsc_sys_dim_levels_b where level_table_name=p_table;
6297 --
6298 l_name varchar2(200);
6299 Begin
6300   if g_debug then
6301     write_to_log_file_n('In get_level_short_name->select short_name from bsc_sys_dim_levels_b where level_table_name='||
6302     p_table_name);
6303   end if;
6304   open c1(p_table_name);
6305   fetch c1 into l_name;
6306   close c1;
6307   return l_name;
6308 Exception when others then
6309   BSC_IM_UTILS.g_status_message:=sqlerrm;
6310   g_status_message:=sqlerrm;
6311   write_to_log_file_n('Exception in get_level_short_name '||sqlerrm);
6312   return null;
6313 End;
6314 
6315 function create_dbi_dim_tables return boolean is
6316 l_error_message varchar2(4000);
6317 Begin
6318   if g_debug then
6319     write_to_log_file_n('In create_dbi_dim_tables');
6320   end if;
6324     end if;
6321   if g_create_dbi_dim_tables is null or g_create_dbi_dim_tables=false then
6322     if g_debug then
6323       write_to_log_file_n('Going to call bsc_update_dim.Create_Dbi_Dim_Tables');
6325     if bsc_update_dim.Create_Dbi_Dim_Tables(l_error_message)=false then
6326       BSC_IM_UTILS.g_status_message:=l_error_message;
6327       g_status_message:=l_error_message;
6328       write_to_log_file_n('Error bsc_update_dim.Create_Dbi_Dim_Tables '||l_error_message);
6329       return false;
6330     end if;
6331     g_create_dbi_dim_tables:=true; --create these tables only once
6332   end if;
6333   return true;
6334 Exception when others then
6335   BSC_IM_UTILS.g_status_message:=sqlerrm;
6336   g_status_message:=sqlerrm;
6337   write_to_log_file_n('Exception in create_dbi_dim_tables '||sqlerrm);
6338   return false;
6339 End;
6340 
6341 /*
6342       For PMV
6343 */
6344 procedure get_list_of_rec_dim(
6345 p_dim_list out nocopy bsc_varchar2_table_type,
6346 p_num_dim_list out nocopy number,
6347 p_error_message out nocopy varchar2) is
6348 --
6349 l_dbi_dim_data BSC_UPDATE_DIM.t_array_dbi_dim_data;
6350 l_num_dbi_dim_data number;
6351 --
6352 Begin
6353   --p_dim_list.delete;
6354   p_num_dim_list:=0;
6355   BSC_UPDATE_DIM.Get_All_Dbi_Dim_Data(l_dbi_dim_data);
6356   l_num_dbi_dim_data:=l_dbi_dim_data.count;
6357   p_dim_list:=bsc_varchar2_table_type();
6358   for i in 1..l_num_dbi_dim_data loop
6359     if l_dbi_dim_data(i).top_n_levels is not null and l_dbi_dim_data(i).top_n_levels>0 then
6360       p_num_dim_list:=p_num_dim_list+1;
6361       p_dim_list.extend;
6362       p_dim_list(p_num_dim_list):=l_dbi_dim_data(i).short_name;
6363     end if;
6364   end loop;
6365 Exception when others then
6366   p_error_message:=sqlerrm;
6367   write_to_log_file_n('Exception in get_list_of_rec_dim '||sqlerrm);
6368   raise;
6369 End;
6370 
6371 --internal API
6372 procedure get_list_of_rec_dim(
6373 p_dim_list out nocopy BSC_UPDATE_DIM.t_array_dbi_dim_data
6374 ) is
6375 --
6376 l_dbi_dim_data BSC_UPDATE_DIM.t_array_dbi_dim_data;
6377 l_num_dbi_dim_data number;
6378 l_count number;
6379 --
6380 Begin
6381   p_dim_list.delete;
6382   BSC_UPDATE_DIM.Get_All_Dbi_Dim_Data(l_dbi_dim_data);
6383   l_num_dbi_dim_data:=l_dbi_dim_data.count;
6384   l_count:=0;
6385   for i in 1..l_num_dbi_dim_data loop
6386     if l_dbi_dim_data(i).top_n_levels is not null and l_dbi_dim_data(i).top_n_levels>0 then
6387       l_count:=l_count+1;
6388       p_dim_list(l_count):=l_dbi_dim_data(i);
6389     end if;
6390   end loop;
6391 Exception when others then
6392   write_to_log_file_n('Exception in get_list_of_rec_dim '||sqlerrm);
6393   raise;
6394 End;
6395 
6396 procedure set_and_get_dim_sql(
6397 p_dim_level_short_name bsc_varchar2_table_type,
6398 p_dim_level_value bsc_varchar2_table_type,
6399 p_num_dim_level number,
6400 p_dim_level_sql out nocopy bsc_varchar2_table_type,
6401 p_error_message out nocopy varchar2
6402 ) is
6403 --
6404 l_num_dim_level number;
6405 --
6406 l_stmt varchar2(2000);
6407 TYPE CurTyp IS REF CURSOR;
6408 cv   CurTyp;
6409 l_level varchar2(40);
6410 --
6411 Begin
6412   if g_rec_dbi_dim.count=0 then --cache once
6413     get_list_of_rec_dim(g_rec_dbi_dim);
6414   end if;
6415   l_num_dim_level:=p_num_dim_level;
6416   p_dim_level_sql:=bsc_varchar2_table_type();
6417   if l_num_dim_level>0 then
6418     for i in 1..l_num_dim_level loop
6419       p_dim_level_sql.extend;
6420       p_dim_level_sql(i):=null;
6421       for j in 1..g_rec_dbi_dim.count loop
6422         if p_dim_level_short_name(i)=g_rec_dbi_dim(j).short_name then
6423           --this is a rec dim
6424           --see if the value is in the denorm table
6425           l_level:=null;
6426           if p_dim_level_value(i)='0' then
6427             --if zeco code, we first see if the top levels are materialized. if yes, we return the sql to
6428             --hit all the top levels
6429             --PMV will bind and put the brackets
6430             if g_rec_dbi_dim(j).top_n_levels_in_mv>0 then
6431               p_dim_level_sql(i):='select distinct '||g_rec_dbi_dim(j).parent_col||' from '||g_rec_dbi_dim(j).denorm_table||' where '||
6432               g_rec_dbi_dim(j).parent_level_col||'=1';
6433               exit;
6434             end if;
6435           else
6436             l_stmt:='select '||g_rec_dbi_dim(j).parent_level_col||' from '||g_rec_dbi_dim(j).denorm_table||
6437             ' where '||g_rec_dbi_dim(j).parent_col||'=:1 and rownum=1';
6438             open cv for l_stmt using p_dim_level_value(i);
6439             fetch cv into l_level;
6440             close cv;
6441             if to_number(l_level)>g_rec_dbi_dim(j).top_n_levels_in_mv then
6442               --value not in MV
6443               p_dim_level_sql(i):='select '||g_rec_dbi_dim(j).child_col||' from '||g_rec_dbi_dim(j).denorm_table||' where '||
6444               g_rec_dbi_dim(j).parent_col||' ';
6445               exit;
6446             end if;
6447           end if;
6448         end if;
6449       end loop;
6450     end loop;
6451   end if;
6452 Exception when others then
6453   p_error_message:=sqlerrm;
6454   write_to_log_file_n('Exception in set_and_get_dim_sql '||sqlerrm);
6455   raise;
6456 End;
6457 
6458 procedure create_int_md_fk(
6459 p_mv_name varchar2
6460 ) is
6461 --
6462 l_s_tables BSC_IM_UTILS.varchar_tabletype;
6463 l_number_s_tables number;
6464 l_fk BSC_IM_UTILS.varchar_tabletype;
6465 l_number_fk number;
6466 l_exception exception;
6467 --
6468 Begin
6469   if get_s_tables_for_mv(p_mv_name,l_s_tables,l_number_s_tables)=false then
6470     raise l_exception;
6471   end if;
6472   if get_table_fks(l_s_tables(1),l_fk,l_number_fk)=false then
6473     raise l_exception;
6474   end if;
6475   for i in 1..l_number_fk loop
6476     if BSC_IM_INT_MD.create_fk(
6477       l_fk(i),
6478       'SUMMARY MV',
6479       p_mv_name,
6480       null,
6481       null,
6482       null,
6483       'BSC',
6484       'SUMMARY MV')=false then
6485       raise l_exception;
6486     end if;
6487   end loop;
6488 Exception
6489   when l_exception then
6490   raise;
6491   when others then
6492   write_to_log_file_n('Exception in create_int_md_fk '||sqlerrm);
6493   raise;
6494 End;
6495 
6496 /*
6497 **************************************************************************************************
6498 */
6499 
6500 END BSC_BSC_ADAPTER;