#!/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.
#
#

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

# get src connection descriptor
${ECHO} "Last Modified - WPC 5.3.2 FP10 - 20080606" 
#cmpcode Begin 
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
#Getting company id for TRIGO CODE

${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}/src/db/schema/util/set_db.sh
. ${TOP}/bin/read_config_file.sh $TOP/etc/default/common.properties
${MKDIR} ${_CCD_CONF_TMP_DIR}
tmpfile=${_CCD_CONF_TMP_DIR}basic_integrity_verify.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 off;
set trimspool on;
set term off;
"

#CheckDupChi
#
# some bugs with duplicate chi rows
#
CheckDupChi() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_chi_category_hierarchy
    where chi_next_version_id = 999999999 and chi_company_id=$CMP_ID
    group by  chi_parent_id, chi_child_id
    having count(1) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] chi rows that need deletion!!!!";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for dupl chi entries" 1>/dev/null 1>&2
}

#CheckDupCat
#
# some bugs with duplicate cat rows
#
CheckDupCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_cat_category
    where cat_next_version_id = 999999999 and cat_company_id=$CMP_ID
    group by cat_id
    having count(1) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] cat rows that need deletion!!!!";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for dupl cat entries" 1>/dev/null 1>&2
}


#CheckDupItm
#
# some bugs with duplicate itm rows
#
CheckDupItm() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_itm_item
    where itm_next_version_id = 999999999 and itm_company_id=$CMP_ID
    group by  itm_container_id, itm_id
    having count(1) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] itm rows that need deletion!!!!";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for dupl itm entries" 1>/dev/null 1>&2
}


#CheckDupIcm
#
# some bugs with duplicate icm rows
#
CheckDupIcm() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_icm_item_category_map
    where icm_next_version_id = 999999999 and icm_company_id=$CMP_ID
    group by  icm_catalog_id, icm_cat_tree_id, icm_item_id, icm_category_id
    having count(1) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] icm rows that need deletion!!!!";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for dupl icm entries" 1>/dev/null 1>&2
}


#CheckDeadIta
#
# dead ita that refer to non existing items
#
CheckDeadIta() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    from tctg_ita_item_attributes
    where ita_next_version_id = 999999999
     and ita_company_id=$CMP_ID
      and not exists (select
                      1
                      from itm
                      where itm_id = ita_item_id
                      and itm_company_id = $CMP_ID
                      and itm_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] ita rows that refer to non existing items";
    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;
    select
    count(1)
    from tctg_itd_item_detail
    where itd_next_version_id = 999999999
    and itd_company_id=$CMP_ID  
    and not exists (select
                      1
                      from itm
                      where itm_id = itd_item_id
                      and itm_company_id = $CMP_ID
                      and itm_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] itd rows that refer to non existing items";
    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;
    select
    count(1)
    from tctg_icm_item_category_map
    where icm_next_version_id = 999999999
    and icm_company_id=$CMP_ID
      and not exists (select
                      1
                      from tctg_itm_item
                      where itm_id = icm_item_id
                      and itm_company_id = $CMP_ID
                        and itm_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] icm rows that refer to non existing items";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for icm rows refering to non existing items" 1>/dev/null 1>&2
}

#CheckDeadItmNoItd
#
# dead itm that refer to non existing item detail
#
CheckDeadItmNoItd() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    from tctg_itm_item
    where itm_next_version_id = 999999999
    and itm_company_id=$CMP_ID
      and not exists (select
                      1
                      from tctg_itd_item_detail
                      where itm_id = itd_item_id
                      and itd_company_id = $CMP_ID
                        and itd_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] itm rows that refer to non existing item detail";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for itm rows refering to non existing item detail" 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
        begin
                dbms_output.put_line(count_errs || ' itms with no icms found for ctg_id = [' || ctg_id || '] ctg_name = [' || ctg_name || '] ctr_id = [' || ctr_id || '] ctr_name = [' || ctr_name || ']  !!!!!');
        end;
        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 test for itm rows refering to non existing icms" 1>/dev/null 1>&2
}

#CheckDeadCatNoCad
#
# dead itm that refer to non existing item detail
#
CheckDeadCatNoCad() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    from cat
    where cat_next_version_id = 999999999
    and cat_company_id = $CMP_ID
      and not exists (select
                      1
                      from cad
                      where cat_id = cad_category_id
                      and cad_company_id = $CMP_ID
                        and cad_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] cat rows that refer to non existing category detail";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with test for cat rows refering to non existing category detail" 1>/dev/null 1>&2
}

#CheckDuplicateNodeOrder
#
#  Two nodes in a given spec should NOT have the same order
#

CheckDuplicateNodeOrder() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
      nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_noh_node_hierarchy
    where noh_next_version_id = 999999999 and noh_company_id=$CMP_ID
    group by noh_spec_id, noh_order
    having count(*) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] noh rows with duplicate orders.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for multiple Nodes with same order" 1>/dev/null 1>&2
}


