import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

public class PersonBean implements EntityBean {
	//persistent fields
	private String name;
	private GregorianCalendar birthdate;
	private String street;

	public PersonBean() {
	}

	//methods which are part of the remote interface
	public int getAge() {
		return (
			new GregorianCalendar().get(Calendar.YEAR)
				- birthdate.get(Calendar.YEAR));
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) throws RemoteException {
		if (street.length() <= 30) {
			this.street = street;
		} else {
			throw new RemoteException("cannot update street");
		}
	}
	//	------------------------------------------------------------------
	public String ejbCreate(
		int year,
		int month,
		int day,
		String name,
		String street)
		throws CreateException {
		if (year > 1900
			&& month >= 1
			&& month <= 12
			&& day >= 1
			&& day <= 31
			&& name.length() <= 20
			&& street.length() <= 30) {

			this.name = name;
			this.birthdate = new GregorianCalendar(year, month, day);
			this.street = street;
			Statement stmt = getStatement();
			String s =
				new String(
					"INSERT INTO PERSON VALUES ('"
						+ name
						+ "','"
						+ birthdate.get(Calendar.YEAR)
						+ "-"
						+ birthdate.get(Calendar.MONTH)
						+ "-"
						+ birthdate.get(Calendar.DATE)
						+ "',"
						+ "'"
						+ street
						+ "'"
						+ ");");
			try {
				stmt.executeUpdate(s);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			throw new CreateException("Invalid values supplied");
		}
		return name;
	}
	public void ejbPostCreate(
		int year,
		int month,
		int day,
		String name,
		String street) {
	}
	//	------------------------------------------------------------------
	public int ejbHomeGetAge() {
		return 0;
	}

	public String ejbHomeGetStreet() {
		return new String();
	}

	public void ejbHomeSetStreet(String street) {
	}

	public Statement getStatement() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection con = null;
		try {
			con =
				(Connection) DriverManager.getConnection(
					"jdbc:mysql://10.0.0.1/Address/",
					"mario",
					"thePassword");
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			return ((Statement) con.createStatement());
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		return null; //never gets here
	}

	// ------------------------------------------------------------------
	public void setEntityContext(EntityContext ectx)
		throws EJBException, RemoteException {
	}

	public void unsetEntityContext() throws EJBException, RemoteException {
	}

	public void ejbRemove()
		throws RemoveException, EJBException, RemoteException {
		Statement stmt = getStatement();
		String s = new String("DELETE FROM PERSON WHERE Name='" + name + "';");
		try {
			stmt.executeUpdate(s);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void ejbActivate() throws EJBException, RemoteException {
	}

	public void ejbPassivate() throws EJBException, RemoteException {
	}

	public void ejbLoad() throws EJBException, RemoteException {
	}

	public void ejbStore() throws EJBException, RemoteException {
		Statement stmt = getStatement();
		String s =
			new String(
				"UPDATE PERSON SET Name='"
					+ name
					+ "', BDATE='"
					+ birthdate.get(Calendar.YEAR)
					+ "-"
					+ birthdate.get(Calendar.MONTH)
					+ "-"
					+ birthdate.get(Calendar.DATE)
					+ "', Street = '"
					+ street
					+ "' WHERE Name = '"
					+ name
					+ "';");
		try {
			stmt.executeUpdate(s);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public String ejbFindByPrimaryKey(String name) throws FinderException {
		Statement stmt = getStatement();
		String s =
			new String("SELECT Name FROM PERSON WHERE Name='" + name + "';");
		try {
			ResultSet rs = stmt.executeQuery(s);
			rs.first();
			return rs.getString("Name");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null; //never gets here
	}
}
