CREATE PROCEDURE COUNT_SHOW_SOME_PK_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Description VARCHAR(150) )
  DYNAMIC RESULT SETS 2
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;
		 DECLARE my_cv_count_FOUND INTEGER DEFAULT NULL;
		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 DECLARE my_cv_ROWCOUNT INTEGER ;
		 BEGIN

        DECLARE view_rec_itm_primary_key VARCHAR(300) ;
		 		 DECLARE sql_stmt VARCHAR(200) ;
		 		 DECLARE sql_count_stmt VARCHAR(200) ;
		 		 DECLARE rows_fetched INTEGER ;
		 		 DECLARE KEYS_TO_SHOW BIGINT DEFAULT 5;
		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;
		 		 DECLARE my_cv_ST1_STMT STATEMENT ;
		 		 DECLARE my_cv_count_STMT STATEMENT ;
		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST1_STMT;
		 		 DECLARE my_cv_count CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_count_STMT;
		 
		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		         BEGIN   		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('Error in proc COUNT_SHOW_SOME_PK_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Description)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 		 END;

		         BEGIN
		 		 		 DECLARE EXIT HANDLER FOR SQLSTATE '42710'
		 		 		 BEGIN
		 		 		 END;
		     		 END;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('*** REPORT FOR VIEW ') || RTRIM(CHAR(View_Name)) || CHAR(', Description = ') || RTRIM(CHAR(Description)));
		 		 COMMIT;
		 		 

		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));
		 		 SET sql_count_stmt = CHAR('SELECT COUNT(*) FROM ') || CHAR(LTRIM(RTRIM(View_Name)));

		 		 PREPARE my_cv_count_STMT FROM sql_count_stmt ;
		 		 OPEN my_cv_count ;


		 		 

                BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_count_FOUND = SQLCODE ;
		 		 		 FETCH FROM my_cv_count INTO rows_fetched ;
		 		 END;		 
		 		 
                INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('Total number of primary keys in VIEW = ') || RTRIM(CHAR(rows_fetched)));
                COMMIT;
                


 		 		 CLOSE my_cv_count ;

 		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 SET my_cv_ROWCOUNT = 1;
		 		 
		 		 PREPARE my_cv_ST1_STMT FROM sql_stmt;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL1:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_primary_key ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL1;
		 		 		 END IF;

		 		 		 IF my_cv_ROWCOUNT >  KEYS_TO_SHOW THEN
		 		 		 		 LEAVE LOOP_LABEL1;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('Next primary key in this view = ') || RTRIM(CHAR(view_rec_itm_primary_key)));
		 		 		 COMMIT;
		 		 SET my_cv_ROWCOUNT = my_cv_ROWCOUNT + 1;
		 		 		 

		 		 END LOOP LOOP_LABEL1 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
 		 		 
 		 		 

		 END ;
END p1 @
 


CREATE PROCEDURE COUNT_SHOW_SOME_ITEM_ID_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Description VARCHAR(150) )
  DYNAMIC RESULT SETS 4
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;
		 DECLARE my_cv_count_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;

		 DECLARE my_cv_ROWCOUNT INTEGER ;
		 BEGIN
		 		 DECLARE view_rec_itm_id INTEGER ;

		 		 DECLARE sql_stmt VARCHAR(200) ;
                DECLARE sql_count_stmt VARCHAR(200);
		 		 DECLARE rows_fetched INTEGER ;

		 		 DECLARE KEYS_TO_SHOW BIGINT DEFAULT 5;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST2_STMT STATEMENT ;
		 		 DECLARE my_cv_count_STMT STATEMENT;



		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST2_STMT;
                DECLARE my_cv_count CURSOR WITH HOLD WITH RETURN TO CALLER FOR my_cv_count_STMT;
		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN

                  INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Error in proc COUNT_SHOW_SOME_ITEM_ID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Description)) || CHAR(')')); 
                  COMMIT;
                  

                END;

		 		 BEGIN
		 		 		 DECLARE EXIT HANDLER FOR SQLSTATE '42710'
		 		 		 BEGIN
		 		 		 END;
		 		 END;



		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('*** REPORT FOR VIEW ') || RTRIM(CHAR(View_Name)) || CHAR(', Description = ') || RTRIM(CHAR(Description)));
		 		 COMMIT;
		 		 



		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));
                SET sql_count_stmt = CHAR('SELECT COUNT(*) FROM ') || CHAR(LTRIM(RTRIM(View_Name)));

		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_count_STMT FROM sql_count_stmt ;
		 		 OPEN my_cv_count ;


		 		 

                BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_count_FOUND = SQLCODE ;
		 		 		 FETCH FROM my_cv_count INTO rows_fetched ;
		 		 END;		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('Total number of item ids in view = ') || RTRIM(CHAR(rows_fetched)));
		 		 COMMIT;
		 		 

		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv_count ;


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 SET my_cv_ROWCOUNT = 1;
		 		 
		 		 PREPARE my_cv_ST2_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL2:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL2;
		 		 		 END IF;

		 		 		 IF my_cv_ROWCOUNT >  KEYS_TO_SHOW THEN
		 		 		 		 LEAVE LOOP_LABEL2;
		 		 		 END IF;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('Next item id in this view = ') || RTRIM(CHAR(view_rec_itm_id)));
		 		 COMMIT;
		 		 SET my_cv_ROWCOUNT = my_cv_ROWCOUNT + 1;
		 		 

		 		 END LOOP LOOP_LABEL2 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;

		 END ;
END p1  @
 


CREATE PROCEDURE SHOW_ALL_PK_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Description VARCHAR(150) )
  DYNAMIC RESULT SETS 5
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN
		 		 

		 		 DECLARE view_rec_itm_primary_key VARCHAR(300) ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST1_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST1_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Error in proc SHOW_ALL_PRIMARY_KEY_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Description)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;

		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST1_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL3:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_primary_key ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL3;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,RTRIM(CHAR(view_rec_itm_primary_key)));
		 		 		 COMMIT;
		 		 		 

		 		 END LOOP LOOP_LABEL3 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;

		 END ;
END p1 @




CREATE PROCEDURE SHOW_ALL_IID_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Description VARCHAR(150) )
  DYNAMIC RESULT SETS 6
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN
		 		 
		 		 DECLARE view_rec_itm_id INTEGER ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST2_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST2_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 

		 		 		 --INSERT INTO MESSAGES (message_id , collab_area_id, message_text,message_description) VALUES(nextval for --message_id_seq,null,CHAR('Error in proc SHOW_ALL_IID_VIEW, View = ') || RTRIM(CHAR(View_Name)),RTRIM(CHAR(Description)));
		 		 		 COMMIT;
		 		 		 
		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Error in proc SHOW_ALL_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Description)) || CHAR(')'));
		 		 		 COMMIT;
		 		 		 


		 		 END;

		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST2_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL4:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL4;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,RTRIM(CHAR(view_rec_itm_id)));
		 		 		 COMMIT;
		 		 		 

		 		 END LOOP LOOP_LABEL4 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;

		 END ;
END p1 @
 
CREATE PROCEDURE GET_CATALOG_ID_FROM_NAME
		 ( 
		 		 name VARCHAR(100) ,
		 		 INOUT adv_returnvalue BIGINT )
  DYNAMIC RESULT SETS 12
LANGUAGE  SQL
p1: BEGIN 

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN 

		 		 DECLARE out_id BIGINT DEFAULT 0;
		 		 
		 		 DECLARE query_str VARCHAR(200) ;

		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE query_str_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND 
		 		 BEGIN 
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;

		 		 SET query_str  = 'select distinct ctg_id from ctg where ctg_name =' || '''' || name || '''' ;


		 		 SET c_OPEN_FLAG  = 1;
		 		 PREPARE c_STMT FROM query_str ;
		 		 OPEN c ;

		 		 LOOP_LABEL19:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 		 SET c_FOUND = 0;
		 		 		 FETCH FROM c INTO out_id ;
		 		 		 END ;

		 		 		 IF  c_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL19;
		 		 		 END IF;
		 		 END LOOP LOOP_LABEL19 ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = out_id;

		 		 RETURN 0;

		 END ;
END p1 @


CREATE PROCEDURE GET_COLLAB_ID_FROM_NAME
		 ( 
		 		 ca_name VARCHAR(200) ,
		 		 INOUT adv_returnvalue BIGINT )
  DYNAMIC RESULT SETS 13
LANGUAGE  SQL
p1: BEGIN 

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN 

		 		 DECLARE out_id BIGINT DEFAULT 0;
		 		 
		 		 DECLARE query_str VARCHAR(200) ;

		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE query_str_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND 
		 		 BEGIN 
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;

		 		 SET query_str  = 'select distinct coa_id from coa where coa_name =' || '''' || ca_name || '''' ;


		 		 SET c_OPEN_FLAG  = 1;
		 		 PREPARE c_STMT FROM query_str ;
		 		 OPEN c ;

		 		 LOOP_LABEL20:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 		 SET c_FOUND = 0;
		 		 		 FETCH FROM c INTO out_id ;
		 		 		 END ;

		 		 		 IF  c_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL20;
		 		 		 END IF;
		 		 END LOOP LOOP_LABEL20 ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = out_id;

		 		 RETURN 0;

		 END ;
END p1 @




CREATE PROCEDURE GET_SRC_CTG_ID_FROM_CA_ID
		 (
		 		 clnCoaId INTEGER ,
		 		 INOUT adv_returnvalue BIGINT )
  DYNAMIC RESULT SETS 15
