package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.buddycloud.channelserver.Configuration;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.channel.node.configuration.Helper;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.PacketError.Condition;
import org.xmpp.packet.PacketError.Type;

public abstract class PubSubElementProcessorAbstract
    implements PubSubElementProcessor
{
	public static final String NS_RSM = "http://jabber.org/protocol/rsm";
    
	public static Logger logger = Logger.getLogger(PubSubElementProcessorAbstract.class);
	
    protected BlockingQueue<Packet> outQueue;
    protected ChannelManager        channelManager;
    protected Element               element;
    protected IQ                    response;
    protected IQ                    request;
    protected JID                   actor;
    protected String                serverDomain;
    protected String                topicsDomain;
    protected String                node = null;
    protected Helper                configurationHelper;

	protected Element resultSetManagement;
	protected String firstItem;
	protected String lastItem;
	protected int totalEntriesCount;

	private Collection<JID> adminUsers;

	public void setOutQueue(BlockingQueue<Packet> outQueue)
	{
		this.outQueue = outQueue;
	}

	public void setChannelManager(ChannelManager channelManager)
	{
		this.channelManager = channelManager;
	}
	
	public void setServerDomain(String domain)
	{
		serverDomain = domain;
	}

	protected String getServerDomain()
	{
		if (null == serverDomain) {
            serverDomain = Configuration.getInstance()
			    .getProperty("server.domain");
		}
		return serverDomain;
	}

	protected Collection<JID> getAdminUsers() {
		if (null == adminUsers) {
			adminUsers = Configuration.getInstance().getAdminUsers();
		}
		return adminUsers;
	}
	
	public void setTopicsDomain(String domain)
	{
	    topicsDomain = domain;		
	}
	
	public void setNode(String node) {
        this.node = node;
	}
	
	protected String getTopicsDomain()
	{
		if (null == topicsDomain) {
            topicsDomain = Configuration.getInstance()
			    .getProperty("server.domain.topics");
		}
		return topicsDomain;		
	}

	public void setConfigurationHelper(Helper helper)
	{
		configurationHelper = helper;
	}
	
	protected Helper getNodeConfigurationHelper()
	{
		if (null == configurationHelper) {
			configurationHelper = new Helper();
		}
		return configurationHelper;
	}
	
	protected void setErrorCondition(Type type, Condition condition)
	{
		if (null == response) response = IQ.createResultIQ(request);
		response.setType(IQ.Type.error);
		PacketError error = new PacketError(condition, type);
		response.setError(error);
	}
	
	protected void createExtendedErrorReply(Type type, Condition condition,
			String additionalElement) {
		if (null == response) response = IQ.createResultIQ(request);
		response.setType(IQ.Type.error);
		Element standardError = new DOMElement(condition.toXMPP(),
				new org.dom4j.Namespace("", JabberPubsub.NS_XMPP_STANZAS));
		Element extraError = new DOMElement(additionalElement,
				new org.dom4j.Namespace("", JabberPubsub.NS_PUBSUB_ERROR));
		Element error = new DOMElement("error");
		error.addAttribute("type", type.toXMPP());
		error.add(standardError);
		error.add(extraError);
		response.setChildElement(error);
	}
	
	protected Document getDocumentHelper()
	{
		return DocumentHelper.createDocument();
	}
}
@@ -2828,33 +2828,32 @@
 ationHelper;%0A%09%7D%0A
-%09
 %0A%09protected void
@@ -2902,18 +2902,17 @@
 ndition)
-%0A%09
+ 
 %7B%0A%09%09if (
@@ -2920,33 +2920,36 @@
 ull == response)
- 
+%0A%09%09%09
 response = IQ.cr
@@ -3085,34 +3085,33 @@
 rror(error);%0A%09%7D%0A
-%09
 %0A
+
 %09protected void 
@@ -3194,24 +3194,259 @@
 lElement) %7B%0A
+%09%09createExtendedErrorReply(type, condition, additionalElement,%0A%09%09%09%09JabberPubsub.NS_PUBSUB_ERROR);%0A%09%7D%0A%0A%09private void createExtendedErrorReply(Type type, Condition condition,%0A%09%09%09String additionalElement, String additionalNamespace) %7B%0A%09%09%0A
 %09%09if (null =
@@ -3457,16 +3457,21 @@
 sponse) 
+%7B%0A%09%09%09
 response
@@ -3493,32 +3493,36 @@
 ultIQ(request);%0A
+%09%09%7D%0A
 %09%09response.setTy
@@ -3758,36 +3758,27 @@
 %22%22, 
-JabberPubsub.NS_PUBSUB_ERROR
+additionalNamespace
 ));%0A

Add ability to specify application condition error namespace