FileDocCategorySizeDatePackage
PostalScheme.javaAPI DocExample10563Sat Dec 07 22:21:58 GMT 2002ora.jwsnut.chapter7.jaxr

PostalScheme

public class PostalScheme extends Object

Fields Summary
private static javax.xml.registry.BusinessQueryManager
bqm
private static javax.xml.registry.BusinessLifeCycleManager
blcm
Constructors Summary
Methods Summary
private static voiddoDisplayPostalAddresses(java.util.Collection orgs)

        Iterator oIter = orgs.iterator();
        while (oIter.hasNext()) {
            Organization org = (Organization)oIter.next();
            User user = org.getPrimaryContact();
            Collection coll = user.getPostalAddresses();
            if (coll.size() > 0) {
                Iterator iter = coll.iterator();
                PostalAddress address = (PostalAddress)iter.next();
                System.out.println("\n\nPOSTAL ADDRESS for " + org.getName().getValue());
                System.out.println("\tStreet number: " + address.getStreetNumber());
                System.out.println("\tStreet: " + address.getStreet());
                System.out.println("\tCity: " + address.getCity());
                System.out.println("\tState: " + address.getStateOrProvince());
                System.out.println("\tPostcode: " + address.getPostalCode());
                System.out.println("\tCountry: " + address.getCountry());
                System.out.println("\nSLOTS:");
                coll = address.getSlots();
                iter = coll.iterator();
                while (iter.hasNext()) {
                    Slot slot = (Slot)iter.next();
                    System.out.print("\t" + slot.getName() + ": ");
                    Iterator sIter = slot.getValues().iterator();
                    while (sIter.hasNext()) {
                        System.out.print(sIter.next() + "; ");
                    }
                    System.out.println();
                }
            }
        }
    
private static voiddoInstallNewOrganization()

        
        Organization dummy = blcm.createOrganization(
                        blcm.createInternationalString("Another Organization"));
        dummy.setDescription(
            blcm.createInternationalString("An organization with an IBM-style address"));
        TelephoneNumber number = blcm.createTelephoneNumber();
        number.setNumber("123-456-7890");
        ArrayList list = new ArrayList();
        list.add(number);
        dummy.setTelephoneNumbers(list);
        list.clear();
        
        // Create the primary contact and include the address
        User user = blcm.createUser();
        PersonName personName = blcm.createPersonName("Another User");
        user.setDescription(blcm.createInternationalString("Another Organization primary contact"));        
        user.setPersonName(personName);
        PostalAddress address = blcm.createPostalAddress(
                            "1005", "Gravenstein Highway North", "Sebastopol",
                            "CA", "USA", "95472", "Headquarters");        
        // Add a slot containing the sort code 
        address.addSlot(blcm.createSlot(Slot.SORT_CODE_SLOT, "1234", null));        
        list.add(address);
        user.setPostalAddresses(list);
        list.clear();
                
        EmailAddress email = blcm.createEmailAddress("info@anotherorg.com");
        list.add(email);
        user.setEmailAddresses(list);
        list.clear();

        list.add(number);
        user.setTelephoneNumbers(list);
        list.clear();        
        
        // Install the primary contact
        dummy.setPrimaryContact(user);
        
        // Save the Organization
        list.add(dummy);
        BulkResponse res = blcm.saveOrganizations(list);
        Collection coll = res.getExceptions();
        if (coll != null) {
            Iterator iter = coll.iterator();
            while (iter.hasNext()) {
                System.out.println(iter.next());
            }
        }
    
