import javax.xml.registry.*;
import javax.xml.registry.infomodel.*;
import java.net.*;
import java.util.*;

/*
 * This is the FindBusiness UDDI example implemented using
 * the JAXR libraries and the reference implementation
 * JAXR provider for accessing a UDDI registry.
 */
public class JAXRFindBusiness {
	static String queryString = new String("%");

	public static void main(String[] args) {
		
		String inquiryURL = "http://localhost:8080/juddi/inquiry";
		
		String publishURL = "http://localhost:8080/juddi/publish";

		System.out.println("Query string is " + queryString);

		Connection conn = null;

		// Define connection configuration properties
		// To query, you need only the query URL
		Properties props = new Properties();

		props.setProperty("javax.xml.registry.queryManagerURL",
				inquiryURL);
		props.setProperty("javax.xml.registry.lifeCycleManagerURL",
				publishURL);
		props.setProperty("javax.xml.registry.ConnectionFactoryClass",
				"org.apache.ws.scout.registry.ConnectionFactoryImpl");
		props.setProperty("scout.proxy.transportClass", "org.apache.ws.scout.transport.AxisTransport");
		props.setProperty("javax.xml.registry.uddi.maxRows", String.valueOf(100));

		
//		props.setProperty("javax.xml.registry.ConnectionFactoryClass",
//				"org.apache.ws.scout.registry.ConnectionFactoryImpl");

		// String transportClass = System.getProperty(
		// "juddi.proxy.transportClass",
		// "org.jboss.jaxr.juddi.transport.SaajTransport");

		// String transportClass = "org.apache.juddi.proxy.AxisTransport";
		//		
		// System.setProperty("juddi.proxy.transportClass", transportClass);

		try {
			// Create the connection, passing it the
			// configuration properties
			// ConnectionFactory factory =
			// ConnectionFactory.newInstance( );
			// factory.setProperties(props);
			// conn = factory.createConnection( );
			conn = getConnection(props);

			// Get registry service and query manager
			RegistryService rs = conn.getRegistryService();
			BusinessQueryManager bqm = rs.getBusinessQueryManager();

			// Define find qualifiers and name patterns
			Collection qualifiers = new ArrayList();
			qualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
			Collection namePatterns = new ArrayList();
			namePatterns.add(queryString);

			// Find using the name
			BulkResponse response = bqm.findOrganizations(qualifiers,
					namePatterns, null, null, null, null);
			Collection orgs = response.getCollection();

			System.out.println("Organization(s) found: " + orgs.size());

			// Display information about the organizations found
			Iterator orgIter = orgs.iterator();
			while (orgIter.hasNext()) {
				Organization org = (Organization) orgIter.next();
				System.out.println("Organization key: " + getKey(org));
				System.out.println("Organization name: " + getName(org));
				System.out.println("Organization description: "
						+ getDescription(org));

				// Display primary contact information
				User pc = org.getPrimaryContact();
				if (pc != null) {
					PersonName pcName = pc.getPersonName();
					System.out
							.println(" Contact name: " + pcName.getFullName());
					Collection phNums = pc.getTelephoneNumbers(pc.getType());
					System.out
							.println("  Phone Numbers size: " + phNums.size());
					Iterator phIter = phNums.iterator();
					while (phIter.hasNext()) {
						TelephoneNumber num = (TelephoneNumber) phIter.next();
						System.out
								.println("  Phone number: " + num.getNumber());
					}
					Collection eAddrs = pc.getEmailAddresses();
					System.out.println("  Email Addresses size: "
							+ eAddrs.size());
					Iterator eaIter = eAddrs.iterator();
					while (eaIter.hasNext()) {
						System.out.println("  Email Address: "
								+ ((EmailAddress) eaIter.next()).getAddress());
					}

					Collection postAddrs = pc.getPostalAddresses();
					System.out.println("  Postal Addresses Size: "
							+ postAddrs.size());
					Iterator paIter = postAddrs.iterator();
					while (paIter.hasNext()) {
						System.out.println("  Postal Address: "
								+ (PostalAddress) paIter.next());
					}

				}

				// Display service and binding information
				Collection services = org.getServices();
				System.out.println("Services found: " + services.size());
				Iterator svcIter = services.iterator();
				while (svcIter.hasNext()) {
					Service svc = (Service) svcIter.next();
					System.out.println(" Service name: " + getName(svc));
					System.out.println(" Service description: "
							+ getDescription(svc));
					Collection serviceBindings = svc.getServiceBindings();
					Iterator sbIter = serviceBindings.iterator();
					while (sbIter.hasNext()) {
						ServiceBinding sb = (ServiceBinding) sbIter.next();
						System.out.println("  Binding " + "Description: "
								+ getDescription(sb));
						System.out
								.println("  Access URI: " + sb.getAccessURI());
					}
				}
				// Print spacer between organizations
				System.out.println(" --- ");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// At end, close connection to registry
			if (conn != null) {
				try {
					conn.close();
				} catch (JAXRException je) {
				}
			}
		}
	}

	private static Connection getConnection(Properties props)
			throws JAXRException {
		ConnectionFactory factory = ConnectionFactory.newInstance();
		System.out.println("==================================");
		System.out.println("ConnectionFactory CLASS: "
				+ factory.getClass().getName());
		System.out.println("==================================");

		factory.setProperties(props);
		Connection conn = factory.createConnection();
		if (conn == null)
			System.out.println("No Connection");
		return conn;
	}

	private static String getName(RegistryObject ro) throws JAXRException {
		try {
			return ro.getName().getValue();
		} catch (NullPointerException npe) {
			return "";
		}
	}

	private static String getDescription(RegistryObject ro)
			throws JAXRException {
		try {
			return ro.getDescription().getValue();
		} catch (NullPointerException npe) {
			return "";
		}
	}

	private static String getKey(RegistryObject ro) throws JAXRException {
		try {
			return ro.getKey().getId();
		} catch (NullPointerException npe) {
			return "";
		}
	}
}