/*
 * @(#)DbReportProcessor.java	1.0 15/02/04
 * Copyright 2004 OST(India), All rights reserved.
 * OST PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * @author v dayal
 */
package com.ReportTool.gen;
import java.util.*;
import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.*;
import com.ReportTool.*;
import com.ReportTool.util.*;
import com.ReportTool.exception.*;
import java.net.*;
import java.io.*;
import java.math.*;
import oracle.jdbc.driver.*;
import java.text.*;

/**
 * Main class which handles the lifecycle of the Report generation process.
 * The class DbReportProcessor passes all the information (ReportId, BatchId, XMLDoc, SectionId, ListId etc.)
 * to update the database with report results.
 *
 */
public class DbReportProcessor {
    int iBatchId;
    int iRepId;
    int iBranchId;
    String sRoleCode;
    int iUserId;
    String DataBaseType;
    String Dsn;
    String databaseInfo;
    String repQuery;
    String Query_type;
    
    private Connection conQuery;
    private Statement st  ;
    private ResultSet rs  ;
    private CallableStatement call;
    private cConnection userCon	   	=	new cConnection();
    
    private NewReportProperties rlp			=	new NewReportProperties();
    
    java.util.Date dtGenerationDate;
    java.util.Date dtExecutionDate;
    java.util.Date dtBatchDate;
    ArrayList arParamValues=	new ArrayList();
    ArrayList arReportListNotes=	new ArrayList();
    ArrayList arFinalMultiSectionInfo=	new ArrayList();
    ArrayList arFinalColumnInfo=	new ArrayList();
    private NewFolderPathSpecifier fPath		=	new NewFolderPathSpecifier();
    private ArrayList	arReportAttributes		=	new	ArrayList();//having reportProperties
    private String GenerationRequest=null;
    private NewCommitReportGeneration commitReport      =       new NewCommitReportGeneration();
    ReportFieldInfo rfi = null;

    public DbReportProcessor(int iBatchId,int iRepId,int iBranchId,String sRoleCode,int iUserId, java.util.Date dtGenerationDate,java.util.Date dtExecutionDate,java.util.Date dtBatchDate,ArrayList arParamValues,String GenerationRequest) 
    throws Exception 
    {  
        this.iBatchId=iBatchId;
        this.iRepId=iRepId;
        this.iBranchId=iBranchId;
        this.sRoleCode=sRoleCode;
        this.iUserId=iUserId;
        this.dtGenerationDate=dtGenerationDate;
        this.dtExecutionDate=dtExecutionDate;
        this.dtBatchDate=dtBatchDate;
        this.arParamValues=arParamValues;
        this.arReportAttributes = rlp.getReportAttributes(iRepId);
        this.GenerationRequest = GenerationRequest;
        this.startReportProcess();
    }


    
    
    public DbReportProcessor(int iBatchId,int iRepId,int iBranchId,String sRoleCode,int iUserId, java.util.Date dtGenerationDate,java.util.Date dtExecutionDate,java.util.Date dtBatchDate,ArrayList arParamValues,String GenerationRequest,ReportFieldInfo rfi) 
    throws Exception 
    {  
        this.iBatchId=iBatchId;
        this.iRepId=iRepId;
        this.iBranchId=iBranchId;
        this.sRoleCode=sRoleCode;
        this.iUserId=iUserId;
        this.dtGenerationDate=dtGenerationDate;
        this.dtExecutionDate=dtExecutionDate;
        this.dtBatchDate=dtBatchDate;
        this.arParamValues=arParamValues;
        this.arReportAttributes = rlp.getReportAttributes(iRepId);
        this.GenerationRequest = GenerationRequest;
        this.rfi=rfi;
        this.startReportProcess();
    }
    
/*     public DbReportProcessor(int iBatchId,int iRepId,int iBranchId,String sRoleCode,int iUserId, java.util.Date dtGenerationDate,java.util.Date dtExecutionDate,java.util.Date dtBatchDate,ArrayList arParamValues,String GenerationRequest) 
    throws Exception 
    {  
        this.iBatchId=iBatchId;
        this.iRepId=iRepId;
        this.iBranchId=iBranchId;
        this.sRoleCode=sRoleCode;
        this.iUserId=iUserId;
        this.dtGenerationDate=dtGenerationDate;
        this.dtExecutionDate=dtExecutionDate;
        this.dtBatchDate=dtBatchDate;
        this.arParamValues=arParamValues;
        this.arReportAttributes = rlp.getReportAttributes(iRepId);
        this.GenerationRequest = GenerationRequest;
        this.startReportProcess();
    } */

