package org.biomoby.registry.rdfagent.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.transport.http.HTTPConstants;

/**
 * 
 * @author Eddie
 * created Feb 20, 2006
 */
public class CentralAdmin {

	private String endpoint = "http://localhost/cgi-bin/MOBY-Admin.pl";

	private String uri = "http://localhost/MOBY/Admin";
	
	private String password = "";
	private String username = "";
	private String phrase = "";

	/**
	 * 
	 * @param endpoint the biomoby registry endpoint 
	 * @param uri the biomoby registry uri
	 * @param username the user name needed for authorization
	 * @param password the password needed for authorization
	 * @param phrase the phrase that is contained in the mobycentral.config file
	 */
	public CentralAdmin(String endpoint, String uri,String username, String password, String phrase) {
		this.endpoint = endpoint;
		this.uri = uri;
		this.username = username;
		this.password = password;
		this.phrase = phrase;
	}

	/**
	 * 
	 * @param servicename
	 *            the name of the service to remove
	 * @param authority
	 *            the authority of the service
	 * @return an int that is one of the following:
	 *         <p>
	 *         101 - missing authority
	 *         <p>
	 *         102 - missing service name
	 *         <p>
	 *         103 - missing pass phrase
	 *         <p>
	 *         200 - service successfully removed
	 *         <p>
	 *         404 - service not found in the registry
	 *         <p>
	 *         501 - key phrase didn't match
	 *         <p>
	 *         505 - unknown error
	 *         <p>
	 *         506 - soap service exception
	 *         <p>
	 *         507 - axis fault
	 *         <p>
	 *         508 - bad url
	 *         <p>
	 */
	public int deleteService(String servicename, String authority) {
		// set up an authenticator

		Service service = new Service();
		Call call = null;
		try {
			call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(endpoint));
			call.setTimeout(new Integer(0));
			call.setSOAPActionURI(uri + "#" + "deregisterService");
			call.setReturnType(org.apache.axis.Constants.XSD_INT);
			call.addParameter("message", org.apache.axis.Constants.XSD_STRING,
					String.class, ParameterMode.IN);
			call.setPassword(password);
			call.setUsername(username);
		    Hashtable<String, String> t = new Hashtable<String, String>();
		    t.put(HTTPConstants.HEADER_USER_AGENT, "RDFAgent/1.0");
			call.setProperty(HTTPConstants.REQUEST_HEADERS, t);
			String xml = "<CentralAdmin>" + "\t<name>" + servicename
					+ "</name>" + "\t<phrase>" + phrase + "</phrase>"
					+ "\t<authority>" + authority + "</authority>"
					+ "</CentralAdmin>";
			Object result = call.invoke(uri, "deregisterService",
					new Object[] { xml });
			if (result != null && !result.equals("")) {
				return (new Integer(result + "")).intValue();
			} else {
				return 505;
			}
		} catch (ServiceException e) {
			System.err.println(e.getLocalizedMessage());
			return 506;
		} catch (AxisFault e) {
			System.err.println(e.getLocalizedMessage());
			return 507;
		} catch (MalformedURLException e) {
			System.err.println(e.getLocalizedMessage());
			return 508;
		}
	}
}
