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.taglib.ui;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.PrefsParamUtil;
019    import com.liferay.portal.kernel.util.PrefsPropsUtil;
020    import com.liferay.portal.kernel.util.PropertiesParamUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portlet.ratings.RatingsType;
029    import com.liferay.portlet.ratings.definition.PortletRatingsDefinitionUtil;
030    import com.liferay.portlet.ratings.model.RatingsEntry;
031    import com.liferay.portlet.ratings.model.RatingsStats;
032    import com.liferay.portlet.ratings.transformer.RatingsDataTransformerUtil;
033    import com.liferay.taglib.util.IncludeTag;
034    
035    import javax.portlet.PortletPreferences;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     * @author Roberto D??az
043     */
044    public class RatingsTag extends IncludeTag {
045    
046            public void setClassName(String className) {
047                    _className = className;
048            }
049    
050            public void setClassPK(long classPK) {
051                    _classPK = classPK;
052            }
053    
054            public void setNumberOfStars(int numberOfStars) {
055                    _numberOfStars = numberOfStars;
056            }
057    
058            public void setRatingsEntry(RatingsEntry ratingsEntry) {
059                    _ratingsEntry = ratingsEntry;
060    
061                    _setRatingsEntry = true;
062            }
063    
064            public void setRatingsStats(RatingsStats ratingsStats) {
065                    _ratingsStats = ratingsStats;
066    
067                    _setRatingsStats = true;
068            }
069    
070            public void setRound(boolean round) {
071                    _round = round;
072            }
073    
074            public void setType(String type) {
075                    _type = type;
076            }
077    
078            public void setUrl(String url) {
079                    _url = url;
080            }
081    
082            @Override
083            protected void cleanUp() {
084                    _className = null;
085                    _classPK = 0;
086                    _numberOfStars = _DEFAULT_NUMBER_OF_STARS;
087                    _ratingsEntry = null;
088                    _ratingsStats = null;
089                    _round = true;
090                    _setRatingsEntry = false;
091                    _setRatingsStats = false;
092                    _type = null;
093                    _url = null;
094            }
095    
096            @Override
097            protected String getPage() {
098                    return _PAGE;
099            }
100    
101            protected String getType(HttpServletRequest request) {
102                    if (Validator.isNotNull(_type)) {
103                            return _type;
104                    }
105    
106                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
107                            WebKeys.THEME_DISPLAY);
108    
109                    long companyId = themeDisplay.getCompanyId();
110    
111                    PortletPreferences companyPortletPreferences =
112                            PrefsPropsUtil.getPreferences(companyId);
113    
114                    Group group = themeDisplay.getSiteGroup();
115    
116                    if (group.isStagingGroup()) {
117                            group = group.getLiveGroup();
118                    }
119    
120                    UnicodeProperties groupTypeSettings = new UnicodeProperties();
121    
122                    if (group != null) {
123                            groupTypeSettings = group.getTypeSettingsProperties();
124                    }
125    
126                    RatingsType defaultRatingsType =
127                            PortletRatingsDefinitionUtil.getDefaultRatingsType(_className);
128    
129                    if (defaultRatingsType != null) {
130                            String propertyKey = RatingsDataTransformerUtil.getPropertyKey(
131                                    _className);
132    
133                            String companyRatingsType = PrefsParamUtil.getString(
134                                    companyPortletPreferences, request, propertyKey,
135                                    defaultRatingsType.getValue());
136    
137                            String type = PropertiesParamUtil.getString(
138                                    groupTypeSettings, request, propertyKey, companyRatingsType);
139    
140                            if (Validator.isNotNull(type)) {
141                                    return type;
142                            }
143                    }
144    
145                    return _DEFAULT_TYPE;
146            }
147    
148            @Override
149            protected boolean isCleanUpSetAttributes() {
150                    return _CLEAN_UP_SET_ATTRIBUTES;
151            }
152    
153            @Override
154            protected void setAttributes(HttpServletRequest request) {
155                    request.setAttribute("liferay-ui:ratings:className", _className);
156                    request.setAttribute(
157                            "liferay-ui:ratings:classPK", String.valueOf(_classPK));
158                    request.setAttribute(
159                            "liferay-ui:ratings:numberOfStars", String.valueOf(_numberOfStars));
160                    request.setAttribute("liferay-ui:ratings:ratingsEntry", _ratingsEntry);
161                    request.setAttribute("liferay-ui:ratings:ratingsStats", _ratingsStats);
162                    request.setAttribute(
163                            "liferay-ui:ratings:round", String.valueOf(_round));
164                    request.setAttribute(
165                            "liferay-ui:ratings:setRatingsEntry",
166                            String.valueOf(_setRatingsEntry));
167                    request.setAttribute(
168                            "liferay-ui:ratings:setRatingsStats",
169                            String.valueOf(_setRatingsStats));
170                    request.setAttribute("liferay-ui:ratings:type", getType(request));
171                    request.setAttribute("liferay-ui:ratings:url", _url);
172            }
173    
174            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
175    
176            private static final int _DEFAULT_NUMBER_OF_STARS = GetterUtil.getInteger(
177                    PropsUtil.get(PropsKeys.RATINGS_DEFAULT_NUMBER_OF_STARS));
178    
179            private static final String _DEFAULT_TYPE = RatingsType.STARS.getValue();
180    
181            private static final String _PAGE = "/html/taglib/ui/ratings/page.jsp";
182    
183            private String _className;
184            private long _classPK;
185            private int _numberOfStars = _DEFAULT_NUMBER_OF_STARS;
186            private RatingsEntry _ratingsEntry;
187            private RatingsStats _ratingsStats;
188            private boolean _round;
189            private boolean _setRatingsEntry;
190            private boolean _setRatingsStats;
191            private String _type;
192            private String _url;
193    
194    }