package com.adaptris.core.services.aggregator;

import com.adaptris.annotation.AdvancedConfig;
import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.CoreException;
import com.adaptris.core.MultiPayloadAdaptrisMessage;
import com.adaptris.core.ServiceException;
import com.adaptris.core.util.Args;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collection;

/**
 * Combine multiple standard Adaptris messages into a single
 * multi-payload Adaptris message.
 */
public class MultiPayloadMessageAggregator extends MessageAggregatorImpl
{
	private static final transient Logger log = LoggerFactory.getLogger(MultiPayloadMessageAggregator.class);

	@NotNull
	@Valid
	@AdvancedConfig
	private Boolean replaceOriginalMessage = true;

	/**
	 * Set whether to replace the original multi-payload message
	 * payload. If true then the original message will only
	 * contain the payloads from the collection of messages,
	 * otherwise it will append the collection of messages while
	 * maintaining the original message payload.
	 *
	 * @param replaceOriginalMessage Whether to replace the original message payload.
	 */
	public void setReplaceOriginalMessage(Boolean replaceOriginalMessage)
	{
		this.replaceOriginalMessage = Args.notNull(replaceOriginalMessage, "replaceOriginalMessage");
	}

	/**
	 * Get whether to replace the original multi-payload message
	 * payload. If true then the original message will only
	 * contain the payloads from the collection of messages,
	 * otherwise it will append the collection of messages while
	 * maintaining the original message payload.
	 *
	 * @return Whether to replace the original message payload.
	 */
	public Boolean getReplaceOriginalMessage()
	{
		return replaceOriginalMessage;
	}

	/**
	 * Joins multiple {@link AdaptrisMessage}s into a single MultiPayloadAdaptrisMessage object.
	 *
	 * @param original  The message to insert all the messages into.
	 * @param messages The list of messages to join.
	 * @throws CoreException Wrapping any other exception
	 */
	@Override
	public void joinMessage(AdaptrisMessage original, Collection<AdaptrisMessage> messages) throws CoreException
	{
		if (!(original instanceof MultiPayloadAdaptrisMessage))
		{
			throw new ServiceException("Multi-payload message aggregator cannot merge multiple messages into a message that isn't a multi-payload adaptris message!");
		}

		log.info("Adding messages to existing message [{}]", original.getUniqueId());

		MultiPayloadAdaptrisMessage multiMessage = (MultiPayloadAdaptrisMessage)original;
		String originalId = multiMessage.getCurrentPayloadId();
		for (AdaptrisMessage message : messages)
		{
			log.info("Adding message payload [{}]", message.getUniqueId());

			multiMessage.addPayload(message.getUniqueId(), message.getPayload());
		}
		if (replaceOriginalMessage)
		{
			multiMessage.deletePayload(originalId);
		}

		log.info("Finished adding {} messages", messages.size());
	}
}

@@ -91,16 +91,66 @@
 onfig;%0D%0A
+import com.adaptris.annotation.ComponentProfile;%0D%0A
 import c
@@ -176,32 +176,32 @@
 aptrisMessage;%0D%0A
-
 import com.adapt
@@ -361,16 +361,75 @@
 .Args;%0D%0A
+import com.thoughtworks.xstream.annotations.XStreamAlias;%0D%0A
 import o
@@ -696,17 +696,353 @@
 age.%0D%0A *
-/
+%0D%0A * @author amanderson%0D%0A * @config multi-payload-aggregator%0D%0A * @see MultiPayloadAdaptrisMessage%0D%0A * @since 3.10%0D%0A */%0D%0A@XStreamAlias(%22multi-payload-aggregator%22)%0D%0A@ComponentProfile(summary = %22Combine many Adaptris messages into a single multi-payload message with each payload separate%22, tag = %22multi-payload,aggregator%22, since = %223.10%22)
 %0D%0Apublic

Update JavaDoc and ComponentProfile info