/*
 * File: SubmitBean.java
 * Created on Mar 18, 2005
 */
package com.dragonsoft.tryapp.ejb.session;


import java.io.File;
import java.rmi.RemoteException;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.dragonsoft.tryapp.common.exceptions.ExceptionHandler;
import com.dragonsoft.tryapp.common.exceptions.TryException;
import com.dragonsoft.tryapp.ejb.entity.interfaces.ActivityFS;
import com.dragonsoft.tryapp.ejb.entity.interfaces.ActivityFSHome;
import com.dragonsoft.tryapp.ejb.entity.interfaces.ActivityKey;
import com.dragonsoft.tryapp.ejb.entity.interfaces.AssignmentKey;
import com.dragonsoft.tryapp.ejb.entity.interfaces.CourseKey;
import com.dragonsoft.tryapp.ejb.entity.interfaces.Submission;
import com.dragonsoft.tryapp.ejb.entity.interfaces.SubmissionHome;
import com.dragonsoft.tryapp.ejb.session.interfaces.ProcessMonitor;
import com.dragonsoft.tryapp.ejb.session.interfaces.ProcessMonitorHome;
import com.dragonsoft.tryapp.support.TryFileSystem;

import fireTester.tests.batch.BatchTest;

/**
 * Responsible for submission of a SubmissionObj. Testing should
 * then be queued based off the activity skeleton.
 * @ejb.bean name="Submit"
 *           display-name="Name for Submit"
 *           description="Description for Submit"
 *           jndi-name="ejb/Submit"
 *           type="Stateless"
 *           view-type="remote"
 */
public class SubmitBean implements SessionBean {
	private SubmissionHome subHome;
	private ProcessMonitorHome procHome;
	private ActivityFSHome actHome;
	/**
	 * 
	 */
	public SubmitBean() {
		super();
	}

	/* 
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext arg0)
		throws EJBException,
		RemoteException {

	}

	/* 
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {


	}

	/* 
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		init();

	}

	/* 
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {

	}

	/**
	 * Allows the submission of files through the system.  
	 * @param submission an object which encapsulates the necessary information for a submission.
	 * @return The Hash that represents the submission.
	 * @throws RemoteException If there is a problem with the connection.
	 * 
	 * @ejb.interface-method  view-type = "remote"
	 */
	public String submit(
		com.dragonsoft.tryapp.common.SubmissionObj submission) throws RemoteException {
		// Should only contain storage of submission(talking to entity) and then
		// load up Assignment Information for the given activity.
		// then go to the mantaray stuff and send it the correct info.
		Submission sub = null;
		String hashCode =null;
		try {
			//save in DB
			String pathToFiles = TryFileSystem.getPathToStudentSubmission(submission);
			File directoryStructure = new File(pathToFiles);
			if(!directoryStructure.exists())
				directoryStructure.mkdirs();

			//copy the files over
			Iterator iterator = submission.getSubmittedFiles().iterator();
			while (iterator.hasNext()) {
				File originalFile = new File((String) iterator.next());
				File newFile = new File(pathToFiles + originalFile.getName());
				TryFileSystem.copy(originalFile.getAbsolutePath(),newFile.getAbsolutePath());
				boolean worked=originalFile.delete();
				if (!worked){
					// This will be caught why throw it?
					throw new TryException("File copying failed. FILESRC = " + originalFile.getAbsolutePath()+"\nDESTFILE="+newFile.getAbsolutePath());
				}
			}
			
			
			hashCode = submission.getSubmissionID();
			ProcessMonitor proc = procHome.create();
			ActivityKey key = new ActivityKey(new AssignmentKey(new CourseKey("20043","4003101"),"1"),"1");
			ActivityFS act = actHome.findByPrimaryKey(key);
			BatchTest[] test = act.getTests();
			proc.addProcess(submission,test[0]);
			
		}catch(CreateException e){
			ExceptionHandler.logException(e,this);
		}catch (Exception e) {
			try {
				if(sub!=null)
					sub.remove();
			} catch (RemoteException e1) {
				System.out.println(e1.getMessage());
			} catch (RemoveException e1) {
				System.out.println(e1.getMessage());
			}
			ExceptionHandler.logException(e,this);
		}
		return hashCode;
	}

	/**
	 * Create method
	 * @ejb.create-method  view-type = "local"
	 */
	public void ejbCreate() throws javax.ejb.CreateException {
		init();
	}
	
	private void init(){
		try{
		InitialContext context = new InitialContext();
		Object obj = context.lookup(SubmissionHome.JNDI_NAME);
		subHome = (SubmissionHome) PortableRemoteObject.narrow(
			obj,
			SubmissionHome.class);
		obj = context.lookup(ProcessMonitorHome.JNDI_NAME);
		procHome = (ProcessMonitorHome)PortableRemoteObject.narrow(obj,ProcessMonitorHome.class);
		
		obj = context.lookup(ActivityFSHome.JNDI_NAME);
		actHome = (ActivityFSHome)PortableRemoteObject.narrow(obj,ActivityFSHome.class);
		
		}catch(NamingException e){
			ExceptionHandler.logException(e,this);
		}
	}
	
}