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.portal.lar.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
021    import com.liferay.portal.kernel.lar.MissingReferences;
022    import com.liferay.portal.kernel.lar.lifecycle.ExportImportLifecycleConstants;
023    import com.liferay.portal.kernel.lar.lifecycle.ExportImportLifecycleManager;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.model.BackgroundTask;
028    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.spring.transaction.TransactionalCallableUtil;
031    
032    import java.io.File;
033    import java.io.Serializable;
034    
035    import java.util.Date;
036    import java.util.HashMap;
037    import java.util.Map;
038    import java.util.concurrent.Callable;
039    
040    /**
041     * @author Julio Camarero
042     * @author Daniel Kocsis
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                    MissingReferences missingReferences = null;
057    
058                    HashMap<String, Serializable> serializableTaskContextMap =
059                            new HashMap<String, Serializable>(
060                                    backgroundTask.getTaskContextMap());
061    
062                    try {
063                            ExportImportThreadLocal.setPortletStagingInProcess(true);
064    
065                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
066                                    ExportImportLifecycleConstants.
067                                            EVENT_PUBLICATION_PORTLET_LOCAL_STARTED,
068                                    serializableTaskContextMap);
069    
070                            missingReferences = TransactionalCallableUtil.call(
071                                    transactionAttribute,
072                                    new PortletStagingCallable(
073                                            backgroundTask.getBackgroundTaskId()));
074    
075                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
076                                    ExportImportLifecycleConstants.
077                                            EVENT_PUBLICATION_PORTLET_LOCAL_SUCCEEDED,
078                                    serializableTaskContextMap);
079                    }
080                    catch (Throwable t) {
081                            ExportImportLifecycleManager.fireExportImportLifecycleEvent(
082                                    ExportImportLifecycleConstants.
083                                            EVENT_PUBLICATION_PORTLET_LOCAL_FAILED,
084                                    serializableTaskContextMap);
085    
086                            if (_log.isDebugEnabled()) {
087                                    _log.debug(t, t);
088                            }
089                            else if (_log.isWarnEnabled()) {
090                                    _log.warn("Unable to publish portlet: " + t.getMessage());
091                            }
092    
093                            throw new SystemException(t);
094                    }
095                    finally {
096                            ExportImportThreadLocal.setPortletStagingInProcess(false);
097                    }
098    
099                    return processMissingReferences(
100                            backgroundTask.getBackgroundTaskId(), missingReferences);
101            }
102    
103            private static final Log _log = LogFactoryUtil.getLog(
104                    PortletStagingBackgroundTaskExecutor.class);
105    
106            private class PortletStagingCallable
107                    implements Callable<MissingReferences> {
108    
109                    public PortletStagingCallable(long backgroundTaskId) {
110                            _backgroundTaskId = backgroundTaskId;
111                    }
112    
113                    @Override
114                    public MissingReferences call() throws PortalException {
115                            BackgroundTask backgroundTask =
116                                    BackgroundTaskLocalServiceUtil.getBackgroundTask(
117                                            _backgroundTaskId);
118    
119                            Map<String, Serializable> taskContextMap =
120                                    backgroundTask.getTaskContextMap();
121    
122                            long userId = MapUtil.getLong(taskContextMap, "userId");
123                            long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
124                            long targetGroupId = MapUtil.getLong(
125                                    taskContextMap, "targetGroupId");
126                            String portletId = MapUtil.getString(taskContextMap, "portletId");
127                            Map<String, String[]> parameterMap =
128                                    (Map<String, String[]>)taskContextMap.get("parameterMap");
129    
130                            long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
131                            long sourceGroupId = MapUtil.getLong(
132                                    taskContextMap, "sourceGroupId");
133                            Date startDate = (Date)taskContextMap.get("startDate");
134                            Date endDate = (Date)taskContextMap.get("endDate");
135    
136                            File larFile = null;
137                            MissingReferences missingReferences = null;
138    
139                            try {
140                                    larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
141                                            sourcePlid, sourceGroupId, portletId, parameterMap,
142                                            startDate, endDate);
143    
144                                    markBackgroundTask(_backgroundTaskId, "exported");
145    
146                                    missingReferences =
147                                            LayoutLocalServiceUtil.validateImportPortletInfo(
148                                                    userId, targetPlid, targetGroupId, portletId,
149                                                    parameterMap, larFile);
150    
151                                    markBackgroundTask(_backgroundTaskId, "validated");
152    
153                                    LayoutLocalServiceUtil.importPortletInfo(
154                                            userId, targetPlid, targetGroupId, portletId, parameterMap,
155                                            larFile);
156                            }
157                            finally {
158                                    larFile.delete();
159                            }
160    
161                            return missingReferences;
162                    }
163    
164                    private final long _backgroundTaskId;
165    
166            }
167    
168    }