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.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.request.ServiceRequestBase;
030    import microsoft.exchange.webservices.data.core.enumeration.search.ItemTraversal;
031    import microsoft.exchange.webservices.data.core.enumeration.service.ServiceObjectType;
032    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
033    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceValidationException;
034    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
035    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
036    
037    import java.util.Date;
038    
039    /**
040     * Represents a date range view of appointments in calendar folder search
041     * operations.
042     */
043    public final class CalendarView extends ViewBase {
044    
045      /**
046       * The traversal.
047       */
048      private ItemTraversal traversal = ItemTraversal.Shallow;
049    
050      /**
051       * The max item returned.
052       */
053      private Integer maxItemsReturned;
054    
055      /**
056       * The start date.
057       */
058      private Date startDate;
059    
060      /**
061       * The end date.
062       */
063      private Date endDate;
064    
065      /**
066       * Writes the attribute to XML.
067       *
068       * @param writer the writer
069       * @throws ServiceXmlSerializationException the service xml serialization exception
070       */
071      public void writeAttributesToXml(EwsServiceXmlWriter writer)
072          throws ServiceXmlSerializationException {
073        writer.writeAttributeValue(XmlAttributeNames.Traversal, this
074            .getTraversal());
075      }
076    
077      /**
078       * Writes the search settings to XML.
079       *
080       * @param writer  the writer
081       * @param groupBy the group by
082       */
083      protected void internalWriteSearchSettingsToXml(EwsServiceXmlWriter writer,
084          Grouping groupBy) {
085        // No search settings for calendar views.
086      }
087    
088      /**
089       * Writes OrderBy property to XML.
090       *
091       * @param writer the writer
092       */
093      public void writeOrderByToXml(EwsServiceXmlWriter writer) {
094        // No OrderBy for calendar views.
095      }
096    
097      /**
098       * Gets the type of service object this view applies to.
099       *
100       * @return A ServiceObjectType value.
101       */
102      protected ServiceObjectType getServiceObjectType() {
103        return ServiceObjectType.Item;
104      }
105    
106      /**
107       * Initializes a new instance of CalendarView.
108       *
109       * @param startDate the start date
110       * @param endDate   the end date
111       */
112      public CalendarView(Date startDate, Date endDate) {
113        super();
114        this.startDate = startDate;
115        this.endDate = endDate;
116      }
117    
118      /**
119       * Initializes a new instance of CalendarView.
120       *
121       * @param startDate        the start date
122       * @param endDate          the end date
123       * @param maxItemsReturned the max item returned
124       */
125      public CalendarView(Date startDate, Date endDate, int maxItemsReturned) {
126        this(startDate, endDate);
127        this.maxItemsReturned = maxItemsReturned;
128      }
129    
130      /**
131       * Validate instance.
132       *
133       * @param request the request
134       * @throws ServiceVersionException    the service version exception
135       * @throws ServiceValidationException the service validation exception
136       */
137      public void internalValidate(ServiceRequestBase request)
138          throws ServiceVersionException, ServiceValidationException {
139        super.internalValidate(request);
140    
141        if (this.endDate.compareTo(this.startDate) < 0) {
142          throw new ServiceValidationException("EndDate must be greater than StartDate.");
143        }
144      }
145    
146      /**
147       * Write to XML.
148       *
149       * @param writer the writer
150       * @throws Exception the exception
151       */
152      protected void internalWriteViewToXml(EwsServiceXmlWriter writer)
153          throws Exception {
154        super.internalWriteViewToXml(writer);
155    
156        writer.writeAttributeValue(XmlAttributeNames.StartDate, this.startDate);
157        writer.writeAttributeValue(XmlAttributeNames.EndDate, this.endDate);
158      }
159    
160      /**
161       * Gets the name of the view XML element.
162       *
163       * @return XML element name
164       */
165      protected String getViewXmlElementName() {
166        return XmlElementNames.CalendarView;
167      }
168    
169      /**
170       * Gets the maximum number of item or folder the search operation should
171       * return.
172       *
173       * @return The maximum number of item the search operation should return.
174       */
175      protected Integer getMaxEntriesReturned() {
176        return this.maxItemsReturned;
177      }
178    
179      /**
180       * Gets the start date.
181       *
182       * @return the start date
183       */
184      public Date getStartDate() {
185        return this.startDate;
186      }
187    
188      /**
189       * Sets the start date.
190       *
191       * @param startDate the new start date
192       */
193      public void setStartDate(Date startDate) {
194        this.startDate = startDate;
195      }
196    
197      /**
198       * Gets the end date.
199       *
200       * @return the end date
201       */
202      public Date getEndDate() {
203        return this.endDate;
204      }
205    
206      /**
207       * Sets the end date.
208       *
209       * @param endDate the new end date
210       */
211      public void setEndDate(Date endDate) {
212        this.endDate = endDate;
213      }
214    
215      /**
216       * The maximum number of item the search operation should return.
217       *
218       * @return the max item returned
219       */
220      public Integer getMaxItemsReturned() {
221    
222        return this.maxItemsReturned;
223      }
224    
225      /**
226       * Sets the max item returned.
227       *
228       * @param maxItemsReturned the new max item returned
229       * @throws ArgumentException the argument exception
230       */
231      public void setMaxItemsReturned(Integer maxItemsReturned)
232          throws ArgumentException {
233        if (maxItemsReturned != null) {
234          if (maxItemsReturned.intValue() <= 0) {
235            throw new ArgumentException("The value must be greater than 0.");
236          }
237        }
238    
239        this.maxItemsReturned = maxItemsReturned;
240      }
241    
242      /**
243       * Gets  the search traversal mode. Defaults to
244       * ItemTraversal.Shallow.
245       *
246       * @return the traversal
247       */
248      public ItemTraversal getTraversal() {
249        return this.traversal;
250    
251      }
252    
253      /**
254       * Sets the traversal.
255       *
256       * @param traversal the new traversal
257       */
258      public void setTraversal(ItemTraversal traversal) {
259        this.traversal = traversal;
260      }
261    
262    }