Coverage Report - org.apache.ws.scout.util.ScoutJaxrUddiHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ScoutJaxrUddiHelper
82%
411/504
56%
154/274
0
 
 1  
 /**
 2  
  *
 3  
  * Copyright 2004 The Apache Software Foundation
 4  
  *
 5  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 6  
  *  you may not use this file except in compliance with the License.
 7  
  *  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  */
 17  
 package org.apache.ws.scout.util;
 18  
 
 19  
 import java.util.Arrays;
 20  
 import java.util.Collection;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 import javax.xml.registry.JAXRException;
 26  
 import javax.xml.registry.infomodel.Association;
 27  
 import javax.xml.registry.infomodel.Classification;
 28  
 import javax.xml.registry.infomodel.ClassificationScheme;
 29  
 import javax.xml.registry.infomodel.Concept;
 30  
 import javax.xml.registry.infomodel.EmailAddress;
 31  
 import javax.xml.registry.infomodel.ExternalIdentifier;
 32  
 import javax.xml.registry.infomodel.ExternalLink;
 33  
 import javax.xml.registry.infomodel.InternationalString;
 34  
 import javax.xml.registry.infomodel.Key;
 35  
 import javax.xml.registry.infomodel.LocalizedString;
 36  
 import javax.xml.registry.infomodel.Organization;
 37  
 import javax.xml.registry.infomodel.PostalAddress;
 38  
 import javax.xml.registry.infomodel.RegistryObject;
 39  
 import javax.xml.registry.infomodel.Service;
 40  
 import javax.xml.registry.infomodel.ServiceBinding;
 41  
 import javax.xml.registry.infomodel.Slot;
 42  
 import javax.xml.registry.infomodel.SpecificationLink;
 43  
 import javax.xml.registry.infomodel.TelephoneNumber;
 44  
 import javax.xml.registry.infomodel.User;
 45  
 
 46  
 import org.apache.commons.logging.Log;
 47  
 import org.apache.commons.logging.LogFactory;
 48  
 import org.apache.ws.scout.model.uddi.v2.AccessPoint;
 49  
 import org.apache.ws.scout.model.uddi.v2.Address;
 50  
 import org.apache.ws.scout.model.uddi.v2.AddressLine;
 51  
 import org.apache.ws.scout.model.uddi.v2.BindingTemplate;
 52  
 import org.apache.ws.scout.model.uddi.v2.BindingTemplates;
 53  
 import org.apache.ws.scout.model.uddi.v2.BusinessEntity;
 54  
 import org.apache.ws.scout.model.uddi.v2.BusinessService;
 55  
 import org.apache.ws.scout.model.uddi.v2.BusinessServices;
 56  
 import org.apache.ws.scout.model.uddi.v2.CategoryBag;
 57  
 import org.apache.ws.scout.model.uddi.v2.Contact;
 58  
 import org.apache.ws.scout.model.uddi.v2.Contacts;
 59  
 import org.apache.ws.scout.model.uddi.v2.Description;
 60  
 import org.apache.ws.scout.model.uddi.v2.DiscoveryURL;
 61  
 import org.apache.ws.scout.model.uddi.v2.DiscoveryURLs;
 62  
 import org.apache.ws.scout.model.uddi.v2.Email;
 63  
 import org.apache.ws.scout.model.uddi.v2.HostingRedirector;
 64  
 import org.apache.ws.scout.model.uddi.v2.IdentifierBag;
 65  
 import org.apache.ws.scout.model.uddi.v2.InstanceDetails;
 66  
 import org.apache.ws.scout.model.uddi.v2.KeyedReference;
 67  
 import org.apache.ws.scout.model.uddi.v2.Name;
 68  
 import org.apache.ws.scout.model.uddi.v2.ObjectFactory;
 69  
 import org.apache.ws.scout.model.uddi.v2.OverviewDoc;
 70  
 import org.apache.ws.scout.model.uddi.v2.Phone;
 71  
 import org.apache.ws.scout.model.uddi.v2.PublisherAssertion;
 72  
 import org.apache.ws.scout.model.uddi.v2.TModel;
 73  
 import org.apache.ws.scout.model.uddi.v2.TModelBag;
 74  
 import org.apache.ws.scout.model.uddi.v2.TModelInstanceDetails;
 75  
 import org.apache.ws.scout.model.uddi.v2.TModelInstanceInfo;
 76  
 import org.apache.ws.scout.model.uddi.v2.URLType;
 77  
 import org.apache.ws.scout.registry.infomodel.InternationalStringImpl;
 78  
 
 79  
 /**
 80  
  * Helper class that does Jaxr->UDDI Mapping
 81  
  *
 82  
  * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
 83  
  * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
 84  
  * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
 85  
  * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
 86  
  */
 87  0
 public class ScoutJaxrUddiHelper 
 88  
 {
 89  
     private static final String UDDI_ORG_TYPES = "uuid:C1ACF26D-9672-4404-9D70-39B756E62AB4";
 90  6
         private static Log log = LogFactory.getLog(ScoutJaxrUddiHelper.class);
 91  6
         private static ObjectFactory objectFactory = new ObjectFactory();
 92  
         
 93  
     /**
 94  
      * Get UDDI Address given JAXR Postal Address
 95  
      */
 96  
         public static Address getAddress(PostalAddress postalAddress) throws JAXRException {
 97  66
                 Address address = objectFactory.createAddress();
 98  
 
 99  66
                 AddressLine[] addarr = new AddressLine[6];
 100  
 
 101  66
         String stnum = postalAddress.getStreetNumber();
 102  66
         String st = postalAddress.getStreet();
 103  66
         String city = postalAddress.getCity();
 104  66
         String country = postalAddress.getCountry();
 105  66
         String code = postalAddress.getPostalCode();
 106  66
         String state = postalAddress.getStateOrProvince();
 107  
 
 108  66
                 AddressLine stnumAL = objectFactory.createAddressLine();
 109  66
         stnumAL.setKeyName("STREET_NUMBER");
 110  66
                 if (stnum != null) {
 111  66
         stnumAL.setKeyValue(stnum);
 112  
                 }
 113  
 
 114  66
                 AddressLine stAL = objectFactory.createAddressLine();
 115  66
         stAL.setKeyName("STREET");
 116  66
                 if (st != null) {
 117  66
         stAL.setKeyValue(st);
 118  
                 }
 119  
 
 120  66
                 AddressLine cityAL = objectFactory.createAddressLine();
 121  66
         cityAL.setKeyName("CITY");
 122  66
                 if (city != null) {
 123  66
         cityAL.setKeyValue(city);
 124  
                 }
 125  
 
 126  66
                 AddressLine countryAL = objectFactory.createAddressLine();
 127  66
         countryAL.setKeyName("COUNTRY");
 128  66
                 if (country != null) {
 129  66
         countryAL.setKeyValue(country);
 130  
                 }
 131  
 
 132  66
                 AddressLine codeAL = objectFactory.createAddressLine();
 133  66
         codeAL.setKeyName("POSTALCODE");
 134  66
                 if (code != null) {
 135  66
         codeAL.setKeyValue(code);
 136  
                 }
 137  
 
 138  66
                 AddressLine stateAL = objectFactory.createAddressLine();
 139  66
         stateAL.setKeyName("STATE");
 140  66
                 if (state != null) {
 141  66
         stateAL.setKeyValue(state);
 142  
                 }
 143  
 
 144  
                 // Add the AddressLine to vector
 145  66
                 addarr[0] = stnumAL;
 146  66
                 addarr[1] = stAL;
 147  66
                 addarr[2] = cityAL;
 148  66
                 addarr[3] = countryAL;
 149  66
                 addarr[4] = codeAL;
 150  66
                 addarr[5] = stateAL;
 151  
 
 152  66
                 address.getAddressLine().addAll(Arrays.asList(addarr));
 153  
 
 154  66
         return address;
 155  
     }
 156  
 
 157  
         public static BindingTemplate getBindingTemplateFromJAXRSB(
 158  
                         ServiceBinding serviceBinding) throws JAXRException {
 159  36
                 BindingTemplate bt = objectFactory.createBindingTemplate();
 160  36
                 if (serviceBinding.getKey() != null && serviceBinding.getKey().getId() != null) {
 161  0
                         bt.setBindingKey(serviceBinding.getKey().getId());
 162  
                 } else {
 163  36
                         bt.setBindingKey("");
 164  
                 }
 165  
         
 166  
                 try {
 167  
                         // Set Access URI
 168  36
             String accessuri = serviceBinding.getAccessURI();
 169  36
                         if (accessuri != null) {
 170  30
                                 AccessPoint accessPoint = objectFactory.createAccessPoint();
 171  30
                 accessPoint.setURLType(getURLType(accessuri));
 172  30
                                 accessPoint.setValue(accessuri);
 173  30
                 bt.setAccessPoint(accessPoint);
 174  
             }
 175  36
             ServiceBinding sb = serviceBinding.getTargetBinding();
 176  36
                         if (sb != null) {
 177  0
                                 HostingRedirector red = objectFactory.createHostingRedirector();
 178  0
                 Key key = sb.getKey();
 179  0
                                 if (key != null && key.getId() != null) {
 180  0
                                         red.setBindingKey(key.getId());
 181  
                 } else {
 182  0
                     red.setBindingKey("");
 183  
                 }
 184  0
                 bt.setHostingRedirector(red);
 185  0
             } else {
 186  36
                     if (bt.getAccessPoint() == null) {
 187  6
                             bt.setAccessPoint(objectFactory.createAccessPoint());
 188  
                     }
 189  
             }
 190  
                         // TODO:Need to look further at the mapping b/w BindingTemplate and
 191  
                         // Jaxr ServiceBinding
 192  
 
 193  
                         // Get Service information
 194  36
            Service svc = serviceBinding.getService();
 195  36
                         if (svc != null && svc.getKey() != null && svc.getKey().getId() != null) {
 196  24
               bt.setServiceKey(svc.getKey().getId());
 197  
            }
 198  
                         
 199  36
                         InternationalString idesc = serviceBinding.getDescription();
 200  
             
 201  36
             addDescriptions(bt.getDescription(), idesc);
 202  
 
 203  
                         // SpecificationLink
 204  36
            Collection<SpecificationLink> slcol = serviceBinding.getSpecificationLinks();
 205  36
                         TModelInstanceDetails tid = objectFactory.createTModelInstanceDetails();
 206  36
                         if (slcol != null && !slcol.isEmpty()) {
 207  6
               Iterator<SpecificationLink> iter = slcol.iterator();
 208  12
                                 while (iter.hasNext()) {
 209  6
                                         SpecificationLink slink = (SpecificationLink) iter.next();
 210  
 
 211  6
                                         TModelInstanceInfo emptyTInfo = objectFactory.createTModelInstanceInfo();
 212  6
                                         tid.getTModelInstanceInfo().add(emptyTInfo);
 213  
 
 214  6
                     RegistryObject specificationObject = slink.getSpecificationObject();
 215  6
                                         if (specificationObject.getKey() != null && specificationObject.getKey().getId() != null) {
 216  6
                                                 emptyTInfo.setTModelKey(specificationObject.getKey().getId());
 217  6
                         if (specificationObject.getDescription()!=null) {
 218  6
                             for (Object o : specificationObject.getDescription().getLocalizedStrings()) {
 219  6
                                 LocalizedString locDesc = (LocalizedString) o;
 220  6
                                 Description description = objectFactory.createDescription();
 221  6
                                 emptyTInfo.getDescription().add(description);
 222  6
                                 description.setValue(locDesc.getValue());
 223  6
                                 description.setLang(locDesc.getLocale().getLanguage());
 224  6
                             }
 225  
                         }
 226  6
                         Collection<ExternalLink> externalLinks = slink.getExternalLinks();
 227  6
                         if (externalLinks!=null && externalLinks.size()>0) {
 228  6
                             for (ExternalLink link : externalLinks) {
 229  6
                                 InstanceDetails ids = objectFactory.createInstanceDetails();
 230  6
                                 emptyTInfo.setInstanceDetails(ids);
 231  6
                                 if (link.getDescription()!=null) {
 232  6
                                     Description description = objectFactory.createDescription();
 233  6
                                     ids.getDescription().add(description);
 234  6
                                     description.setValue(link.getDescription().getValue());
 235  
                                 }
 236  6
                                 if (link.getExternalURI()!=null) {
 237  6
                                     OverviewDoc overviewDoc = objectFactory.createOverviewDoc();
 238  6
                                     ids.setOverviewDoc(overviewDoc);
 239  6
                                     overviewDoc.setOverviewURL(link.getExternalURI());
 240  
                                 }
 241  6
                             } 
 242  
                         }
 243  
                                         }
 244  6
               }
 245  
             }
 246  36
                         bt.setTModelInstanceDetails(tid);
 247  36
                         log.debug("BindingTemplate=" + bt.toString());
 248  0
                 } catch (Exception ud) {
 249  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 250  36
         }
 251  36
         return bt;
 252  
     }
 253  
 
 254  
         public static PublisherAssertion getPubAssertionFromJAXRAssociation(
 255  
                         Association association) throws JAXRException {
 256  24
                 PublisherAssertion pa = objectFactory.createPublisherAssertion();
 257  
                 try {
 258  24
                         if (association.getSourceObject().getKey() != null && 
 259  
                                 association.getSourceObject().getKey().getId() != null) {
 260  24
             pa.setFromKey(association.getSourceObject().getKey().getId());
 261  
                         }
 262  
                         
 263  24
                         if (association.getTargetObject().getKey() != null &&
 264  
                                 association.getTargetObject().getKey().getId() != null) {
 265  24
             pa.setToKey(association.getTargetObject().getKey().getId());
 266  
                         }
 267  24
             Concept c = association.getAssociationType();
 268  24
             String v = c.getValue();
 269  24
                         KeyedReference kr = objectFactory.createKeyedReference();
 270  24
             Key key = c.getKey();
 271  24
                         if (key == null) {
 272  
                                 // TODO:Need to check this. If the concept is a predefined
 273  
                                 // enumeration, the key can be the parent classification scheme
 274  6
                 key = c.getClassificationScheme().getKey();
 275  
             }
 276  24
                         if (key != null && key.getId() != null) {
 277  24
                                 kr.setTModelKey(key.getId());
 278  
                         } 
 279  24
             kr.setKeyName("Concept");
 280  
 
 281  24
                         if (v != null) {
 282  24
                                 kr.setKeyValue(v);
 283  
                         }
 284  
 
 285  24
             pa.setKeyedReference(kr);
 286  0
                 } catch (Exception ud) {
 287  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 288  24
         }
 289  24
         return pa;
 290  
     }
 291  
 
 292  
         public static PublisherAssertion getPubAssertionFromJAXRAssociationKey(
 293  
                         String key) throws JAXRException {
 294  12
                 PublisherAssertion pa = objectFactory.createPublisherAssertion();
 295  
                 try {
 296  12
                         StringTokenizer token = new StringTokenizer(key, ":");
 297  12
                         if (token.hasMoreTokens()) {
 298  12
                pa.setFromKey(getToken(token.nextToken()));
 299  12
                pa.setToKey(getToken(token.nextToken()));
 300  12
                                 KeyedReference kr = objectFactory.createKeyedReference();
 301  
                                 // Sometimes the Key is UUID:something
 302  12
                String str = getToken(token.nextToken());
 303  12
                                 if ("UUID".equals(str))
 304  0
                                         str += ":" + getToken(token.nextToken());
 305  12
                kr.setTModelKey(str);
 306  12
                kr.setKeyName(getToken(token.nextToken()));
 307  12
                kr.setKeyValue(getToken(token.nextToken()));
 308  12
                pa.setKeyedReference(kr);
 309  
             }
 310  
 
 311  0
                 } catch (Exception ud) {
 312  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 313  12
         }
 314  12
         return pa;
 315  
     }
 316  
 
 317  
         public static BusinessService getBusinessServiceFromJAXRService(
 318  
                         Service service) throws JAXRException {
 319  48
                 BusinessService bs = objectFactory.createBusinessService();
 320  
                 try {
 321  48
                         InternationalString iname = service.getName();
 322  
                                                 
 323  48
                         addNames(bs.getName(), iname);
 324  
                  
 325  48
             InternationalString idesc = service.getDescription();
 326  
     
 327  48
            addDescriptions(bs.getDescription(), idesc);
 328  
 
 329  48
             Organization o = service.getProvidingOrganization();
 330  
 
 331  
             /*
 332  
              * there may not always be a key...
 333  
              */
 334  48
             if (o != null) {
 335  48
                 Key k = o.getKey();
 336  
 
 337  48
                                 if (k != null && k.getId() != null) {
 338  36
                     bs.setBusinessKey(k.getId());
 339  
                 } 
 340  
                     
 341  48
                         } else {
 342  
                 /*
 343  
                  * gmj - I *think* this is the right thing to do
 344  
                  */
 345  0
                                 throw new JAXRException(
 346  
                                                 "Service has no associated organization");
 347  
             }
 348  
 
 349  48
                         if (service.getKey() != null && service.getKey().getId() != null) {
 350  0
                 bs.setServiceKey(service.getKey().getId());
 351  
             } else {
 352  48
                 bs.setServiceKey("");
 353  
             }
 354  
 
 355  48
             CategoryBag catBag = getCategoryBagFromClassifications(service.getClassifications());
 356  48
             if (catBag!=null) {
 357  6
                 bs.setCategoryBag(catBag);
 358  
             }
 359  
 
 360  
             //Add the ServiceBinding information
 361  48
             BindingTemplates bt = getBindingTemplates(service.getServiceBindings());
 362  48
             if (bt != null) {
 363  12
                 bs.setBindingTemplates(bt);
 364  
             }
 365  
                        
 366  48
             log.debug("BusinessService=" + bs.toString());
 367  0
                 } catch (Exception ud) {
 368  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 369  48
         }
 370  48
         return bs;
 371  
     }
 372  
 
 373  
         public static TModel getTModelFromJAXRClassificationScheme(
 374  
                         ClassificationScheme classificationScheme) throws JAXRException {
 375  30
                 TModel tm = objectFactory.createTModel();
 376  
                 try {
 377  
             /*
 378  
              * a fresh scheme might not have a key
 379  
              */
 380  
 
 381  30
             Key k = classificationScheme.getKey();
 382  
 
 383  30
             if (k != null && k.getId() != null) {
 384  0
                 tm.setTModelKey(k.getId());
 385  
             } else {
 386  30
                 tm.setTModelKey("");
 387  
             }
 388  
 
 389  
             /*
 390  
              * There's no reason to believe these are here either
 391  
              */
 392  
 
 393  30
             Slot s = classificationScheme.getSlot("authorizedName");
 394  
 
 395  30
                         if (s != null && s.getName() != null) {
 396  0
                 tm.setAuthorizedName(s.getName());
 397  
             }
 398  
 
 399  30
             s = classificationScheme.getSlot("operator");
 400  
 
 401  30
                         if (s != null && s.getName() != null) {
 402  0
                 tm.setOperator(s.getName());
 403  
             }
 404  
 
 405  30
                         InternationalString iname = classificationScheme.getName();
 406  
                          
 407  30
             tm.setName(getFirstName(iname));
 408  
 
 409  30
                         InternationalString idesc = classificationScheme.getDescription();
 410  
                         
 411  30
                     addDescriptions(tm.getDescription(), idesc);
 412  
 
 413  30
             IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(classificationScheme.getExternalIdentifiers());
 414  30
             if (idBag!=null) {
 415  0
                 tm.setIdentifierBag(idBag);
 416  
             }
 417  30
             CategoryBag catBag = getCategoryBagFromClassifications(classificationScheme.getClassifications());
 418  30
             if (catBag!=null) {
 419  6
                 tm.setCategoryBag(catBag);
 420  
             }
 421  
                         
 422  
                         // ToDO: overviewDoc
 423  0
                 } catch (Exception ud) {
 424  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 425  30
         }
 426  30
         return tm;
 427  
     }
 428  
 
 429  
     public static TModel getTModelFromJAXRConcept(Concept concept)
 430  
                         throws JAXRException {
 431  12
             TModel tm = objectFactory.createTModel();
 432  12
                 if (concept == null)
 433  0
                         return null;
 434  
                 try {
 435  12
             Key key = concept.getKey();
 436  12
                         if (key != null && key.getId() != null)
 437  0
                                 tm.setTModelKey(key.getId());
 438  12
             Slot sl1 = concept.getSlot("authorizedName");
 439  12
                         if (sl1 != null && sl1.getName() != null)
 440  0
                                 tm.setAuthorizedName(sl1.getName());
 441  
 
 442  12
             Slot sl2 = concept.getSlot("operator");
 443  12
                         if (sl2 != null && sl2.getName() != null)
 444  0
                                 tm.setOperator(sl2.getName());
 445  
 
 446  12
                         InternationalString iname = concept.getName();
 447  
 
 448  12
             tm.setName(getFirstName(iname));
 449  
 
 450  12
             InternationalString idesc = concept.getDescription();
 451  
                         
 452  12
             addDescriptions(tm.getDescription(), idesc);
 453  
 
 454  
 //          External Links
 455  12
             Collection<ExternalLink> externalLinks = concept.getExternalLinks(); 
 456  12
             if(externalLinks != null && externalLinks.size() > 0)
 457  
             {
 458  6
                 tm.setOverviewDoc(getOverviewDocFromExternalLink((ExternalLink)externalLinks.iterator().next()));
 459  
             }  
 460  
 
 461  12
             IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(concept.getExternalIdentifiers());
 462  12
             if (idBag!=null) {
 463  0
                 tm.setIdentifierBag(idBag);
 464  
             }
 465  12
             CategoryBag catBag = getCategoryBagFromClassifications(concept.getClassifications());
 466  12
             if (catBag!=null) {
 467  6
                 tm.setCategoryBag(catBag);
 468  
             }
 469  
 
 470  0
                 } catch (Exception ud) {
 471  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 472  12
         }
 473  12
         return tm;
 474  
     }
 475  
 
 476  
     private static void addDescriptions(List<Description> descripions, InternationalString idesc) throws JAXRException {
 477  228
         if (idesc != null) {
 478  228
             for (Object o : idesc.getLocalizedStrings()) {
 479  216
                 LocalizedString locName = (LocalizedString) o;
 480  216
                 Description desc = objectFactory.createDescription();
 481  216
                 descripions.add(desc);
 482  216
                 desc.setValue(locName.getValue());
 483  216
                 desc.setLang(locName.getLocale().getLanguage());
 484  216
             }
 485  
         }
 486  228
     }
 487  
 
 488  
     private static Name getFirstName(InternationalString iname) throws JAXRException {
 489  42
         for (Object o : iname.getLocalizedStrings()) {
 490  42
             LocalizedString locName = (LocalizedString) o;
 491  42
             Name name = objectFactory.createName();
 492  42
             name.setValue(locName.getValue());
 493  42
             name.setLang(locName.getLocale().getLanguage());
 494  42
             return name;
 495  
         }
 496  0
         return null;
 497  
     }
 498  
     private static void addNames(List<Name> names, InternationalString iname) throws JAXRException {
 499  150
         for (Object o : iname.getLocalizedStrings()) {
 500  150
             LocalizedString locName = (LocalizedString) o;
 501  150
             Name name = objectFactory.createName();
 502  150
             name.setValue(locName.getValue());
 503  150
             name.setLang(locName.getLocale().getLanguage());
 504  150
             names.add(name);
 505  150
         }
 506  150
     }
 507  
 
 508  
     public static BusinessEntity getBusinessEntityFromJAXROrg(Organization organization)
 509  
                         throws JAXRException {
 510  102
                 BusinessEntity biz = objectFactory.createBusinessEntity();
 511  102
                 BusinessServices bss = objectFactory.createBusinessServices();
 512  102
                 BusinessService[] barr = new BusinessService[0];
 513  
 
 514  
                 try {
 515  
                         // It may just be an update
 516  102
             Key key = organization.getKey();
 517  102
                         if (key != null && key.getId() != null) {
 518  0
                                 biz.setBusinessKey(key.getId());
 519  
             } else {
 520  102
                 biz.setBusinessKey("");
 521  
             }
 522  
                         // Lets get the Organization attributes at the top level
 523  
                         
 524  102
                         InternationalString iname = organization.getName();
 525  
                         
 526  102
                         if (iname != null) {
 527  102
                 addNames(biz.getName(), iname);
 528  
                         }
 529  
                         
 530  102
                         InternationalString idesc = organization.getDescription();
 531  
                         
 532  102
             addDescriptions(biz.getDescription(), idesc);
 533  
 
 534  102
                         if (organization.getPrimaryContact() != null && 
 535  
                                 organization.getPrimaryContact().getPersonName()!= null &&
 536  
                                 organization.getPrimaryContact().getPersonName().getFullName() != null) {
 537  
 
 538  60
                                 biz.setAuthorizedName(organization.getPrimaryContact().getPersonName()
 539  
                                                 .getFullName());
 540  
                         }
 541  
 
 542  102
             Collection<Service> s = organization.getServices();
 543  102
             log.debug("?Org has services=" + s.isEmpty());
 544  
 
 545  102
                         barr = new BusinessService[s.size()];
 546  
 
 547  102
             Iterator<Service> iter = s.iterator();
 548  102
                         int barrPos = 0;
 549  114
                         while (iter.hasNext()) {
 550  12
                                 BusinessService bs = ScoutJaxrUddiHelper
 551  
                                                 .getBusinessServiceFromJAXRService((Service) iter
 552  
                                                                 .next());
 553  12
                                 barr[barrPos] = bs;
 554  12
                                 barrPos++;
 555  12
             }
 556  
 
 557  
             /*
 558  
              * map users : JAXR has concept of 'primary contact', which is a
 559  
              * special designation for one of the users, and D6.1 seems to say
 560  
              * that the first UDDI user is the primary contact
 561  
              */
 562  
 
 563  102
                         Contacts cts = objectFactory.createContacts();
 564  102
                         Contact[] carr = new Contact[0];
 565  
 
 566  102
             User primaryContact = organization.getPrimaryContact();
 567  102
             Collection<User> users = organization.getUsers();
 568  
 
 569  
             // Expand array to necessary size only (xmlbeans does not like
 570  
             // null items in cases like this)
 571  
 
 572  102
             int carrSize = 0;
 573  
 
 574  102
             if (primaryContact != null) {
 575  60
                 carrSize += 1;
 576  
             }
 577  
 
 578  
             // TODO: Clean this up and make it more efficient
 579  102
             Iterator<User> it = users.iterator();
 580  168
             while (it.hasNext()) {
 581  66
                 User u = (User) it.next();
 582  66
                 if (u != primaryContact) {
 583  6
                     carrSize++;
 584  
                 }
 585  66
             }
 586  
 
 587  102
             carr = new Contact[carrSize];
 588  
 
 589  
             /*
 590  
              * first do primary, and then filter that out in the loop
 591  
              */
 592  102
             if (primaryContact != null) {
 593  60
                 Contact ct = getContactFromJAXRUser(primaryContact);
 594  60
                 carr[0] = ct;
 595  
             }
 596  
 
 597  102
             it = users.iterator();
 598  102
             int carrPos = 1;
 599  168
             while (it.hasNext()) {
 600  66
                 User u = (User) it.next();
 601  
 
 602  66
                 if (u != primaryContact) {
 603  6
                     Contact ct = getContactFromJAXRUser(u);
 604  6
                     carr[carrPos] = ct;
 605  6
                     carrPos++;
 606  
                 }
 607  66
             }
 608  
 
 609  102
                         bss.getBusinessService().addAll(Arrays.asList(barr));
 610  102
             if (carr.length>0) {
 611  60
                 cts.getContact().addAll(Arrays.asList(carr));
 612  60
                 biz.setContacts(cts);
 613  
             }
 614  102
             biz.setBusinessServices(bss);
 615  
 
 616  
             // External Links
 617  102
             Iterator<ExternalLink> exiter = organization.getExternalLinks().iterator();
 618  102
             DiscoveryURLs emptyDUs = null;
 619  102
             boolean first = true;
 620  102
             while (exiter.hasNext()) {
 621  0
                 ExternalLink link = (ExternalLink) exiter.next();
 622  
                 /** Note: jUDDI adds its own discoverURL as the businessEntity* */
 623  0
                 if (first) {
 624  0
                     emptyDUs = objectFactory.createDiscoveryURLs();
 625  0
                     biz.setDiscoveryURLs(emptyDUs);
 626  0
                     first = false;
 627  
                 }
 628  0
                 DiscoveryURL emptyDU = objectFactory.createDiscoveryURL();
 629  0
                 emptyDUs.getDiscoveryURL().add(emptyDU);
 630  0
                 emptyDU.setUseType("businessEntityExt");
 631  
                                 
 632  0
                 if (link.getExternalURI() != null) {
 633  0
                     emptyDU.setValue(link.getExternalURI());
 634  
                 }
 635  0
             }
 636  
                         
 637  102
           IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(organization.getExternalIdentifiers());
 638  102
           if (idBag!=null) {
 639  0
               biz.setIdentifierBag(idBag);
 640  
           }
 641  102
           CategoryBag catBag = getCategoryBagFromClassifications(organization.getClassifications());
 642  102
           if (catBag!=null) {
 643  12
               biz.setCategoryBag(catBag);
 644  
           }
 645  
                         
 646  0
                 } catch (Exception ud) {
 647  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 648  102
         }
 649  102
         return biz;
 650  
     }
 651  
 
 652  
     /**
 653  
      *
 654  
      * Convert JAXR User Object to UDDI  Contact
 655  
      */
 656  
     public static Contact getContactFromJAXRUser(User user)
 657  
                         throws JAXRException {
 658  66
                 Contact ct = objectFactory.createContact();
 659  66
         if (user == null) {
 660  0
             return null;
 661  
         }
 662  
 
 663  66
                 Address[] addarr = new Address[0];
 664  66
                 Phone[] phonearr = new Phone[0];
 665  66
                 Email[] emailarr = new Email[0];
 666  
                 try {
 667  
                         
 668  66
                         if (user.getPersonName() != null && user.getPersonName().getFullName() != null) {
 669  66
                                 ct.setPersonName(user.getPersonName().getFullName());
 670  
                         }
 671  
                         
 672  66
                         if (user.getType() != null) {
 673  66
             ct.setUseType(user.getType());
 674  
                         }
 675  
                         // Postal Address
 676  66
             Collection<PostalAddress> postc = user.getPostalAddresses();
 677  
 
 678  66
                         addarr = new Address[postc.size()];
 679  
 
 680  66
             Iterator<PostalAddress> iterator = postc.iterator();
 681  66
                         int addarrPos = 0;
 682  132
                         while (iterator.hasNext()) {
 683  66
                 PostalAddress post = (PostalAddress) iterator.next();
 684  66
                                 addarr[addarrPos] = ScoutJaxrUddiHelper.getAddress(post);
 685  66
                                 addarrPos++;
 686  66
             }
 687  
                         // Phone Numbers
 688  66
             Collection ph = user.getTelephoneNumbers(null);
 689  
 
 690  66
                         phonearr = new Phone[ph.size()];
 691  
 
 692  66
             Iterator it = ph.iterator();
 693  66
                         int phonearrPos = 0;
 694  132
                         while (it.hasNext()) {
 695  66
                 TelephoneNumber t = (TelephoneNumber) it.next();
 696  66
                                 Phone phone = objectFactory.createPhone();
 697  66
                 String str = t.getNumber();
 698  66
                 log.debug("Telephone=" + str);
 699  
                                 
 700  
                                 // FIXME: If phone number is null, should the phone 
 701  
                                 // not be set at all, or set to empty string?
 702  66
                                 if (str != null) {
 703  66
                                         phone.setValue(str);
 704  
                                 } else {
 705  0
                                         phone.setValue("");
 706  
                                 }
 707  
 
 708  66
                                 phonearr[phonearrPos] = phone;
 709  66
                                 phonearrPos++;
 710  66
             }
 711  
 
 712  
                         // Email Addresses
 713  66
             Collection ec = user.getEmailAddresses();
 714  
 
 715  66
                         emailarr = new Email[ec.size()];
 716  
 
 717  66
             Iterator iter = ec.iterator();
 718  66
                         int emailarrPos = 0;
 719  132
                         while (iter.hasNext()) {
 720  66
                 EmailAddress ea = (EmailAddress) iter.next();
 721  66
                                 Email email = objectFactory.createEmail();
 722  
                                 
 723  66
                                 if (ea.getAddress() != null) {
 724  66
                                         email.setValue(ea.getAddress());
 725  
                                 }
 726  
                                 // email.setText( ea.getAddress() );
 727  
                                 
 728  66
                                 if (ea.getType() != null) {
 729  0
                 email.setUseType(ea.getType());
 730  
             }
 731  
 
 732  66
                                 emailarr[emailarrPos] = email;
 733  66
                                 emailarrPos++;
 734  66
                         }
 735  66
                         ct.getAddress().addAll(Arrays.asList(addarr));
 736  66
                         ct.getPhone().addAll(Arrays.asList(phonearr));
 737  66
                         ct.getEmail().addAll(Arrays.asList(emailarr));
 738  0
                 } catch (Exception ud) {
 739  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 740  66
         }
 741  66
         return ct;
 742  
     }
 743  
 
 744  
         private static String getToken(String tokenstr) {
 745  
                 // Token can have the value NULL which need to be converted into null
 746  60
                 if (tokenstr.equals("NULL"))
 747  0
                         tokenstr = "";
 748  60
       return tokenstr;
 749  
    }
 750  
 
 751  
         private static URLType getURLType(String accessuri) {
 752  30
        String acc = accessuri.toLowerCase();
 753  30
                 URLType uri = URLType.OTHER;
 754  30
                 if (acc.startsWith("http:"))
 755  18
                         uri = URLType.HTTP;
 756  12
                 else if (acc.startsWith("https:"))
 757  0
                         uri = URLType.HTTPS;
 758  12
                 else if (acc.startsWith("ftp:"))
 759  0
                         uri = URLType.FTP;
 760  12
                 else if (acc.startsWith("phone:"))
 761  0
                         uri = URLType.PHONE;// TODO:Handle this better
 762  
 
 763  30
        return uri;
 764  
    }
 765  
     
 766  
         /**
 767  
      * According to JAXR Javadoc, there are two types of classification, internal and external and they use the Classification, Concept,     
 768  
      * and ClassificationScheme objects.  It seems the only difference between internal and external (as related to UDDI) is that the
 769  
      * name/value pair of the categorization is held in the Concept for internal classifications and the Classification for external (bypassing
 770  
      * the Concept entirely).
 771  
      * 
 772  
      * The translation to UDDI is simple.  Relevant objects have a category bag which contains a bunch of KeyedReferences (name/value pairs).  
 773  
      * These KeyedReferences optionally refer to a tModel that identifies the type of category (translates to the ClassificationScheme key).  If
 774  
      * this is set and the tModel doesn't exist in the UDDI registry, then an invalid key error will occur when trying to save the object.
 775  
      * 
 776  
      * @param classifications classifications to turn into categories
 777  
      * @throws JAXRException
 778  
      */
 779  
         public static CategoryBag getCategoryBagFromClassifications(Collection classifications) throws JAXRException {
 780  
             try {
 781  300
                         if (classifications == null || classifications.size()==0)
 782  246
                                 return null;
 783  
                     
 784  
                     // Classifications
 785  54
                         CategoryBag cbag = objectFactory.createCategoryBag();
 786  54
                         Iterator classiter = classifications.iterator();
 787  108
                         while (classiter.hasNext()) {
 788  54
                                 Classification classification = (Classification) classiter.next();
 789  54
                                 if (classification != null ) {
 790  54
                                         KeyedReference keyr = objectFactory.createKeyedReference();
 791  54
                                         cbag.getKeyedReference().add(keyr);
 792  
         
 793  54
                                         InternationalStringImpl iname = null;
 794  54
                                         String value = null;
 795  54
                                         ClassificationScheme scheme = classification.getClassificationScheme();
 796  54
                     if (scheme==null || (classification.isExternal() && classification.getConcept()==null)) {
 797  
                         /*
 798  
                         * JAXR 1.0 Specification: Section D6.4.4
 799  
                         * Specification related tModels mapped from Concept may be automatically
 800  
                         * categorized by the well-known uddi-org:types taxonomy in UDDI (with
 801  
                         * tModelKey uuid:C1ACF26D-9672-4404-9D70-39B756E62AB4) as follows:
 802  
                         * The keyed reference is assigned a taxonomy value of specification.
 803  
                         */
 804  0
                         keyr.setTModelKey(UDDI_ORG_TYPES);
 805  0
                         keyr.setKeyValue("specification"); 
 806  
                     } else {
 807  54
                                             if (classification.isExternal()) {
 808  54
                             iname = (InternationalStringImpl) ((RegistryObject) classification).getName();
 809  54
                             value = classification.getValue();
 810  
                                             } else {
 811  0
                                                     Concept concept = classification.getConcept();
 812  0
                                                     if (concept != null) {
 813  0
                                                             iname = (InternationalStringImpl) ((RegistryObject) concept).getName();
 814  0
                                                             value = concept.getValue();
 815  0
                                                             scheme = concept.getClassificationScheme();
 816  
                                                     }
 817  
                                             }
 818  
             
 819  54
                                             String name = iname.getValue();
 820  54
                                             if (name != null)
 821  54
                                                     keyr.setKeyName(name);
 822  
             
 823  54
                                             if (value != null)
 824  54
                                                     keyr.setKeyValue(value);
 825  
                                             
 826  54
                                             if (scheme != null) {
 827  54
                                                     Key key = scheme.getKey();
 828  54
                                                     if (key != null && key.getId() != null)
 829  54
                                                             keyr.setTModelKey(key.getId());
 830  
                                             }
 831  
                                     }
 832  
                 }
 833  54
                         }
 834  54
                         return cbag;
 835  0
             } catch (Exception ud) {
 836  0
                         throw new JAXRException("Apache JAXR Impl:", ud);
 837  
                 }
 838  
     }
 839  
 
 840  
         public static TModelBag getTModelBagFromSpecifications(Collection specifications) throws JAXRException {
 841  
             try {
 842  12
                         if (specifications == null || specifications.size()==0)
 843  6
                                 return null;
 844  
                     
 845  
                     // Classifications
 846  6
                         TModelBag tbag = objectFactory.createTModelBag();
 847  6
                         Iterator speciter = specifications.iterator();
 848  12
                         while (speciter.hasNext()) {
 849  6
                                 RegistryObject registryobject = (RegistryObject) speciter.next();
 850  6
                                 if (registryobject instanceof SpecificationLink) {
 851  6
                                         SpecificationLink specificationlink = (SpecificationLink) registryobject;
 852  6
                                         if (specificationlink.getSpecificationObject() != null) {
 853  6
                                                 RegistryObject ro = specificationlink.getSpecificationObject();
 854  6
                                                 if (ro.getKey() != null) {
 855  6
                                                         Key key = ro.getKey();
 856  6
                                                         tbag.getTModelKey().add(key.toString());
 857  
                                                 }
 858  
                                         }
 859  6
                                 } else {
 860  0
                                         log.info("ebXML case - the RegistryObject is an ExtrinsicObject, Not implemented");
 861  
                                 }
 862  6
                         }
 863  6
                         return tbag;
 864  0
             } catch (Exception ud) {
 865  0
                         throw new JAXRException("Apache JAXR Impl:", ud);
 866  
                 }
 867  
     }
 868  
 
 869  
         
 870  
         /**
 871  
      * Adds the objects identifiers from JAXR's external identifier collection
 872  
      * 
 873  
      * @param identifiers external identifiers to turn into identifiers
 874  
      * @throws JAXRException
 875  
      */
 876  
         public static IdentifierBag getIdentifierBagFromExternalIdentifiers(Collection identifiers) throws JAXRException {
 877  
             try {
 878  210
                         if (identifiers == null || identifiers.size()==0)
 879  210
                                 return null;
 880  
                     
 881  
                     // Identifiers
 882  0
                         IdentifierBag ibag = objectFactory.createIdentifierBag();
 883  0
                         Iterator iditer = identifiers.iterator();
 884  0
                         while (iditer.hasNext()) {
 885  0
                                 ExternalIdentifier extid = (ExternalIdentifier) iditer.next();
 886  0
                                 if (extid != null ) {
 887  0
                                         KeyedReference keyr = objectFactory.createKeyedReference();
 888  0
                                         ibag.getKeyedReference().add(keyr);
 889  
         
 890  0
                                         InternationalStringImpl iname = (InternationalStringImpl) ((RegistryObject) extid).getName();
 891  0
                                         String value = extid.getValue();
 892  0
                                         ClassificationScheme scheme = extid.getIdentificationScheme();
 893  
         
 894  0
                                         String name = iname.getValue();
 895  0
                                         if (name != null)
 896  0
                                                 keyr.setKeyName(name);
 897  
         
 898  0
                                         if (value != null)
 899  0
                                                 keyr.setKeyValue(value);
 900  
                                         
 901  0
                                         if (scheme != null) {
 902  0
                                                 Key key = scheme.getKey();
 903  0
                                                 if (key != null && key.getId() != null)
 904  0
                                                         keyr.setTModelKey(key.getId());
 905  
                                         }
 906  
                                 }
 907  0
                         }
 908  0
                         return ibag;
 909  0
             } catch (Exception ud) {
 910  0
                         throw new JAXRException("Apache JAXR Impl:", ud);
 911  
                 }
 912  
     }
 913  
     
 914  
     private static OverviewDoc getOverviewDocFromExternalLink(ExternalLink link)
 915  
        throws JAXRException
 916  
        {
 917  6
            OverviewDoc od = objectFactory.createOverviewDoc();
 918  6
            String url = link.getExternalURI();
 919  6
            if(url != null)
 920  6
                od.setOverviewURL(url);
 921  6
            InternationalString extDesc = link.getDescription();
 922  6
            if(extDesc != null) {
 923  6
                Description description = objectFactory.createDescription();
 924  6
                od.getDescription().add(description);
 925  6
                description.setValue(extDesc.getValue());
 926  
            }
 927  6
            return od;
 928  
        }
 929  
 
 930  
     private static BindingTemplates getBindingTemplates(Collection serviceBindings)
 931  
         throws JAXRException {
 932  48
         BindingTemplates bt = null;
 933  48
         if(serviceBindings != null && serviceBindings.size() > 0) {
 934  12
             bt = objectFactory.createBindingTemplates();
 935  12
             Iterator iter = serviceBindings.iterator();
 936  12
             int currLoc = 0;
 937  12
             BindingTemplate[] bindingTemplateArray = new BindingTemplate[serviceBindings.size()];
 938  24
             while(iter.hasNext()) {
 939  12
                 ServiceBinding sb = (ServiceBinding)iter.next();
 940  12
                 bindingTemplateArray[currLoc] = getBindingTemplateFromJAXRSB(sb);
 941  12
                 currLoc++;
 942  12
             }
 943  12
             if (bindingTemplateArray != null) {
 944  12
                 bt.getBindingTemplate().addAll(Arrays.asList(bindingTemplateArray));
 945  
             }
 946  
         }
 947  48
         return bt; 
 948  
     }
 949  
 }