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