<commit_before>package org.buddycloud.channelserver.utils.users;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
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.ChannelManagerFactory;
import org.buddycloud.channelserver.channel.LocalDomainChecker;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Presence.Type;
import org.xmpp.resultsetmanagement.ResultSet;

public class OnlineResourceManager {

	public static final String UNAVAILABLE = "unavailable";
	private static final Logger LOGGER = Logger
			.getLogger(OnlineResourceManager.class);

	private HashMap<String, ArrayList<JID>> users = new HashMap<String, ArrayList<JID>>();
	private Properties configuration;
	private ChannelManager channelManager;
	private Boolean useDatabaseStorage = false;

	public OnlineResourceManager(Properties configuration,
			ChannelManager channelManager) {
		if (configuration
				.getProperty(Configuration.CONFIGURATION_SERVER_DOMAIN) == null
				&& configuration
						.getProperty(Configuration.CONFIGURATION_LOCAL_DOMAIN_CHECKER) == null) {
			throw new NullPointerException(
					"Missing server domain configuration");

		}
		this.configuration = configuration;
		this.channelManager = channelManager;
		this.useDatabaseStorage = Boolean.valueOf(configuration.getProperty(
				Configuration.PERSIST_PRESENCE_DATA, "false"));

	}

	public void subscribeToNodeListeners(BlockingQueue<Packet> outQueue) {
		try {
			ResultSet<NodeSubscription> subscriptions = channelManager
					.getNodeSubscriptionListeners();
			for (NodeSubscription subscription : subscriptions) {
				Presence presence = new Presence(Type.subscribe);
				presence.setTo(subscription.getListener().toBareJID());
				outQueue.put(presence);
			}
		} catch (Exception e) {
			LOGGER.warn("Error while sending presence to node listeners", e);
		}
	}

	public ArrayList<JID> getResources(JID jid) throws NodeStoreException {
		if ((jid.getResource() != null)
				|| ((jid.getResource() == null) && (jid.getNode() == null))) {
			ArrayList<JID> user = new ArrayList<JID>();
			user.add(jid);
			return user;
		}
		if (true == useDatabaseStorage) {
			ArrayList<JID> jids = channelManager.onlineJids(jid);
		}
		if (!users.containsKey(jid.toBareJID())) {
			return new ArrayList<JID>();
		}
		return users.get(jid.toBareJID());
	}

	public void updateStatus(JID jid, String type) throws NodeStoreException {
		if (!LocalDomainChecker.isLocal(jid.getDomain(), configuration)) {
			return;
		}
		
		ArrayList<JID> user = null;
		if (users.containsKey(jid.toBareJID())) {
			user = users.get(jid.toBareJID());
		}
		if ((type != null) && type.equals(UNAVAILABLE)) {
			LOGGER.info("User going offline: " + jid.toString());
			if (true == useDatabaseStorage) {
				channelManager.jidOffline(jid);
			} else {
				if (null != user) {
					user.remove(jid);
				}
			}
			return;
		}
		if (true == useDatabaseStorage) {
			channelManager.jidOnline(jid);<commit_msg>Make code easier to read<commit_after>package org.buddycloud.channelserver.utils.users;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
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.ChannelManagerFactory;
import org.buddycloud.channelserver.channel.LocalDomainChecker;
import org.buddycloud.channelserver.db.exception.NodeStoreException;
import org.buddycloud.channelserver.pubsub.model.NodeSubscription;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Presence.Type;
import org.xmpp.resultsetmanagement.ResultSet;

public class OnlineResourceManager {

	public static final String UNAVAILABLE = "unavailable";
	private static final Logger LOGGER = Logger
			.getLogger(OnlineResourceManager.class);

	private HashMap<String, ArrayList<JID>> users = new HashMap<String, ArrayList<JID>>();
	private Properties configuration;
	private ChannelManager channelManager;
	private Boolean useDatabaseStorage = false;

	public OnlineResourceManager(Properties configuration,
			ChannelManager channelManager) {
		if (configuration
				.getProperty(Configuration.CONFIGURATION_SERVER_DOMAIN) == null
				&& configuration
						.getProperty(Configuration.CONFIGURATION_LOCAL_DOMAIN_CHECKER) == null) {
			throw new NullPointerException(
					"Missing server domain configuration");

		}
		this.configuration = configuration;
		this.channelManager = channelManager;
		this.useDatabaseStorage = Boolean.valueOf(configuration.getProperty(
				Configuration.PERSIST_PRESENCE_DATA, "false"));

	}

	public void subscribeToNodeListeners(BlockingQueue<Packet> outQueue) {
		try {
			ResultSet<NodeSubscription> subscriptions = channelManager
					.getNodeSubscriptionListeners();
			for (NodeSubscription subscription : subscriptions) {
				Presence presence = new Presence(Type.subscribe);
				presence.setTo(subscription.getListener().toBareJID());
				outQueue.put(presence);
			}
		} catch (Exception e) {
			LOGGER.warn("Error while sending presence to node listeners", e);
		}
	}

	public ArrayList<JID> getResources(JID jid) throws NodeStoreException {

		boolean isBareJid = (jid.getResource() == null);
		boolean isServerJid = ((jid.getResource() == null) && (jid.getNode() == null));

		JID bareJid = new JID(jid.toBareJID());

		ArrayList<JID> user = new ArrayList<JID>();

		if (!isBareJid || isServerJid) {
			user.add(jid);
			return user;
		}

		if (true == useDatabaseStorage) {
			return channelManager.onlineJids(jid);
		}
		if (users.containsKey(bareJid)) {
			return users.get(bareJid);
		}
		return user;
	}

	public void updateStatus(JID jid, String type) throws NodeStoreException {
		if (!LocalDomainChecker.isLocal(jid.getDomain(), configuration)) {
			return;
		}

		ArrayList<JID> user = null;
		if (users.containsKey(jid.toBareJID())) {
			user = users.get(jid.toBareJID());
		}
		if ((type != null) && type.equals(UNAVAILABLE)) {
			LOGGER.info("User going offline: " + jid.toString());
			if (true == useDatabaseStorage) {
				channelManager.jidOffline(jid);
			} else {
				if (null != user) {
					user.remove(jid);
				}
			}
			return;
		}
		if (true == useDatabaseStorage) {
			channelManager.jidOnline(jid);
{'fraction_non_alphanumeric': 0.1134478672985782, 'fraction_numerical': 0.0002962085308056872, 'mean_word_length': 5.008007117437723, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '422556', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2068, 'n_words': 407}