# {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.
#
#

#
# 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 FP13 - 20090331" 
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 CMP 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}/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
sqlfile=${_CCD_CONF_TMP_DIR}restore.sql

echo "
--CheckDeadItm
    delete from itm 
    where itm_next_version_id = 999999999 
    and itm_container_id in 
    (select ctg_id from ctg where ctg_type = 'CATALOG' and ctg_company_id = $CMP_ID)
    and itm_company_id = $CMP_ID
    and not exists 
        (select 1 from icm 
        where icm_item_id = itm_id
        and icm_company_id = $CMP_ID 
        and icm_next_version_id = 999999999 ); 

--CheckDeadIta
delete from tctg_ita_item_attributes
where ita_next_version_id = 999999999 and ita_company_id = $CMP_ID
and not exists (select 1 from tctg_itm_item where itm_id = ita_item_id and itm_next_version_id = 999999999);

commit;

--CheckDeadIcm
delete 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);

commit;

--CheckDeadItd
delete from tctg_itd_item_detail where itd_next_version_id = 999999999 and itd_company_id = $CMP_ID
 and not exists (select 1 from tctg_itm_item where itm_id = itd_item_id and itm_company_id = $CMP_ID and itm_next_version_id = 999999999);

commit;

--CheckCsmCat
--
-- ROWS IN CSM NOT IN CAT
--
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; 

--CheckCsaCat
--
-- ROWS IN CSA NOT IN CAT
--
    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;


--CheckChiParentCat
--
-- PARENT ROWS IN CHI NOT IN CAT
--

    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;

--CheckChiChildCat
--
-- CHILD ROWS IN CHI NOT IN CAT
-- ----------------------------------------------------------

    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;

--CheckCcmSourceCat
--
-- SOURCE ROWS IN CCM NOT IN CAT
--

    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;


--CheckCcmDestCat
--
-- DESTINATION ROWS IN CCM NOT IN CAT
--

    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;

--CheckIcmCat
--
-- ROWS IN ICM NOT IN CAT
-- --------------------------------------------

    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;

--CheckCfpCat
--
-- ROWS IN CFP NOT IN CAT 
-- ------------------------------------------------

    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;

--CheckCsmCfp
--
-- ROWS IN CSM NOT IN CFP
-- 

    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;

--CheckCsaCfp
--
-- ROWS IN CSA NOT IN CFP
-- 

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;

--CheckChiParentCfp
--
-- PARENT ROWS IN CHI NOT IN CFP
-- 
    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;

--CheckChiChildCfp
--
-- CHILD ROWS IN CHI NOT IN CFP
-- 

    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;

-- CheckCcmSourceCfp
-- SRC ROWS IN CCM NOT IN CFP
-- 

    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;

--CheckCcmDestCfp
--
-- DEST ROWS IN CCM NOT IN CFP
-- 

    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;

--CheckIcmCfp
--
-- ROWS IN ICM NOT IN CFP
-- 

    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;

--CheckCatCfp
--
-- ROWS IN CAT NOT IN CFP
-- 

    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;
" > $sqlfile

. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$sqlfile --errorfilter=yes | ${GREP} -Ev "SQL0100W|empty table"
