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.backgroundtask;
016    
017    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_PUBLICATION_PORTLET_LOCAL_FAILED;
018    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_PUBLICATION_PORTLET_LOCAL_STARTED;
019    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_PUBLICATION_PORTLET_LOCAL_SUCCEEDED;
020    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS;
021    
022    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.model.BackgroundTask;
028    import com.liferay.portal.spring.transaction.TransactionHandlerUtil;
029    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
030    import com.liferay.portlet.exportimport.lar.MissingReferences;
031    import com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleManager;
032    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
033    import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
034    
035    import java.io.File;
036    
037    import java.util.concurrent.Callable;
038    
039    /**
040     * @author Julio Camarero
041     * @author Daniel Kocsis
042     * @author Akos Thurzo
043     */
044    public class PortletStagingBackgroundTaskExecutor
045            extends BaseStagingBackgroundTaskExecutor {
046    
047            public PortletStagingBackgroundTaskExecutor() {
048                    setBackgroundTaskStatusMessageTranslator(
049                            new PortletStagingBackgroundTaskStatusMessageTranslator());
050            }
051    
052            @Override
053            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
054                    throws Exception {
055    
056                    ExportImportConfiguration exportImportConfiguration =
057                            getExportImportConfiguration(backgroundTask);
058    
059                    File file = null;
060                    MissingReferences missingReferences = null;
061    
062                    try {
063                            ExportImportThreadLocal.setPortletStagingInProcess(true);
064    
065                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
066                                    EVENT_PUBLICATION_PORTLET_LOCAL_STARTED,
067                                    PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS,
068                                    exportImportConfiguration);
069    
070                            file = ExportImportLocalServiceUtil.exportPortletInfoAsFile(
071                                    exportImportConfiguration);
072    
073                            markBackgroundTask(
074                                    backgroundTask.getBackgroundTaskId(), "exported");
075    
076                            missingReferences = TransactionHandlerUtil.invoke(
077                                    transactionAttribute,
078                                    new PortletStagingCallable(
079                                            backgroundTask.getBackgroundTaskId(),
080                                            exportImportConfiguration, file));
081    
082                            ExportImportThreadLocal.setPortletStagingInProcess(false);
083    
084                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
085                                    EVENT_PUBLICATION_PORTLET_LOCAL_SUCCEEDED,
086                                    PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS,
087                                    exportImportConfiguration);
088                    }
089                    catch (Throwable t) {
090                            ExportImportThreadLocal.setPortletStagingInProcess(false);
091    
092                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
093                                    EVENT_PUBLICATION_PORTLET_LOCAL_FAILED,
094                                    PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS,
095                                    exportImportConfiguration);
096    
097                            if (_log.isDebugEnabled()) {
098                                    _log.debug(t, t);
099                            }
100                            else if (_log.isWarnEnabled()) {
101                                    _log.warn("Unable to publish portlet: " + t.getMessage());
102                            }
103    
104                            deleteTempLarOnFailure(file);
105    
106                            throw new SystemException(t);
107                    }
108    
109                    deleteTempLarOnSuccess(file);
110    
111                    return processMissingReferences(
112                            backgroundTask.getBackgroundTaskId(), missingReferences);
113            }
114    
115            private static final Log _log = LogFactoryUtil.getLog(
116                    PortletStagingBackgroundTaskExecutor.class);
117    
118            private class PortletStagingCallable
119                    implements Callable<MissingReferences> {
120    
121                    public PortletStagingCallable(
122                            long backgroundTaskId,
123                            ExportImportConfiguration exportImportConfiguration, File file) {
124    
125                            _backgroundTaskId = backgroundTaskId;
126                            _exportImportConfiguration = exportImportConfiguration;
127                            _file = file;
128                    }
129    
130                    @Override
131                    public MissingReferences call() throws PortalException {
132                            ExportImportLocalServiceUtil.importPortletDataDeletions(
133                                    _exportImportConfiguration, _file);
134    
135                            MissingReferences missingReferences =
136                                    ExportImportLocalServiceUtil.validateImportPortletInfo(
137                                            _exportImportConfiguration, _file);
138    
139                            markBackgroundTask(_backgroundTaskId, "validated");
140    
141                            ExportImportLocalServiceUtil.importPortletInfo(
142                                    _exportImportConfiguration, _file);
143    
144                            return missingReferences;
145                    }
146    
147                    private final long _backgroundTaskId;
148                    private final ExportImportConfiguration _exportImportConfiguration;
149                    private final File _file;
150    
151            }
152    
153    }