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.ratings.kernel.display.context;
016    
017    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
018    import com.liferay.portal.kernel.theme.ThemeDisplay;
019    import com.liferay.portal.kernel.util.PrefsParamUtil;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.ratings.kernel.RatingsType;
022    import com.liferay.ratings.kernel.definition.PortletRatingsDefinitionUtil;
023    import com.liferay.ratings.kernel.definition.PortletRatingsDefinitionValues;
024    import com.liferay.ratings.kernel.transformer.RatingsDataTransformerUtil;
025    
026    import java.util.Collections;
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    import javax.portlet.PortletPreferences;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Roberto D??az
036     */
037    public class CompanyPortletRatingsDefinitionDisplayContext {
038    
039            public CompanyPortletRatingsDefinitionDisplayContext(
040                    PortletPreferences companyPortletPreferences,
041                    HttpServletRequest request) {
042    
043                    _populateRatingsTypeMaps(companyPortletPreferences, request);
044            }
045    
046            public Map<String, Map<String, RatingsType>> getCompanyRatingsTypeMaps() {
047                    return Collections.unmodifiableMap(_companyRatingsTypeMaps);
048            }
049    
050            public RatingsType getRatingsType(String portletId, String className) {
051                    Map<String, RatingsType> ratingsTypeMap = _companyRatingsTypeMaps.get(
052                            portletId);
053    
054                    return ratingsTypeMap.get(className);
055            }
056    
057            private void _populateRatingsTypeMaps(
058                    PortletPreferences companyPortletPreferences,
059                    HttpServletRequest request) {
060    
061                    Map<String, PortletRatingsDefinitionValues>
062                            portletRatingsDefinitionValuesMap =
063                                    PortletRatingsDefinitionUtil.
064                                            getPortletRatingsDefinitionValuesMap();
065    
066                    for (Map.Entry<String, PortletRatingsDefinitionValues> entry :
067                                    portletRatingsDefinitionValuesMap.entrySet()) {
068    
069                            PortletRatingsDefinitionValues portletRatingsDefinitionValues =
070                                    entry.getValue();
071    
072                            if (portletRatingsDefinitionValues == null) {
073                                    continue;
074                            }
075    
076                            String portletId = portletRatingsDefinitionValues.getPortletId();
077    
078                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
079                                    WebKeys.THEME_DISPLAY);
080    
081                            if (!PortletLocalServiceUtil.hasPortlet(
082                                            themeDisplay.getCompanyId(), portletId)) {
083    
084                                    continue;
085                            }
086    
087                            String className = entry.getKey();
088    
089                            Map<String, RatingsType> ratingsTypeMap = new HashMap<>();
090    
091                            String propertyKey = RatingsDataTransformerUtil.getPropertyKey(
092                                    className);
093    
094                            RatingsType ratingsType =
095                                    portletRatingsDefinitionValues.getDefaultRatingsType();
096    
097                            String companyRatingsTypeString = PrefsParamUtil.getString(
098                                    companyPortletPreferences, request, propertyKey,
099                                    ratingsType.getValue());
100    
101                            ratingsTypeMap.put(
102                                    className, RatingsType.parse(companyRatingsTypeString));
103    
104                            _companyRatingsTypeMaps.put(portletId, ratingsTypeMap);
105                    }
106            }
107    
108            private final Map<String, Map<String, RatingsType>>
109                    _companyRatingsTypeMaps = new HashMap<>();
110    
111    }