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.cms.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;
025
026import com.opensymphony.workflow.WorkflowException;
027import com.opensymphony.workflow.spi.Step;
028
029/**
030 * OS workflow function to send mail after an action is triggered.
031 */
032public class SendMailToUserFunction extends SendMailFunction
033{
034    @Override
035    protected Set<UserIdentity> _getUsers(WorkflowAwareContent content, Set<String> rights) throws WorkflowException
036    {
037        Set<UserIdentity> users = new HashSet<>();
038        
039        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(content);
040        long workflowId = content.getWorkflowId();
041        
042        Iterator<Step> steps = workflow.getHistorySteps(workflowId).iterator();
043        
044        // Browse the step list to find the newest proposition action.
045        if (steps.hasNext())
046        {
047            steps.next();
048            if (steps.hasNext())
049            {
050                String caller = steps.next().getCaller();
051                users.add(UserIdentity.stringToUserIdentity(caller));
052            }
053        }
054        
055        return users;
056    }
057}