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.SiteDAO;
import edu.usc.glidein.stubs.types.Site;
import edu.usc.glidein.stubs.types.SiteStatus;

public class SiteResource implements PersistentResource, ResourceProperties
{
	private SimpleResourcePropertySet resourceProperties;
	private Site site;
	private SiteStatus status;
	
	/**
	 * Default constructor required
	 */
	public SiteResource() { }
	
	public SiteResource(Site site, SiteStatus status) throws ResourceException
	{
		this.site = site;
		this.status = status;
		setResourceProperties();
	}
	
	public void setSite(Site site) {
		this.site = site;
	}
	
	public Site getSite() {
		return site;
	}
	
	public void setStatus(SiteStatus status) {
		this.status = status;
	}
	
	public SiteStatus getStatus() {
		return status;
	}
	
	private void setResourceProperties() throws ResourceException
	{
		try {
			resourceProperties = new SimpleResourcePropertySet(SiteQNames.RESOURCE_PROPERTIES);
			resourceProperties.add(new ReflectionResourceProperty(
					SiteQNames.RP_SITE_ID,"Id",site));
			// TODO Set the rest of the resource properties
		} catch(Exception e) {
			throw new ResourceException("Unable to set site resource properties",e);
		}
	}
	
	public synchronized void create() throws ResourceException
	{
		try {
			Database db = Database.getDatabase();
			SiteDAO dao = db.getSiteDAO();
			dao.create(site);
		} catch (DatabaseException de) {
			throw new ResourceException(de);
		}
	}

	public synchronized void load(ResourceKey key) throws ResourceException
	{
		System.out.println("Loading "+key.getValue()); //XXX
		int id = ((Integer)key.getValue()).intValue();
		try {
			Database db = Database.getDatabase();
			SiteDAO dao = db.getSiteDAO();
			site = 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 "+getSite().getId()); //XXX
		try {
			Database db = Database.getDatabase();
			SiteDAO dao = db.getSiteDAO();
			dao.store(site);
			dao.updateStatus(site.getId(), status);
		} catch(DatabaseException de) {
			throw new ResourceException(de);
		}
	}
	
	public synchronized void delete() throws ResourceException
	{
		System.out.println("Deleting "+getSite().getId()); //XXX
		try {
			Database db = Database.getDatabase();
			SiteDAO dao = db.getSiteDAO();
			dao.delete(site.getId());
			site = null;
			status = null;
			resourceProperties = null;
		} catch(DatabaseException de) {
			throw new ResourceException(de);
		}
	}
	
	public Object getID() {
		return new Integer(site.getId());
	}

	public void remove() throws ResourceException {
		
	}

	public ResourcePropertySet getResourcePropertySet() {
		return resourceProperties;
	}
}

@@ -536,55 +536,8 @@
 ite;
-%0Aimport edu.usc.glidein.stubs.types.SiteStatus;
 %0A%0Apu
@@ -691,36 +691,8 @@
 te;%0A
-%09private SiteStatus status;%0A
 %09%0A%09/
@@ -795,27 +795,8 @@
 site
-, SiteStatus status
 ) th
@@ -845,32 +845,8 @@
 te;%0A
-%09%09this.status = status;%0A
 %09%09se
@@ -981,136 +981,8 @@
 %7D%0A%09%0A
-%09public void setStatus(SiteStatus status) %7B%0A%09%09this.status = status;%0A%09%7D%0A%09%0A%09public SiteStatus getStatus() %7B%0A%09%09return status;%0A%09%7D%0A%09%0A
 %09pri
@@ -1105,25 +1105,24 @@
 ertySet(Site
-Q
 Names.RESOUR
@@ -1208,9 +1208,8 @@
 Site
-Q
 Name
@@ -1932,39 +1932,8 @@
 d);%0A
-%09%09%09status = dao.getStatus(id);%0A
 %09%09%09s
@@ -2262,51 +2262,8 @@
 e);%0A
-%09%09%09dao.updateStatus(site.getId(), status);%0A
 %09%09%7D 
@@ -2404,67 +2404,8 @@
 %0A%09%7B%0A
-%09%09System.out.println(%22Deleting %22+getSite().getId()); //XXX%0A
 %09%09tr
@@ -2532,26 +2532,8 @@
 ll;%0A
-%09%09%09status = null;%0A
 %09%09%09r

Remove SiteStatus