	/*  
	
	procs_category.sql -	PL/SQL Procedures used by ghost items detect and clean scripts, category workflows.

	Copyright 2005, IBM Corporation
	Version April 11, 2006

	*/
CREATE OR REPLACE PROCEDURE COUNT_SHOW_SOME_PK_VIEW(View_Name VARCHAR2, Description VARCHAR2, KEYS_TO_SHOW NUMBER)
IS
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_primary_key VARCHAR2(300)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
	rows_fetched NUMBER;
   	TYPE primary_key_tab_type is table of VARCHAR2(300) index by binary_integer;
	primary_key_tab primary_key_tab_type;
	-- KEYS_TO_SHOW NUMBER := 5;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** REPORT FOR VIEW ' || View_Name || ', Description = ' || Description);
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name)) || ' ORDER BY cat_PRIMARY_KEY';

	-- Report total count	
	OPEN my_cv FOR sql_stmt;
	FETCH my_cv BULK COLLECT INTO primary_key_tab;
	rows_fetched := my_cv%ROWCOUNT;
	DBMS_OUTPUT.PUT_LINE('Total number of primary keys in view = ' || rows_fetched);
	CLOSE my_cv;

	-- show some primary keys
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		IF (KEYS_TO_SHOW > 0) THEN EXIT WHEN my_cv%ROWCOUNT > KEYS_TO_SHOW; END IF;
		DBMS_OUTPUT.PUT_LINE('Next primary key in this view = ' || view_rec.cat_primary_key);
	END LOOP;
	CLOSE my_cv;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc COUNT_SHOW_SOME_PK_VIEW('||View_Name||', '||Description||')');
END;
/
CREATE OR REPLACE PROCEDURE COUNT_SHOW_SOME_CAT_ID_VIEW(View_Name VARCHAR2, Description VARCHAR2, KEYS_TO_SHOW NUMBER)
IS
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_id NUMBER(9)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
	rows_fetched NUMBER;
   	TYPE CAT_ID_tab_type is table of NUMBER(9) index by binary_integer;
	CAT_ID_tab CAT_ID_tab_type;
	-- KEYS_TO_SHOW NUMBER := 5;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** REPORT FOR VIEW ' || View_Name || ', Description = ' || Description);
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name));

	-- Report total count	
	OPEN my_cv FOR sql_stmt;
	FETCH my_cv BULK COLLECT INTO CAT_ID_tab;
	rows_fetched := my_cv%ROWCOUNT;
	DBMS_OUTPUT.PUT_LINE('Total number of category ids in view = ' || rows_fetched);
	CLOSE my_cv;

	-- show some category ids
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		IF (KEYS_TO_SHOW > 0) THEN EXIT WHEN my_cv%ROWCOUNT > KEYS_TO_SHOW; END IF;
		DBMS_OUTPUT.PUT_LINE('Next category id in this view = ' || view_rec.cat_id);
	END LOOP;
	CLOSE my_cv;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc COUNT_SHOW_SOME_CAT_ID_VIEW('||View_Name||', '||Description||')');
END;
/

CREATE OR REPLACE PROCEDURE SHOW_ALL_PK_VIEW(View_Name VARCHAR2, Description VARCHAR2)
IS
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_primary_key VARCHAR2(300)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
BEGIN
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name)) || ' ORDER BY cat_PRIMARY_KEY';

	-- show all primary keys
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE(view_rec.cat_primary_key);
	END LOOP;
	CLOSE my_cv;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc SHOW_ALL_PRIMARY_KEY_VIEW('||View_Name||', '||Description||')');
END;
/


CREATE OR REPLACE PROCEDURE SHOW_ALL_CID_VIEW(View_Name VARCHAR2, Description VARCHAR2)
IS
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_id NUMBER(9)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
BEGIN
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name));

	-- show all category ids
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE(view_rec.cat_id);
	END LOOP;
	CLOSE my_cv;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc SHOW_ALL_CID_VIEW('||View_Name||', '||Description||')');
END;
/


CREATE OR REPLACE FUNCTION GET_CAT_ID(ctr_id INTEGER, pk VARCHAR2) RETURN NUMBER IS
    out_id NUMBER := 0;
    TYPE cur_typ IS REF CURSOR;
    c cur_typ;
    query_str VARCHAR2(200);
BEGIN
    query_str := 'select distinct cat_id from cat where cat_primary_key = :pk and cat_container_id = :cid';
    OPEN c FOR query_str USING pk, ctr_id;
    LOOP
        FETCH c INTO out_id;
        EXIT WHEN c%NOTFOUND;
    END LOOP;
    CLOSE c;
   RETURN out_id;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_CAT_ID;
/


CREATE OR REPLACE FUNCTION GET_CATEGORY_PRIMARY_KEY(ctr_id INTEGER, CAT_ID NUMBER) RETURN VARCHAR2 IS
    out_pk VARCHAR2(300) := ' '; -- default empty pk
    TYPE cur_typ IS REF CURSOR;
    c cur_typ;
    query_str VARCHAR2(200);
BEGIN
    query_str := 'select distinct cat_primary_key from cat where cat_id = :CID and cat_container_id = :cid';
    OPEN c FOR query_str USING CAT_ID, ctr_id;
    LOOP
        FETCH c INTO out_pk;
        EXIT WHEN c%NOTFOUND;
    END LOOP;
    CLOSE c;
   RETURN out_pk;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_CATEGORY_PRIMARY_KEY;
/


