001/*
002 *  Copyright 2020 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 org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.cocoon.components.ContextHelper;
021import org.apache.cocoon.environment.Request;
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.core.authentication.AuthenticateAction;
025import org.ametys.core.user.User;
026import org.ametys.core.user.UserIdentity;
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;
031
032/**
033 * Common code for {@link ExportArchiveSchedulable} and {@link ImportArchiveSchedulable}
034 */
035abstract class AbstractArchiveSchedulable extends AbstractStaticSchedulable
036{
037    protected I18nUtils _i18nUtils;
038    protected ArchiveHandler _archiveHandler;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
045        _archiveHandler = (ArchiveHandler) manager.lookup(ArchiveHandler.ROLE);
046    }
047    
048    protected final String _getUserEmail()
049    {
050        Request request = ContextHelper.getRequest(_context);
051        UserIdentity userIdentity = AuthenticateAction.getUserIdentityFromSession(request);
052        User user = userIdentity != null ? _userManager.getUser(userIdentity) : null;
053        String userEmail = null;
054        if (user != null)
055        {
056            userEmail = user.getEmail();
057        }
058        
059        return userEmail;
060    }
061    
062    protected final void _sendMail(String subject, String body, String userEmail)
063    {
064        String adminEmail = Config.getInstance().getValue("smtp.mail.sysadminto");
065        String email = StringUtils.isNotEmpty(userEmail) ? userEmail : StringUtils.isNotEmpty(adminEmail) ? adminEmail : null;
066        
067        if (email != null)
068        {
069            try
070            {
071                String smtpFrom = Config.getInstance().getValue("smtp.mail.from");
072                SendMailHelper.sendMail(subject, body, null, email, smtpFrom);
073            }
074            catch (Exception e)
075            {
076                getLogger().warn("Unable to send archiving/import result by email to " + email, e);
077            }
078        }
079        else
080        {
081            getLogger().warn("An email should have been sent after archiving/import process but the current user has no email and the admin email is not set in the configuration.");
082        }
083    }
084}
085