001/*
002 *  Copyright 2017 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.List;
019import java.util.Map;
020import java.util.concurrent.ExecutionException;
021import java.util.concurrent.Future;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableContent;
029
030import com.opensymphony.workflow.WorkflowException;
031
032/**
033 * OSWorkflow function for creating a entry of reference table.
034 */
035public class CreateReferenceTableContentFunction extends CreateContentFunction
036{
037    private static final String __PARENT_TRANSIENT_VAR = "parent";
038    
039    /** The helper component for hierarchical simple contents */
040    protected HierarchicalReferenceTablesHelper _hierarchicalRefTableContentsHelper;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _hierarchicalRefTableContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE);
047    }
048    
049    @Override
050    protected List<Future> _notifyContentAdded(Content content, Map transientVars) throws WorkflowException
051    {
052        List<Future> futures = super._notifyContentAdded(content, transientVars);
053        
054        // Wait for asynchonous observers to finish
055        for (Future future : futures)
056        {
057            try
058            {
059                future.get();
060            }
061            catch (ExecutionException | InterruptedException e)
062            {
063                _logger.error(String.format("Error while waiting for async observer to complete"));
064            }
065        }
066        
067        return futures;
068    }
069    
070    @Override
071    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
072    {
073        super._populateAdditionalData(transientVars, content);
074        
075        if (transientVars.containsKey(__PARENT_TRANSIENT_VAR))
076        {
077            String parentId = (String) transientVars.get(__PARENT_TRANSIENT_VAR);
078            if (_resolver.hasAmetysObjectForId(parentId))
079            {
080                Content parent = _resolver.resolveById(parentId);
081                _hierarchicalRefTableContentsHelper.setParentMetadata(content, parent);
082            }
083        }
084    }
085}