001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.SystemException;
019    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
020    import com.liferay.portal.kernel.lar.MissingReferences;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.model.BackgroundTask;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.spring.transaction.TransactionalCallableUtil;
027    
028    import java.io.File;
029    import java.io.Serializable;
030    
031    import java.util.Date;
032    import java.util.Map;
033    import java.util.concurrent.Callable;
034    
035    /**
036     * @author Julio Camarero
037     * @author Daniel Kocsis
038     */
039    public class PortletStagingBackgroundTaskExecutor
040            extends BaseStagingBackgroundTaskExecutor {
041    
042            public PortletStagingBackgroundTaskExecutor() {
043                    setBackgroundTaskStatusMessageTranslator(
044                            new PortletStagingBackgroundTaskStatusMessageTranslator());
045            }
046    
047            @Override
048            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
049                    throws Exception {
050    
051                    MissingReferences missingReferences = null;
052    
053                    try {
054                            ExportImportThreadLocal.setPortletStagingInProcess(true);
055    
056                            missingReferences = TransactionalCallableUtil.call(
057                                    transactionAttribute,
058                                    new PortletStagingCallable(backgroundTask));
059                    }
060                    catch (Throwable t) {
061                            if (_log.isDebugEnabled()) {
062                                    _log.debug(t, t);
063                            }
064                            else if (_log.isWarnEnabled()) {
065                                    _log.warn("Unable to publish portlet: " + t.getMessage());
066                            }
067    
068                            throw new SystemException(t);
069                    }
070                    finally {
071                            ExportImportThreadLocal.setPortletStagingInProcess(false);
072                    }
073    
074                    return processMissingReferences(backgroundTask, missingReferences);
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(
078                    PortletStagingBackgroundTaskExecutor.class);
079    
080            private class PortletStagingCallable
081                    implements Callable<MissingReferences> {
082    
083                    public PortletStagingCallable(BackgroundTask backgroundTask) {
084                            _backgroundTask = backgroundTask;
085                    }
086    
087                    @Override
088                    public MissingReferences call() throws Exception {
089                            Map<String, Serializable> taskContextMap =
090                                    _backgroundTask.getTaskContextMap();
091    
092                            long userId = MapUtil.getLong(taskContextMap, "userId");
093                            long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
094                            long targetGroupId = MapUtil.getLong(
095                                    taskContextMap, "targetGroupId");
096                            String portletId = MapUtil.getString(taskContextMap, "portletId");
097                            Map<String, String[]> parameterMap =
098                                    (Map<String, String[]>)taskContextMap.get("parameterMap");
099    
100                            long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
101                            long sourceGroupId = MapUtil.getLong(
102                                    taskContextMap, "sourceGroupId");
103                            Date startDate = (Date)taskContextMap.get("startDate");
104                            Date endDate = (Date)taskContextMap.get("endDate");
105    
106                            File larFile = null;
107                            MissingReferences missingReferences = null;
108    
109                            try {
110                                    larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
111                                            sourcePlid, sourceGroupId, portletId, parameterMap,
112                                            startDate, endDate);
113    
114                                    _backgroundTask = markBackgroundTask(
115                                            _backgroundTask, "exported");
116    
117                                    missingReferences =
118                                            LayoutLocalServiceUtil.validateImportPortletInfo(
119                                                    userId, targetPlid, targetGroupId, portletId,
120                                                    parameterMap, larFile);
121    
122                                    _backgroundTask = markBackgroundTask(
123                                            _backgroundTask, "validated");
124    
125                                    LayoutLocalServiceUtil.importPortletInfo(
126                                            userId, targetPlid, targetGroupId, portletId, parameterMap,
127                                            larFile);
128                            }
129                            finally {
130                                    larFile.delete();
131                            }
132    
133                            return missingReferences;
134                    }
135    
136                    private BackgroundTask _backgroundTask;
137    
138            }
139    
140    }