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.BackgroundTaskConstants;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskResult;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.model.BackgroundTask;
026    import com.liferay.portal.spring.transaction.TransactionHandlerUtil;
027    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
028    import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
029    
030    import java.io.File;
031    
032    import java.util.List;
033    import java.util.concurrent.Callable;
034    
035    /**
036     * @author Daniel Kocsis
037     * @author Akos Thurzo
038     */
039    public class PortletImportBackgroundTaskExecutor
040            extends BaseExportImportBackgroundTaskExecutor {
041    
042            public PortletImportBackgroundTaskExecutor() {
043                    setBackgroundTaskStatusMessageTranslator(
044                            new PortletExportImportBackgroundTaskStatusMessageTranslator());
045    
046                    // Isolation level guarantees this will be serial in a group
047    
048                    setIsolationLevel(BackgroundTaskConstants.ISOLATION_LEVEL_GROUP);
049            }
050    
051            @Override
052            public BackgroundTaskResult execute(BackgroundTask backgroundTask)
053                    throws Exception {
054    
055                    ExportImportConfiguration exportImportConfiguration =
056                            getExportImportConfiguration(backgroundTask);
057    
058                    List<FileEntry> attachmentsFileEntries =
059                            backgroundTask.getAttachmentsFileEntries();
060    
061                    File file = null;
062    
063                    for (FileEntry attachmentsFileEntry : attachmentsFileEntries) {
064                            try {
065                                    file = FileUtil.createTempFile("lar");
066    
067                                    FileUtil.write(file, attachmentsFileEntry.getContentStream());
068    
069                                    TransactionHandlerUtil.invoke(
070                                            transactionAttribute,
071                                            new PortletImportCallable(exportImportConfiguration, file));
072                            }
073                            catch (Throwable t) {
074                                    if (_log.isDebugEnabled()) {
075                                            _log.debug(t, t);
076                                    }
077                                    else if (_log.isWarnEnabled()) {
078                                            _log.warn("Unable to import portlet: " + t.getMessage());
079                                    }
080    
081                                    throw new SystemException(t);
082                            }
083                            finally {
084                                    FileUtil.delete(file);
085                            }
086                    }
087    
088                    return BackgroundTaskResult.SUCCESS;
089            }
090    
091            private static final Log _log = LogFactoryUtil.getLog(
092                    PortletImportBackgroundTaskExecutor.class);
093    
094            private class PortletImportCallable implements Callable<Void> {
095    
096                    public PortletImportCallable(
097                            ExportImportConfiguration exportImportConfiguration, File file) {
098    
099                            _exportImportConfiguration = exportImportConfiguration;
100                            _file = file;
101                    }
102    
103                    @Override
104                    public Void call() throws PortalException {
105                            ExportImportLocalServiceUtil.importPortletDataDeletions(
106                                    _exportImportConfiguration, _file);
107    
108                            ExportImportLocalServiceUtil.importPortletInfo(
109                                    _exportImportConfiguration, _file);
110    
111                            return null;
112                    }
113    
114                    private final ExportImportConfiguration _exportImportConfiguration;
115                    private final File _file;
116    
117            }
118    
119    }