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.definition;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.PropertyBag;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
031    import microsoft.exchange.webservices.data.core.enumeration.property.PropertyDefinitionFlags;
032    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlDeserializationException;
034    import microsoft.exchange.webservices.data.property.complex.recurrence.pattern.Recurrence;
035    import microsoft.exchange.webservices.data.property.complex.recurrence.range.EndDateRecurrenceRange;
036    import microsoft.exchange.webservices.data.property.complex.recurrence.range.NoEndRecurrenceRange;
037    import microsoft.exchange.webservices.data.property.complex.recurrence.range.NumberedRecurrenceRange;
038    import microsoft.exchange.webservices.data.property.complex.recurrence.range.RecurrenceRange;
039    import microsoft.exchange.webservices.data.security.XmlNodeType;
040    
041    import java.util.EnumSet;
042    
043    /**
044     * Represenrs recurrence property definition.
045     */
046    public class RecurrencePropertyDefinition extends PropertyDefinition {
047    
048      /**
049       * Initializes a new instance of the RecurrencePropertyDefinition class.
050       *
051       * @param xmlElementName the xml element name
052       * @param uri            the uri
053       * @param flags          the flags
054       * @param version        the version
055       */
056      public RecurrencePropertyDefinition(String xmlElementName, String uri,
057          EnumSet<PropertyDefinitionFlags> flags, ExchangeVersion version) {
058    
059        super(xmlElementName, uri, flags, version);
060    
061      }
062    
063      /**
064       * Loads from XML.
065       *
066       * @param reader      the reader
067       * @param propertyBag the property bag
068       * @throws Exception the exception
069       */
070      public void loadPropertyValueFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag) throws Exception {
071        reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
072            XmlElementNames.Recurrence);
073    
074        Recurrence recurrence = null;
075    
076        reader.read(new XmlNodeType(XmlNodeType.START_ELEMENT)); // This is the
077        // pattern
078        // element
079    
080        if (reader.getLocalName().equals(
081            XmlElementNames.RelativeYearlyRecurrence)) {
082    
083          recurrence = new Recurrence.RelativeYearlyPattern();
084        } else if (reader.getLocalName().equals(
085            XmlElementNames.AbsoluteYearlyRecurrence)) {
086    
087          recurrence = new Recurrence.YearlyPattern();
088        } else if (reader.getLocalName().equals(
089            XmlElementNames.RelativeMonthlyRecurrence)) {
090    
091          recurrence = new Recurrence.RelativeMonthlyPattern();
092        } else if (reader.getLocalName().equals(
093            XmlElementNames.AbsoluteMonthlyRecurrence)) {
094    
095          recurrence = new Recurrence.MonthlyPattern();
096        } else if (reader.getLocalName()
097            .equals(XmlElementNames.DailyRecurrence)) {
098    
099          recurrence = new Recurrence.DailyPattern();
100        } else if (reader.getLocalName().equals(
101            XmlElementNames.DailyRegeneration)) {
102    
103          recurrence = new Recurrence.DailyRegenerationPattern();
104        } else if (reader.getLocalName().equals(
105            XmlElementNames.WeeklyRecurrence)) {
106    
107          recurrence = new Recurrence.WeeklyPattern();
108        } else if (reader.getLocalName().equals(
109            XmlElementNames.WeeklyRegeneration)) {
110    
111          recurrence = new Recurrence.WeeklyRegenerationPattern();
112        } else if (reader.getLocalName().equals(
113            XmlElementNames.MonthlyRegeneration)) {
114    
115          recurrence = new Recurrence.MonthlyRegenerationPattern();
116        } else if (reader.getLocalName().equals(
117            XmlElementNames.YearlyRegeneration)) {
118    
119          recurrence = new Recurrence.YearlyRegenerationPattern();
120        } else {
121    
122          throw new ServiceXmlDeserializationException(String.format("Invalid recurrence pattern: (%s).", reader.getLocalName()));
123        }
124    
125        recurrence.loadFromXml(reader, reader.getLocalName());
126    
127        reader.read(new XmlNodeType(XmlNodeType.START_ELEMENT)); // This is the
128        // range
129        // element
130    
131        RecurrenceRange range;
132    
133        if (reader.getLocalName().equals(XmlElementNames.NoEndRecurrence)) {
134    
135          range = new NoEndRecurrenceRange();
136        } else if (reader.getLocalName().equals(
137            XmlElementNames.EndDateRecurrence)) {
138    
139          range = new EndDateRecurrenceRange();
140        } else if (reader.getLocalName().equals(
141            XmlElementNames.NumberedRecurrence)) {
142    
143          range = new NumberedRecurrenceRange();
144        } else {
145          throw new ServiceXmlDeserializationException(String.format("Invalid recurrence range: (%s).", reader.getLocalName()));
146        }
147    
148        range.loadFromXml(reader, reader.getLocalName());
149        range.setupRecurrence(recurrence);
150    
151        reader.readEndElementIfNecessary(XmlNamespace.Types,
152            XmlElementNames.Recurrence);
153    
154        propertyBag.setObjectFromPropertyDefinition(this, recurrence);
155      }
156    
157      /**
158       * Writes to XML.
159       *
160       * @param writer            the writer
161       * @param propertyBag       the property bag
162       * @param isUpdateOperation the is update operation
163       * @throws Exception the exception
164       */
165      public void writePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag,
166          boolean isUpdateOperation)
167          throws Exception {
168        Recurrence value = propertyBag.getObjectFromPropertyDefinition(this);
169    
170        if (value != null) {
171          value.writeToXml(writer, XmlElementNames.Recurrence);
172        }
173      }
174    
175      /**
176       * Gets the property type.
177       */
178      @Override
179      public Class<Recurrence> getType() {
180        return Recurrence.class;
181      }
182    
183    }