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