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.search;
025
026import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.PropertySet;
029import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.request.ServiceRequestBase;
031import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
032import microsoft.exchange.webservices.data.core.enumeration.service.ServiceObjectType;
033import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
034import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
035import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
036import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
037
038import javax.xml.stream.XMLStreamException;
039
040/**
041 * Represents the base view class for search operations.
042 */
043@EditorBrowsable(state = EditorBrowsableState.Never)
044public abstract class ViewBase {
045
046  /**
047   * The property set.
048   */
049  private PropertySet propertySet;
050
051  /**
052   * Initializes a new instance of the "ViewBase" class.
053   */
054  ViewBase() {
055  }
056
057  /**
058   * Validates this view.
059   *
060   * @param request The request using this view.
061   * @throws ServiceValidationException the service validation exception
062   * @throws ServiceVersionException    the service version exception
063   */
064  public void internalValidate(ServiceRequestBase request)
065      throws ServiceValidationException, ServiceVersionException {
066    if (this.getPropertySet() != null) {
067      this.getPropertySet().internalValidate();
068      this.getPropertySet().validateForRequest(
069          request,
070          true /* summaryPropertiesOnly */);
071    }
072  }
073
074  /**
075   * Writes this view to XML.
076   *
077   * @param writer The writer
078   * @throws ServiceXmlSerializationException the service xml serialization exception
079   * @throws Exception                        the exception
080   */
081  protected void internalWriteViewToXml(EwsServiceXmlWriter writer)
082      throws ServiceXmlSerializationException, Exception {
083    Integer maxEntriesReturned = this.getMaxEntriesReturned();
084
085    if (maxEntriesReturned != null) {
086      writer.writeAttributeValue(XmlAttributeNames.MaxEntriesReturned,
087          maxEntriesReturned);
088    }
089  }
090
091  /**
092   * Writes the search settings to XML.
093   *
094   * @param writer  the writer
095   * @param groupBy the group by clause
096   * @throws XMLStreamException the XML stream exception
097   * @throws ServiceXmlSerializationException the service xml serialization exception
098   */
099  protected abstract void internalWriteSearchSettingsToXml(
100      EwsServiceXmlWriter writer, Grouping groupBy)
101      throws XMLStreamException, ServiceXmlSerializationException;
102
103  /**
104   * Writes OrderBy property to XML.
105   *
106   * @param writer the writer
107   * @throws XMLStreamException the XML stream exception
108   * @throws ServiceXmlSerializationException the service xml serialization exception
109   */
110  public abstract void writeOrderByToXml(EwsServiceXmlWriter writer)
111      throws XMLStreamException, ServiceXmlSerializationException;
112
113  /**
114   * Gets the name of the view XML element.
115   *
116   * @return TheXml Element name
117   */
118  protected abstract String getViewXmlElementName();
119
120  /**
121   * Gets the maximum number of item or folder the search operation should
122   * return.
123   *
124   * @return The maximum number of item or folder that should be returned by
125   * the search operation.
126   */
127  protected abstract Integer getMaxEntriesReturned();
128
129  /**
130   * Gets the type of service object this view applies to.
131   *
132   * @return A ServiceObjectType value.
133   */
134  protected abstract ServiceObjectType getServiceObjectType();
135
136  /**
137   * Writes the attribute to XML.
138   *
139   * @param writer The writer.
140   * @throws ServiceXmlSerializationException the service xml serialization exception
141   */
142  public abstract void writeAttributesToXml(EwsServiceXmlWriter writer)
143      throws ServiceXmlSerializationException;
144
145  /**
146   * Writes to XML.
147   *
148   * @param writer  The writer.
149   * @param groupBy The group by clause.
150   * @throws Exception the exception
151   */
152  public void writeToXml(EwsServiceXmlWriter writer, Grouping groupBy)
153      throws Exception {
154    this.getPropertySetOrDefault().writeToXml(writer,
155        this.getServiceObjectType());
156    writer.writeStartElement(XmlNamespace.Messages, this
157        .getViewXmlElementName());
158    this.internalWriteViewToXml(writer);
159    writer.writeEndElement(); // this.GetViewXmlElementName()
160    this.internalWriteSearchSettingsToXml(writer, groupBy);
161  }
162
163  /**
164   * Gets the property set or the default.
165   *
166   * @return PropertySet
167   */
168  public PropertySet getPropertySetOrDefault() {
169    if (this.getPropertySet() == null) {
170      return PropertySet.getFirstClassProperties();
171    } else {
172      return this.getPropertySet();
173    }
174  }
175
176  /**
177   * Gets the property set. PropertySet determines which property will be
178   * loaded on found item. If PropertySet is null, all first class property
179   * are loaded on found item.
180   *
181   * @return the property set
182   */
183  public PropertySet getPropertySet() {
184    return propertySet;
185  }
186
187  /**
188   * Sets the property set. PropertySet determines which property will be
189   * loaded on found item. If PropertySet is null, all first class property
190   * are loaded on found item.
191   *
192   * @param propertySet The property set
193   */
194  public void setPropertySet(PropertySet propertySet) {
195    this.propertySet = propertySet;
196  }
197
198}