package edu.usc.glidein.service.impl;

import org.globus.wsrf.ResourceException;
import org.globus.wsrf.ResourceKey;
import org.globus.wsrf.PersistentResource;
import org.globus.wsrf.ResourceProperties;
import org.globus.wsrf.ResourcePropertySet;
import org.globus.wsrf.impl.ReflectionResourceProperty;
import org.globus.wsrf.impl.SimpleResourcePropertySet;

import edu.usc.glidein.service.db.Database;
import edu.usc.glidein.service.db.DatabaseException;
import edu.usc.glidein.service.db.GlideinDAO;
import edu.usc.glidein.stubs.types.Glidein;
import edu.usc.glidein.stubs.types.GlideinStatus;

public class GlideinResource implements PersistentResource, ResourceProperties
{
	private SimpleResourcePropertySet resourceProperties;
	private Glidein glidein = null;
	private GlideinStatus status = null;
	
	/**
	 * Default constructor required
	 */
	public GlideinResource() { }
	
	public GlideinResource(Glidein glidein, GlideinStatus status) throws ResourceException
	{
		this.glidein = glidein;
		this.status = status;
		setResourceProperties();
	}
	
	public void setStatus(GlideinStatus status) {
		this.status = status;
	}
	
	public GlideinStatus getStatus() {
		return status;
	}
	
	public void setGlidein(Glidein glidein) {
		this.glidein = glidein;
	}
	
	public Glidein getGlidein() {
		return glidein;
	}
	
	private void setResourceProperties() throws ResourceException {
		try {
			resourceProperties = new SimpleResourcePropertySet(GlideinQNames.RESOURCE_PROPERTIES);
			resourceProperties.add(new ReflectionResourceProperty(GlideinQNames.RP_GLIDEIN_ID,"Id",glidein));
			// TODO Set the rest of the resource properties
		} catch(Exception e) {
			throw new ResourceException("Unable to set glidein resource properties",e);
		}
	}
	
	public synchronized void create() throws ResourceException
	{
		try {
			Database db = Database.getDatabase();
			GlideinDAO dao = db.getGlideinDAO();
			dao.create(glidein);
			setResourceProperties();
		} catch (DatabaseException dbe) {
			throw new ResourceException(dbe);
		}
	}

	public synchronized void load(ResourceKey key) throws ResourceException {
		System.out.println("Loading "+key.getValue());
		int id = ((Integer)key.getValue()).intValue();
		try {
			Database db = Database.getDatabase();
			GlideinDAO dao = db.getGlideinDAO();
			glidein = dao.load(id);
			status = dao.getStatus(id);
			setResourceProperties();
		} catch(DatabaseException de) {
			throw new ResourceException(de);
		}
	}
	
	public synchronized void store() throws ResourceException {
		System.out.println("Storing "+getGlidein().getId());
		try {
			Database db = Database.getDatabase();
			GlideinDAO dao = db.getGlideinDAO();
			dao.store(glidein);
			dao.updateStatus(glidein.getId(), status);
		} catch(DatabaseException de) {
			throw new ResourceException(de);
		}
	}

	public Object getID() {
		return new Integer(glidein.getId());
	}

	public void remove() throws ResourceException {
		
	}
	
	public synchronized void delete() throws ResourceException {
		System.out.println("Deleting "+getGlidein().getId());
		try {
			Database db = Database.getDatabase();
			GlideinDAO dao = db.getGlideinDAO();
			dao.delete(glidein.getId());
			glidein = null;
			status = null;
			resourceProperties = null;
		} catch(DatabaseException de) {
			throw new ResourceException(de);
		}
	}

	public ResourcePropertySet getResourcePropertySet() {
		return resourceProperties;
	}
}

@@ -542,58 +542,8 @@
 ein;
-%0Aimport edu.usc.glidein.stubs.types.GlideinStatus;
 %0A%0Apu
@@ -713,46 +713,8 @@
 ll;%0A
-%09private GlideinStatus status = null;%0A
 %09%0A%09/
@@ -829,30 +829,8 @@
 dein
-, GlideinStatus status
 ) th
@@ -885,32 +885,8 @@
 in;%0A
-%09%09this.status = status;%0A
 %09%09se
@@ -917,142 +917,8 @@
 %7D%0A%09%0A
-%09public void setStatus(GlideinStatus status) %7B%0A%09%09this.status = status;%0A%09%7D%0A%09%0A%09public GlideinStatus getStatus() %7B%0A%09%09return status;%0A%09%7D%0A%09%0A
 %09pub
@@ -1171,25 +1171,24 @@
 ySet(Glidein
-Q
 Names.RESOUR
@@ -1271,9 +1271,8 @@
 dein
-Q
 Name
@@ -2045,39 +2045,8 @@
 d);%0A
-%09%09%09status = dao.getStatus(id);%0A
 %09%09%09s
@@ -2380,54 +2380,8 @@
 n);%0A
-%09%09%09dao.updateStatus(glidein.getId(), status);%0A
 %09%09%7D 
@@ -2818,16 +2818,16 @@
 tId());%0A
+
 %09%09%09glide
@@ -2841,26 +2841,8 @@
 ll;%0A
-%09%09%09status = null;%0A
 %09%09%09r

Remove GlideinStatus