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;
025    
026    import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
029    import microsoft.exchange.webservices.data.core.EwsUtilities;
030    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
031    import microsoft.exchange.webservices.data.core.XmlElementNames;
032    import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
033    import microsoft.exchange.webservices.data.core.enumeration.property.EmailAddressKey;
034    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
035    import microsoft.exchange.webservices.data.core.enumeration.property.MailboxType;
036    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
037    
038    /**
039     * Represents an entry of an EmailAddressDictionary.
040     */
041    @EditorBrowsable(state = EditorBrowsableState.Never)
042    public final class EmailAddressEntry extends DictionaryEntryProperty<EmailAddressKey> implements
043                                                                                          IComplexPropertyChangedDelegate {
044      // / The email address.
045      /**
046       * The email address.
047       */
048      private EmailAddress emailAddress;
049    
050      /**
051       * Initializes a new instance of the <see cref="EmailAddressEntry"/> class.
052       */
053      protected EmailAddressEntry() {
054        super(EmailAddressKey.class);
055        this.emailAddress = new EmailAddress();
056        this.emailAddress.addOnChangeEvent(this);
057      }
058    
059      /**
060       * Initializes a new instance of the "EmailAddressEntry" class.
061       *
062       * @param key          The key.
063       * @param emailAddress The email address.
064       */
065      protected EmailAddressEntry(EmailAddressKey key,
066          EmailAddress emailAddress) {
067        super(EmailAddressKey.class, key);
068        this.emailAddress = emailAddress;
069      }
070    
071      /**
072       * Reads the attribute from XML.
073       *
074       * @param reader accepts EwsServiceXmlReader
075       * @throws Exception throws Exception
076       */
077      @Override
078      public void readAttributesFromXml(EwsServiceXmlReader reader)
079          throws Exception {
080        super.readAttributesFromXml(reader);
081        this.getEmailAddress().setName(
082            reader.readAttributeValue(XmlAttributeNames.Name));
083        this
084            .getEmailAddress()
085            .setRoutingType(
086                reader
087                    .readAttributeValue(XmlAttributeNames.
088                        RoutingType));
089        String mailboxTypeString = reader
090            .readAttributeValue(XmlAttributeNames.MailboxType);
091        if ((mailboxTypeString != null) && (!mailboxTypeString.isEmpty())) {
092          this.getEmailAddress().setMailboxType(
093              EwsUtilities.parse(MailboxType.class, mailboxTypeString));
094        } else {
095          this.getEmailAddress().setMailboxType(null);
096        }
097      }
098    
099      /**
100       * Reads the text value from XML.
101       *
102       * @param reader accepts EwsServiceXmlReader
103       * @throws Exception the exception
104       */
105      @Override
106      public void readTextValueFromXml(EwsServiceXmlReader reader)
107          throws Exception {
108        this.getEmailAddress().setAddress(reader.readValue());
109      }
110    
111      /**
112       * Writes the attribute to XML.
113       *
114       * @param writer accepts EwsServiceXmlWriter
115       * @throws ServiceXmlSerializationException throws ServiceXmlSerializationException
116       */
117      @Override
118      public void writeAttributesToXml(EwsServiceXmlWriter writer)
119          throws ServiceXmlSerializationException {
120        super.writeAttributesToXml(writer);
121        if (writer.getService().getRequestedServerVersion().ordinal() >
122            ExchangeVersion.Exchange2007_SP1
123                .ordinal()) {
124          writer.writeAttributeValue(XmlAttributeNames.Name, this
125              .getEmailAddress().getName());
126          writer.writeAttributeValue(XmlAttributeNames.RoutingType, this
127              .getEmailAddress().getRoutingType());
128          if (this.getEmailAddress().getMailboxType() != MailboxType.Unknown) {
129            writer.writeAttributeValue(XmlAttributeNames.MailboxType, this
130                .getEmailAddress().getMailboxType());
131          }
132        }
133      }
134    
135      /**
136       * Writes elements to XML.
137       *
138       * @param writer accepts EwsServiceXmlWriter
139       * @throws ServiceXmlSerializationException throws ServiceXmlSerializationException
140       */
141      @Override
142      public void writeElementsToXml(EwsServiceXmlWriter writer)
143          throws ServiceXmlSerializationException {
144        writer.writeValue(this.getEmailAddress().getAddress(),
145            XmlElementNames.EmailAddress);
146      }
147    
148      /**
149       * Gets the e-mail address of the entry.
150       *
151       * @return the email address
152       */
153      public EmailAddress getEmailAddress() {
154        return this.emailAddress;
155        // set { this.SetFieldValue<EmailAddress>(ref this.emailAddress, value);
156        // }
157      }
158    
159      /**
160       * Sets the e-mail address of the entry.
161       *
162       * @param value the new email address
163       */
164      public void setEmailAddress(Object value) {
165        //this.canSetFieldValue((EmailAddress) this.emailAddress, value);
166        if (this.canSetFieldValue(this.emailAddress, value)) {
167          this.emailAddress = (EmailAddress) value;
168        }
169      }
170    
171      /**
172       * E-mail address was changed.
173       *
174       * @param complexProperty the complex property
175       */
176      private void emailAddressChanged(ComplexProperty complexProperty) {
177        this.changed();
178      }
179    
180      /*
181       * (non-Javadoc)
182       *
183       * @see
184       * microsoft.exchange.webservices.ComplexPropertyChangedDelegateInterface
185       * #complexPropertyChanged(microsoft.exchange.webservices.ComplexProperty)
186       */
187      @Override
188      public void complexPropertyChanged(ComplexProperty complexProperty) {
189        this.emailAddressChanged(complexProperty);
190    
191      }
192    
193    }