001
014
015 package com.liferay.portlet.ratings.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
035 public class RateEntryAction extends JSONAction {
036
037 public String getJSON(
038 ActionMapping mapping, ActionForm form, HttpServletRequest request,
039 HttpServletResponse response)
040 throws Exception {
041
042 String className = getClassName(request);
043 long classPK = getClassPK(request);
044 double score = ParamUtil.getDouble(request, "score");
045
046 if (score == 0) {
047 RatingsEntryServiceUtil.deleteEntry(className, classPK);
048 }
049 else {
050 RatingsEntryServiceUtil.updateEntry(className, classPK, score);
051 }
052
053 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
054 className, classPK);
055
056 double averageScore = MathUtil.format(stats.getAverageScore(), 1, 1);
057
058 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
059
060 jsonObj.put("totalEntries", stats.getTotalEntries());
061 jsonObj.put("totalScore", stats.getTotalScore());
062 jsonObj.put("averageScore", averageScore);
063
064 return jsonObj.toString();
065 }
066
067 protected String getClassName(HttpServletRequest request) {
068 return ParamUtil.getString(request, "className");
069 }
070
071 protected long getClassPK(HttpServletRequest request) {
072 return ParamUtil.getLong(request, "classPK");
073 }
074
075 }