/**
 * This is the main file for console based Ejbgenerator.
 *
 * @version      $Id: generateEjb.java,v 1.1 4:09 PM $
 *
 * @author       Sharique Sayeed
 *
 *
  *
 */

import java.io.*;
import java.util.*;

public class generateEJB 
{

	static String EJBname="";
	static String packageName="";
	static String tablename="";
	static String poolname="";
	static Vector primaryKeys=new Vector();
	static Vector accessorMethods=new Vector();
	static Vector mutatorMethods=new Vector();
	static Vector tableFieldName=new Vector();
	static Vector fieldType=new Vector();
	static StringBuffer homebuffer=new StringBuffer();
	static StringBuffer remotebuffer=new StringBuffer();
	static StringBuffer beanbuffer=new StringBuffer();
	static String primarykey="";
	static StringBuffer PKbuffer=new StringBuffer();
	static StringBuffer ejbjarbuffer=new StringBuffer();
	static StringBuffer cmpbuffer=new StringBuffer();
	static StringBuffer weblejarbuffer=new StringBuffer();

	//the main method
	
	public static void main(String args[])throws IOException
	{
	
	//to read the name of the ejb to be created
		System.out.println("Enter the name of the EJB");
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		EJBname=br.readLine();
		System.out.println(EJBname);
	//to read the name of the Package in which the classes are to be placed to be created
		System.out.println("Enter the name of the package");
		packageName=br.readLine();
	//to read the name of the table to which this Entity bean is to represent
		System.out.println("Enter the name of the table");
		tablename=br.readLine();
	//to read the name of the Poolname used for the database
		System.out.println("Enter the Poolname for connection to the database");
		poolname=br.readLine();
		System.out.println(tablename);
	//to read the columns of the table and the associated variable type
		System.out.println("Enter the name of the columns in the table........  ");
		System.out.println("When you have finished press 'n' ");	
		String line=br.readLine();
		while (!line.equalsIgnoreCase("n"))
		{
			tableFieldName.add(line);
			System.out.println("Enter the datatype ");	
			line=br.readLine();
			fieldType.add(line);
			System.out.println("Enter the name of the columns in the table........  ");
			System.out.println("When you have finished press 'n' ");	
			line=br.readLine();
		}
	//to read the name of the primarykey column
		System.out.println("Enter the name of the Primarykey column");
		primarykey=br.readLine();

/***************************HOME INTERFACE*********************************/
//creates the home interface of the bean
/*********************************************************************************/
		homebuffer.append("package "+packageName+";\n");
		homebuffer.append("import javax.ejb.*;\n");
		homebuffer.append("import java.rmi.RemoteException;\n");
		homebuffer.append("import java.util.Enumeration; \n");
		homebuffer.append("public interface "+EJBname+"Home"+" extends EJBHome \n {");
		homebuffer.append("public "+EJBname+" create (");
		for(int i=0;i<tableFieldName.size();i++)
		{
			if(i<(tableFieldName.size()-1))
			homebuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+",");
			else
			homebuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i));
		}
		homebuffer.append(") throws RemoteException,CreateException;\n");
		homebuffer.append("\t public "+EJBname+ " findByPrimaryKey("+EJBname+"PK key) throws RemoteException,FinderException;\n");
		for(int i=1;i<tableFieldName.size();i++)
		{
			homebuffer.append("\t public Enumeration findBy"+tableFieldName.elementAt(i)+"("+fieldType.elementAt(i)+" "+ tableFieldName.elementAt(i)+") throws RemoteException,FinderException;\n");
		}
		homebuffer.append("}");
		java.io.FileOutputStream homeInterface = new java.io.FileOutputStream(EJBname+"Home.java",false);
		String homeString=homebuffer.toString();
		byte[] b=homeString.getBytes();
		homeInterface.write(b);
		homeInterface.close();
/**************************REMOTE INTERFACE******************************************/
//creates the Remote interface of the bean
/********************************************************************************************/

		remotebuffer.append("package "+packageName+";\n");
		remotebuffer.append("import javax.ejb.*;\n");
		remotebuffer.append("import java.rmi.*;\n");
		remotebuffer.append("import java.util.Enumeration; \n");
		remotebuffer.append("public interface "+EJBname+" extends EJBObject \n {\n");
		
		for(int i=0;i<tableFieldName.size();i++)
		{
			remotebuffer.append("\t public "+fieldType.elementAt(i)+" get"+tableFieldName.elementAt(i)+"( ) throws RemoteException;\n");
			remotebuffer.append("\t public void set"+tableFieldName.elementAt(i)+"("+fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+") throws RemoteException;\n");
		}
		remotebuffer.append("}\n");

		java.io.FileOutputStream remoteInterface = new java.io.FileOutputStream(EJBname+".java",false);
		String remoteString=remotebuffer.toString();
		byte[] bi=remoteString.getBytes();
		remoteInterface.write(bi);
		remoteInterface.close();

/***************************BEAN************************************/	
//creates the Bean 
/*********************************************************************************/

		beanbuffer.append("package "+packageName+";\n");
		beanbuffer.append("import javax.ejb.*;\n");
		beanbuffer.append("import java.rmi.*;\n");
		beanbuffer.append("import java.util.*; \n");
		beanbuffer.append("import javax.naming.*; \n");

		beanbuffer.append("public class "+EJBname+"Bean implements EntityBean \n {\n");
		
		beanbuffer.append("\t protected EntityContext ctx;\n");
		for(int i=0;i<tableFieldName.size();i++)
		{
			beanbuffer.append("\t public "+fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+";\n");
		}
		
		for(int i=0;i<tableFieldName.size();i++)
		{
			beanbuffer.append("public "+fieldType.elementAt(i)+" get"+tableFieldName.elementAt(i)+"( ) throws RemoteException\n");
			beanbuffer.append("{\n"+"\t return "+tableFieldName.elementAt(i)+";\n}\n");
			beanbuffer.append("public void set"+tableFieldName.elementAt(i)+"("+fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+") throws RemoteException\n");
			beanbuffer.append("{\n\t"+"this."+tableFieldName.elementAt(i)+"="+tableFieldName.elementAt(i)+";\n}\n");
		}
		beanbuffer.append("\t public "+EJBname+"Bean() \n {}\n");
		beanbuffer.append("public "+EJBname+"PK"+" ejbCreate (");
		for(int i=0;i<tableFieldName.size();i++)
		{
			if(i<(tableFieldName.size()-1))
			beanbuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+",");
			else
			beanbuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i));
		}
		beanbuffer.append(") throws RemoteException,CreateException\n {");
		
		for(int i=0;i<tableFieldName.size();i++)
		{
		beanbuffer.append("\n\t"+"this."+tableFieldName.elementAt(i)+"="+tableFieldName.elementAt(i)+";\n");
		}
		beanbuffer.append("\t"+EJBname+"PK pk=new "+EJBname+"PK("+primarykey+");\n");
		beanbuffer.append("\t return pk;\n}\n");

		beanbuffer.append("public void ejbPostCreate (");
		for(int i=0;i<tableFieldName.size();i++)
		{
			if(i<(tableFieldName.size()-1))
			beanbuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i)+",");
			else
			beanbuffer.append(fieldType.elementAt(i)+" "+tableFieldName.elementAt(i));
		}
		beanbuffer.append(") throws RemoteException,CreateException\n {}\n");

		beanbuffer.append("public void ejbPassivate()throws RemoteException \n{\n\t\t System.out.println(\"EjbPassivate called\");\n}\n");
		beanbuffer.append("public void ejbActivate()throws RemoteException \n{\n\t\t System.out.println(\"EjbActivate called\");\n}\n");
		beanbuffer.append("public void ejbLoad()throws RemoteException \n{\n\t\t System.out.println(\"EjbLoad called\");\n}\n");
		beanbuffer.append("public void ejbStore()throws RemoteException \n{\n\t\t System.out.println(\"EjbStore called\");\n}\n");
		beanbuffer.append("public void setEntityContext(EntityContext ctx)throws RemoteException \n{\n\t\t this.ctx=ctx;\n}\n");
		beanbuffer.append("public void unsetEntityContext()throws RemoteException \n{\n\t\t this.ctx=null;\n}\n");
		beanbuffer.append("public void ejbRemove()throws RemoteException \n{\n\t\t System.out.println(\"EjbRemove called\");\n}\n");
		beanbuffer.append("\n}\n");
		java.io.FileOutputStream bean = new java.io.FileOutputStream(EJBname+"Bean.java",false);
		String beanString=beanbuffer.toString();
		byte[] bb=beanString.getBytes();
		bean.write(bb);
		bean.close();

		/****************************primary class****************************************/
		//creates the primary class javafile of the bean
		/*********************************************************************************/

		PKbuffer.append("package "+packageName+";\n");
		PKbuffer.append("import java.io.Serializable;\n");
		PKbuffer.append("public class "+EJBname+"PK implements java.io.Serializable \n {\n");
		int k=tableFieldName.indexOf(primarykey);
		PKbuffer.append("public "+fieldType.elementAt(k) +" "+primarykey+";\n");

		if((fieldType.elementAt(k).toString()).equals("int"))
		PKbuffer.append("public int hashCode()\n{\n \t return "+primarykey+";\n}\n");
		else
		PKbuffer.append("public int hashCode() \n	{\n \t return Integer.parseInt("+primarykey+");\n}\n");

		
	PKbuffer.append("public boolean equals(Object obj)\n");
	PKbuffer.append("{\n if(obj instanceof "+EJBname+"PK)");
	PKbuffer.append("\n{\n\t return ("+primarykey+"==(("+EJBname+"PK)obj)."+primarykey+");\n}\n\t return false;\n}\n");
	PKbuffer.append("public "+EJBname+"PK("+fieldType.elementAt(k)+" "+ primarykey+")	\n{\n\t this."+primarykey+"="+primarykey+";	\n}\n");
	
	PKbuffer.append("public "+EJBname+"PK()\n	{}\n}");
		java.io.FileOutputStream beanPK = new java.io.FileOutputStream(EJBname+"PK.java",false);
		String PKString=PKbuffer.toString();
		byte[] bpk=PKString.getBytes();
		beanPK.write(bpk);
		beanPK.close();
