001/*
002 *  Copyright 2010 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.web.workflow;
017
018import java.util.HashSet;
019import java.util.Iterator;
020import java.util.Set;
021
022import org.ametys.cms.repository.WorkflowAwareContent;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
025import org.ametys.runtime.i18n.I18nizableText;
026
027import com.opensymphony.workflow.WorkflowException;
028import com.opensymphony.workflow.spi.Step;
029
030/**
031 * OS workflow function to send mail after an action is triggered.
032 */
033public class SendMailToUserFunction extends SendMailFunction
034{
035    @Override
036    protected Set<UserIdentity> _getUsers(WorkflowAwareContent content, Set<String> rights) throws WorkflowException
037    {
038        Set<UserIdentity> users = new HashSet<>();
039        
040        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(content);
041        long workflowId = content.getWorkflowId();
042        
043        Iterator<Step> steps = workflow.getHistorySteps(workflowId).iterator();
044        
045        // Browse the step list to find the newest proposition action.
046        if (steps.hasNext())
047        {
048            steps.next();
049            if (steps.hasNext())
050            {
051                String caller = steps.next().getCaller();
052                users.add(UserIdentity.stringToUserIdentity(caller));
053            }
054        }
055        
056        return users;
057    }
058    
059    @Override 
060    public I18nizableText getLabel()
061    {
062        return new I18nizableText("plugin.web", "PLUGINS_WEB_SEND_MAIL_TO_USER_FUNCTION_LABEL");
063    }
064}