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.notification;
025
026import microsoft.exchange.webservices.data.core.ExchangeService;
027import microsoft.exchange.webservices.data.misc.AsyncCallback;
028import microsoft.exchange.webservices.data.misc.IAsyncResult;
029
030/**
031 * Represents a pull subscription.
032 */
033public final class PullSubscription extends SubscriptionBase {
034  /**
035   * The more events available.
036   */
037  private boolean moreEventsAvailable;
038
039  /**
040   * Initializes a new instance.
041   *
042   * @param service the service
043   * @throws Exception the exception
044   */
045  public PullSubscription(ExchangeService service) throws Exception {
046    super(service);
047  }
048
049  /**
050   * Obtains a collection of events that occurred on the subscribed folder
051   * since the point in time defined by the Watermark property. When GetEvents
052   * succeeds, Watermark is updated.
053   *
054   * @return Returns a collection of events that occurred since the last
055   * watermark
056   * @throws Exception the exception
057   */
058  public GetEventsResults getEvents() throws Exception {
059    GetEventsResults results = getService().getEvents(this.getId(),
060        this.getWaterMark());
061    this.setWaterMark(results.getNewWatermark());
062    this.moreEventsAvailable = results.isMoreEventsAvailable();
063    return results;
064  }
065
066  /**
067   * Begins an asynchronous request to obtain a collection of events that occurred on the subscribed
068   * folder since the point in time defined by the Watermark property
069   *
070   * @param callback The AsyncCallback delegate
071   * @param state    An object that contains state information for this request
072   * @return An IAsyncResult that references the asynchronous request
073   * @throws Exception
074   */
075  public IAsyncResult beginGetEvents(AsyncCallback callback, Object state) throws Exception {
076    return this.getService().beginGetEvents(callback, state, this.getId(), this.getWaterMark());
077  }
078
079  /**
080   * Ends an asynchronous request to obtain a collection of events that occurred on the subscribed
081   * folder since the point in time defined by the Watermark property. When EndGetEvents succeeds,
082   * Watermark is updated.
083   *
084   * @param asyncResult An IAsyncResult that references the asynchronous request.
085   * @return Returns a collection of events that occurred since the last watermark.
086   * @throws Exception
087   */
088  public GetEventsResults endGetEvents(IAsyncResult asyncResult) throws Exception {
089    GetEventsResults results = this.getService().endGetEvents(asyncResult);
090
091    this.setWaterMark(results.getNewWatermark());
092    this.moreEventsAvailable = results.isMoreEventsAvailable();
093
094    return results;
095  }
096
097  /**
098   * Unsubscribes from the pull subscription.
099   *
100   * @throws Exception the exception
101   */
102  public void unsubscribe() throws Exception {
103    getService().unsubscribe(getId());
104  }
105
106  /**
107   * Begins an asynchronous request to unsubscribe from the pull subscription.
108   *
109   * @param callback The AsyncCallback delegate.
110   * @param state    An object that contains state information for this request
111   * @return An IAsyncResult that references the asynchronous request
112   * @throws Exception
113   */
114  public IAsyncResult beginUnsubscribe(AsyncCallback callback, Object state) throws Exception {
115    return this.getService().beginUnsubscribe(callback, state, this.getId());
116  }
117
118  /**
119   * Ends an asynchronous request to unsubscribe from the pull subscription.
120   *
121   * @param asyncResult An IAsyncResult that references the asynchronous request.
122   * @throws Exception
123   */
124  public void endUnsubscribe(IAsyncResult asyncResult) throws Exception {
125    this.getService().endUnsubscribe(asyncResult);
126  }
127
128  /**
129   * Gets a value indicating whether more events are available on the server.
130   * MoreEventsAvailable is undefined (null) until GetEvents is called.
131   *
132   * @return true, if is more events available
133   */
134  public boolean isMoreEventsAvailable() {
135    return moreEventsAvailable;
136  }
137}