/*
 * Created on Apr 19, 2005
 *
 */
package com.dragonsoft.tryapp.ejb.entity.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.dragonsoft.tryapp.common.exceptions.TryIOException;
import com.dragonsoft.tryapp.ejb.entity.AssignmentFSBean;
import com.dragonsoft.tryapp.ejb.entity.interfaces.AssignmentKey;
import com.dragonsoft.tryapp.ejb.entity.interfaces.CourseKey;

/**
 * @author Greg
 * 
 *  
 */
public final class AssignmentFSUtils {
	public static final String ASSIGNMENT_FOLDER = "assignments";

	/**
	 * Gets the File location path from a CourseKey and an Assignment Number.
	 * 
	 * @param obj
	 *            the CourseKey object
	 * @param assignmentNumber
	 *            the assignment number that is to be found
	 * @return a string that represents the location of the wanted assignment.
	 */
	/*private static String getPath(CourseKey obj, String assignmentNumber) {
		StringBuffer path = new StringBuffer(CourseFSUtils.getPath(obj));
		path.append(ASSIGNMENT_FOLDER).append(File.separatorChar);
		path.append(assignmentNumber).append(File.separatorChar);
		System.out.println(path.toString());
		return path.toString();
	}*/

	/**
	 * Gets the Path to the assignment in question.
	 * 
	 * @param key
	 *            the object that provides indexing information for an
	 *            assignment.
	 * @return a String path that represents the location of the assignment.
	 */
/*	public static String getPath(AssignmentKey key) {
		return getPath(key.getAssociatedCourse(), key.getAssignmentNumber());
	}
*/
	/**
	 * Find all the Assignments that are associated with a course, given its
	 * location.
	 * 
	 * @param courseFolder
	 *            The File object which represents
	 * @return a collection of AssignmentKey Objects that represent the
	 *         assignments associated with the course, represented by the file.
	 */
	public static Collection findByCourseFolder(File courseFolder)
			throws FileNotFoundException, IOException {
		List lst = new ArrayList();
		File assignmentDir = null;
		File[] assignments = null;
		if (courseFolder.exists() && courseFolder.isDirectory()) {
			assignmentDir = new File(courseFolder.getAbsolutePath()
					+ ASSIGNMENT_FOLDER);
			if (assignmentDir.exists() && assignmentDir.isDirectory()) {
				assignments = assignmentDir.listFiles();
				for (int i = 0; i < assignments.length; i++) {
					if (assignments[i].exists() && assignments[i].isDirectory()) {
						lst.add(new AssignmentKey(
								(CourseFSUtils.unserialize(new FileReader(
														new File(courseFolder
																.getAbsolutePath()
																+AssignmentFSBean.COURSE_FILENAME)))),
										assignments[i].getName()));
					}
				}
			}
		}
		return lst;
	}

	public static void serialize(AssignmentKey key,Writer writer)throws TryIOException {
		try {
			PrintWriter writer2 = new PrintWriter(writer);
			writer2.println(key.getAssignmentNumber());

			writer2.flush();
			CourseFSUtils.serialize(key.getAssociatedCourse(), writer2);
		} catch (IOException e) {
			throw new TryIOException("Problem saving AssignmentKey",e);
		}
	}

	public static AssignmentKey unserialize(Reader reader) throws TryIOException{
		BufferedReader reader2 = new BufferedReader(reader);
		AssignmentKey key = null;
		CourseKey crs = null;
		String name = null;
		try{
		name =reader2.readLine();
		crs = CourseFSUtils.unserialize(reader2);
		}catch(IOException e){
			throw new TryIOException("Problem Reading AssignmentKey",e);
		}
		return new AssignmentKey(crs,name);
	}

}
