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

microsoft.exchange.webservices.data.GroupMemberCollection 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="GroupMemberCollection.java" company="Microsoft"
 *     Copyright (c) Microsoft Corporation.  All rights reserved.
 * 
 * Defines the GroupMemberCollection.java.
 **************************************************************************/
package microsoft.exchange.webservices.data;

import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLStreamException;

/***
 * Represents a collection of members of GroupMember type.
 */
public final class GroupMemberCollection extends
ComplexPropertyCollection implements
ICustomXmlUpdateSerializer {
	/***
	 * If the collection is cleared, then store PDL members collection is
	 * updated with "SetItemField". If the collection is not cleared, then store
	 * PDL members collection is updated with "AppendToItemField".
	 */
	private boolean collectionIsCleared = false;

	/**
	 * * Initializes a new instance.
	 */
	public GroupMemberCollection() {
		super();
	}

	/**
	 * * Retrieves the XML element name corresponding to the provided
	 * GroupMember object.
	 * 
	 * @param member
	 *            the member
	 * @return The XML element name corresponding to the provided GroupMember
	 *         object
	 */
	@Override
	protected String getCollectionItemXmlElementName(GroupMember member) {
		return XmlElementNames.Member;
	}

	/**
	 ** Finds the member with the specified key in the collection.Members that
	 * have not yet been saved do not have a key.
	 * 
	 * @param key
	 *            the key
	 * @return The member with the specified key
	 * @throws Exception
	 *             the exception
	 */
	public GroupMember find(String key) throws Exception {
		EwsUtilities.validateParam(key, "key");

		for (GroupMember item : this.getItems()) {
			if (item.getKey().equals(key)) {
				return item;
			}
		}

		return null;
	}

	/***
	 *Clears the collection.
	 */
	public void clear() {
		// mark the whole collection for deletion
		this.internalClear();
		this.collectionIsCleared = true;
	}

	/**
	 * * Adds a member to the collection.
	 * 
	 * @param member
	 *            the member
	 * @throws Exception
	 *             the exception
	 */
	public void add(GroupMember member) throws Exception {
		EwsUtilities.validateParam(member, "member");
		EwsUtilities.EwsAssert(member.getKey() == null,
				"GroupMemberCollection.Add", "member.Key is not null.");
		EwsUtilities.EwsAssert(!this.contains(member),
				"GroupMemberCollection.Add",
		"The member is already in the collection");

		this.internalAdd(member);
	}

	/**
	 * * Adds multiple members to the collection.
	 * 
	 * @param members
	 *            the members
	 * @throws Exception
	 *             the exception
	 */
	public void addRange(Iterator members) throws Exception {
		EwsUtilities.validateParam(members, "members");
		while (members.hasNext()) {
			this.add((GroupMember)members.next());

		}
	}

	/**
	 * * Adds a member linked to a Contact Group.
	 * 
	 * @param contactGroupId
	 *            the contact group id
	 * @throws Exception
	 *             the exception
	 */
	public void addContactGroup(ItemId contactGroupId) throws Exception {
		this.add(new GroupMember(contactGroupId));
	}

	/**
	 * * Adds a member linked to a specific contact?s e-mail address.
	 * 
	 * @param contactId
	 *            the contact id
	 * @param addressToLink
	 *            the address to link
	 * @throws Exception
	 *             the exception
	 */
	public void addPersonalContact(ItemId contactId, String addressToLink)
	throws Exception {
		this.add(new GroupMember(contactId, addressToLink));
	}

	/**
	 * * Adds a member linked to a contact?s first available e-mail address.
	 * 
	 * @param contactId
	 *            the contact id
	 * @throws Exception
	 *             the exception
	 */
	public void addPersonalContact(ItemId contactId) throws Exception {
		this.addPersonalContact(contactId, null);
	}

	/**
	 * * Adds a member linked to an Active Directory user.
	 * 
	 * @param smtpAddress
	 *            the smtp address
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addDirectoryUser(String smtpAddress)
	throws ServiceLocalException, Exception {
		this.addDirectoryUser(smtpAddress, new EmailAddress()
		.getSmtpRoutingType());
	}

	/**
	 * * Adds a member linked to an Active Directory user.
	 * 
	 * @param address
	 *            the address
	 * @param routingType
	 *            the routing type
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addDirectoryUser(String address, String routingType)
	throws ServiceLocalException, Exception {
		this.add(new GroupMember(address, routingType, MailboxType.Mailbox));
	}

	/**
	 * * Adds a member linked to an Active Directory contact.
	 * 
	 * @param smtpAddress
	 *            the smtp address
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addDirectoryContact(String smtpAddress)
	throws ServiceLocalException, Exception {
		this.addDirectoryContact(smtpAddress, new EmailAddress()
		.getSmtpRoutingType());
	}

	/**
	 * * Adds a member linked to an Active Directory contact.
	 * 
	 * @param address
	 *            the address
	 * @param routingType
	 *            the routing type
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addDirectoryContact(String address, String routingType)
	throws ServiceLocalException, Exception {
		this.add(new GroupMember(address, routingType, MailboxType.Contact));
	}

	/**
	 * * Adds a member linked to a Public Group.
	 * 
	 * @param smtpAddress
	 *            the smtp address
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addPublicGroup(String smtpAddress)
	throws ServiceLocalException, Exception {
		this.add(new GroupMember(smtpAddress, new EmailAddress()
		.getSmtpRoutingType(), MailboxType.PublicGroup));
	}

	/**
	 * * Adds a member linked to a mail-enabled Public Folder.
	 * 
	 * @param smtpAddress
	 *            the smtp address
	 * @throws ServiceLocalException
	 *             the service local exception
	 * @throws Exception
	 *             the exception
	 */
	public void addDirectoryPublicFolder(String smtpAddress)
	throws ServiceLocalException, Exception {
		this.add(new GroupMember(smtpAddress, new EmailAddress()
		.getSmtpRoutingType(), MailboxType.PublicFolder));
	}

	/**
	 * * Adds a one-off member.
	 * 
	 * @param displayName
	 *            the display name
	 * @param address
	 *            the address
	 * @param routingType
	 *            the routing type
	 * @throws Exception
	 *             the exception
	 */
	public void addOneOff(String displayName, 
			String address, String routingType)
	throws Exception {
		this.add(new GroupMember(displayName, address, routingType));
	}

	/**
	 * * Adds a one-off member.
	 * 
	 * @param displayName
	 *            the display name
	 * @param smtpAddress
	 *            the smtp address
	 * @throws Exception
	 *             the exception
	 */
	public void addOneOff(String displayName, String smtpAddress)
	throws Exception {
		this.addOneOff(displayName, smtpAddress, new EmailAddress()
		.getSmtpRoutingType());
	}

	/**
	 * * Adds a member that is linked to a specific e-mail address of a contact.
	 * 
	 * @param contact
	 *            the contact
	 * @param emailAddressKey
	 *            the email address key
	 * @throws Exception
	 *             the exception
	 */
	public void addContactEmailAddress(Contact contact,
			EmailAddressKey emailAddressKey) throws Exception {
		this.add(new GroupMember(contact, emailAddressKey));
	}

	/**
	 * * Removes a member at the specified index.
	 * 
	 * @param index
	 *            the index
	 */
	public void removeAt(int index) {
		if (index < 0 || index >= this.getCount()) {
			throw new IllegalArgumentException("index", new Throwable(
					Strings.IndexIsOutOfRange));

		}

		this.internalRemoveAt(index);
	}

	/**
	 * * Removes a member from the collection.
	 * 
	 * @param member
	 *            the member
	 * @return True if the group member was successfully removed from the
	 *         collection, false otherwise.
	 */
	public boolean remove(GroupMember member) {
		return this.internalRemove(member);
	}

	/**
	 * * Writes the update to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @param ownerObject
	 *            the owner object
	 * @param propertyDefinition
	 *            the property definition
	 * @return True if property generated serialization.
	 * @throws Exception
	 *             the exception
	 */
	public boolean writeSetUpdateToXml(EwsServiceXmlWriter writer,
			ServiceObject ownerObject, PropertyDefinition propertyDefinition)
	throws Exception {
		if (this.collectionIsCleared) {

			if (!this.getAddedItems().isEmpty()) { // not visible

				// Delete the whole members collection
				this.writeDeleteMembersCollectionToXml(writer);
			} else {
				// The collection is cleared, so Set
				this.writeSetOrAppendMembersToXml(writer, this.getAddedItems(),
						true);
			}
		} else {
			// The collection is not cleared, i.e. dl.Members.Clear() is not
			// called.
			// Append AddedItems.
			this.writeSetOrAppendMembersToXml(writer, this.getAddedItems(),
					false);

			// Since member replacement is not supported by server
			// Delete old ModifiedItems, then recreate new instead.
			this.writeDeleteMembersToXml(writer, this.getModifiedItems());
			this.writeSetOrAppendMembersToXml(writer, this.getModifiedItems(),
					false);

			// Delete RemovedItems.
			this.writeDeleteMembersToXml(writer, this.getRemovedItems());
		}

		return true;
	}

	/**
	 * * Writes the deletion update to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @param ewsObject
	 *            the ews object
	 * @return True if property generated serialization.
	 */
	public boolean writeDeleteUpdateToXml(EwsServiceXmlWriter writer,
			ServiceObject ewsObject) {
		return false;
	}

	/**
	 * * Creates a GroupMember object from an XML element name.
	 * 
	 * @param xmlElementName
	 *            the xml element name
	 * @return An GroupMember object
	 */
	protected GroupMember createComplexProperty(String xmlElementName) {
		return new GroupMember();
	}

	/***
	 *Clears the change log.
	 */
	protected void clearChangeLog() {
		super.clearChangeLog();
		this.collectionIsCleared = false;
	}

	/**
	 * * Delete the whole members collection.
	 * 
	 * @param writer
	 *            the writer
	 * @throws XMLStreamException
	 *             the xML stream exception
	 * @throws ServiceXmlSerializationException
	 *             the service xml serialization exception
	 */
	private void writeDeleteMembersCollectionToXml(EwsServiceXmlWriter writer)
	throws XMLStreamException, ServiceXmlSerializationException {
		writer.writeStartElement(XmlNamespace.Types,
				XmlElementNames.DeleteItemField);
		ContactGroupSchema.Members.writeToXml(writer);
		writer.writeEndElement();
	}

	/**
	 * * Generate XML to delete individual members.
	 * 
	 * @param writer
	 *            the writer
	 * @param members
	 *            the members
	 * @throws XMLStreamException
	 *             the xML stream exception
	 * @throws ServiceXmlSerializationException
	 *             the service xml serialization exception
	 */
	private void writeDeleteMembersToXml(EwsServiceXmlWriter writer,
			List members) throws XMLStreamException,
			ServiceXmlSerializationException {
		if (!members.isEmpty()) {
			GroupMemberPropertyDefinition memberPropDef = 
				new GroupMemberPropertyDefinition();

			for (GroupMember member : members) {
				writer.writeStartElement(XmlNamespace.Types,
						XmlElementNames.DeleteItemField);

				memberPropDef.setKey(member.getKey());
				memberPropDef.writeToXml(writer);

				writer.writeEndElement(); // DeleteItemField
			}
		}
	}

	/**
	 * Write set or append members to xml.
	 * 
	 * @param writer
	 *            the writer
	 * @param members
	 *            the members
	 * @param setMode
	 *            the set mode
	 * @throws Exception
	 *             the exception
	 */
	private void writeSetOrAppendMembersToXml(EwsServiceXmlWriter writer,
			List members, boolean setMode) throws Exception {
		if (!members.isEmpty()) {
			writer.writeStartElement(XmlNamespace.Types,
					setMode ? XmlElementNames.SetItemField
							: XmlElementNames.AppendToItemField);

			ContactGroupSchema.Members.writeToXml(writer);

			writer.writeStartElement(XmlNamespace.Types,
					XmlElementNames.DistributionList);
			writer.writeStartElement(XmlNamespace.Types,
					XmlElementNames.Members);

			for (GroupMember member : members) {
				member.writeToXml(writer, XmlElementNames.Member);
			}

			writer.writeEndElement(); // Members
			writer.writeEndElement(); // Group
			writer.writeEndElement(); // setMode ? SetItemField :
			// AppendItemField
		}
	}

	/**
	 * Validates this instance.
	 * @throws Exception 
	 */
	@Override
	protected void internalValidate() throws Exception {
		super.internalValidate();

		for(GroupMember groupMember : this.getModifiedItems()) {
			if(!(groupMember.getKey()==null || groupMember.getKey().isEmpty())) {
				throw new ServiceValidationException(Strings.
						ContactGroupMemberCannotBeUpdatedWithoutBeingLoadedFirst);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy