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>
077                                                            serviceReference) {
078    
079                                                    String[] classNames = null;
080    
081                                                    Object modelClassName = serviceReference.getProperty(
082                                                            "model.class.name");
083    
084                                                    if (modelClassName instanceof Object[]) {
085                                                            classNames = (String[])modelClassName;
086                                                    }
087                                                    else {
088                                                            classNames = new String[] {(String)modelClassName};
089                                                    }
090    
091                                                    if (ArrayUtil.isEmpty(classNames)) {
092                                                            if (_log.isWarnEnabled()) {
093                                                                    _log.warn(
094                                                                            "Property \"model.class.name\" is not set");
095                                                            }
096    
097                                                            return null;
098                                                    }
099    
100                                                    Registry registry = RegistryUtil.getRegistry();
101    
102                                                    PortletRatingsDefinition portletRatingsDefinition =
103                                                            registry.getService(serviceReference);
104    
105                                                    RatingsType defaultRatingsType =
106                                                            portletRatingsDefinition.getDefaultRatingsType();
107    
108                                                    if (defaultRatingsType == null) {
109                                                            if (_log.isWarnEnabled()) {
110                                                                    _log.warn("Default ratings type is null");
111                                                            }
112    
113                                                            return null;
114                                                    }
115    
116                                                    String portletId =
117                                                            portletRatingsDefinition.getPortletId();
118    
119                                                    if (Validator.isNull(portletId)) {
120                                                            if (_log.isWarnEnabled()) {
121                                                                    _log.warn("Portlet ID is null");
122                                                            }
123    
124                                                            return null;
125                                                    }
126    
127                                                    return new PortletRatingsDefinitionValues(
128                                                            classNames, defaultRatingsType, portletId);
129                                            }
130    
131                                            @Override
132                                            public void modifiedService(
133                                                    ServiceReference<PortletRatingsDefinition>
134                                                            serviceReference,
135                                                    PortletRatingsDefinitionValues
136                                                            portletRatingsDefinitionValues) {
137                                            }
138    
139                                            @Override
140                                            public void removedService(
141                                                    ServiceReference<PortletRatingsDefinition>
142                                                            serviceReference,
143                                                    PortletRatingsDefinitionValues
144                                                            portletRatingsDefinitionValues) {
145    
146                                                    Registry registry = RegistryUtil.getRegistry();
147    
148                                                    registry.ungetService(serviceReference);
149                                            }
150    
151                                    };
152    
153            private static final ServiceTrackerMap
154                    <String, PortletRatingsDefinitionValues> _serviceTrackerMap =
155                            ServiceTrackerCollections.openSingleValueMap(
156                                    PortletRatingsDefinition.class, "model.class.name",
157                                    _serviceTrackerCustomizer);
158    
159    }