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.EwsUtilities;
027import microsoft.exchange.webservices.data.core.service.ServiceObject;
028import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
029import microsoft.exchange.webservices.data.core.enumeration.property.PropertyDefinitionFlags;
030import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
031import microsoft.exchange.webservices.data.property.complex.ICreateComplexPropertyDelegate;
032import microsoft.exchange.webservices.data.property.complex.IOwnedProperty;
033
034import java.util.EnumSet;
035
036/**
037 * Represents base complex property type.
038 *
039 * @param <TComplexProperty> The type of the complex property.
040 */
041public class ComplexPropertyDefinition<TComplexProperty extends ComplexProperty>
042    extends ComplexPropertyDefinitionBase {
043
044  private Class<TComplexProperty> instance;
045  /**
046   * The property creation delegate.
047   */
048  private ICreateComplexPropertyDelegate<TComplexProperty> propertyCreationDelegate;
049
050  /**
051   * Initializes a new instance.
052   *
053   * @param xmlElementName           Name of the XML element.
054   * @param flags                    The flags.
055   * @param version                  The version.
056   * @param propertyCreationDelegate Delegate used to create instances of ComplexProperty.
057   */
058  public ComplexPropertyDefinition(
059      Class<TComplexProperty> cls,
060      String xmlElementName,
061      EnumSet<PropertyDefinitionFlags> flags,
062      ExchangeVersion version,
063      ICreateComplexPropertyDelegate<TComplexProperty>
064          propertyCreationDelegate) {
065    super(xmlElementName, flags, version);
066    this.instance = cls;
067    EwsUtilities.ewsAssert(propertyCreationDelegate != null, "ComplexPropertyDefinition ctor",
068                           "CreateComplexPropertyDelegate cannot be null");
069
070    this.propertyCreationDelegate = propertyCreationDelegate;
071  }
072
073  /**
074   * Initializes a new instance.
075   *
076   * @param xmlElementName           Name of the XML element.
077   * @param uri                      The URI.
078   * @param version                  The version.
079   * @param propertyCreationDelegate Delegate used to create instances of ComplexProperty.
080   */
081  public ComplexPropertyDefinition(
082      Class<TComplexProperty> cls,
083      String xmlElementName,
084      String uri,
085      ExchangeVersion version,
086      ICreateComplexPropertyDelegate<TComplexProperty>
087          propertyCreationDelegate) {
088    super(xmlElementName, uri, version);
089    this.instance = cls;
090    this.propertyCreationDelegate = propertyCreationDelegate;
091  }
092
093  public ComplexPropertyDefinition(String xmlElementName, String uri, ExchangeVersion version,
094      ICreateComplexPropertyDelegate<TComplexProperty> propertyCreationDelegate) {
095    super(xmlElementName, uri, version);
096    this.propertyCreationDelegate = propertyCreationDelegate;
097  }
098
099  /**
100   * Instantiates a new complex property definition.
101   *
102   * @param xmlElementName           the xml element name
103   * @param uri                      the uri
104   * @param flags                    the flags
105   * @param version                  the version
106   * @param propertyCreationDelegate the property creation delegate
107   */
108  public ComplexPropertyDefinition(Class<TComplexProperty> cls, String xmlElementName, String uri,
109      EnumSet<PropertyDefinitionFlags> flags, ExchangeVersion version,
110      ICreateComplexPropertyDelegate<TComplexProperty> propertyCreationDelegate) {
111    super(xmlElementName, uri, flags, version);
112    this.instance = cls;
113    this.propertyCreationDelegate = propertyCreationDelegate;
114  }
115
116
117  /**
118   * Instantiates a new complex property definition.
119   *
120   * @param xmlElementName           the xml element name
121   * @param attachments              the attachments
122   * @param flags                    the flags
123   * @param version                  the version
124   * @param propertyCreationDelegate the property creation delegate
125   */
126  public ComplexPropertyDefinition(
127      String attachments,
128      String xmlElementName,
129      ExchangeVersion version,
130      EnumSet<PropertyDefinitionFlags> flags,
131      ICreateComplexPropertyDelegate<TComplexProperty> propertyCreationDelegate) {
132    // TODO Auto-generated constructor stub
133    super(xmlElementName, attachments, flags, version);
134    this.propertyCreationDelegate = propertyCreationDelegate;
135  }
136
137  /**
138   * Creates the property instance.
139   *
140   * @param owner The owner.
141   * @return ComplexProperty instance.
142   */
143  @Override public ComplexProperty createPropertyInstance(ServiceObject owner) {
144    TComplexProperty complexProperty = this.propertyCreationDelegate
145        .createComplexProperty();
146    if (complexProperty instanceof IOwnedProperty) {
147      IOwnedProperty ownedProperty = (IOwnedProperty) complexProperty;
148      ownedProperty.setOwner(owner);
149    }
150    return complexProperty;
151  }
152
153  /**
154   * Gets the property type.
155   */
156  @Override
157  public Class<TComplexProperty> getType() {
158                /*ParameterizedType parameterizedType =
159                (ParameterizedType) getClass().getGenericSuperclass();
160             return (Class) parameterizedType.getActualTypeArguments()[0];
161
162                 instance = ((Class)((ParameterizedType)this.getClass(). 
163                               getGenericSuperclass()).getActualTypeArguments()[0]).
164                               newInstance(); */
165                /*return ((Class)((ParameterizedType)this.getClass().
166                               getGenericSuperclass()).getActualTypeArguments()[0]).
167                               newInstance();*/
168    //return ComplexProperty.class;
169    return this.instance;
170  }
171}