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.XmlElementNames;
031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
033import microsoft.exchange.webservices.data.misc.TimeSpan;
034import microsoft.exchange.webservices.data.property.complex.time.TimeZoneDefinition;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037
038/**
039 * Represents a time zone in which a meeting is defined.
040 */
041public final class MeetingTimeZone extends ComplexProperty {
042
043  private static final Log LOG = LogFactory.getLog(MeetingTimeZone.class);
044
045  /**
046   * The name.
047   */
048  private String name;
049
050  /**
051   * The base offset.
052   */
053  private TimeSpan baseOffset;
054
055  /**
056   * The standard.
057   */
058  private TimeChange standard;
059
060  /**
061   * The daylight.
062   */
063  private TimeChange daylight;
064
065  /**
066   * Initializes a new instance of the MeetingTimeZone class.
067   *
068   * @param timeZone The time zone used to initialize this instance.
069   */
070  public MeetingTimeZone(TimeZoneDefinition timeZone) {
071    // Unfortunately, MeetingTimeZone does not support all the time
072    // transition types
073    // supported by TimeZoneInfo. That leaves us unable to accurately
074    // convert TimeZoneInfo
075    // into MeetingTimeZone. So we don't... Instead, we emit the time zone's
076    // Id and
077    // hope the server will find a match (which it should).
078    this.name = timeZone.getId();
079  }
080
081  /**
082   * Initializes a new instance of the MeetingTimeZone class.
083   */
084  public MeetingTimeZone() {
085    super();
086  }
087
088  /**
089   * Initializes a new instance of the MeetingTimeZone class.
090   *
091   * @param name The name of the time zone.
092   */
093  public MeetingTimeZone(String name) {
094    this();
095    this.name = name;
096  }
097
098  /**
099   * Gets the minimum required server version.
100   *
101   * @param reader the reader
102   * @return Earliest Exchange version in which this service object type is
103   * supported.
104   * @throws Exception the exception
105   */
106  @Override
107  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
108      throws Exception {
109    if (reader.getLocalName().equals(XmlElementNames.BaseOffset)) {
110      this.baseOffset = EwsUtilities.getXSDurationToTimeSpan(reader.readElementValue());
111      return true;
112    } else if (reader.getLocalName().equals(XmlElementNames.Standard)) {
113      this.standard = new TimeChange();
114      this.standard.loadFromXml(reader, reader.getLocalName());
115      return true;
116    } else if (reader.getLocalName().equals(XmlElementNames.Daylight)) {
117      this.daylight = new TimeChange();
118      this.daylight.loadFromXml(reader, reader.getLocalName());
119      return true;
120    } else {
121      return false;
122    }
123  }
124
125  /**
126   * Reads the attribute from XML.
127   *
128   * @param reader the reader
129   * @throws Exception the exception
130   */
131  @Override
132  public void readAttributesFromXml(EwsServiceXmlReader reader)
133      throws Exception {
134    this.name = reader.readAttributeValue(XmlAttributeNames.TimeZoneName);
135  }
136
137  /**
138   * Writes the attribute to XML.
139   *
140   * @param writer the writer
141   * @throws ServiceXmlSerializationException the service xml serialization exception
142   */
143  @Override
144  public void writeAttributesToXml(EwsServiceXmlWriter writer)
145      throws ServiceXmlSerializationException {
146    writer.writeAttributeValue(XmlAttributeNames.TimeZoneName, this
147        .getName());
148  }
149
150  /**
151   * Writes the attribute to XML.
152   *
153   * @param writer the writer
154   * @throws Exception the exception
155   */
156  @Override
157  public void writeElementsToXml(EwsServiceXmlWriter writer)
158      throws Exception {
159    if (this.baseOffset != null) {
160      writer.writeElementValue(XmlNamespace.Types,
161          XmlElementNames.BaseOffset, EwsUtilities
162              .getTimeSpanToXSDuration(this.getBaseOffset()));
163    }
164
165    if (this.getStandard() != null) {
166      this.getStandard().writeToXml(writer, XmlElementNames.Standard);
167    }
168
169    if (this.getDaylight() != null) {
170      this.getDaylight().writeToXml(writer, XmlElementNames.Daylight);
171    }
172  }
173
174  /**
175   * Converts this meeting time zone into a TimeZoneInfo structure.
176   *
177   * @return the time zone
178   */
179  public TimeZoneDefinition toTimeZoneInfo() {
180    TimeZoneDefinition result = null;
181
182    try {
183      result = new TimeZoneDefinition();
184      //TimeZone.getTimeZone(this.getName());
185      result.setId(this.getName());
186    } catch (Exception e) {
187      // Could not find a time zone with that Id on the local system.
188      LOG.error(e);
189    }
190
191    // Again, we cannot accurately convert MeetingTimeZone into TimeZoneInfo
192    // because TimeZoneInfo doesn't support absolute date transitions. So if
193    // there is no system time zone that has a matching Id, we return null.
194    return result;
195  }
196
197  /**
198   * Gets  the name of the time zone.
199   *
200   * @return the name
201   */
202  public String getName() {
203    return this.name;
204  }
205
206  /**
207   * Sets the name.
208   *
209   * @param value the new name
210   */
211  public void setName(String value) {
212    if (this.canSetFieldValue(this.name, value)) {
213      this.name = value;
214      this.changed();
215    }
216  }
217
218  /**
219   * Gets the base offset of the time zone from the UTC time zone.
220   *
221   * @return the base offset
222   */
223  public TimeSpan getBaseOffset() {
224    return this.baseOffset;
225  }
226
227  /**
228   * Sets the base offset.
229   *
230   * @param value the new base offset
231   */
232  public void setBaseOffset(TimeSpan value) {
233    if (this.canSetFieldValue(this.name, value)) {
234      this.baseOffset = value;
235      this.changed();
236    }
237  }
238
239  /**
240   * Gets  a TimeChange defining when the time changes to Standard
241   * Time.
242   *
243   * @return the standard
244   */
245  public TimeChange getStandard() {
246    return this.standard;
247  }
248
249  /**
250   * Sets the standard.
251   *
252   * @param value the new standard
253   */
254  public void setStandard(TimeChange value) {
255    if (this.canSetFieldValue(this.standard, value)) {
256      this.standard = value;
257      this.changed();
258    }
259  }
260
261  /**
262   * Gets  a TimeChange defining when the time changes to Daylight
263   * Saving Time.
264   *
265   * @return the daylight
266   */
267  public TimeChange getDaylight() {
268    return this.daylight;
269  }
270
271  /**
272   * Sets the daylight.
273   *
274   * @param value the new daylight
275   */
276  public void setDaylight(TimeChange value) {
277    if (this.canSetFieldValue(this.daylight, value)) {
278      this.daylight = value;
279      this.changed();
280    }
281  }
282
283}