001/*
002 * Copyright 2016 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.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.User;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.user.UserManager;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034
035/**
036 * Helper component allowing to retrieve information in order to send emails
037 */
038public abstract class AbstractMailInformationHelper extends AbstractLogEnabled implements Component, Serviceable
039{
040    /** The current user provider */
041    protected CurrentUserProvider _currentUserProvider;
042    
043    /** The user manager */
044    protected UserManager _userManager;
045    
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
049        _userManager = (UserManager) serviceManager.lookup(UserManager.ROLE);
050    }
051    
052    /**
053     * Get information on the given entry of the given form
054     * @param formId the id of the form
055     * @param entryId the id of the entry
056     * @return a map of information on the current user and on the email fields filled in the entry
057     */
058    @Callable
059    public Map<String, Object> getMailInfo(String formId, Object entryId)
060    {
061        Map<String, Object> result = new HashMap<> ();
062        
063        // Retrieve the current user name and email
064        UserIdentity userIdentity = _currentUserProvider.getUser();
065        User user = _userManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin());
066        
067        Map<String, Object> currentUser = new HashMap<> ();
068        currentUser.put("email", user.getEmail());
069        currentUser.put("fullname", user.getFullName());
070        
071        Map<String, Object> sendMail = new HashMap<> ();
072        sendMail.put("current-user", currentUser);
073        
074        List<Map<String, Object>> emails = new ArrayList<> ();
075        for (MailInfo mailInfo : _getMailInfos(formId, entryId))
076        {
077            emails.add(mailInfo.toJson());
078        }
079        sendMail.put("emails", emails);
080        result.put("send-mail", sendMail);
081        return result;
082    }
083    
084    /**
085     * Get mail infos
086     * @param formId the form id
087     * @param entryId the entry id
088     * @return the list of mail info
089     */
090    protected abstract List<MailInfo> _getMailInfos(String formId, Object entryId);
091    
092    /**
093     * Class representing mail info
094     */
095    public static class MailInfo
096    {
097        private String _email;
098        private String _displayValue;
099    
100        /**
101         * The constructor
102         * @param email the email
103         * @param displayValue the display value
104         */
105        public MailInfo(String email, String displayValue)
106        {
107            this._email = email;
108            this._displayValue = displayValue;
109        }
110        
111        /**
112         * Get the email
113         * @return the email
114         */
115        public String getEmail()
116        {
117            return this._email;
118        }
119        
120        /**
121         * Get display value
122         * @return the display value
123         */
124        public String getDisplayValue()
125        {
126            return this._displayValue;
127        }
128        
129        /**
130         * The mail info to json
131         * @return the mail info to json
132         */
133        public Map<String, Object> toJson()
134        {
135            Map<String, Object> email = new HashMap<> ();
136            
137            email.put("displayValue", this._displayValue);
138            email.put("value", this._email);
139            
140            return email;
141        }
142    }
143}