package edu.usc.glidein.cli;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.cli.CommandLine;

import edu.usc.glidein.api.SiteService;
import edu.usc.glidein.service.impl.SiteNames;

public class RemoveSiteCommand extends Command
{	
	private boolean force;
	private List<Integer> ids;
	
	public RemoveSiteCommand()
	{
		ids = new LinkedList<Integer>();
	}
	
	
	public void addOptions(List<Option> options)
	{
		options.add(
			Option.create()
				  .setOption("f")
				  .setLongOption("force")
				  .setUsage("-f [--force]")
				  .setDescription("Force the site to be deleted regardless of state")
		);
	}
	
	public void setArguments(CommandLine cmdln) throws CommandException
	{
		// Force
		if (cmdln.hasOption("f")) {
			force = true;
		} else {
			force = false;
		}
		
		// Get IDs
		String[] args = cmdln.getArgs();
		if (args.length == 0) {
			throw new CommandException(getUsage());
		}
		for (String arg : args) {
			if (arg.matches("[1-9][0-9]*")) {
				int id = Integer.parseInt(arg);
				ids.add(id);
			} else {
				System.out.println("Invalid site id: "+arg);
			}
		}
	}
	
	public void execute() throws CommandException
	{
		/* Delete all the sites */
		for (int id : ids) {
			try {
				if (isDebug()) System.out.print("Removing site "+id+"... ");
				SiteService instance = new SiteService(
						getServiceURL(SiteNames.SITE_SERVICE),id);
				instance.setDescriptor(getClientSecurityDescriptor());
				instance.remove(force);
				if (isDebug()) System.out.println("done.");
			} catch (Exception e) {
				System.out.println("Unable to remove site '"+id+"': "+e.getMessage());
				if (isDebug()) e.printStackTrace();
			}
		}
	}
	
	public String getName()
	{
		return "remove-site";
	}
	
	public String[] getAliases()
	{
		return new String[]{"rs"};
	}
	
	public String getDescription()
	{
		return "remove-site (rs): Remove an existing site";
	}
	
	public String getUsage()
	{
		return "Usage: remove-site ID...";
	}
}

@@ -98,31 +98,185 @@
 che.
-commons.cli.CommandLine
+axis.message.addressing.EndpointReferenceType;%0Aimport org.apache.commons.cli.CommandLine;%0Aimport org.globus.gsi.GlobusCredential;%0Aimport org.globus.gsi.GlobusCredentialException
 ;%0A%0Ai
@@ -464,16 +464,54 @@
 r%3E ids;%0A
+%09private GlobusCredential credential;%0A
 %09%0A%09publi
@@ -823,16 +823,286 @@
 %22)%0A%09%09);%0A
+%09%09options.add(%0A%09%09%09Option.create()%0A%09%09%09%09  .setOption(%22C%22)%0A%09%09%09%09  .setLongOption(%22credential%22)%0A%09%09%09%09  .setUsage(%22-C %5B--credential%5D %3Cfile%3E%22)%0A%09%09%09%09  .setDescription(%22The user's credential as a proxy file. If not specified the Globus default is used.%22)%0A%09%09%09%09  .hasArgument()%0A%09%09);%0A
 %09%7D%0A%09%0A%09pu
@@ -1578,72 +1578,764 @@
 %09%7D%0A%09
-%7D%0A%09%0A%09public void execute() throws CommandException%0A%09%7B%0A%09%09/* Delet
+%09%0A%09%09/* Get proxy credential */%0A%09%09if (cmdln.hasOption(%22C%22)) %7B%0A%09%09%09String proxy = cmdln.getOptionValue(%22C%22);%0A%09%09%09try %7B%0A%09%09%09%09credential = new GlobusCredential(proxy);%0A%09%09%09%7D catch (GlobusCredentialException ce) %7B%0A%09%09%09%09throw new CommandException(%22Unable to read proxy %22 +%0A%09%09%09%09%09%09%22credential: %22+proxy+%22: %22+ce.getMessage(),ce);%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09try %7B%0A%09%09%09%09credential = GlobusCredential.getDefaultCredential();%0A%09%09%09%7D catch (GlobusCredentialException ce) %7B%0A%09%09%09%09throw new CommandException(%22Unable to read default proxy %22 +%0A%09%09%09%09%09%09%22credential: %22+ce.getMessage(),ce);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void execute() throws CommandException%0A%09%7B%0A%09%09/* Delegate a credential for the uninstall operation */%0A%09%09EndpointReferenceType credentialEPR = delegateCredential(credential);%0A%09%09%0A%09%09/* Remov
 e al
@@ -2623,16 +2623,30 @@
 ve(force
+,credentialEPR
 );%0A%09%09%09%09i

Add delegated credential to remove operation