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.repository.model.FileEntry;
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.Serializable;
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Daniel Kocsis
034     */
035    public class PortletImportBackgroundTaskExecutor
036            extends BaseBackgroundTaskExecutor {
037    
038            public PortletImportBackgroundTaskExecutor() {
039                    setBackgroundTaskStatusMessageTranslator(
040                            new PortletExportImportBackgroundTaskStatusMessageTranslator());
041                    setSerial(true);
042            }
043    
044            @Override
045            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
046                    throws Exception {
047    
048                    Map<String, Serializable> taskContextMap =
049                            backgroundTask.getTaskContextMap();
050    
051                    long userId = MapUtil.getLong(taskContextMap, "userId");
052                    long plid = MapUtil.getLong(taskContextMap, "plid");
053                    long groupId = MapUtil.getLong(taskContextMap, "groupId");
054                    String portletId = MapUtil.getString(taskContextMap, "portletId");
055                    Map<String, String[]> parameterMap =
056                            (Map<String, String[]>)taskContextMap.get("parameterMap");
057    
058                    List<FileEntry> attachmentsFileEntries =
059                            backgroundTask.getAttachmentsFileEntries();
060    
061                    for (FileEntry attachmentsFileEntry : attachmentsFileEntries) {
062                            LayoutLocalServiceUtil.importPortletInfo(
063                                    userId, plid, groupId, portletId, parameterMap,
064                                    attachmentsFileEntry.getContentStream());
065                    }
066    
067                    return BackgroundTaskResult.SUCCESS;
068            }
069    
070            @Override
071            public String handleException(BackgroundTask backgroundTask, Exception e)
072                    throws SystemException {
073    
074                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
075                            getLocale(backgroundTask), e, backgroundTask.getTaskContextMap());
076    
077                    return jsonObject.toString();
078            }
079    
080    }