#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# © Copyright IBM Corp. 2000, 2007
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

. ${TOP}/src/db/schema/util/set_db.sh

DB_USER=`${ECHO} ${DB_USER_NAME} | ${TR} "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"`
START_TIME=`date`
report_file=$TOP/logs/constraint_data_verification_report.out

${ECHO} "---------------------"
${ECHO} "Executing constraint verification script"
${ECHO} "---------------------"

if [ "$CCD_DB" = "oracle" ]; then

${ECHO} "Verifying Spec objects"

STATEMENT=`cat <<EOF
PROMPT 'TCTG_SPC_SPECIFICATION';
select 'Spec Name = ' || spc_name || '; Node name = ' || nod_name
as Spec_nodes
from nod, spc,
(select nod_id id, nod_next_version_id nxt, nod_spec_id spc, nod_company_id cmp from nod
where nod_next_version_id = 999999999
group by nod_id, nod_next_version_id, nod_spec_id, nod_company_id
having count(1) > 1) g
where nod_spec_id = spc_id
and nod_next_version_id = 999999999
and nod_id = g.id
and nod_next_version_id = g.nxt
and nod_spec_id = g.spc
and nod_company_id = g.cmp;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF > $report_file
$STATEMENT
EOF

${ECHO} "Verifying Spec node hierarchies" 

STATEMENT=`cat <<EOF
PROMPT 'TCTG_NOH_NODE_HIERARCHY';
select 'Spec Name = ' || spc_name || '; Child node name = ' || chld.nod_name || ';' Spec_child_node
from noh, nod chld, spc, 
(select noh_spec_id spc, noh_next_version_id nxt, noh_order ord, noh_company_id cmp from noh
where noh_next_version_id = 999999999
group by noh_spec_id, noh_next_version_id, noh_order, noh_company_id
having count(1) > 1) g
where noh_child_node_id = nod_id
and noh_spec_id = nod_spec_id
and noh_next_version_id = nod_next_version_id
and nod_spec_id = spc_id 
and noh_next_version_id = 999999999
and noh_spec_id = g.spc
and noh_next_version_id = g.nxt
and noh_order = g.ord
and noh_company_id = g.cmp;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Category objects" 

STATEMENT=`cat <<EOF
PROMPT 'TCTG_CAT_CATEGORY';
select cfp_full_path category_path from cfp, cat, 
(select cat_id id, cat_next_version_id nxt, cat_container_id cont, cat_company_id cmp from cat i
group by cat_id, cat_next_version_id, cat_container_id, cat_company_id
having count(1) > 1) g
where cfp_category_id = cat_id
and cat_next_version_id = 999999999
and cat_id = g.id;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Category path" 

STATEMENT=`cat <<EOF
PROMPT 'TCTG_CFP_CAT_FULL_PATHS';
select cfp_full_path as category_path from cfp o, 
(select cfp_full_path fp, cfp_full_path_type fpt, cfp_next_version_id nxt, cfp_cat_tree_id ctr from cfp
group by cfp_full_path, cfp_full_path_type, cfp_next_version_id, cfp_cat_tree_id
having count(1) > 1) g
where cfp_next_version_id = 999999999
and g.fp = o.cfp_full_path;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Category hierarchies" 

STATEMENT=`cat <<EOF
PROMPT 'TCTG_CHI_CATEGORY_HIERARCHY';
select 'Parent category path = ' || prnt.cfp_full_path || '; Child category path = ' || child.cfp_full_path
as category_hierarchy
from cfp prnt, cfp child, chi o, 
(select chi_company_id cmp, chi_parent_id prn, chi_child_id chd, chi_next_version_id nxt, count(1) from chi i
group by chi_company_id, chi_parent_id, chi_child_id, chi_next_version_id
having count(1) > 1) g
where chi_parent_id = prnt.cfp_category_id
and chi_parent_id = g.prn
and chi_child_id = child.cfp_category_id
and chi_next_version_id = 999999999;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Item objects" 

STATEMENT=`cat <<EOF
PROMPT 'TCTG_ITM_ITEM';
select 'Catalog name = ' || ctg_name || '; Item Primary key = ' || itm_primary_key
as item_catalog_primary_key
from itm o inner join
(select itm_id id, itm_next_version_id nxt, itm_container_id cont, itm_company_id cmp from itm
group by itm_id, itm_next_version_id, itm_container_id, itm_company_id
having count(1) > 1) g on g.id = o.itm_id
inner join ctg on o.itm_container_id = ctg_id
where  itm_next_version_id = 999999999;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Item Category mapping"

