#!/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" 
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_verify.out
${RM} $tmpfile
sqlfile=${_CCD_CONF_TMP_DIR}verify.sql

echo "
---CheckDupChi
---
--- some bugs with duplicate chi rows
---

    select
    count(1) "Check_Dup_Chi"
    from tctg_chi_category_hierarchy
    where chi_next_version_id = 999999999
    and chi_company_id = $CMP_ID
    group by chi_company_id, chi_parent_id, chi_child_id
    having count(1) > 1;

---CheckDupCat
---
--- some bugs with duplicate cat rows
---
    select
    count(1) "Check_Dup_Cat"
    from tctg_cat_category
    where cat_next_version_id = 999999999
    and cat_company_id = $CMP_ID
    group by cat_id
    having count(1) > 1;

---CheckDupItm
---
--- some bugs with duplicate itm rows
---
    select
    count(1) "Check_Dup_Itm"
    from tctg_itm_item
    where itm_next_version_id = 999999999
    and itm_company_id = $CMP_ID
    group by itm_company_id, itm_container_id, itm_id
    having count(1) > 1;

---CheckDupIcm
---
--- some bugs with duplicate icm rows
---
    select
    count(1) "Check_Dup_Icm"
    from tctg_icm_item_category_map
    where icm_next_version_id = 999999999
    and icm_company_id = $CMP_ID
    group by icm_company_id, icm_catalog_id, icm_cat_tree_id, icm_item_id, icm_category_id
    having count(1) > 1;

---CheckDeadIta
---
--- dead ita that refer to non existing items
---
    select
    count(1) "Check_Dead_Ita"
    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);

---CheckDeadItd
---
--- dead ita that refer to non existing items
---
    select
    count(1) "Check_Dead_Itd"
    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);

---CheckDeadIcm
---
--- dead icm that refer to non existing items
---
    select
    count(1) "Check_Dead_Icm"
    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);

---CheckDeadItmNoItd
---
--- dead itm that refer to non existing item detail
---
    select
    count(1) "Check_Dead_Itm_No_Itd"
    from tctg_itm_item
    where itm_next_version_id = 999999999
    and itm_company_id = $CMP_ID
      and not exists (select
                      1
                      from itd
                      where itm_id = itd_item_id
                      and itd_company_id = $CMP_ID
                      and itd_next_version_id = 999999999);
---CheckDeadItm
---
--- every item in a catalog  has at least one row in the ICM table
---
    select 
    count(1) "Check_Dead_Itm" from itm, ctg 
    where itm_next_version_id = 999999999 
    and itm_container_id = ctg_id  
    and ctg_type = 'CATALOG' 
    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 );                      

---CheckDeadCatNoCad
---
--- dead cat that refer to non existing category detail
---
    select
    count(1) "Check_Dead_Cat_No_Cad"
    from tctg_cat_category
    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);

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

    select
      count(1) "Check_Duplicate_Node_Order"
    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;

---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:
---

    select
    count(1) "Check_Node_Attributes"
    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;

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

    select
    count(1) "Check_Node_Hierarchy"
    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 noh_next_version_id = 999999999);

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

    select
    count(1) "Check_Cfp_Cat"
    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;


---CheckCsmCat
---
--- ROWS IN CSM NOT IN CAT
---

    select
    count(1) "Check_Csm_Cat"
    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);

---CheckCsaCat
---
--- ROWS IN CSA NOT IN CAT
---

    select
    count(1) "Check_Csa_Cat"
    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);

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

    select
    count(1) "Check_Chi_Parent_Cat"
    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);

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

    select
    count(1) "Check_Chi_Child_Cat"
    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);

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

    select
    count(1) "Check_Ccm_Source_Cat"
    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);


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

    select
    count(1) "Check_Ccm_Dest_Cat"
    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);

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

    select
    count(1) "Check_Icm_Cat" 
    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);

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

    select
    count(1) "Check_Cat_Cfp"
    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;

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

    select
    count(1) "Check_Csm_Cfp"
    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);

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

    select
    count(1) "Check_Csa_Cfp"
    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);
    

---CheckChiParentCfp
---
--- PARENT ROWS IN CHI NOT IN CFP
--- 

    select
    count(1) "Check_Chi_Parent_Cfp"
    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);

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

    select
    count(1) "Check_Chi_Child_Cfp"
    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);
        


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

    select
    count(1) "Check_Ccm_Source_Cfp"
    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);

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

    select
    count(1) "Check_Ccm_Dest_Cfp"
    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);




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

    select
    count(1) "Check_Icm_Cfp"
    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);
" > $sqlfile

. ${TOP}/src/db/schema/util/run_db_cmd.sh --file=$sqlfile --errorfilter=yes
