001
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
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 }