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; 032 033import com.opensymphony.workflow.WorkflowException; 034 035/** 036 * OSWorkflow function for creating a entry of reference table. 037 */ 038public class CreateReferenceTableContentFunction extends CreateContentFunction 039{ 040 /** The Solr indexer */ 041 protected SolrIndexer _solrIndexer; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 048 } 049 050 @Override 051 protected Map<String, Object> _eventParamsForContentAdded(Content content) 052 { 053 Map<String, Object> eventParams = super._eventParamsForContentAdded(content); 054 eventParams.put(ObservationConstants.ARGS_CONTENT_COMMIT, false); 055 return eventParams; 056 } 057 058 @Override 059 protected List<Future> _notifyContentAdded(Content content, Map transientVars) throws WorkflowException 060 { 061 List<Future> futures = super._notifyContentAdded(content, transientVars); 062 063 // Wait for asynchonous observers to finish 064 for (Future future : futures) 065 { 066 try 067 { 068 future.get(); 069 } 070 catch (ExecutionException | InterruptedException e) 071 { 072 _logger.error(String.format("Error while waiting for async observer to complete")); 073 } 074 } 075 076 try 077 { 078 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 079 { 080 _solrIndexer.commit(); 081 } 082 } 083 catch (SolrServerException | IOException e) 084 { 085 _logger.error("An exception occured when trying to commit changes to Solr", e); 086 } 087 088 return futures; 089 } 090}