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    
024    package microsoft.exchange.webservices.data.search;
025    
026    import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
029    import microsoft.exchange.webservices.data.core.request.ServiceRequestBase;
030    import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
031    import microsoft.exchange.webservices.data.core.enumeration.search.OffsetBasePoint;
032    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
033    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
034    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
035    
036    import javax.xml.stream.XMLStreamException;
037    
038    /**
039     * Represents a view settings that support paging in a search operation.
040     */
041    @EditorBrowsable(state = EditorBrowsableState.Never)
042    public abstract class PagedView extends ViewBase {
043    
044      /**
045       * The page size.
046       */
047      private int pageSize;
048    
049      /**
050       * The offset base point.
051       */
052      private OffsetBasePoint offsetBasePoint = OffsetBasePoint.Beginning;
053    
054      /**
055       * The offset.
056       */
057      private int offset;
058    
059      /**
060       * Write to XML.
061       *
062       * @param writer The Writer
063       * @throws Exception the exception
064       */
065      @Override
066      protected void internalWriteViewToXml(EwsServiceXmlWriter writer)
067          throws Exception {
068        super.internalWriteViewToXml(writer);
069    
070        writer.writeAttributeValue(XmlAttributeNames.Offset, this.getOffset());
071        writer.writeAttributeValue(XmlAttributeNames.BasePoint, this
072            .getOffsetBasePoint());
073      }
074    
075      /**
076       * Gets the maximum number of item or folder the search operation should
077       * return.
078       *
079       * @return The maximum number of item or folder that should be returned by
080       * the search operation.
081       */
082      @Override
083      protected Integer getMaxEntriesReturned() {
084        return this.getPageSize();
085      }
086    
087      /**
088       * Internals the write search settings to XML.
089       *
090       * @param writer  the writer
091       * @param groupBy the group by clause
092       * @throws XMLStreamException the XML stream exception
093       * @throws ServiceXmlSerializationException the service xml serialization exception
094       */
095      @Override
096      protected void internalWriteSearchSettingsToXml(EwsServiceXmlWriter writer,
097          Grouping groupBy) throws XMLStreamException,
098          ServiceXmlSerializationException {
099        if (groupBy != null) {
100          groupBy.writeToXml(writer);
101        }
102      }
103    
104      /**
105       * Writes OrderBy property to XML.
106       *
107       * @param writer the writer
108       * @throws XMLStreamException the XML stream exception
109       * @throws ServiceXmlSerializationException the service xml serialization exception
110       */
111      @Override public void writeOrderByToXml(EwsServiceXmlWriter writer)
112          throws XMLStreamException, ServiceXmlSerializationException {
113        // No order by for paged view
114      }
115    
116      /**
117       * Validates this view.
118       *
119       * @param request The request using this view.
120       * @throws ServiceVersionException    the service version exception
121       * @throws ServiceValidationException the service validation exception
122       */
123      @Override public void internalValidate(ServiceRequestBase request)
124          throws ServiceVersionException, ServiceValidationException {
125        super.internalValidate(request);
126      }
127    
128      /**
129       * Initializes a new instance of the "PagedView" class.
130       *
131       * @param pageSize The maximum number of elements the search operation should
132       *                 return.
133       */
134      protected PagedView(int pageSize) {
135        super();
136        this.setPageSize(pageSize);
137      }
138    
139      /**
140       * Initializes a new instance of the "PagedView" class.
141       *
142       * @param pageSize The maximum number of elements the search operation should
143       *                 return.
144       * @param offset   The offset of the view from the base point.
145       */
146      protected PagedView(int pageSize, int offset) {
147        this(pageSize);
148        this.setOffset(offset);
149      }
150    
151      /**
152       * Initializes a new instance of the "PagedView" class.
153       *
154       * @param pageSize        The maximum number of elements the search operation should
155       *                        return.
156       * @param offset          The offset of the view from the base point.
157       * @param offsetBasePoint The base point of the offset.
158       */
159      protected PagedView(int pageSize, int offset,
160          OffsetBasePoint offsetBasePoint) {
161        this(pageSize, offset);
162        this.setOffsetBasePoint(offsetBasePoint);
163      }
164    
165      /**
166       * Gets the maximum number of item or folder the search operation should
167       * return.
168       *
169       * @return the page size
170       */
171      public int getPageSize() {
172        return pageSize;
173      }
174    
175      /**
176       * Sets the maximum number of item or folder the search operation should
177       * return.
178       *
179       * @param pageSize the new page size
180       */
181      public void setPageSize(int pageSize) {
182        if (pageSize <= 0) {
183          throw new IllegalArgumentException("The value must be greater than 0.");
184        }
185        this.pageSize = pageSize;
186      }
187    
188      /**
189       * Gets the base point of the offset.
190       *
191       * @return the offset base point
192       */
193      public OffsetBasePoint getOffsetBasePoint() {
194        return offsetBasePoint;
195      }
196    
197      /**
198       * Sets the base point of the offset.
199       *
200       * @param offsetBasePoint the new offset base point
201       */
202      public void setOffsetBasePoint(OffsetBasePoint offsetBasePoint) {
203        this.offsetBasePoint = offsetBasePoint;
204      }
205    
206      /**
207       * Gets the offset.
208       *
209       * @return the offset
210       */
211      public int getOffset() {
212        return offset;
213      }
214    
215      /**
216       * Sets the offset.
217       *
218       * @param offset the new offset
219       */
220      public void setOffset(int offset) {
221        if (offset >= 0) {
222          this.offset = offset;
223        } else {
224          throw new IllegalArgumentException("The offset must be greater than 0.");
225        }
226      }
227    
228    }