package org.buddycloud.channelserver.packetprocessor.message.event;

import java.util.Properties;
import java.util.concurrent.BlockingQueue;

import org.buddycloud.channelserver.Configuration;
import org.buddycloud.channelserver.channel.ChannelManager;
import org.buddycloud.channelserver.packetprocessor.PacketProcessor;
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
import org.buddycloud.channelserver.pubsub.model.NodeMembership;
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
import org.buddycloud.channelserver.utils.NotificationScheme;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.resultsetmanagement.ResultSet;

abstract public class AbstractMessageProcessor implements PacketProcessor<Message> {

	
	
	protected Message message;
	protected String node;
	protected ChannelManager channelManager;
	protected Properties configuration;
	protected BlockingQueue<Packet> outQueue;
	
	public AbstractMessageProcessor(ChannelManager channelManager, Properties configuration, BlockingQueue<Packet> outQueue) {
		this.channelManager = channelManager;
		setConfiguration(configuration);
		this.outQueue = outQueue;
	}
	
	public void setConfiguration(Properties configuration) {
		this.configuration = configuration;
	}
	
	abstract public void process(Message packet) throws Exception;
	
	void sendLocalNotifications(NotificationScheme scheme) throws Exception {
	    sendLocalNotifications(scheme, null);	
	}
	
	void sendLocalNotifications(NotificationScheme scheme, JID user) throws Exception {
		ResultSet<NodeMembership> members = channelManager
				.getNodeMemberships(node);
		message.setFrom(new JID(configuration
				.getProperty(Configuration.CONFIGURATION_SERVER_CHANNELS_DOMAIN)));
		
		for (NodeMembership member : members) {
			if (false == channelManager.isLocalJID(member.getUser())) continue;
			
			switch (scheme) {
			    case SCHEME_VALID_SUBSCRIBERS: 
				    if (false == userIsValidSubscriber(member)) {
				    	continue;
				    }
				    break;
			    case SCHEME_OWNER_MODERATOR:
			    	if (false == userIsOwnerOrModerator(member)) {
			    		continue;
			    	}
			    	break;
			    default:
			    	throw new UnsupportedOperationException(NotificationScheme.UNSUPPORTED_SCHEME);
			}

			
			message.setTo(member.getUser());
			outQueue.put(message.createCopy());
		}
		
		if (null != user) {
			message.setTo(user.toBareJID());
			outQueue.put(message.createCopy());
		}
	}

	private boolean userIsOwnerOrModerator(NodeMembership member) {
		if (false == Subscriptions.subscribed.equals(member.getSubscription())) {
			return false;
		}
		if (false == member.getAffiliation().canAuthorize()) {
			return false;
		}
		return true;
	}

	private boolean userIsValidSubscriber(NodeMembership member) {
		if (false == Subscriptions.subscribed.equals(member.getSubscription())) {
			return false;
		}
		if (Affiliations.outcast.equals(member.getAffiliation())) {
			return false;
		}
		return true;
	}
}
@@ -1985,22 +1985,18 @@
 %0A%09%09%09
-switch
+if
  (scheme
 ) %7B%0A
@@ -1995,72 +1995,65 @@
 heme
-) %7B%0A%09%09%09    case SCHEME_VALID_SUBSCRIBERS: %0A%09%09%09%09    if (false == 
+.equals(NotificationScheme.SCHEME_VALID_SUBSCRIBERS) && !
 user
@@ -2085,21 +2085,16 @@
 ) %7B%0A%09%09%09%09
-    %09
 continue
@@ -2102,87 +2102,80 @@
 %0A%09%09%09
-%09    %7D%0A%09%09%09%09    break;%0A%09%09%09    case SCHEME_OWNER_MODERATOR:%0A%09%09%09    %09if (false == 
+%7D else if (scheme.equals(NotificationScheme.SCHEME_OWNER_MODERATOR) && !
 user
@@ -2207,21 +2207,16 @@
 )) %7B%0A%09%09%09
-    %09
 %09continu
@@ -2222,147 +2222,14 @@
 ue;%0A
-%09%09%09    %09%7D%0A%09%09%09    %09break;%0A%09%09%09    default:%0A%09%09%09    %09throw new UnsupportedOperationException(NotificationScheme.UNSUPPORTED_SCHEME);%0A
 %09%09%09%7D%0A%0A
-%09%09%09%0A
 %09%09%09m

Use a contract style