Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
* Copyright (c) 2012 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package microsoft.exchange.webservices.data.core.service.item;
import microsoft.exchange.webservices.data.attribute.Attachable;
import microsoft.exchange.webservices.data.attribute.ServiceObjectDefinition;
import microsoft.exchange.webservices.data.core.EwsUtilities;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.XmlElementNames;
import microsoft.exchange.webservices.data.core.service.schema.ContactSchema;
import microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema;
import microsoft.exchange.webservices.data.core.enumeration.service.ContactSource;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.service.FileAsMapping;
import microsoft.exchange.webservices.data.core.enumeration.property.PhysicalAddressIndex;
import microsoft.exchange.webservices.data.core.exception.service.local.PropertyException;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
import microsoft.exchange.webservices.data.misc.OutParam;
import microsoft.exchange.webservices.data.property.complex.Attachment;
import microsoft.exchange.webservices.data.property.complex.ByteArrayArray;
import microsoft.exchange.webservices.data.property.complex.CompleteName;
import microsoft.exchange.webservices.data.property.complex.EmailAddress;
import microsoft.exchange.webservices.data.property.complex.EmailAddressCollection;
import microsoft.exchange.webservices.data.property.complex.EmailAddressDictionary;
import microsoft.exchange.webservices.data.property.complex.FileAttachment;
import microsoft.exchange.webservices.data.property.complex.ImAddressDictionary;
import microsoft.exchange.webservices.data.property.complex.ItemAttachment;
import microsoft.exchange.webservices.data.property.complex.ItemId;
import microsoft.exchange.webservices.data.property.complex.PhoneNumberDictionary;
import microsoft.exchange.webservices.data.property.complex.PhysicalAddressDictionary;
import microsoft.exchange.webservices.data.property.complex.StringList;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
/**
* Represents a contact. Properties available on contacts are defined in the
* ContactSchema class.
*/
@Attachable
@ServiceObjectDefinition(xmlElementName = XmlElementNames.Contact, returnedByServer = true)
public class Contact extends Item {
/**
* The Contact picture name.
*/
private final String ContactPictureName = "ContactPicture.jpg";
/**
* Initializes an unsaved local instance of {@link Contact}.
* To bind to an existing contact, use Contact.Bind() instead.
*
* @param service the service
* @throws Exception the exception
*/
public Contact(ExchangeService service) throws Exception {
super(service);
}
/**
* Initializes a new instance of the {@link Contact} class.
*
* @param parentAttachment the parent attachment
* @throws Exception the exception
*/
public Contact(ItemAttachment parentAttachment) throws Exception {
super(parentAttachment);
}
/**
* Binds to an existing contact and loads the specified set of property.
* Calling this method results in a call to EWS.
*
* @param service the service
* @param id the id
* @param propertySet the property set
* @return A Contact instance representing the contact corresponding to the
* specified Id.
* @throws Exception the exception
*/
public static Contact bind(ExchangeService service, ItemId id,
PropertySet propertySet) throws Exception {
return service.bindToItem(Contact.class, id, propertySet);
}
/**
* Binds to an existing contact and loads its first class property.
* Calling this method results in a call to EWS.
*
* @param service the service
* @param id the id
* @return A Contact instance representing the contact corresponding to the
* specified Id.
* @throws Exception the exception
*/
public static Contact bind(ExchangeService service, ItemId id)
throws Exception {
return Contact.bind(service, id, PropertySet.getFirstClassProperties());
}
/**
* Internal method to return the schema associated with this type of object.
*
* @return The schema associated with this type of object.
*/
@Override public ServiceObjectSchema getSchema() {
return ContactSchema.Instance;
}
/**
* Gets the minimum required server version.
*
* @return Earliest Exchange version in which this service object type is
* supported.
*/
@Override public ExchangeVersion getMinimumRequiredServerVersion() {
return ExchangeVersion.Exchange2007_SP1;
}
/**
* Sets the contact's picture using the specified byte array.
*
* @param content the new contact picture
* @throws Exception the exception
*/
public void setContactPicture(byte[] content) throws Exception {
EwsUtilities.validateMethodVersion(this.getService(), ExchangeVersion.Exchange2010, "SetContactPicture");
internalRemoveContactPicture();
FileAttachment fileAttachment = getAttachments().addFileAttachment(
ContactPictureName, content);
fileAttachment.setIsContactPhoto(true);
}
/**
* Sets the contact's picture using the specified stream.
*
* @param contentStream the new contact picture
* @throws Exception the exception
*/
public void setContactPicture(InputStream contentStream) throws Exception {
EwsUtilities.validateMethodVersion(this.getService(),
ExchangeVersion.Exchange2010, "SetContactPicture");
internalRemoveContactPicture();
FileAttachment fileAttachment = getAttachments().addFileAttachment(
ContactPictureName, contentStream);
fileAttachment.setIsContactPhoto(true);
}
/**
* Sets the contact's picture using the specified file.
*
* @param fileName the new contact picture
* @throws Exception the exception
*/
public void setContactPicture(String fileName) throws Exception {
EwsUtilities.validateMethodVersion(this.getService(),
ExchangeVersion.Exchange2010, "SetContactPicture");
internalRemoveContactPicture();
FileAttachment fileAttachment = getAttachments().addFileAttachment(
new File(fileName).getName(), fileName);
fileAttachment.setIsContactPhoto(true);
}
/**
* Retrieves the file attachment that holds the contact's picture.
*
* @return The file attachment that holds the contact's picture.
* @throws ServiceLocalException the service local exception
*/
public FileAttachment getContactPictureAttachment()
throws ServiceLocalException {
EwsUtilities.validateMethodVersion(this.getService(),
ExchangeVersion.Exchange2010, "GetContactPictureAttachment");
if (!this.getPropertyBag().isPropertyLoaded(ContactSchema.Attachments)) {
throw new PropertyException("The attachment collection must be loaded.");
}
for (Attachment fileAttachment : this.getAttachments()) {
if (fileAttachment instanceof FileAttachment) {
if (((FileAttachment) fileAttachment).isContactPhoto()) {
return (FileAttachment) fileAttachment;
}
}
}
return null;
}
/**
* Removes the picture from local attachment collection.
*
* @throws Exception the exception
*/
private void internalRemoveContactPicture() throws Exception {
// Iterates in reverse order to remove file attachments that have
// IsContactPhoto set to true.
for (int index = this.getAttachments().getCount() - 1; index >= 0; index--) {
FileAttachment fileAttachment = (FileAttachment) this
.getAttachments().getPropertyAtIndex(index);
if (fileAttachment != null) {
if (fileAttachment.isContactPhoto()) {
this.getAttachments().remove(fileAttachment);
}
}
}
}
/**
* Removes the contact's picture.
*
* @throws Exception the exception
*/
public void removeContactPicture() throws Exception {
EwsUtilities.validateMethodVersion(this.getService(),
ExchangeVersion.Exchange2010, "RemoveContactPicture");
if (!this.getPropertyBag().isPropertyLoaded(ContactSchema.Attachments)) {
throw new PropertyException("The attachment collection must be loaded.");
}
internalRemoveContactPicture();
}
/**
* Validates this instance.
*
* @throws ServiceVersionException the service version exception
* @throws Exception the exception
*/
@Override public void validate() throws ServiceVersionException, Exception {
super.validate();
Object fileAsMapping;
OutParam