# {COPYRIGHT-TOP} 
# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
# {COPYRIGHT-END} 

#!/bin/bash


# Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# IBM makes no representations or warranties about the suitability of
# this program, either express or implied, including but not limited to
# the implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement.


#
#
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement.
#
#

# 
# basic_integrity_restore.sh
#  
# This file reports on existing bad data in db into STDOUT
# This file reports its progress into STDERR 
#  

${ECHO} "Last Modified - WPC 5.3.2 FP15 - 20090829" 
OUTPUT_FILE="${TOP}/logs/tmp_cmp_id.log"
. ${TOP}/bin/get_params.sh

if [ "${_CCD_CODE}" = "" ]; then
    ${ECHO} "Usage: $0 --code=<cmp code>"
    exit
fi

. ${TOP}/src/db/schema/util/set_db.sh --confirm_db=no

${TOUCH} tmpcmpid.sql

${ECHO} "Getting company id for the $_CCD_CODE"

${CAT} $DB_SCRIPTS/get_cmpid_for_cmpcode.sql | ${SED} "s/%_CCD_CODE%/$_CCD_CODE/g" > tmpcmpid.sql

CMP_ID_QRY=`. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=tmpcmpid.sql --errorfilter=yes | ${GREP} 'cmpid-'`

CMP_QRY_VAL="${CMP_ID_QRY}"
CMP_ID=`${ECHO} $CMP_QRY_VAL | ${GREP} 'cmpid-' | cut -f2 -d "-"`

if [ "$CMP_ID" != "" ]; then
#  ${ECHO} $CMP_ID_QRY
  ${ECHO} "company id found $CMP_ID"
  ${RM} tmpcmpid.sql
else 
  ${ECHO} "company id not found. Exiting..."
  exit 0
fi

. ${TOP}/bin/read_config_file.sh $TOP/etc/default/common.properties
${MKDIR} ${_CCD_CONF_TMP_DIR}
tmpfile=${_CCD_CONF_TMP_DIR}basic_integrity_restore.out
${RM} $tmpfile

TOP_SQL="
set sqlnumber off;
set heading off;
set pagesize 0;
set linesize 10000;
set echo off;
set show off;
set feedback on;
set trimspool on;
set term off;
"

#CheckDeadIta
#
# dead ita that refer to non existing items
#
CheckDeadIta() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile
    delete from tctg_ita_item_attributes where ita_next_version_id = 999999999 and ita_company_id = $CMP_ID and not exists (select itm_id from tctg_itm_item where itm_id = ita_item_id and itm_company_id = $CMP_ID and itm_next_version_id = 999999999);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
    if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid ita entries";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with test for ita rows refering to non existing items" 1>/dev/null 1>&2
}

#CheckDeadItd
#
# dead itd that refer to non existing items
#
CheckDeadItd() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile
    delete from tctg_itd_item_detail where itd_next_version_id = 999999999 and itd_company_id = $CMP_ID and not exists (select itm_id from tctg_itm_item where itm_id = itd_item_id and itm_company_id = $CMP_ID and itm_next_version_id = 999999999);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
    if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid itd entries";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with test for itd rows refering to non existing items" 1>/dev/null 1>&2
}

#CheckDeadIcm
#
# dead icm that refer to non existing items
#
CheckDeadIcm() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile
    delete from tctg_icm_item_category_map where icm_next_version_id = 999999999 and icm_company_id = $CMP_ID and not exists (select itm_id from tctg_itm_item where itm_id = icm_item_id and itm_company_id = $CMP_ID and itm_next_version_id = 999999999);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
    if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid icm entries";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with test for icm rows refering to non existing items" 1>/dev/null 1>&2
}


#CheckDeadItm
#
# dead itm that refer to non existing icm
# need to do per ctr
# LKP TABLES ARE EXCLUDED because of no migration!!!
#
CheckDeadItm() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    set serveroutput on size 1000000;
    $TOP_SQL
    spool $tmpfile;
declare
count_errs number;
ctg_name varchar2(3000);
ctg_id number;
ctg_company_id number;
ctr_id number;
ctr_name varchar2(3000);

cursor ctg_row is
       select * from tctg_ctg_catalog where ctg_type = 'CATALOG' and ctg_company_id = $CMP_ID ;

cursor caa_row(ctg_id number) is
       select * from tctg_caa_catalog_attributes, tctg_ctr_category_tree where caa_catalog_id = ctg_id and caa_name = 'CATEGORY_TREE_ID' and caa_next_version_id = 999999999 and ctr_id = caa_value and ctr_company_id = $CMP_ID and caa_company_id = $CMP_ID;

