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    import com.liferay.portal.util.PortletKeys;
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    
051            @Override
052            protected PortletPreferences doDeleteData(
053                            PortletDataContext portletDataContext, String portletId,
054                            PortletPreferences portletPreferences)
055                    throws Exception {
056    
057                    if (portletDataContext.addPrimaryKey(
058                                    LayoutPrototypePortletDataHandler.class, "deleteData")) {
059    
060                            return portletPreferences;
061                    }
062    
063                    LayoutPrototypeLocalServiceUtil.deleteNondefaultLayoutPrototypes(
064                            portletDataContext.getCompanyId());
065    
066                    return portletPreferences;
067            }
068    
069            @Override
070            protected String doExportData(
071                            final PortletDataContext portletDataContext, String portletId,
072                            PortletPreferences portletPreferences)
073                    throws Exception {
074    
075                    portletDataContext.addPermissions(
076                            PortletKeys.PORTAL, portletDataContext.getCompanyId());
077    
078                    Element rootElement = addExportDataRootElement(portletDataContext);
079    
080                    rootElement.addAttribute(
081                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
082    
083                    ActionableDynamicQuery actionableDynamicQuery =
084                            new LayoutPrototypeExportActionableDynamicQuery(portletDataContext);
085    
086                    actionableDynamicQuery.performActions();
087    
088                    return getExportDataRootElementString(rootElement);
089            }
090    
091            @Override
092            protected PortletPreferences doImportData(
093                            PortletDataContext portletDataContext, String portletId,
094                            PortletPreferences portletPreferences, String data)
095                    throws Exception {
096    
097                    portletDataContext.importPermissions(
098                            PortletKeys.PORTAL, portletDataContext.getSourceCompanyId(),
099                            portletDataContext.getCompanyId());
100    
101                    Element layoutPrototypesElement =
102                            portletDataContext.getImportDataGroupElement(LayoutPrototype.class);
103    
104                    List<Element> layoutPrototypeElements =
105                            layoutPrototypesElement.elements();
106    
107                    for (Element layoutPrototypeElement : layoutPrototypeElements) {
108                            StagedModelDataHandlerUtil.importStagedModel(
109                                    portletDataContext, layoutPrototypeElement);
110                    }
111    
112                    return null;
113            }
114    
115            @Override
116            protected void doPrepareManifestSummary(
117                            PortletDataContext portletDataContext,
118                            PortletPreferences portletPreferences)
119                    throws Exception {
120    
121                    ActionableDynamicQuery layoutPrototypeExportActionableDynamicQuery =
122                            new LayoutPrototypeExportActionableDynamicQuery(portletDataContext);
123    
124                    layoutPrototypeExportActionableDynamicQuery.performCount();
125            }
126    
127    }