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.exportimport.lar;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.spring.orm.LastSessionRecorderHelperUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.model.StagedModel;
026    import com.liferay.portal.service.PortletLocalServiceUtil;
027    
028    import java.util.Collections;
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Mate Thurzo
035     */
036    @ProviderType
037    public class StagedModelDataHandlerUtil {
038    
039            public static void deleteStagedModel(
040                            PortletDataContext portletDataContext, Element deletionElement)
041                    throws PortalException {
042    
043                    String className = deletionElement.attributeValue("class-name");
044                    String extraData = deletionElement.attributeValue("extra-data");
045                    String uuid = deletionElement.attributeValue("uuid");
046    
047                    StagedModelDataHandler<?> stagedModelDataHandler =
048                            StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
049                                    className);
050    
051                    if (stagedModelDataHandler != null) {
052                            stagedModelDataHandler.deleteStagedModel(
053                                    uuid, portletDataContext.getScopeGroupId(), className,
054                                    extraData);
055                    }
056            }
057    
058            public static <T extends StagedModel> Element exportReferenceStagedModel(
059                            PortletDataContext portletDataContext, String referrerPortletId,
060                            T stagedModel)
061                    throws PortletDataException {
062    
063                    Portlet referrerPortlet = PortletLocalServiceUtil.getPortletById(
064                            referrerPortletId);
065    
066                    if (!ExportImportHelperUtil.isReferenceWithinExportScope(
067                                    portletDataContext, stagedModel)) {
068    
069                            return portletDataContext.addReferenceElement(
070                                    referrerPortlet, portletDataContext.getExportDataRootElement(),
071                                    stagedModel, PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
072                                    true);
073                    }
074    
075                    exportStagedModel(portletDataContext, stagedModel);
076    
077                    return portletDataContext.addReferenceElement(
078                            referrerPortlet, portletDataContext.getExportDataRootElement(),
079                            stagedModel, PortletDataContext.REFERENCE_TYPE_DEPENDENCY, false);
080            }
081    
082            /**
083             * @deprecated As of 7.0.0, replaced by {@link
084             *             #exportReferenceStagedModel(PortletDataContext, StagedModel,
085             *             StagedModel, String)}
086             */
087            @Deprecated
088            public static <T extends StagedModel, U extends StagedModel> Element
089                            exportReferenceStagedModel(
090                                    PortletDataContext portletDataContext, T referrerStagedModel,
091                                    Class<?> referrerStagedModelClass, U stagedModel,
092                                    Class<?> stagedModelClass, String referenceType)
093                    throws PortletDataException {
094    
095                    return exportReferenceStagedModel(
096                            portletDataContext, referrerStagedModel, stagedModel,
097                            referenceType);
098            }
099    
100            /**
101             * @deprecated As of 7.0.0, replaced by {@link
102             *             #exportReferenceStagedModel(PortletDataContext, StagedModel,
103             *             StagedModel, String)}
104             */
105            @Deprecated
106            public static <T extends StagedModel, U extends StagedModel> Element
107                            exportReferenceStagedModel(
108                                    PortletDataContext portletDataContext, T referrerStagedModel,
109                                    Element referrerStagedModelElement, U stagedModel,
110                                    Class<?> stagedModelClass, String referenceType)
111                    throws PortletDataException {
112    
113                    return exportReferenceStagedModel(
114                            portletDataContext, referrerStagedModel, stagedModel,
115                            referenceType);
116            }
117    
118            public static <T extends StagedModel, U extends StagedModel> Element
119                            exportReferenceStagedModel(
120                                    PortletDataContext portletDataContext, T referrerStagedModel,
121                                    U stagedModel, String referenceType)
122                    throws PortletDataException {
123    
124                    Element referrerStagedModelElement =
125                            portletDataContext.getExportDataElement(referrerStagedModel);
126    
127                    if (!ExportImportHelperUtil.isReferenceWithinExportScope(
128                                    portletDataContext, stagedModel)) {
129    
130                            return portletDataContext.addReferenceElement(
131                                    referrerStagedModel, referrerStagedModelElement, stagedModel,
132                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
133                    }
134    
135                    exportStagedModel(portletDataContext, stagedModel);
136    
137                    return portletDataContext.addReferenceElement(
138                            referrerStagedModel, referrerStagedModelElement, stagedModel,
139                            referenceType, false);
140            }
141    
142            public static <T extends StagedModel> void exportStagedModel(
143                            PortletDataContext portletDataContext, T stagedModel)
144                    throws PortletDataException {
145    
146                    StagedModelDataHandler<T> stagedModelDataHandler =
147                            _getStagedModelDataHandler(stagedModel);
148    
149                    if (stagedModelDataHandler == null) {
150                            return;
151                    }
152    
153                    stagedModelDataHandler.exportStagedModel(
154                            portletDataContext, stagedModel);
155            }
156    
157            public static <T extends StagedModel> String getDisplayName(T stagedModel) {
158                    StagedModelDataHandler<T> stagedModelDataHandler =
159                            _getStagedModelDataHandler(stagedModel);
160    
161                    if (stagedModelDataHandler == null) {
162                            return StringPool.BLANK;
163                    }
164    
165                    return stagedModelDataHandler.getDisplayName(stagedModel);
166            }
167    
168            public static Map<String, String> getReferenceAttributes(
169                    PortletDataContext portletDataContext, StagedModel stagedModel) {
170    
171                    StagedModelDataHandler<StagedModel> stagedModelDataHandler =
172                            _getStagedModelDataHandler(stagedModel);
173    
174                    if (stagedModelDataHandler == null) {
175                            return Collections.emptyMap();
176                    }
177    
178                    return stagedModelDataHandler.getReferenceAttributes(
179                            portletDataContext, stagedModel);
180            }
181    
182            /**
183             * Imports the staged model that is referenced by a portlet. To import a
184             * staged model referenced by another staged model, use {@link
185             * #importReferenceStagedModel(PortletDataContext, StagedModel, Class,
186             * long)}.
187             *
188             * @param  portletDataContext the portlet data context of the current
189             *         process
190             * @param  stagedModelClass the class of the referenced staged model to be
191             *         imported
192             * @param  classPK the primary key of the referenced staged model to be
193             *         imported
194             * @throws PortletDataException if a portlet data exception occurred
195             */
196            public static void importReferenceStagedModel(
197                            PortletDataContext portletDataContext, Class<?> stagedModelClass,
198                            long classPK)
199                    throws PortletDataException {
200    
201                    importReferenceStagedModel(
202                            portletDataContext, stagedModelClass.getName(), classPK);
203            }
204    
205            /**
206             * Imports the staged model that is referenced by a portlet. To import a
207             * staged model referenced by another staged model, use {@link
208             * #importReferenceStagedModel(PortletDataContext, StagedModel, String,
209             * long)}.
210             *
211             * @param  portletDataContext the portlet data context of the current
212             *         process
213             * @param  stagedModelClassName the class name of the referenced staged
214             *         model to be imported
215             * @param  classPK the primary key of the referenced staged model to be
216             *         imported
217             * @throws PortletDataException if a portlet data exception occurred
218             */
219            public static void importReferenceStagedModel(
220                            PortletDataContext portletDataContext, String stagedModelClassName,
221                            long classPK)
222                    throws PortletDataException {
223    
224                    Element referenceElement = portletDataContext.getReferenceElement(
225                            stagedModelClassName, classPK);
226    
227                    doImportReferenceStagedModel(
228                            portletDataContext, referenceElement, stagedModelClassName);
229            }
230    
231            /**
232             * Imports the staged model that is referenced by another staged model. To
233             * import a staged model referenced by a portlet, use {@link
234             * #importReferenceStagedModel(PortletDataContext, Class, long)}.
235             *
236             * @param  portletDataContext the portlet data context of the current
237             *         process
238             * @param  referrerStagedModel the staged model that references the staged
239             *         model to be imported
240             * @param  stagedModelClass the class of the referenced staged model to be
241             *         imported
242             * @param  classPK the primary key of the referenced staged model to be
243             *         imported
244             * @throws PortletDataException if a portlet data exception occurred
245             */
246            public static <T extends StagedModel> void importReferenceStagedModel(
247                            PortletDataContext portletDataContext, T referrerStagedModel,
248                            Class<?> stagedModelClass, long classPK)
249                    throws PortletDataException {
250    
251                    importReferenceStagedModel(
252                            portletDataContext, referrerStagedModel, stagedModelClass.getName(),
253                            classPK);
254            }
255    
256            /**
257             * Imports the staged model that is referenced by another staged model. To
258             * import a staged model referenced by a portlet, use {@link
259             * #importReferenceStagedModel(PortletDataContext, String, long)}.
260             *
261             * @param  portletDataContext the portlet data context of the current
262             *         process
263             * @param  referrerStagedModel the staged model that references the staged
264             *         model to be imported
265             * @param  stagedModelClassName the class name of the referenced staged
266             *         model to be imported
267             * @param  classPK the primary key of the referenced staged model to be
268             *         imported
269             * @throws PortletDataException if a portlet data exception occurred
270             */
271            public static <T extends StagedModel> void importReferenceStagedModel(
272                            PortletDataContext portletDataContext, T referrerStagedModel,
273                            String stagedModelClassName, long classPK)
274                    throws PortletDataException {
275    
276                    Element referenceElement = portletDataContext.getReferenceElement(
277                            referrerStagedModel, stagedModelClassName, classPK);
278    
279                    doImportReferenceStagedModel(
280                            portletDataContext, referenceElement, stagedModelClassName);
281            }
282    
283            public static void importReferenceStagedModels(
284                            PortletDataContext portletDataContext, Class<?> stagedModelClass)
285                    throws PortletDataException {
286    
287                    Element importDataRootElement =
288                            portletDataContext.getImportDataRootElement();
289    
290                    Element referencesElement = importDataRootElement.element("references");
291    
292                    if (referencesElement == null) {
293                            return;
294                    }
295    
296                    List<Element> referenceElements = referencesElement.elements();
297    
298                    for (Element referenceElement : referenceElements) {
299                            String className = referenceElement.attributeValue("class-name");
300                            String stagedModelClassName = stagedModelClass.getName();
301    
302                            if (!stagedModelClassName.equals(className)) {
303                                    continue;
304                            }
305    
306                            boolean missing = portletDataContext.isMissingReference(
307                                    referenceElement);
308    
309                            StagedModelDataHandler<?> stagedModelDataHandler =
310                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
311                                            stagedModelClassName);
312    
313                            if (stagedModelDataHandler == null) {
314                                    continue;
315                            }
316    
317                            if (missing) {
318                                    stagedModelDataHandler.importMissingReference(
319                                            portletDataContext, referenceElement);
320    
321                                    continue;
322                            }
323    
324                            importStagedModel(portletDataContext, referenceElement);
325                    }
326            }
327    
328            public static <T extends StagedModel> void importReferenceStagedModels(
329                            PortletDataContext portletDataContext, T referrerStagedModel,
330                            Class<?> stagedModelClass)
331                    throws PortletDataException {
332    
333                    List<Element> referenceElements =
334                            portletDataContext.getReferenceElements(
335                                    referrerStagedModel, stagedModelClass);
336    
337                    for (Element referenceElement : referenceElements) {
338                            long classPK = GetterUtil.getLong(
339                                    referenceElement.attributeValue("class-pk"));
340    
341                            importReferenceStagedModel(
342                                    portletDataContext, referrerStagedModel, stagedModelClass,
343                                    classPK);
344                    }
345            }
346    
347            public static void importStagedModel(
348                            PortletDataContext portletDataContext, Element element)
349                    throws PortletDataException {
350    
351                    StagedModel stagedModel = _getStagedModel(portletDataContext, element);
352    
353                    importStagedModel(portletDataContext, stagedModel);
354            }
355    
356            public static <T extends StagedModel> void importStagedModel(
357                            PortletDataContext portletDataContext, T stagedModel)
358                    throws PortletDataException {
359    
360                    StagedModelDataHandler<T> stagedModelDataHandler =
361                            _getStagedModelDataHandler(stagedModel);
362    
363                    if (stagedModelDataHandler == null) {
364                            return;
365                    }
366    
367                    stagedModelDataHandler.importStagedModel(
368                            portletDataContext, stagedModel);
369    
370                    LastSessionRecorderHelperUtil.syncLastSessionState();
371            }
372    
373            protected static void doImportReferenceStagedModel(
374                            PortletDataContext portletDataContext, Element referenceElement,
375                            String stagedModelClassName)
376                    throws PortletDataException {
377    
378                    if (referenceElement == null) {
379                            return;
380                    }
381    
382                    boolean missing = portletDataContext.isMissingReference(
383                            referenceElement);
384    
385                    StagedModelDataHandler<?> stagedModelDataHandler =
386                            StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
387                                    stagedModelClassName);
388    
389                    if (stagedModelDataHandler == null) {
390                            return;
391                    }
392    
393                    if (missing) {
394                            stagedModelDataHandler.importMissingReference(
395                                    portletDataContext, referenceElement);
396    
397                            return;
398                    }
399    
400                    importStagedModel(portletDataContext, referenceElement);
401            }
402    
403            private static StagedModel _getReferenceStagedModel(
404                    PortletDataContext portletDataContext, Element element) {
405    
406                    long groupId = GetterUtil.getLong(element.attributeValue("group-id"));
407                    String className = element.attributeValue("class-name");
408                    long classPK = GetterUtil.getLong(element.attributeValue("class-pk"));
409    
410                    String path = ExportImportPathUtil.getModelPath(
411                            groupId, className, classPK);
412    
413                    StagedModel stagedModel =
414                            (StagedModel)portletDataContext.getZipEntryAsObject(element, path);
415    
416                    if (stagedModel != null) {
417                            return stagedModel;
418                    }
419    
420                    path = ExportImportPathUtil.getCompanyModelPath(
421                            portletDataContext.getSourceCompanyId(), className, classPK);
422    
423                    return (StagedModel)portletDataContext.getZipEntryAsObject(
424                            element, path);
425            }
426    
427            private static StagedModel _getStagedModel(
428                    PortletDataContext portletDataContext, Element element) {
429    
430                    StagedModel stagedModel = null;
431    
432                    String elementName = element.getName();
433    
434                    if (elementName.equals("reference")) {
435                            stagedModel = _getReferenceStagedModel(portletDataContext, element);
436                    }
437                    else {
438                            String path = element.attributeValue("path");
439    
440                            stagedModel = (StagedModel)portletDataContext.getZipEntryAsObject(
441                                    element, path);
442                    }
443    
444                    return stagedModel;
445            }
446    
447            private static <T extends StagedModel> StagedModelDataHandler<T>
448                    _getStagedModelDataHandler(T stagedModel) {
449    
450                    StagedModelDataHandler<T> stagedModelDataHandler =
451                            (StagedModelDataHandler<T>)
452                                    StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
453                                            ExportImportClassedModelUtil.getClassName(stagedModel));
454    
455                    return stagedModelDataHandler;
456            }
457    
458    }