DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMS_LISTGENERATION_PKG

Source


1 PACKAGE BODY AMS_LISTGENERATION_PKG as
2 /* $Header: amsvlgnb.pls 120.33.12010000.2 2008/08/04 09:50:08 rsatyava ship $ */
3 -- Start of Comments
4 -- NAME
5 --   AMS_LISTGENERATION
6 -- PURPOSE
7 --   This package performs the generation of all oracle marketing defined lists.
8 -------------------------------------------------------------------------------
9 -- NOTES
10 --
11 -- HISTORY
12 --   06/21/1999 tdonohoe created
13 --   07/07/2000 tdonohoe modified Validate_SQL to fix BUG 1349322.
14 --   07/26/2000 tdonohoe added code to get all list entry data when
15 --                       deduplication of a list is requested.
16 --                       this occurs before the call to random_generation.
17 --   10/4/2000  vbhandar modified to fix bug 1420272
18 --   11/8/2000  gjoby    modified to fix bug 1482180
19 --                       Performance bug Used bind variables and Modified where
20 --                       clause in sub query to eliminate additional conditions
21 --   21/1/2001  gjoby    Recreated for hornet release
22 --   03/09/2005 sthattil Got rid of unwanted code with Balaji
23 --   05/23/2005 sthattil changes for list generation cancel
24 --   07/11/2005 ryedator Modified process_list_actions to select 'include'
25 --			 selections by rank number - bug 4443619
26 --   08/17/2005	bmuthukr With more R12 changes.
27 --   08/13/2007 AMLAL    Bug#6338292 : Truncated the Input parameters passed for
28 --                                     list generation.
29 -- End of Comments
30 
31 
32 G_PKG_NAME      CONSTANT VARCHAR2(30):='AMS_LISTGENERATION_PKG';
33 G_FILE_NAME     CONSTANT VARCHAR2(12):='amslsgnb.pls';
34 
35 G_MAX_SQL_STR_LEN  CONSTANT NUMBER := 2000;
36 G_MAX_STRING_LEN   CONSTANT NUMBER := 32767;
37 G_OVERFLOW_AMOUNT  CONSTANT NUMBER := 100;
38 --g_count             NUMBER := 1;
39 /*
40 g_message_table  sql_string;
41 g_message_table_null  sql_string;
42 */
43 --g_message_table  sql_string_4K;
44 --g_message_table_null  sql_string_4K;
45 --g_date           t_date;
46 
47 g_remote_list		VARCHAR2(1) := 'N';
48 
49 
50 
51 g_reqd_num_tbl                  t_number;
52 g_act_num_tbl                   t_number;
53 g_no_of_rows_ini_selected  number :=0;
54 
55 AMS_DEBUG_HIGH_ON boolean := FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH);
56 AMS_DEBUG_LOW_ON boolean := FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW);
57 AMS_DEBUG_MEDIUM_ON boolean := FND_MSG_PUB.CHECK_MSG_LEVEL(FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM);
58 ------------------------------------------------------------------
59 --This Variable stores the primary key of the list header.      --
60 ------------------------------------------------------------------
61 g_list_header_id         ams_list_headers_all.list_header_id%type;
62 ----------------------------------------------------------------------------
63 
64 cursor g_initial_count is
65 select count(1)
66   from ams_list_entries
67  where list_header_id = g_list_header_id
68    and enabled_flag = 'Y';
69 
70 PROCEDURE logger is
71 --  This procedure was written to replace Autonomous Transactions
72 --
73  l_return_status VARCHAR2(1);
74 BEGIN
75    -- Standard Start of API savepoint
76    SAVEPOINT logger_save;
77 --  g_message_table(g_count) := 'Calling logger';
78 --  g_count := g_count +1;
79   -- Not reqd since we are not storing it in pl/sql table anymore.
80   /*
81   FORALL I in g_message_table.first .. g_message_table.last
82       INSERT INTO ams_act_logs(
83          activity_log_id
84          ,last_update_date
85          ,last_updated_by
86          ,creation_date
87          ,created_by
88          ,last_update_login
89          ,object_version_number
90          ,act_log_used_by_id
91          ,arc_act_log_used_by
92          ,log_transaction_id
93          ,log_message_text
94       )
95       VALUES (
96          ams_act_logs_s.NEXTVAL
97          ,g_date(i)
98          ,FND_GLOBAL.User_Id
99          ,g_date(i)
100          ,FND_GLOBAL.User_Id
101          ,FND_GLOBAL.Conc_Login_Id
102          ,1
103          ,g_list_header_id
104          ,'LIST'
105          ,ams_act_logs_transaction_id_s.NEXTVAL
106          ,g_message_table(i)
107       ) ;*/
108      commit;
109 exception
110    -- Logger has failed
111    when others then
112       null;
113 END logger;
114 
115 --Procedure to identify the message log level for this user(list owner).
116 -- Will be called from generate_list and generate_target_group_list_old.
117 
118 PROCEDURE find_log_level(p_list_header_id number) is
119 l_debug_prof_level   number := -1;
120 
121 cursor c_get_user_id(l_list_header_id number) is
122 select jtf.user_id
123   from jtf_rs_resource_extns jtf, ams_list_headers_all hd
124  where jtf.resource_id = hd.owner_user_id
125    and hd.list_header_id = l_list_header_id;
126 
127 begin
128    open c_get_user_id(p_list_header_id);
129    fetch c_get_user_id into g_user_id;
130    close c_get_user_id;
131 
132    delete from ams_act_logs
133     where arc_act_log_used_by = 'LIST'
134       and act_log_used_by_id  = p_list_header_id ;
135 
136    g_list_header_id := p_list_header_id;
137    g_message_table  := g_message_table_null ;
138    g_count := 0;
139    g_log_level := null;
140 
141    --Will take the value of the profile "FND: Message Level Threshold" for the list owner.
142    l_debug_prof_level := fnd_profile.value_specific(name => 'FND_AS_MSG_LEVEL_THRESHOLD',
143                                                     user_id =>  g_user_id);
144    if nvl(l_debug_prof_level,-1) = 10  then --Profile set to Debug Low. All the messages will be logged.
145       g_log_level := 'LOW';
146       write_to_act_log('All the debug messages will be logged. To view only critical messages set the profile (at user level) FND: Message Level Threshold to a value other than Debug Low',
147                        'LIST',
148 		       g_list_header_id,
149 		       'HIGH');
150       --g_message_table(g_count) := 'All the debug messages will be logged. To view only critical messages set the profile (at user level) FND: Message Level Threshold to a value other than Debug Low';
151    else --Only messages with HIGH severity will be logged.
152       g_log_level := 'HIGH';
153       write_to_act_log('Only messages with high severity will be logged. To view all the messages set the profile (at user level) FND: Message Level Threshold to Debug Low',
154                        'LIST',
155 		       g_list_header_id,
156 		       'HIGH');
157       --g_message_table(g_count) := 'Only messages with high severity will be logged. To view all the messages set the profile (at user level) FND: Message Level Threshold to Debug Low';
158    end if;
159    g_date(g_count) := sysdate;
160    g_count   := g_count + 1;
161 exception
162    when others then
163       null;
164 END find_log_level;
165 
166 -- Start of Comments
167 -- NAME ---   WRITE_TO_ACT_LOG
168 -- PURPOSE
169 --     writes to the Ams_Act_Logs table.
170 -- NOTES
171 -- HISTORY
172 --   08/02/1999        tdonohoe            created
173 --                     gjoby    Commented out  ams_utility_pvt part
174 --                              Autonomous trans fails in distributed trans
175 -- End of Comments
176 ----------------------------------------------------------------------------
177 
178 PROCEDURE WRITE_TO_ACT_LOG(p_msg_data in VARCHAR2,
179                            p_arc_log_used_by in VARCHAR2,
180                            p_log_used_by_id in number,
181 			   p_level          in varchar2 := 'LOW')
182                            IS
183  --PRAGMA AUTONOMOUS_TRANSACTION;
184  l_return_status VARCHAR2(1);
185 
186 BEGIN
187    if nvl(g_log_level,'HIGH') = 'HIGH' and p_level = 'LOW' then
188       return;
189    end if;
190 
191    INSERT INTO ams_act_logs(
192          activity_log_id
193          ,last_update_date
194          ,last_updated_by
195          ,creation_date
196          ,created_by
197          ,last_update_login
198          ,object_version_number
199          ,act_log_used_by_id
200          ,arc_act_log_used_by
201          ,log_transaction_id
202          ,log_message_text
203       )
204    VALUES (
205          ams_act_logs_s.NEXTVAL
206          ,sysdate
207          ,FND_GLOBAL.User_Id
208          ,sysdate
209          ,FND_GLOBAL.User_Id
210          ,FND_GLOBAL.Conc_Login_Id
211          ,1
212          ,nvl(p_log_used_by_id,g_list_header_id)
213          ,'LIST'
214          ,ams_act_logs_transaction_id_s.NEXTVAL
215          ,p_msg_data
216       ) ;
217      commit;
218 
219 --   g_message_table(g_count) := p_msg_data;
220 --   g_date(g_count) := sysdate;
221 --   g_count   := g_count + 1;
222 
223    fnd_file.put(1,substr(p_msg_data,1,255));
224    fnd_file.new_line(1,1);
225 
226 /*
227   AMS_UTILITY_PVT.CREATE_LOG(
228                              x_return_status    => l_return_status,
229                              p_arc_log_used_by  => 'LIST',
230                              p_log_used_by_id   => g_list_header_id,
231                              p_msg_data         => p_msg_data);
232 */
233 -- logger;
234 --  COMMIT;
235 END WRITE_TO_ACT_LOG;
236 
237 PROCEDURE get_count(p_list_select_action_id in number,
238                     p_order_number in number,
239 		    p_incl_type in varchar2 default 'OTHERS',
240 		    p_sql_string in varchar2 default null) is
241 
242 l_no_of_rows_in_list  number := 0;
243 l_sql_string      varchar2(32767) := p_sql_string;
244 l_str_len         number := 0;
245 l_position        number := 0;
246 l_rep_str         varchar2(32767) := 'SELECT COUNT(1) FROM ';
247 
248 l_replaced_str    varchar2(32767);
249 l_cnt_string      varchar2(32767);
250 l_cnt		  number := 0;
251 l_cnt1		  number := 0;
252 l_dist_pct        number := 0;
253 l_no_of_rows_reqd number := 0;
254 l_incl_object_id  number := 0;
255 l_import_type     varchar2(100) := null;
256 l_selection_cnt   number := 0;
257 l_gen_type        varchar2(100) := null;
258 x_msg_count       number;
259 x_msg_data        varchar2(1000);
260 x_return_status   varchar2(1);
261 
262 
263 cursor c_gen_type is
264 select generation_type
265   from ams_list_headers_all
266  where list_header_id = g_list_header_id;
267 
268 cursor c_no_of_rows_reqd is
269 select distribution_pct,incl_object_id
270   from ams_list_select_actions
271  where list_select_action_id = p_list_select_action_id;
272 
273 cursor c_no_of_rows_in_list is
274 select no_of_rows_active
275   from ams_list_headers_all
276  where list_header_id = l_incl_object_id;
277 
278 cursor c_import_type is
279 select decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST')
280   from ams_imp_list_headers_all
281  where import_list_header_id = l_incl_object_id;
282 
283 cursor c_no_of_rows_in_b2b_list is
284 select count(1)
285   from ams_hz_b2b_mapping_v
286  where import_list_header_id = l_incl_object_id
287    and enabled_flag = 'Y';
288 
289 cursor c_no_of_rows_in_b2c_list is
290 select count(1)
291   from ams_hz_b2c_mapping_v
292  where import_list_header_id = l_incl_object_id
293    and enabled_flag = 'Y';
294 
295 cursor c_get_cnt_from_sel is
296 select count(1)
297   from ams_list_entries
298  where list_header_id = g_list_header_id
299    and list_select_action_id = p_list_select_action_id;
300 
301 begin
302    open c_no_of_rows_reqd;
303    fetch c_no_of_rows_reqd into l_dist_pct,l_incl_object_id;
304    close c_no_of_rows_reqd;
305 
306    write_to_act_log('Executing procedure get_count','LIST',g_list_header_id,'HIGH');
307    write_to_act_log('Included object id is '||l_incl_object_id,'LIST',g_list_header_id,'LOW');
308 
309    if nvl(l_dist_pct,100) = 100 then
310       write_to_act_log('All the rows will be taken from this selection','LIST',g_list_header_id,'HIGH');
311       g_act_num_tbl(p_order_number)  := -1;
312       g_reqd_num_tbl(p_order_number) := -1;
313       return;
314    end if;
315 
316 
317    if p_incl_type  = 'LIST' then
318       open c_no_of_rows_in_list;
319       fetch c_no_of_rows_in_list into l_cnt;
320       close c_no_of_rows_in_list;
321       write_to_act_log('No of rows in the included list is '||l_cnt,'LIST',g_list_header_id,'LOW');
322    elsif p_incl_type = 'IMPH' then
323       open c_import_type;
324       fetch c_import_type into l_import_type;
325       close c_import_type;
326       if l_import_type = 'PERSON_LIST' then
327          open c_no_of_rows_in_b2c_list;
328    	 fetch c_no_of_rows_in_b2c_list into l_cnt;
329          close c_no_of_rows_in_b2c_list;
330       elsif l_import_type = 'ORGANIZATION_CONTACT_LIST' then
331          open c_no_of_rows_in_b2b_list;
332 	 fetch c_no_of_rows_in_b2b_list into l_cnt;
333 	 close c_no_of_rows_in_b2b_list;
334       end if;
335       write_to_act_log('No of rows in the included import list is '||l_cnt,'LIST',g_list_header_id,'LOW');
336    elsif (p_incl_type in ('OTHERS') and p_sql_string is not null) then
337       if nvl(g_remote_list_gen,'N') = 'Y' then
338          execute immediate
339             'begin
340                ams_remote_listgen_pkg.remote_get_count'||'@'||g_database_link||'(:1,:2,:3,:4,:5)'||';'||
341             ' end;'
342             using p_sql_string,
343             out l_cnt,
344             out x_msg_count,
345             out x_msg_data,
346             out x_return_status;
347             write_to_act_log('Total # of rows from this selection is '||l_cnt , 'LIST', g_list_header_id,'LOW');
348             if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
349                write_to_act_log('Error while executing remote_get_count procedure.', 'LIST', g_list_header_id,'HIGH');
350                write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
351             elsif nvl(x_return_status,'S') = 'S' then
352                write_to_act_log('Procedure remote_get_count executed successfully.', 'LIST', g_list_header_id,'HIGH');
353             end if;
354       else
355          --execute immediate l_rep_str||'('||p_sql_string||')' INTO l_cnt;
356 	 execute immediate p_sql_string INTO l_cnt;
357       end if;
358       write_to_act_log('No of rows returned by the sql/segment/workbook is '||l_cnt,'LIST',g_list_header_id,'LOW');
359    end if;
360 
361    open c_gen_type;
362    fetch c_gen_type into l_gen_type;
363    close c_gen_type;
364 
365    l_no_of_rows_reqd := round(l_cnt * (l_dist_pct/100));
366    write_to_act_log('No of rows to be taken from this selection is '||l_no_of_rows_reqd,'LIST',g_list_header_id,'LOW');
367 
368    if nvl(l_gen_type,'NONE') = 'INCREMENTAL' then
369       open c_get_cnt_from_sel;
370       fetch c_get_cnt_from_sel into l_selection_cnt;
371       close c_get_cnt_from_sel;
372 
373       if l_no_of_rows_reqd > l_selection_cnt then
374          l_no_of_rows_reqd := l_no_of_rows_reqd - l_selection_cnt;
375          write_to_act_log('Already there are '||l_selection_cnt||' entries. Need to insert '||l_no_of_rows_reqd||' entries.','LIST',g_list_header_id,'LOW');
376       elsif l_no_of_rows_reqd <= l_selection_cnt then
377          l_no_of_rows_reqd := 0;
378          write_to_act_log('Already there are '||l_selection_cnt||' entries. No need to insert.','LIST',g_list_header_id,'LOW');
379       end if;
380    end if;
381 
382    g_act_num_tbl(p_order_number) := l_cnt;
383    g_reqd_num_tbl(p_order_number) := l_no_of_rows_reqd;
384 
385 exception
386    when others then
387       write_to_act_log('Error while executing get_count procedure','LIST',g_list_header_id,'HIGH');
388       write_to_act_log('Error '||sqlcode||'  '||sqlerrm,'LIST',g_list_header_id,'HIGH');
389 end get_count;
390 
391 --Added for bug 4577528 by bmuthukr.
392 PROCEDURE UPDATE_REMOTE_LIST_HEADER(P_LIST_HEADER_ID NUMBER,
393                                     X_RETURN_STATUS  OUT NOCOPY VARCHAR2,
394 				    X_MSG_COUNT      OUT NOCOPY NUMBER,
395 				    X_MSG_DATA       OUT NOCOPY VARCHAR2) IS
396 CURSOR C1(p_list_header_id number) IS
397 SELECT list_header_id,
398        last_update_date,
399        last_updated_by,
400        creation_date,
401        created_by,
402        last_update_login,
403        list_used_by_id,
404        arc_list_used_by,
405        list_type,
406        status_code,
407        status_date,
408        generation_type,
409        owner_user_id,
410        row_selection_type,
411        no_of_rows_max_requested,
412        main_random_pct_row_selection,
413        ctrl_gen_mode,
414        ctrl_status_code,
415        ctrl_conc_job_id,
416        status_code_old,
417        ctrl_limit_of_error,
418        ctrl_req_resp_rate,
419        ctrl_conf_level,
420        ctrl_random_nth_row_selection,
421        ctrl_random_pct_row_selection
422   FROM ams_list_headers_all
423  WHERE list_header_id = p_list_header_id;
424 c1_rec c1%rowtype;
425 
426 BEGIN
427    x_return_status := 'S';
428 
429    if nvl(g_remote_list_gen,'N') = 'N' then
430       return;
431    end if;
432 
433    open c1(p_list_header_id);
434    fetch c1 into c1_rec;
435    close c1;
436 
437    write_to_act_log('Passing the details to remote database to update the header info.', 'LIST', p_list_header_id,'HIGH');
438    execute immediate
439       'begin
440          ams_remote_listgen_pkg.remote_insert_list_headers'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17,:18,:19,:20,:21,:22,:23,:24,:25,:26,:27,:28)'||';'||
441       ' end;'
442       using
443     c1_rec.list_header_id,
444     c1_rec.last_update_date,
445     c1_rec.last_updated_by,
446     c1_rec.creation_date,
447     c1_rec.created_by,
448     c1_rec.last_update_login,
449     c1_rec.list_used_by_id,
450     c1_rec.arc_list_used_by,
451     c1_rec.list_type,
452     c1_rec.status_code,
453     c1_rec.status_date,
454     c1_rec.generation_type,
455     c1_rec.owner_user_id,
456     c1_rec.row_selection_type,
457     c1_rec.no_of_rows_max_requested,
458     c1_rec.main_random_pct_row_selection,
459     c1_rec.ctrl_gen_mode,
460     c1_rec.ctrl_status_code,
461     c1_rec.ctrl_conc_job_id,
462     c1_rec.status_code_old,
463     c1_rec.ctrl_limit_of_error,
464     c1_rec.ctrl_req_resp_rate,
465     c1_rec.ctrl_conf_level,
466     c1_rec.ctrl_random_nth_row_selection,
467     c1_rec.ctrl_random_pct_row_selection,
468     out x_msg_count,
469     out x_msg_data,
470     out x_return_status;
471     write_to_act_log('Header info updated in the remote database.', 'LIST', g_list_header_id,'HIGH');
472     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
473        write_to_act_log('Error in executing remote_insert_list_headers procedure', 'LIST', g_list_header_id,'HIGH');
474        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
475     else
476        write_to_act_log('remote_insert_list_headers procedure executed successfully.' ,'LIST',g_list_header_id,'LOW');
477     end if;
478 
479 EXCEPTION
480    when others then
481       write_to_act_log('Error while executing update_remote_list_header '||sqlcode||'  '||sqlerrm,'LIST',g_list_header_id,'HIGH');
482 END UPDATE_REMOTE_LIST_HEADER;
483 ------------------------------------------------------------------------------
484 -- Start of Comments
485 --
486 -- NAME
487 --  migrate_lists
488 --
489 -- PURPOSE
490 --   This procedure migrates the list from the remote instance.
491 PROCEDURE migrate_lists(
492                             p_list_header_id NUMBER
493                             );
494 
495 
496 ------------------------------------------------------------------------------
497 -- Start of Comments
498 --
499 -- NAME
500 --   Update_List_Result_Text
501 --
502 -- PURPOSE
503 --   This procedure updates the result text field on the list header table with
504 --   processing information such as progress so far and any error messages
505 --   encountered.
506 --   The Procedure is an AUTONOMOUS_TRANSACTION which will cause the
507 --   messages to be saved even if the main transaction is rolled back.
508 --
509 -- CALLED BY.
510 --     Initialize_List.
511 --     Process_List_Actions.
512 --
513 -- NOTES
514 --
515 --
516 -- HISTORY
517 --   06/23/1999        tdonohoe            created
518 --                     gjoby           Not being used
519 -- End of Comments
520 ---------------------------------------------------------------------------------
521 PROCEDURE update_list_result_text is
522   PRAGMA AUTONOMOUS_TRANSACTION;
523   l_msg_data       VARCHAR2(2000);
524   l_msg_count      number;
525 BEGIN
526   l_msg_count :=  FND_MSG_PUB.Count_Msg;
527   IF(l_msg_count <> 0)THEN
528      FOR l_iterator IN 1 .. l_msg_count LOOP
529         FND_MSG_PUB.Get(FND_MSG_PUB.G_NEXT,
530                         FND_API.G_FALSE,
531                         l_msg_data,
532                         l_msg_count);
533         UPDATE ams_list_headers_all
534         SET    result_text    = result_text||' '|| l_msg_data
535         WHERE  list_header_id = g_list_header_id;
536       END LOOP;
537       COMMIT;
538    END IF;
539 end update_list_result_text;
540 
541 ----------------------------------------------------------------------------
542 -- Start of Comments
543 --
544 -- NAME
545 --    Update_List_Action_Dets
546 --
547 -- PURPOSE
548 --    1. Updates The No Available,
549 --                   No Requested,
550 --             No Used fields
551 --         for each List Select Action.
552 --
553 --    2. The Values in these fields will be calculated differently
554 --       depending on the row selection type of the list.
555 -- CALLED BY.
556 --    1. Random_List_Generation.
557 --    2. Check_Max_Entries_Dist_Pct.
558 --
559 --
560 -- HISTORY
561 --   07/29/1999        tdonohoe            created
562 --                     gjoby               Changed for hornet
563 -- End of Comments
564 ----------------------------------------------------------------------------
565 PROCEDURE UPDATE_LIST_ACTION_DETS( p_list_select_action_id in t_number ,
566                                    p_no_of_rows_used       in t_number ,
567                                    p_no_of_rows_available  in t_number ,
568                                    p_no_of_rows_duplicates in t_number      ) IS
569 BEGIN
570 
571  write_to_act_log(p_msg_data => 'Updating list selections. No of selections =  '||to_char(p_list_select_action_id.count),
572                   p_arc_log_used_by => 'LIST',
573 		  p_log_used_by_id  => g_list_header_id,
574 		  p_level => 'LOW');
575 
576 
577   FORALL I in p_list_select_action_id.first .. p_list_select_action_id.last
578      UPDATE ams_list_select_actions
579      SET    no_of_rows_used       = p_no_of_rows_used(i),
580             no_of_rows_available  = p_no_of_rows_available(i),
581 	    no_of_rows_duplicates = p_no_of_rows_duplicates(i)
582     WHERE  list_select_action_id = p_list_select_action_id(i);
583 
584  --write_to_act_log('Update_List_Action_Dets : Finished');
585 
586 EXCEPTION
587   WHEN OTHERS THEN
588        -- Minor error not rolling back
589        write_to_act_log(p_msg_data => 'Error while executing procedure update_list_action_dets '||sqlcode||sqlerrm,
590                         p_arc_log_used_by => 'LIST',
591                         p_log_used_by_id => g_list_header_id,
592 			p_level =>'HIGH');
593 END UPDATE_LIST_ACTION_DETS;
594 
595 ---------------------------------------------------------------------------------
596 -- Start of Comments
597 --
598 -- NAME
599 --    UPDATE_LIST_DETS
600 --
601 -- PURPOSE
602 --  1. Updates The No_of_Rows_In_List,
603 --                 No_of_Rows_In_Ctrl_Group,
604 --                 No_of_Rows_Duplicates,
605 --                 Last_Generation_Success_Flag,
606 --                 Main_Gen_End_Time columns on The Ams_List_Headers_All table.
607 --                     gjoby               Changed for hornet
608 
609 -- CALLED BY.
610 --    1. Generate_List.
611 
612 --
613 -- HISTORY
614 -- END of Comments
615 
616 procedure Update_List_Dets(p_list_header_id IN NUMBER,
617                            x_return_status OUT NOCOPY varchar2) is
618 
619 --------------------------------------------------------------
620 --This Variable stores the result of cursor c_list_gen_type.--
621 --------------------------------------------------------------
622 l_generation_type AMS_LIST_HEADERS_ALL.GENERATION_TYPE%TYPE;
623 
624 -- updates the count of ams_list_entries and list selections
625 -- Calls update_list_action_dets for updating selection records
626 -- If the list criteria is not met then the list status is set back to draft
627 ------------------------------------------------------------------------------
628 --gets the number of entries in the list for a particular list select action--
629 ------------------------------------------------------------------------------
630 CURSOR C_LIST_ACTION_DETS(p_list_header_id NUMBER) IS
631 SELECT b.no_of_rows_active Count,
632        sum(decode(e.enabled_flag,'Y',1,0)),
633        e.List_select_action_id,
634        a.distribution_pct,
635        a.rank rank_col,
636        sum(decode(e.marked_as_duplicate_flag,'Y',1,0))
637 FROM   ams_list_entries e,
638        ams_list_select_actions a
639        ,ams_list_headers_all b
640 WHERE  e.list_header_id = p_list_header_id
641 AND    e.list_select_action_id = a.list_select_action_id
642 AND    a.arc_action_used_by = 'LIST'
643 AND    a.action_used_by_id = p_list_header_id
644 AND    b.list_header_id = a.incl_object_id
645 and    a.arc_incl_object_from = 'LIST'
646 GROUP  BY e.list_select_action_id,a.distribution_pct,a.rank,b.no_of_rows_active
647 UNION ALL
648 SELECT COUNT(e.List_Entry_Id) Count,
649        sum(decode(e.enabled_flag,'Y',1,0)),
650        e.List_select_action_id,
651        a.distribution_pct,
652        a.rank rank_col,
653        sum(decode(e.marked_as_duplicate_flag,'Y',1,0))
654 FROM   ams_list_entries e,
655        ams_list_select_actions a
656 WHERE  e.list_header_id = p_list_header_id
657 AND    e.list_select_action_id = a.list_select_action_id
658 AND    a.arc_action_used_by = 'LIST'
659 AND    a.action_used_by_id = p_list_header_id
660 and    a.arc_incl_object_from <> 'LIST'
661 GROUP  BY e.list_select_action_id,a.distribution_pct,a.rank
662 ORDER  BY rank_col desc ;
663 
664 CURSOR C_LIST_ACTION_DETS_TG(p_list_header_id NUMBER) IS
665 SELECT COUNT(e.List_Entry_Id),
666        sum(decode(e.enabled_flag,'Y',1,0)),
667        a.List_select_action_id,
668        a.distribution_pct,
669        a.rank,
670        sum(decode(e.marked_as_duplicate_flag,'Y',1,0))
671 FROM   ams_list_entries e,
672        ams_list_select_actions a,
673        ams_act_lists t
674 WHERE  e.list_header_id = p_list_header_id
675 AND    e.list_select_action_id   = t.act_list_header_id
676 and    t.list_header_id = a.INCL_OBJECT_ID
677 AND    a.arc_action_used_by = 'LIST'
678 AND    a.action_used_by_id = p_list_header_id
679 GROUP  BY a.list_select_action_id,a.distribution_pct,a.rank
680 ORDER  BY a.rank desc ;
681 
682 cursor c_count_list_entries(cur_p_list_header_id number) is
683 select sum(decode(enabled_flag,'N',0,1)),
684        sum(decode(enabled_flag,'Y',0,1)),
685        sum(1),
686        sum(decode(part_of_control_group_flag,'Y',1,0)),
687        sum(decode(marked_as_random_flag,'Y',1,0)),
688        sum(decode(marked_as_duplicate_flag,'Y',1,0)),
689        sum(decode(manually_entered_flag,
690                      'Y',decode(enabled_flag,'Y','1',0),
691                      0)),
692        sum(decode(MARKED_AS_SUPPRESSED_FLAG,'Y',1,0)),
693        sum(decode(MARKED_AS_FATIGUED_FLAG,'Y',1,0)),
694        sum(decode(TCA_LOAD_STATUS,'ERROR',1,0))
695 from ams_list_entries
696 where list_header_id = cur_p_list_header_id ;
697 
698 l_tca_error_recs	number;
699 l_TCA_FAILED_RECORDS	number;
700 
701 cursor c_tca_error is
702 select count(1) from ams_list_entries
703 where list_header_id = p_list_header_id
704   and TCA_LOAD_STATUS = 'ERROR';
705 
706 l_list_type	varchar2(100);
707 cursor c_list_type is
708 select list_type from ams_list_headers_all where list_header_id = p_list_header_id;
709 
710 
711 l_list_entry_action_count NUMBER;
712 l_list_select_action_id   NUMBER;
713 l_distribution_pct        NUMBER;
714 l_rank                    NUMBER;
715 l_iterator                NUMBER := 1;
716 l_min_rows                number;
717 l_new_status              varchar2(30);
718 l_new_status_id           number;
719 
720 l_no_of_rows_duplicates         number;
721 l_no_of_rows_in_list            number;
722 l_no_of_rows_active             number;
723 l_no_of_rows_inactive           number;
724 l_no_of_rows_manually_entered   number;
725 l_no_of_rows_in_ctrl_group      number;
726 l_no_of_rows_random             number;
727 l_no_of_rows_used               number;
728 l_no_of_rows_suppressed         number := 0;
729 l_no_of_rows_fatigued           number := 0;
730 t_list_select_action_id   t_number;
731 t_no_of_rows_requested    t_number;
732 t_no_of_rows_available    t_number;
733 t_no_of_rows_used         t_number;
734 t_no_of_rows_duplicates   t_number;
735 l_no_of_rows_dup          number;
736 x_msg_count		  number;
737 x_msg_data		  varchar2(2000);
738 
739 /* Added by rrajesh on 08/02/04. Bugfix: 3799191*/
740 CURSOR C_GET_LIST_TYPE(p_list_header_id NUMBER) IS
741    select list_type from ams_list_headers_all
742    where list_header_id = p_list_header_id;
743 l_type   varchar2(30);
744 /* End Bugfix: 3799191*/
745 
746 BEGIN
747   if g_list_header_id is null then
748      g_list_header_id := p_list_header_id;
749   end if;
750   write_to_act_log('Executing update_list_dets to update the list header details','LIST', g_list_header_id,'LOW');
751 /* Bugfix: 3799191. Modified by rrajesh. Number of TCA error records not getting updated for remote based TG */
752 Open C_GET_LIST_TYPE(p_list_header_id);
753 Fetch C_GET_LIST_TYPE into l_type;
754 Close C_GET_LIST_TYPE;
755 --if g_remote_list_gen = 'N' then
756 if ((g_remote_list_gen = 'N') OR (l_type = 'TARGET'))
757 then
758 /* End Bugfix: 3799191. */
759   open c_count_list_entries(p_list_header_id);
760   fetch c_count_list_entries
761    into l_no_of_rows_active            ,
762         l_no_of_rows_inactive          ,
763         l_no_of_rows_in_list           ,
764         l_no_of_rows_in_ctrl_group     ,
765         l_no_of_rows_random            ,
766         l_no_of_rows_duplicates        ,
767         l_no_of_rows_manually_entered  ,
768         l_no_of_rows_suppressed,
769         l_no_of_rows_fatigued,
770         l_TCA_FAILED_RECORDS;
771   close c_count_list_entries;
772  ELSE
773   write_to_act_log('Calling remote procedure to update list header details','LIST', g_list_header_id,'LOW');
774   execute immediate
775       'BEGIN
776          AMS_Remote_ListGen_PKG.remote_list_status_detils'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11)'||';'||
777       ' END;'
778       using  p_list_header_id,
779              OUT l_no_of_rows_active,
780              OUT l_no_of_rows_inactive,
781              OUT l_no_of_rows_in_list,
782              OUT l_no_of_rows_in_ctrl_group,
783 	     OUT l_no_of_rows_random,
784 	     OUT l_no_of_rows_duplicates,
785 	     OUT l_no_of_rows_manually_entered,
786              OUT x_msg_count,
787              OUT x_msg_data,
788              OUT x_return_status;
789 
790    if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
791       write_to_act_log('Error in executing remote procedure while updating list details', 'LIST', g_list_header_id,'HIGH');
792       write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
793    else
794       write_to_act_log('Remote procedure executed. No of active rows '||l_no_of_rows_active||' , No of inactive rows '||l_no_of_rows_inactive||
795                         ' ,No of rows in list '||l_no_of_rows_in_list||' , No of duplicates '||l_no_of_rows_duplicates,'LIST', g_list_header_id,'LOW');
796    end if;
797 
798 
799  end if;
800 
801 
802 
803 -- Start changes for migration
804 /*
805  SELECT nvl(no_of_rows_min_requested,0)
806  INTO   l_min_rows
807  FROM   ams_list_headers_all
808  WHERE  list_header_id = p_list_header_id;
809 
810  if l_min_rows > l_no_of_rows_active then
811     l_new_status :=  'DRAFT';
812     l_new_status_id   :=  300;
813  else
814     l_new_status :=  'AVAILABLE';
815     l_new_status_id   :=  303;
816  end if;
817 */
818 
819 --  if l_no_of_rows_active >= 0 then
820     l_new_status := 'AVAILABLE';
821     l_new_status_id := 303;
822 --  end if;
823 
824 -- End changes for migration
825 
826   update ams_list_headers_all
827   set no_of_rows_in_list           = nvl(l_no_of_rows_in_list,0),
828       no_of_rows_active            = nvl(l_no_of_rows_active,0),
829       no_of_rows_inactive          = nvl(l_no_of_rows_inactive,0),
830       no_of_rows_in_ctrl_group     = nvl(l_no_of_rows_in_ctrl_group,0),
831       no_of_rows_random            = nvl(l_no_of_rows_random,0),
832       no_of_rows_duplicates        = nvl(l_no_of_rows_duplicates,0),
833       no_of_rows_manually_entered  = nvl(l_no_of_rows_manually_entered,0),
834       no_of_rows_suppressed        = nvl(l_no_of_rows_suppressed,0),
835       NO_OF_ROWS_FATIGUED          = nvl(l_no_of_rows_fatigued,0),
836       TCA_FAILED_RECORDS           = nvl(l_TCA_FAILED_RECORDS,0),
837       last_generation_success_flag = decode(l_new_status_id,303,'Y','N'),
838       -- MIGRATION_DATE               = decode(g_remote_list_gen,'Y',null,MIGRATION_DATE),
839       status_code                  = l_new_status,
840       user_status_id               = l_new_status_id,
841       status_date                  = sysdate,
842       last_update_date             = sysdate,
843       main_gen_end_time            = sysdate,
844       no_of_rows_initially_selected = g_no_of_rows_ini_selected,
845       remote_gen_flag              = nvl(g_remote_list_gen,'N')
846   WHERE  list_header_id            = p_list_header_id;
847   write_to_act_log('No of rows in list/target group = '||nvl(l_no_of_rows_in_list,0),'LIST', g_list_header_id,'HIGH');
848   write_to_act_log('No of active rows in list/target group = '||nvl(l_no_of_rows_active,0),'LIST', g_list_header_id,'HIGH');
849   write_to_act_log('No of inactive rows in list/target group = '||nvl(l_no_of_rows_inactive,0),'LIST', g_list_header_id,'HIGH');
850   write_to_act_log('No of duplicates in list/target group = '||nvl(l_no_of_rows_duplicates,0),'LIST', g_list_header_id,'HIGH');
851   write_to_act_log('List header table updated','LIST', g_list_header_id,'LOW');
852   -- Added for cancel list gen as it prevents parallel update- Raghu
853   -- of list headers when cancel button is pressed
854   commit;
855 
856   if g_remote_list = 'Y' then
857      -- bug # 3839014. even if the tg, based on remote DS, is generated locally we are
858      --calling tca_updload_process..so we need this validation,
859         open c_tca_error;
860 	fetch c_tca_error into l_tca_error_recs;
861 	close c_tca_error;
862 	/* Modified by rrajesh on 08/02/04. Bugfix: 3799191. Even if one record fails,
863 	the target group/list status should be failed */
864 	--if l_no_of_rows_in_list = l_tca_error_recs then
865 	if l_tca_error_recs > 0 then
866 	/* End fix: 3799191 */
867 	  update ams_list_headers_all
868 		set status_code = 'FAILED',
869 		user_status_id  = 311
870       WHERE list_header_id = p_list_header_id;
871       write_to_act_log('TCA fields not mapped for atleast one record. Marking the status as FAILED.','LIST', g_list_header_id,'HIGH');
872   -- Added for cancel list gen as it prevents parallel update- Raghu
873   -- of list headers when cancel button is pressed
874       commit;
875     end if;
876 end if;
877 
878 open  c_list_type;
879 fetch c_list_type into l_list_type;
880 close c_list_type;
881 
882  write_to_act_log('Updating list select actions for each selection.'||l_list_type, 'LIST', g_list_header_id,'LOW');
883  OPEN C_LIST_ACTION_DETS(p_list_header_id);
884  LOOP
885     FETCH C_LIST_ACTION_DETS INTO l_list_entry_action_count,
886                                   l_no_of_rows_used  ,
887                                   l_list_select_action_id,
888                                   l_distribution_pct,
889                                   l_rank,
890                                   l_no_of_rows_dup;
891 
892     EXIT WHEN C_LIST_ACTION_DETS%NOTFOUND;
893 
894     t_list_select_action_id(l_iterator) := l_list_select_action_id;
895     t_no_of_rows_requested(l_iterator)  := l_list_entry_action_count;
896     t_no_of_rows_available(l_iterator)  := l_list_entry_action_count;
897     t_no_of_rows_used(l_iterator)       := l_no_of_rows_used               ;
898     t_no_of_rows_duplicates(l_iterator) := l_no_of_rows_dup;
899     write_to_act_log('Calling update_list_action_dets to update the list selection '||l_list_select_action_id, 'LIST', g_list_header_id,'LOW');
900     update_list_action_dets( t_list_select_action_id,
901                              t_no_of_rows_used,
902                              t_no_of_rows_available,
903 			     t_no_of_rows_duplicates);
904 
905     l_iterator := l_iterator +1;
906 
907   END LOOP;
908   CLOSE C_LIST_ACTION_DETS;
909 -- end if;
910 
911 
912 if l_list_type = 'TARGET' then
913  l_iterator := 1;
914  write_to_act_log('Updating selections in TG ', 'LIST', g_list_header_id,'LOW');
915  OPEN C_LIST_ACTION_DETS_TG(p_list_header_id);
916 -- dbms_output.put_line('UPDATE_LIST_DETS: 2222222');
917  LOOP
918     FETCH C_LIST_ACTION_DETS_TG INTO l_list_entry_action_count,
919                                   l_no_of_rows_used  ,
920                                   l_list_select_action_id,
921                                   l_distribution_pct,
922                                   l_rank,
923                                   l_no_of_rows_dup;
924 
925     EXIT WHEN C_LIST_ACTION_DETS_TG%NOTFOUND;
926     t_list_select_action_id(l_iterator) := l_list_select_action_id;
927     t_no_of_rows_requested(l_iterator)  := l_list_entry_action_count;
928     t_no_of_rows_available(l_iterator)  := l_list_entry_action_count;
929     t_no_of_rows_used(l_iterator)       := l_no_of_rows_used               ;
930     t_no_of_rows_duplicates(l_iterator) := l_no_of_rows_dup;
931     write_to_act_log('Calling update_list_action_dets for updating the TG selection '||l_list_select_action_id, 'LIST', g_list_header_id,'LOW');
932     update_list_action_dets( t_list_select_action_id,
933                              t_no_of_rows_used,
934                              t_no_of_rows_available,
935                              t_no_of_rows_duplicates);
936 
937     l_iterator := l_iterator +1;
938 
939   END LOOP;
940   CLOSE C_LIST_ACTION_DETS_TG;
941 
942 end if;
943 
944 write_to_act_log('List summary details updated in header and selection tables','LIST', g_list_header_id,'LOW');
945 
946 EXCEPTION
947  WHEN OTHERS THEN
948   -- Minor error not rolling back
949    write_to_act_log( 'Error while executing procedure update_list_dets '||sqlcode||'   '||sqlerrm, 'LIST',  g_list_header_id,'HIGH');
950 END UPDATE_LIST_DETS ;
951 
952 
953 
954 
955 -----------------------------------------------------------------------------
956 --  Delete_List_Entries
957 --  1.  Delete List entries which may have occured from previous generations
958 ---     of the list. Table Name : ams_list_tmp_entries and ams_list_entries
959 --  2.  Delete from ams_list_src_type_usages
960 --      List_src_type_usages store the source type code against each type
961 --      of list
962 --  10/29/1999 TDONOHOE   Created
963 --  01/24/2001 GJOBY      Changed from function to Procedure
964 --                        Added FND Message procedures
965 ------------------------------------------------------------------------------
966 
967 PROCEDURE delete_list_entries(p_list_header_id     number ,
968                               x_msg_count      OUT NOCOPY number,
969                               x_msg_data       OUT NOCOPY varchar2,
970                               x_return_status  OUT NOCOPY varchar2)  IS
971 
972 l_delete_action varchar2(80);
973 l_total_recs	number;
974 l_null		varchar2(30) := null;
975 -- deletes from ams_list_entries in case of standard type of generation
976 BEGIN
977    write_to_act_log('Executing delete_list_entries to delete entries from ams_list_entries(Since list/target group is generated in STANDARD mode).', 'LIST', g_list_header_id,'LOW');
978    x_return_status := FND_API.G_RET_STS_SUCCESS;
979 -------------------------------------------------------------------------------
980 --Delete all existing entries for this list which are in the temporary table.--
981 -------------------------------------------------------------------------------
982    l_delete_action   := 'List tmp Entries delete';
983 ----------------------------------------------
984 --Delete all existing entries for this list.--
985 ----------------------------------------------
986    l_delete_action   := 'List Entries delete';
987    DELETE FROM ams_list_entries
988    WHERE  list_header_id = p_list_header_id;
989    write_to_act_log(sql%rowcount||' entries deleted from ams_list_entries in local instance.', 'LIST', g_list_header_id,'LOW');
990    /********************************************************************
991     Dynamic procedure will delete the list from the remote instance in
992     case of remote list
993    *********************************************************************/
994    if g_remote_list = 'Y' then
995       write_to_act_log('Calling remote procedure with process type as DELETE_LIST_ENTRIES to delete entries in remote instance', 'LIST', g_list_header_id,'LOW');
996       execute immediate
997       'BEGIN
998        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
999       ' END;'
1000       using  '1',
1001              l_null,
1002              'T',
1003              l_null,
1004              OUT x_return_status,
1005              OUT x_msg_count,
1006              OUT x_msg_data,
1007              p_list_header_id,
1008              l_null,
1009              l_null,
1010              OUT l_total_recs,
1011              'DELETE_LIST_ENTRIES';
1012       if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
1013          write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
1014          write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
1015       else
1016          write_to_act_log('Entries deleted succesfully in remote instance','LIST', g_list_header_id,'LOW');
1017      end if;
1018    end if;
1019 ------------------------------------------------------------------------------
1020 --Delete all entries in the ams_list_src_type_usages table.                 --
1021 --These entries must be refreshed each time that a list is refreshed.       --
1022 ------------------------------------------------------------------------------
1023    l_delete_action   := 'List Source Type usages';
1024    write_to_act_log('Deleting entries from list src type usages tables.', 'LIST', g_list_header_id,'LOW');
1025    DELETE FROM ams_list_src_type_usages
1026    WHERE  list_header_id = p_list_header_id;
1027    write_to_act_log('Procedure delete_list_entries executed successfully.', 'LIST', g_list_header_id,'LOW');
1028 EXCEPTION
1029     WHEN OTHERS THEN
1030        write_to_act_log('Error while executing delete_list_entries '||sqlcode || ' '||sqlerrm, 'LIST',g_list_header_id,'HIGH');
1031        FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
1032        FND_MESSAGE.Set_Token('TEXT', 'Delete List Entries ' ||
1033                          l_delete_action || ' '|| SQLERRM||' '||SQLCODE);
1034        FND_MSG_PUB.Add;
1035        x_return_status  := FND_API.G_RET_STS_ERROR ;
1036 
1037 END delete_list_entries;
1038 
1039 
1040 
1041 -----------------------------------------------------------------------------
1042 --  initialize_list_header
1043 --
1044 --  PURPOSE
1045 --  list header fields must be initialized.
1046 --  updates the result_text, main_gen_start_time, main_gen_end_time
1047 --  01/24/2001 GJOBY      Modified for hornet
1048 -----------------------------------------------------------------------------
1049 PROCEDURE initialize_list_header (p_list_header_id     NUMBER,
1050                                   x_msg_count      OUT NOCOPY number,
1051                                   x_msg_data       OUT NOCOPY varchar2,
1052                                   x_return_status  OUT NOCOPY VARCHAR2)  IS
1053 BEGIN
1054   write_to_act_log('Executing procedure initialize_list_header', 'LIST', g_list_header_id,'LOW');
1055   UPDATE ams_list_headers_all
1056   SET  result_text         = NULL,
1057        main_gen_start_time = SYSDATE,
1058        last_update_date    = SYSDATE,
1059        main_gen_end_time   = NULL
1060   WHERE  list_header_id    =   p_list_header_id;
1061   -- Added for cancel list gen as it prevents parallel update- Raghu
1062   -- of list headers when cancel button is pressed
1063   commit;
1064 
1065   UPDATE ams_list_select_actions
1066   SET  no_of_rows_used =  0
1067   WHERE  arc_action_used_by = 'LIST'
1068     and  action_used_by_id    =   p_list_header_id;
1069 
1070   x_return_status  := FND_API.G_RET_STS_SUCCESS;
1071 
1072 EXCEPTION
1073     WHEN OTHERS THEN
1074        write_to_act_log('Error while executing initialize_list_header procedure', 'LIST', g_list_header_id,'HIGH');
1075        FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
1076        FND_MESSAGE.Set_Token('TEXT', 'Initialize List Header ' ||
1077                                SQLERRM||' '||SQLCODE);
1078        FND_MSG_PUB.Add;
1079        x_return_status  := FND_API.G_RET_STS_ERROR ;
1080 END initialize_list_header;
1081 
1082 -------------------------------------------------------------------------------
1083 -- Start of Comments
1084 -- NAME initialize_list
1085 -- PURPOSE
1086 --  1. Deletes the log information from ams_act_logs
1087 --  2. Initializes List header (initialize_list_header)
1088 --  3. deletes list entries    (delete_list_entries )
1089 -- CALLED BY.
1090 --    1. Generate_List.
1091 -- HISTORY
1092 --   06/21/1999   tdonohoe  created
1093 --   01/20/2000   tdonohoe  added code to delete any existing log entries
1094 --                          for the list.
1095 --   01/23/2001   gjoby     Modification for Hornet Release
1096 --                          Changed the initiliaze list to procedure
1097 --                          Removed unnecessary procedures
1098 --                          removed the global variables
1099 --                          Changed from function to procedure
1100 --                          Error logging at each stage
1101 -- END of Comments
1102 -----------------------------------------------------------------------------
1103 PROCEDURE initialize_list
1104      (p_list_header_rec    ams_listheader_pvt.list_header_rec_type,
1105       x_msg_count          OUT NOCOPY number,
1106       x_msg_data           OUT NOCOPY varchar2,
1107       x_return_status      OUT NOCOPY VARCHAR2   ) IS
1108 BEGIN
1109    write_to_act_log('Executing procedure initialize_list', 'LIST', g_list_header_id,'LOW');
1110    x_return_status := FND_API.G_RET_STS_SUCCESS;
1111    -----------------------------------------------------------------
1112    --Delete any existing log entries for this list.               --
1113    -----------------------------------------------------------------
1114    --DELETE FROM ams_act_logs
1115    --WHERE  arc_act_log_used_by = 'LIST'
1116    --AND    act_log_used_by_id  = p_list_header_rec.list_header_id ;
1117    --write_to_act_log(sql%rowcount||' entries deleted from ams_act_logs table for this list.', 'LIST', g_list_header_id,'LOW');
1118 
1119    --------------------------------------------------------------------------
1120    -- Initializes the list header  generation date and time as null        --
1121    --------------------------------------------------------------------------
1122    initialize_list_header(p_list_header_id => p_list_header_rec.list_header_id,
1123                           x_msg_count      => x_msg_count,
1124                           x_msg_data       => x_msg_data,
1125                           x_return_status  => x_return_status);
1126    if x_return_status <> FND_API.g_ret_sts_success then
1127          write_to_act_log('Error while executing initialize_list_header. Unable to Initialize List ', 'LIST', g_list_header_id,'HIGH');
1128       raise FND_API.g_exc_unexpected_error;
1129    end if;
1130    -----------------------------------------------------------------
1131    --Deleting any existing entries. if generation type is Standard--
1132    -----------------------------------------------------------------
1133    if p_list_header_rec.generation_type = 'STANDARD' then
1134       write_to_act_log('Calling delete_list_entries to delete the existing entries.(The generation type is STANDARD).', 'LIST', g_list_header_id,'LOW');
1135       delete_list_entries (p_list_header_id => p_list_header_rec.list_header_id,
1136                            x_msg_count      => x_msg_count,
1137                            x_msg_data       => x_msg_data,
1138                            x_return_status  => x_return_status);
1139       if x_return_status <> FND_API.g_ret_sts_success then
1140          write_to_act_log('Error while executing delete_list_entries. Unable to delete entries.', 'LIST', g_list_header_id,'HIGH');
1141          raise FND_API.g_exc_unexpected_error;
1142       end if;
1143   end if;
1144   write_to_act_log('Procedure initialize_list executed successfully.', 'LIST', g_list_header_id,'LOW');
1145    -- CHECK if we need to initlialize list select actions and
1146    -- and list header number of rows etc.
1147 EXCEPTION
1148    WHEN FND_API.g_exc_error THEN
1149      write_to_act_log('Error while executing procedure initialize_list '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
1150       x_return_status := FND_API.g_ret_sts_error;
1151       FND_MSG_PUB.count_and_get(
1152             p_encoded => FND_API.g_false,
1153             p_count   => x_msg_count,
1154             p_data    => x_msg_data
1155       );
1156 
1157    WHEN FND_API.g_exc_unexpected_error THEN
1158      write_to_act_log('Error while executing procedure initialize_list '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
1159       x_return_status := FND_API.g_ret_sts_unexp_error ;
1160       FND_MSG_PUB.count_and_get(
1161             p_encoded => FND_API.g_false,
1162             p_count   => x_msg_count,
1163             p_data    => x_msg_data
1164       );
1165 
1166    WHEN OTHERS THEN
1167      write_to_act_log('Error while executing procedure initialize_list '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
1168      x_return_status := FND_API.g_ret_sts_unexp_error ;
1169      IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
1170         FND_MSG_PUB.add_exc_msg(g_pkg_name, g_file_name);
1171      END IF;
1172      FND_MSG_PUB.count_and_get(
1173             p_encoded => FND_API.g_false,
1174             p_count   => x_msg_count,
1175             p_data    => x_msg_data
1176       );
1177 
1178 END initialize_list;
1179 
1180 ---------------------------------------------------------------------------
1181 -- Start of Comments
1182 --
1183 -- NAME
1184 --    Insert_List_Mapping_Usage
1185 --
1186 -- PURPOSE
1187 
1188 --    1. Performs a Check to ensure that the list mapping has not already been
1189 --       inserted into the Usage table.
1190 --    2. Inserts a Mapping type into the ams_list_src_usages table for each
1191 --       master
1192 --       and sub type found in any list action source, i.e. List or WorkBooks.
1193 --    3. Tracking this information allows us to update sub sets of the lists
1194 --       entries in the future.
1195 -- HISTORY
1196 --  01/24/2001 GJOBY      Modified for hornet
1197 ---------------------------------------------------------------------------
1198 
1199 PROCEDURE insert_list_mapping_usage
1200                 (p_list_header_id   AMS_LIST_HEADERS_ALL.LIST_HEADER_ID%TYPE,
1201                  p_source_type_code AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE) IS
1202 
1203 l_found NUMBER;
1204 BEGIN
1205 
1206 
1207    INSERT INTO ams_list_src_type_usages
1208    (
1209       list_source_type_usage_id
1210       ,last_update_date
1211       ,last_updated_by
1212       ,creation_date
1213       ,created_by
1214       ,last_update_login
1215       ,object_version_number
1216       ,source_type_code
1217       ,list_header_id
1218     )
1219     select
1220       AMS_LIST_SRC_TYPE_USAGES_S.NEXTVAL,
1221       SYSDATE,
1222       FND_GLOBAL.USER_ID,
1223       SYSDATE,
1224       FND_GLOBAL.USER_ID,
1225       FND_GLOBAL.USER_ID,
1226       1,
1227       p_source_type_code,
1228       p_list_header_id
1229    from dual
1230    where not exists
1231       ( select  'x'
1232         from  ams_list_src_type_usages
1233         where list_header_id = p_list_header_id
1234           and source_type_code = p_source_type_code ) ;
1235 EXCEPTION
1236   WHEN OTHERS THEN
1237     write_to_act_log('Error while executing procedure insert_list_mapping_usage '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
1238 END INSERT_LIST_MAPPING_USAGE;
1239 
1240 PROCEDURE process_imph
1241              (p_action_used_by_id in number,
1242               p_incl_object_id in number,
1243               p_list_action_type  in varchar2,
1244               p_list_select_action_id   in number,
1245               p_order_number   in number,
1246               p_rank   in number,
1247               p_include_control_group  in varchar2,
1248               x_msg_count      OUT NOCOPY number,
1249               x_msg_data       OUT NOCOPY varchar2,
1250               x_return_status  IN OUT NOCOPY VARCHAR2,
1251               x_std_sql OUT NOCOPY varchar2 ,
1252               x_include_sql OUT NOCOPY varchar2
1253               ) is
1254 l_list_entry_source_type  varchar2(30);
1255 cursor c_get_source_type
1256 is select  decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST')
1257 from ams_imp_list_headers_all
1258 where  import_list_header_id = p_incl_object_id;
1259 l_no_of_chunks  number;
1260 
1261 l_created_by                NUMBER;
1262 
1263 CURSOR cur_get_created_by (x_list_header_id IN NUMBER) IS
1264       SELECT created_by
1265       FROM ams_list_headers_all
1266       WHERE list_header_id= x_list_header_id;
1267 
1268 BEGIN
1269     write_to_act_log('Executing process_imph since imported list has been included in list/target group selections.', 'LIST', g_list_header_id,'LOW');
1270     open  c_get_source_type ;
1271     fetch c_get_source_type into l_list_entry_source_type  ;
1272     close  c_get_source_type ;
1273     write_to_act_log('List entry source type is '||l_list_entry_source_type, 'LIST', g_list_header_id,'LOW');
1274 
1275 
1276     if p_list_action_type  = 'INCLUDE' then
1277        --get_count(p_list_select_action_id,p_order_number,'IMPH',null);
1278        if   l_list_entry_source_type <> 'PERSON_LIST' then
1279           l_created_by := 0;
1280 
1281 	 OPEN cur_get_created_by(p_action_used_by_id);
1282 
1283 	 FETCH cur_get_created_by INTO l_created_by;
1284          CLOSE cur_get_created_by;
1285 
1286        x_include_sql  := ' insert into ams_list_entries
1287              (list_header_id ,
1288               list_entry_id,
1289               imp_source_line_id,
1290               object_version_number,
1291               source_code                     ,
1292               source_code_for_id              ,
1293               arc_list_used_by_source         ,
1294               arc_list_select_action_from     ,
1295               pin_code                        ,
1296               view_application_id             ,
1297               manually_entered_flag           ,
1298               marked_as_random_flag           ,
1299               marked_as_duplicate_flag        ,
1300               part_of_control_group_flag      ,
1301               exclude_in_triggered_list_flag  ,
1302               enabled_flag ,
1303               LIST_SELECT_ACTION_FROM_NAME,
1304               last_update_date,
1305               last_updated_by,
1306               creation_date,
1307               created_by,
1308               last_update_login,
1309               list_entry_source_system_id,
1310               list_entry_source_system_type,
1311               list_select_action_id,
1312 	      rank,
1313               ADDRESS_LINE1,
1314               ADDRESS_LINE2,
1315               COL127,
1316               COL128,
1317               COL227,
1318               CITY,
1319               COUNTRY,
1320               COL118,
1321               COL142,
1322               COL138,
1323               COL122,
1324               EMAIL_ADDRESS,
1325               COL239,
1326               FIRST_NAME,
1327               COL243,
1328               COL144,
1329 	      COL145, --Added by bmuthukr for bug 5156979
1330               LAST_NAME,
1331               COL251,
1332               COL252,
1333               COL137,
1334               SUFFIX,
1335               COL259,
1336               COL6,
1337               COL5,
1338               COL7,
1339               PHONE,
1340               ZIPCODE,
1341               COL120,
1342               STATE,
1343               COL125,
1344               COL2,
1345               TITLE,
1346               customer_name,
1347               party_id,
1348               COL276 ,
1349               NOTES                                    ,
1350               VEHICLE_RESPONSE_CODE                   ,
1351               SALES_AGENT_EMAIL_ADDRESS               ,
1352               RESOURCE_ID                              ,
1353               col147,
1354               location_id ,
1355               contact_point_id ,
1356               orig_system_reference,
1357               col116,
1358               col117,
1359 	      CUSTOM_COLUMN1,
1360 	      CUSTOM_COLUMN2,
1361 	      CUSTOM_COLUMN3,
1362 	      CUSTOM_COLUMN4,
1363 	      CUSTOM_COLUMN5,
1364 	      CUSTOM_COLUMN6,
1365 	      CUSTOM_COLUMN7,
1366 	      CUSTOM_COLUMN8,
1367 	      CUSTOM_COLUMN9,
1368 	      CUSTOM_COLUMN10,
1369 	      CUSTOM_COLUMN11,
1370 	      CUSTOM_COLUMN12,
1371 	      CUSTOM_COLUMN13,
1372 	      CUSTOM_COLUMN14,
1373 	      CUSTOM_COLUMN15,
1374 	      CUSTOM_COLUMN16,
1375 	      CUSTOM_COLUMN17,
1376 	      CUSTOM_COLUMN18,
1377 	      CUSTOM_COLUMN19,
1378 	      CUSTOM_COLUMN20,
1379 	      CUSTOM_COLUMN21,
1380 	      CUSTOM_COLUMN22,
1381 	      CUSTOM_COLUMN23,
1382 	      CUSTOM_COLUMN24,
1383 	      CUSTOM_COLUMN25,
1384               FAX
1385               )
1386               select
1387                      ' || p_action_used_by_id || ' ,
1388               ams_list_entries_s.nextval, import_source_line_id,
1389               1 ,' ||
1390               ''''||'NONE'                ||''''     || ','||
1391               0                           || ','     ||
1392               ''''||'NONE'                ||''''     || ','||
1393               ''''||'IMPH'                ||''''     || ','||
1394               'ams_list_entries_s.currval'|| ','||
1395               530              || ','||
1396               ''''||'N'  ||''''|| ','||
1397               ''''||'N'  ||''''|| ','||
1398               ''''||'N'  ||''''|| ','||
1399               ''''||'N'  ||''''|| ','||
1400               ''''||'N'  ||''''|| ','||
1401               ''''||'Y'  ||''''||',
1402               '||p_action_used_by_id || ',
1403               sysdate,
1404               FND_GLOBAL.USER_ID,
1405               sysdate,
1406               '||nvl(l_created_by,FND_GLOBAL.USER_ID)||',
1407               FND_GLOBAL.USER_ID,
1408               nvl(party_id,import_source_line_id), ' ||
1409               ''''|| l_list_entry_source_type||''''|| ' , '||
1410               p_list_select_action_id   || ' ,'||
1411               p_rank   ||',
1412               ADDRESS1,
1413               ADDRESS2,
1414               BEST_TIME_CONTACT_BEGIN,
1415               BEST_TIME_CONTACT_END,
1416               CEO_NAME,
1417               CITY,
1418               COUNTRY,
1419               COUNTY,
1420               DECISION_MAKER_FLAG,
1421               DEPARTMENT,
1422               DUNS_NUMBER,
1423               EMAIL_ADDRESS,
1424               EMPLOYEES_TOTAL,
1425               PERSON_FIRST_NAME,
1426               FISCAL_YEAREND_MONTH,
1427               JOB_TITLE,
1428 	      JOB_TITLE_CODE, --Added by bmuthukr for bug 5156979
1429               PERSON_LAST_NAME,
1430               LEGAL_STATUS,
1431               LINE_OF_BUSINESS,
1432               PERSON_MIDDLE_NAME,
1433               PERSON_NAME_SUFFIX,
1434               party_name,
1435               PHONE_AREA_CODE,
1436               PHONE_COUNTRY_CODE,
1437               PHONE_EXTENTION,
1438               PHONE_NUMBER,
1439               POSTAL_CODE,
1440               PROVINCE,
1441               STATE,
1442               TAX_REFERENCE,
1443               TIME_ZONE,
1444               PERSON_NAME_PREFIX,
1445               party_name,
1446               party_id,
1447               YEAR_ESTABLISHED,
1448               NOTES                                    ,
1449               VEHICLE_RESPONSE_CODE                   ,
1450               SALES_AGENT_EMAIL_ADDRESS               ,
1451               RESOURCE_ID                              ,
1452               ORGANIZATION_ID,
1453               location_id ,
1454               contact_point_id ,
1455               orig_system_reference,
1456               address3,
1457               address4,
1458 	      CUSTOM_COLUMN1,
1459 	      CUSTOM_COLUMN2,
1460 	      CUSTOM_COLUMN3,
1461 	      CUSTOM_COLUMN4,
1462 	      CUSTOM_COLUMN5,
1463 	      CUSTOM_COLUMN6,
1464 	      CUSTOM_COLUMN7,
1465 	      CUSTOM_COLUMN8,
1466 	      CUSTOM_COLUMN9,
1467 	      CUSTOM_COLUMN10,
1468 	      CUSTOM_COLUMN11,
1469 	      CUSTOM_COLUMN12,
1470 	      CUSTOM_COLUMN13,
1471 	      CUSTOM_COLUMN14,
1472 	      CUSTOM_COLUMN15,
1473 	      CUSTOM_COLUMN16,
1474 	      CUSTOM_COLUMN17,
1475 	      CUSTOM_COLUMN18,
1476 	      CUSTOM_COLUMN19,
1477 	      CUSTOM_COLUMN20,
1478 	      CUSTOM_COLUMN21,
1479 	      CUSTOM_COLUMN22,
1480 	      CUSTOM_COLUMN23,
1481 	      CUSTOM_COLUMN24,
1482 	      CUSTOM_COLUMN25,
1483 	      FAX_NUMBER
1484        from   ams_hz_b2b_mapping_v ail
1485             where  enabled_flag = '||''''||'Y'||''''||
1486              ' and import_list_header_id =' || p_incl_object_id   ||
1487              ' and nvl(party_id, import_source_line_id) in (' ;
1488           write_to_act_log('Insert statement constructed for imported B2B list', 'LIST', g_list_header_id,'LOW');
1489       else
1490        x_include_sql  := ' insert into ams_list_entries
1491              (list_header_id ,
1492               list_entry_id,
1493               imp_source_line_id,
1494               source_code                     ,
1495               source_code_for_id              ,
1496               arc_list_used_by_source         ,
1497               arc_list_select_action_from     ,
1498               pin_code                        ,
1499               view_application_id             ,
1500               manually_entered_flag           ,
1501               marked_as_random_flag           ,
1502               marked_as_duplicate_flag        ,
1503               part_of_control_group_flag      ,
1504               exclude_in_triggered_list_flag  ,
1505               enabled_flag ,
1506               LIST_SELECT_ACTION_FROM_NAME,
1507               last_update_date,
1508               last_updated_by,
1509               creation_date,
1510               created_by,
1511               last_update_login,
1512               object_version_number,
1513               list_entry_source_system_id,
1514               list_entry_source_system_type,
1515               list_select_action_id,
1516               rank,
1517               ADDRESS_LINE1,
1518               ADDRESS_LINE2,
1519               CITY,
1520               COL127,
1521               COL128,
1522               COL118,
1523               COUNTRY,
1524               FIRST_NAME,
1525               LAST_NAME,
1526               COL137,
1527               EMAIL_ADDRESS,
1528               col70,
1529               COL145,
1530               STATE,
1531               ZIPCODE,
1532               COL120,
1533               TITLE,
1534               COL2,
1535               col5,
1536               col6,
1537               PHONE,
1538               col7,
1539               party_id,
1540               customer_name,
1541               SUFFIX  ,
1542               NOTES                                    ,
1543               VEHICLE_RESPONSE_CODE                   ,
1544               SALES_AGENT_EMAIL_ADDRESS               ,
1545               RESOURCE_ID                              ,
1546               location_id,
1547               contact_point_id ,
1548               orig_system_reference,
1549               col116,
1550               col117,
1551 	      CUSTOM_COLUMN1,
1552 	      CUSTOM_COLUMN2,
1553 	      CUSTOM_COLUMN3,
1554 	      CUSTOM_COLUMN4,
1555 	      CUSTOM_COLUMN5,
1556 	      CUSTOM_COLUMN6,
1557 	      CUSTOM_COLUMN7,
1558 	      CUSTOM_COLUMN8,
1559 	      CUSTOM_COLUMN9,
1560 	      CUSTOM_COLUMN10,
1561 	      CUSTOM_COLUMN11,
1562 	      CUSTOM_COLUMN12,
1563 	      CUSTOM_COLUMN13,
1564 	      CUSTOM_COLUMN14,
1565 	      CUSTOM_COLUMN15,
1566 	      CUSTOM_COLUMN16,
1567 	      CUSTOM_COLUMN17,
1568 	      CUSTOM_COLUMN18,
1569 	      CUSTOM_COLUMN19,
1570 	      CUSTOM_COLUMN20,
1571 	      CUSTOM_COLUMN21,
1572 	      CUSTOM_COLUMN22,
1573 	      CUSTOM_COLUMN23,
1574 	      CUSTOM_COLUMN24,
1575 	      CUSTOM_COLUMN25,
1576 	      FAX
1577               )
1578             select
1579                      ' || p_action_used_by_id || ' ,
1580               ams_list_entries_s.nextval, ' ||
1581               ' import_source_line_id , ' ||
1582               ''''||'NONE'                ||''''     || ','||
1583              0                           || ','     ||
1584              ''''||'NONE'                ||''''     || ','||
1585              ''''||'IMPH'                ||''''     || ','||
1586              'ams_list_entries_s.currval'|| ','||
1587              530              || ','||
1588              ''''||'N'  ||''''|| ','||
1589              ''''||'N'  ||''''|| ','||
1590              ''''||'N'  ||''''|| ','||
1591              ''''||'N'  ||''''|| ','||
1592              ''''||'N'  ||''''|| ','||
1593              ''''||'Y'  ||''''||',
1594               '||p_action_used_by_id || ',
1595               sysdate,
1596               FND_GLOBAL.USER_ID,
1597               sysdate,
1598               '||nvl(l_created_by,FND_GLOBAL.USER_ID)||',
1599               FND_GLOBAL.USER_ID,
1600               1,
1601               nvl(party_id,import_source_line_id), ' ||
1602               ''''|| l_list_entry_source_type||''''|| ' , '||
1603               p_list_select_action_id   || ' ,'||
1604               p_rank   ||',
1605               ADDRESS1,
1606               ADDRESS2,
1607               CITY,
1608               BEST_TIME_CONTACT_BEGIN,
1609               BEST_TIME_CONTACT_END,
1610               COUNTY,
1611               COUNTRY,
1612               PERSON_FIRST_NAME,
1613               PERSON_LAST_NAME,
1614               PERSON_MIDDLE_NAME,
1615               EMAIL_ADDRESS,
1616               GENDER,
1617               HOUSEHOLD_INCOME,
1618               STATE,
1619               POSTAL_CODE,
1620               PROVINCE,
1621               PERSON_NAME_PREFIX,
1622               TIME_ZONE  ,
1623               PHONE_COUNTRY_CODE,
1624               PHONE_AREA_CODE   ,
1625               PHONE_NUMBER      ,
1626               PHONE_EXTENTION   ,
1627               party_id,
1628               PERSON_LAST_NAME || '|| ''''|| ' , ' || ''''||
1629                                  ' || PERSON_FIRST_NAME,
1630               PERSON_NAME_SUFFIX ,
1631               NOTES                                    ,
1632               VEHICLE_RESPONSE_CODE                   ,
1633               SALES_AGENT_EMAIL_ADDRESS               ,
1634               RESOURCE_ID                              ,
1635           location_id ,
1636           contact_point_id ,
1637               orig_system_reference,
1638               address3,
1639               address4 ,
1640 	      CUSTOM_COLUMN1,
1641 	      CUSTOM_COLUMN2,
1642 	      CUSTOM_COLUMN3,
1643 	      CUSTOM_COLUMN4,
1644 	      CUSTOM_COLUMN5,
1645 	      CUSTOM_COLUMN6,
1646 	      CUSTOM_COLUMN7,
1647 	      CUSTOM_COLUMN8,
1648 	      CUSTOM_COLUMN9,
1649 	      CUSTOM_COLUMN10,
1650 	      CUSTOM_COLUMN11,
1651 	      CUSTOM_COLUMN12,
1652 	      CUSTOM_COLUMN13,
1653 	      CUSTOM_COLUMN14,
1654 	      CUSTOM_COLUMN15,
1655 	      CUSTOM_COLUMN16,
1656 	      CUSTOM_COLUMN17,
1657 	      CUSTOM_COLUMN18,
1658 	      CUSTOM_COLUMN19,
1659 	      CUSTOM_COLUMN20,
1660 	      CUSTOM_COLUMN21,
1661 	      CUSTOM_COLUMN22,
1662 	      CUSTOM_COLUMN23,
1663 	      CUSTOM_COLUMN24,
1664 	      CUSTOM_COLUMN25,
1665               FAX_NUMBER
1666             from ams_hz_b2c_mapping_v
1667             where  enabled_flag = '||''''||'Y'||''''||
1668              ' and import_list_header_id =' || p_incl_object_id   ||
1669              ' and nvl(party_id, import_source_line_id) in (' ;
1670             write_to_act_log('Insert statement constructed for imported B2B list', 'LIST', g_list_header_id,'LOW');
1671         end if;
1672    end if;
1673    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
1674    if l_no_of_chunks is not null then
1675       for i in 1 ..l_no_of_chunks
1676         loop
1677            WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
1678       end loop;
1679    end if;
1680 /*
1681   commented OUT NOCOPY because of performance reasons
1682 
1683    x_std_sql := ' select party_idl_list_entry_source_type
1684                   ' from ams_imp_source_lines
1685                      where  import_list_header_id = ' ||   p_incl_object_id   ;
1686 */
1687    x_std_sql := ' select nvl(party_id,import_source_line_id)
1688                   from ams_imp_source_lines
1689                   where  import_list_header_id = ' ||   p_incl_object_id   ||
1690              '  and    nvl(duplicate_flag,' ||''''||'N'||''''||') = '||
1691                                               ''''||'N'||'''' ;
1692   write_to_act_log('Execution of procedure process_imph completed.', 'LIST', g_list_header_id,'LOW');
1693 END process_imph ;
1694 
1695 PROCEDURE process_list
1696              (p_action_used_by_id in number,
1697               p_incl_object_id in number,
1698               p_list_action_type  in varchar2,
1699               p_list_select_action_id   in number,
1700               p_order_number   in number,
1701               p_rank   in number,
1702               p_include_control_group  in varchar2,
1703               x_msg_count      OUT NOCOPY number,
1704               x_msg_data       OUT NOCOPY varchar2,
1705               x_return_status  IN OUT NOCOPY VARCHAR2,
1706               x_std_sql OUT NOCOPY varchar2 ,
1707               x_include_sql OUT NOCOPY varchar2
1708               ) is
1709 l_no_of_chunks  number;
1710 BEGIN
1711     write_to_act_log('Executing process_list since list has been included in list/target group selections.', 'LIST', g_list_header_id,'LOW');
1712     if p_list_action_type  = 'INCLUDE' then
1713         --bmuthukr for lpo
1714        --get_count(p_list_select_action_id,p_order_number,'LIST',null);
1715        x_include_sql := 'insert into ams_list_entries
1716         (list_header_id ,
1717          list_entry_id,
1718          object_version_number,
1719          source_code                     ,
1720          source_code_for_id              ,
1721          arc_list_used_by_source         ,
1722          arc_list_select_action_from     ,
1723          pin_code                        ,
1724          view_application_id             ,
1725          manually_entered_flag           ,
1726          marked_as_random_flag           ,
1727          marked_as_duplicate_flag        ,
1728          part_of_control_group_flag      ,
1729          exclude_in_triggered_list_flag  ,
1730          enabled_flag ,
1731          LIST_SELECT_ACTION_FROM_NAME,
1732          last_update_date,
1733          last_updated_by,
1734          creation_date,
1735          created_by,
1736          last_update_login,
1737          list_entry_source_system_id,
1738          list_entry_source_system_type,
1739          list_select_action_id,
1740  	 Rank,
1741          SUFFIX,
1742          FIRST_NAME,
1743          LAST_NAME,
1744          CUSTOMER_NAME,
1745          TITLE,
1746          ADDRESS_LINE1,
1747          ADDRESS_LINE2,
1748          CITY,
1749          STATE,
1750          ZIPCODE,
1751          COUNTRY,
1752          FAX,
1753          PHONE,
1754          EMAIL_ADDRESS,
1755          CUSTOMER_ID                              ,
1756          LIST_SOURCE                              ,
1757          PARTY_ID                                 ,
1758          PARENT_PARTY_ID                          ,
1759          IMP_SOURCE_LINE_ID                       ,
1760          COL1,
1761          COL2,
1762          COL3,
1763          COL4,
1764          COL5,
1765          COL6,
1766          COL7,
1767          COL8,
1768          COL9,
1769          COL10,
1770          COL11,
1771          COL12,
1772          COL13,
1773          COL14,
1774          COL15,
1775          COL16,
1776          COL17,
1777          COL18,
1778          COL19,
1779          COL20,
1780          COL21,
1781          COL22,
1782          COL23,
1783          COL24,
1784          COL25,
1785          COL26,
1786          COL27,
1787          COL28,
1788          COL29,
1789          COL30,
1790          COL31,
1791          COL32,
1792          COL33,
1793          COL34,
1794          COL35,
1795          COL36,
1796          COL37,
1797          COL38,
1798          COL39,
1799          COL40,
1800          COL41,
1801          COL42,
1802          COL43,
1803          COL44,
1804          COL45,
1805          COL46,
1806          COL47,
1807          COL48,
1808          COL49,
1809          COL50,
1810          COL51,
1811          COL52,
1812          COL53,
1813          COL54,
1814          COL55,
1815          COL56,
1816          COL57,
1817          COL58,
1818          COL59,
1819          COL60,
1820          COL61,
1821          COL62,
1822          COL63,
1823          COL64,
1824          COL65,
1825          COL66,
1826          COL67,
1827          COL68,
1828          COL69,
1829          COL70,
1830          COL71,
1831          COL72,
1832          COL73,
1833          COL74,
1834          COL75,
1835          COL76,
1836          COL77,
1837          COL78,
1838          COL79,
1839          COL80,
1840          COL81,
1841          COL82,
1842          COL83,
1843          COL84,
1844          COL85,
1845          COL86,
1846          COL87,
1847          COL88,
1848          COL89,
1849          COL90,
1850          COL91,
1851          COL92,
1852          COL93,
1853          COL94,
1854          COL95,
1855          COL96,
1856          COL97,
1857          COL98,
1858          COL99,
1859          COL100,
1860          COL101,
1861          COL102,
1862          COL103,
1863          COL104,
1864          COL105,
1865          COL106,
1866          COL107,
1867          COL108,
1868          COL109,
1869          COL110,
1870          COL111,
1871          COL112,
1872          COL113,
1873          COL114,
1874          COL115,
1875          COL116,
1876          COL117,
1877          COL118,
1878          COL119,
1879          COL120,
1880          COL121,
1881          COL122,
1882          COL123,
1883          COL124,
1884          COL125,
1885          COL126,
1886          COL127,
1887          COL128,
1888          COL129,
1889          COL130,
1890          COL131,
1891          COL132,
1892          COL133,
1893          COL134,
1894          COL135,
1895          COL136,
1896          COL137,
1897          COL138,
1898          COL139,
1899          COL140,
1900          COL141,
1901          COL142,
1902          COL143,
1903          COL144,
1904          COL145,
1905          COL146,
1906          COL147,
1907          COL148,
1908          COL149,
1909          COL150,
1910          COL151,
1911          COL152,
1912          COL153,
1913          COL154,
1914          COL155,
1915          COL156,
1916          COL157,
1917          COL158,
1918          COL159,
1919          COL160,
1920          COL161,
1921          COL162,
1922          COL163,
1923          COL164,
1924          COL165,
1925          COL166,
1926          COL167,
1927          COL168,
1928          COL169,
1929          COL170,
1930          COL171,
1931          COL172,
1932          COL173,
1933          COL174,
1934          COL175,
1935          COL176,
1936          COL177,
1937          COL178,
1938          COL179,
1939          COL180,
1940          COL181,
1941          COL182,
1942          COL183,
1943          COL184,
1944          COL185,
1945          COL186,
1946          COL187,
1947          COL188,
1948          COL189,
1949          COL190,
1950          COL191,
1951          COL192,
1952          COL193,
1953          COL194,
1954          COL195,
1955          COL196,
1956          COL197,
1957          COL198,
1958          COL199,
1959          COL200,
1960          COL201,
1961          COL202,
1962          COL203,
1963          COL204,
1964          COL205,
1965          COL206,
1966          COL207,
1967          COL208,
1968          COL209,
1969          COL210,
1970          COL211,
1971          COL212,
1972          COL213,
1973          COL214,
1974          COL215,
1975          COL216,
1976          COL217,
1977          COL218,
1978          COL219,
1979          COL220,
1980          COL221,
1981          COL222,
1982          COL223,
1983          COL224,
1984          COL225,
1985          COL226,
1986          COL227,
1987          COL228,
1988          COL229,
1989          COL230,
1990          COL231,
1991          COL232,
1992          COL233,
1993          COL234,
1994          COL235,
1995          COL236,
1996          COL237,
1997          COL238,
1998          COL239,
1999          COL240,
2000          COL241,
2001          COL242,
2002          COL243,
2003          COL244,
2004          COL245,
2005          COL246,
2006          COL247,
2007          COL248,
2008          COL249,
2009          COL250 ,
2010          COL251     ,
2011          COL252     ,
2012          COL253     ,
2013          COL254     ,
2014          COL256     ,
2015          COL255     ,
2016          COL257     ,
2017          COL258     ,
2018          COL259     ,
2019          COL260     ,
2020          COL261     ,
2021          COL262     ,
2022          COL263     ,
2023          COL264     ,
2024          COL265     ,
2025          COL266     ,
2026          COL267     ,
2027          COL268     ,
2028          COL269     ,
2029          COL270     ,
2030          COL271     ,
2031          COL272     ,
2032          COL273     ,
2033          COL274     ,
2034          COL275     ,
2035          COL276     ,
2036          COL277     ,
2037          COL278     ,
2038          COL279     ,
2039          COL280     ,
2040          COL281     ,
2041          COL282     ,
2042          COL283     ,
2043          COL284     ,
2044          COL285     ,
2045          COL286     ,
2046          COL287     ,
2047          COL288     ,
2048          COL289     ,
2049          COL290     ,
2050          COL291     ,
2051          COL292     ,
2052          COL293     ,
2053          COL294     ,
2054          COL295     ,
2055          COL296     ,
2056          COL297     ,
2057          COL298     ,
2058          COL299     ,
2059          COL300     ,
2060               NOTES                                    ,
2061               VEHICLE_RESPONSE_CODE                   ,
2062               SALES_AGENT_EMAIL_ADDRESS               ,
2063               RESOURCE_ID                              ,
2064               location_id ,
2065               contact_point_id ,
2066               orig_system_reference,
2067               CUSTOM_COLUMN1,
2068               CUSTOM_COLUMN2,
2069               CUSTOM_COLUMN3,
2070               CUSTOM_COLUMN4,
2071               CUSTOM_COLUMN5,
2072               CUSTOM_COLUMN6,
2073               CUSTOM_COLUMN7,
2074               CUSTOM_COLUMN8,
2075               CUSTOM_COLUMN9,
2076               CUSTOM_COLUMN10,
2077               CUSTOM_COLUMN11,
2078               CUSTOM_COLUMN12,
2079               CUSTOM_COLUMN13,
2080               CUSTOM_COLUMN14,
2081               CUSTOM_COLUMN15,
2082               CUSTOM_COLUMN16,
2083               CUSTOM_COLUMN17,
2084               CUSTOM_COLUMN18,
2085               CUSTOM_COLUMN19,
2086               CUSTOM_COLUMN20,
2087               CUSTOM_COLUMN21,
2088               CUSTOM_COLUMN22,
2089               CUSTOM_COLUMN23,
2090               CUSTOM_COLUMN24,
2091               CUSTOM_COLUMN25
2092         )
2093         select ' ||
2094          p_action_used_by_id ||',
2095          ams_list_entries_s.nextval,
2096          1 ,' ||
2097          ''''||'NONE'                ||''''     || ','||
2098         0                           || ','     ||
2099         ''''||'NONE'                ||''''     || ','||
2100         ''''||'LIST'                ||''''     || ','||
2101         'ams_list_entries_s.currval'|| ','||
2102         530              || ','||
2103         ''''||'N'  ||''''|| ','||
2104         ''''||'N'  ||''''|| ','||
2105         ''''||'N'  ||''''|| ','||
2106         ''''||'N'  ||''''|| ','||
2107         ''''||'N'  ||''''|| ','||
2108         ''''||'Y'  ||''''||',
2109          list_entry_source_system_id||list_entry_source_system_type,
2110          sysdate,
2111          last_updated_by,
2112          sysdate,
2113          created_by,
2114          last_update_login,
2115          list_entry_source_system_id,
2116          list_entry_source_system_type, '||
2117          p_list_select_action_id   ||','||
2118          p_rank   ||',
2119          SUFFIX,
2120          FIRST_NAME,
2121          LAST_NAME,
2122          CUSTOMER_NAME,
2123          TITLE,
2124          ADDRESS_LINE1,
2125          ADDRESS_LINE2,
2126          CITY,
2127          STATE,
2128          ZIPCODE,
2129           COUNTRY,
2130           FAX,
2131           PHONE,
2132           EMAIL_ADDRESS,
2133           CUSTOMER_ID                              ,
2134           LIST_SOURCE                              ,
2135           PARTY_ID                                 ,
2136           PARENT_PARTY_ID                          ,
2137           IMP_SOURCE_LINE_ID                       ,
2138           COL1,
2139           COL2,
2140           COL3,
2141           COL4,
2142           COL5,
2143           COL6,
2144           COL7,
2145           COL8,
2146           COL9,
2147           COL10,
2148           COL11,
2149           COL12,
2150           COL13,
2151           COL14,
2152           COL15,
2153           COL16,
2154           COL17,
2155           COL18,
2156           COL19,
2157           COL20,
2158           COL21,
2159           COL22,
2160           COL23,
2161           COL24,
2162           COL25,
2163           COL26,
2164           COL27,
2165           COL28,
2166           COL29,
2167           COL30,
2168           COL31,
2169           COL32,
2170           COL33,
2171           COL34,
2172           COL35,
2173           COL36,
2174           COL37,
2175           COL38,
2176           COL39,
2177           COL40,
2178           COL41,
2179           COL42,
2180           COL43,
2181           COL44,
2182           COL45,
2183           COL46,
2184           COL47,
2185           COL48,
2186           COL49,
2187           COL50,
2188           COL51,
2189           COL52,
2190           COL53,
2191           COL54,
2192           COL55,
2193           COL56,
2194           COL57,
2195           COL58,
2196           COL59,
2197           COL60,
2198           COL61,
2199           COL62,
2200           COL63,
2201           COL64,
2202           COL65,
2203           COL66,
2204           COL67,
2205           COL68,
2206           COL69,
2207           COL70,
2208           COL71,
2209           COL72,
2210           COL73,
2211           COL74,
2212           COL75,
2213           COL76,
2214           COL77,
2215           COL78,
2216           COL79,
2217           COL80,
2218           COL81,
2219           COL82,
2220           COL83,
2221           COL84,
2222           COL85,
2223           COL86,
2224           COL87,
2225           COL88,
2226           COL89,
2227           COL90,
2228           COL91,
2229           COL92,
2230           COL93,
2231           COL94,
2232           COL95,
2233           COL96,
2234           COL97,
2235           COL98,
2236           COL99,
2237           COL100,
2238           COL101,
2239           COL102,
2240           COL103,
2241           COL104,
2242           COL105,
2243           COL106,
2244           COL107,
2245           COL108,
2246           COL109,
2247           COL110,
2248           COL111,
2249           COL112,
2250           COL113,
2251           COL114,
2252           COL115,
2253           COL116,
2254           COL117,
2255           COL118,
2256           COL119,
2257           COL120,
2258           COL121,
2259           COL122,
2260           COL123,
2261           COL124,
2262           COL125,
2263           COL126,
2264           COL127,
2265           COL128,
2266           COL129,
2267           COL130,
2268           COL131,
2269           COL132,
2270           COL133,
2271           COL134,
2272           COL135,
2273           COL136,
2274           COL137,
2275           COL138,
2276           COL139,
2277           COL140,
2278           COL141,
2279           COL142,
2280           COL143,
2281           COL144,
2282           COL145,
2283           COL146,
2284           COL147,
2285           COL148,
2286           COL149,
2287           COL150,
2288           COL151,
2289           COL152,
2290           COL153,
2291           COL154,
2292           COL155,
2293           COL156,
2294           COL157,
2295           COL158,
2296           COL159,
2297           COL160,
2298           COL161,
2299           COL162,
2300           COL163,
2301           COL164,
2302           COL165,
2303           COL166,
2304           COL167,
2305           COL168,
2306           COL169,
2307           COL170,
2308           COL171,
2309           COL172,
2310           COL173,
2311           COL174,
2312           COL175,
2313           COL176,
2314           COL177,
2315           COL178,
2316           COL179,
2317           COL180,
2318           COL181,
2319           COL182,
2320           COL183,
2321           COL184,
2322           COL185,
2323           COL186,
2324           COL187,
2325           COL188,
2326           COL189,
2327           COL190,
2328           COL191,
2329           COL192,
2330           COL193,
2331           COL194,
2332           COL195,
2333           COL196,
2334           COL197,
2335           COL198,
2336           COL199,
2337           COL200,
2338           COL201,
2339           COL202,
2340           COL203,
2341           COL204,
2342           COL205,
2343           COL206,
2344           COL207,
2345           COL208,
2346           COL209,
2347           COL210,
2348           COL211,
2349           COL212,
2350           COL213,
2351           COL214,
2352           COL215,
2353           COL216,
2354           COL217,
2355           COL218,
2356           COL219,
2357           COL220,
2358           COL221,
2359           COL222,
2360           COL223,
2361           COL224,
2362           COL225,
2363           COL226,
2364           COL227,
2365           COL228,
2366           COL229,
2367           COL230,
2368           COL231,
2369           COL232,
2370           COL233,
2371           COL234,
2372           COL235,
2373           COL236,
2374           COL237,
2375           COL238,
2376           COL239,
2377           COL240,
2378           COL241,
2379           COL242,
2380           COL243,
2381           COL244,
2382           COL245,
2383           COL246,
2384           COL247,
2385           COL248,
2386           COL249,
2387           COL250 ,
2388           COL251 ,
2389           COL252 ,
2390           COL253 ,
2391           COL254 ,
2392           COL256 ,
2393           COL255 ,
2394           COL257 ,
2395           COL258 ,
2396           COL259 ,
2397           COL260 ,
2398           COL261 ,
2399           COL262 ,
2400           COL263 ,
2401           COL264 ,
2402           COL265 ,
2403           COL266 ,
2404           COL267 ,
2405           COL268 ,
2406           COL269 ,
2407           COL270 ,
2408           COL271 ,
2409           COL272 ,
2410           COL273 ,
2411           COL274 ,
2412           COL275 ,
2413           COL276 ,
2414           COL277 ,
2415           COL278 ,
2416           COL279 ,
2417           COL280 ,
2418           COL281 ,
2419           COL282 ,
2420           COL283 ,
2421           COL284 ,
2422           COL285 ,
2423           COL286 ,
2424           COL287 ,
2425           COL288 ,
2426           COL289 ,
2427           COL290 ,
2428           COL291 ,
2429           COL292 ,
2430           COL293 ,
2431           COL294 ,
2432           COL295 ,
2433           COL296 ,
2434           COL297 ,
2435           COL298 ,
2436           COL299 ,
2437           COL300     ,
2438               NOTES                                    ,
2439               VEHICLE_RESPONSE_CODE                   ,
2440               SALES_AGENT_EMAIL_ADDRESS               ,
2441               RESOURCE_ID                              ,
2442               location_id ,
2443               contact_point_id ,
2444               orig_system_reference,
2445               CUSTOM_COLUMN1,
2446               CUSTOM_COLUMN2,
2447               CUSTOM_COLUMN3,
2448               CUSTOM_COLUMN4,
2449               CUSTOM_COLUMN5,
2450               CUSTOM_COLUMN6,
2451               CUSTOM_COLUMN7,
2452               CUSTOM_COLUMN8,
2453               CUSTOM_COLUMN9,
2454               CUSTOM_COLUMN10,
2455               CUSTOM_COLUMN11,
2456               CUSTOM_COLUMN12,
2457               CUSTOM_COLUMN13,
2458               CUSTOM_COLUMN14,
2459               CUSTOM_COLUMN15,
2460               CUSTOM_COLUMN16,
2461               CUSTOM_COLUMN17,
2462               CUSTOM_COLUMN18,
2463               CUSTOM_COLUMN19,
2464               CUSTOM_COLUMN20,
2465               CUSTOM_COLUMN21,
2466               CUSTOM_COLUMN22,
2467               CUSTOM_COLUMN23,
2468               CUSTOM_COLUMN24,
2469               CUSTOM_COLUMN25
2470        from ams_list_entries
2471        where   list_header_id = ' ||   p_incl_object_id   ||
2472         '  and    nvl(enabled_flag,' ||''''||'N'||''''||') = '||
2473                                               ''''||'Y'||'''' ||
2474        ' and list_entry_source_system_id in (' ;
2475 
2476     /*Commented OUT NOCOPY due to perf  reasons
2477        and list_entry_source_system_id||list_entry_source_system_type in (' ;
2478    */
2479        INSERT INTO ams_list_src_type_usages
2480        (
2481           list_source_type_usage_id
2482           ,last_update_date
2483           ,last_updated_by
2484           ,creation_date
2485           ,created_by
2486           ,last_update_login
2487           ,object_version_number
2488           ,source_type_code
2489           ,list_header_id
2490         )
2491         select
2492           AMS_LIST_SRC_TYPE_USAGES_S.NEXTVAL,
2493           SYSDATE,
2494           FND_GLOBAL.USER_ID,
2495           SYSDATE,
2496           FND_GLOBAL.USER_ID,
2497           FND_GLOBAL.USER_ID,
2498           1,
2499           als.source_type_code,
2500           p_action_used_by_id
2501        from ams_list_src_type_usages  als
2502        where not exists
2503           ( select  'x'
2504             from  ams_list_src_type_usages  als1
2505             where als1.list_header_id = p_action_used_by_id -- p_incl_object_id
2506               and als.source_type_code = als1.source_type_code )
2507        and als.list_header_id = p_incl_object_id ;
2508    write_to_act_log('Insert statement constructed based on the list included.', 'LIST', g_list_header_id,'LOW');
2509    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
2510    for i in 1 ..l_no_of_chunks
2511      loop
2512         WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
2513    end loop;
2514    end if;
2515    --     WRITE_TO_ACT_LOG('Outside Include ');
2516 /*
2517   Commented OUT NOCOPY  because of preformance issues
2518   list_entry_source_syetem_type is not selected
2519    x_std_sql := ' select list_entry_source_system_id||
2520                      list_entry_source_system_type from ams_list_entries
2521                      where   list_header_id = ' ||   p_incl_object_id   ||
2522                      ' and     enabled_flag = ' || ''''||'Y' || '''' ;
2523 */
2524    x_std_sql := ' select list_entry_source_system_id
2525                      from ams_list_entries
2526                      where   list_header_id = ' ||   p_incl_object_id   ||
2527                      ' and     enabled_flag = ' || ''''||'Y' || '''' ;
2528   write_to_act_log('Execution of procedure process_list completed', 'LIST', g_list_header_id,'LOW');
2529 END process_list ;
2530 
2531 PROCEDURE process_manual
2532              (p_action_used_by_id in number,
2533               p_incl_object_id in number,
2534               p_list_action_type  in varchar2,
2535               p_list_select_action_id   in number,
2536               p_order_number   in number,
2537               p_rank   in number,
2538               p_include_control_group  in varchar2,
2539               x_msg_count      OUT NOCOPY number,
2540               x_msg_data       OUT NOCOPY varchar2,
2541               x_return_status  IN OUT NOCOPY VARCHAR2,
2542               x_std_sql OUT NOCOPY varchar2 ,
2543               x_include_sql OUT NOCOPY varchar2
2544              ) IS
2545 BEGIN
2546      process_list
2547              (p_action_used_by_id => p_action_used_by_id,
2548               p_incl_object_id => p_incl_object_id,
2549               p_list_action_type  => p_list_action_type,
2550               p_list_select_action_id   => p_list_select_action_id,
2551               p_order_number   => p_order_number,
2552               p_rank   => p_rank,
2553               p_include_control_group  => p_include_control_group,
2554               x_msg_count      => x_msg_count,
2555               x_msg_data       => x_msg_data,
2556               x_return_status  => x_return_status,
2557               x_std_sql => x_std_sql ,
2558               x_include_sql  => x_include_sql  );
2559 END;
2560 PROCEDURE process_standard
2561              (p_action_used_by_id in number,
2562               p_incl_object_id in number,
2563               p_list_action_type  in varchar2,
2564               p_list_select_action_id   in number,
2565               p_order_number   in number,
2566               p_rank   in number,
2567               p_include_control_group  in varchar2,
2568               x_msg_count      OUT NOCOPY number,
2569               x_msg_data       OUT NOCOPY varchar2,
2570               x_return_status  IN OUT NOCOPY VARCHAR2,
2571               x_std_sql OUT NOCOPY varchar2 ,
2572               x_include_sql OUT NOCOPY varchar2 ) is
2573 BEGIN
2574      process_list
2575              (p_action_used_by_id => p_action_used_by_id,
2576               p_incl_object_id => p_incl_object_id,
2577               p_list_action_type  => p_list_action_type,
2578               p_list_select_action_id   => p_list_select_action_id,
2579               p_order_number   => p_order_number,
2580               p_rank   => p_rank,
2581               p_include_control_group  => p_include_control_group,
2582               x_msg_count      => x_msg_count,
2583               x_msg_data       => x_msg_data,
2584               x_return_status  => x_return_status,
2585               x_std_sql => x_std_sql ,
2586               x_include_sql  => x_include_sql  );
2587 END;
2588 PROCEDURE check_char(p_input_string          in  varchar2
2589                     ,p_comma_valid  in varchar2
2590                     ,p_valid_string OUT NOCOPY varchar2) IS
2591 BEGIN
2592    if p_input_string = ' ' then
2593       p_valid_string :='Y';
2594    elsif p_input_string = fnd_global.newline then
2595       p_valid_string :='Y';
2596    elsif p_input_string = fnd_global.tab then
2597       p_valid_string :='Y';
2598    elsif p_input_string = '
2599 ' then
2600       p_valid_string :='Y';
2601    elsif p_input_string = ',' then
2602       if p_comma_valid = 'Y' then
2603          p_valid_string :='Y';
2604       else
2605          p_valid_string :='N';
2606       end if;
2607    else
2608       p_valid_string :='N';
2609    end if;
2610 exception
2611     when others then
2612       write_to_act_log('check_char  sqlerrm : ' || sqlerrm, 'LIST', g_list_header_id );
2613 END check_char;
2614 
2615 PROCEDURE form_sql_statement(p_select_statement in varchar2,
2616                              p_select_add_statement in varchar2,
2617                              p_master_type        in varchar2,
2618                              p_child_types     in child_type,
2619                              p_from_string in sql_string_4K,
2620                              p_action_used_by_id  in number,
2621                              p_list_select_action_id  in number,
2622                              p_list_action_type  in varchar2,
2623                              p_order_number in number,
2624                              p_rank  in number,
2625                              x_final_string OUT NOCOPY varchar2
2626                              ) is
2627 -- child_type      IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
2628 l_data_source_types varchar2(2000);
2629 l_field_col_tbl JTF_VARCHAR2_TABLE_100;
2630 l_source_col_tbl JTF_VARCHAR2_TABLE_100;
2631 l_view_tbl JTF_VARCHAR2_TABLE_100;
2632 l_source_col_dt_tbl JTF_VARCHAR2_TABLE_100;
2633 cursor c_master_source_type is
2634 select source_object_name , source_object_name || '.' || source_object_pk_field ,  list_source_type_id
2635 from ams_list_src_types
2636 where source_type_code = p_master_type;
2637 l_master_source_type_id number;
2638 cursor c_child_source_type (l_child_src_type varchar2 )is
2639 select a.source_object_name ,
2640        a.source_object_name || '.' || b.sub_source_type_pk_column
2641        ,b.master_source_type_pk_column
2642 from ams_list_src_types  a, ams_list_src_type_assocs b
2643 where a.source_type_code = l_child_src_type
2644 and   b.sub_source_type_id = a.list_source_type_id
2645 and   b.master_source_type_id =  l_master_source_type_id ;
2646 l_count                   number;
2647 l_master_object_name      varchar2(4000);
2648 l_child_object_name       varchar2(4000);
2649 l_master_primary_key      varchar2(1000);
2650 l_child_primary_key       varchar2(32767);
2651 l_from_clause             varchar2(32767);
2652 l_where_clause            varchar2(32767);
2653 l_select_clause           varchar2(32767);
2654 l_insert_clause           varchar2(32767);
2655 l_final_sql               varchar2(32767);
2656 l_insert_sql              varchar2(32767);
2657 l_no_of_chunks            number;
2658 l_master_fkey             Varchar2(1000);
2659 l_dummy_primary_key      varchar2(1000);
2660 
2661 l_created_by                NUMBER;
2662 
2663 CURSOR cur_get_created_by (x_list_header_id IN NUMBER) IS
2664       SELECT created_by
2665       FROM ams_list_headers_all
2666       WHERE list_header_id= x_list_header_id;
2667 
2668 begin
2669     WRITE_TO_ACT_LOG('Execution of procedure form_sql_statement started' || p_master_type, 'LIST', g_list_header_id,'LOW');
2670 open  c_master_source_type;
2671 fetch c_master_source_type into l_master_object_name , l_master_primary_key, l_master_source_type_id;
2672 close c_master_source_type;
2673    write_to_act_log('Master object name is ' || p_master_type||' , primary key is '||l_master_primary_key||' ,l_master_source_type_id is '||l_master_source_type_id, 'LIST', g_list_header_id,'LOW');
2674      --WRITE_TO_ACT_LOG('form_sql_statement->after master' || l_master_object_name);
2675 l_from_clause :=  ' FROM ' || l_master_object_name;
2676 l_data_source_types := ' ('|| ''''|| p_master_type ||'''';
2677 l_where_clause := 'where 1 = 1 ';
2678 
2679 l_count  := p_child_types.count();
2680 if l_count > 0  then
2681    for i in 1..p_child_types.last
2682    loop
2683       l_data_source_types := l_data_source_types || ','|| ''''
2684                              || p_child_types(i)||'''' ;
2685       open  c_child_source_type(p_child_types(i));
2686       fetch c_child_source_type into l_child_object_name , l_child_primary_key
2687                                      ,l_master_fkey;
2688       l_dummy_primary_key := '';
2689       if l_master_fkey is not null then
2690          l_dummy_primary_key     := l_master_object_name || '.'|| l_master_fkey;
2691       else
2692          l_dummy_primary_key      := l_master_primary_key;
2693       end if;
2694       l_from_clause := l_from_clause || ','|| l_child_object_name ;
2695       l_where_clause := l_where_clause || 'and '
2696                               ||l_dummy_primary_key || ' = '
2697                         || l_child_primary_key || '(+)';
2698       close c_child_source_type;
2699    end loop;
2700 end if;
2701 l_data_source_types := l_data_source_types || ') ' ;
2702 
2703 
2704   /* change made by savio here for bug 3916350  added condition AND   c.list_source_type = ''TARGET'' */
2705  EXECUTE IMMEDIATE
2706      'BEGIN
2707       SELECT b.field_column_name ,
2708                c.source_object_name,
2709                b.source_column_name,
2710                b.field_data_type
2711         BULK COLLECT INTO :1 ,:2  ,:3 ,:4
2712         FROM ams_list_src_fields b, ams_list_src_types c
2713         WHERE b.list_source_type_id = c.list_source_type_id
2714 	AND   c.list_source_type = ''TARGET''
2715           and b.DE_LIST_SOURCE_TYPE_CODE IN  '|| l_data_source_types ||
2716           ' AND b.ROWID >= (SELECT MAX(a.ROWID)
2717                             FROM ams_list_src_fields a
2718                            WHERE a.field_column_name= b.field_column_name
2719 	                    AND  a.DE_LIST_SOURCE_TYPE_CODE IN '
2720                                  || l_data_source_types || ') ;
2721       END; '
2722   USING OUT l_field_col_tbl ,OUT l_view_tbl , OUT l_source_col_tbl ,OUT l_source_col_dt_tbl;
2723 for i in 1 .. l_field_col_tbl.last
2724 loop
2725   l_insert_clause  := l_insert_clause || ' ,' || l_field_col_tbl(i) ;
2726   if l_source_col_dt_tbl(i) = 'DATE' then
2727      l_select_clause  := l_select_clause || ' ,' ||
2728                       'to_char('||l_view_tbl(i) || '.'||l_source_col_tbl(i)||','||''''||'DD-MM-RRRR'||''''||')' ;
2729   else
2730      l_select_clause  := l_select_clause || ' ,' ||
2731                       l_view_tbl(i) || '.'||l_source_col_tbl(i) ;
2732   end if;
2733 end loop;
2734   write_to_act_log('Insert clause formed is '||l_insert_clause, 'LIST', g_list_header_id,'LOW');
2735   write_to_act_log('Select clause formed is '||l_insert_clause, 'LIST', g_list_header_id,'LOW');
2736 
2737 
2738 l_created_by := 0;
2739 
2740        OPEN cur_get_created_by(p_action_used_by_id);
2741 
2742        FETCH cur_get_created_by INTO l_created_by;
2743        CLOSE cur_get_created_by;
2744 
2745   l_insert_sql := 'insert into ams_list_entries        '||
2746                    '( LIST_SELECT_ACTION_FROM_NAME,    '||
2747                    '  LIST_ENTRY_SOURCE_SYSTEM_ID ,    '||
2748                    '  LIST_ENTRY_SOURCE_SYSTEM_TYPE,   '||
2749                    ' list_select_action_id ,           '||
2750                    ' rank                  ,           '||
2751                    ' list_header_id,last_update_date,  '||
2752                    ' last_updated_by,creation_date,created_by,'||
2753                    'list_entry_id, '||
2754                    'object_version_number, ' ||
2755                    'source_code                     , ' ||
2756                    'source_code_for_id              , ' ||
2757                    'arc_list_used_by_source         , ' ||
2758                    'arc_list_select_action_from     , ' ||
2759                    'pin_code                        , ' ||
2760                    'view_application_id             , ' ||
2761                    'manually_entered_flag           , ' ||
2762                    'marked_as_random_flag           , ' ||
2763                    'marked_as_duplicate_flag        , ' ||
2764                    'part_of_control_group_flag      , ' ||
2765                    'exclude_in_triggered_list_flag  , ' ||
2766                    'enabled_flag ' ||
2767                    l_insert_clause || ' ) ' ||
2768 
2769                    'select ' ||
2770                    l_master_primary_key ||','||
2771                    l_master_primary_key ||','||
2772                    ''''||p_master_type||''''||','||
2773                    p_list_select_action_id || ',' ||
2774                    p_rank                  || ',' ||
2775                    to_char(p_action_used_by_id )|| ',' ||''''||
2776                    to_char(sysdate )|| ''''||','||
2777                    to_char(FND_GLOBAL.login_id )|| ',' ||''''||
2778                    to_char(sysdate )|| ''''||','||
2779                    to_char(nvl(l_created_by, FND_GLOBAL.login_id) )|| ',' ||
2780                    'ams_list_entries_s.nextval'  || ','||
2781                    1 || ','||
2782                    ''''||'NONE'                ||''''     || ','||
2783                    0                           || ','     ||
2784                    ''''||'NONE'                ||''''     || ','||
2785                    ''''||'NONE'                ||''''     || ','||
2786                    'ams_list_entries_s.currval'|| ','||
2787                    530              || ','||
2788                    ''''||'N'  ||''''|| ','||
2789                    ''''||'N'  ||''''|| ','||
2790                    ''''||'N'  ||''''|| ','||
2791                    ''''||'N'  ||''''|| ','||
2792                    ''''||'N'  ||''''|| ','||
2793                    ''''||'Y'  ||''''||
2794                    l_select_clause ;
2795 
2796 /* commented OUT NOCOPY becuase of performance reasons
2797      l_final_sql := l_insert_sql || '  ' ||
2798                   l_from_clause ||  '  '||
2799                   l_where_clause   || ' and  ' ||
2800                    l_master_primary_key ||
2801                      '||  '||''''||p_master_type ||''''|| ' in  ( ' ;
2802 */
2803      l_final_sql := l_insert_sql || '  ' ||
2804                   l_from_clause ||  '  '||
2805                   l_where_clause   || ' and  ' ||
2806                    l_master_primary_key|| ' in  ( ' ;
2807      x_final_string := l_final_sql;
2808   WRITE_TO_ACT_LOG('SQL statement formed finally ', 'LIST', g_list_header_id,'LOW');
2809      l_no_of_chunks  := ceil(length(l_final_sql)/2000 );
2810      for i in 1 ..l_no_of_chunks
2811      loop
2812         WRITE_TO_ACT_LOG(substrb(l_final_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
2813      end loop;
2814 exception
2815    when others then
2816      write_to_act_log('Error while executing procedure form_sql_statement '||sqlcode||'  '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
2817 end form_sql_statement;
2818 
2819 PROCEDURE process_insert_sql(p_select_statement in varchar2,
2820                              p_select_add_statement in varchar2,
2821                              p_master_type        in varchar2,
2822                              p_child_types     in child_type,
2823                              p_from_string in sql_string_4K,
2824                              p_action_used_by_id  in number,
2825                              p_list_select_action_id  in number,
2826                              p_list_action_type  in varchar2,
2827                              p_order_number in number,
2828                              p_rank  in number,
2829                              x_std_sql OUT NOCOPY varchar2 ,
2830                              x_include_sql OUT NOCOPY varchar2
2831                              ) is
2832 l_final_sql   varchar2(32767);
2833 l_insert_sql varchar2(32767);
2834 l_insert_sql1 varchar2(32767);
2835 l_table_name  varchar2(80) := ' ams_list_tmp_entries ';
2836 BEGIN
2837   write_to_act_log('Executing process_insert_sql procedure', 'LIST', g_list_header_id,'LOW');
2838 /*
2839   if p_list_action_type <> 'INCLUDE' then
2840     l_table_name := ' ams_list_delete_tmp_entries ';
2841   end if;
2842    l_insert_sql := 'insert into '|| l_table_name  || ' '||
2843                    '(list_entry_source_key, ' ||
2844                    ' list_entry_source_id,list_entry_source_type, ' ||
2845                    ' list_select_action_id ,' ||
2846                    ' list_header_id,last_update_date, ' ||
2847                    ' last_updated_by,creation_date,created_by,'||
2848                    ' rank) '||
2849                    p_select_statement || '  '||
2850                    p_select_add_statement || ' ,' ||
2851                    p_list_select_action_id || ',' ||
2852                    to_char(p_action_used_by_id )|| ',' ||''''||
2853                    to_char(sysdate )|| ''''||','||
2854                    to_char(FND_GLOBAL.login_id )|| ',' ||''''||
2855                    to_char(sysdate )|| ''''||','||
2856                    to_char(FND_GLOBAL.login_id )|| ',' ||
2857                    to_char( nvl(p_rank,9999999));
2858   --write_to_act_log(l_insert_sql);
2859 */
2860   l_insert_sql := p_select_statement ;
2861   for i in 1 .. p_from_string.last
2862   loop
2863 --    write_to_act_log('length p_from_string(i)= '||lengthb(p_from_string(i)), 'LIST', g_list_header_id,'LOW');
2864 --    write_to_act_log(p_from_string(i), 'LIST', g_list_header_id,'LOW');
2865     l_insert_sql  := l_insert_sql || p_from_string(i);
2866   end loop;
2867   x_std_sql := l_insert_sql;
2868 
2869   write_to_act_log('Calling form_sql_statement', 'LIST', g_list_header_id,'LOW');
2870   if p_list_action_type = 'INCLUDE' then
2871           form_sql_statement(p_select_statement ,
2872                              p_select_add_statement ,
2873                              p_master_type        ,
2874                              p_child_types     ,
2875                              p_from_string ,
2876                              p_action_used_by_id  ,
2877                              p_list_select_action_id  ,
2878                              p_list_action_type  ,
2879                              p_order_number ,
2880                              p_rank  ,
2881                              l_final_sql
2882                              ) ;
2883   end if;
2884   x_include_sql := l_final_sql;
2885   write_to_act_log('Procedure process_insert_sql completed', 'LIST', g_list_header_id,'LOW');
2886 exception
2887    when others then
2888    write_to_act_log('Error while executing procedure process_insert_sql  '||sqlcode||'   '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
2889 END process_insert_sql;
2890 
2891 PROCEDURE validate_sql_string
2892              (p_sql_string     in sql_string
2893               ,p_search_string in varchar2
2894               ,p_comma_valid   in varchar2
2895               ,x_found         OUT NOCOPY varchar2
2896               ,x_position      OUT NOCOPY number
2897               ,x_counter       OUT NOCOPY number
2898               )  IS
2899 l_sql_string_1           varchar2(2000) := ' ';
2900 l_sql_string_2           varchar2(2000) ;
2901 l_concat_string          varchar2(4000) ;
2902 l_valid_string           varchar2(1) := 'N';
2903 l_position               varchar2(200);
2904 BEGIN
2905 --  write_to_act_log('FIRST LINE IN validate_sql_string  ', 'LIST', g_list_header_id );
2906   /* Searching of the string is done by concatenating the two strings of
2907      2000 each   gjoby more expln needed
2908   */
2909   x_found    := 'N';
2910   l_sql_string_1  := lpad(l_sql_string_1,2000,' ');
2911 --  write_to_act_log('length(l_sql_string_1) =  '||length(l_sql_string_1), 'LIST', g_list_header_id );
2912 --  write_to_act_log('p_sql_string.count  =  '||p_sql_string.count, 'LIST', g_list_header_id );
2913 
2914   for i in 1 .. p_sql_string.last
2915   loop
2916 
2917      l_sql_string_2 := p_sql_string(i);
2918 --  write_to_act_log('length(l_sql_string_2) =  '||length(l_sql_string_2), 'LIST', g_list_header_id );
2919 --  write_to_act_log('p_search_string  =  '||p_search_string, 'LIST', g_list_header_id );
2920      if p_search_string = 'FROM' then
2921         l_concat_string := upper(l_sql_string_1) || upper(l_sql_string_2);
2922 --  write_to_act_log('IF length(l_concat_string) =  '||length(l_concat_string), 'LIST', g_list_header_id );
2923      else
2924         l_concat_string := l_sql_string_1 || l_sql_string_2;
2925 --  write_to_act_log('ELSE length(l_concat_string) =  '||length(l_concat_string), 'LIST', g_list_header_id );
2926      end if;
2927 
2928      x_position := instrb(l_concat_string ,p_search_string);
2929 --  write_to_act_log('x_position =   '||to_char(x_position), 'LIST', g_list_header_id );
2930      if x_position > 0 then
2931         loop
2932              l_valid_string := 'N' ;
2933              if x_position = 0 then
2934                 exit;
2935              else
2936                check_char
2937                    (p_input_string=>substrb(l_concat_string, x_position -1, 1)
2938                     ,p_comma_valid =>p_comma_valid
2939                     ,p_valid_string=> l_valid_string);
2940 --  write_to_act_log('ELSE check_char l_valid_string  =   '||l_valid_string, 'LIST', g_list_header_id );
2941                if l_valid_string = 'Y' then
2942                   check_char
2943                       (p_input_string=>substrb(l_concat_string,
2944                                  x_position + length(p_search_string)
2945                                   , 1)
2946                        ,p_comma_valid =>p_comma_valid
2947                        ,p_valid_string=> l_valid_string);
2948 --  write_to_act_log('l_valid_string = Y check_char l_valid_string  =   '||l_valid_string, 'LIST', g_list_header_id );
2949                end if;
2950              end if;
2951              if l_valid_string = 'Y' then
2952                 if x_position > 2000 then
2953                    x_found    := 'Y';
2954                    x_counter  := i;
2955                    x_position := x_position - 2000;
2956                    exit;
2957                 end if;
2958                 if x_position < 2001 then
2959                    x_found    := 'Y';
2960                    x_counter  := i -1 ;
2961                    exit;
2962                 end if;
2963              end if;
2964 /*
2965   write_to_act_log('BEFORE LOOP x_position ', 'LIST', g_list_header_id );
2966   write_to_act_log('length(l_concat_string) =  '||length(l_concat_string), 'LIST', g_list_header_id );
2967   write_to_act_log(''||substr(l_concat_string,1,2000), 'LIST', g_list_header_id );
2968   write_to_act_log(''||substr(l_concat_string,2001,4000), 'LIST', g_list_header_id );
2969   write_to_act_log('x_position+1  =   '||to_char(x_position+1), 'LIST', g_list_header_id );
2970   write_to_act_log('p_search_string =   '||p_search_string , 'LIST', g_list_header_id );
2971 */
2972               x_position := instrb(l_concat_string ,  p_search_string,x_position+1);
2973               -- x_position := instrb(l_concat_string , x_position+1, p_search_string);
2974 
2975 
2976 --  write_to_act_log('AFTER LOOP x_position  =   ', 'LIST', g_list_header_id );
2977 --  write_to_act_log('in LOOP x_position  =   '||x_position, 'LIST', g_list_header_id );
2978         end loop;
2979         exit;
2980      end if;
2981   l_sql_string_1 := l_sql_string_2;
2982   end loop;
2983 --  write_to_act_log('LAST LINE IN validate_sql_string  ', 'LIST', g_list_header_id );
2984 exception
2985     when others then
2986       write_to_act_log('validate_sql_string sqlerrm : ' || sqlerrm, 'LIST', g_list_header_id );
2987 END validate_sql_string;
2988 
2989 PROCEDURE get_child_types (p_sql_string in sql_string,
2990                            p_start_length      in number,
2991                            p_start_counter      in number,
2992                            p_end_length      in number,
2993                            p_end_counter      in number,
2994                            p_master_type_id     in number,
2995                            x_child_types     OUT NOCOPY child_type,
2996                            x_found     OUT NOCOPY varchar2 ) is
2997 
2998 
2999 cursor c_mapping_subtypes(p_master_type_id
3000                           ams_list_src_type_assocs.master_source_type_id%type)
3001 IS
3002 select source_type_code
3003 from   ams_list_src_types a,
3004        ams_list_src_type_assocs b
3005 where  b.master_source_type_id = p_master_type_id
3006   and  b.sub_source_type_id  = a.list_source_type_id
3007 and    b.enabled_flag = 'Y'
3008 and    a.enabled_flag = 'Y'
3009 and  exists (select 'x' from ams_list_src_fields
3010                  where list_source_type_id = b.sub_source_type_id
3011                    and field_column_name is not null) ;
3012 
3013 p_mapping_subtype_rec c_mapping_subtypes%rowtype;
3014 l_counter number :=0;
3015 l_child_count number :=0;
3016 l_position         number;
3017 l_found            varchar2(1) := 'N';
3018 l_sql_string    sql_string;
3019 
3020 BEGIN
3021   for i in 1 .. p_sql_string.last
3022   loop
3023     l_sql_string(i) := p_sql_string(i);
3024 
3025     if p_start_counter > i then
3026       l_sql_string(i) := substrb(lpad(' ',2000,' '),1,2000);
3027     elsif p_start_counter = i then
3028       l_sql_string(i) := substrb(lpad(' ',p_start_length -1 ,' ')||
3029                          substrb(l_sql_string(i),p_start_length ),1,2000);
3030     end if;
3031     if p_end_counter < i then
3032       l_sql_string(i) := substrb(lpad(' ',2000,' '),1,2000);
3033     elsif p_end_counter = i then
3034       l_sql_string(i) := substrb(rpad(substrb(l_sql_string(i),1,p_end_length ),2000,' '),1,2000);
3035 
3036     end if;
3037 
3038   end loop;
3039   open c_mapping_subtypes(p_master_type_id);
3040   loop
3041   fetch c_mapping_subtypes
3042      into p_mapping_subtype_rec;
3043   exit when c_mapping_subtypes%notfound;
3044 /*
3045      validate_sql_string(p_sql_string => l_sql_string ,
3046                       p_search_string => ''''||
3047                                          p_mapping_subtype_rec.source_type_code
3048                                          ||'''',
3049                       p_comma_valid   => 'Y',
3050                       x_found    => l_found,
3051 
3052                       x_position =>l_position,
3053                       x_counter => l_counter) ;
3054       if l_found = 'Y' then*
3055 */
3056          l_child_count := l_child_count +1;
3057          x_found := 'Y' ;
3058          x_child_types(l_child_count) := p_mapping_subtype_rec.source_type_code;
3059 
3060 
3061 --      end if;
3062   end loop;
3063 
3064   close c_mapping_subtypes;
3065 
3066       --l_found := 'Y' ;
3067       --l_child_count := l_child_count +1;
3068       --x_found := 'Y' ;
3069       --x_child_types(l_child_count) := 'FAX';
3070 END;
3071 
3072 procedure process_sql_string( p_sql_string in sql_string,
3073                               p_start_length in number,
3074                               p_start_counter in number,
3075                               p_end_length in number,
3076                               p_end_counter in number,
3077                               x_sql_string OUT NOCOPY sql_string ) is
3078 begin
3079   for i in 1 .. p_sql_string.last
3080   loop
3081     x_sql_string(i) := p_sql_string(i);
3082     if p_start_counter > i then
3083       x_sql_string(i) := substrb(lpad(' ',2000,' '),1,2000);
3084     elsif p_start_counter = i then
3085       x_sql_string(i) := substrb(lpad(' ',p_start_length -1 ,' ')||
3086                          substrb(x_sql_string(i),p_start_length ),1,2000);
3087     end if;
3088     if p_end_counter < i then
3089       x_sql_string(i) := substrb(lpad(' ',2000,' '),1,2000);
3090     elsif p_start_counter = i then
3091       x_sql_string(i) := substrb(rpad(substrb(x_sql_string(i),1,p_end_length ),2000,' '),1,2000);
3092     end if;
3093   end loop;
3094 end;
3095 
3096 PROCEDURE get_master_types
3097           (p_sql_string in sql_string,
3098            p_start_length in number,
3099            p_start_counter in number,
3100            p_end_length in number,
3101            p_end_counter in number,
3102            x_master_type_id OUT NOCOPY number,
3103            x_master_type OUT NOCOPY varchar2,
3104            x_found OUT NOCOPY varchar2,
3105            x_source_object_name OUT NOCOPY varchar2,
3106            x_source_object_pk_field  OUT NOCOPY varchar2)   IS
3107 cursor c_mapping_types IS
3108 SELECT list_source_type_id,
3109        source_type_code,
3110        source_object_name,
3111        source_object_pk_field
3112   FROM ams_list_src_types
3113  WHERE master_source_type_flag = 'Y'
3114    AND list_source_type in ('ANALYTICS', 'TARGET');
3115 
3116 cursor c_default_mapping_types IS
3117 SELECT a.list_source_type_id,
3118        a.source_type_code,
3119        a.source_object_name,
3120        a.source_object_pk_field
3121   FROM ams_list_src_types a,ams_list_headers_all b
3122  WHERE a.master_source_type_flag = 'Y'
3123    and a.source_type_code = b.list_source_type
3124    and b.list_header_id = g_list_header_id
3125    AND a.list_source_type in ('ANALYTICS', 'TARGET');
3126 
3127 p_mapping_type_rec c_mapping_types%rowtype;
3128 p_default_mapping_type_rec c_default_mapping_types%rowtype;
3129 l_position         number;
3130 l_counter          number;
3131 l_found            varchar2(1) := 'N';
3132 l_sql_string    sql_string;
3133 BEGIN
3134   process_sql_string( p_sql_string => p_sql_string ,
3135                       p_start_length => p_start_length,
3136                       p_start_counter => p_start_counter,
3137                       p_end_length => p_end_length,
3138                       p_end_counter => p_end_counter,
3139                       x_sql_string => l_sql_string ) ;
3140 
3141   open c_mapping_types;
3142   loop
3143   fetch c_mapping_types
3144      into p_mapping_type_rec;
3145   exit when c_mapping_types%notfound;
3146      validate_sql_string(p_sql_string => l_sql_string ,
3147                       p_search_string => ''''||
3148                                          p_mapping_type_rec.source_type_code
3149                                          ||'''',
3150                       p_comma_valid   => 'Y',
3151                       x_found    => l_found,
3152                       x_position =>l_position,
3153                       x_counter => l_counter) ;
3154       if l_found = 'Y' then
3155          x_found := 'Y' ;
3156          x_master_type_id          := p_mapping_type_rec.list_source_type_id;
3157          x_master_type             := p_mapping_type_rec.source_type_code;
3158          x_source_object_name      := p_mapping_type_rec.source_object_name;
3159          x_source_object_pk_field  := p_mapping_type_rec.source_object_pk_field;
3160          exit;
3161       end if;
3162   end loop;
3163   close c_mapping_types;
3164   if l_found = 'Y' then
3165      null;
3166   else
3167      open c_default_mapping_types;
3168      loop
3169      fetch c_default_mapping_types
3170      into p_default_mapping_type_rec;
3171      exit when c_default_mapping_types%notfound;
3172          x_found := 'Y' ;
3173          x_master_type_id          := p_default_mapping_type_rec.list_source_type_id;
3174          x_master_type             := p_default_mapping_type_rec.source_type_code;
3175          x_source_object_name      := p_default_mapping_type_rec.source_object_name;
3176          x_source_object_pk_field  := p_default_mapping_type_rec.source_object_pk_field;
3177      end loop;
3178      close c_default_mapping_types;
3179   end if;
3180 EXCEPTION
3181        WHEN  others THEN
3182          write_to_act_log('Error whie executing procedure get_master_types '||sqlcode||'   '||sqlerrm,'LIST', g_list_header_id,'HIGH');
3183 END get_master_types;
3184 
3185 PROCEDURE get_condition(p_sql_string in sql_string ,
3186                         p_search_string     in varchar2,
3187                         p_comma_valid   in varchar2,
3188                         x_position OUT NOCOPY number,
3189                         x_counter OUT NOCOPY number,
3190                         x_found    OUT NOCOPY varchar2,
3191                         x_sql_string OUT NOCOPY sql_string_4K) is
3192 l_where_position   number;
3193 l_where_counter   number;
3194 l_counter   number := 0;
3195 l_sql_string      sql_string;
3196 begin
3197   write_to_act_log('Executing procedure get_condition. Calling validate_sql_string','LIST', g_list_header_id,'LOW');
3198   validate_sql_string(p_sql_string => p_sql_string ,
3199                       p_search_string => p_search_string,
3200                       p_comma_valid   => 'N',
3201                       x_found    => x_found,
3202                       x_position =>x_position,
3203                       x_counter => x_counter) ;
3204 
3205   write_to_act_log('get_condition x_position =  '||x_position||' , x_counter = '||x_counter,'LIST', g_list_header_id,'LOW');
3206   if x_counter > 0 then
3207     for i in x_counter .. p_sql_string.last
3208     loop
3209       l_counter := l_counter +1;
3210       write_to_act_log('get_condition length(p_sql_string('||i||'))  =  '||length(p_sql_string(i)),'LIST', g_list_header_id,'LOW');
3211       x_sql_string(l_counter) := p_sql_string(i);
3212       if x_counter = i then
3213          write_to_act_log('get_condition lpad  =  '||length(lpad(substrb(x_sql_string(l_counter), x_position),2000) ),'LIST', g_list_header_id);
3214          x_sql_string(l_counter) := lpad(substrb(x_sql_string(l_counter), x_position),2000);
3215       end if;
3216     end loop;
3217   end if;
3218 exception
3219     when others then
3220       write_to_act_log('Error while executing procedure get_condition ' ||sqlcode||'    '||sqlerrm, 'LIST', g_list_header_id ,'HIGH');
3221 end get_condition;
3222 
3223 PROCEDURE process_all_sql  (p_action_used_by_id in number,
3224                             p_incl_object_id in number,
3225                             p_list_action_type  in varchar2,
3226                             p_list_select_action_id   in number,
3227                             p_order_number   in number,
3228                             p_rank   in number,
3229                             p_include_control_group  in varchar2,
3230                             p_sql_string    in sql_string,
3231                             p_primary_key   in  varchar2,
3232                             p_source_object_name in  varchar2,
3233                             x_msg_count      OUT NOCOPY number,
3234                             x_msg_data       OUT NOCOPY varchar2,
3235                             x_return_status  IN OUT NOCOPY VARCHAR2,
3236                             x_std_sql OUT NOCOPY varchar2 ,
3237                             x_include_sql OUT NOCOPY varchar2
3238                             ) is
3239 l_sql_string         sql_string;
3240 l_where_string       sql_string;
3241 -- l_from_string       sql_string;
3242 l_from_string       sql_string_4K;
3243 l_counter            NUMBER := 1;
3244 l_from_position      number;
3245 l_from_counter       number;
3246 l_end_position      number;
3247 l_end_counter       number;
3248 l_order_position      number;
3249 l_order_counter       number;
3250 l_group_position      number;
3251 l_group_counter       number;
3252 l_found              varchar2(1) := 'N';
3253 l_master_type        varchar2(80);
3254 l_master_type_id     number;
3255 l_source_object_name  varchar2(80);
3256 l_source_object_pk_field  varchar2(80);
3257 l_child_types        child_type;
3258 l_select_condition    varchar2(2000);
3259 l_select_add_condition    varchar2(2000);
3260 l_sql_string_v2           varchar2(4000);
3261 
3262 --for list processing order..
3263 l_no_of_rows_reqd      number := 0;
3264 l_string varchar2(32767);
3265 --for list processing order..
3266 
3267 BEGIN
3268   /* Validate Sql String will take all the sql statement fragement and
3269      check if the search string is present. If it is present it will
3270      return the position of fragement and the counter
3271   */
3272   --
3273   --for i in p_sql_string.first..p_sql_string.last
3274   --loop
3275   --   l_string := l_string||p_sql_string(i);
3276   --end loop;
3277   --get_count(p_list_select_action_id,p_order_number,'OTHERS',l_string);
3278   --g_reqd_num_tbl(p_order_number) := l_no_of_rows_reqd;
3279   --
3280 
3281   l_sql_string := p_sql_string;
3282   write_to_act_log('Executing process_all_sql to process the sql string','LIST',g_list_header_id,'LOW');
3283   l_found  := 'N';
3284   write_to_act_log('Calling validate_sql_string to validate the sql','LIST',g_list_header_id,'LOW');
3285   validate_sql_string(p_sql_string => l_sql_string ,
3286                       p_search_string => 'FROM',
3287                       p_comma_valid   => 'N',
3288                       x_found    => l_found,
3289                       x_position =>l_from_position,
3290                       x_counter => l_from_counter) ;
3291 
3292   if l_found = 'N' then
3293      write_to_act_log('Validation failed for this SQL string in validate_sql_string procedure'||l_found,'LIST', g_list_header_id,'HIGH');
3294      FND_MESSAGE.set_name('AMS', 'AMS_LIST_FROM_NOT_FOUND');
3295      FND_MSG_PUB.Add;
3296      RAISE FND_API.G_EXC_ERROR;
3297   end if;
3298   write_to_act_log('SQL string validated successfully in validate_sql_string','LIST', g_list_header_id,'LOW');
3299 
3300   l_found  := 'N';
3301   write_to_act_log('Calling get_master_types','LIST', g_list_header_id,'LOW');
3302   get_master_types (p_sql_string => l_sql_string,
3303                     p_start_length => 1,
3304                     p_start_counter => 1,
3305                     p_end_length => l_from_position,
3306                     p_end_counter => l_from_counter,
3307                     x_master_type_id=> l_master_type_id,
3308                     x_master_type=> l_master_type,
3309                     x_found=> l_found,
3310                     x_source_object_name => l_source_object_name,
3311                     x_source_object_pk_field  => l_source_object_pk_field);
3312 
3313   if l_found = 'N' then
3314      write_to_act_log('No master type found','LIST', g_list_header_id,'LOW');
3315      FND_MESSAGE.set_name('AMS', 'AMS_LIST_NO_MASTER_TYPE');
3316      FND_MSG_PUB.Add;
3317      RAISE FND_API.G_EXC_ERROR;
3318   end if;
3319 
3320   if p_list_action_type  = 'INCLUDE' then
3321      l_found  := 'N';
3322      write_to_act_log('Calling get_child_types','LIST', g_list_header_id,'LOW');
3323      get_child_types (p_sql_string => l_sql_string,
3324                       p_start_length => 1,
3325                       p_start_counter => 1,
3326                       p_end_length => l_from_position,
3327                       p_end_counter => l_from_counter,
3328                       p_master_type_id=> l_master_type_id,
3329                       x_child_types=> l_child_types,
3330                       x_found=> l_found);
3331 
3332      if l_found = 'Y' then
3333          write_to_act_log('No of child types '||l_child_types.count,'LIST', g_list_header_id,'LOW');
3334          if l_child_types.last > 0 then
3335             for i in 1 .. l_child_types.last
3336             loop
3337                write_to_act_log('Child type '|| l_child_types(i), 'LIST', g_list_header_id,'LOW');
3338 	       write_to_act_log('Calling insert list_mapping usage', 'LIST', g_list_header_id,'LOW');
3339                insert_list_mapping_usage
3340                       (p_list_header_id   => p_action_used_by_id,
3341                        p_source_type_code => l_child_types(i) ) ;
3342             end loop;
3343          end if;
3344      end if;
3345   end if;
3346 
3347   l_found  := 'N';
3348   write_to_act_log('Calling get_condition', 'LIST', g_list_header_id,'LOW');
3349   get_condition(p_sql_string => l_sql_string ,
3350                 p_search_string => 'FROM',
3351                 p_comma_valid   => 'N',
3352                 x_position =>l_from_position,
3353                 x_counter => l_from_counter,
3354                 x_found    => l_found,
3355                 x_sql_string => l_from_string) ;
3356 
3357   /* FOR SQL STATEMENTS  WHICH ARE NOT FROM THE DERIVING MASTER SOURCE TABLE  */
3358   if p_primary_key is not null then
3359      l_source_object_pk_field := p_primary_key;
3360      l_source_object_name     := p_source_object_name ;
3361   end if;
3362   l_select_condition := 'SELECT ' ||l_source_object_name||'.'
3363                         ||l_source_object_pk_field;
3364                         --||'||'||''''
3365                         --||l_master_type||'''';
3366   l_select_add_condition := ','||l_source_object_name||'.'
3367                         ||l_source_object_pk_field||','||''''
3368                         ||l_master_type||'''' ;
3369 
3370    write_to_act_log('Calling process_insert_sql', 'LIST', g_list_header_id,'LOW');
3371    process_insert_sql(p_select_statement       => l_select_condition,
3372                       p_select_add_statement   => l_select_add_condition,
3373                       p_master_type            => l_master_type,
3374                       p_child_types            => l_child_types,
3375                       p_from_string            => l_from_string  ,
3376                       p_list_select_action_id  => p_list_select_action_id  ,
3377                       p_action_used_by_id      => p_action_used_by_id ,
3378                       p_list_action_type       => p_list_action_type ,
3379                       p_order_number           => p_order_number,
3380                       p_rank                   => p_rank,
3381                       x_std_sql                => x_std_sql,
3382                       x_include_sql            => x_include_sql
3383                       );
3384 
3385    if  p_list_action_type   = 'INCLUDE' then
3386       write_to_act_log('Calling insert_list_mapping_usage', 'LIST', g_list_header_id,'LOW');
3387       insert_list_mapping_usage
3388                 (p_list_header_id   => p_action_used_by_id,
3389                  p_source_type_code => l_master_type ) ;
3390 
3391       if l_child_types.last > 0 then
3392          for i in 1 .. l_child_types.last
3393          loop
3394             write_to_act_log('child_type ->' || i || '->'||l_child_types(i), 'LIST', g_list_header_id,'LOW');
3395             insert_list_mapping_usage
3396                       (p_list_header_id   => p_action_used_by_id,
3397                        p_source_type_code => l_child_types(i) ) ;
3398          end loop;
3399       end if;
3400    end if;
3401    write_to_act_log('Execution of process_all_sql ends ', 'LIST', g_list_header_id,'LOW');
3402 EXCEPTION
3403    WHEN FND_API.G_EXC_ERROR THEN
3404      write_to_act_log('Error while executing process_all_sql '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
3405      x_return_status := FND_API.G_RET_STS_ERROR ;
3406       FND_MSG_PUB.count_and_get(
3407             p_encoded => FND_API.g_false,
3408             p_count   => x_msg_count,
3409             p_data    => x_msg_data
3410       );
3411 
3412   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3413      write_to_act_log('Error while executing process_all_sql '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
3414      x_return_status := FND_API.G_RET_STS_ERROR ;
3415       FND_MSG_PUB.count_and_get(
3416             p_encoded => FND_API.g_false,
3417             p_count   => x_msg_count,
3418             p_data    => x_msg_data
3419       );
3420 
3421   WHEN OTHERS THEN
3422      write_to_act_log('Error while executing process_all_sql '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
3423      x_return_status := FND_API.G_RET_STS_ERROR ;
3424       FND_MSG_PUB.count_and_get(
3425             p_encoded => FND_API.g_false,
3426             p_count   => x_msg_count,
3427             p_data    => x_msg_data
3428       );
3429 
3430 
3431 END process_all_sql;
3432 
3433 PROCEDURE process_sql (p_action_used_by_id in number,
3434               p_incl_object_id in number,
3435               p_list_action_type  in varchar2,
3436               p_list_select_action_id   in number,
3437               p_order_number   in number,
3438               p_rank   in number,
3439               p_include_control_group  in varchar2,
3440               x_msg_count      OUT NOCOPY number,
3441               x_msg_data       OUT NOCOPY varchar2,
3442               x_return_status  IN OUT NOCOPY VARCHAR2,
3443               x_std_sql OUT NOCOPY varchar2 ,
3444               x_include_sql OUT NOCOPY varchar2
3445               ) is
3446 
3447 ------------------------------------------------------------------------------
3448 -- Given the sql id from ams_list_select_actions it will retrieve the
3449 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
3450 -- workbook_name.
3451 ------------------------------------------------------------------------------
3452 cursor cur_sql  is
3453 SELECT query,primary_key, source_object_name
3454 FROM   ams_list_queries_all
3455 WHERE  (list_query_id = p_incl_object_id )
3456 or (parent_list_query_id = p_incl_object_id )
3457 order by sequence_order;
3458 
3459 /* sql_string column is obsolete: bug 4604653
3460 cursor cur_old_sql  is
3461 SELECT sql_string, primary_key, source_object_name
3462 FROM   ams_list_queries_all
3463 WHERE  (list_query_id = p_incl_object_id )
3464 or (parent_list_query_id = p_incl_object_id )
3465 order by sequence_order;
3466 */
3467 
3468 cursor cur_primary_key_sql  is
3469 SELECT lc.SOURCE_OBJECT_NAME, lc.SOURCE_OBJECT_PK_FIELD
3470 FROM   ams_list_queries_all  lq,
3471        ams_list_headers_all lh,
3472        ams_list_src_types  lc
3473 WHERE lq.list_query_id = p_incl_object_id
3474 and   lq.ARC_ACT_LIST_QUERY_USED_BY = 'LIST'
3475 and   lq.ACT_LIST_QUERY_USED_BY_ID = lh.list_header_id
3476 and   lc.source_type_code = lh.list_source_type;
3477 
3478 
3479 l_sql_string         sql_string;
3480 l_where_string       sql_string;
3481 l_from_string       sql_string;
3482 l_counter            NUMBER := 1;
3483 l_from_position      number;
3484 l_from_counter       number;
3485 l_end_position      number;
3486 l_end_counter       number;
3487 l_order_position      number;
3488 l_order_counter       number;
3489 l_group_position      number;
3490 l_group_counter       number;
3491 l_found              varchar2(1);
3492 l_master_type        varchar2(80);
3493 l_master_type_id     number;
3494 l_source_object_name  varchar2(80);
3495 l_source_object_pk_field  varchar2(80);
3496 l_child_types        child_type;
3497 l_select_condition    varchar2(2000);
3498 l_select_add_condition    varchar2(2000);
3499 l_sql_string_v2           varchar2(4000);
3500 l_primary_key          varchar2(80);
3501 l_no_pieces            number :=0;
3502 l_big_sql VARCHAR2(32767);
3503 BEGIN
3504     write_to_act_log('Executing procedure process_sql. Incl object id is '||p_incl_object_id  , 'LIST', g_list_header_id,'LOW');
3505   open cur_sql;
3506   loop
3507     fetch cur_sql into l_big_sql,l_primary_key,l_source_object_name;
3508     exit when cur_sql%notfound ;
3509 --    write_to_act_log('Process_sql query cursor:' || p_incl_object_id, 'LIST', g_list_header_id);
3510     l_no_pieces := ceil(length(l_big_sql)/2000);
3511     write_to_act_log('No of chunks for this sql' || l_no_pieces, 'LIST', g_list_header_id,'LOW');
3512     if l_no_pieces  > 0 then
3513        for i  in 1 .. l_no_pieces
3514        loop
3515          -- write_to_act_log('number of pieces:->' || i, 'LIST', g_list_header_id);
3516           --write_to_act_log('Process_sql: before ' );
3517           l_sql_string(l_counter):= substrb(l_big_sql,2000*i -1999,2000);
3518           --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
3519           l_counter  := l_counter +1 ;
3520        end loop;
3521     end if;
3522     -- l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
3523     -- l_counter  := l_counter +1 ;
3524   end loop;
3525   close cur_sql;
3526 
3527     write_to_act_log('lenth of pieces:' || l_no_pieces, 'LIST', g_list_header_id);
3528 /*
3529   if l_no_pieces = 0   or
3530      l_no_pieces is null then
3531      open cur_old_sql;
3532      loop
3533        fetch cur_old_sql into l_sql_string_v2,l_primary_key,l_source_object_name;
3534 --       write_to_act_log('Process_sql old cursor 4000->:' || p_incl_object_id, 'LIST', g_list_header_id);
3535        exit when cur_old_sql%notfound ;
3536        --write_to_act_log('Process_sql: before ' );
3537        l_sql_string(l_counter):= substrb(l_sql_string_v2,1,2000);
3538        --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
3539        l_counter  := l_counter +1 ;
3540        l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
3541        l_counter  := l_counter +1 ;
3542      end loop;
3543      close cur_old_sql;
3544   end if;
3545   */
3546   if l_source_object_name is null or
3547      l_primary_key is null then
3548      open cur_primary_key_sql  ;
3549      loop
3550        fetch cur_primary_key_sql into l_source_object_name,l_primary_key;
3551        exit when cur_primary_key_sql%notfound ;
3552      end loop;
3553      close cur_primary_key_sql  ;
3554   end if;
3555   write_to_act_log('Source object name is '||l_source_object_name||' and primary key is '||l_primary_key, 'LIST', g_list_header_id,'HIGH');
3556   process_all_sql(p_action_used_by_id => p_action_used_by_id ,
3557                   p_incl_object_id => p_incl_object_id ,
3558                   p_list_action_type  => p_list_action_type  ,
3559                   p_list_select_action_id   => p_list_select_action_id   ,
3560                   p_order_number   => p_order_number   ,
3561                   p_rank   => p_rank   ,
3562                   p_include_control_group  => p_include_control_group,
3563                   p_sql_string    => l_sql_string    ,
3564                   p_primary_key   => l_primary_key,
3565                   p_source_object_name   => l_source_object_name,
3566                   x_msg_count      => x_msg_count      ,
3567                   x_msg_data   => x_msg_data   ,
3568                   x_return_status   => x_return_status   ,
3569                   x_std_sql                => x_std_sql,
3570                   x_include_sql            => x_include_sql
3571                   );
3572   write_to_act_log('Procedure process_sql executed.', 'LIST', g_list_header_id,'LOW'  );
3573 exception
3574    when others then
3575     write_to_act_log('Error while executing procedure process_sql '||sqlcode||'  '|| sqlerrm, 'LIST', g_list_header_id,'HIGH');
3576     x_return_status := FND_API.G_RET_STS_ERROR ;
3577 END process_sql;
3578 
3579 PROCEDURE process_diwb (p_action_used_by_id in number,
3580               p_incl_object_id in number,
3581               p_list_action_type  in varchar2,
3582               p_list_select_action_id   in number,
3583               p_order_number   in number,
3584               p_rank   in number,
3585               p_include_control_group  in varchar2,
3586               x_msg_count      OUT NOCOPY number,
3587               x_msg_data       OUT NOCOPY varchar2,
3588               x_return_status  IN OUT NOCOPY VARCHAR2,
3589               x_std_sql OUT NOCOPY varchar2 ,
3590               x_include_sql OUT NOCOPY varchar2
3591               ) is
3592 
3593 ------------------------------------------------------------------------------
3594 -- Given the sql id from ams_list_select_actions it will retrieve the
3595 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
3596 -- workbook_name.
3597 ------------------------------------------------------------------------------
3598 --bmuthukr 4351740. this cursor should be used to pick the disc sql
3599 --since sql could be stored in more than one record this reqd.
3600 cursor cur_diwb(l_incl_object_id  in number )  is
3601 SELECT sql_string
3602 FROM   ams_discoverer_sql
3603 WHERE  (workbook_name, worksheet_name )
3604 IN
3605 ( SELECT workbook_name, worksheet_name
3606   FROM   ams_discoverer_sql
3607   WHERE  discoverer_sql_id = l_incl_object_id)
3608 ORDER BY sequence_order;
3609 
3610 l_sql_string         sql_string;
3611 l_where_string       sql_string;
3612 l_from_string       sql_string;
3613 l_counter            NUMBER := 1;
3614 l_from_position      number;
3615 l_from_counter       number;
3616 l_end_position      number;
3617 l_end_counter       number;
3618 l_order_position      number;
3619 l_order_counter       number;
3620 l_group_position      number;
3621 l_group_counter       number;
3622 l_found              varchar2(1);
3623 l_master_type        varchar2(80);
3624 l_master_type_id     number;
3625 l_source_object_name  varchar2(80);
3626 l_source_object_pk_field  varchar2(80);
3627 l_child_types        child_type;
3628 l_select_condition    varchar2(2000);
3629 l_select_add_condition    varchar2(2000);
3630 BEGIN
3631   write_to_act_log('Executing process_diwb since workbook has been included in list/target group selections.', 'LIST', g_list_header_id,'LOW');
3632 
3633   /* Populating l_sql_string with sql statements from ams_discoverer_sql
3634      l_sql_string is of table type of varchar2(2000)
3635   */
3636   open cur_diwb(p_incl_object_id);
3637   loop
3638     fetch cur_diwb into l_sql_string(l_counter);
3639     exit when cur_diwb%notfound ;
3640     --Added by bmuthukr for bug 3944161.
3641     if instr(l_sql_string(l_counter), 'ORDER BY') > 0  or instr(l_sql_string(l_counter), 'GROUP BY') > 0 then
3642       write_to_act_log('Workbook sql has ORDER BY or GROUP BY clause. Aborting list/target group generation.', 'LIST', g_list_header_id,'HIGH');
3643       update ams_list_headers_all
3644         set last_generation_success_flag = 'N',
3645             status_code                  = 'FAILED',
3646             user_status_id               = 311,
3647             status_date                  = sysdate,
3648             last_update_date             = sysdate,
3649             main_gen_end_time            = sysdate
3650       where list_header_id               = g_list_header_id;
3651   -- Added for cancel list gen as it prevents parallel update- Raghu
3652   -- of list headers when cancel button is pressed
3653       commit;
3654       x_return_status := FND_API.G_RET_STS_ERROR;
3655       return;
3656     end if;
3657     --
3658     l_counter  := l_counter +1 ;
3659   end loop;
3660   close cur_diwb;
3661   write_to_act_log('Passing sql from ams_discoverer sql for this workbook to process_all_sql procedure', 'LIST', g_list_header_id,'LOW');
3662   process_all_sql(p_action_used_by_id => p_action_used_by_id ,
3663                   p_incl_object_id => p_incl_object_id ,
3664                   p_list_action_type  => p_list_action_type  ,
3665                   p_list_select_action_id   => p_list_select_action_id   ,
3666                   p_order_number   => p_order_number   ,
3667                   p_rank   => p_rank   ,
3668                   p_include_control_group  => p_include_control_group,
3669                   p_sql_string    => l_sql_string,
3670 						p_primary_key   => null,
3671 						p_source_object_name => null,
3672                   x_msg_count      => x_msg_count      ,
3673                   x_msg_data   => x_msg_data   ,
3674                   x_return_status   => x_return_status   ,
3675                   x_std_sql                => x_std_sql,
3676                   x_include_sql            => x_include_sql);
3677   write_to_act_log('Execution of process_diwb ends', 'LIST', g_list_header_id,'LOW');
3678 
3679 END process_diwb ;
3680 
3681 PROCEDURE process_cell
3682              (p_action_used_by_id in  number,
3683               p_incl_object_id in number,
3684               p_list_action_type  in varchar2,
3685               p_list_select_action_id   in number,
3686               p_order_number   in number,
3687               p_rank   in number,
3688               p_include_control_group  in varchar2,
3689               x_msg_count      OUT NOCOPY number,
3690               x_msg_data       OUT NOCOPY varchar2,
3691               x_return_status  IN OUT NOCOPY VARCHAR2,
3692               x_std_sql OUT NOCOPY varchar2 ,
3693               x_include_sql OUT NOCOPY varchar2
3694                ) is
3695 
3696 ------------------------------------------------------------------------------
3697 -- Given the sql id from ams_list_select_actions it will retrieve the
3698 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
3699 -- workbook_name.
3700 ------------------------------------------------------------------------------
3701 l_sql_string         sql_string;
3702 l_where_string       sql_string;
3703 l_from_string       sql_string;
3704 l_counter            NUMBER := 1;
3705 l_from_position      number;
3706 l_from_counter       number;
3707 l_end_position      number;
3708 l_end_counter       number;
3709 l_order_position      number;
3710 l_order_counter       number;
3711 l_group_position      number;
3712 l_group_counter       number;
3713 l_found              varchar2(1);
3714 l_master_type        varchar2(80);
3715 l_master_type_id     number;
3716 l_source_object_name  varchar2(80);
3717 l_source_object_pk_field  varchar2(80);
3718 l_child_types        child_type;
3719 l_select_condition    varchar2(2000);
3720 l_select_add_condition    varchar2(2000);
3721 l_msg_data       VARCHAR2(2000);
3722 l_msg_count      number;
3723 l_sql_2          DBMS_SQL.VARCHAR2S;
3724 l_sql_string_final    varchar2(4000);
3725 j number     := 1;
3726 BEGIN
3727   write_to_act_log('Executing process_cell since segment has been included in list/target group selections.', 'LIST', g_list_header_id,'LOW');
3728   write_to_act_log('Calling ams_cell_pvt.get_comp_sql to get the sql', 'LIST', g_list_header_id,'LOW');
3729   ams_cell_pvt.get_comp_sql(
3730       p_api_version       => 1.0,
3731       p_init_msg_list     => FND_API.g_false,
3732       p_validation_level  => FND_API.g_valid_level_full,
3733       x_return_status     => x_return_status,
3734       x_msg_count         => x_msg_count ,
3735       x_msg_data           =>x_msg_data,
3736       p_cell_id           => p_incl_object_id ,
3737       p_party_id_only     => FND_API.g_true, --bug 4635925
3738       x_sql_tbl           => l_sql_2
3739    );
3740   write_to_act_log('Procedure ams_cell_pvt.get_comp_sql executed. Return status is '||x_return_status, 'LIST', g_list_header_id,'LOW');
3741   l_sql_string_final := '';
3742   for i in 1 .. l_sql_2.last
3743   loop
3744       l_sql_string_final := l_sql_string_final || l_sql_2(i);
3745      if length(l_sql_string_final) > 2000 then
3746         l_sql_string(j) := substrb(l_sql_string_final,1,2000);
3747         l_sql_string_final := substrb(l_sql_string_final,2001 ,2000);
3748         j := j+1;
3749      end if;
3750   end loop;
3751   l_sql_string(j) := substrb(l_sql_string_final,1,2000);
3752   if length(l_sql_string_final) > 2000 then
3753     j := j+1;
3754     l_sql_string(j) := substrb(l_sql_string_final,2001 ,2000);
3755   end if;
3756 
3757   write_to_act_log('Passing sql associated with this segment to process_all_sql procedure', 'LIST', g_list_header_id,'LOW');
3758   process_all_sql(p_action_used_by_id => p_action_used_by_id ,
3759                   p_incl_object_id => p_incl_object_id ,
3760                   p_list_action_type  => p_list_action_type  ,
3761                   p_list_select_action_id   => p_list_select_action_id   ,
3762                   p_order_number   => p_order_number   ,
3763                   p_rank   => p_rank   ,
3764                   p_include_control_group  => p_include_control_group,
3765                   p_sql_string    => l_sql_string    ,
3766                   x_msg_count      => x_msg_count      ,
3767                   x_msg_data   => x_msg_data   ,
3768                   x_return_status   => x_return_status   ,
3769                   x_std_sql                => x_std_sql,
3770                   x_include_sql            => x_include_sql,
3771                   p_primary_key   => null,
3772                   p_source_object_name => null);
3773   write_to_act_log('Execution of process_cell completed. Return status is '||x_return_status, 'LIST', g_list_header_id,'LOW');
3774 exception
3775     when others then
3776         write_to_act_log('Error while executing process cell procedure. Please check the segment definitions', 'LIST', g_list_header_id,'HIGH');
3777 
3778 END process_cell ;
3779 -----------------------------------------------------------------------------
3780 -- START OF COMMENTS
3781 -- NAME : PROCESS_LIST_ACTIONS
3782 -- PURPOSE
3783 -- CALLED BY.
3784 --    1. Generate_List.
3785 --
3786 -- HISTORY
3787 --   02/01/2001      gjoby  created
3788 -- END OF COMMENTS
3789 -----------------------------------------------------------------------------
3790 
3791 PROCEDURE process_list_actions
3792              (p_action_used_by_id  in  number,
3793               p_action_used_by     in  varchar2  ,-- DEFAULT 'LIST',
3794               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
3795               x_return_status      OUT NOCOPY VARCHAR2,
3796               x_msg_count          OUT NOCOPY NUMBER,
3797               x_msg_data           OUT NOCOPY VARCHAR2) IS
3798 
3799   -- AMS_LIST_SELECT_ACTIONS Record for init record and complete record
3800   l_tmp_action_rec             ams_listaction_pvt.action_rec_type;
3801   p_action_rec                 ams_listaction_pvt.action_rec_type;
3802   l_list_select_action_id      number;
3803   l_return_status             VARCHAR2(100) := FND_API.G_FALSE;
3804 
3805   ----------------------------------------------------------------------------
3806   -- Cursor definition to select list_select_action_id.Will be used in loop to
3807   -- Process each cursor record according to order specified by the user
3808   ----------------------------------------------------------------------------
3809 /*
3810   CURSOR c_action_dets is
3811   SELECT list_select_action_id
3812     FROM ams_list_select_actions
3813    WHERE action_used_by_id   = p_action_used_by_id
3814      AND arc_action_used_by  = p_action_used_by
3815    ORDER by rank; -- Raghu Jul 07
3816 --   ORDER by order_number;
3817 */
3818   CURSOR c_action_dets is
3819   SELECT list_select_action_id
3820     FROM ams_list_select_actions
3821    WHERE action_used_by_id   = p_action_used_by_id
3822      AND arc_action_used_by  = p_action_used_by
3823    ORDER by order_number;
3824 
3825   TYPE big_tbl_type is table of VARCHAR2(32767) index by BINARY_INTEGER;
3826   l_std_sql VARCHAR2(32767);
3827   l_include_sql VARCHAR2(32767);
3828   l_include_count number:=0;
3829   l_final_big_sql VARCHAR2(32767);
3830   l_include_sql_tbl  big_tbl_type ;
3831   l_std_sql_tbl  big_tbl_type ;
3832   l_join_string   varchar2(50);
3833   l_no_of_chunks  number;
3834   l_const_sql varchar2(4000) ;
3835   TYPE char_tbl_type is table of VARCHAR2(100) index by BINARY_INTEGER;
3836   TYPE num_tbl_type is table of number index by BINARY_INTEGER;
3837 --  l_rank_tbl      char_tbl_type;
3838   l_rank_num_tbl      num_tbl_type;
3839   l_order_num_tbl     num_tbl_type; -- added for bug fix 4443619
3840   l_order_num   number;
3841 l_sorted   number;
3842 l_update_sql  VARCHAR2(32767);
3843 l_list_header_id number ;
3844 l_string VARCHAR2(32767);
3845 
3846 cursor c1 is
3847 select generation_type
3848 from ams_list_headers_all
3849 where list_header_id = l_list_header_id;
3850 l_generation_type varchar2(60);
3851 l_PARAMETERIZED_FLAG  varchar2(1) := 'N';
3852 TYPE table_char  IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
3853 l_table_char table_char;
3854 
3855 cursor c_query(l_query_id number) is select
3856 nvl(PARAMETERIZED_FLAG ,'N')
3857 from ams_list_queries_all
3858 where  list_query_id = l_query_id ;
3859 
3860 cursor c_param_values(l_query_id in number) is
3861 select PARAMETER_ORDER, PARAMETER_VALUE,parameter_name
3862 from ams_list_queries_param
3863 where list_query_id = l_query_id
3864 order by PARAMETER_ORDER;
3865 
3866 l_remote_update_sql  VARCHAR2(32767);
3867 l_total_chunks  number;
3868 l_null		number;
3869 l_total_recs    number;
3870 l_query_templ_flag   varchar2(1) ;
3871 
3872 cursor c_count1  is select count(1)
3873 from ams_list_entries
3874 where list_header_id  = g_list_header_id ;
3875 l_count1 number:= 0;
3876 
3877 cursor c_query_temp_type is
3878 select 'Y'
3879 from ams_list_headers_vl  a ,
3880      ams_query_template_all b
3881 where a.list_header_id = g_list_header_id
3882   and b.template_type  = 'PARAMETERIZED'
3883   and a.query_template_id = b.template_id ;
3884 
3885 l_param_string VARCHAR2(32767);
3886 l_const_sql1 varchar2(4000) ;
3887 l_l_sele_action_id      number;
3888 
3889 l_temp_sql            varchar2(32767);
3890 
3891 l_dist_pct_tbl        num_tbl_type;
3892 l_list_select_id      num_tbl_type;
3893 l_incl_object_type    char_tbl_type;
3894 
3895 
3896 
3897 /*  CURSOR c_action_dets1 is
3898   SELECT list_select_action_id
3899     FROM ams_list_select_actions
3900    WHERE action_used_by_id   = p_action_used_by_id
3901      AND arc_action_used_by  = p_action_used_by
3902      AND order_number        = l_order_num; -- added for bug fix 4443619
3903 --     AND order_number        = l_sorted;-- removed for bug fix 4443619*/
3904   CURSOR c_action_dets1 is
3905   SELECT list_select_action_id,order_number
3906     FROM ams_list_select_actions
3907    WHERE action_used_by_id   = p_action_used_by_id
3908      AND arc_action_used_by  = p_action_used_by
3909      AND order_number        = l_order_num;
3910 
3911 l_order_number number := 0;
3912 
3913 --Bug 4685389. bmuthukr. to check the total # of parameters
3914 cursor c_check_num_params(p_incl_object_id number) is
3915 select count(1)
3916   from ams_list_queries_param
3917  where list_query_id = p_incl_object_id;
3918 
3919  l_tot_params   number := 0;
3920 --
3921 
3922 BEGIN
3923    write_to_act_log('Executing process_list_actions','LIST',g_list_header_id, 'HIGH');
3924 l_const_sql := ' minus '||
3925                ' select list_entry_source_system_id ' ||
3926                ' from ams_list_entries ' ||
3927                ' where list_header_id  = ' || p_action_used_by_id   ;
3928 
3929 l_const_sql1 := '   and LIST_SELECT_ACTION_ID = ';
3930 
3931   write_to_act_log('Fetching info for the selections','LIST',g_list_header_id, 'LOW');
3932   OPEN c_action_dets;
3933   LOOP
3934 
3935 /******************************************************************************/
3936 /**************** call for cancel list generation added 05/23/2005 ************/
3937 /******************************************************************************/
3938 -- Inside process_list_actions
3939 
3940    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
3941                 p_list_header_id => g_list_header_id ,
3942                 p_remote_gen     => g_remote_list    ,
3943                 p_remote_gen_list=> g_remote_list_gen,
3944                 p_database_link  => g_database_link,
3945                 x_msg_count      => x_msg_count ,
3946                 x_msg_data       => x_msg_data ,
3947                 x_return_status  => x_return_status
3948                );
3949 
3950   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
3951      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
3952         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
3953         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
3954      end if;
3955      RAISE FND_API.G_EXC_ERROR;
3956   ELSE
3957      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
3958   END IF;
3959 
3960 /******************************************************************************/
3961 /**************** call for cancel list generation added 05/23/2005 ************/
3962 /******************************************************************************/
3963 
3964     FETCH c_action_dets INTO l_list_select_action_id;
3965     EXIT WHEN c_action_dets%NOTFOUND;
3966 
3967     -------------------------------------------------------------------------
3968     -- Gets list select actions record details
3969     -- Intialize the record, set the list_select_action_id and get the
3970     -- details
3971     -------------------------------------------------------------------------
3972     ams_listaction_pvt.init_action_rec(l_tmp_action_rec);
3973     l_tmp_action_rec.list_select_action_id := l_list_select_action_id;
3974     ams_listaction_pvt.complete_action_rec
3975                        (p_action_rec   => l_tmp_action_rec,
3976                         x_complete_rec => p_action_rec);
3977     -------------------------------------------------------------------------
3978     write_to_act_log('Included object is of type '||p_action_rec.arc_incl_object_from||' , action type is '||p_action_rec.list_action_type||' , included object_id is '||to_char(p_action_rec.incl_object_id),'LIST',g_list_header_id,'LOW');
3979 
3980    ----------------------------------------------------------------------
3981     --validating that the first executed action has a type of "INCLUDE".--
3982     ----------------------------------------------------------------------
3983     IF (c_action_dets%ROWCOUNT = 1) THEN
3984        IF (p_action_rec.list_action_type <> 'INCLUDE')then
3985            write_to_act_log('Action type of the first selection should always be INCLUDE. Aborting list generation.', 'LIST', g_list_header_id,'HIGH');
3986            FND_MESSAGE.set_name('AMS', 'AMS_LIST_ACT_FIRST_INCLUDE');
3987            FND_MSG_PUB.Add;
3988            RAISE FND_API.G_EXC_ERROR;
3989        END IF;  -- End of if for list_action_type check
3990     END IF; --End of Validation:- First Action Include Check
3991 
3992     write_to_act_log('Calling validate_listaction procedure to validate the list selection attributes','LIST',g_list_header_id, 'LOW');
3993     ams_listaction_pvt.Validate_ListAction
3994     ( p_api_version            => 1.0,
3995       p_init_msg_list          => FND_API.G_FALSE,
3996       p_validation_level       => JTF_PLSQL_API.G_VALID_LEVEL_RECORD,
3997       x_return_status          => x_return_status,
3998       x_msg_count              => x_msg_count,
3999       x_msg_data               => x_msg_data,
4000       p_action_rec             => p_action_rec
4001     );
4002 
4003    IF x_return_status = FND_API.g_ret_sts_error THEN
4004       RAISE FND_API.g_exc_error;
4005    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4006       RAISE FND_API.g_exc_unexpected_error;
4007    END IF;
4008 
4009    if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4010       write_to_act_log('Error while validating list selections', 'LIST', g_list_header_id,'HIGH');
4011       write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4012    end if;
4013 
4014    if p_action_rec.ARC_INCL_OBJECT_FROM = 'SQL' then
4015       OPEN c_query( p_action_rec.incl_object_id );
4016       FETCH c_query INTO l_PARAMETERIZED_FLAG  ;
4017       close  c_query;
4018    end if;
4019    /********************************************************************
4020       This dynamic procedure will process action for each object type
4021       If the object type is of CELL the process will be procecss_cell
4022       Using the same logic the procedure could be extended for new
4023       action types
4024    *********************************************************************/
4025    -- Bug 4685389. bmuthukr. to check the total # of parameters. If it exceeds 100 abort the process
4026    if nvl(l_parameterized_flag,'N') = 'Y' then
4027 
4028       open c_check_num_params(p_action_rec.incl_object_id);
4029       fetch c_check_num_params into l_tot_params;
4030       close c_check_num_params;
4031 
4032       if nvl(l_tot_params,0) > 100 then
4033          write_to_act_log('Number of parameters used exceeds 100. Aborting list generation process. Please redefine your criteria and restrict it to 100.',
4034   	 'LIST',g_list_header_id,'HIGH');
4035          UPDATE ams_list_headers_all
4036             SET last_generation_success_flag = 'N',
4037                 status_code                  = 'FAILED',
4038                 user_status_id               = 311,
4039                 status_date                  = sysdate,
4040                 last_update_date             = sysdate,
4041                 main_gen_end_time            = sysdate
4042           WHERE list_header_id               = g_list_header_id;
4043          x_return_status := 'E';
4044          logger;
4045 	 commit;
4046          RETURN;
4047       end if;
4048    end if;
4049    --
4050 
4051    -- Employeee list changes..
4052    if p_action_rec.arc_incl_object_from NOT IN ('IMPH','LIST','SQL','DIWB','CELL') then
4053       write_to_act_log(p_msg_data => 'Invalid included object -- Valid inclusions are imported list, list, custom sql, segment, work book. Aborting list generation process.',
4054                        p_arc_log_used_by => 'LIST',
4055                        p_log_used_by_id  => g_list_header_id,
4056 		       p_level => 'HIGH');
4057 
4058         UPDATE ams_list_headers_all
4059         SET    last_generation_success_flag = 'N',
4060                status_code                  = 'FAILED',
4061                user_status_id               = 311,
4062                status_date                  = sysdate,
4063                last_update_date             = sysdate,
4064                main_gen_end_time            = sysdate
4065         WHERE  list_header_id               = g_list_header_id;
4066   -- Added for cancel list gen as it prevents parallel update- Raghu
4067   -- of list headers when cancel button is pressed
4068         commit;
4069 
4070      x_return_status := 'E';
4071      x_msg_count := 1;
4072      x_msg_data := ' Invalid Included Object-- Valid inclusions are imported list, list, custom sql, segment, work book. Aborting list generation process.';
4073      RETURN;
4074    end if;
4075 
4076       write_to_act_log('Calling process_'||p_action_rec.arc_incl_object_from,'LIST',g_list_header_id,'LOW');
4077    write_to_act_log('p_action_rec.list_select_action_id = '||p_action_rec.list_select_action_id, 'LIST', g_list_header_id,'LOW');
4078       execute immediate
4079       'BEGIN
4080         AMS_ListGeneration_PKG.process_'||p_action_rec.arc_incl_object_from ||
4081          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
4082       END;'
4083       using  p_action_used_by_id,
4084              p_action_rec.incl_object_id ,
4085              p_action_rec.list_action_type,
4086              p_action_rec.list_select_action_id,
4087              p_action_rec.order_number,
4088              p_action_rec.rank,
4089              'N',--CHECK p_action_rec.incl_control_group,
4090              OUT x_msg_data,
4091              OUT x_msg_count,
4092              in OUT x_return_status ,
4093              OUT l_std_sql ,
4094              OUT l_include_sql;
4095 
4096     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4097        write_to_act_log('Error when executing process_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'HIGH');
4098        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4099        UPDATE ams_list_headers_all
4100           SET last_generation_success_flag = 'N',
4101               status_code                  = 'FAILED',
4102               user_status_id               = 311,
4103               status_date                  = sysdate,
4104               last_update_date             = sysdate,
4105               main_gen_end_time            = sysdate
4106         WHERE list_header_id               = g_list_header_id;
4107        logger;
4108        commit;
4109        RETURN;
4110     else
4111        write_to_act_log('Process_'||p_action_rec.arc_incl_object_from||' executed successfully.', 'LIST', g_list_header_id,'LOW');
4112     end if;
4113 
4114      if p_action_rec.list_action_type = 'INCLUDE' then
4115         l_include_count := l_include_count + 1 ;
4116         l_include_sql_tbl(l_include_count) := l_include_sql ;
4117         l_std_sql_tbl(l_include_count) := l_std_sql;
4118         --l_rank_tbl(l_include_count) := lpad(p_action_rec.rank,50,'0')
4119                          --|| lpad(p_action_rec.order_number,50,'0');
4120         l_order_num_tbl(l_include_count) := p_action_rec.order_number;-- Raghu Jul 07
4121 	l_rank_num_tbl(l_include_count) := p_action_rec.rank; -- Raghu Jul 07
4122 	l_dist_pct_tbl(l_include_count) := p_action_rec.distribution_pct;
4123         l_list_select_id(l_include_count) := p_action_rec.list_select_action_id;
4124         l_incl_object_type(l_include_count) := p_action_rec.arc_incl_object_from;
4125      else
4126        if p_action_rec.list_action_type = 'EXCLUDE' then
4127           l_join_string := ' minus ';
4128           l_list_header_id := p_action_rec.action_used_by_id;
4129           open c1;
4130           FETCH c1 into l_generation_type;
4131           close c1;
4132        else
4133           l_join_string := ' intersect ';
4134           l_list_header_id := p_action_rec.action_used_by_id;
4135           open c1;
4136           FETCH c1 into l_generation_type;
4137           close c1;
4138        end if;
4139        write_to_act_log('SQL statement for INCLUSION','LIST',g_list_header_id,'LOW');
4140      FOR i IN 1 .. l_include_count
4141      loop
4142         l_std_sql_tbl(i) := l_std_sql_tbl(i)  ||
4143                              l_join_string ||
4144                              l_std_sql;
4145         l_no_of_chunks := ceil(length(l_std_sql_tbl(i))/2000 );
4146         for j in 1..l_no_of_chunks
4147         loop
4148            WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4149         end loop;
4150      end loop;
4151 
4152      end if;
4153 
4154      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
4155        RAISE FND_API.G_EXC_ERROR;
4156      END IF;
4157    write_to_act_log('Action on the list/target group selection completed. ', 'LIST', g_list_header_id,'LOW');
4158 -- end Of Dynamic Procedure
4159    l_join_string :='';
4160 ---------------------------------------------------------------------------
4161   END LOOP;  --  End loop c_action_dets
4162   CLOSE c_action_dets;
4163 
4164    write_to_act_log('No of inclusions in the selection '|| l_include_count, 'LIST', g_list_header_id,'LOW');
4165    write_to_act_log('Sorting based on rank for the selection. '|| l_include_count, 'LIST', g_list_header_id,'LOW');
4166 /*
4167        -- Sorting According to rank
4168        FOR i IN 1 .. l_include_count
4169        loop
4170           l_rank_num_tbl(i) := i;
4171           if i <> 1 then
4172              for j in 1 .. i-1
4173              loop
4174                if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
4175                   for k in reverse j .. i-1
4176                   loop
4177                      l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
4178                   end loop;
4179                   l_rank_num_tbl(j) := i;
4180                   exit;
4181                end if;
4182              end loop;
4183 	  end if;
4184        end loop;
4185 */
4186 
4187 
4188   for  i in 1 .. l_include_count
4189   loop
4190 	l_l_sele_action_id := null;
4191         --l_sorted := l_rank_num_tbl(i);
4192         l_sorted := i;--l_rank_num_tbl(i);
4193         l_order_num := l_order_num_tbl(i);
4194         --open c_action_dets1;
4195         --fetch c_action_dets1 into l_l_sele_action_id,l_order_number;
4196         --close c_action_dets1;
4197         WRITE_TO_ACT_LOG('List selection id is '||l_l_sele_action_id||' for order number '||l_order_num, 'LIST', g_list_header_id,'LOW');
4198 /*
4199 l_final_big_sql := l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')';
4200 */
4201        -- l_std_sql_tbl(l_sorted) || l_const_sql || ')';
4202      WRITE_TO_ACT_LOG('Final SQL formed for generating list.', 'LIST', g_list_header_id,'LOW');
4203      if l_final_big_sql is not null then
4204       l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
4205       for i in 1 ..l_no_of_chunks
4206       loop
4207         WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4208       end loop;
4209      end if;
4210 
4211      if l_include_sql_tbl(l_sorted) is not null then
4212       l_no_of_chunks := 0;
4213       l_no_of_chunks  := ceil(length(l_include_sql_tbl(l_sorted))/2000 );
4214       for i in 1 ..l_no_of_chunks
4215        loop
4216         WRITE_TO_ACT_LOG(substrb(l_include_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4217        end loop;
4218      end if;
4219     if l_std_sql_tbl(l_sorted) is not null then
4220       l_no_of_chunks := 0;
4221       l_no_of_chunks  := ceil(length(l_std_sql_tbl(l_sorted))/2000 );
4222       for i in 1 ..l_no_of_chunks
4223        loop
4224         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4225        end loop;
4226     end if;
4227     if l_const_sql is not null then
4228      l_no_of_chunks := 0;
4229      l_no_of_chunks  := ceil(length(l_const_sql)/2000 );
4230      for i in 1 ..l_no_of_chunks
4231      loop
4232         WRITE_TO_ACT_LOG(substrb(l_const_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4233      end loop;
4234     end if;
4235 
4236       if l_PARAMETERIZED_FLAG  = 'N' then
4237          write_to_act_log('No parameters required for generating this list', 'LIST', g_list_header_id,'LOW');
4238          if nvl(l_dist_pct_tbl(l_sorted),100) <> 100 then
4239 	    write_to_act_log('Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
4240 	    write_to_act_log('% Requested for this selection is '||l_dist_pct_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
4241    	    if l_incl_object_type(l_sorted) in ('SQL','DIWB','CELL') then
4242 	       write_to_act_log('Inclusion No is '||l_sorted||'  '||'Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
4243 	       l_temp_sql := l_include_sql_tbl(l_sorted);
4244 	       l_temp_sql := 'SELECT count(1) '||substr(l_temp_sql,instr(l_temp_sql, ' FROM '));
4245                -- Modified for bug 5238900. bmuthukr
4246 	       -- get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||l_const_sql1||l_l_sele_action_id||')' );
4247                get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||')' );
4248             elsif l_incl_object_type(l_sorted) = 'LIST' then
4249                get_count(l_list_select_id(l_sorted),l_sorted,'LIST',null);
4250             elsif l_incl_object_type(l_sorted) = 'IMPH' then
4251                get_count(l_list_select_id(l_sorted),l_sorted,'IMPH',null);
4252             end if;
4253             write_to_act_log('No of rows requested from the selection is '||g_reqd_num_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
4254          else
4255             g_act_num_tbl(l_sorted)  := -1;
4256             g_reqd_num_tbl(l_sorted) := -1;
4257          end if;
4258          if g_remote_list_gen = 'N' then
4259 		/* If the list is not based on the remote data source and if it's based on remote data source
4260                    but needs to be generated in the local instance means it's migrated to the local instance */
4261  	   l_const_sql1 := ' ';
4262            l_l_sele_action_id := null;
4263 
4264 	   if g_reqd_num_tbl(l_sorted) <> -1 then
4265               EXECUTE IMMEDIATE l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||' and rownum <= '||g_reqd_num_tbl(l_sorted);
4266            else
4267               EXECUTE IMMEDIATE l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')';
4268            end if;
4269            write_to_act_log('List generated in local instance', 'LIST', g_list_header_id,'HIGH');
4270 	  else
4271 		/* If the list is based on the remote data source and it's not migrated to the local instance or
4272                    a segment , sql or workbook is in the list selection then it will be generated in the remote
4273                    instance through a dynamic procedure call */
4274                write_to_act_log('Calling remote procedure to generate list in remote instance', 'LIST', g_list_header_id,'LOW');
4275  	   l_const_sql1 := ' ';
4276            l_l_sele_action_id := null;
4277 
4278 	   if g_reqd_num_tbl(l_sorted) <> -1 then
4279 	      write_to_act_log(' % Requested at selection level for this selection ','LIST',g_list_header_id,'LOW');
4280 	       execute immediate
4281       	      'BEGIN
4282                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
4283               ' END;'
4284               using  '1',
4285               l_null,
4286               'T',
4287               l_null,
4288               OUT x_return_status,
4289               OUT x_msg_count,
4290               OUT x_msg_data,
4291               g_list_header_id,
4292               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||' and rownum <= '||g_reqd_num_tbl(l_sorted),
4293 	      --   l_final_big_sql,
4294               l_null,
4295              OUT l_total_recs,
4296              'LISTGEN';
4297           else
4298 	     write_to_act_log(' % Not Requested at selection level for this selection ','LIST',g_list_header_id,'LOW');
4299              execute immediate
4300       	      'BEGIN
4301                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
4302               ' END;'
4303               using  '1',
4304               l_null,
4305               'T',
4306               l_null,
4307               OUT x_return_status,
4308               OUT x_msg_count,
4309               OUT x_msg_data,
4310               g_list_header_id,
4311               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')',
4312 	   --   l_final_big_sql,
4313               l_null,
4314              OUT l_total_recs,
4315              'LISTGEN';
4316           end if;
4317            if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4318               write_to_act_log('Error while executing remote procedure for generating list', 'LIST', g_list_header_id,'HIGH');
4319               write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4320               --Added for bug 4577528 by bmuthukr.
4321               update ams_list_headers_all
4322                  set last_generation_success_flag = 'N',
4323                      status_code                  = 'FAILED',
4324                      user_status_id               = 311,
4325                      status_date                  = sysdate,
4326                      last_update_date             = sysdate,
4327                      main_gen_end_time            = sysdate
4328                where list_header_id               = g_list_header_id;
4329  	      update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
4330               write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
4331 	      x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
4332 	      return;
4333 	      --
4334            else
4335               write_to_act_log('List generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
4336            end if;
4337 	end if;
4338       else  -- FOR l_PARAMETERIZED_FLAG = 'Y'
4339 --       if g_remote_list_gen = 'N' then
4340         write_to_act_log('Fetching parameters required for generating this list', 'LIST', g_list_header_id,'LOW');
4341         for i in 1 .. 100 loop
4342            l_table_char(i) := ' ';
4343         end loop;
4344         for i in c_param_values(p_action_rec.incl_object_id )
4345         loop
4346            l_table_char(i.PARAMETER_ORDER) := trim(i.PARAMETER_VALUE);
4347 	   write_to_act_log('Parameter - '||i.parameter_order||' is '||'*'||l_table_char(i.PARAMETER_ORDER)||'*','LIST', g_list_header_id,'LOW');
4348         end loop;
4349         l_const_sql1 := ' ';
4350         l_l_sele_action_id := null;
4351 	-- l_final_big_sql := ' insert into t11 values (99,:PARTY_TYPE)';
4352         l_string := 'DECLARE   ' ||
4353         'l_string1 varchar2(10000) ; ' ||
4354         'begin    ' ||
4355         ' l_string1 :=   :1  || ' || ' :2  || ' || ' :3  || ' || ' :4  || ' ||
4356                        ' :5  || ' || ' :6  || ' || ' :7  || ' || ' :8  || ' ||
4357                        ' :9  || ' || ' :10  || ' || ' :11  || ' || ' :12  || ' ||
4358                        ' :13  || ' || ' :14  || ' || ' :15  || ' || ' :16  || ' ||
4359                        ' :17  || ' || ' :18  || ' || ' :19  || ' || ' :20  || ' ||
4360                        ' :21  || ' || ' :22  || ' || ' :23  || ' || ' :24  || ' ||
4361                        ' :25  || ' || ' :26  || ' || ' :27  || ' || ' :28  || ' ||
4362                        ' :29  || ' || ' :30  || ' || ' :31  || ' || ' :32  || ' ||
4363                        ' :33  || ' || ' :34  || ' || ' :35  || ' || ' :36  || ' ||
4364                        ' :37  || ' || ' :38  || ' || ' :39  || ' || ' :40  || ' ||
4365                        ' :41  || ' || ' :42  || ' || ' :43  || ' || ' :44  || ' ||
4366                        ' :45  || ' || ' :46  || ' || ' :47  || ' || ' :48  || ' ||
4367                        ' :49  || ' || ' :50  || ' || ' :51  || ' || ' :52  || ' ||
4368                        ' :53  || ' || ' :54  || ' || ' :55  || ' || ' :56  || ' ||
4369                        ' :57  || ' || ' :58  || ' || ' :59  || ' || ' :60  || ' ||
4370                        ' :61  || ' || ' :62  || ' || ' :63  || ' || ' :64  || ' ||
4371                        ' :65  || ' || ' :66  || ' || ' :67  || ' || ' :68  || ' ||
4372                        ' :69  || ' || ' :70  || ' || ' :71  || ' || ' :72  || ' ||
4373                        ' :73  || ' || ' :74  || ' || ' :75  || ' || ' :76  || ' ||
4374                        ' :77  || ' || ' :78  || ' || ' :79  || ' || ' :80  || ' ||
4375                        ' :81  || ' || ' :82  || ' || ' :83  || ' || ' :84  || ' ||
4376                        ' :85  || ' || ' :86  || ' || ' :87  || ' || ' :88  || ' ||
4377                        ' :89  || ' || ' :90  || ' || ' :91  || ' || ' :92  || ' ||
4378                        ' :93  || ' || ' :94  || ' || ' :95  || ' || ' :96  || ' ||
4379                        ' :97  || ' || ' :98  || ' || ' :99  || ' || ' :100  ; ' ||' '||
4380  --    l_final_big_sql ||
4381       l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||
4382        ';  end;  '  ;
4383 /* Changed to fix :1 and :name issue */
4384      open c_query_temp_type ;
4385         fetch  c_query_temp_type into l_query_templ_flag   ;
4386            --EXIT WHEN c_query_temp_type%NOTFOUND;
4387            if l_query_templ_flag  = 'Y' then
4388               l_no_of_chunks := 0;
4389               for i in c_param_values(p_action_rec.incl_object_id )
4390               loop
4391                  l_no_of_chunks := l_no_of_chunks + 1;
4392                  l_string := replace(l_string,':' || l_no_of_chunks  || ' ' , ':'|| i.parameter_name||' ' );
4393               end loop;
4394            end if;
4395            l_no_of_chunks := 0;
4396      close c_query_temp_type ;
4397  /*END Changed to fix :1 and :name issue */
4398      WRITE_TO_ACT_LOG('SQL to be executed to generate list', 'LIST', g_list_header_id,'LOW');
4399      WRITE_TO_ACT_LOG('Length of the sql  '||length(l_string), 'LIST', g_list_header_id,'LOW');
4400      l_no_of_chunks  := ceil(length(l_string)/2000 );
4401      for i in 1 ..l_no_of_chunks
4402      loop
4403         WRITE_TO_ACT_LOG(substrb(l_string,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4404      end loop;
4405 if g_remote_list_gen = 'N' then
4406 
4407      WRITE_TO_ACT_LOG('Generating list with parameters ','LIST', g_list_header_id,'LOW');
4408 execute immediate   l_string
4409 using l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
4410       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
4411       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
4412       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
4413       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
4414       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
4415       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
4416       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
4417       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
4418       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
4419       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
4420       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
4421       l_table_char(49), l_table_char(50),
4422       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
4423       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
4424       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
4425       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
4426       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
4427       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
4428       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
4429       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
4430       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
4431       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
4432       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
4433       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
4434       l_table_char(99), l_table_char(100);
4435       open c_count1;
4436       fetch c_count1 into l_count1;
4437       close c_count1;
4438       write_to_act_log('Number of entries in list is '|| l_count1, 'LIST', g_list_header_id,'LOW');
4439  end if;
4440 
4441  if g_remote_list_gen = 'Y' then
4442   	   l_const_sql1 := ' ';
4443            l_l_sele_action_id := null;
4444 
4445         write_to_act_log('Calling remote procedure with parameters to generate list in remote instance ', 'LIST', g_list_header_id,'LOW');
4446       	       execute immediate
4447       	      'BEGIN
4448 	AMS_Remote_ListGen_PKG.remote_param_list_gen'||'@'||g_database_link||'( :1  , :2  , :3  , :4  , :5  , :6  , :7  , :8  , :9  , :10  , :11  , :12  ,
4449        :13   , :14   , :15  , :16   , :17   , :18   , :19   , :20   ,
4450        :21   , :22   , :23  , :24   , :25   , :26   , :27   , :28   , :29   , :30   ,
4451        :31   , :32   , :33  , :34   , :35   , :36   , :37   , :38   , :39   , :40   ,
4452        :41   , :42   , :43  , :44   , :45   , :46   , :47   , :48   , :49   , :50   ,
4453        :51   , :52   , :53  , :54   , :55   , :56   , :57   , :58   , :59   , :60   ,
4454        :61   , :62   , :63  , :64   , :65   , :66   , :67   , :68   , :69   , :70   ,
4455        :71   , :72   , :73  , :74   , :75   , :76   , :77   , :78   , :79   , :80   ,
4456        :81   , :82   , :83  , :84   , :85   , :86   , :87   , :88   , :89   , :90   ,
4457        :91   , :92   , :93  , :94   , :95   , :96   , :97   , :98   , :99   , :100  ,
4458        :101   , :102   , :103   , :104   , :105   , :106   , :107   , :108   , :109   , :110   , :111   , :112
4459            )'||';'||
4460               ' END;'
4461               using  '1',
4462               l_null,
4463               'T',
4464               l_null,
4465               OUT x_return_status,
4466               OUT x_msg_count,
4467               OUT x_msg_data,
4468               g_list_header_id,
4469 	      l_string,
4470               l_null,
4471              OUT l_total_recs,
4472              'PARAMLISTGEN',
4473       l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
4474       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
4475       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
4476       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
4477       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
4478       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
4479       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
4480       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
4481       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
4482       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
4483       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
4484       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
4485       l_table_char(49), l_table_char(50),
4486       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
4487       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
4488       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
4489       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
4490       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
4491       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
4492       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
4493       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
4494       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
4495       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
4496       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
4497       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
4498       l_table_char(99), l_table_char(100);
4499       if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4500          write_to_act_log('Error while generating  list in remote instance.', 'LIST', g_list_header_id,'HIGH');
4501          write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4502          --Added for bug 4577528 by bmuthukr.
4503          update ams_list_headers_all
4504             set last_generation_success_flag = 'N',
4505                 status_code                  = 'FAILED',
4506                 user_status_id               = 311,
4507                 status_date                  = sysdate,
4508                 last_update_date             = sysdate,
4509                 main_gen_end_time            = sysdate
4510           where list_header_id               = g_list_header_id;
4511          update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
4512          write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
4513 	 x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
4514 	 return;
4515 	 --
4516       else
4517          write_to_act_log('List generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
4518       end if;
4519     end if;
4520    end if;
4521   end loop;
4522   WRITE_TO_ACT_LOG('Execution of Process_list_action completed', 'LIST', g_list_header_id,'LOW');
4523 EXCEPTION
4524    WHEN FND_API.G_EXC_ERROR THEN
4525      IF(c_action_dets%ISOPEN)THEN
4526         CLOSE c_action_dets;
4527      END IF;
4528      write_to_act_log('Error while executing process_list_actions '
4529                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4530      -- Check if reset of the status is required
4531      x_return_status := FND_API.G_RET_STS_ERROR ;
4532       FND_MSG_PUB.count_and_get(
4533             p_encoded => FND_API.g_false,
4534             p_count   => x_msg_count,
4535             p_data    => x_msg_data);
4536 
4537   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4538      IF(c_action_dets%ISOPEN)THEN
4539         CLOSE c_action_dets;
4540      END IF;
4541      write_to_act_log('Error while executing process_list_actions '
4542                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4543      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
4544      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
4545      FND_MSG_PUB.Add;
4546      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4547       FND_MSG_PUB.count_and_get(
4548             p_encoded => FND_API.g_false,
4549             p_count   => x_msg_count,
4550             p_data    => x_msg_data);
4551 
4552   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
4553      IF(c_action_dets%ISOPEN)THEN
4554         CLOSE c_action_dets;
4555      END IF;
4556 
4557      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4558 
4559      write_to_act_log('executing process_list_actions - user action to cancel list generation detected ' ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4560      -- Got to raise the exception again because Listgen has to end generation.
4561      raise AMS_LISTGENERATION_UTIL_PKG.cancelListGen;
4562 
4563   WHEN OTHERS THEN
4564      IF(c_action_dets%ISOPEN)THEN
4565         CLOSE c_action_dets;
4566      END IF;
4567      write_to_act_log('Error while executing process_list_actions '
4568                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4569      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
4570      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
4571      FND_MSG_PUB.Add;
4572      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4573       FND_MSG_PUB.count_and_get(
4574             p_encoded => FND_API.g_false,
4575             p_count   => x_msg_count,
4576             p_data    => x_msg_data);
4577 
4578 END process_list_actions;
4579 
4580 -- START OF COMMENTS
4581 --
4582 -- NAME
4583 --   CREATE_LIST_ENTRIES
4584 --
4585 -- PURPOSE
4586 
4587 --    1. Populate The AMS_LIST_ENTRIES table with a DISTINCT set of
4588 --       Entries from The AMS_LIST_TMP_ENTRIES table.
4589 --    2. An Entry is considered UNIQUE by values in The list_entry_source_id
4590 --       and List_Entry_Source_Type fields from The AMS_LIST_TMP_ENTRIES table.
4591 --    3. There may be more than one Entry in The AMS_LIST_TMP_ENTRIES table
4592 --       with The same values for The list_entry_source_id and
4593 --       List_Entry_Source_Type fields, in this case The Entry with
4594 --       the Highest Rank is choosen, if Two Equivalent entries have
4595 --       the same rank then one entry is choosen arbitrarily.
4596 --    4. The set of distinct entries in The AMS_LIST_TMP_ENTRIES table
4597 --       have the SAVE_ROW_FLAG updated to 'Y'.
4598 --       Rows with this value are then inserted into the AMS_LIST_ENTRIES table.
4599 --    5. if less entries exist in the AMS_LIST_ENTRIES_TABLE than specifed
4600 --       in the AMS_LIST_HEADERS_ALL
4601 --       .NO_OF_ROWS_MIN_REQUESTED column then an error is reported.
4602 
4603 
4604 -- CALLED BY.
4605 --    1. GENERATE_LIST.
4606 --  01/24/2001 GJOBY      Modified for hornet
4607 --
4608 -- HISTORY
4609 -- END of Comments
4610 
4611 
4612 procedure create_list_entries (p_list_header_id in number,
4613                                p_no_of_rows_min_requested  in number,
4614                                x_return_status OUT NOCOPY  VARCHAR2 ) IS
4615 
4616   l_entry_count              NUMBER;
4617   l_return_status            VARCHAR2(100) := FND_API.G_FALSE;
4618   l_source_code              VARCHAR2(100) := 'NONE';
4619   l_source_id                NUMBER        := 0;
4620 
4621   l_created_by                NUMBER;
4622 
4623    CURSOR cur_get_created_by (x_list_header_id IN NUMBER) IS
4624       SELECT created_by
4625       FROM ams_list_headers_all
4626       WHERE list_header_id= x_list_header_id;
4627 
4628 BEGIN
4629    write_to_act_log('Executing procedure create_list_entries', 'LIST', g_list_header_id,'LOW');
4630    delete from ams_list_tmp_entries alte1
4631    where alte1.rowid > (select min(alte2.rowid)
4632                         from  ams_list_tmp_entries alte2
4633                         where alte2.list_header_id = alte1.list_header_id
4634                           and alte2.list_select_action_id
4635                                     = alte1.list_select_action_id
4636                           and alte2.list_entry_source_key
4637                                     =  alte1.list_entry_source_key  );
4638 
4639    delete from ams_list_tmp_entries alte1
4640    where alte1.rank > (select min(alte2.rank)
4641                         from  ams_list_tmp_entries alte2
4642                         where alte2.list_header_id = alte1.list_header_id
4643                           and alte2.list_entry_source_key
4644                                     =  alte1.list_entry_source_key  );
4645 
4646       l_created_by := 0;
4647 
4648        OPEN cur_get_created_by(p_list_header_id);
4649 
4650        FETCH cur_get_created_by INTO l_created_by;
4651        CLOSE cur_get_created_by;
4652 
4653 
4654    INSERT INTO ams_List_Entries
4655    ( list_entry_id                   ,
4656      last_update_date                ,
4657      last_updated_by                 ,
4658      creation_date                   ,
4659      created_by                      ,
4660      last_update_login               ,
4661      list_header_id                  ,
4662      list_select_action_id           ,
4663      arc_list_select_action_from     ,
4664      list_select_action_from_name    ,
4665      source_code                     ,
4666      source_code_for_id              ,
4667      arc_list_used_by_source         ,
4668      pin_code                        ,
4669      list_entry_source_system_id     ,
4670      list_entry_source_system_type   ,
4671      view_application_id             ,
4672      manually_entered_flag           ,
4673      marked_as_random_flag           ,
4674      marked_as_duplicate_flag        ,
4675      part_of_control_group_flag      ,
4676      exclude_in_triggered_list_flag  ,
4677      enabled_flag,
4678      object_version_number
4679    )
4680    ( select ams_list_entries_s.nextval,
4681             sysdate,
4682             fnd_global.user_id,
4683             sysdate,
4684             nvl(l_created_by, fnd_global.user_id),
4685             fnd_global.conc_login_id,
4686             p_list_header_id,
4687             s.list_select_action_id,
4688             s.arc_incl_object_from,
4689             s.incl_object_name,
4690             'NONE',
4691             0,
4692             h.list_used_by_id,
4693             ams_list_entries_s.currval,
4694             t.list_entry_source_id,
4695             t.list_entry_source_type,
4696             fnd_global.conc_login_id,
4697             'N',
4698             'N',
4699             'N',
4700             'N',
4701             'N',
4702             'Y',
4703              1
4704      from   ams_list_select_actions s,
4705             ams_list_tmp_entries    t,
4706             ams_list_headers_all    h
4707      where  h.list_header_id        = p_list_header_id
4708      and    t.list_header_id        = h.list_header_id
4709      and    t.list_select_action_id = s.list_select_action_id
4710      );
4711 
4712      select count(*)
4713      into   l_entry_count
4714      from   ams_list_entries
4715      where  list_header_id = p_list_header_id;
4716 
4717      update ams_list_headers_all
4718      set no_of_rows_in_list = l_entry_count,
4719          last_update_date  = sysdate
4720      where  list_header_id = p_list_header_id;
4721 
4722    write_to_act_log('Procedure create_list_entries executed', 'LIST', g_list_header_id,'LOW');
4723   -- Added for cancel list gen as it prevents parallel update- Raghu
4724   -- of list headers when cancel button is pressed
4725   commit;
4726 
4727 -- Start changes for migration
4728 /*
4729     if(l_entry_count < p_no_of_rows_min_requested)then
4730           FND_MESSAGE.set_name('AMS', 'AMS_LIST_MINIMUM_NOT_REACHED');
4731           FND_MESSAGE.Set_Token('NO_AVAILABLE',TO_CHAR(l_entry_count));
4732           FND_MESSAGE.Set_Token('NO_REQUESTED',TO_CHAR(p_no_of_rows_min_requested));
4733           FND_MSG_PUB.Add;
4734           x_return_status := FND_API.G_RET_STS_ERROR;
4735      ELSE
4736           x_return_status := FND_API.G_RET_STS_SUCCESS;
4737      END if;
4738 */
4739      x_return_status := FND_API.G_RET_STS_SUCCESS;
4740 -- End changes for migration
4741   exception
4742    when others then
4743    write_to_act_log('Error while executing procedure create_list_entries', 'LIST', g_list_header_id,'HIGH');
4744    x_return_status := FND_API.G_RET_STS_ERROR;
4745 
4746 end create_list_entries;
4747 
4748 
4749 procedure update_list_entries(p_list_header_id in number) is
4750 l_tg_check	varchar2(1) := null;
4751 cursor c_check_tg is
4752 select 'Y' from ams_act_lists where list_header_id = p_list_header_id;
4753 
4754 begin
4755 open  c_check_tg;
4756 fetch c_check_tg into l_tg_check;
4757 close c_check_tg;
4758 
4759 if l_tg_check = 'Y' then
4760    write_to_act_log('Generating target group(which has selections based on LIST) in update mode.','LIST',g_list_header_id,'LOW');
4761    update ams_list_entries  ale
4762    set (
4763        ale.newly_updated_flag ,
4764        ale.enabled_flag ,
4765        ale.SUFFIX,
4766        ale.FIRST_NAME,
4767        ale.LAST_NAME,
4768        ale.CUSTOMER_NAME,
4769        ale.TITLE,
4770        ale.ADDRESS_LINE1,
4771        ale.ADDRESS_LINE2,
4772        ale.CITY,
4773        ale.STATE,
4774        ale.ZIPCODE,
4775        ale.COUNTRY,
4776        ale.FAX,
4777        ale.PHONE,
4778        ale.EMAIL_ADDRESS,
4779        ale.COL1,
4780        ale.COL2,
4781        ale.COL3,
4782        ale.COL4,
4783        ale.COL5,
4784        ale.COL6,
4785        ale.COL7,
4786        ale.COL8,
4787        ale.COL9,
4788        ale.COL10,
4789        ale.COL11,
4790        ale.COL12,
4791        ale.COL13,
4792        ale.COL14,
4793        ale.COL15,
4794        ale.COL16,
4795        ale.COL17,
4796        ale.COL18,
4797        ale.COL19,
4798        ale.COL20,
4799        ale.COL21,
4800        ale.COL22,
4801        ale.COL23,
4802        ale.COL24,
4803        ale.COL25,
4804        ale.COL26,
4805        ale.COL27,
4806        ale.COL28,
4807        ale.COL29,
4808        ale.COL30,
4809        ale.COL31,
4810        ale.COL32,
4811        ale.COL33,
4812        ale.COL34,
4813        ale.COL35,
4814        ale.COL36,
4815        ale.COL37,
4816        ale.COL38,
4817        ale.COL39,
4818        ale.COL40,
4819        ale.COL41,
4820        ale.COL42,
4821        ale.COL43,
4822        ale.COL44,
4823        ale.COL45,
4824        ale.COL46,
4825        ale.COL47,
4826        ale.COL48,
4827        ale.COL49,
4828        ale.COL50,
4829        ale.COL51,
4830        ale.COL52,
4831        ale.COL53,
4832        ale.COL54,
4833        ale.COL55,
4834        ale.COL56,
4835        ale.COL57,
4836        ale.COL58,
4837        ale.COL59,
4838        ale.COL60,
4839        ale.COL61,
4840        ale.COL62,
4841        ale.COL63,
4842        ale.COL64,
4843        ale.COL65,
4844        ale.COL66,
4845        ale.COL67,
4846        ale.COL68,
4847        ale.COL69,
4848        ale.COL70,
4849        ale.COL71,
4850        ale.COL72,
4851        ale.COL73,
4852        ale.COL74,
4853        ale.COL75,
4854        ale.COL76,
4855        ale.COL77,
4856        ale.COL78,
4857        ale.COL79,
4858        ale.COL80,
4859        ale.COL81,
4860        ale.COL82,
4861        ale.COL83,
4862        ale.COL84,
4863        ale.COL85,
4864        ale.COL86,
4865        ale.COL87,
4866        ale.COL88,
4867        ale.COL89,
4868        ale.COL90,
4869        ale.COL91,
4870        ale.COL92,
4871        ale.COL93,
4872        ale.COL94,
4873        ale.COL95,
4874        ale.COL96,
4875        ale.COL97,
4876        ale.COL98,
4877        ale.COL99,
4878        ale.COL100,
4879        ale.COL101,
4880        ale.COL102,
4881        ale.COL103,
4882        ale.COL104,
4883        ale.COL105,
4884        ale.COL106,
4885        ale.COL107,
4886        ale.COL108,
4887        ale.COL109,
4888        ale.COL110,
4889        ale.COL111,
4890        ale.COL112,
4891        ale.COL113,
4892        ale.COL114,
4893        ale.COL115,
4894        ale.COL116,
4895        ale.COL117,
4896        ale.COL118,
4897        ale.COL119,
4898        ale.COL120,
4899        ale.COL121,
4900        ale.COL122,
4901        ale.COL123,
4902        ale.COL124,
4903        ale.COL125,
4904        ale.COL126,
4905        ale.COL127,
4906        ale.COL128,
4907        ale.COL129,
4908        ale.COL130,
4909        ale.COL131,
4910        ale.COL132,
4911        ale.COL133,
4912        ale.COL134,
4913        ale.COL135,
4914        ale.COL136,
4915        ale.COL137,
4916        ale.COL138,
4917        ale.COL139,
4918        ale.COL140,
4919        ale.COL141,
4920        ale.COL142,
4921        ale.COL143,
4922        ale.COL144,
4923        ale.COL145,
4924        ale.COL146,
4925        ale.COL147,
4926        ale.COL148,
4927        ale.COL149,
4928        ale.COL150,
4929        ale.COL151,
4930        ale.COL152,
4931        ale.COL153,
4932        ale.COL154,
4933        ale.COL155,
4934        ale.COL156,
4935        ale.COL157,
4936        ale.COL158,
4937        ale.COL159,
4938        ale.COL160,
4939        ale.COL161,
4940        ale.COL162,
4941        ale.COL163,
4942        ale.COL164,
4943        ale.COL165,
4944        ale.COL166,
4945        ale.COL167,
4946        ale.COL168,
4947        ale.COL169,
4948        ale.COL170,
4949        ale.COL171,
4950        ale.COL172,
4951        ale.COL173,
4952        ale.COL174,
4953        ale.COL175,
4954        ale.COL176,
4955        ale.COL177,
4956        ale.COL178,
4957        ale.COL179,
4958        ale.COL180,
4959        ale.COL181,
4960        ale.COL182,
4961        ale.COL183,
4962        ale.COL184,
4963        ale.COL185,
4964        ale.COL186,
4965        ale.COL187,
4966        ale.COL188,
4967        ale.COL189,
4968        ale.COL190,
4969        ale.COL191,
4970        ale.COL192,
4971        ale.COL193,
4972        ale.COL194,
4973        ale.COL195,
4974        ale.COL196,
4975        ale.COL197,
4976        ale.COL198,
4977        ale.COL199,
4978        ale.COL200,
4979        ale.COL201,
4980        ale.COL202,
4981        ale.COL203,
4982        ale.COL204,
4983        ale.COL205,
4984        ale.COL206,
4985        ale.COL207,
4986        ale.COL208,
4987        ale.COL209,
4988        ale.COL210,
4989        ale.COL211,
4990        ale.COL212,
4991        ale.COL213,
4992        ale.COL214,
4993        ale.COL215,
4994        ale.COL216,
4995        ale.COL217,
4996        ale.COL218,
4997        ale.COL219,
4998        ale.COL220,
4999        ale.COL221,
5000        ale.COL222,
5001        ale.COL223,
5002        ale.COL224,
5003        ale.COL225,
5004        ale.COL226,
5005        ale.COL227,
5006        ale.COL228,
5007        ale.COL229,
5008        ale.COL230,
5009        ale.COL231,
5010        ale.COL232,
5011        ale.COL233,
5012        ale.COL234,
5013        ale.COL235,
5014        ale.COL236,
5015        ale.COL237,
5016        ale.COL238,
5017        ale.COL239,
5018        ale.COL240,
5019        ale.COL241,
5020        ale.COL242,
5021        ale.COL243,
5022        ale.COL244,
5023        ale.COL245,
5024        ale.COL246,
5025        ale.COL247,
5026        ale.COL248,
5027        ale.COL249,
5028        ale.COL250 ,
5029        ale.COL251     ,
5030        ale.COL252     ,
5031        ale.COL253     ,
5032        ale.COL254     ,
5033        ale.COL256     ,
5034        ale.COL255     ,
5035        ale.COL257     ,
5036        ale.COL258     ,
5037        ale.COL259     ,
5038        ale.COL260     ,
5039        ale.COL261     ,
5040        ale.COL262     ,
5041        ale.COL263     ,
5042        ale.COL264     ,
5043        ale.COL265     ,
5044        ale.COL266     ,
5045        ale.COL267     ,
5046        ale.COL268     ,
5047        ale.COL269     ,
5048        ale.COL270     ,
5049        ale.COL271     ,
5050        ale.COL272     ,
5051        ale.COL273     ,
5052        ale.COL274     ,
5053        ale.COL275     ,
5054        ale.COL276     ,
5055        ale.COL277     ,
5056        ale.COL278     ,
5057        ale.COL279     ,
5058        ale.COL280     ,
5059        ale.COL281     ,
5060        ale.COL282     ,
5061        ale.COL283     ,
5062        ale.COL284     ,
5063        ale.COL285     ,
5064        ale.COL286     ,
5065        ale.COL287     ,
5066        ale.COL288     ,
5067        ale.COL289     ,
5068        ale.COL290     ,
5069        ale.COL291     ,
5070        ale.COL292     ,
5071        ale.COL293     ,
5072        ale.COL294     ,
5073        ale.COL295     ,
5074        ale.COL296     ,
5075        ale.COL297     ,
5076        ale.COL298     ,
5077        ale.COL299     ,
5078        ale.COL300     ,
5079        ale.SALES_AGENT_EMAIL_ADDRESS,
5080        ale.RESOURCE_ID,
5081        ale.location_id,
5082        ale.contact_point_id,
5083        ale.orig_system_reference,
5084        ale.CUSTOM_COLUMN1,
5085        ale.CUSTOM_COLUMN2,
5086        ale.CUSTOM_COLUMN3,
5087        ale.CUSTOM_COLUMN4,
5088        ale.CUSTOM_COLUMN5,
5089        ale.CUSTOM_COLUMN6,
5090        ale.CUSTOM_COLUMN7,
5091        ale.CUSTOM_COLUMN8,
5092        ale.CUSTOM_COLUMN9,
5093        ale.CUSTOM_COLUMN10,
5094        ale.CUSTOM_COLUMN11,
5095        ale.CUSTOM_COLUMN12,
5096        ale.CUSTOM_COLUMN13,
5097        ale.CUSTOM_COLUMN14,
5098        ale.CUSTOM_COLUMN15,
5099        ale.CUSTOM_COLUMN16,
5100        ale.CUSTOM_COLUMN17,
5101        ale.CUSTOM_COLUMN18,
5102        ale.CUSTOM_COLUMN19,
5103        ale.CUSTOM_COLUMN20,
5104        ale.CUSTOM_COLUMN21,
5105        ale.CUSTOM_COLUMN22,
5106        ale.CUSTOM_COLUMN23,
5107        ale.CUSTOM_COLUMN24,
5108        ale.CUSTOM_COLUMN25
5109        ) =
5110         (select
5111         'Y',
5112         'Y',
5113        ail.SUFFIX,
5114        ail.FIRST_NAME,
5115        ail.LAST_NAME,
5116        ail.CUSTOMER_NAME,
5117        ail.TITLE,
5118        ail.ADDRESS_LINE1,
5119        ail.ADDRESS_LINE2,
5120        ail.CITY,
5121        ail.STATE,
5122        ail.ZIPCODE,
5123        ail.COUNTRY,
5124        ail.FAX,
5125        ail.PHONE,
5126        ail.EMAIL_ADDRESS,
5127        ail.COL1,
5128        ail.COL2,
5129        ail.COL3,
5130        ail.COL4,
5131        ail.COL5,
5132        ail.COL6,
5133        ail.COL7,
5134        ail.COL8,
5135        ail.COL9,
5136        ail.COL10,
5137        ail.COL11,
5138        ail.COL12,
5139        ail.COL13,
5140        ail.COL14,
5141        ail.COL15,
5142        ail.COL16,
5143        ail.COL17,
5144        ail.COL18,
5145        ail.COL19,
5146        ail.COL20,
5147        ail.COL21,
5148        ail.COL22,
5149        ail.COL23,
5150        ail.COL24,
5151        ail.COL25,
5152        ail.COL26,
5153        ail.COL27,
5154        ail.COL28,
5155        ail.COL29,
5156        ail.COL30,
5157        ail.COL31,
5158        ail.COL32,
5159        ail.COL33,
5160        ail.COL34,
5161        ail.COL35,
5162        ail.COL36,
5163        ail.COL37,
5164        ail.COL38,
5165        ail.COL39,
5166        ail.COL40,
5167        ail.COL41,
5168        ail.COL42,
5169        ail.COL43,
5170        ail.COL44,
5171        ail.COL45,
5172        ail.COL46,
5173        ail.COL47,
5174        ail.COL48,
5175        ail.COL49,
5176        ail.COL50,
5177        ail.COL51,
5178        ail.COL52,
5179        ail.COL53,
5180        ail.COL54,
5181        ail.COL55,
5182        ail.COL56,
5183        ail.COL57,
5184        ail.COL58,
5185        ail.COL59,
5186        ail.COL60,
5187        ail.COL61,
5188        ail.COL62,
5189        ail.COL63,
5190        ail.COL64,
5191        ail.COL65,
5192        ail.COL66,
5193        ail.COL67,
5194        ail.COL68,
5195        ail.COL69,
5196        ail.COL70,
5197        ail.COL71,
5198        ail.COL72,
5199        ail.COL73,
5200        ail.COL74,
5201        ail.COL75,
5202        ail.COL76,
5203        ail.COL77,
5204        ail.COL78,
5205        ail.COL79,
5206        ail.COL80,
5207        ail.COL81,
5208        ail.COL82,
5209        ail.COL83,
5210        ail.COL84,
5211        ail.COL85,
5212        ail.COL86,
5213        ail.COL87,
5214        ail.COL88,
5215        ail.COL89,
5216        ail.COL90,
5217        ail.COL91,
5218        ail.COL92,
5219        ail.COL93,
5220        ail.COL94,
5221        ail.COL95,
5222        ail.COL96,
5223        ail.COL97,
5224        ail.COL98,
5225        ail.COL99,
5226        ail.COL100,
5227        ail.COL101,
5228        ail.COL102,
5229        ail.COL103,
5230        ail.COL104,
5231        ail.COL105,
5232        ail.COL106,
5233        ail.COL107,
5234        ail.COL108,
5235        ail.COL109,
5236        ail.COL110,
5237        ail.COL111,
5238        ail.COL112,
5239        ail.COL113,
5240        ail.COL114,
5241        ail.COL115,
5242        ail.COL116,
5243        ail.COL117,
5244        ail.COL118,
5245        ail.COL119,
5246        ail.COL120,
5247        ail.COL121,
5248        ail.COL122,
5249        ail.COL123,
5250        ail.COL124,
5251        ail.COL125,
5252        ail.COL126,
5253        ail.COL127,
5254        ail.COL128,
5255        ail.COL129,
5256        ail.COL130,
5257        ail.COL131,
5258        ail.COL132,
5259        ail.COL133,
5260        ail.COL134,
5261        ail.COL135,
5262        ail.COL136,
5263        ail.COL137,
5264        ail.COL138,
5265        ail.COL139,
5266        ail.COL140,
5267        ail.COL141,
5268        ail.COL142,
5269        ail.COL143,
5270        ail.COL144,
5271        ail.COL145,
5272        ail.COL146,
5273        ail.COL147,
5274        ail.COL148,
5275        ail.COL149,
5276        ail.COL150,
5277        ail.COL151,
5278        ail.COL152,
5279        ail.COL153,
5280        ail.COL154,
5281        ail.COL155,
5282        ail.COL156,
5283        ail.COL157,
5284        ail.COL158,
5285        ail.COL159,
5286        ail.COL160,
5287        ail.COL161,
5288        ail.COL162,
5289        ail.COL163,
5290        ail.COL164,
5291        ail.COL165,
5292        ail.COL166,
5293        ail.COL167,
5294        ail.COL168,
5295        ail.COL169,
5296        ail.COL170,
5297        ail.COL171,
5298        ail.COL172,
5299        ail.COL173,
5300        ail.COL174,
5301        ail.COL175,
5302        ail.COL176,
5303        ail.COL177,
5304        ail.COL178,
5305        ail.COL179,
5306        ail.COL180,
5307        ail.COL181,
5308        ail.COL182,
5309        ail.COL183,
5310        ail.COL184,
5311        ail.COL185,
5312        ail.COL186,
5313        ail.COL187,
5314        ail.COL188,
5315        ail.COL189,
5316        ail.COL190,
5317        ail.COL191,
5318        ail.COL192,
5319        ail.COL193,
5320        ail.COL194,
5321        ail.COL195,
5322        ail.COL196,
5323        ail.COL197,
5324        ail.COL198,
5325        ail.COL199,
5326        ail.COL200,
5327        ail.COL201,
5328        ail.COL202,
5329        ail.COL203,
5330        ail.COL204,
5331        ail.COL205,
5332        ail.COL206,
5333        ail.COL207,
5334        ail.COL208,
5335        ail.COL209,
5336        ail.COL210,
5337        ail.COL211,
5338        ail.COL212,
5339        ail.COL213,
5340        ail.COL214,
5341        ail.COL215,
5342        ail.COL216,
5343        ail.COL217,
5344        ail.COL218,
5345        ail.COL219,
5346        ail.COL220,
5347        ail.COL221,
5348        ail.COL222,
5349        ail.COL223,
5350        ail.COL224,
5351        ail.COL225,
5352        ail.COL226,
5353        ail.COL227,
5354        ail.COL228,
5355        ail.COL229,
5356        ail.COL230,
5357        ail.COL231,
5358        ail.COL232,
5359        ail.COL233,
5360        ail.COL234,
5361        ail.COL235,
5362        ail.COL236,
5363        ail.COL237,
5364        ail.COL238,
5365        ail.COL239,
5366        ail.COL240,
5367        ail.COL241,
5368        ail.COL242,
5369        ail.COL243,
5370        ail.COL244,
5371        ail.COL245,
5372        ail.COL246,
5373        ail.COL247,
5374        ail.COL248,
5375        ail.COL249,
5376        ail.COL250 ,
5377        ail.COL251 ,
5378        ail.COL252 ,
5379        ail.COL253 ,
5380        ail.COL254 ,
5381        ail.COL256 ,
5382        ail.COL255 ,
5383        ail.COL257 ,
5384        ail.COL258 ,
5385        ail.COL259 ,
5386        ail.COL260 ,
5387        ail.COL261 ,
5388        ail.COL262 ,
5389        ail.COL263 ,
5390        ail.COL264 ,
5391        ail.COL265 ,
5392        ail.COL266 ,
5393        ail.COL267 ,
5394        ail.COL268 ,
5395        ail.COL269 ,
5396        ail.COL270 ,
5397        ail.COL271 ,
5398        ail.COL272 ,
5399        ail.COL273 ,
5400        ail.COL274 ,
5401        ail.COL275 ,
5402        ail.COL276 ,
5403        ail.COL277 ,
5404        ail.COL278 ,
5405        ail.COL279 ,
5406        ail.COL280 ,
5407        ail.COL281 ,
5408        ail.COL282 ,
5409        ail.COL283 ,
5410        ail.COL284 ,
5411        ail.COL285 ,
5412        ail.COL286 ,
5413        ail.COL287 ,
5414        ail.COL288 ,
5415        ail.COL289 ,
5416        ail.COL290 ,
5417        ail.COL291 ,
5418        ail.COL292 ,
5419        ail.COL293 ,
5420        ail.COL294 ,
5421        ail.COL295 ,
5422        ail.COL296 ,
5423        ail.COL297 ,
5424        ail.COL298 ,
5425        ail.COL299 ,
5426        ail.COL300     ,
5427        ail.SALES_AGENT_EMAIL_ADDRESS,
5428        ail.RESOURCE_ID,
5429        ail.location_id,
5430        ail.contact_point_id,
5431        ail.orig_system_reference,
5432        ail.CUSTOM_COLUMN1,
5433        ail.CUSTOM_COLUMN2,
5434        ail.CUSTOM_COLUMN3,
5435        ail.CUSTOM_COLUMN4,
5436        ail.CUSTOM_COLUMN5,
5437        ail.CUSTOM_COLUMN6,
5438        ail.CUSTOM_COLUMN7,
5439        ail.CUSTOM_COLUMN8,
5440        ail.CUSTOM_COLUMN9,
5441        ail.CUSTOM_COLUMN10,
5442        ail.CUSTOM_COLUMN11,
5443        ail.CUSTOM_COLUMN12,
5444        ail.CUSTOM_COLUMN13,
5445        ail.CUSTOM_COLUMN14,
5446        ail.CUSTOM_COLUMN15,
5447        ail.CUSTOM_COLUMN16,
5448        ail.CUSTOM_COLUMN17,
5449        ail.CUSTOM_COLUMN18,
5450        ail.CUSTOM_COLUMN19,
5451        ail.CUSTOM_COLUMN20,
5452        ail.CUSTOM_COLUMN21,
5453        ail.CUSTOM_COLUMN22,
5454        ail.CUSTOM_COLUMN23,
5455        ail.CUSTOM_COLUMN24,
5456        ail.CUSTOM_COLUMN25
5457     from   ams_list_entries ail,
5458            ams_act_lists als
5459    where   als.ACT_LIST_HEADER_ID = ale.list_select_action_id
5460      and   als.LIST_HEADER_ID = ail.list_header_id
5461      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
5462      and   ail.enabled_flag = 'Y'
5463      and   rownum <=1 )
5464   where arc_list_select_action_from = 'LIST'
5465   and list_header_id = p_list_header_id
5466   and exists (select 'x'
5467              from ams_list_entries ail,
5468                   ams_act_lists als
5469              where   als.ACT_LIST_HEADER_ID = ale.list_select_action_id
5470                and   als.LIST_HEADER_ID = ail.list_header_id
5471                and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
5472                and   ail.enabled_flag = 'Y' )
5473   and exists (select 'x'
5474              from ams_act_lists als1
5475              where als1.ACT_LIST_HEADER_ID = ale.list_select_action_id
5476                and als1.LIST_ACTION_TYPE = 'INCLUDE' );
5477   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
5478 
5479  ELSE -- For List Generation
5480    write_to_act_log('Generating list(which has selections based on LIST) in update mode.','LIST',g_list_header_id,'LOW');
5481    update ams_list_entries  ale
5482    set (
5483        ale.newly_updated_flag ,
5484        ale.enabled_flag ,
5485        ale.SUFFIX,
5486        ale.FIRST_NAME,
5487        ale.LAST_NAME,
5488        ale.CUSTOMER_NAME,
5489        ale.TITLE,
5490        ale.ADDRESS_LINE1,
5491        ale.ADDRESS_LINE2,
5492        ale.CITY,
5493        ale.STATE,
5494        ale.ZIPCODE,
5495        ale.COUNTRY,
5496        ale.FAX,
5497        ale.PHONE,
5498        ale.EMAIL_ADDRESS,
5499        ale.COL1,
5500        ale.COL2,
5501        ale.COL3,
5502        ale.COL4,
5503        ale.COL5,
5504        ale.COL6,
5505        ale.COL7,
5506        ale.COL8,
5507        ale.COL9,
5508        ale.COL10,
5509        ale.COL11,
5510        ale.COL12,
5511        ale.COL13,
5512        ale.COL14,
5513        ale.COL15,
5514        ale.COL16,
5515        ale.COL17,
5516        ale.COL18,
5517        ale.COL19,
5518        ale.COL20,
5519        ale.COL21,
5520        ale.COL22,
5521        ale.COL23,
5522        ale.COL24,
5523        ale.COL25,
5524        ale.COL26,
5525        ale.COL27,
5526        ale.COL28,
5527        ale.COL29,
5528        ale.COL30,
5529        ale.COL31,
5530        ale.COL32,
5531        ale.COL33,
5532        ale.COL34,
5533        ale.COL35,
5534        ale.COL36,
5535        ale.COL37,
5536        ale.COL38,
5537        ale.COL39,
5538        ale.COL40,
5539        ale.COL41,
5540        ale.COL42,
5541        ale.COL43,
5542        ale.COL44,
5543        ale.COL45,
5544        ale.COL46,
5545        ale.COL47,
5546        ale.COL48,
5547        ale.COL49,
5548        ale.COL50,
5549        ale.COL51,
5550        ale.COL52,
5551        ale.COL53,
5552        ale.COL54,
5553        ale.COL55,
5554        ale.COL56,
5555        ale.COL57,
5556        ale.COL58,
5557        ale.COL59,
5558        ale.COL60,
5559        ale.COL61,
5560        ale.COL62,
5561        ale.COL63,
5562        ale.COL64,
5563        ale.COL65,
5564        ale.COL66,
5565        ale.COL67,
5566        ale.COL68,
5567        ale.COL69,
5568        ale.COL70,
5569        ale.COL71,
5570        ale.COL72,
5571        ale.COL73,
5572        ale.COL74,
5573        ale.COL75,
5574        ale.COL76,
5575        ale.COL77,
5576        ale.COL78,
5577        ale.COL79,
5578        ale.COL80,
5579        ale.COL81,
5580        ale.COL82,
5581        ale.COL83,
5582        ale.COL84,
5583        ale.COL85,
5584        ale.COL86,
5585        ale.COL87,
5586        ale.COL88,
5587        ale.COL89,
5588        ale.COL90,
5589        ale.COL91,
5590        ale.COL92,
5591        ale.COL93,
5592        ale.COL94,
5593        ale.COL95,
5594        ale.COL96,
5595        ale.COL97,
5596        ale.COL98,
5597        ale.COL99,
5598        ale.COL100,
5599        ale.COL101,
5600        ale.COL102,
5601        ale.COL103,
5602        ale.COL104,
5603        ale.COL105,
5604        ale.COL106,
5605        ale.COL107,
5606        ale.COL108,
5607        ale.COL109,
5608        ale.COL110,
5609        ale.COL111,
5610        ale.COL112,
5611        ale.COL113,
5612        ale.COL114,
5613        ale.COL115,
5614        ale.COL116,
5615        ale.COL117,
5616        ale.COL118,
5617        ale.COL119,
5618        ale.COL120,
5619        ale.COL121,
5620        ale.COL122,
5621        ale.COL123,
5622        ale.COL124,
5623        ale.COL125,
5624        ale.COL126,
5625        ale.COL127,
5626        ale.COL128,
5627        ale.COL129,
5628        ale.COL130,
5629        ale.COL131,
5630        ale.COL132,
5631        ale.COL133,
5632        ale.COL134,
5633        ale.COL135,
5634        ale.COL136,
5635        ale.COL137,
5636        ale.COL138,
5637        ale.COL139,
5638        ale.COL140,
5639        ale.COL141,
5640        ale.COL142,
5641        ale.COL143,
5642        ale.COL144,
5643        ale.COL145,
5644        ale.COL146,
5645        ale.COL147,
5646        ale.COL148,
5647        ale.COL149,
5648        ale.COL150,
5649        ale.COL151,
5650        ale.COL152,
5651        ale.COL153,
5652        ale.COL154,
5653        ale.COL155,
5654        ale.COL156,
5655        ale.COL157,
5656        ale.COL158,
5657        ale.COL159,
5658        ale.COL160,
5659        ale.COL161,
5660        ale.COL162,
5661        ale.COL163,
5662        ale.COL164,
5663        ale.COL165,
5664        ale.COL166,
5665        ale.COL167,
5666        ale.COL168,
5667        ale.COL169,
5668        ale.COL170,
5669        ale.COL171,
5670        ale.COL172,
5671        ale.COL173,
5672        ale.COL174,
5673        ale.COL175,
5674        ale.COL176,
5675        ale.COL177,
5676        ale.COL178,
5677        ale.COL179,
5678        ale.COL180,
5679        ale.COL181,
5680        ale.COL182,
5681        ale.COL183,
5682        ale.COL184,
5683        ale.COL185,
5684        ale.COL186,
5685        ale.COL187,
5686        ale.COL188,
5687        ale.COL189,
5688        ale.COL190,
5689        ale.COL191,
5690        ale.COL192,
5691        ale.COL193,
5692        ale.COL194,
5693        ale.COL195,
5694        ale.COL196,
5695        ale.COL197,
5696        ale.COL198,
5697        ale.COL199,
5698        ale.COL200,
5699        ale.COL201,
5700        ale.COL202,
5701        ale.COL203,
5702        ale.COL204,
5703        ale.COL205,
5704        ale.COL206,
5705        ale.COL207,
5706        ale.COL208,
5707        ale.COL209,
5708        ale.COL210,
5709        ale.COL211,
5710        ale.COL212,
5711        ale.COL213,
5712        ale.COL214,
5713        ale.COL215,
5714        ale.COL216,
5715        ale.COL217,
5716        ale.COL218,
5717        ale.COL219,
5718        ale.COL220,
5719        ale.COL221,
5720        ale.COL222,
5721        ale.COL223,
5722        ale.COL224,
5723        ale.COL225,
5724        ale.COL226,
5725        ale.COL227,
5726        ale.COL228,
5727        ale.COL229,
5728        ale.COL230,
5729        ale.COL231,
5730        ale.COL232,
5731        ale.COL233,
5732        ale.COL234,
5733        ale.COL235,
5734        ale.COL236,
5735        ale.COL237,
5736        ale.COL238,
5737        ale.COL239,
5738        ale.COL240,
5739        ale.COL241,
5740        ale.COL242,
5741        ale.COL243,
5742        ale.COL244,
5743        ale.COL245,
5744        ale.COL246,
5745        ale.COL247,
5746        ale.COL248,
5747        ale.COL249,
5748        ale.COL250 ,
5749        ale.COL251     ,
5750        ale.COL252     ,
5751        ale.COL253     ,
5752        ale.COL254     ,
5753        ale.COL256     ,
5754        ale.COL255     ,
5755        ale.COL257     ,
5756        ale.COL258     ,
5757        ale.COL259     ,
5758        ale.COL260     ,
5759        ale.COL261     ,
5760        ale.COL262     ,
5761        ale.COL263     ,
5762        ale.COL264     ,
5763        ale.COL265     ,
5764        ale.COL266     ,
5765        ale.COL267     ,
5766        ale.COL268     ,
5767        ale.COL269     ,
5768        ale.COL270     ,
5769        ale.COL271     ,
5770        ale.COL272     ,
5771        ale.COL273     ,
5772        ale.COL274     ,
5773        ale.COL275     ,
5774        ale.COL276     ,
5775        ale.COL277     ,
5776        ale.COL278     ,
5777        ale.COL279     ,
5778        ale.COL280     ,
5779        ale.COL281     ,
5780        ale.COL282     ,
5781        ale.COL283     ,
5782        ale.COL284     ,
5783        ale.COL285     ,
5784        ale.COL286     ,
5785        ale.COL287     ,
5786        ale.COL288     ,
5787        ale.COL289     ,
5788        ale.COL290     ,
5789        ale.COL291     ,
5790        ale.COL292     ,
5791        ale.COL293     ,
5792        ale.COL294     ,
5793        ale.COL295     ,
5794        ale.COL296     ,
5795        ale.COL297     ,
5796        ale.COL298     ,
5797        ale.COL299     ,
5798        ale.COL300
5799        ) =
5800         (select
5801         'Y',
5802         'Y',
5803        ail.SUFFIX,
5804        ail.FIRST_NAME,
5805        ail.LAST_NAME,
5806        ail.CUSTOMER_NAME,
5807        ail.TITLE,
5808        ail.ADDRESS_LINE1,
5809        ail.ADDRESS_LINE2,
5810        ail.CITY,
5811        ail.STATE,
5812        ail.ZIPCODE,
5813        ail.COUNTRY,
5814        ail.FAX,
5815        ail.PHONE,
5816        ail.EMAIL_ADDRESS,
5817        ail.COL1,
5818        ail.COL2,
5819        ail.COL3,
5820        ail.COL4,
5821        ail.COL5,
5822        ail.COL6,
5823        ail.COL7,
5824        ail.COL8,
5825        ail.COL9,
5826        ail.COL10,
5827        ail.COL11,
5828        ail.COL12,
5829        ail.COL13,
5830        ail.COL14,
5831        ail.COL15,
5832        ail.COL16,
5833        ail.COL17,
5834        ail.COL18,
5835        ail.COL19,
5836        ail.COL20,
5837        ail.COL21,
5838        ail.COL22,
5839        ail.COL23,
5840        ail.COL24,
5841        ail.COL25,
5842        ail.COL26,
5843        ail.COL27,
5844        ail.COL28,
5845        ail.COL29,
5846        ail.COL30,
5847        ail.COL31,
5848        ail.COL32,
5849        ail.COL33,
5850        ail.COL34,
5851        ail.COL35,
5852        ail.COL36,
5853        ail.COL37,
5854        ail.COL38,
5855        ail.COL39,
5856        ail.COL40,
5857        ail.COL41,
5858        ail.COL42,
5859        ail.COL43,
5860        ail.COL44,
5861        ail.COL45,
5862        ail.COL46,
5863        ail.COL47,
5864        ail.COL48,
5865        ail.COL49,
5866        ail.COL50,
5867        ail.COL51,
5868        ail.COL52,
5869        ail.COL53,
5870        ail.COL54,
5871        ail.COL55,
5872        ail.COL56,
5873        ail.COL57,
5874        ail.COL58,
5875        ail.COL59,
5876        ail.COL60,
5877        ail.COL61,
5878        ail.COL62,
5879        ail.COL63,
5880        ail.COL64,
5881        ail.COL65,
5882        ail.COL66,
5883        ail.COL67,
5884        ail.COL68,
5885        ail.COL69,
5886        ail.COL70,
5887        ail.COL71,
5888        ail.COL72,
5889        ail.COL73,
5890        ail.COL74,
5891        ail.COL75,
5892        ail.COL76,
5893        ail.COL77,
5894        ail.COL78,
5895        ail.COL79,
5896        ail.COL80,
5897        ail.COL81,
5898        ail.COL82,
5899        ail.COL83,
5900        ail.COL84,
5901        ail.COL85,
5902        ail.COL86,
5903        ail.COL87,
5904        ail.COL88,
5905        ail.COL89,
5906        ail.COL90,
5907        ail.COL91,
5908        ail.COL92,
5909        ail.COL93,
5910        ail.COL94,
5911        ail.COL95,
5912        ail.COL96,
5913        ail.COL97,
5914        ail.COL98,
5915        ail.COL99,
5916        ail.COL100,
5917        ail.COL101,
5918        ail.COL102,
5919        ail.COL103,
5920        ail.COL104,
5921        ail.COL105,
5922        ail.COL106,
5923        ail.COL107,
5924        ail.COL108,
5925        ail.COL109,
5926        ail.COL110,
5927        ail.COL111,
5928        ail.COL112,
5929        ail.COL113,
5930        ail.COL114,
5931        ail.COL115,
5932        ail.COL116,
5933        ail.COL117,
5934        ail.COL118,
5935        ail.COL119,
5936        ail.COL120,
5937        ail.COL121,
5938        ail.COL122,
5939        ail.COL123,
5940        ail.COL124,
5941        ail.COL125,
5942        ail.COL126,
5943        ail.COL127,
5944        ail.COL128,
5945        ail.COL129,
5946        ail.COL130,
5947        ail.COL131,
5948        ail.COL132,
5949        ail.COL133,
5950        ail.COL134,
5951        ail.COL135,
5952        ail.COL136,
5953        ail.COL137,
5954        ail.COL138,
5955        ail.COL139,
5956        ail.COL140,
5957        ail.COL141,
5958        ail.COL142,
5959        ail.COL143,
5960        ail.COL144,
5961        ail.COL145,
5962        ail.COL146,
5963        ail.COL147,
5964        ail.COL148,
5965        ail.COL149,
5966        ail.COL150,
5967        ail.COL151,
5968        ail.COL152,
5969        ail.COL153,
5970        ail.COL154,
5971        ail.COL155,
5972        ail.COL156,
5973        ail.COL157,
5974        ail.COL158,
5975        ail.COL159,
5976        ail.COL160,
5977        ail.COL161,
5978        ail.COL162,
5979        ail.COL163,
5980        ail.COL164,
5981        ail.COL165,
5982        ail.COL166,
5983        ail.COL167,
5984        ail.COL168,
5985        ail.COL169,
5986        ail.COL170,
5987        ail.COL171,
5988        ail.COL172,
5989        ail.COL173,
5990        ail.COL174,
5991        ail.COL175,
5992        ail.COL176,
5993        ail.COL177,
5994        ail.COL178,
5995        ail.COL179,
5996        ail.COL180,
5997        ail.COL181,
5998        ail.COL182,
5999        ail.COL183,
6000        ail.COL184,
6001        ail.COL185,
6002        ail.COL186,
6003        ail.COL187,
6004        ail.COL188,
6005        ail.COL189,
6006        ail.COL190,
6007        ail.COL191,
6008        ail.COL192,
6009        ail.COL193,
6010        ail.COL194,
6011        ail.COL195,
6012        ail.COL196,
6013        ail.COL197,
6014        ail.COL198,
6015        ail.COL199,
6016        ail.COL200,
6017        ail.COL201,
6018        ail.COL202,
6019        ail.COL203,
6020        ail.COL204,
6021        ail.COL205,
6022        ail.COL206,
6023        ail.COL207,
6024        ail.COL208,
6025        ail.COL209,
6026        ail.COL210,
6027        ail.COL211,
6028        ail.COL212,
6029        ail.COL213,
6030        ail.COL214,
6031        ail.COL215,
6032        ail.COL216,
6033        ail.COL217,
6034        ail.COL218,
6035        ail.COL219,
6036        ail.COL220,
6037        ail.COL221,
6038        ail.COL222,
6039        ail.COL223,
6040        ail.COL224,
6041        ail.COL225,
6042        ail.COL226,
6043        ail.COL227,
6044        ail.COL228,
6045        ail.COL229,
6046        ail.COL230,
6047        ail.COL231,
6048        ail.COL232,
6049        ail.COL233,
6050        ail.COL234,
6051        ail.COL235,
6052        ail.COL236,
6053        ail.COL237,
6054        ail.COL238,
6055        ail.COL239,
6056        ail.COL240,
6057        ail.COL241,
6058        ail.COL242,
6059        ail.COL243,
6060        ail.COL244,
6061        ail.COL245,
6062        ail.COL246,
6063        ail.COL247,
6064        ail.COL248,
6065        ail.COL249,
6066        ail.COL250 ,
6067        ail.COL251 ,
6068        ail.COL252 ,
6069        ail.COL253 ,
6070        ail.COL254 ,
6071        ail.COL256 ,
6072        ail.COL255 ,
6073        ail.COL257 ,
6074        ail.COL258 ,
6075        ail.COL259 ,
6076        ail.COL260 ,
6077        ail.COL261 ,
6078        ail.COL262 ,
6079        ail.COL263 ,
6080        ail.COL264 ,
6081        ail.COL265 ,
6082        ail.COL266 ,
6083        ail.COL267 ,
6084        ail.COL268 ,
6085        ail.COL269 ,
6086        ail.COL270 ,
6087        ail.COL271 ,
6088        ail.COL272 ,
6089        ail.COL273 ,
6090        ail.COL274 ,
6091        ail.COL275 ,
6092        ail.COL276 ,
6093        ail.COL277 ,
6094        ail.COL278 ,
6095        ail.COL279 ,
6096        ail.COL280 ,
6097        ail.COL281 ,
6098        ail.COL282 ,
6099        ail.COL283 ,
6100        ail.COL284 ,
6101        ail.COL285 ,
6102        ail.COL286 ,
6103        ail.COL287 ,
6104        ail.COL288 ,
6105        ail.COL289 ,
6106        ail.COL290 ,
6107        ail.COL291 ,
6108        ail.COL292 ,
6109        ail.COL293 ,
6110        ail.COL294 ,
6111        ail.COL295 ,
6112        ail.COL296 ,
6113        ail.COL297 ,
6114        ail.COL298 ,
6115        ail.COL299 ,
6116        ail.COL300
6117     from   ams_list_entries ail,
6118            ams_list_select_actions als
6119    where   als.list_select_action_id = ale.list_select_action_id
6120      and   als.incl_object_id = ail.list_header_id
6121      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
6122      and   ail.enabled_flag = 'Y'
6123      and   rownum <=1 )
6124   where arc_list_select_action_from = 'LIST'
6125   and list_header_id = p_list_header_id
6126   and exists (select 'x'
6127     from   ams_list_entries ail,
6128            ams_list_select_actions als
6129    where   als.list_select_action_id = ale.list_select_action_id
6130      and   als.incl_object_id = ail.list_header_id
6131      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
6132      and   ail.enabled_flag = 'Y' )
6133   and exists (select 'x'
6134              from ams_list_select_actions als1
6135              where als1.list_select_action_id = ale.list_select_action_id
6136                and als1.list_action_type = 'INCLUDE' );
6137   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6138 end if;
6139 end;
6140 procedure update_import_list_entries(p_list_header_id in number) is
6141 cursor c1 is
6142 select imp.import_type
6143 from  ams_imp_list_headers_all imp,
6144       ams_list_select_actions ail,
6145       ams_list_headers_all alh
6146 where alh.list_header_id = p_list_header_id
6147 and   alh.list_header_id = ail.action_used_by_id
6148 and   ail.arc_action_used_by = 'LIST'
6149 and   ail.arc_incl_object_from  = 'IMPH'
6150 and   imp.import_list_header_id = ail.incl_object_id ;
6151 l_b2b_flag varchar2(5) := 'U' ;
6152 begin
6153   open c1;
6154   fetch c1 into l_b2b_flag ;
6155   close c1;
6156   write_to_act_log('Import type is '||l_b2b_flag,'LIST',g_list_header_id,'LOW');
6157   if l_b2b_flag = 'B2B' then
6158      write_to_act_log('Generating list(which has selections based on imported B2B list) in update mode.','LIST',g_list_header_id,'LOW');
6159      update ams_list_entries  ale
6160      set (
6161        ale.newly_updated_flag ,
6162 --       ale.enabled_flag ,
6163        ale.ADDRESS_LINE1,
6164        ale.ADDRESS_LINE2,
6165        ale.COL127,
6166        ale.COL128,
6167        ale.COL227,
6168        ale.CITY,
6169        ale.COUNTRY,
6170        ale.COL118,
6171        ale.COL142,
6172        ale.COL138,
6173        ale.COL122,
6174        ale.EMAIL_ADDRESS,
6175        ale.COL239,
6176        ale.FIRST_NAME,
6177        ale.COL243,
6178        ale.COL144,
6179        ale.LAST_NAME,
6180        ale.COL251,
6181        ale.COL252,
6182        ale.COL137,
6183        ale.SUFFIX,
6184        ale.COL259,
6185        ale.COL6,
6186        ale.COL5,
6187        ale.COL7,
6188        ale.PHONE,
6189        ale.ZIPCODE,
6190        ale.COL120,
6191        ale.STATE,
6192        ale.COL125,
6193        ale.COL2,
6194        ale.TITLE,
6195        ale.customer_name,
6196        ale.party_id,
6197        ale.COL276 ,
6198        ale.NOTES                                    ,
6199               ale.VEHICLE_RESPONSE_CODE                   ,
6200               ale.SALES_AGENT_EMAIL_ADDRESS               ,
6201               ale.RESOURCE_ID                              ,
6202               ale.col147,
6203               ale.location_id ,
6204               ale.contact_point_id ,
6205               ale.orig_system_reference,
6206               col116,
6207               col117
6208        )
6209      =
6210      ( select
6211            'Y',
6212 --           'Y',
6213            ail.ADDRESS1,
6214            ail.ADDRESS2,
6215            ail.BEST_TIME_CONTACT_BEGIN,
6216            ail.BEST_TIME_CONTACT_END,
6217            ail.CEO_NAME,
6218            ail.CITY,
6219            ail.COUNTRY,
6220            ail.COUNTY,
6221            ail.DECISION_MAKER_FLAG,
6222            ail.DEPARTMENT,
6223            ail.DUNS_NUMBER,
6224            ail.EMAIL_ADDRESS,
6225            ail.EMPLOYEES_TOTAL,
6226            ail.PERSON_FIRST_NAME,
6227            ail.FISCAL_YEAREND_MONTH,
6228            ail.JOB_TITLE,
6229            ail.PERSON_LAST_NAME,
6230            ail.LEGAL_STATUS,
6231            ail.LINE_OF_BUSINESS,
6232            ail.PERSON_MIDDLE_NAME,
6233            ail.PERSON_NAME_SUFFIX,
6234            ail.party_name,
6235            ail.PHONE_AREA_CODE,
6236            ail.PHONE_COUNTRY_CODE,
6237            ail.PHONE_EXTENTION,
6238            ail.PHONE_NUMBER,
6239            ail.POSTAL_CODE,
6240            ail.PROVINCE,
6241            ail.STATE,
6242            ail.TAX_REFERENCE,
6243            ail.TIME_ZONE,
6244            ail.PERSON_NAME_PREFIX,
6245            ail.party_name
6246            ,ail.party_id
6247            ,ail.YEAR_ESTABLISHED
6248            ,ail.NOTES                                    ,
6249               ail.VEHICLE_RESPONSE_CODE                   ,
6250               ail.SALES_AGENT_EMAIL_ADDRESS               ,
6251               ail.RESOURCE_ID                              ,
6252               ail.ORGANIZATION_ID,
6253              ail.location_id ,
6254              ail.contact_point_id ,
6255              ail.orig_system_reference,
6256               ail.address3,
6257               ail.address4
6258     from   ams_hz_b2b_mapping_v ail,
6259            ams_list_select_actions als
6260    where   ail.import_list_header_id = als.incl_object_id
6261      and   als.list_select_action_id = ale.list_select_action_id
6262      and   ail.party_id = ale.list_entry_source_system_id
6263      and   ail.IMPORT_SOURCE_LINE_ID = ale.IMP_SOURCE_LINE_ID)
6264   where arc_list_select_action_from = 'IMPH'
6265   and list_header_id = p_list_header_id
6266   and exists (select 'x'
6267              from ams_list_select_actions als1
6268              where als1.list_select_action_id = ale.list_select_action_id
6269                and als1.list_action_type = 'INCLUDE' );
6270   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6271   end if;
6272   if l_b2b_flag = 'B2C' then
6273      write_to_act_log('Generating list(which has selections based on imported B2C list) in update mode.','LIST',g_list_header_id,'LOW');
6274      update ams_list_entries  ale
6275      set (
6276        ale.newly_updated_flag ,
6277 --       ale.enabled_flag ,
6278        ale.customer_name,
6279        ale.ADDRESS_LINE1,
6280        ale.ADDRESS_LINE2,
6281        ale.CITY,
6282        ale.COL127,
6283        ale.COL128,
6284        ale.COL118,
6285        ale.COUNTRY,
6286        ale.FIRST_NAME,
6287        ale.LAST_NAME,
6288        ale.COL137,
6289        ale.EMAIL_ADDRESS,
6290        ale.col70,
6291        ale.COL145,
6292        ale.STATE,
6293        ale.ZIPCODE,
6294        ale.COL120,
6295        ale.TITLE,
6296        ale.COL2,
6297        ale.col5,
6298        ale.col6,
6299        ale.PHONE,
6300        ale.col7,
6301        ale.party_id,
6302        ale.SUFFIX  ,
6303        ale.NOTES                                    ,
6304               ale.VEHICLE_RESPONSE_CODE                   ,
6305               ale.SALES_AGENT_EMAIL_ADDRESS               ,
6306               ale.RESOURCE_ID                              ,
6307               ale.location_id ,
6308               ale.contact_point_id ,
6309               ale.orig_system_reference,
6310               ale.col116,
6311               ale.col117
6312        )
6313       =
6314      (select
6315            'Y',
6316 --           'Y',
6317            ail.PERSON_LAST_NAME || ' , ' || ail.PERSON_FIRST_NAME ,
6318            ail.ADDRESS1,
6319            ail.ADDRESS2,
6320            ail.CITY,
6321            ail.BEST_TIME_CONTACT_BEGIN,
6322            ail.BEST_TIME_CONTACT_END,
6323            ail.COUNTY,
6324            ail.COUNTRY,
6325            ail.PERSON_FIRST_NAME,
6326            ail.PERSON_LAST_NAME,
6327            ail.PERSON_MIDDLE_NAME,
6328            ail.EMAIL_ADDRESS,
6329            ail.GENDER,
6330            ail.HOUSEHOLD_INCOME,
6331            ail.STATE,
6332            ail.POSTAL_CODE,
6333            ail.PROVINCE,
6334            ail.PERSON_NAME_PREFIX,
6335            ail.TIME_ZONE  ,
6336            ail.PHONE_COUNTRY_CODE,
6337            ail.PHONE_AREA_CODE   ,
6338            ail.PHONE_NUMBER      ,
6339            ail.PHONE_EXTENTION   ,
6340            ail.party_id,
6341            ail.PERSON_NAME_SUFFIX ,
6342            ail.NOTES                                    ,
6343               ail.VEHICLE_RESPONSE_CODE                   ,
6344               ail.SALES_AGENT_EMAIL_ADDRESS               ,
6345               ail.RESOURCE_ID                              ,
6346           ail.location_id ,
6347           ail.contact_point_id ,
6348           ail.orig_system_reference,
6349               ail.address3,
6350               ail.address4
6351     from   ams_hz_b2c_mapping_v ail,
6352            ams_list_select_actions als
6353    where   ail.import_list_header_id = als.incl_object_id
6354      and   als.list_select_action_id = ale.list_select_action_id
6355      and   ail.party_id = ale.list_entry_source_system_id
6356      and   ail.IMPORT_SOURCE_LINE_ID = ale.IMP_SOURCE_LINE_ID)
6357   where   ale.arc_list_select_action_from = 'IMPH'
6358   and ale.list_header_id = p_list_header_id
6359   and exists (select 'x'
6360              from ams_list_select_actions als1
6361              where als1.list_select_action_id = ale.list_select_action_id
6362                and als1.list_action_type = 'INCLUDE' );
6363   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6364  end if;
6365 end;
6366 --
6367 -- NAME
6368 --   GET_LIST_ENTRY_DATA.
6369 --
6370 -- PURPOSE
6371 --
6372 --  01/24/2001 GJOBY      Modified for hornet
6373 -- END OF COMMENTS
6374 
6375 PROCEDURE GET_LIST_ENTRY_DATA
6376                  (p_list_header_id in number,
6377                   p_additional_where_condition in varchar2,
6378                   x_return_status OUT NOCOPY varchar2 )IS
6379 
6380 --------------------------------------------------------------------------
6381 --Retrieve all mapping types used in  an action workbook sql statement. --
6382 --this includes all master and sub types, if a type is a sub type there --
6383 --will be values in the c.sub_source_type_pk_column and source_type_code--
6384 --fields.                                                               --
6385 --------------------------------------------------------------------------
6386 CURSOR  C_MAPPING_TYPES_USED(p_list_header_id
6387                              AMS_LIST_HEADERS_ALL.LIST_HEADER_ID%TYPE)
6388 IS SELECT a.list_source_type_id,
6389 	  a.source_type_code,
6390 	  a.source_object_name,
6391 	  a.source_object_pk_field,
6392 	  a.master_source_type_flag
6393      FROM ams_list_src_types a,
6394 	  ams_list_src_type_usages b
6395     WHERE a.source_type_code       = b.source_type_code
6396       AND b.list_header_id         = p_list_header_id
6397       AND master_source_type_flag = 'Y'  ;
6398 cursor c_child_mapping(c_master_type_id in number) is
6399 	SELECT al.SUB_SOURCE_TYPE_ID,
6400                al.SUB_SOURCE_TYPE_PK_COLUMN,
6401                als.source_object_name,
6402                als.source_type_code,
6403                al.master_source_type_pk_column
6404 	 FROM ams_list_src_type_assocs al,
6405               ams_list_src_types als ,
6406               ams_list_src_type_usages b
6407 	WHERE al.MASTER_SOURCE_TYPE_ID   = c_master_type_id
6408 	  AND als.list_source_type_id    = al.sub_source_type_id
6409 	  AND als.source_type_code       = b.source_type_code
6410 	  AND b.list_header_id           = p_list_header_id ;
6411 l_list_source_type_id      AMS_LIST_SRC_TYPES.LIST_SOURCE_TYPE_ID%TYPE;
6412 l_source_type_code         AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6413 l_source_object_name       AMS_LIST_SRC_TYPES.SOURCE_OBJECT_NAME%TYPE;
6414 l_source_object_pk_field   AMS_LIST_SRC_TYPES.SOURCE_OBJECT_PK_FIELD%TYPE;
6415 l_master_source_type_flag  AMS_LIST_SRC_TYPES.MASTER_SOURCE_TYPE_FLAG%TYPE;
6416 l_sub_source_type_pk_column      AMS_LIST_SRC_TYPE_ASSOCS.SUB_SOURCE_TYPE_PK_COLUMN%TYPE;
6417 l_dummy_pk_column      AMS_LIST_SRC_TYPE_ASSOCS.master_SOURCE_TYPE_PK_COLUMN%TYPE;
6418 l_SUB_SOURCE_TYPE_ID      number;
6419 l_SUB_SOURCE_OBJECT_NAME      varchar2(300);
6420 l_sub_source_type_code   AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6421 l_sub_source_master_type   AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6422 l_source_code              AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6423 
6424 ------------------------------------------------------------
6425 --Retrive all the fields to be used for each mapping type.--
6426 ------------------------------------------------------------
6427 CURSOR C_MAPPING_TYPE_FIELDS(p_list_source_type_id
6428                              AMS_LIST_SRC_TYPES.LIST_SOURCE_TYPE_ID%TYPE)
6429 IS
6430 SELECT field_column_name,
6431        source_column_name
6432 FROM   ams_list_src_fields
6433 WHERE  list_source_type_id = p_list_source_type_id
6434   and  used_in_list_entries = 'Y';
6435 
6436 
6437 ------------------------------------------------
6438 --a table which holds list entry column names.--
6439 ------------------------------------------------
6440 TYPE t_list_columns is Table of VARCHAR2(30) index by binary_integer;
6441 
6442 --------------------------------------------------
6443 --a local table variable of type t_list_columns.--
6444 --------------------------------------------------
6445 l_list_entry_columns t_list_columns;
6446 
6447 --------------------------------------------------
6448 --a local table variable of type t_list_columns.--
6449 --------------------------------------------------
6450 l_source_columns t_list_columns;
6451 
6452 --------------------------------------------------------
6453 --Used to initialize variables of type t_list_columns.--
6454 --------------------------------------------------------
6455 l_NULL_table t_list_columns;
6456 
6457 ------------------------------------------
6458 --the number of columns for the mapping.--
6459 ------------------------------------------
6460 l_column_count NUMBER;
6461 l_iterator NUMBER;
6462 l_sub_type_detected NUMBER;
6463 
6464 --------------------------------------------------------------
6465 --The composite strings which compose a valid SQL statement.--
6466 --------------------------------------------------------------
6467 l_update_str      VARCHAR2(32767);
6468 l_select_str      VARCHAR2(32767);
6469 l_header_clause   VARCHAR2(1000);
6470 l_type_clause     VARCHAR2(1000);
6471 l_where_clause    VARCHAR2(1000);
6472 
6473 l_add_where_clause   VARCHAR2(2000);
6474 cursor c_source_map is
6475 select field_column_name
6476 from ams_list_src_fields
6477 where list_source_type_id = l_list_source_type_id--l_sub_source_type_id
6478 and   source_column_name = l_dummy_pk_column;
6479 
6480 l_dummy_sr_column      AMS_LIST_SRC_FIELDS.Field_column_name%TYPE;
6481 l_no_of_chunks number;
6482 
6483 x_msg_count        number;
6484 x_msg_data         varchar2(1000);
6485 l_tot_cnt          number;
6486 l_null		   varchar2(30) := null;
6487 l_remote_cnt       number := 0;
6488 
6489 
6490 BEGIN
6491 
6492     if g_list_header_id is null  then
6493        g_list_header_id := p_list_header_id;
6494     end if;
6495 
6496     if nvl(g_remote_list,'N') = 'Y' then
6497        execute immediate 'select count(1) from ams_list_entries@'||g_database_link||' where list_header_id = '||p_list_header_id||' and rownum = 1' into l_remote_cnt;
6498        if l_remote_cnt = 0 then
6499           write_to_act_log('No entries in remote schema for this list/target group. Cannot update.', 'LIST', g_list_header_id,'LOW');
6500           x_return_status := 'S';
6501 	  return;
6502        else
6503           write_to_act_log('List entries will be updated in remote schema.', 'LIST', g_list_header_id,'LOW');
6504           g_remote_list_gen := 'Y';
6505        end if;
6506     end if;
6507 
6508     write_to_act_log('Executing procedure get_list_entry_data.', 'LIST', g_list_header_id,'LOW');
6509     OPEN C_MAPPING_TYPES_USED(p_list_header_id);
6510     LOOP
6511       l_update_str         := 'UPDATE ams_list_entries SET (';
6512       l_select_str         := ' ) = ( SELECT ';
6513       l_header_clause      := ' AND list_header_id = '||
6514                                to_char(p_list_header_id);
6515       l_type_clause        := ' AND list_entry_source_system_type = ';
6516 
6517       l_add_where_clause   :=  ' AND arc_list_select_action_from not in  ' ||
6518                                    -- ' (''LIST'', ''IMPH'') ';LPO
6519 				   ' (''IMPH'') ';
6520 
6521       l_add_where_clause  := l_add_where_clause||' and enabled_flag = '||'''Y''';
6522 
6523       l_add_where_clause   :=  l_add_where_clause ||
6524                                p_additional_where_condition ;
6525 
6526       l_sub_type_detected  :=0;
6527       l_iterator           :=0;
6528       l_list_entry_columns := l_NULL_table;
6529       l_source_columns     := l_NULL_table;
6530 
6531      FETCH C_MAPPING_TYPES_USED INTO l_list_source_type_id,
6532                                       l_source_type_code,
6533                                       l_source_object_name,
6534                                       l_source_object_pk_field,
6535                                       l_master_source_type_flag;
6536 
6537      write_to_act_log('List source type id = '||l_list_source_type_id , 'LIST', g_list_header_id,'LOW');
6538      write_to_act_log('List source type code = '||l_source_type_code , 'LIST', g_list_header_id,'LOW');
6539      write_to_act_log('Source object name = '||l_source_object_name , 'LIST', g_list_header_id,'LOW');
6540      write_to_act_log('Source object primary key = '||l_source_object_pk_field, 'LIST', g_list_header_id,'LOW');
6541      write_to_act_log('Master source type flag= '||l_master_source_type_flag, 'LIST', g_list_header_id,'LOW');
6542      EXIT WHEN C_MAPPING_TYPES_USED%NOTFOUND;
6543 
6544       ------------------------------------------------------------
6545       --getting the field mappings between the mapping type     --
6546       --source object and the list entry table.                 --
6547       ------------------------------------------------------------
6548       OPEN C_MAPPING_TYPE_FIELDS(l_list_source_type_id);
6549       LOOP
6550          l_iterator := l_iterator + 1;
6551          FETCH c_mapping_type_fields
6552          INTO l_list_entry_columns(l_iterator),
6553               l_source_columns(l_iterator);
6554          EXIT WHEN  C_MAPPING_TYPE_FIELDS%NOTFOUND;
6555          write_to_act_log('Field column name = '||l_list_entry_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6556          write_to_act_log('Source column name = '||l_source_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6557       END LOOP;
6558       CLOSE C_MAPPING_TYPE_FIELDS;
6559 
6560       FOR i IN l_list_entry_columns.FIRST .. l_list_entry_columns.LAST LOOP
6561             l_update_str := l_update_str||l_list_entry_columns(i)||',';
6562             l_select_str := l_select_str||l_source_columns(i)||',';
6563       END LOOP;
6564       l_update_str := substr(l_update_str,1,length(l_update_str) - 1);
6565       l_select_str := substr(l_select_str,1,length(l_select_str) - 1);
6566 
6567       --l_update_str := substrb(l_update_str,1,length(l_update_str)-1);
6568       --l_select_str := substrb(l_select_str,1,length(l_select_str)-1);
6569      /* l_update_str := l_update_str || 'newly_updated_flag' ;
6570       l_select_str := l_select_str || ''''|| 'Y'||'''';
6571       l_update_str := l_update_str || ','|| 'enabled_flag' ;
6572       l_select_str := l_select_str || ','|| ''''|| 'Y'||'''';*/
6573 
6574       l_select_str := l_select_str||' FROM '||
6575                       l_source_object_name||' WHERE '||
6576                       l_source_object_pk_field
6577                       ||' = list_entry_source_system_id ';
6578       l_type_clause := l_type_clause||' :b2 ';
6579       l_source_code :=  l_source_type_code;
6580       l_where_clause:= ' WHERE list_header_id = :b1 ';
6581 
6582       l_update_str := l_update_str||l_select_str||')'||
6583                        l_where_clause||l_type_clause || l_add_where_clause;
6584       write_to_act_log('Source_code = '||l_source_code, 'LIST', g_list_header_id,'LOW');
6585       write_to_act_log('Update statement : '||l_update_str, 'LIST', g_list_header_id,'LOW');
6586 
6587       if nvl(g_remote_list,'N') = 'N' then
6588          write_to_act_log('Executing the update statement in local schema', 'LIST', g_list_header_id,'LOW');
6589          EXECUTE IMMEDIATE l_update_str using to_char(p_list_header_id) , l_source_code;
6590       else
6591         write_to_act_log('Executing the update statement in remote schema', 'LIST', g_list_header_id,'LOW');
6592 	execute immediate
6593 	'BEGIN
6594 	 AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
6595 	' END;'
6596 	 using  '1',
6597 	 l_null,
6598 	 'T',
6599 	 l_null,
6600 	 OUT x_return_status,
6601 	 OUT x_msg_count,
6602 	 OUT x_msg_data,
6603 	 g_list_header_id,
6604 	 l_update_str,
6605 	 l_source_code,
6606 	 OUT l_tot_cnt,
6607 	 'UPDATE';
6608       end if;
6609 
6610     write_to_act_log('Identifying attributes from child datasource', 'LIST',g_list_header_id,'LOW');
6611 
6612     OPEN c_child_mapping(l_list_source_type_id);
6613     LOOP
6614       l_dummy_pk_column := '';
6615       l_dummy_sr_column := '';
6616      FETCH c_child_mapping
6617            INTO l_SUB_SOURCE_TYPE_ID,
6618                 l_SUB_SOURCE_TYPE_PK_COLUMN,
6619                 l_SUB_SOURCE_OBJECT_NAME,
6620                 l_sub_source_type_code,
6621                 l_dummy_pk_column;
6622 
6623       EXIT WHEN c_child_mapping%NOTFOUND;
6624       if l_dummy_pk_column is not null then
6625          open c_source_map ;
6626          fetch c_source_map into l_dummy_sr_column;
6627          close c_source_map ;
6628          --l_dummy_sr_column := '';
6629       end if;
6630        l_iterator := 0;
6631       l_list_entry_columns := l_NULL_table;
6632       l_source_columns     := l_NULL_table;
6633       l_update_str         := 'UPDATE ams_list_entries SET (';
6634       l_select_str         := ' ) = ( SELECT ';
6635       l_header_clause      := ' AND list_header_id = '||
6636                                to_char(p_list_header_id);
6637       l_type_clause        := ' AND list_entry_source_system_type = ';
6638       l_add_where_clause       := ' AND arc_list_select_action_from not in  ' ||
6639                                    -- ' (''LIST'', ''IMPH'') ';LPO
6640 				   ' (''IMPH'') ';
6641       l_add_where_clause  := l_add_where_clause || ' and enabled_flag = '||'''Y''';
6642       l_add_where_clause       :=  l_add_where_clause ||
6643                                p_additional_where_condition ;
6644 
6645 
6646       OPEN C_MAPPING_TYPE_FIELDS(l_sub_source_type_id);
6647       LOOP
6648         l_iterator := l_iterator + 1;
6649         FETCH c_mapping_type_fields
6650          INTO l_list_entry_columns(l_iterator),
6651               l_source_columns(l_iterator);
6652          EXIT WHEN  C_MAPPING_TYPE_FIELDS%NOTFOUND;
6653          write_to_act_log('Field column name = '||l_list_entry_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6654          write_to_act_log('Source column name = '||l_source_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6655       END LOOP;
6656       CLOSE C_MAPPING_TYPE_FIELDS;
6657 
6658       if l_list_entry_columns.count > 0 then
6659          FOR i IN l_list_entry_columns.FIRST .. l_list_entry_columns.LAST LOOP
6660             l_update_str := l_update_str||l_list_entry_columns(i)||',';
6661             l_select_str := l_select_str||l_source_columns(i)||',';
6662          END LOOP;
6663          l_update_str := substrb(l_update_str,1,length(l_update_str)-1);
6664          l_select_str := substrb(l_select_str,1,length(l_select_str)-1);
6665 
6666          if l_dummy_sr_column is null then
6667             l_select_str := l_select_str||' FROM '||
6668                             l_SUB_SOURCE_OBJECT_NAME||' WHERE '||
6669                             l_SUB_SOURCE_TYPE_PK_COLUMN
6670                             ||' = list_entry_source_system_id ';
6671          else
6672 
6673              l_select_str := l_select_str||' FROM '||
6674                              l_SUB_SOURCE_OBJECT_NAME||' WHERE '||
6675                              l_SUB_SOURCE_TYPE_PK_COLUMN
6676                             ||' = ' || l_dummy_sr_column ;
6677           end if;
6678           l_type_clause := l_type_clause||' :b2 ';
6679           l_source_code :=  l_source_type_code;
6680           l_where_clause:= ' WHERE list_header_id = :b1 ';
6681 
6682           l_update_str := l_update_str||l_select_str||')'||
6683                           l_where_clause||l_type_clause || l_add_where_clause;
6684 
6685           l_no_of_chunks  := ceil(length(l_update_str)/2000 );
6686           for i in 1 ..l_no_of_chunks
6687           loop
6688              write_to_act_log(substrb(l_update_str,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
6689           end loop;
6690           write_to_act_log('Source_code = '||l_source_code, 'LIST', g_list_header_id,'LOW');
6691           if nvl(g_remote_list,'N') = 'N' then
6692              EXECUTE IMMEDIATE l_update_str using to_char(p_list_header_id) , l_source_code;
6693           else
6694 	     execute immediate
6695 	     'BEGIN
6696               AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
6697              ' END;'
6698              using  '1',
6699              l_null,
6700              'T',
6701              l_null,
6702              OUT x_return_status,
6703              OUT x_msg_count,
6704              OUT x_msg_data,
6705              g_list_header_id,
6706              l_update_str,
6707              l_source_code,
6708              OUT l_tot_cnt,
6709              'UPDATE';
6710 	     g_remote_list_gen := 'Y';
6711           end if;
6712        else
6713           write_to_act_log('No field is updated since no field is mapped for data source : '||l_SUB_SOURCE_OBJECT_NAME, 'LIST', g_list_header_id,'LOW');
6714       end if; -- if l_list_entry_columns.count > 0 then
6715     END LOOP;
6716     CLOSE c_child_mapping;
6717     END LOOP;
6718     CLOSE C_MAPPING_TYPES_USED;
6719      x_return_status := FND_API.G_RET_STS_SUCCESS;
6720     write_to_act_log('Calling update_list_entries to update selections based on list.','LIST', g_list_header_id,'LOW');
6721    -- update_list_entries(p_list_header_id); LPO
6722     write_to_act_log('Calling update_import_list_entries to update selections based on import.','LIST', g_list_header_id,'LOW');
6723     update_import_list_entries (p_list_header_id);
6724 EXCEPTION
6725   WHEN OTHERS THEN
6726 
6727     IF(C_MAPPING_TYPES_USED%ISOPEN )THEN
6728        CLOSE  C_MAPPING_TYPES_USED;
6729     END IF;
6730 
6731     IF(C_MAPPING_TYPE_FIELDS%ISOPEN )THEN
6732          CLOSE  C_MAPPING_TYPE_FIELDS;
6733     END IF;
6734 
6735     write_to_act_log('Error while generating list in update mode : '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
6736     x_return_status := FND_API.G_RET_STS_ERROR;
6737 END GET_LIST_ENTRY_DATA;
6738 
6739 
6740 -- START OF COMMENTS
6741 -- NAME  :   GENERATE_LIST.
6742 -- PURPOSE
6743 --   1. Public Procedure which when called will generate a set of list
6744 --      entries into the ams_list_entries table.
6745 -- HISTORY
6746 --   06/01/1999        tdonohoe            created
6747 --   01/24/2001        gjoby               Re-Created for Hornet
6748 -- END OF COMMENTS
6749 -------------------------------------------------------------------------------
6750 
6751 PROCEDURE GENERATE_LIST
6752 ( p_api_version            IN     NUMBER,
6753   p_init_msg_list          IN     VARCHAR2   := FND_API.G_TRUE,
6754   p_commit                 IN     VARCHAR2   := FND_API.G_FALSE,
6755   p_validation_level       IN     NUMBER     := FND_API.G_VALID_LEVEL_FULL,
6756   p_list_header_id         IN     NUMBER,
6757   x_return_status          OUT NOCOPY    VARCHAR2,
6758   x_msg_count              OUT NOCOPY    NUMBER,
6759   x_msg_data               OUT NOCOPY    VARCHAR2) IS
6760 
6761   l_api_name            CONSTANT VARCHAR2(30)  := 'GENERATE_LIST';
6762   l_api_version         CONSTANT NUMBER        := 1.0;
6763 
6764   -----------------------------------------------------------
6765   --The no. of entries flagged as duplicates for this list.--
6766   --Only populated if deduplication is requested.          --
6767   -----------------------------------------------------------
6768   --gjoby check if l_no_of_duplicates required
6769   l_no_of_duplicates      NUMBER := 0;
6770 
6771   -- Two records are required for init rec and complete rec
6772   -- Table ams_list_headers_all_tl and ams_list_headers_all
6773   l_listheader_rec        ams_listheader_pvt.list_header_rec_type;
6774   l_tmp_listheader_rec    ams_listheader_pvt.list_header_rec_type;
6775 
6776   -- Two records are required for init rec and complete rec
6777   -- Table ams_list_select_actions
6778   l_listaction_rec        ams_listaction_pvt.action_rec_type;
6779   l_tmp_listaction_rec    ams_listaction_pvt.action_rec_type;
6780   l_no_of_rows_in_list number ;
6781   cursor c_get_rows (c_list_header_id in number ) is
6782   select no_of_rows_in_list
6783   from ams_list_headers_all
6784   where list_header_id = c_list_header_id ;
6785   l_error_position       varchar2(100);
6786 
6787   cursor c_remote_list is
6788   select nvl(stypes.remote_flag,'N') ,database_link
6789     from ams_list_src_types stypes, ams_list_headers_all list
6790    where list.list_source_type = stypes.source_type_code
6791      and list_header_id  =  p_list_header_id;
6792 
6793   l_list_selection 	varchar2(1);
6794   l_onlylist_selection  varchar2(1);
6795   l_no_of_rows  number := 0;
6796 
6797 cursor c_check_gen_mode is
6798 select nvl(no_of_rows_in_list ,0)
6799   from ams_list_headers_all
6800  where list_header_id = p_list_header_id;
6801 
6802   cursor c_list_selection is
6803   select 'Y' from ams_list_select_actions
6804    where  action_used_by_id = p_list_header_id
6805      and  arc_action_used_by = 'LIST'
6806      and  arc_incl_object_from in ('CELL','DIWB','SQL');
6807 
6808   cursor c_only_list_selection is
6809   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
6810    where  act.action_used_by_id = p_list_header_id
6811    and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
6812    and  act.INCL_OBJECT_ID = head.list_header_id
6813    and  head.status_code = 'AVAILABLE'
6814    and  head.MIGRATION_DATE is null;
6815 
6816 l_null		varchar2(30) := null;
6817 l_total_recs	number;
6818 l_request_id    number :=0;
6819 --Bug 5235979. Bmuthukr
6820 
6821 /* cursor c1 is
6822     SELECT list_rule_id
6823        FROM ams_list_rule_usages
6824        WHERE list_header_id = g_list_header_id;*/
6825 
6826 CURSOR C1 IS
6827 SELECT us.list_rule_id
6828   FROM ams_list_rule_usages us, ams_list_rules_all rules
6829  WHERE us.list_header_id = g_list_header_id
6830    AND us.list_rule_id = rules.list_rule_id
6831    AND rules.list_source_type = l_listheader_rec.list_source_type
6832    AND rules.list_rule_type = 'TARGET';
6833 
6834 -- Ends changes
6835 
6836 l_list_rule_id number := 0;
6837 l_action varchar2(30) := 'LIST';
6838 
6839 l_list_field_mapped  varchar2(1);
6840 
6841 -- SOLIN, bug 4410333
6842 -- check whether datasource is enabled.
6843 cursor c_check_datasource(c_list_header_id NUMBER) is
6844   SELECT a.enabled_flag
6845   FROM ams_list_src_types a,
6846        ams_list_headers_all b
6847   WHERE a.source_type_code = b.list_source_type
6848     AND b.list_header_id = c_list_header_id;
6849 
6850 l_ds_enabled_flag      VARCHAR2(1);
6851 -- SOLIN, end
6852 
6853 cursor c_master_ds_fields_mapped is
6854 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
6855 where hd.list_header_id = p_list_header_id
6856   and hd.LIST_SOURCE_TYPE = ty.source_type_code
6857   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
6858   and fd.FIELD_COLUMN_NAME is NOT NULL;
6859 
6860 cursor c_child_ds_fields_mapped is
6861 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
6862 ams_list_src_type_assocs ats
6863 where hd.list_header_id = p_list_header_id
6864   and hd.LIST_SOURCE_TYPE = ty.source_type_code
6865   and ty.list_source_type_id = ats.master_source_type_id
6866   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
6867   and fd.FIELD_COLUMN_NAME is NOT NULL;
6868 
6869 -- SOLIN, bug 3484653
6870 CURSOR c_get_dup_fields(c_list_header_id NUMBER) IS
6871 SELECT min(master_child.field_column_name1) ,count(master_child.field_column_name) from
6872   (
6873   SELECT d.field_column_name field_column_name1,d.field_column_name
6874   FROM ams_list_src_types a,
6875        ams_list_headers_all b,
6876        ams_list_src_fields d
6877   WHERE a.source_type_code = b.list_source_type
6878    and b.list_header_id = p_list_header_id
6879    and d.list_source_type_id = a.list_source_type_id
6880    and d.USED_IN_LIST_ENTRIES = 'Y'
6881   union all
6882    SELECT d.field_column_name field_column_name1,d.field_column_name
6883   FROM ams_list_src_types a,
6884        ams_list_headers_all b,
6885        ams_list_src_fields d,
6886        ams_list_src_type_assocs e
6887   WHERE a.source_type_code = b.list_source_type
6888    and b.list_header_id = p_list_header_id
6889    and e.master_source_type_id = a.list_source_type_id
6890    and d.list_source_type_id = e.sub_source_type_id
6891    and d.USED_IN_LIST_ENTRIES = 'Y'
6892    ) master_child
6893   GROUP BY master_child.field_column_name
6894   having COUNT(master_child.field_column_name) > 1;
6895 
6896 --bmuthukr bug 4997699
6897 l_ds_name         varchar2(1000);
6898 l_field_col_name  varchar2(1000);
6899 l_source_col_name varchar2(1000);
6900 
6901 cursor c_get_dup_mapping(p_col_name in varchar2) is
6902 SELECT d.source_column_name, d.field_column_name , d.de_list_source_type_code  stc
6903   FROM ams_list_src_types a,
6904        ams_list_headers_all b,
6905        ams_list_src_fields d
6906  WHERE a.source_type_code = b.list_source_type
6907    and b.list_header_id = g_list_header_id
6908    and d.list_source_type_id = a.list_source_type_id
6909    and d.field_column_name = p_col_name
6910    and d.USED_IN_LIST_ENTRIES = 'Y'
6911 union all
6912 SELECT d.source_column_name, d.field_column_name,  d.de_list_source_type_code stc
6913   FROM ams_list_src_types a,
6914        ams_list_headers_all b,
6915        ams_list_src_fields d,
6916        ams_list_src_type_assocs e
6917  WHERE a.source_type_code = b.list_source_type
6918    and b.list_header_id = g_list_header_id
6919    and e.master_source_type_id = a.list_source_type_id
6920    and d.list_source_type_id = e.sub_source_type_id
6921    and d.field_column_name = p_col_name
6922    and d.USED_IN_LIST_ENTRIES = 'Y';
6923 
6924 l_field_column_name VARCHAR2(30);
6925 l_count             NUMBER;
6926 -- SOLIN, end
6927 
6928 /* added for remote bug ... savio ******/
6929 /*************************************************/
6930 CURSOR C10(P_LIST_HEADER_ID NUMBER) IS
6931   SELECT
6932     LIST_HEADER_ID,
6933     LAST_UPDATE_DATE,
6934     LAST_UPDATED_BY,
6935     CREATION_DATE,
6936     CREATED_BY,
6937     LAST_UPDATE_LOGIN,
6938     LIST_USED_BY_ID,
6939     ARC_LIST_USED_BY,
6940     LIST_TYPE,
6941     STATUS_CODE,
6942     STATUS_DATE,
6943     GENERATION_TYPE,
6944     OWNER_USER_ID,
6945     ROW_SELECTION_TYPE,
6946     NO_OF_ROWS_MAX_REQUESTED
6947   FROM AMS_LIST_HEADERS_ALL
6948  WHERE LIST_HEADER_ID = P_LIST_HEADER_ID;
6949 
6950 CURSOR C11(P_LIST_HEADER_ID NUMBER,
6951            P_ACTION         VARCHAR2) IS
6952   SELECT
6953     LIST_SELECT_ACTION_ID,
6954     LAST_UPDATE_DATE,
6955     LAST_UPDATED_BY,
6956     CREATION_DATE,
6957     CREATED_BY,
6958     LAST_UPDATE_LOGIN,
6959     ORDER_NUMBER,
6960     LIST_ACTION_TYPE,
6961     INCL_OBJECT_NAME,
6962     ARC_INCL_OBJECT_FROM,
6963     RANK,
6964     NO_OF_ROWS_AVAILABLE,
6965     NO_OF_ROWS_REQUESTED,
6966     NO_OF_ROWS_USED,
6967     DISTRIBUTION_PCT,
6968     ARC_ACTION_USED_BY,
6969     ACTION_USED_BY_ID,
6970     INCL_CONTROL_GROUP,
6971     NO_OF_ROWS_TARGETED,
6972     NO_OF_ROWS_DUPLICATES,
6973     RUNNING_TOTAL,
6974     DELTA
6975  FROM  AMS_LIST_SELECT_ACTIONS
6976  WHERE ACTION_USED_BY_ID = P_LIST_HEADER_ID
6977    AND ARC_ACTION_USED_BY = P_ACTION;
6978 
6979 c11_rec  c11%rowtype;
6980 c10_rec  c10%rowtype;
6981 l_main_random_nth_row_select number;
6982 
6983 
6984 l_remote_list_gen varchar2(1) := 'N';
6985 
6986 /* added for remote bug ... savio ******/
6987 
6988   l_is_manual   varchar2(1) := 'N'; --Added by bmuthukr for bug 3710720
6989 
6990 
6991 BEGIN
6992   l_error_position := '<- start List generate ->';
6993   -----------------------------------------------------------------------------
6994   -- g_list_header_id global variable for this session
6995   -- This eliminates the need for passing variables across procedures
6996   -- Particularly for logging debug messages ams_act_logs
6997   -----------------------------------------------------------------------------
6998 
6999   l_request_id := nvl(FND_GLOBAL.conc_request_id, -1);
7000 
7001   g_remote_list           := 'N';
7002   g_remote_list_gen       := 'N';
7003   g_database_link         := ' ';
7004   g_list_header_id        :=  p_list_header_id;
7005   g_count                 := 1;
7006   g_message_table  := g_message_table_null ;
7007 
7008   find_log_level(p_list_header_id);
7009 
7010   write_to_act_log(p_msg_data => 'Executing procedure generate_list. List generation started.',p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
7011   --write_to_act_log(p_msg_data => 'Concurrent request id is '||l_request_id,p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
7012   write_to_act_log(p_msg_data => 'Work flow item key(list header id) is '||p_list_header_id||' Process type is AMS List Generation',p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
7013 
7014   --Added by bmuthukr for bug 3710720
7015   is_manual(p_list_header_id  => p_list_header_id,
7016             x_return_status   => x_return_status,
7017             x_msg_count       => x_msg_count,
7018             x_msg_data        => x_msg_data,
7019             x_is_manual       => l_is_manual);
7020   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7021      write_to_act_log('Error in executing is_manual procedure', 'LIST', g_list_header_id,'HIGH');
7022      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7023      logger;
7024      return;
7025   end if;
7026 
7027   if nvl(l_is_manual,'N') = 'Y' then
7028      write_to_act_log('	Either list is a manual list, or incl are based on EMP list. Cannot generate','LIST',p_list_header_id,'HIGH');
7029      logger;
7030      return;
7031   end if;
7032   --Ends changes.
7033 
7034   -- SOLIN, bug 4410333
7035   l_ds_enabled_flag := 'N';
7036   OPEN c_check_datasource(p_list_header_id);
7037   FETCH c_check_datasource INTO l_ds_enabled_flag;
7038   CLOSE c_check_datasource;
7039 
7040   IF l_ds_enabled_flag = 'N' THEN
7041      write_to_act_log(
7042           p_msg_data => 'Aborting the List generation process. The datasource for this list is not enabled. Contact your administrator to enable the datasource, and generate the list again.',
7043           p_arc_log_used_by => 'LIST',
7044           p_log_used_by_id  => p_list_header_id,
7045 	  p_level => 'HIGH');
7046      UPDATE ams_list_headers_all
7047         SET last_generation_success_flag = 'N',
7048             status_code                  = 'FAILED',
7049             user_status_id               = 311,
7050             status_date                  = sysdate,
7051             last_update_date             = sysdate,
7052             main_gen_end_time            = sysdate
7053       WHERE list_header_id               = g_list_header_id;
7054       FND_MSG_PUB.count_and_get(
7055             p_encoded => FND_API.g_false,
7056             p_count   => x_msg_count,
7057             p_data    => x_msg_data
7058       );
7059       x_return_status := FND_API.g_ret_sts_error;
7060       --
7061       logger;
7062       RETURN;
7063   END IF;
7064 -- SOLIN, end
7065 
7066 
7067   open  c_master_ds_fields_mapped;
7068   fetch c_master_ds_fields_mapped into l_list_field_mapped;
7069   close c_master_ds_fields_mapped;
7070 
7071   open  c_child_ds_fields_mapped;
7072   fetch c_child_ds_fields_mapped into l_list_field_mapped;
7073   close c_child_ds_fields_mapped;
7074 
7075   if l_list_field_mapped is NULL THEN
7076      write_to_act_log(p_msg_data => 'Master/Child datasource fields are not mapped. Aborting list generation. ' ,
7077                       p_arc_log_used_by => 'LIST',
7078                       p_log_used_by_id  => p_list_header_id,
7079   	  	      p_level =>'HIGH');
7080 
7081       UPDATE ams_list_headers_all
7082       SET    last_generation_success_flag = 'N',
7083              status_code                  = 'FAILED',
7084              user_status_id               = 311,
7085              status_date                  = sysdate,
7086              last_update_date             = sysdate,
7087              main_gen_end_time            = sysdate
7088       WHERE  list_header_id               = p_list_header_id;
7089    -- calling logging program
7090       logger;
7091    --
7092      IF FND_API.To_Boolean ( p_commit ) THEN
7093        COMMIT WORK;
7094      END IF;
7095      --Modified by bmuthukr. Bug # 4083665
7096      x_return_status := FND_API.g_ret_sts_error;
7097      --
7098      RETURN;
7099   end if;
7100 
7101   -- SOLIN, bug 3484653
7102   OPEN c_get_dup_fields(p_list_header_id);
7103   FETCH c_get_dup_fields INTO l_field_column_name, l_count;
7104   CLOSE c_get_dup_fields;
7105 
7106   IF l_count>1 THEN
7107      /*delete from ams_act_logs
7108       where arc_act_log_used_by = 'LIST'
7109         and act_log_used_by_id  = p_list_header_id ;*/
7110 
7111 
7112      write_to_act_log(
7113           p_msg_data => 'Aborting the List generation process. Atleast one list entry column is mapped morethan once in the datasources.Pls see the following details for more info.',
7114           p_arc_log_used_by => 'LIST',
7115           p_log_used_by_id  => p_list_header_id,
7116 	  p_level => 'HIGH');
7117      --bmuthukr bug 4997699
7118      open c_get_dup_mapping(l_field_column_name);
7119      loop
7120         fetch c_get_dup_mapping into l_source_col_name, l_field_col_name  ,l_ds_name;
7121 	exit when c_get_dup_mapping%notfound;
7122         write_to_Act_log('Data Source Name :- '||l_ds_name||'          '||' Source Column :- '||l_source_col_name||'          '||' List Entries Col :- '||l_field_col_name,'LIST',p_list_header_id,'HIGH');
7123      end loop;
7124      --
7125      UPDATE ams_list_headers_all
7126         SET last_generation_success_flag = 'N',
7127             status_code                  = 'FAILED',
7128             user_status_id               = 311,
7129             status_date                  = sysdate,
7130             last_update_date             = sysdate,
7131             main_gen_end_time            = sysdate
7132       WHERE list_header_id               = g_list_header_id;
7133       --Modified by bmuthukr. Bug # 4083665
7134       x_return_status := FND_API.g_ret_sts_error;
7135       --
7136       logger;
7137       RETURN;
7138   END IF;
7139 -- SOLIN, end
7140 
7141   write_to_act_log(p_msg_data => 'List header id is '||g_list_header_id,
7142                    p_arc_log_used_by => 'LIST',
7143                    p_log_used_by_id  => p_list_header_id,
7144 		   p_level => 'LOW');
7145 
7146   x_return_status := FND_API.G_RET_STS_SUCCESS;
7147 
7148   -- Standard call to check for call compatibility.
7149   IF NOT FND_API.Compatible_API_Call ( l_api_version,
7150                                        p_api_version,
7151                                        l_api_name,
7152                                        G_PKG_NAME)
7153   THEN
7154     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7155   END IF;
7156 
7157   IF FND_API.to_Boolean( p_init_msg_list ) THEN
7158      FND_MSG_PUB.initialize;
7159   END IF;
7160 
7161   -- Checking if Debug is set. If debug is set then log debugging message
7162   IF (AMS_DEBUG_HIGH_ON) THEN
7163      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7164      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration : Start');
7165      FND_MSG_PUB.Add;
7166   END IF;
7167 
7168   --  Initialize API return status to success
7169   x_return_status := FND_API.G_RET_STS_SUCCESS;
7170 
7171 
7172   open c_remote_list;
7173   fetch c_remote_list into g_remote_list,g_database_link;
7174   close c_remote_list;
7175 
7176   if nvl(g_remote_list,'N') = 'N' then
7177      write_to_act_log(p_msg_data => 'List is based not on remote datasource.',
7178                       p_arc_log_used_by => 'LIST',
7179                       p_log_used_by_id  => p_list_header_id,
7180    	   	      p_level => 'LOW');
7181   elsif nvl(g_remote_list,'Y') = 'Y' then
7182      write_to_act_log(p_msg_data => 'List is based on remote datasource. Database link is  ' ||g_database_link,
7183                       p_arc_log_used_by => 'LIST',
7184                       p_log_used_by_id  => p_list_header_id,
7185    	   	      p_level => 'HIGH');
7186   end if;
7187 
7188   if g_remote_list = 'Y' then
7189 
7190      remote_list_gen(p_list_header_id  => p_list_header_id,
7191                      x_return_status   => x_return_status,
7192                      x_msg_count       => x_msg_count,
7193                      x_msg_data        => x_msg_data,
7194                      x_remote_gen      => g_remote_list_gen);
7195      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7196         write_to_act_log('Error in executing remote_list_gen procedure', 'LIST', g_list_header_id,'HIGH');
7197         write_to_act_log('Error '||x_msg_data, 'LIST', g_list_header_id,'HIGH');
7198      else
7199         write_to_act_log(p_msg_data => 'remote_list_gen procedure executed successfully.' ,
7200                          p_arc_log_used_by => 'LIST',
7201                          p_log_used_by_id  => p_list_header_id,
7202  			 p_level => 'LOW');
7203      end if;
7204 
7205   end if;
7206 
7207   l_error_position := '<- Init List->';
7208   -----------------------------------------------------------------------------
7209   -- Gets list header record details
7210   -- Intialize the record, set the list header id and retrieve the records
7211   -----------------------------------------------------------------------------
7212   write_to_act_log(p_msg_data => 'Calling ams_listheader_pvt to get the list header details.' ,
7213                    p_arc_log_used_by => 'LIST',
7214                    p_log_used_by_id  => p_list_header_id,
7215    	   	   p_level => 'LOW');
7216 
7217   ams_listheader_pvt.init_listheader_rec(l_tmp_listheader_rec);
7218   l_tmp_listheader_rec.list_header_id := p_list_header_id;
7219 
7220   l_error_position := '<- complete rec ->';
7221   ams_listheader_pvt.complete_listheader_rec
7222                    (p_listheader_rec  =>l_tmp_listheader_rec,
7223                     x_complete_rec    =>l_listheader_rec);
7224   -----------------------------------------------------------------------------
7225 
7226   -----------------------------------------------------------
7227   -- Initializes the list header record
7228   -----------------------------------------------------------
7229   l_error_position := '<- Initialize List ->';
7230 
7231   write_to_act_log(p_msg_data => 'Calling initialize_list to initialize the list.' ,
7232                    p_arc_log_used_by => 'LIST',
7233                    p_log_used_by_id  => p_list_header_id,
7234    	   	   p_level => 'LOW');
7235 
7236   initialize_List(p_list_header_rec => l_listheader_rec,
7237                   x_msg_count       => x_msg_count,
7238                   x_msg_data        => x_msg_data,
7239                   x_return_status   => x_return_status);
7240   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7241      write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
7242      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7243   else
7244      write_to_act_log(p_msg_data => 'List initialized.' ,
7245                       p_arc_log_used_by => 'LIST',
7246                       p_log_used_by_id  => p_list_header_id,
7247    	       	      p_level => 'LOW');
7248   end if;
7249 
7250   IF x_return_status = FND_API.g_ret_sts_error THEN
7251      RAISE FND_API.g_exc_error;
7252   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7253      RAISE FND_API.g_exc_unexpected_error;
7254   END IF;
7255 
7256   if l_listheader_rec.generation_type = 'UPD' then
7257      open c_check_gen_mode;
7258      fetch c_check_gen_mode into l_no_of_rows;
7259      close c_check_gen_mode;
7260 
7261      if l_no_of_rows = 0 then
7262         write_to_act_log('No entries in list entries table. Unable to generate list in update mode. Pls generate in full refresh/append mode.','LIST',g_list_header_id,'HIGH');
7263         UPDATE ams_list_headers_all
7264            SET last_generation_success_flag = 'N',
7265                status_code                  = 'FAILED',
7266                ctrl_status_code             = 'DRAFT',
7267                user_status_id               = 311,
7268                status_date                  = sysdate,
7269                last_update_date             = sysdate,
7270                main_gen_end_time            = sysdate,
7271                no_of_rows_in_ctrl_group     = null
7272          WHERE list_header_id               = g_list_header_id;
7273          x_return_status := FND_API.g_ret_sts_error;
7274          logger;
7275          RETURN;
7276       end if;
7277    end if;
7278 
7279 --From R12, only enabled entries will be updated in the update mode.
7280 --So no need to enable all the entries before generation.
7281   -- if l_listheader_rec.generation_type = 'UPDATE' then
7282 /*  if l_listheader_rec.generation_type = 'UPD' then
7283      write_to_act_log(' List is generated in UPDATE mode', 'LIST', g_list_header_id,'HIGH');
7284        update ams_list_entries
7285        set newly_updated_flag = 'N',
7286 	   enabled_flag = 'Y'
7287        where list_header_id = l_listheader_rec.list_header_id;
7288 */
7289    /********************************************************************
7290     Dynamic procedure will update the list from the remote instance in
7291     case of remote list
7292    *********************************************************************/
7293  /*    write_to_act_log(p_msg_data => 'Updating the list in remote instance. ',
7294                       p_arc_log_used_by => 'LIST',
7295                       p_log_used_by_id  => p_list_header_id,
7296 		      p_level => 'LOW');
7297      if g_remote_list = 'Y' then
7298        execute immediate
7299       'BEGIN
7300       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
7301       ' END;'
7302       using  '1',
7303              l_null,
7304              'T',
7305              l_null,
7306              OUT x_return_status,
7307              OUT x_msg_count,
7308              OUT x_msg_data,
7309              l_listheader_rec.list_header_id,
7310              l_null,
7311              l_null,
7312              OUT l_total_recs,
7313              'UPDATE';
7314        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7315           write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
7316           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7317        else
7318           write_to_act_log(p_msg_data => 'List generated in UPDATE mode in remote instance. ' ,
7319                            p_arc_log_used_by => 'LIST',
7320                            p_log_used_by_id  => p_list_header_id,
7321 	  		   p_level => 'LOW');
7322        end if;
7323      end if;
7324   end if;
7325 */
7326   update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
7327   if l_listheader_rec.generation_type = 'UPD' then
7328        l_error_position := '<- Get_list_entry_data inside deduplication ->';
7329 --For bug 5216890
7330 --   if g_remote_list <> 'Y' then
7331      write_to_act_log('List is generated in UPDATE mode in local instance.', 'LIST', g_list_header_id,'HIGH');
7332    --
7333    -- This will not be performed for the remote list generation
7334    --
7335      GET_LIST_ENTRY_DATA(
7336            p_list_header_id =>l_listheader_rec.list_header_id,
7337            x_return_status => x_return_status);
7338      IF x_return_status = FND_API.g_ret_sts_error THEN
7339         RAISE FND_API.g_exc_error;
7340      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7341         RAISE FND_API.g_exc_unexpected_error;
7342      END IF;
7343   -- end if;
7344 --  END IF;
7345   ELSE --
7346   l_error_position := '<- Process List Actions  ->';
7347   write_to_act_log('List will be generated in '||l_listheader_rec.generation_type||' mode.','LIST', g_list_header_id,'HIGH');
7348   write_to_act_log('Calling process_list_actions to generate list.', 'LIST', g_list_header_id,'LOW');
7349   process_list_Actions(p_action_used_by_id => l_listheader_rec.list_header_id,
7350                        p_action_used_by    => 'LIST',
7351                        p_log_flag          => l_listheader_rec.enable_log_flag,
7352                        x_return_status     => x_return_status,
7353                        x_msg_count         => x_msg_count,
7354                        x_msg_data          => x_msg_data);
7355   if x_return_status = 'E' then
7356      logger;
7357      commit;
7358      return;
7359   end if;
7360   END IF;
7361 
7362   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7363      write_to_act_log('Error in generating list.', 'LIST', g_list_header_id,'HIGH');
7364      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7365   end if;
7366 
7367   IF x_return_status = FND_API.g_ret_sts_error THEN
7368      RAISE FND_API.g_exc_error;
7369   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7370      RAISE FND_API.g_exc_unexpected_error;
7371   END IF;
7372 
7373 
7374 /******************************************************************************/
7375 /**************** call for cancel list generation added 05/23/2005 ************/
7376 /******************************************************************************/
7377 -- Inside generate_list 1
7378 
7379    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
7380                p_list_header_id => g_list_header_id ,
7381                p_remote_gen     => g_remote_list    ,
7382                p_remote_gen_list=> g_remote_list_gen,
7383                p_database_link  => g_database_link,
7384                x_msg_count      => x_msg_count ,
7385                x_msg_data       => x_msg_data ,
7386                x_return_status  => x_return_status
7387          );
7388 
7389   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
7390      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7391         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
7392         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
7393      end if;
7394      RAISE FND_API.G_EXC_ERROR;
7395   ELSE
7396      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
7397   END IF;
7398 
7399 /******************************************************************************/
7400 /**************** call for cancel list generation added 05/23/2005 ************/
7401 /******************************************************************************/
7402 /*
7403   -- if l_listheader_rec.generation_type = 'UPDATE' then
7404   if l_listheader_rec.generation_type = 'UPD' then
7405      l_error_position := '<- set enabled flag for gen type UPDATE ';
7406     if g_remote_list <> 'Y' then
7407        update ams_list_entries
7408        set enabled_flag  = 'N'
7409        where newly_updated_flag = 'N'
7410          and list_header_id = l_listheader_rec.list_header_id;
7411       write_to_act_log(sql%rowcount||' entries disabled when generating list in update mode','LIST',g_list_header_id,'HIGH');
7412      IF x_return_status = FND_API.g_ret_sts_error THEN
7413         RAISE FND_API.g_exc_error;
7414      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7415         RAISE FND_API.g_exc_unexpected_error;
7416      END IF;
7417 -- -----------------------
7418    else --  if g_remote_list = 'Y' then
7419       write_to_act_log('Updating list in remote instance.', 'LIST', p_list_header_id,'HIGH');
7420       execute immediate
7421       'BEGIN
7422        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';
7423 '||
7424       ' END;'
7425       using  '1',
7426              l_null,
7427              'T',
7428              l_null,
7429              OUT x_return_status,
7430              OUT x_msg_count,
7431              OUT x_msg_data,
7432              l_listheader_rec.list_header_id,
7433              l_null,
7434              l_null,
7435              OUT l_total_recs,
7436              'UPDATE';
7437         if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7438           write_to_act_log('Error in updating list in remote instance', 'LIST', g_list_header_id,'HIGH');
7439           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7440        end if;
7441      end if;
7442 
7443 -- -----------------------
7444   END IF;
7445 -- --------------------------------
7446 */
7447 if g_remote_list_gen = 'N' then
7448    if l_listheader_rec.generation_type = 'STANDARD' then --R12 applicable only for full refresh mode
7449       write_to_act_log('Identifying duplicate records(based on party id) in the list.','LIST',g_list_header_id,'HIGH');
7450       UPDATE ams_list_entries a
7451          SET a.enabled_flag  = 'N',
7452              a.marked_as_duplicate_flag = 'Y'
7453        WHERE a.list_header_id = p_list_header_id
7454          and a.enabled_flag = 'Y'
7455          AND a.rowid >  (SELECT min(b.rowid)
7456                            from ams_list_entries  b
7457                           where b.list_header_id = p_list_header_id
7458                             and b.party_id = a.party_id
7459                             and b.enabled_flag = 'Y'
7460                    );
7461       write_to_act_log('No of duplicates identified.'||sql%rowcount,'LIST',g_list_header_id,'HIGH');
7462       write_to_act_log('Duplicate records(based on party) identified and marked.','LIST',g_list_header_id,'LOW');
7463 /*    UPDATE ams_list_entries a
7464          SET a.enabled_flag  = 'N',
7465             a.marked_as_duplicate_flag = 'Y'
7466        WHERE a.list_header_id = p_list_header_id
7467          and a.enabled_flag = 'Y'
7468          -- AND a.rowid >  (SELECT min(b.rowid)
7469          AND a.rank >  (SELECT min(b.rank)
7470                    from ams_list_entries  b
7471                    where b.list_header_id = p_list_header_id
7472                      and b.party_id = a.party_id
7473                      and b.enabled_flag = 'Y'
7474                    );*/
7475    end if;
7476 end if;
7477 -- --------------------------------
7478 
7479    open g_initial_count;
7480    fetch g_initial_count into g_no_of_rows_ini_selected;
7481    close g_initial_count;
7482 
7483    if l_listheader_rec.generation_type = 'STANDARD' then --lpo
7484       open c1;
7485       fetch c1 into l_list_rule_id ;
7486       close c1;
7487 
7488       IF (l_list_rule_id <> 0 ) THEN   --Deduplication of the list has been requested.--
7489          write_to_act_log('De Duplication requested for this list', 'LIST', g_list_header_id,'HIGH');
7490          l_error_position := '<- de dupe ->';
7491          if g_remote_list_gen = 'N' then
7492             /* For local list generation */
7493             write_to_act_log('Calling ams_listdedupe_pvt for deduplication.', 'LIST', g_list_header_id,'HIGH');
7494             l_no_of_duplicates := AMS_LISTDEDUPE_PVT.DEDUPE_LIST
7495                            (p_list_header_id               => p_list_header_id,
7496                             p_enable_word_replacement_flag => 'Y',
7497                                   -- l_listheader_rec.enable_word_replacement_flag,
7498                             p_send_to_log    => l_listheader_rec.enable_log_flag,
7499                            p_object_name    => 'AMS_LIST_ENTRIES');
7500             write_to_act_log('Deduplication done for this list.', 'LIST', g_list_header_id,'HIGH');
7501          else
7502             /* For Remote list generation */
7503             write_to_act_log('Call Execute_Remote_Dedupe_List for deduplication in remote instance.', 'LIST', g_list_header_id,'HIGH');
7504             Execute_Remote_Dedupe_List
7505                           (p_list_header_id               => p_list_header_id,
7506                             p_enable_word_replacement_flag => 'Y',
7507                                 --   l_listheader_rec.enable_word_replacement_flag,
7508                             p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
7509                             p_object_name    => 'AMS_LIST_ENTRIES');
7510             write_to_act_log('Deduplication done for this list in remote instance.', 'LIST', g_list_header_id,'LOW');
7511          end if;
7512       end if;
7513    end if;
7514 
7515 
7516    if l_listheader_rec.generation_type in ('STANDARD','INCREMENTAL') then --lpo
7517       if nvl(g_remote_list_gen,'N') = 'N' then
7518          AMS_List_Options_Pvt.apply_size_reduction(p_list_header_id => g_list_header_id ,
7519                                                    p_log_level => g_log_level,
7520                                                    p_msg_tbl   => g_msg_tbl_opt,
7521                                                    x_return_status  => x_return_status,
7522                                                    x_msg_count      => x_msg_count,
7523                                                    x_msg_data       => x_msg_data);
7524          if g_msg_tbl_opt.count > 0 then
7525             for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
7526 	    loop
7527 	       write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
7528                --g_message_table(g_count) := g_msg_tbl_opt(I);
7529                --g_date(g_count) := sysdate;
7530                --g_count   := g_count + 1;
7531             end loop;
7532 	    g_msg_tbl_opt.delete;
7533          end if;
7534       else
7535          write_to_act_log('Calling apply_size_reduction procedure in the remote instance.', 'LIST', g_list_header_id,'LOW');
7536          execute immediate
7537          'BEGIN
7538           AMS_LIST_OPTIONS_PVT.apply_size_reduction'||'@'||g_database_link||'(:1,:2,:3,:4,:5)'||';'||
7539          ' END;'
7540          using g_list_header_id,
7541          'NULL',
7542          out x_return_status,
7543          out x_msg_count,
7544          out x_msg_data;
7545          write_to_act_log('x return status '||x_return_status, 'LIST', g_list_header_id,'LOW');
7546          write_to_act_log('apply_size_reduction procedure executed in the remote instance.', 'LIST', g_list_header_id,'LOW');
7547       end if;
7548       IF (x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
7549          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7550             write_to_act_log('Error in while executing size reduction procedure.', 'LIST', g_list_header_id,'HIGH');
7551          end if;
7552          RAISE FND_API.G_EXC_ERROR;
7553       ELSE
7554          write_to_act_log('Procedure apply_size_reduction executed successfully.', 'LIST', g_list_header_id,'LOW');
7555          IF FND_API.To_Boolean ( p_commit ) THEN
7556             COMMIT;
7557          END IF;
7558       END IF;
7559    end if;
7560 -- end if;
7561 
7562  if g_remote_list = 'Y' then
7563     write_to_act_log('Updating the list header info in the remote instance.', 'LIST', p_list_header_id,'HIGH');
7564     write_to_act_log('Deleting the existing ist header record deleted in remote instance.', 'LIST', p_list_header_id,'LOW');
7565     --execute immediate 'begin Delete from ams_list_headers_all'||'@'||g_database_link||' where list_header_id = :1 ; end;' using p_list_header_id;
7566 
7567 /*********** added by savio for remote bug 3764343 **************************/
7568 
7569 /*    open c10(p_list_header_id);
7570     fetch c10 into c10_rec;
7571     close c10;
7572 
7573    write_to_act_log('Passing list header details to the remote procedure, to insert it there.', 'LIST', p_list_header_id,'HIGH');
7574    execute immediate
7575       'begin
7576          ams_remote_listgen_pkg.remote_insert_list_headers'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17,:18)'||';'||
7577       ' end;'
7578       using
7579     c10_rec.list_header_id,
7580     c10_rec.last_update_date,
7581     c10_rec.last_updated_by,
7582     c10_rec.creation_date,
7583     c10_rec.created_by,
7584     c10_rec.last_update_login,
7585     c10_rec.list_used_by_id,
7586     c10_rec.arc_list_used_by,
7587     c10_rec.list_type,
7588     c10_rec.status_code,
7589     c10_rec.status_date,
7590     c10_rec.generation_type,
7591     c10_rec.owner_user_id,
7592     c10_rec.row_selection_type,
7593     c10_rec.no_of_rows_max_requested,
7594     out x_msg_count,
7595     out x_msg_data,
7596     out x_return_status;
7597 
7598     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7599        write_to_act_log('Error updating list header information in remote instance.', 'LIST', g_list_header_id,'HIGH');
7600        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7601     else
7602        write_to_act_log('List header information updated.', 'LIST', g_list_header_id,'LOW');
7603     end if;*/
7604 
7605 /**********  added by savio for remote bug 3764343 *************************/
7606 
7607 /**********  added by savio for remote bug 3764343 ******************************************/
7608     open c11(p_list_header_id, l_action);
7609     fetch c11 into c11_rec;
7610     close c11;
7611     write_to_act_log('Updating the list selections in the remote instance.', 'LIST', p_list_header_id,'HIGH');
7612     write_to_act_log('Passing the selecions values to remote_insert_list_sel_actions proceudure, to insert it there.', 'LIST', p_list_header_id,'LOW');
7613 
7614     execute immediate
7615       'begin
7616          ams_remote_listgen_pkg.remote_insert_list_sel_actions'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17,:18,:19,:20,:21,:22,:23,:24,:25)'||';'||
7617       ' end;'
7618       using
7619     c11_rec.list_select_action_id,
7620     c11_rec.last_update_date,
7621     c11_rec.last_updated_by,
7622     c11_rec.creation_date,
7623     c11_rec.created_by,
7624     c11_rec.last_update_login,
7625     c11_rec.order_number,
7626     c11_rec.list_action_type,
7627     c11_rec.incl_object_name,
7628     c11_rec.arc_incl_object_from,
7629     c11_rec.rank,
7630     c11_rec.no_of_rows_available,
7631     c11_rec.no_of_rows_requested,
7632     c11_rec.no_of_rows_used,
7633     c11_rec.distribution_pct,
7634     c11_rec.arc_action_used_by,
7635     c11_rec.action_used_by_id,
7636     c11_rec.incl_control_group,
7637     c11_rec.no_of_rows_targeted,
7638     c11_rec.no_of_rows_duplicates,
7639     c11_rec.running_total,
7640     c11_rec.delta,
7641     out x_msg_count,
7642     out x_msg_data,
7643     out x_return_status;
7644 
7645     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7646        write_to_act_log('Error updating list selections in remote instance.', 'LIST', g_list_header_id,'HIGH');
7647        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7648     else
7649        write_to_act_log('List selections updated in remote instance.', 'LIST', g_list_header_id,'LOW');
7650     end if;
7651 
7652 /**********  added by savio for remote bug 3764343 ******************************************/
7653     execute immediate 'begin Update ams_list_select_actions a1  set no_of_rows_requested =
7654             (select  no_of_rows_requested from ams_list_select_actions'||'@'||g_database_link||
7655 	    ' b1 where b1.list_select_action_id = a1.list_select_action_id)
7656 	    where action_used_by_id = :1 and arc_action_used_by = :2; end; ' using p_list_header_id, l_action;
7657     write_to_act_log('No_of_rows_requested in list header table updated with values from remote instance.', 'LIST', p_list_header_id,'LOW');
7658 
7659   end if;
7660 
7661   l_error_position := '<- update list dets ->';
7662 
7663   write_to_act_log('Calling update_list_dets to update list header and selections info.'||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7664   Update_List_Dets(p_list_header_id,x_return_status);
7665 
7666   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7667      write_to_act_log('Error in updating list header/selections info', 'LIST', g_list_header_id,'HIGH');
7668      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7669   elsif nvl(x_return_status,'S') = 'S' then
7670      write_to_act_log('List header and selections info updated.'||x_msg_data , 'LIST', g_list_header_id,'LOW');
7671   end if;
7672 
7673   IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
7674      write_to_act_log('Error after calling update_list_dets procedure', 'LIST', p_list_header_id,'HIGH');
7675      RAISE FND_API.G_EXC_ERROR;
7676   ELSE
7677    IF FND_API.To_Boolean ( p_commit ) THEN
7678      write_to_act_log('Generate_list : Commit', 'LIST', g_list_header_id,'LOW');
7679      COMMIT;
7680    END IF;
7681   END IF;
7682   write_to_act_log('Procedure generate_list executed. List generated successfully.','LIST',g_list_header_id,'HIGH');
7683   -- calling logging program
7684   logger;
7685   --
7686   -- END of API body.
7687   --
7688 /******************************************************************************/
7689 /**************** call for cancel list generation added 05/26/2005 ************/
7690 /******************************************************************************/
7691 --inside generate_list 2
7692 
7693    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
7694                 p_list_header_id => g_list_header_id ,
7695                 p_remote_gen     => g_remote_list    ,
7696                 p_remote_gen_list=> g_remote_list_gen,
7697                 p_database_link  => g_database_link,
7698                 x_msg_count      => x_msg_count ,
7699                 x_msg_data       => x_msg_data ,
7700                 x_return_status  => x_return_status
7701                );
7702 
7703   logger;
7704   -- Standard check of p_commit.
7705 
7706   IF FND_API.To_Boolean ( p_commit ) THEN
7707      COMMIT WORK;
7708   END IF;
7709 
7710   -- Success Message
7711   -- MMSG
7712   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
7713   --THEN
7714   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
7715   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
7716   FND_MSG_PUB.Add;
7717   --END IF;
7718 
7719   x_return_status := FND_API.G_RET_STS_SUCCESS;
7720   --IF (AMS_DEBUG_HIGH_ON) THEN
7721   --THEN
7722   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7723   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.Generate_List: END');
7724   FND_MSG_PUB.Add;
7725   --END IF;
7726       FND_MSG_PUB.count_and_get(
7727             p_encoded => FND_API.g_false,
7728             p_count   => x_msg_count,
7729             p_data    => x_msg_data
7730       );
7731 
7732 EXCEPTION
7733    WHEN FND_API.G_EXC_ERROR THEN
7734   -- calling logging program
7735   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7736 
7737   --
7738         UPDATE ams_list_headers_all
7739         SET    last_generation_success_flag = 'N',
7740                status_code         = 'FAILED',
7741                user_status_id      = 311,
7742                status_date         = sysdate,
7743                last_update_date    = sysdate,
7744                main_gen_end_time   = sysdate
7745         WHERE  list_header_id      = p_list_header_id;
7746      logger;
7747      -- Check if reset of the status is required
7748      x_return_status := FND_API.G_RET_STS_ERROR ;
7749       FND_MSG_PUB.count_and_get(
7750             p_encoded => FND_API.g_false,
7751             p_count   => x_msg_count,
7752             p_data    => x_msg_data);
7753 
7754   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7755   -- calling logging program
7756   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7757   --
7758         UPDATE ams_list_headers_all
7759         SET    last_generation_success_flag = 'N',
7760                status_code         = 'FAILED',
7761                user_status_id      = 311,
7762                last_update_date    = sysdate,
7763                status_date         = sysdate,
7764                main_gen_end_time   = sysdate
7765         WHERE  list_header_id      = p_list_header_id;
7766      logger;
7767      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7768      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
7769      FND_MSG_PUB.Add;
7770      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7771       FND_MSG_PUB.count_and_get(
7772             p_encoded => FND_API.g_false,
7773             p_count   => x_msg_count,
7774             p_data    => x_msg_data);
7775 ---------------Cancel List Gen Exception Begin --------------
7776   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
7777     write_to_act_log('In GENERATE_LIST: User cancel list gen action detected. ' ,'LIST',g_list_header_id,'HIGH');
7778     write_to_act_log('List Generation Stopped successfully.','LIST',g_list_header_id,'HIGH');
7779 
7780   UPDATE ams_list_headers_all
7781      SET    last_generation_success_flag = 'N',
7782                status_code         = 'DRAFT',
7783                user_status_id      = 300,
7784                last_update_date    = sysdate,
7785                status_date         = sysdate,
7786                main_gen_end_time   = sysdate
7787      WHERE  list_header_id         = p_list_header_id;
7788   logger;
7789 
7790   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
7791   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
7792   FND_MSG_PUB.Add;
7793 
7794   x_return_status := FND_API.G_RET_STS_SUCCESS;
7795 
7796   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7797   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.Generate_List: END');
7798   FND_MSG_PUB.Add;
7799 
7800   FND_MSG_PUB.count_and_get(
7801             p_encoded => FND_API.g_false,
7802             p_count   => x_msg_count,
7803             p_data    => x_msg_data
7804       );
7805 ---------------Cancel List Gen Exception End --------------
7806 
7807   WHEN OTHERS THEN
7808   -- calling logging program
7809   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7810   --
7811         UPDATE ams_list_headers_all
7812         SET    last_generation_success_flag = 'N',
7813                status_code                  = 'FAILED',
7814                user_status_id               = 311,
7815                last_update_date             = sysdate,
7816                status_date                  = sysdate,
7817                main_gen_end_time            = sysdate
7818         WHERE  list_header_id               = p_list_header_id;
7819       logger;
7820      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7821      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
7822      FND_MSG_PUB.Add;
7823      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7824       FND_MSG_PUB.count_and_get(
7825             p_encoded => FND_API.g_false,
7826             p_count   => x_msg_count,
7827             p_data    => x_msg_data);
7828 END GENERATE_LIST;
7829 
7830 PROCEDURE create_list
7831 ( p_api_version            IN    NUMBER,
7832   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
7833   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
7834   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
7835   p_list_name              in    varchar2,
7836   p_list_type              in    varchar2,
7837   p_owner_user_id          in    number,
7838   p_sql_string             in  OUT NOCOPY varchar2,
7839   p_primary_key            in    varchar2,
7840   p_source_object_name     in    varchar2,
7841   p_master_type            in    varchar2,
7842   x_return_status          OUT NOCOPY   VARCHAR2,
7843   x_msg_count              OUT NOCOPY   NUMBER,
7844   x_msg_data               OUT NOCOPY   VARCHAR2,
7845   x_list_header_id         OUT NOCOPY   NUMBER  ) is
7846 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
7847 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
7848 l_api_version       number         := 1.0;
7849 l_api_name          constant varchar2(30) := 'Create_List';
7850 l_list_query_rec    AMS_List_Query_PVT.list_query_rec_type ;
7851 l_list_query_id     number ;
7852 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
7853 l_action_id         number;
7854 cursor c_mapping_types(p_master_type varchar2) is
7855 SELECT list_source_type_id
7856 FROM   ams_list_src_types a
7857 WHERE a.source_type_code = p_master_type
7858   AND a.master_source_type_flag = 'Y';
7859 cursor c_mapping_subtypes(p_master_type_id
7860                           ams_list_src_type_assocs.master_source_type_id%type)is
7861 select ','||''''||source_type_code||''''
7862 from   ams_list_src_types a,
7863        ams_list_src_type_assocs b
7864 where  b.master_source_type_id = p_master_type_id
7865   and  b.sub_source_type_id  = a.list_source_type_id;
7866 
7867 l_master_type_id number;
7868 l_source_type_code varchar2(30);
7869 l_select_string varchar2(2000) := 'SELECT ' ||''''|| p_master_type || '''';
7870 BEGIN
7871   open c_mapping_types(p_master_type )  ;
7872   fetch c_mapping_types into l_master_type_id;
7873   close c_mapping_types;
7874 /*
7875   open c_mapping_subtypes(l_master_type_id )  ;
7876   loop
7877   fetch c_mapping_subtypes
7878   into l_source_type_code;
7879   exit when c_mapping_subtypes%notfound;
7880   l_select_string := l_select_string || l_source_type_code;
7881   end loop;
7882   close c_mapping_subtypes;
7883 */
7884 
7885   p_sql_string := l_select_string ||','|| substrb(p_sql_string,instr(upper(p_sql_string),'SELECT',1)+6);
7886   x_return_status := FND_API.G_RET_STS_SUCCESS;
7887   -- Standard call to check for call compatibility.
7888   IF NOT FND_API.Compatible_API_Call ( l_api_version,
7889                                        p_api_version,
7890                                        l_api_name,
7891                                        G_PKG_NAME) THEN
7892      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7893   END IF;
7894 
7895 
7896   -- Initialize message list IF p_init_msg_list is set to TRUE.
7897   IF FND_API.to_Boolean( p_init_msg_list ) THEN
7898      FND_MSG_PUB.initialize;
7899   END IF;
7900 
7901   -- Debug Message
7902   IF (AMS_DEBUG_HIGH_ON) THEN
7903      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7904      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
7905      FND_MSG_PUB.Add;
7906   END IF;
7907 
7908   --  Initialize API return status to success
7909   x_return_status := FND_API.G_RET_STS_SUCCESS;
7910   -- Perform the database operation
7911 
7912   -- ams_listheader_pvt.init_listheader_rec(l_list_header_rec);
7913   l_list_header_rec.list_name :=  p_list_name;
7914   l_list_header_rec.list_type :=  p_list_type;
7915   l_list_header_rec.owner_user_id :=  p_owner_user_id;
7916 l_list_header_rec.list_source_type := p_master_type            ;
7917 if l_list_header_rec.view_application_id is null then
7918 l_list_header_rec.view_application_id := 530;
7919 end if;
7920   AMS_ListHeader_PVT.Create_Listheader
7921   ( p_api_version           => 1.0,
7922   p_init_msg_list           => l_init_msg_list,
7923   p_commit                  => p_commit,
7924   p_validation_level        => p_validation_level ,
7925   x_return_status           => x_return_status,
7926   x_msg_count               => x_msg_count,
7927   x_msg_data                => x_msg_data,
7928   p_listheader_rec          => l_list_header_rec,
7929   x_listheader_id           => x_list_header_id
7930   );
7931 
7932   if x_return_status <> FND_API.g_ret_sts_success  THEN
7933      RAISE FND_API.G_EXC_ERROR;
7934   end if;
7935 
7936      FND_MESSAGE.set_name('AMS', 'bfore list query');
7937      FND_MSG_PUB.Add;
7938    l_list_query_rec.name        :=  p_list_name || x_list_header_id;
7939    l_list_query_rec.sql_string  :=  p_sql_string ;
7940    l_list_query_rec.primary_key  :=  p_primary_key ;
7941    l_list_query_rec.type  :=  p_master_type ;
7942    l_list_query_rec.source_object_name  :=  p_source_object_name ;
7943    AMS_List_Query_PVT.Create_List_Query(
7944        p_api_version_number  => 1.0,
7945        p_init_msg_list       => l_init_msg_list,
7946        p_commit              => p_commit,
7947        p_validation_level    => p_validation_level,
7948        x_return_status       => x_return_status,
7949        x_msg_count           => x_msg_count,
7950        x_msg_data            => x_msg_data,
7951        p_list_query_rec      => l_list_query_rec ,
7952        x_list_query_id       => l_list_query_id
7953      );
7954   if x_return_status <> FND_API.g_ret_sts_success  THEN
7955      RAISE FND_API.G_EXC_ERROR;
7956   end if;
7957 
7958   l_action_rec.arc_action_used_by := 'LIST';
7959   l_action_rec.action_used_by_id := x_list_header_id ;
7960   l_action_rec.order_number := 1 ;
7961   l_action_rec.list_action_type := 'INCLUDE';
7962   l_action_rec.arc_incl_object_from := 'SQL';
7963   l_action_rec.incl_object_id := l_list_query_id;
7964   l_action_rec.rank := 1;
7965   AMS_ListAction_PVT.Create_ListAction
7966   ( p_api_version           => 1.0,
7967     p_init_msg_list         => l_init_msg_list,
7968     p_commit                => p_commit,
7969     p_validation_level      => p_validation_level,
7970     x_return_status         => x_return_status,
7971     x_msg_count             => x_msg_count,
7972     x_msg_data              => x_msg_data,
7973     p_action_rec            => l_action_rec,
7974     x_action_id             => l_action_id
7975     ) ;
7976   if x_return_status <> FND_API.g_ret_sts_success  THEN
7977      RAISE FND_API.G_EXC_ERROR;
7978   end if;
7979   --For bug 4351391
7980     ams_list_wf.StartListBizEventProcess(p_list_header_id  => x_list_header_id);
7981 
7982   /*
7983   GENERATE_LIST
7984   ( p_api_version           => 1.0,
7985     p_init_msg_list         => l_init_msg_list,
7986     p_commit                => p_commit,
7987     p_validation_level      => p_validation_level,
7988     p_list_header_id        => x_list_header_id,
7989     x_return_status         => x_return_status,
7990     x_msg_count             => x_msg_count,
7991     x_msg_data              => x_msg_data);
7992 
7993   if x_return_status <> FND_API.g_ret_sts_success  THEN
7994      RAISE FND_API.G_EXC_ERROR;
7995   end if;
7996   -- END of API body.
7997 
7998   if x_return_status <> FND_API.g_ret_sts_success  THEN
7999      RAISE FND_API.G_EXC_ERROR;
8000   end if;
8001   -- END of API body.
8002   --
8003   */
8004   -- Standard check of p_commit.
8005 
8006   IF FND_API.To_Boolean ( p_commit ) THEN
8007      COMMIT WORK;
8008   END IF;
8009 
8010   -- Success Message
8011   -- MMSG
8012   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8013   --THEN
8014   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8015   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8016   FND_MSG_PUB.Add;
8017   --END IF;
8018 
8019   --IF (AMS_DEBUG_HIGH_ON) THEN
8020   --THEN
8021   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8022   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8023   FND_MSG_PUB.Add;
8024   --END IF;
8025       FND_MSG_PUB.count_and_get(
8026             p_encoded => FND_API.g_false,
8027             p_count   => x_msg_count,
8028             p_data    => x_msg_data
8029       );
8030 
8031 EXCEPTION
8032    WHEN FND_API.G_EXC_ERROR THEN
8033      -- Check if reset of the status is required
8034      x_return_status := FND_API.G_RET_STS_ERROR ;
8035       FND_MSG_PUB.count_and_get(
8036             p_encoded => FND_API.g_false,
8037             p_count   => x_msg_count,
8038             p_data    => x_msg_data);
8039 
8040   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8041      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8042       FND_MSG_PUB.count_and_get(
8043             p_encoded => FND_API.g_false,
8044             p_count   => x_msg_count,
8045             p_data    => x_msg_data);
8046 
8047   WHEN OTHERS THEN
8048      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8049       FND_MSG_PUB.count_and_get(
8050             p_encoded => FND_API.g_false,
8051             p_count   => x_msg_count,
8052             p_data    => x_msg_data);
8053 END CREATE_LIST;
8054 
8055 PROCEDURE create_import_list
8056 ( p_api_version            IN    NUMBER,
8057   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8058   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8059   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8060   p_owner_user_id          in    number,
8061   p_imp_list_header_id     in    number,
8062   x_return_status          OUT NOCOPY   VARCHAR2,
8063   x_msg_count              OUT NOCOPY   NUMBER,
8064   x_msg_data               OUT NOCOPY   VARCHAR2,
8065   x_list_header_id         OUT NOCOPY   NUMBER  ,
8066   p_list_name              in    VARCHAR2 ) is
8067 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8068 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8069 l_api_version       number         := 1.0;
8070 l_api_name          constant varchar2(30) := 'Create_import_List';
8071 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8072 l_action_id         number;
8073 l_import_list_name  varchar2(200);
8074 cursor c_chk_name is
8075 select  'x'
8076 from ams_list_headers_vl
8077 where list_name = p_list_name ;
8078 cursor c_get_source_type
8079 is select  decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST'),
8080       name
8081 from ams_imp_list_headers_vl
8082 where  import_list_header_id = p_imp_list_header_id     ;
8083 l_source_type varchar2(100);
8084 l_var  varchar2(1);
8085 BEGIN
8086  open c_get_source_type ;
8087  fetch c_get_source_type into l_source_type ,l_import_list_name   ;
8088  close c_get_source_type ;
8089  if l_source_type is null then
8090    l_source_type := 'PERSON_LIST';
8091  end if;
8092   if p_list_name is not null then
8093      open c_chk_name ;
8094      fetch c_chk_name into l_var   ;
8095      close c_chk_name ;
8096   else
8097      l_var := 'x';
8098   end if;
8099   if  l_var is not null  then
8100       select l_import_list_name|| ' -:'|| to_char(sysdate,'DD-MON-YY HH:MM:SS')
8101       into l_import_list_name
8102       from ams_imp_list_headers_vl
8103       where import_list_header_id = p_imp_list_header_id     ;
8104  else
8105     l_import_list_name := p_list_name ;
8106  end if;
8107 
8108   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8109                                        p_api_version,
8110                                        l_api_name,
8111                                        G_PKG_NAME) THEN
8112      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8113   END IF;
8114 
8115 
8116   -- Initialize message list IF p_init_msg_list is set to TRUE.
8117   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8118      FND_MSG_PUB.initialize;
8119   END IF;
8120 
8121   -- Debug Message
8122   IF (AMS_DEBUG_HIGH_ON) THEN
8123      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8124      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8125      FND_MSG_PUB.Add;
8126   END IF;
8127   --  Initialize API return status to success
8128   x_return_status := FND_API.G_RET_STS_SUCCESS;
8129   -- Perform the database operation
8130 
8131   -- ams_listheader_pvt.init_listheader_rec(l_list_header_rec);
8132   l_list_header_rec.list_name :=  l_import_list_name  ;
8133   l_list_header_rec.list_type :=  'STANDARD';
8134   l_list_header_rec.list_source_type :=  l_source_type;
8135   l_list_header_rec.owner_user_id :=  p_owner_user_id;
8136   l_list_header_rec.view_application_id :=  530;
8137 --  l_list_header_rec.purpose_code :=  'GENERAL';
8138 
8139   AMS_ListHeader_PVT.Create_Listheader
8140   ( p_api_version           => 1.0,
8141   p_init_msg_list           => l_init_msg_list,
8142   p_commit                  => p_commit,
8143   p_validation_level        => p_validation_level ,
8144   x_return_status           => x_return_status,
8145   x_msg_count               => x_msg_count,
8146   x_msg_data                => x_msg_data,
8147   p_listheader_rec          => l_list_header_rec,
8148   x_listheader_id           => x_list_header_id
8149   );
8150 
8151   if x_return_status <> FND_API.g_ret_sts_success  THEN
8152      RAISE FND_API.G_EXC_ERROR;
8153   end if;
8154 
8155   l_action_rec.arc_action_used_by := 'LIST';
8156   l_action_rec.action_used_by_id := x_list_header_id ;
8157   l_action_rec.order_number := 1 ;
8158   l_action_rec.list_action_type := 'INCLUDE';
8159   l_action_rec.arc_incl_object_from := 'IMPH';
8160   l_action_rec.incl_object_id := p_imp_list_header_id     ;
8161   l_action_rec.rank := 1;
8162   AMS_ListAction_PVT.Create_ListAction
8163   ( p_api_version           => 1.0,
8164     p_init_msg_list         => l_init_msg_list,
8165     p_commit                => p_commit,
8166     p_validation_level      => p_validation_level,
8167     x_return_status         => x_return_status,
8168     x_msg_count             => x_msg_count,
8169     x_msg_data              => x_msg_data,
8170     p_action_rec            => l_action_rec,
8171     x_action_id             => l_action_id
8172     ) ;
8173      FND_MESSAGE.set_name('AMS','after list action->'|| l_action_id|| '<-');
8174      FND_MSG_PUB.Add;
8175   if x_return_status <> FND_API.g_ret_sts_success  THEN
8176      RAISE FND_API.G_EXC_ERROR;
8177   end if;
8178 
8179   GENERATE_LIST
8180   ( p_api_version           => 1.0,
8181     p_init_msg_list         => l_init_msg_list,
8182     p_commit                => p_commit,
8183     p_validation_level      => p_validation_level,
8184     p_list_header_id        => x_list_header_id,
8185     x_return_status         => x_return_status,
8186     x_msg_count             => x_msg_count,
8187     x_msg_data              => x_msg_data);
8188 
8189   if x_return_status <> FND_API.g_ret_sts_success  THEN
8190      RAISE FND_API.G_EXC_ERROR;
8191   end if;
8192   -- END of API body.
8193   --
8194 
8195   -- Standard check of p_commit.
8196 
8197   IF FND_API.To_Boolean ( p_commit ) THEN
8198      COMMIT WORK;
8199   END IF;
8200 
8201   -- Success Message
8202   -- MMSG
8203   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8204   --THEN
8205   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8206   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8207   FND_MSG_PUB.Add;
8208   --END IF;
8209 
8210 
8211   --IF (AMS_DEBUG_HIGH_ON) THEN
8212   --THEN
8213   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8214   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8215   FND_MSG_PUB.Add;
8216   --END IF;
8217       FND_MSG_PUB.count_and_get(
8218             p_encoded => FND_API.g_false,
8219             p_count   => x_msg_count,
8220             p_data    => x_msg_data
8221       );
8222 
8223 EXCEPTION
8224    WHEN FND_API.G_EXC_ERROR THEN
8225      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8226      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8227      FND_MSG_PUB.Add;
8228      -- Check if reset of the status is required
8229      x_return_status := FND_API.G_RET_STS_ERROR ;
8230       FND_MSG_PUB.count_and_get(
8231             p_encoded => FND_API.g_false,
8232             p_count   => x_msg_count,
8233             p_data    => x_msg_data);
8234 
8235   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8236      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8237      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8238      FND_MSG_PUB.Add;
8239      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8240       FND_MSG_PUB.count_and_get(
8241             p_encoded => FND_API.g_false,
8242             p_count   => x_msg_count,
8243             p_data    => x_msg_data);
8244 
8245   WHEN OTHERS THEN
8246      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8247      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8248      FND_MSG_PUB.Add;
8249      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8250       FND_MSG_PUB.count_and_get(
8251             p_encoded => FND_API.g_false,
8252             p_count   => x_msg_count,
8253             p_data    => x_msg_data);
8254 END CREATE_import_LIST;
8255 
8256 PROCEDURE create_list_from_query
8257 ( p_api_version            IN    NUMBER,
8258   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8259   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8260   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8261   p_list_name              in    varchar2,
8262   p_list_type              in    varchar2,
8263   p_owner_user_id          in    number,
8264   p_list_header_id         in    number,
8265   p_sql_string_tbl         in    AMS_List_Query_PVT.sql_string_tbl      ,
8266   p_primary_key            in    varchar2,
8267   p_source_object_name     in    varchar2,
8268   p_master_type            in    varchar2,
8269   x_return_status          OUT NOCOPY   VARCHAR2,
8270   x_msg_count              OUT NOCOPY   NUMBER,
8271   x_msg_data               OUT NOCOPY   VARCHAR2
8272   ) is
8273 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8274 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8275 l_api_version       number         := 1.0;
8276 l_api_name          constant varchar2(30) := 'Create_List';
8277 l_list_query_rec_tbl    AMS_List_Query_PVT.list_query_rec_type_tbl ;
8278 l_list_query_id     number ;
8279 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8280 l_action_id         number;
8281 
8282 l_master_type_id number;
8283 l_source_type_code varchar2(30);
8284 BEGIN
8285   x_return_status := FND_API.G_RET_STS_SUCCESS;
8286   -- Standard call to check for call compatibility.
8287   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8288                                        p_api_version,
8289                                        l_api_name,
8290                                        G_PKG_NAME) THEN
8291      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8292   END IF;
8293 
8294 
8295   -- Initialize message list IF p_init_msg_list is set to TRUE.
8296   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8297      FND_MSG_PUB.initialize;
8298   END IF;
8299 
8300   -- Debug Message
8301   IF (AMS_DEBUG_HIGH_ON) THEN
8302      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8303      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8304      FND_MSG_PUB.Add;
8305   END IF;
8306 
8307   --  Initialize API return status to success
8308   x_return_status := FND_API.G_RET_STS_SUCCESS;
8309   -- Perform the database operation
8310 
8311    l_list_query_rec_tbl.name        :=  p_list_name ||'_'|| p_list_header_id;
8312   -- l_list_query_rec_tbl.sql_string  :=  p_sql_string ;
8313    l_list_query_rec_tbl.primary_key  :=  p_primary_key ;
8314    l_list_query_rec_tbl.type  :=  p_master_type ;
8315    l_list_query_rec_tbl.source_object_name  :=  p_source_object_name ;
8316    AMS_List_Query_PVT.Create_List_Query(
8317        p_api_version_number  => 1.0,
8318        p_init_msg_list       => l_init_msg_list,
8319        p_commit              => p_commit,
8320        p_validation_level    => p_validation_level,
8321        x_return_status       => x_return_status,
8322        x_msg_count           => x_msg_count,
8323        x_msg_data            => x_msg_data,
8324        p_list_query_rec_tbl    => l_list_query_rec_tbl ,
8325        p_sql_string_tbl       => p_sql_string_tbl         ,
8326        x_parent_list_query_id   => l_list_query_id
8327      );
8328 
8329 
8330   if x_return_status <> FND_API.g_ret_sts_success  THEN
8331      RAISE FND_API.G_EXC_ERROR;
8332   end if;
8333 
8334   l_action_rec.arc_action_used_by := 'LIST';
8335   l_action_rec.action_used_by_id := p_list_header_id ;
8336   l_action_rec.order_number := 1 ;
8337   l_action_rec.list_action_type := 'INCLUDE';
8338   l_action_rec.arc_incl_object_from := 'SQL';
8339   l_action_rec.incl_object_id := l_list_query_id;
8340   l_action_rec.rank := 1;
8341   l_action_rec.order_number := 1 ;
8342   AMS_ListAction_PVT.Create_ListAction
8343   ( p_api_version           => 1.0,
8344     p_init_msg_list         => l_init_msg_list,
8345     p_commit                => p_commit,
8346     p_validation_level      => p_validation_level,
8347     x_return_status         => x_return_status,
8348     x_msg_count             => x_msg_count,
8349     x_msg_data              => x_msg_data,
8350     p_action_rec            => l_action_rec,
8351     x_action_id             => l_action_id
8352     ) ;
8353   if x_return_status <> FND_API.g_ret_sts_success  THEN
8354      RAISE FND_API.G_EXC_ERROR;
8355   end if;
8356 
8357   GENERATE_LIST
8358   ( p_api_version           => 1.0,
8359     p_init_msg_list         => l_init_msg_list,
8360     p_commit                => p_commit,
8361     p_validation_level      => p_validation_level,
8362     p_list_header_id        => p_list_header_id,
8363     x_return_status         => x_return_status,
8364     x_msg_count             => x_msg_count,
8365     x_msg_data              => x_msg_data);
8366 
8367   if x_return_status <> FND_API.g_ret_sts_success  THEN
8368      RAISE FND_API.G_EXC_ERROR;
8369   end if;
8370   -- END of API body.
8371   --
8372 
8373   -- Standard check of p_commit.
8374 
8375   IF FND_API.To_Boolean ( p_commit ) THEN
8376      COMMIT WORK;
8377   END IF;
8378 
8379   -- Success Message
8380   -- MMSG
8381   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8382   --THEN
8383   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8384   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8385   FND_MSG_PUB.Add;
8386   --END IF;
8387 
8388 
8389   --IF (AMS_DEBUG_HIGH_ON) THEN
8390   --THEN
8391   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8392   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8393   FND_MSG_PUB.Add;
8394   --END IF;
8395       FND_MSG_PUB.count_and_get(
8396             p_encoded => FND_API.g_false,
8397             p_count   => x_msg_count,
8398             p_data    => x_msg_data
8399       );
8400 
8401 EXCEPTION
8402    WHEN FND_API.G_EXC_ERROR THEN
8403      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8404      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8405      FND_MSG_PUB.Add;
8406      -- Check if reset of the status is required
8407      x_return_status := FND_API.G_RET_STS_ERROR ;
8408       FND_MSG_PUB.count_and_get(
8409             p_encoded => FND_API.g_false,
8410             p_count   => x_msg_count,
8411             p_data    => x_msg_data);
8412 
8413   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8414      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8415      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8416      FND_MSG_PUB.Add;
8417      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8418       FND_MSG_PUB.count_and_get(
8419             p_encoded => FND_API.g_false,
8420             p_count   => x_msg_count,
8421             p_data    => x_msg_data);
8422 
8423   WHEN OTHERS THEN
8424      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8425      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8426      FND_MSG_PUB.Add;
8427      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8428       FND_MSG_PUB.count_and_get(
8429             p_encoded => FND_API.g_false,
8430             p_count   => x_msg_count,
8431             p_data    => x_msg_data);
8432 END CREATE_LIST_FROM_QUERY ;
8433 
8434 PROCEDURE create_list_from_query
8435 ( p_api_version            IN    NUMBER,
8436   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8437   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8438   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8439   p_list_name              in    varchar2,
8440   p_list_type              in    varchar2,
8441   p_owner_user_id          in    number,
8442   p_list_header_id         in    number,
8443   p_sql_string_tbl         in    AMS_List_Query_PVT.sql_string_tbl      ,
8444   p_primary_key            in    varchar2,
8445   p_source_object_name     in    varchar2,
8446   p_master_type            in    varchar2,
8447   p_query_param            in    AMS_List_Query_PVT.sql_string_tbl      ,
8448   x_return_status          OUT NOCOPY   VARCHAR2,
8449   x_msg_count              OUT NOCOPY   NUMBER,
8450   x_msg_data               OUT NOCOPY   VARCHAR2
8451   ) is
8452 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8453 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8454 l_api_version       number         := 1.0;
8455 l_api_name          constant varchar2(30) := 'Create_List';
8456 l_list_query_rec_tbl    AMS_List_Query_PVT.list_query_rec_type_tbl ;
8457 l_list_query_id     number ;
8458 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8459 l_action_id         number;
8460 
8461 l_master_type_id number;
8462 l_source_type_code varchar2(30);
8463 BEGIN
8464   x_return_status := FND_API.G_RET_STS_SUCCESS;
8465   -- Standard call to check for call compatibility.
8466   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8467                                        p_api_version,
8468                                        l_api_name,
8469                                        G_PKG_NAME) THEN
8470      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8471   END IF;
8472 
8473 
8474   -- Initialize message list IF p_init_msg_list is set to TRUE.
8475   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8476      FND_MSG_PUB.initialize;
8477   END IF;
8478 
8479   -- Debug Message
8480   IF (AMS_DEBUG_HIGH_ON) THEN
8481      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8482      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8483      FND_MSG_PUB.Add;
8484   END IF;
8485 
8486   --  Initialize API return status to success
8487   x_return_status := FND_API.G_RET_STS_SUCCESS;
8488   -- Perform the database operation
8489 
8490    l_list_query_rec_tbl.name        :=  p_list_name ||'_'|| p_list_header_id;
8491   -- l_list_query_rec_tbl.sql_string  :=  p_sql_string ;
8492    l_list_query_rec_tbl.primary_key  :=  p_primary_key ;
8493    l_list_query_rec_tbl.type  :=  p_master_type ;
8494    l_list_query_rec_tbl.source_object_name  :=  p_source_object_name ;
8495 
8496    AMS_List_Query_PVT.Create_List_Query(
8497        p_api_version_number  => 1.0,
8498        p_init_msg_list       => l_init_msg_list,
8499        p_commit              => p_commit,
8500        p_validation_level    => p_validation_level,
8501        x_return_status       => x_return_status,
8502        x_msg_count           => x_msg_count,
8503        x_msg_data            => x_msg_data,
8504        p_list_query_rec_tbl    => l_list_query_rec_tbl ,
8505        p_sql_string_tbl       => p_sql_string_tbl         ,
8506        p_query_param          => p_query_param          ,
8507        x_parent_list_query_id   => l_list_query_id
8508      );
8509 
8510 
8511   if x_return_status <> FND_API.g_ret_sts_success  THEN
8512      RAISE FND_API.G_EXC_ERROR;
8513   end if;
8514 
8515   l_action_rec.arc_action_used_by := 'LIST';
8516   l_action_rec.action_used_by_id := p_list_header_id ;
8517   l_action_rec.order_number := 1 ;
8518   l_action_rec.list_action_type := 'INCLUDE';
8519   l_action_rec.arc_incl_object_from := 'SQL';
8520   l_action_rec.incl_object_id := l_list_query_id;
8521   l_action_rec.rank := 1;
8522   l_action_rec.order_number := 1 ;
8523   AMS_ListAction_PVT.Create_ListAction
8524   ( p_api_version           => 1.0,
8525     p_init_msg_list         => l_init_msg_list,
8526     p_commit                => p_commit,
8527     p_validation_level      => p_validation_level,
8528     x_return_status         => x_return_status,
8529     x_msg_count             => x_msg_count,
8530     x_msg_data              => x_msg_data,
8531     p_action_rec            => l_action_rec,
8532     x_action_id             => l_action_id
8533     ) ;
8534   if x_return_status <> FND_API.g_ret_sts_success  THEN
8535      RAISE FND_API.G_EXC_ERROR;
8536   end if;
8537 
8538   GENERATE_LIST
8539   ( p_api_version           => 1.0,
8540     p_init_msg_list         => l_init_msg_list,
8541     p_commit                => p_commit,
8542     p_validation_level      => p_validation_level,
8543     p_list_header_id        => p_list_header_id,
8544     x_return_status         => x_return_status,
8545     x_msg_count             => x_msg_count,
8546     x_msg_data              => x_msg_data);
8547 
8548   if x_return_status <> FND_API.g_ret_sts_success  THEN
8549      RAISE FND_API.G_EXC_ERROR;
8550   end if;
8551   -- END of API body.
8552   --
8553 
8554   -- Standard check of p_commit.
8555 
8556   IF FND_API.To_Boolean ( p_commit ) THEN
8557      COMMIT WORK;
8558   END IF;
8559 
8560   -- Success Message
8561   -- MMSG
8562   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8563   --THEN
8564   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8565   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8566   FND_MSG_PUB.Add;
8567   --END IF;
8568 
8569 
8570   --IF (AMS_DEBUG_HIGH_ON) THEN
8571   --THEN
8572   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8573   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8574   FND_MSG_PUB.Add;
8575   --END IF;
8576       FND_MSG_PUB.count_and_get(
8577             p_encoded => FND_API.g_false,
8578             p_count   => x_msg_count,
8579             p_data    => x_msg_data
8580       );
8581 
8582 EXCEPTION
8583    WHEN FND_API.G_EXC_ERROR THEN
8584      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8585      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8586      FND_MSG_PUB.Add;
8587      -- Check if reset of the status is required
8588      x_return_status := FND_API.G_RET_STS_ERROR ;
8589       FND_MSG_PUB.count_and_get(
8590             p_encoded => FND_API.g_false,
8591             p_count   => x_msg_count,
8592             p_data    => x_msg_data);
8593 
8594   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8595      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8596      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8597      FND_MSG_PUB.Add;
8598      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8599       FND_MSG_PUB.count_and_get(
8600             p_encoded => FND_API.g_false,
8601             p_count   => x_msg_count,
8602             p_data    => x_msg_data);
8603 
8604   WHEN OTHERS THEN
8605      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8606      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8607      FND_MSG_PUB.Add;
8608      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8609       FND_MSG_PUB.count_and_get(
8610             p_encoded => FND_API.g_false,
8611             p_count   => x_msg_count,
8612             p_data    => x_msg_data);
8613 END CREATE_LIST_FROM_QUERY ;
8614 -- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
8615 
8616 PROCEDURE process_tar_actions
8617              (p_action_used_by_id  in  number,
8618               p_action_used_by     in  varchar2  ,-- DEFAULT 'LIST',
8619               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
8620               x_return_status      OUT NOCOPY VARCHAR2,
8621               x_msg_count          OUT NOCOPY NUMBER,
8622               x_msg_data           OUT NOCOPY VARCHAR2) IS
8623 
8624   -- AMS_LIST_SELECT_ACTIONS Record for init record and complete record
8625   l_tmp_action_rec             ams_listaction_pvt.action_rec_type;
8626   p_action_rec                 ams_listaction_pvt.action_rec_type;
8627   l_list_select_action_id      number;
8628 
8629   ----------------------------------------------------------------------------
8630   -- Cursor definition to select list_select_action_id.Will be used in loop to
8631   -- Process each cursor record according to order specified by the user
8632   ----------------------------------------------------------------------------
8633   CURSOR c_action_dets is
8634   SELECT a.list_act_type, a.list_used_by,
8635          a.list_action_type, a.order_number,
8636          a.list_header_id  ,
8637          -- a.act_list_header_id,
8638 	 c.list_select_action_id act_list_header_id,
8639          c.distribution_pct distribution_pct,
8640 	 c.arc_incl_object_from arc_incl_object_from
8641     FROM ams_act_lists a ,ams_act_lists b, ams_list_select_actions c
8642    WHERE a.list_used_by_id   = b.list_used_by_id
8643      AND a.list_used_by =   b.list_used_by
8644      AND b.list_header_id =  p_action_used_by_id
8645      AND b.list_act_type  =   'TARGET'
8646      and a.list_act_type <> 'TARGET'
8647 	 and b.list_header_id = c.action_used_by_id
8648 	 and a.order_number = c.order_number
8649    ORDER by c.order_number;
8650 
8651 l_action_dets_rec  c_action_dets%ROWTYPE;
8652 
8653   TYPE big_tbl_type is table of VARCHAR2(32767) index by BINARY_INTEGER;
8654   l_std_sql VARCHAR2(32767);
8655   l_include_sql VARCHAR2(32767);
8656   l_include_count number:=0;
8657   l_final_big_sql VARCHAR2(32767);
8658   l_include_sql_tbl  big_tbl_type ;
8659   l_std_sql_tbl  big_tbl_type ;
8660   l_join_string   varchar2(50);
8661 l_no_of_chunks            number;
8662 l_const_sql varchar2(4000) ;
8663   TYPE char_tbl_type is table of VARCHAR2(100) index by BINARY_INTEGER;
8664   TYPE num_tbl_type is table of number index by BINARY_INTEGER;
8665   l_rank_tbl      char_tbl_type;
8666   l_rank_num_tbl      num_tbl_type;
8667 l_sorted   number;
8668 l_update_sql  VARCHAR2(32767);
8669 l_list_header_id number ;
8670 cursor c1 is
8671 select generation_type
8672 from ams_list_headers_all
8673 where list_header_id = l_list_header_id;
8674 l_generation_type varchar2(60);
8675 l_PARAMETERIZED_FLAG  varchar2(1) := 'N';
8676 TYPE table_char  IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
8677 l_table_char table_char;
8678 
8679 cursor c_query(l_query_id number) is select
8680 nvl(PARAMETERIZED_FLAG ,'N')
8681 from ams_list_queries_all
8682 where  list_query_id = l_query_id ;
8683 
8684 cursor c_param_values(l_query_id in number) is
8685 select PARAMETER_ORDER, PARAMETER_VALUE,parameter_name
8686 from ams_list_queries_param
8687 where list_query_id = l_query_id
8688 order by PARAMETER_ORDER;
8689 
8690 l_string VARCHAR2(32767);
8691 l1 varchar2(2000);
8692 l_remote_update_sql  VARCHAR2(32767);
8693 l_null          number;
8694 l_total_recs    number;
8695 l_query_templ_flag   varchar2(1) ;
8696 
8697 l_temp_sql   varchar2(32767);
8698 l_dist_pct_tbl        num_tbl_type;
8699 l_list_select_id      num_tbl_type;
8700 l_incl_object_type    char_tbl_type;
8701 
8702 cursor c_query_temp_type is
8703 select 'Y'
8704 from ams_list_headers_vl  a ,
8705      ams_query_template_all b
8706 where a.list_header_id = g_list_header_id
8707  and b.template_type  = 'PARAMETERIZED'
8708   and a.query_template_id = b.template_id ;
8709 
8710 l_const_sql1 varchar2(4000) ;
8711 l_l_sele_action_id      number;
8712 
8713   CURSOR c_action_dets1 is
8714   SELECT list_select_action_id,order_number
8715     FROM ams_list_select_actions
8716    WHERE action_used_by_id   = p_action_used_by_id
8717      AND arc_action_used_by  = p_action_used_by
8718      AND order_number        = l_sorted;
8719 l_order_number number := 0;
8720 
8721 l_repeat_tg	varchar2(1);
8722 CURSOR c_repeat_tg is
8723 select 'Y' from ams_campaign_schedules_b b, ams_list_headers_all h
8724 where h.list_header_id = g_list_header_id
8725   and h.LIST_USED_BY_ID = b.schedule_id
8726   and b.orig_csch_id is not null;
8727 
8728 l_incl_header_id	number;
8729 
8730 CURSOR c_repeat_tg_id is
8731   SELECT a.list_header_id
8732     FROM ams_act_lists a ,ams_act_lists b
8733    WHERE a.list_used_by_id   = b.list_used_by_id
8734      AND a.list_used_by =   b.list_used_by
8735      AND b.list_header_id =   g_list_header_id
8736      AND b.list_act_type  =   'TARGET'
8737      and a.list_act_type <> 'TARGET'
8738 	 and a.list_action_type = 'INCLUDE'
8739    ORDER by a.order_number;
8740 
8741 
8742 --Bug 4685389. bmuthukr. to check the total # of parameters
8743 cursor c_check_num_params(p_incl_object_id number) is
8744 select count(1)
8745   from ams_list_queries_param
8746  where list_query_id = p_incl_object_id;
8747 
8748  l_tot_params   number := 0;
8749 --
8750 
8751 BEGIN
8752   --IF(p_log_flag ='Y')then
8753   write_to_act_log('Executing process_tar_actions. ','LIST',g_list_header_id, 'HIGH');
8754   --END IF;
8755 
8756 l_const_sql := ' minus '||
8757                ' select list_entry_source_system_id ' ||
8758                ' from ams_list_entries ' ||
8759                ' where list_header_id  = ' || p_action_used_by_id   ;
8760 
8761 l_const_sql1 := '   and LIST_SELECT_ACTION_ID = ';
8762 
8763   open c_repeat_tg;
8764   fetch c_repeat_tg into l_repeat_tg;
8765   close c_repeat_tg;
8766  write_to_act_log(p_msg_data => 'Repeat  TG - '||l_repeat_tg,
8767 		  p_arc_log_used_by => 'LIST',
8768 		  p_log_used_by_id  => g_list_header_id,
8769 		  p_level=>'LOW');
8770   OPEN C_ACTION_DETS;
8771   LOOP
8772     FETCH c_action_dets INTO l_action_dets_rec;
8773     EXIT WHEN c_action_dets%NOTFOUND;
8774 
8775      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
8776      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : before IMMEDIATE');
8777      FND_MSG_PUB.Add;
8778      write_to_act_log('Selection included in target group is of type '||l_action_dets_rec.list_act_type||' , action type is '||l_action_dets_rec.list_action_type, 'LIST', g_list_header_id,'HIGH');
8779      write_to_act_log('List header id is '||l_action_dets_rec.list_header_id||' act list header id is '||l_action_dets_rec.act_list_header_id||' and order no is '||l_action_dets_rec.order_number, 'LIST', g_list_header_id,'HIGH');
8780    if l_action_dets_rec.list_act_type = 'SQL' then
8781       OPEN c_query( l_action_dets_rec.list_header_id );
8782       FETCH c_query INTO l_PARAMETERIZED_FLAG  ;
8783       close  c_query;
8784    end if;
8785 
8786    -- Bug 4685389. bmuthukr. to check the total # of parameters. If it exceeds 100 abort the process
8787    if nvl(l_parameterized_flag,'N') = 'Y' then
8788 
8789       open c_check_num_params(l_action_dets_rec.list_header_id);
8790       fetch c_check_num_params into l_tot_params;
8791       close c_check_num_params;
8792 
8793       if nvl(l_tot_params,0) > 100 then
8794          write_to_act_log('Numbers of parameters exceed 100. Aborting list generation process. Please redefine your criteria and restrict it to 100.',
8795 	 'LIST',g_list_header_id,'HIGH');
8796 
8797          UPDATE ams_list_headers_all
8798             SET last_generation_success_flag = 'N',
8799                 status_code                  = 'FAILED',
8800                 ctrl_status_code             = 'DRAFT',
8801                 user_status_id               = 311,
8802                 status_date                  = sysdate,
8803                 last_update_date             = sysdate,
8804                 main_gen_end_time            = sysdate,
8805                 no_of_rows_in_ctrl_group     = null
8806           WHERE list_header_id               = g_list_header_id;
8807          x_return_status := 'E';
8808          logger;
8809    	 commit;
8810          RETURN;
8811       end if;
8812    end if;
8813    --
8814 
8815    -- Changes for employee list issue..
8816    if l_action_dets_rec.list_act_type NOT IN ('IMPH','LIST','SQL','DIWB','CELL') then
8817       write_to_act_log(p_msg_data => 'Invalid included object-- Valid inclusions are imported list, list, custom sql, segment, work book. Aborting list generation process.',
8818                        p_arc_log_used_by => 'LIST',
8819                        p_log_used_by_id  => g_list_header_id,
8820 		       p_level => 'HIGH');
8821 
8822         UPDATE ams_list_headers_all
8823         SET    last_generation_success_flag = 'N',
8824                status_code                  = 'FAILED',
8825                user_status_id               = 311,
8826                status_date                  = sysdate,
8827                last_update_date             = sysdate,
8828                main_gen_end_time            = sysdate,
8829 	       ctrl_status_code             = 'DRAFT',
8830 	       no_of_rows_in_ctrl_group     = null
8831         WHERE  list_header_id               = g_list_header_id;
8832 
8833     -- Added for cancel list gen as it prevents parallel update- Raghu
8834     -- of list headers when cancel button is pressed
8835     commit;
8836 
8837      x_return_status := 'E';
8838      x_msg_count := 1;
8839      x_msg_data := ' Invalid Included Object--Valid inclusions are imported list, list, custom sql, segment, work book';
8840      RETURN;
8841    end if;
8842    --
8843 
8844 /******************************************************************************/
8845 /************** call for cancel list generation added 05/23/2005 **************/
8846 /******************************************************************************/
8847 -- inside process_tar_actions
8848 
8849    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
8850                   p_list_header_id => g_list_header_id ,
8851                   p_remote_gen     => g_remote_list    ,
8852                   p_remote_gen_list=> g_remote_list_gen,
8853                   p_database_link  => g_database_link,
8854                   x_msg_count      => x_msg_count ,
8855                   x_msg_data       => x_msg_data ,
8856                   x_return_status  => x_return_status
8857              );
8858 
8859   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
8860      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
8861         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
8862         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
8863      end if;
8864      RAISE FND_API.G_EXC_ERROR;
8865   ELSE
8866      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
8867   END IF;
8868 
8869 /******************************************************************************/
8870 /************** call for cancel list generation added 05/23/2005 **************/
8871 /******************************************************************************/
8872 
8873       write_to_act_log('Calling process_'||l_action_dets_rec.list_act_type,'LIST',g_list_header_id,'LOW');
8874       execute immediate
8875       'BEGIN
8876         AMS_ListGeneration_PKG.process_'||l_action_dets_rec.list_act_type ||
8877          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
8878       END;'
8879       using  p_action_used_by_id,
8880              l_action_dets_rec.list_header_id ,
8881              l_action_dets_rec.list_action_type,
8882              l_action_dets_rec.act_list_header_id,
8883              l_action_dets_rec.order_number,
8884              l_action_dets_rec.order_number,
8885              'N',--CHECK p_action_rec.incl_control_group,
8886              OUT  x_msg_data,
8887              OUT  x_msg_count,
8888              in OUT  x_return_status ,
8889              OUT l_std_sql ,
8890              OUT l_include_sql;
8891     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
8892        write_to_act_log('Error when executing process_'||l_action_dets_rec.list_act_type, 'LIST', g_list_header_id,'HIGH');
8893        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
8894        UPDATE ams_list_headers_all
8895           SET last_generation_success_flag = 'N',
8896               status_code                  = 'FAILED',
8897               user_status_id               = 311,
8898               status_date                  = sysdate,
8899               last_update_date             = sysdate,
8900               main_gen_end_time            = sysdate
8901         WHERE list_header_id               = g_list_header_id;
8902        commit;
8903        RETURN;
8904     else
8905        write_to_act_log('Process_'||l_action_dets_rec.list_act_type||' executed successfully.', 'LIST', g_list_header_id,'LOW');
8906     end if;
8907 
8908      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
8909      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : AFTER IMMEDIATE');
8910      FND_MSG_PUB.Add;
8911 
8912      if l_action_dets_rec.list_action_type  = 'INCLUDE' then
8913         l_include_count := l_include_count +1  ;
8914         l_include_sql_tbl(l_include_count) := l_include_sql ;
8915         l_std_sql_tbl(l_include_count)  := l_std_sql;
8916         l_rank_tbl(l_include_count)  :=  lpad(l_action_dets_rec.Order_number,50,'0')
8917                          || lpad(l_action_dets_rec.order_number,50,'0');
8918 
8919 	l_dist_pct_tbl(l_include_count) := l_action_dets_rec.distribution_pct;
8920         l_list_select_id(l_include_count) := l_action_dets_rec.act_list_header_id;
8921         l_incl_object_type(l_include_count) := l_action_dets_rec.arc_incl_object_from;
8922 
8923      else
8924         if l_action_dets_rec.list_action_type  = 'EXCLUDE' then
8925            l_join_string := ' minus ';
8926            l_list_header_id := l_action_dets_rec.list_header_id;
8927            open c1;
8928            FETCH c1 into l_generation_type;
8929            close c1;
8930         else
8931            l_join_string := ' intersect ';
8932            l_list_header_id := l_action_dets_rec.list_header_Id;
8933            open c1;
8934            FETCH c1 into l_generation_type;
8935            close c1;
8936         end if;
8937 
8938         write_to_act_log('SQL statement for INCLUSION','LIST',g_list_header_id,'LOW');
8939 
8940        FOR i IN 1 .. l_include_count
8941        loop
8942         l_std_sql_tbl(i)  :=
8943                                l_std_sql_tbl(i)   ||
8944                                l_join_string ||
8945                                l_std_sql;
8946      l_no_of_chunks  := ceil(length(l_std_sql_tbl(i))/2000 );
8947      for j in 1 ..l_no_of_chunks
8948      loop
8949           null;
8950         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000),'LIST',g_list_header_id, 'LOW');
8951      end loop;
8952        end loop;
8953      end if;
8954 
8955 
8956      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
8957        RAISE FND_API.G_EXC_ERROR;
8958      END IF;
8959 -- end Of Dynamic Procedure
8960    l_join_string :='';
8961    write_to_act_log('Action on this selection completed. ', 'LIST', g_list_header_id,'LOW');
8962 ---------------------------------------------------------------------------
8963   END LOOP;  --  End loop c_action_dets
8964   CLOSE c_action_dets;
8965 
8966      write_to_act_log('No of inclusions in the selection '|| l_include_count, 'LIST', g_list_header_id,'LOW');
8967      write_to_act_log('Sorting based on rank for the selection. ', 'LIST', g_list_header_id,'LOW');
8968 
8969 /*       -- Sorting According to rank
8970        FOR i IN 1 .. l_include_count
8971        loop
8972           l_rank_num_tbl(i) := i;
8973           if i <> 1 then
8974              for j in 1 .. i-1
8975              loop
8976                if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
8977                   for k in reverse j .. i-1
8978                   loop
8979                      l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
8980                   end loop;
8981                   l_rank_num_tbl(j) := i;
8982                   exit;
8983                end if;
8984              end loop;
8985 	  end if;
8986        end loop;*/
8987   for  i in 1 .. l_include_count
8988   loop
8989         l_l_sele_action_id := null;
8990         l_sorted := i;--l_rank_num_tbl(i);
8991   --      open c_action_dets1;
8992   --      fetch c_action_dets1 into l_l_sele_action_id,l_order_number;
8993    --     close c_action_dets1;
8994         WRITE_TO_ACT_LOG('List selection id is '||l_l_sele_action_id||' for order number '||l_sorted, 'LIST', g_list_header_id,'LOW');
8995 -- l_final_big_sql := l_include_sql_tbl(l_sorted)||l_std_sql_tbl(l_sorted)||l_const_sql||l_const_sql1||l_l_sele_action_id ||')';
8996      --  l_std_sql_tbl(l_sorted) || l_const_sql || ')';
8997      WRITE_TO_ACT_LOG('Final SQL formed for generating TG.', 'LIST', g_list_header_id,'LOW');
8998     if l_final_big_sql is not null then
8999      l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
9000      for i in 1 ..l_no_of_chunks
9001      loop
9002           null;
9003           WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000),'LIST',g_list_header_id,'LOW');
9004      end loop;
9005    end if;
9006 
9007      if l_include_sql_tbl(l_sorted) is not null then
9008       l_no_of_chunks := 0;
9009       l_no_of_chunks  := ceil(length(l_include_sql_tbl(l_sorted))/2000 );
9010       for i in 1 ..l_no_of_chunks
9011        loop
9012         WRITE_TO_ACT_LOG(substrb(l_include_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9013        end loop;
9014      end if;
9015     if l_std_sql_tbl(l_sorted) is not null then
9016       l_no_of_chunks := 0;
9017       l_no_of_chunks  := ceil(length(l_std_sql_tbl(l_sorted))/2000 );
9018       for i in 1 ..l_no_of_chunks
9019        loop
9020         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9021        end loop;
9022     end if;
9023     if l_const_sql is not null then
9024      l_no_of_chunks := 0;
9025      l_no_of_chunks  := ceil(length(l_const_sql)/2000 );
9026      for i in 1 ..l_no_of_chunks
9027      loop
9028         WRITE_TO_ACT_LOG(substrb(l_const_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9029      end loop;
9030     end if;
9031    -- write_to_act_log(' '||l_const_sql1||to_char(l_l_sele_action_id), 'LIST', g_list_header_id,'LOW');
9032     if l_PARAMETERIZED_FLAG  = 'N' then
9033         write_to_act_log('No parameters required for generating this TG', 'LIST', g_list_header_id,'LOW');
9034         if nvl(l_dist_pct_tbl(l_sorted),100) <> 100 then
9035            write_to_act_log('Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
9036 	   write_to_act_log('% Requested for this selection is '||l_dist_pct_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
9037    	   if l_incl_object_type(l_sorted) in ('SQL','DIWB','CELL') then
9038 	      write_to_act_log('Inclusion No is '||l_sorted||'  '||'Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
9039 	      l_temp_sql := l_include_sql_tbl(l_sorted);
9040 	      l_temp_sql := 'SELECT count(1) '||substr(l_temp_sql,instr(l_temp_sql, ' FROM '));
9041               -- Modified for bug 5238900. bmuthukr
9042 	      -- get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||l_const_sql1||l_l_sele_action_id||')' );
9043 	      get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||')' );
9044            elsif l_incl_object_type(l_sorted) = 'LIST' then
9045               get_count(l_list_select_id(l_sorted),l_sorted,'LIST',null);
9046            elsif l_incl_object_type(l_sorted) = 'IMPH' then
9047               get_count(l_list_select_id(l_sorted),l_sorted,'IMPH',null);
9048            end if;
9049            write_to_act_log('No of rows requested from the selection is '||g_reqd_num_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
9050         else
9051            g_act_num_tbl(l_sorted)  := -1;
9052            g_reqd_num_tbl(l_sorted) := -1;
9053         end if;
9054         if g_remote_list_gen = 'N' then
9055                 /* If the list is not based on the remote data source and if it's based on remote data source
9056                    but needs to be generated in the local instance means it's migrated to the local instance */
9057  	   l_const_sql1 := ' ';
9058            l_l_sele_action_id := null;
9059            if g_reqd_num_tbl(l_sorted) <> -1 then
9060               EXECUTE IMMEDIATE l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||' and rownum <= '||g_reqd_num_tbl(l_sorted);
9061            else
9062               EXECUTE IMMEDIATE l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')';
9063            end if;
9064 
9065 	   write_to_act_log('Target group generated in local instance', 'LIST', g_list_header_id,'HIGH');
9066         else
9067                 /* If the list is based on the remote data source and it's not migrated to the local instance or
9068                    a segment , sql or workbook is in the list selection then it will be generated in the remote
9069                    instance through a dynamic procedure call */
9070  	   l_const_sql1 := ' ';
9071            l_l_sele_action_id := null;
9072 
9073        	   if g_reqd_num_tbl(l_sorted) <> -1 then
9074 	      write_to_act_log('Calling remote procedure to generate target group in remote instance', 'LIST', g_list_header_id,'LOW');
9075               execute immediate
9076               'BEGIN
9077                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9078               ' END;'
9079               using  '1',
9080               l_null,
9081               'T',
9082               l_null,
9083               OUT x_return_status,
9084               OUT x_msg_count,
9085               OUT x_msg_data,
9086               g_list_header_id,
9087               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||' and rownum <= '||g_reqd_num_tbl(l_sorted),
9088 --              l_final_big_sql,
9089               l_null,
9090               OUT l_total_recs,
9091               'LISTGEN';
9092            else
9093               execute immediate
9094       	      'BEGIN
9095                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9096               ' END;'
9097               using  '1',
9098               l_null,
9099               'T',
9100               l_null,
9101               OUT x_return_status,
9102               OUT x_msg_count,
9103               OUT x_msg_data,
9104               g_list_header_id,
9105               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')',
9106 	      --   l_final_big_sql,
9107               l_null,
9108               OUT l_total_recs,
9109               'LISTGEN';
9110            end if;
9111            if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9112               write_to_act_log('Error while executing remote procedure for generating target group', 'LIST', g_list_header_id,'HIGH');
9113               write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9114               --Added for bug 4577528 by bmuthukr.
9115               update ams_list_headers_all
9116                  set last_generation_success_flag = 'N',
9117                      status_code                  = 'FAILED',
9118                      user_status_id               = 311,
9119                      status_date                  = sysdate,
9120                      last_update_date             = sysdate,
9121                      main_gen_end_time            = sysdate,
9122 		     ctrl_status_code             = 'DRAFT',
9123                      no_of_rows_in_ctrl_group     = null
9124                where list_header_id               = g_list_header_id;
9125  	      update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
9126               write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
9127 	      x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
9128 	      return;
9129 	      --
9130            else
9131               write_to_act_log('Target group generated in remote instance', 'LIST', g_list_header_id,'HIGH');
9132            end if;
9133 
9134         end if;
9135  else  -- For l_PARAMETERIZED_FLAG  = 'Y'
9136         write_to_act_log('Fetching parameters required for generating this target group', 'LIST', g_list_header_id,'LOW');
9137  	   l_const_sql1 := ' ';
9138            l_l_sele_action_id := null;
9139 
9140         for i in 1 .. 100 loop
9141            l_table_char(i) := ' ';
9142         end loop;
9143 	 write_to_act_log(p_msg_data => 'Repeat TG = '||l_repeat_tg,
9144 						p_arc_log_used_by => 'LIST',
9145 						p_log_used_by_id  => g_list_header_id,
9146 						p_level =>'LOW');
9147 	l_incl_header_id	:= null;
9148 	if l_repeat_tg = 'Y' then
9149 		open c_repeat_tg_id;
9150 		fetch c_repeat_tg_id into l_incl_header_id;
9151 		close c_repeat_tg_id;
9152            else
9153 		l_incl_header_id := l_action_dets_rec.list_header_id;
9154 	end if;
9155 
9156 --	for i in c_param_values(l_action_dets_rec.list_header_id )
9157         for i in c_param_values(l_incl_header_id)
9158         loop
9159            l_table_char(i.PARAMETER_ORDER) := i.PARAMETER_VALUE;
9160 	   write_to_act_log('Parameter - '||i.parameter_order||' is '||l_table_char(i.PARAMETER_ORDER),'LIST', g_list_header_id,'LOW');
9161         end loop;
9162         l_string := 'DECLARE   ' ||
9163         'l_string1 varchar2(10000) ; ' ||
9164         'begin    ' ||
9165 	        ' l_string1 :=   :1  || ' || ' :2  || ' || ' :3  || ' || ' :4  || ' ||
9166                        ' :5  || ' || ' :6  || ' || ' :7  || ' || ' :8  || ' ||
9167                        ' :9  || ' || ' :10  || ' || ' :11  || ' || ' :12  || ' ||
9168                        ' :13  || ' || ' :14  || ' || ' :15  || ' || ' :16  || ' ||
9169                        ' :17  || ' || ' :18  || ' || ' :19  || ' || ' :20  || ' ||
9170                        ' :21  || ' || ' :22  || ' || ' :23  || ' || ' :24  || ' ||
9171                        ' :25  || ' || ' :26  || ' || ' :27  || ' || ' :28  || ' ||
9172                        ' :29  || ' || ' :30  || ' || ' :31  || ' || ' :32  || ' ||
9173                        ' :33  || ' || ' :34  || ' || ' :35  || ' || ' :36  || ' ||
9174                        ' :37  || ' || ' :38  || ' || ' :39  || ' || ' :40  || ' ||
9175                        ' :41  || ' || ' :42  || ' || ' :43  || ' || ' :44  || ' ||
9176                        ' :45  || ' || ' :46  || ' || ' :47  || ' || ' :48  || ' ||
9177                        ' :49  || ' || ' :50  || ' || ' :51  || ' || ' :52  || ' ||
9178                        ' :53  || ' || ' :54  || ' || ' :55  || ' || ' :56  || ' ||
9179                        ' :57  || ' || ' :58  || ' || ' :59  || ' || ' :60  || ' ||
9180                        ' :61  || ' || ' :62  || ' || ' :63  || ' || ' :64  || ' ||
9181                        ' :65  || ' || ' :66  || ' || ' :67  || ' || ' :68  || ' ||
9182                        ' :69  || ' || ' :70  || ' || ' :71  || ' || ' :72  || ' ||
9183                        ' :73  || ' || ' :74  || ' || ' :75  || ' || ' :76  || ' ||
9184                        ' :77  || ' || ' :78  || ' || ' :79  || ' || ' :80  || ' ||
9185                        ' :81  || ' || ' :82  || ' || ' :83  || ' || ' :84  || ' ||
9186                        ' :85  || ' || ' :86  || ' || ' :87  || ' || ' :88  || ' ||
9187                        ' :89  || ' || ' :90  || ' || ' :91  || ' || ' :92  || ' ||
9188                        ' :93  || ' || ' :94  || ' || ' :95  || ' || ' :96  || ' ||
9189                        ' :97  || ' || ' :98  || ' || ' :99  || ' || ' :100  ; ' ||' '||
9190        --  l_final_big_sql ||
9191 l_include_sql_tbl(l_sorted)||l_std_sql_tbl(l_sorted)||l_const_sql||l_const_sql1||l_l_sele_action_id ||')'||
9192        '; end;  '  ;
9193 /* Changed to fix :1 and :name issue */
9194 
9195      open C_QUERY_TEMP_TYPE ;
9196         fetch  c_query_temp_type into l_query_templ_flag   ;
9197            if l_query_templ_flag  = 'Y' then
9198               l_no_of_chunks := 0;
9199 		-- bmuthukr 4339703
9200               for i in c_param_values(l_incl_header_id)
9201               loop
9202                  l_no_of_chunks := l_no_of_chunks + 1;
9203                  l_string := replace(l_string,':' || l_no_of_chunks  || ' ' , ':'|| i.parameter_name||' ' );
9204 	      end loop;
9205            end if;
9206            l_no_of_chunks := 0;
9207      close c_query_temp_type ;
9208  /*END Changed to fix :1 and :name issue */
9209      WRITE_TO_ACT_LOG('SQL to be executed to generate target group', 'LIST', g_list_header_id,'LOW');
9210      WRITE_TO_ACT_LOG('Length of the sql  '||length(l_string), 'LIST', g_list_header_id,'LOW');
9211      l_no_of_chunks  := ceil(length(l_string)/80 );
9212      for i in 1 ..l_no_of_chunks
9213      loop
9214         WRITE_TO_ACT_LOG(substrb(l_string,(80*i) - 79,80), 'LIST', g_list_header_id,'LOW');
9215         l1 := substrb(l_string,(80*i)-79,80);
9216      end loop;
9217 if g_remote_list_gen = 'N' then
9218      WRITE_TO_ACT_LOG('Generating target group with parameters ','LIST', g_list_header_id,'LOW');
9219 execute immediate   l_string
9220 using l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
9221       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
9222       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
9223       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
9224       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
9225       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
9226       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
9227       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
9228       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
9229       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
9230       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
9231       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
9232       l_table_char(49), l_table_char(50),
9233       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
9234       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
9235       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
9236       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
9237       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
9238       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
9239       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
9240       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
9241       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
9242       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
9243       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
9244       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
9245       l_table_char(79), l_table_char(100);
9246       WRITE_TO_ACT_LOG('Generating target group in local instance.','LIST', g_list_header_id,'HIGH');
9247 
9248  end if;
9249 
9250  if g_remote_list_gen = 'Y' then
9251     write_to_act_log('Calling remote procedure with parameters to generate target group in remote instance ', 'LIST', g_list_header_id,'LOW');
9252       	       execute immediate
9253       	      'BEGIN
9254        AMS_Remote_ListGen_PKG.remote_param_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,
9255        :13,:14,:15,:16,:17,:18,:19,:20,
9256        :21,:22,:23,:24,:25,:26,:27,:28,:29,:30,
9257        :31,:32,:33,:34,:35,:36,:37,:38,:39,:40,
9258        :41,:42,:43,:44,:45,:46,:47,:48,:49,:50,
9259        :51,:52,:53,:54,:55,:56,:57,:58,:59,:60,
9260        :61,:62,:63,:64,:65,:66,:67,:68,:69,:70,
9261        :71,:72,:73,:74,:75,:76,:77,:78,:79,:80,
9262        :81,:82,:83,:84,:85,:86,:87,:88,:89,:90,
9263        :91,:92,:93,:94,:95,:96,:97,:98,:99,:100,
9264        :101,:102,:103,:104,:105,:106,:107,:108,:109,:110,:111,:112
9265        )'||';'||
9266               ' END;'
9267               using  '1',
9268               l_null,
9269               'T',
9270               l_null,
9271               OUT x_return_status,
9272               OUT x_msg_count,
9273               OUT x_msg_data,
9274               g_list_header_id,
9275 	      l_string,
9276               l_null,
9277              OUT l_total_recs,
9278              'PARAMLISTGEN',
9279       l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
9280       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
9281       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
9282       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
9283       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
9284       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
9285       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
9286       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
9287       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
9288       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
9289       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
9290       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
9291       l_table_char(49), l_table_char(50),
9292       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
9293       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
9294       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
9295       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
9296       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
9297       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
9298       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
9299       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
9300       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
9301       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
9302       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
9303       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
9304       l_table_char(79), l_table_char(100);
9305       if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9306          write_to_act_log('Error while generating target group in remote instance.', 'LIST', g_list_header_id,'HIGH');
9307          write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9308          --Added for bug 4577528 by bmuthukr.
9309          update ams_list_headers_all
9310             set last_generation_success_flag = 'N',
9311                 status_code                  = 'FAILED',
9312                 user_status_id               = 311,
9313                 status_date                  = sysdate,
9314                 last_update_date             = sysdate,
9315                 main_gen_end_time            = sysdate,
9316                 ctrl_status_code             = 'DRAFT',
9317                 no_of_rows_in_ctrl_group     = null
9318           where list_header_id               = g_list_header_id;
9319          update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
9320          write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
9321 	 x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
9322 	 return;
9323 	 --
9324       else
9325          write_to_act_log('Target group generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
9326       end if;
9327     end if;
9328 
9329 end if;
9330   end loop;
9331     WRITE_TO_ACT_LOG('Execution of procedure process_tar_action completed.', 'LIST', g_list_header_id,'LOW');
9332 
9333 EXCEPTION
9334 
9335   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
9336      IF(c_action_dets%ISOPEN)THEN
9337         CLOSE c_action_dets;
9338      END IF;
9339 
9340      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9341 
9342      write_to_act_log('executing process_tar_actions - user action to cancel list generation detected ', 'LIST', g_list_header_id,'HIGH');
9343      -- Got to raise the exception again because Listgen has to end generation.
9344      raise AMS_LISTGENERATION_UTIL_PKG.cancelListGen;
9345 
9346    WHEN FND_API.G_EXC_ERROR THEN
9347      IF(c_action_dets%ISOPEN)THEN
9348         CLOSE c_action_dets;
9349      END IF;
9350      -- Check if reset of the status is required
9351      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9352      x_return_status := FND_API.G_RET_STS_ERROR ;
9353       FND_MSG_PUB.count_and_get(
9354             p_encoded => FND_API.g_false,
9355             p_count   => x_msg_count,
9356             p_data    => x_msg_data);
9357 
9358   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9359      IF(c_action_dets%ISOPEN)THEN
9360         CLOSE c_action_dets;
9361      END IF;
9362      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9363      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9364      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
9365      FND_MSG_PUB.Add;
9366      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9367       FND_MSG_PUB.count_and_get(
9368             p_encoded => FND_API.g_false,
9369             p_count   => x_msg_count,
9370             p_data    => x_msg_data);
9371 
9372   WHEN OTHERS THEN
9373      IF(c_action_dets%ISOPEN)THEN
9374         CLOSE c_action_dets;
9375      END IF;
9376      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9377      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9378      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
9379      FND_MSG_PUB.Add;
9380      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9381       FND_MSG_PUB.count_and_get(
9382             p_encoded => FND_API.g_false,
9383             p_count   => x_msg_count,
9384             p_data    => x_msg_data);
9385 END process_tar_actions;
9386 
9387 -- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
9388 
9389 PROCEDURE GENERATE_TARGET_GROUP
9390 ( p_api_version            IN     NUMBER,
9391   p_init_msg_list          IN     VARCHAR2   := FND_API.G_TRUE,
9392   p_commit                 IN     VARCHAR2   := FND_API.G_FALSE,
9393   p_validation_level       IN     NUMBER     := FND_API.G_VALID_LEVEL_FULL,
9394   p_list_header_id         IN     NUMBER,
9395   x_return_status          OUT NOCOPY    VARCHAR2,
9396   x_msg_count              OUT NOCOPY    NUMBER,
9397   x_msg_data               OUT NOCOPY    VARCHAR2) IS
9398 
9399   l_api_name            CONSTANT VARCHAR2(30)  := 'GENERATE_LIST';
9400   l_api_version         CONSTANT NUMBER        := 1.0;
9401 
9402   -----------------------------------------------------------
9403   --The no. of entries flagged as duplicates for this list.--
9404   --Only populated if deduplication is requested.          --
9405   -----------------------------------------------------------
9406   --gjoby check if l_no_of_duplicates required
9407   l_no_of_duplicates      NUMBER := 0;
9408 
9409   -- Two records are required for init rec and complete rec
9410   -- Table ams_list_headers_all_tl and ams_list_headers_all
9411   l_listheader_rec        ams_listheader_pvt.list_header_rec_type;
9412   l_tmp_listheader_rec    ams_listheader_pvt.list_header_rec_type;
9413 
9414   -- Two records are required for init rec and complete rec
9415   -- Table ams_list_select_actions
9416   l_listaction_rec        ams_listaction_pvt.action_rec_type;
9417   l_tmp_listaction_rec    ams_listaction_pvt.action_rec_type;
9418   l_no_of_rows_in_list number ;
9419   cursor c_get_rows (c_list_header_id in number ) is
9420   select no_of_rows_in_list
9421   from ams_list_headers_all
9422   where list_header_id = c_list_header_id ;
9423   l_error_position       varchar2(100);
9424 
9425 
9426   cursor c_remote_list is
9427   select nvl(stypes.remote_flag,'N') ,database_link
9428     from ams_list_src_types stypes, ams_list_headers_all list
9429    where list.list_source_type = stypes.source_type_code
9430      and list_header_id  =  p_list_header_id;
9431 
9432   l_list_selection      varchar2(1);
9433   l_onlylist_selection  varchar2(1);
9434   cursor c_list_selection is
9435   select 'Y' from ams_list_select_actions
9436    where  action_used_by_id = p_list_header_id
9437      and  arc_action_used_by = 'LIST'
9438      and  arc_incl_object_from in ('CELL','DIWB','SQL');
9439 
9440   cursor c_only_list_selection is
9441   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
9442    where  act.action_used_by_id = p_list_header_id
9443    and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
9444    and  act.INCL_OBJECT_ID = head.list_header_id
9445    and  head.status_code = 'AVAILABLE'
9446    and  head.MIGRATION_DATE is null;
9447 
9448 l_null          varchar2(30) := null;
9449 l_total_recs    number;
9450 l_field_column_name VARCHAR2(30);
9451 l_count             NUMBER;
9452 
9453 
9454 --Bug 5235979. Bmuthukr
9455 
9456 /* cursor c1 is
9457     SELECT list_rule_id
9458        FROM ams_list_rule_usages
9459        WHERE list_header_id = g_list_header_id;*/
9460 
9461 CURSOR C1 IS
9462 SELECT us.list_rule_id
9463   FROM ams_list_rule_usages us, ams_list_rules_all rules
9464  WHERE us.list_header_id = g_list_header_id
9465    AND us.list_rule_id = rules.list_rule_id
9466    AND rules.list_source_type = l_listheader_rec.list_source_type
9467    AND rules.list_rule_type = 'TARGET';
9468 
9469 -- Ends changes
9470 
9471 l_list_rule_id number := 0;
9472 l_hd_status	varchar(60);
9473 l_entry_count	number;
9474 cursor c_hd_status is
9475 select STATUS_CODE from ams_list_headers_all
9476 where list_header_id = p_list_header_id;
9477 
9478 cursor c_entry_count is
9479 select count(1) from ams_list_entries
9480 where list_header_id = p_list_header_id;
9481 
9482 l_list_field_mapped  varchar2(1);
9483 
9484 cursor c_master_ds_fields_mapped is
9485 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
9486 where hd.list_header_id = p_list_header_id
9487   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9488   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
9489   and fd.FIELD_COLUMN_NAME is NOT NULL;
9490 
9491 cursor c_child_ds_fields_mapped is
9492 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
9493 ams_list_src_type_assocs ats
9494 where hd.list_header_id = p_list_header_id
9495   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9496   and ty.list_source_type_id = ats.master_source_type_id
9497   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
9498   and fd.FIELD_COLUMN_NAME is NOT NULL;
9499 
9500 l_tca_field_mapped  varchar2(1);
9501 
9502 l_no_of_rows   number := 0;
9503 
9504 cursor c_check_gen_mode is
9505 select nvl(no_of_rows_in_list ,0)
9506   from ams_list_headers_all
9507  where list_header_id = g_list_header_id;
9508 
9509 -- SOLIN, bug 4410333
9510 -- check whether datasource is enabled.
9511 cursor c_check_datasource(c_list_header_id NUMBER) is
9512   SELECT a.enabled_flag
9513   FROM ams_list_src_types a,
9514        ams_list_headers_all b
9515   WHERE a.source_type_code = b.list_source_type
9516     AND b.list_header_id = c_list_header_id;
9517 
9518 l_ds_enabled_flag      VARCHAR2(1);
9519 -- SOLIN, end
9520 
9521 cursor c_master_ds_tca_mapped is
9522 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
9523 where hd.list_header_id = p_list_header_id
9524   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9525   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
9526   and fd.tca_column_id is NOT NULL;
9527 
9528 cursor c_child_ds_tca_mapped is
9529 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
9530 ams_list_src_type_assocs ats
9531 where hd.list_header_id = p_list_header_id
9532   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9533   and ty.list_source_type_id = ats.master_source_type_id
9534   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
9535   and fd.tca_column_id is NOT NULL;
9536 
9537 CURSOR c_get_dup_fields(c_list_header_id NUMBER) IS
9538 SELECT min(master_child.field_column_name1) ,count(master_child.field_column_name) from
9539   (
9540   SELECT d.field_column_name field_column_name1,d.field_column_name
9541   FROM ams_list_src_types a,
9542        ams_list_headers_all b,
9543        ams_list_src_fields d
9544   WHERE a.source_type_code = b.list_source_type
9545    and b.list_header_id = p_list_header_id
9546    and d.list_source_type_id = a.list_source_type_id
9547    and d.USED_IN_LIST_ENTRIES = 'Y'
9548   union all
9549    SELECT d.field_column_name field_column_name1,d.field_column_name
9550   FROM ams_list_src_types a,
9551        ams_list_headers_all b,
9552        ams_list_src_fields d,
9553        ams_list_src_type_assocs e
9554   WHERE a.source_type_code = b.list_source_type
9555    and b.list_header_id = p_list_header_id
9556    and e.master_source_type_id = a.list_source_type_id
9557    and d.list_source_type_id = e.sub_source_type_id
9558    and d.USED_IN_LIST_ENTRIES = 'Y'
9559    ) master_child
9560   GROUP BY master_child.field_column_name
9561   having COUNT(master_child.field_column_name) > 1;
9562 l_ctrl_grp_status   VARCHAR2(100);
9563 
9564 --bmuthukr bug 4997699
9565 l_ds_name         varchar2(1000);
9566 l_field_col_name  varchar2(1000);
9567 l_source_col_name varchar2(1000);
9568 
9569 cursor c_get_dup_mapping(p_col_name in varchar2) is
9570 SELECT d.source_column_name, d.field_column_name , d.de_list_source_type_code  stc
9571   FROM ams_list_src_types a,
9572        ams_list_headers_all b,
9573        ams_list_src_fields d
9574  WHERE a.source_type_code = b.list_source_type
9575    and b.list_header_id = g_list_header_id
9576    and d.list_source_type_id = a.list_source_type_id
9577    and d.field_column_name = p_col_name
9578    and d.USED_IN_LIST_ENTRIES = 'Y'
9579 union all
9580 SELECT d.source_column_name, d.field_column_name,  d.de_list_source_type_code stc
9581   FROM ams_list_src_types a,
9582        ams_list_headers_all b,
9583        ams_list_src_fields d,
9584        ams_list_src_type_assocs e
9585  WHERE a.source_type_code = b.list_source_type
9586    and b.list_header_id = g_list_header_id
9587    and e.master_source_type_id = a.list_source_type_id
9588    and d.list_source_type_id = e.sub_source_type_id
9589    and d.field_column_name = p_col_name
9590    and d.USED_IN_LIST_ENTRIES = 'Y';
9591 --
9592 cursor c_check_supp is
9593 select nvl(apply_suppression_flag,'N')
9594   from ams_list_headers_all
9595  where list_header_id = p_list_header_id;
9596 
9597 l_supp_flag   varchar2(1) := 'N';
9598 
9599 BEGIN
9600 
9601 
9602   l_error_position := '<- start List generate ->';
9603   -----------------------------------------------------------------------------
9604   -- g_list_header_id global variable for this session
9605   -- This eliminates the need for passing variables across procedures
9606   -- Particularly for logging debug messages ams_act_logs
9607   -----------------------------------------------------------------------------
9608   g_remote_list           := 'N';
9609   g_remote_list_gen       := 'N';
9610   g_database_link         := ' ';
9611   g_list_header_id        :=  p_list_header_id;
9612 
9613   write_to_act_log(p_msg_data => 'Executing procedure generate_target_group. Target group generation started.',
9614                    p_arc_log_used_by => 'LIST',
9615                    p_log_used_by_id  => p_list_header_id,
9616 		   p_level => 'HIGH');
9617 
9618   --write_to_act_log(p_msg_data => 'Concurrent request id is '||fnd_global.conc_request_id,p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
9619 
9620   write_to_act_log(p_msg_data => 'Work flow item key(list header id) is '||p_list_header_id||'	 Process type is AMS List Generation',p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
9621 
9622   -- SOLIN, bug 4410333
9623   l_ds_enabled_flag := 'N';
9624   OPEN c_check_datasource(p_list_header_id);
9625   FETCH c_check_datasource INTO l_ds_enabled_flag;
9626   CLOSE c_check_datasource;
9627 
9628   IF l_ds_enabled_flag = 'N' THEN
9629      write_to_act_log(
9630           p_msg_data => 'Aborting the List generation process. The datasource for this list is not enabled. Contact your administrator to enable the datasource, and generate the list again.',
9631           p_arc_log_used_by => 'LIST',
9632           p_log_used_by_id  => p_list_header_id,
9633 	  p_level => 'HIGH');
9634      UPDATE ams_list_headers_all
9635         SET last_generation_success_flag = 'N',
9636             status_code                  = 'FAILED',
9637             user_status_id               = 311,
9638             status_date                  = sysdate,
9639             last_update_date             = sysdate,
9640             main_gen_end_time            = sysdate,
9641             ctrl_status_code             = 'DRAFT',
9642             no_of_rows_in_ctrl_group     = null
9643       WHERE list_header_id               = p_list_header_id;
9644       FND_MSG_PUB.count_and_get(
9645             p_encoded => FND_API.g_false,
9646             p_count   => x_msg_count,
9647             p_data    => x_msg_data
9648       );
9649       x_return_status := FND_API.g_ret_sts_error;
9650       --
9651       logger;
9652       RETURN;
9653   END IF;
9654 -- SOLIN, end
9655 
9656   write_to_act_log(p_msg_data => 'Checking if Master/Child datasource fields are mapped.' ,
9657                    p_arc_log_used_by => 'LIST',
9658                    p_log_used_by_id  => p_list_header_id,
9659                    p_level =>'LOW');
9660 
9661   open  c_master_ds_fields_mapped;
9662   fetch c_master_ds_fields_mapped into l_list_field_mapped;
9663   close c_master_ds_fields_mapped;
9664 
9665   open  c_child_ds_fields_mapped;
9666   fetch c_child_ds_fields_mapped into l_list_field_mapped;
9667   close c_child_ds_fields_mapped;
9668 
9669   if l_list_field_mapped is NULL THEN
9670      write_to_act_log(p_msg_data => 'Master/Child datasource fields are not mapped. Aborting target group generation. ' ,
9671                       p_arc_log_used_by => 'LIST',
9672                       p_log_used_by_id  => p_list_header_id,
9673 	     	      p_level =>'HIGH');
9674 
9675       UPDATE ams_list_headers_all
9676          SET last_generation_success_flag = 'N',
9677              status_code                  = 'FAILED',
9678              user_status_id               = 311,
9679              status_date                  = sysdate,
9680              last_update_date             = sysdate,
9681              main_gen_end_time            = sysdate,
9682              ctrl_status_code             = 'DRAFT',
9683              no_of_rows_in_ctrl_group     = null
9684        WHERE list_header_id               = p_list_header_id;
9685       -- calling logging program
9686         logger;
9687       --
9688       IF FND_API.To_Boolean ( p_commit ) THEN
9689         COMMIT WORK;
9690       END IF;
9691       --Modified by bmuthukr. Bug # 4083665
9692       x_return_status := FND_API.g_ret_sts_error;
9693       --
9694       RETURN;
9695    end if;
9696 
9697 
9698 
9699 
9700   OPEN c_get_dup_fields(p_list_header_id);
9701   FETCH c_get_dup_fields INTO l_field_column_name, l_count;
9702   CLOSE c_get_dup_fields;
9703 
9704   IF l_count>1 THEN
9705      /*DELETE FROM ams_act_logs
9706       WHERE arc_act_log_used_by = 'LIST'
9707         AND act_log_used_by_id  = p_list_header_id ;*/
9708 
9709      write_to_act_log(
9710           p_msg_data => 'Aborting the Target group generation process. Atleast one list entry column is mapped morethan once in the datasources.Pls see the following details for more info.',
9711           p_arc_log_used_by => 'LIST',
9712           p_log_used_by_id  => p_list_header_id,
9713 	  p_level => 'HIGH');
9714 
9715      --bmuthukr bug 4997699
9716      open c_get_dup_mapping(l_field_column_name);
9717      loop
9718         fetch c_get_dup_mapping into l_source_col_name, l_field_col_name  ,l_ds_name;
9719 	exit when c_get_dup_mapping%notfound;
9720         write_to_Act_log('Data Source Name :- '||l_ds_name||'          '||' Source Column :- '||l_source_col_name||'          '||' List Entries Col :- '||l_field_col_name,'LIST',p_list_header_id,'HIGH');
9721      end loop;
9722      --
9723      UPDATE ams_list_headers_all
9724         SET last_generation_success_flag = 'N',
9725             status_code                  = 'FAILED',
9726             user_status_id               = 311,
9727             status_date                  = sysdate,
9728             last_update_date             = sysdate,
9729             main_gen_end_time            = sysdate,
9730             ctrl_status_code             = 'DRAFT',
9731             no_of_rows_in_ctrl_group     = null
9732       WHERE list_header_id               = g_list_header_id;
9733       --Modified by bmuthukr. Bug # 4083665
9734       x_return_status := FND_API.g_ret_sts_error;
9735       --
9736       logger;
9737       RETURN;
9738   END IF;
9739 
9740   x_return_status := FND_API.G_RET_STS_SUCCESS;
9741 
9742   -- Standard call to check for call compatibility.
9743   IF NOT FND_API.Compatible_API_Call ( l_api_version,
9744                                        p_api_version,
9745                                        l_api_name,
9746                                        G_PKG_NAME)
9747   THEN
9748     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9749   END IF;
9750 
9751   IF FND_API.to_Boolean( p_init_msg_list ) THEN
9752      FND_MSG_PUB.initialize;
9753   END IF;
9754 
9755   -- Checking if Debug is set. If debug is set then log debugging message
9756   IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH)
9757   THEN
9758      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9759      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : Start');
9760      FND_MSG_PUB.Add;
9761   END IF;
9762 
9763   --  Initialize API return status to success
9764   x_return_status := FND_API.G_RET_STS_SUCCESS;
9765 
9766   l_error_position := '<- Init List->';
9767   --These values need to be fetched before calling initialize_list. Changes done by bmuthukr.
9768 
9769 
9770   open c_remote_list;
9771   fetch c_remote_list into g_remote_list,g_database_link;
9772   close c_remote_list;
9773 
9774   if nvl(g_remote_list,'N') = 'N' then
9775      write_to_act_log(p_msg_data => 'Target group is based not on remote datasource.',
9776                       p_arc_log_used_by => 'LIST',
9777                       p_log_used_by_id  => p_list_header_id,
9778    	   	      p_level => 'LOW');
9779   elsif nvl(g_remote_list,'Y') = 'Y' then
9780      write_to_act_log(p_msg_data => 'Target group is based on remote datasource. Database link is  ' ||g_database_link,
9781                       p_arc_log_used_by => 'LIST',
9782                       p_log_used_by_id  => p_list_header_id,
9783    	   	      p_level => 'HIGH');
9784   end if;
9785 
9786   --
9787 
9788   -----------------------------------------------------------------------------
9789   -- Gets list header record details
9790   -- Intialize the record, set the list header id and retrieve the records
9791   -----------------------------------------------------------------------------
9792   write_to_act_log(p_msg_data => 'Calling ams_listheader_pvt to get the header details.' ,
9793                    p_arc_log_used_by => 'LIST',
9794                    p_log_used_by_id  => p_list_header_id,
9795    	   	   p_level => 'LOW');
9796 
9797   ams_listheader_pvt.init_listheader_rec(l_tmp_listheader_rec);
9798   l_tmp_listheader_rec.list_header_id   := p_list_header_id;
9799 
9800   l_error_position := '<- complete rec ->';
9801   ams_listheader_pvt.complete_listheader_rec
9802                    (p_listheader_rec  =>l_tmp_listheader_rec,
9803                     x_complete_rec    =>l_listheader_rec);
9804   -----------------------------------------------------------------------------
9805 
9806   -----------------------------------------------------------
9807   -- Initializes the list header record
9808   -----------------------------------------------------------
9809   l_error_position := '<- Initialize List ->';
9810   write_to_act_log(p_msg_data => 'Calling initialize_list to initialize the list.' ,
9811                    p_arc_log_used_by => 'LIST',
9812                    p_log_used_by_id  => p_list_header_id,
9813    	   	   p_level => 'LOW');
9814 
9815   initialize_List(p_list_header_rec => l_listheader_rec,
9816                   x_msg_count       => x_msg_count,
9817                   x_msg_data        => x_msg_data,
9818                   x_return_status   => x_return_status);
9819 
9820   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9821      write_to_act_log('Error while executing procedure initialize_list', 'LIST', g_list_header_id,'HIGH');
9822      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9823   else
9824      write_to_act_log('Target group initialized.' ,'LIST',p_list_header_id,'HIGH');
9825   end if;
9826 
9827   IF x_return_status = FND_API.g_ret_sts_error THEN
9828      RAISE FND_API.g_exc_error;
9829   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
9830      RAISE FND_API.g_exc_unexpected_error;
9831   END IF;
9832 -- -------------------------------------------------------------------------
9833   if g_remote_list = 'Y' then
9834        remote_list_gen(p_list_header_id  => p_list_header_id,
9835                      x_return_status   => x_return_status,
9836                      x_msg_count       => x_msg_count,
9837                      x_msg_data        => x_msg_data,
9838                      x_remote_gen      => g_remote_list_gen);
9839      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9840         write_to_act_log('Error in executing remote_list_gen procedure', 'LIST', g_list_header_id,'HIGH');
9841         write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9842      else
9843         write_to_act_log(p_msg_data => 'remote_list_gen procedure executed successfully.' ,
9844                          p_arc_log_used_by => 'LIST',
9845                          p_log_used_by_id  => p_list_header_id,
9846  			 p_level => 'LOW');
9847      end if;
9848   end if;
9849 
9850   if l_listheader_rec.generation_type = 'UPD' then
9851      open c_check_gen_mode;
9852      fetch c_check_gen_mode into l_no_of_rows;
9853      close c_check_gen_mode;
9854 
9855      if l_no_of_rows = 0 then
9856         write_to_act_log('No entries in list entries table. Unable to generate target group in update mode. Pls generate in full refresh/append mode.','LIST',g_list_header_id,'HIGH');
9857         UPDATE ams_list_headers_all
9858            SET last_generation_success_flag = 'N',
9859                status_code                  = 'FAILED',
9860                ctrl_status_code             = 'DRAFT',
9861                user_status_id               = 311,
9862                status_date                  = sysdate,
9863                last_update_date             = sysdate,
9864                main_gen_end_time            = sysdate
9865          WHERE list_header_id               = g_list_header_id;
9866          x_return_status := FND_API.g_ret_sts_error;
9867          logger;
9868          RETURN;
9869       end if;
9870    end if;
9871 
9872 --In Update mode need to update only the enabled entries from R12.
9873 /*
9874   if l_listheader_rec.generation_type = 'UPD' then
9875      write_to_act_log('Target group is generated in UPDATE mode', 'LIST', g_list_header_id,'HIGH');
9876        update ams_list_entries
9877        set newly_updated_flag = 'N' , enabled_flag = 'Y'
9878          where list_header_id = l_listheader_rec.list_header_id;
9879 */
9880    /********************************************************************
9881     Dynamic procedure will update the list from the remote instance in
9882     case of remote list
9883    *********************************************************************/
9884 /*     if g_remote_list = 'Y' then
9885         write_to_act_log(p_msg_data => 'Updating the target group in remote instance. ' ,
9886                          p_arc_log_used_by => 'LIST',
9887                          p_log_used_by_id  => p_list_header_id,
9888 	   	         p_level => 'LOW');
9889       execute immediate
9890       'BEGIN
9891       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9892       ' END;'
9893       using  '1',
9894              l_null,
9895              'T',
9896              l_null,
9897              OUT x_return_status,
9898              OUT x_msg_count,
9899              OUT x_msg_data,
9900              l_listheader_rec.list_header_id,
9901              l_null,
9902              l_null,
9903              OUT l_total_recs,
9904              'UPDATE';
9905        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9906           write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
9907           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9908        else
9909           write_to_act_log(p_msg_data => 'Target group updated in remote instance. ' ,
9910                            p_arc_log_used_by => 'LIST',
9911                            p_log_used_by_id  => p_list_header_id,
9912 	  		   p_level => 'LOW');
9913        end if;
9914      end if;
9915   end if;
9916 */
9917   update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
9918   if l_listheader_rec.generation_type = 'UPD' then
9919      l_error_position := '<- Get_list_entry_data inside deduplication ->';
9920    -- For bug 5216890
9921    -- if g_remote_list <> 'Y' then
9922    --
9923    -- This will not be performed for the remote list generation
9924    --
9925      write_to_act_log('Target group is generated in UPDATE mode in local instance.', 'LIST', g_list_header_id,'HIGH');
9926      GET_LIST_ENTRY_DATA(
9927                  p_list_header_id =>l_listheader_rec.list_header_id,
9928                   x_return_status => x_return_status);
9929      IF x_return_status = FND_API.g_ret_sts_error THEN
9930         RAISE FND_API.g_exc_error;
9931      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
9932         RAISE FND_API.g_exc_unexpected_error;
9933      END IF;
9934    --end if;
9935   -- END IF;
9936   else
9937   l_error_position := '<- Process List Actions  ->';
9938   write_to_act_log('Target group is generated in '||l_listheader_rec.generation_type||' mode.','LIST', g_list_header_id,'HIGH');
9939   write_to_act_log('Calling process_tar_actions to generate Target group.', 'LIST', g_list_header_id,'LOW');
9940   process_tar_Actions(p_action_used_by_id => l_listheader_rec.list_header_id,
9941                        p_action_used_by    => 'LIST',
9942                        p_log_flag          => l_listheader_rec.enable_log_flag,
9943                        x_return_status     => x_return_status,
9944                        x_msg_count         => x_msg_count,
9945                        x_msg_data          => x_msg_data);
9946      if x_return_status = 'E' then
9947         logger;
9948         commit;
9949         return;
9950      end if;
9951 
9952   END IF;
9953 
9954   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9955      write_to_act_log('Error in generating list', 'LIST', g_list_header_id,'HIGH');
9956      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9957   end if;
9958 
9959   IF x_return_status = FND_API.g_ret_sts_error THEN
9960      RAISE FND_API.g_exc_error;
9961   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
9962      RAISE FND_API.g_exc_unexpected_error;
9963   END IF;
9964 
9965 
9966   -- added for R12. bmuthukr
9967   if l_listheader_rec.generation_type = 'STANDARD' then
9968      if g_remote_list_gen = 'N' then
9969         write_to_act_log('Identifying duplicate records(based on party id) in the target group.','LIST',g_list_header_id,'HIGH');
9970         UPDATE ams_list_entries a
9971            SET a.enabled_flag  = 'N',
9972                a.marked_as_duplicate_flag = 'Y'
9973          WHERE a.list_header_id = p_list_header_id
9974            AND a.enabled_flag = 'Y'
9975            AND a.rowid >  (SELECT min(b.rowid)
9976                              from ams_list_entries  b
9977                             where b.list_header_id = p_list_header_id
9978                               and b.party_id = a.party_id
9979                               and b.enabled_flag = 'Y'
9980                            );
9981         write_to_act_log('No of duplicates identified.'||sql%rowcount,'LIST',g_list_header_id,'HIGH');
9982         open g_initial_count;
9983         fetch g_initial_count into g_no_of_rows_ini_selected;
9984         close g_initial_count;
9985      end if;
9986   end if;
9987 
9988   --
9989 
9990 --** DE DUPLICATION FOR TG START **--
9991    write_to_act_log('Checking if dedupe is requested for target group. ', 'LIST', p_list_header_id,'HIGH');
9992    if l_listheader_rec.generation_type = 'STANDARD' then
9993       open c1;
9994       fetch c1 into l_list_rule_id ;
9995       close c1;
9996 
9997       IF (l_list_rule_id <> 0 ) THEN                                  -- NEW logic is base on l_list_rule_id (dedupe rule)
9998          write_to_act_log('De duplication requested for this target group', 'LIST', g_list_header_id,'LOW');
9999          l_error_position := '<- de dupe ->';
10000          if g_remote_list_gen = 'N' then
10001             /* For local Target Group generation */
10002             write_to_act_log('Calling ams_listdedupe_pvt for deduplication.', 'LIST', g_list_header_id,'HIGH');
10003             l_no_of_duplicates := AMS_LISTDEDUPE_PVT.DEDUPE_LIST
10004                              (p_list_header_id               => p_list_header_id,
10005                               p_enable_word_replacement_flag => 'Y', -- l_listheader_rec.enable_word_replacement_flag,
10006                               p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
10007                               p_object_name    => 'AMS_LIST_ENTRIES');
10008             write_to_act_log('Deduplication done for target group.', 'LIST', g_list_header_id,'HIGH');
10009          else
10010             /* For Remote Target Group generation */
10011             write_to_act_log('Calling Execute_Remote_Dedupe_List for deduplication in remote instance.', 'LIST', g_list_header_id,'LOW');
10012             Execute_Remote_Dedupe_List
10013                              (p_list_header_id               => p_list_header_id,
10014                               p_enable_word_replacement_flag => 'Y', -- l_listheader_rec.enable_word_replacement_flag,
10015                               p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
10016                               p_object_name    => 'AMS_LIST_ENTRIES');
10017          end if;
10018       END IF; -- for l_list_rule_id
10019    end if;
10020    -- Call to suppresion to be done..
10021    --** DE DUPLICATION FOR TG END   **--
10022    write_to_Act_log('Generation type is '||l_listheader_rec.generation_type,'LIST',g_list_header_id,'LOW');
10023    if l_listheader_rec.generation_type in ('STANDARD','INCREMENTAL') then
10024       open c_check_supp;
10025       fetch c_check_supp into l_supp_flag;
10026       close c_check_supp;
10027       write_to_Act_log('Suppression flag is '||l_supp_flag,'LIST',g_list_header_id,'LOW');
10028       if nvl(l_supp_flag,'N') = 'Y' then
10029          write_to_Act_log('Calling suppression api','LIST',g_list_header_id,'LOW');
10030          ams_act_list_pvt.check_supp(p_list_used_by    =>  l_listheader_rec.arc_list_used_by,
10031                                      p_list_used_by_id =>  l_listheader_rec.list_used_by_id,
10032                                      p_list_header_id  =>  l_listheader_rec.list_header_id,
10033   				     x_return_status   =>  x_return_status,
10034                                      x_msg_count       =>  x_msg_count,
10035                                      x_msg_data        =>  x_msg_data);
10036 
10037       end if;
10038 
10039       if nvl(g_remote_list_gen,'N') = 'N' then
10040          write_to_Act_log('Calling apply_size_reduction procedure','LIST',g_list_header_id,'LOW');
10041          AMS_List_Options_Pvt.apply_size_reduction(p_list_header_id => g_list_header_id ,
10042                                                    p_log_level      => g_log_level,
10043                                                    p_msg_tbl        => g_msg_tbl_opt,
10044                                                    x_return_status  => x_return_status,
10045                                                    x_msg_count      => x_msg_count,
10046                                                    x_msg_data       => x_msg_data);
10047          if g_msg_tbl_opt.count > 0 then
10048             for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
10049 	    loop
10050 	       write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
10051                g_message_table(g_count) := g_msg_tbl_opt(I);
10052                g_date(g_count) := sysdate;
10053                g_count   := g_count + 1;
10054             end loop;
10055             g_msg_tbl_opt.delete;
10056          end if;
10057          write_to_Act_log('apply_size_reduction procedure executed successfully','LIST',g_list_header_id,'LOW');
10058       else
10059          write_to_Act_log('Calling apply_size_reduction procedure in the remote database','LIST',g_list_header_id,'LOW');
10060          execute immediate
10061          'BEGIN
10062           AMS_LIST_OPTIONS_PVT.apply_size_reduction'||'@'||g_database_link||'(:1,:2,:3,:4,:5)'||';'||
10063          ' END;'
10064          using g_list_header_id,
10065          'NULL',
10066 	 out x_return_status,
10067          out x_msg_count,
10068          out x_msg_data;
10069          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10070             write_to_act_log('Error while executing apply_size_reduction in the remote database.', 'LIST', g_list_header_id,'HIGH');
10071             write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10072          else
10073             write_to_act_log('apply_size_reduction procedure executed successfully in the remote database', 'LIST', g_list_header_id,'HIGH');
10074          end if;
10075       end if;
10076    end if;
10077 
10078    -- Call to CG
10079    if l_listheader_rec.generation_type = 'STANDARD' then
10080       --if nvl(l_listheader_rec.generate_control_group_flag,'N') <> 'N' then--for now will have to change it later lpo
10081          if nvl(g_remote_list_gen,'N') = 'N' then
10082             write_to_Act_log('Calling control_group_generation procedure ','LIST',g_list_header_id,'LOW');
10083 	    AMS_List_Options_Pvt.Control_Group_Generation(p_list_header_id  => g_list_header_id,
10084   	                                                  p_log_level       => g_log_level,
10085                                                           p_msg_tbl         => g_msg_tbl_opt,
10086                                                           x_ctrl_grp_status => l_ctrl_grp_status,
10087 		                                          x_return_status   => x_return_status,
10088                                                           x_msg_count       => x_msg_count,
10089                                                           x_msg_data        => x_msg_data);
10090 
10091             if g_msg_tbl_opt.count > 0 then
10092                for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
10093 	       loop
10094    	          write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
10095                   --g_message_table(g_count) := g_msg_tbl_opt(I);
10096                   --g_date(g_count) := sysdate;
10097                   --g_count   := g_count + 1;
10098                end loop;
10099 	       g_msg_tbl_opt.delete;
10100             end if;
10101             write_to_act_log('Control group generated successfully', 'LIST', g_list_header_id,'HIGH');
10102          else
10103             write_to_Act_log('Calling control_group_generation procedure in the remote database','LIST',g_list_header_id,'LOW');
10104             execute immediate
10105             'BEGIN
10106             AMS_LIST_OPTIONS_PVT.Control_Group_Generation'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6)'||';'||
10107             ' END;'
10108             using g_list_header_id,
10109                   'NULL',
10110                   out l_ctrl_grp_status,
10111                   out x_return_status,
10112 		  out x_msg_count,
10113                   out x_msg_data;
10114          end if;
10115          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10116             write_to_act_log('Error while generating control group in the remote database.', 'LIST', g_list_header_id,'HIGH');
10117             write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10118          end if;
10119 
10120       --end if;
10121    end if;
10122 
10123 
10124    if g_remote_list = 'Y' then
10125        -- ------------------------------------------------
10126        write_to_act_log(p_msg_data => 'Checking Master/Child datasource fields are mapped with TCA fields.',
10127                                        p_arc_log_used_by => 'LIST',
10128                                        p_log_used_by_id  => p_list_header_id,
10129 	  			       p_level => 'LOW');
10130        open  c_master_ds_tca_mapped;
10131        fetch c_master_ds_tca_mapped into l_tca_field_mapped;
10132        close c_master_ds_tca_mapped;
10133 
10134        open  c_child_ds_tca_mapped;
10135        fetch c_child_ds_tca_mapped into l_tca_field_mapped;
10136        close c_child_ds_tca_mapped;
10137 
10138        if l_tca_field_mapped is NULL THEN
10139           write_to_act_log(p_msg_data => 'Master/Child datasource fields are NOT mapped with TCA fields. Aborting target group generation.',
10140                    p_arc_log_used_by => 'LIST',
10141                    p_log_used_by_id  => p_list_header_id,
10142 		   p_level=>'HIGH');
10143           write_to_act_log(p_msg_data => 'Deleting entries in the remote instance. Calling remote procedure with process type as DELETE_LIST_ENTRIES.' ,
10144                       p_arc_log_used_by => 'LIST',
10145                       p_log_used_by_id  => p_list_header_id,
10146 		      p_level=>'LOW');
10147           execute immediate
10148           'BEGIN
10149 	   AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10150 	  ' END;'
10151 	      using  '1',
10152               l_null,
10153               'T',
10154               l_null,
10155               OUT x_return_status,
10156               OUT x_msg_count,
10157               OUT x_msg_data,
10158               p_list_header_id,
10159               l_null,
10160               l_null,
10161               OUT l_total_recs,
10162               'DELETE_LIST_ENTRIES';
10163 
10164           if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10165              write_to_act_log('Error in executing remote procedure for deleting target group entries', 'LIST', g_list_header_id,'HIGH');
10166              write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10167           end if;
10168 
10169           UPDATE ams_list_headers_all
10170              SET last_generation_success_flag = 'N',
10171 	         NO_OF_ROWS_ACTIVE            = null,
10172 	         NO_OF_ROWS_IN_LIST           = null,
10173                  status_code                  = 'FAILED',
10174                  user_status_id               = 311,
10175                  status_date                  = sysdate,
10176                  last_update_date             = sysdate,
10177                  main_gen_end_time            = sysdate,
10178                  ctrl_status_code             = 'DRAFT',
10179   	         no_of_rows_in_ctrl_group     = null
10180            WHERE list_header_id               = p_list_header_id;
10181     -- calling logging program
10182       logger;
10183      RETURN;
10184 end if;
10185 
10186   -- ------------------------------------------------
10187 
10188        open c_hd_status;
10189        fetch c_hd_status into l_hd_status;
10190        close c_hd_status;
10191 
10192 /*********************** changed by savio per bug 3817650 *************/
10193 /* if target group has been generatad locally there is no need to migrate it */
10194 
10195       if g_remote_list_gen = 'Y' then
10196        write_to_act_log('Status of the list before migrating to local instance : '||l_hd_status, 'LIST', p_list_header_id,'LOW');
10197        migrate_lists(p_list_header_id);
10198        write_to_act_log('List migrated to local instance.', 'LIST', p_list_header_id,'LOW');
10199       end if;
10200 /*********************** changed by savio per bug 3817650 *************/
10201 
10202 
10203 
10204        open c_hd_status;
10205        fetch c_hd_status into l_hd_status;
10206        close c_hd_status;
10207 	if l_hd_status = 'AVAILABLE'  then
10208        UPDATE ams_list_headers_all
10209        SET     status_code      = 'GENERATING',
10210                user_status_id   = 302
10211        WHERE  list_header_id    = p_list_header_id;
10212     -- Added for cancel list gen as it prevents parallel update- Raghu
10213     -- of list headers when cancel button is pressed
10214       commit;
10215 
10216     end if;
10217 
10218        open c_entry_count;
10219        fetch c_entry_count into l_entry_count;
10220        close c_entry_count;
10221 
10222        write_to_act_log('No of entries after migration is '||to_char(l_entry_count), 'LIST', p_list_header_id,'LOW');
10223        if l_listheader_rec.generation_type <> 'UPD' then
10224           write_to_act_log('Calling tca_upload_process to upload data in TCA.','LIST', p_list_header_id,'HIGH');
10225           tca_upload_process
10226                 (p_list_header_id  ,
10227                  'Y',      -- p_log_flag,
10228                  x_return_status ,
10229                  x_msg_count     ,
10230                  x_msg_data      );
10231        end if;
10232        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10233           write_to_act_log('Error while uploading data in TCA.', 'LIST', g_list_header_id,'HIGH');
10234           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10235        else
10236           write_to_act_log('TCA upload process completed successfully', 'LIST', g_list_header_id,'HIGH');
10237        end if;
10238 
10239     END if;
10240     --Code movded from tg api.
10241 /*    UPDATE ams_list_entries set
10242            source_code = l_source_code    ,
10243            arc_list_used_by_source = p_list_used_by ,
10244            source_code_for_id = p_list_used_by_id
10245      where list_header_id = g_list_header_id ;
10246 
10247    AMS_LISTGENERATION_PKG.Update_List_Dets(g_list_header_id ,x_return_status ) ;
10248   */
10249   -- calling logging program
10250   write_to_act_log('Execution of procedure generate_target_group completed. Target group generated successfully ','LIST', g_list_header_id, 'HIGH');
10251   --logger;  -- will be called from ams_act_list_pvt proc.
10252   --
10253   -- END of API body.
10254   --
10255   -- Standard check of p_commit.
10256 
10257   IF FND_API.To_Boolean ( p_commit ) THEN
10258      COMMIT WORK;
10259   END IF;
10260 
10261   -- Success Message
10262   -- MMSG
10263   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
10264   --THEN
10265   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
10266   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
10267   FND_MSG_PUB.Add;
10268   --END IF;
10269 
10270 
10271   x_return_status := FND_API.G_RET_STS_SUCCESS;
10272   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH)
10273   --THEN
10274   FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10275   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List: END');
10276   FND_MSG_PUB.Add;
10277   --END IF;
10278       FND_MSG_PUB.count_and_get(
10279             p_encoded => FND_API.g_false,
10280             p_count   => x_msg_count,
10281             p_data    => x_msg_data
10282       );
10283 
10284 EXCEPTION
10285    WHEN FND_API.G_EXC_ERROR THEN
10286   -- calling logging program
10287   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10288 
10289         UPDATE ams_list_headers_all
10290         SET    last_generation_success_flag = 'N',
10291                status_code                  = 'FAILED',
10292                user_status_id               = 311,
10293                status_date                  = sysdate,
10294                last_update_date             = sysdate,
10295                main_gen_end_time            = sysdate,
10296                ctrl_status_code             = 'DRAFT',
10297 	       no_of_rows_in_ctrl_group     = null
10298         WHERE  list_header_id               = p_list_header_id;
10299 
10300         logger;
10301 
10302      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List: '||
10303            --l_error_position||sqlerrm||sqlcode);
10304      -- Check if reset of the status is required
10305      x_return_status := FND_API.G_RET_STS_ERROR ;
10306       FND_MSG_PUB.count_and_get(
10307             p_encoded => FND_API.g_false,
10308             p_count   => x_msg_count,
10309             p_data    => x_msg_data);
10310 
10311   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10312   -- calling logging program
10313   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10314 
10315         UPDATE ams_list_headers_all
10316         SET    last_generation_success_flag = 'N',
10317                status_code                  = 'FAILED',
10318                user_status_id               = 311,
10319                last_update_date             = sysdate,
10320                status_date                  = sysdate,
10321                main_gen_end_time            = sysdate,
10322                ctrl_status_code             = 'DRAFT',
10323 	       no_of_rows_in_ctrl_group     = null
10324         WHERE  list_header_id               = p_list_header_id;
10325         logger;
10326      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List:'||
10327                       --l_error_position||sqlerrm||sqlcode);
10328      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10329      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
10330      FND_MSG_PUB.Add;
10331      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10332       FND_MSG_PUB.count_and_get(
10333             p_encoded => FND_API.g_false,
10334             p_count   => x_msg_count,
10335             p_data    => x_msg_data);
10336 
10337   WHEN OTHERS THEN
10338   -- calling logging program
10339   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10340 
10341         UPDATE ams_list_headers_all
10342         SET    last_generation_success_flag = 'N',
10343                status_code                  = 'FAILED',
10344                user_status_id               = 311,
10345                last_update_date             = sysdate,
10346                status_date                  = sysdate,
10347                main_gen_end_time            = sysdate,
10348                ctrl_status_code             = 'DRAFT',
10349 	       no_of_rows_in_ctrl_group     = null
10350         WHERE  list_header_id               = p_list_header_id;
10351 
10352         logger;
10353 
10354      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List:'||
10355                        --l_error_position||sqlerrm||sqlcode);
10356      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10357      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
10358      FND_MSG_PUB.Add;
10359      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10360       FND_MSG_PUB.count_and_get(
10361             p_encoded => FND_API.g_false,
10362             p_count   => x_msg_count,
10363             p_data    => x_msg_data);
10364 END GENERATE_TARGET_GROUP;
10365 
10366 -- -----------------------------------------------------------------------
10367 PROCEDURE Execute_Remote_Dedupe_List
10368  (p_list_header_id        AMS_LIST_HEADERS_ALL.LIST_HEADER_ID%TYPE
10369  ,p_enable_word_replacement_flag
10370                           AMS_LIST_HEADERS_ALL.ENABLE_WORD_REPLACEMENT_FLAG%TYPE
10371  ,p_send_to_log           VARCHAR2 := 'N'
10372  ,p_object_name           VARCHAR2 := 'AMS_LIST_ENTRIES'
10373  )
10374 IS
10375     -- the set of rules associated with a list.
10376     CURSOR c_list_rules (my_list_header_id IN NUMBER)
10377     IS SELECT list_rule_id
10378        FROM ams_list_rule_usages
10379        WHERE list_header_id = my_list_header_id
10380        ORDER BY priority;
10381 
10382     -- the list of fields for the list rule which are used to generate the key.
10383     CURSOR c_rule_fields
10384            (my_list_rule_id IN
10385             ams_list_rules_all.list_rule_id%TYPE)
10386     IS
10387         SELECT field_table_name,
10388                field_column_name,
10389                substring_length,
10390                word_replacement_code
10391         FROM ams_list_rule_fields
10392         WHERE list_rule_id = my_list_rule_id;
10393 
10394     -- perform a check to see if this list has been deduped already.
10395     CURSOR c_deduped_before (my_list_header_id IN NUMBER)
10396     IS
10397         SELECT last_deduped_by_user_id
10398         FROM ams_list_headers_all
10399         WHERE list_header_id = my_list_header_id;
10400 
10401     -- get a distinct list of merge keys for the list and a
10402     -- count of the occurance of each key
10403     -- we also exclude any records where the dedupe flag is already set.
10404     CURSOR c_dedupe_keys (my_list_header_id IN NUMBER)
10405     IS
10406         SELECT DISTINCT dedupe_key, COUNT (dedupe_key)
10407         FROM ams_list_entries
10408          WHERE list_header_id = my_list_header_id
10409          GROUP BY dedupe_key;
10410 
10411 
10412     CURSOR c_minimum_rank (my_list_header_id IN NUMBER)
10413     IS
10414     SELECT min(b.rank) FROM ams_list_select_actions b
10415     WHERE b.action_used_by_id = p_list_header_id
10416     and b.arc_action_used_by = 'LIST'
10417     GROUP BY b.rank;
10418 
10419 
10420     l_sql_stmt1         VARCHAR2(10000);
10421     l_sql_stmt2         VARCHAR2(10000);
10422 
10423     l_fields            VARCHAR2(10000);
10424     l_no_of_masters     NUMBER := 0;
10425     l_list_rule_id      ams_list_rules_all.list_rule_id%TYPE;
10426     l_last_dedupe_by    ams_list_headers_all.last_deduped_by_user_id%TYPE;
10427     l_dedupe_key        ams_list_entries.dedupe_key%TYPE;
10428     l_dedupe_key_count  NUMBER;
10429     l_rank_count        NUMBER;
10430     i                   BINARY_INTEGER := 1;
10431 
10432     TYPE rule_details
10433     IS TABLE OF c_rule_fields%ROWTYPE
10434     INDEX BY BINARY_INTEGER;
10435 
10436     list_rules          rule_details;
10437     empty_list_rules    rule_details;
10438 
10439     l_null		varchar(30) := NULL;
10440     l_total_dup_recs	number;
10441     l_return_status     varchar(1);
10442     l_msg_count		number;
10443     l_msg_data		varchar(2000);
10444     l_rank              number := 0;
10445 BEGIN
10446     write_to_act_log('Executing procedure execute_remote_dedupe_list', 'LIST', p_list_header_id,'LOW');
10447     IF (p_object_name = 'AMS_LIST_ENTRIES') THEN
10448         l_sql_stmt1 := 'update ams_list_entries set dedupe_key = ';
10449     END IF;
10450 
10451     --performing check to see if this list has been deduped before.
10452     OPEN c_deduped_before (p_list_header_id);
10453     FETCH c_deduped_before INTO l_last_dedupe_by;
10454     CLOSE c_deduped_before;
10455 
10456     Write_To_Act_Log ('Dedupe started on ' ||TO_CHAR (SYSDATE, 'DD-MON-RRRR HH24:MM:SS'),'LIST',p_list_header_id,'LOW');
10457         IF  (p_enable_word_replacement_flag = 'Y') THEN
10458             Write_To_Act_Log ('Word replacement flag is set ' ,'LIST',p_list_header_id,'LOW' );
10459         END IF;
10460 
10461     -- we must ensure that this flag gets reset to NULL for the list to
10462     -- ensure accurate results.
10463     -- if a dedupe has never been perfomed then this field will contains
10464     -- NULLS and there is no
10465     -- need to perform this update
10466     IF  (l_last_dedupe_by IS NOT NULL) THEN
10467     /*    UPDATE ams_list_entries
10468            SET dedupe_key = NULL
10469          WHERE list_header_id = p_list_header_id; */
10470       write_to_act_log('Executing remote procedure with process type as DEDUPE1', 'LIST', p_list_header_id,'HIGH');
10471       execute immediate
10472       'BEGIN
10473        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10474       ' END;'
10475       using  '1',
10476              l_null,
10477              'T',
10478              l_null,
10479              OUT l_return_status,
10480              OUT l_msg_count,
10481              OUT l_msg_data,
10482              p_list_header_id,
10483              'UPDATE ams_list_entries SET dedupe_key = NULL WHERE list_header_id = '||to_char(p_list_header_id),
10484              l_null,
10485              l_null,
10486              OUT l_total_dup_recs,
10487              'DEDUPE1';
10488         if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10489            write_to_act_log('Error in executing remote procedure for dedupe.', 'LIST', p_list_header_id,'HIGH');
10490            write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10491         else
10492            write_to_act_log('Remote procedure executed successfully. Dedupe key set to null.', 'LIST', p_list_header_id,'LOW');
10493         end if;
10494 
10495     END IF;
10496 
10497     -- checking to see if there are any List Source Ranks associated
10498     -- with the List.
10499     SELECT COUNT (rank)
10500       INTO l_rank_count
10501       FROM ams_list_select_actions
10502      WHERE action_used_by_id = p_list_header_id
10503        and arc_action_used_by = 'LIST';
10504 
10505 --   Write_To_Act_Log (' # of Ranks for this list = ' ||TO_CHAR (l_rank_count),'LIST',p_list_header_id ,'LOW');
10506 
10507     --getting the list rules for the list.
10508     OPEN c_list_rules (p_list_header_id);
10509     LOOP
10510         FETCH c_list_rules INTO l_list_rule_id;
10511         Write_To_Act_Log ('List rule id is  ' ||TO_CHAR (l_list_rule_id), 'LIST',p_list_header_id,'LOW' );
10512         IF (c_list_rules%notfound) THEN
10513   	   IF  (p_send_to_log = 'Y') THEN
10514             Write_To_Act_Log ('No more rules associated with the List','LIST',p_list_header_id ,'LOW');
10515 	      NULL;
10516             END IF;
10517 
10518             IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error)
10519             THEN
10520                  FND_MESSAGE.set_name('AMS', 'AMS_LIST_NO_LIST_RULE');
10521                  FND_MSG_PUB.add;
10522             END IF;
10523 
10524             CLOSE c_list_rules;
10525             return;
10526         END IF;
10527 
10528       IF  (c_list_rules%rowcount > 1) THEN
10529           --we have more than one rule for this list
10530           --we must ensure that the key gets reset to NULL for the list to
10531           -- ensure accurate results.
10532           -- removed khung 07/07/1999
10533 /*
10534          IF (p_object_name = 'AMS_LIST_ENTRIES') THEN
10535              UPDATE ams_list_entries
10536              SET dedupe_key = NULL
10537              WHERE list_header_id = p_list_header_id
10538              AND marked_as_duplicate_flag IS NULL;
10539              COMMIT;
10540          END IF;
10541 */
10542          Write_To_Act_Log ( 'More than one rule cannot be applied to the list','LIST',p_list_header_id ,'HIGH');
10543          IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
10544             FND_MESSAGE.set_name('AMS', 'AMS_LIST_ONLY_ONE_LIST_RULE');
10545             FND_MSG_PUB.add;
10546          END IF;
10547 
10548          CLOSE c_list_rules;
10549          RETURN;
10550       END IF; -- End of if for more than 1 rule count
10551       Write_To_Act_Log ('Fetching attributes associated with this rule','LIST',p_list_header_id,'LOW' );
10552         --fetch the rule entries associated with this list.
10553         OPEN c_rule_fields (l_list_rule_id);
10554         LOOP
10555             FETCH c_rule_fields INTO
10556                          list_rules (i).field_table_name,
10557                          list_rules (i).field_column_name,
10558                          list_rules (i).substring_length,
10559                          list_rules (i).word_replacement_code;
10560             EXIT WHEN c_rule_fields%notfound;
10561 
10562             Write_To_Act_Log ('Table name is '||list_rules (i).field_table_name||' ,field column name is '||list_rules (i).field_column_name,'LIST',p_list_header_id ,'LOW');
10563             -- if the enable word replacement flag is set we construct the sql
10564             -- to call the filter word function.
10565             IF  (p_enable_word_replacement_flag = 'Y') THEN
10566                Write_To_Act_Log ('Calling replact_word procedure for word replacement','LIST',p_list_header_id,'LOW' );
10567                 l_fields :=
10568                   l_fields ||
10569                       'AMS_Remote_ListGen_PKG.replace_word(' ||
10570                       upper(list_rules (i).field_column_name) ||
10571                       ',' ||
10572                       '''' ||
10573                       list_rules (i).word_replacement_code||
10574                       '''' ||
10575                       ')' ||
10576                       '||' ||
10577                       '''' ||
10578                       '.' ||
10579                       '''' ||
10580                       '||';
10581 
10582             ELSE
10583             --no substr specified for the rule field.
10584                 IF  (list_rules (i).substring_length IS NULL)
10585                 THEN
10586                     l_fields :=
10587                       l_fields ||
10588                       'upper(' ||
10589                       list_rules (i).field_column_name ||
10590                       ')||' ||
10591                       '''' ||
10592                       '.' ||
10593                       '''' ||
10594                       '||';
10595                 ELSE
10596                     l_fields :=
10597                       l_fields ||
10598                       'upper(substr(' ||
10599                       list_rules (i).field_column_name ||
10600                       ',1,' ||
10601                       TO_CHAR (list_rules (i).substring_length) ||
10602                       '))||' ||
10603                       '''' ||
10604                       '.' ||
10605                       '''' ||
10606                       '||';
10607                 END IF;
10608             END IF;
10609 
10610             i := i + 1;
10611 
10612         END LOOP;   --c_rule_fields
10613 
10614         i := 1;   --reseting to one.
10615         list_rules := empty_list_rules;   --re-initializing because we can have many rules.
10616         CLOSE c_rule_fields;
10617 
10618         -- removing the last '.' from the string as this will cause an invalid syntax error
10619         -- in the query.
10620 
10621         l_fields := SUBSTR (l_fields, 1, LENGTH (l_fields) - 7);
10622 
10623         l_sql_stmt2 := l_sql_stmt1;
10624         l_sql_stmt2 := l_sql_stmt2 ||
10625                 l_fields ||
10626                 ' where list_header_id =' ||
10627                 TO_CHAR (p_list_header_id);
10628 
10629 
10630         Write_To_Act_Log ('SQL generated ' ||l_sql_stmt2 ,'LIST',p_list_header_id,'LOW' );
10631         --09/27/2000 vbhandar,modified Execute immediate , INTO should only be used for single row queries
10632         --notice that l_no_of_masters is not really used, left it there to keep the signature of the function
10633         --Dedupe_LIST unchanged.
10634 --         EXECUTE IMMEDIATE l_sql_stmt2;
10635 
10636       write_to_act_log('Calling remote procedure with process type as DEDUPE2', 'LIST', p_list_header_id,'LOW');
10637       execute immediate
10638       'BEGIN
10639        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10640       ' END;'
10641       using  '1',
10642              l_null,
10643              'T',
10644              l_null,
10645              OUT l_return_status,
10646              OUT l_msg_count,
10647              OUT l_msg_data,
10648              p_list_header_id,
10649              l_sql_stmt2,
10650              l_null,
10651              OUT l_total_dup_recs,
10652              'DEDUPE2';
10653     if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10654        write_to_act_log('Error in executing remote procedure with process type as DEDUPE2', 'LIST', p_list_header_id,'HIGH');
10655        write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10656     else
10657        write_to_act_log('Remote procedure executed successfully for dedupe', 'LIST', p_list_header_id,'LOW');
10658     end if;
10659 
10660       open c_minimum_rank(p_list_header_id);
10661       fetch c_minimum_rank into l_rank;
10662       close c_minimum_rank;
10663 
10664       write_to_act_log('Calling remote procedure with process type as DEDUPE3 for identifying duplicates based on rank', 'LIST', p_list_header_id,'LOW');
10665       execute immediate
10666       'BEGIN
10667       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10668       ' END;'
10669       using  '1',
10670              l_null,
10671              'T',
10672              l_null,
10673              OUT l_return_status,
10674              OUT l_msg_count,
10675              OUT l_msg_data,
10676              p_list_header_id,
10677              l_rank_count,
10678              l_rank,
10679              OUT l_total_dup_recs,
10680              'DEDUPE3';
10681         if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10682            write_to_act_log('Error in executing remote procedure with process type as DEDUPE3', 'LIST', p_list_header_id,'HIGH');
10683            write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10684         else
10685           write_to_act_log('Remote procedure executed successfully, duplicated identified based on rank', 'LIST', p_list_header_id,'LOW');
10686         end if;
10687 
10688         --initializing as we may have more than one rule.
10689 
10690         l_fields := NULL;
10691         l_sql_stmt2 := l_sql_stmt1;
10692 
10693     END LOOP;   --c_list_rules loop
10694 
10695     --recording who performed the deduplication and at what time.
10696     --recording the number of duplicates found.
10697     UPDATE ams_list_headers_all
10698        SET last_deduped_by_user_id = FND_GLOBAL.User_Id
10699            ,last_dedupe_date = SYSDATE
10700            ,no_of_rows_duplicates = l_total_dup_recs
10701      WHERE list_header_id = p_list_header_id;
10702 
10703    /*   savio removing potential multiple update statement */
10704    /*
10705 --    UPDATE ams_list_headers_all
10706 --       SET no_of_rows_duplicates = l_total_dup_recs
10707 --     WHERE list_header_id = p_list_header_id;
10708    */
10709 
10710     write_to_act_log('Updated list header table with the userid and the time','LIST',p_list_header_id, 'LOW');
10711     write_to_act_log('Procedure execute_remote_dedupe_list executed','LIST',p_list_header_id, 'LOW');
10712 
10713     COMMIT;
10714 
10715 END Execute_Remote_Dedupe_List;
10716 -- -----------------------------
10717 
10718 PROCEDURE migrate_lists_from_remote(
10719                             Errbuf          OUT NOCOPY     VARCHAR2,
10720                             Retcode         OUT NOCOPY     VARCHAR2,
10721                             p_list_header_id NUMBER
10722                             ) IS
10723 
10724 l_list_header_id	NUMBER;
10725   cursor c_migrate_list is
10726   select head.list_header_id
10727     from ams_list_headers_all head, ams_list_src_types stypes
10728    where head.status_code = 'AVAILABLE'
10729      and head.MIGRATION_DATE is NULL
10730      and head.list_source_type = stypes.source_type_code
10731      and stypes.remote_flag = 'Y';
10732 
10733 Begin
10734 
10735 Ams_Utility_Pvt.Write_Conc_log('Start migrate_lists_from_remote : ');
10736 if p_list_header_id is not null then
10737      migrate_lists( p_list_header_id ) ;
10738  else
10739   open c_migrate_list;
10740   LOOP
10741     fetch c_migrate_list into l_list_header_id;
10742     exit when c_migrate_list%notfound;
10743      migrate_lists( l_list_header_id ) ;
10744   END LOOP;
10745   close c_migrate_list;
10746 end if;
10747 commit;
10748 Ams_Utility_Pvt.Write_Conc_log('End migrate_lists_from_remote : ');
10749 EXCEPTION
10750   WHEN OTHERS THEN
10751    Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_lists_from_remote : '||SQLERRM);
10752     errbuf:= substr(SQLERRM,1,254);
10753     retcode:= 2;
10754    raise;
10755 End migrate_lists_from_remote;
10756 -- -----------------------------------------------------------------
10757 
10758 PROCEDURE migrate_lists(
10759                             p_list_header_id NUMBER
10760                             ) IS
10761 
10762 l_dblink        varchar2(100);
10763 l_start_rownum	number := 1;
10764 l_end_rownum	number := 10000;
10765 l_total_records number;
10766 l_insert_sql   varchar2(32767);
10767 l_return_status varchar2(1);
10768 l_lookup_code                         VARCHAR2(30);
10769 l_user_status_id                      NUMBER;
10770 l_no_of_chunks				number;
10771 
10772 
10773   cursor c_dblink is
10774   select database_link
10775     from ams_list_src_types stypes, ams_list_headers_all list
10776    where list.list_source_type = stypes.source_type_code
10777      and list_header_id  =  p_list_header_id;
10778 
10779 begin
10780  Ams_Utility_Pvt.Write_Conc_log('Start migrate_lists : '||to_char(p_list_header_id));
10781 
10782  open c_dblink;
10783  fetch c_dblink into l_dblink;
10784  close c_dblink;
10785  Ams_Utility_Pvt.Write_Conc_log('l_dblink : '||l_dblink);
10786 
10787  write_to_act_log('Database link is '||l_dblink,'LIST',p_list_header_id,'LOW');
10788  Ams_Utility_Pvt.Write_Conc_log('Start Delete list entries from local instance  : ');
10789  write_to_act_log('Deleting existing entries from the local instance.','LIST',p_list_header_id,'LOW');
10790   delete from ams_list_entries
10791    where list_header_id = p_list_header_id;
10792  Ams_Utility_Pvt.Write_Conc_log('End Delete list entries from local instance  : ');
10793 
10794        l_insert_sql := 'insert into ams_list_entries
10795         (list_header_id ,
10796          list_entry_id,
10797          object_version_number,
10798          source_code                     ,
10799          source_code_for_id              ,
10800          arc_list_used_by_source         ,
10801          arc_list_select_action_from     ,
10802          pin_code                        ,
10803          view_application_id             ,
10804          manually_entered_flag           ,
10805          marked_as_random_flag           ,
10806          marked_as_duplicate_flag        ,
10807          part_of_control_group_flag      ,
10808          exclude_in_triggered_list_flag  ,
10809          enabled_flag ,
10810          LIST_SELECT_ACTION_FROM_NAME,
10811          last_update_date,
10812          last_updated_by,
10813          creation_date,
10814          created_by,
10815          last_update_login,
10816          list_entry_source_system_id,
10817          list_entry_source_system_type,
10818          list_select_action_id,
10819          SUFFIX,
10820          FIRST_NAME,
10821          LAST_NAME,
10822          CUSTOMER_NAME,
10823          TITLE,
10824          ADDRESS_LINE1,
10825          ADDRESS_LINE2,
10826          CITY,
10827          STATE,
10828          ZIPCODE,
10829          COUNTRY,
10830          FAX,
10831          PHONE,
10832          EMAIL_ADDRESS,
10833          CUSTOMER_ID                              ,
10834          LIST_SOURCE                              ,
10835          PARTY_ID                                 ,
10836          PARENT_PARTY_ID                          ,
10837          IMP_SOURCE_LINE_ID                       ,
10838          COL1,
10839          COL2,
10840          COL3,
10841          COL4,
10842          COL5,
10843          COL6,
10844          COL7,
10845          COL8,
10846          COL9,
10847          COL10,
10848          COL11,
10849          COL12,
10850          COL13,
10851          COL14,
10852          COL15,
10853          COL16,
10854          COL17,
10855          COL18,
10856          COL19,
10857          COL20,
10858          COL21,
10859          COL22,
10860          COL23,
10861          COL24,
10862          COL25,
10863          COL26,
10864          COL27,
10865          COL28,
10866          COL29,
10867          COL30,
10868          COL31,
10869          COL32,
10870          COL33,
10871          COL34,
10872          COL35,
10873          COL36,
10874          COL37,
10875          COL38,
10876          COL39,
10877          COL40,
10878          COL41,
10879          COL42,
10880          COL43,
10881          COL44,
10882          COL45,
10883          COL46,
10884          COL47,
10885          COL48,
10886          COL49,
10887          COL50,
10888          COL51,
10889          COL52,
10890          COL53,
10891          COL54,
10892          COL55,
10893          COL56,
10894          COL57,
10895          COL58,
10896          COL59,
10897          COL60,
10898          COL61,
10899          COL62,
10900          COL63,
10901          COL64,
10902          COL65,
10903          COL66,
10904          COL67,
10905          COL68,
10906          COL69,
10907          COL70,
10908          COL71,
10909          COL72,
10910          COL73,
10911          COL74,
10912          COL75,
10913          COL76,
10914          COL77,
10915          COL78,
10916          COL79,
10917          COL80,
10918          COL81,
10919          COL82,
10920          COL83,
10921          COL84,
10922          COL85,
10923          COL86,
10924          COL87,
10925          COL88,
10926          COL89,
10927          COL90,
10928          COL91,
10929          COL92,
10930          COL93,
10931          COL94,
10932          COL95,
10933          COL96,
10934          COL97,
10935          COL98,
10936          COL99,
10937          COL100,
10938          COL101,
10939          COL102,
10940          COL103,
10941          COL104,
10942          COL105,
10943          COL106,
10944          COL107,
10945          COL108,
10946          COL109,
10947          COL110,
10948          COL111,
10949          COL112,
10950          COL113,
10951          COL114,
10952          COL115,
10953          COL116,
10954          COL117,
10955          COL118,
10956          COL119,
10957          COL120,
10958          COL121,
10959          COL122,
10960          COL123,
10961          COL124,
10962          COL125,
10963          COL126,
10964          COL127,
10965          COL128,
10966          COL129,
10967          COL130,
10968          COL131,
10969          COL132,
10970          COL133,
10971          COL134,
10972          COL135,
10973          COL136,
10974          COL137,
10975          COL138,
10976          COL139,
10977          COL140,
10978          COL141,
10979          COL142,
10980          COL143,
10981          COL144,
10982          COL145,
10983          COL146,
10984          COL147,
10985          COL148,
10986          COL149,
10987          COL150,
10988          COL151,
10989          COL152,
10990          COL153,
10991          COL154,
10992          COL155,
10993          COL156,
10994          COL157,
10995          COL158,
10996          COL159,
10997          COL160,
10998          COL161,
10999          COL162,
11000          COL163,
11001          COL164,
11002          COL165,
11003          COL166,
11004          COL167,
11005          COL168,
11006          COL169,
11007          COL170,
11008          COL171,
11009          COL172,
11010          COL173,
11011          COL174,
11012          COL175,
11013          COL176,
11014          COL177,
11015          COL178,
11016          COL179,
11017          COL180,
11018          COL181,
11019          COL182,
11020          COL183,
11021          COL184,
11022          COL185,
11023          COL186,
11024          COL187,
11025          COL188,
11026          COL189,
11027          COL190,
11028          COL191,
11029          COL192,
11030          COL193,
11031          COL194,
11032          COL195,
11033          COL196,
11034          COL197,
11035          COL198,
11036          COL199,
11037          COL200,
11038          COL201,
11039          COL202,
11040          COL203,
11041          COL204,
11042          COL205,
11043          COL206,
11044          COL207,
11045          COL208,
11046          COL209,
11047          COL210,
11048          COL211,
11049          COL212,
11050          COL213,
11051          COL214,
11052          COL215,
11053          COL216,
11054          COL217,
11055          COL218,
11056          COL219,
11057          COL220,
11058          COL221,
11059          COL222,
11060          COL223,
11061          COL224,
11062          COL225,
11063          COL226,
11064          COL227,
11065          COL228,
11066          COL229,
11067          COL230,
11068          COL231,
11069          COL232,
11070          COL233,
11071          COL234,
11072          COL235,
11073          COL236,
11074          COL237,
11075          COL238,
11076          COL239,
11077          COL240,
11078          COL241,
11079          COL242,
11080          COL243,
11081          COL244,
11082          COL245,
11083          COL246,
11084          COL247,
11085          COL248,
11086          COL249,
11087          COL250 ,
11088          COL251     ,
11089          COL252     ,
11090          COL253     ,
11091          COL254     ,
11092          COL256     ,
11093          COL255     ,
11094          COL257     ,
11095          COL258     ,
11096          COL259     ,
11097          COL260     ,
11098          COL261     ,
11099          COL262     ,
11100          COL263     ,
11101          COL264     ,
11102          COL265     ,
11103          COL266     ,
11104          COL267     ,
11105          COL268     ,
11106          COL269     ,
11107          COL270     ,
11108          COL271     ,
11109          COL272     ,
11110          COL273     ,
11111          COL274     ,
11112          COL275     ,
11113          COL276     ,
11114          COL277     ,
11115          COL278     ,
11116          COL279     ,
11117          COL280     ,
11118          COL281     ,
11119          COL282     ,
11120          COL283     ,
11121          COL284     ,
11122          COL285     ,
11123          COL286     ,
11124          COL287     ,
11125          COL288     ,
11126          COL289     ,
11127          COL290     ,
11128          COL291     ,
11129          COL292     ,
11130          COL293     ,
11131          COL294     ,
11132          COL295     ,
11133          COL296     ,
11134          COL297     ,
11135          COL298     ,
11136          COL299     ,
11137          COL300     ,
11138 GROUP_CODE,
11139 NEWLY_UPDATED_FLAG,
11140 OUTCOME_ID,
11141 RESULT_ID,
11142 REASON_ID,
11143 NOTES,
11144 VEHICLE_RESPONSE_CODE,
11145 SALES_AGENT_EMAIL_ADDRESS,
11146 RESOURCE_ID,
11147 LOCATION_ID,
11148 CONTACT_POINT_ID,
11149 ORIG_SYSTEM_REFERENCE,
11150 MARKED_AS_FATIGUED_FLAG,
11151 MARKED_AS_SUPPRESSED_FLAG,
11152 REMOTE_LIST_ENTRY_ID,
11153 -- ERROR_TEXT,
11154 -- ERROR_FLAG,
11155 LAST_CONTACTED_DATE,
11156 CUSTOM_COLUMN1,
11157 CUSTOM_COLUMN2,
11158 CUSTOM_COLUMN3,
11159 CUSTOM_COLUMN4,
11160 CUSTOM_COLUMN5,
11161 CUSTOM_COLUMN6,
11162 CUSTOM_COLUMN7,
11163 CUSTOM_COLUMN8,
11164 CUSTOM_COLUMN9,
11165 CUSTOM_COLUMN10,
11166 CUSTOM_COLUMN11,
11167 CUSTOM_COLUMN12,
11168 CUSTOM_COLUMN13,
11169 CUSTOM_COLUMN14,
11170 CUSTOM_COLUMN15,
11171 CUSTOM_COLUMN16,
11172 CUSTOM_COLUMN17,
11173 CUSTOM_COLUMN18,
11174 CUSTOM_COLUMN19,
11175 CUSTOM_COLUMN20,
11176 CUSTOM_COLUMN21,
11177 CUSTOM_COLUMN22,
11178 CUSTOM_COLUMN23,
11179 CUSTOM_COLUMN24,
11180 CUSTOM_COLUMN25,
11181 RANK
11182         )
11183 	SELECT
11184          list_header_id,
11185          ams_list_entries_s.nextval,
11186          object_version_number,
11187          source_code                     ,
11188          source_code_for_id              ,
11189          arc_list_used_by_source         ,
11190          arc_list_select_action_from     ,
11191          pin_code                        ,
11192          view_application_id             ,
11193          manually_entered_flag           ,
11194          marked_as_random_flag           ,
11195          marked_as_duplicate_flag        ,
11196          part_of_control_group_flag      ,
11197          exclude_in_triggered_list_flag  ,
11198          enabled_flag ,
11199          LIST_SELECT_ACTION_FROM_NAME,
11200          last_update_date,
11201          last_updated_by,
11202          creation_date,
11203          created_by,
11204          last_update_login,
11205          list_entry_source_system_id,
11206          list_entry_source_system_type,
11207          list_select_action_id,
11208          SUFFIX,
11209          FIRST_NAME,
11210          LAST_NAME,
11211          CUSTOMER_NAME,
11212          TITLE,
11213          ADDRESS_LINE1,
11214          ADDRESS_LINE2,
11215          CITY,
11216          STATE,
11217          ZIPCODE,
11218          COUNTRY,
11219          FAX,
11220          PHONE,
11221          EMAIL_ADDRESS,
11222          CUSTOMER_ID                              ,
11223          LIST_SOURCE                              ,
11224          null,  -- PARTY_ID                                 ,
11225          null,  -- PARENT_PARTY_ID                          ,
11226          IMP_SOURCE_LINE_ID                       ,
11227          COL1,
11228          COL2,
11229          COL3,
11230          COL4,
11231          COL5,
11232          COL6,
11233          COL7,
11234          COL8,
11235          COL9,
11236          COL10,
11237          COL11,
11238          COL12,
11239          COL13,
11240          COL14,
11241          COL15,
11242          COL16,
11243          COL17,
11244          COL18,
11245          COL19,
11246          COL20,
11247          COL21,
11248          COL22,
11249          COL23,
11250          COL24,
11251          COL25,
11252          COL26,
11253          COL27,
11254          COL28,
11255          COL29,
11256          COL30,
11257          COL31,
11258          COL32,
11259          COL33,
11260          COL34,
11261          COL35,
11262          COL36,
11263          COL37,
11264          COL38,
11265          COL39,
11266          COL40,
11267          COL41,
11268          COL42,
11269          COL43,
11270          COL44,
11271          COL45,
11272          COL46,
11273          COL47,
11274          COL48,
11275          COL49,
11276          COL50,
11277          COL51,
11278          COL52,
11279          COL53,
11280          COL54,
11281          COL55,
11282          COL56,
11283          COL57,
11284          COL58,
11285          COL59,
11286          COL60,
11287          COL61,
11288          COL62,
11289          COL63,
11290          COL64,
11291          COL65,
11292          COL66,
11293          COL67,
11294          COL68,
11295          COL69,
11296          COL70,
11297          COL71,
11298          COL72,
11299          COL73,
11300          COL74,
11301          COL75,
11302          COL76,
11303          COL77,
11304          COL78,
11305          COL79,
11306          COL80,
11307          COL81,
11308          COL82,
11309          COL83,
11310          COL84,
11311          COL85,
11312          COL86,
11313          COL87,
11314          COL88,
11315          COL89,
11316          COL90,
11317          COL91,
11318          COL92,
11319          COL93,
11320          COL94,
11321          COL95,
11322          COL96,
11323          COL97,
11324          COL98,
11325          COL99,
11326          COL100,
11327          COL101,
11328          COL102,
11329          COL103,
11330          COL104,
11331          COL105,
11332          COL106,
11333          COL107,
11334          COL108,
11335          COL109,
11336          COL110,
11337          COL111,
11338          COL112,
11339          COL113,
11340          COL114,
11341          COL115,
11342          COL116,
11343          COL117,
11344          COL118,
11345          COL119,
11346          COL120,
11347          COL121,
11348          COL122,
11349          COL123,
11350          COL124,
11351          COL125,
11352          COL126,
11353          COL127,
11354          COL128,
11355          COL129,
11356          COL130,
11357          COL131,
11358          COL132,
11359          COL133,
11360          COL134,
11361          COL135,
11362          COL136,
11363          COL137,
11364          COL138,
11365          COL139,
11366          COL140,
11367           COL141,
11368           COL142,
11369           COL143,
11370           COL144,
11371           COL145,
11372           COL146,
11373           COL147,
11374           COL148,
11375           COL149,
11376           COL150,
11377           COL151,
11378           COL152,
11379           COL153,
11380           COL154,
11381           COL155,
11382           COL156,
11383           COL157,
11384           COL158,
11385           COL159,
11386           COL160,
11387           COL161,
11388           COL162,
11389           COL163,
11390           COL164,
11391           COL165,
11392           COL166,
11393           COL167,
11394           COL168,
11395           COL169,
11396           COL170,
11397           COL171,
11398           COL172,
11399           COL173,
11400           COL174,
11401           COL175,
11402           COL176,
11403           COL177,
11404           COL178,
11405           COL179,
11406           COL180,
11407           COL181,
11408           COL182,
11409           COL183,
11410           COL184,
11411           COL185,
11412           COL186,
11413           COL187,
11414           COL188,
11415           COL189,
11416           COL190,
11417           COL191,
11418           COL192,
11419           COL193,
11420           COL194,
11421           COL195,
11422           COL196,
11423           COL197,
11424           COL198,
11425           COL199,
11426           COL200,
11427           COL201,
11428           COL202,
11429           COL203,
11430           COL204,
11431           COL205,
11432           COL206,
11433           COL207,
11434           COL208,
11435           COL209,
11436           COL210,
11437           COL211,
11438           COL212,
11439           COL213,
11440           COL214,
11441           COL215,
11442           COL216,
11443           COL217,
11444           COL218,
11445           COL219,
11446           COL220,
11447           COL221,
11448           COL222,
11449           COL223,
11450           COL224,
11451           COL225,
11452           COL226,
11453           COL227,
11454           COL228,
11455           COL229,
11456           COL230,
11457           COL231,
11458           COL232,
11459           COL233,
11460           COL234,
11461           COL235,
11462           COL236,
11463           COL237,
11464           COL238,
11465           COL239,
11466           COL240,
11467           COL241,
11468           COL242,
11469           COL243,
11470           COL244,
11471           COL245,
11472           COL246,
11473           COL247,
11474           COL248,
11475           COL249,
11476           COL250 ,
11477           COL251 ,
11478           COL252 ,
11479           COL253 ,
11480           COL254 ,
11481           COL256 ,
11482           COL255 ,
11483           COL257 ,
11484           COL258 ,
11485           COL259 ,
11486           COL260 ,
11487           COL261 ,
11488           COL262 ,
11489           COL263 ,
11490           COL264 ,
11491           COL265 ,
11492           COL266 ,
11493           COL267 ,
11494           COL268 ,
11495           COL269 ,
11496           COL270 ,
11497           COL271 ,
11498           COL272 ,
11499           COL273 ,
11500           COL274 ,
11501           COL275 ,
11502           COL276 ,
11503           COL277 ,
11504           COL278 ,
11505           COL279 ,
11506           COL280 ,
11507           COL281 ,
11508           COL282 ,
11509           COL283 ,
11510           COL284 ,
11511           COL285 ,
11512           COL286 ,
11513           COL287 ,
11514           COL288 ,
11515           COL289 ,
11516           COL290 ,
11517           COL291 ,
11518           COL292 ,
11519           COL293 ,
11520           COL294 ,
11521           COL295 ,
11522           COL296 ,
11523           COL297 ,
11524           COL298 ,
11525           COL299 ,
11526           COL300 ,
11527 GROUP_CODE,
11528 NEWLY_UPDATED_FLAG,
11529 OUTCOME_ID,
11530 RESULT_ID,
11531 REASON_ID,
11532 NOTES,
11533 VEHICLE_RESPONSE_CODE,
11534 SALES_AGENT_EMAIL_ADDRESS,
11535 RESOURCE_ID,
11536 LOCATION_ID,
11537 CONTACT_POINT_ID,
11538 ORIG_SYSTEM_REFERENCE,
11539 MARKED_AS_FATIGUED_FLAG,
11540 MARKED_AS_SUPPRESSED_FLAG,
11541 LIST_ENTRY_ID,
11542 -- ERROR_TEXT,
11543 -- ERROR_FLAG,
11544 LAST_CONTACTED_DATE,
11545 CUSTOM_COLUMN1,
11546 CUSTOM_COLUMN2,
11547 CUSTOM_COLUMN3,
11548 CUSTOM_COLUMN4,
11549 CUSTOM_COLUMN5,
11550 CUSTOM_COLUMN6,
11551 CUSTOM_COLUMN7,
11552 CUSTOM_COLUMN8,
11553 CUSTOM_COLUMN9,
11554 CUSTOM_COLUMN10,
11555 CUSTOM_COLUMN11,
11556 CUSTOM_COLUMN12,
11557 CUSTOM_COLUMN13,
11558 CUSTOM_COLUMN14,
11559 CUSTOM_COLUMN15,
11560 CUSTOM_COLUMN16,
11561 CUSTOM_COLUMN17,
11562 CUSTOM_COLUMN18,
11563 CUSTOM_COLUMN19,
11564 CUSTOM_COLUMN20,
11565 CUSTOM_COLUMN21,
11566 CUSTOM_COLUMN22,
11567 CUSTOM_COLUMN23,
11568 CUSTOM_COLUMN24,
11569 CUSTOM_COLUMN25,
11570 RANK
11571        from ams_list_entries@'||l_dblink||'  '||
11572        'where   list_header_id = ' ||to_char(p_list_header_id);
11573 --       ||' and rownum between '||to_char(l_start_rownum)||' and '||to_char(l_end_rownum);
11574  l_no_of_chunks := 0;
11575  l_no_of_chunks  := ceil(length(l_insert_sql)/2000 );
11576  write_to_act_log('Insert statement formed for migrating the list is..','LIST',p_list_header_id,'LOW');
11577  for i in 1 ..l_no_of_chunks
11578  loop
11579       Ams_Utility_Pvt.Write_Conc_log('l_insert_sql = '||substrb(l_insert_sql,(2000*i) - 1999,2000));
11580  end loop;
11581  execute immediate l_insert_sql;
11582  commit;
11583 /*
11584       l_start_rownum := l_start_rownum + 10000;
11585       l_end_rownum   := l_end_rownum   + 10000;
11586     END LOOP;
11587 */
11588  Ams_Utility_Pvt.Write_Conc_log('End migrate_lists : '||to_char(p_list_header_id));
11589          SELECT user_status_id into l_user_status_id FROM ams_user_statuses_vl
11590          WHERE system_status_type = 'AMS_LIST_STATUS' AND
11591          system_status_code = 'AVAILABLE' and default_flag = 'Y';
11592          UPDATE ams_list_headers_all
11593          set status_code      = 'AVAILABLE',
11594             user_status_id    =  l_user_status_id,
11595             migration_date = sysdate
11596          where list_header_id = p_list_header_id;
11597      write_to_act_log('List migrated to local instance. Updating list header details.','LIST',p_list_header_id,'HIGH');
11598      -- Added for cancel list gen as it prevents parallel update- Raghu
11599      -- of list headers when cancel button is pressed
11600      commit;
11601 
11602  Update_List_Dets(p_list_header_id,l_return_status);
11603  if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
11604     write_to_act_log('Error while updating list header details.', 'LIST', g_list_header_id,'HIGH');
11605  end if;
11606 
11607  Ams_Utility_Pvt.Write_Conc_log('After Update_List_Dets call : ');
11608 
11609   IF(l_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
11610      RAISE FND_API.G_EXC_ERROR;
11611   END IF;
11612 EXCEPTION
11613   WHEN OTHERS THEN
11614         UPDATE ams_list_headers_all
11615         SET    last_generation_success_flag = 'N',
11616                status_code                  = 'FAILED',
11617                user_status_id               = 311,
11618                last_update_date             = sysdate,
11619                status_date                  = sysdate,
11620                main_gen_end_time            = sysdate
11621         WHERE  list_header_id               = p_list_header_id;
11622    write_to_act_log('Error while migrating list from remote instance. '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
11623      -- Added for cancel list gen as it prevents parallel update- Raghu
11624      -- of list headers when cancel button is pressed
11625      commit;
11626 
11627      Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_lists : '||SQLERRM);
11628   --  errbuf:= substr(SQLERRM,1,254);
11629   --  retcode:= 2;
11630    raise;
11631 end migrate_lists;
11632 -- --------------------------------------------
11633 PROCEDURE migrate_word_replacements(
11634                             Errbuf          OUT NOCOPY     VARCHAR2,
11635                             Retcode         OUT NOCOPY     VARCHAR2,
11636                             dblink          VARCHAR2
11637                             ) IS
11638 
11639 l_insert_sql   varchar2(32767);
11640 l_delete_sql   varchar2(32767);
11641 begin
11642 Ams_Utility_Pvt.Write_Conc_log('Start migrate_word_replacements : ');
11643 l_delete_sql := 'Delete from AMS_HZ_WORD_REPLACEMENTS@'||dblink;
11644 l_insert_sql := 'Insert into AMS_HZ_WORD_REPLACEMENTS@'||dblink||' (
11645 			ORIGINAL_WORD,
11646 			REPLACEMENT_WORD,
11647 			TYPE,
11648 			COUNTRY_CODE,
11649 			LAST_UPDATE_DATE,
11650 			LAST_UPDATED_BY,
11651 			CREATION_DATE,
11652 			CREATED_BY,
11653 			LAST_UPDATE_LOGIN,
11654 			ATTRIBUTE_CATEGORY,
11655 			ATTRIBUTE1,
11656 			ATTRIBUTE2,
11657 			ATTRIBUTE3,
11658 			ATTRIBUTE4,
11659 			ATTRIBUTE5,
11660 			ATTRIBUTE6,
11661 			ATTRIBUTE7,
11662 			ATTRIBUTE8,
11663 			ATTRIBUTE9,
11664 			ATTRIBUTE10,
11665 			ATTRIBUTE11,
11666 			ATTRIBUTE12,
11667 			ATTRIBUTE13,
11668 			ATTRIBUTE14,
11669 			ATTRIBUTE15,
11670 			WORD_LIST_ID,
11671 			OBJECT_VERSION_NUMBER
11672 		)
11673 		 SELECT
11674                         ORIGINAL_WORD,
11675                         REPLACEMENT_WORD,
11676                         TYPE,
11677                         COUNTRY_CODE,
11678                         LAST_UPDATE_DATE,
11679                         LAST_UPDATED_BY,
11680                         CREATION_DATE,
11681                         CREATED_BY,
11682                         LAST_UPDATE_LOGIN,
11683                         ATTRIBUTE_CATEGORY,
11684                         ATTRIBUTE1,
11685                         ATTRIBUTE2,
11686                         ATTRIBUTE3,
11687                         ATTRIBUTE4,
11688                         ATTRIBUTE5,
11689                         ATTRIBUTE6,
11690                         ATTRIBUTE7,
11691                         ATTRIBUTE8,
11692                         ATTRIBUTE9,
11693                         ATTRIBUTE10,
11694                         ATTRIBUTE11,
11695                         ATTRIBUTE12,
11696                         ATTRIBUTE13,
11697                         ATTRIBUTE14,
11698                         ATTRIBUTE15,
11699                         WORD_LIST_ID,
11700                         OBJECT_VERSION_NUMBER
11701                 FROM  	HZ_WORD_REPLACEMENTS ';
11702 Ams_Utility_Pvt.Write_Conc_log('l_delete_sql =  '||l_delete_sql);
11703 	execute immediate l_delete_sql;
11704 	commit;
11705 Ams_Utility_Pvt.Write_Conc_log('l_insert_sql =  '||l_insert_sql);
11706 	execute immediate l_insert_sql;
11707         commit;
11708 Ams_Utility_Pvt.Write_Conc_log('End migrate_word_replacements : ');
11709 EXCEPTION
11710   WHEN OTHERS THEN
11711    Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_word_replacements : '||SQLERRM);
11712     errbuf:= substr(SQLERRM,1,254);
11713     retcode:= 2;
11714    raise;
11715 end migrate_word_replacements;
11716 -- --------------------------------------------------
11717 PROCEDURE UPDATE_FOR_TRAFFIC_COP( p_list_header_id      in number ,
11718                                   p_list_entry_id       in t_number ) IS
11719 l_return_status varchar2(1);
11720 l_fatigued_records      number;
11721 l_no_of_rows_active     number;
11722 
11723 cursor c_count_list_entries(cur_p_list_header_id number) is
11724 select sum(decode(enabled_flag,'Y',1,0)),
11725        sum(decode(marked_as_fatigued_flag,'Y',1,0))
11726 from ams_list_entries
11727 where list_header_id = cur_p_list_header_id ;
11728 
11729 
11730 begin
11731 
11732              AMS_Utility_PVT.Create_Log (
11733               x_return_status   => l_return_status,
11734               p_arc_log_used_by => 'LIST',
11735               p_log_used_by_id  => p_list_header_id,
11736               p_msg_data        => 'UPDATE_FOR_TRAFFIC_COP : Started ',
11737               p_msg_type        => 'DEBUG');
11738 
11739   FORALL I in p_list_entry_id.first .. p_list_entry_id.last
11740      UPDATE ams_list_entries
11741      SET    ENABLED_FLAG            = 'N',
11742             MARKED_AS_FATIGUED_FLAG = 'Y'
11743     WHERE  list_entry_id = p_list_entry_id(i)
11744       AND  list_header_id = p_list_header_id ;
11745 
11746   open c_count_list_entries(p_list_header_id);
11747   fetch c_count_list_entries
11748    into l_no_of_rows_active            ,
11749         l_fatigued_records      ;
11750   close c_count_list_entries;
11751 
11752     UPDATE ams_list_headers_all
11753      SET    NO_OF_ROWS_FATIGUED = l_fatigued_records,
11754             NO_OF_ROWS_ACTIVE = l_no_of_rows_active
11755     WHERE  list_header_id = p_list_header_id ;
11756  -- Added for cancel list gen as it prevents parallel update- Raghu
11757  -- of list headers when cancel button is pressed
11758  commit;
11759 
11760              AMS_Utility_PVT.Create_Log (
11761               x_return_status   => l_return_status,
11762               p_arc_log_used_by => 'LIST',
11763               p_log_used_by_id  => p_list_header_id,
11764               p_msg_data        => 'UPDATE_FOR_TRAFFIC_COP : End ',
11765               p_msg_type        => 'DEBUG');
11766 
11767 EXCEPTION
11768   WHEN OTHERS THEN
11769              AMS_Utility_PVT.Create_Log (
11770               x_return_status   => l_return_status,
11771               p_arc_log_used_by => 'LIST',
11772               p_log_used_by_id  => p_list_header_id,
11773               p_msg_data        => sqlerrm,
11774               p_msg_type        => 'DEBUG');
11775 
11776 end UPDATE_FOR_TRAFFIC_COP;
11777 
11778 -- --------------------------------------------------
11779 -- **********************************************************************************************
11780 PROCEDURE calc_selection_running_total
11781              (p_action_used_by_id  in  number,
11782               p_action_used_by     in  varchar2  ,-- DEFAULT 'LIST',
11783               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
11784               x_return_status      OUT NOCOPY VARCHAR2,
11785               x_msg_count          OUT NOCOPY NUMBER,
11786               x_msg_data           OUT NOCOPY VARCHAR2) IS
11787 l_request_id	number;
11788 l_msg_count number := 0;
11789 
11790 begin
11791   IF(p_log_flag ='Y')then
11792        write_to_act_log('Process calc_running_total : started', 'LIST', g_list_header_id,'LOW');
11793   END IF;
11794 	l_request_id := FND_REQUEST.SUBMIT_REQUEST(
11795 			application => 'AMS',
11796 			program     => 'AMSLSRTC',
11797 			argument1   => p_action_used_by_id);
11798 		commit;
11799      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11800      FND_MESSAGE.Set_Token('TEXT',  'request_id->'||l_request_id || '<-');
11801      FND_MSG_PUB.Add;
11802           --    FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11803            --   FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
11804             --  FND_MSG_PUB.Add;
11805                FND_MSG_PUB.count_and_get(
11806                      p_encoded => FND_API.g_false,
11807                      p_count   => x_msg_count,
11808                      p_data    => x_msg_data  );
11809       x_return_status := 'S';
11810       x_msg_data := to_char(l_request_id);
11811 
11812            IF l_request_id = 0 THEN
11813               write_to_act_log('Unexpected Error for the program--concurrent program_id is '||to_char(l_request_id), 'LIST', g_list_header_id,'HIGH');
11814 
11815 
11816               FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11817               FND_MESSAGE.Set_Token('TEXT', 'Error '|| sqlerrm||' '||sqlcode);
11818               FND_MSG_PUB.Add;
11819                FND_MSG_PUB.count_and_get(
11820                      p_encoded => FND_API.g_false,
11821                      p_count   => x_msg_count,
11822                      p_data    => x_msg_data
11823       );
11824       x_return_status := 'E';
11825               RAISE FND_API.g_exc_unexpected_error;
11826            end if;
11827 end calc_selection_running_total;
11828 
11829 -- ----------------------------------------------------------------------------
11830 PROCEDURE calc_running_total (
11831               Errbuf               OUT NOCOPY     VARCHAR2,
11832               Retcode              OUT NOCOPY     VARCHAR2,
11833               p_action_used_by_id  in  number ) IS
11834 
11835   -- AMS_LIST_SELECT_ACTIONS Record for init record and complete record
11836   l_tmp_action_rec             ams_listaction_pvt.action_rec_type;
11837   p_action_rec                 ams_listaction_pvt.action_rec_type;
11838   l_list_select_action_id      number;
11839   l_running_total               number := 0;
11840    p_action_used_by  varchar2(30) := 'LIST';
11841    p_log_flag   	varchar2(1) := 'Y';
11842               x_return_status    varchar2(30);
11843               x_msg_count        number;
11844               x_msg_data         varchar2(4000);
11845 
11846   ----------------------------------------------------------------------------
11847   -- Cursor definition to select list_select_action_id.Will be used in loop to
11848   -- Process each cursor record according to order specified by the user
11849   ----------------------------------------------------------------------------
11850   CURSOR c_action_dets is
11851   SELECT list_select_action_id
11852     FROM ams_list_select_actions
11853    WHERE action_used_by_id   = p_action_used_by_id
11854      AND arc_action_used_by  = p_action_used_by
11855    ORDER by order_number;
11856 
11857   TYPE big_tbl_type is table of VARCHAR2(32767) index by BINARY_INTEGER;
11858   l_std_sql VARCHAR2(32767);
11859   l_include_sql VARCHAR2(32767);
11860   l_include_count number:=0;
11861   l_final_big_sql VARCHAR2(32767);
11862   l_include_sql_tbl  big_tbl_type ;
11863   l_std_sql_tbl  big_tbl_type ;
11864   l_join_string   varchar2(50);
11865 l_no_of_chunks            number;
11866 l_const_sql varchar2(4000) ;
11867   TYPE char_tbl_type is table of VARCHAR2(100) index by BINARY_INTEGER;
11868   TYPE num_tbl_type is table of number index by BINARY_INTEGER;
11869   l_rank_tbl      char_tbl_type;
11870   l_rank_num_tbl      num_tbl_type;
11871 l_sorted   number;
11872 l_update_sql  VARCHAR2(32767);
11873 l_list_header_id number ;
11874 cursor c1 is
11875 select generation_type
11876 from ams_list_headers_all
11877 where list_header_id = l_list_header_id;
11878 l_generation_type varchar2(60);
11879 l_selection_results   t_number;
11880 l_list_select_action   t_number;
11881 
11882 l_delta			number := 0;
11883 l_previous_incl_total	number := 0;
11884 l_list_act_id		number;
11885 l_ord_num		number;
11886 l_r_totals		number;
11887 cursor c_delta is
11888 select list_select_action_id,order_number,running_total from ams_list_select_actions where
11889 action_used_by_id = p_action_used_by_id and arc_action_used_by = 'LIST'
11890 and list_action_type = 'INCLUDE' order by order_number;
11891 l_last_generation_success_flag varchar2(1);
11892 
11893 cursor c_last_gen is
11894 select nvl(last_generation_success_flag,'N')
11895 from  ams_list_headers_all
11896 where list_header_id = p_action_used_by_id;
11897 
11898 BEGIN
11899 g_list_header_id := p_action_used_by_id;
11900 Ams_Utility_Pvt.Write_Conc_log('Start calc_running_total : ');
11901    UPDATE ams_list_select_actions
11902       SET RUNNING_TOTAL = null, DELTA = null
11903    WHERE action_used_by_id   = p_action_used_by_id
11904      AND arc_action_used_by  = 'LIST';
11905 
11906   IF(p_log_flag ='Y')then
11907        write_to_act_log('Executing procedure calc_running_total', 'LIST', g_list_header_id,'LOW');
11908   END IF;
11909 l_const_sql := ' minus '||
11910                ' select list_entry_source_system_id ' ||
11911                ' from ams_list_entries ' ||
11912                ' where list_header_id  = ' || p_action_used_by_id   ;
11913 open c_last_gen;
11914 fetch c_last_gen into l_last_generation_success_flag;
11915 close c_last_gen;
11916 if l_last_generation_success_flag = 'Y' then
11917         l_const_sql := NULL;
11918 end if;
11919 
11920   OPEN c_action_dets;
11921   LOOP
11922     FETCH c_action_dets INTO l_list_select_action_id;
11923     EXIT WHEN c_action_dets%NOTFOUND;
11924 
11925     -------------------------------------------------------------------------
11926     -- Gets list select actions record details
11927     -- Intialize the record, set the list_select_action_id and get the
11928     -- details
11929     -------------------------------------------------------------------------
11930     ams_listaction_pvt.init_action_rec(l_tmp_action_rec);
11931     l_tmp_action_rec.list_select_action_id := l_list_select_action_id;
11932     ams_listaction_pvt.complete_action_rec
11933                        (p_action_rec      =>l_tmp_action_rec,
11934                         x_complete_rec    =>p_action_rec);
11935 
11936     ----------------------------------------------------------------------
11937     --validating that the first executed action has a type of "INCLUDE".--
11938     ----------------------------------------------------------------------
11939     IF (c_action_dets%ROWCOUNT = 1) THEN
11940        IF (p_action_rec.list_action_type <> 'INCLUDE')then
11941            write_to_act_log('Error. The action type of the first selection is NOT INCLUDE.', 'LIST', g_list_header_id,'HIGH');
11942            Ams_Utility_Pvt.Write_Conc_log('process list actions : first action INCLUDE check failed');
11943            FND_MESSAGE.set_name('AMS', 'AMS_LIST_ACT_FIRST_INCLUDE');
11944            FND_MSG_PUB.Add;
11945            RAISE FND_API.G_EXC_ERROR;
11946        END IF;  -- End of if for list_action_type check
11947     END IF; --End of Validation:- First Action Include Check
11948 
11949 
11950     ams_listaction_pvt.Validate_ListAction
11951     ( p_api_version            => 1.0,
11952       p_init_msg_list          => FND_API.G_FALSE,
11953       p_validation_level       => JTF_PLSQL_API.G_VALID_LEVEL_RECORD,
11954       x_return_status          => x_return_status,
11955       x_msg_count              => x_msg_count,
11956       x_msg_data               => x_msg_data,
11957       p_action_rec             => p_action_rec
11958     );
11959 
11960    IF x_return_status = FND_API.g_ret_sts_error THEN
11961       RAISE FND_API.g_exc_error;
11962    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
11963       RAISE FND_API.g_exc_unexpected_error;
11964    END IF;
11965 
11966 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : Dynamic ');
11967    /********************************************************************
11968       This dynamic procedure will process action for each object type
11969       If the object type is of CELL the process will be procecss_cell
11970       Using the same logic the procedure could be extended for new
11971       action types
11972    *********************************************************************/
11973       write_to_act_log('Calling AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'LOW');
11974       execute immediate
11975       'BEGIN
11976         AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from ||
11977          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
11978       END;'
11979       using  p_action_used_by_id,
11980              p_action_rec.incl_object_id ,
11981              p_action_rec.list_action_type,
11982              p_action_rec.list_select_action_id,
11983              p_action_rec.order_number,
11984              p_action_rec.rank,
11985              'N',--CHECK p_action_rec.incl_control_group,
11986              OUT x_msg_data,
11987              OUT x_msg_count,
11988              in OUT x_return_status ,
11989              OUT l_std_sql ,
11990              OUT l_include_sql;
11991 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : End Dynamic 001-> ');
11992       write_to_act_log('Executed AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'LOW');
11993      if p_action_rec.list_action_type  = 'INCLUDE' then
11994 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total include: '||to_char(l_include_count));
11995         l_include_count := l_include_count +1  ;
11996         l_include_sql_tbl(l_include_count) := l_include_sql ;
11997         l_std_sql_tbl(l_include_count)  := l_std_sql;
11998 	l_list_select_action(l_include_count)  := l_list_select_action_id;
11999         l_rank_tbl(l_include_count)  :=  lpad(p_action_rec.rank,50,'0')
12000                          || lpad(p_action_rec.order_number,50,'0');
12001      else
12002        if p_action_rec.list_action_type  = 'EXCLUDE' then
12003           l_join_string := ' minus ';
12004        l_list_header_id := p_action_rec.action_used_by_id;
12005        else
12006           l_join_string := ' intersect ';
12007        l_list_header_id := p_action_rec.action_used_by_id;
12008      end if;
12009     Ams_Utility_Pvt.Write_Conc_log('process list actions noinclude: '||to_char(l_include_count));
12010    write_to_act_log('No of inclusions is ' || l_include_count, 'LIST', g_list_header_id,'LOW');
12011        FOR i IN 1 .. l_include_count
12012        loop
12013         l_std_sql_tbl(i)  :=
12014                                l_std_sql_tbl(i)   ||
12015                                l_join_string ||
12016                                l_std_sql;
12017      l_no_of_chunks  := ceil(length(l_std_sql_tbl(i))/2000 );
12018      for j in 1 ..l_no_of_chunks
12019      loop
12020         Ams_Utility_Pvt.Write_Conc_log('l_std_sql_tbl : '||substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000));
12021         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12022      end loop;
12023     end loop;
12024   end if; -- if p_action_rec.list_action_type  = 'INCLUDE'
12025 
12026 
12027      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
12028        RAISE FND_API.G_EXC_ERROR;
12029      END IF;
12030 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : End Dynamic ');
12031 -- end Of Dynamic Procedure
12032    l_join_string :='';
12033 ---------------------------------------------------------------------------
12034   END LOOP;  --  End loop c_action_dets
12035   CLOSE c_action_dets;
12036 Ams_Utility_Pvt.Write_Conc_log('***************** '||to_char(l_include_count));
12037 
12038      WRITE_TO_ACT_LOG('Sorting according to rank', 'LIST', g_list_header_id,'LOW');
12039 
12040 -- SOLIN, bug 3759988, use order_number, not rank for running total
12041        -- Sorting According to rank
12042 --       FOR i IN 1 .. l_include_count
12043 --       loop
12044 --          l_rank_num_tbl(i) := i;
12045 --          if i <> 1 then
12046 --             for j in 1 .. i-1
12047 --             loop
12048 --Ams_Utility_Pvt.Write_Conc_log('***************** '||l_rank_tbl(i) || '*i*' || i);
12049 --     WRITE_TO_ACT_LOG(l_rank_tbl(i) || '*i*' || i, 'LIST', g_list_header_id,'LOW');
12050 --               if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
12051 --                  for k in reverse j .. i-1
12052 --                  loop
12053 --                     l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
12054 --                  end loop;
12055 --                  l_rank_num_tbl(j) := i;
12056 --                  exit;
12057 --               end if;
12058 --             end loop;
12059 --	  end if;
12060 --       end loop;
12061 -- SOLIN, end
12062   for  i in 1 .. l_include_count
12063   loop
12064         l_sorted := i; -- l_rank_num_tbl(i); SOLIN, bug 3759988
12065 Ams_Utility_Pvt.Write_Conc_log('*****************SORTED '|| l_sorted ||' '||l_rank_tbl(i));
12066         l_final_big_sql := l_include_sql_tbl(l_sorted) ||
12067         l_std_sql_tbl(l_sorted) || l_const_sql || ')';
12068      WRITE_TO_ACT_LOG('Final SQL formed in calc_running_total proc', 'LIST', g_list_header_id,'LOW');
12069 Ams_Utility_Pvt.Write_Conc_log('********calc_selection_running_total: FINAL SQL ************');
12070      l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
12071      for i in 1 ..l_no_of_chunks
12072      loop
12073         WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12074         Ams_Utility_Pvt.Write_Conc_log('l_final_big_sql :'||substrb(l_final_big_sql,(2000*i) - 1999,2000));
12075      end loop;
12076 Ams_Utility_Pvt.Write_Conc_log('*******Process_list_actions: FINAL SQL END************');
12077   l_final_big_sql := 'BEGIN '||l_final_big_sql||' ; END;';
12078    EXECUTE IMMEDIATE l_final_big_sql using out l_selection_results(l_sorted);
12079 Ams_Utility_Pvt.Write_Conc_log('*******l_list_select_action(l_sorted) = '||l_list_select_action(l_sorted));
12080 Ams_Utility_Pvt.Write_Conc_log('*******l_selection_results(l_sorted) = '||l_selection_results(l_sorted));
12081    l_running_total := l_running_total + l_selection_results(l_sorted);
12082 Ams_Utility_Pvt.Write_Conc_log('*******l_running_total = '||l_running_total);
12083    Update ams_list_select_actions set RUNNING_TOTAL = l_running_total
12084    Where LIST_SELECT_ACTION_ID = l_list_select_action(l_sorted)
12085       and arc_action_used_by = 'LIST';
12086 
12087   end loop;
12088  commit;
12089 
12090 Ams_Utility_Pvt.Write_Conc_log('*******Delta Calculation*********');
12091 Ams_Utility_Pvt.Write_Conc_log('*******p_action_used_by_id = '||p_action_used_by_id);
12092    open c_delta;
12093    loop
12094      fetch c_delta into l_list_act_id,l_ord_num,l_r_totals;
12095 Ams_Utility_Pvt.Write_Conc_log('*******l_list_act_id = '||l_list_act_id);
12096 Ams_Utility_Pvt.Write_Conc_log('*******l_ord_num = '||l_ord_num);
12097 Ams_Utility_Pvt.Write_Conc_log('*******l_r_totals = '||l_r_totals);
12098      exit when c_delta%notfound;
12099      if l_ord_num = 1 then
12100         l_delta := 0;
12101 Ams_Utility_Pvt.Write_Conc_log('l_ord_num = 1 *******l_delta = '||l_delta);
12102      end if;
12103      if l_ord_num > 1 then
12104         l_delta := l_r_totals - l_previous_incl_total;
12105 Ams_Utility_Pvt.Write_Conc_log(' l_ord_num > 1 *******l_delta = '||l_delta);
12106      end if;
12107      Update ams_list_select_actions set delta = l_delta
12108      Where LIST_SELECT_ACTION_ID = l_list_act_id;
12109      l_delta := 0;
12110      l_previous_incl_total := l_r_totals;
12111 Ams_Utility_Pvt.Write_Conc_log('*******l_previous_incl_total = '||l_previous_incl_total);
12112    end loop;
12113    close c_delta;
12114 commit;
12115 EXCEPTION
12116    WHEN FND_API.G_EXC_ERROR THEN
12117    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12118      IF(c_action_dets%ISOPEN)THEN
12119         CLOSE c_action_dets;
12120      END IF;
12121      -- Check if reset of the status is required
12122      x_return_status := FND_API.G_RET_STS_ERROR ;
12123       FND_MSG_PUB.count_and_get(
12124             p_encoded => FND_API.g_false,
12125             p_count   => x_msg_count,
12126             p_data    => x_msg_data);
12127 
12128   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12129    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12130      IF(c_action_dets%ISOPEN)THEN
12131         CLOSE c_action_dets;
12132      END IF;
12133 Ams_Utility_Pvt.Write_Conc_log('Error: AMS_ListGeneration_PKG.Process_list_actions: '||sqlerrm||sqlcode);
12134      write_to_act_log('Error: AMS_ListGeneration_PKG.Process_list_actions:'
12135                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id);
12136      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
12137      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
12138      FND_MSG_PUB.Add;
12139      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12140       FND_MSG_PUB.count_and_get(
12141             p_encoded => FND_API.g_false,
12142             p_count   => x_msg_count,
12143             p_data    => x_msg_data);
12144 
12145   WHEN OTHERS THEN
12146    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12147      IF(c_action_dets%ISOPEN)THEN
12148         CLOSE c_action_dets;
12149      END IF;
12150 Ams_Utility_Pvt.Write_Conc_log('Error: AMS_ListGeneration_PKG.Process_list_actions: '||sqlerrm||sqlcode);
12151      write_to_act_log('Error: AMS_ListGeneration_PKG.Process_list_actions:'
12152                        ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
12153      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
12154      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
12155      FND_MSG_PUB.Add;
12156      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12157       FND_MSG_PUB.count_and_get(
12158             p_encoded => FND_API.g_false,
12159             p_count   => x_msg_count,
12160             p_data    => x_msg_data);
12161 END calc_running_total;
12162 
12163 -- ----------------------------------------------------------
12164 
12165 PROCEDURE process_run_total_imph
12166              (p_action_used_by_id in number,
12167               p_incl_object_id in number,
12168               p_list_action_type  in varchar2,
12169               p_list_select_action_id   in number,
12170               p_order_number   in number,
12171               p_rank   in number,
12172               p_include_control_group  in varchar2,
12173               x_msg_count      OUT NOCOPY number,
12174               x_msg_data       OUT NOCOPY varchar2,
12175               x_return_status  IN OUT NOCOPY VARCHAR2,
12176               x_std_sql OUT NOCOPY varchar2 ,
12177               x_include_sql OUT NOCOPY varchar2
12178               ) is
12179 l_list_entry_source_type  varchar2(30);
12180 cursor c_get_source_type
12181 is select  decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST')
12182 from ams_imp_list_headers_all
12183 where  import_list_header_id = p_incl_object_id;
12184 l_no_of_chunks  number;
12185 BEGIN
12186     write_to_act_log('Executing procedure process_run_total_imph', 'LIST', g_list_header_id,'LOW');
12187     open  c_get_source_type ;
12188     fetch c_get_source_type into l_list_entry_source_type  ;
12189     close  c_get_source_type ;
12190 
12191     if p_list_action_type  = 'INCLUDE' then
12192        if   l_list_entry_source_type <> 'PERSON_LIST' then
12193        x_include_sql  := '
12194               select
12195               count(*) into :1
12196        from   ams_hz_b2b_mapping_v ail
12197             where  enabled_flag = '||''''||'Y'||''''||
12198              ' and import_list_header_id =' || p_incl_object_id   ||
12199              ' and nvl(party_id, import_source_line_id) in (' ;
12200       else
12201        x_include_sql  := '
12202             select
12203               count(*) :1
12204             from ams_hz_b2c_mapping_v
12205             where  enabled_flag = '||''''||'Y'||''''||
12206              ' and import_list_header_id =' || p_incl_object_id   ||
12207              ' and nvl(party_id, import_source_line_id) in (' ;
12208         end if;
12209    end if;
12210    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
12211    if l_no_of_chunks is not null then
12212       for i in 1 ..l_no_of_chunks
12213         loop
12214            WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12215       end loop;
12216    end if;
12217 
12218    x_std_sql := ' select nvl(party_id,import_source_line_id)
12219                   from ams_imp_source_lines
12220                   where  import_list_header_id = ' ||   p_incl_object_id   ||
12221              '  and    nvl(duplicate_flag,' ||''''||'N'||''''||') = '||
12222                                               ''''||'N'||'''' ;
12223    WRITE_TO_ACT_LOG(x_std_sql, 'LIST', g_list_header_id,'LOW');
12224    write_to_act_log('Procedure process_run_total_imph executed', 'LIST', g_list_header_id,'LOW');
12225 END process_run_total_imph ;
12226 
12227 
12228 -- ----------------------------------------------------------------------------
12229 PROCEDURE process_run_total_list
12230              (p_action_used_by_id in number,
12231               p_incl_object_id in number,
12232               p_list_action_type  in varchar2,
12233               p_list_select_action_id   in number,
12234               p_order_number   in number,
12235               p_rank   in number,
12236               p_include_control_group  in varchar2,
12237               x_msg_count      OUT NOCOPY number,
12238               x_msg_data       OUT NOCOPY varchar2,
12239               x_return_status  IN OUT NOCOPY VARCHAR2,
12240               x_std_sql OUT NOCOPY varchar2 ,
12241               x_include_sql OUT NOCOPY varchar2
12242               ) is
12243 l_no_of_chunks  number;
12244 BEGIN
12245     write_to_act_log('Executing procedure process_run_total_list', 'LIST', g_list_header_id,'LOW');
12246     if p_list_action_type  = 'INCLUDE' then
12247        x_include_sql := '
12248                    select
12249               count(*) into :1
12250        from ams_list_entries
12251        where   list_header_id = ' ||   p_incl_object_id   ||
12252         '  and    nvl(enabled_flag,' ||''''||'N'||''''||') = '||
12253                                               ''''||'Y'||'''' ||
12254        ' and list_entry_source_system_id in (' ;
12255 
12256    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
12257    for i in 1 ..l_no_of_chunks
12258      loop
12259         WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12260    end loop;
12261    end if;
12262 
12263    x_std_sql := ' select list_entry_source_system_id
12264                      from ams_list_entries
12265                      where   list_header_id = ' ||   p_incl_object_id   ||
12266                      ' and     enabled_flag = ' || ''''||'Y' || '''' ;
12267         --WRITE_TO_ACT_LOG('std nclude ');
12268    WRITE_TO_ACT_LOG(x_std_sql, 'LIST', g_list_header_id,'LOW');
12269    write_to_act_log('Procedure process_run_total_list executed', 'LIST', g_list_header_id,'LOW');
12270 END process_run_total_list ;
12271 -- ----------------------------------------------------------------------------------
12272 PROCEDURE form_rt_sql_statement(p_select_statement in varchar2,
12273                              p_select_add_statement in varchar2,
12274                              p_master_type        in varchar2,
12275                              p_child_types     in child_type,
12276                              p_from_string in sql_string_4K,
12277                              p_action_used_by_id  in number,
12278                              p_list_select_action_id  in number,
12279                              p_list_action_type  in varchar2,
12280                              p_order_number in number,
12281                              p_rank  in number,
12282                              x_final_string OUT NOCOPY varchar2
12283                              ) is
12284 -- child_type      IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
12285 l_data_source_types varchar2(2000);
12286 l_field_col_tbl JTF_VARCHAR2_TABLE_100;
12287 l_source_col_tbl JTF_VARCHAR2_TABLE_100;
12288 l_view_tbl JTF_VARCHAR2_TABLE_100;
12289 l_source_col_dt_tbl JTF_VARCHAR2_TABLE_100;
12290 cursor c_master_source_type is
12291 select source_object_name , source_object_name || '.' || source_object_pk_field
12292 from ams_list_src_types
12293 where source_type_code = p_master_type;
12294 cursor c_child_source_type (l_child_src_type varchar2 )is
12295 select a.source_object_name ,
12296        a.source_object_name || '.' || b.sub_source_type_pk_column
12297        ,b.master_source_type_pk_column
12298 from ams_list_src_types  a, ams_list_src_type_assocs b
12299 where a.source_type_code = l_child_src_type
12300 and   b.sub_source_type_id = a.list_source_type_id;
12301 l_count                   number;
12302 l_master_object_name      varchar2(4000);
12303 l_child_object_name       varchar2(4000);
12304 l_master_primary_key      varchar2(1000);
12305 l_child_primary_key       varchar2(32767);
12306 l_from_clause             varchar2(32767);
12307 l_where_clause            varchar2(32767);
12308 l_select_clause           varchar2(32767);
12309 l_insert_clause           varchar2(32767);
12310 l_final_sql               varchar2(32767);
12311 l_insert_sql              varchar2(32767);
12312 l_no_of_chunks            number;
12313 l_master_fkey             Varchar2(1000);
12314 l_dummy_primary_key      varchar2(1000);
12315 begin
12316     write_to_act_log('Executing procedure form_rt_sql_statement', 'LIST', g_list_header_id,'LOW');
12317     WRITE_TO_ACT_LOG('master type is ' || p_master_type, 'LIST', g_list_header_id,'LOW');
12318 open  c_master_source_type;
12319 fetch c_master_source_type into l_master_object_name , l_master_primary_key;
12320 close c_master_source_type;
12321     WRITE_TO_ACT_LOG('master object name is ' || l_master_object_name||' , master primary key is '||l_master_primary_key, 'LIST', g_list_header_id,'LOW');
12322 l_from_clause :=  ' FROM ' || l_master_object_name;
12323 l_data_source_types := ' ('|| ''''|| p_master_type ||'''';
12324 l_where_clause := 'where 1 = 1 ';
12325 
12326 l_count  := p_child_types.count();
12327 if l_count > 0  then
12328    for i in 1..p_child_types.last
12329    loop
12330       l_data_source_types := l_data_source_types || ','|| ''''
12331                              || p_child_types(i)||'''' ;
12332       open  c_child_source_type(p_child_types(i));
12333       fetch c_child_source_type into l_child_object_name , l_child_primary_key
12334                                      ,l_master_fkey;
12335       l_dummy_primary_key := '';
12336       if l_master_fkey is not null then
12337          l_dummy_primary_key     := l_master_object_name || '.'|| l_master_fkey;
12338       else
12339          l_dummy_primary_key      := l_master_primary_key;
12340       end if;
12341       l_from_clause := l_from_clause || ','|| l_child_object_name ;
12342       l_where_clause := l_where_clause || 'and '
12343                               ||l_dummy_primary_key || ' = '
12344                         || l_child_primary_key || '(+)';
12345       close c_child_source_type;
12346    end loop;
12347 end if;
12348 l_data_source_types := l_data_source_types || ') ' ;
12349 
12350  EXECUTE IMMEDIATE
12351      'BEGIN
12352       SELECT b.field_column_name ,
12353                c.source_object_name,
12354                b.source_column_name,
12355                b.field_data_type
12356         BULK COLLECT INTO :1 ,:2  ,:3 ,:4
12357         FROM ams_list_src_fields b, ams_list_src_types c
12358         WHERE b.list_source_type_id = c.list_source_type_id
12359           and b.DE_LIST_SOURCE_TYPE_CODE IN  '|| l_data_source_types ||
12360           ' AND b.ROWID >= (SELECT MAX(a.ROWID)
12361                             FROM ams_list_src_fields a
12362                            WHERE a.field_column_name= b.field_column_name
12363 	                    AND  a.DE_LIST_SOURCE_TYPE_CODE IN '
12364                                  || l_data_source_types || ') ;
12365       END; '
12366   USING OUT l_field_col_tbl ,OUT l_view_tbl , OUT l_source_col_tbl,OUT l_source_col_dt_tbl ;
12367   --WRITE_TO_ACT_LOG('imp: p_select_statement' || p_select_statement);
12368   --WRITE_TO_ACT_LOG('imp: p_select_add_statement' || p_select_add_statement);
12369   --WRITE_TO_ACT_LOG('imp: select clause ' || l_select_clause);
12370 for i in 1 .. l_field_col_tbl.last
12371 loop
12372   l_insert_clause  := l_insert_clause || ' ,' || l_field_col_tbl(i) ;
12373     if l_source_col_dt_tbl(i) = 'DATE' then
12374      l_select_clause  := l_select_clause || ' ,' ||
12375                       'to_char('||l_view_tbl(i) || '.'||l_source_col_tbl(i)||','||''''||'DD-MM-RRRR'||''''||')' ;
12376      else
12377       l_select_clause  := l_select_clause || ' ,' ||
12378                       l_view_tbl(i) || '.'||l_source_col_tbl(i) ;
12379     end if;
12380   --WRITE_TO_ACT_LOG('imp: select clause'||i||':->' || l_select_clause);
12381 end loop;
12382 --  WRITE_TO_ACT_LOG('form_sql_statement:before insert_sql ', 'LIST', g_list_header_id);
12383   l_insert_sql := 'select count(*) into :1 ';
12384    --insert into test3 values (6,'l_insert_sql = :'||l_insert_sql);
12385   --commit;
12386    --insert into test3 values (7,'l_from_clause = :'||l_from_clause);
12387   --commit;
12388      --insert into test3 values (8,'l_where_clause = :'||l_where_clause);
12389   --commit;
12390   --WRITE_TO_ACT_LOG('form_rt_sql_statement:before final sql ', 'LIST', g_list_header_id);
12391      l_final_sql := l_insert_sql || '  ' ||
12392                   l_from_clause ||  '  '||
12393                   l_where_clause   || ' and  ' ||
12394                    l_master_primary_key|| ' in  ( ' ;
12395      x_final_string := l_final_sql;
12396   WRITE_TO_ACT_LOG('Final SQL formed in form_rt_sql_statement', 'LIST', g_list_header_id,'LOW');
12397      l_no_of_chunks  := ceil(length(l_final_sql)/2000 );
12398      for i in 1 ..l_no_of_chunks
12399      loop
12400         WRITE_TO_ACT_LOG(substrb(l_final_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12401      end loop;
12402      write_to_act_log('Procedure form_rt_sql_statement executed', 'LIST', g_list_header_id,'LOW');
12403 exception
12404    when others then
12405      write_to_act_log('Error while executing procedure form_rt_sql_statement '||sqlcode||'   '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
12406 end form_rt_sql_statement;
12407 
12408 -- ----------------------------------------------------------------------------------
12409 PROCEDURE process_rt_insert_sql(p_select_statement in varchar2,
12410                              p_select_add_statement in varchar2,
12411                              p_master_type        in varchar2,
12412                              p_child_types     in child_type,
12413                              p_from_string in sql_string_4K ,
12414                              p_action_used_by_id  in number,
12415                              p_list_select_action_id  in number,
12416                              p_list_action_type  in varchar2,
12417                              p_order_number in number,
12418                              p_rank  in number,
12419                              x_std_sql OUT NOCOPY varchar2 ,
12420                              x_include_sql OUT NOCOPY varchar2
12421                              ) is
12422 l_final_sql   varchar2(32767);
12423 l_insert_sql varchar2(32767);
12424 l_insert_sql1 varchar2(32767);
12425 l_table_name  varchar2(80) := ' ams_list_tmp_entries ';
12426 BEGIN
12427   write_to_act_log('Execution of procedure process_rt_insert_sql started', 'LIST', g_list_header_id,'LOW');
12428 
12429   l_insert_sql := p_select_statement ;
12430   for i in 1 .. p_from_string.last
12431   loop
12432 --     write_to_act_log(p_from_string(i), 'LIST', g_list_header_id);
12433     l_insert_sql  := l_insert_sql || p_from_string(i);
12434   end loop;
12435   x_std_sql := l_insert_sql;
12436   if p_list_action_type = 'INCLUDE' then
12437           form_rt_sql_statement(p_select_statement ,
12438                              p_select_add_statement ,
12439                              p_master_type        ,
12440                              p_child_types     ,
12441                              p_from_string ,
12442                              p_action_used_by_id  ,
12443                              p_list_select_action_id  ,
12444                              p_list_action_type  ,
12445                              p_order_number ,
12446                              p_rank  ,
12447                              l_final_sql
12448                              ) ;
12449   end if;
12450   x_include_sql := l_final_sql;
12451 
12452    write_to_act_log('Procedure process_rt_insert_sql executed', 'LIST', g_list_header_id,'LOW');
12453 exception
12454    when others then
12455    write_to_act_log('Error while executing process_rt_insert_sql '||sqlcode||'  '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
12456 END process_rt_insert_sql;
12457 
12458 -- ----------------------------------------------------------------------------------
12459 PROCEDURE process_rt_all_sql  (p_action_used_by_id in number,
12460                             p_incl_object_id in number,
12461                             p_list_action_type  in varchar2,
12462                             p_list_select_action_id   in number,
12463                             p_order_number   in number,
12464                             p_rank   in number,
12465                             p_include_control_group  in varchar2,
12466                             p_sql_string    in sql_string,
12467                             p_primary_key   in  varchar2,
12468                             p_source_object_name in  varchar2,
12469                             x_msg_count      OUT NOCOPY number,
12470                             x_msg_data       OUT NOCOPY varchar2,
12471                             x_return_status  IN OUT NOCOPY VARCHAR2,
12472                             x_std_sql OUT NOCOPY varchar2 ,
12473                             x_include_sql OUT NOCOPY varchar2
12474                             ) is
12475 l_sql_string         sql_string;
12476 l_where_string       sql_string;
12477 -- l_from_string       sql_string;
12478 l_from_string       sql_string_4K;
12479 l_counter            NUMBER := 1;
12480 l_from_position      number;
12481 l_from_counter       number;
12482 l_end_position      number;
12483 l_end_counter       number;
12484 l_order_position      number;
12485 l_order_counter       number;
12486 l_group_position      number;
12487 l_group_counter       number;
12488 l_found              varchar2(1) := 'N';
12489 l_master_type        varchar2(80);
12490 l_master_type_id     number;
12491 l_source_object_name  varchar2(80);
12492 l_source_object_pk_field  varchar2(80);
12493 l_child_types        child_type;
12494 l_select_condition    varchar2(2000);
12495 l_select_add_condition    varchar2(2000);
12496 l_sql_string_v2           varchar2(4000);
12497 BEGIN
12498   /* Validate Sql String will take all the sql statement fragement and
12499      check if the search string is present. If it is present it will
12500      return the position of fragement and the counter
12501   */
12502   write_to_act_log('Execution of procedure process_rt_all_sql started', 'LIST', g_list_header_id,'LOW');
12503 
12504   l_sql_string := p_sql_string;
12505 
12506   --write_to_act_log('Process_rt_all_sql: start ');
12507   l_found  := 'N';
12508   validate_sql_string(p_sql_string => l_sql_string ,
12509                       p_search_string => 'FROM',
12510                       p_comma_valid   => 'N',
12511                       x_found    => l_found,
12512                       x_position =>l_from_position,
12513                       x_counter => l_from_counter) ;
12514 
12515 --  write_to_act_log('Process_rt_all_sql l_found : '||l_found,'LIST', g_list_header_id);
12516 --  write_to_act_log('Process_rt_all_sql return status: After validate_sql_string call  ','LIST', g_list_header_id);
12517   if l_found = 'N' then
12518      FND_MESSAGE.set_name('AMS', 'AMS_LIST_FROM_NOT_FOUND');
12519      FND_MSG_PUB.Add;
12520      RAISE FND_API.G_EXC_ERROR;
12521   end if;
12522 /*     write_to_act_log('Process_rt_all_sql: FROM Position ->'|| l_from_position ||
12523                                    '<--FROM Counter ->' || l_from_counter ||
12524                                    '<--FROM Found ->' || l_found, 'LIST', g_list_header_id);*/
12525   l_found  := 'N';
12526   get_master_types (p_sql_string => l_sql_string,
12527                     p_start_length => 1,
12528                     p_start_counter => 1,
12529                     p_end_length => l_from_position,
12530                     p_end_counter => l_from_counter,
12531                     x_master_type_id=> l_master_type_id,
12532                     x_master_type=> l_master_type,
12533                     x_found=> l_found,
12534                     x_source_object_name => l_source_object_name,
12535                     x_source_object_pk_field  => l_source_object_pk_field);
12536 
12537   if l_found = 'N' then
12538      FND_MESSAGE.set_name('AMS', 'AMS_LIST_NO_MASTER_TYPE');
12539      FND_MSG_PUB.Add;
12540      RAISE FND_API.G_EXC_ERROR;
12541   end if;
12542   write_to_act_log('Master type is '|| l_master_type ||'<--'  , 'LIST', g_list_header_id,'LOW');
12543 
12544   l_found  := 'N';
12545   write_to_act_log('Calling procedure get_condition'  , 'LIST', g_list_header_id,'LOW');
12546 
12547   get_condition(p_sql_string => l_sql_string ,
12548                 p_search_string => 'FROM',
12549                 p_comma_valid   => 'N',
12550                 x_position =>l_from_position,
12551                 x_counter => l_from_counter,
12552                 x_found    => l_found,
12553                 x_sql_string => l_from_string) ;
12554 
12555   /* FOR SQL STATEMENTS  WHICH ARE NOT FROM THE DERIVING MASTER SOURCE TABLE  */
12556   if p_primary_key is not null then
12557      l_source_object_pk_field := p_primary_key;
12558      l_source_object_name     := p_source_object_name ;
12559   end if;
12560   l_select_condition := 'SELECT ' ||l_source_object_name||'.'
12561                         ||l_source_object_pk_field;
12562                         --||'||'||''''
12563                         --||l_master_type||'''';
12564   l_select_add_condition := ','||l_source_object_name||'.'
12565                         ||l_source_object_pk_field||','||''''
12566                         ||l_master_type||'''' ;
12567 
12568    write_to_act_log('Calling procedure process_rt_insert_sql'  , 'LIST', g_list_header_id,'LOW');
12569    process_rt_insert_sql(p_select_statement       => l_select_condition,
12570                       p_select_add_statement   => l_select_add_condition,
12571                       p_master_type            => l_master_type,
12572                       p_child_types            => l_child_types,
12573                       p_from_string            => l_from_string  ,
12574                       p_list_select_action_id  => p_list_select_action_id  ,
12575                       p_action_used_by_id      => p_action_used_by_id ,
12576                       p_list_action_type       => p_list_action_type ,
12577                       p_order_number           => p_order_number,
12578                       p_rank                   => p_rank,
12579                       x_std_sql                => x_std_sql,
12580                       x_include_sql            => x_include_sql
12581                       );
12582 
12583    write_to_act_log('Procedure process_rt_all_sql executed ', 'LIST', g_list_header_id ,'LOW');
12584 
12585 EXCEPTION
12586    WHEN FND_API.G_EXC_ERROR THEN
12587      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12588      x_return_status := FND_API.G_RET_STS_ERROR ;
12589       FND_MSG_PUB.count_and_get(
12590             p_encoded => FND_API.g_false,
12591             p_count   => x_msg_count,
12592             p_data    => x_msg_data
12593       );
12594 
12595   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12596      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12597      x_return_status := FND_API.G_RET_STS_ERROR ;
12598       FND_MSG_PUB.count_and_get(
12599             p_encoded => FND_API.g_false,
12600             p_count   => x_msg_count,
12601             p_data    => x_msg_data
12602       );
12603 
12604   WHEN OTHERS THEN
12605      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12606      x_return_status := FND_API.G_RET_STS_ERROR ;
12607       FND_MSG_PUB.count_and_get(
12608             p_encoded => FND_API.g_false,
12609             p_count   => x_msg_count,
12610             p_data    => x_msg_data
12611       );
12612 
12613 
12614 END process_rt_all_sql;
12615 
12616 -- ----------------------------------------------------------------------------------
12617 
12618 
12619 PROCEDURE process_run_total_sql (p_action_used_by_id in number,
12620               p_incl_object_id in number,
12621               p_list_action_type  in varchar2,
12622               p_list_select_action_id   in number,
12623               p_order_number   in number,
12624               p_rank   in number,
12625               p_include_control_group  in varchar2,
12626               x_msg_count      OUT NOCOPY number,
12627               x_msg_data       OUT NOCOPY varchar2,
12628               x_return_status  IN OUT NOCOPY VARCHAR2,
12629               x_std_sql OUT NOCOPY varchar2 ,
12630               x_include_sql OUT NOCOPY varchar2
12631               ) is
12632 
12633 ------------------------------------------------------------------------------
12634 -- Given the sql id from ams_list_select_actions it will retrieve the
12635 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12636 -- workbook_name.
12637 ------------------------------------------------------------------------------
12638 cursor cur_sql  is
12639 SELECT query, primary_key, source_object_name
12640 FROM   ams_list_queries_all
12641 WHERE  (list_query_id = p_incl_object_id )
12642 or (parent_list_query_id = p_incl_object_id )
12643 order by sequence_order;
12644 
12645 /* sql_string column is obsolete: bug 4604653
12646 cursor cur_old_sql  is
12647 SELECT sql_string, primary_key, source_object_name
12648 FROM   ams_list_queries_all
12649 WHERE  (list_query_id = p_incl_object_id )
12650 or (parent_list_query_id = p_incl_object_id )
12651 order by sequence_order;
12652 */
12653 
12654 cursor cur_primary_key_sql  is
12655 SELECT lc.SOURCE_OBJECT_NAME, lc.SOURCE_OBJECT_PK_FIELD
12656 FROM   ams_list_queries_all  lq,
12657        ams_list_headers_all lh,
12658        ams_list_src_types  lc
12659 WHERE lq.list_query_id = p_incl_object_id
12660 and   lq.ARC_ACT_LIST_QUERY_USED_BY = 'LIST'
12661 and   lq.ACT_LIST_QUERY_USED_BY_ID = lh.list_header_id
12662 and   lc.source_type_code = lh.list_source_type;
12663 
12664 l_sql_string         sql_string;
12665 l_where_string       sql_string;
12666 l_from_string       sql_string;
12667 l_counter            NUMBER := 1;
12668 l_from_position      number;
12669 l_from_counter       number;
12670 l_end_position      number;
12671 l_end_counter       number;
12672 l_order_position      number;
12673 l_order_counter       number;
12674 l_group_position      number;
12675 l_group_counter       number;
12676 l_found              varchar2(1);
12677 l_master_type        varchar2(80);
12678 l_master_type_id     number;
12679 l_source_object_name  varchar2(80);
12680 l_source_object_pk_field  varchar2(80);
12681 l_child_types        child_type;
12682 l_select_condition    varchar2(2000);
12683 l_select_add_condition    varchar2(2000);
12684 l_sql_string_v2           varchar2(4000);
12685 l_primary_key          varchar2(80);
12686 l_no_pieces            number :=0;
12687 l_big_sql VARCHAR2(32767);
12688 BEGIN
12689   write_to_act_log('Procedure process_run_total_sql started ', 'LIST', g_list_header_id ,'LOW');
12690   open cur_sql;
12691   loop
12692     fetch cur_sql into l_big_sql,l_primary_key,l_source_object_name;
12693     exit when cur_sql%notfound ;
12694     write_to_act_log('Incl object id' || p_incl_object_id, 'LIST', g_list_header_id,'LOW');
12695     l_no_pieces := ceil(length(l_big_sql)/2000);
12696     write_to_act_log('length of SQL' || l_no_pieces, 'LIST', g_list_header_id,'LOW');
12697     if l_no_pieces  > 0 then
12698        for i  in 1 .. l_no_pieces
12699        loop
12700           write_to_act_log('number of chunks ' || i, 'LIST', g_list_header_id,'LOW');
12701           --write_to_act_log('Process_sql: before ' );
12702           l_sql_string(l_counter):= substrb(l_big_sql,2000*i -1999,2000);
12703           --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
12704           l_counter  := l_counter +1 ;
12705        end loop;
12706     end if;
12707     -- l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
12708     -- l_counter  := l_counter +1 ;
12709   end loop;
12710   close cur_sql;
12711 
12712 --    write_to_act_log('lenth of pieces:' || l_no_pieces, 'LIST', g_list_header_id);
12713 /*
12714   if l_no_pieces = 0   or
12715      l_no_pieces is null then
12716      open cur_old_sql;
12717      loop
12718        fetch cur_old_sql into l_sql_string_v2,l_primary_key,l_source_object_name;
12719   --     write_to_act_log('Process_sql old cursor 4000->:' || p_incl_object_id, 'LIST', g_list_header_id);
12720        exit when cur_old_sql%notfound ;
12721        --write_to_act_log('Process_sql: before ' );
12722        l_sql_string(l_counter):= substrb(l_sql_string_v2,1,2000);
12723        --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
12724        l_counter  := l_counter +1 ;
12725        l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
12726        l_counter  := l_counter +1 ;
12727      end loop;
12728      close cur_old_sql;
12729   end if;
12730   */
12731   if l_source_object_name is null or
12732      l_primary_key is null then
12733      open cur_primary_key_sql  ;
12734      loop
12735        fetch cur_primary_key_sql into l_primary_key,l_source_object_name;
12736        exit when cur_primary_key_sql%notfound ;
12737      end loop;
12738      close cur_primary_key_sql  ;
12739   end if;
12740   write_to_act_log('Calling procedure process_rt_all_sql', 'LIST', g_list_header_id,'LOW');
12741   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
12742                   p_incl_object_id => p_incl_object_id ,
12743                   p_list_action_type  => p_list_action_type  ,
12744                   p_list_select_action_id   => p_list_select_action_id   ,
12745                   p_order_number   => p_order_number   ,
12746                   p_rank   => p_rank   ,
12747                   p_include_control_group  => p_include_control_group,
12748                   p_sql_string    => l_sql_string    ,
12749                   p_primary_key   => l_primary_key,
12750                   p_source_object_name   => l_source_object_name,
12751                   x_msg_count      => x_msg_count      ,
12752                   x_msg_data   => x_msg_data   ,
12753                   x_return_status   => x_return_status   ,
12754                   x_std_sql                => x_std_sql,
12755                   x_include_sql            => x_include_sql
12756                   );
12757   write_to_act_log('Procedure process_run_total_sql executed', 'LIST', g_list_header_id,'LOW'  );
12758 
12759 exception
12760    when others then
12761     write_to_act_log('Error while executing process_run_total_sql ' ||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12762     x_return_status := FND_API.G_RET_STS_ERROR ;
12763 END process_run_total_sql;
12764 
12765 -- ----------------------------------------------------------------------------------------------
12766 PROCEDURE process_run_total_cell
12767              (p_action_used_by_id in  number,
12768               p_incl_object_id in number,
12769               p_list_action_type  in varchar2,
12770               p_list_select_action_id   in number,
12771               p_order_number   in number,
12772               p_rank   in number,
12773               p_include_control_group  in varchar2,
12774               x_msg_count      OUT NOCOPY number,
12775               x_msg_data       OUT NOCOPY varchar2,
12776               x_return_status  IN OUT NOCOPY VARCHAR2,
12777               x_std_sql OUT NOCOPY varchar2 ,
12778               x_include_sql OUT NOCOPY varchar2
12779                ) is
12780 
12781 ------------------------------------------------------------------------------
12782 -- Given the sql id from ams_list_select_actions it will retrieve the
12783 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12784 -- workbook_name.
12785 ------------------------------------------------------------------------------
12786 l_sql_string         sql_string;
12787 l_where_string       sql_string;
12788 l_from_string       sql_string;
12789 l_counter            NUMBER := 1;
12790 l_from_position      number;
12791 l_from_counter       number;
12792 l_end_position      number;
12793 l_end_counter       number;
12794 l_order_position      number;
12795 l_order_counter       number;
12796 l_group_position      number;
12797 l_group_counter       number;
12798 l_found              varchar2(1);
12799 l_master_type        varchar2(80);
12800 l_master_type_id     number;
12801 l_source_object_name  varchar2(80);
12802 l_source_object_pk_field  varchar2(80);
12803 l_child_types        child_type;
12804 l_select_condition    varchar2(2000);
12805 l_select_add_condition    varchar2(2000);
12806 l_msg_data       VARCHAR2(2000);
12807 l_msg_count      number;
12808 l_sql_2          DBMS_SQL.VARCHAR2S;
12809 l_sql_string_final    varchar2(4000);
12810 j number     := 1;
12811 BEGIN
12812 
12813   write_to_act_log('Executing procedure process_run_total_cell', 'LIST', g_list_header_id,'LOW'  );
12814   ams_cell_pvt.get_comp_sql(
12815       p_api_version       => 1.0,
12816       p_init_msg_list     => FND_API.g_false,
12817       p_validation_level  => FND_API.g_valid_level_full,
12818       x_return_status     => x_return_status,
12819       x_msg_count         => x_msg_count ,
12820       x_msg_data           =>x_msg_data,
12821       p_cell_id           => p_incl_object_id ,
12822       p_party_id_only     => FND_API.g_false,
12823       x_sql_tbl           => l_sql_2
12824    );
12825     write_to_act_log('AMS_ListGeneration_PKG.After Comp sql:', 'LIST', g_list_header_id,'LOW');
12826 
12827   l_sql_string_final := '';
12828   for i in 1 .. l_sql_2.last
12829   loop
12830       l_sql_string_final := l_sql_string_final || l_sql_2(i);
12831      if length(l_sql_string_final) > 2000 then
12832         l_sql_string(j) := substrb(l_sql_string_final,1,2000);
12833         l_sql_string_final := substrb(l_sql_string_final,2001 ,2000);
12834         j := j+1;
12835      end if;
12836   end loop;
12837   l_sql_string(j) := substrb(l_sql_string_final,1,2000);
12838   if length(l_sql_string_final) > 2000 then
12839     j := j+1;
12840     l_sql_string(j) := substrb(l_sql_string_final,2001 ,2000);
12841   end if;
12842   write_to_act_log('process_rt_all_sql', 'LIST', g_list_header_id,'LOW');
12843 
12844   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
12845                   p_incl_object_id => p_incl_object_id ,
12846                   p_list_action_type  => p_list_action_type  ,
12847                   p_list_select_action_id   => p_list_select_action_id   ,
12848                   p_order_number   => p_order_number   ,
12849                   p_rank   => p_rank   ,
12850                   p_include_control_group  => p_include_control_group,
12851                   p_sql_string    => l_sql_string    ,
12852                   x_msg_count      => x_msg_count      ,
12853                   x_msg_data   => x_msg_data   ,
12854                   x_return_status   => x_return_status   ,
12855                   x_std_sql                => x_std_sql,
12856                   x_include_sql            => x_include_sql,
12857                   p_primary_key   => null,
12858                   p_source_object_name => null);
12859 
12860     write_to_act_log('Procedure process_run_total_cell executed.', 'LIST', g_list_header_id,'LOW');
12861 --No exception??? need to include one..
12862 
12863 END process_run_total_cell ;
12864 
12865 -- -----------------------------------------------------------------------------------------------
12866 PROCEDURE process_run_total_diwb (p_action_used_by_id in number,
12867               p_incl_object_id in number,
12868               p_list_action_type  in varchar2,
12869               p_list_select_action_id   in number,
12870               p_order_number   in number,
12871               p_rank   in number,
12872               p_include_control_group  in varchar2,
12873               x_msg_count      OUT NOCOPY number,
12874               x_msg_data       OUT NOCOPY varchar2,
12875               x_return_status  IN OUT NOCOPY VARCHAR2,
12876               x_std_sql OUT NOCOPY varchar2 ,
12877               x_include_sql OUT NOCOPY varchar2
12878               ) is
12879 
12880 ------------------------------------------------------------------------------
12881 -- Given the sql id from ams_list_select_actions it will retrieve the
12882 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12883 -- workbook_name.
12884 ------------------------------------------------------------------------------
12885 cursor cur_diwb(l_incl_object_id  in number )  is
12886 SELECT sql_string
12887 FROM   ams_discoverer_sql
12888 WHERE  (workbook_name, worksheet_name )
12889 IN
12890 ( SELECT workbook_name, worksheet_name
12891   FROM   ams_discoverer_sql
12892   WHERE  discoverer_sql_id = l_incl_object_id)
12893 ORDER BY sequence_order;
12894 
12895 l_sql_string         sql_string;
12896 l_where_string       sql_string;
12897 l_from_string       sql_string;
12898 l_counter            NUMBER := 1;
12899 l_from_position      number;
12900 l_from_counter       number;
12901 l_end_position      number;
12902 l_end_counter       number;
12903 l_order_position      number;
12904 l_order_counter       number;
12905 l_group_position      number;
12906 l_group_counter       number;
12907 l_found              varchar2(1);
12908 l_master_type        varchar2(80);
12909 l_master_type_id     number;
12910 l_source_object_name  varchar2(80);
12911 l_source_object_pk_field  varchar2(80);
12912 l_child_types        child_type;
12913 l_select_condition    varchar2(2000);
12914 l_select_add_condition    varchar2(2000);
12915 BEGIN
12916   write_to_act_log('Executing procedure process_run_total_diwb', 'LIST', g_list_header_id,'LOW');
12917 
12918   /* Populating l_sql_string with sql statements from ams_discoverer_sql
12919      l_sql_string is of table type of varchar2(2000)
12920   */
12921   open cur_diwb(p_incl_object_id);
12922   loop
12923     fetch cur_diwb into l_sql_string(l_counter);
12924     exit when cur_diwb%notfound ;
12925     l_counter  := l_counter +1 ;
12926   end loop;
12927   close cur_diwb;
12928   write_to_act_log('Calling procedure process_rt_all_sql','LIST', g_list_header_id,'LOW');
12929   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
12930                   p_incl_object_id => p_incl_object_id ,
12931                   p_list_action_type  => p_list_action_type  ,
12932                   p_list_select_action_id   => p_list_select_action_id   ,
12933                   p_order_number   => p_order_number   ,
12934                   p_rank   => p_rank   ,
12935                   p_include_control_group  => p_include_control_group,
12936                   p_sql_string    => l_sql_string,
12937 		  p_primary_key   => null,
12938 		  p_source_object_name => null,
12939                   x_msg_count      => x_msg_count      ,
12940                   x_msg_data   => x_msg_data   ,
12941                   x_return_status   => x_return_status   ,
12942                   x_std_sql                => x_std_sql,
12943                   x_include_sql            => x_include_sql);
12944     write_to_act_log('Procedure process_run_total_diwb executed','LIST', g_list_header_id,'LOW');
12945 END process_run_total_diwb ;
12946 
12947 -- ------------------------------------------------------------------------------------------------
12948 PROCEDURE  tca_upload_process
12949              (p_list_header_id  in  number,
12950               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
12951               x_return_status      OUT NOCOPY VARCHAR2,
12952               x_msg_count          OUT NOCOPY NUMBER,
12953               x_msg_data           OUT NOCOPY VARCHAR2) IS
12954 
12955 l_list_entry_id		Number;
12956 list_column_name	Varchar2(30);
12957 tca_column_name		Varchar2(30);
12958 l_string		Varchar2(2000);
12959 l_entry_value		Varchar2(1000);
12960 l_b2b_flag		Varchar2(6);
12961 l_b2b			Varchar2(1);
12962 l_party_id		Number;
12963 l_component_name	Varchar2(30);
12964 l_new_party		Varchar2(1);
12965 x_tmp_var                       VARCHAR2(4000);
12966 x_tmp_var1                      VARCHAR2(4000);
12967 
12968  party_rec       hz_party_v2pub.party_rec_type;
12969  org_rec         hz_party_v2pub.organization_rec_type;
12970  person_rec      hz_party_v2pub.person_rec_type;
12971  location_rec    hz_location_v2pub.location_rec_type;
12972  psite_rec       hz_party_site_v2pub.party_site_rec_type;
12973  psiteuse_rec    hz_party_site_v2pub.party_site_use_rec_type;
12974  cpoint_rec      hz_contact_point_v2pub.contact_point_rec_type;
12975  email_rec       hz_contact_point_v2pub.email_rec_type;
12976  phone_rec       hz_contact_point_v2pub.phone_rec_type;
12977  fax_rec         hz_contact_point_v2pub.phone_rec_type;
12978  ocon_rec        hz_party_contact_v2pub.org_contact_rec_type;
12979  edi_rec         hz_contact_point_v2pub.edi_rec_type;
12980  telex_rec       hz_contact_point_v2pub.telex_rec_type;
12981  web_rec         hz_contact_point_v2pub.web_rec_type;
12982 
12983 
12984 cursor c_list_entries is
12985 select list_entry_id from ams_list_entries where list_header_id = p_list_header_id
12986    -- and party_id is null
12987    and nvl(tca_load_status,'x') not in ('SUCCESS','ERROR')
12988    and enabled_flag = 'Y'; --bmuthukr. R12 Need to upload only the enabled entries.
12989 
12990 cursor c_tca_columns is
12991 select flds.field_column_name , tca.column_name
12992 from ams_list_src_fields flds, AMS_DS_TCA_ENTITY_COLS tca,
12993       ams_list_headers_all hdr, ams_list_src_types typ
12994 where hdr.list_header_id = p_list_header_id
12995   and hdr.LIST_SOURCE_TYPE = typ.SOURCE_TYPE_CODE
12996   and typ.list_source_type_id = flds.list_source_type_id
12997   and flds.enabled_flag = 'Y'
12998   and used_in_list_entries = 'Y'
12999   and flds.tca_column_id = tca.ENTITY_COLUMN_ID;
13000 
13001 cursor c_data_type is
13002 select source_category from ams_list_src_types types, ams_list_headers_all head
13003 where head.list_header_id = p_list_header_id
13004 and head.list_source_type = types.source_type_code;
13005 
13006 l_prof VARCHAR2(50);
13007 
13008 BEGIN
13009   write_to_act_log('Executing procedure tca_upload_process', 'LIST', p_list_header_id,'LOW');
13010 
13011   --TCA mandate: bug 4587049
13012   --Disable TCA events before bulk TCA data processing
13013   l_prof := fnd_profile.value('HZ_EXECUTE_API_CALLOUTS');
13014   if l_prof <> 'N' then
13015      fnd_profile.put('HZ_EXECUTE_API_CALLOUTS','N');
13016   end if;
13017 
13018   open c_data_type;
13019   fetch c_data_type into l_b2b_flag;
13020   close c_data_type;
13021   write_to_act_log('Source category is '||l_b2b_flag, 'LIST', p_list_header_id,'LOW');
13022   open c_list_entries;
13023   LOOP
13024      fetch c_list_entries into l_list_entry_id;
13025      Exit when c_list_entries%notfound;
13026      open c_tca_columns;
13027      loop
13028        fetch c_tca_columns into list_column_name, tca_column_name;
13029        exit when c_tca_columns%notfound;
13030        l_string := 'begin select '||list_column_name||' into :l_entry_value from ams_list_entries where
13031                     list_entry_id = '||to_char(l_list_entry_id)||' ; end;';
13032  -- DBMS_OUTPUT.PUT_LINE('l_string  = '||l_string);
13033 	execute immediate l_string using out l_entry_value;
13034   -- DBMS_OUTPUT.PUT_LINE('l_entry_value = '||l_entry_value);
13035     if l_b2b_flag = 'B2B' then
13036 	l_b2b := 'Y';
13037        if tca_column_name = 'PARTY_NAME' then
13038 	 org_rec.organization_name := l_entry_value;
13039   -- DBMS_OUTPUT.PUT_LINE('org_rec.organization_name = '||org_rec.organization_name);
13040        end if;
13041        if tca_column_name = 'FISCAL_YEAREND_MONTH' then
13042            org_rec.fiscal_yearend_month := l_entry_value;
13043        elsif tca_column_name = 'DUNS_NUMBER_C' then
13044            org_rec.duns_number_c := l_entry_value;
13045        elsif tca_column_name = 'EMPLOYEES_TOTAL' then
13046            org_rec.employees_total := l_entry_value;
13047        elsif tca_column_name = 'LINE_OF_BUSINESS' then
13048            org_rec.line_of_business := l_entry_value;
13049        elsif tca_column_name = 'YEAR-ESTABLISHED' then
13050            org_rec.year_established := l_entry_value;
13051        elsif tca_column_name = 'TAX_REFERENCE' then
13052            org_rec.tax_reference := l_entry_value;
13053        elsif tca_column_name = 'CEO_NAME' then
13054            org_rec.ceo_name := l_entry_value;
13055        elsif tca_column_name = 'PERSON_FIRST_NAME' then
13056            person_rec.person_first_name := l_entry_value;
13057        elsif tca_column_name = 'PERSON_MIDDLE_NAME' then
13058            person_rec.person_middle_name := l_entry_value;
13059        elsif tca_column_name = 'PERSON_LAST_NAME' then
13060            person_rec.person_last_name := l_entry_value;
13061        elsif tca_column_name = 'PERSON_NAME_SUFFIX' then
13062            person_rec.person_name_suffix := l_entry_value;
13063        elsif tca_column_name = 'PERSON_PRE_NAME_ADJUNCT' then
13064            person_rec.person_pre_name_adjunct := l_entry_value;
13065        elsif tca_column_name = upper('country') then
13066            location_rec.country := l_entry_value;
13067        elsif tca_column_name = upper('address1') then
13068            location_rec.address1 := l_entry_value;
13069        elsif tca_column_name = upper('address2') then
13070            location_rec.address2 := l_entry_value;
13071        elsif tca_column_name = upper('city') then
13072            location_rec.city := l_entry_value;
13073        elsif tca_column_name = upper('county') then
13074            location_rec.county := l_entry_value;
13075        elsif tca_column_name = upper('state') then
13076            location_rec.state := l_entry_value;
13077        elsif tca_column_name = upper('province') then
13078            location_rec.province := l_entry_value;
13079        elsif tca_column_name = upper('postal_code') then
13080            location_rec.postal_code := l_entry_value;
13081        elsif tca_column_name = upper('email_address') then
13082            email_rec.email_address := l_entry_value;
13083        elsif tca_column_name = upper('phone_country_code') then
13084            phone_rec.phone_country_code := l_entry_value;
13085        elsif tca_column_name = upper('phone_area_code') then
13086            phone_rec.phone_area_code := l_entry_value;
13087        elsif tca_column_name = upper('phone_number') then
13088            phone_rec.phone_number := l_entry_value;
13089        elsif tca_column_name = upper('phone_extension') then
13090            phone_rec.phone_extension := l_entry_value;
13091        elsif tca_column_name = upper('department') then
13092            ocon_rec.department := l_entry_value;
13093        elsif tca_column_name = upper('job_title') then
13094            ocon_rec.job_title := l_entry_value;
13095        elsif tca_column_name = upper('decision_maker_flag') then
13096            ocon_rec.decision_maker_flag := l_entry_value;
13097        elsif tca_column_name = upper('sic_code') then
13098            org_rec.sic_code := l_entry_value;
13099        elsif tca_column_name = upper('sic_code_type') then
13100            org_rec.sic_code_type := l_entry_value;
13101        elsif tca_column_name = upper('analysis_fy') then
13102            org_rec.analysis_fy := l_entry_value;
13103        elsif tca_column_name = upper('CURR_FY_POTENTIAL_REVENUE') then
13104            org_rec.CURR_FY_POTENTIAL_REVENUE := l_entry_value;
13105        elsif tca_column_name = upper('NEXT_FY_POTENTIAL_REVENUE') then
13106            org_rec.NEXT_FY_POTENTIAL_REVENUE := l_entry_value;
13107        elsif tca_column_name = upper('GSA_INDICATOR_FLAG') then
13108            org_rec.GSA_INDICATOR_FLAG := l_entry_value;
13109        elsif tca_column_name = upper('MISSION_STATEMENT') then
13110            org_rec.MISSION_STATEMENT := l_entry_value;
13111        elsif tca_column_name = upper('ORGANIZATION_NAME_PHONETIC') then
13112            org_rec.ORGANIZATION_NAME_PHONETIC := l_entry_value;
13113        elsif tca_column_name = upper('CATEGORY_CODE') then
13114            org_rec.party_rec.CATEGORY_CODE := l_entry_value;
13115        elsif tca_column_name = upper('JGZZ_FISCAL_CODE') then
13116            org_rec.JGZZ_FISCAL_CODE := l_entry_value;
13117        elsif tca_column_name = upper('ADDRESS3') then
13118            location_rec.ADDRESS3 := l_entry_value;
13119        elsif tca_column_name = upper('ADDRESS4') then
13120            location_rec.ADDRESS4 := l_entry_value;
13121        elsif tca_column_name = upper('ADDRESS_LINES_PHONETIC') then
13122            location_rec.ADDRESS_LINES_PHONETIC := l_entry_value;
13123        elsif tca_column_name = upper('PO_BOX_NUMBER') then
13124            -- location_rec.PO_BOX_NUMBER := l_entry_value; Refer bug 4704727
13125 	   location_rec.PO_BOX_NUMBER := null;
13126        elsif tca_column_name = upper('HOUSE_NUMBER') then
13127            -- location_rec.HOUSE_NUMBER := l_entry_value; Refer bug 4704727
13128 	   location_rec.HOUSE_NUMBER := null;
13129        elsif tca_column_name = upper('STREET_SUFFIX') then
13130            -- location_rec.STREET_SUFFIX := l_entry_value; Refer bug 4704727
13131 	   location_rec.STREET_SUFFIX := null;
13132        elsif tca_column_name = upper('STREET') then
13133            -- location_rec.STREET := l_entry_value; Refer bug 4704727
13134 	   location_rec.STREET := null;
13135        elsif tca_column_name = upper('STREET_NUMBER') then
13136 	   -- location_rec.STREET_NUMBER := l_entry_value; Refer bug 4704727
13137 	   location_rec.STREET_NUMBER := null;
13138        elsif tca_column_name = upper('FLOOR') then
13139            -- location_rec.FLOOR := l_entry_value; Refer bug 4704727
13140 	   location_rec.FLOOR := null;
13141        elsif tca_column_name = upper('SUITE') then
13142            -- location_rec.SUITE := l_entry_value; Refer bug 4704727
13143 	   location_rec.SUITE := null;
13144        elsif tca_column_name = upper('POSTAL_PLUS4_CODE') then
13145            location_rec.POSTAL_PLUS4_CODE := l_entry_value;
13146        elsif tca_column_name = upper('identifying_address_flag') then
13147            psite_rec.identifying_address_flag := l_entry_value;
13148        elsif tca_column_name = upper('address_effective_date') then
13149            location_rec.address_effective_date := l_entry_value;
13150        elsif tca_column_name = upper('address_expiration_date') then
13151            location_rec.address_expiration_date := l_entry_value;
13152        elsif tca_column_name = upper('branch_flag') then
13153            org_rec.branch_flag := l_entry_value;
13154        elsif tca_column_name = upper('line_of_business') then
13155            org_rec.line_of_business := l_entry_value;
13156        elsif tca_column_name = upper('business_scope') then
13157            org_rec.business_scope := l_entry_value;
13158        elsif tca_column_name = upper('ceo_title') then
13159            org_rec.ceo_title := l_entry_value;
13160        elsif tca_column_name = upper('cong_dist_code') then
13161            org_rec.cong_dist_code := l_entry_value;
13162        elsif tca_column_name = upper('control_yr') then
13163            org_rec.control_yr := l_entry_value;
13164        elsif tca_column_name = upper('corporation_class') then
13165            org_rec.corporation_class := l_entry_value;
13166        elsif tca_column_name = upper('credit_score') then
13167            org_rec.credit_score := l_entry_value;
13168        elsif tca_column_name = upper('credit_score_commentary') then
13169            org_rec.credit_score_commentary := l_entry_value;
13170        elsif tca_column_name = upper('db_rating') then
13171            org_rec.db_rating := l_entry_value;
13172        elsif tca_column_name = upper('date_of_birth') then
13173            person_rec.date_of_birth := l_entry_value;
13174        elsif tca_column_name = upper('') then
13175            person_rec.date_of_death := l_entry_value;
13176        elsif tca_column_name = upper('date_of_death') then
13177            org_rec.debarments_count := l_entry_value;
13178        elsif tca_column_name = upper('debarments_date') then
13179            org_rec.debarments_date := l_entry_value;
13180        elsif tca_column_name = upper('declared_ethnicity') then
13181            person_rec.declared_ethnicity := l_entry_value;
13182        elsif tca_column_name = upper('debarment_ind') then
13183            org_rec.debarment_ind := l_entry_value;
13184        elsif tca_column_name = upper('description') then
13185            location_rec.description := l_entry_value;
13186        elsif tca_column_name = upper('disadv_8a_ind') then
13187            org_rec.disadv_8a_ind := l_entry_value;
13188        elsif tca_column_name = upper('enquiry_duns') then
13189            org_rec.enquiry_duns := l_entry_value;
13190        elsif tca_column_name = upper('export_ind') then
13191            org_rec.export_ind := l_entry_value;
13192        elsif tca_column_name = upper('failure_score') then
13193            org_rec.failure_score := l_entry_value;
13194        elsif tca_column_name = upper('failure_score_commentary') then
13195            org_rec.failure_score_commentary := l_entry_value;
13196        elsif tca_column_name = upper('failure_score_natnl_percentile') then
13197            org_rec.failure_score_natnl_percentile := l_entry_value;
13198        elsif tca_column_name = upper('failure_score_override_code') then
13199            org_rec.failure_score_override_code := l_entry_value;
13200        elsif tca_column_name = upper('global_failure_score') then
13201            org_rec.global_failure_score := l_entry_value;
13202        elsif tca_column_name = upper('hq_branch_ind') then
13203            org_rec.hq_branch_ind := l_entry_value;
13204        elsif tca_column_name = upper('head_of_household_flag') then
13205            person_rec.head_of_household_flag := l_entry_value;
13206        elsif tca_column_name = upper('household_size') then
13207            person_rec.household_size := l_entry_value;
13208        elsif tca_column_name = upper('import_ind') then
13209            org_rec.import_ind := l_entry_value;
13210        elsif tca_column_name = upper('known_as') then
13211            org_rec.known_as := l_entry_value;
13212        elsif tca_column_name = upper('known_as2') then
13213            org_rec.known_as2 := l_entry_value;
13214        elsif tca_column_name = upper('known_as3') then
13215            org_rec.known_as3 := l_entry_value;
13216        elsif tca_column_name = upper('known_as4') then
13217            org_rec.known_as4 := l_entry_value;
13218        elsif tca_column_name = upper('known_as5') then
13219            org_rec.known_as5 := l_entry_value;
13220        elsif tca_column_name = upper('known_as') then
13221            person_rec.known_as := l_entry_value;
13222        elsif tca_column_name = upper('known_as2') then
13223            person_rec.known_as2 := l_entry_value;
13224        elsif tca_column_name = upper('') then
13225            person_rec.known_as3 := l_entry_value;
13226        elsif tca_column_name = upper('known_as3') then
13227            person_rec.known_as4 := l_entry_value;
13228        elsif tca_column_name = upper('known_as5') then
13229            person_rec.known_as5 := l_entry_value;
13230        elsif tca_column_name = upper('labor_surplus_ind') then
13231            org_rec.labor_surplus_ind := l_entry_value;
13232        elsif tca_column_name = upper('local_activity_code') then
13233            org_rec.local_activity_code := l_entry_value;
13234        elsif tca_column_name = upper('local_activity_code_type') then
13235            org_rec.local_activity_code_type := l_entry_value;
13236        elsif tca_column_name = upper('location_directions') then
13237            location_rec.location_directions := l_entry_value;
13238        elsif tca_column_name = upper('marital_status') then
13239            person_rec.marital_status := l_entry_value;
13240        elsif tca_column_name = upper('marital_status_effective_date') then
13241            person_rec.marital_status_effective_date := l_entry_value;
13242        elsif tca_column_name = upper('minority_owned_ind') then
13243            org_rec.minority_owned_ind := l_entry_value;
13244        elsif tca_column_name = upper('minority_owned_type') then
13245            org_rec.minority_owned_type := l_entry_value;
13246        elsif tca_column_name = upper('organization_type') then
13247            org_rec.organization_type := l_entry_value;
13248        elsif tca_column_name = upper('') then
13249            web_rec.url := l_entry_value;
13250        elsif tca_column_name = upper('url') then
13251            org_rec.oob_ind := l_entry_value;
13252        elsif tca_column_name = upper('personal_income') then
13253            person_rec.personal_income := l_entry_value;
13254        elsif tca_column_name = upper('person_academic_title') then
13255            person_rec.person_academic_title := l_entry_value;
13256        elsif tca_column_name = upper('person_first_name_phonetic') then
13257            person_rec.person_first_name_phonetic := l_entry_value;
13258        elsif tca_column_name = upper('person_last_name_phonetic') then
13259            person_rec.person_last_name_phonetic := l_entry_value;
13260        elsif tca_column_name = upper('middle_name_phonetic') then
13261            person_rec.middle_name_phonetic := l_entry_value;
13262        elsif tca_column_name = upper('person_name_phonetic') then
13263            person_rec.person_name_phonetic := l_entry_value;
13264        elsif tca_column_name = upper('person_previous_last_name') then
13265            person_rec.person_previous_last_name := l_entry_value;
13266        elsif tca_column_name = upper('place_of_birth') then
13267            person_rec.place_of_birth := l_entry_value;
13268        elsif tca_column_name = upper('principal_name') then
13269            org_rec.principal_name := l_entry_value;
13270        elsif tca_column_name = upper('principal_title') then
13271            org_rec.principal_title := l_entry_value;
13272        elsif tca_column_name = upper('public_private_ownership_flag') then
13273            org_rec.public_private_ownership_flag := l_entry_value;
13274        elsif tca_column_name = upper('') then
13275            org_rec.rent_own_ind := l_entry_value;
13276        elsif tca_column_name = upper('person_academic_title') then
13277            person_rec.person_academic_title := l_entry_value;
13278        elsif tca_column_name = upper('short_description') then
13279            location_rec.short_description := l_entry_value;
13280        elsif tca_column_name = upper('small_bus_ind') then
13281            org_rec.small_bus_ind := l_entry_value;
13282        elsif tca_column_name = upper('woman_owned_ind') then
13283            org_rec.woman_owned_ind := l_entry_value;
13284        elsif tca_column_name = upper('attribute1') then
13285            org_rec.party_rec.attribute1 := l_entry_value;
13286        elsif tca_column_name = upper('attribute2') then
13287            org_rec.party_rec.attribute2 := l_entry_value;
13288        elsif tca_column_name = upper('attribute3') then
13289            org_rec.party_rec.attribute3 := l_entry_value;
13290        elsif tca_column_name = upper('attribute4') then
13291            org_rec.party_rec.attribute4 := l_entry_value;
13292        elsif tca_column_name = upper('attribute5') then
13293            org_rec.party_rec.attribute5 := l_entry_value;
13294        elsif tca_column_name = upper('attribute6') then
13295            org_rec.party_rec.attribute6 := l_entry_value;
13296        elsif tca_column_name = upper('attribute7') then
13297            org_rec.party_rec.attribute7 := l_entry_value;
13298        elsif tca_column_name = upper('attribute8') then
13299            org_rec.party_rec.attribute8 := l_entry_value;
13300        elsif tca_column_name = upper('attribute9') then
13301            org_rec.party_rec.attribute9 := l_entry_value;
13302        elsif tca_column_name = upper('attribute10') then
13303            org_rec.party_rec.attribute10 := l_entry_value;
13304        elsif tca_column_name = upper('attribute11') then
13305            org_rec.party_rec.attribute11 := l_entry_value;
13306        elsif tca_column_name = upper('attribute12') then
13307            org_rec.party_rec.attribute12 := l_entry_value;
13308        elsif tca_column_name = upper('attribute13') then
13309            org_rec.party_rec.attribute13 := l_entry_value;
13310        elsif tca_column_name = upper('attribute14') then
13311            org_rec.party_rec.attribute14 := l_entry_value;
13312        elsif tca_column_name = upper('') then
13313            org_rec.party_rec.attribute15 := l_entry_value;
13314        elsif tca_column_name = upper('attribute1') then
13315            person_rec.attribute1 := l_entry_value;
13316        elsif tca_column_name = upper('attribute2') then
13317            person_rec.attribute2 := l_entry_value;
13318        elsif tca_column_name = upper('attribute3') then
13319            person_rec.attribute3 := l_entry_value;
13320        elsif tca_column_name = upper('attribute4') then
13321            person_rec.attribute4 := l_entry_value;
13322        elsif tca_column_name = upper('attribute5') then
13323            person_rec.attribute5 := l_entry_value;
13324        elsif tca_column_name = upper('attribute6') then
13325            person_rec.attribute6 := l_entry_value;
13326        elsif tca_column_name = upper('attribute7') then
13327            person_rec.attribute7 := l_entry_value;
13328        elsif tca_column_name = upper('attribute8') then
13329            person_rec.attribute8 := l_entry_value;
13330        elsif tca_column_name = upper('attribute9') then
13331            person_rec.attribute9 := l_entry_value;
13332        elsif tca_column_name = upper('attribute10') then
13333            person_rec.attribute10 := l_entry_value;
13334        elsif tca_column_name = upper('attribute11') then
13335            person_rec.attribute11 := l_entry_value;
13336        elsif tca_column_name = upper('attribute12') then
13337            person_rec.attribute12 := l_entry_value;
13338        elsif tca_column_name = upper('attribute13') then
13339            person_rec.attribute13 := l_entry_value;
13340        elsif tca_column_name = upper('attribute14') then
13341            person_rec.attribute14 := l_entry_value;
13342        elsif tca_column_name = upper('attribute15') then
13343            person_rec.attribute15 := l_entry_value;
13344        elsif tca_column_name = upper('attribute1') then
13345            ocon_rec.attribute1 := l_entry_value;
13346        elsif tca_column_name = upper('attribute2') then
13347            ocon_rec.attribute2 := l_entry_value;
13348        elsif tca_column_name = upper('attribute3') then
13349            ocon_rec.attribute3 := l_entry_value;
13350        elsif tca_column_name = upper('attribute4') then
13351            ocon_rec.attribute4 := l_entry_value;
13352        elsif tca_column_name = upper('attribute5') then
13353            ocon_rec.attribute5 := l_entry_value;
13354        elsif tca_column_name = upper('attribute6') then
13355            ocon_rec.attribute6 := l_entry_value;
13356        elsif tca_column_name = upper('attribute7') then
13357            ocon_rec.attribute7 := l_entry_value;
13358        elsif tca_column_name = upper('attribute8') then
13359            ocon_rec.attribute8 := l_entry_value;
13360        elsif tca_column_name = upper('attribute9') then
13361            ocon_rec.attribute9 := l_entry_value;
13362        elsif tca_column_name = upper('attribute10') then
13363            ocon_rec.attribute10 := l_entry_value;
13364        elsif tca_column_name = upper('attribute11') then
13365            ocon_rec.attribute11 := l_entry_value;
13366        elsif tca_column_name = upper('attribute12') then
13367            ocon_rec.attribute12 := l_entry_value;
13368        elsif tca_column_name = upper('attribute13') then
13369            ocon_rec.attribute13 := l_entry_value;
13370        elsif tca_column_name = upper('attribute14') then
13371            ocon_rec.attribute14 := l_entry_value;
13372        elsif tca_column_name = upper('attribute15') then
13373            ocon_rec.attribute15 := l_entry_value;
13374        elsif tca_column_name = upper('attribute1') then
13375            location_rec.attribute1 := l_entry_value;
13376        elsif tca_column_name = upper('attribute2') then
13377            location_rec.attribute2 := l_entry_value;
13378        elsif tca_column_name = upper('attribute3') then
13379            location_rec.attribute3 := l_entry_value;
13380        elsif tca_column_name = upper('attribute4') then
13381            location_rec.attribute4 := l_entry_value;
13382        elsif tca_column_name = upper('attribute5') then
13383            location_rec.attribute5 := l_entry_value;
13384        elsif tca_column_name = upper('attribute6') then
13385            location_rec.attribute6 := l_entry_value;
13386        elsif tca_column_name = upper('attribute7') then
13387            location_rec.attribute7 := l_entry_value;
13388        elsif tca_column_name = upper('attribute8') then
13389            location_rec.attribute8 := l_entry_value;
13390        elsif tca_column_name = upper('attribute9') then
13391            location_rec.attribute9 := l_entry_value;
13392        elsif tca_column_name = upper('attribute10') then
13393            location_rec.attribute10 := l_entry_value;
13394        elsif tca_column_name = upper('attribute11') then
13395            location_rec.attribute11 := l_entry_value;
13396        elsif tca_column_name = upper('attribute12') then
13397            location_rec.attribute12 := l_entry_value;
13398        elsif tca_column_name = upper('attribute13') then
13399            location_rec.attribute13 := l_entry_value;
13400        elsif tca_column_name = upper('attribute14') then
13401            location_rec.attribute14 := l_entry_value;
13402        elsif tca_column_name = upper('attribute15') then
13403            location_rec.attribute15 := l_entry_value;
13404        elsif tca_column_name = upper('phone_country_code') then
13405            fax_rec.phone_country_code := l_entry_value;
13406        elsif tca_column_name = upper('phone_area_code') then
13407            fax_rec.phone_area_code := l_entry_value;
13408        elsif tca_column_name = upper('phone_number') then
13409            fax_rec.phone_number := l_entry_value;
13410        elsif tca_column_name = upper('attribute_category') then
13411            org_rec.party_rec.attribute_category := l_entry_value;
13412        elsif tca_column_name = upper('attribute_category') then
13413            person_rec.attribute_category := l_entry_value;
13414        elsif tca_column_name = upper('attribute_category') then
13415            ocon_rec.attribute_category := l_entry_value;
13416        elsif tca_column_name = upper('attribute_category') then
13417            location_rec.attribute_category := l_entry_value;
13418        elsif tca_column_name = upper('site_use_type') then
13419            psiteuse_rec.site_use_type := l_entry_value;
13420        end if;
13421 
13422        --TCA mandate: bug 4587049
13423          --party_rec.created_by_module := 'AMS';
13424          org_rec.created_by_module := 'AMS';
13425          person_rec.created_by_module := 'AMS';
13426          ocon_rec.created_by_module := 'AMS';
13427          location_rec.created_by_module := 'AMS';
13428          psite_rec.created_by_module := 'AMS';
13429          psiteuse_rec.created_by_module := 'AMS';
13430 
13431    end if;   -- l_b2b_flag = 'Y'
13432 
13433    if l_b2b_flag = 'B2C' then
13434 	l_b2b := 'N';
13435         if tca_column_name = upper('person_first_name') then
13436            person_rec.person_first_name := l_entry_value;
13437        elsif tca_column_name = upper('person_middle_name') then
13438            person_rec.person_middle_name := l_entry_value;
13439        elsif tca_column_name = upper('person_last_name') then
13440            person_rec.person_last_name := l_entry_value;
13441        elsif tca_column_name = upper('person_name_suffix') then
13442            person_rec.person_name_suffix := l_entry_value;
13443        elsif tca_column_name = upper('person_pre_name_adjunct') then
13444            person_rec.person_pre_name_adjunct := l_entry_value;
13445        elsif tca_column_name = upper('country') then
13446            location_rec.country := l_entry_value;
13447        elsif tca_column_name = upper('address1') then
13448            location_rec.address1 := l_entry_value;
13449        elsif tca_column_name = upper('address2') then
13450            location_rec.address2 := l_entry_value;
13451        elsif tca_column_name = upper('city') then
13452            location_rec.city := l_entry_value;
13453        elsif tca_column_name = upper('county') then
13454            location_rec.county := l_entry_value;
13455        elsif tca_column_name = upper('state') then
13456            location_rec.state := l_entry_value;
13457        elsif tca_column_name = upper('province') then
13458            location_rec.province := l_entry_value;
13459        elsif tca_column_name = upper('postal_code') then
13460            location_rec.postal_code := l_entry_value;
13461        elsif tca_column_name = upper('email_address') then
13462            email_rec.email_address := l_entry_value;
13463        elsif tca_column_name = upper('phone_country_code') then
13464            phone_rec.phone_country_code := l_entry_value;
13465        elsif tca_column_name = upper('phone_area_code') then
13466            phone_rec.phone_area_code := l_entry_value;
13467        elsif tca_column_name = upper('phone_number') then
13468            phone_rec.phone_number := l_entry_value;
13469        elsif tca_column_name = upper('phone_extension') then
13470            phone_rec.phone_extension := l_entry_value;
13471        elsif tca_column_name = upper('SALUTATION') then
13472            person_rec.party_rec.SALUTATION := l_entry_value;
13473        elsif tca_column_name = upper('ADDRESS3') then
13474            location_rec.ADDRESS3 := l_entry_value;
13475        elsif tca_column_name = upper('ADDRESS4') then
13476            location_rec.ADDRESS4 := l_entry_value;
13477        elsif tca_column_name = upper('ADDRESS_LINES_PHONETIC') then
13478            location_rec.ADDRESS_LINES_PHONETIC := l_entry_value;
13479        elsif tca_column_name = upper('PO_BOX_NUMBER') then
13480            -- location_rec.PO_BOX_NUMBER := l_entry_value; Refer bug 4704727
13481 	   location_rec.PO_BOX_NUMBER := null;
13482        elsif tca_column_name = upper('HOUSE_NUMBER') then
13483            -- location_rec.HOUSE_NUMBER := l_entry_value; Refer bug 4704727
13484 	   location_rec.HOUSE_NUMBER := null;
13485        elsif tca_column_name = upper('STREET_SUFFIX') then
13486            -- location_rec.STREET_SUFFIX := l_entry_value; Refer bug 4704727
13487 	   location_rec.STREET_SUFFIX := null;
13488        elsif tca_column_name = upper('STREET') then
13489            -- location_rec.STREET := l_entry_value; Refer bug 4704727
13490 	   location_rec.STREET := null;
13491        elsif tca_column_name = upper('STREET_NUMBER') then
13492 	   -- location_rec.STREET_NUMBER := l_entry_value; Refer bug 4704727
13493 	   location_rec.STREET_NUMBER := null;
13494        elsif tca_column_name = upper('FLOOR') then
13495            -- location_rec.FLOOR := l_entry_value; Refer bug 4704727
13496 	   location_rec.FLOOR := null;
13497        elsif tca_column_name = upper('SUITE') then
13498            -- location_rec.SUITE := l_entry_value; Refer bug 4704727
13499 	   location_rec.SUITE := null;
13500        elsif tca_column_name = upper('POSTAL_PLUS4_CODE') then
13501            location_rec.POSTAL_PLUS4_CODE := l_entry_value;
13502        elsif tca_column_name = upper('identifying_address_flag') then
13503            psite_rec.identifying_address_flag := l_entry_value;
13504        elsif tca_column_name = upper('person_last_name_phonetic') then
13505            person_rec.person_last_name_phonetic := l_entry_value;
13506        elsif tca_column_name = upper('person_first_name_phonetic') then
13507            person_rec.person_first_name_phonetic := l_entry_value;
13508        elsif tca_column_name = upper('url') then
13509            web_rec.url := l_entry_value;
13510        elsif tca_column_name = upper('person_academic_title') then
13511            person_rec.person_academic_title := l_entry_value;
13512        elsif tca_column_name = upper('date_of_birth') then
13513            person_rec.date_of_birth := l_entry_value;
13514        elsif tca_column_name = upper('person_academic_title') then
13515            person_rec.person_academic_title := l_entry_value;
13516        elsif tca_column_name = upper('person_previous_last_name') then
13517            person_rec.person_previous_last_name := l_entry_value;
13518        elsif tca_column_name = upper('known_as') then
13519            person_rec.known_as := l_entry_value;
13520        elsif tca_column_name = upper('known_as2') then
13521            person_rec.known_as2 := l_entry_value;
13522        elsif tca_column_name = upper('known_as3') then
13523            person_rec.known_as3 := l_entry_value;
13524        elsif tca_column_name = upper('known_as4') then
13525            person_rec.known_as4 := l_entry_value;
13526        elsif tca_column_name = upper('known_as5') then
13527            person_rec.known_as5 := l_entry_value;
13528        elsif tca_column_name = upper('person_name_phonetic') then
13529            person_rec.person_name_phonetic := l_entry_value;
13530        elsif tca_column_name = upper('middle_name_phonetic') then
13531            person_rec.middle_name_phonetic := l_entry_value;
13532        elsif tca_column_name = upper('jgzz_fiscal_code') then
13533            person_rec.jgzz_fiscal_code := l_entry_value;
13534        elsif tca_column_name = upper('place_of_birth') then
13535            person_rec.place_of_birth := l_entry_value;
13536        elsif tca_column_name = upper('date_of_death') then
13537            person_rec.date_of_death := l_entry_value;
13538        elsif tca_column_name = upper('declared_ethnicity') then
13539            person_rec.declared_ethnicity := l_entry_value;
13540        elsif tca_column_name = upper('marital_status') then
13541            person_rec.marital_status := l_entry_value;
13542        elsif tca_column_name = upper('personal_income') then
13543            person_rec.personal_income := l_entry_value;
13544        elsif tca_column_name = upper('marital_status_effective_date') then
13545            person_rec.marital_status_effective_date := l_entry_value;
13546        elsif tca_column_name = upper('head_of_household_flag') then
13547            person_rec.head_of_household_flag := l_entry_value;
13548        elsif tca_column_name = upper('household_size') then
13549            person_rec.household_size := l_entry_value;
13550        elsif tca_column_name = upper('location_directions') then
13551            location_rec.location_directions := l_entry_value;
13552        elsif tca_column_name = upper('address_effective_date') then
13553            location_rec.address_effective_date := l_entry_value;
13554        elsif tca_column_name = upper('address_expiration_date') then
13555 	   location_rec.address_expiration_date := l_entry_value;
13556        elsif tca_column_name = upper('attribute1') then
13557            person_rec.party_rec.attribute1 := l_entry_value;
13558        elsif tca_column_name = upper('attribute2') then
13559            person_rec.party_rec.attribute2 := l_entry_value;
13560        elsif tca_column_name = upper('attribute3') then
13561            person_rec.party_rec.attribute3 := l_entry_value;
13562        elsif tca_column_name = upper('attribute4') then
13563            person_rec.party_rec.attribute4 := l_entry_value;
13564        elsif tca_column_name = upper('attribute5') then
13565            person_rec.party_rec.attribute5 := l_entry_value;
13566        elsif tca_column_name = upper('attribute6') then
13567            person_rec.party_rec.attribute6 := l_entry_value;
13568        elsif tca_column_name = upper('attribute7') then
13569            person_rec.party_rec.attribute7 := l_entry_value;
13570        elsif tca_column_name = upper('attribute8') then
13571            person_rec.party_rec.attribute8 := l_entry_value;
13572        elsif tca_column_name = upper('') then
13573            person_rec.party_rec.attribute9 := l_entry_value;
13574        elsif tca_column_name = upper('attribute9') then
13575            person_rec.party_rec.attribute10 := l_entry_value;
13576        elsif tca_column_name = upper('attribute11') then
13577            person_rec.party_rec.attribute11 := l_entry_value;
13578        elsif tca_column_name = upper('attribute12') then
13579            person_rec.party_rec.attribute12 := l_entry_value;
13580        elsif tca_column_name = upper('attribute13') then
13581            person_rec.party_rec.attribute13 := l_entry_value;
13582        elsif tca_column_name = upper('attribute14') then
13583            person_rec.party_rec.attribute14 := l_entry_value;
13584        elsif tca_column_name = upper('attribute15') then
13585            person_rec.party_rec.attribute15 := l_entry_value;
13586        elsif tca_column_name = upper('attribute1') then
13587            location_rec.attribute1 := l_entry_value;
13588        elsif tca_column_name = upper('attribute2') then
13589            location_rec.attribute2 := l_entry_value;
13590        elsif tca_column_name = upper('attribute3') then
13591            location_rec.attribute3 := l_entry_value;
13592        elsif tca_column_name = upper('attribute4') then
13593            location_rec.attribute4 := l_entry_value;
13594        elsif tca_column_name = upper('attribute5') then
13595            location_rec.attribute5 := l_entry_value;
13596        elsif tca_column_name = upper('attribute6') then
13597            location_rec.attribute6 := l_entry_value;
13598        elsif tca_column_name = upper('attribute7') then
13599            location_rec.attribute7 := l_entry_value;
13600        elsif tca_column_name = upper('attribute8') then
13601            location_rec.attribute8 := l_entry_value;
13602        elsif tca_column_name = upper('attribute9') then
13603            location_rec.attribute9 := l_entry_value;
13604        elsif tca_column_name = upper('attribute10') then
13605            location_rec.attribute10 := l_entry_value;
13606        elsif tca_column_name = upper('attribute11') then
13607            location_rec.attribute11 := l_entry_value;
13608        elsif tca_column_name = upper('attribute12') then
13609            location_rec.attribute12 := l_entry_value;
13610        elsif tca_column_name = upper('attribute13') then
13611            location_rec.attribute13 := l_entry_value;
13612        elsif tca_column_name = upper('attribute14') then
13613            location_rec.attribute14 := l_entry_value;
13614        elsif tca_column_name = upper('attribute15') then
13615            location_rec.attribute15 := l_entry_value;
13616        elsif tca_column_name = upper('phone_country_code') then
13617            fax_rec.phone_country_code := l_entry_value;
13618        elsif tca_column_name = upper('phone_area_code') then
13619            fax_rec.phone_area_code := l_entry_value;
13620        elsif tca_column_name = upper('phone_number') then
13621 	   fax_rec.phone_number := l_entry_value;
13622        elsif tca_column_name = upper('attribute_category') then
13623            person_rec.party_rec.attribute_category := l_entry_value;
13624        elsif tca_column_name = upper('attribute_category') then
13625            location_rec.attribute_category  := l_entry_value;
13626        elsif tca_column_name = upper('short_description') then
13627            location_rec.short_description := l_entry_value;
13628        elsif tca_column_name = upper('description') then
13629            location_rec.description := l_entry_value;
13630        elsif tca_column_name = upper('site_use_type') then
13631            psiteuse_rec.site_use_type := l_entry_value;
13632        elsif tca_column_name = upper('orig_system_reference') then
13633            person_rec.party_rec.orig_system_reference := l_entry_value;
13634        elsif tca_column_name = upper('tax_reference') then
13635            person_rec.tax_reference := l_entry_value;
13636        elsif tca_column_name = upper('rent_own_ind') then
13637            person_rec.rent_own_ind := l_entry_value;
13638        elsif tca_column_name = upper('gender') then
13639            person_rec.gender := l_entry_value;
13640        elsif tca_column_name = upper('HOUSEHOLD_INCOME') then
13641            person_rec.HOUSEHOLD_INCOME  := l_entry_value;
13642        end if;
13643 
13644        --TCA mandate: bug 4587049
13645          --party_rec.created_by_module := 'AMS';
13646          person_rec.created_by_module := 'AMS';
13647          location_rec.created_by_module := 'AMS';
13648          psite_rec.created_by_module := 'AMS';
13649          psiteuse_rec.created_by_module := 'AMS';
13650 
13651       end if; -- l_b2b_flag = 'N'
13652      end loop; -- for c_tca_columns
13653      close c_tca_columns;
13654      l_entry_value := NULL;
13655 
13656  -- DBMS_OUTPUT.PUT_LINE('before tca call');
13657   --write_to_act_log('Calling ams_list_import_pub to create customer', 'LIST', p_list_header_id,'LOW');
13658 
13659      -- ---------------------------TCA CALL ---------------------------------
13660 
13661 
13662   AMS_List_Import_PUB.Create_Customer (
13663   p_api_version              => 1,
13664   p_init_msg_list            => 'T',
13665   p_commit                   => 'F',
13666   x_return_status            => x_return_status,
13667   x_msg_count                => x_msg_count,
13668   x_msg_data                 => x_msg_data,
13669   p_party_id                 => l_party_id,
13670   p_b2b_flag                 => l_b2b,
13671   p_import_list_header_id    =>null,
13672   p_party_rec                => party_rec,
13673   p_org_rec                  => org_rec,
13674   p_person_rec               => person_rec,
13675   p_location_rec             => location_rec,
13676   p_psite_rec                => psite_rec,
13677   p_cpoint_rec               => cpoint_rec,
13678   p_email_rec                => email_rec,
13679   p_phone_rec                => phone_rec,
13680   p_fax_rec                  => fax_rec,
13681   p_ocon_rec                 => ocon_rec,
13682   p_siteuse_rec              => psiteuse_rec,
13683   p_web_rec                  => web_rec,
13684   x_new_party                => l_new_party,
13685   p_component_name           => l_component_name,
13686   l_import_source_line_id    => null
13687   );
13688    -- write_to_act_log('Total no of messages '||x_msg_count, 'LIST', p_list_header_id,'LOW');
13689    -- write_to_act_log('List entry id is '||l_list_entry_id, 'LIST', p_list_header_id,'LOW');
13690    -- write_to_act_log('Party id is '||l_party_id, 'LIST', p_list_header_id,'LOW');
13691   if x_msg_count > 0 then
13692     FOR i IN 1..x_msg_count  LOOP
13693          -- Following code was modified by bmuthukr
13694          --x_tmp_var := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
13695          --x_tmp_var1 := substrb(x_tmp_var1 || ' '|| x_tmp_var,1,4000);
13696 	 x_tmp_var := 'Please make sure that the required fields (eg. person first name, last name) are available in the remote database';
13697          write_to_act_log('Updating the tca_load_status as ERROR', 'LIST', p_list_header_id,'HIGH');
13698 
13699 	 -- DBMS_OUTPUT.PUT_LINE('TCA Upload Process : x_tmp_var  ->' || x_tmp_var );
13700 	 -- DBMS_OUTPUT.PUT_LINE('TCA Upload Process : x_tmp_var  ->' || x_tmp_var1 );
13701     END LOOP;
13702     update ams_list_entries set error_flag = 'E',
13703 				tca_load_status = 'ERROR',
13704 				ENABLED_FLAG = 'N',
13705 				--error_text = 'TCA API ERROR :'||substr(x_tmp_var1,1,3000)
13706 				error_text = 'TCA API ERROR :'||x_tmp_var
13707     where list_entry_id = l_list_entry_id;
13708   end if;
13709     if l_party_id is  null then
13710     update ams_list_entries set error_flag = 'E',
13711 				tca_load_status = 'ERROR',
13712 				ENABLED_FLAG = 'N',
13713 				--error_text = 'TCA API ERROR :'||nvl(x_tmp_var,substr(x_tmp_var1,1,3000))
13714 				error_text = 'TCA API ERROR :'||x_tmp_var
13715     where list_entry_id = l_list_entry_id;
13716    end if;
13717 
13718   if l_party_id is not null then
13719     update ams_list_entries set party_id = l_party_id,
13720 				error_flag = 'S',
13721 				ENABLED_FLAG = 'Y',
13722                                 tca_load_status = 'SUCCESS'
13723     where list_entry_id = l_list_entry_id;
13724    end if;
13725    l_party_id := NULL;
13726  -- DBMS_OUTPUT.PUT_LINE('after tca call');
13727 
13728      -- ---------------------------TCA CALL ---------------------------------
13729 
13730   END LOOP; -- For c_list_entries
13731   close c_list_entries;
13732   update ams_list_headers_all
13733      set migration_date = sysdate
13734    where list_header_id = p_list_header_id;
13735 
13736   write_to_act_log('Executed procedure tca_upload_process', 'LIST', g_list_header_id,'LOW');
13737 
13738   -- Added for cancel list gen as it prevents parallel update- Raghu
13739   -- of list headers when cancel button is pressed
13740 
13741   --TCA mandate: bug 4587049
13742   --Enable/restore TCA events after bulk TCA data processing
13743   if l_prof <> 'N' then
13744      fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13745   end if;
13746 
13747   commit;
13748 
13749 EXCEPTION
13750    WHEN FND_API.G_EXC_ERROR THEN
13751      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13752      x_return_status := FND_API.G_RET_STS_ERROR ;
13753 
13754      if l_prof <> 'N' then
13755         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13756      end if;
13757 
13758       FND_MSG_PUB.count_and_get(
13759             p_encoded => FND_API.g_false,
13760             p_count   => x_msg_count,
13761             p_data    => x_msg_data
13762       );
13763 
13764 
13765 
13766   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13767      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13768      x_return_status := FND_API.G_RET_STS_ERROR ;
13769 
13770      if l_prof <> 'N' then
13771         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13772      end if;
13773 
13774       FND_MSG_PUB.count_and_get(
13775             p_encoded => FND_API.g_false,
13776             p_count   => x_msg_count,
13777             p_data    => x_msg_data
13778       );
13779 
13780   WHEN OTHERS THEN
13781      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13782      x_return_status := FND_API.G_RET_STS_ERROR ;
13783 
13784      if l_prof <> 'N' then
13785         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13786      end if;
13787 
13788       FND_MSG_PUB.count_and_get(
13789             p_encoded => FND_API.g_false,
13790             p_count   => x_msg_count,
13791             p_data    => x_msg_data
13792       );
13793 
13794 END tca_upload_process;
13795 
13796 PROCEDURE remote_list_gen
13797              (p_list_header_id     in  number,
13798               x_return_status      OUT NOCOPY VARCHAR2,
13799               x_msg_count          OUT NOCOPY NUMBER,
13800               x_msg_data           OUT NOCOPY VARCHAR2,
13801 	      x_remote_gen         OUT NOCOPY VARCHAR2)
13802 IS
13803   l_remote_list_gen	varchar2(1) := 'N';
13804   l_list_selection      varchar2(1);
13805   l_onlylist_selection  varchar2(1);
13806 
13807   cursor c_list_selection is
13808   select 'Y' from ams_list_select_actions
13809    where  action_used_by_id = p_list_header_id
13810      and  arc_action_used_by = 'LIST'
13811      and  arc_incl_object_from in ('CELL','DIWB','SQL');
13812 
13813   cursor c_only_list_selection is
13814   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
13815    where  act.action_used_by_id = p_list_header_id
13816      and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
13817      and  act.INCL_OBJECT_ID = head.list_header_id
13818      and  head.status_code = 'AVAILABLE'
13819      and  (head.MIGRATION_DATE is null or head.main_gen_end_time > head.migration_date);
13820 
13821 begin
13822 
13823      open c_list_selection;
13824      fetch c_list_selection into l_list_selection;
13825      close c_list_selection;
13826 
13827      if nvl(l_list_selection,'N') = 'Y' then
13828         write_to_act_log(p_msg_data => 'List selection includes segments/workbook/SQL.' ,
13829                          p_arc_log_used_by => 'LIST',
13830                          p_log_used_by_id  => p_list_header_id,
13831                          p_level => 'LOW');
13832         x_remote_gen := 'Y';
13833         x_return_status := FND_API.G_RET_STS_SUCCESS;
13834      else
13835         write_to_act_log(p_msg_data => 'List selection does not include segments/workbook/SQL.' ,
13836                          p_arc_log_used_by => 'LIST',
13837                          p_log_used_by_id  => p_list_header_id,
13838                          p_level => 'LOW');
13839      end if;
13840 
13841      if l_list_selection is null then
13842         open c_only_list_selection;
13843         fetch c_only_list_selection into l_onlylist_selection;
13844         close c_only_list_selection;
13845         if nvl(l_onlylist_selection,'N') = 'Y' then
13846            write_to_act_log(p_msg_data => 'List/TG will be generated in remote instance.' ,
13847                             p_arc_log_used_by => 'LIST',
13848                             p_log_used_by_id  => p_list_header_id,
13849 			    p_level => 'LOW');
13850            x_remote_gen := 'Y';
13851            x_return_status := FND_API.G_RET_STS_SUCCESS;
13852         else
13853            write_to_act_log(p_msg_data => 'List/TG will be generated in local instance.' ,
13854                             p_arc_log_used_by => 'LIST',
13855                             p_log_used_by_id  => p_list_header_id,
13856                             p_level => 'LOW');
13857            x_remote_gen := 'N';
13858            x_return_status := FND_API.G_RET_STS_SUCCESS;
13859         end if;
13860      end if;
13861 
13862   EXCEPTION
13863   WHEN OTHERS THEN
13864     write_to_act_log('Error while executing procedure remote_list_gen '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13865      x_return_status := FND_API.G_RET_STS_ERROR ;
13866      x_msg_data := 'Error while executing procedure remote_list_gen '||sqlcode||'  '||sqlerrm;
13867      x_msg_count := 1;
13868       FND_MSG_PUB.count_and_get(
13869             p_encoded => FND_API.g_false,
13870             p_count   => x_msg_count,
13871             p_data    => x_msg_data
13872       );
13873 
13874 end remote_list_gen;
13875 
13876 --Procedure is_manual added by bmuthukr for bug 3710720
13877 PROCEDURE is_manual
13878              (p_list_header_id     in  number,
13879               x_return_status      OUT NOCOPY VARCHAR2,
13880               x_msg_count          OUT NOCOPY NUMBER,
13881               x_msg_data           OUT NOCOPY VARCHAR2,
13882               x_is_manual          OUT NOCOPY varchar2 ) is
13883 
13884   cursor c1 is
13885   select list_type
13886     from ams_list_headers_all
13887    where list_header_id = p_list_header_id;
13888 
13889   cursor c2 is
13890   select 1
13891     from ams_list_entries
13892    where list_header_id = p_list_header_id
13893      and enabled_flag = 'Y'
13894      and rownum = 1;
13895 
13896   cursor c_get_list_used_by_id is
13897   select list_used_by_id
13898     from ams_act_lists
13899    where list_header_id = p_list_header_id
13900      and list_used_by = 'CSCH'
13901      and list_act_type = 'TARGET';
13902 
13903   cursor c3 is
13904   select 1
13905     from ams_list_select_actions
13906    where list_header_id = p_list_header_id
13907      and arc_incl_object_from  = 'EMPLOYEE';
13908 
13909   cursor c4(l_list_used_by_id number) is
13910   select 1
13911     from ams_act_lists
13912    where list_used_by_id = l_list_used_by_id
13913      and list_act_type = 'EMPLOYEE';
13914 
13915 l_list_type        varchar2(100) := null;
13916 l_dummy            number := 0;
13917 l_list_used_by_id  number := 0;
13918 
13919 
13920 begin
13921    open c1;
13922    fetch c1 into l_list_type;
13923    close c1;
13924 
13925    write_to_act_log('List is of type '||l_list_type,'LIST',p_list_header_id,'HIGH');
13926 
13927    if l_list_type = 'MANUAL' then --List type is manual. Make the status as either available/draft.
13928       write_to_act_log('List is of type MANUAL. Cannot regenerate. ','LIST',p_list_header_id,'HIGH');
13929       x_is_manual := 'Y';
13930    else
13931       x_is_manual := 'N';
13932    end if;
13933 
13934    if l_list_type = 'TARGET' then -- To see if any of the incl are based on emp list in TG
13935       open c_get_list_used_by_id;
13936       fetch c_get_list_used_by_id into l_list_used_by_id;
13937       close c_get_list_used_by_id;
13938 
13939       open c4(l_list_used_by_id);
13940       fetch c4 into l_dummy;
13941       if c4%found then
13942          write_to_act_log('Target group inclusions has EMPLOYEE list. Cannot generate','LIST',p_list_header_id,'HIGH');
13943          x_is_manual := 'Y';
13944       end if;
13945       close c4;
13946    end if;
13947 
13948    if l_list_type = 'STANDARD' then -- To see if any of the incl are based on emp list in std list
13949       open c3;
13950       fetch c3 into l_dummy;
13951       if c3%found then
13952          write_to_act_log('List inclusions has EMPLOYEE list. Cannot generate','LIST',p_list_header_id,'HIGH');
13953          x_is_manual := 'Y';
13954       end if;
13955       close c3;
13956    end if;
13957 
13958 
13959    if nvl(x_is_manual,'N') = 'Y' then --Either if it is manual list or if any of the incl are of type emp list.
13960       open c2;
13961       fetch c2 into l_dummy;
13962       if c2%found then -- List already has enabled entries.. So making it as available.
13963          update ams_list_headers_all
13964             set status_code          = 'AVAILABLE',
13965                 user_status_id       = 303,
13966                 status_date          = sysdate,
13967                 last_update_date     = sysdate
13968           where list_header_id       = p_list_header_id;
13969       else  -- No enabled entries..So it will be in DRAFT status
13970          update ams_list_headers_all
13971             set status_code          = 'DRAFT',
13972                 user_status_id       = 300,
13973                 status_date          = sysdate,
13974                 last_update_date     = sysdate
13975           where list_header_id       = p_list_header_id;
13976       end if;
13977       close c2;
13978 
13979       -- Added for cancel list gen as it prevents parallel update- Raghu
13980       -- of list headers when cancel button is pressed
13981       commit;
13982    end if;
13983 
13984 exception
13985    when others then
13986       write_to_act_log('Error while executing procedure is_manual '||sqlcode||'   '||sqlerrm,'LIST',p_list_header_id,'HIGH');
13987       x_return_status := FND_API.G_RET_STS_ERROR ;
13988       x_msg_data := 'Error while executing procedure is_manual '||sqlcode||'  '||sqlerrm;
13989       x_msg_count := 1;
13990 end is_manual;
13991 
13992 procedure upd_list_header_info(p_list_header_id in number,
13993                                x_msg_count      out nocopy number,
13994                                x_msg_data       out nocopy varchar2,
13995                                x_return_status  out nocopy varchar2) is
13996 
13997 l_list_type                     varchar2(100);
13998 l_remote_flag                   varchar2(1);
13999 l_database_link                 varchar2(200);
14000 
14001 l_no_of_rows_duplicates         number;
14002 l_no_of_rows_in_list            number;
14003 l_no_of_rows_active             number;
14004 l_no_of_rows_inactive           number;
14005 l_no_of_rows_manually_entered   number;
14006 l_no_of_rows_in_ctrl_group      number;
14007 l_no_of_rows_random             number;
14008 l_no_of_rows_used               number;
14009 l_no_of_rows_suppressed         number;
14010 l_no_of_rows_fatigued           number;
14011 
14012 cursor c_list_det is
14013 select stypes.database_link,
14014        list.remote_gen_flag,
14015        list.list_type
14016   from ams_list_src_types stypes, ams_list_headers_all list
14017  where list.list_source_type = stypes.source_type_code
14018    and list_header_id  =  p_list_header_id;
14019 
14020 cursor c_count_list_entries is
14021 select sum(decode(enabled_flag,'N',0,1)),
14022        sum(decode(enabled_flag,'Y',0,1)),
14023        sum(1),
14024        sum(decode(manually_entered_flag,'Y',decode(enabled_flag,'Y','1',0),0))
14025   from ams_list_entries
14026  where list_header_id = p_list_header_id;
14027 
14028 begin
14029    open c_list_det;
14030    fetch c_list_det into l_database_link,l_remote_flag,l_list_type;
14031    close c_list_det;
14032 
14033    if l_remote_flag = 'N' or l_list_type <> 'TARGET' then
14034       open c_count_list_entries;
14035       fetch c_count_list_entries
14036        into l_no_of_rows_active            ,
14037             l_no_of_rows_inactive          ,
14038             l_no_of_rows_in_list           ,
14039             l_no_of_rows_manually_entered  ;
14040       close c_count_list_entries;
14041    else
14042       execute immediate
14043       'BEGIN
14044          AMS_Remote_ListGen_PKG.remote_list_status_detils'||'@'||l_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11)'||';'||
14045       ' END;'
14046       using  p_list_header_id,
14047              OUT l_no_of_rows_active,
14048              OUT l_no_of_rows_inactive,
14049              OUT l_no_of_rows_in_list,
14050              OUT l_no_of_rows_in_ctrl_group,
14051 	     OUT l_no_of_rows_random,
14052 	     OUT l_no_of_rows_duplicates,
14053 	     OUT l_no_of_rows_manually_entered,
14054              OUT x_msg_count,
14055              OUT x_msg_data,
14056              OUT x_return_status;
14057    end if;
14058 
14059    update ams_list_headers_all
14060       set no_of_rows_in_list          = nvl(l_no_of_rows_in_list,0),
14061           no_of_rows_active           = nvl(l_no_of_rows_active,0),
14062           no_of_rows_inactive         = nvl(l_no_of_rows_inactive,0),
14063           no_of_rows_manually_entered = nvl(l_no_of_rows_manually_entered,0)
14064     where list_header_id = p_list_header_id;
14065     commit;
14066 
14067 exception
14068    when others then
14069       x_return_status := FND_API.G_RET_STS_ERROR;
14070       x_msg_data      := 'Error while executing upd_list_header_info. '||sqlcode||'  '||sqlerrm;
14071       x_msg_count     := 1;
14072 end upd_list_header_info;
14073 
14074 END AMS_ListGeneration_PKG;