001/*
002 *  Copyright 2024 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.odf.observation;
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.repository.Content;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.odf.ProgramItem;
030import org.ametys.odf.cdmfr.CDMFRHandler;
031import org.ametys.odf.orgunit.OrgUnit;
032import org.ametys.odf.person.Person;
033import org.ametys.odf.program.Program;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035
036/**
037 * Abstract observer to handle CDMFR file when a odf content is validated
038 */
039public abstract class AbstractCDMFRObserver extends AbstractLogEnabled implements Observer, Serviceable
040{
041    /** The CDM-fr handler */
042    protected CDMFRHandler _cdmfrHandler;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _cdmfrHandler = (CDMFRHandler) manager.lookup(CDMFRHandler.ROLE);
048    }
049    
050    @Override
051    public boolean supports(Event event)
052    {
053        if (isActive() && !_cdmfrHandler.isSuspended() && event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED))
054        {
055            Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
056            return content instanceof OrgUnit || content instanceof Person || content instanceof ProgramItem;
057        }
058        
059        return false;
060    }
061    
062    @Override
063    public int getPriority()
064    {
065        return 3000;
066    }
067
068    @Override
069    public void observe(Event event, Map<String, Object> transientVars) throws Exception
070    {
071        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
072        Set<Program> programs = _cdmfrHandler.getProgramParents(content);
073        doObserve(programs);
074    }
075    
076    /**
077     * <code>true</code> if the observer is active
078     * @return <code>true</code> if the observer is active
079     */
080    protected abstract boolean isActive();
081
082    /**
083     * Do observe for CDM-fr observer
084     * @param programs the concerned programs
085     */
086    protected abstract void doObserve(Set<Program> programs);
087}