001/*
002 * Copyright 2022 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.forms.workflow;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.core.user.User;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.plugins.forms.helper.FormMailHelper;
029import org.ametys.plugins.forms.repository.FormEntry;
030import org.ametys.plugins.forms.repository.FormQuestion;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032
033/**
034 * Helper component allowing to retrieve information in order to send emails
035 */
036public class MailInformationHelper extends AbstractMailInformationHelper
037{
038    /** The Avalon role */
039    public static final String ROLE = MailInformationHelper.class.getName();
040
041    /** The Ametys object resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    /** The form mail helper */
045    protected FormMailHelper _formMailHelper;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
052        _formMailHelper = (FormMailHelper) serviceManager.lookup(FormMailHelper.ROLE);
053    }
054    
055    @Override
056    protected List<MailInfo> _getMailInfos(String formId, Object entryId)
057    {
058        List<MailInfo> emails = new ArrayList<> ();
059        
060        FormEntry formEntry = _resolver.resolveById((String) entryId);
061        UserIdentity userId = formEntry.getUser();
062        if (userId != null)
063        {
064            User user = _userManager.getUser(userId);
065            String email = user.getEmail();
066            if (StringUtils.isNotBlank(email))
067            {
068                MailInfo mailInfo = new MailInfo(
069                    email,
070                    user.getFullName() + " <" + email + ">"
071                );
072                
073                emails.add(mailInfo);
074            }
075        }
076        
077        for (FormQuestion question : _formMailHelper.getQuestionWithMail(formId))
078        {
079            Optional<String> receiver = _formMailHelper.getReceiver(formEntry, Optional.of(question.getNameForForm()));
080            if (receiver.isPresent())
081            {
082                MailInfo mailInfo = new MailInfo(
083                    receiver.get(),
084                    question.getTitle() + " <" + receiver.get() + ">"
085                );
086                emails.add(mailInfo);
087            }
088        }
089        
090        return emails;
091    }
092}