Advertisement
dahomee_69

BatchDriver

Dec 21st, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.32 KB | None | 0 0
  1.  
  2. package gosi.common.batch.util;
  3. import java.util.HashMap;
  4. import java.sql.Timestamp;
  5. import gosi.common.reports.beans.BatchReportConstants;
  6.  
  7. /**
  8.  * @CLASS NAME               : BatchDriver
  9.  * @TYPE                     :
  10.  * @AUTHOR                  :TCS
  11.  * @VERSION                 : 1
  12.  * @EXTENDS                 : Nil
  13.  * @INFORMATION             : This is the calss which is called by the batch scheduler. This class process the batch id which is sent to it and
  14.                             checkes the frequency of the job if the frequency matches it calls the appropriate batch component to run the batch.
  15.  * @IMPLEMENTS              :
  16.  * @LAST MODIFIED BY        :
  17.  * @LAST MODIFIED DATE      :
  18.  **/
  19.  
  20.  
  21. public class BatchDriver
  22. {
  23.     // Flag sent by the scheduler to process only erroe or all records.
  24.     private static boolean processErrorRecords= false;
  25.     // flag for Is isHijMonthEndJob
  26.     private static boolean isHijMonthEndJob = false;
  27.     // flag for Is isGregMonthEndJob
  28.     private static boolean isGregMonthEndJob = false;
  29.     // flag for Is isHijYearEndJob
  30.     private static boolean isHijYearEndJob = false;
  31.     // flag for Is isGregYearEndJob
  32.     private static boolean isGregYearEndJob = false;
  33.     // flag for Is isHijMonthStartJob
  34.     private static boolean isHijMonthStartJob = false;
  35.     // flag for Is isHijYearStartJob
  36.     private static boolean isHijYearStartJob = false;
  37.     // flag to store whether the batch is runnable or not depending on the frequency
  38.     private static boolean isRunnable = false;
  39.     // To store the passed value in the second position is number (error flag) or date
  40.     private static boolean isNumberEntered = false;
  41.  
  42. /**
  43.  * @METHOD NAME              : main
  44.  * @INFORMATION              : This method is start point of any batch job
  45.  * @PARAM                    : args[]
  46.  * @RETURN                   : void
  47.  * @EXCEPTION                :
  48.  * @LAST MODIFIED BY         :
  49.  * @LAST MODIFIED DATE       :
  50. **/
  51.  
  52.     public static void main(String[] args)
  53.     throws Exception
  54.     {
  55.         String batchId=null;
  56.         boolean process = true;
  57.         try
  58.         {
  59.             //this to run the batch local
  60.             args = new String [2];
  61.    //        REG_018
  62.   //      CMN_003
  63.   //    REG_019
  64.             args [0] = "ANN_204";
  65.             args [1] = "30/11/2016";
  66.             // Checking the no of parameters should not be less than 1 and should not be greater than 3
  67.             // The parameters expected are
  68.             // 1. Batch Id (Mandatory)
  69.             // 2. Error Flag (Optional) if 1 means all the records if 0 means only error records if nothing is passed then taken as 1
  70.             // 3. Date (Optional)
  71.             // Batch id will be always in the 1st potion
  72.             // If error flag is passed it will be always in teh 2nd position
  73.             // If error flag is not passed and date is passed then data also can come in the second position
  74.             if(args.length<1 || args.length>3)
  75.             {
  76.                 // If check fails then throwing exception
  77.                      new GOSIBatchException(null,
  78.                                 "BatchCore",
  79.                                 "BatchDriver",
  80.                                 "main",
  81.                                 "CMN_001_ERR_1000",
  82.                                 GOSIBatchConstants.SEVERITY_ONE);
  83.             }
  84.             // Taking batch id from the passed parameters
  85.             batchId=args[0];
  86.             // Taking the flag as PROCESS_ALL_RECORDS
  87.             byte flag=GOSIBatchConstants.PROCESS_ALL_RECORDS;
  88.             // Intiating GOSIBatchProperties to read the GOSIBatch.xml and store in the class
  89.             GOSIBatchProperties batchProperties=new GOSIBatchProperties();
  90.             // Intiating GOSIBatchSQL to read the SQL files and cache
  91.             GOSIBatchSQL gosiBatchSQL=new GOSIBatchSQL(batchId);
  92.             // Intiating GOSIBatchErrorMessages to read the Batch Error messages
  93.            GOSIBatchErrorMessages gosiBatchErrMessages =
  94.                     new GOSIBatchErrorMessages(batchId);
  95.             // Declaring the GOSIBatchUtilities class object
  96.             GOSIBatchUtilities gosiBatchUtilities =null;
  97.             // Creating a switch statment depending on the no of parameters to read the parameters
  98.             switch(args.length)
  99.             {
  100.                 // If no of parameters are 2
  101.                 // 1st will be batch id which is taken already
  102.                 // So we need to check the 2nd parameter whether it is data or the error flag
  103.                 case 2:
  104.                 // Checking whether the second parameter is number or not
  105.                 if(isNumberEntered(args[1]))
  106.                 {
  107.                     // IF no then taking tha value as erroe flag
  108.                        flag = Byte.parseByte(args[1]);
  109.                        processErrorRecords = true;
  110.                 }
  111.                 else
  112.                 {
  113.                     // If the not number then taking that value as Date of run
  114.                     GOSIBatchDateUtilities.validateGregorianDate(args[1]);
  115.                     // Updating the passed value in the GOSIBatchUtilities.setCurrentSystemTime
  116.                     // SO that it will not take the date from System.tim file
  117.                     GOSIBatchUtilities.setCurrentSystemTime(GOSIBatchUtilities.
  118.                         getTimestamp(args[1]));
  119.                 }
  120.                 break;
  121.                 // If the no of parameters or 3
  122.                 case 3:
  123.                       String arg2=args[1];
  124.                     String arg3=args[2];
  125.                     // Taking the flag from second position if it is no if not throwing exception
  126.                     if(isNumberEntered(arg2))
  127.                     {
  128.                         flag = Byte.parseByte(args[1]);
  129.                         processErrorRecords = true;
  130.                     }
  131.                     else
  132.                     {
  133.                         System.out.println("USAGE : java gosi.common.batch." +
  134.                             "util.BatchDriver <BatchID> [Flag/Date] [DATE]");
  135.                         System.exit(0);
  136.                     }
  137.                     // Taking the date from 3rd position and setting in the GOSIBatchUtilities.setCurrentSystemTime
  138.                     GOSIBatchDateUtilities.validateGregorianDate(args[2]);
  139.                     GOSIBatchUtilities.setCurrentSystemTime(GOSIBatchUtilities.
  140.                         getTimestamp(args[2]));
  141.                     break;
  142.             }
  143.  
  144.             try
  145.             {
  146.                 // Creating the gosiBatchUtilities by passing batchId
  147.                 // This does the following things
  148.                 // Gets the batch master details from T_BATCHMASTER like
  149.                 // BATCHID,BATCHDESCRIPTION,NUMOFPARAM,COMMITFREQUENCY,PROCESSORNAME,BATCHTYPE,BUSINESSERRORFLAG,BATCHRUNFREQUENCY
  150.                 // 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
  151.                 // As well it gets the system user and and system role id
  152.                 // All the values will be loaded in the BatchMaster object of the GOSIBatchUtilities.
  153.                 gosiBatchUtilities =
  154.                     new GOSIBatchUtilities(batchId);
  155.             }
  156.             catch(GOSIBatchException ge)
  157.             {
  158.                 System.out.println("equals(ge.getErrorId()"+ge.getErrorId());
  159.                 // CMN_001_ERR_1065 is thrown when there are no request found for the adhoc batch
  160.                 // So instead of throwing exception which will show it as failed in Scheduler simply returning
  161.                 if("CMN_001_ERR_1065".equals(ge.getErrorId()))
  162.                 {
  163.                     return;
  164.                 }
  165.                 throw ge;
  166.             }
  167.             // Geting the frequncy from the BatchMaster loaded in the GOSIBatchUtlities
  168.              String frequencies = gosiBatchUtilities.getCurrentBatchRunFrequency(
  169.                 );
  170.             // Setting all the conditions check the method to know what it does
  171.             setAllConditions(frequencies);
  172.  
  173.             // Checking the system date with the flag if both are true then making the flag as true else flase
  174.             if(isHijMonthEndJob && GOSIBatchDateUtilities.isHijraMonthEnd())
  175.             {
  176.                 isHijMonthEndJob = true;
  177.             }
  178.             else
  179.             {
  180.                 isHijMonthEndJob = false;
  181.             }
  182.             if(isHijYearEndJob && GOSIBatchDateUtilities.isHijraYearEnd())
  183.             {
  184.                 isHijYearEndJob = true;
  185.             }
  186.             else
  187.             {
  188.                 isHijYearEndJob = false;
  189.             }
  190.  
  191.             if(isGregMonthEndJob && GOSIBatchDateUtilities.isGregorianMonthEnd()
  192.                 )
  193.             {
  194.                 isGregMonthEndJob = true;
  195.             }
  196.             else
  197.             {
  198.                 isGregMonthEndJob = false;
  199.             }
  200.  
  201.             if(isGregYearEndJob && GOSIBatchDateUtilities.isGregorianYearEnd())
  202.             {
  203.                 isGregYearEndJob = true;
  204.             }
  205.             else
  206.             {
  207.                 isGregYearEndJob = false;
  208.             }
  209.             if(isHijYearStartJob && GOSIBatchDateUtilities.isHijYearStart())
  210.             {
  211.                 isHijYearStartJob = true;
  212.             }
  213.             else
  214.             {
  215.                 isHijYearStartJob = false;
  216.             }
  217.             if(isHijMonthStartJob && GOSIBatchDateUtilities.isHijMonthStart())
  218.             {
  219.                 isHijMonthStartJob = true;
  220.             }
  221.             else
  222.             {
  223.                 isHijMonthStartJob = false;
  224.             }
  225.  
  226.             // Creating the batchReportConstants  to read the batch reports properties xml
  227.             BatchReportConstants batchReportConstants = new
  228.                 BatchReportConstants();
  229.             // Creating the object of BatchProcessController
  230.             BatchProcessController batchProcessController = null;
  231.             // if the frequency is not null
  232.             // Then doing the following things
  233.             if(frequencies!=null)
  234.             {
  235.                 System.out.println("is daily JOb  :"+isRunnable);
  236.                 // If the job is isHijMonthEndJob or isHijYearEndJob or isGregMonthEndJob or isGregYearEndJob
  237.                 if(isHijMonthEndJob || isHijYearEndJob ||
  238.                 isGregMonthEndJob || isGregYearEndJob)
  239.                 {
  240.                 System.out.println("Batch running for any of end frequency setting ");
  241.                    
  242.                     process = false;
  243.                     // Creating the hashmap for the constructor parameter
  244.                     HashMap hashMap = new HashMap();
  245.                     hashMap.put("isBatchFlag", new Byte(flag));
  246.                     hashMap.put("isHijMonthEndJob",
  247.                         new Boolean(isHijMonthEndJob));
  248.                     hashMap.put("isHijYearEndJob",
  249.                         new Boolean(isHijYearEndJob));
  250.                     hashMap.put("isGregMonthEndJob",
  251.                         new Boolean(isGregMonthEndJob));
  252.                     hashMap.put("isGregYearEndJob",
  253.                         new Boolean(isGregYearEndJob));
  254.                     // Passing the batch is and hash map for getting the application process controller
  255.                     batchProcessController=
  256.                             GOSIBatchUtilities.getProcessor
  257.                             (batchId, hashMap);
  258.                 }
  259.                 // If processErrorRecords is true or isRunnable is true or isHijMonthStartJob or isHijYearStartJob
  260.                 else if(processErrorRecords || isRunnable ||
  261.                 isHijMonthStartJob || isHijYearStartJob)
  262.                 {
  263.                 System.out.println("Batch running process error records or others");
  264.                 // Getting the application process controller by passing batch id and the flag
  265.                 batchProcessController=
  266.                             GOSIBatchUtilities.getProcessor
  267.                             (batchId, flag);
  268.                 }
  269.                 else
  270.                 {
  271.                     // If non of them throwing exception
  272.                     System.out.println("Batch run is not fulfilled its " +
  273.                         "criteria ");
  274.                     return;
  275.                 }
  276.             }
  277.             else
  278.             {
  279.                 System.out.println("Batch running for Null frequency setting ");
  280.                 // If rfquency is null the getting the application process controler by passing batch id and flag
  281.                     batchProcessController=
  282.                             GOSIBatchUtilities.getProcessor
  283.                             (batchId, flag);
  284.             }
  285.             // IF flag is all records then calling GOSIBatchUtilities.isAccidentalRerun to check whether the batch has run for this date already
  286.             if(flag==GOSIBatchConstants.PROCESS_ALL_RECORDS)
  287.             {
  288.                 // If accdiental run then exception will be thrown
  289.                 GOSIBatchUtilities.isAccidentalRerun(batchProcessController);
  290.             }
  291.  
  292.             // This is to call the invokeBatchService in the application process controller which will do the required batch processing
  293.             boolean success = false;
  294.             try
  295.             {
  296.             // This method disable the online links which are dependent on this batch
  297.             GOSIBatchUtilities.disableOnlineMenu(batchId);
  298.             success = batchProcessController.invokeBatchService();
  299.             }
  300.             catch (Exception e)
  301.             {
  302.             throw e;
  303.             }
  304.             finally{
  305.             GOSIBatchUtilities.enableOnlineMenu(batchId);
  306.             }
  307.             // Enabling the online links which are disabled
  308.             // Updating the run isntacs and as succss and last successful date
  309.             GOSIBatchUtilities.updateRunInstance(batchId);
  310.             System.out.println(":success "+success);
  311.             if(!success)
  312.             {
  313.                 // If not succss then setting the status as un successful
  314.                 GOSIBatchUtilities.completeProcess(batchId, GOSIBatchConstants.
  315.                 BATCH_UN_SUCCESSFULY_COMPLETED);
  316.                 throw new Exception();
  317.             }
  318.             else
  319.             {
  320.                 // If the flag is GOSIBatchConstants.PROCESS_ALL_RECORDS then setting the restart key as null once the batch is run succssfully
  321.                 if(flag==GOSIBatchConstants.PROCESS_ALL_RECORDS)
  322.                 {
  323.                     GOSIBatchUtilities.updateRestartKeys(batchId);
  324.                 }
  325.                 // Setting the process as completed successfully
  326.                 GOSIBatchUtilities.completeProcess(batchId, GOSIBatchConstants.
  327.                 BATCH_SUCCESSFULY_COMPLETED);
  328.             }
  329.  
  330.             System.out.println("Commiting all the records successfully ");
  331.  
  332.             System.out.println("Batch master business Flag : "+
  333.             batchProcessController.getBatchMaster().getBusinessErrorFlag());
  334.             //Checking is there are any error records in the batch and if the error flag is 1 in the T_BATCHMASTER
  335.             // then throwing the exception so that the dependent jobs will not run
  336.             if((batchProcessController.hasBusinessError()) &&
  337.             (batchProcessController.getBatchMaster().getBusinessErrorFlag()==1))
  338.             {
  339.                 System.out.println("Throwing the business Exception ");
  340.                 throw new Exception();
  341.             }  
  342.         }  
  343.         catch(GOSIBatchException ge)
  344.         {
  345.             // If any exception enabling the menus for the batch
  346.             //GOSIBatchUtilities.enableOnlineMenu(batchId);
  347.             throw new Exception();
  348.         }
  349.     }
  350.  
  351. /**
  352.  * @METHOD NAME              : setAllConditions
  353.  * @INFORMATION              : This method sets various batch frequencies  
  354.                                at batch driver so that, batch driver will
  355.                                find whether batch is needed to run or not
  356.  * @PARAM                    : frequencies
  357.  * @RETURN                   : void
  358.  * @EXCEPTION                :
  359.  * @LAST MODIFIED BY         :
  360.  * @LAST MODIFIED DATE       :
  361. **/
  362.  
  363.     public static void setAllConditions(String frequencies)
  364.     {
  365.         if(frequencies!=null&& (!(frequencies.length()<12)))
  366.         {
  367.             if("1".equals(frequencies.substring(0,1)))
  368.             {
  369.                 processErrorRecords = true;
  370.             }
  371.  
  372.  
  373.             if("1".equals(frequencies.substring(1,2)) ||
  374.             "1".equals(frequencies.substring(2,3)) ||
  375.             "1".equals(frequencies.substring(8,9)) ||
  376.             "1".equals(frequencies.substring(10,11)) ||
  377.             "1".equals(frequencies.substring(11,12)))
  378.             {
  379.                 isRunnable = true;
  380.             }
  381.             // Checking HijMonthEnd,HijYearEnd ,GregMonthEnd,GregYearEnd,HijMonthStart and HijYearStart positon
  382.             // And setting those values as true
  383.             if("1".equals(frequencies.substring(3,4)))
  384.             {
  385.                 isHijMonthEndJob = true;
  386.             }
  387.             if("1".equals(frequencies.substring(4,5)))
  388.             {
  389.                 isHijYearEndJob = true;
  390.             }
  391.             if("1".equals(frequencies.substring(5,6)))
  392.             {
  393.                 isGregMonthEndJob = true;
  394.             }
  395.             if("1".equals(frequencies.substring(6,7)))
  396.             {
  397.                 isGregYearEndJob = true;
  398.             }
  399.             if("1".equals(frequencies.substring(7,8)))
  400.             {
  401.                 isHijMonthStartJob = true;
  402.             }
  403.             if("1".equals(frequencies.substring(9,10)))
  404.             {
  405.                 isHijYearStartJob = true;
  406.             }
  407.         }
  408.     }
  409. /**
  410.  * @METHOD NAME              : setAllProcess
  411.  * @INFORMATION              : This method sets various batch frequencies
  412.                                at batch driver so that, batch driver will
  413.                                find whether batch is needed to run or not
  414.  * @PARAM                    : frequencies
  415.  * @RETURN                   : void
  416.  * @EXCEPTION                :
  417.  * @LAST MODIFIED BY         :
  418.  * @LAST MODIFIED DATE       :
  419. **/
  420.  
  421.     public static void setAllProcess(int value)
  422.     {
  423.         int initial = 4;
  424.         int curValue =0;
  425.         for(int i =initial;i>=0;i--)
  426.         {
  427.             curValue=(int)Math.pow((double)2, (double)i);
  428.             if(value>=curValue)
  429.             {
  430.                 value-=curValue;
  431.                 switch(i)
  432.                 {
  433.                     case 0:
  434.                         processErrorRecords = true;
  435.                         break;
  436.                     case 1:
  437.                         isHijMonthEndJob = true;
  438.                         break;
  439.                     case 2:
  440.                         isGregMonthEndJob = true;
  441.                         break;
  442.                     case 3:
  443.                         isHijYearEndJob = true;
  444.                         break;
  445.                     case 4:
  446.                         isGregYearEndJob = true;
  447.                            break;
  448.                        default:
  449.                            break;
  450.                 }
  451.             }
  452.         }
  453.     }
  454.  
  455. /**
  456.  * @METHOD NAME              : isNumberEntered
  457.  * @INFORMATION              : This method checks whether passed value is number or not
  458.  * @PARAM                    : frequencies
  459.  * @RETURN                   : void
  460.  * @EXCEPTION                :
  461.  * @LAST MODIFIED BY         :
  462.  * @LAST MODIFIED DATE       :
  463. **/
  464.  
  465.     private static boolean isNumberEntered(String value)
  466.     throws GOSIBatchException
  467.     {
  468.         boolean check= true;
  469.         try
  470.         {
  471.             int val= Integer.parseInt(value);
  472.             isNumberEntered =true;
  473.         }catch(Exception e)
  474.         {
  475.             check=false;
  476.         }
  477.         return check ;
  478.     }
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement