001/*
002 *  Copyright 2011 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.cdmfr.DeleteRemoteProgramProcessor;
030import org.ametys.odf.program.Program;
031import org.ametys.runtime.config.Config;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * Observes {@link Program} content deletion in order to synchronize with remote server
036 */
037public class RemoteProgramDeletedObserver extends AbstractLogEnabled implements Observer, Serviceable
038{
039    /** The delete remote program processor */
040    protected DeleteRemoteProgramProcessor _deleteRemoteProgramProcessor;
041
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        _deleteRemoteProgramProcessor = (DeleteRemoteProgramProcessor) smanager.lookup(DeleteRemoteProgramProcessor.ROLE);
045    }
046    
047    @Override
048    public boolean supports(Event event)
049    {
050        if (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING) && (boolean) Config.getInstance().getValue("odf.publish.server"))
051        {
052            Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
053            return content instanceof Program;
054        }
055        
056        return false;
057    }
058
059    @Override
060    public int getPriority()
061    {
062        return 0;
063    }
064
065    @Override
066    public void observe(Event event, Map<String, Object> transientVars) throws Exception
067    {
068        Program program = (Program) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
069        _deleteRemoteProgramProcessor.processPrograms(Set.of(program));
070    }
071
072}