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.recurrence.range;
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;
031import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
032import microsoft.exchange.webservices.data.property.complex.recurrence.pattern.Recurrence;
033
034import javax.xml.stream.XMLStreamException;
035
036import java.text.DateFormat;
037import java.text.SimpleDateFormat;
038import java.util.Date;
039
040/**
041 * Represents recurrence range with start and end dates.
042 */
043public abstract class RecurrenceRange extends ComplexProperty {
044
045  /**
046   * The start date.
047   */
048  private Date startDate;
049
050  /**
051   * The recurrence.
052   */
053  private Recurrence recurrence;
054
055  /**
056   * Initializes a new instance.
057   */
058  protected RecurrenceRange() {
059    super();
060  }
061
062  /**
063   * Initializes a new instance.
064   *
065   * @param startDate the start date
066   */
067  protected RecurrenceRange(Date startDate) {
068    this();
069    this.startDate = startDate;
070  }
071
072  /**
073   * Changes handler.
074   */
075  public void changed() {
076    if (this.recurrence != null) {
077      this.recurrence.changed();
078    }
079  }
080
081  /**
082   * Setup the recurrence.
083   *
084   * @param recurrence the new up recurrence
085   * @throws Exception the exception
086   */
087  public void setupRecurrence(Recurrence recurrence) throws Exception {
088    recurrence.setStartDate(this.getStartDate());
089  }
090
091  /**
092   * Writes elements to XML..
093   *
094   * @param writer the writer
095   * @throws XMLStreamException the XML stream exception
096   * @throws ServiceXmlSerializationException the service xml serialization exception
097   */
098  public void writeElementsToXml(EwsServiceXmlWriter writer)
099      throws XMLStreamException, ServiceXmlSerializationException {
100    Date d = this.startDate;
101    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
102    String formattedString = df.format(d);
103
104    writer.writeElementValue(XmlNamespace.Types, XmlElementNames.StartDate,
105        formattedString);
106  }
107
108  /**
109   * Tries to read element from XML.
110   *
111   * @param reader the reader
112   * @return True if element was read
113   * @throws Exception the exception
114   */
115  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
116      throws Exception {
117    if (reader.getLocalName().equals(XmlElementNames.StartDate)) {
118      //this.startDate = reader.readElementValueAsDateTime();
119      Date startDate = reader.readElementValueAsUnspecifiedDate();
120      if (startDate != null) {
121        this.startDate = startDate;
122        return true;
123      }
124      return false;
125    } else {
126      return false;
127    }
128  }
129
130  /**
131   * Gets the name of the XML element.
132   *
133   * @return recurrence
134   */
135  public abstract String getXmlElementName();
136
137  /**
138   * Gets or sets the recurrence.
139   *
140   * @return recurrence
141   */
142  protected Recurrence getRecurrence() {
143    return this.recurrence;
144  }
145
146  /**
147   * Sets the recurrence.
148   *
149   * @param value the new recurrence
150   */
151  protected void setRecurrence(Recurrence value) {
152    this.recurrence = value;
153  }
154
155  /**
156   * Gets the start date.
157   *
158   * @return startDate
159   */
160  protected Date getStartDate() {
161    return this.startDate;
162
163  }
164
165  /**
166   * Sets the start date.
167   *
168   * @param value the new start date
169   */
170  protected void setStartDate(Date value) {
171    this.canSetFieldValue(this.startDate, value);
172  }
173
174}