BEGIN
for ctg_record in ctg_row loop
    ctg_name := ctg_record.ctg_name;
    ctg_id := ctg_record.ctg_id;
    ctg_company_id := ctg_record.ctg_company_id;
    for caa_record in caa_row(ctg_id) loop
        ctr_id := caa_record.ctr_id;
        ctr_name := caa_record.ctr_name;

        --now do count of itm/icm to see if there are any missing rows
        select
        count(1) into count_errs
        from tctg_itm_item
        where itm_next_version_id = 999999999
        and itm_company_id = $CMP_ID
          and itm_container_id = ctg_id  -- ctg_id from what we select earlier
        and not exists (select
                        1
                        from tctg_icm_item_category_map
                        where icm_item_id = itm_id
                          and icm_catalog_id = ctg_id  --ctg_id from where we select earlier
                          and icm_cat_tree_id = ctr_id --ctr_id from what we selected earlier
                          and icm_company_id = $CMP_ID
                          and icm_next_version_id = 999999999);
                  
                                 
       if (count_errs > 0) then
       delete from tctg_itm_item
		        where itm_next_version_id = 999999999
		          and itm_company_id = $CMP_ID
		          and itm_container_id = ctg_id  -- ctg_id from what we select earlier
		        and not exists (select
		                        1
		                        from tctg_icm_item_category_map
		                        where icm_item_id = itm_id
		                        and icm_company_id = $CMP_ID 
		                          and icm_catalog_id = ctg_id  --ctg_id from where we select earlier
		                          and icm_cat_tree_id = ctr_id --ctr_id from what we selected earlier
                          and icm_next_version_id = 999999999);
        commit;
        dbms_output.put_line(count_errs || ' itms with no icms found and deleted for ctg_id = [' || ctg_id || '] ctg_name = [' || ctg_name || '] ctr_id = [' || ctr_id || '] ctr_name = [' || ctr_name || ']  !!!!!');
       end if;
    end loop;
end loop;
end;
/
    spool off;
EOF
    txt=`cat $tmpfile | grep -v 'SQL'`
    if [ "$txt" != "" ]; then
        ${ECHO} "$txt";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with check and delete of itm rows refering to non existing icms" 1>/dev/null 1>&2
}

#CheckCsmCat
#
# ROWS IN CSM NOT IN CAT
#

CheckCsmCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from csm where csm_category_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999
            and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id = $CMP_ID);
    commit; 
    spool off;
    
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
    if [ "$cnt" != "0 rows deleted." ]; then
     ${ECHO} "Removed [$cnt] invalid CSM entries not in CAT";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with test for CSMs not in CAT" 1>/dev/null 1>&2
}


#CheckCsaCat
#
# ROWS IN CSA NOT IN CAT
#

CheckCsaCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from csa where csa_category_id in 
            (select cfp_category_id 
            from cfp 
            where cfp_category_id  not in 
                (select distinct cat_id 
                from cat 
                where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
     commit;
     spool off;
             
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
       if [ "$cnt" != "0 rows deleted." ]; then
         ${ECHO} "Removed [$cnt] invalid CSA entries not in CAT";
       fi
       ${RM} $tmpfile
       ${ECHO} "done with test for CSAs not in CAT" 1>/dev/null 1>&2
}


#CheckChiParentCat
#
# PARENT ROWS IN CHI NOT IN CAT
#

CheckChiParentCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from chi where chi_parent_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
         if [ "$cnt" != "0 rows deleted." ]; then
           ${ECHO} "Removed [$cnt] invalid parent CHI entries not in CAT";
         fi
         ${RM} $tmpfile
         ${ECHO} "done with test for parent CHIs not in CAT" 1>/dev/null 1>&2
}


#CheckChiChildCat
#
# CHILD ROWS IN CHI NOT IN CAT

CheckChiChildCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from chi where chi_child_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
       if [ "$cnt" != "0 rows deleted." ]; then
         ${ECHO} "Removed [$cnt] invalid child CHI entries not in CAT";
       fi
       ${RM} $tmpfile
       ${ECHO} "done with test for child CHIs not in CAT" 1>/dev/null 1>&2
}


#CheckCcmSourceCat
#
# SOURCE ROWS IN CCM NOT IN CAT
#

CheckCcmSourceCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from ccm where ccm_src_category_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
      if [ "$cnt" != "0 rows deleted." ]; then
       ${ECHO} "Removed [$cnt] invalid source CCM entries not in CAT";
      fi
      ${RM} $tmpfile
      ${ECHO} "done with test for source CCMs not in CAT" 1>/dev/null 1>&2
}


#CheckCcmDestCat
#
# DESTINATION ROWS IN CCM NOT IN CAT
#

CheckCcmDestCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from ccm where ccm_dst_category_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
      if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid dest CCM entries not in CAT";
      fi
      ${RM} $tmpfile
      ${ECHO} "done with test for dest CCMs not in CAT" 1>/dev/null 1>&2
}


#CheckIcmCat
#
# ROWS IN ICM NOT IN CAT

CheckIcmCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from icm where icm_category_id in 
        (select cfp_category_id 
        from cfp 
        where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
      if [ "$cnt" != "0 rows deleted." ]; then
         ${ECHO} "Removed [$cnt] invalid ICM entries not in CAT";
      fi
      ${RM} $tmpfile
      ${ECHO} "done with test for ICMs not in CAT" 1>/dev/null 1>&2
    }

#CheckCfpCat
#
# ROWS IN CFP NOT IN CAT 

CheckCfpCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from cfp where cfp_category_id  not in 
            (select distinct cat_id 
            from cat 
            where cat_next_version_id = 999999999 and cat_company_id = $CMP_ID) 
        and cfp_next_version_id=999999999 and cfp_company_id=$CMP_ID;
    commit;
    spool off;
    
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
        if [ "$cnt" != "0 rows deleted." ]; then
            ${ECHO} "Removed [$cnt] invalid CFP entries not in CAT";
        fi
        ${RM} $tmpfile
        ${ECHO} "done with test for CFPs not in CAT" 1>/dev/null 1>&2
}


#CheckCsmCfp
#
# ROWS IN CSM NOT IN CFP
# 

CheckCsmCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from csm where csm_category_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;

EOF

cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
   if [ "$cnt" != "0 rows deleted." ]; then
   ${ECHO} "Removed [$cnt] invalid CSM entries not in CFP";
   fi
   ${RM} $tmpfile
   ${ECHO} "done with test for CSMs not in CFP" 1>/dev/null 1>&2
}



#CheckCsaCfp
#
# ROWS IN CSA NOT IN CFP
# 

CheckCsaCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from csa where csa_category_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
       if [ "$cnt" != "0 rows deleted." ]; then
       ${ECHO} "Removed [$cnt] invalid CSA entries not in CFP";
       fi
       ${RM} $tmpfile
       ${ECHO} "done with test for CSAs not in CFP" 1>/dev/null 1>&2
}


#CheckChiParentCfp
#
# PARENT ROWS IN CHI NOT IN CFP
# 

CheckChiParentCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from chi where chi_parent_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
       if [ "$cnt" != "0 rows deleted." ]; then
       ${ECHO} "Removed [$cnt] invalid parent CHI entries not in CFP";
       fi
       ${RM} $tmpfile
       ${ECHO} "done with test for parent CHIs not in CFP" 1>/dev/null 1>&2
}


#CheckChiChildCfp
#
# CHILD ROWS IN CHI NOT IN CFP
# 

CheckChiChildCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from chi where chi_child_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
           if [ "$cnt" != "0 rows deleted." ]; then
           ${ECHO} "Removed [$cnt] invalid child CHI entries not in CFP";
           fi
           ${RM} $tmpfile
           ${ECHO} "done with test for child CHIs not in CFP" 1>/dev/null 1>&2
    }



#CheckCcmSourceCfp
#
# SRC ROWS IN CCM NOT IN CFP
# 

CheckCcmSourceCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from ccm where ccm_src_category_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
    if [ "$cnt" != "0 rows deleted." ]; then
    ${ECHO} "Removed [$cnt] invalid source CCM entries not in CFP";
    fi
    ${RM} $tmpfile
    ${ECHO} "done with test for source CCMs not in CFP" 1>/dev/null 1>&2
 }
        



#CheckCcmDestCfp
#
# DEST ROWS IN CCM NOT IN CFP
# 

CheckCcmDestCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from ccm where ccm_dst_category_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
        if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid dest CCM entries not in CFP";
        fi
        ${RM} $tmpfile
        ${ECHO} "done with test for dest CCMs not in CFP" 1>/dev/null 1>&2
 }


#CheckIcmCfp
#
# ROWS IN ICM NOT IN CFP
# 

CheckIcmCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from icm where icm_category_id in 
        (select cat_id
        from cat
        where cat_id  not in 
            (select distinct cfp_category_id 
            from cfp 
            where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID);
    commit;
    spool off;
    
EOF
cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
        if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid ICM entries not in CFP";
        fi
        ${RM} $tmpfile
        ${ECHO} "done with test for ICMs not in CFP" 1>/dev/null 1>&2
 }
 

#CheckCatCfp
#
# ROWS IN CAT NOT IN CFP
# 

CheckCatCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    delete from cat
    where cat_id  not in 
        (select distinct cfp_category_id 
        from cfp 
        where cfp_next_version_id = 999999999 and cfp_company_id = $CMP_ID) 
        and cat_next_version_id=999999999 and cat_company_id=$CMP_ID;
    commit;
    spool off;
EOF
cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL' | ${GREP} -v "Commit complete" | ${GREP} -v "^$"`
        if [ "$cnt" != "0 rows deleted." ]; then
        ${ECHO} "Removed [$cnt] invalid CAT entries not in CFP";
        fi
        ${RM} $tmpfile
        ${ECHO} "*********This is the last test***************";
        ${ECHO} "done with test for CATs not in CFP" 1>/dev/null 1>&2
 }
 

#
# Main
#

CheckDeadIta
CheckDeadIcm
CheckDeadItm
CheckDeadItd
CheckCsmCat
CheckCsaCat
CheckChiParentCat
CheckChiChildCat
CheckCcmSourceCat
CheckCcmDestCat
CheckIcmCat
CheckCfpCat
CheckCsmCfp
CheckCsaCfp
CheckChiParentCfp
CheckChiChildCfp
CheckCcmSourceCfp
CheckCcmDestCfp
CheckIcmCfp
CheckCatCfp
