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