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