#!/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-YY"
    exit
fi

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

cd $DB_DIR/util;

echo '========================================================'
echo '== Dropping indexes from catalog tables'
echo '========================================================'

dropCtgIndexesFile="tmpDropCtgIndexes.sql"

# drop catalog indexes so that updating doesn't take forever
${GREP} 'drop index' $DB_DIR/gen/idx_ctg_catalog.sql > $dropCtgIndexesFile

${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 > rcrt_index.out
set pagesize 0;
set linesize 200;
prompt Dropping indexes on catalog tables;
@$dropCtgIndexesFile;
commit;
EOF

$RM $dropCtgIndexesFile

deletefile=$TOP/logs/default/del_versions.out

echo '========================================================'
echo '== Deleting 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 '&';
spool $deletefile;

prompt =================================================================;
prompt == Creating temporary indexes on ITA, ITM, and ICM tables;
prompt ==================================================================;

create index temp_itm_0 on tctg_itm_item (
    itm_id,
    itm_next_version_id,
    itm_company_id
)
tablespace indx;

analyze index temp_itm_0 estimate statistics;

create index temp_ita_0 on tctg_ita_item_attributes (
    ita_item_id,
    ita_next_version_id,
    ita_company_id
)
tablespace indx;

analyze index temp_ita_0 estimate statistics;

create index temp_icm_0 on tctg_icm_item_category_map (
icm_item_id,
icm_next_version_id,
icm_company_id
)
tablespace indx;

analyze index temp_icm_0 estimate statistics;

prompt =================================================================;
prompt == Running a procedure to delete versions before $_CCD_DATE ;
prompt ==================================================================;

DECLARE

delete_size constant number := 10000;
min_itm_id number;
max_itm_id number;
current_itm_id number;
max_version_id number;
version_date constant date := '$_CCD_DATE';
company_id constant number := $_CCD_CMPID;

BEGIN

select max(ver_id) into max_version_id from ver where ver_date < version_date and ver_company_id = company_id;

select max(itm_id) into max_itm_id from tctg_itm_item ;
select min(itm_id) into min_itm_id from tctg_itm_item ;

WHILE min_itm_id <= max_itm_id LOOP
delete from tctg_ita_item_attributes where ita_item_id <= min_itm_id + delete_size and ita_item_id >= min_itm_id and ita_next_version_id <= max_version_id and ita_company_id = company_id;
commit;
delete from tctg_itm_item where itm_id <= min_itm_id + delete_size and itm_id >= min_itm_id and itm_next_version_id <= max_version_id and itm_company_id = company_id;
commit;
delete from tctg_icm_item_category_map where icm_item_id <= min_itm_id + delete_size and icm_item_id >= min_itm_id and icm_next_version_id <= max_version_id and icm_company_id = company_id;
commit;
min_itm_id := min_itm_id + delete_size;
END LOOP;

delete from caa where caa_next_version_id <= max_version_id and caa_company_id = company_id;
commit;
delete from cab where cab_next_version_id <= max_version_id and cab_company_id = company_id;
commit;
delete from cat where cat_next_version_id <= max_version_id and cat_company_id = company_id;
commit;
delete from ccm where ccm_next_version_id <= max_version_id and ccm_company_id = company_id;
commit;
delete from cgm where cgm_next_version_id <= max_version_id and cgm_company_id = company_id;
commit;
delete from chi where chi_next_version_id <= max_version_id and chi_company_id = company_id;
commit;
delete from csa where csa_next_version_id <= max_version_id and csa_company_id = company_id;
commit;
delete from csm where csm_next_version_id <= max_version_id and csm_company_id = company_id;
commit;
delete from cta where cta_next_version_id <= max_version_id and cta_company_id = company_id;
commit;
delete from noa where noa_next_version_id <= max_version_id and noa_company_id = company_id;
commit;
delete from nod where nod_next_version_id <= max_version_id and nod_company_id = company_id;
commit;
delete from noh where noh_next_version_id <= max_version_id and noh_company_id = company_id;
commit;
delete from obm where obm_next_version_id <= max_version_id and obm_company_id = company_id;
commit;
delete from omd where omd_next_version_id <= max_version_id and omd_company_id = company_id;
commit;
delete from oms where oms_next_version_id <= max_version_id and oms_company_id = company_id;
commit;
delete from rul where rul_next_version_id <= max_version_id and rul_company_id = company_id;
commit;
delete from spa where spa_next_version_id <= max_version_id and spa_company_id = company_id;
commit;
delete from tmt where tmt_next_version_id <= max_version_id and tmt_company_id = company_id;
commit;
delete from tctg_ver_version where ver_date < version_date and ver_id <= max_version_id and ver_company_id = company_id;
commit;

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

delete from lck where lck_object_type = 'WORKFLOW_EVENT' and exists (select 1 from wfe where lck_object_id = wfe_id and wfe_start_time < version_date and wfe_cmp_id = company_id and wfe_start_time < sysdate - 7) ;
commit;

delete from wee where exists (select 1 from wfe where wfe_id = wee_event_id and wfe_start_time < version_date and wfe_cmp_id = company_id and wfe_start_time < sysdate - 7) ;
commit;

delete from wfe where wfe_start_time < version_date and wfe_start_time < sysdate - 7  and wfe_cmp_id = company_id ;
commit;

END;
/

prompt =====================================================;
prompt == Deleting temporary indexes;
prompt =====================================================;

drop index temp_itm_0;
drop index temp_ita_0;
drop index temp_icm_0;

spool off;
EOF

echo '======================================================'
echo '== Creating indexes on catalog table'
echo '======================================================'

${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 >> rcrt_index.out
set pagesize 0;
set linesize 200;
@$DB_DIR/gen/idx_ctg_catalog.sql;
commit;
EOF
