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.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.property.StandardUser;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
032    
033    import javax.xml.stream.XMLStreamException;
034    
035    /**
036     * Represents the Id of a user.
037     */
038    public class UserId extends ComplexProperty {
039    
040      /**
041       * The s id.
042       */
043      private String sID;
044    
045      /**
046       * The primary smtp address.
047       */
048      private String primarySmtpAddress;
049    
050      /**
051       * The display name.
052       */
053      private String displayName;
054    
055      /**
056       * The standard user.
057       */
058      private StandardUser standardUser;
059    
060      /**
061       * Initializes a new instance.
062       */
063      public UserId() {
064        super();
065      }
066    
067      /**
068       * Initializes a new instance.
069       *
070       * @param primarySmtpAddress the primary smtp address
071       */
072      public UserId(String primarySmtpAddress) {
073    
074        this.primarySmtpAddress = primarySmtpAddress;
075      }
076    
077      /**
078       * Initializes a new instance.
079       *
080       * @param standardUser the standard user
081       */
082      public UserId(StandardUser standardUser) {
083        this();
084        this.standardUser = standardUser;
085      }
086    
087      /**
088       * Determines whether this instance is valid.
089       *
090       * @return true, if this instance is valid. Else, false
091       */
092      protected boolean isValid() {
093        return (this.standardUser != null ||
094            !(this.primarySmtpAddress == null || this.primarySmtpAddress
095                .isEmpty()) || !(this.sID == null ||
096            this.sID.isEmpty()));
097      }
098    
099      /**
100       * Gets the SID of the user.
101       *
102       * @return the sID
103       */
104      public String getSID() {
105        return this.sID;
106      }
107    
108      /**
109       * Sets the sID.
110       *
111       * @param sID the new sID
112       */
113      public void setSID(String sID) {
114        if (this.canSetFieldValue(this.sID, sID)) {
115          this.sID = sID;
116          this.changed();
117        }
118      }
119    
120      /**
121       * Gets the primary SMTP address or the user.
122       *
123       * @return the primary smtp address
124       */
125      public String getPrimarySmtpAddress() {
126        return this.primarySmtpAddress;
127      }
128    
129      /**
130       * Sets the primary smtp address.
131       *
132       * @param primarySmtpAddress the new primary smtp address
133       */
134      public void setPrimarySmtpAddress(String primarySmtpAddress) {
135        if (this.canSetFieldValue(this.primarySmtpAddress, primarySmtpAddress)) {
136          this.primarySmtpAddress = primarySmtpAddress;
137          this.changed();
138        }
139    
140      }
141    
142      /**
143       * Gets the display name of the user.
144       *
145       * @return the display name
146       */
147      public String getDisplayName() {
148        return this.displayName;
149      }
150    
151      /**
152       * Sets the display name.
153       *
154       * @param displayName the new display name
155       */
156      public void setDisplayName(String displayName) {
157        if (this.canSetFieldValue(this.displayName, displayName)) {
158          this.displayName = displayName;
159          this.changed();
160        }
161      }
162    
163      /**
164       * Gets  a value indicating which standard user the user
165       * represents.
166       *
167       * @return the standard user
168       */
169      public StandardUser getstandardUser() {
170        return this.standardUser;
171      }
172    
173      /**
174       * Sets the standard user.
175       *
176       * @param standardUser the new standard user
177       */
178      public void setStandardUser(StandardUser standardUser) {
179        if (this.canSetFieldValue(this.standardUser, standardUser)) {
180          this.standardUser = standardUser;
181          this.changed();
182        }
183      }
184    
185      /**
186       * Implements an implicit conversion between a string representing a
187       * primary SMTP address and UserId.
188       *
189       * @param primarySmtpAddress the primary smtp address
190       * @return A UserId initialized with the specified primary SMTP address
191       */
192      public static UserId getUserId(String primarySmtpAddress) {
193        return new UserId(primarySmtpAddress);
194      }
195    
196      /**
197       * Implements an implicit conversion between StandardUser and UserId.
198       *
199       * @param standardUser the standard user
200       * @return A UserId initialized with the specified standard user value
201       */
202      public static UserId getUserIdFromStandardUser(StandardUser standardUser) {
203        return new UserId(standardUser);
204      }
205    
206      /**
207       * Tries to read element from XML.
208       *
209       * @param reader the reader
210       * @return True if element was read.
211       * @throws Exception the exception
212       */
213      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
214          throws Exception {
215        if (reader.getLocalName().equals(XmlElementNames.SID)) {
216          this.sID = reader.readValue();
217          return true;
218        } else if (reader.getLocalName().equals(
219            XmlElementNames.PrimarySmtpAddress)) {
220          this.primarySmtpAddress = reader.readValue();
221          return true;
222        } else if (reader.getLocalName().equals(XmlElementNames.DisplayName)) {
223          this.displayName = reader.readValue();
224          return true;
225        } else if (reader.getLocalName().equals(
226            XmlElementNames.DistinguishedUser)) {
227          this.standardUser = reader.readValue(StandardUser.class);
228          return true;
229        } else {
230          return false;
231        }
232      }
233    
234      /**
235       * Writes elements to XML.
236       *
237       * @param writer the writer
238       * @throws XMLStreamException the XML stream exception
239       * @throws ServiceXmlSerializationException the service xml serialization exception
240       */
241      public void writeElementsToXml(EwsServiceXmlWriter writer)
242          throws XMLStreamException, ServiceXmlSerializationException {
243        writer.writeElementValue(XmlNamespace.Types, XmlElementNames.SID,
244            this.sID);
245        writer.writeElementValue(XmlNamespace.Types,
246            XmlElementNames.PrimarySmtpAddress, this.primarySmtpAddress);
247        writer.writeElementValue(XmlNamespace.Types,
248            XmlElementNames.DisplayName, this.displayName);
249        writer.writeElementValue(XmlNamespace.Types,
250            XmlElementNames.DistinguishedUser, this.standardUser);
251      }
252    }