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

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Represents a generic dictionary that can be sent to or retrieved from EWS.
 * TKey The type of key. TEntry The type of entry.
 * 
 * @param 
 *            the generic type
 * @param 
 *            the generic type
 */
@EditorBrowsable(state = EditorBrowsableState.Never)
public abstract class DictionaryProperty
		>
		extends ComplexProperty implements ICustomXmlUpdateSerializer,
		IComplexPropertyChangedDelegate {

	/** The entries. */
	private Map entries = new HashMap();

	/** The removed entries. */
	private Map removedEntries = new HashMap();

	/** The added entries. */
	private List addedEntries = new ArrayList();

	/** The modified entries. */
	private List modifiedEntries = new ArrayList();

	/**
	 * * Entry was changed.
	 * 
	 * @param complexProperty
	 *            the complex property
	 */
	private void entryChanged(ComplexProperty complexProperty) {
		TKey key = ((TEntry)complexProperty).getKey();

		if (!this.addedEntries.contains(key) && !this.modifiedEntries.contains(key)) {
			this.modifiedEntries.add(key);
			this.changed();
		}
	}

	/**
	 * * Writes the URI to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @param key
	 *            the key
	 * @throws Exception
	 *             the exception
	 */
	private void writeUriToXml(EwsServiceXmlWriter writer, TKey key)
			throws Exception {
		writer.writeStartElement(XmlNamespace.Types,
				XmlElementNames.IndexedFieldURI);
		writer.writeAttributeValue(XmlAttributeNames.FieldURI, this
				.getFieldURI());
		writer.writeAttributeValue(XmlAttributeNames.FieldIndex, this
				.getFieldIndex(key));
		writer.writeEndElement();
	}

	/**
	 * * Gets the index of the field.
	 * 
	 * @param key
	 *            the key
	 * @return Key index.
	 */
	protected String getFieldIndex(TKey key) {
		return key.toString();
	}

	/***
	 * Gets the field URI.
	 * 
	 * @return Field URI.
	 */
	protected String getFieldURI() {
		return null;
	}

	/**
	 * * Creates the entry.
	 * 
	 * @param reader
	 *            the reader
	 * @return Dictionary entry.
	 */
	protected TEntry createEntry(EwsServiceXmlReader reader) {
		if (reader.getLocalName().equalsIgnoreCase(XmlElementNames.Entry)) {
			return this.createEntryInstance();
		} else {
			return null;
		}
	}

	/***
	 * Creates instance of dictionary entry.
	 * 
	 * @return New instance.
	 */
	protected abstract TEntry createEntryInstance();

	/**
	 * * Gets the name of the entry XML element.
	 * 
	 * @param entry
	 *            the entry
	 * @return XML element name.
	 */
	protected String getEntryXmlElementName(TEntry entry) {
		return XmlElementNames.Entry;
	}

	/***
	 * Clears the change log.
	 */
	protected void clearChangeLog() {
		this.addedEntries.clear();
		this.removedEntries.clear();
		this.modifiedEntries.clear();

		for (TEntry entry : this.entries.values()) {
			entry.clearChangeLog();
		}
	}

	/**
	 * * Add entry.
	 * 
	 * @param entry
	 *            the entry
	 */
	protected void internalAdd(TEntry entry) {
		entry.addOnChangeEvent(this);
		
		this.entries.put(entry.getKey(), entry);
		this.addedEntries.add(entry.getKey());
		this.removedEntries.remove(entry.getKey());

		this.changed();
	}

	/**
	 * Complex property changed.
	 * 
	 * @param complexProperty
	 *            accepts ComplexProperty
	 */
	@Override
	public void complexPropertyChanged(ComplexProperty complexProperty) {
		this.entryChanged(complexProperty);
	}

	/**
	 * * Add or replace entry.
	 * 
	 * @param entry
	 *            the entry
	 */
	protected void internalAddOrReplace(TEntry entry) {
		TEntry oldEntry;
		if (this.entries.containsKey(entry.getKey())) {
			oldEntry = this.entries.get(entry.getKey());
			oldEntry.removeChangeEvent(this);

			entry.addOnChangeEvent(this);

			if (!this.addedEntries.contains(entry.getKey())) {
				if (!this.modifiedEntries.contains(entry.getKey())) {
					this.modifiedEntries.add(entry.getKey());
				}
			}

			this.changed();
		} else {
			this.internalAdd(entry);
		}
	}

	/**
	 * * Remove entry based on key.
	 * 
	 * @param key
	 *            the key
	 */
	protected void internalRemove(TKey key) {
		TEntry entry;
		if (this.entries.containsKey(key)) {
			entry = this.entries.get(key);
			entry.removeChangeEvent(this);

			this.entries.remove(key);
			this.removedEntries.put(key, entry);

			this.changed();
		}

		this.addedEntries.remove(key);
	}

	/**
	 * * Loads from XML.
	 * 
	 * @param reader
	 *            the reader
	 * @param localElementName
	 *            the local element name
	 * @throws Exception
	 *             the exception
	 */
	protected void loadFromXml(EwsServiceXmlReader reader,
			String localElementName) throws Exception {
		reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
				localElementName);

		if (!reader.isEmptyElement()) {
			do {
				reader.read();

				if (reader.isStartElement()) {
					TEntry entry = this.createEntry(reader);

					if (entry != null) {
						entry.loadFromXml(reader, reader.getLocalName());
						this.internalAdd(entry);
					} else {
						reader.skipCurrentElement();
					}
				}
			} while (!reader.isEndElement(XmlNamespace.Types,
					localElementName));
		} else {
			reader.read();
		}
	}

	/**
	 * Writes to XML.
	 * @param writer The writer
	 * @param xmlNamespace The XML namespace.          
	 * @param xmlElementName Name of the XML element.	 
	 * @throws Exception 	 
	 */
	@Override
	protected  void writeToXml(EwsServiceXmlWriter writer,
			XmlNamespace xmlNamespace,
            String xmlElementName) throws Exception {
            //  Only write collection if it has at least one element.
            if (this.entries.size() > 0) {
            	super.writeToXml(
                    writer,
                    xmlNamespace,
                    xmlElementName);
            }
        }
	
	/***
	 * Writes elements to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @throws Exception
	 *             the exception
	 */
	protected void writeElementsToXml(EwsServiceXmlWriter writer)
			throws Exception {
		for (Entry keyValuePair : this.entries.entrySet()) {
			keyValuePair.getValue().writeToXml(writer,
					this.getEntryXmlElementName(keyValuePair.getValue()));
		}
	}

	/***
	 * Gets the entries.
	 * 
	 * @return The entries.
	 */
	protected Map getEntries() {
		return entries;
	}

	/**
	 * * Determines whether this instance contains the specified key.
	 * 
	 * @param key
	 *            the key
	 * @return true if this instance contains the specified key; otherwise,
	 *         false.
	 */
	public boolean contains(TKey key) {
		return this.entries.containsKey(key);
	}

	/**
	 * * Writes updates to XML.
	 * 
	 * @param writer
	 *            the writer
	 * @param ewsObject
	 *            the ews object
	 * @param propertyDefinition
	 *            the property definition
	 * @return True if property generated serialization.
	 * @throws Exception
	 *             the exception
	 */
	public boolean writeSetUpdateToXml(EwsServiceXmlWriter writer,
			ServiceObject ewsObject, PropertyDefinition propertyDefinition)
			throws Exception {
		List tempEntries = new ArrayList();

		for (TKey key : this.addedEntries) {
			tempEntries.add(this.entries.get(key));
		}
		for (TKey key : this.modifiedEntries) {
			tempEntries.add(this.entries.get(key));
		}
		for (TEntry entry : tempEntries) {

			if (!entry.writeSetUpdateToXml(writer, ewsObject,
					propertyDefinition.getXmlElement())) {
				writer.writeStartElement(XmlNamespace.Types, ewsObject
						.getSetFieldXmlElementName());
				this.writeUriToXml(writer, entry.getKey());

				writer.writeStartElement(XmlNamespace.Types, ewsObject
						.getXmlElementName());
				//writer.writeStartElement(XmlNamespace.Types, propertyDefinition.getXmlElementName());
				writer.writeStartElement(XmlNamespace.Types, propertyDefinition.getXmlElement());
				entry.writeToXml(writer, this.getEntryXmlElementName(entry));
				writer.writeEndElement();
				writer.writeEndElement();

				writer.writeEndElement();
			}
		}

		for (TEntry entry : this.removedEntries.values()) {
			if (!entry.writeDeleteUpdateToXml(writer, ewsObject)) {
				writer.writeStartElement(XmlNamespace.Types, ewsObject
						.getDeleteFieldXmlElementName());
				this.writeUriToXml(writer, entry.getKey());
				writer.writeEndElement();
			}
		}

		return true;
	}

	/**
	 * * Writes 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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy