#!/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.

. ${TOP}/bin/get_params.sh

if [ $# -lt 3 ]
then
    ${ECHO} "Invalid number of arguments."
    ${ECHO} "Usage: $0 --cmpid=<CompanyID> --ctgid=<CatalogID> --date=DD-MON-YY"
    exit
fi

echo 'Company ID : ' $_CCD_CMPID
echo 'Catalog ID : ' $_CCD_CTGID
echo 'DATE       : ' $_CCD_DATE

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

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
set pagesize 0;
set linesize 200;
@$dropCtgIndexesFile;
commit;
EOF

$RM $dropCtgIndexesFile

echo '========================================================'
echo '== Creating temporary indexes on Catalog tables'
echo '========================================================'


echo 'CONNECTION: ' $CONNECTION

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

set heading off;
drop index temp_itm_0;
create index temp_itm_0 on tctg_itm_item (
        itm_id,
        itm_next_version_id,
        itm_container_id,
        itm_company_id
)
tablespace  INDX ;

analyze index temp_itm_0 estimate statistics;

drop index temp_icm_0;
create index temp_icm_0 on tctg_icm_item_category_map (
        icm_item_id,
        icm_next_version_id,
	icm_catalog_id,
        icm_company_id
)
tablespace  INDX ;

analyze index temp_icm_0 estimate statistics;

drop index temp_ita_0;
create index temp_ita_0 on tctg_ita_item_attributes (
        ita_item_id,
        ita_next_version_id,
        ita_catalog_id,
        ita_company_id
)
tablespace  INDX ;

analyze index temp_ita_0 estimate statistics;

prompt ===============================================================;
prompt == Deleting old versions from ITA,ITM, ICM, CAA and VER tables;
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';
purge_ctg_id constant number := $_CCD_CTGID;
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 and ver_object_id = purge_ctg_id and ver_object_type = 'CTG';

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_catalog_id = purge_ctg_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_container_id = purge_ctg_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_catalog_id = purge_ctg_id and icm_company_id = company_id;
commit;
min_itm_id := min_itm_id + delete_size;
END LOOP;

delete from TCTG_CAA_CATALOG_ATTRIBUTES where caa_next_version_id <= max_version_id and caa_catalog_id = purge_ctg_id and caa_company_id = company_id;
delete from VER where ver_date < version_date and ver_id <= max_version_id and ver_company_id = company_id and ver_object_id = purge_ctg_id and ver_object_type = 'CTG';
commit;

END;
/

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

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

EOF

echo '========================================================'
echo '== Re-creating indexes on catalog tables'
echo '========================================================'

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

${ECHO} ====================================================
${ECHO} Analyzing the schema
${ECHO} ====================================================
cd ${TOP}/src/db/schema/util
./analyze_schema.sh --confirm_db=no
