001/*
002 *  Copyright 2018 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.export;
017
018import java.util.ArrayList;
019
020import javax.mail.MessagingException;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.core.schedule.Schedulable;
027import org.ametys.core.util.I18nUtils;
028import org.ametys.core.util.mail.SendMailHelper;
029import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
030import org.ametys.runtime.config.Config;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * A {@link Schedulable} job which delete classified ads after an defined number of days
035 */
036public class ContentExportSchedulable extends AbstractStaticSchedulable
037{
038    /** The export manager */
039    protected ExportManager _exportManager;
040    
041    /** The i18n translator */
042    protected I18nUtils _i18nTranslator;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _exportManager = (ExportManager) manager.lookup(ExportManager.ROLE);
049        _i18nTranslator = (I18nUtils) manager.lookup(I18nUtils.ROLE);
050    }
051    
052    @Override
053    public void execute(JobExecutionContext context) throws Exception
054    {
055        long time_0 = System.currentTimeMillis();
056        boolean error = false;
057        try
058        {
059            _exportManager.export();
060        }
061        catch (Exception ex)
062        {
063            error = true;
064            getLogger().error(_i18nTranslator.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_CONTENT_EXPORT_LOG_ERROR")), ex);
065        }
066        finally
067        {
068            if (!error && getLogger().isInfoEnabled())
069            {
070                ArrayList<String> i18nParams = new ArrayList<>();
071                i18nParams.add(String.valueOf(System.currentTimeMillis() - time_0));
072                getLogger().info(_i18nTranslator.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_CONTENT_EXPORT_LOG_END", i18nParams)));
073            }
074            
075            try
076            {
077                _sendMail(error);
078            }
079            catch (MessagingException e)
080            {
081                if (getLogger().isWarnEnabled())
082                {
083                    getLogger().warn(_i18nTranslator.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_CONTENT_EXPORT_LOG_SEND_MAIL_ERROR")), e);
084                }
085            }
086        }
087    }
088    
089    /**
090     * Send the mail report
091     * @param error true if some error occurred
092     * @throws MessagingException if a messaging error occurred
093     */
094    protected void _sendMail(boolean error) throws MessagingException
095    {
096        String subject = _i18nTranslator.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_CONTENT_EXPORT_REPORT_MAIL_SUBJECT")); 
097        String smtpFrom = Config.getInstance().getValue("smtp.mail.from");
098        String email = Config.getInstance().getValue("org.ametys.plugins.contentio.content.export.admin.mail");
099        String body = _i18nTranslator.translate(new I18nizableText("plugin.contentio", "PLUGINS_CONTENTIO_CONTENT_EXPORT_REPORT_MAIL_BODY" + (error ? "_ERROR" : "")));
100        
101        SendMailHelper.sendMail(subject, null, body, email, smtpFrom);
102    }
103}