/*
 * Created on Mar 12, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.dragonsoft.tryapp.ejb.entity;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.EJBException;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import com.dragonsoft.tryapp.ejb.entity.interfaces.UserPK;

/**
 * @ejb.bean name="User"
 *           display-name="Name for User"
 *           description="Description for User"
 *           jndi-name="ejb/User"
 *           type="BMP"
 *           view-type="both"
 * 			 primkey-field="itemKey"
 */
public class UserBean extends ConnectionBean {
	
	private UserPK itemKey;
	
	private String username;
	private String password;

	private EntityContext ctx;

	/**
	 * 
	 */
	public UserBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext ctx)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub
		this.ctx = ctx;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#unsetEntityContext()
	 */
	public void unsetEntityContext() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		ctx = null;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbRemove()
	 */
	public void ejbRemove()
		throws RemoveException,
		EJBException,
		RemoteException {
		UserPK key = (UserPK) ctx.getPrimaryKey();
		Connection con = null;
		PreparedStatement statement = null;
		String query = "DELETE FROM user where Username=?;";
		try {
			con = getConnection();
			statement = con.prepareStatement(query);
			statement.setString(1, key.getUsername());
			if (!statement.execute()) {
				throw new SQLException("Could not delete Entry with username "
					+ key.getUsername());
			}
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}

		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		UserPK key = (UserPK) ctx.getPrimaryKey();
		Connection con = null;
		PreparedStatement state = null;
		ResultSet set = null;
		try {
			con = getConnection();
			state = con
				.prepareStatement("SELECT Username, Password FROM user Where Username=?;");
			state.setString(1, key.getUsername());
			set = state.executeQuery();
			if (set.next()) {
				this.username = set.getString(1);
				this.password = set.getString(2);
			}
			set.close();
			state.close();
			con.close();
		} catch (SQLException e) {
			System.err.println("Could not load bean, " + key.getUsername());
		}

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement state = null;
		UserPK key = (UserPK) ctx.getPrimaryKey();
		try {
			con = getConnection();
			state = con
				.prepareStatement("UPDATE user SET Username=?,Password=? WHERE Username=?;");
			state.setString(1, username);
			state.setString(2, password);
			state.setString(3, key.getUsername());
			if (!state.execute())
				throw new EJBException("Could not update Record for user:"
					+ key.getUsername());
			state.close();
			con.close();
		} catch (SQLException e) {
			System.err.println("ejbStore: Error in SQL Syntax");
		}finally{
			try {
				con.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	/**
	 * Create method
	 * @ejb.create-method  view-type = "both"
	 */
	public UserPK ejbCreate(
		java.lang.String username,
		java.lang.String password) throws javax.ejb.CreateException {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement state = null;
		try {
			con = getConnection();
			state = con
				.prepareStatement("INSERT INTO user(Username,Password) VALUES(?,?);");
			state.setString(1, username);
			state.setString(2, password);
			if (!state.execute())
				throw new javax.ejb.CreateException(
					"Could not Create User with given username and password");
			state.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return new UserPK(username);
	}
	/**
	 * Post Create method
	 */
	public void ejbPostCreate(
		java.lang.String username,
		java.lang.String password) throws javax.ejb.CreateException {
		// TODO Auto-generated method stub
	}
	/**
	 * Business method
	 * @ejb.finder   view-type = "both"
	 * 				 result-type-mapping = "Remote"
	 */
	public UserPK ejbFindByPrimaryKey(UserPK key)
		throws FinderException {
		Connection con = null;
		PreparedStatement state = null;
		try {
			ResultSet set = null;
			con = getConnection();
			state = con
				.prepareStatement("SELECT username FROM user Where Username=?;");
			state.setString(1, key.getUsername());
			set = state.executeQuery();
			if (!set.next()) {
				throw new FinderException("Could not find by PrimaryKey:"
					+ key.getUsername());
			}
			set.close();
			state.close();
		} catch (SQLException e) {

		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}
		return key;
	}
	/**
	 * Home method
	 * 
	 * 				
	 */
	public UserPK ejbFindByChallenge(java.lang.String username,java.lang.String password)
		throws javax.ejb.FinderException {
		Connection con = null;
		PreparedStatement state = null;
		String str = null;
		try {
			ResultSet set = null;
			con = getConnection();
			state = con
				.prepareStatement("SELECT username FROM user Where Username=? AND Password=?;");
			state.setString(1, username);
			state.setString(2, password);
			set = state.executeQuery();
			if (set.next()) {
				str = set.getString(1);
			} else {
				System.err.println("ERROR");
				throw new FinderException("Password does not Match");
			}
			set.close();
			state.close();

		} catch (SQLException e) {
			throw new FinderException("ERROR"+ e.getMessage());
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return new UserPK(str);
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getUsername() {
		return username;
	}
	
	/**
	 * @return Returns the itemKey.
	 */
	public UserPK getItemKey() {
		return itemKey;
	}
	/**
	 * @param itemKey The itemKey to set.
	 */
	public void setItemKey(UserPK itemKey) {
		this.itemKey = itemKey;
	}
	/**
	 * @return Returns the password.
	 */
	public String getPassword() {
		return password;
	}
	

}