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.FileUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.model.BackgroundTask;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.spring.transaction.TransactionalCallableUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.io.File;
031    import java.io.Serializable;
032    
033    import java.util.Date;
034    import java.util.Map;
035    import java.util.concurrent.Callable;
036    
037    /**
038     * @author Julio Camarero
039     * @author Daniel Kocsis
040     */
041    public class PortletStagingBackgroundTaskExecutor
042            extends BaseStagingBackgroundTaskExecutor {
043    
044            public PortletStagingBackgroundTaskExecutor() {
045                    setBackgroundTaskStatusMessageTranslator(
046                            new PortletStagingBackgroundTaskStatusMessageTranslator());
047            }
048    
049            @Override
050            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
051                    throws Exception {
052    
053                    MissingReferences missingReferences = null;
054    
055                    try {
056                            ExportImportThreadLocal.setPortletStagingInProcess(true);
057    
058                            missingReferences = TransactionalCallableUtil.call(
059                                    transactionAttribute,
060                                    new PortletStagingCallable(backgroundTask));
061                    }
062                    catch (Throwable t) {
063                            if (_log.isDebugEnabled()) {
064                                    _log.debug(t, t);
065                            }
066                            else if (_log.isWarnEnabled()) {
067                                    _log.warn("Unable to publish portlet: " + t.getMessage());
068                            }
069    
070                            throw new SystemException(t);
071                    }
072                    finally {
073                            ExportImportThreadLocal.setPortletStagingInProcess(false);
074                    }
075    
076                    return processMissingReferences(backgroundTask, missingReferences);
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(
080                    PortletStagingBackgroundTaskExecutor.class);
081    
082            private class PortletStagingCallable
083                    implements Callable<MissingReferences> {
084    
085                    public PortletStagingCallable(BackgroundTask backgroundTask) {
086                            _backgroundTask = backgroundTask;
087                    }
088    
089                    @Override
090                    public MissingReferences call() throws Exception {
091                            Map<String, Serializable> taskContextMap =
092                                    _backgroundTask.getTaskContextMap();
093    
094                            long userId = MapUtil.getLong(taskContextMap, "userId");
095                            long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
096                            long targetGroupId = MapUtil.getLong(
097                                    taskContextMap, "targetGroupId");
098                            String portletId = MapUtil.getString(taskContextMap, "portletId");
099                            Map<String, String[]> parameterMap =
100                                    (Map<String, String[]>)taskContextMap.get("parameterMap");
101    
102                            long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
103                            long sourceGroupId = MapUtil.getLong(
104                                    taskContextMap, "sourceGroupId");
105                            Date startDate = (Date)taskContextMap.get("startDate");
106                            Date endDate = (Date)taskContextMap.get("endDate");
107    
108                            File larFile = null;
109                            MissingReferences missingReferences = null;
110    
111                            try {
112                                    larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
113                                            sourcePlid, sourceGroupId, portletId, parameterMap,
114                                            startDate, endDate);
115    
116                                    _backgroundTask = markBackgroundTask(
117                                            _backgroundTask, "exported");
118    
119                                    missingReferences =
120                                            LayoutLocalServiceUtil.validateImportPortletInfo(
121                                                    userId, targetPlid, targetGroupId, portletId,
122                                                    parameterMap, larFile);
123    
124                                    _backgroundTask = markBackgroundTask(
125                                            _backgroundTask, "validated");
126    
127                                    LayoutLocalServiceUtil.importPortletInfo(
128                                            userId, targetPlid, targetGroupId, portletId, parameterMap,
129                                            larFile);
130                            }
131                            catch (Exception e) {
132                                    if (PropsValues.STAGING_DELETE_TEMP_LAR_ON_FAILURE) {
133                                            FileUtil.delete(larFile);
134                                    }
135                                    else if ((larFile != null) && _log.isErrorEnabled()) {
136                                            _log.error(
137                                                    "Kept temporary LAR file " + larFile.getAbsolutePath());
138                                    }
139    
140                                    throw e;
141                            }
142    
143                            if (PropsValues.STAGING_DELETE_TEMP_LAR_ON_SUCCESS) {
144                                    FileUtil.delete(larFile);
145                            }
146                            else if ((larFile != null) && _log.isDebugEnabled()) {
147                                    _log.debug(
148                                            "Kept temporary LAR file " + larFile.getAbsolutePath());
149                            }
150    
151                            return missingReferences;
152                    }
153    
154                    private BackgroundTask _backgroundTask;
155    
156            }
157    
158    }