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.misc;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.XmlAttributeNames;
028import microsoft.exchange.webservices.data.core.XmlElementNames;
029import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030import microsoft.exchange.webservices.data.property.complex.EmailAddress;
031
032import java.util.ArrayList;
033import java.util.Collection;
034import java.util.Iterator;
035
036/**
037 * Represents the results of an ExpandGroup operation.
038 */
039public final class ExpandGroupResults implements Iterable<EmailAddress> {
040
041  /**
042   * True, if all members are returned. EWS always returns true on ExpandDL,
043   * i.e. all members are returned.
044   */
045  private boolean includesAllMembers;
046
047  /**
048   * DL members.
049   */
050  private Collection<EmailAddress> members = new ArrayList<EmailAddress>();
051
052  /**
053   * Initializes a new instance of the class.
054   */
055  public ExpandGroupResults() {
056  }
057
058  /**
059   * Gets the number of members that were returned by the ExpandGroup
060   * operation. Count might be less than the total number of members in the
061   * group, in which case the value of the IncludesAllMembers is false.
062   *
063   * @return the count
064   */
065  public int getCount() {
066    return this.getMembers().size();
067  }
068
069  /**
070   * Gets a value indicating whether all the members of the group have been
071   * returned by ExpandGroup.
072   *
073   * @return the includes all members
074   */
075  public boolean getIncludesAllMembers() {
076    return this.includesAllMembers;
077  }
078
079  /**
080   * Gets the members of the expanded group.
081   *
082   * @return the members
083   */
084  public Collection<EmailAddress> getMembers() {
085    return this.members;
086  }
087
088  /**
089   * Gets the members of the expanded group.
090   *
091   * @param reader the reader
092   * @throws Exception the exception
093   */
094  public void loadFromXml(EwsServiceXmlReader reader) throws Exception {
095    reader.readStartElement(XmlNamespace.Messages,
096        XmlElementNames.DLExpansion);
097    if (!reader.isEmptyElement()) {
098      int totalItemsInView = reader.readAttributeValue(Integer.class,
099          XmlAttributeNames.TotalItemsInView);
100      this.includesAllMembers = reader.readAttributeValue(Boolean.class,
101          XmlAttributeNames.IncludesLastItemInRange);
102
103      for (int i = 0; i < totalItemsInView; i++) {
104        EmailAddress emailAddress = new EmailAddress();
105
106        reader.readStartElement(XmlNamespace.Types,
107            XmlElementNames.Mailbox);
108        emailAddress.loadFromXml(reader, XmlElementNames.Mailbox);
109
110        this.getMembers().add(emailAddress);
111      }
112
113      reader.readEndElement(XmlNamespace.Messages,
114          XmlElementNames.DLExpansion);
115    } else {
116      reader.read();
117    }
118  }
119
120  /**
121   * Returns an iterator over a set of elements of type T.
122   *
123   * @return an Iterator.
124   */
125  @Override
126  public Iterator<EmailAddress> iterator() {
127
128    return members.iterator();
129  }
130}