import java.net.PasswordAuthentication;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.xml.registry.BulkResponse;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.JAXRException;
import javax.xml.registry.LifeCycleManager;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.EmailAddress;
import javax.xml.registry.infomodel.ExternalLink;
import javax.xml.registry.infomodel.Organization;
import javax.xml.registry.infomodel.PersonName;
import javax.xml.registry.infomodel.PostalAddress;
import javax.xml.registry.infomodel.RegistryObject;
import javax.xml.registry.infomodel.TelephoneNumber;
import javax.xml.registry.infomodel.User;

public class JAXRGetProviderOrganization {

	private static final String queryurl = "http://planetarium.hki.uni-koeln.de:8080/juddi/inquiry";

	private static final String publishurl = "http://planetarium.hki.uni-koeln.de:8080/juddi/publish";

	private static final String username = "provider";

	private static final String password = "";

	public static void main(String[] args) {
		System.err.println("STARTING JAXRGetProviderOrganization");
		try {
			
			PasswordAuthentication passwdAuth = new PasswordAuthentication(username, "password".toCharArray());
        	Set creds = new HashSet();
            creds.add(passwdAuth);
			
			
			Properties props = new Properties();
			props.setProperty("javax.xml.registry.queryManagerURL",
					"http://localhost:8080/juddi/inquiry");
			props.setProperty("javax.xml.registry.lifeCycleManagerURL",
					"http://localhost:8080/juddi/publish");
			
//			props.setProperty("javax.xml.registry.factoryClass",
//					"org.apache.ws.scout.registry.ConnectionFactoryImpl");
			
			
//			System.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);
			
//			String factoryString = "javax.xml.registry.ConnectionFactoryClass";
//			String factoryClass = System.getProperty(factoryString);
//			if (factoryClass == null || factoryClass.length() == 0)
//				System.setProperty(factoryString,
//						"org.apache.ws.scout.registry.ConnectionFactoryImpl");
			
			
			Connection connection = getConnection(props);
			connection.setCredentials(creds);
			RegistryService rs = connection.getRegistryService();
            BusinessQueryManager bqm = rs.getBusinessQueryManager();
            
            BulkResponse br = bqm.getRegistryObjects(LifeCycleManager.ORGANIZATION);
            
            Collection orgs = br.getCollection();
            System.out.println("Results found: " + orgs.size() + "\n");
            Iterator iter = orgs.iterator();
            
            while (iter.hasNext()) {
                Organization org = (Organization) iter.next();
                System.out.println("Organization Key: " + org.getKey().getId());
                System.out.println("Organization Name: " + org.getName().getValue());
                System.out.println("Organization Description: " + org.getDescription().getValue());
                
                              
                
                
                
//              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(  ));
                    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(  ));
                    }
                    
                }
                
                
                
            }
            
			

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private static Connection getConnection(Properties props) throws JAXRException {
        ConnectionFactory factory = ConnectionFactory.newInstance();
        
        System.err.println("============================");
        System.err.println("ConnectionFactory CLASS: " + factory.getClass().getName());
//        System.err.println("ConnectionFactory PROP: " + factory.getProperties().getProperty("javax.xml.registry.ConnectionFactoryClass"));
//        System.err.println("System PROP: " + System.getProperty("javax.xml.registry.ConnectionFactoryClass"));
//        System.err.println("QueryManager PROP: " + factory.getProperties().getProperty("javax.xml.registry.queryManagerURL"));
        System.err.println("============================");
        
        factory.setProperties(props);
        
        System.err.println("ConnectionFactory CLASS: " + factory.getClass().getName());
        System.err.println("ConnectionFactory PROP: " + factory.getProperties().getProperty("javax.xml.registry.ConnectionFactoryClass"));
        System.err.println("System PROP: " + System.getProperty("javax.xml.registry.ConnectionFactoryClass"));
        System.err.println("QueryManager PROP: " + factory.getProperties().getProperty("javax.xml.registry.queryManagerURL"));
        System.err.println("============================");
        
        Connection conn = factory.createConnection();
        if (conn == null) System.out.println("No Connection");
        return conn;
    }

}
