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.display.context;
016    
017    import com.liferay.portal.kernel.util.PrefsParamUtil;
018    import com.liferay.portal.kernel.util.WebKeys;
019    import com.liferay.portal.service.PortletLocalServiceUtil;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portlet.ratings.RatingsType;
022    import com.liferay.portlet.ratings.definition.PortletRatingsDefinitionUtil;
023    import com.liferay.portlet.ratings.definition.PortletRatingsDefinitionValues;
024    import com.liferay.portlet.ratings.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 (String className : portletRatingsDefinitionValuesMap.keySet()) {
067                            PortletRatingsDefinitionValues portletRatingsDefinitionValues =
068                                    portletRatingsDefinitionValuesMap.get(className);
069    
070                            if (portletRatingsDefinitionValues == null) {
071                                    continue;
072                            }
073    
074                            String portletId = portletRatingsDefinitionValues.getPortletId();
075    
076                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
077                                    WebKeys.THEME_DISPLAY);
078    
079                            if (!PortletLocalServiceUtil.hasPortlet(
080                                            themeDisplay.getCompanyId(), portletId)) {
081    
082                                    continue;
083                            }
084    
085                            Map<String, RatingsType> ratingsTypeMap = new HashMap<>();
086    
087                            String propertyKey = RatingsDataTransformerUtil.getPropertyKey(
088                                    className);
089    
090                            RatingsType ratingsType =
091                                    portletRatingsDefinitionValues.getDefaultRatingsType();
092    
093                            String companyRatingsTypeString = PrefsParamUtil.getString(
094                                    companyPortletPreferences, request, propertyKey,
095                                    ratingsType.getValue());
096    
097                            ratingsTypeMap.put(
098                                    className, RatingsType.parse(companyRatingsTypeString));
099    
100                            _companyRatingsTypeMaps.put(portletId, ratingsTypeMap);
101                    }
102            }
103    
104            private final Map<String, Map<String, RatingsType>>
105                    _companyRatingsTypeMaps = new HashMap<>();
106    
107    }