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.enumeration.misc.ExchangeVersion;
030    import microsoft.exchange.webservices.data.core.enumeration.property.PropertyDefinitionFlags;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032    import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
033    import microsoft.exchange.webservices.data.property.complex.ICreateComplexPropertyDelegate;
034    
035    import java.util.EnumSet;
036    
037    /**
038     * Represents contained property definition.
039     *
040     * @param <TComplexProperty> The type of the complex property.
041     */
042    public class ContainedPropertyDefinition<TComplexProperty extends ComplexProperty>
043        extends ComplexPropertyDefinition<TComplexProperty> {
044    
045      /**
046       * The contained xml element name.
047       */
048      private String containedXmlElementName;
049    
050      /**
051       * Initializes a new instance of. ContainedPropertyDefinition
052       *
053       * @param xmlElementName           Name of the XML element.
054       * @param uri                      The URI.
055       * @param containedXmlElementName  Name of the contained XML element.
056       * @param flags                    The flags.
057       * @param version                  The version.
058       * @param propertyCreationDelegate Delegate used to create instances of ComplexProperty.
059       */
060      public ContainedPropertyDefinition(Class<TComplexProperty> cls, String xmlElementName, String uri,
061          String containedXmlElementName, EnumSet<PropertyDefinitionFlags> flags, ExchangeVersion version,
062          ICreateComplexPropertyDelegate<TComplexProperty> propertyCreationDelegate) {
063        super(cls, xmlElementName, uri, flags, version,
064            propertyCreationDelegate);
065        this.containedXmlElementName = containedXmlElementName;
066      }
067    
068      /**
069       * Load from XML.
070       *
071       * @param reader      the reader
072       * @param propertyBag the property bag
073       * @throws Exception the exception
074       */
075      @Override
076      protected void internalLoadFromXml(EwsServiceXmlReader reader,
077          PropertyBag propertyBag) throws Exception {
078        reader.readStartElement(XmlNamespace.Types,
079            this.containedXmlElementName);
080        super.internalLoadFromXml(reader, propertyBag);
081        reader.readEndElementIfNecessary(XmlNamespace.Types,
082            this.containedXmlElementName);
083    
084      }
085    
086      /**
087       * Writes to XML.
088       *
089       * @param writer            the writer
090       * @param propertyBag       the property bag
091       * @param isUpdateOperation the is update operation
092       * @throws Exception the exception
093       */
094      @Override public void writePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag,
095          boolean isUpdateOperation)
096          throws Exception {
097    
098        Object o = propertyBag.getObjectFromPropertyDefinition(this);
099        if (o instanceof ComplexProperty) {
100          ComplexProperty complexProperty = (ComplexProperty) o;
101          writer.writeStartElement(XmlNamespace.Types, this.getXmlElement());
102          complexProperty.writeToXml(writer, this.containedXmlElementName);
103          writer.writeEndElement(); // this.XmlElementName
104        }
105      }
106    }