CREATE OR REPLACE PROCEDURE BACKUP_CATEGORY_NEW(clnCoaId INTEGER, primary_key VARCHAR2, src_CAT_ID NUMBER, ca_CAT_ID NUMBER)
IS
-- Backup an category by pk, both from collaboration area and source catalog
-- category may be in only one or both.  Specified by positive-valued ids passed.
BEGIN
  -- DBMS_OUTPUT.PUT_LINE(' ');
  DBMS_OUTPUT.PUT_LINE('   STARTING BACKUP_category('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||') ...');

  -- Do source category first 
  IF (src_CAT_ID > 0) THEN
    DBMS_OUTPUT.PUT_LINE('     BACKUP SOURCE category ID='||src_CAT_ID);
    execute immediate 'insert into catRSV_CLNCOA (select * from cat where cat_id =' || src_CAT_ID || ')';
    execute immediate 'insert into CEM_CLNCOA  (select * from cem where cem_src_entry_id =' || src_CAT_ID || ')';
    execute immediate 'insert into CEH_CLNCOA (select * from ceh where ceh_entry_key =''' || primary_key||''')';
  END IF;

  -- DO CA category
  IF (ca_CAT_ID > 0) THEN
    DBMS_OUTPUT.PUT_LINE('     BACKUP CA category ID='||ca_CAT_ID);
    execute immediate 'insert into cat_CLNCOA (select * from cat where cat_id = ' || ca_CAT_ID || ')' ;
    execute immediate 'insert into cab_CLNCOA (select * from cab where cab_category_id = ' || ca_CAT_ID || ')' ;
    execute immediate 'insert into cfp_CLNCOA (select * from cfp where cfp_category_id = ' || ca_CAT_ID || ')' ;
    execute immediate 'insert into csa_CLNCOA (select * from csa where csa_category_id =' || ca_CAT_ID ||')';    
    execute immediate 'insert into chi_CLNCOA (select * from chi where chi_parent_id =' || ca_CAT_ID || ' or chi_child_id =' || ca_CAT_ID ||')';
   
    execute immediate 'insert into CEM_CLNCOA (select * from cem where cem_ca_entry_id = ' || ca_CAT_ID || ')' ;
    execute immediate 'insert into CAE_CLNCOA (select * from cae where cae_entry_id = ' || ca_CAT_ID || ')' ;
    execute immediate 'insert into CEH_CLNCOA (select * from ceh where ceh_entry_key =''' || primary_key ||''')';
    execute immediate 'insert into CEL_CLNCOA (select * from cel where cel_entry_id = ' || ca_CAT_ID || ')' ;
    -- 3.31.06 - why fails?
    --execute immediate 'insert into WEE_CLNCOA (select clnCoaId, wee_event_id, wee_entry_id, wee_event_entry_status from wee where wee_entry_id = ' ||ca_CAT_ID||')';
  END IF;
  DBMS_OUTPUT.PUT_LINE('   FINISHED BACKUP_category('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||')');
-- 
EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc BACKUP_CATEGORY_NEW('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||')');
END;
/

CREATE OR REPLACE PROCEDURE BACKUP_CA_CATEGORY(clnCoaId INTEGER, primary_key VARCHAR2, ca_CAT_ID NUMBER)
IS
-- Backup an category known in a collaboration area (i.e. not a source catalog category)
-- 
-- primary_key may be NULL, but ca_CAT_ID should not be.
--
BEGIN
	--DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('   STARTING BACKUP_CA_CATEGORY('||primary_key||', '||ca_CAT_ID||') ...');

	-- DO CA category
	IF (ca_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     BACKUP CA category ID='||ca_CAT_ID);
		
    execute immediate 'insert into cat_CLNCOA (select * from cat where cat_id =' || ca_CAT_ID || ')';
    execute immediate 'insert into cab_CLNCOA (select * from cab where cab_category_id =' || ca_CAT_ID ||')';
    execute immediate 'insert into cfp_CLNCOA (select * from cfp where cfp_category_id =' || ca_CAT_ID ||')';
    execute immediate 'insert into csa_CLNCOA (select * from csa where csa_category_id =' || ca_CAT_ID ||')';    
    execute immediate 'insert into chi_CLNCOA (select * from chi where chi_parent_id =' || ca_CAT_ID || ' or chi_child_id =' || ca_CAT_ID ||')';
    
    execute immediate 'insert into CEM_CLNCOA (select * from cem where cem_ca_entry_id =' || ca_CAT_ID || ')';
    execute immediate 'insert into CAE_CLNCOA (select * from cae where cae_entry_id =' || ca_CAT_ID || ')';
    execute immediate 'insert into CEH_CLNCOA (select * from ceh where ceh_entry_key =''' ||  primary_key || ''')';
    execute immediate 'insert into CEL_CLNCOA (select * from cel where cel_entry_id = ' || ca_CAT_ID || ')';
	-- 3.31.06 - why fails?
    --execute immediate 'insert into WEE_CLNCOA (select clnCoaId, wee_event_id, wee_entry_id, wee_event_entry_status from wee where wee_entry_id =' || ca_CAT_ID  ||')';
	END IF;

	DBMS_OUTPUT.PUT_LINE('   FINISHED BACKUP_CA_category('||primary_key||', '||ca_CAT_ID||')');

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc BACKUP_CA_CATEGORY('||primary_key||', '||ca_CAT_ID||')');
END;
/


CREATE OR REPLACE PROCEDURE BACKUP_SRC_CATEGORY(clnCoaId INTEGER, primary_key VARCHAR2, src_CAT_ID NUMBER)
IS
-- Backup an category known to be in a source catalog (i.e. not a collaboration area category).
-- 
-- primary_key may be NULL, but src_CAT_ID should not be.
--
BEGIN
	-- DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('   STARTING BACKUP_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||') ...');

	-- Do source category first 
	IF (src_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     BACKUP SOURCE category ID='||src_CAT_ID);
    execute immediate 'insert into catRSV_CLNCOA (select * from cat where cat_id = ' || src_CAT_ID || ' and cat_next_version_id=999999999)' ;
    execute immediate 'insert into CEM_CLNCOA  (select * from cem where cem_src_entry_id = ' || src_CAT_ID || ')' ;
    execute immediate 'insert into CEH_CLNCOA (select * from ceh where ceh_entry_key =''' || primary_key || ''')';
	END IF;

	DBMS_OUTPUT.PUT_LINE('   FINISHED BACKUP_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||')');

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc BACKUP_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||')');
END;
/


CREATE OR REPLACE PROCEDURE CLEAN_CATEGORY(clnCoaId INTEGER, primary_key VARCHAR2, src_CAT_ID NUMBER, ca_CAT_ID NUMBER)
IS
-- CLEAN an category by pk, both from collaboration area and source catalog
-- category may be in only one or both.  Specified by positive-valued ids passed.
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_CATEGORY('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||') ...');

        BACKUP_CATEGORY_NEW(clnCoaId, primary_key, src_CAT_ID, ca_CAT_ID);

	-- Do source category first 
	IF (src_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     CLEANING SOURCE category ID='||src_CAT_ID);
		update cat set cat_collab_info = ''  where cat_id = src_CAT_ID and cat_next_version_id=999999999;
		delete from cem where cem_src_entry_id = src_CAT_ID;
	END IF;

	-- DO CA category
	IF (ca_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     CLEANING CA category ID='||ca_CAT_ID);

		delete from chi where (chi_parent_id = ca_CAT_ID or chi_child_id = ca_CAT_ID);
		delete from csa where csa_category_id = ca_CAT_ID;
		delete from cab where cab_category_id = ca_CAT_ID;
		delete from cfp where cfp_category_id = ca_CAT_ID;
		delete from cem where cem_ca_entry_id = ca_CAT_ID;
		delete from cae where cae_entry_id = ca_CAT_ID;
		delete from ceh where ceh_entry_key = primary_key;
		delete from cel where cel_entry_id = ca_CAT_ID;
		delete from wee where wee_entry_id = ca_CAT_ID;
		delete from cat where cat_id = ca_CAT_ID;
	END IF;

	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_CATEGORY('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||')');

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_CATEGORY('||primary_key||', '||src_CAT_ID||', '||ca_CAT_ID||')');
END;
/
CREATE OR REPLACE PROCEDURE CLEAN_CA_CATEGORY(clnCoaId INTEGER, primary_key VARCHAR2, ca_CAT_ID NUMBER)
IS
-- CLEAN an category known in a collaboration area (i.e. not a source category tree category)
--
-- WARNING: be careful not to pass in the id of an category in a source category tree; it will be deleted !!!!!!!
--
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_CA_CATEGORY('||primary_key||', '||ca_CAT_ID||') ...');

        BACKUP_CA_CATEGORY(clnCoaId, primary_key, ca_CAT_ID);

	-- DO CA category
	IF (ca_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     CLEANING CA category ID='||ca_CAT_ID);
		
		delete from chi where (chi_parent_id = ca_CAT_ID or chi_child_id = ca_CAT_ID);
		delete from csa where csa_category_id = ca_CAT_ID;
		delete from cab where cab_category_id = ca_CAT_ID;
		delete from cfp where cfp_category_id = ca_CAT_ID;
		delete from cem where cem_ca_entry_id = ca_CAT_ID;
		delete from cae where cae_entry_id = ca_CAT_ID;
		delete from ceh where ceh_entry_key = primary_key;
		delete from cel where cel_entry_id = ca_CAT_ID;
		delete from wee where wee_entry_id = ca_CAT_ID;
		delete from cat where cat_id = ca_CAT_ID;
	END IF;

	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_CA_CATEGORY('||primary_key||', '||ca_CAT_ID||')');

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_CA_CATEGORY('||primary_key||', '||ca_CAT_ID||')');
END;
/
CREATE OR REPLACE PROCEDURE CLEAN_SRC_CATEGORY(clnCoaId INTEGER, primary_key VARCHAR2, src_CAT_ID NUMBER)
IS
-- CLEAN an category known to be in a source catalog (i.e. not a collaboration area category).
--
-- Passing a CA category by mistake should be benign.
--
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||') ...');

        BACKUP_SRC_CATEGORY(clnCoaId, primary_key, src_CAT_ID);

	-- Do source category first 
	IF (src_CAT_ID > 0) THEN
		DBMS_OUTPUT.PUT_LINE('     CLEANING SOURCE category ID='||src_CAT_ID);
		update cat set cat_collab_info = ''  where cat_id = src_CAT_ID and cat_next_version_id=999999999;
		delete from cem where cem_src_entry_id = src_CAT_ID;
	END IF;

	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||')');

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_SRC_CATEGORY('||primary_key||', '||src_CAT_ID||')');
END;
/
CREATE OR REPLACE PROCEDURE CLEAN_CATS_OF_PK_VIEW(View_Name VARCHAR2, Collab_Id INTEGER, src_ctr_id INTEGER, ca_ctr_id INTEGER)
IS
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_primary_key VARCHAR2(300)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
	src_CAT_ID number(9);
	ca_CAT_ID number(9);
	pk VARCHAR2(300);
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_CATS_OF_PK_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||', '||ca_ctr_id||') ...');
	DBMS_OUTPUT.PUT_LINE(' ');
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name));

	-- do all primary keys
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		pk := view_rec.cat_primary_key;
		src_CAT_ID := GET_CAT_ID(src_ctr_id, pk);
		ca_CAT_ID := GET_CAT_ID(ca_ctr_id, pk);
		CLEAN_CATEGORY(Collab_Id, pk, src_CAT_ID, ca_CAT_ID);
	END LOOP;
	CLOSE my_cv;
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_CATS_OF_PK_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||', '||ca_ctr_id||')');
	DBMS_OUTPUT.PUT_LINE(' ');
EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_CATS_OF_PK_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||', '||ca_ctr_id||')');
END;
/
CREATE OR REPLACE PROCEDURE CLEAN_CA_CATS_OF_CID_VIEW(View_Name VARCHAR2, Collab_Id INTEGER, ca_ctr_id INTEGER)
IS
--
-- WARNING: BE SURE View_Name is an CAT_ID_VIEW consisting of CA, not source, categorys !!!!!
--		Else you risk deleting source catalog categorys !!!!!
--
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_id NUMBER(9)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
	ca_CAT_ID NUMBER(9);
	pk VARCHAR2(300);
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_CA_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||ca_ctr_id||') ...');
	DBMS_OUTPUT.PUT_LINE(' ');
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name));

	-- do all category ids
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		ca_CAT_ID := view_rec.cat_id;
		pk := GET_CATEGORY_PRIMARY_KEY(ca_ctr_id, ca_CAT_ID);
		CLEAN_CA_CATEGORY(Collab_Id, pk, ca_CAT_ID);
	END LOOP;
	CLOSE my_cv;
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_CA_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||ca_ctr_id||')');
	DBMS_OUTPUT.PUT_LINE(' ');
EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_CA_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||ca_ctr_id||')');
END;
/
CREATE OR REPLACE PROCEDURE CLEAN_SRC_CATS_OF_CID_VIEW(View_Name VARCHAR2, Collab_Id INTEGER, src_ctr_id INTEGER)
IS
--
-- View_Name is an CAT_ID_VIEW consisting of source, not CA, categorys.
--
	TYPE MyCurTyp IS REF CURSOR;
	my_cv MyCurTyp;
	TYPE view_rec_type IS RECORD(cat_id NUMBER(9)); -- dependent on cat ddl
	view_rec view_rec_type;
	sql_stmt VARCHAR2(200);
	src_CAT_ID NUMBER(9);
	pk VARCHAR2(300);
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('STARTING CLEAN_SRC_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||') ...');
	DBMS_OUTPUT.PUT_LINE(' ');
	sql_stmt := 'SELECT * FROM ' || LTRIM(RTRIM(View_Name));

	-- do all category ids
	OPEN my_cv FOR sql_stmt;
	LOOP
		FETCH my_cv INTO view_rec;
		EXIT WHEN my_cv%NOTFOUND;
		src_CAT_ID := view_rec.cat_id;
		pk := GET_CATEGORY_PRIMARY_KEY(src_ctr_id, src_CAT_ID);
		CLEAN_SRC_CATEGORY(Collab_Id, pk, src_CAT_ID);
	END LOOP;
	CLOSE my_cv;
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*************************************************');
	DBMS_OUTPUT.PUT_LINE('FINISHED CLEAN_SRC_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||')');
	DBMS_OUTPUT.PUT_LINE(' ');
EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc CLEAN_SRC_CATS_OF_CID_VIEW('||View_Name||', '||Collab_Id||', '
		||src_ctr_id||')');
END;
/


CREATE OR REPLACE PROCEDURE RESTORE_catrsv(clnSrcCtrId INTEGER)
IS
 -- assumes source categorys only checked out to one collab area, because restores all categorys of a given source catalog.
	CURSOR my_cv is select * from catrsv_clncoa where cat_container_id = clnSrcCtrId;
	cat_rec catrsv_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF catrsv TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cat_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cat row = ' || cat_rec.cat_id||', '||cat_rec.cat_collab_info);
	
		-- select existing row if any
		delete from cat where (                                        
    cat_company_id = cat_rec.cat_company_id and
    cat_id = cat_rec.cat_id  and
    cat_container_id = cat_rec.cat_container_id  and
    cat_primary_key  = cat_rec.cat_primary_key  and
    cat_version_id = cat_rec.cat_version_id and
    cat_next_version_id = cat_rec.cat_next_version_id 
    -- and cat_status = cat_rec.status 
    -- and cat_collab_info  = cat_rec.cat_collab_info --- XXX varchar2(3000) compare?
		); 
		
		-- restore row
		insert into cat values (                                       
    cat_rec.cat_company_id,
    cat_rec.cat_id  ,
    cat_rec.cat_container_id  ,
    cat_rec.cat_primary_key  ,
    cat_rec.cat_version_id ,
    cat_rec.cat_next_version_id  ,
    cat_rec.cat_status  ,
    cat_rec.cat_collab_info
		);  

		
	END LOOP;


	CLOSE my_cv;
	commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_catrsv');
END;
/



CREATE OR REPLACE PROCEDURE RESTORE_chi(clnCoaCtrID INTEGER)
IS
	CURSOR my_cv is select * from chi_clncoa where chi_cat_tree_id = clnCoaCtrID;
	chi_rec chi_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF chi TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO chi_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next chi row = ' || chi_rec.chi_parent_id || ' ' || chi_rec.chi_child_id );
	
		-- delete existing row if any
		delete from chi where (                                        
    chi_company_id = chi_rec.chi_company_id and
    chi_cat_tree_id = chi_rec.chi_cat_tree_id  and
    chi_parent_id = chi_rec.chi_parent_id  and
    chi_child_id = chi_rec.chi_child_id  and
	chi_version_id  = chi_rec.chi_version_id  and
    chi_next_version_id = chi_rec.chi_next_version_id 
		); 
		
		-- restore row
		insert into chi values (                                       
    chi_rec.chi_company_id,
    chi_rec.chi_cat_tree_id  ,
    chi_rec.chi_parent_id  ,
    chi_rec.chi_child_id  ,
	chi_rec.chi_version_id,
    chi_rec.chi_next_version_id 
		); 
	END LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_chi');
END;
/

CREATE OR REPLACE PROCEDURE RESTORE_csa(clnCoaCtrID INTEGER)
IS
	CURSOR my_cv is select * from csa_clncoa where csa_cat_tree_id = clnCoaCtrID;
	csa_rec csa_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF csa TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO csa_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next csa row = ' || csa_rec.csa_CATegory_ID);
	
		-- delete existing row if any
		delete from csa where (                                        
    csa_company_id = csa_rec.csa_company_id and
    csa_cat_tree_id = csa_rec.csa_cat_tree_id  and
    csa_CATegory_ID = csa_rec.csa_CATegory_ID  and
    csa_name = csa_rec.csa_name  and
    csa_value = csa_rec.csa_value  and  
	csa_version_id  = csa_rec.csa_version_id  and
    csa_next_version_id = csa_rec.csa_next_version_id 
		); 
		
		-- restore row
		insert into csa values (                                       
    csa_rec.csa_company_id,
    csa_rec.csa_cat_tree_id  ,
    csa_rec.csa_CATegory_ID  ,
    csa_rec.csa_name  ,
	csa_rec.csa_value,
	csa_rec.csa_version_id,
    csa_rec.csa_next_version_id 
		); 
	END LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_csa');
END;
/

CREATE OR REPLACE PROCEDURE RESTORE_cat(clnCoaCtrID INTEGER)
IS
	CURSOR my_cv is select * from cat_clncoa where cat_container_id = clnCoaCtrID;
	cat_rec cat_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cat TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cat_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cat row = ' || cat_rec.cat_container_id||', '||cat_rec.cat_primary_key);
	
		-- delete existing row if any
		delete from cat where (                                        
    cat_company_id = cat_rec.cat_company_id and
    cat_id = cat_rec.cat_id  and
    cat_container_id = cat_rec.cat_container_id  and
    cat_primary_key  = cat_rec.cat_primary_key  and
    cat_version_id = cat_rec.cat_version_id and
    cat_next_version_id = cat_rec.cat_next_version_id 
    -- and cat_status = cat_rec.cat_status
    -- and cat_collab_info  = cat_rec.cat_collab_info --- XXX varchar2(3000) compare?
		); 
		
		-- restore row
		insert into cat values (                                       
    cat_rec.cat_company_id,
    cat_rec.cat_id  ,
    cat_rec.cat_container_id  ,
    cat_rec.cat_primary_key  ,
    cat_rec.cat_version_id ,
    cat_rec.cat_next_version_id  ,
    cat_rec.cat_status  ,
    cat_rec.cat_collab_info
		); 
		
	END LOOP;

	CLOSE my_cv;
	commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cat');
END;
/


CREATE OR REPLACE PROCEDURE RESTORE_cab(clnCoaCtrID INTEGER)
IS
	CURSOR my_cv is select * from cab_clncoa where cab_cat_tree_id = clnCoaCtrID;
	cab_rec cab_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cab TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cab_rec;
		EXIT WHEN my_cv%NOTFOUND;
		-- no confirm output line for ghost items; generates excess amount of output for Gap; 10/2/05 Dennis
        -- 4/6/2005 - turn in back on for ghost categories
		DBMS_OUTPUT.PUT_LINE('Next cab row = ' || cab_rec.cab_category_id||', '||cab_rec.cab_node_id);
	
		-- delete existing row if any
		delete from cab where (                                        
    cab_company_id = cab_rec.cab_company_id and
    cab_cat_tree_id = cab_rec.cab_cat_tree_id  and
    cab_category_id = cab_rec.cab_category_id  and
    cab_node_id  = cab_rec.cab_node_id  and
    -- cab_value_numeric = cab_rec.cab_value_numeric and
    -- cab_value_string = cab_rec.cab_value_string  and  
    -- cab_value_string_search = cab_rec.cab_value_string_search  and
    -- cab_value_date  = cab_rec.cab_value_date and  -- XXX date compare
    cab_version_id = cab_rec.cab_version_id  and
    cab_next_version_id  = cab_rec.cab_next_version_id  and
    cab_order = cab_rec.cab_order and
    cab_parent_order = cab_rec.cab_parent_order and  
    cab_row_order = cab_rec.cab_row_order and
    cab_mod_user_id  = cab_rec.cab_mod_user_id
    -- and cab_mod_date = cab_rec.cab_mod_date  -- XXX why date compare not work?
		); 
		
		-- restore row
		insert into cab values (                                       
    cab_rec.cab_company_id,
    cab_rec.cab_cat_tree_id  ,
    cab_rec.cab_category_id  ,
    cab_rec.cab_node_id  ,
    cab_rec.cab_value_numeric ,
    cab_rec.cab_value_string  ,
    cab_rec.cab_value_string_search  ,
    cab_rec.cab_value_date  ,
    cab_rec.cab_version_id ,
    cab_rec.cab_next_version_id,
    cab_rec.cab_order,
    cab_rec.cab_parent_order,  
    cab_rec.cab_row_order,
    cab_rec.cab_mod_user_id,
    cab_rec.cab_mod_date 
		); 

		
	END LOOP;


	CLOSE my_cv;
	commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cab');
END;
/




CREATE OR REPLACE PROCEDURE RESTORE_cfp(clnCoaCtrID INTEGER)
IS
	CURSOR my_cv is select * from cfp_clncoa where cfp_cat_tree_id = clnCoaCtrID;
	cfp_rec cfp_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cfp TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cfp_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cfp row = ' || cfp_rec.cfp_category_id);
	
		-- delete existing row if any
		delete from cfp where (                                        
    cfp_company_id = cfp_rec.cfp_company_id and
    cfp_cat_tree_id = cfp_rec.cfp_cat_tree_id  and
    cfp_CATegory_ID = cfp_rec.cfp_CATegory_ID  and
	cfp_version_id  = cfp_rec.cfp_version_id  and
    cfp_next_version_id = cfp_rec.cfp_next_version_id 
		); 
		
		-- restore row
		insert into cfp values (                                       
    cfp_rec.cfp_company_id,
    cfp_rec.cfp_cat_tree_id  ,
    cfp_rec.cfp_CATegory_ID  ,
    cfp_rec.cfp_full_path_type  ,
	cfp_rec.cfp_full_path,
	cfp_rec.cfp_version_id,
    cfp_rec.cfp_next_version_id 
		); 
	END LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cfp');
END;
/


CREATE OR REPLACE PROCEDURE RESTORE_cel(clnCoaId INTEGER)
IS
	CURSOR my_cv is select * from cel_clncoa where cel_container_id = clnCoaId;
		-- "container" id here is actually the CA id
	cel_rec cel_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cel TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cel_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cel row = ' || cel_rec.cel_entry_id);
	
		-- delete existing row if any
		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_time_acquired = cel_rec.cel_time_acquired  and      -- XXX Date compare not working?  
    -- cel_time_of_expiry = cel_rec.cel_time_of_expiry   and    -- XXX Date compare not working?       
    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 
		); 
		
		-- restore row
		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 LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cel');
END;
/


CREATE OR REPLACE PROCEDURE RESTORE_cae(clnCoaId INTEGER)
IS
	CURSOR my_cv is select * from cae_clncoa where cae_ca_id = clnCoaId;
	cae_rec cae_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cae TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cae_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cae row = ' || cae_rec.cae_step_path_id);
	
		-- delete existing row if any
		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_deadline = cae_rec.cae_deadline  and  -- dont use date XXX why 
    cae_data = cae_rec.cae_data  and
    cae_available  = cae_rec.cae_available  and
    cae_lock_id = cae_rec.cae_lock_id
		); 
		
		-- restore row
		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 LOOP;


	CLOSE my_cv;
	commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cae');
END;
/



CREATE OR REPLACE PROCEDURE RESTORE_cem(clnCoaId INTEGER)
IS
	CURSOR my_cv is select * from cem_clncoa where cem_ca_id = clnCoaId;
	cem_rec cem_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF cem TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO cem_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next cem row = ' || cem_rec.cem_ca_entry_id);
	
		-- delete existing row if any
		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 
		); 
		
		-- restore row
		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 LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_cem');
END;
/



CREATE OR REPLACE PROCEDURE RESTORE_ceh(clnCAName VARCHAR)
IS
	CURSOR my_cv is select * from ceh_clncoa where ceh_ca_name = clnCAName;
	ceh_rec ceh_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF ceh TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO ceh_rec;
		EXIT WHEN my_cv%NOTFOUND;
        -- no confirm output line for ghost items; generates excess amount of output for Gap; 10/2/05 Dennis
        -- 4/6/2005 - turn in back on for ghost categories
		DBMS_OUTPUT.PUT_LINE('Next ceh row = ' || ceh_rec.ceh_event||', '||ceh_rec.ceh_entry_key);
	
		-- delete existing row if any
		delete from ceh where (                                        
    ceh_cmp_id = ceh_rec.ceh_cmp_id and
    ceh_id = ceh_rec.ceh_id   -- dont need anything more
    --and ceh_date = ceh_rec.ceh_date  and   -- XXX date compare
    -- ceh_entry_key  = ceh_rec.ceh_entry_key  and
    -- ceh_ca_name = ceh_rec.ceh_ca_name and
    -- ceh_wfl_name = ceh_rec.ceh_wfl_name  and  
    -- ceh_user_name = ceh_rec.ceh_user_name  and
    -- ceh_step_path  = ceh_rec.ceh_step_path and
    -- ceh_event = ceh_rec.ceh_event  and
    -- ceh_data  = ceh_rec.ceh_data
		); 
		
		-- restore row
		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
		); 

		
	END LOOP;


	CLOSE my_cv;
	commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_ceh');
END;
/

CREATE OR REPLACE PROCEDURE RESTORE_wee(clnCoaId INTEGER)
IS
	CURSOR my_cv is select * from wee_clncoa where coa_id =  clnCoaId;
	wee_rec wee_clncoa%ROWTYPE;
BEGIN
	DBMS_OUTPUT.PUT_LINE(' ');
	DBMS_OUTPUT.PUT_LINE('*** BEGIN RESTORE OF wee TABLE');

	-- show some primary keys
	OPEN my_cv;
	LOOP
		FETCH my_cv INTO wee_rec;
		EXIT WHEN my_cv%NOTFOUND;
		DBMS_OUTPUT.PUT_LINE('Next wee row = ' || wee_rec.wee_entry_id);
	
		-- delete existing row if any
		delete from wee where (                                        
    wee_event_id = wee_rec.wee_event_id  and
    wee_entry_id = wee_rec.wee_entry_id
    -- and wee_event_entry_status = wee_rec.wee_event_entry_status -- XXX failing string compare
		); 
		
		-- restore row
		 insert into wee values (                                       
    wee_rec.wee_event_id,
    wee_rec.wee_entry_id,
    wee_rec.wee_event_entry_status
		); 
	END LOOP;
	CLOSE my_cv;
    commit;

EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_wee');
END;
/

CREATE OR REPLACE PROCEDURE RESTORE_all(clnCoaId INTEGER)
IS
	clnCAName varchar2(100);
	clnSrcCtrId number(9);
	clnCoaCtrID number(9);
BEGIN
  clnSrcCtrId := GET_SRC_CTR_ID_FROM_CA_ID(clnCoaId);
  clnCAName := GET_CA_NAME_FROM_ID(clnCoaId);
  clnCoaCtrID := GET_CA_CTR_ID_FROM_CA_ID(clnCoaId);

RESTORE_wee(clnCoaId);
RESTORE_ceh(clnCAName);
RESTORE_cel(clnCoaId);
RESTORE_cem(clnCoaId);
RESTORE_cae(clnCoaId);

RESTORE_chi(clnCoaCtrID);
RESTORE_csa(clnCoaCtrID); 
RESTORE_cab(clnCoaCtrID);
RESTORE_cfp(clnCoaCtrID); 
RESTORE_cat(clnCoaCtrID);
RESTORE_catrsv(clnSrcCtrId); -- assumes source categorys only checked out to one collab area


EXCEPTION
	WHEN OTHERS THEN
		DBMS_OUTPUT.PUT_LINE('Error in proc RESTORE_all');
END;
/


CREATE OR REPLACE FUNCTION GET_CTR_ID_FROM_NAME(ctg_name VARCHAR2) RETURN NUMBER IS
    out_id NUMBER := 0;
    TYPE cur_typ IS REF CURSOR;
    c cur_typ;
    query_str VARCHAR2(200);
BEGIN
    query_str := 'select distinct ctr_id from ctg where ctg_name = :name';
    OPEN c FOR query_str USING ctg_name;
    LOOP
        FETCH c INTO out_id;
        EXIT WHEN c%NOTFOUND;
    END LOOP;
    CLOSE c;
   RETURN out_id;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_CTR_ID_FROM_NAME;
/


CREATE OR REPLACE FUNCTION GET_COLLAB_ID_FROM_NAME(ca_name VARCHAR2) RETURN NUMBER IS
    out_id NUMBER := 0;
    TYPE cur_typ IS REF CURSOR;
    c cur_typ;
    query_str VARCHAR2(200);
BEGIN
    query_str := 'select distinct coa_id from coa where coa_name = :name';
    OPEN c FOR query_str USING ca_name;
    LOOP
        FETCH c INTO out_id;
        EXIT WHEN c%NOTFOUND;
    END LOOP;
    CLOSE c;
   RETURN out_id;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_COLLAB_ID_FROM_NAME;
/



CREATE OR REPLACE PROCEDURE REPORT_VIEWS(which_checks NUMBER, keys_to_show NUMBER)
IS
-- The Views are assumed to exist and contain data for the desired collaboration area.
BEGIN 
  DBMS_OUTPUT.enable(1000000);
  
-- 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 the same, but tries to say that we REALLY don't want to display this view.
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('SRC_CHECKED_OUT_PRIMARY_KEY', 'Primary keys of all source catalog categorys checked out to this CA', keys_to_show); 
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('CA_ALL_PRIMARY_KEY', 'Primary keys of all categorys in the CA catalog', keys_to_show); 
-- BRIEF TOO BIG  -- COUNT_SHOW_SOME_PK_VIEW('SRC_ALL_PRIMARY_KEY', 'All primary keys of source catalog categorys', keys_to_show); 
--COUNT_SHOW_SOME_CAT_ID_VIEW('ERROR_EVENT_CAT_ID', 'category ids of all collaboration area categorys in workflow events with ERROR status', keys_to_show); 
--COUNT_SHOW_SOME_CAT_ID_VIEW('NEW_EVENT_CAT_ID', 'category ids of all collaboration area categorys in workflow events with NEW status', keys_to_show); 
--COUNT_SHOW_SOME_CAT_ID_VIEW('DONE_EVENT_CAT_ID', 'category ids of all collaboration area categorys in workflow events with DONE status', keys_to_show); 
--COUNT_SHOW_SOME_CAT_ID_VIEW('PROCESSING_EVENT_CAT_ID', 'category ids of all collaboration area categorys in workflow events with PROCESSING status', keys_to_show); 
--COUNT_SHOW_SOME_CAT_ID_VIEW('GOOD_EVENT_CAT_ID', 'category ids of all collaboration area and source categorys that are only in NEW or DONE workflow events', keys_to_show); 
-- COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_IN_CEM', 'Primary keys of source categorys with corresponding CA categorys in CEM table', keys_to_show); 
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_IN_CEM_CAE', 'Primary keys of source categorys with corresponding CA categorys that are in both the CEM and CAE tables in a state of integrity', keys_to_show);
-- BRIEF TOO BIG  -- 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', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_EVENT_ONLY', 'Primary keys of source categorys absent from CAE, CEM, or both, but in either a NEW or DONE event', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('SRC_PRIMARY_KEY_OK_CEL', 'Primary keys of checked out source categorys which either have no rows, or only good rows, in CEL', keys_to_show);
--COUNT_SHOW_SOME_PK_VIEW('SRC_GOOD_PRIMARY_KEY', 'Primary keys of checked out source categorys which appear to be in a state of integrity', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('SRC_BAD_PRIMARY_KEY', 'Primary keys of checked out source categorys that are true ghost categorys', keys_to_show); 
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY', 'Primary keys of CA categorys NOT the pk of any (good or bad) checked-out source category', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_IN_CEM_CAE', 'Primary keys of added CA categorys that are in both the CEM and CAE tables in a state of integrity', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_NOT_IN_CEM_CAE', 'Primary keys of added CA categorys that are not in ADD_PRIMARY_KEY_IN_CEM_CAE', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_EVENT_ONLY', 'Primary keys of added CA categorys that are NOT in CEM, CAE, but which have only good eventsXXX', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('ADD_PRIMARY_KEY_OK_CEL', 'Primary keys of added CA categorys which either have no rows, or only good rows, in CEL', keys_to_show);
--COUNT_SHOW_SOME_PK_VIEW('ADD_GOOD_PRIMARY_KEY', 'Primary keys of added CA categorys which appear to be in a state of integrity', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('ADD_BAD_PRIMARY_KEY', 'Primary keys of added CA categorys that are true ghost categorys', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('IN_INITIAL_PRIMARY_KEY', 'Primary keys of CA categorys in the workflow Initial step', keys_to_show); 
--COUNT_SHOW_SOME_PK_VIEW('CA_cat_NO_cab_PRIMARY_KEY', 'Primary keys of cat rows of CA catalog categorys with no corresponding cab rows', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('CAE_ORPHAN_CAT_ID', 'CAE orphan category ids - CA catalog category ids found in CAE with no corresponding cat row', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('CEM_SRC_ORPHAN_CAT_ID', 'CEM source orphan category ids - Source catalog category ids found in CEM with no corresponding cat row', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('CEM_CA_ORPHAN_CAT_ID', 'CEM CA orphan category ids - CA catalog category ids found in CEM with no corresponding cat row', keys_to_show);
--COUNT_SHOW_SOME_CAT_ID_VIEW('CEL_CA_ORPHAN_CAT_ID', 'CA catalog category ids found in CEL with no corresponding cat row', keys_to_show);
--COUNT_SHOW_SOME_CAT_ID_VIEW('WEE_CA_ORPHAN_CAT_ID', 'CA catalog category ids found in WEE with no corresponding cat row', keys_to_show);
--COUNT_SHOW_SOME_CAT_ID_VIEW('CA_cab_NO_cat', 'CA catalog cab rows with no corresponding cat row', keys_to_show);
--COUNT_SHOW_SOME_CAT_ID_VIEW('CA_cat_NO_cab', 'CA catalog cat rows with no corresponding cab rows', keys_to_show);
--COUNT_SHOW_SOME_CAT_ID_VIEW('TOTAL_BAD_category_EVTQ', 'CA category ids having workflow events in ERROR or PROCESSING or with orphan WEE rows', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('ADDITIONAL_BAD_CA_CAT_ID', 'All CA catalog orphan category ids', keys_to_show);
-- COUNT_SHOW_SOME_CAT_ID_VIEW('ADDITIONAL_BAD_SRC_CAT_ID', 'Source categorys that are CEM orphans', keys_to_show);
DBMS_OUTPUT.Put_Line(' ');
DBMS_OUTPUT.Put_Line(' ');
DBMS_OUTPUT.Put_Line(' ');
--COUNT_SHOW_SOME_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', 'Complete list of primary keys of true ghost categorys', keys_to_show);
COUNT_SHOW_SOME_PK_VIEW('SRC_BAD_PRIMARY_KEY', 'Complete list of primary keys of true ghost categories', keys_to_show);
COUNT_SHOW_SOME_CAT_ID_VIEW('TOTAL_BAD_CA_CATEGORY_ID', 'Complete list of CA Catalog true ghost category ids', keys_to_show);
COUNT_SHOW_SOME_CAT_ID_VIEW('TOTAL_BAD_SRC_CATEGORY_ID', 'Complete list of Source catalog true ghost category ids', keys_to_show);
DBMS_OUTPUT.Put_Line(' ');
DBMS_OUTPUT.Put_Line(' ');
DBMS_OUTPUT.Put_Line(' ');
-- BRIEF --COUNT_SHOW_SOME_PK_VIEW('TOTAL_GOOD_PRIMARY_KEY', 'Complete list of primary keys of good CA categorys, i.e. non-ghost categorys', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('TOTAL_GOOD_CA_CAT_ID', 'Complete list of CA category ids of good CA categorys, i.e. non-ghost categorys', keys_to_show);
-- BRIEF --COUNT_SHOW_SOME_CAT_ID_VIEW('TOTAL_GOOD_SRC_CAT_ID', 'Complete list of source category ids of good CA categorys, i.e. non-ghost categorys', keys_to_show);


  --DBMS_OUTPUT.Put_Line(' ');
  --DBMS_OUTPUT.Put_Line('*** REPORT - FULL LIST OF PRIMARY KEYS OF GOOD categoryS, WHOSE PRIMARY KEYS ARE KNOWN');
  --SHOW_ALL_PK_VIEW( 'TOTAL_GOOD_PRIMARY_KEY', 'All bad categorys, by primary key.' );
  --DBMS_OUTPUT.Put_Line(' ');
  --DBMS_OUTPUT.Put_Line('*** REPORT - FULL LIST OF PRIMARY KEYS OF BAD categoryS, WHOSE PRIMARY KEYS ARE KNOWN');
  --SHOW_ALL_PK_VIEW( 'TOTAL_BAD_PRIMARY_KEY', 'All bad categorys, by primary key.' );
  --DBMS_OUTPUT.Put_Line(' ');
  --DBMS_OUTPUT.Put_Line('*** REPORT - FULL LIST OF category IDS OF BAD COLLABORATION AREA categoryS, SOME KNOWN ALSO BY PK, SOME KNOWN ONLY BY category ID');
  --SHOW_ALL_CID_VIEW( 'TOTAL_BAD_CA_CATEGORY_ID', 'All bad CA categorys, by category id.' );
  --DBMS_OUTPUT.Put_Line(' ');
  --DBMS_OUTPUT.Put_Line('*** REPORT - FULL LIST OF category IDS OF BAD SOURCE CATALOG categoryS, SOME KNOWN ALSO BY PK, SOME KNOWN ONLY BY category ID');
  --SHOW_ALL_CID_VIEW( 'TOTAL_BAD_SRC_CATEGORY_ID', 'All bad SRC categorys, by category id.' );
  --DBMS_OUTPUT.Put_Line(' ');
  --DBMS_OUTPUT.Put_Line('*** DONE WITH REPORTS');
  --COMMIT; 
END; 
/

CREATE OR REPLACE FUNCTION GET_CA_CTR_ID_FROM_CA_ID(clnCoaId INTEGER) RETURN NUMBER IS
	TYPE MyCurTyp IS REF CURSOR;
	clnCoaCtrID number(9);
        c MyCurTyp;
BEGIN
  OPEN c FOR 'select COA_DST_CONTAINER_ID from coa where coa_id = ' || clnCoaId; FETCH c INTO clnCoaCtrID;
    CLOSE c;
   RETURN clnCoaCtrID;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_CA_CTR_ID_FROM_CA_ID;
/

CREATE OR REPLACE FUNCTION GET_SRC_CTR_ID_FROM_CA_ID(clnCoaId INTEGER) RETURN NUMBER IS
	TYPE MyCurTyp IS REF CURSOR;
	clnSrcCtrId number(9);
        c MyCurTyp;
BEGIN
  OPEN c FOR 'select COA_SRC_CONTAINER_ID from coa where coa_id = ' || clnCoaId; FETCH c INTO clnSrcCtrId;
    CLOSE c;
   RETURN clnSrcCtrId;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_SRC_CTR_ID_FROM_CA_ID;
/


CREATE OR REPLACE FUNCTION GET_CA_NAME_FROM_ID(clnCoaId INTEGER) RETURN varchar2 IS
	TYPE MyCurTyp IS REF CURSOR;
	clnCAName varchar2(100);
        c MyCurTyp;
BEGIN
    OPEN c FOR 'select COA_NAME from coa where coa_id = ' || clnCoaId; FETCH c INTO clnCAName;
    CLOSE c;
   RETURN clnCAName;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       NULL;
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END GET_CA_NAME_FROM_ID;
/


CREATE OR REPLACE PROCEDURE DELETE_CA_FROM_BACKUP_TABLES(clnCoaId INTEGER)
IS
-- The Views are assumed to exist and contain data for the desired collaboration area.
	clnCAName varchar2(100);
	clnSrcCtrId number(9);
	clnCoaCtrID number(9);
BEGIN
  clnSrcCtrId := GET_SRC_CTR_ID_FROM_CA_ID(clnCoaId);
  clnCAName := GET_CA_NAME_FROM_ID(clnCoaId);
  clnCoaCtrID := GET_CA_CTR_ID_FROM_CA_ID(clnCoaId);

  DBMS_OUTPUT.enable(1000000);

execute immediate 'delete catRSV_CLNCOA where cat_container_id =' || clnSrcCtrId;
execute immediate 'delete cat_CLNCOA where cat_container_id =' || clnCoaCtrID;
execute immediate 'delete cab_CLNCOA where cab_cat_tree_id = ' ||clnCoaCtrID;
execute immediate 'delete cfp_CLNCOA where cfp_cat_tree_id =' || clnCoaCtrID;
execute immediate 'delete csa_CLNCOA where csa_cat_tree_id = ' ||clnCoaCtrID;
execute immediate 'delete chi_CLNCOA where chi_cat_tree_id =' || clnCoaCtrID;
execute immediate 'delete CEM_CLNCOA where cem_ca_id = ' ||clnCoaId;
execute immediate 'delete CAE_CLNCOA where cae_ca_id = ' || clnCoaId;
execute immediate 'delete CEH_CLNCOA where ceh_ca_name = ''' || clnCAName || '''';
execute immediate 'delete CEL_CLNCOA where cel_container_id =' ||  clnCoaId;
execute immediate 'delete WEE_CLNCOA where coa_id =' || clnCoaId;
  
COMMIT; 

END; 
/


CREATE OR REPLACE PROCEDURE CLEAN_AND_BACKUP_DATABASE_ROWS(clnCoaId INTEGER)
IS
-- The Views are assumed to exist and contain data for the desired collaboration area.
	clnSrcCtrId number(9);
	clnCoaCtrID number(9);
BEGIN
  clnSrcCtrId := GET_SRC_CTR_ID_FROM_CA_ID(clnCoaId);
  clnCoaCtrID := GET_CA_CTR_ID_FROM_CA_ID(clnCoaId);

  DBMS_OUTPUT.enable(1000000);

-- Remove pre-existing backup rows for this collab area, if any
DELETE_CA_FROM_BACKUP_TABLES(clnCoaId);


-- Now clean and backup

  --CLEAN_CATS_OF_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', clnCoaId, clnSrcCtrId, clnCoaCtrID);
  CLEAN_CATS_OF_PK_VIEW('SRC_BAD_PRIMARY_KEY', clnCoaId, clnSrcCtrId, clnCoaCtrID);
  CLEAN_CA_CATS_OF_CID_VIEW('TOTAL_BAD_CA_CATEGORY_ID', clnCoaId, clnCoaCtrID);    -- note:  COLLAB AREA CTR ID ONLY
  
  
  -- 3/31/2006 - seems now redundant, produces duplicate rows in catrsv_clncoa backup table
  -- CLEAN_SRC_CATS_OF_CID_VIEW('TOTAL_BAD_SRC_CAT_ID', clnCoaId, clnSrcCtrId); -- note:  SOURCE CTR ID ONLY


--  Old way

-- 4/6/06 - *** CHECK THAT YOU HAVE ALL THE BELOW VIEWNAMES SPELLED RIGHT IF YOU EVER ENABLE ANY OF THE BELOW LINES
--CLEAN_CATS_OF_PK_VIEW('TOTAL_BAD_PRIMARY_KEY', clnCoaId, clnSrcCtrId, clnCoaCtrID);

--CLEAN_CA_CATS_OF_CID_VIEW('ADDITIONAL_BAD_CA_CAT_ID', clnCoaId, clnCoaCtrID);    -- note:  COLLAB AREA CATALOG ID ONLY

---- temporarily use just CEM_SRC_ORPHAN_CAT_ID in place of ADDITIONAL_BAD_SRC_CAT_ID (see create_views.sql)
---- CLEAN_SRC_CATS_OF_CID_VIEW('ADDITIONAL_BAD_SRC_CAT_ID', clnCoaId, clnSrcCtrId); -- note:  SOURCE CATALOG ID ONLY
--CLEAN_SRC_CATS_OF_CID_VIEW('CEM_SRC_ORPHAN_CAT_ID', clnCoaId, clnSrcCtrId); -- note:  SOURCE CATALOG ID ONLY


  DBMS_OUTPUT.Put_Line(' ');
  DBMS_OUTPUT.Put_Line('*********** DONE WITH CLEAN ************************');

  COMMIT; 
END; 
/


