#!/bin/bash
# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
# 
# # 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. 
# 
#


# The purpose of this script is to migrate the database schema used by MDMPIM from any FP of 6.0.0 to 6.0.0 Current FP installed
# Please consult MDMPIM support before attempting to use this script



. ${TOP}/src/db/schema/util/set_db_localized.sh
_ERROR_LOGS=$TOP/logs/errfile.log
${ECHO} " " > $_ERROR_LOGS 


_DEPENDENCIES_LOGS=$TOP/logs/dependenciesCheck.log
${ECHO} " " > $_DEPENDENCIES_LOGS

${ECHO} " " > ${DB_SCRIPTS}/verifyFPMigration.sql

EXISTS=1;
NOTEXISTS=0;
count=0;

if [ -d $TOP/tmp ]; then
  TMPDIREXISTS=$EXISTS
else
  TMPDIREXISTS=$NOTEXISTS
fi

getErrorCountInLogFile()
{
    if [ ! -f $_ERROR_LOGS ]; then
        errorCount=0
    else
        errorCount=`cat $_ERROR_LOGS | wc -c`
    fi
}

DoStartAndPrepare()
{
    ${ECHO} "-----------------------------------------" 
    ${ECHO} $1
    ${ECHO} "-----------------------------------------" 

    getErrorCountInLogFile
    initial=$errorCount
}

DoEndAndReportError()
{

    getErrorCountInLogFile
    final=$errorCount

    if [ ! $initial -eq $final ]; then

        ${ECHO} "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        ${ECHO} $1
        ${ECHO} "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        ${ECHO} "COMMON_INFO_CHECK_ERROR_LOG_FILE_SH" $_ERROR_LOGS
        
    else
        ${ECHO} ""
        ${ECHO} "-----------------------------------------"
        ${ECHO} $2
        ${ECHO} "-----------------------------------------"
    fi
}

#
# Define the various migration functions. New functions can be added in this section.
#


updateLCKIndex()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH LCK_Index"       
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/tutl_lck_lock_indexes.sql --errorfilter=yes 2>> $_ERROR_LOGS
        if [ ${CCD_DB} = "oracle" ]; then
            ${ECHO} "column column_name format a30;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name= 'TUTL_LCK_LOCK' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql         
            ${ECHO} "select UNIQUENESS from user_indexes where index_name='IUTL_LCK_0';" >> ${DB_SCRIPTS}/verifyFPMigration.sql 
        else
            ${ECHO} "select indname, colname, colseq from syscat.indexcoluse where indschema= CURRENT SCHEMA and indname like '%LCK%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select UNIQUERULE from syscat.indexes where indschema= CURRENT SCHEMA and indname='IUTL_LCK_0';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select volatile from syscat.tables where tabschema= CURRENT SCHEMA and tabname='TUTL_LCK_LOCK';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        fi
    DoEndAndReportError  "COMMON_INFO_FAILED_SH LCK_Index" "COMMON_INFO_SUCCEEDED_SH LCK_Index"
}


updateITACABColumns()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH ITA_CAB_Columns"
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_ita_cab_columns.sql --errorfilter=yes 2>> $_ERROR_LOGS
    
    if [ ${CCD_DB} = "oracle" ]; then
           ${ECHO} "describe TCTG_ITA_ITEM_ATTRIBUTES;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
           ${ECHO} "describe TCTG_CAB_CATEGORY_ATTRIBUTES;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
       else
            ${ECHO} "select TABNAME,COLNAME,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'ITA_VALUE_STRING' and TABNAME = 'TCTG_ITA_ITEM_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select TABNAME,COLNAME,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'ITA_VALUE_STRING_IGNORECASE' and TABNAME = 'TCTG_ITA_ITEM_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select TABNAME,COLNAME,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'CAB_VALUE_STRING' and TABNAME = 'TCTG_CAB_CATEGORY_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
            ${ECHO} "select TABNAME,COLNAME,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'CAB_VALUE_STRING_IGNORECASE' and TABNAME = 'TCTG_CAB_CATEGORY_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
   fi
   
    DoEndAndReportError  "COMMON_INFO_FAILED_SH ITA_CAB_Columns" "COMMON_INFO_SUCCEEDED_SH ITA_CAB_Columns"
}



