001    /**
002     * Copyright (c) 2000-2010 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.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    /**
033     * @author Brian Wing Shun Chan
034     */
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    }