DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMW_ORG_HIERARCHY_PKG

Source


1 PACKAGE BODY AMW_ORG_HIERARCHY_PKG as
2 /*$Header: amwoghrb.pls 120.16.12000000.4 2007/04/19 10:34:39 shelango ship $*/
3 
4 
5 G_PKG_NAME CONSTANT VARCHAR2(30):= 'AMW_ORG_HIERARCHY_PKG';
6 G_FILE_NAME CONSTANT VARCHAR2(12) := 'amwoghrb.pls';
7 G_USER_ID NUMBER := FND_GLOBAL.USER_ID;
8 G_LOGIN_ID NUMBER := FND_GLOBAL.CONC_LOGIN_ID;
9 
10 -- ****************************************************************************
11 -- it's enough if we check just the latest hierarchy that the child being added
12 -- exists as a parent
13 function is_child_an_ancestor(p_org_id in number,
14                               p_parent_process_id in number,
15                               p_child_process_id in number) return boolean
16 is
17 l_dummy number;
18 begin
19 select parent_id
20     into l_dummy
21     from amw_latest_hierarchies
22     where parent_id = p_child_process_id
23     start with child_id = p_parent_process_id  and organization_id = p_org_id
24     connect by prior parent_id = child_id
25     and organization_id = p_org_id;
26 /*
27      select 1
28      into l_dummy
29      from amw_org_hierarchy_denorm
30      where process_id = p_parent_process_id
31      and   parent_child_id = p_child_process_id
32      and   up_down_ind = 'U'
33      and   hierarchy_type = 'L'
34      and  organization_id = p_org_id;
35 */
36      return true;
37 exception
38     when no_data_found then
39         return false;
40     when too_many_rows then
41         return true;
42 end is_child_an_ancestor;
43 
44 -- ****************************************************************************
45 
46 -- check if a process exists in an org.
47 -- If it does not exist, return NOEXIST
48 -- If it exists but is "deleted", return DEL
49 -- If it exists but in no hierarchy, but exists in the org, return NOHIER
50 -- If it exists in latest hierarchy only, return LATEST
51 -- If it exists in approved hierarchy only, return APPROV
52 -- If it exists in both approved 1 hierarchy only, return BOTH
53 function ex_proc_in_which_hier (
54 	p_org_id in number,
55 	p_process_id in number) return varchar2
56 is
57 l_dummy number;
58 l_score number := 0;
59 l_return varchar2(30) := '';
60 
61 begin
62 -- check in latest hierarchy
63     begin
64     select 1
65     into l_dummy
66     from AMW_LATEST_HIERARCHY_ORG_V
67     where child_process_id =  p_process_id
68     and child_organization_id = p_org_id;
69 
70     l_score := 1;
71 
72     exception
73     when too_many_rows then
74          l_score := 1;
75     when no_data_found then
76          l_score := 0;
77     end;
78 
79 -- check in approved hierarchy
80     begin
81     select 1
82     into l_dummy
83     from AMW_CURR_APP_HIERARCHY_org_v
84     where child_process_id =  p_process_id
85     and child_organization_id = p_org_id;
86 
87     l_score := l_score + 2;
88 
89     exception
90     when too_many_rows then
91         l_score := l_score + 2;
92     when no_data_found then
93         l_score := l_score + 0;
94     end;
95 
96     if l_score = 3 then
97         return 'BOTH';
98     elsif l_score = 1 then
99         return 'LATEST';
100     elsif l_score = 2 then
101         return 'APPROV';
102     else
103         begin
104         select 1
105         into l_dummy
106         from AMW_CURR_APPROVED_REV_ORG_v
107         where process_id =  p_process_id
108         and organization_id = p_org_id
109         and deletion_date is not null;
110 
111         l_return := 'DEL';
112         exception
113             when too_many_rows then
114                 l_return := 'DEL';
115             when no_data_found then
116                 l_return := '';
117         end;
118         if l_return is null then
119                 begin
120                 select 1
121                 into l_dummy
122                 from amw_process_organization
123                 where process_id =  p_process_id
124                 and organization_id = p_org_id;
125 
126                 l_return := 'NOHIER';
127                 exception
128                     when too_many_rows then
129                         l_return := 'NOHIER';
130                     when no_data_found then
131                         l_return := 'NOEXIST';
132                 end;
133         end if;
134         return l_return;
135     end if;
136 
137 end ex_proc_in_which_hier;
138 
139 -- ****************************************************************************
140 -- if process is approved, revise it and create a draft.
141 -- if process is draft, move on
142 procedure revise_process_if_necessary (
143 	p_org_id in number,
144 	p_process_id in number) is
145 
146 l_rev_num  number;
147 
148 begin
149 
150     if p_process_id <> -2 then -- do not revise the root
151 
152   -- check if the latest revision is approved.
153     select REVISION_NUMBER
154     into l_rev_num
155     from amw_process_organization
156     where process_id = p_process_id
157     and organization_id = p_org_id
158     and end_date is null
159     and approval_status = 'A';
160 
161 
162   -- if you've come here => the latest revision is approved. We need to revise.
163 
164         -- insert a new revision row in amw_process_organization
165 		insert into amw_process_organization
166 			(CONTROL_COUNT,
167 			 RISK_COUNT,
168 			 PROCESS_ORGANIZATION_ID,
169 			 PROCESS_ID,
170 			 STANDARD_PROCESS_FLAG,
171 			 RISK_CATEGORY,
172 			 CERTIFICATION_STATUS,
173 			 LAST_AUDIT_STATUS,
174 			 ORGANIZATION_ID,
175 			 LAST_CERTIFICATION_DATE,
176 			 LAST_AUDIT_DATE,
177 			 NEXT_AUDIT_DATE,
178 			 APPLICATION_OWNER_ID,
179 			 PROCESS_OWNER_ID,
180 			 PROCESS_CATEGORY_CODE,
181 			 SIGNIFICANT_PROCESS_FLAG,
182 			 CREATED_FROM,
183 			 REQUEST_ID,
184 			 PROGRAM_APPLICATION_ID,
185 			  PROGRAM_ID,
186 			  PROGRAM_UPDATE_DATE,
187 			  ATTRIBUTE_CATEGORY,
188 			  ATTRIBUTE1,
189 			  ATTRIBUTE2,
190 			  ATTRIBUTE3,
191 			  ATTRIBUTE4,
192 			  ATTRIBUTE5,
193 			  ATTRIBUTE6,
194 			  ATTRIBUTE7,
195 			  ATTRIBUTE8,
196 			  ATTRIBUTE9,
197 			  ATTRIBUTE10,
198 			  ATTRIBUTE11,
199 			  ATTRIBUTE12,
200 			  ATTRIBUTE13,
201 			  ATTRIBUTE14,
202 			  ATTRIBUTE15,
203 			  SECURITY_GROUP_ID,
204 			  FINANCE_OWNER_ID,
205 			  PROCESS_CODE,
206 			  PROCESS_TYPE,
207 			  CONTROL_ACTIVITY_TYPE,
208 			  RL_PROCESS_REV_ID,
209 			  STANDARD_VARIATION,
210 			  PROCESS_CATEGORY,
211 			  LAST_UPDATE_DATE,
212 			  LAST_UPDATED_BY,
213 			  CREATION_DATE,
214 			  CREATED_BY,
215 			  LAST_UPDATE_LOGIN,
216 			  REVISION_NUMBER,
217 			  OBJECT_VERSION_NUMBER,
218 			  APPROVAL_STATUS,
219 			  END_DATE,
220 			  PROCESS_ORG_REV_ID,
221 			  START_DATE,
222 			  APPROVAL_DATE,
223 			  APPROVAL_END_DATE,
224 			  DELETION_DATE )
225 
226 			  (select
227 			  CONTROL_COUNT,
228 			  RISK_COUNT,
229 			  AMW_PROCESS_ORGANIZATION_S.nextval,  --kosriniv this is primary key till AMW.C PROCESS_ORGANIZATION_ID,
230 			  PROCESS_ID,
231 			  STANDARD_PROCESS_FLAG,
232 			  RISK_CATEGORY,
233 			  CERTIFICATION_STATUS,
234 			  LAST_AUDIT_STATUS,
235 			  ORGANIZATION_ID,
236 			  LAST_CERTIFICATION_DATE,
237 			  LAST_AUDIT_DATE,
238 			  NEXT_AUDIT_DATE,
239 			  APPLICATION_OWNER_ID,
240 			  PROCESS_OWNER_ID,
241 			  PROCESS_CATEGORY_CODE,
242 			  SIGNIFICANT_PROCESS_FLAG,
243 			  CREATED_FROM,
244 			  REQUEST_ID,
245 			  PROGRAM_APPLICATION_ID,
246 			  PROGRAM_ID,
247 			  PROGRAM_UPDATE_DATE,
248 			  ATTRIBUTE_CATEGORY,
249 			  ATTRIBUTE1,
250 			  ATTRIBUTE2,
251 			  ATTRIBUTE3,
252 			  ATTRIBUTE4,
253 			  ATTRIBUTE5,
254 			  ATTRIBUTE6,
255 			  ATTRIBUTE7,
256 			  ATTRIBUTE8,
257 			  ATTRIBUTE9,
258 			  ATTRIBUTE10,
259 			  ATTRIBUTE11,
260 			  ATTRIBUTE12,
261 			  ATTRIBUTE13,
262 			  ATTRIBUTE14,
263 			  ATTRIBUTE15,
264 			  SECURITY_GROUP_ID,
265 			  FINANCE_OWNER_ID,
266 			  PROCESS_CODE,
267 			  PROCESS_TYPE,
268 			  CONTROL_ACTIVITY_TYPE,
269 			  RL_PROCESS_REV_ID,
270 			  STANDARD_VARIATION,
271 			  PROCESS_CATEGORY,
272 			  sysdate,
273 			  G_USER_ID,
274 			  sysdate,
275 			  G_USER_ID,
276 			  G_LOGIN_ID,
277 			  REVISION_NUMBER + 1,
278 			  1,
279 			  'D',
280 			  null,
281 			  AMW_PROCESS_ORG_REV_S.nextval,
282 			  sysdate,
283 			  null,
284 			  null,
285 			  DELETION_DATE
286 			  from amw_process_organization
287 			  where process_id = p_process_id
288 			  and organization_id = p_org_id
289 			  and end_date is null
290 			  and approval_status = 'A');
291 
292         -- update the old row
293          update amw_process_organization
294          set    end_date = sysdate,
295                 object_version_number = object_version_number + 1
296          where  process_id = p_process_id
297          and    revision_number = l_rev_num
298          and    organization_id = p_org_id;
299 
300     end if;
301 
302     exception
303         when no_data_found then
304           -- if you've come here => the latest revision is draft. Just move on.
305           -- assumption: it's not pending approval. that check has been made.
306             null;
307 end revise_process_if_necessary;
308 
309 --******************************************************************************
310 -- This revises the process in all the organizations in one go
311 --******************************************************************************
312 procedure revise_process_if_necessary (p_process_id in number) is
313     l_rev_num  number;
314     l_Org_Ids t_Org_Ids;
315     l_count number := 1;
316     l_rev_number t_number;
317     l_msg_data              varchar2(4000);
318     l_msg_count	            number;
319 
320 begin
321     if p_process_id = -2 then
322         return;
323     END IF ;
324 
325     l_Org_Ids :=t_Org_Ids();
326     l_rev_number :=t_number();
327 
328     FOR indx IN Org_Ids.FIRST .. Org_Ids.LAST
329     LOOP
330         Begin
331             -- check if the latest revision is approved.
332             select REVISION_NUMBER
333             into l_rev_num
334             from amw_process_organization
335             where process_id = p_process_id
336             and organization_id = Org_Ids(indx)
337             and end_date is null
338             and approval_status = 'A';
339             l_Org_Ids.EXTEND(1);
340             l_rev_number.EXTEND(1);
341             l_Org_Ids(l_count) := Org_Ids(indx);
342             l_rev_number(l_count) := l_rev_num ;
343             l_count := l_count + 1;
344         exception
345             when no_data_found then
346                 -- if you've come here => the latest revision is draft. Just move on.
347                 -- assumption: it's not pending approval. that check has been made.
348             raise;
349         end;
350     END LOOP;
351 
352     -- if you've come here => the latest revision is approved. We need to revise.
353 
354     -- insert a new revision row in amw_process_organization
355     FORALL indx IN l_Org_Ids.FIRST .. l_Org_Ids.LAST
356         insert into amw_process_organization
357 			(CONTROL_COUNT,
358 			 RISK_COUNT,
359 			 PROCESS_ORGANIZATION_ID,
360 			 PROCESS_ID,
361 			 STANDARD_PROCESS_FLAG,
362 			 RISK_CATEGORY,
363 			 CERTIFICATION_STATUS,
364 			 LAST_AUDIT_STATUS,
365 			 ORGANIZATION_ID,
366 			 LAST_CERTIFICATION_DATE,
367 			 LAST_AUDIT_DATE,
368 			 NEXT_AUDIT_DATE,
369 			 APPLICATION_OWNER_ID,
370 			 PROCESS_OWNER_ID,
371 			 PROCESS_CATEGORY_CODE,
372 			 SIGNIFICANT_PROCESS_FLAG,
373 			 CREATED_FROM,
374 			 REQUEST_ID,
375 			 PROGRAM_APPLICATION_ID,
376 			  PROGRAM_ID,
377 			  PROGRAM_UPDATE_DATE,
378 			  ATTRIBUTE_CATEGORY,
379 			  ATTRIBUTE1,
380 			  ATTRIBUTE2,
381 			  ATTRIBUTE3,
382 			  ATTRIBUTE4,
383 			  ATTRIBUTE5,
384 			  ATTRIBUTE6,
385 			  ATTRIBUTE7,
386 			  ATTRIBUTE8,
387 			  ATTRIBUTE9,
388 			  ATTRIBUTE10,
389 			  ATTRIBUTE11,
390 			  ATTRIBUTE12,
391 			  ATTRIBUTE13,
392 			  ATTRIBUTE14,
393 			  ATTRIBUTE15,
394 			  SECURITY_GROUP_ID,
395 			  FINANCE_OWNER_ID,
396 			  PROCESS_CODE,
397 			  PROCESS_TYPE,
398 			  CONTROL_ACTIVITY_TYPE,
399 			  RL_PROCESS_REV_ID,
400 			  STANDARD_VARIATION,
401 			  PROCESS_CATEGORY,
402 			  LAST_UPDATE_DATE,
403 			  LAST_UPDATED_BY,
404 			  CREATION_DATE,
405 			  CREATED_BY,
406 			  LAST_UPDATE_LOGIN,
407 			  REVISION_NUMBER,
408 			  OBJECT_VERSION_NUMBER,
409 			  APPROVAL_STATUS,
410 			  END_DATE,
411 			  PROCESS_ORG_REV_ID,
412 			  START_DATE,
413 			  APPROVAL_DATE,
414 			  APPROVAL_END_DATE,
415 			  DELETION_DATE )
416 
417 			  (select
418 			  CONTROL_COUNT,
419 			  RISK_COUNT,
420 			  AMW_PROCESS_ORGANIZATION_S.nextval,  --kosriniv this is primary key till AMW.C PROCESS_ORGANIZATION_ID,
421 			  PROCESS_ID,
422 			  STANDARD_PROCESS_FLAG,
423 			  RISK_CATEGORY,
424 			  CERTIFICATION_STATUS,
425 			  LAST_AUDIT_STATUS,
426 			  ORGANIZATION_ID,
427 			  LAST_CERTIFICATION_DATE,
428 			  LAST_AUDIT_DATE,
429 			  NEXT_AUDIT_DATE,
430 			  APPLICATION_OWNER_ID,
431 			  PROCESS_OWNER_ID,
432 			  PROCESS_CATEGORY_CODE,
433 			  SIGNIFICANT_PROCESS_FLAG,
434 			  CREATED_FROM,
435 			  REQUEST_ID,
436 			  PROGRAM_APPLICATION_ID,
437 			  PROGRAM_ID,
438 			  PROGRAM_UPDATE_DATE,
439 			  ATTRIBUTE_CATEGORY,
440 			  ATTRIBUTE1,
441 			  ATTRIBUTE2,
442 			  ATTRIBUTE3,
443 			  ATTRIBUTE4,
444 			  ATTRIBUTE5,
445 			  ATTRIBUTE6,
446 			  ATTRIBUTE7,
447 			  ATTRIBUTE8,
448 			  ATTRIBUTE9,
449 			  ATTRIBUTE10,
450 			  ATTRIBUTE11,
451 			  ATTRIBUTE12,
452 			  ATTRIBUTE13,
453 			  ATTRIBUTE14,
454 			  ATTRIBUTE15,
455 			  SECURITY_GROUP_ID,
456 			  FINANCE_OWNER_ID,
457 			  PROCESS_CODE,
458 			  PROCESS_TYPE,
459 			  CONTROL_ACTIVITY_TYPE,
460 			  RL_PROCESS_REV_ID,
461 			  STANDARD_VARIATION,
462 			  PROCESS_CATEGORY,
463 			  sysdate,
464 			  G_USER_ID,
465 			  sysdate,
466 			  G_USER_ID,
467 			  G_LOGIN_ID,
468 			  REVISION_NUMBER + 1,
469 			  1,
470 			  'D',
471 			  null,
472 			  AMW_PROCESS_ORG_REV_S.nextval,
473 			  sysdate,
474 			  null,
475 			  null,
476 			  DELETION_DATE
477 			  from amw_process_organization
478 			  where process_id    = p_process_id
479 			  and organization_id = l_Org_Ids(indx)
480 			  and end_date is null
481 			  and approval_status = 'A');
482 
483         -- update the old row
484     FORALL indx IN l_Org_Ids.FIRST .. l_Org_Ids.LAST
485          update amw_process_organization
486          set    end_date = sysdate,
487                 object_version_number = object_version_number + 1
488          where  process_id = p_process_id
489          and    revision_number = l_rev_number(indx)
490          and    organization_id = l_Org_Ids(indx);
491 Exception
492     WHEN OTHERS THEN
493         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
494         fnd_file.put_line(fnd_file.LOG, ' Error in Revision '||sqlerrm);
495         fnd_file.put_line(fnd_file.LOG, l_msg_data);
496         raise;
497 
498 end revise_process_if_necessary;
499 
500 
501 -- ****************************************************************************
502 
503 -- The parent process and the child process both exist as ICM processes
504 -- Make a new link or delete an exisitng link. Revise parent if necessary
505 procedure add_delete_ex_child (
506 	p_org_id in number,
507 	p_parent_process_id in number,
508 	p_child_process_id in number,
509     p_action in varchar2)
510 is
511   l_dummy number;
512   l_child_order_num amw_latest_hierarchies.child_order_number%type;
513 l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
514 l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
515 
516 begin
517 	if( l_log_stmt_level >= l_curr_log_level ) then
518     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.Begin',
519         'p_org_id:'||p_org_id ||';p_parent_process_id:'||p_parent_process_id ||';p_child_process_id:'||p_child_process_id ||';p_action:'||'ADD');
520         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.produce_err_if_circular',
521         p_org_id ||';'||p_parent_process_id ||';'||p_child_process_id);
522 	end if;
523 
524 -- check for potential circular hierarchy formation
525   produce_err_if_circular(
526 	p_org_id => p_org_id,
527 	p_parent_process_id => p_parent_process_id,
528     p_child_process_id => p_child_process_id);
529 
530 
531   -- find out if the latest revision for parent_id is approved or not.
532   -- if approved, revise it. if draft, don't do anything
533 	  if( l_log_stmt_level >= l_curr_log_level ) then
534     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.revise_process_if_necessary',
535         p_org_id ||';'||p_parent_process_id );
536 	  end if;
537       revise_process_if_necessary(p_org_id, p_parent_process_id);
538 
539         --insert the latest hierarchy table
540         if p_action = 'ADD' then
541                insert into amw_latest_hierarchies
542                 (ORGANIZATION_ID, PARENT_ID, CHILD_ID, CHILD_ORDER_NUMBER, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATION_DATE, CREATED_BY, object_version_number)
543                VALUES
544                 (p_org_id,p_parent_process_id,p_child_process_id,AMW_ORG_CHILD_ORDER_S.nextval, sysdate, G_USER_ID, G_LOGIN_ID, sysdate, G_USER_ID, 1)
545                 returning                CHILD_ORDER_NUMBER
546                  into                     l_child_order_num;
547                 AMW_RL_HIERARCHY_PKG.update_appr_ch_ord_num_if_reqd
548                 (p_org_id      =>  p_org_id,
549                  p_parent_id   =>  p_parent_process_id,
550                  p_child_id    =>  p_child_process_id,
551                  p_instance_id =>  l_child_order_num);
552         elsif p_action = 'DEL' then
553                delete from amw_latest_hierarchies
554                where parent_id = p_parent_process_id
555                and   child_id  = p_child_process_id
556                and organization_id = p_org_id;
557         end if;
558 
559         -- if the parent is the root, then it doesn't get revised. This means if you
560         -- add to the root, or delete from the root, the information must be transferred
561         -- to the amw_approved_hierarchies table.
562         if p_parent_process_id = -2 then
563             if p_action = 'ADD' then
564             	if( l_log_stmt_level >= l_curr_log_level ) then
565     				FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.write_approved_hierarchy',
566         			'-2;0;'||p_org_id ||':'||sysdate);
567 	  			end if;
568                 AMW_PROC_ORG_APPROVAL_PKG.write_approved_hierarchy(-2, 0, p_org_id,sysdate);
569             elsif p_action = 'DEL' then
570             	if( l_log_stmt_level >= l_curr_log_level ) then
571     				FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.write_approved_hierarchy',
572         			'-2;2;'||p_org_id ||':'||sysdate);
573 	  			end if;
574                 AMW_PROC_ORG_APPROVAL_PKG.write_approved_hierarchy(-2, 2, p_org_id,sysdate);
575             end if;
576             if( l_log_stmt_level >= l_curr_log_level ) then
577     				FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.write_approved_hierarchy',
578         			'-2;3;'||p_org_id ||':'||sysdate);
579 	  		end if;
580             AMW_PROC_ORG_APPROVAL_PKG.write_approved_hierarchy(-2, 3, p_org_id,sysdate);
581 
582             if( l_log_stmt_level >= l_curr_log_level ) then
583     				FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.write_approved_hierarchy',
584         			'WRITE_APPROVED_HIERARCHY_END');
585 	  		end if;
586         end if;
587 
588 	if( l_log_stmt_level >= l_curr_log_level ) then
589     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ADD_DELETE_EX_CHILD.End',
590         'End');
591 	end if;
592 
593 end add_delete_ex_child;
594 
595 
596 -- ****************************************************************************
597 /*
598 -- The parent process and the child process both exist as ICM processes
599 -- Make a new link or delete an exisitng link. Revise parent if necessary
600 procedure add_delete_ex_child (
601 	p_org_id in number,
602 	p_parent_process_id in number,
603 	p_child_process_id in number,
604     p_action in varchar2,
605     x_return_status out nocopy varchar2,
606     x_msg_count out nocopy number,
607     x_msg_data out nocopy varchar2)
608 is
609   l_api_name CONSTANT varchar2(30) := 'add_delete_ex_child';
610   p_init_msg_list varchar2(10) := FND_API.G_FALSE;
611   l_dummy number;
612 
613 begin
614 
615   x_return_status := FND_API.G_RET_STS_SUCCESS;
616   if FND_API.to_Boolean(p_init_msg_list) then
617      FND_MSG_PUB.initialize;
618   end if;
619 
620   if FND_GLOBAL.user_id is null then
621      AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
622      raise FND_API.G_EXC_ERROR;
623   end if;
624 
625   -- find out if the latest revision for parent_id is approved or not.
626   -- if approved, revise it. if draft, don't do anything
627 
628       revise_process_if_necessary(p_org_id, p_parent_process_id);
629 
630         --insert the latest hierarchy table
631         if p_action = 'ADD' then
632                insert into amw_latest_hierarchies
633                 (ORGANIZATION_ID, PARENT_ID, CHILD_ID, CHILD_ORDER_NUMBER, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATION_DATE, CREATED_BY, object_version_number)
634                VALUES
635                 (p_org_id,p_parent_process_id,p_child_process_id,AMW_ORG_CHILD_ORDER_S.nextval, sysdate, G_USER_ID, G_LOGIN_ID, sysdate, G_USER_ID, 1);
636         elsif p_action = 'DEL' then
637                delete from amw_latest_hierarchies
638                where parent_id = p_parent_process_id
639                and   child_id  = p_child_process_id
640                and organization_id = p_org_id;
641         end if;
642 
643 
644 exception
645   when FND_API.G_EXC_ERROR then
646      ROLLBACK;
647      x_return_status := FND_API.G_RET_STS_ERROR;
648      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
649                                p_count => x_msg_count,
650                                p_data => x_msg_data);
651 
652 
653   when FND_API.G_EXC_UNEXPECTED_ERROR then
654      ROLLBACK;
655      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
656      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
657                                p_count => x_msg_count,
658                                p_data => x_msg_data);
659 
660   when OTHERS then
661       ROLLBACK;
662       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
663 
664       FND_MSG_PUB.Add_Exc_Msg(p_pkg_name => G_PKG_NAME,
665                               p_procedure_name => l_api_name,
666                               p_error_text => SUBSTRB(SQLERRM,1,240));
667 
668       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
669                                 p_count => x_msg_count,
670                                 p_data => x_msg_data);
671 
672 end add_delete_ex_child;
673 */
674 -- ****************************************************************************
675 
676 -- you can delete a process, i.e. set the deletion_date only when all occurrances of the
677 -- process is removed from the latest hierarchy.
678 procedure delete_process (
679 	p_org_id in number,
680 	p_process_id in number)
681 is
682 appexst varchar2(1);
683 l_return_status varchar2(10);
684 l_msg_count number;
685 l_msg_data varchar2(4000);
686 begin
687 
688 	  appexst := does_apprvd_ver_exst(p_org_id,p_process_id);
689 	  if appexst = 'Y' then
690 
691       	revise_process_if_necessary(p_org_id, p_process_id);
692 
693 	      update amw_process_organization
694     	  set deletion_date = sysdate
695 	      where process_id = p_process_id
696     	  and organization_id = p_org_id
697       	  and end_date is null;
698       else
699 
700       	delete_draft(   p_organization_id  => p_org_id,
701                         p_process_id       => p_process_id,
702                         x_return_status    => l_return_status,
703                         x_msg_count        => l_msg_count,
704                         x_msg_data         => l_msg_data);
705 
706       end if;
707 
708 end delete_process;
709 
710 
711 -- ****************************************************************************
712 
713 -- import a process from rl into an org as child of an existing org-process
714 -- if parent is draft, just add child,
715 -- else revise parent. This involves creation
716 -- of a new org-process
717 procedure import_rlproc_as_child_of_ex (
718 	p_org_id in number,
719 	p_parent_process_id in number, -- exisitng org process
720 	p_child_process_id in number,  -- amw_process id
721     apply_rcm in varchar2) is
722 
723 l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
724 l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
725 begin
726 	if( l_log_stmt_level >= l_curr_log_level ) then
727     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
728         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.begin',
729         	'OrgId:' ||p_org_id || ';ProcessId:'||p_child_process_id
730         	||';ParentProcessId:'||p_parent_process_id||';ApplyRCM:'||apply_rcm);
731         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
732         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.produce_err_if_circular','produce_err_if_circular');
733 	end if;
734 -- check for potential circular hierarchy formation
735   produce_err_if_circular(
736 	p_org_id => p_org_id,
737 	p_parent_process_id => p_parent_process_id,
738     p_child_process_id => p_child_process_id);
739 
740   -- find out if the latest revision for parent_id is approved or not.
741   -- if approved, revise it. if draft, don't do anything
742 
743   	 if( l_log_stmt_level >= l_curr_log_level ) then
744     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
745         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.revise_process_if_necessary','Revise parent:'|| p_parent_process_id );
746 	 end if;
747       revise_process_if_necessary(p_org_id, p_parent_process_id);
748 
749      if( l_log_stmt_level >= l_curr_log_level ) then
750     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
751         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.insert Row','Insert Row' );
752 	 end if;
753 
754   --insert into amw_process_organization table
755 		insert into amw_process_organization
756 			( PROCESS_ORG_REV_ID,
757    			  PROCESS_ORGANIZATION_ID,
758 			  REVISION_NUMBER,
759 			  PROCESS_ID,
760        		  ORGANIZATION_ID,
761 			  PROCESS_CODE,
762 			  RL_PROCESS_REV_ID,
763 			  SIGNIFICANT_PROCESS_FLAG,
764 			  STANDARD_PROCESS_FLAG,
765 			  PROCESS_CATEGORY_CODE,
766 			  STANDARD_VARIATION,
767 			  ATTRIBUTE_CATEGORY,
768 			  ATTRIBUTE1,
769 			  ATTRIBUTE2,
770 			  ATTRIBUTE3,
771 			  ATTRIBUTE4,
772 			  ATTRIBUTE5,
773 			  ATTRIBUTE6,
774 			  ATTRIBUTE7,
775 			  ATTRIBUTE8,
776 			  ATTRIBUTE9,
777 			  ATTRIBUTE10,
778 			  ATTRIBUTE11,
779 			  ATTRIBUTE12,
780 			  ATTRIBUTE13,
781 			  ATTRIBUTE14,
782 			  ATTRIBUTE15,
783 			  SECURITY_GROUP_ID,
784 			  PROCESS_TYPE,
785 			  CONTROL_ACTIVITY_TYPE,
786 			  LAST_UPDATE_DATE,
787 			  LAST_UPDATED_BY,
788 			  CREATION_DATE,
789 			  CREATED_BY,
790 			  LAST_UPDATE_LOGIN,
791 			  OBJECT_VERSION_NUMBER,
792 			  APPROVAL_STATUS,
793   			  START_DATE,
794               risk_category)
795 
796 			  (select
797 			  AMW_PROCESS_ORG_REV_S.nextval,
798 			  AMW_PROCESS_ORGANIZATION_S.nextval,
799 			  1,
800 			  PROCESS_ID,
801 			  p_org_id,
802 			  PROCESS_CODE,
803 			  PROCESS_REV_ID,
804 			  SIGNIFICANT_PROCESS_FLAG,
805 			  STANDARD_PROCESS_FLAG,
806 			  PROCESS_CATEGORY,
807 			  STANDARD_VARIATION,
808 			  ATTRIBUTE_CATEGORY,
809 			  ATTRIBUTE1,
810 			  ATTRIBUTE2,
811 			  ATTRIBUTE3,
812 			  ATTRIBUTE4,
813 			  ATTRIBUTE5,
814 			  ATTRIBUTE6,
815 			  ATTRIBUTE7,
816 			  ATTRIBUTE8,
817 			  ATTRIBUTE9,
818 			  ATTRIBUTE10,
819 			  ATTRIBUTE11,
820 			  ATTRIBUTE12,
821 			  ATTRIBUTE13,
822 			  ATTRIBUTE14,
823 			  ATTRIBUTE15,
824 			  SECURITY_GROUP_ID,
825 			  PROCESS_TYPE,
826 			  CONTROL_ACTIVITY_TYPE,
827 			  sysdate,
828 			  G_USER_ID,
829 			  sysdate,
830 			  G_USER_ID,
831 			  G_LOGIN_ID,
832 			  1,
833 			  'D',
834 			  sysdate,
835               'High'
836 			  from amw_process
837 			  where process_id = p_child_process_id
838 			  and approval_date is not null
839 			  and approval_end_date is null);
840 
841 		if( l_log_stmt_level >= l_curr_log_level ) then
842     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
843         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.insert_latest_hierarchy','insert_latest_hierarchy' );
844 	 	end if;
845   --insert into latest hierarchy table
846       insert into amw_latest_hierarchies
847         (ORGANIZATION_ID, PARENT_ID, CHILD_ID, CHILD_ORDER_NUMBER, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATION_DATE, CREATED_BY, object_version_number)
848       VALUES
849         (p_org_id,p_parent_process_id,p_child_process_id,AMW_ORG_CHILD_ORDER_S.nextval, sysdate, G_USER_ID, G_LOGIN_ID, sysdate, G_USER_ID, 1);
850 
851 
852   -- import process objectives, key accounts, significant elements
853   		if( l_log_stmt_level >= l_curr_log_level ) then
854     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.import_process_attributes',
855     		'p_child_process_id:' || p_child_process_id || ';p_org_id:'||p_org_id );
856 	 	end if;
857       import_process_attributes(p_child_process_id, p_org_id);
858 
859   -- import rcm
860       if apply_rcm = 'Y' then
861       	  if( l_log_stmt_level >= l_curr_log_level ) then
862     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.IMPORT_RLPROC_AS_CHILD_OF_EX.import_rcm_for_new_orgprocess',
863     		'p_child_process_id:' || p_child_process_id || ';p_org_id:'||p_org_id );
864 	 	end if;
865           import_rcm_for_new_orgprocess(p_child_process_id, p_org_id);
866       end if;
867 
868 end import_rlproc_as_child_of_ex;
869 
870 
871 -- ****************************************************************************
872 PROCEDURE sync_attachments
873    ( p_process_id IN NUMBER
874      ,p_org_id IN NUMBER
875      ,p_add_upd_flag VARCHAR2)
876    IS
877 -- Purpose: Briefly explain the functionality of the procedure
878 -- Procedure for synchronizing the attachments
879 -- MODIFICATION HISTORY
880 -- Person      Date    Comments
881 --  dpatel      20/03/2006
882 -- ---------   ------  -------------------------------------------
883 v_proc_rev_id FND_ATTACHED_DOCUMENTS.pk1_value%type;
884 v_proc_org_rev_id FND_ATTACHED_DOCUMENTS.pk1_value%type;
885 BEGIN
886     select process_org_rev_id into v_proc_org_rev_id
887     from amw_process_organization
888     where process_id = p_process_id
889         and organization_id = p_org_id
890         and end_date is null;
891 
892     select distinct pk1_value into v_proc_rev_id
893     From FND_ATTACHED_DOCUMENTS
894     where entity_name ='AMW_PROCESS'
895         and pk1_value in ( select PROCESS_REV_ID
896                             from amw_process_vl
897                             where  process_id = p_process_id
898                             and end_date is null);
899 
900     IF p_add_upd_flag = 'U' THEN
901         FND_ATTACHED_DOCUMENTS2_PKG.delete_attachments(X_entity_name => 'AMW_PROCESS_ORGANIZATION'
902          ,X_pk1_value => v_proc_org_rev_id);
903          --,X_delete_document_flag => 'Y');
904 
905     END IF; -- if update then delete the current attachments before adding new
906 
907     FND_ATTACHED_DOCUMENTS2_PKG.copy_attachments(X_from_entity_name => 'AMW_PROCESS'
908      ,X_from_pk1_value => v_proc_rev_id
909      ,X_to_entity_name => 'AMW_PROCESS_ORGANIZATION'
910      ,X_to_pk1_value => v_proc_org_rev_id
911      ,X_created_by => G_USER_ID
912      ,X_last_update_login => G_LOGIN_ID
913      );
914 
915 EXCEPTION
916 WHEN NO_DATA_FOUND THEN
917     RETURN;
918 END sync_attachments; -- Procedure
919 
920 -- import key accounts
921 procedure import_process_attributes(p_child_process_id in number,
922                                     p_org_id in number) is
923 cursor party_list(pid number) is
924    SELECT      TO_NUMBER(REPLACE(grants.grantee_key,'HZ_PARTY:','')) party_id,
925         	   granted_menu.menu_name role_name,
926         	   obj.obj_name object_name,
927      		   granted_menu.menu_id menu_id,
928 		   grants.end_date end_date
929          FROM fnd_grants grants,
930              fnd_menus granted_menu,
931              fnd_objects obj
932          WHERE obj.obj_name = 'AMW_PROCESS_APPR_ETTY'
933          AND   grants.object_id = obj.object_id
934          AND   grants.grantee_type ='USER'
935          AND   grantee_key like 'HZ_PARTY%'
936          AND   NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
937          AND   grants.menu_id = granted_menu.menu_id
938          AND   grants.instance_type = 'INSTANCE'
939          AND   grants.instance_pk1_value = to_char(pid)
940          AND   grants.instance_pk2_value = '*NULL*'
941          AND   grants.instance_pk3_value = '*NULL*'
942          AND   grants.instance_pk4_value = '*NULL*'
943          AND   grants.instance_pk5_value = '*NULL*'
944          and   granted_menu.menu_name in ('AMW_RL_PROC_OWNER_ROLE', 'AMW_RL_PROC_FINANCE_OWNER_ROLE', 'AMW_RL_PROC_APPL_OWNER_ROLE');
945 
946 l_return_status varchar2(10);
947 l_msg_count number;
948 l_msg_data varchar2(4000);
949 
950 begin
951 
952   for party_list_rec in party_list(p_child_process_id) loop
953 	  exit when party_list%notfound;
954 
955 	  if party_list_rec.role_name = 'AMW_RL_PROC_OWNER_ROLE' then
956 
957               AMW_SECURITY_PUB.grant_role_guid
958               (
959                p_api_version           => 1,
960                p_role_name             => 'AMW_ORG_PROC_OWNER_ROLE',
961                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
962                p_instance_type         => 'INSTANCE',
963                p_instance_set_id       => null,
964                p_instance_pk1_value    => p_org_id,
965                p_instance_pk2_value    => p_child_process_id,
966                p_instance_pk3_value    => null,
967                p_instance_pk4_value    => null,
968                p_instance_pk5_value    => null,
969                p_party_id              => party_list_rec.party_id,
970                p_start_date            => sysdate,
971                p_end_date              => party_list_rec.end_date,
972                x_return_status         => l_return_status,
973                x_errorcode             => l_msg_count,
974                x_grant_guid            => l_msg_data,
975                p_check_for_existing    => FND_API.G_FALSE);
976 
977 	  elsif party_list_rec.role_name = 'AMW_RL_PROC_FINANCE_OWNER_ROLE' then
978 
979               AMW_SECURITY_PUB.grant_role_guid
980               (
981                p_api_version           => 1,
982                p_role_name             => 'AMW_ORG_PROC_FIN_OWNER_ROLE',
983                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
984                p_instance_type         => 'INSTANCE',
985                p_instance_set_id       => null,
986                p_instance_pk1_value    => p_org_id,
987                p_instance_pk2_value    => p_child_process_id,
988                p_instance_pk3_value    => null,
989                p_instance_pk4_value    => null,
990                p_instance_pk5_value    => null,
991                p_party_id              => party_list_rec.party_id,
992                p_start_date            => sysdate,
993                p_end_date              => party_list_rec.end_date,
994                x_return_status         => l_return_status,
995                x_errorcode             => l_msg_count,
996                x_grant_guid            => l_msg_data,
997                p_check_for_existing    => FND_API.G_FALSE);
998 
999 	  elsif party_list_rec.role_name = 'AMW_RL_PROC_APPL_OWNER_ROLE' then
1000 
1001               AMW_SECURITY_PUB.grant_role_guid
1002               (
1003                p_api_version           => 1,
1004                p_role_name             => 'AMW_ORG_PROC_APPL_OWNER_ROLE',
1005                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
1006                p_instance_type         => 'INSTANCE',
1007                p_instance_set_id       => null,
1008                p_instance_pk1_value    => p_org_id,
1009                p_instance_pk2_value    => p_child_process_id,
1010                p_instance_pk3_value    => null,
1011                p_instance_pk4_value    => null,
1012                p_instance_pk5_value    => null,
1013                p_party_id              => party_list_rec.party_id,
1014                p_start_date            => sysdate,
1015                p_end_date              => party_list_rec.end_date,
1016                x_return_status         => l_return_status,
1017                x_errorcode             => l_msg_count,
1018                x_grant_guid            => l_msg_data,
1019                p_check_for_existing    => FND_API.G_FALSE);
1020 
1021 	  end if;
1022 
1023   end loop;
1024 
1025     insert into amw_acct_associations
1026     (ACCT_ASSOC_ID,
1027     NATURAL_ACCOUNT_ID,
1028     PK1,
1029     PK2,
1030     STATEMENT_ID,
1031     STATEMENT_LINE_ID,
1032     ORIG_SYSTEM_ACCT_VALUE,
1033     ASSOCIATION_CREATION_DATE,
1034     APPROVAL_DATE,
1035     DELETION_DATE,
1036     DELETION_APPROVAL_DATE,
1037     OBJECT_TYPE,
1038     LAST_UPDATE_DATE,
1039     LAST_UPDATED_BY,
1040     CREATION_DATE,
1041     CREATED_BY,
1042     LAST_UPDATE_LOGIN,
1043     OBJECT_VERSION_NUMBER)
1044     (select
1045     AMW_ACCT_ASSOCIATIONS_S.nextval,
1046     NATURAL_ACCOUNT_ID,
1047     p_org_id,
1048     PK1,
1049     STATEMENT_ID,
1050     STATEMENT_LINE_ID,
1051     ORIG_SYSTEM_ACCT_VALUE,
1052     sysdate,
1053     null,
1054     null,
1055     null,
1056     'PROCESS_ORG',
1057     sysdate,
1058     G_USER_ID,
1059     sysdate,
1060     G_USER_ID,
1061     G_LOGIN_ID,
1062     1
1063     from amw_acct_associations
1064     where PK1 = p_child_process_id
1065     and object_type = 'PROCESS'
1066     and approval_date is not null
1067     and deletion_approval_date is null);
1068 insert into amw_objective_associations
1069     (OBJECTIVE_ASSOCIATION_ID,
1070     PROCESS_OBJECTIVE_ID,
1071     PK1,
1072     PK2,
1073     ASSOCIATION_CREATION_DATE,
1074     APPROVAL_DATE,
1075     DELETION_DATE,
1076     DELETION_APPROVAL_DATE,
1077     OBJECT_TYPE,
1078     LAST_UPDATE_DATE,
1079     LAST_UPDATED_BY,
1080     CREATION_DATE,
1081     CREATED_BY,
1082     LAST_UPDATE_LOGIN,
1083     OBJECT_VERSION_NUMBER)
1084     (select
1085     AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
1086     PROCESS_OBJECTIVE_ID,
1087     p_org_id,
1088     PK1,
1089     sysdate,
1090     null,
1091     null,
1092     null,
1093     'PROCESS_ORG',
1094     sysdate,
1095     G_USER_ID,
1096     sysdate,
1097     G_USER_ID,
1098     G_LOGIN_ID,
1099     1
1100     from amw_objective_associations
1101     where PK1 = p_child_process_id
1102     and object_type = 'PROCESS'
1103     and approval_date is not null
1104     and deletion_approval_date is null);
1105 
1106 sync_attachments(p_process_id => p_child_process_id
1107     ,p_org_id => p_org_id
1108     ,p_add_upd_flag => 'A');
1109 
1110 end;
1111 
1112 -- ****************************************************************************
1113 
1114 procedure import_rcm_for_new_orgprocess(p_child_process_id in number,
1115                                         p_org_id in number) is
1116 
1117 begin
1118 
1119     insert into amw_risk_associations
1120     (RISK_ASSOCIATION_ID,
1121     RISK_ID,
1122     PK1,
1123     PK2,
1124     RISK_LIKELIHOOD_CODE,
1125     RISK_IMPACT_CODE,
1126     MATERIAL,
1127     MATERIAL_VALUE,
1128     ASSOCIATION_CREATION_DATE,
1129     APPROVAL_DATE,
1130     DELETION_DATE,
1131     DELETION_APPROVAL_DATE,
1132     OBJECT_TYPE,
1133     LAST_UPDATE_DATE,
1134     LAST_UPDATED_BY,
1135     CREATION_DATE,
1136     CREATED_BY,
1137     LAST_UPDATE_LOGIN,
1138     OBJECT_VERSION_NUMBER)
1139     (select
1140     AMW_RISK_ASSOCIATIONS_S.nextval,
1141     RISK_ID,
1142     p_org_id,
1143     PK1,
1144     RISK_LIKELIHOOD_CODE,
1145     RISK_IMPACT_CODE,
1146     MATERIAL,
1147     MATERIAL_VALUE,
1148     sysdate,
1149     null,
1150     null,
1151     null,
1152     'PROCESS_ORG',
1153     sysdate,
1154     G_USER_ID,
1155     sysdate,
1156     G_USER_ID,
1157     G_LOGIN_ID,
1158     1
1159     from amw_risk_associations
1160     where PK1 = p_child_process_id
1161     and object_type = 'PROCESS'
1162     and approval_date is not null
1163     and deletion_approval_date is null);
1164 --    and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
1165 --    and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)));
1166 
1167 
1168     insert into amw_control_associations
1169     (CONTROL_ASSOCIATION_ID,
1170     CONTROL_ID,
1171     PK1,
1172     PK2,
1173     PK3,
1174     ASSOCIATION_CREATION_DATE,
1175     APPROVAL_DATE,
1176     DELETION_DATE,
1177     DELETION_APPROVAL_DATE,
1178     OBJECT_TYPE,
1179     LAST_UPDATE_DATE,
1180     LAST_UPDATED_BY,
1181     CREATION_DATE,
1182     CREATED_BY,
1183     LAST_UPDATE_LOGIN,
1184     OBJECT_VERSION_NUMBER)
1185     (select
1186     AMW_CONTROL_ASSOCIATIONS_S.nextval,
1187     CONTROL_ID,
1188     p_org_id,
1189     PK1,
1190     PK2,
1191     sysdate,
1192     null,
1193     null,
1194     null,
1195     'RISK_ORG',
1196     sysdate,
1197     G_USER_ID,
1198     sysdate,
1199     G_USER_ID,
1200     G_LOGIN_ID,
1201     1
1202     from amw_control_associations
1203     where PK1 = p_child_process_id
1204     and object_type = 'RISK'
1205     and approval_date is not null
1206     and deletion_approval_date is null);
1207 --    and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
1208 --    and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)));
1209 
1210 
1211 -- abedajna, control objective import
1212     insert into amw_objective_associations
1213     (OBJECTIVE_ASSOCIATION_ID,
1214     PROCESS_OBJECTIVE_ID,
1215     PK1,
1216     PK2,
1217     PK3,
1218     PK4,
1219     ASSOCIATION_CREATION_DATE,
1220     APPROVAL_DATE,
1221     DELETION_DATE,
1222     DELETION_APPROVAL_DATE,
1223     OBJECT_TYPE,
1224     LAST_UPDATE_DATE,
1225     LAST_UPDATED_BY,
1226     CREATION_DATE,
1227     CREATED_BY,
1228     LAST_UPDATE_LOGIN,
1229     OBJECT_VERSION_NUMBER)
1230     (select
1231     AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
1232     o.PROCESS_OBJECTIVE_ID,
1233     p_org_id,
1234     o.PK1,
1235     o.PK2,
1236     o.pk3,
1237     sysdate,
1238     null,
1239     null,
1240     null,
1241     'CONTROL_ORG',
1242     sysdate,
1243     G_USER_ID,
1244     sysdate,
1245     G_USER_ID,
1246     G_LOGIN_ID,
1247     1
1248     from amw_objective_associations o, amw_control_associations c
1249     where o.object_type = 'CONTROL'
1250     and o.approval_date is not null
1251     and o.deletion_approval_date is null
1252     and c.object_type = 'RISK_ORG'
1253     and c.approval_date is null
1254     and c.deletion_date is null
1255     and c.pk1 = p_org_id
1256     and c.pk2 = p_child_process_id
1257     and c.pk2 = o.pk1
1258     and c.pk3 = o.pk2
1259     and o.pk3 = c.control_id);
1260 
1261     insert into amw_ap_associations
1262     (AP_ASSOCIATION_ID,
1263     AUDIT_PROCEDURE_ID,
1264     PK1,
1265     PK2,
1266     PK3,
1267     DESIGN_EFFECTIVENESS,
1268     OP_EFFECTIVENESS,
1269     ASSOCIATION_CREATION_DATE,
1270     APPROVAL_DATE,
1271     DELETION_DATE,
1272     DELETION_APPROVAL_DATE,
1273     OBJECT_TYPE,
1274     LAST_UPDATE_DATE,
1275     LAST_UPDATED_BY,
1276     CREATION_DATE,
1277     CREATED_BY,
1278     LAST_UPDATE_LOGIN,
1279     OBJECT_VERSION_NUMBER)
1280     (select
1281     AMW_AP_ASSOCIATIONS_S.nextval,
1282     AUDIT_PROCEDURE_ID,
1283     p_org_id,
1284     p_child_process_id,
1285     PK1,
1286 --ko, the values are pk1 = org, pk2 = process, pk3 = control in the org context.    PK2,
1287     DESIGN_EFFECTIVENESS,
1288     OP_EFFECTIVENESS,
1289     null, --ko commenting.. we set association creation date upon approval of the process..sysdate,
1290     null,
1291     null,
1292     null,
1293     'CTRL_ORG',
1294     sysdate,
1295     G_USER_ID,
1296     sysdate,
1297     G_USER_ID,
1298     G_LOGIN_ID,
1299     1
1300     from amw_ap_associations
1301     where PK1 in --ko, replacing  = with in  controls can be more than one..
1302         (select distinct control_id
1303         from amw_control_associations
1304         where PK1 = p_child_process_id
1305         and object_type = 'RISK'
1306         and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
1307         and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)))
1308     and object_type = 'CTRL'
1309     and approval_date is not null
1310     and deletion_approval_date is null);
1311 --    and (APPROVAL_DATE is not null and APPROVAL_DATE <= sysdate)
1312 --    and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)));
1313 
1314 
1315 end import_rcm_for_new_orgprocess;
1316 -- ****************************************************************************
1317 
1318 -- make the attributes and RCM of a target process in org the same as those of
1319 -- the same process in rl. If the target is in draft, just update, else revise and update
1320 procedure synch_process_att_rcm(
1321 	p_org_id in number,
1322 	p_process_id in number,
1323 	apply_rcm in varchar2) is
1324 
1325 l_RL_PROCESS_REV_ID               amw_process.PROCESS_REV_ID%type;
1326 l_SIGNIFICANT_PROCESS_FLAG	      amw_process.SIGNIFICANT_PROCESS_FLAG%type;
1327 l_STANDARD_PROCESS_FLAG 	      amw_process.STANDARD_PROCESS_FLAG%type;
1328 l_PROCESS_CATEGORY    		      amw_process.PROCESS_CATEGORY%type;
1329 l_STANDARD_VARIATION      	      amw_process.STANDARD_VARIATION%type;
1330 l_ATTRIBUTE_CATEGORY      	      amw_process.ATTRIBUTE_CATEGORY%type;
1331 l_ATTRIBUTE1              	      amw_process.ATTRIBUTE1%type;
1332 l_ATTRIBUTE2              	      amw_process.ATTRIBUTE2%type;
1333 l_ATTRIBUTE3              	      amw_process.ATTRIBUTE3%type;
1334 l_ATTRIBUTE4              	      amw_process.ATTRIBUTE4%type;
1335 l_ATTRIBUTE5              	      amw_process.ATTRIBUTE5%type;
1336 l_ATTRIBUTE6              	      amw_process.ATTRIBUTE6%type;
1337 l_ATTRIBUTE7              	      amw_process.ATTRIBUTE7%type;
1338 l_ATTRIBUTE8              	      amw_process.ATTRIBUTE8%type;
1339 l_ATTRIBUTE9              	      amw_process.ATTRIBUTE9%type;
1340 l_ATTRIBUTE10             	      amw_process.ATTRIBUTE10%type;
1341 l_ATTRIBUTE11             	      amw_process.ATTRIBUTE11%type;
1342 l_ATTRIBUTE12             	      amw_process.ATTRIBUTE12%type;
1343 l_ATTRIBUTE13             	      amw_process.ATTRIBUTE13%type;
1344 l_ATTRIBUTE14             	      amw_process.ATTRIBUTE14%type;
1345 l_ATTRIBUTE15             	      amw_process.ATTRIBUTE15%type;
1346 l_SECURITY_GROUP_ID       	      amw_process.SECURITY_GROUP_ID%type;
1347 l_PROCESS_TYPE            	      amw_process.PROCESS_TYPE%type;
1348 l_CONTROL_ACTIVITY_TYPE		      amw_process.CONTROL_ACTIVITY_TYPE%type;
1349 
1350 
1351 begin
1352   -- find out if the latest revision for target is approved or not.
1353   -- if approved, revise it. if draft, don't do anything
1354       revise_process_if_necessary(p_org_id, p_process_id);
1355 
1356   -- copy the attributes from rl to org
1357        select PROCESS_REV_ID,
1358 			  SIGNIFICANT_PROCESS_FLAG,
1359 			  STANDARD_PROCESS_FLAG,
1360 			  PROCESS_CATEGORY,
1361 			  STANDARD_VARIATION,
1362 			  ATTRIBUTE_CATEGORY,
1363 			  ATTRIBUTE1,
1364 			  ATTRIBUTE2,
1365 			  ATTRIBUTE3,
1366 			  ATTRIBUTE4,
1367 			  ATTRIBUTE5,
1368 			  ATTRIBUTE6,
1369 			  ATTRIBUTE7,
1370 			  ATTRIBUTE8,
1371 			  ATTRIBUTE9,
1372 			  ATTRIBUTE10,
1373 			  ATTRIBUTE11,
1374 			  ATTRIBUTE12,
1375 			  ATTRIBUTE13,
1376 			  ATTRIBUTE14,
1377 			  ATTRIBUTE15,
1378 			  SECURITY_GROUP_ID,
1379 			  PROCESS_TYPE,
1380 			  CONTROL_ACTIVITY_TYPE
1381               into
1382               l_RL_PROCESS_REV_ID,
1383 			  l_SIGNIFICANT_PROCESS_FLAG,
1384 			  l_STANDARD_PROCESS_FLAG,
1385 			  l_PROCESS_CATEGORY,
1386 			  l_STANDARD_VARIATION,
1387 			  l_ATTRIBUTE_CATEGORY,
1388 			  l_ATTRIBUTE1,
1389 			  l_ATTRIBUTE2,
1390 			  l_ATTRIBUTE3,
1391 			  l_ATTRIBUTE4,
1392 			  l_ATTRIBUTE5,
1393 			  l_ATTRIBUTE6,
1394 			  l_ATTRIBUTE7,
1395 			  l_ATTRIBUTE8,
1396 			  l_ATTRIBUTE9,
1397 			  l_ATTRIBUTE10,
1398 			  l_ATTRIBUTE11,
1399 			  l_ATTRIBUTE12,
1400 			  l_ATTRIBUTE13,
1401 			  l_ATTRIBUTE14,
1402 			  l_ATTRIBUTE15,
1403 			  l_SECURITY_GROUP_ID,
1404 			  l_PROCESS_TYPE,
1405 			  l_CONTROL_ACTIVITY_TYPE
1406               from amw_process
1407               where process_id = p_process_id
1408               and end_date is null;
1409 
1410 
1411       update amw_process_organization
1412       set     RL_PROCESS_REV_ID             =   l_RL_PROCESS_REV_ID,
1413 			  SIGNIFICANT_PROCESS_FLAG      =   l_SIGNIFICANT_PROCESS_FLAG,
1414 			  STANDARD_PROCESS_FLAG	        =   l_STANDARD_PROCESS_FLAG,
1415 			  PROCESS_CATEGORY_CODE	            =   l_PROCESS_CATEGORY,
1416 			  STANDARD_VARIATION	        =   l_STANDARD_VARIATION,
1417 			  ATTRIBUTE_CATEGORY            =   l_ATTRIBUTE_CATEGORY,
1418 			  ATTRIBUTE1		    =   l_ATTRIBUTE1,
1419 			  ATTRIBUTE2			=   l_ATTRIBUTE2,
1420 			  ATTRIBUTE3			=   l_ATTRIBUTE3,
1421 			  ATTRIBUTE4			=   l_ATTRIBUTE4,
1422 			  ATTRIBUTE5			=   l_ATTRIBUTE5,
1423 			  ATTRIBUTE6			=   l_ATTRIBUTE6,
1424 			  ATTRIBUTE7			=   l_ATTRIBUTE7,
1425 			  ATTRIBUTE8			=   l_ATTRIBUTE8,
1426 			  ATTRIBUTE9			=   l_ATTRIBUTE9,
1427 			  ATTRIBUTE10			=   l_ATTRIBUTE10,
1428 			  ATTRIBUTE11			=   l_ATTRIBUTE11,
1429 			  ATTRIBUTE12			=   l_ATTRIBUTE12,
1430 			  ATTRIBUTE13			=   l_ATTRIBUTE13,
1431 			  ATTRIBUTE14			=   l_ATTRIBUTE14,
1432 			  ATTRIBUTE15			=   l_ATTRIBUTE15,
1433 			  SECURITY_GROUP_ID		=   l_SECURITY_GROUP_ID,
1434 			  PROCESS_TYPE			=   l_PROCESS_TYPE,
1435 			  CONTROL_ACTIVITY_TYPE	=   l_CONTROL_ACTIVITY_TYPE
1436       where organization_id = p_org_id
1437       and process_id = p_process_id
1438       and end_date is null;
1439 
1440   -- apply process key accounts
1441 
1442     -- delete existing accounts
1443         update amw_acct_associations
1444         set DELETION_DATE = sysdate
1445         where pk1 = p_org_id
1446         and   pk2 = p_process_id
1447         and   object_type = 'PROCESS_ORG'
1448         and   deletion_date is null;
1449     --ko, delete the rows that are not approved..happens in case of synchronizing latest revision and the some accounts are added to that revision..
1450       delete amw_acct_associations
1451       where pk1 = p_org_id
1452         and   pk2 = p_process_id
1453         and   object_type = 'PROCESS_ORG'
1454         and   approval_date is null;
1455 
1456     -- add new accounts
1457     import_process_attributes(p_process_id, p_org_id);
1458 
1459   -- apply rcm if necessary
1460     if apply_rcm = 'Y' then
1461 
1462     -- delete existing rcm
1463     -- populate the delete date for the existing rows where delete_date is null and
1464     -- import corresponding data from library context. Ideally we don't need to populate
1465     -- the delete_dates for all the current rows, we can do an incremental thing, i.e.
1466     -- delete rows that are not there in rl, and add the remaining. However, that means
1467     -- we need to do some in memory processing, and for the time being, I choose to skip that.
1468         delete_existing_rcm(p_process_id, p_org_id);
1469 
1470     -- apply new rcm
1471         import_rcm_for_new_orgprocess(p_process_id, p_org_id);
1472     end if;
1473 
1474 end synch_process_att_rcm;
1475 
1476 
1477 -- ****************************************************************************
1478 
1479 procedure delete_existing_rcm(
1480 p_process_id in number,
1481 p_org_id     in number) is
1482 
1483 begin
1484 
1485 -- ko, We Don't want the draft associations to linger in the table..So delete them..
1486         delete amw_risk_associations
1487         where pk1 = p_org_id
1488         and   pk2 = p_process_id
1489         and   object_type = 'PROCESS_ORG'
1490         and   approval_date is null;
1491 
1492         update amw_risk_associations
1493         set DELETION_DATE = sysdate
1494         where pk1 = p_org_id
1495         and   pk2 = p_process_id
1496         and   object_type = 'PROCESS_ORG'
1497         and   deletion_date is null;
1498 
1499 --ko, Similarly for controls also..
1500 
1501         delete amw_control_associations
1502         where pk1 = p_org_id
1503         and   pk2 = p_process_id
1504         and   object_type = 'RISK_ORG'
1505         and   approval_date is null;
1506 
1507         update amw_control_associations
1508         set DELETION_DATE = sysdate
1509         where pk1 = p_org_id
1510         and   pk2 = p_process_id
1511         and   object_type = 'RISK_ORG'
1512         and   deletion_date is null;
1513 
1514 -- abedajna, control objectives
1515         delete amw_objective_associations
1516         where pk1 = p_org_id
1517         and   pk2 = p_process_id
1518         and   object_type = 'CONTROL_ORG'
1519         and   approval_date is null;
1520 
1521         update amw_objective_associations
1522         set DELETION_DATE = sysdate
1523         where pk1 = p_org_id
1524         and   pk2 = p_process_id
1525         and   object_type = 'CONTROL_ORG'
1526         and   deletion_date is null;
1527 
1528 --      in orgs, ap's don't undergo revisions..
1529 -- association creation date is null for AP's for draft control associations..We can remove those aps..
1530 -- ko1. uncommenting the following lines.. We do need to set the deletion date in order to maintain the history.
1531       delete from amw_ap_associations
1532       where pk1 = p_org_id
1533       and   pk2 = p_process_id
1534       and association_creation_date is null
1535       and   object_type = 'CTRL_ORG';
1536 
1537       update amw_ap_associations
1538       set DELETION_DATE = sysdate
1539       where pk1 = p_org_id
1540       and   pk2 = p_process_id
1541       and   object_type = 'CTRL_ORG'
1542       and   deletion_date is null;
1543 
1544 --ko2. commenting the following..
1545 --
1546 
1547 
1548 end delete_existing_rcm;
1549 
1550 -- ****************************************************************************
1551 
1552 /*
1553 create_new_as_child_of_ex; (the basic assumption is the process itself doesn't exist in the org)
1554 examine children in rl;
1555 	if child does not exist in org, create_new_as_child_of_ex;
1556 	if child exists in org
1557 		if p_revise_existing = 'Y', synchronize
1558         if p_revise_existing = 'N', exit
1559 
1560 
1561 
1562 
1563 		if p_revise_existing = 'N'
1564 			does child exist as "direct child" of parent?
1565 				if yes then
1566 					exit;
1567 				if no then
1568 					add_ex_as_child_of_ex;
1569 					exit;
1570 		else if p_revise_existing = 'Y'
1571 			is the child in Pending Approval status?
1572 				is yes then
1573 					exit with error;
1574 			does child exist as "direct child" of parent?
1575 				if yes then
1576 					revise_process_att_rcm;
1577 					-- note that we revise the process even if the
1578 					-- atts and rcm are the same in org and rl, i.e.
1579 					-- we do not perform this extra check
1580 				if no then
1581 					-- note that we revise the process even if the
1582 					-- atts and rcm are the same in org and rl, i.e.
1583 					-- we do not perform this extra check
1584 					revise_process_att_rcm;
1585 					add_ex_as_child_of_ex;
1586 */
1587 
1588 -- main procedure for associating.
1589 -- just make sure before calling this that the p_associated_proc_id does not already
1590 procedure associate_process_to_org (
1591 	p_org_id in number,
1592 	p_parent_process_id in number,
1593 	p_associated_proc_id in number,
1594 	p_revise_existing in varchar2,
1595     p_apply_rcm in varchar2) is
1596 
1597 pex varchar2(1);
1598 l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
1599 l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
1600 
1601 begin
1602 
1603 	if( l_log_stmt_level >= l_curr_log_level ) then
1604     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.begin',
1605         	'OrgId:' ||p_org_id || ';ProcessId:'||p_associated_proc_id
1606         	||';ParentProcessId:'||p_parent_process_id||';reviseExisting:'||p_revise_existing||';ApplyRCM:'||p_apply_rcm);
1607         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.does_process_exist_in_org',
1608         	'p_associated_proc_id:'||p_associated_proc_id||';p_org_id:'||p_org_id);
1609 	end if;
1610     pex := does_process_exist_in_org(p_associated_proc_id, p_org_id);
1611 	if pex = 'N' then
1612 	   -- The process is not existing in the organziation...
1613 		-- When the process is not existing in the system, apply_rcm is always true....so pass 'Y' in the apply rcm parameter.
1614 		if( l_log_stmt_level >= l_curr_log_level ) then
1615     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.import_rlproc_as_child_of_ex',
1616         		p_org_id||';'||p_parent_process_id||';'||p_associated_proc_id||';'||'Y');
1617 		end if;
1618         import_rlproc_as_child_of_ex (p_org_id, p_parent_process_id, p_associated_proc_id, 'Y');
1619 
1620         if( l_log_stmt_level >= l_curr_log_level ) then
1621     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.associate_hierarchy',
1622         		p_associated_proc_id ||';'||p_org_id ||';'||p_revise_existing ||';'||p_apply_rcm);
1623 		end if;
1624 		associate_hierarchy(p_associated_proc_id, p_org_id, p_revise_existing, p_apply_rcm);
1625 
1626 	elsif pex = 'D' then --The process is existing in the system some time back and got deleted. so undelete it and sync up..
1627 			if( l_log_stmt_level >= l_curr_log_level ) then
1628     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.undelete',
1629         			p_associated_proc_id ||';'||p_org_id );
1630 			end if;
1631 	        undelete(p_associated_proc_id, p_org_id);
1632 	        if( l_log_stmt_level >= l_curr_log_level ) then
1633     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.add_delete_ex_child',
1634         			p_org_id ||';'||p_parent_process_id ||';'||p_associated_proc_id ||';'||'ADD');
1635 			end if;
1636 			add_delete_ex_child (p_org_id, p_parent_process_id, p_associated_proc_id, 'ADD');
1637 			if( l_log_stmt_level >= l_curr_log_level ) then
1638     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.Synchronize_process',
1639         			p_org_id ||';'||p_associated_proc_id);
1640 			end if;
1641             Synchronize_process(p_org_id      => p_org_id,
1642 								p_process_id  => p_associated_proc_id,
1643 								p_sync_mode   => 'PONLY',               -- Include sub processes also..
1644 								p_sync_hierarchy => 'NO',              -- Sync up the hierarchy also..
1645 								p_sync_attributes => 'YES',				-- Sync up the attributes also..
1646 								p_sync_rcm        => 'SLIB',			-- Sync with the library definition...
1647 								p_sync_people     => 'SLIB'				-- Sync with library definition....
1648 								 );
1649 			if( l_log_stmt_level >= l_curr_log_level ) then
1650     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.associate_hierarchy',
1651         			p_associated_proc_id ||';'||p_org_id||';'||p_revise_existing||';'||p_apply_rcm);
1652 			end if;
1653 			associate_hierarchy(p_associated_proc_id, p_org_id, p_revise_existing, p_apply_rcm);
1654     end if;
1655 
1656     if( l_log_stmt_level >= l_curr_log_level ) then
1657     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_PROCESS_TO_ORG.End',
1658         'End');
1659 	end if;
1660 end associate_process_to_org;
1661 
1662 
1663 -- ****************************************************************************
1664 -- note that p_revise_existing => leave the exisitng process (if exists) as is
1665 -- or "revise" it. Here "revise" is used more in a loose sense of the term,
1666 -- in the sense of synchronization
1667 procedure associate_hierarchy (
1668 	p_parent_process_id in number,
1669   	p_org_id in number,
1670     p_revise_existing in varchar2,
1671     p_apply_rcm in varchar2) is
1672 
1673   cursor c1 (l_pid number) is
1674     select ah.child_id child_process_id
1675       from amw_approved_hierarchies ah
1676       where ah.parent_id = (select pp.process_id
1677                             from amw_process pp
1678                             where pp.process_id = ah.parent_id
1679                             and pp.approval_date is not null
1680                             and pp.approval_end_date is null
1681                             and pp.deletion_date is null)
1682        and ah.child_id  =  ( select Cp.process_id
1683                             from amw_process Cp
1684                             where Cp.process_id = ah.child_id
1685                             and Cp.approval_date is not null
1686                             and Cp.approval_end_date is null
1687                             and Cp.deletion_date is null)
1688        and ah.start_date is not null
1689        and ah.end_date is null
1690        and ah.organization_id = -1
1691        and ah.parent_id = l_pid;
1692 
1693   c1_rec c1%rowtype;
1694   pex varchar2(1);
1695   l_child_process_id number;
1696   pending_approval_exception exception;
1697   err_msg varchar2(4000);
1698   l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
1699   l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
1700 
1701 begin
1702   if( l_log_stmt_level >= l_curr_log_level ) then
1703     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1704         'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.begin',
1705         ' OrgId:' ||p_org_id ||';ParentProcessId:'||p_parent_process_id||';reviseExisting:'||p_revise_existing||';ApplyRCM:'||p_apply_rcm);
1706   end if;
1707 
1708   for c1_rec in c1(p_parent_process_id) loop
1709 	  exit when c1%notfound;
1710         l_child_process_id := c1_rec.child_process_id;
1711         if( l_log_stmt_level >= l_curr_log_level ) then
1712     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1713         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.does_process_exist_in_org',l_child_process_id||';'||p_org_id);
1714   		end if;
1715         pex := does_process_exist_in_org(l_child_process_id, p_org_id);
1716 
1717         -- case 1: l_process_id does not exist in the org
1718         if pex = 'N' then
1719         -- Apply RCM is Yes for a new process.....
1720         	if( l_log_stmt_level >= l_curr_log_level ) then
1721     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1722         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.import_rlproc_as_child_of_ex',
1723         		p_org_id||';'||p_parent_process_id||';'||l_child_process_id||';'||'Y');
1724   			end if;
1725             import_rlproc_as_child_of_ex(p_org_id, p_parent_process_id, l_child_process_id, 'Y');
1726 			if( l_log_stmt_level >= l_curr_log_level ) then
1727     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1728         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.associate_hierarchy',
1729         		l_child_process_id||';'||p_org_id||';'||p_revise_existing||';'||p_apply_rcm);
1730   			end if;
1731             associate_hierarchy(l_child_process_id, p_org_id, p_revise_existing, p_apply_rcm);
1732         -- case 2: l_process_id exists in the org
1733         elsif pex = 'Y' then
1734         	if( l_log_stmt_level >= l_curr_log_level ) then
1735     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.add_delete_ex_child',
1736         			p_org_id ||';'||p_parent_process_id ||';'||l_child_process_id ||';'||'ADD');
1737 			end if;
1738             add_delete_ex_child (p_org_id, p_parent_process_id, l_child_process_id, 'ADD');
1739             if p_revise_existing = 'Y' then
1740                     -- Don't need the following line..sync process handles this..
1741                     --produce_err_if_pa_or_locked(p_org_id, l_child_process_id);
1742                     -- User want to synchronize the changes..so do sync up the process..apply_rcm is passed by user for the existing processes.
1743                     if( l_log_stmt_level >= l_curr_log_level ) then
1744     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.Synchronize_process',
1745         				p_org_id ||';'||l_child_process_id ||';'||'PSUBP');
1746 					end if;
1747                     Synchronize_process(
1748 										p_org_id      => p_org_id,
1749 										p_process_id  => l_child_process_id,
1750 										p_sync_mode   => 'PSUBP',               -- Include sub processes also..
1751 										p_sync_hierarchy => 'YES',              -- Sync up the hierarchy also..
1752 										p_sync_attributes => 'YES',				-- Sync up the attributes also..
1753 										p_sync_rcm        => p_apply_rcm,				-- get the value from the user...
1754 										p_sync_people     => 'SLIB'				-- Sync with library definition....
1755 								      );
1756 			else
1757 				 if( l_log_stmt_level >= l_curr_log_level ) then
1758     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.Synchronize_process',
1759         				p_org_id ||';'||l_child_process_id ||';'||'PONLY');
1760 				 end if;
1761 				Synchronize_process(    p_org_id      => p_org_id,
1762 										p_process_id  => l_child_process_id,
1763 										p_sync_mode   => 'PONLY',               -- DON'T Include sub processes also..
1764 										p_sync_hierarchy => 'NO',              --  NO Sync up the hierarchy also..
1765 										p_sync_attributes => 'NO',				-- DON'T Sync up the attributes also..
1766 										p_sync_rcm        => p_apply_rcm,	    -- DO IT ACCORDING TO THE value from the user...
1767 										p_sync_people     => 'RDEF'				-- DON'T DO ANY CHANGES..Sync with library definition....
1768 								      );
1769 
1770             end if;
1771         -- case 3: l_process_id exists in the org but is deleted.
1772         elsif pex = 'D' then
1773 
1774             if p_revise_existing = 'Y' then
1775             		if( l_log_stmt_level >= l_curr_log_level ) then
1776     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.produce_err_if_pa_or_locked',
1777         				p_org_id ||';'||l_child_process_id );
1778 				 	end if;
1779                     produce_err_if_pa_or_locked(p_org_id, l_child_process_id);
1780                     if( l_log_stmt_level >= l_curr_log_level ) then
1781     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.associate_process_to_org',
1782         				p_org_id ||';'||l_child_process_id ||';'||l_child_process_id);
1783 				 	end if;
1784                     associate_process_to_org (  p_org_id => p_org_id,
1785 												p_parent_process_id => p_parent_process_id,
1786 												p_associated_proc_id => l_child_process_id,
1787 												p_revise_existing => p_revise_existing,
1788 												p_apply_rcm => p_apply_rcm);
1789             elsif p_revise_existing = 'N' then
1790                 -- Check if the process got deleted and approved..In this case we need to bring this back ...
1791                 BEGIN
1792                 	select 'Y' into pex
1793                 	from amw_process_organization
1794                 	where organization_id = p_org_id
1795                 	and process_id = l_child_process_id
1796                 	and end_date is null
1797                 	and approval_date is not null
1798                 	and deletion_date is not null;
1799 
1800                 	-- HERE WE NEED TO ASSOCIATE THIS ONE AS NEW PROCESS..
1801                 	if( l_log_stmt_level >= l_curr_log_level ) then
1802     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.associate_process_to_org',
1803         				p_org_id ||';'||l_child_process_id ||';'||l_child_process_id);
1804 				 	end if;
1805                 	associate_process_to_org (
1806 											p_org_id => p_org_id,
1807 											p_parent_process_id => p_parent_process_id,
1808 											p_associated_proc_id => l_child_process_id,
1809 											p_revise_existing => p_revise_existing,
1810 											p_apply_rcm => p_apply_rcm);
1811 
1812                 EXCEPTION
1813                 	WHEN NO_DATA_FOUND THEN
1814                 		null;
1815 
1816 				END;
1817 
1818             end if;
1819 
1820         end if;
1821   end loop;
1822  if( l_log_stmt_level >= l_curr_log_level ) then
1823     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1824         'amw.plsql.AMW_ORG_HIERARCHY_PKG.ASSOCIATE_HIERARCHY.End',
1825         'End');
1826   end if;
1827 end associate_hierarchy;
1828 
1829 -- ****************************************************************************
1830 -- this is very similar to associate_hierarchy, but there are some important differences.
1831 -- During synchronization, the existing children of the process being synchronized are deleted
1832 -- in the org. Obviously, p_revise_existing has no meaning, by default it is Y
1833 
1834 -- assume that p_parent_process_id exists in the org and its attributes and RCM
1835 -- are already synchronized. also, this process is in draft status.
1836 -- traverse the approved hierarchy in rl. For every process_id in org, delete its
1837 -- existing list of children and add the children from rl (associate if necessary).
1838 -- do this recursively.
1839 procedure synchronize_hierarchy (
1840 								p_org_id   in number,
1841 								p_parent_process_id  in number,
1842 								p_sync_attributes in varchar2,
1843 								p_sync_rcm in varchar2,
1844 								p_sync_people in varchar2
1845 								) is
1846 
1847     cursor c1 (l_pid number) is
1848     select ah.child_id child_process_id
1849       from amw_approved_hierarchies ah
1850       where ah.parent_id = (select pp.process_id
1851                             from amw_process pp
1852                             where pp.process_id = ah.parent_id
1853                             and pp.approval_date is not null
1854                             and pp.approval_end_date is null
1855                             and pp.deletion_date is null)
1856        and ah.child_id  =  ( select Cp.process_id
1857                             from amw_process Cp
1858                             where Cp.process_id = ah.child_id
1859                             and Cp.approval_date is not null
1860                             and Cp.approval_end_date is null
1861                             and Cp.deletion_date is null)
1862        and ah.start_date is not null
1863        and ah.end_date is null
1864        and ah.organization_id = -1
1865        and ah.parent_id = l_pid;
1866 
1867   c1_rec c1%rowtype;
1868   l_child_process_id number;
1869   pex varchar2(1);
1870   l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
1871   l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
1872 
1873 begin
1874 
1875 	if( l_log_stmt_level >= l_curr_log_level ) then
1876     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1877         'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.begin',
1878         ' OrgId:' ||p_org_id || ';p_parent_process_id:'||p_parent_process_id
1879         ||';p_sync_attributes:'||p_sync_attributes||';p_sync_rcm:'||p_sync_rcm||';p_sync_people:'||p_sync_people);
1880 	end if;
1881 
1882 
1883 -- first delete all children of this process in the org.
1884   delete from amw_latest_hierarchies
1885   where parent_id = p_parent_process_id
1886   and organization_id = p_org_id;
1887 
1888 
1889   for c1_rec in c1(p_parent_process_id) loop
1890 	  exit when c1%notfound;
1891         l_child_process_id := c1_rec.child_process_id;
1892         if( l_log_stmt_level >= l_curr_log_level ) then
1893     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1894         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.does_process_exist_in_org',
1895         	l_child_process_id||';'||p_org_id);
1896 		end if;
1897         pex := does_process_exist_in_org(l_child_process_id, p_org_id);
1898 
1899         -- case 1: child does not exist in the org, associate it
1900         if pex = 'N' then
1901         	if( l_log_stmt_level >= l_curr_log_level ) then
1902     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1903         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.import_rlproc_as_child_of_ex',
1904         		p_org_id||';'||l_child_process_id);
1905 			end if;
1906             import_rlproc_as_child_of_ex(p_org_id, p_parent_process_id, l_child_process_id, 'Y');
1907           	if( l_log_stmt_level >= l_curr_log_level ) then
1908     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1909         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.synchronize_hierarchy',
1910         		p_org_id||';'||l_child_process_id);
1911 			end if;
1912 
1913             synchronize_hierarchy(p_org_id   		   =>p_org_id,
1914 								  p_parent_process_id  =>l_child_process_id,
1915 								  p_sync_attributes    =>p_sync_attributes,
1916 								  p_sync_rcm           =>p_sync_rcm,
1917 								  p_sync_people        =>p_sync_people
1918 								  );
1919         -- case 2: child exists in the org
1920         elsif pex = 'Y' then
1921         	if( l_log_stmt_level >= l_curr_log_level ) then
1922     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1923         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.add_delete_ex_child',
1924         		p_org_id||';'||p_parent_process_id||';'||l_child_process_id||';'||'ADD');
1925 			end if;
1926             add_delete_ex_child (p_org_id, p_parent_process_id, l_child_process_id, 'ADD');
1927 
1928 --            produce_err_if_pa_or_locked(p_org_id, l_child_process_id);
1929 			if( l_log_stmt_level >= l_curr_log_level ) then
1930     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1931         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.synchronize_process',
1932         		p_org_id||';'||l_child_process_id);
1933 			end if;
1934             synchronize_process(  p_org_id   		   =>p_org_id,
1935 								  p_process_id  =>l_child_process_id,
1936 								  p_sync_mode          => 'PSUBP',          -- SYNC UP ALL THE CHILD PROCESSES
1937 								  p_sync_hierarchy     =>'YES',				-- SYNC UP THE HIERARCHY ALSO..
1938 								  p_sync_attributes    =>p_sync_attributes,
1939 								  p_sync_rcm           =>p_sync_rcm,
1940 								  p_sync_people        =>p_sync_people
1941 								  );
1942         -- case 3: child exists in the org but is deleted.
1943         elsif pex = 'D' then
1944 
1945         -- Check if the process got deleted and approved..In this case we need to bring this back ...
1946                 BEGIN
1947                 	select 'Y' into pex
1948                 	from amw_process_organization
1949                 	where organization_id = p_org_id
1950                 	and process_id = l_child_process_id
1951                 	and end_date is null
1952                 	and approval_date is not null
1953                 	and deletion_date is not null;
1954 					if( l_log_stmt_level >= l_curr_log_level ) then
1955     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1956         				'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.undelete',
1957         				l_child_process_id||';'||p_org_id);
1958 					end if;
1959                 	undelete(l_child_process_id, p_org_id);
1960                 	if( l_log_stmt_level >= l_curr_log_level ) then
1961     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1962         				'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.add_delete_ex_child',
1963         				p_org_id||';'||p_parent_process_id||';'||l_child_process_id||';'||'ADD');
1964 					end if;
1965                   	add_delete_ex_child (p_org_id, p_parent_process_id, l_child_process_id, 'ADD');
1966                   	if( l_log_stmt_level >= l_curr_log_level ) then
1967     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1968         				'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.synchronize_process',
1969         				p_org_id||';'||l_child_process_id);
1970 					end if;
1971                   	Synchronize_process(p_org_id      => p_org_id,
1972 								p_process_id  => l_child_process_id,
1973 								p_sync_mode   => 'PONLY',               -- Include sub processes also..
1974 								p_sync_hierarchy => 'NO',              -- Sync up the hierarchy also..
1975 								p_sync_attributes => 'YES',				-- Sync up the attributes also..
1976 								p_sync_rcm        => 'SLIB',			-- Sync with the library definition...
1977 								p_sync_people     => 'SLIB'				-- Sync with library definition....
1978 								 );
1979 
1980 					if( l_log_stmt_level >= l_curr_log_level ) then
1981     					FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1982         				'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.synchronize_hierarchy',
1983         				p_org_id||';'||l_child_process_id);
1984 					end if;
1985                 	synchronize_hierarchy(p_org_id     =>p_org_id,
1986 								  p_parent_process_id  =>l_child_process_id,
1987 								  p_sync_attributes    =>p_sync_attributes,
1988 								  p_sync_rcm           =>p_sync_rcm,
1989 								  p_sync_people        =>p_sync_people
1990 								  );
1991 
1992                 EXCEPTION
1993                 	WHEN NO_DATA_FOUND THEN
1994                 		if( l_log_stmt_level >= l_curr_log_level ) then
1995     						FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1996         					'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.undelete',
1997         					l_child_process_id||';'||p_org_id);
1998 						end if;
1999                 		undelete(l_child_process_id, p_org_id);
2000                 		if( l_log_stmt_level >= l_curr_log_level ) then
2001     						FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
2002         					'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.add_delete_ex_child',
2003         					p_org_id||';'||p_parent_process_id||';'||l_child_process_id||';'||'ADD');
2004 						end if;
2005                   		add_delete_ex_child (p_org_id, p_parent_process_id, l_child_process_id, 'ADD');
2006                   		if( l_log_stmt_level >= l_curr_log_level ) then
2007     						FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
2008         					'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_HIERARCHY.synchronize_process',
2009         					p_org_id||';'||l_child_process_id);
2010 						end if;
2011                   		Synchronize_process(p_org_id      => p_org_id,
2012 								p_process_id  => l_child_process_id,
2013 								p_sync_mode   => 'PSUBP',               -- Include sub processes also..
2014 								p_sync_hierarchy => 'YES',              -- Sync up the hierarchy also..
2015 								p_sync_attributes => p_sync_attributes,				-- Sync up the attributes also..
2016 								p_sync_rcm        => p_sync_rcm,			-- Sync with the library definition...
2017 								p_sync_people     => p_sync_people				-- Sync with library definition....
2018 								 );
2019 
2020 				END;
2021 
2022         end if;
2023   end loop;
2024 
2025 
2026 end synchronize_hierarchy;
2027 
2028 --*****************************************************************************
2029 -- Synchronize Risk library Process hierarchy with the Organization
2030 -- Process Hierarchy
2031 --*****************************************************************************
2032 procedure synchronize_hierarchy (
2033 								p_parent_process_id  in number,
2034 								p_sync_attributes in varchar2,
2035 								p_sync_rcm in varchar2,
2036 								p_sync_people in varchar2
2037 								) is
2038 
2039     cursor c1 (l_pid number) is
2040     select ah.child_id child_process_id
2041       from amw_approved_hierarchies ah
2042       where ah.parent_id = (select pp.process_id
2043                             from amw_process pp
2044                             where pp.process_id = ah.parent_id
2045                             and pp.approval_date is not null
2046                             and pp.approval_end_date is null
2047                             and pp.deletion_date is null)
2048        and ah.child_id  =  ( select Cp.process_id
2049                             from amw_process Cp
2050                             where Cp.process_id = ah.child_id
2051                             and Cp.approval_date is not null
2052                             and Cp.approval_end_date is null
2053                             and Cp.deletion_date is null)
2054        and ah.start_date is not null
2055        and ah.end_date is null
2056        and ah.organization_id = -1
2057        and ah.parent_id = l_pid;
2058 
2059   c1_rec c1%rowtype;
2060   l_child_process_id number;
2061   pex varchar2(1);
2062   l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
2063   l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
2064 
2065 begin
2066 
2067     -- first delete all children of this process in the org.
2068     FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
2069         delete from amw_latest_hierarchies
2070         where parent_id = p_parent_process_id
2071         and organization_id = Org_Ids(indx);
2072 
2073     FOR indx IN Org_Ids.FIRST .. Org_Ids.LAST
2074     LOOP
2075         for c1_rec in c1(p_parent_process_id) loop
2076         exit when c1%notfound;
2077             l_child_process_id := c1_rec.child_process_id;
2078             pex := does_process_exist_in_org(l_child_process_id, Org_Ids(indx));
2079 
2080             -- case 1: child does not exist in the org, associate it
2081             if pex = 'N' then
2082                 import_rlproc_as_child_of_ex(Org_Ids(indx), p_parent_process_id, l_child_process_id, 'Y');
2083                 synchronize_hierarchy(p_org_id   		   =>Org_Ids(indx),
2084 				    				  p_parent_process_id  =>l_child_process_id,
2085 					       			  p_sync_attributes    =>p_sync_attributes,
2086 					   	       		  p_sync_rcm           =>p_sync_rcm,
2087 							     	  p_sync_people        =>p_sync_people
2088 								     );
2089             -- case 2: child exists in the org
2090             elsif pex = 'Y' then
2091                 add_delete_ex_child (Org_Ids(indx), p_parent_process_id, l_child_process_id, 'ADD');
2092 
2093                 synchronize_process(  p_org_id   		   =>Org_Ids(indx),
2094 	       							  p_process_id         =>l_child_process_id,
2095 		      						  p_sync_mode          => 'PSUBP',          -- SYNC UP ALL THE CHILD PROCESSES
2096 			     					  p_sync_hierarchy     =>'YES',				-- SYNC UP THE HIERARCHY ALSO..
2097 				    				  p_sync_attributes    =>p_sync_attributes,
2098 					       			  p_sync_rcm           =>p_sync_rcm,
2099 						      		  p_sync_people        =>p_sync_people
2100                                     );
2101             -- case 3: child exists in the org but is deleted.
2102             elsif pex = 'D' then
2103             -- Check if the process got deleted and approved..In this case we need to bring this back ...
2104                 BEGIN
2105                 	select 'Y' into pex
2106                 	from amw_process_organization
2107                 	where organization_id = Org_Ids(indx)
2108                 	and process_id = l_child_process_id
2109                 	and end_date is null
2110                 	and approval_date is not null
2111                 	and deletion_date is not null;
2112 
2113                 	undelete(l_child_process_id, Org_Ids(indx));
2114 
2115                   	add_delete_ex_child (Org_Ids(indx), p_parent_process_id, l_child_process_id, 'ADD');
2116 
2117                   	Synchronize_process(p_org_id      => Org_Ids(indx),
2118 								p_process_id  => l_child_process_id,
2119 								p_sync_mode   => 'PONLY',               -- Include sub processes also..
2120 								p_sync_hierarchy => 'NO',               -- Sync up the hierarchy also..
2121 								p_sync_attributes => 'YES',				-- Sync up the attributes also..
2122 								p_sync_rcm        => 'SLIB',			-- Sync with the library definition...
2123 								p_sync_people     => 'SLIB'				-- Sync with library definition....
2124 								 );
2125 
2126                 	synchronize_hierarchy(p_org_id     =>Org_Ids(indx),
2127 								  p_parent_process_id  =>l_child_process_id,
2128 								  p_sync_attributes    =>p_sync_attributes,
2129 								  p_sync_rcm           =>p_sync_rcm,
2130 								  p_sync_people        =>p_sync_people
2131 								  );
2132                 EXCEPTION
2133                 	WHEN NO_DATA_FOUND THEN
2134                 		undelete(l_child_process_id, Org_Ids(indx));
2135                   		add_delete_ex_child (Org_Ids(indx), p_parent_process_id, l_child_process_id, 'ADD');
2136                   		Synchronize_process(p_org_id      => Org_Ids(indx),
2137 								p_process_id  => l_child_process_id,
2138 								p_sync_mode   => 'PSUBP',               -- Include sub processes also..
2139 								p_sync_hierarchy => 'YES',              -- Sync up the hierarchy also..
2140 								p_sync_attributes => p_sync_attributes,	-- Sync up the attributes also..
2141 								p_sync_rcm        => p_sync_rcm,		-- Sync with the library definition...
2142 								p_sync_people     => p_sync_people		-- Sync with library definition....
2143 								 );
2144 				END;
2145             end if;
2146         end loop;
2147     END LOOP;
2148 end synchronize_hierarchy;
2149 -- ****************************************************************************
2150 
2151 function process_locked(p_process_id in number, p_org_id in number) return boolean is
2152 l_dummy number;
2153 begin
2154     select 1
2155     into l_dummy
2156     from amw_process_locks
2157     where locked_process_id = p_process_id
2158     and organization_id = p_org_id;
2159 
2160     return true;
2161 exception
2162     when no_data_found then
2163         return false;
2164 
2165     when too_many_rows then
2166         return true;
2167 
2168 end process_locked;
2169 
2170 
2171 -- ****************************************************************************
2172 
2173 function process_pending_approval(p_process_id in number, p_org_id in number) return boolean is
2174 l_dummy number;
2175 begin
2176     select 1
2177     into l_dummy
2178     from amw_process_organization
2179     where process_id = p_process_id
2180     and organization_id = p_org_id
2181     and end_date is null
2182     and approval_status = 'PA';
2183 
2184     return true;
2185 exception
2186     when no_data_found then
2187         return false;
2188 end process_pending_approval;
2189 
2190 -- ****************************************************************************
2191 
2192 -- remove the deletion_date. If deletion is approved, revise the process.
2193 procedure undelete (
2194 	p_process_id in number,
2195   	p_org_id in number) is
2196 
2197 begin
2198 
2199 -- KSR CHANGES BEGIN...
2200 
2201 -- Make changes such that the disassociation is also taken care of....
2202 
2203 
2204 -- KSR CHANGES END.....
2205     revise_process_if_necessary (p_org_id, p_process_id);
2206 
2207     update amw_process_organization
2208          set    deletion_date = null,
2209                 object_version_number = object_version_number + 1
2210          where  process_id = p_process_id
2211          and    organization_id = p_org_id
2212          and    end_date is null;
2213 
2214 end undelete;
2215 
2216 -- ****************************************************************************
2217 
2218 function does_process_exist_in_org(p_process_id in number, p_org_id in number) return varchar2 is
2219 l_del_date date := null;
2220 begin
2221 -- check if the latest version is end-dated
2222     select deletion_date
2223     into l_del_date
2224     from amw_process_organization
2225     where process_id = p_process_id
2226     and organization_id = p_org_id
2227     and end_date is null;
2228 
2229     if l_del_date is null then
2230         return 'Y';
2231     else
2232         return 'D';
2233     end if;
2234 
2235 exception
2236     when no_data_found then
2237         return 'N';
2238 
2239 --    when too_many_rows then
2240 --        return 'Y';
2241 
2242 end does_process_exist_in_org;
2243 
2244 -- ****************************************************************************
2245 
2246 procedure find_rl_app_hier_children(p_process_id in number)
2247 is
2248   cursor c1 (l_pid number) is
2249     select ah.child_id child_process_id
2250       from amw_approved_hierarchies ah
2251       where ah.parent_id = (select pp.process_id
2252                             from amw_process pp
2253                             where pp.process_id = ah.parent_id
2254                             and pp.approval_date is not null
2255                             and pp.approval_end_date is null
2256                             and pp.deletion_date is null)
2257        and ah.child_id  =  ( select Cp.process_id
2258                             from amw_process Cp
2259                             where Cp.process_id = ah.child_id
2260                             and Cp.approval_date is not null
2261                             and Cp.approval_end_date is null
2262                             and Cp.deletion_date is null)
2263        and ah.start_date is not null
2264        and ah.end_date is null
2265        and ah.organization_id = -1
2266        and ah.parent_id = l_pid;
2267 
2268   c1_rec c1%rowtype;
2269 
2270 begin
2271   for c1_rec in c1(p_process_id) loop
2272 	  exit when c1%notfound;
2273 --	  child_num := child_num + 1;
2274 --	  v_child_name(child_num) := c1_rec.CHILD_PROCESS_NAME;
2275           find_rl_app_hier_children(p_process_id =>c1_rec.child_process_id);
2276   end loop;
2277 end find_rl_app_hier_children;
2278 
2279 -- ****************************************************************************
2280 
2281 procedure  produce_err_if_pa_or_locked(
2282 	p_org_id in number,
2283 	p_process_id in number) is
2284 
2285 pending_approval_exception exception;
2286 err_msg varchar2(4000);
2287 
2288 begin
2289 
2290  if (process_pending_approval(p_process_id, p_org_id) OR
2291     process_locked(p_process_id, p_org_id)) then
2292                     raise pending_approval_exception;
2293  end if;
2294 
2295 exception
2296 
2297     when pending_approval_exception then
2298 	 rollback;
2299          fnd_message.set_name('AMW','AMW_ATTEMPT_MODIF_LOCKED');
2300          err_msg := fnd_message.get;
2301          fnd_msg_pub.add_exc_msg(p_pkg_name  =>    'amw_org_hierarchy_pkg',
2302                    	     p_procedure_name =>   'produce_err_if_pa_or_locked',
2303   	                     p_error_text => err_msg);
2304          raise;
2305 
2306 end produce_err_if_pa_or_locked;
2307 
2308 
2309 
2310 -- ****************************************************************************
2311 
2312 procedure  produce_err_if_circular(
2313 	p_org_id in number,
2314 	p_parent_process_id in number,
2315     p_child_process_id in number) is
2316 
2317 circular_exception exception;
2318 err_msg varchar2(4000);
2319 
2320 begin
2321 
2322       if is_child_an_ancestor(p_org_id => p_org_id,
2323                               p_parent_process_id => p_parent_process_id,
2324                               p_child_process_id => p_child_process_id) then
2325           raise circular_exception;
2326       end if;
2327 
2328 exception
2329 
2330     when circular_exception then
2331 	 rollback;
2332          fnd_message.set_name('AMW','AMW_ATTEMPT_CIRCULAR');
2333          err_msg := fnd_message.get;
2334          fnd_msg_pub.add_exc_msg(p_pkg_name  =>    'amw_org_hierarchy_pkg',
2335                    	     p_procedure_name =>   'produce_err_if_circular',
2336   	                     p_error_text => err_msg);
2337          raise;
2338 
2339 end produce_err_if_circular;
2340 
2341 
2342 -- ****************************************************************************
2343 
2344 -- to be called recursively from disassociate_process_org
2345 procedure disassociate_process_org_hier (
2346 	p_org_id in number,
2347 	p_process_id in number) is
2348 
2349   cursor c3 (l_pid number, l_org number) is
2350     select child_process_id
2351 	from AMW_LATEST_HIERARCHY_ORG_V
2352     where parent_process_id=l_pid
2353     and child_organization_id = l_org;
2354 
2355   c3_rec c3%rowtype;
2356   l_child_process_id number;
2357   l_dummy number;
2358   l_exists_elsewhere boolean := false;
2359 
2360 begin
2361 
2362     for c3_rec in c3(p_process_id, p_org_id) loop
2363 	  exit when c3%notfound;
2364         l_child_process_id := c3_rec.child_process_id;
2365 
2366      -- Remove the link between the parent process and the child process...
2367         /*delete from amw_latest_hierarchies
2368         where child_id  = l_child_process_id
2369         and parent_id = p_process_id
2370         and organization_id = p_org_id;*/
2371         /*commenting the above delete and adding the following update
2372          so that when a process is dissasociated and approved,the
2373          children will also be approved using the below link*/
2374          update amw_latest_hierarchies
2375           set organization_id = organization_id * (-1),
2376               object_version_number = object_version_number + 1,
2377               LAST_UPDATE_DATE = sysdate,
2378               LAST_UPDATED_BY = g_user_id,
2379               LAST_UPDATE_LOGIN = g_login_id
2380           where child_id  = l_child_process_id
2381           and parent_id = p_process_id
2382           and organization_id = p_org_id;
2383 --remove the rcm links...dpatel
2384 delete_existing_rcm(l_child_process_id,p_org_id);
2385 
2386 -- Now check whether this process is locked.
2387         if not process_locked(l_child_process_id, p_org_id) then
2388           -- So we can update this to be deleted. But need to check whether this is existing as a child of any other process
2389             begin
2390               select 1 into l_dummy
2391               from amw_latest_hierarchies
2392               where child_id  = l_child_process_id
2393               and organization_id = p_org_id
2394               and parent_id <> p_process_id;
2395 
2396               l_exists_elsewhere := true;
2397 
2398             exception
2399               when no_data_found then
2400                   l_exists_elsewhere := false;
2401               when too_many_rows then
2402                   l_exists_elsewhere := true;
2403             end;
2404 
2405           if l_exists_elsewhere = false then
2406            -- So the Process does not exist any where..Delete from latest hierarchy..
2407           --   delete from amw_latest_hierarchies
2408           --   where child_id  = l_child_process_id
2409           --   and parent_id = p_process_id
2410           --   and organization_id = p_org_id;
2411             -- We need to process the child hierarchy now to disassociate --So call disassociate_proc_org_hier on child.
2412             disassociate_process_org_hier(p_org_id, l_child_process_id);
2413             -- So delete the process now..
2414             delete_process(p_org_id, l_child_process_id);
2415           end if;
2416       end if;
2417     end loop;
2418 
2419 end disassociate_process_org_hier;
2420 
2421 -- ****************************************************************************
2422 
2423 procedure disassociate_process_org (
2424 	p_org_id in number,
2425 	p_process_id in number) is
2426 
2427   cursor c2 (l_pid number, l_org number) is
2428     select parent_process_id
2429 	from AMW_LATEST_HIERARCHY_ORG_V
2430     where child_process_id = l_pid
2431     and child_organization_id = l_org;
2432 
2433   c2_rec c2%rowtype;
2434   l_parent_process_id number;
2435 
2436 begin
2437 --ko 1. First Check the process is not in locked state. Because we need to set its deletion date.
2438   produce_err_if_pa_or_locked(p_org_id => p_org_id,p_process_id => p_process_id);
2439 -- ko 1. Also Check the Parents of this process can be revised/updated.. Else Produce Error.
2440 -- obviously you got to revise those parent processes
2441   for c2_rec in c2(p_process_id, p_org_id) loop
2442     	exit when c2%notfound;
2443         l_parent_process_id := c2_rec.parent_process_id;
2444         produce_err_if_pa_or_locked(p_org_id, l_parent_process_id);
2445         revise_process_if_necessary(p_org_id, l_parent_process_id);
2446   end loop;
2447 
2448 --ko 3. So revised all the necessary parents. No go ahead and delete them from the latest hierarchy.
2449 -- delete all the links from latest hierarchy where p_process_id is child
2450         delete from amw_latest_hierarchies
2451         where child_id  = p_process_id
2452         and organization_id = p_org_id;
2453 --remove the rcm links...dpatel
2454 delete_existing_rcm(p_process_id,p_org_id);
2455 
2456 -- koStart 4. Call the following API. I have a doubt regarding the functionality.
2457 -- The functionality of disassociating a process is like the following.
2458 -- 1. Delete the process From All its parents.
2459 --2. For each of the child,
2460 --      Check if the child is not locked and it is not under any other parent. If not so proceed to the next child.
2461 --3. If the child can not be found under any other parent, then you need to disassociate this process.
2462 --4  Just delete the parent child link. Delete the Process. call the disassociate_process_org_hier with the child.
2463 -- koEnd.
2464 -- disassociate the hierarchy
2465         disassociate_process_org_hier(p_org_id, p_process_id);
2466 -- Now we need to delete the links of the process from the root process if there are any..
2467         AMW_PROC_ORG_APPROVAL_PKG.write_approved_hierarchy(-2, 2, p_org_id);
2468 
2469  -- Now we need to set the deletion date of this process...................
2470 
2471 		delete_process(p_org_id, p_process_id);
2472 
2473 end disassociate_process_org;
2474 
2475 -- ****************************************************************************
2476 
2477 procedure upd_ltst_risk_count(p_org_id in number, p_process_id in number) is
2478 
2479 cursor c1 is
2480    (select process_id
2481     from amw_process_organization
2482     where organization_id = p_org_id
2483     and process_id in ( select parent_id
2484                     		from amw_latest_hierarchies
2485                     		start with child_id = p_process_id and organization_id = p_org_id
2486                     		connect by prior parent_id = child_id
2487                     		and organization_id = p_org_id
2488                         union all
2489                         select p_process_id from dual
2490                       )
2491     and end_date is null
2492     );
2493 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2494 /*
2495    (select process_id
2496     from amw_process_organization
2497     where organization_id = p_org_id
2498     and process_id in ( select parent_child_id
2499                         from amw_org_hierarchy_denorm
2500                         where process_id = p_process_id
2501                         and organization_id = p_org_id
2502                         and up_down_ind = 'U'
2503                         and hierarchy_type = 'L'
2504                        )
2505     and end_date is null
2506     union
2507     select p_process_id from dual);
2508 */
2509 cursor c2 is
2510     select process_id
2511     from amw_process_organization
2512     where end_date is null
2513    and organization_id = p_org_id;
2514 
2515 type t_n is table of number;
2516 
2517 x t_n;
2518 
2519 begin
2520 if p_process_id is null then
2521     open c2;
2522     fetch c2 bulk collect into x;
2523     close c2;
2524 else
2525     open c1;
2526     fetch c1 bulk collect into x;
2527     close c1;
2528 end if;
2529 
2530 if x.exists(1) then
2531 forall ctr in x.first .. x.last
2532 update amw_process_organization
2533         set risk_count_latest = (select count(*) from (
2534                             select distinct risk_id from amw_risk_associations
2535                             where pk1 = p_org_id
2536                             and pk2 in (
2537                             						select child_id
2538   																			from amw_latest_hierarchies
2539   																			start with child_id = x(ctr) and organization_id = p_org_id
2540   																			connect by prior child_id = parent_id and organization_id = p_org_id
2541                             						)
2542 /* ko removing the usage of amw_org_hierarchy_denorm
2543 												    ( ( select parent_child_id
2544                             from amw_org_hierarchy_denorm
2545                             where process_id = x(ctr)
2546                             and organization_id = p_org_id
2547                             and up_down_ind = 'D'
2548                             and hierarchy_type = 'L' ) union (select x(ctr) from dual) )
2549 */
2550                             and deletion_date is null
2551                             and object_type = 'PROCESS_ORG'
2552                             ) ),last_update_date = sysdate
2553               ,last_updated_by = G_USER_ID
2554               ,last_update_login = G_LOGIN_ID
2555         where end_date is null
2556         and process_id <> -2
2557         and organization_id = p_org_id
2558         and process_id = x(ctr);
2559 end if;
2560 
2561 exception
2562 
2563 when others
2564   then raise FND_API.G_EXC_UNEXPECTED_ERROR;
2565 
2566 end upd_ltst_risk_count;
2567 
2568 -- ****************************************************************************
2569 
2570 procedure upd_ltst_control_count(p_org_id in number, p_process_id in number) is
2571 
2572 
2573 cursor c1 is
2574 		select process_id
2575     from amw_process_organization
2576     where organization_id = p_org_id
2577     and process_id in ( select parent_id
2578                     		from amw_latest_hierarchies
2579                     		start with child_id = p_process_id and organization_id = p_org_id
2580                     		connect by prior parent_id = child_id
2581                     		and organization_id = p_org_id
2582   		                union all
2583   		                select p_process_id from dual
2584                        )
2585     and end_date is null;
2586 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2587 /*
2588    (select process_id
2589     from amw_process_organization
2590     where organization_id = p_org_id
2591     and process_id in ( select parent_child_id
2592                         from amw_org_hierarchy_denorm
2593                         where process_id = p_process_id
2594                         and organization_id = p_org_id
2595                         and up_down_ind = 'U'
2596                         and hierarchy_type = 'L'
2597                        )
2598     and end_date is null
2599     union
2600     select p_process_id from dual);
2601 */
2602 
2603 cursor c2 is
2604     select process_id
2605     from amw_process_organization
2606     where end_date is null
2607    and organization_id = p_org_id;
2608 
2609 type t_n is table of number;
2610 
2611 x t_n;
2612 
2613 begin
2614 if p_process_id is null then
2615     open c2;
2616     fetch c2 bulk collect into x;
2617     close c2;
2618 else
2619     open c1;
2620     fetch c1 bulk collect into x;
2621     close c1;
2622 end if;
2623 
2624 if x.exists(1) then
2625 forall ctr in x.first .. x.last
2626 update amw_process_organization
2627         set control_count_latest = (select count(*) from (
2628                             select distinct control_id from amw_control_associations
2629                             where pk1 = p_org_id
2630                             and pk2 in ( select child_id
2631   																			from amw_latest_hierarchies
2632   																			start with child_id = x(ctr) and organization_id = p_org_id
2633   																			connect by prior child_id = parent_id and organization_id = p_org_id
2634   																			)
2635 /* ko remove org_denorm
2636                             ( ( select parent_child_id
2637                             from amw_org_hierarchy_denorm
2638                             where process_id = x(ctr)
2639                             and organization_id = p_org_id
2640                             and up_down_ind = 'D'
2641                             and hierarchy_type = 'L' ) union (select x(ctr) from dual)
2642                             )
2643 */
2644                             and deletion_date is null
2645                             and object_type = 'RISK_ORG'
2646                             ) ),last_update_date = sysdate
2647               ,last_updated_by = G_USER_ID
2648               ,last_update_login = G_LOGIN_ID
2649         where end_date is null
2650         and process_id <> -2
2651         and organization_id = p_org_id
2652         and process_id = x(ctr);
2653 end if;
2654 
2655 end upd_ltst_control_count;
2656 
2657 -- ****************************************************************************
2658 
2659 procedure upd_appr_risk_count(p_org_id in number, p_process_id in number) is
2660 
2661 cursor c1 is
2662 		select process_id
2663     from amw_process_organization
2664     where organization_id = p_org_id
2665     and process_id in ( select parent_id
2666                         from amw_approved_hierarchies
2667                         start with child_id = p_process_id and organization_id = p_org_id
2668                         and start_date is not null and end_date is null
2669                         connect by prior parent_id = child_id and organization_id = p_org_id
2670                         and start_date is not null and end_date is null
2671                         union all
2672                         select p_process_id from dual
2673                         )
2674     and end_date is null;
2675 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2676 /*
2677    (select process_id
2678     from amw_process_organization
2679     where organization_id = p_org_id
2680     and process_id in ( select parent_child_id
2681                         from amw_org_hierarchy_denorm
2682                         where process_id = p_process_id
2683                         and organization_id = p_org_id
2684                         and up_down_ind = 'U'
2685                         and hierarchy_type = 'A'
2686                        )
2687     and end_date is null
2688     union
2689     select p_process_id from dual);
2690 */
2691 cursor c2 is
2692     select process_id
2693     from amw_process_organization
2694     where approval_date is not null
2695         and approval_end_date is null
2696         and organization_id = p_org_id;
2697 type t_n is table of number;
2698 
2699 x t_n;
2700 
2701 begin
2702 if p_process_id is null then
2703     open c2;
2704     fetch c2 bulk collect into x;
2705     close c2;
2706 else
2707     open c1;
2708     fetch c1 bulk collect into x;
2709     close c1;
2710 end if;
2711 
2712 if x.exists(1) then
2713 forall ctr in x.first .. x.last
2714 update amw_process_organization
2715         set risk_count = (select count(*) from (
2716                             select distinct risk_id from amw_risk_associations
2717                             where pk1 = p_org_id
2718                             and pk2 in ( select child_id
2719                                          from amw_approved_hierarchies
2720                                          start with child_id = x(ctr) and organization_id = p_org_id
2721                                                 and start_date is not null and end_date is null
2722                                          connect by prior child_id = parent_id and organization_id = p_org_id
2723                                                 and start_date is not null and end_date is null
2724 																				)
2725 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2726 /*
2727                             ( ( select parent_child_id
2728                             from amw_org_hierarchy_denorm
2729                             where process_id = x(ctr)
2730                             and organization_id = p_org_id
2731                             and up_down_ind = 'D'
2732                             and hierarchy_type = 'A' ) union (select x(ctr) from dual)
2733                             )
2734 */
2735                             and approval_date is not null
2736                             and deletion_approval_date is null
2737                             and object_type = 'PROCESS_ORG'
2738                             ) ),last_update_date = sysdate
2739               ,last_updated_by = G_USER_ID
2740               ,last_update_login = G_LOGIN_ID
2741         where approval_date is not null
2742         and approval_end_date is null
2743         and process_id <> -2
2744         and organization_id = p_org_id
2745         and process_id = x(ctr);
2746 end if;
2747 
2748 
2749 
2750 end upd_appr_risk_count;
2751 
2752 -- ****************************************************************************
2753 
2754 procedure upd_appr_control_count(p_org_id in number, p_process_id in number) is
2755 
2756 cursor c1 is
2757 		select process_id
2758     from amw_process_organization
2759     where organization_id = p_org_id
2760     and process_id in ( select parent_id
2761                         from amw_approved_hierarchies
2762                         start with child_id = p_process_id and organization_id = p_org_id
2763                         and start_date is not null and end_date is null
2764                         connect by prior parent_id = child_id and organization_id = p_org_id
2765                         and start_date is not null and end_date is null
2766                         union all
2767                         select p_process_id from dual
2768                         )
2769     and end_date is null;
2770 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2771 /*
2772    (select process_id
2773     from amw_process_organization
2774     where organization_id = p_org_id
2775     and process_id in ( select parent_child_id
2776                         from amw_org_hierarchy_denorm
2777                         where process_id = p_process_id
2778                         and organization_id = p_org_id
2779                         and up_down_ind = 'U'
2780                         and hierarchy_type = 'A'
2781                        )
2782     and end_date is null
2783     union
2784     select p_process_id from dual);
2785 */
2786 cursor c2 is
2787     select process_id
2788     from amw_process_organization
2789     where approval_date is not null
2790         and approval_end_date is null
2791         and organization_id = p_org_id;
2792 
2793 type t_n is table of number;
2794 
2795 x t_n;
2796 
2797 begin
2798 if p_process_id is null then
2799     open c2;
2800     fetch c2 bulk collect into x;
2801     close c2;
2802 else
2803     open c1;
2804     fetch c1 bulk collect into x;
2805     close c1;
2806 end if;
2807 
2808 if x.exists(1) then
2809 forall ctr in x.first .. x.last
2810 update amw_process_organization
2811         set control_count= (select count(*) from (
2812                             select distinct control_id from amw_control_associations
2813                             where pk1 = p_org_id
2814                             and pk2 in ( select child_id
2815                                          from amw_approved_hierarchies
2816                                          start with child_id = x(ctr) and organization_id = p_org_id
2817                                                 and start_date is not null and end_date is null
2818                                          connect by prior child_id = parent_id and organization_id = p_org_id
2819                                                 and start_date is not null and end_date is null
2820 																				)
2821 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
2822 /*
2823                             ( ( select parent_child_id
2824                             from amw_org_hierarchy_denorm
2825                             where process_id = x(ctr)
2826                             and organization_id = p_org_id
2827                             and up_down_ind = 'D'
2828                             and hierarchy_type = 'A' ) union (select x(ctr) from dual)
2829                             )
2830 */
2831                             and approval_date is not null
2832                             and deletion_approval_date is null
2833                             and object_type = 'RISK_ORG'
2834                             ) ),last_update_date = sysdate
2835               ,last_updated_by = G_USER_ID
2836               ,last_update_login = G_LOGIN_ID
2837         where approval_date is not null
2838         and approval_end_date is null
2839         and process_id <> -2
2840         and organization_id = p_org_id
2841         and process_id = x(ctr);
2842 end if;
2843 
2844 
2845 end upd_appr_control_count;
2846 
2847 -- ****************************************************************************
2848 PROCEDURE push_proc_org_srs(
2849     errbuf                  OUT NOCOPY VARCHAR2,
2850     retcode                 OUT NOCOPY VARCHAR2,
2851     p_process_id		    IN number,
2852     p_org_name		        IN varchar2,
2853     p_org_range_from		IN varchar2,
2854     p_org_range_to			IN varchar2,
2855     p_synchronize		    IN varchar2,
2856     p_apply_rcm			    IN varchar2
2857 )
2858 IS
2859 cursor c1 (pid number) is
2860         select parent_child_id process_to_count
2861         from amw_proc_hierarchy_denorm
2862         where process_id = pid
2863         and up_down_ind = 'D'
2864         and hierarchy_type = 'A'
2865         union
2866         select pid process_to_count from dual;
2867 
2868 cursor c2(pid number, orgName varchar2) is
2869     select  aauv.organization_id,
2870     		name
2871     from    amw_audit_units_v aauv
2872     where   NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
2873     and     'Y' = AMW_UTILITY_PVT.IS_ORG_REGISTERED(aauv.ORGANIZATION_ID)
2874     and     aauv.organization_id not in(
2875                 select distinct organization_id
2876                 from amw_process_organization
2877                 where process_id = pid
2878                 and end_date is null
2879                 and (
2880                     deletion_date is null or
2881                     (deletion_date is not null and approval_date is null)
2882                 )
2883             )
2884     and (UPPER(NAME) LIKE UPPER(orgName));
2885 cursor c3(pid number, rangeFrom varchar2, rangeTo varchar2) is
2886     select  aauv.organization_id,
2887     		name
2888     from    amw_audit_units_v aauv
2889     where   NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
2890     and     'Y' = AMW_UTILITY_PVT.IS_ORG_REGISTERED(aauv.ORGANIZATION_ID)
2891     and     aauv.organization_id not in(
2892                 select distinct organization_id
2893                 from amw_process_organization
2894                 where process_id = pid
2895                 and end_date is null
2896                 and (
2897                     deletion_date is null or
2898                     (deletion_date is not null and approval_date is null)
2899                 )
2900             )
2901     and NAME >= rangeFrom and substr(NAME,0,length(rangeTo))<= rangeTo;
2902 
2903 
2904     conc_status             boolean;
2905     p_mode                  varchar2(5) := 'ASSOC';
2906     L_API_NAME CONSTANT     varchar2(30):= 'push_proc_org_srs';
2907     l_return_status	        varchar2(10);
2908     l_msg_data              varchar2(4000);
2909     l_msg_count	            number;
2910     p_parent_orgprocess_id  number := -2 ;
2911 
2912 
2913 cursor c_processes (pid number) is
2914         select parent_child_id process_to_count
2915         from amw_proc_hierarchy_denorm
2916         where process_id = pid
2917         and up_down_ind = 'D'
2918         and hierarchy_type = 'A'
2919         union
2920         select pid process_to_count from dual;
2921 
2922 
2923 type t_audit_unit_rec is record (organization_id  amw_audit_units_v.organization_id%type,
2924                          	   org_name  amw_audit_units_v.name%type);
2925 
2926 type t_audit_units_tbl is table of t_audit_unit_rec;
2927 l_audit_units_tbl t_audit_units_tbl;
2928 
2929 show_warning boolean:= false;
2930 
2931 BEGIN
2932 
2933     IF FND_GLOBAL.User_Id IS NULL THEN
2934         AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
2935         RAISE FND_API.G_EXC_ERROR;
2936     END IF;
2937     if p_process_id = -1 OR p_process_id = -2 then
2938         conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: Cannot Associate Root Process');
2939         return;
2940     end if;
2941     if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
2942     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
2943         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_SRS.Begin','BEGIN');
2944   end if;
2945 	if  p_org_range_from is not null and p_org_range_to is not null then
2946     	open c3(p_process_id, p_org_range_from,p_org_range_to);
2947     	fetch c3 bulk collect into l_audit_units_tbl;
2948     	close c3;
2949     elsif p_org_name is null then
2950     	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: No Organization filter found to proceed');
2951     	return;
2952     else
2953     	open c2(p_process_id, p_org_name || '%');
2954     	fetch c2 bulk collect into l_audit_units_tbl;
2955     	close c2;
2956 	end if;
2957 	if l_audit_units_tbl.exists(1)  then
2958     FOR orgid IN l_audit_units_tbl.first .. l_audit_units_tbl.last loop
2959 --    	fnd_file.put_line(fnd_file.LOG, 'Associating to ' || l_audit_units_tbl(orgid).org_name  );
2960      	push_proc_per_org(
2961             p_parent_orgprocess_id	=> p_parent_orgprocess_id,
2962 			p_process_id		=> p_process_id,
2963 			p_org_id			=> l_audit_units_tbl(orgid).organization_id,
2964 			p_mode				=> p_mode,
2965 			p_apply_rcm			=> p_apply_rcm,
2966 			p_synchronize		=> p_synchronize,
2967 			p_update_count		=> FND_API.G_FALSE,
2968 			p_commit            => FND_API.G_FALSE,
2969 			x_return_status		=> l_return_status,
2970 			x_msg_count			=> l_msg_count,
2971 			x_msg_data			=> l_msg_data);
2972 
2973            	IF l_return_status <> 'S' THEN
2974            	  show_warning := true;
2975 		      fnd_file.put_line(fnd_file.LOG, 'Error when Associating the process to ' || l_audit_units_tbl(orgid).org_name  );
2976 		      fnd_file.put_line(fnd_file.LOG, l_msg_data );
2977             ELSE
2978             -- If user wants the processes to be associated as approved, user should set this profile to Y.
2979             -- Note that to get the whole hierarchy approved, user needs to set the approval option to
2980             -- "approve everything down below". Else only the process id passed will be approved.
2981             -- Although this is not very user friendly, I cannot see an option, as I cannot change the
2982             -- approval option ad hoc for this association process. When user associates a process,
2983             -- may be some-subprocess down below is in pending approval status, and that should prevent
2984             -- modifying the approval option.
2985             -- Also note that the "Approval Required" parameter for the org will be overridden, if set to yes.
2986 	       	IF fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' THEN
2987 	       		BEGIN
2988                 	AMW_PROC_ORG_APPROVAL_PKG.sub_for_approval(p_process_id, l_audit_units_tbl(orgid).organization_id);
2989                 	AMW_PROC_ORG_APPROVAL_PKG.approve(p_process_id, l_audit_units_tbl(orgid).organization_id,FND_API.G_FALSE);
2990                 EXCEPTION
2991                 	WHEN OTHERS THEN
2992                 		show_warning := true;
2993                 		ROLLBACK;
2994                 		-- Unapproved object associations exists exception may happen..catche them here..
2995                 		FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
2996                 		fnd_file.put_line(fnd_file.LOG, ' Error when Approving the process in organization ' ||l_audit_units_tbl(orgid).org_name  );
2997                 		fnd_file.put_line(fnd_file.LOG, l_msg_data);
2998                 END;
2999 	       	END IF;
3000 	       END IF;
3001 	       -- Done associating...Commit here..
3002 	       COMMIT;
3003  	END LOOP;
3004  	if show_warning then
3005  	  conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Process cannot be associated to some organizations');
3006     end if;
3007 
3008 -- update the org count..
3009 	update amw_process AP
3010   	set AP.org_count = (select count(o.organization_id)
3011     					from hr_all_organization_units o,
3012               			hr_organization_information o2
3013         					WHERE o.organization_id = o2.organization_id
3014          					 and o2.org_information_context = 'CLASS'
3015          					 and o2.org_information1 = 'AMW_AUDIT_UNIT'
3016          					 and o2.org_information2 = 'Y'
3017                              and exists (select 1
3018                             			from amw_process_organization APO
3019                             			WHERE APO.organization_id = o.ORGANIZATION_ID
3020                             			AND  APO.process_id = AP.PROCESS_ID
3021                             			and APO.end_date is null
3022                             			and (APO.deletion_date is null or (APO.deletion_date is not null and APO.approval_date is null)))),
3023          AP.object_version_number = AP.object_version_number + 1
3024         ,AP.last_update_date = sysdate
3025         ,AP.last_updated_by = G_USER_ID
3026         ,AP.last_update_login = G_LOGIN_ID
3027    	where AP.approval_date is not null
3028     and AP.approval_end_date is null
3029     and AP.process_id <> -1
3030     and AP.process_id   IN (select APHD.parent_child_id process_to_count
3031                               from amw_proc_hierarchy_denorm APHD
3032                               where APHD.process_id = p_process_id
3033                               and APHD.up_down_ind = 'D'
3034                               and APHD.hierarchy_type = 'A'
3035                               union
3036                               select p_process_id process_to_count from dual);
3037 
3038 	if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3039     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3040         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_SRS.Update_ORG_Counts','UPDATED');
3041   	end if;
3042 -- commit here..
3043 	COMMIT;
3044 
3045 
3046  	if p_org_range_from is not null and p_org_range_to is not null then
3047 
3048  		update amw_process_organization APO
3049         set APO.risk_count_latest = (
3050                             select count(distinct ARA.risk_id) from amw_risk_associations ARA
3051                             where ARA.pk1 = APO.ORGANIZATION_ID
3052                             and ARA.pk2 in ( select alh.child_id
3053 																				     from amw_latest_hierarchies alh
3054 																				     start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3055 																				     connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3056 																				    )
3057 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3058 /*
3059                             ( ( select AOH.parent_child_id
3060                             from amw_org_hierarchy_denorm AOH
3061                             where AOH.process_id = APO.PROCESS_ID
3062                             and AOH.organization_id = APO.ORGANIZATION_ID
3063                             and AOH.up_down_ind = 'D'
3064                             and AOH.hierarchy_type = 'L' ) union all (select APO.PROCESS_ID from dual)
3065                             )
3066 */
3067                             and ARA.deletion_date is null
3068                             and ARA.object_type = 'PROCESS_ORG'
3069                             ),
3070         APO.control_count_latest = (
3071                             select count(distinct ACA.CONTROL_ID) from amw_control_associations ACA
3072                             where ACA.pk1 = APO.ORGANIZATION_ID
3073                             and ACA.pk2 in ( select alh.child_id
3074 																				     from amw_latest_hierarchies alh
3075 																				     start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3076 																				     connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3077 																				    )
3078 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3079 /*													( ( select AOH.parent_child_id
3080                             from amw_org_hierarchy_denorm AOH
3081                             where AOH.process_id = APO.PROCESS_ID
3082                             and AOH.organization_id = APO.ORGANIZATION_ID
3083                             and AOH.up_down_ind = 'D'
3084                             and AOH.hierarchy_type = 'L' ) union all (select APO.PROCESS_ID from dual)
3085                             )
3086 */
3087                             and ACA.deletion_date is null
3088                             and ACA.object_type = 'RISK_ORG'
3089                             )
3090               ,APO.last_update_date = sysdate
3091               ,APO.last_updated_by = G_USER_ID
3092               ,APO.last_update_login = G_LOGIN_ID
3093         where APO.end_date is null
3094         and APO.process_id <> -2
3095         and APO.organization_id in (  select  aauv.organization_id
3096     								  from    amw_audit_units_v aauv
3097     								  where NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
3098     								  and  NAME >= p_org_range_from and substr(NAME,0,length(p_org_range_to))<= p_org_range_to);
3099     else
3100 
3101     	update amw_process_organization APO
3102         set APO.risk_count_latest = (
3103                             select count(distinct ARA.risk_id) from amw_risk_associations ARA
3104                             where ARA.pk1 = APO.ORGANIZATION_ID
3105                             and ARA.pk2 in ( select alh.child_id
3106 																				     from amw_latest_hierarchies alh
3107 																				     start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3108 																				     connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3109 																				    )
3110 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3111 /*
3112                             ( ( select AOH.parent_child_id
3113                             from amw_org_hierarchy_denorm AOH
3114                             where AOH.process_id = APO.PROCESS_ID
3115                             and AOH.organization_id = APO.ORGANIZATION_ID
3116                             and AOH.up_down_ind = 'D'
3117                             and AOH.hierarchy_type = 'L' ) union all (select APO.PROCESS_ID from dual)
3118                             )
3119 */
3120                             and ARA.deletion_date is null
3121                             and ARA.object_type = 'PROCESS_ORG'
3122                             ),
3123         APO.control_count_latest = (
3124                             select count(distinct ACA.CONTROL_ID) from amw_control_associations ACA
3125                             where ACA.pk1 = APO.ORGANIZATION_ID
3126                             and ACA.pk2 in ( select alh.child_id
3127 																				     from amw_latest_hierarchies alh
3128 																				     start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3129 																				     connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3130 																				    )
3131 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3132 /*
3133                             ( ( select AOH.parent_child_id
3134                             from amw_org_hierarchy_denorm AOH
3135                             where AOH.process_id = APO.PROCESS_ID
3136                             and AOH.organization_id = APO.ORGANIZATION_ID
3137                             and AOH.up_down_ind = 'D'
3138                             and AOH.hierarchy_type = 'L' ) union all (select APO.PROCESS_ID from dual)
3139                             )
3140 */
3141                             and ACA.deletion_date is null
3142                             and ACA.object_type = 'RISK_ORG'
3143                             )
3144               ,APO.last_update_date = sysdate
3145               ,APO.last_updated_by = G_USER_ID
3146               ,APO.last_update_login = G_LOGIN_ID
3147         where APO.end_date is null
3148         and APO.process_id <> -2
3149         and APO.organization_id in (  select  aauv.organization_id
3150     								  from    amw_audit_units_v aauv
3151     								  where NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
3152     								  and (UPPER(NAME) LIKE UPPER(p_org_name || '%')));
3153 
3154 	end if;
3155 	if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3156     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3157         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_SRS.Update_Latest_Counts','UPDATED');
3158   	end if;
3159 	-- do a COMMIT;
3160 	COMMIT;
3161 
3162 	-- Update the latest risk and control counts
3163 
3164         if fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' then
3165 
3166           if p_org_range_from is not null and p_org_range_to is not null then
3167 
3168 	   		update amw_process_organization APO
3169         	set APO.risk_count = (
3170                             select count(distinct ARA.risk_id) from amw_risk_associations ARA
3171                             where ARA.pk1 = APO.ORGANIZATION_ID
3172                             and ARA.pk2 in (  select alh.child_id
3173 											  											from amw_approved_hierarchies  alh
3174 											  											start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3175 											     															and alh.start_date is not null and alh.end_date is null
3176 								              								connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3177 								                												and alh.start_date is not null and alh.end_date is null
3178 								           									)
3179 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3180 /*
3181                             ( ( select AOH.parent_child_id
3182                             from amw_org_hierarchy_denorm AOH
3183                             where AOH.process_id = APO.PROCESS_ID
3184                             and AOH.organization_id = APO.ORGANIZATION_ID
3185                             and AOH.up_down_ind = 'D'
3186                             and AOH.hierarchy_type = 'A' ) union all (select APO.PROCESS_ID from dual)
3187                             )
3188 */
3189                             and ARA.approval_date is not null
3190                             and ARA.deletion_approval_date is null
3191                             and ARA.object_type = 'PROCESS_ORG'
3192                             ),
3193         	APO.control_count = (
3194                             select count(distinct ACA.CONTROL_ID) from amw_control_associations ACA
3195                             where ACA.pk1 = APO.ORGANIZATION_ID
3196                             and ACA.pk2 in (  select alh.child_id
3197 											  											from amw_approved_hierarchies  alh
3198 											  											start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3199 											     															and alh.start_date is not null and alh.end_date is null
3200 								              								connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3201 								                												and alh.start_date is not null and alh.end_date is null
3202 								           									)
3203 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3204 /*
3205                             ( ( select AOH.parent_child_id
3206                             from amw_org_hierarchy_denorm AOH
3207                             where AOH.process_id = APO.PROCESS_ID
3208                             and AOH.organization_id = APO.ORGANIZATION_ID
3209                             and AOH.up_down_ind = 'D'
3210                             and AOH.hierarchy_type = 'A' ) union all (select APO.PROCESS_ID from dual)
3211                             )
3212 */
3213                             and ACA.approval_date is not null
3214                             and ACA.deletion_approval_date is null
3215                             and ACA.object_type = 'RISK_ORG'
3216                             )
3217               ,APO.last_update_date = sysdate
3218               ,APO.last_updated_by = G_USER_ID
3219               ,APO.last_update_login = G_LOGIN_ID
3220            where APO.approval_date is not null
3221            and APO.approval_end_date is null
3222            and APO.process_id <> -2
3223            and APO.organization_id in (  select  aauv.organization_id
3224     								  from    amw_audit_units_v aauv
3225     								  where NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
3226     								  and  NAME >= p_org_range_from and substr(NAME,0,length(p_org_range_to))<= p_org_range_to);
3227         else
3228 
3229     		update amw_process_organization APO
3230         	set APO.risk_count = (
3231                             select count(distinct ARA.risk_id) from amw_risk_associations ARA
3232                             where ARA.pk1 = APO.ORGANIZATION_ID
3233                             and ARA.pk2 in (  select alh.child_id
3234 											  											from amw_approved_hierarchies  alh
3235 											  											start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3236 											     															and alh.start_date is not null and alh.end_date is null
3237 								              								connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3238 								                												and alh.start_date is not null and alh.end_date is null
3239 								           									)
3240 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3241 /*
3242                             ( ( select AOH.parent_child_id
3243                             from amw_org_hierarchy_denorm AOH
3244                             where AOH.process_id = APO.PROCESS_ID
3245                             and AOH.organization_id = APO.ORGANIZATION_ID
3246                             and AOH.up_down_ind = 'D'
3247                             and AOH.hierarchy_type = 'A' ) union all (select APO.PROCESS_ID from dual)
3248                             )
3249 */
3250                             and ARA.approval_date is not null
3251                             and ARA.deletion_approval_date is null
3252                             and ARA.object_type = 'PROCESS_ORG'
3253                             ),
3254         	APO.control_count = (
3255                             select count(distinct ACA.CONTROL_ID) from amw_control_associations ACA
3256                             where ACA.pk1 = APO.ORGANIZATION_ID
3257                             and ACA.pk2 in (  select alh.child_id
3258 											  											from amw_approved_hierarchies  alh
3259 											  											start with alh.child_id = APO.PROCESS_ID and alh.organization_id = APO.ORGANIZATION_ID
3260 											     															and alh.start_date is not null and alh.end_date is null
3261 								              								connect by prior alh.child_id = alh.parent_id and alh.organization_id = APO.ORGANIZATION_ID
3262 								                												and alh.start_date is not null and alh.end_date is null
3263 								           									)
3264 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3265 /*
3266                             ( ( select AOH.parent_child_id
3267                             from amw_org_hierarchy_denorm AOH
3268                             where AOH.process_id = APO.PROCESS_ID
3269                             and AOH.organization_id = APO.ORGANIZATION_ID
3270                             and AOH.up_down_ind = 'D'
3271                             and AOH.hierarchy_type = 'A' ) union all (select APO.PROCESS_ID from dual)
3272                             )
3273 */
3274                             and ACA.approval_date is not null
3275                             and ACA.deletion_approval_date is null
3276                             and ACA.object_type = 'RISK_ORG'
3277                             )
3278               ,APO.last_update_date = sysdate
3279               ,APO.last_updated_by = G_USER_ID
3280               ,APO.last_update_login = G_LOGIN_ID
3281            where APO.approval_date is not null
3282            and APO.approval_end_date is null
3283            and APO.process_id <> -2
3284            and APO.organization_id in (  select  aauv.organization_id
3285     								  from    amw_audit_units_v aauv
3286     								  where NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
3287     								  and (UPPER(NAME) LIKE UPPER(p_org_name || '%')));
3288 	     end if;
3289 
3290 	    if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3291     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3292         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_SRS.Update_Approved_Counts','UPDATED');
3293   		end if;
3294       end if;
3295   end if;
3296 
3297 	COMMIT;
3298   if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3299     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3300         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_SRS.End','END');
3301   end if;
3302 EXCEPTION
3303 
3304   WHEN FND_API.G_EXC_ERROR THEN
3305     ROLLBACK;
3306     retcode := 2;
3307 	errbuf  := SUBSTR(SQLERRM,1,1000);
3308 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
3309 
3310   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3311     ROLLBACK;
3312 	retcode := 2;
3313 	errbuf  := SUBSTR(SQLERRM,1,1000);
3314 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
3315 
3316   WHEN OTHERS THEN
3317     ROLLBACK;
3318 	retcode := 2;
3319 	errbuf  := SUBSTR(SQLERRM,1,1000);
3320 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
3321 END push_proc_org_srs;
3322 
3323 
3324 -- ****************************************************************************
3325 
3326 --psomanat : assocciate to org concurrent program proceedure
3327 PROCEDURE push_proc_org_conc_request(
3328     errbuf                  OUT NOCOPY VARCHAR2,
3329     retcode                 OUT NOCOPY VARCHAR2,
3330     p_parent_orgprocess_id	IN varchar2,
3331     p_process_id		    IN varchar2,
3332     p_mode			        IN varchar2,
3333     p_apply_rcm			    IN varchar2,
3334     p_synchronize		    IN varchar2,
3335     p_org_id_count		    IN varchar2,
3336     p_org_id_string1		IN varchar2 := NULL,
3337     p_org_id_string2		IN varchar2 := NULL,
3338     p_org_id_string3		IN varchar2 := NULL,
3339     p_org_id_string4		IN varchar2 := NULL,
3340     p_org_id_string5		IN varchar2 := NULL,
3341     p_org_id_string6		IN varchar2 := NULL,
3342     p_org_id_string7		IN varchar2 := NULL,
3343     p_org_id_string8		IN varchar2 := NULL,
3344     p_org_id_string9		IN varchar2 := NULL,
3345     p_org_id_string10		IN varchar2 := NULL,
3346     p_org_id_string11		IN varchar2 := NULL,
3347     p_org_id_string12		IN varchar2 := NULL,
3348     p_org_id_string13		IN varchar2 := NULL,
3349     p_org_id_string14		IN varchar2 := NULL,
3350     p_org_id_string15		IN varchar2 := NULL,
3351     p_org_id_string16		IN varchar2 := NULL,
3352     p_org_id_string17		IN varchar2 := NULL,
3353     p_org_id_string18		IN varchar2 := NULL,
3354     p_org_id_string19		IN varchar2 := NULL,
3355     p_org_id_string20		IN varchar2 := NULL,
3356     p_org_id_string21		IN varchar2 := NULL,
3357     p_org_id_string22		IN varchar2 := NULL,
3358     p_org_id_string23		IN varchar2 := NULL,
3359     p_org_id_string24		IN varchar2 := NULL,
3360     p_org_id_string25		IN varchar2 := NULL,
3361     p_org_id_string26		IN varchar2 := NULL,
3362     p_org_id_string27		IN varchar2 := NULL,
3363     p_org_id_string28		IN varchar2 := NULL,
3364     p_org_id_string29		IN varchar2 := NULL,
3365     p_org_id_string30		IN varchar2 := NULL,
3366     p_org_id_string31		IN varchar2 := NULL,
3367     p_org_id_string32		IN varchar2 := NULL,
3368     p_org_id_string33		IN varchar2 := NULL,
3369     p_org_id_string34		IN varchar2 := NULL,
3370     p_org_id_string35		IN varchar2 := NULL,
3371     p_org_id_string36		IN varchar2 := NULL,
3372     p_org_id_string37		IN varchar2 := NULL,
3373     p_org_id_string38		IN varchar2 := NULL,
3374     p_org_id_string39		IN varchar2 := NULL,
3375     p_org_id_string40		IN varchar2 := NULL,
3376     p_org_id_string41		IN varchar2 := NULL,
3377     p_org_id_string42		IN varchar2 := NULL,
3378     p_org_id_string43		IN varchar2 := NULL,
3379     p_org_id_string44		IN varchar2 := NULL,
3380     p_org_id_string45		IN varchar2 := NULL,
3381     p_org_id_string46		IN varchar2 := NULL,
3382     p_org_id_string47		IN varchar2 := NULL,
3383     p_org_id_string48		IN varchar2 := NULL,
3384     p_org_id_string49		IN varchar2 := NULL,
3385     p_org_id_string50		IN varchar2 := NULL,
3386     p_org_id_string51		IN varchar2 := NULL,
3387     p_org_id_string52		IN varchar2 := NULL,
3388     p_org_id_string53		IN varchar2 := NULL,
3389     p_org_id_string54		IN varchar2 := NULL,
3390     p_org_id_string55		IN varchar2 := NULL,
3391     p_org_id_string56		IN varchar2 := NULL,
3392     p_org_id_string57		IN varchar2 := NULL,
3393     p_org_id_string58		IN varchar2 := NULL,
3394     p_org_id_string59		IN varchar2 := NULL,
3395     p_org_id_string60		IN varchar2 := NULL,
3396     p_org_id_string61		IN varchar2 := NULL,
3397     p_org_id_string62		IN varchar2 := NULL,
3398     p_org_id_string63		IN varchar2 := NULL,
3399     p_org_id_string64		IN varchar2 := NULL,
3400     p_org_id_string65		IN varchar2 := NULL,
3401     p_org_id_string66		IN varchar2 := NULL,
3402     p_org_id_string67		IN varchar2 := NULL,
3403     p_org_id_string68		IN varchar2 := NULL,
3404     p_org_id_string69		IN varchar2 := NULL,
3405     p_org_id_string70		IN varchar2 := NULL,
3406     p_org_id_string71		IN varchar2 := NULL,
3407     p_org_id_string72		IN varchar2 := NULL,
3408     p_org_id_string73		IN varchar2 := NULL,
3409     p_org_id_string74		IN varchar2 := NULL,
3410     p_org_id_string75		IN varchar2 := NULL,
3411     p_org_id_string76		IN varchar2 := NULL,
3412     p_org_id_string77		IN varchar2 := NULL,
3413     p_org_id_string78		IN varchar2 := NULL,
3414     p_org_id_string79		IN varchar2 := NULL,
3415     p_org_id_string80		IN varchar2 := NULL,
3416     p_org_id_string81		IN varchar2 := NULL,
3417     p_org_id_string82		IN varchar2 := NULL,
3418     p_org_id_string83		IN varchar2 := NULL,
3419     p_org_id_string84		IN varchar2 := NULL,
3420     p_org_id_string85		IN varchar2 := NULL,
3421     p_org_id_string86		IN varchar2 := NULL,
3422     p_org_id_string87		IN varchar2 := NULL,
3423     p_org_id_string88		IN varchar2 := NULL,
3424     p_org_id_string89		IN varchar2 := NULL,
3425     p_org_id_string90		IN varchar2 := NULL,
3426     p_org_id_string91		IN varchar2 := NULL,
3427     p_org_id_string92		IN varchar2 := NULL
3428 )
3429 IS
3430 TYPE VARCHAR_TABLETYPE IS TABLE OF VARCHAR2(32000);
3431 p_org_ids     VARCHAR_TABLETYPE;
3432 
3433 conc_status     boolean;
3434 l_return_status	varchar2(1);
3435 l_msg_count	    number;
3436 l_msg_data	    varchar2(4000);
3437 
3438 cursor c_processes (pid number) is
3439         select parent_child_id process_to_count
3440         from amw_proc_hierarchy_denorm
3441         where process_id = pid
3442         and up_down_ind = 'D'
3443         and hierarchy_type = 'A'
3444         union
3445         select pid process_to_count from dual;
3446 
3447 type t_tn is table of number;
3448 
3449 x_ptbl t_tn;
3450 l_org_string varchar2(32000) := null;
3451 l_sql_string varchar2(32000);
3452 
3453 TYPE t_proc_cur_type IS REF CURSOR;
3454 l_proc_cur    t_proc_cur_type;
3455 
3456 TYPE t_org_cur IS REF CURSOR;
3457 l_org_cur t_org_cur;
3458 
3459 type t_audit_unit_rec is record (organization_id  amw_audit_units_v.organization_id%type,
3460                          	   org_name  amw_audit_units_v.name%type);
3461 
3462 type t_audit_units_tbl is table of t_audit_unit_rec;
3463 l_audit_units_tbl t_audit_units_tbl;
3464 
3465 
3466 l_org_id NUMBER;
3467 
3468 type t_org_proc_rec is record (organization_id  amw_process_organization.organization_id%type,
3469                          		process_id amw_process_organization.process_id%type);
3470 
3471 type t_org_proc_tbl is table of t_org_proc_rec;
3472 l_org_proc_tbl t_org_proc_tbl;
3473 
3474 
3475 l_orgs_tbl t_tn;
3476 l_procs_tbl t_tn;
3477 show_warning boolean := false;
3478 
3479 
3480 BEGIN
3481 	IF to_number(p_process_id) = -1 OR to_number(p_process_id) = -2 then
3482 		conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: Cannot Associate Root Process');
3483         return;
3484 	end if;
3485 	retcode     := 0;
3486 	errbuf      := '';
3487     conc_status := TRUE;
3488 	if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3489     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3490     'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_CONC_REQUEST.Begin','BEGIN');
3491     end if;
3492     p_org_ids:=VARCHAR_TABLETYPE(
3493                p_org_id_string1,p_org_id_string2,p_org_id_string3,p_org_id_string4,
3494                p_org_id_string5,p_org_id_string6,p_org_id_string7,p_org_id_string8,
3495                p_org_id_string9,p_org_id_string10,p_org_id_string11,p_org_id_string12,
3496                p_org_id_string13,p_org_id_string14,p_org_id_string15,p_org_id_string16,
3497                p_org_id_string17,p_org_id_string18,p_org_id_string19,p_org_id_string20,
3498                p_org_id_string21,p_org_id_string22,p_org_id_string23,p_org_id_string24,
3499                p_org_id_string25,p_org_id_string26,p_org_id_string27,p_org_id_string28,
3500                p_org_id_string29,p_org_id_string30,p_org_id_string31,p_org_id_string32,
3501                p_org_id_string33,p_org_id_string34,p_org_id_string35,p_org_id_string36,
3502                p_org_id_string37,p_org_id_string38,p_org_id_string39,p_org_id_string40,
3503                p_org_id_string41,p_org_id_string42,p_org_id_string43,p_org_id_string44,
3504                p_org_id_string45,p_org_id_string46,p_org_id_string47,p_org_id_string48,
3505                p_org_id_string49,p_org_id_string50,p_org_id_string51,p_org_id_string52,
3506                p_org_id_string53,p_org_id_string54,p_org_id_string55,p_org_id_string56,
3507                p_org_id_string57,p_org_id_string58,p_org_id_string59,p_org_id_string60,
3508                p_org_id_string61,p_org_id_string62,p_org_id_string63,p_org_id_string64,
3509                p_org_id_string65,p_org_id_string66,p_org_id_string67,p_org_id_string68,
3510                p_org_id_string69,p_org_id_string70,p_org_id_string71,p_org_id_string72,
3511                p_org_id_string73,p_org_id_string74,p_org_id_string75,p_org_id_string76,
3512                p_org_id_string77,p_org_id_string78,p_org_id_string79,p_org_id_string80,
3513                p_org_id_string81,p_org_id_string82,p_org_id_string83,p_org_id_string84,
3514                p_org_id_string85,p_org_id_string86,p_org_id_string87,p_org_id_string88,
3515                p_org_id_string89,p_org_id_string90,p_org_id_string91,p_org_id_string92);
3516 
3517     FOR k IN 1..TO_NUMBER(p_org_id_count) LOOP
3518     	l_org_string := l_org_string || p_org_ids(k);
3519 	END LOOP;
3520 	l_sql_string  := 'select organization_id, name  from amw_audit_units_v where  organization_id in ( ' ||
3521 								   replace(rtrim(l_org_string,'x'),'x',',') || ')';
3522 	open l_org_cur for l_sql_string;
3523 	fetch l_org_cur bulk collect into l_audit_units_tbl;
3524 	close l_org_cur;
3525 	if l_audit_units_tbl.exists(1)  then
3526     FOR orgid IN l_audit_units_tbl.first .. l_audit_units_tbl.last loop
3527 --    	fnd_file.put_line(fnd_file.LOG, 'Associating to ' || l_audit_units_tbl(orgid).org_name  );
3528      	push_proc_per_org(
3529             p_parent_orgprocess_id	=> p_parent_orgprocess_id,
3530 			p_process_id		=> p_process_id,
3531 			p_org_id			=> l_audit_units_tbl(orgid).organization_id,
3532 			p_mode				=> p_mode,
3533 			p_apply_rcm			=> p_apply_rcm,
3534 			p_synchronize		=> p_synchronize,
3535 			p_update_count		=> FND_API.G_FALSE,
3536 			p_commit            => FND_API.G_FALSE,
3537 			x_return_status		=> l_return_status,
3538 			x_msg_count			=> l_msg_count,
3539 			x_msg_data			=> l_msg_data);
3540 
3541            	IF l_return_status <> 'S' THEN
3542            	  show_warning := true;
3543 		      fnd_file.put_line(fnd_file.LOG, 'Error when Associating the process to ' || l_audit_units_tbl(orgid).org_name  );
3544 		      fnd_file.put_line(fnd_file.LOG, l_msg_data );
3545             ELSE
3546             -- If user wants the processes to be associated as approved, user should set this profile to Y.
3547             -- Note that to get the whole hierarchy approved, user needs to set the approval option to
3548             -- "approve everything down below". Else only the process id passed will be approved.
3549             -- Although this is not very user friendly, I cannot see an option, as I cannot change the
3550             -- approval option ad hoc for this association process. When user associates a process,
3551             -- may be some-subprocess down below is in pending approval status, and that should prevent
3552             -- modifying the approval option.
3553             -- Also note that the "Approval Required" parameter for the org will be overridden, if set to yes.
3554 	       	IF fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' THEN
3555 	       		BEGIN
3556                 	AMW_PROC_ORG_APPROVAL_PKG.sub_for_approval(p_process_id, l_audit_units_tbl(orgid).organization_id);
3557                 	AMW_PROC_ORG_APPROVAL_PKG.approve(p_process_id, l_audit_units_tbl(orgid).organization_id,FND_API.G_FALSE);
3558                 EXCEPTION
3559                 	WHEN OTHERS THEN
3560                 		show_warning := true;
3561                 		ROLLBACK;
3562                 		-- Unapproved object associations exists exception may happen..catche them here..
3563                 		FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
3564                 		fnd_file.put_line(fnd_file.LOG, ' Error when Approving the process in organization ' ||l_audit_units_tbl(orgid).org_name  );
3565                 		fnd_file.put_line(fnd_file.LOG, l_msg_data);
3566                 END;
3567 	       	END IF;
3568 	       END IF;
3569 	       -- Done associating...Commit here..
3570 	       COMMIT;
3571  	END LOOP;
3572  	if show_warning then
3573  	  conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Process cannot be associated to some organizations');
3574     end if;
3575 
3576     /* This block is to update the latest and approved risk control counts..*/
3577     l_sql_string  := 'select organization_id, process_id  from amw_process_organization where revision_number = 1' ||
3578     							  ' and process_id <> -2  and organization_id in ( ' ||
3579 								   replace(rtrim(l_org_string,'x'),'x',',') || ')';
3580 	open l_proc_cur for l_sql_string;
3581 	fetch l_proc_cur bulk collect into l_org_proc_tbl;
3582 	close l_proc_cur;
3583 
3584 	-- Update the latest risk and control counts
3585 	if l_org_proc_tbl.exists(1) then
3586 		l_orgs_tbl :=  t_tn();
3587 		l_procs_tbl := t_tn();
3588 		for ctr in l_org_proc_tbl.first .. l_org_proc_tbl.last loop
3589 			l_orgs_tbl.extend();
3590 			l_procs_tbl.extend();
3591 			l_orgs_tbl(l_orgs_tbl.last) := l_org_proc_tbl(ctr).organization_id;
3592 			l_procs_tbl(l_procs_tbl.last) := l_org_proc_tbl(ctr).process_id;
3593 		end loop;
3594 
3595 		forall ctr in l_orgs_tbl.first .. l_orgs_tbl.last
3596 		update amw_process_organization
3597         set risk_count_latest = (select count(*) from (
3598                             select distinct risk_id from amw_risk_associations
3599                             where pk1 = l_orgs_tbl(ctr)
3600                             and pk2 in (select alh.child_id
3601 					from amw_latest_hierarchies alh
3602 					start with alh.child_id = l_procs_tbl(ctr) and alh.organization_id = l_orgs_tbl(ctr)
3603 					connect by prior alh.child_id = alh.parent_id and alh.organization_id = l_orgs_tbl(ctr)
3604 					)
3605 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3606 /*
3607                             ( ( select parent_child_id
3608                             from amw_org_hierarchy_denorm
3609                             where process_id = l_procs_tbl(ctr)
3610                             and organization_id = l_orgs_tbl(ctr)
3611                             and up_down_ind = 'D'
3612                             and hierarchy_type = 'L' ) union (select l_procs_tbl(ctr) from dual)
3613                             )
3614 */
3615                             and deletion_date is null
3616                             and object_type = 'PROCESS_ORG'
3617                             ) ),
3618             control_count_latest = (select count(*) from (
3619                             select distinct control_id from amw_control_associations
3620                             where pk1 = l_orgs_tbl(ctr)
3621                             and pk2 in (select alh.child_id
3622 					from amw_latest_hierarchies alh
3623 					start with alh.child_id = l_procs_tbl(ctr) and alh.organization_id = l_orgs_tbl(ctr)
3624 					connect by prior alh.child_id = alh.parent_id and alh.organization_id = l_orgs_tbl(ctr)
3625 					)
3626 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3627 /*
3628 														( ( select parent_child_id
3629                             from amw_org_hierarchy_denorm
3630                             where process_id = l_procs_tbl(ctr)
3631                             and organization_id = l_orgs_tbl(ctr)
3632                             and up_down_ind = 'D'
3633                             and hierarchy_type = 'L' ) union (select l_procs_tbl(ctr) from dual)
3634                             )
3635 */
3636                             and deletion_date is null
3637                             and object_type = 'RISK_ORG'
3638                             ) )
3639               ,last_update_date = sysdate
3640               ,last_updated_by = G_USER_ID
3641               ,last_update_login = G_LOGIN_ID
3642         where end_date is null
3643         and process_id <> -2
3644         and organization_id = l_orgs_tbl(ctr)
3645         and process_id = l_procs_tbl(ctr);
3646 
3647 
3648         if fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' then
3649 
3650 
3651         forall ctr in l_orgs_tbl.first .. l_orgs_tbl.last
3652         update amw_process_organization
3653         set risk_count = (select count(*) from (
3654                             select distinct risk_id from amw_risk_associations
3655                             where pk1 = l_orgs_tbl(ctr)
3656                             and pk2 in (  select alh.child_id
3657                                           from  amw_approved_hierarchies alh
3658 																					start with alh.child_id = l_procs_tbl(ctr) and alh.organization_id = l_orgs_tbl(ctr)
3659                                               and alh.start_date is not null and alh.end_date is null
3660 																					connect by prior alh.child_id = alh.parent_id and alh.organization_id = l_orgs_tbl(ctr)
3661 										      										and alh.start_date is not null and alh.end_date is null
3662 							             							)
3663 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3664 /*
3665                             ( ( select parent_child_id
3666                             from amw_org_hierarchy_denorm
3667                             where process_id = l_procs_tbl(ctr)
3668                             and organization_id = l_orgs_tbl(ctr)
3669                             and up_down_ind = 'D'
3670                             and hierarchy_type = 'A' ) union (select l_procs_tbl(ctr) from dual)
3671                             )
3672 */
3673                             and approval_date is not null
3674                             and deletion_approval_date is null
3675                             and object_type = 'PROCESS_ORG'
3676                             ) ),
3677              control_count= (select count(*) from (
3678                             select distinct control_id from amw_control_associations
3679                             where pk1 = l_orgs_tbl(ctr)
3680                             and pk2 in (  select alh.child_id
3681                                           from  amw_approved_hierarchies alh
3682 																					start with alh.child_id = l_procs_tbl(ctr) and alh.organization_id = l_orgs_tbl(ctr)
3683                                               and alh.start_date is not null and alh.end_date is null
3684 																					connect by prior alh.child_id = alh.parent_id and alh.organization_id = l_orgs_tbl(ctr)
3685 										      										and alh.start_date is not null and alh.end_date is null
3686 							             							)
3687 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
3688 /*
3689                             ( ( select parent_child_id
3690                             from amw_org_hierarchy_denorm
3691                             where process_id = l_procs_tbl(ctr)
3692                             and organization_id = l_orgs_tbl(ctr)
3693                             and up_down_ind = 'D'
3694                             and hierarchy_type = 'A' ) union (select l_procs_tbl(ctr) from dual)
3695                             )
3696 */
3697                             and approval_date is not null
3698                             and deletion_approval_date is null
3699                             and object_type = 'RISK_ORG'
3700                             ) )
3701               ,last_update_date = sysdate
3702               ,last_updated_by = G_USER_ID
3703               ,last_update_login = G_LOGIN_ID
3704         where approval_date is not null
3705         and approval_end_date is null
3706         and process_id <> -2
3707         and organization_id = l_orgs_tbl(ctr)
3708         and process_id = l_procs_tbl(ctr);
3709       end if;
3710 
3711 	end if;
3712 
3713   	open c_processes(TO_NUMBER(p_process_id));
3714   	fetch c_processes bulk collect into x_ptbl;
3715   	close c_processes;
3716   	if(x_ptbl.exists(1)) then
3717   	forall i in x_ptbl.first .. x_ptbl.last
3718   		update amw_process
3719   		set org_count = (select count(*) from
3720                 (select distinct organization_id
3721                 from amw_process_organization
3722                 where process_id = x_ptbl(i)
3723                 and end_date is null
3724                 and (deletion_date is null or (deletion_date is not null and approval_date is null)))),
3725     	object_version_number = object_version_number + 1
3726     	,last_update_date = sysdate
3727     	,last_updated_by = G_USER_ID
3728     	,last_update_login = G_LOGIN_ID
3729 		where approval_date is not null
3730 		and approval_end_date is null
3731 		and process_id <> -1  --retained for safety
3732 		and process_id = x_ptbl(i);
3733 	end if;
3734 
3735     COMMIT;
3736     end if;
3737 	if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
3738     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3739     'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_ORG_CONC_REQUEST.End','END');
3740     end if;
3741 EXCEPTION
3742      WHEN others THEN
3743 		retcode :=2;
3744 		errbuf :=SUBSTR(SQLERRM,1,1000);
3745 		conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
3746 END push_proc_org_Conc_Request;
3747 
3748 -- ****************************************************************************
3749 
3750 procedure push_proc_org(
3751 p_parent_orgprocess_id	in number,
3752 p_process_id			in number,
3753 p_org_id_string			in varchar2,
3754 p_mode				    in varchar2,
3755 p_apply_rcm			    in varchar2,
3756 p_synchronize			in varchar2,
3757 p_update_count		    in varchar2 := FND_API.G_TRUE,
3758 p_commit			    in varchar2 := FND_API.G_FALSE,
3759 p_validation_level		IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
3760 p_init_msg_list			IN VARCHAR2 := FND_API.G_FALSE,
3761 x_return_status			out nocopy varchar2,
3762 x_msg_count			    out nocopy number,
3763 x_msg_data			    out nocopy varchar2 )
3764 
3765 is
3766 
3767 
3768 cursor c1 (pid number) is
3769         select parent_child_id process_to_count
3770         from amw_proc_hierarchy_denorm
3771         where process_id = pid
3772         and up_down_ind = 'D'
3773         and hierarchy_type = 'A'
3774         union
3775         select pid process_to_count from dual;
3776 
3777 L_API_NAME CONSTANT VARCHAR2(30) := 'push_proc_org';
3778 l_return_status	 varchar2(10);
3779 l_msg_count	 number;
3780 l_msg_data	 varchar2(4000);
3781 str              varchar2(4000);
3782 diff		 number;
3783 orgstr		 varchar2(100);
3784 l_org_string     varchar2(4000);
3785 orgid		 number;
3786 
3787 
3788 begin
3789 
3790   x_return_status := FND_API.G_RET_STS_SUCCESS;
3791   IF FND_API.to_Boolean( p_init_msg_list )  THEN
3792      FND_MSG_PUB.initialize;
3793   END IF;
3794   IF FND_GLOBAL.User_Id IS NULL THEN
3795     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
3796     RAISE FND_API.G_EXC_ERROR;
3797   END IF;
3798 
3799 
3800 l_org_string := p_org_id_string;
3801 while LENGTH(l_org_string) <> 0 loop
3802 select LTRIM(l_org_string, '1234567890') into str from dual;
3803 diff := LENGTH(l_org_string) - LENGTH(str);
3804 if  LENGTH(str) is null then  diff := LENGTH(l_org_string); end if;
3805 select SUBSTR(l_org_string, 1, diff) into orgstr from dual;
3806 orgid := to_number(orgstr);
3807 
3808 	push_proc_per_org(
3809             p_parent_orgprocess_id	=> p_parent_orgprocess_id,
3810 			p_process_id		=> p_process_id,
3811 			p_org_id			=> orgid,
3812 			p_mode				=> p_mode,
3813 			p_apply_rcm			=> p_apply_rcm,
3814 			p_synchronize		=> p_synchronize,
3815 			p_update_count     => p_update_count,
3816 			p_commit            =>p_commit,
3817 			x_return_status		=> l_return_status,
3818 			x_msg_count			=> l_msg_count,
3819 			x_msg_data			=> l_msg_data);
3820 
3821 	if l_return_status <> 'S' then
3822 		raise FND_API.G_EXC_ERROR;
3823 	end if;
3824 
3825 
3826 
3827 -- If user wants the processes to be associated as approved, user should set this profile to Y.
3828 -- Note that to get the whole hierarchy approved, user needs to set the approval option to
3829 -- "approve everything down below". Else only the process id passed will be approved.
3830 -- Although this is not very user friendly, I cannot see an option, as I cannot change the
3831 -- approval option ad hoc for this association process. When user associates a process,
3832 -- may be some-subprocess down below is in pending approval status, and that should prevent
3833 -- modifying the approval option.
3834 -- Also note that the "Approval Required" parameter for the org will be overridden, if set to yes.
3835 	if fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' then
3836 		AMW_PROC_ORG_APPROVAL_PKG.sub_for_approval(p_process_id, orgid);
3837 		AMW_PROC_ORG_APPROVAL_PKG.approve(p_process_id, orgid,p_update_count);
3838 	end if;
3839 
3840 
3841 select LTRIM(str, 'x') into l_org_string from dual;
3842 end loop;
3843 
3844 IF (p_update_count = FND_API.G_TRUE) THEN
3845 	-- update the org counts of the child process and its hierarchy....
3846 	for descendents_rec in c1(p_process_id) loop
3847      		exit when c1%notfound;
3848       		amw_rl_hierarchy_pkg.update_org_count(descendents_rec.process_to_count);
3849  	end loop;
3850 END IF;
3851 
3852   IF (p_commit = FND_API.G_TRUE) then
3853     commit;
3854   END IF;
3855 
3856 exception
3857 
3858   WHEN FND_API.G_EXC_ERROR THEN
3859      ROLLBACK;
3860      x_return_status := FND_API.G_RET_STS_ERROR;
3861      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
3862 
3863   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3864      ROLLBACK;
3865      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3866      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
3867 
3868   WHEN OTHERS THEN
3869      ROLLBACK;
3870      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3871      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3872      THEN
3873         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
3874      END IF;
3875      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count, p_data => x_msg_data);
3876 
3877 end push_proc_org;
3878 
3879 
3880 
3881 procedure push_proc_per_org(
3882 p_parent_orgprocess_id	in number,
3883 p_process_id			in number,
3884 p_org_id			in number,
3885 p_mode				in varchar2,
3886 p_apply_rcm			in varchar2,
3887 p_synchronize			in varchar2,
3888 p_update_count		    in varchar2 := FND_API.G_TRUE,
3889 p_commit			in varchar2 := FND_API.G_FALSE,
3890 p_validation_level		IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
3891 p_init_msg_list			IN VARCHAR2 := FND_API.G_FALSE,
3892 x_return_status			out nocopy varchar2,
3893 x_msg_count			out nocopy number,
3894 x_msg_data			out nocopy varchar2 )
3895 
3896 is
3897 
3898 L_API_NAME CONSTANT VARCHAR2(30) := 'push_proc_per_org';
3899 l_return_status	 varchar2(10);
3900 l_msg_count	 number;
3901 l_msg_data	 varchar2(4000);
3902 l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
3903 l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
3904 
3905 begin
3906   if( l_log_stmt_level >= l_curr_log_level ) then
3907     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3908         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_PER_ORG.Begin',
3909         'OrgId:' ||p_org_id || ';ProcessId:'||p_process_id
3910         ||';ParentProcessId:'||p_parent_orgprocess_id||';reviseExisting:'||p_synchronize||';ApplyRCM:'||p_apply_rcm);
3911   end if;
3912   x_return_status := FND_API.G_RET_STS_SUCCESS;
3913   IF FND_API.to_Boolean( p_init_msg_list )  THEN
3914      FND_MSG_PUB.initialize;
3915   END IF;
3916   IF FND_GLOBAL.User_Id IS NULL THEN
3917     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
3918     RAISE FND_API.G_EXC_ERROR;
3919   END IF;
3920 
3921   if( l_log_stmt_level >= l_curr_log_level ) then
3922     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3923         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_PER_ORG.associate_process_to_org',
3924         'OrgId:' ||p_org_id || ';p_associated_proc_id:'||p_process_id
3925         ||';ParentProcessId:'||p_parent_orgprocess_id||';reviseExisting:'||p_synchronize||';ApplyRCM:'||p_apply_rcm);
3926   end if;
3927 
3928   associate_process_to_org (
3929 	p_org_id => p_org_id,
3930 	p_parent_process_id => p_parent_orgprocess_id,
3931 	p_associated_proc_id => p_process_id,
3932 	p_revise_existing => p_synchronize,
3933     p_apply_rcm => p_apply_rcm);
3934 
3935 
3936 -- ko Though We can determine whether to update all the counts / or simply set to zero depending on th eapply_rcm parameter.
3937 -- ko for the time being updating for all the processes in the organization..
3938 --ko we need to come up an efficient count algorithm to  update the counts in downward direction also..
3939 --Ko Update the Proc_org_hierarchy_denorm tables..
3940 --ko  removing amw_org_hierarchy_denorm usage...
3941 /*
3942   if( l_log_stmt_level >= l_curr_log_level ) then
3943     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3944         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_PER_ORG.update_denorm',
3945         'Begin update denrom; OrgId:' ||p_org_id );
3946   end if;
3947   AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_org_id);
3948 */
3949   IF p_update_count = FND_API.G_TRUE THEN
3950   -- ko update the risk1 counts of the child process..
3951   if( l_log_stmt_level >= l_curr_log_level ) then
3952     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3953         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_PER_ORG.update_counts',
3954         'Begin update counts; OrgId:' ||p_org_id );
3955   end if;
3956   upd_ltst_risk_count(p_org_id => p_org_id, p_process_id => null);
3957 
3958 
3959   upd_ltst_control_count(p_org_id => p_org_id, p_process_id => null);
3960   END IF;
3961 
3962   if (p_commit = FND_API.G_TRUE) then
3963     commit;
3964   end if;
3965 
3966   if( l_log_stmt_level >= l_curr_log_level ) then
3967     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3968         'amw.plsql.AMW_ORG_HIERARCHY_PKG.PUSH_PROC_PER_ORG.End',
3969         'End');
3970   end if;
3971 
3972 exception
3973 
3974   WHEN FND_API.G_EXC_ERROR THEN
3975      ROLLBACK;
3976 
3977      x_return_status := FND_API.G_RET_STS_ERROR;
3978      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
3979 
3980   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3981      ROLLBACK;
3982 
3983      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3984      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
3985 
3986   WHEN OTHERS THEN
3987      ROLLBACK;
3988 
3989      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3990      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3991      THEN
3992         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
3993      END IF;
3994      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count, p_data => x_msg_data);
3995 
3996 end push_proc_per_org;
3997 
3998 -- **********************************************************************************************************
3999 
4000 /*kosriniv..... Procedure to add a process in the organization under a parent process..
4001  * p_organization_id  ---> Organization Id
4002  * p_parent_id        ---> parent process Id to which the child is being added
4003  * p_child_id         ---> process being associated
4004  * p_add_from         ---> identifies to add the existing process from org or new from RL
4005 			'O' ... Add from Organization
4006 			'R' ... Add from Risk Library
4007  * p_revise_existing  ---> if adding from Risk Library whether to keep the existing process or sync it
4008 			'Y' ... Revise / 'N'... use existing
4009  * p_apply_rcm        ---> if adding from Risk Library whether to apply the RCM of the process in Org or NOt
4010 	'RDEF'		  - Retain Definition in the organization.. Do not make any changes to Risks, controls and Audit Procedures.
4011 	'SLIB' 		  - Synchronize with the library definition .. Risks, Controls and Audit Procedures list equal to the RL
4012 	'ARCM'		  - Add Risks and Controls and Audit Procedures that exists in RL but not in Org.
4013 */
4014 
4015 PROCEDURE add_organization_child
4016 ( p_organization_id	    IN NUMBER,
4017   p_child_id                IN NUMBER,
4018   P_parent_id		    IN NUMBER,
4019   P_add_from 		    IN VARCHAR2,
4020   p_revise_existing	    IN VARCHAR2,
4021   P_apply_rcm	            IN VARCHAR2,
4022   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4023   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4024   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4025   x_return_status		   OUT NOCOPY VARCHAR2,
4026   x_msg_count			   OUT NOCOPY VARCHAR2,
4027   x_msg_data			   OUT NOCOPY VARCHAR2)
4028 
4029 IS
4030 
4031 cursor c1 (pid number) is
4032         select parent_child_id process_to_count
4033         from amw_proc_hierarchy_denorm
4034         where process_id = pid
4035         and up_down_ind = 'D'
4036         and hierarchy_type = 'A'
4037         union
4038         select pid process_to_count from dual;
4039 
4040 L_API_NAME CONSTANT VARCHAR2(30) := 'Add_organization_child';
4041 l_dummy NUMBER;
4042 
4043 begin
4044 
4045 --always initialize global variables in th api's used from SelfSerivice Fwk..
4046   G_USER_ID := FND_GLOBAL.USER_ID;
4047   G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4048   x_return_status := FND_API.G_RET_STS_SUCCESS;
4049   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4050      FND_MSG_PUB.initialize;
4051   END IF;
4052   IF FND_GLOBAL.User_Id IS NULL THEN
4053     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4054     RAISE FND_API.G_EXC_ERROR;
4055   END IF;
4056 
4057   -- Make sure latest revision of parent is existing in the organization..
4058 
4059   SELECT 1 INTO l_dummy
4060   FROM amw_process_organization
4061   where process_id = p_parent_id
4062   and organization_id = p_organization_id
4063   and end_date is null
4064   and deletion_date is null;
4065 
4066   IF p_add_from = 'O'  THEN -- Add an existing process from organization
4067 	-- Check that parent can be updated..(Not in pA or in locked state..)
4068 	produce_err_if_pa_or_locked(p_org_id => p_organization_id,p_process_id => p_parent_id);
4069         -- Parent can be updated...Add the child and parent... Circular hierarchy check is taken care in the procedure.
4070        -- Make sure latest revision of child exists in the organization
4071        	SELECT 1 INTO l_dummy
4072         FROM amw_process_organization
4073         where process_id = p_child_id
4074         and organization_id = p_organization_id
4075         and end_date is null
4076   	and deletion_date is null;
4077 
4078 	BEGIN
4079 	 -- dO not add if the relation ship already exists in the latest hierarchy
4080 	 SELECT 1 INTo l_dummy
4081 	 from amw_latest_hierarchies
4082 	 where organization_id = p_organization_id
4083 	 and parent_id = p_parent_id
4084 	 and child_id = p_child_id;
4085 
4086 	EXCEPTION
4087 		WHEN no_data_found THEN
4088 
4089  		 add_delete_ex_child (
4090 			p_org_id => p_organization_id,
4091 			p_parent_process_id => p_parent_id,
4092 			p_child_process_id => p_child_id,
4093 			p_action => 'ADD');
4094 
4095 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
4096 /*
4097 		-- Update the Proc_org_hierarchy_denorm tables..
4098 
4099 
4100 		AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_organization_id);
4101 */
4102 		-- update the risk 1 counts of the parent process ..
4103 		upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => p_parent_id);
4104 
4105 		upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => p_parent_id);
4106 	END;
4107 
4108   ELSIF p_add_from = 'R' THEN  -- Add the process from Risk library... Note that entire child hirarchy should be moved in to..
4109 
4110 
4111   --  Make sure that  the process not existing in the org.
4112   	BEGIN
4113 		SELECT 1 INTO l_dummy
4114   		FROM amw_process_organization
4115 		where process_id = p_child_id
4116 		and organization_id = p_organization_id
4117   		and end_date is null
4118 	  	and deletion_date is null;
4119 
4120 	  EXCEPTION
4121 	  	WHEN no_data_found THEN
4122 			associate_process_to_org (
4123 			p_org_id => p_organization_id,
4124 			p_parent_process_id => p_parent_id,
4125 			p_associated_proc_id => p_child_id,
4126 			p_revise_existing => p_revise_existing,
4127 			p_apply_rcm => p_apply_rcm);
4128 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
4129 /*
4130 			-- Update the Proc_org_hierarchy_denorm tables..
4131 			AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_organization_id);
4132 */
4133 			-- update the risk1 counts of the child process..
4134 			upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => null);
4135 
4136 			upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => null);
4137 
4138 			-- update the org counts of the child process and its hierarchy....
4139 			for descendents_rec in c1(p_child_id) loop
4140     	  		exit when c1%notfound;
4141     	  		amw_rl_hierarchy_pkg.update_org_count(descendents_rec.process_to_count);
4142  			end loop;
4143 	END;
4144 
4145  END IF;
4146 
4147 exception
4148   WHEN FND_API.G_EXC_ERROR THEN
4149      ROLLBACK;
4150      x_return_status := FND_API.G_RET_STS_ERROR;
4151      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4152 
4153   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4154      ROLLBACK;
4155      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4156      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4157 
4158   WHEN OTHERS THEN
4159      ROLLBACK;
4160      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4161      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4162      THEN
4163         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4164      END IF;
4165      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4166 END add_organization_child;
4167 
4168 -- **********************************************************************************************************
4169 
4170 /*kosriniv..... Procedure to delete a process in the organization from a parent process..
4171  * p_organization_id  ---> Organization Id
4172  * p_parent_id        ---> parent process Id to which the child is being added
4173  * p_child_id         ---> process being associated
4174 */
4175 
4176 PROCEDURE delete_organization_child
4177 ( p_organization_id	    IN NUMBER,
4178   p_child_id                IN NUMBER,
4179   P_parent_id		    IN NUMBER,
4180   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4181   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4182   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4183   x_return_status		   OUT NOCOPY VARCHAR2,
4184   x_msg_count			   OUT NOCOPY VARCHAR2,
4185   x_msg_data			   OUT NOCOPY VARCHAR2)
4186 
4187 IS
4188 
4189 L_API_NAME CONSTANT VARCHAR2(30) := 'delete_organization_child';
4190 l_dummy NUMBER;
4191 
4192 begin
4193 
4194 --always initialize global variables in th api's used from SelfSerivice Fwk..
4195   G_USER_ID := FND_GLOBAL.USER_ID;
4196   G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4197   x_return_status := FND_API.G_RET_STS_SUCCESS;
4198   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4199      FND_MSG_PUB.initialize;
4200   END IF;
4201   IF FND_GLOBAL.User_Id IS NULL THEN
4202     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4203     RAISE FND_API.G_EXC_ERROR;
4204   END IF;
4205 
4206   -- Make sure latest revision of parent is existing in the organization..
4207 
4208   SELECT 1 INTO l_dummy
4209   FROM amw_process_organization
4210   where process_id = p_parent_id
4211   and organization_id = p_organization_id
4212   and end_date is null
4213   and deletion_date is null;
4214 
4215 -- Check that parent can be updated..(Not in pA or in locked state..)
4216 
4217   produce_err_if_pa_or_locked(p_org_id => p_organization_id,p_process_id => p_parent_id);
4218 
4219 -- Parent can be updated...Add the child and parent... Circular hierarchy check is taken care in the procedure.
4220 
4221 -- Make sure latest revision of child exists in the organization
4222 
4223    SELECT 1 INTO l_dummy
4224    FROM amw_process_organization
4225    where process_id = p_child_id
4226    and organization_id = p_organization_id
4227    and end_date is null
4228    and deletion_date is null;
4229 
4230 
4231    add_delete_ex_child (
4232 	p_org_id => p_organization_id,
4233 	p_parent_process_id => p_parent_id,
4234 	p_child_process_id => p_child_id,
4235 	p_action => 'DEL');
4236 
4237 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
4238 /*
4239    -- Update the Proc_org_hierarchy_denorm tables..
4240 
4241    AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_organization_id);
4242 */
4243    -- update the risk 1 counts of the parent process ..
4244    upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => p_parent_id);
4245 
4246    upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => p_parent_id);
4247 
4248    -- update the org counts of the child process and its hierarchy....
4249   -- Need to be updated only after the process added to/deleted from approved hierarchy...
4250 
4251    -- Need to write a procedure for update the counts for all the child processes....
4252 
4253 
4254 
4255 exception
4256   WHEN FND_API.G_EXC_ERROR THEN
4257      ROLLBACK;
4258      x_return_status := FND_API.G_RET_STS_ERROR;
4259      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4260 
4261   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4262      ROLLBACK;
4263      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4264      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4265 
4266   WHEN OTHERS THEN
4267      ROLLBACK;
4268      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4269      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4270      THEN
4271         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4272      END IF;
4273      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4274 END delete_organization_child;
4275 
4276 --============================================================================================================================================
4277 
4278 PROCEDURE disassociate_org_process
4279 ( p_organization_id	    IN NUMBER,
4280   p_process_id		    IN NUMBER,
4281   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4282   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4283   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4284   x_return_status		   OUT NOCOPY VARCHAR2,
4285   x_msg_count			   OUT NOCOPY VARCHAR2,
4286   x_msg_data			   OUT NOCOPY VARCHAR2)
4287 IS
4288 
4289   L_API_NAME CONSTANT VARCHAR2(30) := 'disassociate_org_process';
4290 
4291 
4292 BEGIN
4293 
4294 --always initialize global variables in th api's used from SelfSerivice Fwk..
4295    G_USER_ID := FND_GLOBAL.USER_ID;
4296    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4297    x_return_status := FND_API.G_RET_STS_SUCCESS;
4298   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4299     FND_MSG_PUB.initialize;
4300   END IF;
4301   IF FND_GLOBAL.User_Id IS NULL THEN
4302     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4303     RAISE FND_API.G_EXC_ERROR;
4304   END IF;
4305 
4306 -- Disassociation functionality is to delete the process as a child and  mark the process as deleted
4307   disassociate_process_org(p_org_id => p_organization_id,p_process_id => p_process_id);
4308 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
4309 /*
4310   -- Update the Proc_org_hierarchy_denorm tables..
4311 
4312    AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_organization_id);
4313 */
4314 
4315   upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => NULL);
4316 
4317   upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => NULL);
4318 
4319 exception
4320   WHEN FND_API.G_EXC_ERROR THEN
4321      ROLLBACK;
4322      x_return_status := FND_API.G_RET_STS_ERROR;
4323      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4324 
4325   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4326      ROLLBACK;
4327      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4328      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4329 
4330   WHEN OTHERS THEN
4331      ROLLBACK;
4332      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4333      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4334      THEN
4335         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4336      END IF;
4337      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4338 END disassociate_org_process;
4339 
4340 
4341 --============================================================================================================================================
4342 
4343 PROCEDURE synchronize_org_process
4344 ( p_org_id   in number,
4345   p_process_id  in number,
4346   p_sync_mode in varchar2,
4347   p_sync_hierarchy in varchar2,
4348   p_sync_attributes in varchar2,
4349   p_sync_rcm in varchar2,
4350   p_sync_people in varchar2,
4351   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4352   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4353   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4354   x_return_status		   OUT NOCOPY VARCHAR2,
4355   x_msg_count			   OUT NOCOPY VARCHAR2,
4356   x_msg_data			   OUT NOCOPY VARCHAR2)
4357 IS
4358 
4359   cursor c1 (pid number) is
4360      select parent_child_id process_to_count
4361      from amw_proc_hierarchy_denorm
4362      where process_id = pid
4363      and up_down_ind = 'D'
4364      and hierarchy_type = 'A';
4365 
4366   L_API_NAME CONSTANT VARCHAR2(30) := 'synchronize_org_process';
4367 
4368 
4369 BEGIN
4370 
4371 --always initialize global variables in th api's used from SelfSerivice Fwk..
4372    G_USER_ID := FND_GLOBAL.USER_ID;
4373    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4374    x_return_status := FND_API.G_RET_STS_SUCCESS;
4375   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4376     FND_MSG_PUB.initialize;
4377   END IF;
4378   IF FND_GLOBAL.User_Id IS NULL THEN
4379     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4380     RAISE FND_API.G_EXC_ERROR;
4381   END IF;
4382 
4383 -- do the basic validation..i.e. the process can not be locked state..
4384 
4385   produce_err_if_pa_or_locked(p_org_id => p_org_id,p_process_id => p_process_id);
4386 
4387 -- So process is not in locked state.. Can be disassociated from  organization..
4388 
4389 -- Check whether the synchronization of the process is clicked or not..
4390 
4391 	synchronize_process(p_org_id      => p_org_id,
4392                         p_process_id  => p_process_id,
4393                         p_sync_mode   => p_sync_mode,
4394 						p_sync_hierarchy =>p_sync_hierarchy,
4395                         p_sync_attributes => p_sync_attributes,
4396                         p_sync_rcm => p_sync_rcm,
4397                         p_sync_people => p_sync_people );
4398 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
4399 /*
4400       -- Synchronized the hierarchy...So update the hierarchy denorm tables.
4401    AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_org_id);
4402 */
4403    upd_ltst_risk_count(p_org_id => p_org_id, p_process_id => NULL);
4404 
4405    upd_ltst_control_count(p_org_id => p_org_id, p_process_id => NULL);
4406 
4407    -- update the org counts of the child process and its hierarchy....
4408 	for descendents_rec in c1(p_process_id) loop
4409     	exit when c1%notfound;
4410     	amw_rl_hierarchy_pkg.update_org_count(descendents_rec.process_to_count);
4411  	end loop;
4412 
4413 
4414 
4415 exception
4416   WHEN FND_API.G_EXC_ERROR THEN
4417      ROLLBACK;
4418      x_return_status := FND_API.G_RET_STS_ERROR;
4419      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4420 
4421   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4422      ROLLBACK;
4423      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4424      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4425 
4426   WHEN OTHERS THEN
4427      ROLLBACK;
4428      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4429      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4430      THEN
4431         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4432      END IF;
4433      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4434 END synchronize_org_process;
4435 
4436 
4437 --============================================================================================================================================
4438 
4439 PROCEDURE update_latest_rc_counts
4440 ( p_organization_id	    IN NUMBER,
4441   P_process_id		    IN NUMBER,
4442   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4443   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4444   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4445   x_return_status		   OUT NOCOPY VARCHAR2,
4446   x_msg_count			   OUT NOCOPY VARCHAR2,
4447   x_msg_data			   OUT NOCOPY VARCHAR2)
4448 IS
4449 
4450   L_API_NAME CONSTANT VARCHAR2(30) := 'update_latest_rc_counts';
4451 
4452 
4453 BEGIN
4454 
4455 --always initialize global variables in th api's used from SelfSerivice Fwk..
4456    G_USER_ID := FND_GLOBAL.USER_ID;
4457    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4458    x_return_status := FND_API.G_RET_STS_SUCCESS;
4459   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4460     FND_MSG_PUB.initialize;
4461   END IF;
4462   IF FND_GLOBAL.User_Id IS NULL THEN
4463     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4464     RAISE FND_API.G_EXC_ERROR;
4465   END IF;
4466 
4467 -- Update the Risk Counts..
4468     upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => p_process_id);
4469 -- Update the Control Counts..
4470     upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => p_process_id);
4471 
4472 
4473 exception
4474   WHEN FND_API.G_EXC_ERROR THEN
4475      ROLLBACK;
4476      x_return_status := FND_API.G_RET_STS_ERROR;
4477      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4478 
4479   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4480      ROLLBACK;
4481      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4482      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4483 
4484   WHEN OTHERS THEN
4485      ROLLBACK;
4486      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4487      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4488      THEN
4489         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4490      END IF;
4491      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4492 END update_latest_rc_counts;
4493 
4494 --============================================================================================================================================
4495 
4496 PROCEDURE update_approved_rc_counts
4497 ( p_organization_id	    IN NUMBER,
4498   P_process_id		    IN NUMBER,
4499   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
4500   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4501   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
4502   x_return_status		   OUT NOCOPY VARCHAR2,
4503   x_msg_count			   OUT NOCOPY VARCHAR2,
4504   x_msg_data			   OUT NOCOPY VARCHAR2)
4505 IS
4506 
4507   L_API_NAME CONSTANT VARCHAR2(30) := 'update_approved_rc_counts';
4508 
4509 
4510 BEGIN
4511 
4512 --always initialize global variables in th api's used from SelfSerivice Fwk..
4513    G_USER_ID := FND_GLOBAL.USER_ID;
4514    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4515    x_return_status := FND_API.G_RET_STS_SUCCESS;
4516   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4517     FND_MSG_PUB.initialize;
4518   END IF;
4519   IF FND_GLOBAL.User_Id IS NULL THEN
4520     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4521     RAISE FND_API.G_EXC_ERROR;
4522   END IF;
4523 
4524 -- Update the Risk Counts..
4525     upd_appr_risk_count(p_org_id => p_organization_id, p_process_id => p_process_id);
4526 -- Update the Control Counts..
4527     upd_appr_control_count(p_org_id => p_organization_id, p_process_id => p_process_id);
4528 
4529 
4530 exception
4531   WHEN FND_API.G_EXC_ERROR THEN
4532      ROLLBACK;
4533      x_return_status := FND_API.G_RET_STS_ERROR;
4534      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4535 
4536   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4537      ROLLBACK;
4538      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4539      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4540 
4541   WHEN OTHERS THEN
4542      ROLLBACK;
4543      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4544      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4545      THEN
4546         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4547      END IF;
4548      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4549 END update_approved_rc_counts;
4550 
4551 
4552 -- ===============================================================================================================================================================================
4553 PROCEDURE insert_exception_justification (
4554 p_exception_Id		IN Number,
4555 p_justification	        IN Varchar2,
4556 p_commit		in varchar2 := FND_API.G_FALSE,
4557 p_validation_level	IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
4558 p_init_msg_list		IN VARCHAR2 := FND_API.G_FALSE,
4559 x_return_status		out nocopy varchar2,
4560 x_msg_count		out nocopy number,
4561 x_msg_data		out nocopy varchar2
4562 )
4563 IS
4564 
4565   L_API_NAME CONSTANT VARCHAR2(30) := 'insert_exception_justification';
4566 
4567 
4568 BEGIN
4569 
4570 --always initialize global variables in th api's used from SelfSerivice Fwk..
4571    G_USER_ID := FND_GLOBAL.USER_ID;
4572    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4573    x_return_status := FND_API.G_RET_STS_SUCCESS;
4574   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4575     FND_MSG_PUB.initialize;
4576   END IF;
4577   IF FND_GLOBAL.User_Id IS NULL THEN
4578     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4579     RAISE FND_API.G_EXC_ERROR;
4580   END IF;
4581 
4582 -- insert the justification rows.
4583     insert into amw_exceptions_tl
4584 		 (
4585                   EXCEPTION_ID,
4586                   LANGUAGE,
4587                   SOURCE_LANG,
4588                   JUSTIFICATION,
4589                   LAST_UPDATE_DATE,
4590                   LAST_UPDATED_BY,
4591                   CREATION_DATE,
4592                   CREATED_BY,
4593                   LAST_UPDATE_LOGIN
4594                   )
4595                   select
4596                  	p_Exception_Id,
4597                  	L.LANGUAGE_CODE,
4598                  	userenv('LANG'),
4599                  	p_Justification,
4600                  	sysdate,
4601                  	G_USER_ID,
4602                  	sysdate,
4603                  	G_USER_ID,
4604                  	G_LOGIN_ID
4605 			from FND_LANGUAGES L
4606                  	where L.INSTALLED_FLAG in ('I', 'B')
4607                  	and not exists
4608                  		(select NULL
4609                  		 from AMW_EXCEPTIONS_TL T
4610                  		 where T.EXCEPTION_ID = p_Exception_Id
4611                  		 and T.LANGUAGE = L.LANGUAGE_CODE);
4612 
4613 
4614 exception
4615   WHEN FND_API.G_EXC_ERROR THEN
4616      ROLLBACK;
4617      x_return_status := FND_API.G_RET_STS_ERROR;
4618      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4619 
4620   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4621      ROLLBACK;
4622      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4623      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
4624 
4625   WHEN OTHERS THEN
4626      ROLLBACK;
4627      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4628      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4629      THEN
4630         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4631      END IF;
4632      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
4633 END insert_exception_justification;
4634 
4635 -- ===============================================================================================================================================================================
4636 function areChildListSame(p_organization_id	    IN NUMBER,p_process_id in number) return varchar is
4637 retvalue varchar2(1);
4638 l_dummy number;
4639 begin
4640 
4641 retvalue := 'N';
4642 
4643         begin
4644            select child_id
4645            into l_dummy
4646            from amw_approved_hierarchies
4647            where parent_id = p_process_id
4648            and organization_id = p_organization_id
4649            and (end_date is null or end_date > sysdate)
4650            and child_id not in
4651               (select child_id
4652               from amw_latest_hierarchies
4653               where parent_id = p_process_id
4654               and organization_id = p_organization_id);
4655        exception
4656             when no_data_found then
4657                 begin
4658                    select child_id
4659                    into l_dummy
4660                    from amw_latest_hierarchies
4661                    where parent_id = p_process_id
4662                    and organization_id = p_organization_id
4663                    and child_id not in
4664                        (select child_id
4665                        from amw_approved_hierarchies
4666                        where parent_id = p_process_id
4667                        and organization_id = p_organization_id
4668                        and (end_date is null or end_date > sysdate));
4669                 exception
4670                     when too_many_rows then
4671                         return retvalue;
4672                     when no_data_found then
4673                         retvalue := 'Y';
4674                         return retvalue;
4675                 end;
4676             when too_many_rows then
4677                 return retvalue;
4678         end;
4679 return retvalue;
4680 end;
4681 -- ===============================================================================================================================================================================
4682 function does_apprvd_ver_exst(p_organization_id in number,p_process_id in number) return varchar is
4683 l_dummy number;
4684 begin
4685     select 1
4686     into l_dummy
4687     from amw_process_organization
4688     where process_id = p_process_id
4689     and organization_id = p_organization_id
4690     and approval_status = 'A';
4691 
4692     return 'Y';
4693 
4694 exception
4695     when no_data_found then
4696         return 'N';
4697     when too_many_rows then
4698         return 'Y';
4699 end does_apprvd_ver_exst;
4700 
4701 -- ===============================================================================================================================================================================
4702 -- this api is to be called from java to figure out if the process
4703 -- is undoable or not. Based on this, the Undo buutton should
4704 -- be rendered
4705 procedure isProcessUndoAble (p_organization_id in number,
4706                 			p_process_id in number,
4707                 			ret_value out nocopy varchar2,
4708 	                                x_return_status out nocopy varchar2,
4709                                         x_msg_count out nocopy number,
4710                                         x_msg_data out nocopy varchar2) is
4711 
4712 l_api_name constant varchar2(30) := 'isProcessUndoAble';
4713 p_init_msg_list varchar2(10) := FND_API.G_FALSE;
4714 err_msg varchar2(4000);
4715 l_dummy number;
4716 appstatus varchar2(10);
4717 
4718 begin
4719 --always initialize global variables in th api's used from SelfSerivice Fwk..
4720    G_USER_ID := FND_GLOBAL.USER_ID;
4721    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4722   x_return_status := FND_API.G_RET_STS_SUCCESS;
4723   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4724      FND_MSG_PUB.initialize;
4725   END IF;
4726   IF FND_GLOBAL.User_Id IS NULL THEN
4727     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4728     RAISE FND_API.G_EXC_ERROR;
4729   END IF;
4730 
4731 ret_value := 'N';
4732 
4733 -- check if the process is draft
4734 
4735 select approval_status into appstatus
4736 from amw_process_organization
4737 where process_id = p_process_id
4738 and organization_id = p_organization_id
4739 and end_date is null;
4740 
4741 if appstatus <> 'D' then
4742 	return;
4743 end if;
4744 
4745 -- check if the draft has been created due to addition/deletion of children
4746 
4747 if areChildListSame(p_organization_id,p_process_id) = 'Y' then
4748 	ret_value := 'Y';
4749 	return;
4750 else
4751 	return;
4752 end if;
4753 
4754 exception
4755   WHEN FND_API.G_EXC_ERROR THEN
4756      ROLLBACK;
4757      x_return_status := FND_API.G_RET_STS_ERROR;
4758      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
4759                                 p_count => x_msg_count,
4760                                 p_data => x_msg_data);
4761 
4762   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4763      ROLLBACK;
4764      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4765      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
4766                                p_count => x_msg_count,
4767                                p_data => x_msg_data);
4768 
4769   WHEN OTHERS THEN
4770      ROLLBACK;
4771      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4772      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4773      THEN
4774         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
4775      END IF;
4776      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,
4777                                 p_count => x_msg_count,
4778                                 p_data => x_msg_data);
4779 end;
4780 
4781 -- ===============================================================================================================================================================================
4782 -- call this only after calling isProcessUndoAble.
4783 -- This api performs the delete (purging of draft row) action
4784 -- if conditions are satisfied
4785 procedure delete_draft (p_organization_id in number,
4786                         p_process_id in number,
4787                         x_return_status out nocopy varchar2,
4788                         x_msg_count out nocopy number,
4789                         x_msg_data out nocopy varchar2) is
4790 
4791 l_api_name constant varchar2(30) := 'delete_draft';
4792 p_init_msg_list varchar2(10) := FND_API.G_FALSE;
4793 err_msg varchar2(4000);
4794 appexst varchar2(1);
4795 l_risk_exists boolean :=false;
4796 l_control_exists boolean :=false;
4797 cursor parents(orgId NUMBER, pid number) is
4798              select parent_id
4799              from amw_latest_hierarchies
4800              where child_id = pid
4801              and organization_id = orgId ;
4802 
4803 parent_rec parents%rowtype;
4804 l_flag varchar2(10);
4805 previd number;
4806 l_dummy number;
4807 ret_val varchar2(10);
4808 
4809 begin
4810 --always initialize global variables in th api's used from SelfSerivice Fwk..
4811    G_USER_ID := FND_GLOBAL.USER_ID;
4812    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
4813   x_return_status := FND_API.G_RET_STS_SUCCESS;
4814   IF FND_API.to_Boolean( p_init_msg_list )  THEN
4815      FND_MSG_PUB.initialize;
4816   END IF;
4817   IF FND_GLOBAL.User_Id IS NULL THEN
4818     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
4819     RAISE FND_API.G_EXC_ERROR;
4820   END IF;
4821 
4822 select process_org_rev_id
4823     into previd from amw_process_organization
4824     where process_id = p_process_id
4825     and organization_id = p_organization_id
4826     and end_date is null;
4827 
4828 appexst := does_apprvd_ver_exst(p_organization_id,p_process_id);
4829 
4830 
4831 if appexst = 'Y' then
4832 
4833     -- do another check for undoablity
4834 
4835          isProcessUndoAble (	p_organization_id => p_organization_id ,
4836                                         p_process_id => p_process_id,
4837                 			ret_value => ret_val,
4838 	                            x_return_status => x_return_status,
4839 	                            x_msg_count => x_msg_count,
4840 	                            x_msg_data => x_msg_data);
4841 
4842 	     if ret_val <> 'Y' then
4843             fnd_message.set_name('AMW','AMW_CANT_UNDO_DRAFT');
4844             err_msg := fnd_message.get;
4845             fnd_msg_pub.add_exc_msg(p_pkg_name  => 'amw_ORG_hierarchy_pkg',
4846                        	            p_procedure_name => 'delete_draft',
4847                                     p_error_text => err_msg);
4848             raise FND_API.G_EXC_ERROR;
4849 	     end if;
4850          if  x_return_status <> FND_API.G_RET_STS_SUCCESS then
4851             raise FND_API.G_EXC_UNEXPECTED_ERROR;
4852          end if;
4853 
4854 
4855 
4856 
4857     delete from amw_process_organization
4858     where process_id = p_process_id
4859     and organization_id = p_organization_id
4860     and end_date is null;
4861 
4862 
4863     update amw_process_organization
4864     set end_date = null
4865     where process_id = p_process_id
4866     and organization_id = p_organization_id
4867     and approval_date is not null
4868     and approval_end_date is null;
4869 
4870     -- If the previous version is deleted; then we make it deleted...
4871     begin
4872     	select 1 into l_dummy
4873     	from amw_process_organization
4874     	where process_id = p_process_id
4875     	and organization_id = p_organization_id
4876     	and deletion_date is not null
4877     	and end_date is null;
4878 
4879     	-- So  process is deleted..so remove the process from hierarchy
4880     	for parent_rec in parents(p_organization_id,p_process_id) loop
4881     	  exit when parents%notfound;
4882     	  revise_process_if_necessary(p_organization_id,parent_rec.parent_id);
4883     	  delete from amw_latest_hierarchies
4884     	  where parent_id = parent_rec.parent_id
4885     	  and child_id = p_process_id
4886     	  and organization_id = p_organization_id;
4887  	    end loop;
4888 
4889     	delete from amw_latest_hierarchies
4890     	where parent_id = p_process_id
4891     	and organization_id = p_organization_id;
4892 	exception
4893 		when no_data_found then
4894 			null;
4895 	end;
4896 
4897 
4898 
4899 else -- appexst = 'N'
4900 
4901     for parent_rec in parents(p_organization_id,p_process_id) loop
4902     	  exit when parents%notfound;
4903     	  revise_process_if_necessary(p_organization_id,parent_rec.parent_id);
4904     	  delete from amw_latest_hierarchies
4905     	  where parent_id = parent_rec.parent_id
4906     	  and child_id = p_process_id
4907     	  and organization_id = p_organization_id;
4908  	end loop;
4909 
4910     delete from amw_latest_hierarchies
4911     where parent_id = p_process_id
4912     and organization_id = p_organization_id;
4913     delete from amw_process_organization where process_id = p_process_id and organization_id = p_organization_id;
4914 
4915 end if;
4916 
4917 -- perform other common delete operations
4918 
4919 delete from amw_risk_associations
4920 where pk1 = p_organization_id
4921 and pk2 = p_process_id
4922 and approval_date is null
4923 and object_type = 'PROCESS_ORG';
4924 IF SQL%FOUND THEN
4925 l_risk_exists := TRUE;
4926 END IF;
4927 update amw_risk_associations
4928 set deletion_date = null
4929 where pk1 = p_organization_id
4930 and pk2 = p_process_id
4931 and object_type = 'PROCESS_ORG'
4932 and deletion_date is not null
4933 and deletion_approval_date is null;
4934 IF SQL%FOUND THEN
4935 l_risk_exists := TRUE;
4936 END IF;
4937 
4938 
4939 delete from amw_control_associations
4940 where pk1 = p_organization_id
4941 and pk2 = p_process_id
4942 and approval_date is null
4943 and object_type = 'RISK_ORG';
4944 IF SQL%FOUND THEN
4945 l_control_exists := TRUE;
4946 END IF;
4947 
4948 update amw_control_associations
4949 set deletion_date = null
4950 where pk1 = p_organization_id
4951 and pk2 = p_process_id
4952 and object_type = 'RISK_ORG'
4953 and deletion_date is not null
4954 and deletion_approval_date is null;
4955 IF SQL%FOUND THEN
4956 l_control_exists := TRUE;
4957 END IF;
4958 
4959 
4960 delete from amw_acct_associations
4961 where pk1 = p_organization_id
4962 and pk2 = p_process_id
4963 and approval_date is null
4964 and object_type = 'PROCESS_ORG';
4965 
4966 update amw_acct_associations
4967 set deletion_date = null
4968 where pk1 = p_organization_id
4969 and pk2 = p_process_id
4970 and object_type = 'PROCESS_ORG'
4971 and deletion_date is not null
4972 and deletion_approval_date is null;
4973 
4974 
4975 delete from amw_objective_associations
4976 where pk1 = p_organization_id
4977 and pk2 = p_process_id
4978 and approval_date is null
4979 and object_type in ('PROCESS_ORG', 'CONTROL_ORG');
4980 
4981 update amw_objective_associations
4982 set deletion_date = null
4983 where pk1 = p_organization_id
4984 and pk2 = p_process_id
4985 and object_type in ('PROCESS_ORG', 'CONTROL_ORG')
4986 and deletion_date is not null
4987 and deletion_approval_date is null;
4988 
4989 delete from amw_ap_associations
4990 where pk1 = p_organization_id
4991 and   pk2 = p_process_id
4992 and association_creation_date is null
4993 and   object_type = 'CTRL_ORG';
4994 
4995 
4996 FND_ATTACHED_DOCUMENTS2_PKG.delete_attachments(X_entity_name => 'AMW_PROCESS_ORGANIZATION',
4997                                                X_pk1_value   => previd);
4998 
4999 -- cancel existing change requests
5000 -- update org count..
5001 amw_rl_hierarchy_pkg.update_org_count(p_process_id);
5002 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
5003 /*
5004 -- update latest hierarchy denorm
5005 amw_rl_hierarchy_pkg.update_denorm (p_organization_id, sysdate);
5006 */
5007 if appexst = 'Y' AND l_risk_exists then
5008 
5009 -- Update the latest risk control counts..
5010 upd_ltst_risk_count(p_organization_id,p_process_id);
5011 
5012 
5013 end if;
5014 
5015 if appexst = 'Y' AND l_control_exists then
5016 
5017 -- Update the latest risk control counts..
5018 upd_ltst_control_count(p_organization_id,p_process_id);
5019 
5020 end if;
5021 
5022 exception
5023   WHEN FND_API.G_EXC_ERROR THEN
5024      ROLLBACK;
5025      x_return_status := FND_API.G_RET_STS_ERROR;
5026      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5027                                 p_count => x_msg_count,
5028                                 p_data => x_msg_data);
5029 
5030   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5031      ROLLBACK;
5032      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5033      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5034                                p_count => x_msg_count,
5035                                p_data => x_msg_data);
5036 
5037   WHEN OTHERS THEN
5038      ROLLBACK;
5039      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5040      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5041      THEN
5042         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
5043      END IF;
5044      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,
5045                                 p_count => x_msg_count,
5046                                 p_data => x_msg_data);
5047 end;
5048 
5049 --==============================================================================================================
5050 -- Bring the AuditProcedures for newly added controls in the risklibrary to organization controls
5051 procedure UPDATE_ORG_PROC_AP(p_organization_id	    IN NUMBER,
5052                         p_process_id in number,
5053                         p_date in DATE,
5054                         x_return_status out nocopy varchar2,
5055                         x_msg_count out nocopy number,
5056                         x_msg_data out nocopy varchar2) is
5057 
5058 l_api_name constant varchar2(30) := 'UPDATE_ORG_PROC_AP';
5059 p_init_msg_list varchar2(10) := FND_API.G_FALSE;
5060 err_msg varchar2(4000);
5061 
5062 begin
5063 --always initialize global variables in th api's used from SelfSerivice Fwk..
5064    G_USER_ID := FND_GLOBAL.USER_ID;
5065    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
5066   x_return_status := FND_API.G_RET_STS_SUCCESS;
5067   IF FND_API.to_Boolean( p_init_msg_list )  THEN
5068      FND_MSG_PUB.initialize;
5069   END IF;
5070   IF FND_GLOBAL.User_Id IS NULL THEN
5071     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
5072     RAISE FND_API.G_EXC_ERROR;
5073   END IF;
5074 
5075 
5076 	insert into amw_ap_associations
5077     (AP_ASSOCIATION_ID,
5078     AUDIT_PROCEDURE_ID,
5079     PK1,
5080     PK2,
5081     PK3,
5082     DESIGN_EFFECTIVENESS,
5083     OP_EFFECTIVENESS,
5084     ASSOCIATION_CREATION_DATE,
5085     APPROVAL_DATE,
5086     DELETION_DATE,
5087     DELETION_APPROVAL_DATE,
5088     OBJECT_TYPE,
5089     LAST_UPDATE_DATE,
5090     LAST_UPDATED_BY,
5091     CREATION_DATE,
5092     CREATED_BY,
5093     LAST_UPDATE_LOGIN,
5094     OBJECT_VERSION_NUMBER)
5095     (select
5096     AMW_AP_ASSOCIATIONS_S.nextval,
5097     APA.AUDIT_PROCEDURE_ID,
5098     p_organization_id,
5099     p_process_id,
5100     CONTROLS.control_id,
5101     APA.DESIGN_EFFECTIVENESS,
5102     APA.OP_EFFECTIVENESS,
5103     null,
5104     null,
5105     null,
5106     null,
5107     'CTRL_ORG',
5108     sysdate,
5109     G_USER_ID,
5110     sysdate,
5111     G_USER_ID,
5112     G_LOGIN_ID,
5113     1
5114 	from
5115 	amw_ap_associations APA,
5116 	(SELECT distinct control_id from
5117 	amw_control_associations
5118 	where object_type = 'RISK_ORG'
5119 	AND PK1 = p_organization_id
5120 	AND PK2 = p_process_id
5121 	AND association_creation_date = p_date
5122 	AND approval_date is null) CONTROLS
5123 	where APA.object_type = 'CTRL'
5124 	and APA.pk1 = CONTROLS.control_id
5125 	and APA.approval_date is not null
5126 	and APA.deletion_approval_date is null
5127 	and APA.audit_procedure_id not in ( select audit_procedure_id from amw_ap_associations
5128                                 where object_type = 'CTRL_ORG'
5129                                 and pk1 = p_organization_id
5130                                 and pk2 = p_process_id
5131                                 and pk3 =  CONTROLS.control_id
5132                                 and deletion_date is null));
5133 
5134 
5135 DELETE FROM AMW_AP_ASSOCIATIONS
5136 WHERE object_type = 'CTRL_ORG'
5137 and pk1 = p_organization_id
5138 and pk2 = p_process_id
5139 and association_creation_date is null
5140 and pk3 not in ( SELECT control_id from
5141                  amw_control_associations
5142                  where object_type = 'RISK_ORG'
5143                  AND PK1 = p_organization_id
5144                  AND PK2 = p_process_id
5145                  AND approval_date is null);
5146 
5147 exception
5148   WHEN FND_API.G_EXC_ERROR THEN
5149      ROLLBACK;
5150      x_return_status := FND_API.G_RET_STS_ERROR;
5151      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5152                                 p_count => x_msg_count,
5153                                 p_data => x_msg_data);
5154 
5155   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5156      ROLLBACK;
5157      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5158      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5159                                p_count => x_msg_count,
5160                                p_data => x_msg_data);
5161 
5162   WHEN OTHERS THEN
5163      ROLLBACK;
5164      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5165      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5166      THEN
5167         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
5168      END IF;
5169      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,
5170                                 p_count => x_msg_count,
5171                                 p_data => x_msg_data);
5172 end;
5173 
5174 --==============================================================================================================
5175 -- Bring the AuditProcedures for newly added controls in the risklibrary to organization controls
5176 procedure UPDATE_ENTITY_AP(p_organization_id	    IN NUMBER,
5177                         p_date in DATE,
5178                         x_return_status out nocopy varchar2,
5179                         x_msg_count out nocopy number,
5180                         x_msg_data out nocopy varchar2) is
5181 
5182 l_api_name constant varchar2(30) := 'UPDATE_ENTITY_AP';
5183 p_init_msg_list varchar2(10) := FND_API.G_FALSE;
5184 err_msg varchar2(4000);
5185 
5186 begin
5187 --always initialize global variables in th api's used from SelfSerivice Fwk..
5188    G_USER_ID := FND_GLOBAL.USER_ID;
5189    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
5190   x_return_status := FND_API.G_RET_STS_SUCCESS;
5191   IF FND_API.to_Boolean( p_init_msg_list )  THEN
5192      FND_MSG_PUB.initialize;
5193   END IF;
5194   IF FND_GLOBAL.User_Id IS NULL THEN
5195     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
5196     RAISE FND_API.G_EXC_ERROR;
5197   END IF;
5198 
5199 
5200     insert into amw_ap_associations
5201     (AP_ASSOCIATION_ID,
5202     AUDIT_PROCEDURE_ID,
5203     PK1,
5204     PK2,
5205     DESIGN_EFFECTIVENESS,
5206     OP_EFFECTIVENESS,
5207     ASSOCIATION_CREATION_DATE,
5208     APPROVAL_DATE,
5209     DELETION_DATE,
5210     DELETION_APPROVAL_DATE,
5211     OBJECT_TYPE,
5212     LAST_UPDATE_DATE,
5213     LAST_UPDATED_BY,
5214     CREATION_DATE,
5215     CREATED_BY,
5216     LAST_UPDATE_LOGIN,
5217     OBJECT_VERSION_NUMBER)
5218     (select
5219     AMW_AP_ASSOCIATIONS_S.nextval,
5220     APA.AUDIT_PROCEDURE_ID,
5221     p_organization_id,
5222     CONTROLS.control_id,
5223     APA.DESIGN_EFFECTIVENESS,
5224     APA.OP_EFFECTIVENESS,
5225     sysdate,
5226     null,
5227     null,
5228     null,
5229     'ENTITY_AP',
5230     sysdate,
5231     G_USER_ID,
5232     sysdate,
5233     G_USER_ID,
5234     G_LOGIN_ID,
5235     1
5236 	from
5237 	amw_ap_associations APA,
5238 	(SELECT distinct control_id from
5239 	amw_control_associations
5240 	where object_type = 'ENTITY_CONTROL'
5241 	AND PK1 = p_organization_id
5242 	AND association_creation_date = p_date	) CONTROLS
5243 	where APA.object_type = 'CTRL'
5244 	and APA.pk1 = CONTROLS.control_id
5245 	and APA.approval_date is not null
5246 	and APA.deletion_approval_date is null
5247 	and APA.audit_procedure_id not in ( select audit_procedure_id from amw_ap_associations
5248                                 where object_type = 'ENTITY_AP'
5249                                 and pk1 = p_organization_id
5250                                 and pk2 = CONTROLS.control_id)
5251     );
5252 
5253 
5254 DELETE FROM AMW_AP_ASSOCIATIONS
5255 WHERE object_type = 'ENTITY_AP'
5256 and pk1 = p_organization_id
5257 and pk2 not in ( SELECT control_id from
5258                  amw_control_associations
5259                  where object_type = 'ENTITY_CONTROL'
5260                  AND PK1 = p_organization_id
5261                  );
5262 
5263 exception
5264   WHEN FND_API.G_EXC_ERROR THEN
5265      ROLLBACK;
5266      x_return_status := FND_API.G_RET_STS_ERROR;
5267      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5268                                 p_count => x_msg_count,
5269                                 p_data => x_msg_data);
5270 
5271   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5272      ROLLBACK;
5273      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5274      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,
5275                                p_count => x_msg_count,
5276                                p_data => x_msg_data);
5277 
5278   WHEN OTHERS THEN
5279      ROLLBACK;
5280      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5281      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5282      THEN
5283         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
5284      END IF;
5285      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,
5286                                 p_count => x_msg_count,
5287                                 p_data => x_msg_data);
5288 end;
5289 
5290 
5291 procedure sync_process_attributes(p_org_id   in number,
5292 								  p_process_id  in number
5293 								 ) is
5294 
5295 
5296 l_RL_PROCESS_REV_ID               amw_process.PROCESS_REV_ID%type;
5297 l_SIGNIFICANT_PROCESS_FLAG	      amw_process.SIGNIFICANT_PROCESS_FLAG%type;
5298 l_STANDARD_PROCESS_FLAG 	      amw_process.STANDARD_PROCESS_FLAG%type;
5299 l_PROCESS_CATEGORY    		      amw_process.PROCESS_CATEGORY%type;
5300 l_STANDARD_VARIATION      	      amw_process.STANDARD_VARIATION%type;
5301 l_ATTRIBUTE_CATEGORY      	      amw_process.ATTRIBUTE_CATEGORY%type;
5302 l_ATTRIBUTE1              	      amw_process.ATTRIBUTE1%type;
5303 l_ATTRIBUTE2              	      amw_process.ATTRIBUTE2%type;
5304 l_ATTRIBUTE3              	      amw_process.ATTRIBUTE3%type;
5305 l_ATTRIBUTE4              	      amw_process.ATTRIBUTE4%type;
5306 l_ATTRIBUTE5              	      amw_process.ATTRIBUTE5%type;
5307 l_ATTRIBUTE6              	      amw_process.ATTRIBUTE6%type;
5308 l_ATTRIBUTE7              	      amw_process.ATTRIBUTE7%type;
5309 l_ATTRIBUTE8              	      amw_process.ATTRIBUTE8%type;
5310 l_ATTRIBUTE9              	      amw_process.ATTRIBUTE9%type;
5311 l_ATTRIBUTE10             	      amw_process.ATTRIBUTE10%type;
5312 l_ATTRIBUTE11             	      amw_process.ATTRIBUTE11%type;
5313 l_ATTRIBUTE12             	      amw_process.ATTRIBUTE12%type;
5314 l_ATTRIBUTE13             	      amw_process.ATTRIBUTE13%type;
5315 l_ATTRIBUTE14             	      amw_process.ATTRIBUTE14%type;
5316 l_ATTRIBUTE15             	      amw_process.ATTRIBUTE15%type;
5317 l_SECURITY_GROUP_ID       	      amw_process.SECURITY_GROUP_ID%type;
5318 l_PROCESS_TYPE            	      amw_process.PROCESS_TYPE%type;
5319 l_CONTROL_ACTIVITY_TYPE		      amw_process.CONTROL_ACTIVITY_TYPE%type;
5320 
5321 
5322 begin
5323   -- find out if the latest revision for target is approved or not.
5324   -- if approved, revise it. if draft, don't do anything
5325       revise_process_if_necessary(p_org_id, p_process_id);
5326 
5327   -- copy the attributes from rl to org
5328        select PROCESS_REV_ID,
5329 			  SIGNIFICANT_PROCESS_FLAG,
5330 			  STANDARD_PROCESS_FLAG,
5331 			  PROCESS_CATEGORY,
5332 			  STANDARD_VARIATION,
5333 			  ATTRIBUTE_CATEGORY,
5334 			  ATTRIBUTE1,
5335 			  ATTRIBUTE2,
5336 			  ATTRIBUTE3,
5337 			  ATTRIBUTE4,
5338 			  ATTRIBUTE5,
5339 			  ATTRIBUTE6,
5340 			  ATTRIBUTE7,
5341 			  ATTRIBUTE8,
5342 			  ATTRIBUTE9,
5343 			  ATTRIBUTE10,
5344 			  ATTRIBUTE11,
5345 			  ATTRIBUTE12,
5346 			  ATTRIBUTE13,
5347 			  ATTRIBUTE14,
5348 			  ATTRIBUTE15,
5349 			  SECURITY_GROUP_ID,
5350 			  PROCESS_TYPE,
5351 			  CONTROL_ACTIVITY_TYPE
5352               into
5353               l_RL_PROCESS_REV_ID,
5354 			  l_SIGNIFICANT_PROCESS_FLAG,
5355 			  l_STANDARD_PROCESS_FLAG,
5356 			  l_PROCESS_CATEGORY,
5357 			  l_STANDARD_VARIATION,
5358 			  l_ATTRIBUTE_CATEGORY,
5359 			  l_ATTRIBUTE1,
5360 			  l_ATTRIBUTE2,
5361 			  l_ATTRIBUTE3,
5362 			  l_ATTRIBUTE4,
5363 			  l_ATTRIBUTE5,
5364 			  l_ATTRIBUTE6,
5365 			  l_ATTRIBUTE7,
5366 			  l_ATTRIBUTE8,
5367 			  l_ATTRIBUTE9,
5368 			  l_ATTRIBUTE10,
5369 			  l_ATTRIBUTE11,
5370 			  l_ATTRIBUTE12,
5371 			  l_ATTRIBUTE13,
5372 			  l_ATTRIBUTE14,
5373 			  l_ATTRIBUTE15,
5374 			  l_SECURITY_GROUP_ID,
5375 			  l_PROCESS_TYPE,
5376 			  l_CONTROL_ACTIVITY_TYPE
5377               from amw_process
5378               where process_id = p_process_id
5379 --ko need to sync with approved revision
5380 --              and end_date is null;
5381               and approval_date is not null
5382               and approval_end_date is null;
5383 
5384 
5385 
5386       update amw_process_organization
5387       set     RL_PROCESS_REV_ID             =   l_RL_PROCESS_REV_ID,
5388 			  SIGNIFICANT_PROCESS_FLAG      =   l_SIGNIFICANT_PROCESS_FLAG,
5389 			  STANDARD_PROCESS_FLAG	        =   l_STANDARD_PROCESS_FLAG,
5390 			  PROCESS_CATEGORY_CODE	            =   l_PROCESS_CATEGORY,
5391 			  STANDARD_VARIATION	        =   l_STANDARD_VARIATION,
5392 			  ATTRIBUTE_CATEGORY            =   l_ATTRIBUTE_CATEGORY,
5393 			  ATTRIBUTE1		    =   l_ATTRIBUTE1,
5394 			  ATTRIBUTE2			=   l_ATTRIBUTE2,
5395 			  ATTRIBUTE3			=   l_ATTRIBUTE3,
5396 			  ATTRIBUTE4			=   l_ATTRIBUTE4,
5397 			  ATTRIBUTE5			=   l_ATTRIBUTE5,
5398 			  ATTRIBUTE6			=   l_ATTRIBUTE6,
5399 			  ATTRIBUTE7			=   l_ATTRIBUTE7,
5400 			  ATTRIBUTE8			=   l_ATTRIBUTE8,
5401 			  ATTRIBUTE9			=   l_ATTRIBUTE9,
5402 			  ATTRIBUTE10			=   l_ATTRIBUTE10,
5403 			  ATTRIBUTE11			=   l_ATTRIBUTE11,
5404 			  ATTRIBUTE12			=   l_ATTRIBUTE12,
5405 			  ATTRIBUTE13			=   l_ATTRIBUTE13,
5406 			  ATTRIBUTE14			=   l_ATTRIBUTE14,
5407 			  ATTRIBUTE15			=   l_ATTRIBUTE15,
5408 			  SECURITY_GROUP_ID		=   l_SECURITY_GROUP_ID,
5409 			  PROCESS_TYPE			=   l_PROCESS_TYPE,
5410 			  CONTROL_ACTIVITY_TYPE	=   l_CONTROL_ACTIVITY_TYPE
5411       where organization_id = p_org_id
5412       and process_id = p_process_id
5413       and end_date is null;
5414 
5415   -- apply process key accounts -- Delete the accounts that does not exists in the rl process...
5416 
5417  	--ko, delete all the unapproved rows
5418         delete amw_acct_associations
5419         where pk1 = p_org_id
5420         and   pk2 = p_process_id
5421         and   object_type = 'PROCESS_ORG'
5422         and   approval_date is null;
5423 
5424     -- delete existing accounts that are not found in RL process...
5425         update amw_acct_associations
5426         set DELETION_DATE = sysdate
5427         where pk1 = p_org_id
5428         and   pk2 = p_process_id
5429         and   object_type = 'PROCESS_ORG'
5430         and   deletion_date is null
5431         and natural_account_id not in (select natural_account_id
5432         								from amw_acct_associations
5433         								where pk1 = p_process_id
5434         								and object_type = 'PROCESS'
5435         								and approval_date is not null
5436         								and deletion_approval_date is null);
5437 
5438 
5439 		insert into amw_acct_associations(
5440 		ACCT_ASSOC_ID,
5441         NATURAL_ACCOUNT_ID,
5442         PK1,
5443         PK2,
5444         STATEMENT_ID,
5445         STATEMENT_LINE_ID,
5446         ORIG_SYSTEM_ACCT_VALUE,
5447         ASSOCIATION_CREATION_DATE,
5448         APPROVAL_DATE,
5449         DELETION_DATE,
5450         DELETION_APPROVAL_DATE,
5451         OBJECT_TYPE,
5452         LAST_UPDATE_DATE,
5453         LAST_UPDATED_BY,
5454         CREATION_DATE,
5455         CREATED_BY,
5456         LAST_UPDATE_LOGIN,
5457         OBJECT_VERSION_NUMBER)
5458         (select
5459         AMW_ACCT_ASSOCIATIONS_S.nextval,
5460         NATURAL_ACCOUNT_ID,
5461         p_org_id,
5462         PK1,
5463         STATEMENT_ID,
5464         STATEMENT_LINE_ID,
5465         ORIG_SYSTEM_ACCT_VALUE,
5466         sysdate,
5467         null,
5468         null,
5469         null,
5470         'PROCESS_ORG',
5471         sysdate,
5472         G_USER_ID,
5473         sysdate,
5474         G_USER_ID,
5475         G_LOGIN_ID,
5476         1
5477         from amw_acct_associations
5478         where PK1 = p_process_id
5479         and object_type = 'PROCESS'
5480         and approval_date is not null
5481         and deletion_approval_date is null
5482         and NATURAL_ACCOUNT_ID not in(select natural_account_id
5483                                       from amw_acct_associations
5484                                       where pk1 = p_org_id
5485         							  and   pk2 = p_process_id
5486         							  and   object_type = 'PROCESS_ORG'
5487         							  and   deletion_date is null));
5488 
5489 -- Copy attachments...by dpatel
5490 sync_attachments(p_process_id => p_process_id
5491     ,p_org_id => p_org_id
5492     ,p_add_upd_flag => 'U');
5493 -- Copy Objectives...by dpatel
5494 --first delete all the unapproved rows...by dpatel
5495 delete from amw_objective_associations
5496 where pk1 = p_org_id
5497 and   pk2 = p_process_id
5498 and   object_type = 'PROCESS_ORG'
5499 and   approval_date is null;
5500 
5501 --then delete existing objectives that are not found in RL process...by dpatel
5502 update amw_objective_associations
5503 set deletion_date = sysdate
5504 where pk1 = p_org_id
5505 and   pk2 = p_process_id
5506 and   object_type = 'PROCESS_ORG'
5507 and   deletion_date is null
5508 and PROCESS_OBJECTIVE_ID not in (select PROCESS_OBJECTIVE_ID
5509 								from amw_objective_associations
5510 								where pk1 = p_process_id
5511 								and object_type = 'PROCESS'
5512 								and approval_date is not null
5513 								and deletion_approval_date is null);
5514 --add objectives from the RL...by dpatel
5515 insert into amw_objective_associations
5516     (OBJECTIVE_ASSOCIATION_ID,
5517     PROCESS_OBJECTIVE_ID,
5518     PK1,
5519     PK2,
5520     ASSOCIATION_CREATION_DATE,
5521     APPROVAL_DATE,
5522     DELETION_DATE,
5523     DELETION_APPROVAL_DATE,
5524     OBJECT_TYPE,
5525     LAST_UPDATE_DATE,
5526     LAST_UPDATED_BY,
5527     CREATION_DATE,
5528     CREATED_BY,
5529     LAST_UPDATE_LOGIN,
5530     OBJECT_VERSION_NUMBER)
5531     (select
5532     AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
5533     PROCESS_OBJECTIVE_ID,
5534     p_org_id,
5535     PK1,
5536     sysdate,
5537     null,
5538     null,
5539     null,
5540     'PROCESS_ORG',
5541     sysdate,
5542     G_USER_ID,
5543     sysdate,
5544     G_USER_ID,
5545     G_LOGIN_ID,
5546     1
5547     from amw_objective_associations
5548     where PK1 = p_process_id
5549     and object_type = 'PROCESS'
5550     and approval_date is not null
5551     and deletion_approval_date is null
5552     and PROCESS_OBJECTIVE_ID not in (select PROCESS_OBJECTIVE_ID
5553                                     from amw_objective_associations
5554                                     where
5555                                     PK1 = p_org_id
5556                                     and PK2 = p_process_id
5557                                     and object_type = 'PROCESS_ORG'
5558                                     and deletion_date is null)
5559     );
5560 
5561 end;
5562 
5563 /*
5564   Synchronize the Process Attributes of the process in a all organizations.
5565 */
5566 procedure sync_process_attributes(p_process_id  in number ) is
5567 l_RL_PROCESS_REV_ID               amw_process.PROCESS_REV_ID%type;
5568 l_SIGNIFICANT_PROCESS_FLAG	      amw_process.SIGNIFICANT_PROCESS_FLAG%type;
5569 l_STANDARD_PROCESS_FLAG 	      amw_process.STANDARD_PROCESS_FLAG%type;
5570 l_PROCESS_CATEGORY    		      amw_process.PROCESS_CATEGORY%type;
5571 l_STANDARD_VARIATION      	      amw_process.STANDARD_VARIATION%type;
5572 l_ATTRIBUTE_CATEGORY      	      amw_process.ATTRIBUTE_CATEGORY%type;
5573 l_ATTRIBUTE1              	      amw_process.ATTRIBUTE1%type;
5574 l_ATTRIBUTE2              	      amw_process.ATTRIBUTE2%type;
5575 l_ATTRIBUTE3              	      amw_process.ATTRIBUTE3%type;
5576 l_ATTRIBUTE4              	      amw_process.ATTRIBUTE4%type;
5577 l_ATTRIBUTE5              	      amw_process.ATTRIBUTE5%type;
5578 l_ATTRIBUTE6              	      amw_process.ATTRIBUTE6%type;
5579 l_ATTRIBUTE7              	      amw_process.ATTRIBUTE7%type;
5580 l_ATTRIBUTE8              	      amw_process.ATTRIBUTE8%type;
5581 l_ATTRIBUTE9              	      amw_process.ATTRIBUTE9%type;
5582 l_ATTRIBUTE10             	      amw_process.ATTRIBUTE10%type;
5583 l_ATTRIBUTE11             	      amw_process.ATTRIBUTE11%type;
5584 l_ATTRIBUTE12             	      amw_process.ATTRIBUTE12%type;
5585 l_ATTRIBUTE13             	      amw_process.ATTRIBUTE13%type;
5586 l_ATTRIBUTE14             	      amw_process.ATTRIBUTE14%type;
5587 l_ATTRIBUTE15             	      amw_process.ATTRIBUTE15%type;
5588 l_SECURITY_GROUP_ID       	      amw_process.SECURITY_GROUP_ID%type;
5589 l_PROCESS_TYPE            	      amw_process.PROCESS_TYPE%type;
5590 l_CONTROL_ACTIVITY_TYPE		      amw_process.CONTROL_ACTIVITY_TYPE%type;
5591 begin
5592   -- find out if the latest revision for target is approved or not.
5593   -- if approved, revise it. if draft, don't do anything
5594 
5595   -- psomanat : The check is allready done in Synchronize_process Procedure
5596   -- revise_process_if_necessary(p_org_id, p_process_id);
5597 
5598   -- copy the attributes from rl to org
5599 
5600     select  PROCESS_REV_ID,
5601             SIGNIFICANT_PROCESS_FLAG,
5602             STANDARD_PROCESS_FLAG,
5603 		    PROCESS_CATEGORY,
5604 			STANDARD_VARIATION,
5605 			ATTRIBUTE_CATEGORY,
5606 			ATTRIBUTE1,
5607 			ATTRIBUTE2,
5608 			ATTRIBUTE3,
5609 			ATTRIBUTE4,
5610 			ATTRIBUTE5,
5611 			ATTRIBUTE6,
5612 			ATTRIBUTE7,
5613 			ATTRIBUTE8,
5614 			ATTRIBUTE9,
5615 			ATTRIBUTE10,
5616 			ATTRIBUTE11,
5617 			ATTRIBUTE12,
5618 			ATTRIBUTE13,
5619 			ATTRIBUTE14,
5620 			ATTRIBUTE15,
5621 			SECURITY_GROUP_ID,
5622 			PROCESS_TYPE,
5623 			CONTROL_ACTIVITY_TYPE
5624             into
5625             l_RL_PROCESS_REV_ID,
5626 			l_SIGNIFICANT_PROCESS_FLAG,
5627 			l_STANDARD_PROCESS_FLAG,
5628 			l_PROCESS_CATEGORY,
5629 			l_STANDARD_VARIATION,
5630 			l_ATTRIBUTE_CATEGORY,
5631 			l_ATTRIBUTE1,
5632 			l_ATTRIBUTE2,
5633 			l_ATTRIBUTE3,
5634 			l_ATTRIBUTE4,
5635 			l_ATTRIBUTE5,
5636 			l_ATTRIBUTE6,
5637 			l_ATTRIBUTE7,
5638 			l_ATTRIBUTE8,
5639 			l_ATTRIBUTE9,
5640 			l_ATTRIBUTE10,
5641 			l_ATTRIBUTE11,
5642 			l_ATTRIBUTE12,
5643 			l_ATTRIBUTE13,
5644 			l_ATTRIBUTE14,
5645 			l_ATTRIBUTE15,
5646 			l_SECURITY_GROUP_ID,
5647 			l_PROCESS_TYPE,
5648 			l_CONTROL_ACTIVITY_TYPE
5649     from amw_process
5650     where process_id = p_process_id
5651 --ko need to sync with approved revision
5652 --              and end_date is null;
5653       and approval_date is not null
5654       and approval_end_date is null;
5655 
5656     FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5657         update amw_process_organization
5658         set    RL_PROCESS_REV_ID             =   l_RL_PROCESS_REV_ID,
5659 			   SIGNIFICANT_PROCESS_FLAG      =   l_SIGNIFICANT_PROCESS_FLAG,
5660 			   STANDARD_PROCESS_FLAG	     =   l_STANDARD_PROCESS_FLAG,
5661 			   PROCESS_CATEGORY_CODE	     =   l_PROCESS_CATEGORY,
5662 			   STANDARD_VARIATION	         =   l_STANDARD_VARIATION,
5663 			   ATTRIBUTE_CATEGORY            =   l_ATTRIBUTE_CATEGORY,
5664 			   ATTRIBUTE1		             =   l_ATTRIBUTE1,
5665 			   ATTRIBUTE2			         =   l_ATTRIBUTE2,
5666 			   ATTRIBUTE3			         =   l_ATTRIBUTE3,
5667 			   ATTRIBUTE4			         =   l_ATTRIBUTE4,
5668 			   ATTRIBUTE5			         =   l_ATTRIBUTE5,
5669 			   ATTRIBUTE6			         =   l_ATTRIBUTE6,
5670 			   ATTRIBUTE7			         =   l_ATTRIBUTE7,
5671 			   ATTRIBUTE8			         =   l_ATTRIBUTE8,
5672 			   ATTRIBUTE9			         =   l_ATTRIBUTE9,
5673 			   ATTRIBUTE10			         =   l_ATTRIBUTE10,
5674 			   ATTRIBUTE11			         =   l_ATTRIBUTE11,
5675 			   ATTRIBUTE12			         =   l_ATTRIBUTE12,
5676 			   ATTRIBUTE13			         =   l_ATTRIBUTE13,
5677 			   ATTRIBUTE14			         =   l_ATTRIBUTE14,
5678 			   ATTRIBUTE15			         =   l_ATTRIBUTE15,
5679 			   SECURITY_GROUP_ID		     =   l_SECURITY_GROUP_ID,
5680 			   PROCESS_TYPE			         =   l_PROCESS_TYPE,
5681 			   CONTROL_ACTIVITY_TYPE	     =   l_CONTROL_ACTIVITY_TYPE
5682         where organization_id = Org_Ids(indx)
5683         and process_id = p_process_id
5684         and end_date is null;
5685 
5686 
5687   -- apply process key accounts -- Delete the accounts that does not exists in the rl process...
5688 
5689  	--ko, delete all the unapproved rows
5690     FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5691         delete amw_acct_associations
5692         where pk1 = Org_Ids(indx)
5693         and   pk2 = p_process_id
5694         and   object_type = 'PROCESS_ORG'
5695         and   approval_date is null;
5696 
5697     -- delete existing accounts that are not found in RL process...
5698     FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5699         update amw_acct_associations
5700         set DELETION_DATE = sysdate
5701         where pk1 = Org_Ids(indx)
5702         and   pk2 = p_process_id
5703         and   object_type = 'PROCESS_ORG'
5704         and   deletion_date is null
5705         and   natural_account_id not in (select natural_account_id
5706         								 from   amw_acct_associations
5707         								 where  pk1 = p_process_id
5708         								 and    object_type = 'PROCESS'
5709         								 and    approval_date is not null
5710         								 and    deletion_approval_date is null);
5711 
5712     FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5713 		insert into amw_acct_associations(
5714 		ACCT_ASSOC_ID,
5715         NATURAL_ACCOUNT_ID,
5716         PK1,
5717         PK2,
5718         STATEMENT_ID,
5719         STATEMENT_LINE_ID,
5720         ORIG_SYSTEM_ACCT_VALUE,
5721         ASSOCIATION_CREATION_DATE,
5722         APPROVAL_DATE,
5723         DELETION_DATE,
5724         DELETION_APPROVAL_DATE,
5725         OBJECT_TYPE,
5726         LAST_UPDATE_DATE,
5727         LAST_UPDATED_BY,
5728         CREATION_DATE,
5729         CREATED_BY,
5730         LAST_UPDATE_LOGIN,
5731         OBJECT_VERSION_NUMBER)
5732         (select
5733         AMW_ACCT_ASSOCIATIONS_S.nextval,
5734         NATURAL_ACCOUNT_ID,
5735         Org_Ids(indx),
5736         PK1,
5737         STATEMENT_ID,
5738         STATEMENT_LINE_ID,
5739         ORIG_SYSTEM_ACCT_VALUE,
5740         sysdate,
5741         null,
5742         null,
5743         null,
5744         'PROCESS_ORG',
5745         sysdate,
5746         G_USER_ID,
5747         sysdate,
5748         G_USER_ID,
5749         G_LOGIN_ID,
5750         1
5751         from amw_acct_associations
5752         where PK1 = p_process_id
5753         and object_type = 'PROCESS'
5754         and approval_date is not null
5755         and deletion_approval_date is null
5756         and NATURAL_ACCOUNT_ID not in (select natural_account_id
5757                                       from amw_acct_associations
5758                                       where pk1 = Org_Ids(indx)
5759         							  and   pk2 = p_process_id
5760         							  and   object_type = 'PROCESS_ORG'
5761         							  and   deletion_date is null));
5762 
5763         FOR indx IN Org_Ids.FIRST .. Org_Ids.LAST LOOP
5764             sync_attachments(p_process_id => p_process_id
5765                 ,p_org_id => Org_Ids(indx)
5766                 ,p_add_upd_flag => 'U');
5767         END LOOP;
5768 
5769         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5770             delete from amw_objective_associations
5771             where pk1 = Org_Ids(indx)
5772             and   pk2 = p_process_id
5773             and   object_type = 'PROCESS_ORG'
5774             and   approval_date is null;
5775 
5776         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5777             update amw_objective_associations
5778             set deletion_date = sysdate
5779             where pk1 = Org_Ids(indx)
5780             and   pk2 = p_process_id
5781             and   object_type = 'PROCESS_ORG'
5782             and   deletion_date is null
5783             and PROCESS_OBJECTIVE_ID not in (select PROCESS_OBJECTIVE_ID
5784             								from amw_objective_associations
5785             								where pk1 = p_process_id
5786             								and object_type = 'PROCESS'
5787             								and approval_date is not null
5788             								and deletion_approval_date is null);
5789 
5790         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
5791             insert into amw_objective_associations
5792                 (OBJECTIVE_ASSOCIATION_ID,
5793                 PROCESS_OBJECTIVE_ID,
5794                 PK1,
5795                 PK2,
5796                 ASSOCIATION_CREATION_DATE,
5797                 APPROVAL_DATE,
5798                 DELETION_DATE,
5799                 DELETION_APPROVAL_DATE,
5800                 OBJECT_TYPE,
5801                 LAST_UPDATE_DATE,
5802                 LAST_UPDATED_BY,
5803                 CREATION_DATE,
5804                 CREATED_BY,
5805                 LAST_UPDATE_LOGIN,
5806                 OBJECT_VERSION_NUMBER)
5807                 (select
5808                 AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
5809                 PROCESS_OBJECTIVE_ID,
5810                 Org_Ids(indx),
5811                 PK1,
5812                 sysdate,
5813                 null,
5814                 null,
5815                 null,
5816                 'PROCESS_ORG',
5817                 sysdate,
5818                 G_USER_ID,
5819                 sysdate,
5820                 G_USER_ID,
5821                 G_LOGIN_ID,
5822                 1
5823                 from amw_objective_associations
5824                 where PK1 = p_process_id
5825                 and object_type = 'PROCESS'
5826                 and approval_date is not null
5827                 and deletion_approval_date is null
5828                 and PROCESS_OBJECTIVE_ID not in (select PROCESS_OBJECTIVE_ID
5829                                                 from amw_objective_associations
5830                                                 where
5831                                                 PK1 = Org_Ids(indx)
5832                                                 and PK2 = p_process_id
5833                                                 and object_type = 'PROCESS_ORG'
5834                                                 and deletion_date is null)
5835                 );
5836 
5837 end;
5838 procedure sync_process_rcm(p_org_id      in number,
5839 				 		   p_process_id  in number,
5840 				           p_sync_rcm    in varchar2
5841 				          ) is
5842 
5843 BEGIN
5844 
5845 	revise_process_if_necessary(p_org_id, p_process_id);
5846 	IF p_sync_rcm = 'SLIB' THEN
5847 		-- Reflect the RCM list to be like that RL process..
5848 		-- 1.First sync up the Risks....
5849 		-- We Don't want the draft associations to linger in the table..So delete them..
5850         delete amw_risk_associations
5851         where pk1 = p_org_id
5852         and   pk2 = p_process_id
5853         and   object_type = 'PROCESS_ORG'
5854         and   approval_date is null;
5855 
5856         update amw_risk_associations
5857         set DELETION_DATE = sysdate
5858         where pk1 = p_org_id
5859         and   pk2 = p_process_id
5860         and   object_type = 'PROCESS_ORG'
5861         and   deletion_date is null
5862         and  risk_id not in (select risk_id
5863         					 from amw_risk_associations
5864         					 where pk1 = p_process_id
5865         					 and object_type = 'PROCESS'
5866         					 and approval_date is not null
5867         					 and deletion_approval_date is null);
5868 		-- Now deleted all the risks that exists in org only but not in rl..Now copy all the risks that exists in rl only and not in org..
5869   		insert into amw_risk_associations
5870         (RISK_ASSOCIATION_ID,
5871         RISK_ID,
5872         PK1,
5873         PK2,
5874         RISK_LIKELIHOOD_CODE,
5875         RISK_IMPACT_CODE,
5876         MATERIAL,
5877         MATERIAL_VALUE,
5878         ASSOCIATION_CREATION_DATE,
5879         APPROVAL_DATE,
5880         DELETION_DATE,
5881         DELETION_APPROVAL_DATE,
5882         OBJECT_TYPE,
5883         LAST_UPDATE_DATE,
5884         LAST_UPDATED_BY,
5885         CREATION_DATE,
5886         CREATED_BY,
5887         LAST_UPDATE_LOGIN,
5888         OBJECT_VERSION_NUMBER)
5889         (select
5890         AMW_RISK_ASSOCIATIONS_S.nextval,
5891         RISK_ID,
5892         p_org_id,
5893         PK1,
5894         RISK_LIKELIHOOD_CODE,
5895         RISK_IMPACT_CODE,
5896         MATERIAL,
5897         MATERIAL_VALUE,
5898         sysdate,
5899         null,
5900         null,
5901         null,
5902         'PROCESS_ORG',
5903         sysdate,
5904         G_USER_ID,
5905         sysdate,
5906         G_USER_ID,
5907         G_LOGIN_ID,
5908         1
5909         from amw_risk_associations
5910         where PK1 = p_process_id
5911         and object_type = 'PROCESS'
5912         and approval_date is not null
5913         and deletion_approval_date is null
5914         and risk_id not in(select risk_id
5915                             from amw_risk_associations
5916                             where pk1 = p_org_id
5917         					and   pk2 = p_process_id
5918         					and   object_type = 'PROCESS_ORG'
5919         					and   deletion_date is null));
5920 
5921         -- SECOND SYNC UP THE CONTROLS.
5922         delete amw_control_associations
5923         where pk1 = p_org_id
5924         and   pk2 = p_process_id
5925         and   object_type = 'RISK_ORG'
5926         and   approval_date is null;
5927 
5928         update amw_control_associations
5929         set DELETION_DATE = sysdate
5930         where pk1 = p_org_id
5931         and   pk2 = p_process_id
5932         and   object_type = 'RISK_ORG'
5933         and   deletion_date is null
5934         and   (pk3, control_id) not in (select pk2, control_id
5935         					 			from amw_control_associations
5936         					 			where pk1 = p_process_id
5937         					 			and object_type = 'RISK'
5938         					 			and approval_date is not null
5939         					 			and deletion_approval_date is null);
5940     	insert into amw_control_associations
5941         (CONTROL_ASSOCIATION_ID,
5942         CONTROL_ID,
5943         PK1,
5944         PK2,
5945         PK3,
5946         ASSOCIATION_CREATION_DATE,
5947         APPROVAL_DATE,
5948         DELETION_DATE,
5949         DELETION_APPROVAL_DATE,
5950         OBJECT_TYPE,
5951         LAST_UPDATE_DATE,
5952         LAST_UPDATED_BY,
5953         CREATION_DATE,
5954         CREATED_BY,
5955         LAST_UPDATE_LOGIN,
5956         OBJECT_VERSION_NUMBER)
5957         (select
5958         AMW_CONTROL_ASSOCIATIONS_S.nextval,
5959         CONTROL_ID,
5960         p_org_id,
5961         PK1,
5962         PK2,
5963         sysdate,
5964         null,
5965         null,
5966         null,
5967         'RISK_ORG',
5968         sysdate,
5969         G_USER_ID,
5970         sysdate,
5971         G_USER_ID,
5972         G_LOGIN_ID,
5973         1
5974         from amw_control_associations
5975         where PK1 = p_process_id
5976         and object_type = 'RISK'
5977         and approval_date is not null
5978         and deletion_approval_date is null
5979         and (pk2, control_id) not in(select pk3,control_id
5980                             from amw_control_associations
5981                             where pk1 = p_org_id
5982         					and   pk2 = p_process_id
5983         					and   object_type = 'RISK_ORG'
5984         					and   deletion_date is null));
5985 	-- THIRD..AUDIT PROCEDURES..
5986 		delete from amw_ap_associations
5987       	where pk1 = p_org_id
5988       	and   pk2 = p_process_id
5989       	and association_creation_date is null
5990       	and   object_type = 'CTRL_ORG';
5991 
5992      	update amw_ap_associations
5993       	set DELETION_DATE = sysdate
5994       	where pk1 = p_org_id
5995       	and   pk2 = p_process_id
5996       	and   object_type = 'CTRL_ORG'
5997       	and   deletion_date is null
5998         and   (pk3, audit_procedure_id) not in (select pk1, audit_procedure_id
5999         					 			from amw_ap_associations
6000         					 			where object_type = 'CTRL'
6001         					 			and approval_date is not null
6002         					 			and deletion_approval_date is null);
6003 		insert into amw_ap_associations
6004         (AP_ASSOCIATION_ID,
6005         AUDIT_PROCEDURE_ID,
6006         PK1,
6007         PK2,
6008         PK3,
6009         DESIGN_EFFECTIVENESS,
6010         OP_EFFECTIVENESS,
6011         ASSOCIATION_CREATION_DATE,
6012         APPROVAL_DATE,
6013         DELETION_DATE,
6014         DELETION_APPROVAL_DATE,
6015         OBJECT_TYPE,
6016         LAST_UPDATE_DATE,
6017         LAST_UPDATED_BY,
6018         CREATION_DATE,
6019         CREATED_BY,
6020         LAST_UPDATE_LOGIN,
6021         OBJECT_VERSION_NUMBER)
6022         (select
6023         AMW_AP_ASSOCIATIONS_S.nextval,
6024         AUDIT_PROCEDURE_ID,
6025         p_org_id,
6026         p_process_id,
6027         PK1,
6028     --ko, the values are pk1 = org, pk2 = process, pk3 = control in the org context.    PK2,
6029         DESIGN_EFFECTIVENESS,
6030         OP_EFFECTIVENESS,
6031         null, --ko commenting.. we set association creation date upon approval of the process..sysdate,
6032         null,
6033         null,
6034         null,
6035         'CTRL_ORG',
6036         sysdate,
6037         G_USER_ID,
6038         sysdate,
6039         G_USER_ID,
6040         G_LOGIN_ID,
6041         1
6042         from amw_ap_associations
6043         where PK1 in --ko, replacing  = with in  controls can be more than one..
6044             (select distinct control_id
6045             from amw_control_associations
6046             where PK1 = p_process_id
6047             and object_type = 'RISK'
6048             and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
6049             and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)))
6050         and object_type = 'CTRL'
6051         and approval_date is not null
6052         and deletion_approval_date is null
6053         and (pk1, audit_procedure_id) not in(select pk3,audit_procedure_id
6054                             from amw_ap_associations
6055                             where pk1 = p_org_id
6056         					and   pk2 = p_process_id
6057         					and   object_type = 'CTRL_ORG'
6058         					and   deletion_date is null));
6059       -- FOURTH..OBJECTIVES...
6060 
6061       	delete amw_objective_associations
6062         where pk1 = p_org_id
6063         and   pk2 = p_process_id
6064 --        and   object_type  IN ('PROCESS_ORG','CONTROL_ORG') ...by dpatel
6065         and   object_type = 'CONTROL_ORG'
6066         and   approval_date is null;
6067 		-- UPDATE CONTROL OBJECTIVES....
6068         update amw_objective_associations
6069         set DELETION_DATE = sysdate
6070         where pk1 = p_org_id
6071         and   pk2 = p_process_id
6072         and   object_type  = 'CONTROL_ORG'
6073         and   deletion_date is null
6074         and  (pk3,pk4,process_objective_id) not in (select pk2,pk3, process_objective_id
6075         					 						from amw_objective_associations
6076         					 						where pk1 = p_process_id
6077         					 						and object_type = 'CONTROL'
6078         					 						and approval_date is not null
6079         					 						and deletion_approval_date is null);
6080 /*...by dpatel
6081        	-- UPDATE PROCESS OBJECTIVES..
6082        	update amw_objective_associations
6083         set DELETION_DATE = sysdate
6084         where pk1 = p_org_id
6085         and   pk2 = p_process_id
6086         and   object_type  = 'PROCESS_ORG'
6087         and   deletion_date is null
6088         and  process_objective_id not in (select process_objective_id
6089         					 			  from amw_objective_associations
6090         					 			  where pk1 = p_process_id
6091         					 			  and object_type = 'PROCESS'
6092         					 			  and approval_date is not null
6093         					 			  and deletion_approval_date is null);
6094 		 -- INSERT PROCESS OBJECTIVES..
6095         insert into amw_objective_associations
6096         (OBJECTIVE_ASSOCIATION_ID,
6097         PROCESS_OBJECTIVE_ID,
6098         PK1,
6099         PK2,
6100         ASSOCIATION_CREATION_DATE,
6101         APPROVAL_DATE,
6102         DELETION_DATE,
6103         DELETION_APPROVAL_DATE,
6104         OBJECT_TYPE,
6105         LAST_UPDATE_DATE,
6106         LAST_UPDATED_BY,
6107         CREATION_DATE,
6108         CREATED_BY,
6109         LAST_UPDATE_LOGIN,
6110         OBJECT_VERSION_NUMBER)
6111         (select
6112         AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6113         PROCESS_OBJECTIVE_ID,
6114         p_org_id,
6115         PK1,
6116         sysdate,
6117         null,
6118         null,
6119         null,
6120         'PROCESS_ORG',
6121         sysdate,
6122         G_USER_ID,
6123         sysdate,
6124         G_USER_ID,
6125         G_LOGIN_ID,
6126         1
6127         from amw_objective_associations
6128         where PK1 = p_process_id
6129         and object_type = 'PROCESS'
6130         and approval_date is not null
6131         and deletion_approval_date is null
6132         and process_objective_id not in(select process_objective_id
6133                             			from amw_objective_associations
6134                             			where pk1 = p_org_id
6135         								and   pk2 = p_process_id
6136         								and   object_type = 'PROCESS_ORG'
6137         								and   deletion_date is null));
6138 */
6139        -- Insert Control Objectives...
6140    	   insert into amw_objective_associations
6141        (OBJECTIVE_ASSOCIATION_ID,
6142        PROCESS_OBJECTIVE_ID,
6143        PK1,
6144        PK2,
6145        PK3,
6146        PK4,
6147        ASSOCIATION_CREATION_DATE,
6148        APPROVAL_DATE,
6149        DELETION_DATE,
6150        DELETION_APPROVAL_DATE,
6151        OBJECT_TYPE,
6152        LAST_UPDATE_DATE,
6153        LAST_UPDATED_BY,
6154        CREATION_DATE,
6155        CREATED_BY,
6156        LAST_UPDATE_LOGIN,
6157        OBJECT_VERSION_NUMBER)
6158        (select
6159        AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6160        PROCESS_OBJECTIVE_ID,
6161        p_org_id,
6162        PK1,
6163        PK2,
6164        pk3,
6165        sysdate,
6166        null,
6167        null,
6168        null,
6169        'CONTROL_ORG',
6170        sysdate,
6171        G_USER_ID,
6172        sysdate,
6173        G_USER_ID,
6174        G_LOGIN_ID,
6175        1
6176        from amw_objective_associations
6177        where object_type = 'CONTROL'
6178        and pk1 = p_process_id
6179        and approval_date is not null
6180        and deletion_approval_date is null
6181        and  (pk2,pk3,process_objective_id) not in (select pk3,pk4, process_objective_id
6182         					 						from amw_objective_associations
6183         					 						where pk1 = p_org_id
6184         											and   pk2 = p_process_id
6185         											and   object_type  = 'CONTROL_ORG'
6186         					 						and   deletion_date is null));
6187 --delete the control objectives which are not present in 'PROCESS_ORG'...by dpatel
6188         update amw_objective_associations
6189         set DELETION_DATE = sysdate
6190         where pk1 = p_org_id
6191         and   pk2 = p_process_id
6192         and   object_type  = 'CONTROL_ORG'
6193         and   deletion_date is null
6194         and  process_objective_id not in (select process_objective_id
6195                             			from amw_objective_associations
6196                             			where pk1 = p_org_id
6197         								and   pk2 = p_process_id
6198         								and   object_type = 'PROCESS_ORG'
6199         								and   deletion_date is null);
6200 
6201 	ELSIF p_sync_rcm = 'ARCM' THEN
6202 
6203 	    -- WE JUST NEED TO ADD THE NEWLY ADDED RISKS/CONTROLS/AUDIT PROCEDURES TO THE PROCESS..
6204 	    -- so Add Risks..
6205 	    insert into amw_risk_associations
6206         (RISK_ASSOCIATION_ID,
6207         RISK_ID,
6208         PK1,
6209         PK2,
6210         RISK_LIKELIHOOD_CODE,
6211         RISK_IMPACT_CODE,
6212         MATERIAL,
6213         MATERIAL_VALUE,
6214         ASSOCIATION_CREATION_DATE,
6215         APPROVAL_DATE,
6216         DELETION_DATE,
6217         DELETION_APPROVAL_DATE,
6218         OBJECT_TYPE,
6219         LAST_UPDATE_DATE,
6220         LAST_UPDATED_BY,
6221         CREATION_DATE,
6222         CREATED_BY,
6223         LAST_UPDATE_LOGIN,
6224         OBJECT_VERSION_NUMBER)
6225         (select
6226         AMW_RISK_ASSOCIATIONS_S.nextval,
6227         RISK_ID,
6228         p_org_id,
6229         PK1,
6230         RISK_LIKELIHOOD_CODE,
6231         RISK_IMPACT_CODE,
6232         MATERIAL,
6233         MATERIAL_VALUE,
6234         sysdate,
6235         null,
6236         null,
6237         null,
6238         'PROCESS_ORG',
6239         sysdate,
6240         G_USER_ID,
6241         sysdate,
6242         G_USER_ID,
6243         G_LOGIN_ID,
6244         1
6245         from amw_risk_associations
6246         where PK1 = p_process_id
6247         and object_type = 'PROCESS'
6248         and approval_date is not null
6249         and deletion_approval_date is null
6250         and risk_id not in(select risk_id
6251                             from  amw_risk_associations
6252                             where pk1 = p_org_id
6253         					and   pk2 = p_process_id
6254         					and   object_type = 'PROCESS_ORG'
6255         					and   deletion_date is null));
6256 		-- Add controls...
6257 		insert into amw_control_associations
6258         (CONTROL_ASSOCIATION_ID,
6259         CONTROL_ID,
6260         PK1,
6261         PK2,
6262         PK3,
6263         ASSOCIATION_CREATION_DATE,
6264         APPROVAL_DATE,
6265         DELETION_DATE,
6266         DELETION_APPROVAL_DATE,
6267         OBJECT_TYPE,
6268         LAST_UPDATE_DATE,
6269         LAST_UPDATED_BY,
6270         CREATION_DATE,
6271         CREATED_BY,
6272         LAST_UPDATE_LOGIN,
6273         OBJECT_VERSION_NUMBER)
6274         (select
6275         AMW_CONTROL_ASSOCIATIONS_S.nextval,
6276         CONTROL_ID,
6277         p_org_id,
6278         PK1,
6279         PK2,
6280         sysdate,
6281         null,
6282         null,
6283         null,
6284         'RISK_ORG',
6285         sysdate,
6286         G_USER_ID,
6287         sysdate,
6288         G_USER_ID,
6289         G_LOGIN_ID,
6290         1
6291         from amw_control_associations
6292         where PK1 = p_process_id
6293         and object_type = 'RISK'
6294         and approval_date is not null
6295         and deletion_approval_date is null
6296         and (pk2, control_id) not in(select pk3,control_id
6297                             from amw_control_associations
6298                             where pk1 = p_org_id
6299         					and   pk2 = p_process_id
6300         					and   object_type = 'RISK_ORG'
6301         					and   deletion_date is null));
6302 		-- NOW AUDIT PROCEDURES...
6303         insert into amw_ap_associations
6304         (AP_ASSOCIATION_ID,
6305         AUDIT_PROCEDURE_ID,
6306         PK1,
6307         PK2,
6308         PK3,
6309         DESIGN_EFFECTIVENESS,
6310         OP_EFFECTIVENESS,
6311         ASSOCIATION_CREATION_DATE,
6312         APPROVAL_DATE,
6313         DELETION_DATE,
6314         DELETION_APPROVAL_DATE,
6315         OBJECT_TYPE,
6316         LAST_UPDATE_DATE,
6317         LAST_UPDATED_BY,
6318         CREATION_DATE,
6319         CREATED_BY,
6320         LAST_UPDATE_LOGIN,
6321         OBJECT_VERSION_NUMBER)
6322         (select
6323         AMW_AP_ASSOCIATIONS_S.nextval,
6324         AUDIT_PROCEDURE_ID,
6325         p_org_id,
6326         p_process_id,
6327         PK1,
6328     --ko, the values are pk1 = org, pk2 = process, pk3 = control in the org context.    PK2,
6329         DESIGN_EFFECTIVENESS,
6330         OP_EFFECTIVENESS,
6331         null, --ko commenting.. we set association creation date upon approval of the process..sysdate,
6332         null,
6333         null,
6334         null,
6335         'CTRL_ORG',
6336         sysdate,
6337         G_USER_ID,
6338         sysdate,
6339         G_USER_ID,
6340         G_LOGIN_ID,
6341         1
6342         from amw_ap_associations
6343         where PK1 in --ko, replacing  = with in  controls can be more than one..
6344             (select distinct control_id
6345             from amw_control_associations
6346             where PK1 = p_process_id
6347             and object_type = 'RISK'
6348             and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
6349             and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)))
6350         and object_type = 'CTRL'
6351         and approval_date is not null
6352         and deletion_approval_date is null
6353         and (pk1, audit_procedure_id) not in(select pk3,audit_procedure_id
6354                             from amw_ap_associations
6355                             where pk1 = p_org_id
6356         					and   pk2 = p_process_id
6357         					and   object_type = 'CTRL_ORG'
6358         					and   deletion_date is null));
6359 /*...by dpatel
6360 		 -- INSERT PROCESS OBJECTIVES..
6361         insert into amw_objective_associations
6362         (OBJECTIVE_ASSOCIATION_ID,
6363         PROCESS_OBJECTIVE_ID,
6364         PK1,
6365         PK2,
6366         ASSOCIATION_CREATION_DATE,
6367         APPROVAL_DATE,
6368         DELETION_DATE,
6369         DELETION_APPROVAL_DATE,
6370         OBJECT_TYPE,
6371         LAST_UPDATE_DATE,
6372         LAST_UPDATED_BY,
6373         CREATION_DATE,
6374         CREATED_BY,
6375         LAST_UPDATE_LOGIN,
6376         OBJECT_VERSION_NUMBER)
6377         (select
6378         AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6379         PROCESS_OBJECTIVE_ID,
6380         p_org_id,
6381         PK1,
6382         sysdate,
6383         null,
6384         null,
6385         null,
6386         'PROCESS_ORG',
6387         sysdate,
6388         G_USER_ID,
6389         sysdate,
6390         G_USER_ID,
6391         G_LOGIN_ID,
6392         1
6393         from amw_objective_associations
6394         where PK1 = p_process_id
6395         and object_type = 'PROCESS'
6396         and approval_date is not null
6397         and deletion_approval_date is null
6398         and process_objective_id not in(select process_objective_id
6399                             			from amw_objective_associations
6400                             			where pk1 = p_org_id
6401         								and   pk2 = p_process_id
6402         								and   object_type = 'PROCESS_ORG'
6403         								and   deletion_date is null));
6404 */
6405        --Insert Control Objectives...by dpatel
6406    	   insert into amw_objective_associations
6407        (OBJECTIVE_ASSOCIATION_ID,
6408        PROCESS_OBJECTIVE_ID,
6409        PK1,
6410        PK2,
6411        PK3,
6412        PK4,
6413        ASSOCIATION_CREATION_DATE,
6414        APPROVAL_DATE,
6415        DELETION_DATE,
6416        DELETION_APPROVAL_DATE,
6417        OBJECT_TYPE,
6418        LAST_UPDATE_DATE,
6419        LAST_UPDATED_BY,
6420        CREATION_DATE,
6421        CREATED_BY,
6422        LAST_UPDATE_LOGIN,
6423        OBJECT_VERSION_NUMBER)
6424        (select
6425        AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6426        PROCESS_OBJECTIVE_ID,
6427        p_org_id,
6428        PK1,
6429        PK2,
6430        pk3,
6431        sysdate,
6432        null,
6433        null,
6434        null,
6435        'CONTROL_ORG',
6436        sysdate,
6437        G_USER_ID,
6438        sysdate,
6439        G_USER_ID,
6440        G_LOGIN_ID,
6441        1
6442        from amw_objective_associations
6443        where object_type = 'CONTROL'
6444        and pk1 = p_process_id
6445        and approval_date is not null
6446        and deletion_approval_date is null
6447        and  (pk2,pk3,process_objective_id) not in (select pk3,pk4, process_objective_id
6448         					 						from amw_objective_associations
6449         					 						where pk1 = p_org_id
6450         											and   pk2 = p_process_id
6451         											and   object_type  = 'CONTROL_ORG'
6452         					 						and   deletion_date is null)
6453 	   and (pk2,pk3) not in (select pk3,pk4 from amw_objective_associations
6454         					 						where pk1 = p_org_id
6455         											and   pk2 = p_process_id
6456         											and   object_type  = 'CONTROL_ORG'
6457         					 						and   deletion_date is null)
6458         );
6459 --delete the control objectives which are not present in 'PROCESS_ORG'...by dpatel
6460         update amw_objective_associations
6461         set DELETION_DATE = sysdate
6462         where pk1 = p_org_id
6463         and   pk2 = p_process_id
6464         and   object_type  = 'CONTROL_ORG'
6465         and   deletion_date is null
6466         and  process_objective_id not in (select process_objective_id
6467                             			from amw_objective_associations
6468                             			where pk1 = p_org_id
6469         								and   pk2 = p_process_id
6470         								and   object_type = 'PROCESS_ORG'
6471         								and   deletion_date is null);
6472 
6473 	END IF;
6474 
6475 END;
6476 
6477 /*
6478     Revise the Risk Library Process RCM with organizations process in one step.
6479 */
6480 procedure sync_process_rcm( p_process_id  in number,
6481 				            p_sync_rcm    in varchar2 )
6482 is
6483 
6484 BEGIN
6485 
6486 	IF p_sync_rcm = 'SLIB' THEN
6487 		-- Reflect the RCM list to be like that RL process..
6488 		-- 1.First sync up the Risks....
6489 		-- We Don't want the draft associations to linger in the table..So delete them..
6490 
6491         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6492             delete amw_risk_associations
6493             where pk1 = Org_Ids(indx)
6494             and   pk2 = p_process_id
6495             and   object_type = 'PROCESS_ORG'
6496             and   approval_date is null;
6497 
6498         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6499             update amw_risk_associations
6500             set DELETION_DATE = sysdate
6501             where pk1 = Org_Ids(indx)
6502             and   pk2 = p_process_id
6503             and   object_type = 'PROCESS_ORG'
6504             and   deletion_date is null
6505             and   risk_id not in (select risk_id
6506 	 				              from amw_risk_associations
6507             					  where pk1 = p_process_id
6508         	       				  and object_type = 'PROCESS'
6509         	   	      			  and approval_date is not null
6510         		      			  and deletion_approval_date is null);
6511 		-- Now deleted all the risks that exists in org only but not in rl..Now copy all the risks that exists in rl only and not in org..
6512         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6513   		    insert into amw_risk_associations
6514             (RISK_ASSOCIATION_ID,
6515             RISK_ID,
6516             PK1,
6517             PK2,
6518             RISK_LIKELIHOOD_CODE,
6519             RISK_IMPACT_CODE,
6520             MATERIAL,
6521             MATERIAL_VALUE,
6522             ASSOCIATION_CREATION_DATE,
6523             APPROVAL_DATE,
6524             DELETION_DATE,
6525             DELETION_APPROVAL_DATE,
6526             OBJECT_TYPE,
6527             LAST_UPDATE_DATE,
6528             LAST_UPDATED_BY,
6529             CREATION_DATE,
6530             CREATED_BY,
6531             LAST_UPDATE_LOGIN,
6532             OBJECT_VERSION_NUMBER)
6533             (select
6534             AMW_RISK_ASSOCIATIONS_S.nextval,
6535             RISK_ID,
6536             Org_Ids(indx),
6537             PK1,
6538             RISK_LIKELIHOOD_CODE,
6539             RISK_IMPACT_CODE,
6540             MATERIAL,
6541             MATERIAL_VALUE,
6542             sysdate,
6543             null,
6544             null,
6545             null,
6546             'PROCESS_ORG',
6547             sysdate,
6548             G_USER_ID,
6549             sysdate,
6550             G_USER_ID,
6551             G_LOGIN_ID,
6552             1
6553             from amw_risk_associations
6554             where PK1 = p_process_id
6555             and object_type = 'PROCESS'
6556             and approval_date is not null
6557             and deletion_approval_date is null
6558             and risk_id not in (select risk_id
6559                                 from amw_risk_associations
6560                                 where pk1 = Org_Ids(indx)
6561         		      			and   pk2 = p_process_id
6562         		  	     		and   object_type = 'PROCESS_ORG'
6563         			     		and   deletion_date is null));
6564 
6565         -- SECOND SYNC UP THE CONTROLS.
6566         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6567             delete  amw_control_associations
6568             where   pk1 = Org_Ids(indx)
6569             and     pk2 = p_process_id
6570             and     object_type = 'RISK_ORG'
6571             and     approval_date is null;
6572 
6573         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6574             update amw_control_associations
6575             set DELETION_DATE = sysdate
6576             where pk1 = Org_Ids(indx)
6577             and   pk2 = p_process_id
6578             and   object_type = 'RISK_ORG'
6579             and   deletion_date is null
6580             and   (pk3, control_id) not in (select pk2, control_id
6581         					 			from amw_control_associations
6582         					 			where pk1 = p_process_id
6583         					 			and object_type = 'RISK'
6584         					 			and approval_date is not null
6585         					 			and deletion_approval_date is null);
6586 
6587         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6588     	   insert into amw_control_associations
6589             (CONTROL_ASSOCIATION_ID,
6590             CONTROL_ID,
6591             PK1,
6592             PK2,
6593             PK3,
6594             ASSOCIATION_CREATION_DATE,
6595             APPROVAL_DATE,
6596             DELETION_DATE,
6597             DELETION_APPROVAL_DATE,
6598             OBJECT_TYPE,
6599             LAST_UPDATE_DATE,
6600             LAST_UPDATED_BY,
6601             CREATION_DATE,
6602             CREATED_BY,
6603             LAST_UPDATE_LOGIN,
6604             OBJECT_VERSION_NUMBER)
6605             (select
6606             AMW_CONTROL_ASSOCIATIONS_S.nextval,
6607             CONTROL_ID,
6608             Org_Ids(indx),
6609             PK1,
6610             PK2,
6611             sysdate,
6612             null,
6613             null,
6614             null,
6615             'RISK_ORG',
6616             sysdate,
6617             G_USER_ID,
6618             sysdate,
6619             G_USER_ID,
6620             G_LOGIN_ID,
6621             1
6622             from amw_control_associations
6623             where PK1 = p_process_id
6624             and object_type = 'RISK'
6625             and approval_date is not null
6626             and deletion_approval_date is null
6627             and (pk2, control_id) not in(select pk3,control_id
6628                             from amw_control_associations
6629                             where pk1 = Org_Ids(indx)
6630         					and   pk2 = p_process_id
6631         					and   object_type = 'RISK_ORG'
6632         					and   deletion_date is null));
6633 
6634 
6635 	-- THIRD..AUDIT PROCEDURES..
6636         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6637             delete from amw_ap_associations
6638             where pk1 = Org_Ids(indx)
6639             and   pk2 = p_process_id
6640             and   association_creation_date is null
6641             and   object_type = 'CTRL_ORG';
6642 
6643         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6644             update amw_ap_associations
6645             set DELETION_DATE = sysdate
6646             where pk1 = Org_Ids(indx)
6647             and   pk2 = p_process_id
6648             and   object_type = 'CTRL_ORG'
6649             and   deletion_date is null
6650             and   (pk3, audit_procedure_id) not in (select pk1, audit_procedure_id
6651         					 			from amw_ap_associations
6652         					 			where object_type = 'CTRL'
6653         					 			and approval_date is not null
6654         					 			and deletion_approval_date is null);
6655 
6656         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6657             insert into amw_ap_associations
6658             (AP_ASSOCIATION_ID,
6659             AUDIT_PROCEDURE_ID,
6660             PK1,
6661             PK2,
6662             PK3,
6663             DESIGN_EFFECTIVENESS,
6664             OP_EFFECTIVENESS,
6665             ASSOCIATION_CREATION_DATE,
6666             APPROVAL_DATE,
6667             DELETION_DATE,
6668             DELETION_APPROVAL_DATE,
6669             OBJECT_TYPE,
6670             LAST_UPDATE_DATE,
6671             LAST_UPDATED_BY,
6672             CREATION_DATE,
6673             CREATED_BY,
6674             LAST_UPDATE_LOGIN,
6675             OBJECT_VERSION_NUMBER)
6676             (select
6677             AMW_AP_ASSOCIATIONS_S.nextval,
6678             AUDIT_PROCEDURE_ID,
6679             Org_Ids(indx),
6680             p_process_id,
6681             PK1,
6682             --ko, the values are pk1 = org, pk2 = process, pk3 = control in the org context.    PK2,
6683             DESIGN_EFFECTIVENESS,
6684             OP_EFFECTIVENESS,
6685             null, --ko commenting.. we set association creation date upon approval of the process..sysdate,
6686             null,
6687             null,
6688             null,
6689             'CTRL_ORG',
6690             sysdate,
6691             G_USER_ID,
6692             sysdate,
6693             G_USER_ID,
6694             G_LOGIN_ID,
6695             1
6696             from amw_ap_associations
6697             where PK1 in --ko, replacing  = with in  controls can be more than one..
6698                 (select distinct control_id
6699                 from amw_control_associations
6700                 where PK1 = p_process_id
6701                 and object_type = 'RISK'
6702                 and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
6703                 and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)))
6704                 and object_type = 'CTRL'
6705                 and approval_date is not null
6706                 and deletion_approval_date is null
6707                 and (pk1, audit_procedure_id) not in(select pk3,audit_procedure_id
6708                             from amw_ap_associations
6709                             where pk1 = Org_Ids(indx)
6710         					and   pk2 = p_process_id
6711         					and   object_type = 'CTRL_ORG'
6712         					and   deletion_date is null));
6713       -- FOURTH..OBJECTIVES...
6714 
6715         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6716           	delete amw_objective_associations
6717             where pk1 = Org_Ids(indx)
6718             and   pk2 = p_process_id
6719             and   object_type = 'CONTROL_ORG'
6720             and   approval_date is null;
6721 
6722 		  -- UPDATE CONTROL OBJECTIVES....
6723         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6724             update amw_objective_associations
6725             set DELETION_DATE = sysdate
6726             where pk1 = Org_Ids(indx)
6727             and   pk2 = p_process_id
6728             and   object_type  = 'CONTROL_ORG'
6729             and   deletion_date is null
6730             and  (pk3,pk4,process_objective_id) not in (select pk2,pk3, process_objective_id
6731         					 						from amw_objective_associations
6732         					 						where pk1 = p_process_id
6733         					 						and object_type = 'CONTROL'
6734         					 						and approval_date is not null
6735         					 						and deletion_approval_date is null);
6736 
6737            -- Insert Control Objectives...
6738         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6739    	       insert into amw_objective_associations
6740            (OBJECTIVE_ASSOCIATION_ID,
6741             PROCESS_OBJECTIVE_ID,
6742             PK1,
6743             PK2,
6744             PK3,
6745             PK4,
6746             ASSOCIATION_CREATION_DATE,
6747             APPROVAL_DATE,
6748             DELETION_DATE,
6749             DELETION_APPROVAL_DATE,
6750             OBJECT_TYPE,
6751             LAST_UPDATE_DATE,
6752             LAST_UPDATED_BY,
6753             CREATION_DATE,
6754             CREATED_BY,
6755             LAST_UPDATE_LOGIN,
6756             OBJECT_VERSION_NUMBER)
6757             (select
6758             AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6759             PROCESS_OBJECTIVE_ID,
6760             Org_Ids(indx),
6761             PK1,
6762             PK2,
6763             pk3,
6764             sysdate,
6765             null,
6766             null,
6767             null,
6768             'CONTROL_ORG',
6769             sysdate,
6770             G_USER_ID,
6771             sysdate,
6772             G_USER_ID,
6773             G_LOGIN_ID,
6774             1
6775             from amw_objective_associations
6776             where object_type = 'CONTROL'
6777             and pk1 = p_process_id
6778             and approval_date is not null
6779             and deletion_approval_date is null
6780             and  (pk2,pk3,process_objective_id) not in (select pk3,pk4, process_objective_id
6781         					 						from amw_objective_associations
6782         					 						where pk1 = Org_Ids(indx)
6783         											and   pk2 = p_process_id
6784         											and   object_type  = 'CONTROL_ORG'
6785         					 						and   deletion_date is null));
6786 
6787         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6788             update amw_objective_associations
6789             set DELETION_DATE = sysdate
6790             where pk1 = Org_Ids(indx)
6791             and   pk2 = p_process_id
6792             and   object_type  = 'CONTROL_ORG'
6793             and   deletion_date is null
6794             and  process_objective_id not in (select process_objective_id
6795                                 			from amw_objective_associations
6796                                 			where pk1 = Org_Ids(indx)
6797             								and   pk2 = p_process_id
6798             								and   object_type = 'PROCESS_ORG'
6799             								and   deletion_date is null);
6800 
6801 	ELSIF p_sync_rcm = 'ARCM' THEN
6802 
6803 	    -- WE JUST NEED TO ADD THE NEWLY ADDED RISKS/CONTROLS/AUDIT PROCEDURES TO THE PROCESS..
6804 	    -- so Add Risks..
6805         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6806         insert into amw_risk_associations
6807         (RISK_ASSOCIATION_ID,
6808         RISK_ID,
6809         PK1,
6810         PK2,
6811         RISK_LIKELIHOOD_CODE,
6812         RISK_IMPACT_CODE,
6813         MATERIAL,
6814         MATERIAL_VALUE,
6815         ASSOCIATION_CREATION_DATE,
6816         APPROVAL_DATE,
6817         DELETION_DATE,
6818         DELETION_APPROVAL_DATE,
6819         OBJECT_TYPE,
6820         LAST_UPDATE_DATE,
6821         LAST_UPDATED_BY,
6822         CREATION_DATE,
6823         CREATED_BY,
6824         LAST_UPDATE_LOGIN,
6825         OBJECT_VERSION_NUMBER)
6826         (select
6827         AMW_RISK_ASSOCIATIONS_S.nextval,
6828         RISK_ID,
6829         Org_Ids(indx),
6830         PK1,
6831         RISK_LIKELIHOOD_CODE,
6832         RISK_IMPACT_CODE,
6833         MATERIAL,
6834         MATERIAL_VALUE,
6835         sysdate,
6836         null,
6837         null,
6838         null,
6839         'PROCESS_ORG',
6840         sysdate,
6841         G_USER_ID,
6842         sysdate,
6843         G_USER_ID,
6844         G_LOGIN_ID,
6845         1
6846         from amw_risk_associations
6847         where PK1 = p_process_id
6848         and object_type = 'PROCESS'
6849         and approval_date is not null
6850         and deletion_approval_date is null
6851         and risk_id not in (select risk_id
6852                             from  amw_risk_associations
6853                             where pk1 = Org_Ids(indx)
6854         					and   pk2 = p_process_id
6855         					and   object_type = 'PROCESS_ORG'
6856         					and   deletion_date is null));
6857 		-- Add controls...
6858         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6859 		insert into amw_control_associations
6860         (CONTROL_ASSOCIATION_ID,
6861         CONTROL_ID,
6862         PK1,
6863         PK2,
6864         PK3,
6865         ASSOCIATION_CREATION_DATE,
6866         APPROVAL_DATE,
6867         DELETION_DATE,
6868         DELETION_APPROVAL_DATE,
6869         OBJECT_TYPE,
6870         LAST_UPDATE_DATE,
6871         LAST_UPDATED_BY,
6872         CREATION_DATE,
6873         CREATED_BY,
6874         LAST_UPDATE_LOGIN,
6875         OBJECT_VERSION_NUMBER)
6876         (select
6877         AMW_CONTROL_ASSOCIATIONS_S.nextval,
6878         CONTROL_ID,
6879         Org_Ids(indx),
6880         PK1,
6881         PK2,
6882         sysdate,
6883         null,
6884         null,
6885         null,
6886         'RISK_ORG',
6887         sysdate,
6888         G_USER_ID,
6889         sysdate,
6890         G_USER_ID,
6891         G_LOGIN_ID,
6892         1
6893         from amw_control_associations
6894         where PK1 = p_process_id
6895         and object_type = 'RISK'
6896         and approval_date is not null
6897         and deletion_approval_date is null
6898         and (pk2, control_id) not in (select pk3,control_id
6899                             from amw_control_associations
6900                             where pk1 = Org_Ids(indx)
6901         					and   pk2 = p_process_id
6902         					and   object_type = 'RISK_ORG'
6903         					and   deletion_date is null));
6904 		-- NOW AUDIT PROCEDURES...
6905         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6906         insert into amw_ap_associations
6907         (AP_ASSOCIATION_ID,
6908         AUDIT_PROCEDURE_ID,
6909         PK1,
6910         PK2,
6911         PK3,
6912         DESIGN_EFFECTIVENESS,
6913         OP_EFFECTIVENESS,
6914         ASSOCIATION_CREATION_DATE,
6915         APPROVAL_DATE,
6916         DELETION_DATE,
6917         DELETION_APPROVAL_DATE,
6918         OBJECT_TYPE,
6919         LAST_UPDATE_DATE,
6920         LAST_UPDATED_BY,
6921         CREATION_DATE,
6922         CREATED_BY,
6923         LAST_UPDATE_LOGIN,
6924         OBJECT_VERSION_NUMBER)
6925         (select
6926         AMW_AP_ASSOCIATIONS_S.nextval,
6927         AUDIT_PROCEDURE_ID,
6928         Org_Ids(indx),
6929         p_process_id,
6930         PK1,
6931     --ko, the values are pk1 = org, pk2 = process, pk3 = control in the org context.    PK2,
6932         DESIGN_EFFECTIVENESS,
6933         OP_EFFECTIVENESS,
6934         null, --ko commenting.. we set association creation date upon approval of the process..sysdate,
6935         null,
6936         null,
6937         null,
6938         'CTRL_ORG',
6939         sysdate,
6940         G_USER_ID,
6941         sysdate,
6942         G_USER_ID,
6943         G_LOGIN_ID,
6944         1
6945         from amw_ap_associations
6946         where PK1 in --ko, replacing  = with in  controls can be more than one..
6947             (select distinct control_id
6948             from amw_control_associations
6949             where PK1 = p_process_id
6950             and object_type = 'RISK'
6951             and (APPROVAL_DATE is not null and APPROVAL_DATE <=  sysdate)
6952             and (DELETION_DATE is null or (DELETION_DATE is not null and DELETION_APPROVAL_DATE is null)))
6953         and object_type = 'CTRL'
6954         and approval_date is not null
6955         and deletion_approval_date is null
6956         and (pk1, audit_procedure_id) not in(select pk3,audit_procedure_id
6957                             from amw_ap_associations
6958                             where pk1 = Org_Ids(indx)
6959         					and   pk2 = p_process_id
6960         					and   object_type = 'CTRL_ORG'
6961         					and   deletion_date is null));
6962 
6963         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
6964        	   insert into amw_objective_associations
6965            (OBJECTIVE_ASSOCIATION_ID,
6966            PROCESS_OBJECTIVE_ID,
6967            PK1,
6968            PK2,
6969            PK3,
6970            PK4,
6971            ASSOCIATION_CREATION_DATE,
6972            APPROVAL_DATE,
6973            DELETION_DATE,
6974            DELETION_APPROVAL_DATE,
6975            OBJECT_TYPE,
6976            LAST_UPDATE_DATE,
6977            LAST_UPDATED_BY,
6978            CREATION_DATE,
6979            CREATED_BY,
6980            LAST_UPDATE_LOGIN,
6981            OBJECT_VERSION_NUMBER)
6982            (select
6983            AMW_OBJECTIVE_ASSOCIATIONS_S.nextval,
6984            PROCESS_OBJECTIVE_ID,
6985            Org_Ids(indx),
6986            PK1,
6987            PK2,
6988            pk3,
6989            sysdate,
6990            null,
6991            null,
6992            null,
6993            'CONTROL_ORG',
6994            sysdate,
6995            G_USER_ID,
6996            sysdate,
6997            G_USER_ID,
6998            G_LOGIN_ID,
6999            1
7000            from amw_objective_associations
7001            where object_type = 'CONTROL'
7002            and pk1 = p_process_id
7003            and approval_date is not null
7004            and deletion_approval_date is null
7005            and  (pk2,pk3,process_objective_id) not in (select pk3,pk4, process_objective_id
7006             					 						from amw_objective_associations
7007             					 						where pk1 = Org_Ids(indx)
7008             											and   pk2 = p_process_id
7009             											and   object_type  = 'CONTROL_ORG'
7010             					 						and   deletion_date is null)
7011     	   and (pk2,pk3) not in (select pk3,pk4 from amw_objective_associations
7012             					 						where pk1 = Org_Ids(indx)
7013             											and   pk2 = p_process_id
7014             											and   object_type  = 'CONTROL_ORG'
7015             					 						and   deletion_date is null)
7016             );
7017         FORALL indx IN Org_Ids.FIRST .. Org_Ids.LAST
7018             update amw_objective_associations
7019             set DELETION_DATE = sysdate
7020             where pk1 = Org_Ids(indx)
7021             and   pk2 = p_process_id
7022             and   object_type  = 'CONTROL_ORG'
7023             and   deletion_date is null
7024             and  process_objective_id not in (select process_objective_id
7025                                 			from amw_objective_associations
7026                                 			where pk1 = Org_Ids(indx)
7027             								and   pk2 = p_process_id
7028             								and   object_type = 'PROCESS_ORG'
7029             								and   deletion_date is null);
7030 
7031 	END IF;
7032 
7033 END;
7034 /* kosriniv...Need to uncomment and change old procedure..
7035 
7036 procedure sync_process_people(p_org_id      in number,
7037 		                      p_process_id  in number,
7038 						 	  p_sync_people in varchar2
7039 						     ) is
7040 
7041 
7042 cursor process_party_list(pid number) is
7043    SELECT      TO_NUMBER(REPLACE(grants.grantee_key,'HZ_PARTY:','')) party_id,
7044         	   granted_menu.menu_name role_name,
7045         	   obj.obj_name object_name,
7046      		   granted_menu.menu_id menu_id,
7047 		   grants.end_date end_date
7048          FROM fnd_grants grants,
7049              fnd_menus granted_menu,
7050              fnd_objects obj
7051          WHERE obj.obj_name = 'AMW_PROCESS_APPR_ETTY'
7052          AND   grants.object_id = obj.object_id
7053          AND   grants.grantee_type ='USER'
7054          AND   grantee_key like 'HZ_PARTY%'
7055          AND   NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7056          AND   grants.menu_id = granted_menu.menu_id
7057          AND   grants.instance_type = 'INSTANCE'
7058          ---06.27.2005 npanandi: bug fix for ADS bug 4458414, passing to_char instead of num
7059 		 ---AND   grants.instance_pk1_value = pid
7060 		 AND   grants.instance_pk1_value = to_char(pid)
7061          AND   grants.instance_pk2_value = '*NULL*'
7062          AND   grants.instance_pk3_value = '*NULL*'
7063          AND   grants.instance_pk4_value = '*NULL*'
7064          AND   grants.instance_pk5_value = '*NULL*'
7065          and   granted_menu.menu_name in ('AMW_RL_PROC_OWNER_ROLE', 'AMW_RL_PROC_FINANCE_OWNER_ROLE', 'AMW_RL_PROC_APPL_OWNER_ROLE');
7066 
7067 cursor org_only_party_list(org_id number, process_id number, org_menu_name varchar2, rl_menu_name varchar2) is
7068    SELECT   grants.grant_guid grant_guid
7069    FROM 	fnd_grants grants,
7070         	fnd_menus granted_menu,
7071             fnd_objects obj
7072    		WHERE obj.obj_name = 'AMW_PROCESS_ORGANIZATION'
7073          AND   grants.object_id = obj.object_id
7074          AND   grants.grantee_type ='USER'
7075          AND   grantee_key like 'HZ_PARTY%'
7076          AND   NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7077          AND   grants.menu_id = granted_menu.menu_id
7078          AND   grants.instance_type = 'INSTANCE'
7079          ---06.27.2005 npanandi: bug fix for ADS bug 4458414, passing to_char instead of num
7080          ---AND   grants.instance_pk1_value = org_id
7081 		 AND   grants.instance_pk1_value = to_char(org_id)
7082 		 ---06.27.2005 npanandi: bug fix for ADS bug 4458414, passing to_char instead of num
7083          ----AND   grants.instance_pk2_value = process_id
7084 		 AND   grants.instance_pk2_value = to_char(process_id)
7085          AND   grants.instance_pk3_value = '*NULL*'
7086          AND   grants.instance_pk4_value = '*NULL*'
7087          AND   grants.instance_pk5_value = '*NULL*'
7088          and   granted_menu.menu_name = org_menu_name
7089          and (grants.grantee_key, obj.object_id ,grants.menu_id ) not in (select grants.grantee_key,obj.object_id, grants.menu_id
7090          	                                                              FROM fnd_grants grants,
7091          																	    fnd_menus granted_menu,
7092          																	    fnd_objects obj
7093          																	WHERE obj.obj_name = 'AMW_PROCESS_APPR_ETTY'
7094          																	AND   grants.object_id = obj.object_id
7095          																	AND   grants.grantee_type ='USER'
7096          																	AND   grantee_key like 'HZ_PARTY%'
7097          																	AND   NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7098          																	AND   grants.menu_id = granted_menu.menu_id
7099          																	AND   grants.instance_type = 'INSTANCE'
7100          																	---06.27.2005 npanandi: bug fix for ADS bug 4458414, passing to_char instead of num
7101          																	---AND   grants.instance_pk1_value = process_id
7102 																			AND   grants.instance_pk1_value = to_char(process_id)
7103          																	AND   grants.instance_pk2_value = '*NULL*'
7104          																	AND   grants.instance_pk3_value = '*NULL*'
7105          																	AND   grants.instance_pk4_value = '*NULL*'
7106          																	AND   grants.instance_pk5_value = '*NULL*'
7107          																	and   granted_menu.menu_name = rl_menu_name);
7108 
7109 
7110 l_return_status varchar2(10);
7111 l_err_code number;
7112 l_msg_data varchar2(4000);
7113 BEGIN
7114     revise_process_if_necessary(p_org_id, p_process_id);
7115 	IF  p_sync_people = 'SLIB' THEN
7116 		-- NOW remove the ORG SPECIFIC PROC OWNERS..
7117 
7118 		FOR powners_list_rec in org_only_party_list(p_org_id,p_process_id,'AMW_ORG_PROC_OWNER_ROLE','AMW_RL_PROC_OWNER_ROLE') LOOP
7119 	  	EXIT WHEN org_only_party_list%NOTFOUND;
7120 
7121 		  AMW_SECURITY_PUB.revoke_grant(
7122 	  								   p_api_version    =>  1,
7123 						     		   p_grant_guid     => 	powners_list_rec.grant_guid,
7124                                        x_return_status  =>  l_return_status,
7125                                        x_errorcode      =>  l_err_code
7126     	                                );
7127 		END LOOP;
7128 
7129 		FOR fowners_list_rec in org_only_party_list(p_org_id,p_process_id,'AMW_ORG_PROC_FIN_OWNER_ROLE','AMW_RL_PROC_FINANCE_OWNER_ROLE') LOOP
7130 	  	EXIT WHEN org_only_party_list%NOTFOUND;
7131         AMW_SECURITY_PUB.revoke_grant(
7132 	  								   p_api_version    =>  1,
7133 						     		   p_grant_guid     => 	fowners_list_rec.grant_guid,
7134                                        x_return_status  =>  l_return_status,
7135                                        x_errorcode      =>  l_err_code
7136                                      );
7137 		END LOOP;
7138 
7139 		FOR aowners_list_rec in org_only_party_list(p_org_id,p_process_id,'AMW_ORG_PROC_APPL_OWNER_ROLE','AMW_RL_PROC_APPL_OWNER_ROLE') LOOP
7140 	  	EXIT WHEN org_only_party_list%NOTFOUND;
7141 
7142 	  	AMW_SECURITY_PUB.revoke_grant(
7143 	  								   p_api_version    =>  1,
7144 						     		   p_grant_guid     => 	aowners_list_rec.grant_guid,
7145                                        x_return_status  =>  l_return_status,
7146                                        x_errorcode      =>  l_err_code
7147         	                          );
7148 		END LOOP;
7149 
7150 	END IF;
7151 
7152 	-- NOW ADD ALL THE ROLES NOW..
7153 
7154 	for party_list_rec in process_party_list(p_process_id) loop
7155 	  exit when process_party_list%notfound;
7156 
7157 	  if party_list_rec.role_name = 'AMW_RL_PROC_OWNER_ROLE' then
7158 
7159               AMW_SECURITY_PUB.grant_role_guid
7160               (
7161                p_api_version           => 1,
7162                p_role_name             => 'AMW_ORG_PROC_OWNER_ROLE',
7163                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
7164                p_instance_type         => 'INSTANCE',
7165                p_instance_set_id       => null,
7166                p_instance_pk1_value    => p_org_id,
7167                p_instance_pk2_value    => p_process_id,
7168                p_instance_pk3_value    => null,
7169                p_instance_pk4_value    => null,
7170                p_instance_pk5_value    => null,
7171                p_party_id              => party_list_rec.party_id,
7172                p_start_date            => sysdate,
7173                p_end_date              => party_list_rec.end_date,
7174                x_return_status         => l_return_status,
7175                x_errorcode             => l_err_code,
7176                x_grant_guid            => l_msg_data);
7177 
7178 	  elsif party_list_rec.role_name = 'AMW_RL_PROC_FINANCE_OWNER_ROLE' then
7179 
7180               AMW_SECURITY_PUB.grant_role_guid
7181               (
7182                p_api_version           => 1,
7183                p_role_name             => 'AMW_ORG_PROC_FIN_OWNER_ROLE',
7184                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
7185                p_instance_type         => 'INSTANCE',
7186                p_instance_set_id       => null,
7187                p_instance_pk1_value    => p_org_id,
7188                p_instance_pk2_value    => p_process_id,
7189                p_instance_pk3_value    => null,
7190                p_instance_pk4_value    => null,
7191                p_instance_pk5_value    => null,
7192                p_party_id              => party_list_rec.party_id,
7193                p_start_date            => sysdate,
7194                p_end_date              => party_list_rec.end_date,
7195                x_return_status         => l_return_status,
7196                x_errorcode             => l_err_code,
7197                x_grant_guid            => l_msg_data);
7198 
7199 	  elsif party_list_rec.role_name = 'AMW_RL_PROC_APPL_OWNER_ROLE' then
7200 
7201               AMW_SECURITY_PUB.grant_role_guid
7202               (
7203                p_api_version           => 1,
7204                p_role_name             => 'AMW_ORG_PROC_APPL_OWNER_ROLE',
7205                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
7206                p_instance_type         => 'INSTANCE',
7207                p_instance_set_id       => null,
7208                p_instance_pk1_value    => p_org_id,
7209                p_instance_pk2_value    => p_process_id,
7210                p_instance_pk3_value    => null,
7211                p_instance_pk4_value    => null,
7212                p_instance_pk5_value    => null,
7213                p_party_id              => party_list_rec.party_id,
7214                p_start_date            => sysdate,
7215                p_end_date              => party_list_rec.end_date,
7216                x_return_status         => l_return_status,
7217                x_errorcode             => l_err_code,
7218                x_grant_guid            => l_msg_data);
7219 
7220 	  end if;
7221 
7222   end loop;
7223 
7224 
7225 END;
7226 */
7227 
7228 procedure sync_process_people(p_org_id      in number,
7229 		                      p_process_id  in number,
7230 						 	  p_sync_people in varchar2
7231 						     ) is
7232 BEGIN
7233     -- psomanat : This step allready done in Syncronize Process.
7234     --revise_process_if_necessary(p_org_id, p_process_id);
7235    	    IF  p_sync_people = 'SLIB' THEN
7236             sync_people_revoke_grant(p_org_id,p_process_id,'AMW_ORG_PROC_OWNER_ROLE','AMW_RL_PROC_OWNER_ROLE');
7237             sync_people_revoke_grant(p_org_id,p_process_id,'AMW_ORG_PROC_FIN_OWNER_ROLE','AMW_RL_PROC_FINANCE_OWNER_ROLE');
7238             sync_people_revoke_grant(p_org_id,p_process_id,'AMW_ORG_PROC_APPL_OWNER_ROLE','AMW_RL_PROC_APPL_OWNER_ROLE');
7239         END IF;
7240         sync_people_add_grant(p_org_id,p_process_id,'AMW_ORG_PROC_OWNER_ROLE','AMW_RL_PROC_OWNER_ROLE');
7241         sync_people_add_grant(p_org_id,p_process_id,'AMW_ORG_PROC_FIN_OWNER_ROLE','AMW_RL_PROC_FINANCE_OWNER_ROLE');
7242         sync_people_add_grant(p_org_id,p_process_id,'AMW_ORG_PROC_APPL_OWNER_ROLE','AMW_RL_PROC_APPL_OWNER_ROLE');
7243 END;
7244 
7245 --******************************************************************************
7246 -- Remove the people with the given grant in risk library from the Organization.
7247 -- p_org_id          : The Organization id
7248 -- p_process_id      : The process in Risk Library whose people are syncronized
7249 --                     to the same process in the given organization.
7250 -- p_org_menu_name   : The Organization Grant Name
7251 -- p_rl_menu_name    : The process Grant Name.
7252 --******************************************************************************
7253 procedure sync_people_revoke_grant(  p_org_id in number,
7254 						 	         p_process_id in number,
7255                                      p_org_menu_name in varchar2,
7256                                      p_rl_menu_name in varchar2 ) is
7257 
7258     TYPE t_grant_guid IS TABLE OF fnd_grants.grant_guid%type;
7259     l_grant_guid t_grant_guid;
7260     l_return_status varchar2(10);
7261     l_err_code number;
7262     l_msg_data varchar2(4000);
7263     l_msg_count	            number;
7264 
7265 
7266 BEGIN
7267     l_grant_guid := t_grant_guid();
7268     l_grant_guid.delete;
7269 
7270     SELECT  grants.grant_guid grant_guid
7271     BULK COLLECT INTO l_grant_guid
7272     FROM 	fnd_grants grants,
7273             fnd_menus granted_menu,
7274             fnd_objects obj
7275     WHERE   obj.obj_name = 'AMW_PROCESS_ORGANIZATION'
7276     AND     grants.object_id = obj.object_id
7277     AND     grants.grantee_type ='USER'
7278     AND     grantee_key like 'HZ_PARTY%'
7279     AND     NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7280     AND     grants.menu_id = granted_menu.menu_id
7281     AND     grants.instance_type = 'INSTANCE'
7282     AND     grants.instance_pk1_value = to_char(p_org_id)
7283     AND     grants.instance_pk2_value = to_char(p_process_id)
7284     AND     grants.instance_pk3_value = '*NULL*'
7285     AND     grants.instance_pk4_value = '*NULL*'
7286     AND     grants.instance_pk5_value = '*NULL*'
7287     and     granted_menu.menu_name = p_org_menu_name
7288     and     (grants.grantee_key) not in (   select  grants.grantee_key
7289                                             FROM    fnd_grants grants,
7290 				                                    fnd_menus granted_menu,
7291                                                     fnd_objects obj
7292                                             WHERE obj.obj_name = 'AMW_PROCESS_APPR_ETTY'
7293          										AND   grants.object_id = obj.object_id
7294          										AND   grants.grantee_type ='USER'
7295          										AND   grantee_key like 'HZ_PARTY%'
7296          										AND   NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7297          										AND   grants.menu_id = granted_menu.menu_id
7298          										AND   grants.instance_type = 'INSTANCE'
7299 												AND   grants.instance_pk1_value = to_char(p_process_id)
7300          										AND   grants.instance_pk2_value = '*NULL*'
7301          										AND   grants.instance_pk3_value = '*NULL*'
7302          										AND   grants.instance_pk4_value = '*NULL*'
7303          										AND   grants.instance_pk5_value = '*NULL*'
7304          										and   granted_menu.menu_name = p_rl_menu_name);
7305 
7306         IF l_grant_guid.exists(1)  THEN
7307             FOR i IN l_grant_guid.FIRST .. l_grant_guid.LAST
7308             LOOP
7309                 AMW_SECURITY_PUB.revoke_grant(
7310 		                  p_api_version    =>  1,
7311                           p_grant_guid     => 	l_grant_guid(i),
7312                           x_return_status  =>  l_return_status,
7313                           x_errorcode      =>  l_err_code
7314                     );
7315             END LOOP;
7316         END IF;
7317 
7318 Exception
7319     when others then
7320         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
7321         fnd_file.put_line(fnd_file.LOG, ' Error in sync_people_revoke_grant '||sqlerrm);
7322         fnd_file.put_line(fnd_file.LOG, l_msg_data);
7323         raise;
7324 END	;
7325 
7326 --******************************************************************************
7327 -- Adds the people with the given grant in risk library from the Organization.
7328 -- p_org_id          : The Organization id
7329 -- p_process_id      : The process in Risk Library whose people are syncronized
7330 --                     to the same process in the given organization.
7331 -- p_org_menu_name   : The Organization Grant Name
7332 -- p_rl_menu_name    : The process Grant Name.
7333 --******************************************************************************
7334 procedure sync_people_add_grant(     p_org_id in number,
7335 						 	         p_process_id in number,
7336                                      p_org_menu_name in varchar2,
7337                                      p_rl_menu_name in varchar2 ) is
7338 
7339     TYPE t_party_id IS TABLE OF number;
7340     TYPE t_end_date IS TABLE OF fnd_grants.end_date%TYPE;
7341 
7342     l_party_id t_party_id;
7343     l_end_date t_end_date;
7344 
7345     l_return_status varchar2(10);
7346     l_err_code number;
7347     l_msg_data varchar2(4000);
7348     l_msg_count	number;
7349 
7350 
7351 BEGIN
7352     l_party_id := t_party_id();
7353     l_end_date := t_end_date();
7354     l_party_id.delete;
7355     l_end_date.delete;
7356 
7357 
7358     SELECT  TO_NUMBER(REPLACE(grants.grantee_key,'HZ_PARTY:','')) party_id,
7359             grants.end_date end_date
7360     BULK COLLECT INTO   l_party_id,
7361                         l_end_date
7362     FROM    fnd_grants grants,
7363             fnd_menus granted_menu,
7364             fnd_objects obj
7365     WHERE   obj.obj_name = 'AMW_PROCESS_APPR_ETTY'
7366     AND     grants.object_id = obj.object_id
7367     AND     grants.grantee_type ='USER'
7368     AND     grantee_key like 'HZ_PARTY%'
7369     AND     NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7370     AND     grants.menu_id = granted_menu.menu_id
7371     AND     grants.instance_type = 'INSTANCE'
7372     AND     grants.instance_pk1_value = to_char(p_process_id)
7373     AND     grants.instance_pk2_value = '*NULL*'
7374     AND     grants.instance_pk3_value = '*NULL*'
7375     AND     grants.instance_pk4_value = '*NULL*'
7376     AND     grants.instance_pk5_value = '*NULL*'
7377     and     granted_menu.menu_name = p_rl_menu_name
7378     and     (grants.grantee_key) not in (   select  grants.grantee_key
7379                                             FROM    fnd_grants grants,
7380          							                fnd_menus granted_menu,
7381                                                     fnd_objects obj
7382          							        WHERE   obj.obj_name = 'AMW_PROCESS_ORGANIZATION'
7383          							        AND     grants.object_id = obj.object_id
7384          							        AND     grants.grantee_type ='USER'
7385          							        AND     grantee_key like 'HZ_PARTY%'
7386          							        AND     NVL(grants.end_date, SYSDATE+1) >= TRUNC(SYSDATE)
7387          							        AND     grants.menu_id = granted_menu.menu_id
7388          							        AND     grants.instance_type = 'INSTANCE'
7389                                             AND     grants.instance_pk1_value = to_char(p_org_id)
7390                                             AND     grants.instance_pk2_value = to_char(p_process_id)
7391          							        AND     grants.instance_pk3_value = '*NULL*'
7392          							        AND     grants.instance_pk4_value = '*NULL*'
7393          							        AND     grants.instance_pk5_value = '*NULL*'
7394          							        and     granted_menu.menu_name = p_org_menu_name );
7395     IF l_party_id.exists(1)  THEN
7396         FOR i IN l_party_id.FIRST .. l_party_id.LAST
7397         LOOP
7398               AMW_SECURITY_PUB.grant_role_guid
7399               (
7400                p_api_version           => 1,
7401                p_role_name             => p_org_menu_name,
7402                p_object_name           => 'AMW_PROCESS_ORGANIZATION',
7403                p_instance_type         => 'INSTANCE',
7404                p_instance_set_id       => null,
7405                p_instance_pk1_value    => p_org_id,
7406                p_instance_pk2_value    => p_process_id,
7407                p_instance_pk3_value    => null,
7408                p_instance_pk4_value    => null,
7409                p_instance_pk5_value    => null,
7410                p_party_id              => l_party_id(i),
7411                p_start_date            => sysdate,
7412                p_end_date              => l_end_date(i),
7413                x_return_status         => l_return_status,
7414                x_errorcode             => l_err_code,
7415                x_grant_guid            => l_msg_data);
7416         END LOOP;
7417     END IF;
7418 
7419 Exception
7420     when others then
7421         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
7422         fnd_file.put_line(fnd_file.LOG, ' Error in sync_people_add_grant '||sqlerrm);
7423         fnd_file.put_line(fnd_file.LOG, l_msg_data);
7424         raise;
7425 
7426 END	;
7427 
7428 /*
7429   Syncronize the Risk Library People with the same process in
7430   all organization in one step.
7431 */
7432 procedure sync_process_people(  p_process_id  in number,
7433 						 	    p_sync_people in varchar2 ) is
7434 	l_return_status varchar2(10);
7435     l_err_code number;
7436     l_msg_data varchar2(4000);
7437     l_msg_count	            number;
7438 BEGIN
7439     FOR indx IN Org_Ids.FIRST .. Org_Ids.LAST
7440     LOOP
7441    	    IF  p_sync_people = 'SLIB' THEN
7442             sync_people_revoke_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_OWNER_ROLE','AMW_RL_PROC_OWNER_ROLE');
7443             sync_people_revoke_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_FIN_OWNER_ROLE','AMW_RL_PROC_FINANCE_OWNER_ROLE');
7444             sync_people_revoke_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_APPL_OWNER_ROLE','AMW_RL_PROC_APPL_OWNER_ROLE');
7445         END IF;
7446         sync_people_add_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_OWNER_ROLE','AMW_RL_PROC_OWNER_ROLE');
7447         sync_people_add_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_FIN_OWNER_ROLE','AMW_RL_PROC_FINANCE_OWNER_ROLE');
7448         sync_people_add_grant(Org_Ids(indx),p_process_id,'AMW_ORG_PROC_APPL_OWNER_ROLE','AMW_RL_PROC_APPL_OWNER_ROLE');
7449     END LOOP;
7450 EXCEPTION
7451     when others then
7452         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
7453         fnd_file.put_line(fnd_file.LOG, ' Error in sync_process_people '||sqlerrm);
7454         fnd_file.put_line(fnd_file.LOG, l_msg_data);
7455         raise;
7456 END;
7457 
7458 /*
7459 Synchornization Parameters
7460 ==========================
7461 
7462 1. p_org_id          - Organization Id
7463 
7464 2. p_process_id      - Process Id
7465 
7466 3. p_sync_mode
7467 	'PSUBP'  - Current Process and its Sub Processes
7468 	'PONLY'   - Current Process Only.
7469 
7470 4. p_sync_hierarchy
7471 	'NO'         - Retain Definition In the Organization.. Do not change the hierarchy
7472 	'YES'         - Synchronize with the library definition..Hierarchy Made equivalent to the Risk Library
7473 
7474 5 p_sync_attributes
7475 	'YES'        - Synchronize the process attributes..(attributes, keyaccounts, attachments)
7476 	'NO'         - Do not change...
7477 
7478 6. p_sync_rcm
7479 	'RDEF'		  - Retain Definition in the organization.. Do not make any changes to Risks, controls and Audit Procedures.
7480 	'SLIB' 		  - Synchronize with the library definition .. Risks, Controls and Audit Procedures list equal to the RL
7481 	'ARCM'		  - Add Risks and Controls and Audit Procedures that exists in RL but not in Org.
7482 
7483 7. p_sync_people
7484 	'RDEF'		  -  Retain Definition In the Organization.. Do no make any changes to People list
7485 	'SLIB'        -  Synchronize with the library definition...Make Equal to the RL list
7486 	'APPL'		  -  Add Process People.
7487 */
7488 procedure Synchronize_process(
7489 				p_org_id   in number,
7490 				p_process_id  in number,
7491 				p_sync_mode in varchar2,
7492 				p_sync_hierarchy in varchar2,
7493 				p_sync_attributes in varchar2,
7494 				p_sync_rcm in varchar2,
7495 				p_sync_people in varchar2
7496 			) is
7497 
7498  	cursor c1 (l_pid number) is
7499     select ah.child_id child_process_id
7500       from amw_approved_hierarchies ah
7501       where ah.parent_id = (select pp.process_id
7502                             from amw_process pp
7503                             where pp.process_id = ah.parent_id
7504                             and pp.approval_date is not null
7505                             and pp.approval_end_date is null
7506                             and pp.deletion_date is null)
7507        and ah.child_id  =  ( select Cp.process_id
7508                             from amw_process Cp
7509                             where Cp.process_id = ah.child_id
7510                             and Cp.approval_date is not null
7511                             and Cp.approval_end_date is null
7512                             and Cp.deletion_date is null)
7513        and ah.start_date is not null
7514        and ah.end_date is null
7515        and ah.organization_id = -1
7516        and ah.parent_id = l_pid;
7517 
7518     c1_rec c1%rowtype;
7519     l_child_process_id number;
7520     l_dummy number;
7521   l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
7522   l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
7523   pending_approval_exception exception;
7524   l_approval_status varchar2(1);
7525   l_proc_latest FND_ATTACHED_DOCUMENTS.pk1_value%type;
7526   l_proc_prev FND_ATTACHED_DOCUMENTS.pk1_value%type;
7527 
7528 BEGIN
7529 
7530 -- synchronization is only done to a process when it exists in the organization..
7531 	if( l_log_stmt_level >= l_curr_log_level ) then
7532     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7533         'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.begin',
7534         ' OrgId:' ||p_org_id || ';ProcessId:'||p_process_id
7535         ||';p_sync_mode:'||p_sync_mode||';p_sync_hierarchy:'||p_sync_hierarchy||';p_sync_attributes:'||p_sync_attributes
7536         ||';p_sync_rcm:'||p_sync_rcm||';p_sync_people:'||p_sync_people);
7537 	end if;
7538 
7539     IF p_sync_hierarchy = 'NO' AND p_sync_attributes = 'NO' AND p_sync_rcm = 'RDEF' AND p_sync_people = 'RDEF' THEN
7540     	if( l_log_stmt_level >= l_curr_log_level ) then
7541     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7542         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.End;',
7543         	'End');
7544 		end if;
7545 		return;
7546 	END IF;
7547 
7548 	SELECT 1 INTO l_dummy
7549 	from amw_process_organization
7550 	where organization_id = p_org_id
7551 	and process_id = p_process_id
7552 	and end_date is null
7553 	and deletion_date is null;
7554 
7555 	if( l_log_stmt_level >= l_curr_log_level ) then
7556     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7557         'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.produce_err_if_pa_or_locked',
7558         ' OrgId:' ||p_org_id || ';ProcessId:'||p_process_id );
7559 	end if;
7560 	produce_err_if_pa_or_locked(p_org_id, p_process_id);
7561 	if( l_log_stmt_level >= l_curr_log_level ) then
7562     	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7563         'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.revise_process_if_necessary',
7564         ' OrgId:' ||p_org_id || ';ProcessId:'||p_process_id );
7565 	end if;
7566 
7567 	--attachment bug5968299 codefix start
7568 
7569 	select approval_status
7570         into l_approval_status
7571         from amw_process_organization
7572         where process_id = p_process_id
7573         and organization_id = p_org_id
7574         and end_date is null
7575         and  deletion_date is null;
7576         /*the approval status should be known so that
7577         when a new revision is created while synchronising
7578         and if process attributes are not synchronised the attachments
7579         should be copied from previour revision to the latest revision*/
7580 
7581 
7582 
7583 	--attachment bug5968299 codefix end
7584 
7585 	revise_process_if_necessary(p_org_id, p_process_id);
7586 
7587 		/*the following case will happen when
7588                  process attributes are not synchronised .
7589                  In that case we have to copy attachments from
7590                  latest approved revision to the latest revision in the organization */
7591 	IF p_sync_attributes = 'NO' THEN
7592 		if( l_log_stmt_level >= l_curr_log_level ) then
7593     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7594         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.sync_process_attributes(bug5962899)',
7595         	p_org_id || p_process_id );
7596 		end if;
7597 		if l_approval_status='A' then
7598 		    select process_org_rev_id into l_proc_latest
7599 		    from amw_process_organization
7600                     where process_id = p_process_id
7601                     and organization_id = p_org_id
7602                     and end_date is null;
7603 
7604                     select process_org_rev_id into l_proc_prev
7605 	   	    from amw_process_organization
7606                     where process_id = p_process_id
7607                     and organization_id = p_org_id
7608                     and approval_date is not null
7609                     and approval_end_date is null;
7610 
7611                     FND_ATTACHED_DOCUMENTS2_PKG.copy_attachments(X_from_entity_name => 'AMW_PROCESS_ORGANIZATION'
7612                    ,X_from_pk1_value => l_proc_prev
7613                    ,X_to_entity_name => 'AMW_PROCESS_ORGANIZATION'
7614                    ,X_to_pk1_value => l_proc_latest
7615                    ,X_created_by => G_USER_ID
7616                    ,X_last_update_login => G_LOGIN_ID
7617                     );
7618 
7619 
7620 
7621 		end if;
7622 
7623 
7624    	END IF;
7625 
7626 	IF p_sync_attributes = 'YES' THEN
7627 		if( l_log_stmt_level >= l_curr_log_level ) then
7628     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7629         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.sync_process_attributes',
7630         	p_org_id || p_process_id );
7631 		end if;
7632 		sync_process_attributes(p_org_id      => p_org_id,
7633 								p_process_id  => p_process_id			-- Sync up the attributes also..
7634 								);
7635 	END IF;
7636 
7637 
7638 	IF p_sync_rcm = 'SLIB' or p_sync_rcm = 'ARCM' THEN
7639 		if( l_log_stmt_level >= l_curr_log_level ) then
7640     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7641         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.sync_process_rcm',
7642         	p_org_id || p_process_id ||p_sync_rcm);
7643 		end if;
7644 		sync_process_rcm(p_org_id      => p_org_id,
7645 						 p_process_id  => p_process_id,
7646 						 p_sync_rcm => p_sync_rcm				-- Sync up the RCM ...
7647 						 );
7648 	END IF;
7649 
7650 
7651 	IF p_sync_people = 'SLIB' or p_sync_people = 'APPL' THEN
7652 		if( l_log_stmt_level >= l_curr_log_level ) then
7653     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7654         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.sync_process_people',
7655         	p_org_id || p_process_id ||p_sync_people);
7656 		end if;
7657 		sync_process_people(p_org_id      => p_org_id,
7658 						 	p_process_id  => p_process_id,
7659 						 	p_sync_people => p_sync_people				-- Sync up the PEOPLE...
7660 						   );
7661 	END IF;
7662 
7663 
7664 	IF p_sync_mode = 'PSUBP' THEN
7665 		IF p_sync_hierarchy = 'YES' THEN
7666 			if( l_log_stmt_level >= l_curr_log_level ) then
7667     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7668         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.synchronize_hierarchy',
7669         	p_org_id || p_process_id );
7670 		end if;
7671 			synchronize_hierarchy( p_org_id      => p_org_id,
7672                                   p_parent_process_id  => p_process_id,
7673                                   p_sync_attributes => p_sync_attributes,
7674                                   p_sync_rcm => p_sync_rcm,
7675                                   p_sync_people => p_sync_people );
7676 	    ELSE
7677 	    	-- For each of the child in the RL process, Call Synchronize_process
7678 
7679 	    	for c1_rec in c1(p_process_id) loop
7680 	  		exit when c1%notfound;
7681         	l_child_process_id := c1_rec.child_process_id;
7682 			if( l_log_stmt_level >= l_curr_log_level ) then
7683     			FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7684         		'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.synchronize_process',
7685         		'child_id:' || l_child_process_id );
7686 			end if;
7687 	    	synchronize_process(p_org_id      => p_org_id,
7688                                   p_process_id  => l_child_process_id,
7689                                   p_sync_mode   => p_sync_mode,
7690 								  p_sync_hierarchy =>p_sync_hierarchy,
7691                                   p_sync_attributes => p_sync_attributes,
7692                                   p_sync_rcm => p_sync_rcm,
7693                                   p_sync_people => p_sync_people );
7694 		    end loop;
7695 
7696 
7697         END IF;
7698     END IF;
7699     if( l_log_stmt_level >= l_curr_log_level ) then
7700     		FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7701         	'amw.plsql.AMW_ORG_HIERARCHY_PKG.SYNCHRONIZE_PROCESS.End;',
7702         	'End');
7703 	end if;
7704 
7705 EXCEPTION
7706 	when pending_approval_exception then
7707 	    raise;
7708 	WHEN no_data_found THEN
7709         return;
7710 
7711 END;
7712 /*
7713     Synchronizes the Risk Library Process with the Process in all organization.
7714 */
7715 procedure Synchronize_process(
7716 				p_process_id  in number,
7717 				p_sync_mode in varchar2,
7718 				p_sync_hierarchy in varchar2,
7719 				p_sync_attributes in varchar2,
7720 				p_sync_rcm in varchar2,
7721 				p_sync_people in varchar2
7722 			) is
7723 
7724  	cursor c1 (l_pid number) is
7725     select ah.child_id child_process_id
7726       from amw_approved_hierarchies ah
7727       where ah.parent_id = (select pp.process_id
7728                             from amw_process pp
7729                             where pp.process_id = ah.parent_id
7730                             and pp.approval_date is not null
7731                             and pp.approval_end_date is null
7732                             and pp.deletion_date is null)
7733        and ah.child_id  =  ( select Cp.process_id
7734                             from amw_process Cp
7735                             where Cp.process_id = ah.child_id
7736                             and Cp.approval_date is not null
7737                             and Cp.approval_end_date is null
7738                             and Cp.deletion_date is null)
7739        and ah.start_date is not null
7740        and ah.end_date is null
7741        and ah.organization_id = -1
7742        and ah.parent_id = l_pid;
7743 
7744     c1_rec c1%rowtype;
7745     l_child_process_id number;
7746     l_dummy number;
7747   l_curr_log_level number := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
7748   l_log_stmt_level number := FND_LOG.LEVEL_STATEMENT;
7749   pending_approval_exception exception;
7750     l_msg_data              varchar2(4000);
7751     l_msg_count	            number;
7752 
7753 BEGIN
7754     -- This to be handled later
7755 	-- produce_err_if_pa_or_locked(p_org_id, p_process_id);
7756 
7757 	revise_process_if_necessary(p_process_id);
7758 
7759 	IF p_sync_attributes = 'YES' THEN
7760 		sync_process_attributes(p_process_id  => p_process_id);
7761 	END IF;
7762 
7763 	IF p_sync_rcm = 'SLIB' or p_sync_rcm = 'ARCM' THEN
7764 		sync_process_rcm(p_process_id  => p_process_id,
7765 						 p_sync_rcm    => p_sync_rcm				-- Sync up the RCM ...
7766                 );
7767 	END IF;
7768 
7769 	IF p_sync_people = 'SLIB' or p_sync_people = 'APPL' THEN
7770 		sync_process_people(p_process_id  => p_process_id,
7771 						 	p_sync_people => p_sync_people				-- Sync up the PEOPLE...
7772 						   );
7773 	END IF;
7774 
7775 	IF p_sync_mode = 'PSUBP' THEN
7776 		IF p_sync_hierarchy = 'YES' THEN
7777 			synchronize_hierarchy( p_parent_process_id  => p_process_id,
7778                                    p_sync_attributes => p_sync_attributes,
7779                                    p_sync_rcm => p_sync_rcm,
7780                                    p_sync_people => p_sync_people );
7781 	    ELSE
7782 	    	-- For each of the child in the RL process, Call Synchronize_process
7783 	    	for c1_rec in c1(p_process_id) loop
7784 	  		exit when c1%notfound;
7785         	l_child_process_id := c1_rec.child_process_id;
7786 	    	synchronize_process(  p_process_id  => l_child_process_id,
7787                                   p_sync_mode   => p_sync_mode,
7788 								  p_sync_hierarchy =>p_sync_hierarchy,
7789                                   p_sync_attributes => p_sync_attributes,
7790                                   p_sync_rcm => p_sync_rcm,
7791                                   p_sync_people => p_sync_people );
7792 		    end loop;
7793         END IF;
7794     END IF;
7795 EXCEPTION
7796 	when pending_approval_exception then
7797         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
7798         fnd_file.put_line(fnd_file.LOG, ' Error in Revision '||sqlerrm);
7799         fnd_file.put_line(fnd_file.LOG, l_msg_data);
7800 
7801 	   raise;
7802 	WHEN no_data_found THEN
7803         FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
7804         fnd_file.put_line(fnd_file.LOG, ' Error in Revision '||sqlerrm);
7805         fnd_file.put_line(fnd_file.LOG, l_msg_data);
7806 
7807         return;
7808 END;
7809 
7810 procedure sync_proc_organizations(
7811 p_process_id			in number,
7812 p_org_id_string			in varchar2,
7813 p_sync_mode 			in varchar2,
7814 p_sync_hierarchy 		in varchar2,
7815 p_sync_attributes 		in varchar2,
7816 p_sync_rcm 				in varchar2,
7817 p_sync_people 			in varchar2,
7818 p_commit			    in varchar2 := FND_API.G_FALSE,
7819 p_validation_level		IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
7820 p_init_msg_list			IN VARCHAR2 := FND_API.G_FALSE,
7821 x_return_status			out nocopy varchar2,
7822 x_msg_count			    out nocopy number,
7823 x_msg_data			    out nocopy varchar2 )is
7824 
7825 
7826 cursor c1 (pid number) is
7827         select parent_child_id process_to_count
7828         from amw_proc_hierarchy_denorm
7829         where process_id = pid
7830         and up_down_ind = 'D'
7831         and hierarchy_type = 'A';
7832 
7833 L_API_NAME CONSTANT VARCHAR2(30) := 'sync_proc_organizations';
7834 
7835 str              varchar2(4000);
7836 diff		 number;
7837 orgstr		 varchar2(100);
7838 l_org_string     varchar2(4000);
7839 orgid		 number;
7840 
7841 
7842 begin
7843 
7844   x_return_status := FND_API.G_RET_STS_SUCCESS;
7845   IF FND_API.to_Boolean( p_init_msg_list )  THEN
7846      FND_MSG_PUB.initialize;
7847   END IF;
7848   IF FND_GLOBAL.User_Id IS NULL THEN
7849     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
7850     RAISE FND_API.G_EXC_ERROR;
7851   END IF;
7852 
7853 
7854 l_org_string := p_org_id_string;
7855 while LENGTH(l_org_string) <> 0 loop
7856 select LTRIM(l_org_string, '1234567890') into str from dual;
7857 diff := LENGTH(l_org_string) - LENGTH(str);
7858 if  LENGTH(str) is null then  diff := LENGTH(l_org_string); end if;
7859 select SUBSTR(l_org_string, 1, diff) into orgstr from dual;
7860 orgid := to_number(orgstr);
7861 
7862 	synchronize_process(p_org_id      => orgid,
7863                         p_process_id  => p_process_id,
7864                         p_sync_mode   => p_sync_mode,
7865 						p_sync_hierarchy =>p_sync_hierarchy,
7866                         p_sync_attributes => p_sync_attributes,
7867                         p_sync_rcm => p_sync_rcm,
7868                         p_sync_people => p_sync_people );
7869 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
7870 /*
7871 --Ko Update the Proc_org_hierarchy_denorm tables..
7872   AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => orgid);
7873 */
7874   -- ko update the risk1 counts of the child process..
7875   upd_ltst_risk_count(p_org_id => orgid, p_process_id => null);
7876 
7877   upd_ltst_control_count(p_org_id => orgid, p_process_id => null);
7878 
7879 
7880 
7881 
7882 select LTRIM(str, 'x') into l_org_string from dual;
7883 end loop;
7884 
7885 -- update the org counts of the child process and its hierarchy....
7886 	for descendents_rec in c1(p_process_id) loop
7887      		exit when c1%notfound;
7888       		amw_rl_hierarchy_pkg.update_org_count(descendents_rec.process_to_count);
7889  	end loop;
7890 
7891 exception
7892 
7893   WHEN FND_API.G_EXC_ERROR THEN
7894      ROLLBACK;
7895      x_return_status := FND_API.G_RET_STS_ERROR;
7896      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
7897 
7898   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7899      ROLLBACK;
7900      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7901      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
7902 
7903   WHEN OTHERS THEN
7904      ROLLBACK;
7905      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7906      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7907      THEN
7908         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
7909      END IF;
7910      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count, p_data => x_msg_data);
7911 
7912 end sync_proc_organizations;
7913 
7914 
7915 procedure reset_count(
7916 			 errbuf     out nocopy  varchar2,
7917 			retcode    out nocopy  varchar2,
7918 			p_org_id in number
7919 			) is
7920 conc_status boolean;
7921 
7922 cursor all_orgs is
7923 select distinct organization_id
7924 from amw_process_organization
7925 where process_id = -2;
7926 
7927 begin
7928 
7929 	retcode :=0;
7930 	errbuf :='';
7931 	if p_org_id is null then
7932 		for org_cursor in all_orgs loop
7933 			exit when all_orgs%notfound;
7934 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
7935 /*
7936 			--updates latest hier denorm
7937 			amw_rl_hierarchy_pkg.update_denorm (org_cursor.organization_id, sysdate);
7938 			--updates approved hier denorm
7939 			amw_rl_hierarchy_pkg.update_approved_denorm (org_cursor.organization_id, sysdate);
7940 */
7941 			update amw_process_organization
7942 			set risk_count = null,
7943 			control_count = null,
7944 			risk_count_latest = null,
7945 			control_count_latest = null
7946 			where organization_id = org_cursor.organization_id;
7947 
7948 			-- update latest risk and control counts
7949 
7950 			upd_ltst_risk_count(p_org_id => org_cursor.organization_id, p_process_id => null);
7951 
7952 			upd_ltst_control_count(p_org_id => org_cursor.organization_id, p_process_id => null);
7953 
7954 			-- update latest risk and control counts
7955 
7956 			upd_appr_risk_count(p_org_id => org_cursor.organization_id, p_process_id => null);
7957 
7958 			upd_appr_control_count(p_org_id => org_cursor.organization_id, p_process_id => null);
7959 		end loop;
7960 
7961 	else
7962 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
7963 /*
7964 		--updates latest hier denorm
7965 		amw_rl_hierarchy_pkg.update_denorm (p_org_id, sysdate);
7966 		--updates approved hier denorm
7967 		amw_rl_hierarchy_pkg.update_approved_denorm (p_org_id, sysdate);
7968 */
7969 		update amw_process_organization
7970 		set risk_count = null,
7971 		control_count = null,
7972 		risk_count_latest = null,
7973 		control_count_latest = null
7974 		where organization_id = p_org_id;
7975 
7976 		-- update latest risk and control counts
7977 
7978 		upd_ltst_risk_count(p_org_id => p_org_id, p_process_id => null);
7979 
7980 		upd_ltst_control_count(p_org_id => p_org_id, p_process_id => null);
7981 
7982 		-- update latest risk and control counts
7983 
7984 		upd_appr_risk_count(p_org_id => p_org_id, p_process_id => null);
7985 
7986 		upd_appr_control_count(p_org_id => p_org_id, p_process_id => null);
7987 	end if;
7988 
7989 	commit;
7990 exception
7991 	when others then
7992 		rollback;
7993 		retcode :=2;
7994 		errbuf :=SUBSTR(SQLERRM,1,1000);
7995 		conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
7996 
7997 end reset_count;
7998 
7999 procedure delete_activities(p_parent_process_id in number,
8000 						    p_organization_id in number,
8001 			   				p_child_id_string in varchar2,
8002   						 	p_init_msg_list	IN VARCHAR2 := FND_API.G_FALSE,
8003 	                        x_return_status out nocopy varchar2,
8004                             x_msg_count out nocopy number,
8005                             x_msg_data out nocopy varchar2)
8006 is
8007   l_api_name constant varchar2(30) := 'delete_activities';
8008   str              varchar2(4000);
8009   diff		 	 number;
8010   childstr		 varchar2(100);
8011   l_child_string   varchar2(4000);
8012   l_child_id		 number;
8013 
8014 begin
8015 
8016   x_return_status := FND_API.G_RET_STS_SUCCESS;
8017   IF FND_API.to_Boolean( p_init_msg_list )  THEN
8018      FND_MSG_PUB.initialize;
8019   END IF;
8020   if FND_GLOBAL.user_id is null then
8021      AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8022      raise FND_API.G_EXC_ERROR;
8023   end if;
8024 
8025   --check if parent_process_id is null
8026   if p_parent_process_id is null or p_organization_id is null then
8027      raise FND_API.G_EXC_ERROR;
8028   end if;
8029 
8030   l_child_string :=  p_child_id_string;
8031   while LENGTH(l_child_string) <> 0 loop
8032     select LTRIM(l_child_string, '1234567890') into str from dual;
8033     diff := LENGTH(l_child_string) - LENGTH(str);
8034     if  LENGTH(str) is null then
8035       diff := LENGTH(l_child_string);
8036     end if;
8037     select SUBSTR(l_child_string, 1, diff) into childstr from dual;
8038     l_child_id := to_number(childstr);
8039 
8040     delete from amw_latest_hierarchies
8041     where parent_id = p_parent_process_id
8042     and   child_id  = l_child_id
8043     and organization_id = p_organization_id;
8044 
8045     select LTRIM(str, 'x') into l_child_string from dual;
8046   end loop;
8047 exception
8048   WHEN FND_API.G_EXC_ERROR THEN
8049      ROLLBACK;
8050      x_return_status := FND_API.G_RET_STS_ERROR;
8051      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8052 
8053   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8054      ROLLBACK;
8055      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8056      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8057 
8058   WHEN OTHERS THEN
8059      ROLLBACK;
8060      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8061      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8062      THEN
8063         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
8064      END IF;
8065      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count, p_data => x_msg_data);
8066 end delete_activities;
8067 
8068 procedure add_org_activities(p_parent_process_id in number,
8069 						    p_organization_id in number,
8070 			   				p_child_id_string in varchar2,
8071   						 	p_init_msg_list	IN VARCHAR2 := FND_API.G_FALSE,
8072 	                        x_return_status out nocopy varchar2,
8073                             x_msg_count out nocopy number,
8074                             x_msg_data out nocopy varchar2)
8075 is
8076   l_api_name constant varchar2(30) := 'add_org_activities';
8077   str              varchar2(4000);
8078   diff		 	 number;
8079   childstr		 varchar2(100);
8080   l_child_string   varchar2(4000);
8081   l_child_id		 number;
8082   l_child_order_num amw_latest_hierarchies.child_order_number%type;
8083 
8084 begin
8085   x_return_status := FND_API.G_RET_STS_SUCCESS;
8086   IF FND_API.to_Boolean( p_init_msg_list )  THEN
8087      FND_MSG_PUB.initialize;
8088   END IF;
8089   if FND_GLOBAL.user_id is null then
8090      AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8091      raise FND_API.G_EXC_ERROR;
8092   end if;
8093 
8094   --check if parent_process_id is null
8095   if p_parent_process_id is null or p_organization_id is null then
8096      raise FND_API.G_EXC_ERROR;
8097   end if;
8098 
8099   l_child_string :=  p_child_id_string;
8100   while LENGTH(l_child_string) <> 0 loop
8101     select LTRIM(l_child_string, '1234567890') into str from dual;
8102     diff := LENGTH(l_child_string) - LENGTH(str);
8103     if  LENGTH(str) is null then
8104       diff := LENGTH(l_child_string);
8105     end if;
8106     select SUBSTR(l_child_string, 1, diff) into childstr from dual;
8107     l_child_id := to_number(childstr);
8108 
8109     produce_err_if_circular(
8110 	p_org_id => p_organization_id,
8111 	p_parent_process_id => p_parent_process_id,
8112     p_child_process_id => l_child_id);
8113 
8114     insert into amw_latest_hierarchies
8115     (ORGANIZATION_ID, PARENT_ID, CHILD_ID, CHILD_ORDER_NUMBER, LAST_UPDATE_DATE, LAST_UPDATED_BY, LAST_UPDATE_LOGIN, CREATION_DATE, CREATED_BY, object_version_number)
8116     VALUES
8117     (p_organization_id,p_parent_process_id,l_child_id,AMW_ORG_CHILD_ORDER_S.nextval, sysdate, G_USER_ID, G_LOGIN_ID, sysdate, G_USER_ID, 1)
8118     returning                CHILD_ORDER_NUMBER
8119     into                     l_child_order_num;
8120 
8121     AMW_RL_HIERARCHY_PKG.update_appr_ch_ord_num_if_reqd
8122                 (p_org_id      =>  p_organization_id,
8123                  p_parent_id   =>  p_parent_process_id,
8124                  p_child_id    =>  l_child_id,
8125                  p_instance_id =>  l_child_order_num);
8126 
8127 
8128     select LTRIM(str, 'x') into l_child_string from dual;
8129   end loop;
8130 exception
8131   WHEN FND_API.G_EXC_ERROR THEN
8132      ROLLBACK;
8133      x_return_status := FND_API.G_RET_STS_ERROR;
8134      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8135 
8136   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8137      ROLLBACK;
8138      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8139      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8140 
8141   WHEN OTHERS THEN
8142      ROLLBACK;
8143      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8144      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8145      THEN
8146         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
8147      END IF;
8148      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count, p_data => x_msg_data);
8149 end add_org_activities;
8150 
8151 procedure add_rl_activities(p_parent_process_id in number,
8152 						    p_organization_id in number,
8153 			   				p_comb_string in varchar2,
8154   						 	p_init_msg_list	IN VARCHAR2 := FND_API.G_FALSE,
8155 	                        x_return_status out nocopy varchar2,
8156                             x_msg_count out nocopy number,
8157                             x_msg_data out nocopy varchar2)
8158 is
8159   l_api_name constant varchar2(30) := 'add_rl_activities';
8160   iStart pls_integer := 1;
8161   iEnd   pls_integer;
8162   childstr		 varchar2(100);
8163   l_child_id		 number;
8164   l_revise_existing varchar2(10);
8165   l_apply_RCM varchar2(10);
8166   l_dummy number;
8167 
8168   cursor c1 (pid number) is
8169         select parent_child_id process_to_count
8170         from amw_proc_hierarchy_denorm
8171         where process_id = pid
8172         and up_down_ind = 'D'
8173         and hierarchy_type = 'A'
8174         union
8175         select pid process_to_count from dual;
8176 begin
8177 
8178 	x_return_status := FND_API.G_RET_STS_SUCCESS;
8179   	IF FND_API.to_Boolean( p_init_msg_list )  THEN
8180      FND_MSG_PUB.initialize;
8181   	END IF;
8182   	if FND_GLOBAL.user_id is null then
8183      AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8184      raise FND_API.G_EXC_ERROR;
8185   	end if;
8186 
8187 	while (true) loop
8188 
8189      /* returns the position of first occurence of 'w' */
8190      iEnd := INSTR(p_comb_string, 'x', iStart);
8191      if(iEnd = 0)
8192      then
8193        exit;
8194      end if;
8195 
8196      childstr := substr(p_comb_string, iStart, iEnd-iStart);
8197      iStart := iEnd+1;
8198      iEnd := INSTR(p_comb_string, 'x', iStart);
8199      l_revise_existing := substr(p_comb_string,iStart, iEnd-iStart);
8200      iStart := iEnd+1;
8201      iEnd := INSTR(p_comb_string, 'w', iStart);
8202      if(iEnd = 0)
8203      then
8204        iEnd := length(p_comb_string) + 1;
8205      end if;
8206      l_apply_RCM := substr(p_comb_string, iStart,iEnd-iStart);
8207      iStart := iEnd + 1;
8208 
8209      l_child_id := to_number(childstr);
8210 
8211      BEGIN
8212 		 SELECT 1 INTO l_dummy
8213   		 FROM amw_process_organization
8214 		 where process_id = l_child_id
8215 		 and organization_id = p_organization_id
8216   		 and end_date is null
8217 	  	 and deletion_date is null;
8218 
8219 	  EXCEPTION
8220 	  	WHEN no_data_found THEN
8221 			associate_process_to_org (
8222 			p_org_id => p_organization_id,
8223 			p_parent_process_id => p_parent_process_id,
8224 			p_associated_proc_id => l_child_id,
8225 			p_revise_existing => l_revise_existing,
8226 			p_apply_rcm => l_apply_RCM);
8227 
8228 			-- update the org counts of the child process and its hierarchy....
8229 			for descendents_rec in c1(l_child_id) loop
8230     	  		exit when c1%notfound;
8231     	  		amw_rl_hierarchy_pkg.update_org_count(descendents_rec.process_to_count);
8232  			end loop;
8233 	END;
8234    end loop;
8235 exception
8236   WHEN FND_API.G_EXC_ERROR THEN
8237      ROLLBACK;
8238      x_return_status := FND_API.G_RET_STS_ERROR;
8239      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8240 
8241   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8242      ROLLBACK;
8243      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8244      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8245 
8246   WHEN OTHERS THEN
8247      ROLLBACK;
8248      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8249      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8250      THEN
8251         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
8252      END IF;
8253      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
8254 
8255 end add_rl_activities;
8256 
8257 PROCEDURE update_latest_denorm_counts
8258 ( p_organization_id	    IN NUMBER,
8259   P_process_id		    IN NUMBER,
8260   p_commit		           IN VARCHAR2 := FND_API.G_FALSE,
8261   p_validation_level		   IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
8262   p_init_msg_list		   IN VARCHAR2 := FND_API.G_FALSE,
8263   x_return_status		   OUT NOCOPY VARCHAR2,
8264   x_msg_count			   OUT NOCOPY VARCHAR2,
8265   x_msg_data			   OUT NOCOPY VARCHAR2)
8266 IS
8267 
8268   L_API_NAME CONSTANT VARCHAR2(30) := 'update_latest_denorm_counts';
8269 
8270 
8271 BEGIN
8272 
8273 --always initialize global variables in th api's used from SelfSerivice Fwk..
8274    G_USER_ID := FND_GLOBAL.USER_ID;
8275    G_LOGIN_ID  := FND_GLOBAL.CONC_LOGIN_ID;
8276    x_return_status := FND_API.G_RET_STS_SUCCESS;
8277   IF FND_API.to_Boolean( p_init_msg_list )  THEN
8278     FND_MSG_PUB.initialize;
8279   END IF;
8280   IF FND_GLOBAL.User_Id IS NULL THEN
8281     AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8282     RAISE FND_API.G_EXC_ERROR;
8283   END IF;
8284 
8285 -- update the latest denorm hierarchy..
8286 	AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id => p_organization_id);
8287 -- Update the Risk Counts..
8288     upd_ltst_risk_count(p_org_id => p_organization_id, p_process_id => p_process_id);
8289 -- Update the Control Counts..
8290     upd_ltst_control_count(p_org_id => p_organization_id, p_process_id => p_process_id);
8291 
8292 
8293 exception
8294   WHEN FND_API.G_EXC_ERROR THEN
8295      ROLLBACK;
8296      x_return_status := FND_API.G_RET_STS_ERROR;
8297      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8298 
8299   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8300      ROLLBACK;
8301      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8302      FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data => x_msg_data);
8303 
8304   WHEN OTHERS THEN
8305      ROLLBACK;
8306      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8307      IF FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8308      THEN
8309         FND_MSG_PUB.Add_Exc_Msg(G_PKG_NAME,l_api_name);
8310      END IF;
8311      FND_MSG_PUB.Count_And_Get (p_encoded => FND_API.G_FALSE,p_count => x_msg_count,p_data  => x_msg_data);
8312 END update_latest_denorm_counts;
8313 
8314 --******************************************************************************
8315 --  Initialize the Nested Tables used in Synchronize Process to Organization.
8316 --  The NEsted Table holds all the organization Ids.
8317 --******************************************************************************
8318 
8319 procedure init is
8320 begin
8321     Org_Ids := t_Org_Ids();
8322     Org_Ids.delete;
8323 end init;
8324 -- ****************************************************************************
8325 PROCEDURE sync_proc_org_srs(
8326     errbuf                  OUT NOCOPY VARCHAR2,
8327     retcode                 OUT NOCOPY VARCHAR2,
8328     p_process_id		    IN number,
8329     p_org_name		        IN varchar2,
8330     p_org_range_from		IN varchar2,
8331     p_org_range_to			IN varchar2,
8332     p_sync_mode 			in varchar2,
8333     p_sync_hierarchy 		in varchar2,
8334     p_sync_attributes 		in varchar2,
8335     p_sync_rcm 				in varchar2,
8336     p_sync_people 			in varchar2,
8337     p_sync_approve 			in varchar2
8338 )
8339 IS
8340     conc_status             boolean;
8341     p_mode                  varchar2(5) := 'SYNC';
8342     L_API_NAME CONSTANT     varchar2(30):= 'sync_proc_org_srs';
8343     l_return_status	        varchar2(10);
8344     l_msg_data              varchar2(4000);
8345     l_msg_count	            number;
8346     show_warning            boolean:= false;
8347     l_sync_mode             varchar2(5);
8348     l_sync_hierarchy        varchar2(3);
8349     l_sync_attributes       varchar2(3);
8350     pending_approval_exception exception;
8351 
8352     cursor c1 (pid number) is
8353         select parent_child_id process_to_count
8354         from amw_proc_hierarchy_denorm
8355         where process_id = pid
8356         and up_down_ind = 'D'
8357         and hierarchy_type = 'A'
8358         union
8359         select pid process_to_count from dual;
8360 
8361     cursor c_processes (pid number) is
8362         select parent_child_id process_to_count
8363         from amw_proc_hierarchy_denorm
8364         where process_id = pid
8365         and up_down_ind = 'D'
8366         and hierarchy_type = 'A'
8367         union
8368         select pid process_to_count from dual;
8369 
8370 
8371 BEGIN
8372 
8373     IF p_sync_hierarchy = 'N' AND p_sync_attributes = 'N' AND p_sync_rcm = 'RDEF' AND p_sync_people = 'RDEF' THEN
8374 		RETURN;
8375 	END IF;
8376 
8377     IF FND_GLOBAL.User_Id IS NULL THEN
8378         AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8379         RAISE FND_API.G_EXC_ERROR;
8380     END IF;
8381 
8382     IF p_process_id = -1 OR p_process_id = -2 THEN
8383         conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: Cannot Associate Root Process');
8384         RETURN;
8385     END IF;
8386 
8387     IF p_org_name IS NULL THEN
8388        	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: No Organization filter found to proceed');
8389     	RETURN;
8390     ELSIF p_org_range_from IS NOT NULL and p_org_range_to IS NOT NULL then
8391         init;
8392         SELECT  DISTINCT aauv.organization_id
8393         BULK COLLECT INTO Org_Ids
8394         FROM    amw_audit_units_v aauv,
8395                 amw_process_organization apo
8396         WHERE   aauv.organization_id=apo.organization_id
8397         AND     NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
8398         AND     apo.process_id=p_process_id
8399         AND     apo.end_date IS NULL
8400         AND     apo.deletion_date IS NULL
8401         AND     aauv.NAME >= p_org_range_from AND substr(aauv.NAME,0,length(p_org_range_to))<= p_org_range_to;
8402     else
8403         init;
8404         select  distinct aauv.organization_id
8405         BULK COLLECT INTO Org_Ids
8406         from    amw_audit_units_v aauv,
8407                 amw_process_organization apo
8408         where aauv.organization_id=apo.organization_id
8409         and   NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
8410         and apo.process_id=p_process_id
8411         and apo.end_date is null
8412         and apo.deletion_date is null
8413         and (UPPER(aauv.NAME) LIKE UPPER(p_org_name || '%'));
8414 	end if;
8415 
8416 	-- set the sync mode..
8417 	IF p_sync_mode = 'Y' then
8418 		l_sync_mode := 'PSUBP';
8419 	ELSE
8420 		l_sync_mode := 'PONLY';
8421 	END IF;
8422 
8423 	-- Set the p_sync_hierarchy
8424 	IF p_sync_hierarchy = 'Y' then
8425 
8426 	   l_sync_hierarchy := 'YES' ;
8427 	ELSE
8428 
8429 	   l_sync_hierarchy := 'NO' ;
8430 	END IF;
8431 
8432 	-- Set the p_sync_attributes
8433 	IF p_sync_attributes = 'Y' then
8434 
8435 	   l_sync_attributes := 'YES' ;
8436     ELSE
8437 
8438 	   l_sync_attributes := 'NO' ;
8439 	END IF;
8440 
8441 	IF Org_Ids.exists(1)  THEN
8442 	   BEGIN
8443 	       synchronize_process(    p_process_id  => p_process_id,
8444                                    p_sync_mode   => L_sync_mode,
8445 						           p_sync_hierarchy =>l_sync_hierarchy,
8446                                    p_sync_attributes => l_sync_attributes,
8447                                    p_sync_rcm => p_sync_rcm,
8448                                    p_sync_people => p_sync_people
8449                                );
8450 
8451             FOR indx IN Org_Ids.FIRST .. Org_Ids.LAST
8452             LOOP
8453 --ko replacing the below clause for removing amw_org_hierarchy_denorm usage...
8454 /*
8455     	        --Ko Update the Proc_org_hierarchy_denorm tables..
8456                 AMW_RL_HIERARCHY_PKG.update_denorm(p_org_id =>Org_Ids(indx));
8457 */
8458        	        IF p_sync_approve = 'AUTO' THEN
8459                     BEGIN
8460        	                AMW_PROC_ORG_APPROVAL_PKG.sub_for_approval(p_process_id, Org_Ids(indx));
8461        	                AMW_PROC_ORG_APPROVAL_PKG.approve(p_process_id, Org_Ids(indx),FND_API.G_FALSE);
8462                     EXCEPTION
8463            	            WHEN OTHERS THEN
8464           		            show_warning := true;
8465               		        ROLLBACK;
8466                 	       -- Unapproved object associations exists exception may happen..catche them here..
8467                 	       FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
8468                 	       fnd_file.put_line(fnd_file.LOG, ' Error when Approving the process in organization ' ||Org_Ids(indx) );
8469                 	       fnd_file.put_line(fnd_file.LOG, l_msg_data);
8470                     END;
8471        	        END IF;
8472             END LOOP;
8473 
8474             COMMIt;
8475 
8476          EXCEPTION
8477 	     	 when pending_approval_exception then
8478 	     	   show_warning := true;
8479                ROLLBACK;
8480 	 		   FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
8481                fnd_file.put_line(fnd_file.LOG, ' Error when Synchronizing the process in organization');
8482                fnd_file.put_line(fnd_file.LOG, l_msg_data);
8483 	      	 when OTHERS then
8484 	     	   show_warning := true;
8485                ROLLBACK;
8486 	 		   FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
8487                fnd_file.put_line(fnd_file.LOG, ' Error when Synchronizing the process in organization ');
8488                fnd_file.put_line(fnd_file.LOG, l_msg_data);
8489         END;
8490 
8491  	    if show_warning then
8492  	          conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Process cannot be synchronized to some organizations');
8493         end if;
8494 
8495         IF p_sync_mode = 'Y' or p_sync_hierarchy = 'Y' then
8496 
8497         -- update the org count..
8498             update amw_process AP
8499             set AP.org_count = (select  COUNT(o.organization_id)
8500                                 from    hr_all_organization_units o,
8501                                         hr_organization_information o2,
8502                                         amw_process_organization APO
8503                                 WHERE   o.organization_id = o2.organization_id
8504                                 AND     APO.organization_id = o.ORGANIZATION_ID
8505                                 and     o2.org_information_context = 'CLASS'
8506                                 and     o2.org_information1 = 'AMW_AUDIT_UNIT'
8507                                 and     o2.org_information2 = 'Y'
8508                                 AND    APO.process_id = AP.PROCESS_ID
8509                                 and    APO.end_date is null
8510                                 and    ( APO.deletion_date is null
8511                                 or
8512                                     ( APO.deletion_date is not null and APO.approval_date is null)
8513                                     )
8514                                 ),
8515                 AP.object_version_number = AP.object_version_number + 1,
8516                 AP.last_update_date = sysdate,
8517                 AP.last_updated_by = G_USER_ID,
8518                 AP.last_update_login = G_LOGIN_ID
8519             where AP.approval_date is not null
8520             and   AP.approval_end_date is null
8521             and   AP.process_id <> -1
8522             and   AP.process_id   IN (  select APHD.parent_child_id process_to_count
8523                                 from   amw_proc_hierarchy_denorm APHD
8524                                 where  APHD.process_id = p_process_id
8525                                 and    APHD.up_down_ind = 'D'
8526                                 and    APHD.hierarchy_type = 'A'
8527                                 union
8528                                 select p_process_id process_to_count from dual
8529                             );
8530         END IF;
8531      END IF;
8532      commit;
8533 EXCEPTION
8534   WHEN FND_API.G_EXC_ERROR THEN
8535     ROLLBACK;
8536     retcode := 2;
8537 	errbuf  := SUBSTR(SQLERRM,1,1000);
8538 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8539 
8540   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8541     ROLLBACK;
8542 	retcode := 2;
8543 	errbuf  := SUBSTR(SQLERRM,1,1000);
8544 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8545 
8546   WHEN OTHERS THEN
8547     ROLLBACK;
8548 	retcode := 2;
8549 	errbuf  := SUBSTR(SQLERRM,1,1000);
8550 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8551 END sync_proc_org_srs;
8552 
8553 /** Following 2 functions added by dpatel on 8th Feb, 2006*/
8554 FUNCTION get_latest_conc_request (concur_prog_id number) RETURN NUMBER;
8555 FUNCTION get_concur_program_id (concur_prog_name varchar2) RETURN NUMBER;
8556 
8557 FUNCTION get_latest_conc_request (concur_prog_id number) RETURN NUMBER
8558 IS
8559 p_request_id number(15);
8560 BEGIN
8561     select
8562       request_id into p_request_id
8563     from
8564           fnd_concurrent_requests
8565     where
8566           CONCURRENT_PROGRAM_ID = concur_prog_id
8567           and last_update_date = (select max(last_update_date) from fnd_concurrent_requests where CONCURRENT_PROGRAM_ID = concur_prog_id)
8568           and phase_code<>'C';
8569     return p_request_id;
8570 END get_latest_conc_request;
8571 
8572 FUNCTION get_concur_program_id (concur_prog_name varchar2) RETURN NUMBER
8573 IS
8574 v_concurrent_program_id number(15);
8575 
8576 BEGIN
8577     select
8578         concurrent_program_id into v_concurrent_program_id
8579     from
8580         fnd_concurrent_programs
8581     where CONCURRENT_PROGRAM_NAME = concur_prog_name;
8582     return v_concurrent_program_id;
8583 END get_concur_program_id;
8584 /** Block by dpatel ends */
8585 
8586 -- ****************************************************************************
8587 PROCEDURE push_proc_org_no_count(
8588     errbuf                  OUT NOCOPY VARCHAR2,
8589     retcode                 OUT NOCOPY VARCHAR2,
8590     p_process_id		    IN number,
8591     p_org_name		        IN varchar2,
8592     p_org_range_from		IN varchar2,
8593     p_org_range_to			IN varchar2,
8594     p_synchronize		    IN varchar2,
8595     p_apply_rcm			    IN varchar2
8596 )
8597 IS
8598 cursor c1 (pid number) is
8599         select parent_child_id process_to_count
8600         from amw_proc_hierarchy_denorm
8601         where process_id = pid
8602         and up_down_ind = 'D'
8603         and hierarchy_type = 'A'
8604         union
8605         select pid process_to_count from dual;
8606 
8607 cursor c2(pid number, orgName varchar2) is
8608     select  aauv.organization_id,
8609     		name
8610     from    amw_audit_units_v aauv
8611     where   NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
8612     and     'Y' = AMW_UTILITY_PVT.IS_ORG_REGISTERED(aauv.ORGANIZATION_ID)
8613     and     aauv.organization_id not in(
8614                 select distinct organization_id
8615                 from amw_process_organization
8616                 where process_id = pid
8617                 and end_date is null
8618                 and (
8619                     deletion_date is null or
8620                     (deletion_date is not null and approval_date is null)
8621                 )
8622             )
8623     and (UPPER(NAME) LIKE UPPER(orgName));
8624 cursor c3(pid number, rangeFrom varchar2, rangeTo varchar2) is
8625     select  aauv.organization_id,
8626     		name
8627     from    amw_audit_units_v aauv
8628     where   NVL( AAUV.DATE_TO,SYSDATE ) >= SYSDATE
8629     and     'Y' = AMW_UTILITY_PVT.IS_ORG_REGISTERED(aauv.ORGANIZATION_ID)
8630     and     aauv.organization_id not in(
8631                 select distinct organization_id
8632                 from amw_process_organization
8633                 where process_id = pid
8634                 and end_date is null
8635                 and (
8636                     deletion_date is null or
8637                     (deletion_date is not null and approval_date is null)
8638                 )
8639             )
8640     and NAME >= rangeFrom and substr(NAME,0,length(rangeTo))<= rangeTo;
8641 
8642 
8643     conc_status             boolean;
8644     p_mode                  varchar2(5) := 'ASSOC';
8645     L_API_NAME CONSTANT     varchar2(30):= 'push_proc_org_no_count';
8646     l_return_status	        varchar2(10);
8647     l_msg_data              varchar2(4000);
8648     l_msg_count	            number;
8649     p_parent_orgprocess_id  number := -2 ;
8650 
8651 
8652 cursor c_processes (pid number) is
8653         select parent_child_id process_to_count
8654         from amw_proc_hierarchy_denorm
8655         where process_id = pid
8656         and up_down_ind = 'D'
8657         and hierarchy_type = 'A'
8658         union
8659         select pid process_to_count from dual;
8660 
8661 
8662 
8663 type t_audit_unit_rec is record (organization_id  amw_audit_units_v.organization_id%type,
8664                          	   org_name  amw_audit_units_v.name%type);
8665 
8666 type t_audit_units_tbl is table of t_audit_unit_rec;
8667 l_audit_units_tbl t_audit_units_tbl;
8668 
8669 show_warning boolean:= false;
8670 /* dpatel on 8th Feb, 2006*/
8671 cursor c_current_requests(prog_id number) IS
8672  select
8673   request_id, argument1, argument2, argument3, argument4
8674 from
8675       fnd_concurrent_requests
8676 where
8677       CONCURRENT_PROGRAM_ID = prog_id
8678       and last_update_date < (select max(last_update_date) from fnd_concurrent_requests where CONCURRENT_PROGRAM_ID = prog_id)
8679       and phase_code<>'C';
8680 v_concurrent_program_id integer;
8681 same_request_exception exception;
8682 prior_process_id		    number;
8683 prior_org_name		        varchar2(240);
8684 prior_org_range_from		varchar2(240);
8685 prior_org_range_to			varchar2(240);
8686 p_U_org_name		        varchar2(240);
8687 p_U_org_range_from		varchar2(240);
8688 p_U_org_range_to			varchar2(240);
8689 l_assoc_app_prof            varchar2(1);
8690 BEGIN
8691 
8692     IF FND_GLOBAL.User_Id IS NULL THEN
8693         AMW_Utility_PVT.Error_Message(p_message_name => 'USER_PROFILE_MISSING');
8694         RAISE FND_API.G_EXC_ERROR;
8695     END IF;
8696     if p_process_id = -1 OR p_process_id = -2 then
8697         conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: Cannot Associate Root Process');
8698         return;
8699     end if;
8700     if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
8701     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
8702         'amw.plsql.AMW_ORG_HIERARCHY_PKG.push_proc_org_no_count.Begin','BEGIN');
8703   end if;
8704 
8705 /** Following block added by dpatel on 8th Feb, 2006*/
8706     p_U_org_name := upper(p_org_name);
8707     p_U_org_range_from := upper(p_org_range_from);
8708     p_U_org_range_to := upper(p_org_range_to);
8709 
8710     v_concurrent_program_id := get_concur_program_id('AMWASSOCPROCNOCOUNT');
8711 
8712     FOR r_current_requests IN c_current_requests(v_concurrent_program_id) LOOP
8713         prior_process_id := r_current_requests.argument1;
8714         prior_org_name := UPPER(r_current_requests.argument2);
8715         prior_org_range_from := UPPER(r_current_requests.argument3);
8716         prior_org_range_to := UPPER(r_current_requests.argument4);
8717 
8718       IF p_process_id = prior_process_id THEN
8719     	if p_U_org_range_from is not null and p_U_org_range_to is not null then
8720     	   if prior_org_range_from is not null and prior_org_range_to is not null then
8721     	       if p_U_org_range_from >= prior_org_range_from and p_U_org_range_from <= prior_org_range_to
8722                     or p_U_org_range_to >= prior_org_range_from and p_U_org_range_to <= prior_org_range_to
8723                     or p_U_org_range_from <= prior_org_range_from and p_U_org_range_to >= prior_org_range_to
8724                 then
8725     	           RAISE same_request_exception;
8726     	       end if;
8727 	       elsif prior_org_name is not null then
8728     	       if prior_org_name >= p_U_org_range_from and prior_org_name <= p_U_org_range_to then
8729     	           RAISE same_request_exception;
8730     	       end if;
8731     	   end if;
8732         elsif p_U_org_name is not null then
8733     	   if prior_org_range_from is not null and prior_org_range_to is not null then
8734     	       if p_U_org_name >= prior_org_range_from and p_U_org_name <= prior_org_range_to then
8735     	           RAISE same_request_exception;
8736     	       end if;
8737 	       elsif prior_org_name is not null then
8738                if p_U_org_name like prior_org_name ||'%' or prior_org_name like p_U_org_name ||'%' then
8739 	               RAISE same_request_exception;
8740                end if;
8741     	   end if;
8742     	end if;
8743       END IF;
8744     END LOOP;
8745 /** Block by dpatel ends */
8746 
8747 	if  p_U_org_range_from is not null and p_U_org_range_to is not null then
8748     	open c3(p_process_id, p_U_org_range_from,p_U_org_range_to);
8749     	fetch c3 bulk collect into l_audit_units_tbl;
8750     	close c3;
8751     elsif p_U_org_name is null then
8752     	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Warning: No Organization filter found to proceed');
8753     	return;
8754     else
8755     	open c2(p_process_id, p_U_org_name || '%');
8756     	fetch c2 bulk collect into l_audit_units_tbl;
8757     	close c2;
8758 	end if;
8759 	if l_audit_units_tbl.exists(1)  then
8760 	   l_assoc_app_prof := fnd_profile.value('AMW_PROC_ORG_ASS_APPRV');
8761 	   IF l_assoc_app_prof = 'Y' THEN
8762             AMW_UTILITY_PVT.cache_appr_options;
8763        	   END IF;
8764     FOR orgid IN l_audit_units_tbl.first .. l_audit_units_tbl.last loop
8765 --    	fnd_file.put_line(fnd_file.LOG, 'Associating to ' || l_audit_units_tbl(orgid).org_name  );
8766      	push_proc_per_org(
8767             p_parent_orgprocess_id	=> p_parent_orgprocess_id,
8768 			p_process_id		=> p_process_id,
8769 			p_org_id			=> l_audit_units_tbl(orgid).organization_id,
8770 			p_mode				=> p_mode,
8771 			p_apply_rcm			=> p_apply_rcm,
8772 			p_synchronize		=> p_synchronize,
8773 			p_update_count		=> FND_API.G_FALSE,
8774 			p_commit            => FND_API.G_FALSE,
8775 			x_return_status		=> l_return_status,
8776 			x_msg_count			=> l_msg_count,
8777 			x_msg_data			=> l_msg_data);
8778 
8779            	IF l_return_status <> 'S' THEN
8780            	  show_warning := true;
8781 		      fnd_file.put_line(fnd_file.LOG, 'Error when Associating the process to ' || l_audit_units_tbl(orgid).org_name  );
8782 		      fnd_file.put_line(fnd_file.LOG, l_msg_data );
8783             ELSE
8784             -- If user wants the processes to be associated as approved, user should set this profile to Y.
8785             -- Note that to get the whole hierarchy approved, user needs to set the approval option to
8786             -- "approve everything down below". Else only the process id passed will be approved.
8787             -- Although this is not very user friendly, I cannot see an option, as I cannot change the
8788             -- approval option ad hoc for this association process. When user associates a process,
8789             -- may be some-subprocess down below is in pending approval status, and that should prevent
8790             -- modifying the approval option.
8791             -- Also note that the "Approval Required" parameter for the org will be overridden, if set to yes.
8792 	       	IF fnd_profile.value('AMW_PROC_ORG_ASS_APPRV') = 'Y' THEN
8793 	       		BEGIN
8794                 	AMW_PROC_ORG_APPROVAL_PKG.sub_for_approval(p_process_id, l_audit_units_tbl(orgid).organization_id);
8795                 	AMW_PROC_ORG_APPROVAL_PKG.approve(p_process_id, l_audit_units_tbl(orgid).organization_id,FND_API.G_FALSE);
8796                 EXCEPTION
8797                 	WHEN OTHERS THEN
8798                 		show_warning := true;
8799                 		ROLLBACK;
8800                 		-- Unapproved object associations exists exception may happen..catche them here..
8801                 		FND_MSG_PUB.Count_And_Get(p_encoded => FND_API.G_FALSE,p_count => l_msg_count,p_data => l_msg_data);
8802                 		fnd_file.put_line(fnd_file.LOG, ' Error when Approving the process in organization ' ||l_audit_units_tbl(orgid).org_name  );
8803                 		fnd_file.put_line(fnd_file.LOG, l_msg_data);
8804                 END;
8805 	       	END IF;
8806 	       END IF;
8807 	       -- Done associating...Commit here..
8808 	       COMMIT;
8809  	END LOOP;
8810  	if show_warning then
8811  	  conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING','Process cannot be associated to some organizations');
8812     end if;
8813 	AMW_UTILITY_PVT.unset_appr_cache;
8814   END IF;
8815   if( FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL ) then
8816     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
8817         'amw.plsql.AMW_ORG_HIERARCHY_PKG.push_proc_org_no_count.End','END');
8818   end if;
8819   COMMIT;
8820 EXCEPTION
8821  WHEN same_request_exception THEN
8822     fnd_file.put_line(fnd_file.LOG, 'ERROR:'|| SQLERRM);
8823     conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',
8824     'There is already a concurrent process running for a similar process id.'||
8825     'This concurrent request is also being run with the same value for parameter "Process id" '||
8826     ' and the parameter "Organization Name or Range" that the earlier concurrent program is running with. '
8827     );
8828   WHEN FND_API.G_EXC_ERROR THEN
8829     ROLLBACK;
8830     retcode := 2;
8831 	errbuf  := SUBSTR(SQLERRM,1,1000);
8832 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8833 
8834   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8835     ROLLBACK;
8836 	retcode := 2;
8837 	errbuf  := SUBSTR(SQLERRM,1,1000);
8838 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8839 
8840   WHEN OTHERS THEN
8841     ROLLBACK;
8842 	retcode := 2;
8843 	errbuf  := SUBSTR(SQLERRM,1,1000);
8844 	conc_status:=FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR','Error: '|| SQLERRM);
8845 END push_proc_org_no_count;
8846 
8847 end AMW_ORG_HIERARCHY_PKG;