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