Coverage Report - org.apache.ws.scout.util.ScoutJaxrUddiHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ScoutJaxrUddiHelper
0%
0/504
0%
0/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  0
         private static Log log = LogFactory.getLog(ScoutJaxrUddiHelper.class);
 91  0
         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  0
                 Address address = objectFactory.createAddress();
 98  
 
 99  0
                 AddressLine[] addarr = new AddressLine[6];
 100  
 
 101  0
         String stnum = postalAddress.getStreetNumber();
 102  0
         String st = postalAddress.getStreet();
 103  0
         String city = postalAddress.getCity();
 104  0
         String country = postalAddress.getCountry();
 105  0
         String code = postalAddress.getPostalCode();
 106  0
         String state = postalAddress.getStateOrProvince();
 107  
 
 108  0
                 AddressLine stnumAL = objectFactory.createAddressLine();
 109  0
         stnumAL.setKeyName("STREET_NUMBER");
 110  0
                 if (stnum != null) {
 111  0
         stnumAL.setKeyValue(stnum);
 112  
                 }
 113  
 
 114  0
                 AddressLine stAL = objectFactory.createAddressLine();
 115  0
         stAL.setKeyName("STREET");
 116  0
                 if (st != null) {
 117  0
         stAL.setKeyValue(st);
 118  
                 }
 119  
 
 120  0
                 AddressLine cityAL = objectFactory.createAddressLine();
 121  0
         cityAL.setKeyName("CITY");
 122  0
                 if (city != null) {
 123  0
         cityAL.setKeyValue(city);
 124  
                 }
 125  
 
 126  0
                 AddressLine countryAL = objectFactory.createAddressLine();
 127  0
         countryAL.setKeyName("COUNTRY");
 128  0
                 if (country != null) {
 129  0
         countryAL.setKeyValue(country);
 130  
                 }
 131  
 
 132  0
                 AddressLine codeAL = objectFactory.createAddressLine();
 133  0
         codeAL.setKeyName("POSTALCODE");
 134  0
                 if (code != null) {
 135  0
         codeAL.setKeyValue(code);
 136  
                 }
 137  
 
 138  0
                 AddressLine stateAL = objectFactory.createAddressLine();
 139  0
         stateAL.setKeyName("STATE");
 140  0
                 if (state != null) {
 141  0
         stateAL.setKeyValue(state);
 142  
                 }
 143  
 
 144  
                 // Add the AddressLine to vector
 145  0
                 addarr[0] = stnumAL;
 146  0
                 addarr[1] = stAL;
 147  0
                 addarr[2] = cityAL;
 148  0
                 addarr[3] = countryAL;
 149  0
                 addarr[4] = codeAL;
 150  0
                 addarr[5] = stateAL;
 151  
 
 152  0
                 address.getAddressLine().addAll(Arrays.asList(addarr));
 153  
 
 154  0
         return address;
 155  
     }
 156  
 
 157  
         public static BindingTemplate getBindingTemplateFromJAXRSB(
 158  
                         ServiceBinding serviceBinding) throws JAXRException {
 159  0
                 BindingTemplate bt = objectFactory.createBindingTemplate();
 160  0
                 if (serviceBinding.getKey() != null && serviceBinding.getKey().getId() != null) {
 161  0
                         bt.setBindingKey(serviceBinding.getKey().getId());
 162  
                 } else {
 163  0
                         bt.setBindingKey("");
 164  
                 }
 165  
         
 166  
                 try {
 167  
                         // Set Access URI
 168  0
             String accessuri = serviceBinding.getAccessURI();
 169  0
                         if (accessuri != null) {
 170  0
                                 AccessPoint accessPoint = objectFactory.createAccessPoint();
 171  0
                 accessPoint.setURLType(getURLType(accessuri));
 172  0
                                 accessPoint.setValue(accessuri);
 173  0
                 bt.setAccessPoint(accessPoint);
 174  
             }
 175  0
             ServiceBinding sb = serviceBinding.getTargetBinding();
 176  0
                         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  0
                     if (bt.getAccessPoint() == null) {
 187  0
                             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  0
            Service svc = serviceBinding.getService();
 195  0
                         if (svc != null && svc.getKey() != null && svc.getKey().getId() != null) {
 196  0
               bt.setServiceKey(svc.getKey().getId());
 197  
            }
 198  
                         
 199  0
                         InternationalString idesc = serviceBinding.getDescription();
 200  
             
 201  0
             addDescriptions(bt.getDescription(), idesc);
 202  
 
 203  
                         // SpecificationLink
 204  0
            Collection<SpecificationLink> slcol = serviceBinding.getSpecificationLinks();
 205  0
                         TModelInstanceDetails tid = objectFactory.createTModelInstanceDetails();
 206  0
                         if (slcol != null && !slcol.isEmpty()) {
 207  0
               Iterator<SpecificationLink> iter = slcol.iterator();
 208  0
                                 while (iter.hasNext()) {
 209  0
                                         SpecificationLink slink = (SpecificationLink) iter.next();
 210  
 
 211  0
                                         TModelInstanceInfo emptyTInfo = objectFactory.createTModelInstanceInfo();
 212  0
                                         tid.getTModelInstanceInfo().add(emptyTInfo);
 213  
 
 214  0
                     RegistryObject specificationObject = slink.getSpecificationObject();
 215  0
                                         if (specificationObject.getKey() != null && specificationObject.getKey().getId() != null) {
 216  0
                                                 emptyTInfo.setTModelKey(specificationObject.getKey().getId());
 217  0
                         if (specificationObject.getDescription()!=null) {
 218  0
                             for (Object o : specificationObject.getDescription().getLocalizedStrings()) {
 219  0
                                 LocalizedString locDesc = (LocalizedString) o;
 220  0
                                 Description description = objectFactory.createDescription();
 221  0
                                 emptyTInfo.getDescription().add(description);
 222  0
                                 description.setValue(locDesc.getValue());
 223  0
                                 description.setLang(locDesc.getLocale().getLanguage());
 224  0
                             }
 225  
                         }
 226  0
                         Collection<ExternalLink> externalLinks = slink.getExternalLinks();
 227  0
                         if (externalLinks!=null && externalLinks.size()>0) {
 228  0
                             for (ExternalLink link : externalLinks) {
 229  0
                                 InstanceDetails ids = objectFactory.createInstanceDetails();
 230  0
                                 emptyTInfo.setInstanceDetails(ids);
 231  0
                                 if (link.getDescription()!=null) {
 232  0
                                     Description description = objectFactory.createDescription();
 233  0
                                     ids.getDescription().add(description);
 234  0
                                     description.setValue(link.getDescription().getValue());
 235  
                                 }
 236  0
                                 if (link.getExternalURI()!=null) {
 237  0
                                     OverviewDoc overviewDoc = objectFactory.createOverviewDoc();
 238  0
                                     ids.setOverviewDoc(overviewDoc);
 239  0
                                     overviewDoc.setOverviewURL(link.getExternalURI());
 240  
                                 }
 241  0
                             } 
 242  
                         }
 243  
                                         }
 244  0
               }
 245  
             }
 246  0
                         bt.setTModelInstanceDetails(tid);
 247  0
                         log.debug("BindingTemplate=" + bt.toString());
 248  0
                 } catch (Exception ud) {
 249  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 250  0
         }
 251  0
         return bt;
 252  
     }
 253  
 
 254  
         public static PublisherAssertion getPubAssertionFromJAXRAssociation(
 255  
                         Association association) throws JAXRException {
 256  0
                 PublisherAssertion pa = objectFactory.createPublisherAssertion();
 257  
                 try {
 258  0
                         if (association.getSourceObject().getKey() != null && 
 259  
                                 association.getSourceObject().getKey().getId() != null) {
 260  0
             pa.setFromKey(association.getSourceObject().getKey().getId());
 261  
                         }
 262  
                         
 263  0
                         if (association.getTargetObject().getKey() != null &&
 264  
                                 association.getTargetObject().getKey().getId() != null) {
 265  0
             pa.setToKey(association.getTargetObject().getKey().getId());
 266  
                         }
 267  0
             Concept c = association.getAssociationType();
 268  0
             String v = c.getValue();
 269  0
                         KeyedReference kr = objectFactory.createKeyedReference();
 270  0
             Key key = c.getKey();
 271  0
                         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  0
                 key = c.getClassificationScheme().getKey();
 275  
             }
 276  0
                         if (key != null && key.getId() != null) {
 277  0
                                 kr.setTModelKey(key.getId());
 278  
                         } 
 279  0
             kr.setKeyName("Concept");
 280  
 
 281  0
                         if (v != null) {
 282  0
                                 kr.setKeyValue(v);
 283  
                         }
 284  
 
 285  0
             pa.setKeyedReference(kr);
 286  0
                 } catch (Exception ud) {
 287  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 288  0
         }
 289  0
         return pa;
 290  
     }
 291  
 
 292  
         public static PublisherAssertion getPubAssertionFromJAXRAssociationKey(
 293  
                         String key) throws JAXRException {
 294  0
                 PublisherAssertion pa = objectFactory.createPublisherAssertion();
 295  
                 try {
 296  0
                         StringTokenizer token = new StringTokenizer(key, "|");
 297  0
                         if (token.hasMoreTokens()) {
 298  0
                pa.setFromKey(getToken(token.nextToken()));
 299  0
                pa.setToKey(getToken(token.nextToken()));
 300  0
                                 KeyedReference kr = objectFactory.createKeyedReference();
 301  
                                 // Sometimes the Key is UUID:something
 302  0
                String str = getToken(token.nextToken());
 303  0
                                 if ("UUID".equals(str))
 304  0
                                         str += ":" + getToken(token.nextToken());
 305  0
                kr.setTModelKey(str);
 306  0
                kr.setKeyName(getToken(token.nextToken()));
 307  0
                kr.setKeyValue(getToken(token.nextToken()));
 308  0
                pa.setKeyedReference(kr);
 309  
             }
 310  
 
 311  0
                 } catch (Exception ud) {
 312  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 313  0
         }
 314  0
         return pa;
 315  
     }
 316  
 
 317  
         public static BusinessService getBusinessServiceFromJAXRService(
 318  
                         Service service) throws JAXRException {
 319  0
                 BusinessService bs = objectFactory.createBusinessService();
 320  
                 try {
 321  0
                         InternationalString iname = service.getName();
 322  
                                                 
 323  0
                         addNames(bs.getName(), iname);
 324  
                  
 325  0
             InternationalString idesc = service.getDescription();
 326  
     
 327  0
            addDescriptions(bs.getDescription(), idesc);
 328  
 
 329  0
             Organization o = service.getProvidingOrganization();
 330  
 
 331  
             /*
 332  
              * there may not always be a key...
 333  
              */
 334  0
             if (o != null) {
 335  0
                 Key k = o.getKey();
 336  
 
 337  0
                                 if (k != null && k.getId() != null) {
 338  0
                     bs.setBusinessKey(k.getId());
 339  
                 } 
 340  
                     
 341  0
                         } 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  0
                         if (service.getKey() != null && service.getKey().getId() != null) {
 350  0
                 bs.setServiceKey(service.getKey().getId());
 351  
             } else {
 352  0
                 bs.setServiceKey("");
 353  
             }
 354  
 
 355  0
             CategoryBag catBag = getCategoryBagFromClassifications(service.getClassifications());
 356  0
             if (catBag!=null) {
 357  0
                 bs.setCategoryBag(catBag);
 358  
             }
 359  
 
 360  
             //Add the ServiceBinding information
 361  0
             BindingTemplates bt = getBindingTemplates(service.getServiceBindings());
 362  0
             if (bt != null) {
 363  0
                 bs.setBindingTemplates(bt);
 364  
             }
 365  
                        
 366  0
             log.debug("BusinessService=" + bs.toString());
 367  0
                 } catch (Exception ud) {
 368  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 369  0
         }
 370  0
         return bs;
 371  
     }
 372  
 
 373  
         public static TModel getTModelFromJAXRClassificationScheme(
 374  
                         ClassificationScheme classificationScheme) throws JAXRException {
 375  0
                 TModel tm = objectFactory.createTModel();
 376  
                 try {
 377  
             /*
 378  
              * a fresh scheme might not have a key
 379  
              */
 380  
 
 381  0
             Key k = classificationScheme.getKey();
 382  
 
 383  0
             if (k != null && k.getId() != null) {
 384  0
                 tm.setTModelKey(k.getId());
 385  
             } else {
 386  0
                 tm.setTModelKey("");
 387  
             }
 388  
 
 389  
             /*
 390  
              * There's no reason to believe these are here either
 391  
              */
 392  
 
 393  0
             Slot s = classificationScheme.getSlot("authorizedName");
 394  
 
 395  0
                         if (s != null && s.getName() != null) {
 396  0
                 tm.setAuthorizedName(s.getName());
 397  
             }
 398  
 
 399  0
             s = classificationScheme.getSlot("operator");
 400  
 
 401  0
                         if (s != null && s.getName() != null) {
 402  0
                 tm.setOperator(s.getName());
 403  
             }
 404  
 
 405  0
                         InternationalString iname = classificationScheme.getName();
 406  
                          
 407  0
             tm.setName(getFirstName(iname));
 408  
 
 409  0
                         InternationalString idesc = classificationScheme.getDescription();
 410  
                         
 411  0
                     addDescriptions(tm.getDescription(), idesc);
 412  
 
 413  0
             IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(classificationScheme.getExternalIdentifiers());
 414  0
             if (idBag!=null) {
 415  0
                 tm.setIdentifierBag(idBag);
 416  
             }
 417  0
             CategoryBag catBag = getCategoryBagFromClassifications(classificationScheme.getClassifications());
 418  0
             if (catBag!=null) {
 419  0
                 tm.setCategoryBag(catBag);
 420  
             }
 421  
                         
 422  
                         // ToDO: overviewDoc
 423  0
                 } catch (Exception ud) {
 424  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 425  0
         }
 426  0
         return tm;
 427  
     }
 428  
 
 429  
     public static TModel getTModelFromJAXRConcept(Concept concept)
 430  
                         throws JAXRException {
 431  0
             TModel tm = objectFactory.createTModel();
 432  0
                 if (concept == null)
 433  0
                         return null;
 434  
                 try {
 435  0
             Key key = concept.getKey();
 436  0
                         if (key != null && key.getId() != null)
 437  0
                                 tm.setTModelKey(key.getId());
 438  0
             Slot sl1 = concept.getSlot("authorizedName");
 439  0
                         if (sl1 != null && sl1.getName() != null)
 440  0
                                 tm.setAuthorizedName(sl1.getName());
 441  
 
 442  0
             Slot sl2 = concept.getSlot("operator");
 443  0
                         if (sl2 != null && sl2.getName() != null)
 444  0
                                 tm.setOperator(sl2.getName());
 445  
 
 446  0
                         InternationalString iname = concept.getName();
 447  
 
 448  0
             tm.setName(getFirstName(iname));
 449  
 
 450  0
             InternationalString idesc = concept.getDescription();
 451  
                         
 452  0
             addDescriptions(tm.getDescription(), idesc);
 453  
 
 454  
 //          External Links
 455  0
             Collection<ExternalLink> externalLinks = concept.getExternalLinks(); 
 456  0
             if(externalLinks != null && externalLinks.size() > 0)
 457  
             {
 458  0
                 tm.setOverviewDoc(getOverviewDocFromExternalLink((ExternalLink)externalLinks.iterator().next()));
 459  
             }  
 460  
 
 461  0
             IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(concept.getExternalIdentifiers());
 462  0
             if (idBag!=null) {
 463  0
                 tm.setIdentifierBag(idBag);
 464  
             }
 465  0
             CategoryBag catBag = getCategoryBagFromClassifications(concept.getClassifications());
 466  0
             if (catBag!=null) {
 467  0
                 tm.setCategoryBag(catBag);
 468  
             }
 469  
 
 470  0
                 } catch (Exception ud) {
 471  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 472  0
         }
 473  0
         return tm;
 474  
     }
 475  
 
 476  
     private static void addDescriptions(List<Description> descripions, InternationalString idesc) throws JAXRException {
 477  0
         if (idesc != null) {
 478  0
             for (Object o : idesc.getLocalizedStrings()) {
 479  0
                 LocalizedString locName = (LocalizedString) o;
 480  0
                 Description desc = objectFactory.createDescription();
 481  0
                 descripions.add(desc);
 482  0
                 desc.setValue(locName.getValue());
 483  0
                 desc.setLang(locName.getLocale().getLanguage());
 484  0
             }
 485  
         }
 486  0
     }
 487  
 
 488  
     private static Name getFirstName(InternationalString iname) throws JAXRException {
 489  0
         for (Object o : iname.getLocalizedStrings()) {
 490  0
             LocalizedString locName = (LocalizedString) o;
 491  0
             Name name = objectFactory.createName();
 492  0
             name.setValue(locName.getValue());
 493  0
             name.setLang(locName.getLocale().getLanguage());
 494  0
             return name;
 495  
         }
 496  0
         return null;
 497  
     }
 498  
     private static void addNames(List<Name> names, InternationalString iname) throws JAXRException {
 499  0
         for (Object o : iname.getLocalizedStrings()) {
 500  0
             LocalizedString locName = (LocalizedString) o;
 501  0
             Name name = objectFactory.createName();
 502  0
             name.setValue(locName.getValue());
 503  0
             name.setLang(locName.getLocale().getLanguage());
 504  0
             names.add(name);
 505  0
         }
 506  0
     }
 507  
 
 508  
     public static BusinessEntity getBusinessEntityFromJAXROrg(Organization organization)
 509  
                         throws JAXRException {
 510  0
                 BusinessEntity biz = objectFactory.createBusinessEntity();
 511  0
                 BusinessServices bss = objectFactory.createBusinessServices();
 512  0
                 BusinessService[] barr = new BusinessService[0];
 513  
 
 514  
                 try {
 515  
                         // It may just be an update
 516  0
             Key key = organization.getKey();
 517  0
                         if (key != null && key.getId() != null) {
 518  0
                                 biz.setBusinessKey(key.getId());
 519  
             } else {
 520  0
                 biz.setBusinessKey("");
 521  
             }
 522  
                         // Lets get the Organization attributes at the top level
 523  
                         
 524  0
                         InternationalString iname = organization.getName();
 525  
                         
 526  0
                         if (iname != null) {
 527  0
                 addNames(biz.getName(), iname);
 528  
                         }
 529  
                         
 530  0
                         InternationalString idesc = organization.getDescription();
 531  
                         
 532  0
             addDescriptions(biz.getDescription(), idesc);
 533  
 
 534  0
                         if (organization.getPrimaryContact() != null && 
 535  
                                 organization.getPrimaryContact().getPersonName()!= null &&
 536  
                                 organization.getPrimaryContact().getPersonName().getFullName() != null) {
 537  
 
 538  0
                                 biz.setAuthorizedName(organization.getPrimaryContact().getPersonName()
 539  
                                                 .getFullName());
 540  
                         }
 541  
 
 542  0
             Collection<Service> s = organization.getServices();
 543  0
             log.debug("?Org has services=" + s.isEmpty());
 544  
 
 545  0
                         barr = new BusinessService[s.size()];
 546  
 
 547  0
             Iterator<Service> iter = s.iterator();
 548  0
                         int barrPos = 0;
 549  0
                         while (iter.hasNext()) {
 550  0
                                 BusinessService bs = ScoutJaxrUddiHelper
 551  
                                                 .getBusinessServiceFromJAXRService((Service) iter
 552  
                                                                 .next());
 553  0
                                 barr[barrPos] = bs;
 554  0
                                 barrPos++;
 555  0
             }
 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  0
                         Contacts cts = objectFactory.createContacts();
 564  0
                         Contact[] carr = new Contact[0];
 565  
 
 566  0
             User primaryContact = organization.getPrimaryContact();
 567  0
             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  0
             int carrSize = 0;
 573  
 
 574  0
             if (primaryContact != null) {
 575  0
                 carrSize += 1;
 576  
             }
 577  
 
 578  
             // TODO: Clean this up and make it more efficient
 579  0
             Iterator<User> it = users.iterator();
 580  0
             while (it.hasNext()) {
 581  0
                 User u = (User) it.next();
 582  0
                 if (u != primaryContact) {
 583  0
                     carrSize++;
 584  
                 }
 585  0
             }
 586  
 
 587  0
             carr = new Contact[carrSize];
 588  
 
 589  
             /*
 590  
              * first do primary, and then filter that out in the loop
 591  
              */
 592  0
             if (primaryContact != null) {
 593  0
                 Contact ct = getContactFromJAXRUser(primaryContact);
 594  0
                 carr[0] = ct;
 595  
             }
 596  
 
 597  0
             it = users.iterator();
 598  0
             int carrPos = 1;
 599  0
             while (it.hasNext()) {
 600  0
                 User u = (User) it.next();
 601  
 
 602  0
                 if (u != primaryContact) {
 603  0
                     Contact ct = getContactFromJAXRUser(u);
 604  0
                     carr[carrPos] = ct;
 605  0
                     carrPos++;
 606  
                 }
 607  0
             }
 608  
 
 609  0
                         bss.getBusinessService().addAll(Arrays.asList(barr));
 610  0
             if (carr.length>0) {
 611  0
                 cts.getContact().addAll(Arrays.asList(carr));
 612  0
                 biz.setContacts(cts);
 613  
             }
 614  0
             biz.setBusinessServices(bss);
 615  
 
 616  
             // External Links
 617  0
             Iterator<ExternalLink> exiter = organization.getExternalLinks().iterator();
 618  0
             DiscoveryURLs emptyDUs = null;
 619  0
             boolean first = true;
 620  0
             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  0
           IdentifierBag idBag = getIdentifierBagFromExternalIdentifiers(organization.getExternalIdentifiers());
 638  0
           if (idBag!=null) {
 639  0
               biz.setIdentifierBag(idBag);
 640  
           }
 641  0
           CategoryBag catBag = getCategoryBagFromClassifications(organization.getClassifications());
 642  0
           if (catBag!=null) {
 643  0
               biz.setCategoryBag(catBag);
 644  
           }
 645  
                         
 646  0
                 } catch (Exception ud) {
 647  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 648  0
         }
 649  0
         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  0
                 Contact ct = objectFactory.createContact();
 659  0
         if (user == null) {
 660  0
             return null;
 661  
         }
 662  
 
 663  0
                 Address[] addarr = new Address[0];
 664  0
                 Phone[] phonearr = new Phone[0];
 665  0
                 Email[] emailarr = new Email[0];
 666  
                 try {
 667  
                         
 668  0
                         if (user.getPersonName() != null && user.getPersonName().getFullName() != null) {
 669  0
                                 ct.setPersonName(user.getPersonName().getFullName());
 670  
                         }
 671  
                         
 672  0
                         if (user.getType() != null) {
 673  0
             ct.setUseType(user.getType());
 674  
                         }
 675  
                         // Postal Address
 676  0
             Collection<PostalAddress> postc = user.getPostalAddresses();
 677  
 
 678  0
                         addarr = new Address[postc.size()];
 679  
 
 680  0
             Iterator<PostalAddress> iterator = postc.iterator();
 681  0
                         int addarrPos = 0;
 682  0
                         while (iterator.hasNext()) {
 683  0
                 PostalAddress post = (PostalAddress) iterator.next();
 684  0
                                 addarr[addarrPos] = ScoutJaxrUddiHelper.getAddress(post);
 685  0
                                 addarrPos++;
 686  0
             }
 687  
                         // Phone Numbers
 688  0
             Collection ph = user.getTelephoneNumbers(null);
 689  
 
 690  0
                         phonearr = new Phone[ph.size()];
 691  
 
 692  0
             Iterator it = ph.iterator();
 693  0
                         int phonearrPos = 0;
 694  0
                         while (it.hasNext()) {
 695  0
                 TelephoneNumber t = (TelephoneNumber) it.next();
 696  0
                                 Phone phone = objectFactory.createPhone();
 697  0
                 String str = t.getNumber();
 698  0
                 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  0
                                 if (str != null) {
 703  0
                                         phone.setValue(str);
 704  
                                 } else {
 705  0
                                         phone.setValue("");
 706  
                                 }
 707  
 
 708  0
                                 phonearr[phonearrPos] = phone;
 709  0
                                 phonearrPos++;
 710  0
             }
 711  
 
 712  
                         // Email Addresses
 713  0
             Collection ec = user.getEmailAddresses();
 714  
 
 715  0
                         emailarr = new Email[ec.size()];
 716  
 
 717  0
             Iterator iter = ec.iterator();
 718  0
                         int emailarrPos = 0;
 719  0
                         while (iter.hasNext()) {
 720  0
                 EmailAddress ea = (EmailAddress) iter.next();
 721  0
                                 Email email = objectFactory.createEmail();
 722  
                                 
 723  0
                                 if (ea.getAddress() != null) {
 724  0
                                         email.setValue(ea.getAddress());
 725  
                                 }
 726  
                                 // email.setText( ea.getAddress() );
 727  
                                 
 728  0
                                 if (ea.getType() != null) {
 729  0
                 email.setUseType(ea.getType());
 730  
             }
 731  
 
 732  0
                                 emailarr[emailarrPos] = email;
 733  0
                                 emailarrPos++;
 734  0
                         }
 735  0
                         ct.getAddress().addAll(Arrays.asList(addarr));
 736  0
                         ct.getPhone().addAll(Arrays.asList(phonearr));
 737  0
                         ct.getEmail().addAll(Arrays.asList(emailarr));
 738  0
                 } catch (Exception ud) {
 739  0
             throw new JAXRException("Apache JAXR Impl:", ud);
 740  0
         }
 741  0
         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  0
                 if (tokenstr.equals("NULL"))
 747  0
                         tokenstr = "";
 748  0
       return tokenstr;
 749  
    }
 750  
 
 751  
         private static URLType getURLType(String accessuri) {
 752  0
        String acc = accessuri.toLowerCase();
 753  0
                 URLType uri = URLType.OTHER;
 754  0
                 if (acc.startsWith("http:"))
 755  0
                         uri = URLType.HTTP;
 756  0
                 else if (acc.startsWith("https:"))
 757  0
                         uri = URLType.HTTPS;
 758  0
                 else if (acc.startsWith("ftp:"))
 759  0
                         uri = URLType.FTP;
 760  0
                 else if (acc.startsWith("phone:"))
 761  0
                         uri = URLType.PHONE;// TODO:Handle this better
 762  
 
 763  0
        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  0
                         if (classifications == null || classifications.size()==0)
 782  0
                                 return null;
 783  
                     
 784  
                     // Classifications
 785  0
                         CategoryBag cbag = objectFactory.createCategoryBag();
 786  0
                         Iterator classiter = classifications.iterator();
 787  0
                         while (classiter.hasNext()) {
 788  0
                                 Classification classification = (Classification) classiter.next();
 789  0
                                 if (classification != null ) {
 790  0
                                         KeyedReference keyr = objectFactory.createKeyedReference();
 791  0
                                         cbag.getKeyedReference().add(keyr);
 792  
         
 793  0
                                         InternationalStringImpl iname = null;
 794  0
                                         String value = null;
 795  0
                                         ClassificationScheme scheme = classification.getClassificationScheme();
 796  0
                     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  0
                                             if (classification.isExternal()) {
 808  0
                             iname = (InternationalStringImpl) ((RegistryObject) classification).getName();
 809  0
                             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  0
                                             String name = iname.getValue();
 820  0
                                             if (name != null)
 821  0
                                                     keyr.setKeyName(name);
 822  
             
 823  0
                                             if (value != null)
 824  0
                                                     keyr.setKeyValue(value);
 825  
                                             
 826  0
                                             if (scheme != null) {
 827  0
                                                     Key key = scheme.getKey();
 828  0
                                                     if (key != null && key.getId() != null)
 829  0
                                                             keyr.setTModelKey(key.getId());
 830  
                                             }
 831  
                                     }
 832  
                 }
 833  0
                         }
 834  0
                         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  0
                         if (specifications == null || specifications.size()==0)
 843  0
                                 return null;
 844  
                     
 845  
                     // Classifications
 846  0
                         TModelBag tbag = objectFactory.createTModelBag();
 847  0
                         Iterator speciter = specifications.iterator();
 848  0
                         while (speciter.hasNext()) {
 849  0
                                 RegistryObject registryobject = (RegistryObject) speciter.next();
 850  0
                                 if (registryobject instanceof SpecificationLink) {
 851  0
                                         SpecificationLink specificationlink = (SpecificationLink) registryobject;
 852  0
                                         if (specificationlink.getSpecificationObject() != null) {
 853  0
                                                 RegistryObject ro = specificationlink.getSpecificationObject();
 854  0
                                                 if (ro.getKey() != null) {
 855  0
                                                         Key key = ro.getKey();
 856  0
                                                         tbag.getTModelKey().add(key.toString());
 857  
                                                 }
 858  
                                         }
 859  0
                                 } else {
 860  0
                                         log.info("ebXML case - the RegistryObject is an ExtrinsicObject, Not implemented");
 861  
                                 }
 862  0
                         }
 863  0
                         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  0
                         if (identifiers == null || identifiers.size()==0)
 879  0
                                 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  0
            OverviewDoc od = objectFactory.createOverviewDoc();
 918  0
            String url = link.getExternalURI();
 919  0
            if(url != null)
 920  0
                od.setOverviewURL(url);
 921  0
            InternationalString extDesc = link.getDescription();
 922  0
            if(extDesc != null) {
 923  0
                Description description = objectFactory.createDescription();
 924  0
                od.getDescription().add(description);
 925  0
                description.setValue(extDesc.getValue());
 926  
            }
 927  0
            return od;
 928  
        }
 929  
 
 930  
     private static BindingTemplates getBindingTemplates(Collection serviceBindings)
 931  
         throws JAXRException {
 932  0
         BindingTemplates bt = null;
 933  0
         if(serviceBindings != null && serviceBindings.size() > 0) {
 934  0
             bt = objectFactory.createBindingTemplates();
 935  0
             Iterator iter = serviceBindings.iterator();
 936  0
             int currLoc = 0;
 937  0
             BindingTemplate[] bindingTemplateArray = new BindingTemplate[serviceBindings.size()];
 938  0
             while(iter.hasNext()) {
 939  0
                 ServiceBinding sb = (ServiceBinding)iter.next();
 940  0
                 bindingTemplateArray[currLoc] = getBindingTemplateFromJAXRSB(sb);
 941  0
                 currLoc++;
 942  0
             }
 943  0
             if (bindingTemplateArray != null) {
 944  0
                 bt.getBindingTemplate().addAll(Arrays.asList(bindingTemplateArray));
 945  
             }
 946  
         }
 947  0
         return bt; 
 948  
     }
 949  
 }