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.definition; 025 026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader; 027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter; 028import microsoft.exchange.webservices.data.core.ExchangeService; 029import microsoft.exchange.webservices.data.core.PropertyBag; 030import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; 031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace; 032import microsoft.exchange.webservices.data.core.enumeration.property.PropertyDefinitionFlags; 033import microsoft.exchange.webservices.data.core.service.ServiceObject; 034import microsoft.exchange.webservices.data.misc.OutParam; 035import microsoft.exchange.webservices.data.property.complex.ComplexProperty; 036 037import java.util.EnumSet; 038 039/** 040 * Represents abstract complex property definition. 041 */ 042public abstract class ComplexPropertyDefinitionBase extends PropertyDefinition { 043 044 /** 045 * Initializes a new instance. 046 * 047 * @param xmlElementName Name of the XML element. 048 * @param flags The flags. 049 * @param version The version. 050 */ 051 protected ComplexPropertyDefinitionBase(String xmlElementName, 052 EnumSet<PropertyDefinitionFlags> flags, ExchangeVersion version) { 053 super(xmlElementName, flags, version); 054 } 055 056 /** 057 * Initializes a new instance. 058 * 059 * @param xmlElementName Name of the XML element. 060 * @param uri The URI. 061 * @param version The version. 062 */ 063 protected ComplexPropertyDefinitionBase(String xmlElementName, String uri, 064 ExchangeVersion version) { 065 super(xmlElementName, uri, version); 066 } 067 068 /** 069 * Initializes a new instance. 070 * 071 * @param xmlElementName Name of the XML element. 072 * @param uri The URI. 073 * @param flags The flags. 074 * @param version The version. 075 */ 076 protected ComplexPropertyDefinitionBase(String xmlElementName, String uri, 077 EnumSet<PropertyDefinitionFlags> flags, ExchangeVersion version) { 078 super(xmlElementName, uri, flags, version); 079 } 080 081 /** 082 * Creates the property instance. 083 * 084 * @param owner The owner. 085 * @return ComplexProperty. 086 */ 087 public abstract ComplexProperty createPropertyInstance(ServiceObject owner); 088 089 /** 090 * Internals the load from XML. 091 * 092 * @param reader The reader. 093 * @param propertyBag The property bag. 094 * @throws Exception the exception 095 */ 096 protected void internalLoadFromXml( 097 final EwsServiceXmlReader reader, final PropertyBag propertyBag 098 ) throws Exception { 099 final OutParam<ComplexProperty> complexProperty = new OutParam<ComplexProperty>(); 100 final boolean justCreated = getPropertyInstance(propertyBag, complexProperty); 101 102 if (!justCreated && this.hasFlag(PropertyDefinitionFlags.UpdateCollectionItems, 103 propertyBag.getOwner().getService().getRequestedServerVersion())) { 104 final ComplexProperty c = complexProperty.getParam(); 105 c.updateFromXml(reader, reader.getLocalName()); 106 } else { 107 final ComplexProperty c = complexProperty.getParam(); 108 c.loadFromXml(reader, reader.getLocalName()); 109 } 110 111 propertyBag.setObjectFromPropertyDefinition(this, complexProperty 112 .getParam()); 113 } 114 115 116 117 /** 118 * Gets the property instance. 119 * 120 * @param propertyBag The property bag. 121 * @param complexProperty The property instance. 122 * @return True if the instance is newly created. 123 */ 124 private boolean getPropertyInstance( 125 final PropertyBag propertyBag, final OutParam<ComplexProperty> complexProperty 126 ) { 127 final ServiceObject owner = propertyBag.getOwner(); 128 final ExchangeService service = owner.getService(); 129 130 if (!propertyBag.tryGetValue(this, complexProperty) 131 || !hasFlag(PropertyDefinitionFlags.ReuseInstance, service.getRequestedServerVersion())) { 132 complexProperty.setParam(createPropertyInstance(owner)); 133 return true; 134 } 135 return false; 136 } 137 138 /** 139 * Loads from XML. 140 * 141 * @param reader The reader. 142 * @param propertyBag The property bag. 143 * @throws Exception the exception 144 */ 145 @Override public void loadPropertyValueFromXml(EwsServiceXmlReader reader, PropertyBag propertyBag) throws Exception { 146 reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types, this 147 .getXmlElement()); 148 149 if (!reader.isEmptyElement() || reader.hasAttributes()) { 150 this.internalLoadFromXml(reader, propertyBag); 151 } 152 reader.readEndElementIfNecessary(XmlNamespace.Types, this 153 .getXmlElement()); 154 } 155 156 /** 157 * Writes to XML. 158 * 159 * @param writer The writer. 160 * @param propertyBag The property bag. 161 * @param isUpdateOperation Indicates whether the context is an update operation. 162 * @throws Exception the exception 163 */ 164 @Override public void writePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag, 165 boolean isUpdateOperation) 166 throws Exception { 167 ComplexProperty complexProperty = 168 propertyBag.getObjectFromPropertyDefinition(this); 169 if (complexProperty != null) { 170 complexProperty.writeToXml(writer, this.getXmlElement()); 171 } 172 } 173}