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.layoutsetprototypes.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.Conjunction;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StreamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.model.LayoutPrototype;
032    import com.liferay.portal.model.LayoutSetPrototype;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
036    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
037    import com.liferay.portal.service.ServiceContext;
038    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
039    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
040    import com.liferay.portlet.exportimport.lar.PortletDataContext;
041    import com.liferay.portlet.exportimport.lar.PortletDataException;
042    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
043    import com.liferay.portlet.sites.util.SitesUtil;
044    
045    import java.io.File;
046    import java.io.FileInputStream;
047    import java.io.InputStream;
048    
049    import java.util.ArrayList;
050    import java.util.List;
051    
052    /**
053     * @author Daniela Zapata Riesco
054     */
055    @OSGiBeanProperties
056    public class LayoutSetPrototypeStagedModelDataHandler
057            extends BaseStagedModelDataHandler<LayoutSetPrototype> {
058    
059            public static final String[] CLASS_NAMES =
060                    {LayoutSetPrototype.class.getName()};
061    
062            @Override
063            public void deleteStagedModel(LayoutSetPrototype layoutSetPrototype)
064                    throws PortalException {
065    
066                    LayoutSetPrototypeLocalServiceUtil.deleteLayoutSetPrototype(
067                            layoutSetPrototype);
068            }
069    
070            @Override
071            public void deleteStagedModel(
072                            String uuid, long groupId, String className, String extraData)
073                    throws PortalException {
074    
075                    Group group = GroupLocalServiceUtil.getGroup(groupId);
076    
077                    LayoutSetPrototype layoutSetPrototype =
078                            fetchStagedModelByUuidAndGroupId(uuid, group.getCompanyId());
079    
080                    if (layoutSetPrototype != null) {
081                            deleteStagedModel(layoutSetPrototype);
082                    }
083            }
084    
085            @Override
086            public List<LayoutSetPrototype> fetchStagedModelsByUuidAndCompanyId(
087                    String uuid, long companyId) {
088    
089                    List<LayoutSetPrototype> layoutSetPrototypes = new ArrayList<>();
090    
091                    layoutSetPrototypes.add(
092                            LayoutSetPrototypeLocalServiceUtil.
093                                    fetchLayoutSetPrototypeByUuidAndCompanyId(uuid, companyId));
094    
095                    return layoutSetPrototypes;
096            }
097    
098            @Override
099            public String[] getClassNames() {
100                    return CLASS_NAMES;
101            }
102    
103            @Override
104            protected void doExportStagedModel(
105                            PortletDataContext portletDataContext,
106                            LayoutSetPrototype layoutSetPrototype)
107                    throws Exception {
108    
109                    Element layoutSetPrototypeElement =
110                            portletDataContext.getExportDataElement(layoutSetPrototype);
111    
112                    portletDataContext.addClassedModel(
113                            layoutSetPrototypeElement,
114                            ExportImportPathUtil.getModelPath(layoutSetPrototype),
115                            layoutSetPrototype);
116    
117                    exportLayouts(layoutSetPrototype, portletDataContext);
118    
119                    exportLayoutPrototypes(
120                            portletDataContext, layoutSetPrototype, layoutSetPrototypeElement);
121            }
122    
123            @Override
124            protected void doImportStagedModel(
125                            PortletDataContext portletDataContext,
126                            LayoutSetPrototype layoutSetPrototype)
127                    throws Exception {
128    
129                    long userId = portletDataContext.getUserId(
130                            layoutSetPrototype.getUserUuid());
131    
132                    UnicodeProperties settingsProperties =
133                            layoutSetPrototype.getSettingsProperties();
134    
135                    boolean layoutsUpdateable = GetterUtil.getBoolean(
136                            settingsProperties.getProperty("layoutsUpdateable"), true);
137    
138                    ServiceContext serviceContext = portletDataContext.createServiceContext(
139                            layoutSetPrototype);
140    
141                    serviceContext.setAttribute("addDefaultLayout", false);
142    
143                    LayoutSetPrototype importedLayoutSetPrototype = null;
144    
145                    if (portletDataContext.isDataStrategyMirror()) {
146                            LayoutSetPrototype existingLayoutSetPrototype =
147                                    LayoutSetPrototypeLocalServiceUtil.
148                                            fetchLayoutSetPrototypeByUuidAndCompanyId(
149                                                    layoutSetPrototype.getUuid(),
150                                                    portletDataContext.getCompanyId());
151    
152                            if (existingLayoutSetPrototype == null) {
153                                    serviceContext.setUuid(layoutSetPrototype.getUuid());
154    
155                                    importedLayoutSetPrototype =
156                                            LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
157                                                    userId, portletDataContext.getCompanyId(),
158                                                    layoutSetPrototype.getNameMap(),
159                                                    layoutSetPrototype.getDescriptionMap(),
160                                                    layoutSetPrototype.isActive(), layoutsUpdateable,
161                                                    serviceContext);
162                            }
163                            else {
164                                    importedLayoutSetPrototype =
165                                            LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(
166                                                    existingLayoutSetPrototype.getLayoutSetPrototypeId(),
167                                                    layoutSetPrototype.getNameMap(),
168                                                    layoutSetPrototype.getDescriptionMap(),
169                                                    layoutSetPrototype.isActive(), layoutsUpdateable,
170                                                    serviceContext);
171                            }
172                    }
173                    else {
174                            importedLayoutSetPrototype =
175                                    LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
176                                            userId, portletDataContext.getCompanyId(),
177                                            layoutSetPrototype.getNameMap(),
178                                            layoutSetPrototype.getDescriptionMap(),
179                                            layoutSetPrototype.isActive(), layoutsUpdateable,
180                                            serviceContext);
181                    }
182    
183                    importLayoutPrototypes(portletDataContext, layoutSetPrototype);
184                    importLayouts(
185                            portletDataContext, layoutSetPrototype, importedLayoutSetPrototype,
186                            serviceContext);
187    
188                    portletDataContext.importClassedModel(
189                            layoutSetPrototype, importedLayoutSetPrototype);
190            }
191    
192            protected void exportLayoutPrototypes(
193                            PortletDataContext portletDataContext,
194                            LayoutSetPrototype layoutSetPrototype,
195                            Element layoutSetPrototypeElement)
196                    throws Exception {
197    
198                    DynamicQuery dynamicQuery = LayoutLocalServiceUtil.dynamicQuery();
199    
200                    Property groupIdProperty = PropertyFactoryUtil.forName("groupId");
201    
202                    dynamicQuery.add(groupIdProperty.eq(layoutSetPrototype.getGroupId()));
203    
204                    Conjunction conjunction = RestrictionsFactoryUtil.conjunction();
205    
206                    Property layoutPrototypeUuidProperty = PropertyFactoryUtil.forName(
207                            "layoutPrototypeUuid");
208    
209                    conjunction.add(layoutPrototypeUuidProperty.isNotNull());
210                    conjunction.add(layoutPrototypeUuidProperty.ne(StringPool.BLANK));
211    
212                    dynamicQuery.add(conjunction);
213    
214                    List<Layout> layouts = LayoutLocalServiceUtil.dynamicQuery(
215                            dynamicQuery);
216    
217                    boolean exportLayoutPrototypes = portletDataContext.getBooleanParameter(
218                            "layout_set_prototypes", "page-templates");
219    
220                    for (Layout layout : layouts) {
221                            String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();
222    
223                            LayoutPrototype layoutPrototype =
224                                    LayoutPrototypeLocalServiceUtil.
225                                            getLayoutPrototypeByUuidAndCompanyId(
226                                                    layoutPrototypeUuid, portletDataContext.getCompanyId());
227    
228                            portletDataContext.addReferenceElement(
229                                    layout, layoutSetPrototypeElement, layoutPrototype,
230                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
231                                    !exportLayoutPrototypes);
232    
233                            if (exportLayoutPrototypes) {
234                                    StagedModelDataHandlerUtil.exportStagedModel(
235                                            portletDataContext, layoutPrototype);
236                            }
237                    }
238            }
239    
240            protected void exportLayouts(
241                            LayoutSetPrototype layoutSetPrototype,
242                            PortletDataContext portletDataContext)
243                    throws Exception {
244    
245                    File file = null;
246                    InputStream inputStream = null;
247    
248                    try {
249                            file = SitesUtil.exportLayoutSetPrototype(
250                                    layoutSetPrototype, new ServiceContext());
251    
252                            inputStream = new FileInputStream(file);
253    
254                            String layoutSetPrototypeLARPath =
255                                    ExportImportPathUtil.getModelPath(
256                                            layoutSetPrototype,
257                                            getLayoutSetPrototypeLARFileName(layoutSetPrototype));
258    
259                            portletDataContext.addZipEntry(
260                                    layoutSetPrototypeLARPath, inputStream);
261    
262                            List<Layout> layoutSetPrototypeLayouts =
263                                    LayoutLocalServiceUtil.getLayouts(
264                                            layoutSetPrototype.getGroupId(), true);
265    
266                            Element layoutSetPrototypeElement =
267                                    portletDataContext.getExportDataElement(layoutSetPrototype);
268    
269                            for (Layout layoutSetPrototypeLayout : layoutSetPrototypeLayouts) {
270                                    portletDataContext.addReferenceElement(
271                                            layoutSetPrototype, layoutSetPrototypeElement,
272                                            layoutSetPrototypeLayout,
273                                            PortletDataContext.REFERENCE_TYPE_EMBEDDED, false);
274                            }
275                    }
276                    finally {
277                            StreamUtil.cleanUp(inputStream);
278    
279                            if (file != null) {
280                                    file.delete();
281                            }
282                    }
283            }
284    
285            protected String getLayoutSetPrototypeLARFileName(
286                    LayoutSetPrototype layoutSetPrototype) {
287    
288                    return layoutSetPrototype.getLayoutSetPrototypeId() + ".lar";
289            }
290    
291            protected void importLayoutPrototypes(
292                            PortletDataContext portletDataContext,
293                            LayoutSetPrototype layoutSetPrototype)
294                    throws PortletDataException {
295    
296                    List<Element> layoutPrototypeElements =
297                            portletDataContext.getReferenceDataElements(
298                                    layoutSetPrototype, LayoutPrototype.class);
299    
300                    for (Element layoutPrototypeElement : layoutPrototypeElements) {
301                            StagedModelDataHandlerUtil.importStagedModel(
302                                    portletDataContext, layoutPrototypeElement);
303                    }
304            }
305    
306            protected void importLayouts(
307                            PortletDataContext portletDataContext,
308                            LayoutSetPrototype layoutSetPrototype,
309                            LayoutSetPrototype importedLayoutSetPrototype,
310                            ServiceContext serviceContext)
311                    throws PortalException {
312    
313                    InputStream inputStream = null;
314    
315                    try {
316                            String layoutSetPrototypeLARPath =
317                                    ExportImportPathUtil.getModelPath(
318                                            layoutSetPrototype,
319                                            getLayoutSetPrototypeLARFileName(layoutSetPrototype));
320    
321                            inputStream = portletDataContext.getZipEntryAsInputStream(
322                                    layoutSetPrototypeLARPath);
323    
324                            SitesUtil.importLayoutSetPrototype(
325                                    importedLayoutSetPrototype, inputStream, serviceContext);
326                    }
327                    finally {
328                            StreamUtil.cleanUp(inputStream);
329                    }
330            }
331    
332            @Override
333            protected void importReferenceStagedModels(
334                    PortletDataContext portletDataContext,
335                    LayoutSetPrototype layoutSetPrototype) {
336            }
337    
338    }