LANGUAGE  SQL
p1: BEGIN

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE clnSrcCtgId INTEGER ;
		 		 

		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT_STR VARCHAR(4000) ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;


		 		 SET c_OPEN_FLAG  = 1;
                		 		 
		 		  SET c_STMT_STR  = 'select COA_SRC_CONTAINER_ID from coa where coa_id = '|| RTRIM(CHAR(clnCoaId));
		 		 
		 		 
		 		 
		 		 PREPARE c_STMT FROM c_STMT_STR ;
		 		 OPEN c ;

		 		 BEGIN
 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 SET c_FOUND = 0;
		 		 FETCH FROM c INTO clnSrcCtgId ;
		 		 END ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = clnSrcCtgId;

		 		 RETURN 0;

		 END ;
END p1 @
 


CREATE PROCEDURE GET_CA_CATALOG_ID_FROM_CA_ID
		 (
		 		 clnCoaId INTEGER ,
		 		 INOUT adv_returnvalue BIGINT )
  DYNAMIC RESULT SETS 14
LANGUAGE  SQL
p1: BEGIN

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE clnCoaCtgId INTEGER ;
		 		 
		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT_STR VARCHAR(4000) ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;


		 		 SET c_OPEN_FLAG  = 1;

		 		 SET c_STMT_STR  = 'select COA_DST_CONTAINER_ID from coa where coa_id = ' || RTRIM(CHAR(clnCoaId));
		 		 PREPARE c_STMT FROM c_STMT_STR ;
		 		 OPEN c ;

		 		 BEGIN
 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 SET c_FOUND = 0;
		 		 FETCH FROM c INTO clnCoaCtgId ;
		 		 END ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = clnCoaCtgId;

		 		 RETURN 0;

		 END ;
END p1 @
 



 CREATE PROCEDURE GET_CA_NAME_FROM_ID
		 (
		 		 clnCoaId INTEGER ,
		 		 INOUT adv_returnvalue VARCHAR(200) )
  DYNAMIC RESULT SETS 16
LANGUAGE  SQL
p1: BEGIN

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE clnCAName VARCHAR(100) ;
		 		 
		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT_STR VARCHAR(4000) ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;


		 		 SET c_OPEN_FLAG  = 1;

		 		 SET c_STMT_STR  = 'select COA_NAME from coa where coa_id = ' || RTRIM(CHAR(clnCoaId));
		 		 PREPARE c_STMT FROM c_STMT_STR ;
		 		 OPEN c ;

		 		 BEGIN
 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 SET c_FOUND = 0;
		 		 FETCH FROM c INTO clnCAName ;
		 		 END ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = clnCAName;

		 		 RETURN 0;

		 END ;
END p1 @
 

