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.recurrence.pattern.Recurrence;
032
033import javax.xml.stream.XMLStreamException;
034
035import java.util.Date;
036
037/**
038 * The Class NumberedRecurrenceRange.
039 */
040public final class NumberedRecurrenceRange extends RecurrenceRange {
041
042  /**
043   * The number of occurrences.
044   */
045  private Integer numberOfOccurrences;
046
047  /**
048   * Initializes a new instance.
049   */
050  public NumberedRecurrenceRange() {
051    super();
052  }
053
054  /**
055   * Initializes a new instance.
056   *
057   * @param startDate           the start date
058   * @param numberOfOccurrences the number of occurrences
059   */
060  public NumberedRecurrenceRange(Date startDate,
061      Integer numberOfOccurrences) {
062    super(startDate);
063    this.numberOfOccurrences = numberOfOccurrences;
064  }
065
066  /**
067   * Gets the name of the XML element.
068   *
069   * @return The name of the XML element
070   */
071  public String getXmlElementName() {
072    return XmlElementNames.NumberedRecurrence;
073  }
074
075  /**
076   * Setups the recurrence.
077   *
078   * @param recurrence the new up recurrence
079   * @throws Exception the exception
080   */
081  public void setupRecurrence(Recurrence recurrence) throws Exception {
082    super.setupRecurrence(recurrence);
083    recurrence.setNumberOfOccurrences(this.numberOfOccurrences);
084  }
085
086  /**
087   * Writes the elements to XML..
088   *
089   * @param writer the writer
090   * @throws XMLStreamException the XML stream exception
091   * @throws ServiceXmlSerializationException the service xml serialization exception
092   */
093  public void writeElementsToXml(EwsServiceXmlWriter writer)
094      throws XMLStreamException, ServiceXmlSerializationException {
095    super.writeElementsToXml(writer);
096
097    if (this.numberOfOccurrences != null) {
098      writer.writeElementValue(XmlNamespace.Types,
099          XmlElementNames.NumberOfOccurrences,
100          this.numberOfOccurrences);
101    }
102  }
103
104  /**
105   * Tries to read element from XML.
106   *
107   * @param reader the reader
108   * @return True if element was read
109   * @throws Exception the exception
110   */
111  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
112      throws Exception {
113    if (super.tryReadElementFromXml(reader)) {
114      return true;
115    } else {
116      if (reader.getLocalName().equals(
117          XmlElementNames.NumberOfOccurrences)) {
118        this.numberOfOccurrences = reader
119            .readElementValue(Integer.class);
120        return true;
121      } else {
122        return false;
123      }
124    }
125  }
126
127  /**
128   * Gets the number of occurrences.
129   *
130   * @return numberOfOccurrences
131   */
132
133  public Integer getNumberOfOccurrences() {
134    return this.numberOfOccurrences;
135  }
136
137  /**
138   * sets the number of occurrences.
139   *
140   * @param value the new number of occurrences
141   */
142  public void setNumberOfOccurrences(Integer value) {
143    this.canSetFieldValue(this.numberOfOccurrences, value);
144
145  }
146
147}