//Title:       DataStore
//Version:	   1.7
//Author:      Stuart Lowry & Scott Palmateer
//Company:     Science Applications International Corporation
//Description: Data repository for managing data files that are to be served up.

//package saic.oaa2;

import java.io.*;
import java.util.*;

/**
* This class implements a data store, into which data can be placed, and out
* of which the data can be retrieved. Data stored in the data store is accessed 
* via unique reference numbers.
*
* @author Scott Palmateer & Stuart Lowry
* @version 1.7
*
**/

public class DataStore
{
	int keynum=0;
	Hashtable store = new Hashtable();
	String username = null;
	String basedir = null;
	Vector files = new Vector();

	/**
	* Constructor. Sets the user name that this data store represents, and 
	* the directory in which data should be placed.
	*
	* @param usernamep user name
	* @param basedirp directory in which data will be physically placed
	**/
	public DataStore (String usernamep, String basedirp)
	{
		username = usernamep;
		basedir = basedirp;
	}

	/**
	* Generates a unique filename given a reference number.
	*
	* @return unique File (or null on error)
	**/
	File generateTempFile()
	{
		File f = null;
		try {
			f = File.createTempFile("stage",username, new File(basedir));
			f.deleteOnExit();
			files.add(f);
		} catch (Exception ex) {
			System.err.println("datastore: caught exception (generateTempFile()): " + ex.getClass().getName());
		}
		return f;
	}
	/**
	* Physically writes data to a file
	*
	* @param filename the file into which data is to be written
	* @param data the data to be written
	**/
	void writeData(String filename, String data)
	{
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
			bw.write(data, 0, data.length());
			bw.flush();
			bw.close();
		} catch (Exception ex) {
			System.err.println("datastore: caught exception (generateTempFile()): " + ex.getClass().getName());
		}
	}
	/**
	* Physically reads data from a file
	*
	* @param filename the file from which data is to be read
	* @return the data from this file
	**/
	String readData(String filename)
	{
		StringBuffer accum = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line = br.readLine();
			while(line != null)
			{
				accum.append(line);
				accum.append("\n");
				line=br.readLine();
			}
			br.close();
		} catch (FileNotFoundException ex) {
			System.err.println("datastore: caught exception (readData()): " + 
				ex.getClass().getName()+ "\nfile not found "+filename);
		}
		catch (IOException ex) {
			System.err.println("datastore: caught exception (readData()): " + 
				ex.getClass().getName()+ "\nIO Esception ");
		}
		return accum.toString();
	}
	/**
	* Store some data
	*
	* @param data the data to be stored
	* @return the refernce number by which this data can be retrieved
	**/
	public String put(String data)
	{
		String key = "";
		File f = generateTempFile();
		if (f != null)
		{
			int thiskeynum = keynum;
			keynum++;
			String filename = f.toString();
			writeData(filename,data);
			key = Integer.toString(thiskeynum);
			store.put(key,f);
		}
		return key;
	}
	/**
	* Get some data, given a reference to it. Once data is retrieved, it cannot be
	* retrieved again.
	*
	* @param key the reference number
	* @return the data
	**/
	public String get(String key)
	{
		String data = null;
		File f = (File)store.get(key);
		if (f != null)
		{
			String filename = f.toString();
			data = readData(filename);
			store.remove(key);
			f.delete();
		}
		return data;
	}
	/**
	* Get the filename of the data associated with the given reference.
	*
	* @param key the reference number
	* @return the file name where the data is stored
	**/
	public String getFile(String key)
	{
		String retval = null;
		File f = (File)store.get(key);
		if (f != null)
		{
			retval = f.toString();
			store.remove(key);
		}
		return retval;
	}
	/**
	* Store the contents of a file
	*
	* @param filename the name of the file to be stored
	* @return the reference by which this data can be retrieved
	**/
	public String putFile(String filename)
	{
		String key = "";
		File f = generateTempFile();
		if (f != null)
		{
			int thiskeynum = keynum;
			keynum++;
			String data = readData(filename);
			String tmpfilename = f.toString();
			writeData(tmpfilename,data);
			key = Integer.toString(thiskeynum);
			store.put(key,f);
		}
		return key;
	}
}
