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