CREATE PROCEDURE BACKUP_ITEM_NEW
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 src_item_id BIGINT ,
		 		 ca_item_id BIGINT )
  DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- Backup an item by pk, both from collaboration area and source catalog
		 		 -- Item may be in only one or both.  Specified by positive-valued ids passed.

		 		 DECLARE EX_IMMD_VAR1 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR2 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR3 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR4 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR5 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR6 VARCHAR(4000) ;
		 		 
                 DECLARE EX_IMMD_VAR7 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_STMT1 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT2 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT3 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT4 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT5 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT6 STATEMENT ;
		 		 
                 DECLARE EX_IMMD_STMT7 STATEMENT ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION
		 		 BEGIN
		 		 
		 		  INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('Error in proc BACKUP_ITEM_NEW(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		  COMMIT;
		 		  
		 		 END;

		 		 SET EX_IMMD_VAR1  = 'insert into ITM_CLNCOA (select * from itm where itm_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 SET EX_IMMD_VAR2  = 'insert into ITA_CLNCOA (select * from ita where ita_item_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 SET EX_IMMD_VAR7  = 'insert into ITD_CLNCOA (select * from itd where itd_item_id = '||RTRIM(CHAR(ca_item_id))||')';
		 		 
                 SET EX_IMMD_VAR3  = 'insert into ICM_CLNCOA (select * from icm where icm_item_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 SET EX_IMMD_VAR4  = 'insert into CEM_CLNCOA (select * from cem where cem_ca_entry_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 SET EX_IMMD_VAR5  = 'insert into CAE_CLNCOA (select * from cae where cae_entry_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 SET EX_IMMD_VAR6  = 'insert into CEL_CLNCOA (select * from cel where cel_entry_id = '||RTRIM(CHAR(ca_item_id))||')';

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('STARTING BACKUP_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

		 		 -- Do source item first
		 		 IF (  src_item_id >  0 ) THEN

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('BACKUP SOURCE ITEM ID=') || RTRIM(CHAR(src_item_id)));
		 		 		 COMMIT;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITMRSV_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  itm
		 		 		 WHERE		  itm_id  = src_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEM_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  cem
		 		 		 WHERE		  cem_src_entry_id  = src_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEH_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ceh
		 		 		 WHERE		  ceh_entry_key  = primary_key
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;
		 		 -- DO CA item
		 		 IF (  ca_item_id >  0 ) THEN

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('BACKUP CA ITEM ID=') || RTRIM(CHAR(ca_item_id)));
		 		 		 COMMIT;
		 		 		 

		 		 		 		 		 		 PREPARE EX_IMMD_STMT1 FROM EX_IMMD_VAR1 ;
		 		 		  EXECUTE EX_IMMD_STMT1;
		 		 		 		 		 		 PREPARE EX_IMMD_STMT2 FROM EX_IMMD_VAR2 ;
		 		 		  EXECUTE EX_IMMD_STMT2;
                                                 PREPARE EX_IMMD_STMT7 FROM EX_IMMD_VAR7 ;
		 		 		  EXECUTE EX_IMMD_STMT7;
		 		 		 		 		 		 PREPARE EX_IMMD_STMT3 FROM EX_IMMD_VAR3 ;
		 		 		  EXECUTE EX_IMMD_STMT3;
		 		 		 		 		 		 PREPARE EX_IMMD_STMT4 FROM EX_IMMD_VAR4 ;
		 		 		  EXECUTE EX_IMMD_STMT4;
		 		 		 		 		 		 PREPARE EX_IMMD_STMT5 FROM EX_IMMD_VAR5 ;
		 		 		  EXECUTE EX_IMMD_STMT5;

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEH_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ceh
		 		 		 WHERE		  ceh_entry_key  = primary_key
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 		 		 		 		 PREPARE EX_IMMD_STMT6 FROM EX_IMMD_VAR6 ;
		 		 		  EXECUTE EX_IMMD_STMT6;

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  WEE_CLNCOA
		 		 		 (SELECT
		 		 		 		 		  clnCoaId,
		 		 		 		 		  wee_event_id,
		 		 		 		 		  wee_entry_id,
		 		 		 		 		  wee_event_entry_status
		 		 		 FROM  wee
		 		 		 WHERE		  wee_entry_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;
 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('FINISHED BACKUP_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
 		 		 COMMIT;
 		 		 


		 END p1 @

 CREATE PROCEDURE DELETE_CA_FROM_BACKUP_TABLES
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 16
LANGUAGE  SQL
p1: BEGIN

		 DECLARE rt_val1 BIGINT ;

		 DECLARE rt_val2 VARCHAR(200) ;

		 DECLARE rt_val3 BIGINT ;
		 BEGIN
		 		 -- The Views are assumed to exist and contain data for the desired collaboration area.

		 		 DECLARE clnCAName VARCHAR(100) ;

		 		 DECLARE clnSrcCtgId INTEGER ;

		 		 DECLARE clnCoaCtgId INTEGER ;
		 		 CALL GET_SRC_CTG_ID_FROM_CA_ID(clnCoaId, rt_val1);

		 		 SET clnSrcCtgId  = rt_val1;
		 		 CALL GET_CA_NAME_FROM_ID(clnCoaId, rt_val2);

		 		 SET clnCAName  = rt_val2;
		 		 CALL GET_CA_CATALOG_ID_FROM_CA_ID(clnCoaId, rt_val3);

		 		 SET clnCoaCtgId  = rt_val3;


		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   ITMRSV_CLNCOA
		 		 WHERE  itm_container_id  = clnSrcCtgId
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   ITM_CLNCOA
		 		 WHERE  itm_container_id  = clnCoaCtgId
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   ITA_CLNCOA
		 		 WHERE  ita_catalog_id  = clnCoaCtgId
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   ICM_CLNCOA
		 		 WHERE  icm_catalog_id  = clnCoaCtgId
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   CEM_CLNCOA
		 		 WHERE  cem_ca_id  = DECIMAL(clnCoaId)
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   CAE_CLNCOA
		 		 WHERE  cae_ca_id  = DECIMAL(clnCoaId)
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   CEH_CLNCOA
		 		 WHERE  ceh_ca_name  = clnCAName
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   CEL_CLNCOA
		 		 WHERE  cel_container_id  = DECIMAL(clnCoaId)
		 		 ;
		 		 END;
		 		 
		 		 
		 		 BEGIN
		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 BEGIN
		 		 		 END;
		 		 		 DELETE FROM   WEE_CLNCOA
		 		 WHERE  coa_id  = clnCoaId
		 		 ;
		 		 END;
		 		 
		 		 COMMIT;

		 END ;
END p1 @



CREATE PROCEDURE CLEAN_ITEM
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 src_item_id BIGINT ,
		 		 ca_item_id BIGINT )
DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- CLEAN an item by pk, both from collaboration area and source catalog
		 		 -- Item may be in only one or both.  Specified by positive-valued ids passed.

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('Error in proc CLEAN_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		 		 COMMIT;
		 		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('STARTING CLEAN_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		 CALL BACKUP_ITEM_NEW(clnCoaId, primary_key, src_item_id, ca_item_id);
		 		 -- Do source item first
		 		 IF (  src_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('CLEANING SOURCE ITEM ID=') || RTRIM(CHAR(src_item_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 UPDATE  itm
		 		 		 SET		 itm_collab_info = ''
		 		 		 WHERE  itm_id  = src_item_id
		 		 		  AND		 itm_next_version_id  = 999999999
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cem
		 		 		 WHERE  cem_src_entry_id  = src_item_id and cem_ca_id = clnCoaId
                          ;
		 		 		 END;
		 		 		 
		 		 END IF ;
		 		 -- DO CA item
		 		 IF (  ca_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('     CLEANING CA ITEM ID=') || RTRIM(CHAR(ca_item_id)));
		 		 		 COMMIT;
		 		 		 
		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itm
		 		 		 WHERE  itm_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ita
		 		 		 WHERE  ita_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itd
		 		 		 WHERE  itd_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   icm
		 		 		 WHERE  icm_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cem
		 		 		 WHERE  cem_ca_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cae
		 		 		 WHERE  cae_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ceh
		 		 		 WHERE  ceh_entry_key  = primary_key
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cel
		 		 		 WHERE  cel_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   wee
		 		 		 WHERE  wee_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('FINISHED CLEAN_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 END p1 @

		 

		 
		  




CREATE PROCEDURE BACKUP_CA_ITEM
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 ca_item_id BIGINT )
  DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- Backup an item known in a collaboration area (i.e. not a source catalog item)
		 		 --
		 		 -- primary_key may be NULL, but ca_item_id should not be.
		 		 --

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 
		 		  INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId,CHAR('Error in proc BACKUP_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		  COMMIT;
		 		  


		 		 END;
 		 		 
		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('STARTING BACKUP_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

		 		 -- DO CA item
		 		 IF (  ca_item_id >  0 ) THEN


 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('BACKUP CA ITEM ID=') || RTRIM(CHAR(ca_item_id)));
 		 		 COMMIT;
 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITM_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  itm
		 		 		 WHERE		  itm_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITA_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ita
		 		 		 WHERE		  ita_item_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ICM_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  icm
		 		 		 WHERE		  icm_item_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEM_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  cem
		 		 		 WHERE		  cem_ca_entry_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CAE_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  cae
		 		 		 WHERE		  cae_entry_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEH_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ceh
		 		 		 WHERE		  ceh_entry_key  = primary_key
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEL_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  cel
		 		 		 WHERE		  cel_entry_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  WEE_CLNCOA
		 		 		 (SELECT
		 		 		 		 		  clnCoaId,
		 		 		 		 		  wee_event_id,
		 		 		 		 		  wee_entry_id,
		 		 		 		 		  wee_event_entry_status
		 		 		 FROM  wee
		 		 		 WHERE		  wee_entry_id  = ca_item_id
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;
		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('FINISHED BACKUP_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 END p1 @


 CREATE PROCEDURE CLEAN_CA_ITEM
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 ca_item_id BIGINT )
  DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- CLEAN an item known in a collaboration area (i.e. not a source catalog item)
		 		 --
		 		 -- WARNING: be careful not to pass in the id of an item in a source catalog; it will be deleted !!!!!!!
		 		 --

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('Error in proc CLEAN_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('STARTING CLEAN_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		 CALL BACKUP_CA_ITEM(clnCoaId, primary_key, ca_item_id);
		 		 -- DO CA item
		 		 IF (  ca_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('CLEANING CA ITEM ID=') || RTRIM(CHAR(ca_item_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itm
		 		 		 WHERE  itm_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ita
		 		 		 WHERE  ita_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itd
		 		 		 WHERE  itd_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		
                         
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   icm
		 		 		 WHERE  icm_item_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cem
		 		 		 WHERE  cem_ca_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cae
		 		 		 WHERE  cae_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ceh
		 		 		 WHERE  ceh_entry_key  = primary_key
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cel
		 		 		 WHERE  cel_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   wee
		 		 		 WHERE  wee_entry_id  = ca_item_id
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;


		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('FINISHED CLEAN_CA_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(ca_item_id)) || CHAR(')'));
		 COMMIT;
		 


		 END p1 @



 CREATE PROCEDURE GET_ITEM_ID
		 (
		 		 catalog_id INTEGER ,
		 		 pk VARCHAR(300) ,
		 		 INOUT adv_returnvalue BIGINT )
  DYNAMIC RESULT SETS 7
LANGUAGE  SQL
p1: BEGIN

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE out_id BIGINT DEFAULT 0;
		 		 
		 		 DECLARE query_str VARCHAR(200) ;

		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE query_str_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,  NOT FOUND
		 		 BEGIN
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;

		 		 SET query_str  = 'select distinct itm_id from itm where itm_primary_key = ' || '''' || pk || ''''  || ' and itm_container_id = ' || CHAR(catalog_id) ;

        INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(2005, null,CHAR(query_str));
        COMMIT;
        
		 		 SET c_OPEN_FLAG  = 1;
		 		 PREPARE c_STMT FROM query_str ;
		 		 OPEN c ;

		 		 LOOP_LABEL5:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 		 SET c_FOUND = 0;
		 		 		 FETCH FROM c INTO out_id ;
		 		 		 END ;

		 		 		 IF  c_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL5;
		 		 		 END IF;
		 		 END LOOP LOOP_LABEL5 ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = out_id;
                INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(2006, null,CHAR(adv_returnvalue));
                COMMIT;
                
		 		 RETURN 0;

		 END ;
END p1  @



CREATE PROCEDURE GET_ITEM_PRIMARY_KEY
		 (
		 		 catalog_id INTEGER ,
		 		 item_id BIGINT ,
		 		 INOUT adv_returnvalue VARCHAR(300) )
  DYNAMIC RESULT SETS 8
LANGUAGE  SQL
p1: BEGIN

		 DECLARE c_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE out_pk VARCHAR(300) DEFAULT ' ' ;
		 		 
		 		 DECLARE query_str VARCHAR(200) ;

		 		 DECLARE c_OPEN_FLAG INTEGER ;

		 		 DECLARE c_STMT STATEMENT ;

		 		 DECLARE query_str_STMT STATEMENT ;

		 		 DECLARE c CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR c_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLSTATE '02000'
		 		 BEGIN 		 		 		 BEGIN END ;

		 		 END;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 		 		 		 -- Consider logging the error and then re-raise
		 		 		 RESIGNAL;

		 		 END;

		 		 SET query_str  = 'select distinct itm_primary_key from itm where itm_id = ' || CHAR(item_id) || ' and itm_container_id = ' || CHAR(catalog_id) ;


		 		 SET c_OPEN_FLAG  = 1;
		 		 PREPARE c_STMT FROM query_str ;
		 		 OPEN c ;

		 		 LOOP_LABEL6:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_FOUND = SQLCODE ;
		 		 		 SET c_FOUND = 0;
		 		 		 FETCH FROM c INTO out_pk ;
		 		 		 END ;

		 		 		 IF  c_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL6;
		 		 		 END IF;
		 		 END LOOP LOOP_LABEL6 ;
		 		  SET c_OPEN_FLAG = 0;
 		 		 CLOSE c ;

		 		 SET adv_returnvalue  = out_pk;

		 		 RETURN 0;

		 END ;
END p1 @

CREATE PROCEDURE CLEAN_CA_ITEMS_OF_IID_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Collab_Id INTEGER ,
		 		 ca_catalog_id INTEGER )
  DYNAMIC RESULT SETS 10
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;

		 DECLARE rt_val1 VARCHAR(300) ;
		 BEGIN		 
		 

		 		 DECLARE view_rec_itm_id INTEGER ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE ca_item_id INTEGER ;

		 		 DECLARE pk VARCHAR(300) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST2_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST2_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('Error in proc CLEAN_CA_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('STARTING CLEAN_CA_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

  		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));

		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST2_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL8:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL8;
		 		 		 END IF;

		 		 		 SET ca_item_id  = view_rec_itm_id;
		 		 		 CALL GET_ITEM_PRIMARY_KEY(ca_catalog_id, ca_item_id, rt_val1);

		 		 		 SET pk  = rt_val1;

		 		 		 CALL CLEAN_CA_ITEM(Collab_Id, pk, ca_item_id);
		 		 END LOOP LOOP_LABEL8 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('FINISHED CLEAN_CA_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 END ;
END p1 @

CREATE PROCEDURE BACKUP_SRC_ITEM
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 src_item_id BIGINT )
DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- Backup an item known to be in a source catalog (i.e. not a collaboration area item).
		 		 --
		 		 -- primary_key may be NULL, but src_item_id should not be.
		 		 --

		 		 DECLARE EX_IMMD_VAR7 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_VAR8 VARCHAR(4000) ;

		 		 DECLARE EX_IMMD_STMT7 STATEMENT ;

		 		 DECLARE EX_IMMD_STMT8 STATEMENT ;

		 --		 DECLARE EXIT HANDLER FOR SQLEXCEPTION
		 --		 BEGIN
		 		 

		 --		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('Error in proc BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 COMMIT;
		 


		 --		 END;

		 		 SET EX_IMMD_VAR7  = 'insert into ITMRSV_CLNCOA (select * from itm where itm_id = ' || RTRIM(CHAR(src_item_id)) || ' and itm_next_version_id=999999999)';

		 		 SET EX_IMMD_VAR8  = 'insert into CEM_CLNCOA  (select * from cem where cem_src_entry_id = ' || RTRIM(CHAR(src_item_id)) || ')';


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('STARTING BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

		 		 -- Do source item first
		 		 IF (  src_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('BACKUP SOURCE ITEM ID=') || RTRIM(CHAR(src_item_id)));
		 		 		 COMMIT;
		 		 		 

		 		 		 		 		 		 PREPARE EX_IMMD_STMT7 FROM EX_IMMD_VAR7 ;
		 		 		  EXECUTE EX_IMMD_STMT7;
		 		 		 		 		 		 PREPARE EX_IMMD_STMT8 FROM EX_IMMD_VAR8 ;
		 		 		  EXECUTE EX_IMMD_STMT8;

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  CEH_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ceh
		 		 		 WHERE		  ceh_entry_key  = primary_key
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('FINISHED BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 END p1 @
 


CREATE PROCEDURE CLEAN_SRC_ITEM
		 (
		 		 clnCoaId INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 src_item_id BIGINT )
DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- CLEAN an item known to be in a source catalog (i.e. not a collaboration area item).
		 		 --
		 		 -- Passing a CA item by mistake should be benign.
		 		 --

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('Error in proc CLEAN_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('STARTING CLEAN_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		 CALL BACKUP_SRC_ITEM(clnCoaId, primary_key, src_item_id);
		 		 -- Do source item first
		 		 IF (  src_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, CHAR('CLEANING SOURCE ITEM ID=') || RTRIM(CHAR(src_item_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 UPDATE  itm
		 		 		 SET		 itm_collab_info = ''
		 		 		 WHERE  itm_id  = src_item_id
		 		 		  AND		 itm_next_version_id  = 999999999
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cem
		 		 		 WHERE  cem_src_entry_id  = src_item_id
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END IF ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, clnCoaId, CHAR('FINISHED CLEAN_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 END p1 @
 




CREATE PROCEDURE CLEAN_SRC_ITEMS_OF_IID_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Collab_Id INTEGER ,
		 		 src_catalog_id INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;

		 DECLARE rt_val1 VARCHAR(300) ;
		 BEGIN


		 		 DECLARE view_rec_itm_id INTEGER ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE src_item_id INTEGER ;

		 		 DECLARE pk VARCHAR(300) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST1_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST1_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,  NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, Collab_Id, CHAR('Error in proc CLEAN_SRC_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, Collab_Id, CHAR('STARTING CLEAN_SRC_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST1_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL9:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL9;
		 		 		 END IF;

		 		 		 SET src_item_id  = view_rec_itm_id;
		 		 		 CALL GET_ITEM_PRIMARY_KEY(src_catalog_id, src_item_id, rt_val1);

		 		 		 SET pk  = rt_val1;

		 		 		 CALL CLEAN_SRC_ITEM(Collab_Id, pk, src_item_id);
		 		 END LOOP LOOP_LABEL9 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, Collab_Id, CHAR('FINISHED CLEAN_SRC_ITEMS_OF_IID_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 END ;
END p1 @
 



 CREATE PROCEDURE RESTORE_ceh
		 (
		 		 clnCAName VARCHAR(100) )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE ceh_rec_ceh_cmp_id BIGINT ;

		 		 DECLARE ceh_rec_ceh_id BIGINT ;

		 		 DECLARE ceh_rec_ceh_date TIMESTAMP ;

		 		 DECLARE ceh_rec_ceh_entry_key VARCHAR(300) ;

		 		 DECLARE ceh_rec_ceh_ca_name VARCHAR(100) ;

		 		 DECLARE ceh_rec_ceh_wfl_name VARCHAR(100) ;

		 		 DECLARE ceh_rec_ceh_user_name VARCHAR(20) ;

		 		 DECLARE ceh_rec_ceh_step_path VARCHAR(3000) ;

		 		 DECLARE ceh_rec_ceh_event VARCHAR(100) ;

		 		 DECLARE ceh_rec_ceh_data VARCHAR(900) ;
		 		 
		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  ceh_clncoa
		 		 WHERE		  ceh_ca_name  = clnCAName
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'Error in proc RESTORE_ceh' );
		 		 		 COMMIT;
		 		 		 


		 		 END;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF ceh TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL17:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO ceh_rec_ceh_cmp_id, ceh_rec_ceh_id, ceh_rec_ceh_date, ceh_rec_ceh_entry_key, ceh_rec_ceh_ca_name, ceh_rec_ceh_wfl_name, ceh_rec_ceh_user_name, ceh_rec_ceh_step_path, ceh_rec_ceh_event, ceh_rec_ceh_data ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL17;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Next ceh row = ') || RTRIM(CHAR(ceh_rec_ceh_event)) || CHAR(', ') || RTRIM(CHAR(ceh_rec_ceh_entry_key)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ceh
		 		 		 WHERE  (ceh_cmp_id  = ceh_rec_ceh_cmp_id
		 		 		  AND		 ceh_id  = ceh_rec_ceh_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ceh
		 		 		  VALUES 		 		 ( ceh_rec_ceh_cmp_id ,
		 		 		 		 		 ceh_rec_ceh_id ,
		 		 		 		 		 ceh_rec_ceh_date ,
		 		 		 		 		 ceh_rec_ceh_entry_key ,
		 		 		 		 		 ceh_rec_ceh_ca_name ,
		 		 		 		 		 ceh_rec_ceh_wfl_name ,
		 		 		 		 		 ceh_rec_ceh_user_name ,
		 		 		 		 		 ceh_rec_ceh_step_path ,
		 		 		 		 		 ceh_rec_ceh_event ,
		 		 		 		 		 ceh_rec_ceh_data,
                                                                                 -1
		 		 		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL17 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @




CREATE PROCEDURE RESTORE_cel
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE cel_rec_cel_cmp_id BIGINT ;

		 		 DECLARE cel_rec_cel_id BIGINT ;

		 		 DECLARE cel_rec_cel_user_id BIGINT ;

		 		 DECLARE cel_rec_cel_time_acquired TIMESTAMP ;

		 		 DECLARE cel_rec_cel_time_of_expiry TIMESTAMP ;

		 		 DECLARE cel_rec_cel_container_id BIGINT ;

		 		 DECLARE cel_rec_cel_container_type VARCHAR(200) ;

		 		 DECLARE cel_rec_cel_entry_id BIGINT ;

		 		 DECLARE cel_rec_cel_step_path_id BIGINT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  cel_clncoa
		 		 WHERE		  cel_container_id  = clnCoaId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'Error in proc RESTORE_cel' );
		 		 		 COMMIT;
		 		 		 


		 		 END;


		 		 



		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF cel TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL14:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO cel_rec_cel_cmp_id, cel_rec_cel_id, cel_rec_cel_user_id, cel_rec_cel_time_acquired, cel_rec_cel_time_of_expiry, cel_rec_cel_container_id, cel_rec_cel_container_type, cel_rec_cel_entry_id, cel_rec_cel_step_path_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL14;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,CHAR('Next cel row = ') || RTRIM(CHAR(cel_rec_cel_entry_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cel
		 		 		 WHERE  (cel_cmp_id  = cel_rec_cel_cmp_id
		 		 		  AND		 cel_id  = cel_rec_cel_id
		 		 		  AND		 cel_user_id  = cel_rec_cel_user_id
		 		 		  AND		 cel_container_id  = cel_rec_cel_container_id
		 		 		  AND		 cel_container_type  = cel_rec_cel_container_type
		 		 		  AND		 cel_entry_id  = cel_rec_cel_entry_id
		 		 		  AND		 cel_step_path_id  = cel_rec_cel_step_path_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  cel
		 		 		  VALUES 		 		 ( cel_rec_cel_cmp_id ,
		 		 		 		 		 cel_rec_cel_id ,
		 		 		 		 		 cel_rec_cel_user_id ,
		 		 		 		 		 cel_rec_cel_time_acquired ,
		 		 		 		 		 cel_rec_cel_time_of_expiry ,
		 		 		 		 		 cel_rec_cel_container_id ,
		 		 		 		 		 cel_rec_cel_container_type ,
		 		 		 		 		 cel_rec_cel_entry_id ,
		 		 		 		 		 cel_rec_cel_step_path_id )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL14 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @


CREATE PROCEDURE RESTORE_cem
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE cem_rec_cem_cmp_id BIGINT ;

		 		 DECLARE cem_rec_cem_ca_id BIGINT ;

		 		 DECLARE cem_rec_cem_ca_entry_id BIGINT ;

		 		 DECLARE cem_rec_cem_src_ctg_id BIGINT ;

		 		 DECLARE cem_rec_cem_src_entry_id BIGINT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  cem_clncoa
		 		 WHERE		  cem_ca_id  = clnCoaId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'Error in proc RESTORE_cem' );
		 		 		 COMMIT;
		 		 		 


		 		 END;




		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'*** BEGIN RESTORE OF cem TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL16:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO cem_rec_cem_cmp_id, cem_rec_cem_ca_id, cem_rec_cem_ca_entry_id, cem_rec_cem_src_ctg_id, cem_rec_cem_src_entry_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL16;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,CHAR('Next cem row = ') || RTRIM(CHAR(cem_rec_cem_ca_entry_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cem
		 		 		 WHERE  (cem_cmp_id  = cem_rec_cem_cmp_id
		 		 		  AND		 cem_ca_id  = cem_rec_cem_ca_id
		 		 		  AND		 cem_ca_entry_id  = cem_rec_cem_ca_entry_id
		 		 		  AND		 cem_src_ctg_id  = cem_rec_cem_src_ctg_id
		 		 		  AND		 cem_src_entry_id  = cem_rec_cem_src_entry_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  cem
		 		 		  VALUES 		 		 ( cem_rec_cem_cmp_id ,
		 		 		 		 		 cem_rec_cem_ca_id ,
		 		 		 		 		 cem_rec_cem_ca_entry_id ,
		 		 		 		 		 cem_rec_cem_src_ctg_id ,
		 		 		 		 		 cem_rec_cem_src_entry_id )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL16 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @



CREATE PROCEDURE RESTORE_cae
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE cae_rec_cae_cmp_id BIGINT ;

		 		 DECLARE cae_rec_cae_ca_id BIGINT ;

		 		 DECLARE cae_rec_cae_entry_id BIGINT ;

		 		 DECLARE cae_rec_cae_step_path_id BIGINT ;

		 		 DECLARE cae_rec_cae_deadline_step_path_id BIGINT ;

		 		 DECLARE cae_rec_cae_deadline TIMESTAMP ;

		 		 DECLARE cae_rec_cae_data VARCHAR(3000) ;

		 		 DECLARE cae_rec_cae_available VARCHAR(1) ;

		 		 DECLARE cae_rec_cae_lock_id BIGINT ;

               
		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  cae_clncoa
		 		 WHERE		  cae_ca_id  = clnCoaId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'Error in proc RESTORE_cae' );
		 		 COMMIT;
		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'*** BEGIN RESTORE OF cae TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL15:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO cae_rec_cae_cmp_id, cae_rec_cae_ca_id, cae_rec_cae_entry_id, cae_rec_cae_step_path_id, cae_rec_cae_deadline_step_path_id, cae_rec_cae_deadline, cae_rec_cae_data, cae_rec_cae_available, cae_rec_cae_lock_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL15;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,CHAR('Next cae row = ') || RTRIM(CHAR(cae_rec_cae_step_path_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   cae
		 		 		 WHERE  (cae_cmp_id  = cae_rec_cae_cmp_id
		 		 		  AND		 cae_ca_id  = cae_rec_cae_ca_id
		 		 		  AND		 cae_entry_id  = cae_rec_cae_entry_id
		 		 		  AND		 cae_step_path_id  = cae_rec_cae_step_path_id
		 		 		  AND		 cae_deadline_step_path_id  = cae_rec_cae_deadline_step_path_id
		 		 		  AND		 cae_data  = cae_rec_cae_data
		 		 		  AND		 cae_available  = cae_rec_cae_available
		 		 		  AND		 cae_lock_id  = cae_rec_cae_lock_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  cae
		 		 		  VALUES 		 		 ( cae_rec_cae_cmp_id ,
		 		 		 		 		 cae_rec_cae_ca_id ,
		 		 		 		 		 cae_rec_cae_entry_id ,
		 		 		 		 		 cae_rec_cae_step_path_id ,
		 		 		 		 		 cae_rec_cae_deadline_step_path_id ,
		 		 		 		 		 cae_rec_cae_deadline ,
		 		 		 		 		 cae_rec_cae_data ,
		 		 		 		 		 cae_rec_cae_available ,
		 		 		 		 		 cae_rec_cae_lock_id )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL15 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @



CREATE PROCEDURE RESTORE_itm
		 (
		 		 clnCoaCtgId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE itm_rec_itm_company_id BIGINT ;

		 		 DECLARE itm_rec_itm_id BIGINT ;

		 		 DECLARE itm_rec_itm_container_id BIGINT ;

		 		 DECLARE itm_rec_itm_primary_key VARCHAR(300) ;

		 		 DECLARE itm_rec_itm_version_id BIGINT ;

		 		 DECLARE itm_rec_itm_next_version_id BIGINT ;

		 		 DECLARE itm_rec_itm_status VARCHAR(300) ;

		 		 DECLARE itm_rec_itm_collab_info VARCHAR(3000) ;
		 		 
		  
		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  itm_clncoa
		 		 WHERE		  itm_container_id  = clnCoaCtgId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'Error in proc RESTORE_itm' );
		 		 		 COMMIT;
		 		 		 


		 		 END;


		 		 



		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF itm TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL11:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO itm_rec_itm_company_id, itm_rec_itm_id, itm_rec_itm_container_id, itm_rec_itm_primary_key, itm_rec_itm_version_id, itm_rec_itm_next_version_id, itm_rec_itm_status, itm_rec_itm_collab_info;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL11;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Next itm row = ') || CHAR(itm_rec_itm_container_id) || CHAR(', ') || RTRIM(CHAR(itm_rec_itm_primary_key)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itm
		 		 		 WHERE  (itm_company_id  = itm_rec_itm_company_id
		 		 		  AND		 itm_id  = itm_rec_itm_id
		 		 		  AND		 itm_container_id  = itm_rec_itm_container_id
		 		 		  AND		 itm_primary_key  = itm_rec_itm_primary_key
		 		 		  AND		 itm_version_id  = itm_rec_itm_version_id
		 		 		  AND		 itm_next_version_id  = itm_rec_itm_next_version_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  itm
		 		 		  VALUES 		 		 ( itm_rec_itm_company_id ,
		 		 		 		 		 itm_rec_itm_id ,
		 		 		 		 		 itm_rec_itm_container_id ,
		 		 		 		 		 itm_rec_itm_primary_key ,
		 		 		 		 		 itm_rec_itm_version_id ,
		 		 		 		 		 itm_rec_itm_next_version_id ,
		 		 		 		 		 itm_rec_itm_status ,
		 		 		 		 		 itm_rec_itm_collab_info,
		 		 		 		 		 null)
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL11 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1  @

CREATE PROCEDURE RESTORE_ita
		 (
		 		 clnCoaCtgId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE ita_rec_ita_company_id BIGINT ;

		 		 DECLARE ita_rec_ita_catalog_id BIGINT ;

		 		 DECLARE ita_rec_ita_item_id BIGINT ;

		 		 DECLARE ita_rec_ita_node_id BIGINT ;

		 		 DECLARE ita_rec_ita_value_numeric DECIMAL(31,12) ;

		 		 DECLARE ita_rec_ita_value_string VARCHAR(900) ;

		 		 DECLARE ita_rec_ita_value_string_search VARCHAR(750) ;

		 		 DECLARE ita_rec_ita_value_date TIMESTAMP;
 
		 		 DECLARE ita_rec_ita_version_id BIGINT ;

		 		 DECLARE ita_rec_ita_next_version_id BIGINT ;

		 		 DECLARE ita_rec_ita_order BIGINT ;

		 		 DECLARE ita_rec_ita_parent_order BIGINT ;

		 		 DECLARE ita_rec_ita_row_order BIGINT ;

		 		 DECLARE ita_rec_ita_mod_user_id BIGINT ;

		 		 DECLARE ita_rec_ita_mod_date TIMESTAMP;

                                 DECLARE ita_rec_ita_occurrence_id             BIGINT;
                                 DECLARE ita_rec_ita_value_string_ignorecase   VARCHAR(4000);

                 
		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  ita_clncoa
		 		 WHERE		  ita_catalog_id  = clnCoaCtgId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'Error in proc RESTORE_ita' );
		 		 		 COMMIT;
		 		 		 


		 		 END;


		 		 



		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF ita TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL12:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO ita_rec_ita_company_id, ita_rec_ita_catalog_id, ita_rec_ita_item_id, ita_rec_ita_node_id, ita_rec_ita_value_numeric, ita_rec_ita_value_string,ita_rec_ita_version_id, ita_rec_ita_next_version_id,ita_rec_ita_occurrence_id, ita_rec_ita_value_string_ignorecase  ;             
		 		 		-- FETCH FROM my_cv INTO ita_rec_ita_company_id, ita_rec_ita_catalog_id, ita_rec_ita_item_id, ita_rec_ita_node_id, ita_rec_ita_value_numeric, ita_rec_ita_value_string,ita_rec_ita_value_string_search,ita_rec_ita_value_date,ita_rec_ita_version_id, ita_rec_ita_next_version_id,ita_rec_ita_order,ita_rec_ita_parent_order,ita_rec_ita_row_order, ita_rec_ita_mod_user_id,ita_rec_ita_mod_date;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL12;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Next ita row = ') || RTRIM(CHAR(ita_rec_ita_item_id)) || CHAR(', ') || RTRIM(CHAR(ita_rec_ita_node_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   ita
		 		 		 WHERE  (ita_company_id  = ita_rec_ita_company_id
		 		 		  AND		 ita_catalog_id  = ita_rec_ita_catalog_id
		 		 		  AND		 ita_item_id  = ita_rec_ita_item_id
		 		 		  AND		 ita_node_id  = ita_rec_ita_node_id
		 		 		  AND		 ita_value_string  = ita_rec_ita_value_string
 		 --		 		  AND		 ita_value_string_search  = ita_rec_ita_value_string_search
		 -- 		 		  AND    ita_value_date = ita_rec_ita_value_date
		 		 		  AND		 ita_version_id  = ita_rec_ita_version_id
		 		 		  AND		 ita_next_version_id  = ita_rec_ita_next_version_id
    		-- 		 		  AND		 ita_order  = ita_rec_ita_order
                --         AND		 ita_parent_order  = ita_rec_ita_parent_order
                --         AND		 ita_row_order  = ita_rec_ita_row_order
                --         AND    ita_mod_user_id = ita_rec_ita_mod_user_id		 		 		  
	        --	                  AND    ita_mod_date = ita_rec_ita_mod_date
                                          AND ita_occurrence_id =  ita_rec_ita_occurrence_id             
                                          AND ita_value_string_ignorecase =  ita_rec_ita_value_string_ignorecase   
                                                 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ita
		 		 		  VALUES 		 		 ( ita_rec_ita_company_id ,
		 		 		 		 		 ita_rec_ita_catalog_id ,
		 		 		 		 		 ita_rec_ita_item_id ,
		 		 		 		 		 ita_rec_ita_node_id ,
		 		 		 		 		 ita_rec_ita_value_numeric ,
		 		 		 		 		 ita_rec_ita_value_string ,
		 		 		 		 	--	 ita_rec_ita_value_string_search, 
		 		 		 		 --		 ita_rec_ita_value_date,
		 		 		 		 		 ita_rec_ita_version_id ,
		 		 		 		 		 ita_rec_ita_next_version_id,
		 		 		 		 --		 ita_rec_ita_order,
		 		 		 		 --		 ita_rec_ita_parent_order,
  		 		 		 		  --       ita_rec_ita_row_order,
                                     --   ita_rec_ita_mod_user_id,
                                      --  ita_rec_ita_mod_date 
                                          ita_rec_ita_occurrence_id,             
                                          ita_rec_ita_value_string_ignorecase   
                                                  )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL12 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1   @

 CREATE PROCEDURE RESTORE_icm
		 (
		 		 clnCoaCtgId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN

		 		 DECLARE icm_rec_icm_company_id BIGINT ;

		 		 DECLARE icm_rec_icm_catalog_id BIGINT ;

		 		 DECLARE icm_rec_icm_item_id BIGINT ;

		 		 DECLARE icm_rec_icm_cat_tree_id BIGINT ;

		 		 DECLARE icm_rec_icm_category_id BIGINT ;

		 		 DECLARE icm_rec_icm_version_id BIGINT ;

		 		 DECLARE icm_rec_icm_next_version_id BIGINT ;

 		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  icm_clncoa
		 		 WHERE		  icm_catalog_id  = clnCoaCtgId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'Error in proc RESTORE_icm' );
		 		 		 COMMIT;
		 		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF icm TABLE' );
		 		 COMMIT;
		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL13:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO icm_rec_icm_company_id, icm_rec_icm_catalog_id, icm_rec_icm_item_id, icm_rec_icm_cat_tree_id, icm_rec_icm_category_id, icm_rec_icm_version_id, icm_rec_icm_next_version_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL13;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Next icm row = ') || RTRIM(CHAR(icm_rec_icm_item_id)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   icm
		 		 		 WHERE  (icm_company_id  = icm_rec_icm_company_id
		 		 		  AND		 icm_catalog_id  = icm_rec_icm_catalog_id
		 		 		  AND		 icm_item_id  = icm_rec_icm_item_id
		 		 		  AND		 icm_cat_tree_id  = icm_rec_icm_cat_tree_id
		 		 		  AND		 icm_category_id  = icm_rec_icm_category_id
		 		 		  AND		 icm_version_id  = icm_rec_icm_version_id
		 		 		  AND		 icm_next_version_id  = icm_rec_icm_next_version_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  icm
		 		 		  VALUES 		 		 ( icm_rec_icm_company_id ,
		 		 		 		 		 icm_rec_icm_catalog_id ,
		 		 		 		 		 icm_rec_icm_item_id ,
		 		 		 		 		 icm_rec_icm_cat_tree_id ,
		 		 		 		 		 icm_rec_icm_category_id ,
		 		 		 		 		 icm_rec_icm_version_id ,
		 		 		 		 		 icm_rec_icm_next_version_id )
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL13 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @
 


CREATE PROCEDURE RESTORE_itmrsv
		 (
		 		 clnSrcCtgId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;
		 BEGIN
                DECLARE itm_rec_itm_company_id BIGINT ;

		 		 DECLARE itm_rec_itm_id BIGINT ;
		 		 
		 		 DECLARE itm_rec_itm_container_id BIGINT ;

		 		 DECLARE itm_rec_itm_primary_key VARCHAR(300) ;

		 		 DECLARE itm_rec_itm_version_id BIGINT ;

		 		 DECLARE itm_rec_itm_next_version_id BIGINT ;

		 		 DECLARE itm_rec_itm_status VARCHAR(300) ;

		 		 DECLARE itm_rec_itm_collab_info VARCHAR(3000) ;
		 		 

		 		 DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
		 		 FROM  itmrsv_clncoa
		 		 WHERE		  itm_container_id  = clnSrcCtgId
		 		 ;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
		 		 BEGIN
		 		 

		 		 		 INSERT INTO MESSAGES (message_text) VALUES('Error in proc RESTORE_itm' );
		 		 		 COMMIT;
		 		 		 


		 		 END;

  		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,'*** BEGIN RESTORE OF itmrsv TABLE' );
  		 		 COMMIT;
  		 		 

		 		 -- show some primary keys

		 		 SET my_cv_OPEN_FLAG  = 1;

		 		 
		 		 OPEN my_cv ;
		 		 LOOP_LABEL10:		 		 LOOP
		 		 		 -- -- restore row
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO itm_rec_itm_company_id, itm_rec_itm_id, itm_rec_itm_container_id, itm_rec_itm_primary_key, itm_rec_itm_version_id, itm_rec_itm_next_version_id, itm_rec_itm_status, itm_rec_itm_collab_info;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL10;
		 		 		 END IF;


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null,CHAR('Next itm row = ') || CHAR(itm_rec_itm_id) || CHAR(', ') || RTRIM(CHAR(itm_rec_itm_collab_info)));
		 		 		 COMMIT;
		 		 		 


		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM   itm
		 		 		 WHERE  (itm_company_id  = itm_rec_itm_company_id
		 		 		  AND		 itm_id  = itm_rec_itm_id
		 		 		  AND		 itm_container_id  = itm_rec_itm_container_id
		 		 		  AND		 itm_primary_key  = itm_rec_itm_primary_key
		 		 		  AND		 itm_version_id  = itm_rec_itm_version_id
		 		 		  AND		 itm_next_version_id  = itm_rec_itm_next_version_id)
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  itm
		 		 		  VALUES 		 		 ( itm_rec_itm_company_id ,
		 		 		 		 		 itm_rec_itm_id ,
		 		 		 		 		 itm_rec_itm_container_id ,
		 		 		 		 		 itm_rec_itm_primary_key ,
		 		 		 		 		 itm_rec_itm_version_id ,
		 		 		 		 		 itm_rec_itm_next_version_id ,
		 		 		 		 		 itm_rec_itm_status ,
		 		 		 		 		 itm_rec_itm_collab_info,null)
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 END LOOP LOOP_LABEL10 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;
		 		 COMMIT;

		 END ;
END p1 @


CREATE PROCEDURE RESTORE_wee
        (
                clnCoaId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
p1: BEGIN

        DECLARE my_cv_OPEN_FLAG INTEGER DEFAULT 0;

        DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

        DECLARE SQLCODE INTEGER DEFAULT 0;
        BEGIN

 --             DECLARE wee_rec wee_clncoa%rowtype ;
                DECLARE wee_rec_wee_coaid BIGINT;
                DECLARE wee_rec_wee_event_id BIGINT;

                DECLARE wee_rec_wee_entry_id BIGINT;

                DECLARE wee_rec_wee_event_entry_status VARCHAR(50);



                DECLARE my_cv CURSOR WITH HOLD  FOR SELECT *
                FROM  wee_clncoa
                WHERE    coa_id  = clnCoaId
                ;

                DECLARE EXIT HANDLER FOR SQLEXCEPTION,   NOT FOUND
                BEGIN


                        INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId, 'Error in proc RESTORE_wee' );
                        COMMIT;



                END;


                INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'*** BEGIN RESTORE OF wee TABLE' );
                COMMIT;


                -- show some primary keys

                SET my_cv_OPEN_FLAG  = 1;

               OPEN my_cv ;
                LOOP_LABEL18:           LOOP
                        -- -- restore row
                        BEGIN
                        DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
                        SET my_cv_FOUND = 0;
                        FETCH FROM my_cv INTO wee_rec_wee_coaid,wee_rec_wee_event_id,wee_rec_wee_entry_id,wee_rec_wee_event_entry_status ;
                        END ;

                        IF  my_cv_FOUND  = 100 THEN
                                LEAVE LOOP_LABEL18;
                        END IF;


                        INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,CHAR('Next wee row = ') || RTRIM(CHAR(wee_rec_wee_entry_id )));
                        COMMIT;




                        BEGIN
                                DECLARE CONTINUE HANDLER FOR NOT FOUND
                                BEGIN
                                END;
                                DELETE FROM   wee
                        WHERE  (wee_event_id  = wee_rec_wee_event_id
                         AND    wee_entry_id  = wee_rec_wee_entry_id)
                        ;
                        END;



                        BEGIN
                                DECLARE CONTINUE HANDLER FOR NOT FOUND
                                BEGIN
                                END;
                                INSERT INTO  wee
                         VALUES                 ( wee_rec_wee_event_id ,
                                        wee_rec_wee_entry_id ,
                                        wee_rec_wee_event_entry_status )
                        ;
                        END;

                END LOOP LOOP_LABEL18 ;
                 SET my_cv_OPEN_FLAG = 0;
                CLOSE my_cv ;
                COMMIT;

        END ;
END p1 @


CREATE PROCEDURE RESTORE_all
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 11
LANGUAGE  SQL
		 p1: BEGIN

		 		 DECLARE clnCAName VARCHAR(100) ;

		 		 DECLARE clnSrcCtgId INTEGER ;

		 		 DECLARE clnCoaCtgId INTEGER ;

		 		 DECLARE retValue1 BIGINT;
		 		 DECLARE retValue2 VARCHAR(200);
		 		 DECLARE retValue3 BIGINT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,clnCoaId,'Error in proc RESTORE_all' );
		 		 COMMIT;
		 		 


		 		 END;


		 		 CALL GET_SRC_CTG_ID_FROM_CA_ID(clnCoaId,retValue1);
                SET clnSrcCtgId = retValue1;




                CALL GET_CA_NAME_FROM_ID(clnCoaId,retValue2);

		 		 SET clnCAName  = retValue2;




		 		 CALL GET_CA_CATALOG_ID_FROM_CA_ID(clnCoaId,retValue3);

		 		 SET clnCoaCtgId  = retValue3;



		 		 CALL RESTORE_wee(clnCoaId);

		 		 CALL RESTORE_ceh(clnCAName);

		 		 CALL RESTORE_cel(clnCoaId);

		 		 CALL RESTORE_cem(clnCoaId);

		 		 CALL RESTORE_cae(clnCoaId);

		 		 CALL RESTORE_itm(clnCoaCtgId);

		 		 CALL RESTORE_ita(clnCoaCtgId);

		 		 CALL RESTORE_icm(clnCoaCtgId);

		 		 CALL RESTORE_itmrsv(clnSrcCtgId);

		 END p1 @


CREATE PROCEDURE CLEAN_ITEMS_OF_PK_VIEW
		 (
		 		 View_Name VARCHAR(50) ,
		 		 Collab_Id INTEGER ,
		 		 src_catalog_id INTEGER ,
		 		 ca_catalog_id INTEGER )
  DYNAMIC RESULT SETS 9
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;

		 DECLARE rt_val1 BIGINT ;

		 DECLARE rt_val2 BIGINT ;
		 BEGIN


		 		 DECLARE view_rec_itm_primary_key VARCHAR(300) ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE src_item_id INTEGER ;

		 		 DECLARE ca_item_id INTEGER ;

		 		 DECLARE pk VARCHAR(300) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST1_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST1_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 
		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('Error in proc CLEAN_ITEMS_OF_PK_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 		 END;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('STARTING CLEAN_ITEMS_OF_PK_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

		 		 SET sql_stmt  = CHAR('SELECT * FROM ') || CHAR(LTRIM(RTRIM(View_Name)));


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST1_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL7:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_primary_key ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL7;
		 		 		 END IF;

		 		 		 SET pk  = view_rec_itm_primary_key;
		 		 		 CALL GET_ITEM_ID(src_catalog_id, pk, rt_val1);

		 		 		 SET src_item_id  = rt_val1;
		 		 		 CALL GET_ITEM_ID(ca_catalog_id, pk, rt_val2);

		 		 		 SET ca_item_id  = rt_val2;

		 		 		 CALL CLEAN_ITEM(Collab_Id, pk, src_item_id, ca_item_id);
		 		 END LOOP LOOP_LABEL7 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,Collab_Id, CHAR('FINISHED CLEAN_ITEMS_OF_PK_VIEW(') || RTRIM(CHAR(View_Name)) || CHAR(', ') || RTRIM(CHAR(Collab_Id)) || CHAR(', ') || RTRIM(CHAR(src_catalog_id)) || CHAR(', ') || RTRIM(CHAR(ca_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

  END ;
END p1  @



CREATE PROCEDURE CLEAN_AND_BACKUP_DATABASE_ROWS
		 (
		 		 clnCoaId INTEGER )
  DYNAMIC RESULT SETS 16
LANGUAGE  SQL
p1: BEGIN

		 DECLARE rt_val1 BIGINT ;

		 DECLARE rt_val2 BIGINT ;
		 BEGIN
		 		 -- The Views are assumed to exist and contain data for the desired collaboration area.

		 		 DECLARE clnSrcCtgId INTEGER ;

		 		 DECLARE clnCoaCtgId INTEGER ;

		 
		 		 CALL GET_SRC_CTG_ID_FROM_CA_ID(clnCoaId, rt_val1);

		 		 SET clnSrcCtgId  = rt_val1;
		 		 CALL GET_CA_CATALOG_ID_FROM_CA_ID(clnCoaId, rt_val2);

		 		 SET clnCoaCtgId  = rt_val2;

		 		 CALL DELETE_CA_FROM_BACKUP_TABLES(clnCoaId);

		 		 CALL CLEAN_ITEMS_OF_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', clnCoaId, clnSrcCtgId, clnCoaCtgId);

		 		 CALL CLEAN_CA_ITEMS_OF_IID_VIEW('TOTAL_BAD_CA_ITEM_ID', clnCoaId, clnCoaCtgId);
		 		 CALL CLEAN_SRC_ITEMS_OF_IID_VIEW('TOTAL_BAD_SRC_ITEM_ID', clnCoaId, clnSrcCtgId);
		 		 CALL CLEAN_CA_ITEMS_OF_IID_VIEW('ADDITIONAL_BAD_CA_ITEM_ID', clnCoaId, clnCoaCtgId);

		 		 --CALL CLEAN_SRC_ITEMS_OF_IID_VIEW('ADDITIONAL_BAD_SRC_ITEM_ID', clnCoaId, clnSrcCtgId);

		 		 COMMIT;

		 END ;
END p1 @
 


CREATE PROCEDURE REPORT_VIEWS
		 (
)
  DYNAMIC RESULT SETS 13
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- The Views are assumed to exist and contain data for the desired collaboration area.

--The comment "--BRIEF--" asserts that this view is being built and could be displayed, but is suppressed for brevity of output
--The comment "--BRIEF TOO BIG--" asserts that the same , but tries to say that we REALLY don't want to display this view
	-- BRIEF --	 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_CHECKED_OUT_PRIMARY_KEY', 'Primary keys of all source catalog items checked out to this collab area'); 
	-- BRIEF --	 		 CALL COUNT_SHOW_SOME_PK_VIEW('CA_ALL_PRIMARY_KEY', 'Primary keys of all items in the collaboration area catalog'); 
	--BRIEF TOO BIG --		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_ALL_PRIMARY_KEY', 'All primary keys of source catalog items'); 
		 	--	 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('ERROR_EVENT_ITEM_ID', 'Item ids of all collaboration area items in workflow events with ERROR status'); 
		-- 		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('NEW_EVENT_ITEM_ID', 'Item ids of all collaboration area items in workflow events with NEW status'); 
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('DONE_EVENT_ITEM_ID', 'Item ids of all collaboration area items in workflow events with DONE status'); 
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('PROCESSING_EVENT_ITEM_ID', 'Item ids of all collaboration area items in workflow events with PROCESSING status'); 
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('GOOD_EVENT_ITEM_ID', 'Item ids of all collaboration area and source items that are only in NEW or DONE workflow events'); 
		 		 -- CALL COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_IN_CEM', 'Primary keys of source items with corresponding CA items in CEM table'); 
	--BRIEF --	 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_IN_CEM_CAE', 'Primary keys of source items with corresponding CA items that are in both the CEM and CAE tables in a state of integrity and have only good events');
	-- BRIEF TOO BIG --		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_NOT_IN_CEM_CAE', 'All source catalog primary keys not in SRC_PRIMARY_KEY_IN_CEM_CAE'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_EVENT_ONLY', 'Primary keys of source items absent from CAE, CEM, or both, but in either a NEW or DONE event'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_OK_CEL', 'Primary keys of checked out source items which either have no rows, or only good rows, in CEL');
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_GOOD_PRIMARY_KEY', 'Primary keys of checked out source items which appear to be in a state of integrity');
	--BRIEF-- 		 CALL COUNT_SHOW_SOME_PK_VIEW('SRC_BAD_PRIMARY_KEY', 'Primary keys of checked out source items already found NOT in a state of integrity'); 
	--BRIEF--	 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY', 'Primary keys of collab area items NOT the pk of any (good or bad) checked-out source item');
	--BRIEF--	 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_IN_CEM_CAE', 'Primary keys of added CA items that are in both the CEM and CAE tables in a state of integrity and have only good events'); 
		-- 		 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_NOT_IN_CEM_CAE', 'Primary keys of added CA items that are not in ADD_PRIMARY_KEY_IN_CEM_CAE'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_EVENT_ONLY', 'Primary keys of added CA items that are NOT in CEM, CAE, but which have only good eventsXXX'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_OK_CEL', 'Primary keys of added CA items which either have no rows, or only good rows, in CEL');
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_GOOD_PRIMARY_KEY', 'Primary keys of added CA items which appear to be in a state of integrity');
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('ADD_BAD_PRIMARY_KEY', 'Primary keys of added CA items already found NOT in a state of integrity'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('IN_INITIAL_PRIMARY_KEY', 'Primary keys of CA items in the workflow Initial step'); 
	--	 		 CALL COUNT_SHOW_SOME_PK_VIEW('CA_ITM_NO_ITA_PRIMARY_KEY', 'Primary keys of ITM rows of CA catalog items with no corresponding ITA rows');
		 		 CALL COUNT_SHOW_SOME_PK_VIEW('CA_ITM_NO_ITD_PRIMARY_KEY', 'Primary keys of ITM rows of CA catalog items with no corresponding ITD rows');
		 		 
                 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CAE_ORPHAN_ITEM_ID', 'Item ids found in CAE with no corresponding ITM row');
		 		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CEM_SRC_ORPHAN_ITEM_ID', 'Source catalog item ids found in CEM with no corresponding ITM row');
		 		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CEM_CA_ORPHAN_ITEM_ID', 'CA catalog item ids found in CEM with no corresponding ITM row');
		 	--	 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CEL_CA_ORPHAN_ITEM_ID', 'CA catalog item ids found in CEL with no corresponding ITM row');
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('WEE_CA_ORPHAN_ITEM_ID', 'CA catalog item ids found in WEE with no corresponding ITM row');
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CA_ITA_NO_ITM', 'CA catalog ITA rows with no corresponding ITM row');
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('CA_ITM_NO_ITA', 'CA catalog ITM rows with no corresponding ITA rows');
		 		 CALL COUNT_SHOW_SOME_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', 'Complete list of primary keys found NOT to be in a state of integrity - true ghost items');
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('TOTAL_BAD_ITEM_EVTQ', 'CA Item ids having workflow events in ERROR or PROCESSING or with orphan WEE rows');
		 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('ADDITIONAL_BAD_CA_ITEM_ID', 'CA Item ids that are orphans, have bad events, or have ITA/ITM corruption');
		 		 -- CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('ADDITIONAL_BAD_SRC_ITEM_ID', 'Source items that are CEM orphans');
		 		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('TOTAL_BAD_CA_ITEM_ID', 'Complete list of CA Item ids NOT in a state of integrity - CA catalog true ghost item ids');
		 		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('TOTAL_BAD_SRC_ITEM_ID', 'Complete list of source Item ids NOT in a state of integrity - Source catalog true ghost items');
--	BRIEF --	 CALL COUNT_SHOW_SOME_PK_VIEW('TOTAL_GOOD_PRIMARY_KEY', 'Complete list of primary keys in a state of integrity');
--BRIEF	 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('TOTAL_GOOD_CA_ITEM_ID', 'Complete list of CA Item ids of CA Items in a state of integrity');
	--BRIEF	 --		 CALL COUNT_SHOW_SOME_ITEM_ID_VIEW('TOTAL_GOOD_SRC_ITEM_ID', 'Complete list of source Item ids in a state of integrity');

	--	 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null,CHAR('*** REPORT - FULL LIST OF PRIMARY KEYS OF GOOD ITEMS, WHOSE PRIMARY KEYS ARE KNOWN' ));
	--	 		 COMMIT;
		 		 


		 		 --CALL SHOW_ALL_PK_VIEW( 'TOTAL_GOOD_PRIMARY_KEY', 'All bad Items, by primary key.' );


	--	 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('*** REPORT - FULL LIST OF PRIMARY KEYS OF BAD ITEMS, WHOSE PRIMARY KEYS ARE KNOWN' ));
	--	 		 COMMIT;
		 		 

		 		-- CALL SHOW_ALL_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', 'All bad Items, by primary key.');


	--	 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('*** REPORT - FULL LIST OF ITEM IDS OF BAD COLLABORATION AREA ITEMS, SOME KNOWN ALSO BY PK, SOME KNOWN ONLY BY ITEM ID' ));
	--	 		 COMMIT;
		 		 


		 		 --CALL SHOW_ALL_IID_VIEW('TOTAL_BAD_CA_ITEM_ID', 'All bad CA Items, by item id.');


	--	 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('*** REPORT - FULL LIST OF ITEM IDS OF BAD SOURCE CATALOG ITEMS, SOME KNOWN ALSO BY PK, SOME KNOWN ONLY BY ITEM ID' ));
	--	 		 COMMIT;
		 		 


		 		 --CALL SHOW_ALL_IID_VIEW('TOTAL_BAD_SRC_ITEM_ID', 'All bad SRC Items, by item id.');


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('*** DONE WITH REPORTS' ));
		 		 COMMIT;
		 		 
		 END p1 @



CREATE PROCEDURE FULL_BACKUP_SRC_ITEM
		 (srcCtgItm INTEGER ,
		  primary_key VARCHAR(300) ,
		  src_item_id BIGINT )
  DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- Backup an item known in a collaboration area (i.e. not a source catalog item)
		 		 --
		 		 -- primary_key may be NULL, but ca_item_id should not be.
		 		 --

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 
		 		  INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null,CHAR('Error in proc FULL_BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		  COMMIT;
		 		  


		 		 END;
 		 		 
		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('STARTING FULL_BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 

		 		 -- DO SRC item
		 		 IF (  src_item_id >  0 ) THEN


 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('BACKUP SRC ITEM ID=') || RTRIM(CHAR(src_item_id)));
 		 		 COMMIT;
 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITMRSV_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  itm
		 		 		 WHERE		  itm_id  = src_item_id
                         and
                                      itm_next_version_id = 999999999
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITA_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  ita
		 		 		 WHERE		  ita_item_id  = src_item_id
                         and 
                                      ita_next_version_id = 999999999
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ITD_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  itd
		 		 		 WHERE		  itd_item_id  = src_item_id
                         and
                                      itd_next_version_id = 999999999
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 INSERT INTO  ICM_CLNCOA
		 		 		 (SELECT *
		 		 		 FROM  icm
		 		 		 WHERE		  icm_item_id  = src_item_id
                         and
                                      icm_next_version_id = 999999999
		 		 		 )
		 		 		 ;
		 		 		 END;
		 		 		 

		 		 		 
		 		 		 
		 		 END IF ;
		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('FINISHED FULL_BACKUP_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 END p1 @

CREATE PROCEDURE PURGE_SRC_ITEM
		 (
		 		 srcCtgItm INTEGER ,
		 		 primary_key VARCHAR(300) ,
		 		 src_item_id BIGINT )
DYNAMIC RESULT SETS 8
LANGUAGE  SQL
		 p1: BEGIN
		 		 -- CLEAN an item known to be in a source catalog (i.e. not a collaboration area item).
		 		 --
		 		 -- Passing a CA item by mistake should be benign.
		 		 --

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('Error in proc PURGE_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('STARTING PURGE_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		 CALL FULL_BACKUP_SRC_ITEM(srcCtgItm, primary_key, src_item_id);
		 		 -- Do source item first
		 		 IF (  src_item_id >  0 ) THEN


		 		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq,null, CHAR('CLEANING SOURCE ITEM ID=') || RTRIM(CHAR(src_item_id)));
		 		 		 COMMIT;
		 		 		 
		 		 		 
		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM  itm 
		 		 		 WHERE  itm_id  = src_item_id
                         and
                         itm_next_version_id = 999999999
		 		 		 ;
		 		 		 END;
		 		 		 
		 		 		 BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM  ita 
		 		 		 WHERE  ita_item_id  = src_item_id
                         and
                         ita_next_version_id = 999999999
		 		 		 ;
		 		 		 END;
		 		 		 
                         BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM  itd 
		 		 		 WHERE  itd_item_id  = src_item_id
                         and
                         itd_next_version_id = 999999999
		 		 		 ;
		 		 		 END;
                         
                         BEGIN
		 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND
		 		 		 		 BEGIN
		 		 		 		 END;
		 		 		 		 DELETE FROM  icm 
		 		 		 WHERE  icm_item_id  = src_item_id
                         and
                         icm_next_version_id = 999999999
		 		 		 ;
		 		 		 END;



		 		 END IF ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('FINISHED CLEAN_SRC_ITEM(') || RTRIM(CHAR(primary_key)) || CHAR(', ') || RTRIM(CHAR(src_item_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 END p1 @


CREATE PROCEDURE PURGE_SRC_ITEMS_FROM_CET
		 ( src_catalog_id INTEGER )
  DYNAMIC RESULT SETS 1
LANGUAGE  SQL
p1: BEGIN

		 DECLARE my_cv_FOUND INTEGER DEFAULT NULL;

		 DECLARE SQLCODE INTEGER DEFAULT 0;

		 DECLARE rt_val1 VARCHAR(300) ;
		 BEGIN


		 		 DECLARE view_rec_itm_id INTEGER ;

		 		 DECLARE sql_stmt VARCHAR(200) ;

		 		 DECLARE src_item_id INTEGER ;

		 		 DECLARE pk VARCHAR(300) ;

		 		 DECLARE my_cv_OPEN_FLAG INTEGER ;

		 		 DECLARE my_cv_ST1_STMT STATEMENT ;

		 		 DECLARE sql_stmt_STMT STATEMENT ;

		 		 DECLARE my_cv CURSOR WITH HOLD  WITH RETURN TO CALLER  FOR my_cv_ST1_STMT;

		 		 DECLARE EXIT HANDLER FOR SQLEXCEPTION,  NOT FOUND
		 		 BEGIN
		 		 

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('Error in proc PURGE _SRC_ITEMS_OF_IID_VIEW(') ||  RTRIM(CHAR(src_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 


		 		 END;

		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('STARTING PURGE_SRC_ITEMS_FROM_CET(') ||  RTRIM(CHAR(src_catalog_id)) || CHAR(') ...'));
		 		 COMMIT;
		 		 


		 		-- SET sql_stmt  = CHAR('SELECT * FROM cet_BADBLOB WHERE CET_SRC_CONTAINER_ID =') || LTRIM(CHAR(src_catalog_id));
		 		 SET sql_stmt  = 'SELECT cet_src_entry_id  FROM CET_BADBLOB WHERE CET_SRC_CONTAINER_ID =' || LTRIM(RTRIM(CHAR(src_catalog_id)));


		 		 SET my_cv_OPEN_FLAG  = 1;
		 		 PREPARE my_cv_ST1_STMT FROM sql_stmt ;
		 		 OPEN my_cv ;

		 		 LOOP_LABEL29:		 		 LOOP
		 		 		 BEGIN
 		 		 		 DECLARE CONTINUE HANDLER FOR NOT FOUND SET my_cv_FOUND = SQLCODE ;
		 		 		 SET my_cv_FOUND = 0;
		 		 		 FETCH FROM my_cv INTO view_rec_itm_id ;
		 		 		 END ;

		 		 		 IF  my_cv_FOUND  = 100 THEN
		 		 		 		 LEAVE LOOP_LABEL29;
		 		 		 END IF;

		 		 		 SET src_item_id  = view_rec_itm_id;
		 		 		 CALL GET_ITEM_PRIMARY_KEY(src_catalog_id, src_item_id, rt_val1);

		 		 		 SET pk  = rt_val1;

		 		 		 CALL PURGE_SRC_ITEM(src_catalog_id, pk, src_item_id);
		 		 END LOOP LOOP_LABEL29 ;
		 		  SET my_cv_OPEN_FLAG = 0;
 		 		 CLOSE my_cv ;


		 		 INSERT INTO MESSAGES (message_id , collab_area_id, message_text) VALUES(nextval for message_id_seq, null, CHAR('FINISHED PURGE_SRC_ITEMS_FROM_CET(') ||  RTRIM(CHAR(src_catalog_id)) || CHAR(')'));
		 		 COMMIT;
		 		 

		 END ;

		 END p1 @