/*****************************ejb-jar*************************************/
//creates the EjbJar.xml for Weblogic
/*********************************************************************************/

ejbjarbuffer.append("<?xml version=\"1.0\"?>\n");
ejbjarbuffer.append("<!DOCTYPE ejb-jar PUBLIC \'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN\' \'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd\'>\n");
ejbjarbuffer.append("<ejb-jar>\n");
ejbjarbuffer.append("<display-name>"+EJBname+"</display-name>\n");
ejbjarbuffer.append("<enterprise-beans>\n");
ejbjarbuffer.append("<entity>\n");
ejbjarbuffer.append("<ejb-name>"+EJBname+"</ejb-name>\n");
ejbjarbuffer.append("<home>"+packageName+"."+EJBname+"Home</home>\n");
ejbjarbuffer.append("<remote>"+packageName+"."+EJBname+"</remote>\n");
ejbjarbuffer.append("<ejb-class>"+packageName+"."+EJBname+"Bean</ejb-class>\n");
ejbjarbuffer.append("<persistence-type>Container</persistence-type>\n");
ejbjarbuffer.append("<prim-key-class>"+packageName+"."+EJBname+"PK</prim-key-class>\n");
ejbjarbuffer.append("<reentrant>False</reentrant>\n");

for(int i=0;i<tableFieldName.size();i++)
		{
			ejbjarbuffer.append("	<cmp-field>\n");
			ejbjarbuffer.append("	<field-name>"+tableFieldName.elementAt(i)+"</field-name>\n");
			ejbjarbuffer.append("</cmp-field>\n");
		}
ejbjarbuffer.append("</entity>\n");
ejbjarbuffer.append("</enterprise-beans>\n");
ejbjarbuffer.append("<assembly-descriptor></assembly-descriptor>\n");
ejbjarbuffer.append("</ejb-jar>\n");
		File file1=new File("META-INF");
		file1.mkdir();
		java.io.FileOutputStream ejbxml = new java.io.FileOutputStream("META-INF/ejb-jar.xml",false);
		String ejbjar=ejbjarbuffer.toString();
		byte[] ejx=ejbjar.getBytes();
		ejbxml.write(ejx);
		ejbxml.close();


/****************************cmpxml***************************************/
//creates the CMP xml file for Weblogic
/*********************************************************************************/

