package edu.usc.glidein.service.impl;

import java.net.URL;
import java.rmi.RemoteException;

import org.apache.axis.MessageContext;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.apache.log4j.Logger;
import org.globus.wsrf.ResourceContext;
import org.globus.wsrf.ResourceKey;
import org.globus.wsrf.container.ServiceHost;
import org.globus.wsrf.utils.AddressingUtils;

import edu.usc.glidein.stubs.types.Site;

public class SiteFactoryService
{
	private Logger logger = Logger.getLogger(SiteFactoryService.class);

	public SiteFactoryService() { }
	
	public EndpointReferenceType createSite(Site site)
	throws RemoteException
	{
		// Create a new resource
		ResourceKey key = null;
		try {
			ResourceContext rctx = ResourceContext.getResourceContext();
			SiteResourceHome home = (SiteResourceHome) rctx.getResourceHome();
			key = home.create(site);
		} catch (Exception e) {
			String message = "Unable to create site resource";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
		
		// Create an endpoint reference for the new resource
		EndpointReferenceType epr = null;
		try {
			URL baseURL = ServiceHost.getBaseURL();
			MessageContext mctx = MessageContext.getCurrentContext();
			String svc = (String) mctx.getService().getOption("instance");
			String instanceURI = baseURL.toString() + svc;
			epr = AddressingUtils.createEndpointReference(instanceURI, key);
		} catch (Exception e) {
			String message = "Unable to create endpoint reference";
			logger.error(message,e);
			throw new RemoteException(message, e);
		}
		
		// Return the endpoint reference to the client
		return epr;
	}
}
@@ -420,23 +420,252 @@
 in.s
-tubs.types.Site
+ervice.db.Database;%0Aimport edu.usc.glidein.service.db.DatabaseException;%0Aimport edu.usc.glidein.service.db.SiteDAO;%0Aimport edu.usc.glidein.stubs.types.Sites;%0Aimport edu.usc.glidein.stubs.types.Site;%0Aimport edu.usc.glidein.stubs.types.SiteStatus
 ;%0A%0Ap
@@ -882,11 +882,148 @@
 ion%0A
-
 %09%7B%0A
+%09%09// TODO Initialize site%0A%09%09site.setStatus(SiteStatus.NEW);%0A%09%09site.setStatusMessage(%22Created%22);%0A%09%09site.setSubmitPath(%22/submit/path%22);%0A%09%09%0A
 %09%09//
@@ -2013,9 +2013,331 @@
 epr;%0A%09%7D%0A
+%09%0A%09public Sites listSites(boolean longFormat)%0A%09throws RemoteException%0A%09%7B%0A%09%09try %7B%0A%09%09%09Database db = Database.getDatabase();%0A%09%09%09SiteDAO dao = db.getSiteDAO();%0A%09%09%09Site%5B%5D sites = dao.list(longFormat);%0A%09%09%09return new Sites(sites);%0A%09%09%7D catch(DatabaseException de) %7B%0A%09%09%09throw new RemoteException(%22Unable to list sites%22,de);%0A%09%09%7D%0A%09%7D%0A
 %7D

Add new site initialization (needs more work) Add listSites