KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > handler > BusinessEntityHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.handler;
17
18 import java.util.Vector JavaDoc;
19
20 import org.apache.juddi.datatype.Address;
21 import org.apache.juddi.datatype.AddressLine;
22 import org.apache.juddi.datatype.CategoryBag;
23 import org.apache.juddi.datatype.Description;
24 import org.apache.juddi.datatype.DiscoveryURL;
25 import org.apache.juddi.datatype.DiscoveryURLs;
26 import org.apache.juddi.datatype.Email;
27 import org.apache.juddi.datatype.IdentifierBag;
28 import org.apache.juddi.datatype.KeyedReference;
29 import org.apache.juddi.datatype.Name;
30 import org.apache.juddi.datatype.OverviewDoc;
31 import org.apache.juddi.datatype.Phone;
32 import org.apache.juddi.datatype.RegistryObject;
33 import org.apache.juddi.datatype.binding.AccessPoint;
34 import org.apache.juddi.datatype.binding.BindingTemplate;
35 import org.apache.juddi.datatype.binding.BindingTemplates;
36 import org.apache.juddi.datatype.binding.HostingRedirector;
37 import org.apache.juddi.datatype.binding.InstanceDetails;
38 import org.apache.juddi.datatype.binding.TModelInstanceDetails;
39 import org.apache.juddi.datatype.binding.TModelInstanceInfo;
40 import org.apache.juddi.datatype.business.BusinessEntity;
41 import org.apache.juddi.datatype.business.Contact;
42 import org.apache.juddi.datatype.business.Contacts;
43 import org.apache.juddi.datatype.service.BusinessService;
44 import org.apache.juddi.datatype.service.BusinessServices;
45 import org.apache.juddi.util.xml.XMLUtils;
46 import org.w3c.dom.Element JavaDoc;
47
48 /**
49  * "Knows about the creation and populating of BusinessEntity objects.
50  * Returns BusinessEntity."
51  *
52  * @author Steve Viens (sviens@apache.org)
53  * @author Anou Mana (anou_mana@users.sourceforge.net)
54  */

