View Javadoc

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;
19  
20  import org.apache.juddi.datatype.BusinessKey;
21  import org.apache.juddi.datatype.KeyedReference;
22  import org.apache.juddi.datatype.RegistryObject;
23  import org.apache.juddi.datatype.request.FindQualifier;
24  import org.apache.juddi.datatype.request.FindQualifiers;
25  import org.apache.juddi.datatype.request.FindRelatedBusinesses;
26  import org.apache.juddi.util.xml.XMLUtils;
27  import org.w3c.dom.Element;
28  
29  /***
30   * @author Steve Viens (sviens@apache.org)
31   * @author Anou Mana (anou_mana@users.sourceforge.net)
32   */
33  public class FindRelatedBusinessesHandler extends AbstractHandler
34  {
35    public static final String TAG_NAME = "find_relatedBusinesses";
36  
37    private HandlerMaker maker = null;
38  
39    protected FindRelatedBusinessesHandler(HandlerMaker maker)
40    {
41      this.maker = maker;
42    }
43  
44    public RegistryObject unmarshal(Element element)
45    {
46      FindRelatedBusinesses obj = new FindRelatedBusinesses();
47      Vector nodeList = null;
48      AbstractHandler handler = null;
49  
50      // Attributes
51      String generic = element.getAttribute("generic");
52      if ((generic != null && (generic.trim().length() > 0)))
53        obj.setGeneric(generic);
54  
55      String maxRows = element.getAttribute("maxRows");
56      if ((maxRows != null) && (maxRows.length() > 0))
57        obj.setMaxRows(maxRows);
58  
59      // Text Node Value
60      // {none}
61  
62      // Child Elements
63      nodeList = XMLUtils.getChildElementsByTagName(element,BusinessKeyHandler.TAG_NAME);
64      if (nodeList.size() > 0)
65      {
66        handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
67        obj.setBusinessKey((BusinessKey)handler.unmarshal((Element)nodeList.elementAt(0)));
68      }
69  
70      nodeList = XMLUtils.getChildElementsByTagName(element,FindQualifiersHandler.TAG_NAME);
71      if (nodeList.size() > 0)
72      {
73        handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
74        obj.setFindQualifiers((FindQualifiers)handler.unmarshal((Element)nodeList.elementAt(0)));
75      }
76  
77      nodeList = XMLUtils.getChildElementsByTagName(element,KeyedReferenceHandler.TAG_NAME);
78      if (nodeList.size() > 0)
79      {
80        handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
81        obj.setKeyedReference((KeyedReference)handler.unmarshal((Element)nodeList.elementAt(0)));
82      }
83  
84      return obj;
85    }
86  
87    public void marshal(RegistryObject object,Element parent)
88    {
89      FindRelatedBusinesses request = (FindRelatedBusinesses)object;
90      String generic = request.getGeneric();
91      generic = getGeneric(generic);
92      String namespace = getUDDINamespace(generic);
93      Element element = parent.getOwnerDocument().createElementNS(namespace,TAG_NAME);
94      AbstractHandler handler = null;
95  
96      element.setAttribute("generic",generic);
97  
98      int maxRows = request.getMaxRows();
99      if (maxRows > 0)
100       element.setAttribute("maxRows",String.valueOf(maxRows));
101 
102     FindQualifiers qualifiers = request.getFindQualifiers();
103     if ((qualifiers != null) && (qualifiers.size() > 0))
104     {
105       handler = maker.lookup(FindQualifiersHandler.TAG_NAME);
106       handler.marshal(qualifiers,element);
107     }
108 
109   BusinessKey businessKey = new BusinessKey(request.getBusinessKey());
110   if (businessKey != null)
111   {
112     handler = maker.lookup(BusinessKeyHandler.TAG_NAME);
113     handler.marshal(businessKey,element);
114   }
115 
116     KeyedReference keyedRef = request.getKeyedReference();
117     if (keyedRef != null)
118     {
119       handler = maker.lookup(KeyedReferenceHandler.TAG_NAME);
120       handler.marshal(keyedRef,element);
121     }
122 
123     parent.appendChild(element);
124   }
125 
126 
127   /****************************************************************************/
128   /****************************** TEST DRIVER *********************************/
129   /****************************************************************************/
130 
131 
132   public static void main(String args[])
133     throws Exception
134   {
135     HandlerMaker maker = HandlerMaker.getInstance();
136     AbstractHandler handler = maker.lookup(FindRelatedBusinessesHandler.TAG_NAME);
137 
138     Element parent = XMLUtils.newRootElement();
139     Element child = null;
140 
141     FindRelatedBusinesses request = new FindRelatedBusinesses();
142     request.setBusinessKey("10ad2903-932f-49fe-aaed-bf80d0ed50f0");
143     request.addFindQualifier(new FindQualifier(FindQualifier.SORT_BY_DATE_ASC));
144     request.addFindQualifier(new FindQualifier(FindQualifier.AND_ALL_KEYS));
145     request.setMaxRows(43);
146     request.setKeyedReference(new KeyedReference("uuid:8ff45356-acde-4a4c-86bf-f953611d20c6","catBagKeyName2","catBagKeyValue2"));
147 
148     System.out.println();
149 
150     RegistryObject regObject = request;
151     handler.marshal(regObject,parent);
152     child = (Element)parent.getFirstChild();
153     parent.removeChild(child);
154     XMLUtils.writeXML(child,System.out);
155 
156     System.out.println();
157 
158     regObject = handler.unmarshal(child);
159     handler.marshal(regObject,parent);
160     child = (Element)parent.getFirstChild();
161     parent.removeChild(child);
162     XMLUtils.writeXML(child,System.out);
163   }
164 }