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.Map;
019
020import org.ametys.core.user.UserIdentity;
021import org.ametys.plugins.repository.AmetysRepositoryException;
022import org.ametys.plugins.repository.lock.LockHelper;
023import org.ametys.plugins.repository.lock.LockableAmetysObject;
024
025import com.opensymphony.module.propertyset.PropertySet;
026import com.opensymphony.workflow.FunctionProvider;
027import com.opensymphony.workflow.WorkflowException;
028
029/**
030 * OSWorkflow function to unlock a content.
031 */
032public class UnlockContentFunction extends AbstractContentWorkflowComponent implements FunctionProvider
033{
034    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
035    {
036        _logger.info("Performing edit workflow function");
037
038        // Retrieve current content
039        LockableAmetysObject content = (LockableAmetysObject) getContent(transientVars);
040        
041        try
042        {
043            if (content.isLocked())
044            {
045                UserIdentity user = getUser(transientVars);
046                boolean sameUser = LockHelper.isLockOwner(content, user);
047                
048                if (sameUser)
049                {
050                    if (_logger.isDebugEnabled())
051                    {
052                        _logger.debug(String.format("Content: %s is locked by current user, unlocking it", content));
053                    }
054                    
055                    content.unlock();
056                }
057                else
058                {
059                    if (_logger.isDebugEnabled())
060                    {
061                        _logger.debug(String.format("Content: %s is locked by user: %s, preventing from unlocking it", content, content.getLockOwner()));
062                    }
063                    throw new WorkflowException(String.format("User %s tried to unlock content %s but he is not the lock owner %s", user, content, content.getLockOwner()));
064                }
065            }
066        }
067        catch (AmetysRepositoryException e)
068        {
069            throw new WorkflowException(String.format("Unable to unlock content %s", content));
070        }
071    }
072
073}