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.plugins.workflow.EnhancedCondition;
033import org.ametys.runtime.i18n.I18nizableText;
034
035import com.opensymphony.module.propertyset.PropertySet;
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 EnhancedCondition
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<ConditionFailure> conditionFailures = getConditionFailures(transientVars);
074                        if (conditionFailures != null)
075                        {
076                            conditionFailures.add(new ConditionFailure(
077                                String.format("Lock condition failed : content: %s is locked by user: %s", lockableContent, lockableContent.getLockOwner()),
078                                LockCondition.class.getName()
079                            ));
080                        }
081                        
082                        List<String> i18nParams = new ArrayList<>();
083                        i18nParams.add(_contentHelper.getTitle(content));
084                        UserIdentity lockOwnerIdentity = lockableContent.getLockOwner();
085                        User lockOwner = _userManager.getUser(lockOwnerIdentity.getPopulationId(), lockOwnerIdentity.getLogin());
086                        i18nParams.add(lockOwner != null ? lockOwner.getFullName() : "");
087                        i18nParams.add(lockOwnerIdentity.getLogin());
088                        
089                        addWorkflowError(transientVars, new I18nizableText("plugin.cms", "WORKFLOW_CONTENT_LOCK_CONDITION_FAILED", i18nParams));
090                        
091                        if (_logger.isDebugEnabled())
092                        {
093                            _logger.debug(String.format("Content: %s is locked by user: %s, failing condition", lockableContent, lockableContent.getLockOwner()));
094                        }
095                    } 
096                    else if (_logger.isDebugEnabled())
097                    {
098                        _logger.debug(String.format("Content: %s is locked by current user, passing condition", lockableContent));
099                    }
100                    
101                    return sameUser;
102                }
103                else
104                {
105                    // Le contenu n'est pas verrouillé
106                    // FIXME faut-il le vérrouiller ?, si oui il faut faire une pre-function
107                    return true;
108                }
109            }
110            
111            // The content is not lockable: continue.
112            return true;
113        }
114        catch (AmetysRepositoryException e)
115        {
116            _logger.error("Unable to check the lock state of the content from the repository", e);
117            getConditionFailures(transientVars).add(new ConditionFailure(
118                String.format("Lock condition failed"),
119                LockCondition.class.getName()
120            ));
121            return false;
122        }
123    }
124
125    @Override
126    public I18nizableText getLabel()
127    {
128        return new I18nizableText("plugin.cms", "PLUGINS_CMS_LOCKABLE_CONDITION_LABEL");
129    }
130}