/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.kernel.util;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.PortalPreferences;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;

import java.util.ConcurrentModificationException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Brian Wing Shun Chan
 * @author Raymond Augé
 */
public class SessionClicks {

	public static String get(
		HttpServletRequest httpServletRequest, String key,
		String defaultValue) {

		return get(httpServletRequest, _DEFAULT_NAMESPACE, key, defaultValue);
	}

	public static String get(
		HttpServletRequest httpServletRequest, String namespace, String key,
		String defaultValue) {

		try {
			PortalPreferences portalPreferences =
				PortletPreferencesFactoryUtil.getPortalPreferences(
					httpServletRequest);

			return portalPreferences.getValue(namespace, key, defaultValue);
		}
		catch (Exception e) {
			_log.error(e, e);

			return null;
		}
	}

	public static String get(
		HttpSession session, String key, String defaultValue) {

		return get(session, _DEFAULT_NAMESPACE, key, defaultValue);
	}

	public static String get(
		HttpSession session, String namespace, String key,
		String defaultValue) {

		String sessionKey = namespace.concat(
			StringPool.COLON
		).concat(
			key
		);

		return GetterUtil.getString(
			session.getAttribute(sessionKey), defaultValue);
	}

	public static void put(
		HttpServletRequest httpServletRequest, String key, String value) {

		put(httpServletRequest, _DEFAULT_NAMESPACE, key, value);
	}

	public static void put(
		HttpServletRequest httpServletRequest, String namespace, String key,
		String value) {

		if (!_isValidKeyValue(key, value)) {
			return;
		}

		while (true) {
			try {
				PortalPreferences portalPreferences =
					PortletPreferencesFactoryUtil.getPortalPreferences(
						httpServletRequest);

				int size = portalPreferences.size();

				if (_isValidSize(size, key, value)) {
					portalPreferences.setValue(namespace, key, value);
				}

				break;
			}
			catch (ConcurrentModificationException cme) {
				continue;
			}
			catch (Exception e) {
				_log.error(e, e);

				break;
			}
		}
	}

	public static void put(HttpSession session, String key, String value) {
		put(session, _DEFAULT_NAMESPACE, key, value);
	}

	public static void put(
		HttpSession session, String namespace, String key, String value) {

		if (!_isValidKeyValue(key, value)) {
			return;
		}

		Enumeration<String> enumeration = session.getAttributeNames();

		int size = 0;

		while (enumeration.hasMoreElements()) {
			enumeration.nextElement();

			size++;
		}

		if (_isValidSize(size, key, value)) {
			String sessionKey = StringBundler.concat(
				namespace, StringPool.COLON, key);

			session.setAttribute(sessionKey, value);
		}
	}

	private static boolean _isValidKeyValue(String key, String value) {
		if ((key.length() <= _SESSION_CLICKS_MAX_SIZE_TERMS) &&
			(value.length() <= _SESSION_CLICKS_MAX_SIZE_TERMS)) {

			return true;
		}

		if (_log.isWarnEnabled()) {
			_log.warn(
				StringBundler.concat(
					"Session clicks has attempted to exceed the maximum size ",
					"allowed for keys or values with key \"", key,
					"\" and value \"", value, "\""));
		}

		return false;
	}

	private static boolean _isValidSize(int size, String key, String value) {
		if (size < _SESSION_CLICKS_MAX_ALLOWED_VALUES) {
			return true;
		}

		if (_log.isWarnEnabled()) {
			_log.warn(
				StringBundler.concat(
					"Session clicks has attempted to exceed the maximum ",
					"number of allowed values with key \"", key,
					"\" and value \"", value, "\""));
		}

		return false;
	}

	private static final String _DEFAULT_NAMESPACE =
		SessionClicks.class.getName();

	private static final int _SESSION_CLICKS_MAX_ALLOWED_VALUES =
		GetterUtil.getInteger(
			PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_ALLOWED_VALUES));

	private static final int _SESSION_CLICKS_MAX_SIZE_TERMS =
		GetterUtil.getInteger(
			PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_SIZE_TERMS));

	private static final Log _log = LogFactoryUtil.getLog(SessionClicks.class);

}