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

if [ $# -lt 2 ]
then
    ${ECHO} "Invalid number of arguments."
    ${ECHO} "Usage: $0 --cmpid=<CompanyID> --date=DD-MON-YYYY"
    exit
fi
${ECHO} "Last Revision: 5.3.2 FP11 20080805"

. ${TOP}/bin/get_params.sh

. ${TOP}/src/db/schema/util/set_db.sh \
    --db_user_name=${_CCD_DB_USER_NAME} \
    --db_user_pwd=${_CCD_DB_USER_PWD} \
    --db_service_name=${_CCD_DB_SERVICE_NAME}

${ECHO} 'CONNECTION: ' $CONNECTION
${ECHO} 'DATE: ' $_CCD_DATE
${ECHO} 'COMPANY ID: ' $_CCD_CMPID
version_date=$_CCD_DATE
company_id=$_CCD_CMPID

${ECHO} '========================================================'
${ECHO} '== Estimating old versions before ' $_CCD_DATE
${ECHO} '========================================================'

${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1
set pagesize 0;
set linesize 500;
set heading off;
set timing on;
set define '&';

select 'ITA ROWS BEFORE ${version_date} =', count(1) ita_rows from ita where ita_next_version_id <= (select max(ver_id) VER_ID from ver where ver_date <  To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and ver_company_id = ${company_id} ) and ita_company_id =  ${company_id} ;

select 'ITM ROWS BEFORE ${version_date} =', count(1) itm_rows from itm where itm_next_version_id <= (select max(ver_id) VER_ID from ver where ver_date <  To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and ver_company_id = ${company_id}) and itm_company_id = ${company_id} ;

select 'ICM ROWS BEFORE ${version_date} =', count(1) icm_rows from icm where icm_next_version_id <= (select max(ver_id) VER_ID from ver where ver_date <  To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and ver_company_id = ${company_id}) and icm_company_id = ${company_id} ;

select 'ITD ROWS BEFORE ${version_date} =', count(1) itd_rows from itd where itd_next_version_id <= (select max(ver_id) VER_ID from ver where ver_date <  To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and ver_company_id = ${company_id}) and itd_company_id = ${company_id} ;

select 'LCK ROWS OF OBSOLETE ITEMS =', count(1) lck_rows from lck where lck_object_type = 'ITEM' and not exists (select 1 from itm where lck_object_id = itm_id and itm_next_version_id = 999999999) ;

select 'LCK WORKFLOW ROWS BEFORE ${version_date} =', count(1) lck_rows from lck where lck_object_type = 'WORKFLOW_EVENT' and exists (select 1 from wfe where lck_object_id = wfe_id and wfe_start_time < To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and wfe_cmp_id = ${company_id} and wfe_start_time < sysdate - 7);

select 'WEE ROWS BEFORE ${version_date} =', count(1) wee_rows from wee where exists (select 1 from wfe where wfe_id = wee_event_id and wfe_start_time < To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and wfe_cmp_id = ${company_id} and wfe_start_time < sysdate - 7) ;

select 'WFE ROWS BEFORE ${version_date} =', count(1) wfe_rows from wfe where wfe_start_time < To_Date('${version_date}','DD-MON-YYYY HH24:MI:SS') and wfe_start_time < sysdate - 7  and wfe_cmp_id = ${company_id} ;

commit;

EOF
