001    /**
002     * Copyright (c) 2000-present 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.blogs.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.util.PropsValues;
020    import com.liferay.portlet.blogs.model.BlogsEntry;
021    import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
022    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
023    import com.liferay.portlet.blogs.service.BlogsStatsUserLocalServiceUtil;
024    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
025    import com.liferay.portlet.blogs.util.BlogsConstants;
026    import com.liferay.portlet.exportimport.lar.BasePortletDataHandler;
027    import com.liferay.portlet.exportimport.lar.PortletDataContext;
028    import com.liferay.portlet.exportimport.lar.PortletDataHandlerBoolean;
029    import com.liferay.portlet.exportimport.lar.PortletDataHandlerControl;
030    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
031    import com.liferay.portlet.exportimport.lar.StagedModelType;
032    import com.liferay.portlet.exportimport.xstream.XStreamAliasRegistryUtil;
033    
034    import java.util.List;
035    
036    import javax.portlet.PortletPreferences;
037    
038    /**
039     * @author Bruno Farache
040     * @author Raymond Aug??
041     * @author Juan Fern??ndez
042     * @author Zsolt Berentey
043     */
044    public class BlogsPortletDataHandler extends BasePortletDataHandler {
045    
046            public static final String NAMESPACE = "blogs";
047    
048            public BlogsPortletDataHandler() {
049                    setDeletionSystemEventStagedModelTypes(
050                            new StagedModelType(BlogsEntry.class));
051                    setExportControls(
052                            new PortletDataHandlerBoolean(
053                                    NAMESPACE, "entries", true, false,
054                                    new PortletDataHandlerControl[] {
055                                            new PortletDataHandlerBoolean(
056                                                    NAMESPACE, "referenced-content")
057                                    },
058                                    BlogsEntry.class.getName()));
059                    setPublishToLiveByDefault(PropsValues.BLOGS_PUBLISH_TO_LIVE_BY_DEFAULT);
060    
061                    XStreamAliasRegistryUtil.register(BlogsEntryImpl.class, "BlogsEntry");
062            }
063    
064            @Override
065            public String getServiceName() {
066                    return BlogsConstants.SERVICE_NAME;
067            }
068    
069            @Override
070            protected PortletPreferences doDeleteData(
071                            PortletDataContext portletDataContext, String portletId,
072                            PortletPreferences portletPreferences)
073                    throws Exception {
074    
075                    if (portletDataContext.addPrimaryKey(
076                                    BlogsPortletDataHandler.class, "deleteData")) {
077    
078                            return portletPreferences;
079                    }
080    
081                    BlogsEntryLocalServiceUtil.deleteEntries(
082                            portletDataContext.getScopeGroupId());
083    
084                    BlogsStatsUserLocalServiceUtil.deleteStatsUserByGroupId(
085                            portletDataContext.getScopeGroupId());
086    
087                    return portletPreferences;
088            }
089    
090            @Override
091            protected String doExportData(
092                            final PortletDataContext portletDataContext, String portletId,
093                            PortletPreferences portletPreferences)
094                    throws Exception {
095    
096                    Element rootElement = addExportDataRootElement(portletDataContext);
097    
098                    if (!portletDataContext.getBooleanParameter(NAMESPACE, "entries")) {
099                            return getExportDataRootElementString(rootElement);
100                    }
101    
102                    portletDataContext.addPortletPermissions(BlogsPermission.RESOURCE_NAME);
103    
104                    rootElement.addAttribute(
105                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
106    
107                    ActionableDynamicQuery actionableDynamicQuery =
108                            BlogsEntryLocalServiceUtil.getExportActionableDynamicQuery(
109                                    portletDataContext);
110    
111                    actionableDynamicQuery.performActions();
112    
113                    return getExportDataRootElementString(rootElement);
114            }
115    
116            @Override
117            protected PortletPreferences doImportData(
118                            PortletDataContext portletDataContext, String portletId,
119                            PortletPreferences portletPreferences, String data)
120                    throws Exception {
121    
122                    if (!portletDataContext.getBooleanParameter(NAMESPACE, "entries")) {
123                            return null;
124                    }
125    
126                    portletDataContext.importPortletPermissions(
127                            BlogsPermission.RESOURCE_NAME);
128    
129                    Element entriesElement = portletDataContext.getImportDataGroupElement(
130                            BlogsEntry.class);
131    
132                    List<Element> entryElements = entriesElement.elements();
133    
134                    for (Element entryElement : entryElements) {
135                            StagedModelDataHandlerUtil.importStagedModel(
136                                    portletDataContext, entryElement);
137                    }
138    
139                    return null;
140            }
141    
142            @Override
143            protected void doPrepareManifestSummary(
144                            PortletDataContext portletDataContext,
145                            PortletPreferences portletPreferences)
146                    throws Exception {
147    
148                    ActionableDynamicQuery actionableDynamicQuery =
149                            BlogsEntryLocalServiceUtil.getExportActionableDynamicQuery(
150                                    portletDataContext);
151    
152                    actionableDynamicQuery.performCount();
153            }
154    
155    }