updateCollabAttributes()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH Collab Attributes"
        $JAVA_RT com.ibm.ccd.common.migration.FixCollaborationAttributes 2>> $_ERROR_LOGS 
    DoEndAndReportError  "COMMON_INFO_FAILED_SH Collab Attributes" "COMMON_INFO_SUCCEEDED_SH Collab Attributes" 
}


alterEntryTabbedPane()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH EntryTabbedPane"
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_etp_table.sql --errorfilter=yes 2>> $_ERROR_LOGS 
    DoEndAndReportError  "COMMON_INFO_FAILED_SH EntryTabbedPane" "COMMON_INFO_SUCCEEDED_SH EntryTabbedPane" 

    if [ ${CCD_DB} = "oracle" ]; then
        ${ECHO} "describe tctg_etp_entry_tabbed_pane;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select etp_tab_attr_order from tctg_etp_entry_tabbed_pane where etp_tab_attr_order != 'SPEC_ORDER';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
    else
        ${ECHO} "select COLNAME from syscat.columns where TABNAME = 'TCTG_ETP_ENTRY_TABBED_PANE' AND TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select etp_tab_attr_order from tctg_etp_entry_tabbed_pane where etp_tab_attr_order != 'SPEC_ORDER';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
    fi
}

dropIndexExplainObject()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH Explain_Object"
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/drop_index_explain_object.sql --errorfilter=yes 2>> $_ERROR_LOGS
        ${ECHO} "select indname, colname, colseq from syscat.indexcoluse where indschema= CURRENT SCHEMA and indname like '%EXP_OBJ_IND%' order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
    DoEndAndReportError  "COMMON_INFO_FAILED_SH Explain_Object" "COMMON_INFO_SUCCEEDED_SH Explain_Object"
}

updateContainerIndex()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH ICNT_EEM_Index"      
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/creat_icnt_eem_2_index.sql 2>> $_ERROR_LOGS
    DoEndAndReportError  "COMMON_INFO_FAILED_SH ICNT_EEM_Index" "COMMON_INFO_SUCCEEDED_SH ICNT_EEM_Index"
    if [ ${CCD_DB} = "oracle" ]; then
        ${ECHO} "column column_name format a30;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCNT_EEM_ENTRY_ENTRY_MAP' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
    else
        ${ECHO} "select indname, colname, colseq from syscat.indexcoluse where indschema= CURRENT SCHEMA and indname='ICNT_EEM_2' order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql     
    fi
}


updateACGs()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH ACGs"
        exportACGRoles
        recreateObjectActions
        importACGRoles
        updateAdminRoles
    DoEndAndReportError  "COMMON_INFO_FAILED_SH ACGs" "COMMON_INFO_SUCCEEDED_SH ACGs"   
}

exportACGRoles() 
{
    ${ECHO} "EXPORTING..."
    for CMP_CODE_COL in $COMPANIES; 
    do
        CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
        if [ "$CMP_CODE" != "" ]; then          
            ${ECHO} $CMP_CODE
            ${ECHO} 
            
            $JAVA_RT com.ibm.ccd.envexpimp.common.EnvironmentExporter -companycode="$CMP_CODE" -scriptpath=$TOP/bin/exportACGRolesScript.wpcs 2>> $_ERROR_LOGS  
                                
            $JAVA_RT com.ibm.ccd.docstore.util.DocDownloader -companycode="$CMP_CODE" -subtree=archives/fpmigration/ -xmlfile=$TOP/tmp/abc.xml -rootlocalpath=$TOP/tmp/ 2>> $_ERROR_LOGS | ${GREP} -v '.*'
            
            `mv $TOP/tmp/__archives__fpmigration__Export.zip $TOP/tmp/__archives__fpmigration__cmp"$count".zip `
                
            count=`expr $count + 1`
                
            `rm -f $TOP/tmp/abc.xml `
        fi            
    done
}

