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.exception.PortalException;
018    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.LayoutConstants;
023    import com.liferay.portal.model.LayoutPrototype;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
029    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
030    import com.liferay.portlet.exportimport.lar.PortletDataContext;
031    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    /**
037     * @author Daniela Zapata Riesco
038     */
039    @OSGiBeanProperties
040    public class LayoutPrototypeStagedModelDataHandler
041            extends BaseStagedModelDataHandler<LayoutPrototype> {
042    
043            public static final String[] CLASS_NAMES =
044                    {LayoutPrototype.class.getName()};
045    
046            @Override
047            public void deleteStagedModel(LayoutPrototype layoutPrototype)
048                    throws PortalException {
049    
050                    LayoutPrototypeLocalServiceUtil.deleteLayoutPrototype(layoutPrototype);
051            }
052    
053            @Override
054            public void deleteStagedModel(
055                            String uuid, long groupId, String className, String extraData)
056                    throws PortalException {
057    
058                    Group group = GroupLocalServiceUtil.getGroup(groupId);
059    
060                    LayoutPrototype layoutPrototype =
061                            LayoutPrototypeLocalServiceUtil.
062                                    fetchLayoutPrototypeByUuidAndCompanyId(
063                                            uuid, group.getCompanyId());
064    
065                    if (layoutPrototype != null) {
066                            deleteStagedModel(layoutPrototype);
067                    }
068            }
069    
070            @Override
071            public List<LayoutPrototype> fetchStagedModelsByUuidAndCompanyId(
072                    String uuid, long companyId) {
073    
074                    List<LayoutPrototype> layoutPrototypes = new ArrayList<>();
075    
076                    layoutPrototypes.add(
077                            LayoutPrototypeLocalServiceUtil.
078                                    fetchLayoutPrototypeByUuidAndCompanyId(uuid, companyId));
079    
080                    return layoutPrototypes;
081            }
082    
083            @Override
084            public String[] getClassNames() {
085                    return CLASS_NAMES;
086            }
087    
088            @Override
089            public String getDisplayName(LayoutPrototype layoutPrototype) {
090                    return layoutPrototype.getNameCurrentValue();
091            }
092    
093            @Override
094            protected void doExportStagedModel(
095                            PortletDataContext portletDataContext,
096                            LayoutPrototype layoutPrototype)
097                    throws Exception {
098    
099                    exportLayouts(portletDataContext, layoutPrototype);
100    
101                    Element layoutPrototypeElement =
102                            portletDataContext.getExportDataElement(layoutPrototype);
103    
104                    portletDataContext.addClassedModel(
105                            layoutPrototypeElement,
106                            ExportImportPathUtil.getModelPath(layoutPrototype),
107                            layoutPrototype);
108            }
109    
110            @Override
111            protected void doImportStagedModel(
112                            PortletDataContext portletDataContext,
113                            LayoutPrototype layoutPrototype)
114                    throws Exception {
115    
116                    long userId = portletDataContext.getUserId(
117                            layoutPrototype.getUserUuid());
118    
119                    ServiceContext serviceContext = portletDataContext.createServiceContext(
120                            layoutPrototype);
121    
122                    serviceContext.setAttribute("addDefaultLayout", false);
123    
124                    LayoutPrototype importedLayoutPrototype = null;
125    
126                    if (portletDataContext.isDataStrategyMirror()) {
127                            LayoutPrototype existingLayoutPrototype =
128                                    LayoutPrototypeLocalServiceUtil.
129                                            fetchLayoutPrototypeByUuidAndCompanyId(
130                                                    layoutPrototype.getUuid(),
131                                                    portletDataContext.getCompanyId());
132    
133                            if (existingLayoutPrototype == null) {
134                                    serviceContext.setUuid(layoutPrototype.getUuid());
135    
136                                    importedLayoutPrototype =
137                                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
138                                                    userId, portletDataContext.getCompanyId(),
139                                                    layoutPrototype.getNameMap(),
140                                                    layoutPrototype.getDescriptionMap(),
141                                                    layoutPrototype.isActive(), serviceContext);
142                            }
143                            else {
144                                    importedLayoutPrototype =
145                                            LayoutPrototypeLocalServiceUtil.updateLayoutPrototype(
146                                                    existingLayoutPrototype.getLayoutPrototypeId(),
147                                                    layoutPrototype.getNameMap(),
148                                                    layoutPrototype.getDescriptionMap(),
149                                                    layoutPrototype.isActive(), serviceContext);
150                            }
151                    }
152                    else {
153                            importedLayoutPrototype =
154                                    LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
155                                            userId, portletDataContext.getCompanyId(),
156                                            layoutPrototype.getNameMap(),
157                                            layoutPrototype.getDescriptionMap(),
158                                            layoutPrototype.isActive(), serviceContext);
159                    }
160    
161                    importLayouts(
162                            portletDataContext, layoutPrototype,
163                            importedLayoutPrototype.getGroupId());
164    
165                    portletDataContext.importClassedModel(
166                            layoutPrototype, importedLayoutPrototype);
167            }
168    
169            protected void exportLayouts(
170                            PortletDataContext portletDataContext,
171                            LayoutPrototype layoutPrototype)
172                    throws Exception {
173    
174                    long groupId = portletDataContext.getGroupId();
175                    boolean privateLayout = portletDataContext.isPrivateLayout();
176                    long scopeGroupId = portletDataContext.getScopeGroupId();
177    
178                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
179                            layoutPrototype.getGroupId(), true,
180                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
181    
182                    try {
183                            portletDataContext.setGroupId(layoutPrototype.getGroupId());
184                            portletDataContext.setPrivateLayout(true);
185                            portletDataContext.setScopeGroupId(layoutPrototype.getGroupId());
186    
187                            for (Layout layout : layouts) {
188                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
189                                            portletDataContext, layoutPrototype, layout,
190                                            PortletDataContext.REFERENCE_TYPE_EMBEDDED);
191                            }
192                    }
193                    finally {
194                            portletDataContext.setGroupId(groupId);
195                            portletDataContext.setPrivateLayout(privateLayout);
196                            portletDataContext.setScopeGroupId(scopeGroupId);
197                    }
198            }
199    
200            protected void importLayouts(
201                            PortletDataContext portletDataContext,
202                            LayoutPrototype layoutPrototype, long importedGroupId)
203                    throws PortalException {
204    
205                    long groupId = portletDataContext.getGroupId();
206                    boolean privateLayout = portletDataContext.isPrivateLayout();
207                    long scopeGroupId = portletDataContext.getScopeGroupId();
208    
209                    try {
210                            portletDataContext.setGroupId(importedGroupId);
211                            portletDataContext.setPrivateLayout(true);
212                            portletDataContext.setScopeGroupId(importedGroupId);
213    
214                            StagedModelDataHandlerUtil.importReferenceStagedModels(
215                                    portletDataContext, layoutPrototype, Layout.class);
216                    }
217                    finally {
218                            portletDataContext.setGroupId(groupId);
219                            portletDataContext.setPrivateLayout(privateLayout);
220                            portletDataContext.setScopeGroupId(scopeGroupId);
221                    }
222            }
223    
224            @Override
225            protected void importReferenceStagedModels(
226                    PortletDataContext portletDataContext,
227                    LayoutPrototype layoutPrototype) {
228            }
229    
230    }