001/*
002 *  Copyright 2016 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.cdmfr;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.activity.Initializable;
022
023import org.ametys.cms.repository.DefaultContent;
024import org.ametys.cms.repository.WorkflowAwareContent;
025import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
026import org.ametys.odf.CallWSHelper;
027import org.ametys.odf.ProgramItem;
028import org.ametys.odf.program.Program;
029import org.ametys.runtime.config.Config;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.FunctionProvider;
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * This function calls a WS on remote server to delete a Program
037 * This function should be executed as a post-function of "unpublish" action
038 */
039public class DeleteRemoteProgramFunction extends AbstractContentWorkflowComponent implements FunctionProvider, Initializable
040{
041    private boolean _isActive;
042    
043    @Override
044    public void initialize() throws Exception
045    {
046        _isActive = Config.getInstance().getValueAsBoolean("odf.publish.server");
047    }
048
049    @Override
050    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
051    {
052        if (!_isActive)
053        {
054            return;
055        }
056        
057        // Retrieve current content
058        WorkflowAwareContent content = getContent(transientVars);
059        
060        if (content instanceof Program)
061        {
062            Map<String, String> params = new HashMap<>();
063            params.put(ProgramItem.METADATA_CODE, ((Program) content).getCode());
064            params.put(ProgramItem.METADATA_CATALOG, ((Program) content).getCatalog());
065            params.put(DefaultContent.METADATA_LANGUAGE, ((Program) content).getLanguage());
066            
067            // Delete program on remote application
068            CallWSHelper.callWS("_odf-sync/delete-program", params, _logger);
069        }
070    }
071}