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.EwsUtilities;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.service.folder.CalendarFolder;
031import microsoft.exchange.webservices.data.core.service.folder.Folder;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
034import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.Iterator;
041
042/**
043 * Represents a collection of folder permissions.
044 */
045public final class FolderPermissionCollection extends ComplexPropertyCollection<FolderPermission> {
046
047  private static final Log LOG = LogFactory.getLog(FolderPermissionCollection.class);
048
049  /**
050   * The is calendar folder.
051   */
052  private boolean isCalendarFolder;
053
054  /**
055   * The unknown entries.
056   */
057  private Collection<String> unknownEntries = new ArrayList<String>();
058
059  /**
060   * Initializes a new instance of the FolderPermissionCollection class.
061   *
062   * @param owner the owner
063   */
064  public FolderPermissionCollection(Folder owner) {
065    super();
066    this.isCalendarFolder = owner instanceof CalendarFolder;
067  }
068
069  /**
070   * Gets the name of the inner collection XML element.
071   *
072   * @return the inner collection xml element name
073   */
074  private String getInnerCollectionXmlElementName() {
075    return this.isCalendarFolder ? XmlElementNames.CalendarPermissions :
076        XmlElementNames.Permissions;
077  }
078
079  /**
080   * Gets the name of the collection item XML element.
081   *
082   * @return the collection item xml element name
083   */
084  private String getCollectionItemXmlElementName() {
085    return this.isCalendarFolder ? XmlElementNames.CalendarPermission :
086        XmlElementNames.Permission;
087  }
088
089  /**
090   * Gets the name of the collection item XML element.
091   *
092   * @param complexProperty the complex property
093   * @return the collection item xml element name
094   */
095  @Override
096  protected String getCollectionItemXmlElementName(
097      FolderPermission complexProperty) {
098    return this.getCollectionItemXmlElementName();
099  }
100
101  /**
102   * Loads from XML.
103   *
104   * @param reader           the reader
105   * @param localElementName the local element name
106   * @throws Exception the exception
107   */
108  @Override public void loadFromXml(EwsServiceXmlReader reader, String localElementName) throws Exception {
109    reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
110        localElementName);
111
112    reader.readStartElement(XmlNamespace.Types, this
113        .getInnerCollectionXmlElementName());
114    super.loadFromXml(reader, this.getInnerCollectionXmlElementName());
115    reader.readEndElementIfNecessary(XmlNamespace.Types, this
116        .getInnerCollectionXmlElementName());
117
118    reader.read();
119
120    if (reader.isStartElement(XmlNamespace.Types,
121        XmlElementNames.UnknownEntries)) {
122      do {
123        reader.read();
124
125        if (reader.isStartElement(XmlNamespace.Types,
126            XmlElementNames.UnknownEntry)) {
127          this.unknownEntries.add(reader.readElementValue());
128        }
129      } while (!reader.isEndElement(XmlNamespace.Types,
130          XmlElementNames.UnknownEntries));
131    }
132  }
133
134  /**
135   * Validates this instance.
136   */
137  public void validate() {
138    for (int permissionIndex = 0; permissionIndex < this.getItems().size(); permissionIndex++) {
139      FolderPermission permission = this.getItems().get(permissionIndex);
140      try {
141        permission.validate(this.isCalendarFolder, permissionIndex);
142      } catch (ServiceValidationException e) {
143        LOG.error(e);
144      } catch (ServiceLocalException e) {
145        LOG.error(e);
146      }
147    }
148  }
149
150  /**
151   * Writes the elements to XML.
152   *
153   * @param writer the writer
154   * @throws Exception the exception
155   */
156  @Override
157  public void writeElementsToXml(EwsServiceXmlWriter writer)
158      throws Exception {
159    writer.writeStartElement(XmlNamespace.Types, this
160        .getInnerCollectionXmlElementName());
161    for (FolderPermission folderPermission : this) {
162      folderPermission.writeToXml(writer, this
163              .getCollectionItemXmlElementName(folderPermission),
164          this.isCalendarFolder);
165    }
166    writer.writeEndElement(); // this.InnerCollectionXmlElementName
167  }
168
169  /**
170   * Creates the complex property.
171   *
172   * @param xmlElementName the xml element name
173   * @return FolderPermission instance.
174   */
175  @Override
176  protected FolderPermission createComplexProperty(String xmlElementName) {
177    return new FolderPermission();
178  }
179
180  /**
181   * Adds a permission to the collection.
182   *
183   * @param permission the permission
184   */
185  public void add(FolderPermission permission) {
186    this.internalAdd(permission);
187  }
188
189  /**
190   * Adds the specified permissions to the collection.
191   *
192   * @param permissions the permissions
193   * @throws Exception the exception
194   */
195  public void addFolderRange(Iterator<FolderPermission> permissions)
196      throws Exception {
197    EwsUtilities.validateParam(permissions, "permissions");
198
199    if (null != permissions) {
200      while (permissions.hasNext()) {
201        this.add(permissions.next());
202      }
203    }
204  }
205
206  /**
207   * Clears this collection.
208   */
209  public void clear() {
210    this.internalClear();
211  }
212
213  /**
214   * Removes a permission from the collection.
215   *
216   * @param permission the permission
217   * @return True if the folder permission was successfully removed from the
218   * collection, false otherwise.
219   */
220  public boolean remove(FolderPermission permission) {
221    return this.internalRemove(permission);
222  }
223
224  /**
225   * Removes a permission from the collection.
226   *
227   * @param index the index
228   */
229  public void removeAt(int index) {
230    this.internalRemoveAt(index);
231  }
232
233  /**
234   * Gets a list of unknown user Ids in the collection.
235   *
236   * @return the unknown entries
237   */
238  public Collection<String> getUnknownEntries() {
239    return this.unknownEntries;
240  }
241}