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.PropertiesParamUtil;
018    import com.liferay.portal.kernel.util.UnicodeProperties;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.service.PortletLocalServiceUtil;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portlet.ratings.RatingsType;
024    import com.liferay.portlet.ratings.definition.PortletRatingsDefinitionUtil;
025    import com.liferay.portlet.ratings.definition.PortletRatingsDefinitionValues;
026    import com.liferay.portlet.ratings.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 (String className : portletRatingsDefinitionValuesMap.keySet()) {
058                            PortletRatingsDefinitionValues portletRatingsDefinitionValues =
059                                    portletRatingsDefinitionValuesMap.get(className);
060    
061                            if (portletRatingsDefinitionValues == null) {
062                                    continue;
063                            }
064    
065                            String portletId = portletRatingsDefinitionValues.getPortletId();
066    
067                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
068                                    WebKeys.THEME_DISPLAY);
069    
070                            if (!PortletLocalServiceUtil.hasPortlet(
071                                            themeDisplay.getCompanyId(), portletId)) {
072    
073                                    continue;
074                            }
075    
076                            Map<String, RatingsType> ratingsTypeMap = new HashMap<>();
077    
078                            String propertyKey = RatingsDataTransformerUtil.getPropertyKey(
079                                    className);
080    
081                            String groupRatingsTypeString = PropertiesParamUtil.getString(
082                                    groupTypeSettings, request, propertyKey);
083    
084                            RatingsType ratingsType = null;
085    
086                            if (Validator.isNotNull(groupRatingsTypeString)) {
087                                    ratingsType = RatingsType.parse(groupRatingsTypeString);
088                            }
089    
090                            ratingsTypeMap.put(className, ratingsType);
091    
092                            _groupRatingsTypeMaps.put(portletId, ratingsTypeMap);
093                    }
094            }
095    
096            private final Map<String, Map<String, RatingsType>>
097                    _groupRatingsTypeMaps = new HashMap<>();
098    
099    }