/*
 * Created on Apr 20, 2005
 *
 */
package com.dragonsoft.tryapp.ejb.entity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;

import com.dragonsoft.tryapp.common.exceptions.ExceptionHandler;
import com.dragonsoft.tryapp.common.exceptions.TryException;
import com.dragonsoft.tryapp.common.exceptions.TryIOException;
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.utils.AssignmentFSUtils;
import com.dragonsoft.tryapp.ejb.entity.utils.FileSystemTraverser;
import com.dragonsoft.tryapp.ejb.entity.utils.SerializationManager;
import com.dragonsoft.tryapp.ejb.entity.utils.activity.ActivityFSBeanReader;
import com.dragonsoft.tryapp.ejb.entity.utils.activity.ActivityFSBeanWriter;

import fireTester.tests.batch.BatchTest;

/**
 * @ejb.bean name="ActivityFS"
 *           display-name="Name for ActivityFS"
 *           description="Description for ActivityFS"
 *           jndi-name="ejb/ActivityFS"
 *           type="BMP"
 *           view-type="both"
 * 			 primkey-field="key"
 * @author Greg
 */
public class ActivityFSBean implements EntityBean {

	private static ActivityKey key;
	//Files
	
	private static final String KEY_FILE = "actkey.fire";
	private static final String REQUIRED_FILES = "required.fire";
	private static final String OPTIONAL_FILES = "optional.fire";
	private static final String REJECTED_FILES = "rejected.fire";
	//Directories
	private static final String INPUT_DIR = "inputs_files"
		+ File.pathSeparatorChar;
	private static final String SOLN_DIR = "solution_files"
		+ File.pathSeparatorChar;
	
	private static final String DOWNLOAD_FILES = "download_files"
		+ File.pathSeparatorChar;
	// Enitities
	private static final String DEFAULT_DATE = "DEFAULT";

	//vars
	private EntityContext ctx;

	private String name;
	private AssignmentKey owningAssignment;
	private String activityNumber;
	private Map dateMap;
	private String[] required;
	private String[] optional;
	private String[] rejected;
	private File[] solns;
	private File[] materials;
	private BatchTest[] tests;

	/**
	 * 
	 */
	public ActivityFSBean() {
		super();
		dateMap = new HashMap();
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
	 */
	public void setEntityContext(EntityContext ctx)
		throws EJBException,
		RemoteException {
		this.ctx = ctx;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#unsetEntityContext()
	 */
	public void unsetEntityContext() throws EJBException, RemoteException {
		this.ctx = null;
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbRemove()
	 */
	public void ejbRemove()
		throws RemoveException,
		EJBException,
		RemoteException {

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {

	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbLoad()
	 */
	public void ejbLoad() throws EJBException, RemoteException {
		// load the keys
		//
		ActivityKey key = (ActivityKey)ctx.getPrimaryKey();
		FileSystemTraverser traverser = new FileSystemTraverser();
		File thisDir;//new File(ActivityFSUtils.getPath(key));
		try {
			thisDir = traverser.getActivity(key);
			this.activityNumber = key.getActivityNumber();
			this.owningAssignment = key.getAssociatedAssignment();
			ActivityFSBeanReader reader =SerializationManager.getActivityBeanReader();
			reader.readActivityFSBean(this,thisDir);
		} catch (TryException e) {
			throw new EJBException("Problem Activity:",e);
		}
	}

	/* (non-Javadoc)
	 * @see javax.ejb.EntityBean#ejbStore()
	 */
	public void ejbStore() throws EJBException, RemoteException {
		// save the keys
		// make the dirs for the tests, if not there.
		// 
		FileSystemTraverser trav = new FileSystemTraverser();
		File thisDir;//new File(ActivityFSUtils.getPath(getKey()));
		try {
			thisDir = trav.getActivity(getKey());
			File keyFile = new File(thisDir, KEY_FILE);
			System.out.println("MAKING A STORAGE");
			ActivityFSBeanWriter bWriter = SerializationManager
					.getActivityBeanWriter();
			System.out.println("MAKING A STORAGE1");
			bWriter.writeActivityFSBean(this, thisDir);
			//this should all be moved into the bean writer.
			PrintWriter writer = new PrintWriter(new FileWriter(keyFile));
			writer.println(this.activityNumber);
			AssignmentFSUtils.serialize(this.owningAssignment, writer);
			writer.flush();
		}catch(TryIOException e){
			ExceptionHandler.logException(e,this);
			throw new EJBException("Problem with Activity Storage",e);
		}catch(IOException e){
			ExceptionHandler.logException(e,this);
			throw new EJBException("Problem with Activity Storage",e);
		} catch (TryException e) {
			ExceptionHandler.logException(e,this);
			throw new EJBException("Problem with Activity Storage",e);
		}
		
		
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getName() {
		return name;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void addRequiredFile(String pattern) {

	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String[] getRequiredFiles() {

		return null;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void addOptionalFile(String pattern) {

	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String[] getOptionalFiles() {

		return null;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void addRejectedFile(String pattern) {

	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String[] getRejectedFiles() {
		return null;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void addTest(Object o) {

	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Object getTest(Object o) {
		return null;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setDueDate(Date obj) {
		this.dateMap.put(DEFAULT_DATE, obj);
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setDueDate(Date obj, String user) {
		this.dateMap.put(user, obj);
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getDueDate() {
		return (Date) dateMap.get(DEFAULT_DATE);
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Date getDueDate(String user) {
		return (Date) dateMap.get(user);
	}

	/**
	 * Create method
	 * @ejb.create-method  view-type = "both"
	 */
	public ActivityKey ejbCreate(
		AssignmentKey assignment,
		String activityID,
		String name) throws javax.ejb.CreateException {
		this.name = name;
		this.activityNumber = activityID;
		this.owningAssignment = assignment;
		System.out.println("CREATED AN ACTIVITY");
		
		return new ActivityKey(this.owningAssignment, this.activityNumber);
	}

	/**
	 * Post Create method
	 */
	public void ejbPostCreate(
		AssignmentKey assignment,
		String activityID,
		String name) throws javax.ejb.CreateException {

	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setTests(BatchTest[] tests) {
		this.tests = tests;
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public BatchTest[] getTests() {
		return tests;
	}

	public ActivityKey ejbFindByPrimaryKey(ActivityKey key) {
		return key;
	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public ActivityKey getKey() {
		return new ActivityKey(this.owningAssignment,this.activityNumber);
	}
	
	public Map getDueDates(){
		return this.dateMap;
	}
	
	
	public void setDueDates(Map map){
		this.dateMap = map;
	}
	
	public void setRequiredFilePatterns(String[] filename){
		this.required = filename;
	}
	
	public void setOptionalFilePatterns(String[] filename){
		this.optional = filename;
	}
	public void setRejectedFilePatterns(String[] filename){
		this.rejected = filename;
	}
	
	public Collection ejbFindByCourseKey(CourseKey key){
		List list = new ArrayList();
		
		return null;
	}
}
