All Downloads are FREE. Search and download functionalities are using the official Maven repository.

microsoft.exchange.webservices.data.Attachment Maven / Gradle / Ivy

Go to download

The source came from http://archive.msdn.microsoft.com/ewsjavaapi Support for Maven has been added.

The newest version!
/**************************************************************************
 * copyright file="Attachment.java" company="Microsoft"
 *     Copyright (c) Microsoft Corporation.  All rights reserved.
 * 
 * Defines the Attachment.java.
 **************************************************************************/
package microsoft.exchange.webservices.data;

import java.util.Date;

/***
 * Represents an attachment to an item.
 * 
 * 
 */
public abstract class Attachment extends ComplexProperty {

	/** The owner. */
	private Item owner;

	/** The id. */
	private String id;

	/** The name. */
	private String name;

	/** The content type. */
	private String contentType;

	/** The content id. */
	private String contentId;

	/** The content location. */
	private String contentLocation;

	/** The size. */
	private int size;

	/** The last modified time. */
	private Date lastModifiedTime;

	/** The is inline. */
	private boolean isInline;

	/**
	 * * Initializes a new instance.
	 * 
	 * @param owner
	 *            The owner.
	 */
	protected Attachment(Item owner) {
		this.owner = owner;
	}

	/***
	 * Throws exception if this is not a new service object.
	 */
	protected void throwIfThisIsNotNew() {
		if (!this.isNew()) {
			throw new UnsupportedOperationException(
					Strings.AttachmentCannotBeUpdated);
		}
	}

	/**
	 * Sets value of field.
	 * 
	 * We override the base implementation. Attachments cannot be modified so
	 * any attempts the change a property on an existing attachment is an error.
	 * 
	 * @param 
	 *            the generic type
	 * @param field
	 *            The field
	 * @param value
	 *            The value.
	 * @return true, if successful
	 */
	protected  boolean canSetFieldValue(T field, T value) {
		this.throwIfThisIsNotNew();
		return super.canSetFieldValue(field, value);
	};

	/**
	 * Gets the Id of the attachment.
	 * 
	 * @return the id
	 */
	public String getId() {
		return this.id;
	}

	/**
	 * Gets the name of the attachment.
	 * 
	 * @return the name
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param value
	 *            the new name
	 */
	public void setName(String value) {
		if (this.canSetFieldValue(this.name, value)) {
			this.name = value;
			this.changed();
		}
	}

	/**
	 * Gets  the content type of the attachment.
	 * 
	 * @return the content type
	 */
	public String getContentType() {
		return this.contentType;
	}

	/**
	 * Sets the content type.
	 * 
	 * @param value
	 *            the new content type
	 */
	public void setContentType(String value) {
		if (this.canSetFieldValue(this.contentType, value)) {
			this.contentType = value;
			this.changed();
		}
	}

	/**
	 * Gets  the content Id of the attachment. ContentId can be used as a
	 * custom way to identify an attachment in order to reference it from within
	 * the body of the item the attachment belongs to.
	 * 
	 * @return the content id
	 */
	public String getContentId() {
		return this.contentId;
	}

	/**
	 * Sets the content id.
	 * 
	 * @param value
	 *            the new content id
	 */
	public void setContentId(String value) {
		if (this.canSetFieldValue(this.contentId, value)) {
			this.contentId = value;
			this.changed();
		}
	}

	/**
	 * Gets  the content location of the attachment. ContentLocation can
	 * be used to associate an attachment with a Url defining its location on
	 * the Web.
	 * 
	 * @return the content location
	 */
	public String getContentLocation() {
		return this.contentLocation;
	}

	/**
	 * Sets the content location.
	 * 
	 * @param value
	 *            the new content location
	 */
	public void setContentLocation(String value) {
		if (this.canSetFieldValue(this.contentLocation, value)) {
			this.contentLocation = value;
			this.changed();
		}
	}

	/**
	 * Gets the size of the attachment.
	 *
	 * @return the size
	 * @throws ServiceVersionException
	 *             throws ServiceVersionException
	 */
	public int getSize() throws ServiceVersionException {
		EwsUtilities.validatePropertyVersion(this.getOwner().getService(),
				ExchangeVersion.Exchange2010, "Size");
		return this.size;
	}

	/**
	 * Gets the date and time when this attachment was last modified.
	 *
	 * @return the last modified time
	 * @throws ServiceVersionException the service version exception
	 */
	public Date getLastModifiedTime() throws ServiceVersionException {

		EwsUtilities.validatePropertyVersion(this.getOwner().getService(),
				ExchangeVersion.Exchange2010, "LastModifiedTime");

		return this.lastModifiedTime;

	}

