001    /**
002     * Copyright (c) 2000-present 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.portlet.exportimport.backgroundtask;
016    
017    import com.liferay.portal.kernel.backgroundtask.BaseBackgroundTaskExecutor;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.transaction.Propagation;
020    import com.liferay.portal.kernel.util.MapUtil;
021    import com.liferay.portal.model.BackgroundTask;
022    import com.liferay.portal.spring.transaction.TransactionAttributeBuilder;
023    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
024    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
025    import com.liferay.portlet.exportimport.staging.StagingUtil;
026    
027    import java.io.Serializable;
028    
029    import java.util.Map;
030    
031    import org.springframework.transaction.interceptor.TransactionAttribute;
032    
033    /**
034     * @author Akos Thurzo
035     */
036    public abstract class BaseExportImportBackgroundTaskExecutor
037            extends BaseBackgroundTaskExecutor {
038    
039            public BaseExportImportBackgroundTaskExecutor() {
040                    setBackgroundTaskStatusMessageTranslator(
041                            new DefaultExportImportBackgroundTaskStatusMessageTranslator());
042            }
043    
044            @Override
045            public String handleException(BackgroundTask backgroundTask, Exception e) {
046                    ExportImportConfiguration exportImportConfiguration =
047                            getExportImportConfiguration(backgroundTask);
048    
049                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
050                            getLocale(backgroundTask), e,
051                            exportImportConfiguration.getSettingsMap());
052    
053                    return jsonObject.toString();
054            }
055    
056            protected ExportImportConfiguration getExportImportConfiguration(
057                    BackgroundTask backgroundTask) {
058    
059                    Map<String, Serializable> taskContextMap =
060                            backgroundTask.getTaskContextMap();
061    
062                    long exportImportConfigurationId = MapUtil.getLong(
063                            taskContextMap, "exportImportConfigurationId");
064    
065                    return ExportImportConfigurationLocalServiceUtil.
066                            fetchExportImportConfiguration(exportImportConfigurationId);
067            }
068    
069            protected TransactionAttribute transactionAttribute =
070                    TransactionAttributeBuilder.build(
071                            Propagation.REQUIRED, new Class<?>[] {Exception.class});
072    
073    }