001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.contentio.archive;
017
018import java.io.File;
019import java.nio.file.Path;
020import java.time.ZonedDateTime;
021import java.time.format.DateTimeFormatter;
022
023import org.apache.commons.io.FileUtils;
024import org.apache.commons.lang3.StringUtils;
025import org.quartz.JobExecutionContext;
026
027import org.ametys.core.schedule.progression.ContainerProgressionTracker;
028import org.ametys.core.user.User;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Job for archiving data on disk, for backup or other purposes.
033 */
034public class ExportArchiveSchedulable extends AbstractArchiveSchedulable
035{
036    @Override
037    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
038    {
039        User user = _getUser();
040        
041        String userEmail = null;
042        String language = _userLanguagesManager.getDefaultLanguage();
043        if (user != null)
044        {
045            userEmail = user.getEmail();
046            
047            String userLanguage = user.getLanguage();
048            language = StringUtils.defaultIfBlank(userLanguage, language);
049        }
050        
051        File output = _createFileOutput();
052        
053        getLogger().info("Exporting archive {} ...", output.getAbsolutePath());
054        long t0 = System.currentTimeMillis();
055        
056        try
057        {
058            _archiveHandler.export(output);
059            
060            getLogger().info("Archive {} exported without error in {} ms", output.getAbsolutePath(), System.currentTimeMillis() - t0);
061            
062            // Once finished with archive, inform user
063            String subject = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAIL_SUBJECT"), language);
064            String body = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAIL_BODY"), language);
065            
066            _sendMail(subject, body + " " + output.getCanonicalPath(), userEmail);
067        }
068        catch (Exception e)
069        {
070            FileUtils.deleteQuietly(output);
071            
072            // Archiving encountered an error during execution, send error mail
073            String subject = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAILERROR_SUBJECT"), language);
074            String body = _i18nUtils.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_ARCHIVE_MAILERROR_BODY"), language);
075            
076            _sendMail(subject, body, userEmail);
077            
078            // rethrow to ensure the job is marked as failed
079            throw e;
080        }
081    }
082    
083    private File _createFileOutput()
084    {
085        String timestamp = DateTimeFormatter.ofPattern("uuuuMMddHHmmss").format(ZonedDateTime.now());
086        Path parent = _archiveHandler.getArchiveFolder();
087        File output = parent.resolve("archive-" + timestamp + ".zip").toFile();
088        return output;
089    }
090}