DBA Data[Home] [Help]

PACKAGE BODY: APPS.AMS_LISTGENERATION_PKG

Source


1 PACKAGE BODY AMS_LISTGENERATION_PKG as
2 /* $Header: amsvlgnb.pls 120.38.12020000.3 2012/09/28 06:14:23 sagpraka 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 
3878 cursor c_query_temp_type is
3879 select 'Y'
3880 from ams_list_headers_vl  a ,
3881      ams_query_template_all b
3882 where a.list_header_id = g_list_header_id
3883   and b.template_type  = 'PARAMETERIZED'
3884   and a.query_template_id = b.template_id ;
3885 
3886 l_param_string VARCHAR2(32767);
3887 l_const_sql1 varchar2(4000) ;
3888 l_l_sele_action_id      number;
3889 
3890 l_temp_sql            varchar2(32767);
3891 
3892 l_dist_pct_tbl        num_tbl_type;
3893 l_list_select_id      num_tbl_type;
3894 l_incl_object_type    char_tbl_type;
3895 
3896 
3897 
3898 /*  CURSOR c_action_dets1 is
3899   SELECT list_select_action_id
3900     FROM ams_list_select_actions
3901    WHERE action_used_by_id   = p_action_used_by_id
3902      AND arc_action_used_by  = p_action_used_by
3903      AND order_number        = l_order_num; -- added for bug fix 4443619
3904 --     AND order_number        = l_sorted;-- removed for bug fix 4443619*/
3905   CURSOR c_action_dets1 is
3906   SELECT list_select_action_id
3907     FROM ams_list_select_actions
3908    WHERE action_used_by_id   = p_action_used_by_id
3909      AND arc_action_used_by  = p_action_used_by
3910      AND order_number        = l_order_num;
3911 
3912 l_order_number number := 0;
3913 
3914 --Bug 4685389. bmuthukr. to check the total # of parameters
3915 cursor c_check_num_params(p_incl_object_id number) is
3916 select count(1)
3917   from ams_list_queries_param
3918  where list_query_id = p_incl_object_id;
3919 
3920  l_tot_params   number := 0;
3921 --
3922 
3923 BEGIN
3924    write_to_act_log('Executing process_list_actions','LIST',g_list_header_id, 'HIGH');
3925 l_const_sql := ' minus '||
3926                ' select list_entry_source_system_id ' ||
3927                ' from ams_list_entries ' ||
3928                ' where list_header_id  = ' || p_action_used_by_id   ;
3929 
3930 l_const_sql1 := '   and LIST_SELECT_ACTION_ID = ';
3931 
3932   write_to_act_log('Fetching info for the selections','LIST',g_list_header_id, 'LOW');
3933   OPEN c_action_dets;
3934   LOOP
3935 
3936 /******************************************************************************/
3937 /**************** call for cancel list generation added 05/23/2005 ************/
3938 /******************************************************************************/
3939 -- Inside process_list_actions
3940 
3941    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
3942                 p_list_header_id => g_list_header_id ,
3943                 p_remote_gen     => g_remote_list    ,
3944                 p_remote_gen_list=> g_remote_list_gen,
3945                 p_database_link  => g_database_link,
3946                 x_msg_count      => x_msg_count ,
3947                 x_msg_data       => x_msg_data ,
3948                 x_return_status  => x_return_status
3949                );
3950 
3951   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
3952      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
3953         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
3954         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
3955      end if;
3956      RAISE FND_API.G_EXC_ERROR;
3957   ELSE
3958      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
3959   END IF;
3960 
3961 /******************************************************************************/
3962 /**************** call for cancel list generation added 05/23/2005 ************/
3963 /******************************************************************************/
3964 
3965     FETCH c_action_dets INTO l_list_select_action_id;
3966     EXIT WHEN c_action_dets%NOTFOUND;
3967 
3968     -------------------------------------------------------------------------
3969     -- Gets list select actions record details
3970     -- Intialize the record, set the list_select_action_id and get the
3971     -- details
3972     -------------------------------------------------------------------------
3973     ams_listaction_pvt.init_action_rec(l_tmp_action_rec);
3974     l_tmp_action_rec.list_select_action_id := l_list_select_action_id;
3975     ams_listaction_pvt.complete_action_rec
3976                        (p_action_rec   => l_tmp_action_rec,
3977                         x_complete_rec => p_action_rec);
3978     -------------------------------------------------------------------------
3979     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');
3980 
3981    ----------------------------------------------------------------------
3982     --validating that the first executed action has a type of "INCLUDE".--
3983     ----------------------------------------------------------------------
3984     IF (c_action_dets%ROWCOUNT = 1) THEN
3985        IF (p_action_rec.list_action_type <> 'INCLUDE')then
3986            write_to_act_log('Action type of the first selection should always be INCLUDE. Aborting list generation.', 'LIST', g_list_header_id,'HIGH');
3987            FND_MESSAGE.set_name('AMS', 'AMS_LIST_ACT_FIRST_INCLUDE');
3988            FND_MSG_PUB.Add;
3989            RAISE FND_API.G_EXC_ERROR;
3990        END IF;  -- End of if for list_action_type check
3991     END IF; --End of Validation:- First Action Include Check
3992 
3993     write_to_act_log('Calling validate_listaction procedure to validate the list selection attributes','LIST',g_list_header_id, 'LOW');
3994     ams_listaction_pvt.Validate_ListAction
3995     ( p_api_version            => 1.0,
3996       p_init_msg_list          => FND_API.G_FALSE,
3997       p_validation_level       => JTF_PLSQL_API.G_VALID_LEVEL_RECORD,
3998       x_return_status          => x_return_status,
3999       x_msg_count              => x_msg_count,
4000       x_msg_data               => x_msg_data,
4001       p_action_rec             => p_action_rec
4002     );
4003 
4004    IF x_return_status = FND_API.g_ret_sts_error THEN
4005       RAISE FND_API.g_exc_error;
4006    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
4007       RAISE FND_API.g_exc_unexpected_error;
4008    END IF;
4009 
4010    if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4011       write_to_act_log('Error while validating list selections', 'LIST', g_list_header_id,'HIGH');
4012       write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4013    end if;
4014 
4015    if p_action_rec.ARC_INCL_OBJECT_FROM = 'SQL' then
4016       OPEN c_query( p_action_rec.incl_object_id );
4017       FETCH c_query INTO l_PARAMETERIZED_FLAG  ;
4018       close  c_query;
4019    end if;
4020    /********************************************************************
4021       This dynamic procedure will process action for each object type
4022       If the object type is of CELL the process will be procecss_cell
4023       Using the same logic the procedure could be extended for new
4024       action types
4025    *********************************************************************/
4026    -- Bug 4685389. bmuthukr. to check the total # of parameters. If it exceeds 100 abort the process
4027    if nvl(l_parameterized_flag,'N') = 'Y' then
4028 
4029       open c_check_num_params(p_action_rec.incl_object_id);
4030       fetch c_check_num_params into l_tot_params;
4031       close c_check_num_params;
4032 
4033       if nvl(l_tot_params,0) > 100 then
4034          write_to_act_log('Number of parameters used exceeds 100. Aborting list generation process. Please redefine your criteria and restrict it to 100.',
4035   	 'LIST',g_list_header_id,'HIGH');
4036          UPDATE ams_list_headers_all
4037             SET last_generation_success_flag = 'N',
4038                 status_code                  = 'FAILED',
4039                 user_status_id               = 311,
4040                 status_date                  = sysdate,
4041                 last_update_date             = sysdate,
4042                 main_gen_end_time            = sysdate
4043           WHERE list_header_id               = g_list_header_id;
4044          x_return_status := 'E';
4045          logger;
4046 	 commit;
4047          RETURN;
4048       end if;
4049    end if;
4050    --
4051 
4052    -- Employeee list changes..
4053    if p_action_rec.arc_incl_object_from NOT IN ('IMPH','LIST','SQL','DIWB','CELL') then
4054       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.',
4055                        p_arc_log_used_by => 'LIST',
4056                        p_log_used_by_id  => g_list_header_id,
4057 		       p_level => 'HIGH');
4058 
4059         UPDATE ams_list_headers_all
4060         SET    last_generation_success_flag = 'N',
4061                status_code                  = 'FAILED',
4062                user_status_id               = 311,
4063                status_date                  = sysdate,
4064                last_update_date             = sysdate,
4065                main_gen_end_time            = sysdate
4066         WHERE  list_header_id               = g_list_header_id;
4067   -- Added for cancel list gen as it prevents parallel update- Raghu
4068   -- of list headers when cancel button is pressed
4069         commit;
4070 
4071      x_return_status := 'E';
4072      x_msg_count := 1;
4073      x_msg_data := ' Invalid Included Object-- Valid inclusions are imported list, list, custom sql, segment, work book. Aborting list generation process.';
4074      RETURN;
4075    end if;
4076 
4077       write_to_act_log('Calling process_'||p_action_rec.arc_incl_object_from,'LIST',g_list_header_id,'LOW');
4078    write_to_act_log('p_action_rec.list_select_action_id = '||p_action_rec.list_select_action_id, 'LIST', g_list_header_id,'LOW');
4079       execute immediate
4080       'BEGIN
4081         AMS_ListGeneration_PKG.process_'||p_action_rec.arc_incl_object_from ||
4082          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
4083       END;'
4084       using  p_action_used_by_id,
4085              p_action_rec.incl_object_id ,
4086              p_action_rec.list_action_type,
4087              p_action_rec.list_select_action_id,
4088              p_action_rec.order_number,
4089              p_action_rec.rank,
4090              'N',--CHECK p_action_rec.incl_control_group,
4091              OUT x_msg_data,
4092              OUT x_msg_count,
4093              in OUT x_return_status ,
4094              OUT l_std_sql ,
4095              OUT l_include_sql;
4096 
4097     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4098        write_to_act_log('Error when executing process_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'HIGH');
4099        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4100        UPDATE ams_list_headers_all
4101           SET last_generation_success_flag = 'N',
4102               status_code                  = 'FAILED',
4103               user_status_id               = 311,
4104               status_date                  = sysdate,
4105               last_update_date             = sysdate,
4106               main_gen_end_time            = sysdate
4107         WHERE list_header_id               = g_list_header_id;
4108        logger;
4109        commit;
4110        RETURN;
4111     else
4112        write_to_act_log('Process_'||p_action_rec.arc_incl_object_from||' executed successfully.', 'LIST', g_list_header_id,'LOW');
4113     end if;
4114 
4115      if p_action_rec.list_action_type = 'INCLUDE' then
4116         l_include_count := l_include_count + 1 ;
4117         l_include_sql_tbl(l_include_count) := l_include_sql ;
4118         l_std_sql_tbl(l_include_count) := l_std_sql;
4119         --l_rank_tbl(l_include_count) := lpad(p_action_rec.rank,50,'0')
4120                          --|| lpad(p_action_rec.order_number,50,'0');
4121         l_order_num_tbl(l_include_count) := p_action_rec.order_number;-- Raghu Jul 07
4122 	l_rank_num_tbl(l_include_count) := p_action_rec.rank; -- Raghu Jul 07
4123 	l_dist_pct_tbl(l_include_count) := p_action_rec.distribution_pct;
4124         l_list_select_id(l_include_count) := p_action_rec.list_select_action_id;
4125         l_incl_object_type(l_include_count) := p_action_rec.arc_incl_object_from;
4126      else
4127        if p_action_rec.list_action_type = 'EXCLUDE' then
4128           l_join_string := ' minus ';
4129           l_list_header_id := p_action_rec.action_used_by_id;
4130           open c1;
4131           FETCH c1 into l_generation_type;
4132           close c1;
4133        else
4134           l_join_string := ' intersect ';
4135           l_list_header_id := p_action_rec.action_used_by_id;
4136           open c1;
4137           FETCH c1 into l_generation_type;
4138           close c1;
4139        end if;
4140        write_to_act_log('SQL statement for INCLUSION','LIST',g_list_header_id,'LOW');
4141      FOR i IN 1 .. l_include_count
4142      loop
4143         l_std_sql_tbl(i) := l_std_sql_tbl(i)  ||
4144                              l_join_string ||
4145                              l_std_sql;
4146         l_no_of_chunks := ceil(length(l_std_sql_tbl(i))/2000 );
4147         for j in 1..l_no_of_chunks
4148         loop
4149            WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4150         end loop;
4151      end loop;
4152 
4153      end if;
4154 
4155      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
4156        RAISE FND_API.G_EXC_ERROR;
4157      END IF;
4158    write_to_act_log('Action on the list/target group selection completed. ', 'LIST', g_list_header_id,'LOW');
4159 -- end Of Dynamic Procedure
4160    l_join_string :='';
4161 ---------------------------------------------------------------------------
4162   END LOOP;  --  End loop c_action_dets
4163   CLOSE c_action_dets;
4164 
4165    write_to_act_log('No of inclusions in the selection '|| l_include_count, 'LIST', g_list_header_id,'LOW');
4166    write_to_act_log('Sorting based on rank for the selection. '|| l_include_count, 'LIST', g_list_header_id,'LOW');
4167 /*
4168        -- Sorting According to rank
4169        FOR i IN 1 .. l_include_count
4170        loop
4171           l_rank_num_tbl(i) := i;
4172           if i <> 1 then
4173              for j in 1 .. i-1
4174              loop
4175                if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
4176                   for k in reverse j .. i-1
4177                   loop
4178                      l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
4179                   end loop;
4180                   l_rank_num_tbl(j) := i;
4181                   exit;
4182                end if;
4183              end loop;
4184 	  end if;
4185        end loop;
4186 */
4187 
4188 
4189   for  i in 1 .. l_include_count
4190   loop
4191 	l_l_sele_action_id := null;
4192         --l_sorted := l_rank_num_tbl(i);
4193         l_sorted := i;--l_rank_num_tbl(i);
4194         l_order_num := l_order_num_tbl(i);
4195         open c_action_dets1;
4196         fetch c_action_dets1 into l_l_sele_action_id;
4197         close c_action_dets1;
4198         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');
4199 /*
4200 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 ||')';
4201 */
4202        -- l_std_sql_tbl(l_sorted) || l_const_sql || ')';
4203      WRITE_TO_ACT_LOG('Final SQL formed for generating list.', 'LIST', g_list_header_id,'LOW');
4204      if l_final_big_sql is not null then
4205       l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
4206       for i in 1 ..l_no_of_chunks
4207       loop
4208         WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4209       end loop;
4210      end if;
4211 
4212      if l_include_sql_tbl(l_sorted) is not null then
4213       l_no_of_chunks := 0;
4214       l_no_of_chunks  := ceil(length(l_include_sql_tbl(l_sorted))/2000 );
4215       for i in 1 ..l_no_of_chunks
4216        loop
4217         WRITE_TO_ACT_LOG(substrb(l_include_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4218        end loop;
4219      end if;
4220     if l_std_sql_tbl(l_sorted) is not null then
4221       l_no_of_chunks := 0;
4222       l_no_of_chunks  := ceil(length(l_std_sql_tbl(l_sorted))/2000 );
4223       for i in 1 ..l_no_of_chunks
4224        loop
4225         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4226        end loop;
4227     end if;
4228     if l_const_sql is not null then
4229      l_no_of_chunks := 0;
4230      l_no_of_chunks  := ceil(length(l_const_sql)/2000 );
4231      for i in 1 ..l_no_of_chunks
4232      loop
4233         WRITE_TO_ACT_LOG(substrb(l_const_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4234      end loop;
4235     end if;
4236 
4237 	  write_to_act_log(' '||l_const_sql1||to_char(l_l_sele_action_id), 'LIST', g_list_header_id,'LOW');
4238       if l_PARAMETERIZED_FLAG  = 'N' then
4239          write_to_act_log('No parameters required for generating this list', 'LIST', g_list_header_id,'LOW');
4240          if nvl(l_dist_pct_tbl(l_sorted),100) <> 100 then
4241 	    write_to_act_log('Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
4242 	    write_to_act_log('% Requested for this selection is '||l_dist_pct_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
4243    	    if l_incl_object_type(l_sorted) in ('SQL','DIWB','CELL') then
4244 	       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');
4245 	       l_temp_sql := l_include_sql_tbl(l_sorted);
4246 	       l_temp_sql := 'SELECT count(1) '||substr(l_temp_sql,instr(l_temp_sql, ' FROM '));
4247                -- Modified for bug 5238900. bmuthukr
4248 	       -- 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||')' );
4249                get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||')' );
4250             elsif l_incl_object_type(l_sorted) = 'LIST' then
4251                get_count(l_list_select_id(l_sorted),l_sorted,'LIST',null);
4252             elsif l_incl_object_type(l_sorted) = 'IMPH' then
4253                get_count(l_list_select_id(l_sorted),l_sorted,'IMPH',null);
4254             end if;
4255             write_to_act_log('No of rows requested from the selection is '||g_reqd_num_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
4256          else
4257             g_act_num_tbl(l_sorted)  := -1;
4258             g_reqd_num_tbl(l_sorted) := -1;
4259          end if;
4260          if g_remote_list_gen = 'N' then
4261 		/* If the list is not based on the remote data source and if it's based on remote data source
4262                    but needs to be generated in the local instance means it's migrated to the local instance */
4263            --l_const_sql1 := ' ';
4264            --l_l_sele_action_id := null;
4265 
4266 	   if g_reqd_num_tbl(l_sorted) <> -1 then
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 ||')'||' and rownum <= '||g_reqd_num_tbl(l_sorted);
4268            else
4269               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 ||')';
4270            end if;
4271            write_to_act_log('List generated in local instance', 'LIST', g_list_header_id,'HIGH');
4272 	  else
4273 		/* If the list is based on the remote data source and it's not migrated to the local instance or
4274                    a segment , sql or workbook is in the list selection then it will be generated in the remote
4275                    instance through a dynamic procedure call */
4276                write_to_act_log('Calling remote procedure to generate list in remote instance', 'LIST', g_list_header_id,'LOW');
4277            --l_const_sql1 := ' ';
4278           -- l_l_sele_action_id := null;
4279 
4280 	   if g_reqd_num_tbl(l_sorted) <> -1 then
4281 	      write_to_act_log(' % Requested at selection level for this selection ','LIST',g_list_header_id,'LOW');
4282 	       execute immediate
4283       	      'BEGIN
4284                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
4285               ' END;'
4286               using  '1',
4287               l_null,
4288               'T',
4289               l_null,
4290               OUT x_return_status,
4291               OUT x_msg_count,
4292               OUT x_msg_data,
4293               g_list_header_id,
4294               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),
4295 	      --   l_final_big_sql,
4296               l_null,
4297              OUT l_total_recs,
4298              'LISTGEN';
4299           else
4300 	     write_to_act_log(' % Not Requested at selection level for this selection ','LIST',g_list_header_id,'LOW');
4301              execute immediate
4302       	      'BEGIN
4303                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
4304               ' END;'
4305               using  '1',
4306               l_null,
4307               'T',
4308               l_null,
4309               OUT x_return_status,
4310               OUT x_msg_count,
4311               OUT x_msg_data,
4312               g_list_header_id,
4313               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')',
4314 	   --   l_final_big_sql,
4315               l_null,
4316              OUT l_total_recs,
4317              'LISTGEN';
4318           end if;
4319            if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4320               write_to_act_log('Error while executing remote procedure for generating list', 'LIST', g_list_header_id,'HIGH');
4321               write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4322               --Added for bug 4577528 by bmuthukr.
4323               update ams_list_headers_all
4324                  set last_generation_success_flag = 'N',
4325                      status_code                  = 'FAILED',
4326                      user_status_id               = 311,
4327                      status_date                  = sysdate,
4328                      last_update_date             = sysdate,
4329                      main_gen_end_time            = sysdate
4330                where list_header_id               = g_list_header_id;
4331  	      update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
4332               write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
4333 	      x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
4334 	      return;
4335 	      --
4336            else
4337               write_to_act_log('List generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
4338            end if;
4339 	end if;
4340       else  -- FOR l_PARAMETERIZED_FLAG = 'Y'
4341 --       if g_remote_list_gen = 'N' then
4342         write_to_act_log('Fetching parameters required for generating this list', 'LIST', g_list_header_id,'LOW');
4343         for i in 1 .. 100 loop
4344            l_table_char(i) := ' ';
4345         end loop;
4346         for i in c_param_values(p_action_rec.incl_object_id )
4347         loop
4348            l_table_char(i.PARAMETER_ORDER) := trim(i.PARAMETER_VALUE);
4349 	   write_to_act_log('Parameter - '||i.parameter_order||' is '||'*'||l_table_char(i.PARAMETER_ORDER)||'*','LIST', g_list_header_id,'LOW');
4350         end loop;
4351        -- l_const_sql1 := ' ';
4352        -- l_l_sele_action_id := null;
4353 	-- l_final_big_sql := ' insert into t11 values (99,:PARTY_TYPE)';
4354         l_string := 'DECLARE   ' ||
4355         'l_string1 varchar2(10000) ; ' ||
4356         'begin    ' ||
4357         ' l_string1 :=   :1  || ' || ' :2  || ' || ' :3  || ' || ' :4  || ' ||
4358                        ' :5  || ' || ' :6  || ' || ' :7  || ' || ' :8  || ' ||
4359                        ' :9  || ' || ' :10  || ' || ' :11  || ' || ' :12  || ' ||
4360                        ' :13  || ' || ' :14  || ' || ' :15  || ' || ' :16  || ' ||
4361                        ' :17  || ' || ' :18  || ' || ' :19  || ' || ' :20  || ' ||
4362                        ' :21  || ' || ' :22  || ' || ' :23  || ' || ' :24  || ' ||
4363                        ' :25  || ' || ' :26  || ' || ' :27  || ' || ' :28  || ' ||
4364                        ' :29  || ' || ' :30  || ' || ' :31  || ' || ' :32  || ' ||
4365                        ' :33  || ' || ' :34  || ' || ' :35  || ' || ' :36  || ' ||
4366                        ' :37  || ' || ' :38  || ' || ' :39  || ' || ' :40  || ' ||
4367                        ' :41  || ' || ' :42  || ' || ' :43  || ' || ' :44  || ' ||
4368                        ' :45  || ' || ' :46  || ' || ' :47  || ' || ' :48  || ' ||
4369                        ' :49  || ' || ' :50  || ' || ' :51  || ' || ' :52  || ' ||
4370                        ' :53  || ' || ' :54  || ' || ' :55  || ' || ' :56  || ' ||
4371                        ' :57  || ' || ' :58  || ' || ' :59  || ' || ' :60  || ' ||
4372                        ' :61  || ' || ' :62  || ' || ' :63  || ' || ' :64  || ' ||
4373                        ' :65  || ' || ' :66  || ' || ' :67  || ' || ' :68  || ' ||
4374                        ' :69  || ' || ' :70  || ' || ' :71  || ' || ' :72  || ' ||
4375                        ' :73  || ' || ' :74  || ' || ' :75  || ' || ' :76  || ' ||
4376                        ' :77  || ' || ' :78  || ' || ' :79  || ' || ' :80  || ' ||
4377                        ' :81  || ' || ' :82  || ' || ' :83  || ' || ' :84  || ' ||
4378                        ' :85  || ' || ' :86  || ' || ' :87  || ' || ' :88  || ' ||
4379                        ' :89  || ' || ' :90  || ' || ' :91  || ' || ' :92  || ' ||
4380                        ' :93  || ' || ' :94  || ' || ' :95  || ' || ' :96  || ' ||
4381                        ' :97  || ' || ' :98  || ' || ' :99  || ' || ' :100  ; ' ||' '||
4382  --    l_final_big_sql ||
4383       l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')'||
4384        ';  end;  '  ;
4385 /* Changed to fix :1 and :name issue */
4386      open c_query_temp_type ;
4387         fetch  c_query_temp_type into l_query_templ_flag   ;
4388            --EXIT WHEN c_query_temp_type%NOTFOUND;
4389            if l_query_templ_flag  = 'Y' then
4390               l_no_of_chunks := 0;
4391               for i in c_param_values(p_action_rec.incl_object_id )
4392               loop
4393                  l_no_of_chunks := l_no_of_chunks + 1;
4394                  l_string := replace(l_string,':' || l_no_of_chunks  || ' ' , ':'|| i.parameter_name||' ' );
4395               end loop;
4396            end if;
4397            l_no_of_chunks := 0;
4398      close c_query_temp_type ;
4399  /*END Changed to fix :1 and :name issue */
4400      WRITE_TO_ACT_LOG('SQL to be executed to generate list', 'LIST', g_list_header_id,'LOW');
4401      WRITE_TO_ACT_LOG('Length of the sql  '||length(l_string), 'LIST', g_list_header_id,'LOW');
4402      l_no_of_chunks  := ceil(length(l_string)/2000 );
4403      for i in 1 ..l_no_of_chunks
4404      loop
4405         WRITE_TO_ACT_LOG(substrb(l_string,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
4406      end loop;
4407 if g_remote_list_gen = 'N' then
4408 
4409      WRITE_TO_ACT_LOG('Generating list with parameters ','LIST', g_list_header_id,'LOW');
4410 execute immediate   l_string
4411 using l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
4412       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
4413       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
4414       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
4415       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
4416       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
4417       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
4418       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
4419       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
4420       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
4421       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
4422       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
4423       l_table_char(49), l_table_char(50),
4424       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
4425       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
4426       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
4427       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
4428       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
4429       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
4430       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
4431       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
4432       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
4433       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
4434       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
4435       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
4436       l_table_char(99), l_table_char(100);
4437       open c_count1;
4438       fetch c_count1 into l_count1;
4439       close c_count1;
4440       write_to_act_log('Number of entries in list is '|| l_count1, 'LIST', g_list_header_id,'LOW');
4441  end if;
4442 
4443  if g_remote_list_gen = 'Y' then
4444           -- l_const_sql1 := ' ';
4445           -- l_l_sele_action_id := null;
4446 
4447         write_to_act_log('Calling remote procedure with parameters to generate list in remote instance ', 'LIST', g_list_header_id,'LOW');
4448       	       execute immediate
4449       	      'BEGIN
4450 	AMS_Remote_ListGen_PKG.remote_param_list_gen'||'@'||g_database_link||'( :1  , :2  , :3  , :4  , :5  , :6  , :7  , :8  , :9  , :10  , :11  , :12  ,
4451        :13   , :14   , :15  , :16   , :17   , :18   , :19   , :20   ,
4452        :21   , :22   , :23  , :24   , :25   , :26   , :27   , :28   , :29   , :30   ,
4453        :31   , :32   , :33  , :34   , :35   , :36   , :37   , :38   , :39   , :40   ,
4454        :41   , :42   , :43  , :44   , :45   , :46   , :47   , :48   , :49   , :50   ,
4455        :51   , :52   , :53  , :54   , :55   , :56   , :57   , :58   , :59   , :60   ,
4456        :61   , :62   , :63  , :64   , :65   , :66   , :67   , :68   , :69   , :70   ,
4457        :71   , :72   , :73  , :74   , :75   , :76   , :77   , :78   , :79   , :80   ,
4458        :81   , :82   , :83  , :84   , :85   , :86   , :87   , :88   , :89   , :90   ,
4459        :91   , :92   , :93  , :94   , :95   , :96   , :97   , :98   , :99   , :100  ,
4460        :101   , :102   , :103   , :104   , :105   , :106   , :107   , :108   , :109   , :110   , :111   , :112
4461            )'||';'||
4462               ' END;'
4463               using  '1',
4464               l_null,
4465               'T',
4466               l_null,
4467               OUT x_return_status,
4468               OUT x_msg_count,
4469               OUT x_msg_data,
4470               g_list_header_id,
4471 	      l_string,
4472               l_null,
4473              OUT l_total_recs,
4474              'PARAMLISTGEN',
4475       l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
4476       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
4477       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
4478       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
4479       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
4480       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
4481       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
4482       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
4483       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
4484       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
4485       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
4486       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
4487       l_table_char(49), l_table_char(50),
4488       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
4489       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
4490       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
4491       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
4492       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
4493       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
4494       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
4495       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
4496       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
4497       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
4498       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
4499       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
4500       l_table_char(99), l_table_char(100);
4501       if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
4502          write_to_act_log('Error while generating  list in remote instance.', 'LIST', g_list_header_id,'HIGH');
4503          write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
4504          --Added for bug 4577528 by bmuthukr.
4505          update ams_list_headers_all
4506             set last_generation_success_flag = 'N',
4507                 status_code                  = 'FAILED',
4508                 user_status_id               = 311,
4509                 status_date                  = sysdate,
4510                 last_update_date             = sysdate,
4511                 main_gen_end_time            = sysdate
4512           where list_header_id               = g_list_header_id;
4513          update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
4514          write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
4515 	 x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
4516 	 return;
4517 	 --
4518       else
4519          write_to_act_log('List generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
4520       end if;
4521     end if;
4522    end if;
4523   end loop;
4524 
4525   open c_count1;
4526       fetch c_count1 into l_count1;
4527       close c_count1;
4528       WRITE_TO_ACT_LOG('Total Number of entries in list is '|| l_count1, 'LIST', g_list_header_id,'LOW');
4529   WRITE_TO_ACT_LOG('Execution of Process_list_action completed', 'LIST', g_list_header_id,'LOW');
4530 EXCEPTION
4531    WHEN FND_API.G_EXC_ERROR THEN
4532      IF(c_action_dets%ISOPEN)THEN
4533         CLOSE c_action_dets;
4534      END IF;
4535      write_to_act_log('Error while executing process_list_actions '
4536                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4537      -- Check if reset of the status is required
4538      x_return_status := FND_API.G_RET_STS_ERROR ;
4539       FND_MSG_PUB.count_and_get(
4540             p_encoded => FND_API.g_false,
4541             p_count   => x_msg_count,
4542             p_data    => x_msg_data);
4543 
4544   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4545      IF(c_action_dets%ISOPEN)THEN
4546         CLOSE c_action_dets;
4547      END IF;
4548      write_to_act_log('Error while executing process_list_actions '
4549                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4550      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
4551      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
4552      FND_MSG_PUB.Add;
4553      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4554       FND_MSG_PUB.count_and_get(
4555             p_encoded => FND_API.g_false,
4556             p_count   => x_msg_count,
4557             p_data    => x_msg_data);
4558 
4559   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
4560      IF(c_action_dets%ISOPEN)THEN
4561         CLOSE c_action_dets;
4562      END IF;
4563 
4564      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4565 
4566      write_to_act_log('executing process_list_actions - user action to cancel list generation detected ' ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4567      -- Got to raise the exception again because Listgen has to end generation.
4568      raise AMS_LISTGENERATION_UTIL_PKG.cancelListGen;
4569 
4570   WHEN OTHERS THEN
4571      IF(c_action_dets%ISOPEN)THEN
4572         CLOSE c_action_dets;
4573      END IF;
4574      write_to_act_log('Error while executing process_list_actions '
4575                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
4576      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
4577      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
4578      FND_MSG_PUB.Add;
4579      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4580       FND_MSG_PUB.count_and_get(
4581             p_encoded => FND_API.g_false,
4582             p_count   => x_msg_count,
4583             p_data    => x_msg_data);
4584 
4585 END process_list_actions;
4586 
4587 -- START OF COMMENTS
4588 --
4589 -- NAME
4590 --   CREATE_LIST_ENTRIES
4591 --
4592 -- PURPOSE
4593 
4594 --    1. Populate The AMS_LIST_ENTRIES table with a DISTINCT set of
4595 --       Entries from The AMS_LIST_TMP_ENTRIES table.
4596 --    2. An Entry is considered UNIQUE by values in The list_entry_source_id
4597 --       and List_Entry_Source_Type fields from The AMS_LIST_TMP_ENTRIES table.
4598 --    3. There may be more than one Entry in The AMS_LIST_TMP_ENTRIES table
4599 --       with The same values for The list_entry_source_id and
4600 --       List_Entry_Source_Type fields, in this case The Entry with
4601 --       the Highest Rank is choosen, if Two Equivalent entries have
4602 --       the same rank then one entry is choosen arbitrarily.
4603 --    4. The set of distinct entries in The AMS_LIST_TMP_ENTRIES table
4604 --       have the SAVE_ROW_FLAG updated to 'Y'.
4605 --       Rows with this value are then inserted into the AMS_LIST_ENTRIES table.
4606 --    5. if less entries exist in the AMS_LIST_ENTRIES_TABLE than specifed
4607 --       in the AMS_LIST_HEADERS_ALL
4608 --       .NO_OF_ROWS_MIN_REQUESTED column then an error is reported.
4609 
4610 
4611 -- CALLED BY.
4612 --    1. GENERATE_LIST.
4613 --  01/24/2001 GJOBY      Modified for hornet
4614 --
4615 -- HISTORY
4616 -- END of Comments
4617 
4618 
4619 procedure create_list_entries (p_list_header_id in number,
4620                                p_no_of_rows_min_requested  in number,
4621                                x_return_status OUT NOCOPY  VARCHAR2 ) IS
4622 
4623   l_entry_count              NUMBER;
4624   l_return_status            VARCHAR2(100) := FND_API.G_FALSE;
4625   l_source_code              VARCHAR2(100) := 'NONE';
4626   l_source_id                NUMBER        := 0;
4627 
4628   l_created_by                NUMBER;
4629 
4630    CURSOR cur_get_created_by (x_list_header_id IN NUMBER) IS
4631       SELECT created_by
4632       FROM ams_list_headers_all
4633       WHERE list_header_id= x_list_header_id;
4634 
4635 BEGIN
4636    write_to_act_log('Executing procedure create_list_entries', 'LIST', g_list_header_id,'LOW');
4637    delete from ams_list_tmp_entries alte1
4638    where alte1.rowid > (select min(alte2.rowid)
4639                         from  ams_list_tmp_entries alte2
4640                         where alte2.list_header_id = alte1.list_header_id
4641                           and alte2.list_select_action_id
4642                                     = alte1.list_select_action_id
4643                           and alte2.list_entry_source_key
4644                                     =  alte1.list_entry_source_key  );
4645 
4646    delete from ams_list_tmp_entries alte1
4647    where alte1.rank > (select min(alte2.rank)
4648                         from  ams_list_tmp_entries alte2
4649                         where alte2.list_header_id = alte1.list_header_id
4650                           and alte2.list_entry_source_key
4651                                     =  alte1.list_entry_source_key  );
4652 
4653       l_created_by := 0;
4654 
4655        OPEN cur_get_created_by(p_list_header_id);
4656 
4657        FETCH cur_get_created_by INTO l_created_by;
4658        CLOSE cur_get_created_by;
4659 
4660 
4661    INSERT INTO ams_List_Entries
4662    ( list_entry_id                   ,
4663      last_update_date                ,
4664      last_updated_by                 ,
4665      creation_date                   ,
4666      created_by                      ,
4667      last_update_login               ,
4668      list_header_id                  ,
4669      list_select_action_id           ,
4670      arc_list_select_action_from     ,
4671      list_select_action_from_name    ,
4672      source_code                     ,
4673      source_code_for_id              ,
4674      arc_list_used_by_source         ,
4675      pin_code                        ,
4676      list_entry_source_system_id     ,
4677      list_entry_source_system_type   ,
4678      view_application_id             ,
4679      manually_entered_flag           ,
4680      marked_as_random_flag           ,
4681      marked_as_duplicate_flag        ,
4682      part_of_control_group_flag      ,
4683      exclude_in_triggered_list_flag  ,
4684      enabled_flag,
4685      object_version_number
4686    )
4687    ( select ams_list_entries_s.nextval,
4688             sysdate,
4689             fnd_global.user_id,
4690             sysdate,
4691             nvl(l_created_by, fnd_global.user_id),
4692             fnd_global.conc_login_id,
4693             p_list_header_id,
4694             s.list_select_action_id,
4695             s.arc_incl_object_from,
4696             s.incl_object_name,
4697             'NONE',
4698             0,
4699             h.list_used_by_id,
4700             ams_list_entries_s.currval,
4701             t.list_entry_source_id,
4702             t.list_entry_source_type,
4703             fnd_global.conc_login_id,
4704             'N',
4705             'N',
4706             'N',
4707             'N',
4708             'N',
4709             'Y',
4710              1
4711      from   ams_list_select_actions s,
4712             ams_list_tmp_entries    t,
4713             ams_list_headers_all    h
4714      where  h.list_header_id        = p_list_header_id
4715      and    t.list_header_id        = h.list_header_id
4716      and    t.list_select_action_id = s.list_select_action_id
4717      );
4718 
4719      select count(*)
4720      into   l_entry_count
4721      from   ams_list_entries
4722      where  list_header_id = p_list_header_id;
4723 
4724      update ams_list_headers_all
4725      set no_of_rows_in_list = l_entry_count,
4726          last_update_date  = sysdate
4727      where  list_header_id = p_list_header_id;
4728 
4729    write_to_act_log('Procedure create_list_entries executed', 'LIST', g_list_header_id,'LOW');
4730   -- Added for cancel list gen as it prevents parallel update- Raghu
4731   -- of list headers when cancel button is pressed
4732   commit;
4733 
4734 -- Start changes for migration
4735 /*
4736     if(l_entry_count < p_no_of_rows_min_requested)then
4737           FND_MESSAGE.set_name('AMS', 'AMS_LIST_MINIMUM_NOT_REACHED');
4738           FND_MESSAGE.Set_Token('NO_AVAILABLE',TO_CHAR(l_entry_count));
4739           FND_MESSAGE.Set_Token('NO_REQUESTED',TO_CHAR(p_no_of_rows_min_requested));
4740           FND_MSG_PUB.Add;
4741           x_return_status := FND_API.G_RET_STS_ERROR;
4742      ELSE
4743           x_return_status := FND_API.G_RET_STS_SUCCESS;
4744      END if;
4745 */
4746      x_return_status := FND_API.G_RET_STS_SUCCESS;
4747 -- End changes for migration
4748   exception
4749    when others then
4750    write_to_act_log('Error while executing procedure create_list_entries', 'LIST', g_list_header_id,'HIGH');
4751    x_return_status := FND_API.G_RET_STS_ERROR;
4752 
4753 end create_list_entries;
4754 
4755 
4756 procedure update_list_entries(p_list_header_id in number) is
4757 l_tg_check	varchar2(1) := null;
4758 cursor c_check_tg is
4759 select 'Y' from ams_act_lists where list_header_id = p_list_header_id;
4760 
4761 begin
4762 open  c_check_tg;
4763 fetch c_check_tg into l_tg_check;
4764 close c_check_tg;
4765 
4766 if l_tg_check = 'Y' then
4767    write_to_act_log('Generating target group(which has selections based on LIST) in update mode.','LIST',g_list_header_id,'LOW');
4768    update ams_list_entries  ale
4769    set (
4770        ale.newly_updated_flag ,
4771        ale.enabled_flag ,
4772        ale.SUFFIX,
4773        ale.FIRST_NAME,
4774        ale.LAST_NAME,
4775        ale.CUSTOMER_NAME,
4776        ale.TITLE,
4777        ale.ADDRESS_LINE1,
4778        ale.ADDRESS_LINE2,
4779        ale.CITY,
4780        ale.STATE,
4781        ale.ZIPCODE,
4782        ale.COUNTRY,
4783        ale.FAX,
4784        ale.PHONE,
4785        ale.EMAIL_ADDRESS,
4786        ale.COL1,
4787        ale.COL2,
4788        ale.COL3,
4789        ale.COL4,
4790        ale.COL5,
4791        ale.COL6,
4792        ale.COL7,
4793        ale.COL8,
4794        ale.COL9,
4795        ale.COL10,
4796        ale.COL11,
4797        ale.COL12,
4798        ale.COL13,
4799        ale.COL14,
4800        ale.COL15,
4801        ale.COL16,
4802        ale.COL17,
4803        ale.COL18,
4804        ale.COL19,
4805        ale.COL20,
4806        ale.COL21,
4807        ale.COL22,
4808        ale.COL23,
4809        ale.COL24,
4810        ale.COL25,
4811        ale.COL26,
4812        ale.COL27,
4813        ale.COL28,
4814        ale.COL29,
4815        ale.COL30,
4816        ale.COL31,
4817        ale.COL32,
4818        ale.COL33,
4819        ale.COL34,
4820        ale.COL35,
4821        ale.COL36,
4822        ale.COL37,
4823        ale.COL38,
4824        ale.COL39,
4825        ale.COL40,
4826        ale.COL41,
4827        ale.COL42,
4828        ale.COL43,
4829        ale.COL44,
4830        ale.COL45,
4831        ale.COL46,
4832        ale.COL47,
4833        ale.COL48,
4834        ale.COL49,
4835        ale.COL50,
4836        ale.COL51,
4837        ale.COL52,
4838        ale.COL53,
4839        ale.COL54,
4840        ale.COL55,
4841        ale.COL56,
4842        ale.COL57,
4843        ale.COL58,
4844        ale.COL59,
4845        ale.COL60,
4846        ale.COL61,
4847        ale.COL62,
4848        ale.COL63,
4849        ale.COL64,
4850        ale.COL65,
4851        ale.COL66,
4852        ale.COL67,
4853        ale.COL68,
4854        ale.COL69,
4855        ale.COL70,
4856        ale.COL71,
4857        ale.COL72,
4858        ale.COL73,
4859        ale.COL74,
4860        ale.COL75,
4861        ale.COL76,
4862        ale.COL77,
4863        ale.COL78,
4864        ale.COL79,
4865        ale.COL80,
4866        ale.COL81,
4867        ale.COL82,
4868        ale.COL83,
4869        ale.COL84,
4870        ale.COL85,
4871        ale.COL86,
4872        ale.COL87,
4873        ale.COL88,
4874        ale.COL89,
4875        ale.COL90,
4876        ale.COL91,
4877        ale.COL92,
4878        ale.COL93,
4879        ale.COL94,
4880        ale.COL95,
4881        ale.COL96,
4882        ale.COL97,
4883        ale.COL98,
4884        ale.COL99,
4885        ale.COL100,
4886        ale.COL101,
4887        ale.COL102,
4888        ale.COL103,
4889        ale.COL104,
4890        ale.COL105,
4891        ale.COL106,
4892        ale.COL107,
4893        ale.COL108,
4894        ale.COL109,
4895        ale.COL110,
4896        ale.COL111,
4897        ale.COL112,
4898        ale.COL113,
4899        ale.COL114,
4900        ale.COL115,
4901        ale.COL116,
4902        ale.COL117,
4903        ale.COL118,
4904        ale.COL119,
4905        ale.COL120,
4906        ale.COL121,
4907        ale.COL122,
4908        ale.COL123,
4909        ale.COL124,
4910        ale.COL125,
4911        ale.COL126,
4912        ale.COL127,
4913        ale.COL128,
4914        ale.COL129,
4915        ale.COL130,
4916        ale.COL131,
4917        ale.COL132,
4918        ale.COL133,
4919        ale.COL134,
4920        ale.COL135,
4921        ale.COL136,
4922        ale.COL137,
4923        ale.COL138,
4924        ale.COL139,
4925        ale.COL140,
4926        ale.COL141,
4927        ale.COL142,
4928        ale.COL143,
4929        ale.COL144,
4930        ale.COL145,
4931        ale.COL146,
4932        ale.COL147,
4933        ale.COL148,
4934        ale.COL149,
4935        ale.COL150,
4936        ale.COL151,
4937        ale.COL152,
4938        ale.COL153,
4939        ale.COL154,
4940        ale.COL155,
4941        ale.COL156,
4942        ale.COL157,
4943        ale.COL158,
4944        ale.COL159,
4945        ale.COL160,
4946        ale.COL161,
4947        ale.COL162,
4948        ale.COL163,
4949        ale.COL164,
4950        ale.COL165,
4951        ale.COL166,
4952        ale.COL167,
4953        ale.COL168,
4954        ale.COL169,
4955        ale.COL170,
4956        ale.COL171,
4957        ale.COL172,
4958        ale.COL173,
4959        ale.COL174,
4960        ale.COL175,
4961        ale.COL176,
4962        ale.COL177,
4963        ale.COL178,
4964        ale.COL179,
4965        ale.COL180,
4966        ale.COL181,
4967        ale.COL182,
4968        ale.COL183,
4969        ale.COL184,
4970        ale.COL185,
4971        ale.COL186,
4972        ale.COL187,
4973        ale.COL188,
4974        ale.COL189,
4975        ale.COL190,
4976        ale.COL191,
4977        ale.COL192,
4978        ale.COL193,
4979        ale.COL194,
4980        ale.COL195,
4981        ale.COL196,
4982        ale.COL197,
4983        ale.COL198,
4984        ale.COL199,
4985        ale.COL200,
4986        ale.COL201,
4987        ale.COL202,
4988        ale.COL203,
4989        ale.COL204,
4990        ale.COL205,
4991        ale.COL206,
4992        ale.COL207,
4993        ale.COL208,
4994        ale.COL209,
4995        ale.COL210,
4996        ale.COL211,
4997        ale.COL212,
4998        ale.COL213,
4999        ale.COL214,
5000        ale.COL215,
5001        ale.COL216,
5002        ale.COL217,
5003        ale.COL218,
5004        ale.COL219,
5005        ale.COL220,
5006        ale.COL221,
5007        ale.COL222,
5008        ale.COL223,
5009        ale.COL224,
5010        ale.COL225,
5011        ale.COL226,
5012        ale.COL227,
5013        ale.COL228,
5014        ale.COL229,
5015        ale.COL230,
5016        ale.COL231,
5017        ale.COL232,
5018        ale.COL233,
5019        ale.COL234,
5020        ale.COL235,
5021        ale.COL236,
5022        ale.COL237,
5023        ale.COL238,
5024        ale.COL239,
5025        ale.COL240,
5026        ale.COL241,
5027        ale.COL242,
5028        ale.COL243,
5029        ale.COL244,
5030        ale.COL245,
5031        ale.COL246,
5032        ale.COL247,
5033        ale.COL248,
5034        ale.COL249,
5035        ale.COL250 ,
5036        ale.COL251     ,
5037        ale.COL252     ,
5038        ale.COL253     ,
5039        ale.COL254     ,
5040        ale.COL256     ,
5041        ale.COL255     ,
5042        ale.COL257     ,
5043        ale.COL258     ,
5044        ale.COL259     ,
5045        ale.COL260     ,
5046        ale.COL261     ,
5047        ale.COL262     ,
5048        ale.COL263     ,
5049        ale.COL264     ,
5050        ale.COL265     ,
5051        ale.COL266     ,
5052        ale.COL267     ,
5053        ale.COL268     ,
5054        ale.COL269     ,
5055        ale.COL270     ,
5056        ale.COL271     ,
5057        ale.COL272     ,
5058        ale.COL273     ,
5059        ale.COL274     ,
5060        ale.COL275     ,
5061        ale.COL276     ,
5062        ale.COL277     ,
5063        ale.COL278     ,
5064        ale.COL279     ,
5065        ale.COL280     ,
5066        ale.COL281     ,
5067        ale.COL282     ,
5068        ale.COL283     ,
5069        ale.COL284     ,
5070        ale.COL285     ,
5071        ale.COL286     ,
5072        ale.COL287     ,
5073        ale.COL288     ,
5074        ale.COL289     ,
5075        ale.COL290     ,
5076        ale.COL291     ,
5077        ale.COL292     ,
5078        ale.COL293     ,
5079        ale.COL294     ,
5080        ale.COL295     ,
5081        ale.COL296     ,
5082        ale.COL297     ,
5083        ale.COL298     ,
5084        ale.COL299     ,
5085        ale.COL300     ,
5086        ale.SALES_AGENT_EMAIL_ADDRESS,
5087        ale.RESOURCE_ID,
5088        ale.location_id,
5089        ale.contact_point_id,
5090        ale.orig_system_reference,
5091        ale.CUSTOM_COLUMN1,
5092        ale.CUSTOM_COLUMN2,
5093        ale.CUSTOM_COLUMN3,
5094        ale.CUSTOM_COLUMN4,
5095        ale.CUSTOM_COLUMN5,
5096        ale.CUSTOM_COLUMN6,
5097        ale.CUSTOM_COLUMN7,
5098        ale.CUSTOM_COLUMN8,
5099        ale.CUSTOM_COLUMN9,
5100        ale.CUSTOM_COLUMN10,
5101        ale.CUSTOM_COLUMN11,
5102        ale.CUSTOM_COLUMN12,
5103        ale.CUSTOM_COLUMN13,
5104        ale.CUSTOM_COLUMN14,
5105        ale.CUSTOM_COLUMN15,
5106        ale.CUSTOM_COLUMN16,
5107        ale.CUSTOM_COLUMN17,
5108        ale.CUSTOM_COLUMN18,
5109        ale.CUSTOM_COLUMN19,
5110        ale.CUSTOM_COLUMN20,
5111        ale.CUSTOM_COLUMN21,
5112        ale.CUSTOM_COLUMN22,
5113        ale.CUSTOM_COLUMN23,
5114        ale.CUSTOM_COLUMN24,
5115        ale.CUSTOM_COLUMN25
5116        ) =
5117         (select
5118         'Y',
5119         'Y',
5120        ail.SUFFIX,
5121        ail.FIRST_NAME,
5122        ail.LAST_NAME,
5123        ail.CUSTOMER_NAME,
5124        ail.TITLE,
5125        ail.ADDRESS_LINE1,
5126        ail.ADDRESS_LINE2,
5127        ail.CITY,
5128        ail.STATE,
5129        ail.ZIPCODE,
5130        ail.COUNTRY,
5131        ail.FAX,
5132        ail.PHONE,
5133        ail.EMAIL_ADDRESS,
5134        ail.COL1,
5135        ail.COL2,
5136        ail.COL3,
5137        ail.COL4,
5138        ail.COL5,
5139        ail.COL6,
5140        ail.COL7,
5141        ail.COL8,
5142        ail.COL9,
5143        ail.COL10,
5144        ail.COL11,
5145        ail.COL12,
5146        ail.COL13,
5147        ail.COL14,
5148        ail.COL15,
5149        ail.COL16,
5150        ail.COL17,
5151        ail.COL18,
5152        ail.COL19,
5153        ail.COL20,
5154        ail.COL21,
5155        ail.COL22,
5156        ail.COL23,
5157        ail.COL24,
5158        ail.COL25,
5159        ail.COL26,
5160        ail.COL27,
5161        ail.COL28,
5162        ail.COL29,
5163        ail.COL30,
5164        ail.COL31,
5165        ail.COL32,
5166        ail.COL33,
5167        ail.COL34,
5168        ail.COL35,
5169        ail.COL36,
5170        ail.COL37,
5171        ail.COL38,
5172        ail.COL39,
5173        ail.COL40,
5174        ail.COL41,
5175        ail.COL42,
5176        ail.COL43,
5177        ail.COL44,
5178        ail.COL45,
5179        ail.COL46,
5180        ail.COL47,
5181        ail.COL48,
5182        ail.COL49,
5183        ail.COL50,
5184        ail.COL51,
5185        ail.COL52,
5186        ail.COL53,
5187        ail.COL54,
5188        ail.COL55,
5189        ail.COL56,
5190        ail.COL57,
5191        ail.COL58,
5192        ail.COL59,
5193        ail.COL60,
5194        ail.COL61,
5195        ail.COL62,
5196        ail.COL63,
5197        ail.COL64,
5198        ail.COL65,
5199        ail.COL66,
5200        ail.COL67,
5201        ail.COL68,
5202        ail.COL69,
5203        ail.COL70,
5204        ail.COL71,
5205        ail.COL72,
5206        ail.COL73,
5207        ail.COL74,
5208        ail.COL75,
5209        ail.COL76,
5210        ail.COL77,
5211        ail.COL78,
5212        ail.COL79,
5213        ail.COL80,
5214        ail.COL81,
5215        ail.COL82,
5216        ail.COL83,
5217        ail.COL84,
5218        ail.COL85,
5219        ail.COL86,
5220        ail.COL87,
5221        ail.COL88,
5222        ail.COL89,
5223        ail.COL90,
5224        ail.COL91,
5225        ail.COL92,
5226        ail.COL93,
5227        ail.COL94,
5228        ail.COL95,
5229        ail.COL96,
5230        ail.COL97,
5231        ail.COL98,
5232        ail.COL99,
5233        ail.COL100,
5234        ail.COL101,
5235        ail.COL102,
5236        ail.COL103,
5237        ail.COL104,
5238        ail.COL105,
5239        ail.COL106,
5240        ail.COL107,
5241        ail.COL108,
5242        ail.COL109,
5243        ail.COL110,
5244        ail.COL111,
5245        ail.COL112,
5246        ail.COL113,
5247        ail.COL114,
5248        ail.COL115,
5249        ail.COL116,
5250        ail.COL117,
5251        ail.COL118,
5252        ail.COL119,
5253        ail.COL120,
5254        ail.COL121,
5255        ail.COL122,
5256        ail.COL123,
5257        ail.COL124,
5258        ail.COL125,
5259        ail.COL126,
5260        ail.COL127,
5261        ail.COL128,
5262        ail.COL129,
5263        ail.COL130,
5264        ail.COL131,
5265        ail.COL132,
5266        ail.COL133,
5267        ail.COL134,
5268        ail.COL135,
5269        ail.COL136,
5270        ail.COL137,
5271        ail.COL138,
5272        ail.COL139,
5273        ail.COL140,
5274        ail.COL141,
5275        ail.COL142,
5276        ail.COL143,
5277        ail.COL144,
5278        ail.COL145,
5279        ail.COL146,
5280        ail.COL147,
5281        ail.COL148,
5282        ail.COL149,
5283        ail.COL150,
5284        ail.COL151,
5285        ail.COL152,
5286        ail.COL153,
5287        ail.COL154,
5288        ail.COL155,
5289        ail.COL156,
5290        ail.COL157,
5291        ail.COL158,
5292        ail.COL159,
5293        ail.COL160,
5294        ail.COL161,
5295        ail.COL162,
5296        ail.COL163,
5297        ail.COL164,
5298        ail.COL165,
5299        ail.COL166,
5300        ail.COL167,
5301        ail.COL168,
5302        ail.COL169,
5303        ail.COL170,
5304        ail.COL171,
5305        ail.COL172,
5306        ail.COL173,
5307        ail.COL174,
5308        ail.COL175,
5309        ail.COL176,
5310        ail.COL177,
5311        ail.COL178,
5312        ail.COL179,
5313        ail.COL180,
5314        ail.COL181,
5315        ail.COL182,
5316        ail.COL183,
5317        ail.COL184,
5318        ail.COL185,
5319        ail.COL186,
5320        ail.COL187,
5321        ail.COL188,
5322        ail.COL189,
5323        ail.COL190,
5324        ail.COL191,
5325        ail.COL192,
5326        ail.COL193,
5327        ail.COL194,
5328        ail.COL195,
5329        ail.COL196,
5330        ail.COL197,
5331        ail.COL198,
5332        ail.COL199,
5333        ail.COL200,
5334        ail.COL201,
5335        ail.COL202,
5336        ail.COL203,
5337        ail.COL204,
5338        ail.COL205,
5339        ail.COL206,
5340        ail.COL207,
5341        ail.COL208,
5342        ail.COL209,
5343        ail.COL210,
5344        ail.COL211,
5345        ail.COL212,
5346        ail.COL213,
5347        ail.COL214,
5348        ail.COL215,
5349        ail.COL216,
5350        ail.COL217,
5351        ail.COL218,
5352        ail.COL219,
5353        ail.COL220,
5354        ail.COL221,
5355        ail.COL222,
5356        ail.COL223,
5357        ail.COL224,
5358        ail.COL225,
5359        ail.COL226,
5360        ail.COL227,
5361        ail.COL228,
5362        ail.COL229,
5363        ail.COL230,
5364        ail.COL231,
5365        ail.COL232,
5366        ail.COL233,
5367        ail.COL234,
5368        ail.COL235,
5369        ail.COL236,
5370        ail.COL237,
5371        ail.COL238,
5372        ail.COL239,
5373        ail.COL240,
5374        ail.COL241,
5375        ail.COL242,
5376        ail.COL243,
5377        ail.COL244,
5378        ail.COL245,
5379        ail.COL246,
5380        ail.COL247,
5381        ail.COL248,
5382        ail.COL249,
5383        ail.COL250 ,
5384        ail.COL251 ,
5385        ail.COL252 ,
5386        ail.COL253 ,
5387        ail.COL254 ,
5388        ail.COL256 ,
5389        ail.COL255 ,
5390        ail.COL257 ,
5391        ail.COL258 ,
5392        ail.COL259 ,
5393        ail.COL260 ,
5394        ail.COL261 ,
5395        ail.COL262 ,
5396        ail.COL263 ,
5397        ail.COL264 ,
5398        ail.COL265 ,
5399        ail.COL266 ,
5400        ail.COL267 ,
5401        ail.COL268 ,
5402        ail.COL269 ,
5403        ail.COL270 ,
5404        ail.COL271 ,
5405        ail.COL272 ,
5406        ail.COL273 ,
5407        ail.COL274 ,
5408        ail.COL275 ,
5409        ail.COL276 ,
5410        ail.COL277 ,
5411        ail.COL278 ,
5412        ail.COL279 ,
5413        ail.COL280 ,
5414        ail.COL281 ,
5415        ail.COL282 ,
5416        ail.COL283 ,
5417        ail.COL284 ,
5418        ail.COL285 ,
5419        ail.COL286 ,
5420        ail.COL287 ,
5421        ail.COL288 ,
5422        ail.COL289 ,
5423        ail.COL290 ,
5424        ail.COL291 ,
5425        ail.COL292 ,
5426        ail.COL293 ,
5427        ail.COL294 ,
5428        ail.COL295 ,
5429        ail.COL296 ,
5430        ail.COL297 ,
5431        ail.COL298 ,
5432        ail.COL299 ,
5433        ail.COL300     ,
5434        ail.SALES_AGENT_EMAIL_ADDRESS,
5435        ail.RESOURCE_ID,
5436        ail.location_id,
5437        ail.contact_point_id,
5438        ail.orig_system_reference,
5439        ail.CUSTOM_COLUMN1,
5440        ail.CUSTOM_COLUMN2,
5441        ail.CUSTOM_COLUMN3,
5442        ail.CUSTOM_COLUMN4,
5443        ail.CUSTOM_COLUMN5,
5444        ail.CUSTOM_COLUMN6,
5445        ail.CUSTOM_COLUMN7,
5446        ail.CUSTOM_COLUMN8,
5447        ail.CUSTOM_COLUMN9,
5448        ail.CUSTOM_COLUMN10,
5449        ail.CUSTOM_COLUMN11,
5450        ail.CUSTOM_COLUMN12,
5451        ail.CUSTOM_COLUMN13,
5452        ail.CUSTOM_COLUMN14,
5453        ail.CUSTOM_COLUMN15,
5454        ail.CUSTOM_COLUMN16,
5455        ail.CUSTOM_COLUMN17,
5456        ail.CUSTOM_COLUMN18,
5457        ail.CUSTOM_COLUMN19,
5458        ail.CUSTOM_COLUMN20,
5459        ail.CUSTOM_COLUMN21,
5460        ail.CUSTOM_COLUMN22,
5461        ail.CUSTOM_COLUMN23,
5462        ail.CUSTOM_COLUMN24,
5463        ail.CUSTOM_COLUMN25
5464     from   ams_list_entries ail,
5465            ams_act_lists als
5466    where   als.ACT_LIST_HEADER_ID = ale.list_select_action_id
5467      and   als.LIST_HEADER_ID = ail.list_header_id
5468      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
5469      and   ail.enabled_flag = 'Y'
5470      and   rownum <=1 )
5471   where arc_list_select_action_from = 'LIST'
5472   and list_header_id = p_list_header_id
5473   and exists (select 'x'
5474              from ams_list_entries ail,
5475                   ams_act_lists als
5476              where   als.ACT_LIST_HEADER_ID = ale.list_select_action_id
5477                and   als.LIST_HEADER_ID = ail.list_header_id
5478                and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
5479                and   ail.enabled_flag = 'Y' )
5480   and exists (select 'x'
5481              from ams_act_lists als1
5482              where als1.ACT_LIST_HEADER_ID = ale.list_select_action_id
5483                and als1.LIST_ACTION_TYPE = 'INCLUDE' );
5484   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
5485 
5486  ELSE -- For List Generation
5487    write_to_act_log('Generating list(which has selections based on LIST) in update mode.','LIST',g_list_header_id,'LOW');
5488    update ams_list_entries  ale
5489    set (
5490        ale.newly_updated_flag ,
5491        ale.enabled_flag ,
5492        ale.SUFFIX,
5493        ale.FIRST_NAME,
5494        ale.LAST_NAME,
5495        ale.CUSTOMER_NAME,
5496        ale.TITLE,
5497        ale.ADDRESS_LINE1,
5498        ale.ADDRESS_LINE2,
5499        ale.CITY,
5500        ale.STATE,
5501        ale.ZIPCODE,
5502        ale.COUNTRY,
5503        ale.FAX,
5504        ale.PHONE,
5505        ale.EMAIL_ADDRESS,
5506        ale.COL1,
5507        ale.COL2,
5508        ale.COL3,
5509        ale.COL4,
5510        ale.COL5,
5511        ale.COL6,
5512        ale.COL7,
5513        ale.COL8,
5514        ale.COL9,
5515        ale.COL10,
5516        ale.COL11,
5517        ale.COL12,
5518        ale.COL13,
5519        ale.COL14,
5520        ale.COL15,
5521        ale.COL16,
5522        ale.COL17,
5523        ale.COL18,
5524        ale.COL19,
5525        ale.COL20,
5526        ale.COL21,
5527        ale.COL22,
5528        ale.COL23,
5529        ale.COL24,
5530        ale.COL25,
5531        ale.COL26,
5532        ale.COL27,
5533        ale.COL28,
5534        ale.COL29,
5535        ale.COL30,
5536        ale.COL31,
5537        ale.COL32,
5538        ale.COL33,
5539        ale.COL34,
5540        ale.COL35,
5541        ale.COL36,
5542        ale.COL37,
5543        ale.COL38,
5544        ale.COL39,
5545        ale.COL40,
5546        ale.COL41,
5547        ale.COL42,
5548        ale.COL43,
5549        ale.COL44,
5550        ale.COL45,
5551        ale.COL46,
5552        ale.COL47,
5553        ale.COL48,
5554        ale.COL49,
5555        ale.COL50,
5556        ale.COL51,
5557        ale.COL52,
5558        ale.COL53,
5559        ale.COL54,
5560        ale.COL55,
5561        ale.COL56,
5562        ale.COL57,
5563        ale.COL58,
5564        ale.COL59,
5565        ale.COL60,
5566        ale.COL61,
5567        ale.COL62,
5568        ale.COL63,
5569        ale.COL64,
5570        ale.COL65,
5571        ale.COL66,
5572        ale.COL67,
5573        ale.COL68,
5574        ale.COL69,
5575        ale.COL70,
5576        ale.COL71,
5577        ale.COL72,
5578        ale.COL73,
5579        ale.COL74,
5580        ale.COL75,
5581        ale.COL76,
5582        ale.COL77,
5583        ale.COL78,
5584        ale.COL79,
5585        ale.COL80,
5586        ale.COL81,
5587        ale.COL82,
5588        ale.COL83,
5589        ale.COL84,
5590        ale.COL85,
5591        ale.COL86,
5592        ale.COL87,
5593        ale.COL88,
5594        ale.COL89,
5595        ale.COL90,
5596        ale.COL91,
5597        ale.COL92,
5598        ale.COL93,
5599        ale.COL94,
5600        ale.COL95,
5601        ale.COL96,
5602        ale.COL97,
5603        ale.COL98,
5604        ale.COL99,
5605        ale.COL100,
5606        ale.COL101,
5607        ale.COL102,
5608        ale.COL103,
5609        ale.COL104,
5610        ale.COL105,
5611        ale.COL106,
5612        ale.COL107,
5613        ale.COL108,
5614        ale.COL109,
5615        ale.COL110,
5616        ale.COL111,
5617        ale.COL112,
5618        ale.COL113,
5619        ale.COL114,
5620        ale.COL115,
5621        ale.COL116,
5622        ale.COL117,
5623        ale.COL118,
5624        ale.COL119,
5625        ale.COL120,
5626        ale.COL121,
5627        ale.COL122,
5628        ale.COL123,
5629        ale.COL124,
5630        ale.COL125,
5631        ale.COL126,
5632        ale.COL127,
5633        ale.COL128,
5634        ale.COL129,
5635        ale.COL130,
5636        ale.COL131,
5637        ale.COL132,
5638        ale.COL133,
5639        ale.COL134,
5640        ale.COL135,
5641        ale.COL136,
5642        ale.COL137,
5643        ale.COL138,
5644        ale.COL139,
5645        ale.COL140,
5646        ale.COL141,
5647        ale.COL142,
5648        ale.COL143,
5649        ale.COL144,
5650        ale.COL145,
5651        ale.COL146,
5652        ale.COL147,
5653        ale.COL148,
5654        ale.COL149,
5655        ale.COL150,
5656        ale.COL151,
5657        ale.COL152,
5658        ale.COL153,
5659        ale.COL154,
5660        ale.COL155,
5661        ale.COL156,
5662        ale.COL157,
5663        ale.COL158,
5664        ale.COL159,
5665        ale.COL160,
5666        ale.COL161,
5667        ale.COL162,
5668        ale.COL163,
5669        ale.COL164,
5670        ale.COL165,
5671        ale.COL166,
5672        ale.COL167,
5673        ale.COL168,
5674        ale.COL169,
5675        ale.COL170,
5676        ale.COL171,
5677        ale.COL172,
5678        ale.COL173,
5679        ale.COL174,
5680        ale.COL175,
5681        ale.COL176,
5682        ale.COL177,
5683        ale.COL178,
5684        ale.COL179,
5685        ale.COL180,
5686        ale.COL181,
5687        ale.COL182,
5688        ale.COL183,
5689        ale.COL184,
5690        ale.COL185,
5691        ale.COL186,
5692        ale.COL187,
5693        ale.COL188,
5694        ale.COL189,
5695        ale.COL190,
5696        ale.COL191,
5697        ale.COL192,
5698        ale.COL193,
5699        ale.COL194,
5700        ale.COL195,
5701        ale.COL196,
5702        ale.COL197,
5703        ale.COL198,
5704        ale.COL199,
5705        ale.COL200,
5706        ale.COL201,
5707        ale.COL202,
5708        ale.COL203,
5709        ale.COL204,
5710        ale.COL205,
5711        ale.COL206,
5712        ale.COL207,
5713        ale.COL208,
5714        ale.COL209,
5715        ale.COL210,
5716        ale.COL211,
5717        ale.COL212,
5718        ale.COL213,
5719        ale.COL214,
5720        ale.COL215,
5721        ale.COL216,
5722        ale.COL217,
5723        ale.COL218,
5724        ale.COL219,
5725        ale.COL220,
5726        ale.COL221,
5727        ale.COL222,
5728        ale.COL223,
5729        ale.COL224,
5730        ale.COL225,
5731        ale.COL226,
5732        ale.COL227,
5733        ale.COL228,
5734        ale.COL229,
5735        ale.COL230,
5736        ale.COL231,
5737        ale.COL232,
5738        ale.COL233,
5739        ale.COL234,
5740        ale.COL235,
5741        ale.COL236,
5742        ale.COL237,
5743        ale.COL238,
5744        ale.COL239,
5745        ale.COL240,
5746        ale.COL241,
5747        ale.COL242,
5748        ale.COL243,
5749        ale.COL244,
5750        ale.COL245,
5751        ale.COL246,
5752        ale.COL247,
5753        ale.COL248,
5754        ale.COL249,
5755        ale.COL250 ,
5756        ale.COL251     ,
5757        ale.COL252     ,
5758        ale.COL253     ,
5759        ale.COL254     ,
5760        ale.COL256     ,
5761        ale.COL255     ,
5762        ale.COL257     ,
5763        ale.COL258     ,
5764        ale.COL259     ,
5765        ale.COL260     ,
5766        ale.COL261     ,
5767        ale.COL262     ,
5768        ale.COL263     ,
5769        ale.COL264     ,
5770        ale.COL265     ,
5771        ale.COL266     ,
5772        ale.COL267     ,
5773        ale.COL268     ,
5774        ale.COL269     ,
5775        ale.COL270     ,
5776        ale.COL271     ,
5777        ale.COL272     ,
5778        ale.COL273     ,
5779        ale.COL274     ,
5780        ale.COL275     ,
5781        ale.COL276     ,
5782        ale.COL277     ,
5783        ale.COL278     ,
5784        ale.COL279     ,
5785        ale.COL280     ,
5786        ale.COL281     ,
5787        ale.COL282     ,
5788        ale.COL283     ,
5789        ale.COL284     ,
5790        ale.COL285     ,
5791        ale.COL286     ,
5792        ale.COL287     ,
5793        ale.COL288     ,
5794        ale.COL289     ,
5795        ale.COL290     ,
5796        ale.COL291     ,
5797        ale.COL292     ,
5798        ale.COL293     ,
5799        ale.COL294     ,
5800        ale.COL295     ,
5801        ale.COL296     ,
5802        ale.COL297     ,
5803        ale.COL298     ,
5804        ale.COL299     ,
5805        ale.COL300
5806        ) =
5807         (select
5808         'Y',
5809         'Y',
5810        ail.SUFFIX,
5811        ail.FIRST_NAME,
5812        ail.LAST_NAME,
5813        ail.CUSTOMER_NAME,
5814        ail.TITLE,
5815        ail.ADDRESS_LINE1,
5816        ail.ADDRESS_LINE2,
5817        ail.CITY,
5818        ail.STATE,
5819        ail.ZIPCODE,
5820        ail.COUNTRY,
5821        ail.FAX,
5822        ail.PHONE,
5823        ail.EMAIL_ADDRESS,
5824        ail.COL1,
5825        ail.COL2,
5826        ail.COL3,
5827        ail.COL4,
5828        ail.COL5,
5829        ail.COL6,
5830        ail.COL7,
5831        ail.COL8,
5832        ail.COL9,
5833        ail.COL10,
5834        ail.COL11,
5835        ail.COL12,
5836        ail.COL13,
5837        ail.COL14,
5838        ail.COL15,
5839        ail.COL16,
5840        ail.COL17,
5841        ail.COL18,
5842        ail.COL19,
5843        ail.COL20,
5844        ail.COL21,
5845        ail.COL22,
5846        ail.COL23,
5847        ail.COL24,
5848        ail.COL25,
5849        ail.COL26,
5850        ail.COL27,
5851        ail.COL28,
5852        ail.COL29,
5853        ail.COL30,
5854        ail.COL31,
5855        ail.COL32,
5856        ail.COL33,
5857        ail.COL34,
5858        ail.COL35,
5859        ail.COL36,
5860        ail.COL37,
5861        ail.COL38,
5862        ail.COL39,
5863        ail.COL40,
5864        ail.COL41,
5865        ail.COL42,
5866        ail.COL43,
5867        ail.COL44,
5868        ail.COL45,
5869        ail.COL46,
5870        ail.COL47,
5871        ail.COL48,
5872        ail.COL49,
5873        ail.COL50,
5874        ail.COL51,
5875        ail.COL52,
5876        ail.COL53,
5877        ail.COL54,
5878        ail.COL55,
5879        ail.COL56,
5880        ail.COL57,
5881        ail.COL58,
5882        ail.COL59,
5883        ail.COL60,
5884        ail.COL61,
5885        ail.COL62,
5886        ail.COL63,
5887        ail.COL64,
5888        ail.COL65,
5889        ail.COL66,
5890        ail.COL67,
5891        ail.COL68,
5892        ail.COL69,
5893        ail.COL70,
5894        ail.COL71,
5895        ail.COL72,
5896        ail.COL73,
5897        ail.COL74,
5898        ail.COL75,
5899        ail.COL76,
5900        ail.COL77,
5901        ail.COL78,
5902        ail.COL79,
5903        ail.COL80,
5904        ail.COL81,
5905        ail.COL82,
5906        ail.COL83,
5907        ail.COL84,
5908        ail.COL85,
5909        ail.COL86,
5910        ail.COL87,
5911        ail.COL88,
5912        ail.COL89,
5913        ail.COL90,
5914        ail.COL91,
5915        ail.COL92,
5916        ail.COL93,
5917        ail.COL94,
5918        ail.COL95,
5919        ail.COL96,
5920        ail.COL97,
5921        ail.COL98,
5922        ail.COL99,
5923        ail.COL100,
5924        ail.COL101,
5925        ail.COL102,
5926        ail.COL103,
5927        ail.COL104,
5928        ail.COL105,
5929        ail.COL106,
5930        ail.COL107,
5931        ail.COL108,
5932        ail.COL109,
5933        ail.COL110,
5934        ail.COL111,
5935        ail.COL112,
5936        ail.COL113,
5937        ail.COL114,
5938        ail.COL115,
5939        ail.COL116,
5940        ail.COL117,
5941        ail.COL118,
5942        ail.COL119,
5943        ail.COL120,
5944        ail.COL121,
5945        ail.COL122,
5946        ail.COL123,
5947        ail.COL124,
5948        ail.COL125,
5949        ail.COL126,
5950        ail.COL127,
5951        ail.COL128,
5952        ail.COL129,
5953        ail.COL130,
5954        ail.COL131,
5955        ail.COL132,
5956        ail.COL133,
5957        ail.COL134,
5958        ail.COL135,
5959        ail.COL136,
5960        ail.COL137,
5961        ail.COL138,
5962        ail.COL139,
5963        ail.COL140,
5964        ail.COL141,
5965        ail.COL142,
5966        ail.COL143,
5967        ail.COL144,
5968        ail.COL145,
5969        ail.COL146,
5970        ail.COL147,
5971        ail.COL148,
5972        ail.COL149,
5973        ail.COL150,
5974        ail.COL151,
5975        ail.COL152,
5976        ail.COL153,
5977        ail.COL154,
5978        ail.COL155,
5979        ail.COL156,
5980        ail.COL157,
5981        ail.COL158,
5982        ail.COL159,
5983        ail.COL160,
5984        ail.COL161,
5985        ail.COL162,
5986        ail.COL163,
5987        ail.COL164,
5988        ail.COL165,
5989        ail.COL166,
5990        ail.COL167,
5991        ail.COL168,
5992        ail.COL169,
5993        ail.COL170,
5994        ail.COL171,
5995        ail.COL172,
5996        ail.COL173,
5997        ail.COL174,
5998        ail.COL175,
5999        ail.COL176,
6000        ail.COL177,
6001        ail.COL178,
6002        ail.COL179,
6003        ail.COL180,
6004        ail.COL181,
6005        ail.COL182,
6006        ail.COL183,
6007        ail.COL184,
6008        ail.COL185,
6009        ail.COL186,
6010        ail.COL187,
6011        ail.COL188,
6012        ail.COL189,
6013        ail.COL190,
6014        ail.COL191,
6015        ail.COL192,
6016        ail.COL193,
6017        ail.COL194,
6018        ail.COL195,
6019        ail.COL196,
6020        ail.COL197,
6021        ail.COL198,
6022        ail.COL199,
6023        ail.COL200,
6024        ail.COL201,
6025        ail.COL202,
6026        ail.COL203,
6027        ail.COL204,
6028        ail.COL205,
6029        ail.COL206,
6030        ail.COL207,
6031        ail.COL208,
6032        ail.COL209,
6033        ail.COL210,
6034        ail.COL211,
6035        ail.COL212,
6036        ail.COL213,
6037        ail.COL214,
6038        ail.COL215,
6039        ail.COL216,
6040        ail.COL217,
6041        ail.COL218,
6042        ail.COL219,
6043        ail.COL220,
6044        ail.COL221,
6045        ail.COL222,
6046        ail.COL223,
6047        ail.COL224,
6048        ail.COL225,
6049        ail.COL226,
6050        ail.COL227,
6051        ail.COL228,
6052        ail.COL229,
6053        ail.COL230,
6054        ail.COL231,
6055        ail.COL232,
6056        ail.COL233,
6057        ail.COL234,
6058        ail.COL235,
6059        ail.COL236,
6060        ail.COL237,
6061        ail.COL238,
6062        ail.COL239,
6063        ail.COL240,
6064        ail.COL241,
6065        ail.COL242,
6066        ail.COL243,
6067        ail.COL244,
6068        ail.COL245,
6069        ail.COL246,
6070        ail.COL247,
6071        ail.COL248,
6072        ail.COL249,
6073        ail.COL250 ,
6074        ail.COL251 ,
6075        ail.COL252 ,
6076        ail.COL253 ,
6077        ail.COL254 ,
6078        ail.COL256 ,
6079        ail.COL255 ,
6080        ail.COL257 ,
6081        ail.COL258 ,
6082        ail.COL259 ,
6083        ail.COL260 ,
6084        ail.COL261 ,
6085        ail.COL262 ,
6086        ail.COL263 ,
6087        ail.COL264 ,
6088        ail.COL265 ,
6089        ail.COL266 ,
6090        ail.COL267 ,
6091        ail.COL268 ,
6092        ail.COL269 ,
6093        ail.COL270 ,
6094        ail.COL271 ,
6095        ail.COL272 ,
6096        ail.COL273 ,
6097        ail.COL274 ,
6098        ail.COL275 ,
6099        ail.COL276 ,
6100        ail.COL277 ,
6101        ail.COL278 ,
6102        ail.COL279 ,
6103        ail.COL280 ,
6104        ail.COL281 ,
6105        ail.COL282 ,
6106        ail.COL283 ,
6107        ail.COL284 ,
6108        ail.COL285 ,
6109        ail.COL286 ,
6110        ail.COL287 ,
6111        ail.COL288 ,
6112        ail.COL289 ,
6113        ail.COL290 ,
6114        ail.COL291 ,
6115        ail.COL292 ,
6116        ail.COL293 ,
6117        ail.COL294 ,
6118        ail.COL295 ,
6119        ail.COL296 ,
6120        ail.COL297 ,
6121        ail.COL298 ,
6122        ail.COL299 ,
6123        ail.COL300
6124     from   ams_list_entries ail,
6125            ams_list_select_actions als
6126    where   als.list_select_action_id = ale.list_select_action_id
6127      and   als.incl_object_id = ail.list_header_id
6128      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
6129      and   ail.enabled_flag = 'Y'
6130      and   rownum <=1 )
6131   where arc_list_select_action_from = 'LIST'
6132   and list_header_id = p_list_header_id
6133   and exists (select 'x'
6134     from   ams_list_entries ail,
6135            ams_list_select_actions als
6136    where   als.list_select_action_id = ale.list_select_action_id
6137      and   als.incl_object_id = ail.list_header_id
6138      and   ail.list_entry_source_system_id = ale.list_entry_source_system_id
6139      and   ail.enabled_flag = 'Y' )
6140   and exists (select 'x'
6141              from ams_list_select_actions als1
6142              where als1.list_select_action_id = ale.list_select_action_id
6143                and als1.list_action_type = 'INCLUDE' );
6144   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6145 end if;
6146 end;
6147 procedure update_import_list_entries(p_list_header_id in number) is
6148 cursor c1 is
6149 select imp.import_type
6150 from  ams_imp_list_headers_all imp,
6151       ams_list_select_actions ail,
6152       ams_list_headers_all alh
6153 where alh.list_header_id = p_list_header_id
6154 and   alh.list_header_id = ail.action_used_by_id
6155 and   ail.arc_action_used_by = 'LIST'
6156 and   ail.arc_incl_object_from  = 'IMPH'
6157 and   imp.import_list_header_id = ail.incl_object_id ;
6158 l_b2b_flag varchar2(5) := 'U' ;
6159 begin
6160   open c1;
6161   fetch c1 into l_b2b_flag ;
6162   close c1;
6163   write_to_act_log('Import type is '||l_b2b_flag,'LIST',g_list_header_id,'LOW');
6164   if l_b2b_flag = 'B2B' then
6165      write_to_act_log('Generating list(which has selections based on imported B2B list) in update mode.','LIST',g_list_header_id,'LOW');
6166      update ams_list_entries  ale
6167      set (
6168        ale.newly_updated_flag ,
6169 --       ale.enabled_flag ,
6170        ale.ADDRESS_LINE1,
6171        ale.ADDRESS_LINE2,
6172        ale.COL127,
6173        ale.COL128,
6174        ale.COL227,
6175        ale.CITY,
6176        ale.COUNTRY,
6177        ale.COL118,
6178        ale.COL142,
6179        ale.COL138,
6180        ale.COL122,
6181        ale.EMAIL_ADDRESS,
6182        ale.COL239,
6183        ale.FIRST_NAME,
6184        ale.COL243,
6185        ale.COL144,
6186        ale.LAST_NAME,
6187        ale.COL251,
6188        ale.COL252,
6189        ale.COL137,
6190        ale.SUFFIX,
6191        ale.COL259,
6192        ale.COL6,
6193        ale.COL5,
6194        ale.COL7,
6195        ale.PHONE,
6196        ale.ZIPCODE,
6197        ale.COL120,
6198        ale.STATE,
6199        ale.COL125,
6200        ale.COL2,
6201        ale.TITLE,
6202        ale.customer_name,
6203        ale.party_id,
6204        ale.COL276 ,
6205        ale.NOTES                                    ,
6206               ale.VEHICLE_RESPONSE_CODE                   ,
6207               ale.SALES_AGENT_EMAIL_ADDRESS               ,
6208               ale.RESOURCE_ID                              ,
6209               ale.col147,
6210               ale.location_id ,
6211               ale.contact_point_id ,
6212               ale.orig_system_reference,
6213               col116,
6214               col117
6215        )
6216      =
6217      ( select
6218            'Y',
6219 --           'Y',
6220            ail.ADDRESS1,
6221            ail.ADDRESS2,
6222            ail.BEST_TIME_CONTACT_BEGIN,
6223            ail.BEST_TIME_CONTACT_END,
6224            ail.CEO_NAME,
6225            ail.CITY,
6226            ail.COUNTRY,
6227            ail.COUNTY,
6228            ail.DECISION_MAKER_FLAG,
6229            ail.DEPARTMENT,
6230            ail.DUNS_NUMBER,
6231            ail.EMAIL_ADDRESS,
6232            ail.EMPLOYEES_TOTAL,
6233            ail.PERSON_FIRST_NAME,
6234            ail.FISCAL_YEAREND_MONTH,
6235            ail.JOB_TITLE,
6236            ail.PERSON_LAST_NAME,
6237            ail.LEGAL_STATUS,
6238            ail.LINE_OF_BUSINESS,
6239            ail.PERSON_MIDDLE_NAME,
6240            ail.PERSON_NAME_SUFFIX,
6241            ail.party_name,
6242            ail.PHONE_AREA_CODE,
6243            ail.PHONE_COUNTRY_CODE,
6244            ail.PHONE_EXTENTION,
6245            ail.PHONE_NUMBER,
6246            ail.POSTAL_CODE,
6247            ail.PROVINCE,
6248            ail.STATE,
6249            ail.TAX_REFERENCE,
6250            ail.TIME_ZONE,
6251            ail.PERSON_NAME_PREFIX,
6252            ail.party_name
6253            ,ail.party_id
6254            ,ail.YEAR_ESTABLISHED
6255            ,ail.NOTES                                    ,
6256               ail.VEHICLE_RESPONSE_CODE                   ,
6257               ail.SALES_AGENT_EMAIL_ADDRESS               ,
6258               ail.RESOURCE_ID                              ,
6259               ail.ORGANIZATION_ID,
6260              ail.location_id ,
6261              ail.contact_point_id ,
6262              ail.orig_system_reference,
6263               ail.address3,
6264               ail.address4
6265     from   ams_hz_b2b_mapping_v ail,
6266            ams_list_select_actions als
6267    where   ail.import_list_header_id = als.incl_object_id
6268      and   als.list_select_action_id = ale.list_select_action_id
6269      and   ail.party_id = ale.list_entry_source_system_id
6270      and   ail.IMPORT_SOURCE_LINE_ID = ale.IMP_SOURCE_LINE_ID)
6271   where arc_list_select_action_from = 'IMPH'
6272   and list_header_id = p_list_header_id
6273   and exists (select 'x'
6274              from ams_list_select_actions als1
6275              where als1.list_select_action_id = ale.list_select_action_id
6276                and als1.list_action_type = 'INCLUDE' );
6277   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6278   end if;
6279   if l_b2b_flag = 'B2C' then
6280      write_to_act_log('Generating list(which has selections based on imported B2C list) in update mode.','LIST',g_list_header_id,'LOW');
6281      update ams_list_entries  ale
6282      set (
6283        ale.newly_updated_flag ,
6284 --       ale.enabled_flag ,
6285        ale.customer_name,
6286        ale.ADDRESS_LINE1,
6287        ale.ADDRESS_LINE2,
6288        ale.CITY,
6289        ale.COL127,
6290        ale.COL128,
6291        ale.COL118,
6292        ale.COUNTRY,
6293        ale.FIRST_NAME,
6294        ale.LAST_NAME,
6295        ale.COL137,
6296        ale.EMAIL_ADDRESS,
6297        ale.col70,
6298        ale.COL145,
6299        ale.STATE,
6300        ale.ZIPCODE,
6301        ale.COL120,
6302        ale.TITLE,
6303        ale.COL2,
6304        ale.col5,
6305        ale.col6,
6306        ale.PHONE,
6307        ale.col7,
6308        ale.party_id,
6309        ale.SUFFIX  ,
6310        ale.NOTES                                    ,
6311               ale.VEHICLE_RESPONSE_CODE                   ,
6312               ale.SALES_AGENT_EMAIL_ADDRESS               ,
6313               ale.RESOURCE_ID                              ,
6314               ale.location_id ,
6315               ale.contact_point_id ,
6316               ale.orig_system_reference,
6317               ale.col116,
6318               ale.col117
6319        )
6320       =
6321      (select
6322            'Y',
6323 --           'Y',
6324            ail.PERSON_LAST_NAME || ' , ' || ail.PERSON_FIRST_NAME ,
6325            ail.ADDRESS1,
6326            ail.ADDRESS2,
6327            ail.CITY,
6328            ail.BEST_TIME_CONTACT_BEGIN,
6329            ail.BEST_TIME_CONTACT_END,
6330            ail.COUNTY,
6331            ail.COUNTRY,
6332            ail.PERSON_FIRST_NAME,
6333            ail.PERSON_LAST_NAME,
6334            ail.PERSON_MIDDLE_NAME,
6335            ail.EMAIL_ADDRESS,
6336            ail.GENDER,
6337            ail.HOUSEHOLD_INCOME,
6338            ail.STATE,
6339            ail.POSTAL_CODE,
6340            ail.PROVINCE,
6341            ail.PERSON_NAME_PREFIX,
6342            ail.TIME_ZONE  ,
6343            ail.PHONE_COUNTRY_CODE,
6344            ail.PHONE_AREA_CODE   ,
6345            ail.PHONE_NUMBER      ,
6346            ail.PHONE_EXTENTION   ,
6347            ail.party_id,
6348            ail.PERSON_NAME_SUFFIX ,
6349            ail.NOTES                                    ,
6350               ail.VEHICLE_RESPONSE_CODE                   ,
6351               ail.SALES_AGENT_EMAIL_ADDRESS               ,
6352               ail.RESOURCE_ID                              ,
6353           ail.location_id ,
6354           ail.contact_point_id ,
6355           ail.orig_system_reference,
6356               ail.address3,
6357               ail.address4
6358     from   ams_hz_b2c_mapping_v ail,
6359            ams_list_select_actions als
6360    where   ail.import_list_header_id = als.incl_object_id
6361      and   als.list_select_action_id = ale.list_select_action_id
6362      and   ail.party_id = ale.list_entry_source_system_id
6363      and   ail.IMPORT_SOURCE_LINE_ID = ale.IMP_SOURCE_LINE_ID)
6364   where   ale.arc_list_select_action_from = 'IMPH'
6365   and ale.list_header_id = p_list_header_id
6366   and exists (select 'x'
6367              from ams_list_select_actions als1
6368              where als1.list_select_action_id = ale.list_select_action_id
6369                and als1.list_action_type = 'INCLUDE' );
6370   write_to_act_log(sql%rowcount||' entries updated.','LIST',g_list_header_id,'LOW');
6371  end if;
6372 end;
6373 --
6374 -- NAME
6375 --   GET_LIST_ENTRY_DATA.
6376 --
6377 -- PURPOSE
6378 --
6379 --  01/24/2001 GJOBY      Modified for hornet
6380 -- END OF COMMENTS
6381 
6382 PROCEDURE GET_LIST_ENTRY_DATA
6383                  (p_list_header_id in number,
6384                   p_additional_where_condition in varchar2,
6385                   x_return_status OUT NOCOPY varchar2 )IS
6386 
6387 --------------------------------------------------------------------------
6388 --Retrieve all mapping types used in  an action workbook sql statement. --
6389 --this includes all master and sub types, if a type is a sub type there --
6390 --will be values in the c.sub_source_type_pk_column and source_type_code--
6391 --fields.                                                               --
6392 --------------------------------------------------------------------------
6393 CURSOR  C_MAPPING_TYPES_USED(p_list_header_id
6394                              AMS_LIST_HEADERS_ALL.LIST_HEADER_ID%TYPE)
6395 IS SELECT a.list_source_type_id,
6396 	  a.source_type_code,
6397 	  a.source_object_name,
6398 	  a.source_object_pk_field,
6399 	  a.master_source_type_flag
6400      FROM ams_list_src_types a,
6401 	  ams_list_src_type_usages b
6402     WHERE a.source_type_code       = b.source_type_code
6403       AND b.list_header_id         = p_list_header_id
6404       AND master_source_type_flag = 'Y'  ;
6405 cursor c_child_mapping(c_master_type_id in number) is
6406 	SELECT al.SUB_SOURCE_TYPE_ID,
6407                al.SUB_SOURCE_TYPE_PK_COLUMN,
6408                als.source_object_name,
6409                als.source_type_code,
6410                al.master_source_type_pk_column
6411 	 FROM ams_list_src_type_assocs al,
6412               ams_list_src_types als ,
6413               ams_list_src_type_usages b
6414 	WHERE al.MASTER_SOURCE_TYPE_ID   = c_master_type_id
6415 	  AND als.list_source_type_id    = al.sub_source_type_id
6416 	  AND als.source_type_code       = b.source_type_code
6417 	  AND b.list_header_id           = p_list_header_id ;
6418 l_list_source_type_id      AMS_LIST_SRC_TYPES.LIST_SOURCE_TYPE_ID%TYPE;
6419 l_source_type_code         AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6420 l_source_object_name       AMS_LIST_SRC_TYPES.SOURCE_OBJECT_NAME%TYPE;
6421 l_source_object_pk_field   AMS_LIST_SRC_TYPES.SOURCE_OBJECT_PK_FIELD%TYPE;
6422 l_master_source_type_flag  AMS_LIST_SRC_TYPES.MASTER_SOURCE_TYPE_FLAG%TYPE;
6423 l_sub_source_type_pk_column      AMS_LIST_SRC_TYPE_ASSOCS.SUB_SOURCE_TYPE_PK_COLUMN%TYPE;
6424 l_dummy_pk_column      AMS_LIST_SRC_TYPE_ASSOCS.master_SOURCE_TYPE_PK_COLUMN%TYPE;
6425 l_SUB_SOURCE_TYPE_ID      number;
6426 l_SUB_SOURCE_OBJECT_NAME      varchar2(300);
6427 l_sub_source_type_code   AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6428 l_sub_source_master_type   AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6429 l_source_code              AMS_LIST_SRC_TYPES.SOURCE_TYPE_CODE%TYPE;
6430 
6431 ------------------------------------------------------------
6432 --Retrive all the fields to be used for each mapping type.--
6433 ------------------------------------------------------------
6434 CURSOR C_MAPPING_TYPE_FIELDS(p_list_source_type_id
6435                              AMS_LIST_SRC_TYPES.LIST_SOURCE_TYPE_ID%TYPE)
6436 IS
6437 SELECT field_column_name,
6438        source_column_name
6439 FROM   ams_list_src_fields
6440 WHERE  list_source_type_id = p_list_source_type_id
6441   and  used_in_list_entries = 'Y';
6442 
6443 
6444 ------------------------------------------------
6445 --a table which holds list entry column names.--
6446 ------------------------------------------------
6447 TYPE t_list_columns is Table of VARCHAR2(30) index by binary_integer;
6448 
6449 --------------------------------------------------
6450 --a local table variable of type t_list_columns.--
6451 --------------------------------------------------
6452 l_list_entry_columns t_list_columns;
6453 
6454 --------------------------------------------------
6455 --a local table variable of type t_list_columns.--
6456 --------------------------------------------------
6457 l_source_columns t_list_columns;
6458 
6459 --------------------------------------------------------
6460 --Used to initialize variables of type t_list_columns.--
6461 --------------------------------------------------------
6462 l_NULL_table t_list_columns;
6463 
6464 ------------------------------------------
6465 --the number of columns for the mapping.--
6466 ------------------------------------------
6467 l_column_count NUMBER;
6468 l_iterator NUMBER;
6469 l_sub_type_detected NUMBER;
6470 
6471 --------------------------------------------------------------
6472 --The composite strings which compose a valid SQL statement.--
6473 --------------------------------------------------------------
6474 l_update_str      VARCHAR2(32767);
6475 l_select_str      VARCHAR2(32767);
6476 l_header_clause   VARCHAR2(1000);
6477 l_type_clause     VARCHAR2(1000);
6478 l_where_clause    VARCHAR2(1000);
6479 
6480 l_add_where_clause   VARCHAR2(2000);
6481 cursor c_source_map is
6482 select field_column_name
6483 from ams_list_src_fields
6484 where list_source_type_id = l_list_source_type_id--l_sub_source_type_id
6485 and   source_column_name = l_dummy_pk_column;
6486 
6487 l_dummy_sr_column      AMS_LIST_SRC_FIELDS.Field_column_name%TYPE;
6488 l_no_of_chunks number;
6489 
6490 x_msg_count        number;
6491 x_msg_data         varchar2(1000);
6492 l_tot_cnt          number;
6493 l_null		   varchar2(30) := null;
6494 l_remote_cnt       number := 0;
6495 
6496 
6497 BEGIN
6498 
6499     if g_list_header_id is null  then
6500        g_list_header_id := p_list_header_id;
6501     end if;
6502 
6503     if nvl(g_remote_list,'N') = 'Y' then
6504        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;
6505        if l_remote_cnt = 0 then
6506           write_to_act_log('No entries in remote schema for this list/target group. Cannot update.', 'LIST', g_list_header_id,'LOW');
6507           x_return_status := 'S';
6508 	  return;
6509        else
6510           write_to_act_log('List entries will be updated in remote schema.', 'LIST', g_list_header_id,'LOW');
6511           g_remote_list_gen := 'Y';
6512        end if;
6513     end if;
6514 
6515     write_to_act_log('Executing procedure get_list_entry_data.', 'LIST', g_list_header_id,'LOW');
6516     OPEN C_MAPPING_TYPES_USED(p_list_header_id);
6517     LOOP
6518       l_update_str         := 'UPDATE ams_list_entries SET (';
6519       l_select_str         := ' ) = ( SELECT DISTINCT ';
6520       l_header_clause      := ' AND list_header_id = '||
6521                                to_char(p_list_header_id);
6522       l_type_clause        := ' AND list_entry_source_system_type = ';
6523 
6524       l_add_where_clause   :=  ' AND arc_list_select_action_from not in  ' ||
6525                                    -- ' (''LIST'', ''IMPH'') ';LPO
6526 				   ' (''IMPH'') ';
6527 
6528       l_add_where_clause  := l_add_where_clause||' and enabled_flag = '||'''Y''';
6529 
6530       l_add_where_clause   :=  l_add_where_clause ||
6531                                p_additional_where_condition ;
6532 
6533       l_sub_type_detected  :=0;
6534       l_iterator           :=0;
6535       l_list_entry_columns := l_NULL_table;
6536       l_source_columns     := l_NULL_table;
6537 
6538      FETCH C_MAPPING_TYPES_USED INTO l_list_source_type_id,
6539                                       l_source_type_code,
6540                                       l_source_object_name,
6541                                       l_source_object_pk_field,
6542                                       l_master_source_type_flag;
6543 
6544      write_to_act_log('List source type id = '||l_list_source_type_id , 'LIST', g_list_header_id,'LOW');
6545      write_to_act_log('List source type code = '||l_source_type_code , 'LIST', g_list_header_id,'LOW');
6546      write_to_act_log('Source object name = '||l_source_object_name , 'LIST', g_list_header_id,'LOW');
6547      write_to_act_log('Source object primary key = '||l_source_object_pk_field, 'LIST', g_list_header_id,'LOW');
6548      write_to_act_log('Master source type flag= '||l_master_source_type_flag, 'LIST', g_list_header_id,'LOW');
6549      EXIT WHEN C_MAPPING_TYPES_USED%NOTFOUND;
6550 
6551       ------------------------------------------------------------
6552       --getting the field mappings between the mapping type     --
6553       --source object and the list entry table.                 --
6554       ------------------------------------------------------------
6555       OPEN C_MAPPING_TYPE_FIELDS(l_list_source_type_id);
6556       LOOP
6557          l_iterator := l_iterator + 1;
6558          FETCH c_mapping_type_fields
6559          INTO l_list_entry_columns(l_iterator),
6560               l_source_columns(l_iterator);
6561          EXIT WHEN  C_MAPPING_TYPE_FIELDS%NOTFOUND;
6562          write_to_act_log('Field column name = '||l_list_entry_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6563          write_to_act_log('Source column name = '||l_source_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6564       END LOOP;
6565       CLOSE C_MAPPING_TYPE_FIELDS;
6566 
6567       FOR i IN l_list_entry_columns.FIRST .. l_list_entry_columns.LAST LOOP
6568             l_update_str := l_update_str||l_list_entry_columns(i)||',';
6569             l_select_str := l_select_str||l_source_columns(i)||',';
6570       END LOOP;
6571       l_update_str := substr(l_update_str,1,length(l_update_str) - 1);
6572       l_select_str := substr(l_select_str,1,length(l_select_str) - 1);
6573 
6574       --l_update_str := substrb(l_update_str,1,length(l_update_str)-1);
6575       --l_select_str := substrb(l_select_str,1,length(l_select_str)-1);
6576      /* l_update_str := l_update_str || 'newly_updated_flag' ;
6577       l_select_str := l_select_str || ''''|| 'Y'||'''';
6578       l_update_str := l_update_str || ','|| 'enabled_flag' ;
6579       l_select_str := l_select_str || ','|| ''''|| 'Y'||'''';*/
6580 
6581       l_select_str := l_select_str||' FROM '||
6582                       l_source_object_name||' WHERE '||
6583                       l_source_object_pk_field
6584                       ||' = list_entry_source_system_id ';
6585 
6586 	  ----------------------------------------------------------
6587 	  -- Only for Org Contact datasource + filter for role type
6588 	  ----------------------------------------------------------
6589 	  if(l_source_type_code = 'ORGANIZATION_CONTACT_LIST') then
6590 
6591 		l_select_str := l_select_str ||' AND ROLE_TYPE=COL278' ;
6592 
6593 	  end if ;
6594 
6595 
6596 
6597 
6598 
6599       l_type_clause := l_type_clause||' :b2 ';
6600       l_source_code :=  l_source_type_code;
6601       l_where_clause:= ' WHERE list_header_id = :b1 ';
6602 
6603       l_update_str := l_update_str||l_select_str||')'||
6604                        l_where_clause||l_type_clause || l_add_where_clause;
6605       write_to_act_log('Source_code = '||l_source_code, 'LIST', g_list_header_id,'LOW');
6606       write_to_act_log('Update statement : '||l_update_str, 'LIST', g_list_header_id,'HIGH');
6607 
6608       if nvl(g_remote_list,'N') = 'N' then
6609          write_to_act_log('Executing the update statement in local schema', 'LIST', g_list_header_id,'LOW');
6610          EXECUTE IMMEDIATE l_update_str using to_char(p_list_header_id) , l_source_code;
6611       else
6612         write_to_act_log('Executing the update statement in remote schema', 'LIST', g_list_header_id,'LOW');
6613 	execute immediate
6614 	'BEGIN
6615 	 AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
6616 	' END;'
6617 	 using  '1',
6618 	 l_null,
6619 	 'T',
6620 	 l_null,
6621 	 OUT x_return_status,
6622 	 OUT x_msg_count,
6623 	 OUT x_msg_data,
6624 	 g_list_header_id,
6625 	 l_update_str,
6626 	 l_source_code,
6627 	 OUT l_tot_cnt,
6628 	 'UPDATE';
6629       end if;
6630 
6631     write_to_act_log('Identifying attributes from child datasource', 'LIST',g_list_header_id,'LOW');
6632 
6633     OPEN c_child_mapping(l_list_source_type_id);
6634     LOOP
6635       l_dummy_pk_column := '';
6636       l_dummy_sr_column := '';
6637      FETCH c_child_mapping
6638            INTO l_SUB_SOURCE_TYPE_ID,
6639                 l_SUB_SOURCE_TYPE_PK_COLUMN,
6640                 l_SUB_SOURCE_OBJECT_NAME,
6641                 l_sub_source_type_code,
6642                 l_dummy_pk_column;
6643 
6644       EXIT WHEN c_child_mapping%NOTFOUND;
6645       if l_dummy_pk_column is not null then
6646          open c_source_map ;
6647          fetch c_source_map into l_dummy_sr_column;
6648          close c_source_map ;
6649          --l_dummy_sr_column := '';
6650       end if;
6651        l_iterator := 0;
6652       l_list_entry_columns := l_NULL_table;
6653       l_source_columns     := l_NULL_table;
6654       l_update_str         := 'UPDATE ams_list_entries SET (';
6655       l_select_str         := ' ) = ( SELECT ';
6656       l_header_clause      := ' AND list_header_id = '||
6657                                to_char(p_list_header_id);
6658       l_type_clause        := ' AND list_entry_source_system_type = ';
6659       l_add_where_clause       := ' AND arc_list_select_action_from not in  ' ||
6660                                    -- ' (''LIST'', ''IMPH'') ';LPO
6661 				   ' (''IMPH'') ';
6662       l_add_where_clause  := l_add_where_clause || ' and enabled_flag = '||'''Y''';
6663       l_add_where_clause       :=  l_add_where_clause ||
6664                                p_additional_where_condition ;
6665 
6666 
6667       OPEN C_MAPPING_TYPE_FIELDS(l_sub_source_type_id);
6668       LOOP
6669         l_iterator := l_iterator + 1;
6670         FETCH c_mapping_type_fields
6671          INTO l_list_entry_columns(l_iterator),
6672               l_source_columns(l_iterator);
6673          EXIT WHEN  C_MAPPING_TYPE_FIELDS%NOTFOUND;
6674          write_to_act_log('Field column name = '||l_list_entry_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6675          write_to_act_log('Source column name = '||l_source_columns(l_iterator), 'LIST', g_list_header_id,'LOW');
6676       END LOOP;
6677       CLOSE C_MAPPING_TYPE_FIELDS;
6678 
6679       if l_list_entry_columns.count > 0 then
6680          FOR i IN l_list_entry_columns.FIRST .. l_list_entry_columns.LAST LOOP
6681             l_update_str := l_update_str||l_list_entry_columns(i)||',';
6682             l_select_str := l_select_str||l_source_columns(i)||',';
6683          END LOOP;
6684          l_update_str := substrb(l_update_str,1,length(l_update_str)-1);
6685          l_select_str := substrb(l_select_str,1,length(l_select_str)-1);
6686 
6687          if l_dummy_sr_column is NULL OR l_SUB_SOURCE_TYPE_PK_COLUMN = l_dummy_sr_column then
6688             l_select_str := l_select_str||' FROM '||
6689                             l_SUB_SOURCE_OBJECT_NAME||' WHERE '||
6690                             l_SUB_SOURCE_TYPE_PK_COLUMN
6691                             ||' = list_entry_source_system_id ';
6692          else
6693 
6694              l_select_str := l_select_str||' FROM '||
6695                              l_SUB_SOURCE_OBJECT_NAME||' WHERE '||
6696                              l_SUB_SOURCE_TYPE_PK_COLUMN
6697                             ||' = ' || l_dummy_sr_column ;
6698           end if;
6699  -- Code for Specific child data sources
6700 	  if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE1_V') then
6701 
6702 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL1' ;
6703 	 end if ;
6704 
6705 	 if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE2_V') then
6706 
6707 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL18' ;
6708 
6709 	 end if ;
6710 
6711 	 if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE3_V') then
6712 
6713 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL37' ;
6714 
6715 	 end if ;
6716 
6717 	 if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE4_V') then
6718 
6719 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL56' ;
6720 
6721 	 end if ;
6722 
6723 	 if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE5_V') then
6724 
6725 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL75' ;
6726 
6727 	 end if ;
6728 
6729 	 if(l_SUB_SOURCE_OBJECT_NAME = 'AMS_ORG_PHONE6_V') then
6730 
6731 		l_select_str := l_select_str || 'AND CONTACT_POINT_ID = COL94' ;
6732 
6733 	 end if ;
6734 
6735           l_type_clause := l_type_clause||' :b2 ';
6736           l_source_code :=  l_source_type_code;
6737           l_where_clause:= ' WHERE list_header_id = :b1 ';
6738 
6739           l_update_str := l_update_str||l_select_str||')'||
6740                           l_where_clause||l_type_clause || l_add_where_clause;
6741 
6742           l_no_of_chunks  := ceil(length(l_update_str)/2000 );
6743           for i in 1 ..l_no_of_chunks
6744           loop
6745              write_to_act_log(substrb(l_update_str,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'HIGH');
6746           end loop;
6747           write_to_act_log('Source_code = '||l_source_code, 'LIST', g_list_header_id,'LOW');
6748           if nvl(g_remote_list,'N') = 'N' then
6749              EXECUTE IMMEDIATE l_update_str using to_char(p_list_header_id) , l_source_code;
6750           else
6751 	     execute immediate
6752 	     'BEGIN
6753               AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
6754              ' END;'
6755              using  '1',
6756              l_null,
6757              'T',
6758              l_null,
6759              OUT x_return_status,
6760              OUT x_msg_count,
6761              OUT x_msg_data,
6762              g_list_header_id,
6763              l_update_str,
6764              l_source_code,
6765              OUT l_tot_cnt,
6766              'UPDATE';
6767 	     g_remote_list_gen := 'Y';
6768           end if;
6769        else
6770           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');
6771       end if; -- if l_list_entry_columns.count > 0 then
6772     END LOOP;
6773     CLOSE c_child_mapping;
6774     END LOOP;
6775     CLOSE C_MAPPING_TYPES_USED;
6776      x_return_status := FND_API.G_RET_STS_SUCCESS;
6777     write_to_act_log('Calling update_list_entries to update selections based on list.','LIST', g_list_header_id,'LOW');
6778    -- update_list_entries(p_list_header_id); LPO
6779     write_to_act_log('Calling update_import_list_entries to update selections based on import.','LIST', g_list_header_id,'LOW');
6780     update_import_list_entries (p_list_header_id);
6781 EXCEPTION
6782   WHEN OTHERS THEN
6783 
6784     IF(C_MAPPING_TYPES_USED%ISOPEN )THEN
6785        CLOSE  C_MAPPING_TYPES_USED;
6786     END IF;
6787 
6788     IF(C_MAPPING_TYPE_FIELDS%ISOPEN )THEN
6789          CLOSE  C_MAPPING_TYPE_FIELDS;
6790     END IF;
6791 
6792     write_to_act_log('Error while generating list in update mode : '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
6793     x_return_status := FND_API.G_RET_STS_ERROR;
6794 END GET_LIST_ENTRY_DATA;
6795 
6796 
6797 -- START OF COMMENTS
6798 -- NAME  :   GENERATE_LIST.
6799 -- PURPOSE
6800 --   1. Public Procedure which when called will generate a set of list
6801 --      entries into the ams_list_entries table.
6802 -- HISTORY
6803 --   06/01/1999        tdonohoe            created
6804 --   01/24/2001        gjoby               Re-Created for Hornet
6805 -- END OF COMMENTS
6806 -------------------------------------------------------------------------------
6807 
6808 PROCEDURE GENERATE_LIST
6809 ( p_api_version            IN     NUMBER,
6810   p_init_msg_list          IN     VARCHAR2   := FND_API.G_TRUE,
6811   p_commit                 IN     VARCHAR2   := FND_API.G_FALSE,
6812   p_validation_level       IN     NUMBER     := FND_API.G_VALID_LEVEL_FULL,
6813   p_list_header_id         IN     NUMBER,
6814   x_return_status          OUT NOCOPY    VARCHAR2,
6815   x_msg_count              OUT NOCOPY    NUMBER,
6816   x_msg_data               OUT NOCOPY    VARCHAR2) IS
6817 
6818   l_api_name            CONSTANT VARCHAR2(30)  := 'GENERATE_LIST';
6819   l_api_version         CONSTANT NUMBER        := 1.0;
6820 
6821   -----------------------------------------------------------
6822   --The no. of entries flagged as duplicates for this list.--
6823   --Only populated if deduplication is requested.          --
6824   -----------------------------------------------------------
6825   --gjoby check if l_no_of_duplicates required
6826   l_no_of_duplicates      NUMBER := 0;
6827 
6828   -- Two records are required for init rec and complete rec
6829   -- Table ams_list_headers_all_tl and ams_list_headers_all
6830   l_listheader_rec        ams_listheader_pvt.list_header_rec_type;
6831   l_tmp_listheader_rec    ams_listheader_pvt.list_header_rec_type;
6832 
6833   -- Two records are required for init rec and complete rec
6834   -- Table ams_list_select_actions
6835   l_listaction_rec        ams_listaction_pvt.action_rec_type;
6836   l_tmp_listaction_rec    ams_listaction_pvt.action_rec_type;
6837   l_no_of_rows_in_list number ;
6838   cursor c_get_rows (c_list_header_id in number ) is
6839   select no_of_rows_in_list
6840   from ams_list_headers_all
6841   where list_header_id = c_list_header_id ;
6842   l_error_position       varchar2(100);
6843 
6844   cursor c_remote_list is
6845   select nvl(stypes.remote_flag,'N') ,database_link
6846     from ams_list_src_types stypes, ams_list_headers_all list
6847    where list.list_source_type = stypes.source_type_code
6848      and list_header_id  =  p_list_header_id;
6849 
6850   l_list_selection 	varchar2(1);
6851   l_onlylist_selection  varchar2(1);
6852   l_no_of_rows  number := 0;
6853 
6854 cursor c_check_gen_mode is
6855 select nvl(no_of_rows_in_list ,0)
6856   from ams_list_headers_all
6857  where list_header_id = p_list_header_id;
6858 
6859   cursor c_list_selection is
6860   select 'Y' from ams_list_select_actions
6861    where  action_used_by_id = p_list_header_id
6862      and  arc_action_used_by = 'LIST'
6863      and  arc_incl_object_from in ('CELL','DIWB','SQL');
6864 
6865   cursor c_only_list_selection is
6866   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
6867    where  act.action_used_by_id = p_list_header_id
6868    and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
6869    and  act.INCL_OBJECT_ID = head.list_header_id
6870    and  head.status_code = 'AVAILABLE'
6871    and  head.MIGRATION_DATE is null;
6872 
6873 l_null		varchar2(30) := null;
6874 l_total_recs	number;
6875 l_request_id    number :=0;
6876 --Bug 5235979. Bmuthukr
6877 
6878 /* cursor c1 is
6879     SELECT list_rule_id
6880        FROM ams_list_rule_usages
6881        WHERE list_header_id = g_list_header_id;*/
6882 
6883 CURSOR C1 IS
6884 SELECT us.list_rule_id
6885   FROM ams_list_rule_usages us, ams_list_rules_all rules
6886  WHERE us.list_header_id = g_list_header_id
6887    AND us.list_rule_id = rules.list_rule_id
6888    AND rules.list_source_type = l_listheader_rec.list_source_type
6889    AND rules.list_rule_type = 'TARGET';
6890 
6891 -- Ends changes
6892 
6893 l_list_rule_id number := 0;
6894 l_action varchar2(30) := 'LIST';
6895 
6896 l_list_field_mapped  varchar2(1);
6897 
6898 -- SOLIN, bug 4410333
6899 -- check whether datasource is enabled.
6900 cursor c_check_datasource(c_list_header_id NUMBER) is
6901   SELECT a.enabled_flag
6902   FROM ams_list_src_types a,
6903        ams_list_headers_all b
6904   WHERE a.source_type_code = b.list_source_type
6905     AND b.list_header_id = c_list_header_id;
6906 
6907 l_ds_enabled_flag      VARCHAR2(1);
6908 -- SOLIN, end
6909 
6910 cursor c_master_ds_fields_mapped is
6911 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
6912 where hd.list_header_id = p_list_header_id
6913   and hd.LIST_SOURCE_TYPE = ty.source_type_code
6914   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
6915   and fd.FIELD_COLUMN_NAME is NOT NULL;
6916 
6917 cursor c_child_ds_fields_mapped is
6918 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
6919 ams_list_src_type_assocs ats
6920 where hd.list_header_id = p_list_header_id
6921   and hd.LIST_SOURCE_TYPE = ty.source_type_code
6922   and ty.list_source_type_id = ats.master_source_type_id
6923   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
6924   and fd.FIELD_COLUMN_NAME is NOT NULL;
6925 
6926 -- SOLIN, bug 3484653
6927 CURSOR c_get_dup_fields(c_list_header_id NUMBER) IS
6928 SELECT min(master_child.field_column_name1) ,count(master_child.field_column_name) from
6929   (
6930   SELECT d.field_column_name field_column_name1,d.field_column_name
6931   FROM ams_list_src_types a,
6932        ams_list_headers_all b,
6933        ams_list_src_fields d
6934   WHERE a.source_type_code = b.list_source_type
6935    and b.list_header_id = p_list_header_id
6936    and d.list_source_type_id = a.list_source_type_id
6937    and d.USED_IN_LIST_ENTRIES = 'Y'
6938   union all
6939    SELECT d.field_column_name field_column_name1,d.field_column_name
6940   FROM ams_list_src_types a,
6941        ams_list_headers_all b,
6942        ams_list_src_fields d,
6943        ams_list_src_type_assocs e
6944   WHERE a.source_type_code = b.list_source_type
6945    and b.list_header_id = p_list_header_id
6946    and e.master_source_type_id = a.list_source_type_id
6947    and d.list_source_type_id = e.sub_source_type_id
6948    and d.USED_IN_LIST_ENTRIES = 'Y'
6949    ) master_child
6950   GROUP BY master_child.field_column_name
6951   having COUNT(master_child.field_column_name) > 1;
6952 
6953 --bmuthukr bug 4997699
6954 l_ds_name         varchar2(1000);
6955 l_field_col_name  varchar2(1000);
6956 l_source_col_name varchar2(1000);
6957 
6958 cursor c_get_dup_mapping(p_col_name in varchar2) is
6959 SELECT d.source_column_name, d.field_column_name , d.de_list_source_type_code  stc
6960   FROM ams_list_src_types a,
6961        ams_list_headers_all b,
6962        ams_list_src_fields d
6963  WHERE a.source_type_code = b.list_source_type
6964    and b.list_header_id = g_list_header_id
6965    and d.list_source_type_id = a.list_source_type_id
6966    and d.field_column_name = p_col_name
6967    and d.USED_IN_LIST_ENTRIES = 'Y'
6968 union all
6969 SELECT d.source_column_name, d.field_column_name,  d.de_list_source_type_code stc
6970   FROM ams_list_src_types a,
6971        ams_list_headers_all b,
6972        ams_list_src_fields d,
6973        ams_list_src_type_assocs e
6974  WHERE a.source_type_code = b.list_source_type
6975    and b.list_header_id = g_list_header_id
6976    and e.master_source_type_id = a.list_source_type_id
6977    and d.list_source_type_id = e.sub_source_type_id
6978    and d.field_column_name = p_col_name
6979    and d.USED_IN_LIST_ENTRIES = 'Y';
6980 
6981 l_field_column_name VARCHAR2(30);
6982 l_count             NUMBER;
6983 -- SOLIN, end
6984 
6985 /* added for remote bug ... savio ******/
6986 /*************************************************/
6987 CURSOR C10(P_LIST_HEADER_ID NUMBER) IS
6988   SELECT
6989     LIST_HEADER_ID,
6990     LAST_UPDATE_DATE,
6991     LAST_UPDATED_BY,
6992     CREATION_DATE,
6993     CREATED_BY,
6994     LAST_UPDATE_LOGIN,
6995     LIST_USED_BY_ID,
6996     ARC_LIST_USED_BY,
6997     LIST_TYPE,
6998     STATUS_CODE,
6999     STATUS_DATE,
7000     GENERATION_TYPE,
7001     OWNER_USER_ID,
7002     ROW_SELECTION_TYPE,
7003     NO_OF_ROWS_MAX_REQUESTED
7004   FROM AMS_LIST_HEADERS_ALL
7005  WHERE LIST_HEADER_ID = P_LIST_HEADER_ID;
7006 
7007 CURSOR C11(P_LIST_HEADER_ID NUMBER,
7008            P_ACTION         VARCHAR2) IS
7009   SELECT
7010     LIST_SELECT_ACTION_ID,
7011     LAST_UPDATE_DATE,
7012     LAST_UPDATED_BY,
7013     CREATION_DATE,
7014     CREATED_BY,
7015     LAST_UPDATE_LOGIN,
7016     ORDER_NUMBER,
7017     LIST_ACTION_TYPE,
7018     INCL_OBJECT_NAME,
7019     ARC_INCL_OBJECT_FROM,
7020     RANK,
7021     NO_OF_ROWS_AVAILABLE,
7022     NO_OF_ROWS_REQUESTED,
7023     NO_OF_ROWS_USED,
7024     DISTRIBUTION_PCT,
7025     ARC_ACTION_USED_BY,
7026     ACTION_USED_BY_ID,
7027     INCL_CONTROL_GROUP,
7028     NO_OF_ROWS_TARGETED,
7029     NO_OF_ROWS_DUPLICATES,
7030     RUNNING_TOTAL,
7031     DELTA
7032  FROM  AMS_LIST_SELECT_ACTIONS
7033  WHERE ACTION_USED_BY_ID = P_LIST_HEADER_ID
7034    AND ARC_ACTION_USED_BY = P_ACTION;
7035 
7036  CURSOR C_WAIT_LIST(P_LIST_HEADER_ID NUMBER) IS
7037  SELECT COUNT(1)
7038  FROM  AMS_LIST_SELECT_ACTIONS ACT,
7039  AMS_LIST_HEADERS_ALL HEAD
7040  WHERE  ACT.ACTION_USED_BY_ID = P_LIST_HEADER_ID
7041    AND  ACT.ARC_INCL_OBJECT_FROM = 'LIST' AND ACT.ARC_ACTION_USED_BY = 'LIST'
7042    AND  ACT.INCL_OBJECT_ID = HEAD.LIST_HEADER_ID
7043    AND  HEAD.STATUS_CODE IN ('SCHEDULED','GENERATING')
7044    AND  HEAD.LIST_HEADER_ID>P_LIST_HEADER_ID;
7045 
7046  l_wait_count             NUMBER:=1;
7047 
7048 c11_rec  c11%rowtype;
7049 c10_rec  c10%rowtype;
7050 l_main_random_nth_row_select number;
7051 
7052 
7053 l_remote_list_gen varchar2(1) := 'N';
7054 
7055 /* added for remote bug ... savio ******/
7056 
7057   l_is_manual   varchar2(1) := 'N'; --Added by bmuthukr for bug 3710720
7058 
7059 
7060 BEGIN
7061   l_error_position := '<- start List generate ->';
7062   -----------------------------------------------------------------------------
7063   -- g_list_header_id global variable for this session
7064   -- This eliminates the need for passing variables across procedures
7065   -- Particularly for logging debug messages ams_act_logs
7066   -----------------------------------------------------------------------------
7067 
7068   l_request_id := nvl(FND_GLOBAL.conc_request_id, -1);
7069 
7070   g_remote_list           := 'N';
7071   g_remote_list_gen       := 'N';
7072   g_database_link         := ' ';
7073   g_list_header_id        :=  p_list_header_id;
7074   g_count                 := 1;
7075   g_message_table  := g_message_table_null ;
7076 
7077   find_log_level(p_list_header_id);
7078 
7079   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');
7080   --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');
7081   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');
7082 
7083 
7084      -- AMLAL: Check if the list has to wait on any included/excluded list for generation
7085 	write_to_act_log(p_msg_data => 'Check if list gen has to wait ',p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
7086 
7087   while l_wait_count>0 loop
7088 
7089 	  OPEN C_WAIT_LIST(p_list_header_id);
7090 	  FETCH C_WAIT_LIST INTO l_wait_count;
7091 	  CLOSE C_WAIT_LIST;
7092 
7093 	  IF l_wait_count>0 THEN
7094 		  write_to_act_log(p_msg_data => 'Waiting for 15 secs for included list ',p_arc_log_used_by => 'LIST',p_log_used_by_id  => p_list_header_id,p_level => 'HIGH');
7095 		  dbms_lock.sleep(15);
7096 	  END IF;
7097   end loop;
7098 
7099   --Added by bmuthukr for bug 3710720
7100   is_manual(p_list_header_id  => p_list_header_id,
7101             x_return_status   => x_return_status,
7102             x_msg_count       => x_msg_count,
7103             x_msg_data        => x_msg_data,
7104             x_is_manual       => l_is_manual);
7105   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7106      write_to_act_log('Error in executing is_manual procedure', 'LIST', g_list_header_id,'HIGH');
7107      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7108      logger;
7109      return;
7110   end if;
7111 
7112   if nvl(l_is_manual,'N') = 'Y' then
7113      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');
7114      logger;
7115      return;
7116   end if;
7117   --Ends changes.
7118 
7119   -- SOLIN, bug 4410333
7120   l_ds_enabled_flag := 'N';
7121   OPEN c_check_datasource(p_list_header_id);
7122   FETCH c_check_datasource INTO l_ds_enabled_flag;
7123   CLOSE c_check_datasource;
7124 
7125   IF l_ds_enabled_flag = 'N' THEN
7126      write_to_act_log(
7127           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.',
7128           p_arc_log_used_by => 'LIST',
7129           p_log_used_by_id  => p_list_header_id,
7130 	  p_level => 'HIGH');
7131      UPDATE ams_list_headers_all
7132         SET last_generation_success_flag = 'N',
7133             status_code                  = 'FAILED',
7134             user_status_id               = 311,
7135             status_date                  = sysdate,
7136             last_update_date             = sysdate,
7137             main_gen_end_time            = sysdate
7138       WHERE list_header_id               = g_list_header_id;
7139       FND_MSG_PUB.count_and_get(
7140             p_encoded => FND_API.g_false,
7141             p_count   => x_msg_count,
7142             p_data    => x_msg_data
7143       );
7144       x_return_status := FND_API.g_ret_sts_error;
7145       --
7146       logger;
7147       RETURN;
7148   END IF;
7149 -- SOLIN, end
7150 
7151 
7152   open  c_master_ds_fields_mapped;
7153   fetch c_master_ds_fields_mapped into l_list_field_mapped;
7154   close c_master_ds_fields_mapped;
7155 
7156   open  c_child_ds_fields_mapped;
7157   fetch c_child_ds_fields_mapped into l_list_field_mapped;
7158   close c_child_ds_fields_mapped;
7159 
7160   if l_list_field_mapped is NULL THEN
7161      write_to_act_log(p_msg_data => 'Master/Child datasource fields are not mapped. Aborting list generation. ' ,
7162                       p_arc_log_used_by => 'LIST',
7163                       p_log_used_by_id  => p_list_header_id,
7164   	  	      p_level =>'HIGH');
7165 
7166       UPDATE ams_list_headers_all
7167       SET    last_generation_success_flag = 'N',
7168              status_code                  = 'FAILED',
7169              user_status_id               = 311,
7170              status_date                  = sysdate,
7171              last_update_date             = sysdate,
7172              main_gen_end_time            = sysdate
7173       WHERE  list_header_id               = p_list_header_id;
7174    -- calling logging program
7175       logger;
7176    --
7177      IF FND_API.To_Boolean ( p_commit ) THEN
7178        COMMIT WORK;
7179      END IF;
7180      --Modified by bmuthukr. Bug # 4083665
7181      x_return_status := FND_API.g_ret_sts_error;
7182      --
7183      RETURN;
7184   end if;
7185 
7186   -- SOLIN, bug 3484653
7187   OPEN c_get_dup_fields(p_list_header_id);
7188   FETCH c_get_dup_fields INTO l_field_column_name, l_count;
7189   CLOSE c_get_dup_fields;
7190 
7191   IF l_count>1 THEN
7192      /*delete from ams_act_logs
7193       where arc_act_log_used_by = 'LIST'
7194         and act_log_used_by_id  = p_list_header_id ;*/
7195 
7196 
7197      write_to_act_log(
7198           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.',
7199           p_arc_log_used_by => 'LIST',
7200           p_log_used_by_id  => p_list_header_id,
7201 	  p_level => 'HIGH');
7202      --bmuthukr bug 4997699
7203      open c_get_dup_mapping(l_field_column_name);
7204      loop
7205         fetch c_get_dup_mapping into l_source_col_name, l_field_col_name  ,l_ds_name;
7206 	exit when c_get_dup_mapping%notfound;
7207         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');
7208      end loop;
7209      --
7210      UPDATE ams_list_headers_all
7211         SET last_generation_success_flag = 'N',
7212             status_code                  = 'FAILED',
7213             user_status_id               = 311,
7214             status_date                  = sysdate,
7215             last_update_date             = sysdate,
7216             main_gen_end_time            = sysdate
7217       WHERE list_header_id               = g_list_header_id;
7218       --Modified by bmuthukr. Bug # 4083665
7219       x_return_status := FND_API.g_ret_sts_error;
7220       --
7221       logger;
7222       RETURN;
7223   END IF;
7224 -- SOLIN, end
7225 
7226   write_to_act_log(p_msg_data => 'List header id is '||g_list_header_id,
7227                    p_arc_log_used_by => 'LIST',
7228                    p_log_used_by_id  => p_list_header_id,
7229 		   p_level => 'LOW');
7230 
7231   x_return_status := FND_API.G_RET_STS_SUCCESS;
7232 
7233   -- Standard call to check for call compatibility.
7234   IF NOT FND_API.Compatible_API_Call ( l_api_version,
7235                                        p_api_version,
7236                                        l_api_name,
7237                                        G_PKG_NAME)
7238   THEN
7239     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7240   END IF;
7241 
7242   IF FND_API.to_Boolean( p_init_msg_list ) THEN
7243      FND_MSG_PUB.initialize;
7244   END IF;
7245 
7246   -- Checking if Debug is set. If debug is set then log debugging message
7247   IF (AMS_DEBUG_HIGH_ON) THEN
7248      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7249      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration : Start');
7250      FND_MSG_PUB.Add;
7251   END IF;
7252 
7253   --  Initialize API return status to success
7254   x_return_status := FND_API.G_RET_STS_SUCCESS;
7255 
7256 
7257   open c_remote_list;
7258   fetch c_remote_list into g_remote_list,g_database_link;
7259   close c_remote_list;
7260 
7261   if nvl(g_remote_list,'N') = 'N' then
7262      write_to_act_log(p_msg_data => 'List is based not on remote datasource.',
7263                       p_arc_log_used_by => 'LIST',
7264                       p_log_used_by_id  => p_list_header_id,
7265    	   	      p_level => 'LOW');
7266   elsif nvl(g_remote_list,'Y') = 'Y' then
7267      write_to_act_log(p_msg_data => 'List is based on remote datasource. Database link is  ' ||g_database_link,
7268                       p_arc_log_used_by => 'LIST',
7269                       p_log_used_by_id  => p_list_header_id,
7270    	   	      p_level => 'HIGH');
7271   end if;
7272 
7273   if g_remote_list = 'Y' then
7274 
7275      remote_list_gen(p_list_header_id  => p_list_header_id,
7276                      x_return_status   => x_return_status,
7277                      x_msg_count       => x_msg_count,
7278                      x_msg_data        => x_msg_data,
7279                      x_remote_gen      => g_remote_list_gen);
7280      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7281         write_to_act_log('Error in executing remote_list_gen procedure', 'LIST', g_list_header_id,'HIGH');
7282         write_to_act_log('Error '||x_msg_data, 'LIST', g_list_header_id,'HIGH');
7283      else
7284         write_to_act_log(p_msg_data => 'remote_list_gen procedure executed successfully.' ,
7285                          p_arc_log_used_by => 'LIST',
7286                          p_log_used_by_id  => p_list_header_id,
7287  			 p_level => 'LOW');
7288      end if;
7289 
7290   end if;
7291 
7292   l_error_position := '<- Init List->';
7293   -----------------------------------------------------------------------------
7294   -- Gets list header record details
7295   -- Intialize the record, set the list header id and retrieve the records
7296   -----------------------------------------------------------------------------
7297   write_to_act_log(p_msg_data => 'Calling ams_listheader_pvt to get the list header details.' ,
7298                    p_arc_log_used_by => 'LIST',
7299                    p_log_used_by_id  => p_list_header_id,
7300    	   	   p_level => 'LOW');
7301 
7302   ams_listheader_pvt.init_listheader_rec(l_tmp_listheader_rec);
7303   l_tmp_listheader_rec.list_header_id := p_list_header_id;
7304 
7305   l_error_position := '<- complete rec ->';
7306   ams_listheader_pvt.complete_listheader_rec
7307                    (p_listheader_rec  =>l_tmp_listheader_rec,
7308                     x_complete_rec    =>l_listheader_rec);
7309   -----------------------------------------------------------------------------
7310 
7311   -----------------------------------------------------------
7312   -- Initializes the list header record
7313   -----------------------------------------------------------
7314   l_error_position := '<- Initialize List ->';
7315 
7316   write_to_act_log(p_msg_data => 'Calling initialize_list to initialize the list.' ,
7317                    p_arc_log_used_by => 'LIST',
7318                    p_log_used_by_id  => p_list_header_id,
7319    	   	   p_level => 'LOW');
7320 
7321   initialize_List(p_list_header_rec => l_listheader_rec,
7322                   x_msg_count       => x_msg_count,
7323                   x_msg_data        => x_msg_data,
7324                   x_return_status   => x_return_status);
7325   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7326      write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
7327      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7328   else
7329      write_to_act_log(p_msg_data => 'List initialized.' ,
7330                       p_arc_log_used_by => 'LIST',
7331                       p_log_used_by_id  => p_list_header_id,
7332    	       	      p_level => 'LOW');
7333   end if;
7334 
7335   IF x_return_status = FND_API.g_ret_sts_error THEN
7336      RAISE FND_API.g_exc_error;
7337   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7338      RAISE FND_API.g_exc_unexpected_error;
7339   END IF;
7340 
7341   if l_listheader_rec.generation_type = 'UPD' then
7342      open c_check_gen_mode;
7343      fetch c_check_gen_mode into l_no_of_rows;
7344      close c_check_gen_mode;
7345 
7346      if l_no_of_rows = 0 then
7347         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');
7348         UPDATE ams_list_headers_all
7349            SET last_generation_success_flag = 'N',
7350                status_code                  = 'FAILED',
7351                ctrl_status_code             = 'DRAFT',
7352                user_status_id               = 311,
7353                status_date                  = sysdate,
7354                last_update_date             = sysdate,
7355                main_gen_end_time            = sysdate,
7356                no_of_rows_in_ctrl_group     = null
7357          WHERE list_header_id               = g_list_header_id;
7358          x_return_status := FND_API.g_ret_sts_error;
7359          logger;
7360          RETURN;
7361       end if;
7362    end if;
7363 
7364 --From R12, only enabled entries will be updated in the update mode.
7365 --So no need to enable all the entries before generation.
7366   -- if l_listheader_rec.generation_type = 'UPDATE' then
7367 /*  if l_listheader_rec.generation_type = 'UPD' then
7368      write_to_act_log(' List is generated in UPDATE mode', 'LIST', g_list_header_id,'HIGH');
7369        update ams_list_entries
7370        set newly_updated_flag = 'N',
7371 	   enabled_flag = 'Y'
7372        where list_header_id = l_listheader_rec.list_header_id;
7373 */
7374    /********************************************************************
7375     Dynamic procedure will update the list from the remote instance in
7376     case of remote list
7377    *********************************************************************/
7378  /*    write_to_act_log(p_msg_data => 'Updating the list in remote instance. ',
7379                       p_arc_log_used_by => 'LIST',
7380                       p_log_used_by_id  => p_list_header_id,
7381 		      p_level => 'LOW');
7382      if g_remote_list = 'Y' then
7383        execute immediate
7384       'BEGIN
7385       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
7386       ' END;'
7387       using  '1',
7388              l_null,
7389              'T',
7390              l_null,
7391              OUT x_return_status,
7392              OUT x_msg_count,
7393              OUT x_msg_data,
7394              l_listheader_rec.list_header_id,
7395              l_null,
7396              l_null,
7397              OUT l_total_recs,
7398              'UPDATE';
7399        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7400           write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
7401           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7402        else
7403           write_to_act_log(p_msg_data => 'List generated in UPDATE mode in remote instance. ' ,
7404                            p_arc_log_used_by => 'LIST',
7405                            p_log_used_by_id  => p_list_header_id,
7406 	  		   p_level => 'LOW');
7407        end if;
7408      end if;
7409   end if;
7410 */
7411   update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
7412   if l_listheader_rec.generation_type = 'UPD' then
7413        l_error_position := '<- Get_list_entry_data inside deduplication ->';
7414 --For bug 5216890
7415 --   if g_remote_list <> 'Y' then
7416      write_to_act_log('List is generated in UPDATE mode in local instance.', 'LIST', g_list_header_id,'HIGH');
7417    --
7418    -- This will not be performed for the remote list generation
7419    --
7420      GET_LIST_ENTRY_DATA(
7421            p_list_header_id =>l_listheader_rec.list_header_id,
7422            x_return_status => x_return_status);
7423      IF x_return_status = FND_API.g_ret_sts_error THEN
7424         RAISE FND_API.g_exc_error;
7425      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7426         RAISE FND_API.g_exc_unexpected_error;
7427      END IF;
7428   -- end if;
7429 --  END IF;
7430   ELSE --
7431   l_error_position := '<- Process List Actions  ->';
7432   write_to_act_log('List will be generated in '||l_listheader_rec.generation_type||' mode.','LIST', g_list_header_id,'HIGH');
7433   write_to_act_log('Calling process_list_actions to generate list.', 'LIST', g_list_header_id,'LOW');
7434   process_list_Actions(p_action_used_by_id => l_listheader_rec.list_header_id,
7435                        p_action_used_by    => 'LIST',
7436                        p_log_flag          => l_listheader_rec.enable_log_flag,
7437                        x_return_status     => x_return_status,
7438                        x_msg_count         => x_msg_count,
7439                        x_msg_data          => x_msg_data);
7440   if x_return_status = 'E' then
7441      logger;
7442      commit;
7443      return;
7444   end if;
7445   END IF;
7446 
7447   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7448      write_to_act_log('Error in generating list.', 'LIST', g_list_header_id,'HIGH');
7449      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7450   end if;
7451 
7452   IF x_return_status = FND_API.g_ret_sts_error THEN
7453      RAISE FND_API.g_exc_error;
7454   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7455      RAISE FND_API.g_exc_unexpected_error;
7456   END IF;
7457 
7458 
7459 /******************************************************************************/
7460 /**************** call for cancel list generation added 05/23/2005 ************/
7461 /******************************************************************************/
7462 -- Inside generate_list 1
7463 
7464    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
7465                p_list_header_id => g_list_header_id ,
7466                p_remote_gen     => g_remote_list    ,
7467                p_remote_gen_list=> g_remote_list_gen,
7468                p_database_link  => g_database_link,
7469                x_msg_count      => x_msg_count ,
7470                x_msg_data       => x_msg_data ,
7471                x_return_status  => x_return_status
7472          );
7473 
7474   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
7475      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7476         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
7477         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
7478      end if;
7479      RAISE FND_API.G_EXC_ERROR;
7480   ELSE
7481      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
7482   END IF;
7483 
7484 /******************************************************************************/
7485 /**************** call for cancel list generation added 05/23/2005 ************/
7486 /******************************************************************************/
7487 /*
7488   -- if l_listheader_rec.generation_type = 'UPDATE' then
7489   if l_listheader_rec.generation_type = 'UPD' then
7490      l_error_position := '<- set enabled flag for gen type UPDATE ';
7491     if g_remote_list <> 'Y' then
7492        update ams_list_entries
7493        set enabled_flag  = 'N'
7494        where newly_updated_flag = 'N'
7495          and list_header_id = l_listheader_rec.list_header_id;
7496       write_to_act_log(sql%rowcount||' entries disabled when generating list in update mode','LIST',g_list_header_id,'HIGH');
7497      IF x_return_status = FND_API.g_ret_sts_error THEN
7498         RAISE FND_API.g_exc_error;
7499      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
7500         RAISE FND_API.g_exc_unexpected_error;
7501      END IF;
7502 -- -----------------------
7503    else --  if g_remote_list = 'Y' then
7504       write_to_act_log('Updating list in remote instance.', 'LIST', p_list_header_id,'HIGH');
7505       execute immediate
7506       'BEGIN
7507        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';
7508 '||
7509       ' END;'
7510       using  '1',
7511              l_null,
7512              'T',
7513              l_null,
7514              OUT x_return_status,
7515              OUT x_msg_count,
7516              OUT x_msg_data,
7517              l_listheader_rec.list_header_id,
7518              l_null,
7519              l_null,
7520              OUT l_total_recs,
7521              'UPDATE';
7522         if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7523           write_to_act_log('Error in updating list in remote instance', 'LIST', g_list_header_id,'HIGH');
7524           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7525        end if;
7526      end if;
7527 
7528 -- -----------------------
7529   END IF;
7530 -- --------------------------------
7531 */
7532 if g_remote_list_gen = 'N' then
7533    if l_listheader_rec.generation_type = 'STANDARD' then --R12 applicable only for full refresh mode
7534       write_to_act_log('Identifying duplicate records(based on party id) in the list.','LIST',g_list_header_id,'HIGH');
7535        UPDATE ams_list_entries a
7536         SET a.enabled_flag  = 'N',
7537             a.marked_as_duplicate_flag = 'Y'
7538      WHERE a.list_header_id = p_list_header_id
7539        and a.enabled_flag = 'Y'
7540        AND a.rowid >  (SELECT min(b.rowid)
7541                    from ams_list_entries  b
7542                    where b.list_header_id = p_list_header_id
7543                      and b.party_id = a.party_id
7544                      and b.enabled_flag = 'Y'
7545                      and b.rank = a.rank
7546                    );
7547      write_to_act_log('Duplicate records(based on party) within the selections identified and marked.','LIST',g_list_header_id,'LOW');
7548      UPDATE ams_list_entries a
7549         SET a.enabled_flag  = 'N',
7550             a.marked_as_duplicate_flag = 'Y'
7551      WHERE a.list_header_id = p_list_header_id
7552        and a.enabled_flag = 'Y'
7553        -- AND a.rowid >  (SELECT min(b.rowid)
7554        AND a.rank >  (SELECT min(b.rank)
7555                    from ams_list_entries  b
7556                    where b.list_header_id = p_list_header_id
7557                      and b.party_id = a.party_id
7558                      and b.enabled_flag = 'Y'
7559                    );
7560      write_to_act_log('No of duplicates identified.'||sql%rowcount,'LIST',g_list_header_id,'HIGH');
7561      write_to_act_log('Duplicate records(based on party) for the entire list identified and marked.','LIST',g_list_header_id,'LOW');
7562 
7563    end if;
7564 end if;
7565 -- --------------------------------
7566 
7567    open g_initial_count;
7568    fetch g_initial_count into g_no_of_rows_ini_selected;
7569    close g_initial_count;
7570 
7571    if l_listheader_rec.generation_type = 'STANDARD' then --lpo
7572       open c1;
7573       fetch c1 into l_list_rule_id ;
7574       close c1;
7575 
7576       IF (l_list_rule_id <> 0 ) THEN   --Deduplication of the list has been requested.--
7577          write_to_act_log('De Duplication requested for this list', 'LIST', g_list_header_id,'HIGH');
7578          l_error_position := '<- de dupe ->';
7579          if g_remote_list_gen = 'N' then
7580             /* For local list generation */
7581             write_to_act_log('Calling ams_listdedupe_pvt for deduplication.', 'LIST', g_list_header_id,'HIGH');
7582             l_no_of_duplicates := AMS_LISTDEDUPE_PVT.DEDUPE_LIST
7583                            (p_list_header_id               => p_list_header_id,
7584                             p_enable_word_replacement_flag => 'Y',
7585                                   -- l_listheader_rec.enable_word_replacement_flag,
7586                             p_send_to_log    => l_listheader_rec.enable_log_flag,
7587                            p_object_name    => 'AMS_LIST_ENTRIES');
7588             write_to_act_log('Deduplication done for this list.', 'LIST', g_list_header_id,'HIGH');
7589          else
7590             /* For Remote list generation */
7591             write_to_act_log('Call Execute_Remote_Dedupe_List for deduplication in remote instance.', 'LIST', g_list_header_id,'HIGH');
7592             Execute_Remote_Dedupe_List
7593                           (p_list_header_id               => p_list_header_id,
7594                             p_enable_word_replacement_flag => 'Y',
7595                                 --   l_listheader_rec.enable_word_replacement_flag,
7596                             p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
7597                             p_object_name    => 'AMS_LIST_ENTRIES');
7598             write_to_act_log('Deduplication done for this list in remote instance.', 'LIST', g_list_header_id,'LOW');
7599          end if;
7600       end if;
7601    end if;
7602 
7603 
7604    if l_listheader_rec.generation_type in ('STANDARD','INCREMENTAL') then --lpo
7605       if nvl(g_remote_list_gen,'N') = 'N' then
7606          AMS_List_Options_Pvt.apply_size_reduction(p_list_header_id => g_list_header_id ,
7607                                                    p_log_level => g_log_level,
7608                                                    p_msg_tbl   => g_msg_tbl_opt,
7609                                                    x_return_status  => x_return_status,
7610                                                    x_msg_count      => x_msg_count,
7611                                                    x_msg_data       => x_msg_data);
7612          if g_msg_tbl_opt.count > 0 then
7613             for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
7614 	    loop
7615 	       write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
7616                --g_message_table(g_count) := g_msg_tbl_opt(I);
7617                --g_date(g_count) := sysdate;
7618                --g_count   := g_count + 1;
7619             end loop;
7620 	    g_msg_tbl_opt.delete;
7621          end if;
7622       else
7623          write_to_act_log('Calling apply_size_reduction procedure in the remote instance.', 'LIST', g_list_header_id,'LOW');
7624          execute immediate
7625          'BEGIN
7626           AMS_LIST_OPTIONS_PVT.apply_size_reduction'||'@'||g_database_link||'(:1,:2,:3,:4,:5)'||';'||
7627          ' END;'
7628          using g_list_header_id,
7629          'NULL',
7630          out x_return_status,
7631          out x_msg_count,
7632          out x_msg_data;
7633          write_to_act_log('x return status '||x_return_status, 'LIST', g_list_header_id,'LOW');
7634          write_to_act_log('apply_size_reduction procedure executed in the remote instance.', 'LIST', g_list_header_id,'LOW');
7635       end if;
7636       IF (x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
7637          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7638             write_to_act_log('Error in while executing size reduction procedure.', 'LIST', g_list_header_id,'HIGH');
7639          end if;
7640          RAISE FND_API.G_EXC_ERROR;
7641       ELSE
7642          write_to_act_log('Procedure apply_size_reduction executed successfully.', 'LIST', g_list_header_id,'LOW');
7643          IF FND_API.To_Boolean ( p_commit ) THEN
7644             COMMIT;
7645          END IF;
7646       END IF;
7647    end if;
7648 -- end if;
7649 
7650  if g_remote_list = 'Y' then
7651     write_to_act_log('Updating the list header info in the remote instance.', 'LIST', p_list_header_id,'HIGH');
7652     write_to_act_log('Deleting the existing ist header record deleted in remote instance.', 'LIST', p_list_header_id,'LOW');
7653     --execute immediate 'begin Delete from ams_list_headers_all'||'@'||g_database_link||' where list_header_id = :1 ; end;' using p_list_header_id;
7654 
7655 /*********** added by savio for remote bug 3764343 **************************/
7656 
7657 /*    open c10(p_list_header_id);
7658     fetch c10 into c10_rec;
7659     close c10;
7660 
7661    write_to_act_log('Passing list header details to the remote procedure, to insert it there.', 'LIST', p_list_header_id,'HIGH');
7662    execute immediate
7663       'begin
7664          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)'||';'||
7665       ' end;'
7666       using
7667     c10_rec.list_header_id,
7668     c10_rec.last_update_date,
7669     c10_rec.last_updated_by,
7670     c10_rec.creation_date,
7671     c10_rec.created_by,
7672     c10_rec.last_update_login,
7673     c10_rec.list_used_by_id,
7674     c10_rec.arc_list_used_by,
7675     c10_rec.list_type,
7676     c10_rec.status_code,
7677     c10_rec.status_date,
7678     c10_rec.generation_type,
7679     c10_rec.owner_user_id,
7680     c10_rec.row_selection_type,
7681     c10_rec.no_of_rows_max_requested,
7682     out x_msg_count,
7683     out x_msg_data,
7684     out x_return_status;
7685 
7686     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7687        write_to_act_log('Error updating list header information in remote instance.', 'LIST', g_list_header_id,'HIGH');
7688        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7689     else
7690        write_to_act_log('List header information updated.', 'LIST', g_list_header_id,'LOW');
7691     end if;*/
7692 
7693 /**********  added by savio for remote bug 3764343 *************************/
7694 
7695 /**********  added by savio for remote bug 3764343 ******************************************/
7696     open c11(p_list_header_id, l_action);
7697     fetch c11 into c11_rec;
7698     close c11;
7699     write_to_act_log('Updating the list selections in the remote instance.', 'LIST', p_list_header_id,'HIGH');
7700     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');
7701 
7702     execute immediate
7703       'begin
7704          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)'||';'||
7705       ' end;'
7706       using
7707     c11_rec.list_select_action_id,
7708     c11_rec.last_update_date,
7709     c11_rec.last_updated_by,
7710     c11_rec.creation_date,
7711     c11_rec.created_by,
7712     c11_rec.last_update_login,
7713     c11_rec.order_number,
7714     c11_rec.list_action_type,
7715     c11_rec.incl_object_name,
7716     c11_rec.arc_incl_object_from,
7717     c11_rec.rank,
7718     c11_rec.no_of_rows_available,
7719     c11_rec.no_of_rows_requested,
7720     c11_rec.no_of_rows_used,
7721     c11_rec.distribution_pct,
7722     c11_rec.arc_action_used_by,
7723     c11_rec.action_used_by_id,
7724     c11_rec.incl_control_group,
7725     c11_rec.no_of_rows_targeted,
7726     c11_rec.no_of_rows_duplicates,
7727     c11_rec.running_total,
7728     c11_rec.delta,
7729     out x_msg_count,
7730     out x_msg_data,
7731     out x_return_status;
7732 
7733     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7734        write_to_act_log('Error updating list selections in remote instance.', 'LIST', g_list_header_id,'HIGH');
7735        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7736     else
7737        write_to_act_log('List selections updated in remote instance.', 'LIST', g_list_header_id,'LOW');
7738     end if;
7739 
7740 /**********  added by savio for remote bug 3764343 ******************************************/
7741     execute immediate 'begin Update ams_list_select_actions a1  set no_of_rows_requested =
7742             (select  no_of_rows_requested from ams_list_select_actions'||'@'||g_database_link||
7743 	    ' b1 where b1.list_select_action_id = a1.list_select_action_id)
7744 	    where action_used_by_id = :1 and arc_action_used_by = :2; end; ' using p_list_header_id, l_action;
7745     write_to_act_log('No_of_rows_requested in list header table updated with values from remote instance.', 'LIST', p_list_header_id,'LOW');
7746 
7747   end if;
7748 
7749   l_error_position := '<- update list dets ->';
7750 
7751   write_to_act_log('Calling update_list_dets to update list header and selections info.'||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7752   Update_List_Dets(p_list_header_id,x_return_status);
7753 
7754   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
7755      write_to_act_log('Error in updating list header/selections info', 'LIST', g_list_header_id,'HIGH');
7756      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
7757   elsif nvl(x_return_status,'S') = 'S' then
7758      write_to_act_log('List header and selections info updated.'||x_msg_data , 'LIST', g_list_header_id,'LOW');
7759   end if;
7760 
7761   IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
7762      write_to_act_log('Error after calling update_list_dets procedure', 'LIST', p_list_header_id,'HIGH');
7763      RAISE FND_API.G_EXC_ERROR;
7764   ELSE
7765    IF FND_API.To_Boolean ( p_commit ) THEN
7766      write_to_act_log('Generate_list : Commit', 'LIST', g_list_header_id,'LOW');
7767      COMMIT;
7768    END IF;
7769   END IF;
7770   write_to_act_log('Procedure generate_list executed. List generated successfully.','LIST',g_list_header_id,'HIGH');
7771   -- calling logging program
7772   logger;
7773   --
7774   -- END of API body.
7775   --
7776 /******************************************************************************/
7777 /**************** call for cancel list generation added 05/26/2005 ************/
7778 /******************************************************************************/
7779 --inside generate_list 2
7780 
7781    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
7782                 p_list_header_id => g_list_header_id ,
7783                 p_remote_gen     => g_remote_list    ,
7784                 p_remote_gen_list=> g_remote_list_gen,
7785                 p_database_link  => g_database_link,
7786                 x_msg_count      => x_msg_count ,
7787                 x_msg_data       => x_msg_data ,
7788                 x_return_status  => x_return_status
7789                );
7790 
7791   logger;
7792   -- Standard check of p_commit.
7793 
7794   IF FND_API.To_Boolean ( p_commit ) THEN
7795      COMMIT WORK;
7796   END IF;
7797 
7798   -- Success Message
7799   -- MMSG
7800   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
7801   --THEN
7802   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
7803   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
7804   FND_MSG_PUB.Add;
7805   --END IF;
7806 
7807   x_return_status := FND_API.G_RET_STS_SUCCESS;
7808   --IF (AMS_DEBUG_HIGH_ON) THEN
7809   --THEN
7810   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7811   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.Generate_List: END');
7812   FND_MSG_PUB.Add;
7813   --END IF;
7814       FND_MSG_PUB.count_and_get(
7815             p_encoded => FND_API.g_false,
7816             p_count   => x_msg_count,
7817             p_data    => x_msg_data
7818       );
7819 
7820 EXCEPTION
7821    WHEN FND_API.G_EXC_ERROR THEN
7822   -- calling logging program
7823   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7824 
7825   --
7826         UPDATE ams_list_headers_all
7827         SET    last_generation_success_flag = 'N',
7828                status_code         = 'FAILED',
7829                user_status_id      = 311,
7830                status_date         = sysdate,
7831                last_update_date    = sysdate,
7832                main_gen_end_time   = sysdate
7833         WHERE  list_header_id      = p_list_header_id;
7834      logger;
7835      -- Check if reset of the status is required
7836      x_return_status := FND_API.G_RET_STS_ERROR ;
7837       FND_MSG_PUB.count_and_get(
7838             p_encoded => FND_API.g_false,
7839             p_count   => x_msg_count,
7840             p_data    => x_msg_data);
7841 
7842   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7843   -- calling logging program
7844   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7845   --
7846         UPDATE ams_list_headers_all
7847         SET    last_generation_success_flag = 'N',
7848                status_code         = 'FAILED',
7849                user_status_id      = 311,
7850                last_update_date    = sysdate,
7851                status_date         = sysdate,
7852                main_gen_end_time   = sysdate
7853         WHERE  list_header_id      = p_list_header_id;
7854      logger;
7855      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7856      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
7857      FND_MSG_PUB.Add;
7858      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7859       FND_MSG_PUB.count_and_get(
7860             p_encoded => FND_API.g_false,
7861             p_count   => x_msg_count,
7862             p_data    => x_msg_data);
7863 ---------------Cancel List Gen Exception Begin --------------
7864   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
7865     write_to_act_log('In GENERATE_LIST: User cancel list gen action detected. ' ,'LIST',g_list_header_id,'HIGH');
7866     write_to_act_log('List Generation Stopped successfully.','LIST',g_list_header_id,'HIGH');
7867 
7868   UPDATE ams_list_headers_all
7869      SET    last_generation_success_flag = 'N',
7870                status_code         = 'DRAFT',
7871                user_status_id      = 300,
7872                last_update_date    = sysdate,
7873                status_date         = sysdate,
7874                main_gen_end_time   = sysdate
7875      WHERE  list_header_id         = p_list_header_id;
7876   logger;
7877 
7878   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
7879   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
7880   FND_MSG_PUB.Add;
7881 
7882   x_return_status := FND_API.G_RET_STS_SUCCESS;
7883 
7884   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7885   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.Generate_List: END');
7886   FND_MSG_PUB.Add;
7887 
7888   FND_MSG_PUB.count_and_get(
7889             p_encoded => FND_API.g_false,
7890             p_count   => x_msg_count,
7891             p_data    => x_msg_data
7892       );
7893 ---------------Cancel List Gen Exception End --------------
7894 
7895   WHEN OTHERS THEN
7896   -- calling logging program
7897   write_to_act_log('Error in generating list while executing procedure generate_list '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
7898   --
7899         UPDATE ams_list_headers_all
7900         SET    last_generation_success_flag = 'N',
7901                status_code                  = 'FAILED',
7902                user_status_id               = 311,
7903                last_update_date             = sysdate,
7904                status_date                  = sysdate,
7905                main_gen_end_time            = sysdate
7906         WHERE  list_header_id               = p_list_header_id;
7907       logger;
7908      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7909      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
7910      FND_MSG_PUB.Add;
7911      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7912       FND_MSG_PUB.count_and_get(
7913             p_encoded => FND_API.g_false,
7914             p_count   => x_msg_count,
7915             p_data    => x_msg_data);
7916 END GENERATE_LIST;
7917 
7918 PROCEDURE create_list
7919 ( p_api_version            IN    NUMBER,
7920   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
7921   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
7922   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
7923   p_list_name              in    varchar2,
7924   p_list_type              in    varchar2,
7925   p_owner_user_id          in    number,
7926   p_sql_string             in  OUT NOCOPY varchar2,
7927   p_primary_key            in    varchar2,
7928   p_source_object_name     in    varchar2,
7929   p_master_type            in    varchar2,
7930   x_return_status          OUT NOCOPY   VARCHAR2,
7931   x_msg_count              OUT NOCOPY   NUMBER,
7932   x_msg_data               OUT NOCOPY   VARCHAR2,
7933   x_list_header_id         OUT NOCOPY   NUMBER  ) is
7934 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
7935 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
7936 l_api_version       number         := 1.0;
7937 l_api_name          constant varchar2(30) := 'Create_List';
7938 l_list_query_rec    AMS_List_Query_PVT.list_query_rec_type ;
7939 l_list_query_id     number ;
7940 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
7941 l_action_id         number;
7942 cursor c_mapping_types(p_master_type varchar2) is
7943 SELECT list_source_type_id
7944 FROM   ams_list_src_types a
7945 WHERE a.source_type_code = p_master_type
7946   AND a.master_source_type_flag = 'Y';
7947 cursor c_mapping_subtypes(p_master_type_id
7948                           ams_list_src_type_assocs.master_source_type_id%type)is
7949 select ','||''''||source_type_code||''''
7950 from   ams_list_src_types a,
7951        ams_list_src_type_assocs b
7952 where  b.master_source_type_id = p_master_type_id
7953   and  b.sub_source_type_id  = a.list_source_type_id;
7954 
7955 l_master_type_id number;
7956 l_source_type_code varchar2(30);
7957 l_select_string varchar2(2000) := 'SELECT ' ||''''|| p_master_type || '''';
7958 BEGIN
7959   open c_mapping_types(p_master_type )  ;
7960   fetch c_mapping_types into l_master_type_id;
7961   close c_mapping_types;
7962 /*
7963   open c_mapping_subtypes(l_master_type_id )  ;
7964   loop
7965   fetch c_mapping_subtypes
7966   into l_source_type_code;
7967   exit when c_mapping_subtypes%notfound;
7968   l_select_string := l_select_string || l_source_type_code;
7969   end loop;
7970   close c_mapping_subtypes;
7971 */
7972 
7973   p_sql_string := l_select_string ||','|| substrb(p_sql_string,instr(upper(p_sql_string),'SELECT',1)+6);
7974   x_return_status := FND_API.G_RET_STS_SUCCESS;
7975   -- Standard call to check for call compatibility.
7976   IF NOT FND_API.Compatible_API_Call ( l_api_version,
7977                                        p_api_version,
7978                                        l_api_name,
7979                                        G_PKG_NAME) THEN
7980      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7981   END IF;
7982 
7983 
7984   -- Initialize message list IF p_init_msg_list is set to TRUE.
7985   IF FND_API.to_Boolean( p_init_msg_list ) THEN
7986      FND_MSG_PUB.initialize;
7987   END IF;
7988 
7989   -- Debug Message
7990   IF (AMS_DEBUG_HIGH_ON) THEN
7991      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
7992      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
7993      FND_MSG_PUB.Add;
7994   END IF;
7995 
7996   --  Initialize API return status to success
7997   x_return_status := FND_API.G_RET_STS_SUCCESS;
7998   -- Perform the database operation
7999 
8000   -- ams_listheader_pvt.init_listheader_rec(l_list_header_rec);
8001   l_list_header_rec.list_name :=  p_list_name;
8002   l_list_header_rec.list_type :=  p_list_type;
8003   l_list_header_rec.owner_user_id :=  p_owner_user_id;
8004 l_list_header_rec.list_source_type := p_master_type            ;
8005 if l_list_header_rec.view_application_id is null then
8006 l_list_header_rec.view_application_id := 530;
8007 end if;
8008   AMS_ListHeader_PVT.Create_Listheader
8009   ( p_api_version           => 1.0,
8010   p_init_msg_list           => l_init_msg_list,
8011   p_commit                  => p_commit,
8012   p_validation_level        => p_validation_level ,
8013   x_return_status           => x_return_status,
8014   x_msg_count               => x_msg_count,
8015   x_msg_data                => x_msg_data,
8016   p_listheader_rec          => l_list_header_rec,
8017   x_listheader_id           => x_list_header_id
8018   );
8019 
8020   if x_return_status <> FND_API.g_ret_sts_success  THEN
8021      RAISE FND_API.G_EXC_ERROR;
8022   end if;
8023 
8024      FND_MESSAGE.set_name('AMS', 'bfore list query');
8025      FND_MSG_PUB.Add;
8026    l_list_query_rec.name        :=  p_list_name || x_list_header_id;
8027    l_list_query_rec.sql_string  :=  p_sql_string ;
8028    l_list_query_rec.primary_key  :=  p_primary_key ;
8029    l_list_query_rec.type  :=  p_master_type ;
8030    l_list_query_rec.source_object_name  :=  p_source_object_name ;
8031    AMS_List_Query_PVT.Create_List_Query(
8032        p_api_version_number  => 1.0,
8033        p_init_msg_list       => l_init_msg_list,
8034        p_commit              => p_commit,
8035        p_validation_level    => p_validation_level,
8036        x_return_status       => x_return_status,
8037        x_msg_count           => x_msg_count,
8038        x_msg_data            => x_msg_data,
8039        p_list_query_rec      => l_list_query_rec ,
8040        x_list_query_id       => l_list_query_id
8041      );
8042   if x_return_status <> FND_API.g_ret_sts_success  THEN
8043      RAISE FND_API.G_EXC_ERROR;
8044   end if;
8045 
8046   l_action_rec.arc_action_used_by := 'LIST';
8047   l_action_rec.action_used_by_id := x_list_header_id ;
8048   l_action_rec.order_number := 1 ;
8049   l_action_rec.list_action_type := 'INCLUDE';
8050   l_action_rec.arc_incl_object_from := 'SQL';
8051   l_action_rec.incl_object_id := l_list_query_id;
8052   l_action_rec.rank := 1;
8053   AMS_ListAction_PVT.Create_ListAction
8054   ( p_api_version           => 1.0,
8055     p_init_msg_list         => l_init_msg_list,
8056     p_commit                => p_commit,
8057     p_validation_level      => p_validation_level,
8058     x_return_status         => x_return_status,
8059     x_msg_count             => x_msg_count,
8060     x_msg_data              => x_msg_data,
8061     p_action_rec            => l_action_rec,
8062     x_action_id             => l_action_id
8063     ) ;
8064   if x_return_status <> FND_API.g_ret_sts_success  THEN
8065      RAISE FND_API.G_EXC_ERROR;
8066   end if;
8067   --For bug 4351391
8068     ams_list_wf.StartListBizEventProcess(p_list_header_id  => x_list_header_id);
8069 
8070   /*
8071   GENERATE_LIST
8072   ( p_api_version           => 1.0,
8073     p_init_msg_list         => l_init_msg_list,
8074     p_commit                => p_commit,
8075     p_validation_level      => p_validation_level,
8076     p_list_header_id        => x_list_header_id,
8077     x_return_status         => x_return_status,
8078     x_msg_count             => x_msg_count,
8079     x_msg_data              => x_msg_data);
8080 
8081   if x_return_status <> FND_API.g_ret_sts_success  THEN
8082      RAISE FND_API.G_EXC_ERROR;
8083   end if;
8084   -- END of API body.
8085 
8086   if x_return_status <> FND_API.g_ret_sts_success  THEN
8087      RAISE FND_API.G_EXC_ERROR;
8088   end if;
8089   -- END of API body.
8090   --
8091   */
8092   -- Standard check of p_commit.
8093 
8094   IF FND_API.To_Boolean ( p_commit ) THEN
8095      COMMIT WORK;
8096   END IF;
8097 
8098   -- Success Message
8099   -- MMSG
8100   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8101   --THEN
8102   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8103   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8104   FND_MSG_PUB.Add;
8105   --END IF;
8106 
8107   --IF (AMS_DEBUG_HIGH_ON) THEN
8108   --THEN
8109   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8110   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8111   FND_MSG_PUB.Add;
8112   --END IF;
8113       FND_MSG_PUB.count_and_get(
8114             p_encoded => FND_API.g_false,
8115             p_count   => x_msg_count,
8116             p_data    => x_msg_data
8117       );
8118 
8119 EXCEPTION
8120    WHEN FND_API.G_EXC_ERROR THEN
8121      -- Check if reset of the status is required
8122      x_return_status := FND_API.G_RET_STS_ERROR ;
8123       FND_MSG_PUB.count_and_get(
8124             p_encoded => FND_API.g_false,
8125             p_count   => x_msg_count,
8126             p_data    => x_msg_data);
8127 
8128   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8129      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8130       FND_MSG_PUB.count_and_get(
8131             p_encoded => FND_API.g_false,
8132             p_count   => x_msg_count,
8133             p_data    => x_msg_data);
8134 
8135   WHEN OTHERS THEN
8136      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8137       FND_MSG_PUB.count_and_get(
8138             p_encoded => FND_API.g_false,
8139             p_count   => x_msg_count,
8140             p_data    => x_msg_data);
8141 END CREATE_LIST;
8142 
8143 PROCEDURE create_import_list
8144 ( p_api_version            IN    NUMBER,
8145   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8146   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8147   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8148   p_owner_user_id          in    number,
8149   p_imp_list_header_id     in    number,
8150   x_return_status          OUT NOCOPY   VARCHAR2,
8151   x_msg_count              OUT NOCOPY   NUMBER,
8152   x_msg_data               OUT NOCOPY   VARCHAR2,
8153   x_list_header_id         OUT NOCOPY   NUMBER  ,
8154   p_list_name              in    VARCHAR2 ) is
8155 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8156 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8157 l_api_version       number         := 1.0;
8158 l_api_name          constant varchar2(30) := 'Create_import_List';
8159 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8160 l_action_id         number;
8161 l_import_list_name  varchar2(200);
8162 cursor c_chk_name is
8163 select  'x'
8164 from ams_list_headers_vl
8165 where list_name = p_list_name ;
8166 cursor c_get_source_type
8167 is select  decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST'),
8168       name
8169 from ams_imp_list_headers_vl
8170 where  import_list_header_id = p_imp_list_header_id     ;
8171 l_source_type varchar2(100);
8172 l_var  varchar2(1);
8173 BEGIN
8174  open c_get_source_type ;
8175  fetch c_get_source_type into l_source_type ,l_import_list_name   ;
8176  close c_get_source_type ;
8177  if l_source_type is null then
8178    l_source_type := 'PERSON_LIST';
8179  end if;
8180   if p_list_name is not null then
8181      open c_chk_name ;
8182      fetch c_chk_name into l_var   ;
8183      close c_chk_name ;
8184   else
8185      l_var := 'x';
8186   end if;
8187   if  l_var is not null  then
8188       select l_import_list_name|| ' -:'|| to_char(sysdate,'DD-MON-YY HH:MM:SS')
8189       into l_import_list_name
8190       from ams_imp_list_headers_vl
8191       where import_list_header_id = p_imp_list_header_id     ;
8192  else
8193     l_import_list_name := p_list_name ;
8194  end if;
8195 
8196   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8197                                        p_api_version,
8198                                        l_api_name,
8199                                        G_PKG_NAME) THEN
8200      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8201   END IF;
8202 
8203 
8204   -- Initialize message list IF p_init_msg_list is set to TRUE.
8205   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8206      FND_MSG_PUB.initialize;
8207   END IF;
8208 
8209   -- Debug Message
8210   IF (AMS_DEBUG_HIGH_ON) THEN
8211      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8212      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8213      FND_MSG_PUB.Add;
8214   END IF;
8215   --  Initialize API return status to success
8216   x_return_status := FND_API.G_RET_STS_SUCCESS;
8217   -- Perform the database operation
8218 
8219   -- ams_listheader_pvt.init_listheader_rec(l_list_header_rec);
8220   l_list_header_rec.list_name :=  l_import_list_name  ;
8221   l_list_header_rec.list_type :=  'STANDARD';
8222   l_list_header_rec.list_source_type :=  l_source_type;
8223   l_list_header_rec.owner_user_id :=  p_owner_user_id;
8224   l_list_header_rec.view_application_id :=  530;
8225 --  l_list_header_rec.purpose_code :=  'GENERAL';
8226 
8227   AMS_ListHeader_PVT.Create_Listheader
8228   ( p_api_version           => 1.0,
8229   p_init_msg_list           => l_init_msg_list,
8230   p_commit                  => p_commit,
8231   p_validation_level        => p_validation_level ,
8232   x_return_status           => x_return_status,
8233   x_msg_count               => x_msg_count,
8234   x_msg_data                => x_msg_data,
8235   p_listheader_rec          => l_list_header_rec,
8236   x_listheader_id           => x_list_header_id
8237   );
8238 
8239   if x_return_status <> FND_API.g_ret_sts_success  THEN
8240      RAISE FND_API.G_EXC_ERROR;
8241   end if;
8242 
8243   l_action_rec.arc_action_used_by := 'LIST';
8244   l_action_rec.action_used_by_id := x_list_header_id ;
8245   l_action_rec.order_number := 1 ;
8246   l_action_rec.list_action_type := 'INCLUDE';
8247   l_action_rec.arc_incl_object_from := 'IMPH';
8248   l_action_rec.incl_object_id := p_imp_list_header_id     ;
8249   l_action_rec.rank := 1;
8250   AMS_ListAction_PVT.Create_ListAction
8251   ( p_api_version           => 1.0,
8252     p_init_msg_list         => l_init_msg_list,
8253     p_commit                => p_commit,
8254     p_validation_level      => p_validation_level,
8255     x_return_status         => x_return_status,
8256     x_msg_count             => x_msg_count,
8257     x_msg_data              => x_msg_data,
8258     p_action_rec            => l_action_rec,
8259     x_action_id             => l_action_id
8260     ) ;
8261      FND_MESSAGE.set_name('AMS','after list action->'|| l_action_id|| '<-');
8262      FND_MSG_PUB.Add;
8263   if x_return_status <> FND_API.g_ret_sts_success  THEN
8264      RAISE FND_API.G_EXC_ERROR;
8265   end if;
8266 
8267   GENERATE_LIST
8268   ( p_api_version           => 1.0,
8269     p_init_msg_list         => l_init_msg_list,
8270     p_commit                => p_commit,
8271     p_validation_level      => p_validation_level,
8272     p_list_header_id        => x_list_header_id,
8273     x_return_status         => x_return_status,
8274     x_msg_count             => x_msg_count,
8275     x_msg_data              => x_msg_data);
8276 
8277   if x_return_status <> FND_API.g_ret_sts_success  THEN
8278      RAISE FND_API.G_EXC_ERROR;
8279   end if;
8280   -- END of API body.
8281   --
8282 
8283   -- Standard check of p_commit.
8284 
8285   IF FND_API.To_Boolean ( p_commit ) THEN
8286      COMMIT WORK;
8287   END IF;
8288 
8289   -- Success Message
8290   -- MMSG
8291   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8292   --THEN
8293   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8294   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8295   FND_MSG_PUB.Add;
8296   --END IF;
8297 
8298 
8299   --IF (AMS_DEBUG_HIGH_ON) THEN
8300   --THEN
8301   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8302   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8303   FND_MSG_PUB.Add;
8304   --END IF;
8305       FND_MSG_PUB.count_and_get(
8306             p_encoded => FND_API.g_false,
8307             p_count   => x_msg_count,
8308             p_data    => x_msg_data
8309       );
8310 
8311 EXCEPTION
8312    WHEN FND_API.G_EXC_ERROR THEN
8313      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8314      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8315      FND_MSG_PUB.Add;
8316      -- Check if reset of the status is required
8317      x_return_status := FND_API.G_RET_STS_ERROR ;
8318       FND_MSG_PUB.count_and_get(
8319             p_encoded => FND_API.g_false,
8320             p_count   => x_msg_count,
8321             p_data    => x_msg_data);
8322 
8323   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8324      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8325      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8326      FND_MSG_PUB.Add;
8327      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8328       FND_MSG_PUB.count_and_get(
8329             p_encoded => FND_API.g_false,
8330             p_count   => x_msg_count,
8331             p_data    => x_msg_data);
8332 
8333   WHEN OTHERS THEN
8334      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8335      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8336      FND_MSG_PUB.Add;
8337      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8338       FND_MSG_PUB.count_and_get(
8339             p_encoded => FND_API.g_false,
8340             p_count   => x_msg_count,
8341             p_data    => x_msg_data);
8342 END CREATE_import_LIST;
8343 
8344 PROCEDURE create_list_from_query
8345 ( p_api_version            IN    NUMBER,
8346   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8347   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8348   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8349   p_list_name              in    varchar2,
8350   p_list_type              in    varchar2,
8351   p_owner_user_id          in    number,
8352   p_list_header_id         in    number,
8353   p_sql_string_tbl         in    AMS_List_Query_PVT.sql_string_tbl      ,
8354   p_primary_key            in    varchar2,
8355   p_source_object_name     in    varchar2,
8356   p_master_type            in    varchar2,
8357   x_return_status          OUT NOCOPY   VARCHAR2,
8358   x_msg_count              OUT NOCOPY   NUMBER,
8359   x_msg_data               OUT NOCOPY   VARCHAR2
8360   ) is
8361 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8362 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8363 l_api_version       number         := 1.0;
8364 l_api_name          constant varchar2(30) := 'Create_List';
8365 l_list_query_rec_tbl    AMS_List_Query_PVT.list_query_rec_type_tbl ;
8366 l_list_query_id     number ;
8367 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8368 l_action_id         number;
8369 
8370 l_master_type_id number;
8371 l_source_type_code varchar2(30);
8372 BEGIN
8373   x_return_status := FND_API.G_RET_STS_SUCCESS;
8374   -- Standard call to check for call compatibility.
8375   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8376                                        p_api_version,
8377                                        l_api_name,
8378                                        G_PKG_NAME) THEN
8379      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8380   END IF;
8381 
8382 
8383   -- Initialize message list IF p_init_msg_list is set to TRUE.
8384   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8385      FND_MSG_PUB.initialize;
8386   END IF;
8387 
8388   -- Debug Message
8389   IF (AMS_DEBUG_HIGH_ON) THEN
8390      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8391      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8392      FND_MSG_PUB.Add;
8393   END IF;
8394 
8395   --  Initialize API return status to success
8396   x_return_status := FND_API.G_RET_STS_SUCCESS;
8397   -- Perform the database operation
8398 
8399    l_list_query_rec_tbl.name        :=  p_list_name ||'_'|| p_list_header_id;
8400   -- l_list_query_rec_tbl.sql_string  :=  p_sql_string ;
8401    l_list_query_rec_tbl.primary_key  :=  p_primary_key ;
8402    l_list_query_rec_tbl.type  :=  p_master_type ;
8403    l_list_query_rec_tbl.source_object_name  :=  p_source_object_name ;
8404    AMS_List_Query_PVT.Create_List_Query(
8405        p_api_version_number  => 1.0,
8406        p_init_msg_list       => l_init_msg_list,
8407        p_commit              => p_commit,
8408        p_validation_level    => p_validation_level,
8409        x_return_status       => x_return_status,
8410        x_msg_count           => x_msg_count,
8411        x_msg_data            => x_msg_data,
8412        p_list_query_rec_tbl    => l_list_query_rec_tbl ,
8413        p_sql_string_tbl       => p_sql_string_tbl         ,
8414        x_parent_list_query_id   => l_list_query_id
8415      );
8416 
8417 
8418   if x_return_status <> FND_API.g_ret_sts_success  THEN
8419      RAISE FND_API.G_EXC_ERROR;
8420   end if;
8421 
8422   l_action_rec.arc_action_used_by := 'LIST';
8423   l_action_rec.action_used_by_id := p_list_header_id ;
8424   l_action_rec.order_number := 1 ;
8425   l_action_rec.list_action_type := 'INCLUDE';
8426   l_action_rec.arc_incl_object_from := 'SQL';
8427   l_action_rec.incl_object_id := l_list_query_id;
8428   l_action_rec.rank := 1;
8429   l_action_rec.order_number := 1 ;
8430   AMS_ListAction_PVT.Create_ListAction
8431   ( p_api_version           => 1.0,
8432     p_init_msg_list         => l_init_msg_list,
8433     p_commit                => p_commit,
8434     p_validation_level      => p_validation_level,
8435     x_return_status         => x_return_status,
8436     x_msg_count             => x_msg_count,
8437     x_msg_data              => x_msg_data,
8438     p_action_rec            => l_action_rec,
8439     x_action_id             => l_action_id
8440     ) ;
8441   if x_return_status <> FND_API.g_ret_sts_success  THEN
8442      RAISE FND_API.G_EXC_ERROR;
8443   end if;
8444 
8445   GENERATE_LIST
8446   ( p_api_version           => 1.0,
8447     p_init_msg_list         => l_init_msg_list,
8448     p_commit                => p_commit,
8449     p_validation_level      => p_validation_level,
8450     p_list_header_id        => p_list_header_id,
8451     x_return_status         => x_return_status,
8452     x_msg_count             => x_msg_count,
8453     x_msg_data              => x_msg_data);
8454 
8455   if x_return_status <> FND_API.g_ret_sts_success  THEN
8456      RAISE FND_API.G_EXC_ERROR;
8457   end if;
8458   -- END of API body.
8459   --
8460 
8461   -- Standard check of p_commit.
8462 
8463   IF FND_API.To_Boolean ( p_commit ) THEN
8464      COMMIT WORK;
8465   END IF;
8466 
8467   -- Success Message
8468   -- MMSG
8469   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8470   --THEN
8471   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8472   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8473   FND_MSG_PUB.Add;
8474   --END IF;
8475 
8476 
8477   --IF (AMS_DEBUG_HIGH_ON) THEN
8478   --THEN
8479   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8480   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8481   FND_MSG_PUB.Add;
8482   --END IF;
8483       FND_MSG_PUB.count_and_get(
8484             p_encoded => FND_API.g_false,
8485             p_count   => x_msg_count,
8486             p_data    => x_msg_data
8487       );
8488 
8489 EXCEPTION
8490    WHEN FND_API.G_EXC_ERROR THEN
8491      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8492      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8493      FND_MSG_PUB.Add;
8494      -- Check if reset of the status is required
8495      x_return_status := FND_API.G_RET_STS_ERROR ;
8496       FND_MSG_PUB.count_and_get(
8497             p_encoded => FND_API.g_false,
8498             p_count   => x_msg_count,
8499             p_data    => x_msg_data);
8500 
8501   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8502      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8503      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8504      FND_MSG_PUB.Add;
8505      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8506       FND_MSG_PUB.count_and_get(
8507             p_encoded => FND_API.g_false,
8508             p_count   => x_msg_count,
8509             p_data    => x_msg_data);
8510 
8511   WHEN OTHERS THEN
8512      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8513      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8514      FND_MSG_PUB.Add;
8515      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8516       FND_MSG_PUB.count_and_get(
8517             p_encoded => FND_API.g_false,
8518             p_count   => x_msg_count,
8519             p_data    => x_msg_data);
8520 END CREATE_LIST_FROM_QUERY ;
8521 
8522 PROCEDURE create_list_from_query
8523 ( p_api_version            IN    NUMBER,
8524   p_init_msg_list          IN    VARCHAR2   := FND_API.G_TRUE,
8525   p_commit                 IN    VARCHAR2   := FND_API.G_FALSE,
8526   p_validation_level       IN    NUMBER     := FND_API.G_VALID_LEVEL_FULL,
8527   p_list_name              in    varchar2,
8528   p_list_type              in    varchar2,
8529   p_owner_user_id          in    number,
8530   p_list_header_id         in    number,
8531   p_sql_string_tbl         in    AMS_List_Query_PVT.sql_string_tbl      ,
8532   p_primary_key            in    varchar2,
8533   p_source_object_name     in    varchar2,
8534   p_master_type            in    varchar2,
8535   p_query_param            in    AMS_List_Query_PVT.sql_string_tbl      ,
8536   x_return_status          OUT NOCOPY   VARCHAR2,
8537   x_msg_count              OUT NOCOPY   NUMBER,
8538   x_msg_data               OUT NOCOPY   VARCHAR2
8539   ) is
8540 l_list_header_rec   AMS_ListHeader_PVT.list_header_rec_type;
8541 l_init_msg_list     varchar2(2000) := FND_API.G_FALSE;
8542 l_api_version       number         := 1.0;
8543 l_api_name          constant varchar2(30) := 'Create_List';
8544 l_list_query_rec_tbl    AMS_List_Query_PVT.list_query_rec_type_tbl ;
8545 l_list_query_id     number ;
8546 l_action_rec        AMS_ListAction_PVT.action_rec_type ;
8547 l_action_id         number;
8548 
8549 l_master_type_id number;
8550 l_source_type_code varchar2(30);
8551 BEGIN
8552   x_return_status := FND_API.G_RET_STS_SUCCESS;
8553   -- Standard call to check for call compatibility.
8554   IF NOT FND_API.Compatible_API_Call ( l_api_version,
8555                                        p_api_version,
8556                                        l_api_name,
8557                                        G_PKG_NAME) THEN
8558      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
8559   END IF;
8560 
8561 
8562   -- Initialize message list IF p_init_msg_list is set to TRUE.
8563   IF FND_API.to_Boolean( p_init_msg_list ) THEN
8564      FND_MSG_PUB.initialize;
8565   END IF;
8566 
8567   -- Debug Message
8568   IF (AMS_DEBUG_HIGH_ON) THEN
8569      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8570      FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.cerate_list: Start', TRUE);
8571      FND_MSG_PUB.Add;
8572   END IF;
8573 
8574   --  Initialize API return status to success
8575   x_return_status := FND_API.G_RET_STS_SUCCESS;
8576   -- Perform the database operation
8577 
8578    l_list_query_rec_tbl.name        :=  p_list_name ||'_'|| p_list_header_id;
8579   -- l_list_query_rec_tbl.sql_string  :=  p_sql_string ;
8580    l_list_query_rec_tbl.primary_key  :=  p_primary_key ;
8581    l_list_query_rec_tbl.type  :=  p_master_type ;
8582    l_list_query_rec_tbl.source_object_name  :=  p_source_object_name ;
8583 
8584    AMS_List_Query_PVT.Create_List_Query(
8585        p_api_version_number  => 1.0,
8586        p_init_msg_list       => l_init_msg_list,
8587        p_commit              => p_commit,
8588        p_validation_level    => p_validation_level,
8589        x_return_status       => x_return_status,
8590        x_msg_count           => x_msg_count,
8591        x_msg_data            => x_msg_data,
8592        p_list_query_rec_tbl    => l_list_query_rec_tbl ,
8593        p_sql_string_tbl       => p_sql_string_tbl         ,
8594        p_query_param          => p_query_param          ,
8595        x_parent_list_query_id   => l_list_query_id
8596      );
8597 
8598 
8599   if x_return_status <> FND_API.g_ret_sts_success  THEN
8600      RAISE FND_API.G_EXC_ERROR;
8601   end if;
8602 
8603   l_action_rec.arc_action_used_by := 'LIST';
8604   l_action_rec.action_used_by_id := p_list_header_id ;
8605   l_action_rec.order_number := 1 ;
8606   l_action_rec.list_action_type := 'INCLUDE';
8607   l_action_rec.arc_incl_object_from := 'SQL';
8608   l_action_rec.incl_object_id := l_list_query_id;
8609   l_action_rec.rank := 1;
8610   l_action_rec.order_number := 1 ;
8611   AMS_ListAction_PVT.Create_ListAction
8612   ( p_api_version           => 1.0,
8613     p_init_msg_list         => l_init_msg_list,
8614     p_commit                => p_commit,
8615     p_validation_level      => p_validation_level,
8616     x_return_status         => x_return_status,
8617     x_msg_count             => x_msg_count,
8618     x_msg_data              => x_msg_data,
8619     p_action_rec            => l_action_rec,
8620     x_action_id             => l_action_id
8621     ) ;
8622   if x_return_status <> FND_API.g_ret_sts_success  THEN
8623      RAISE FND_API.G_EXC_ERROR;
8624   end if;
8625 
8626   GENERATE_LIST
8627   ( p_api_version           => 1.0,
8628     p_init_msg_list         => l_init_msg_list,
8629     p_commit                => p_commit,
8630     p_validation_level      => p_validation_level,
8631     p_list_header_id        => p_list_header_id,
8632     x_return_status         => x_return_status,
8633     x_msg_count             => x_msg_count,
8634     x_msg_data              => x_msg_data);
8635 
8636   if x_return_status <> FND_API.g_ret_sts_success  THEN
8637      RAISE FND_API.G_EXC_ERROR;
8638   end if;
8639   -- END of API body.
8640   --
8641 
8642   -- Standard check of p_commit.
8643 
8644   IF FND_API.To_Boolean ( p_commit ) THEN
8645      COMMIT WORK;
8646   END IF;
8647 
8648   -- Success Message
8649   -- MMSG
8650   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
8651   --THEN
8652   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
8653   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.create_list: ');
8654   FND_MSG_PUB.Add;
8655   --END IF;
8656 
8657 
8658   --IF (AMS_DEBUG_HIGH_ON) THEN
8659   --THEN
8660   FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8661   FND_MESSAGE.Set_Token('TEXT', 'AMS_ListGeneration_PKG.create_list: END');
8662   FND_MSG_PUB.Add;
8663   --END IF;
8664       FND_MSG_PUB.count_and_get(
8665             p_encoded => FND_API.g_false,
8666             p_count   => x_msg_count,
8667             p_data    => x_msg_data
8668       );
8669 
8670 EXCEPTION
8671    WHEN FND_API.G_EXC_ERROR THEN
8672      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8673      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8674      FND_MSG_PUB.Add;
8675      -- Check if reset of the status is required
8676      x_return_status := FND_API.G_RET_STS_ERROR ;
8677       FND_MSG_PUB.count_and_get(
8678             p_encoded => FND_API.g_false,
8679             p_count   => x_msg_count,
8680             p_data    => x_msg_data);
8681 
8682   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8683      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8684      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8685      FND_MSG_PUB.Add;
8686      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8687       FND_MSG_PUB.count_and_get(
8688             p_encoded => FND_API.g_false,
8689             p_count   => x_msg_count,
8690             p_data    => x_msg_data);
8691 
8692   WHEN OTHERS THEN
8693      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
8694      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
8695      FND_MSG_PUB.Add;
8696      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8697       FND_MSG_PUB.count_and_get(
8698             p_encoded => FND_API.g_false,
8699             p_count   => x_msg_count,
8700             p_data    => x_msg_data);
8701 END CREATE_LIST_FROM_QUERY ;
8702 -- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
8703 
8704 PROCEDURE process_tar_actions
8705              (p_action_used_by_id  in  number,
8706               p_action_used_by     in  varchar2  ,-- DEFAULT 'LIST',
8707               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
8708               x_return_status      OUT NOCOPY VARCHAR2,
8709               x_msg_count          OUT NOCOPY NUMBER,
8710               x_msg_data           OUT NOCOPY VARCHAR2) IS
8711 
8712   -- AMS_LIST_SELECT_ACTIONS Record for init record and complete record
8713   l_tmp_action_rec             ams_listaction_pvt.action_rec_type;
8714   p_action_rec                 ams_listaction_pvt.action_rec_type;
8715   l_list_select_action_id      number;
8716 
8717   ----------------------------------------------------------------------------
8718   -- Cursor definition to select list_select_action_id.Will be used in loop to
8719   -- Process each cursor record according to order specified by the user
8720   ----------------------------------------------------------------------------
8721   CURSOR c_action_dets is
8722   SELECT a.list_act_type, a.list_used_by,
8723          a.list_action_type, a.order_number,
8724          a.list_header_id  ,
8725          -- a.act_list_header_id,
8726          c.list_select_action_id act_list_header_id,
8727          c.distribution_pct distribution_pct,
8728          c.arc_incl_object_from arc_incl_object_from
8729     FROM ams_act_lists a ,ams_act_lists b, ams_list_select_actions c
8730    WHERE a.list_used_by_id   = b.list_used_by_id
8731      AND a.list_used_by =   b.list_used_by
8732      AND b.list_header_id =  p_action_used_by_id
8733      AND b.list_act_type  =   'TARGET'
8734      and a.list_act_type <> 'TARGET'
8735          and b.list_header_id = c.action_used_by_id
8736          and a.order_number = c.order_number
8737    ORDER by c.order_number;
8738 
8739 l_action_dets_rec  c_action_dets%ROWTYPE;
8740 
8741   TYPE big_tbl_type is table of VARCHAR2(32767) index by BINARY_INTEGER;
8742   l_std_sql VARCHAR2(32767);
8743   l_include_sql VARCHAR2(32767);
8744   l_include_count number:=0;
8745   l_final_big_sql VARCHAR2(32767);
8746   l_include_sql_tbl  big_tbl_type ;
8747   l_std_sql_tbl  big_tbl_type ;
8748   l_join_string   varchar2(50);
8749 l_no_of_chunks            number;
8750 l_const_sql varchar2(4000) ;
8751   TYPE char_tbl_type is table of VARCHAR2(100) index by BINARY_INTEGER;
8752   TYPE num_tbl_type is table of number index by BINARY_INTEGER;
8753   l_rank_tbl      char_tbl_type;
8754   l_rank_num_tbl      num_tbl_type;
8755   l_order_num_tbl     num_tbl_type;
8756 l_sorted   number;
8757 l_order_num   number;
8758 l_update_sql  VARCHAR2(32767);
8759 l_list_header_id number ;
8760 cursor c1 is
8761 select generation_type
8762 from ams_list_headers_all
8763 where list_header_id = l_list_header_id;
8764 l_generation_type varchar2(60);
8765 l_PARAMETERIZED_FLAG  varchar2(1) := 'N';
8766 TYPE table_char  IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
8767 l_table_char table_char;
8768 
8769 cursor c_query(l_query_id number) is select
8770 nvl(PARAMETERIZED_FLAG ,'N')
8771 from ams_list_queries_all
8772 where  list_query_id = l_query_id ;
8773 
8774 cursor c_param_values(l_query_id in number) is
8775 select PARAMETER_ORDER, PARAMETER_VALUE,parameter_name
8776 from ams_list_queries_param
8777 where list_query_id = l_query_id
8778 order by PARAMETER_ORDER;
8779 
8780 l_string VARCHAR2(32767);
8781 l1 varchar2(2000);
8782 l_remote_update_sql  VARCHAR2(32767);
8783 l_null          number;
8784 l_total_recs    number;
8785 l_query_templ_flag   varchar2(1) ;
8786 
8787 l_temp_sql   varchar2(32767);
8788 l_dist_pct_tbl        num_tbl_type;
8789 l_list_select_id      num_tbl_type;
8790 l_incl_object_type    char_tbl_type;
8791 
8792 cursor c_count1  is select count(1)
8793 from ams_list_entries
8794 where list_header_id  = g_list_header_id ;
8795 l_count1 number:= 0;
8796 
8797 cursor c_query_temp_type is
8798 select 'Y'
8799 from ams_list_headers_vl  a ,
8800      ams_query_template_all b
8801 where a.list_header_id = g_list_header_id
8802  and b.template_type  = 'PARAMETERIZED'
8803   and a.query_template_id = b.template_id ;
8804 
8805 l_const_sql1 varchar2(4000) ;
8806 l_l_sele_action_id      number;
8807 
8808   CURSOR c_action_dets1 is
8809   SELECT list_select_action_id
8810     FROM ams_list_select_actions
8811    WHERE action_used_by_id   = p_action_used_by_id
8812      AND arc_action_used_by  = p_action_used_by
8813      AND order_number        = l_order_num;
8814 l_order_number number := 0;
8815 
8816 l_repeat_tg     varchar2(1);
8817 CURSOR c_repeat_tg is
8818 select 'Y' from ams_campaign_schedules_b b, ams_list_headers_all h
8819 where h.list_header_id = g_list_header_id
8820   and h.LIST_USED_BY_ID = b.schedule_id
8821   and b.orig_csch_id is not null;
8822 
8823 l_incl_header_id        number;
8824 
8825 CURSOR c_repeat_tg_id is
8826   SELECT a.list_header_id
8827     FROM ams_act_lists a ,ams_act_lists b
8828    WHERE a.list_used_by_id   = b.list_used_by_id
8829      AND a.list_used_by =   b.list_used_by
8830      AND b.list_header_id =   g_list_header_id
8831      AND b.list_act_type  =   'TARGET'
8832      and a.list_act_type <> 'TARGET'
8833          and a.list_action_type = 'INCLUDE'
8834    ORDER by a.order_number;
8835 
8836 
8837 --Bug 4685389. bmuthukr. to check the total # of parameters
8838 cursor c_check_num_params(p_incl_object_id number) is
8839 select count(1)
8840   from ams_list_queries_param
8841  where list_query_id = p_incl_object_id;
8842 
8843  l_tot_params   number := 0;
8844 --
8845 
8846 BEGIN
8847   --IF(p_log_flag ='Y')then
8848   write_to_act_log('Executing process_tar_actions. ','LIST',g_list_header_id, 'HIGH');
8849   --END IF;
8850 
8851 l_const_sql := ' minus '||
8852                ' select list_entry_source_system_id ' ||
8853                ' from ams_list_entries ' ||
8854                ' where list_header_id  = ' || p_action_used_by_id   ;
8855 
8856 l_const_sql1 := '   and LIST_SELECT_ACTION_ID = ';
8857 
8858   open c_repeat_tg;
8859   fetch c_repeat_tg into l_repeat_tg;
8860   close c_repeat_tg;
8861  write_to_act_log(p_msg_data => 'Repeat  TG - '||l_repeat_tg,
8862                   p_arc_log_used_by => 'LIST',
8863                   p_log_used_by_id  => g_list_header_id,
8864                   p_level=>'LOW');
8865   OPEN C_ACTION_DETS;
8866   LOOP
8867     FETCH c_action_dets INTO l_action_dets_rec;
8868     EXIT WHEN c_action_dets%NOTFOUND;
8869 
8870      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
8871      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : before IMMEDIATE');
8872      FND_MSG_PUB.Add;
8873      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');
8874      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');
8875    if l_action_dets_rec.list_act_type = 'SQL' then
8876       OPEN c_query( l_action_dets_rec.list_header_id );
8877       FETCH c_query INTO l_PARAMETERIZED_FLAG  ;
8878       close  c_query;
8879    end if;
8880 
8881    -- Bug 4685389. bmuthukr. to check the total # of parameters. If it exceeds 100 abort the process
8882    if nvl(l_parameterized_flag,'N') = 'Y' then
8883 
8884       open c_check_num_params(l_action_dets_rec.list_header_id);
8885       fetch c_check_num_params into l_tot_params;
8886       close c_check_num_params;
8887 
8888       if nvl(l_tot_params,0) > 100 then
8889          write_to_act_log('Numbers of parameters exceed 100. Aborting list generation process. Please redefine your criteria and restrict it to 100.',
8890          'LIST',g_list_header_id,'HIGH');
8891 
8892          UPDATE ams_list_headers_all
8893             SET last_generation_success_flag = 'N',
8894                 status_code                  = 'FAILED',
8895                 ctrl_status_code             = 'DRAFT',
8896                 user_status_id               = 311,
8897                 status_date                  = sysdate,
8898                 last_update_date             = sysdate,
8899                 main_gen_end_time            = sysdate,
8900                 no_of_rows_in_ctrl_group     = null
8901           WHERE list_header_id               = g_list_header_id;
8902          x_return_status := 'E';
8903          logger;
8904          commit;
8905          RETURN;
8906       end if;
8907    end if;
8908    --
8909 
8910    -- Changes for employee list issue..
8911    if l_action_dets_rec.list_act_type NOT IN ('IMPH','LIST','SQL','DIWB','CELL') then
8912       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.',
8913                        p_arc_log_used_by => 'LIST',
8914                        p_log_used_by_id  => g_list_header_id,
8915                        p_level => 'HIGH');
8916 
8917         UPDATE ams_list_headers_all
8918         SET    last_generation_success_flag = 'N',
8919                status_code                  = 'FAILED',
8920                user_status_id               = 311,
8921                status_date                  = sysdate,
8922                last_update_date             = sysdate,
8923                main_gen_end_time            = sysdate,
8924                ctrl_status_code             = 'DRAFT',
8925                no_of_rows_in_ctrl_group     = null
8926         WHERE  list_header_id               = g_list_header_id;
8927 
8928     -- Added for cancel list gen as it prevents parallel update- Raghu
8929     -- of list headers when cancel button is pressed
8930     commit;
8931 
8932      x_return_status := 'E';
8933      x_msg_count := 1;
8934      x_msg_data := ' Invalid Included Object--Valid inclusions are imported list, list, custom sql, segment, work book';
8935      RETURN;
8936    end if;
8937    --
8938 
8939 /******************************************************************************/
8940 /************** call for cancel list generation added 05/23/2005 **************/
8941 /******************************************************************************/
8942 -- inside process_tar_actions
8943 
8944    AMS_LISTGENERATION_UTIL_PKG.cancel_list_gen(
8945                   p_list_header_id => g_list_header_id ,
8946                   p_remote_gen     => g_remote_list    ,
8947                   p_remote_gen_list=> g_remote_list_gen,
8948                   p_database_link  => g_database_link,
8949                   x_msg_count      => x_msg_count ,
8950                   x_msg_data       => x_msg_data ,
8951                   x_return_status  => x_return_status
8952              );
8953 
8954   IF(x_return_status <> FND_API.G_RET_STS_SUCCESS )THEN
8955      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
8956         write_to_act_log('Error in Cancel List generation', 'LIST', g_list_header_id,'HIGH');
8957         write_to_act_log('Error while executing Cancel List generation '||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
8958      end if;
8959      RAISE FND_API.G_EXC_ERROR;
8960   ELSE
8961      write_to_act_log('Success in Cancel List generation', 'LIST', g_list_header_id,'LOW');
8962   END IF;
8963 
8964 /******************************************************************************/
8965 /************** call for cancel list generation added 05/23/2005 **************/
8966 /******************************************************************************/
8967 
8968       write_to_act_log('Calling process_'||l_action_dets_rec.list_act_type,'LIST',g_list_header_id,'LOW');
8969       execute immediate
8970       'BEGIN
8971         AMS_ListGeneration_PKG.process_'||l_action_dets_rec.list_act_type ||
8972          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
8973       END;'
8974       using  p_action_used_by_id,
8975              l_action_dets_rec.list_header_id ,
8976              l_action_dets_rec.list_action_type,
8977              l_action_dets_rec.act_list_header_id,
8978              l_action_dets_rec.order_number,
8979              l_action_dets_rec.order_number,
8980              'N',--CHECK p_action_rec.incl_control_group,
8981              OUT  x_msg_data,
8982              OUT  x_msg_count,
8983              in OUT  x_return_status ,
8984              OUT l_std_sql ,
8985              OUT l_include_sql;
8986     if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
8987        write_to_act_log('Error when executing process_'||l_action_dets_rec.list_act_type, 'LIST', g_list_header_id,'HIGH');
8988        write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
8989        UPDATE ams_list_headers_all
8990           SET last_generation_success_flag = 'N',
8991               status_code                  = 'FAILED',
8992               user_status_id               = 311,
8993               status_date                  = sysdate,
8994               last_update_date             = sysdate,
8995               main_gen_end_time            = sysdate
8996         WHERE list_header_id               = g_list_header_id;
8997        commit;
8998        RETURN;
8999     else
9000        write_to_act_log('Process_'||l_action_dets_rec.list_act_type||' executed successfully.', 'LIST', g_list_header_id,'LOW');
9001     end if;
9002 
9003      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9004      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : AFTER IMMEDIATE');
9005      FND_MSG_PUB.Add;
9006 
9007      if l_action_dets_rec.list_action_type  = 'INCLUDE' then
9008         l_include_count := l_include_count +1  ;
9009         l_include_sql_tbl(l_include_count) := l_include_sql ;
9010         l_std_sql_tbl(l_include_count)  := l_std_sql;
9011 		l_order_num_tbl(l_include_count) := l_action_dets_rec.order_number;
9012        -- l_rank_tbl(l_include_count)  :=  lpad(l_action_dets_rec.Order_number,50,'0')
9013                         -- || lpad(l_action_dets_rec.order_number,50,'0');
9014 
9015         l_dist_pct_tbl(l_include_count) := l_action_dets_rec.distribution_pct;
9016         l_list_select_id(l_include_count) := l_action_dets_rec.act_list_header_id;
9017         l_incl_object_type(l_include_count) := l_action_dets_rec.arc_incl_object_from;
9018 
9019      else
9020         if l_action_dets_rec.list_action_type  = 'EXCLUDE' then
9021            l_join_string := ' minus ';
9022            l_list_header_id := l_action_dets_rec.list_header_id;
9023            open c1;
9024            FETCH c1 into l_generation_type;
9025            close c1;
9026         else
9027            l_join_string := ' intersect ';
9028            l_list_header_id := l_action_dets_rec.list_header_Id;
9029            open c1;
9030            FETCH c1 into l_generation_type;
9031            close c1;
9032         end if;
9033 
9034         write_to_act_log('SQL statement for INCLUSION','LIST',g_list_header_id,'LOW');
9035 
9036        FOR i IN 1 .. l_include_count
9037        loop
9038         l_std_sql_tbl(i)  :=
9039                                l_std_sql_tbl(i)   ||
9040                                l_join_string ||
9041                                l_std_sql;
9042      l_no_of_chunks  := ceil(length(l_std_sql_tbl(i))/2000 );
9043      for j in 1 ..l_no_of_chunks
9044      loop
9045           null;
9046         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000),'LIST',g_list_header_id, 'LOW');
9047      end loop;
9048        end loop;
9049      end if;
9050 
9051 
9052      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
9053        RAISE FND_API.G_EXC_ERROR;
9054      END IF;
9055 -- end Of Dynamic Procedure
9056    l_join_string :='';
9057    write_to_act_log('Action on this selection completed. ', 'LIST', g_list_header_id,'LOW');
9058 ---------------------------------------------------------------------------
9059   END LOOP;  --  End loop c_action_dets
9060   CLOSE c_action_dets;
9061 
9062      write_to_act_log('No of inclusions in the selection '|| l_include_count, 'LIST', g_list_header_id,'LOW');
9063      write_to_act_log('Sorting based on rank for the selection. ', 'LIST', g_list_header_id,'LOW');
9064 
9065 /*       -- Sorting According to rank
9066        FOR i IN 1 .. l_include_count
9067        loop
9068           l_rank_num_tbl(i) := i;
9069           if i <> 1 then
9070              for j in 1 .. i-1
9071              loop
9072                if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
9073                   for k in reverse j .. i-1
9074                   loop
9075                      l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
9076                   end loop;
9077                   l_rank_num_tbl(j) := i;
9078                   exit;
9079                end if;
9080              end loop;
9081           end if;
9082        end loop;*/
9083   for  i in 1 .. l_include_count
9084   loop
9085         l_l_sele_action_id := null;
9086         l_sorted := i;--l_rank_num_tbl(i);
9087 		l_order_num := l_order_num_tbl(i);
9088         open c_action_dets1;
9089         fetch c_action_dets1 into l_l_sele_action_id;
9090         close c_action_dets1;
9091         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');
9092 -- 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 ||')';
9093      --  l_std_sql_tbl(l_sorted) || l_const_sql || ')';
9094      WRITE_TO_ACT_LOG('Final SQL formed for generating TG.', 'LIST', g_list_header_id,'LOW');
9095     if l_final_big_sql is not null then
9096      l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
9097      for i in 1 ..l_no_of_chunks
9098      loop
9099           null;
9100           WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000),'LIST',g_list_header_id,'LOW');
9101      end loop;
9102    end if;
9103 
9104      if l_include_sql_tbl(l_sorted) is not null then
9105       l_no_of_chunks := 0;
9106       l_no_of_chunks  := ceil(length(l_include_sql_tbl(l_sorted))/2000 );
9107       for i in 1 ..l_no_of_chunks
9108        loop
9109         WRITE_TO_ACT_LOG(substrb(l_include_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9110        end loop;
9111      end if;
9112     if l_std_sql_tbl(l_sorted) is not null then
9113       l_no_of_chunks := 0;
9114       l_no_of_chunks  := ceil(length(l_std_sql_tbl(l_sorted))/2000 );
9115       for i in 1 ..l_no_of_chunks
9116        loop
9117         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(l_sorted),(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9118        end loop;
9119     end if;
9120     if l_const_sql is not null then
9121      l_no_of_chunks := 0;
9122      l_no_of_chunks  := ceil(length(l_const_sql)/2000 );
9123      for i in 1 ..l_no_of_chunks
9124      loop
9125         WRITE_TO_ACT_LOG(substrb(l_const_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
9126      end loop;
9127     end if;
9128     write_to_act_log(' '||l_const_sql1||to_char(l_l_sele_action_id), 'LIST', g_list_header_id,'LOW');
9129     if l_PARAMETERIZED_FLAG  = 'N' then
9130         write_to_act_log('No parameters required for generating this TG', 'LIST', g_list_header_id,'LOW');
9131         if nvl(l_dist_pct_tbl(l_sorted),100) <> 100 then
9132            write_to_act_log('Included object is of type '||l_incl_object_type(l_sorted),'LIST',g_list_header_id,'LOW');
9133            write_to_act_log('% Requested for this selection is '||l_dist_pct_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
9134            if l_incl_object_type(l_sorted) in ('SQL','DIWB','CELL') then
9135               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');
9136               l_temp_sql := l_include_sql_tbl(l_sorted);
9137               l_temp_sql := 'SELECT count(1) '||substr(l_temp_sql,instr(l_temp_sql, ' FROM '));
9138               -- Modified for bug 5238900. bmuthukr
9139               -- 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||')' );
9140               get_count(l_list_select_id(l_sorted),l_sorted,'OTHERS',l_temp_sql||l_std_sql_tbl(l_sorted)||l_const_sql||')' );
9141            elsif l_incl_object_type(l_sorted) = 'LIST' then
9142               get_count(l_list_select_id(l_sorted),l_sorted,'LIST',null);
9143            elsif l_incl_object_type(l_sorted) = 'IMPH' then
9144               get_count(l_list_select_id(l_sorted),l_sorted,'IMPH',null);
9145            end if;
9146            write_to_act_log('No of rows requested from the selection is '||g_reqd_num_tbl(l_sorted),'LIST',g_list_header_id,'LOW');
9147         else
9148            g_act_num_tbl(l_sorted)  := -1;
9149            g_reqd_num_tbl(l_sorted) := -1;
9150         end if;
9151         if g_remote_list_gen = 'N' then
9152                 /* If the list is not based on the remote data source and if it's based on remote data source
9153                    but needs to be generated in the local instance means it's migrated to the local instance */
9154            --l_const_sql1 := ' ';
9155            --l_l_sele_action_id := null;
9156            if g_reqd_num_tbl(l_sorted) <> -1 then
9157               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);
9158            else
9159               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 ||')';
9160            end if;
9161 
9162            write_to_act_log('Target group generated in local instance', 'LIST', g_list_header_id,'HIGH');
9163            	else
9164                 /* If the list is based on the remote data source and it's not migrated to the local instance or
9165                    a segment , sql or workbook is in the list selection then it will be generated in the remote
9166                    instance through a dynamic procedure call */
9167            --l_const_sql1 := ' ';
9168            --l_l_sele_action_id := null;
9169 
9170            if g_reqd_num_tbl(l_sorted) <> -1 then
9171               write_to_act_log('Calling remote procedure to generate target group in remote instance', 'LIST', g_list_header_id,'LOW');
9172               execute immediate
9173               'BEGIN
9174                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9175               ' END;'
9176               using  '1',
9177               l_null,
9178               'T',
9179               l_null,
9180               OUT x_return_status,
9181               OUT x_msg_count,
9182               OUT x_msg_data,
9183               g_list_header_id,
9184               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),
9185 --              l_final_big_sql,
9186               l_null,
9187               OUT l_total_recs,
9188               'LISTGEN';
9189            else
9190               execute immediate
9191               'BEGIN
9192                AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9193               ' END;'
9194               using  '1',
9195               l_null,
9196               'T',
9197               l_null,
9198               OUT x_return_status,
9199               OUT x_msg_count,
9200               OUT x_msg_data,
9201               g_list_header_id,
9202               l_include_sql_tbl(l_sorted) ||l_std_sql_tbl(l_sorted) || l_const_sql ||l_const_sql1||l_l_sele_action_id ||')',
9203               --   l_final_big_sql,
9204               l_null,
9205               OUT l_total_recs,
9206               'LISTGEN';
9207            end if;
9208            if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9209               write_to_act_log('Error while executing remote procedure for generating target group', 'LIST', g_list_header_id,'HIGH');
9210               write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9211               --Added for bug 4577528 by bmuthukr.
9212               update ams_list_headers_all
9213                  set last_generation_success_flag = 'N',
9214                      status_code                  = 'FAILED',
9215                      user_status_id               = 311,
9216                      status_date                  = sysdate,
9217                      last_update_date             = sysdate,
9218                      main_gen_end_time            = sysdate,
9219                      ctrl_status_code             = 'DRAFT',
9220                      no_of_rows_in_ctrl_group     = null
9221                where list_header_id               = g_list_header_id;
9222               update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
9223               write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
9224               x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
9225               return;
9226               --
9227            else
9228               write_to_act_log('Target group generated in remote instance', 'LIST', g_list_header_id,'HIGH');
9229            end if;
9230 
9231         end if;
9232  else  -- For l_PARAMETERIZED_FLAG  = 'Y'
9233         write_to_act_log('Fetching parameters required for generating this target group', 'LIST', g_list_header_id,'LOW');
9234            --l_const_sql1 := ' ';
9235            --l_l_sele_action_id := null;
9236 
9237         for i in 1 .. 100 loop
9238            l_table_char(i) := ' ';
9239         end loop;
9240          write_to_act_log(p_msg_data => 'Repeat TG = '||l_repeat_tg,
9241                                                 p_arc_log_used_by => 'LIST',
9242                                                 p_log_used_by_id  => g_list_header_id,
9243                                                 p_level =>'LOW');
9244         l_incl_header_id        := null;
9245         if l_repeat_tg = 'Y' then
9246                 open c_repeat_tg_id;
9247                 fetch c_repeat_tg_id into l_incl_header_id;
9248                 close c_repeat_tg_id;
9249            else
9250                 l_incl_header_id := l_action_dets_rec.list_header_id;
9251         end if;
9252 
9253 --      for i in c_param_values(l_action_dets_rec.list_header_id )
9254         for i in c_param_values(l_incl_header_id)
9255         loop
9256            l_table_char(i.PARAMETER_ORDER) := i.PARAMETER_VALUE;
9257            write_to_act_log('Parameter - '||i.parameter_order||' is '||l_table_char(i.PARAMETER_ORDER),'LIST', g_list_header_id,'LOW');
9258         end loop;
9259         l_string := 'DECLARE   ' ||
9260         'l_string1 varchar2(10000) ; ' ||
9261         'begin    ' ||
9262                 ' l_string1 :=   :1  || ' || ' :2  || ' || ' :3  || ' || ' :4  || ' ||
9263                        ' :5  || ' || ' :6  || ' || ' :7  || ' || ' :8  || ' ||
9264                        ' :9  || ' || ' :10  || ' || ' :11  || ' || ' :12  || ' ||
9265                        ' :13  || ' || ' :14  || ' || ' :15  || ' || ' :16  || ' ||
9266                        ' :17  || ' || ' :18  || ' || ' :19  || ' || ' :20  || ' ||
9267                        ' :21  || ' || ' :22  || ' || ' :23  || ' || ' :24  || ' ||
9268                        ' :25  || ' || ' :26  || ' || ' :27  || ' || ' :28  || ' ||
9269                        ' :29  || ' || ' :30  || ' || ' :31  || ' || ' :32  || ' ||
9270                        ' :33  || ' || ' :34  || ' || ' :35  || ' || ' :36  || ' ||
9271                        ' :37  || ' || ' :38  || ' || ' :39  || ' || ' :40  || ' ||
9272                        ' :41  || ' || ' :42  || ' || ' :43  || ' || ' :44  || ' ||
9273                        ' :45  || ' || ' :46  || ' || ' :47  || ' || ' :48  || ' ||
9274                        ' :49  || ' || ' :50  || ' || ' :51  || ' || ' :52  || ' ||
9275                        ' :53  || ' || ' :54  || ' || ' :55  || ' || ' :56  || ' ||
9276                        ' :57  || ' || ' :58  || ' || ' :59  || ' || ' :60  || ' ||
9277                        ' :61  || ' || ' :62  || ' || ' :63  || ' || ' :64  || ' ||
9278                        ' :65  || ' || ' :66  || ' || ' :67  || ' || ' :68  || ' ||
9279                        ' :69  || ' || ' :70  || ' || ' :71  || ' || ' :72  || ' ||
9280                        ' :73  || ' || ' :74  || ' || ' :75  || ' || ' :76  || ' ||
9281                        ' :77  || ' || ' :78  || ' || ' :79  || ' || ' :80  || ' ||
9282                        ' :81  || ' || ' :82  || ' || ' :83  || ' || ' :84  || ' ||
9283                        ' :85  || ' || ' :86  || ' || ' :87  || ' || ' :88  || ' ||
9284                        ' :89  || ' || ' :90  || ' || ' :91  || ' || ' :92  || ' ||
9285                        ' :93  || ' || ' :94  || ' || ' :95  || ' || ' :96  || ' ||
9286                        ' :97  || ' || ' :98  || ' || ' :99  || ' || ' :100  ; ' ||' '||
9287        --  l_final_big_sql ||
9288 l_include_sql_tbl(l_sorted)||l_std_sql_tbl(l_sorted)||l_const_sql||l_const_sql1||l_l_sele_action_id ||')'||
9289        '; end;  '  ;
9290 /* Changed to fix :1 and :name issue */
9291 
9292      open C_QUERY_TEMP_TYPE ;
9293         fetch  c_query_temp_type into l_query_templ_flag   ;
9294            if l_query_templ_flag  = 'Y' then
9295               l_no_of_chunks := 0;
9296                 -- bmuthukr 4339703
9297               for i in c_param_values(l_incl_header_id)
9298               loop
9299                  l_no_of_chunks := l_no_of_chunks + 1;
9300                  l_string := replace(l_string,':' || l_no_of_chunks  || ' ' , ':'|| i.parameter_name||' ' );
9301               end loop;
9302            end if;
9303            l_no_of_chunks := 0;
9304      close c_query_temp_type ;
9305  /*END Changed to fix :1 and :name issue */
9306      WRITE_TO_ACT_LOG('SQL to be executed to generate target group', 'LIST', g_list_header_id,'LOW');
9307      WRITE_TO_ACT_LOG('Length of the sql  '||length(l_string), 'LIST', g_list_header_id,'LOW');
9308      l_no_of_chunks  := ceil(length(l_string)/80 );
9309      for i in 1 ..l_no_of_chunks
9310      loop
9311         WRITE_TO_ACT_LOG(substrb(l_string,(80*i) - 79,80), 'LIST', g_list_header_id,'LOW');
9312         l1 := substrb(l_string,(80*i)-79,80);
9313      end loop;
9314 if g_remote_list_gen = 'N' then
9315      WRITE_TO_ACT_LOG('Generating target group with parameters ','LIST', g_list_header_id,'LOW');
9316 execute immediate   l_string
9317 using l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
9318       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
9319       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
9320       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
9321       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
9322       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
9323       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
9324       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
9325       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
9326       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
9327       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
9328       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
9329       l_table_char(49), l_table_char(50),
9330       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
9331       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
9332       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
9333       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
9334       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
9335       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
9336       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
9337       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
9338       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
9339       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
9340       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
9341       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
9342       l_table_char(79), l_table_char(100);
9343       WRITE_TO_ACT_LOG('Generating target group in local instance.','LIST', g_list_header_id,'HIGH');
9344 
9345  end if;
9346 
9347  if g_remote_list_gen = 'Y' then
9348     write_to_act_log('Calling remote procedure with parameters to generate target group in remote instance ', 'LIST', g_list_header_id,'LOW');
9349                execute immediate
9350               'BEGIN
9351        AMS_Remote_ListGen_PKG.remote_param_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,
9352        :13,:14,:15,:16,:17,:18,:19,:20,
9353        :21,:22,:23,:24,:25,:26,:27,:28,:29,:30,
9354        :31,:32,:33,:34,:35,:36,:37,:38,:39,:40,
9355        :41,:42,:43,:44,:45,:46,:47,:48,:49,:50,
9356        :51,:52,:53,:54,:55,:56,:57,:58,:59,:60,
9357        :61,:62,:63,:64,:65,:66,:67,:68,:69,:70,
9358        :71,:72,:73,:74,:75,:76,:77,:78,:79,:80,
9359        :81,:82,:83,:84,:85,:86,:87,:88,:89,:90,
9360        :91,:92,:93,:94,:95,:96,:97,:98,:99,:100,
9361        :101,:102,:103,:104,:105,:106,:107,:108,:109,:110,:111,:112
9362        )'||';'||
9363               ' END;'
9364               using  '1',
9365               l_null,
9366               'T',
9367               l_null,
9368               OUT x_return_status,
9369               OUT x_msg_count,
9370               OUT x_msg_data,
9371               g_list_header_id,
9372               l_string,
9373               l_null,
9374              OUT l_total_recs,
9375              'PARAMLISTGEN',
9376       l_table_char(1), l_table_char(2), l_table_char(3), l_table_char(4),
9377       l_table_char(5), l_table_char(6), l_table_char(7), l_table_char(8),
9378       l_table_char(9), l_table_char(10), l_table_char(11), l_table_char(12),
9379       l_table_char(13), l_table_char(14), l_table_char(15), l_table_char(16),
9380       l_table_char(17), l_table_char(18), l_table_char(19), l_table_char(20),
9381       l_table_char(21), l_table_char(22), l_table_char(23), l_table_char(24),
9382       l_table_char(25), l_table_char(26), l_table_char(27), l_table_char(28),
9383       l_table_char(29), l_table_char(30), l_table_char(31), l_table_char(32),
9384       l_table_char(33), l_table_char(34), l_table_char(35), l_table_char(36),
9385       l_table_char(37), l_table_char(38), l_table_char(39), l_table_char(40),
9386       l_table_char(41), l_table_char(42), l_table_char(43), l_table_char(44),
9387       l_table_char(45), l_table_char(46), l_table_char(47), l_table_char(48),
9388       l_table_char(49), l_table_char(50),
9389       l_table_char(51), l_table_char(52), l_table_char(53), l_table_char(54),
9390       l_table_char(55), l_table_char(56), l_table_char(57), l_table_char(58),
9391       l_table_char(59), l_table_char(60), l_table_char(61), l_table_char(62),
9392       l_table_char(63), l_table_char(64), l_table_char(65), l_table_char(66),
9393       l_table_char(67), l_table_char(68), l_table_char(69), l_table_char(70),
9394       l_table_char(71), l_table_char(72), l_table_char(73), l_table_char(74),
9395       l_table_char(75), l_table_char(76), l_table_char(77), l_table_char(78),
9396       l_table_char(79), l_table_char(80), l_table_char(81), l_table_char(82),
9397       l_table_char(83), l_table_char(84), l_table_char(85), l_table_char(86),
9398       l_table_char(87), l_table_char(88), l_table_char(89), l_table_char(90),
9399       l_table_char(91), l_table_char(92), l_table_char(93), l_table_char(94),
9400       l_table_char(95), l_table_char(96), l_table_char(97), l_table_char(98),
9401       l_table_char(79), l_table_char(100);
9402       if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9403          write_to_act_log('Error while generating target group in remote instance.', 'LIST', g_list_header_id,'HIGH');
9404          write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9405          --Added for bug 4577528 by bmuthukr.
9406          update ams_list_headers_all
9407             set last_generation_success_flag = 'N',
9408                 status_code                  = 'FAILED',
9409                 user_status_id               = 311,
9410                 status_date                  = sysdate,
9411                 last_update_date             = sysdate,
9412                 main_gen_end_time            = sysdate,
9413                 ctrl_status_code             = 'DRAFT',
9414                 no_of_rows_in_ctrl_group     = null
9415           where list_header_id               = g_list_header_id;
9416          update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
9417          write_to_act_log('Aborting list generation ', 'LIST', g_list_header_id,'HIGH');
9418          x_return_status := FND_API.g_ret_sts_error; --Gen return status should go to error.
9419          return;
9420          --
9421       else
9422          write_to_act_log('Target group generated successfully in remote instance', 'LIST', g_list_header_id,'HIGH');
9423       end if;
9424     end if;
9425 
9426 end if;
9427   end loop;
9428    open c_count1;
9429       fetch c_count1 into l_count1;
9430       close c_count1;
9431       WRITE_TO_ACT_LOG('Total Number of entries in list is '|| l_count1, 'LIST', g_list_header_id,'LOW');
9432       WRITE_TO_ACT_LOG('Execution of procedure process_tar_action completed.', 'LIST', g_list_header_id,'LOW');
9433 
9434 EXCEPTION
9435 
9436   WHEN AMS_LISTGENERATION_UTIL_PKG.cancelListGen THEN
9437      IF(c_action_dets%ISOPEN)THEN
9438         CLOSE c_action_dets;
9439      END IF;
9440 
9441      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9442 
9443      write_to_act_log('executing process_tar_actions - user action to cancel list generation detected ', 'LIST', g_list_header_id,'HIGH');
9444      -- Got to raise the exception again because Listgen has to end generation.
9445      raise AMS_LISTGENERATION_UTIL_PKG.cancelListGen;
9446 
9447    WHEN FND_API.G_EXC_ERROR THEN
9448      IF(c_action_dets%ISOPEN)THEN
9449         CLOSE c_action_dets;
9450      END IF;
9451      -- Check if reset of the status is required
9452      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9453      x_return_status := FND_API.G_RET_STS_ERROR ;
9454       FND_MSG_PUB.count_and_get(
9455             p_encoded => FND_API.g_false,
9456             p_count   => x_msg_count,
9457             p_data    => x_msg_data);
9458 
9459   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9460      IF(c_action_dets%ISOPEN)THEN
9461         CLOSE c_action_dets;
9462      END IF;
9463      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9464      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9465      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
9466      FND_MSG_PUB.Add;
9467      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9468       FND_MSG_PUB.count_and_get(
9469             p_encoded => FND_API.g_false,
9470             p_count   => x_msg_count,
9471             p_data    => x_msg_data);
9472 
9473   WHEN OTHERS THEN
9474      IF(c_action_dets%ISOPEN)THEN
9475         CLOSE c_action_dets;
9476      END IF;
9477      write_to_act_log('Error while executing procedure process_tar_actions '||sqlcode||'   '||sqlerrm,'LIST',g_list_header_id,'HIGH');
9478      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9479      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
9480      FND_MSG_PUB.Add;
9481      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
9482       FND_MSG_PUB.count_and_get(
9483             p_encoded => FND_API.g_false,
9484             p_count   => x_msg_count,
9485             p_data    => x_msg_data);
9486 END process_tar_actions;
9487 
9488 -- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
9489 
9490 PROCEDURE GENERATE_TARGET_GROUP
9491 ( p_api_version            IN     NUMBER,
9492   p_init_msg_list          IN     VARCHAR2   := FND_API.G_TRUE,
9493   p_commit                 IN     VARCHAR2   := FND_API.G_FALSE,
9494   p_validation_level       IN     NUMBER     := FND_API.G_VALID_LEVEL_FULL,
9495   p_list_header_id         IN     NUMBER,
9496   x_return_status          OUT NOCOPY    VARCHAR2,
9497   x_msg_count              OUT NOCOPY    NUMBER,
9498   x_msg_data               OUT NOCOPY    VARCHAR2) IS
9499 
9500   l_api_name            CONSTANT VARCHAR2(30)  := 'GENERATE_LIST';
9501   l_api_version         CONSTANT NUMBER        := 1.0;
9502 
9503   -----------------------------------------------------------
9504   --The no. of entries flagged as duplicates for this list.--
9505   --Only populated if deduplication is requested.          --
9506   -----------------------------------------------------------
9507   --gjoby check if l_no_of_duplicates required
9508   l_no_of_duplicates      NUMBER := 0;
9509 
9510   -- Two records are required for init rec and complete rec
9511   -- Table ams_list_headers_all_tl and ams_list_headers_all
9512   l_listheader_rec        ams_listheader_pvt.list_header_rec_type;
9513   l_tmp_listheader_rec    ams_listheader_pvt.list_header_rec_type;
9514 
9515   -- Two records are required for init rec and complete rec
9516   -- Table ams_list_select_actions
9517   l_listaction_rec        ams_listaction_pvt.action_rec_type;
9518   l_tmp_listaction_rec    ams_listaction_pvt.action_rec_type;
9519   l_no_of_rows_in_list number ;
9520   cursor c_get_rows (c_list_header_id in number ) is
9521   select no_of_rows_in_list
9522   from ams_list_headers_all
9523   where list_header_id = c_list_header_id ;
9524   l_error_position       varchar2(100);
9525 
9526 
9527   cursor c_remote_list is
9528   select nvl(stypes.remote_flag,'N') ,database_link
9529     from ams_list_src_types stypes, ams_list_headers_all list
9530    where list.list_source_type = stypes.source_type_code
9531      and list_header_id  =  p_list_header_id;
9532 
9533   l_list_selection      varchar2(1);
9534   l_onlylist_selection  varchar2(1);
9535   cursor c_list_selection is
9536   select 'Y' from ams_list_select_actions
9537    where  action_used_by_id = p_list_header_id
9538      and  arc_action_used_by = 'LIST'
9539      and  arc_incl_object_from in ('CELL','DIWB','SQL');
9540 
9541   cursor c_only_list_selection is
9542   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
9543    where  act.action_used_by_id = p_list_header_id
9544    and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
9545    and  act.INCL_OBJECT_ID = head.list_header_id
9546    and  head.status_code = 'AVAILABLE'
9547    and  head.MIGRATION_DATE is null;
9548 
9549 l_null          varchar2(30) := null;
9550 l_total_recs    number;
9551 l_field_column_name VARCHAR2(30);
9552 l_count             NUMBER;
9553 
9554 
9555 --Bug 5235979. Bmuthukr
9556 
9557 /* cursor c1 is
9558     SELECT list_rule_id
9559        FROM ams_list_rule_usages
9560        WHERE list_header_id = g_list_header_id;*/
9561 
9562 CURSOR C1 IS
9563 SELECT us.list_rule_id
9564   FROM ams_list_rule_usages us, ams_list_rules_all rules
9565  WHERE us.list_header_id = g_list_header_id
9566    AND us.list_rule_id = rules.list_rule_id
9567    AND rules.list_source_type = l_listheader_rec.list_source_type
9568    AND rules.list_rule_type = 'TARGET';
9569 
9570 -- Ends changes
9571 
9572 l_list_rule_id number := 0;
9573 l_hd_status	varchar(60);
9574 l_entry_count	number;
9575 cursor c_hd_status is
9576 select STATUS_CODE from ams_list_headers_all
9577 where list_header_id = p_list_header_id;
9578 
9579 cursor c_entry_count is
9580 select count(1) from ams_list_entries
9581 where list_header_id = p_list_header_id;
9582 
9583 l_list_field_mapped  varchar2(1);
9584 
9585 cursor c_master_ds_fields_mapped is
9586 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
9587 where hd.list_header_id = p_list_header_id
9588   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9589   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
9590   and fd.FIELD_COLUMN_NAME is NOT NULL;
9591 
9592 cursor c_child_ds_fields_mapped is
9593 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
9594 ams_list_src_type_assocs ats
9595 where hd.list_header_id = p_list_header_id
9596   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9597   and ty.list_source_type_id = ats.master_source_type_id
9598   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
9599   and fd.FIELD_COLUMN_NAME is NOT NULL;
9600 
9601 l_tca_field_mapped  varchar2(1);
9602 
9603 l_no_of_rows   number := 0;
9604 
9605 cursor c_check_gen_mode is
9606 select nvl(no_of_rows_in_list ,0)
9607   from ams_list_headers_all
9608  where list_header_id = g_list_header_id;
9609 
9610 -- SOLIN, bug 4410333
9611 -- check whether datasource is enabled.
9612 cursor c_check_datasource(c_list_header_id NUMBER) is
9613   SELECT a.enabled_flag
9614   FROM ams_list_src_types a,
9615        ams_list_headers_all b
9616   WHERE a.source_type_code = b.list_source_type
9617     AND b.list_header_id = c_list_header_id;
9618 
9619 l_ds_enabled_flag      VARCHAR2(1);
9620 -- SOLIN, end
9621 
9622 cursor c_master_ds_tca_mapped is
9623 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty
9624 where hd.list_header_id = p_list_header_id
9625   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9626   and ty.list_source_type_id = fd.LIST_SOURCE_TYPE_ID
9627   and fd.tca_column_id is NOT NULL;
9628 
9629 cursor c_child_ds_tca_mapped is
9630 select 'Y' from ams_list_src_fields fd, ams_list_headers_all hd, ams_list_src_types ty,
9631 ams_list_src_type_assocs ats
9632 where hd.list_header_id = p_list_header_id
9633   and hd.LIST_SOURCE_TYPE = ty.source_type_code
9634   and ty.list_source_type_id = ats.master_source_type_id
9635   and ats.sub_source_type_id = fd.LIST_SOURCE_TYPE_ID
9636   and fd.tca_column_id is NOT NULL;
9637 
9638 CURSOR c_get_dup_fields(c_list_header_id NUMBER) IS
9639 SELECT min(master_child.field_column_name1) ,count(master_child.field_column_name) from
9640   (
9641   SELECT d.field_column_name field_column_name1,d.field_column_name
9642   FROM ams_list_src_types a,
9643        ams_list_headers_all b,
9644        ams_list_src_fields d
9645   WHERE a.source_type_code = b.list_source_type
9646    and b.list_header_id = p_list_header_id
9647    and d.list_source_type_id = a.list_source_type_id
9648    and d.USED_IN_LIST_ENTRIES = 'Y'
9649   union all
9650    SELECT d.field_column_name field_column_name1,d.field_column_name
9651   FROM ams_list_src_types a,
9652        ams_list_headers_all b,
9653        ams_list_src_fields d,
9654        ams_list_src_type_assocs e
9655   WHERE a.source_type_code = b.list_source_type
9656    and b.list_header_id = p_list_header_id
9657    and e.master_source_type_id = a.list_source_type_id
9658    and d.list_source_type_id = e.sub_source_type_id
9659    and d.USED_IN_LIST_ENTRIES = 'Y'
9660    ) master_child
9661   GROUP BY master_child.field_column_name
9662   having COUNT(master_child.field_column_name) > 1;
9663 l_ctrl_grp_status   VARCHAR2(100);
9664 
9665 --bmuthukr bug 4997699
9666 l_ds_name         varchar2(1000);
9667 l_field_col_name  varchar2(1000);
9668 l_source_col_name varchar2(1000);
9669 
9670 cursor c_get_dup_mapping(p_col_name in varchar2) is
9671 SELECT d.source_column_name, d.field_column_name , d.de_list_source_type_code  stc
9672   FROM ams_list_src_types a,
9673        ams_list_headers_all b,
9674        ams_list_src_fields d
9675  WHERE a.source_type_code = b.list_source_type
9676    and b.list_header_id = g_list_header_id
9677    and d.list_source_type_id = a.list_source_type_id
9678    and d.field_column_name = p_col_name
9679    and d.USED_IN_LIST_ENTRIES = 'Y'
9680 union all
9681 SELECT d.source_column_name, d.field_column_name,  d.de_list_source_type_code stc
9682   FROM ams_list_src_types a,
9683        ams_list_headers_all b,
9684        ams_list_src_fields d,
9685        ams_list_src_type_assocs e
9686  WHERE a.source_type_code = b.list_source_type
9687    and b.list_header_id = g_list_header_id
9688    and e.master_source_type_id = a.list_source_type_id
9689    and d.list_source_type_id = e.sub_source_type_id
9690    and d.field_column_name = p_col_name
9691    and d.USED_IN_LIST_ENTRIES = 'Y';
9692 --
9693 cursor c_check_supp is
9694 select nvl(apply_suppression_flag,'N')
9695   from ams_list_headers_all
9696  where list_header_id = p_list_header_id;
9697 
9698 l_supp_flag   varchar2(1) := 'N';
9699 
9700 BEGIN
9701 
9702 
9703   l_error_position := '<- start List generate ->';
9704   -----------------------------------------------------------------------------
9705   -- g_list_header_id global variable for this session
9706   -- This eliminates the need for passing variables across procedures
9707   -- Particularly for logging debug messages ams_act_logs
9708   -----------------------------------------------------------------------------
9709   g_remote_list           := 'N';
9710   g_remote_list_gen       := 'N';
9711   g_database_link         := ' ';
9712   g_list_header_id        :=  p_list_header_id;
9713 
9714   write_to_act_log(p_msg_data => 'Executing procedure generate_target_group. Target group generation started.',
9715                    p_arc_log_used_by => 'LIST',
9716                    p_log_used_by_id  => p_list_header_id,
9717 		   p_level => 'HIGH');
9718 
9719   --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');
9720 
9721   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');
9722 
9723   -- SOLIN, bug 4410333
9724   l_ds_enabled_flag := 'N';
9725   OPEN c_check_datasource(p_list_header_id);
9726   FETCH c_check_datasource INTO l_ds_enabled_flag;
9727   CLOSE c_check_datasource;
9728 
9729   IF l_ds_enabled_flag = 'N' THEN
9730      write_to_act_log(
9731           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.',
9732           p_arc_log_used_by => 'LIST',
9733           p_log_used_by_id  => p_list_header_id,
9734 	  p_level => 'HIGH');
9735      UPDATE ams_list_headers_all
9736         SET last_generation_success_flag = 'N',
9737             status_code                  = 'FAILED',
9738             user_status_id               = 311,
9739             status_date                  = sysdate,
9740             last_update_date             = sysdate,
9741             main_gen_end_time            = sysdate,
9742             ctrl_status_code             = 'DRAFT',
9743             no_of_rows_in_ctrl_group     = null
9744       WHERE list_header_id               = p_list_header_id;
9745       FND_MSG_PUB.count_and_get(
9746             p_encoded => FND_API.g_false,
9747             p_count   => x_msg_count,
9748             p_data    => x_msg_data
9749       );
9750       x_return_status := FND_API.g_ret_sts_error;
9751       --
9752       logger;
9753       RETURN;
9754   END IF;
9755 -- SOLIN, end
9756 
9757   write_to_act_log(p_msg_data => 'Checking if Master/Child datasource fields are mapped.' ,
9758                    p_arc_log_used_by => 'LIST',
9759                    p_log_used_by_id  => p_list_header_id,
9760                    p_level =>'LOW');
9761 
9762   open  c_master_ds_fields_mapped;
9763   fetch c_master_ds_fields_mapped into l_list_field_mapped;
9764   close c_master_ds_fields_mapped;
9765 
9766   open  c_child_ds_fields_mapped;
9767   fetch c_child_ds_fields_mapped into l_list_field_mapped;
9768   close c_child_ds_fields_mapped;
9769 
9770   if l_list_field_mapped is NULL THEN
9771      write_to_act_log(p_msg_data => 'Master/Child datasource fields are not mapped. Aborting target group generation. ' ,
9772                       p_arc_log_used_by => 'LIST',
9773                       p_log_used_by_id  => p_list_header_id,
9774 	     	      p_level =>'HIGH');
9775 
9776       UPDATE ams_list_headers_all
9777          SET last_generation_success_flag = 'N',
9778              status_code                  = 'FAILED',
9779              user_status_id               = 311,
9780              status_date                  = sysdate,
9781              last_update_date             = sysdate,
9782              main_gen_end_time            = sysdate,
9783              ctrl_status_code             = 'DRAFT',
9784              no_of_rows_in_ctrl_group     = null
9785        WHERE list_header_id               = p_list_header_id;
9786       -- calling logging program
9787         logger;
9788       --
9789       IF FND_API.To_Boolean ( p_commit ) THEN
9790         COMMIT WORK;
9791       END IF;
9792       --Modified by bmuthukr. Bug # 4083665
9793       x_return_status := FND_API.g_ret_sts_error;
9794       --
9795       RETURN;
9796    end if;
9797 
9798 
9799 
9800 
9801   OPEN c_get_dup_fields(p_list_header_id);
9802   FETCH c_get_dup_fields INTO l_field_column_name, l_count;
9803   CLOSE c_get_dup_fields;
9804 
9805   IF l_count>1 THEN
9806      /*DELETE FROM ams_act_logs
9807       WHERE arc_act_log_used_by = 'LIST'
9808         AND act_log_used_by_id  = p_list_header_id ;*/
9809 
9810      write_to_act_log(
9811           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.',
9812           p_arc_log_used_by => 'LIST',
9813           p_log_used_by_id  => p_list_header_id,
9814 	  p_level => 'HIGH');
9815 
9816      --bmuthukr bug 4997699
9817      open c_get_dup_mapping(l_field_column_name);
9818      loop
9819         fetch c_get_dup_mapping into l_source_col_name, l_field_col_name  ,l_ds_name;
9820 	exit when c_get_dup_mapping%notfound;
9821         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');
9822      end loop;
9823      --
9824      UPDATE ams_list_headers_all
9825         SET last_generation_success_flag = 'N',
9826             status_code                  = 'FAILED',
9827             user_status_id               = 311,
9828             status_date                  = sysdate,
9829             last_update_date             = sysdate,
9830             main_gen_end_time            = sysdate,
9831             ctrl_status_code             = 'DRAFT',
9832             no_of_rows_in_ctrl_group     = null
9833       WHERE list_header_id               = g_list_header_id;
9834       --Modified by bmuthukr. Bug # 4083665
9835       x_return_status := FND_API.g_ret_sts_error;
9836       --
9837       logger;
9838       RETURN;
9839   END IF;
9840 
9841   x_return_status := FND_API.G_RET_STS_SUCCESS;
9842 
9843   -- Standard call to check for call compatibility.
9844   IF NOT FND_API.Compatible_API_Call ( l_api_version,
9845                                        p_api_version,
9846                                        l_api_name,
9847                                        G_PKG_NAME)
9848   THEN
9849     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9850   END IF;
9851 
9852   IF FND_API.to_Boolean( p_init_msg_list ) THEN
9853      FND_MSG_PUB.initialize;
9854   END IF;
9855 
9856   -- Checking if Debug is set. If debug is set then log debugging message
9857   IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH)
9858   THEN
9859      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
9860      FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration : Start');
9861      FND_MSG_PUB.Add;
9862   END IF;
9863 
9864   --  Initialize API return status to success
9865   x_return_status := FND_API.G_RET_STS_SUCCESS;
9866 
9867   l_error_position := '<- Init List->';
9868   --These values need to be fetched before calling initialize_list. Changes done by bmuthukr.
9869 
9870 
9871   open c_remote_list;
9872   fetch c_remote_list into g_remote_list,g_database_link;
9873   close c_remote_list;
9874 
9875   if nvl(g_remote_list,'N') = 'N' then
9876      write_to_act_log(p_msg_data => 'Target group is based not on remote datasource.',
9877                       p_arc_log_used_by => 'LIST',
9878                       p_log_used_by_id  => p_list_header_id,
9879    	   	      p_level => 'LOW');
9880   elsif nvl(g_remote_list,'Y') = 'Y' then
9881      write_to_act_log(p_msg_data => 'Target group is based on remote datasource. Database link is  ' ||g_database_link,
9882                       p_arc_log_used_by => 'LIST',
9883                       p_log_used_by_id  => p_list_header_id,
9884    	   	      p_level => 'HIGH');
9885   end if;
9886 
9887   --
9888 
9889   -----------------------------------------------------------------------------
9890   -- Gets list header record details
9891   -- Intialize the record, set the list header id and retrieve the records
9892   -----------------------------------------------------------------------------
9893   write_to_act_log(p_msg_data => 'Calling ams_listheader_pvt to get the header details.' ,
9894                    p_arc_log_used_by => 'LIST',
9895                    p_log_used_by_id  => p_list_header_id,
9896    	   	   p_level => 'LOW');
9897 
9898   ams_listheader_pvt.init_listheader_rec(l_tmp_listheader_rec);
9899   l_tmp_listheader_rec.list_header_id   := p_list_header_id;
9900 
9901   l_error_position := '<- complete rec ->';
9902   ams_listheader_pvt.complete_listheader_rec
9903                    (p_listheader_rec  =>l_tmp_listheader_rec,
9904                     x_complete_rec    =>l_listheader_rec);
9905   -----------------------------------------------------------------------------
9906 
9907   -----------------------------------------------------------
9908   -- Initializes the list header record
9909   -----------------------------------------------------------
9910   l_error_position := '<- Initialize List ->';
9911   write_to_act_log(p_msg_data => 'Calling initialize_list to initialize the list.' ,
9912                    p_arc_log_used_by => 'LIST',
9913                    p_log_used_by_id  => p_list_header_id,
9914    	   	   p_level => 'LOW');
9915 
9916   initialize_List(p_list_header_rec => l_listheader_rec,
9917                   x_msg_count       => x_msg_count,
9918                   x_msg_data        => x_msg_data,
9919                   x_return_status   => x_return_status);
9920 
9921   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9922      write_to_act_log('Error while executing procedure initialize_list', 'LIST', g_list_header_id,'HIGH');
9923      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9924   else
9925      write_to_act_log('Target group initialized.' ,'LIST',p_list_header_id,'HIGH');
9926   end if;
9927 
9928   IF x_return_status = FND_API.g_ret_sts_error THEN
9929      RAISE FND_API.g_exc_error;
9930   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
9931      RAISE FND_API.g_exc_unexpected_error;
9932   END IF;
9933 -- -------------------------------------------------------------------------
9934   if g_remote_list = 'Y' then
9935        remote_list_gen(p_list_header_id  => p_list_header_id,
9936                      x_return_status   => x_return_status,
9937                      x_msg_count       => x_msg_count,
9938                      x_msg_data        => x_msg_data,
9939                      x_remote_gen      => g_remote_list_gen);
9940      if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
9941         write_to_act_log('Error in executing remote_list_gen procedure', 'LIST', g_list_header_id,'HIGH');
9942         write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
9943      else
9944         write_to_act_log(p_msg_data => 'remote_list_gen procedure executed successfully.' ,
9945                          p_arc_log_used_by => 'LIST',
9946                          p_log_used_by_id  => p_list_header_id,
9947  			 p_level => 'LOW');
9948      end if;
9949   end if;
9950 
9951   if l_listheader_rec.generation_type = 'UPD' then
9952      open c_check_gen_mode;
9953      fetch c_check_gen_mode into l_no_of_rows;
9954      close c_check_gen_mode;
9955 
9956      if l_no_of_rows = 0 then
9957         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');
9958         UPDATE ams_list_headers_all
9959            SET last_generation_success_flag = 'N',
9960                status_code                  = 'FAILED',
9961                ctrl_status_code             = 'DRAFT',
9962                user_status_id               = 311,
9963                status_date                  = sysdate,
9964                last_update_date             = sysdate,
9965                main_gen_end_time            = sysdate
9966          WHERE list_header_id               = g_list_header_id;
9967          x_return_status := FND_API.g_ret_sts_error;
9968          logger;
9969          RETURN;
9970       end if;
9971    end if;
9972 
9973 --In Update mode need to update only the enabled entries from R12.
9974 /*
9975   if l_listheader_rec.generation_type = 'UPD' then
9976      write_to_act_log('Target group is generated in UPDATE mode', 'LIST', g_list_header_id,'HIGH');
9977        update ams_list_entries
9978        set newly_updated_flag = 'N' , enabled_flag = 'Y'
9979          where list_header_id = l_listheader_rec.list_header_id;
9980 */
9981    /********************************************************************
9982     Dynamic procedure will update the list from the remote instance in
9983     case of remote list
9984    *********************************************************************/
9985 /*     if g_remote_list = 'Y' then
9986         write_to_act_log(p_msg_data => 'Updating the target group in remote instance. ' ,
9987                          p_arc_log_used_by => 'LIST',
9988                          p_log_used_by_id  => p_list_header_id,
9989 	   	         p_level => 'LOW');
9990       execute immediate
9991       'BEGIN
9992       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
9993       ' END;'
9994       using  '1',
9995              l_null,
9996              'T',
9997              l_null,
9998              OUT x_return_status,
9999              OUT x_msg_count,
10000              OUT x_msg_data,
10001              l_listheader_rec.list_header_id,
10002              l_null,
10003              l_null,
10004              OUT l_total_recs,
10005              'UPDATE';
10006        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10007           write_to_act_log('Error in executing remote procedure', 'LIST', g_list_header_id,'HIGH');
10008           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10009        else
10010           write_to_act_log(p_msg_data => 'Target group updated in remote instance. ' ,
10011                            p_arc_log_used_by => 'LIST',
10012                            p_log_used_by_id  => p_list_header_id,
10013 	  		   p_level => 'LOW');
10014        end if;
10015      end if;
10016   end if;
10017 */
10018   update_remote_list_header(g_list_header_id,x_return_status,x_msg_count,x_msg_data);
10019   if l_listheader_rec.generation_type = 'UPD' then
10020      l_error_position := '<- Get_list_entry_data inside deduplication ->';
10021    -- For bug 5216890
10022    -- if g_remote_list <> 'Y' then
10023    --
10024    -- This will not be performed for the remote list generation
10025    --
10026      write_to_act_log('Target group is generated in UPDATE mode in local instance.', 'LIST', g_list_header_id,'HIGH');
10027      GET_LIST_ENTRY_DATA(
10028                  p_list_header_id =>l_listheader_rec.list_header_id,
10029                   x_return_status => x_return_status);
10030      IF x_return_status = FND_API.g_ret_sts_error THEN
10031         RAISE FND_API.g_exc_error;
10032      ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
10033         RAISE FND_API.g_exc_unexpected_error;
10034      END IF;
10035    --end if;
10036   -- END IF;
10037   else
10038   l_error_position := '<- Process List Actions  ->';
10039   write_to_act_log('Target group is generated in '||l_listheader_rec.generation_type||' mode.','LIST', g_list_header_id,'HIGH');
10040   write_to_act_log('Calling process_tar_actions to generate Target group.', 'LIST', g_list_header_id,'LOW');
10041   process_tar_Actions(p_action_used_by_id => l_listheader_rec.list_header_id,
10042                        p_action_used_by    => 'LIST',
10043                        p_log_flag          => l_listheader_rec.enable_log_flag,
10044                        x_return_status     => x_return_status,
10045                        x_msg_count         => x_msg_count,
10046                        x_msg_data          => x_msg_data);
10047      if x_return_status = 'E' then
10048         logger;
10049         commit;
10050         return;
10051      end if;
10052 
10053   END IF;
10054 
10055   if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10056      write_to_act_log('Error in generating list', 'LIST', g_list_header_id,'HIGH');
10057      write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10058   end if;
10059 
10060   IF x_return_status = FND_API.g_ret_sts_error THEN
10061      RAISE FND_API.g_exc_error;
10062   ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
10063      RAISE FND_API.g_exc_unexpected_error;
10064   END IF;
10065 
10066 
10067   -- added for R12. bmuthukr
10068   if l_listheader_rec.generation_type = 'STANDARD' then
10069      if g_remote_list_gen = 'N' then
10070         write_to_act_log('Identifying duplicate records(based on party id) in the target group.','LIST',g_list_header_id,'HIGH');
10071         UPDATE ams_list_entries a
10072            SET a.enabled_flag  = 'N',
10073                a.marked_as_duplicate_flag = 'Y'
10074          WHERE a.list_header_id = p_list_header_id
10075            AND a.enabled_flag = 'Y'
10076            AND a.rowid >  (SELECT min(b.rowid)
10077                              from ams_list_entries  b
10078                             where b.list_header_id = p_list_header_id
10079                               and b.party_id = a.party_id
10080                               and b.enabled_flag = 'Y'
10081                            );
10082         write_to_act_log('No of duplicates identified.'||sql%rowcount,'LIST',g_list_header_id,'HIGH');
10083         open g_initial_count;
10084         fetch g_initial_count into g_no_of_rows_ini_selected;
10085         close g_initial_count;
10086      end if;
10087   end if;
10088 
10089   --
10090 
10091 --** DE DUPLICATION FOR TG START **--
10092    write_to_act_log('Checking if dedupe is requested for target group. ', 'LIST', p_list_header_id,'HIGH');
10093    if l_listheader_rec.generation_type = 'STANDARD' then
10094       open c1;
10095       fetch c1 into l_list_rule_id ;
10096       close c1;
10097 
10098       IF (l_list_rule_id <> 0 ) THEN                                  -- NEW logic is base on l_list_rule_id (dedupe rule)
10099          write_to_act_log('De duplication requested for this target group', 'LIST', g_list_header_id,'LOW');
10100          l_error_position := '<- de dupe ->';
10101          if g_remote_list_gen = 'N' then
10102             /* For local Target Group generation */
10103             write_to_act_log('Calling ams_listdedupe_pvt for deduplication.', 'LIST', g_list_header_id,'HIGH');
10104             l_no_of_duplicates := AMS_LISTDEDUPE_PVT.DEDUPE_LIST
10105                              (p_list_header_id               => p_list_header_id,
10106                               p_enable_word_replacement_flag => 'Y', -- l_listheader_rec.enable_word_replacement_flag,
10107                               p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
10108                               p_object_name    => 'AMS_LIST_ENTRIES');
10109             write_to_act_log('Deduplication done for target group.', 'LIST', g_list_header_id,'HIGH');
10110          else
10111             /* For Remote Target Group generation */
10112             write_to_act_log('Calling Execute_Remote_Dedupe_List for deduplication in remote instance.', 'LIST', g_list_header_id,'LOW');
10113             Execute_Remote_Dedupe_List
10114                              (p_list_header_id               => p_list_header_id,
10115                               p_enable_word_replacement_flag => 'Y', -- l_listheader_rec.enable_word_replacement_flag,
10116                               p_send_to_log    => 'Y', -- l_listheader_rec.enable_log_flag,
10117                               p_object_name    => 'AMS_LIST_ENTRIES');
10118          end if;
10119       END IF; -- for l_list_rule_id
10120    end if;
10121    -- Call to suppresion to be done..
10122    --** DE DUPLICATION FOR TG END   **--
10123    write_to_Act_log('Generation type is '||l_listheader_rec.generation_type,'LIST',g_list_header_id,'LOW');
10124    if l_listheader_rec.generation_type in ('STANDARD','INCREMENTAL') then
10125       open c_check_supp;
10126       fetch c_check_supp into l_supp_flag;
10127       close c_check_supp;
10128       write_to_Act_log('Suppression flag is '||l_supp_flag,'LIST',g_list_header_id,'LOW');
10129       if nvl(l_supp_flag,'N') = 'Y' then
10130          write_to_Act_log('Calling suppression api','LIST',g_list_header_id,'LOW');
10131          ams_act_list_pvt.check_supp(p_list_used_by    =>  l_listheader_rec.arc_list_used_by,
10132                                      p_list_used_by_id =>  l_listheader_rec.list_used_by_id,
10133                                      p_list_header_id  =>  l_listheader_rec.list_header_id,
10134   				     x_return_status   =>  x_return_status,
10135                                      x_msg_count       =>  x_msg_count,
10136                                      x_msg_data        =>  x_msg_data);
10137 
10138       end if;
10139 
10140       if nvl(g_remote_list_gen,'N') = 'N' then
10141          write_to_Act_log('Calling apply_size_reduction procedure','LIST',g_list_header_id,'LOW');
10142          AMS_List_Options_Pvt.apply_size_reduction(p_list_header_id => g_list_header_id ,
10143                                                    p_log_level      => g_log_level,
10144                                                    p_msg_tbl        => g_msg_tbl_opt,
10145                                                    x_return_status  => x_return_status,
10146                                                    x_msg_count      => x_msg_count,
10147                                                    x_msg_data       => x_msg_data);
10148          if g_msg_tbl_opt.count > 0 then
10149             for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
10150 	    loop
10151 	       write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
10152                g_message_table(g_count) := g_msg_tbl_opt(I);
10153                g_date(g_count) := sysdate;
10154                g_count   := g_count + 1;
10155             end loop;
10156             g_msg_tbl_opt.delete;
10157          end if;
10158          write_to_Act_log('apply_size_reduction procedure executed successfully','LIST',g_list_header_id,'LOW');
10159       else
10160          write_to_Act_log('Calling apply_size_reduction procedure in the remote database','LIST',g_list_header_id,'LOW');
10161          execute immediate
10162          'BEGIN
10163           AMS_LIST_OPTIONS_PVT.apply_size_reduction'||'@'||g_database_link||'(:1,:2,:3,:4,:5)'||';'||
10164          ' END;'
10165          using g_list_header_id,
10166          'NULL',
10167 	 out x_return_status,
10168          out x_msg_count,
10169          out x_msg_data;
10170          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10171             write_to_act_log('Error while executing apply_size_reduction in the remote database.', 'LIST', g_list_header_id,'HIGH');
10172             write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10173          else
10174             write_to_act_log('apply_size_reduction procedure executed successfully in the remote database', 'LIST', g_list_header_id,'HIGH');
10175          end if;
10176       end if;
10177    end if;
10178 
10179    -- Call to CG
10180    if l_listheader_rec.generation_type = 'STANDARD' then
10181       --if nvl(l_listheader_rec.generate_control_group_flag,'N') <> 'N' then--for now will have to change it later lpo
10182          if nvl(g_remote_list_gen,'N') = 'N' then
10183             write_to_Act_log('Calling control_group_generation procedure ','LIST',g_list_header_id,'LOW');
10184 	    AMS_List_Options_Pvt.Control_Group_Generation(p_list_header_id  => g_list_header_id,
10185   	                                                  p_log_level       => g_log_level,
10186                                                           p_msg_tbl         => g_msg_tbl_opt,
10187                                                           x_ctrl_grp_status => l_ctrl_grp_status,
10188 		                                          x_return_status   => x_return_status,
10189                                                           x_msg_count       => x_msg_count,
10190                                                           x_msg_data        => x_msg_data);
10191 
10192             if g_msg_tbl_opt.count > 0 then
10193                for i in g_msg_tbl_opt.first .. g_msg_tbl_opt.last
10194 	       loop
10195    	          write_to_Act_log(g_msg_tbl_opt(I),'LIST',g_list_header_id,'HIGH');
10196                   --g_message_table(g_count) := g_msg_tbl_opt(I);
10197                   --g_date(g_count) := sysdate;
10198                   --g_count   := g_count + 1;
10199                end loop;
10200 	       g_msg_tbl_opt.delete;
10201             end if;
10202             write_to_act_log('Control group generated successfully', 'LIST', g_list_header_id,'HIGH');
10203          else
10204             write_to_Act_log('Calling control_group_generation procedure in the remote database','LIST',g_list_header_id,'LOW');
10205             execute immediate
10206             'BEGIN
10207             AMS_LIST_OPTIONS_PVT.Control_Group_Generation'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6)'||';'||
10208             ' END;'
10209             using g_list_header_id,
10210                   'NULL',
10211                   out l_ctrl_grp_status,
10212                   out x_return_status,
10213 		  out x_msg_count,
10214                   out x_msg_data;
10215          end if;
10216          if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10217             write_to_act_log('Error while generating control group in the remote database.', 'LIST', g_list_header_id,'HIGH');
10218             write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10219          end if;
10220 
10221       --end if;
10222    end if;
10223 
10224 
10225    if g_remote_list = 'Y' then
10226        -- ------------------------------------------------
10227        write_to_act_log(p_msg_data => 'Checking Master/Child datasource fields are mapped with TCA fields.',
10228                                        p_arc_log_used_by => 'LIST',
10229                                        p_log_used_by_id  => p_list_header_id,
10230 	  			       p_level => 'LOW');
10231        open  c_master_ds_tca_mapped;
10232        fetch c_master_ds_tca_mapped into l_tca_field_mapped;
10233        close c_master_ds_tca_mapped;
10234 
10235        open  c_child_ds_tca_mapped;
10236        fetch c_child_ds_tca_mapped into l_tca_field_mapped;
10237        close c_child_ds_tca_mapped;
10238 
10239        if l_tca_field_mapped is NULL THEN
10240           write_to_act_log(p_msg_data => 'Master/Child datasource fields are NOT mapped with TCA fields. Aborting target group generation.',
10241                    p_arc_log_used_by => 'LIST',
10242                    p_log_used_by_id  => p_list_header_id,
10243 		   p_level=>'HIGH');
10244           write_to_act_log(p_msg_data => 'Deleting entries in the remote instance. Calling remote procedure with process type as DELETE_LIST_ENTRIES.' ,
10245                       p_arc_log_used_by => 'LIST',
10246                       p_log_used_by_id  => p_list_header_id,
10247 		      p_level=>'LOW');
10248           execute immediate
10249           'BEGIN
10250 	   AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10251 	  ' END;'
10252 	      using  '1',
10253               l_null,
10254               'T',
10255               l_null,
10256               OUT x_return_status,
10257               OUT x_msg_count,
10258               OUT x_msg_data,
10259               p_list_header_id,
10260               l_null,
10261               l_null,
10262               OUT l_total_recs,
10263               'DELETE_LIST_ENTRIES';
10264 
10265           if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10266              write_to_act_log('Error in executing remote procedure for deleting target group entries', 'LIST', g_list_header_id,'HIGH');
10267              write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10268           end if;
10269 
10270           UPDATE ams_list_headers_all
10271              SET last_generation_success_flag = 'N',
10272 	         NO_OF_ROWS_ACTIVE            = null,
10273 	         NO_OF_ROWS_IN_LIST           = null,
10274                  status_code                  = 'FAILED',
10275                  user_status_id               = 311,
10276                  status_date                  = sysdate,
10277                  last_update_date             = sysdate,
10278                  main_gen_end_time            = sysdate,
10279                  ctrl_status_code             = 'DRAFT',
10280   	         no_of_rows_in_ctrl_group     = null
10281            WHERE list_header_id               = p_list_header_id;
10282     -- calling logging program
10283       logger;
10284      RETURN;
10285 end if;
10286 
10287   -- ------------------------------------------------
10288 
10289        open c_hd_status;
10290        fetch c_hd_status into l_hd_status;
10291        close c_hd_status;
10292 
10293 /*********************** changed by savio per bug 3817650 *************/
10294 /* if target group has been generatad locally there is no need to migrate it */
10295 
10296       if g_remote_list_gen = 'Y' then
10297        write_to_act_log('Status of the list before migrating to local instance : '||l_hd_status, 'LIST', p_list_header_id,'LOW');
10298        migrate_lists(p_list_header_id);
10299        write_to_act_log('List migrated to local instance.', 'LIST', p_list_header_id,'LOW');
10300       end if;
10301 /*********************** changed by savio per bug 3817650 *************/
10302 
10303 
10304 
10305        open c_hd_status;
10306        fetch c_hd_status into l_hd_status;
10307        close c_hd_status;
10308 	if l_hd_status = 'AVAILABLE'  then
10309        UPDATE ams_list_headers_all
10310        SET     status_code      = 'GENERATING',
10311                user_status_id   = 302
10312        WHERE  list_header_id    = p_list_header_id;
10313     -- Added for cancel list gen as it prevents parallel update- Raghu
10314     -- of list headers when cancel button is pressed
10315       commit;
10316 
10317     end if;
10318 
10319        open c_entry_count;
10320        fetch c_entry_count into l_entry_count;
10321        close c_entry_count;
10322 
10323        write_to_act_log('No of entries after migration is '||to_char(l_entry_count), 'LIST', p_list_header_id,'LOW');
10324        if l_listheader_rec.generation_type <> 'UPD' then
10325           write_to_act_log('Calling tca_upload_process to upload data in TCA.','LIST', p_list_header_id,'HIGH');
10326           tca_upload_process
10327                 (p_list_header_id  ,
10328                  'Y',      -- p_log_flag,
10329                  x_return_status ,
10330                  x_msg_count     ,
10331                  x_msg_data      );
10332        end if;
10333        if nvl(x_return_status,'S') in ('E','U') then -- resulted in error.
10334           write_to_act_log('Error while uploading data in TCA.', 'LIST', g_list_header_id,'HIGH');
10335           write_to_act_log('Error '||x_msg_data , 'LIST', g_list_header_id,'HIGH');
10336        else
10337           write_to_act_log('TCA upload process completed successfully', 'LIST', g_list_header_id,'HIGH');
10338        end if;
10339 
10340     END if;
10341     --Code movded from tg api.
10342 /*    UPDATE ams_list_entries set
10343            source_code = l_source_code    ,
10344            arc_list_used_by_source = p_list_used_by ,
10345            source_code_for_id = p_list_used_by_id
10346      where list_header_id = g_list_header_id ;
10347 
10348    AMS_LISTGENERATION_PKG.Update_List_Dets(g_list_header_id ,x_return_status ) ;
10349   */
10350   -- calling logging program
10351   write_to_act_log('Execution of procedure generate_target_group completed. Target group generated successfully ','LIST', g_list_header_id, 'HIGH');
10352   --logger;  -- will be called from ams_act_list_pvt proc.
10353   --
10354   -- END of API body.
10355   --
10356   -- Standard check of p_commit.
10357 
10358   IF FND_API.To_Boolean ( p_commit ) THEN
10359      COMMIT WORK;
10360   END IF;
10361 
10362   -- Success Message
10363   -- MMSG
10364   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_SUCCESS)
10365   --THEN
10366   FND_MESSAGE.Set_Name('AMS', 'API_SUCCESS');
10367   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List');
10368   FND_MSG_PUB.Add;
10369   --END IF;
10370 
10371 
10372   x_return_status := FND_API.G_RET_STS_SUCCESS;
10373   --IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH)
10374   --THEN
10375   FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10376   FND_MESSAGE.Set_Token('ROW', 'AMS_ListGeneration_PKG.Generate_List: END');
10377   FND_MSG_PUB.Add;
10378   --END IF;
10379       FND_MSG_PUB.count_and_get(
10380             p_encoded => FND_API.g_false,
10381             p_count   => x_msg_count,
10382             p_data    => x_msg_data
10383       );
10384 
10385 EXCEPTION
10386    WHEN FND_API.G_EXC_ERROR THEN
10387   -- calling logging program
10388   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10389 
10390         UPDATE ams_list_headers_all
10391         SET    last_generation_success_flag = 'N',
10392                status_code                  = 'FAILED',
10393                user_status_id               = 311,
10394                status_date                  = sysdate,
10395                last_update_date             = sysdate,
10396                main_gen_end_time            = sysdate,
10397                ctrl_status_code             = 'DRAFT',
10398 	       no_of_rows_in_ctrl_group     = null
10399         WHERE  list_header_id               = p_list_header_id;
10400 
10401         logger;
10402 
10403      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List: '||
10404            --l_error_position||sqlerrm||sqlcode);
10405      -- Check if reset of the status is required
10406      x_return_status := FND_API.G_RET_STS_ERROR ;
10407       FND_MSG_PUB.count_and_get(
10408             p_encoded => FND_API.g_false,
10409             p_count   => x_msg_count,
10410             p_data    => x_msg_data);
10411 
10412   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
10413   -- calling logging program
10414   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10415 
10416         UPDATE ams_list_headers_all
10417         SET    last_generation_success_flag = 'N',
10418                status_code                  = 'FAILED',
10419                user_status_id               = 311,
10420                last_update_date             = sysdate,
10421                status_date                  = sysdate,
10422                main_gen_end_time            = sysdate,
10423                ctrl_status_code             = 'DRAFT',
10424 	       no_of_rows_in_ctrl_group     = null
10425         WHERE  list_header_id               = p_list_header_id;
10426         logger;
10427      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List:'||
10428                       --l_error_position||sqlerrm||sqlcode);
10429      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10430      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
10431      FND_MSG_PUB.Add;
10432      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10433       FND_MSG_PUB.count_and_get(
10434             p_encoded => FND_API.g_false,
10435             p_count   => x_msg_count,
10436             p_data    => x_msg_data);
10437 
10438   WHEN OTHERS THEN
10439   -- calling logging program
10440   write_to_act_log('Error while executing procedure generate_target_group for generating target group.', 'LIST', g_list_header_id,'HIGH');
10441 
10442         UPDATE ams_list_headers_all
10443         SET    last_generation_success_flag = 'N',
10444                status_code                  = 'FAILED',
10445                user_status_id               = 311,
10446                last_update_date             = sysdate,
10447                status_date                  = sysdate,
10448                main_gen_end_time            = sysdate,
10449                ctrl_status_code             = 'DRAFT',
10450 	       no_of_rows_in_ctrl_group     = null
10451         WHERE  list_header_id               = p_list_header_id;
10452 
10453         logger;
10454 
10455      --write_to_act_log('Error: AMS_ListGeneration_PKG.Generate_List:'||
10456                        --l_error_position||sqlerrm||sqlcode);
10457      FND_MESSAGE.set_name('AMS', 'API_DEBUG_MESSAGE');
10458      FND_MESSAGE.Set_Token('ROW', sqlerrm||' '||sqlcode);
10459      FND_MSG_PUB.Add;
10460      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
10461       FND_MSG_PUB.count_and_get(
10462             p_encoded => FND_API.g_false,
10463             p_count   => x_msg_count,
10464             p_data    => x_msg_data);
10465 END GENERATE_TARGET_GROUP;
10466 
10467 -- -----------------------------------------------------------------------
10468 PROCEDURE Execute_Remote_Dedupe_List
10469  (p_list_header_id        AMS_LIST_HEADERS_ALL.LIST_HEADER_ID%TYPE
10470  ,p_enable_word_replacement_flag
10471                           AMS_LIST_HEADERS_ALL.ENABLE_WORD_REPLACEMENT_FLAG%TYPE
10472  ,p_send_to_log           VARCHAR2 := 'N'
10473  ,p_object_name           VARCHAR2 := 'AMS_LIST_ENTRIES'
10474  )
10475 IS
10476     -- the set of rules associated with a list.
10477     CURSOR c_list_rules (my_list_header_id IN NUMBER)
10478     IS SELECT list_rule_id
10479        FROM ams_list_rule_usages
10480        WHERE list_header_id = my_list_header_id
10481        ORDER BY priority;
10482 
10483     -- the list of fields for the list rule which are used to generate the key.
10484     CURSOR c_rule_fields
10485            (my_list_rule_id IN
10486             ams_list_rules_all.list_rule_id%TYPE)
10487     IS
10488         SELECT field_table_name,
10489                field_column_name,
10490                substring_length,
10491                word_replacement_code
10492         FROM ams_list_rule_fields
10493         WHERE list_rule_id = my_list_rule_id;
10494 
10495     -- perform a check to see if this list has been deduped already.
10496     CURSOR c_deduped_before (my_list_header_id IN NUMBER)
10497     IS
10498         SELECT last_deduped_by_user_id
10499         FROM ams_list_headers_all
10500         WHERE list_header_id = my_list_header_id;
10501 
10502     -- get a distinct list of merge keys for the list and a
10503     -- count of the occurance of each key
10504     -- we also exclude any records where the dedupe flag is already set.
10505     CURSOR c_dedupe_keys (my_list_header_id IN NUMBER)
10506     IS
10507         SELECT DISTINCT dedupe_key, COUNT (dedupe_key)
10508         FROM ams_list_entries
10509          WHERE list_header_id = my_list_header_id
10510          GROUP BY dedupe_key;
10511 
10512 
10513     CURSOR c_minimum_rank (my_list_header_id IN NUMBER)
10514     IS
10515     SELECT min(b.rank) FROM ams_list_select_actions b
10516     WHERE b.action_used_by_id = p_list_header_id
10517     and b.arc_action_used_by = 'LIST'
10518     GROUP BY b.rank;
10519 
10520 
10521     l_sql_stmt1         VARCHAR2(10000);
10522     l_sql_stmt2         VARCHAR2(10000);
10523 
10524     l_fields            VARCHAR2(10000);
10525     l_no_of_masters     NUMBER := 0;
10526     l_list_rule_id      ams_list_rules_all.list_rule_id%TYPE;
10527     l_last_dedupe_by    ams_list_headers_all.last_deduped_by_user_id%TYPE;
10528     l_dedupe_key        ams_list_entries.dedupe_key%TYPE;
10529     l_dedupe_key_count  NUMBER;
10530     l_rank_count        NUMBER;
10531     i                   BINARY_INTEGER := 1;
10532 
10533     TYPE rule_details
10534     IS TABLE OF c_rule_fields%ROWTYPE
10535     INDEX BY BINARY_INTEGER;
10536 
10537     list_rules          rule_details;
10538     empty_list_rules    rule_details;
10539 
10540     l_null		varchar(30) := NULL;
10541     l_total_dup_recs	number;
10542     l_return_status     varchar(1);
10543     l_msg_count		number;
10544     l_msg_data		varchar(2000);
10545     l_rank              number := 0;
10546 BEGIN
10547     write_to_act_log('Executing procedure execute_remote_dedupe_list', 'LIST', p_list_header_id,'LOW');
10548     IF (p_object_name = 'AMS_LIST_ENTRIES') THEN
10549         l_sql_stmt1 := 'update ams_list_entries set dedupe_key = ';
10550     END IF;
10551 
10552     --performing check to see if this list has been deduped before.
10553     OPEN c_deduped_before (p_list_header_id);
10554     FETCH c_deduped_before INTO l_last_dedupe_by;
10555     CLOSE c_deduped_before;
10556 
10557     Write_To_Act_Log ('Dedupe started on ' ||TO_CHAR (SYSDATE, 'DD-MON-RRRR HH24:MM:SS'),'LIST',p_list_header_id,'LOW');
10558         IF  (p_enable_word_replacement_flag = 'Y') THEN
10559             Write_To_Act_Log ('Word replacement flag is set ' ,'LIST',p_list_header_id,'LOW' );
10560         END IF;
10561 
10562     -- we must ensure that this flag gets reset to NULL for the list to
10563     -- ensure accurate results.
10564     -- if a dedupe has never been perfomed then this field will contains
10565     -- NULLS and there is no
10566     -- need to perform this update
10567     IF  (l_last_dedupe_by IS NOT NULL) THEN
10568     /*    UPDATE ams_list_entries
10569            SET dedupe_key = NULL
10570          WHERE list_header_id = p_list_header_id; */
10571       write_to_act_log('Executing remote procedure with process type as DEDUPE1', 'LIST', p_list_header_id,'HIGH');
10572       execute immediate
10573       'BEGIN
10574        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10575       ' END;'
10576       using  '1',
10577              l_null,
10578              'T',
10579              l_null,
10580              OUT l_return_status,
10581              OUT l_msg_count,
10582              OUT l_msg_data,
10583              p_list_header_id,
10584              'UPDATE ams_list_entries SET dedupe_key = NULL WHERE list_header_id = '||to_char(p_list_header_id),
10585              l_null,
10586              l_null,
10587              OUT l_total_dup_recs,
10588              'DEDUPE1';
10589         if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10590            write_to_act_log('Error in executing remote procedure for dedupe.', 'LIST', p_list_header_id,'HIGH');
10591            write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10592         else
10593            write_to_act_log('Remote procedure executed successfully. Dedupe key set to null.', 'LIST', p_list_header_id,'LOW');
10594         end if;
10595 
10596     END IF;
10597 
10598     -- checking to see if there are any List Source Ranks associated
10599     -- with the List.
10600     SELECT COUNT (rank)
10601       INTO l_rank_count
10602       FROM ams_list_select_actions
10603      WHERE action_used_by_id = p_list_header_id
10604        and arc_action_used_by = 'LIST';
10605 
10606 --   Write_To_Act_Log (' # of Ranks for this list = ' ||TO_CHAR (l_rank_count),'LIST',p_list_header_id ,'LOW');
10607 
10608     --getting the list rules for the list.
10609     OPEN c_list_rules (p_list_header_id);
10610     LOOP
10611         FETCH c_list_rules INTO l_list_rule_id;
10612         Write_To_Act_Log ('List rule id is  ' ||TO_CHAR (l_list_rule_id), 'LIST',p_list_header_id,'LOW' );
10613         IF (c_list_rules%notfound) THEN
10614   	   IF  (p_send_to_log = 'Y') THEN
10615             Write_To_Act_Log ('No more rules associated with the List','LIST',p_list_header_id ,'LOW');
10616 	      NULL;
10617             END IF;
10618 
10619             IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error)
10620             THEN
10621                  FND_MESSAGE.set_name('AMS', 'AMS_LIST_NO_LIST_RULE');
10622                  FND_MSG_PUB.add;
10623             END IF;
10624 
10625             CLOSE c_list_rules;
10626             return;
10627         END IF;
10628 
10629       IF  (c_list_rules%rowcount > 1) THEN
10630           --we have more than one rule for this list
10631           --we must ensure that the key gets reset to NULL for the list to
10632           -- ensure accurate results.
10633           -- removed khung 07/07/1999
10634 /*
10635          IF (p_object_name = 'AMS_LIST_ENTRIES') THEN
10636              UPDATE ams_list_entries
10637              SET dedupe_key = NULL
10638              WHERE list_header_id = p_list_header_id
10639              AND marked_as_duplicate_flag IS NULL;
10640              COMMIT;
10641          END IF;
10642 */
10643          Write_To_Act_Log ( 'More than one rule cannot be applied to the list','LIST',p_list_header_id ,'HIGH');
10644          IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
10645             FND_MESSAGE.set_name('AMS', 'AMS_LIST_ONLY_ONE_LIST_RULE');
10646             FND_MSG_PUB.add;
10647          END IF;
10648 
10649          CLOSE c_list_rules;
10650          RETURN;
10651       END IF; -- End of if for more than 1 rule count
10652       Write_To_Act_Log ('Fetching attributes associated with this rule','LIST',p_list_header_id,'LOW' );
10653         --fetch the rule entries associated with this list.
10654         OPEN c_rule_fields (l_list_rule_id);
10655         LOOP
10656             FETCH c_rule_fields INTO
10657                          list_rules (i).field_table_name,
10658                          list_rules (i).field_column_name,
10659                          list_rules (i).substring_length,
10660                          list_rules (i).word_replacement_code;
10661             EXIT WHEN c_rule_fields%notfound;
10662 
10663             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');
10664             -- if the enable word replacement flag is set we construct the sql
10665             -- to call the filter word function.
10666             IF  (p_enable_word_replacement_flag = 'Y') THEN
10667                Write_To_Act_Log ('Calling replact_word procedure for word replacement','LIST',p_list_header_id,'LOW' );
10668                 l_fields :=
10669                   l_fields ||
10670                       'AMS_Remote_ListGen_PKG.replace_word(' ||
10671                       upper(list_rules (i).field_column_name) ||
10672                       ',' ||
10673                       '''' ||
10674                       list_rules (i).word_replacement_code||
10675                       '''' ||
10676                       ')' ||
10677                       '||' ||
10678                       '''' ||
10679                       '.' ||
10680                       '''' ||
10681                       '||';
10682 
10683             ELSE
10684             --no substr specified for the rule field.
10685                 IF  (list_rules (i).substring_length IS NULL)
10686                 THEN
10687                     l_fields :=
10688                       l_fields ||
10689                       'upper(' ||
10690                       list_rules (i).field_column_name ||
10691                       ')||' ||
10692                       '''' ||
10693                       '.' ||
10694                       '''' ||
10695                       '||';
10696                 ELSE
10697                     l_fields :=
10698                       l_fields ||
10699                       'upper(substr(' ||
10700                       list_rules (i).field_column_name ||
10701                       ',1,' ||
10702                       TO_CHAR (list_rules (i).substring_length) ||
10703                       '))||' ||
10704                       '''' ||
10705                       '.' ||
10706                       '''' ||
10707                       '||';
10708                 END IF;
10709             END IF;
10710 
10711             i := i + 1;
10712 
10713         END LOOP;   --c_rule_fields
10714 
10715         i := 1;   --reseting to one.
10716         list_rules := empty_list_rules;   --re-initializing because we can have many rules.
10717         CLOSE c_rule_fields;
10718 
10719         -- removing the last '.' from the string as this will cause an invalid syntax error
10720         -- in the query.
10721 
10722         l_fields := SUBSTR (l_fields, 1, LENGTH (l_fields) - 7);
10723 
10724         l_sql_stmt2 := l_sql_stmt1;
10725         l_sql_stmt2 := l_sql_stmt2 ||
10726                 l_fields ||
10727                 ' where list_header_id =' ||
10728                 TO_CHAR (p_list_header_id);
10729 
10730 
10731         Write_To_Act_Log ('SQL generated ' ||l_sql_stmt2 ,'LIST',p_list_header_id,'LOW' );
10732         --09/27/2000 vbhandar,modified Execute immediate , INTO should only be used for single row queries
10733         --notice that l_no_of_masters is not really used, left it there to keep the signature of the function
10734         --Dedupe_LIST unchanged.
10735 --         EXECUTE IMMEDIATE l_sql_stmt2;
10736 
10737       write_to_act_log('Calling remote procedure with process type as DEDUPE2', 'LIST', p_list_header_id,'LOW');
10738       execute immediate
10739       'BEGIN
10740        AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10741       ' END;'
10742       using  '1',
10743              l_null,
10744              'T',
10745              l_null,
10746              OUT l_return_status,
10747              OUT l_msg_count,
10748              OUT l_msg_data,
10749              p_list_header_id,
10750              l_sql_stmt2,
10751              l_null,
10752              OUT l_total_dup_recs,
10753              'DEDUPE2';
10754     if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10755        write_to_act_log('Error in executing remote procedure with process type as DEDUPE2', 'LIST', p_list_header_id,'HIGH');
10756        write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10757     else
10758        write_to_act_log('Remote procedure executed successfully for dedupe', 'LIST', p_list_header_id,'LOW');
10759     end if;
10760 
10761       open c_minimum_rank(p_list_header_id);
10762       fetch c_minimum_rank into l_rank;
10763       close c_minimum_rank;
10764 
10765       write_to_act_log('Calling remote procedure with process type as DEDUPE3 for identifying duplicates based on rank', 'LIST', p_list_header_id,'LOW');
10766       execute immediate
10767       'BEGIN
10768       AMS_Remote_ListGen_PKG.remote_list_gen'||'@'||g_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12)'||';'||
10769       ' END;'
10770       using  '1',
10771              l_null,
10772              'T',
10773              l_null,
10774              OUT l_return_status,
10775              OUT l_msg_count,
10776              OUT l_msg_data,
10777              p_list_header_id,
10778              l_rank_count,
10779              l_rank,
10780              OUT l_total_dup_recs,
10781              'DEDUPE3';
10782         if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
10783            write_to_act_log('Error in executing remote procedure with process type as DEDUPE3', 'LIST', p_list_header_id,'HIGH');
10784            write_to_act_log('Error '||l_msg_data , 'LIST', p_list_header_id,'HIGH');
10785         else
10786           write_to_act_log('Remote procedure executed successfully, duplicated identified based on rank', 'LIST', p_list_header_id,'LOW');
10787         end if;
10788 
10789         --initializing as we may have more than one rule.
10790 
10791         l_fields := NULL;
10792         l_sql_stmt2 := l_sql_stmt1;
10793 
10794     END LOOP;   --c_list_rules loop
10795 
10796     --recording who performed the deduplication and at what time.
10797     --recording the number of duplicates found.
10798     UPDATE ams_list_headers_all
10799        SET last_deduped_by_user_id = FND_GLOBAL.User_Id
10800            ,last_dedupe_date = SYSDATE
10801            ,no_of_rows_duplicates = l_total_dup_recs
10802      WHERE list_header_id = p_list_header_id;
10803 
10804    /*   savio removing potential multiple update statement */
10805    /*
10806 --    UPDATE ams_list_headers_all
10807 --       SET no_of_rows_duplicates = l_total_dup_recs
10808 --     WHERE list_header_id = p_list_header_id;
10809    */
10810 
10811     write_to_act_log('Updated list header table with the userid and the time','LIST',p_list_header_id, 'LOW');
10812     write_to_act_log('Procedure execute_remote_dedupe_list executed','LIST',p_list_header_id, 'LOW');
10813 
10814     COMMIT;
10815 
10816 END Execute_Remote_Dedupe_List;
10817 -- -----------------------------
10818 
10819 PROCEDURE migrate_lists_from_remote(
10820                             Errbuf          OUT NOCOPY     VARCHAR2,
10821                             Retcode         OUT NOCOPY     VARCHAR2,
10822                             p_list_header_id NUMBER
10823                             ) IS
10824 
10825 l_list_header_id	NUMBER;
10826   cursor c_migrate_list is
10827   select head.list_header_id
10828     from ams_list_headers_all head, ams_list_src_types stypes
10829    where head.status_code = 'AVAILABLE'
10830      and head.MIGRATION_DATE is NULL
10831      and head.list_source_type = stypes.source_type_code
10832      and stypes.remote_flag = 'Y';
10833 
10834 Begin
10835 
10836 Ams_Utility_Pvt.Write_Conc_log('Start migrate_lists_from_remote : ');
10837 if p_list_header_id is not null then
10838      migrate_lists( p_list_header_id ) ;
10839  else
10840   open c_migrate_list;
10841   LOOP
10842     fetch c_migrate_list into l_list_header_id;
10843     exit when c_migrate_list%notfound;
10844      migrate_lists( l_list_header_id ) ;
10845   END LOOP;
10846   close c_migrate_list;
10847 end if;
10848 commit;
10849 Ams_Utility_Pvt.Write_Conc_log('End migrate_lists_from_remote : ');
10850 EXCEPTION
10851   WHEN OTHERS THEN
10852    Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_lists_from_remote : '||SQLERRM);
10853     errbuf:= substr(SQLERRM,1,254);
10854     retcode:= 2;
10855    raise;
10856 End migrate_lists_from_remote;
10857 -- -----------------------------------------------------------------
10858 
10859 PROCEDURE migrate_lists(
10860                             p_list_header_id NUMBER
10861                             ) IS
10862 
10863 l_dblink        varchar2(100);
10864 l_start_rownum	number := 1;
10865 l_end_rownum	number := 10000;
10866 l_total_records number;
10867 l_insert_sql   varchar2(32767);
10868 l_return_status varchar2(1);
10869 l_lookup_code                         VARCHAR2(30);
10870 l_user_status_id                      NUMBER;
10871 l_no_of_chunks				number;
10872 
10873 
10874   cursor c_dblink is
10875   select database_link
10876     from ams_list_src_types stypes, ams_list_headers_all list
10877    where list.list_source_type = stypes.source_type_code
10878      and list_header_id  =  p_list_header_id;
10879 
10880 begin
10881  Ams_Utility_Pvt.Write_Conc_log('Start migrate_lists : '||to_char(p_list_header_id));
10882 
10883  open c_dblink;
10884  fetch c_dblink into l_dblink;
10885  close c_dblink;
10886  Ams_Utility_Pvt.Write_Conc_log('l_dblink : '||l_dblink);
10887 
10888  write_to_act_log('Database link is '||l_dblink,'LIST',p_list_header_id,'LOW');
10889  Ams_Utility_Pvt.Write_Conc_log('Start Delete list entries from local instance  : ');
10890  write_to_act_log('Deleting existing entries from the local instance.','LIST',p_list_header_id,'LOW');
10891   delete from ams_list_entries
10892    where list_header_id = p_list_header_id;
10893  Ams_Utility_Pvt.Write_Conc_log('End Delete list entries from local instance  : ');
10894 
10895        l_insert_sql := 'insert into ams_list_entries
10896         (list_header_id ,
10897          list_entry_id,
10898          object_version_number,
10899          source_code                     ,
10900          source_code_for_id              ,
10901          arc_list_used_by_source         ,
10902          arc_list_select_action_from     ,
10903          pin_code                        ,
10904          view_application_id             ,
10905          manually_entered_flag           ,
10906          marked_as_random_flag           ,
10907          marked_as_duplicate_flag        ,
10908          part_of_control_group_flag      ,
10909          exclude_in_triggered_list_flag  ,
10910          enabled_flag ,
10911          LIST_SELECT_ACTION_FROM_NAME,
10912          last_update_date,
10913          last_updated_by,
10914          creation_date,
10915          created_by,
10916          last_update_login,
10917          list_entry_source_system_id,
10918          list_entry_source_system_type,
10919          list_select_action_id,
10920          SUFFIX,
10921          FIRST_NAME,
10922          LAST_NAME,
10923          CUSTOMER_NAME,
10924          TITLE,
10925          ADDRESS_LINE1,
10926          ADDRESS_LINE2,
10927          CITY,
10928          STATE,
10929          ZIPCODE,
10930          COUNTRY,
10931          FAX,
10932          PHONE,
10933          EMAIL_ADDRESS,
10934          CUSTOMER_ID                              ,
10935          LIST_SOURCE                              ,
10936          PARTY_ID                                 ,
10937          PARENT_PARTY_ID                          ,
10938          IMP_SOURCE_LINE_ID                       ,
10939          COL1,
10940          COL2,
10941          COL3,
10942          COL4,
10943          COL5,
10944          COL6,
10945          COL7,
10946          COL8,
10947          COL9,
10948          COL10,
10949          COL11,
10950          COL12,
10951          COL13,
10952          COL14,
10953          COL15,
10954          COL16,
10955          COL17,
10956          COL18,
10957          COL19,
10958          COL20,
10959          COL21,
10960          COL22,
10961          COL23,
10962          COL24,
10963          COL25,
10964          COL26,
10965          COL27,
10966          COL28,
10967          COL29,
10968          COL30,
10969          COL31,
10970          COL32,
10971          COL33,
10972          COL34,
10973          COL35,
10974          COL36,
10975          COL37,
10976          COL38,
10977          COL39,
10978          COL40,
10979          COL41,
10980          COL42,
10981          COL43,
10982          COL44,
10983          COL45,
10984          COL46,
10985          COL47,
10986          COL48,
10987          COL49,
10988          COL50,
10989          COL51,
10990          COL52,
10991          COL53,
10992          COL54,
10993          COL55,
10994          COL56,
10995          COL57,
10996          COL58,
10997          COL59,
10998          COL60,
10999          COL61,
11000          COL62,
11001          COL63,
11002          COL64,
11003          COL65,
11004          COL66,
11005          COL67,
11006          COL68,
11007          COL69,
11008          COL70,
11009          COL71,
11010          COL72,
11011          COL73,
11012          COL74,
11013          COL75,
11014          COL76,
11015          COL77,
11016          COL78,
11017          COL79,
11018          COL80,
11019          COL81,
11020          COL82,
11021          COL83,
11022          COL84,
11023          COL85,
11024          COL86,
11025          COL87,
11026          COL88,
11027          COL89,
11028          COL90,
11029          COL91,
11030          COL92,
11031          COL93,
11032          COL94,
11033          COL95,
11034          COL96,
11035          COL97,
11036          COL98,
11037          COL99,
11038          COL100,
11039          COL101,
11040          COL102,
11041          COL103,
11042          COL104,
11043          COL105,
11044          COL106,
11045          COL107,
11046          COL108,
11047          COL109,
11048          COL110,
11049          COL111,
11050          COL112,
11051          COL113,
11052          COL114,
11053          COL115,
11054          COL116,
11055          COL117,
11056          COL118,
11057          COL119,
11058          COL120,
11059          COL121,
11060          COL122,
11061          COL123,
11062          COL124,
11063          COL125,
11064          COL126,
11065          COL127,
11066          COL128,
11067          COL129,
11068          COL130,
11069          COL131,
11070          COL132,
11071          COL133,
11072          COL134,
11073          COL135,
11074          COL136,
11075          COL137,
11076          COL138,
11077          COL139,
11078          COL140,
11079          COL141,
11080          COL142,
11081          COL143,
11082          COL144,
11083          COL145,
11084          COL146,
11085          COL147,
11086          COL148,
11087          COL149,
11088          COL150,
11089          COL151,
11090          COL152,
11091          COL153,
11092          COL154,
11093          COL155,
11094          COL156,
11095          COL157,
11096          COL158,
11097          COL159,
11098          COL160,
11099          COL161,
11100          COL162,
11101          COL163,
11102          COL164,
11103          COL165,
11104          COL166,
11105          COL167,
11106          COL168,
11107          COL169,
11108          COL170,
11109          COL171,
11110          COL172,
11111          COL173,
11112          COL174,
11113          COL175,
11114          COL176,
11115          COL177,
11116          COL178,
11117          COL179,
11118          COL180,
11119          COL181,
11120          COL182,
11121          COL183,
11122          COL184,
11123          COL185,
11124          COL186,
11125          COL187,
11126          COL188,
11127          COL189,
11128          COL190,
11129          COL191,
11130          COL192,
11131          COL193,
11132          COL194,
11133          COL195,
11134          COL196,
11135          COL197,
11136          COL198,
11137          COL199,
11138          COL200,
11139          COL201,
11140          COL202,
11141          COL203,
11142          COL204,
11143          COL205,
11144          COL206,
11145          COL207,
11146          COL208,
11147          COL209,
11148          COL210,
11149          COL211,
11150          COL212,
11151          COL213,
11152          COL214,
11153          COL215,
11154          COL216,
11155          COL217,
11156          COL218,
11157          COL219,
11158          COL220,
11159          COL221,
11160          COL222,
11161          COL223,
11162          COL224,
11163          COL225,
11164          COL226,
11165          COL227,
11166          COL228,
11167          COL229,
11168          COL230,
11169          COL231,
11170          COL232,
11171          COL233,
11172          COL234,
11173          COL235,
11174          COL236,
11175          COL237,
11176          COL238,
11177          COL239,
11178          COL240,
11179          COL241,
11180          COL242,
11181          COL243,
11182          COL244,
11183          COL245,
11184          COL246,
11185          COL247,
11186          COL248,
11187          COL249,
11188          COL250 ,
11189          COL251     ,
11190          COL252     ,
11191          COL253     ,
11192          COL254     ,
11193          COL256     ,
11194          COL255     ,
11195          COL257     ,
11196          COL258     ,
11197          COL259     ,
11198          COL260     ,
11199          COL261     ,
11200          COL262     ,
11201          COL263     ,
11202          COL264     ,
11203          COL265     ,
11204          COL266     ,
11205          COL267     ,
11206          COL268     ,
11207          COL269     ,
11208          COL270     ,
11209          COL271     ,
11210          COL272     ,
11211          COL273     ,
11212          COL274     ,
11213          COL275     ,
11214          COL276     ,
11215          COL277     ,
11216          COL278     ,
11217          COL279     ,
11218          COL280     ,
11219          COL281     ,
11220          COL282     ,
11221          COL283     ,
11222          COL284     ,
11223          COL285     ,
11224          COL286     ,
11225          COL287     ,
11226          COL288     ,
11227          COL289     ,
11228          COL290     ,
11229          COL291     ,
11230          COL292     ,
11231          COL293     ,
11232          COL294     ,
11233          COL295     ,
11234          COL296     ,
11235          COL297     ,
11236          COL298     ,
11237          COL299     ,
11238          COL300     ,
11239 GROUP_CODE,
11240 NEWLY_UPDATED_FLAG,
11241 OUTCOME_ID,
11242 RESULT_ID,
11243 REASON_ID,
11244 NOTES,
11245 VEHICLE_RESPONSE_CODE,
11246 SALES_AGENT_EMAIL_ADDRESS,
11247 RESOURCE_ID,
11248 LOCATION_ID,
11249 CONTACT_POINT_ID,
11250 ORIG_SYSTEM_REFERENCE,
11251 MARKED_AS_FATIGUED_FLAG,
11252 MARKED_AS_SUPPRESSED_FLAG,
11253 REMOTE_LIST_ENTRY_ID,
11254 -- ERROR_TEXT,
11255 -- ERROR_FLAG,
11256 LAST_CONTACTED_DATE,
11257 CUSTOM_COLUMN1,
11258 CUSTOM_COLUMN2,
11259 CUSTOM_COLUMN3,
11260 CUSTOM_COLUMN4,
11261 CUSTOM_COLUMN5,
11262 CUSTOM_COLUMN6,
11263 CUSTOM_COLUMN7,
11264 CUSTOM_COLUMN8,
11265 CUSTOM_COLUMN9,
11266 CUSTOM_COLUMN10,
11267 CUSTOM_COLUMN11,
11268 CUSTOM_COLUMN12,
11269 CUSTOM_COLUMN13,
11270 CUSTOM_COLUMN14,
11271 CUSTOM_COLUMN15,
11272 CUSTOM_COLUMN16,
11273 CUSTOM_COLUMN17,
11274 CUSTOM_COLUMN18,
11275 CUSTOM_COLUMN19,
11276 CUSTOM_COLUMN20,
11277 CUSTOM_COLUMN21,
11278 CUSTOM_COLUMN22,
11279 CUSTOM_COLUMN23,
11280 CUSTOM_COLUMN24,
11281 CUSTOM_COLUMN25,
11282 RANK
11283         )
11284 	SELECT
11285          list_header_id,
11286          ams_list_entries_s.nextval,
11287          object_version_number,
11288          source_code                     ,
11289          source_code_for_id              ,
11290          arc_list_used_by_source         ,
11291          arc_list_select_action_from     ,
11292          pin_code                        ,
11293          view_application_id             ,
11294          manually_entered_flag           ,
11295          marked_as_random_flag           ,
11296          marked_as_duplicate_flag        ,
11297          part_of_control_group_flag      ,
11298          exclude_in_triggered_list_flag  ,
11299          enabled_flag ,
11300          LIST_SELECT_ACTION_FROM_NAME,
11301          last_update_date,
11302          last_updated_by,
11303          creation_date,
11304          created_by,
11305          last_update_login,
11306          list_entry_source_system_id,
11307          list_entry_source_system_type,
11308          list_select_action_id,
11309          SUFFIX,
11310          FIRST_NAME,
11311          LAST_NAME,
11312          CUSTOMER_NAME,
11313          TITLE,
11314          ADDRESS_LINE1,
11315          ADDRESS_LINE2,
11316          CITY,
11317          STATE,
11318          ZIPCODE,
11319          COUNTRY,
11320          FAX,
11321          PHONE,
11322          EMAIL_ADDRESS,
11323          CUSTOMER_ID                              ,
11324          LIST_SOURCE                              ,
11325          null,  -- PARTY_ID                                 ,
11326          null,  -- PARENT_PARTY_ID                          ,
11327          IMP_SOURCE_LINE_ID                       ,
11328          COL1,
11329          COL2,
11330          COL3,
11331          COL4,
11332          COL5,
11333          COL6,
11334          COL7,
11335          COL8,
11336          COL9,
11337          COL10,
11338          COL11,
11339          COL12,
11340          COL13,
11341          COL14,
11342          COL15,
11343          COL16,
11344          COL17,
11345          COL18,
11346          COL19,
11347          COL20,
11348          COL21,
11349          COL22,
11350          COL23,
11351          COL24,
11352          COL25,
11353          COL26,
11354          COL27,
11355          COL28,
11356          COL29,
11357          COL30,
11358          COL31,
11359          COL32,
11360          COL33,
11361          COL34,
11362          COL35,
11363          COL36,
11364          COL37,
11365          COL38,
11366          COL39,
11367          COL40,
11368          COL41,
11369          COL42,
11370          COL43,
11371          COL44,
11372          COL45,
11373          COL46,
11374          COL47,
11375          COL48,
11376          COL49,
11377          COL50,
11378          COL51,
11379          COL52,
11380          COL53,
11381          COL54,
11382          COL55,
11383          COL56,
11384          COL57,
11385          COL58,
11386          COL59,
11387          COL60,
11388          COL61,
11389          COL62,
11390          COL63,
11391          COL64,
11392          COL65,
11393          COL66,
11394          COL67,
11395          COL68,
11396          COL69,
11397          COL70,
11398          COL71,
11399          COL72,
11400          COL73,
11401          COL74,
11402          COL75,
11403          COL76,
11404          COL77,
11405          COL78,
11406          COL79,
11407          COL80,
11408          COL81,
11409          COL82,
11410          COL83,
11411          COL84,
11412          COL85,
11413          COL86,
11414          COL87,
11415          COL88,
11416          COL89,
11417          COL90,
11418          COL91,
11419          COL92,
11420          COL93,
11421          COL94,
11422          COL95,
11423          COL96,
11424          COL97,
11425          COL98,
11426          COL99,
11427          COL100,
11428          COL101,
11429          COL102,
11430          COL103,
11431          COL104,
11432          COL105,
11433          COL106,
11434          COL107,
11435          COL108,
11436          COL109,
11437          COL110,
11438          COL111,
11439          COL112,
11440          COL113,
11441          COL114,
11442          COL115,
11443          COL116,
11444          COL117,
11445          COL118,
11446          COL119,
11447          COL120,
11448          COL121,
11449          COL122,
11450          COL123,
11451          COL124,
11452          COL125,
11453          COL126,
11454          COL127,
11455          COL128,
11456          COL129,
11457          COL130,
11458          COL131,
11459          COL132,
11460          COL133,
11461          COL134,
11462          COL135,
11463          COL136,
11464          COL137,
11465          COL138,
11466          COL139,
11467          COL140,
11468           COL141,
11469           COL142,
11470           COL143,
11471           COL144,
11472           COL145,
11473           COL146,
11474           COL147,
11475           COL148,
11476           COL149,
11477           COL150,
11478           COL151,
11479           COL152,
11480           COL153,
11481           COL154,
11482           COL155,
11483           COL156,
11484           COL157,
11485           COL158,
11486           COL159,
11487           COL160,
11488           COL161,
11489           COL162,
11490           COL163,
11491           COL164,
11492           COL165,
11493           COL166,
11494           COL167,
11495           COL168,
11496           COL169,
11497           COL170,
11498           COL171,
11499           COL172,
11500           COL173,
11501           COL174,
11502           COL175,
11503           COL176,
11504           COL177,
11505           COL178,
11506           COL179,
11507           COL180,
11508           COL181,
11509           COL182,
11510           COL183,
11511           COL184,
11512           COL185,
11513           COL186,
11514           COL187,
11515           COL188,
11516           COL189,
11517           COL190,
11518           COL191,
11519           COL192,
11520           COL193,
11521           COL194,
11522           COL195,
11523           COL196,
11524           COL197,
11525           COL198,
11526           COL199,
11527           COL200,
11528           COL201,
11529           COL202,
11530           COL203,
11531           COL204,
11532           COL205,
11533           COL206,
11534           COL207,
11535           COL208,
11536           COL209,
11537           COL210,
11538           COL211,
11539           COL212,
11540           COL213,
11541           COL214,
11542           COL215,
11543           COL216,
11544           COL217,
11545           COL218,
11546           COL219,
11547           COL220,
11548           COL221,
11549           COL222,
11550           COL223,
11551           COL224,
11552           COL225,
11553           COL226,
11554           COL227,
11555           COL228,
11556           COL229,
11557           COL230,
11558           COL231,
11559           COL232,
11560           COL233,
11561           COL234,
11562           COL235,
11563           COL236,
11564           COL237,
11565           COL238,
11566           COL239,
11567           COL240,
11568           COL241,
11569           COL242,
11570           COL243,
11571           COL244,
11572           COL245,
11573           COL246,
11574           COL247,
11575           COL248,
11576           COL249,
11577           COL250 ,
11578           COL251 ,
11579           COL252 ,
11580           COL253 ,
11581           COL254 ,
11582           COL256 ,
11583           COL255 ,
11584           COL257 ,
11585           COL258 ,
11586           COL259 ,
11587           COL260 ,
11588           COL261 ,
11589           COL262 ,
11590           COL263 ,
11591           COL264 ,
11592           COL265 ,
11593           COL266 ,
11594           COL267 ,
11595           COL268 ,
11596           COL269 ,
11597           COL270 ,
11598           COL271 ,
11599           COL272 ,
11600           COL273 ,
11601           COL274 ,
11602           COL275 ,
11603           COL276 ,
11604           COL277 ,
11605           COL278 ,
11606           COL279 ,
11607           COL280 ,
11608           COL281 ,
11609           COL282 ,
11610           COL283 ,
11611           COL284 ,
11612           COL285 ,
11613           COL286 ,
11614           COL287 ,
11615           COL288 ,
11616           COL289 ,
11617           COL290 ,
11618           COL291 ,
11619           COL292 ,
11620           COL293 ,
11621           COL294 ,
11622           COL295 ,
11623           COL296 ,
11624           COL297 ,
11625           COL298 ,
11626           COL299 ,
11627           COL300 ,
11628 GROUP_CODE,
11629 NEWLY_UPDATED_FLAG,
11630 OUTCOME_ID,
11631 RESULT_ID,
11632 REASON_ID,
11633 NOTES,
11634 VEHICLE_RESPONSE_CODE,
11635 SALES_AGENT_EMAIL_ADDRESS,
11636 RESOURCE_ID,
11637 LOCATION_ID,
11638 CONTACT_POINT_ID,
11639 ORIG_SYSTEM_REFERENCE,
11640 MARKED_AS_FATIGUED_FLAG,
11641 MARKED_AS_SUPPRESSED_FLAG,
11642 LIST_ENTRY_ID,
11643 -- ERROR_TEXT,
11644 -- ERROR_FLAG,
11645 LAST_CONTACTED_DATE,
11646 CUSTOM_COLUMN1,
11647 CUSTOM_COLUMN2,
11648 CUSTOM_COLUMN3,
11649 CUSTOM_COLUMN4,
11650 CUSTOM_COLUMN5,
11651 CUSTOM_COLUMN6,
11652 CUSTOM_COLUMN7,
11653 CUSTOM_COLUMN8,
11654 CUSTOM_COLUMN9,
11655 CUSTOM_COLUMN10,
11656 CUSTOM_COLUMN11,
11657 CUSTOM_COLUMN12,
11658 CUSTOM_COLUMN13,
11659 CUSTOM_COLUMN14,
11660 CUSTOM_COLUMN15,
11661 CUSTOM_COLUMN16,
11662 CUSTOM_COLUMN17,
11663 CUSTOM_COLUMN18,
11664 CUSTOM_COLUMN19,
11665 CUSTOM_COLUMN20,
11666 CUSTOM_COLUMN21,
11667 CUSTOM_COLUMN22,
11668 CUSTOM_COLUMN23,
11669 CUSTOM_COLUMN24,
11670 CUSTOM_COLUMN25,
11671 RANK
11672        from ams_list_entries@'||l_dblink||'  '||
11673        'where   list_header_id = ' ||to_char(p_list_header_id);
11674 --       ||' and rownum between '||to_char(l_start_rownum)||' and '||to_char(l_end_rownum);
11675  l_no_of_chunks := 0;
11676  l_no_of_chunks  := ceil(length(l_insert_sql)/2000 );
11677  write_to_act_log('Insert statement formed for migrating the list is..','LIST',p_list_header_id,'LOW');
11678  for i in 1 ..l_no_of_chunks
11679  loop
11680       Ams_Utility_Pvt.Write_Conc_log('l_insert_sql = '||substrb(l_insert_sql,(2000*i) - 1999,2000));
11681  end loop;
11682  execute immediate l_insert_sql;
11683  commit;
11684 /*
11685       l_start_rownum := l_start_rownum + 10000;
11686       l_end_rownum   := l_end_rownum   + 10000;
11687     END LOOP;
11688 */
11689  Ams_Utility_Pvt.Write_Conc_log('End migrate_lists : '||to_char(p_list_header_id));
11690          SELECT user_status_id into l_user_status_id FROM ams_user_statuses_vl
11691          WHERE system_status_type = 'AMS_LIST_STATUS' AND
11692          system_status_code = 'AVAILABLE' and default_flag = 'Y';
11693          UPDATE ams_list_headers_all
11694          set status_code      = 'AVAILABLE',
11695             user_status_id    =  l_user_status_id,
11696             migration_date = sysdate
11697          where list_header_id = p_list_header_id;
11698      write_to_act_log('List migrated to local instance. Updating list header details.','LIST',p_list_header_id,'HIGH');
11699      -- Added for cancel list gen as it prevents parallel update- Raghu
11700      -- of list headers when cancel button is pressed
11701      commit;
11702 
11703  Update_List_Dets(p_list_header_id,l_return_status);
11704  if nvl(l_return_status,'S') in ('E','U') then -- resulted in error.
11705     write_to_act_log('Error while updating list header details.', 'LIST', g_list_header_id,'HIGH');
11706  end if;
11707 
11708  Ams_Utility_Pvt.Write_Conc_log('After Update_List_Dets call : ');
11709 
11710   IF(l_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
11711      RAISE FND_API.G_EXC_ERROR;
11712   END IF;
11713 EXCEPTION
11714   WHEN OTHERS THEN
11715         UPDATE ams_list_headers_all
11716         SET    last_generation_success_flag = 'N',
11717                status_code                  = 'FAILED',
11718                user_status_id               = 311,
11719                last_update_date             = sysdate,
11720                status_date                  = sysdate,
11721                main_gen_end_time            = sysdate
11722         WHERE  list_header_id               = p_list_header_id;
11723    write_to_act_log('Error while migrating list from remote instance. '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
11724      -- Added for cancel list gen as it prevents parallel update- Raghu
11725      -- of list headers when cancel button is pressed
11726      commit;
11727 
11728      Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_lists : '||SQLERRM);
11729   --  errbuf:= substr(SQLERRM,1,254);
11730   --  retcode:= 2;
11731    raise;
11732 end migrate_lists;
11733 -- --------------------------------------------
11734 PROCEDURE migrate_word_replacements(
11735                             Errbuf          OUT NOCOPY     VARCHAR2,
11736                             Retcode         OUT NOCOPY     VARCHAR2,
11737                             dblink          VARCHAR2
11738                             ) IS
11739 
11740 l_insert_sql   varchar2(32767);
11741 l_delete_sql   varchar2(32767);
11742 begin
11743 Ams_Utility_Pvt.Write_Conc_log('Start migrate_word_replacements : ');
11744 l_delete_sql := 'Delete from AMS_HZ_WORD_REPLACEMENTS@'||dblink;
11745 l_insert_sql := 'Insert into AMS_HZ_WORD_REPLACEMENTS@'||dblink||' (
11746 			ORIGINAL_WORD,
11747 			REPLACEMENT_WORD,
11748 			TYPE,
11749 			COUNTRY_CODE,
11750 			LAST_UPDATE_DATE,
11751 			LAST_UPDATED_BY,
11752 			CREATION_DATE,
11753 			CREATED_BY,
11754 			LAST_UPDATE_LOGIN,
11755 			ATTRIBUTE_CATEGORY,
11756 			ATTRIBUTE1,
11757 			ATTRIBUTE2,
11758 			ATTRIBUTE3,
11759 			ATTRIBUTE4,
11760 			ATTRIBUTE5,
11761 			ATTRIBUTE6,
11762 			ATTRIBUTE7,
11763 			ATTRIBUTE8,
11764 			ATTRIBUTE9,
11765 			ATTRIBUTE10,
11766 			ATTRIBUTE11,
11767 			ATTRIBUTE12,
11768 			ATTRIBUTE13,
11769 			ATTRIBUTE14,
11770 			ATTRIBUTE15,
11771 			WORD_LIST_ID,
11772 			OBJECT_VERSION_NUMBER
11773 		)
11774 		 SELECT
11775                         ORIGINAL_WORD,
11776                         REPLACEMENT_WORD,
11777                         TYPE,
11778                         COUNTRY_CODE,
11779                         LAST_UPDATE_DATE,
11780                         LAST_UPDATED_BY,
11781                         CREATION_DATE,
11782                         CREATED_BY,
11783                         LAST_UPDATE_LOGIN,
11784                         ATTRIBUTE_CATEGORY,
11785                         ATTRIBUTE1,
11786                         ATTRIBUTE2,
11787                         ATTRIBUTE3,
11788                         ATTRIBUTE4,
11789                         ATTRIBUTE5,
11790                         ATTRIBUTE6,
11791                         ATTRIBUTE7,
11792                         ATTRIBUTE8,
11793                         ATTRIBUTE9,
11794                         ATTRIBUTE10,
11795                         ATTRIBUTE11,
11796                         ATTRIBUTE12,
11797                         ATTRIBUTE13,
11798                         ATTRIBUTE14,
11799                         ATTRIBUTE15,
11800                         WORD_LIST_ID,
11801                         OBJECT_VERSION_NUMBER
11802                 FROM  	HZ_WORD_REPLACEMENTS ';
11803 Ams_Utility_Pvt.Write_Conc_log('l_delete_sql =  '||l_delete_sql);
11804 	execute immediate l_delete_sql;
11805 	commit;
11806 Ams_Utility_Pvt.Write_Conc_log('l_insert_sql =  '||l_insert_sql);
11807 	execute immediate l_insert_sql;
11808         commit;
11809 Ams_Utility_Pvt.Write_Conc_log('End migrate_word_replacements : ');
11810 EXCEPTION
11811   WHEN OTHERS THEN
11812    Ams_Utility_Pvt.Write_Conc_log('Exception in migrate_word_replacements : '||SQLERRM);
11813     errbuf:= substr(SQLERRM,1,254);
11814     retcode:= 2;
11815    raise;
11816 end migrate_word_replacements;
11817 -- --------------------------------------------------
11818 PROCEDURE UPDATE_FOR_TRAFFIC_COP( p_list_header_id      in number ,
11819                                   p_list_entry_id       in t_number ) IS
11820 l_return_status varchar2(1);
11821 l_fatigued_records      number;
11822 l_no_of_rows_active     number;
11823 
11824 cursor c_count_list_entries(cur_p_list_header_id number) is
11825 select sum(decode(enabled_flag,'Y',1,0)),
11826        sum(decode(marked_as_fatigued_flag,'Y',1,0))
11827 from ams_list_entries
11828 where list_header_id = cur_p_list_header_id ;
11829 
11830 
11831 begin
11832 
11833              AMS_Utility_PVT.Create_Log (
11834               x_return_status   => l_return_status,
11835               p_arc_log_used_by => 'LIST',
11836               p_log_used_by_id  => p_list_header_id,
11837               p_msg_data        => 'UPDATE_FOR_TRAFFIC_COP : Started ',
11838               p_msg_type        => 'DEBUG');
11839 
11840   FORALL I in p_list_entry_id.first .. p_list_entry_id.last
11841      UPDATE ams_list_entries
11842      SET    ENABLED_FLAG            = 'N',
11843             MARKED_AS_FATIGUED_FLAG = 'Y'
11844     WHERE  list_entry_id = p_list_entry_id(i)
11845       AND  list_header_id = p_list_header_id ;
11846 
11847   open c_count_list_entries(p_list_header_id);
11848   fetch c_count_list_entries
11849    into l_no_of_rows_active            ,
11850         l_fatigued_records      ;
11851   close c_count_list_entries;
11852 
11853     UPDATE ams_list_headers_all
11854      SET    NO_OF_ROWS_FATIGUED = l_fatigued_records,
11855             NO_OF_ROWS_ACTIVE = l_no_of_rows_active
11856     WHERE  list_header_id = p_list_header_id ;
11857  -- Added for cancel list gen as it prevents parallel update- Raghu
11858  -- of list headers when cancel button is pressed
11859  commit;
11860 
11861              AMS_Utility_PVT.Create_Log (
11862               x_return_status   => l_return_status,
11863               p_arc_log_used_by => 'LIST',
11864               p_log_used_by_id  => p_list_header_id,
11865               p_msg_data        => 'UPDATE_FOR_TRAFFIC_COP : End ',
11866               p_msg_type        => 'DEBUG');
11867 
11868 EXCEPTION
11869   WHEN OTHERS THEN
11870              AMS_Utility_PVT.Create_Log (
11871               x_return_status   => l_return_status,
11872               p_arc_log_used_by => 'LIST',
11873               p_log_used_by_id  => p_list_header_id,
11874               p_msg_data        => sqlerrm,
11875               p_msg_type        => 'DEBUG');
11876 
11877 end UPDATE_FOR_TRAFFIC_COP;
11878 
11879 -- --------------------------------------------------
11880 -- **********************************************************************************************
11881 PROCEDURE calc_selection_running_total
11882              (p_action_used_by_id  in  number,
11883               p_action_used_by     in  varchar2  ,-- DEFAULT 'LIST',
11884               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
11885               x_return_status      OUT NOCOPY VARCHAR2,
11886               x_msg_count          OUT NOCOPY NUMBER,
11887               x_msg_data           OUT NOCOPY VARCHAR2) IS
11888 l_request_id	number;
11889 l_msg_count number := 0;
11890 
11891 begin
11892   IF(p_log_flag ='Y')then
11893        write_to_act_log('Process calc_running_total : started', 'LIST', g_list_header_id,'LOW');
11894   END IF;
11895 	l_request_id := FND_REQUEST.SUBMIT_REQUEST(
11896 			application => 'AMS',
11897 			program     => 'AMSLSRTC',
11898 			argument1   => p_action_used_by_id);
11899 		commit;
11900      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11901      FND_MESSAGE.Set_Token('TEXT',  'request_id->'||l_request_id || '<-');
11902      FND_MSG_PUB.Add;
11903           --    FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11904            --   FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
11905             --  FND_MSG_PUB.Add;
11906                FND_MSG_PUB.count_and_get(
11907                      p_encoded => FND_API.g_false,
11908                      p_count   => x_msg_count,
11909                      p_data    => x_msg_data  );
11910       x_return_status := 'S';
11911       x_msg_data := to_char(l_request_id);
11912 
11913            IF l_request_id = 0 THEN
11914               write_to_act_log('Unexpected Error for the program--concurrent program_id is '||to_char(l_request_id), 'LIST', g_list_header_id,'HIGH');
11915 
11916 
11917               FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
11918               FND_MESSAGE.Set_Token('TEXT', 'Error '|| sqlerrm||' '||sqlcode);
11919               FND_MSG_PUB.Add;
11920                FND_MSG_PUB.count_and_get(
11921                      p_encoded => FND_API.g_false,
11922                      p_count   => x_msg_count,
11923                      p_data    => x_msg_data
11924       );
11925       x_return_status := 'E';
11926               RAISE FND_API.g_exc_unexpected_error;
11927            end if;
11928 end calc_selection_running_total;
11929 
11930 -- ----------------------------------------------------------------------------
11931 PROCEDURE calc_running_total (
11932               Errbuf               OUT NOCOPY     VARCHAR2,
11933               Retcode              OUT NOCOPY     VARCHAR2,
11934               p_action_used_by_id  in  number ) IS
11935 
11936   -- AMS_LIST_SELECT_ACTIONS Record for init record and complete record
11937   l_tmp_action_rec             ams_listaction_pvt.action_rec_type;
11938   p_action_rec                 ams_listaction_pvt.action_rec_type;
11939   l_list_select_action_id      number;
11940   l_running_total               number := 0;
11941    p_action_used_by  varchar2(30) := 'LIST';
11942    p_log_flag   	varchar2(1) := 'Y';
11943               x_return_status    varchar2(30);
11944               x_msg_count        number;
11945               x_msg_data         varchar2(4000);
11946 
11947   ----------------------------------------------------------------------------
11948   -- Cursor definition to select list_select_action_id.Will be used in loop to
11949   -- Process each cursor record according to order specified by the user
11950   ----------------------------------------------------------------------------
11951   CURSOR c_action_dets is
11952   SELECT list_select_action_id
11953     FROM ams_list_select_actions
11954    WHERE action_used_by_id   = p_action_used_by_id
11955      AND arc_action_used_by  = p_action_used_by
11956    ORDER by order_number;
11957 
11958   TYPE big_tbl_type is table of VARCHAR2(32767) index by BINARY_INTEGER;
11959   l_std_sql VARCHAR2(32767);
11960   l_include_sql VARCHAR2(32767);
11961   l_include_count number:=0;
11962   l_final_big_sql VARCHAR2(32767);
11963   l_include_sql_tbl  big_tbl_type ;
11964   l_std_sql_tbl  big_tbl_type ;
11965   l_join_string   varchar2(50);
11966 l_no_of_chunks            number;
11967 l_const_sql varchar2(4000) ;
11968   TYPE char_tbl_type is table of VARCHAR2(100) index by BINARY_INTEGER;
11969   TYPE num_tbl_type is table of number index by BINARY_INTEGER;
11970   l_rank_tbl      char_tbl_type;
11971   l_rank_num_tbl      num_tbl_type;
11972 l_sorted   number;
11973 l_update_sql  VARCHAR2(32767);
11974 l_list_header_id number ;
11975 cursor c1 is
11976 select generation_type
11977 from ams_list_headers_all
11978 where list_header_id = l_list_header_id;
11979 l_generation_type varchar2(60);
11980 l_selection_results   t_number;
11981 l_list_select_action   t_number;
11982 
11983 l_delta			number := 0;
11984 l_previous_incl_total	number := 0;
11985 l_list_act_id		number;
11986 l_ord_num		number;
11987 l_r_totals		number;
11988 cursor c_delta is
11989 select list_select_action_id,order_number,running_total from ams_list_select_actions where
11990 action_used_by_id = p_action_used_by_id and arc_action_used_by = 'LIST'
11991 and list_action_type = 'INCLUDE' order by order_number;
11992 l_last_generation_success_flag varchar2(1);
11993 
11994 cursor c_last_gen is
11995 select nvl(last_generation_success_flag,'N')
11996 from  ams_list_headers_all
11997 where list_header_id = p_action_used_by_id;
11998 
11999 BEGIN
12000 g_list_header_id := p_action_used_by_id;
12001 Ams_Utility_Pvt.Write_Conc_log('Start calc_running_total : ');
12002    UPDATE ams_list_select_actions
12003       SET RUNNING_TOTAL = null, DELTA = null
12004    WHERE action_used_by_id   = p_action_used_by_id
12005      AND arc_action_used_by  = 'LIST';
12006 
12007   IF(p_log_flag ='Y')then
12008        write_to_act_log('Executing procedure calc_running_total', 'LIST', g_list_header_id,'LOW');
12009   END IF;
12010 l_const_sql := ' minus '||
12011                ' select list_entry_source_system_id ' ||
12012                ' from ams_list_entries ' ||
12013                ' where list_header_id  = ' || p_action_used_by_id   ;
12014 open c_last_gen;
12015 fetch c_last_gen into l_last_generation_success_flag;
12016 close c_last_gen;
12017 if l_last_generation_success_flag = 'Y' then
12018         l_const_sql := NULL;
12019 end if;
12020 
12021   OPEN c_action_dets;
12022   LOOP
12023     FETCH c_action_dets INTO l_list_select_action_id;
12024     EXIT WHEN c_action_dets%NOTFOUND;
12025 
12026     -------------------------------------------------------------------------
12027     -- Gets list select actions record details
12028     -- Intialize the record, set the list_select_action_id and get the
12029     -- details
12030     -------------------------------------------------------------------------
12031     ams_listaction_pvt.init_action_rec(l_tmp_action_rec);
12032     l_tmp_action_rec.list_select_action_id := l_list_select_action_id;
12033     ams_listaction_pvt.complete_action_rec
12034                        (p_action_rec      =>l_tmp_action_rec,
12035                         x_complete_rec    =>p_action_rec);
12036 
12037     ----------------------------------------------------------------------
12038     --validating that the first executed action has a type of "INCLUDE".--
12039     ----------------------------------------------------------------------
12040     IF (c_action_dets%ROWCOUNT = 1) THEN
12041        IF (p_action_rec.list_action_type <> 'INCLUDE')then
12042            write_to_act_log('Error. The action type of the first selection is NOT INCLUDE.', 'LIST', g_list_header_id,'HIGH');
12043            Ams_Utility_Pvt.Write_Conc_log('process list actions : first action INCLUDE check failed');
12044            FND_MESSAGE.set_name('AMS', 'AMS_LIST_ACT_FIRST_INCLUDE');
12045            FND_MSG_PUB.Add;
12046            RAISE FND_API.G_EXC_ERROR;
12047        END IF;  -- End of if for list_action_type check
12048     END IF; --End of Validation:- First Action Include Check
12049 
12050 
12051     ams_listaction_pvt.Validate_ListAction
12052     ( p_api_version            => 1.0,
12053       p_init_msg_list          => FND_API.G_FALSE,
12054       p_validation_level       => JTF_PLSQL_API.G_VALID_LEVEL_RECORD,
12055       x_return_status          => x_return_status,
12056       x_msg_count              => x_msg_count,
12057       x_msg_data               => x_msg_data,
12058       p_action_rec             => p_action_rec
12059     );
12060 
12061    IF x_return_status = FND_API.g_ret_sts_error THEN
12062       RAISE FND_API.g_exc_error;
12063    ELSIF x_return_status = FND_API.g_ret_sts_unexp_error THEN
12064       RAISE FND_API.g_exc_unexpected_error;
12065    END IF;
12066 
12067 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : Dynamic ');
12068    /********************************************************************
12069       This dynamic procedure will process action for each object type
12070       If the object type is of CELL the process will be procecss_cell
12071       Using the same logic the procedure could be extended for new
12072       action types
12073    *********************************************************************/
12074       write_to_act_log('Calling AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'LOW');
12075       execute immediate
12076       'BEGIN
12077         AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from ||
12078          '(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12) ;
12079       END;'
12080       using  p_action_used_by_id,
12081              p_action_rec.incl_object_id ,
12082              p_action_rec.list_action_type,
12083              p_action_rec.list_select_action_id,
12084              p_action_rec.order_number,
12085              p_action_rec.rank,
12086              'N',--CHECK p_action_rec.incl_control_group,
12087              OUT x_msg_data,
12088              OUT x_msg_count,
12089              in OUT x_return_status ,
12090              OUT l_std_sql ,
12091              OUT l_include_sql;
12092 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : End Dynamic 001-> ');
12093       write_to_act_log('Executed AMS_ListGeneration_PKG.process_run_total_'||p_action_rec.arc_incl_object_from, 'LIST', g_list_header_id,'LOW');
12094      if p_action_rec.list_action_type  = 'INCLUDE' then
12095 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total include: '||to_char(l_include_count));
12096         l_include_count := l_include_count +1  ;
12097         l_include_sql_tbl(l_include_count) := l_include_sql ;
12098         l_std_sql_tbl(l_include_count)  := l_std_sql;
12099 	l_list_select_action(l_include_count)  := l_list_select_action_id;
12100         l_rank_tbl(l_include_count)  :=  lpad(p_action_rec.rank,50,'0')
12101                          || lpad(p_action_rec.order_number,50,'0');
12102      else
12103        if p_action_rec.list_action_type  = 'EXCLUDE' then
12104           l_join_string := ' minus ';
12105        l_list_header_id := p_action_rec.action_used_by_id;
12106        else
12107           l_join_string := ' intersect ';
12108        l_list_header_id := p_action_rec.action_used_by_id;
12109      end if;
12110     Ams_Utility_Pvt.Write_Conc_log('process list actions noinclude: '||to_char(l_include_count));
12111    write_to_act_log('No of inclusions is ' || l_include_count, 'LIST', g_list_header_id,'LOW');
12112        FOR i IN 1 .. l_include_count
12113        loop
12114         l_std_sql_tbl(i)  :=
12115                                l_std_sql_tbl(i)   ||
12116                                l_join_string ||
12117                                l_std_sql;
12118      l_no_of_chunks  := ceil(length(l_std_sql_tbl(i))/2000 );
12119      for j in 1 ..l_no_of_chunks
12120      loop
12121         Ams_Utility_Pvt.Write_Conc_log('l_std_sql_tbl : '||substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000));
12122         WRITE_TO_ACT_LOG(substrb(l_std_sql_tbl(i),(2000*j) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12123      end loop;
12124     end loop;
12125   end if; -- if p_action_rec.list_action_type  = 'INCLUDE'
12126 
12127 
12128      IF(x_return_status <>FND_API.G_RET_STS_SUCCESS )THEN
12129        RAISE FND_API.G_EXC_ERROR;
12130      END IF;
12131 Ams_Utility_Pvt.Write_Conc_log('calc_selection_running_total : End Dynamic ');
12132 -- end Of Dynamic Procedure
12133    l_join_string :='';
12134 ---------------------------------------------------------------------------
12135   END LOOP;  --  End loop c_action_dets
12136   CLOSE c_action_dets;
12137 Ams_Utility_Pvt.Write_Conc_log('***************** '||to_char(l_include_count));
12138 
12139      WRITE_TO_ACT_LOG('Sorting according to rank', 'LIST', g_list_header_id,'LOW');
12140 
12141 -- SOLIN, bug 3759988, use order_number, not rank for running total
12142        -- Sorting According to rank
12143 --       FOR i IN 1 .. l_include_count
12144 --       loop
12145 --          l_rank_num_tbl(i) := i;
12146 --          if i <> 1 then
12147 --             for j in 1 .. i-1
12148 --             loop
12149 --Ams_Utility_Pvt.Write_Conc_log('***************** '||l_rank_tbl(i) || '*i*' || i);
12150 --     WRITE_TO_ACT_LOG(l_rank_tbl(i) || '*i*' || i, 'LIST', g_list_header_id,'LOW');
12151 --               if l_rank_tbl(i)  < l_rank_tbl(l_rank_num_tbl(j)) then
12152 --                  for k in reverse j .. i-1
12153 --                  loop
12154 --                     l_rank_num_tbl(k+1) := l_rank_num_tbl(k);
12155 --                  end loop;
12156 --                  l_rank_num_tbl(j) := i;
12157 --                  exit;
12158 --               end if;
12159 --             end loop;
12160 --	  end if;
12161 --       end loop;
12162 -- SOLIN, end
12163   for  i in 1 .. l_include_count
12164   loop
12165         l_sorted := i; -- l_rank_num_tbl(i); SOLIN, bug 3759988
12166 Ams_Utility_Pvt.Write_Conc_log('*****************SORTED '|| l_sorted ||' '||l_rank_tbl(i));
12167         l_final_big_sql := l_include_sql_tbl(l_sorted) ||
12168         l_std_sql_tbl(l_sorted) || l_const_sql || ')';
12169      WRITE_TO_ACT_LOG('Final SQL formed in calc_running_total proc', 'LIST', g_list_header_id,'LOW');
12170 Ams_Utility_Pvt.Write_Conc_log('********calc_selection_running_total: FINAL SQL ************');
12171      l_no_of_chunks  := ceil(length(l_final_big_sql)/2000 );
12172      for i in 1 ..l_no_of_chunks
12173      loop
12174         WRITE_TO_ACT_LOG(substrb(l_final_big_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12175         Ams_Utility_Pvt.Write_Conc_log('l_final_big_sql :'||substrb(l_final_big_sql,(2000*i) - 1999,2000));
12176      end loop;
12177 Ams_Utility_Pvt.Write_Conc_log('*******Process_list_actions: FINAL SQL END************');
12178   l_final_big_sql := 'BEGIN '||l_final_big_sql||' ; END;';
12179    EXECUTE IMMEDIATE l_final_big_sql using out l_selection_results(l_sorted);
12180 Ams_Utility_Pvt.Write_Conc_log('*******l_list_select_action(l_sorted) = '||l_list_select_action(l_sorted));
12181 Ams_Utility_Pvt.Write_Conc_log('*******l_selection_results(l_sorted) = '||l_selection_results(l_sorted));
12182    l_running_total := l_running_total + l_selection_results(l_sorted);
12183 Ams_Utility_Pvt.Write_Conc_log('*******l_running_total = '||l_running_total);
12184    Update ams_list_select_actions set RUNNING_TOTAL = l_running_total
12185    Where LIST_SELECT_ACTION_ID = l_list_select_action(l_sorted)
12186       and arc_action_used_by = 'LIST';
12187 
12188   end loop;
12189  commit;
12190 
12191 Ams_Utility_Pvt.Write_Conc_log('*******Delta Calculation*********');
12192 Ams_Utility_Pvt.Write_Conc_log('*******p_action_used_by_id = '||p_action_used_by_id);
12193    open c_delta;
12194    loop
12195      fetch c_delta into l_list_act_id,l_ord_num,l_r_totals;
12196 Ams_Utility_Pvt.Write_Conc_log('*******l_list_act_id = '||l_list_act_id);
12197 Ams_Utility_Pvt.Write_Conc_log('*******l_ord_num = '||l_ord_num);
12198 Ams_Utility_Pvt.Write_Conc_log('*******l_r_totals = '||l_r_totals);
12199      exit when c_delta%notfound;
12200      if l_ord_num = 1 then
12201         l_delta := 0;
12202 Ams_Utility_Pvt.Write_Conc_log('l_ord_num = 1 *******l_delta = '||l_delta);
12203      end if;
12204      if l_ord_num > 1 then
12205         l_delta := l_r_totals - l_previous_incl_total;
12206 Ams_Utility_Pvt.Write_Conc_log(' l_ord_num > 1 *******l_delta = '||l_delta);
12207      end if;
12208      Update ams_list_select_actions set delta = l_delta
12209      Where LIST_SELECT_ACTION_ID = l_list_act_id;
12210      l_delta := 0;
12211      l_previous_incl_total := l_r_totals;
12212 Ams_Utility_Pvt.Write_Conc_log('*******l_previous_incl_total = '||l_previous_incl_total);
12213    end loop;
12214    close c_delta;
12215 commit;
12216 EXCEPTION
12217    WHEN FND_API.G_EXC_ERROR THEN
12218    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12219      IF(c_action_dets%ISOPEN)THEN
12220         CLOSE c_action_dets;
12221      END IF;
12222      -- Check if reset of the status is required
12223      x_return_status := FND_API.G_RET_STS_ERROR ;
12224       FND_MSG_PUB.count_and_get(
12225             p_encoded => FND_API.g_false,
12226             p_count   => x_msg_count,
12227             p_data    => x_msg_data);
12228 
12229   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12230    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12231      IF(c_action_dets%ISOPEN)THEN
12232         CLOSE c_action_dets;
12233      END IF;
12234 Ams_Utility_Pvt.Write_Conc_log('Error: AMS_ListGeneration_PKG.Process_list_actions: '||sqlerrm||sqlcode);
12235      write_to_act_log('Error: AMS_ListGeneration_PKG.Process_list_actions:'
12236                       ||sqlerrm||sqlcode, 'LIST', g_list_header_id);
12237      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
12238      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
12239      FND_MSG_PUB.Add;
12240      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12241       FND_MSG_PUB.count_and_get(
12242             p_encoded => FND_API.g_false,
12243             p_count   => x_msg_count,
12244             p_data    => x_msg_data);
12245 
12246   WHEN OTHERS THEN
12247    write_to_act_log('Error while executing procedure calc_running_total '||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12248      IF(c_action_dets%ISOPEN)THEN
12249         CLOSE c_action_dets;
12250      END IF;
12251 Ams_Utility_Pvt.Write_Conc_log('Error: AMS_ListGeneration_PKG.Process_list_actions: '||sqlerrm||sqlcode);
12252      write_to_act_log('Error: AMS_ListGeneration_PKG.Process_list_actions:'
12253                        ||sqlerrm||sqlcode, 'LIST', g_list_header_id,'HIGH');
12254      FND_MESSAGE.set_name('AMS', 'AMS_API_DEBUG_MESSAGE');
12255      FND_MESSAGE.Set_Token('TEXT', sqlerrm||' '||sqlcode);
12256      FND_MSG_PUB.Add;
12257      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
12258       FND_MSG_PUB.count_and_get(
12259             p_encoded => FND_API.g_false,
12260             p_count   => x_msg_count,
12261             p_data    => x_msg_data);
12262 END calc_running_total;
12263 
12264 -- ----------------------------------------------------------
12265 
12266 PROCEDURE process_run_total_imph
12267              (p_action_used_by_id in number,
12268               p_incl_object_id in number,
12269               p_list_action_type  in varchar2,
12270               p_list_select_action_id   in number,
12271               p_order_number   in number,
12272               p_rank   in number,
12273               p_include_control_group  in varchar2,
12274               x_msg_count      OUT NOCOPY number,
12275               x_msg_data       OUT NOCOPY varchar2,
12276               x_return_status  IN OUT NOCOPY VARCHAR2,
12277               x_std_sql OUT NOCOPY varchar2 ,
12278               x_include_sql OUT NOCOPY varchar2
12279               ) is
12280 l_list_entry_source_type  varchar2(30);
12281 cursor c_get_source_type
12282 is select  decode(import_type,'B2C','PERSON_LIST','ORGANIZATION_CONTACT_LIST')
12283 from ams_imp_list_headers_all
12284 where  import_list_header_id = p_incl_object_id;
12285 l_no_of_chunks  number;
12286 BEGIN
12287     write_to_act_log('Executing procedure process_run_total_imph', 'LIST', g_list_header_id,'LOW');
12288     open  c_get_source_type ;
12289     fetch c_get_source_type into l_list_entry_source_type  ;
12290     close  c_get_source_type ;
12291 
12292     if p_list_action_type  = 'INCLUDE' then
12293        if   l_list_entry_source_type <> 'PERSON_LIST' then
12294        x_include_sql  := '
12295               select
12296               count(*) into :1
12297        from   ams_hz_b2b_mapping_v ail
12298             where  enabled_flag = '||''''||'Y'||''''||
12299              ' and import_list_header_id =' || p_incl_object_id   ||
12300              ' and nvl(party_id, import_source_line_id) in (' ;
12301       else
12302        x_include_sql  := '
12303             select
12304               count(*) :1
12305             from ams_hz_b2c_mapping_v
12306             where  enabled_flag = '||''''||'Y'||''''||
12307              ' and import_list_header_id =' || p_incl_object_id   ||
12308              ' and nvl(party_id, import_source_line_id) in (' ;
12309         end if;
12310    end if;
12311    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
12312    if l_no_of_chunks is not null then
12313       for i in 1 ..l_no_of_chunks
12314         loop
12315            WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12316       end loop;
12317    end if;
12318 
12319    x_std_sql := ' select nvl(party_id,import_source_line_id)
12320                   from ams_imp_source_lines
12321                   where  import_list_header_id = ' ||   p_incl_object_id   ||
12322              '  and    nvl(duplicate_flag,' ||''''||'N'||''''||') = '||
12323                                               ''''||'N'||'''' ;
12324    WRITE_TO_ACT_LOG(x_std_sql, 'LIST', g_list_header_id,'LOW');
12325    write_to_act_log('Procedure process_run_total_imph executed', 'LIST', g_list_header_id,'LOW');
12326 END process_run_total_imph ;
12327 
12328 
12329 -- ----------------------------------------------------------------------------
12330 PROCEDURE process_run_total_list
12331              (p_action_used_by_id in number,
12332               p_incl_object_id in number,
12333               p_list_action_type  in varchar2,
12334               p_list_select_action_id   in number,
12335               p_order_number   in number,
12336               p_rank   in number,
12337               p_include_control_group  in varchar2,
12338               x_msg_count      OUT NOCOPY number,
12339               x_msg_data       OUT NOCOPY varchar2,
12340               x_return_status  IN OUT NOCOPY VARCHAR2,
12341               x_std_sql OUT NOCOPY varchar2 ,
12342               x_include_sql OUT NOCOPY varchar2
12343               ) is
12344 l_no_of_chunks  number;
12345 BEGIN
12346     write_to_act_log('Executing procedure process_run_total_list', 'LIST', g_list_header_id,'LOW');
12347     if p_list_action_type  = 'INCLUDE' then
12348        x_include_sql := '
12349                    select
12350               count(*) into :1
12351        from ams_list_entries
12352        where   list_header_id = ' ||   p_incl_object_id   ||
12353         '  and    nvl(enabled_flag,' ||''''||'N'||''''||') = '||
12354                                               ''''||'Y'||'''' ||
12355        ' and list_entry_source_system_id in (' ;
12356 
12357    l_no_of_chunks  := ceil(length(x_include_sql)/2000 );
12358    for i in 1 ..l_no_of_chunks
12359      loop
12360         WRITE_TO_ACT_LOG(substrb(x_include_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12361    end loop;
12362    end if;
12363 
12364    x_std_sql := ' select list_entry_source_system_id
12365                      from ams_list_entries
12366                      where   list_header_id = ' ||   p_incl_object_id   ||
12367                      ' and     enabled_flag = ' || ''''||'Y' || '''' ;
12368         --WRITE_TO_ACT_LOG('std nclude ');
12369    WRITE_TO_ACT_LOG(x_std_sql, 'LIST', g_list_header_id,'LOW');
12370    write_to_act_log('Procedure process_run_total_list executed', 'LIST', g_list_header_id,'LOW');
12371 END process_run_total_list ;
12372 -- ----------------------------------------------------------------------------------
12373 PROCEDURE form_rt_sql_statement(p_select_statement in varchar2,
12374                              p_select_add_statement in varchar2,
12375                              p_master_type        in varchar2,
12376                              p_child_types     in child_type,
12377                              p_from_string in sql_string_4K,
12378                              p_action_used_by_id  in number,
12379                              p_list_select_action_id  in number,
12380                              p_list_action_type  in varchar2,
12381                              p_order_number in number,
12382                              p_rank  in number,
12383                              x_final_string OUT NOCOPY varchar2
12384                              ) is
12385 -- child_type      IS TABLE OF VARCHAR2(80) INDEX  BY BINARY_INTEGER;
12386 l_data_source_types varchar2(2000);
12387 l_field_col_tbl JTF_VARCHAR2_TABLE_100;
12388 l_source_col_tbl JTF_VARCHAR2_TABLE_100;
12389 l_view_tbl JTF_VARCHAR2_TABLE_100;
12390 l_source_col_dt_tbl JTF_VARCHAR2_TABLE_100;
12391 cursor c_master_source_type is
12392 select source_object_name , source_object_name || '.' || source_object_pk_field
12393 from ams_list_src_types
12394 where source_type_code = p_master_type;
12395 cursor c_child_source_type (l_child_src_type varchar2 )is
12396 select a.source_object_name ,
12397        a.source_object_name || '.' || b.sub_source_type_pk_column
12398        ,b.master_source_type_pk_column
12399 from ams_list_src_types  a, ams_list_src_type_assocs b
12400 where a.source_type_code = l_child_src_type
12401 and   b.sub_source_type_id = a.list_source_type_id;
12402 l_count                   number;
12403 l_master_object_name      varchar2(4000);
12404 l_child_object_name       varchar2(4000);
12405 l_master_primary_key      varchar2(1000);
12406 l_child_primary_key       varchar2(32767);
12407 l_from_clause             varchar2(32767);
12408 l_where_clause            varchar2(32767);
12409 l_select_clause           varchar2(32767);
12410 l_insert_clause           varchar2(32767);
12411 l_final_sql               varchar2(32767);
12412 l_insert_sql              varchar2(32767);
12413 l_no_of_chunks            number;
12414 l_master_fkey             Varchar2(1000);
12415 l_dummy_primary_key      varchar2(1000);
12416 begin
12417     write_to_act_log('Executing procedure form_rt_sql_statement', 'LIST', g_list_header_id,'LOW');
12418     WRITE_TO_ACT_LOG('master type is ' || p_master_type, 'LIST', g_list_header_id,'LOW');
12419 open  c_master_source_type;
12420 fetch c_master_source_type into l_master_object_name , l_master_primary_key;
12421 close c_master_source_type;
12422     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');
12423 l_from_clause :=  ' FROM ' || l_master_object_name;
12424 l_data_source_types := ' ('|| ''''|| p_master_type ||'''';
12425 l_where_clause := 'where 1 = 1 ';
12426 
12427 l_count  := p_child_types.count();
12428 if l_count > 0  then
12429    for i in 1..p_child_types.last
12430    loop
12431       l_data_source_types := l_data_source_types || ','|| ''''
12432                              || p_child_types(i)||'''' ;
12433       open  c_child_source_type(p_child_types(i));
12434       fetch c_child_source_type into l_child_object_name , l_child_primary_key
12435                                      ,l_master_fkey;
12436       l_dummy_primary_key := '';
12437       if l_master_fkey is not null then
12438          l_dummy_primary_key     := l_master_object_name || '.'|| l_master_fkey;
12439       else
12440          l_dummy_primary_key      := l_master_primary_key;
12441       end if;
12442       l_from_clause := l_from_clause || ','|| l_child_object_name ;
12443       l_where_clause := l_where_clause || 'and '
12444                               ||l_dummy_primary_key || ' = '
12445                         || l_child_primary_key || '(+)';
12446       close c_child_source_type;
12447    end loop;
12448 end if;
12449 l_data_source_types := l_data_source_types || ') ' ;
12450 
12451  EXECUTE IMMEDIATE
12452      'BEGIN
12453       SELECT b.field_column_name ,
12454                c.source_object_name,
12455                b.source_column_name,
12456                b.field_data_type
12457         BULK COLLECT INTO :1 ,:2  ,:3 ,:4
12458         FROM ams_list_src_fields b, ams_list_src_types c
12459         WHERE b.list_source_type_id = c.list_source_type_id
12460           and b.DE_LIST_SOURCE_TYPE_CODE IN  '|| l_data_source_types ||
12461           ' AND b.ROWID >= (SELECT MAX(a.ROWID)
12462                             FROM ams_list_src_fields a
12463                            WHERE a.field_column_name= b.field_column_name
12464 	                    AND  a.DE_LIST_SOURCE_TYPE_CODE IN '
12465                                  || l_data_source_types || ') ;
12466       END; '
12467   USING OUT l_field_col_tbl ,OUT l_view_tbl , OUT l_source_col_tbl,OUT l_source_col_dt_tbl ;
12468   --WRITE_TO_ACT_LOG('imp: p_select_statement' || p_select_statement);
12469   --WRITE_TO_ACT_LOG('imp: p_select_add_statement' || p_select_add_statement);
12470   --WRITE_TO_ACT_LOG('imp: select clause ' || l_select_clause);
12471 for i in 1 .. l_field_col_tbl.last
12472 loop
12473   l_insert_clause  := l_insert_clause || ' ,' || l_field_col_tbl(i) ;
12474     if l_source_col_dt_tbl(i) = 'DATE' then
12475      l_select_clause  := l_select_clause || ' ,' ||
12476                       'to_char('||l_view_tbl(i) || '.'||l_source_col_tbl(i)||','||''''||'DD-MM-RRRR'||''''||')' ;
12477      else
12478       l_select_clause  := l_select_clause || ' ,' ||
12479                       l_view_tbl(i) || '.'||l_source_col_tbl(i) ;
12480     end if;
12481   --WRITE_TO_ACT_LOG('imp: select clause'||i||':->' || l_select_clause);
12482 end loop;
12483 --  WRITE_TO_ACT_LOG('form_sql_statement:before insert_sql ', 'LIST', g_list_header_id);
12484   l_insert_sql := 'select count(*) into :1 ';
12485    --insert into test3 values (6,'l_insert_sql = :'||l_insert_sql);
12486   --commit;
12487    --insert into test3 values (7,'l_from_clause = :'||l_from_clause);
12488   --commit;
12489      --insert into test3 values (8,'l_where_clause = :'||l_where_clause);
12490   --commit;
12491   --WRITE_TO_ACT_LOG('form_rt_sql_statement:before final sql ', 'LIST', g_list_header_id);
12492      l_final_sql := l_insert_sql || '  ' ||
12493                   l_from_clause ||  '  '||
12494                   l_where_clause   || ' and  ' ||
12495                    l_master_primary_key|| ' in  ( ' ;
12496      x_final_string := l_final_sql;
12497   WRITE_TO_ACT_LOG('Final SQL formed in form_rt_sql_statement', 'LIST', g_list_header_id,'LOW');
12498      l_no_of_chunks  := ceil(length(l_final_sql)/2000 );
12499      for i in 1 ..l_no_of_chunks
12500      loop
12501         WRITE_TO_ACT_LOG(substrb(l_final_sql,(2000*i) - 1999,2000), 'LIST', g_list_header_id,'LOW');
12502      end loop;
12503      write_to_act_log('Procedure form_rt_sql_statement executed', 'LIST', g_list_header_id,'LOW');
12504 exception
12505    when others then
12506      write_to_act_log('Error while executing procedure form_rt_sql_statement '||sqlcode||'   '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
12507 end form_rt_sql_statement;
12508 
12509 -- ----------------------------------------------------------------------------------
12510 PROCEDURE process_rt_insert_sql(p_select_statement in varchar2,
12511                              p_select_add_statement in varchar2,
12512                              p_master_type        in varchar2,
12513                              p_child_types     in child_type,
12514                              p_from_string in sql_string_4K ,
12515                              p_action_used_by_id  in number,
12516                              p_list_select_action_id  in number,
12517                              p_list_action_type  in varchar2,
12518                              p_order_number in number,
12519                              p_rank  in number,
12520                              x_std_sql OUT NOCOPY varchar2 ,
12521                              x_include_sql OUT NOCOPY varchar2
12522                              ) is
12523 l_final_sql   varchar2(32767);
12524 l_insert_sql varchar2(32767);
12525 l_insert_sql1 varchar2(32767);
12526 l_table_name  varchar2(80) := ' ams_list_tmp_entries ';
12527 BEGIN
12528   write_to_act_log('Execution of procedure process_rt_insert_sql started', 'LIST', g_list_header_id,'LOW');
12529 
12530   l_insert_sql := p_select_statement ;
12531   for i in 1 .. p_from_string.last
12532   loop
12533 --     write_to_act_log(p_from_string(i), 'LIST', g_list_header_id);
12534     l_insert_sql  := l_insert_sql || p_from_string(i);
12535   end loop;
12536   x_std_sql := l_insert_sql;
12537   if p_list_action_type = 'INCLUDE' then
12538           form_rt_sql_statement(p_select_statement ,
12539                              p_select_add_statement ,
12540                              p_master_type        ,
12541                              p_child_types     ,
12542                              p_from_string ,
12543                              p_action_used_by_id  ,
12544                              p_list_select_action_id  ,
12545                              p_list_action_type  ,
12546                              p_order_number ,
12547                              p_rank  ,
12548                              l_final_sql
12549                              ) ;
12550   end if;
12551   x_include_sql := l_final_sql;
12552 
12553    write_to_act_log('Procedure process_rt_insert_sql executed', 'LIST', g_list_header_id,'LOW');
12554 exception
12555    when others then
12556    write_to_act_log('Error while executing process_rt_insert_sql '||sqlcode||'  '||sqlerrm , 'LIST', g_list_header_id,'HIGH');
12557 END process_rt_insert_sql;
12558 
12559 -- ----------------------------------------------------------------------------------
12560 PROCEDURE process_rt_all_sql  (p_action_used_by_id in number,
12561                             p_incl_object_id in number,
12562                             p_list_action_type  in varchar2,
12563                             p_list_select_action_id   in number,
12564                             p_order_number   in number,
12565                             p_rank   in number,
12566                             p_include_control_group  in varchar2,
12567                             p_sql_string    in sql_string,
12568                             p_primary_key   in  varchar2,
12569                             p_source_object_name in  varchar2,
12570                             x_msg_count      OUT NOCOPY number,
12571                             x_msg_data       OUT NOCOPY varchar2,
12572                             x_return_status  IN OUT NOCOPY VARCHAR2,
12573                             x_std_sql OUT NOCOPY varchar2 ,
12574                             x_include_sql OUT NOCOPY varchar2
12575                             ) is
12576 l_sql_string         sql_string;
12577 l_where_string       sql_string;
12578 -- l_from_string       sql_string;
12579 l_from_string       sql_string_4K;
12580 l_counter            NUMBER := 1;
12581 l_from_position      number;
12582 l_from_counter       number;
12583 l_end_position      number;
12584 l_end_counter       number;
12585 l_order_position      number;
12586 l_order_counter       number;
12587 l_group_position      number;
12588 l_group_counter       number;
12589 l_found              varchar2(1) := 'N';
12590 l_master_type        varchar2(80);
12591 l_master_type_id     number;
12592 l_source_object_name  varchar2(80);
12593 l_source_object_pk_field  varchar2(80);
12594 l_child_types        child_type;
12595 l_select_condition    varchar2(2000);
12596 l_select_add_condition    varchar2(2000);
12597 l_sql_string_v2           varchar2(4000);
12598 BEGIN
12599   /* Validate Sql String will take all the sql statement fragement and
12600      check if the search string is present. If it is present it will
12601      return the position of fragement and the counter
12602   */
12603   write_to_act_log('Execution of procedure process_rt_all_sql started', 'LIST', g_list_header_id,'LOW');
12604 
12605   l_sql_string := p_sql_string;
12606 
12607   --write_to_act_log('Process_rt_all_sql: start ');
12608   l_found  := 'N';
12609   validate_sql_string(p_sql_string => l_sql_string ,
12610                       p_search_string => 'FROM',
12611                       p_comma_valid   => 'N',
12612                       x_found    => l_found,
12613                       x_position =>l_from_position,
12614                       x_counter => l_from_counter) ;
12615 
12616 --  write_to_act_log('Process_rt_all_sql l_found : '||l_found,'LIST', g_list_header_id);
12617 --  write_to_act_log('Process_rt_all_sql return status: After validate_sql_string call  ','LIST', g_list_header_id);
12618   if l_found = 'N' then
12619      FND_MESSAGE.set_name('AMS', 'AMS_LIST_FROM_NOT_FOUND');
12620      FND_MSG_PUB.Add;
12621      RAISE FND_API.G_EXC_ERROR;
12622   end if;
12623 /*     write_to_act_log('Process_rt_all_sql: FROM Position ->'|| l_from_position ||
12624                                    '<--FROM Counter ->' || l_from_counter ||
12625                                    '<--FROM Found ->' || l_found, 'LIST', g_list_header_id);*/
12626   l_found  := 'N';
12627   get_master_types (p_sql_string => l_sql_string,
12628                     p_start_length => 1,
12629                     p_start_counter => 1,
12630                     p_end_length => l_from_position,
12631                     p_end_counter => l_from_counter,
12632                     x_master_type_id=> l_master_type_id,
12633                     x_master_type=> l_master_type,
12634                     x_found=> l_found,
12635                     x_source_object_name => l_source_object_name,
12636                     x_source_object_pk_field  => l_source_object_pk_field);
12637 
12638   if l_found = 'N' then
12639      FND_MESSAGE.set_name('AMS', 'AMS_LIST_NO_MASTER_TYPE');
12640      FND_MSG_PUB.Add;
12641      RAISE FND_API.G_EXC_ERROR;
12642   end if;
12643   write_to_act_log('Master type is '|| l_master_type ||'<--'  , 'LIST', g_list_header_id,'LOW');
12644 
12645   l_found  := 'N';
12646   write_to_act_log('Calling procedure get_condition'  , 'LIST', g_list_header_id,'LOW');
12647 
12648   get_condition(p_sql_string => l_sql_string ,
12649                 p_search_string => 'FROM',
12650                 p_comma_valid   => 'N',
12651                 x_position =>l_from_position,
12652                 x_counter => l_from_counter,
12653                 x_found    => l_found,
12654                 x_sql_string => l_from_string) ;
12655 
12656   /* FOR SQL STATEMENTS  WHICH ARE NOT FROM THE DERIVING MASTER SOURCE TABLE  */
12657   if p_primary_key is not null then
12658      l_source_object_pk_field := p_primary_key;
12659      l_source_object_name     := p_source_object_name ;
12660   end if;
12661   l_select_condition := 'SELECT ' ||l_source_object_name||'.'
12662                         ||l_source_object_pk_field;
12663                         --||'||'||''''
12664                         --||l_master_type||'''';
12665   l_select_add_condition := ','||l_source_object_name||'.'
12666                         ||l_source_object_pk_field||','||''''
12667                         ||l_master_type||'''' ;
12668 
12669    write_to_act_log('Calling procedure process_rt_insert_sql'  , 'LIST', g_list_header_id,'LOW');
12670    process_rt_insert_sql(p_select_statement       => l_select_condition,
12671                       p_select_add_statement   => l_select_add_condition,
12672                       p_master_type            => l_master_type,
12673                       p_child_types            => l_child_types,
12674                       p_from_string            => l_from_string  ,
12675                       p_list_select_action_id  => p_list_select_action_id  ,
12676                       p_action_used_by_id      => p_action_used_by_id ,
12677                       p_list_action_type       => p_list_action_type ,
12678                       p_order_number           => p_order_number,
12679                       p_rank                   => p_rank,
12680                       x_std_sql                => x_std_sql,
12681                       x_include_sql            => x_include_sql
12682                       );
12683 
12684    write_to_act_log('Procedure process_rt_all_sql executed ', 'LIST', g_list_header_id ,'LOW');
12685 
12686 EXCEPTION
12687    WHEN FND_API.G_EXC_ERROR THEN
12688      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12689      x_return_status := FND_API.G_RET_STS_ERROR ;
12690       FND_MSG_PUB.count_and_get(
12691             p_encoded => FND_API.g_false,
12692             p_count   => x_msg_count,
12693             p_data    => x_msg_data
12694       );
12695 
12696   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
12697      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12698      x_return_status := FND_API.G_RET_STS_ERROR ;
12699       FND_MSG_PUB.count_and_get(
12700             p_encoded => FND_API.g_false,
12701             p_count   => x_msg_count,
12702             p_data    => x_msg_data
12703       );
12704 
12705   WHEN OTHERS THEN
12706      write_to_act_log('Error when executing procedure process_rt_all_sql '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12707      x_return_status := FND_API.G_RET_STS_ERROR ;
12708       FND_MSG_PUB.count_and_get(
12709             p_encoded => FND_API.g_false,
12710             p_count   => x_msg_count,
12711             p_data    => x_msg_data
12712       );
12713 
12714 
12715 END process_rt_all_sql;
12716 
12717 -- ----------------------------------------------------------------------------------
12718 
12719 
12720 PROCEDURE process_run_total_sql (p_action_used_by_id in number,
12721               p_incl_object_id in number,
12722               p_list_action_type  in varchar2,
12723               p_list_select_action_id   in number,
12724               p_order_number   in number,
12725               p_rank   in number,
12726               p_include_control_group  in varchar2,
12727               x_msg_count      OUT NOCOPY number,
12728               x_msg_data       OUT NOCOPY varchar2,
12729               x_return_status  IN OUT NOCOPY VARCHAR2,
12730               x_std_sql OUT NOCOPY varchar2 ,
12731               x_include_sql OUT NOCOPY varchar2
12732               ) is
12733 
12734 ------------------------------------------------------------------------------
12735 -- Given the sql id from ams_list_select_actions it will retrieve the
12736 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12737 -- workbook_name.
12738 ------------------------------------------------------------------------------
12739 cursor cur_sql  is
12740 SELECT query, primary_key, source_object_name
12741 FROM   ams_list_queries_all
12742 WHERE  (list_query_id = p_incl_object_id )
12743 or (parent_list_query_id = p_incl_object_id )
12744 order by sequence_order;
12745 
12746 /* sql_string column is obsolete: bug 4604653
12747 cursor cur_old_sql  is
12748 SELECT sql_string, primary_key, source_object_name
12749 FROM   ams_list_queries_all
12750 WHERE  (list_query_id = p_incl_object_id )
12751 or (parent_list_query_id = p_incl_object_id )
12752 order by sequence_order;
12753 */
12754 
12755 cursor cur_primary_key_sql  is
12756 SELECT lc.SOURCE_OBJECT_NAME, lc.SOURCE_OBJECT_PK_FIELD
12757 FROM   ams_list_queries_all  lq,
12758        ams_list_headers_all lh,
12759        ams_list_src_types  lc
12760 WHERE lq.list_query_id = p_incl_object_id
12761 and   lq.ARC_ACT_LIST_QUERY_USED_BY = 'LIST'
12762 and   lq.ACT_LIST_QUERY_USED_BY_ID = lh.list_header_id
12763 and   lc.source_type_code = lh.list_source_type;
12764 
12765 l_sql_string         sql_string;
12766 l_where_string       sql_string;
12767 l_from_string       sql_string;
12768 l_counter            NUMBER := 1;
12769 l_from_position      number;
12770 l_from_counter       number;
12771 l_end_position      number;
12772 l_end_counter       number;
12773 l_order_position      number;
12774 l_order_counter       number;
12775 l_group_position      number;
12776 l_group_counter       number;
12777 l_found              varchar2(1);
12778 l_master_type        varchar2(80);
12779 l_master_type_id     number;
12780 l_source_object_name  varchar2(80);
12781 l_source_object_pk_field  varchar2(80);
12782 l_child_types        child_type;
12783 l_select_condition    varchar2(2000);
12784 l_select_add_condition    varchar2(2000);
12785 l_sql_string_v2           varchar2(4000);
12786 l_primary_key          varchar2(80);
12787 l_no_pieces            number :=0;
12788 l_big_sql VARCHAR2(32767);
12789 BEGIN
12790   write_to_act_log('Procedure process_run_total_sql started ', 'LIST', g_list_header_id ,'LOW');
12791   open cur_sql;
12792   loop
12793     fetch cur_sql into l_big_sql,l_primary_key,l_source_object_name;
12794     exit when cur_sql%notfound ;
12795     write_to_act_log('Incl object id' || p_incl_object_id, 'LIST', g_list_header_id,'LOW');
12796     l_no_pieces := ceil(length(l_big_sql)/2000);
12797     write_to_act_log('length of SQL' || l_no_pieces, 'LIST', g_list_header_id,'LOW');
12798     if l_no_pieces  > 0 then
12799        for i  in 1 .. l_no_pieces
12800        loop
12801           write_to_act_log('number of chunks ' || i, 'LIST', g_list_header_id,'LOW');
12802           --write_to_act_log('Process_sql: before ' );
12803           l_sql_string(l_counter):= substrb(l_big_sql,2000*i -1999,2000);
12804           --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
12805           l_counter  := l_counter +1 ;
12806        end loop;
12807     end if;
12808     -- l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
12809     -- l_counter  := l_counter +1 ;
12810   end loop;
12811   close cur_sql;
12812 
12813 --    write_to_act_log('lenth of pieces:' || l_no_pieces, 'LIST', g_list_header_id);
12814 /*
12815   if l_no_pieces = 0   or
12816      l_no_pieces is null then
12817      open cur_old_sql;
12818      loop
12819        fetch cur_old_sql into l_sql_string_v2,l_primary_key,l_source_object_name;
12820   --     write_to_act_log('Process_sql old cursor 4000->:' || p_incl_object_id, 'LIST', g_list_header_id);
12821        exit when cur_old_sql%notfound ;
12822        --write_to_act_log('Process_sql: before ' );
12823        l_sql_string(l_counter):= substrb(l_sql_string_v2,1,2000);
12824        --write_to_act_log('Process_sql:' || l_sql_string(l_counter));
12825        l_counter  := l_counter +1 ;
12826        l_sql_string(l_counter):= substrb(l_sql_string_v2,2001,2000);
12827        l_counter  := l_counter +1 ;
12828      end loop;
12829      close cur_old_sql;
12830   end if;
12831   */
12832   if l_source_object_name is null or
12833      l_primary_key is null then
12834      open cur_primary_key_sql  ;
12835      loop
12836        fetch cur_primary_key_sql into l_primary_key,l_source_object_name;
12837        exit when cur_primary_key_sql%notfound ;
12838      end loop;
12839      close cur_primary_key_sql  ;
12840   end if;
12841   write_to_act_log('Calling procedure process_rt_all_sql', 'LIST', g_list_header_id,'LOW');
12842   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
12843                   p_incl_object_id => p_incl_object_id ,
12844                   p_list_action_type  => p_list_action_type  ,
12845                   p_list_select_action_id   => p_list_select_action_id   ,
12846                   p_order_number   => p_order_number   ,
12847                   p_rank   => p_rank   ,
12848                   p_include_control_group  => p_include_control_group,
12849                   p_sql_string    => l_sql_string    ,
12850                   p_primary_key   => l_primary_key,
12851                   p_source_object_name   => l_source_object_name,
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                   );
12858   write_to_act_log('Procedure process_run_total_sql executed', 'LIST', g_list_header_id,'LOW'  );
12859 
12860 exception
12861    when others then
12862     write_to_act_log('Error while executing process_run_total_sql ' ||sqlcode||'   '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
12863     x_return_status := FND_API.G_RET_STS_ERROR ;
12864 END process_run_total_sql;
12865 
12866 -- ----------------------------------------------------------------------------------------------
12867 PROCEDURE process_run_total_cell
12868              (p_action_used_by_id in  number,
12869               p_incl_object_id in number,
12870               p_list_action_type  in varchar2,
12871               p_list_select_action_id   in number,
12872               p_order_number   in number,
12873               p_rank   in number,
12874               p_include_control_group  in varchar2,
12875               x_msg_count      OUT NOCOPY number,
12876               x_msg_data       OUT NOCOPY varchar2,
12877               x_return_status  IN OUT NOCOPY VARCHAR2,
12878               x_std_sql OUT NOCOPY varchar2 ,
12879               x_include_sql OUT NOCOPY varchar2
12880                ) is
12881 
12882 ------------------------------------------------------------------------------
12883 -- Given the sql id from ams_list_select_actions it will retrieve the
12884 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12885 -- workbook_name.
12886 ------------------------------------------------------------------------------
12887 l_sql_string         sql_string;
12888 l_where_string       sql_string;
12889 l_from_string       sql_string;
12890 l_counter            NUMBER := 1;
12891 l_from_position      number;
12892 l_from_counter       number;
12893 l_end_position      number;
12894 l_end_counter       number;
12895 l_order_position      number;
12896 l_order_counter       number;
12897 l_group_position      number;
12898 l_group_counter       number;
12899 l_found              varchar2(1);
12900 l_master_type        varchar2(80);
12901 l_master_type_id     number;
12902 l_source_object_name  varchar2(80);
12903 l_source_object_pk_field  varchar2(80);
12904 l_child_types        child_type;
12905 l_select_condition    varchar2(2000);
12906 l_select_add_condition    varchar2(2000);
12907 l_msg_data       VARCHAR2(2000);
12908 l_msg_count      number;
12909 l_sql_2          DBMS_SQL.VARCHAR2S;
12910 l_sql_string_final    varchar2(4000);
12911 j number     := 1;
12912 BEGIN
12913 
12914   write_to_act_log('Executing procedure process_run_total_cell', 'LIST', g_list_header_id,'LOW'  );
12915   ams_cell_pvt.get_comp_sql(
12916       p_api_version       => 1.0,
12917       p_init_msg_list     => FND_API.g_false,
12918       p_validation_level  => FND_API.g_valid_level_full,
12919       x_return_status     => x_return_status,
12920       x_msg_count         => x_msg_count ,
12921       x_msg_data           =>x_msg_data,
12922       p_cell_id           => p_incl_object_id ,
12923       p_party_id_only     => FND_API.g_false,
12924       x_sql_tbl           => l_sql_2
12925    );
12926     write_to_act_log('AMS_ListGeneration_PKG.After Comp sql:', 'LIST', g_list_header_id,'LOW');
12927 
12928   l_sql_string_final := '';
12929   for i in 1 .. l_sql_2.last
12930   loop
12931       l_sql_string_final := l_sql_string_final || l_sql_2(i);
12932      if length(l_sql_string_final) > 2000 then
12933         l_sql_string(j) := substrb(l_sql_string_final,1,2000);
12934         l_sql_string_final := substrb(l_sql_string_final,2001 ,2000);
12935         j := j+1;
12936      end if;
12937   end loop;
12938   l_sql_string(j) := substrb(l_sql_string_final,1,2000);
12939   if length(l_sql_string_final) > 2000 then
12940     j := j+1;
12941     l_sql_string(j) := substrb(l_sql_string_final,2001 ,2000);
12942   end if;
12943   write_to_act_log('process_rt_all_sql', 'LIST', g_list_header_id,'LOW');
12944 
12945   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
12946                   p_incl_object_id => p_incl_object_id ,
12947                   p_list_action_type  => p_list_action_type  ,
12948                   p_list_select_action_id   => p_list_select_action_id   ,
12949                   p_order_number   => p_order_number   ,
12950                   p_rank   => p_rank   ,
12951                   p_include_control_group  => p_include_control_group,
12952                   p_sql_string    => l_sql_string    ,
12953                   x_msg_count      => x_msg_count      ,
12954                   x_msg_data   => x_msg_data   ,
12955                   x_return_status   => x_return_status   ,
12956                   x_std_sql                => x_std_sql,
12957                   x_include_sql            => x_include_sql,
12958                   p_primary_key   => null,
12959                   p_source_object_name => null);
12960 
12961     write_to_act_log('Procedure process_run_total_cell executed.', 'LIST', g_list_header_id,'LOW');
12962 --No exception??? need to include one..
12963 
12964 END process_run_total_cell ;
12965 
12966 -- -----------------------------------------------------------------------------------------------
12967 PROCEDURE process_run_total_diwb (p_action_used_by_id in number,
12968               p_incl_object_id in number,
12969               p_list_action_type  in varchar2,
12970               p_list_select_action_id   in number,
12971               p_order_number   in number,
12972               p_rank   in number,
12973               p_include_control_group  in varchar2,
12974               x_msg_count      OUT NOCOPY number,
12975               x_msg_data       OUT NOCOPY varchar2,
12976               x_return_status  IN OUT NOCOPY VARCHAR2,
12977               x_std_sql OUT NOCOPY varchar2 ,
12978               x_include_sql OUT NOCOPY varchar2
12979               ) is
12980 
12981 ------------------------------------------------------------------------------
12982 -- Given the sql id from ams_list_select_actions it will retrieve the
12983 -- sql_srtings from ams_discoverer_sql for a particular worksheet_name and
12984 -- workbook_name.
12985 ------------------------------------------------------------------------------
12986 cursor cur_diwb(l_incl_object_id  in number )  is
12987 SELECT sql_string
12988 FROM   ams_discoverer_sql
12989 WHERE  (workbook_name, worksheet_name )
12990 IN
12991 ( SELECT workbook_name, worksheet_name
12992   FROM   ams_discoverer_sql
12993   WHERE  discoverer_sql_id = l_incl_object_id)
12994 ORDER BY sequence_order;
12995 
12996 l_sql_string         sql_string;
12997 l_where_string       sql_string;
12998 l_from_string       sql_string;
12999 l_counter            NUMBER := 1;
13000 l_from_position      number;
13001 l_from_counter       number;
13002 l_end_position      number;
13003 l_end_counter       number;
13004 l_order_position      number;
13005 l_order_counter       number;
13006 l_group_position      number;
13007 l_group_counter       number;
13008 l_found              varchar2(1);
13009 l_master_type        varchar2(80);
13010 l_master_type_id     number;
13011 l_source_object_name  varchar2(80);
13012 l_source_object_pk_field  varchar2(80);
13013 l_child_types        child_type;
13014 l_select_condition    varchar2(2000);
13015 l_select_add_condition    varchar2(2000);
13016 BEGIN
13017   write_to_act_log('Executing procedure process_run_total_diwb', 'LIST', g_list_header_id,'LOW');
13018 
13019   /* Populating l_sql_string with sql statements from ams_discoverer_sql
13020      l_sql_string is of table type of varchar2(2000)
13021   */
13022   open cur_diwb(p_incl_object_id);
13023   loop
13024     fetch cur_diwb into l_sql_string(l_counter);
13025     exit when cur_diwb%notfound ;
13026     l_counter  := l_counter +1 ;
13027   end loop;
13028   close cur_diwb;
13029   write_to_act_log('Calling procedure process_rt_all_sql','LIST', g_list_header_id,'LOW');
13030   process_rt_all_sql(p_action_used_by_id => p_action_used_by_id ,
13031                   p_incl_object_id => p_incl_object_id ,
13032                   p_list_action_type  => p_list_action_type  ,
13033                   p_list_select_action_id   => p_list_select_action_id   ,
13034                   p_order_number   => p_order_number   ,
13035                   p_rank   => p_rank   ,
13036                   p_include_control_group  => p_include_control_group,
13037                   p_sql_string    => l_sql_string,
13038 		  p_primary_key   => null,
13039 		  p_source_object_name => null,
13040                   x_msg_count      => x_msg_count      ,
13041                   x_msg_data   => x_msg_data   ,
13042                   x_return_status   => x_return_status   ,
13043                   x_std_sql                => x_std_sql,
13044                   x_include_sql            => x_include_sql);
13045     write_to_act_log('Procedure process_run_total_diwb executed','LIST', g_list_header_id,'LOW');
13046 END process_run_total_diwb ;
13047 
13048 -- ------------------------------------------------------------------------------------------------
13049 PROCEDURE  tca_upload_process
13050              (p_list_header_id  in  number,
13051               p_log_flag           in  varchar2  ,-- DEFAULT 'Y',
13052               x_return_status      OUT NOCOPY VARCHAR2,
13053               x_msg_count          OUT NOCOPY NUMBER,
13054               x_msg_data           OUT NOCOPY VARCHAR2) IS
13055 
13056 l_list_entry_id		Number;
13057 list_column_name	Varchar2(30);
13058 tca_column_name		Varchar2(30);
13059 l_string		Varchar2(2000);
13060 l_entry_value		Varchar2(1000);
13061 l_b2b_flag		Varchar2(6);
13062 l_b2b			Varchar2(1);
13063 l_party_id		Number;
13064 l_component_name	Varchar2(30);
13065 l_new_party		Varchar2(1);
13066 x_tmp_var                       VARCHAR2(4000);
13067 x_tmp_var1                      VARCHAR2(4000);
13068 
13069  party_rec       hz_party_v2pub.party_rec_type;
13070  org_rec         hz_party_v2pub.organization_rec_type;
13071  person_rec      hz_party_v2pub.person_rec_type;
13072  location_rec    hz_location_v2pub.location_rec_type;
13073  psite_rec       hz_party_site_v2pub.party_site_rec_type;
13074  psiteuse_rec    hz_party_site_v2pub.party_site_use_rec_type;
13075  cpoint_rec      hz_contact_point_v2pub.contact_point_rec_type;
13076  email_rec       hz_contact_point_v2pub.email_rec_type;
13077  phone_rec       hz_contact_point_v2pub.phone_rec_type;
13078  fax_rec         hz_contact_point_v2pub.phone_rec_type;
13079  ocon_rec        hz_party_contact_v2pub.org_contact_rec_type;
13080  edi_rec         hz_contact_point_v2pub.edi_rec_type;
13081  telex_rec       hz_contact_point_v2pub.telex_rec_type;
13082  web_rec         hz_contact_point_v2pub.web_rec_type;
13083 
13084 
13085 cursor c_list_entries is
13086 select list_entry_id from ams_list_entries where list_header_id = p_list_header_id
13087    -- and party_id is null
13088    and nvl(tca_load_status,'x') not in ('SUCCESS','ERROR')
13089    and enabled_flag = 'Y'; --bmuthukr. R12 Need to upload only the enabled entries.
13090 
13091 cursor c_tca_columns is
13092 select flds.field_column_name , tca.column_name
13093 from ams_list_src_fields flds, AMS_DS_TCA_ENTITY_COLS tca,
13094       ams_list_headers_all hdr, ams_list_src_types typ
13095 where hdr.list_header_id = p_list_header_id
13096   and hdr.LIST_SOURCE_TYPE = typ.SOURCE_TYPE_CODE
13097   and typ.list_source_type_id = flds.list_source_type_id
13098   and flds.enabled_flag = 'Y'
13099   and used_in_list_entries = 'Y'
13100   and flds.tca_column_id = tca.ENTITY_COLUMN_ID;
13101 
13102 cursor c_data_type is
13103 select source_category from ams_list_src_types types, ams_list_headers_all head
13104 where head.list_header_id = p_list_header_id
13105 and head.list_source_type = types.source_type_code;
13106 
13107 l_prof VARCHAR2(50);
13108 
13109 BEGIN
13110   write_to_act_log('Executing procedure tca_upload_process', 'LIST', p_list_header_id,'LOW');
13111 
13112   --TCA mandate: bug 4587049
13113   --Disable TCA events before bulk TCA data processing
13114   l_prof := fnd_profile.value('HZ_EXECUTE_API_CALLOUTS');
13115   if l_prof <> 'N' then
13116      fnd_profile.put('HZ_EXECUTE_API_CALLOUTS','N');
13117   end if;
13118 
13119   open c_data_type;
13120   fetch c_data_type into l_b2b_flag;
13121   close c_data_type;
13122   write_to_act_log('Source category is '||l_b2b_flag, 'LIST', p_list_header_id,'LOW');
13123   open c_list_entries;
13124   LOOP
13125      fetch c_list_entries into l_list_entry_id;
13126      Exit when c_list_entries%notfound;
13127      open c_tca_columns;
13128      loop
13129        fetch c_tca_columns into list_column_name, tca_column_name;
13130        exit when c_tca_columns%notfound;
13131        l_string := 'begin select '||list_column_name||' into :l_entry_value from ams_list_entries where
13132                     list_entry_id = '||to_char(l_list_entry_id)||' ; end;';
13133  -- DBMS_OUTPUT.PUT_LINE('l_string  = '||l_string);
13134 	execute immediate l_string using out l_entry_value;
13135   -- DBMS_OUTPUT.PUT_LINE('l_entry_value = '||l_entry_value);
13136     if l_b2b_flag = 'B2B' then
13137 	l_b2b := 'Y';
13138        if tca_column_name = 'PARTY_NAME' then
13139 	 org_rec.organization_name := l_entry_value;
13140   -- DBMS_OUTPUT.PUT_LINE('org_rec.organization_name = '||org_rec.organization_name);
13141        end if;
13142        if tca_column_name = 'FISCAL_YEAREND_MONTH' then
13143            org_rec.fiscal_yearend_month := l_entry_value;
13144        elsif tca_column_name = 'DUNS_NUMBER_C' then
13145            org_rec.duns_number_c := l_entry_value;
13146        elsif tca_column_name = 'EMPLOYEES_TOTAL' then
13147            org_rec.employees_total := l_entry_value;
13148        elsif tca_column_name = 'LINE_OF_BUSINESS' then
13149            org_rec.line_of_business := l_entry_value;
13150        elsif tca_column_name = 'YEAR-ESTABLISHED' then
13151            org_rec.year_established := l_entry_value;
13152        elsif tca_column_name = 'TAX_REFERENCE' then
13153            org_rec.tax_reference := l_entry_value;
13154        elsif tca_column_name = 'CEO_NAME' then
13155            org_rec.ceo_name := l_entry_value;
13156        elsif tca_column_name = 'PERSON_FIRST_NAME' then
13157            person_rec.person_first_name := l_entry_value;
13158        elsif tca_column_name = 'PERSON_MIDDLE_NAME' then
13159            person_rec.person_middle_name := l_entry_value;
13160        elsif tca_column_name = 'PERSON_LAST_NAME' then
13161            person_rec.person_last_name := l_entry_value;
13162        elsif tca_column_name = 'PERSON_NAME_SUFFIX' then
13163            person_rec.person_name_suffix := l_entry_value;
13164        elsif tca_column_name = 'PERSON_PRE_NAME_ADJUNCT' then
13165            person_rec.person_pre_name_adjunct := l_entry_value;
13166        elsif tca_column_name = upper('country') then
13167            location_rec.country := l_entry_value;
13168        elsif tca_column_name = upper('address1') then
13169            location_rec.address1 := l_entry_value;
13170        elsif tca_column_name = upper('address2') then
13171            location_rec.address2 := l_entry_value;
13172        elsif tca_column_name = upper('city') then
13173            location_rec.city := l_entry_value;
13174        elsif tca_column_name = upper('county') then
13175            location_rec.county := l_entry_value;
13176        elsif tca_column_name = upper('state') then
13177            location_rec.state := l_entry_value;
13178        elsif tca_column_name = upper('province') then
13179            location_rec.province := l_entry_value;
13180        elsif tca_column_name = upper('postal_code') then
13181            location_rec.postal_code := l_entry_value;
13182        elsif tca_column_name = upper('email_address') then
13183            email_rec.email_address := l_entry_value;
13184        elsif tca_column_name = upper('phone_country_code') then
13185            phone_rec.phone_country_code := l_entry_value;
13186        elsif tca_column_name = upper('phone_area_code') then
13187            phone_rec.phone_area_code := l_entry_value;
13188        elsif tca_column_name = upper('phone_number') then
13189            phone_rec.phone_number := l_entry_value;
13190        elsif tca_column_name = upper('phone_extension') then
13191            phone_rec.phone_extension := l_entry_value;
13192        elsif tca_column_name = upper('department') then
13193            ocon_rec.department := l_entry_value;
13194        elsif tca_column_name = upper('job_title') then
13195            ocon_rec.job_title := l_entry_value;
13196        elsif tca_column_name = upper('decision_maker_flag') then
13197            ocon_rec.decision_maker_flag := l_entry_value;
13198        elsif tca_column_name = upper('sic_code') then
13199            org_rec.sic_code := l_entry_value;
13200        elsif tca_column_name = upper('sic_code_type') then
13201            org_rec.sic_code_type := l_entry_value;
13202        elsif tca_column_name = upper('analysis_fy') then
13203            org_rec.analysis_fy := l_entry_value;
13204        elsif tca_column_name = upper('CURR_FY_POTENTIAL_REVENUE') then
13205            org_rec.CURR_FY_POTENTIAL_REVENUE := l_entry_value;
13206        elsif tca_column_name = upper('NEXT_FY_POTENTIAL_REVENUE') then
13207            org_rec.NEXT_FY_POTENTIAL_REVENUE := l_entry_value;
13208        elsif tca_column_name = upper('GSA_INDICATOR_FLAG') then
13209            org_rec.GSA_INDICATOR_FLAG := l_entry_value;
13210        elsif tca_column_name = upper('MISSION_STATEMENT') then
13211            org_rec.MISSION_STATEMENT := l_entry_value;
13212        elsif tca_column_name = upper('ORGANIZATION_NAME_PHONETIC') then
13213            org_rec.ORGANIZATION_NAME_PHONETIC := l_entry_value;
13214        elsif tca_column_name = upper('CATEGORY_CODE') then
13215            org_rec.party_rec.CATEGORY_CODE := l_entry_value;
13216        elsif tca_column_name = upper('JGZZ_FISCAL_CODE') then
13217            org_rec.JGZZ_FISCAL_CODE := l_entry_value;
13218        elsif tca_column_name = upper('ADDRESS3') then
13219            location_rec.ADDRESS3 := l_entry_value;
13220        elsif tca_column_name = upper('ADDRESS4') then
13221            location_rec.ADDRESS4 := l_entry_value;
13222        elsif tca_column_name = upper('ADDRESS_LINES_PHONETIC') then
13223            location_rec.ADDRESS_LINES_PHONETIC := l_entry_value;
13224        elsif tca_column_name = upper('PO_BOX_NUMBER') then
13225            -- location_rec.PO_BOX_NUMBER := l_entry_value; Refer bug 4704727
13226 	   location_rec.PO_BOX_NUMBER := null;
13227        elsif tca_column_name = upper('HOUSE_NUMBER') then
13228            -- location_rec.HOUSE_NUMBER := l_entry_value; Refer bug 4704727
13229 	   location_rec.HOUSE_NUMBER := null;
13230        elsif tca_column_name = upper('STREET_SUFFIX') then
13231            -- location_rec.STREET_SUFFIX := l_entry_value; Refer bug 4704727
13232 	   location_rec.STREET_SUFFIX := null;
13233        elsif tca_column_name = upper('STREET') then
13234            -- location_rec.STREET := l_entry_value; Refer bug 4704727
13235 	   location_rec.STREET := null;
13236        elsif tca_column_name = upper('STREET_NUMBER') then
13237 	   -- location_rec.STREET_NUMBER := l_entry_value; Refer bug 4704727
13238 	   location_rec.STREET_NUMBER := null;
13239        elsif tca_column_name = upper('FLOOR') then
13240            -- location_rec.FLOOR := l_entry_value; Refer bug 4704727
13241 	   location_rec.FLOOR := null;
13242        elsif tca_column_name = upper('SUITE') then
13243            -- location_rec.SUITE := l_entry_value; Refer bug 4704727
13244 	   location_rec.SUITE := null;
13245        elsif tca_column_name = upper('POSTAL_PLUS4_CODE') then
13246            location_rec.POSTAL_PLUS4_CODE := l_entry_value;
13247        elsif tca_column_name = upper('identifying_address_flag') then
13248            psite_rec.identifying_address_flag := l_entry_value;
13249        elsif tca_column_name = upper('address_effective_date') then
13250            location_rec.address_effective_date := l_entry_value;
13251        elsif tca_column_name = upper('address_expiration_date') then
13252            location_rec.address_expiration_date := l_entry_value;
13253        elsif tca_column_name = upper('branch_flag') then
13254            org_rec.branch_flag := l_entry_value;
13255        elsif tca_column_name = upper('line_of_business') then
13256            org_rec.line_of_business := l_entry_value;
13257        elsif tca_column_name = upper('business_scope') then
13258            org_rec.business_scope := l_entry_value;
13259        elsif tca_column_name = upper('ceo_title') then
13260            org_rec.ceo_title := l_entry_value;
13261        elsif tca_column_name = upper('cong_dist_code') then
13262            org_rec.cong_dist_code := l_entry_value;
13263        elsif tca_column_name = upper('control_yr') then
13264            org_rec.control_yr := l_entry_value;
13265        elsif tca_column_name = upper('corporation_class') then
13266            org_rec.corporation_class := l_entry_value;
13267        elsif tca_column_name = upper('credit_score') then
13268            org_rec.credit_score := l_entry_value;
13269        elsif tca_column_name = upper('credit_score_commentary') then
13270            org_rec.credit_score_commentary := l_entry_value;
13271        elsif tca_column_name = upper('db_rating') then
13272            org_rec.db_rating := l_entry_value;
13273        elsif tca_column_name = upper('date_of_birth') then
13274            person_rec.date_of_birth := l_entry_value;
13275        elsif tca_column_name = upper('') then
13276            person_rec.date_of_death := l_entry_value;
13277        elsif tca_column_name = upper('date_of_death') then
13278            org_rec.debarments_count := l_entry_value;
13279        elsif tca_column_name = upper('debarments_date') then
13280            org_rec.debarments_date := l_entry_value;
13281        elsif tca_column_name = upper('declared_ethnicity') then
13282            person_rec.declared_ethnicity := l_entry_value;
13283        elsif tca_column_name = upper('debarment_ind') then
13284            org_rec.debarment_ind := l_entry_value;
13285        elsif tca_column_name = upper('description') then
13286            location_rec.description := l_entry_value;
13287        elsif tca_column_name = upper('disadv_8a_ind') then
13288            org_rec.disadv_8a_ind := l_entry_value;
13289        elsif tca_column_name = upper('enquiry_duns') then
13290            org_rec.enquiry_duns := l_entry_value;
13291        elsif tca_column_name = upper('export_ind') then
13292            org_rec.export_ind := l_entry_value;
13293        elsif tca_column_name = upper('failure_score') then
13294            org_rec.failure_score := l_entry_value;
13295        elsif tca_column_name = upper('failure_score_commentary') then
13296            org_rec.failure_score_commentary := l_entry_value;
13297        elsif tca_column_name = upper('failure_score_natnl_percentile') then
13298            org_rec.failure_score_natnl_percentile := l_entry_value;
13299        elsif tca_column_name = upper('failure_score_override_code') then
13300            org_rec.failure_score_override_code := l_entry_value;
13301        elsif tca_column_name = upper('global_failure_score') then
13302            org_rec.global_failure_score := l_entry_value;
13303        elsif tca_column_name = upper('hq_branch_ind') then
13304            org_rec.hq_branch_ind := l_entry_value;
13305        elsif tca_column_name = upper('head_of_household_flag') then
13306            person_rec.head_of_household_flag := l_entry_value;
13307        elsif tca_column_name = upper('household_size') then
13308            person_rec.household_size := l_entry_value;
13309        elsif tca_column_name = upper('import_ind') then
13310            org_rec.import_ind := l_entry_value;
13311        elsif tca_column_name = upper('known_as') then
13312            org_rec.known_as := l_entry_value;
13313        elsif tca_column_name = upper('known_as2') then
13314            org_rec.known_as2 := l_entry_value;
13315        elsif tca_column_name = upper('known_as3') then
13316            org_rec.known_as3 := l_entry_value;
13317        elsif tca_column_name = upper('known_as4') then
13318            org_rec.known_as4 := l_entry_value;
13319        elsif tca_column_name = upper('known_as5') then
13320            org_rec.known_as5 := l_entry_value;
13321        elsif tca_column_name = upper('known_as') then
13322            person_rec.known_as := l_entry_value;
13323        elsif tca_column_name = upper('known_as2') then
13324            person_rec.known_as2 := l_entry_value;
13325        elsif tca_column_name = upper('') then
13326            person_rec.known_as3 := l_entry_value;
13327        elsif tca_column_name = upper('known_as3') then
13328            person_rec.known_as4 := l_entry_value;
13329        elsif tca_column_name = upper('known_as5') then
13330            person_rec.known_as5 := l_entry_value;
13331        elsif tca_column_name = upper('labor_surplus_ind') then
13332            org_rec.labor_surplus_ind := l_entry_value;
13333        elsif tca_column_name = upper('local_activity_code') then
13334            org_rec.local_activity_code := l_entry_value;
13335        elsif tca_column_name = upper('local_activity_code_type') then
13336            org_rec.local_activity_code_type := l_entry_value;
13337        elsif tca_column_name = upper('location_directions') then
13338            location_rec.location_directions := l_entry_value;
13339        elsif tca_column_name = upper('marital_status') then
13340            person_rec.marital_status := l_entry_value;
13341        elsif tca_column_name = upper('marital_status_effective_date') then
13342            person_rec.marital_status_effective_date := l_entry_value;
13343        elsif tca_column_name = upper('minority_owned_ind') then
13344            org_rec.minority_owned_ind := l_entry_value;
13345        elsif tca_column_name = upper('minority_owned_type') then
13346            org_rec.minority_owned_type := l_entry_value;
13347        elsif tca_column_name = upper('organization_type') then
13348            org_rec.organization_type := l_entry_value;
13349        elsif tca_column_name = upper('') then
13350            web_rec.url := l_entry_value;
13351        elsif tca_column_name = upper('url') then
13352            org_rec.oob_ind := l_entry_value;
13353        elsif tca_column_name = upper('personal_income') then
13354            person_rec.personal_income := l_entry_value;
13355        elsif tca_column_name = upper('person_academic_title') then
13356            person_rec.person_academic_title := l_entry_value;
13357        elsif tca_column_name = upper('person_first_name_phonetic') then
13358            person_rec.person_first_name_phonetic := l_entry_value;
13359        elsif tca_column_name = upper('person_last_name_phonetic') then
13360            person_rec.person_last_name_phonetic := l_entry_value;
13361        elsif tca_column_name = upper('middle_name_phonetic') then
13362            person_rec.middle_name_phonetic := l_entry_value;
13363        elsif tca_column_name = upper('person_name_phonetic') then
13364            person_rec.person_name_phonetic := l_entry_value;
13365        elsif tca_column_name = upper('person_previous_last_name') then
13366            person_rec.person_previous_last_name := l_entry_value;
13367        elsif tca_column_name = upper('place_of_birth') then
13368            person_rec.place_of_birth := l_entry_value;
13369        elsif tca_column_name = upper('principal_name') then
13370            org_rec.principal_name := l_entry_value;
13371        elsif tca_column_name = upper('principal_title') then
13372            org_rec.principal_title := l_entry_value;
13373        elsif tca_column_name = upper('public_private_ownership_flag') then
13374            org_rec.public_private_ownership_flag := l_entry_value;
13375        elsif tca_column_name = upper('') then
13376            org_rec.rent_own_ind := l_entry_value;
13377        elsif tca_column_name = upper('person_academic_title') then
13378            person_rec.person_academic_title := l_entry_value;
13379        elsif tca_column_name = upper('short_description') then
13380            location_rec.short_description := l_entry_value;
13381        elsif tca_column_name = upper('small_bus_ind') then
13382            org_rec.small_bus_ind := l_entry_value;
13383        elsif tca_column_name = upper('woman_owned_ind') then
13384            org_rec.woman_owned_ind := l_entry_value;
13385        elsif tca_column_name = upper('attribute1') then
13386            org_rec.party_rec.attribute1 := l_entry_value;
13387        elsif tca_column_name = upper('attribute2') then
13388            org_rec.party_rec.attribute2 := l_entry_value;
13389        elsif tca_column_name = upper('attribute3') then
13390            org_rec.party_rec.attribute3 := l_entry_value;
13391        elsif tca_column_name = upper('attribute4') then
13392            org_rec.party_rec.attribute4 := l_entry_value;
13393        elsif tca_column_name = upper('attribute5') then
13394            org_rec.party_rec.attribute5 := l_entry_value;
13395        elsif tca_column_name = upper('attribute6') then
13396            org_rec.party_rec.attribute6 := l_entry_value;
13397        elsif tca_column_name = upper('attribute7') then
13398            org_rec.party_rec.attribute7 := l_entry_value;
13399        elsif tca_column_name = upper('attribute8') then
13400            org_rec.party_rec.attribute8 := l_entry_value;
13401        elsif tca_column_name = upper('attribute9') then
13402            org_rec.party_rec.attribute9 := l_entry_value;
13403        elsif tca_column_name = upper('attribute10') then
13404            org_rec.party_rec.attribute10 := l_entry_value;
13405        elsif tca_column_name = upper('attribute11') then
13406            org_rec.party_rec.attribute11 := l_entry_value;
13407        elsif tca_column_name = upper('attribute12') then
13408            org_rec.party_rec.attribute12 := l_entry_value;
13409        elsif tca_column_name = upper('attribute13') then
13410            org_rec.party_rec.attribute13 := l_entry_value;
13411        elsif tca_column_name = upper('attribute14') then
13412            org_rec.party_rec.attribute14 := l_entry_value;
13413        elsif tca_column_name = upper('') then
13414            org_rec.party_rec.attribute15 := l_entry_value;
13415        elsif tca_column_name = upper('attribute1') then
13416            person_rec.attribute1 := l_entry_value;
13417        elsif tca_column_name = upper('attribute2') then
13418            person_rec.attribute2 := l_entry_value;
13419        elsif tca_column_name = upper('attribute3') then
13420            person_rec.attribute3 := l_entry_value;
13421        elsif tca_column_name = upper('attribute4') then
13422            person_rec.attribute4 := l_entry_value;
13423        elsif tca_column_name = upper('attribute5') then
13424            person_rec.attribute5 := l_entry_value;
13425        elsif tca_column_name = upper('attribute6') then
13426            person_rec.attribute6 := l_entry_value;
13427        elsif tca_column_name = upper('attribute7') then
13428            person_rec.attribute7 := l_entry_value;
13429        elsif tca_column_name = upper('attribute8') then
13430            person_rec.attribute8 := l_entry_value;
13431        elsif tca_column_name = upper('attribute9') then
13432            person_rec.attribute9 := l_entry_value;
13433        elsif tca_column_name = upper('attribute10') then
13434            person_rec.attribute10 := l_entry_value;
13435        elsif tca_column_name = upper('attribute11') then
13436            person_rec.attribute11 := l_entry_value;
13437        elsif tca_column_name = upper('attribute12') then
13438            person_rec.attribute12 := l_entry_value;
13439        elsif tca_column_name = upper('attribute13') then
13440            person_rec.attribute13 := l_entry_value;
13441        elsif tca_column_name = upper('attribute14') then
13442            person_rec.attribute14 := l_entry_value;
13443        elsif tca_column_name = upper('attribute15') then
13444            person_rec.attribute15 := l_entry_value;
13445        elsif tca_column_name = upper('attribute1') then
13446            ocon_rec.attribute1 := l_entry_value;
13447        elsif tca_column_name = upper('attribute2') then
13448            ocon_rec.attribute2 := l_entry_value;
13449        elsif tca_column_name = upper('attribute3') then
13450            ocon_rec.attribute3 := l_entry_value;
13451        elsif tca_column_name = upper('attribute4') then
13452            ocon_rec.attribute4 := l_entry_value;
13453        elsif tca_column_name = upper('attribute5') then
13454            ocon_rec.attribute5 := l_entry_value;
13455        elsif tca_column_name = upper('attribute6') then
13456            ocon_rec.attribute6 := l_entry_value;
13457        elsif tca_column_name = upper('attribute7') then
13458            ocon_rec.attribute7 := l_entry_value;
13459        elsif tca_column_name = upper('attribute8') then
13460            ocon_rec.attribute8 := l_entry_value;
13461        elsif tca_column_name = upper('attribute9') then
13462            ocon_rec.attribute9 := l_entry_value;
13463        elsif tca_column_name = upper('attribute10') then
13464            ocon_rec.attribute10 := l_entry_value;
13465        elsif tca_column_name = upper('attribute11') then
13466            ocon_rec.attribute11 := l_entry_value;
13467        elsif tca_column_name = upper('attribute12') then
13468            ocon_rec.attribute12 := l_entry_value;
13469        elsif tca_column_name = upper('attribute13') then
13470            ocon_rec.attribute13 := l_entry_value;
13471        elsif tca_column_name = upper('attribute14') then
13472            ocon_rec.attribute14 := l_entry_value;
13473        elsif tca_column_name = upper('attribute15') then
13474            ocon_rec.attribute15 := l_entry_value;
13475        elsif tca_column_name = upper('attribute1') then
13476            location_rec.attribute1 := l_entry_value;
13477        elsif tca_column_name = upper('attribute2') then
13478            location_rec.attribute2 := l_entry_value;
13479        elsif tca_column_name = upper('attribute3') then
13480            location_rec.attribute3 := l_entry_value;
13481        elsif tca_column_name = upper('attribute4') then
13482            location_rec.attribute4 := l_entry_value;
13483        elsif tca_column_name = upper('attribute5') then
13484            location_rec.attribute5 := l_entry_value;
13485        elsif tca_column_name = upper('attribute6') then
13486            location_rec.attribute6 := l_entry_value;
13487        elsif tca_column_name = upper('attribute7') then
13488            location_rec.attribute7 := l_entry_value;
13489        elsif tca_column_name = upper('attribute8') then
13490            location_rec.attribute8 := l_entry_value;
13491        elsif tca_column_name = upper('attribute9') then
13492            location_rec.attribute9 := l_entry_value;
13493        elsif tca_column_name = upper('attribute10') then
13494            location_rec.attribute10 := l_entry_value;
13495        elsif tca_column_name = upper('attribute11') then
13496            location_rec.attribute11 := l_entry_value;
13497        elsif tca_column_name = upper('attribute12') then
13498            location_rec.attribute12 := l_entry_value;
13499        elsif tca_column_name = upper('attribute13') then
13500            location_rec.attribute13 := l_entry_value;
13501        elsif tca_column_name = upper('attribute14') then
13502            location_rec.attribute14 := l_entry_value;
13503        elsif tca_column_name = upper('attribute15') then
13504            location_rec.attribute15 := l_entry_value;
13505        elsif tca_column_name = upper('phone_country_code') then
13506            fax_rec.phone_country_code := l_entry_value;
13507        elsif tca_column_name = upper('phone_area_code') then
13508            fax_rec.phone_area_code := l_entry_value;
13509        elsif tca_column_name = upper('phone_number') then
13510            fax_rec.phone_number := l_entry_value;
13511        elsif tca_column_name = upper('attribute_category') then
13512            org_rec.party_rec.attribute_category := l_entry_value;
13513        elsif tca_column_name = upper('attribute_category') then
13514            person_rec.attribute_category := l_entry_value;
13515        elsif tca_column_name = upper('attribute_category') then
13516            ocon_rec.attribute_category := l_entry_value;
13517        elsif tca_column_name = upper('attribute_category') then
13518            location_rec.attribute_category := l_entry_value;
13519        elsif tca_column_name = upper('site_use_type') then
13520            psiteuse_rec.site_use_type := l_entry_value;
13521        end if;
13522 
13523        --TCA mandate: bug 4587049
13524          --party_rec.created_by_module := 'AMS';
13525          org_rec.created_by_module := 'AMS';
13526          person_rec.created_by_module := 'AMS';
13527          ocon_rec.created_by_module := 'AMS';
13528          location_rec.created_by_module := 'AMS';
13529          psite_rec.created_by_module := 'AMS';
13530          psiteuse_rec.created_by_module := 'AMS';
13531 
13532    end if;   -- l_b2b_flag = 'Y'
13533 
13534    if l_b2b_flag = 'B2C' then
13535 	l_b2b := 'N';
13536         if tca_column_name = upper('person_first_name') then
13537            person_rec.person_first_name := l_entry_value;
13538        elsif tca_column_name = upper('person_middle_name') then
13539            person_rec.person_middle_name := l_entry_value;
13540        elsif tca_column_name = upper('person_last_name') then
13541            person_rec.person_last_name := l_entry_value;
13542        elsif tca_column_name = upper('person_name_suffix') then
13543            person_rec.person_name_suffix := l_entry_value;
13544        elsif tca_column_name = upper('person_pre_name_adjunct') then
13545            person_rec.person_pre_name_adjunct := l_entry_value;
13546        elsif tca_column_name = upper('country') then
13547            location_rec.country := l_entry_value;
13548        elsif tca_column_name = upper('address1') then
13549            location_rec.address1 := l_entry_value;
13550        elsif tca_column_name = upper('address2') then
13551            location_rec.address2 := l_entry_value;
13552        elsif tca_column_name = upper('city') then
13553            location_rec.city := l_entry_value;
13554        elsif tca_column_name = upper('county') then
13555            location_rec.county := l_entry_value;
13556        elsif tca_column_name = upper('state') then
13557            location_rec.state := l_entry_value;
13558        elsif tca_column_name = upper('province') then
13559            location_rec.province := l_entry_value;
13560        elsif tca_column_name = upper('postal_code') then
13561            location_rec.postal_code := l_entry_value;
13562        elsif tca_column_name = upper('email_address') then
13563            email_rec.email_address := l_entry_value;
13564        elsif tca_column_name = upper('phone_country_code') then
13565            phone_rec.phone_country_code := l_entry_value;
13566        elsif tca_column_name = upper('phone_area_code') then
13567            phone_rec.phone_area_code := l_entry_value;
13568        elsif tca_column_name = upper('phone_number') then
13569            phone_rec.phone_number := l_entry_value;
13570        elsif tca_column_name = upper('phone_extension') then
13571            phone_rec.phone_extension := l_entry_value;
13572        elsif tca_column_name = upper('SALUTATION') then
13573            person_rec.party_rec.SALUTATION := l_entry_value;
13574        elsif tca_column_name = upper('ADDRESS3') then
13575            location_rec.ADDRESS3 := l_entry_value;
13576        elsif tca_column_name = upper('ADDRESS4') then
13577            location_rec.ADDRESS4 := l_entry_value;
13578        elsif tca_column_name = upper('ADDRESS_LINES_PHONETIC') then
13579            location_rec.ADDRESS_LINES_PHONETIC := l_entry_value;
13580        elsif tca_column_name = upper('PO_BOX_NUMBER') then
13581            -- location_rec.PO_BOX_NUMBER := l_entry_value; Refer bug 4704727
13582 	   location_rec.PO_BOX_NUMBER := null;
13583        elsif tca_column_name = upper('HOUSE_NUMBER') then
13584            -- location_rec.HOUSE_NUMBER := l_entry_value; Refer bug 4704727
13585 	   location_rec.HOUSE_NUMBER := null;
13586        elsif tca_column_name = upper('STREET_SUFFIX') then
13587            -- location_rec.STREET_SUFFIX := l_entry_value; Refer bug 4704727
13588 	   location_rec.STREET_SUFFIX := null;
13589        elsif tca_column_name = upper('STREET') then
13590            -- location_rec.STREET := l_entry_value; Refer bug 4704727
13591 	   location_rec.STREET := null;
13592        elsif tca_column_name = upper('STREET_NUMBER') then
13593 	   -- location_rec.STREET_NUMBER := l_entry_value; Refer bug 4704727
13594 	   location_rec.STREET_NUMBER := null;
13595        elsif tca_column_name = upper('FLOOR') then
13596            -- location_rec.FLOOR := l_entry_value; Refer bug 4704727
13597 	   location_rec.FLOOR := null;
13598        elsif tca_column_name = upper('SUITE') then
13599            -- location_rec.SUITE := l_entry_value; Refer bug 4704727
13600 	   location_rec.SUITE := null;
13601        elsif tca_column_name = upper('POSTAL_PLUS4_CODE') then
13602            location_rec.POSTAL_PLUS4_CODE := l_entry_value;
13603        elsif tca_column_name = upper('identifying_address_flag') then
13604            psite_rec.identifying_address_flag := l_entry_value;
13605        elsif tca_column_name = upper('person_last_name_phonetic') then
13606            person_rec.person_last_name_phonetic := l_entry_value;
13607        elsif tca_column_name = upper('person_first_name_phonetic') then
13608            person_rec.person_first_name_phonetic := l_entry_value;
13609        elsif tca_column_name = upper('url') then
13610            web_rec.url := l_entry_value;
13611        elsif tca_column_name = upper('person_academic_title') then
13612            person_rec.person_academic_title := l_entry_value;
13613        elsif tca_column_name = upper('date_of_birth') then
13614            person_rec.date_of_birth := l_entry_value;
13615        elsif tca_column_name = upper('person_academic_title') then
13616            person_rec.person_academic_title := l_entry_value;
13617        elsif tca_column_name = upper('person_previous_last_name') then
13618            person_rec.person_previous_last_name := l_entry_value;
13619        elsif tca_column_name = upper('known_as') then
13620            person_rec.known_as := l_entry_value;
13621        elsif tca_column_name = upper('known_as2') then
13622            person_rec.known_as2 := l_entry_value;
13623        elsif tca_column_name = upper('known_as3') then
13624            person_rec.known_as3 := l_entry_value;
13625        elsif tca_column_name = upper('known_as4') then
13626            person_rec.known_as4 := l_entry_value;
13627        elsif tca_column_name = upper('known_as5') then
13628            person_rec.known_as5 := l_entry_value;
13629        elsif tca_column_name = upper('person_name_phonetic') then
13630            person_rec.person_name_phonetic := l_entry_value;
13631        elsif tca_column_name = upper('middle_name_phonetic') then
13632            person_rec.middle_name_phonetic := l_entry_value;
13633        elsif tca_column_name = upper('jgzz_fiscal_code') then
13634            person_rec.jgzz_fiscal_code := l_entry_value;
13635        elsif tca_column_name = upper('place_of_birth') then
13636            person_rec.place_of_birth := l_entry_value;
13637        elsif tca_column_name = upper('date_of_death') then
13638            person_rec.date_of_death := l_entry_value;
13639        elsif tca_column_name = upper('declared_ethnicity') then
13640            person_rec.declared_ethnicity := l_entry_value;
13641        elsif tca_column_name = upper('marital_status') then
13642            person_rec.marital_status := l_entry_value;
13643        elsif tca_column_name = upper('personal_income') then
13644            person_rec.personal_income := l_entry_value;
13645        elsif tca_column_name = upper('marital_status_effective_date') then
13646            person_rec.marital_status_effective_date := l_entry_value;
13647        elsif tca_column_name = upper('head_of_household_flag') then
13648            person_rec.head_of_household_flag := l_entry_value;
13649        elsif tca_column_name = upper('household_size') then
13650            person_rec.household_size := l_entry_value;
13651        elsif tca_column_name = upper('location_directions') then
13652            location_rec.location_directions := l_entry_value;
13653        elsif tca_column_name = upper('address_effective_date') then
13654            location_rec.address_effective_date := l_entry_value;
13655        elsif tca_column_name = upper('address_expiration_date') then
13656 	   location_rec.address_expiration_date := l_entry_value;
13657        elsif tca_column_name = upper('attribute1') then
13658            person_rec.party_rec.attribute1 := l_entry_value;
13659        elsif tca_column_name = upper('attribute2') then
13660            person_rec.party_rec.attribute2 := l_entry_value;
13661        elsif tca_column_name = upper('attribute3') then
13662            person_rec.party_rec.attribute3 := l_entry_value;
13663        elsif tca_column_name = upper('attribute4') then
13664            person_rec.party_rec.attribute4 := l_entry_value;
13665        elsif tca_column_name = upper('attribute5') then
13666            person_rec.party_rec.attribute5 := l_entry_value;
13667        elsif tca_column_name = upper('attribute6') then
13668            person_rec.party_rec.attribute6 := l_entry_value;
13669        elsif tca_column_name = upper('attribute7') then
13670            person_rec.party_rec.attribute7 := l_entry_value;
13671        elsif tca_column_name = upper('attribute8') then
13672            person_rec.party_rec.attribute8 := l_entry_value;
13673        elsif tca_column_name = upper('') then
13674            person_rec.party_rec.attribute9 := l_entry_value;
13675        elsif tca_column_name = upper('attribute9') then
13676            person_rec.party_rec.attribute10 := l_entry_value;
13677        elsif tca_column_name = upper('attribute11') then
13678            person_rec.party_rec.attribute11 := l_entry_value;
13679        elsif tca_column_name = upper('attribute12') then
13680            person_rec.party_rec.attribute12 := l_entry_value;
13681        elsif tca_column_name = upper('attribute13') then
13682            person_rec.party_rec.attribute13 := l_entry_value;
13683        elsif tca_column_name = upper('attribute14') then
13684            person_rec.party_rec.attribute14 := l_entry_value;
13685        elsif tca_column_name = upper('attribute15') then
13686            person_rec.party_rec.attribute15 := l_entry_value;
13687        elsif tca_column_name = upper('attribute1') then
13688            location_rec.attribute1 := l_entry_value;
13689        elsif tca_column_name = upper('attribute2') then
13690            location_rec.attribute2 := l_entry_value;
13691        elsif tca_column_name = upper('attribute3') then
13692            location_rec.attribute3 := l_entry_value;
13693        elsif tca_column_name = upper('attribute4') then
13694            location_rec.attribute4 := l_entry_value;
13695        elsif tca_column_name = upper('attribute5') then
13696            location_rec.attribute5 := l_entry_value;
13697        elsif tca_column_name = upper('attribute6') then
13698            location_rec.attribute6 := l_entry_value;
13699        elsif tca_column_name = upper('attribute7') then
13700            location_rec.attribute7 := l_entry_value;
13701        elsif tca_column_name = upper('attribute8') then
13702            location_rec.attribute8 := l_entry_value;
13703        elsif tca_column_name = upper('attribute9') then
13704            location_rec.attribute9 := l_entry_value;
13705        elsif tca_column_name = upper('attribute10') then
13706            location_rec.attribute10 := l_entry_value;
13707        elsif tca_column_name = upper('attribute11') then
13708            location_rec.attribute11 := l_entry_value;
13709        elsif tca_column_name = upper('attribute12') then
13710            location_rec.attribute12 := l_entry_value;
13711        elsif tca_column_name = upper('attribute13') then
13712            location_rec.attribute13 := l_entry_value;
13713        elsif tca_column_name = upper('attribute14') then
13714            location_rec.attribute14 := l_entry_value;
13715        elsif tca_column_name = upper('attribute15') then
13716            location_rec.attribute15 := l_entry_value;
13717        elsif tca_column_name = upper('phone_country_code') then
13718            fax_rec.phone_country_code := l_entry_value;
13719        elsif tca_column_name = upper('phone_area_code') then
13720            fax_rec.phone_area_code := l_entry_value;
13721        elsif tca_column_name = upper('phone_number') then
13722 	   fax_rec.phone_number := l_entry_value;
13723        elsif tca_column_name = upper('attribute_category') then
13724            person_rec.party_rec.attribute_category := l_entry_value;
13725        elsif tca_column_name = upper('attribute_category') then
13726            location_rec.attribute_category  := l_entry_value;
13727        elsif tca_column_name = upper('short_description') then
13728            location_rec.short_description := l_entry_value;
13729        elsif tca_column_name = upper('description') then
13730            location_rec.description := l_entry_value;
13731        elsif tca_column_name = upper('site_use_type') then
13732            psiteuse_rec.site_use_type := l_entry_value;
13733        elsif tca_column_name = upper('orig_system_reference') then
13734            person_rec.party_rec.orig_system_reference := l_entry_value;
13735        elsif tca_column_name = upper('tax_reference') then
13736            person_rec.tax_reference := l_entry_value;
13737        elsif tca_column_name = upper('rent_own_ind') then
13738            person_rec.rent_own_ind := l_entry_value;
13739        elsif tca_column_name = upper('gender') then
13740            person_rec.gender := l_entry_value;
13741        elsif tca_column_name = upper('HOUSEHOLD_INCOME') then
13742            person_rec.HOUSEHOLD_INCOME  := l_entry_value;
13743        end if;
13744 
13745        --TCA mandate: bug 4587049
13746          --party_rec.created_by_module := 'AMS';
13747          person_rec.created_by_module := 'AMS';
13748          location_rec.created_by_module := 'AMS';
13749          psite_rec.created_by_module := 'AMS';
13750          psiteuse_rec.created_by_module := 'AMS';
13751 
13752       end if; -- l_b2b_flag = 'N'
13753      end loop; -- for c_tca_columns
13754      close c_tca_columns;
13755      l_entry_value := NULL;
13756 
13757  -- DBMS_OUTPUT.PUT_LINE('before tca call');
13758   --write_to_act_log('Calling ams_list_import_pub to create customer', 'LIST', p_list_header_id,'LOW');
13759 
13760      -- ---------------------------TCA CALL ---------------------------------
13761 
13762 
13763   AMS_List_Import_PUB.Create_Customer (
13764   p_api_version              => 1,
13765   p_init_msg_list            => 'T',
13766   p_commit                   => 'F',
13767   x_return_status            => x_return_status,
13768   x_msg_count                => x_msg_count,
13769   x_msg_data                 => x_msg_data,
13770   p_party_id                 => l_party_id,
13771   p_b2b_flag                 => l_b2b,
13772   p_import_list_header_id    =>null,
13773   p_party_rec                => party_rec,
13774   p_org_rec                  => org_rec,
13775   p_person_rec               => person_rec,
13776   p_location_rec             => location_rec,
13777   p_psite_rec                => psite_rec,
13778   p_cpoint_rec               => cpoint_rec,
13779   p_email_rec                => email_rec,
13780   p_phone_rec                => phone_rec,
13781   p_fax_rec                  => fax_rec,
13782   p_ocon_rec                 => ocon_rec,
13783   p_siteuse_rec              => psiteuse_rec,
13784   p_web_rec                  => web_rec,
13785   x_new_party                => l_new_party,
13786   p_component_name           => l_component_name,
13787   l_import_source_line_id    => null
13788   );
13789    -- write_to_act_log('Total no of messages '||x_msg_count, 'LIST', p_list_header_id,'LOW');
13790    -- write_to_act_log('List entry id is '||l_list_entry_id, 'LIST', p_list_header_id,'LOW');
13791    -- write_to_act_log('Party id is '||l_party_id, 'LIST', p_list_header_id,'LOW');
13792   if x_msg_count > 0 then
13793     FOR i IN 1..x_msg_count  LOOP
13794          -- Following code was modified by bmuthukr
13795          --x_tmp_var := fnd_msg_pub.get(p_encoded => fnd_api.g_false);
13796          --x_tmp_var1 := substrb(x_tmp_var1 || ' '|| x_tmp_var,1,4000);
13797 	 x_tmp_var := 'Please make sure that the required fields (eg. person first name, last name) are available in the remote database';
13798          write_to_act_log('Updating the tca_load_status as ERROR', 'LIST', p_list_header_id,'HIGH');
13799 
13800 	 -- DBMS_OUTPUT.PUT_LINE('TCA Upload Process : x_tmp_var  ->' || x_tmp_var );
13801 	 -- DBMS_OUTPUT.PUT_LINE('TCA Upload Process : x_tmp_var  ->' || x_tmp_var1 );
13802     END LOOP;
13803     update ams_list_entries set error_flag = 'E',
13804 				tca_load_status = 'ERROR',
13805 				ENABLED_FLAG = 'N',
13806 				--error_text = 'TCA API ERROR :'||substr(x_tmp_var1,1,3000)
13807 				error_text = 'TCA API ERROR :'||x_tmp_var
13808     where list_entry_id = l_list_entry_id;
13809   end if;
13810     if l_party_id is  null then
13811     update ams_list_entries set error_flag = 'E',
13812 				tca_load_status = 'ERROR',
13813 				ENABLED_FLAG = 'N',
13814 				--error_text = 'TCA API ERROR :'||nvl(x_tmp_var,substr(x_tmp_var1,1,3000))
13815 				error_text = 'TCA API ERROR :'||x_tmp_var
13816     where list_entry_id = l_list_entry_id;
13817    end if;
13818 
13819   if l_party_id is not null then
13820     update ams_list_entries set party_id = l_party_id,
13821 				error_flag = 'S',
13822 				ENABLED_FLAG = 'Y',
13823                                 tca_load_status = 'SUCCESS'
13824     where list_entry_id = l_list_entry_id;
13825    end if;
13826    l_party_id := NULL;
13827  -- DBMS_OUTPUT.PUT_LINE('after tca call');
13828 
13829      -- ---------------------------TCA CALL ---------------------------------
13830 
13831   END LOOP; -- For c_list_entries
13832   close c_list_entries;
13833   update ams_list_headers_all
13834      set migration_date = sysdate
13835    where list_header_id = p_list_header_id;
13836 
13837   write_to_act_log('Executed procedure tca_upload_process', 'LIST', g_list_header_id,'LOW');
13838 
13839   -- Added for cancel list gen as it prevents parallel update- Raghu
13840   -- of list headers when cancel button is pressed
13841 
13842   --TCA mandate: bug 4587049
13843   --Enable/restore TCA events after bulk TCA data processing
13844   if l_prof <> 'N' then
13845      fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13846   end if;
13847 
13848   commit;
13849 
13850 EXCEPTION
13851    WHEN FND_API.G_EXC_ERROR THEN
13852      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13853      x_return_status := FND_API.G_RET_STS_ERROR ;
13854 
13855      if l_prof <> 'N' then
13856         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13857      end if;
13858 
13859       FND_MSG_PUB.count_and_get(
13860             p_encoded => FND_API.g_false,
13861             p_count   => x_msg_count,
13862             p_data    => x_msg_data
13863       );
13864 
13865 
13866 
13867   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
13868      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13869      x_return_status := FND_API.G_RET_STS_ERROR ;
13870 
13871      if l_prof <> 'N' then
13872         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13873      end if;
13874 
13875       FND_MSG_PUB.count_and_get(
13876             p_encoded => FND_API.g_false,
13877             p_count   => x_msg_count,
13878             p_data    => x_msg_data
13879       );
13880 
13881   WHEN OTHERS THEN
13882      write_to_act_log('Error while executing procedure tca_upload_process '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13883      x_return_status := FND_API.G_RET_STS_ERROR ;
13884 
13885      if l_prof <> 'N' then
13886         fnd_profile.put('HZ_EXECUTE_API_CALLOUTS',l_prof);
13887      end if;
13888 
13889       FND_MSG_PUB.count_and_get(
13890             p_encoded => FND_API.g_false,
13891             p_count   => x_msg_count,
13892             p_data    => x_msg_data
13893       );
13894 
13895 END tca_upload_process;
13896 
13897 PROCEDURE remote_list_gen
13898              (p_list_header_id     in  number,
13899               x_return_status      OUT NOCOPY VARCHAR2,
13900               x_msg_count          OUT NOCOPY NUMBER,
13901               x_msg_data           OUT NOCOPY VARCHAR2,
13902 	      x_remote_gen         OUT NOCOPY VARCHAR2)
13903 IS
13904   l_remote_list_gen	varchar2(1) := 'N';
13905   l_list_selection      varchar2(1);
13906   l_onlylist_selection  varchar2(1);
13907 
13908   cursor c_list_selection is
13909   select 'Y' from ams_list_select_actions
13910    where  action_used_by_id = p_list_header_id
13911      and  arc_action_used_by = 'LIST'
13912      and  arc_incl_object_from in ('CELL','DIWB','SQL');
13913 
13914   cursor c_only_list_selection is
13915   select 'Y' from ams_list_select_actions act, ams_list_headers_all head
13916    where  act.action_used_by_id = p_list_header_id
13917      and  act.arc_incl_object_from = 'LIST' and act.arc_action_used_by = 'LIST'
13918      and  act.INCL_OBJECT_ID = head.list_header_id
13919      and  head.status_code = 'AVAILABLE'
13920      and  (head.MIGRATION_DATE is null or head.main_gen_end_time > head.migration_date);
13921 
13922 begin
13923 
13924      open c_list_selection;
13925      fetch c_list_selection into l_list_selection;
13926      close c_list_selection;
13927 
13928      if nvl(l_list_selection,'N') = 'Y' then
13929         write_to_act_log(p_msg_data => 'List selection includes segments/workbook/SQL.' ,
13930                          p_arc_log_used_by => 'LIST',
13931                          p_log_used_by_id  => p_list_header_id,
13932                          p_level => 'LOW');
13933         x_remote_gen := 'Y';
13934         x_return_status := FND_API.G_RET_STS_SUCCESS;
13935      else
13936         write_to_act_log(p_msg_data => 'List selection does not include segments/workbook/SQL.' ,
13937                          p_arc_log_used_by => 'LIST',
13938                          p_log_used_by_id  => p_list_header_id,
13939                          p_level => 'LOW');
13940      end if;
13941 
13942      if l_list_selection is null then
13943         open c_only_list_selection;
13944         fetch c_only_list_selection into l_onlylist_selection;
13945         close c_only_list_selection;
13946         if nvl(l_onlylist_selection,'N') = 'Y' then
13947            write_to_act_log(p_msg_data => 'List/TG will be generated in remote instance.' ,
13948                             p_arc_log_used_by => 'LIST',
13949                             p_log_used_by_id  => p_list_header_id,
13950 			    p_level => 'LOW');
13951            x_remote_gen := 'Y';
13952            x_return_status := FND_API.G_RET_STS_SUCCESS;
13953         else
13954            write_to_act_log(p_msg_data => 'List/TG will be generated in local instance.' ,
13955                             p_arc_log_used_by => 'LIST',
13956                             p_log_used_by_id  => p_list_header_id,
13957                             p_level => 'LOW');
13958            x_remote_gen := 'N';
13959            x_return_status := FND_API.G_RET_STS_SUCCESS;
13960         end if;
13961      end if;
13962 
13963   EXCEPTION
13964   WHEN OTHERS THEN
13965     write_to_act_log('Error while executing procedure remote_list_gen '||sqlcode||'  '||sqlerrm, 'LIST', g_list_header_id,'HIGH');
13966      x_return_status := FND_API.G_RET_STS_ERROR ;
13967      x_msg_data := 'Error while executing procedure remote_list_gen '||sqlcode||'  '||sqlerrm;
13968      x_msg_count := 1;
13969       FND_MSG_PUB.count_and_get(
13970             p_encoded => FND_API.g_false,
13971             p_count   => x_msg_count,
13972             p_data    => x_msg_data
13973       );
13974 
13975 end remote_list_gen;
13976 
13977 --Procedure is_manual added by bmuthukr for bug 3710720
13978 PROCEDURE is_manual
13979              (p_list_header_id     in  number,
13980               x_return_status      OUT NOCOPY VARCHAR2,
13981               x_msg_count          OUT NOCOPY NUMBER,
13982               x_msg_data           OUT NOCOPY VARCHAR2,
13983               x_is_manual          OUT NOCOPY varchar2 ) is
13984 
13985   cursor c1 is
13986   select list_type
13987     from ams_list_headers_all
13988    where list_header_id = p_list_header_id;
13989 
13990   cursor c2 is
13991   select 1
13992     from ams_list_entries
13993    where list_header_id = p_list_header_id
13994      and enabled_flag = 'Y'
13995      and rownum = 1;
13996 
13997   cursor c_get_list_used_by_id is
13998   select list_used_by_id
13999     from ams_act_lists
14000    where list_header_id = p_list_header_id
14001      and list_used_by = 'CSCH'
14002      and list_act_type = 'TARGET';
14003 
14004   cursor c3 is
14005   select 1
14006     from ams_list_select_actions
14007    where list_header_id = p_list_header_id
14008      and arc_incl_object_from  = 'EMPLOYEE';
14009 
14010   cursor c4(l_list_used_by_id number) is
14011   select 1
14012     from ams_act_lists
14013    where list_used_by_id = l_list_used_by_id
14014      and list_act_type = 'EMPLOYEE';
14015 
14016 l_list_type        varchar2(100) := null;
14017 l_dummy            number := 0;
14018 l_list_used_by_id  number := 0;
14019 
14020 
14021 begin
14022    open c1;
14023    fetch c1 into l_list_type;
14024    close c1;
14025 
14026    write_to_act_log('List is of type '||l_list_type,'LIST',p_list_header_id,'HIGH');
14027 
14028    if l_list_type = 'MANUAL' then --List type is manual. Make the status as either available/draft.
14029       write_to_act_log('List is of type MANUAL. Cannot regenerate. ','LIST',p_list_header_id,'HIGH');
14030       x_is_manual := 'Y';
14031    else
14032       x_is_manual := 'N';
14033    end if;
14034 
14035    if l_list_type = 'TARGET' then -- To see if any of the incl are based on emp list in TG
14036       open c_get_list_used_by_id;
14037       fetch c_get_list_used_by_id into l_list_used_by_id;
14038       close c_get_list_used_by_id;
14039 
14040       open c4(l_list_used_by_id);
14041       fetch c4 into l_dummy;
14042       if c4%found then
14043          write_to_act_log('Target group inclusions has EMPLOYEE list. Cannot generate','LIST',p_list_header_id,'HIGH');
14044          x_is_manual := 'Y';
14045       end if;
14046       close c4;
14047    end if;
14048 
14049    if l_list_type = 'STANDARD' then -- To see if any of the incl are based on emp list in std list
14050       open c3;
14051       fetch c3 into l_dummy;
14052       if c3%found then
14053          write_to_act_log('List inclusions has EMPLOYEE list. Cannot generate','LIST',p_list_header_id,'HIGH');
14054          x_is_manual := 'Y';
14055       end if;
14056       close c3;
14057    end if;
14058 
14059 
14060    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.
14061       open c2;
14062       fetch c2 into l_dummy;
14063       if c2%found then -- List already has enabled entries.. So making it as available.
14064          update ams_list_headers_all
14065             set status_code          = 'AVAILABLE',
14066                 user_status_id       = 303,
14067                 status_date          = sysdate,
14068                 last_update_date     = sysdate
14069           where list_header_id       = p_list_header_id;
14070       else  -- No enabled entries..So it will be in DRAFT status
14071          update ams_list_headers_all
14072             set status_code          = 'DRAFT',
14073                 user_status_id       = 300,
14074                 status_date          = sysdate,
14075                 last_update_date     = sysdate
14076           where list_header_id       = p_list_header_id;
14077       end if;
14078       close c2;
14079 
14080       -- Added for cancel list gen as it prevents parallel update- Raghu
14081       -- of list headers when cancel button is pressed
14082       commit;
14083    end if;
14084 
14085 exception
14086    when others then
14087       write_to_act_log('Error while executing procedure is_manual '||sqlcode||'   '||sqlerrm,'LIST',p_list_header_id,'HIGH');
14088       x_return_status := FND_API.G_RET_STS_ERROR ;
14089       x_msg_data := 'Error while executing procedure is_manual '||sqlcode||'  '||sqlerrm;
14090       x_msg_count := 1;
14091 end is_manual;
14092 
14093 procedure upd_list_header_info(p_list_header_id in number,
14094                                x_msg_count      out nocopy number,
14095                                x_msg_data       out nocopy varchar2,
14096                                x_return_status  out nocopy varchar2) is
14097 
14098 l_list_type                     varchar2(100);
14099 l_remote_flag                   varchar2(1);
14100 l_database_link                 varchar2(200);
14101 
14102 l_no_of_rows_duplicates         number;
14103 l_no_of_rows_in_list            number;
14104 l_no_of_rows_active             number;
14105 l_no_of_rows_inactive           number;
14106 l_no_of_rows_manually_entered   number;
14107 l_no_of_rows_in_ctrl_group      number;
14108 l_no_of_rows_random             number;
14109 l_no_of_rows_used               number;
14110 l_no_of_rows_suppressed         number;
14111 l_no_of_rows_fatigued           number;
14112 
14113 cursor c_list_det is
14114 select stypes.database_link,
14115        list.remote_gen_flag,
14116        list.list_type
14117   from ams_list_src_types stypes, ams_list_headers_all list
14118  where list.list_source_type = stypes.source_type_code
14119    and list_header_id  =  p_list_header_id;
14120 
14121 cursor c_count_list_entries is
14122 select sum(decode(enabled_flag,'N',0,1)),
14123        sum(decode(enabled_flag,'Y',0,1)),
14124        sum(1),
14125        sum(decode(manually_entered_flag,'Y',decode(enabled_flag,'Y','1',0),0))
14126   from ams_list_entries
14127  where list_header_id = p_list_header_id;
14128 
14129 begin
14130    open c_list_det;
14131    fetch c_list_det into l_database_link,l_remote_flag,l_list_type;
14132    close c_list_det;
14133 
14134    if l_remote_flag = 'N' or l_list_type <> 'TARGET' then
14135       open c_count_list_entries;
14136       fetch c_count_list_entries
14137        into l_no_of_rows_active            ,
14138             l_no_of_rows_inactive          ,
14139             l_no_of_rows_in_list           ,
14140             l_no_of_rows_manually_entered  ;
14141       close c_count_list_entries;
14142    else
14143       execute immediate
14144       'BEGIN
14145          AMS_Remote_ListGen_PKG.remote_list_status_detils'||'@'||l_database_link||'(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11)'||';'||
14146       ' END;'
14147       using  p_list_header_id,
14148              OUT l_no_of_rows_active,
14149              OUT l_no_of_rows_inactive,
14150              OUT l_no_of_rows_in_list,
14151              OUT l_no_of_rows_in_ctrl_group,
14152 	     OUT l_no_of_rows_random,
14153 	     OUT l_no_of_rows_duplicates,
14154 	     OUT l_no_of_rows_manually_entered,
14155              OUT x_msg_count,
14156              OUT x_msg_data,
14157              OUT x_return_status;
14158    end if;
14159 
14160    update ams_list_headers_all
14161       set no_of_rows_in_list          = nvl(l_no_of_rows_in_list,0),
14162           no_of_rows_active           = nvl(l_no_of_rows_active,0),
14163           no_of_rows_inactive         = nvl(l_no_of_rows_inactive,0),
14164           no_of_rows_manually_entered = nvl(l_no_of_rows_manually_entered,0)
14165     where list_header_id = p_list_header_id;
14166     commit;
14167 
14168 exception
14169    when others then
14170       x_return_status := FND_API.G_RET_STS_ERROR;
14171       x_msg_data      := 'Error while executing upd_list_header_info. '||sqlcode||'  '||sqlerrm;
14172       x_msg_count     := 1;
14173 end upd_list_header_info;
14174 
14175 END AMS_ListGeneration_PKG;