package com.gss.gmo.cao.integration.xmpp.component.core;

import static com.gss.gmo.cao.integration.xmpp.component.core.IQListener.IQType.error;
import static com.gss.gmo.cao.integration.xmpp.component.core.IQListener.IQType.result;

import java.util.EnumMap;

import org.xmpp.component.AbstractComponent;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;

import com.gss.gmo.cao.integration.xmpp.component.core.IQListener.IQType;

/**
 * @author linus_chien
 *
 */
public class GlobalComponent extends AbstractComponent {

	private String name;
	private String description;

	private MessageListener messageListener;
	private PresenceListener presenceListener;
	private EnumMap<IQType, IQListener> iqListenerMap = new EnumMap<IQType, IQListener>(IQType.class);

	public GlobalComponent(String name, String description) {
		super();
		this.name = name;
		this.description = description;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getDescription() {
		return description;
	}

	public void setMessageListener(MessageListener messageListener) {
		this.messageListener = messageListener;
	}

	public void setPresenceListener(PresenceListener presenceListener) {
		this.presenceListener = presenceListener;
	}

	public void addIQListener(IQType iqType, IQListener iqListener) {
		this.iqListenerMap.put(iqType, iqListener);
	}

	@Override
	protected void handleMessage(Message message) {
		if (messageListener != null) {
			messageListener.handleMessage(message);
		}
	}

	@Override
	protected void handlePresence(Presence presence) {
		if (presenceListener != null) {
			presenceListener.handlePresence(presence);
		}
	}

	@Override
	protected void handleIQResult(IQ iq) {
		if (iqListenerMap.get(result) != null) {
			iqListenerMap.get(result).handleIQ(iq);
		}
	}

	@Override
	protected void handleIQError(IQ iq) {
		if (iqListenerMap.get(error) != null) {
			iqListenerMap.get(error).handleIQ(iq);
		} else {
			super.handleIQError(iq);
		}
	}

}

@@ -822,16 +822,144 @@
 class);%0D
+%0A%09private EnumMap%3CIQHandler.IQType, IQHandler%3E iqHandlerMap = new EnumMap%3CIQHandler.IQType, IQHandler%3E(IQHandler.IQType.class);%0D
 %0A%0D%0A%09publ
@@ -1577,32 +1577,158 @@
 stener);%0D%0A%09%7D%0D%0A%0D%0A
+%09public void addIQHandler(IQHandler.IQType iqType, IQHandler iqHandler) %7B%0D%0A%09%09this.iqHandlerMap.put(iqType, iqHandler);%0D%0A%09%7D%0D%0A%0D%0A
 %09@Override%0D%0A%09pro
@@ -2322,16 +2322,16 @@
 else %7B%0D%0A
-
 %09%09%09super
@@ -2362,11 +2362,429 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09@Override%0D%0A%09protected IQ handleIQGet(IQ iq) %7B%0D%0A%09%09if (iqHandlerMap.get(IQHandler.IQType.get) != null) %7B%0D%0A%09%09%09return iqHandlerMap.get(IQHandler.IQType.get).handleIQ(iq);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09protected IQ handleIQSet(IQ iq) %7B%0D%0A%09%09if (iqHandlerMap.get(IQHandler.IQType.set) != null) %7B%0D%0A%09%09%09return iqHandlerMap.get(IQHandler.IQType.set).handleIQ(iq);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A
 %7D%0D%0A

add IQHandler Map