001 /*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024 package microsoft.exchange.webservices.data.property.complex;
025
026 import microsoft.exchange.webservices.data.attribute.RequiredServerVersion;
027 import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028 import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
029 import microsoft.exchange.webservices.data.core.EwsUtilities;
030 import microsoft.exchange.webservices.data.core.XmlAttributeNames;
031 import microsoft.exchange.webservices.data.core.XmlElementNames;
032 import microsoft.exchange.webservices.data.core.service.item.Contact;
033 import microsoft.exchange.webservices.data.core.enumeration.property.EmailAddressKey;
034 import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
035 import microsoft.exchange.webservices.data.core.enumeration.property.MailboxType;
036 import microsoft.exchange.webservices.data.core.enumeration.property.MemberStatus;
037 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
038 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
039 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
040
041 /**
042 * Represents a group member.
043 */
044 @RequiredServerVersion(version = ExchangeVersion.Exchange2010)
045 public class GroupMember extends ComplexProperty implements IComplexPropertyChangedDelegate {
046
047 // AddressInformation field.
048 /**
049 * The address information.
050 */
051 private EmailAddress addressInformation;
052
053 // Status field.
054
055 /**
056 * The status.
057 */
058 private MemberStatus status;
059
060 // / Member key field.
061
062 /**
063 * The key.
064 */
065 private String key;
066
067 /**
068 * Initializes a new instance of the GroupMember class.
069 */
070
071 public GroupMember() {
072 super();
073
074 // Key is assigned by server
075 this.key = null;
076
077 // Member status is calculated by server
078 this.status = MemberStatus.Unrecognized;
079 }
080
081 /**
082 * Initializes a new instance of the GroupMember class.
083 *
084 * @param smtpAddress The SMTP address of the member
085 */
086 public GroupMember(String smtpAddress) {
087 this();
088 this.setAddressInformation(new EmailAddress(smtpAddress));
089 }
090
091 /**
092 * Initializes a new instance of the GroupMember class.
093 *
094 * @param address the address
095 * @param routingType The routing type of the address.
096 * @param mailboxType The mailbox type of the member.
097 * @throws ServiceLocalException the service local exception
098 */
099 public GroupMember(String address, String routingType,
100 MailboxType mailboxType) throws ServiceLocalException {
101 this();
102
103 switch (mailboxType) {
104 case PublicGroup:
105 case PublicFolder:
106 case Mailbox:
107 case Contact:
108 case OneOff:
109 this.setAddressInformation(new EmailAddress(null, address,
110 routingType, mailboxType));
111 break;
112
113 default:
114 throw new ServiceLocalException("The mailbox type isn't valid.");
115 }
116 }
117
118 /**
119 * Initializes a new instance of the GroupMember class.
120 *
121 * @param smtpAddress The SMTP address of the member
122 * @param mailboxType The mailbox type of the member.
123 * @throws ServiceLocalException the service local exception
124 */
125 public GroupMember(String smtpAddress, MailboxType mailboxType)
126 throws ServiceLocalException {
127
128 this(smtpAddress, EmailAddress.SmtpRoutingType, mailboxType);
129
130 }
131
132 /**
133 * Initializes a new instance of the GroupMember class.
134 *
135 * @param name The name of the one-off member.
136 * @param address the address
137 * @param routingType The routing type of the address.
138 */
139 public GroupMember(String name, String address, String routingType) {
140 this();
141
142 this.setAddressInformation(new EmailAddress(name, address, routingType,
143 MailboxType.OneOff));
144 }
145
146 /**
147 * Initializes a new instance of the GroupMember class.
148 *
149 * @param name The name of the one-off member.
150 * @param smtpAddress The SMTP address of the member
151 */
152 public GroupMember(String name, String smtpAddress) {
153 this(name, smtpAddress, EmailAddress.SmtpRoutingType);
154
155 }
156
157 /**
158 * Initializes a new instance of the GroupMember class.
159 *
160 * @param contactGroupId The Id of the contact group to link the member to.
161 */
162 public GroupMember(ItemId contactGroupId) {
163 this();
164
165 this.setAddressInformation(new EmailAddress(null, null, null,
166 MailboxType.ContactGroup, contactGroupId));
167 }
168
169 /**
170 * Initializes a new instance of the GroupMember class.
171 *
172 * @param contactId The Id of the contact member
173 * @param addressToLink The Id of the contact to link the member to.
174 */
175 public GroupMember(ItemId contactId, String addressToLink) {
176 this();
177
178 this.setAddressInformation(new EmailAddress(null, addressToLink, null,
179 MailboxType.Contact, contactId));
180 }
181
182 /**
183 * Initializes a new instance of the GroupMember class.
184 *
185 * @param addressInformation The e-mail address of the member.
186 * @throws Exception the exception
187 */
188 public GroupMember(EmailAddress addressInformation) throws Exception {
189 this();
190
191 this.setAddressInformation(new EmailAddress(addressInformation));
192 }
193
194 /**
195 * Initializes a new instance of the GroupMember class.
196 *
197 * @param member GroupMember class instance to copy.
198 * @throws Exception the exception
199 */
200 protected GroupMember(GroupMember member) throws Exception {
201 this();
202
203 EwsUtilities.validateParam(member, "member");
204 this.setAddressInformation(new EmailAddress(member
205 .getAddressInformation()));
206 }
207
208 /**
209 * Initializes a new instance of the GroupMember class.
210 *
211 * @param contact The contact to link to.
212 * @param emailAddressKey The contact's e-mail address to link to.
213 * @throws Exception the exception
214 */
215 public GroupMember(Contact contact, EmailAddressKey emailAddressKey)
216 throws Exception {
217 this();
218
219 EwsUtilities.validateParam(contact, "contact");
220 EmailAddress emailAddress = contact.getEmailAddresses()
221 .getEmailAddress(emailAddressKey);
222 this.setAddressInformation(new EmailAddress(emailAddress));
223 this.getAddressInformation().setId(contact.getId());
224 }
225
226 /**
227 * Gets the key of the member.
228 *
229 * @return the key
230 */
231 public String getKey() {
232
233 return this.key;
234
235 }
236
237 /**
238 * Gets the address information of the member.
239 *
240 * @return the address information
241 */
242 public EmailAddress getAddressInformation() {
243
244 return this.addressInformation;
245 }
246
247 /**
248 * Sets the address information.
249 *
250 * @param value the new address information
251 */
252 protected void setAddressInformation(EmailAddress value) {
253
254 if (this.addressInformation != null) {
255
256 this.addressInformation.removeChangeEvent(this);
257 }
258
259 this.addressInformation = value;
260
261 if (this.addressInformation != null) {
262
263 this.addressInformation.addOnChangeEvent(this);
264 }
265 }
266
267 /**
268 * Gets the status of the member.
269 *
270 * @return the status
271 */
272
273 public MemberStatus getStatus() {
274
275 return this.status;
276
277 }
278
279 /**
280 * Reads the member Key attribute from XML.
281 *
282 * @param reader the reader
283 * @throws Exception the exception
284 */
285 public void readAttributesFromXml(EwsServiceXmlReader reader)
286 throws Exception {
287 this.key = reader.readAttributeValue(String.class,
288 XmlAttributeNames.Key);
289 }
290
291 /**
292 * Tries to read Status or Mailbox elements from XML.
293 *
294 * @param reader the reader
295 * @return True if element was read.
296 * @throws Exception the exception
297 */
298 public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
299 throws Exception {
300 if (reader.getLocalName().equals(XmlElementNames.Status)) {
301
302 this.status = EwsUtilities.parse(MemberStatus.class, reader
303 .readElementValue());
304 return true;
305 } else if (reader.getLocalName().equals(XmlElementNames.Mailbox)) {
306
307 this.setAddressInformation(new EmailAddress());
308 this.getAddressInformation().loadFromXml(reader,
309 reader.getLocalName());
310 return true;
311 } else {
312
313 return false;
314 }
315 }
316
317 /**
318 * Writes the member key attribute to XML.
319 *
320 * @param writer the writer
321 * @throws ServiceXmlSerializationException the service xml serialization exception
322 */
323 public void writeAttributesToXml(EwsServiceXmlWriter writer)
324 throws ServiceXmlSerializationException {
325 // if this.key is null or empty, writer skips the attribute
326 writer.writeAttributeValue(XmlAttributeNames.Key, this.key);
327 }
328
329 /**
330 * Writes elements to XML.
331 *
332 * @param writer the writer
333 * @throws Exception the exception
334 */
335 public void writeElementsToXml(EwsServiceXmlWriter writer)
336 throws Exception {
337 // No need to write member Status back to server
338 // Write only AddressInformation container element
339 this.getAddressInformation().writeToXml(writer, XmlNamespace.Types,
340 XmlElementNames.Mailbox);
341 }
342
343 /**
344 * AddressInformation instance is changed.
345 *
346 * @param complexProperty Changed property.
347 */
348 private void addressInformationChanged(ComplexProperty complexProperty) {
349 this.changed();
350 }
351
352 /**
353 * Complex property changed.
354 *
355 * @param complexProperty accepts ComplexProperty
356 */
357 @Override
358 public void complexPropertyChanged(ComplexProperty complexProperty) {
359
360 this.addressInformationChanged(complexProperty);
361 }
362 }