import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;

class TRestrictionList {
    private String CATALOG_NAME;

    public TRestrictionList() {
		CATALOG_NAME = new String("");
	}

	public String getCATALOG_NAME() {
		return CATALOG_NAME;
	}

	public void setCATALOG_NAME(String name) {
		CATALOG_NAME = name;
	}
}

class TRestrictions {
	public TRestrictions() {
		RestrictionList = new TRestrictionList();
	}

	public TRestrictionList getRestrictionList() {
		return RestrictionList;
	}

	public void setRestrictionList(TRestrictionList list) {
		RestrictionList = list;
	}

    private TRestrictionList RestrictionList;
}

class TPropertyList {
    private String DataSourceInfo;
    private String Format;
    private String Catalog;

    public TPropertyList() {
		DataSourceInfo = new String("");
		Format = new String("");
		Catalog = new String("");
	}

	public String getDataSourceInfo() {
		return DataSourceInfo;
	}

	public String getFormat() {
		return Format;
	}

	public String getCatalog() {
		return Catalog;
	}

	public void setDataSourceInfo(String dsInfo) {
		DataSourceInfo = dsInfo;
	}

	public void setFormat( String format) {
		Format = format;
	}

	public void setCatalog(String cat) {
		Catalog = cat;
	}
}

class TProperties {
    private TPropertyList PropertyList;

    public TProperties() {
		PropertyList = new TPropertyList();
	}

	public TPropertyList getPropertyList() {
		return PropertyList;
	}

	public void setPropertyList( TPropertyList  pList) {
		PropertyList = pList;
	}
}

public class DiscoverCubes {

	public static void main(String[] args) throws Exception {

		URL url = new URL ("http://localhost/xmla/msxisapi.dll");


		SOAPMappingRegistry smr = new SOAPMappingRegistry ();
		StringDeserializer sd = new StringDeserializer ();
		BeanSerializer beanSer = new BeanSerializer();
		smr.mapTypes (Constants.NS_URI_SOAP_ENC,    new QName ("", "Result"), null, null, sd);
		smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName("urn:schemas-microsoft-com:xml-analysis",
			"Restrictions"), TRestrictions.class, beanSer, beanSer);
		smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName("urn:schemas-microsoft-com:xml-analysis",
			"Properties"), TProperties.class, beanSer, beanSer);
		smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName("urn:schemas-microsoft-com:xml-analysis",
			"RestrictionList"), TRestrictionList.class, beanSer, beanSer);
		smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName("urn:schemas-microsoft-com:xml-analysis",
			"PropertyList"), TPropertyList.class, beanSer, beanSer);

		System.out.println("Mapped All Types");

		// create the transport and set parameters
		SOAPHTTPConnection st = new SOAPHTTPConnection();

		// build the call.
		Call call = new Call ();
		call.setSOAPTransport(st);
		call.setSOAPMappingRegistry (smr);

		call.setTargetObjectURI ("urn:schemas-microsoft-com:xml-analysis");
		call.setMethodName("Discover");
		call.setEncodingStyleURI ("http://schemas.xmlsoap.org/soap/encoding/");
		System.out.println("Discover Cubes...........1");

		Vector params = new Vector();
		TRestrictions restrictions = new TRestrictions();
		restrictions.getRestrictionList().setCATALOG_NAME("FoodMart 2000");
		System.out.println("Discover Cubes...........2");

		TProperties properties = new TProperties();
		properties.getPropertyList().setDataSourceInfo("Provider=MSOLAP;Data Source=s152969;");
		properties.getPropertyList().setFormat("Tabular");
		properties.getPropertyList().setCatalog("FoodMart 2000");
		System.out.println("Discover Cubes...........3");

		params.addElement(new Parameter("RequestType", String.class, "MDSCHEMA_CUBES", null));
		params.addElement(new Parameter("Restrictions", TRestrictions.class,
			restrictions, null));
		System.out.println("Discover Cubes...........4");
		params.addElement(new Parameter("Properties", TProperties.class,
			properties, null));
		System.out.println("Discover Cubes...........5");

		//params.addElement(new Parameter("Restrictions", String.class, "<RestrictionList><CATALOG_NAME>FoodMart 2000</CATALOG_NAME></RestrictionList>", null ));
		//params.addElement(new Parameter("Properties", String.class, "<PropertyList><DataSourceInfo>Provider=MSOLAP;Data Source=s152969;</DataSourceInfo><Catalog>FoodMart 2000</Catalog></PropertyList>", null ));



		call.setParams(params);
		System.out.println("Discover Cubes...........6");

		Response resp = null;
		try {
		  resp = call.invoke (url, "urn:schemas-microsoft-com:xml-analysis:Discover");
		  System.out.println("Discover Cubes...........7");
		}
		catch (SOAPException e) {
			System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " + e.getMessage ());
			return;
		}

		// check response
		if (resp != null && !resp.generatedFault()) {
		   Parameter ret = resp.getReturnValue();
		  Object value = ret.getValue();

		  System.out.println ("Answer--> " + value);
		}
		else {
			Fault fault = resp.getFault ();
			System.err.println ("Generated fault: ");
			System.out.println (" Fault Code = " + fault.getFaultCode());
			System.out.println (" Fault String = " + fault.getFaultString());
		}
	}
}

