import java.net.PasswordAuthentication;
import java.util.ArrayList;
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.BusinessLifeCycleManager;
import javax.xml.registry.BusinessQueryManager;
import javax.xml.registry.Connection;
import javax.xml.registry.ConnectionFactory;
import javax.xml.registry.FindQualifier;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.EmailAddress;
import javax.xml.registry.infomodel.InternationalString;
import javax.xml.registry.infomodel.Organization;
import javax.xml.registry.infomodel.PersonName;
import javax.xml.registry.infomodel.Service;
import javax.xml.registry.infomodel.ServiceBinding;
import javax.xml.registry.infomodel.SpecificationLink;
import javax.xml.registry.infomodel.TelephoneNumber;
import javax.xml.registry.infomodel.User;

/*
 * Created on 27 nov. 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Xavier
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JAXRBrowser {

	public static void main(String[] args) {
		try {
		ConnectionFactory factory = 
			  ConnectionFactory.newInstance();
		
		Properties props = new Properties();
		
		//IBM
		props.setProperty("javax.xml.registry.queryManagerURL",
		  "http://uddi.ibm.com/testregistry/inquiryapi");
		props.setProperty("javax.xml.registry.lifeCycleManagerURL",
		  "https://uddi.ibm.com/testregistry/publishapi");
		
		//Microsoft
		//props.setProperty("javax.xml.registry.queryManagerURL",
		//  "http://test.uddi.microsoft.com/inquire");
		
		
		
		//props.setProperty("javax.xml.registry.security.authenticationMethod" , "UDDI_GET_AUTHTOKEN");
		String userName = "xblanc";
        String password = "un1deux2";
        PasswordAuthentication passwdAuth =
            new PasswordAuthentication(userName, password.toCharArray());
        Set creds = new HashSet();
        creds.add(passwdAuth);
        
		factory.setProperties(props);
		Connection connection = factory.createConnection();
		
		connection.setCredentials(creds);
		
		RegistryService rs = connection.getRegistryService();
		BusinessQueryManager bqm = rs.getBusinessQueryManager();
		BusinessLifeCycleManager blcm = 
		  rs.getBusinessLifeCycleManager();
		
		String qString = "%Coffee%";
//		 Define find qualifiers and name patterns
		Collection findQualifiers = new ArrayList();
		findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
		Collection namePatterns = new ArrayList();
		namePatterns.add(qString);

//		 Find using the name
		BulkResponse response = 
		  bqm.findOrganizations(findQualifiers,namePatterns, null, null, null, null);
		Collection orgs = response.getCollection();
		
		for (Iterator it = orgs.iterator() ; it.hasNext();){
			Organization org = (Organization) it.next();
			System.out.println("organisation = "+org.getDescription().getValue());
			Collection services = org.getServices();
			for (Iterator itS = services.iterator() ; itS.hasNext() ;) {
				Service serv = (Service) itS.next();
				Collection serviceBindings = serv.getServiceBindings();
				for (Iterator itB = serviceBindings.iterator() ; itB.hasNext() ; ) {
					ServiceBinding servB = (ServiceBinding) itB.next();
					System.out.println("URI="+servB.getAccessURI());
					Collection specificationL = servB.getSpecificationLinks();
					for (Iterator itSpec = specificationL.iterator() ; itSpec.hasNext() ;){
						SpecificationLink spec = (SpecificationLink) itSpec.next();
						System.out.println("usage="+spec.getUsageDescription());
					}
				}
				
			}
		}
		
		System.out.println("ok for finding");
		
		//Manage 
//		 Create organization name and description
		Organization org = 
		  blcm.createOrganization("The Coffee Break");
		InternationalString s =
		  blcm.createInternationalString("Purveyor of " +
		    "the finest coffees. Established 1914");
		org.setDescription(s);

//		 Create primary contact, set name
		User primaryContact = blcm.createUser();
		PersonName pName = blcm.createPersonName("Jane Doe");
		primaryContact.setPersonName(pName);

//		 Set primary contact phone number
		TelephoneNumber tNum = blcm.createTelephoneNumber();
		tNum.setNumber("(800) 555-1212");
		Collection phoneNums = new ArrayList();
		phoneNums.add(tNum);
		primaryContact.setTelephoneNumbers(phoneNums);

//		 Set primary contact email address
		EmailAddress emailAddress = 
		  blcm.createEmailAddress("jane.doe@TheCoffeeBreak.com");
		Collection emailAddresses = new ArrayList();
		emailAddresses.add(emailAddress);
		primaryContact.setEmailAddresses(emailAddresses);

//		 Set primary contact for organization
		org.setPrimaryContact(primaryContact); 


//		 Add organization and submit to registry
//		 Retrieve key if successful
		Collection orgs_submit = new ArrayList();
		orgs_submit.add(org);
		BulkResponse response_submit = blcm.saveOrganizations(orgs_submit);
		Collection exceptions = response_submit.getExceptions();
		if (exceptions == null) {
		  System.out.println("Organization saved");

		  Collection keys = response_submit.getCollection();
		  Iterator keyIter = keys.iterator();
		  if (keyIter.hasNext()) {
		    javax.xml.registry.infomodel.Key orgKey = 
		      (javax.xml.registry.infomodel.Key) keyIter.next();
		    String id = orgKey.getId();
		    System.out.println("Organization key is " + id);
		  }
		} 


		String qString2 = "The Coffee Break";
//		 Define find qualifiers and name patterns
		Collection findQualifiers2 = new ArrayList();
		findQualifiers2.add(FindQualifier.SORT_BY_NAME_DESC);
		Collection namePatterns2 = new ArrayList();
		namePatterns2.add(qString2);

//		 Find using the name
		BulkResponse response2 = 
		  bqm.findOrganizations(findQualifiers2,namePatterns2, null, null, null, null);
		Collection orgs2 = response2.getCollection();
		
		for (Iterator it = orgs2.iterator() ; it.hasNext();){
			Organization org2 = (Organization) it.next();
			System.out.println("organisation = "+org2.getDescription().getValue());
		}
		
		System.out.println("ok for finding 2");

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