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.core.request;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
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.ServiceErrorHandling;
031    import microsoft.exchange.webservices.data.core.response.GetUserConfigurationResponse;
032    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
033    import microsoft.exchange.webservices.data.core.enumeration.misc.UserConfigurationProperties;
034    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
035    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
036    import microsoft.exchange.webservices.data.misc.UserConfiguration;
037    import microsoft.exchange.webservices.data.property.complex.FolderId;
038    
039    import java.util.EnumSet;
040    
041    /**
042     * The Class GetUserConfigurationRequest.
043     */
044    public class GetUserConfigurationRequest extends
045        MultiResponseServiceRequest<GetUserConfigurationResponse> {
046    
047      /**
048       * The name.
049       */
050      private String name;
051    
052      /**
053       * The parent folder id.
054       */
055      private FolderId parentFolderId;
056    
057      /**
058       * The property.
059       */
060      private EnumSet<UserConfigurationProperties> properties;
061    
062      /**
063       * The user configuration.
064       */
065      private UserConfiguration userConfiguration;
066    
067      /**
068       * Validate request.
069       *
070       * @throws ServiceLocalException the service local exception
071       * @throws Exception                                                 the exception
072       */
073      @Override
074      protected void validate() throws ServiceLocalException, Exception {
075        super.validate();
076    
077        EwsUtilities.validateParam(this.name, "name");
078        EwsUtilities.validateParam(this.parentFolderId, "parentFolderId");
079        this.getParentFolderId().validate(
080            this.getService().getRequestedServerVersion());
081      }
082    
083      /**
084       * Creates the service response.
085       *
086       * @param service       the service
087       * @param responseIndex the response index
088       * @return Service response.
089       * @throws Exception the exception
090       */
091      @Override
092      protected GetUserConfigurationResponse createServiceResponse(
093          ExchangeService service, int responseIndex) throws Exception {
094        // In the case of UserConfiguration.Load(), this.userConfiguration is
095        // set.
096        if (this.userConfiguration == null) {
097          this.userConfiguration = new UserConfiguration(service,
098              this.properties);
099          this.userConfiguration.setName(this.name);
100          this.userConfiguration.setParentFolderId(this.parentFolderId);
101        }
102    
103        return new GetUserConfigurationResponse(this.userConfiguration);
104      }
105    
106      /**
107       * Gets the request version.
108       *
109       * @return Earliest Exchange version in which this request is supported.
110       */
111      @Override
112      protected ExchangeVersion getMinimumRequiredServerVersion() {
113        return ExchangeVersion.Exchange2010;
114      }
115    
116      /**
117       * Gets the expected response message count.
118       *
119       * @return Number of expected response messages.
120       */
121      @Override
122      protected int getExpectedResponseMessageCount() {
123        return 1;
124      }
125    
126      /**
127       * Gets the name of the XML element.
128       *
129       * @return XML element name
130       */
131      @Override public String getXmlElementName() {
132        return XmlElementNames.GetUserConfiguration;
133      }
134    
135      /**
136       * Gets the name of the response XML element.
137       *
138       * @return XML element name
139       */
140      @Override
141      protected String getResponseXmlElementName() {
142        return XmlElementNames.GetUserConfigurationResponse;
143      }
144    
145      /**
146       * Gets the name of the response message XML element.
147       *
148       * @return XML element name
149       */
150      @Override
151      protected String getResponseMessageXmlElementName() {
152        return XmlElementNames.GetUserConfigurationResponseMessage;
153      }
154    
155      /**
156       * Writes XML elements.
157       *
158       * @param writer the writer
159       * @throws Exception the exception
160       */
161      @Override
162      protected void writeElementsToXml(EwsServiceXmlWriter writer)
163          throws Exception {
164        final String EnumDelimiter = ",";
165    
166        // Write UserConfiguationName element
167        UserConfiguration.writeUserConfigurationNameToXml(writer,
168            XmlNamespace.Messages, this.name, this.parentFolderId);
169    
170        // Write UserConfigurationProperties element
171        writer.writeElementValue(XmlNamespace.Messages,
172            XmlElementNames.UserConfigurationProperties, this.properties
173                .toString().replace(EnumDelimiter, "").
174                    replace("[", "").replace("]", ""));
175      }
176    
177      /**
178       * Initializes a new instance of the class.
179       *
180       * @param service the service
181       * @throws Exception
182       */
183      public GetUserConfigurationRequest(ExchangeService service)
184          throws Exception {
185        super(service, ServiceErrorHandling.ThrowOnError);
186      }
187    
188      /**
189       * Gets  the name. <value>The name.</value>
190       *
191       * @return the name
192       */
193      protected String getName() {
194        return this.name;
195      }
196    
197      /**
198       * Sets the name.
199       *
200       * @param name the new name
201       */
202      public void setName(String name) {
203        this.name = name;
204      }
205    
206      /**
207       * Gets  the parent folder Id. <value>The parent folder Id.</value>
208       *
209       * @return the parent folder id
210       */
211      protected FolderId getParentFolderId() {
212        return this.parentFolderId;
213      }
214    
215      /**
216       * Sets the parent folder id.
217       *
218       * @param parentFolderId the new parent folder id
219       */
220      public void setParentFolderId(FolderId parentFolderId) {
221        this.parentFolderId = parentFolderId;
222      }
223    
224      /**
225       * Gets  the user configuration. <value>The user
226       * configuration.</value>
227       *
228       * @return the user configuration
229       */
230      protected UserConfiguration getUserConfiguration() {
231        return this.userConfiguration;
232      }
233    
234      /**
235       * Sets the user configuration.
236       *
237       * @param userConfiguration the new user configuration
238       */
239      public void setUserConfiguration(UserConfiguration userConfiguration) {
240        this.userConfiguration = userConfiguration;
241        this.name = this.userConfiguration.getName();
242        this.parentFolderId = this.userConfiguration.getParentFolderId();
243      }
244    
245      /**
246       * Gets the property.
247       *
248       * @return the property
249       */
250      protected EnumSet<UserConfigurationProperties> getProperties() {
251        return this.properties;
252      }
253    
254      /**
255       * Sets the property.
256       *
257       * @param properties the new property
258       */
259      public void setProperties(EnumSet<UserConfigurationProperties> properties) {
260        this.properties = properties;
261      }
262    
263    }