/**
 * 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.organizations.internal.configuration.persistence.listener;

import com.liferay.organizations.internal.configuration.OrganizationTypeConfiguration;
import com.liferay.portal.configuration.persistence.listener.ConfigurationModelListener;
import com.liferay.portal.configuration.persistence.listener.ConfigurationModelListenerException;
import com.liferay.portal.kernel.util.LocaleThreadLocal;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.util.Validator;

import java.util.Locale;
import java.util.Dictionary;
import java.util.ResourceBundle;

import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Drew Brokke
 */
@Component(
	immediate = true,
	property = "model.class.name=com.liferay.organizations.internal.configuration.OrganizationTypeConfiguration",
	service = ConfigurationModelListener.class
)
public class OrganizationTypeConfigurationModelListener
	implements ConfigurationModelListener {

	@Override
	public void onBeforeSave(String pid, Dictionary<String, Object> properties)
		throws ConfigurationModelListenerException {

		try {
			String name = (String)properties.get("name");

			_validateNameExists(name);

			_validateUniqueConfiguration(pid, name);
		}
		catch (Exception exception) {
			throw new ConfigurationModelListenerException(
				exception, OrganizationTypeConfiguration.class,
				getClass(), properties);
		}
	}

	private ResourceBundle _getResourceBundle() {
		Locale locale = LocaleThreadLocal.getThemeDisplayLocale();
		
		if (locale == null) {
			locale = LocaleUtil.getDefault();
		}

		return ResourceBundleUtil.getBundle(
			"content.Language", locale, getClass());
	}

	private void _validateNameExists(String name) throws Exception {
		if (Validator.isNotNull(name)) {
			return;
		}

		ResourceBundle resourceBundle = _getResourceBundle();

		String message = ResourceBundleUtil.getString(
			resourceBundle, "an-organization-type-must-have-a-valid-name");

		throw new Exception(message);
	}

	private void _validateUniqueConfiguration(String pid, String name)
		throws Exception {

		String filterString = String.format(
			"(&(service.factoryPid=%s)(name=%s))",
			OrganizationTypeConfiguration.class.getName(), name);

		Configuration[] configurations = _configurationAdmin.listConfigurations(
			filterString);

		if (configurations == null) {
			return;
		}

		Configuration configuration = configurations[0];

		if (pid.equals(configuration.getPid())) {
			return;
		}

		ResourceBundle resourceBundle = _getResourceBundle();

		String message = ResourceBundleUtil.getString(
			resourceBundle,
			"there-is-already-an-organization-type-with-the-name-x", name);

		throw new Exception(message);
	}

	@Reference
	private ConfigurationAdmin _configurationAdmin;

}