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.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
031import org.apache.commons.lang3.StringUtils;
032
033/**
034 * Represents the Id of an Exchange object.
035 */
036public abstract class ServiceId extends ComplexProperty {
037
038  /**
039   * The change key.
040   */
041  private String changeKey;
042
043  /**
044   * The unique id.
045   */
046  private String uniqueId;
047
048  /**
049   * Initializes a new instance.
050   */
051  public ServiceId() {
052    super();
053  }
054
055  /**
056   * Initializes a new instance.
057   *
058   * @param uniqueId The unique id.
059   * @throws Exception the exception
060   */
061  public ServiceId(String uniqueId) throws Exception {
062    this();
063    EwsUtilities.validateParam(uniqueId, "uniqueId");
064    this.uniqueId = uniqueId;
065  }
066
067  /**
068   * Read attribute from XML.
069   *
070   * @param reader The reader.
071   * @throws Exception the exception
072   */
073  @Override
074  public void readAttributesFromXml(EwsServiceXmlReader reader)
075      throws Exception {
076    this.uniqueId = reader.readAttributeValue(XmlAttributeNames.Id);
077    this.changeKey = reader.readAttributeValue(XmlAttributeNames.ChangeKey);
078
079  }
080
081  /**
082   * Writes attribute to XML.
083   *
084   * @param writer The writer.
085   * @throws ServiceXmlSerializationException the service xml serialization exception
086   */
087  @Override
088  public void writeAttributesToXml(EwsServiceXmlWriter writer)
089      throws ServiceXmlSerializationException {
090    writer.writeAttributeValue(XmlAttributeNames.Id, this.getUniqueId());
091    writer.writeAttributeValue(XmlAttributeNames.ChangeKey, this
092        .getChangeKey());
093  }
094
095  /**
096   * Gets the name of the XML element.
097   *
098   * @return XML element name.
099   */
100  public abstract String getXmlElementName();
101
102  /**
103   * Writes to XML.
104   *
105   * @param writer The writer.
106   * @throws Exception the exception
107   */
108  public void writeToXml(EwsServiceXmlWriter writer) throws Exception {
109    this.writeToXml(writer, this.getXmlElementName());
110  }
111
112  /**
113   * Assigns from existing id.
114   *
115   * @param source The source.
116   */
117  public void assign(ServiceId source) {
118    this.uniqueId = source.getUniqueId();
119    this.changeKey = source.getChangeKey();
120  }
121
122  /**
123   * True if this instance is valid, false otherthise.
124   *
125   * @return true if this instance is valid; otherwise,false
126   */
127  public boolean isValid() {
128    return (null != this.uniqueId && !this.uniqueId.isEmpty());
129  }
130
131  /**
132   * Gets the unique Id of the Exchange object.
133   *
134   * @return unique Id of the Exchange object.
135   */
136  public String getUniqueId() {
137    return uniqueId;
138  }
139
140  /**
141   * Sets the unique Id of the Exchange object.
142   *
143   * @param uniqueId unique Id of the Exchange object.
144   */
145  public void setUniqueId(String uniqueId) {
146    this.uniqueId = uniqueId;
147  }
148
149  /**
150   * Gets the change key associated with the Exchange object. The change key
151   * represents the version of the associated item or folder.
152   *
153   * @return change key associated with the Exchange object.
154   */
155  public String getChangeKey() {
156    return changeKey;
157  }
158
159  /**
160   * Sets the change key associated with the Exchange object. The change key
161   * represents the version of the associated item or folder.
162   *
163   * @param changeKey change key associated with the Exchange object.
164   */
165  public void setChangeKey(String changeKey) {
166    this.changeKey = changeKey;
167  }
168
169  /**
170   * Determines whether two ServiceId instances are equal (including
171   * ChangeKeys).
172   *
173   * @param other The ServiceId to compare with the current ServiceId.
174   * @return true if equal otherwise false.
175   */
176  public boolean sameIdAndChangeKey(final ServiceId other) {
177    return this.equals(other) && StringUtils.equals(this.getChangeKey(), other.getChangeKey());
178  }
179
180  /**
181   * Determines whether the specified instance is equal to the current
182   * instance. We do not consider the ChangeKey for ServiceId.Equals.
183   *
184   * @param obj The object to compare with the current instance
185   * @return true if the specified object is equal to the current instance,
186   * otherwise, false.
187   */
188  @Override
189  public boolean equals(Object obj) {
190    if (super.equals(obj)) {
191      return true;
192    } else {
193      if (!(obj instanceof ServiceId)) {
194        return false;
195      } else {
196        ServiceId other = (ServiceId) obj;
197        if (!(this.isValid() && other.isValid())) {
198          return false;
199        } else {
200          return this.getUniqueId().equals(other.getUniqueId());
201        }
202      }
203    }
204  }
205
206  /**
207   * Serves as a hash function for a particular type. We do not consider the
208   * change key in the hash code computation.
209   *
210   * @return A hash code for the current
211   */
212  @Override
213  public int hashCode() {
214    return this.isValid() ? this.getUniqueId().hashCode() : super
215        .hashCode();
216  }
217
218  /**
219   * Returns a string that represents the current instance.
220   *
221   * @return A string that represents the current instance.
222   */
223  @Override
224  public String toString() {
225    return (this.uniqueId == null) ? "" : this.uniqueId;
226  }
227}