001    /**
002     * Copyright (c) 2000-2013 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.lar.MissingReferences;
019    import com.liferay.portal.kernel.util.MapUtil;
020    import com.liferay.portal.model.BackgroundTask;
021    import com.liferay.portal.service.LayoutLocalServiceUtil;
022    
023    import java.io.File;
024    import java.io.Serializable;
025    
026    import java.util.Date;
027    import java.util.Map;
028    
029    /**
030     * @author Julio Camarero
031     */
032    public class PortletStagingBackgroundTaskExecutor
033            extends BaseStagingBackgroundTaskExecutor {
034    
035            public PortletStagingBackgroundTaskExecutor() {
036                    setBackgroundTaskStatusMessageTranslator(
037                            new PortletStagingBackgroundTaskStatusMessageTranslator());
038            }
039    
040            @Override
041            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
042                    throws Exception {
043    
044                    Map<String, Serializable> taskContextMap =
045                            backgroundTask.getTaskContextMap();
046    
047                    long userId = MapUtil.getLong(taskContextMap, "userId");
048                    long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
049                    long targetGroupId = MapUtil.getLong(taskContextMap, "targetGroupId");
050                    String portletId = MapUtil.getString(taskContextMap, "portletId");
051                    Map<String, String[]> parameterMap =
052                            (Map<String, String[]>)taskContextMap.get("parameterMap");
053    
054                    long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
055                    long sourceGroupId = MapUtil.getLong(taskContextMap, "sourceGroupId");
056                    Date startDate = (Date)taskContextMap.get("startDate");
057                    Date endDate = (Date)taskContextMap.get("endDate");
058    
059                    File larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
060                            sourcePlid, sourceGroupId, portletId, parameterMap, startDate,
061                            endDate);
062    
063                    backgroundTask = markBackgroundTask(backgroundTask, "exported");
064    
065                    MissingReferences missingReferences = null;
066    
067                    try {
068                            missingReferences =
069                                    LayoutLocalServiceUtil.validateImportPortletInfo(
070                                            userId, targetPlid, targetGroupId, portletId, parameterMap,
071                                            larFile);
072    
073                            backgroundTask = markBackgroundTask(backgroundTask, "validated");
074    
075                            LayoutLocalServiceUtil.importPortletInfo(
076                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
077                                    larFile);
078                    }
079                    finally {
080                            larFile.delete();
081                    }
082    
083                    return processMissingReferences(backgroundTask, missingReferences);
084            }
085    
086    }