call D:\Loyalty_Scheduler\Shared\SetVariables.cmd
@echo off
setLocal EnableDelayedExpansion

REM AUTHOR - Veena Veeravalli - 07/28/2015

set /a BATCH_NUMS_PER_TASK=50
set /a TASKS_PER_SERVER=5
set /a MIN_BATCH_NUM=6001

FOR /F "tokens=1,2,3,4 delims=/- " %%a in ('date/T') do set CDATE=%%b%%c%%d
FOR /F "tokens=1,2,3,4 delims=:., " %%a in ("%TIME%") do set CTIME=%%a%%b
echo Transaction Import process started at %CDATE% %CTIME% > \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log

echo Batch Numbers per EIM task %BATCH_NUMS_PER_TASK% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
echo Maximum EIM tasks per Server %TASKS_PER_SERVER% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
echo Starting Batch number is %MIN_BATCH_NUM% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log

echo Executing Prerequisite Checks for Starting EIM IMPORT PROCESS >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log

echo Checking if there are any DUP_RECORD_EXISTS or IMPORTED status records in the Transaction EIM table >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log

%SOLAR_INSTALL%\siebsrvr\bin\odbcsql /s %SOLAR_ODBC% /u %SOLAR_SIEBEL_UID% /p %SOLAR_SIEBEL_PWD% D:\Loyalty_Scheduler\Transaction_Purge\EIM\txn_importedstatesql.inp /o D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_importedstateoutput.txt /h
rem Determine the line number of the next to last line  
For /F %%A In ('Find /C /V "" ^< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_importedstateoutput.txt') Do Set /A _ = %%A - 1
rem Read last line  
More /E +%_% D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_importedstateoutput.txt > D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_importedstatecount.txt
set /p txn_importedstatecount= < D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_importedstatecount.txt
echo %txn_importedstatecount% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
if %txn_importedstatecount% NEQ 0 goto EIM_ERROR

echo Checking if there are any records to import in the EIM_LOY_TXN table >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
%SOLAR_INSTALL%\siebsrvr\bin\odbcsql /s %SOLAR_ODBC% /u %SOLAR_SIEBEL_UID% /p %SOLAR_SIEBEL_PWD% D:\Loyalty_Scheduler\Transaction_Purge\EIM\txn_forimportstatesql.inp /o D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_forimportstateoutput.txt /h
For /F %%A In ('Find /C /V "" ^< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_forimportstateoutput.txt') Do Set /A _ = %%A - 1
More /E +%_% D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_forimportstateoutput.txt > D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_forimportstatecount.txt
set /p txn_forimportstatecount= < D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_forimportstatecount.txt
echo %txn_forimportstatecount% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
if %txn_forimportstatecount% == 0 goto NO_RECORDS_TO_IMPORT

:CHECK_BATCH_RANGE
echo Checking how many batch numbers are used in the EIM table >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
%SOLAR_INSTALL%\siebsrvr\bin\odbcsql /s %SOLAR_ODBC% /u %SOLAR_SIEBEL_UID% /p %SOLAR_SIEBEL_PWD% D:\Loyalty_Scheduler\Transaction_Purge\EIM\txn_countbatchnum.inp /o D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_countbatchnumoutput.txt /h
For /F %%A In ('Find /C /V "" ^< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_countbatchnumoutput.txt') Do Set /A _ = %%A - 1
More /E +%_% D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_countbatchnumoutput.txt > D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_countbatchnum.txt
set /p max_batch_num=< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\txn_countbatchnum.txt
echo Total Batch Numbers Used is %max_batch_num% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
if %max_batch_num% LEQ 0 goto BATCH_ERROR


:CHECK_AVAILABLE_SERVERS
echo Checking how many servers are allocated to run the EIM process >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log

set "file=D:\Loyalty_Scheduler\Transaction_Purge\EIM\available_servers.txt"
set /a i=0
set /a server_count=0

for /F "usebackq delims=" %%a in ("%file%") do (
set /a i+=1
set servers[!i!]=%%a
set server_count=!i!
)
echo Number of available servers to run EIM process %server_count% >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
if %server_count% == 0 goto SERVER_ERROR