recreateObjectActions()
{
    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/recreateOAC_OBT.sql --errorfilter=yes 2>> $_ERROR_LOGS | ${GREP} -v '.*'
    ${ECHO} "select count(*) NO_OF_OAC_RECORDS from oac;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
    ${ECHO} "select count(*) NO_OF_OBT_RECORDS from obt;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
}

importACGRoles()
{
    count=0;
    ${ECHO} "IMPORTING..."
    for CMP_CODE_COL in $COMPANIES; 
        do
            CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
            if [ "$CMP_CODE" != "" ]; then
                ${ECHO} $CMP_CODE
                ${ECHO}
                $JAVA_RT com.ibm.ccd.envexpimp.common.EnvironmentImporter -companycode="$CMP_CODE" -zipfilepath=$TOP/tmp/__archives__fpmigration__cmp"$count".zip 2>> $_ERROR_LOGS | ${GREP} -v '.*'
                                
                if [ $TMPDIREXISTS -eq $EXISTS ]; then
                    `rm $TOP/tmp/__archives__fpmigration__cmp"$count".zip `
                fi      
                
                count=`expr $count + 1`
            fi            
    done
    
    if [ $TMPDIREXISTS -eq $NOTEXISTS ]; then
        `rm -rf $TOP/tmp/ `
    fi
}

updateAdminRoles()
{   
    `rm -f $DB_SCRIPTS/updateAdminORM.sql `
    ${ECHO} " " > ${DB_SCRIPTS}/updateAdminORM.sql
    for CMP_CODE_COL in $COMPANIES; 
    do
        CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
        if [ "$CMP_CODE" != "" ]; then          
            ${ECHO} "update orm set orm_access = 'feffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01'
                     where orm_company_id=(select cmp_company_id from cmp where cmp_company_code='$CMP_CODE') 
                        and orm_role_id in (select rol_role_id from rol 
                                         where rol_company_id=(select cmp_company_id from cmp where cmp_company_code='$CMP_CODE') 
                                            and rol_role_name = '${CMP_CODE}_admin');" >> ${DB_SCRIPTS}/updateAdminORM.sql
            ${ECHO} "commit;" >> ${DB_SCRIPTS}/updateAdminORM.sql
        fi            
    done  
    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/updateAdminORM.sql --errorfilter=yes 2>> $_ERROR_LOGS 
}

importScripts()
{   
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH Report Scripts"      
        for CMP_CODE_COL in $COMPANIES; 
            do
                CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
                if [ "$CMP_CODE" != "" ]; then
                    ${ECHO} "COMMON_INFO_UPDATING_WPC_SCRIPTS_SH" $CMP_CODE
                    $JAVA_RT com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin ${SCRIPTS_DIR}/common/report_scripts.xml Cp1252 2>> $_ERROR_LOGS 
                fi            
        done
    DoEndAndReportError  "COMMON_INFO_FAILED_SH Report Scripts" "COMMON_INFO_SUCCEEDED_SH Report Scripts"
}

updateLkpTablePrivileges()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH LookupTable Privileges"
        for CMP_CODE_COL in $COMPANIES; 
        do
            CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
            if [ "$CMP_CODE" != "" ]; then
                $JAVA_RT com.ibm.ccd.common.interpreter.test.TestScript --companycode=$CMP_CODE --scriptpath=$TOP/bin/updateLkpTableACGs.wpcs --username="Admin" 2>> $TOP/logs/errfile1.log             
            fi            
        done 
        `rm -f $TOP/logs/errfile1.log `
    DoEndAndReportError  "COMMON_INFO_FAILED_SH LookupTable Privileges" "COMMON_INFO_SUCCEEDED_SH LookupTable Privileges"
}

