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    
024    package microsoft.exchange.webservices.data.property.complex.recurrence.range;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
031    import microsoft.exchange.webservices.data.property.complex.recurrence.pattern.Recurrence;
032    
033    import javax.xml.stream.XMLStreamException;
034    
035    import java.text.DateFormat;
036    import java.text.SimpleDateFormat;
037    import java.util.Date;
038    
039    /**
040     * Represents recurrent range with an end date.
041     */
042    public final class EndDateRecurrenceRange extends RecurrenceRange {
043    
044      /**
045       * The end date.
046       */
047      private Date endDate;
048    
049      /**
050       * Initializes a new instance.
051       */
052      public EndDateRecurrenceRange() {
053        super();
054      }
055    
056      /**
057       * Initializes a new instance.
058       *
059       * @param startDate the start date
060       * @param endDate   the end date
061       */
062      public EndDateRecurrenceRange(Date startDate, Date endDate) {
063        super(startDate);
064        this.endDate = endDate;
065      }
066    
067      /**
068       * Gets the name of the XML element.
069       *
070       * @return The name of the XML element
071       */
072      public String getXmlElementName() {
073        return XmlElementNames.EndDateRecurrence;
074      }
075    
076      /**
077       * Setups the recurrence.
078       *
079       * @param recurrence the new up recurrence
080       * @throws Exception the exception
081       */
082      public void setupRecurrence(Recurrence recurrence) throws Exception {
083        super.setupRecurrence(recurrence);
084        recurrence.setEndDate(this.endDate);
085      }
086    
087      /**
088       * Writes the elements to XML.
089       *
090       * @param writer the writer
091       * @throws XMLStreamException the XML stream exception
092       * @throws ServiceXmlSerializationException the service xml serialization exception
093       */
094      public void writeElementsToXml(EwsServiceXmlWriter writer)
095          throws XMLStreamException, ServiceXmlSerializationException {
096        Date d = this.endDate;
097        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
098        String formattedString = df.format(d);
099    
100        super.writeElementsToXml(writer);
101    
102        writer.writeElementValue(XmlNamespace.Types, XmlElementNames.EndDate,
103            formattedString);
104      }
105    
106      /**
107       * Tries to read element from XML.
108       *
109       * @param reader the reader
110       * @return True if element was read
111       * @throws Exception the exception
112       */
113      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
114          throws Exception {
115        if (super.tryReadElementFromXml(reader)) {
116          return true;
117        } else {
118          if (reader.getLocalName().equals(XmlElementNames.EndDate)) {
119    
120            Date temp = reader.readElementValueAsUnspecifiedDate();
121    
122            if (temp != null) {
123              this.endDate = temp;
124            }
125            return true;
126          } else {
127            return false;
128          }
129        }
130      }
131    
132      /**
133       * Gets the end date.
134       *
135       * @return endDate
136       */
137      public Date getEndDate() {
138        return this.endDate;
139      }
140    
141      /**
142       * sets the end date.
143       *
144       * @param value the new end date
145       */
146      public void setEndDate(Date value) {
147        this.canSetFieldValue(this.endDate, value);
148      }
149    
150    }