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            @Override
036            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
037                    throws Exception {
038    
039                    Map<String, Serializable> taskContextMap =
040                            backgroundTask.getTaskContextMap();
041    
042                    long userId = MapUtil.getLong(taskContextMap, "userId");
043                    long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
044                    long targetGroupId = MapUtil.getLong(taskContextMap, "targetGroupId");
045                    String portletId = MapUtil.getString(taskContextMap, "portletId");
046                    Map<String, String[]> parameterMap =
047                            (Map<String, String[]>)taskContextMap.get("parameterMap");
048    
049                    long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
050                    long sourceGroupId = MapUtil.getLong(taskContextMap, "sourceGroupId");
051                    Date startDate = (Date)taskContextMap.get("startDate");
052                    Date endDate = (Date)taskContextMap.get("endDate");
053    
054                    File larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
055                            sourcePlid, sourceGroupId, portletId, parameterMap, startDate,
056                            endDate);
057    
058                    backgroundTask = markBackgroundTask(backgroundTask, "exported");
059    
060                    MissingReferences missingReferences = null;
061    
062                    try {
063                            missingReferences =
064                                    LayoutLocalServiceUtil.validateImportPortletInfo(
065                                            userId, targetPlid, targetGroupId, portletId, parameterMap,
066                                            larFile);
067    
068                            backgroundTask = markBackgroundTask(backgroundTask, "validated");
069    
070                            LayoutLocalServiceUtil.importPortletInfo(
071                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
072                                    larFile);
073                    }
074                    finally {
075                            larFile.delete();
076                    }
077    
078                    return processMissingReferences(backgroundTask, missingReferences);
079            }
080    
081    }