updateAuditLogs()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH Audit Logs"
        $JAVA_RT com.ibm.ccd.common.migration.AuditLogMigration 2>> $_ERROR_LOGS 
    DoEndAndReportError  "COMMON_INFO_FAILED_SH Audit Logs" "COMMON_INFO_SUCCEEDED_SH Audit Logs"   
}


GET_COMPANIES=`. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/get_cmpcode.sql`
COMPANIES="${GET_COMPANIES}"



putConstraints()
{

    DoStartAndPrepare "COMMON_INFO_UPDATING_SH Constraints"  

    if [ -z "$TOP" -o ! -r "$TOP" ]; then
       ${ECHO} TOP variable not defined
       exit 1
    fi

    if [ "$CCD_DB" = "oracle" ]; then    
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/oracle/validation.sql >> $_DEPENDENCIES_LOGS
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/oracle/cleanup.sql 2>> $_ERROR_LOGS
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/oracle/create_constraints.sql 2>> $_ERROR_LOGS
        
         ${ECHO} "SET PAGESIZE 100; " >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "column TABLE_NAME        format a17  HEADING   'TABLE NAME';"       >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "column COLUMN_NAME       format a14  HEADING   'COLUMN_NAME';"      >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "column CONSTRAINT_NAME    format a12 HEADING   'CNSTR_NAME';"  >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "column CONSTRAINT_TYPE   format a10  HEADING   'CNSTR_TYPE';"  >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "column SEARCH_CONDITION  format a22 HEADING   'SEARCH_CONDITION';" >> ${DB_SCRIPTS}/verifyFPMigration.sql


         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_CHI_CATEGORY_HIERARCHY' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_CHI_CATEGORY_HIERARCHY' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         

         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.INDEX_NAME, 'U' AS CONSTRAINT_NAME, ' ' AS SEARCH_CONDITION FROM USER_IND_COLUMNS A, USER_INDEXES B WHERE A.TABLE_NAME = 'TCTG_CAT_CATEGORY' AND A.INDEX_NAME LIKE 'CAT%UK%' AND A.INDEX_NAME = B.INDEX_NAME UNION SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, CASE B.CONSTRAINT_TYPE WHEN 'C' THEN A.COLUMN_NAME ||' IS NOT NULL' END FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_CAT_CATEGORY' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.CONSTRAINT_NAME NOT LIKE '%_UK' order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_CAT_CATEGORY' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.INDEX_NAME, 'U' AS CONSTRAINT_NAME, ' ' AS SEARCH_CONDITION FROM USER_IND_COLUMNS A, USER_INDEXES B WHERE A.TABLE_NAME = 'TCTG_ITM_ITEM' AND A.INDEX_NAME LIKE 'ITM%UK%' AND A.INDEX_NAME = B.INDEX_NAME UNION SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, CASE B.CONSTRAINT_TYPE WHEN 'C' THEN A.COLUMN_NAME ||' IS NOT NULL' END FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_ITM_ITEM' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.CONSTRAINT_NAME NOT LIKE '%_UK' order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_ITM_ITEM' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_ICM_ITEM_CATEGORY_MAP' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_ICM_ITEM_CATEGORY_MAP' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_CFP_CAT_FULL_PATHS' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_CFP_CAT_FULL_PATHS' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_NOH_NODE_HIERARCHY' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_NOH_NODE_HIERARCHY' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_NOD_NODE' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TCTG_NOD_NODE' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         
         
         ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, A.CONSTRAINT_NAME, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TWFL_WFE_WORKFLOW_EVENT' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.OWNER = (SELECT USERNAME FROM USER_USERS) AND A.OWNER = B.OWNER order by 3,2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         ${ECHO} "select index_name, column_name, column_position from user_ind_columns where table_name='TWFL_WFE_WORKFLOW_EVENT' order by 1, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
         
        
    else
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/db2/validation.sql >> $_DEPENDENCIES_LOGS
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/db2/cleanup.sql 2>> $_ERROR_LOGS
        . ${DB_DIR}/util/run_db_cmd.sh --file=$TOP/bin/dbmigration/constraints/db2/create_constraints.sql 2>> $_ERROR_LOGS

        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_CHI_CATEGORY_HIERARCHY' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_CHI_CATEGORY_HIERARCHY' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_CHI_CATEGORY_HIERARCHY' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_CHI%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_CAT_CATEGORY' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_CAT_CATEGORY' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_CAT_CATEGORY' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_CAT%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_ITM_ITEM' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_ITM_ITEM' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_ITM_ITEM' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_ITM%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_ICM_ITEM_CATEGORY_MAP' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_ICM_ITEM_CATEGORY_MAP' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_ICM_ITEM_CATEGORY_MAP' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_ICM%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_CFP_CAT_FULL_PATHS' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_CFP_CAT_FULL_PATHS' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_CFP_CAT_FULL_PATHS' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_CFP%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
                

        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_NOH_NODE_HIERARCHY' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_NOH_NODE_HIERARCHY' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_NOH_NODE_HIERARCHY' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_NOH%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TCTG_NOD_NODE' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_NOD_NODE' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TCTG_NOD_NODE' and c.indschema= CURRENT SCHEMA and i.indname like '%ICTG_NOD%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        ${ECHO} "select TABNAME, COLNAME, CONSTNAME, COLSEQ from SYSCAT.KEYCOLUSE where  TABNAME = 'TWFL_WFE_WORKFLOW_EVENT' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TWFL_WFE_WORKFLOW_EVENT' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select c.indname, colname, colseq from syscat.indexcoluse c, syscat.indexes i where c.indschema = i.indschema and c.indname =  i.indname and i.tabname = 'TWFL_WFE_WORKFLOW_EVENT' and c.indschema= CURRENT SCHEMA and i.indname like '%IWFL_WFE%'order by indname, colseq;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        
        
        
    fi  
    DoEndAndReportError  "COMMON_INFO_FAILED_SH Constraints" "COMMON_INFO_SUCCEEDED_SH "
}

