package ibase.webitm.ejb.sys;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import ibase.webitm.utility.*;
import java.util.StringTokenizer;
import ibase.webitm.utility.*;
import ibase.utility.*;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.util.ArrayList;
import java.text.*;
import ibase.webitm.ejb.dis.*;
import java.sql.*;
import java.io.*;


public class CreateRCPXML
{
	private String winName = null;
	private String tranIdCol = null;
	private String tranId = null;
	
	private String filePath = null;
	
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	
	private String winSqls[];
	//private String formNos[];
	
	private ArrayList formNos = new ArrayList();
	
	public CreateRCPXML(){}
	
	public CreateRCPXML( String winName, String tranIdCol )
	{
		this.winName = winName;
		this.tranIdCol = tranIdCol;
		//to change as per the db name 
		
		filePath = CommonConstants.JBOSSHOME + File.separator + "SQL" + File.separator + CommonConstants.DB_NAME + File.separator; 

	}
	
	public String getTranXML( String tranId, Connection conn ) throws Exception
	{
		String sql = "select form_no from obj_forms where win_name = '" + this.winName + "'";
		
		this.tranId = tranId;
		
		pstmt = conn.prepareStatement( sql );
		
		rs = pstmt.executeQuery();
		
		while( rs.next() )
		{
			formNos.add( rs.getString( "form_no" ) );
		}
		rs.close();
		rs = null;
		pstmt.close();
		pstmt = null;
		
		int noOfForms = formNos.size();
		
		winSqls = new String[ noOfForms ];
		for( int formIdx = 0; formIdx < noOfForms ; formIdx++ )
		{
			sql = getFormSQL( ( String )formNos.get( formIdx ) );
			winSqls[ formIdx ] = sql;
		}
		
		return prepareXML( winSqls, conn );
		
	}
	private String getFormSQL( String formNo ) throws Exception
	{
		StringBuffer dataBuff = new StringBuffer();
		String lineStr = null;
		String fileName = winName.substring( 2 ) + ( formNo.equalsIgnoreCase( "1" ) ? "21" : "1" + formNo );
		BufferedReader br = new BufferedReader( new FileReader( filePath + fileName + ".sql" ) );
		
		while( ( lineStr = br.readLine() ) != null )
		{
			dataBuff.append( lineStr );
		}
		
		return dataBuff.toString();
	}
	private String prepareXML( String []formSQLs, Connection conn ) throws Exception
	{
		StringBuffer tranXmlBuff = new StringBuffer( " <DocumentRoot> "
									+" <description>Datawindow Root</description> "
									+" <group0><description>Group0 description</description> "
									+" <Header0><description>Header0 members</description> "
									+" <DocumentRoot><description>Datawindow Root</description> "
									+" <group0><description>Group0 description</description> "
									+" <Header0><description>Header0 members</description> "
									);
		String colName = null;
		String colVal = null;
		String colXml = null;
		String detTag = null;
		String detEndTag = null;
		
		for( int formSqlIdx = 0; formSqlIdx < formSQLs.length ; formSqlIdx++ )
		{
			pstmt = conn.prepareStatement( formSQLs[ formSqlIdx ] );
			
			pstmt.setString( 1, this.tranId );
			
			rs = pstmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int numberOfColumns = rsmd.getColumnCount();
			detTag = "<Detail" + ( formSqlIdx + 1 ) + ">";
			detEndTag = "</Detail" + ( formSqlIdx + 1 ) + ">";
			
			while( rs.next() )
			{
				tranXmlBuff.append( "\n\t" + detTag ); 
				for( int rsIdx = 1; rsIdx < numberOfColumns ; rsIdx++ )
				{
					colName = rsmd.getColumnName( rsIdx );
					colVal = rs.getString( colName );
					colXml = "\n\t\t<" + colName.toLowerCase() + ">" 
									+ ( colVal == null || colVal.trim().length() == 0 || colVal.equalsIgnoreCase( "null" ) ? "" : colVal )
							+ "</" + colName.toLowerCase() + ">";
					tranXmlBuff.append( colXml );
				}
				tranXmlBuff.append( "\t" + detEndTag );
			}
			
			rsmd = null;
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			
		}
		tranXmlBuff.append( "			</Header0> "
							+"		</group0> "
							+"	</DocumentRoot> "
						+"	</Header0> "
						+" </group0> "
					+"</DocumentRoot> " );
					
		return tranXmlBuff.toString();
	}
	
}
