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
024package microsoft.exchange.webservices.data.core.request;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.ExchangeService;
029import microsoft.exchange.webservices.data.core.ILazyMember;
030import microsoft.exchange.webservices.data.core.LazyMember;
031import microsoft.exchange.webservices.data.core.PropertySet;
032import microsoft.exchange.webservices.data.core.XmlAttributeNames;
033import microsoft.exchange.webservices.data.core.XmlElementNames;
034import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
035import microsoft.exchange.webservices.data.core.response.ResolveNamesResponse;
036import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
037import microsoft.exchange.webservices.data.core.enumeration.search.ResolveNameSearchLocation;
038import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
039import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
040import microsoft.exchange.webservices.data.misc.FolderIdWrapperList;
041
042import java.util.HashMap;
043import java.util.Map;
044
045/**
046 * Represents a ResolveNames request.
047 */
048public final class ResolveNamesRequest extends
049    MultiResponseServiceRequest<ResolveNamesResponse> {
050
051  /**
052   * The Search scope map.
053   */
054  private static LazyMember<Map<ResolveNameSearchLocation, String>>
055      searchScopeMap =
056      new LazyMember<Map<ResolveNameSearchLocation, String>>(
057          new ILazyMember<Map<ResolveNameSearchLocation, String>>() {
058            @Override
059            public Map<ResolveNameSearchLocation, String>
060            createInstance() {
061
062              Map<ResolveNameSearchLocation, String> map =
063                  new HashMap<ResolveNameSearchLocation, String>();
064
065              map.put(ResolveNameSearchLocation.DirectoryOnly,
066                  "ActiveDirectory");
067              map.put(ResolveNameSearchLocation.DirectoryThenContacts,
068                  "ActiveDirectoryContacts");
069              map.put(ResolveNameSearchLocation.ContactsOnly,
070                  "Contacts");
071              map.put(ResolveNameSearchLocation.ContactsThenDirectory,
072                  "ContactsActiveDirectory");
073
074              return map;
075            }
076
077          });
078
079  /**
080   * The name to resolve.
081   */
082  private String nameToResolve;
083
084  /**
085   * The return full contact data.
086   */
087  private boolean returnFullContactData;
088
089  /**
090   * The search location.
091   */
092  private ResolveNameSearchLocation searchLocation;
093
094  /**
095   * The Contact PropertySet.   *
096   */
097  private PropertySet contactDataPropertySet;
098
099  /**
100   * The parent folder ids.
101   */
102  private FolderIdWrapperList parentFolderIds = new FolderIdWrapperList();
103
104
105
106  /**
107   * Asserts the valid.
108   *
109   * @throws Exception the exception
110   */
111  @Override
112  protected void validate() throws Exception {
113    super.validate();
114    EwsUtilities.validateNonBlankStringParam(this.
115        getNameToResolve(), "NameToResolve");
116  }
117
118  /**
119   * Creates the service response.
120   *
121   * @param service       the service
122   * @param responseIndex the response index
123   * @return Service response
124   */
125  @Override
126  protected ResolveNamesResponse createServiceResponse(
127      ExchangeService service, int responseIndex) {
128    return new ResolveNamesResponse(service);
129  }
130
131  /**
132   * Gets the name of the XML element.
133   *
134   * @return XML element name
135   */
136  @Override public String getXmlElementName() {
137    return XmlElementNames.ResolveNames;
138  }
139
140  /**
141   * Gets the name of the response XML element.
142   *
143   * @return XML element name
144   */
145  @Override
146  protected String getResponseXmlElementName() {
147    return XmlElementNames.ResolveNamesResponse;
148  }
149
150  /**
151   * Gets the name of the response message XML element.
152   *
153   * @return XML element name
154   */
155  @Override
156  protected String getResponseMessageXmlElementName() {
157    return XmlElementNames.ResolveNamesResponseMessage;
158  }
159
160  /**
161   * Initializes a new instance of the class.
162   *
163   * @param service the service
164   * @throws Exception
165   */
166  public ResolveNamesRequest(ExchangeService service)
167      throws Exception {
168    super(service, ServiceErrorHandling.ThrowOnError);
169  }
170
171  /**
172   * Gets the expected response message count.
173   *
174   * @return Number of expected response messages
175   */
176  @Override
177  protected int getExpectedResponseMessageCount() {
178    return 1;
179  }
180
181  /**
182   * Writes the attribute to XML.
183   *
184   * @param writer the writer
185   * @throws ServiceXmlSerializationException the service xml serialization exception
186   */
187  @Override
188  protected void writeAttributesToXml(EwsServiceXmlWriter writer)
189      throws ServiceXmlSerializationException {
190    writer.writeAttributeValue(XmlAttributeNames.ReturnFullContactData,
191        this.returnFullContactData);
192
193    String searchScope = null;
194    if (searchScopeMap.getMember().containsKey(searchLocation)) {
195      searchScope = searchScopeMap.getMember().get(searchLocation);
196    }
197
198    EwsUtilities
199        .ewsAssert((!(searchScope == null || searchScope.isEmpty())),
200                   "ResolveNameRequest.WriteAttributesToXml",
201                   "The specified search location cannot be mapped to an EWS search scope.");
202
203    String propertySet = null;
204    if (this.getContactDataPropertySet() != null) {
205      //((PropertyBag)PropertySet.getDefaultPropertySetDictionary( ).getMember()).tryGetValue(this.contactDataPropertySet.getBasePropertySet(),  propertySet);
206      if (PropertySet.getDefaultPropertySetMap().getMember()
207          .containsKey(this.getContactDataPropertySet().getBasePropertySet())) {
208        propertySet = PropertySet.getDefaultPropertySetMap().getMember()
209            .get(this.getContactDataPropertySet().getBasePropertySet());
210      }
211    }
212
213    if (!this.getService().getExchange2007CompatibilityMode()) {
214      writer.writeAttributeValue(XmlAttributeNames.
215          SearchScope, searchScope);
216    }
217    if (!(propertySet == null)) {
218      writer.writeAttributeValue(XmlAttributeNames.ContactDataShape, propertySet);
219    }
220  }
221
222  /**
223   * Writes the attribute to XML.
224   *
225   * @param writer the writer
226   * @throws Exception the exception
227   */
228  @Override
229  protected void writeElementsToXml(EwsServiceXmlWriter writer)
230      throws Exception {
231    this.getParentFolderIds().writeToXml(writer, XmlNamespace.Messages,
232        XmlElementNames.ParentFolderIds);
233
234    writer.writeElementValue(XmlNamespace.Messages,
235        XmlElementNames.UnresolvedEntry, this.getNameToResolve());
236  }
237
238  /**
239   * Gets the request version.
240   *
241   * @return Earliest Exchange version in which this request is supported.
242   */
243  @Override
244  protected ExchangeVersion getMinimumRequiredServerVersion() {
245    return ExchangeVersion.Exchange2007_SP1;
246  }
247
248  /**
249   * Gets the name to resolve.
250   *
251   * @return the name to resolve
252   */
253  public String getNameToResolve() {
254    return this.nameToResolve;
255  }
256
257  /**
258   * Sets the name to resolve.
259   *
260   * @param nameToResolve the new name to resolve
261   */
262  public void setNameToResolve(String nameToResolve) {
263    this.nameToResolve = nameToResolve;
264  }
265
266  /**
267   * Gets a value indicating whether to return full contact data or not.
268   * "true" if should return full contact data; otherwise, "false".
269   *
270   * @return the return full contact data
271   */
272  public boolean getReturnFullContactData() {
273    return this.returnFullContactData;
274  }
275
276  /**
277   * Sets the return full contact data.
278   *
279   * @param returnFullContactData the new return full contact data
280   */
281  public void setReturnFullContactData(boolean returnFullContactData) {
282    this.returnFullContactData = returnFullContactData;
283  }
284
285  /**
286   * Gets the search location.
287   *
288   * @return the search location
289   */
290  public ResolveNameSearchLocation getSearchLocation() {
291    return this.searchLocation;
292  }
293
294  /**
295   * Sets the search location.
296   *
297   * @param searchLocation the new search location
298   */
299  public void setSearchLocation(ResolveNameSearchLocation searchLocation) {
300    this.searchLocation = searchLocation;
301  }
302
303  /**
304   * Gets the parent folder ids.
305   *
306   * @return the parent folder ids
307   */
308  public FolderIdWrapperList getParentFolderIds() {
309    return this.parentFolderIds;
310  }
311
312  /**
313   * Gets or sets the PropertySet for Contact Data
314   * <p/>
315   * The PropertySet
316   */
317  public void setContactDataPropertySet(PropertySet propertySet) {
318
319
320    this.contactDataPropertySet = propertySet;
321  }
322
323  /**
324   * Gets or sets the PropertySet for Contact Data
325   *
326   * @return The PropertySet
327   */
328  public PropertySet getContactDataPropertySet() {
329    return this.contactDataPropertySet;
330  }
331
332}