This project has retired. For details please refer to its Attic page.
OrganizationImpl xref
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  
17  package org.apache.ws.scout.registry.infomodel;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Set;
24  
25  import javax.xml.registry.JAXRException;
26  import javax.xml.registry.LifeCycleManager;
27  import javax.xml.registry.UnsupportedCapabilityException;
28  import javax.xml.registry.infomodel.Organization;
29  import javax.xml.registry.infomodel.PostalAddress;
30  import javax.xml.registry.infomodel.Service;
31  import javax.xml.registry.infomodel.TelephoneNumber;
32  import javax.xml.registry.infomodel.User;
33  
34  /**
35   * Organization Interface
36   * * Implements JAXR Interface.
37   * For futher details, look into the JAXR API Javadoc.
38   *
39   * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
40   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
41   */
42  public class OrganizationImpl extends RegistryObjectImpl implements Organization
43  {
44      private User primaryContact;
45      private Set<User> users = new HashSet<User>();
46      private Set<TelephoneNumber> telephoneNumbers = new HashSet<TelephoneNumber>();
47      private Collection<Service> services = new ArrayList<Service>();
48  
49      public OrganizationImpl(LifeCycleManager lifeCycleManager)
50      {
51          super(lifeCycleManager);
52      }
53  
54      public User getPrimaryContact() throws JAXRException
55      {
56          //TODO: How do we fix this? Run JAXRQueryTest and you will hit this problem.
57          //
58          //  gmj : I think I fixed w/ primary contact hack...
59          //
60          //if (primaryContact == null) {
61          //    throw new IllegalStateException("primaryContact is null and the spec says we cannot return a null value");
62          //}
63          return primaryContact;
64      }
65  
66      public void setPrimaryContact(User user) throws JAXRException
67      {
68          if (user == null)
69          {
70              throw new IllegalArgumentException("primaryContact must not be null");
71          }
72  
73          /*
74           * first check to see if user already exists in user set
75           */
76  
77          primaryContact = user;
78  
79          if (!users.contains(user)) {
80              addUser(user);
81          }
82      }
83  
84      public void addUser(User user) throws JAXRException
85      {
86          doPrimaryContactHack(user);
87  
88          users.add(user);
89          ((UserImpl) user).setOrganization(this);
90      }
91  
92      /**
93       *
94       *  to solve the getPrimaryContactProblem(), if we have no defined
95       *  primaryContact, we'll designate the first user as such
96       *
97       * @param user
98       */
99      private void doPrimaryContactHack(User user) {
100 
101         if (primaryContact == null && users.size() == 0) {
102             primaryContact = user;
103         }
104     }
105 
106     public void addUsers(Collection collection) throws JAXRException
107     {
108         // do this by hand to ensure all members are actually instances of User
109         for (Iterator iterator = collection.iterator(); iterator.hasNext();)
110         {
111             User user = (User) iterator.next();
112             addUser(user);
113         }
114     }
115 
116     public Collection<User> getUsers() throws JAXRException
117     {
118         return users;
119     }
120 
121     public void removeUser(User user) throws JAXRException
122     {
123         if (user != null) {
124             users.remove(user);
125         }
126 
127         /*
128          * more primaryContact hakiness - nothing says that you can't
129          * remove the user that is the PC...
130          */
131 
132         if (!users.contains(primaryContact)) {
133             primaryContact = null;
134         }
135     }
136 
137     public void removeUsers(Collection collection) throws JAXRException
138     {
139         if (collection != null) {
140             users.removeAll(collection);
141         }
142 
143         /*
144          * more primaryContact hakiness - nothing says that you can't
145          * remove the user that is the PC...
146          */
147 
148         if (!users.contains(primaryContact)) {
149             primaryContact = null;
150         }
151 
152     }
153 
154     public Collection<TelephoneNumber> getTelephoneNumbers(String phoneType) throws JAXRException
155     {
156         Set<TelephoneNumber> filteredNumbers;
157         if (phoneType == null)
158         {
159             filteredNumbers = telephoneNumbers;
160         } else
161         {
162             filteredNumbers = new HashSet<TelephoneNumber>(telephoneNumbers.size());
163             for (Iterator i = telephoneNumbers.iterator(); i.hasNext();)
164             {
165                 TelephoneNumber number = (TelephoneNumber) i.next();
166                 if (phoneType.equals(number.getType()))
167                 {
168                     filteredNumbers.add(number);
169                 }
170             }
171         }
172         return filteredNumbers;
173     }
174 
175     public void setTelephoneNumbers(Collection collection) throws JAXRException
176     {
177         // do this by hand to ensure all members are actually instances of TelephoneNumber
178         Set<TelephoneNumber> numbers = new HashSet<TelephoneNumber>(collection.size());
179         for (Object number : collection) {
180             numbers.add((TelephoneNumber) number);
181         }
182         this.telephoneNumbers = numbers;
183     }
184 
185     public void addService(Service service) throws JAXRException
186     {
187         services.add(service);
188 
189         /*
190          * we need to tell the service who it's organization is so
191          * we can set the UDDI Business Key...
192          */
193 
194         service.setProvidingOrganization(this);
195     }
196 
197     public void addServices(Collection collection) throws JAXRException
198     {
199         // do this by hand to ensure all members are actually instances of Service
200         for (Iterator iterator = collection.iterator(); iterator.hasNext();)
201         {
202             Service service = (Service) iterator.next();
203 
204             addService(service);
205         }
206     }
207 
208     public Collection<Service> getServices() throws JAXRException
209     {
210         return services;
211     }
212 
213     public void removeService(Service service) throws JAXRException
214     {
215     	services.remove(service);
216     }
217 
218     public void removeServices(Collection collection) throws JAXRException
219     {
220         services.removeAll(collection);
221     }
222 
223     ///////////////////////////////////////////////////////////////////////////
224     // Level 1 features must throw exceptions
225     ///////////////////////////////////////////////////////////////////////////
226 
227     public Organization getParentOrganization() throws JAXRException
228     {
229         throw new UnsupportedCapabilityException("Level 1 feature");
230     }
231 
232     public Collection<Organization> getDescendantOrganizations() throws JAXRException
233     {
234         throw new UnsupportedCapabilityException("Level 1 feature");
235     }
236 
237     public Organization getRootOrganization() throws JAXRException
238     {
239         throw new UnsupportedCapabilityException("Level 1 feature");
240     }
241 
242     public void addChildOrganization(Organization organization) throws JAXRException
243     {
244         throw new UnsupportedCapabilityException("Level 1 feature");
245     }
246 
247     public void addChildOrganizations(Collection collection) throws JAXRException
248     {
249         throw new UnsupportedCapabilityException("Level 1 feature");
250     }
251 
252     public int getChildOrganizationCount() throws JAXRException
253     {
254         throw new UnsupportedCapabilityException("Level 1 feature");
255     }
256 
257     public Collection<Organization> getChildOrganizations() throws JAXRException
258     {
259         throw new UnsupportedCapabilityException("Level 1 feature");
260     }
261 
262     public void removeChildOrganization(Organization organization) throws JAXRException
263     {
264         throw new UnsupportedCapabilityException("Level 1 feature");
265     }
266 
267     public void removeChildOrganizations(Collection collection) throws JAXRException
268     {
269         throw new UnsupportedCapabilityException("Level 1 feature");
270     }
271 
272     public PostalAddress getPostalAddress() throws JAXRException
273     {
274         throw new UnsupportedCapabilityException("Level 1 feature");
275     }
276 
277     public void setPostalAddress(PostalAddress postalAddress) throws JAXRException
278     {
279         throw new UnsupportedCapabilityException("Level 1 feature");
280     }
281 }