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.complex;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.XmlElementNames;
029import microsoft.exchange.webservices.data.core.enumeration.property.StandardUser;
030import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
032
033/**
034 * Represents a delegate user.
035 */
036public final class DelegateUser extends ComplexProperty {
037
038  /**
039   * The user id.
040   */
041  private UserId userId = new UserId();
042
043  /**
044   * The permissions.
045   */
046  private DelegatePermissions permissions = new DelegatePermissions();
047
048  /**
049   * The receive copies of meeting messages.
050   */
051  private boolean receiveCopiesOfMeetingMessages;
052
053  /**
054   * The view private item.
055   */
056  private boolean viewPrivateItems;
057
058  /**
059   * Initializes a new instance of the <see cref="DelegateUser"/> class.
060   */
061  public DelegateUser() {
062    super();
063    this.receiveCopiesOfMeetingMessages = false;
064    this.viewPrivateItems = false;
065  }
066
067  /**
068   * Initializes a new instance of the <see cref="DelegateUser"/> class.
069   *
070   * @param primarySmtpAddress the primary smtp address
071   */
072  public DelegateUser(String primarySmtpAddress) {
073    this();
074    this.userId.setPrimarySmtpAddress(primarySmtpAddress);
075  }
076
077  /**
078   * Initializes a new instance of the <see cref="DelegateUser"/> class.
079   *
080   * @param standardUser the standard user
081   */
082  public DelegateUser(StandardUser standardUser) {
083    this();
084
085    this.userId.setStandardUser(standardUser);
086  }
087
088  /**
089   * Gets the user Id of the delegate user.
090   *
091   * @return the user id
092   */
093  public UserId getUserId() {
094    return this.userId;
095  }
096
097  /**
098   * Gets the list of delegate user's permissions.
099   *
100   * @return the permissions
101   */
102  public DelegatePermissions getPermissions() {
103    return this.permissions;
104  }
105
106  /**
107   * Gets  a value indicating if the delegate user should receive
108   * copies of meeting request.
109   *
110   * @return the receive copies of meeting messages
111   */
112  public boolean getReceiveCopiesOfMeetingMessages() {
113    return this.receiveCopiesOfMeetingMessages;
114
115  }
116
117  /**
118   * Sets the receive copies of meeting messages.
119   *
120   * @param value the new receive copies of meeting messages
121   */
122  public void setReceiveCopiesOfMeetingMessages(boolean value) {
123    this.receiveCopiesOfMeetingMessages = value;
124  }
125
126  /**
127   * Gets  a value indicating if the delegate user should be
128   * able to view the principal's private item.
129   *
130   * @return the view private item
131   */
132  public boolean getViewPrivateItems() {
133    return this.viewPrivateItems;
134
135  }
136
137  /**
138   * Gets  a value indicating if the delegate user should be able to
139   * view the principal's private item.
140   *
141   * @param value the new view private item
142   */
143  public void setViewPrivateItems(boolean value) {
144
145    this.viewPrivateItems = value;
146  }
147
148  /**
149   * Tries to read element from XML.
150   *
151   * @param reader the reader
152   * @return true, if successful
153   * @throws Exception the exception
154   */
155  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
156      throws Exception {
157    if (reader.getLocalName().equals(XmlElementNames.UserId)) {
158
159      this.userId = new UserId();
160      this.userId.loadFromXml(reader, reader.getLocalName());
161      return true;
162    } else if (reader.getLocalName().equals(XmlElementNames.UserId)) {
163
164      this.permissions.reset();
165      this.permissions.loadFromXml(reader, reader.getLocalName());
166      return true;
167    } else if (reader.getLocalName().equals(
168        XmlElementNames.ReceiveCopiesOfMeetingMessages)) {
169
170      this.receiveCopiesOfMeetingMessages = reader
171          .readElementValue(Boolean.class);
172      return true;
173    } else if (reader.getLocalName().equals(
174        XmlElementNames.ViewPrivateItems)) {
175
176      this.viewPrivateItems = reader.readElementValue(Boolean.class);
177      return true;
178    } else {
179
180      return false;
181    }
182  }
183
184  /**
185   * Writes elements to XML.
186   *
187   * @param writer the writer
188   * @throws Exception the exception
189   */
190  public void writeElementsToXml(EwsServiceXmlWriter writer)
191      throws Exception {
192    this.getUserId().writeToXml(writer, XmlElementNames.UserId);
193    this.getPermissions().writeToXml(writer,
194        XmlElementNames.DelegatePermissions);
195
196    writer.writeElementValue(XmlNamespace.Types,
197        XmlElementNames.ReceiveCopiesOfMeetingMessages,
198        this.receiveCopiesOfMeetingMessages);
199
200    writer.writeElementValue(XmlNamespace.Types,
201        XmlElementNames.ViewPrivateItems, this.viewPrivateItems);
202  }
203
204  /**
205   * Validates this instance.
206   *
207   * @throws ServiceValidationException the service validation exception
208   */
209  protected void internalValidate() throws ServiceValidationException {
210    if (this.getUserId() == null) {
211      throw new ServiceValidationException("The UserId in the DelegateUser hasn't been specified.");
212    } else if (!this.getUserId().isValid()) {
213      throw new ServiceValidationException(
214          "The UserId in the DelegateUser is invalid. The StandardUser, PrimarySmtpAddress or SID property must be set.");
215    }
216  }
217
218  /**
219   * Validates this instance for AddDelegate.
220   *
221   * @throws Exception
222   * @throws ServiceValidationException
223   */
224  protected void validateAddDelegate() throws ServiceValidationException,
225      Exception {
226    {
227      this.permissions.validateAddDelegate();
228    }
229  }
230
231  /**
232   * Validates this instance for UpdateDelegate.
233   */
234  public void validateUpdateDelegate() throws Exception {
235    {
236      this.permissions.validateUpdateDelegate();
237    }
238  }
239}