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