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