public static voidmain(java.lang.String[] args)

        
        HashSet credentials = new HashSet();
        
        // Validate arguments
        if (args.length !=5) {
            usage();
        }
        String userName = args[0];
        String password = args[1];
        String queryURL = args[2];
        String lifecycleURL = args[3];
        String operation = args[4];        
        PasswordAuthentication auth = 
                    new PasswordAuthentication(userName, password.toCharArray());
        credentials.add(auth);  
        
        Properties props = new Properties();
        props.put("javax.xml.registry.queryManagerURL", queryURL);
        props.put("javax.xml.registry.lifeCycleManagerURL", lifecycleURL);
        props.put("javax.xml.registry.security.authenticationMethod", "UDDI_GET_AUTHTOKEN");

        // Work out what we need to do and set up the appropriate
        // PostalScheme.
        boolean install = operation.equals("install");
        if (install || operation.equals("ibm")) {
            // Install a PostalAddress using the IBM postal scheme,
            // or display using the IBM postal scheme
            props.setProperty("javax.xml.registry.postalAddressScheme", 
                                "uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B");
            props.setProperty("javax.xml.registry.semanticEquivalences",
              "urn:uuid:PostalAddressAttributes/StreetNumber," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/StreetAddressNumber|" +
              "urn:uuid:PostalAddressAttributes/Street," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/StreetAddress|" +
              "urn:uuid:PostalAddressAttributes/City," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/City|" +
              "urn:uuid:PostalAddressAttributes/State," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/State|" +
              "urn:uuid:PostalAddressAttributes/PostalCode," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/ZipCode|" +
              "urn:uuid:PostalAddressAttributes/Country," +
              "urn:uuid:6EAF4B50-4196-11D6-9E2B-000629DC0A2B/Country");           
        } else if (operation.equals("default")) {
            // Get postal address with the JAXR default PostalAddress scheme
            props.setProperty("javax.xml.registry.postalAddressScheme", 
                                "PostalAddressAttributes");
            props.setProperty("javax.xml.registry.semanticEquivalences",
              "urn:uuid:PostalAddressAttributes/StreetNumber," +
              "urn:uuid:PostalAddressAttributes/StreetNumber|" +
              "urn:uuid:PostalAddressAttributes/Street," +
              "urn:uuid:PostalAddressAttributes/Street|" +
              "urn:uuid:PostalAddressAttributes/City," +
              "urn:uuid:PostalAddressAttributes/City|" +
              "urn:uuid:PostalAddressAttributes/State," +
              "urn:uuid:PostalAddressAttributes/State|" +
              "urn:uuid:PostalAddressAttributes/PostalCode," +
              "urn:uuid:PostalAddressAttributes/PostalCode|" +
              "urn:uuid:PostalAddressAttributes/Country," +
              "urn:uuid:PostalAddressAttributes/Country");    
        } else if (operation.equals("none")) {
            // Get postal address with no PostalAddress scheme
            // Nothing to do here...
        } else {
            usage();
        }      
        
        try {
            // Get the ConnectionFactory            
            ConnectionFactory cf = ConnectionFactory.newInstance();
            cf.setProperties(props);
            
            // Get and initialize the connection
            Connection conn = cf.createConnection();
            conn.setCredentials(credentials);
            conn.setSynchronous(true);
            
            // Get the RegistryService and the managers
            RegistryService registry = conn.getRegistryService();
            bqm = registry.getBusinessQueryManager();            
            blcm = registry.getBusinessLifeCycleManager();

            // Find the organization that we will modify
            ArrayList names = new ArrayList();
            names.add("Another Organization");
            BulkResponse res = bqm.findOrganizations(null, names, null, null, null, null);
            Collection coll = res.getCollection();
            int count = coll.size();
            if (install && count > 0) {
                System.out.println("Install already completed");
                System.exit(0);
            } else if (!install && coll.size() == 0) {
                System.out.println("Please use the install target first");
                System.exit(1);
            }
            
            // Do what we were asked to do..
            if (install) {
                doInstallNewOrganization();
            } else {
                names.add("%Reilly%");
                res = bqm.findOrganizations(null, names, null, null, null, null);
                coll = res.getCollection();
                doDisplayPostalAddresses(coll);
            }
            
            // Close connection
            conn.close();           
            
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace(System.out);
            System.exit(1);
        }    
        
        System.exit(0);
    
public static voidusage()

        System.out.println("Usage:\tPostalScheme username password queryURL updateURL [install|none|default|ibm]");
        System.exit(1);