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