DBA Data[Home] [Help]

PACKAGE BODY: APPS.BSC_PMF_UI_WRAPPER

Source


1 package body BSC_PMF_UI_WRAPPER as
2 /* $Header: BSCPMFWB.pls 120.21 2007/06/01 06:49:24 ashankar ship $ */
3 /*
4  +======================================================================================+
5  |    Copyright (c) 2001 Oracle Corporation, Redwood Shores, CA, USA                    |
6  |                         All rights reserved.                                         |
7  +======================================================================================+
8  | FILENAME                                                                             |
9  |                      BSCPMFWB.pls                                                    |
10  |                                                                                      |
11  | Creation Date:                                                                       |
12  |                      October 18, 2001                                                |
13  |                                                                                      |
14  | Creator:                                                                             |
15  |                      Mario-Jair Campos                                               |
16  |                      Pankaj Johri   Fix Bug #2608683                                 |
17  |                      Adeulgao bug fix #2680214                                       |
18  |              Adeulgao bug fix #2722565 06-Jan-2003                                   |
19  |              Adeulgao bug fix #2731334 06-Jan-2003                                   |
20  |              Pradeep  bug fix #2747256 15-Jan-2003                                   |
21  |              Adeulgao bug fix #2731334 22-Jan-2003                                   |
22  |                      Aditya   bug fix #2786053 20-Feb-2003                           |
23  |                      PWALI    Bug #2942895, SQL BIND COMPLIANCE    13-MAY-2003       |
24  |                      Adrao fixed bug#3118110 added function is_In_Dimension          |
25  |                      ADEULGAO fixed bug#3127992 MODIFIED function is_In_Dimension    |
26  |                      ADEULGAO fixed bug#3138010                                      |
27  |                      ADRAO  Bug #3248729 14-NOV-2003                                 |
28  |                      ADRAO  Modified Delete_KPI_Group() not to throw error for       |
29  |                              for KPIs associated for Bug #3315077 14-DEC-2003        |
30  |                                                                                      |
31  |                                                                                      |
32  |  08-JAN-2004        krishan fixed for the bug 3357984                                |
33  |  27-FEB-2004        krishna fixed the bug# 3464251                                   |
34  |   10-MAR-04          jxyu  Modified for enhancement #3493589                         |
35  |  30-APR-2004        PAJOHRI Bug #3598852                                             |
36  |   05-MAY6-04        wcano add procedure Update_Kpi_Periodicities                     |
37  |   18-MAY-04          adrao Modified PL/SQL records and CRUD to accept SHORT_NAME     |
38  |   19-MAY-04          adrao Modified Assign_Kpi_Group to pass FALSE as p_commit to    |
39  |                      assign_kpi API                                                  |
40  |   02-JUL-04         rpenneru Modified for enh# 3532517                               |
41  |   30-SEP-04         visuri modified for bug 3852611                                  |
42  |   08-JUL-05         ashankar nodified the API Check_Tab by changing the API          |
43  |                     is_Scorecard_From_AG_Report TO is_Scorecard_From_Reports         |
44  |   14-JUL-05          Krishna modified update_kpi_periodicities for bug#4376162       |
45  |   25-JUL-05         hengliu added Check_Tabview_Dependency for bug#4237294           |
46  |   31-AUG-2005       ashankar Bugfix#4576022                                          |
47  |   25-MAY-2006       jxyu Bugfix#4174625                                              |
48  |   02-Aug-2006       ashankar Bug fix #5400575 made changes to the method Unassign_kpi|
49  |   17-Oct-2006       ppandey  Bug #5584826 Calender properties cascaded to Shared Obj |
50  |   08-jan-07         ashankar Bug#5652713 Added the method Is_Valid_Sim_Period        |
51  |   16-NOV-2006       ankgoel  Color By KPI enh#5244136                                |
52  |   09-feb-2007       ashankar Simulation Tree Enhacement 5386112                      |
53  |   27-feb-07         ashankar Fixed the issue in Update_Kpi_Periodicities             |
54  |   30-MAR-2007       akoduri  Enh #5928640 Migration of Periodicity properties from   |
55  |                              VB to Html                                              |
56  +======================================================================================+
57 */
58 
59 G_PKG_NAME        varchar2(30) := 'BSC_PMF_UI_WRAPPER';
60 
61 C_COL_M1_LEVEL1          CONSTANT      VARCHAR2(20):= 'COL_M1_LEVEL1';
62 C_COL_M1_LEVEL2          CONSTANT      VARCHAR2(20):= 'COL_M1_LEVEL2';
63 C_COL_M2_LEVEL1          CONSTANT      VARCHAR2(20):= 'COL_M2_LEVEL1';
64 C_COL_M2_LEVEL2          CONSTANT      VARCHAR2(20):= 'COL_M2_LEVEL2';
65 C_COL_M3_LEVEL1          CONSTANT      VARCHAR2(20):= 'COL_M3_LEVEL1';
66 C_COL_M3_LEVEL2          CONSTANT      VARCHAR2(20):= 'COL_M3_LEVEL2';
67 C_COL_M3_LEVEL3          CONSTANT      VARCHAR2(20):= 'COL_M3_LEVEL3';
68 C_COL_M3_LEVEL4          CONSTANT      VARCHAR2(20):= 'COL_M3_LEVEL4';
69 C_LANGUAGE               CONSTANT      VARCHAR2(2) := 'US';
70 C_ENABLE_FLAG            CONSTANT      NUMBER      := 2;
71 C_DISABLE_FLAG           CONSTANT      NUMBER      := 1;
72 C_HIDE_FLAG              CONSTANT      NUMBER      := 0;
73 
74 -- Define global variables to be passed to PMF-BSC API.
75 g_Bsc_Pmf_Ui_Rec      BSC_PMF_UI_API_PUB.Bsc_Pmf_Ui_Rec_Type;
76 g_Bsc_Pmf_Dim_Tbl     BSC_PMF_UI_API_PUB.Bsc_Pmf_Dim_Tbl_Type;
77 g_Fire                varchar2(3);
78 
79 
80 
81 
82 FUNCTION remove_percent(
83   p_input IN VARCHAR2
84 ) RETURN NUMBER;
85 
86 
87 -- adrao added
88 
89 FUNCTION Is_More
90 (
91         p_dim_short_names IN  OUT NOCOPY  VARCHAR2
92     ,   p_dim_name        OUT NOCOPY  VARCHAR2
93 ) RETURN BOOLEAN
94 IS
95     l_pos_ids               NUMBER;
96     l_pos_rel_types         NUMBER;
97     l_pos_rel_columns       NUMBER;
98 BEGIN
99     IF (p_dim_short_names IS NOT NULL) THEN
100         l_pos_ids           := INSTR(p_dim_short_names,   ','); -- adeulgao changed from ";" to ","
101         IF (l_pos_ids > 0) THEN
102             p_dim_name      :=  TRIM(SUBSTR(p_dim_short_names,    1,    l_pos_ids - 1));
103 
104             p_dim_short_names     :=  TRIM(SUBSTR(p_dim_short_names,    l_pos_ids + 1));
105         ELSE
106             p_dim_name      :=  TRIM(p_dim_short_names);
107 
108             p_dim_short_names     :=  NULL;
109         END IF;
110         RETURN TRUE;
111     ELSE
112         RETURN FALSE;
113     END IF;
114 END Is_More;
115 
116 
117 procedure Fire_Api(
118   p_api_call    varchar2
119   ) is
120 
121 begin
122    FND_MSG_PUB.Initialize;
123   if p_api_call = 'YES' then
124   g_Fire := 'YES';
125   else
126   g_Fire := 'NO';
127   end if;
128 
129 EXCEPTION
130   when others then
131     rollback;
132 end Fire_Api;
133 
134 
135 procedure Table_Generator(
136   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
137  ,p_kpi_id                      number
138  ,p_meas_short_name             varchar2
139  ,p_dim_level_short_name        varchar2
140 ) is
141 
142 l_commit      varchar2(10);
143 l_return_status     varchar2(100);
144 l_error_tbl     BIS_UTILITIES_PUB.Error_Tbl_Type;
145 l_msg_count     number;
146 l_msg_data      varchar2(255);
147 l_bad_level     varchar2(50);
148 
149 l_tbl_cnt     number;
150 
151 begin
152    FND_MSG_PUB.Initialize;
153   g_Bsc_Pmf_Ui_Rec.Kpi_id := to_number(p_kpi_id);
154   g_Bsc_Pmf_Ui_Rec.Measure_Short_Name := p_meas_short_name;
155 
156   l_tbl_cnt := g_Bsc_Pmf_Dim_Tbl.count;
157 
158   g_Bsc_Pmf_Dim_Tbl(l_tbl_cnt + 1).Dimension_Level_Short_Name := p_dim_level_short_name;
159 
160   if g_Fire = 'YES' then
161     BSC_PMF_UI_API_PUB.Bsc_Pmf_Ui_Api( FND_API.G_FALSE
162                                       ,g_Bsc_Pmf_Ui_Rec
163                                       ,g_Bsc_Pmf_Dim_Tbl
164                                       ,0
165                                       ,l_bad_level
166                                       ,l_return_status
167                                       ,l_msg_count
168                                       ,l_msg_data);
169     for i in 1..g_Bsc_Pmf_Dim_Tbl.count loop
170       g_Bsc_Pmf_Dim_Tbl.delete(i);
171     end loop;
172     g_Bsc_Pmf_Ui_Rec := null;
173 
174   end if;
175   IF (p_commit = FND_API.G_TRUE) THEN
176     COMMIT;
177   END IF;
178 EXCEPTION
179   WHEN FND_API.G_EXC_ERROR THEN
180     rollback;
181     FND_MSG_PUB.Count_And_Get( p_count  =>      l_msg_count
182                               ,p_data   =>      l_msg_data);
183   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
184     rollback;
185     FND_MSG_PUB.Count_And_Get( p_count    =>      l_msg_count
186                               ,p_data     =>      l_msg_data);
187   WHEN NO_DATA_FOUND THEN
188     rollback;
189     FND_MSG_PUB.Count_And_Get( p_count    =>      l_msg_count
190                               ,p_data     =>      l_msg_data);
191   WHEN OTHERS THEN
192     rollback;
193     FND_MSG_PUB.Count_And_Get( p_count    =>      l_msg_count
194                               ,p_data     =>      l_msg_data);
195 end Table_Generator;
196 
197 /************************************************************************************
198 ************************************************************************************/
199 
200 -- this procedure creates analysis options in a KPI.  No commits are passed to the
201 -- BSC APIs because these commits will undo the indicator lock.  The commit is
202 -- executed at the end of this procedure.
203 
204 PROCEDURE Add_Analysis_Option(
205   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
206  ,p_option_name                 IN VARCHAR2
207  ,p_option_description          IN VARCHAR2
208  ,p_meas_short_name             IN VARCHAR2
209  ,p_dim_level_short_names       IN VARCHAR2
210  ,p_kpi_id                      IN NUMBER
211  ,x_bad_level           OUT NOCOPY     varchar2
212  ,x_return_status       OUT NOCOPY     varchar2
213  ,x_msg_count           OUT NOCOPY     number
214  ,x_msg_data            OUT NOCOPY     varchar2
215 
216 ) IS
217 
218 TYPE Recdc_value    IS REF CURSOR;
219 dc_value      Recdc_value;
220 
221 l_Bsc_Kpi_Entity_Rec            BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
222 
223 l_sql       varchar2(1000);
224 
225 l_kpi_id      number;
226 
227   l_commit              VARCHAR2(10) := FND_API.G_FALSE;
228   l_return_status VARCHAR2(100);
229   l_error_tbl           BIS_UTILITIES_PUB.Error_Tbl_Type;
230   l_msg_count           NUMBER;
231   l_msg_data            VARCHAR2(255);
232   l_dim_level           VARCHAR2(100);
233   l_dim_levels          VARCHAR2(32000);
234   l_dim_levels_count    NUMBER;
235   l_dim_levels_table    BSC_PMF_UI_API_PUB.Bsc_Pmf_Dim_Tbl_Type;
236   l_analysis_option_rec BSC_PMF_UI_API_PUB.Bsc_Pmf_Ui_Rec_Type;
237   l_pos                 NUMBER;
238 
239   -- following set of variables add to support Default and View by Levels.
240   l_bis_dim_levels  varchar2(5000);
241   l_bis_end   number;
242   l_bis_lvl_status  varchar2(10);
243   l_temp    varchar2(30);
244 
245   l_cntr    number;
246 
247 
248 BEGIN
249    FND_MSG_PUB.Initialize;
250    x_return_status := FND_API.G_RET_STS_SUCCESS;
251   -- The first thing to do is not to allow
252 
253   l_analysis_option_rec.Kpi_id := p_kpi_id;
254   l_analysis_option_rec.Measure_Short_Name := p_meas_short_name;
255   l_analysis_option_rec.Option_Name := p_option_name;
256   l_analysis_option_rec.Option_Description := p_option_description;
257   l_dim_levels := p_dim_level_short_names;
258   l_dim_levels_count := 0;
259 
260 
261   -- This called could be made from two different places in IBuilder, when importing
262   -- a measure from BIS, or when adding a measure already within BSC.  If importing
263   -- from BIS then we need to handle it differently.
264   --
265   -- A BIS measure import will contain the string "BIS;" as its first 4 characters.
266   -- The entire string will have the following format:
267   -- 'BIS;'DIMLEVEL1;DIMLEVEL2;DIMLEVEL3;DFL;DIMLEVEL1,LD;DIMLEVEL3,LD;DIMLEVEL3;C'
268   -- where from "BIS" to "DFL" contains all dimension levels.  After "DFL" a dimension level
269   -- with "LD" is a default Dimension Level, and a level with "C" is a 'View by' Level.
270 
271   if (substr(p_dim_level_short_names, 1,4) = 'BIS;') then
272    -- l_bis_dim_levels := p_dim_level_short_names;
273     -- get a string containing onlya the dimension levels with no ALL or View by info.
274     l_bis_dim_levels := substr(p_dim_level_short_names, 5, (instr(p_dim_level_short_names, 'DFL;') - 5));
275 
276     -- assign the dimension levels to the TABLE type.
277     while length(l_bis_dim_levels) > 0 loop
278       l_pos := instr(l_bis_dim_levels, ';');
279       if l_pos > 0 then
280         l_dim_level := ltrim(rtrim(substr(l_bis_dim_levels, 1, l_pos - 1)));
281         l_bis_dim_levels := substr(l_bis_dim_levels, l_pos + 1, length(l_bis_dim_levels));
282       else
283         l_dim_level := ltrim(rtrim(l_bis_dim_levels));
284         l_bis_dim_levels := '';
285       end if;
286 
287       if length(l_dim_level) > 0 THEN
288         l_dim_levels_count := l_dim_levels_count + 1;
289         l_dim_levels_table(l_dim_levels_count).Dimension_Level_Short_Name := l_dim_level;
290       end if;
291 
292     end loop;
293 
294     -- reset l_bis_dim_levels to read the rest of the string containing info
295     -- on view by or ALL
296     l_bis_dim_levels := substr(p_dim_level_short_names, (instr(p_dim_level_short_names, 'DFL;') + 4));
297 
298     -- parse string into levels and their ALL or view by value.
299     while length(l_bis_dim_levels) > 0 loop
300       l_pos := instr(l_bis_dim_levels, ';');
301       if l_pos > 0 then
302         l_dim_level := ltrim(rtrim(substr(l_bis_dim_levels, 1, l_pos - 1)));
303         l_bis_dim_levels := substr(l_bis_dim_levels, l_pos + 1, length(l_bis_dim_levels));
304       else
305         l_dim_level := ltrim(rtrim(l_bis_dim_levels));
306         l_bis_dim_levels := '';
307       end if;
308 
309       if length(l_dim_level) > 0 THEN
310         l_dim_levels_count := l_dim_levels_count + 1;
311       end if;
312 
313       -- Now extract whether this level is ALL (LD) or view by (C)
314       l_bis_lvl_status := substr(l_dim_level, instr(l_dim_level, ',') + 1, length(l_dim_level) -1);
315       l_dim_level := substr(l_dim_level, 1, instr(l_dim_level, ',') - 1);
316 
317       -- Now set the level ALL or View By value to the TABLE TYPE
318       l_cntr := 1;
319       for l_cntr in 1..l_dim_levels_table.count loop
320         if (l_dim_levels_table(l_cntr).Dimension_Level_Short_Name = l_dim_level) then
321           l_dim_levels_table(l_cntr).Dimension_Level_Status := l_bis_lvl_status;
322 
323         end if;
324       end loop;
325 
326     end loop;
327 
328 
329   else
330 
331     -- Parse the dimension level short names
332     WHILE LENGTH(l_dim_levels) > 0 LOOP
333       l_pos := INSTR(l_dim_levels, ';');
334 
335       IF l_pos > 0 THEN
336         l_dim_level := LTRIM(RTRIM(SUBSTR(l_dim_levels, 1, l_pos - 1)));
337         l_dim_levels := SUBSTR(l_dim_levels, l_pos + 1, LENGTH(l_dim_levels));
338       ELSE
339         l_dim_level := LTRIM(RTRIM(l_dim_levels));
340         l_dim_levels := '';
341       END IF;
342 
343       IF LENGTH(l_dim_level) > 0 THEN
344         l_dim_levels_count := l_dim_levels_count + 1;
345         l_dim_levels_table(l_dim_levels_count).Dimension_Level_Short_Name := l_dim_level;
346       END IF;
347     END LOOP;
348 
349   end if;
350 
351   -- call the api to create analysis option
352   BSC_PMF_UI_API_PUB.Bsc_Pmf_Ui_Api(
353     FND_API.G_FALSE,
354     l_analysis_option_rec,
355     l_dim_levels_table,
356     0,
357     x_bad_level,
358     x_return_status,
359     x_msg_count,
360     x_msg_data);
361 
362     -- check value of x_bad_level, if not null then raise error.
363     if (x_bad_level is not null) then
364       RAISE FND_API.G_EXC_ERROR;
365     end if;
366 
367   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
368 
369   BSC_KPI_PUB.Update_Kpi_Time_Stamp( FND_API.G_FALSE
370                                     ,l_Bsc_Kpi_Entity_Rec
371                                     ,x_return_status
372                                     ,x_msg_count
373                                     ,x_msg_data);
374 
375   -- Add the analysis dimension levels to the shared Indicators.
376   l_sql := 'select indicator ' ||
377            '  from BSC_KPIS_B ' ||
378            ' where source_indicator = :1';
379 
380   open dc_value for l_sql using p_kpi_id;
381     loop
382       fetch dc_value into l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_id;
383       exit when dc_value%NOTFOUND;
384 
385       l_kpi_id := l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_id;
386 
387       BSC_DESIGNER_PVT.Duplicate_Record_by_Indicator('BSC_KPI_DIM_LEVELS_B', p_kpi_id, l_kpi_id);
388       BSC_DESIGNER_PVT.Duplicate_Record_by_Indicator('BSC_KPI_DIM_LEVELS_TL', p_kpi_id, l_kpi_id);
389       BSC_DESIGNER_PVT.Duplicate_Record_by_Indicator('BSC_KPI_DIM_GROUPS', p_kpi_id, l_kpi_id);
390       BSC_DESIGNER_PVT.Duplicate_Record_by_Indicator('BSC_KPI_DIM_SETS_TL', p_kpi_id, l_kpi_id);
391       BSC_DESIGNER_PVT.Duplicate_Record_by_Indicator('BSC_KPI_DIM_LEVEL_PROPERTIES', p_kpi_id, l_kpi_id);
392 
393       BSC_KPI_PUB.Update_Kpi_Time_Stamp( FND_API.G_FALSE
394                                         ,l_Bsc_Kpi_Entity_Rec
395                                         ,x_return_status
396                                         ,x_msg_count
397                                         ,x_msg_data);
398 
399     end loop;
400   close dc_value;
401 
402 
403   IF (p_commit = FND_API.G_TRUE) THEN
404     COMMIT;
405   END IF;
406 
407 EXCEPTION
408     WHEN FND_API.G_EXC_ERROR THEN
409         ROLLBACK;
410         IF (x_msg_data IS NULL) THEN
411             FND_MSG_PUB.Count_And_Get
412             (      p_encoded   =>  FND_API.G_FALSE
413                ,   p_count     =>  x_msg_count
414                ,   p_data      =>  x_msg_data
415             );
416         END IF;
417         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
418         x_return_status :=  FND_API.G_RET_STS_ERROR;
419     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
420         ROLLBACK;
421         IF (x_msg_data IS NULL) THEN
422             FND_MSG_PUB.Count_And_Get
423             (      p_encoded   =>  FND_API.G_FALSE
424                ,   p_count     =>  x_msg_count
425                ,   p_data      =>  x_msg_data
426             );
427         END IF;
428         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
429         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
430     WHEN NO_DATA_FOUND THEN
431         ROLLBACK;
432         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
433         IF (x_msg_data IS NOT NULL) THEN
434             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Add_Analysis_Option ';
435         ELSE
436             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Add_Analysis_Option ';
437         END IF;
438         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
439     WHEN OTHERS THEN
440         ROLLBACK;
441         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
442         IF (x_msg_data IS NOT NULL) THEN
443             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Add_Analysis_Option ';
444         ELSE
445             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Add_Analysis_Option ';
446         END IF;
447         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
448 END Add_Analysis_Option;
449 
450 /************************************************************************************
451 ************************************************************************************/
452 
453 --Modified procedure. It does not return the kpi_group_id.
454 procedure Create_Kpi_Group(
455   p_commit              IN             VARCHAR2 := FND_API.G_TRUE
456  ,p_tab_id              IN             number
457  ,p_kpi_group_id        IN             number
458  ,x_return_status       OUT NOCOPY     varchar2
459  ,x_msg_count           OUT NOCOPY     number
460  ,x_msg_data            OUT NOCOPY     varchar2
461  ,p_kpi_group_name      IN             varchar2
462  ,p_kpi_group_help      IN             varchar2
463 ) is
464 
465 l_Bsc_Kpi_Group_Rec              BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
466 
467 l_commit                        varchar2(10);
468 
469 --l_kpi_group_id      number;
470 
471 begin
472    FND_MSG_PUB.Initialize;
473    x_return_status := FND_API.G_RET_STS_SUCCESS;
474   l_commit := FND_API.G_FALSE;
475   SAVEPOINT  BSCCrtKPIGrpWrapper;
476 
477   --Call procedure to initialize Kpi Group Rec
478   BSC_KPI_GROUP_PUB.Initialize_Kpi_Group_Rec(
479                           p_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
480                          ,x_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
481                          ,x_return_status => x_return_status
482                          ,x_msg_count => x_msg_count
483                          ,x_msg_data => x_msg_data);
484   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
485       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
486   END IF;
487 
488   -- set the passed values to the record.
489   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Name := p_kpi_group_name;
490   /*Fix Bug #2608683 */
491   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Help := p_kpi_group_help;
492   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := p_kpi_group_id;
493 
494 /*  Below code has been commented until further review.
495 
496   -- The BSC APIs (following original builder functionality) assign Tab ID of -1
497   -- as a default tab when a KPI group is first created.  Most of the logic in the
498   -- current APIs assume this default tab id.
499   -- However, for IBuilder (HTML builder) tab id of -1 is not being used.  Instead
500   -- a "Library Tab" is being used, with id of -2.
501   -- Therefore, in order to have both ends (UI and back end APIS) match, this wrapper
502   -- calls the APIs passing null for the tab id.  This will assign it to tab -1.  This
503   -- should only happen if the group is being created for the first time (UI tab id -2)
504   -- After that we will call the APIs again, but with -2 (if first time) or with whatever
505   -- tab id desired.
506   if p_tab_id = -2 then
507     l_Bsc_Kpi_Group_Rec.Bsc_Tab_Id := -1;
508     BSC_KPI_GROUP_PUB.Create_Kpi_Group( l_commit
509                                        ,l_Bsc_Kpi_Group_Rec
510                                        ,x_return_status
511                                        ,x_msg_count
512                                        ,x_msg_data);
513 
514     -- the following piece of code should be removed ASAP.  We need it due to the fact
515     -- mentioned above (tabs -1, -2 , 2 different default tabs).  We need to assign a
516     -- brand new KPI group to both tabs, however, after assigning to one, the group is no
517     -- longer new.  We need to get the id for this group.
518     -- To do this we ASSUME that the id that was created last is what was created in the
519     -- previous call.  DANGEROUS.
520 
521     select MAX(ind_group_id)
522       into l_kpi_group_id
523       from BSC_TAB_IND_GROUPS_TL;
524 
525     l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := l_kpi_group_id;
526   end if;
527 */
528 
529   -- set the Tab id to value passed.
530   l_Bsc_Kpi_Group_Rec.Bsc_Tab_Id := p_tab_id;
531 
532   if l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id is null then
533     BSC_KPI_GROUP_PUB.Create_Kpi_Group( p_commit => l_commit
534                                        ,p_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
535                                        ,x_return_status => x_return_status
536                                        ,x_msg_count => x_msg_count
537                                        ,x_msg_data => x_msg_data);
538     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
539         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
540     END IF;
541   else
542     BSC_KPI_GROUP_PUB.Update_Kpi_Group( FND_API.G_FALSE
543                                        ,l_Bsc_Kpi_Group_Rec
544                                        ,x_return_status
545                                        ,x_msg_count
546                                        ,x_msg_data);
547     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
548         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
549     END IF;
550   end if;
551 
552   IF (p_commit = FND_API.G_TRUE) THEN
553     COMMIT;
554   END IF;
555 
556 EXCEPTION
557     WHEN FND_API.G_EXC_ERROR THEN
558         ROLLBACK TO BSCCrtKPIGrpWrapper;
559         IF (x_msg_data IS NULL) THEN
560             FND_MSG_PUB.Count_And_Get
561             (      p_encoded   =>  FND_API.G_FALSE
562                ,   p_count     =>  x_msg_count
563                ,   p_data      =>  x_msg_data
564             );
565         END IF;
566         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
567         x_return_status :=  FND_API.G_RET_STS_ERROR;
568     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
569         ROLLBACK TO BSCCrtKPIGrpWrapper;
570         IF (x_msg_data IS NULL) THEN
571             FND_MSG_PUB.Count_And_Get
572             (      p_encoded   =>  FND_API.G_FALSE
573                ,   p_count     =>  x_msg_count
574                ,   p_data      =>  x_msg_data
575             );
576         END IF;
577         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
578         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
579     WHEN NO_DATA_FOUND THEN
580         ROLLBACK TO BSCCrtKPIGrpWrapper;
581         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
582         IF (x_msg_data IS NOT NULL) THEN
583             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi_Group ';
584         ELSE
585             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi_Group ';
586         END IF;
587         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
588     WHEN OTHERS THEN
589         ROLLBACK TO BSCCrtKPIGrpWrapper;
590         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
591         IF (x_msg_data IS NOT NULL) THEN
592             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi_Group ';
593         ELSE
594             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi_Group ';
595         END IF;
596         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
597 
598 end Create_Kpi_Group;
599 
600 
601 
602 /************************************************************************************
603 ************************************************************************************/
604 
605 --New procedure. It returns the kpi_group_id.
606 procedure Create_Kpi_Group(
607   p_commit                IN             VARCHAR2 := FND_API.G_FALSE
608  ,p_tab_id                IN             NUMBER  -- It needs to pass NULL or -1  if you want to create a kpi group
609  ,p_kpi_group_id          IN             NUMBER
610  ,p_kpi_group_name        IN             VARCHAR2
611  ,p_kpi_group_help        IN             VARCHAR2
612  ,p_Kpi_Group_short_Name  IN             VARCHAR2 := NULL
613  ,x_kpi_group_id          OUT NOCOPY     NUMBER  -- OUT parameter for kpi group id
614  ,x_return_status         OUT NOCOPY     VARCHAR2
615  ,x_msg_count             OUT NOCOPY     NUMBER
616  ,x_msg_data              OUT NOCOPY     VARCHAR2
617 ) is
618 
619 l_Bsc_Kpi_Group_Rec              BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
620 l_Bsc_Kpi_Group_Rec_Out          BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
621 
622 l_commit                        varchar2(10);
623 
624 begin
625    FND_MSG_PUB.Initialize;
626    x_return_status := FND_API.G_RET_STS_SUCCESS;
627   l_commit := FND_API.G_FALSE;  --local commit flag.
628   SAVEPOINT  BSCCrtKPIGrpWrapper;
629 
630   --Call procedure to initialize Kpi Group Rec
631   BSC_KPI_GROUP_PUB.Initialize_Kpi_Group_Rec(
632           p_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
633          ,x_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
634          ,x_return_status     => x_return_status
635          ,x_msg_count         => x_msg_count
636          ,x_msg_data          => x_msg_data
637   );
638   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
639       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
640   END IF;
641 
642   -- set the passed values to the record.
643   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Name := p_kpi_group_name;
644   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Help := p_kpi_group_help;
645   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := p_kpi_group_id;
646   l_Bsc_Kpi_Group_Rec.Bsc_Tab_Id := p_tab_id;
647   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Short_Name := p_Kpi_Group_short_Name;
648 
649   if l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id is null then
650     BSC_KPI_GROUP_PUB.Create_Kpi_Group( p_commit => l_commit
651                                        ,p_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec
652                                        ,x_Bsc_Kpi_Group_Rec => l_Bsc_Kpi_Group_Rec_Out
653                                        ,x_return_status => x_return_status
654                                        ,x_msg_count => x_msg_count
655                                        ,x_msg_data => x_msg_data);
656     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
657         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
658     END IF;
659 
660     x_kpi_group_id := l_Bsc_Kpi_Group_Rec_Out.Bsc_Kpi_Group_Id;
661 
662   else
663     BSC_KPI_GROUP_PUB.Update_Kpi_Group( FND_API.G_FALSE
664                                        ,l_Bsc_Kpi_Group_Rec
665                                        ,x_return_status
666                                        ,x_msg_count
667                                        ,x_msg_data);
668     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
669         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
670     END IF;
671   end if;
672 
673   IF (p_commit = FND_API.G_TRUE) THEN
674     COMMIT;
675   END IF;
676 
677  --DBMS_OUTPUT.PUT_LINE('Created kpi_group_id '||x_kpi_group_id);
678 
679 EXCEPTION
680     WHEN FND_API.G_EXC_ERROR THEN
681         ROLLBACK TO BSCCrtKPIGrpWrapper;
682         IF (x_msg_data IS NULL) THEN
683             FND_MSG_PUB.Count_And_Get
684             (      p_encoded   =>  FND_API.G_FALSE
685                ,   p_count     =>  x_msg_count
686                ,   p_data      =>  x_msg_data
687             );
688         END IF;
689         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
690         x_return_status :=  FND_API.G_RET_STS_ERROR;
691     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
692         ROLLBACK TO BSCCrtKPIGrpWrapper;
693         IF (x_msg_data IS NULL) THEN
694             FND_MSG_PUB.Count_And_Get
695             (      p_encoded   =>  FND_API.G_FALSE
696                ,   p_count     =>  x_msg_count
697                ,   p_data      =>  x_msg_data
698             );
699         END IF;
700         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
701         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
702     WHEN NO_DATA_FOUND THEN
703         ROLLBACK TO BSCCrtKPIGrpWrapper;
704         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
705         IF (x_msg_data IS NOT NULL) THEN
706             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi_Group with parameter x_kpi_group_id ';
707         ELSE
708             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi_Group with parameter x_kpi_group_id ';
709         END IF;
710         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
711     WHEN OTHERS THEN
712         ROLLBACK TO BSCCrtKPIGrpWrapper;
713         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
714         IF (x_msg_data IS NOT NULL) THEN
715             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi_Group with parameter x_kpi_group_id ';
716         ELSE
717             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi_Group with parameter x_kpi_group_id ';
718         END IF;
719         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
720 
721 end Create_Kpi_Group;
722 
723 
724 /************************************************************************************
725 ************************************************************************************/
726 
727 procedure Update_Kpi_Group(
728   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
729  ,p_kpi_group_id                number
730  ,p_kpi_group_name              varchar2
731  ,x_return_status       OUT NOCOPY     varchar2
732  ,x_msg_count           OUT NOCOPY     number
733  ,x_msg_data            OUT NOCOPY     varchar2
734  ,p_kpi_group_help              varchar2
735 ) is
736 
737 l_Bsc_Kpi_Group_Rec              BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
738 
739 l_commit                        varchar2(10);
740 
741 l_kpi_group_id                  number;
742 
743 begin
744    FND_MSG_PUB.Initialize;
745    x_return_status := FND_API.G_RET_STS_SUCCESS;
746    SAVEPOINT BSCUpdKPIGrpWrapper;
747   l_commit := FND_API.G_TRUE;
748 
749   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := p_kpi_group_id;
750   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Name := p_kpi_group_name;
751   l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Help := p_kpi_group_help;
752 
753   -- set some defaults.  At the moment the UI is unable to set/pass these values.
754   -- Added 'USERENV()' to implement NLS fix for Bug #2786053
755   l_Bsc_Kpi_Group_Rec.Bsc_Tab_Id := -1;
756   l_Bsc_Kpi_Group_Rec.Bsc_Language := NVL(USERENV('LANG'),'US');
757   l_Bsc_Kpi_Group_Rec.Bsc_Source_Language := 'US';
758 
759 
760   BSC_KPI_GROUP_PUB.Update_Kpi_Group( FND_API.G_FALSE
761                                      ,l_Bsc_Kpi_Group_Rec
762                                      ,x_return_status
763                                      ,x_msg_count
764                                      ,x_msg_data);
765     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
766         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
767     END IF;
768   IF (p_commit = FND_API.G_TRUE) THEN
769     COMMIT;
770   END IF;
771 
772 EXCEPTION
773     WHEN FND_API.G_EXC_ERROR THEN
774         ROLLBACK TO BSCUpdKPIGrpWrapper;
775         IF (x_msg_data IS NULL) THEN
776             FND_MSG_PUB.Count_And_Get
777             (      p_encoded   =>  FND_API.G_FALSE
778                ,   p_count     =>  x_msg_count
779                ,   p_data      =>  x_msg_data
780             );
781         END IF;
782         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
783         x_return_status :=  FND_API.G_RET_STS_ERROR;
784     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
785         ROLLBACK TO BSCUpdKPIGrpWrapper;
786         IF (x_msg_data IS NULL) THEN
787             FND_MSG_PUB.Count_And_Get
788             (      p_encoded   =>  FND_API.G_FALSE
789                ,   p_count     =>  x_msg_count
790                ,   p_data      =>  x_msg_data
791             );
792         END IF;
793         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
794         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
795     WHEN NO_DATA_FOUND THEN
796         ROLLBACK TO BSCUpdKPIGrpWrapper;
797         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
798         IF (x_msg_data IS NOT NULL) THEN
799             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Kpi_Group ';
800         ELSE
801             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Kpi_Group ';
802         END IF;
803         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
804     WHEN OTHERS THEN
805         ROLLBACK TO BSCUpdKPIGrpWrapper;
806         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
807         IF (x_msg_data IS NOT NULL) THEN
808             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Kpi_Group ';
809         ELSE
810             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Kpi_Group ';
811         END IF;
812         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
813 end Update_Kpi_Group;
814 
815 /************************************************************************************
816 ************************************************************************************/
817 
818 --Modified procedure. It does not return the kpi_id.
819 procedure Create_Kpi(
820   p_commit              IN             VARCHAR2 := FND_API.G_TRUE
821  ,p_group_id            IN             number
822  ,p_responsibility_id   IN             number
823  ,x_return_status       OUT NOCOPY     varchar2
824  ,x_msg_count           OUT NOCOPY     number
825  ,x_msg_data            OUT NOCOPY     varchar2
826  ,p_kpi_name            IN             varchar2
827  ,p_kpi_help            IN             varchar2
828 ) is
829 
830 l_Bsc_Kpi_Entity_Rec              BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
831 
832 l_commit                        varchar2(10);
833 
834 
835 begin
836    FND_MSG_PUB.Initialize;
837    x_return_status := FND_API.G_RET_STS_SUCCESS;
838    SAVEPOINT BSCCreateKPIWrapper;
839   l_commit := FND_API.G_FALSE;
840 
841   --Call procedure to initialize Kpi Entity Rec
842   BSC_KPI_PUB.Initialize_Kpi_Entity_Rec(
843                           p_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
844                          ,x_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
845                          ,x_return_status => x_return_status
846                          ,x_msg_count => x_msg_count
847                          ,x_msg_data => x_msg_data);
848   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
849       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
850   END IF;
851 
852   -- set the passed values to the record.
853   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id := p_group_id;
854   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Name := p_kpi_name;
855   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Help := p_kpi_help;
856   l_Bsc_Kpi_Entity_Rec.Bsc_Responsibility_Id := p_responsibility_id;
857 
858 
859   BSC_KPI_PUB.Create_Kpi( p_commit => l_commit
860                          ,p_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
861                          ,x_return_status => x_return_status
862                          ,x_msg_count => x_msg_count
863                          ,x_msg_data => x_msg_data);
864     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
865         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
866     END IF;
867   IF (p_commit = FND_API.G_TRUE) THEN
868     COMMIT;
869   END IF;
870 
871 EXCEPTION
872     WHEN FND_API.G_EXC_ERROR THEN
873         ROLLBACK TO BSCCreateKPIWrapper;
874         IF (x_msg_data IS NULL) THEN
875             FND_MSG_PUB.Count_And_Get
876             (      p_encoded   =>  FND_API.G_FALSE
877                ,   p_count     =>  x_msg_count
878                ,   p_data      =>  x_msg_data
879             );
880         END IF;
881         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
882         x_return_status :=  FND_API.G_RET_STS_ERROR;
883     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
884         ROLLBACK TO BSCCreateKPIWrapper;
885         IF (x_msg_data IS NULL) THEN
886             FND_MSG_PUB.Count_And_Get
887             (      p_encoded   =>  FND_API.G_FALSE
888                ,   p_count     =>  x_msg_count
889                ,   p_data      =>  x_msg_data
890             );
891         END IF;
892         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
893         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
894     WHEN NO_DATA_FOUND THEN
895         ROLLBACK TO BSCCreateKPIWrapper;
896         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
897         IF (x_msg_data IS NOT NULL) THEN
898             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi ';
899         ELSE
900             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi ';
901         END IF;
902         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
903     WHEN OTHERS THEN
904         ROLLBACK TO BSCCreateKPIWrapper;
905         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
906         IF (x_msg_data IS NOT NULL) THEN
907             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi ';
908         ELSE
909             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi ';
910         END IF;
911         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
912 end Create_Kpi;
913 
914 
915 /************************************************************************************
916 ************************************************************************************/
917 
918 --New procedure. It returns the kpi_id.
919 procedure Create_Kpi(
920   p_commit              IN            VARCHAR2 := FND_API.G_FALSE
921  ,p_group_id            IN            NUMBER
922  ,p_responsibility_id   IN            NUMBER
923  ,p_kpi_name            IN            VARCHAR2
924  ,p_kpi_help            IN            VARCHAR2
925  ,p_Kpi_Short_Name      IN            VARCHAR2 := NULL
926  ,p_Kpi_Indicator_Type  IN            NUMBER   := NULL
927  ,x_kpi_id              OUT NOCOPY    NUMBER
928  ,x_return_status       OUT NOCOPY    VARCHAR2
929  ,x_msg_count           OUT NOCOPY    NUMBER
930  ,x_msg_data            OUT NOCOPY    VARCHAR2
931 ) is
932 
933 l_Bsc_Kpi_Entity_Rec              BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
934 l_Bsc_Kpi_Entity_Rec_Out          BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
935 l_commit                          varchar2(10);
936 
937 begin
938    FND_MSG_PUB.Initialize;
939    x_return_status := FND_API.G_RET_STS_SUCCESS;
940    SAVEPOINT BSCCreateKPIWrapper;
941   l_commit := FND_API.G_FALSE;  --local commit flag.
942 
943   --Call procedure to initialize Kpi Entity Rec
944   BSC_KPI_PUB.Initialize_Kpi_Entity_Rec(
945                           p_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
946                          ,x_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
947                          ,x_return_status => x_return_status
948                          ,x_msg_count => x_msg_count
949                          ,x_msg_data => x_msg_data);
950   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
951       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
952   END IF;
953 
954   -- set the passed values to the record.
955   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id := p_group_id;
956   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Name := p_kpi_name;
957   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Help := p_kpi_help;
958   l_Bsc_Kpi_Entity_Rec.Bsc_Responsibility_Id := p_responsibility_id;
959 
960   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Short_Name := p_Kpi_Short_Name;
961   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Indicator_Type := p_Kpi_Indicator_Type;
962 
963   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Config_Type := BSC_BIS_KPI_CRUD_PUB.C_NORMAL_INDICATOR_CONFIG_TYPE;
964 
965   IF(p_Kpi_Indicator_Type = BSC_BIS_KPI_CRUD_PUB.C_SIMULATION_INDICATOR)THEN
966     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Config_Type := BSC_BIS_KPI_CRUD_PUB.C_SIM_INDICATOR_CONFIG_TYPE;
967     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Indicator_Type :=   BSC_BIS_KPI_CRUD_PUB.C_SINGLE_BAR_INDICATOR;
968   END IF;
969 
970 
971   BSC_KPI_PUB.Create_Kpi( p_commit => l_commit
972                          ,p_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
973                          ,x_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec_Out
974                          ,x_return_status => x_return_status
975                          ,x_msg_count => x_msg_count
976                          ,x_msg_data => x_msg_data);
977   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
978       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
979   END IF;
980 
981   x_kpi_id := l_Bsc_Kpi_Entity_Rec_Out.Bsc_Kpi_Id;
982 
983   IF (p_commit = FND_API.G_TRUE) THEN
984     COMMIT;
985   END IF;
986 
987  --DBMS_OUTPUT.PUT_LINE('Created kpi_id '||x_kpi_id);
988 
989 EXCEPTION
990     WHEN FND_API.G_EXC_ERROR THEN
991         ROLLBACK TO BSCCreateKPIWrapper;
992         IF (x_msg_data IS NULL) THEN
993             FND_MSG_PUB.Count_And_Get
994             (      p_encoded   =>  FND_API.G_FALSE
995                ,   p_count     =>  x_msg_count
996                ,   p_data      =>  x_msg_data
997             );
998         END IF;
999         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1000         x_return_status :=  FND_API.G_RET_STS_ERROR;
1001     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1002         ROLLBACK TO BSCCreateKPIWrapper;
1003         IF (x_msg_data IS NULL) THEN
1004             FND_MSG_PUB.Count_And_Get
1005             (      p_encoded   =>  FND_API.G_FALSE
1006                ,   p_count     =>  x_msg_count
1007                ,   p_data      =>  x_msg_data
1008             );
1009         END IF;
1010         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1011         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1012     WHEN NO_DATA_FOUND THEN
1013         ROLLBACK TO BSCCreateKPIWrapper;
1014         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1015         IF (x_msg_data IS NOT NULL) THEN
1016             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi with parameter x_kpi_id ';
1017         ELSE
1018             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi with parameter x_kpi_id ';
1019         END IF;
1020         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1021     WHEN OTHERS THEN
1022         ROLLBACK TO BSCCreateKPIWrapper;
1023         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1024         IF (x_msg_data IS NOT NULL) THEN
1025             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Kpi with parameter x_kpi_id ';
1026         ELSE
1027             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Kpi with parameter x_kpi_id ';
1028         END IF;
1029         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1030 end Create_Kpi;
1031 
1032 
1033 /************************************************************************************
1034 ************************************************************************************/
1035 
1036 procedure Update_Kpi(
1037   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1038  ,p_kpi_id            IN      number
1039  ,x_return_status       OUT NOCOPY     varchar2
1040  ,x_msg_count           OUT NOCOPY     number
1041  ,x_msg_data            OUT NOCOPY     varchar2
1042  ,p_kpi_name            IN      varchar2 DEFAULT null
1043  ,p_kpi_help            IN      varchar2 DEFAULT null
1044 ) is
1045 
1046 l_Bsc_Kpi_Entity_Rec              BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
1047 
1048 TYPE Recdc_value                IS REF CURSOR;
1049 dc_value                        Recdc_value;
1050 
1051 l_commit                        varchar2(10);
1052 l_sql                           varchar2(1000);
1053 l_tab_name                      BSC_TABS_TL.NAME%TYPE;
1054 
1055 l_count       number;
1056 
1057 begin
1058    FND_MSG_PUB.Initialize;
1059    x_return_status := FND_API.G_RET_STS_SUCCESS;
1060    SAVEPOINT BSCUpdateKPIWrapper;
1061   l_commit := FND_API.G_TRUE;
1062 
1063   l_count := 0;
1064 
1065   -- We need to check if there is an update on the Indicator name in order
1066   -- to prevent it.  To do this we need to check if there is a different
1067   -- indicator in the same tab  with the same name.
1068   select count(indicator)
1069     into l_count
1070     from bsc_tab_indicators a
1071    where a.indicator <> p_kpi_id
1072      and a.tab_id = (select tab_id
1073                        from bsc_tab_indicators
1074                       where indicator = p_kpi_id)
1075      and a.indicator in (select indicator
1076                            from bsc_kpis_tl
1077                           where name = p_kpi_name);
1078 
1079   if l_count <> 0 then
1080     select d.name
1081       into l_tab_name
1082       from BSC_TAB_INDICATORS a,
1083            BSC_KPIS_TL b,
1084            BSC_KPIS_TL c,
1085            BSC_TABS_TL d
1086      where a.indicator = b.indicator
1087        and a.tab_id = d.tab_id
1088        and b.name = c.name
1089        and c.indicator = p_kpi_id;
1090 
1091     FND_MESSAGE.SET_NAME('BSC','BSC_B_NO_SAMEKPI_TAB');
1092     FND_MESSAGE.SET_TOKEN('Indicator name: ', p_kpi_name);
1093     FND_MESSAGE.SET_TOKEN('Tab name: ', l_tab_name);
1094     FND_MSG_PUB.ADD;
1095     RAISE FND_API.G_EXC_ERROR;
1096   end if;
1097 
1098 
1099   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
1100   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Name := p_kpi_name;
1101   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Help := p_kpi_help;
1102 
1103   -- set some default values.
1104   l_Bsc_Kpi_Entity_Rec.Bsc_Language := 'US';
1105   l_Bsc_Kpi_Entity_Rec.Bsc_Source_Language:= 'US';
1106 
1107   -- Calling private version.  The public version does a few things which
1108   -- are not supported yet.  When those features are supported (Update
1109   -- Properties, Update Defaults, etc.) then the call should be changed
1110   -- to the Public version.  The UI only allows "Updates" on Master Indicators
1111   -- so it is assumed the Indicator currently being handled is the Masater Indicator.
1112 
1113   BSC_KPI_PVT.Update_Kpi( FND_API.G_FALSE
1114                          ,l_Bsc_Kpi_Entity_Rec
1115                          ,x_return_status
1116                          ,x_msg_count
1117                          ,x_msg_data);
1118     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1119         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1120     END IF;
1121 
1122 
1123   -- The public version of the API determines how many shared indicators there
1124   -- are.  However since we are bypassing the Public version we need to determine
1125   -- those indicators here.  Once we call the Public version again then we can
1126   -- remove this block.
1127 
1128   -- determine if there are any shared KPI based on this KPI.
1129   select count(indicator)
1130     into l_count
1131     from BSC_KPIS_B
1132    where source_indicator = p_kpi_id;
1133 
1134   -- if there are any shared KPIs update those also.
1135   if l_count > 0 then
1136 
1137     l_sql := 'select indicator ' ||
1138              '  from BSC_KPIS_B ' ||
1139              ' where source_indicator = :1';
1140 
1141     open dc_value for l_sql using p_kpi_id;
1142       loop
1143         fetch dc_value into l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
1144         exit when dc_value%NOTFOUND;
1145 
1146         BSC_KPI_PVT.Update_Kpi( FND_API.G_FALSE
1147                                ,l_Bsc_Kpi_Entity_Rec
1148                                ,x_return_status
1149                                ,x_msg_count
1150                                ,x_msg_data);
1151     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1152         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1153     END IF;
1154 
1155 
1156       END LOOP;
1157     close dc_value;
1158 
1159   end if;
1160 
1161 
1162   IF (p_commit = FND_API.G_TRUE) THEN
1163     COMMIT;
1164   END IF;
1165 
1166 EXCEPTION
1167     WHEN FND_API.G_EXC_ERROR THEN
1168         ROLLBACK TO BSCUpdateKPIWrapper;
1169         IF (x_msg_data IS NULL) THEN
1170             FND_MSG_PUB.Count_And_Get
1171             (      p_encoded   =>  FND_API.G_FALSE
1172                ,   p_count     =>  x_msg_count
1173                ,   p_data      =>  x_msg_data
1174             );
1175         END IF;
1176         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1177         x_return_status :=  FND_API.G_RET_STS_ERROR;
1178     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1179         ROLLBACK TO BSCUpdateKPIWrapper;
1180         IF (x_msg_data IS NULL) THEN
1181             FND_MSG_PUB.Count_And_Get
1182             (      p_encoded   =>  FND_API.G_FALSE
1183                ,   p_count     =>  x_msg_count
1184                ,   p_data      =>  x_msg_data
1185             );
1186         END IF;
1187         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1188         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1189     WHEN NO_DATA_FOUND THEN
1190         ROLLBACK TO BSCUpdateKPIWrapper;
1191         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1192         IF (x_msg_data IS NOT NULL) THEN
1193             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Kpi ';
1194         ELSE
1195             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Kpi ';
1196         END IF;
1197         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1198     WHEN OTHERS THEN
1199         ROLLBACK TO BSCUpdateKPIWrapper;
1200         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1201         IF (x_msg_data IS NOT NULL) THEN
1202             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Kpi ';
1203         ELSE
1204             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Kpi ';
1205         END IF;
1206         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1207 
1208 end Update_Kpi;
1209 
1210 /************************************************************************************
1211 ************************************************************************************/
1212 
1213 --It doesn't need to be modified for enhancement#3493589.
1214 procedure Create_Tab
1215 (   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1216   , p_responsibility_id     IN          NUMBER
1217   , x_return_status         OUT NOCOPY  VARCHAR2
1218   , x_msg_count             OUT NOCOPY  NUMBER
1219   , x_msg_data              OUT NOCOPY  VARCHAR2
1220   , p_tab_name              IN          VARCHAR2 := NULL
1221   , p_tab_help              IN          VARCHAR2 := NULL
1222 ) IS
1223     l_Bsc_Tab_Entity_Rec            BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1224     l_commit                        VARCHAR2(10);
1225     l_tab_Id                        NUMBER;
1226 BEGIN
1227     FND_MSG_PUB.Initialize;
1228     x_return_status := FND_API.G_RET_STS_SUCCESS;
1229     SAVEPOINT BSCPMFUICrtTab;
1230     l_commit            := FND_API.G_TRUE;
1231     BSC_PMF_UI_WRAPPER.Create_Tab
1232     (   p_commit             =>   FND_API.G_FALSE
1233       , p_responsibility_id  =>   p_responsibility_id
1234       , p_parent_tab_id      =>   NULL
1235       , p_owner_id           =>   NULL
1236       , x_tab_id             =>   l_tab_Id
1237       , x_return_status      =>   x_return_status
1238       , x_msg_count          =>   x_msg_count
1239       , x_msg_data           =>   x_msg_data
1240       , p_tab_name           =>   p_tab_name
1241       , p_tab_help           =>   p_tab_help
1242       , p_tab_info           =>   NULL
1243     );
1244     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1245         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1246     END IF;
1247 
1248   IF (p_commit = FND_API.G_TRUE) THEN
1249     COMMIT;
1250   END IF;
1251 EXCEPTION
1252     WHEN FND_API.G_EXC_ERROR THEN
1253         ROLLBACK TO BSCPMFUICrtTab;
1254         IF (x_msg_data IS NULL) THEN
1255             FND_MSG_PUB.Count_And_Get
1256             (      p_encoded   =>  FND_API.G_FALSE
1257                ,   p_count     =>  x_msg_count
1258                ,   p_data      =>  x_msg_data
1259             );
1260         END IF;
1261         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1262         x_return_status :=  FND_API.G_RET_STS_ERROR;
1263     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1264         ROLLBACK TO BSCPMFUICrtTab;
1265         IF (x_msg_data IS NULL) THEN
1266             FND_MSG_PUB.Count_And_Get
1267             (      p_encoded   =>  FND_API.G_FALSE
1268                ,   p_count     =>  x_msg_count
1269                ,   p_data      =>  x_msg_data
1270             );
1271         END IF;
1272         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1273         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1274     WHEN NO_DATA_FOUND THEN
1275         ROLLBACK TO BSCPMFUICrtTab;
1276         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1277         IF (x_msg_data IS NOT NULL) THEN
1278             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Tab ';
1279         ELSE
1280             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Tab ';
1281         END IF;
1282         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1283     WHEN OTHERS THEN
1284         ROLLBACK TO BSCPMFUICrtTab;
1285         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1286         IF (x_msg_data IS NOT NULL) THEN
1287             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Tab ';
1288         ELSE
1289             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Tab ';
1290         END IF;
1291         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1292 end Create_Tab;
1293 
1294 /************************************************************************************
1295 ************************************************************************************/
1296 
1297 --modified procedure to correct the way to get the tab_id
1298 -- Modifed Procedure to accept SHORT_NAME
1299 PROCEDURE Create_Tab
1300 (   p_Commit            IN          VARCHAR2 := FND_API.G_TRUE --should be false
1301   , p_Responsibility_Id IN          NUMBER
1302   , p_Parent_Tab_Id     IN          NUMBER
1303   , p_Owner_Id          IN          NUMBER
1304   , p_Short_Name        IN          VARCHAR2 := NULL
1305   , x_tab_id            OUT NOCOPY  NUMBER
1306   , x_return_status     OUT NOCOPY  VARCHAR2
1307   , x_msg_count         OUT NOCOPY  NUMBER
1308   , x_msg_data          OUT NOCOPY  VARCHAR2
1309   , p_tab_name          IN          VARCHAR2 := NULL
1310   , p_tab_help          IN          VARCHAR2 := NULL
1311   , p_tab_info          IN          VARCHAR2 := NULL
1312 ) IS
1313     l_Bsc_Tab_Entity_Rec        BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1314     l_Bsc_Tab_Entity_Rec_Out    BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1315     l_Bsc_Tab_Entity_Rec_P      BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1316     l_commit                    VARCHAR2(10);
1317     l_parent_tab_id             NUMBER := NULL;
1318 BEGIN
1319    FND_MSG_PUB.Initialize;
1320    x_return_status := FND_API.G_RET_STS_SUCCESS;
1321    SAVEPOINT BSCPMFUICrtTab1;
1322     l_commit := FND_API.G_FALSE;
1323     -- check value of parent_tab_id.  If value is -2 then the tab about to be created is
1324     -- a root tab (no parent), else this new tab will have a parent.
1325     IF (p_parent_tab_id >= 0) THEN
1326         l_parent_tab_id := p_parent_tab_id;
1327     END IF;
1328 
1329   --Call procedure to initialize Tab Entity Rec
1330   BSC_SCORECARD_PUB.Initialize_Tab_Entity_Rec(
1331                           p_Bsc_Tab_Entity_Rec => l_Bsc_Tab_Entity_Rec
1332                          ,x_Bsc_Tab_Entity_Rec => l_Bsc_Tab_Entity_Rec
1333                          ,x_return_status => x_return_status
1334                          ,x_msg_count => x_msg_count
1335                          ,x_msg_data => x_msg_data);
1336   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1337       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1338   END IF;
1339 
1340     -- set the passed values to the record.
1341     l_Bsc_Tab_Entity_Rec.Bsc_Parent_Tab_Id      := l_parent_tab_id;
1342     l_Bsc_Tab_Entity_Rec.Bsc_Responsibility_Id  := p_responsibility_id;
1343     l_Bsc_Tab_Entity_Rec.Bsc_Owner_Id           := p_owner_id;
1344     l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name           := TRIM(p_tab_name);
1345     l_Bsc_Tab_Entity_Rec.Bsc_Tab_Help           := p_tab_help;
1346     l_Bsc_Tab_Entity_Rec.Bsc_Tab_Info           := p_tab_info;
1347     l_Bsc_Tab_Entity_Rec.Bsc_Short_Name         := p_Short_Name;
1348 
1349 
1350 
1351     BSC_SCORECARD_PUB.Create_Tab( p_commit => l_commit
1352                                 , p_Bsc_Tab_Entity_Rec => l_Bsc_Tab_Entity_Rec
1353                                 , x_Bsc_Tab_Entity_Rec => l_Bsc_Tab_Entity_Rec_Out
1354                                 , x_return_status => x_return_status
1355                                 , x_msg_count => x_msg_count
1356                                 , x_msg_data => x_msg_data);
1357     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1358         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1359     END IF;
1360 
1361     x_tab_id := l_Bsc_Tab_Entity_Rec_Out.Bsc_Tab_Id;
1362 
1363     --Only for parent tab.
1364     l_Bsc_Tab_Entity_Rec_P.Bsc_Tab_Id := p_parent_tab_id;
1365 
1366     --Update the tab time stamp for parent tab.
1367     IF (p_parent_tab_id is not NULL) THEN
1368        BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
1369                                               , l_Bsc_Tab_Entity_Rec_P
1370                                               , x_return_status
1371                                               , x_msg_count
1372                                               , x_msg_data);
1373        IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1374         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1375        END IF;
1376     END IF;
1377 
1378     --update the system time stamp.
1379     BSC_SCORECARD_PUB.Update_System_Time_Stamp( FND_API.G_FALSE
1380                                               , l_Bsc_Tab_Entity_Rec
1381                                               , x_return_status
1382                                               , x_msg_count
1383                                               , x_msg_data);
1384     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1385         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1386     END IF;
1387 
1388   IF (p_commit = FND_API.G_TRUE) THEN
1389     COMMIT;
1390   END IF;
1391 
1392     --DBMS_OUTPUT.PUT_LINE('Created tab_id '||x_tab_id);
1393 
1394 
1395 EXCEPTION
1396     WHEN FND_API.G_EXC_ERROR THEN
1397         ROLLBACK TO BSCPMFUICrtTab1;
1398         IF (x_msg_data IS NULL) THEN
1399             FND_MSG_PUB.Count_And_Get
1400             (      p_encoded   =>  FND_API.G_FALSE
1401                ,   p_count     =>  x_msg_count
1402                ,   p_data      =>  x_msg_data
1403             );
1404         END IF;
1405         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1406         x_return_status :=  FND_API.G_RET_STS_ERROR;
1407     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1408         ROLLBACK TO BSCPMFUICrtTab1;
1409         IF (x_msg_data IS NULL) THEN
1410             FND_MSG_PUB.Count_And_Get
1411             (      p_encoded   =>  FND_API.G_FALSE
1412                ,   p_count     =>  x_msg_count
1413                ,   p_data      =>  x_msg_data
1414             );
1415         END IF;
1416         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1417         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1418     WHEN NO_DATA_FOUND THEN
1419         ROLLBACK TO BSCPMFUICrtTab1;
1420         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1421         IF (x_msg_data IS NOT NULL) THEN
1422             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Tab ';
1423         ELSE
1424             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Tab ';
1425         END IF;
1426         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1427     WHEN OTHERS THEN
1428         ROLLBACK TO BSCPMFUICrtTab1;
1429         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1430         IF (x_msg_data IS NOT NULL) THEN
1431             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Tab ';
1432         ELSE
1433             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Tab ';
1434         END IF;
1435         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1436 
1437 end Create_Tab;
1438 
1439 
1440 /************************************************************************************
1441 ************************************************************************************/
1442 
1443 -- this procedure updates a tab in BSC.  No commits are passed to the
1444 -- BSC APIs because these commits will undo the indicator lock.  The commit is
1445 -- executed at the end of this procedure.
1446 
1447 procedure Update_Tab(
1448   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1449  ,p_tab_id    IN      number
1450  ,p_tab_name            IN      varchar2
1451  ,x_return_status       OUT NOCOPY     varchar2
1452  ,x_msg_count           OUT NOCOPY     number
1453  ,x_msg_data            OUT NOCOPY     varchar2
1454  ,p_tab_help            IN      varchar2 DEFAULT null
1455 ) is
1456 
1457 l_Bsc_Tab_Entity_Rec            BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1458 
1459 l_commit                        varchar2(10);
1460 
1461 begin
1462    FND_MSG_PUB.Initialize;
1463    x_return_status := FND_API.G_RET_STS_SUCCESS;
1464    SAVEPOINT BSCPMFUIUpdTab;
1465    BSC_PMF_UI_WRAPPER.Update_Tab
1466    (    p_commit        =>  FND_API.G_FALSE
1467      ,  p_tab_id        =>  p_tab_id
1468      ,  p_owner_id      =>  NULL
1469      ,  p_tab_name      =>  p_tab_name
1470      ,  x_return_status =>  x_return_status
1471      ,  x_msg_count     =>  x_msg_count
1472      ,  x_msg_data      =>  x_msg_data
1473      ,  p_tab_help      =>  p_tab_help
1474      ,  p_tab_info      =>  NULL
1475      ,  p_time_stamp    =>  NULL
1476     );
1477     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1478         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1479     END IF;
1480 
1481 
1482   IF (p_commit = FND_API.G_TRUE) THEN
1483     COMMIT;
1484   END IF;
1485 
1486 EXCEPTION
1487     WHEN FND_API.G_EXC_ERROR THEN
1488         ROLLBACK TO BSCPMFUIUpdTab;
1489         IF (x_msg_data IS NULL) THEN
1490             FND_MSG_PUB.Count_And_Get
1491             (      p_encoded   =>  FND_API.G_FALSE
1492                ,   p_count     =>  x_msg_count
1493                ,   p_data      =>  x_msg_data
1494             );
1495         END IF;
1496         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1497         x_return_status :=  FND_API.G_RET_STS_ERROR;
1498     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1499         ROLLBACK TO BSCPMFUIUpdTab;
1500         IF (x_msg_data IS NULL) THEN
1501             FND_MSG_PUB.Count_And_Get
1502             (      p_encoded   =>  FND_API.G_FALSE
1503                ,   p_count     =>  x_msg_count
1504                ,   p_data      =>  x_msg_data
1505             );
1506         END IF;
1507         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1508         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1509     WHEN NO_DATA_FOUND THEN
1510         ROLLBACK TO BSCPMFUIUpdTab;
1511         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1512         IF (x_msg_data IS NOT NULL) THEN
1513             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab ';
1514         ELSE
1515             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab ';
1516         END IF;
1517         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1518     WHEN OTHERS THEN
1519         ROLLBACK TO BSCPMFUIUpdTab;
1520         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1521         IF (x_msg_data IS NOT NULL) THEN
1522             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab ';
1523         ELSE
1524             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab ';
1525         END IF;
1526         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1527 
1528 end Update_Tab;
1529 
1530 /************************************************************************************
1531 ************************************************************************************/
1532 
1533 -- this procedure updates a tab in BSC.  No commits are passed to the
1534 -- BSC APIs because these commits will undo the indicator lock.  The commit is
1535 -- executed at the end of this procedure.
1536 
1537 procedure Update_Tab(
1538   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1539  ,p_tab_id    IN      number
1540  ,p_owner_id    IN  number
1541  ,p_tab_name            IN      varchar2
1542  ,x_return_status       OUT NOCOPY     varchar2
1543  ,x_msg_count           OUT NOCOPY     number
1544  ,x_msg_data            OUT NOCOPY     varchar2
1545  ,p_tab_help            IN      varchar2 DEFAULT null
1546  ,p_tab_info            IN      varchar2 DEFAULT null
1547  ,p_time_stamp          IN      VARCHAR2 := NULL
1548 
1549 ) is
1550 
1551 l_Bsc_Tab_Entity_Rec            BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
1552 
1553   l_commit                        varchar2(10);
1554   l_Count                       NUMBER := 0;
1555   CURSOR c_Tab_Name IS
1556   SELECT Name
1557   FROM   BSC_TABS_VL
1558   WHERE  Tab_Id = l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id;
1559 begin
1560    FND_MSG_PUB.Initialize;
1561    x_return_status := FND_API.G_RET_STS_SUCCESS;
1562    SAVEPOINT BSCPMFUIUpdTab1;
1563   l_commit := FND_API.G_FALSE;
1564 
1565   IF (c_Tab_Name%ISOPEN) THEN
1566     CLOSE c_Tab_Name;
1567   END IF;
1568   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id   := p_tab_id;
1569   OPEN c_Tab_Name;
1570       FETCH c_Tab_Name INTO l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name;
1571   CLOSE c_Tab_Name;
1572   IF (l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name IS NULL) THEN
1573     FND_MESSAGE.SET_NAME('BSC','BSC_MUSER_DELETE_MESSAGE');
1574     FND_MESSAGE.SET_TOKEN('TYPE', BSC_APPS.Get_Lookup_Value('BSC_UI_IVIEWER', 'SCORECARD'), TRUE);
1575     FND_MSG_PUB.ADD;
1576     RAISE FND_API.G_EXC_ERROR;
1577   END IF;
1578   IF ((p_tab_name IS NOT NULL) AND (l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name <> p_tab_name)) THEN
1579       l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name := TRIM(p_tab_name);
1580 
1581       SELECT COUNT(*) INTO l_Count
1582       FROM   BSC_TABS_VL
1583       WHERE  Tab_Id     <> l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id
1584       AND    UPPER(Name) = UPPER(l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name);
1585       IF (l_Count <> 0) THEN
1586           FND_MESSAGE.SET_NAME('BSC',      'BSC_TAB_NAME_EXISTS');
1587           FND_MESSAGE.SET_TOKEN('BSC_TAB',  l_Bsc_Tab_Entity_Rec.Bsc_Tab_Name);
1588           FND_MSG_PUB.ADD;
1589           RAISE FND_API.G_EXC_ERROR;
1590       END IF;
1591   END IF;
1592 
1593   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Help := p_tab_help;
1594   l_Bsc_Tab_Entity_Rec.Bsc_Owner_Id := p_owner_id;
1595   if p_tab_info is null then
1596         -- Set space when is null in order to delete the contens of the column --
1597   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Info := ' ';
1598   else
1599     l_Bsc_Tab_Entity_Rec.Bsc_Tab_Info := p_tab_info;
1600   end if;
1601 
1602   --set some default values
1603   l_Bsc_Tab_Entity_Rec.Bsc_Language := 'US';
1604   l_Bsc_Tab_Entity_Rec.Bsc_Source_Language := 'US';
1605   BSC_BIS_LOCKS_PUB.LOCK_TAB(
1606            p_tab_id          => p_tab_id
1607           ,p_time_stamp             => p_time_stamp  -- Granular Locking
1608           ,x_return_status          => x_return_status
1609           ,x_msg_count              => x_msg_count
1610           ,x_msg_data               => x_msg_data
1611   );
1612     IF ((x_return_status IS NOT NULL)AND(x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1613          RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1614     END IF;
1615 
1616 
1617   BSC_SCORECARD_PUB.Update_Tab( FND_API.G_FALSE
1618                                ,l_Bsc_Tab_Entity_Rec
1619                                ,x_return_status
1620                                ,x_msg_count
1621                                ,x_msg_data);
1622     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1623         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1624     END IF;
1625 
1626   BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
1627                                           ,l_Bsc_Tab_Entity_Rec
1628                                           ,x_return_status
1629                                           ,x_msg_count
1630                                           ,x_msg_data);
1631     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1632         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1633     END IF;
1634 
1635   BSC_SCORECARD_PUB.Update_System_Time_Stamp( FND_API.G_FALSE
1636                                              ,l_Bsc_Tab_Entity_Rec
1637                                              ,x_return_status
1638                                              ,x_msg_count
1639                                              ,x_msg_data);
1640     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1641         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1642     END IF;
1643 
1644   IF (p_commit = FND_API.G_TRUE) THEN
1645     COMMIT;
1646   END IF;
1647 --DBMS_OUTPUT.PUT_LINE('End Update_Tab ');
1648 
1649 EXCEPTION
1650     WHEN FND_API.G_EXC_ERROR THEN
1651         ROLLBACK TO BSCPMFUIUpdTab1;
1652         IF (c_Tab_Name%ISOPEN) THEN
1653             CLOSE c_Tab_Name;
1654         END IF;
1655         IF (x_msg_data IS NULL) THEN
1656             FND_MSG_PUB.Count_And_Get
1657             (      p_encoded   =>  FND_API.G_FALSE
1658                ,   p_count     =>  x_msg_count
1659                ,   p_data      =>  x_msg_data
1660             );
1661         END IF;
1662         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
1663         x_return_status :=  FND_API.G_RET_STS_ERROR;
1664     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1665         ROLLBACK TO BSCPMFUIUpdTab1;
1666         IF (c_Tab_Name%ISOPEN) THEN
1667             CLOSE c_Tab_Name;
1668         END IF;
1669         IF (x_msg_data IS NULL) THEN
1670             FND_MSG_PUB.Count_And_Get
1671             (      p_encoded   =>  FND_API.G_FALSE
1672                ,   p_count     =>  x_msg_count
1673                ,   p_data      =>  x_msg_data
1674             );
1675         END IF;
1676         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1677         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
1678     WHEN NO_DATA_FOUND THEN
1679         ROLLBACK TO BSCPMFUIUpdTab1;
1680         IF (c_Tab_Name%ISOPEN) THEN
1681             CLOSE c_Tab_Name;
1682         END IF;
1683         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1684         IF (x_msg_data IS NOT NULL) THEN
1685             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab ';
1686         ELSE
1687             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab ';
1688         END IF;
1689         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
1690     WHEN OTHERS THEN
1691         ROLLBACK TO BSCPMFUIUpdTab1;
1692         IF (c_Tab_Name%ISOPEN) THEN
1693             CLOSE c_Tab_Name;
1694         END IF;
1695         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1696         IF (x_msg_data IS NOT NULL) THEN
1697             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab ';
1698         ELSE
1699             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab ';
1700         END IF;
1701         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
1702 
1703 end Update_Tab;
1704 
1705 /************************************************************************************
1706 ************************************************************************************/
1707 
1708 procedure Update_Analysis_Option(
1709   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1710  ,p_kpi_id              IN      number
1711  ,p_option_group_id IN  number
1712  ,p_option_id   IN  number
1713  ,p_option_name   IN  varchar2
1714  ,x_return_status       OUT NOCOPY     varchar2
1715  ,x_msg_count           OUT NOCOPY     number
1716  ,x_msg_data            OUT NOCOPY     varchar2
1717  ,p_option_help            IN      varchar2 DEFAULT null
1718 ) is
1719 
1720 l_Bsc_Option_Rec    BSC_ANALYSIS_OPTION_PUB.Bsc_Option_Rec_Type;
1721 l_Bsc_Kpi_Entity_Rec            BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
1722 
1723 l_commit                        varchar2(10);
1724 
1725 begin
1726    FND_MSG_PUB.Initialize;
1727    x_return_status := FND_API.G_RET_STS_SUCCESS;
1728    SAVEPOINT BSCPMFUIUpdAnaOpt;
1729   l_commit := FND_API.G_FALSE;
1730 
1731   l_Bsc_Option_Rec.Bsc_Kpi_Id := p_kpi_id;
1732   l_Bsc_Option_Rec.Bsc_Analysis_Group_Id := p_option_group_id;
1733   l_Bsc_Option_Rec.Bsc_Analysis_Option_Id := p_option_id;
1734   l_Bsc_Option_Rec.Bsc_Option_Name := p_option_name;
1735   l_Bsc_Option_Rec.Bsc_Option_Help := p_option_help;
1736   l_Bsc_Option_Rec.Bsc_Option_Group0 := p_option_group_id;
1737   l_Bsc_Option_Rec.Bsc_Option_Group1 := 0;
1738   l_Bsc_Option_Rec.Bsc_Option_Group2 := 0;
1739 
1740   -- set some default values.
1741   l_Bsc_Option_Rec.Bsc_Language := 'US';
1742   l_Bsc_Option_Rec.Bsc_Source_Language := 'US';
1743 
1744   BSC_ANALYSIS_OPTION_PUB.Update_Analysis_Options( FND_API.G_FALSE
1745                                                   ,l_Bsc_Option_Rec
1746                                                   ,x_return_status
1747                                                   ,x_msg_count
1748                                                   ,x_msg_data);
1749     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1750         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1751     END IF;
1752 
1753   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
1754   BSC_KPI_PUB.Update_Kpi_Time_Stamp( FND_API.G_FALSE
1755                                     ,l_Bsc_Kpi_Entity_Rec
1756                                     ,x_return_status
1757                                     ,x_msg_count
1758                                     ,x_msg_data);
1759     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1760         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1761     END IF;
1762 
1763   IF (p_commit = FND_API.G_TRUE) THEN
1764     COMMIT;
1765   END IF;
1766 
1767 EXCEPTION
1768   WHEN FND_API.G_EXC_ERROR THEN
1769     rollback to BSCPMFUIUpdAnaOpt;
1770     x_return_status := FND_API.G_RET_STS_ERROR;
1771     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
1772                               ,p_data   =>      x_msg_data);
1773   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1774     rollback to BSCPMFUIUpdAnaOpt;
1775     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1776     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
1777                               ,p_data     =>      x_msg_data);
1778   WHEN NO_DATA_FOUND THEN
1779     rollback to BSCPMFUIUpdAnaOpt;
1780     x_return_status := FND_API.G_RET_STS_ERROR;
1781     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
1782                               ,p_data     =>      x_msg_data);
1783   WHEN OTHERS THEN
1784     rollback to BSCPMFUIUpdAnaOpt;
1785     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1786     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
1787                               ,p_data     =>      x_msg_data);
1788 
1789 end Update_Analysis_Option;
1790 
1791 /************************************************************************************
1792 ************************************************************************************/
1793 
1794 procedure Delete_Analysis_Option(
1795   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
1796  ,p_kpi_id    IN  number
1797  ,p_option_group_id IN  number
1798  ,p_option_id   IN  number
1799  ,x_return_status       OUT NOCOPY     varchar2
1800  ,x_msg_count           OUT NOCOPY     number
1801  ,x_msg_data            OUT NOCOPY     varchar2
1802 ) is
1803 
1804 TYPE Recdc_opt      IS REF CURSOR;
1805 dc_opt        Recdc_opt;
1806 dc_shr        Recdc_opt;
1807 
1808 TYPE Recdc_opt1     IS REF CURSOR;
1809 dc_opt1       Recdc_opt1;
1810 
1811 l_Bsc_Anal_Opt_Rec    BSC_ANALYSIS_OPTION_PUB.Bsc_Option_Rec_Type;
1812 l_Bsc_Kpi_Entity_Rec            BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
1813 l_Bsc_Dim_Set_Rec_Type    BSC_DIMENSION_SETS_PUB.Bsc_Dim_Set_Rec_Type;
1814 
1815 l_commit      varchar2(10);
1816 l_opt_sql     varchar2(1000);
1817 l_opt_sql1      varchar2(1000);
1818 l_shr_sql     varchar2(1000);
1819 
1820 l_dim_set_id      number;
1821 l_count       number;
1822 l_ind_tab_count                 number;
1823 l_tab_id                        number;
1824 
1825 begin
1826    FND_MSG_PUB.Initialize;
1827    x_return_status := FND_API.G_RET_STS_SUCCESS;
1828   l_commit := FND_API.G_FALSE;
1829 
1830   -- set some of the values for the Record Type
1831   l_Bsc_Anal_Opt_Rec.Bsc_Kpi_Id := p_kpi_id;
1832   l_Bsc_Anal_Opt_Rec.Bsc_Analysis_Group_Id := p_option_group_id;
1833   l_Bsc_Anal_Opt_Rec.Bsc_Analysis_Option_Id := p_option_id;
1834 
1835   -- we also need to normalize the values for column ANALYSYS_OPTION0,
1836   -- ANALYSYS_OPTION1, ANALYSYS_OPTION2 based on group id.
1837   if p_option_group_id = 0 then
1838     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group0 := p_option_id;
1839     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group1 := 0;
1840     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group2 := 0;
1841   elsif p_option_group_id = 1 then
1842     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group1 := p_option_id;
1843     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group0 := 0;
1844     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group2 := 0;
1845   else
1846     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group2 := p_option_id;
1847     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group0 := 0;
1848     l_Bsc_Anal_Opt_Rec.Bsc_Option_Group1 := 0;
1849   end if;
1850 
1851   --Determine if Indicator assigned to a tab.
1852   select count(indicator)
1853     into l_ind_tab_count
1854     from BSC_TAB_INDICATORS
1855    where indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
1856 
1857   -- if indicator assigned to tab get tab id.
1858   if l_ind_tab_count > 0 then
1859 
1860     select tab_id
1861       into l_tab_id
1862     from BSC_TAB_INDICATORS
1863    where indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
1864 
1865   end if;
1866 
1867 
1868   -- obtain dimension set id this option is using.
1869   select dim_set_id
1870     into l_dim_set_id
1871     from BSC_KPI_ANALYSIS_OPTIONS_B
1872    where indicator = p_kpi_id
1873      and analysis_group_id = p_option_group_id
1874      and option_id = p_option_id;
1875 
1876 
1877   -- Call Analysis Option API.
1878   BSC_ANALYSIS_OPTION_PUB.Delete_Analysis_Options( FND_API.G_FALSE
1879                                                   ,l_Bsc_Anal_Opt_Rec
1880                                                   ,x_return_status
1881                                                   ,x_msg_count
1882                                                   ,x_msg_data);
1883     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1884         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1885     END IF;
1886 
1887   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
1888   BSC_KPI_PUB.Update_Kpi_Time_Stamp( FND_API.G_FALSE
1889                                     ,l_Bsc_Kpi_Entity_Rec
1890                                     ,x_return_status
1891                                     ,x_msg_count
1892                                     ,x_msg_data);
1893     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1894         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1895     END IF;
1896 
1897   -- Determine if the dimension set is being used by other options.
1898   select count(option_id)
1899     into l_count
1900     from BSC_KPI_ANALYSIS_OPTIONS_B
1901    where indicator = p_kpi_id
1902      and dim_set_id = l_dim_set_id;
1903 
1904   -- If there are no more options using this dim set delete it.
1905   if l_count = 0 then
1906 
1907     --populate Dimension Set Record.
1908     l_Bsc_Dim_Set_Rec_Type.Bsc_Dim_Set_Id := l_dim_set_id;
1909 
1910 
1911     -- If deleting the dimension set, then delete it from all indicators,
1912     -- master,  and shared.
1913 
1914     l_shr_sql := 'select indicator ' ||
1915                  '  from BSC_KPIS_B ' ||
1916                  ' where indicator = :1' ||
1917                  '    or source_indicator = :2';
1918 
1919     open dc_shr for l_shr_sql using p_kpi_id, p_kpi_id;
1920       loop
1921         fetch dc_shr into l_Bsc_Dim_Set_Rec_Type.Bsc_Kpi_Id ;
1922         exit when dc_shr%NOTFOUND;
1923 
1924         --Determine if Indicator assigned to a tab.
1925         select count(indicator)
1926           into l_ind_tab_count
1927           from BSC_TAB_INDICATORS
1928          where indicator = l_Bsc_Dim_Set_Rec_Type.Bsc_Kpi_Id;
1929 
1930         -- if indicator assigned to tab get indicator.
1931         if l_ind_tab_count > 0 then
1932           select tab_id
1933             into l_tab_id
1934           from BSC_TAB_INDICATORS
1935          where indicator = l_Bsc_Dim_Set_Rec_Type.Bsc_Kpi_Id;
1936         end if;
1937 
1938 
1939 
1940         -- get dimension groups in this dimension set for this indicator.
1941         l_opt_sql := 'select dim_group_id ' ||
1942                  '  from BSC_KPI_DIM_GROUPS ' ||
1943                  ' where indicator = :1' ||
1944                  '   and dim_set_id = :2';
1945 
1946         open dc_opt for l_opt_sql using l_Bsc_Dim_Set_Rec_Type.Bsc_Kpi_Id, l_Bsc_Dim_Set_Rec_Type.Bsc_Dim_Set_Id;
1947           loop
1948             fetch dc_opt into l_Bsc_Dim_Set_Rec_Type.Bsc_Dim_Level_Group_Id;
1949             exit when dc_opt%NOTFOUND;
1950 
1951             BSC_DIMENSION_SETS_PVT.Delete_Dim_Group_In_Dset( FND_API.G_FALSE
1952                                                             ,l_Bsc_Dim_Set_Rec_Type
1953                                                             ,x_return_status
1954                                                             ,x_msg_count
1955                                                             ,x_msg_data);
1956     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1957         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1958     END IF;
1959 
1960 --        l_Bsc_Dim_Set_Rec_Type.Bsc_Action := 'RESET';
1961 
1962           end loop;
1963         close dc_opt;
1964 
1965         -- Delete the levels from the kpi and dimension set.
1966         delete from BSC_KPI_DIM_LEVEL_PROPERTIES
1967          where indicator = l_Bsc_Dim_Set_Rec_Type.Bsc_Kpi_Id
1968            and dim_set_id = l_dim_set_id;
1969 
1970 
1971         BSC_DIMENSION_SETS_PVT.Delete_Dim_Levels( FND_API.G_FALSE
1972                                                  ,l_Bsc_Dim_Set_Rec_Type
1973                                                  ,x_return_status
1974                                                  ,x_msg_count
1975                                                  ,x_msg_data);
1976     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1977         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1978     END IF;
1979 
1980         BSC_DIMENSION_SETS_PUB.Delete_Bsc_Kpi_Dim_Sets_Tl( FND_API.G_FALSE
1981                                                           ,l_Bsc_Dim_Set_Rec_Type
1982                                                           ,x_return_status
1983                                                           ,x_msg_count
1984                                                           ,x_msg_data);
1985     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1986         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1987     END IF;
1988 
1989         -- Need to call procedure for list button logic.
1990         BSC_COMMON_DIM_LEVELS_PUB.Check_Common_Dim_Levels( FND_API.G_FALSE
1991                                                           ,l_tab_id
1992                                                           ,x_return_status
1993                                                           ,x_msg_count
1994                                                           ,x_msg_data);
1995     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
1996         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
1997     END IF;
1998 
1999        end loop;
2000 
2001     close dc_shr;
2002 
2003   end if;
2004 
2005   IF (p_commit = FND_API.G_TRUE) THEN
2006     COMMIT;
2007   END IF;
2008 
2009 EXCEPTION
2010   WHEN FND_API.G_EXC_ERROR THEN
2011     rollback;
2012     x_return_status := FND_API.G_RET_STS_ERROR;
2013     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
2014                               ,p_data   =>      x_msg_data);
2015   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2016     rollback;
2017     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2018     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
2019                               ,p_data     =>      x_msg_data);
2020   WHEN NO_DATA_FOUND THEN
2021     rollback;
2022     x_return_status := FND_API.G_RET_STS_ERROR;
2023     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
2024                               ,p_data     =>      x_msg_data);
2025   WHEN OTHERS THEN
2026     rollback;
2027     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2028     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
2029                               ,p_data     =>      x_msg_data);
2030 
2031 
2032 
2033 end Delete_Analysis_Option;
2034 
2035 /************************************************************************************
2036 ************************************************************************************/
2037 
2038 procedure Delete_Kpi(
2039   p_commit              IN            VARCHAR2 := FND_API.G_TRUE
2040  ,p_kpi_id              IN            NUMBER
2041  ,x_return_status       OUT NOCOPY    VARCHAR2
2042  ,x_msg_count           OUT NOCOPY    NUMBER
2043  ,x_msg_data            OUT NOCOPY    VARCHAR2
2044 ) is
2045 
2046 l_Bsc_Kpi_Entity        BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
2047 l_Bsc_Tab_Entity_Rec    BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
2048 l_commit                VARCHAR2(10);
2049 l_count                 NUMBER;
2050 l_Tab_Id                BSC_TABS_B.Tab_Id%TYPE;
2051 l_Row_Count             NUMBER;
2052 l_Tabs_Tbl_Type         BSC_PMF_UI_WRAPPER.Bsc_Tabs_Tbl_Rec;
2053 
2054 
2055 CURSOR c_Tab_Id IS
2056 SELECT DISTINCT A.Tab_Id
2057 FROM   BSC_TAB_INDICATORS     A
2058      , BSC_KPIS_B             B
2059      , BSC_SYS_FILTERS_VIEWS  C
2060 WHERE  A.Indicator    = B.Indicator
2061 AND    C.Source_Type  = BSC_DIM_FILTERS_PUB.Source_Type_Tab
2062 AND    C.Source_Code  = A.Tab_Id
2063 AND    ((B.Indicator  = p_kpi_id) OR (B.Source_Indicator = p_kpi_id));
2064 
2065 begin
2066    FND_MSG_PUB.Initialize;
2067    x_return_status := FND_API.G_RET_STS_SUCCESS;
2068    SAVEPOINT BSCDelKPIWrapper;
2069   l_commit := FND_API.G_TRUE;
2070 
2071   -- check if this Indicator has been assigned to a Tab.
2072   select count(tab_id)
2073     into l_count
2074     from BSC_TAB_INDICATORS
2075    where indicator = p_kpi_id;
2076 
2077   if l_count > 0 then
2078     -- Get the tab id for this indicator.
2079     select tab_id
2080     into l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id
2081     from BSC_TAB_INDICATORS
2082     where indicator = p_kpi_id;
2083   end if;
2084 
2085   -- set some of the values for the Record Type
2086   l_Bsc_Kpi_Entity.Bsc_Kpi_Id := p_kpi_id;
2087 
2088   l_Row_Count := 0;
2089 
2090   FOR cd IN c_Tab_Id LOOP
2091    l_Tabs_Tbl_Type(l_Row_Count).Bsc_tab_id := cd.Tab_Id;
2092    l_Row_Count := l_Row_Count + 1;
2093   END LOOP;
2094 
2095   BSC_KPI_PUB.Delete_Kpi( FND_API.G_FALSE
2096                          ,l_Bsc_Kpi_Entity
2097                          ,x_return_status
2098                          ,x_msg_count
2099                          ,x_msg_data);
2100     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
2101         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2102     END IF;
2103   -- if indicator assigned to a tab then get tab id and update time stamp.
2104 
2105   FOR i IN 0..(l_Tabs_Tbl_Type.COUNT-1 ) LOOP
2106     BSC_DIM_FILTERS_PUB.Check_Filters_Not_Apply
2107       (   p_Tab_Id         =>  l_Tabs_Tbl_Type(i).Bsc_tab_id
2108        ,  x_return_status  =>  x_return_status
2109        ,  x_msg_count      =>  x_msg_count
2110        ,  x_msg_data       =>  x_msg_data
2111       );
2112     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
2113         --DBMS_OUTPUT.PUT_LINE('BSC_PMF_UI_WRAPPER.Delete_Kpi Failed: at BSC_DIM_FILTERS_PUB.Check_Filters_Not_Apply');
2114         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2115     END IF;
2116   END LOOP;
2117 
2118   if l_count > 0 then
2119 
2120     BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
2121                                             ,l_Bsc_Tab_Entity_Rec
2122                                             ,x_return_status
2123                                             ,x_msg_count
2124                                             ,x_msg_data);
2125     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2126         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2127     END IF;
2128 
2129   end if;
2130 
2131 
2132   IF (p_commit = FND_API.G_TRUE) THEN
2133     COMMIT;
2134   END IF;
2135 
2136 EXCEPTION
2137     WHEN FND_API.G_EXC_ERROR THEN
2138 
2139         IF(c_Tab_Id%ISOPEN) THEN
2140           CLOSE c_Tab_Id;
2141         END IF;
2142 
2143         ROLLBACK TO BSCDelKPIWrapper;
2144         IF (x_msg_data IS NULL) THEN
2145             FND_MSG_PUB.Count_And_Get
2146             (      p_encoded   =>  FND_API.G_FALSE
2147                ,   p_count     =>  x_msg_count
2148                ,   p_data      =>  x_msg_data
2149             );
2150         END IF;
2151         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
2152         x_return_status :=  FND_API.G_RET_STS_ERROR;
2153     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2154 
2155         IF(c_Tab_Id%ISOPEN) THEN
2156            CLOSE c_Tab_Id;
2157         END IF;
2158         ROLLBACK TO BSCDelKPIWrapper;
2159         IF (x_msg_data IS NULL) THEN
2160             FND_MSG_PUB.Count_And_Get
2161             (      p_encoded   =>  FND_API.G_FALSE
2162                ,   p_count     =>  x_msg_count
2163                ,   p_data      =>  x_msg_data
2164             );
2165         END IF;
2166         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2167         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
2168     WHEN NO_DATA_FOUND THEN
2169 
2170         IF(c_Tab_Id%ISOPEN) THEN
2171            CLOSE c_Tab_Id;
2172         END IF;
2173         ROLLBACK TO BSCDelKPIWrapper;
2174         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2175         IF (x_msg_data IS NOT NULL) THEN
2176             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Kpi ';
2177         ELSE
2178             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Kpi ';
2179         END IF;
2180         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
2181     WHEN OTHERS THEN
2182 
2183         IF(c_Tab_Id%ISOPEN) THEN
2184            CLOSE c_Tab_Id;
2185         END IF;
2186         ROLLBACK TO BSCDelKPIWrapper;
2187         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2188         IF (x_msg_data IS NOT NULL) THEN
2189             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Kpi ';
2190         ELSE
2191             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Kpi ';
2192         END IF;
2193         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
2194 end Delete_Kpi;
2195 
2196 /************************************************************************************
2197 ************************************************************************************/
2198 
2199 procedure Delete_Kpi_Group(
2200   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
2201  ,p_kpi_group_id  IN      number
2202  ,p_tab_id    IN  number
2203  ,x_return_status       OUT NOCOPY     varchar2
2204  ,x_msg_count           OUT NOCOPY     number
2205  ,x_msg_data            OUT NOCOPY     varchar2
2206 ) is
2207 
2208   l_Bsc_Kpi_Group                 BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
2209   l_commit                        varchar2(10);
2210   l_count                         NUMBER;
2211   l_group_name                    BSC_TAB_IND_GROUPS_TL.name%TYPE;
2212 begin
2213    FND_MSG_PUB.Initialize;
2214    x_return_status := FND_API.G_RET_STS_SUCCESS;
2215    SAVEPOINT BSCDelKPIGrpWrapper;
2216   l_commit := FND_API.G_TRUE;
2217 
2218   -- set some of the values for the Record Type
2219   l_Bsc_Kpi_Group.Bsc_Kpi_Group_Id := p_kpi_group_id;
2220   l_Bsc_Kpi_Group.Bsc_Tab_Id       := p_tab_id;
2221   --before deleteing indicator group, delete all the indicators first
2222 
2223   BSC_KPI_GROUP_PUB.Delete_Kpi_Group( FND_API.G_FALSE
2224                                      ,l_Bsc_Kpi_Group
2225                                      ,x_return_status
2226                                      ,x_msg_count
2227                                      ,x_msg_data);
2228     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
2229         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2230     END IF;
2231   IF (p_commit = FND_API.G_TRUE) THEN
2232     COMMIT;
2233   END IF;
2234 
2235 EXCEPTION
2236     WHEN FND_API.G_EXC_ERROR THEN
2237         ROLLBACK TO BSCDelKPIGrpWrapper;
2238         IF (x_msg_data IS NULL) THEN
2239             FND_MSG_PUB.Count_And_Get
2240             (      p_encoded   =>  FND_API.G_FALSE
2241                ,   p_count     =>  x_msg_count
2242                ,   p_data      =>  x_msg_data
2243             );
2244         END IF;
2245         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
2246         x_return_status :=  FND_API.G_RET_STS_ERROR;
2247     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2248         ROLLBACK TO BSCDelKPIGrpWrapper;
2249         IF (x_msg_data IS NULL) THEN
2250             FND_MSG_PUB.Count_And_Get
2251             (      p_encoded   =>  FND_API.G_FALSE
2252                ,   p_count     =>  x_msg_count
2253                ,   p_data      =>  x_msg_data
2254             );
2255         END IF;
2256         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2257         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
2258     WHEN NO_DATA_FOUND THEN
2259         ROLLBACK TO BSCDelKPIGrpWrapper;
2260         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2261         IF (x_msg_data IS NOT NULL) THEN
2262             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Kpi_Group ';
2263         ELSE
2264             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Kpi_Group ';
2265         END IF;
2266         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
2267     WHEN OTHERS THEN
2268         ROLLBACK TO BSCDelKPIGrpWrapper;
2269         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2270         IF (x_msg_data IS NOT NULL) THEN
2271             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Kpi_Group ';
2272         ELSE
2273             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Kpi_Group ';
2274         END IF;
2275         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
2276 
2277 end Delete_Kpi_Group;
2278 
2279 /************************************************************************************
2280 ************************************************************************************/
2281 
2282 -- this procedure deletes a Tab in BSC.  No commits are passed to the
2283 -- BSC APIs because these commits will undo the indicator lock.  The commit is
2284 -- executed at the end of this procedure.
2285 
2286 procedure Delete_Tab(
2287   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
2288  ,p_tab_id              IN      number
2289  ,x_return_status       OUT NOCOPY     varchar2
2290  ,x_msg_count           OUT NOCOPY     number
2291  ,x_msg_data            OUT NOCOPY     varchar2
2292 ) is
2293 
2294   l_Bsc_Tab_Entity                 BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
2295   l_Bsc_Kpi_Entity        BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
2296   l_commit                        varchar2(10);
2297 
2298   CURSOR c_Share_Ind IS
2299   SELECT DISTINCT K.Indicator
2300   FROM   BSC_KPIS_VL        K
2301        , BSC_TAB_INDICATORS T
2302   WHERE  K.Indicator    =  T.Indicator
2303   AND    K.Share_Flag   =  2
2304   AND    T.Tab_Id       =  p_tab_id;
2305 
2306 
2307 begin
2308    FND_MSG_PUB.Initialize;
2309    x_return_status := FND_API.G_RET_STS_SUCCESS;
2310    SAVEPOINT BSCPMFUIDelete;
2311   l_commit := FND_API.G_FALSE;
2312 
2313     FOR cd_Share_Ind IN c_Share_Ind LOOP
2314         -- call the procedure to delete share kpi from scorecard;
2315         -- Master Kpi will not be deleted.
2316         l_Bsc_Kpi_Entity.Bsc_Kpi_Id := cd_Share_Ind.Indicator;
2317         BSC_KPI_PUB.Delete_Kpi
2318         (   p_commit                =>  FND_API.G_FALSE
2319           , p_Bsc_Kpi_Entity_Rec    =>  l_Bsc_Kpi_Entity
2320           , x_return_status         =>  x_return_status
2321           , x_msg_count             =>  x_msg_count
2322           , x_msg_data              =>  x_msg_data
2323         );
2324         IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2325             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2326         END IF;
2327     END LOOP;
2328 
2329   -- set some of the values for the Record Type
2330   l_Bsc_Tab_Entity.Bsc_Tab_Id := p_tab_id;
2331 
2332   BSC_SCORECARD_PUB.Delete_Tab( FND_API.G_FALSE
2333                                ,l_Bsc_Tab_Entity
2334                                ,x_return_status
2335                                ,x_msg_count
2336                                ,x_msg_data);
2337     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2338         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2339     END IF;
2340 
2341   BSC_SCORECARD_PUB.Update_System_Time_Stamp( FND_API.G_FALSE
2342                                              ,l_Bsc_Tab_Entity
2343                                              ,x_return_status
2344                                              ,x_msg_count
2345                                              ,x_msg_data);
2346     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2347         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2348     END IF;
2349 
2350 
2351   IF (p_commit = FND_API.G_TRUE) THEN
2352     COMMIT;
2353   END IF;
2354 
2355   -- Fix for bug 3459282
2356   -- Delete the Dim Object Filters Associated to the Scorecard.
2357   -- It needs to be at the final it deletes view objects
2358    BSC_DIM_FILTERS_PUB.Drop_Filter_By_Tab (
2359            p_Tab_Id             =>   p_Tab_Id
2360        ,   x_return_status      =>   x_return_status
2361        ,   x_msg_COUNT          =>   x_msg_COUNT
2362        ,   x_msg_data           =>   x_msg_data
2363     );
2364 
2365 
2366 EXCEPTION
2367     WHEN FND_API.G_EXC_ERROR THEN
2368         ROLLBACK TO BSCPMFUIDelete;
2369         IF (x_msg_data IS NULL) THEN
2370             FND_MSG_PUB.Count_And_Get
2371             (      p_encoded   =>  FND_API.G_FALSE
2372                ,   p_count     =>  x_msg_count
2373                ,   p_data      =>  x_msg_data
2374             );
2375         END IF;
2376         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
2377         x_return_status :=  FND_API.G_RET_STS_ERROR;
2378     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2379         ROLLBACK TO BSCPMFUIDelete;
2380         IF (x_msg_data IS NULL) THEN
2381             FND_MSG_PUB.Count_And_Get
2382             (      p_encoded   =>  FND_API.G_FALSE
2383                ,   p_count     =>  x_msg_count
2384                ,   p_data      =>  x_msg_data
2385             );
2386         END IF;
2387         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2388         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
2389     WHEN NO_DATA_FOUND THEN
2390         ROLLBACK TO BSCPMFUIDelete;
2391         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2392         IF (x_msg_data IS NOT NULL) THEN
2393             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Tab ';
2394         ELSE
2395             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Tab ';
2396         END IF;
2397         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
2398     WHEN OTHERS THEN
2399         ROLLBACK TO BSCPMFUIDelete;
2400         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2401         IF (x_msg_data IS NOT NULL) THEN
2402             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Tab ';
2403         ELSE
2404             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Tab ';
2405         END IF;
2406         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
2407 
2408 end Delete_Tab;
2409 
2410 
2411 /************************************************************************************
2412 ************************************************************************************/
2413 
2414 -- this procedure unassign Indicators  to a Tab.  It Received the Master KPI ID
2415 
2416 -- This is part of the Master/ Shared Indicator process.
2417 -- No commits are passed to the BSC APIs because these commits will undo the
2418 -- indicator lock.  The commit is executed at the end of this procedure.
2419 
2420 -- This Procedure unassign a KPI from a Tab. It Received the Master KPI ID
2421 
2422 -- Logic :
2423 
2424 -- Check if the KPI is already assigned to other Tab
2425 -- If the KPI is not assigned to other Tab then
2426      -- Just assign the KPI to the Tab and set as Master
2427 -- else the KPI is already assigned to other Tab then
2428          -- Create a Share KPI
2429          -- Assign the Share KPI to the Tab
2430 -- end if
2431 
2432 procedure Assign_KPI(
2433   p_commit                IN          VARCHAR2 := FND_API.G_FALSE
2434  ,p_kpi_id    IN  number
2435  ,p_tab_id              IN      number
2436  ,x_return_status       IN OUT NOCOPY     varchar2
2437  ,x_msg_count           IN OUT NOCOPY     number
2438  ,x_msg_data            IN OUT NOCOPY     varchar2
2439  ,p_time_stamp          IN      varchar2        :=  NULL
2440 ) is
2441 
2442 l_Bsc_Kpi_Entity_Rec            BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
2443 l_Bsc_Tab_Entity_Rec    BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
2444 
2445 l_commit      varchar2(10);
2446 l_option_ids      varchar2(300);
2447 l_kpi_name      varchar2(250);
2448 
2449 l_kpi_id      number;
2450 
2451 l_count                         number;
2452 l_kpi_source                    number;
2453 l_beg_str     number;
2454 l_end_str     number;
2455 l_occur       number;
2456 l_opt_id      number;
2457 l_share_flag      number;
2458 l_same_name     number;
2459 l_kpi_group_type    number;
2460 
2461 begin
2462    SAVEPOINT BscPmfUIAssignKpi;
2463    FND_MSG_PUB.Initialize;
2464    x_return_status := FND_API.G_RET_STS_SUCCESS;
2465 
2466   --l_commit := FND_API.G_FALSE;
2467 
2468 
2469   -- get the name, l_share_flag and Indicator source .  It will be used for tab validation.
2470 
2471   select name, share_flag, source_indicator
2472     into l_kpi_name, l_share_flag, l_kpi_source
2473     from BSC_KPIS_VL
2474    where indicator = p_kpi_id;
2475 
2476   -- Validate that the p_KPI_id corresponds to the Master KPI.
2477   if l_share_flag <> 2 then
2478        l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
2479   else
2480       -- if it's a share KPI it got the Master KPI Id
2481       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := l_kpi_source;
2482   end if;
2483   -- Evaluate if the KPI is unassign to the Tab:
2484   if Is_KPI_Assigned (l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id, p_tab_id ) = FND_API.G_FALSE  then
2485 
2486   -- if the KPI is not assigned to the Tab yet
2487 
2488     l_Bsc_Kpi_Entity_Rec.Bsc_Tab_Id := p_tab_id;
2489     l_Bsc_Kpi_Entity_Rec.Bsc_Source_Language := 'US';
2490     l_Bsc_Kpi_Entity_Rec.Bsc_Language := 'US';
2491 
2492     -- Determine if there are kpis in this tab which have the same name as
2493     -- the kpi being passed.
2494     select count(indicator)
2495       into l_same_name
2496       from BSC_TAB_INDICATORS
2497       where tab_id = p_tab_id
2498        and indicator in (select indicator
2499                          from BSC_KPIS_TL
2500                         where upper(name) = upper(l_kpi_name));
2501     -- if there are kpis in this tab which have the same name it throws an error.
2502     if l_same_name <> 0 then
2503         FND_MESSAGE.SET_NAME('BSC','BSC_B_NO_SAMEKPI_TAB');
2504         FND_MESSAGE.SET_TOKEN('Indicator name: ', l_kpi_name);
2505         FND_MSG_PUB.ADD;
2506         RAISE FND_API.G_EXC_ERROR;
2507     end if;
2508 
2509     -- need to get kpi group for this kpi.
2510     select distinct(ind_group_id)
2511       into l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id
2512       from BSC_KPIS_B
2513       where indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
2514    -- Evaluate the Group Type
2515     SELECT GROUP_TYPE
2516       into l_kpi_group_type
2517       FROM BSC_TAB_IND_GROUPS_B
2518       WHERE IND_GROUP_ID = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id
2519       and tab_id = -1 ;
2520 
2521     if l_kpi_group_type = 1 then
2522       -- if the the Group is Below the name
2523         SELECT COUNT(BK.INDICATOR)
2524             into l_count
2525             FROM BSC_KPIS_B BK, BSC_TAB_INDICATORS TI
2526             WHERE BK.IND_GROUP_ID = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id
2527              AND TI.TAB_ID = p_tab_id
2528              AND BK.INDICATOR = TI.INDICATOR;
2529         if l_count > 0 then
2530             update BSC_TAB_IND_GROUPS_B
2531               set GROUP_TYPE = 0
2532               where IND_GROUP_ID = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Group_Id;
2533         end if;
2534     end if;
2535 
2536     -- Need to determine if this KPI will be master or shared;
2537     -- Check If the Master is already assigned to other Tab.
2538     select count(tab_id)
2539       into l_count
2540       from BSC_TAB_INDICATORS
2541      where indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
2542 
2543          BSC_BIS_LOCKS_PUB.LOCK_TAB(
2544              p_tab_id          => p_tab_id
2545             ,p_time_stamp             => p_time_stamp  -- Granular Locking
2546             ,x_return_status          => x_return_status
2547             ,x_msg_count              => x_msg_count
2548             ,x_msg_data               => x_msg_data
2549         );
2550 
2551       IF ((x_return_status IS NOT NULL)AND(x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2552            RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2553     END IF;
2554     -- if count is zero then ussigned KPI will be the master, else will be the shared one.
2555     if l_count = 0 then
2556       -- about to create master KPI
2557 
2558       --DBMS_OUTPUT.PUT_LINE('BSC_PMF_UI_WRAPPER.Assign_KPI -  Flag 7');
2559 
2560 
2561 
2562 
2563       BSC_KPI_PUB.Create_Master_Kpi( FND_API.G_FALSE
2564                                     ,l_Bsc_Kpi_Entity_Rec
2565                                     ,x_return_status
2566                                     ,x_msg_count
2567                                     ,x_msg_data);
2568     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2569         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2570     END IF;
2571 
2572     else
2573       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Source_Ind := l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
2574 
2575       --DBMS_OUTPUT.PUT_LINE('BSC_PMF_UI_WRAPPER.Assign_KPI -  Flag 8');
2576       --DBMS_OUTPUT.PUT_LINE('l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Source_Ind -'||l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Source_Ind );
2577 
2578 
2579       BSC_KPI_PUB.Create_Shared_Kpi( FND_API.G_FALSE
2580                                     ,l_Bsc_Kpi_Entity_Rec
2581                                     ,x_return_status
2582                                     ,x_msg_count
2583                                     ,x_msg_data);
2584     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2585         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2586     END IF;
2587     end if;
2588 
2589      -- Added to fix issue oin bug 2731675
2590 
2591 
2592 
2593      BSC_COMMON_DIM_LEVELS_PUB.Check_Dim_Level_Default_Value( FND_API.G_FALSE
2594                                                      ,p_tab_id
2595                                                      ,x_return_status
2596                                                      ,x_msg_count
2597                                                      ,x_msg_data);
2598     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2599         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2600     END IF;
2601 
2602 
2603 
2604   end if;
2605 
2606       --BSC_DEBUG.PUT_LINE('BSC_PMF_UI_WRAPPER.Assign_KPI -  Flag 11');
2607 
2608   -- Need to call procedure for list button logic.
2609 
2610 
2611   BSC_COMMON_DIM_LEVELS_PUB.Check_Common_Dim_Levels( FND_API.G_FALSE
2612                                                      ,p_tab_id
2613                                                      ,x_return_status
2614                                                      ,x_msg_count
2615                                                      ,x_msg_data);
2616     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2617         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2618     END IF;
2619 
2620 
2621 
2622    -- Need to fix bug 3543675
2623    BSC_DIM_FILTERS_PUB.Synch_Fiters_And_Kpi_Dim
2624    (       p_Tab_Id            => p_tab_id
2625        ,   x_return_status     => x_return_status
2626        ,   x_msg_count         => x_msg_count
2627        ,   x_msg_data          => x_msg_data
2628    );
2629 
2630   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id := p_tab_id;
2631   BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
2632                                           ,l_Bsc_Tab_Entity_Rec
2633                                           ,x_return_status
2634                                           ,x_msg_count
2635                                           ,x_msg_data);
2636     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2637         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2638     END IF;
2639 
2640 
2641 
2642   IF (p_commit = FND_API.G_TRUE) THEN
2643 
2644     COMMIT;
2645   END IF;
2646 
2647 
2648     --BSC_DEBUG.PUT_LINE('End BSC_PMF_UI_WRAPPER.Assign_KPI' );
2649   --BSC_DEBUG.finish;
2650 
2651 EXCEPTION
2652     WHEN FND_API.G_EXC_ERROR THEN
2653         ROLLBACK TO BscPmfUIAssignKpi;
2654         IF (x_msg_data IS NULL) THEN
2655             FND_MSG_PUB.Count_And_Get
2656             (      p_encoded   =>  FND_API.G_FALSE
2657                ,   p_count     =>  x_msg_count
2658                ,   p_data      =>  x_msg_data
2659             );
2660         END IF;
2661         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
2662         x_return_status :=  FND_API.G_RET_STS_ERROR;
2663     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2664         ROLLBACK TO BscPmfUIAssignKpi;
2665         IF (x_msg_data IS NULL) THEN
2666             FND_MSG_PUB.Count_And_Get
2667             (      p_encoded   =>  FND_API.G_FALSE
2668                ,   p_count     =>  x_msg_count
2669                ,   p_data      =>  x_msg_data
2670             );
2671         END IF;
2672         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2673         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
2674     WHEN NO_DATA_FOUND THEN
2675         ROLLBACK TO BscPmfUIAssignKpi;
2676         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2677         IF (x_msg_data IS NOT NULL) THEN
2678             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Assign_KPI ';
2679         ELSE
2680             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Assign_KPI ';
2681         END IF;
2682         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
2683     WHEN OTHERS THEN
2684         ROLLBACK TO BscPmfUIAssignKpi;
2685         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2686         IF (x_msg_data IS NOT NULL) THEN
2687             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Assign_KPI ';
2688         ELSE
2689             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Assign_KPI ';
2690         END IF;
2691         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
2692 
2693 end Assign_KPI;
2694 
2695 
2696 
2697 /************************************************************************************
2698 ************************************************************************************/
2699 -- This Procedure unassign a KPI from a Tab. It Received the Master KPI ID
2700 
2701 -- Logic for the Maste/Share KPI :
2702 
2703 -- If the KPI assign to the current tab is the Master then
2704      -- if there is not more share KPIs assigned to any tab then
2705          -- Just unassign the Master from the tab
2706      -- else if there is just one Share KPI then
2707          -- unassign the Master from the tab
2708          -- Delete the Master KPI Metadata
2709          -- Set the Share KPI as Master KPI
2710      -- else if there are more than one Share KPI assigned the tabs then
2711          -- User can not unassign the Master KPI
2712      -- end if
2713 -- else if the KPI assigned to the Tab is a Share KPI then
2714          -- unassign the Master from the tab
2715          -- Delete the Master KPI Metadata
2716 -- end if
2717 
2718 -- Logic for handle the KPI Groups:
2719   --   Checks to see if this is the last KPI from the group.
2720   --   If it is the last KPI from the group assigned anywhere,
2721   --          then delete the group.
2722   --   else If it is the last KPI from the group assigned to this tab
2723   --          but there are other KPIs assigned elsewhere then
2724   --          just remove group from tab.
2725 
2726 
2727 procedure Unassign_KPI(
2728   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
2729  ,p_kpi_id              IN      number
2730  ,p_tab_id              IN      number
2731  ,x_return_status       OUT NOCOPY     varchar2
2732  ,x_msg_count           OUT NOCOPY     number
2733  ,x_msg_data            OUT NOCOPY     varchar2
2734  ,p_time_stamp          IN             varchar2 := NULL
2735 ) is
2736 
2737 l_Bsc_Kpi_Entity_Rec            BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
2738 l_Bsc_Kpi_Group_Rec             BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
2739 l_Bsc_Option_Rec                BSC_ANALYSIS_OPTION_PUB.Bsc_Option_Rec_Type;
2740 l_Bsc_Tab_Entity_Rec    BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
2741 
2742 l_commit                        varchar2(10);
2743 
2744 l_count       number;
2745 l_kpi_group_id      number;
2746 l_kpi_count     number;
2747 l_kpi_id      number;
2748 l_share_flag                     number;
2749 l_Master_KPI_id                  number;
2750 l_unassiged                      number;
2751 l_kpi_source                     number;
2752 l_is_obj_in_production           BOOLEAN;
2753 l_prototype_flag                 number;
2754 l_Simulation_Tree_flag           number;
2755 
2756 begin
2757    FND_MSG_PUB.Initialize;
2758    x_return_status := FND_API.G_RET_STS_SUCCESS;
2759    SAVEPOINT BscPmfUIUnAssignKpi;
2760   l_commit := FND_API.G_TRUE;
2761 
2762   --assign values.
2763   l_is_obj_in_production := FALSE;
2764   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
2765   l_Bsc_Kpi_Entity_Rec.Bsc_Tab_Id := p_tab_id;
2766   l_Bsc_Kpi_Entity_Rec.Bsc_Language := 'US';
2767   l_Bsc_Kpi_Entity_Rec.Bsc_Source_Language := 'US';
2768 
2769   l_Bsc_Kpi_Group_Rec.Bsc_Tab_Id := p_tab_id;
2770   l_Bsc_Kpi_Group_Rec.Bsc_Language := 'US';
2771   l_Bsc_Kpi_Group_Rec.Bsc_Source_Language := 'US';
2772 
2773   -- get l_share_flag,  Indicator source and KPI Group.  It will be used for tab validation.
2774   select share_flag, source_indicator
2775     into l_share_flag, l_kpi_source
2776     from BSC_KPIS_B
2777    where indicator = p_kpi_id;
2778 
2779   -- Validate that the p_KPI_id corresponds to the Master KPI.
2780   if l_share_flag <> 2 then
2781       l_Master_KPI_id := p_kpi_id;
2782   else
2783       -- if it a share KPI it got the Master KPI Id
2784       l_Master_KPI_id := l_kpi_source;
2785   end if;
2786 
2787   -- Evaluate if the KPI is assigned to the Tab:
2788   if Is_KPI_Assigned (l_Master_KPI_id, p_tab_id ) = FND_API.G_TRUE  then
2789 
2790     l_unassiged := 1;
2791 
2792     -- Get the KPI group id.
2793     select IND_GROUP_ID
2794       into l_kpi_group_id
2795       from BSC_KPIS_B
2796      where indicator = p_kpi_id;
2797      l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := l_kpi_group_id;
2798 
2799     -- Evaluate if the KPI Assigned to the Tab is the Master or a Share KPI
2800     select count(indicator)
2801         into l_count
2802     from BSC_TAB_INDICATORS
2803     where tab_id = l_Bsc_Kpi_Entity_Rec.Bsc_Tab_Id
2804           and indicator = l_Master_KPI_id;
2805 
2806     -- If l_count = 0 then the KPI assiged is a Share KPI
2807     if l_count = 0 then
2808       -- get the code for Share KPI assiged to the Tab
2809           select indicator
2810           into l_Bsc_Kpi_Entity_Rec.Bsc_kpi_id
2811           from BSC_TAB_INDICATORS
2812           where tab_id = l_Bsc_Kpi_Entity_Rec.Bsc_Tab_Id
2813           and indicator in (select indicator from BSC_KPIS_B
2814                              where source_indicator = l_Master_KPI_id
2815                            );
2816   --   Granular Locking
2817         BSC_BIS_LOCKS_PUB.LOCK_TAB(
2818               p_tab_id                 => p_tab_id
2819              ,p_time_stamp             => p_time_stamp  -- Granular Locking
2820              ,x_return_status          => x_return_status
2821              ,x_msg_count              => x_msg_count
2822              ,x_msg_data               => x_msg_data
2823    )  ;
2824 
2825        IF ((x_return_status IS NOT NULL)AND(x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2826             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2827        END IF;
2828         -- Delete the Share KPI
2829 
2830 
2831         BSC_KPI_PUB.Delete_Kpi( FND_API.G_FALSE
2832                                ,l_Bsc_Kpi_Entity_Rec
2833                                ,x_return_status
2834                                ,x_msg_count
2835                                ,x_msg_data);
2836       IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2837           RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2838       END IF;
2839 
2840 
2841     else -- The KPI assiged to the Tab is the Master KPI
2842       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := l_Master_KPI_id;
2843       -- Count the Share KPIs in other tabs
2844       select count(indicator)
2845         into l_kpi_count
2846         from BSC_TAB_INDICATORS
2847         where indicator in (select indicator
2848                                 from BSC_KPIS_B
2849                                 where source_indicator = l_Master_KPI_id );
2850       -- Get production Mode Flag for Master KPI
2851         select prototype_flag, decode(CONFIG_TYPE, 7, 1, 0)
2852             into l_prototype_flag, l_Simulation_Tree_flag
2853             from BSC_KPIS_B
2854            where indicator = l_Master_KPI_id;
2855         if l_prototype_flag <> 1 and l_prototype_flag <> 3 then
2856             l_is_obj_in_production := TRUE;
2857         else
2858             l_is_obj_in_production := FALSE;
2859         end if;
2860       -- if l_kpi_count is 0 there are no shared KPIs.  Delete Master from Tab.  Reset flag.
2861        IF l_kpi_count = 0 then
2862 
2863         BSC_KPI_PUB.Delete_Kpi_In_Tab( FND_API.G_FALSE
2864                                     ,l_Bsc_Kpi_Entity_Rec
2865                                     ,x_return_status
2866                                     ,x_msg_count
2867                                     ,x_msg_data);
2868         IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2869             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2870         END IF;
2871 
2872         BSC_KPI_PUB.Update_Kpi_Time_Stamp( FND_API.G_FALSE
2873                                         ,l_Bsc_Kpi_Entity_Rec
2874                                         ,x_return_status
2875                                         ,x_msg_count
2876                                         ,x_msg_data);
2877         IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2878             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2879         END IF;
2880 
2881        ELSIF l_kpi_count = 1 THEN
2882            -- if l_kpi_count is one then
2883            -- Check The Production Flag for The Master KPI
2884            IF (l_is_obj_in_production) THEN
2885              l_unassiged := 0;
2886              FND_MESSAGE.SET_NAME('BSC','BSC_KPI_PROD_UNASSIGN');
2887              FND_MSG_PUB.ADD;
2888              RAISE FND_API.G_EXC_ERROR;
2889            ELSE
2890               IF (l_Simulation_Tree_flag = 0) THEN
2891                    ---  Delete Master KPI from Tab and Library.
2892                    ---  Make Shared KPI as the Master KPI to make it appear in library.
2893                    --   Get the Share KPI Code
2894                    SELECT indicator
2895                    INTO   l_kpi_id
2896                    FROM   BSC_TAB_INDICATORS
2897                    WHERE  indicator IN (SELECT indicator
2898                                         FROM   bsc_kpis_b
2899                                         WHERE  source_indicator = l_Master_KPI_id );
2900 
2901                    -- Once the shared KPI obtained then delete master KPI.
2902                    -- call the public version of Delete_Kpi_Defaults.
2903                    BSC_KPI_PUB.Delete_Kpi_Defaults( FND_API.G_FALSE
2904                                                   , l_Bsc_Kpi_Entity_Rec
2905                                                   , x_return_status
2906                                                   , x_msg_count
2907                                                   , x_msg_data);
2908 
2909                   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2910                     RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2911                   END IF;
2912 
2913                    -- Call the private version.  The public version deletes all shared kpis.
2914                   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
2915                   BSC_KPI_PVT.Delete_Kpi( FND_API.G_FALSE
2916                                          ,l_Bsc_Kpi_Entity_Rec
2917                                          ,x_return_status
2918                                          ,x_msg_count
2919                                          ,x_msg_data);
2920                   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2921                     RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2922                   END IF;
2923                   -- reset flags for Shared KPI as Master KPI.
2924                   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Source_Ind := null;
2925                   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Share_Flag := 1;
2926                   l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := l_kpi_id;
2927 
2928                   BSC_KPI_PVT.Update_Kpi(  FND_API.G_FALSE
2929                                          , l_Bsc_Kpi_Entity_Rec
2930                                          , x_return_status
2931                                          , x_msg_count
2932                                          , x_msg_data);
2933                   IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2934                      RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2935                   END IF;
2936                    -- Need to do this update directly because the procedure will see the null
2937                    -- value for Source Indicator and it will update it with previos value, not
2938                    -- the null one.
2939                   UPDATE bsc_kpis_b
2940                   SET    source_indicator = NULL
2941                   WHERE  indicator = l_kpi_id;
2942 
2943               ELSE
2944                   BSC_KPI_PUB.move_master_kpi
2945                   (
2946                           p_master_kpi     =>   l_Master_KPI_id
2947                          ,x_return_status  =>   x_return_status
2948                          ,x_msg_count      =>   x_msg_count
2949                          ,x_msg_data       =>   x_msg_data
2950                   );
2951                 IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2952                   RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2953                 END IF;
2954               END IF;
2955            END IF;
2956       ELSE
2957         -- If there is more than one Share KPI user can not unassign the Master KPI from de Tab
2958         l_unassiged := 0;
2959         FND_MESSAGE.SET_NAME('BSC','BSC_NO_MASTER_DELETE');
2960         FND_MESSAGE.SET_TOKEN('BSC_KPI', l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id);
2961         FND_MSG_PUB.ADD;
2962         RAISE FND_API.G_EXC_ERROR;
2963       end if;
2964     end if;
2965 
2966     if l_unassiged <> 0 then
2967       -- Evaluate if it needs to unassign the KPI Group
2968 
2969       select count(b.indicator)
2970         into l_kpi_count
2971         from bsc_kpis_b a, bsc_tab_indicators b
2972         where a.ind_group_id = l_kpi_group_id
2973          and a.indicator = b.indicator
2974          and b.tab_id = p_tab_id;
2975 
2976 
2977       -- if l_kpi_count is zero then unassign group from Tab.
2978       if l_kpi_count = 0 then
2979 
2980         BSC_KPI_GROUP_PUB.Delete_Kpi_Group( FND_API.G_FALSE
2981                                            ,l_Bsc_Kpi_Group_Rec
2982                                            ,x_return_status
2983                                            ,x_msg_count
2984                                            ,x_msg_data);
2985             IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2986                 RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2987             END IF;
2988       end if;
2989 
2990 
2991       -- Call procedure to handle list button logic.
2992       BSC_COMMON_DIM_LEVELS_PUB.Check_Common_Dim_Levels( FND_API.G_FALSE
2993                                                         ,p_tab_id
2994                                                         ,x_return_status
2995                                                         ,x_msg_count
2996                                                         ,x_msg_data);
2997         IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
2998             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
2999         END IF;
3000 
3001       BSC_DIM_FILTERS_PUB.Check_Filters_Not_Apply
3002       (        p_Tab_Id         =>  p_tab_id
3003             ,  x_return_status  =>  x_return_status
3004             ,  x_msg_count      =>  x_msg_count
3005             ,  x_msg_data       =>  x_msg_data
3006       );
3007        IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
3008           --DBMS_OUTPUT.PUT_LINE('BSC_PMF_UI_WRAPPER.Unassign_KPI Failed: at BSC_DIM_FILTERS_PUB.Check_Filters_Not_Apply');
3009           RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3010        END IF;
3011 
3012        -- Need to fix bug 3543675
3013        -- it pass null in order to syscronize KPIs not assigned to any tab
3014       BSC_DIM_FILTERS_PUB.Synch_Fiters_And_Kpi_Dim
3015       (       p_Tab_Id            => null
3016            ,   x_return_status     => x_return_status
3017            ,   x_msg_count         => x_msg_count
3018            ,   x_msg_data          => x_msg_data
3019       );
3020 
3021       l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id := p_tab_id;
3022       BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
3023                                           ,l_Bsc_Tab_Entity_Rec
3024                                           ,x_return_status
3025                                           ,x_msg_count
3026                                           ,x_msg_data);
3027         IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3028             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3029         END IF;
3030     end if;
3031 
3032      BSC_CUSTOM_VIEW_PUB.Delete_Custom_View_Links
3033      (
3034           p_commit         =>  FND_API.G_FALSE
3035         , p_tab_id         =>  p_tab_id
3036         , p_obj_id         =>  p_kpi_id
3037         , x_return_status  =>  x_return_status
3038         , x_msg_count      =>  x_msg_count
3039         , x_msg_data       =>  x_msg_data
3040      );
3041      IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3042          RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3043      END IF;
3044 
3045 
3046   end if;
3047 
3048   IF (p_commit = FND_API.G_TRUE) THEN
3049     COMMIT;
3050   END IF;
3051 
3052 EXCEPTION
3053     WHEN FND_API.G_EXC_ERROR THEN
3054         ROLLBACK TO BscPmfUIUnAssignKpi;
3055         IF (x_msg_data IS NULL) THEN
3056             FND_MSG_PUB.Count_And_Get
3057             (      p_encoded   =>  FND_API.G_FALSE
3058                ,   p_count     =>  x_msg_count
3059                ,   p_data      =>  x_msg_data
3060             );
3061         END IF;
3062         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
3063         x_return_status :=  FND_API.G_RET_STS_ERROR;
3064     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3065         ROLLBACK TO BscPmfUIUnAssignKpi;
3066         IF (x_msg_data IS NULL) THEN
3067             FND_MSG_PUB.Count_And_Get
3068             (      p_encoded   =>  FND_API.G_FALSE
3069                ,   p_count     =>  x_msg_count
3070                ,   p_data      =>  x_msg_data
3071             );
3072         END IF;
3073         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3074         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
3075     WHEN NO_DATA_FOUND THEN
3076         ROLLBACK TO BscPmfUIUnAssignKpi;
3077         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3078         IF (x_msg_data IS NOT NULL) THEN
3079             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Unassign_KPI ';
3080         ELSE
3081             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Unassign_KPI ';
3082         END IF;
3083         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
3084     WHEN OTHERS THEN
3085         ROLLBACK TO BscPmfUIUnAssignKpi;
3086         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3087         IF (x_msg_data IS NOT NULL) THEN
3088             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Unassign_KPI ';
3089         ELSE
3090             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Unassign_KPI ';
3091         END IF;
3092         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
3093 
3094 end Unassign_KPI;
3095 
3096 
3097 
3098 /************************************************************************************
3099 ************************************************************************************/
3100 
3101 -- Return TRUE or False ('T' or 'F')  when the KPI (p_kpi_id)  is assigned
3102 -- to the tab (p_tab_id) or a Share KPI of the mension KPI (p_kpi_id) is assigned to the tab.
3103 
3104 function Is_KPI_Assigned(
3105   p_kpi_id              IN      number
3106  ,p_tab_id              IN      number
3107 ) return varchar2 IS
3108  l_count     number;
3109 Begin
3110 
3111   select count(indicator)
3112     into l_count
3113     from BSC_TAB_INDICATORS
3114     where tab_id = p_tab_id
3115       and (indicator = p_kpi_id  or
3116            indicator in (select indicator
3117                            from BSC_KPIS_B
3118                            where source_indicator = p_kpi_id)
3119           );
3120 
3121   if l_count <> 0 then
3122   return FND_API.G_TRUE;
3123   else
3124     return FND_API.G_FALSE;
3125   end if;
3126 
3127 EXCEPTION
3128   WHEN OTHERS THEN
3129     return FND_API.G_FALSE;
3130 
3131 End Is_KPI_Assigned;
3132 
3133 /************************************************************************************
3134 ************************************************************************************/
3135 
3136 -- Revamped API to do proper rollback and handling
3137 
3138 procedure Assign_KPI_Group(
3139   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
3140  ,p_kpi_Group_id        IN      number
3141  ,p_tab_id              IN      number
3142  ,x_return_status       OUT NOCOPY     varchar2
3143  ,x_msg_count           OUT NOCOPY     number
3144  ,x_msg_data            OUT NOCOPY     varchar2
3145 ) is
3146 
3147 l_kpi_id    number;
3148     CURSOR c_kpis IS
3149     SELECT indicator
3150     FROM bsc_kpis_b
3151     WHERE ind_group_id = p_kpi_group_id
3152     AND SHARE_FLAG <> 2
3153     AND PROTOTYPE_FLAG <> 2;
3154 
3155 l_Bsc_Kpi_Group_Rec   BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
3156 l_Bsc_tab_Group_Rec   BSC_KPI_GROUP_PUB.Bsc_Kpi_Group_Rec;
3157 l_count           NUMBER;
3158 
3159 BEGIN
3160 
3161    FND_MSG_PUB.Initialize;
3162    x_return_status := FND_API.G_RET_STS_SUCCESS;
3163    SAVEPOINT BscPmfUIUAssignKpiGrp;
3164 
3165    SELECT COUNT(*)
3166    INTO  l_count
3167    FROM BSC_KPIS_B
3168    WHERE  ind_group_id = p_kpi_group_id
3169    and SHARE_FLAG <> 2
3170    and PROTOTYPE_FLAG <> 2;
3171 
3172    IF (l_count = 0) THEN
3173 
3174       l_Bsc_Kpi_Group_Rec.Bsc_Kpi_Group_Id := p_kpi_Group_id;
3175       l_Bsc_Kpi_Group_Rec.Bsc_Language     := USERENV('LANG');
3176 
3177       BSC_KPI_GROUP_PVT.Retrieve_Kpi_Group(
3178              p_commit              =>  FND_API.G_FALSE
3179             ,p_Bsc_Kpi_Group_Rec   =>  l_Bsc_Kpi_Group_Rec
3180             ,x_Bsc_Kpi_Group_Rec   =>  l_Bsc_tab_Group_Rec
3181             ,x_return_status       =>  x_return_status
3182             ,x_msg_count           =>  x_msg_count
3183             ,x_msg_data            =>  x_msg_data
3184        );
3185        IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3186              RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3187        END IF;
3188 
3189        l_Bsc_tab_Group_Rec.Bsc_Tab_Id := p_tab_id;
3190        l_Bsc_tab_Group_Rec.Bsc_Kpi_Group_Id := p_kpi_Group_id;
3191 
3192        BSC_KPI_GROUP_PVT.Create_Kpi_Group(
3193            p_commit            =>  FND_API.G_FALSE
3194           ,p_Bsc_Kpi_Group_Rec =>  l_Bsc_tab_Group_Rec
3195           ,x_return_status       =>  x_return_status
3196           ,x_msg_count           =>  x_msg_count
3197           ,x_msg_data            =>  x_msg_data
3198        );
3199     ELSE
3200        IF (c_kpis%ISOPEN) THEN
3201             CLOSE c_kpis;
3202        END IF;
3203        OPEN c_kpis;
3204        LOOP
3205          FETCH c_kpis INTO l_kpi_id;
3206          EXIT WHEN c_kpis%NOTFOUND;
3207 
3208 
3209          BSC_PMF_UI_WRAPPER.Assign_KPI(
3210                 p_Commit         =>  FND_API.G_FALSE
3211                ,p_kpi_id         =>  l_kpi_id
3212                ,p_tab_id         =>  p_tab_id
3213                ,x_return_status  =>  x_return_status
3214                ,x_msg_count      =>  x_msg_count
3215                ,x_msg_data       =>  x_msg_data
3216          );
3217          IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3218               RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3219          END IF;
3220         END LOOP;
3221        CLOSE c_kpis;
3222    END IF;
3223 
3224 EXCEPTION
3225   WHEN FND_API.G_EXC_ERROR THEN
3226     IF (c_kpis%ISOPEN) THEN
3227         CLOSE c_kpis;
3228     END IF;
3229     rollback to BscPmfUIUAssignKpiGrp;
3230     x_return_status := FND_API.G_RET_STS_ERROR;
3231     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3232                               ,p_data   =>      x_msg_data);
3233   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3234     IF (c_kpis%ISOPEN) THEN
3235         CLOSE c_kpis;
3236     END IF;
3237     rollback to BscPmfUIUAssignKpiGrp;
3238     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3239     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3240                               ,p_data     =>      x_msg_data);
3241   WHEN NO_DATA_FOUND THEN
3242     IF (c_kpis%ISOPEN) THEN
3243         CLOSE c_kpis;
3244     END IF;
3245     rollback to BscPmfUIUAssignKpiGrp;
3246     x_return_status := FND_API.G_RET_STS_ERROR;
3247     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3248                               ,p_data     =>      x_msg_data);
3249   WHEN OTHERS THEN
3250     IF (c_kpis%ISOPEN) THEN
3251         CLOSE c_kpis;
3252     END IF;
3253     rollback to BscPmfUIUAssignKpiGrp;
3254     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3255     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3256                               ,p_data     =>      x_msg_data);
3257 
3258 end Assign_KPI_Group;
3259 
3260 /************************************************************************************
3261 ************************************************************************************/
3262 
3263 procedure Assign_Analysis_Option(
3264   p_kpi_id                  IN      number
3265  ,p_analysis_group_id       IN      number
3266  ,p_option_id               IN      number
3267  ,p_parent_option_id        IN      number
3268  ,p_grandparent_option_id   IN      number
3269  ,x_return_status           OUT NOCOPY     varchar2
3270  ,x_msg_count               OUT NOCOPY     number
3271  ,x_msg_data                OUT NOCOPY     varchar2
3272  ,p_commit                  IN      varchar2 /* := FND_API.G_TRUE */
3273  ,p_time_stamp_to_check     IN      varchar2 /* := null */
3274 
3275 ) IS
3276 
3277  l_Bsc_kpi_Entity_Rec      BSC_KPI_PUB.Bsc_kpi_Entity_Rec;
3278  l_time_stamp              varchar(200);
3279 Begin
3280    FND_MSG_PUB.Initialize;
3281    x_return_status := FND_API.G_RET_STS_SUCCESS;
3282    SAVEPOINT BscPmfUIUAssignAnaOpt;
3283  --DBMS_OUTPUT.PUT_LINE('Begin BSC_PMF_UI_WRAPPER.Assign_Analysis_Option');
3284  FND_MSG_PUB.Initialize;
3285 
3286  IF p_time_stamp_to_check IS NOT NULL then
3287         l_time_stamp := get_KPI_Time_Stamp(p_kpi_id);
3288         IF l_time_stamp IS NULL or l_time_stamp <> p_time_stamp_to_check then
3289             FND_MESSAGE.SET_NAME('BSC','BSC_KPI_CHANGED');
3290             FND_MSG_PUB.ADD;
3291             RAISE FND_API.G_EXC_ERROR;
3292         END if;
3293  ELSE
3294    Populate_Option_Dependency_Rec (
3295                   FND_API.G_FALSE
3296                  ,p_kpi_id
3297                  ,p_analysis_group_id
3298          ,p_option_id
3299          ,p_parent_option_id
3300          ,p_grandparent_option_id
3301          ,l_Bsc_kpi_Entity_Rec
3302          ,x_return_status
3303          ,x_msg_count
3304          ,x_msg_data );
3305            IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3306                RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3307            END IF;
3308    BSC_KPI_PUB.Assign_Analysis_Option(
3309               l_Bsc_kpi_Entity_Rec
3310        ,x_return_status
3311        ,x_msg_count
3312        ,x_msg_data );
3313    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3314        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3315    END IF ;
3316    BSC_KPI_PUB.Update_Kpi_Time_Stamp(
3317          FND_API.G_FALSE
3318         ,l_Bsc_kpi_Entity_Rec
3319         ,x_return_status
3320         ,x_msg_count
3321         ,x_msg_data );
3322    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3323        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3324    END IF ;
3325 
3326    IF p_commit = FND_API.G_TRUE THEN
3327      commit;
3328    END IF;
3329 
3330  END IF;
3331 
3332 
3333  --DBMS_OUTPUT.PUT_LINE('End BSC_PMF_UI_WRAPPER.Assign_Analysis_Option');
3334 
3335 EXCEPTION
3336   WHEN FND_API.G_EXC_ERROR THEN
3337     rollback to BscPmfUIUAssignAnaOpt;
3338     x_return_status := FND_API.G_RET_STS_ERROR;
3339     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3340                               ,p_data   =>      x_msg_data);
3341   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3342     rollback to BscPmfUIUAssignAnaOpt;
3343     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3344     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3345                               ,p_data     =>      x_msg_data);
3346   WHEN NO_DATA_FOUND THEN
3347     rollback to BscPmfUIUAssignAnaOpt;
3348     x_return_status := FND_API.G_RET_STS_ERROR;
3349     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3350                               ,p_data     =>      x_msg_data);
3351   WHEN OTHERS THEN
3352     rollback to BscPmfUIUAssignAnaOpt;
3353     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3354     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3355                               ,p_data     =>      x_msg_data);
3356 
3357 End Assign_Analysis_Option;
3358 
3359 
3360 /************************************************************************************
3361 ************************************************************************************/
3362 
3363 procedure Unassign_Analysis_Option(
3364   p_kpi_id                  IN      number
3365  ,p_analysis_group_id       IN      number
3366  ,p_option_id               IN      number
3367  ,p_parent_option_id        IN      number
3368  ,p_grandparent_Option_id   IN      number
3369  ,x_return_status           OUT NOCOPY     varchar2
3370  ,x_msg_count               OUT NOCOPY     number
3371  ,x_msg_data                OUT NOCOPY     varchar2
3372  ,p_commit                  IN      varchar2 /* := FND_API.G_TRUE */
3373  ,p_time_stamp_to_check     IN      varchar2 /* := null */
3374 
3375 ) IS
3376  l_Bsc_kpi_Entity_Rec      BSC_KPI_PUB.Bsc_kpi_Entity_Rec;
3377  l_time_stamp              varchar(200);
3378 
3379 
3380 Begin
3381    FND_MSG_PUB.Initialize;
3382    x_return_status := FND_API.G_RET_STS_SUCCESS;
3383    SAVEPOINT BscPmfUIUnAssignAnaOpt;
3384 
3385  IF p_time_stamp_to_check IS NOT NULL then
3386         l_time_stamp := get_KPI_Time_Stamp(p_kpi_id);
3387         IF l_time_stamp IS NULL or l_time_stamp <> p_time_stamp_to_check then
3388             FND_MESSAGE.SET_NAME('BSC','BSC_KPI_CHANGED');
3389             FND_MSG_PUB.ADD;
3390             RAISE FND_API.G_EXC_ERROR;
3391         END if;
3392  ELSE
3393     Populate_Option_Dependency_Rec (
3394                   FND_API.G_FALSE
3395                  ,p_kpi_id
3396                  ,p_analysis_group_id
3397          ,p_option_id
3398          ,p_parent_option_id
3399          ,p_grandparent_option_id
3400          ,l_Bsc_kpi_Entity_Rec
3401          ,x_return_status
3402          ,x_msg_count
3403          ,x_msg_data );
3404    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3405        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3406    END IF ;
3407 
3408     BSC_KPI_PUB.Unassign_Analysis_Option(
3409                          l_Bsc_kpi_Entity_Rec
3410        ,x_return_status
3411        ,x_msg_count
3412        ,x_msg_data );
3413    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3414        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3415    END IF;
3416 
3417    BSC_KPI_PUB.Update_Kpi_Time_Stamp(
3418          FND_API.G_FALSE
3419         ,l_Bsc_kpi_Entity_Rec
3420         ,x_return_status
3421         ,x_msg_count
3422         ,x_msg_data );
3423    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3424        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3425    END IF;
3426 
3427    IF p_commit = FND_API.G_TRUE THEN
3428      commit;
3429    END IF;
3430 
3431  END IF;
3432 
3433 
3434  --DBMS_OUTPUT.PUT_LINE('End BSC_PMF_UI_WRAPPER.Unassign_Analysis_Option');
3435 
3436 EXCEPTION
3437   WHEN FND_API.G_EXC_ERROR THEN
3438     rollback to BscPmfUIUnAssignAnaOpt;
3439     x_return_status := FND_API.G_RET_STS_ERROR;
3440     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3441                               ,p_data   =>      x_msg_data);
3442   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3443     rollback to BscPmfUIUnAssignAnaOpt;
3444     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3445     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3446                               ,p_data     =>      x_msg_data);
3447   WHEN NO_DATA_FOUND THEN
3448     rollback to BscPmfUIUnAssignAnaOpt;
3449     x_return_status := FND_API.G_RET_STS_ERROR;
3450      FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3451                               ,p_data     =>      x_msg_data);
3452   WHEN OTHERS THEN
3453     rollback to BscPmfUIUnAssignAnaOpt;
3454     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3455     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3456                               ,p_data     =>      x_msg_data);
3457 
3458 End Unassign_Analysis_Option;
3459 
3460 /************************************************************************************
3461 ************************************************************************************/
3462 
3463 function Is_Analysis_Option_Selected(
3464   p_kpi_id                  IN      number
3465  ,p_analysis_group_id       IN      number
3466  ,p_option_id               IN      number
3467  ,p_parent_option_id        IN      number
3468  ,p_grandparent_Option_id   IN      number
3469 ) return varchar2 IS
3470 
3471  l_Bsc_kpi_Entity_Rec      BSC_KPI_PUB.Bsc_kpi_Entity_Rec;
3472  x_return_status          varchar2(3000);
3473  x_msg_count              number;
3474  x_msg_data               varchar2(3000);
3475  temp                     varchar2(5);
3476 
3477 Begin
3478    FND_MSG_PUB.Initialize;
3479    x_return_status := FND_API.G_RET_STS_SUCCESS;
3480  --DBMS_OUTPUT.PUT_LINE('Begin BSC_PMF_UI_WRAPPER.Is_Analysis_Option_Selected');
3481 
3482  -- it return Empty if option does not exist
3483  temp := '';
3484  -- populate the record l_Bsc_kpi_Entity_Rec with the parameters
3485  Populate_Option_Dependency_Rec ( FND_API.G_FALSE
3486                                  ,p_kpi_id
3487                                  ,p_analysis_group_id
3488          ,p_option_id
3489          ,p_parent_option_id
3490          ,p_grandparent_option_id
3491          ,l_Bsc_kpi_Entity_Rec
3492          ,x_return_status
3493          ,x_msg_count
3494          ,x_msg_data );
3495    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
3496        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
3497    END IF;
3498 
3499  -- Call main function
3500  temp := BSC_KPI_PUB.Is_Analysis_Option_Selected(
3501                             l_Bsc_kpi_Entity_Rec
3502           ,x_return_status
3503           ,x_msg_count
3504           ,x_msg_data );
3505 
3506  --DBMS_OUTPUT.PUT_LINE('End BSC_PMF_UI_WRAPPER.Is_Analysis_Option_Selected  -  return ' || temp);
3507 
3508  return temp;
3509 
3510 EXCEPTION
3511   WHEN OTHERS THEN
3512     return temp;
3513 
3514 end Is_Analysis_Option_Selected;
3515 
3516 /************************************************************************************
3517 ************************************************************************************/
3518 
3519 function Is_Leaf_Analysis_Option(
3520   p_kpi_id                  IN      number
3521  ,p_analysis_group_id       IN      number
3522  ,p_option_id               IN      number
3523  ,p_parent_option_id        IN      number
3524  ,p_grandparent_Option_id   IN      number
3525 ) return varchar2 IS
3526 
3527  l_Bsc_kpi_Entity_Rec     BSC_KPI_PUB.Bsc_kpi_Entity_Rec;
3528  x_return_status          varchar2(3000);
3529  x_msg_count              number;
3530  x_msg_data               varchar2(3000);
3531  temp                     varchar2(5);
3532  l_count      number;
3533 
3534 Begin
3535    FND_MSG_PUB.Initialize;
3536    x_return_status := FND_API.G_RET_STS_SUCCESS;
3537   --DBMS_OUTPUT.PUT_LINE('Begin BSC_PMF_UI_WRAPPER.Is_Leaf_Analysis_Option ');
3538 
3539 
3540   l_Bsc_kpi_Entity_Rec.Bsc_kpi_Id := p_kpi_id;
3541   l_Bsc_kpi_Entity_Rec.Bsc_kpi_Group_Id := p_analysis_group_id;
3542 
3543   l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := 0;
3544   l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option2 := 0;
3545 
3546   if p_analysis_group_id = 0 then
3547       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_option_id;
3548   elsif p_analysis_group_id = 1 then
3549       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_parent_option_id;
3550       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := p_option_id;
3551   else
3552       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_grandparent_option_id;
3553       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := p_parent_option_id;
3554       l_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option2 := p_option_id;
3555   end if;
3556 
3557   -- Evaluate if the option exists
3558   select count(option_id)
3559      into l_count
3560    from BSC_KPI_ANALYSIS_OPTIONS_B
3561    where indicator = p_kpi_id
3562          and analysis_group_id = p_analysis_group_id
3563            and option_id = p_option_id
3564      and parent_option_id = p_parent_option_id
3565      and grandparent_option_id = p_grandparent_option_id;
3566 
3567   if l_count <> 0 then
3568      temp := BSC_KPI_PUB.Is_Leaf_Analysis_Option(
3569                             l_Bsc_kpi_Entity_Rec
3570           ,x_return_status
3571           ,x_msg_count
3572           ,x_msg_data );
3573   else
3574     -- Return Empty when the Option does not exists
3575     temp := '';
3576   end if;
3577 
3578 
3579   --DBMS_OUTPUT.PUT_LINE('End BSC_PMF_UI_WRAPPER.Is_Leaf_Analysis_Option  -  return ' || temp);
3580 
3581   return temp;
3582 
3583 EXCEPTION
3584   WHEN OTHERS THEN
3585     return '' ;
3586 
3587 end Is_Leaf_Analysis_Option;
3588 
3589 /************************************************************************************
3590 ************************************************************************************/
3591 
3592 procedure Populate_Option_Dependency_Rec(
3593   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
3594  ,p_kpi_id                  IN      number
3595  ,p_analysis_group_id       IN      number
3596  ,p_option_id               IN      number
3597  ,p_parent_option_id        IN      number
3598  ,p_grandparent_option_id   IN      number
3599  ,p_Bsc_kpi_Entity_Rec      OUT NOCOPY     BSC_KPI_PUB.Bsc_kpi_Entity_Rec
3600  ,x_return_status           OUT NOCOPY     varchar2
3601  ,x_msg_count               OUT NOCOPY     number
3602  ,x_msg_data                OUT NOCOPY     varchar2
3603 ) IS
3604 
3605  l_count  number;
3606 Begin
3607    FND_MSG_PUB.Initialize;
3608    x_return_status := FND_API.G_RET_STS_SUCCESS;
3609   --DBMS_OUTPUT.PUT_LINE('Begin BSC_PMF_UI_WRAPPER.Populate_Option_Dependency_Rec');
3610 
3611   -- Evaluate if the Analysis option Exist in the Metadata
3612   select count(option_id)
3613      into l_count
3614    from BSC_KPI_ANALYSIS_OPTIONS_B
3615    where indicator = p_kpi_id
3616          and analysis_group_id = p_analysis_group_id
3617            and option_id = p_option_id
3618      and parent_option_id = p_parent_option_id
3619      and grandparent_option_id = p_grandparent_option_id;
3620 
3621   if l_count = 0 then
3622     --  l_count = 0 means the option does not exist
3623     --DBMS_OUTPUT.PUT_LINE('--BSC_PMF_UI_WRAPPER.Populate_Option_Dependency_Rec -  BSC_NO_VALUE_FOUND ');
3624     FND_MSG_PUB.Initialize;
3625     FND_MESSAGE.SET_NAME('BSC','BSC_NO_VALUE_FOUND');
3626     FND_MESSAGE.SET_TOKEN('BSC_OBJECT', 'Populate_Option_Dependency_Rec');
3627     FND_MSG_PUB.ADD;
3628     RAISE FND_API.G_EXC_ERROR;
3629   end if;
3630 
3631   p_Bsc_kpi_Entity_Rec.Bsc_kpi_Id := p_kpi_id;
3632   p_Bsc_kpi_Entity_Rec.Bsc_kpi_Group_Id := p_analysis_group_id;
3633 
3634   p_Bsc_kpi_Entity_Rec.Bsc_Dependency_Flag := 0;
3635   p_Bsc_kpi_Entity_Rec.Bsc_gp_Dependency_Flag := 0;
3636 
3637 
3638   if p_analysis_group_id > 0 then
3639     select DEPENDENCY_FLAG
3640       into p_Bsc_kpi_Entity_Rec.Bsc_Dependency_Flag
3641       from BSC_KPI_ANALYSIS_GROUPS
3642       where INDICATOR = p_kpi_id
3643         and ANALYSIS_GROUP_ID = p_analysis_group_id;
3644 
3645      if p_analysis_group_id = 2 and p_Bsc_kpi_Entity_Rec.Bsc_Dependency_Flag <> 0 then
3646         select DEPENDENCY_FLAG
3647           into p_Bsc_kpi_Entity_Rec.Bsc_gp_Dependency_Flag
3648           from BSC_KPI_ANALYSIS_GROUPS
3649           where INDICATOR = p_kpi_id
3650             and ANALYSIS_GROUP_ID = 1;
3651      end if;
3652 
3653   end if;
3654 
3655   p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := 0;
3656   p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := 0;
3657   p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option2 := 0;
3658 
3659   if p_analysis_group_id = 0 then
3660       p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_option_id;
3661   elsif p_analysis_group_id = 1 then
3662       if p_Bsc_kpi_Entity_Rec.Bsc_Dependency_Flag <> 0 then
3663         p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_parent_option_id;
3664       end if;
3665       p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := p_option_id;
3666   else
3667       if p_Bsc_kpi_Entity_Rec.Bsc_gp_Dependency_Flag <> 0 then
3668          p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option0 := p_grandparent_option_id;
3669       end if;
3670       if p_Bsc_kpi_Entity_Rec.Bsc_Dependency_Flag <> 0 then
3671         p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option1 := p_parent_option_id;
3672       end if;
3673       p_Bsc_kpi_Entity_Rec.Bsc_kpi_Analysis_Option2 := p_option_id;
3674   end if;
3675 
3676   --DBMS_OUTPUT.PUT_LINE('End BSC_PMF_UI_WRAPPER.Populate_Option_Dependency_Rec');
3677 
3678 
3679 EXCEPTION
3680 
3681   WHEN FND_API.G_EXC_ERROR THEN
3682     x_return_status := FND_API.G_RET_STS_ERROR;
3683     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3684                               ,p_data   =>      x_msg_data);
3685     raise;
3686   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3687     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3688     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3689                               ,p_data     =>      x_msg_data);
3690     raise;
3691   WHEN NO_DATA_FOUND THEN
3692     x_return_status := FND_API.G_RET_STS_ERROR;
3693     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3694                               ,p_data     =>      x_msg_data);
3695     raise;
3696   WHEN OTHERS THEN
3697     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3698     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3699                               ,p_data     =>      x_msg_data);
3700     raise;
3701 
3702 End Populate_Option_Dependency_Rec;
3703 
3704 
3705 /*********************************************************************************
3706 
3707 -- Procedures to Handle Relationships between Dimension Levels
3708 
3709 **********************************************************************************/
3710 
3711 procedure Change_Error_Msg(
3712   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
3713  ,p_msg_name        IN          varchar2
3714  ,p_new_msg_name    IN          varchar2
3715  ,p_token1          IN          varchar2
3716  ,p_token1_value    IN          varchar2
3717  ,p_token2          IN          varchar2
3718  ,p_token2_value    IN          varchar2
3719  ,p_initialize_flag IN          varchar2
3720  ,p_sys_admin_flag  IN          varchar2
3721  ,x_return_status   IN OUT NOCOPY      varchar2
3722  ,x_msg_count       OUT NOCOPY      number
3723  ,x_msg_data        OUT NOCOPY      varchar2
3724 
3725 ) is
3726 
3727 begin
3728    FND_MSG_PUB.Initialize;
3729    x_return_status := FND_API.G_RET_STS_SUCCESS;
3730 --DBMS_OUTPUT.PUT_LINE('Begin ChangeErrorMsg');
3731 
3732  if x_return_status is not null then
3733 
3734    FND_MSG_PUB.Count_And_Get( p_count     =>      x_msg_count
3735                               ,p_data     =>      x_msg_data);
3736  --DBMS_OUTPUT.PUT_LINE( '--- Change_Error_Msg   -   x_return_status = ' || x_return_status  );
3737  --DBMS_OUTPUT.PUT_LINE( '--- Change_Error_Msg   -   x_msg_data  = ' || x_msg_data );
3738    if p_msg_name is not null then
3739     if instr(x_msg_data, p_msg_name) <> 0 then
3740           if p_initialize_flag = 'Y' then
3741         FND_MSG_PUB.Initialize;
3742           end if;
3743         FND_MESSAGE.SET_NAME('BSC',p_new_msg_name);
3744         IF p_token1 is not null then
3745         FND_MESSAGE.SET_TOKEN(p_token1, p_token1_value);
3746         end if;
3747         IF p_token2 is not null then
3748         FND_MESSAGE.SET_TOKEN(p_token2, p_token2_value);
3749         end if;
3750           FND_MSG_PUB.ADD;
3751     if p_sys_admin_flag = 'Y' then
3752             FND_MESSAGE.SET_NAME('BSC','BSC_CONTACT_SYS_AD');
3753             FND_MSG_PUB.ADD;
3754                 end if;
3755           RAISE FND_API.G_EXC_ERROR;
3756     end if;
3757    elsif p_new_msg_name is not null then
3758         if p_initialize_flag = 'Y' then
3759       FND_MSG_PUB.Initialize;
3760         end if;
3761       FND_MESSAGE.SET_NAME('BSC', p_new_msg_name);
3762       IF p_token1 is not null then
3763       FND_MESSAGE.SET_TOKEN(p_token1, p_token1_value);
3764       end if;
3765       IF p_token2 is not null then
3766       FND_MESSAGE.SET_TOKEN(p_token2, p_token2_value);
3767       end if;
3768         FND_MSG_PUB.ADD;
3769   if p_sys_admin_flag = 'Y' then
3770           FND_MESSAGE.SET_NAME('BSC','BSC_CONTACT_SYS_AD');
3771           FND_MSG_PUB.ADD;
3772         end if;
3773         if x_return_status = FND_API.G_RET_STS_ERROR then
3774             RAISE FND_API.G_EXC_ERROR;
3775         else
3776             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3777   end if;
3778    elsif x_return_status = FND_API.G_RET_STS_UNEXP_ERROR then
3779         if p_initialize_flag = 'Y' then
3780       FND_MSG_PUB.Initialize;
3781         end if;
3782         FND_MESSAGE.SET_NAME('BSC','BSC_WARNING_DIMREL_IMPORTERR');
3783         FND_MESSAGE.SET_TOKEN('RETURN_STATUS', x_return_status);
3784         FND_MESSAGE.SET_TOKEN('RETURN_MESSAGE', x_msg_data);
3785         FND_MSG_PUB.ADD;
3786   if p_sys_admin_flag = 'Y' then
3787           FND_MESSAGE.SET_NAME('BSC','BSC_CONTACT_SYS_AD');
3788           FND_MSG_PUB.ADD;
3789         end if;
3790         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3791    end if;
3792 
3793  end if;
3794 --DBMS_OUTPUT.PUT_LINE('End ChangeErrorMsg');
3795 
3796 
3797 EXCEPTION
3798   WHEN FND_API.G_EXC_ERROR THEN
3799     x_return_status := FND_API.G_RET_STS_ERROR;
3800 
3801     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3802                               ,p_data   =>      x_msg_data);
3803   --DBMS_OUTPUT.PUT_LINE('End ChangeErrorMsg');
3804 
3805   WHEN OTHERS THEN
3806     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3807     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3808                               ,p_data     =>      x_msg_data);
3809   --DBMS_OUTPUT.PUT_LINE('End ChangeErrorMsg');
3810 
3811 
3812 end Change_Error_Msg;
3813 
3814 /*-------------------------------------------------------------------------------------------------------------------
3815    Import_Dim_Level
3816 -------------------------------------------------------------------------------------------------------------------*/
3817 
3818 PROCEDURE Import_Dim_Level(
3819   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
3820  ,p_Short_Name          IN      varchar2
3821  ,x_return_status       OUT NOCOPY     varchar2
3822  ,x_msg_count   OUT NOCOPY  number
3823  ,x_msg_data    OUT NOCOPY  varchar2
3824 ) IS
3825 
3826  v_Bsc_Pmf_Dim_Rec               BSC_PMF_UI_API_PUB.Bsc_Pmf_Dim_Rec_Type;
3827  v_commit                        varchar(5) := FND_API.G_FALSE;
3828  v_temp         number;
3829  v_level_view_name               varchar(200);
3830  v_sql                           varchar(300);
3831 
3832  TYPE RefCurTyp IS REF CURSOR;
3833  cv RefCurTyp;
3834 
3835 
3836 BEGIN
3837    FND_MSG_PUB.Initialize;
3838    x_return_status := FND_API.G_RET_STS_SUCCESS;
3839 
3840     v_Bsc_Pmf_Dim_Rec.Dimension_Level_Short_Name := p_Short_Name;
3841 
3842     -- Get the BSC Dimension Level Id of the Imported PMF Level
3843     v_temp :=  BSC_DIMENSION_LEVELS_PVT.get_Dim_Level_Id(v_Bsc_Pmf_Dim_Rec.Dimension_Level_Short_Name);
3844 
3845     if v_temp is null  then   /*  If the Dimension Level has not been Imported */
3846 
3847     v_sql := 'select LEVEL_VIEW_NAME from bsc_bis_dim_levels_v where SHORT_NAME = :1';
3848   OPEN cv FOR v_sql USING p_Short_Name;
3849   FETCH cv INTO v_level_view_name;
3850   --DBMS_OUTPUT.PUT_LINE(' Import_Dim_Level (wrapper)   -  v_level_view_name = ' || v_level_view_name );
3851 
3852         BSC_PMF_UI_API_PUB.Import_PMF_Dim_Level( v_commit ,v_Bsc_Pmf_Dim_Rec  ,x_return_status  ,x_msg_count  ,x_msg_data );
3853     end if;
3854 --DBMS_OUTPUT.PUT_LINE('End  Import_Dim_Level (wrapper) ' );
3855 
3856 EXCEPTION
3857   WHEN FND_API.G_EXC_ERROR THEN
3858     rollback;
3859     x_return_status := FND_API.G_RET_STS_ERROR;
3860     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
3861                               ,p_data   =>      x_msg_data);
3862     Change_Error_Msg(
3863     p_msg_name    => 'BSC_PMF_LEVEL_NOT_EXISTS'
3864    ,p_new_msg_name      => 'BSC_LVLREL_VIEW_NOTEXIST'
3865    ,p_token1            => 'LEVEL_SHORT_NAME'
3866    ,p_token1_value      => p_Short_Name
3867    ,p_token2            => 'LEVEL_VIEW_NAME'
3868    ,p_token2_value      => v_level_view_name
3869          ,p_sys_admin_flag      => 'Y'
3870    ,x_return_status     => x_return_status
3871    ,x_msg_count         => x_msg_count
3872    ,x_msg_data          => x_msg_data
3873     );
3874   WHEN OTHERS THEN /*FND_API.G_EXC_UNEXPECTED_ERROR THEN */
3875     rollback;
3876     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3877     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
3878                               ,p_data     =>      x_msg_data);
3879     Change_Error_Msg(
3880     p_new_msg_name      => ''
3881    ,x_return_status     => x_return_status
3882    ,x_msg_count         => x_msg_count
3883    ,x_msg_data          => x_msg_data
3884     );
3885 
3886 END Import_Dim_Level;
3887 
3888 /*-------------------------------------------------------------------------------------------------------------------
3889    Update_RelationShips
3890 -------------------------------------------------------------------------------------------------------------------*/
3891 PROCEDURE Update_RelationShips(
3892   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
3893  ,p_Dim_Level_Id        IN      number
3894  ,p_Short_Name          IN      varchar2
3895  ,p_Parents             IN      varchar2
3896  ,x_return_status       OUT NOCOPY     varchar2
3897  ,x_msg_count   OUT NOCOPY  number
3898  ,x_msg_data    OUT NOCOPY  varchar2
3899 ) IS
3900 
3901  x_array      t_of_varchar2;
3902  v_count number;
3903  v_Temp boolean;
3904  v_aux number;
3905 
3906  v_Num_Columns    INTEGER :=3 ;
3907  v_Num_Rows     INTEGER;
3908  I INTEGER ;
3909 
3910  v_array_Dim_Levels   t_of_Bsc_Dim_Level_Rec;
3911 
3912  v_Dim_Level_Rec  BSC_DIMENSION_LEVELS_PUB.Bsc_Dim_Level_Rec_Type;
3913  v_Bsc_Pmf_Dim_Rec      BSC_PMF_UI_API_PUB.Bsc_Pmf_Dim_Rec_Type;
3914  v_commit               varchar(5) := FND_API.G_FALSE;
3915 
3916  v_Dim_Level_Id   number;
3917 
3918     CURSOR c_parents IS
3919     SELECT PARENT_DIM_LEVEL_ID
3920     FROM   BSC_SYS_DIM_LEVEL_RELS
3921     WHERE  DIM_LEVEL_ID = v_Dim_Level_Id;
3922 
3923 BEGIN
3924    FND_MSG_PUB.Initialize;
3925    x_return_status := FND_API.G_RET_STS_SUCCESS;
3926   -- Ckeck the Imput parameter
3927 /*
3928  if p_Dim_Level_Id Is null then
3929        v_Dim_Level_Rec.Bsc_Level_Id := null;
3930        v_Dim_Level_Rec.Bsc_Level_Short_Name := p_Short_Name;
3931        v_Dim_Level_Rec.Bsc_Source := 'PMF';
3932  else
3933       v_Dim_Level_Rec.Bsc_Level_Id := p_Dim_Level_Id;
3934       v_Dim_Level_Rec.Bsc_Level_Short_Name := null;
3935       v_Dim_Level_Rec.Bsc_Source := 'BSC';
3936  end if;
3937 */
3938 -- For now just work for PMF Levels
3939   v_Dim_Level_Rec.Bsc_Level_Id := null;
3940   v_Dim_Level_Rec.Bsc_Level_Short_Name := p_Short_Name;
3941   v_Dim_Level_Rec.Bsc_Source := 'PMF';
3942 
3943  if v_Dim_Level_Rec.Bsc_Source <> 'BSC' then
3944        v_Dim_Level_Rec.Bsc_Level_Id := BSC_DIMENSION_LEVELS_PVT.get_Dim_Level_Id(v_Dim_Level_Rec.Bsc_Level_Short_Name);
3945        if v_Dim_Level_Rec.Bsc_Level_Id is null then
3946            v_Bsc_Pmf_Dim_Rec.Dimension_Level_Short_Name := v_Dim_Level_Rec.Bsc_Level_Short_Name;
3947      BSC_PMF_UI_API_PUB.Import_PMF_Dim_Level(v_commit ,v_Bsc_Pmf_Dim_Rec
3948              ,x_return_status ,x_msg_count ,x_msg_data );
3949            v_Dim_Level_Rec.Bsc_Level_Id := BSC_DIMENSION_LEVELS_PVT.get_Dim_Level_Id(v_Dim_Level_Rec.Bsc_Level_Short_Name);
3950            BSC_MULTI_USER_PVT.Apply_Multi_User_Env('DIM_LEVEL', v_Dim_Level_Rec.Bsc_Level_Id
3951                            , null, null, null, 'LCK',  null, x_return_status,  x_msg_count  ,x_msg_data);
3952        end if;
3953  end if;
3954 
3955     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip    v_Dim_Level_Rec.Bsc_Source = ' || v_Dim_Level_Rec.Bsc_Source);
3956     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip    v_Dim_Level_Rec.Bsc_Level_Id = ' || v_Dim_Level_Rec.Bsc_Level_Id );
3957     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip    v_Dim_Level_Rec.Bsc_Level_Short_Name = ' || v_Dim_Level_Rec.Bsc_Level_Short_Name );
3958 
3959  v_count := Decompose_String_List(p_Parents, x_array, ';');
3960  v_Num_Rows := v_count / v_Num_Columns;
3961 
3962 
3963 -- Evaluate Get parameters and Evaluate the Relationships
3964 
3965 v_Temp := TRUE;
3966 
3967 FOR I IN 1.. v_Num_Rows LOOP
3968    -- Get the maim parameters for each relation --
3969    v_aux := (I - 1) * v_Num_Columns ;
3970 
3971     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip    v_aux = ' || v_aux );
3972     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip  x_array(v_aux+1) ' ||  x_array(v_aux+1) );
3973 
3974    if LOWER( x_array(v_aux+1)) = 'null' OR  x_array(v_aux+1) = '' then
3975        v_Dim_Level_Rec.Bsc_Parent_Level_Id := NULL;
3976        v_Dim_Level_Rec.Bsc_Parent_Level_Source := 'PMF';
3977    else
3978        v_Dim_Level_Rec.Bsc_Parent_Level_Id := TO_NUMBER(x_array(v_aux+1));
3979        v_Dim_Level_Rec.Bsc_Parent_Level_Source := 'BSC';
3980    end if;
3981 
3982     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip  x_array(v_aux+2) ' ||  x_array(v_aux+2) );
3983 
3984    if LOWER( x_array(v_aux+2)) = 'null' OR x_array(v_aux+2) = '' then
3985        v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name := NULL;
3986    else
3987        v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name := x_array(v_aux+2);
3988    end if;
3989    v_Dim_Level_Rec.Bsc_Relation_Column := x_array(v_aux+3);
3990    v_Dim_Level_Rec.Bsc_Relation_Type := 1;
3991 
3992     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip   v_Dim_Level_Rec.Bsc_Relation_Column = ' || v_Dim_Level_Rec.Bsc_Relation_Column );
3993     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip   v_Dim_Level_Rec.Bsc_Parent_Level_Source = ' || v_Dim_Level_Rec.Bsc_Parent_Level_Source );
3994     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip   v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name = ' || v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name );
3995 
3996    -- Evaluate if it need import the child dimension Level--
3997    if v_Dim_Level_Rec.Bsc_Parent_Level_Source <> 'BSC' then
3998 
3999        v_Dim_Level_Rec.Bsc_Parent_Level_Id := BSC_DIMENSION_LEVELS_PVT.get_Dim_Level_Id(v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name);
4000        if v_Dim_Level_Rec.Bsc_Parent_Level_Id is null then
4001           v_Bsc_Pmf_Dim_Rec.Dimension_Level_Short_Name := v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name;
4002 
4003           BSC_PMF_UI_API_PUB.Import_PMF_Dim_Level(v_commit ,v_Bsc_Pmf_Dim_Rec
4004              ,x_return_status ,x_msg_count ,x_msg_data );
4005 
4006           v_Dim_Level_Rec.Bsc_Parent_Level_Id := BSC_DIMENSION_LEVELS_PVT.get_Dim_Level_Id(v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name);
4007           BSC_MULTI_USER_PVT.Apply_Multi_User_Env('DIM_LEVEL', v_Dim_Level_Rec.Bsc_Level_Id, null, null, null
4008                                                    , 'LCK',  null, x_return_status,  x_msg_count  ,x_msg_data);
4009 
4010        end if;
4011    end if;
4012     --DBMS_OUTPUT.PUT_LINE('Update_RelationShip    v_Dim_Level_Rec.Bsc_Parent_Level_Id = ' || v_Dim_Level_Rec.Bsc_Parent_Level_Id);
4013 
4014    -- Evaluate if the relation is valid --
4015    if BSC_DIMENSION_LEVELS_PUB.Is_Valid_Relationship(v_commit, v_Dim_Level_Rec, x_return_status, x_msg_count, x_msg_data) then
4016      -- Set the Relation into the array --
4017         v_array_Dim_Levels(v_Dim_Level_Rec.Bsc_Parent_Level_Id):=  v_Dim_Level_Rec;
4018    else
4019   v_Temp :=  false;
4020    end if;
4021 
4022  END LOOP;
4023           --DBMS_OUTPUT.PUT_LINE('Update_RelationShip ******** Delete ****** ');
4024 
4025 if v_Temp  then
4026 -- Delete the Relationships that Not apply any more ---
4027 
4028    v_Dim_Level_Id := v_Dim_Level_Rec.Bsc_Level_Id;  /* set Query parameter */
4029    IF (c_parents%ISOPEN) THEN
4030         CLOSE c_parents;
4031    END IF;
4032    OPEN c_parents;
4033    LOOP
4034      FETCH c_parents INTO v_Dim_Level_Rec.Bsc_Parent_Level_Id;
4035      EXIT WHEN c_parents%NOTFOUND;
4036 
4037         if v_array_Dim_Levels.EXISTS(v_Dim_Level_Rec.Bsc_Parent_Level_Id) = false then
4038 
4039             --DBMS_OUTPUT.PUT_LINE('Update_RelationShip  Delete_Dim_Level_Relation ' ||   v_Dim_Level_Rec.Bsc_Parent_Level_Id);
4040 
4041             BSC_DIMENSION_LEVELS_PUB.Delete_Dim_Level_Relation(v_commit, v_Dim_Level_Rec
4042                                            ,x_return_status, x_msg_count, x_msg_data);
4043             BSC_MULTI_USER_PVT. Apply_Multi_User_Env('DIM_LEVEL', v_Dim_Level_Rec.Bsc_Level_Id
4044                            , null, null, null, 'LCK',  null, x_return_status,  x_msg_count  ,x_msg_data);
4045         else
4046             v_array_Dim_Levels(v_Dim_Level_Rec.Bsc_Parent_Level_Id).Bsc_Flag := -999;
4047         end if;
4048    END LOOP;
4049 
4050 -- Update/Insert the Apply the Relationships
4051 
4052           --DBMS_OUTPUT.PUT_LINE('Update_RelationShip ***** Update/Insert ****');
4053 
4054  if v_array_Dim_Levels.COUNT > 0 then
4055     v_aux := v_array_Dim_Levels.FIRST;
4056 
4057     LOOP
4058       v_Dim_Level_Rec := v_array_Dim_Levels(v_aux);
4059       if v_Dim_Level_Rec.Bsc_Flag = -999 then  /* Flag for Update */
4060           --DBMS_OUTPUT.PUT_LINE('Update_RelationShip  Delete  ' || v_Dim_Level_Rec.Bsc_Parent_Level_Id  );
4061 
4062             BSC_DIMENSION_LEVELS_PUB.Delete_Dim_Level_Relation(v_commit, v_Dim_Level_Rec
4063                                            ,x_return_status, x_msg_count, x_msg_data);
4064       end if;
4065           --DBMS_OUTPUT.PUT_LINE('Update_RelationShip  Insert  ' || v_Dim_Level_Rec.Bsc_Parent_Level_Id  );
4066 
4067       BSC_DIMENSION_LEVELS_PUB.Create_Dim_Level_Relation(v_commit, v_Dim_Level_Rec
4068                                            ,x_return_status, x_msg_count, x_msg_data);
4069 
4070       EXIT WHEN (v_aux = v_array_Dim_Levels.LAST);
4071       v_aux := v_array_Dim_Levels.NEXT(v_aux);
4072    END LOOP;
4073  end if;
4074 end if;
4075     --DBMS_OUTPUT.PUT_LINE('End Update_RelationShip');
4076 
4077   IF (p_commit = FND_API.G_TRUE) THEN
4078     COMMIT;
4079   END IF;
4080 EXCEPTION
4081   WHEN FND_API.G_EXC_ERROR THEN
4082     rollback;
4083     IF (c_parents%ISOPEN) THEN
4084         CLOSE c_parents;
4085     END IF;
4086     x_return_status := FND_API.G_RET_STS_ERROR;
4087     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
4088                               ,p_data   =>      x_msg_data);
4089   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4090     rollback;
4091     IF (c_parents%ISOPEN) THEN
4092         CLOSE c_parents;
4093     END IF;
4094     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4095     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4096                               ,p_data     =>      x_msg_data);
4097   WHEN NO_DATA_FOUND THEN
4098     rollback;
4099     IF (c_parents%ISOPEN) THEN
4100         CLOSE c_parents;
4101     END IF;
4102     x_return_status := FND_API.G_RET_STS_ERROR;
4103     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4104                               ,p_data     =>      x_msg_data);
4105   WHEN OTHERS THEN
4106     rollback;
4107     IF (c_parents%ISOPEN) THEN
4108         CLOSE c_parents;
4109     END IF;
4110     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4111     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4112                               ,p_data     =>      x_msg_data);
4113 
4114 END Update_RelationShips;
4115 
4116 
4117 /*===========================================================================+
4118 | FUNCTION Decompose_String_List
4119 +============================================================================*/
4120 
4121 FUNCTION Decompose_String_List(
4122  x_string IN VARCHAR2,
4123  x_varchar2_array IN OUT NOCOPY t_of_varchar2,
4124  x_separator IN VARCHAR2
4125  ) RETURN VARCHAR2 IS
4126 
4127     h_num_items NUMBER := 0;
4128 
4129     h_sub_string VARCHAR2(32700);
4130     h_position NUMBER;
4131 
4132 BEGIN
4133    FND_MSG_PUB.Initialize;
4134       --DBMS_OUTPUT.PUT_LINE('Begin Decompose_String_List');
4135 
4136     IF x_string IS NOT NULL THEN
4137         h_sub_string := x_string;
4138         h_position := INSTR(h_sub_string, x_separator);
4139 
4140         WHILE h_position <> 0 LOOP
4141             h_num_items := h_num_items + 1;
4142             x_varchar2_array(h_num_items) :=
4143                            RTRIM(LTRIM(SUBSTR(h_sub_string, 1, h_position - 1)));
4144 
4145             h_sub_string := SUBSTR(h_sub_string, h_position + 1);
4146             h_position := INSTR(h_sub_string, x_separator);
4147         END LOOP;
4148 
4149         h_num_items := h_num_items + 1;
4150         x_varchar2_array(h_num_items) := RTRIM(LTRIM(h_sub_string));
4151 
4152     END IF;
4153 
4154       --DBMS_OUTPUT.PUT_LINE('End Decompose_String_List');
4155 
4156 
4157     RETURN h_num_items;
4158 
4159 END Decompose_String_List;
4160 
4161 /*===========================================================================+
4162 | FUNCTION Create_PMF_Relationship
4163 +============================================================================*/
4164 procedure Create_PMF_Relationship (
4165   p_commit               IN      varchar := FND_API.G_FALSE
4166  ,p_SHORT_NAME        IN      VARCHAR2
4167  ,p_PARENT_SHORT_NAME   IN      VARCHAR2
4168  ,p_RELATION_COL        IN      VARCHAR2
4169 
4170 ) is
4171 
4172  v_Dim_Level_Rec BSC_DIMENSION_LEVELS_PUB.Bsc_Dim_Level_Rec_Type;
4173   x_return_status       varchar2(3000);
4174   x_msg_count   number;
4175   x_msg_data    varchar2(3000);
4176   v_count               number;
4177 
4178 begin
4179    FND_MSG_PUB.Initialize;
4180    x_return_status := FND_API.G_RET_STS_SUCCESS;
4181 --DBMS_OUTPUT.PUT_LINE('Begin Create_PMF_Relationship' );
4182 
4183   v_Dim_Level_Rec.Bsc_Source := 'PMF';
4184   v_Dim_Level_Rec.Bsc_Level_Short_Name := p_SHORT_NAME;
4185   v_Dim_Level_Rec.Bsc_Relation_Column := p_RELATION_COL;
4186   v_Dim_Level_Rec.Bsc_Relation_Type := 1;
4187   v_Dim_Level_Rec.Bsc_Parent_Level_Source := 'PMF';
4188   v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name := p_PARENT_SHORT_NAME;
4189 
4190       --DBMS_OUTPUT.PUT_LINE('  Create_PMF_Relationship     v_Dim_Level_Rec.Bsc_Level_Short_Name = ' || v_Dim_Level_Rec.Bsc_Level_Short_Name );
4191       --DBMS_OUTPUT.PUT_LINE('  Create_PMF_Relationship     v_Dim_Level_Rec.Bsc_Relation_Column = '  || v_Dim_Level_Rec.Bsc_Relation_Column );
4192       --DBMS_OUTPUT.PUT_LINE('  Create_PMF_Relationship     v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name =  ' || v_Dim_Level_Rec.Bsc_Parent_Level_Short_Name );
4193 
4194         select count(*)
4195            into v_count
4196            from BSC_SYS_DIM_LEVEL_RELS_V
4197      where SHORT_NAME = p_SHORT_NAME
4198              AND PARENT_SHORT_NAME = p_PARENT_SHORT_NAME;
4199 
4200   --DBMS_OUTPUT.PUT_LINE('  Create_PMF_Relationship     v_count =  ' || v_count );
4201 
4202         if v_count <> 0 then
4203               --DBMS_OUTPUT.PUT_LINE('  Create_PMF_Relationship     Deleting Relation  ');
4204 
4205           BSC_DIMENSION_LEVELS_PUB.Delete_Dim_Level_Relation(FND_API.G_FALSE, v_Dim_Level_Rec
4206                                            ,x_return_status, x_msg_count, x_msg_data);
4207            IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4208                RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4209            END IF;
4210         end if;
4211   BSC_DIMENSION_LEVELS_PUB.Create_Dim_Level_Relation(FND_API.G_FALSE, v_Dim_Level_Rec
4212                                            ,x_return_status, x_msg_count, x_msg_data);
4213    IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4214        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4215    END IF;
4216   IF (p_commit = FND_API.G_TRUE) THEN
4217     COMMIT;
4218   END IF;
4219 --DBMS_OUTPUT.PUT_LINE('End Create_PMF_Relationship' );
4220 
4221 EXCEPTION
4222   WHEN FND_API.G_EXC_ERROR THEN
4223     rollback;
4224     raise;
4225   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4226     rollback;
4227     raise;
4228   WHEN NO_DATA_FOUND THEN
4229     rollback;
4230     raise;
4231   WHEN OTHERS THEN
4232     rollback;
4233     raise;
4234 end Create_PMF_Relationship;
4235 
4236 
4237 /************************************************************************************
4238 ************************************************************************************/
4239 
4240 procedure Order_Tab_Index(
4241   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
4242  ,p_tab_ids             IN      varchar2
4243  ,x_return_status       OUT NOCOPY     varchar2
4244  ,x_msg_count           OUT NOCOPY     number
4245  ,x_msg_data            OUT NOCOPY     varchar2
4246 ) is
4247 
4248 TYPE Bsc_Tab_Index_Rec is RECORD(
4249   Bsc_Tab_Id    number
4250  ,Bsc_Tab_Index   number
4251 );
4252 
4253 TYPE Bsc_Tab_Index_Tbl IS TABLE OF Bsc_Tab_Index_Rec
4254   INDEX BY BINARY_INTEGER;
4255 
4256 Bsc_Tab_Index     Bsc_Tab_Index_Tbl;
4257 
4258 l_tab_id_string     varchar2(1000);
4259 
4260 l_tab_id      number;
4261 l_index       number;
4262 l_pos       number;
4263 
4264 begin
4265    FND_MSG_PUB.Initialize;
4266    x_return_status := FND_API.G_RET_STS_SUCCESS;
4267    SAVEPOINT BscPmfUIOrdTabIndex;
4268 
4269   l_tab_id_string := p_tab_ids;
4270   l_index := 0;
4271 
4272   -- do the loop while l_tab_id_string contains characters.
4273   while length(l_tab_id_string) > 0 loop
4274     -- find position of next semi-colon.
4275     l_pos := instr(l_tab_id_string, ';');
4276 
4277     -- get the first id in current string.
4278     l_tab_id := substr(l_tab_id_string, 1, l_pos -1);
4279 
4280     -- assign the current tab_id and current index to the Record Table.
4281     Bsc_Tab_Index(l_index + 1).Bsc_Tab_Id := l_tab_id;
4282     Bsc_Tab_Index(l_index + 1).Bsc_Tab_Index := l_index;
4283 
4284     l_index := l_index + 1;
4285 
4286     -- update the string to contain characters starting from the semi-colon
4287     -- position found earlier.
4288     l_tab_id_string := substr(l_tab_id_string, l_pos + 1);
4289 
4290 
4291   end loop;
4292 
4293   -- Update Table
4294   for i in 1..Bsc_Tab_Index.count loop
4295 
4296     update BSC_TABS_B
4297        set tab_index = Bsc_Tab_Index(i).Bsc_Tab_Index
4298      where tab_id = Bsc_Tab_Index(i).Bsc_Tab_Id;
4299 
4300   end loop;
4301 
4302   IF (p_commit = FND_API.G_TRUE) THEN
4303     COMMIT;
4304   END IF;
4305 
4306 EXCEPTION
4307     WHEN FND_API.G_EXC_ERROR THEN
4308         ROLLBACK TO BscPmfUIOrdTabIndex;
4309         IF (x_msg_data IS NULL) THEN
4310             FND_MSG_PUB.Count_And_Get
4311             (      p_encoded   =>  FND_API.G_FALSE
4312                ,   p_count     =>  x_msg_count
4313                ,   p_data      =>  x_msg_data
4314             );
4315         END IF;
4316         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
4317         x_return_status :=  FND_API.G_RET_STS_ERROR;
4318     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4319         ROLLBACK TO BscPmfUIOrdTabIndex;
4320         IF (x_msg_data IS NULL) THEN
4321             FND_MSG_PUB.Count_And_Get
4322             (      p_encoded   =>  FND_API.G_FALSE
4323                ,   p_count     =>  x_msg_count
4324                ,   p_data      =>  x_msg_data
4325             );
4326         END IF;
4327         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4328         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
4329     WHEN NO_DATA_FOUND THEN
4330         ROLLBACK TO BscPmfUIOrdTabIndex;
4331         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4332         IF (x_msg_data IS NOT NULL) THEN
4333             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Order_Tab_Index ';
4334         ELSE
4335             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Order_Tab_Index ';
4336         END IF;
4337         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
4338     WHEN OTHERS THEN
4339         ROLLBACK TO BscPmfUIOrdTabIndex;
4340         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4341         IF (x_msg_data IS NOT NULL) THEN
4342             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Order_Tab_Index ';
4343         ELSE
4344             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Order_Tab_Index ';
4345         END IF;
4346         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
4347 
4348 end Order_Tab_Index;
4349 
4350 /************************************************************************************
4351 ************************************************************************************/
4352 
4353 procedure Update_Tab_Parent(
4354   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
4355  ,p_tab_id              IN      number
4356  ,p_parent_tab_id       IN      number
4357  ,x_return_status       OUT NOCOPY     varchar2
4358  ,x_msg_count           OUT NOCOPY     number
4359  ,x_msg_data            OUT NOCOPY     varchar2
4360 ) is
4361 
4362 l_Bsc_Tab_Entity_Rec    BSC_SCORECARD_PUB.Bsc_Tab_Entity_Rec;
4363 
4364 l_commit                        varchar2(10);
4365 l_tab_name      varchar2(105);
4366 l_tab_index     number;
4367 l_return_value                  varchar2(10);
4368 
4369 l_count       number;
4370 l_rollback                      number := 0;  -- flag to prevent rollback for a specific
4371                                               -- case.
4372 
4373 begin
4374    FND_MSG_PUB.Initialize;
4375    x_return_status := FND_API.G_RET_STS_SUCCESS;
4376    SAVEPOINT BscPmfUIUptTabParent;
4377   l_commit := FND_API.G_FALSE;
4378 
4379 --DBMS_OUTPUT.PUT_LINE('Begin Update_Tab_Parent' );
4380 
4381 --DBMS_OUTPUT.PUT_LINE('  Update_Tab_Parent   p_tab_id = ' || p_tab_id );
4382 --DBMS_OUTPUT.PUT_LINE('  Update_Tab_Parent   p_parent_tab_id = ' || p_parent_tab_id );
4383 
4384   -- execute lock on parent tab_id.
4385   -- Execute the lock only if parent tab id is a valid tab, i.e. not the root node.
4386   if p_parent_tab_id <> -2 then
4387 
4388     BSC_MULTI_USER_PVT.Tab_Details_Lock( p_parent_tab_id
4389                                         ,x_return_status
4390                                         ,x_msg_count
4391                                         ,x_msg_data);
4392     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4393        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4394     END IF;
4395     if x_return_status = 'D' then
4396       -- set the rollback flag so as not to do a rollback.
4397       -- this will eventually be removed.
4398       l_rollback := 1;
4399       FND_MESSAGE.SET_NAME('BSC','BSC_NO_PARENT_TAB');
4400       FND_MESSAGE.SET_TOKEN('BSC_TAB', p_parent_tab_id);
4401       FND_MSG_PUB.ADD;
4402       RAISE FND_API.G_EXC_ERROR;
4403     end if;
4404 
4405   end if;
4406 
4407 
4408 
4409   -- check to see tab is not being made a child of itself.
4410   if p_tab_id = p_parent_tab_id then
4411     --get tab name to display error.
4412     select name into l_tab_name
4413       from BSC_TABS_VL where tab_id = p_tab_id;
4414     FND_MESSAGE.SET_NAME('BSC','BSC_TAB_SELF_REL');
4415     FND_MESSAGE.SET_TOKEN('BSC_TAB', l_tab_name);
4416     FND_MSG_PUB.ADD;
4417     RAISE FND_API.G_EXC_ERROR;
4418   else
4419     --Evaluate circular parent reference to fixs bug 2406652--
4420     l_return_value := BSC_SCORECARD_PUB.is_child_tab_of( p_parent_tab_id, p_tab_id );
4421     if l_return_value = FND_API.G_TRUE then
4422       --DBMS_OUTPUT.PUT_LINE('  Update_Tab_Parent   l_return_value = ' || l_return_value );
4423       FND_MESSAGE.SET_NAME('BSC','BSC_TAB_CIRCULAR_REL');
4424   select name into l_tab_name
4425         from BSC_TABS_VL where tab_id = p_tab_id;
4426       FND_MESSAGE.SET_TOKEN('BSC_TAB', l_tab_name);
4427   select name into l_tab_name
4428         from BSC_TABS_VL where tab_id = p_parent_tab_id;
4429       FND_MESSAGE.SET_TOKEN('BSC_TABCHILD', l_tab_name);
4430       FND_MSG_PUB.ADD;
4431       RAISE FND_API.G_EXC_ERROR;
4432     end if;
4433 
4434   end if;
4435 
4436 
4437   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id := p_tab_id;
4438   l_Bsc_Tab_Entity_Rec.Bsc_Parent_Tab_Id := p_parent_tab_id;
4439 
4440 
4441   -- if parent tab_id = -2 then the tab being moved will be a root node and
4442   -- need to change parent to null.
4443 --  if p_parent_tab_id <> -2 then
4444 --    l_Bsc_Tab_Entity_Rec.Bsc_Parent_Tab_Id := p_parent_tab_id;
4445 --  end if;
4446 
4447   --set some default values
4448   l_Bsc_Tab_Entity_Rec.Bsc_Language := 'US';
4449   l_Bsc_Tab_Entity_Rec.Bsc_Source_Language := 'US';
4450 
4451   -- The tab (tab_id) is being moved to a different parent.  This tab needs an
4452   -- Index under the new parent.  We will assing the next Index.
4453   select max(tab_index) + 1
4454     into l_Bsc_Tab_Entity_Rec.Bsc_Tab_Index
4455     from BSC_TABS_B
4456    where tab_id = p_parent_tab_id;
4457 
4458   -- call update Tab procedure.
4459   BSC_SCORECARD_PUB.Update_Tab( FND_API.G_FALSE
4460                                ,l_Bsc_Tab_Entity_Rec
4461                                ,x_return_status
4462                                ,x_msg_count
4463                                ,x_msg_data);
4464     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4465        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4466     END IF;
4467 
4468   -- call time stamp for Tab
4469   BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
4470                                           ,l_Bsc_Tab_Entity_Rec
4471                                           ,x_return_status
4472                                           ,x_msg_count
4473                                           ,x_msg_data);
4474     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4475        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4476     END IF;
4477   -- Update time stamp for parent tab.
4478   l_Bsc_Tab_Entity_Rec.Bsc_Tab_Id := p_parent_tab_id;
4479   BSC_SCORECARD_PUB.Update_Tab_Time_Stamp( FND_API.G_FALSE
4480                                           ,l_Bsc_Tab_Entity_Rec
4481                                           ,x_return_status
4482                                           ,x_msg_count
4483                                           ,x_msg_data);
4484     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4485        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4486     END IF;
4487   -- Update system time stamp.
4488   BSC_SCORECARD_PUB.Update_System_Time_Stamp( FND_API.G_FALSE
4489                                              ,l_Bsc_Tab_Entity_Rec
4490                                              ,x_return_status
4491                                              ,x_msg_count
4492                                              ,x_msg_data);
4493     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4494        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4495     END IF;
4496   IF (p_commit = FND_API.G_TRUE) THEN
4497     COMMIT;
4498   END IF;
4499 
4500 --DBMS_OUTPUT.PUT_LINE('End Update_Tab_Parent' );
4501 
4502 EXCEPTION
4503     WHEN FND_API.G_EXC_ERROR THEN
4504         ROLLBACK TO BscPmfUIUptTabParent;
4505         IF (x_msg_data IS NULL) THEN
4506             FND_MSG_PUB.Count_And_Get
4507             (      p_encoded   =>  FND_API.G_FALSE
4508                ,   p_count     =>  x_msg_count
4509                ,   p_data      =>  x_msg_data
4510             );
4511         END IF;
4512         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
4513         x_return_status :=  FND_API.G_RET_STS_ERROR;
4514     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4515         ROLLBACK TO BscPmfUIUptTabParent;
4516         IF (x_msg_data IS NULL) THEN
4517             FND_MSG_PUB.Count_And_Get
4518             (      p_encoded   =>  FND_API.G_FALSE
4519                ,   p_count     =>  x_msg_count
4520                ,   p_data      =>  x_msg_data
4521             );
4522         END IF;
4523         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4524         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
4525     WHEN NO_DATA_FOUND THEN
4526         ROLLBACK TO BscPmfUIUptTabParent;
4527         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4528         IF (x_msg_data IS NOT NULL) THEN
4529             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab_Parent ';
4530         ELSE
4531             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab_Parent ';
4532         END IF;
4533         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
4534     WHEN OTHERS THEN
4535         ROLLBACK TO BscPmfUIUptTabParent;
4536         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4537         IF (x_msg_data IS NOT NULL) THEN
4538             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Tab_Parent ';
4539         ELSE
4540             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Tab_Parent ';
4541         END IF;
4542         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
4543 
4544 
4545 end Update_Tab_Parent;
4546 
4547 /************************************************************************************
4548 ************************************************************************************/
4549 
4550 procedure Create_Measure(
4551   p_commit                IN          VARCHAR2 := FND_API.G_TRUE
4552  ,p_short_name          IN      varchar2
4553  ,x_return_status       OUT NOCOPY     varchar2
4554  ,x_msg_count           OUT NOCOPY     number
4555  ,x_msg_data            OUT NOCOPY     varchar2
4556 ) is
4557 
4558 l_Bsc_Dataset_Rec               BSC_DATASETS_PUB.Bsc_Dataset_Rec_Type;
4559 l_commit                        varchar2(15);
4560 l_measure_col                   BSC_SYS_MEASURES.MEASURE_COL%TYPE;
4561 
4562 begin
4563    FND_MSG_PUB.Initialize;
4564    x_return_status := FND_API.G_RET_STS_SUCCESS;
4565    SAVEPOINT BscPmfUICrtMeas;
4566   l_commit := FND_API.G_FALSE;
4567 
4568   -- Get PMF measure long based on the shortname.
4569   select distinct measure_name
4570     into l_Bsc_Dataset_Rec.Bsc_Dataset_Name
4571     from BISFV_PERFORMANCE_MEASURES
4572    where upper(measure_short_name) = upper(p_short_name);
4573 
4574   -- Set some expected values.
4575   l_Bsc_Dataset_Rec.Bsc_Measure_Short_Name := p_short_name;
4576   l_Bsc_Dataset_Rec.Bsc_Measure_Operation := 'SUM';
4577   --l_Bsc_Dataset_Rec.Bsc_Measure_Col := p_short_name;
4578   l_Bsc_Dataset_Rec.Bsc_Language := 'US';
4579   l_Bsc_Dataset_Rec.Bsc_Source_Language := 'US';
4580   l_Bsc_Dataset_Rec.Bsc_Source := 'PMF';
4581   l_measure_col := BSC_BIS_MEASURE_PUB.get_measure_col(l_Bsc_Dataset_Rec.Bsc_Dataset_Name, NULL, NULL, p_short_name);
4582   if (l_measure_col is not null) then
4583       l_Bsc_Dataset_Rec.Bsc_Measure_Col := l_measure_col;
4584   else
4585       l_Bsc_Dataset_Rec.Bsc_Measure_Col := p_short_name;
4586   end if;
4587 
4588 
4589   BSC_DATASETS_PUB.Create_Measures( FND_API.G_FALSE
4590                                    ,l_Bsc_Dataset_Rec
4591                                    ,x_return_status
4592                                    ,x_msg_count
4593                                    ,x_msg_data);
4594     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4595        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4596     END IF;
4597   IF (p_commit = FND_API.G_TRUE) THEN
4598     COMMIT;
4599   END IF;
4600 
4601 EXCEPTION
4602     WHEN FND_API.G_EXC_ERROR THEN
4603         ROLLBACK TO BscPmfUICrtMeas;
4604         IF (x_msg_data IS NULL) THEN
4605             FND_MSG_PUB.Count_And_Get
4606             (      p_encoded   =>  FND_API.G_FALSE
4607                ,   p_count     =>  x_msg_count
4608                ,   p_data      =>  x_msg_data
4609             );
4610         END IF;
4611         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
4612         x_return_status :=  FND_API.G_RET_STS_ERROR;
4613     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4614         ROLLBACK TO BscPmfUICrtMeas;
4615         IF (x_msg_data IS NULL) THEN
4616             FND_MSG_PUB.Count_And_Get
4617             (      p_encoded   =>  FND_API.G_FALSE
4618                ,   p_count     =>  x_msg_count
4619                ,   p_data      =>  x_msg_data
4620             );
4621         END IF;
4622         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4623         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
4624     WHEN NO_DATA_FOUND THEN
4625         ROLLBACK TO BscPmfUICrtMeas;
4626         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4627         IF (x_msg_data IS NOT NULL) THEN
4628             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Measure ';
4629         ELSE
4630             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Measure ';
4631         END IF;
4632         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
4633     WHEN OTHERS THEN
4634         ROLLBACK TO BscPmfUICrtMeas;
4635         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4636         IF (x_msg_data IS NOT NULL) THEN
4637             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Create_Measure ';
4638         ELSE
4639             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Create_Measure ';
4640         END IF;
4641         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
4642 
4643 
4644 end Create_Measure;
4645 
4646 /************************************************************************************
4647 ************************************************************************************/
4648 
4649 procedure Create_Measure_VB(
4650   p_short_name          IN      varchar2
4651 ) is
4652 
4653  l_return_status       varchar2(30);
4654  l_msg_count           number;
4655  l_msg_data            varchar2(32000);
4656 
4657  l_count    number;
4658  e_error    exception;
4659 
4660 begin
4661    FND_MSG_PUB.Initialize;
4662   BSC_APPS.Init_Bsc_Apps;
4663   BSC_MESSAGE.Init;
4664 
4665   -- Create the measure
4666   Create_Measure(
4667       p_short_name => p_short_name
4668       ,x_return_status => l_return_status
4669       ,x_msg_count => l_msg_count
4670       ,x_msg_data => l_msg_data
4671   );
4672     IF ((l_return_status IS NOT NULL) AND (l_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4673        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4674     END IF;
4675   -- Verify that the dataset was created
4676   SELECT count(*)
4677   INTO l_count
4678   FROM bsc_sys_datasets_vl
4679   WHERE measure_id1 = (
4680      SELECT measure_id
4681      FROM bsc_sys_measures
4682      WHERE short_name = p_short_name);
4683 
4684   IF l_count = 0 THEN
4685       RAISE e_error;
4686   END IF;
4687 
4688 
4689 EXCEPTION
4690     WHEN e_error THEN
4691         BSC_MESSAGE.Add(x_message => bsc_apps.get_message('BSC_MEASURE_NOT_IMPORTED'),
4692                         x_source => 'BSC_PMF_UI_WRAPPER.Create_Measure_Vb',
4693                         x_mode => 'I');
4694         COMMIT;
4695     WHEN OTHERS THEN
4696         BSC_MESSAGE.Add(x_message => SQLERRM,
4697                         x_source => 'BSC_PMF_UI_WRAPPER.Create_Measure_Vb',
4698                         x_mode => 'I');
4699         COMMIT;
4700 end Create_Measure_VB;
4701 
4702 /************************************************************************************
4703 ************************************************************************************/
4704 
4705 procedure Get_PMV_Report_Levels(
4706   p_region_code         IN      varchar2
4707  ,p_measure_short_name  IN      varchar2
4708  ,x_dim1_name   OUT NOCOPY     varchar2
4709  ,x_dim1_levels   OUT NOCOPY  varchar2
4710  ,x_dim2_name           OUT NOCOPY     varchar2
4711  ,x_dim2_levels         OUT NOCOPY     varchar2
4712  ,x_dim3_name           OUT NOCOPY     varchar2
4713  ,x_dim3_levels         OUT NOCOPY     varchar2
4714  ,x_dim4_name           OUT NOCOPY     varchar2
4715  ,x_dim4_levels         OUT NOCOPY     varchar2
4716  ,x_dim5_name           OUT NOCOPY     varchar2
4717  ,x_dim5_levels         OUT NOCOPY     varchar2
4718  ,x_dim6_name           OUT NOCOPY     varchar2
4719  ,x_dim6_levels         OUT NOCOPY     varchar2
4720  ,x_dim7_name           OUT NOCOPY     varchar2
4721  ,x_dim7_levels         OUT NOCOPY     varchar2
4722  ,x_is_there_time       OUT NOCOPY     varchar2
4723  ,x_return_status       OUT NOCOPY     varchar2
4724  ,x_msg_count           OUT NOCOPY     number
4725  ,x_msg_data            OUT NOCOPY     varchar2
4726 ) is
4727 
4728 TYPE Pmv_Dim_Levels_Rec IS RECORD (
4729   Dim_Name      varchar2(30)
4730  ,Dim_Short_Level   varchar2(500)
4731  ,Dim_Number      number
4732  ,ViewByLevel     varchar2(5)
4733  ,AllLevel      varchar2(5)
4734 );
4735 
4736 TYPE Pmv_Dim_Levels_Tbl IS TABLE OF Pmv_Dim_Levels_Rec
4737   INDEX BY BINARY_INTEGER;
4738 
4739 l_Pmv_Dim_Levels_Tbl    Pmv_Dim_Levels_Tbl;
4740 
4741 l_DimLevel_Viewby_Tbl   BIS_PMV_BSC_API_PUB.DimLevel_Viewby_Tbl_Type;
4742 l_cntr        number;
4743 l_2cntr       number;
4744 l_number_records    number;
4745 
4746 l_new_level     varchar2(1);
4747 l_temp_dim      varchar2(30);
4748 l_temp_level      varchar2(500);
4749 
4750 is_time_dim     varchar2(1) := 'N';
4751 sub_string      number := -1;
4752 
4753 l_region_code     varchar2(30);
4754 l_function_code     varchar2(30);
4755 
4756 begin
4757    FND_MSG_PUB.Initialize;
4758    x_return_status := FND_API.G_RET_STS_SUCCESS;
4759   -- When this call was first implemented, PMV had mistakenly advice on using
4760   -- the most recent region code to obtain the proper report for the given measure
4761   -- After further research it was determined that this wrapper should be making
4762   -- the same call as Iviewer is, which does not coincide with PMVs advice.
4763   -- In order not to change current java files, we are implementing the call
4764   -- completely within this Wrapper.  Therefore the region code passed to this
4765   -- wrapper is completely disregarded.
4766   -- For more references see bug# 2647833
4767   bsc_jv_pmf.get_pmf_measure( p_measure_short_name
4768                              ,l_function_code
4769                              ,l_region_code);
4770 
4771 
4772 
4773   x_is_there_time := 'N';
4774 
4775 --  BSC_PMF_UI_API_PUB.Get_DimLevel_Viewby( 1
4776   BIS_PMV_BSC_API_PUB.Get_DimLevel_Viewby( 1
4777                                          ,l_region_code
4778                                          ,p_measure_short_name
4779                                          ,l_DimLevel_Viewby_Tbl
4780                                          ,x_return_status
4781                                          ,x_msg_count
4782                                          ,x_msg_data);
4783     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
4784        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4785     END IF;
4786   -- loop over the TABLE RECORD obtained from above call. Parse it into the Dimension and dimension level.
4787   l_cntr := 1;
4788   for l_cntr in 1..l_DimLevel_Viewby_Tbl.count loop
4789     l_temp_dim := substr(l_DimLevel_Viewby_Tbl(l_cntr).Dim_DimLevel, 1, (instr(l_DimLevel_Viewby_Tbl(l_cntr).Dim_DimLevel, '+') - 1));
4790     l_temp_level := substr(l_DimLevel_Viewby_Tbl(l_cntr).Dim_DimLevel, (instr(l_DimLevel_Viewby_Tbl(l_cntr).Dim_DimLevel, '+')+1));
4791 
4792 
4793   -- Determine if the current dimension is time dimension
4794   if (is_time_dim = 'N') then
4795     sub_string := instr(l_temp_dim, 'TIME');
4796     if (sub_string > 0) then
4797       is_time_dim := 'Y';
4798       x_is_there_time := 'Y';
4799     end if;
4800   end if;
4801 
4802     -- With the parsed Dimension, check if it exists in the local TABLE Record.  If it exists then set
4803 
4804     -- the New Level Flag to 'N' and add the level to the level member.
4805     l_2cntr := 1;
4806     l_new_level := 'Y';
4807     l_number_records := l_Pmv_Dim_Levels_Tbl.count;
4808     for l_2cntr in 1..l_number_records loop
4809       if (l_Pmv_Dim_Levels_Tbl(l_2cntr).Dim_Name = l_temp_dim) then
4810         l_new_level := 'N';
4811         if (l_Pmv_Dim_Levels_Tbl(l_2cntr).Dim_Short_Level is null) then
4812           l_Pmv_Dim_Levels_Tbl(l_2cntr).Dim_Short_Level := l_temp_level || ', -' ||
4813             l_DimLevel_Viewby_Tbl(l_cntr).Viewby_Applicable || ',' ||
4814             l_DimLevel_Viewby_Tbl(l_cntr).All_Applicable || ';';
4815         else
4816           l_Pmv_Dim_Levels_Tbl(l_2cntr).Dim_Short_Level := l_Pmv_Dim_Levels_Tbl(l_2cntr).Dim_Short_Level ||
4817             l_temp_level || ', -' || l_DimLevel_Viewby_Tbl(l_cntr).Viewby_Applicable || ',' ||
4818             l_DimLevel_Viewby_Tbl(l_cntr).All_Applicable || ';';
4819         end if;
4820       end if;
4821     end loop;
4822 
4823     if (l_new_level = 'Y') then
4824       l_Pmv_Dim_Levels_Tbl(l_number_records + 1).Dim_Name := l_temp_dim;
4825       l_Pmv_Dim_Levels_Tbl(l_number_records + 1).Dim_Short_Level := l_temp_level || ', -' ||
4826                                  l_DimLevel_Viewby_Tbl(l_cntr).Viewby_Applicable || ',' ||
4827                                  l_DimLevel_Viewby_Tbl(l_cntr).All_Applicable || ';';
4828     end if;
4829 
4830   end loop;
4831 
4832   -- set the values in the record to the OUT NOCOPY parameters. Only setting up to the size l_number_records
4833   -- adeulgao changed from l_number_records to l_Pmv_Dim_Levels_Tbl.COUNT
4834 
4835   l_cntr := 1;
4836   for l_cntr in 1..l_Pmv_Dim_Levels_Tbl.COUNT loop
4837     -- set the values in the record to the OUT NOCOPY parameters.
4838         if (l_cntr =1) then
4839             x_dim1_name := l_Pmv_Dim_Levels_Tbl(1).Dim_Name;
4840             x_dim1_levels := l_Pmv_Dim_Levels_Tbl(1).Dim_Short_Level;
4841         elsif (l_cntr =2) then
4842             x_dim2_name := l_Pmv_Dim_Levels_Tbl(2).Dim_Name;
4843             x_dim2_levels := l_Pmv_Dim_Levels_Tbl(2).Dim_Short_Level;
4844         elsif (l_cntr =3) then
4845             x_dim3_name := l_Pmv_Dim_Levels_Tbl(3).Dim_Name;
4846             x_dim3_levels := l_Pmv_Dim_Levels_Tbl(3).Dim_Short_Level;
4847         elsif (l_cntr =4) then
4848             x_dim4_name := l_Pmv_Dim_Levels_Tbl(4).Dim_Name;
4849             x_dim4_levels := l_Pmv_Dim_Levels_Tbl(4).Dim_Short_Level;
4850         elsif (l_cntr =5) then
4851             x_dim5_name := l_Pmv_Dim_Levels_Tbl(5).Dim_Name;
4852             x_dim5_levels := l_Pmv_Dim_Levels_Tbl(5).Dim_Short_Level;
4853         elsif (l_cntr =6) then
4854             x_dim6_name := l_Pmv_Dim_Levels_Tbl(6).Dim_Name;
4855             x_dim6_levels := l_Pmv_Dim_Levels_Tbl(6).Dim_Short_Level;
4856         elsif (l_cntr =7) then
4857             x_dim7_name := l_Pmv_Dim_Levels_Tbl(7).Dim_Name;
4858             x_dim7_levels := l_Pmv_Dim_Levels_Tbl(7).Dim_Short_Level;
4859         end if;
4860   end loop;
4861 
4862  /***********************************************************
4863   if (l_Pmv_Dim_Levels_Tbl(1).Dim_Name is not null) then
4864     x_dim1_name := l_Pmv_Dim_Levels_Tbl(1).Dim_Name;
4865     x_dim1_levels := l_Pmv_Dim_Levels_Tbl(1).Dim_Short_Level;
4866   end if;
4867   if (l_Pmv_Dim_Levels_Tbl(2).Dim_Name is not null) then
4868     x_dim2_name := l_Pmv_Dim_Levels_Tbl(2).Dim_Name;
4869     x_dim2_levels := l_Pmv_Dim_Levels_Tbl(2).Dim_Short_Level;
4870   end if;
4871   if (l_Pmv_Dim_Levels_Tbl(3).Dim_Name is not null) then
4872     x_dim3_name := l_Pmv_Dim_Levels_Tbl(3).Dim_Name;
4873     x_dim3_levels := l_Pmv_Dim_Levels_Tbl(3).Dim_Short_Level;
4874   end if;
4875   if (l_Pmv_Dim_Levels_Tbl(4).Dim_Name is not null) then
4876     x_dim4_name := l_Pmv_Dim_Levels_Tbl(4).Dim_Name;
4877     x_dim4_levels := l_Pmv_Dim_Levels_Tbl(4).Dim_Short_Level;
4878   end if;
4879   if (l_Pmv_Dim_Levels_Tbl(5).Dim_Name is not null) then
4880     x_dim5_name := l_Pmv_Dim_Levels_Tbl(5).Dim_Name;
4881     x_dim5_levels := l_Pmv_Dim_Levels_Tbl(5).Dim_Short_Level;
4882   end if;
4883   if (l_Pmv_Dim_Levels_Tbl(6).Dim_Name is not null) then
4884     x_dim6_name := l_Pmv_Dim_Levels_Tbl(6).Dim_Name;
4885     x_dim6_levels := l_Pmv_Dim_Levels_Tbl(6).Dim_Short_Level;
4886   end if;
4887   if (l_Pmv_Dim_Levels_Tbl(7).Dim_Name is not null) then
4888     x_dim7_name := l_Pmv_Dim_Levels_Tbl(7).Dim_Name;
4889     x_dim7_levels := l_Pmv_Dim_Levels_Tbl(7).Dim_Short_Level;
4890   end if;
4891  *****************************************************************/
4892 
4893 EXCEPTION
4894   WHEN FND_API.G_EXC_ERROR THEN
4895     rollback;
4896     x_return_status := FND_API.G_RET_STS_ERROR;
4897     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
4898                               ,p_data   =>      x_msg_data);
4899   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4900     rollback;
4901     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4902     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4903                               ,p_data     =>      x_msg_data);
4904   WHEN NO_DATA_FOUND THEN
4905     rollback;
4906     x_return_status := FND_API.G_RET_STS_ERROR;
4907     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4908                               ,p_data     =>      x_msg_data);
4909   WHEN OTHERS THEN
4910     rollback;
4911     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4912     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
4913                               ,p_data     =>      x_msg_data);
4914 
4915 
4916 end Get_PMV_Report_Levels;
4917 
4918 /************************************************************************************
4919 ************************************************************************************/
4920 
4921 
4922 function get_KPI_Time_Stamp(
4923   p_kpi_id              IN      number
4924 ) return varchar2 is
4925    l_time_stamp  date;
4926 begin
4927    l_time_stamp := BSC_KPI_PUB.get_KPI_Time_Stamp(p_kpi_id);
4928    return  TO_CHAR(l_time_stamp , g_time_stamp_format );
4929 
4930 EXCEPTION
4931   WHEN OTHERS THEN
4932     return null;
4933 end get_KPI_Time_Stamp;
4934 
4935 /************************************************************************************
4936 ************************************************************************************/
4937 
4938 FUNCTION remove_percent(
4939   p_input IN VARCHAR2
4940 ) RETURN NUMBER IS
4941 BEGIN
4942 IF (SUBSTR(p_input, LENGTH(p_input), 1) = '%') THEN
4943   RETURN SUBSTR(p_input, 1, LENGTH(p_input)-1);
4944 ELSE
4945   RETURN p_input;
4946 END IF;
4947 END remove_percent;
4948 
4949 /************************************************************************************
4950 
4951   Adeulgao changed
4952 
4953   The API now call get_DimObj_ViewBy_Tbl()
4954   written in BSC_BIS_KPI_MEAS_PUB to get dimension levels
4955   associated with PMV report.
4956 
4957 ************************************************************************************/
4958 
4959 FUNCTION is_In_Dimension
4960 (       p_measure_short_name IN  VARCHAR2
4961     ,   p_dims_short_name    IN  VARCHAR2
4962     ,   p_dim_obj            IN  VARCHAR2
4963 ) RETURN VARCHAR2
4964 IS
4965     l_region_code          VARCHAR2(80);
4966     l_function_code      VARCHAR2(80);
4967     l_DimObj_ViewBy_Tbl    BSC_BIS_KPI_MEAS_PUB.DimObj_Viewby_Tbl_Type;
4968     l_return_status        VARCHAR2(10);
4969     l_msg_count            NUMBER;
4970     l_msg_data             VARCHAR2(2000);
4971     l_temp                 VARCHAR2(8000);
4972     l_table_index      NUMBER;
4973 
4974 BEGIN
4975     --DBMS_OUTPUT.PUT_LINE(' Before ');
4976 
4977     /*SELECT  DISTINCT region_code Region
4978     INTO    l_region_code
4979     FROM    ak_region_items
4980     WHERE   attribute1= 'MEASURE'
4981     AND     attribute2 = p_measure_short_name;*/
4982 
4983     --DBMS_OUTPUT.PUT_LINE(' After ');
4984 
4985     BSC_JV_PMF.get_Pmf_Measure
4986               (        p_Measure_ShortName   =>    p_Measure_Short_Name
4987                       ,x_function_name       =>    l_function_code
4988                       ,x_region_code         =>    l_Region_Code
4989           );
4990 
4991     IF (l_region_code IS NULL) THEN
4992       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
4993     END IF;
4994 
4995     --DBMS_OUTPUT.PUT_LINE(' ******* After 1');
4996 
4997     BSC_BIS_KPI_MEAS_PUB.get_DimObj_ViewBy_Tbl
4998         (       p_Measure_Short_Name   =>   p_measure_short_name
4999               ,   p_Region_Code          =>   l_region_code
5000               ,   x_DimObj_ViewBy_Tbl    =>   l_DimObj_ViewBy_Tbl
5001               ,   x_return_status        =>   l_return_status
5002               ,   x_msg_count            =>   l_msg_count
5003               ,   x_msg_data             =>   l_msg_data
5004           );
5005 
5006   IF (l_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5007     --DBMS_OUTPUT.PUT_LINE(' *** Error calling PMV API');
5008       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5009     END IF;
5010 
5011   FOR l_table_index IN 0..l_DimObj_ViewBy_Tbl.COUNT-1
5012   LOOP
5013 
5014     IF (TRIM(l_DimObj_ViewBy_Tbl(l_table_index).p_Dimension_Name) = p_dims_short_name ) THEN
5015 
5016       --DBMS_OUTPUT.PUT_LINE(' dimension found -- '||l_DimObj_ViewBy_Tbl(l_table_index).p_Dimension_Name);
5017       WHILE (is_more(  p_dim_short_names  =>  l_DimObj_ViewBy_Tbl(l_table_index).p_Dim_Object_Names
5018                           ,p_dim_name         =>  l_temp)
5019           ) LOOP
5020 
5021             --DBMS_OUTPUT.PUT_LINE(' searched -- '||l_temp);
5022 
5023               l_temp := l_temp||',';
5024               IF (TRIM(SUBSTR(l_temp, 0, INSTR(l_temp, ',') - 1)) = p_dim_obj) THEN
5025                   RETURN 'Y';
5026               END IF;
5027           END LOOP;
5028     END IF;
5029   END LOOP;
5030 
5031   RETURN 'N';
5032 
5033 EXCEPTION
5034     WHEN OTHERS THEN
5035     RAISE;
5036 END is_In_Dimension;
5037 
5038 FUNCTION is_group_selected
5039 (
5040     p_tab_id    IN  NUMBER
5041   ,   p_group_id  IN  NUMBER
5042 
5043 ) RETURN VARCHAR2
5044 IS
5045 l_count  NUMBER;
5046 
5047 BEGIN
5048 
5049   SELECT count(*)
5050   INTO  l_count
5051   FROM  BSC_TAB_IND_GROUPS_B
5052   WHERE tab_id =  p_tab_id
5053   AND   ind_group_id  =   p_group_id;
5054 
5055   IF (l_count = 0) THEN
5056     RETURN 'N';
5057   ELSE
5058     RETURN 'Y';
5059   END IF;
5060 
5061 EXCEPTION
5062     WHEN OTHERS THEN
5063         RETURN 'N';
5064 END is_group_selected;
5065 
5066 
5067 /********************************************************************************
5068  Name :- Assign_Kpi_Tab
5069  Description :- This procedure will assign the Kpis to the tab.
5070         It will internally call the existing API Assign_KPI.
5071         Internally it will get the group of the kpi and check if the kpi is
5072         assigned to the tab, if not then it will assign the group to the tab
5073         and after that assign the kpi to the tab.
5074 Input :-    p_tab_id
5075         p_kpi_ids  Comma separated kpi ids.
5076 Creator : - ashankar 13-FEB-2003
5077 /********************************************************************************/
5078 
5079 
5080 PROCEDURE Assign_Kpi_Tab
5081 (
5082       p_commit              IN              VARCHAR2   := FND_API.G_FALSE
5083     , p_tab_id      IN      NUMBER
5084     , p_kpi_ids     IN      VARCHAR2
5085     , x_return_status   IN OUT NOCOPY   VARCHAR2
5086     , x_msg_count       IN OUT NOCOPY   NUMBER
5087     , x_msg_data        IN OUT NOCOPY   VARCHAR2
5088     , p_time_stamp  IN      VARCHAR2  :=  NULL
5089 )IS
5090     l_kpi_ids       VARCHAR2(32000);
5091         l_kpi_id        VARCHAR2(10);
5092     l_Time_Stamp    VARCHAR2(100);
5093 BEGIN
5094     SAVEPOINT BscPmfUIAssignKpiTab;
5095     FND_MSG_PUB.Initialize;
5096     x_return_status := FND_API.G_RET_STS_SUCCESS;
5097 
5098     IF(p_kpi_ids IS NOT NULL) THEN
5099         l_kpi_ids := p_kpi_ids;
5100         l_Time_Stamp := p_time_stamp;
5101         WHILE (is_more(  p_dim_short_names  =>  l_kpi_ids
5102                     ,p_dim_name         =>  l_kpi_id)
5103            ) LOOP
5104 
5105              BSC_PMF_UI_WRAPPER.Assign_KPI
5106              (
5107                   p_commit          =>    FND_API.G_FALSE
5108                 , p_kpi_id          =>    l_kpi_id
5109                 , p_tab_id          =>    p_tab_id
5110                 , x_return_status   =>    x_return_status
5111                 , x_msg_count       =>    x_msg_count
5112                 , x_msg_data        =>    x_msg_data
5113                 , p_time_stamp      =>    l_Time_Stamp
5114           );
5115           IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5116             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5117           END IF;
5118           -- retrieving the current value of timestamp from database which will
5119           --be assigned to p_time_stamp after the 1st cycle.
5120 
5121           l_Time_Stamp := BSC_BIS_LOCKS_PVT.get_tab_time_stamp(p_tab_id);
5122            END LOOP;
5123     END IF;
5124 
5125   IF (p_commit = FND_API.G_TRUE) THEN
5126     COMMIT;
5127   END IF;
5128 EXCEPTION
5129   WHEN FND_API.G_EXC_ERROR THEN
5130     rollback TO BscPmfUIAssignKpiTab;
5131     x_return_status := FND_API.G_RET_STS_ERROR;
5132     FND_MSG_PUB.Count_And_Get( p_count  =>      x_msg_count
5133                               ,p_data   =>      x_msg_data);
5134 
5135   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5136     rollback TO BscPmfUIAssignKpiTab;
5137     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5138     if( x_msg_data is null) then
5139 
5140       FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
5141                               ,p_data     =>      x_msg_data);
5142     end if;
5143 
5144   WHEN NO_DATA_FOUND THEN
5145     rollback TO BscPmfUIAssignKpiTab;
5146     x_return_status := FND_API.G_RET_STS_ERROR;
5147     FND_MSG_PUB.Count_And_Get( p_count    =>      x_msg_count
5148                               ,p_data     =>      x_msg_data);
5149 
5150   WHEN OTHERS THEN
5151     rollback TO BscPmfUIAssignKpiTab;
5152     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5153     IF (x_msg_data IS NOT NULL) THEN
5154         x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Assign_Kpi_Tab ';
5155     ELSE
5156         x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Assign_Kpi_Tab ';
5157     END IF;
5158 
5159 END Assign_Kpi_Tab;
5160 
5161 FUNCTION get_Tab_Id
5162 (
5163     p_Tab_Name  IN  VARCHAR2
5164 ) RETURN NUMBER IS
5165 
5166   CURSOR c_Tab_Id IS
5167   SELECT Tab_ID
5168   FROM   BSC_TABS_VL
5169   WHERE  Name = p_Tab_Name;
5170 
5171   l_Tab_Id NUMBER := -2;
5172 BEGIN
5173   IF (c_Tab_Id%ISOPEN) THEN
5174       CLOSE c_Tab_Id;
5175   END IF;
5176   OPEN c_Tab_Id;
5177       FETCH c_Tab_Id INTO l_Tab_Id;
5178   CLOSE c_Tab_Id;
5179   RETURN l_Tab_Id;
5180 EXCEPTION
5181     WHEN OTHERS THEN
5182     IF (c_Tab_Id%ISOPEN) THEN
5183         CLOSE c_Tab_Id;
5184     END IF;
5185         RETURN l_Tab_Id;
5186 END get_Tab_Id;
5187 
5188 /*
5189  * Objective Calendar properties. (Added as part of Bug #5584826 fix)
5190  * Internal procedure called from Update_Kpi_Periodicities only.
5191  * The API is called seperately for objective and its shared objectives if exists.
5192  */
5193 
5194 PROCEDURE Update_Obj_Cal_properties (
5195    p_commit              IN             VARCHAR2
5196   ,p_calendar_id         IN             NUMBER
5197   ,l_Bsc_Kpi_Entity_Rec  IN OUT NOCOPY BSC_KPI_PUB.Bsc_Kpi_Entity_Rec
5198   ,l_periodicities_tbl   IN BSC_BIS_LOCKS_PUB.t_numberTable
5199   ,l_current_periods     IN BSC_BIS_LOCKS_PUB.t_numberTable
5200   ,l_Dft_periodicity_id  IN NUMBER
5201   ,l_action_flag         IN NUMBER
5202   ,x_return_status       OUT NOCOPY     VARCHAR2
5203   ,x_msg_count           OUT NOCOPY     NUMBER
5204   ,x_msg_data            OUT NOCOPY     VARCHAR2
5205 ) IS
5206   l_count NUMBER;
5207 
5208   CURSOR c_reorder_periods IS
5209   SELECT rownum-1 newRow, periodicity_id
5210   FROM   bsc_kpi_periodicities
5211   WHERE  indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
5212 
5213 BEGIN
5214   -- Create the new periodicites
5215   FOR l_count IN 1.. l_periodicities_tbl.count LOOP
5216     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Periodicity_Id := l_periodicities_tbl(l_count);
5217     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Current_Period := l_current_periods (l_count);
5218 
5219     IF l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Current_Period > 1900 then
5220       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Num_Years := 10;
5221       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Previous_Years := 5;
5222     ELSE
5223       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Num_Years := 0;
5224       l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Previous_Years := 0 ;
5225     END IF;
5226 
5227     BSC_KPI_PUB.Create_Kpi_Periodicity
5228     ( p_commit              => p_commit
5229     , p_Bsc_Kpi_Entity_Rec  => l_Bsc_Kpi_Entity_Rec
5230     , x_return_status       => x_return_status
5231     , x_msg_count           => x_msg_count
5232     , x_msg_data            => x_msg_data
5233     );
5234     IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5235     RAISE   FND_API.G_EXC_UNEXPECTED_ERROR;
5236     END IF;
5237   END LOOP;
5238 
5239   -- Reorder the sequence.
5240   FOR cd_reorder IN c_reorder_periods LOOP
5241     UPDATE bsc_kpi_periodicities
5242     SET    display_order = cd_reorder.newRow
5243     WHERE  periodicity_id = cd_reorder.periodicity_id
5244     AND    indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
5245   END LOOP;
5246 
5247   UPDATE bsc_kpis_b
5248   SET    calendar_id      = p_calendar_id
5249         ,periodicity_id   = l_Dft_periodicity_id
5250         ,last_update_date = SYSDATE
5251   WHERE  indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
5252 
5253   UPDATE bsc_kpi_periodicities
5254   SET    user_level0 = C_DISABLE_FLAG
5255         ,user_level1 = C_DISABLE_FLAG
5256   WHERE  indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5257   AND    periodicity_id = l_Dft_periodicity_id;
5258 
5259   IF (l_action_flag IS NOT NULL) THEN
5260     --Reset the db_transform
5261     --ppandey 'DB_TRANSFORM' is Objective Type 0->Precalculated, 1->normal, 2->Target at differet Benchmarks.
5262     IF (l_action_flag = BSC_DESIGNER_PVT.G_ActionFlag.Prototype) THEN
5263       UPDATE bsc_kpi_properties
5264       SET    property_value = 1
5265       WHERE  indicator      = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5266       AND    property_code  = 'DB_TRANSFORM'
5267       AND    property_value = 2;
5268 
5269       UPDATE bsc_kpi_periodicities
5270       SET    target_level = 1
5271       WHERE  indicator    = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id;
5272       --AND    target_level <> 1;
5273     END IF;
5274 
5275     BSC_DESIGNER_PVT.ActionFlag_Change
5276     ( x_indicator => l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5277     , x_newflag   => l_action_flag
5278     );
5279   END IF;
5280 END Update_Obj_Cal_properties;
5281 
5282 /************************************************************************************
5283 --      API name        : Update_Periodicity_Props
5284 --      Type            : Private
5285 ************************************************************************************/
5286 PROCEDURE Update_Periodicity_Props(
5287   p_commit              IN   VARCHAR2 := FND_API.G_FALSE
5288  ,p_Indicator           IN   NUMBER
5289  ,p_calendar_id         IN   NUMBER
5290  ,p_Periods_In_Graph    IN   FND_TABLE_OF_NUMBER := NULL
5291  ,p_Periodicity_Id_Tbl  IN   FND_TABLE_OF_NUMBER := NULL
5292  ,p_Number_Of_Years     IN   NUMBER := 10
5293  ,p_Previous_Years      IN   NUMBER := 5
5294  ,p_cascade_shared      IN   BOOLEAN := FALSE
5295  ,x_return_status       OUT  NOCOPY   VARCHAR2
5296  ,x_msg_count           OUT  NOCOPY   NUMBER
5297  ,x_msg_data            OUT  NOCOPY   VARCHAR2
5298 ) IS
5299  l_Max_Periods NUMBER := 0;
5300  l_Bsc_Kpi_Entity_Rec  BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
5301 
5302  CURSOR c_share_kpis IS
5303  SELECT
5304    indicator
5305  FROM
5306    bsc_kpis_b
5307  WHERE
5308    source_indicator = p_Indicator
5309    AND PROTOTYPE_FLAG <> 2;
5310 
5311  CURSOR c_Kpi_Periodicities IS
5312  SELECT
5313    kp. periodicity_id,
5314    p.periodicity_type,
5315    DECODE(p.periodicity_type, 1 , kp.num_of_years, p.num_of_periods) max_periods
5316  FROM
5317    bsc_kpi_periodicities kp,
5318    bsc_sys_periodicities_vl p
5319  WHERE
5320    kp.indicator = p_Indicator
5321    AND kp.periodicity_id = p.periodicity_id
5322    AND p.calendar_id = p_calendar_id;
5323 BEGIN
5324 
5325   SAVEPOINT  UpdatePeriodicityPropsPUB;
5326   x_return_status := FND_API.G_RET_STS_SUCCESS;
5327   FND_MSG_PUB.Initialize;
5328 
5329   FOR cd IN c_Kpi_Periodicities LOOP
5330     IF p_Periodicity_Id_Tbl IS NOT NULL AND p_Periods_In_Graph IS NOT NULL THEN
5331       FOR i IN 1..p_Periodicity_Id_Tbl.COUNT LOOP
5332         IF p_Periodicity_Id_Tbl(i) = cd.periodicity_id THEN
5333 
5334          -- If both are same ignore otherwise viewport_flag = 1 and viewport_defaultsize = p_Periods_In_Graph(i)
5335           IF cd.max_periods > p_Periods_In_Graph(i) THEN
5336              l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Viewport_Flag := 1;
5337              l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Viewport_Default_Size :=  p_Periods_In_Graph(i);
5338           ELSE
5339              l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Viewport_Flag := 0;
5340              l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Viewport_Default_Size :=  0;
5341           END IF;
5342 
5343           IF cd.periodicity_type = 1 THEN
5344             l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Num_Years := p_Number_Of_Years;
5345             l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Previous_Years := p_Previous_Years;
5346           ELSE
5347             l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Num_Years := 0;
5348             l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Previous_Years := 0;
5349           END IF;
5350 
5351           l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_Indicator;
5352           l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Periodicity_Id := cd.periodicity_id;
5353           BSC_KPI_PUB.Update_Kpi_Periodicity (
5354             p_commit             => FND_API.G_FALSE
5355            ,p_Bsc_Kpi_Entity_Rec =>  l_Bsc_Kpi_Entity_Rec
5356            ,x_return_status      =>  x_return_status
5357            ,x_msg_count          =>  x_msg_count
5358            ,x_msg_data           =>  x_msg_data
5359           ) ;
5360           IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5361             RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5362           END IF;
5363 
5364           IF p_cascade_shared THEN
5365             FOR cd IN c_share_kpis LOOP
5366               l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := cd.Indicator;
5367               BSC_KPI_PUB.Update_Kpi_Periodicity (
5368                 p_commit             => FND_API.G_FALSE
5369                ,p_Bsc_Kpi_Entity_Rec =>  l_Bsc_Kpi_Entity_Rec
5370                ,x_return_status      =>  x_return_status
5371                ,x_msg_count          =>  x_msg_count
5372                ,x_msg_data           =>  x_msg_data
5373               );
5374 	      IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5375 	        RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5376 	      END IF;
5377 	    END LOOP;
5378           END IF;
5379         END IF;
5380       END LOOP;
5381     END IF;
5382   END LOOP;
5383 
5384   IF p_Commit = FND_API.G_TRUE THEN
5385     COMMIT;
5386   END IF;
5387 
5388 EXCEPTION
5389   WHEN FND_API.G_EXC_ERROR THEN
5390     ROLLBACK TO UpdatePeriodicityPropsPUB;
5391     IF (x_msg_data IS NULL) THEN
5392         FND_MSG_PUB.Count_And_Get
5393         (      p_encoded   =>  FND_API.G_FALSE
5394            ,   p_count     =>  x_msg_count
5395            ,   p_data      =>  x_msg_data
5396         );
5397     END IF;
5398     x_return_status :=  FND_API.G_RET_STS_ERROR;
5399   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5400     ROLLBACK TO UpdatePeriodicityPropsPUB;
5401     IF (x_msg_data IS NULL) THEN
5402         FND_MSG_PUB.Count_And_Get
5403         (      p_encoded   =>  FND_API.G_FALSE
5404            ,   p_count     =>  x_msg_count
5405            ,   p_data      =>  x_msg_data
5406         );
5407     END IF;
5408     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5409   WHEN NO_DATA_FOUND THEN
5410     ROLLBACK TO UpdatePeriodicityPropsPUB;
5411     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5412     IF (x_msg_data IS NOT NULL) THEN
5413         x_msg_data      :=  x_msg_data||' ->BSC_PMF_UI_WRAPPER.Update_Periodicity_Props ';
5414     ELSE
5415         x_msg_data      :=  SQLERRM||'BSC_PMF_UI_WRAPPER.Update_Periodicity_Props ';
5416     END IF;
5417   WHEN OTHERS THEN
5418     ROLLBACK TO UpdatePeriodicityPropsPUB;
5419     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5420     IF (x_msg_data IS NOT NULL) THEN
5421         x_msg_data      :=  x_msg_data||' ->BSC_PMF_UI_WRAPPER.Update_Periodicity_Props ';
5422     ELSE
5423         x_msg_data      :=  SQLERRM||'BSC_PMF_UI_WRAPPER.Update_Periodicity_Props ';
5424     END IF;
5425 END Update_Periodicity_Props;
5426 
5427 
5428 /********************************************************************************
5429  Name :- Update_Kpi_Periodicities
5430  Description :- This procedure will assign the Periodicities to one KPI
5431         It will replace the previous peridicities with the new set of
5432         periodicities.
5433 
5434 Input :-  p_kpi_id           Indicator ID (Master)
5435           p_calendar_id      Calendar Id. It is mandatory
5436           p_periodicity_ids  Set of periodicities separates by ","
5437                              when it is null it will assign allo the Calendar
5438                              periodicities
5439           p_Dft_periodicity_id   Default kpi periodicity Id
5440                                  It is null it will set the first periodicity
5441                                  id in the p_periodicity_ids LIST
5442 
5443 
5444 Creator : - William Cano APR 16 / 2003
5445 /********************************************************************************/
5446 procedure Update_Kpi_Periodicities(
5447   p_commit              IN             VARCHAR2 -- := FND_API.G_FALSE
5448  ,p_kpi_id              IN             NUMBER
5449  ,p_calendar_id         IN             NUMBER
5450  ,p_periodicity_ids     IN             VARCHAR2
5451  ,p_Dft_periodicity_id  IN             NUMBER
5452  ,p_Periods_In_Graph    IN             FND_TABLE_OF_NUMBER := NULL
5453  ,p_Periodicity_Id_Tbl  IN             FND_TABLE_OF_NUMBER := NULL
5454  ,p_Number_Of_Years     IN             NUMBER := 10
5455  ,p_Previous_Years      IN             NUMBER := 5
5456  ,x_return_status       OUT NOCOPY     VARCHAR2
5457  ,x_msg_count           OUT NOCOPY     NUMBER
5458  ,x_msg_data            OUT NOCOPY     VARCHAR2
5459 ) IS
5460   l_periodicity_ids     VARCHAR2(300);
5461   l_count               NUMBER;
5462   l_per_count           NUMBER;
5463   l_cur_cal_id          NUMBER;
5464   l_cur_periodicity_id  NUMBER;
5465   l_Bsc_Kpi_Entity_Rec  BSC_KPI_PUB.Bsc_Kpi_Entity_Rec;
5466   l_cursor              BSC_BIS_LOCKS_PUB.t_cursor;
5467   l_sql                 varchar2(500);
5468   l_Dft_periodicity_id  NUMBER ;
5469   l_periodicity_id      NUMBER;
5470   l_current_period      NUMBER;
5471   l_periodicities_tbl   BSC_BIS_LOCKS_PUB.t_numberTable;
5472   l_current_periods     BSC_BIS_LOCKS_PUB.t_numberTable;
5473   l_Indic_Type          BSC_KPIS_B.INDICATOR_TYPE%TYPE;
5474   l_Indic_Config_Type   BSC_KPIS_B.CONFIG_TYPE%TYPE;
5475   l_Periodicity_Type    BSC_SYS_PERIODICITIES.PERIODICITY_ID%TYPE;
5476   l_Is_Invalid_Per_Sim  BOOLEAN := FALSE;
5477   l_Period_Name         BSC_SYS_PERIODICITIES_TL.NAME%TYPE;
5478   l_Invalid_Period_Name BSC_SYS_PERIODICITIES_TL.NAME%TYPE;
5479   l_Count_Tables        NUMBER;
5480   l_action_flag         NUMBER;
5481   l_old_periodicities   NUMBER;
5482   l_new_periodicities   NUMBER;
5483   l_cur_prototype_flag  NUMBER;
5484   l_is_prototype        BOOLEAN;
5485   l_Old_Number_Years    bsc_kpi_periodicities.num_of_years%TYPE := 0;
5486   l_Old_Previous_Years  bsc_kpi_periodicities.previous_years%TYPE := 0;
5487   l_New_Number_Years    bsc_kpi_periodicities.num_of_years%TYPE := 0;
5488   l_New_Previous_Years  bsc_kpi_periodicities.previous_years%TYPE := 0;
5489 
5490 
5491   CURSOR c_share_kpis IS
5492    SELECT indicator
5493    FROM BSC_KPIS_B
5494    WHERE source_indicator = p_kpi_id
5495      AND PROTOTYPE_FLAG <> 2;
5496 
5497   CURSOR c_Yearly_Periodicity IS
5498   SELECT
5499     NVL(kp.num_of_years,0),
5500     NVL(kp.previous_years,0)
5501   FROM
5502     bsc_kpi_periodicities kp,
5503     bsc_sys_periodicities_vl p
5504   WHERE
5505     kp.indicator(+) = p_kpi_id
5506     AND kp.periodicity_id(+) = p.periodicity_id
5507     AND p.calendar_id = p_calendar_id;
5508 
5509 BEGIN
5510 
5511   SAVEPOINT BSCUpdKpiPeriodicities;
5512   FND_MSG_PUB.Initialize;
5513 
5514   x_return_status := FND_API.G_RET_STS_SUCCESS;
5515   l_periodicity_ids := TRIM(p_periodicity_ids);
5516   l_Dft_periodicity_id := NULL;
5517 
5518   /* The following validation apply for this API
5519     1)Simulation tree cannot have more than one periodicity
5520     2)Simulation tree cannot have periodicities of type 1,9,10
5521  */
5522 
5523   IF(l_periodicity_ids IS NULL )THEN
5524     RETURN;
5525     -- It should give a message, but needs to be verified that it has no impact on Report Designer.
5526     /*FND_MESSAGE.SET_NAME('BSC','BSC_D_KPI_AT_LEAST_1_PERIOD');
5527       FND_MSG_PUB.ADD;
5528     RAISE FND_API.G_EXC_ERROR;*/
5529   END IF;
5530 
5531   OPEN c_Yearly_Periodicity;
5532   FETCH c_Yearly_Periodicity INTO l_Old_Number_Years,l_Old_Previous_Years;
5533   CLOSE c_Yearly_Periodicity;
5534 
5535   SELECT indicator_type, config_type, calendar_id, periodicity_id, prototype_flag
5536   INTO   l_Indic_Type,l_Indic_Config_Type, l_cur_cal_id, l_cur_periodicity_id, l_cur_prototype_flag
5537   FROM   bsc_kpis_b
5538   WHERE  indicator = p_kpi_id;
5539 
5540   --now we will support more than 1 periodicity for Simulation tree.
5541   -- so relaxing this condition
5542   /*IF(l_Indic_Type = 1 AND l_Indic_Config_Type = 7) THEN
5543 
5544     l_sql :='
5545       SELECT COUNT(1)
5546       FROM   bsc_sys_periodicities_vl P
5547            , bsc_sys_calendars_b C
5548       WHERE C.calendar_id =:1
5549       AND   P.calendar_id = C.calendar_id';
5550     l_sql := l_sql || '  AND periodicity_id IN (' || l_periodicity_ids || ')';
5551 
5552     EXECUTE IMMEDIATE l_sql INTO l_count USING p_calendar_id;
5553 
5554     IF(l_count > 1) THEN
5555       FND_MESSAGE.SET_NAME('BSC','BSC_D_ONE_PERIOD_IN_SIM_TREE');
5556       FND_MSG_PUB.ADD;
5557       RAISE FND_API.G_EXC_ERROR;
5558     END IF;
5559   END IF;*/
5560 
5561   IF (l_cur_cal_id = p_calendar_id) THEN -- Calendar is not changed.
5562     -- Find new Periods added and make the list which will be passed to Update_Obj_Cal_properties
5563 
5564     -- If yearly get the fiscal year, else put it to 1, it will be updated by loader as per the data loaded.
5565     l_sql :='
5566     SELECT P.periodicity_id
5567           ,DECODE (P.yearly_flag, 1, C.fiscal_year, 1 )
5568           ,P.periodicity_type
5569           ,P.name
5570     FROM   bsc_sys_periodicities_vl P
5571          , bsc_sys_calendars_b C
5572     WHERE C.calendar_id =:1
5573     AND   P.calendar_id = C.calendar_id
5574     AND   periodicity_id NOT IN (
5575           SELECT periodicity_id FROM bsc_kpi_periodicities where indicator = :2
5576     )';
5577     l_sql := l_sql || '  AND periodicity_id IN (' || l_periodicity_ids || ')';
5578 
5579     l_count := 0;
5580     OPEN l_cursor FOR l_sql USING p_calendar_id, p_kpi_id;
5581         LOOP
5582           FETCH   l_cursor INTO l_periodicity_id, l_current_period ,l_periodicity_type,l_Period_Name;
5583           EXIT WHEN l_cursor%NOTFOUND;
5584           l_count :=  l_count + 1;
5585           l_periodicities_tbl(l_count) := l_periodicity_id;
5586           l_current_periods (l_count)  := l_current_period;
5587         END LOOP;
5588     CLOSE l_cursor;
5589 
5590     IF (l_cur_prototype_flag = BSC_DESIGNER_PVT.G_ActionFlag.Prototype) THEN
5591       l_is_prototype := TRUE;
5592     ELSE
5593       l_is_prototype := FALSE;
5594     END IF;
5595 
5596     IF (NOT l_is_prototype AND l_count > 0) THEN
5597       l_action_flag := BSC_DESIGNER_PVT.G_ActionFlag.Prototype;
5598       l_is_prototype:= TRUE;
5599     ELSE
5600       SELECT COUNT(1)
5601       INTO   l_old_periodicities
5602       FROM   bsc_kpi_periodicities
5603       WHERE  indicator = p_kpi_id;
5604     END IF;
5605 
5606     -- Delete the Periods which are Removed (from master and shared objectives).
5607     l_sql :='
5608     DELETE bsc_kpi_periodicities
5609     WHERE  indicator IN
5610            (SELECT indicator
5611             FROM   bsc_kpis_b
5612             WHERE  indicator = :1 OR source_indicator = :2)';
5613     l_sql := l_sql || '  AND periodicity_id NOT IN (' || l_periodicity_ids || ')';
5614 
5615     EXECUTE IMMEDIATE l_sql USING p_kpi_id, p_kpi_id;
5616 
5617     IF (l_count = 0) THEN
5618       SELECT COUNT(1)
5619       INTO   l_new_periodicities
5620       FROM   bsc_kpi_periodicities
5621       WHERE  indicator = p_kpi_id;
5622       IF (l_old_periodicities <> l_new_periodicities) THEN
5623         l_action_flag := BSC_DESIGNER_PVT.G_ActionFlag.Prototype;
5624       END IF;
5625     END IF;
5626 
5627     -- p_Dft_periodicity_id is null in case of Report Designer Create flow Bug #5629309
5628     IF (p_Dft_periodicity_id IS NOT NULL) THEN
5629       l_sql :='
5630         SELECT COUNT(1)
5631         FROM   bsc_sys_periodicities_vl P
5632              , bsc_sys_calendars_b C
5633         WHERE C.calendar_id =:1
5634         AND   P.calendar_id = C.calendar_id
5635         AND   periodicity_id = :2';
5636         l_sql := l_sql || '  AND periodicity_id IN (' || l_periodicity_ids || ')';
5637 
5638       EXECUTE IMMEDIATE l_sql INTO l_per_count USING p_calendar_id, p_Dft_periodicity_id;
5639 
5640       IF (l_per_count > 0 ) THEN
5641         l_Dft_periodicity_id := p_Dft_periodicity_id;
5642       END IF;
5643     END IF;
5644 
5645     IF (l_Dft_periodicity_id IS NULL) THEN
5646       IF (l_count > 0) THEN
5647         l_Dft_periodicity_id:= l_periodicities_tbl(1);
5648       ELSE
5649         SELECT periodicity_id
5650         INTO   l_Dft_periodicity_id
5651         FROM   bsc_kpi_periodicities
5652         WHERE  indicator = p_kpi_id
5653         AND    rownum = 1;
5654       END IF;
5655     END IF;
5656 
5657     IF (l_Dft_periodicity_id <> l_cur_periodicity_id) THEN
5658       IF (NOT l_is_prototype) THEN
5659         l_action_flag := BSC_DESIGNER_PVT.G_ActionFlag.GAA_Color;
5660       END IF;
5661     -- Reset the default period to enable, it will be taken care in Update_Obj_Cal_properties
5662       UPDATE bsc_kpi_periodicities
5663       SET    user_level0=C_ENABLE_FLAG, user_level1=C_ENABLE_FLAG
5664       WHERE  user_level0=C_DISABLE_FLAG
5665       AND    user_level1=C_DISABLE_FLAG
5666       AND    periodicity_id = l_cur_periodicity_id
5667       AND    indicator IN
5668              (SELECT indicator
5669               FROM   bsc_kpis_b
5670               WHERE  indicator = p_kpi_id
5671               OR     source_indicator=p_kpi_id);
5672     END IF;
5673 
5674   ELSE -- calendar is changed for the objective
5675     --We don't need to check if the current prototype_flag is not equal to
5676     --prototype_flag,because changing the calendar always result in
5677     --structural changes.so we will pass the prototype_flag
5678     /*IF (l_cur_prototype_flag <> BSC_DESIGNER_PVT.G_ActionFlag.Prototype) THEN
5679       l_action_flag := BSC_DESIGNER_PVT.G_ActionFlag.Prototype;
5680     END IF;*/
5681     l_action_flag := BSC_DESIGNER_PVT.G_ActionFlag.Prototype;
5682 
5683     l_sql :='
5684     SELECT P.periodicity_id
5685           ,DECODE (P.yearly_flag, 1, C.fiscal_year, 1 )
5686           ,P.periodicity_type
5687           ,P.name
5688     FROM   bsc_sys_periodicities_vl P
5689          , bsc_sys_calendars_b C
5690     WHERE C.calendar_id =:1
5691     AND   P.calendar_id = C.calendar_id';
5692     l_sql := l_sql || '  AND periodicity_id IN (' || l_periodicity_ids || ')';
5693     l_count := 0;
5694 
5695     OPEN l_cursor FOR l_sql USING p_calendar_id;
5696     LOOP
5697       FETCH   l_cursor INTO l_periodicity_id, l_current_period ,l_periodicity_type,l_Period_Name;
5698       EXIT WHEN l_cursor%NOTFOUND;
5699       l_count :=  l_count + 1;
5700       l_periodicities_tbl(l_count) := l_periodicity_id;
5701       l_current_periods (l_count)  := l_current_period;
5702       IF p_Dft_periodicity_id = l_periodicity_id THEN
5703        l_Dft_periodicity_id := l_periodicity_id ;
5704       END IF;
5705       -- Following code doesn't seems to be used anywhere
5706       IF(l_periodicity_type = 1 OR l_periodicity_type = 9 OR l_periodicity_type = 10 ) THEN
5707         l_Is_Invalid_Per_Sim := TRUE;
5708         l_Invalid_Period_Name := l_Period_Name;
5709       END IF;
5710     END LOOP;
5711     CLOSE l_cursor;
5712 
5713     -- Remove all the Master/Shared Objective periodicities.
5714     DELETE bsc_kpi_periodicities p
5715     WHERE p.indicator in
5716        (SELECT k.indicator
5717         FROM   bsc_kpis_b k
5718         WHERE  k.indicator = p_kpi_id
5719         OR k.source_indicator = p_kpi_id);
5720 
5721     IF (l_Dft_periodicity_id IS NULL) THEN
5722       l_Dft_periodicity_id:= l_periodicities_tbl(1);
5723     END IF;
5724   END IF;
5725 
5726   -- If any chage then only do the operations
5727   -- l_action_flag null if no change, Color change on Default, prototype on calendar change or periodicity add/remove.
5728   IF (l_action_flag IS NOT NULL) THEN
5729     BSC_KPI_PUB.Initialize_Kpi_Entity_Rec
5730     ( p_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
5731     , x_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
5732     , x_return_status      => x_return_status
5733     , x_msg_count          => x_msg_count
5734     , x_msg_data           => x_msg_data
5735     );
5736 
5737     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
5738       RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5739     END IF;
5740 
5741     -- All the periodicities will be enabled.
5742     -- Only default will be disabled, which is taken care in Update_Obj_Cal_properties.
5743     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_User_Level0 := C_ENABLE_FLAG;
5744     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_User_Level1 := C_ENABLE_FLAG;
5745 
5746     l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id := p_kpi_id;
5747     l_periodicity_ids := TRIM(p_periodicity_ids);
5748 
5749     -- lock the KPI
5750     BSC_BIS_LOCKS_PUB.LOCK_KPI
5751     ( p_kpi_Id               => l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5752     , p_time_stamp           => NULL
5753     , p_full_lock_flag       => FND_API.G_FALSE
5754     , x_return_status        => x_return_status
5755     , x_msg_count            => x_msg_count
5756     , x_msg_data             => x_msg_data
5757     );
5758 
5759     -- Create the new periodicites for Master.
5760     Update_Obj_Cal_properties (
5761       p_commit             => p_commit
5762      ,p_calendar_id        => p_calendar_id
5763      ,l_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
5764      ,l_periodicities_tbl  => l_periodicities_tbl
5765      ,l_current_periods    => l_current_periods
5766      ,l_Dft_periodicity_id => l_Dft_periodicity_id
5767      ,l_action_flag        => l_action_flag
5768      ,x_return_status      => x_return_status
5769      ,x_msg_count          => x_msg_count
5770      ,x_msg_data           => x_msg_data
5771     );
5772 
5773     FOR l_count IN 1.. l_periodicities_tbl.count LOOP
5774       l_periodicity_id := l_periodicities_tbl(l_count);
5775 
5776       SELECT COUNT(1)
5777       INTO   l_Count_Tables
5778       FROM   bsc_kpi_data_tables
5779       WHERE  indicator = l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5780       AND    periodicity_id = l_periodicity_id;
5781       IF(l_Count_Tables = 0) THEN
5782         INSERT INTO bsc_kpi_data_tables
5783                ( indicator
5784                , periodicity_id
5785                , dim_set_id
5786                , level_comb)
5787         VALUES
5788                ( l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id
5789                , l_periodicity_id
5790                , 0
5791                , '?'
5792         );
5793 
5794       END IF;
5795     END LOOP;
5796 
5797   -------------------------------------
5798   --  share kpis
5799   -------------------------------------
5800     IF (c_share_kpis%ISOPEN) THEN
5801       CLOSE c_share_kpis;
5802     END IF;
5803 
5804     OPEN c_share_kpis;
5805     LOOP
5806       FETCH  c_share_kpis INTO l_Bsc_Kpi_Entity_Rec.Bsc_Kpi_Id ;
5807       EXIT WHEN c_share_kpis%NOTFOUND;
5808 
5809      Update_Obj_Cal_properties (
5810        p_commit             => p_commit
5811       ,p_calendar_id        => p_calendar_id
5812       ,l_Bsc_Kpi_Entity_Rec => l_Bsc_Kpi_Entity_Rec
5813       ,l_periodicities_tbl  => l_periodicities_tbl
5814       ,l_current_periods    => l_current_periods
5815       ,l_Dft_periodicity_id => l_Dft_periodicity_id
5816       ,l_action_flag        => l_action_flag
5817       ,x_return_status      => x_return_status
5818       ,x_msg_count          => x_msg_count
5819       ,x_msg_data           => x_msg_data
5820      );
5821 
5822     END LOOP;
5823     CLOSE c_share_kpis;
5824 
5825   END IF;
5826 
5827   Update_Periodicity_Props (
5828     p_commit             => p_commit
5829    ,p_Indicator          => p_kpi_id
5830    ,p_calendar_id        => p_calendar_id
5831    ,p_Periods_In_Graph   => p_Periods_In_Graph
5832    ,p_Periodicity_Id_Tbl => p_Periodicity_Id_Tbl
5833    ,p_Number_Of_Years    => p_Number_Of_Years
5834    ,p_Previous_Years     => p_Previous_Years
5835    ,p_cascade_shared     => TRUE
5836    ,x_return_status      => x_return_status
5837    ,x_msg_count          => x_msg_count
5838    ,x_msg_data           => x_msg_data
5839   );
5840   IF (x_return_status  <> FND_API.G_RET_STS_SUCCESS) THEN
5841     RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5842   END IF;
5843 
5844   OPEN c_Yearly_Periodicity;
5845   FETCH c_Yearly_Periodicity INTO l_New_Number_Years,l_New_Previous_Years;
5846   CLOSE c_Yearly_Periodicity;
5847 
5848   IF l_Old_Number_Years <> l_New_Number_Years OR
5849     l_Old_Previous_Years <> l_New_Previous_Years THEN
5850     BSC_DESIGNER_PVT.ActionFlag_Change(p_kpi_id , BSC_DESIGNER_PVT.G_ActionFlag.GAA_Update);
5851     FOR cd IN c_share_kpis LOOP
5852       BSC_DESIGNER_PVT.ActionFlag_Change(cd.indicator , BSC_DESIGNER_PVT.G_ActionFlag.GAA_Update);
5853     END LOOP;
5854   END IF;
5855 
5856   IF p_commit = FND_API.G_TRUE then
5857     commit;
5858   END IF;
5859 
5860 EXCEPTION
5861     WHEN FND_API.G_EXC_ERROR THEN
5862         ROLLBACK TO BSCUpdKpiPeriodicities;
5863         IF (c_share_kpis%ISOPEN) THEN
5864             CLOSE c_share_kpis;
5865         END IF;
5866         IF (x_msg_data IS NULL) THEN
5867             FND_MSG_PUB.Count_And_Get
5868             (      p_encoded   =>  FND_API.G_FALSE
5869                ,   p_count     =>  x_msg_count
5870                ,   p_data      =>  x_msg_data
5871             );
5872         END IF;
5873         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
5874         x_return_status :=  FND_API.G_RET_STS_ERROR;
5875     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5876         ROLLBACK TO BSCUpdKpiPeriodicities;
5877         IF (c_share_kpis%ISOPEN) THEN
5878             CLOSE c_share_kpis;
5879         END IF;
5880         IF (x_msg_data IS NULL) THEN
5881             FND_MSG_PUB.Count_And_Get
5882             (      p_encoded   =>  FND_API.G_FALSE
5883                ,   p_count     =>  x_msg_count
5884                ,   p_data      =>  x_msg_data
5885             );
5886         END IF;
5887         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5888         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
5889     WHEN OTHERS THEN
5890         ROLLBACK TO BSCUpdKpiPeriodicities;
5891         IF (c_share_kpis%ISOPEN) THEN
5892             CLOSE c_share_kpis;
5893         END IF;
5894         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5895         IF (x_msg_data IS NOT NULL) THEN
5896             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Update_Kpi_Periodicities ';
5897         ELSE
5898             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Update_Kpi_Periodicities ';
5899         END IF;
5900         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
5901 
5902 end Update_Kpi_Periodicities;
5903 
5904 /*
5905  * This procedure is used to check whether any KPI in a scorecard is attached to
5906  * any KPI region(KPI portlet).
5907  */
5908  procedure Check_Tab(
5909   p_tab_id              IN      number
5910  ,x_return_status       OUT NOCOPY     varchar2
5911  ,x_msg_count           OUT NOCOPY     number
5912  ,x_msg_data            OUT NOCOPY     varchar2
5913  ) is
5914 
5915    l_ret_status            varchar2(10);
5916    l_msg_data              varchar2(100);
5917    l_parent_obj_table  BIS_RSG_PUB_API_PKG.t_BIA_RSG_Obj_Table;
5918    l_index integer;
5919    l_dep_obj_name          varchar2(240);
5920    l_dep_obj_message       varchar2(1000);
5921    l_dep_obj_list          varchar2(2000);
5922    l_scr_name              BSC_TABS_VL.NAME%TYPE;
5923    l_scr_short_name        BSC_TABS_VL.SHORT_NAME%TYPE;
5924    l_objective_name        BSC_KPIS_VL.NAME%TYPE;
5925    l_Is_Ag_Type_Scorecard  VARCHAR2(1);
5926    l_exist_dependency      VARCHAR2(10);
5927 
5928     CURSOR c_scr IS
5929     SELECT name, short_name
5930     FROM   bsc_tabs_vl
5931     WHERE tab_id = p_tab_id;
5932 
5933     CURSOR c_Kpi_function IS
5934     SELECT c.function_name,d.name
5935     FROM bsc_oaf_analysys_opt_comb_v a,bsc_tab_indicators b,bis_indicators c,bsc_kpis_vl d
5936     WHERE a.indicator = b.indicator
5937     AND a.dataset_id = c.dataset_id
5938     AND a.indicator = d.indicator
5939     AND b.tab_id = p_tab_id;
5940 
5941    BEGIN
5942     FND_MSG_PUB.Initialize;
5943     x_return_status := FND_API.G_RET_STS_SUCCESS;
5944     l_dep_obj_list  := '';
5945     x_msg_data := '';
5946     l_exist_dependency := FND_API.G_FALSE;
5947 
5948     IF (c_scr%ISOPEN) THEN
5949       CLOSE c_scr;
5950     END IF;
5951 
5952     OPEN c_scr;
5953     FETCH c_scr INTO l_scr_name,l_scr_short_name;
5954     CLOSE c_scr;
5955 
5956     BSC_PMF_UI_WRAPPER.Check_Tabviews(
5957           p_tab_id             => p_tab_id
5958         , p_list_dependency    => FND_API.G_TRUE
5959         , x_exist_dependency   => l_exist_dependency
5960         , x_dep_obj_list       => l_dep_obj_list
5961         , x_return_status      => l_ret_status
5962         , x_msg_count          => x_msg_count
5963         , x_msg_data           => l_msg_data);
5964 
5965     IF ((l_ret_status IS NOT NULL) AND (l_ret_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
5966         FND_MSG_PUB.Initialize;
5967         FND_MESSAGE.SET_NAME('BIS',l_msg_data);
5968         FND_MSG_PUB.ADD;
5969         RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
5970     END IF;
5971 
5972     IF (l_exist_dependency = FND_API.G_TRUE) THEN
5973         FND_MESSAGE.SET_NAME('BSC','BSC_SCR_DELETE_VIEW_ERR');
5974         FND_MESSAGE.SET_TOKEN('SCR_NAME', l_scr_name);
5975         FND_MESSAGE.SET_TOKEN('DEP_OBJ_LIST',l_dep_obj_list);
5976         FND_MSG_PUB.ADD;
5977         FND_MSG_PUB.Count_And_Get(
5978          p_encoded   =>  FND_API.G_FALSE
5979        , p_count     =>  x_msg_count
5980        , p_data      =>  x_msg_data);
5981        return;
5982     END IF;
5983 
5984     IF (BSC_BIS_CUSTOM_KPI_UTIL_PUB.is_kpi_endtoend_scorecard(l_scr_short_name) = 'F') THEN
5985        RETURN;
5986     END IF;
5987 
5988     l_Is_Ag_Type_Scorecard := BSC_BIS_KPI_CRUD_PUB.is_Scorecard_From_Reports(l_scr_short_name);
5989 
5990     IF (l_Is_Ag_Type_Scorecard = FND_API.G_TRUE) THEN
5991         -- The scorecard, Report, Objective and Objective Group have the same name in
5992         -- an AG Report scenario.
5993         FND_MESSAGE.SET_NAME('BSC','BSC_AG_SCORECARD_DELETE');
5994         FND_MESSAGE.SET_TOKEN('SNAME', l_scr_name);
5995         FND_MESSAGE.SET_TOKEN('RNAME', l_scr_name);
5996         FND_MSG_PUB.ADD;
5997     ELSE
5998         FOR cd_kpi_function IN c_Kpi_function LOOP
5999             -- call the procedure to get the parent objects for each KPI
6000             l_dep_obj_name  := cd_kpi_function.function_name;
6001             l_objective_name := cd_kpi_function.name;
6002             l_parent_obj_table := BIS_RSG_PUB_API_PKG.GetParentObjects(l_dep_obj_name,'REPORT','PORTLET',l_ret_status,l_msg_data);
6003             IF ((l_ret_status IS NOT NULL) AND (l_ret_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
6004                 FND_MSG_PUB.Initialize;
6005                 FND_MESSAGE.SET_NAME('BIS',l_msg_data);
6006                 FND_MSG_PUB.ADD;
6007                 RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
6008             ELSE
6009                 IF (l_parent_obj_table.COUNT > 0) THEN
6010                   l_index := l_parent_obj_table.first;
6011                   LOOP
6012                     FND_MESSAGE.SET_NAME('BSC','BSC_SCR_DEP_OBJ');
6013                     FND_MESSAGE.SET_TOKEN('OBJECTIVE',l_objective_name);
6014                     FND_MESSAGE.SET_TOKEN('KPI_REGION',l_parent_obj_table(l_index).user_object_name);
6015                     l_dep_obj_message := FND_MESSAGE.GET;
6016                     IF l_index = l_parent_obj_table.first THEN
6017                       l_dep_obj_list :=  '<ul><li>'|| l_dep_obj_message ||'</li>';
6018                     ELSE
6019                       l_dep_obj_list := '<li>'|| l_dep_obj_message ||'</li>';
6020                     END IF;
6021                     EXIT WHEN l_index = l_parent_obj_table.last;
6022                     l_index := l_parent_obj_table.next(l_index);
6023                   END LOOP;
6024                   l_dep_obj_list := l_dep_obj_list || '</ul>';
6025                   FND_MESSAGE.SET_NAME('BSC','BSC_SCR_DELETE');
6026                   FND_MESSAGE.SET_TOKEN('SCR_NAME', l_scr_name);
6027                   FND_MESSAGE.SET_TOKEN('DEP_OBJ_LIST',l_dep_obj_list);
6028                   FND_MSG_PUB.ADD;
6029                 END IF;
6030             END IF;
6031         END LOOP;
6032     END IF;
6033 
6034     FND_MSG_PUB.Count_And_Get
6035     (      p_encoded   =>  FND_API.G_FALSE
6036        ,   p_count     =>  x_msg_count
6037        ,   p_data      =>  x_msg_data
6038     );
6039   EXCEPTION
6040     WHEN FND_API.G_EXC_ERROR THEN
6041         IF (c_scr%ISOPEN) THEN
6042           CLOSE c_scr;
6043         END IF;
6044         IF (x_msg_data IS NULL) THEN
6045             FND_MSG_PUB.Count_And_Get
6046             (      p_encoded   =>  FND_API.G_FALSE
6047                ,   p_count     =>  x_msg_count
6048                ,   p_data      =>  x_msg_data
6049             );
6050         END IF;
6051         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_ERROR '||x_msg_data);
6052         x_return_status :=  FND_API.G_RET_STS_ERROR;
6053     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6054         IF (c_scr%ISOPEN) THEN
6055           CLOSE c_scr;
6056         END IF;
6057         IF (x_msg_data IS NULL) THEN
6058             FND_MSG_PUB.Count_And_Get
6059             (      p_encoded   =>  FND_API.G_FALSE
6060                ,   p_count     =>  x_msg_count
6061                ,   p_data      =>  x_msg_data
6062             );
6063         END IF;
6064         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6065         --DBMS_OUTPUT.PUT_LINE('EXCEPTION FND_API.G_EXC_UNEXPECTED_ERROR '||x_msg_data);
6066     WHEN NO_DATA_FOUND THEN
6067         IF (c_scr%ISOPEN) THEN
6068           CLOSE c_scr;
6069         END IF;
6070         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6071         IF (x_msg_data IS NOT NULL) THEN
6072             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Tab ';
6073         ELSE
6074             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Tab ';
6075         END IF;
6076         --DBMS_OUTPUT.PUT_LINE('EXCEPTION NO_DATA_FOUND '||x_msg_data);
6077     WHEN OTHERS THEN
6078         IF (c_scr%ISOPEN) THEN
6079           CLOSE c_scr;
6080         END IF;
6081         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6082         IF (x_msg_data IS NOT NULL) THEN
6083             x_msg_data      :=  x_msg_data||' -> BSC_PMF_UI_WRAPPER.Delete_Tab ';
6084         ELSE
6085             x_msg_data      :=  SQLERRM||' at BSC_PMF_UI_WRAPPER.Delete_Tab ';
6086         END IF;
6087         --DBMS_OUTPUT.PUT_LINE('EXCEPTION OTHERS '||x_msg_data);
6088   end Check_Tab;
6089 
6090 PROCEDURE Check_Tabviews (
6091    p_tab_id              IN             NUMBER
6092   ,p_list_dependency     IN             VARCHAR2
6093   ,x_exist_dependency    OUT NOCOPY     VARCHAR2
6094   ,x_dep_obj_list        OUT NOCOPY     VARCHAR2
6095   ,x_return_status       OUT NOCOPY     VARCHAR2
6096   ,x_msg_count           OUT NOCOPY     NUMBER
6097   ,x_msg_data            OUT NOCOPY     VARCHAR2
6098  ) IS
6099 
6100   l_tab_view_id             BSC_TAB_VIEWS_VL.tab_view_id%TYPE;
6101   l_tab_view_name           BSC_TAB_VIEWS_VL.NAME%TYPE;
6102   l_dep_obj_list            VARCHAR2(2000);
6103   l_dep_obj_count           NUMBER;
6104   l_msg_data            VARCHAR2(1000);
6105   l_temp_list           VARCHAR2(200);
6106   l_msg_count           NUMBER;
6107   l_exist_dependency    VARCHAR2(10);
6108 
6109   CURSOR c_tab_views IS
6110     SELECT UNIQUE NAME, tab_view_id
6111     FROM BSC_TAB_VIEWS_VL
6112     WHERE tab_id = p_tab_id;
6113 
6114  BEGIN
6115     x_return_status := FND_API.G_RET_STS_SUCCESS;
6116     l_exist_dependency := FND_API.G_FALSE;
6117 
6118     FOR cd_tab_view IN c_tab_views LOOP
6119         l_tab_view_id := cd_tab_view.tab_view_id;
6120         l_tab_view_name := cd_tab_view.NAME;
6121 
6122         BSC_PMF_UI_WRAPPER.Check_Tabview_Dependency( p_tab_id           => p_tab_id
6123                                                     ,p_tab_view_id      => l_tab_view_id
6124                                                     ,p_list_dependency  => p_list_dependency
6125                                                     ,x_exist_dependency => l_exist_dependency
6126                                                     ,x_dep_obj_list     => l_temp_list
6127                                                     ,x_return_status    => x_return_status
6128                                                     ,x_msg_count        => l_msg_count
6129                                                     ,x_msg_data         => l_msg_data);
6130 
6131     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
6132           FND_MSG_PUB.Initialize;
6133           FND_MESSAGE.SET_NAME('BIS',l_msg_data);
6134           FND_MSG_PUB.ADD;
6135           RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
6136         END IF;
6137 
6138         IF (l_exist_dependency = FND_API.G_TRUE) THEN
6139       x_exist_dependency := l_exist_dependency;
6140       l_dep_obj_list := l_dep_obj_list || l_temp_list;
6141         END IF;
6142 
6143     EXIT WHEN (x_exist_dependency = FND_API.G_TRUE AND p_list_dependency = FND_API.G_FALSE);
6144 
6145 
6146     END LOOP;
6147 
6148     x_dep_obj_list := l_dep_obj_list;
6149 
6150 
6151  EXCEPTION
6152    WHEN FND_API.G_EXC_ERROR THEN
6153       IF (x_msg_data IS NULL) THEN
6154         FND_MSG_PUB.Count_And_Get
6155         (   p_encoded   =>  FND_API.G_FALSE
6156            ,p_count     =>  x_msg_count
6157            ,p_data      =>  x_msg_data
6158         );
6159       END IF;
6160       x_return_status :=  FND_API.G_RET_STS_ERROR;
6161    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6162       IF (x_msg_data IS NULL) THEN
6163         FND_MSG_PUB.Count_And_Get
6164         ( p_encoded   =>  FND_API.G_FALSE
6165          ,p_count     =>  x_msg_count
6166          ,p_data      =>  x_msg_data
6167          );
6168       END IF;
6169       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6170    WHEN OTHERS THEN
6171      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6172      IF (x_msg_data IS NOT NULL) THEN
6173         x_msg_data      :=  x_msg_data||' -> Check_Tabviews';
6174      ELSE
6175         x_msg_data      :=  SQLERRM||' at Check_Tabviews';
6176      END IF;
6177 
6178  END Check_Tabviews;
6179 
6180 PROCEDURE Check_Tabview_Dependency (
6181    p_tab_id              IN             NUMBER
6182   ,p_tab_view_id         IN             NUMBER
6183   ,p_list_dependency     IN             VARCHAR2
6184   ,x_exist_dependency    OUT NOCOPY     VARCHAR2
6185   ,x_dep_obj_list        OUT NOCOPY     VARCHAR2
6186   ,x_return_status       OUT NOCOPY     VARCHAR2
6187   ,x_msg_count           OUT NOCOPY     NUMBER
6188   ,x_msg_data            OUT NOCOPY     VARCHAR2
6189 ) IS
6190   l_param_search_string     FND_FORM_FUNCTIONS_VL.PARAMETERS%TYPE;
6191   l_portlet_type            VARCHAR2(20);
6192   l_temp_list               VARCHAR2(1000);
6193 BEGIN
6194     x_return_status := FND_API.G_RET_STS_SUCCESS;
6195     x_exist_dependency := FND_API.G_FALSE;
6196     l_portlet_type := 'CUSTOM_VIEW_PORTLET';
6197     l_param_search_string := '%pRequestType=C&%pTabId=' || p_tab_id || '&pViewId=' || p_tab_view_id;
6198 
6199     BIS_UTIL.Check_Object_Dependency(p_param_search_string => l_param_search_string
6200                                     ,p_obj_portlet_type    => l_portlet_type
6201                                     ,p_list_dependency     => p_list_dependency
6202                                     ,x_exist_dependency    => x_exist_dependency
6203                                     ,x_dep_obj_list        => x_dep_obj_list
6204                                     ,x_return_status       => x_return_status
6205                                     ,x_msg_count           => x_msg_count
6206                                     ,x_msg_data            => x_msg_data);
6207     IF ((x_return_status IS NOT NULL) AND (x_return_status  <> FND_API.G_RET_STS_SUCCESS)) THEN
6208           FND_MSG_PUB.Initialize;
6209           FND_MESSAGE.SET_NAME('BIS',x_msg_data);
6210           FND_MSG_PUB.ADD;
6211           RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
6212     END IF;
6213 
6214  EXCEPTION
6215    WHEN FND_API.G_EXC_ERROR THEN
6216       IF (x_msg_data IS NULL) THEN
6217         FND_MSG_PUB.Count_And_Get
6218         (   p_encoded   =>  FND_API.G_FALSE
6219            ,p_count     =>  x_msg_count
6220            ,p_data      =>  x_msg_data
6221         );
6222       END IF;
6223       x_return_status :=  FND_API.G_RET_STS_ERROR;
6224    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6225       IF (x_msg_data IS NULL) THEN
6226         FND_MSG_PUB.Count_And_Get
6227         ( p_encoded   =>  FND_API.G_FALSE
6228          ,p_count     =>  x_msg_count
6229          ,p_data      =>  x_msg_data
6230          );
6231       END IF;
6232       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6233    WHEN OTHERS THEN
6234      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6235      IF (x_msg_data IS NOT NULL) THEN
6236         x_msg_data      :=  x_msg_data||' -> Check_Tabview_Dependency';
6237      ELSE
6238         x_msg_data      :=  SQLERRM||' at Check_Tabview_Dependency';
6239      END IF;
6240 END Check_Tabview_Dependency;
6241 
6242  --/////////////////Added for TGSS Enahcnement ///////////////////////////////////
6243 
6244  PROCEDURE  Add_Or_Update_Tab_Logo
6245  (
6246    p_tab_id            IN NUMBER
6247   ,p_image_id          IN NUMBER
6248   ,p_file_name         IN VARCHAR2
6249   ,p_description       IN VARCHAR2
6250   ,p_width             IN NUMBER
6251   ,p_height            IN NUMBER
6252   ,p_mime_type         IN VARCHAR2
6253   ,x_image_id          OUT NOCOPY NUMBER
6254   ,x_return_status     OUT NOCOPY VARCHAR2
6255   ,x_msg_count         OUT NOCOPY NUMBER
6256   ,x_msg_data          OUT NOCOPY VARCHAR2
6257 ) IS
6258 l_count            NUMBER;
6259 l_next_image_id    BSC_SYS_IMAGES.image_id%TYPE;
6260 BEGIN
6261   SAVEPOINT AddOrUpdateTabLogo;
6262   FND_MSG_PUB.INITIALIZE;
6263   x_return_status := FND_API.G_RET_STS_SUCCESS;
6264 
6265 
6266   SELECT COUNT(0)
6267   INTO   l_count
6268   FROM bsc_sys_images bsi,
6269        bsc_sys_images_map_vl bsim
6270   WHERE bsim.source_type =BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6271   AND   bsim.source_code = p_tab_id
6272   AND   bsim.type = BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6273   AND   bsim.image_id = p_image_id
6274   AND   bsim.image_id = bsi.image_id;
6275 
6276 
6277   IF (l_count > 0) THEN
6278       --check if the image is owned by current NLS session
6279 
6280       SELECT COUNT(0)
6281       INTO   l_count
6282       FROM   bsc_sys_images_map_TL
6283       WHERE  source_type =BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6284       AND    source_code = p_tab_id
6285       AND    type = BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6286       AND    image_id = p_image_id
6287       AND    source_lang = USERENV('LANG');
6288 
6289       IF (l_count > 0) THEN
6290         --image owned by this NLS session, just simply update the same image
6291         x_image_id := p_image_id;
6292 
6293         BEGIN
6294           UPDATE  BSC_SYS_IMAGES
6295           SET     FILE_NAME              =   p_file_name,
6296                   DESCRIPTION            =   p_description,
6297                   WIDTH                  =   p_width,
6298                   HEIGHT                 =   p_height,
6299                   MIME_TYPE              =   p_mime_type,
6300                   LAST_UPDATE_DATE       =   SYSDATE,
6301                   LAST_UPDATED_BY        =   fnd_global.user_id,
6302                   LAST_UPDATE_LOGIN      =   fnd_global.login_id,
6303                   FILE_BODY              =   EMPTY_BLOB()
6304           WHERE   IMAGE_ID               =   p_image_id;
6305         EXCEPTION
6306           WHEN OTHERS THEN
6307             ROLLBACK TO AddOrUpdateTabLogo;
6308             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6309             x_msg_data := 'Update to BSC_SYS_IMAGES failed' || SQLERRM;
6310             RETURN;
6311         END;
6312 
6313         BSC_SYS_IMAGES_MAP_PKG.UPDATE_ROW
6314         (
6315            X_SOURCE_TYPE       => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6316           ,X_SOURCE_CODE       => p_tab_id
6317           ,X_TYPE              => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6318           ,X_IMAGE_ID          => p_image_id
6319           ,X_CREATION_DATE     => SYSDATE
6320           ,X_CREATED_BY        => fnd_global.user_id
6321           ,X_LAST_UPDATE_DATE  => SYSDATE
6322           ,X_LAST_UPDATED_BY   => fnd_global.user_id
6323           ,X_LAST_UPDATE_LOGIN => fnd_global.login_id
6324         );
6325 
6326       ELSE
6327         --image not owned by this NLS session, need to create a new image and update the image map
6328         SELECT BSC_SYS_IMAGE_ID_S.NEXTVAL
6329         INTO   l_next_image_id
6330         FROM   dual;
6331 
6332         x_image_id := l_next_image_id;
6333 
6334         BEGIN
6335           BSC_SYS_IMAGES_PKG.INSERT_ROW
6336           (
6337              X_IMAGE_ID           => l_next_image_id
6338             ,X_FILE_NAME          => p_file_name
6339             ,X_DESCRIPTION        => p_description
6340             ,X_WIDTH              => p_width
6341             ,X_HEIGHT             => p_height
6342             ,X_MIME_TYPE          => p_mime_type
6343             ,X_CREATED_BY         => fnd_global.user_id
6344             ,X_LAST_UPDATED_BY    => fnd_global.user_id
6345             ,X_LAST_UPDATE_LOGIN  => fnd_global.login_id
6346           );
6347 
6348         EXCEPTION
6349           WHEN OTHERS THEN
6350             ROLLBACK TO AddOrUpdateTabLogo;
6351             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6352             x_msg_data := 'Insertion to BSC_SYS_IMAGES_PKG failed' || SQLERRM;
6353             RETURN;
6354         END;
6355 
6356         BSC_SYS_IMAGES_MAP_PKG.UPDATE_ROW
6357         (
6358            X_SOURCE_TYPE       => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6359           ,X_SOURCE_CODE       => p_tab_id
6360           ,X_TYPE              => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6361           ,X_IMAGE_ID          => p_image_id
6362           ,X_CREATION_DATE     => SYSDATE
6363           ,X_CREATED_BY        => fnd_global.user_id
6364           ,X_LAST_UPDATE_DATE  => SYSDATE
6365           ,X_LAST_UPDATED_BY   => fnd_global.user_id
6366           ,X_LAST_UPDATE_LOGIN => fnd_global.login_id
6367         );
6368         END IF;
6369   ELSE
6370       --create a new image for this Simulation Tree Objective
6371       Create_Scorecard_logo (
6372         p_obj_id            => p_tab_id
6373        ,p_file_name         => p_file_name
6374        ,p_description       => p_description
6375        ,p_width             => p_width
6376        ,p_height            => p_height
6377        ,p_mime_type         => p_mime_type
6378        ,x_image_id          => x_image_id
6379        ,x_return_status     => x_return_status
6380        ,x_msg_count         => x_msg_count
6381        ,x_msg_data          => x_msg_data
6382       );
6383     END IF;
6384 
6385   EXCEPTION
6386     WHEN OTHERS THEN
6387       ROLLBACK TO AddOrUpdateTabLogo;
6388       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6389       x_msg_data      :=  SQLERRM;
6390  END Add_Or_Update_Tab_Logo;
6391 
6392 
6393  --/////////////////Added for TGSS Enahcnement ///////////////////////////////////
6394 
6395 
6396  PROCEDURE Create_Scorecard_logo (
6397    p_obj_id            IN NUMBER
6398   ,p_file_name         IN VARCHAR2
6399   ,p_description       IN VARCHAR2
6400   ,p_width             IN NUMBER
6401   ,p_height            IN NUMBER
6402   ,p_mime_type         IN VARCHAR2
6403   ,x_image_id          OUT NOCOPY NUMBER
6404   ,x_return_status     OUT NOCOPY VARCHAR2
6405   ,x_msg_count         OUT NOCOPY NUMBER
6406   ,x_msg_data          OUT NOCOPY VARCHAR2
6407  ) IS
6408   l_next_image_id      NUMBER;
6409   l_str                VARCHAR2(100);
6410  BEGIN
6411 
6412    SAVEPOINT CreateScorecardlogo;
6413    FND_MSG_PUB.INITIALIZE;
6414    x_return_status := FND_API.G_RET_STS_SUCCESS;
6415 
6416    SELECT BSC_SYS_IMAGE_ID_S.NEXTVAL
6417    INTO   l_next_image_id
6418    FROM   dual;
6419 
6420    x_image_id := l_next_image_id;
6421 
6422    BEGIN
6423      BSC_SYS_IMAGES_PKG.INSERT_ROW
6424      (
6425         X_IMAGE_ID         => l_next_image_id
6426        ,X_FILE_NAME        => p_file_name
6427        ,X_DESCRIPTION      => p_description
6428        ,X_WIDTH            => p_width
6429        ,X_HEIGHT           => p_height
6430        ,X_MIME_TYPE        => p_mime_type
6431        ,X_CREATED_BY       => fnd_global.user_id
6432        ,X_LAST_UPDATED_BY  => fnd_global.user_id
6433        ,X_LAST_UPDATE_LOGIN=> fnd_global.login_id
6434      );
6435 
6436    EXCEPTION
6437      WHEN OTHERS THEN
6438        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6439        x_msg_data := 'Insertion to BSC_SYS_IMAGES_PKG failed' || SQLERRM;
6440        RAISE;
6441    END;
6442 
6443    BSC_SYS_IMAGES_MAP_PKG.INSERT_ROW
6444    (
6445       X_ROWID              => l_str
6446      ,X_SOURCE_TYPE        => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6447      ,X_SOURCE_CODE        => p_obj_id
6448      ,X_TYPE               => BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6449      ,X_IMAGE_ID           => l_next_image_id
6450      ,X_CREATION_DATE      => SYSDATE
6451      ,X_CREATED_BY         => fnd_global.user_id
6452      ,X_LAST_UPDATE_DATE   => SYSDATE
6453      ,X_LAST_UPDATED_BY    => fnd_global.user_id
6454      ,X_LAST_UPDATE_LOGIN  => fnd_global.login_id
6455    );
6456  EXCEPTION
6457    WHEN others THEN
6458      ROLLBACK TO CreateScorecardlogo;
6459      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6460      x_msg_data      :=  SQLERRM;
6461      RAISE;
6462  END Create_Scorecard_logo;
6463 
6464 
6465  --/////////////////Added for TGSS Enahcnement ///////////////////////////////////
6466 
6467 
6468  PROCEDURE Delete_Tab_Logo
6469  (
6470      p_tab_id            IN         BSC_TABS_B.tab_id%TYPE
6471     ,x_return_status     OUT NOCOPY VARCHAR2
6472     ,x_msg_count         OUT NOCOPY NUMBER
6473     ,x_msg_data          OUT NOCOPY VARCHAR2
6474  )IS
6475   CURSOR c_tab_logo IS
6476   SELECT image_id
6477   FROM   bsc_sys_images_map_tl
6478   WHERE  source_type = BSC_PMF_UI_WRAPPER.C_SCORECARD_LOGO_TYPE
6479   AND    source_code = p_tab_id;
6480 
6481   l_image_id  BSC_SYS_IMAGES_MAP_TL.image_id%TYPE;
6482 
6483  BEGIN
6484    SAVEPOINT DeleteTabLogo;
6485    FND_MSG_PUB.INITIALIZE;
6486    x_return_status := FND_API.G_RET_STS_SUCCESS;
6487 
6488    IF(p_tab_id IS NOT NULL) THEN
6489        FOR cd IN  c_tab_logo LOOP
6490          l_image_id := cd.image_id;
6491        END LOOP;
6492        DELETE FROM bsc_sys_images_map_tl
6493        WHERE  image_id =l_image_id;
6494 
6495        DELETE FROM bsc_sys_images
6496        WHERE  image_id =l_image_id;
6497    END IF;
6498 
6499  EXCEPTION
6500    WHEN others THEN
6501      ROLLBACK TO DeleteTabLogo;
6502      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6503      x_msg_data      :=  SQLERRM;
6504      RAISE;
6505  END  Delete_Tab_Logo;
6506 
6507 
6508 
6509 END BSC_PMF_UI_WRAPPER;