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.io.IOException;
019import java.util.List;
020import java.util.Map;
021import java.util.concurrent.ExecutionException;
022import java.util.concurrent.Future;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.solr.client.solrj.SolrServerException;
027
028import org.ametys.cms.ObservationConstants;
029import org.ametys.cms.content.indexing.solr.SolrIndexer;
030import org.ametys.cms.content.indexing.solr.observation.ObserverHelper;
031import org.ametys.cms.repository.Content;
032import org.ametys.plugins.repository.jcr.NameHelper.NameComputationMode;
033import org.ametys.runtime.i18n.I18nizableText;
034
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OSWorkflow function for creating a entry of reference table.
039 */
040public class CreateReferenceTableContentFunction extends CreateContentFunction
041{
042    /** The Solr indexer */
043    protected SolrIndexer _solrIndexer;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
050    }
051    
052    @Override
053    protected Map<String, Object> _eventParamsForContentAdded(Content content)
054    {
055        Map<String, Object> eventParams = super._eventParamsForContentAdded(content);
056        eventParams.put(ObservationConstants.ARGS_CONTENT_COMMIT, false);
057        return eventParams;
058    }
059    
060    @Override
061    protected List<Future> _notifyContentAdded(Content content, Map transientVars) throws WorkflowException
062    {
063        List<Future> futures = super._notifyContentAdded(content, transientVars);
064        
065        // Wait for asynchonous observers to finish
066        for (Future future : futures)
067        {
068            try
069            {
070                future.get();
071            }
072            catch (ExecutionException | InterruptedException e)
073            {
074                _logger.error(String.format("Error while waiting for async observer to complete"));
075            }
076        }
077        
078        try
079        {
080            if (ObserverHelper.isNotSuspendedObservationForIndexation())
081            {
082                _solrIndexer.commit();
083            }
084        }
085        catch (SolrServerException | IOException e)
086        {
087            _logger.error("An exception occured when trying to commit changes to Solr", e);
088        }
089        
090        return futures;
091    }
092    
093    @Override
094    protected NameComputationMode _getDefaultNameComputationMode()
095    {
096        return NameComputationMode.GENERATED_KEY;
097    }
098    
099    @Override
100    public I18nizableText getLabel()
101    {
102        return new I18nizableText("plugin.cms", "PLUGINS_CMS_CREATE_REFERENCE_TABLE_CONTENT_FUNCTION_LABEL");
103    }
104}