001    /**
002     * Copyright (c) 2000-2012 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.pageratings.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
023    
024    import javax.portlet.PortletPreferences;
025    
026    /**
027     * @author Bruno Farache
028     */
029    public class PageRatingsPortletDataHandler extends BasePortletDataHandler {
030    
031            public static final String NAMESPACE = "page_ratings";
032    
033            public PageRatingsPortletDataHandler() {
034                    setExportControls(
035                            new PortletDataHandlerBoolean(NAMESPACE, "ratings", true, true));
036            }
037    
038            @Override
039            protected PortletPreferences doDeleteData(
040                            PortletDataContext portletDataContext, String portletId,
041                            PortletPreferences portletPreferences)
042                    throws Exception {
043    
044                    RatingsStatsLocalServiceUtil.deleteStats(
045                            Layout.class.getName(), portletDataContext.getPlid());
046    
047                    return portletPreferences;
048            }
049    
050            @Override
051            protected String doExportData(
052                            PortletDataContext portletDataContext, String portletId,
053                            PortletPreferences portletPreferences)
054                    throws Exception {
055    
056                    portletDataContext.addRatingsEntries(
057                            Layout.class, portletDataContext.getPlid());
058    
059                    return String.valueOf(portletDataContext.getPlid());
060            }
061    
062            @Override
063            protected PortletPreferences doImportData(
064                            PortletDataContext portletDataContext, String portletId,
065                            PortletPreferences portletPreferences, String data)
066                    throws Exception {
067    
068                    portletDataContext.importRatingsEntries(
069                            Layout.class, GetterUtil.getLong(data),
070                            portletDataContext.getPlid());
071    
072                    return null;
073            }
074    
075    }