/*
 * Created on Mar 16, 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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

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.SubmissionPK;

/**
 * @ejb.bean name="Submission"
 *           display-name="Name for Submission"
 *           description="Description for Submission"
 *           jndi-name="ejb/Submission"
 *           type="BMP"
 *           view-type="both"
 * 			 primkey-field = "itemKey"
 */
public class SubmissionBean extends ConnectionBean {

	private String date;

	private int submissionID;

	private String username;

	private String courseID;

	private String assignmentID;

	private String activityID;

	private int grade;

	private EntityContext ctx;

	/**
	 * 
	 */
	public SubmissionBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Inserts a new Submission into the database.
	 * @throws EJBException
	 * @throws RemoteException
	 * @ejb.create-method 
	 */
	public SubmissionPK ejbCreate(SubmissionObj submission)
		throws javax.ejb.CreateException {
		Connection connection = null;
		try {
			int submissionID = 0;
			connection = getConnection();
			PreparedStatement statement1 = connection
				.prepareStatement("SELECT MAX(submissionID) FROM submission WHERE username=? AND assignmentID=? AND activityID=? ;");
			statement1.setString(1, submission.getStudentUsername());
			statement1.setString(2, submission.getAssignmentID());
			statement1.setString(3, submission.getActivityID());
			ResultSet set = statement1.executeQuery();
			if (set.next()) {
				submissionID = set.getInt(1)+1;
			}
			set.close();
			statement1.close();

			statement1 = connection
				.prepareStatement("INSERT INTO submission (submissionID, courseID, username, activityID, assignmentID, submitDate) VALUES (?, ?, ?, ?, ?, ?)");
			statement1.setInt(1, this.submissionID=submissionID);
			statement1.setString(2, this.courseID=submission.getCourseID());
			statement1.setString(3, this.username = submission.getStudentUsername());
			statement1.setString(4, this.activityID=submission.getActivityID());
			statement1.setString(5, this.assignmentID=submission.getAssignmentID());
			SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
			String strDate = formatter.format(new Date());
			statement1.setString(6, this.date=strDate);
			statement1.executeUpdate();
			statement1.close();
			connection.close();
		} catch (SQLException e) {
			throw new EJBException("Could not create a Submission", e);
		}finally{
			try {
				connection.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}

		return new SubmissionPK(
			submissionID,
			submission.getStudentUsername(),
			submission.getActivityID(),
			submission.getAssignmentID());
	}

	public void ejbPostCreate(SubmissionObj submission)
		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 {
		SubmissionPK itemKey = (SubmissionPK) ctx.getPrimaryKey();
		Connection connection = null;
		connection = getConnection();
		PreparedStatement statement;
		try {
			statement = connection
				.prepareStatement("SELECT courseID,date,grade FROM submission WHERE username=? AND assignmentID=? AND activityID=? AND submissionID=?");

			statement.setString(1, itemKey.getUsername());
			statement.setString(2, itemKey.getAssignmentID());
			statement.setString(3, itemKey.getActivityID());
			statement.setInt(4, itemKey.getSubmissionID());
			ResultSet set = statement.executeQuery();
			if (set.next()) {
				this.submissionID = itemKey.getSubmissionID();
				this.assignmentID = itemKey.getAssignmentID();
				this.activityID = itemKey.getActivityID();
				this.courseID = set.getString(1);
				this.date = set.getString(2);
				this.grade = set.getInt(3);
			}
			set.close();
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}

	}

	/* (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
		SubmissionPK key = (SubmissionPK) ctx.getPrimaryKey();
		Connection connection = null;
		try {
			connection = getConnection();
			PreparedStatement statement = connection
				.prepareStatement("DELETE FROM submission WHERE submissionID = ?"
					+ " AND username = ?"
					+ " AND activityID = ?"
					+ " AND assignmentID = ?");
			statement.setInt(1, key.getSubmissionID());
			statement.setString(2, key.getUsername());
			statement.setString(3, key.getActivityID());
			statement.setString(4, key.getAssignmentID());
			if (!statement.execute()) {
				throw new RemoveException(
					"Could not remove Submission, No Change");
			}
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not remove Submission", e);
		} finally {
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		// TODO Auto-generated method stub
		System.out.println("CANNOT UPDATE YET");
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext arg0)
		throws EJBException,
		RemoteException {
		this.ctx = arg0;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#unsetEntityContext()
	 */
	public void unsetEntityContext() throws EJBException, RemoteException {
		ctx = null;
	}

	/**
	 * Business method
	 * @ejb.finder   view-type = "both"
	 * 				 result-type-mapping = "Remote"
	 */
	public SubmissionPK ejbFindByPrimaryKey(SubmissionPK 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 submissionID FROM submission WHERE submissionID = ?"
					+ " AND username = ?"
					+ " AND activityID = ?"
					+ " AND assignmentID = ?");
			statement.setInt(1, key.getSubmissionID());
			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 Submission: submissionID");
			}
			resultSet.close();
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not find Submission");
		}finally{
			try {
				connection.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}

		return key;
	}

	public Collection ejbFindAllByStudent(String username)
		throws FinderException {
		Connection connection = null;
		ArrayList itemList = new ArrayList();
		try {
			connection = getConnection();
			PreparedStatement statement = connection
				.prepareStatement("SELECT submissionID, username, activityID, assignmentID FROM submission WHERE username =  ?");
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				throw new FinderException("Could not find any Submissions");
			}
			SubmissionPK temp = new SubmissionPK(
				resultSet.getInt(1),
				resultSet.getString(2),
				resultSet.getString(3),
				resultSet.getString(4));
			
			itemList.add(temp);
			while (resultSet.next()) {
				temp = new SubmissionPK(resultSet.getInt(1), resultSet
					.getString(2), resultSet.getString(3), resultSet
					.getString(4));
				itemList.add(temp);
			}
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not find any Submissions.");
		}finally{
			try {
				connection.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return itemList;
	}

	public Collection ejbFindAllByActivity(String activityID)
		throws FinderException {
		Connection connection = null;
		ArrayList itemList = new ArrayList();
		try {
			connection = getConnection();
			PreparedStatement statement = connection
				.prepareStatement("SELECT submissionID, username, activityID, assignmentID FROM submission WHERE activityID =  ?");
			statement.setString(1, activityID);
			ResultSet resultSet = statement.executeQuery();
			if (!resultSet.next()) {
				statement.close();
				throw new FinderException("Could not find any Submissions");
			}
			SubmissionPK temp = new SubmissionPK(
				resultSet.getInt(1),
				resultSet.getString(2),
				resultSet.getString(3),
				resultSet.getString(4));
			itemList.add(temp);
			while (resultSet.next()) {
				temp = new SubmissionPK(resultSet.getInt(1), resultSet
					.getString(2), resultSet.getString(3), resultSet
					.getString(4));
				itemList.add(temp);
			}
			statement.close();
		} catch (SQLException e) {
			throw new EJBException("Could not find any Submissions.");
		}finally{
			try {
				connection.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return itemList;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public SubmissionObj getSubmission() {

		SubmissionObj rtVal = new SubmissionObj(
			Integer.toString(this.submissionID),
			this.courseID,
			this.username,
			this.activityID,
			this.assignmentID);
		return rtVal;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public SubmissionPK getItemKey() {
		SubmissionPK itemKey = new SubmissionPK(submissionID, username, activityID, assignmentID);
		return itemKey;
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public java.lang.String getDate() {
		return this.date;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public int getSubmissionID() {
		return this.submissionID;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public java.lang.String getUsername() {
		return this.username;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getAssignmentID() {
		return this.assignmentID;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getCourseID() {
		return this.courseID;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getActivityID() {
		return this.activityID;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public int getGrade() {
		// TODO Auto-generated method stub
		return this.grade;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setGrade(int grade) {
		this.grade = grade;
	}
}