001/*
002 *  Copyright 2018 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.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.references.OutgoingReferences;
024import org.ametys.cms.content.references.OutgoingReferencesExtractor;
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.lock.LockHelper;
030import org.ametys.plugins.repository.lock.LockableAmetysObject;
031import org.ametys.plugins.workflow.EnhancedFunction;
032import org.ametys.runtime.i18n.I18nizableText;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OSWorkflow function to extract outgoing references in a content.
039 * 
040 * The required transient variables:
041 * - AbstractContentWorkflowComponent.RESULT_MAP_KEY - Map<String, Object> The map containing the results of the function.
042 * - AbstractContentWorkflowComponent.RESULT_MAP_KEY.result - String "true" when everything goes fine. Missing in other case.
043 * - AbstractContentWorkflowComponent.CONTENT_KEY - WorkflowAwareContent The content that will be edited. Should have the lock token.
044 */
045public class ExtractOutgoingReferencesFunction extends AbstractContentWorkflowComponent implements EnhancedFunction
046{
047    /** The outgoing references extractor */
048    protected OutgoingReferencesExtractor _outgoingReferencesExtractor;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _outgoingReferencesExtractor = (OutgoingReferencesExtractor) smanager.lookup(OutgoingReferencesExtractor.ROLE);
055    }
056    
057    @Override
058    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
059    {
060        _logger.info("Performing extract outgoing references workflow function");
061
062        
063        // Retrieve current content
064        WorkflowAwareContent content = getContent(transientVars);
065        UserIdentity user = getUser(transientVars);
066        
067        if (!(content instanceof ModifiableContent))
068        {
069            throw new IllegalArgumentException("The provided content " + content.getId() + " is not a ModifiableContent.");
070        }
071        
072        ModifiableContent modifiableContent = (ModifiableContent) content;
073        
074        try
075        {
076            LockableAmetysObject lockableContent = null;
077            if (content instanceof LockableAmetysObject)
078            {
079                lockableContent = (LockableAmetysObject) content;
080                if (lockableContent.isLocked() && !LockHelper.isLockOwner(lockableContent, user))
081                {
082                    throw new WorkflowException("User '" + user + "' try to save content '" + modifiableContent.getName() + "' but it is locked by another user");
083                }
084            }
085            
086            _extractOutgoingReferences(modifiableContent);
087            
088            // Commit changes
089            modifiableContent.saveChanges();
090            
091            getResultsMap(transientVars).put("result", "ok");
092        }
093        catch (AmetysRepositoryException e)
094        {
095            throw new WorkflowException("Unable to edit content " + modifiableContent + " from the repository", e);
096        }
097    }
098
099    /**
100     * Analyze the content to extract outgoing references and store them
101     * @param content The content to analyze
102     */
103    protected void _extractOutgoingReferences(ModifiableContent content)
104    {
105        Map<String, OutgoingReferences> outgoingReferencesByPath = _outgoingReferencesExtractor.getOutgoingReferences(content);
106        content.setOutgoingReferences(outgoingReferencesByPath);
107    }
108
109    @Override
110    public FunctionType getFunctionExecType()
111    {
112        return FunctionType.POST;
113    }
114    
115    @Override
116    public I18nizableText getLabel()
117    {
118        return new I18nizableText("plugin.cms", "PLUGINS_CMS_EXTRACT_OUTGOING_REFERENCES_FUNCTION_LABEL");
119    }
120}