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
024package microsoft.exchange.webservices.data.property.complex;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.EwsUtilities;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
032import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
033
034import javax.xml.stream.XMLStreamException;
035
036/**
037 * Represents a mailbox reference.
038 */
039public class Mailbox extends ComplexProperty implements ISearchStringProvider {
040
041  // Routing type
042  /**
043   * The routing type.
044   */
045  private String routingType;
046
047  // Email address
048  /**
049   * The address.
050   */
051  private String address;
052
053  /**
054   * Initializes a new instance of the Mailbox class.
055   */
056  public Mailbox() {
057    super();
058  }
059
060  /**
061   * Initializes a new instance of the Mailbox class.
062   *
063   * @param smtpAddress the smtp address
064   */
065  public Mailbox(String smtpAddress) {
066    this();
067    this.setAddress(smtpAddress);
068  }
069
070  /**
071   * Initializes a new instance of the Mailbox class.
072   *
073   * @param address     the address
074   * @param routingType the routing type
075   */
076  public Mailbox(String address, String routingType) {
077    this(address);
078    this.setRoutingType(routingType);
079  }
080
081  /**
082   * Gets the address.
083   *
084   * @return the address
085   */
086  public String getAddress() {
087    return address;
088  }
089
090  /**
091   * Sets the address.
092   *
093   * @param address the new address
094   */
095  public void setAddress(String address) {
096    this.address = address;
097  }
098
099  /**
100   * True if this instance is valid, false otherthise.
101   *
102   * @return true if this instance is valid; otherwise false
103   */
104  public boolean isValid() {
105    return !(this.getAddress() == null || this.getAddress().isEmpty());
106  }
107
108  /**
109   * Gets  the routing type of the address used to refer to the user
110   * mailbox.
111   *
112   * @return the routing type
113   */
114  public String getRoutingType() {
115    return routingType;
116  }
117
118  /**
119   * Sets the routing type.
120   *
121   * @param routingType the new routing type
122   */
123  public void setRoutingType(String routingType) {
124    this.routingType = routingType;
125  }
126
127  /**
128   * Defines an implicit conversion between a string representing an SMTP
129   * address and Mailbox.
130   *
131   * @param smtpAddress the smtp address
132   * @return A Mailbox initialized with the specified SMTP address.
133   */
134  public static Mailbox getMailboxFromString(String smtpAddress) {
135    return new Mailbox(smtpAddress);
136  }
137
138  /**
139   * Tries to read element from XML.
140   *
141   * @param reader the reader
142   * @return True if element was read.
143   * @throws Exception the exception
144   */
145  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
146      throws Exception {
147    if (reader.getLocalName()
148        .equalsIgnoreCase(XmlElementNames.EmailAddress)) {
149      this.setAddress(reader.readElementValue());
150      return true;
151    } else if (reader.getLocalName().equalsIgnoreCase(
152        XmlElementNames.RoutingType)) {
153      this.setRoutingType(reader.readElementValue());
154      return true;
155    } else {
156      return false;
157    }
158  }
159
160  /**
161   * Writes elements to XML.
162   *
163   * @param writer the writer
164   * @throws XMLStreamException the XML stream exception
165   * @throws ServiceXmlSerializationException the service xml serialization exception
166   */
167  public void writeElementsToXml(EwsServiceXmlWriter writer)
168      throws XMLStreamException, ServiceXmlSerializationException {
169    writer.writeElementValue(XmlNamespace.Types,
170        XmlElementNames.EmailAddress, this.address);
171    writer.writeElementValue(XmlNamespace.Types,
172        XmlElementNames.RoutingType, this.routingType);
173  }
174
175  /**
176   * Get a string representation for using this instance in a search filter.
177   *
178   * @return String representation of instance.
179   */
180  public String getSearchString() {
181    return this.address;
182  }
183
184  /**
185   * Validates this instance.
186   *
187   * @throws Exception
188   * @throws ServiceValidationException
189   */
190  @Override
191  protected void internalValidate()
192      throws ServiceValidationException, Exception {
193    super.internalValidate();
194
195    EwsUtilities.validateNonBlankStringParamAllowNull(this.getAddress(), "address");
196    EwsUtilities.validateNonBlankStringParamAllowNull(
197        this.getRoutingType(), "routingType");
198  }
199
200
201  /**
202   * Determines whether the specified Object is equal to the current Object.
203   *
204   * @param obj the obj
205   * @return true if the specified Object is equal to the current Object
206   * otherwise, false.
207   */
208  @Override
209  public boolean equals(Object obj) {
210    if (super.equals(obj)) {
211      return true;
212    } else {
213      if (!(obj instanceof Mailbox)) {
214        return false;
215      } else {
216        Mailbox other = (Mailbox) obj;
217        if (((this.address == null) && (other.address == null))
218            || ((this.address != null) && this.address
219            .equalsIgnoreCase(other.address))) {
220          return ((this.routingType == null) &&
221              (other.routingType == null))
222              || ((this.routingType != null) && this.routingType
223              .equalsIgnoreCase(other.routingType));
224        } else {
225          return false;
226        }
227      }
228    }
229  }
230
231  /**
232   * Serves as a hash function for a particular type.
233   *
234   * @return A hash code for the current object
235   */
236  @Override
237  public int hashCode() {
238    if (!(null == this.getAddress() || this.getAddress().isEmpty())) {
239      int hashCode = this.address.hashCode();
240
241      if (!(null == this.getRoutingType() || this.getRoutingType()
242          .isEmpty())) {
243        hashCode ^= this.routingType.hashCode();
244      }
245      return hashCode;
246    } else {
247      return super.hashCode();
248    }
249  }
250
251  /**
252   * Returns a String that represents the current Object.
253   *
254   * @return A String that represents the current Object.
255   */
256  @Override
257  public String toString() {
258    if (!this.isValid()) {
259      return "";
260    } else if (!(this.routingType == null || this.routingType.isEmpty())) {
261      return this.routingType + ":" + this.address;
262    } else {
263      return this.address;
264    }
265  }
266}