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.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    }