/*
 * $Id: DataManager.java,v 1.6 2003/12/18 18:49:54 bigmac Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: DataManager.java,v $
 * Revision 1.6  2003/12/18 18:49:54  bigmac
 * Überflüssige Imports gelöscht
 *
 * Revision 1.5  2003/12/10 14:43:43  koppy
 * ID-Generierung auf UUID umgestellt
 *
 * Revision 1.4  2003/08/02 16:59:23  bigmac
 * *** empty log message ***
 *
 * Revision 1.3  2003/05/02 12:08:48  koppy
 * + e.printStackTrace()
 *
 * Revision 1.2  2003/05/02 11:22:59  koppy
 * Additional contructor
 *
 * Revision 1.1  2002/10/21 14:36:13  bigmac
 * *** empty log message ***
 *
 * Revision 1.2  2002/07/24 13:34:09  moelle
 * Fehlendes import eingefuegt
 *
 * Revision 1.1  2002/04/29 14:42:44  koppy
 * initial revision
 *
 *
 */

package vkc.server.archive;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import vkc.util.FileUtils;

public class DataManager {

	private DataStore ds = null;

	/**
	 * Konstruktor
	 *
	 * @param   url - URL fuer den DataStore
	 * @author koppy
	 */
	public DataManager(String url) throws DataStoreException {

		setDataStore(DataStoreFactory.getDataStoreByUrl(url));

	}

	public DataManager(DataStore ds) {
		this.ds = ds;
	}

	/**
	 * Finalize
	 *
	 * @author koppy
	 */
	public void finalize() throws DataStoreException {

		if (ds != null) {
			ds.close();
			ds = null;
		}

	}

	/**
	 * Liefert den verwendeten DataStore
	 *
	 * @param   url - URL fuer das DataStore
	 * @author koppy
	 */
	public DataStore getDataStore() {
		return ds;
	}

	/**
	 * Setzt den zu verwendenden DataStore
	 *
	 * @param   ds - den zu verwendenden DataStore
	 * @author koppy
	 */
	public void setDataStore(DataStore ds) {

		this.ds = ds;
	}
	

	/**
	 * Speichert ein Objekt unter gegebener ID ab
	 *
	 * @param object - irgendein Object
	 * @param id - eine ID
	 * @author koppy
	 */
	public void saveObject(Object object, String id)
		throws DataStoreException {

		ByteArrayOutputStream baos = null;
		ObjectOutputStream serializer = null;
		try {

			baos = new ByteArrayOutputStream();
			serializer = new ObjectOutputStream(baos);

			serializer.writeObject(object);
			serializer.flush();

			// Im DataStore ablegen
			ds.store(baos.toByteArray(), id);

		} catch (Exception e) {
			
			if (ds == null)
				throw new DataStoreException("DataStore is referenced to null!");
			
			e.printStackTrace();
			throw new DataStoreException("Object could not be stored!");
		} finally {

			FileUtils.closeOutputStream(baos);
			FileUtils.closeOutputStream(serializer);
		}

	}

	/**
	 * Liefert das unter id gespeicherte Objekt
	 *
	 * @param object - irgendein Object
	 * @param id - eine ID
	 * @author koppy
	 */
	public Object loadObject(String id) throws DataStoreException {

		ByteArrayInputStream bais = null;
		ObjectInputStream deserializer = null;

		try {
			byte[] byteUser = ds.load(id);

			bais = new ByteArrayInputStream(byteUser);
			deserializer = new ObjectInputStream(bais);

			return deserializer.readObject();

		} catch (Exception e) {

			if (ds == null)
				throw new DataStoreException("DataStore is referenced to null!");
			
			e.printStackTrace();
			// Ansonsten Fehler !!!
			throw new DataStoreException("Object could not be load!");

		} finally {

			FileUtils.closeInputStream(bais);
			FileUtils.closeInputStream(deserializer);
		}

	}

}
