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.core.response;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.PropertySet;
029import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.core.service.item.Item;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlDeserializationException;
034import microsoft.exchange.webservices.data.search.FindItemsResults;
035import microsoft.exchange.webservices.data.search.GroupedFindItemsResults;
036import microsoft.exchange.webservices.data.search.ItemGroup;
037import microsoft.exchange.webservices.data.security.XmlNodeType;
038
039import javax.xml.stream.XMLStreamException;
040
041import java.util.ArrayList;
042import java.util.List;
043
044/**
045 * Represents the response to a item search operation.
046 *
047 * @param <TItem> The type of item that the opeartion returned.
048 */
049public final class FindItemResponse
050    <TItem extends Item> extends ServiceResponse {
051
052  /**
053   * The results.
054   */
055  private FindItemsResults<TItem> results;
056
057  /**
058   * The is grouped.
059   */
060  private boolean isGrouped;
061
062  /**
063   * The grouped find results.
064   */
065  private GroupedFindItemsResults<TItem> groupedFindResults;
066
067  /**
068   * The property set.
069   */
070  private PropertySet propertySet;
071
072  /**
073   * Initializes a new instance of the FindItemResponse class.
074   *
075   * @param isGrouped   if set to true if grouped.
076   * @param propertySet The property Set
077   */
078  public FindItemResponse(boolean isGrouped, PropertySet propertySet) {
079    super();
080    this.isGrouped = isGrouped;
081    this.propertySet = propertySet;
082
083    EwsUtilities
084        .ewsAssert(this.propertySet != null, "FindItemResponse.ctor", "PropertySet should not be null");
085  }
086
087  /**
088   * Reads response elements from XML.
089   *
090   * @param reader ,The reader
091   * @throws Exception the exception
092   */
093  @Override
094  protected void readElementsFromXml(EwsServiceXmlReader reader)
095      throws Exception {
096    reader.readStartElement(XmlNamespace.Messages,
097        XmlElementNames.RootFolder);
098
099    int totalItemsInView = reader.readAttributeValue(Integer.class,
100        XmlAttributeNames.TotalItemsInView);
101    boolean moreItemsAvailable = !reader.readAttributeValue(Boolean.class,
102        XmlAttributeNames.IncludesLastItemInRange);
103
104    // Ignore IndexedPagingOffset attribute if moreItemsAvailable is false.
105    Integer nextPageOffset = moreItemsAvailable ? reader
106        .readNullableAttributeValue(Integer.class,
107            XmlAttributeNames.IndexedPagingOffset) : null;
108
109    if (!this.isGrouped) {
110      this.results = new FindItemsResults<TItem>();
111      this.results.setTotalCount(totalItemsInView);
112      this.results.setNextPageOffset(nextPageOffset);
113      this.results.setMoreAvailable(moreItemsAvailable);
114      internalReadItemsFromXml(reader, this.propertySet, this.results
115          .getItems());
116    } else {
117      this.groupedFindResults = new GroupedFindItemsResults<TItem>();
118      this.groupedFindResults.setTotalCount(totalItemsInView);
119      this.groupedFindResults.setNextPageOffset(nextPageOffset);
120      this.groupedFindResults.setMoreAvailable(moreItemsAvailable);
121
122      reader.readStartElement(XmlNamespace.Types, XmlElementNames.Groups);
123
124      if (!reader.isEmptyElement()) {
125        do {
126          reader.read();
127
128          if (reader.isStartElement(XmlNamespace.Types,
129              XmlElementNames.GroupedItems)) {
130            String groupIndex = reader.readElementValue(
131                XmlNamespace.Types, XmlElementNames.GroupIndex);
132
133            ArrayList<TItem> itemList = new ArrayList<TItem>();
134            internalReadItemsFromXml(reader, this.propertySet,
135                itemList);
136
137            reader.readEndElement(XmlNamespace.Types,
138                XmlElementNames.GroupedItems);
139
140            this.groupedFindResults.getItemGroups().add(
141                new ItemGroup<TItem>(groupIndex, itemList));
142          }
143        } while (!reader.isEndElement(XmlNamespace.Types,
144            XmlElementNames.Groups));
145      } else {
146        reader.read();
147      }
148    }
149
150    reader
151        .readEndElement(XmlNamespace.Messages,
152            XmlElementNames.RootFolder);
153  }
154
155  /**
156   * Read item from XML.
157   *
158   * @param reader the reader
159   * @param propertySet the property set
160   * @param destinationList the list in which to add the read item
161   * @throws XMLStreamException the XML stream exception
162   * @throws ServiceXmlDeserializationException the service xml deserialization exception
163   * @throws Exception the exception
164   */
165  private void internalReadItemsFromXml(EwsServiceXmlReader reader,
166      PropertySet propertySet, List<TItem> destinationList)
167      throws XMLStreamException, ServiceXmlDeserializationException,
168      Exception {
169    EwsUtilities.ewsAssert(destinationList != null, "FindItemResponse.InternalReadItemsFromXml",
170                           "destinationList is null.");
171
172    reader.readStartElement(XmlNamespace.Types, XmlElementNames.Items);
173    if (!reader.isEmptyElement()) {
174      do {
175        reader.read();
176
177        if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) {
178          Item item = EwsUtilities.createEwsObjectFromXmlElementName(
179              Item.class, reader.getService(), reader
180                  .getLocalName());
181
182          if (item == null) {
183            reader.skipCurrentElement();
184          } else {
185            item.loadFromXml(reader, true, /* clearPropertyBag */
186                propertySet, true /* summaryPropertiesOnly */);
187
188            destinationList.add((TItem) item);
189          }
190        }
191      } while (!reader.isEndElement(XmlNamespace.Types,
192          XmlElementNames.Items));
193    } else {
194      reader.read();
195    }
196
197  }
198
199  /**
200   * Gets a grouped list of item matching the specified search criteria that
201   * were found in Exchange. ItemGroups is null if the search operation did
202   * not specify grouping options.
203   *
204   * @return the grouped find results
205   */
206  public GroupedFindItemsResults<TItem> getGroupedFindResults() {
207    return groupedFindResults;
208  }
209
210  /**
211   * Gets the results of the search operation.
212   *
213   * @return the results
214   */
215  public FindItemsResults<TItem> getResults() {
216    return results;
217  }
218
219}