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.core.request;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.response.AttendeeAvailability;
031    import microsoft.exchange.webservices.data.core.response.ServiceResponseCollection;
032    import microsoft.exchange.webservices.data.core.response.SuggestionsResponse;
033    import microsoft.exchange.webservices.data.core.enumeration.availability.AvailabilityData;
034    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
035    import microsoft.exchange.webservices.data.core.enumeration.misc.error.ServiceError;
036    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
037    import microsoft.exchange.webservices.data.misc.availability.AttendeeInfo;
038    import microsoft.exchange.webservices.data.misc.availability.AvailabilityOptions;
039    import microsoft.exchange.webservices.data.misc.availability.GetUserAvailabilityResults;
040    import microsoft.exchange.webservices.data.misc.availability.LegacyAvailabilityTimeZone;
041    import microsoft.exchange.webservices.data.misc.availability.TimeWindow;
042    
043    /**
044     * Represents a GetUserAvailability request.
045     */
046    public final class GetUserAvailabilityRequest extends SimpleServiceRequestBase<GetUserAvailabilityResults> {
047    
048      /**
049       * The attendees.
050       */
051      private Iterable<AttendeeInfo> attendees;
052    
053      /**
054       * The time window.
055       */
056      private TimeWindow timeWindow;
057    
058      /**
059       * The requested data.
060       */
061      private AvailabilityData requestedData =
062          AvailabilityData.FreeBusyAndSuggestions;
063    
064      /**
065       * The options.
066       */
067      private AvailabilityOptions options;
068    
069      /**
070       * Initializes a new instance of the "GetUserAvailabilityRequest" class.
071       *
072       * @param service the service
073       * @throws Exception
074       */
075      public GetUserAvailabilityRequest(ExchangeService service)
076          throws Exception {
077        super(service);
078      }
079    
080      /**
081       * Gets the name of the XML element.
082       *
083       * @return XML element name.
084       */
085      @Override public String getXmlElementName() {
086        return XmlElementNames.GetUserAvailabilityRequest;
087      }
088    
089      /**
090       * Gets a value indicating whether free/busy data is requested.
091       *
092       * @return true, if is free busy view requested
093       */
094      public boolean isFreeBusyViewRequested() {
095        return this.requestedData == AvailabilityData.FreeBusy ||
096            this.requestedData == AvailabilityData.
097                FreeBusyAndSuggestions;
098      }
099    
100      /**
101       * Gets a value indicating whether suggestions are requested.
102       *
103       * @return true, if is suggestions view requested
104       */
105      public boolean isSuggestionsViewRequested() {
106        return this.requestedData == AvailabilityData.Suggestions ||
107            this.requestedData == AvailabilityData.
108                FreeBusyAndSuggestions;
109      }
110    
111      /**
112       * Validate request.
113       *
114       * @throws Exception the exception
115       */
116      @Override
117      protected void validate() throws Exception {
118        super.validate();
119    
120        this.options.validate(this.timeWindow.getDuration());
121      }
122    
123      /**
124       * Writes XML elements.
125       *
126       * @param writer the writer
127       * @throws Exception the exception
128       */
129      @Override
130      protected void writeElementsToXml(EwsServiceXmlWriter writer)
131          throws Exception {
132        // Only serialize the TimeZone property against an Exchange 2007 SP1
133        // server.
134        // Against Exchange 2010, the time zone is emitted in the request's SOAP
135        // header.
136        //if (writer.getService().getRequestedServerVersion() ==
137        //ExchangeVersion.Exchange2007_SP1) {
138        LegacyAvailabilityTimeZone legacyTimeZone =
139            new LegacyAvailabilityTimeZone();
140    
141        legacyTimeZone.writeToXml(writer, XmlElementNames.TimeZone);
142    
143    
144        writer.writeStartElement(XmlNamespace.Messages,
145            XmlElementNames.MailboxDataArray);
146    
147        for (AttendeeInfo attendee : this.attendees) {
148          attendee.writeToXml(writer);
149        }
150    
151        writer.writeEndElement(); // MailboxDataArray
152    
153        this.options.writeToXml(writer, this);
154      }
155    
156      /**
157       * Gets the name of the response XML element.
158       *
159       * @return XML element name
160       */
161      @Override
162      protected String getResponseXmlElementName() {
163        return XmlElementNames.GetUserAvailabilityResponse;
164      }
165    
166      /**
167       * {@inheritDoc}
168       */
169      @Override
170      protected GetUserAvailabilityResults parseResponse(EwsServiceXmlReader reader)
171          throws Exception {
172        GetUserAvailabilityResults serviceResponse =
173            new GetUserAvailabilityResults();
174    
175        if (this.isFreeBusyViewRequested()) {
176          serviceResponse
177              .setAttendeesAvailability(new ServiceResponseCollection<AttendeeAvailability>());
178    
179          reader.readStartElement(XmlNamespace.Messages,
180              XmlElementNames.FreeBusyResponseArray);
181    
182          do {
183            reader.read();
184    
185            if (reader.isStartElement(XmlNamespace.Messages,
186                XmlElementNames.FreeBusyResponse)) {
187              AttendeeAvailability freeBusyResponse =
188                  new AttendeeAvailability();
189    
190              freeBusyResponse.loadFromXml(reader,
191                  XmlElementNames.ResponseMessage);
192    
193              if (freeBusyResponse.getErrorCode().equals(
194                  ServiceError.NoError)) {
195                freeBusyResponse.loadFreeBusyViewFromXml(reader,
196                    this.options.getRequestedFreeBusyView());
197              }
198    
199              serviceResponse.getAttendeesAvailability().add(
200                  freeBusyResponse);
201            }
202          } while (!reader.isEndElement(XmlNamespace.Messages,
203              XmlElementNames.FreeBusyResponseArray));
204        }
205    
206        if (this.isSuggestionsViewRequested()) {
207          serviceResponse.setSuggestionsResponse(new SuggestionsResponse());
208    
209          reader.readStartElement(XmlNamespace.Messages,
210              XmlElementNames.SuggestionsResponse);
211    
212          serviceResponse.getSuggestionsResponse().loadFromXml(reader,
213              XmlElementNames.ResponseMessage);
214    
215          if (serviceResponse.getSuggestionsResponse().getErrorCode().equals(
216              ServiceError.NoError)) {
217            serviceResponse.getSuggestionsResponse()
218                .loadSuggestedDaysFromXml(reader);
219          }
220    
221          reader.readEndElement(XmlNamespace.Messages,
222              XmlElementNames.SuggestionsResponse);
223        }
224    
225        return serviceResponse;
226      }
227    
228      /**
229       * Gets the request version.
230       *
231       * @return Earliest Exchange version in which this request is supported.
232       */
233      @Override
234      protected ExchangeVersion getMinimumRequiredServerVersion() {
235        return ExchangeVersion.Exchange2007_SP1;
236      }
237    
238      /**
239       * Executes this request.
240       *
241       * @return Service response.
242       * @throws Exception the exception
243       */
244      public GetUserAvailabilityResults execute() throws Exception {
245        return internalExecute();
246      }
247    
248      /**
249       * Gets  the attendees.
250       *
251       * @return the attendees
252       */
253      public Iterable<AttendeeInfo> getAttendees() {
254        return attendees;
255      }
256    
257      /**
258       * Sets the attendees.
259       *
260       * @param attendees the new attendees
261       */
262      public void setAttendees(Iterable<AttendeeInfo> attendees) {
263        this.attendees = attendees;
264      }
265    
266      /**
267       * Gets the time window in which to retrieve user availability
268       * information.
269       *
270       * @return the time window
271       */
272      public TimeWindow getTimeWindow() {
273        return timeWindow;
274      }
275    
276      /**
277       * Sets the time window.
278       *
279       * @param timeWindow the new time window
280       */
281      public void setTimeWindow(TimeWindow timeWindow) {
282        this.timeWindow = timeWindow;
283      }
284    
285      /**
286       * Gets  a value indicating what data is requested (free/busy and/or
287       * suggestions).
288       *
289       * @return the requested data
290       */
291      public AvailabilityData getRequestedData() {
292        return requestedData;
293      }
294    
295      /**
296       * Sets the requested data.
297       *
298       * @param requestedData the new requested data
299       */
300      public void setRequestedData(AvailabilityData requestedData) {
301        this.requestedData = requestedData;
302      }
303    
304      /**
305       * Gets an object that allows you to specify options controlling the
306       * information returned by the GetUserAvailability request.
307       *
308       * @return the options
309       */
310      public AvailabilityOptions getOptions() {
311        return options;
312      }
313    
314      /**
315       * Sets the options.
316       *
317       * @param options the new options
318       */
319      public void setOptions(AvailabilityOptions options) {
320        this.options = options;
321      }
322    
323    }