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.property.complex.availability;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.XmlElementNames;
029import microsoft.exchange.webservices.data.core.enumeration.property.ConflictType;
030import microsoft.exchange.webservices.data.core.enumeration.availability.SuggestionQuality;
031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
033
034import java.util.ArrayList;
035import java.util.Collection;
036import java.util.Date;
037
038/**
039 * Represents an availability time suggestion.
040 */
041public final class TimeSuggestion extends ComplexProperty {
042
043  /**
044   * The meeting time.
045   */
046  private Date meetingTime;
047
048  /**
049   * The is work time.
050   */
051  private boolean isWorkTime;
052
053  /**
054   * The quality.
055   */
056  private SuggestionQuality quality;
057
058  /**
059   * The conflicts.
060   */
061  private Collection<Conflict> conflicts = new ArrayList<Conflict>();
062
063  /**
064   * Initializes a new instance of the TimeSuggestion class.
065   */
066  protected TimeSuggestion() {
067    super();
068  }
069
070  /**
071   * Tries to read element from XML.
072   *
073   * @param reader the reader
074   * @return True if appropriate element was read.
075   * @throws Exception the exception
076   */
077  @Override
078  public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
079      throws Exception {
080    if (reader.getLocalName().equals(XmlElementNames.MeetingTime)) {
081      this.meetingTime = reader
082          .readElementValueAsUnbiasedDateTimeScopedToServiceTimeZone();
083      return true;
084    } else if (reader.getLocalName().equals(XmlElementNames.IsWorkTime)) {
085      this.isWorkTime = reader.readElementValue(Boolean.class);
086      return true;
087    } else if (reader.getLocalName().equals(
088        XmlElementNames.SuggestionQuality)) {
089      this.quality = reader.readElementValue(SuggestionQuality.class);
090      return true;
091    } else if (reader.getLocalName().equals(
092        XmlElementNames.AttendeeConflictDataArray)) {
093      if (!reader.isEmptyElement()) {
094        do {
095          reader.read();
096
097          if (reader.isStartElement()) {
098            Conflict conflict = null;
099
100            if (reader.getLocalName().equals(
101                XmlElementNames.UnknownAttendeeConflictData)) {
102              conflict = new Conflict(
103                  ConflictType.UnknownAttendeeConflict);
104            } else if (reader
105                .getLocalName()
106                .equals(
107                    XmlElementNames.
108                        TooBigGroupAttendeeConflictData)) {
109              conflict = new Conflict(
110                  ConflictType.GroupTooBigConflict);
111            } else if (reader.getLocalName().equals(
112                XmlElementNames.
113                    IndividualAttendeeConflictData)) {
114              conflict = new Conflict(
115                  ConflictType.IndividualAttendeeConflict);
116            } else if (reader.getLocalName().equals(
117                XmlElementNames.GroupAttendeeConflictData)) {
118              conflict = new Conflict(ConflictType.GroupConflict);
119            } else {
120              EwsUtilities
121                  .ewsAssert(false, "TimeSuggestion." + "TryReadElementFromXml",
122                             String.format("The %s element name " +
123                                           "does not map " +
124                                           "to any AttendeeConflict " +
125                                           "descendant.", reader.getLocalName()));
126
127              // The following line to please the compiler
128            }
129            conflict.loadFromXml(reader, reader.getLocalName());
130
131            this.conflicts.add(conflict);
132          }
133        } while (!reader.isEndElement(XmlNamespace.Types,
134            XmlElementNames.AttendeeConflictDataArray));
135      }
136
137      return true;
138    } else {
139      return false;
140    }
141
142  }
143
144  /**
145   * Gets the suggested time.
146   *
147   * @return the meeting time
148   */
149  public Date getMeetingTime() {
150    return meetingTime;
151  }
152
153  /**
154   * Gets a value indicating whether the suggested time is within working
155   * hours.
156   *
157   * @return true, if is work time
158   */
159  public boolean isWorkTime() {
160    return isWorkTime;
161  }
162
163  /**
164   * Gets the quality of the suggestion.
165   *
166   * @return the quality
167   */
168  public SuggestionQuality getQuality() {
169    return quality;
170  }
171
172  /**
173   * Gets a collection of conflicts at the suggested time.
174   *
175   * @return the conflicts
176   */
177  public Collection<Conflict> getConflicts() {
178    return conflicts;
179  }
180
181}