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.portlet.pagecomments.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.kernel.xml.Element;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
024    
025    import javax.portlet.PortletPreferences;
026    
027    /**
028     * @author Bruno Farache
029     * @author Hugo Huijser
030     */
031    public class PageCommentsPortletDataHandler extends BasePortletDataHandler {
032    
033            public static final String NAMESPACE = "page_comments";
034    
035            public PageCommentsPortletDataHandler() {
036                    setExportControls(
037                            new PortletDataHandlerBoolean(NAMESPACE, "comments", true, true));
038            }
039    
040            @Override
041            protected PortletPreferences doDeleteData(
042                            PortletDataContext portletDataContext, String portletId,
043                            PortletPreferences portletPreferences)
044                    throws Exception {
045    
046                    MBMessageLocalServiceUtil.deleteDiscussionMessages(
047                            Layout.class.getName(), portletDataContext.getPlid());
048    
049                    return portletPreferences;
050            }
051    
052            @Override
053            protected String doExportData(
054                            PortletDataContext portletDataContext, String portletId,
055                            PortletPreferences portletPreferences)
056                    throws Exception {
057    
058                    portletDataContext.addPermissions(
059                            "com.liferay.portlet.pagecomments",
060                            portletDataContext.getScopeGroupId());
061    
062                    Element rootElement = addExportDataRootElement(portletDataContext);
063    
064                    rootElement.addAttribute(
065                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
066                    rootElement.addAttribute(
067                            "plid", String.valueOf(portletDataContext.getPlid()));
068    
069                    if (portletDataContext.getBooleanParameter(NAMESPACE, "comments")) {
070                            portletDataContext.addComments(
071                                    Layout.class, portletDataContext.getPlid());
072                    }
073    
074                    return getExportDataRootElementString(rootElement);
075            }
076    
077            @Override
078            protected PortletPreferences doImportData(
079                            PortletDataContext portletDataContext, String portletId,
080                            PortletPreferences portletPreferences, String data)
081                    throws Exception {
082    
083                    portletDataContext.importPermissions(
084                            "com.liferay.portlet.pagecomments",
085                            portletDataContext.getSourceGroupId(),
086                            portletDataContext.getScopeGroupId());
087    
088                    Element rootElement = portletDataContext.getImportDataRootElement();
089    
090                    long plid = GetterUtil.getLong(rootElement.attributeValue("plid"));
091    
092                    portletDataContext.importComments(
093                            Layout.class, plid, portletDataContext.getPlid(),
094                            portletDataContext.getScopeGroupId());
095    
096                    return null;
097            }
098    
099    }