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.BackgroundTaskConstants;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
019    import com.liferay.portal.kernel.json.JSONArray;
020    import com.liferay.portal.kernel.lar.MissingReference;
021    import com.liferay.portal.kernel.lar.MissingReferences;
022    import com.liferay.portal.kernel.staging.StagingUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.model.BackgroundTask;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    
027    import java.io.File;
028    import java.io.Serializable;
029    
030    import java.util.Date;
031    import java.util.Map;
032    
033    /**
034     * @author Julio Camarero
035     */
036    public class PortletStagingBackgroundTaskExecutor
037            extends BaseStagingBackgroundTaskExecutor {
038    
039            @Override
040            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
041                    throws Exception {
042    
043                    Map<String, Serializable> taskContextMap =
044                            backgroundTask.getTaskContextMap();
045    
046                    long userId = MapUtil.getLong(taskContextMap, "userId");
047                    long targetPlid = MapUtil.getLong(taskContextMap, "targetPlid");
048                    long targetGroupId = MapUtil.getLong(taskContextMap, "targetGroupId");
049                    String portletId = MapUtil.getString(taskContextMap, "portletId");
050                    Map<String, String[]> parameterMap =
051                            (Map<String, String[]>)taskContextMap.get("parameterMap");
052    
053                    long sourcePlid = MapUtil.getLong(taskContextMap, "sourcePlid");
054                    long sourceGroupId = MapUtil.getLong(taskContextMap, "sourceGroupId");
055                    Date startDate = (Date)taskContextMap.get("startDate");
056                    Date endDate = (Date)taskContextMap.get("endDate");
057    
058                    File larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
059                            sourcePlid, sourceGroupId, portletId, parameterMap, startDate,
060                            endDate);
061    
062                    MissingReferences missingReferences = null;
063    
064                    try {
065                            missingReferences =
066                                    LayoutLocalServiceUtil.validateImportPortletInfo(
067                                            userId, targetGroupId, targetPlid, portletId, parameterMap,
068                                            larFile);
069    
070                            backgroundTask = markValidatedBackgroundTask(backgroundTask);
071    
072                            LayoutLocalServiceUtil.importPortletInfo(
073                                    userId, targetPlid, targetGroupId, portletId, parameterMap,
074                                    larFile);
075                    }
076                    finally {
077                            larFile.delete();
078                    }
079    
080                    BackgroundTaskResult backgroundTaskResult = new BackgroundTaskResult(
081                            BackgroundTaskConstants.STATUS_SUCCESSFUL);
082    
083                    Map<String, MissingReference> weakMissingReferences =
084                            missingReferences.getWeakMissingReferences();
085    
086                    if ((weakMissingReferences != null) &&
087                            !weakMissingReferences.isEmpty()) {
088    
089                            JSONArray jsonArray = StagingUtil.getWarningMessagesJSONArray(
090                                    getLocale(backgroundTask), weakMissingReferences,
091                                    backgroundTask.getTaskContextMap());
092    
093                            backgroundTaskResult.setStatusMessage(jsonArray.toString());
094                    }
095    
096                    return backgroundTaskResult;
097            }
098    
099    }