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.Arrays;
019import java.util.Map;
020import java.util.Set;
021import java.util.stream.Collectors;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.cms.CmsConstants;
028import org.ametys.cms.ObservationConstants;
029import org.ametys.cms.repository.Content;
030import org.ametys.core.observation.Event;
031import org.ametys.core.observation.Observer;
032import org.ametys.odf.ProgramItem;
033import org.ametys.odf.cdmfr.CDMFRHandler;
034import org.ametys.odf.orgunit.OrgUnit;
035import org.ametys.odf.person.Person;
036import org.ametys.odf.program.Program;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038
039/**
040 * Abstract observer to handle CDMFR file when a odf content is validated
041 */
042public abstract class AbstractCDMFRObserver extends AbstractLogEnabled implements Observer, Serviceable
043{
044    /** The CDM-fr handler */
045    protected CDMFRHandler _cdmfrHandler;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _cdmfrHandler = (CDMFRHandler) manager.lookup(CDMFRHandler.ROLE);
051    }
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        if (isActive() && !_cdmfrHandler.isSuspended() && event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED))
057        {
058            Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
059            return content instanceof OrgUnit || content instanceof Person || content instanceof ProgramItem;
060        }
061        
062        return false;
063    }
064    
065    @Override
066    public int getPriority()
067    {
068        return 3000;
069    }
070
071    @Override
072    public void observe(Event event, Map<String, Object> transientVars) throws Exception
073    {
074        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
075        Set<Program> programs = getRelatedPrograms(content);
076        doObserve(programs);
077    }
078    
079    /**
080     * Get the related programs from the content
081     * @param content the content
082     * @return the related program
083     */
084    protected Set<Program> getRelatedPrograms(Content content)
085    {
086        return _cdmfrHandler.getProgramParents(content)
087                .stream()
088                .filter(this::_hasLiveVersion)
089                .collect(Collectors.toSet());
090    }
091    
092    private boolean _hasLiveVersion(Program program)
093    {
094        // Get all labels
095        String[] allLabels = program.getAllLabels();
096        return Arrays.asList(allLabels).contains(CmsConstants.LIVE_LABEL);
097    }
098    
099    /**
100     * <code>true</code> if the observer is active
101     * @return <code>true</code> if the observer is active
102     */
103    protected abstract boolean isActive();
104
105    /**
106     * Do observe for CDM-fr observer
107     * @param programs the concerned programs
108     */
109    protected abstract void doObserve(Set<Program> programs);
110    
111}