// UnregisterByAuthority.java
//
//    Created: August 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.shared.MobyService;
import org.biomoby.shared.Central;
import org.biomoby.client.CentralImpl;

import org.tulsoft.tools.BaseCmdLine;

import java.util.Map;
import java.util.Iterator;

/**
 * This is a one-purpose code that unregisters all services registered
 * by a single authority. Only services that *can* be unregistered are
 * unregistered, of course (it depends on their field SignatureURL). <p>
 *
 * The command-line arguments for this client are:
 * <pre>
 *    Usage: java UnregisterByAuthority [-e <endpoint>] [-u <uri>] [-debug] authority
 * </pre>
 * where <em>endpoint</em> is a URL of a Moby Central registry
 * (default value is taken from <tt>org.biomoby.client.CentralImpl.DEFAULT_ENDPOINT</tt>)
 * and <em>uri</em> is a namespace used by that service
 * (default value is taken from <tt>org.biomoby.client.CentralImpl.DEFAULT_NAMESPACE</tt>).
 * And <tt>authority</tt> is the one whose services are going to be unregistered. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: UnregisterByAuthority.java,v 1.3 2005/10/12 11:36:59 senger Exp $
*/
public class UnregisterByAuthority {

    /*************************************************************************
     *
     * Entry point...
     *
     *************************************************************************/
    public static void main (String [] args) {
	try {

	    BaseCmdLine cmd = new BaseCmdLine (args, true);
	    if (cmd.hasParam ("-help") || cmd.hasParam ("-h")) {
		System.out.println
		    ("Usage: java TestingCentral [-e <endpoint>] [-u <uri>] <authority>");
		System.exit (0);
	    }
	    Central worker = new CentralImpl (cmd.getParam ("-e"),
					      cmd.getParam ("-u"));
	    if (cmd.params.length == 0)
		return;

	    String authority = cmd.params[0];
	    System.out.println ("Getting list of all services...");
	    Map authorities = worker.getServiceNamesByAuthority();

	    for (Iterator it = authorities.entrySet().iterator(); it.hasNext(); ) {
		Map.Entry entry = (Map.Entry)it.next();
		if (entry.getKey().equals (authority)) {
		    String[] names = (String[])entry.getValue();
		    for (int i = 0; i < names.length; i++) {
			MobyService service = new MobyService (names[i]);
			service.setAuthority (authority);
			System.out.print ("Unregistering service " + service.toShortString() + "...");
			worker.unregisterService (service);
			System.out.println ("done");
		    }
		    break;
		}
	    }

	} catch (Exception e) {
	    System.err.println ("===ERROR===");
	    e.printStackTrace();
	    System.err.println ("===========");
	}
    }
}
