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.time;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.XmlElementNames;
029import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
031
032import javax.xml.stream.XMLStreamException;
033
034import java.text.ParseException;
035import java.text.SimpleDateFormat;
036import java.util.Date;
037
038/**
039 * Represents a time zone period transition that occurs on a fixed (absolute)
040 * date.
041 */
042public class AbsoluteDateTransition extends TimeZoneTransition {
043
044  /**
045   * The date time.
046   */
047  private Date dateTime;
048
049  /**
050   * Gets the XML element name associated with the transition.
051   *
052   * @return The XML element name associated with the transition.
053   */
054  @Override
055  protected String getXmlElementName() {
056    return XmlElementNames.AbsoluteDateTransition;
057  }
058
059  /**
060   * Tries to read element from XML.
061   *
062   * @param reader the reader
063   * @return True if element was read.
064   * @throws java.text.ParseException the parse exception
065   * @throws Exception                the exception
066   */
067  @Override
068  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
069      throws ParseException, Exception {
070    boolean result = super.tryReadElementFromXml(reader);
071
072    if (!result) {
073      if (reader.getLocalName().equals(XmlElementNames.DateTime)) {
074        SimpleDateFormat sdfin = new SimpleDateFormat(
075            "yyyy-MM-dd'T'HH:mm:ss");
076        this.dateTime = sdfin.parse(reader.readElementValue());
077
078        result = true;
079      }
080    }
081
082    return result;
083  }
084
085  /**
086   * Writes elements to XML.
087   *
088   * @param writer the writer
089   * @throws ServiceXmlSerializationException the service xml serialization exception
090   * @throws XMLStreamException the XML stream exception
091   */
092  @Override
093  public void writeElementsToXml(EwsServiceXmlWriter writer)
094      throws ServiceXmlSerializationException, XMLStreamException {
095    super.writeElementsToXml(writer);
096
097    writer.writeElementValue(XmlNamespace.Types, XmlElementNames.DateTime,
098        this.dateTime);
099  }
100
101  /**
102   * Initializes a new instance of the AbsoluteDateTransition class.
103   *
104   * @param timeZoneDefinition , The time zone definition the transition will belong to.
105   */
106  protected AbsoluteDateTransition(TimeZoneDefinition timeZoneDefinition) {
107    super(timeZoneDefinition);
108  }
109
110  /**
111   * Initializes a new instance of the AbsoluteDateTransition class.
112   *
113   * @param timeZoneDefinition The time zone definition the transition will belong to.
114   * @param targetGroup        the target group
115   */
116  protected AbsoluteDateTransition(TimeZoneDefinition timeZoneDefinition,
117      TimeZoneTransitionGroup targetGroup) {
118    super(timeZoneDefinition, targetGroup);
119  }
120
121  /**
122   * Gets the absolute date and time when the transition occurs.
123   *
124   * @return the date time
125   */
126  public Date getDateTime() {
127    return dateTime;
128  }
129
130  /**
131   * Sets the date time.
132   *
133   * @param dateTime the new date time
134   */
135  protected void setDateTime(Date dateTime) {
136    this.dateTime = dateTime;
137  }
138}