001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.ratings.definition;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portlet.ratings.RatingsType;
022    import com.liferay.registry.Registry;
023    import com.liferay.registry.RegistryUtil;
024    import com.liferay.registry.ServiceReference;
025    import com.liferay.registry.ServiceTrackerCustomizer;
026    import com.liferay.registry.collections.ServiceTrackerCollections;
027    import com.liferay.registry.collections.ServiceTrackerMap;
028    
029    import java.util.Collections;
030    import java.util.HashMap;
031    import java.util.Map;
032    
033    /**
034     * @author Roberto D??az
035     * @author Sergio Gonz??lez
036     */
037    public class PortletRatingsDefinitionUtil {
038    
039            public static RatingsType getDefaultRatingsType(String className) {
040                    PortletRatingsDefinitionValues portletRatingsDefinitionValues =
041                            _serviceTrackerMap.getService(className);
042    
043                    if (portletRatingsDefinitionValues == null) {
044                            return null;
045                    }
046    
047                    return portletRatingsDefinitionValues.getDefaultRatingsType();
048            }
049    
050            public static Map<String, PortletRatingsDefinitionValues>
051                    getPortletRatingsDefinitionValuesMap() {
052    
053                    Map<String, PortletRatingsDefinitionValues>
054                            portletRatingsDefinitionValuesMap = new HashMap<>();
055    
056                    for (String className : _serviceTrackerMap.keySet()) {
057                            portletRatingsDefinitionValuesMap.put(
058                                    className, _serviceTrackerMap.getService(className));
059                    }
060    
061                    return Collections.unmodifiableMap(portletRatingsDefinitionValuesMap);
062            }
063    
064            private static final Log _log = LogFactoryUtil.getLog(
065                    PortletRatingsDefinitionUtil.class);
066    
067            private static final ServiceTrackerCustomizer
068                    <PortletRatingsDefinition, PortletRatingsDefinitionValues>
069                            _serviceTrackerCustomizer =
070                                    new ServiceTrackerCustomizer
071                                            <PortletRatingsDefinition,
072                                                    PortletRatingsDefinitionValues>() {
073    
074                    @Override
075                    public PortletRatingsDefinitionValues addingService(
076                            ServiceReference<PortletRatingsDefinition> serviceReference) {
077    
078                            String[] classNames = null;
079    
080                            Object modelClassName = serviceReference.getProperty(
081                                    "model.class.name");
082    
083                            if (modelClassName instanceof Object[]) {
084                                    classNames = (String[])modelClassName;
085                            }
086                            else {
087                                    classNames = new String[] {(String)modelClassName};
088                            }
089    
090                            if (ArrayUtil.isEmpty(classNames)) {
091                                    if (_log.isWarnEnabled()) {
092                                            _log.warn("Property \"model.class.name\" is not set");
093                                    }
094    
095                                    return null;
096                            }
097    
098                            Registry registry = RegistryUtil.getRegistry();
099    
100                            PortletRatingsDefinition portletRatingsDefinition =
101                                    registry.getService(serviceReference);
102    
103                            RatingsType defaultRatingsType =
104                                    portletRatingsDefinition.getDefaultRatingsType();
105    
106                            if (defaultRatingsType == null) {
107                                    if (_log.isWarnEnabled()) {
108                                            _log.warn("Default ratings type is null");
109                                    }
110    
111                                    return null;
112                            }
113    
114                            String portletId = portletRatingsDefinition.getPortletId();
115    
116                            if (Validator.isNull(portletId)) {
117                                    if (_log.isWarnEnabled()) {
118                                            _log.warn("Portlet ID is null");
119                                    }
120    
121                                    return null;
122                            }
123    
124                            return new PortletRatingsDefinitionValues(
125                                    classNames, defaultRatingsType, portletId);
126                    }
127    
128                    @Override
129                    public void modifiedService(
130                            ServiceReference<PortletRatingsDefinition> serviceReference,
131                            PortletRatingsDefinitionValues
132                                    portletRatingsDefinitionValues) {
133                    }
134    
135                    @Override
136                    public void removedService(
137                            ServiceReference<PortletRatingsDefinition> serviceReference,
138                            PortletRatingsDefinitionValues
139                                    portletRatingsDefinitionValues) {
140    
141                            Registry registry = RegistryUtil.getRegistry();
142    
143                            registry.ungetService(serviceReference);
144                    }
145    
146            };
147    
148            private static final ServiceTrackerMap
149                    <String, PortletRatingsDefinitionValues> _serviceTrackerMap =
150                            ServiceTrackerCollections.openSingleValueMap(
151                                    PortletRatingsDefinition.class, "model.class.name",
152                                    _serviceTrackerCustomizer);
153    
154    }