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.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.repository.WorkflowAwareContent;
026import org.ametys.core.user.User;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.user.UserManager;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.lock.LockHelper;
031import org.ametys.plugins.repository.lock.LockableAmetysObject;
032import org.ametys.runtime.i18n.I18nizableText;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.Condition;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * OSWorkflow condition for testing the lock state of a content.<p>
040 * Check if the content is locked and if it is the case, the condition
041 * passes only if the lock owner is the current caller.
042 */
043public class LockCondition extends AbstractContentWorkflowComponent implements Condition
044{
045    /** The user manager */
046    protected UserManager _userManager;
047
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
053    }
054    
055    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
056    {
057        WorkflowAwareContent content = getContent(transientVars);
058        
059        try
060        {
061            if (content instanceof LockableAmetysObject)
062            {
063                // Récupérer le content
064                LockableAmetysObject lockableContent = (LockableAmetysObject) content;
065        
066                // Vérifier si le contenu n'est pas verrouillé
067                if (lockableContent.isLocked())
068                {
069                    boolean sameUser = LockHelper.isLockOwner(lockableContent, getUser(transientVars));
070                    
071                    if (!sameUser)
072                    {
073                        List<String> conditionFailures = getConditionFailures(transientVars);
074                        if (conditionFailures != null)
075                        {
076                            conditionFailures.add(String.format("Lock condition failed : content: %s is locked by user: %s", lockableContent, lockableContent.getLockOwner()));
077                        }
078                        
079                        List<String> i18nParams = new ArrayList<>();
080                        i18nParams.add(_contentHelper.getTitle(content));
081                        UserIdentity lockOwnerIdentity = lockableContent.getLockOwner();
082                        User lockOwner = _userManager.getUser(lockOwnerIdentity.getPopulationId(), lockOwnerIdentity.getLogin());
083                        i18nParams.add(lockOwner != null ? lockOwner.getFullName() : "");
084                        i18nParams.add(lockOwnerIdentity.getLogin());
085                        
086                        addWorkflowError(transientVars, new I18nizableText("plugin.cms", "WORKFLOW_CONTENT_LOCK_CONDITION_FAILED", i18nParams));
087                        
088                        if (_logger.isDebugEnabled())
089                        {
090                            _logger.debug(String.format("Content: %s is locked by user: %s, failing condition", lockableContent, lockableContent.getLockOwner()));
091                        }
092                    } 
093                    else if (_logger.isDebugEnabled())
094                    {
095                        _logger.debug(String.format("Content: %s is locked by current user, passing condition", lockableContent));
096                    }
097                    
098                    return sameUser;
099                }
100                else
101                {
102                    // Le contenu n'est pas verrouillé
103                    // FIXME faut-il le vérrouiller ?, si oui il faut faire une pre-function
104                    return true;
105                }
106            }
107            
108            // The content is not lockable: continue.
109            return true;
110        }
111        catch (AmetysRepositoryException e)
112        {
113            _logger.error("Unable to check the lock state of the content from the repository", e);
114            getConditionFailures(transientVars).add(String.format("Lock condition failed"));
115            return false;
116        }
117    }
118}