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.layoutprototypes.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.DataLevel;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.lar.StagedModelType;
024    import com.liferay.portal.kernel.lar.xstream.XStreamAliasRegistryUtil;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.model.LayoutPrototype;
027    import com.liferay.portal.model.impl.LayoutPrototypeImpl;
028    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
029    
030    import java.util.List;
031    
032    import javax.portlet.PortletPreferences;
033    
034    /**
035     * @author Daniela Zapata Riesco
036     */
037    public class LayoutPrototypePortletDataHandler extends BasePortletDataHandler {
038    
039            public static final String NAMESPACE = "layout_prototypes";
040    
041            public LayoutPrototypePortletDataHandler() {
042                    setDataLevel(DataLevel.PORTAL);
043                    setDeletionSystemEventStagedModelTypes(
044                            new StagedModelType(LayoutPrototype.class));
045                    setExportControls(
046                            new PortletDataHandlerBoolean(
047                                    NAMESPACE, "page-templates", true, true, null,
048                                    LayoutPrototype.class.getName()));
049    
050                    XStreamAliasRegistryUtil.register(
051                            LayoutPrototypeImpl.class, "LayoutPrototype");
052            }
053    
054            @Override
055            protected PortletPreferences doDeleteData(
056                            PortletDataContext portletDataContext, String portletId,
057                            PortletPreferences portletPreferences)
058                    throws Exception {
059    
060                    if (portletDataContext.addPrimaryKey(
061                                    LayoutPrototypePortletDataHandler.class, "deleteData")) {
062    
063                            return portletPreferences;
064                    }
065    
066                    LayoutPrototypeLocalServiceUtil.deleteNondefaultLayoutPrototypes(
067                            portletDataContext.getCompanyId());
068    
069                    return portletPreferences;
070            }
071    
072            @Override
073            protected String doExportData(
074                            final PortletDataContext portletDataContext, String portletId,
075                            PortletPreferences portletPreferences)
076                    throws Exception {
077    
078                    portletDataContext.addPortalPermissions();
079    
080                    Element rootElement = addExportDataRootElement(portletDataContext);
081    
082                    rootElement.addAttribute(
083                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
084    
085                    ActionableDynamicQuery actionableDynamicQuery =
086                            LayoutPrototypeLocalServiceUtil.getExportActionableDynamicQuery(
087                                    portletDataContext);
088    
089                    actionableDynamicQuery.performActions();
090    
091                    return getExportDataRootElementString(rootElement);
092            }
093    
094            @Override
095            protected PortletPreferences doImportData(
096                            PortletDataContext portletDataContext, String portletId,
097                            PortletPreferences portletPreferences, String data)
098                    throws Exception {
099    
100                    portletDataContext.importPortalPermissions();
101    
102                    Element layoutPrototypesElement =
103                            portletDataContext.getImportDataGroupElement(LayoutPrototype.class);
104    
105                    List<Element> layoutPrototypeElements =
106                            layoutPrototypesElement.elements();
107    
108                    for (Element layoutPrototypeElement : layoutPrototypeElements) {
109                            StagedModelDataHandlerUtil.importStagedModel(
110                                    portletDataContext, layoutPrototypeElement);
111                    }
112    
113                    return null;
114            }
115    
116            @Override
117            protected void doPrepareManifestSummary(
118                            PortletDataContext portletDataContext,
119                            PortletPreferences portletPreferences)
120                    throws Exception {
121    
122                    ActionableDynamicQuery layoutPrototypeExportActionableDynamicQuery =
123                            LayoutPrototypeLocalServiceUtil.getExportActionableDynamicQuery(
124                                    portletDataContext);
125    
126                    layoutPrototypeExportActionableDynamicQuery.performCount();
127            }
128    
129    }