55 public class BusinessEntityHandler extends AbstractHandler
56 {
57   public static final String JavaDoc TAG_NAME = "businessEntity";
58
59   private HandlerMaker maker = null;
60
61   protected BusinessEntityHandler(HandlerMaker maker)
62   {
63     this.maker = maker;
64   }
65
66   public RegistryObject unmarshal(Element JavaDoc element)
67   {
68     BusinessEntity obj = new BusinessEntity();
69     Vector JavaDoc nodeList = null;
70     AbstractHandler handler = null;
71
72     // Attributes
73
obj.setBusinessKey(element.getAttribute("businessKey"));
74     obj.setOperator(element.getAttribute("operator"));
75     obj.setAuthorizedName(element.getAttribute("authorizedName"));
76
77     // Text Node Value
78
// {none}
79

80     // Child Elements
81
nodeList = XMLUtils.getChildElementsByTagName(element,NameHandler.TAG_NAME);
82     for (int i=0; i<nodeList.size(); i++)
83     {
84       handler = maker.lookup(NameHandler.TAG_NAME);
85       Name name = (Name )handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
86       if (name != null)
87         obj.addName(name);
88     }
89
90     nodeList = XMLUtils.getChildElementsByTagName(element,DescriptionHandler.TAG_NAME);
91     for (int i=0; i<nodeList.size(); i++)
92     {
93       handler = maker.lookup(DescriptionHandler.TAG_NAME);
94       Description descr = (Description)handler.unmarshal((Element JavaDoc)nodeList.elementAt(i));
95       if (descr != null)
96         obj.addDescription(descr);
97     }
98
99     nodeList = XMLUtils.getChildElementsByTagName(element,DiscoveryURLsHandler.TAG_NAME);
100     if (nodeList.size() > 0)
101     {
102       handler = maker.lookup(DiscoveryURLsHandler.TAG_NAME);
103       obj.setDiscoveryURLs((DiscoveryURLs)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
104     }
105
106     nodeList = XMLUtils.getChildElementsByTagName(element,ContactsHandler.TAG_NAME);
107     if (nodeList.size() > 0)
108     {
109       handler = maker.lookup(ContactsHandler.TAG_NAME);
110       obj.setContacts((Contacts)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
111     }
112
113     nodeList = XMLUtils.getChildElementsByTagName(element,BusinessServicesHandler.TAG_NAME);
114     if (nodeList.size() > 0)
115     {
116       handler = maker.lookup(BusinessServicesHandler.TAG_NAME);
117       obj.setBusinessServices((BusinessServices)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
118     }
119
120     nodeList = XMLUtils.getChildElementsByTagName(element,IdentifierBagHandler.TAG_NAME);
121     if (nodeList.size() > 0)
122     {
123       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
124       obj.setIdentifierBag((IdentifierBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
125     }
126
127     nodeList = XMLUtils.getChildElementsByTagName(element,CategoryBagHandler.TAG_NAME);
128     if (nodeList.size() > 0)
129     {
130       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
131       obj.setCategoryBag((CategoryBag)handler.unmarshal((Element JavaDoc)nodeList.elementAt(0)));
132     }
133
134     return obj;
135   }
136
137   public void marshal(RegistryObject object,Element JavaDoc parent)
138   {
139     BusinessEntity business = (BusinessEntity)object;
140     Element JavaDoc element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
141     AbstractHandler handler = null;
142
143     String JavaDoc businessKey = business.getBusinessKey();
144     if (businessKey != null)
145       element.setAttribute("businessKey",businessKey);
146     else
147       element.setAttribute("businessKey","");
148     
149     String JavaDoc operator = business.getOperator();
150     if (operator != null)
151       element.setAttribute("operator",operator);
152
153     String JavaDoc authName = business.getAuthorizedName();
154     if (authName != null)
155       element.setAttribute("authorizedName",authName);
156
157     DiscoveryURLs discURLs = business.getDiscoveryURLs();
158     if (discURLs != null)
159     {
160       handler = maker.lookup(DiscoveryURLsHandler.TAG_NAME);
161       handler.marshal(discURLs,element);
162     }
163
164     Vector JavaDoc nameVector = business.getNameVector();
165     if ((nameVector!=null) && (nameVector.size() > 0))
166     {
167       handler = maker.lookup(NameHandler.TAG_NAME);
168       for (int i=0; i < nameVector.size(); i++)
169         handler.marshal((Name)nameVector.elementAt(i),element);
170     }
171
172     Vector JavaDoc descrVector = business.getDescriptionVector();
173     if ((descrVector!=null) && (descrVector.size() > 0))
174     {
175       handler = maker.lookup(DescriptionHandler.TAG_NAME);
176       for (int i=0; i < descrVector.size(); i++)
177         handler.marshal((Description)descrVector.elementAt(i),element);
178     }
179
180     Contacts contacts = business.getContacts();
181     if (contacts != null)
182     {
183       handler = maker.lookup(ContactsHandler.TAG_NAME);
184       handler.marshal(contacts,element);
185     }
186
187     BusinessServices services = business.getBusinessServices();
188     if (services != null)
189     {
190       handler = maker.lookup(BusinessServicesHandler.TAG_NAME);
191       handler.marshal(services,element);
192     }
193
194     IdentifierBag identifierBag = business.getIdentifierBag();
195     if ((identifierBag != null) && (identifierBag.getKeyedReferenceVector() != null) && (!identifierBag.getKeyedReferenceVector().isEmpty()))
196     {
197       handler = maker.lookup(IdentifierBagHandler.TAG_NAME);
198       handler.marshal(identifierBag,element);
199     }
200
201     CategoryBag categoryBag = business.getCategoryBag();
202     if ((categoryBag != null) && (categoryBag.getKeyedReferenceVector() != null) && (!categoryBag.getKeyedReferenceVector().isEmpty()))
203     {
204       handler = maker.lookup(CategoryBagHandler.TAG_NAME);
205       handler.marshal(categoryBag,element);
206     }
207
208     parent.appendChild(element);
209   }
210
211
212   /***************************************************************************/
213   /***************************** TEST DRIVER *********************************/
214   /***************************************************************************/
215
216
217   public static void main(String JavaDoc args[])
218     throws Exception JavaDoc
219   {
220     HandlerMaker maker = HandlerMaker.getInstance();
221     AbstractHandler handler = maker.lookup(BusinessEntityHandler.TAG_NAME);
222     Element JavaDoc parent = XMLUtils.newRootElement();
223     Element JavaDoc child = null;
224
225     OverviewDoc overDoc = new OverviewDoc();
226     overDoc.setOverviewURL("http://www.sviens.com/service.html");
227     overDoc.addDescription(new Description("over-doc Descr"));
228     overDoc.addDescription(new Description("over-doc Descr Two","en"));
229
230     InstanceDetails instDetails = new InstanceDetails();
231     instDetails.addDescription(new Description("body-isa-wunder"));
232     instDetails.addDescription(new Description("sweetchild-o-mine","it"));
233     instDetails.setInstanceParms("inst-det parameters");
234     instDetails.setOverviewDoc(overDoc);
235
236     TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
237     tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
238     tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
239     tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
240     tModInstInfo.setInstanceDetails(instDetails);
241
242     TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
243     tModInstDet.addTModelInstanceInfo(tModInstInfo);
244
245     BindingTemplate binding = new BindingTemplate();
246     binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
247     binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
248     binding.addDescription(new Description("whatever"));
249     binding.addDescription(new Description("whatever too","fr"));
250     binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
251     binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
252     binding.setTModelInstanceDetails(tModInstDet);
253
254     BindingTemplates bindings = new BindingTemplates();
255     bindings.addBindingTemplate(binding);
256
257     CategoryBag catBag = new CategoryBag();
258     catBag.addKeyedReference(new KeyedReference("catBagKeyName","catBagKeyValue"));
259     catBag.addKeyedReference(new KeyedReference("uuid:dfddb58c-4853-4a71-865c-f84509017cc7","catBagKeyName2","catBagKeyValue2"));
260
261     IdentifierBag idBag = new IdentifierBag();
262     idBag.addKeyedReference(new KeyedReference("idBagKeyName","idBagkeyValue"));
263     idBag.addKeyedReference(new KeyedReference("uuid:f78a135a-4769-4e79-8604-54d440314bc0","idBagKeyName2","idBagkeyValue2"));
264
265     DiscoveryURLs discURLs = new DiscoveryURLs();
266     discURLs.addDiscoveryURL(new DiscoveryURL("http","http://www.sviens.com/service.wsdl"));
267     discURLs.addDiscoveryURL(new DiscoveryURL("https","https://www.sviens.com/service.wsdl"));
268     discURLs.addDiscoveryURL(new DiscoveryURL("smtp","servicemngr@sviens.com"));
269
270     Address address = new Address();
271     address.setUseType("myAddressUseType");
272     address.setSortCode("sortThis");
273     address.setTModelKey(null);
274     address.addAddressLine(new AddressLine("AddressLine1","keyNameAttr","keyValueAttr"));
275     address.addAddressLine(new AddressLine("AddressLine2"));
276
277     Contact contact = new Contact();
278     contact.setUseType("myAddressUseType");
279     contact.setPersonNameValue("Bob Whatever");
280     contact.addDescription(new Description("Bob is a big fat jerk"));
281     contact.addDescription(new Description("obBay sIay a igBay atFay erkJay","es"));
282     contact.addEmail(new Email("bob@whatever.com"));
283     contact.addPhone(new Phone("(603)559-1901"));
284     contact.addAddress(address);
285
286     Contacts contacts = new Contacts();
287     contacts.addContact(contact);
288
289     BusinessService service = new BusinessService();
290     service.setServiceKey("fe8af00d-3a2c-4e05-b7ca-95a1259aad4f");
291     service.setBusinessKey("b8cc7266-9eed-4675-b621-34697f252a77");
292     service.setBindingTemplates(bindings);
293     service.setCategoryBag(catBag);
294     service.addName(new Name("serviceNm"));
295     service.addName(new Name("serviceNm2","en"));
296     service.addDescription(new Description("service whatever"));
297     service.addDescription(new Description("service whatever too","it"));
298
299     BusinessServices services = new BusinessServices();
300     services.addBusinessService(service);
301     services.addBusinessService(service);
302
303     BusinessEntity business = new BusinessEntity();
304     business.setBusinessKey("6c0ac186-d36b-4b81-bd27-066a5fe0fc1f");
305     business.setAuthorizedName("Steve Viens");
306     business.setOperator("jUDDI");
307     business.addName(new Name("businessNm"));
308     business.addName(new Name("businessNm2","en"));
309     business.addDescription(new Description("business whatever"));
310     business.addDescription(new Description("business whatever too","fr"));
311     business.setDiscoveryURLs(discURLs);
312     business.setCategoryBag(catBag);
313     business.setIdentifierBag(idBag);
314     business.setContacts(contacts);
315     business.setBusinessServices(services);
316
317     System.out.println();
318
319     RegistryObject regObject = business;
320     handler.marshal(regObject,parent);
321     child = (Element JavaDoc)parent.getFirstChild();
322     parent.removeChild(child);
323     XMLUtils.writeXML(child,System.out);
324
325     System.out.println();
326
327     regObject = handler.unmarshal(child);
328     handler.marshal(regObject,parent);
329     child = (Element JavaDoc)parent.getFirstChild();
330     parent.removeChild(child);
331     XMLUtils.writeXML(child,System.out);
332   }
333 }
Popular Tags