  public void startReportProcess() throws Exception 
  {
            int QueryNum=0;
            System.out.println("******DbReportProcessor:begins******");
            ArrayList arGlobalParameter     =   new ArrayList();
            ArrayList arListWiseParameter   =   new ArrayList();
            String TableName="";
            ArrayList multiFields           =   new ArrayList();
            /********************* PARAMETERS BEGIN*********************************/
            if(arParamValues.size() ==2){
                arGlobalParameter	=(ArrayList)arParamValues.get(0);
                arListWiseParameter	=(ArrayList)arParamValues.get(1);
            }
            
            String globalCondition = "";//global.getGlobalCondition(arGlobalParameter);
                        /*
                         *	PARAMETERS
                         */
            /********************* PARAMETERS END*********************************/
            ArrayList arPath	= fPath.getFolderPaths();
            String outputFolderPath = (String)arPath.get(1);

            System.out.println("dtGenerationDate: "+dtGenerationDate+" :dtBatchDate: "+dtBatchDate);
            String sGenerationDate	= com.ReportTool.util.DateHelper.format(dtGenerationDate,"yyyy-MM-dd");
            String sBatchDate	= com.ReportTool.util.DateHelper.format(dtBatchDate,"yyyy-MM-dd");
            System.out.println("sGenerationDate: "+sGenerationDate+" :sBatchDate: "+sBatchDate);

            try {
                System.out.println("Creating new ReportFieldInfo Object************************");
                ReportFieldInfo ReportObject = null;
                if(rfi==null){
                    
                
                  ReportObject = new ReportFieldInfo(iRepId);
                  ReportObject.setBatchId(iBatchId);
                  System.out.println("----------------calling setParams------------------------------");
                  System.out.print("-------------------report id= "+ReportObject.ReportId+"-----------");
                  ReportObject.setParametersDefault();
                  //ReportObject.setParams(arGlobalParameter,arListWiseParameter); commented by venkat
                }
                else
                    ReportObject = rfi;
                System.out.println("calling createQueryStrings");
                ReportObject.createQueryStrings(iBatchId);
                System.out.println("calling executeRepQuery");
                ReportObject.executeRepQuery(dtBatchDate,dtGenerationDate, dtExecutionDate,iUserId );
                System.out.println("executed new ReportField Object ********************************");
            } catch (Exception e)
            {
                e.printStackTrace();
                throw e;
            }
            
            /**** Insertion to RT_Generation_Log_Dtl Started **** */
            System.out.println("*****Insertion to RT_Generation_Log_Dtl Started****");
            String strBatchInfo = "R"+iBatchId+"_"+iRepId ;
            com.ReportTool.gen.NewReportGenerationLog repGenLog = new com.ReportTool.gen.NewReportGenerationLog(outputFolderPath,strBatchInfo);
           // System.out.println("StrBatchInfo:"+strBatchInfo+"BatchId:"+this.iBatchId+"dtBatchdate:"+this.dtBatchDate+"repid:"+this.iRepId+"BranchId:"+this.iBranchId+"Generationdate:"+this.dtGenerationDate+"ExecutionDate:"+this.dtExecutionDate+"UserId:"+this.iUserId+"RepAttr4:"+(String)arReportAttributes.get(1)+"ArParam:"+this.arParamValues);
            repGenLog.setFileName(strBatchInfo);
            boolean reportStatus = repGenLog.insertIntoLog(this.iBatchId,this.dtBatchDate,this.iRepId,this.iBranchId,this.dtGenerationDate,this.dtExecutionDate,this.iUserId,"1",(String)this.arReportAttributes.get(1),(String)arReportAttributes.get(5),(String)this.arReportAttributes.get(4),this.arParamValues);
            commitReport.commitProcess(this.iBatchId,this.dtBatchDate,this.iRepId,this.iBranchId,this.dtGenerationDate,this.dtExecutionDate,this.iUserId,"1",this.GenerationRequest);
            System.out.println("Report is Successfully Generated");
            /**** Insertion to RT_Generation_Log_Dtl Ended **** */
            
  }

  
} // end of class