All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.ratings.kernel.definition.PortletRatingsDefinitionUtil Maven / Gradle / Ivy

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 156.0.0
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 */

package com.liferay.ratings.kernel.definition;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.ratings.kernel.RatingsType;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import com.liferay.registry.ServiceReference;
import com.liferay.registry.ServiceTrackerCustomizer;
import com.liferay.registry.collections.ServiceTrackerCollections;
import com.liferay.registry.collections.ServiceTrackerMap;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Roberto Díaz
 * @author Sergio González
 */
public class PortletRatingsDefinitionUtil {

	public static RatingsType getDefaultRatingsType(String className) {
		PortletRatingsDefinitionValues portletRatingsDefinitionValues =
			_serviceTrackerMap.getService(className);

		if (portletRatingsDefinitionValues == null) {
			return null;
		}

		return portletRatingsDefinitionValues.getDefaultRatingsType();
	}

	public static Map
		getPortletRatingsDefinitionValuesMap() {

		Map
			portletRatingsDefinitionValuesMap = new HashMap<>();

		for (String className : _serviceTrackerMap.keySet()) {
			portletRatingsDefinitionValuesMap.put(
				className, _serviceTrackerMap.getService(className));
		}

		return Collections.unmodifiableMap(portletRatingsDefinitionValuesMap);
	}

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

	private static final ServiceTrackerCustomizer
		
			_serviceTrackerCustomizer =
				new ServiceTrackerCustomizer
					() {

					@Override
					public PortletRatingsDefinitionValues addingService(
						ServiceReference
							serviceReference) {

						String[] classNames = null;

						Object modelClassName = serviceReference.getProperty(
							"model.class.name");

						if (modelClassName instanceof Object[]) {
							classNames = (String[])modelClassName;
						}
						else {
							classNames = new String[] {(String)modelClassName};
						}

						if (ArrayUtil.isEmpty(classNames)) {
							if (_log.isWarnEnabled()) {
								_log.warn(
									"Property \"model.class.name\" is not set");
							}

							return null;
						}

						Registry registry = RegistryUtil.getRegistry();

						PortletRatingsDefinition portletRatingsDefinition =
							registry.getService(serviceReference);

						RatingsType defaultRatingsType =
							portletRatingsDefinition.getDefaultRatingsType();

						if (defaultRatingsType == null) {
							if (_log.isWarnEnabled()) {
								_log.warn("Default ratings type is null");
							}

							return null;
						}

						String portletId =
							portletRatingsDefinition.getPortletId();

						if (Validator.isNull(portletId)) {
							if (_log.isWarnEnabled()) {
								_log.warn("Portlet ID is null");
							}

							return null;
						}

						return new PortletRatingsDefinitionValues(
							classNames, defaultRatingsType, portletId);
					}

					@Override
					public void modifiedService(
						ServiceReference
							serviceReference,
						PortletRatingsDefinitionValues
							portletRatingsDefinitionValues) {
					}

					@Override
					public void removedService(
						ServiceReference
							serviceReference,
						PortletRatingsDefinitionValues
							portletRatingsDefinitionValues) {

						Registry registry = RegistryUtil.getRegistry();

						registry.ungetService(serviceReference);
					}

				};

	private static final ServiceTrackerMap
		 _serviceTrackerMap =
			ServiceTrackerCollections.openSingleValueMap(
				PortletRatingsDefinition.class, "model.class.name",
				_serviceTrackerCustomizer);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy