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.JAXRException;
import javax.xml.registry.JAXRResponse;
import javax.xml.registry.RegistryService;
import javax.xml.registry.infomodel.Classification;
import javax.xml.registry.infomodel.ClassificationScheme;
import javax.xml.registry.infomodel.EmailAddress;
import javax.xml.registry.infomodel.ExternalIdentifier;
import javax.xml.registry.infomodel.InternationalString;
import javax.xml.registry.infomodel.Key;
import javax.xml.registry.infomodel.Organization;
import javax.xml.registry.infomodel.PersonName;
import javax.xml.registry.infomodel.PostalAddress;
import javax.xml.registry.infomodel.Service;
import javax.xml.registry.infomodel.TelephoneNumber;
import javax.xml.registry.infomodel.User;
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 java.net.URL;


public class SaveJAXROrganization {

    private static Properties prop = new Properties();
    //private static final String queryurl = "http://planetarium.hki.uni-koeln.de:8080/juddi/inquiry";
    private static final String queryurl = "http://localhost:8080/juddi/inquiry";
    //private static final String publishurl = "http://planetarium.hki.uni-koeln.de:8080/juddi/publish";
    private static final String publishurl = "http://localhost:8080/juddi/publish";
    private static BusinessLifeCycleManager blm = null;
    
    private static String bKey = null;

