<%@ page contentType="text/html"
         import="java.util.Properties"
         import="javax.xml.registry.ConnectionFactory"
         import="javax.xml.registry.Connection"
         import="javax.xml.registry.RegistryService"
         import="javax.xml.registry.BulkResponse"
         import="javax.xml.registry.JAXRResponse"
         import="javax.xml.registry.FindQualifier"
         import="javax.xml.registry.BusinessLifeCycleManager"
         import="java.net.PasswordAuthentication"
         import="java.util.ArrayList"
         import="java.util.Collection"
         import="java.util.Properties"
         import="java.util.Iterator"
         import="java.util.Set"
         import="java.util.HashSet"
         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.TelephoneNumber"
         import="javax.xml.registry.infomodel.User"
         import="javax.xml.registry.infomodel.Classification"
         import="javax.xml.registry.infomodel.ClassificationScheme"
         import="javax.xml.registry.infomodel.Concept"
         import="javax.xml.registry.infomodel.Key"
         import="javax.xml.registry.infomodel.Service"
         import="javax.xml.registry.infomodel.ServiceBinding"
         import="javax.xml.registry.infomodel.SpecificationLink"
         import="javax.xml.registry.infomodel.ExternalLink"
         

         import="java.util.Enumeration"
         %>




<%-- Get the home Type --%>
<h2>Populating the UDDI registry</h2>
Adding service to registry...
<%
    Properties prop = new Properties();
    prop.load(this.getClass().getClassLoader().getResourceAsStream("WEB-INF/olstoreUDDI.properties"));
    
    // String Values (Replace these with values from a properties file.
    String queryURL = prop.getProperty("queryURL");
    String publishURL = prop.getProperty("publishURL");
    String username = prop.getProperty("username");
    String password = prop.getProperty("password");
    String specialsURL = prop.getProperty("specialsURL");
    String cartURL = prop.getProperty("cartURL");

    

    // Set the properties to connect to the UDDI provider.
    Properties props = new Properties();
    props.setProperty("javax.xml.registry.queryManagerURL", queryURL);
    props.setProperty("javax.xml.registry.lifeCycleManagerURL", publishURL);


    try {
    
    // Create a new connection.
    ConnectionFactory connFactory = ConnectionFactory.newInstance();

        // Set up properties and create Connection object
        connFactory.setProperties(props);
        Connection connection = connFactory.createConnection();

        // Create authentication object
        PasswordAuthentication passwdAuth = new PasswordAuthentication(username, password.toCharArray());

        // Set the credential with registry provider
        Set creds = new HashSet();
        creds.add(passwdAuth);
        connection.setCredentials(creds);	

        RegistryService rs = connection.getRegistryService();
        BusinessLifeCycleManager blcm = rs.getBusinessLifeCycleManager();

        // Create organization name and description
        Organization org = blcm.createOrganization("Red Hat OlStore");
        InternationalString s = blcm.createInternationalString("Open Source Online Store");
        org.setDescription(s);

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

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

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

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

        // Create service with service name and description
        Service service = blcm.createService("Red Hat Olstore - Specials");
        service.setDescription(blcm.createInternationalString("Special Items Service"));

        // Create serviceBinding
        ServiceBinding serviceBinding = blcm.createServiceBinding();
        serviceBinding.setDescription(blcm.createInternationalString("Information for olstore special items access"));

        //Turn validation of URI off
        serviceBinding.setValidateURI(false);
        serviceBinding.setAccessURI(specialsURL);

        // Create the SpecificationLink information
        SpecificationLink specificationLink = blcm.createSpecificationLink();

        // Set usage description
        specificationLink.setUsageDescription(blcm.createInternationalString("Get list of specials including images"));
        String usageParameter = "None";
        Collection usageParameters = new ArrayList();
        usageParameters.add(usageParameter);

        // Set usage parameters
        specificationLink.setUsageParameters(usageParameters);

        ExternalLink eLink = blcm.createExternalLink(specialsURL, "WSDL Specification");
	
        specificationLink.addExternalLink(eLink);


        // Set specificationConcept on the specificationLink
        Concept httpSpecificationConcept = (Concept) blcm.createObject(blcm.CONCEPT);
        Key httpSpecificationKey = blcm.createKey("uuid:C1ACF26D-9672-4404-9D70-39B756E62AB4");
        httpSpecificationConcept.setKey(httpSpecificationKey);
        specificationLink.setSpecificationObject(httpSpecificationConcept);

        // Add the specificationLink to the serviceBinding
        serviceBinding.addSpecificationLink(specificationLink);

        // Add the serviceBinding to the service
        service.addServiceBinding(serviceBinding);

        // Add the service to the organization
        org.addService(service);

        // Create service with service name and description
        service = blcm.createService("Red Hat Olstore - Shopping cart");
        service.setDescription(blcm.createInternationalString("Shopping cart service"));

        // Create serviceBinding
        serviceBinding = blcm.createServiceBinding();
        serviceBinding.setDescription(blcm.createInternationalString("Information for olstore shopping cart access"));

        //Turn validation of URI off
        serviceBinding.setValidateURI(false);
        serviceBinding.setAccessURI(cartURL);

        // Create the SpecificationLink information
        specificationLink = blcm.createSpecificationLink();

        // Set usage description
        specificationLink.setUsageDescription(blcm.createInternationalString("Checkout the shopping cart contents"));
        usageParameter = "None";
        usageParameters = new ArrayList();
        usageParameters.add(usageParameter);

        // Set usage parameters
        specificationLink.setUsageParameters(usageParameters);

        eLink = blcm.createExternalLink(cartURL,"WSDL Specification");
        specificationLink.addExternalLink(eLink);

        // Set specificationConcept on the specificationLink
        httpSpecificationConcept = (Concept) blcm.createObject(blcm.CONCEPT);
        httpSpecificationKey = blcm.createKey("uuid:C1ACF26D-9672-4404-9D70-39B756E62AB4");
        httpSpecificationConcept.setKey(httpSpecificationKey);
        specificationLink.setSpecificationObject(httpSpecificationConcept);

        // Add the specificationLink to the serviceBinding
        serviceBinding.addSpecificationLink(specificationLink);

        // Add the serviceBinding to the service
        service.addServiceBinding(serviceBinding);

        // Add the service to the organization
        org.addService(service);

        Collection orgs = new ArrayList();
        orgs.add(org);

        // Save organization and whole tree of related objects
        BulkResponse br = blcm.saveOrganizations(orgs);
	
        if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
                out.println("Successfully saved the organization  to the registry provider.");
        }

    } catch (Exception e) {
        out.println("Failed to add organization and service to registry provider.");
    }
	
%>