updateNoaSpaColumns()
{
    DoStartAndPrepare "COMMON_INFO_UPDATING_SH NOA_SPA_Columns"
        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_noa_spa_table.sql --errorfilter=yes 2>> $_ERROR_LOGS

   if [ ${CCD_DB} = "oracle" ]; then
       ${ECHO} "describe TCTG_SPA_SPEC_ATTRIBUTES;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
   else
        ${ECHO} "select TABSCHEMA,TABNAME,COLNAME,COLNO,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'NOA_VALUE' and TABNAME = 'TCTG_NOA_NODE_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
        ${ECHO} "select TABSCHEMA,TABNAME,COLNAME,COLNO,TYPENAME,LENGTH from SYSCAT.COLUMNS where  COLNAME = 'SPA_VALUE' and TABNAME = 'TCTG_SPA_SPEC_ATTRIBUTES' and TABSCHEMA = CURRENT SCHEMA;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
   fi
    DoEndAndReportError  "COMMON_INFO_FAILED_SH NOA_SPA_Columns" "COMMON_INFO_SUCCEEDED_SH NOA_SPA_Columns"

}


modify_wfe_index_organized()
{
 DoStartAndPrepare "COMMON_INFO_UPDATING_SH update_wfe_index" 
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/drop_temp_wfe.sql > $TOP/logs/drop_temp.out
  ${RM} $TOP/logs/drop_temp.out
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/recreate_wfe_iot.sql 2>> $_ERROR_LOGS
  ${ECHO} "select iot_type from user_tables where table_name = 'TWFL_WFE_WORKFLOW_EVENT';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
DoEndAndReportError  "COMMON_INFO_FAILED_SH update_wfe_index" "COMMON_INFO_SUCCEEDED_SH update_wfe_index"
}

