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.ISelfValidate;
027 import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028 import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
029 import microsoft.exchange.webservices.data.core.XmlElementNames;
030 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
032
033 import javax.xml.stream.XMLStreamException;
034
035 import java.text.DateFormat;
036 import java.text.SimpleDateFormat;
037 import java.util.Date;
038 import java.util.TimeZone;
039
040 /**
041 * Represents a time period.
042 */
043 public class TimeWindow implements ISelfValidate {
044
045 /**
046 * The start time.
047 */
048 private Date startTime;
049
050 /**
051 * The end time.
052 */
053 private Date endTime;
054
055 /**
056 * Initializes a new instance of the "TimeWindow" class.
057 */
058 public TimeWindow() {
059 }
060
061 /**
062 * Initializes a new instance of the "TimeWindow" class.
063 *
064 * @param startTime the start time
065 * @param endTime the end time
066 */
067 public TimeWindow(Date startTime, Date endTime) {
068 this();
069 this.startTime = startTime;
070 this.endTime = endTime;
071 }
072
073 /**
074 * Gets the start time.
075 *
076 * @return the start time
077 */
078 public Date getStartTime() {
079 return startTime;
080 }
081
082 /**
083 * Sets the start time.
084 *
085 * @param startTime the new start time
086 */
087 public void setStartTime(Date startTime) {
088 this.startTime = startTime;
089 }
090
091 /**
092 * Gets the end time.
093 *
094 * @return the end time
095 */
096 public Date getEndTime() {
097 return endTime;
098 }
099
100 /**
101 * Sets the end time.
102 *
103 * @param endTime the new end time
104 */
105 public void setEndTime(Date endTime) {
106 this.endTime = endTime;
107 }
108
109 /**
110 * Loads from XML.
111 *
112 * @param reader the reader
113 * @throws Exception the exception
114 */
115 public void loadFromXml(EwsServiceXmlReader reader) throws Exception {
116 reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
117 XmlElementNames.Duration);
118
119 this.startTime = reader.readElementValueAsDateTime(XmlNamespace.Types,
120 XmlElementNames.StartTime);
121 this.endTime = reader.readElementValueAsDateTime(XmlNamespace.Types,
122 XmlElementNames.EndTime);
123
124 reader.readEndElement(XmlNamespace.Types, XmlElementNames.Duration);
125 }
126
127 /**
128 * Writes to XML.
129 *
130 * @param writer the writer
131 * @param xmlElementName the xml element name
132 * @param startTime the start time
133 * @param endTime the end time
134 * @throws XMLStreamException the XML stream exception
135 * @throws ServiceXmlSerializationException the service xml serialization exception
136 */
137 private static void writeToXml(EwsServiceXmlWriter writer,
138 String xmlElementName, Object startTime, Object endTime)
139 throws XMLStreamException, ServiceXmlSerializationException {
140 writer.writeStartElement(XmlNamespace.Types, xmlElementName);
141
142 writer.writeElementValue(XmlNamespace.Types, XmlElementNames.StartTime,
143 startTime);
144
145 writer.writeElementValue(XmlNamespace.Types, XmlElementNames.EndTime,
146 endTime);
147
148 writer.writeEndElement(); // xmlElementName
149 }
150
151 /**
152 * Writes to XML without scoping the dates and without emitting times.
153 *
154 * @param writer the writer
155 * @param xmlElementName the xml element name
156 * @throws XMLStreamException the XML stream exception
157 * @throws ServiceXmlSerializationException the service xml serialization exception
158 */
159 protected void writeToXmlUnscopedDatesOnly(EwsServiceXmlWriter writer,
160 String xmlElementName) throws XMLStreamException, ServiceXmlSerializationException {
161 final String DateOnlyFormat = "yyyy-MM-dd'T'00:00:00";
162
163 DateFormat formatter = new SimpleDateFormat(DateOnlyFormat);
164 formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
165
166 String start = formatter.format(this.startTime);
167 String end = formatter.format(this.endTime);
168 TimeWindow.writeToXml(writer, xmlElementName, start, end);
169 }
170
171 /**
172 * Writes to XML.
173 *
174 * @param writer the writer
175 * @param xmlElementName the xml element name
176 * @throws XMLStreamException the XML stream exception
177 * @throws ServiceXmlSerializationException the service xml serialization exception
178 */
179 public void writeToXml(EwsServiceXmlWriter writer, String xmlElementName)
180 throws XMLStreamException, ServiceXmlSerializationException {
181 TimeWindow.writeToXml(writer, xmlElementName, startTime, endTime);
182 }
183
184 /**
185 * Gets the duration.
186 *
187 * @return the duration
188 */
189 public long getDuration() {
190 return this.endTime.getTime() - this.startTime.getTime();
191 }
192
193 /**
194 * Validates this instance.
195 */
196 public void validate() {
197 }
198 }