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.HashMap;
019import java.util.Map;
020
021import org.apache.cocoon.util.log.SLF4JLoggerAdapter;
022
023import org.ametys.cms.ObservationConstants;
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.DefaultContent;
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.odf.CallWSHelper;
029import org.ametys.odf.ProgramItem;
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
038{    
039    @Override
040    public boolean supports(Event event)
041    {
042        if (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING) && Config.getInstance().getValueAsBoolean("odf.publish.server"))
043        {
044            Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
045            return content instanceof Program;
046        }
047        
048        return false;
049    }
050
051    @Override
052    public int getPriority(Event event)
053    {
054        return 0;
055    }
056
057    @Override
058    public void observe(Event event, Map<String, Object> transientVars) throws Exception
059    {
060        Program program = (Program) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
061        
062        Map<String, String> params = new HashMap<>();
063        params.put(ProgramItem.METADATA_CODE, program.getCode());
064        params.put(ProgramItem.METADATA_CATALOG, program.getCatalog());
065        params.put(DefaultContent.METADATA_LANGUAGE, program.getLanguage());
066        
067        // Delete program on remote application
068        CallWSHelper.callWS("_odf-sync/delete-program", params, new SLF4JLoggerAdapter(getLogger()));
069    }
070
071}