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