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.definition;
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.PropertyBag;
030import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
031import microsoft.exchange.webservices.data.core.enumeration.property.PropertyDefinitionFlags;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.util.DateTimeUtils;
034
035import java.util.Date;
036import java.util.EnumSet;
037
038/**
039 * Represents DateTime property definition.
040 */
041public class DateTimePropertyDefinition extends PropertyDefinition {
042
043  /**
044   * The is nullable.
045   */
046  private boolean isNullable;
047
048  /**
049   * Initializes a new instance of the DateTimePropertyDefinition class.
050   *
051   * @param xmlElementName the xml element name
052   * @param uri            the uri
053   * @param version        the version
054   */
055  public DateTimePropertyDefinition(String xmlElementName, String uri, ExchangeVersion version) {
056    super(xmlElementName, uri, version);
057  }
058
059  /**
060   * Initializes a new instance of the DateTimePropertyDefinition class.
061   *
062   * @param xmlElementName the xml element name
063   * @param uri            the uri
064   * @param flags          the flags
065   * @param version        the version
066   */
067  public DateTimePropertyDefinition(String xmlElementName, String uri, EnumSet<PropertyDefinitionFlags> flags,
068      ExchangeVersion version) {
069    super(xmlElementName, uri, flags, version);
070  }
071
072  /**
073   * Initializes a new instance of the DateTimePropertyDefinition class.
074   *
075   * @param xmlElementName the xml element name
076   * @param uri            the uri
077   * @param flags          the flags
078   * @param version        the version
079   * @param isNullable     the is nullable
080   */
081  public DateTimePropertyDefinition(String xmlElementName, String uri, EnumSet<PropertyDefinitionFlags> flags,
082      ExchangeVersion version, boolean isNullable) {
083    super(xmlElementName, uri, flags, version);
084    this.isNullable = isNullable;
085  }
086
087  /**
088   * Loads from XML.
089   *
090   * @param reader      the reader
091   * @param propertyBag the property bag
092   * @throws Exception the exception
093   */
094  public void loadPropertyValueFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag)
095      throws Exception {
096    String value = reader.readElementValue(XmlNamespace.Types, getXmlElement());
097    propertyBag.setObjectFromPropertyDefinition(this, DateTimeUtils.convertDateTimeStringToDate(value));
098  }
099
100
101  /**
102   * Writes the property value to XML.
103   *
104   * @param writer            accepts EwsServiceXmlWriter
105   * @param propertyBag       accepts PropertyBag
106   * @param isUpdateOperation accepts boolean whether the context is an update operation.
107   * @throws Exception throws Exception
108   */
109  public void writePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag,
110      boolean isUpdateOperation)
111      throws Exception {
112    Object value = propertyBag.getObjectFromPropertyDefinition(this);
113
114    if (value != null) {
115      writer.writeStartElement(XmlNamespace.Types, getXmlElement());
116      // No need of changing the date time zone to UTC as Java takes
117      // default timezone as UTC
118      Date dateTime = (Date) value;
119      writer.writeValue(EwsUtilities.dateTimeToXSDateTime(dateTime),
120          getName());
121
122      writer.writeEndElement();
123    }
124  }
125
126  /**
127   * Gets a value indicating whether this property definition is for a
128   * nullable type (ref, int?, bool?...).
129   *
130   * @return true, if is nullable
131   */
132  public boolean isNullable() {
133    return isNullable;
134  }
135
136  /**
137   * Gets the property type.
138   */
139  @Override
140  public Class<Date> getType() {
141    return Date.class;
142
143  }
144}