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.messaging;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.enumeration.service.error.ConnectionFailureCause;
031    import microsoft.exchange.webservices.data.core.enumeration.service.PhoneCallState;
032    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
033    import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
034    
035    /**
036     * Represents a phone call.
037     */
038    public final class PhoneCall extends ComplexProperty {
039    
040      /**
041       * The Constant successfullResponseText.
042       */
043      private final static String SuccessfullResponseText = "OK";
044    
045      /**
046       * The Constant successfullResponseCode.
047       */
048      private final static int SuccessfullResponseCode = 200;
049    
050      /**
051       * The service.
052       */
053      private ExchangeService service;
054    
055      /**
056       * The state.
057       */
058      private PhoneCallState state;
059    
060      /**
061       * The connection failure cause.
062       */
063      private ConnectionFailureCause connectionFailureCause;
064    
065      /**
066       * The sip response text.
067       */
068      private String sipResponseText;
069    
070      /**
071       * The sip response code.
072       */
073      private int sipResponseCode;
074    
075      /**
076       * The id.
077       */
078      private PhoneCallId id;
079    
080      /**
081       * PhoneCall Constructor.
082       *
083       * @param service the service
084       */
085      public PhoneCall(ExchangeService service) {
086        EwsUtilities.ewsAssert(service != null, "PhoneCall.ctor", "service is null");
087    
088        this.service = service;
089        this.state = PhoneCallState.Connecting;
090        this.connectionFailureCause = ConnectionFailureCause.None;
091        this.sipResponseText = PhoneCall.SuccessfullResponseText;
092        this.sipResponseCode = PhoneCall.SuccessfullResponseCode;
093      }
094    
095      /**
096       * PhoneCall Constructor.
097       *
098       * @param service the service
099       * @param id      the id
100       */
101      protected PhoneCall(ExchangeService service, PhoneCallId id) {
102        this(service);
103        this.id = id;
104      }
105    
106      /**
107       * Refreshes the state of this phone call.
108       *
109       * @throws Exception the exception
110       */
111      public void refresh() throws Exception {
112        PhoneCall phoneCall = service.getUnifiedMessaging()
113            .getPhoneCallInformation(this.id);
114        this.state = phoneCall.getState();
115        this.connectionFailureCause = phoneCall.getConnectionFailureCause();
116        this.sipResponseText = phoneCall.getSipResponseText();
117        this.sipResponseCode = phoneCall.getSipResponseCode();
118      }
119    
120      /**
121       * Disconnects this phone call.
122       *
123       * @throws Exception the exception
124       */
125      public void disconnect() throws Exception {
126        // If call is already disconnected, throw exception
127        //
128        if (this.state == PhoneCallState.Disconnected) {
129          throw new ServiceLocalException("The phone call has already been disconnected.");
130        }
131    
132        this.service.getUnifiedMessaging().disconnectPhoneCall(this.id);
133        this.state = PhoneCallState.Disconnected;
134      }
135    
136      /**
137       * Tries to read an element from XML.
138       *
139       * @param reader the reader
140       * @return True if element was read.
141       * @throws Exception the exception
142       */
143      @Override
144      public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
145          throws Exception {
146        if (reader.getLocalName().equals(XmlElementNames.PhoneCallState)) {
147          this.state = reader.readElementValue(PhoneCallState.class);
148          return true;
149        } else if (reader.getLocalName().equals(
150            XmlElementNames.ConnectionFailureCause)) {
151          this.connectionFailureCause = reader
152              .readElementValue(ConnectionFailureCause.class);
153          return true;
154        } else if (reader.getLocalName()
155            .equals(XmlElementNames.SIPResponseText)) {
156          this.sipResponseText = reader.readElementValue();
157          return true;
158        } else if (reader.getLocalName()
159            .equals(XmlElementNames.SIPResponseCode)) {
160          this.sipResponseCode = reader.readElementValue(Integer.class);
161          return true;
162        } else {
163          return false;
164        }
165    
166      }
167    
168      /**
169       * Gets a value indicating the last known state of this phone call.
170       *
171       * @return the state
172       */
173      public PhoneCallState getState() {
174        return state;
175      }
176    
177      /**
178       * Gets the SIP response text of this phone call.
179       *
180       * @return the sip response text
181       */
182      public String getSipResponseText() {
183        return sipResponseText;
184      }
185    
186      /**
187       * Gets the SIP response code of this phone call.
188       *
189       * @return the sip response code
190       */
191      public int getSipResponseCode() {
192        return sipResponseCode;
193      }
194    
195      /**
196       * Gets a value indicating the reason why this phone call failed to connect.
197       *
198       * @return the connection failure cause
199       */
200      public ConnectionFailureCause getConnectionFailureCause() {
201        return connectionFailureCause;
202      }
203    
204    }