/*
 * Created on Mar 9, 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 java.sql.Statement;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import com.dragonsoft.tryapp.common.ActivityObj;
import com.dragonsoft.tryapp.ejb.entity.interfaces.ActivityPK;

/**
 * This bean is responsible for using the database to perform relational
 * functions on the data assocaited with an activity.
 * 
 * @ejb.bean name="Activity"
 *           display-name = "Activity EJB"
 *           description = "EJB to create/update/delete and query for activities"
 *           jndi-name="ejb/Activity"
 *           type="BMP"
 *           view-type="both"
 * 			 primkey-field="itemKey"
 */
public class ActivityBean extends ConnectionBean {
	
	private ActivityPK itemKey;
	
	private ActivityObj activity;
	
	private EntityContext ctx;
	
	/**
	 * 
	 */
	public ActivityBean() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}
	
	/**
	 * Post Create method
	 */
	public void ejbPostCreate(ActivityObj activity) throws javax.ejb.CreateException {
		// TODO Auto-generated method stub
	}
	
	/**
	 * Inserts a new Activity into the database.
	 * @throws EJBException
	 * @throws RemoteException
	 * @ejb.create-method 
	 */
	public ActivityPK ejbCreate(ActivityObj activity) throws javax.ejb.CreateException  {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		String name = activity.getActivityName();
		String desc = activity.getActivityDesc();
		try {
			connection = null;
			statement = 
				connection.prepareStatement("INSERT INTO activity (assignmentID, name, description) VALUES (?, ?, ?); " + 
											"SELECT activityID FROM Activity WHERE activityID = LAST_INSERT_ID(); ");
			statement.setString(1, "");
			statement.setString(2, name);
			statement.setString(3, desc);
			resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				connection.close();
				throw new EJBException("Could not create Activity");
			}
//			DateFormat df = DateFormat.getDateInstance();
//			activity = new ActivityObj(resultSet.getString(1), 
//									resultSet.getString(2), 
//									resultSet.getString(3), 
//									null,
//									null,
//									null);

			statement.close();
			connection.close();
			return new ActivityPK(activity.getActivityID());
		} catch (SQLException e) {
			throw new EJBException("Could not create Activity");
		}	
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		ActivityPK key = (ActivityPK) ctx.getPrimaryKey();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = getConnection();
			statement =	connection.prepareStatement("SELECT activityID, assignmentID, name, " +
											"description From activity " + 
											"WHERE activityID = ?;");
			statement.setString(1, key.getActivityID());
			resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				connection.close();
				throw new EJBException("Activity not found: id = " + key.getActivityID());
			}
			DateFormat df = DateFormat.getDateInstance();
			activity = new ActivityObj(resultSet.getString(1), 
									resultSet.getString(2), 
									resultSet.getString(3), 
									null, 
									null,
									null);

			statement.close();
			connection.close();
		} catch (SQLException e) {
			throw new EJBException("Could not load Activity: id = " + key.getActivityID());
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbRemove()
	 */
	public void ejbRemove()
		throws RemoveException,
		EJBException,
		RemoteException {
		// TODO Auto-generated method stub
		ActivityPK key = (ActivityPK) ctx.getPrimaryKey();
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = null;
			statement = connection.prepareStatement("DELETE FROM activity WHERE activityID = ?;");
			statement.setString(1, key.getActivityID());
			if (statement.executeUpdate() != 1) {
				throw new RemoveException("Could not remove Activity: id = "
						+ key.getActivityID());
			}
			statement.close();
			connection.close();
			itemKey = null;
			activity = null;
		} catch (SQLException e) {
			throw new EJBException("Could not remove Activity: id = " + key.getActivityID());
		}

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		Connection connection = null;
		PreparedStatement statement = null;
		ActivityPK key = (ActivityPK) ctx.getPrimaryKey();
		String name = activity.getActivityName();
		String desc = activity.getActivityDesc();
		try {
			connection = getConnection();
			statement = connection.prepareStatement("UPDATE activity SET name = ?, description = ? " + 
									  "WHERE activityID = ?;");
			statement.setString(1, name);
			statement.setString(2, desc);
			statement.setString(3, key.getActivityID());
			statement.executeUpdate();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			throw new EJBException("Could not store Activity: id = " + key.getActivityID());
		}
		finally{
			try {
				connection.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext arg0)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub
		this.ctx = arg0;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#unsetEntityContext()
	 */
	public void unsetEntityContext() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		this.ctx = null;
	}
	
	
	// -- Utility Methods
	
	/**
	 * Finds by the primary key, the primary key object that
	 * can then be loaded via ejbLoad.
	 * Business method
	 * @ejb.finder   view-type = "both"
	 * 				 result-type-mapping = "Remote"
	 */
	public ActivityPK ejbFindByPrimaryKey(ActivityPK itemKey)
		throws FinderException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			ResultSet set = null;
			connection = getConnection();
			statement = connection.prepareStatement("SELECT activityID, assignmentID, name, description FROM activity Where activityID=?;");
			statement.setString(1, itemKey.getActivityID());
			resultSet = statement.executeQuery();
			if (!set.next()) {
				throw new FinderException("Could not find by PrimaryKey:"
					+ itemKey.getActivityID());
			}
			resultSet.close();
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not retrive Activity: id = " + itemKey.getActivityID());
		}
		
		return itemKey;
	}
	
	/**
	 * Finds all Activities in the database.
	 * @return a collection of Activities
	 * @throws FinderException
	 * 
	 */
	public Collection ejbFindByAll() throws FinderException {
		Connection connection = null;
		Statement statement = null;
		ArrayList itemList = null;
		try {
			  connection = getConnection();
			  statement = connection.createStatement();
			  ResultSet resultSet = statement.executeQuery("SELECT activityID, assignmentID, name, description FROM activity");
			  if (!resultSet.next()) {
				statement.close();
				connection.close();
				throw new FinderException("Could not find any Activities");
			  }
			  
			  itemList = new ArrayList();
			  ActivityObj temp = new ActivityObj(resultSet.getString(1), 
											resultSet.getString(2), 
											resultSet.getString(3), 
											null, 
											null,
											null);
			  itemList.add(temp);
			  
			  while (resultSet.next()) {
			  	temp = new ActivityObj(resultSet.getString(1), 
									resultSet.getString(2), 
									resultSet.getString(3), 
									null,
									null,
									null);
				itemList.add(temp);
			  }
			  statement.close();
			  connection.close();
			  return itemList;
		} catch (SQLException e) {
			throw new EJBException("Could not find any Activities.");
		} 
	}
	
	/**
	 * Finds all Activities related to the specified assignment.
	 * @param assignmentID
	 * @return a collection of Activities
	 * @throws FinderException
	 * 
	 */
	public Collection ejbFindByAllByAssignmentID(String assignmentID) throws FinderException {
		Connection connection = null;
		List itemList = new ArrayList();
		try {
			  connection = getConnection();
			  PreparedStatement statement = 
			  connection.prepareStatement("SELECT activityID FROM activity " +
											"WHERE assignmentID = ?");
			  
			  statement.setString(1, assignmentID);
			  ResultSet resultSet = statement.executeQuery();
			  if (!resultSet.first()) {
				statement.close();
				connection.close();
				throw new FinderException("Could not find any Activities");
			  }
			  ActivityPK temp = new ActivityPK(resultSet.getString(1));
			  itemList.add(temp);
			  while (resultSet.next()) {
			  	temp = new ActivityPK(resultSet.getString(1));
				itemList.add(temp);
			  }
			  
			  statement.close();
			  connection.close();
			} catch (SQLException e) {
				throw new EJBException("Could not find any Activities.");
			}finally{
				try {
					connection.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
			return itemList;
	}

	/**
	 * Gets the ActivityObj from within the ActivityBean
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public ActivityObj getActivity() {
		return activity;
	}
	/**
	 * Allows setting of the ActivityObj.
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setActivity(ActivityObj activity) {
		this.activity = activity;
	}
	
	/**
	 * Gets the PrimaryKey of the Activity Bean
	 * Business method
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public ActivityPK getItemKey() {
		return itemKey;
	}
	/**
	 * Allows setting of ActivityBean
	 * Business method
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public void setItemKey(ActivityPK itemKey) {
		this.itemKey = itemKey;
	}
}