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.availability;
025
026import microsoft.exchange.webservices.data.ISelfValidate;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
029import microsoft.exchange.webservices.data.core.EwsUtilities;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.core.enumeration.property.OofExternalAudience;
032import microsoft.exchange.webservices.data.core.enumeration.property.OofState;
033import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
034import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
035import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
036import microsoft.exchange.webservices.data.misc.availability.OofReply;
037import microsoft.exchange.webservices.data.misc.availability.TimeWindow;
038import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
039
040import javax.xml.stream.XMLStreamException;
041
042/**
043 * Represents a user's Out of Office (OOF) settings.
044 */
045public final class OofSettings extends ComplexProperty implements ISelfValidate {
046
047  /**
048   * The state.
049   */
050  private OofState state = OofState.Disabled;
051
052  /**
053   * The external audience.
054   */
055  private OofExternalAudience externalAudience = OofExternalAudience.None;
056
057  /**
058   * The allow external oof.
059   */
060  private OofExternalAudience allowExternalOof = OofExternalAudience.None;
061
062  /**
063   * The duration.
064   */
065  private TimeWindow duration;
066
067  /**
068   * The internal reply.
069   */
070  private OofReply internalReply;
071
072  /**
073   * The external reply.
074   */
075  private OofReply externalReply;
076
077  /**
078   * Serializes an OofReply. Emits an empty OofReply in case the one passed in
079   * is null.
080   *
081   * @param oofReply       The oof reply
082   * @param writer         The writer
083   * @param xmlElementName Name of the xml element
084   * @throws XMLStreamException the XML stream exception
085   * @throws ServiceXmlSerializationException the service xml serialization exception
086   */
087  private void serializeOofReply(OofReply oofReply,
088      EwsServiceXmlWriter writer, String xmlElementName)
089      throws XMLStreamException, ServiceXmlSerializationException {
090    if (oofReply != null) {
091      oofReply.writeToXml(writer, xmlElementName);
092    } else {
093      OofReply.writeEmptyReplyToXml(writer, xmlElementName);
094    }
095  }
096
097  /**
098   * Initializes a new instance of OofSettings.
099   */
100  public OofSettings()
101
102  {
103    super();
104  }
105
106  /**
107   * Tries to read element from XML.
108   *
109   * @param reader The reader
110   * @return True if appropriate element was read.
111   * @throws Exception the exception
112   */
113  @Override
114  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
115      throws Exception {
116    if (reader.getLocalName().equals(XmlElementNames.OofState)) {
117      this.state = reader.readValue(OofState.class);
118      return true;
119    } else if (reader.getLocalName().equals(
120        XmlElementNames.ExternalAudience)) {
121      this.externalAudience = reader.readValue(OofExternalAudience.class);
122      return true;
123    } else if (reader.getLocalName().equals(XmlElementNames.Duration)) {
124      this.duration = new TimeWindow();
125      this.duration.loadFromXml(reader);
126      return true;
127    } else if (reader.getLocalName().equals(XmlElementNames.InternalReply)) {
128      this.internalReply = new OofReply();
129      this.internalReply.loadFromXml(reader, reader.getLocalName());
130      return true;
131    } else if (reader.getLocalName().equals(XmlElementNames.ExternalReply)) {
132      this.externalReply = new OofReply();
133      this.externalReply.loadFromXml(reader, reader.getLocalName());
134      return true;
135    } else {
136      return false;
137    }
138  }
139
140  /**
141   * Writes elements to XML.
142   *
143   * @param writer The writer
144   * @throws Exception the exception
145   */
146  @Override
147  public void writeElementsToXml(EwsServiceXmlWriter writer)
148      throws Exception {
149    super.writeElementsToXml(writer);
150
151    writer.writeElementValue(XmlNamespace.Types, XmlElementNames.OofState,
152        this.getState());
153
154    writer.writeElementValue(XmlNamespace.Types,
155        XmlElementNames.ExternalAudience, this.getExternalAudience());
156
157    if (this.getDuration() != null && this.getState() == OofState.Scheduled) {
158      this.getDuration().writeToXml(writer, XmlElementNames.Duration);
159    }
160
161    this.serializeOofReply(this.getInternalReply(), writer,
162        XmlElementNames.InternalReply);
163    this.serializeOofReply(this.getExternalReply(), writer,
164        XmlElementNames.ExternalReply);
165  }
166
167  /**
168   * Gets the user's OOF state.
169   *
170   * @return The user's OOF state.
171   */
172  public OofState getState() {
173    return state;
174  }
175
176  /**
177   * Sets the user's OOF state.
178   *
179   * @param state the new state
180   */
181  public void setState(OofState state) {
182    this.state = state;
183  }
184
185  /**
186   * Gets a value indicating who should receive external OOF messages.
187   *
188   * @return the external audience
189   */
190  public OofExternalAudience getExternalAudience() {
191    return externalAudience;
192  }
193
194  /**
195   * Sets a value indicating who should receive external OOF messages.
196   *
197   * @param externalAudience the new external audience
198   */
199  public void setExternalAudience(OofExternalAudience externalAudience) {
200    this.externalAudience = externalAudience;
201  }
202
203  /**
204   * Gets the duration of the OOF status when State is set to
205   * OofState.Scheduled.
206   *
207   * @return the duration
208   */
209  public TimeWindow getDuration() {
210    return duration;
211  }
212
213  /**
214   * Sets the duration of the OOF status when State is set to
215   * OofState.Scheduled.
216   *
217   * @param duration the new duration
218   */
219  public void setDuration(TimeWindow duration) {
220    this.duration = duration;
221  }
222
223  /**
224   * Gets the OOF response sent other users in the user's domain or trusted
225   * domain.
226   *
227   * @return the internal reply
228   */
229  public OofReply getInternalReply() {
230    return internalReply;
231  }
232
233  /**
234   * Sets the OOF response sent other users in the user's domain or trusted
235   * domain.
236   *
237   * @param internalReply the new internal reply
238   */
239  public void setInternalReply(OofReply internalReply) {
240    this.internalReply = internalReply;
241  }
242
243  /**
244   * Gets the OOF response sent to addresses outside the user's domain or
245   * trusted domain.
246   *
247   * @return the external reply
248   */
249  public OofReply getExternalReply() {
250    return externalReply;
251  }
252
253  /**
254   * Sets the OOF response sent to addresses outside the user's domain or
255   * trusted domain.
256   *
257   * @param externalReply the new external reply
258   */
259  public void setExternalReply(OofReply externalReply) {
260    this.externalReply = externalReply;
261  }
262
263  /**
264   * Gets a value indicating the authorized external OOF notification.
265   *
266   * @return the allow external oof
267   */
268  public OofExternalAudience getAllowExternalOof() {
269    return allowExternalOof;
270  }
271
272  /**
273   * Sets a value indicating the authorized external OOF notification.
274   *
275   * @param allowExternalOof the new allow external oof
276   */
277  public void setAllowExternalOof(OofExternalAudience allowExternalOof) {
278    this.allowExternalOof = allowExternalOof;
279  }
280
281  /**
282   * Validates this instance.
283   *
284   * @throws Exception the exception
285   */
286  @Override
287  public void validate() throws Exception {
288    if (this.getState() == OofState.Scheduled) {
289      if (this.getDuration() == null) {
290        throw new ArgumentException("Duration must be specified when State is equal to Scheduled.");
291      }
292
293      EwsUtilities.validateParam(this.getDuration(), "Duration");
294    }
295  }
296
297}