001: /**
002: *
003: * Copyright 2004 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.ws.scout.util;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Locale;
022:
023: import javax.xml.registry.JAXRException;
024: import javax.xml.registry.LifeCycleManager;
025: import javax.xml.registry.infomodel.Association;
026: import javax.xml.registry.infomodel.Classification;
027: import javax.xml.registry.infomodel.ClassificationScheme;
028: import javax.xml.registry.infomodel.Concept;
029: import javax.xml.registry.infomodel.EmailAddress;
030: import javax.xml.registry.infomodel.ExternalIdentifier;
031: import javax.xml.registry.infomodel.ExternalLink;
032: import javax.xml.registry.infomodel.InternationalString;
033: import javax.xml.registry.infomodel.Organization;
034: import javax.xml.registry.infomodel.PostalAddress;
035: import javax.xml.registry.infomodel.RegistryObject;
036: import javax.xml.registry.infomodel.Service;
037: import javax.xml.registry.infomodel.ServiceBinding;
038: import javax.xml.registry.infomodel.TelephoneNumber;
039: import javax.xml.registry.infomodel.User;
040:
041: import org.apache.ws.scout.registry.infomodel.AssociationImpl;
042: import org.apache.ws.scout.registry.infomodel.ClassificationImpl;
043: import org.apache.ws.scout.registry.infomodel.ClassificationSchemeImpl;
044: import org.apache.ws.scout.registry.infomodel.ConceptImpl;
045: import org.apache.ws.scout.registry.infomodel.EmailAddressImpl;
046: import org.apache.ws.scout.registry.infomodel.ExternalIdentifierImpl;
047: import org.apache.ws.scout.registry.infomodel.ExternalLinkImpl;
048: import org.apache.ws.scout.registry.infomodel.InternationalStringImpl;
049: import org.apache.ws.scout.registry.infomodel.KeyImpl;
050: import org.apache.ws.scout.registry.infomodel.OrganizationImpl;
051: import org.apache.ws.scout.registry.infomodel.PersonNameImpl;
052: import org.apache.ws.scout.registry.infomodel.PostalAddressImpl;
053: import org.apache.ws.scout.registry.infomodel.ServiceBindingImpl;
054: import org.apache.ws.scout.registry.infomodel.ServiceImpl;
055: import org.apache.ws.scout.registry.infomodel.SpecificationLinkImpl;
056: import org.apache.ws.scout.registry.infomodel.TelephoneNumberImpl;
057: import org.apache.ws.scout.registry.infomodel.UserImpl;
058: import org.apache.ws.scout.uddi.AccessPoint;
059: import org.apache.ws.scout.uddi.Address;
060: import org.apache.ws.scout.uddi.AddressLine;
061: import org.apache.ws.scout.uddi.BindingTemplate;
062: import org.apache.ws.scout.uddi.BindingTemplates;
063: import org.apache.ws.scout.uddi.BusinessDetail;
064: import org.apache.ws.scout.uddi.BusinessEntity;
065: import org.apache.ws.scout.uddi.BusinessService;
066: import org.apache.ws.scout.uddi.BusinessServices;
067: import org.apache.ws.scout.uddi.CategoryBag;
068: import org.apache.ws.scout.uddi.Contact;
069: import org.apache.ws.scout.uddi.Contacts;
070: import org.apache.ws.scout.uddi.Description;
071: import org.apache.ws.scout.uddi.DiscoveryURL;
072: import org.apache.ws.scout.uddi.DiscoveryURLs;
073: import org.apache.ws.scout.uddi.Email;
074: import org.apache.ws.scout.uddi.HostingRedirector;
075: import org.apache.ws.scout.uddi.IdentifierBag;
076: import org.apache.ws.scout.uddi.InstanceDetails;
077: import org.apache.ws.scout.uddi.KeyedReference;
078: import org.apache.ws.scout.uddi.Name;
079: import org.apache.ws.scout.uddi.OverviewDoc;
080: import org.apache.ws.scout.uddi.Phone;
081: import org.apache.ws.scout.uddi.ServiceInfo;
082: import org.apache.ws.scout.uddi.TModel;
083: import org.apache.ws.scout.uddi.TModelDetail;
084: import org.apache.ws.scout.uddi.TModelInfo;
085: import org.apache.ws.scout.uddi.TModelInstanceDetails;
086: import org.apache.ws.scout.uddi.TModelInstanceInfo;
087:
088: /**
089: * Helper class that does UDDI->Jaxr Mapping
090: *
091: * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
092: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
093: */
094: public class ScoutUddiJaxrHelper {
095: public static Association getAssociation(Collection orgs,
096: LifeCycleManager lcm) throws JAXRException {
097: Association asso = new AssociationImpl(lcm);
098: Object[] arr = orgs.toArray();
099: asso.setSourceObject((RegistryObject) arr[0]);
100: asso.setTargetObject((RegistryObject) arr[1]);
101: return asso;
102: }
103:
104: public static Organization getOrganization(BusinessEntity entity,
105: LifeCycleManager lcm) throws JAXRException {
106: Name[] namearray = entity.getNameArray();
107: Name n = namearray != null && namearray.length > 0 ? namearray[0]
108: : null;
109: String name = n != null ? n.getStringValue() : null;
110: Description[] descarray = entity.getDescriptionArray();
111: Description desc = descarray != null && descarray.length > 0 ? descarray[0]
112: : null;
113:
114: Organization org = new OrganizationImpl(lcm);
115: if (name != null) {
116: org.setName(getIString(n.getLang(), name, lcm));
117: }
118: if (desc != null) {
119: org.setDescription(getIString(desc.getLang(), desc
120: .getStringValue(), lcm));
121: }
122: org.setKey(lcm.createKey(entity.getBusinessKey()));
123:
124: //Set Services also
125: BusinessServices services = entity.getBusinessServices();
126: BusinessService[] sarr = services != null ? services
127: .getBusinessServiceArray() : null;
128: for (int i = 0; sarr != null && i < sarr.length; i++) {
129: BusinessService s = (BusinessService) sarr[i];
130: org.addService(getService(s, lcm));
131: }
132:
133: /*
134: * Users
135: *
136: * we need to take the first contact and designate as the
137: * 'primary contact'. Currently, the OrganizationImpl
138: * class does that automatically as a safety in case
139: * user forgets to set - lets be explicit here as to not
140: * depend on that behavior
141: */
142:
143: Contacts contacts = entity.getContacts();
144: Contact[] carr = contacts != null ? contacts.getContactArray()
145: : null;
146:
147: for (int i = 0; carr != null && i < carr.length; i++) {
148: Contact contact = (Contact) carr[i];
149: User user = new UserImpl(null);
150: String pname = contact.getPersonName();
151: user.setPersonName(new PersonNameImpl(pname));
152: if (i == 0) {
153: org.setPrimaryContact(user);
154: } else {
155: org.addUser(user);
156: }
157: }
158:
159: //External Links
160: DiscoveryURLs durls = entity.getDiscoveryURLs();
161: if (durls != null) {
162: DiscoveryURL[] darr = durls.getDiscoveryURLArray();
163: for (int j = 0; darr != null && j < darr.length; j++) {
164: DiscoveryURL durl = (DiscoveryURL) darr[j];
165: ExternalLink link = new ExternalLinkImpl(lcm);
166: link.setExternalURI(durl.getStringValue());
167: org.addExternalLink(link);
168: }
169: }
170:
171: org.addExternalIdentifiers(getExternalIdentifiers(entity
172: .getIdentifierBag(), lcm));
173: org.addClassifications(getClassifications(entity
174: .getCategoryBag(), lcm));
175:
176: return org;
177: }
178:
179: public static Organization getOrganization(
180: BusinessDetail bizdetail, LifeCycleManager lcm)
181: throws JAXRException {
182: BusinessEntity[] bz = bizdetail.getBusinessEntityArray();
183:
184: BusinessEntity entity = bz[0];
185: Name[] namearr = entity.getNameArray();
186: Name n = namearr != null && namearr.length > 0 ? namearr[0]
187: : null;
188: String name = n != null ? n.getStringValue() : null;
189: Description[] descarr = entity.getDescriptionArray();
190: Description desc = descarr != null && descarr.length > 0 ? descarr[0]
191: : null;
192:
193: Organization org = new OrganizationImpl(lcm);
194: if (name != null) {
195: org.setName(getIString(n.getLang(), name, lcm));
196: }
197: if (desc != null) {
198: org.setDescription(getIString(desc.getLang(), desc
199: .getStringValue(), lcm));
200: }
201: org.setKey(lcm.createKey(entity.getBusinessKey()));
202:
203: //Set Services also
204: BusinessServices services = entity.getBusinessServices();
205: BusinessService[] sarr = services != null ? services
206: .getBusinessServiceArray() : null;
207: for (int i = 0; sarr != null && i < sarr.length; i++) {
208: BusinessService s = (BusinessService) sarr[i];
209: org.addService(getService(s, lcm));
210:
211: }
212:
213: /*
214: * Users
215: *
216: * we need to take the first contact and designate as the
217: * 'primary contact'. Currently, the OrganizationImpl
218: * class does that automatically as a safety in case
219: * user forgets to set - lets be explicit here as to not
220: * depend on that behavior
221: */
222: Contacts contacts = entity.getContacts();
223: Contact[] carr = contacts != null ? contacts.getContactArray()
224: : null;
225: for (int i = 0; carr != null && i < carr.length; i++) {
226: Contact contact = carr[i];
227: User user = new UserImpl(null);
228: String pname = contact.getPersonName();
229: user.setType(contact.getUseType());
230: user.setPersonName(new PersonNameImpl(pname));
231:
232: Email[] emails = (Email[]) contact.getEmailArray();
233: ArrayList<EmailAddress> tempEmails = new ArrayList<EmailAddress>();
234: for (int x = 0; x < emails.length; x++) {
235: tempEmails.add(new EmailAddressImpl(emails[x]
236: .getStringValue(), null));
237: }
238: user.setEmailAddresses(tempEmails);
239:
240: Address[] addresses = (Address[]) contact.getAddressArray();
241: ArrayList<PostalAddress> tempAddresses = new ArrayList<PostalAddress>();
242: for (int x = 0; x < addresses.length; x++) {
243: AddressLine[] alines = addresses[x]
244: .getAddressLineArray();
245: PostalAddress pa = getPostalAddress(alines);
246: tempAddresses.add(pa);
247: }
248: user.setPostalAddresses(tempAddresses);
249: Phone[] phones = contact.getPhoneArray();
250: ArrayList<TelephoneNumber> tempPhones = new ArrayList<TelephoneNumber>();
251: for (int x = 0; x < phones.length; x++) {
252: TelephoneNumberImpl tni = new TelephoneNumberImpl();
253: tni.setType(phones[x].getUseType());
254: tni.setNumber(phones[x].getStringValue());
255: tempPhones.add(tni);
256: }
257: user.setTelephoneNumbers(tempPhones);
258:
259: if (i == 0) {
260: org.setPrimaryContact(user);
261: } else {
262: org.addUser(user);
263: }
264: }
265:
266: //External Links
267: DiscoveryURLs durls = entity.getDiscoveryURLs();
268: if (durls != null) {
269: DiscoveryURL[] darr = durls.getDiscoveryURLArray();
270: for (int j = 0; darr != null && j < darr.length; j++) {
271: DiscoveryURL durl = darr[j];
272: ExternalLink link = new ExternalLinkImpl(lcm);
273: link.setExternalURI(durl.getStringValue());
274: org.addExternalLink(link);
275: }
276: }
277:
278: org.addExternalIdentifiers(getExternalIdentifiers(entity
279: .getIdentifierBag(), lcm));
280: org.addClassifications(getClassifications(entity
281: .getCategoryBag(), lcm));
282:
283: return org;
284: }
285:
286: private static PostalAddress getPostalAddress(AddressLine[] al)
287: throws JAXRException {
288: PostalAddress pa = new PostalAddressImpl();
289: HashMap<String, String> hm = new HashMap<String, String>();
290: for (int y = 0; y < al.length; y++) {
291: hm.put(al[y].getKeyName(), al[y].getKeyValue());
292: }
293:
294: if (hm.containsKey("STREET_NUMBER")) {
295: pa.setStreetNumber(hm.get("STREET_NUMBER"));
296: }
297:
298: if (hm.containsKey("STREET")) {
299: pa.setStreet(hm.get("STREET"));
300: }
301:
302: if (hm.containsKey("CITY")) {
303: pa.setCity(hm.get("CITY"));
304: }
305:
306: if (hm.containsKey("COUNTRY")) {
307: pa.setCountry(hm.get("COUNTRY"));
308: }
309:
310: if (hm.containsKey("POSTALCODE")) {
311: pa.setPostalCode(hm.get("POSTALCODE"));
312: }
313:
314: if (hm.containsKey("STATE")) {
315: pa.setStateOrProvince(hm.get("STATE"));
316: }
317:
318: return pa;
319: }
320:
321: private static InternationalString getIString(String lang,
322: String str, LifeCycleManager blm) throws JAXRException {
323: return blm.createInternationalString(getLocale(lang), str);
324: }
325:
326: public static InternationalString getIString(String str,
327: LifeCycleManager blm) throws JAXRException {
328: return blm.createInternationalString(str);
329: }
330:
331: public static Service getService(BusinessService bs,
332: LifeCycleManager lcm) throws JAXRException {
333: Service serve = new ServiceImpl(lcm);
334:
335: String keystr = bs.getServiceKey();
336:
337: if (keystr != null) {
338: serve.setKey(lcm.createKey(keystr));
339: }
340:
341: Name[] namearr = bs.getNameArray();
342:
343: Name n = namearr != null && namearr.length > 0 ? namearr[0]
344: : null;
345:
346: if (n != null) {
347: String name = n.getStringValue();
348: serve.setName(lcm.createInternationalString(getLocale(n
349: .getLang()), name));
350: }
351:
352: Description[] descarr = bs.getDescriptionArray();
353: Description desc = descarr != null && descarr.length > 0 ? descarr[0]
354: : null;
355: if (desc != null) {
356: serve.setDescription(lcm.createInternationalString(
357: getLocale(desc.getLang()), desc.getStringValue()));
358: }
359:
360: //Populate the ServiceBindings for this Service
361: BindingTemplates bts = bs.getBindingTemplates();
362: BindingTemplate[] btarr = bts != null ? bts
363: .getBindingTemplateArray() : null;
364: for (int i = 0; btarr != null && i < btarr.length; i++) {
365: BindingTemplate bindingTemplate = (BindingTemplate) btarr[i];
366: serve.addServiceBinding(getServiceBinding(bindingTemplate,
367: lcm));
368: }
369:
370: serve.addClassifications(getClassifications(
371: bs.getCategoryBag(), lcm));
372:
373: return serve;
374: }
375:
376: public static Service getService(ServiceInfo si,
377: LifeCycleManager lcm) throws JAXRException {
378: Service service = new ServiceImpl(lcm);
379:
380: String keystr = si.getServiceKey();
381:
382: if (keystr != null) {
383: service.setKey(lcm.createKey(keystr));
384: }
385:
386: Name[] namearr = si.getNameArray();
387: Name n = namearr != null && namearr.length > 0 ? namearr[0]
388: : null;
389:
390: if (n != null) {
391: String name = n.getStringValue();
392: service.setName(lcm.createInternationalString(getLocale(n
393: .getLang()), name));
394: }
395:
396: return service;
397: }
398:
399: public static ServiceBinding getServiceBinding(BindingTemplate bs,
400: LifeCycleManager lcm) throws JAXRException {
401: ServiceBinding serviceBinding = new ServiceBindingImpl(lcm);
402:
403: String keystr = bs.getServiceKey();
404: if (keystr != null) {
405: Service svc = new ServiceImpl(lcm);
406: svc.setKey(lcm.createKey(keystr));
407: ((ServiceBindingImpl) serviceBinding).setService(svc);
408: }
409: String bindingKey = bs.getBindingKey();
410: if (bindingKey != null)
411: serviceBinding.setKey(new KeyImpl(bindingKey));
412:
413: //Access URI
414: AccessPoint access = bs.getAccessPoint();
415: if (access != null)
416: serviceBinding.setAccessURI(access.getStringValue());
417:
418: //Description
419: Description[] da = bs.getDescriptionArray();
420: if (da != null && da.length > 0) {
421: Description des = da[0];
422: serviceBinding.setDescription(new InternationalStringImpl(
423: des.getStringValue()));
424: }
425: /**Section D.10 of JAXR 1.0 Specification */
426:
427: TModelInstanceDetails details = bs.getTModelInstanceDetails();
428: TModelInstanceInfo[] tmodelInstanceInfoArray = details
429: .getTModelInstanceInfoArray();
430: for (int i = 0; tmodelInstanceInfoArray != null
431: && i < tmodelInstanceInfoArray.length; i++) {
432: TModelInstanceInfo info = (TModelInstanceInfo) tmodelInstanceInfoArray[i];
433: InstanceDetails idetails = info.getInstanceDetails();
434: Collection<ExternalLink> elinks = getExternalLinks(idetails
435: .getOverviewDoc(), lcm);
436: SpecificationLinkImpl slink = new SpecificationLinkImpl(lcm);
437: slink.addExternalIdentifiers(elinks);
438: serviceBinding.addSpecificationLink(slink);
439:
440: ConceptImpl c = new ConceptImpl(lcm);
441: c.setExternalLinks(elinks);
442: c.setKey(lcm.createKey(info.getTModelKey()));
443: c.setName(lcm.createInternationalString(idetails
444: .getInstanceParms()));
445: c.setValue(idetails.getInstanceParms());
446:
447: slink.setSpecificationObject(c);
448: }
449:
450: HostingRedirector hr = bs.getHostingRedirector();
451: if (hr != null) {
452: ServiceBinding sb = lcm.createServiceBinding();
453: sb.setKey(new KeyImpl(hr.getBindingKey()));
454: serviceBinding.setTargetBinding(sb);
455: }
456:
457: return serviceBinding;
458: }
459:
460: public static Concept getConcept(TModelDetail tm,
461: LifeCycleManager lcm) throws JAXRException {
462: Concept concept = new ConceptImpl(lcm);
463: TModel[] tc = tm.getTModelArray();
464: TModel tmodel = tc != null && tc.length > 0 ? tc[0] : null;
465:
466: if (tmodel != null) {
467: concept.setKey(lcm.createKey(tmodel.getTModelKey()));
468: concept.setName(lcm.createInternationalString(
469: getLocale(tmodel.getName().getLang()), tmodel
470: .getName().getStringValue()));
471:
472: Description desc = getDescription(tmodel);
473: if (desc != null) {
474: concept.setDescription(lcm.createInternationalString(
475: getLocale(desc.getLang()), desc
476: .getStringValue()));
477: }
478:
479: concept.addExternalIdentifiers(getExternalIdentifiers(
480: tmodel.getIdentifierBag(), lcm));
481: concept.addClassifications(getClassifications(tmodel
482: .getCategoryBag(), lcm));
483: }
484: return concept;
485: }
486:
487: public static Concept getConcept(TModel tmodel, LifeCycleManager lcm)
488: throws JAXRException {
489: Concept concept = new ConceptImpl(lcm);
490: concept.setKey(lcm.createKey(tmodel.getTModelKey()));
491: concept.setName(lcm.createInternationalString(getLocale(tmodel
492: .getName().getLang()), tmodel.getName()
493: .getStringValue()));
494:
495: Description desc = getDescription(tmodel);
496: if (desc != null) {
497: concept.setDescription(lcm.createInternationalString(
498: getLocale(desc.getLang()), desc.getStringValue()));
499: }
500:
501: concept.addExternalIdentifiers(getExternalIdentifiers(tmodel
502: .getIdentifierBag(), lcm));
503: concept.addClassifications(getClassifications(tmodel
504: .getCategoryBag(), lcm));
505:
506: return concept;
507: }
508:
509: public static Concept getConcept(TModelInfo tm, LifeCycleManager lcm)
510: throws JAXRException {
511: Concept concept = new ConceptImpl(lcm);
512: concept.setKey(lcm.createKey(tm.getTModelKey()));
513: concept.setName(lcm.createInternationalString(getLocale(tm
514: .getName().getLang()), tm.getName().getStringValue()));
515:
516: return concept;
517: }
518:
519: private static Description getDescription(TModel tmodel) {
520: Description[] descarr = tmodel.getDescriptionArray();
521: Description desc = descarr != null && descarr.length > 0 ? descarr[0]
522: : null;
523: return desc;
524: }
525:
526: /**
527: * Classifications - going to assume all are external since UDDI does not use "Concepts".
528: *
529: * @param cbag
530: * @param destinationObj
531: * @param lcm
532: * @throws JAXRException
533: */
534: public static Collection getClassifications(CategoryBag cbag,
535: LifeCycleManager lcm) throws JAXRException {
536: Collection<Classification> classifications = null;
537: if (cbag != null) {
538: classifications = new ArrayList<Classification>();
539: KeyedReference[] keyrarr = cbag.getKeyedReferenceArray();
540: for (int i = 0; keyrarr != null && i < keyrarr.length; i++) {
541: KeyedReference keyr = (KeyedReference) keyrarr[i];
542: Classification classification = new ClassificationImpl(
543: lcm);
544: classification.setValue(keyr.getKeyValue());
545: classification.setName(new InternationalStringImpl(keyr
546: .getKeyName()));
547:
548: String tmodelKey = keyr.getTModelKey();
549: if (tmodelKey != null) {
550: ClassificationScheme scheme = new ClassificationSchemeImpl(
551: lcm);
552: scheme.setKey(new KeyImpl(tmodelKey));
553: classification.setClassificationScheme(scheme);
554: }
555: classifications.add(classification);
556: }
557: }
558: return classifications;
559: }
560:
561: public static Collection<ExternalLink> getExternalLinks(
562: OverviewDoc odoc, LifeCycleManager lcm)
563: throws JAXRException {
564: ArrayList<ExternalLink> alist = new ArrayList<ExternalLink>(1);
565: if (odoc != null) {
566: Description[] descVect = odoc.getDescriptionArray();
567: String desc = "";
568: if (descVect != null && descVect.length > 0) {
569: desc = ((Description) descVect[0]).getStringValue();
570: }
571: alist.add(lcm.createExternalLink(odoc.getOverviewURL(),
572: desc));
573: }
574:
575: return alist;
576: }
577:
578: /**
579: * External Identifiers
580: *
581: * @param ibag
582: * @param destinationObj
583: * @param lcm
584: * @throws JAXRException
585: */
586: public static Collection<ExternalIdentifier> getExternalIdentifiers(
587: IdentifierBag ibag, LifeCycleManager lcm)
588: throws JAXRException {
589: Collection<ExternalIdentifier> extidentifiers = null;
590: if (ibag != null) {
591: extidentifiers = new ArrayList<ExternalIdentifier>();
592: KeyedReference[] keyrarr = ibag.getKeyedReferenceArray();
593: for (int i = 0; keyrarr != null && i < keyrarr.length; i++) {
594: KeyedReference keyr = (KeyedReference) keyrarr[i];
595: ExternalIdentifier extId = new ExternalIdentifierImpl(
596: lcm);
597: extId.setValue(keyr.getKeyValue());
598: extId.setName(new InternationalStringImpl(keyr
599: .getKeyName()));
600:
601: String tmodelKey = keyr.getTModelKey();
602: if (tmodelKey != null) {
603: ClassificationScheme scheme = new ClassificationSchemeImpl(
604: lcm);
605: scheme.setKey(new KeyImpl(tmodelKey));
606: extId.setIdentificationScheme(scheme);
607: }
608: extidentifiers.add(extId);
609: }
610: }
611: return extidentifiers;
612: }
613:
614: private static Locale getLocale(String lang) {
615: if (lang == null || lang.trim().length() == 0) {
616: return Locale.getDefault();
617: } else if (lang.equalsIgnoreCase(Locale.getDefault()
618: .getLanguage())) {
619: return Locale.getDefault();
620: } else {
621: return new Locale(lang);
622: }
623: }
624: }
|