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.backgroundtask.BaseBackgroundTaskExecutor;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.staging.StagingUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.model.BackgroundTask;
023    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
024    import com.liferay.portal.service.LayoutLocalServiceUtil;
025    
026    import java.io.File;
027    import java.io.Serializable;
028    
029    import java.util.Date;
030    import java.util.Map;
031    
032    /**
033     * @author Daniel Kocsis
034     * @author Michael C. Han
035     */
036    public class PortletExportBackgroundTaskExecutor
037            extends BaseBackgroundTaskExecutor {
038    
039            public PortletExportBackgroundTaskExecutor() {
040                    setBackgroundTaskStatusMessageTranslator(
041                            new PortletExportImportBackgroundTaskStatusMessageTranslator());
042                    setSerial(true);
043            }
044    
045            @Override
046            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
047                    throws Exception {
048    
049                    Map<String, Serializable> taskContextMap =
050                            backgroundTask.getTaskContextMap();
051    
052                    long userId = MapUtil.getLong(taskContextMap, "userId");
053                    String fileName = MapUtil.getString(taskContextMap, "fileName");
054    
055                    long plid = MapUtil.getLong(taskContextMap, "plid");
056                    long groupId = MapUtil.getLong(taskContextMap, "groupId");
057                    String portletId = MapUtil.getString(taskContextMap, "portletId");
058                    Map<String, String[]> parameterMap =
059                            (Map<String, String[]>)taskContextMap.get("parameterMap");
060                    Date startDate = (Date)taskContextMap.get("startDate");
061                    Date endDate = (Date)taskContextMap.get("endDate");
062    
063                    File larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
064                            plid, groupId, portletId, parameterMap, startDate, endDate);
065    
066                    BackgroundTaskLocalServiceUtil.addBackgroundTaskAttachment(
067                            userId, backgroundTask.getBackgroundTaskId(), fileName, larFile);
068    
069                    return BackgroundTaskResult.SUCCESS;
070            }
071    
072            @Override
073            public String handleException(BackgroundTask backgroundTask, Exception e) {
074                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
075                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
076    
077                    return jsonObject.toString();
078            }
079    
080    }