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.addPermissions(
094                            BlogsPermission.RESOURCE_NAME,
095                            portletDataContext.getScopeGroupId());
096    
097                    rootElement.addAttribute(
098                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
099    
100                    ActionableDynamicQuery actionableDynamicQuery =
101                            new BlogsEntryExportActionableDynamicQuery(portletDataContext);
102    
103                    actionableDynamicQuery.performActions();
104    
105                    return getExportDataRootElementString(rootElement);
106            }
107    
108            @Override
109            protected PortletPreferences doImportData(
110                            PortletDataContext portletDataContext, String portletId,
111                            PortletPreferences portletPreferences, String data)
112                    throws Exception {
113    
114                    if (!portletDataContext.getBooleanParameter(NAMESPACE, "entries")) {
115                            return null;
116                    }
117    
118                    portletDataContext.importPermissions(
119                            BlogsPermission.RESOURCE_NAME,
120                            portletDataContext.getSourceGroupId(),
121                            portletDataContext.getScopeGroupId());
122    
123                    Element entriesElement = portletDataContext.getImportDataGroupElement(
124                            BlogsEntry.class);
125    
126                    List<Element> entryElements = entriesElement.elements();
127    
128                    for (Element entryElement : entryElements) {
129                            StagedModelDataHandlerUtil.importStagedModel(
130                                    portletDataContext, entryElement);
131                    }
132    
133                    return null;
134            }
135    
136            @Override
137            protected void doPrepareManifestSummary(
138                            PortletDataContext portletDataContext,
139                            PortletPreferences portletPreferences)
140                    throws Exception {
141    
142                    ActionableDynamicQuery actionableDynamicQuery =
143                            new BlogsEntryExportActionableDynamicQuery(portletDataContext);
144    
145                    actionableDynamicQuery.performCount();
146            }
147    
148    }