Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gosi.common.batch.util;
- import java.util.HashMap;
- import java.sql.Timestamp;
- import gosi.common.reports.beans.BatchReportConstants;
- /**
- * @CLASS NAME : BatchDriver
- * @TYPE :
- * @AUTHOR :TCS
- * @VERSION : 1
- * @EXTENDS : Nil
- * @INFORMATION : This is the calss which is called by the batch scheduler. This class process the batch id which is sent to it and
- checkes the frequency of the job if the frequency matches it calls the appropriate batch component to run the batch.
- * @IMPLEMENTS :
- * @LAST MODIFIED BY :
- * @LAST MODIFIED DATE :
- **/
- public class BatchDriver
- {
- // Flag sent by the scheduler to process only erroe or all records.
- private static boolean processErrorRecords= false;
- // flag for Is isHijMonthEndJob
- private static boolean isHijMonthEndJob = false;
- // flag for Is isGregMonthEndJob
- private static boolean isGregMonthEndJob = false;
- // flag for Is isHijYearEndJob
- private static boolean isHijYearEndJob = false;
- // flag for Is isGregYearEndJob
- private static boolean isGregYearEndJob = false;
- // flag for Is isHijMonthStartJob
- private static boolean isHijMonthStartJob = false;
- // flag for Is isHijYearStartJob
- private static boolean isHijYearStartJob = false;
- // flag to store whether the batch is runnable or not depending on the frequency
- private static boolean isRunnable = false;
- // To store the passed value in the second position is number (error flag) or date
- private static boolean isNumberEntered = false;
- /**
- * @METHOD NAME : main
- * @INFORMATION : This method is start point of any batch job
- * @PARAM : args[]
- * @RETURN : void
- * @EXCEPTION :
- * @LAST MODIFIED BY :
- * @LAST MODIFIED DATE :
- **/
- public static void main(String[] args)
- throws Exception
- {
- String batchId=null;
- boolean process = true;
- try
- {
- //this to run the batch local
- args = new String [2];
- // REG_018
- // CMN_003
- // REG_019
- args [0] = "ANN_204";
- args [1] = "30/11/2016";
- // Checking the no of parameters should not be less than 1 and should not be greater than 3
- // The parameters expected are
- // 1. Batch Id (Mandatory)
- // 2. Error Flag (Optional) if 1 means all the records if 0 means only error records if nothing is passed then taken as 1
- // 3. Date (Optional)
- // Batch id will be always in the 1st potion
- // If error flag is passed it will be always in teh 2nd position
- // If error flag is not passed and date is passed then data also can come in the second position
- if(args.length<1 || args.length>3)
- {
- // If check fails then throwing exception
- new GOSIBatchException(null,
- "BatchCore",
- "BatchDriver",
- "main",
- "CMN_001_ERR_1000",
- GOSIBatchConstants.SEVERITY_ONE);
- }
- // Taking batch id from the passed parameters
- batchId=args[0];
- // Taking the flag as PROCESS_ALL_RECORDS
- byte flag=GOSIBatchConstants.PROCESS_ALL_RECORDS;
- // Intiating GOSIBatchProperties to read the GOSIBatch.xml and store in the class
- GOSIBatchProperties batchProperties=new GOSIBatchProperties();
- // Intiating GOSIBatchSQL to read the SQL files and cache
- GOSIBatchSQL gosiBatchSQL=new GOSIBatchSQL(batchId);
- // Intiating GOSIBatchErrorMessages to read the Batch Error messages
- GOSIBatchErrorMessages gosiBatchErrMessages =
- new GOSIBatchErrorMessages(batchId);
- // Declaring the GOSIBatchUtilities class object
- GOSIBatchUtilities gosiBatchUtilities =null;
- // Creating a switch statment depending on the no of parameters to read the parameters
- switch(args.length)
- {
- // If no of parameters are 2
- // 1st will be batch id which is taken already
- // So we need to check the 2nd parameter whether it is data or the error flag
- case 2:
- // Checking whether the second parameter is number or not
- if(isNumberEntered(args[1]))
- {
- // IF no then taking tha value as erroe flag
- flag = Byte.parseByte(args[1]);
- processErrorRecords = true;
- }
- else
- {
- // If the not number then taking that value as Date of run
- GOSIBatchDateUtilities.validateGregorianDate(args[1]);
- // Updating the passed value in the GOSIBatchUtilities.setCurrentSystemTime
- // SO that it will not take the date from System.tim file
- GOSIBatchUtilities.setCurrentSystemTime(GOSIBatchUtilities.
- getTimestamp(args[1]));
- }
- break;
- // If the no of parameters or 3
- case 3:
- String arg2=args[1];
- String arg3=args[2];
- // Taking the flag from second position if it is no if not throwing exception
- if(isNumberEntered(arg2))
- {
- flag = Byte.parseByte(args[1]);
- processErrorRecords = true;
- }
- else
- {
- System.out.println("USAGE : java gosi.common.batch." +
- "util.BatchDriver <BatchID> [Flag/Date] [DATE]");
- System.exit(0);
- }
- // Taking the date from 3rd position and setting in the GOSIBatchUtilities.setCurrentSystemTime
- GOSIBatchDateUtilities.validateGregorianDate(args[2]);
- GOSIBatchUtilities.setCurrentSystemTime(GOSIBatchUtilities.
- getTimestamp(args[2]));
- break;
- }
- try
- {
- // Creating the gosiBatchUtilities by passing batchId
- // This does the following things
- // Gets the batch master details from T_BATCHMASTER like
- // BATCHID,BATCHDESCRIPTION,NUMOFPARAM,COMMITFREQUENCY,PROCESSORNAME,BATCHTYPE,BUSINESSERRORFLAG,BATCHRUNFREQUENCY
- // If batch type is ADHOC and if it requires paraameters then there can be more than one request so it will get even the no of requests
- // As well it gets the system user and and system role id
- // All the values will be loaded in the BatchMaster object of the GOSIBatchUtilities.
- gosiBatchUtilities =
- new GOSIBatchUtilities(batchId);
- }
- catch(GOSIBatchException ge)
- {
- System.out.println("equals(ge.getErrorId()"+ge.getErrorId());
- // CMN_001_ERR_1065 is thrown when there are no request found for the adhoc batch
- // So instead of throwing exception which will show it as failed in Scheduler simply returning
- if("CMN_001_ERR_1065".equals(ge.getErrorId()))
- {
- return;
- }
- throw ge;
- }
- // Geting the frequncy from the BatchMaster loaded in the GOSIBatchUtlities
- String frequencies = gosiBatchUtilities.getCurrentBatchRunFrequency(
- );
- // Setting all the conditions check the method to know what it does
- setAllConditions(frequencies);
- // Checking the system date with the flag if both are true then making the flag as true else flase
- if(isHijMonthEndJob && GOSIBatchDateUtilities.isHijraMonthEnd())
- {
- isHijMonthEndJob = true;
- }
- else
- {
- isHijMonthEndJob = false;
- }
- if(isHijYearEndJob && GOSIBatchDateUtilities.isHijraYearEnd())
- {
- isHijYearEndJob = true;
- }
- else
- {
- isHijYearEndJob = false;
- }
- if(isGregMonthEndJob && GOSIBatchDateUtilities.isGregorianMonthEnd()
- )
- {
- isGregMonthEndJob = true;
- }
- else
- {
- isGregMonthEndJob = false;
- }
- if(isGregYearEndJob && GOSIBatchDateUtilities.isGregorianYearEnd())
- {
- isGregYearEndJob = true;
- }
- else
- {
- isGregYearEndJob = false;
- }
- if(isHijYearStartJob && GOSIBatchDateUtilities.isHijYearStart())
- {
- isHijYearStartJob = true;
- }
- else
- {
- isHijYearStartJob = false;
- }
- if(isHijMonthStartJob && GOSIBatchDateUtilities.isHijMonthStart())
- {
- isHijMonthStartJob = true;
- }
- else
- {
- isHijMonthStartJob = false;
- }
- // Creating the batchReportConstants to read the batch reports properties xml
- BatchReportConstants batchReportConstants = new
- BatchReportConstants();
- // Creating the object of BatchProcessController
- BatchProcessController batchProcessController = null;
- // if the frequency is not null
- // Then doing the following things
- if(frequencies!=null)
- {
- System.out.println("is daily JOb :"+isRunnable);
- // If the job is isHijMonthEndJob or isHijYearEndJob or isGregMonthEndJob or isGregYearEndJob
- if(isHijMonthEndJob || isHijYearEndJob ||
- isGregMonthEndJob || isGregYearEndJob)
- {
- System.out.println("Batch running for any of end frequency setting ");
- process = false;
- // Creating the hashmap for the constructor parameter
- HashMap hashMap = new HashMap();
- hashMap.put("isBatchFlag", new Byte(flag));
- hashMap.put("isHijMonthEndJob",
- new Boolean(isHijMonthEndJob));
- hashMap.put("isHijYearEndJob",
- new Boolean(isHijYearEndJob));
- hashMap.put("isGregMonthEndJob",
- new Boolean(isGregMonthEndJob));
- hashMap.put("isGregYearEndJob",
- new Boolean(isGregYearEndJob));
- // Passing the batch is and hash map for getting the application process controller
- batchProcessController=
- GOSIBatchUtilities.getProcessor
- (batchId, hashMap);
- }
- // If processErrorRecords is true or isRunnable is true or isHijMonthStartJob or isHijYearStartJob
- else if(processErrorRecords || isRunnable ||
- isHijMonthStartJob || isHijYearStartJob)
- {
- System.out.println("Batch running process error records or others");
- // Getting the application process controller by passing batch id and the flag
- batchProcessController=
- GOSIBatchUtilities.getProcessor
- (batchId, flag);
- }
- else
- {
- // If non of them throwing exception
- System.out.println("Batch run is not fulfilled its " +
- "criteria ");
- return;
- }
- }
- else
- {
- System.out.println("Batch running for Null frequency setting ");
- // If rfquency is null the getting the application process controler by passing batch id and flag
- batchProcessController=
- GOSIBatchUtilities.getProcessor
- (batchId, flag);
- }
- // IF flag is all records then calling GOSIBatchUtilities.isAccidentalRerun to check whether the batch has run for this date already
- if(flag==GOSIBatchConstants.PROCESS_ALL_RECORDS)
- {
- // If accdiental run then exception will be thrown
- GOSIBatchUtilities.isAccidentalRerun(batchProcessController);
- }
- // This is to call the invokeBatchService in the application process controller which will do the required batch processing
- boolean success = false;
- try
- {
- // This method disable the online links which are dependent on this batch
- GOSIBatchUtilities.disableOnlineMenu(batchId);
- success = batchProcessController.invokeBatchService();
- }
- catch (Exception e)
- {
- throw e;
- }
- finally{
- GOSIBatchUtilities.enableOnlineMenu(batchId);
- }
- // Enabling the online links which are disabled
- // Updating the run isntacs and as succss and last successful date
- GOSIBatchUtilities.updateRunInstance(batchId);
- System.out.println(":success "+success);
- if(!success)
- {
- // If not succss then setting the status as un successful
- GOSIBatchUtilities.completeProcess(batchId, GOSIBatchConstants.
- BATCH_UN_SUCCESSFULY_COMPLETED);
- throw new Exception();
- }
- else
- {
- // If the flag is GOSIBatchConstants.PROCESS_ALL_RECORDS then setting the restart key as null once the batch is run succssfully
- if(flag==GOSIBatchConstants.PROCESS_ALL_RECORDS)
- {
- GOSIBatchUtilities.updateRestartKeys(batchId);
- }
- // Setting the process as completed successfully
- GOSIBatchUtilities.completeProcess(batchId, GOSIBatchConstants.
- BATCH_SUCCESSFULY_COMPLETED);
- }
- System.out.println("Commiting all the records successfully ");
- System.out.println("Batch master business Flag : "+
- batchProcessController.getBatchMaster().getBusinessErrorFlag());
- //Checking is there are any error records in the batch and if the error flag is 1 in the T_BATCHMASTER
- // then throwing the exception so that the dependent jobs will not run
- if((batchProcessController.hasBusinessError()) &&
- (batchProcessController.getBatchMaster().getBusinessErrorFlag()==1))
- {
- System.out.println("Throwing the business Exception ");
- throw new Exception();
- }
- }
- catch(GOSIBatchException ge)
- {
- // If any exception enabling the menus for the batch
- //GOSIBatchUtilities.enableOnlineMenu(batchId);
- throw new Exception();
- }
- }
- /**
- * @METHOD NAME : setAllConditions
- * @INFORMATION : This method sets various batch frequencies
- at batch driver so that, batch driver will
- find whether batch is needed to run or not
- * @PARAM : frequencies
- * @RETURN : void
- * @EXCEPTION :
- * @LAST MODIFIED BY :
- * @LAST MODIFIED DATE :
- **/
- public static void setAllConditions(String frequencies)
- {
- if(frequencies!=null&& (!(frequencies.length()<12)))
- {
- if("1".equals(frequencies.substring(0,1)))
- {
- processErrorRecords = true;
- }
- if("1".equals(frequencies.substring(1,2)) ||
- "1".equals(frequencies.substring(2,3)) ||
- "1".equals(frequencies.substring(8,9)) ||
- "1".equals(frequencies.substring(10,11)) ||
- "1".equals(frequencies.substring(11,12)))
- {
- isRunnable = true;
- }
- // Checking HijMonthEnd,HijYearEnd ,GregMonthEnd,GregYearEnd,HijMonthStart and HijYearStart positon
- // And setting those values as true
- if("1".equals(frequencies.substring(3,4)))
- {
- isHijMonthEndJob = true;
- }
- if("1".equals(frequencies.substring(4,5)))
- {
- isHijYearEndJob = true;
- }
- if("1".equals(frequencies.substring(5,6)))
- {
- isGregMonthEndJob = true;
- }
- if("1".equals(frequencies.substring(6,7)))
- {
- isGregYearEndJob = true;
- }
- if("1".equals(frequencies.substring(7,8)))
- {
- isHijMonthStartJob = true;
- }
- if("1".equals(frequencies.substring(9,10)))
- {
- isHijYearStartJob = true;
- }
- }
- }
- /**
- * @METHOD NAME : setAllProcess
- * @INFORMATION : This method sets various batch frequencies
- at batch driver so that, batch driver will
- find whether batch is needed to run or not
- * @PARAM : frequencies
- * @RETURN : void
- * @EXCEPTION :
- * @LAST MODIFIED BY :
- * @LAST MODIFIED DATE :
- **/
- public static void setAllProcess(int value)
- {
- int initial = 4;
- int curValue =0;
- for(int i =initial;i>=0;i--)
- {
- curValue=(int)Math.pow((double)2, (double)i);
- if(value>=curValue)
- {
- value-=curValue;
- switch(i)
- {
- case 0:
- processErrorRecords = true;
- break;
- case 1:
- isHijMonthEndJob = true;
- break;
- case 2:
- isGregMonthEndJob = true;
- break;
- case 3:
- isHijYearEndJob = true;
- break;
- case 4:
- isGregYearEndJob = true;
- break;
- default:
- break;
- }
- }
- }
- }
- /**
- * @METHOD NAME : isNumberEntered
- * @INFORMATION : This method checks whether passed value is number or not
- * @PARAM : frequencies
- * @RETURN : void
- * @EXCEPTION :
- * @LAST MODIFIED BY :
- * @LAST MODIFIED DATE :
- **/
- private static boolean isNumberEntered(String value)
- throws GOSIBatchException
- {
- boolean check= true;
- try
- {
- int val= Integer.parseInt(value);
- isNumberEntered =true;
- }catch(Exception e)
- {
- check=false;
- }
- return check ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement