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