modify_lck_table()
{
 DoStartAndPrepare "COMMON_INFO_UPDATING_SH modify_lck_table" 
 if [ "$CCD_DB" = "oracle" ]; then
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/recreate_lck_iot.sql 2>> $_ERROR_LOGS  
  ${ECHO} "select iot_type from user_tables where table_name = 'TUTL_LCK_LOCK';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
 else
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/recreate_lck_mdc.sql 2>> $_ERROR_LOGS  
  ${ECHO} "select substr(indname,1,3) "INDNAME", colnames, indextype from syscat.indexes where tabname = 'TUTL_LCK_LOCK' and indextype = 'DIM';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
 fi
 DoEndAndReportError  "COMMON_INFO_FAILED_SH update_wfe_index" "COMMON_INFO_SUCCEEDED_SH modify_lck_table"
}

modify_udl_table()
{
 DoStartAndPrepare "COMMON_INFO_UPDATING_SH modify_udl_table" 
 #Verify UDL_NAME column length
 if [ "$CCD_DB" = "oracle" ]; then
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/check_udl.sql >> $TOP/logs/check_udl.out
 else
  . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/check_udl.sql >> $TOP/logs/check_udl.out
 fi
 udlErrCount=`${GREP} -c ERROR $TOP/logs/check_udl.out`
 if [ $udlErrCount -eq 0 ]; then
	#Hide drop temp_udl error message
    . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/drop_udl_temp.sql >> $TOP/logs/check_udl.out
	#Recreate UDL table
    . ${DB_DIR}/util/run_db_cmd.sh --file=${DB_SCRIPTS}/udl_pk.sql 2>> $_ERROR_LOGS
	if [ "$CCD_DB" = "oracle" ]; then
	 ${ECHO} "column table_name format a30;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	 ${ECHO} "column cname format a30;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	 ${ECHO} "column column_name format a30;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	 ${ECHO} "SELECT A.TABLE_NAME, A.COLUMN_NAME, SUBSTR(A.CONSTRAINT_NAME,1,3) cname, B.CONSTRAINT_TYPE, B.SEARCH_CONDITION FROM USER_CONS_COLUMNS A, USER_CONSTRAINTS B WHERE A.TABLE_NAME = 'TCTG_UDL_USER_DEFINED_LOGS' AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME and constraint_type = 'C' order by 2;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	 ${ECHO} "select substr(index_name,1,3) cname, column_name, column_position from user_ind_columns where table_name='TCTG_UDL_USER_DEFINED_LOGS' order by index_name, 3;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	else
	 ${ECHO} "select TABNAME, COLNAME, TYPENAME, NULLS from SYSCAT.COLUMNS where  TABNAME = 'TCTG_UDL_USER_DEFINED_LOGS' and  TABSCHEMA = CURRENT SCHEMA  and nulls = 'N';" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	 ${ECHO} "select substr(indname,1,3) INDNAME, colnames, indextype from syscat.indexes where tabname = 'TCTG_UDL_USER_DEFINED_LOGS' and tabschema = current schema;" >> ${DB_SCRIPTS}/verifyFPMigration.sql
	fi
  else
    ${CAT} $TOP/logs/check_udl.out >>  $_ERROR_LOGS
 fi
 rm $TOP/logs/check_udl.out
 DoEndAndReportError  "COMMON_INFO_FAILED_SH modify_udl_table" "COMMON_INFO_SUCCEEDED_SH modify_udl_table"
}