#CheckNodeAttributes
#
#  NOA table should NOT have more than one entry of the folling for a given Node.
#  The code depends on getting a single row back.
#        case SchemaFacetEnum.CATEGORY_DELIMITER:
#

CheckNodeAttributes() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    nvl(sum(count(1)), 0) || ' - ' || count(1)
    from tctg_noa_node_attributes
    where noa_next_version_id = 999999999 and noa_company_id=$CMP_ID
    and noa_name in ('DATE_FORMAT', 'DEFAULT_VALUE', 'LOOKUP_TABLE', 'MINLENGTH', 'PATTERN', 'SEQUENCE_INCREMENT', 'SEQUENCE_START', 'STRING_ENUMERATION_RULE', 'VALIDATION_RULE', 'VALUE_RULE', 'MIN_OCCURRENCE', 'MAX_OCCURRENCE', 'TYPE', 'MAXLENGTH','HIDDEN','UNIQUE','NON_PERSISTENT')
    group by noa_spec_id, noa_node_id, noa_name
    having count(*) > 1;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] noa rows for Single Valued Attributes.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for multiple Node Attributes" 1>/dev/null 1>&2
}



#CheckNodeHierarchy
#
#  All nodes in the NOH table should have the entry in the NOD table.
#

CheckNodeHierarchy() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    from tctg_noh_node_hierarchy
    where noh_next_version_id = 999999999 and noh_company_id=$CMP_ID
    and not exists
    (select 1
      from nod
      where noh_child_node_id = nod_id
      and nod_company_id = $CMP_ID
            and nod_next_version_id = 999999999);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] noh rows for a spec which are not in NOD.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for bad NOH data" 1>/dev/null 1>&2
}


#CheckCfpCat
#
# ROWS IN CFP NOT IN CAT 

CheckCfpCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    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;
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CFP rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CFP data not in CAT" 1>/dev/null 1>&2
}


#CheckCsmCat
#
# ROWS IN CSM NOT IN CAT
#

CheckCsmCat() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    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);
    spool off;
    
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CSM rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CSM data 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;
    select
    count(1)
    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);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CSA rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CSA data 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;
    select
    count(1)
    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);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CHI parent rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CHI parent data 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;
    select
    count(1)
    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);
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CHI child rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CHI child data 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;
    select
    count(1)
    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);

    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CCM source rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CCM source data 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;
    select
    count(1)
    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);
        
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CCM destination rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CCM destination data 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;
    select
    count(1)
    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);

    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] ICM rows which are not in CAT.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for ICM data not in CAT" 1>/dev/null 1>&2
}


#CheckCatCfp
#
# ROWS IN CAT NOT IN CFP
# 

CheckCatCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    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;
    
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CAT rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CAT data not in CFP" 1>/dev/null 1>&2
}


#CheckCsmCfp
#
# ROWS IN CSM NOT IN CFP
# 

CheckCsmCfp() {
    ${CCD_SQLPLUS} ${CONNECTION} <<EOF >& /dev/null
    $TOP_SQL
    spool $tmpfile;
    select
    count(1)
    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);
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CSM rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CSM data 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;
    select
    count(1)
    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);
    
    spool off;
EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CSA rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CSA data 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;
    select
    count(1)
    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);
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CHI Parent rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CHI Parent data 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;
    select
    count(1)
    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);
        
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CHI Child rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CHI Child data 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;
    select
    count(1)
    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);
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CCM Source rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CCM Source data 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;
    select
    count(1)
    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);

spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] CCM Destination rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "Done with the test for CCM Destination data 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;
    select
    count(1)
    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);
            
spool off;

EOF
    cnt=`${CAT} $tmpfile | ${GREP} -v 'SQL'| ${BC}`
    if [ "$cnt" != "0" ]; then
        ${ECHO} "Found [$cnt] ICM rows which are not in CFP.";
    fi
    ${RM} $tmpfile
    ${ECHO} "*********This is the last test***************";
    ${ECHO} "Done with the test for ICM data not in CFP" 1>/dev/null 1>&2
}

#
# Main
#

CheckDupChi
CheckDupCat
CheckDupItm
CheckDupIcm
CheckDeadIta
CheckDeadIcm
CheckDeadItmNoItd
CheckDeadItm
CheckDeadItd
CheckDeadCatNoCad
CheckDuplicateNodeOrder
CheckNodeAttributes
CheckNodeHierarchy
CheckCfpCat
CheckCsmCat
CheckCsaCat
CheckChiParentCat
CheckChiChildCat
CheckCcmSourceCat
CheckCcmDestCat
CheckIcmCat
CheckCatCfp
CheckCsmCfp
CheckCsaCfp
CheckChiParentCfp
CheckChiChildCfp
CheckCcmSourceCfp
CheckCcmDestCfp
CheckIcmCfp
