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