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