001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.ws.scout.registry.infomodel;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: import javax.xml.registry.JAXRException;
026: import javax.xml.registry.LifeCycleManager;
027: import javax.xml.registry.UnsupportedCapabilityException;
028: import javax.xml.registry.infomodel.Organization;
029: import javax.xml.registry.infomodel.PostalAddress;
030: import javax.xml.registry.infomodel.Service;
031: import javax.xml.registry.infomodel.TelephoneNumber;
032: import javax.xml.registry.infomodel.User;
033:
034: /**
035: * Organization Interface
036: * * Implements JAXR Interface.
037: * For futher details, look into the JAXR API Javadoc.
038: *
039: * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
040: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
041: */
042: public class OrganizationImpl extends RegistryObjectImpl implements
043: Organization {
044: private User primaryContact;
045: private Set<User> users = new HashSet<User>();
046: private Set<TelephoneNumber> telephoneNumbers = new HashSet<TelephoneNumber>();
047: private Collection<Service> services = new ArrayList<Service>();
048:
049: public OrganizationImpl(LifeCycleManager lifeCycleManager) {
050: super (lifeCycleManager);
051: }
052:
053: public User getPrimaryContact() throws JAXRException {
054: //TODO: How do we fix this? Run JAXRQueryTest and you will hit this problem.
055: //
056: // gmj : I think I fixed w/ primary contact hack...
057: //
058: //if (primaryContact == null) {
059: // throw new IllegalStateException("primaryContact is null and the spec says we cannot return a null value");
060: //}
061: return primaryContact;
062: }
063:
064: public void setPrimaryContact(User user) throws JAXRException {
065: if (user == null) {
066: throw new IllegalArgumentException(
067: "primaryContact must not be null");
068: }
069:
070: /*
071: * first check to see if user already exists in user set
072: */
073:
074: primaryContact = user;
075:
076: if (!users.contains(user)) {
077: addUser(user);
078: }
079: }
080:
081: public void addUser(User user) throws JAXRException {
082: doPrimaryContactHack(user);
083:
084: users.add(user);
085: ((UserImpl) user).setOrganization(this );
086: }
087:
088: /**
089: *
090: * to solve the getPrimaryContactProblem(), if we have no defined
091: * primaryContact, we'll designate the first user as such
092: *
093: * @param user
094: */
095: private void doPrimaryContactHack(User user) {
096:
097: if (primaryContact == null && users.size() == 0) {
098: primaryContact = user;
099: }
100: }
101:
102: public void addUsers(Collection collection) throws JAXRException {
103: // do this by hand to ensure all members are actually instances of User
104: for (Iterator iterator = collection.iterator(); iterator
105: .hasNext();) {
106: User user = (User) iterator.next();
107: addUser(user);
108: }
109: }
110:
111: public Collection<User> getUsers() throws JAXRException {
112: return users;
113: }
114:
115: public void removeUser(User user) throws JAXRException {
116: if (user != null) {
117: users.remove(user);
118: }
119:
120: /*
121: * more primaryContact hakiness - nothing says that you can't
122: * remove the user that is the PC...
123: */
124:
125: if (!users.contains(primaryContact)) {
126: primaryContact = null;
127: }
128: }
129:
130: public void removeUsers(Collection collection) throws JAXRException {
131: if (collection != null) {
132: users.removeAll(collection);
133: }
134:
135: /*
136: * more primaryContact hakiness - nothing says that you can't
137: * remove the user that is the PC...
138: */
139:
140: if (!users.contains(primaryContact)) {
141: primaryContact = null;
142: }
143:
144: }
145:
146: public Collection<TelephoneNumber> getTelephoneNumbers(
147: String phoneType) throws JAXRException {
148: Set<TelephoneNumber> filteredNumbers;
149: if (phoneType == null) {
150: filteredNumbers = telephoneNumbers;
151: } else {
152: filteredNumbers = new HashSet<TelephoneNumber>(
153: telephoneNumbers.size());
154: for (Iterator i = telephoneNumbers.iterator(); i.hasNext();) {
155: TelephoneNumber number = (TelephoneNumber) i.next();
156: if (phoneType.equals(number.getType())) {
157: filteredNumbers.add(number);
158: }
159: }
160: }
161: return filteredNumbers;
162: }
163:
164: public void setTelephoneNumbers(
165: Collection<TelephoneNumber> collection)
166: throws JAXRException {
167: // do this by hand to ensure all members are actually instances of TelephoneNumber
168: Set<TelephoneNumber> numbers = new HashSet<TelephoneNumber>(
169: collection.size());
170: for (Iterator i = collection.iterator(); i.hasNext();) {
171: TelephoneNumber number = (TelephoneNumber) i.next();
172: numbers.add(number);
173: }
174: this .telephoneNumbers = numbers;
175: }
176:
177: public void addService(Service service) throws JAXRException {
178: services.add(service);
179:
180: /*
181: * we need to tell the service who it's organization is so
182: * we can set the UDDI Business Key...
183: */
184:
185: service.setProvidingOrganization(this );
186: }
187:
188: public void addServices(Collection collection) throws JAXRException {
189: // do this by hand to ensure all members are actually instances of Service
190: for (Iterator iterator = collection.iterator(); iterator
191: .hasNext();) {
192: Service service = (Service) iterator.next();
193:
194: addService(service);
195: }
196: }
197:
198: public Collection<Service> getServices() throws JAXRException {
199: return services;
200: }
201:
202: public void removeService(Service service) throws JAXRException {
203: services.remove(service);
204: }
205:
206: public void removeServices(Collection collection)
207: throws JAXRException {
208: services.removeAll(collection);
209: }
210:
211: ///////////////////////////////////////////////////////////////////////////
212: // Level 1 features must throw exceptions
213: ///////////////////////////////////////////////////////////////////////////
214:
215: public Organization getParentOrganization() throws JAXRException {
216: throw new UnsupportedCapabilityException("Level 1 feature");
217: }
218:
219: public Collection<Organization> getDescendantOrganizations()
220: throws JAXRException {
221: throw new UnsupportedCapabilityException("Level 1 feature");
222: }
223:
224: public Organization getRootOrganization() throws JAXRException {
225: throw new UnsupportedCapabilityException("Level 1 feature");
226: }
227:
228: public void addChildOrganization(Organization organization)
229: throws JAXRException {
230: throw new UnsupportedCapabilityException("Level 1 feature");
231: }
232:
233: public void addChildOrganizations(Collection collection)
234: throws JAXRException {
235: throw new UnsupportedCapabilityException("Level 1 feature");
236: }
237:
238: public int getChildOrganizationCount() throws JAXRException {
239: throw new UnsupportedCapabilityException("Level 1 feature");
240: }
241:
242: public Collection<Organization> getChildOrganizations()
243: throws JAXRException {
244: throw new UnsupportedCapabilityException("Level 1 feature");
245: }
246:
247: public void removeChildOrganization(Organization organization)
248: throws JAXRException {
249: throw new UnsupportedCapabilityException("Level 1 feature");
250: }
251:
252: public void removeChildOrganizations(Collection collection)
253: throws JAXRException {
254: throw new UnsupportedCapabilityException("Level 1 feature");
255: }
256:
257: public PostalAddress getPostalAddress() throws JAXRException {
258: throw new UnsupportedCapabilityException("Level 1 feature");
259: }
260:
261: public void setPostalAddress(PostalAddress postalAddress)
262: throws JAXRException {
263: throw new UnsupportedCapabilityException("Level 1 feature");
264: }
265: }
|