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.user.UserManager;
028import org.ametys.core.util.I18nUtils;
029import org.ametys.core.util.mail.SendMailHelper;
030import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
031import org.ametys.runtime.config.Config;
032
033/**
034 * Common code for {@link ExportArchiveSchedulable} and {@link ImportArchiveSchedulable}
035 */
036abstract class AbstractArchiveSchedulable extends AbstractStaticSchedulable
037{
038    protected I18nUtils _i18nUtils;
039    protected UserManager _userManager;
040    protected ArchiveHandler _archiveHandler;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
047        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
048        _archiveHandler = (ArchiveHandler) manager.lookup(ArchiveHandler.ROLE);
049    }
050    
051    protected final String _getUserEmail()
052    {
053        Request request = ContextHelper.getRequest(_context);
054        UserIdentity userIdentity = AuthenticateAction.getUserIdentityFromSession(request);
055        User user = userIdentity != null ? _userManager.getUser(userIdentity) : null;
056        String userEmail = null;
057        if (user != null)
058        {
059            userEmail = user.getEmail();
060        }
061        
062        return userEmail;
063    }
064    
065    protected final void _sendMail(String subject, String body, String userEmail)
066    {
067        String adminEmail = Config.getInstance().getValue("smtp.mail.sysadminto");
068        String email = StringUtils.isNotEmpty(userEmail) ? userEmail : StringUtils.isNotEmpty(adminEmail) ? adminEmail : null;
069        
070        if (email != null)
071        {
072            try
073            {
074                String smtpFrom = Config.getInstance().getValue("smtp.mail.from");
075                SendMailHelper.sendMail(subject, body, null, email, smtpFrom);
076            }
077            catch (Exception e)
078            {
079                getLogger().warn("Unable to send archiving/import result by email to " + email, e);
080            }
081        }
082        else
083        {
084            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.");
085        }
086    }
087}
088