    public static void main(String[] args) {
    	System.out.println("===============================================");
        try {
            PasswordAuthentication passwdAuth = new PasswordAuthentication("jboss", "juddi".toCharArray());
        	//PasswordAuthentication passwdAuth = new PasswordAuthentication("jbotana", "pass".toCharArray());
            Set creds = new HashSet();
            creds.add(passwdAuth);
            setProperties();
            Connection conn = getConnection();
            conn.setCredentials(creds);
            RegistryService rs = conn.getRegistryService();
            blm = rs.getBusinessLifeCycleManager();
            
            Collection orgs = new ArrayList();
            Organization org = blm.createOrganization(getIString("Apache JBOSS"));
            org.setDescription(getIString("Apache Software Foundation"));
            
            
            Service service = blm.createService(getIString("Apache JAXR Service"));
            service.setDescription(getIString("Services of UDDI Registry"));
            
            User user = blm.createUser();
            org.setPrimaryContact(user);
            PersonName personName = blm.createPersonName("Steve Viens");
            //PersonName personName = blm.createPersonName("Steve", "", "Viens");
            TelephoneNumber telephoneNumber = blm.createTelephoneNumber();
            telephoneNumber.setNumber("410-666-7777");
            telephoneNumber.setType(null);
            PostalAddress address
                    = blm.createPostalAddress("1901",
                            "Munsey Drive", "Forest Hill",
                            "MD", "USA", "21050-2747", "headquarters");
            Collection postalAddresses = new ArrayList();
            postalAddresses.add(address);
            Collection emailAddresses = new ArrayList();
            EmailAddress emailAddress = blm.createEmailAddress("sviens@apache.org");
            emailAddresses.add(emailAddress);
            Collection numbers = new ArrayList();
            numbers.add(telephoneNumber);
            user.setPersonName(personName);
            user.setPostalAddresses(postalAddresses);
            user.setEmailAddresses(emailAddresses);
            user.setTelephoneNumbers(numbers);
            
            //user.setUrl(new URL("http://www.apache.org"));

            //Concepts for NAICS and computer           
            ClassificationScheme cScheme = getClassificationScheme("ntis-gov:naics", "");
            Key cKey = blm.createKey("uuid:C0B9FE13-324F-413D-5A5B-2004DB8E5CC2");
            cScheme.setKey(cKey);
            Classification classification = blm.createClassification(cScheme,
                    "Computer Systems Design and Related Services",
                    "5415");
            org.addClassification(classification);
            ClassificationScheme cScheme1 = getClassificationScheme("D-U-N-S", "");
            Key cKey1 = blm.createKey("uuid:3367C81E-FF1F-4D5A-B202-3EB13AD02423");
            cScheme1.setKey(cKey1);
            ExternalIdentifier ei =
                    blm.createExternalIdentifier(cScheme1, "D-U-N-S number",
                            "08-146-6849");
            org.addExternalIdentifier(ei);
            
            
            
            org.addService(service);
            orgs.add(org);
            
            
            
            
            
            BulkResponse br = blm.saveOrganizations(orgs);
            if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
                System.out.println("Organization Saved");
                Collection coll = br.getCollection();
                Iterator iter = coll.iterator();
                while (iter.hasNext()) {
                    Key key = (Key) iter.next();
                    bKey = key.getId();
                    System.out.println("Saved Key: " + key.getId());
                }//end while
            } else {
                System.out.println("JAXRExceptions " +
                        "occurred during save:");
                Collection exceptions = br.getExceptions();
                Iterator iter = exceptions.iterator();
                while (iter.hasNext()) {
                    Exception e = (Exception) iter.next();
                    System.out.println(e.toString());
                }
            }
            
            
            BusinessQueryManager bqm = rs.getBusinessQueryManager();
            ArrayList names = new ArrayList();
			names.add("USA");
			Collection fQualifiers = new ArrayList();
			fQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
			br = bqm.findOrganizations(fQualifiers, names, null,
					null, null, null);
			
			if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
				System.out.println("===============================================");
                Collection coll = br.getCollection();
                Iterator iter = coll.iterator();
                while (iter.hasNext()) {
                	
                	Organization org1 = (Organization)iter.next();
                	System.out.println("Key: " + org1.getKey().getId());
					System.out.println("Name: " + org1.getName().getValue());
					System.out.println("Desc: " + org1.getDescription().getValue());
					
					User primaryContact = org1.getPrimaryContact();
					System.out.println("Contact Name: " + primaryContact.getPersonName().getFullName());
					
					Collection emails = primaryContact.getEmailAddresses();
					//System.out.println("Emails size: " + emails.size());
					System.out.println("Contact Email: " + ((EmailAddress)primaryContact.getEmailAddresses().iterator().next()).getAddress());
					
					System.out.println("Contact URL: " + primaryContact.getUrl().toExternalForm());
					
					Collection phones = primaryContact.getTelephoneNumbers(null);
					//System.out.println("Phones size: " + phones.size());
					System.out.println("Contact Phone: " + ((TelephoneNumber)primaryContact.getTelephoneNumbers(null).iterator().next()).getNumber());
					
					Collection addresses = primaryContact.getPostalAddresses();
					System.out.println("Addresses size: " + addresses.size());
					PostalAddress pAddress = (PostalAddress)primaryContact.getPostalAddresses().iterator().next();
					
					System.out.println("Contact Street: " + pAddress.getStreet());
					System.out.println("Contact Street Number: " + pAddress.getStreetNumber());
					System.out.println("Contact Postal Code: " + pAddress.getPostalCode());
					System.out.println("Contact City: " + pAddress.getCity());
					System.out.println("Contact Country: " + pAddress.getCountry());
					
					
					
					
					
					
                }//end while
            } else {
                System.out.println("JAXRExceptions " +
                        "occurred during save:");
                Collection exceptions = br.getExceptions();
                Iterator iter = exceptions.iterator();
                while (iter.hasNext()) {
                    Exception e = (Exception) iter.next();
                    System.out.println(e.toString());
                }
            }
			
			
            
            
            
            
        } catch (JAXRException e) {
            e.printStackTrace();
        } catch (Exception es) {
            es.printStackTrace();
        } catch (Throwable t) {
            t.printStackTrace();
            System.out.println("Message from throwable=" + t.getMessage());
        }
    }



    private static void setProperties() {
        prop.setProperty("javax.xml.registry.queryManagerURL", queryurl);
        prop.setProperty("javax.xml.registry.lifeCycleManagerURL", publishurl);
        //prop.setProperty("javax.xml.registry.factoryClass", "org.apache.juddi.jaxr.registry.ConnectionFactoryImpl");
        prop.setProperty("javax.xml.registry.factoryClass", "org.apache.ws.scout.registry.ConnectionFactory");
    }



    private static Connection getConnection() throws JAXRException {
        ConnectionFactory factory = ConnectionFactory.newInstance();
        factory.setProperties(prop);
        Connection conn = factory.createConnection();
        if (conn == null) System.out.println("No Connection");
        return conn;
    }

    private static InternationalString getIString(String str)
            throws JAXRException {
        return blm.createInternationalString(str);
    }

    private static ClassificationScheme getClassificationScheme(String str1, String str2)
            throws JAXRException {
        ClassificationScheme cs = blm.createClassificationScheme(getIString(str1),getIString(str2));
        return cs;
    }
}