DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_II_RELATIONSHIPS_PVT

Source


1 PACKAGE BODY csi_ii_relationships_pvt AS
2 /* $Header: csiviirb.pls 120.29.12020000.4 2012/09/13 11:20:02 sjawaji ship $ */
3 -- start of comments
4 -- package name     : csi_ii_relationships_pvt
5 -- purpose          :
6 -- history          :
7 -- note             :
8 -- END of comments
9 
10 
11 g_pkg_name  CONSTANT VARCHAR2(30) := 'csi_ii_relationships_pvt';
12 g_file_name CONSTANT VARCHAR2(12) := 'csiviirb.pls';
13 p_rel_glbl_tbl   csi_datastructures_pub.ii_relationship_tbl;
14 p_glbl_ctr       NUMBER := 0;
15 
16 p_ii_rel_level_glbl_tbl  csi_ii_relationships_pvt.ii_relationship_level_tbl;   --bug 10321217
17 
18 
19 /* Cyclic Relationships */
20 
21 /* TYPE instance_rec IS RECORD
22 ( instance_id  NUMBER,
23   hop          NUMBER );
24 
25 TYPE instance_tbl IS TABLE OF instance_rec
26 INDEX BY BINARY_INTEGER ;
27 */
28 PROCEDURE get_cyclic_relationships
29  (
30      p_api_version               IN  NUMBER,
31      p_commit                    IN  VARCHAR2,
32      p_init_msg_list             IN  VARCHAR2,
33      p_validation_level          IN  NUMBER,
34      p_instance_id               IN  NUMBER ,
35      p_depth                     IN  NUMBER,
36      p_time_stamp                IN  DATE,
37      p_active_relationship_only  IN  VARCHAR2,
38      x_relationship_tbl          OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
39      x_return_status             OUT NOCOPY VARCHAR2,
40      x_msg_count                 OUT NOCOPY NUMBER,
41      x_msg_data                  OUT NOCOPY VARCHAR2
42  )
43 IS
44    l_neighbor_inst_tbl        instance_tbl;
45    l_visited_tbl              instance_tbl;
46    l_visited_index            INTEGER;
47    l_non_visited_tbl          instance_tbl;
48    l_non_visited_index        INTEGER;
49    l_cur_instance_id          NUMBER;
50    l_in_visited_tbl           BOOLEAN;
51    l_in_non_visited_tbl       BOOLEAN;
52    l_pass_number              NUMBER := 0;
53    l_first_non_vis_ind        INTEGER;
54 
55    l_non_visited_tbl_cnt      NUMBER ;
56    l_start_time               VARCHAR2(60);
57    l_end_time                 VARCHAR2(60);
58    l_non_vis_start            NUMBER ;
59    l_api_name                 CONSTANT VARCHAR2(50) := 'get_cyclic_relationships';
60    l_api_version              CONSTANT NUMBER        := 1.0;
61    l_return_status_full       VARCHAR2(1);
62    l_debug_level              NUMBER;
63    l_cur_hop                  NUMBER;
64 BEGIN
65    -- standard start of api savepoint
66    --SAVEPOINT get_cylic_relationships_pvt;
67 
68    -- standard call to check for call compatibility.
69    IF NOT fnd_api.compatible_api_call ( l_api_version,
70                                         p_api_version,
71                                         l_api_name,
72                                         g_pkg_name)
73    THEN
74       RAISE fnd_api.g_exc_unexpected_error;
75    END IF;
76 
77    -- initialize message list if p_init_msg_list is set to true.
78    IF fnd_api.to_boolean( p_init_msg_list )
79    THEN
80       fnd_msg_pub.initialize;
81    END IF;
82 
83    -- initialize api return status to success
84    x_return_status := fnd_api.g_ret_sts_success;
85    l_debug_level   := fnd_profile.value('CSI_DEBUG_LEVEL');
86 
87    IF (l_debug_level > 0) THEN
88        CSI_gen_utility_pvt.put_line( 'get_cyclic_relationships');
89    END IF;
90 
91    IF (l_debug_level > 1) THEN
92        CSI_gen_utility_pvt.put_line(
93                             p_api_version             ||'-'||
94                             p_commit                  ||'-'||
95                             p_init_msg_list           ||'-'||
96                             p_validation_level        ||'-'||
97                             p_instance_id             ||'-'||
98                             p_time_stamp              ||'-'||
99                             p_active_relationship_only );
100 
101    END IF;
102 
103    --
104    -- API BODY
105    --
106    -- ******************************************************************
107    -- validate environment
108    -- ******************************************************************
109 
110    IF p_instance_id IS NULL
111    THEN
112        fnd_message.set_name('CSI', 'CSI_INVALID_PARAMETERS');
113        fnd_msg_pub.add;
114        x_return_status := fnd_api.g_ret_sts_error;
115        RAISE fnd_api.g_exc_error;
116    END IF;
117 
118    select to_char(sysdate,'YY-MON-DD HH:MI:SS')
119    into   l_start_time
120    from dual ;
121 
122    -- Initialize the given instance to non-visited table
123 
124    l_non_visited_tbl(1).instance_id := p_instance_id ;
125    l_non_visited_tbl(1).hop := 0 ;
126    l_pass_number  := 0;
127    l_non_visited_tbl_cnt := 1 ;
128    l_non_vis_start := 0 ;
129 
130    WHILE l_non_visited_tbl.COUNT > 0
131    LOOP
132       -- Get the first element from Non-Visited table, Note that this may not
133       -- be having index of 1, it may have 5, 1, 10 etc.
134 
135       l_non_visited_index := l_non_visited_tbl.FIRST;
136       l_cur_instance_id := l_non_visited_tbl(l_non_visited_index).instance_id ;
137       l_cur_hop := l_non_visited_tbl(l_non_visited_index).hop ;
138 
139       -- Now delete this instance from Non-visited table
140 
141       l_non_visited_tbl.DELETE(l_non_visited_index);
142       l_non_vis_start := l_non_vis_start + 1 ;
143 
144       --  Get All the Relationships for l_cur_instance_id and populate it in x_relationship_tbl,
145       --  If that relationship does not exists. get_rel_for_instance will populate the relationship
146       --  for l_cur_instance_id only if it does not already exists in x_relationship_tbl
147 
148       IF p_depth IS NOT NULL
149       THEN
150          IF l_cur_hop+1 <= p_depth
151          THEN
152             get_rel_for_instance(
153                      l_cur_instance_id,
154                      p_time_stamp,
155                      p_active_relationship_only ,
156                      x_relationship_tbl);
157          END IF ;
158       ELSE
159         -- p_depth is null so get all the relations
160          get_rel_for_instance(l_cur_instance_id,
161                   p_time_stamp,
162                   p_active_relationship_only,
163                   x_relationship_tbl);
164       END IF ;
165 
166       --  Now Push this Instance into Visited table.
167       l_visited_index := NVL(l_visited_tbl.LAST,0) + 1 ;
168       l_visited_tbl(l_visited_index).instance_id := l_cur_instance_id ;
169 
170       --  Get neighbor Instances for l_cur_instance_id
171       get_neighbors_for_instance(l_cur_instance_id ,
172                                  l_cur_hop ,
173                                  p_depth ,
174                                  p_time_stamp ,
175                                  p_active_relationship_only ,
176                                  l_neighbor_inst_tbl);
177 
178       FOR i IN 1..l_neighbor_inst_tbl.COUNT
179       LOOP
180          l_in_visited_tbl := FALSE ;
181          l_in_non_visited_tbl := FALSE ;
182 
183          --  Check if this instance is in Visited Table
184          FOR j IN 1..l_visited_tbl.COUNT
185          LOOP
186             IF l_neighbor_inst_tbl(i).instance_id = l_visited_tbl(j).instance_id
187             THEN
188                l_in_visited_tbl := TRUE ;
189                EXIT ;
190             END IF ;
191          END LOOP ;
192 
193          --  Check if this instance is in Non Visited Table
194          IF NOT l_in_visited_tbl
195          THEN
196             FOR k IN l_non_vis_start..l_non_visited_tbl_cnt
197             LOOP
198                BEGIN
199                   IF l_neighbor_inst_tbl(i).instance_id = l_non_visited_tbl(k).instance_id
200                   THEN
201                      l_in_non_visited_tbl := TRUE ;
202                      EXIT ;
203                   END IF ;
204                EXCEPTION
205                   WHEN NO_DATA_FOUND THEN
206                      -- There is a gap in the range so ignore and go ahead
207                      NULL ;
208                END ;
209            END LOOP ; --l_non_vis_start..l_non_visited_tbl_cnt
210         END IF ;
211 
212         IF NOT (l_in_visited_tbl OR l_in_non_visited_tbl)
213         THEN
214            -- As this instance is neither in Visited Table nor in Non-Visited Table,
215            --  Push this Instance into Non Visited table.
216 
217            l_non_visited_index := NVL(l_non_visited_tbl.LAST,0) + 1 ;
218            l_non_visited_tbl(l_non_visited_index).instance_id := l_neighbor_inst_tbl(i).instance_id ;
219            l_non_visited_tbl(l_non_visited_index).hop := l_neighbor_inst_tbl(i).
220 hop ;
221            l_non_visited_tbl_cnt := l_non_visited_tbl_cnt+1 ;
222 
223          END IF ;
224       END LOOP ;  -- l_neighbor_inst_tbl.COUNT;
225       l_pass_number := l_pass_number+1 ;
226 
227    END LOOP ; -- l_non_visited_tbl.COUNT > 0
228 
229    select to_char(sysdate,'YY-MON-DD HH:MI:SS')
230    into   l_end_time
231    from   dual ;
232 
233 EXCEPTION
234    WHEN fnd_api.g_exc_unexpected_error THEN
235       --ROLLBACK TO get_cylic_relationships_pvt;
236       x_return_status := fnd_api.g_ret_sts_unexp_error ;
237       fnd_msg_pub.count_AND_get
238            (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
239            p_count => x_msg_count ,
240             p_data => x_msg_data);
241 
242    WHEN OTHERS THEN
243       --ROLLBACK TO get_cylic_relationships_pvt;
244       x_return_status := fnd_api.g_ret_sts_unexp_error ;
245       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
246       THEN
247          fnd_msg_pub.add_exc_msg(g_pkg_name ,l_api_name);
248       END IF;
249       fnd_msg_pub.count_AND_get
250          (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
251            p_count => x_msg_count ,
252           p_data => x_msg_data);
253 END get_cyclic_relationships;
254 
255 PROCEDURE get_rel_for_instance
256        (p_instance_id IN NUMBER,
257         p_time_stamp IN DATE,
258         p_active_relationship_only IN VARCHAR2,
259         x_relationship_tbl IN OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl)
260 IS
261 
262 l_cur_instance_id          NUMBER;
263 i                          NUMBER;
264 l_relation_exists          BOOLEAN;
265 l_sysdate                  DATE;
266 l_time_stamp               DATE;
267 
268 
269 -- x_relationship_tbl may be already populated with instances.
270 -- This procedure gets all the relationships for the given instance and populates it in
271 -- x_relationship_tbl if it already does not exists.
272 
273    CURSOR relations_cur (c_sysdate IN DATE) IS
274    SELECT relationship_id ,
275           relationship_type_code ,
276           object_id ,
277           subject_id ,
278           position_reference ,
279           active_start_date  ,
280           active_end_date ,
281           display_order ,
282           mandatory_flag ,
283           context ,
284           attribute1 ,
285           attribute2 ,
286           attribute3 ,
287           attribute4 ,
288           attribute5 ,
289           attribute6 ,
290           attribute7 ,
291           attribute8 ,
292           attribute9 ,
293           attribute10,
294           attribute11,
295           attribute12,
296           attribute13,
297           attribute14,
298           attribute15,
299           object_version_number
300     FROM  csi_ii_relationships
301     WHERE (subject_id = p_instance_id OR object_id = p_instance_id )
302     AND   relationship_type_code = 'CONNECTED-TO'
303     AND   nvl(active_start_date,creation_date) <= NVL(l_time_stamp,nvl(active_start_date,creation_date))
304     AND   DECODE(p_active_relationship_only,FND_API.G_TRUE,NVL((active_end_date),c_sysdate+1),c_sysdate+1) > NVL((l_time_stamp),c_sysdate) ;
305 
306 BEGIN
307    i:= 0;
308    SELECT SYSDATE
309    INTO   l_sysdate
310    FROM   dual;
311    --
312    -- srramakr Bug # 2882876
313    IF p_time_stamp = FND_API.G_MISS_DATE THEN
314       l_time_stamp := null;
315    ELSE
316       l_time_stamp := p_time_stamp;
317    END IF;
318    --
319    FOR relations_rec IN relations_cur(l_sysdate)
320    LOOP
321       l_relation_exists := FALSE ;
322       IF x_relationship_tbl.COUNT = 0 THEN
323          x_relationship_tbl(1).relationship_id  := relations_rec.relationship_id ;
324          x_relationship_tbl(1).relationship_type_code := relations_rec.relationship_type_code ;
325          x_relationship_tbl(1).object_id := relations_rec.object_id ;
326          x_relationship_tbl(1).subject_id := relations_rec.subject_id ;
327          x_relationship_tbl(1).position_reference := relations_rec.position_reference ;
328          x_relationship_tbl(1).active_start_date  := relations_rec.active_start_date ;
329          x_relationship_tbl(1).active_end_date := relations_rec.active_end_date  ;
330          x_relationship_tbl(1).display_order := relations_rec.display_order;
331          x_relationship_tbl(1).mandatory_flag := relations_rec.mandatory_flag ;
332          x_relationship_tbl(1).context := relations_rec.context ;
333          x_relationship_tbl(1).attribute1 := relations_rec.attribute1 ;
334          x_relationship_tbl(1).attribute2 := relations_rec.attribute2 ;
335          x_relationship_tbl(1).attribute3 := relations_rec.attribute3 ;
336          x_relationship_tbl(1).attribute4 := relations_rec.attribute4 ;
337          x_relationship_tbl(1).attribute5 := relations_rec.attribute5 ;
338          x_relationship_tbl(1).attribute6 := relations_rec.attribute6 ;
339          x_relationship_tbl(1).attribute7 := relations_rec.attribute7 ;
340          x_relationship_tbl(1).attribute8 := relations_rec.attribute8 ;
341          x_relationship_tbl(1).attribute9 := relations_rec.attribute9 ;
342          x_relationship_tbl(1).attribute10:= relations_rec.attribute10 ;
343          x_relationship_tbl(1).attribute11:= relations_rec.attribute11;
344          x_relationship_tbl(1).attribute12:= relations_rec.attribute12;
345          x_relationship_tbl(1).attribute13:= relations_rec.attribute13;
346          x_relationship_tbl(1).attribute14:= relations_rec.attribute14 ;
347          x_relationship_tbl(1).attribute15:= relations_rec.attribute15;
348          x_relationship_tbl(1).object_version_number:= relations_rec.object_version_number; -- Porting fix for Bug 4243513
349       ELSE
350          FOR j IN 1..x_relationship_tbl.COUNT
351          LOOP
352             IF x_relationship_tbl(j).relationship_id = relations_rec.relationship_id
353             THEN
354                l_relation_exists := TRUE ;
355                EXIT ;
356             END IF ;
357          END LOOP ;
358 
359          IF NOT l_relation_exists THEN
360             i := NVL(x_relationship_tbl.LAST,0) + 1 ;
361             x_relationship_tbl(i).relationship_id  := relations_rec.relationship_id ;
362             x_relationship_tbl(i).relationship_type_code := relations_rec.relationship_type_code ;
363             x_relationship_tbl(i).object_id := relations_rec.object_id ;
364             x_relationship_tbl(i).subject_id := relations_rec.subject_id ;
365             x_relationship_tbl(i).position_reference := relations_rec.position_reference;
366             x_relationship_tbl(i).active_start_date  := relations_rec.active_start_date;
367             x_relationship_tbl(i).active_end_date := relations_rec.active_end_date;
368             x_relationship_tbl(i).display_order := relations_rec.display_order;
369             x_relationship_tbl(i).mandatory_flag := relations_rec.mandatory_flag;
370             x_relationship_tbl(i).context := relations_rec.context ;
371             x_relationship_tbl(i).attribute1 := relations_rec.attribute1 ;
372             x_relationship_tbl(i).attribute2 := relations_rec.attribute2 ;
373             x_relationship_tbl(i).attribute3 := relations_rec.attribute3 ;
374             x_relationship_tbl(i).attribute4 := relations_rec.attribute4 ;
375             x_relationship_tbl(i).attribute5 := relations_rec.attribute5 ;
376             x_relationship_tbl(i).attribute6 := relations_rec.attribute6 ;
377             x_relationship_tbl(i).attribute7 := relations_rec.attribute7 ;
378             x_relationship_tbl(i).attribute8 := relations_rec.attribute8 ;
379             x_relationship_tbl(i).attribute9 := relations_rec.attribute9 ;
380             x_relationship_tbl(i).attribute10:= relations_rec.attribute10;
381             x_relationship_tbl(i).attribute11:= relations_rec.attribute11;
382             x_relationship_tbl(i).attribute12:= relations_rec.attribute12;
383             x_relationship_tbl(i).attribute13:= relations_rec.attribute13;
384             x_relationship_tbl(i).attribute14:= relations_rec.attribute14;
385             x_relationship_tbl(i).attribute15:= relations_rec.attribute15;
386             x_relationship_tbl(i).object_version_number:= relations_rec.object_version_number; -- Porting fix for Bug 4243513
387          END IF ;
388        END IF ;
389     END LOOP ;
390 END get_rel_for_instance ;
391 
392 PROCEDURE get_neighbors_for_instance(p_instance_id IN NUMBER ,
393                                      p_hop  IN NUMBER,
394                                      p_depth IN NUMBER,
395                                      p_time_stamp IN DATE,
396                                      p_active_relationship_only IN VARCHAR2,
397                                      x_neighbor_inst_tbl OUT NOCOPY instance_tbl )
398 IS
399 
400    i           NUMBER ;
401    l_sysdate   DATE ;
402    l_time_stamp DATE;
403 
404    CURSOR neighbor_cur (p_instance_id IN NUMBER,
405                         c_sysdate IN DATE )
406    IS
407       SELECT subject_id instance_id
408       FROM   csi_ii_relationships
409       WHERE  (subject_id = p_instance_id OR object_id = p_instance_id)
410       AND    relationship_type_code = 'CONNECTED-TO'
411       AND    nvl(active_start_date,creation_date) <= NVL(l_time_stamp,nvl(active_start_date,creation_date))
412       AND    DECODE(p_active_relationship_only,FND_API.G_TRUE,NVL((active_end_date),c_sysdate+1),c_sysdate+1) > NVL((l_time_stamp),c_sysdate)
413       UNION ALL
414       SELECT object_id instance_id
415       FROM   csi_ii_relationships
416       WHERE  (subject_id = p_instance_id OR object_id = p_instance_id)
417       AND    relationship_type_code = 'CONNECTED-TO'
418       AND    nvl(active_start_date,creation_date) <= NVL(l_time_stamp,nvl(active_start_date,creation_date))
419       AND    DECODE(p_active_relationship_only,FND_API.G_TRUE,NVL((active_end_date),c_sysdate+1),c_sysdate+1) > NVL((l_time_stamp),c_sysdate)  ;
420 
421 BEGIN
422    SELECT SYSDATE
423    INTO   l_sysdate
424    FROM  DUAL;
425    --
426    -- srramakr Bug # 2882876
427    IF p_time_stamp = FND_API.G_MISS_DATE THEN
428       l_time_stamp := null;
429    ELSE
430       l_time_stamp := p_time_stamp;
431    END IF;
432    --
433    i:= 0;
434    x_neighbor_inst_tbl.DELETE ;
435 
436    FOR neighbor_rec IN neighbor_cur (p_instance_id, l_sysdate)
437    LOOP
438       IF neighbor_rec.instance_id <> p_instance_id
439       THEN
440          i := i+1;
441          IF p_depth IS NOT NULL
442          THEN
443             IF p_hop+1 <= p_depth
444             THEN
445                x_neighbor_inst_tbl(i).instance_id := neighbor_rec.instance_id ;
446                x_neighbor_inst_tbl(i).hop := p_hop+1 ;
447             END IF ;
448          ELSE
449             x_neighbor_inst_tbl(i).instance_id := neighbor_rec.instance_id;
450             x_neighbor_inst_tbl(i).hop := p_hop+1 ;
451          END IF ; --p_depth IS NOT NULL
452       END IF ;
453    END LOOP;
454 END get_neighbors_for_instance ;
455 
456 /* End of Cyclic Relationships */
457 
458 PROCEDURE validate_ii_relationships(
459     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
460     p_validation_level           IN   NUMBER       := fnd_api.g_valid_level_full,
461     p_validation_mode            IN   VARCHAR2,
462     p_ii_relationship_tbl        IN   csi_datastructures_pub.ii_relationship_tbl,
463     x_return_status              OUT NOCOPY  VARCHAR2,
464     x_msg_count                  OUT NOCOPY  NUMBER,
465     x_msg_data                   OUT NOCOPY  VARCHAR2
466     );
467 
468 PROCEDURE validate_relationship_id (
469     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
470     p_validation_mode            IN   VARCHAR2,
471     p_relationship_id            IN   NUMBER,
472     x_return_status              OUT NOCOPY  VARCHAR2,
473     x_msg_count                  OUT NOCOPY  NUMBER,
474     x_msg_data                   OUT NOCOPY  VARCHAR2
475     );
476 
477 PROCEDURE validate_rel_type_code (
478     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
479     p_validation_mode            IN   VARCHAR2,
480     p_relationship_type_code     IN   VARCHAR2,
481     x_return_status              OUT NOCOPY  VARCHAR2,
482     x_msg_count                  OUT NOCOPY  NUMBER,
483     x_msg_data                   OUT NOCOPY  VARCHAR2
484     );
485 
486 
487 PROCEDURE validate_object_id (
488     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
489     p_validation_mode            IN   VARCHAR2,
490     p_object_id                  IN   NUMBER,
491     x_return_status              OUT NOCOPY  VARCHAR2,
492     x_msg_count                  OUT NOCOPY  NUMBER,
493     x_msg_data                   OUT NOCOPY  VARCHAR2
494     );
495 
496 
497 PROCEDURE validate_subject_id (
498     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
499     p_validation_mode            IN   VARCHAR2,
500     p_subject_id                 IN   NUMBER,
501     x_return_status              OUT NOCOPY  VARCHAR2,
502     x_msg_count                  OUT NOCOPY  NUMBER,
503     x_msg_data                   OUT NOCOPY  VARCHAR2
504     );
505 
506 
507 PROCEDURE validate_active_end_date (
508     p_init_msg_list              IN   VARCHAR2     := fnd_api.g_false,
509     p_validation_mode            IN   VARCHAR2,
510     p_active_end_date            IN   DATE,
511     x_return_status              OUT NOCOPY  VARCHAR2,
512     x_msg_count                  OUT NOCOPY  NUMBER,
513     x_msg_data                   OUT NOCOPY  VARCHAR2
514     );
515 
516 
517 PROCEDURE define_columns(
518 p_relations_rec                IN   csi_datastructures_pub.ii_relationship_rec,
519 p_cur_get_relations            IN   NUMBER
520 )
521 IS
522 BEGIN
523 
524 
525       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 1, p_relations_rec.relationship_id);
526       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 2, p_relations_rec.relationship_type_code,30);
527       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 3, p_relations_rec.object_id);
528       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 4, p_relations_rec.subject_id);
529       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 5, p_relations_rec.subject_has_child,1);
530       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 6, p_relations_rec.position_reference,30);
531       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 7, p_relations_rec.active_start_date);
532       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 8, p_relations_rec.active_end_date);
533       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 9, p_relations_rec.display_order);
534       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 10, p_relations_rec.mandatory_flag,1);
535       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 11, p_relations_rec.context,30);
536       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 12, p_relations_rec.attribute1,150);
537       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 13, p_relations_rec.attribute2,150);
538       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 14, p_relations_rec.attribute3,150);
539       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 15, p_relations_rec.attribute4,150);
540       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 16, p_relations_rec.attribute5,150);
541       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 17, p_relations_rec.attribute6,150);
542       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 18, p_relations_rec.attribute7,150);
543       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 19, p_relations_rec.attribute8,150);
544       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 20, p_relations_rec.attribute9,150);
545       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 21, p_relations_rec.attribute10,150);
546       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 22, p_relations_rec.attribute11,150);
547       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 23, p_relations_rec.attribute12,150);
548       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 24, p_relations_rec.attribute13,150);
549       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 25, p_relations_rec.attribute14,150);
550       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 26, p_relations_rec.attribute15,150);
551       DBMS_SQL.DEFINE_COLUMN(p_cur_get_relations, 27, p_relations_rec.object_version_number);
552 
553 
554 END define_columns;
555 
556 
557 PROCEDURE get_column_values(
558     p_cur_get_relations      IN   NUMBER,
559     x_rel_rec                OUT NOCOPY  csi_datastructures_pub.ii_relationship_rec
560 )
561 IS
562 BEGIN
563       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 1, x_rel_rec.relationship_id);
564       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 2, x_rel_rec.relationship_type_code);
565       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 3, x_rel_rec.object_id);
566       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 4, x_rel_rec.subject_id);
567       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 5, x_rel_rec.subject_has_child);
568       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 6, x_rel_rec.position_reference);
569       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 7, x_rel_rec.active_start_date);
570       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 8, x_rel_rec.active_end_date);
571       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 9, x_rel_rec.display_order);
572       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 10, x_rel_rec.mandatory_flag);
573       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 11, x_rel_rec.context);
574       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 12, x_rel_rec.attribute1);
575       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 13, x_rel_rec.attribute2);
576       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 14, x_rel_rec.attribute3);
577       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 15, x_rel_rec.attribute4);
578       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 16, x_rel_rec.attribute5);
579       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 17, x_rel_rec.attribute6);
580       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 18, x_rel_rec.attribute7);
581       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 19, x_rel_rec.attribute8);
582       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 20, x_rel_rec.attribute9);
583       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 21, x_rel_rec.attribute10);
584       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 22, x_rel_rec.attribute11);
585       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 23, x_rel_rec.attribute12);
586       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 24, x_rel_rec.attribute13);
587       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 25, x_rel_rec.attribute14);
588       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 26, x_rel_rec.attribute15);
589       DBMS_SQL.COLUMN_VALUE(p_cur_get_relations, 27, x_rel_rec.object_version_number);
590 
591 END get_column_values;
592 
593 
594 PROCEDURE bind(
595     p_relationship_query_rec            IN   csi_datastructures_pub.relationship_query_rec,
596     p_cur_get_relations                 IN   NUMBER
597 )
598 IS
599 BEGIN
600 
601       IF( (p_relationship_query_rec.relationship_id IS NOT NULL) AND (p_relationship_query_rec.relationship_id <> fnd_api.g_miss_num) )
602       THEN
603           dbms_sql.bind_variable(p_cur_get_relations, 'relationship_id', p_relationship_query_rec.relationship_id);
604       END IF;
605 
606       IF( (p_relationship_query_rec.relationship_type_code IS NOT NULL) AND (p_relationship_query_rec.relationship_type_code <> fnd_api.g_miss_char) )
607       THEN
608           dbms_sql.bind_variable(p_cur_get_relations, 'relationship_type_code', p_relationship_query_rec.relationship_type_code);
609       END IF;
610 
611       IF( (p_relationship_query_rec.object_id IS NOT NULL) AND (p_relationship_query_rec.object_id <> fnd_api.g_miss_num) )
612       THEN
613           dbms_sql.bind_variable(p_cur_get_relations, 'object_id', p_relationship_query_rec.object_id);
614       END IF;
615 
616       IF( (p_relationship_query_rec.subject_id IS NOT NULL) AND (p_relationship_query_rec.subject_id <> fnd_api.g_miss_num) )
617       THEN
618           dbms_sql.bind_variable(p_cur_get_relations, 'subject_id', p_relationship_query_rec.subject_id);
619       END IF;
620 
621 
622 END bind;
623 
624 
625 PROCEDURE gen_select(
626     p_relship_query_rec               IN    csi_datastructures_pub.relationship_query_rec,
627     x_select_cl                       OUT NOCOPY   VARCHAR2
628 )
629 IS
630 l_table_name                                VARCHAR2(30);
631 BEGIN
632 
633  x_select_cl := 'SELECT relationship_id,relationship_type_code,object_id,subject_id'
634                 ||',nvl((select '||'''Y'''|| ' from csi_ii_relationships where object_id = ciir.subject_id and relationship_type_code = ciir.relationship_type_code and rownum=1), '||'''N'''||') subject_has_child,'
635                 ||'position_reference,active_start_date,active_end_date,display_order,mandatory_flag,context,attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,'
636                 ||'attribute8,attribute9,attribute10,attribute11,attribute12,attribute13,attribute14'
637                 ||',attribute15,object_version_number FROM csi_ii_relationships ciir';
638 
639 
640 END gen_select;
641 
642 PROCEDURE gen_relations_where(
643     p_relship_query_rec               IN    csi_datastructures_pub.relationship_query_rec,
644     p_active_relationship_only        IN    VARCHAR2,
645     p_depth                           IN    NUMBER,
646     x_relations_where                 OUT NOCOPY   VARCHAR2
647 )
648 IS
649 CURSOR c_chk_str1(p_rec_item VARCHAR2) IS
650     SELECT instr(p_rec_item, '%', 1, 1)
651     FROM dual;
652 CURSOR c_chk_str2(p_rec_item VARCHAR2) IS
653     SELECT instr(p_rec_item, '_', 1, 1)
654     FROM dual;
655 
656 -- return values from cursors
657 str_csr1   NUMBER;
658 str_csr2   NUMBER;
659 i          NUMBER;
660 l_operator VARCHAR2(10);
661 l_cnt      NUMBER :=0;
662 
663 BEGIN
664 
665 
666       IF( (p_relship_query_rec.relationship_id IS NOT NULL) AND (p_relship_query_rec.relationship_id <> fnd_api.g_miss_num) )
667       THEN
668          l_cnt:=l_cnt+1;
669           IF(x_relations_where IS NULL) THEN
670               x_relations_where := ' WHERE ';
671           ELSE
672               x_relations_where := x_relations_where || ' AND ';
673           END IF;
674           x_relations_where := x_relations_where || 'relationship_id = :relationship_id';
675       END IF;
676 
677    /*** COMMENTED   IF  p_active_relationship_only = 'T' THEN
678       l_cnt:=l_cnt+1;
679           IF(x_relations_where IS NULL) THEN
680              x_relations_where := ' WHERE ';
681           ELSE
682              x_relations_where := x_relations_where ||' AND ';
683           END IF;
684              x_relations_where := x_relations_where ||' ( active_end_date is null or active_end_date >= SYSDATE ) ';
685       END IF; **** END OF COMMENT ****/
686 
687       IF( (p_relship_query_rec.subject_id IS NOT NULL) AND (p_relship_query_rec.subject_id <> fnd_api.g_miss_num) )
688       THEN
689         l_cnt:=l_cnt+1;
690           IF(x_relations_where IS NULL) THEN
691               x_relations_where := ' WHERE ';
692           ELSE
693               x_relations_where := x_relations_where || ' AND ';
694           END IF;
695           x_relations_where := x_relations_where || 'subject_id = :subject_id';
696       END IF;
697 
698       IF( (p_relship_query_rec.relationship_type_code IS NOT NULL) AND (p_relship_query_rec.relationship_type_code <> fnd_api.g_miss_char) )
699       THEN
700       l_cnt:=l_cnt+1;
701       i:=0;
702           -- check IF item value contains '%' wildcard
703             OPEN c_chk_str1(p_relship_query_rec.relationship_type_code);
704             FETCH c_chk_str1 INTO str_csr1;
705             CLOSE c_chk_str1;
706             IF(str_csr1 <> 0) THEN
707               l_operator := ' like ';
708               i:=1;
709             ELSE
710               l_operator := ' = ';
711             END IF;
712             IF i=0 THEN
713           -- check if item value contains '_' wildcard
714             OPEN c_chk_str2(p_relship_query_rec.relationship_type_code);
715             FETCH c_chk_str2 INTO str_csr2;
716             CLOSE c_chk_str2;
717             IF(str_csr2 <> 0) THEN
718               l_operator := ' like ';
719             ELSE
720               l_operator := ' = ';
721             END IF;
722             END IF;
723             IF(x_relations_where IS NULL) THEN
724               x_relations_where := ' WHERE ';
725             ELSE
726               x_relations_where := x_relations_where || ' AND ';
727             END IF;
728               x_relations_where := x_relations_where || 'relationship_type_code ' || l_operator || ' :relationship_type_code';
729       END IF;
730 
731       IF( (p_relship_query_rec.object_id IS NOT NULL) AND (p_relship_query_rec.object_id <> fnd_api.g_miss_num) )
732       THEN
733           IF(x_relations_where IS NULL) THEN
734               x_relations_where := ' WHERE ';
735           ELSE
736               IF l_cnt>1 THEN
737               x_relations_where := x_relations_where || ' AND ';
738               ELSE
739               x_relations_where := x_relations_where || ' START WITH ';
740               END IF;
741           END IF;
742           IF l_cnt>1 THEN
743              x_relations_where := x_relations_where || 'object_id = :object_id ';
744           ELSE
745              x_relations_where := x_relations_where || 'object_id = :object_id ' ||'CONNECT BY object_id = PRIOR subject_id' ;
746              IF ( (p_depth IS NOT NULL) AND (p_depth <> fnd_api.g_miss_num) ) THEN
747                 x_relations_where := x_relations_where || ' AND level <= '||p_depth;
748              END IF;
749           END IF;
750       END IF;
751 
752 
753 END gen_relations_where;
754 
755 /*--------------------------------------------------------------------------------------------------------------+
756 |                        <-- csi_ii_relationships_h -->                                                         |
757 |full_dump_flag                                                                                                 |
758 |       y             y             y              y              y             y                               |
759 |   c   |----------------------------------------------------------------------------                c          |
760 |   a   |             |             |              |              |             |    | 11-may-01     a          |
761 |   s   01-may-01     03-may-01     05-may-01      07-may-01      09-may-01     10-may-01            s          |
762 |   e                                                                                                e          |
763 |   1    <------------------------------------case 3--------------------------------->               2          |
764 |                                                                                                               |
765 |   case 1: If the passed time stamp (p_time_stamp) falls before the first full_dump_flag(ex:30-apr-01) then    |
766 |           we have to skip the retreived record.                                                               |
767 |   case 2: If the passed time stamp (p_time_stamp) falls after the last transaction_date(ex:12-may-01) then    |
768 |           we have to add the retreived record for display.                                                    |
769 |   case 3: If the passed time stamp (p_time_stamp say 06-may-01)falls in between 01-may-01 and 11-may-01 then  |
770 |           we need to find the maximum of the minimum record where the full_dump_flag='Y' (05-may-01 in        |
771 |           this case) and start building the history record for display.                                       |
772 +---------------------------------------------------------------------------------------------------------------*/
773 
774 PROCEDURE from_to_tran( p_relationship_id    IN  NUMBER,
775                         p_time_stamp         IN  DATE,
776                         from_time_stamp      OUT NOCOPY VARCHAR2,
777                         to_time_stamp        OUT NOCOPY VARCHAR2)
778 IS
779 l_f_date        VARCHAR2(25)    := fnd_api.g_miss_char;
780 l_t_date        VARCHAR2(25)    := fnd_api.g_miss_char;
781 BEGIN
782 
783              SELECT max(min(to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss')))
784              INTO   l_f_date
785              FROM   csi_transactions a,csi_ii_relationships b,csi_ii_relationships_h c
786              WHERE  b.relationship_id   =  c.relationship_id
787              AND    c.transaction_id    =  a.transaction_id
788              AND    c.full_dump_flag    =  'Y'
789              AND    a.transaction_date <=  p_time_stamp
790              AND    c.relationship_id   =  p_relationship_id
791              GROUP BY  to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss');
792              IF l_f_date IS NULL THEN
793              FROM_time_stamp:=NULL;
794              to_time_stamp:=l_t_date;
795              ELSE
796              FROM_time_stamp:=l_f_date;
797              BEGIN
798                   SELECT max(min(to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss')))
799                   INTO   l_t_date
800                   FROM   csi_transactions a,csi_ii_relationships b,csi_ii_relationships_h c
801                   WHERE  b.relationship_id  = c.relationship_id
802                   AND    c.transaction_id   = a.transaction_id
803                   AND    a.transaction_date<= p_time_stamp
804                   AND    c.relationship_id  = p_relationship_id
805                   GROUP BY  to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss');
806 
807                   IF l_t_date IS NULL THEN
808                   to_time_stamp:=NULL;
809                   ELSE
810                   to_time_stamp:=l_t_date;
811                   END IF;
812              END;
813              END IF;
814 END;
815 
816 
817 PROCEDURE get_history( p_rel_rec    IN   csi_datastructures_pub.ii_relationship_rec
818                       ,p_new_rec    OUT NOCOPY  csi_datastructures_pub.ii_relationship_rec
819                       ,p_flag       OUT NOCOPY  VARCHAR2
820                       ,p_time_stamp IN   DATE
821                       )
822 IS
823 CURSOR hist_csr (p_relationship_id    IN NUMBER,
824                  p_f_time_stamp       IN VARCHAR2,
825                  p_t_time_stamp       IN VARCHAR2)
826      IS
827        SELECT  c.old_subject_id
828               ,c.new_subject_id
829               ,c.old_position_reference
830               ,c.new_position_reference
831               ,c.old_active_start_date
832               ,c.new_active_start_date
833               ,c.old_active_end_date
834               ,c.new_active_end_date
835               ,c.old_mandatory_flag
836               ,c.new_mandatory_flag
837               ,c.old_context
838               ,c.new_context
839               ,c.old_attribute1
840               ,c.new_attribute1
841               ,c.old_attribute2
842               ,c.new_attribute2
843               ,c.old_attribute3
844               ,c.new_attribute3
845               ,c.old_attribute4
846               ,c.new_attribute4
847               ,c.old_attribute5
848               ,c.new_attribute5
849               ,c.old_attribute6
850               ,c.new_attribute6
851               ,c.old_attribute7
852               ,c.new_attribute7
853               ,c.old_attribute8
854               ,c.new_attribute8
855               ,c.old_attribute9
856               ,c.new_attribute9
857               ,c.old_attribute10
858               ,c.new_attribute10
859               ,c.old_attribute11
860               ,c.new_attribute11
861               ,c.old_attribute12
862               ,c.new_attribute12
863               ,c.old_attribute13
864               ,c.new_attribute13
865               ,c.old_attribute14
866               ,c.new_attribute14
867               ,c.old_attribute15
868               ,c.new_attribute15
869               ,c.full_dump_flag
870        FROM   csi_transactions a,csi_ii_relationships b,csi_ii_relationships_h c
871        WHERE  b.relationship_id = c.relationship_id
872        AND    c.transaction_id  = a.transaction_id
873        AND    c.relationship_id = p_relationship_id
874        -- AND    a.transaction_date BETWEEN fnd_date.canonical_to_date(p_f_time_stamp) --to_date(p_f_time_stamp,'dd-mon-rr hh24:mi:ss')
875          --                         AND     fnd_date.canonical_to_date(p_t_time_stamp) --to_date(p_t_time_stamp,'dd-mon-rr hh24:mi:ss')
876        AND    a.transaction_date BETWEEN to_date(p_f_time_stamp,'dd/mm/rr hh24:mi:ss')
877                                  AND     to_date(p_t_time_stamp,'dd/mm/rr hh24:mi:ss')
878        ORDER BY to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss') ;
879 
880 l_f_time_stamp      VARCHAR2(25)    :=fnd_api.g_miss_char;
881 l_t_time_stamp      VARCHAR2(25)    :=fnd_api.g_miss_char;
882 l_to_date           VARCHAR2(25);
883 BEGIN
884       FROM_to_tran(p_relationship_id     =>  p_rel_rec.relationship_id,
885                    p_time_stamp          =>  p_time_stamp,
886                    FROM_time_stamp       =>  l_f_time_stamp,
887                    to_time_stamp         =>  l_t_time_stamp);
888 
889      SELECT max(to_char(a.transaction_date,'dd-mon-rr hh24:mi:ss'))
890      INTO   l_to_date
891      FROM   csi_transactions a,csi_ii_relationships_h b
892      WHERE  a.transaction_id=b.transaction_id
893      AND    b.relationship_id=p_rel_rec.relationship_id;
894 
895        -- IF ( (l_f_time_stamp IS NOT NULL) AND (p_time_stamp>fnd_date.canonical_to_date(l_to_date) ) )-- to_date(l_to_date,'dd-mon-rr hh24:mi:ss')) ) THEN
896        IF ( (l_f_time_stamp IS NOT NULL) AND (p_time_stamp > to_date(l_to_date,'dd/mm/rr hh24:mi:ss')) )THEN
897       -- +-------------------------------------------------------------------------------------------+
898       -- | we have entered into case 2 which we just add the retreived record for display            |
899       -- +-------------------------------------------------------------------------------------------+
900 
901 
902             p_new_rec := p_rel_rec;
903             p_flag   := 'ADD';
904        ELSIF (l_f_time_stamp IS NULL) THEN
905        -- +-------------------------------------------------------------------------------------------+
906        -- | we have entered into case 1 which we have to skip the record.                             |
907        -- +-------------------------------------------------------------------------------------------+
908 
909            p_flag   := 'SKIP';
910        ELSE
911        -- +-------------------------------------------------------------------------------------------+
912        -- |we have entered into case 3 where we have to compare the record and return flag with 'add'.|
913        -- +-------------------------------------------------------------------------------------------+
914 
915           FOR get_csr IN hist_csr(p_rel_rec.relationship_id,l_f_time_stamp,l_t_time_stamp) LOOP
916 
917              p_new_rec.relationship_id:=p_rel_rec.relationship_id;
918              p_new_rec.relationship_type_code:=p_rel_rec.relationship_type_code;
919              p_new_rec.object_id:=p_rel_rec.object_id;
920              p_new_rec.display_order:=p_rel_rec.display_order;
921 
922              IF get_csr.new_subject_id IS NOT NULL THEN
923                 p_new_rec.subject_id := get_csr.new_subject_id;
924              ELSIF get_csr.full_dump_flag='Y' THEN
925                 p_new_rec.subject_id := get_csr.old_subject_id;
926              END IF;
927 
928              IF get_csr.new_position_reference IS NOT NULL THEN
929                 p_new_rec.position_reference := get_csr.new_position_reference;
930              ELSIF get_csr.full_dump_flag='Y' THEN
931                 p_new_rec.position_reference := get_csr.old_position_reference;
932              END IF;
933 
934              IF get_csr.new_active_start_date IS NOT NULL THEN
935                 p_new_rec.active_start_date := get_csr.new_active_start_date;
936              ELSIF get_csr.full_dump_flag='Y' THEN
937                 p_new_rec.active_start_date := get_csr.old_active_start_date;
938              END IF;
939 
940              IF get_csr.new_active_end_date IS NOT NULL THEN
941                 p_new_rec.active_end_date := get_csr.new_active_end_date;
942              ELSIF get_csr.full_dump_flag='Y' THEN
943                 p_new_rec.active_end_date := get_csr.old_active_end_date;
944              END IF;
945 
946              IF get_csr.new_mandatory_flag IS NOT NULL THEN
947                 p_new_rec.mandatory_flag := get_csr.new_mandatory_flag;
948              ELSIF get_csr.full_dump_flag='Y' THEN
949                 p_new_rec.mandatory_flag := get_csr.old_mandatory_flag;
950              END IF;
951 
952              IF get_csr.new_context IS NOT NULL THEN
953                 p_new_rec.context := get_csr.new_context;
954              ELSIF get_csr.full_dump_flag='Y' THEN
955                 p_new_rec.context := get_csr.old_context;
956              END IF;
957 
958              IF get_csr.new_attribute1 IS NOT NULL THEN
959                 p_new_rec.attribute1 := get_csr.new_attribute1;
960              ELSIF get_csr.full_dump_flag='Y' THEN
961                 p_new_rec.attribute1 := get_csr.old_attribute1;
962              END IF;
963 
964              IF get_csr.new_attribute2 IS NOT NULL THEN
965                 p_new_rec.attribute2 := get_csr.new_attribute2;
966              ELSIF get_csr.full_dump_flag='Y' THEN
967                 p_new_rec.attribute2 := get_csr.old_attribute2;
968              END IF;
969 
970              IF get_csr.new_attribute3 IS NOT NULL THEN
971                 p_new_rec.attribute3 := get_csr.new_attribute3;
972              ELSIF get_csr.full_dump_flag='Y' THEN
973                 p_new_rec.attribute3 := get_csr.old_attribute3;
974              END IF;
975 
976              IF get_csr.new_attribute4 IS NOT NULL THEN
977                 p_new_rec.attribute4 := get_csr.new_attribute4;
978              ELSIF get_csr.full_dump_flag='Y' THEN
979                 p_new_rec.attribute4 := get_csr.old_attribute4;
980              END IF;
981 
982              IF get_csr.new_attribute5 IS NOT NULL THEN
983                 p_new_rec.attribute5 := get_csr.new_attribute5;
984              ELSIF get_csr.full_dump_flag='Y' THEN
985                 p_new_rec.attribute5 := get_csr.old_attribute5;
986              END IF;
987 
988              IF get_csr.new_attribute6 IS NOT NULL THEN
989                 p_new_rec.attribute6 := get_csr.new_attribute6;
990              ELSIF get_csr.full_dump_flag='Y' THEN
991                 p_new_rec.attribute6 := get_csr.old_attribute6;
992              END IF;
993 
994              IF get_csr.new_attribute7 IS NOT NULL THEN
995                 p_new_rec.attribute7 := get_csr.new_attribute7;
996              ELSIF get_csr.full_dump_flag='Y' THEN
997                 p_new_rec.attribute7 := get_csr.old_attribute7;
998              END IF;
999 
1000              IF get_csr.new_attribute8 IS NOT NULL THEN
1001                 p_new_rec.attribute8 := get_csr.new_attribute8;
1002              ELSIF get_csr.full_dump_flag='Y' THEN
1003                 p_new_rec.attribute8 := get_csr.old_attribute8;
1004              END IF;
1005 
1006              IF  get_csr.new_attribute9 IS NOT NULL THEN
1007                 p_new_rec.attribute9 := get_csr.new_attribute9;
1008              ELSIF get_csr.full_dump_flag='Y' THEN
1009                 p_new_rec.attribute9 := get_csr.old_attribute9;
1010              END IF;
1011 
1012              IF get_csr.new_attribute10 IS NOT NULL THEN
1013                 p_new_rec.attribute10 := get_csr.new_attribute10;
1014              ELSIF get_csr.full_dump_flag='Y' THEN
1015                 p_new_rec.attribute10 := get_csr.old_attribute10;
1016              END IF;
1017 
1018              IF get_csr.new_attribute11 IS NOT NULL THEN
1019                 p_new_rec.attribute11 := get_csr.new_attribute11;
1020              ELSIF get_csr.full_dump_flag='Y' THEN
1021                 p_new_rec.attribute11 := get_csr.old_attribute11;
1022              END IF;
1023 
1024              IF  get_csr.new_attribute12 IS NOT NULL THEN
1025                 p_new_rec.attribute12 := get_csr.new_attribute12;
1026              ELSIF get_csr.full_dump_flag='Y' THEN
1027                 p_new_rec.attribute12 := get_csr.old_attribute12;
1028              END IF;
1029 
1030              IF get_csr.new_attribute13 IS NOT NULL THEN
1031                 p_new_rec.attribute13 := get_csr.new_attribute13;
1032              ELSIF get_csr.full_dump_flag='Y' THEN
1033                 p_new_rec.attribute13 := get_csr.old_attribute13;
1034              END IF;
1035 
1036              IF  get_csr.new_attribute14 IS NOT NULL THEN
1037                 p_new_rec.attribute14 := get_csr.new_attribute14;
1038              ELSIF get_csr.full_dump_flag='Y' THEN
1039                 p_new_rec.attribute14 := get_csr.old_attribute14;
1040              END IF;
1041 
1042              IF  get_csr.new_attribute15 IS NOT NULL THEN
1043                 p_new_rec.attribute15 := get_csr.new_attribute15;
1044              ELSIF get_csr.full_dump_flag='Y' THEN
1045                 p_new_rec.attribute15 := get_csr.old_attribute15;
1046              END IF;
1047 
1048           END LOOP;
1049              IF  p_new_rec.display_order = fnd_api.g_miss_num THEN
1050                  p_new_rec.display_order := NULL;
1051              END IF;
1052 
1053              IF p_new_rec.subject_id = fnd_api.g_miss_num THEN
1054                 p_new_rec.subject_id := NULL;
1055              END IF;
1056 
1057              IF p_new_rec.position_reference = fnd_api.g_miss_char THEN
1058                  p_new_rec.position_reference := NULL;
1059              END IF;
1060 
1061              IF p_new_rec.active_start_date = fnd_api.g_miss_date THEN
1062                  p_new_rec.active_start_date := NULL;
1063              END IF;
1064 
1065              IF p_new_rec.active_end_date = fnd_api.g_miss_date THEN
1066                 p_new_rec.active_end_date := NULL;
1067              END IF;
1068 
1069              IF p_new_rec.mandatory_flag = fnd_api.g_miss_char THEN
1070                 p_new_rec.mandatory_flag := NULL;
1071              END IF;
1072 
1073              IF p_new_rec.context = fnd_api.g_miss_char THEN
1074                 p_new_rec.context := NULL;
1075              END IF;
1076 
1077              IF p_new_rec.attribute1 = fnd_api.g_miss_char THEN
1078                 p_new_rec.attribute1 := NULL;
1079              END IF;
1080 
1081              IF p_new_rec.attribute2 = fnd_api.g_miss_char THEN
1082                 p_new_rec.attribute2 := NULL;
1083              END IF;
1084 
1085              IF p_new_rec.attribute3 = fnd_api.g_miss_char THEN
1086                 p_new_rec.attribute3 := NULL;
1087              END IF;
1088 
1089              IF p_new_rec.attribute4 = fnd_api.g_miss_char THEN
1090                 p_new_rec.attribute4 := NULL;
1091              END IF;
1092 
1093              IF p_new_rec.attribute5 = fnd_api.g_miss_char THEN
1094                 p_new_rec.attribute5 := NULL;
1095              END IF;
1096 
1097              IF p_new_rec.attribute6 = fnd_api.g_miss_char THEN
1098                 p_new_rec.attribute6 := NULL;
1099              END IF;
1100 
1101              IF p_new_rec.attribute7 =fnd_api.g_miss_char THEN
1102                 p_new_rec.attribute7 := NULL;
1103              END IF;
1104 
1105              IF p_new_rec.attribute8 = fnd_api.g_miss_char THEN
1106                 p_new_rec.attribute8 := NULL;
1107              END IF;
1108 
1109              IF p_new_rec.attribute9 = fnd_api.g_miss_char THEN
1110                 p_new_rec.attribute9 := NULL;
1111              END IF;
1112 
1113              IF p_new_rec.attribute10 = fnd_api.g_miss_char THEN
1114                 p_new_rec.attribute10 := NULL;
1115              END IF;
1116 
1117              IF p_new_rec.attribute11 = fnd_api.g_miss_char THEN
1118                 p_new_rec.attribute11 := NULL;
1119              END IF;
1120 
1121              IF p_new_rec.attribute12 = fnd_api.g_miss_char THEN
1122                 p_new_rec.attribute12 := NULL;
1123              END IF;
1124 
1125              IF p_new_rec.attribute13 = fnd_api.g_miss_char THEN
1126                 p_new_rec.attribute13 := NULL;
1127              END IF;
1128 
1129              IF p_new_rec.attribute14 =fnd_api.g_miss_char THEN
1130                 p_new_rec.attribute14 := NULL;
1131              END IF;
1132 
1133              IF p_new_rec.attribute15 = fnd_api.g_miss_char THEN
1134                 p_new_rec.attribute15 := NULL;
1135              END IF;
1136 
1137            p_flag :='ADD';
1138         END IF;
1139 END;
1140 
1141 PROCEDURE Get_Next_Level
1142     (p_object_id                 IN  NUMBER,
1143      p_rel_tbl                   OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl
1144     ) IS
1145     --
1146     CURSOR REL_CUR IS
1147      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1148             active_start_date,active_end_date,display_order,mandatory_flag,context,
1149             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1150             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1151             object_version_number
1152      from CSI_II_RELATIONSHIPS cir
1153      where cir.object_id = p_object_id;
1154      --
1155      l_ctr      NUMBER := 0;
1156   BEGIN
1157      FOR rel in REL_CUR LOOP
1158         l_ctr := l_ctr + 1;
1159         p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1160         p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1161         p_rel_tbl(l_ctr).object_id := rel.object_id;
1162         p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1163         p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1164         p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1165         p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1166         p_rel_tbl(l_ctr).display_order := rel.display_order;
1167         p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1168         p_rel_tbl(l_ctr).context := rel.context;
1169         p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1170         p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1171         p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1172         p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1173         p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1174         p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1175         p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1176         p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1177         p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1178         p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1179         p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1180         p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1181         p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1182         p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1183         p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1184         p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1185      END LOOP;
1186   END Get_Next_Level;
1187 
1188 
1189   PROCEDURE Get_Children
1190     (p_object_id     IN  NUMBER,
1191      p_rel_tbl       OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl
1192     ) IS
1193     --
1194     l_rel_tbl                 csi_datastructures_pub.ii_relationship_tbl;
1195     l_rel_tbl_next_lvl        csi_datastructures_pub.ii_relationship_tbl;
1196     l_rel_tbl_temp            csi_datastructures_pub.ii_relationship_tbl;
1197     l_rel_tbl_final           csi_datastructures_pub.ii_relationship_tbl;
1198     l_next_ind                NUMBER := 0;
1199     l_final_ind               NUMBER := 0;
1200     l_ctr                     NUMBER := 0;
1201     l_found                   NUMBER;
1202   BEGIN
1203      Get_Next_Level
1204        ( p_object_id                 => p_object_id,
1205          p_rel_tbl                   => l_rel_tbl
1206        );
1207 
1208      <<Next_Level>>
1209 
1210      l_rel_tbl_next_lvl.delete;
1211      l_next_ind := 0;
1212      --
1213      IF l_rel_tbl.count > 0 THEN
1214         FOR l_ind IN l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
1215            l_final_ind := l_final_ind + 1;
1216            l_rel_tbl_final(l_final_ind) := l_rel_tbl(l_ind);
1217            /* get the next level using this Subject ID as the parent */
1218            Get_Next_Level
1219              ( p_object_id                 => l_rel_tbl(l_ind).subject_id,
1220                p_rel_tbl                   => l_rel_tbl_temp
1221              );
1222            --
1223            IF l_rel_tbl_temp.count > 0 THEN
1224               FOR l_temp_ind IN l_rel_tbl_temp.FIRST .. l_rel_tbl_temp.LAST LOOP
1225                  IF l_rel_tbl_final.count > 0 THEN
1226                     l_found := 0;
1227                     FOR i IN l_rel_tbl_final.FIRST .. l_rel_tbl_final.LAST LOOP
1228                        IF l_rel_tbl_final(i).object_id = l_rel_tbl_temp(l_temp_ind).object_id THEN
1229                           l_found := 1;
1230                           exit;
1231                        END IF;
1232                     END LOOP;
1233                  END IF;
1234                  IF l_found = 0 THEN
1235                     l_next_ind := l_next_ind + 1;
1236                     l_rel_tbl_next_lvl(l_next_ind) := l_rel_tbl_temp(l_temp_ind);
1237                  END IF;
1238               END LOOP;
1239            END IF;
1240         END LOOP;
1241         --
1242         IF l_rel_tbl_next_lvl.count > 0 THEN
1243            l_rel_tbl.DELETE;
1244            l_rel_tbl := l_rel_tbl_next_lvl;
1245            --
1246            goto Next_Level;
1247         END IF;
1248      END IF;
1249      --
1250      p_rel_tbl := l_rel_tbl_final;
1251      --
1252      -- The output of l_rel_tbl_final will be Breadth first search Order.
1253   END Get_Children;
1254   --
1255   FUNCTION Parent_of
1256      ( p_subject_id      IN  NUMBER,
1257        p_rel_tbl         IN  csi_datastructures_pub.ii_relationship_tbl
1258      ) RETURN NUMBER IS
1259      l_return_value    NUMBER := -9999;
1260   BEGIN
1261      IF p_rel_tbl.count = 0 OR
1262 	p_subject_id IS NULL THEN
1263 	RETURN -9999;
1264      END IF;
1265      FOR i in p_rel_tbl.FIRST ..p_rel_tbl.LAST LOOP
1266 	IF p_rel_tbl(i).subject_id = p_subject_id THEN
1267 	   l_return_value := p_rel_tbl(i).object_id;
1268 	   exit;
1269 	END IF;
1270      END LOOP;
1271      RETURN l_return_value;
1272   END Parent_of;
1273   --
1274   PROCEDURE Get_Next_Level
1275     (p_object_id                 IN  NUMBER,
1276      p_relationship_id           IN  NUMBER,
1277      p_subject_id                IN  NUMBER,
1278      p_rel_tbl                   OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
1279      p_rel_type_code             IN  VARCHAR2,
1280      p_time_stamp                IN  DATE,
1281      p_active_relationship_only  IN  VARCHAR2,
1282      p_active_instances_only     IN  VARCHAR2,
1283      p_config_only               IN  VARCHAR2
1284     ) IS
1285     --
1286     l_active_relationship_only   VARCHAR2(1) := p_active_relationship_only;
1287     l_active_instances_only      VARCHAR2(1) := p_active_instances_only;
1288     --
1289      CURSOR OBJECT_CUR(c_sysdate IN DATE) IS -- Used when Object ID and Rel Type are passed
1290      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1291             active_start_date,active_end_date,display_order,mandatory_flag,context,
1292             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1293             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1294             object_version_number
1295      from CSI_II_RELATIONSHIPS cir
1296      where cir.object_id = p_object_id
1297      and   cir.relationship_type_code = p_rel_type_code
1298      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1299      and   EXISTS (select 'x'
1300                    from CSI_ITEM_INSTANCES csi
1301                    where csi.instance_id = cir.subject_id
1302                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1303      --
1304      CURSOR OBJECT_CUR1(c_sysdate IN DATE) IS -- Used when Object ID and Rel Type are passed
1305      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1306             active_start_date,active_end_date,display_order,mandatory_flag,context,
1307             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1308             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1309             object_version_number
1310      from CSI_II_RELATIONSHIPS cir
1311      where cir.object_id = p_object_id
1312      and   cir.relationship_type_code = p_rel_type_code
1313      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1314      and   EXISTS (select 'x'
1315                    from CSI_ITEM_INSTANCES csi
1316                    where csi.instance_id = cir.subject_id
1317                    and   csi.config_inst_hdr_id is not null
1318                    and   csi.config_inst_item_id is not null
1319                    and   csi.config_inst_rev_num is not null
1320                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1321      --
1322      CURSOR OBJECT_ONLY_CUR(c_sysdate IN DATE) IS -- Used when only Object ID is passed
1323      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1324             active_start_date,active_end_date,display_order,mandatory_flag,context,
1325             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1326             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1327             object_version_number
1328      from CSI_II_RELATIONSHIPS cir
1329      where cir.object_id = p_object_id
1330      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1331      and   EXISTS (select 'x'
1332                    from CSI_ITEM_INSTANCES csi
1333                    where csi.instance_id = cir.subject_id
1334                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1335      --
1336      CURSOR SUBJECT_CUR(c_sysdate IN DATE) IS --Used when subject ID and Rel type are passed
1337      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1338             active_start_date,active_end_date,display_order,mandatory_flag,context,
1339             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1340             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1341             object_version_number
1342      from CSI_II_RELATIONSHIPS cir
1343      where cir.subject_id = p_subject_id
1344      and   cir.relationship_type_code = p_rel_type_code
1345      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1346      and   EXISTS (select 'x'
1347                    from CSI_ITEM_INSTANCES csi
1348                    where csi.instance_id = cir.subject_id
1349                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1350      --
1351      CURSOR REL_ID_CUR(c_sysdate IN DATE) IS -- Used when only relationship_id is passed
1352      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1353             active_start_date,active_end_date,display_order,mandatory_flag,context,
1354             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1355             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1356             object_version_number
1357      from CSI_II_RELATIONSHIPS cir
1358      where cir.relationship_id = p_relationship_id
1359      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1360      and   EXISTS (select 'x'
1361                    from CSI_ITEM_INSTANCES csi
1362                    where csi.instance_id = cir.subject_id
1363                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1364      --
1365      CURSOR OTHER_PARAM_CUR(c_sysdate IN DATE) IS
1366      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
1367             active_start_date,active_end_date,display_order,mandatory_flag,context,
1368             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
1369             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
1370             object_version_number
1371      from CSI_II_RELATIONSHIPS cir
1372      where cir.relationship_id = NVL(p_relationship_id,cir.relationship_id)
1373      and   cir.object_id = NVL(p_object_id,cir.object_id)
1374      and   cir.relationship_type_code = NVL(p_rel_type_code,cir.relationship_type_code)
1375      and   cir.subject_id = NVL(p_subject_id,cir.subject_id)
1376      and   DECODE(l_active_relationship_only,FND_API.G_TRUE,NVL((cir.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate
1377      and   EXISTS (select 'x'
1378                    from CSI_ITEM_INSTANCES csi
1379                    where csi.instance_id = cir.subject_id
1380                    and   DECODE(l_active_instances_only,FND_API.G_TRUE,NVL((csi.active_end_date),c_sysdate+1),c_sysdate+1) > sysdate);
1381      --
1382      l_ctr      NUMBER := 0;
1383      l_sysdate  DATE;
1384      COMP_EXCEP EXCEPTION;
1385   BEGIN
1386      select sysdate
1387      into l_sysdate
1388      from dual;
1389      --
1390      IF p_time_stamp IS NOT NULL THEN
1391         l_active_instances_only := FND_API.G_FALSE;
1392         l_active_relationship_only := FND_API.G_FALSE;
1393      ELSE
1394         l_active_relationship_only  := p_active_relationship_only;
1395         l_active_instances_only     := p_active_instances_only;
1396      END IF;
1397      --
1398     IF p_object_id IS NOT NULL AND
1399 	   p_rel_type_code IS NOT NULL AND
1400 	   p_subject_id IS NULL AND
1401 	   p_relationship_id IS NULL
1402     THEN
1403     IF p_config_only IS NULL OR
1404        p_config_only = fnd_api.g_false
1405     THEN
1406 	FOR rel IN OBJECT_CUR(l_sysdate) LOOP
1407 	   l_ctr := l_ctr + 1;
1408 	   p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1409 	   p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1410 	   p_rel_tbl(l_ctr).object_id := rel.object_id;
1411 	   p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1412 	   p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1413 	   p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1414 	   p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1415 	   p_rel_tbl(l_ctr).display_order := rel.display_order;
1416 	   p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1417 	   p_rel_tbl(l_ctr).context := rel.context;
1418 	   p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1419 	   p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1420 	   p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1421 	   p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1422 	   p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1423 	   p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1424 	   p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1425 	   p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1426 	   p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1427 	   p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1428 	   p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1429 	   p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1430 	   p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1431 	   p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1432 	   p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1433 	   p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1434 	   --
1435 	   Begin
1436 	      select 'Y'
1437 	      into p_rel_tbl(l_ctr).subject_has_child
1438 	      from CSI_II_RELATIONSHIPS
1439 	      where object_id = rel.subject_id
1440 	      and   relationship_type_code = rel.relationship_type_code
1441 	      and   rownum = 1;
1442 	   Exception
1443 	      when no_data_found then
1444 		 p_rel_tbl(l_ctr).subject_has_child := 'N';
1445 	   End;
1446 	END LOOP;
1447     ELSIF p_config_only IS NOT NULL AND
1448           p_config_only = fnd_api.g_true
1449     THEN
1450 	FOR rel IN OBJECT_CUR1(l_sysdate) LOOP
1451 	   l_ctr := l_ctr + 1;
1452 	   p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1453 	   p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1454 	   p_rel_tbl(l_ctr).object_id := rel.object_id;
1455 	   p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1456 	   p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1457 	   p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1458 	   p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1459 	   p_rel_tbl(l_ctr).display_order := rel.display_order;
1460 	   p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1461 	   p_rel_tbl(l_ctr).context := rel.context;
1462 	   p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1463 	   p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1464 	   p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1465 	   p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1466 	   p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1467 	   p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1468 	   p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1469 	   p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1470 	   p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1471 	   p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1472 	   p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1473 	   p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1474 	   p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1475 	   p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1476 	   p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1477 	   p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1478 	   --
1479 	   Begin
1480 	      select 'Y'
1481 	      into p_rel_tbl(l_ctr).subject_has_child
1482 	      from CSI_II_RELATIONSHIPS
1483 	      where object_id = rel.subject_id
1484 	      and   relationship_type_code = rel.relationship_type_code
1485 	      and   rownum = 1;
1486 	   Exception
1487 	      when no_data_found then
1488 		 p_rel_tbl(l_ctr).subject_has_child := 'N';
1489 	   End;
1490 	END LOOP;
1491 
1492     END IF;
1493 	RAISE COMP_EXCEP;
1494      END IF;
1495      --
1496      IF p_object_id IS NOT NULL AND
1497 	p_rel_type_code IS NULL AND
1498 	p_subject_id IS NULL AND
1499 	p_relationship_id IS NULL THEN
1500 	FOR rel IN OBJECT_ONLY_CUR(l_sysdate) LOOP
1501 	   l_ctr := l_ctr + 1;
1502 	   p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1503 	   p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1504 	   p_rel_tbl(l_ctr).object_id := rel.object_id;
1505 	   p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1506 	   p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1507 	   p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1508 	   p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1509 	   p_rel_tbl(l_ctr).display_order := rel.display_order;
1510 	   p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1511 	   p_rel_tbl(l_ctr).context := rel.context;
1512 	   p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1513 	   p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1514 	   p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1515 	   p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1516 	   p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1517 	   p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1518 	   p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1519 	   p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1520 	   p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1521 	   p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1522 	   p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1523 	   p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1524 	   p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1525 	   p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1526 	   p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1527 	   p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1528 	   --
1529 	   Begin
1530 	      select 'Y'
1531 	      into p_rel_tbl(l_ctr).subject_has_child
1532 	      from CSI_II_RELATIONSHIPS
1533 	      where object_id = rel.subject_id
1534 	      and   relationship_type_code = rel.relationship_type_code
1535 	      and   rownum = 1;
1536 	   Exception
1537 	      when no_data_found then
1538 		 p_rel_tbl(l_ctr).subject_has_child := 'N';
1539 	   End;
1540 	END LOOP;
1541 	RAISE COMP_EXCEP;
1542      END IF;
1543      --
1544      IF p_subject_id IS NOT NULL AND
1545 	p_rel_type_code IS NOT NULL AND
1546 	p_object_id IS NULL AND
1547 	p_relationship_id IS NULL THEN
1548 	FOR rel IN SUBJECT_CUR(l_sysdate) LOOP
1549 	   l_ctr := l_ctr + 1;
1550 	   p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1551 	   p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1552 	   p_rel_tbl(l_ctr).object_id := rel.object_id;
1553 	   p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1554 	   p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1555 	   p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1556 	   p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1557 	   p_rel_tbl(l_ctr).display_order := rel.display_order;
1558 	   p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1559 	   p_rel_tbl(l_ctr).context := rel.context;
1560 	   p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1561 	   p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1562 	   p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1563 	   p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1564 	   p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1565 	   p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1566 	   p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1567 	   p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1568 	   p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1569 	   p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1570 	   p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1571 	   p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1572 	   p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1573 	   p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1574 	   p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1575 	   p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1576 	   --
1577 	   Begin
1578 	      select 'Y'
1579 	      into p_rel_tbl(l_ctr).subject_has_child
1580 	      from CSI_II_RELATIONSHIPS
1581 	      where object_id = rel.subject_id
1582 	      and   relationship_type_code = rel.relationship_type_code
1583 	      and   rownum = 1;
1584 	   Exception
1585 	      when no_data_found then
1586 		 p_rel_tbl(l_ctr).subject_has_child := 'N';
1587 	   End;
1588 	END LOOP;
1589 	RAISE COMP_EXCEP;
1590      END IF;
1591      --
1592      IF p_relationship_id IS NOT NULL AND
1593         p_rel_type_code IS NULL AND
1594         p_subject_id IS NULL AND
1595         p_object_id IS NULL THEN
1596         FOR rel IN REL_ID_CUR(l_sysdate) LOOP
1597 	   l_ctr := l_ctr + 1;
1598 	   p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1599 	   p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1600 	   p_rel_tbl(l_ctr).object_id := rel.object_id;
1601 	   p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1602 	   p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1603 	   p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1604 	   p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1605 	   p_rel_tbl(l_ctr).display_order := rel.display_order;
1606 	   p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1607 	   p_rel_tbl(l_ctr).context := rel.context;
1608 	   p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1609 	   p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1610 	   p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1611 	   p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1612 	   p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1613 	   p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1614 	   p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1615 	   p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1616 	   p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1617 	   p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1618 	   p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1619 	   p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1620 	   p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1621 	   p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1622 	   p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1623 	   p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1624 	   --
1625 	   Begin
1626 	      select 'Y'
1627 	      into p_rel_tbl(l_ctr).subject_has_child
1628 	      from CSI_II_RELATIONSHIPS
1629 	      where object_id = rel.subject_id
1630 	      and   relationship_type_code = rel.relationship_type_code
1631 	      and   rownum = 1;
1632 	   Exception
1633 	      when no_data_found then
1634 		 p_rel_tbl(l_ctr).subject_has_child := 'N';
1635 	   End;
1636         END LOOP;
1637 	RAISE COMP_EXCEP;
1638      END IF;
1639      -- If other parameters are used then following cursor will be used.
1640      FOR rel in OTHER_PARAM_CUR(l_sysdate) LOOP
1641 	l_ctr := l_ctr + 1;
1642 	p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
1643 	p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
1644 	p_rel_tbl(l_ctr).object_id := rel.object_id;
1645 	p_rel_tbl(l_ctr).subject_id := rel.subject_id;
1646 	p_rel_tbl(l_ctr).position_reference := rel.position_reference;
1647 	p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
1648 	p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
1649 	p_rel_tbl(l_ctr).display_order := rel.display_order;
1650 	p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
1651 	p_rel_tbl(l_ctr).context := rel.context;
1652 	p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
1653 	p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
1654 	p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
1655 	p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
1656 	p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
1657 	p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
1658 	p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
1659 	p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
1660 	p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
1661 	p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
1662 	p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
1663 	p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
1664 	p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
1665 	p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
1666 	p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
1667 	p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
1668 	--
1669 	Begin
1670 	   select 'Y'
1671 	   into p_rel_tbl(l_ctr).subject_has_child
1672 	   from CSI_II_RELATIONSHIPS
1673 	   where object_id = rel.subject_id
1674 	   and   relationship_type_code = rel.relationship_type_code
1675 	   and   rownum = 1;
1676 	Exception
1677 	   when no_data_found then
1678 	      p_rel_tbl(l_ctr).subject_has_child := 'N';
1679 	End;
1680      END LOOP;
1681   EXCEPTION
1682      WHEN COMP_EXCEP THEN
1683         NULL;
1684   END Get_Next_Level;
1685   --
1686 
1687   PROCEDURE DFS
1688     (p_relationship_rec    IN  csi_datastructures_pub.ii_relationship_rec,
1689      p_active_relationship_only  IN  VARCHAR2,
1690      p_active_instances_only     IN  VARCHAR2,
1691      p_config_only               IN  VARCHAR2,
1692      p_curr_level                IN NUMBER := 1, --bug 10321217
1693      p_depth_req                 IN NUMBER := 9999 --bug 10321217
1694     ) IS
1695     l_rel_tbl_temp            csi_datastructures_pub.ii_relationship_tbl;
1696     l_time_stamp              DATE;
1697   BEGIN
1698         p_glbl_ctr := p_glbl_ctr + 1;
1699 
1700         IF p_curr_level > p_depth_req THEN   --bug 10321217
1701           RETURN;
1702         END IF;
1703 
1704         p_rel_glbl_tbl(p_glbl_ctr).relationship_id := p_relationship_rec.relationship_id;
1705         p_rel_glbl_tbl(p_glbl_ctr).relationship_type_code := p_relationship_rec.relationship_type_code;
1706         p_rel_glbl_tbl(p_glbl_ctr).object_id := p_relationship_rec.object_id;
1707         p_rel_glbl_tbl(p_glbl_ctr).subject_id := p_relationship_rec.subject_id;
1708         p_rel_glbl_tbl(p_glbl_ctr).position_reference := p_relationship_rec.position_reference;
1709         p_rel_glbl_tbl(p_glbl_ctr).active_start_date := p_relationship_rec.active_start_date;
1710         p_rel_glbl_tbl(p_glbl_ctr).active_end_date := p_relationship_rec.active_end_date;
1711         p_rel_glbl_tbl(p_glbl_ctr).display_order := p_relationship_rec.display_order;
1712         p_rel_glbl_tbl(p_glbl_ctr).mandatory_flag := p_relationship_rec.mandatory_flag;
1713         p_rel_glbl_tbl(p_glbl_ctr).context := p_relationship_rec.context;
1714         p_rel_glbl_tbl(p_glbl_ctr).attribute1 := p_relationship_rec.attribute1;
1715 	p_rel_glbl_tbl(p_glbl_ctr).attribute2 := p_relationship_rec.attribute2;
1716 	p_rel_glbl_tbl(p_glbl_ctr).attribute3 := p_relationship_rec.attribute3;
1717 	p_rel_glbl_tbl(p_glbl_ctr).attribute4 := p_relationship_rec.attribute4;
1718 	p_rel_glbl_tbl(p_glbl_ctr).attribute5 := p_relationship_rec.attribute5;
1719 	p_rel_glbl_tbl(p_glbl_ctr).attribute6 := p_relationship_rec.attribute6;
1720 	p_rel_glbl_tbl(p_glbl_ctr).attribute7 := p_relationship_rec.attribute7;
1721 	p_rel_glbl_tbl(p_glbl_ctr).attribute8 := p_relationship_rec.attribute8;
1722 	p_rel_glbl_tbl(p_glbl_ctr).attribute9 := p_relationship_rec.attribute9;
1723 	p_rel_glbl_tbl(p_glbl_ctr).attribute10 := p_relationship_rec.attribute10;
1724 	p_rel_glbl_tbl(p_glbl_ctr).attribute11 := p_relationship_rec.attribute11;
1725 	p_rel_glbl_tbl(p_glbl_ctr).attribute12 := p_relationship_rec.attribute12;
1726 	p_rel_glbl_tbl(p_glbl_ctr).attribute13 := p_relationship_rec.attribute13;
1727 	p_rel_glbl_tbl(p_glbl_ctr).attribute14 := p_relationship_rec.attribute14;
1728 	p_rel_glbl_tbl(p_glbl_ctr).attribute15 := p_relationship_rec.attribute15;
1729         p_rel_glbl_tbl(p_glbl_ctr).object_version_number := p_relationship_rec.object_version_number;
1730         --bug 10321217
1731         p_rel_glbl_tbl(p_glbl_ctr).subject_has_child := p_relationship_rec.subject_has_child;
1732         p_ii_rel_level_glbl_tbl(p_glbl_ctr).relationship_id := p_relationship_rec.relationship_id;
1733         p_ii_rel_level_glbl_tbl(p_glbl_ctr).current_level := p_curr_level;
1734 
1735       IF ( p_relationship_rec.subject_has_child = 'Y') THEN  --bug 10321217
1736 
1737         Get_Next_Level
1738 		( p_object_id                 => p_relationship_rec.subject_id,
1739 		  p_relationship_id           => null,
1740 		  p_subject_id                => null,
1741 		  p_rel_tbl                   => l_rel_tbl_temp,
1742 		  p_rel_type_code             => p_relationship_rec.relationship_type_code,
1743 		  p_time_stamp                => l_time_stamp,
1744 		  p_active_relationship_only  => p_active_relationship_only,
1745 		  p_active_instances_only     => p_active_instances_only,
1746                   p_config_only               => p_config_only
1747 		);
1748         IF l_rel_tbl_temp.count > 0 THEN
1749 		 FOR l_temp_ind IN l_rel_tbl_temp.FIRST .. l_rel_tbl_temp.LAST LOOP
1750                     DFS(l_rel_tbl_temp(l_temp_ind), p_active_relationship_only, p_active_instances_only, p_config_only);
1751                  END LOOP;
1752         END IF;
1753       END IF;
1754   EXCEPTION
1755         WHEN OTHERS THEN
1756         NULL;
1757   END DFS;
1758 
1759 
1760   PROCEDURE Get_Children
1761     (p_relationship_query_rec    IN  csi_datastructures_pub.relationship_query_rec,
1762      p_rel_tbl                   OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
1763      p_depth                     IN  NUMBER,
1764      p_active_relationship_only  IN  VARCHAR2,
1765      p_active_instances_only     IN  VARCHAR2,
1766      p_config_only               IN  VARCHAR2, -- if true will retrieve instances with config keys
1767      p_time_stamp                IN  DATE,
1768      p_get_dfs                   IN  VARCHAR2,
1769      p_ii_relationship_level_tbl OUT NOCOPY csi_ii_relationships_pvt.ii_relationship_level_tbl,
1770      x_return_status             OUT NOCOPY VARCHAR2,
1771      x_msg_count                 OUT NOCOPY NUMBER,
1772      x_msg_data                  OUT NOCOPY VARCHAR2
1773     ) IS
1774     --
1775     l_api_name                CONSTANT VARCHAR2(30)    := 'get_children';
1776     l_api_version             CONSTANT NUMBER          := 1.0;
1777     l_rel_tbl                 csi_datastructures_pub.ii_relationship_tbl;
1778     l_rel_tbl_next_lvl        csi_datastructures_pub.ii_relationship_tbl;
1779     l_rel_tbl_temp            csi_datastructures_pub.ii_relationship_tbl;
1780     l_rel_tbl_final           csi_datastructures_pub.ii_relationship_tbl;
1781     l_next_ind                NUMBER := 0;
1782     l_final_ind               NUMBER := 0;
1783     l_ctr                     NUMBER := 0;
1784     l_temp_id                 NUMBER;
1785     l_prev_id                 NUMBER;
1786     l_found                   NUMBER;
1787     l_rel_found               NUMBER;
1788     l_depth                   NUMBER := p_depth;
1789     l_level                   NUMBER := 1;
1790     l_rel_type_code           VARCHAR2(30);
1791     l_object_id               NUMBER;
1792     l_subject_id              NUMBER;
1793     l_relationship_id         NUMBER;
1794     l_time_stamp              DATE;
1795     l_max_count               NUMBER;
1796   BEGIN
1797      x_return_status := FND_API.G_RET_STS_SUCCESS;
1798      --savepoint Get_Children;
1799      IF l_depth IS NULL OR
1800         l_depth = FND_API.G_MISS_NUM OR
1801         l_depth <= 0 THEN
1802         l_depth := 9999999;
1803      END IF;
1804      --
1805      IF p_relationship_query_rec.object_id IS NULL OR
1806         p_relationship_query_rec.object_id = FND_API.G_MISS_NUM THEN
1807         l_object_id := null;
1808      ELSE
1809         l_object_id := p_relationship_query_rec.object_id;
1810      END IF;
1811      --
1812      IF p_relationship_query_rec.subject_id IS NULL OR
1813         p_relationship_query_rec.subject_id = FND_API.G_MISS_NUM THEN
1814         l_subject_id := null;
1815      ELSE
1816         l_subject_id := p_relationship_query_rec.subject_id;
1817      END IF;
1818      --
1819      IF p_relationship_query_rec.relationship_id IS NULL OR
1820         p_relationship_query_rec.relationship_id = FND_API.G_MISS_NUM THEN
1821         l_relationship_id := null;
1822      ELSE
1823         l_relationship_id := p_relationship_query_rec.relationship_id;
1824      END IF;
1825      --
1826      IF p_relationship_query_rec.relationship_type_code IS NULL OR
1827         p_relationship_query_rec.relationship_type_code = FND_API.G_MISS_CHAR THEN
1828         l_rel_type_code := null;
1829      ELSE
1830         l_rel_type_code := p_relationship_query_rec.relationship_type_code;
1831      END IF;
1832      --
1833      IF l_object_id IS NULL AND
1834         l_subject_id IS NULL AND
1835         l_relationship_id IS NULL AND
1836         l_rel_type_code IS NULL THEN
1837         fnd_message.set_name('CSI', 'CSI_INVALID_PARAMETERS');
1838         fnd_msg_pub.add;
1839         x_return_status := fnd_api.g_ret_sts_error;
1840         RAISE fnd_api.g_exc_error;
1841      END IF;
1842      --
1843      IF p_time_stamp IS NULL OR
1844         p_time_stamp = FND_API.G_MISS_DATE THEN
1845         l_time_stamp := NULL;
1846      ELSE
1847         l_time_stamp := p_time_stamp;
1848      END IF;
1849      --
1850      /* Added IF lakmohan Bug 7503134 ; forward port by Derek Chang*/
1851      IF p_get_dfs = FND_API.G_FALSE OR
1852         l_object_id IS NULL AND
1853         l_subject_id IS NOT NULL AND
1854         l_relationship_id IS NULL THEN
1855      Get_Next_Level
1856        ( p_object_id                 => l_object_id,
1857          p_relationship_id           => l_relationship_id,
1858          p_subject_id                => l_subject_id,
1859          p_rel_tbl                   => l_rel_tbl,
1860          p_rel_type_code             => l_rel_type_code,
1861          p_time_stamp                => l_time_stamp,
1862          p_active_relationship_only  => p_active_relationship_only,
1863          p_active_instances_only     => p_active_instances_only,
1864          p_config_only               => p_config_only
1865        );
1866 
1867      <<Next_Level>>
1868 
1869      l_rel_tbl_next_lvl.delete;
1870      l_next_ind := 0;
1871      --
1872      IF l_rel_tbl.count > 0 THEN
1873 	FOR l_ind IN l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
1874 	   l_final_ind := l_final_ind + 1;
1875 	   l_rel_tbl_final(l_final_ind) := l_rel_tbl(l_ind);
1876            p_ii_relationship_level_tbl(l_final_ind).relationship_id := l_rel_tbl(l_ind).relationship_id;
1877            p_ii_relationship_level_tbl(l_final_ind).current_level := l_level;
1878 	   /* get the next level using this line ID as the parent */
1879            IF l_object_id IS NOT NULL AND -- Need to Explode if Object ID alone is passed
1880               l_subject_id IS NULL AND
1881               l_relationship_id IS NULL THEN
1882 	      Get_Next_Level
1883 		( p_object_id                 => l_rel_tbl(l_ind).subject_id,
1884 		  p_relationship_id           => null,
1885 		  p_subject_id                => null,
1886 		  p_rel_tbl                   => l_rel_tbl_temp,
1887 		  p_rel_type_code             => l_rel_type_code,
1888 		  p_time_stamp                => l_time_stamp,
1889 		  p_active_relationship_only  => p_active_relationship_only,
1890 		  p_active_instances_only     => p_active_instances_only,
1891           p_config_only               => p_config_only
1892 		);
1893 	      --
1894 	      IF l_rel_tbl_temp.count > 0 THEN
1895 		 FOR l_temp_ind IN l_rel_tbl_temp.FIRST .. l_rel_tbl_temp.LAST LOOP
1896 		    IF l_rel_tbl_final.count > 0 THEN
1897 		       l_found := 0;
1898 		       FOR i IN l_rel_tbl_final.FIRST .. l_rel_tbl_final.LAST LOOP
1899 			  IF l_rel_tbl_final(i).object_id = l_rel_tbl_temp(l_temp_ind).object_id THEN
1900 			     l_found := 1;
1901 			     exit;
1902 			  END IF;
1903 		       END LOOP;
1904 		    END IF;
1905 		    IF l_found = 0 THEN
1906 		       l_next_ind := l_next_ind + 1;
1907 		       l_rel_tbl_next_lvl(l_next_ind) := l_rel_tbl_temp(l_temp_ind);
1908 		    END IF;
1909 		 END LOOP;
1910 	      END IF;
1911            END IF; -- Object_id check
1912 	END LOOP;
1913 	--
1914 	IF l_rel_tbl_next_lvl.count > 0 THEN
1915 	   l_rel_tbl.DELETE;
1916 	   l_rel_tbl := l_rel_tbl_next_lvl;
1917 	   --
1918            l_level := l_level + 1;
1919            IF l_level <= l_depth THEN
1920 	      goto Next_Level;
1921            END IF;
1922 	END IF;
1923      END IF;
1924      --
1925      p_rel_tbl := l_rel_tbl_final;
1926      --
1927 
1928 	-- The output of l_rel_tbl_final will be Breadth first search Order.
1929 	-- This needs to be converted to depth-first-search order.
1930 	-- The following LOOP does this.
1931 	ELSIF nvl(p_get_dfs,FND_API.G_TRUE) = FND_API.G_TRUE AND  -- Need to Sort if Object_id alone is passed
1932         l_object_id IS NOT NULL AND
1933         l_subject_id IS NULL AND
1934         l_relationship_id IS NULL THEN
1935 	p_rel_tbl.DELETE;
1936 	l_ctr := 0;
1937 	--
1938 	-- The output of l_rel_tbl_final will be Breadth first search Order.
1939 	-- This needs to be converted to depth-first-search order.
1940 	-- The following LOOP does this.
1941 	--
1942 
1943         Get_Next_Level
1944        ( p_object_id                 => l_object_id,
1945          p_relationship_id           => l_relationship_id,
1946          p_subject_id                => l_subject_id,
1947          p_rel_tbl                   => l_rel_tbl,
1948          p_rel_type_code             => l_rel_type_code,
1949          p_time_stamp                => l_time_stamp,
1950          p_active_relationship_only  => p_active_relationship_only,
1951          p_active_instances_only     => p_active_instances_only,
1952          p_config_only               => p_config_only
1953        );
1954        IF l_rel_tbl.COUNT > 0 THEN
1955            FOR l_ind IN l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
1956                DFS (p_relationship_rec => l_rel_tbl(l_ind),
1957                     p_active_relationship_only  => p_active_relationship_only,
1958                     p_active_instances_only     => p_active_instances_only,
1959                     p_config_only => p_config_only,
1960                     p_curr_level => 1, --bug 10321217
1961                     p_depth_req => p_depth   --bug 10321217
1962 		    );
1963            END LOOP;
1964        END IF;
1965 
1966 /*	IF l_rel_tbl_final.count > 0 THEN
1967            l_max_count := l_rel_tbl_final.count;
1968 	   l_ctr := l_ctr + 1;
1969 	   p_rel_tbl(l_ctr) := l_rel_tbl_final(1);
1970 	   l_temp_id := l_rel_tbl_final(1).subject_id;
1971            l_rel_tbl_final.DELETE(1);
1972 	   LOOP
1973 	      IF p_rel_tbl.count = l_max_count OR
1974                  l_rel_tbl_final.count = 0 THEN
1975 		 exit;
1976 	      END IF;
1977 	      FOR rel IN l_rel_tbl_final.FIRST .. l_rel_tbl_final.LAST LOOP
1978 		 l_found := 0;
1979                  IF l_rel_tbl_final.EXISTS(rel) THEN
1980 		    IF l_rel_tbl_final(rel).object_id = l_temp_id THEN
1981 		       l_found := 1;
1982 		       l_ctr := l_ctr + 1;
1983 		       p_rel_tbl(l_ctr) := l_rel_tbl_final(rel);
1984 		       l_temp_id := l_rel_tbl_final(rel).subject_id;
1985                        l_rel_tbl_final.DELETE(rel);
1986 		       exit;
1987 		    END IF;
1988                  END IF;
1989 	      END LOOP;
1990 	      IF l_found = 0 THEN -- If No more component then go back
1991 		 -- To get the parent, do not pass l_rel_tbl_final. This may go in an infinite loop.
1992 		 -- Always better to pass the p_rel_tbl which is for sure has the relationship created.
1993 		 -- This is because, same subject can exist under two parents with different relationship_type.
1994 		 l_prev_id := l_temp_id;
1995 		 l_temp_id := Parent_of(p_subject_id    => l_temp_id,
1996 					p_rel_tbl       => p_rel_tbl);
1997 	      END IF;
1998 	   END LOOP;
1999 	END IF; */
2000 
2001         p_rel_tbl := p_rel_glbl_tbl;
2002 
2003         p_ii_relationship_level_tbl :=  p_ii_rel_level_glbl_tbl; --bug 10321217
2004 
2005         p_rel_glbl_tbl.DELETE; --bug 10321217
2006         p_ii_rel_level_glbl_tbl.DELETE; --bug 10321217
2007         p_glbl_ctr := 0; --bug 10321217
2008 		--p_rel_glbl_tbl.delete;
2009      END IF; -- p_get_dfs check
2010   EXCEPTION
2011      WHEN fnd_api.g_exc_error THEN
2012 	 --  ROLLBACK TO Get_Children;
2013 	   x_return_status := fnd_api.g_ret_sts_error ;
2014 	   fnd_msg_pub.count_AND_get
2015 		    (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
2016            p_count => x_msg_count ,
2017 		     p_data => x_msg_data
2018 		    );
2019 
2020       WHEN fnd_api.g_exc_unexpected_error THEN
2021 	  --  ROLLBACK TO Get_Children;
2022 	    x_return_status := fnd_api.g_ret_sts_unexp_error ;
2023 	    fnd_msg_pub.count_AND_get
2024 		     (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
2025            p_count => x_msg_count ,
2026 		      p_data => x_msg_data
2027 		     );
2028 
2029       WHEN OTHERS THEN
2030 	  --  ROLLBACK TO Get_Children;
2031 	    x_return_status := fnd_api.g_ret_sts_unexp_error ;
2032 	      IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2033 		     fnd_msg_pub.add_exc_msg('csi_relationships_pvt' ,l_api_name);
2034 	      END IF;
2035 	    fnd_msg_pub.count_AND_get
2036 		     (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
2037            p_count => x_msg_count ,
2038 		      p_data => x_msg_data
2039 		     );
2040   END Get_Children;
2041   --
2042   PROCEDURE Get_Top_Most_Parent
2043      ( p_subject_id      IN  NUMBER,
2044        p_rel_type_code   IN  VARCHAR2,
2045        p_object_id       OUT NOCOPY NUMBER
2046      ) IS
2047      l_object_id       NUMBER;
2048   BEGIN
2049      IF p_rel_type_code IS NULL OR
2050 	p_subject_id IS NULL THEN
2051         l_object_id := -9999;
2052         p_object_id := l_object_id;
2053 	RETURN;
2054      END IF;
2055      l_object_id := p_subject_id;
2056      Begin
2057         select object_id
2058         into l_object_id
2059         from CSI_II_RELATIONSHIPS
2060         where subject_id = p_subject_id
2061         and   relationship_type_code = p_rel_type_code
2062         and   ((active_end_date is null) or (active_end_date > sysdate));
2063      Exception
2064         when no_data_found then
2065            p_object_id := p_subject_id;
2066            RETURN;
2067      End;
2068      -- Call Recursively for prior parent
2069      Get_Top_Most_Parent
2070           (p_subject_id      => l_object_id,
2071            p_rel_type_code   => p_rel_type_code,
2072            p_object_id       => p_object_id
2073           );
2074   END Get_Top_Most_Parent;
2075 
2076   PROCEDURE Get_Immediate_Parents
2077     ( p_subject_id       IN NUMBER,
2078       p_rel_type_code    IN VARCHAR2,
2079       p_rel_tbl          OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl
2080     ) IS
2081      l_ctr         NUMBER := 0;
2082      l_object_id   NUMBER;
2083      l_subject_id  NUMBER;
2084      l_exists      VARCHAR2(1);
2085   BEGIN
2086      IF p_subject_id IS NULL OR
2087         p_subject_id = FND_API.G_MISS_NUM THEN
2088         Return;
2089      END IF;
2090      --
2091      l_subject_id := p_subject_id;
2092      --
2093      LOOP
2094         Begin
2095            select object_id
2096            into l_object_id
2097            from CSI_II_RELATIONSHIPS
2098            where subject_id = l_subject_id
2099            and   relationship_type_code = p_rel_type_code
2100            and   ((active_end_date is null) or (active_end_date > sysdate));
2101            --
2102            l_ctr := l_ctr + 1;
2103            p_rel_tbl(l_ctr).subject_id := l_subject_id;
2104            p_rel_tbl(l_ctr).object_id := l_object_id;
2105            p_rel_tbl(l_ctr).relationship_type_code := p_rel_type_code;
2106            --
2107            l_exists := 'N';
2108            IF p_rel_tbl.count > 0 THEN
2109               FOR j in p_rel_tbl.FIRST .. p_rel_tbl.LAST Loop
2110                  IF l_object_id = p_rel_tbl(j).subject_id THEN
2111                     l_exists := 'Y';
2112                     exit;
2113                  END IF;
2114               End Loop;
2115            END IF;
2116            --
2117            IF l_exists = 'Y' THEN
2118               exit;
2119            END IF;
2120            --
2121            l_subject_id := l_object_id;
2122         Exception
2123            when no_data_found then
2124               exit;
2125         End;
2126      END LOOP;
2127   END Get_Immediate_Parents;
2128   --
2129 
2130 
2131 PROCEDURE get_relationships
2132  (
2133      p_api_version               IN  NUMBER,
2134      p_commit                    IN  VARCHAR2,
2135      p_init_msg_list             IN  VARCHAR2,
2136      p_validation_level          IN  NUMBER,
2137      p_relationship_query_rec    IN  csi_datastructures_pub.relationship_query_rec,
2138      p_depth                     IN  NUMBER,
2139      p_time_stamp                IN  DATE,
2140      p_active_relationship_only  IN  VARCHAR2,
2141      p_recursive_flag            IN  VARCHAR2,
2142      x_relationship_tbl          OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
2143      x_return_status             OUT NOCOPY VARCHAR2,
2144      x_msg_count                 OUT NOCOPY NUMBER,
2145      x_msg_data                  OUT NOCOPY VARCHAR2
2146  )
2147  IS
2148  CURSOR config_csr (instance_id NUMBER) IS
2149         SELECT * FROM csi_ii_relationships
2150         WHERE object_id=instance_id;
2151 
2152 l_api_name                 CONSTANT VARCHAR2(30)    := 'get_relationships';
2153 l_api_version              CONSTANT NUMBER          := 1.0;
2154 l_return_status_full                VARCHAR2(1);
2155 l_access_flag                       VARCHAR2(1);
2156 i                                   NUMBER          := 1;
2157 l_instance_id                       NUMBER;
2158 l_returned_rec_count                NUMBER          := 0;
2159 l_rel_rec                           csi_datastructures_pub.ii_relationship_rec;
2160 l_debug_level                       NUMBER;
2161 l_new_rec                           csi_datastructures_pub.ii_relationship_rec;
2162 l_flag                              VARCHAR2(4);
2163 l_active_relationship_only          VARCHAR2(1)     := p_active_relationship_only;
2164 l_depth                             NUMBER;
2165 l_found                             VARCHAR2(1);  -- Added by sguthiva for bug 2373109
2166 xc_relationship_tbl                 csi_datastructures_pub.ii_relationship_tbl ;
2167 l_relationship_tbl                  csi_datastructures_pub.ii_relationship_tbl ;
2168 l_ins_child_tbl                     csi_datastructures_pub.ii_relationship_tbl ;
2169 l_rel_count                         NUMBER := 0;
2170 l_temp_relationship_tbl             csi_datastructures_pub.ii_relationship_tbl;
2171 l_rel_tbl                           csi_datastructures_pub.ii_relationship_tbl;
2172 l_ctr                               NUMBER;
2173 l_exists                            VARCHAR2(1);
2174 l_exists_flag                       VARCHAR2(1);
2175 l_msg_index                         NUMBER;
2176 l_msg_count                         NUMBER;
2177 l_last_purge_date                   DATE;
2178 TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
2179 l_exp_inst_tbl                      NUMLIST;
2180 --
2181 l_instance_rec       CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_REC;
2182 l_party_header_tbl   CSI_DATASTRUCTURES_PUB.PARTY_HEADER_TBL;
2183 l_account_header_tbl CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_HEADER_TBL;
2184 l_org_header_tbl     CSI_DATASTRUCTURES_PUB.ORG_UNITS_HEADER_TBL;
2185 l_pricing_attrib_tbl CSI_DATASTRUCTURES_PUB.PRICING_ATTRIBS_TBL;
2186 l_ext_attrib_tbl     CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_VALUES_TBL;
2187 l_ext_attrib_def_tbl CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_TBL;
2188 l_asset_header_tbl   CSI_DATASTRUCTURES_PUB.INSTANCE_ASSET_HEADER_TBL;
2189 --
2190 /*
2191 CURSOR CHILD_CUR(p_instance_id IN NUMBER) IS
2192 SELECT subject_id
2193 from csi_ii_relationships
2194 connect by prior subject_id = object_id
2195 start with subject_id = p_instance_id;
2196 */
2197 -- Added for bug 2999353
2198 /*
2199 CURSOR expired_cur (p_object_id IN number) IS
2200 SELECT relationship_id
2201       ,active_end_date
2202 FROM  csi_ii_relationships
2203 WHERE relationship_type_code='COMPONENT-OF'
2204 START WITH object_id = p_object_id
2205 CONNECT BY object_id = PRIOR subject_id;
2206 */
2207 l_fin_count   NUMBER;
2208 l_fin_count1  NUMBER;
2209 l_exp_tbl     csi_datastructures_pub.ii_relationship_tbl;
2210 l_temp_tbl    csi_datastructures_pub.ii_relationship_tbl;
2211 l_exp_act_tbl csi_datastructures_pub.ii_relationship_tbl;
2212 l_found1      BOOLEAN;
2213 l_found2      BOOLEAN;
2214 l_expire      BOOLEAN;
2215 l_exp_count   NUMBER:=0;
2216 l_relationship_query_rec   csi_datastructures_pub.relationship_query_rec;
2217 l_ii_relationship_level_tbl csi_ii_relationships_pvt.ii_relationship_level_tbl;
2218 
2219  BEGIN
2220       -- standard start of api savepoint
2221     --  SAVEPOINT get_relationships_pvt;
2222 
2223       -- standard call to check for call compatibility.
2224       IF NOT fnd_api.compatible_api_call ( l_api_version,
2225                                            p_api_version,
2226                                            l_api_name,
2227                                            g_pkg_name)
2228       THEN
2229           RAISE fnd_api.g_exc_unexpected_error;
2230       END IF;
2231 
2232 
2233       -- initialize message list if p_init_msg_list is set to true.
2234       IF fnd_api.to_boolean( p_init_msg_list )
2235       THEN
2236           fnd_msg_pub.initialize;
2237       END IF;
2238 
2239 
2240 
2241 
2242       -- initialize api return status to success
2243       x_return_status := fnd_api.g_ret_sts_success;
2244 
2245 
2246       l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
2247     IF (l_debug_level > 0) THEN
2248           CSI_gen_utility_pvt.put_line( 'get_relationships');
2249     END IF;
2250 
2251     IF (l_debug_level > 1) THEN
2252              CSI_gen_utility_pvt.put_line(
2253                             p_api_version             ||'-'||
2254                             p_commit                  ||'-'||
2255                             p_init_msg_list           ||'-'||
2256                             p_validation_level        ||'-'||
2257                             p_depth                   ||'_'||
2258                             p_time_stamp              );
2259 
2260          csi_gen_utility_pvt.dump_rel_query_rec(p_relationship_query_rec);
2261     END IF;
2262 
2263 
2264       --
2265       -- API BODY
2266       --
2267       -- ******************************************************************
2268       -- validate environment
2269       -- ******************************************************************
2270 
2271       IF
2272       ( ((p_relationship_query_rec.relationship_id IS NULL)         OR (p_relationship_query_rec.relationship_id = fnd_api.g_miss_num))
2273     AND ((p_relationship_query_rec.relationship_type_code IS NULL)  OR (p_relationship_query_rec.relationship_type_code = fnd_api.g_miss_char))
2274     AND ((p_relationship_query_rec.object_id IS NULL)               OR (p_relationship_query_rec.object_id  = fnd_api.g_miss_num))
2275     AND ((p_relationship_query_rec.subject_id IS NULL)              OR (p_relationship_query_rec.subject_id  = fnd_api.g_miss_num))
2276       )
2277       THEN
2278        fnd_message.set_name('CSI', 'CSI_INVALID_PARAMETERS');
2279        fnd_msg_pub.add;
2280        x_return_status := fnd_api.g_ret_sts_error;
2281        RAISE fnd_api.g_exc_error;
2282       END IF;
2283 
2284       /* Cyclic Relationships */
2285   IF   (p_relationship_query_rec.relationship_type_code = 'CONNECTED-TO')
2286   THEN
2287          IF ((p_relationship_query_rec.subject_id IS NULL
2288                AND p_relationship_query_rec.object_id IS NULL)
2289             OR ((p_relationship_query_rec.subject_id IS NOT NULL
2290                AND p_relationship_query_rec.subject_id <> fnd_api.g_miss_num)
2291             AND (p_relationship_query_rec.object_id IS NOT NULL
2292                AND p_relationship_query_rec.object_id <> fnd_api.g_miss_num)))
2293          THEN
2294             fnd_message.set_name('CSI', 'CSI_INVALID_PARAMETERS');
2295             fnd_msg_pub.add;
2296             x_return_status := fnd_api.g_ret_sts_error;
2297             RAISE fnd_api.g_exc_error;
2298          ELSIF ((p_relationship_query_rec.subject_id IS NOT NULL
2299                   AND p_relationship_query_rec.subject_id <> fnd_api.g_miss_num))
2300                 OR (p_relationship_query_rec.object_id IS NOT NULL
2301                   AND p_relationship_query_rec.object_id <> fnd_api.g_miss_num)
2302          THEN
2303             IF p_relationship_query_rec.subject_id IS NOT NULL
2304                AND p_relationship_query_rec.subject_id <> fnd_api.g_miss_num
2305             THEN
2306                l_instance_id :=  p_relationship_query_rec.subject_id;
2307             ELSIF p_relationship_query_rec.object_id IS NOT NULL
2308                AND p_relationship_query_rec.object_id <> fnd_api.g_miss_num
2309             THEN
2310                l_instance_id := p_relationship_query_rec.object_id;
2311             END IF;
2312          END IF;
2313 
2314          csi_ii_relationships_pvt.get_cyclic_relationships(
2315             p_api_version                => p_api_version,
2316             p_commit                     => fnd_api.g_false,
2317             p_init_msg_list              => p_init_msg_list,
2318             p_validation_level           => p_validation_level,
2319             p_instance_id                => l_instance_id,
2320             p_depth                      => p_depth ,
2321             p_time_stamp                 => p_time_stamp,
2322             p_active_relationship_only   => p_active_relationship_only,
2323             x_relationship_tbl           => xc_relationship_tbl,
2324             x_return_status              => x_return_status,
2325             x_msg_count                  => x_msg_count,
2326             x_msg_data                   => x_msg_data
2327             );
2328 
2329        -- x_relationship_tbl.DELETE ;
2330        --
2331        -- Get the last purge date from csi_item_instances table
2332        --
2333        BEGIN
2334          SELECT last_purge_date
2335          INTO   l_last_purge_date
2336          FROM   CSI_ITEM_INSTANCES
2337          WHERE  rownum < 2;
2338        EXCEPTION
2339          WHEN no_data_found THEN
2340            NULL;
2341          WHEN others THEN
2342            NULL;
2343        END;
2344        --
2345        IF xc_relationship_tbl.COUNT > 0 THEN
2346           FOR  i IN xc_relationship_tbl.FIRST..xc_relationship_tbl.LAST
2347           LOOP
2348              l_rel_rec := null ;
2349              l_rel_rec :=  xc_relationship_tbl(i);
2350 
2351              IF ((p_time_stamp IS NOT NULL) AND (p_time_stamp <> FND_API.G_MISS_DATE))
2352              THEN
2353                   IF ((l_last_purge_date IS NOT NULL) AND (p_time_stamp <= l_last_purge_date))
2354                   THEN
2355                        csi_gen_utility_pvt.put_line('Warning! History for this entity has already been purged for the datetime stamp passed. ' ||
2356                        'Please provide a valid datetime stamp.');
2357                        FND_MESSAGE.Set_Name('CSI', 'CSI_API_HIST_AFTER_PURGE_REQ');
2358                        FND_MSG_PUB.ADD;
2359                   ELSE
2360                        get_history( p_rel_rec         => l_rel_rec
2361                                    ,p_new_rec         => l_new_rec
2362                                    ,p_flag            => l_flag
2363                                    ,p_time_stamp      => p_time_stamp);
2364 
2365                               IF l_flag='ADD'
2366                               THEN
2367                                  l_relationship_tbl(i) := l_new_rec;
2368                               END IF;
2369                   END IF;
2370              ELSE
2371                 l_relationship_tbl(i) := l_rel_rec;
2372              END IF;
2373 	     --
2374          IF ( (p_time_stamp IS NOT NULL) AND (p_time_stamp <> fnd_api.g_miss_date) )THEN
2375           IF l_relationship_tbl.count > 0
2376           THEN
2377             BEGIN
2378 		      SELECT 'Y'
2379 		      INTO l_relationship_tbl(i).subject_has_child
2380 		      FROM CSI_II_RELATIONSHIPS
2381 		      WHERE OBJECT_ID = l_relationship_tbl(i).subject_id
2382 		      AND   CREATION_DATE <= p_time_stamp
2383 		      AND   (ACTIVE_END_DATE IS NULL OR ACTIVE_END_DATE >= p_time_stamp)
2384 		      AND   ROWNUM < 2;
2385 		    EXCEPTION
2386 		      WHEN OTHERS THEN
2387 		        --l_relationship_tbl(i).subject_has_child := 'N';
2388                IF   (l_relationship_tbl(i).relationship_id IS NOT NULL --Added for bug 2999353
2389                  AND l_relationship_tbl(i).relationship_id <> fnd_api.g_miss_num) --Added for bug 2999353
2390                THEN
2391                     l_relationship_tbl(i).subject_has_child := 'N';
2392                END IF;
2393 		    END;
2394            END IF; -- l_relationship_tbl.count > 0
2395           END IF;
2396          END LOOP;
2397        END IF;
2398         --
2399         l_exp_inst_tbl.DELETE;
2400         IF l_relationship_tbl.count > 0 THEN
2401            FOR rel_row in l_relationship_tbl.FIRST .. l_relationship_tbl.LAST LOOP
2402               IF l_relationship_tbl.EXISTS(rel_row) THEN
2403 		 l_exists_flag := 'N';
2404 		 IF l_exp_inst_tbl.count > 0 THEN
2405 		    FOR exp_rec in l_exp_inst_tbl.FIRST .. l_exp_inst_tbl.LAST LOOP
2406 		       IF l_exp_inst_tbl(exp_rec) = l_relationship_tbl(rel_row).subject_id OR
2407                           l_exp_inst_tbl(exp_rec) = l_relationship_tbl(rel_row).object_id THEN
2408 			  l_exists_flag := 'Y';
2409 			  exit;
2410 		       END IF;
2411 		    END LOOP;
2412 		 END IF;
2413                  --
2414 		 IF l_exists_flag <> 'Y' THEN
2415 		    IF ( (p_time_stamp IS NOT NULL) AND (p_time_stamp <> fnd_api.g_miss_date)
2416              AND (l_relationship_tbl(rel_row).relationship_id IS NOT NULL   --Added for bug 2999353
2417              AND  l_relationship_tbl(rel_row).relationship_id <> fnd_api.g_miss_num))--Added for bug 2999353
2418             THEN
2419 		       l_instance_rec.instance_id := l_relationship_tbl(rel_row).subject_id;
2420 		       CSI_ITEM_INSTANCE_PUB.Get_item_instance_details(
2421 			     1.0,
2422 			    'F',
2423 			    'T',
2424 			    1,
2425 			    l_instance_rec,
2426 			    'F',
2427 			    l_party_header_tbl,
2428 			    'F',
2429 			    l_account_header_tbl,
2430 			    'F',
2431 			    l_org_header_tbl,
2432 			    'F',
2433 			    l_pricing_attrib_tbl,
2434 			    'F',
2435 			    l_ext_attrib_tbl,
2436 			    l_ext_attrib_def_tbl,
2437 			    'F',
2438 			    l_asset_header_tbl,
2439 			    'F',
2440 			    p_time_stamp,
2441 			    x_return_status,
2442 			    x_msg_count,
2443 			    x_msg_data
2444 			);
2445 		       --
2446 		       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2447 			  l_msg_index := 1;
2448 			  l_msg_count := x_msg_count;
2449 			  WHILE l_msg_count > 0 LOOP
2450 			     x_msg_data := FND_MSG_PUB.GET
2451 					      (  l_msg_index,
2452 						 FND_API.G_FALSE );
2453 			     csi_gen_utility_pvt.put_line( ' Error from Get_Item_Instance_Details.. ');
2454 			     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2455 			     l_msg_index := l_msg_index + 1;
2456 			     l_msg_count := l_msg_count - 1;
2457 			  END LOOP;
2458 			  RAISE FND_API.G_EXC_ERROR;
2459 		       END IF;
2460 		       IF nvl(l_instance_rec.active_end_date,(sysdate+1)) < sysdate THEN
2461 			  l_ctr := l_exp_inst_tbl.count;
2462 			  l_ctr := l_ctr + 1;
2463 			  l_exp_inst_tbl(l_ctr) := l_relationship_tbl(rel_row).subject_id;
2464 		       ELSE
2465 			  l_rel_count := l_rel_count + 1;
2466 			  l_temp_relationship_tbl(l_rel_count) := l_relationship_tbl(rel_row);
2467 		       END IF;
2468 		    ELSE -- p_time_stamp is not passed
2469 		       Begin
2470 			  Select 'x'
2471 			  into l_exists
2472 			  from CSI_ITEM_INSTANCES
2473 			  where instance_id = l_relationship_tbl(rel_row).subject_id
2474 			  and   nvl(active_end_date,(sysdate+1)) < sysdate;
2475 			  l_ctr := l_exp_inst_tbl.count;
2476 			  l_ctr := l_ctr + 1;
2477 			  l_exp_inst_tbl(l_ctr) := l_relationship_tbl(rel_row).subject_id;
2478 		       Exception
2479 			  when no_data_found then -- Active Instance
2480 			     l_rel_count := l_rel_count + 1;
2481 			     l_temp_relationship_tbl(l_rel_count) := l_relationship_tbl(rel_row);
2482 		       End;
2483 		    END IF;
2484 		 END IF;
2485               END IF;
2486            END LOOP;
2487         END IF;
2488     -- Added by sguthiva for 2562028 fix
2489       l_rel_count := 0;
2490       IF p_active_relationship_only = 'T' THEN
2491          IF l_temp_relationship_tbl.count > 0 THEN
2492             FOR rel_row in l_temp_relationship_tbl.FIRST .. l_temp_relationship_tbl.LAST
2493             LOOP
2494                IF l_temp_relationship_tbl.EXISTS(rel_row) THEN
2495                   IF l_temp_relationship_tbl(rel_row).active_end_date IS NULL OR
2496                      l_temp_relationship_tbl(rel_row).active_end_date > SYSDATE THEN
2497                      l_rel_count := l_rel_count + 1;
2498                      x_relationship_tbl(l_rel_count) := l_temp_relationship_tbl(rel_row);
2499                   END IF;
2500                END IF;
2501             END LOOP;
2502          END IF;
2503       ELSE
2504          x_relationship_tbl := l_temp_relationship_tbl;
2505       END IF;
2506 
2507     -- End addition by sguthiva
2508   ELSE  ---if not CONNECTED-TO
2509       --gen_select(l_crit_relations_rec,l_select_cl);
2510       IF ( ((p_relationship_query_rec.relationship_id IS NULL) OR (p_relationship_query_rec.relationship_id = fnd_api.g_miss_num))
2511       AND  ((p_relationship_query_rec.subject_id IS NULL) OR (p_relationship_query_rec.subject_id = fnd_api.g_miss_num))
2512       AND  ((p_relationship_query_rec.relationship_type_code IS NULL) OR (p_relationship_query_rec.relationship_type_code = fnd_api.g_miss_char)) )
2513       THEN
2514          IF( (p_relationship_query_rec.object_id IS NOT NULL) AND (p_relationship_query_rec.object_id <> fnd_api.g_miss_num) )
2515          THEN
2516            IF( (p_relationship_query_rec.relationship_type_code IS NULL) OR (p_relationship_query_rec.relationship_type_code = fnd_api.g_miss_char) )
2517            THEN
2518                    fnd_message.set_name('CSI','CSI_NO_RELCODE_PASSED');
2519                    fnd_msg_pub.add;
2520                    RAISE fnd_api.g_exc_error;
2521            END IF;
2522          END IF;
2523        END IF;
2524 /*
2525       gen_relations_where(l_crit_relations_rec,l_active_relationship_only,l_depth,l_relations_where);
2526           IF dbms_sql.is_open(l_cur_get_relations) THEN
2527           dbms_sql.close_CURSOR(l_cur_get_relations);
2528           END IF;
2529 
2530        l_cur_get_relations := dbms_sql.open_CURSOR;
2531 
2532        dbms_sql.parse(l_cur_get_relations, l_select_cl||l_relations_where , dbms_sql.native);
2533 
2534        bind(l_crit_relations_rec, l_cur_get_relations);
2535 
2536        define_columns(l_def_relations_rec, l_cur_get_relations);
2537 
2538        l_ignore := dbms_sql.execute(l_cur_get_relations);
2539 */
2540         -- Bug 8904684 Modified p_get_dfs to take
2541         -- fnd_api.g_false
2542         Get_Children
2543            (p_relationship_query_rec    => p_relationship_query_rec,
2544             p_rel_tbl                   => l_rel_tbl,
2545             p_depth                     => p_depth,
2546             p_active_relationship_only  => p_active_relationship_only,
2547             p_time_stamp                => p_time_stamp,
2548             p_get_dfs                   => fnd_api.g_false,
2549             p_ii_relationship_level_tbl => l_ii_relationship_level_tbl,
2550             x_return_status             => x_return_status,
2551             x_msg_count                 => x_msg_count,
2552             x_msg_data                  => x_msg_data
2553            );
2554          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS)
2555          THEN
2556             l_msg_index := 1;
2557             l_msg_count := x_msg_count;
2558            WHILE l_msg_count > 0
2559            LOOP
2560                x_msg_data := FND_MSG_PUB.GET
2561                           (  l_msg_index,
2562                              FND_API.G_FALSE );
2563                 csi_gen_utility_pvt.put_line( ' Error from Get_Chidren.. ');
2564                 csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2565                 l_msg_index := l_msg_index + 1;
2566                 l_msg_count := l_msg_count - 1;
2567            END LOOP;
2568               RAISE FND_API.G_EXC_ERROR;
2569          END IF;
2570 
2571      --
2572      -- Get the last purge date from csi_item_instances table
2573      --
2574      BEGIN
2575        SELECT last_purge_date
2576        INTO   l_last_purge_date
2577        FROM   CSI_ITEM_INSTANCES
2578        WHERE  rownum < 2;
2579      EXCEPTION
2580        WHEN no_data_found THEN
2581          NULL;
2582        WHEN others THEN
2583          NULL;
2584      END;
2585      --
2586    IF p_time_stamp IS NULL OR p_time_stamp = fnd_api.g_miss_date
2587    THEN
2588         x_relationship_tbl:=l_rel_tbl;
2589    ELSIF l_rel_tbl.COUNT >0
2590    THEN
2591      FOR p_time_csr IN l_rel_tbl.FIRST .. l_rel_tbl.LAST
2592      LOOP
2593          IF l_rel_tbl.EXISTS(p_time_csr)
2594          THEN
2595                       l_new_rec:=NULL;
2596                       l_returned_rec_count:=l_returned_rec_count+1;
2597 
2598                        IF ((l_last_purge_date IS NOT NULL) AND (p_time_stamp <= l_last_purge_date))
2599                        THEN
2600                            csi_gen_utility_pvt.put_line('Warning! History for this entity has already been purged for the datetime stamp passed. ' ||
2601                            'Please provide a valid datetime stamp.');
2602                            FND_MESSAGE.Set_Name('CSI', 'CSI_API_HIST_AFTER_PURGE_REQ');
2603                            FND_MSG_PUB.ADD;
2604                        ELSE
2605                              get_history( p_rel_rec         => l_rel_tbl(p_time_csr)
2606                                          ,p_new_rec         => l_new_rec
2607                                          ,p_flag            => l_flag
2608                                          ,p_time_stamp      => p_time_stamp);
2609                        END IF;
2610                        -- Added by sguthiva for bug 2373109
2611                           IF l_new_rec.relationship_id IS NOT NULL AND
2612                              l_new_rec.relationship_id <> fnd_api.g_miss_num
2613                           THEN
2614                             IF l_flag='ADD' THEN
2615                                l_relationship_tbl(l_returned_rec_count) :=l_new_rec;
2616                        -- Added for bug 2999353
2617                               IF  l_relationship_tbl(l_returned_rec_count).subject_id <>l_rel_tbl(p_time_csr).subject_id
2618                               THEN
2619                                l_relationship_query_rec:=p_relationship_query_rec;
2620                                --
2621                                   l_depth:=0;
2622                                   IF l_ii_relationship_level_tbl.COUNT > 0 AND
2623                                      p_depth IS NOT NULL AND
2624                                      p_depth <> fnd_api.g_miss_num AND
2625                                      p_depth >0
2626                                   THEN
2627                                     FOR l_lvl_csr IN l_ii_relationship_level_tbl.FIRST .. l_ii_relationship_level_tbl.LAST
2628                                     LOOP
2629                                       IF l_ii_relationship_level_tbl.EXISTS(l_lvl_csr)
2630                                       THEN
2631                                         IF l_ii_relationship_level_tbl(l_lvl_csr).relationship_id  IS NOT NULL AND
2632                                            l_ii_relationship_level_tbl(l_lvl_csr).relationship_id <> fnd_api.g_miss_num AND
2633                                            l_ii_relationship_level_tbl(l_lvl_csr).relationship_id =l_relationship_tbl(l_returned_rec_count).relationship_id
2634                                         THEN
2635                                            l_depth:=p_depth-l_ii_relationship_level_tbl(l_lvl_csr).current_level;
2636                                            EXIT;
2637                                         END IF;
2638                                       END IF;
2639                                     END LOOP;
2640                                   END IF;
2641                                IF l_depth>0 OR
2642                                   p_depth IS NULL
2643                                THEN
2644                                      l_relationship_query_rec.object_id:=l_relationship_tbl(l_returned_rec_count).subject_id;
2645                                      l_relationship_query_rec.subject_id:=fnd_api.g_miss_num;
2646                                      csi_gen_utility_pvt.put_line('Into recurrsive call for get_relationships. ');
2647                                      csi_ii_relationships_pvt.get_relationships
2648                                      (  p_api_version               => p_api_version
2649                                        ,p_commit                    => p_commit
2650                                        ,p_init_msg_list             => p_init_msg_list
2651                                        ,p_validation_level          => p_validation_level
2652                                        ,p_relationship_query_rec    => l_relationship_query_rec
2653                                        ,p_depth                     => l_depth
2654                                        ,p_time_stamp                => p_time_stamp
2655                                        ,p_active_relationship_only  => p_active_relationship_only
2656                                        ,p_recursive_flag            => fnd_api.g_true
2657                                        ,x_relationship_tbl          => x_relationship_tbl
2658                                        ,x_return_status             => x_return_status
2659                                        ,x_msg_count                 => x_msg_count
2660                                        ,x_msg_data                  => x_msg_data
2661                                      );
2662 
2663                                   IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS)
2664                                   THEN
2665                                        l_msg_index := 1;
2666                                        l_msg_count := x_msg_count;
2667                                      WHILE l_msg_count > 0
2668                                      LOOP
2669                                          x_msg_data := FND_MSG_PUB.GET
2670                                                       (  l_msg_index,
2671                                                          FND_API.G_FALSE );
2672                                          csi_gen_utility_pvt.put_line( ' Error from recursive Get_relationships.. ');
2673                                          csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2674                                          l_msg_index := l_msg_index + 1;
2675                                          l_msg_count := l_msg_count - 1;
2676                                      END LOOP;
2677                                        RAISE FND_API.G_EXC_ERROR;
2678                                   END IF;
2679 
2680                                     IF x_relationship_tbl.count > 0
2681                                     THEN
2682                                       FOR i IN x_relationship_tbl.FIRST .. x_relationship_tbl.LAST
2683                                       LOOP
2684                                         IF x_relationship_tbl.EXISTS(i)
2685                                         THEN
2686                                           IF x_relationship_tbl(i).relationship_id  IS NOT NULL AND
2687                                              x_relationship_tbl(i).relationship_id <> fnd_api.g_miss_num
2688                                           THEN
2689                                              l_returned_rec_count := l_returned_rec_count + 1;
2690                                              l_relationship_tbl(l_returned_rec_count):=x_relationship_tbl(i);
2691                                           END IF;
2692                                         END IF;
2693                                       END LOOP;
2694                                     END IF;
2695                                END IF;
2696                               END IF;
2697                        -- End May29 Addition
2698                             END IF;
2699                           ELSE
2700                             -- Do not add the current record by default
2701                             BEGIN
2702                               SELECT 'x'
2703                               INTO   l_found
2704                               FROM   csi_ii_relationships_h
2705                               WHERE  relationship_id=l_rel_tbl(p_time_csr).relationship_id;
2706                             EXCEPTION
2707                              WHEN NO_DATA_FOUND THEN
2708                               l_relationship_tbl(l_returned_rec_count) :=l_rel_tbl(p_time_csr);
2709                              WHEN OTHERS THEN
2710                               NULL;
2711                             END;
2712                           END IF;
2713                        -- End addition by sguthiva for bug 2373109
2714 
2715                     --
2716                     IF ( (p_time_stamp IS NOT NULL) AND (p_time_stamp <> fnd_api.g_miss_date) )THEN
2717                      IF l_relationship_tbl.count > 0
2718                      THEN
2719                        BEGIN
2720                           SELECT 'Y'
2721                           INTO l_relationship_tbl(l_returned_rec_count).subject_has_child
2722                           FROM CSI_II_RELATIONSHIPS
2723                           WHERE OBJECT_ID = l_relationship_tbl(l_returned_rec_count).subject_id
2724                           AND   CREATION_DATE <= p_time_stamp
2725                           AND   (ACTIVE_END_DATE IS NULL OR ACTIVE_END_DATE >= p_time_stamp)
2726                           AND   ROWNUM < 2;
2727                        EXCEPTION
2728                           WHEN OTHERS THEN
2729                              --l_relationship_tbl(l_returned_rec_count).subject_has_child := 'N';
2730                              IF (l_relationship_tbl(l_returned_rec_count).relationship_id IS NOT NULL --Added for bug 2999353
2731                              AND l_relationship_tbl(l_returned_rec_count).relationship_id <> fnd_api.g_miss_num) --Added for bug 2999353
2732                              THEN
2733                                  l_relationship_tbl(l_returned_rec_count).subject_has_child := 'N';
2734                              END IF;
2735                        END;
2736                      END IF; -- l_relationship_tbl.count > 0
2737                     END IF;
2738               --END IF;
2739          ELSE
2740             EXIT;
2741          END IF;
2742       END LOOP;
2743       --
2744       l_exp_inst_tbl.DELETE;
2745       IF l_relationship_tbl.count > 0 THEN
2746          FOR rel_row in l_relationship_tbl.FIRST .. l_relationship_tbl.LAST
2747          LOOP
2748             IF l_relationship_tbl.EXISTS(rel_row) THEN
2749                l_exists_flag := 'N';
2750                IF l_exp_inst_tbl.count > 0 THEN
2751                   FOR exp_rec in l_exp_inst_tbl.FIRST .. l_exp_inst_tbl.LAST LOOP
2752                      IF l_exp_inst_tbl(exp_rec) = l_relationship_tbl(rel_row).subject_id THEN
2753                         l_exists_flag := 'Y';
2754                         exit;
2755                      END IF;
2756                   END LOOP;
2757                END IF;
2758                --
2759                IF l_exists_flag <> 'Y' THEN
2760                   IF ( (p_time_stamp IS NOT NULL) AND (p_time_stamp <> fnd_api.g_miss_date)
2761                    AND (l_relationship_tbl(rel_row).relationship_id IS NOT NULL   --Added for bug 2999353
2762                    AND l_relationship_tbl(rel_row).relationship_id <> fnd_api.g_miss_num))--Added for bug 2999353
2763                   THEN
2764                      IF p_recursive_flag=fnd_api.g_false
2765                      THEN
2766                            l_instance_rec.instance_id := l_relationship_tbl(rel_row).subject_id;
2767                                CSI_ITEM_INSTANCE_PUB.Get_item_instance_details(
2768                                                                                1.0,
2769                                                                                'F',
2770                                                                                'T',
2771                                                                                 1,
2772                                                                                l_instance_rec,
2773                                                                                'F',
2774                                                                                l_party_header_tbl,
2775                                                                                'F',
2776                                                                                l_account_header_tbl,
2777                                                                                'F',
2778                                                                                l_org_header_tbl,
2779                                                                                'F',
2780                                                                                l_pricing_attrib_tbl,
2781                                                                                'F',
2782                                                                                l_ext_attrib_tbl,
2783                                                                                l_ext_attrib_def_tbl,
2784                                                                                'F',
2785                                                                                l_asset_header_tbl,
2786                                                                                'F',
2787                                                                                p_time_stamp,
2788                                                                                x_return_status,
2789                                                                                x_msg_count,
2790                                                                                x_msg_data
2791                                                                                 );
2792                      ELSE
2793                        l_instance_rec.instance_id := l_relationship_tbl(rel_row).subject_id;
2794                        l_instance_rec.active_end_date := NULL;
2795                      END IF;
2796                      --
2797                      IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2798                         l_msg_index := 1;
2799                         l_msg_count := x_msg_count;
2800                         WHILE l_msg_count > 0 LOOP
2801                            x_msg_data := FND_MSG_PUB.GET
2802                                             (  l_msg_index,
2803                                                FND_API.G_FALSE );
2804                            csi_gen_utility_pvt.put_line( ' Error from Get_Item_Instance_Details.. ');
2805                            csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2806                            l_msg_index := l_msg_index + 1;
2807                            l_msg_count := l_msg_count - 1;
2808                         END LOOP;
2809                         RAISE FND_API.G_EXC_ERROR;
2810                      END IF;
2811                      IF nvl(l_instance_rec.active_end_date,(sysdate+1)) < sysdate THEN
2812                         l_ctr := l_exp_inst_tbl.count;
2813                         l_ctr := l_ctr + 1;
2814                         l_exp_inst_tbl(l_ctr) := l_relationship_tbl(rel_row).subject_id;
2815                         Get_Children
2816                         (p_object_id => l_relationship_tbl(rel_row).subject_id,
2817                          p_rel_tbl   => l_ins_child_tbl
2818                          );
2819                         -- Modified for bug 2999353
2820                        IF l_ins_child_tbl.COUNT >0
2821                        THEN
2822                            FOR v_rec in l_ins_child_tbl.first .. l_ins_child_tbl.last
2823                            LOOP
2824                              IF l_ins_child_tbl.EXISTS(v_rec)
2825                              THEN
2826                                 l_ctr := l_ctr + 1;
2827                                 l_exp_inst_tbl(l_ctr) := l_ins_child_tbl(v_rec).subject_id;
2828                              END IF;
2829                            END LOOP;
2830                        END IF;
2831                        -- End modification for bug 2999353
2832                      ELSE
2833                         l_rel_count := l_rel_count + 1;
2834                         l_temp_relationship_tbl(l_rel_count) := l_relationship_tbl(rel_row);
2835                      END IF;
2836                  /*
2837                   ELSE -- p_time_stamp is not passed
2838                      Begin
2839                         Select 'x'
2840                         into l_exists
2841                         from CSI_ITEM_INSTANCES
2842                         where instance_id = l_relationship_tbl(rel_row).subject_id
2843                         and   nvl(active_end_date,(sysdate+1)) < sysdate;
2844                         l_ctr := l_exp_inst_tbl.count;
2845                         FOR v_rec in CHILD_CUR(l_relationship_tbl(rel_row).subject_id) LOOP
2846                            l_ctr := l_ctr + 1;
2847                            l_exp_inst_tbl(l_ctr) := v_rec.subject_id;
2848                         END LOOP;
2849                      Exception
2850                         when no_data_found then -- Active Instance
2851                            l_rel_count := l_rel_count + 1;
2852                            l_temp_relationship_tbl(l_rel_count) := l_relationship_tbl(rel_row);
2853                      End;
2854                    */
2855                   END IF;
2856                END IF;
2857             END IF;
2858          END LOOP;
2859       END IF;
2860       --
2861       l_rel_count := 0;
2862       IF p_active_relationship_only = 'T' THEN
2863          IF l_temp_relationship_tbl.count > 0 THEN
2864             FOR rel_row in l_temp_relationship_tbl.FIRST .. l_temp_relationship_tbl.LAST
2865             LOOP
2866                IF l_temp_relationship_tbl.EXISTS(rel_row) THEN
2867                   IF l_temp_relationship_tbl(rel_row).active_end_date IS NULL OR
2868                      l_temp_relationship_tbl(rel_row).active_end_date > SYSDATE THEN
2869                      l_rel_count := l_rel_count + 1;
2870                      x_relationship_tbl(l_rel_count) := l_temp_relationship_tbl(rel_row);
2871 -- Added for bug 2999353
2872                    ELSIF ( l_temp_relationship_tbl(rel_row).active_end_date IS NOT NULL AND
2873                            l_temp_relationship_tbl(rel_row).active_end_date < SYSDATE ) OR
2874                          ( l_temp_relationship_tbl(rel_row).relationship_type_code<>'COMPONENT-OF' )
2875                    THEN
2876 -- Here we capture expired relationship records to use them later down in the program to
2877 -- eliminate all its child components.
2878                      l_exp_count:=l_exp_count + 1;
2879                      l_exp_tbl(l_exp_count) := l_temp_relationship_tbl(rel_row);
2880 -- End addition for bug 2999353
2881                   END IF;
2882                END IF;
2883             END LOOP;
2884          END IF;
2885       ELSE
2886 -- Added for bug 2999353
2887          IF l_temp_relationship_tbl.count > 0 THEN
2888             FOR l_csr IN l_temp_relationship_tbl.FIRST .. l_temp_relationship_tbl.LAST
2889             LOOP
2890                IF l_temp_relationship_tbl.EXISTS(l_csr)
2891                THEN
2892                    IF l_temp_relationship_tbl(l_csr).relationship_id IS NOT NULL AND
2893                       l_temp_relationship_tbl(l_csr).relationship_id<> fnd_api.g_miss_num
2894                    THEN
2895                      IF l_temp_relationship_tbl(l_csr).relationship_type_code<>'COMPONENT-OF'
2896                      THEN
2897                         l_exp_count:=l_exp_count + 1;
2898                         l_exp_tbl(l_exp_count) := l_temp_relationship_tbl(l_csr);
2899                      END IF;
2900                     l_found2:=FALSE;
2901                     IF x_relationship_tbl.COUNT>0
2902                     THEN
2903                        FOR i IN x_relationship_tbl.FIRST..x_relationship_tbl.LAST
2904                        LOOP
2905                           IF x_relationship_tbl.EXISTS(i)
2906                           THEN
2907                             IF x_relationship_tbl(i).relationship_id=l_temp_relationship_tbl(l_csr).relationship_id
2908                             THEN
2909                               l_found2:=TRUE;
2910                               EXIT;
2911                             END IF;
2912                           END IF;
2913                        END LOOP;
2914                        IF NOT(l_found2)
2915                        THEN
2916                          x_relationship_tbl(x_relationship_tbl.count+1) := l_temp_relationship_tbl(l_csr);
2917                        END IF;
2918                     ELSE
2919                         x_relationship_tbl(x_relationship_tbl.count+1) := l_temp_relationship_tbl(l_csr);
2920 -- End addition bug 2999353
2921                     END IF;
2922                    END IF;
2923                END IF;
2924             END LOOP;
2925          END IF;
2926       END IF;
2927 
2928 -- sguthiva added the following code for bug 2999353
2929 -- The following code has been added to eliminate all the child components of an expired relationship record.
2930       IF p_active_relationship_only = 'T'
2931       THEN
2932             l_fin_count:=0;
2933             l_temp_relationship_tbl.DELETE;
2934             l_temp_relationship_tbl:=x_relationship_tbl;
2935          IF l_exp_tbl.COUNT > 0
2936          THEN
2937             FOR l_exp_csr IN l_exp_tbl.FIRST .. l_exp_tbl.LAST
2938             LOOP
2939                IF l_exp_tbl.EXISTS(l_exp_csr)
2940                THEN
2941                   IF l_exp_tbl(l_exp_csr).relationship_id IS NOT NULL AND
2942                      l_exp_tbl(l_exp_csr).relationship_id <> fnd_api.g_miss_num AND
2943                      nvl(l_exp_tbl(l_exp_csr).attribute1,'NOT-EXPIRED')<>'EXPIRED'
2944                   THEN
2945                     l_exp_act_tbl.delete;
2946                     -- Used the following procedure instead of a cursor which use
2947                     -- connect by select statement.
2948                      Get_Children
2949                         (p_object_id => l_exp_tbl(l_exp_csr).subject_id,
2950                          p_rel_tbl   => l_exp_act_tbl
2951                          );
2952                    IF l_exp_act_tbl.count >0 --Added for bug 3228702
2953                    THEN
2954                      FOR l_active_csr IN l_exp_act_tbl.first .. l_exp_act_tbl.last --expired_cur(l_exp_tbl(l_exp_csr).subject_id)
2955                      LOOP
2956                       IF l_exp_act_tbl.EXISTS(l_active_csr)
2957                       THEN
2958                        l_expire:=TRUE;
2959                        l_fin_count:=l_fin_count+1;
2960                          IF l_exp_act_tbl(l_active_csr).active_end_date IS NULL OR
2961                             l_exp_act_tbl(l_active_csr).active_end_date > SYSDATE AND
2962                             l_exp_act_tbl(l_active_csr).relationship_type_code='COMPONENT-OF'
2963                          THEN
2964                             l_temp_tbl(l_fin_count).relationship_id := l_exp_act_tbl(l_active_csr).relationship_id;
2965                          ELSIF l_exp_act_tbl(l_active_csr).relationship_type_code='COMPONENT-OF'
2966                          THEN
2967                             FOR l_end_date_csr IN l_exp_tbl.FIRST .. l_exp_tbl.LAST
2968                             LOOP
2969                               IF l_exp_act_tbl(l_active_csr).relationship_id=l_exp_tbl(l_end_date_csr).relationship_id
2970                               THEN
2971                             -- Since this record is expired which is a child of an expired record and should
2972                             -- not be picked while looping for active records.
2973                                 l_exp_tbl(l_end_date_csr).attribute1:='EXPIRED';
2974                               END IF;
2975                             END LOOP;
2976                          END IF;
2977                       END IF;
2978                      END LOOP; -- End loop for expired_cur
2979                    END IF;
2980 
2981                   END IF; -- End if for 'EXPIRED' check
2982                END IF;
2983             END LOOP; -- End loop for exprired records captured in table l_exp_tbl.
2984          END IF;
2985 
2986 -- Since there are active relationship records for an expired relationship record
2987 -- so delete all the records from output table x_relationship_tbl.
2988          IF l_expire
2989          THEN
2990            x_relationship_tbl.DELETE;
2991          END IF;
2992 
2993 -- Here we build x_relationship_tbl which consists of only active records.
2994 
2995          l_fin_count1:=0;
2996          IF l_temp_relationship_tbl.COUNT > 0
2997          THEN
2998             FOR l_tot_csr IN l_temp_relationship_tbl.FIRST .. l_temp_relationship_tbl.LAST
2999             LOOP
3000               l_found1:=FALSE;
3001                  IF l_temp_relationship_tbl.EXISTS(l_tot_csr)
3002                  THEN
3003                    IF l_temp_tbl.COUNT > 0
3004                    THEN
3005                       FOR l_exp_cld_csr IN l_temp_tbl.FIRST .. l_temp_tbl.LAST
3006                       LOOP
3007                        IF l_temp_tbl.EXISTS(l_exp_cld_csr)
3008                        THEN
3009                          IF l_temp_tbl(l_exp_cld_csr).relationship_id = l_temp_relationship_tbl(l_tot_csr).relationship_id
3010                          THEN
3011                            l_found1:=TRUE;
3012                          END IF;
3013                        END IF;
3014                       END LOOP;
3015                    END IF;
3016                  END IF;
3017 
3018                  IF NOT(l_found1)
3019                  THEN
3020                     l_fin_count1 :=l_fin_count1+1;
3021                     IF l_temp_relationship_tbl(l_tot_csr).relationship_id IS NOT NULL AND
3022                        l_temp_relationship_tbl(l_tot_csr).relationship_id <> fnd_api.g_miss_num
3023                     THEN
3024                       x_relationship_tbl(l_fin_count1):=l_temp_relationship_tbl(l_tot_csr);
3025                     END IF;
3026                  END IF;
3027 
3028             END LOOP;
3029          END IF;
3030 
3031 
3032       END IF; -- This end if is for p_active_relationship_only
3033               -- sk End addition
3034    END IF;
3035   END IF;
3036 
3037       --
3038       -- end of API body
3039       --
3040       -- standard call to get message count and if count is 1, get message info.
3041       fnd_msg_pub.count_AND_get
3042       (  p_encoded => FND_API.G_FALSE, --Added for bug 10081206
3043       p_count          =>   x_msg_count,
3044          p_data           =>   x_msg_data
3045       );
3046       EXCEPTION
3047          WHEN fnd_api.g_exc_error THEN
3048              --  ROLLBACK TO get_relationships_pvt;
3049                x_return_status := fnd_api.g_ret_sts_error ;
3050                fnd_msg_pub.count_AND_get
3051                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
3052            p_count => x_msg_count ,
3053                          p_data => x_msg_data
3054                         );
3055 
3056           WHEN fnd_api.g_exc_unexpected_error THEN
3057              --   ROLLBACK TO get_relationships_pvt;
3058                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
3059                 fnd_msg_pub.count_AND_get
3060                          (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
3061            p_count => x_msg_count ,
3062                           p_data => x_msg_data
3063                          );
3064 
3065           WHEN OTHERS THEN
3066               --  ROLLBACK TO get_relationships_pvt;
3067                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
3068                   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3069                          fnd_msg_pub.add_exc_msg(g_pkg_name ,l_api_name);
3070                   END IF;
3071                 fnd_msg_pub.count_AND_get
3072                          (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
3073            p_count => x_msg_count ,
3074                           p_data => x_msg_data
3075                          );
3076 END get_relationships;
3077 /* End of Cyclic Relationships */
3078 
3079 /* Start of Cyclic Relationships */
3080 FUNCTION valid_in_parameters
3081 (   p_relship_id           IN      VARCHAR2,
3082     p_object_id            IN      NUMBER,
3083     p_subject_id           IN      NUMBER
3084  ) RETURN BOOLEAN IS
3085 
3086 BEGIN
3087    IF p_relship_id is not null AND  P_relship_id <> fnd_api.g_miss_num
3088    THEN
3089       IF ((p_object_id is  null OR p_object_id = fnd_api.g_miss_num)
3090          AND (p_subject_id is  null OR p_subject_id = fnd_api.g_miss_num))
3091       THEN
3092          RETURN TRUE;
3093       ELSIF ((p_object_id is  null OR p_object_id = fnd_api.g_miss_num)
3094          AND (p_subject_id is not null AND  p_subject_id <> fnd_api.g_miss_num))
3095       THEN
3096          RETURN FALSE;
3097       ELSIF ((p_object_id is not null AND p_object_id <> fnd_api.g_miss_num)
3098            AND (p_subject_id is  null OR p_subject_id = fnd_api.g_miss_num))
3099       THEN
3100          RETURN TRUE;
3101       ELSIF  ((p_object_id is not null AND p_object_id <> fnd_api.g_miss_num)
3102          AND (p_subject_id is not null AND p_subject_id <> fnd_api.g_miss_num))
3103       THEN
3104          RETURN TRUE;
3105       END IF;
3106    END IF;
3107 EXCEPTION
3108    WHEN OTHERS THEN
3109       RETURN FALSE;
3110 END valid_in_parameters;
3111 
3112 FUNCTION config_set(p_relationship_id IN NUMBER)
3113 RETURN BOOLEAN IS
3114 
3115    l_configurator_id   NUMBER;
3116 BEGIN
3117   /* SELECT configurator_id
3118    INTO   l_configurator_id
3119    FROM   csi_ii_relationships
3120    WHERE  relationship_id=p_relationship_id;
3121 */
3122    IF l_configurator_id is not null
3123    THEN
3124       fnd_message.set_name('CSI','CSI_CONFIG_SET');
3125       fnd_message.set_token('relationship_id',p_relationship_id);
3126       fnd_msg_pub.add;
3127       RETURN TRUE;
3128    ELSE
3129       RETURN FALSE;
3130    END IF;
3131 EXCEPTION
3132    WHEN NO_DATA_FOUND THEN
3133       RETURN TRUE;
3134 END config_set;
3135 
3136 --   This function is used for 'CONNECTED-TO' relationship type.It checks whether 'CONNECTED-TO'
3137 -- relation exists in the reverse direction for the given subject and object
3138 
3139 FUNCTION relationship_not_exists
3140   ( p_subject_id           IN      NUMBER,
3141     p_object_id            IN      NUMBER,
3142     p_relship_type_code    IN      VARCHAR2,
3143     p_mode                 IN      VARCHAR2,
3144     p_relationship_id      IN      NUMBER
3145   ) RETURN BOOLEAN IS
3146 
3147    l_object_id                    NUMBER;
3148    l_subject_id                   NUMBER;
3149    l_relship_type_code            VARCHAR2(30);
3150    l_dummy                        VARCHAR2(1) :=NULL;
3151 BEGIN
3152  IF p_mode='CREATE'
3153  THEN
3154    -- Bug 11685074/12536407
3155    --SELECT 'x'
3156    --INTO   l_dummy
3157    SELECT relationship_type_code
3158    INTO l_relship_type_code
3159    FROM   csi_ii_relationships
3160    WHERE  (( subject_id=p_object_id AND object_id=p_subject_id)
3161    OR       (subject_id=p_subject_id AND object_id=p_object_id)
3162    OR (subject_id=p_subject_id ))
3163    --AND    relationship_type_code = p_relship_type_code
3164    AND    (active_end_date IS NULL OR active_end_date > SYSDATE)
3165    AND   ROWNUM = 1  ;
3166    IF SQL%FOUND THEN
3167       fnd_message.set_name('CSI','CSI_RELATIONSHIP_EXISTS');
3168       fnd_message.set_token('relationship_type',l_relship_type_code);
3169       fnd_message.set_token('subject_id',p_subject_id);
3170       fnd_message.set_token('object_id',p_object_id);
3171       fnd_msg_pub.add;
3172       RETURN FALSE;
3173    ELSE
3174       RETURN TRUE;
3175    END IF;
3176  ELSIF p_mode='UPDATE'
3177  THEN
3178    -- Bug 11685074/12536407
3179    --SELECT 'x'
3180    --INTO   l_dummy
3181    SELECT relationship_type_code
3182    INTO l_relship_type_code
3183    FROM   csi_ii_relationships
3184    WHERE  (( subject_id=p_object_id AND object_id=p_subject_id)
3185    OR       (subject_id=p_subject_id AND object_id=p_object_id)
3186    OR (subject_id=p_subject_id ))
3187    --AND    relationship_type_code = p_relship_type_code
3188    AND    (active_end_date IS NULL OR active_end_date > SYSDATE)
3189    AND    relationship_id<>p_relationship_id
3190    AND   ROWNUM = 1  ;
3191    IF SQL%FOUND THEN
3192       fnd_message.set_name('CSI','CSI_RELATIONSHIP_EXISTS');
3193       fnd_message.set_token('relationship_type',l_relship_type_code);
3194       fnd_message.set_token('subject_id',p_subject_id);
3195       fnd_message.set_token('object_id',p_object_id);
3196       fnd_msg_pub.add;
3197       RETURN FALSE;
3198    ELSE
3199       RETURN TRUE;
3200    END IF;
3201  END IF;
3202 
3203 EXCEPTION
3204    WHEN NO_DATA_FOUND THEN
3205       RETURN TRUE;
3206 END relationship_not_exists;
3207 
3208 /* End of Cyclic Relationships */
3209 
3210 -- Start of att enhancements by sguthiva
3211 -- This function to make sure that an object or subject instance can participate
3212 -- atmost 2 'CONNECTED-TO' relationships.
3213 
3214 
3215 FUNCTION relationship_for_link
3216   ( p_instance_id          IN      NUMBER,
3217     p_mode                 IN      VARCHAR2,
3218     p_relationship_id      IN      NUMBER
3219   ) RETURN BOOLEAN IS
3220    l_count                        NUMBER :=0;
3221 BEGIN
3222 
3223    IF p_mode='CREATE'
3224    THEN
3225         SELECT COUNT(*)
3226         INTO   l_count
3227         FROM   csi_ii_relationships
3228         WHERE  (subject_id=p_instance_id OR object_id=p_instance_id)
3229         AND    relationship_type_code = 'CONNECTED-TO'
3230         AND    (active_end_date IS NULL OR active_end_date > SYSDATE);
3231    ELSIF p_mode='UPDATE'
3232    THEN
3233    -- Code for update will check other than itself
3234    -- and during unexpiring of an expired relationship.
3235         SELECT COUNT(*)
3236         INTO   l_count
3237         FROM   csi_ii_relationships
3238         WHERE  (subject_id=p_instance_id OR object_id=p_instance_id)
3239         AND    relationship_type_code = 'CONNECTED-TO'
3240         AND    relationship_id <> p_relationship_id
3241         AND    (active_end_date IS NULL OR active_end_date > SYSDATE);
3242    END IF;
3243 
3244 
3245    IF l_count >= 2
3246    THEN
3247       RETURN TRUE;
3248    ELSE
3249       RETURN FALSE;
3250    END IF;
3251 EXCEPTION
3252    WHEN OTHERS THEN
3253       RETURN FALSE;
3254 END relationship_for_link;
3255 
3256 -- This function to find it the instance is of type link
3257 FUNCTION Is_link_type
3258   ( p_instance_id          IN      NUMBER
3259   ) RETURN BOOLEAN IS
3260    l_link                        VARCHAR2(30);
3261    l_class                       VARCHAR2(30) := 'LINK';
3262    --
3263 BEGIN
3264    SELECT msi.ib_item_instance_class
3265    INTO   l_link
3266    FROM   csi_item_instances cii
3267          ,mtl_system_items_b msi
3268    WHERE  cii.instance_id = p_instance_id
3269    AND    msi.inventory_item_id=cii.inventory_item_id
3270    AND    msi.organization_id=cii.last_vld_organization_id
3271    AND    msi.ib_item_instance_class = l_class;
3272    RETURN TRUE;
3273 EXCEPTION
3274    WHEN NO_DATA_FOUND THEN
3275       RETURN FALSE;
3276 END Is_link_type;
3277 
3278 -- End of att enhancements by sguthiva
3279 /* ------------------------------------------------------------------------------------------------------*/
3280 /* during creation/updation of a relationship check for a record for passed subject_id and               */
3281 /* relationship_type_code                                                                                */
3282 /*   1. If exists then return an error message and return false else return true                         */
3283 /*                         a      sub: b ,rel code: component-of                                         */
3284 /*                        /  \    passed (b,component-of) --return false                                 */
3285 /*                       b    c   passed (b,member-of)    --return true                                  */
3286 /* ------------------------------------------------------------------------------------------------------*/
3287 -- Bug 9693832
3288 -- The fix disallows creation of duplicate relationship for object/subject
3289 -- combination
3290 
3291 FUNCTION subject_exists
3292 (       p_subject_id           IN      NUMBER,
3293         p_relship_type_code    IN      VARCHAR2,
3294         p_relationship_id      IN      NUMBER,
3295         p_mode                 IN      VARCHAR2
3296  ) RETURN BOOLEAN IS
3297 
3298  l_subject_id         NUMBER;
3299  l_relship_type_code  VARCHAR2(30);
3300  l_dummy              VARCHAR2(50) :=NULL;
3301  l_return_value       BOOLEAN := TRUE;
3302      BEGIN
3303       IF p_mode='CREATE'
3304       THEN
3305        BEGIN
3306         -- Bug 9693832
3307 
3308         --SELECT 'x'
3309         SELECT relationship_type_code
3310         INTO   l_relship_type_code
3311         FROM   csi_ii_relationships
3312         WHERE  subject_id=p_subject_id
3313         --AND    relationship_type_code = p_relship_type_code
3314         AND   (active_end_date IS NULL OR active_end_date > SYSDATE)
3315         --AND   (SYSDATE BETWEEN NVL(active_start_date, SYSDATE) AND NVL(active_end_date, SYSDATE))
3316         AND    ROWNUM=1;
3317         IF SQL%FOUND THEN
3318 
3319                    fnd_message.set_name('CSI','CSI_SUB_RELCODE_EXIST');
3320                    fnd_message.set_token('relationship_type_code',l_relship_type_code);
3321                    fnd_message.set_token('SUBJECT_ID',p_subject_id);
3322                    fnd_msg_pub.add;
3323            RETURN FALSE;
3324         END IF;
3325        EXCEPTION
3326         WHEN NO_DATA_FOUND THEN
3327             RETURN TRUE;
3328        END;
3329       ELSIF p_mode='UPDATE'
3330       THEN
3331 
3332        BEGIN
3333        -- Bug 9693832
3334 
3335         --SELECT 'x'
3336         SELECT relationship_type_code
3337         INTO   l_relship_type_code
3338         FROM   csi_ii_relationships
3339         WHERE  subject_id=p_subject_id
3340         --AND    relationship_type_code = p_relship_type_code
3341         AND    relationship_id <> p_relationship_id
3342         AND   (active_end_date IS NULL OR active_end_date > SYSDATE)
3343         --AND   (SYSDATE BETWEEN NVL(active_start_date, SYSDATE) AND NVL(active_end_date, SYSDATE))
3344         AND    ROWNUM=1;
3345         IF SQL%FOUND THEN
3346 
3347                    fnd_message.set_name('CSI','CSI_SUB_RELCODE_EXIST');
3348                    fnd_message.set_token('relationship_type_code',l_relship_type_code);
3349                    fnd_message.set_token('SUBJECT_ID',p_subject_id);
3350                    fnd_msg_pub.add;
3351            RETURN FALSE;
3352         END IF;
3353        EXCEPTION
3354         WHEN NO_DATA_FOUND THEN
3355             RETURN TRUE;
3356        END;
3357       END IF;
3358 END subject_exists;
3359 
3360 /* Cyclic Relationships */
3361 /* This function checks whether the subject already exist for the relation
3362 ship */
3363 FUNCTION subject_relship_exists
3364 (   p_relationship_id       IN      NUMBER,
3365     p_subject_id            IN      NUMBER,
3366     p_relship_type_code     IN      VARCHAR2
3367  ) RETURN BOOLEAN IS
3368     l_subject_id                    NUMBER;
3369     l_relship_type_code            VARCHAR2(30);
3370     l_dummy                        VARCHAR2(1) :=NULL;
3371 BEGIN
3372     IF p_relationship_id is not null
3373        AND(p_subject_id  is null OR p_subject_id= fnd_api.g_miss_num)
3374     THEN
3375        RETURN TRUE;
3376     ELSE
3377        SELECT subject_id,relationship_type_code
3378        INTO   l_subject_id,l_relship_type_code
3379        FROM   csi_ii_relationships
3380        WHERE  relationship_id=p_relationship_id
3381        AND    subject_id     =p_subject_id;
3382 
3383        IF SQL%FOUND THEN
3384           RETURN TRUE;
3385        ELSE
3386           RETURN FALSE;
3387        END IF;
3388     END IF;
3389 EXCEPTION
3390     WHEN NO_DATA_FOUND THEN
3391        RETURN FALSE;
3392 END subject_relship_exists;
3393 
3394 /* ---------------------------------------------------------------------------------------------- */
3395 /* during updating a relationship check for the existence of object_id or relationship_type_code  */
3396 /*  For the passed relationship_id                                                                */
3397 /*   example - relid -reltype-object-sub-TRUE                                                     */
3398 /*               1- componentof-i10-i12-exist record                                              */
3399 /*               1- componentof-i20-i13-FALSE                                                     */
3400 /*               1- memberof   -i10-i13-FALSE                                                     */
3401 /*               1- componentof-i10-i13-TRUE                                                      */
3402 /* ---------------------------------------------------------------------------------------------- */
3403 
3404 FUNCTION object_relship_exists
3405 (   p_relationship_id      IN      NUMBER,
3406     p_object_id            IN      NUMBER,
3407     p_relship_type_code    IN      VARCHAR2
3408  ) RETURN BOOLEAN IS
3409     l_object_id                    NUMBER;
3410     l_relship_type_code            VARCHAR2(30);
3411     l_dummy                        VARCHAR2(1) :=NULL;
3412 BEGIN
3413     SELECT object_id,relationship_type_code
3414     INTO   l_object_id,l_relship_type_code
3415     FROM   csi_ii_relationships
3416     WHERE  relationship_id=p_relationship_id;
3417 
3418     IF SQL%FOUND THEN
3419        IF ( (l_object_id<>p_object_id) OR (l_relship_type_code<>p_relship_type_code) ) THEN
3420           fnd_message.set_name('CSI','CSI_CANNOT_UPDATE');
3421           fnd_message.set_token('object_id',p_object_id);
3422           fnd_message.set_token('relationship_type_code',p_relship_type_code);
3423           fnd_msg_pub.add;
3424           RETURN FALSE;
3425        ELSE
3426           RETURN TRUE;
3427        END IF;
3428     END IF;
3429 EXCEPTION
3430     WHEN NO_DATA_FOUND THEN
3431        RETURN TRUE;
3432 END object_relship_exists;
3433 
3434 /* ----------------------------------------------------------------------------------------------- */
3435 /* This procedure(during creation)is used to check if the received subject is already an object    */
3436 /*  If found then raise error else success                                                         */
3437 /*                      a                                                                          */
3438 /*                     / \                                                                         */
3439 /*                    b   c                                                                        */
3440 /*                    / \                                                                          */
3441 /*                   d   a -> Not allowed                                                          */
3442 /* ----------------------------------------------------------------------------------------------- */
3443 
3444 PROCEDURE check_for_object
3445 (   p_subject_id             IN      NUMBER,
3446     p_object_id              IN      NUMBER,
3447     p_relationship_type_code IN      VARCHAR2,
3448     x_return_status          OUT NOCOPY     VARCHAR2,
3449     x_msg_count              OUT NOCOPY     NUMBER,
3450     x_msg_data               OUT NOCOPY     VARCHAR2
3451  )
3452 IS
3453  l_rel_tbl        csi_datastructures_pub.ii_relationship_tbl;
3454  p_relationship_query_rec csi_datastructures_pub.relationship_query_rec;
3455  l_ii_relationship_level_tbl csi_ii_relationships_pvt.ii_relationship_level_tbl;
3456 /* CURSOR chk_obj_csr IS
3457           SELECT 'x'
3458           FROM   csi_ii_relationships
3459           WHERE  subject_id = p_object_id
3460           AND    relationship_type_code = p_relationship_type_code
3461           START WITH object_id = p_subject_id
3462           CONNECT BY object_id = PRIOR subject_id;
3463   l_dummy VARCHAR2(1); */
3464 
3465   BEGIN
3466      x_return_status := fnd_api.g_ret_sts_success;
3467      IF p_subject_id IS NULL OR
3468         p_subject_id = FND_API.G_MISS_NUM OR
3469         p_object_id IS NULL OR
3470         p_object_id = FND_API.G_MISS_NUM THEN
3471         fnd_message.set_name('CSI', 'CSI_PARENT_CHILD_INVALID');
3472         fnd_msg_pub.add;
3473         x_return_status := fnd_api.g_ret_sts_error;
3474         RETURN;
3475      END IF;
3476      --
3477      p_relationship_query_rec.object_id := p_subject_id; -- To check for Loop
3478      p_relationship_query_rec.relationship_type_code := p_relationship_type_code;
3479      --
3480      IF p_subject_id <> p_object_id THEN
3481         csi_ii_relationships_pvt.Get_Children
3482            ( p_relationship_query_rec      => p_relationship_query_rec,
3483              p_rel_tbl                     => l_rel_tbl,
3484              p_depth                       => NULL,
3485              p_active_relationship_only    => FND_API.G_TRUE,
3486              p_active_instances_only       => FND_API.G_FALSE,
3487              p_time_stamp                  => FND_API.G_MISS_DATE,
3488              p_get_dfs                     => FND_API.G_FALSE,
3489              p_ii_relationship_level_tbl   => l_ii_relationship_level_tbl,
3490              x_return_status               => x_return_status,
3491              x_msg_count                   => x_msg_count,
3492              x_msg_data                    => x_msg_data
3493            );
3494         --
3495         IF x_return_status = FND_API.G_RET_STS_SUCCESS AND
3496            l_rel_tbl.count > 0 THEN
3497            FOR j in l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
3498               IF l_rel_tbl(j).subject_id = p_object_id THEN
3499                  fnd_message.set_name('CSI','CSI_CHILD_PARENT_REL_LOOP');
3500                  fnd_msg_pub.add;
3501                  x_return_status := fnd_api.g_ret_sts_error;
3502                  exit;
3503               END IF;
3504            END LOOP;
3505         END IF;
3506      ELSE
3507         fnd_message.set_name('CSI', 'CSI_PARENT_CHILD_INVALID');
3508         fnd_msg_pub.add;
3509         x_return_status := fnd_api.g_ret_sts_error;
3510      END IF;
3511 END check_for_object;
3512 
3513 PROCEDURE validate_history(p_old_relship_rec IN   csi_datastructures_pub.ii_relationship_rec,
3514                            p_new_relship_rec IN   csi_datastructures_pub.ii_relationship_rec,
3515                            p_transaction_id  IN   NUMBER,
3516                            p_flag            IN   VARCHAR2,
3517                            p_sysdate         IN   DATE,
3518                            x_return_status   OUT NOCOPY  VARCHAR2,
3519                            x_msg_count       OUT NOCOPY  NUMBER,
3520                            x_msg_data        OUT NOCOPY  VARCHAR2)
3521 IS
3522 l_old_relship_rec       csi_datastructures_pub.ii_relationship_rec :=p_old_relship_rec;
3523 l_new_relship_rec       csi_datastructures_pub.ii_relationship_rec :=p_new_relship_rec;
3524 l_transaction_id        NUMBER := p_transaction_id;
3525 l_full_dump             NUMBER;
3526 l_relship_hist_rec      csi_datastructures_pub.relationship_history_rec;
3527 
3528 CURSOR rel_hist_csr (p_rel_hist_id NUMBER) IS
3529 SELECT  relationship_history_id
3530        ,relationship_id
3531        ,transaction_id
3532        ,old_subject_id
3533        ,new_subject_id
3534        ,old_position_reference
3535        ,new_position_reference
3536        ,old_active_start_date
3537        ,new_active_start_date
3538        ,old_active_end_date
3539        ,new_active_end_date
3540        ,old_mandatory_flag
3541        ,new_mandatory_flag
3542        ,old_context
3543        ,new_context
3544        ,old_attribute1
3545        ,new_attribute1
3546        ,old_attribute2
3547        ,new_attribute2
3548        ,old_attribute3
3549        ,new_attribute3
3550        ,old_attribute4
3551        ,new_attribute4
3552        ,old_attribute5
3553        ,new_attribute5
3554        ,old_attribute6
3555        ,new_attribute6
3556        ,old_attribute7
3557        ,new_attribute7
3558        ,old_attribute8
3559        ,new_attribute8
3560        ,old_attribute9
3561        ,new_attribute9
3562        ,old_attribute10
3563        ,new_attribute10
3564        ,old_attribute11
3565        ,new_attribute11
3566        ,old_attribute12
3567        ,new_attribute12
3568        ,old_attribute13
3569        ,new_attribute13
3570        ,old_attribute14
3571        ,new_attribute14
3572        ,old_attribute15
3573        ,new_attribute15
3574        ,full_dump_flag
3575        ,object_version_number
3576 FROM   csi_ii_relationships_h
3577 WHERE  csi_ii_relationships_h.relationship_history_id =  p_rel_hist_id
3578 FOR UPDATE OF object_version_number;
3579 
3580 l_rel_hist_csr   rel_hist_csr%ROWTYPE;
3581 l_rel_hist_id    NUMBER;
3582 
3583 BEGIN
3584 
3585 
3586    x_return_status := fnd_api.g_ret_sts_success;
3587    --
3588    IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
3589       csi_gen_utility_pvt.populate_install_param_rec;
3590    END IF;
3591    --
3592    l_full_dump := csi_datastructures_pub.g_install_param_rec.history_full_dump_frequency;
3593    --
3594    IF l_full_dump IS NULL THEN
3595       FND_MESSAGE.SET_NAME('CSI','CSI_API_GET_FULL_DUMP_FAILED');
3596       FND_MSG_PUB.ADD;
3597       RAISE FND_API.G_EXC_ERROR;
3598    END IF;
3599    --
3600    IF p_flag = 'EXPIRE' THEN
3601       l_new_relship_rec.active_end_date := p_sysdate;
3602    END IF;
3603    -- Start of modifications for Bug#2547034 on 09/20/02 - rtalluri
3604        BEGIN
3605         SELECT  relationship_history_id
3606         INTO    l_rel_hist_id
3607         FROM    csi_ii_relationships_h h
3608         WHERE   h.transaction_id = p_transaction_id
3609         AND     h.relationship_id = p_old_relship_rec.relationship_id;
3610 
3611         OPEN   rel_hist_csr(l_rel_hist_id);
3612         FETCH  rel_hist_csr INTO l_rel_hist_csr ;
3613         CLOSE  rel_hist_csr;
3614 
3615         IF l_rel_hist_csr.full_dump_flag = 'Y'
3616         THEN
3617           csi_ii_relationships_h_pkg.update_row(
3618             p_relationship_history_id       =>  l_rel_hist_id                       ,
3619             p_relationship_id               =>  fnd_api.g_miss_num                  ,
3620             p_transaction_id                =>  fnd_api.g_miss_num                  ,
3621             p_old_subject_id                =>  fnd_api.g_miss_num                  ,
3622             p_new_subject_id                =>  l_new_relship_rec.subject_id        ,
3623             p_old_position_reference        =>  fnd_api.g_miss_char                 ,
3624             p_new_position_reference        =>  l_new_relship_rec.position_reference,
3625             p_old_active_start_date         =>  fnd_api.g_miss_date                 ,
3626             p_new_active_start_date         =>  l_new_relship_rec.active_start_date ,
3627             p_old_active_end_date           =>  fnd_api.g_miss_date                 ,
3628             p_new_active_end_date           =>  l_new_relship_rec.active_end_date   ,
3629             p_old_mandatory_flag            =>  fnd_api.g_miss_char                 ,
3630             p_new_mandatory_flag            =>  l_new_relship_rec.mandatory_flag    ,
3631             p_old_context                   =>  fnd_api.g_miss_char                 ,
3632             p_new_context                   =>  l_new_relship_rec.context           ,
3633             p_old_attribute1                =>  fnd_api.g_miss_char                 ,
3634             p_new_attribute1                =>  l_new_relship_rec.attribute1        ,
3635             p_old_attribute2                =>  fnd_api.g_miss_char                 ,
3636             p_new_attribute2                =>  l_new_relship_rec.attribute2        ,
3637             p_old_attribute3                =>  fnd_api.g_miss_char                 ,
3638             p_new_attribute3                =>  l_new_relship_rec.attribute3        ,
3639             p_old_attribute4                =>  fnd_api.g_miss_char                 ,
3640             p_new_attribute4                =>  l_new_relship_rec.attribute4        ,
3641             p_old_attribute5                =>  fnd_api.g_miss_char                 ,
3642             p_new_attribute5                =>  l_new_relship_rec.attribute5        ,
3643             p_old_attribute6                =>  fnd_api.g_miss_char                 ,
3644             p_new_attribute6                =>  l_new_relship_rec.attribute6        ,
3645             p_old_attribute7                =>  fnd_api.g_miss_char                 ,
3646             p_new_attribute7                =>  l_new_relship_rec.attribute7        ,
3647             p_old_attribute8                =>  fnd_api.g_miss_char                 ,
3648             p_new_attribute8                =>  l_new_relship_rec.attribute8        ,
3649             p_old_attribute9                =>  fnd_api.g_miss_char                 ,
3650             p_new_attribute9                =>  l_new_relship_rec.attribute9        ,
3651             p_old_attribute10               =>  fnd_api.g_miss_char                 ,
3652             p_new_attribute10               =>  l_new_relship_rec.attribute10       ,
3653             p_old_attribute11               =>  fnd_api.g_miss_char                 ,
3654             p_new_attribute11               =>  l_new_relship_rec.attribute11       ,
3655             p_old_attribute12               =>  fnd_api.g_miss_char                 ,
3656             p_new_attribute12               =>  l_new_relship_rec.attribute12       ,
3657             p_old_attribute13               =>  fnd_api.g_miss_char                 ,
3658             p_new_attribute13               =>  l_new_relship_rec.attribute13       ,
3659             p_old_attribute14               =>  fnd_api.g_miss_char                 ,
3660             p_new_attribute14               =>  l_new_relship_rec.attribute14       ,
3661             p_old_attribute15               =>  fnd_api.g_miss_char                 ,
3662             p_new_attribute15               =>  l_new_relship_rec.attribute15       ,
3663             p_full_dump_flag                =>  fnd_api.g_miss_char                 ,
3664             p_created_by                    =>  fnd_api.g_miss_num, -- fnd_global.user_id,
3665             p_creation_date                 =>  fnd_api.g_miss_date                 ,
3666             p_last_updated_by               =>  fnd_global.user_id                  ,
3667             p_last_update_date              =>  SYSDATE                             ,
3668             p_last_update_login             =>  fnd_global.conc_login_id            ,
3669             p_object_version_number         =>  fnd_api.g_miss_num );
3670         ELSE
3671           --
3672              IF    ( l_rel_hist_csr.old_subject_id IS NULL
3673                 AND  l_rel_hist_csr.new_subject_id IS NULL ) THEN
3674                      IF  ( l_new_relship_rec.subject_id = l_old_relship_rec.subject_id )
3675                       OR ( l_new_relship_rec.subject_id = fnd_api.g_miss_num ) THEN
3676                            l_rel_hist_csr.old_subject_id := NULL;
3677                            l_rel_hist_csr.new_subject_id := NULL;
3678                      ELSE
3679                            l_rel_hist_csr.old_subject_id := fnd_api.g_miss_num;
3680                            l_rel_hist_csr.new_subject_id := l_new_relship_rec.subject_id;
3681                      END IF;
3682              ELSE
3683                      l_rel_hist_csr.old_subject_id := fnd_api.g_miss_num;
3684                      l_rel_hist_csr.new_subject_id := l_new_relship_rec.subject_id;
3685              END IF;
3686           --
3687              IF    ( l_rel_hist_csr.old_position_reference IS NULL
3688                 AND  l_rel_hist_csr.new_position_reference IS NULL ) THEN
3689                      IF  ( l_new_relship_rec.position_reference = l_old_relship_rec.position_reference )
3690                       OR ( l_new_relship_rec.position_reference = fnd_api.g_miss_char ) THEN
3691                            l_rel_hist_csr.old_position_reference := NULL;
3692                            l_rel_hist_csr.new_position_reference := NULL;
3693                      ELSE
3694                            l_rel_hist_csr.old_position_reference := fnd_api.g_miss_char;
3695                            l_rel_hist_csr.new_position_reference := l_new_relship_rec.position_reference;
3696                      END IF;
3697              ELSE
3698                      l_rel_hist_csr.old_position_reference := fnd_api.g_miss_char;
3699                      l_rel_hist_csr.new_position_reference := l_new_relship_rec.position_reference;
3700              END IF;
3701           --
3702              IF    ( l_rel_hist_csr.old_active_start_date IS NULL
3703                 AND  l_rel_hist_csr.new_active_start_date IS NULL ) THEN
3704                      IF  ( l_new_relship_rec.active_start_date = l_old_relship_rec.active_start_date )
3705                       OR ( l_new_relship_rec.active_start_date = fnd_api.g_miss_date ) THEN
3706                            l_rel_hist_csr.old_active_start_date := NULL;
3707                            l_rel_hist_csr.new_active_start_date := NULL;
3708                      ELSE
3709                            l_rel_hist_csr.old_active_start_date := fnd_api.g_miss_date;
3710                            l_rel_hist_csr.new_active_start_date := l_new_relship_rec.active_start_date;
3711                      END IF;
3712              ELSE
3713                      l_rel_hist_csr.old_active_start_date := fnd_api.g_miss_date;
3714                      l_rel_hist_csr.new_active_start_date := l_new_relship_rec.active_start_date;
3715              END IF;
3716           --
3717              IF    ( l_rel_hist_csr.old_active_end_date IS NULL
3718                 AND  l_rel_hist_csr.new_active_end_date IS NULL ) THEN
3719                      IF  ( l_new_relship_rec.active_end_date = l_old_relship_rec.active_end_date )
3720                       OR ( l_new_relship_rec.active_end_date = fnd_api.g_miss_date ) THEN
3721                            l_rel_hist_csr.old_active_end_date := NULL;
3722                            l_rel_hist_csr.new_active_end_date := NULL;
3723                      ELSE
3724                            l_rel_hist_csr.old_active_end_date := fnd_api.g_miss_date;
3725                            l_rel_hist_csr.new_active_end_date := l_new_relship_rec.active_end_date;
3726                      END IF;
3727              ELSE
3728                      l_rel_hist_csr.old_active_end_date := fnd_api.g_miss_date;
3729                      l_rel_hist_csr.new_active_end_date := l_new_relship_rec.active_end_date;
3730              END IF;
3731           --
3732              IF    ( l_rel_hist_csr.old_mandatory_flag IS NULL
3733                 AND  l_rel_hist_csr.new_mandatory_flag IS NULL ) THEN
3734                      IF  ( l_new_relship_rec.mandatory_flag = l_old_relship_rec.mandatory_flag )
3735                       OR ( l_new_relship_rec.mandatory_flag = fnd_api.g_miss_char ) THEN
3736                            l_rel_hist_csr.old_mandatory_flag := NULL;
3737                            l_rel_hist_csr.new_mandatory_flag := NULL;
3738                      ELSE
3739                            l_rel_hist_csr.old_mandatory_flag := fnd_api.g_miss_char;
3740                            l_rel_hist_csr.new_mandatory_flag := l_new_relship_rec.mandatory_flag;
3741                      END IF;
3742              ELSE
3743                      l_rel_hist_csr.old_mandatory_flag := fnd_api.g_miss_char;
3744                      l_rel_hist_csr.new_mandatory_flag := l_new_relship_rec.mandatory_flag;
3745              END IF;
3746           --
3747              IF    ( l_rel_hist_csr.old_context IS NULL
3748                 AND  l_rel_hist_csr.new_context IS NULL ) THEN
3749                      IF  ( l_new_relship_rec.context = l_old_relship_rec.context )
3750                       OR ( l_new_relship_rec.context = fnd_api.g_miss_char ) THEN
3751                            l_rel_hist_csr.old_context := NULL;
3752                            l_rel_hist_csr.new_context := NULL;
3753                      ELSE
3754                            l_rel_hist_csr.old_context := fnd_api.g_miss_char;
3755                            l_rel_hist_csr.new_context := l_new_relship_rec.context;
3756                      END IF;
3757              ELSE
3758                      l_rel_hist_csr.old_context := fnd_api.g_miss_char;
3759                      l_rel_hist_csr.new_context := l_new_relship_rec.context;
3760              END IF;
3761           --
3762              IF    ( l_rel_hist_csr.old_attribute1 IS NULL
3763                 AND  l_rel_hist_csr.new_attribute1 IS NULL ) THEN
3764                      IF  ( l_new_relship_rec.attribute1 = l_old_relship_rec.attribute1 )
3765                       OR ( l_new_relship_rec.attribute1 = fnd_api.g_miss_char ) THEN
3766                            l_rel_hist_csr.old_attribute1 := NULL;
3767                            l_rel_hist_csr.new_attribute1 := NULL;
3768                      ELSE
3769                            l_rel_hist_csr.old_attribute1 := fnd_api.g_miss_char;
3770                            l_rel_hist_csr.new_attribute1 := l_new_relship_rec.attribute1;
3771                      END IF;
3772              ELSE
3773                      l_rel_hist_csr.old_attribute1 := fnd_api.g_miss_char;
3774                      l_rel_hist_csr.new_attribute1 := l_new_relship_rec.attribute1;
3775              END IF;
3776           --
3777              IF    ( l_rel_hist_csr.old_attribute2 IS NULL
3778                 AND  l_rel_hist_csr.new_attribute2 IS NULL ) THEN
3779                      IF  ( l_new_relship_rec.attribute2 = l_old_relship_rec.attribute2 )
3780                       OR ( l_new_relship_rec.attribute2 = fnd_api.g_miss_char ) THEN
3781                            l_rel_hist_csr.old_attribute2 := NULL;
3782                            l_rel_hist_csr.new_attribute2 := NULL;
3783                      ELSE
3784                            l_rel_hist_csr.old_attribute2 := fnd_api.g_miss_char;
3785                            l_rel_hist_csr.new_attribute2 := l_new_relship_rec.attribute2;
3786                      END IF;
3787              ELSE
3788                      l_rel_hist_csr.old_attribute2 := fnd_api.g_miss_char;
3789                      l_rel_hist_csr.new_attribute2 := l_new_relship_rec.attribute2;
3790              END IF;
3791           --
3792              IF    ( l_rel_hist_csr.old_attribute3 IS NULL
3793                 AND  l_rel_hist_csr.new_attribute3 IS NULL ) THEN
3794                      IF  ( l_new_relship_rec.attribute3 = l_old_relship_rec.attribute3 )
3795                       OR ( l_new_relship_rec.attribute3 = fnd_api.g_miss_char ) THEN
3796                            l_rel_hist_csr.old_attribute3 := NULL;
3797                            l_rel_hist_csr.new_attribute3 := NULL;
3798                      ELSE
3799                            l_rel_hist_csr.old_attribute3 := fnd_api.g_miss_char;
3800                            l_rel_hist_csr.new_attribute3 := l_new_relship_rec.attribute3;
3801                      END IF;
3802              ELSE
3803                      l_rel_hist_csr.old_attribute3 := fnd_api.g_miss_char;
3804                      l_rel_hist_csr.new_attribute3 := l_new_relship_rec.attribute3;
3805              END IF;
3806           --
3807              IF    ( l_rel_hist_csr.old_attribute4 IS NULL
3808                 AND  l_rel_hist_csr.new_attribute4 IS NULL ) THEN
3809                      IF  ( l_new_relship_rec.attribute4 = l_old_relship_rec.attribute4 )
3810                       OR ( l_new_relship_rec.attribute4 = fnd_api.g_miss_char ) THEN
3811                            l_rel_hist_csr.old_attribute4 := NULL;
3812                            l_rel_hist_csr.new_attribute4 := NULL;
3813                      ELSE
3814                            l_rel_hist_csr.old_attribute4 := fnd_api.g_miss_char;
3815                            l_rel_hist_csr.new_attribute4 := l_new_relship_rec.attribute4;
3816                      END IF;
3817              ELSE
3818                      l_rel_hist_csr.old_attribute4 := fnd_api.g_miss_char;
3819                      l_rel_hist_csr.new_attribute4 := l_new_relship_rec.attribute4;
3820              END IF;
3821           --
3822              IF    ( l_rel_hist_csr.old_attribute5 IS NULL
3823                 AND  l_rel_hist_csr.new_attribute5 IS NULL ) THEN
3824                      IF  ( l_new_relship_rec.attribute5 = l_old_relship_rec.attribute5 )
3825                       OR ( l_new_relship_rec.attribute5 = fnd_api.g_miss_char ) THEN
3826                            l_rel_hist_csr.old_attribute5 := NULL;
3827                            l_rel_hist_csr.new_attribute5 := NULL;
3828                      ELSE
3829                            l_rel_hist_csr.old_attribute5 := fnd_api.g_miss_char;
3830                            l_rel_hist_csr.new_attribute5 := l_new_relship_rec.attribute5;
3831                      END IF;
3832              ELSE
3833                      l_rel_hist_csr.old_attribute5 := fnd_api.g_miss_char;
3834                      l_rel_hist_csr.new_attribute5 := l_new_relship_rec.attribute5;
3835              END IF;
3836           --
3837              IF    ( l_rel_hist_csr.old_attribute6 IS NULL
3838                 AND  l_rel_hist_csr.new_attribute6 IS NULL ) THEN
3839                      IF  ( l_new_relship_rec.attribute6 = l_old_relship_rec.attribute6 )
3840                       OR ( l_new_relship_rec.attribute6 = fnd_api.g_miss_char ) THEN
3841                            l_rel_hist_csr.old_attribute6 := NULL;
3842                            l_rel_hist_csr.new_attribute6 := NULL;
3843                      ELSE
3844                            l_rel_hist_csr.old_attribute6 := fnd_api.g_miss_char;
3845                            l_rel_hist_csr.new_attribute6 := l_new_relship_rec.attribute6;
3846                      END IF;
3847              ELSE
3848                      l_rel_hist_csr.old_attribute6 := fnd_api.g_miss_char;
3849                      l_rel_hist_csr.new_attribute6 := l_new_relship_rec.attribute6;
3850              END IF;
3851           --
3852              IF    ( l_rel_hist_csr.old_attribute7 IS NULL
3853                 AND  l_rel_hist_csr.new_attribute7 IS NULL ) THEN
3854                      IF  ( l_new_relship_rec.attribute7 = l_old_relship_rec.attribute7 )
3855                       OR ( l_new_relship_rec.attribute7 = fnd_api.g_miss_char ) THEN
3856                            l_rel_hist_csr.old_attribute7 := NULL;
3857                            l_rel_hist_csr.new_attribute7 := NULL;
3858                      ELSE
3859                            l_rel_hist_csr.old_attribute7 := fnd_api.g_miss_char;
3860                            l_rel_hist_csr.new_attribute7 := l_new_relship_rec.attribute7;
3861                      END IF;
3862              ELSE
3863                      l_rel_hist_csr.old_attribute7 := fnd_api.g_miss_char;
3864                      l_rel_hist_csr.new_attribute7 := l_new_relship_rec.attribute7;
3865              END IF;
3866           --
3867              IF    ( l_rel_hist_csr.old_attribute8 IS NULL
3868                 AND  l_rel_hist_csr.new_attribute8 IS NULL ) THEN
3869                      IF  ( l_new_relship_rec.attribute8 = l_old_relship_rec.attribute8 )
3870                       OR ( l_new_relship_rec.attribute8 = fnd_api.g_miss_char ) THEN
3871                            l_rel_hist_csr.old_attribute8 := NULL;
3872                            l_rel_hist_csr.new_attribute8 := NULL;
3873                      ELSE
3874                            l_rel_hist_csr.old_attribute8 := fnd_api.g_miss_char;
3875                            l_rel_hist_csr.new_attribute8 := l_new_relship_rec.attribute8;
3876                      END IF;
3877              ELSE
3878                      l_rel_hist_csr.old_attribute8 := fnd_api.g_miss_char;
3879                      l_rel_hist_csr.new_attribute8 := l_new_relship_rec.attribute8;
3880              END IF;
3881           --
3882              IF    ( l_rel_hist_csr.old_attribute9 IS NULL
3883                 AND  l_rel_hist_csr.new_attribute9 IS NULL ) THEN
3884                      IF  ( l_new_relship_rec.attribute9 = l_old_relship_rec.attribute9 )
3885                       OR ( l_new_relship_rec.attribute9 = fnd_api.g_miss_char ) THEN
3886                            l_rel_hist_csr.old_attribute9 := NULL;
3887                            l_rel_hist_csr.new_attribute9 := NULL;
3888                      ELSE
3889                            l_rel_hist_csr.old_attribute9 := fnd_api.g_miss_char;
3890                            l_rel_hist_csr.new_attribute9 := l_new_relship_rec.attribute9;
3891                      END IF;
3892              ELSE
3893                      l_rel_hist_csr.old_attribute9 := fnd_api.g_miss_char;
3894                      l_rel_hist_csr.new_attribute9 := l_new_relship_rec.attribute9;
3895              END IF;
3896           --
3897              IF    ( l_rel_hist_csr.old_attribute10 IS NULL
3898                 AND  l_rel_hist_csr.new_attribute10 IS NULL ) THEN
3899                      IF  ( l_new_relship_rec.attribute10 = l_old_relship_rec.attribute10 )
3900                       OR ( l_new_relship_rec.attribute10 = fnd_api.g_miss_char ) THEN
3901                            l_rel_hist_csr.old_attribute10 := NULL;
3902                            l_rel_hist_csr.new_attribute10 := NULL;
3903                      ELSE
3904                            l_rel_hist_csr.old_attribute10 := fnd_api.g_miss_char;
3905                            l_rel_hist_csr.new_attribute10 := l_new_relship_rec.attribute10;
3906                      END IF;
3907              ELSE
3908                      l_rel_hist_csr.old_attribute10 := fnd_api.g_miss_char;
3909                      l_rel_hist_csr.new_attribute10 := l_new_relship_rec.attribute10;
3910              END IF;
3911           --
3912              IF    ( l_rel_hist_csr.old_attribute11 IS NULL
3913                 AND  l_rel_hist_csr.new_attribute11 IS NULL ) THEN
3914                      IF  ( l_new_relship_rec.attribute11 = l_old_relship_rec.attribute11 )
3915                       OR ( l_new_relship_rec.attribute11 = fnd_api.g_miss_char ) THEN
3916                            l_rel_hist_csr.old_attribute11 := NULL;
3917                            l_rel_hist_csr.new_attribute11 := NULL;
3918                      ELSE
3919                            l_rel_hist_csr.old_attribute11 := fnd_api.g_miss_char;
3920                            l_rel_hist_csr.new_attribute11 := l_new_relship_rec.attribute11;
3921                      END IF;
3922              ELSE
3923                      l_rel_hist_csr.old_attribute11 := fnd_api.g_miss_char;
3924                      l_rel_hist_csr.new_attribute11 := l_new_relship_rec.attribute11;
3925              END IF;
3926           --
3927              IF    ( l_rel_hist_csr.old_attribute12 IS NULL
3928                 AND  l_rel_hist_csr.new_attribute12 IS NULL ) THEN
3929                      IF  ( l_new_relship_rec.attribute12 = l_old_relship_rec.attribute12 )
3930                       OR ( l_new_relship_rec.attribute12 = fnd_api.g_miss_char ) THEN
3931                            l_rel_hist_csr.old_attribute12 := NULL;
3932                            l_rel_hist_csr.new_attribute12 := NULL;
3933                      ELSE
3934                            l_rel_hist_csr.old_attribute12 := fnd_api.g_miss_char;
3935                            l_rel_hist_csr.new_attribute12 := l_new_relship_rec.attribute12;
3936                      END IF;
3937              ELSE
3938                      l_rel_hist_csr.old_attribute12 := fnd_api.g_miss_char;
3939                      l_rel_hist_csr.new_attribute12 := l_new_relship_rec.attribute12;
3940              END IF;
3941           --
3942              IF    ( l_rel_hist_csr.old_attribute13 IS NULL
3943                 AND  l_rel_hist_csr.new_attribute13 IS NULL ) THEN
3944                      IF  ( l_new_relship_rec.attribute13 = l_old_relship_rec.attribute13 )
3945                       OR ( l_new_relship_rec.attribute13 = fnd_api.g_miss_char ) THEN
3946                            l_rel_hist_csr.old_attribute13 := NULL;
3947                            l_rel_hist_csr.new_attribute13 := NULL;
3948                      ELSE
3949                            l_rel_hist_csr.old_attribute13 := fnd_api.g_miss_char;
3950                            l_rel_hist_csr.new_attribute13 := l_new_relship_rec.attribute13;
3951                      END IF;
3952              ELSE
3953                      l_rel_hist_csr.old_attribute13 := fnd_api.g_miss_char;
3954                      l_rel_hist_csr.new_attribute13 := l_new_relship_rec.attribute13;
3955              END IF;
3956           --
3957              IF    ( l_rel_hist_csr.old_attribute14 IS NULL
3958                 AND  l_rel_hist_csr.new_attribute14 IS NULL ) THEN
3959                      IF  ( l_new_relship_rec.attribute14 = l_old_relship_rec.attribute14 )
3960                       OR ( l_new_relship_rec.attribute14 = fnd_api.g_miss_char ) THEN
3961                            l_rel_hist_csr.old_attribute14 := NULL;
3962                            l_rel_hist_csr.new_attribute14 := NULL;
3963                      ELSE
3964                            l_rel_hist_csr.old_attribute14 := fnd_api.g_miss_char;
3965                            l_rel_hist_csr.new_attribute14 := l_new_relship_rec.attribute14;
3966                      END IF;
3967              ELSE
3968                      l_rel_hist_csr.old_attribute14 := fnd_api.g_miss_char;
3969                      l_rel_hist_csr.new_attribute14 := l_new_relship_rec.attribute14;
3970              END IF;
3971           --
3972              IF    ( l_rel_hist_csr.old_attribute15 IS NULL
3973                 AND  l_rel_hist_csr.new_attribute15 IS NULL ) THEN
3974                      IF  ( l_new_relship_rec.attribute15 = l_old_relship_rec.attribute15 )
3975                       OR ( l_new_relship_rec.attribute15 = fnd_api.g_miss_char ) THEN
3976                            l_rel_hist_csr.old_attribute15 := NULL;
3977                            l_rel_hist_csr.new_attribute15 := NULL;
3978                      ELSE
3979                            l_rel_hist_csr.old_attribute15 := fnd_api.g_miss_char;
3980                            l_rel_hist_csr.new_attribute15 := l_new_relship_rec.attribute15;
3981                      END IF;
3982              ELSE
3983                      l_rel_hist_csr.old_attribute15 := fnd_api.g_miss_char;
3984                      l_rel_hist_csr.new_attribute15 := l_new_relship_rec.attribute15;
3985              END IF;
3986           --
3987 
3988            csi_ii_relationships_h_pkg.update_row(
3989             p_relationship_history_id       =>  l_rel_hist_id                        ,
3990             p_relationship_id               =>  fnd_api.g_miss_num                   ,
3991             p_transaction_id                =>  fnd_api.g_miss_num                   ,
3992             p_old_subject_id                =>  l_rel_hist_csr.old_subject_id        ,
3993             p_new_subject_id                =>  l_rel_hist_csr.new_subject_id        ,
3994             p_old_position_reference        =>  l_rel_hist_csr.old_position_reference,
3995             p_new_position_reference        =>  l_rel_hist_csr.new_position_reference,
3996             p_old_active_start_date         =>  l_rel_hist_csr.old_active_start_date ,
3997             p_new_active_start_date         =>  l_rel_hist_csr.new_active_start_date ,
3998             p_old_active_end_date           =>  l_rel_hist_csr.old_active_end_date   ,
3999             p_new_active_end_date           =>  l_rel_hist_csr.new_active_end_date   ,
4000             p_old_mandatory_flag            =>  l_rel_hist_csr.old_mandatory_flag    ,
4001             p_new_mandatory_flag            =>  l_rel_hist_csr.new_mandatory_flag    ,
4002             p_old_context                   =>  l_rel_hist_csr.old_context           ,
4003             p_new_context                   =>  l_rel_hist_csr.new_context           ,
4004             p_old_attribute1                =>  l_rel_hist_csr.old_attribute1        ,
4005             p_new_attribute1                =>  l_rel_hist_csr.new_attribute1        ,
4006             p_old_attribute2                =>  l_rel_hist_csr.old_attribute2        ,
4007             p_new_attribute2                =>  l_rel_hist_csr.new_attribute2        ,
4008             p_old_attribute3                =>  l_rel_hist_csr.old_attribute3        ,
4009             p_new_attribute3                =>  l_rel_hist_csr.new_attribute3        ,
4010             p_old_attribute4                =>  l_rel_hist_csr.old_attribute4        ,
4011             p_new_attribute4                =>  l_rel_hist_csr.new_attribute4        ,
4012             p_old_attribute5                =>  l_rel_hist_csr.old_attribute5        ,
4013             p_new_attribute5                =>  l_rel_hist_csr.new_attribute5        ,
4014             p_old_attribute6                =>  l_rel_hist_csr.old_attribute6        ,
4015             p_new_attribute6                =>  l_rel_hist_csr.new_attribute6        ,
4016             p_old_attribute7                =>  l_rel_hist_csr.old_attribute7        ,
4017             p_new_attribute7                =>  l_rel_hist_csr.new_attribute7        ,
4018             p_old_attribute8                =>  l_rel_hist_csr.old_attribute8        ,
4019             p_new_attribute8                =>  l_rel_hist_csr.new_attribute8        ,
4020             p_old_attribute9                =>  l_rel_hist_csr.old_attribute9        ,
4021             p_new_attribute9                =>  l_rel_hist_csr.new_attribute9        ,
4022             p_old_attribute10               =>  l_rel_hist_csr.old_attribute10       ,
4023             p_new_attribute10               =>  l_rel_hist_csr.new_attribute10       ,
4024             p_old_attribute11               =>  l_rel_hist_csr.old_attribute11       ,
4025             p_new_attribute11               =>  l_rel_hist_csr.new_attribute11       ,
4026             p_old_attribute12               =>  l_rel_hist_csr.old_attribute12       ,
4027             p_new_attribute12               =>  l_rel_hist_csr.new_attribute12       ,
4028             p_old_attribute13               =>  l_rel_hist_csr.old_attribute13       ,
4029             p_new_attribute13               =>  l_rel_hist_csr.new_attribute13       ,
4030             p_old_attribute14               =>  l_rel_hist_csr.old_attribute14       ,
4031             p_new_attribute14               =>  l_rel_hist_csr.new_attribute14       ,
4032             p_old_attribute15               =>  l_rel_hist_csr.old_attribute15       ,
4033             p_new_attribute15               =>  l_rel_hist_csr.new_attribute15       ,
4034             p_full_dump_flag                =>  fnd_api.g_miss_char                  ,
4035             p_created_by                    =>  fnd_api.g_miss_num                   ,
4036             p_creation_date                 =>  fnd_api.g_miss_date                  ,
4037             p_last_updated_by               =>  fnd_global.user_id                   ,
4038             p_last_update_date              =>  SYSDATE                              ,
4039             p_last_update_login             =>  fnd_global.conc_login_id             ,
4040             p_object_version_number         =>  fnd_api.g_miss_num );
4041          END IF;
4042 
4043        EXCEPTION
4044          WHEN NO_DATA_FOUND THEN
4045          IF MOD(l_old_relship_rec.object_version_number+1,l_full_dump)=0 THEN
4046 
4047            csi_ii_relationships_h_pkg.insert_row(
4048             px_relationship_history_id      =>  l_relship_hist_rec.relationship_history_id,
4049             p_relationship_id               =>  l_old_relship_rec.relationship_id,
4050             p_transaction_id                =>  l_transaction_id,
4051             p_old_subject_id                =>  l_old_relship_rec.subject_id,
4052             p_new_subject_id                =>  l_new_relship_rec.subject_id,
4053             p_old_position_reference        =>  l_old_relship_rec.position_reference,
4054             p_new_position_reference        =>  l_new_relship_rec.position_reference,
4055             p_old_active_start_date         =>  l_old_relship_rec.active_start_date,
4056             p_new_active_start_date         =>  l_new_relship_rec.active_start_date,
4057             p_old_active_end_date           =>  l_old_relship_rec.active_end_date,
4058             p_new_active_end_date           =>  l_new_relship_rec.active_end_date,
4059             p_old_mandatory_flag            =>  l_old_relship_rec.mandatory_flag,
4060             p_new_mandatory_flag            =>  l_new_relship_rec.mandatory_flag,
4061             p_old_context                   =>  l_old_relship_rec.context,
4062             p_new_context                   =>  l_new_relship_rec.context,
4063             p_old_attribute1                =>  l_old_relship_rec.attribute1,
4064             p_new_attribute1                =>  l_new_relship_rec.attribute1,
4065             p_old_attribute2                =>  l_old_relship_rec.attribute2,
4066             p_new_attribute2                =>  l_new_relship_rec.attribute2,
4067             p_old_attribute3                =>  l_old_relship_rec.attribute3,
4068             p_new_attribute3                =>  l_new_relship_rec.attribute3,
4069             p_old_attribute4                =>  l_old_relship_rec.attribute4,
4070             p_new_attribute4                =>  l_new_relship_rec.attribute4,
4071             p_old_attribute5                =>  l_old_relship_rec.attribute5,
4072             p_new_attribute5                =>  l_new_relship_rec.attribute5,
4073             p_old_attribute6                =>  l_old_relship_rec.attribute6,
4074             p_new_attribute6                =>  l_new_relship_rec.attribute6,
4075             p_old_attribute7                =>  l_old_relship_rec.attribute7,
4076             p_new_attribute7                =>  l_new_relship_rec.attribute7,
4077             p_old_attribute8                =>  l_old_relship_rec.attribute8,
4078             p_new_attribute8                =>  l_new_relship_rec.attribute8,
4079             p_old_attribute9                =>  l_old_relship_rec.attribute9,
4080             p_new_attribute9                =>  l_new_relship_rec.attribute9,
4081             p_old_attribute10               =>  l_old_relship_rec.attribute10,
4082             p_new_attribute10               =>  l_new_relship_rec.attribute10,
4083             p_old_attribute11               =>  l_old_relship_rec.attribute11,
4084             p_new_attribute11               =>  l_new_relship_rec.attribute11,
4085             p_old_attribute12               =>  l_old_relship_rec.attribute12,
4086             p_new_attribute12               =>  l_new_relship_rec.attribute12,
4087             p_old_attribute13               =>  l_old_relship_rec.attribute13,
4088             p_new_attribute13               =>  l_new_relship_rec.attribute13,
4089             p_old_attribute14               =>  l_old_relship_rec.attribute14,
4090             p_new_attribute14               =>  l_new_relship_rec.attribute14,
4091             p_old_attribute15               =>  l_old_relship_rec.attribute15,
4092             p_new_attribute15               =>  l_new_relship_rec.attribute15,
4093             p_full_dump_flag                =>  'Y',
4094             p_created_by                    =>  fnd_global.user_id,
4095             p_creation_date                 =>  SYSDATE,
4096             p_last_updated_by               =>  fnd_global.user_id,
4097             p_last_update_date              =>  SYSDATE,
4098             p_last_update_login             =>  fnd_global.conc_login_id,
4099             p_object_version_number         =>  1);
4100 
4101          ELSE
4102 
4103           IF (l_new_relship_rec.subject_id = fnd_api.g_miss_num) OR
4104               NVL(l_old_relship_rec.subject_id,fnd_api.g_miss_num) = NVL(l_new_relship_rec.subject_id,fnd_api.g_miss_num) THEN
4105           --Modified code for bug 8516781, FP bug 8551918 old and new Subject_ID are always populated in CSI_II_RELATIONSHIPS_H such that
4106                --the parent's history always shows the subject ID which modified
4107                l_relship_hist_rec.old_subject_id := l_old_relship_rec.subject_id;
4108                l_relship_hist_rec.new_subject_id := l_old_relship_rec.subject_id;
4109           ELSIF
4110               NVL(l_old_relship_rec.subject_id,fnd_api.g_miss_num) <> NVL(l_new_relship_rec.subject_id,fnd_api.g_miss_num) THEN
4111                l_relship_hist_rec.old_subject_id := l_old_relship_rec.subject_id ;
4112                l_relship_hist_rec.new_subject_id := l_new_relship_rec.subject_id ;
4113           END IF;
4114           --
4115           IF (l_new_relship_rec.position_reference = fnd_api.g_miss_char) OR
4116               NVL(l_old_relship_rec.position_reference,fnd_api.g_miss_char) = NVL(l_new_relship_rec.position_reference,fnd_api.g_miss_char) THEN
4117                l_relship_hist_rec.old_position_reference := NULL;
4118                l_relship_hist_rec.new_position_reference := NULL;
4119           ELSIF
4120               NVL(l_old_relship_rec.position_reference,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.position_reference,fnd_api.g_miss_char) THEN
4121                l_relship_hist_rec.old_position_reference := l_old_relship_rec.position_reference ;
4122                l_relship_hist_rec.new_position_reference := l_new_relship_rec.position_reference ;
4123           END IF;
4124           --
4125           IF (l_new_relship_rec.active_start_date = fnd_api.g_miss_date) OR
4126               NVL(l_old_relship_rec.active_start_date,fnd_api.g_miss_date) = NVL(l_new_relship_rec.active_start_date,fnd_api.g_miss_date) THEN
4127                l_relship_hist_rec.old_active_start_date := NULL;
4128                l_relship_hist_rec.new_active_start_date := NULL;
4129           ELSIF
4130               NVL(l_old_relship_rec.active_start_date,fnd_api.g_miss_date) <> NVL(l_new_relship_rec.active_start_date,fnd_api.g_miss_date) THEN
4131                l_relship_hist_rec.old_active_start_date := l_old_relship_rec.active_start_date ;
4132                l_relship_hist_rec.new_active_start_date := l_new_relship_rec.active_start_date ;
4133           END IF;
4134           --
4135           IF (l_new_relship_rec.active_end_date = fnd_api.g_miss_date) OR
4136               NVL(l_old_relship_rec.active_end_date,fnd_api.g_miss_date) = NVL(l_new_relship_rec.active_end_date,fnd_api.g_miss_date) THEN
4137                l_relship_hist_rec.old_active_end_date := NULL;
4138                l_relship_hist_rec.new_active_end_date := NULL;
4139           ELSIF
4140               NVL(l_old_relship_rec.active_end_date,fnd_api.g_miss_date) <> NVL(l_new_relship_rec.active_end_date,fnd_api.g_miss_date) THEN
4141                l_relship_hist_rec.old_active_end_date := l_old_relship_rec.active_end_date ;
4142                l_relship_hist_rec.new_active_end_date := l_new_relship_rec.active_end_date ;
4143           END IF;
4144           --
4145           IF (l_new_relship_rec.mandatory_flag = fnd_api.g_miss_char) OR
4146               NVL(l_old_relship_rec.mandatory_flag,fnd_api.g_miss_char) = NVL(l_new_relship_rec.mandatory_flag,fnd_api.g_miss_char) THEN
4147                l_relship_hist_rec.old_mandatory_flag := NULL;
4148                l_relship_hist_rec.new_mandatory_flag := NULL;
4149           ELSIF
4150               NVL(l_old_relship_rec.mandatory_flag,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.mandatory_flag,fnd_api.g_miss_char) THEN
4151                l_relship_hist_rec.old_mandatory_flag := l_old_relship_rec.mandatory_flag ;
4152                l_relship_hist_rec.new_mandatory_flag := l_new_relship_rec.mandatory_flag ;
4153           END IF;
4154           --
4155           IF (l_new_relship_rec.context = fnd_api.g_miss_char) OR
4156               NVL(l_old_relship_rec.context,fnd_api.g_miss_char) = NVL(l_new_relship_rec.context,fnd_api.g_miss_char) THEN
4157                l_relship_hist_rec.old_context := NULL;
4158                l_relship_hist_rec.new_context := NULL;
4159           ELSIF
4160               NVL(l_old_relship_rec.context,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.context,fnd_api.g_miss_char) THEN
4161                l_relship_hist_rec.old_context := l_old_relship_rec.context ;
4162                l_relship_hist_rec.new_context := l_new_relship_rec.context ;
4163           END IF;
4164           --
4165           IF (l_new_relship_rec.attribute1 = fnd_api.g_miss_char) OR
4166               NVL(l_old_relship_rec.attribute1,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute1,fnd_api.g_miss_char) THEN
4167                l_relship_hist_rec.old_attribute1 := NULL;
4168                l_relship_hist_rec.new_attribute1 := NULL;
4169           ELSIF
4170               NVL(l_old_relship_rec.attribute1,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute1,fnd_api.g_miss_char) THEN
4171                l_relship_hist_rec.old_attribute1 := l_old_relship_rec.attribute1 ;
4172                l_relship_hist_rec.new_attribute1 := l_new_relship_rec.attribute1 ;
4173           END IF;
4174           --
4175           IF (l_new_relship_rec.attribute2 = fnd_api.g_miss_char) OR
4176               NVL(l_old_relship_rec.attribute2,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute2,fnd_api.g_miss_char) THEN
4177                l_relship_hist_rec.old_attribute2 := NULL;
4178                l_relship_hist_rec.new_attribute2 := NULL;
4179           ELSIF
4180               NVL(l_old_relship_rec.attribute2,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute2,fnd_api.g_miss_char) THEN
4181                l_relship_hist_rec.old_attribute2 := l_old_relship_rec.attribute2 ;
4182                l_relship_hist_rec.new_attribute2 := l_new_relship_rec.attribute2 ;
4183           END IF;
4184           --
4185           IF (l_new_relship_rec.attribute3 = fnd_api.g_miss_char) OR
4186               NVL(l_old_relship_rec.attribute3,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute3,fnd_api.g_miss_char) THEN
4187                l_relship_hist_rec.old_attribute3 := NULL;
4188                l_relship_hist_rec.new_attribute3 := NULL;
4189           ELSIF
4190               NVL(l_old_relship_rec.attribute3,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute3,fnd_api.g_miss_char) THEN
4191                l_relship_hist_rec.old_attribute3 := l_old_relship_rec.attribute3 ;
4192                l_relship_hist_rec.new_attribute3 := l_new_relship_rec.attribute3 ;
4193           END IF;
4194           --
4195           IF (l_new_relship_rec.attribute4 = fnd_api.g_miss_char) OR
4196               NVL(l_old_relship_rec.attribute4,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute4,fnd_api.g_miss_char) THEN
4197                l_relship_hist_rec.old_attribute4 := NULL;
4198                l_relship_hist_rec.new_attribute4 := NULL;
4199           ELSIF
4200               NVL(l_old_relship_rec.attribute4,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute4,fnd_api.g_miss_char) THEN
4201                l_relship_hist_rec.old_attribute4 := l_old_relship_rec.attribute4 ;
4202                l_relship_hist_rec.new_attribute4 := l_new_relship_rec.attribute4 ;
4203           END IF;
4204           --
4205           IF (l_new_relship_rec.attribute5 = fnd_api.g_miss_char) OR
4206               NVL(l_old_relship_rec.attribute5,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute5,fnd_api.g_miss_char) THEN
4207                l_relship_hist_rec.old_attribute5 := NULL;
4208                l_relship_hist_rec.new_attribute5 := NULL;
4209           ELSIF
4210               NVL(l_old_relship_rec.attribute5,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute5,fnd_api.g_miss_char) THEN
4211                l_relship_hist_rec.old_attribute5 := l_old_relship_rec.attribute5 ;
4212                l_relship_hist_rec.new_attribute5 := l_new_relship_rec.attribute5 ;
4213           END IF;
4214           --
4215           IF (l_new_relship_rec.attribute6 = fnd_api.g_miss_char) OR
4216               NVL(l_old_relship_rec.attribute6,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute6,fnd_api.g_miss_char) THEN
4217                l_relship_hist_rec.old_attribute6 := NULL;
4218                l_relship_hist_rec.new_attribute6 := NULL;
4219           ELSIF
4220               NVL(l_old_relship_rec.attribute6,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute6,fnd_api.g_miss_char) THEN
4221                l_relship_hist_rec.old_attribute6 := l_old_relship_rec.attribute6 ;
4222                l_relship_hist_rec.new_attribute6 := l_new_relship_rec.attribute6 ;
4223           END IF;
4224           --
4225           IF (l_new_relship_rec.attribute7 = fnd_api.g_miss_char) OR
4226               NVL(l_old_relship_rec.attribute7,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute7,fnd_api.g_miss_char) THEN
4227                l_relship_hist_rec.old_attribute7 := NULL;
4228                l_relship_hist_rec.new_attribute7 := NULL;
4229           ELSIF
4230               NVL(l_old_relship_rec.attribute7,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute7,fnd_api.g_miss_char) THEN
4231                l_relship_hist_rec.old_attribute7 := l_old_relship_rec.attribute7 ;
4232                l_relship_hist_rec.new_attribute7 := l_new_relship_rec.attribute7 ;
4233           END IF;
4234           --
4235           IF (l_new_relship_rec.attribute8 = fnd_api.g_miss_char) OR
4236               NVL(l_old_relship_rec.attribute8,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute8,fnd_api.g_miss_char) THEN
4237                l_relship_hist_rec.old_attribute8 := NULL;
4238                l_relship_hist_rec.new_attribute8 := NULL;
4239           ELSIF
4240               NVL(l_old_relship_rec.attribute8,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute8,fnd_api.g_miss_char) THEN
4241                l_relship_hist_rec.old_attribute8 := l_old_relship_rec.attribute8 ;
4242                l_relship_hist_rec.new_attribute8 := l_new_relship_rec.attribute8 ;
4243           END IF;
4244           --
4245           IF (l_new_relship_rec.attribute9 = fnd_api.g_miss_char) OR
4246               NVL(l_old_relship_rec.attribute9,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute9,fnd_api.g_miss_char) THEN
4247                l_relship_hist_rec.old_attribute9 := NULL;
4248                l_relship_hist_rec.new_attribute9 := NULL;
4249           ELSIF
4250               NVL(l_old_relship_rec.attribute9,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute9,fnd_api.g_miss_char) THEN
4251                l_relship_hist_rec.old_attribute9 := l_old_relship_rec.attribute9 ;
4252                l_relship_hist_rec.new_attribute9 := l_new_relship_rec.attribute9 ;
4253           END IF;
4254           --
4255           IF (l_new_relship_rec.attribute10 = fnd_api.g_miss_char) OR
4256               NVL(l_old_relship_rec.attribute10,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute10,fnd_api.g_miss_char) THEN
4257                l_relship_hist_rec.old_attribute10 := NULL;
4258                l_relship_hist_rec.new_attribute10 := NULL;
4259           ELSIF
4260               NVL(l_old_relship_rec.attribute10,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute10,fnd_api.g_miss_char) THEN
4261                l_relship_hist_rec.old_attribute10 := l_old_relship_rec.attribute10 ;
4262                l_relship_hist_rec.new_attribute10 := l_new_relship_rec.attribute10 ;
4263           END IF;
4264           --
4265           IF (l_new_relship_rec.attribute11 = fnd_api.g_miss_char) OR
4266               NVL(l_old_relship_rec.attribute11,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute11,fnd_api.g_miss_char) THEN
4267                l_relship_hist_rec.old_attribute11 := NULL;
4268                l_relship_hist_rec.new_attribute11 := NULL;
4269           ELSIF
4270               NVL(l_old_relship_rec.attribute11,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute11,fnd_api.g_miss_char) THEN
4271                l_relship_hist_rec.old_attribute11 := l_old_relship_rec.attribute11 ;
4272                l_relship_hist_rec.new_attribute11 := l_new_relship_rec.attribute11 ;
4273           END IF;
4274           --
4275           IF (l_new_relship_rec.attribute12 = fnd_api.g_miss_char) OR
4276               NVL(l_old_relship_rec.attribute12,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute12,fnd_api.g_miss_char) THEN
4277                l_relship_hist_rec.old_attribute12 := NULL;
4278                l_relship_hist_rec.new_attribute12 := NULL;
4279           ELSIF
4280               NVL(l_old_relship_rec.attribute12,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute12,fnd_api.g_miss_char) THEN
4281                l_relship_hist_rec.old_attribute12 := l_old_relship_rec.attribute12 ;
4282                l_relship_hist_rec.new_attribute12 := l_new_relship_rec.attribute12 ;
4283           END IF;
4284           --
4285           IF (l_new_relship_rec.attribute13 = fnd_api.g_miss_char) OR
4286               NVL(l_old_relship_rec.attribute13,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute13,fnd_api.g_miss_char) THEN
4287                l_relship_hist_rec.old_attribute13 := NULL;
4288                l_relship_hist_rec.new_attribute13 := NULL;
4289           ELSIF
4290               NVL(l_old_relship_rec.attribute13,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute13,fnd_api.g_miss_char) THEN
4291                l_relship_hist_rec.old_attribute13 := l_old_relship_rec.attribute13 ;
4292                l_relship_hist_rec.new_attribute13 := l_new_relship_rec.attribute13 ;
4293           END IF;
4294           --
4295           IF (l_new_relship_rec.attribute14 = fnd_api.g_miss_char) OR
4296               NVL(l_old_relship_rec.attribute14,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute14,fnd_api.g_miss_char) THEN
4297                l_relship_hist_rec.old_attribute14 := NULL;
4298                l_relship_hist_rec.new_attribute14 := NULL;
4299           ELSIF
4300               NVL(l_old_relship_rec.attribute14,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute14,fnd_api.g_miss_char) THEN
4301                l_relship_hist_rec.old_attribute14 := l_old_relship_rec.attribute14 ;
4302                l_relship_hist_rec.new_attribute14 := l_new_relship_rec.attribute14 ;
4303           END IF;
4304           --
4305           IF (l_new_relship_rec.attribute15 = fnd_api.g_miss_char) OR
4306               NVL(l_old_relship_rec.attribute15,fnd_api.g_miss_char) = NVL(l_new_relship_rec.attribute15,fnd_api.g_miss_char) THEN
4307                l_relship_hist_rec.old_attribute15 := NULL;
4308                l_relship_hist_rec.new_attribute15 := NULL;
4309           ELSIF
4310               NVL(l_old_relship_rec.attribute15,fnd_api.g_miss_char) <> NVL(l_new_relship_rec.attribute15,fnd_api.g_miss_char) THEN
4311                l_relship_hist_rec.old_attribute15 := l_old_relship_rec.attribute15 ;
4312                l_relship_hist_rec.new_attribute15 := l_new_relship_rec.attribute15 ;
4313           END IF;
4314           --
4315           IF p_flag = 'EXPIRE'
4316           THEN
4317              l_relship_hist_rec.new_active_end_date := p_sysdate;
4318           END IF;
4319 
4320         csi_ii_relationships_h_pkg.insert_row(
4321             px_relationship_history_id      =>  l_relship_hist_rec.relationship_history_id,
4322             p_relationship_id               =>  l_old_relship_rec.relationship_id,
4323             p_transaction_id                =>  l_transaction_id,
4324             p_old_subject_id                =>  l_relship_hist_rec.old_subject_id,
4325             p_new_subject_id                =>  l_relship_hist_rec.new_subject_id,
4326             p_old_position_reference        =>  l_relship_hist_rec.old_position_reference,
4327             p_new_position_reference        =>  l_relship_hist_rec.new_position_reference,
4328             p_old_active_start_date         =>  l_relship_hist_rec.old_active_start_date,
4329             p_new_active_start_date         =>  l_relship_hist_rec.new_active_start_date,
4330             p_old_active_end_date           =>  l_relship_hist_rec.old_active_end_date,
4331             p_new_active_end_date           =>  l_relship_hist_rec.new_active_end_date,
4332             p_old_mandatory_flag            =>  l_relship_hist_rec.old_mandatory_flag,
4333             p_new_mandatory_flag            =>  l_relship_hist_rec.new_mandatory_flag,
4334             p_old_context                   =>  l_relship_hist_rec.old_context,
4335             p_new_context                   =>  l_relship_hist_rec.new_context,
4336             p_old_attribute1                =>  l_relship_hist_rec.old_attribute1,
4337             p_new_attribute1                =>  l_relship_hist_rec.new_attribute1,
4338             p_old_attribute2                =>  l_relship_hist_rec.old_attribute2,
4339             p_new_attribute2                =>  l_relship_hist_rec.new_attribute2,
4340             p_old_attribute3                =>  l_relship_hist_rec.old_attribute3,
4341             p_new_attribute3                =>  l_relship_hist_rec.new_attribute3,
4342             p_old_attribute4                =>  l_relship_hist_rec.old_attribute4,
4343             p_new_attribute4                =>  l_relship_hist_rec.new_attribute4,
4344             p_old_attribute5                =>  l_relship_hist_rec.old_attribute5,
4345             p_new_attribute5                =>  l_relship_hist_rec.new_attribute5,
4346             p_old_attribute6                =>  l_relship_hist_rec.old_attribute6,
4347             p_new_attribute6                =>  l_relship_hist_rec.new_attribute6,
4348             p_old_attribute7                =>  l_relship_hist_rec.old_attribute7,
4349             p_new_attribute7                =>  l_relship_hist_rec.new_attribute7,
4350             p_old_attribute8                =>  l_relship_hist_rec.old_attribute8,
4351             p_new_attribute8                =>  l_relship_hist_rec.new_attribute8,
4352             p_old_attribute9                =>  l_relship_hist_rec.old_attribute9,
4353             p_new_attribute9                =>  l_relship_hist_rec.new_attribute9,
4354             p_old_attribute10               =>  l_relship_hist_rec.old_attribute10,
4355             p_new_attribute10               =>  l_relship_hist_rec.new_attribute10,
4356             p_old_attribute11               =>  l_relship_hist_rec.old_attribute11,
4357             p_new_attribute11               =>  l_relship_hist_rec.new_attribute11,
4358             p_old_attribute12               =>  l_relship_hist_rec.old_attribute12,
4359             p_new_attribute12               =>  l_relship_hist_rec.new_attribute12,
4360             p_old_attribute13               =>  l_relship_hist_rec.old_attribute13,
4361             p_new_attribute13               =>  l_relship_hist_rec.new_attribute13,
4362             p_old_attribute14               =>  l_relship_hist_rec.old_attribute14,
4363             p_new_attribute14               =>  l_relship_hist_rec.new_attribute14,
4364             p_old_attribute15               =>  l_relship_hist_rec.old_attribute15,
4365             p_new_attribute15               =>  l_relship_hist_rec.new_attribute15,
4366             p_full_dump_flag                =>  NULL,
4367             p_created_by                    =>  fnd_global.user_id,
4368             p_creation_date                 =>  SYSDATE,
4369             p_last_updated_by               =>  fnd_global.user_id,
4370             p_last_update_date              =>  SYSDATE,
4371             p_last_update_login             =>  fnd_global.conc_login_id,
4372             p_object_version_number         =>  1);
4373          END IF;
4374        END;
4375       -- End of modifications for Bug#2547034 on 09/20/02 - rtalluri
4376 EXCEPTION
4377    WHEN OTHERS THEN
4378      x_return_status := fnd_api.g_ret_sts_error;
4379 END;
4380 /* ----------------------------------------------------------------------------------------------------------+
4381 | Case1. Create/Update Relationship                                                                          |
4382 |        Only 'COMPONENT-OF' If subject_id not null and subject_id's creation_complete_flag                  |
4383 |    is 'Y' and relationship is mandatory then set object_id's completeness_flag to 'Y'                      |
4384 |                                                                                                            |
4385 | Case2. Location attribute inheritance property.                                                            |
4386 |    Create/Update Relationship                                                                              |                                                                                           |
4387 |    Only for 'COMPONENT-OF' relationship_type_code update location of subject_id and its children in        |
4388 |   'COMPONENT-OF' relationship with location of object_id.                                                  |
4389 |                                                                                                            |
4390 |                                                                                                            |
4391 +------------------------------------------------------------------------------------------------------------*/
4392 PROCEDURE update_instance
4393 (   p_api_version                IN   NUMBER,
4394     p_commit                     IN   VARCHAR2,
4395     p_init_msg_list              IN   VARCHAR2,
4396     p_validation_level           IN   NUMBER,
4397     p_ii_relationship_rec        IN   csi_datastructures_pub.ii_relationship_rec,
4398     p_txn_rec                    IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
4399     p_mode                       IN   VARCHAR2,
4400     x_return_status              OUT NOCOPY  VARCHAR2,
4401     x_msg_count                  OUT NOCOPY  NUMBER,
4402     x_msg_data                   OUT NOCOPY  VARCHAR2)
4403 IS
4404 
4405 /* CURSOR completeness_csr (p_subject_id IN NUMBER) IS
4406     SELECT  object_id
4407     FROM    csi_ii_relationships
4408     START WITH subject_id = p_subject_id
4409     CONNECT BY subject_id = PRIOR object_id; */
4410 
4411 l_ii_relationship_rec        csi_datastructures_pub.ii_relationship_rec;
4412 l_ext_attrib_values_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
4413 l_party_tbl                  csi_datastructures_pub.party_tbl;
4414 l_account_tbl                csi_datastructures_pub.party_account_tbl;
4415 l_pricing_attrib_tbl         csi_datastructures_pub.pricing_attribs_tbl;
4416 l_org_assignments_tbl        csi_datastructures_pub.organization_units_tbl;
4417 l_asset_assignment_tbl       csi_datastructures_pub.instance_asset_tbl;
4418 l_instance_id_lst            csi_datastructures_pub.id_tbl;
4419 l_instance_rec               csi_datastructures_pub.instance_rec;
4420 l_instance_rec1              csi_datastructures_pub.instance_rec;
4421 l_temp_ins_rec               csi_datastructures_pub.instance_rec;
4422 l_dummy                      NUMBER;
4423 l_object_version             NUMBER;
4424 l_object_version1            NUMBER;
4425 l_subject_id                 NUMBER;
4426 l_item_attribute_tbl         csi_item_instance_pvt.item_attribute_tbl;
4427 l_location_tbl               csi_item_instance_pvt.location_tbl;
4428 l_generic_id_tbl             csi_item_instance_pvt.generic_id_tbl;
4429 l_lookup_tbl                 csi_item_instance_pvt.lookup_tbl;
4430 l_ins_count_rec              csi_item_instance_pvt.ins_count_rec;
4431 l_rel_tbl                    csi_datastructures_pub.ii_relationship_tbl;
4432 px_oks_txn_inst_tbl          oks_ibint_pub.txn_instance_tbl;
4433 px_child_inst_tbl            csi_item_instance_grp.child_inst_tbl;
4434 -- Begin Add Code for Siebel Genesis Project
4435 p_ext_attrib_values_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
4436 p_party_tbl                  csi_datastructures_pub.party_tbl;
4437 p_account_tbl                csi_datastructures_pub.party_account_tbl;
4438 p_pricing_attrib_tbl         csi_datastructures_pub.pricing_attribs_tbl;
4439 p_org_assignments_tbl        csi_datastructures_pub.organization_units_tbl;
4440 p_asset_assignment_tbl       csi_datastructures_pub.instance_asset_tbl;
4441 -- End Add Code for Siebel Genesis Project
4442 
4443 BEGIN
4444 
4445        x_return_status := fnd_api.g_ret_sts_success;
4446        l_ii_relationship_rec := p_ii_relationship_rec;
4447        --
4448        IF l_ii_relationship_rec.relationship_id IS NOT NULL AND
4449           l_ii_relationship_rec.relationship_id <> FND_API.G_MISS_NUM THEN
4450           IF l_ii_relationship_rec.subject_id IS NULL OR
4451              l_ii_relationship_rec.subject_id = FND_API.G_MISS_NUM THEN
4452              Begin
4453                 select subject_id
4454                 into l_ii_relationship_rec.subject_id
4455                 from CSI_II_RELATIONSHIPS
4456                 where relationship_id = l_ii_relationship_rec.relationship_id;
4457              End;
4458           END IF;
4459           --
4460           IF l_ii_relationship_rec.object_id IS NULL OR
4461              l_ii_relationship_rec.object_id = FND_API.G_MISS_NUM THEN
4462              Begin
4463                 select object_id
4464                 into l_ii_relationship_rec.object_id
4465                 from CSI_II_RELATIONSHIPS
4466                 where relationship_id = l_ii_relationship_rec.relationship_id;
4467              End;
4468           END IF;
4469        END IF;
4470        --
4471 --To handle Case 2
4472       IF l_ii_relationship_rec.relationship_type_code='COMPONENT-OF' THEN
4473         BEGIN
4474            l_instance_rec.instance_id := l_ii_relationship_rec.subject_id;
4475                 SELECT object_version_number,
4476                        config_inst_hdr_id,
4477                        config_inst_item_id,
4478                        config_inst_rev_num
4479                 INTO   l_object_version1,
4480                        l_instance_rec.config_inst_hdr_id,  -- added
4481                        l_instance_rec.config_inst_item_id, -- added
4482                        l_instance_rec.config_inst_rev_num  -- added
4483                 FROM   csi_item_instances
4484                 WHERE  instance_id=l_ii_relationship_rec.subject_id;
4485                 l_instance_rec.object_version_number:=l_object_version1;
4486              IF p_mode='CREATE' OR p_mode='UPDATE'
4487              THEN
4488                 l_instance_rec.instance_usage_code :='IN_RELATIONSHIP';
4489              END IF;
4490                 SELECT active_end_date,
4491 		       --instance_status_id, --added for bug7164722  --later commented for Bug 13831400
4492                        location_type_code,
4493                        location_id,
4494                        inv_organization_id,
4495                        inv_subinventory_name,
4496                        inv_locator_id,
4497                        pa_project_id,
4498                        pa_project_task_id,
4499                        in_transit_order_line_id,
4500                        wip_job_id,
4501                        po_order_line_id,
4502                        operational_status_code,
4503                        install_location_id,
4504                        install_location_type_code
4505                 INTO   l_instance_rec.active_end_date,
4506 		       --l_instance_rec.instance_status_id, --added for bug7164722  --later commented for Bug 13831400
4507                        l_instance_rec.location_type_code,
4508                        l_instance_rec.location_id,
4509                        l_instance_rec.inv_organization_id,
4510                        l_instance_rec.inv_subinventory_name,
4511                        l_instance_rec.inv_locator_id,
4512                        l_instance_rec.pa_project_id,
4513                        l_instance_rec.pa_project_task_id,
4514                        l_instance_rec.in_transit_order_line_id,
4515                        l_instance_rec.wip_job_id,
4516                        l_instance_rec.po_order_line_id,
4517                        l_instance_rec.operational_status_code,
4518                        l_instance_rec.install_location_id,
4519                        l_instance_rec.install_location_type_code
4520                 FROM   csi_item_instances
4521                 WHERE  instance_id=l_ii_relationship_rec.object_id;
4522 
4523 				--Added for Bug 13831400 -- modified for bug 14004522
4524 				IF(p_txn_rec.TRANSACTION_TYPE_ID = 73 OR nvl(FND_PROFILE.VALUE('CSI_CASCADE_STATUS'), 'Y') <> 'N') THEN
4525 					SELECT instance_status_id
4526 					INTO l_instance_rec.instance_status_id
4527 	                FROM   csi_item_instances
4528 	                WHERE  instance_id=l_ii_relationship_rec.object_id;
4529 				END IF;
4530 
4531                 -- Begin Add Code for Siebel Genesis Project
4532                 csi_item_instance_pub.update_item_instance
4533                  (   p_api_version             =>  p_api_version
4534                     ,p_commit                  =>  p_commit
4535                     ,p_init_msg_list           =>  p_init_msg_list
4536                     ,p_validation_level        =>  p_validation_level
4537                     ,p_instance_rec            =>  l_instance_rec
4538                     ,p_ext_attrib_values_tbl   =>  p_ext_attrib_values_tbl
4539                     ,p_party_tbl               =>  p_party_tbl
4540                     ,p_account_tbl             =>  p_account_tbl
4541                     ,p_pricing_attrib_tbl      =>  p_pricing_attrib_tbl
4542                     ,p_org_assignments_tbl     =>  p_org_assignments_tbl
4543                     ,p_asset_assignment_tbl    =>  p_asset_assignment_tbl
4544                     ,p_txn_rec                 =>  p_txn_rec
4545                     ,x_instance_id_lst         =>  l_instance_id_lst
4546                     ,x_return_status           =>  x_return_status
4547                     ,x_msg_count               =>  x_msg_count
4548                     ,x_msg_data                =>  x_msg_data
4549                 );
4550               /*csi_item_instance_pvt.update_item_instance
4551                  (   p_api_version             =>  p_api_version
4552                     ,p_commit                  =>  p_commit
4553                     ,p_init_msg_list           =>  p_init_msg_list
4554                     ,p_validation_level        =>  p_validation_level
4555                     ,p_instance_rec            =>  l_instance_rec
4556                     ,p_txn_rec                 =>  p_txn_rec
4557                     ,x_instance_id_lst         =>  l_instance_id_lst
4558                     ,x_return_status           =>  x_return_status
4559                     ,x_msg_count               =>  x_msg_count
4560                     ,x_msg_data                =>  x_msg_data
4561                     ,p_item_attribute_tbl      =>  l_item_attribute_tbl
4562                     ,p_location_tbl            =>  l_location_tbl
4563                     ,p_generic_id_tbl          =>  l_generic_id_tbl
4564                     ,p_lookup_tbl              =>  l_lookup_tbl
4565                     ,p_ins_count_rec           =>  l_ins_count_rec
4566                     ,p_oks_txn_inst_tbl        =>  px_oks_txn_inst_tbl
4567                     ,p_child_inst_tbl          =>  px_child_inst_tbl
4568                 );*/
4569                 -- End Add Code for Siebel Genesis Project
4570 
4571           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
4572                         fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
4573                         fnd_message.set_token('instance_id',l_instance_rec.instance_id);
4574                         fnd_msg_pub.add;
4575                     RAISE fnd_api.g_exc_error;
4576           END IF;
4577 
4578          EXCEPTION
4579          WHEN OTHERS THEN
4580             NULL;
4581          END;
4582 
4583        END IF;
4584 
4585 --To handle Case 1
4586       BEGIN
4587          --FOR update_instance_csr IN completeness_csr(l_ii_relationship_rec.subject_id)
4588          csi_ii_relationships_pvt.Get_Immediate_Parents
4589             ( p_subject_id        => l_ii_relationship_rec.subject_id,
4590               p_rel_type_code     => 'COMPONENT-OF',
4591               p_rel_tbl           => l_rel_tbl
4592             );
4593          IF l_rel_tbl.count > 0 THEN
4594             FOR j in l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
4595                l_subject_id:=NULL;
4596                BEGIN
4597                   SELECT subject_id
4598                   INTO   l_subject_id
4599                   FROM   csi_ii_relationships
4600                   WHERE  object_id = l_rel_tbl(j).object_id
4601                   AND    mandatory_flag = 'Y'
4602                   AND    relationship_type_code='COMPONENT-OF'
4603                   AND    ROWNUM=1;
4604                EXCEPTION
4605                   WHEN OTHERS THEN
4606                      l_subject_id:=NULL;
4607                END;
4608                --
4609                IF l_subject_id IS NOT NULL
4610                THEN
4611                   SELECT COUNT(*)
4612                   INTO   l_dummy
4613                   FROM   csi_item_instances
4614                   WHERE  instance_id IN (  SELECT subject_id
4615                                            FROM   csi_ii_relationships
4616                                            WHERE  object_id = l_rel_tbl(j).object_id
4617                                            AND    mandatory_flag = 'Y'
4618                                            AND    relationship_type_code='COMPONENT-OF'
4619                                         )
4620                   AND    creation_complete_flag = 'N';
4621 
4622                   IF nvl(l_dummy,fnd_api.g_miss_num) > 0 THEN
4623                      EXIT;
4624                   ELSE
4625                      SELECT object_version_number,
4626                             config_inst_hdr_id, --added
4627                             config_inst_item_id, --added
4628                             config_inst_rev_num
4629                      INTO   l_object_version,
4630                             l_instance_rec1.config_inst_hdr_id, --added
4631                             l_instance_rec1.config_inst_item_id, --added
4632                             l_instance_rec1.config_inst_rev_num --added
4633                      FROM   csi_item_instances
4634                      WHERE  instance_id = l_rel_tbl(j).object_id;
4635 
4636                      l_instance_rec1:=l_temp_ins_rec;
4637                      l_instance_rec1.object_version_number:=l_object_version;
4638                      l_instance_rec1.instance_id:=l_rel_tbl(j).object_id;
4639                      l_instance_rec1.completeness_flag:='Y';
4640 
4641                      csi_item_instance_pvt.update_item_instance
4642                         (   p_api_version             =>  p_api_version
4643                            ,p_commit                  =>  p_commit
4644                            ,p_init_msg_list           =>  p_init_msg_list
4645                            ,p_validation_level        =>  p_validation_level
4646                            ,p_instance_rec            =>  l_instance_rec1
4647                            ,p_txn_rec                 =>  p_txn_rec
4648                            ,x_instance_id_lst         =>  l_instance_id_lst
4649                            ,x_return_status           =>  x_return_status
4650                            ,x_msg_count               =>  x_msg_count
4651                            ,x_msg_data                =>  x_msg_data
4652                            ,p_item_attribute_tbl      =>  l_item_attribute_tbl
4653                            ,p_location_tbl            =>  l_location_tbl
4654                            ,p_generic_id_tbl          =>  l_generic_id_tbl
4655                            ,p_lookup_tbl              =>  l_lookup_tbl
4656                            ,p_ins_count_rec           =>  l_ins_count_rec
4657                            ,p_oks_txn_inst_tbl        =>  px_oks_txn_inst_tbl
4658                            ,p_child_inst_tbl          =>  px_child_inst_tbl
4659                        );
4660 
4661                      IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
4662                         fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
4663                         fnd_message.set_token('instance_id',l_instance_rec1.instance_id);
4664                         fnd_msg_pub.add;
4665                         RAISE fnd_api.g_exc_error;
4666                      END IF;
4667 
4668                   END IF;
4669                END IF;
4670             END LOOP;
4671          END IF; -- l_rel_tbl count check
4672       EXCEPTION
4673          WHEN OTHERS THEN
4674            NULL;
4675       END;
4676 END update_instance;
4677 
4678 -- Added by sk on 9-Apr-02 for bug 2304221
4679 /* ----------------------------------------------------------------------------------------------------------+
4680 |        During creation of a relationship between 2 instances then parent owner party and owner account(in  |
4681 |        case of external) has to inherited to the children.                                                 |
4682 |        Code is applied only for 'COMPONENT-OF' relationship_type_code                                      |                                                                                                            |
4683 +------------------------------------------------------------------------------------------------------------*/
4684 
4685 PROCEDURE update_party_account
4686 (   p_api_version                IN   NUMBER,
4687     p_commit                     IN   VARCHAR2,
4688     p_init_msg_list              IN   VARCHAR2,
4689     p_validation_level           IN   NUMBER,
4690     p_ii_relationship_rec        IN   csi_datastructures_pub.ii_relationship_rec,
4691     p_txn_rec                    IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
4692     x_return_status              OUT NOCOPY  VARCHAR2,
4693     x_msg_count                  OUT NOCOPY  NUMBER,
4694     x_msg_data                   OUT NOCOPY  VARCHAR2)
4695 IS
4696 l_ext_attrib_values_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
4697 l_party_tbl                  csi_datastructures_pub.party_tbl;
4698 l_account_tbl                csi_datastructures_pub.party_account_tbl;
4699 l_pricing_attrib_tbl         csi_datastructures_pub.pricing_attribs_tbl;
4700 l_org_assignments_tbl        csi_datastructures_pub.organization_units_tbl;
4701 l_asset_assignment_tbl       csi_datastructures_pub.instance_asset_tbl;
4702 l_instance_id_lst            csi_datastructures_pub.id_tbl;
4703 l_instance_rec               csi_datastructures_pub.instance_rec;
4704 l_subject_id                 NUMBER;
4705 l_record_found               VARCHAR2(1):= fnd_api.g_true;
4706 BEGIN
4707  IF p_ii_relationship_rec.relationship_type_code='COMPONENT-OF' THEN
4708 
4709       BEGIN
4710             SELECT  cp.party_id
4711                    ,cp.party_source_table
4712                    ,cp.contact_flag
4713                    ,cp.relationship_type_code
4714                    ,ca.party_account_id
4715                    ,ca.relationship_type_code
4716             INTO    l_party_tbl(1).party_id
4717                    ,l_party_tbl(1).party_source_table
4718                    ,l_party_tbl(1).contact_flag
4719                    ,l_party_tbl(1).relationship_type_code
4720                    ,l_account_tbl(1).party_account_id
4721                    ,l_account_tbl(1).relationship_type_code
4722             FROM   csi_i_parties cp,
4723                    csi_ip_accounts ca
4724             WHERE  cp.instance_id = p_ii_relationship_rec.object_id
4725             AND    cp.instance_party_id = ca.instance_party_id
4726             AND    cp.relationship_type_code = 'OWNER'
4727             AND    cp.relationship_type_code = ca.relationship_type_code
4728             AND    (cp.active_end_date IS NULL OR cp.active_end_date > SYSDATE)
4729             AND    (ca.active_end_date IS NULL OR ca.active_end_date > SYSDATE);
4730       EXCEPTION
4731         WHEN OTHERS THEN
4732          l_record_found := fnd_api.g_false;
4733       END;
4734 
4735          BEGIN
4736             SELECT  instance_party_id
4737                    ,object_version_number
4738             INTO    l_party_tbl(1).instance_party_id
4739                    ,l_party_tbl(1).object_version_number
4740             FROM    csi_i_parties
4741             WHERE   instance_id = p_ii_relationship_rec.subject_id
4742             AND     relationship_type_code = 'OWNER';
4743             l_account_tbl(1).instance_party_id := l_party_tbl(1).instance_party_id;
4744         EXCEPTION
4745           WHEN OTHERS THEN
4746             NULL;
4747         END;
4748 
4749       IF l_record_found = fnd_api.g_true
4750       THEN
4751         BEGIN
4752             SELECT  instance_id
4753                    ,object_version_number
4754             INTO    l_instance_rec.instance_id
4755                    ,l_instance_rec.object_version_number
4756             FROM    csi_item_instances
4757             WHERE   instance_id = p_ii_relationship_rec.subject_id;
4758 
4759             l_party_tbl(1).instance_id := p_ii_relationship_rec.subject_id;
4760             l_account_tbl(1).parent_tbl_index := 1;
4761         EXCEPTION
4762           WHEN OTHERS THEN
4763             l_record_found := fnd_api.g_false;
4764         END;
4765       END IF;
4766 
4767 
4768         IF l_record_found = fnd_api.g_true
4769         THEN
4770                  csi_item_instance_pub.update_item_instance
4771                           ( p_api_version           =>  p_api_version
4772                            ,p_commit                =>  p_commit
4773                            ,p_init_msg_list         =>  p_init_msg_list
4774                            ,p_validation_level      =>  p_validation_level
4775                            ,p_instance_rec          =>  l_instance_rec
4776                            ,p_ext_attrib_values_tbl =>  l_ext_attrib_values_tbl
4777                            ,p_party_tbl             =>  l_party_tbl
4778                            ,p_account_tbl           =>  l_account_tbl
4779                            ,p_pricing_attrib_tbl    =>  l_pricing_attrib_tbl
4780                            ,p_org_assignments_tbl   =>  l_org_assignments_tbl
4781                            ,p_asset_assignment_tbl  =>  l_asset_assignment_tbl
4782                            ,p_txn_rec               =>  p_txn_rec
4783                            ,x_instance_id_lst       =>  l_instance_id_lst
4784                            ,x_return_status         =>  x_return_status
4785                            ,x_msg_count             =>  x_msg_count
4786                            ,x_msg_data              =>  x_msg_data
4787                            );
4788 
4789                 IF NOT(x_return_status = fnd_api.g_ret_sts_success)
4790                 THEN
4791                      fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
4792                      fnd_message.set_token('instance_id',l_instance_rec.instance_id);
4793                      fnd_msg_pub.add;
4794                     RAISE fnd_api.g_exc_error;
4795                 END IF;
4796         END IF;
4797  END IF;
4798 END update_party_account;
4799 
4800 -- End addition by sk on 9-Apr-02 for bug 2304221
4801 
4802 
4803 -- hint: primary key needs TO be returned.
4804 PROCEDURE create_relationship(
4805     p_api_version                IN   NUMBER,
4806     p_commit                     IN   VARCHAR2,
4807     p_init_msg_list              IN   VARCHAR2,
4808     p_validation_level           IN   NUMBER,
4809     p_relationship_tbl        IN OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
4810     p_txn_rec                    IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
4811     x_return_status              OUT NOCOPY  VARCHAR2,
4812     x_msg_count                  OUT NOCOPY  NUMBER,
4813     x_msg_data                   OUT NOCOPY  VARCHAR2
4814     )
4815  IS
4816 
4817 
4818 l_api_name                   CONSTANT VARCHAR2(30) := 'create_relationship';
4819 l_api_version                CONSTANT NUMBER       := 1.0;
4820 l_return_status_full                  VARCHAR2(1);
4821 l_access_flag                         VARCHAR2(1);
4822 l_ii_relationship_rec                 csi_datastructures_pub.ii_relationship_rec;
4823 l_line_count                          NUMBER;
4824 l_relationship_id                     NUMBER;
4825 l_debug_level                         NUMBER;
4826 l_relship_history_id                  NUMBER;
4827 l_msg_data                            VARCHAR2(2000);  -- Added by sguthiva for bug 2373109
4828 l_msg_index                           NUMBER;          -- Added by sguthiva for bug 2373109
4829 l_msg_count                           NUMBER;          -- Added by sguthiva for bug 2373109
4830 l_exists                              VARCHAR2(1);
4831 
4832 -- Added for cascade ownership change bug 2972082
4833 l_ext_attrib_values_tbl               csi_datastructures_pub.extend_attrib_values_tbl;
4834 l_party_tbl                           csi_datastructures_pub.party_tbl;
4835 l_account_tbl                         csi_datastructures_pub.party_account_tbl;
4836 l_temp_party_tbl                      csi_datastructures_pub.party_tbl;
4837 l_temp_account_tbl                    csi_datastructures_pub.party_account_tbl;
4838 l_pricing_attrib_tbl                  csi_datastructures_pub.pricing_attribs_tbl;
4839 l_org_assignments_tbl                 csi_datastructures_pub.organization_units_tbl;
4840 l_asset_assignment_tbl                csi_datastructures_pub.instance_asset_tbl;
4841 l_instance_id_lst                     csi_datastructures_pub.id_tbl;
4842 l_instance_rec                        csi_datastructures_pub.instance_rec;
4843 l_cascade_instance_rec                csi_datastructures_pub.instance_rec;
4844 -- End addition for cascade ownership changes bug 2972082
4845 
4846  BEGIN
4847       -- standard start of api savepoint
4848       SAVEPOINT create_relationship_pvt;
4849 
4850       -- standard call to check for call compatibility.
4851       IF NOT fnd_api.compatible_api_call ( l_api_version,
4852                                            p_api_version,
4853                                            l_api_name,
4854                                            g_pkg_name)
4855       THEN
4856           RAISE fnd_api.g_exc_unexpected_error;
4857       END IF;
4858 
4859 
4860       -- initialize message list if p_init_msg_list is set to true.
4861       IF fnd_api.to_boolean( p_init_msg_list )
4862       THEN
4863           fnd_msg_pub.initialize;
4864       END IF;
4865 
4866       -- initialize api return status to success
4867       x_return_status := fnd_api.g_ret_sts_success;
4868 
4869       l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
4870     IF (l_debug_level > 0) THEN
4871           CSI_gen_utility_pvt.put_line( 'create_relationship');
4872     END IF;
4873 
4874     IF (l_debug_level > 1) THEN
4875 
4876 
4877              CSI_gen_utility_pvt.put_line(
4878                             p_api_version             ||'-'||
4879                             p_commit                  ||'-'||
4880                             p_init_msg_list           ||'-'||
4881                             p_validation_level        );
4882 
4883          csi_gen_utility_pvt.dump_rel_tbl(p_relationship_tbl);
4884          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
4885 
4886     END IF;
4887 
4888       -- invoke validation procedures
4889       validate_ii_relationships(
4890           p_init_msg_list           => fnd_api.g_false,
4891           p_validation_level        => p_validation_level,
4892           p_validation_mode         => 'CREATE',
4893           p_ii_relationship_tbl     => p_relationship_tbl,
4894           x_return_status           => x_return_status,
4895           x_msg_count               => x_msg_count,
4896           x_msg_data                => x_msg_data);
4897 
4898 
4899       IF x_return_status<>fnd_api.g_ret_sts_success THEN
4900           RAISE fnd_api.g_exc_error;
4901       END IF;
4902 
4903 
4904 
4905         l_line_count := p_relationship_tbl.count;
4906 
4907   FOR l_count IN 1..l_line_count LOOP
4908       -- check for infinite loop; Added for bug 10081206
4909     IF (p_relationship_tbl(l_count).subject_id = p_relationship_tbl(l_count).object_id) THEN
4910           FND_MESSAGE.SET_NAME('CSI','CSI_RELATIONSHIP_LOOP');
4911           FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_relationship_tbl(l_count).object_id);
4912           FND_MSG_PUB.ADD;
4913           RAISE fnd_api.g_exc_error;
4914     END IF;
4915 
4916     IF ( (p_relationship_tbl(l_count).mandatory_flag IS NULL) OR
4917          (p_relationship_tbl(l_count).mandatory_flag = fnd_api.g_miss_char) ) THEN
4918              p_relationship_tbl(l_count).mandatory_flag:='N';
4919     END IF;
4920 
4921         --Added for MACD lock functionality
4922         IF p_relationship_tbl(l_count).object_id IS NOT NULL AND
4923            p_relationship_tbl(l_count).object_id <> fnd_api.g_miss_num
4924         THEN
4925            IF csi_item_instance_pvt.check_item_instance_lock
4926                                       ( p_instance_id => p_relationship_tbl(l_count).object_id)
4927            THEN
4928             IF p_txn_rec.transaction_type_id NOT IN (51,53,54,401)
4929             THEN
4930              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
4931              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_relationship_tbl(l_count).object_id);
4932              FND_MSG_PUB.ADD;
4933              RAISE FND_API.G_EXC_ERROR;
4934             END IF;
4935            END IF;
4936         END IF;
4937         IF p_relationship_tbl(l_count).subject_id IS NOT NULL AND
4938            p_relationship_tbl(l_count).subject_id <> fnd_api.g_miss_num
4939         THEN
4940            IF csi_item_instance_pvt.check_item_instance_lock
4941                                       ( p_instance_id => p_relationship_tbl(l_count).subject_id)
4942            THEN
4943             IF p_txn_rec.transaction_type_id NOT IN (51,53,54,401)
4944             THEN
4945              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
4946              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_relationship_tbl(l_count).subject_id);
4947              FND_MSG_PUB.ADD;
4948              RAISE FND_API.G_EXC_ERROR;
4949             END IF;
4950            END IF;
4951         END IF;
4952         -- End addition for MACD lock functionality
4953 
4954 
4955     /* Added for cyclic relationships to skip validations for 'CONNECTED-TO' */
4956     IF p_relationship_tbl(l_count).relationship_type_code <> 'CONNECTED-TO'
4957     THEN
4958        IF subject_exists(p_subject_id        => p_relationship_tbl(l_count).subject_id,
4959                          p_relship_type_code => p_relationship_tbl(l_count).relationship_type_code,
4960                          p_relationship_id   => Null,
4961                          p_mode              => 'CREATE' )
4962        THEN
4963           x_return_status:=fnd_api.g_ret_sts_success;
4964        ELSE
4965           x_return_status:=fnd_api.g_ret_sts_error;
4966           RAISE fnd_api.g_exc_error;
4967        END IF;
4968 
4969        IF ((x_return_status=fnd_api.g_ret_sts_success) AND
4970            (p_relationship_tbl(l_count).object_id IS NOT NULL) ) THEN
4971            csi_ii_relationships_pvt.check_for_object
4972             (p_subject_id  =>p_relationship_tbl(l_count).subject_id,
4973               p_object_id              =>p_relationship_tbl(l_count).object_id,
4974               p_relationship_type_code =>p_relationship_tbl(l_count).relationship_type_code,
4975               x_return_status          =>x_return_status,
4976               x_msg_count              =>x_msg_count,
4977               x_msg_data               =>x_msg_data
4978            );
4979          IF x_return_status<>fnd_api.g_ret_sts_success THEN
4980             RAISE fnd_api.g_exc_error;
4981          END IF;
4982        END IF;
4983     END IF; /* end of additional for cyclic relationship */
4984     /* Another one for cyclic relationship */
4985     /* added this to aviod creating 'CONNECTED-TO' relationship in the reverse direction */
4986    IF p_relationship_tbl(l_count).relationship_type_code='CONNECTED-TO' THEN
4987       IF relationship_not_exists(p_relationship_tbl(l_count).subject_id,
4988                                  p_relationship_tbl(l_count).object_id,
4989                                  p_relationship_tbl(l_count).relationship_type_code,
4990                                  'CREATE',
4991                                  fnd_api.g_miss_num)
4992       THEN
4993          x_return_status:=fnd_api.g_ret_sts_success;
4994       ELSE
4995          x_return_status:=fnd_api.g_ret_sts_error;
4996          RAISE fnd_api.g_exc_error;
4997       END IF;
4998       -- Start of att enhancements by sguthiva
4999       IF csi_ii_relationships_pvt.Is_link_type (p_instance_id => p_relationship_tbl(l_count).object_id )
5000       THEN
5001 	 IF csi_ii_relationships_pvt.relationship_for_link
5002                                   ( p_instance_id     => p_relationship_tbl(l_count).object_id
5003 				   ,p_mode            => 'CREATE'
5004 				   ,p_relationship_id => NULL )
5005 	 THEN
5006 	   fnd_message.set_name('CSI','CSI_LINK_EXISTS');
5007 	   fnd_message.set_token('INSTANCE_ID',p_relationship_tbl(l_count).object_id);
5008 	   fnd_msg_pub.add;
5009 	   RAISE fnd_api.g_exc_error;
5010 	 END IF;
5011       END IF;
5012 
5013       IF csi_ii_relationships_pvt.Is_link_type (p_instance_id => p_relationship_tbl(l_count).subject_id )
5014       THEN
5015 	 IF csi_ii_relationships_pvt.relationship_for_link
5016                                   ( p_instance_id     => p_relationship_tbl(l_count).subject_id
5017 				   ,p_mode            => 'CREATE'
5018 				   ,p_relationship_id => NULL )
5019 	 THEN
5020 	   fnd_message.set_name('CSI','CSI_LINK_EXISTS');
5021 	   fnd_message.set_token('INSTANCE_ID',p_relationship_tbl(l_count).subject_id);
5022 	   fnd_msg_pub.add;
5023 	   RAISE fnd_api.g_exc_error;
5024 	 END IF;
5025       END IF;
5026       -- End of att enhancements by sguthiva
5027    END IF;
5028 
5029    /* end of cyclic relationship */
5030 
5031    IF ((p_relationship_tbl(l_count).active_start_date IS NULL) OR
5032        (p_relationship_tbl(l_count).active_start_date = FND_API.G_MISS_DATE))
5033    THEN
5034         p_relationship_tbl(l_count).active_start_date := SYSDATE;
5035    END IF;
5036 
5037       IF x_return_status=fnd_api.g_ret_sts_success THEN
5038          -- srramakr Moved the Update_instance call before the table handler Bug # 3296009
5039          update_instance
5040             (   p_api_version                =>     p_api_version,
5041                 p_commit                     =>     fnd_api.g_false,
5042                 p_init_msg_list              =>     p_init_msg_list,
5043                 p_validation_level           =>     p_validation_level,
5044                 p_ii_relationship_rec        =>     p_relationship_tbl(l_count),
5045                 p_txn_rec                    =>     p_txn_rec,
5046                 p_mode                       =>     'CREATE',
5047                 x_return_status              =>     x_return_status,
5048                 x_msg_count                  =>     x_msg_count,
5049                 x_msg_data                   =>     x_msg_data);
5050          -- Added by sguthiva for bug 2373109
5051            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5052                    l_msg_index := 1;
5053                    l_msg_count := x_msg_count;
5054              WHILE l_msg_count > 0
5055              LOOP
5056                      x_msg_data := FND_MSG_PUB.GET
5057                                (  l_msg_index,
5058                                   FND_API.G_FALSE );
5059               csi_gen_utility_pvt.put_line( ' Error from CSI_II_RELATIONSHIPS_PVT.CREATE_RELATIONSHIP');
5060               csi_gen_utility_pvt.put_line( ' Call to update_instance has errored ....');
5061               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
5062                l_msg_index := l_msg_index + 1;
5063                l_msg_count := l_msg_count - 1;
5064              END LOOP;
5065                RAISE FND_API.G_EXC_ERROR;
5066            END IF;
5067          -- End of addition by sguthiva for bug 2373109
5068 
5069 
5070 
5071         -- Added by sk on 9-Apr-02 for bug 2304221
5072            -- invoke table handler(csi_ii_relationships_pkg.insert_row)
5073              csi_ii_relationships_pkg.insert_row(
5074           px_relationship_id                => p_relationship_tbl(l_count).relationship_id,
5075           p_relationship_type_code          => p_relationship_tbl(l_count).relationship_type_code,
5076           p_object_id                       => p_relationship_tbl(l_count).object_id,
5077           p_subject_id                      => p_relationship_tbl(l_count).subject_id,
5078           p_position_reference              => p_relationship_tbl(l_count).position_reference,
5079           p_active_start_date               => p_relationship_tbl(l_count).active_start_date,
5080           p_active_end_date                 => p_relationship_tbl(l_count).active_end_date,
5081           p_display_order                   => p_relationship_tbl(l_count).display_order,
5082           p_mandatory_flag                  => p_relationship_tbl(l_count).mandatory_flag,
5083           p_context                         => p_relationship_tbl(l_count).context,
5084           p_attribute1                      => p_relationship_tbl(l_count).attribute1,
5085           p_attribute2                      => p_relationship_tbl(l_count).attribute2,
5086           p_attribute3                      => p_relationship_tbl(l_count).attribute3,
5087           p_attribute4                      => p_relationship_tbl(l_count).attribute4,
5088           p_attribute5                      => p_relationship_tbl(l_count).attribute5,
5089           p_attribute6                      => p_relationship_tbl(l_count).attribute6,
5090           p_attribute7                      => p_relationship_tbl(l_count).attribute7,
5091           p_attribute8                      => p_relationship_tbl(l_count).attribute8,
5092           p_attribute9                      => p_relationship_tbl(l_count).attribute9,
5093           p_attribute10                     => p_relationship_tbl(l_count).attribute10,
5094           p_attribute11                     => p_relationship_tbl(l_count).attribute11,
5095           p_attribute12                     => p_relationship_tbl(l_count).attribute12,
5096           p_attribute13                     => p_relationship_tbl(l_count).attribute13,
5097           p_attribute14                     => p_relationship_tbl(l_count).attribute14,
5098           p_attribute15                     => p_relationship_tbl(l_count).attribute15,
5099           p_created_by                      => fnd_global.user_id,
5100           p_creation_date                   => SYSDATE,
5101           p_last_updated_by                 => fnd_global.user_id,
5102           p_last_update_date                => SYSDATE,
5103           p_last_update_login               => fnd_global.conc_login_id,
5104           p_object_version_number           => 1);
5105       -- hint: primary key should be returned.
5106       -- x_relationship_id := px_relationship_id;
5107       END IF;
5108 
5109           IF x_return_status <> fnd_api.g_ret_sts_success THEN
5110               RAISE fnd_api.g_exc_error;
5111           END IF;
5112 
5113         csi_transactions_pvt.create_transaction
5114           (
5115              p_api_version            => p_api_version
5116             ,p_commit                 => p_commit
5117             ,p_init_msg_list          => p_init_msg_list
5118             ,p_validation_level       => p_validation_level
5119             ,p_success_if_exists_flag => 'Y'
5120             ,p_transaction_rec        => p_txn_rec
5121             ,x_return_status          => x_return_status
5122             ,x_msg_count              => x_msg_count
5123             ,x_msg_data               => x_msg_data
5124           );
5125 
5126          IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5127               fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_TXN');
5128               fnd_message.set_token('transaction_id',p_txn_rec.transaction_id );
5129               fnd_msg_pub.add;
5130               RAISE fnd_api.g_exc_error;
5131               ROLLBACK TO create_relationship_pvt;
5132          END IF;
5133 
5134          -- The following code has been commented by sguthiva
5135          -- after a discussion for bug 2373109 on 21-May-02
5136          -- During a creation of relationship there should not be
5137          -- any changes to the ownership.
5138    /*
5139          update_party_account
5140             (   p_api_version                =>     p_api_version,
5141                 p_commit                     =>     fnd_api.g_false,
5142                 p_init_msg_list              =>     p_init_msg_list,
5143                 p_validation_level           =>     p_validation_level,
5144                 p_ii_relationship_rec        =>     p_relationship_tbl(l_count),
5145                 p_txn_rec                    =>     p_txn_rec,
5146                 x_return_status              =>     x_return_status,
5147                 x_msg_count                  =>     x_msg_count,
5148                 x_msg_data                   =>     x_msg_data);
5149          */
5150         -- End addition by sk for bug 2304221
5151 
5152 IF x_return_status = fnd_api.g_ret_sts_success THEN
5153 
5154  l_relship_history_id := NULL;
5155 
5156  csi_ii_relationships_h_pkg.insert_row(
5157             px_relationship_history_id      =>  l_relship_history_id,
5158             p_relationship_id               =>  p_relationship_tbl(l_count).relationship_id,
5159             p_transaction_id                =>  p_txn_rec.transaction_id,
5160             p_old_subject_id                =>  NULL,
5161             p_new_subject_id                =>  p_relationship_tbl(l_count).subject_id,
5162             p_old_position_reference        =>  NULL,
5163             p_new_position_reference        =>  p_relationship_tbl(l_count).position_reference,
5164             p_old_active_start_date         =>  NULL,
5165             p_new_active_start_date         =>  p_relationship_tbl(l_count).active_start_date,
5166             p_old_active_end_date           =>  NULL,
5167             p_new_active_end_date           =>  p_relationship_tbl(l_count).active_end_date,
5168             p_old_mandatory_flag            =>  NULL,
5169             p_new_mandatory_flag            =>  p_relationship_tbl(l_count).mandatory_flag,
5170             p_old_context                   =>  NULL,
5171             p_new_context                   =>  p_relationship_tbl(l_count).context,
5172             p_old_attribute1                =>  NULL,
5173             p_new_attribute1                =>  p_relationship_tbl(l_count).attribute1,
5174             p_old_attribute2                =>  NULL,
5175             p_new_attribute2                =>  p_relationship_tbl(l_count).attribute2,
5176             p_old_attribute3                =>  NULL,
5177             p_new_attribute3                =>  p_relationship_tbl(l_count).attribute3,
5178             p_old_attribute4                =>  NULL,
5179             p_new_attribute4                =>  p_relationship_tbl(l_count).attribute4,
5180             p_old_attribute5                =>  NULL,
5181             p_new_attribute5                =>  p_relationship_tbl(l_count).attribute5,
5182             p_old_attribute6                =>  NULL,
5183             p_new_attribute6                =>  p_relationship_tbl(l_count).attribute6,
5184             p_old_attribute7                =>  NULL,
5185             p_new_attribute7                =>  p_relationship_tbl(l_count).attribute7,
5186             p_old_attribute8                =>  NULL,
5187             p_new_attribute8                =>  p_relationship_tbl(l_count).attribute8,
5188             p_old_attribute9                =>  NULL,
5189             p_new_attribute9                =>  p_relationship_tbl(l_count).attribute9,
5190             p_old_attribute10               =>  NULL,
5191             p_new_attribute10               =>  p_relationship_tbl(l_count).attribute10,
5192             p_old_attribute11               =>  NULL,
5193             p_new_attribute11               =>  p_relationship_tbl(l_count).attribute11,
5194             p_old_attribute12               =>  NULL,
5195             p_new_attribute12               =>  p_relationship_tbl(l_count).attribute12,
5196             p_old_attribute13               =>  NULL,
5197             p_new_attribute13               =>  p_relationship_tbl(l_count).attribute13,
5198             p_old_attribute14               =>  NULL,
5199             p_new_attribute14               =>  p_relationship_tbl(l_count).attribute14,
5200             p_old_attribute15               =>  NULL,
5201             p_new_attribute15               =>  p_relationship_tbl(l_count).attribute15,
5202             p_full_dump_flag                =>  'Y',
5203             p_created_by                    =>  fnd_global.user_id,
5204             p_creation_date                 =>  SYSDATE,
5205             p_last_updated_by               =>  fnd_global.user_id,
5206             p_last_update_date              =>  SYSDATE,
5207             p_last_update_login             =>  fnd_global.conc_login_id,
5208             p_object_version_number         =>  1);
5209 
5210 
5211   END IF;
5212 
5213 -- Start of cascade ownership changes bug 2972082
5214 -- Get the parent instance owner party and owner account
5215 
5216  IF nvl(p_relationship_tbl(l_count).cascade_ownership_flag,'N')='Y'
5217  THEN
5218  csi_gen_utility_pvt.put_line('Cascade_ownership_flag       : '||p_relationship_tbl(l_count).cascade_ownership_flag);
5219           l_instance_rec:=l_cascade_instance_rec;
5220           l_ext_attrib_values_tbl.delete;
5221           l_party_tbl.delete;
5222           l_account_tbl.delete;
5223           l_pricing_attrib_tbl.delete;
5224           l_org_assignments_tbl.delete;
5225           l_instance_id_lst.delete;
5226 
5227         IF p_relationship_tbl(l_count).object_id IS NOT NULL AND
5228            p_relationship_tbl(l_count).object_id <> fnd_api.g_miss_num
5229         THEN
5230            l_instance_rec.instance_id:=p_relationship_tbl(l_count).object_id;
5231         END IF;
5232 
5233         BEGIN
5234           SELECT object_version_number,
5235                  'Y'
5236           INTO   l_instance_rec.object_version_number,
5237                  l_instance_rec.cascade_ownership_flag
5238           FROM   csi_item_instances
5239           WHERE  instance_id=l_instance_rec.instance_id
5240           AND   (active_end_date IS NULL OR active_end_date > sysdate);
5241         EXCEPTION
5242           WHEN OTHERS
5243           THEN
5244              csi_gen_utility_pvt.put_line( 'Error from create relationship API.');
5245              csi_gen_utility_pvt.put_line( 'The object_id, which you are trying to cascade its ownership, is not found or expired in csi_item_instances table. ');
5246              RAISE fnd_api.g_exc_error;
5247         END;
5248 
5249                  csi_item_instance_pub.update_item_instance
5250                  ( p_api_version           =>  p_api_version
5251                   ,p_commit                =>  p_commit
5252                   ,p_init_msg_list         =>  p_init_msg_list
5253                   ,p_validation_level      =>  p_validation_level
5254                   ,p_instance_rec          =>  l_instance_rec
5255                   ,p_ext_attrib_values_tbl =>  l_ext_attrib_values_tbl
5256                   ,p_party_tbl             =>  l_party_tbl
5257                   ,p_account_tbl           =>  l_account_tbl
5258                   ,p_pricing_attrib_tbl    =>  l_pricing_attrib_tbl
5259                   ,p_org_assignments_tbl   =>  l_org_assignments_tbl
5260                   ,p_asset_assignment_tbl  =>  l_asset_assignment_tbl
5261                   ,p_txn_rec               =>  p_txn_rec
5262                   ,x_instance_id_lst       =>  l_instance_id_lst
5263                   ,x_return_status         =>  x_return_status
5264                   ,x_msg_count             =>  x_msg_count
5265                   ,x_msg_data              =>  x_msg_data
5266                   );
5267 
5268                 IF NOT(x_return_status = fnd_api.g_ret_sts_success)
5269                 THEN
5270                      csi_gen_utility_pvt.put_line( 'Error from create relationship API.');
5271                      csi_gen_utility_pvt.put_line( 'Call to update_item_instance API for cascade ownership has errored');
5272                      fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
5273                      fnd_message.set_token('instance_id',l_instance_rec.instance_id);
5274                      fnd_msg_pub.add;
5275                     RAISE fnd_api.g_exc_error;
5276                 END IF;
5277  END IF;
5278 
5279 -- End of cascade ownership changes bug 2972082
5280 
5281  END LOOP;
5282             IF x_return_status <> fnd_api.g_ret_sts_success THEN
5283               RAISE fnd_api.g_exc_error;
5284             END IF;
5285       --
5286       -- END of API BODY
5287       --
5288 
5289       -- standard check for p_commit
5290       IF fnd_api.to_boolean( p_commit )
5291       THEN
5292           COMMIT WORK;
5293       END IF;
5294 
5295       -- standard call to get message count and if count is 1, get message info.
5296       fnd_msg_pub.count_AND_get
5297       (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
5298       p_count          =>   x_msg_count,
5299          p_data           =>   x_msg_data
5300       );
5301 
5302        EXCEPTION
5303           WHEN fnd_api.g_exc_error THEN
5304                 ROLLBACK TO create_relationship_pvt;
5305                 x_return_status := fnd_api.g_ret_sts_error ;
5306                 fnd_msg_pub.count_AND_get
5307                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
5308            p_count => x_msg_count ,
5309                          p_data => x_msg_data
5310                         );
5311 
5312           WHEN fnd_api.g_exc_unexpected_error THEN
5313                 ROLLBACK TO create_relationship_pvt;
5314                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
5315                 fnd_msg_pub.count_AND_get
5316                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
5317            p_count => x_msg_count ,
5318                          p_data => x_msg_data
5319                         );
5320 
5321           WHEN OTHERS THEN
5322                 ROLLBACK TO create_relationship_pvt;
5323                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
5324                   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
5325                          fnd_msg_pub.add_exc_msg(g_pkg_name ,l_api_name);
5326                   END IF;
5327                 fnd_msg_pub.count_AND_get
5328                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
5329            p_count => x_msg_count ,
5330                          p_data => x_msg_data
5331                         );
5332 
5333 END create_relationship;
5334 
5335 
5336 PROCEDURE update_relationship
5337 (
5338      p_api_version                IN  NUMBER,
5339      p_commit                     IN  VARCHAR2,
5340      p_init_msg_list              IN  VARCHAR2,
5341      p_validation_level           IN  NUMBER,
5342      p_relationship_tbl           IN      csi_datastructures_pub.ii_relationship_tbl,
5343      p_replace_flag               IN  VARCHAR2,
5344      p_txn_rec                    IN  OUT NOCOPY csi_datastructures_pub.transaction_rec,
5345      x_return_status              OUT NOCOPY VARCHAR2,
5346      x_msg_count                  OUT NOCOPY NUMBER,
5347      x_msg_data                   OUT NOCOPY VARCHAR2
5348 ) IS
5349 
5350  CURSOR  relship_csr (relship_id  IN  NUMBER) IS
5351      SELECT relationship_id,
5352             relationship_type_code,
5353             object_id,
5354             subject_id,
5355             position_reference,
5356             active_start_date,
5357             active_end_date,
5358             display_order,
5359             mandatory_flag,
5360             context,
5361             attribute1,
5362             attribute2,
5363             attribute3,
5364             attribute4,
5365             attribute5,
5366             attribute6,
5367             attribute7,
5368             attribute8,
5369             attribute9,
5370             attribute10,
5371             attribute11,
5372             attribute12,
5373             attribute13,
5374             attribute14,
5375             attribute15,
5376             object_version_number
5377       FROM  csi_ii_relationships
5378       WHERE relationship_id=relship_id
5379       FOR UPDATE OF object_version_number ;
5380 
5381 
5382     l_api_name               CONSTANT VARCHAR2(30) := 'update_ii_relationships';
5383     l_api_version            CONSTANT NUMBER   := 1.0;
5384     -- local variables
5385     --
5386     l_index                           NUMBER;
5387     l_count                           NUMBER;
5388     l_debug_level                     NUMBER;
5389     l_line_count                      NUMBER;
5390     l_relship_csr                     relship_csr%ROWTYPE;
5391     l_old_relship_rec                 csi_datastructures_pub.ii_relationship_rec;
5392     l_new_relship_rec                 csi_datastructures_pub.ii_relationship_rec;
5393     l_instance_rec                    csi_datastructures_pub.instance_rec;
5394     l_temp_ins_rec                    csi_datastructures_pub.instance_rec;
5395     l_object_id                       NUMBER;
5396     l_obv_number                      NUMBER;
5397     l_ins_usage_code                  VARCHAR2(30);
5398     l_instance_id_lst                 csi_datastructures_pub.id_tbl;
5399     l_item_attribute_tbl              csi_item_instance_pvt.item_attribute_tbl;
5400     l_location_tbl                    csi_item_instance_pvt.location_tbl;
5401     l_generic_id_tbl                  csi_item_instance_pvt.generic_id_tbl;
5402     l_lookup_tbl                      csi_item_instance_pvt.lookup_tbl;
5403     l_ins_count_rec                   csi_item_instance_pvt.ins_count_rec;
5404     l_relationship_tbl                csi_datastructures_pub.ii_relationship_tbl;
5405     l_obj_id                          NUMBER;
5406     l_sub_id                          NUMBER;
5407     l_msg_data                        VARCHAR2(2000);
5408     l_msg_index                       NUMBER;
5409     l_msg_count                       NUMBER;
5410     l_found                           BOOLEAN;
5411     l_exists                          VARCHAR2(1);
5412 -- Added for cascade ownership change bug 2972082
5413     l_relationship_query_rec          csi_datastructures_pub.relationship_query_rec;
5414     l_rel_tbl                         csi_datastructures_pub.ii_relationship_tbl;
5415     l_ii_relationship_level_tbl       csi_ii_relationships_pvt.ii_relationship_level_tbl;
5416     l_ext_attrib_values_tbl           csi_datastructures_pub.extend_attrib_values_tbl;
5417     l_party_tbl                       csi_datastructures_pub.party_tbl;
5418     l_account_tbl                     csi_datastructures_pub.party_account_tbl;
5419     l_temp_party_tbl                  csi_datastructures_pub.party_tbl;
5420     l_temp_account_tbl                csi_datastructures_pub.party_account_tbl;
5421     l_pricing_attrib_tbl              csi_datastructures_pub.pricing_attribs_tbl;
5422     l_org_assignments_tbl             csi_datastructures_pub.organization_units_tbl;
5423     l_asset_assignment_tbl            csi_datastructures_pub.instance_asset_tbl;
5424     l_inst_id_lst                     csi_datastructures_pub.id_tbl;
5425     l_inst_rec                        csi_datastructures_pub.instance_rec;
5426     l_cascade_instance_rec            csi_datastructures_pub.instance_rec;
5427     l_item_id                         NUMBER;
5428     l_srl_ctl                         NUMBER;
5429     l_vld_org                         NUMBER;
5430     l_loc_type_code                   VARCHAR2(30);
5431 -- End addition for cascade ownership changes bug 2972082
5432     l_subject_lock                    NUMBER;
5433     px_oks_txn_inst_tbl               oks_ibint_pub.txn_instance_tbl;
5434     px_child_inst_tbl                 csi_item_instance_grp.child_inst_tbl;
5435 	l_is_a_child					  VARCHAR2(1);	--Added for Bug 14163453
5436 
5437 BEGIN
5438       -- standard start of api savepoint
5439       SAVEPOINT update_relationship_pvt;
5440       -- standard call to check for call compatibility.
5441       IF NOT fnd_api.compatible_api_call ( l_api_version,
5442                                            p_api_version,
5443                                            l_api_name,
5444                                            g_pkg_name)
5445       THEN
5446           RAISE fnd_api.g_exc_unexpected_error;
5447       END IF;
5448 
5449 
5450       -- initialize message list IF p_init_msg_list IS set TO true.
5451       IF fnd_api.to_boolean( p_init_msg_list )
5452       THEN
5453           fnd_msg_pub.initialize;
5454       END IF;
5455 
5456 
5457       -- initialize api return status to success
5458       x_return_status := fnd_api.g_ret_sts_success;
5459 
5460 
5461        l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
5462     IF (l_debug_level > 0) THEN
5463           CSI_gen_utility_pvt.put_line( 'update_relationship');
5464     END IF;
5465 
5466     IF (l_debug_level > 1) THEN
5467 
5468 
5469              CSI_gen_utility_pvt.put_line(
5470                             p_api_version             ||'-'||
5471                             p_commit                  ||'-'||
5472                             p_init_msg_list           ||'-'||
5473                             p_validation_level        );
5474 
5475          csi_gen_utility_pvt.dump_rel_tbl(p_relationship_tbl);
5476          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
5477 
5478     END IF;
5479 
5480 
5481       validate_ii_relationships(
5482           p_init_msg_list           => fnd_api.g_false,
5483           p_validation_level        => p_validation_level,
5484           p_validation_mode         => 'UPDATE',
5485           p_ii_relationship_tbl     => p_relationship_tbl,
5486           x_return_status           => x_return_status,
5487           x_msg_count               => x_msg_count,
5488           x_msg_data                => x_msg_data);
5489 
5490       IF x_return_status<>fnd_api.g_ret_sts_success THEN
5491           RAISE fnd_api.g_exc_error;
5492       END IF;
5493 
5494     l_line_count := p_relationship_tbl.count;
5495 
5496      FOR l_count IN 1..l_line_count LOOP
5497          l_obv_number:=NULL;
5498      /* Cyclic Relationship */
5499      /* added this to validate input parameters */
5500           -- check for infinite loop; Added for bug 10081206
5501         IF (p_relationship_tbl(l_count).subject_id = p_relationship_tbl(l_count).object_id) THEN
5502               RAISE fnd_api.g_exc_unexpected_error;
5503         END IF;
5504       IF NOT  valid_in_parameters
5505            (p_relship_id      => p_relationship_tbl(l_count).relationship_id,
5506             p_object_id       => p_relationship_tbl(l_count).object_id,
5507             p_subject_id      => p_relationship_tbl(l_count).subject_id )
5508       THEN
5509         fnd_message.set_name('CSI','CSI_API_INVALID_PARAMETERS');
5510         fnd_msg_pub.add;
5511         RAISE fnd_api.g_exc_error;
5512       END IF;
5513 
5514         --Added for MACD lock functionality
5515         IF p_relationship_tbl(l_count).object_id IS NOT NULL AND
5516            p_relationship_tbl(l_count).object_id <> fnd_api.g_miss_num
5517         THEN
5518            IF csi_item_instance_pvt.check_item_instance_lock
5519                                       ( p_instance_id => p_relationship_tbl(l_count).object_id)
5520            THEN
5521             IF p_txn_rec.transaction_type_id NOT IN (51,53,54,401)
5522             THEN
5523              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
5524              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_relationship_tbl(l_count).object_id);
5525              FND_MSG_PUB.ADD;
5526              RAISE FND_API.G_EXC_ERROR;
5527             END IF;
5528            END IF;
5529         END IF;
5530         IF p_relationship_tbl(l_count).subject_id IS NOT NULL AND
5531            p_relationship_tbl(l_count).subject_id <> fnd_api.g_miss_num
5532         THEN
5533            IF csi_item_instance_pvt.check_item_instance_lock
5534                                       ( p_instance_id => p_relationship_tbl(l_count).subject_id)
5535            THEN
5536             IF p_txn_rec.transaction_type_id NOT IN (51,53,54,401)
5537             THEN
5538              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
5539              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_relationship_tbl(l_count).subject_id);
5540              FND_MSG_PUB.ADD;
5541              RAISE FND_API.G_EXC_ERROR;
5542             END IF;
5543            END IF;
5544         ELSE
5545            l_subject_lock:=NULL;
5546           BEGIN
5547            SELECT subject_id
5548              INTO l_subject_lock
5549              FROM csi_ii_relationships
5550             WHERE relationship_id=p_relationship_tbl(l_count).relationship_id;
5551            IF csi_item_instance_pvt.check_item_instance_lock
5552                                       ( p_instance_id => l_subject_lock)
5553            THEN
5554             IF p_txn_rec.transaction_type_id NOT IN (51,53,54,401)
5555             THEN
5556              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
5557              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',l_subject_lock);
5558              FND_MSG_PUB.ADD;
5559              RAISE FND_API.G_EXC_ERROR;
5560             END IF;
5561            END IF;
5562            EXCEPTION
5563              WHEN NO_DATA_FOUND THEN
5564               NULL;
5565           END;
5566 
5567         END IF;
5568         -- End addition for MACD lock functionality
5569 
5570       l_new_relship_rec:=p_relationship_tbl(l_count);
5571       OPEN relship_csr (p_relationship_tbl(l_count).relationship_id);
5572       FETCH relship_csr INTO l_relship_csr;
5573        IF ( (l_relship_csr.object_version_number<>p_relationship_tbl(l_count).object_version_number)
5574          AND (p_relationship_tbl(l_count).object_version_number <> fnd_api.g_miss_num) ) THEN
5575          fnd_message.set_name('CSI', 'CSI_RECORD_CHANGED');
5576           fnd_msg_pub.add;
5577          RAISE fnd_api.g_exc_error;
5578        END IF;
5579       CLOSE relship_csr;
5580 
5581       /* Cyclic relationship */
5582       /* added this call to check configurator_id */
5583   /*   IF  config_set
5584          (p_relationship_id=>p_relationship_tbl(l_count).relationship_id)
5585      THEN
5586         RAISE fnd_api.g_exc_error;
5587      END IF;
5588 */
5589      /* added this to skip this validation ,in case subject and object_id are null */
5590      IF  ((p_relationship_tbl(l_count).relationship_id IS NOT NULL
5591          AND  p_relationship_tbl(l_count).relationship_id <> fnd_api.g_miss_num)
5592          AND ( p_relationship_tbl(l_count).object_id IS NOT NULL
5593          AND  p_relationship_tbl(l_count).object_id <> fnd_api.g_miss_num))
5594      THEN
5595          /* End cyclic relationship */
5596         IF object_relship_exists(p_relationship_tbl(l_count).relationship_id,
5597                                  p_relationship_tbl(l_count).object_id,
5598                                  p_relationship_tbl(l_count).relationship_type_code)
5599         THEN
5600             x_return_status := fnd_api.g_ret_sts_success;
5601         ELSE
5602             x_return_status := fnd_api.g_ret_sts_error;
5603             RAISE fnd_api.g_exc_error;
5604         END IF;
5605      END IF;
5606 
5607      /* Cyclic relationship -- added the condition to skip the validation for 'CONNECTED-TO' relationship */
5608      IF p_relationship_tbl(l_count).relationship_type_code <> 'CONNECTED-TO' THEN
5609 
5610        IF subject_exists(p_subject_id        => p_relationship_tbl(l_count).subject_id,
5611                          p_relship_type_code => p_relationship_tbl(l_count).relationship_type_code,
5612                          p_relationship_id   => p_relationship_tbl(l_count).relationship_id,
5613                          p_mode              => 'UPDATE')
5614        THEN
5615            x_return_status:=fnd_api.g_ret_sts_success;
5616        ELSE
5617            x_return_status:=fnd_api.g_ret_sts_error;
5618            RAISE fnd_api.g_exc_error;
5619        END IF;
5620        --
5621        -- check whether the Inverse Active Relationship exists
5622        Begin
5623 	  select 'x'
5624 	  into l_exists
5625 	  from csi_ii_relationships
5626 	  where object_id = decode(p_relationship_tbl(l_count).subject_id,fnd_api.g_miss_num,l_relship_csr.subject_id,
5627                                    p_relationship_tbl(l_count).subject_id)
5628 	  and   subject_id = decode(p_relationship_tbl(l_count).object_id,fnd_api.g_miss_num,l_relship_csr.object_id,
5629                                    p_relationship_tbl(l_count).object_id)
5630 	  and   relationship_type_code = decode(p_relationship_tbl(l_count).relationship_type_code,fnd_api.g_miss_char,
5631                                            l_relship_csr.relationship_type_code,
5632                                            p_relationship_tbl(l_count).relationship_type_code)
5633 	  and   ((active_end_date is null) or (active_end_date > sysdate))
5634 	  and   relationship_id <> p_relationship_tbl(l_count).relationship_id;
5635 	  --
5636 	  fnd_message.set_name('CSI','CSI_CHILD_PARENT_REL_LOOP');
5637 	  fnd_msg_pub.add;
5638 	  x_return_status := fnd_api.g_ret_sts_error;
5639 	  RAISE fnd_api.g_exc_error;
5640       Exception
5641 	 when no_data_found then
5642 	    null;
5643       End;
5644       --
5645    -- Start of att enhancements by sguthiva
5646      ELSIF p_relationship_tbl(l_count).relationship_type_code = 'CONNECTED-TO'
5647      THEN
5648 	IF p_relationship_tbl(l_count).object_id IS NULL OR
5649             p_relationship_tbl(l_count).object_id=fnd_api.g_miss_num
5650 	THEN
5651 	    l_obj_id:=NULL;
5652 	    BEGIN
5653 	       SELECT object_id
5654 	       INTO   l_obj_id
5655 	       FROM   csi_ii_relationships
5656 	       WHERE  relationship_id=p_relationship_tbl(l_count).relationship_id;
5657 	    EXCEPTION
5658 	       WHEN OTHERS THEN
5659 		  NULL;
5660 	    END;
5661 	ELSE
5662 	   l_obj_id := p_relationship_tbl(l_count).object_id;
5663 	END IF;
5664         --
5665         IF p_relationship_tbl(l_count).subject_id IS NULL OR
5666            p_relationship_tbl(l_count).subject_id=fnd_api.g_miss_num
5667         THEN
5668             l_sub_id:=NULL;
5669             BEGIN
5670                SELECT subject_id
5671                INTO   l_sub_id
5672                FROM   csi_ii_relationships
5673                WHERE  relationship_id=p_relationship_tbl(l_count).relationship_id;
5674             EXCEPTION
5675                WHEN OTHERS THEN
5676                   NULL;
5677             END;
5678         ELSE
5679            l_sub_id := p_relationship_tbl(l_count).subject_id;
5680         END IF;
5681         --
5682 	IF csi_ii_relationships_pvt.Is_link_type (p_instance_id => l_obj_id )
5683 	THEN
5684 	   IF csi_ii_relationships_pvt.relationship_for_link
5685                                   ( p_instance_id     => l_obj_id
5686 				     ,p_mode            => 'UPDATE'
5687 				     ,p_relationship_id => p_relationship_tbl(l_count).relationship_id )
5688 	   THEN
5689 	      fnd_message.set_name('CSI','CSI_LINK_EXISTS');
5690 	      fnd_message.set_token('INSTANCE_ID',l_obj_id);
5691 	      fnd_msg_pub.add;
5692 	      RAISE fnd_api.g_exc_error;
5693 	   END IF;
5694 	END IF;
5695         --
5696 	IF csi_ii_relationships_pvt.Is_link_type (p_instance_id => l_sub_id )
5697 	THEN
5698            IF csi_ii_relationships_pvt.relationship_for_link
5699                            ( p_instance_id     => l_sub_id
5700                              ,p_mode            => 'UPDATE'
5701                              ,p_relationship_id => p_relationship_tbl(l_count).relationship_id )
5702 	   THEN
5703 	      fnd_message.set_name('CSI','CSI_LINK_EXISTS');
5704 	      fnd_message.set_token('INSTANCE_ID',l_sub_id);
5705 	      fnd_msg_pub.add;
5706 	      RAISE fnd_api.g_exc_error;
5707 	   END IF;
5708         END IF;
5709         -- End of att enhancements by sguthiva
5710       END IF;
5711      --
5712         IF (p_relationship_tbl(l_count).subject_id IS NOT NULL AND
5713             p_relationship_tbl(l_count).subject_id <> fnd_api.g_miss_num AND
5714             l_relship_csr.subject_id <> p_relationship_tbl(l_count).subject_id
5715             AND (  (p_relationship_tbl(l_count).relationship_type_code IS NOT NULL AND
5716                     p_relationship_tbl(l_count).relationship_type_code <> fnd_api.g_miss_char AND
5717                     p_relationship_tbl(l_count).relationship_type_code = 'COMPONENT-OF'
5718                     )
5719                 OR (p_relationship_tbl(l_count).relationship_type_code = fnd_api.g_miss_char  AND
5720                     l_relship_csr.relationship_type_code = 'COMPONENT-OF'
5721                     )
5722                 )
5723              )
5724             OR
5725             (l_relship_csr.relationship_type_code = 'COMPONENT-OF' AND
5726              p_relationship_tbl(l_count).ACTIVE_END_DATE <= SYSDATE AND
5727              p_relationship_tbl(l_count).ACTIVE_END_DATE <> fnd_api.g_miss_date AND
5728              p_relationship_tbl(l_count).ACTIVE_END_DATE IS NOT NULL
5729              )
5730         THEN
5731              l_object_id := NULL;
5732              l_found:=FALSE;
5733 	     csi_ii_relationships_pvt.Get_Top_Most_Parent
5734 		( p_subject_id       => l_relship_csr.subject_id,
5735 		  p_rel_type_code    => 'COMPONENT-OF',
5736 		  p_object_id        => l_object_id
5737 		);
5738 	      --
5739 	      IF l_object_id <> l_relship_csr.subject_id THEN
5740 		 BEGIN
5741 		    SELECT instance_usage_code
5742 		    INTO   l_ins_usage_code
5743 		    FROM   csi_item_instances
5744 		    WHERE  instance_id=l_object_id;
5745 		 EXCEPTION
5746 		    WHEN NO_DATA_FOUND THEN
5747 		       NULL;
5748 		 END;
5749 
5750 		 l_obv_number:=NULL;
5751 		 BEGIN
5752 		    SELECT  object_version_number,
5753 			    config_inst_hdr_id, --added
5754 			    config_inst_item_id, --added
5755 			    config_inst_rev_num, --added
5756 			    location_type_code,
5757 			    inventory_item_id,
5758 			    last_vld_organization_id
5759 		    INTO    l_obv_number,
5760 			    l_instance_rec.config_inst_hdr_id, --added
5761 			    l_instance_rec.config_inst_item_id, --added
5762 			    l_instance_rec.config_inst_rev_num, --added
5763 			    l_loc_type_code,
5764 			    l_item_id,
5765 			    l_vld_org
5766 		    FROM    csi_item_instances
5767 		    WHERE   instance_id = l_relship_csr.subject_id;
5768 		    --
5769 		    -- Bug 4232599. Serialized at SO issue items cannot have usage as IN_INVENTORY
5770 		    -- as taken from parent. Since relationship can be broken only thru' RMA,
5771 		    -- usage is set as RETURNED. Other INV/WIP txns will not let serialized at SO issue
5772 		    -- items to be transacted with the serial number. Hence other locations are not considered.
5773 		    Begin
5774 		       select serial_number_control_code
5775 		       into l_srl_ctl
5776 		       from MTL_SYSTEM_ITEMS_B
5777 		       where inventory_item_id = l_item_id
5778 		       and   organization_id = l_vld_org;
5779 		       --
5780 		       IF l_srl_ctl = 6 THEN
5781 			  IF l_loc_type_code = 'INVENTORY' THEN
5782 			     l_ins_usage_code := 'RETURNED';
5783 			  END IF;
5784 		       END IF;
5785 		    Exception
5786 		       when no_data_found then
5787 			  null;
5788 		    End;
5789 		 EXCEPTION
5790 		     WHEN NO_DATA_FOUND THEN
5791 		       NULL;
5792 		 END;
5793                  l_found:=TRUE;
5794               END IF;
5795         END IF;
5796         -- End Addition by sk for bug 2151750
5797        /* added this to aviod creating 'CONNECTED-TO' relationship in the reverse direction */
5798        IF p_relationship_tbl(l_count).relationship_type_code='CONNECTED-TO' THEN
5799            IF relationship_not_exists(p_relationship_tbl(l_count).subject_id,
5800                                       p_relationship_tbl(l_count).object_id,
5801                                       p_relationship_tbl(l_count).relationship_type_code,
5802                                       'UPDATE',
5803                                       p_relationship_tbl(l_count).relationship_id)
5804            THEN
5805               x_return_status:=fnd_api.g_ret_sts_success;
5806            ELSE
5807               x_return_status:=fnd_api.g_ret_sts_error;
5808               RAISE fnd_api.g_exc_error;
5809            END IF;
5810         END IF;
5811 
5812     IF x_return_status = fnd_api.g_ret_sts_success  THEN
5813        -- Update for the New subject only if the subject or relationship type is changing
5814        -- OR during Un-expiry
5815        IF (( (p_relationship_tbl(l_count).subject_id IS NOT NULL AND
5816              p_relationship_tbl(l_count).subject_id <> fnd_api.g_miss_num AND
5817              l_relship_csr.subject_id <> p_relationship_tbl(l_count).subject_id) OR
5818             ( p_relationship_tbl(l_count).relationship_type_code IS NOT NULL AND
5819               p_relationship_tbl(l_count).relationship_type_code <> fnd_api.g_miss_char AND
5820               p_relationship_tbl(l_count).relationship_type_code = 'COMPONENT-OF' AND
5821               l_relship_csr.relationship_type_code <> p_relationship_tbl(l_count).relationship_type_code) )
5822             OR
5823             ( (p_relationship_tbl(l_count).active_end_date IS NULL AND
5824               nvl(l_relship_csr.active_end_date,(sysdate+1)) <= SYSDATE) AND
5825              (p_relationship_tbl(l_count).subject_id = FND_API.G_MISS_NUM OR
5826               p_relationship_tbl(l_count).subject_id = l_relship_csr.subject_id) )
5827           )
5828        THEN
5829           csi_gen_utility_pvt.put_line('Calling Update_Instance..');
5830           update_instance
5831 	      ( p_api_version                =>     p_api_version,
5832 	        p_commit                     =>     fnd_api.g_false,--p_commit,
5833 	        p_init_msg_list              =>     p_init_msg_list,
5834 		p_validation_level           =>     p_validation_level,
5835 		p_ii_relationship_rec        =>     p_relationship_tbl(l_count),
5836 		p_txn_rec                    =>     p_txn_rec,
5837 		p_mode                       =>     'UPDATE',
5838 		x_return_status              =>     x_return_status,
5839 		x_msg_count                  =>     x_msg_count,
5840 		x_msg_data                   =>     x_msg_data);
5841 	  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5842 	     l_msg_index := 1;
5843 	     l_msg_count := x_msg_count;
5844 	     WHILE l_msg_count > 0
5845 	     LOOP
5846 	        x_msg_data := FND_MSG_PUB.GET
5847 	        	         (  l_msg_index,
5848 				    FND_API.G_FALSE );
5849 		csi_gen_utility_pvt.put_line( ' Error from CSI_II_RELATIONSHIPS_PVT.UPDATE_RELATIONSHIP');
5850 		csi_gen_utility_pvt.put_line( ' Call to update_instance has errored ....');
5851 		csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
5852 		l_msg_index := l_msg_index + 1;
5853 		l_msg_count := l_msg_count - 1;
5854 	     END LOOP;
5855 	     RAISE FND_API.G_EXC_ERROR;
5856 	  END IF;
5857        END IF;
5858       --
5859       csi_ii_relationships_pkg.update_row(
5860           p_relationship_id                 => p_relationship_tbl(l_count).relationship_id,
5861           p_relationship_type_code          => p_relationship_tbl(l_count).relationship_type_code,
5862           p_object_id                       => p_relationship_tbl(l_count).object_id,
5863           p_subject_id                      => p_relationship_tbl(l_count).subject_id,
5864           p_position_reference              => p_relationship_tbl(l_count).position_reference,
5865           p_active_start_date               => fnd_api.g_miss_date, -- p_relationship_tbl(l_count).active_start_date,
5866           p_active_end_date                 => p_relationship_tbl(l_count).active_end_date,
5867           p_display_order                   => p_relationship_tbl(l_count).display_order,
5868           p_mandatory_flag                  => p_relationship_tbl(l_count).mandatory_flag,
5869           p_context                         => p_relationship_tbl(l_count).context,
5870           p_attribute1                      => p_relationship_tbl(l_count).attribute1,
5871           p_attribute2                      => p_relationship_tbl(l_count).attribute2,
5872           p_attribute3                      => p_relationship_tbl(l_count).attribute3,
5873           p_attribute4                      => p_relationship_tbl(l_count).attribute4,
5874           p_attribute5                      => p_relationship_tbl(l_count).attribute5,
5875           p_attribute6                      => p_relationship_tbl(l_count).attribute6,
5876           p_attribute7                      => p_relationship_tbl(l_count).attribute7,
5877           p_attribute8                      => p_relationship_tbl(l_count).attribute8,
5878           p_attribute9                      => p_relationship_tbl(l_count).attribute9,
5879           p_attribute10                     => p_relationship_tbl(l_count).attribute10,
5880           p_attribute11                     => p_relationship_tbl(l_count).attribute11,
5881           p_attribute12                     => p_relationship_tbl(l_count).attribute12,
5882           p_attribute13                     => p_relationship_tbl(l_count).attribute13,
5883           p_attribute14                     => p_relationship_tbl(l_count).attribute14,
5884           p_attribute15                     => p_relationship_tbl(l_count).attribute15,
5885           p_created_by                      => fnd_api.g_miss_num,
5886           p_creation_date                   => fnd_api.g_miss_date,
5887           p_last_updated_by                 => fnd_global.user_id,
5888           p_last_update_date                => SYSDATE,
5889           p_last_update_login               => fnd_global.conc_login_id,
5890           p_object_version_number           => p_relationship_tbl(l_count).object_version_number);
5891           IF x_return_status <> fnd_api.g_ret_sts_success THEN
5892               RAISE fnd_api.g_exc_error;
5893           END IF;
5894 
5895 /*
5896       ELSE
5897          csi_ii_relationships_pvt.expire_relationship
5898            (p_api_version           =>     p_api_version,
5899             p_commit                =>     fnd_api.g_false,
5900             p_init_msg_list         =>     p_init_msg_list,
5901             p_validation_level      =>     p_validation_level,
5902             p_relationship_rec      =>     p_relationship_tbl(l_count),
5903             p_txn_rec               =>     p_txn_rec,
5904             x_instance_id_lst       =>     l_instance_id_lst,
5905             x_return_status         =>     x_return_status,
5906             x_msg_count             =>     x_msg_count,
5907             x_msg_data              =>     x_msg_data);
5908 
5909 
5910        IF x_return_status <> fnd_api.g_ret_sts_success THEN
5911           RAISE fnd_api.g_exc_error;
5912        END IF;
5913 
5914        l_relationship_tbl(1).relationship_type_code := p_relationship_tbl(l_count).relationship_type_code; --'CONNECTED-TO' ;
5915        l_relationship_tbl(1).object_id := p_relationship_tbl(l_count).object_id;
5916        l_relationship_tbl(1).subject_id  := p_relationship_tbl(l_count).subject_id ;
5917        l_relationship_tbl(1).subject_has_child   := 'Y' ;
5918        l_relationship_tbl(1).active_start_date   := SYSDATE ;
5919        l_relationship_tbl(1).object_version_number:=1;
5920 
5921        csi_ii_relationships_pvt.create_relationship(
5922              p_api_version          => p_api_version,
5923              p_commit               => fnd_api.g_false,
5924              p_init_msg_list        => p_init_msg_list,
5925              p_validation_level     => p_validation_level,
5926              p_relationship_tbl     => l_relationship_tbl,
5927              p_txn_rec              => p_txn_rec,
5928              x_return_status        => x_return_status,
5929              x_msg_count            => x_msg_count,
5930              x_msg_data             => x_msg_data);
5931 
5932        IF x_return_status <> fnd_api.g_ret_sts_success THEN
5933           RAISE fnd_api.g_exc_error;
5934        END IF;
5935        --   END IF;
5936        */
5937     -- END IF;
5938    END IF;
5939 
5940    /*        IF x_return_status <> fnd_api.g_ret_sts_success THEN
5941               RAISE fnd_api.g_exc_error;
5942           END IF;*/
5943            csi_transactions_pvt.create_transaction
5944           (
5945              p_api_version            => p_api_version
5946             ,p_commit                 => p_commit
5947             ,p_init_msg_list          => p_init_msg_list
5948             ,p_validation_level       => p_validation_level
5949             ,p_success_if_exists_flag => 'Y'
5950             ,p_transaction_rec        => p_txn_rec
5951             ,x_return_status          => x_return_status
5952             ,x_msg_count              => x_msg_count
5953             ,x_msg_data               => x_msg_data
5954           );
5955 
5956          IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5957               fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_TXN');
5958               fnd_message.set_token('transaction_id',p_txn_rec.transaction_id );
5959                   fnd_msg_pub.add;
5960               RAISE fnd_api.g_exc_error;
5961               ROLLBACK TO create_relationship_pvt;
5962          END IF;
5963          --
5964 
5965 		 --Added for Bug 14163453
5966 		BEGIN
5967 		    SELECT  object_version_number
5968 			INTO    l_obv_number
5969 			FROM    csi_item_instances
5970 		    WHERE   instance_id = l_relship_csr.subject_id;
5971 
5972 		EXCEPTION
5973 			WHEN OTHERS THEN
5974 				NULL;
5975 		END;
5976 
5977 		l_is_a_child := 'X';
5978 		BEGIN
5979 			SELECT 'Y' INTO l_is_a_child
5980 			FROM csi_ii_relationships WHERE subject_id = l_relship_csr.subject_id
5981 			AND RELATIONSHIP_TYPE_CODE = 'COMPONENT-OF'
5982 			AND Nvl(active_end_date, SYSDATE +1 ) > SYSDATE;
5983 		EXCEPTION
5984 			WHEN no_data_found THEN
5985 				l_is_a_child := 'X';
5986 		END;
5987 
5988 	 l_instance_rec:=l_temp_ins_rec;
5989 	 l_instance_rec.instance_id:= l_relship_csr.subject_id;
5990 	 l_instance_rec.instance_usage_code :=l_ins_usage_code;
5991 	 l_instance_rec.object_version_number :=l_obv_number;
5992          -- TSO with equipment changes.
5993          -- Nullify the keys when the relationship is broken
5994          l_instance_rec.config_inst_hdr_id := NULL;
5995          l_instance_rec.config_inst_rev_num := NULL;
5996          l_instance_rec.config_inst_item_id := NULL;
5997          l_instance_rec.config_valid_status := NULL;
5998          --
5999 	IF l_instance_rec.instance_id IS NOT NULL AND
6000 	   l_instance_rec.object_version_number IS NOT NULL AND
6001 	   l_found AND l_is_a_child <> 'Y' --Changed for Bug 14163453
6002 	THEN
6003 	   IF  l_relship_csr.subject_id = l_instance_rec.instance_id AND
6004 	       nvl(p_replace_flag,fnd_api.g_false)<> fnd_api.g_true
6005 	   THEN
6006 	      csi_gen_utility_pvt.put_line('Calling update_item_instance..');
6007           csi_gen_utility_pvt.put_line('Parameter p_called_from_rel set to true..');
6008 	      csi_item_instance_pvt.update_item_instance
6009          (p_api_version             =>  p_api_version
6010          ,p_commit                  =>  p_commit
6011          ,p_init_msg_list           =>  p_init_msg_list
6012          ,p_validation_level        =>  p_validation_level
6013          ,p_instance_rec            =>  l_instance_rec
6014          ,p_txn_rec                 =>  p_txn_rec
6015          ,x_instance_id_lst         =>  l_instance_id_lst
6016          ,x_return_status           =>  x_return_status
6017          ,x_msg_count               =>  x_msg_count
6018          ,x_msg_data                =>  x_msg_data
6019          ,p_item_attribute_tbl      =>  l_item_attribute_tbl
6020          ,p_location_tbl            =>  l_location_tbl
6021          ,p_generic_id_tbl          =>  l_generic_id_tbl
6022          ,p_lookup_tbl              =>  l_lookup_tbl
6023          ,p_ins_count_rec           =>  l_ins_count_rec
6024          ,p_called_from_rel         =>  fnd_api.g_true
6025          ,p_oks_txn_inst_tbl        =>  px_oks_txn_inst_tbl
6026          ,p_child_inst_tbl          =>  px_child_inst_tbl
6027 	     );
6028 	   END IF;
6029 
6030 	   IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6031 		fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
6032 		fnd_message.set_token('instance_id',l_instance_rec.instance_id);
6033 		fnd_msg_pub.add;
6034 		RAISE fnd_api.g_exc_error;
6035 	   END IF;
6036 	END IF;
6037         --
6038           l_old_relship_rec.relationship_id         :=  l_relship_csr.relationship_id;
6039           l_old_relship_rec.relationship_type_code  :=  l_relship_csr.relationship_id;
6040           l_old_relship_rec.object_id               :=  l_relship_csr.object_id;
6041           l_old_relship_rec.subject_id              :=  l_relship_csr.subject_id;
6042           l_old_relship_rec.position_reference      :=  l_relship_csr.position_reference;
6043           l_old_relship_rec.active_start_date       :=  l_relship_csr.active_start_date;
6044           l_old_relship_rec.active_end_date         :=  l_relship_csr.active_end_date;
6045           l_old_relship_rec.display_order           :=  l_relship_csr.display_order;
6046           l_old_relship_rec.mandatory_flag          :=  l_relship_csr.mandatory_flag;
6047           l_old_relship_rec.context                 :=  l_relship_csr.context;
6048           l_old_relship_rec.attribute1              :=  l_relship_csr.attribute1;
6049           l_old_relship_rec.attribute2              :=  l_relship_csr.attribute2;
6050           l_old_relship_rec.attribute3              :=  l_relship_csr.attribute3;
6051           l_old_relship_rec.attribute4              :=  l_relship_csr.attribute4;
6052           l_old_relship_rec.attribute5              :=  l_relship_csr.attribute5;
6053           l_old_relship_rec.attribute6              :=  l_relship_csr.attribute6;
6054           l_old_relship_rec.attribute7              :=  l_relship_csr.attribute7;
6055           l_old_relship_rec.attribute8              :=  l_relship_csr.attribute8;
6056           l_old_relship_rec.attribute9              :=  l_relship_csr.attribute9;
6057           l_old_relship_rec.attribute10             :=  l_relship_csr.attribute10;
6058           l_old_relship_rec.attribute11             :=  l_relship_csr.attribute11;
6059           l_old_relship_rec.attribute12             :=  l_relship_csr.attribute12;
6060           l_old_relship_rec.attribute13             :=  l_relship_csr.attribute13;
6061           l_old_relship_rec.attribute14             :=  l_relship_csr.attribute14;
6062           l_old_relship_rec.attribute15             :=  l_relship_csr.attribute15;
6063           l_old_relship_rec.object_version_number   :=  l_relship_csr.object_version_number;
6064 
6065 
6066                 validate_history(p_old_relship_rec  =>  l_old_relship_rec,
6067                                  p_new_relship_rec  =>  l_new_relship_rec,
6068                                  p_transaction_id   =>  p_txn_rec.transaction_id,
6069                                  p_flag             =>  NULL,
6070                                  p_sysdate          =>  NULL,
6071                                  x_return_status    =>  x_return_status,
6072                                  x_msg_count        =>  x_msg_count,
6073                                  x_msg_data         =>  x_msg_data
6074                                  );
6075 
6076 -- Start of cascade ownership changes bug 2972082
6077 -- Get the parent instance owner party and owner account
6078 
6079  IF nvl(p_relationship_tbl(l_count).cascade_ownership_flag,'N')='Y'
6080  THEN
6081  csi_gen_utility_pvt.put_line('Cascade_ownership_flag       : '||p_relationship_tbl(l_count).cascade_ownership_flag);
6082           l_inst_rec:=l_cascade_instance_rec;
6083           l_ext_attrib_values_tbl.delete;
6084           l_party_tbl.delete;
6085           l_account_tbl.delete;
6086           l_pricing_attrib_tbl.delete;
6087           l_org_assignments_tbl.delete;
6088           l_inst_id_lst.delete;
6089 
6090         IF p_relationship_tbl(l_count).object_id IS NOT NULL AND
6091            p_relationship_tbl(l_count).object_id <> fnd_api.g_miss_num
6092         THEN
6093            l_inst_rec.instance_id:=p_relationship_tbl(l_count).object_id;
6094         ELSE
6095           BEGIN
6096             SELECT object_id
6097             INTO   l_inst_rec.instance_id
6098             FROM   csi_ii_relationships
6099             WHERE  relationship_id=p_relationship_tbl(l_count).relationship_id;
6100           EXCEPTION
6101             WHEN OTHERS
6102             THEN
6103              csi_gen_utility_pvt.put_line( 'Error from update relationship API.');
6104              csi_gen_utility_pvt.put_line( 'Object_id not found in csi_ii_relationships to cascade ownership');
6105              RAISE fnd_api.g_exc_error;
6106           END;
6107         END IF;
6108 
6109         BEGIN
6110           SELECT object_version_number,
6111                  'Y'
6112           INTO   l_inst_rec.object_version_number,
6113                  l_inst_rec.cascade_ownership_flag
6114           FROM   csi_item_instances
6115           WHERE  instance_id=l_inst_rec.instance_id
6116           AND   (active_end_date IS NULL OR active_end_date > sysdate);
6117         EXCEPTION
6118           WHEN OTHERS
6119           THEN
6120              csi_gen_utility_pvt.put_line( 'Error from update relationship API.');
6121              csi_gen_utility_pvt.put_line( 'The object_id, which you are trying to cascade its ownership, is not found or expired in csi_item_instances table. ');
6122              RAISE fnd_api.g_exc_error;
6123         END;
6124 
6125                  csi_item_instance_pub.update_item_instance
6126                  ( p_api_version           =>  p_api_version
6127                   ,p_commit                =>  p_commit
6128                   ,p_init_msg_list         =>  p_init_msg_list
6129                   ,p_validation_level      =>  p_validation_level
6130                   ,p_instance_rec          =>  l_inst_rec
6131                   ,p_ext_attrib_values_tbl =>  l_ext_attrib_values_tbl
6132                   ,p_party_tbl             =>  l_party_tbl
6133                   ,p_account_tbl           =>  l_account_tbl
6134                   ,p_pricing_attrib_tbl    =>  l_pricing_attrib_tbl
6135                   ,p_org_assignments_tbl   =>  l_org_assignments_tbl
6136                   ,p_asset_assignment_tbl  =>  l_asset_assignment_tbl
6137                   ,p_txn_rec               =>  p_txn_rec
6138                   ,x_instance_id_lst       =>  l_inst_id_lst
6139                   ,x_return_status         =>  x_return_status
6140                   ,x_msg_count             =>  x_msg_count
6141                   ,x_msg_data              =>  x_msg_data
6142                   );
6143 
6144                 IF NOT(x_return_status = fnd_api.g_ret_sts_success)
6145                 THEN
6146                      csi_gen_utility_pvt.put_line( 'Error from update relationship API.');
6147                      csi_gen_utility_pvt.put_line( 'Call to update_item_instance API for cascade ownership has errored');
6148                      fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
6149                      fnd_message.set_token('instance_id',l_instance_rec.instance_id);
6150                      fnd_msg_pub.add;
6151                     RAISE fnd_api.g_exc_error;
6152                 END IF;
6153  END IF;
6154 
6155 -- End of cascade ownership changes bug 2972082
6156 
6157 
6158   END LOOP;
6159     --
6160     -- END of API body.
6161     --
6162 
6163       IF x_return_status <> fnd_api.g_ret_sts_success THEN
6164               RAISE fnd_api.g_exc_error;
6165       END IF;
6166 
6167       -- standard check for p_commit
6168       IF fnd_api.to_boolean( p_commit )
6169       THEN
6170           COMMIT WORK;
6171       END IF;
6172 
6173 
6174 
6175 
6176       -- standard call to get message count and if count is 1, get message info.
6177       fnd_msg_pub.count_AND_get
6178       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6179       p_count          =>   x_msg_count,
6180          p_data           =>   x_msg_data
6181       );
6182 
6183       EXCEPTION
6184           WHEN fnd_api.g_exc_error THEN
6185                 ROLLBACK TO update_relationship_pvt;
6186                 x_return_status := fnd_api.g_ret_sts_error ;
6187                 fnd_msg_pub.count_AND_get
6188                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6189            p_count => x_msg_count ,
6190                          p_data => x_msg_data
6191                         );
6192 
6193           WHEN fnd_api.g_exc_unexpected_error THEN
6194                 ROLLBACK TO update_relationship_pvt;
6195                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
6196                 fnd_msg_pub.count_AND_get
6197                        (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6198            p_count => x_msg_count ,
6199                         p_data => x_msg_data
6200                         );
6201 
6202           WHEN OTHERS THEN
6203                 ROLLBACK TO update_relationship_pvt;
6204                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
6205                   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6206                          fnd_msg_pub.add_exc_msg(g_pkg_name ,l_api_name);
6207                   END IF;
6208                 fnd_msg_pub.count_AND_get
6209                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6210            p_count => x_msg_count ,
6211                          p_data => x_msg_data
6212                         );
6213 
6214 END update_relationship;
6215 
6216 
6217 PROCEDURE expire_relationship
6218 (
6219      p_api_version                 IN       NUMBER,
6220      p_commit                      IN       VARCHAR2,
6221      p_init_msg_list               IN       VARCHAR2,
6222      p_validation_level            IN       NUMBER,
6223      p_relationship_rec            IN  csi_datastructures_pub.ii_relationship_rec,
6224      p_txn_rec                     IN  OUT NOCOPY  csi_datastructures_pub.transaction_rec,
6225      x_instance_id_lst             OUT NOCOPY      csi_datastructures_pub.id_tbl,
6226      x_return_status               OUT NOCOPY      VARCHAR2,
6227      x_msg_count                   OUT NOCOPY      NUMBER,
6228      x_msg_data                    OUT NOCOPY      VARCHAR2
6229 ) IS
6230 
6231 CURSOR  relship_csr (relship_id  IN  NUMBER) IS
6232      SELECT relationship_id,
6233             relationship_type_code,
6234             object_id,
6235             subject_id,
6236             position_reference,
6237             active_start_date,
6238             active_end_date,
6239             display_order,
6240             mandatory_flag,
6241             context,
6242             attribute1,
6243             attribute2,
6244             attribute3,
6245             attribute4,
6246             attribute5,
6247             attribute6,
6248             attribute7,
6249             attribute8,
6250             attribute9,
6251             attribute10,
6252             attribute11,
6253             attribute12,
6254             attribute13,
6255             attribute14,
6256             attribute15,
6257             object_version_number
6258       FROM  csi_ii_relationships
6259       WHERE relationship_id=relship_id
6260       FOR UPDATE OF object_version_number ;
6261 
6262     l_relship_csr                      relship_csr%ROWTYPE;
6263     l_api_name                CONSTANT VARCHAR2(30) := 'expire_relationship';
6264     l_api_version             CONSTANT NUMBER   := 1.0;
6265     l_debug_level                      NUMBER;
6266     l_old_relship_rec                  csi_datastructures_pub.ii_relationship_rec;
6267     l_new_relship_rec                  csi_datastructures_pub.ii_relationship_rec;
6268     l_ii_relationship_tbl              csi_datastructures_pub.ii_relationship_tbl;
6269     l_sysdate                 CONSTANT DATE :=SYSDATE;
6270     l_instance_rec                     csi_datastructures_pub.instance_rec;
6271     l_temp_ins_rec                     csi_datastructures_pub.instance_rec;
6272     l_object_id                        NUMBER;
6273     l_obv_number                       NUMBER;
6274     l_ins_usage_code                   VARCHAR2(30);
6275 	l_instance_status_id               NUMBER;
6276     l_instance_id_lst                  csi_datastructures_pub.id_tbl;
6277     l_item_attribute_tbl               csi_item_instance_pvt.item_attribute_tbl;
6278     l_location_tbl                     csi_item_instance_pvt.location_tbl;
6279     l_generic_id_tbl                   csi_item_instance_pvt.generic_id_tbl;
6280     l_lookup_tbl                       csi_item_instance_pvt.lookup_tbl;
6281     l_ins_count_rec                    csi_item_instance_pvt.ins_count_rec;
6282     l_found                            BOOLEAN;
6283     l_item_id                          NUMBER;
6284     l_vld_org                          NUMBER;
6285     l_srl_ctl                          NUMBER;
6286     l_loc_type_code                    VARCHAR2(30);
6287     px_oks_txn_inst_tbl                oks_ibint_pub.txn_instance_tbl;
6288     px_child_inst_tbl                  csi_item_instance_grp.child_inst_tbl;
6289     l_is_locked	   BOOLEAN;
6290     l_lock_id       NUMBER;
6291     l_serial_number VARCHAR2(60);
6292 BEGIN
6293       -- standard start of api savepoint
6294       SAVEPOINT expire_relationship_pvt;
6295 
6296       -- standard call to check for call compatibility.
6297       IF NOT fnd_api.compatible_api_call ( l_api_version,
6298                                            p_api_version,
6299                                            l_api_name,
6300                                            g_pkg_name)
6301       THEN
6302           RAISE fnd_api.g_exc_unexpected_error;
6303       END IF;
6304 
6305       -- initialize message list if p_init_msg_list is set to true.
6306       IF fnd_api.to_boolean( p_init_msg_list )
6307       THEN
6308           fnd_msg_pub.initialize;
6309       END IF;
6310 
6311       -- initialize api return status to success
6312       x_return_status := fnd_api.g_ret_sts_success;
6313 
6314         l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
6315     IF (l_debug_level > 0) THEN
6316           CSI_gen_utility_pvt.put_line( 'expire_relationship');
6317     END IF;
6318 
6319     IF (l_debug_level > 1) THEN
6320 
6321              CSI_gen_utility_pvt.put_line(
6322                             p_api_version             ||'-'||
6323                             p_commit                  ||'-'||
6324                             p_init_msg_list           ||'-'||
6325                             p_validation_level
6326                                 );
6327 
6328          -- dump the relationship query records
6329          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
6330          csi_gen_utility_pvt.dump_rel_rec(p_relationship_rec);
6331 
6332     END IF;
6333 
6334       --
6335       -- API BODY
6336       --
6337       OPEN relship_csr (p_relationship_rec.relationship_id);
6338       FETCH relship_csr INTO l_relship_csr;
6339        IF ( (l_relship_csr.object_version_number<>p_relationship_rec.object_version_number)
6340          AND (p_relationship_rec.object_version_number <> fnd_api.g_miss_num) ) THEN
6341          fnd_message.set_name('CSI', 'CSI_RECORD_CHANGED');
6342           fnd_msg_pub.add;
6343          RAISE fnd_api.g_exc_error;
6344        END IF;
6345       CLOSE relship_csr;
6346 
6347       l_ii_relationship_tbl(1) := p_relationship_rec;
6348        validate_ii_relationships(
6349           p_init_msg_list           => fnd_api.g_false,
6350           p_validation_level        => p_validation_level,
6351           p_validation_mode         => 'EXPIRE',
6352           p_ii_relationship_tbl     => l_ii_relationship_tbl,
6353           x_return_status           => x_return_status,
6354           x_msg_count               => x_msg_count,
6355           x_msg_data                => x_msg_data);
6356 
6357       IF x_return_status<>fnd_api.g_ret_sts_success THEN
6358           RAISE fnd_api.g_exc_error;
6359       END IF;
6360       IF l_relship_csr.relationship_type_code = 'COMPONENT-OF'
6361       THEN
6362       BEGIN
6363              l_object_id := NULL;
6364              l_found:=FALSE;
6365 	     csi_ii_relationships_pvt.Get_Top_Most_Parent
6366 		( p_subject_id       => l_relship_csr.subject_id,
6367 		  p_rel_type_code    => 'COMPONENT-OF',
6368 		  p_object_id        => l_object_id
6369 		);
6370 	      --
6371 	      IF l_object_id <> l_relship_csr.subject_id THEN
6372 		 BEGIN
6373 		    SELECT instance_usage_code,
6374         serial_number
6375 		    INTO   l_ins_usage_code, l_serial_number
6376 		    FROM   csi_item_instances
6377 		    WHERE  instance_id=l_object_id;
6378 		 EXCEPTION
6379 		    WHEN NO_DATA_FOUND THEN
6380 		       NULL;
6381 		 END;
6382                  l_obv_number:=NULL;
6383 		 BEGIN
6384 		    SELECT  object_version_number,
6385 			    config_inst_hdr_id, --added
6386 			    config_inst_item_id, --added
6387 			    config_inst_rev_num,
6388 			    inventory_item_id,
6389 			    last_vld_organization_id,
6390 			    location_type_code,
6391 				instance_status_id  -- added
6392 		    INTO    l_obv_number,
6393 			    l_instance_rec.config_inst_hdr_id, --added
6394 			    l_instance_rec.config_inst_item_id, --added
6395 			    l_instance_rec.config_inst_rev_num,
6396 			    l_item_id,
6397 			    l_vld_org,
6398 			    l_loc_type_code,
6399 				l_instance_status_id
6400 		    FROM    csi_item_instances
6401 		    WHERE   instance_id = l_relship_csr.subject_id;
6402 		    --
6403 		    -- Bug 4232599. Serialized at SO issue items cannot have usage as IN_INVENTORY
6404 		    -- as taken from parent. Since relationship can be broken only thru' RMA,
6405 		    -- usage is set as RETURNED. Other INV/WIP txns will not let serialized at SO issue
6406 		    -- items to be transacted with the serial number. Hence other locations are not considered.
6407 		    Begin
6408 		       select serial_number_control_code
6409 		       into l_srl_ctl
6410 		       from MTL_SYSTEM_ITEMS_B
6411 		       where inventory_item_id = l_item_id
6412 		       and   organization_id = l_vld_org;
6413 		       --
6414 		       IF l_srl_ctl = 6 THEN
6415 			  IF l_loc_type_code = 'INVENTORY' THEN
6416 			     l_ins_usage_code := 'RETURNED';
6417 			  END IF;
6418 		       END IF;
6419 		    Exception
6420 		       when no_data_found then
6421 			  null;
6422 		    End;
6423 		 EXCEPTION
6424 		     WHEN NO_DATA_FOUND THEN
6425 		       NULL;
6426 		 END;
6427                  l_found:=TRUE;
6428               END IF;
6429         END;
6430         END IF;
6431 
6432 
6433         --Added for MACD lock functionality
6434         IF l_relship_csr.object_id IS NOT NULL AND
6435            l_relship_csr.object_id <> fnd_api.g_miss_num
6436         THEN
6437 			--Added for Bug 12357214
6438 			BEGIN
6439 				SELECT l_lock_id
6440 				  INTO l_lock_id
6441 				  FROM csi_item_instance_locks
6442 				 WHERE lock_status IS NOT NULL
6443 				   AND lock_status <> 0
6444 				   AND instance_id = l_relship_csr.object_id
6445 				   AND nvl(lock_source_header_ref, fnd_api.g_miss_char) <> nvl(p_txn_rec.SOURCE_HEADER_REF, fnd_api.g_miss_char);
6446 
6447 				l_is_locked := TRUE;
6448 			EXCEPTION
6449 			      WHEN NO_DATA_FOUND THEN
6450 			           l_is_locked  := FALSE;
6451 			      WHEN TOO_MANY_ROWS THEN
6452 			           l_is_locked := TRUE;
6453 			END;
6454 
6455 		    --Changed the IF condition for Bug 12357214
6456 		   	--IF csi_item_instance_pvt.check_item_instance_lock( p_instance_id => l_relship_csr.object_id)
6457 		   IF (l_is_locked)
6458            THEN
6459 	   --Added the below if condition for bug 5217556--
6460              IF p_txn_rec.transaction_type_id NOT IN (53,54)
6461               THEN
6462               FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
6463               FND_MESSAGE.SET_TOKEN('INSTANCE_ID',l_relship_csr.object_id);
6464               FND_MSG_PUB.ADD;
6465               RAISE FND_API.G_EXC_ERROR;
6466              END IF;
6467            END IF;
6468         END IF;
6469 
6470         IF l_relship_csr.subject_id IS NOT NULL AND
6471            l_relship_csr.subject_id <> fnd_api.g_miss_num
6472         THEN
6473 	--Added for Bug 12357214
6474 			BEGIN
6475 				SELECT l_lock_id
6476 				  INTO l_lock_id
6477 				  FROM csi_item_instance_locks
6478 				 WHERE lock_status IS NOT NULL
6479 				   AND lock_status <> 0
6480 				   AND instance_id= l_relship_csr.subject_id
6481 				   AND nvl(lock_source_header_ref, fnd_api.g_miss_char) <> nvl(p_txn_rec.SOURCE_HEADER_REF, fnd_api.g_miss_char);
6482 
6483 				l_is_locked := TRUE;
6484 			EXCEPTION
6485 			      WHEN NO_DATA_FOUND THEN
6486 			           l_is_locked  := FALSE;
6487 			      WHEN TOO_MANY_ROWS THEN
6488 			           l_is_locked := TRUE;
6489 			END;
6490 	   --Changed the IF condition for Bug 12357214
6491            --IF csi_item_instance_pvt.check_item_instance_lock( p_instance_id => l_relship_csr.subject_id)
6492 	IF (l_is_locked)
6493            THEN
6494              FND_MESSAGE.SET_NAME('CSI','CSI_LOCKED_INSTANCE');
6495              FND_MESSAGE.SET_TOKEN('INSTANCE_ID',l_relship_csr.subject_id);
6496              FND_MSG_PUB.ADD;
6497              RAISE FND_API.G_EXC_ERROR;
6498            END IF;
6499         END IF;
6500         -- End addition for MACD lock functionality
6501 
6502 
6503 
6504       csi_ii_relationships_pkg.update_row(
6505           p_relationship_id             => p_relationship_rec.relationship_id,
6506           p_relationship_type_code      => fnd_api.g_miss_char,
6507           p_object_id                   => fnd_api.g_miss_num,
6508           p_subject_id                  => fnd_api.g_miss_num,
6509           p_position_reference          => fnd_api.g_miss_char,
6510           p_active_start_date           => fnd_api.g_miss_date,
6511           p_active_end_date             => l_sysdate,
6512           p_display_order               => fnd_api.g_miss_num,
6513           p_mandatory_flag              => fnd_api.g_miss_char,
6514           p_context                     => fnd_api.g_miss_char,
6515           p_attribute1                  => fnd_api.g_miss_char,
6516           p_attribute2                  => fnd_api.g_miss_char,
6517           p_attribute3                  => fnd_api.g_miss_char,
6518           p_attribute4                  => fnd_api.g_miss_char,
6519           p_attribute5                  => fnd_api.g_miss_char,
6520           p_attribute6                  => fnd_api.g_miss_char,
6521           p_attribute7                  => fnd_api.g_miss_char,
6522           p_attribute8                  => fnd_api.g_miss_char,
6523           p_attribute9                  => fnd_api.g_miss_char,
6524           p_attribute10                 => fnd_api.g_miss_char,
6525           p_attribute11                 => fnd_api.g_miss_char,
6526           p_attribute12                 => fnd_api.g_miss_char,
6527           p_attribute13                 => fnd_api.g_miss_char,
6528           p_attribute14                 => fnd_api.g_miss_char,
6529           p_attribute15                 => fnd_api.g_miss_char,
6530           p_created_by                  => fnd_api.g_miss_num,
6531           p_creation_date               => fnd_api.g_miss_date,
6532           p_last_updated_by             => fnd_global.user_id,
6533           p_last_update_date            => l_sysdate,
6534           p_last_update_login           => fnd_global.conc_login_id,
6535           p_object_version_number       => fnd_api.g_miss_num);
6536 
6537 
6538            csi_transactions_pvt.create_transaction
6539           (
6540              p_api_version            => p_api_version
6541             ,p_commit                 => p_commit
6542             ,p_init_msg_list          => p_init_msg_list
6543             ,p_validation_level       => p_validation_level
6544             ,p_success_if_exists_flag => 'Y'
6545             ,p_transaction_rec        => p_txn_rec
6546             ,x_return_status          => x_return_status
6547             ,x_msg_count              => x_msg_count
6548             ,x_msg_data               => x_msg_data
6549           );
6550 
6551          IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6552               fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_TXN');
6553               fnd_message.set_token('transaction_id',p_txn_rec.transaction_id );
6554                   fnd_msg_pub.add;
6555               RAISE fnd_api.g_exc_error;
6556               ROLLBACK TO create_relationship_pvt;
6557          END IF;
6558 
6559                  l_instance_rec:=l_temp_ins_rec;
6560                  l_instance_rec.instance_id:= l_relship_csr.subject_id;
6561 --validation for serialized item instance
6562           ---Base Bug 14349178
6563                IF NVL(l_serial_number, FND_API.G_MISS_CHAR) <> FND_API.G_MISS_CHAR
6564                    THEN
6565                  -- Base Bug 9748923/FP Bug 10091407
6566                  l_instance_rec.instance_usage_code :=l_ins_usage_code; --Uncommented for bug#13729720
6567                  END IF;
6568 				 --Added Bug 10327260
6569 				 l_instance_rec.instance_status_id := l_instance_status_id;
6570                  l_instance_rec.object_version_number :=l_obv_number;
6571                  -- TSO with equipment changes.
6572                  -- Nullify the keys when the relationship is broken
6573                  l_instance_rec.config_inst_hdr_id := null;
6574                  l_instance_rec.config_inst_rev_num := null;
6575                  l_instance_rec.config_inst_item_id := null;
6576                  l_instance_rec.config_valid_status := null;
6577                 IF l_instance_rec.instance_id IS NOT NULL AND
6578                    l_instance_rec.object_version_number IS NOT NULL AND
6579                    l_found
6580                 THEN
6581                  csi_gen_utility_pvt.put_line('Calling Update II from Exp Rel...');
6582 				 csi_item_instance_pvt.update_item_instance
6583                  (   p_api_version             =>  p_api_version
6584                     ,p_commit                  =>  p_commit
6585                     ,p_init_msg_list           =>  p_init_msg_list
6586                     ,p_validation_level        =>  p_validation_level
6587                     ,p_instance_rec            =>  l_instance_rec
6588                     ,p_txn_rec                 =>  p_txn_rec
6589                     ,x_instance_id_lst         =>  l_instance_id_lst
6590                     ,x_return_status           =>  x_return_status
6591                     ,x_msg_count               =>  x_msg_count
6592                     ,x_msg_data                =>  x_msg_data
6593                     ,p_item_attribute_tbl      =>  l_item_attribute_tbl
6594                     ,p_location_tbl            =>  l_location_tbl
6595                     ,p_generic_id_tbl          =>  l_generic_id_tbl
6596                     ,p_lookup_tbl              =>  l_lookup_tbl
6597                     ,p_ins_count_rec           =>  l_ins_count_rec
6598                     ,p_called_from_rel         =>  fnd_api.g_true
6599                     ,p_oks_txn_inst_tbl        =>  px_oks_txn_inst_tbl
6600                     ,p_child_inst_tbl          =>  px_child_inst_tbl
6601                 );
6602 
6603 
6604                    IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6605                         fnd_message.set_name('CSI','CSI_FAILED_TO_VALIDATE_INS');
6606                         fnd_message.set_token('instance_id',l_instance_rec.instance_id);
6607                         fnd_msg_pub.add;
6608                         RAISE fnd_api.g_exc_error;
6609                    END IF;
6610                 END IF;
6611 
6612         -- End Addition by sk for bug 2151750
6613           l_old_relship_rec.relationship_id         :=  l_relship_csr.relationship_id;
6614           l_old_relship_rec.relationship_type_code  :=  l_relship_csr.relationship_id;
6615           l_old_relship_rec.object_id               :=  l_relship_csr.object_id;
6616           l_old_relship_rec.subject_id              :=  l_relship_csr.subject_id;
6617           l_old_relship_rec.position_reference      :=  l_relship_csr.position_reference;
6618           l_old_relship_rec.active_start_date       :=  l_relship_csr.active_start_date;
6619           l_old_relship_rec.active_end_date         :=  l_relship_csr.active_end_date;
6620           l_old_relship_rec.display_order           :=  l_relship_csr.display_order;
6621           l_old_relship_rec.mandatory_flag          :=  l_relship_csr.mandatory_flag;
6622           l_old_relship_rec.context                 :=  l_relship_csr.context;
6623           l_old_relship_rec.attribute1              :=  l_relship_csr.attribute1;
6624           l_old_relship_rec.attribute2              :=  l_relship_csr.attribute2;
6625           l_old_relship_rec.attribute3              :=  l_relship_csr.attribute3;
6626           l_old_relship_rec.attribute4              :=  l_relship_csr.attribute4;
6627           l_old_relship_rec.attribute5              :=  l_relship_csr.attribute5;
6628           l_old_relship_rec.attribute6              :=  l_relship_csr.attribute6;
6629           l_old_relship_rec.attribute7              :=  l_relship_csr.attribute7;
6630           l_old_relship_rec.attribute8              :=  l_relship_csr.attribute8;
6631           l_old_relship_rec.attribute9              :=  l_relship_csr.attribute9;
6632           l_old_relship_rec.attribute10             :=  l_relship_csr.attribute10;
6633           l_old_relship_rec.attribute11             :=  l_relship_csr.attribute11;
6634           l_old_relship_rec.attribute12             :=  l_relship_csr.attribute12;
6635           l_old_relship_rec.attribute13             :=  l_relship_csr.attribute13;
6636           l_old_relship_rec.attribute14             :=  l_relship_csr.attribute14;
6637           l_old_relship_rec.attribute15             :=  l_relship_csr.attribute15;
6638           l_old_relship_rec.object_version_number   :=  l_relship_csr.object_version_number;
6639 
6640 
6641                 validate_history(p_old_relship_rec  =>  l_old_relship_rec,
6642                                  p_new_relship_rec  =>  l_new_relship_rec,
6643                                  p_transaction_id   =>  p_txn_rec.transaction_id,
6644                                  p_flag             =>  'EXPIRE',
6645                                  p_sysdate          =>  l_sysdate,
6646                                  x_return_status    =>  x_return_status,
6647                                  x_msg_count        =>  x_msg_count,
6648                                  x_msg_data         =>  x_msg_data
6649                                  );
6650 
6651 
6652 
6653       IF x_return_status <> fnd_api.g_ret_sts_success THEN
6654               RAISE fnd_api.g_exc_error;
6655       END IF;
6656 
6657       -- standard check for p_commit
6658       IF fnd_api.to_boolean( p_commit )
6659       THEN
6660           COMMIT WORK;
6661       END IF;
6662 
6663       -- standard call to get message count and if count is 1, get message info.
6664       fnd_msg_pub.count_AND_get
6665       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6666       p_count          =>   x_msg_count,
6667          p_data           =>   x_msg_data
6668       );
6669 
6670       EXCEPTION
6671           WHEN fnd_api.g_exc_error THEN
6672                 ROLLBACK TO expire_relationship_pvt;
6673                 x_return_status := fnd_api.g_ret_sts_error ;
6674                 fnd_msg_pub.count_AND_get
6675                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6676            p_count => x_msg_count ,
6677                          p_data => x_msg_data
6678                         );
6679 
6680           WHEN fnd_api.g_exc_unexpected_error THEN
6681                 ROLLBACK TO expire_relationship_pvt;
6682                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
6683                 fnd_msg_pub.count_AND_get
6684                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6685            p_count => x_msg_count ,
6686                          p_data => x_msg_data
6687                         );
6688 
6689           WHEN OTHERS THEN
6690                 ROLLBACK TO expire_relationship_pvt;
6691                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
6692                   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
6693                          fnd_msg_pub.add_exc_msg(g_pkg_name ,l_api_name);
6694                   END IF;
6695                 fnd_msg_pub.count_AND_get
6696                         (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6697            p_count => x_msg_count ,
6698                          p_data => x_msg_data
6699                         );
6700 END expire_relationship;
6701 
6702 
6703 PROCEDURE validate_relationship_id (
6704     p_init_msg_list              IN   VARCHAR2,
6705     p_validation_mode            IN   VARCHAR2,
6706     p_relationship_id            IN   NUMBER,
6707     x_return_status              OUT NOCOPY  VARCHAR2,
6708     x_msg_count                  OUT NOCOPY  NUMBER,
6709     x_msg_data                   OUT NOCOPY  VARCHAR2
6710     )
6711 IS
6712 l_dummy         VARCHAR2(1);
6713 BEGIN
6714 
6715       -- initialize message list IF p_init_msg_list IS set TO true.
6716       IF fnd_api.to_boolean( p_init_msg_list )
6717       THEN
6718           fnd_msg_pub.initialize;
6719       END IF;
6720 
6721 
6722       -- initialize api return status to success
6723       x_return_status := fnd_api.g_ret_sts_success;
6724      IF(p_validation_mode ='CREATE') THEN
6725       -- validate not null column
6726       IF ( (p_relationship_id IS NOT NULL) AND (p_relationship_id <> fnd_api.g_miss_num) )
6727       /* if p_relationship_id is not null check if it already exists if found return success else error*/
6728       THEN
6729           BEGIN
6730           SELECT 'x'
6731           INTO   l_dummy
6732           FROM   csi_ii_relationships
6733           WHERE  relationship_id=p_relationship_id
6734           AND    ROWNUM=1;
6735            fnd_message.set_name('CSI','CSI_INVALID_RELSHIPID');
6736            fnd_message.set_token('relationship_id',p_relationship_id);
6737            fnd_msg_pub.add;
6738            x_return_status := fnd_api.g_ret_sts_error;
6739           EXCEPTION
6740              WHEN NO_DATA_FOUND THEN
6741                  x_return_status := fnd_api.g_ret_sts_success;
6742 
6743           END;
6744 
6745       ELSE
6746          /* if p_relationship_id is null then return success */
6747                x_return_status := fnd_api.g_ret_sts_success;
6748       END IF;
6749      ELSIF ( (p_validation_mode = 'UPDATE') OR (p_validation_mode = 'EXPIRE') ) THEN
6750          IF ( (p_relationship_id IS  NOT NULL) AND (p_relationship_id <> fnd_api.g_miss_num) ) THEN
6751              BEGIN
6752              /* Added the condition 'AND    ACTIVE_END_DATE IS NULL' to avoid updating expired relationship */
6753                      SELECT 'x'
6754                      INTO   l_dummy
6755                      FROM   csi_ii_relationships
6756                      WHERE  relationship_id=p_relationship_id;
6757                     -- AND   ( ACTIVE_END_DATE IS NULL
6758                     -- OR     ACTIVE_END_DATE >SYSDATE)
6759                     -- AND    ROWNUM=1;
6760                     -- IF SQL%FOUND THEN
6761                       x_return_status := fnd_api.g_ret_sts_success;
6762                     -- END IF;
6763               EXCEPTION
6764                  WHEN NO_DATA_FOUND THEN
6765                         fnd_message.set_name('CSI', 'CSI_INVALID_RELSHIPID');
6766                         fnd_msg_pub.add;
6767                         x_return_status := fnd_api.g_ret_sts_error;
6768               END;
6769           ELSE
6770                         fnd_message.set_name('CSI', 'CSI_NO_RELSHIP_ID_PASSED');
6771                         fnd_msg_pub.add;
6772                         x_return_status := fnd_api.g_ret_sts_error;
6773           END IF;
6774      END IF;
6775 
6776 
6777 
6778             -- standard call to get message count and if count is 1, get message info.
6779       fnd_msg_pub.count_AND_get
6780       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6781       p_count          =>   x_msg_count,
6782          p_data           =>   x_msg_data
6783       );
6784 
6785 END validate_relationship_id;
6786 
6787 
6788 PROCEDURE validate_rel_type_code (
6789     p_init_msg_list              IN   VARCHAR2,
6790     p_validation_mode            IN   VARCHAR2,
6791     p_relationship_type_code     IN   VARCHAR2,
6792     x_return_status              OUT NOCOPY  VARCHAR2,
6793     x_msg_count                  OUT NOCOPY  NUMBER,
6794     x_msg_data                   OUT NOCOPY  VARCHAR2
6795     )
6796 IS
6797 l_dummy         VARCHAR2(1);
6798 BEGIN
6799       -- initialize message list if p_init_msg_list is set to true.
6800       IF fnd_api.to_boolean( p_init_msg_list )
6801       THEN
6802           fnd_msg_pub.initialize;
6803       END IF;
6804 
6805       -- initialize api return status to success
6806       x_return_status := fnd_api.g_ret_sts_success;
6807          IF(p_validation_mode ='CREATE')
6808          THEN
6809           IF ((p_relationship_type_code IS NOT NULL) AND (p_relationship_type_code <> fnd_api.g_miss_char)) THEN
6810              BEGIN
6811                SELECT 'x'
6812                INTO   l_dummy
6813                FROM   csi_ii_relation_types
6814                WHERE  relationship_type_code=p_relationship_type_code;
6815 
6816                IF SQL%FOUND THEN
6817                 x_return_status := fnd_api.g_ret_sts_success;
6818                END IF;
6819 
6820              EXCEPTION
6821               WHEN NO_DATA_FOUND THEN
6822                 fnd_message.set_name('CSI', 'CSI_INVALID_RELSHIP_CODE');
6823                 fnd_message.set_token('relationship_type_code',p_relationship_type_code);
6824                 fnd_msg_pub.add;
6825                 x_return_status := fnd_api.g_ret_sts_error;
6826               END;
6827           ELSE
6828              fnd_message.set_name('CSI', 'CSI_NO_RELSHIP_CODE');
6829              --fnd_message.set_token('relationship_type_code',p_relationship_type_code);
6830              fnd_msg_pub.add;
6831              x_return_status := fnd_api.g_ret_sts_error;
6832 
6833           END IF;
6834       ELSIF(p_validation_mode ='UPDATE')
6835       THEN
6836           IF ((p_relationship_type_code IS NOT NULL) AND (p_relationship_type_code <> fnd_api.g_miss_char)) THEN
6837               BEGIN
6838                SELECT 'x'
6839                INTO   l_dummy
6840                FROM   csi_ii_relation_types
6841                WHERE  relationship_type_code=p_relationship_type_code;
6842                IF SQL%FOUND THEN
6843                   x_return_status := fnd_api.g_ret_sts_success;
6844                END IF;
6845               EXCEPTION
6846                WHEN NO_DATA_FOUND THEN
6847                 fnd_message.set_name('CSI', 'CSI_INVALID_RELSHIP_CODE');
6848                 fnd_message.set_token('relationship_type_code',p_relationship_type_code);
6849                 fnd_msg_pub.add;
6850                 x_return_status := fnd_api.g_ret_sts_error;
6851               END;
6852            ELSIF p_relationship_type_code IS NULL -- Added for bug 2151750
6853            THEN
6854              fnd_message.set_name('CSI', 'CSI_NO_RELSHIP_CODE');
6855              fnd_msg_pub.add;
6856              x_return_status := fnd_api.g_ret_sts_error;
6857            END IF;
6858       END IF;
6859       -- standard call to get message count and if count is 1, get message info.
6860       fnd_msg_pub.count_AND_get
6861       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
6862       p_count          =>   x_msg_count,
6863          p_data           =>   x_msg_data
6864       );
6865 
6866 END validate_rel_type_code;
6867 
6868 
6869 PROCEDURE validate_object_id (
6870     p_init_msg_list              IN   VARCHAR2,
6871     p_validation_mode            IN   VARCHAR2,
6872     p_object_id                  IN   NUMBER,
6873     x_return_status              OUT NOCOPY  VARCHAR2,
6874     x_msg_count                  OUT NOCOPY  NUMBER,
6875     x_msg_data                   OUT NOCOPY  VARCHAR2
6876     )
6877 IS
6878 l_dummy                     VARCHAR2(1);
6879 l_instance_id               NUMBER;
6880 l_quantity                  NUMBER;
6881 l_location_type_code        VARCHAR2(30);
6882 l_inventory_item_id         NUMBER;
6883 l_serial_code               NUMBER;
6884 l_item_type                 VARCHAR2(30);
6885 l_bom_item_type             NUMBER;
6886 l_vld_org_id                NUMBER;
6887 l_pick_comp_flag            VARCHAR2(1);
6888 l_base_item_id              NUMBER;
6889 l_repl_order_flag           VARCHAR2(1);
6890 l_active                    VARCHAR2(1);
6891 BEGIN
6892 
6893       -- initialize message list if p_init_msg_list is set to true.
6894       IF fnd_api.to_boolean( p_init_msg_list )
6895       THEN
6896           fnd_msg_pub.initialize;
6897       END IF;
6898 
6899 
6900       -- initialize api return status to success
6901       x_return_status := fnd_api.g_ret_sts_success;
6902 
6903       -- validate not null column
6904       IF(p_validation_mode ='CREATE')
6905       THEN
6906           IF ((p_object_id IS NOT NULL) AND (p_object_id <> fnd_api.g_miss_num)) THEN
6907           -- The following code has been added by sguthiva for bug 2416144.
6908              BEGIN
6909                 SELECT 'x'
6910                 INTO   l_active
6911                 FROM   csi_item_instances cii
6912                 WHERE  cii.instance_id=p_object_id
6913                 AND   (SYSDATE BETWEEN NVL(cii.active_start_date, SYSDATE) AND NVL(cii.active_end_date, SYSDATE));
6914              EXCEPTION
6915                WHEN NO_DATA_FOUND THEN
6916                 fnd_message.set_name('CSI', 'CSI_EXPIRED_OBJECT');
6917                 fnd_message.set_token('object_id',p_object_id);
6918                 fnd_msg_pub.add;
6919                 x_return_status := fnd_api.g_ret_sts_error;
6920                 RAISE fnd_api.g_exc_error;
6921              END;
6922           -- End of addition for bug 2416144.
6923 
6924              BEGIN
6925              -- Modified by sk for bug 2266166
6926              SELECT instance_id
6927                    ,quantity
6928                    ,location_type_code
6929                    ,inventory_item_id
6930                    ,last_vld_organization_id
6931              INTO   l_instance_id
6932                    ,l_quantity
6933                    ,l_location_type_code
6934                    ,l_inventory_item_id
6935                    ,l_vld_org_id
6936              FROM   csi_item_instances
6937              WHERE  instance_id=p_object_id;
6938                  IF l_quantity IS NOT NULL THEN
6939                      IF l_quantity<>1 THEN
6940                         fnd_message.set_name('CSI', 'CSI_QTY_NOTEQUAL_TO_ONE');
6941                         fnd_message.set_token('object_id',p_object_id);
6942                         fnd_msg_pub.add;
6943                         x_return_status := fnd_api.g_ret_sts_error;
6944                      END IF;
6945                  END IF;
6946                  /* commented by sk for bug 2151750
6947                  IF l_location_type_code IS NOT NULL THEN
6948                     IF UPPER(l_location_type_code) = 'INVENTORY' THEN
6949                         fnd_message.set_name('CSI', 'CSI_INVALID_LOCATION_TYPE');
6950                         fnd_message.set_token('object_id',p_object_id);
6951                         fnd_msg_pub.add;
6952                         x_return_status := fnd_api.g_ret_sts_error;
6953                     END IF;
6954                   END IF;
6955                   */
6956                     BEGIN
6957                         SELECT serial_number_control_code
6958                               ,item_type
6959                               ,bom_item_type
6960                               ,pick_components_flag
6961                               ,base_item_id             -- Added by rk on 9-Apr
6962                               ,replenish_to_order_flag  -- for bug 2304221
6963                         INTO   l_serial_code
6964                               ,l_item_type
6965                               ,l_bom_item_type
6966                               ,l_pick_comp_flag
6967                               ,l_base_item_id
6968                               ,l_repl_order_flag
6969                         FROM   mtl_system_items
6970                         WHERE  inventory_item_id = l_inventory_item_id
6971                         AND    organization_id   = l_vld_org_id ;
6972                 -- Item is under serial control but serial_number is NULL
6973                 -- '1' stands for - No serial number control
6974                 -- '2' stands for - Predefined serial numbers
6975                 -- '5' stands for - Dynamic entry at inventory receipt
6976                 -- '6' stands for - Dynamic entry at sales order issue
6977                 -- Passed object_id should be a serialized item if not it should also be an ATO/PTO item type.
6978                 -- below code is commented by rtalluri on 05/20/02 for the bug 2255773
6979                 /*
6980                         IF NVL(l_serial_code,0) IN  (2,5,6) THEN --SERIALIZED ITEM
6981                            NULL;
6982                         ELSIF NVL(l_serial_code,0)=1 AND
6983                               ( l_bom_item_type IN (1,2)
6984                               OR
6985                                 l_pick_comp_flag = 'Y'
6986                               OR
6987                               ( l_base_item_id IS NOT NULL AND l_repl_order_flag = 'Y'))  -- fix for bug 2304221
6988                  */
6989                         IF l_quantity = 1
6990                         THEN
6991 --AND NVL(l_item_type,'X') IN ('ATO','PTO') THEN --NON-SERIALIZED ITEM
6992 -- Modified by sk for bug 2266166
6993                            NULL;
6994                         ELSE
6995                          fnd_message.set_name('CSI', 'CSI_NON_ATO_PTO_ITEM');
6996                          fnd_message.set_token('object_id',p_object_id);
6997                          fnd_msg_pub.add;
6998                          x_return_status := fnd_api.g_ret_sts_error;
6999                         END IF;
7000 
7001                     EXCEPTION
7002                        WHEN OTHERS THEN
7003                        NULL;
7004                     END;
7005              EXCEPTION
7006                WHEN NO_DATA_FOUND THEN
7007                 fnd_message.set_name('CSI', 'CSI_INVALID_OBJECT_ID');
7008                 fnd_message.set_token('object_id',p_object_id);
7009                 fnd_msg_pub.add;
7010                 x_return_status := fnd_api.g_ret_sts_error;
7011              END;
7012           ELSE
7013              fnd_message.set_name('CSI', 'CSI_NO_OBJ_ID_PASSED');
7014              fnd_msg_pub.add;
7015              x_return_status := fnd_api.g_ret_sts_error;
7016            END IF;
7017 
7018       ELSIF(p_validation_mode ='UPDATE')
7019       THEN
7020           IF ((p_object_id IS NOT NULL) AND (p_object_id <> fnd_api.g_miss_num)) THEN
7021              BEGIN
7022              -- Modified by sk for bug 2266166
7023              SELECT instance_id
7024                    ,quantity
7025                    ,location_type_code
7026                    ,inventory_item_id
7027                    ,last_vld_organization_id
7028              INTO   l_instance_id
7029                    ,l_quantity
7030                    ,l_location_type_code
7031                    ,l_inventory_item_id
7032                    ,l_vld_org_id
7033              FROM   csi_item_instances
7034              WHERE  instance_id=p_object_id;
7035 
7036                  IF l_quantity IS NOT NULL THEN
7037                      IF l_quantity<>1 THEN
7038                         fnd_message.set_name('CSI', 'CSI_QTY_NOTEQUAL_TO_ONE');
7039                         fnd_message.set_token('object_id',p_object_id);
7040                         fnd_msg_pub.add;
7041                         x_return_status := fnd_api.g_ret_sts_error;
7042                      END IF;
7043                  END IF;
7044                  /* commented by sk for bug 2151750
7045                  IF l_location_type_code IS NOT NULL THEN
7046                     IF UPPER(l_location_type_code) = 'INVENTORY' THEN
7047                         fnd_message.set_name('CSI', 'CSI_INVALID_LOCATION_TYPE');
7048                         fnd_message.set_token('object_id',p_object_id);
7049                         fnd_msg_pub.add;
7050                         x_return_status := fnd_api.g_ret_sts_error;
7051                     END IF;
7052                   END IF;
7053                   */
7054                     BEGIN
7055                         SELECT serial_number_control_code
7056                               ,item_type
7057                               ,bom_item_type
7058                               ,pick_components_flag
7059                               ,base_item_id
7060                               ,replenish_to_order_flag
7061                         INTO   l_serial_code
7062                               ,l_item_type
7063                               ,l_bom_item_type
7064                               ,l_pick_comp_flag
7065                               ,l_base_item_id        -- fix for bug 2304221
7066                               ,l_repl_order_flag
7067                         FROM   mtl_system_items
7068                         WHERE  inventory_item_id = l_inventory_item_id
7069                         AND    organization_id   = l_vld_org_id ;
7070                 -- Item is under serial control but serial_number is NULL
7071                 -- '1' stands for - No serial number control
7072                 -- '2' stands for - Predefined serial numbers
7073                 -- '5' stands for - Dynamic entry at inventory receipt
7074                 -- '6' stands for - Dynamic entry at sales order issue
7075                 -- Passed object_id should be a serialized item if not it should also be an ATO/PTO item type.
7076                 -- below code is commented by rtalluri on 05/20/02 for the bug 2255773
7077                 /*
7078                         IF NVL(l_serial_code,0) IN  (2,5,6) THEN --SERIALIZED ITEM
7079                            NULL;
7080                         ELSIF NVL(l_serial_code,0)=1 AND
7081                               ( l_bom_item_type IN (1,2)
7082                               OR
7083                                 l_pick_comp_flag = 'Y'
7084                               OR
7085                               ( l_base_item_id IS NOT NULL AND l_repl_order_flag = 'Y'))  -- fix for bug 2304221
7086                  */
7087                         IF l_quantity = 1
7088                         THEN
7089 --NVL(l_item_type,'X') IN ('ATO','PTO') THEN --NON-SERIALIZED ITEM
7090 -- Modified by sk for bug 2266166
7091                            NULL;
7092                         ELSE
7093                          fnd_message.set_name('CSI', 'CSI_NON_ATO_PTO_ITEM');
7094                          fnd_message.set_token('object_id',p_object_id);
7095                          fnd_msg_pub.add;
7096                          x_return_status := fnd_api.g_ret_sts_error;
7097                         END IF;
7098 
7099                     EXCEPTION
7100                        WHEN OTHERS THEN
7101                        NULL;
7102                     END;
7103 
7104              EXCEPTION
7105                WHEN NO_DATA_FOUND THEN
7106                 fnd_message.set_name('CSI', 'CSI_INVALID_OBJECT_ID');
7107                 fnd_message.set_token('object_id',p_object_id);
7108                 fnd_msg_pub.add;
7109                 x_return_status := fnd_api.g_ret_sts_error;
7110              END;
7111           END IF;
7112       END IF;
7113 
7114             -- standard call to get message count and if count is 1, get message info.
7115       fnd_msg_pub.count_and_get
7116       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7117       p_count          =>   x_msg_count,
7118          p_data           =>   x_msg_data
7119       );
7120 
7121 END validate_object_id;
7122 
7123 
7124 PROCEDURE validate_subject_id (
7125     p_init_msg_list              IN   VARCHAR2,
7126     p_validation_mode            IN   VARCHAR2,
7127     p_subject_id                 IN   NUMBER,
7128     x_return_status              OUT NOCOPY  VARCHAR2,
7129     x_msg_count                  OUT NOCOPY  NUMBER,
7130     x_msg_data                   OUT NOCOPY  VARCHAR2
7131     )
7132 IS
7133 l_dummy                     VARCHAR2(1);
7134 l_instance_id               NUMBER;
7135 l_quantity                  NUMBER;
7136 l_location_type_code        VARCHAR2(30);
7137 l_active                    VARCHAR2(1);
7138 BEGIN
7139 
7140       -- initialize message list if p_init_msg_list is set to true.
7141       IF fnd_api.to_boolean( p_init_msg_list )
7142       THEN
7143           fnd_msg_pub.initialize;
7144       END IF;
7145 
7146 
7147       -- initialize API RETURN status TO success
7148       x_return_status := fnd_api.g_ret_sts_success;
7149 
7150           IF ((p_subject_id IS NOT NULL) AND (p_subject_id <> fnd_api.g_miss_num)) THEN
7151           -- The following code has been added by sguthiva for bug 2416144.
7152             IF (p_validation_mode ='CREATE')
7153             THEN
7154              BEGIN
7155                 SELECT 'x'
7156                 INTO   l_active
7157                 FROM   csi_item_instances cii
7158                 WHERE  cii.instance_id=p_subject_id
7159                 AND   (SYSDATE BETWEEN NVL(cii.active_start_date, SYSDATE) AND NVL(cii.active_end_date, SYSDATE));
7160              EXCEPTION
7161                WHEN NO_DATA_FOUND THEN
7162                 fnd_message.set_name('CSI', 'CSI_EXPIRED_SUBJECT');
7163                 fnd_message.set_token('subject_id',p_subject_id);
7164                 fnd_msg_pub.add;
7165                 x_return_status := fnd_api.g_ret_sts_error;
7166                 RAISE fnd_api.g_exc_error;
7167              END;
7168             END IF;
7169           -- End of addition for bug 2416144.
7170              BEGIN
7171              SELECT instance_id,location_type_code
7172              INTO   l_instance_id,l_location_type_code
7173              FROM   csi_item_instances
7174              WHERE  instance_id=p_subject_id;
7175              /* commented by sk for bug 2151750
7176                    IF l_location_type_code IS NOT NULL THEN
7177                     IF UPPER(l_location_type_code) = 'INVENTORY' THEN
7178                         fnd_message.set_name('CSI', 'CSI_INVALID_LOCATION_TYPE');
7179                         fnd_message.set_token('subject_id',p_subject_id);
7180                         fnd_msg_pub.add;
7181                         x_return_status := fnd_api.g_ret_sts_error;
7182                     END IF;
7183                    END IF;
7184               */
7185              EXCEPTION
7186                WHEN NO_DATA_FOUND THEN
7187                 fnd_message.set_name('CSI', 'CSI_INVALID_SUBJECT_ID');
7188                 fnd_message.set_token('subject_id',p_subject_id);
7189                 fnd_msg_pub.add;
7190                 x_return_status := fnd_api.g_ret_sts_error;
7191                END;
7192            END IF;
7193       -- standard call to get message count and if count is 1, get message info.
7194       fnd_msg_pub.count_AND_get
7195       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7196       p_count          =>   x_msg_count,
7197          p_data           =>   x_msg_data
7198       );
7199 
7200 END validate_subject_id;
7201 
7202 PROCEDURE validate_active_end_date (
7203     p_init_msg_list              IN   VARCHAR2,
7204     p_validation_mode            IN   VARCHAR2,
7205     p_active_end_date                IN   DATE,
7206     x_return_status              OUT NOCOPY  VARCHAR2,
7207     x_msg_count                  OUT NOCOPY  NUMBER,
7208     x_msg_data                   OUT NOCOPY  VARCHAR2
7209     )
7210 IS
7211 BEGIN
7212       -- initialize message list if p_init_msg_list is set to true.
7213       IF fnd_api.to_boolean( p_init_msg_list )
7214       THEN
7215           fnd_msg_pub.initialize;
7216       END IF;
7217 
7218 
7219       -- initialize api return status to success
7220       x_return_status := fnd_api.g_ret_sts_success;
7221       IF(p_validation_mode ='CREATE') THEN
7222               IF ((p_active_end_date IS NOT NULL) AND (p_active_end_date <> fnd_api.g_miss_date)) THEN
7223                  fnd_message.set_name('CSI', 'CSI_ACTIVE_END_DATE');
7224                  fnd_message.set_token('ACTIVE_END_DATE',p_active_end_date);
7225                  fnd_msg_pub.add;
7226                  x_return_status := fnd_api.g_ret_sts_error;
7227               END IF;
7228        END IF;
7229           -- standard call to get message count and if count is 1, get message info.
7230       fnd_msg_pub.count_AND_get
7231       ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7232       p_count          =>   x_msg_count,
7233          p_data           =>   x_msg_data
7234       );
7235 
7236 END validate_active_end_date;
7237 
7238 
7239 PROCEDURE validate_object_version_num (
7240     p_init_msg_list              IN   VARCHAR2,
7241     p_validation_mode            IN   VARCHAR2,
7242     p_object_version_number      IN   NUMBER,
7243     x_return_status              OUT NOCOPY  VARCHAR2,
7244     x_msg_count                  OUT NOCOPY  NUMBER,
7245     x_msg_data                   OUT NOCOPY  VARCHAR2
7246     )
7247 IS
7248 l_dummy         VARCHAR2(1);
7249 BEGIN
7250 
7251       -- initialize message list if p_init_msg_list is set to true.
7252       IF fnd_api.to_boolean( p_init_msg_list )
7253       THEN
7254           fnd_msg_pub.initialize;
7255       END IF;
7256 
7257 
7258       -- initialize api return status to success
7259       x_return_status := fnd_api.g_ret_sts_success;
7260 
7261       -- validate not null column
7262 
7263 
7264        IF( (p_validation_mode = 'UPDATE') OR (p_validation_mode = 'EXPIRE') ) THEN
7265           IF ( (p_object_version_number IS NULL) OR (p_object_version_number = fnd_api.g_miss_num) ) THEN
7266              fnd_message.set_name('CSI', 'CSI_MISSING_OBJ_VER_NUM');
7267              fnd_msg_pub.add;
7268              x_return_status := fnd_api.g_ret_sts_error;
7269           ELSE
7270              x_return_status := fnd_api.g_ret_sts_success;
7271           END IF;
7272        END IF;
7273 
7274       -- standard call to get message count and if count is 1, get message info.
7275       fnd_msg_pub.count_and_get
7276       (  p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7277       p_count          =>   x_msg_count,
7278          p_data           =>   x_msg_data
7279       );
7280 
7281 END validate_object_version_num;
7282 
7283 PROCEDURE validate_ii_relationships(
7284     p_init_msg_list              IN   VARCHAR2,
7285     p_validation_level           IN   NUMBER,
7286     p_validation_mode            IN   VARCHAR2,
7287     p_ii_relationship_tbl        IN   csi_datastructures_pub.ii_relationship_tbl,
7288     x_return_status              OUT NOCOPY  VARCHAR2,
7289     x_msg_count                  OUT NOCOPY  NUMBER,
7290     x_msg_data                   OUT NOCOPY  VARCHAR2
7291     )
7292 IS
7293 l_api_name   CONSTANT VARCHAR2(30) := 'validate_ii_relationships';
7294 l_ct                  NUMBER;
7295  BEGIN
7296 
7297 
7298 
7299       -- initialize api return status to success
7300       x_return_status := fnd_api.g_ret_sts_success;
7301 
7302       l_ct := p_ii_relationship_tbl.count;
7303  FOR l_count IN 1..l_ct LOOP
7304 
7305 -- The following IF statement has been commented out for Bug: 3271806
7306 --      IF (p_validation_level >= fnd_api.g_valid_level_full) THEN
7307 
7308           validate_relationship_id(
7309               p_init_msg_list          => fnd_api.g_false,
7310               p_validation_mode        => p_validation_mode,
7311               p_relationship_id        => p_ii_relationship_tbl(l_count).relationship_id,
7312               x_return_status          => x_return_status,
7313               x_msg_count              => x_msg_count,
7314               x_msg_data               => x_msg_data);
7315           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7316               RAISE fnd_api.g_exc_error;
7317           END IF;
7318 
7319           validate_rel_type_code(
7320               p_init_msg_list          => fnd_api.g_false,
7321               p_validation_mode        => p_validation_mode,
7322               p_relationship_type_code => p_ii_relationship_tbl(l_count).relationship_type_code,
7323               x_return_status          => x_return_status,
7324               x_msg_count              => x_msg_count,
7325               x_msg_data               => x_msg_data);
7326           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7327               RAISE fnd_api.g_exc_error;
7328           END IF;
7329 
7330           validate_object_id(
7331               p_init_msg_list          => fnd_api.g_false,
7332               p_validation_mode        => p_validation_mode,
7333               p_object_id              => p_ii_relationship_tbl(l_count).object_id,
7334               x_return_status          => x_return_status,
7335               x_msg_count              => x_msg_count,
7336               x_msg_data               => x_msg_data);
7337           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7338               RAISE fnd_api.g_exc_error;
7339           END IF;
7340 
7341           validate_subject_id(
7342               p_init_msg_list          => fnd_api.g_false,
7343               p_validation_mode        => p_validation_mode,
7344               p_subject_id             => p_ii_relationship_tbl(l_count).subject_id,
7345               x_return_status          => x_return_status,
7346               x_msg_count              => x_msg_count,
7347               x_msg_data               => x_msg_data);
7348           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7349               RAISE fnd_api.g_exc_error;
7350           END IF;
7351 
7352           validate_active_end_date(
7353               p_init_msg_list          => fnd_api.g_false,
7354               p_validation_mode        => p_validation_mode,
7355               p_active_end_date        => p_ii_relationship_tbl(l_count).active_end_date,
7356               x_return_status          => x_return_status,
7357               x_msg_count              => x_msg_count,
7358               x_msg_data               => x_msg_data);
7359           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7360               RAISE fnd_api.g_exc_error;
7361           END IF;
7362 
7363         validate_object_version_num (
7364               p_init_msg_list           => fnd_api.g_false,
7365               p_validation_mode         => p_validation_mode,
7366               p_object_version_number   => p_ii_relationship_tbl(l_count).object_version_number,
7367               x_return_status           => x_return_status,
7368               x_msg_count               => x_msg_count,
7369               x_msg_data                => x_msg_data);
7370 
7371           IF x_return_status <> fnd_api.g_ret_sts_success THEN
7372               RAISE fnd_api.g_exc_error;
7373           END IF;
7374 
7375 --      END IF;
7376  END LOOP;
7377 
7378 END validate_ii_relationships;
7379 
7380 
7381 /*----------------------------------------------------------*/
7382 /* Procedure name:  Resolve_id_columns                      */
7383 /* Description : This procudure gets the descriptions for   */
7384 /*               id columns                                 */
7385 /*----------------------------------------------------------*/
7386 
7387 PROCEDURE  Resolve_id_columns
7388             (p_rel_history_tbl  IN OUT NOCOPY  csi_datastructures_pub.relationship_history_tbl)
7389 IS
7390 
7391 BEGIN
7392    IF p_rel_history_tbl.count > 0 THEN
7393       FOR tab_row in p_rel_history_tbl.FIRST..p_rel_history_tbl.LAST
7394       LOOP
7395 	IF p_rel_history_tbl.EXISTS(tab_row) THEN
7396 	    IF ( (p_rel_history_tbl(tab_row).relationship_type_code IS NOT NULL) AND
7397 		 (p_rel_history_tbl(tab_row).relationship_type_code <> FND_API.G_MISS_CHAR) ) THEN
7398 	       BEGIN
7399 		 SELECT name
7400 		 INTO   p_rel_history_tbl(tab_row).relationship_type
7401 		 FROM   csi_ii_relation_types
7402 		 WHERE  relationship_type_code = p_rel_history_tbl(tab_row).relationship_type_code;
7403 	       EXCEPTION
7404 		 WHEN OTHERS THEN
7405 		   NULL;
7406 	       END;
7407 	    END IF;
7408 
7409 	    IF ( (p_rel_history_tbl(tab_row).OLD_SUBJECT_ID IS NOT NULL) AND
7410 		 (p_rel_history_tbl(tab_row).OLD_SUBJECT_ID <> FND_API.G_MISS_NUM) ) THEN
7411 	       BEGIN
7412 		 SELECT instance_number
7413 		 INTO   p_rel_history_tbl(tab_row).old_subject_number
7414 		 FROM   csi_item_instances
7415 		 WHERE  instance_id = p_rel_history_tbl(tab_row).old_subject_id;
7416 	       EXCEPTION
7417 		 WHEN OTHERS THEN
7418 		   NULL;
7419 	       END;
7420 	    END IF;
7421 
7422 	    IF ( (p_rel_history_tbl(tab_row).NEW_SUBJECT_ID IS NOT NULL) AND
7423 		 (p_rel_history_tbl(tab_row).NEW_SUBJECT_ID <> FND_API.G_MISS_NUM) ) THEN
7424 	       BEGIN
7425 		 SELECT instance_number
7426 		 INTO   p_rel_history_tbl(tab_row).new_subject_number
7427 		 FROM   csi_item_instances
7428 		 WHERE  instance_id = p_rel_history_tbl(tab_row).new_subject_id;
7429 	       EXCEPTION
7430 		 WHEN OTHERS THEN
7431 		   NULL;
7432 	       END;
7433 	    END IF;
7434 	  END IF;
7435       END LOOP;
7436    END IF;
7437 END Resolve_id_columns;
7438 
7439 
7440 /*------------------------------------------------------------*/
7441 /* Procedure name:  get_inst_relationship_hist                */
7442 /* Description :    Procedure used to get inst relationships  */
7443 /*                  from history for a given transaction_id   */
7444 /*------------------------------------------------------------*/
7445 
7446 PROCEDURE get_inst_relationship_hist
7447  (    p_api_version             IN  NUMBER
7448      ,p_commit                  IN  VARCHAR2 := fnd_api.g_false
7449      ,p_init_msg_list           IN  VARCHAR2 := fnd_api.g_false
7450      ,p_validation_level        IN  NUMBER   := fnd_api.g_valid_level_full
7451      ,p_transaction_id          IN  NUMBER
7452      ,x_rel_history_tbl         OUT NOCOPY csi_datastructures_pub.relationship_history_tbl
7453      ,x_return_status           OUT NOCOPY VARCHAR2
7454      ,x_msg_count               OUT NOCOPY NUMBER
7455      ,x_msg_data                OUT NOCOPY VARCHAR2
7456     )IS
7457 
7458      l_api_name                 CONSTANT VARCHAR2(30)   := 'get_inst_relationship_hist' ;
7459      l_api_version              CONSTANT NUMBER         := 1.0                          ;
7460      l_csi_debug_level          NUMBER                                                  ;
7461      l_flag                     VARCHAR2(1)             :='N'                           ;
7462      i                          NUMBER                  :=1                             ;
7463      l_relationship_query_rec   csi_datastructures_pub.relationship_query_rec           ;
7464      l_time_stamp               DATE                                                    ;
7465      l_ii_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl              ;
7466 
7467      CURSOR get_relationship_hist(i_transaction_id NUMBER)
7468      IS
7469      SELECT
7470                  cih.relationship_history_id ,
7471                  cih.relationship_id ,
7472                  cih.transaction_id ,
7473                  cih.old_subject_id ,
7474                  cih.new_subject_id ,
7475                  cih.old_position_reference ,
7476                  cih.new_position_reference ,
7477                  cih.old_active_start_date ,
7478                  cih.new_active_start_date ,
7479                  cih.old_active_end_date ,
7480                  cih.new_active_end_date ,
7481                  cih.old_mandatory_flag ,
7482                  cih.new_mandatory_flag ,
7483                  cih.old_context ,
7484                  cih.new_context ,
7485                  cih.old_attribute1 ,
7486                  cih.new_attribute1 ,
7487                  cih.old_attribute2 ,
7488                  cih.new_attribute2 ,
7489                  cih.old_attribute3 ,
7490                  cih.new_attribute3 ,
7491                  cih.old_attribute4 ,
7492                  cih.new_attribute4 ,
7493                  cih.old_attribute5 ,
7494                  cih.new_attribute5 ,
7495                  cih.old_attribute6 ,
7496                  cih.new_attribute6 ,
7497                  cih.old_attribute7 ,
7498                  cih.new_attribute7 ,
7499                  cih.old_attribute8 ,
7500                  cih.new_attribute8 ,
7501                  cih.old_attribute9 ,
7502                  cih.new_attribute9 ,
7503                  cih.old_attribute10 ,
7504                  cih.new_attribute10 ,
7505                  cih.old_attribute11 ,
7506                  cih.new_attribute11 ,
7507                  cih.old_attribute12 ,
7508                  cih.new_attribute12 ,
7509                  cih.old_attribute13 ,
7510                  cih.new_attribute13 ,
7511                  cih.old_attribute14 ,
7512                  cih.new_attribute14 ,
7513                  cih.old_attribute15 ,
7514                  cih.new_attribute15 ,
7515                  cih.full_dump_flag ,
7516                  cih.object_version_number ,
7517                  cir.relationship_type_code ,
7518                  cir.object_id ,
7519                  cih.creation_date
7520      FROM     csi_ii_relationships_h cih ,
7521               csi_ii_relationships cir
7522      WHERE    cih.transaction_id     = i_transaction_id
7523      AND      cih.relationship_id    = cir.relationship_id;
7524 
7525 BEGIN
7526         -- Standard Start of API savepoint
7527         --SAVEPOINT   get_inst_relationship_hist;
7528 
7529         -- Standard call to check for call compatibility.
7530         IF NOT FND_API.Compatible_API_Call (    l_api_version           ,
7531                                                 p_api_version           ,
7532                                                 l_api_name              ,
7533                                                 g_pkg_name              )
7534         THEN
7535            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7536         END IF;
7537 
7538         -- Initialize message list if p_init_msg_list is set to TRUE.
7539         IF FND_API.to_Boolean( p_init_msg_list ) THEN
7540                 FND_MSG_PUB.initialize;
7541         END IF;
7542 
7543         --  Initialize API return status to success
7544         x_return_status := FND_API.G_RET_STS_SUCCESS;
7545 
7546         -- Check the profile option CSI_DEBUG_LEVEL for debug message reporting
7547         l_csi_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
7548 
7549         -- If CSI_DEBUG_LEVEL = 1 then dump the procedure name
7550         IF (l_csi_debug_level > 0) THEN
7551             csi_gen_utility_pvt.put_line( 'get_inst_relationship_hist');
7552         END IF;
7553 
7554         -- If the debug level = 2 then dump all the parameters values.
7555         IF (l_csi_debug_level > 1) THEN
7556             csi_gen_utility_pvt.put_line(  'get_inst_relationship_hist'       ||
7557                                                  p_api_version           ||'-'||
7558                                                  p_commit                ||'-'||
7559                                                  p_init_msg_list         ||'-'||
7560                                                  p_validation_level      ||'-'||
7561                                                  p_transaction_id               );
7562         END IF;
7563 
7564         /***** srramakr commented for bug # 3304439
7565         -- Check for the profile option and enable trace
7566         l_flag:=csi_gen_utility_pvt.enable_trace(l_trace_flag => l_flag);
7567         -- End enable trace
7568         ****/
7569 
7570         -- Start API body
7571 
7572         FOR C1 IN get_relationship_hist(p_transaction_id)
7573         LOOP
7574 
7575              x_rel_history_tbl(i).relationship_history_id    := c1.relationship_history_id;
7576              x_rel_history_tbl(i).relationship_id            := c1.relationship_id;
7577              x_rel_history_tbl(i).transaction_id             := c1.transaction_id;
7578              x_rel_history_tbl(i).old_subject_id             := c1.old_subject_id;
7579              x_rel_history_tbl(i).new_subject_id             := c1.new_subject_id;
7580              x_rel_history_tbl(i).old_position_reference     := c1.old_position_reference;
7581              x_rel_history_tbl(i).new_position_reference     := c1.new_position_reference;
7582              x_rel_history_tbl(i).old_active_start_date      := c1.old_active_start_date;
7583              x_rel_history_tbl(i).new_active_start_date      := c1.new_active_start_date;
7584              x_rel_history_tbl(i).old_active_end_date        := c1.old_active_end_date;
7585              x_rel_history_tbl(i).new_active_end_date        := c1.new_active_end_date;
7586              x_rel_history_tbl(i).old_mandatory_flag         := c1.old_mandatory_flag;
7587              x_rel_history_tbl(i).new_mandatory_flag         := c1.new_mandatory_flag;
7588              x_rel_history_tbl(i).old_context                := c1.old_context;
7589              x_rel_history_tbl(i).new_context                := c1.new_context;
7590              x_rel_history_tbl(i).old_attribute1             := c1.old_attribute1;
7591              x_rel_history_tbl(i).new_attribute1             := c1.new_attribute1;
7592              x_rel_history_tbl(i).old_attribute2             := c1.old_attribute2;
7593              x_rel_history_tbl(i).new_attribute2             := c1.new_attribute2;
7594              x_rel_history_tbl(i).old_attribute3             := c1.old_attribute3;
7595              x_rel_history_tbl(i).new_attribute3             := c1.new_attribute3;
7596              x_rel_history_tbl(i).old_attribute4             := c1.old_attribute4;
7597              x_rel_history_tbl(i).new_attribute4             := c1.new_attribute4;
7598              x_rel_history_tbl(i).old_attribute5             := c1.old_attribute5;
7599              x_rel_history_tbl(i).new_attribute5             := c1.new_attribute5;
7600              x_rel_history_tbl(i).old_attribute6             := c1.old_attribute6;
7601              x_rel_history_tbl(i).new_attribute6             := c1.new_attribute6;
7602              x_rel_history_tbl(i).old_attribute7             := c1.old_attribute7;
7603              x_rel_history_tbl(i).new_attribute7             := c1.new_attribute7;
7604              x_rel_history_tbl(i).old_attribute8             := c1.old_attribute8;
7605              x_rel_history_tbl(i).new_attribute8             := c1.new_attribute8;
7606              x_rel_history_tbl(i).old_attribute9             := c1.old_attribute9;
7607              x_rel_history_tbl(i).new_attribute9             := c1.new_attribute9;
7608              x_rel_history_tbl(i).old_attribute10            := c1.old_attribute10;
7609              x_rel_history_tbl(i).new_attribute10            := c1.new_attribute10;
7610              x_rel_history_tbl(i).old_attribute11            := c1.old_attribute11;
7611              x_rel_history_tbl(i).new_attribute11            := c1.new_attribute11;
7612              x_rel_history_tbl(i).old_attribute12            := c1.old_attribute12;
7613              x_rel_history_tbl(i).new_attribute12            := c1.new_attribute12;
7614              x_rel_history_tbl(i).old_attribute13            := c1.old_attribute13;
7615              x_rel_history_tbl(i).new_attribute13            := c1.new_attribute13;
7616              x_rel_history_tbl(i).old_attribute14            := c1.old_attribute14;
7617              x_rel_history_tbl(i).new_attribute14            := c1.new_attribute14;
7618              x_rel_history_tbl(i).old_attribute15            := c1.old_attribute15;
7619              x_rel_history_tbl(i).new_attribute15            := c1.new_attribute15;
7620              x_rel_history_tbl(i).full_dump_flag             := c1.full_dump_flag;
7621              x_rel_history_tbl(i).object_version_number      := c1.object_version_number;
7622              x_rel_history_tbl(i).relationship_type_code     := c1.relationship_type_code;
7623              x_rel_history_tbl(i).object_id                  := c1.object_id;
7624              x_rel_history_tbl(i).creation_date              := c1.creation_date;
7625 
7626 
7627              IF (
7628                   (x_rel_history_tbl(i).new_subject_id IS NULL)
7629                OR (x_rel_history_tbl(i).new_subject_id = FND_API.G_MISS_NUM)
7630                 )
7631              THEN
7632 
7633                 l_relationship_query_rec.relationship_id := x_rel_history_tbl(i).relationship_id;
7634                 l_time_stamp                             := x_rel_history_tbl(i).creation_date;
7635 
7636                  get_relationships
7637                    (
7638                       p_api_version               => 1.0,
7639                       p_commit                    => fnd_api.g_false,
7640                       p_init_msg_list             => fnd_api.g_false,
7641                       p_validation_level          => fnd_api.g_valid_level_full,
7642                       p_relationship_query_rec    => l_relationship_query_rec,
7643                       p_depth                     => NULL,
7644                       p_time_stamp                => l_time_stamp,
7645                       p_active_relationship_only  => fnd_api.g_false,
7646                       x_relationship_tbl          => l_ii_relationship_tbl,
7647                       x_return_status             => x_return_status,
7648                       x_msg_count                 => x_msg_count,
7649                       x_msg_data                  => x_msg_data
7650                    );
7651 
7652                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7653                    RAISE fnd_api.g_exc_error;
7654                 END IF;
7655                 --
7656                 IF l_ii_relationship_tbl.count > 0 THEN
7657                    x_rel_history_tbl(i).new_subject_id        := l_ii_relationship_tbl(1).subject_id;
7658                    x_rel_history_tbl(i).old_subject_id        := l_ii_relationship_tbl(1).subject_id;
7659                    x_rel_history_tbl(i).object_id             := l_ii_relationship_tbl(1).object_id;
7660                    x_rel_history_tbl(i).old_active_start_date := l_ii_relationship_tbl(1).active_start_date;
7661                    x_rel_history_tbl(i).new_active_start_date := l_ii_relationship_tbl(1).active_start_date;
7662                    --
7663                 END IF;
7664              END IF;
7665              i := i + 1;
7666        END LOOP;
7667         -- srramakr moved outside the loop
7668         -- Resolve the id columns
7669         csi_ii_relationships_pvt.Resolve_id_columns(x_rel_history_tbl);
7670 
7671        -- End of API body
7672 
7673        -- Standard check of p_commit.
7674        /*
7675        IF FND_API.To_Boolean( p_commit ) THEN
7676              COMMIT WORK;
7677        END IF;
7678        */
7679 
7680        /***** srramakr commented for bug # 3304439
7681        -- Check for the profile option and disable the trace
7682        IF (l_flag = 'Y') THEN
7683             dbms_session.set_sql_trace(false);
7684        END IF;
7685        -- End disable trace
7686        ****/
7687 
7688        -- Standard call to get message count and if count is  get message info.
7689        FND_MSG_PUB.Count_And_Get
7690                 (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7691 		p_count        =>      x_msg_count ,
7692                  p_data         =>      x_msg_data   );
7693 
7694 EXCEPTION
7695 
7696         WHEN FND_API.G_EXC_ERROR THEN
7697              --   ROLLBACK TO get_inst_relationship_hist;
7698                 x_return_status := FND_API.G_RET_STS_ERROR ;
7699                 FND_MSG_PUB.Count_And_Get
7700                 (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7701 		p_count   =>      x_msg_count,
7702                     p_data    =>      x_msg_data );
7703         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7704             --    ROLLBACK TO get_inst_relationship_hist;
7705                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7706                 FND_MSG_PUB.Count_And_Get
7707                 (   p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7708 		p_count   =>      x_msg_count,
7709                     p_data    =>      x_msg_data  );
7710         WHEN OTHERS THEN
7711              --   ROLLBACK TO get_inst_relationship_hist;
7712                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7713                 IF      FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
7714                 FND_MSG_PUB.Add_Exc_Msg
7715                 (       g_pkg_name          ,
7716                         l_api_name           );
7717                 END IF;
7718                 FND_MSG_PUB.Count_And_Get
7719                 ( p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7720 		p_count     =>      x_msg_count,
7721                   p_data      =>      x_msg_data  );
7722 
7723 END get_inst_relationship_hist;
7724 --
7725 /*-------------------------------------------------------------------*/
7726 /* Procedure name:  Get_Cyclic_node                                  */
7727 /* Description :    Given a vertex (instance_id), it traverses thru' */
7728 /*                  the structure and find whether a cycle exists    */
7729 /*                  or not. If p_stop_at_cyclic is set to false,     */
7730 /*                  it gives the complete structure in p_rel_tbl.    */
7731 /*                  This will be Depth-First-Search order.           */
7732 /*                  p_cyclic_node is a vertex that participates in   */
7733 /*                  the cycle.                                       */
7734 /* Author      :    Srinivasan Ramakrishnan                          */
7735 /*-------------------------------------------------------------------*/
7736   PROCEDURE Get_Cyclic_Node
7737        ( p_instance_id      IN         NUMBER,
7738 	 p_cyclic_node      OUT NOCOPY NUMBER,
7739 	 p_rel_tbl          OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl,
7740          p_stop_at_cyclic   IN         VARCHAR2,
7741 	 x_return_status    OUT NOCOPY VARCHAR2,
7742 	 x_msg_count        OUT NOCOPY NUMBER,
7743 	 x_msg_data         OUT NOCOPY VARCHAR2 ) IS
7744      l_max_count    NUMBER;
7745      l_ctr          NUMBER;
7746      l_temp_id      NUMBER;
7747      l_found        NUMBER;
7748      l_node_found   NUMBER;
7749      l_check_id     NUMBER;
7750      l_exists       NUMBER;
7751      l_cycle_exists VARCHAR2(1);
7752      l_instance_id  NUMBER;
7753      l_msg_index    NUMBER;
7754      l_msg_count    NUMBER;
7755      l_debug_level  NUMBER;
7756      l_adj_node     NUMBER;
7757      l_rel_tbl_final csi_datastructures_pub.ii_relationship_tbl;
7758      l_api_name     CONSTANT VARCHAR2(50) := 'get_cyclic_node';
7759      --
7760      l_rel_color_tbl     csi_ii_relationships_pvt.REL_COLOR_TBL;
7761      l_rel_color_ctr     NUMBER := 0;
7762      --
7763      COMP_ERROR     EXCEPTION;
7764   BEGIN
7765      x_return_status := fnd_api.g_ret_sts_success;
7766      p_cyclic_node := NULL;
7767      l_instance_id := p_instance_id;
7768      --
7769      l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
7770      IF (l_debug_level > 0) THEN
7771 	CSI_gen_utility_pvt.put_line( 'Get_Cyclic_Node');
7772      END IF;
7773      --
7774      csi_ii_relationships_pvt.get_cyclic_relationships(
7775 	      p_api_version                => 1.0,
7776 	      p_commit                     => fnd_api.g_false,
7777 	      p_init_msg_list              => fnd_api.g_true,
7778 	      p_validation_level           => fnd_api.g_valid_level_full,
7779 	      p_instance_id                => l_instance_id,
7780 	      p_depth                      => NULL ,
7781 	      p_time_stamp                 => fnd_api.g_miss_date,
7782 	      p_active_relationship_only   => fnd_api.g_true,
7783 	      x_relationship_tbl           => p_rel_tbl,
7784 	      x_return_status              => x_return_status,
7785 	      x_msg_count                  => x_msg_count,
7786 	      x_msg_data                   => x_msg_data
7787 	   );
7788      --
7789      IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7790 	l_msg_index := 1;
7791 	l_msg_count := x_msg_count;
7792 	WHILE l_msg_count > 0 LOOP
7793 	   x_msg_data := FND_MSG_PUB.GET
7794 			    (  l_msg_index,
7795 			       FND_API.G_FALSE );
7796 	   csi_gen_utility_pvt.put_line( ' Error from Get_cyclic_relationships.. ');
7797 	   csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
7798 	   l_msg_index := l_msg_index + 1;
7799 	   l_msg_count := l_msg_count - 1;
7800 	END LOOP;
7801 	RAISE FND_API.G_EXC_ERROR;
7802      END IF;
7803      --
7804      csi_gen_utility_pvt.put_line('p_rel_tbl count after get_cyclic_relationships is '
7805 					  ||to_char(p_rel_tbl.count));
7806      l_rel_tbl_final.DELETE;
7807      l_rel_tbl_final := p_rel_tbl;
7808      p_rel_tbl.DELETE;
7809      l_ctr := 0;
7810      l_rel_color_ctr := 0;
7811      --
7812      -- The output of l_rel_tbl_final will be Breadth first search Order.
7813      -- This needs to be converted to depth-first-search(DFS) order since DFS is capable of
7814      -- hitting the cyclic node.
7815      --
7816      -- There is no concept of Parent-Child in a graph structure. All we do in a DFS is, given a
7817      -- vertex, we try to get its adjacent node and walk thru' the graph.
7818      -- Since in CSI_II_RELATIONSHIPS table, they are identified as Object_id and Subject_id
7819      -- we pick up the adjacent node in whichever form it appears (object or subject).
7820      --
7821      -- The following LOOP does this.
7822      --
7823      IF l_rel_tbl_final.count > 0 THEN
7824 	l_max_count := l_rel_tbl_final.count;
7825 	l_temp_id := l_instance_id;
7826 	LOOP
7827 	   IF p_rel_tbl.count = l_max_count OR
7828 	   l_rel_tbl_final.count = 0 THEN
7829 	      exit;
7830 	   END IF;
7831 	   --
7832            csi_gen_utility_pvt.put_line('Processing '||to_char(l_temp_id));
7833            --
7834 	   FOR rel IN l_rel_tbl_final.FIRST .. l_rel_tbl_final.LAST LOOP
7835 	      l_found := 0;
7836               l_adj_node := -9999;
7837               --
7838 	      IF l_rel_tbl_final.EXISTS(rel) THEN
7839                  -- Try to get the Adjacent Node
7840 		 IF l_rel_tbl_final(rel).object_id = l_temp_id THEN
7841                     l_adj_node := l_rel_tbl_final(rel).subject_id;
7842 		    l_found := 1;
7843                  ELSIF l_rel_tbl_final(rel).subject_id = l_temp_id THEN
7844                     l_adj_node := l_rel_tbl_final(rel).object_id;
7845                     l_found := 1;
7846                  END IF;
7847                  --
7848                  IF l_found = 1 THEN
7849 		    l_ctr := l_ctr + 1;
7850 		    p_rel_tbl(l_ctr) := l_rel_tbl_final(rel); -- Push the processed row
7851                     -- Add the current node if not exists in the visited list
7852                     l_exists := 0;
7853                     IF l_rel_color_tbl.count > 0 THEN
7854                        FOR rel_color in l_rel_color_tbl.FIRST .. l_rel_color_tbl.LAST LOOP
7855                           IF l_rel_color_tbl(rel_color).node_id = l_temp_id THEN
7856                              l_exists := 1;
7857                              exit;
7858                           END IF;
7859                        END LOOP;
7860                     END IF;
7861                     --
7862                     IF l_exists = 0 THEN
7863 	               l_rel_color_ctr := l_rel_color_ctr + 1;
7864 	               l_rel_color_tbl(l_rel_color_ctr).node_id := l_temp_id;
7865 	               l_rel_color_tbl(l_rel_color_ctr).color_code := 'R';
7866                     END IF;
7867                     --
7868 		    l_temp_id := l_adj_node; -- set this to the adjacent node and continue
7869 		    l_rel_tbl_final.DELETE(rel); -- Pop the processed row
7870                     --
7871                     -- Check for cycle
7872 		    IF l_rel_color_tbl.count > 0 THEN
7873 		       FOR color_rec in l_rel_color_tbl.FIRST .. l_rel_color_tbl.LAST LOOP
7874 			  IF l_rel_color_tbl(color_rec).node_id = l_temp_id AND -- adjacent node
7875 			     l_rel_color_tbl(color_rec).color_code = 'R' THEN
7876 			     p_cyclic_node := l_temp_id;
7877                              IF nvl(p_stop_at_cyclic,fnd_api.g_true) = fnd_api.g_true THEN
7878                                 csi_gen_utility_pvt.put_line('Cycle exists at '||to_char(p_cyclic_node));
7879 			        Raise COMP_ERROR;
7880                              END IF;
7881 			  END IF;
7882 		       END LOOP;
7883 		    END IF; -- end of check for cycle
7884 		    exit;
7885 		 END IF; -- l_found = 1
7886 	      END IF;
7887 	   END LOOP;
7888 	   --
7889 	   IF l_found = 0 THEN -- If No more components then go back
7890 	      -- If all the nodes under the current node are traversed then mark the node as 'B'.
7891 	      -- The reason for marking this to 'B' is to eliminate this node while getting
7892               -- the previous visited node
7893 	      l_node_found := 0;
7894 	      IF l_rel_color_tbl.count > 0 THEN
7895 		 FOR mark in l_rel_color_tbl.FIRST .. l_rel_color_tbl.LAST LOOP
7896 		    IF l_rel_color_tbl(mark).node_id = l_temp_id THEN
7897 		       l_rel_color_tbl(mark).color_code := 'B';
7898 		       l_node_found := 1;
7899                        csi_gen_utility_pvt.put_line('Marked '||l_temp_id);
7900                        exit;
7901 		    END IF;
7902 		 END LOOP;
7903 	      END IF;
7904 	      --
7905 	      IF l_node_found = 0 THEN
7906 		 l_rel_color_ctr := l_rel_color_ctr + 1;
7907 		 l_rel_color_tbl(l_rel_color_ctr).node_id := l_temp_id;
7908 		 l_rel_color_tbl(l_rel_color_ctr).color_code := 'B'; -- since l_found = 0
7909                  csi_gen_utility_pvt.put_line('Marked '||l_temp_id);
7910 	      END IF;
7911 	      --
7912               -- Get the unmarked node from l_rel_color_tbl in Reverse direction
7913               -- The idea is to go back in the same path once a node is marked as 'B'.
7914               FOR adj IN REVERSE l_rel_color_tbl.FIRST .. l_rel_color_tbl.LAST LOOP
7915                  IF l_rel_color_tbl(adj).color_code = 'R' THEN
7916                     l_temp_id := l_rel_color_tbl(adj).node_id;
7917                     exit;
7918                  END IF;
7919               END LOOP;
7920               --
7921               csi_gen_utility_pvt.put_line('Adjacent Node is '||l_temp_id);
7922 	      IF l_temp_id = -9999 THEN
7923 		 csi_gen_utility_pvt.put_line('Unable to get the Adjacent Node.. Exiting...');
7924 		 Raise FND_API.G_EXC_UNEXPECTED_ERROR;
7925 	      END IF;
7926 	   END IF; -- l_found = 0 check
7927 	END LOOP;
7928      END IF;
7929      csi_gen_utility_pvt.put_line('End of Get_Cyclic_Node...');
7930   EXCEPTION
7931      WHEN COMP_ERROR THEN
7932 	null;
7933      WHEN FND_API.G_EXC_ERROR THEN
7934 	x_return_status := fnd_api.g_ret_sts_error ;
7935 	fnd_msg_pub.count_AND_get
7936 	   (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7937            p_count => x_msg_count ,
7938 	    p_data => x_msg_data
7939 	   );
7940 
7941      WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7942 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
7943 	fnd_msg_pub.count_AND_get
7944 	   (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7945            p_count => x_msg_count ,
7946 	    p_data => x_msg_data
7947 	   );
7948 
7949      WHEN OTHERS THEN
7950 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
7951 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7952 	   fnd_msg_pub.add_exc_msg(g_pkg_name,l_api_name);
7953 	END IF;
7954 	fnd_msg_pub.count_AND_get
7955 	   (p_encoded => FND_API.G_FALSE, --Added for bug 10081206
7956            p_count => x_msg_count ,
7957 	    p_data => x_msg_data
7958 	   );
7959   END Get_Cyclic_Node;
7960 
7961 -- Begin Add Code for Siebel Genesis Project
7962 FUNCTION Get_Root_Parent
7963 (
7964     p_subject_id      IN  NUMBER,
7965     p_rel_type_code   IN  VARCHAR2,
7966     p_object_id       IN  NUMBER
7967 ) RETURN NUMBER
7968 IS
7969     l_object_id       NUMBER;
7970     l_root_object_id  NUMBER;
7971 BEGIN
7972     IF p_rel_type_code IS NULL OR
7973        p_subject_id IS NULL THEN
7974        l_object_id := p_object_id;
7975        RETURN l_object_id;
7976     END IF;
7977 
7978     l_object_id := p_subject_id;
7979 
7980     Begin
7981         select object_id
7982         into   l_object_id
7983         from   CSI_II_RELATIONSHIPS
7984         where subject_id = p_subject_id
7985         and   relationship_type_code = p_rel_type_code
7986         and   ((active_end_date is null) or (active_end_date > sysdate));
7987      Exception
7988         when no_data_found then
7989            l_object_id := p_subject_id;
7990            RETURN l_object_id;
7991      End;
7992      -- Call Recursively for prior parent
7993      Get_Top_Most_Parent
7994           (p_subject_id      => l_object_id,
7995            p_rel_type_code   => p_rel_type_code,
7996            p_object_id       => l_root_object_id
7997           );
7998      return l_root_object_id;
7999 END Get_Root_Parent;
8000 -- End Add Code for Siebel Genesis Project
8001 
8002 END csi_ii_relationships_pvt;