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.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 LayoutImportBackgroundTaskExecutor
035            extends BaseBackgroundTaskExecutor {
036    
037            public LayoutImportBackgroundTaskExecutor() {
038                    setBackgroundTaskStatusMessageTranslator(
039                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
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 groupId = MapUtil.getLong(taskContextMap, "groupId");
052                    boolean privateLayout = MapUtil.getBoolean(
053                            taskContextMap, "privateLayout");
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.importLayouts(
062                                    userId, groupId, privateLayout, 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    }