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.XmlElementNames;
029import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
031import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
032
033import javax.xml.stream.XMLStreamException;
034
035import java.util.Date;
036
037/**
038 * Represents the date and time range within which messages have been received.
039 */
040public final class RulePredicateDateRange extends ComplexProperty {
041
042  /**
043   * The end DateTime.
044   */
045  private Date start;
046
047  /**
048   * The end DateTime.
049   */
050  private Date end;
051
052  /**
053   * Initializes a new instance of the RulePredicateDateRange class.
054   */
055  protected RulePredicateDateRange() {
056    super();
057  }
058
059  /**
060   * Gets or sets the range start date and time.
061   * If Start is set to null, no start date applies.
062   */
063  public Date getStart() {
064    return this.start;
065  }
066
067  public void setStart(Date value) {
068    if (this.canSetFieldValue(this.start, value)) {
069      this.start = value;
070      this.changed();
071    }
072  }
073
074  /**
075   * Gets or sets the range end date and time.
076   * If End is set to null, no end date applies.
077   */
078  public Date getEnd() {
079    return this.end;
080  }
081
082  public void setEnd(Date value) {
083    if (this.canSetFieldValue(this.end, value)) {
084      this.end = value;
085      this.changed();
086    }
087  }
088
089  /**
090   * Tries to read element from XML.
091   *
092   * @param reader The reader.
093   * @return True if element was read.
094   */
095  @Override
096  public boolean tryReadElementFromXml(EwsServiceXmlReader
097      reader) throws Exception {
098    if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.StartDateTime)) {
099      this.start = reader.readElementValueAsDateTime();
100      return true;
101    } else if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.EndDateTime)) {
102      this.end = reader.readElementValueAsDateTime();
103      return true;
104    } else {
105      return false;
106    }
107  }
108
109  /**
110   * Writes elements to XML.
111   *
112   * @param writer the writer
113   * @throws XMLStreamException the XML stream exception
114   */
115  @Override
116  public void writeElementsToXml(EwsServiceXmlWriter writer)
117      throws ServiceXmlSerializationException, XMLStreamException {
118    if (this.getStart() != null) {
119      writer.writeElementValue(XmlNamespace.Types,
120          XmlElementNames.StartDateTime, this.getStart());
121    }
122    if (this.getEnd() != null) {
123      writer.writeElementValue(XmlNamespace.Types,
124          XmlElementNames.EndDateTime, this.getEnd());
125    }
126  }
127
128  /**
129   * Validates this instance.
130   */
131  @Override
132  protected void internalValidate()
133      throws ServiceValidationException, Exception {
134    super.internalValidate();
135    if (this.start != null &&
136        this.end != null &&
137        this.start.after(this.end)) {
138      throw new ServiceValidationException(
139          "Start date time cannot be bigger than end date time.");
140    }
141  }
142}