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