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.property.complex.availability;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.XmlElementNames;
028    import microsoft.exchange.webservices.data.core.enumeration.property.time.DayOfTheWeek;
029    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030    import microsoft.exchange.webservices.data.misc.availability.LegacyAvailabilityTimeZone;
031    import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
032    import microsoft.exchange.webservices.data.property.complex.time.TimeZoneDefinition;
033    
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.List;
037    
038    /**
039     * Represents the working hours for a specific time zone.
040     */
041    public final class WorkingHours extends ComplexProperty {
042    
043      /**
044       * The time zone.
045       */
046      private TimeZoneDefinition timeZone;
047    
048      /**
049       * The days of the week.
050       */
051      private Collection<DayOfTheWeek> daysOfTheWeek =
052          new ArrayList<DayOfTheWeek>();
053    
054      /**
055       * The start time.
056       */
057      private long startTime;
058    
059      /**
060       * The end time.
061       */
062      private long endTime;
063    
064      /**
065       * Instantiates a new working hours.
066       */
067      public WorkingHours() {
068        super();
069      }
070    
071      /**
072       * Tries to read element from XML.
073       *
074       * @param reader accepts EwsServiceXmlReader
075       * @return True if element was read
076       * @throws Exception throws Exception
077       */
078      @Override
079      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
080          throws Exception {
081        if (reader.getLocalName().equals(XmlElementNames.TimeZone)) {
082          LegacyAvailabilityTimeZone legacyTimeZone =
083              new LegacyAvailabilityTimeZone();
084          legacyTimeZone.loadFromXml(reader, reader.getLocalName());
085    
086          this.timeZone = legacyTimeZone.toTimeZoneInfo();
087    
088          return true;
089        }
090        if (reader.getLocalName().equals(XmlElementNames.WorkingPeriodArray)) {
091          List<WorkingPeriod> workingPeriods = new ArrayList<WorkingPeriod>();
092    
093          do {
094            reader.read();
095    
096            if (reader.isStartElement(XmlNamespace.Types,
097                XmlElementNames.WorkingPeriod)) {
098              WorkingPeriod workingPeriod = new WorkingPeriod();
099    
100              workingPeriod.loadFromXml(reader, reader.getLocalName());
101    
102              workingPeriods.add(workingPeriod);
103            }
104          } while (!reader.isEndElement(XmlNamespace.Types,
105              XmlElementNames.WorkingPeriodArray));
106    
107          // Availability supports a structure that can technically represent
108          // different working
109          // times for each day of the week. This is apparently how the
110          // information is stored in
111          // Exchange. However, no client (Outlook, OWA) either will let you
112          // specify different
113          // working times for each day of the week, and Outlook won't either
114          // honor that complex
115          // structure if it happens to be in Exchange (OWA goes through XSO
116          // which doesn't either
117          // honor the structure).
118          // So here we'll do what Outlook and OWA do: we'll use the start and
119          // end times of the
120          // first working period, but we'll use the week days of all the
121          // periods.
122    
123          this.startTime = workingPeriods.get(0).getStartTime();
124          this.endTime = workingPeriods.get(0).getEndTime();
125    
126          for (WorkingPeriod workingPeriod : workingPeriods) {
127            for (DayOfTheWeek dayOfWeek : workingPeriods.get(0)
128                .getDaysOfWeek()) {
129              if (!this.daysOfTheWeek.contains(dayOfWeek)) {
130                this.daysOfTheWeek.add(dayOfWeek);
131              }
132            }
133          }
134    
135          return true;
136        } else {
137          return false;
138        }
139    
140      }
141    
142      /**
143       * Gets the time zone to which the working hours apply.
144       *
145       * @return the time zone
146       */
147      public TimeZoneDefinition getTimeZone() {
148        return timeZone;
149      }
150    
151      /**
152       * Gets the working days of the attendees.
153       *
154       * @return the days of the week
155       */
156      public Collection<DayOfTheWeek> getDaysOfTheWeek() {
157        return daysOfTheWeek;
158      }
159    
160      /**
161       * Gets the time of the day the attendee starts working.
162       *
163       * @return the start time
164       */
165      public long getStartTime() {
166        return startTime;
167      }
168    
169      /**
170       * Gets the time of the day the attendee stops working.
171       *
172       * @return the end time
173       */
174      public long getEndTime() {
175        return endTime;
176      }
177    
178    }