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.misc.availability;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.property.time.DayOfTheWeek;
030    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031    import microsoft.exchange.webservices.data.misc.TimeSpan;
032    import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
033    import microsoft.exchange.webservices.data.property.complex.time.TimeZoneDefinition;
034    
035    import java.util.UUID;
036    
037    /**
038     * Represents a time zone as used by GetUserAvailabilityRequest.
039     */
040    public final class LegacyAvailabilityTimeZone extends ComplexProperty {
041    
042      /**
043       * The bias.
044       */
045      private TimeSpan bias;
046    
047      /**
048       * The standard time.
049       */
050      private LegacyAvailabilityTimeZoneTime standardTime;
051    
052      /**
053       * The daylight time.
054       */
055      private LegacyAvailabilityTimeZoneTime daylightTime;
056    
057      /**
058       * Initializes a new instance of the LegacyAvailabilityTimeZone class.
059       */
060      public LegacyAvailabilityTimeZone() {
061        super();
062        this.bias = new TimeSpan(0);
063        // If there are no adjustment rules (which is the
064        //case for UTC), we have to come up with two
065        // dummy time changes which both have a delta of
066        //zero and happen at two hard coded dates. This
067        // simulates a time zone in which there are no time changes.
068        this.daylightTime = new LegacyAvailabilityTimeZoneTime();
069        this.daylightTime.setDelta(new TimeSpan(0));
070        this.daylightTime.setDayOrder(1);
071        this.daylightTime.setDayOfTheWeek(DayOfTheWeek.Sunday);
072        this.daylightTime.setMonth(10);
073        this.daylightTime.setTimeOfDay(new TimeSpan(2 * 60 * 60 * 1000));
074        this.daylightTime.setYear(0);
075    
076        this.standardTime = new LegacyAvailabilityTimeZoneTime();
077        this.standardTime.setDelta(new TimeSpan(0));
078        this.standardTime.setDayOrder(1);
079        this.standardTime.setDayOfTheWeek(DayOfTheWeek.Sunday);
080        this.standardTime.setMonth(3);
081        this.standardTime.setTimeOfDay(new TimeSpan(2 * 60 * 60 * 1000));
082        this.daylightTime.setYear(0);
083      }
084    
085      /**
086       * To time zone info.
087       *
088       * @return the time zone
089       */
090      public TimeZoneDefinition toTimeZoneInfo() {
091    
092                    /*NumberFormat formatter = new DecimalFormat("00");
093                    String timeZoneId = this.bias.isNegative() ? "GMT+"+formatter.
094                    format(this.bias.getHours())+":"+
095                    formatter.format(this.bias.getMinutes()) : 
096                    "GMT-"+formatter.format(this.bias.getHours())+":"+
097                    formatter.format(this.bias.getMinutes());
098    */
099        TimeZoneDefinition timeZoneDefinition = new TimeZoneDefinition();
100        timeZoneDefinition.id = UUID.randomUUID().toString();
101        timeZoneDefinition.name = "Custom time zone";
102        return timeZoneDefinition;
103      }
104    
105      /**
106       * Tries to read element from XML.
107       *
108       * @param reader the reader
109       * @return True if element was read.
110       * @throws Exception the exception
111       */
112      @Override
113      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
114          throws Exception {
115        if (reader.getLocalName().equals(XmlElementNames.Bias)) {
116          this.bias = new TimeSpan((long)
117              reader.readElementValue(Integer.class) * 60 * 1000);
118          return true;
119        } else if (reader.getLocalName().equals(XmlElementNames.StandardTime)) {
120          this.standardTime = new LegacyAvailabilityTimeZoneTime();
121          this.standardTime.loadFromXml(reader, reader.getLocalName());
122          return true;
123        } else if (reader.getLocalName().equals(XmlElementNames.DaylightTime)) {
124          this.daylightTime = new LegacyAvailabilityTimeZoneTime();
125          this.daylightTime.loadFromXml(reader, reader.getLocalName());
126          return true;
127        } else {
128    
129          return false;
130        }
131    
132      }
133    
134      /**
135       * Writes the elements to XML.
136       *
137       * @param writer the writer
138       * @throws Exception the exception
139       */
140      @Override
141      public void writeElementsToXml(EwsServiceXmlWriter writer)
142          throws Exception {
143        writer.writeElementValue(
144            XmlNamespace.Types,
145            XmlElementNames.Bias,
146            (int) this.bias.getTotalMinutes());
147    
148        this.standardTime.writeToXml(writer, XmlElementNames.StandardTime);
149        this.daylightTime.writeToXml(writer, XmlElementNames.DaylightTime);
150      }
151    }