	/**
	 * Gets  a value indicating whether this is an inline attachment.
	 * Inline attachments are not visible to end users.
	 *
	 * @return the checks if is inline
	 * @throws ServiceVersionException the service version exception
	 */
	public boolean getIsInline() throws ServiceVersionException {
		EwsUtilities.validatePropertyVersion(this.getOwner().getService(),
				ExchangeVersion.Exchange2010, "IsInline");
		return this.isInline;

	}

	/**
	 * Sets the checks if is inline.
	 *
	 * @param value 
	 *			  the new checks if is inline
	 * @throws ServiceVersionException 
	 * 			  the service version exception
	 */
	public void setIsInline(boolean value) throws ServiceVersionException {
		EwsUtilities.validatePropertyVersion(this.getOwner().getService(),
				ExchangeVersion.Exchange2010, "IsInline");
		if (this.canSetFieldValue(this.isInline, value)) {
			this.isInline = value;
			this.changed();
		}
	}

	/**
	 * True if the attachment has not yet been saved, false otherwise.
	 * 
	 * @return true, if is new
	 */
	protected boolean isNew() {
		return (this.getId() == null || this.getId().isEmpty());
	}

	/**
	 * Gets the owner of the attachment.
	 * 
	 * @return the owner
	 */
	protected Item getOwner() {
		return this.owner;
	}

	/**
	 * Gets the name of the XML element.
	 * 
	 * @return XML element name.
	 */
	abstract String getXmlElementName();

	/**
	 * Tries to read element from XML.
	 * 
	 * @param reader
	 *            The reader.
	 * @return True if element was read.
	 * @throws Exception
	 *             the exception
	 */
	@Override
	protected boolean tryReadElementFromXml(EwsServiceXmlReader reader)
			throws Exception {

		try {
			if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.AttachmentId)) {
				try {
					this.id = reader.readAttributeValue(XmlAttributeNames.Id);
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
				if (this.getOwner() != null) {
					String rootItemChangeKey = reader
							.readAttributeValue(XmlAttributeNames.
									RootItemChangeKey);
					if (null != rootItemChangeKey &&
							 !rootItemChangeKey.isEmpty()) {
						this.getOwner().getRootItemId().setChangeKey(
								rootItemChangeKey);
					}
				}
				reader.readEndElementIfNecessary(XmlNamespace.Types,
						XmlElementNames.AttachmentId);
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.Name)) {
				this.name = reader.readElementValue();
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.ContentType)) {
				this.contentType = reader.readElementValue();
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.ContentId)) {
				this.contentId = reader.readElementValue();
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.ContentLocation)) {
				this.contentLocation = reader.readElementValue();
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.Size)) {
				this.size = reader.readElementValue(Integer.class);
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.LastModifiedTime)) {
				this.lastModifiedTime = reader.readElementValueAsDateTime();
				return true;
			} else if (reader.getLocalName().equalsIgnoreCase(
					XmlElementNames.IsInline)) {
				this.isInline = reader.readElementValue(Boolean.class);
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Writes elements to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @throws Exception
	 *             the exception
	 */
	@Override
	protected void writeElementsToXml(EwsServiceXmlWriter writer)
			throws Exception {
		writer.writeElementValue(XmlNamespace.Types, XmlElementNames.Name, this
				.getName());
		writer.writeElementValue(XmlNamespace.Types,
				XmlElementNames.ContentType, this.getContentType());
		writer.writeElementValue(XmlNamespace.Types, XmlElementNames.ContentId,
				this.getContentId());
		writer.writeElementValue(XmlNamespace.Types,
				XmlElementNames.ContentLocation, this.getContentLocation());
		if (writer.getService().getRequestedServerVersion().ordinal() > 
		ExchangeVersion.Exchange2007_SP1
				.ordinal()) {
			writer.writeElementValue(XmlNamespace.Types,
					XmlElementNames.IsInline, this.getIsInline());
		}
	}

	/**
	 * Load the attachment.
	 * 
	 * @param bodyType
	 *            Type of the body.
	 * @param additionalProperties
	 *            The additional properties.
	 * @throws Exception
	 *             the exception
	 */
	protected void internalLoad(BodyType bodyType,
			Iterable additionalProperties)
			throws Exception {
		this.getOwner().getService().getAttachment(this, bodyType,
				additionalProperties);
	}

	/**
	 * Validates this instance.
	 * 
	 * @param attachmentIndex
	 *            Index of this attachment.
	 * @throws ServiceValidationException
	 *             the service validation exception
	 * @throws Exception
	 *             the exception
	 */
	void validate(int attachmentIndex) throws ServiceValidationException,
			Exception {
	}

	/**
	 * Loads the attachment. Calling this method results in a call to EWS.
	 * 
	 * @throws Exception
	 *             the exception
	 */
	public void load() throws Exception {
		this.internalLoad(null, null);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy