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.misc.id;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.EwsUtilities;
029    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030    import microsoft.exchange.webservices.data.core.XmlElementNames;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.IdFormat;
032    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
033    
034    /**
035     * Represents an Id expressed in a specific format.
036     */
037    public class AlternateId extends AlternateIdBase {
038    
039      /**
040       * Name of schema type used for AlternateId.
041       */
042      public final static String SchemaTypeName = "AlternateIdType";
043    
044      /**
045       * Id.
046       */
047      private String id;
048    
049      /**
050       * SMTP address of the mailbox that the id belongs to.
051       */
052      private String mailbox;
053    
054      /**
055       * Type (primary or archive) mailbox to which the Id belongs
056       */
057      private boolean isArchive;
058    
059      /**
060       * Initializes a new instance of the class.
061       */
062      public AlternateId() {
063        super();
064      }
065    
066      /**
067       * Initializes a new instance of the class.
068       *
069       * @param format  the format
070       * @param id      the id
071       * @param mailbox the mailbox
072       */
073      public AlternateId(IdFormat format, String id, String mailbox) {
074        super(format);
075        this.setUniqueId(id);
076        this.setMailbox(mailbox);
077      }
078    
079      /**
080       * Initializes a new instance of the AlternateId class.
081       *
082       * @param format    The format the Id is expressed in.
083       * @param id        The Id.
084       * @param mailbox   The SMTP address of the mailbox that the Id belongs to.
085       * @param isArchive Primary (false) or archive (true) mailbox.
086       */
087      public AlternateId(
088          IdFormat format,
089          String id,
090          String mailbox,
091          boolean isArchive) {
092        super(format);
093        this.setUniqueId(id);
094        this.setMailbox(mailbox);
095        this.setIsArchive(isArchive);
096      }
097    
098      /**
099       * Gets the Id.
100       *
101       * @return the unique id
102       */
103      public String getUniqueId() {
104        return this.id;
105      }
106    
107      /**
108       * Sets the unique id.
109       *
110       * @param id the new unique id
111       */
112      public void setUniqueId(String id) {
113        this.id = id;
114      }
115    
116      /**
117       * Gets the mailbox to which the Id belongs.
118       *
119       * @return the mailbox
120       */
121      public String getMailbox() {
122        return this.mailbox;
123      }
124    
125      /**
126       * Sets the mailbox.
127       *
128       * @param mailbox the new mailbox
129       */
130      public void setMailbox(String mailbox) {
131        this.mailbox = mailbox;
132      }
133    
134      /**
135       * Gets the type (primary or archive) mailbox to which the Id belongs.
136       */
137      public boolean getIsArchive() {
138        return this.isArchive;
139      }
140    
141      /**
142       * Sets the type (primary or archive) mailbox to which the Id belongs.
143       *
144       * @param isArchive the new isArchive
145       */
146      public void setIsArchive(boolean isArchive) {
147        this.isArchive = isArchive;
148      }
149    
150      /**
151       * Gets the name of the XML element.
152       *
153       * @return XML element name.
154       */
155      @Override
156      protected String getXmlElementName() {
157        return XmlElementNames.AlternateId;
158      }
159    
160      /**
161       * Gets the name of the XML element.
162       *
163       * @param writer the writer
164       * @throws ServiceXmlSerializationException the service xml serialization exception
165       */
166      @Override
167      protected void writeAttributesToXml(EwsServiceXmlWriter writer)
168          throws ServiceXmlSerializationException {
169        super.writeAttributesToXml(writer);
170        writer.writeAttributeValue(XmlAttributeNames.Id, this.getUniqueId());
171        writer.writeAttributeValue(XmlAttributeNames.Mailbox,
172            this.getMailbox());
173        //.getMailbox() == null || this.getMailbox().isEmpty()) ? ""
174        //: this.getMailbox());
175        if (this.getIsArchive()) {
176          writer.writeAttributeValue(XmlAttributeNames.IsArchive, true);
177        }
178    
179      }
180    
181      /**
182       * Gets the name of the XML element.
183       *
184       * @param reader the reader
185       * @throws Exception// the exception
186       */
187      @Override public void loadAttributesFromXml(EwsServiceXmlReader reader)
188          throws Exception {
189        super.loadAttributesFromXml(reader);
190    
191        this.setUniqueId(reader.readAttributeValue(XmlAttributeNames.Id));
192        this.setMailbox(reader.readAttributeValue(XmlAttributeNames.Mailbox));
193        String isArchive = reader.readAttributeValue(
194            XmlAttributeNames.IsArchive);
195    
196        if (!(isArchive == null || isArchive.isEmpty())) {
197          this.isArchive = reader.readAttributeValue(Boolean.class,
198              XmlAttributeNames.IsArchive);
199        } else {
200          this.isArchive = false;
201        }
202      }
203    
204      /**
205       * Validate this instance.
206       */
207      @Override
208      protected void internalValidate() throws Exception {
209        EwsUtilities.validateParam(this.getMailbox(), "mailbox");
210      }
211    }
212    
213