STATEMENT=`cat <<EOF
PROMPT 'TCTG_ICM_ITEM_CATEGORY_MAP';
select 'Catalog name = ' || ctg_name || '; Item Primary key = ' || itm_primary_key ||
'; Category path = ' || cfp_full_path as Item_category_mapping
from icm o inner join
(select icm_item_id icmitem, icm_next_version_id nxt, icm_cat_tree_id ctr, icm_category_id cat, icm_company_id cmp
from icm i
group by icm_item_id, icm_next_version_id, icm_cat_tree_id, icm_category_id, icm_company_id
having count(1) > 1) g on g.icmitem = o.icm_item_id
inner join itm on o.icm_item_id = itm_id
inner join ctg on o.icm_catalog_id = ctg_id
left outer join cfp on o.icm_category_id = cfp_category_id
where o.icm_next_version_id = 999999999;
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

${ECHO} "Verifying Collaboration area events" 

STATEMENT=`cat <<EOF
PROMPT 'TWFL_WFE_WORKFLOW_EVENT';
select 'Collaboration area name = ' || coa_name || '; Workflow name = ' || wfd_name ||
'; Workflow step name = ' || spt_leaf_step_text_path || '; Item primary key = ' || itm_primary_key
as Workflow_item_info
from wfe
left outer join coa on wfe_collaboration_area_id = coa_id
left outer join spt on wfe_source_step_at_path_id = spt_id
left outer join wfd on coa_wfl_id = wfd_id
left outer join wee on wfe_id = wee_event_id
left outer join cem on wee_entry_id = cem_ca_entry_id and cem_ca_id = coa_id
left outer join itm on cem_src_entry_id = itm_id
where wfe_event_status in ('NEW','PROCESSING')
and wfe_id in 
(select wfe_id id from wfe
group by wfe_id
having count(1) > 1);
EOF`

${CCD_SQLPLUS} ${CONNECTION} <<EOF >> $report_file
$STATEMENT
EOF

#===============================================================================
elif [ "$CCD_DB" = "db2" ]; then

db2 -t $CONNECTION
${ECHO} "Verifying Spec objects" 

${ECHO} "
select 'Spec Name = ' || spc_name || '; Node name = ' || nod_name
as TCTG_SPC_SPECIFICATION
from nod, spc,
(select nod_id id, nod_next_version_id nxt, nod_spec_id spc, nod_company_id cmp from nod
where nod_next_version_id = 999999999
group by nod_id, nod_next_version_id, nod_spec_id, nod_company_id
having count(1) > 1) g
where nod_spec_id = spc_id
and nod_next_version_id = 999999999
and nod_id = g.id
and nod_next_version_id = g.nxt
and nod_spec_id = g.spc
and nod_company_id = g.cmp;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql > $report_file

${ECHO} "Verifying Spec node hierarchies"

${ECHO} "
select 'Spec Name = ' || spc_name || '; Child node name = ' || child.nod_name
as TCTG_NOH_NODE_HIERARCHY
from noh, nod child, spc, 
(select noh_spec_id spc, noh_next_version_id nxt, noh_order ord, noh_company_id cmp from noh
where noh_next_version_id = 999999999
group by noh_spec_id, noh_next_version_id, noh_order, noh_company_id
having count(1) > 1) g
where noh_child_node_id = nod_id
and noh_spec_id = nod_spec_id
and noh_next_version_id = nod_next_version_id
and nod_spec_id = spc_id 
and noh_next_version_id = 999999999
and noh_spec_id = g.spc
and noh_next_version_id = g.nxt
and noh_order = g.ord
and noh_company_id = g.cmp;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Category objects" 

${ECHO} "
select cfp_full_path TCTG_CAT_CATEGORY from cfp, cat, 
(select cat_id id, cat_next_version_id nxt, cat_container_id cont, cat_company_id cmp from cat i
group by cat_id, cat_next_version_id, cat_container_id, cat_company_id
having count(1) > 1) g
where cfp_category_id = cat_id
and cat_next_version_id = 999999999
and cat_id = g.id;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Category path" 

${ECHO} "
select cfp_full_path as TCTG_CFP_CAT_FULL_PATHS from cfp o, 
(select cfp_full_path fp, cfp_full_path_type fpt, cfp_next_version_id nxt, cfp_cat_tree_id ctr from cfp
group by cfp_full_path, cfp_full_path_type, cfp_next_version_id, cfp_cat_tree_id
having count(1) > 1) g
where cfp_next_version_id = 999999999
and g.fp = o.cfp_full_path;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Category hierarchies" 

${ECHO} "
select 'Parent category path = ' || prnt.cfp_full_path || '; Child category path = ' || child.cfp_full_path
as TCTG_CHI_CATEGORY_HIERARCHY
from cfp prnt, cfp child, chi o, 
(select chi_company_id cmp, chi_parent_id prn, chi_child_id chd, chi_next_version_id nxt, count(1) from chi i
group by chi_company_id, chi_parent_id, chi_child_id, chi_next_version_id
having count(1) > 1) g
where chi_parent_id = prnt.cfp_category_id
and chi_parent_id = g.prn
and chi_child_id = child.cfp_category_id
and chi_next_version_id = 999999999;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Item objects" 

${ECHO} "
select 'Catalog name = ' || ctg_name || '; Item Primary key = ' || itm_primary_key
as TCTG_ITM_ITEM
from itm o inner join
(select itm_id id, itm_next_version_id nxt, itm_container_id cont, itm_company_id cmp from itm
group by itm_id, itm_next_version_id, itm_container_id, itm_company_id
having count(1) > 1) g on g.id = o.itm_id
inner join ctg on o.itm_container_id = ctg_id
where  itm_next_version_id = 999999999;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Item Category mapping" 

${ECHO} "
select 'Catalog name = ' || ctg_name || '; Item Primary key = ' || itm_primary_key ||
'; Category path = ' || cfp_full_path as TCTG_ICM_ITEM_CATEGORY_MAP
from icm o inner join
(select icm_item_id icmitem, icm_next_version_id nxt, icm_cat_tree_id ctr, icm_category_id cat, icm_company_id cmp
from icm i
group by icm_item_id, icm_next_version_id, icm_cat_tree_id, icm_category_id, icm_company_id
having count(1) > 1) g on g.icmitem = o.icm_item_id
inner join itm on o.icm_item_id = itm_id
inner join ctg on o.icm_catalog_id = ctg_id
left outer join cfp on o.icm_category_id = cfp_category_id
where o.icm_next_version_id = 999999999;
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

${ECHO} "Verifying Collaboration area events" 

${ECHO} "
select 'Collaboration area name = ' || coa_name || '; Workflow name = ' || wfd_name ||
'; Workflow step name = ' || spt_leaf_step_text_path || '; Item primary key = ' || itm_primary_key
as TWFL_WFE_WORKFLOW_EVENT
from wfe
left outer join coa on wfe_collaboration_area_id = coa_id
left outer join spt on wfe_source_step_at_path_id = spt_id
left outer join wfd on coa_wfl_id = wfd_id
left outer join wee on wfe_id = wee_event_id
left outer join cem on wee_entry_id = cem_ca_entry_id and cem_ca_id = coa_id
left outer join itm on cem_src_entry_id = itm_id
where wfe_event_status in ('NEW','PROCESSING')
and wfe_id in 
(select wfe_id id from wfe
group by wfe_id
having count(1) > 1);
" > ${DB_SCRIPTS}/temp_verify.sql

db2 -tf ${DB_SCRIPTS}/temp_verify.sql >> $report_file

#===============================================================================
else  
      ${ECHO} "DB Environment not set"  
      exit 
fi

${ECHO} "Script completed"
${ECHO} " Please see the file for further details : " $report_file

END_TIME=`date` 

${ECHO} "START:" $START_TIME
${ECHO} "END  :" $END_TIME
