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.folder.Folder;
032import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
033import microsoft.exchange.webservices.data.search.FindFoldersResults;
034import microsoft.exchange.webservices.data.security.XmlNodeType;
035
036/**
037 * Represents the response to a folder search operation.
038 */
039public final class FindFolderResponse extends ServiceResponse {
040
041  /**
042   * The results.
043   */
044  private FindFoldersResults results = new FindFoldersResults();
045
046  /**
047   * The property set.
048   */
049  private PropertySet propertySet;
050
051  /**
052   * Reads response elements from XML.
053   *
054   * @param reader The reader
055   * @throws Exception the exception
056   */
057  @Override
058  protected void readElementsFromXml(EwsServiceXmlReader reader)
059      throws Exception {
060    reader.readStartElement(XmlNamespace.Messages,
061        XmlElementNames.RootFolder);
062
063    this.results.setTotalCount(reader.readAttributeValue(Integer.class,
064        XmlAttributeNames.TotalItemsInView));
065    this.results.setMoreAvailable(!reader.readAttributeValue(Boolean.class,
066        XmlAttributeNames.IncludesLastItemInRange));
067
068    // Ignore IndexedPagingOffset attribute if MoreAvailable is false.
069    this.results.setNextPageOffset(results.isMoreAvailable() ? reader
070        .readNullableAttributeValue(Integer.class,
071            XmlAttributeNames.IndexedPagingOffset) : null);
072
073    reader.readStartElement(XmlNamespace.Types, XmlElementNames.Folders);
074    if (!reader.isEmptyElement()) {
075      do {
076        reader.read();
077
078        if (reader.getNodeType().nodeType == XmlNodeType.START_ELEMENT) {
079          Folder folder = EwsUtilities
080              .createEwsObjectFromXmlElementName(Folder.class, reader.getService(), reader.getLocalName());
081
082          if (folder == null) {
083            reader.skipCurrentElement();
084          } else {
085            folder.loadFromXml(reader, true, /* clearPropertyBag */
086                this.propertySet, true /* summaryPropertiesOnly */);
087
088            this.results.getFolders().add(folder);
089          }
090        }
091      } while (!reader.isEndElement(XmlNamespace.Types,
092          XmlElementNames.Folders));
093    } else {
094      reader.read();
095    }
096
097    reader
098        .readEndElement(XmlNamespace.Messages,
099            XmlElementNames.RootFolder);
100  }
101
102  /**
103   * Initializes a new instance of the FindFolderResponse class.
104   *
105   * @param propertySet The property set from, the request.
106   */
107  public FindFolderResponse(PropertySet propertySet) {
108    super();
109    this.propertySet = propertySet;
110
111    EwsUtilities.ewsAssert(this.propertySet != null, "FindFolderResponse.ctor",
112                           "PropertySet should not be null");
113  }
114
115  /**
116   * Gets the results of the search operation.
117   *
118   * @return the results
119   */
120  public FindFoldersResults getResults() {
121    return this.results;
122  }
123
124}