/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2008 "Artur Hefczyc" <artur.hefczyc@tigase.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */

package tigase.xmpp.impl;

//~--- non-JDK imports --------------------------------------------------------

import tigase.util.TigaseStringprepException;

import tigase.xml.Element;

import tigase.xmpp.JID;
import tigase.xmpp.NotAuthorizedException;
import tigase.xmpp.XMPPResourceConnection;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

//~--- classes ----------------------------------------------------------------

/**
 * Created: Nov 28, 2008 10:27:55 PM
 *
 * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a>
 * @version $Rev$
 */
public class DynamicRosterTest implements DynamicRosterIfc {
	private static Logger log = Logger.getLogger("tigase.xmpp.impl.DynamicRosterTest");

	//~--- fields ---------------------------------------------------------------

	private Map<String, Element> memStorage = new LinkedHashMap<String, Element>();

	//~--- get methods ----------------------------------------------------------

	/**
	 * Method description
	 *
	 *
	 * @param session
	 *
	 * @return
	 *
	 * @throws NotAuthorizedException
	 */
	@Override
	public JID[] getBuddies(XMPPResourceConnection session) throws NotAuthorizedException {
		return new JID[] { JID.jidInstanceNS("dynrost@test-d") };
	}

	/**
	 * Method description
	 *
	 *
	 * @param session
	 * @param buddy
	 *
	 * @return
	 *
	 * @throws NotAuthorizedException
	 */
	@Override
	public Element getBuddyItem(XMPPResourceConnection session, JID buddy)
			throws NotAuthorizedException {
		if ("dynrost@test-d".equals(buddy.getBareJID().toString())) {
			return getBuddy();
		} else {
			return null;
		}
	}

	/**
	 * Method description
	 *
	 *
	 * @param item
	 *
	 * @return
	 */
	@Override
	public Element getItemExtraData(Element item) {
		String jid = item.getAttribute("jid");
		Element result = memStorage.get(jid);

		if (log.isLoggable(Level.FINEST)) {
			log.finest("Retrieving item: " + result + ", for jid=" + jid);
		}

		return result;
	}

	/**
	 * Method description
	 *
	 *
	 * @param session
	 *
	 * @return
	 *
	 * @throws NotAuthorizedException
	 */
	@Override
	public List<Element> getRosterItems(XMPPResourceConnection session)
			throws NotAuthorizedException {
		return new ArrayList<Element>(Arrays.asList(getBuddy()));
	}

	//~--- methods --------------------------------------------------------------

	/**
	 * Method description
	 *
	 *
	 * @param props
	 */
	@Override
	public void init(Map<String, Object> props) {}

	/**
	 * Method description
	 *
	 *
	 * @param par
	 */
	@Override
	public void init(String par) {}

	//~--- set methods ----------------------------------------------------------

	/**
	 * Method description
	 *
	 *
	 * @param item
	 */
	@Override
	public void setItemExtraData(Element item) {
		String jid = item.getAttribute("jid");

		if (log.isLoggable(Level.FINEST)) {
			log.finest("Storing item: " + item + ", for jid=" + jid);
		}

		memStorage.put(jid, item);
	}

	//~--- get methods ----------------------------------------------------------

	private Element getBuddy() {
		return new Element("item", new Element[] { new Element("group", "test group") },
				new String[] { "jid",
				"name", "subscription" }, new String[] { "dynrost@test-d", "dynrost", "both" });
	}
}


//~ Formatted in Sun Code Convention


//~ Formatted by Jindent --- http://www.jindent.com

@@ -904,55 +904,8 @@
 --%0A%0A
-import tigase.util.TigaseStringprepException;%0A%0A
 impo
@@ -927,16 +927,16 @@
 lement;%0A
+
 %0Aimport 
@@ -1645,26 +1645,8 @@
 ger(
-%22tigase.xmpp.impl.
 Dyna
@@ -1658,17 +1658,32 @@
 sterTest
-%22
+.class.getName()
 );%0A%0A%09//~
@@ -2834,64 +2834,91 @@
 log.
-finest(%22Retrieving item: %22 + result + %22, for jid=%22 +
+log(Level.FINEST, %22Retrieving item: %7B0%7D, for jid=%7B1%7D%22, new Object%5B%5D %7B result,
  jid
+ %7D
 );%0A%09
@@ -3820,59 +3820,86 @@
 log.
-finest(%22Storing item: %22 + item + %22, for jid=%22 +
+log(Level.FINEST, %22Storing item: %7B0%7D, for jid=%7B1%7D%22, new Object%5B%5D %7B item,
  jid
+ %7D
 );%0A%09

Automatic code reformatting.