001/*
002 *  Copyright 2025 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.trash.observer;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.content.indexing.solr.SolrIndexer;
027import org.ametys.cms.indexing.IndexingObserver;
028import org.ametys.core.observation.AsyncObserver;
029import org.ametys.core.observation.Event;
030
031/**
032 * Abstract class to define observers on trash elements.
033 */
034public abstract class AbstractTrashElementIndexingObserver implements AsyncObserver, IndexingObserver, Serviceable
035{
036    /** The Solr indexer */
037    protected SolrIndexer _solrIndexer;
038    
039    public void service(ServiceManager serviceManager) throws ServiceException
040    {
041        _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE);
042    }
043    
044    public boolean supports(Event event)
045    {
046        return getSupportedEvents().contains(event.getId());
047    }
048    
049    /**
050     * Get the supported events id by the observer.
051     * @return the supported events
052     */
053    protected abstract Set<String> getSupportedEvents();
054    
055    public int getPriority()
056    {
057        // Not prior, but not last to able other observers to be after
058        return MIN_PRIORITY - 2000;
059    }
060    
061    public void observe(Event event, Map<String, Object> transientVars) throws Exception
062    {
063        processTrashElement(getTrashElementId(event));
064    }
065    
066    /**
067     * Observe and process the trash element
068     * @param trashElementId the trash element identifier
069     * @throws Exception if an exception occurs
070     */
071    protected abstract void processTrashElement(String trashElementId) throws Exception;
072    
073    /**
074     * Get the trash element identifier from the event arguments.
075     * @param event The event
076     * @return the trash element identifier
077     */
078    protected String getTrashElementId(Event event)
079    {
080        return (String) event.getArguments().get(ObservationConstants.ARGS_TRASH_ELEMENT_ID);
081    }
082}