/*
 * Created on Mar 23, 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.util.ArrayList;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import com.dragonsoft.tryapp.common.SubmissionObj;
import com.dragonsoft.tryapp.ejb.entity.interfaces.SubmissionRosterPK;

/**
 * @ejb.bean name="SubmissionRoster" display-name="Name for SubmissionRoster"
 *           description="Description for SubmissionRoster"
 *           jndi-name="ejb/SubmissionRoster" type="BMP" view-type="both"
 */
public class SubmissionRosterBean extends ConnectionBean {

	private SubmissionRosterPK itemKey;

	private EntityContext ctx;

	/**
	 *  
	 */
	public SubmissionRosterBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Inserts a new Submission into the database.
	 * 
	 * @throws EJBException
	 * @throws RemoteException
	 * @ejb.create-method
	 */
	public SubmissionRosterPK ejbCreate(String assignmentID, String activityID,
			String courseID, String username, String startDate, String endDate,
			String lateEndDate) throws javax.ejb.CreateException {
		try {
			Connection connection = getConnection();
			PreparedStatement statement = connection
					.prepareStatement("INSERT INTO submissionroster (activityID, assignmentID, courseID, username, startDate, endDate, lateEndDate) VALUES (?, ?, ?, ?, ?, ?, ?)");
			statement.setString(1, assignmentID);
			statement.setString(2, activityID);
			statement.setString(3, courseID);
			statement.setString(4, username);
			statement.setString(5, startDate);
			statement.setString(5, endDate);
			statement.setString(5, lateEndDate);
			statement.executeUpdate();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			throw new EJBException("Could not create Instructor");
		}

		return new SubmissionRosterPK(assignmentID, activityID, courseID,
				username);
	}

	public void ejbPostCreate(String assignmentID, String activityID,
			String courseID, String username, String startDate, String endDate,
			String lateEndDate) throws EJBException, RemoteException {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException {
		SubmissionRosterPK pk = (SubmissionRosterPK)this.ctx.getPrimaryKey();
		
		this.itemKey = pk;
	}

	/*
	 * (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
		SubmissionRosterPK key = (SubmissionRosterPK) ctx.getPrimaryKey();
		try {
			Connection connection = getConnection();
			PreparedStatement statement = connection
					.prepareStatement("DELETE FROM SubmissionRosterPK WHERE courseID = ?"
							+ " AND username = ?"
							+ " AND activityID = ?"
							+ " AND assignmentID = ?");
			statement.setString(1, key.getCourseID());
			statement.setString(2, key.getUsername());
			statement.setString(3, key.getActivityID());
			statement.setString(4, key.getAssignmentID());
			if (statement.executeUpdate() != 1) {
				throw new RemoveException("Could not remove SubmissionRoster");
			}
			statement.close();
			connection.close();
		} catch (SQLException e) {
			throw new EJBException("Could not remove SubmissionRoster");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/*
	 * (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
		ctx = null;
	}

	/**
	 * Business method
	 * 
	 * @ejb.finder view-type = "both" result-type-mapping = "Remote"
	 */
	public SubmissionRosterPK ejbFindByPrimaryKey(SubmissionRosterPK key)
			throws FinderException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		SubmissionObj result = null;
		try {
			ResultSet set = null;
			connection = getConnection();
			statement = connection
					.prepareStatement("SELECT courseID, username, activityID, assignmentID FROM Student WHERE courseID = ?"
							+ " AND username = ?"
							+ " AND activityID = ?"
							+ " AND assignmentID = ?");
			statement.setString(1, key.getCourseID());
			statement.setString(2, key.getUsername());
			statement.setString(3, key.getActivityID());
			statement.setString(4, key.getAssignmentID());
			resultSet = statement.executeQuery();
			if (!set.next()) {
				throw new FinderException("Could not find SubmissionRoster:");
			}
			resultSet.close();
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not find SubmissionRoster");
		}

		return key;
	}

	public Collection ejbFindAssignmentsByStudent(String username)
			throws FinderException {
		try {
			Connection connection = getConnection();
			PreparedStatement statement = connection
					.prepareStatement("SELECT courseID, username, activityID, assignmentID FROM Student WHERE username =  ?");
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				connection.close();
				throw new FinderException("Could not find any SubmissionRoster");
			}

			ArrayList itemList = new ArrayList();

			itemList.add(resultSet.getString(4));
			while (resultSet.next()) {
				itemList.add(resultSet.getString(4));
			}
			statement.close();
			connection.close();
			return itemList;
		} catch (SQLException e) {
			throw new EJBException("Could not find any Submissions.");
		}
	}

	public Collection ejbFindCoursesByStudent(String username)
			throws FinderException {
		Connection connection = null;
		try {
			connection = getConnection();
			PreparedStatement statement = connection
					.prepareStatement("SELECT assignmentID,activityID,courseID, username FROM submissionroster WHERE username =  ?");
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				connection.close();
				throw new FinderException("Could not find any SubmissionRoster");
			}

			ArrayList itemList = new ArrayList();
			String[] keys = new String[4];
			
			while (resultSet.next()) {
				for(int i = 0;i<4;i++){
					keys[i] = resultSet.getString(i+1);
				}
				itemList.add(new SubmissionRosterPK(keys[0],keys[1], keys[2],keys[3]));
			}
			resultSet.close();
			statement.close();
			connection.close();
			return itemList;
		} catch (SQLException e) {
			throw new EJBException("SQL Expression Exception.",e);
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}
	}

	/**
	 * Business method
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public SubmissionRosterPK getItemKey() {
		return itemKey;
	}

	/**
	 * Business method
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public void setItemKey(SubmissionRosterPK itemKey) {
		this.itemKey = itemKey;
	}

}