cmpbuffer.append("<!DOCTYPE weblogic-rdbms-bean PUBLIC \n");
cmpbuffer.append(" \"-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB RDBMS Persistence//EN\" \n");
cmpbuffer.append("\"http://www.bea.com/servers/wls510/dtd/weblogic-rdbms-persistence.dtd\">\n");
cmpbuffer.append("<weblogic-rdbms-bean>\n");
cmpbuffer.append("<pool-name>"+poolname+"</pool-name>\n");
cmpbuffer.append("<table-name>"+tablename+"</table-name>\n");
cmpbuffer.append("<attribute-map>\n");
for(int i=0;i<tableFieldName.size();i++)
		{
			cmpbuffer.append("<object-link>\n");
			cmpbuffer.append("<bean-field>"+tableFieldName.elementAt(i)+"</bean-field>\n");
			cmpbuffer.append("<dbms-column>"+tableFieldName.elementAt(i)+"</dbms-column>\n");
			cmpbuffer.append("</object-link>\n");
		}

cmpbuffer.append("</attribute-map>\n");
cmpbuffer.append("<finder-list>\n");
for(int i=1;i<tableFieldName.size();i++)
		{
			cmpbuffer.append("<finder>\n");
			cmpbuffer.append("<method-name>findBy"+tableFieldName.elementAt(i)+"</method-name>\n");
			cmpbuffer.append("<method-params>\n");
			cmpbuffer.append("<method-param>"+fieldType.elementAt(i)+"</method-param>\n");
			cmpbuffer.append("</method-params>\n");
			cmpbuffer.append("<finder-query><![CDATA[(=$0 "+tableFieldName.elementAt(i)+" )]]></finder-query>\n");
			cmpbuffer.append("</finder>\n");
		}
cmpbuffer.append("</finder-list>\n");
cmpbuffer.append("<options>\n");
cmpbuffer.append("<use-quoted-names>false</use-quoted-names>\n");
cmpbuffer.append("</options>\n");
cmpbuffer.append("</weblogic-rdbms-bean>\n");

		java.io.FileOutputStream cmpxml = new java.io.FileOutputStream("META-INF/weblogic-cmp-rdbms-jar.xml",false);
		String cmpjar=cmpbuffer.toString();
		byte[] cjx=cmpjar.getBytes();
		cmpxml.write(cjx);
		cmpxml.close();


/**************************weblogic ejbjar********************************************/
//creates weblogic-ejb-jar.xml for weblogic
/*****************************************************************************************/
weblejarbuffer.append("<?xml version=\"1.0\"?>\n");

weblejarbuffer.append("<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN' 'http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd'>\n");

weblejarbuffer.append("<weblogic-ejb-jar>\n");
weblejarbuffer.append("<weblogic-enterprise-bean>\n");
weblejarbuffer.append("<ejb-name>"+EJBname+"</ejb-name>\n");
weblejarbuffer.append("<caching-descriptor>\n");
weblejarbuffer.append("<max-beans-in-cache>10</max-beans-in-cache>\n");
weblejarbuffer.append("</caching-descriptor>\n");
weblejarbuffer.append("<persistence-descriptor>\n");
weblejarbuffer.append("<persistence-type>\n");
weblejarbuffer.append("<type-identifier>WebLogic_CMP_RDBMS</type-identifier>\n");
weblejarbuffer.append("<type-version>5.1.0</type-version>\n");
weblejarbuffer.append("<type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>\n");
weblejarbuffer.append("</persistence-type>\n");
weblejarbuffer.append("<persistence-use>\n");
weblejarbuffer.append("<type-identifier>WebLogic_CMP_RDBMS</type-identifier>\n");
weblejarbuffer.append("<type-version>5.1.0</type-version>\n");
weblejarbuffer.append("</persistence-use>\n");
weblejarbuffer.append("</persistence-descriptor>\n");
weblejarbuffer.append("<jndi-name>"+EJBname+"</jndi-name>\n");
weblejarbuffer.append("</weblogic-enterprise-bean>\n");
weblejarbuffer.append("</weblogic-ejb-jar>\n");
java.io.FileOutputStream weblejar = new java.io.FileOutputStream("META-INF/weblogic-ejb-jar.xml",false);
		String webjar=weblejarbuffer.toString();
		byte[] webw=webjar.getBytes();
		weblejar.write(webw);
		weblejar.close();

/**********************************************************************/


	}
}