:RUN_TRANSACTION_EIM
echo Running the EIM process for transactions >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
set /a l=1
set /a n=%MIN_BATCH_NUM%-1
:LOOP
	set /a task_counter=0
	for /L %%k in (1,1,%server_count%) do (
		%SOLAR_INSTALL%\siebsrvr\BIN\srvrmgr /u %SOLAR_ADMIN_UID% /p %SOLAR_ADMIN_PWD% /e %SOLAR_ENTERPRISE% /g %SOLAR_GATEWAY% /s !servers[%%k]! /c "list active tasks for component eim" > D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\num_tasks_on_server%%k.TXT
		for /f %%c in ('find /C "Running" ^< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\num_tasks_on_server%%k.TXT') do set tasksrunning=%%c

		echo EIM Tasks running on server !servers[%%k]! is !tasksrunning! >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
		if !tasksrunning! LSS %TASKS_PER_SERVER% (
			set /a n=n+%BATCH_NUMS_PER_TASK%
			if not !n! GEQ %max_batch_num% (
				start /b %SOLAR_INSTALL%\siebsrvr\bin\srvrmgr /g %SOLAR_GATEWAY% /e %SOLAR_ENTERPRISE% /s !servers[%%k]! /u %SOLAR_ADMIN_UID% /p %SOLAR_ADMIN_PWD% /c "run task for component eim with config=import_summary_txns.ifb,SQLFLAGS=8, TRACEFLAGS=1, ERRORFLAGS=1, ExtendedParams="""batchnum=!MIN_BATCH_NUM!-!n!"""" /o \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\EIM_Task_!servers[%%k]!_!l!_%CDATE%_%CTIME%.log
				echo Server !servers[%%k]! is running the Task !l! with batch range between !MIN_BATCH_NUM! and !n! >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
				set /a l+=1
				set /a MIN_BATCH_NUM=!n!+1
			)
			if !n! GEQ %max_batch_num% (
				echo Processing last batch - Transactions >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
				set /a n=!n!-%BATCH_NUMS_PER_TASK%
				set /a m=%max_batch_num%-!n!
				set /a n=!n!+!m!
				start /b %SOLAR_INSTALL%\siebsrvr\bin\srvrmgr /g %SOLAR_GATEWAY% /e %SOLAR_ENTERPRISE% /s !servers[%%k]! /u %SOLAR_ADMIN_UID% /p %SOLAR_ADMIN_PWD% /c "run task for component eim with config=import_summary_txns.ifb,SQLFLAGS=8, TRACEFLAGS=1, ERRORFLAGS=1, ExtendedParams="""batchnum=!MIN_BATCH_NUM!-!n!"""" /o \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\EIM_Task_!servers[%%k]!_!l!_%CDATE%_%CTIME%.log
				echo Server !servers[%%k]! is running the Task !l! with batch range between !MIN_BATCH_NUM! and !n! >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
				set /a l+=1
				set /a MIN_BATCH_NUM=!n!+1

				goto EIMS_EXITED_CHECK
			)
		)
		if not !tasksrunning! LSS %TASKS_PER_SERVER% (
			set /a task_counter+=1
		)
		if !task_counter!==%server_count% (
			echo waiting for 60 seconds >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
			timeout 60
		)
	)
echo waiting for 5 seconds >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
timeout 5
goto LOOP

:EIMS_EXITED_CHECK
FOR /F "tokens=1,2,3,4 delims=/- " %%a in ('date/T') do set CDATE1=%%b%%c%%d
FOR /F "tokens=1,2,3,4 delims=:., " %%a in ("%TIME%") do set CTIME1=%%a%%b
echo Creating EIM tasks ended at %CDATE1% %CTIME1%. But some of the EIM tasks might be still running. >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
timeout 120
%SOLAR_INSTALL%\siebsrvr\BIN\srvrmgr /u %SOLAR_ADMIN_UID% /p %SOLAR_ADMIN_PWD% /e %SOLAR_ENTERPRISE% /g %SOLAR_GATEWAY% /c "list tasks for component eim" > D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\list_tasks.TXT
for /f %%c in ('find /C "Exited" ^< D:\Loyalty_Scheduler\Transaction_Purge\EIM\work\list_tasks.TXT ') do set eim_tasks_exited=%%c
echo !eim_tasks_exited! EIM tasks exited with error. >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
goto EndofFile

:EIM_ERROR
echo EIM Process is aborted as Data in EIM table is in unstable state. Require Manual Intervention.  >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
goto EndofFile

:NO_RECORDS_TO_IMPORT
echo There are no records to import in EIM_LOY_TXN table. >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
goto EndofFile


:BATCH_ERROR
echo Batch numbers are wonky >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
goto EndofFile

:SERVER_ERROR
echo No servers are available to run the EIM process >> \\%SOLAR_FILESYSTEM%\siebelfs\batchlogs\IMPORT_TRANSACTIONS_%CDATE%_%CTIME%.log
goto EndofFile

:EndofFile