create_DocStoreMaintenanceScript_PimObjects()
{
    	DoStartAndPrepare "COMMON_INFO_UPDATING_SH DocStore Maintenance Script"
	tmplog=$TOP/logs/errfile1.log;
	for CMP_CODE_COL in $COMPANIES; 
	do
		CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode%' | ${CUT} -f2 -d "%"`         
		if [ "$CMP_CODE" != "" ]; then
			#DocStore maintenance spec
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceSpec.script" 2>> $tmplog

			#Data Maintenance Spec
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DataMaintenanceSpec.script" 2>> $tmplog
			
			#DB Specific Queries
			if [ "$CCD_DB" = "db2" ]; then
			  ${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceQueries_db2.xml Cp1252 >> $tmplog
			else
			  ${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceQueries_oracle.xml Cp1252 >> $tmplog
			fi

			#DocStore maintenance lookup
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceLookup.script" 2>> $tmplog

			#Data Maintenance Lookup
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DataMaintenanceLookup.script" 2>> $tmplog
			
			#DocStore scripts
			${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/docStore/xml/docStoreMaintenanceScripts.xml Cp1252 >> $tmplog
			
			#DocStore Reports
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/DocStoreMaintenanceCreate_Report.script" 2>> $tmplog

			#Data Maintenance Code library
			${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/profile_data/xml/dataMaintenanceCodeLibrary.xml Cp1252 >> $tmplog
			#Profile data maintenance script
			${JAVA_RT} com.ibm.ccd.docstore.util.DocLoader "$CMP_CODE" Admin $TOP/src/maintenance/profile_data/xml/profileDataMaintenanceScripts.xml Cp1252 >> $tmplog
			#Profile data maintenance reports
			${JAVA_RT} com.ibm.ccd.common.interpreter.test.TestScript --companycode="$CMP_CODE" --scriptpath="${SCRIPTS_DIR}/common/report/ProfileDataMaintenanceCreate_Report.script" 2>> $tmplog

		fi            
    	done 
    	`rm -f $TOP/logs/errfile1.log `
    	DoEndAndReportError  "COMMON_INFO_FAILED_SH DocStore Maintenance Script" "COMMON_INFO_SUCCEEDED_SH DocStore Maintenance Script"
}

modifyCurrencyNode()
{
 	DoStartAndPrepare "COMMON_INFO_UPDATING_SH modifyCurrencyNode" 
 		. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/update_currencey_nodes.sql 2>> $_ERROR_LOGS  
  	DoEndAndReportError  "COMMON_INFO_FAILED_SH modifyCurrencyNode" "COMMON_INFO_SUCCEEDED_SH modifyCurrencyNode"
}

#
# 
# Call the appropriate migration functions depending upon the source version
#
#
IF4()
{
	#FP7 foreports from prev release
	modify_udl_table
	modifyCurrencyNode
	create_DocStoreMaintenanceScript_PimObjects	
}

IF3()
{
	IF4
}

FP6()
{
	IF3
}

IF2()
{

  # This change introduced in FP6 and should be implemented for IF2 and before
  if [ ${CCD_DB} == "db2" ]; then
     updateITACABColumns
  fi
  #FP6: LCK modified to DB2 Multi Dimensional Clustering or Oracle IOT
  modify_lck_table
  FP6
}

FP5()
{
  IF2
}


FP4()
{

  # This change introduced in FP5 and should be implemented for FP4 and before
  if [ ${CCD_DB} == "oracle" ]; then
     modify_wfe_index_organized
  fi

  updateNoaSpaColumns
  importScripts
  
  FP5

}

FP3()
{
  FP4 
}

FP2()
{
   #This is added in FP3. So needs to be run for all previous Fixpacks.
    putConstraints
    FP3
}


IF1()
{
    
    FP2
}

FP1()
{
    
    IF1
}
BASE()
{
    if [ ${CCD_DB} == "oracle" ]; then
    updateITACABColumns
    fi
    updateLCKIndex
    alterEntryTabbedPane
    updateCollabAttributes
    if [ ${CCD_DB} == "db2" ]; then
        dropIndexExplainObject      
    fi
    updateContainerIndex
    updateACGs
    importScripts
    updateLkpTablePrivileges
    updateAuditLogs     
    FP1
}


    

#Calling the From Version Function
if [ "${FROMVERSION}" == "BASE" ]; then
    BASE
else
    ${FROMVERSION}
fi



#
# Verification step
#

`rm -f $TOP/logs/verify.log ` 
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/verifyFPMigration.sql --errorfilter=yes > $TOP/logs/verify.log

`rm -f $DB_SCRIPTS/verifyFPMigration.sql `
