001    /**
002     * Copyright (c) 2000-2013 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.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.MathUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.struts.JSONAction;
022    import com.liferay.portlet.ratings.model.RatingsStats;
023    import com.liferay.portlet.ratings.service.RatingsEntryServiceUtil;
024    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.struts.action.ActionForm;
030    import org.apache.struts.action.ActionMapping;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class RateEntryAction extends JSONAction {
036    
037            @Override
038            public String getJSON(
039                            ActionMapping actionMapping, ActionForm actionForm,
040                            HttpServletRequest request, HttpServletResponse response)
041                    throws Exception {
042    
043                    String className = getClassName(request);
044                    long classPK = getClassPK(request);
045                    double score = ParamUtil.getDouble(request, "score");
046    
047                    if (score == 0) {
048                            RatingsEntryServiceUtil.deleteEntry(className, classPK);
049                    }
050                    else {
051                            RatingsEntryServiceUtil.updateEntry(className, classPK, score);
052                    }
053    
054                    RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
055                            className, classPK);
056    
057                    double averageScore = MathUtil.format(stats.getAverageScore(), 1, 1);
058    
059                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
060    
061                    jsonObj.put("totalEntries", stats.getTotalEntries());
062                    jsonObj.put("totalScore", stats.getTotalScore());
063                    jsonObj.put("averageScore", averageScore);
064    
065                    return jsonObj.toString();
066            }
067    
068            protected String getClassName(HttpServletRequest request) {
069                    return ParamUtil.getString(request, "className");
070            }
071    
072            protected long getClassPK(HttpServletRequest request) {
073                    return ParamUtil.getLong(request, "classPK");
074            }
075    
076    }