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.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.apache.commons.collections.ListUtils;
024
025import org.ametys.cms.repository.DefaultContent;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
028import org.ametys.odf.CallWSHelper;
029import org.ametys.odf.ProgramItem;
030import org.ametys.odf.program.Program;
031import org.ametys.plugins.workflow.EnhancedFunction;
032import org.ametys.runtime.config.Config;
033import org.ametys.runtime.i18n.I18nizableText;
034
035import com.opensymphony.module.propertyset.PropertySet;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * This function calls a WS on remote server to delete a Program
040 * This function should be executed as a post-function of "unpublish" action
041 */
042public class DeleteRemoteProgramFunction extends AbstractContentWorkflowComponent implements EnhancedFunction, Initializable
043{
044    private boolean _isActive;
045    
046    @Override
047    public void initialize() throws Exception
048    {
049        _isActive = Config.getInstance().getValue("odf.publish.server");
050    }
051
052    @Override
053    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
054    {
055        if (!_isActive)
056        {
057            return;
058        }
059        
060        // Retrieve current content
061        WorkflowAwareContent content = getContent(transientVars);
062        
063        if (content instanceof Program)
064        {
065            Map<String, String> params = new HashMap<>();
066            params.put(ProgramItem.CODE, ((Program) content).getCode());
067            params.put(ProgramItem.CATALOG, ((Program) content).getCatalog());
068            params.put(DefaultContent.METADATA_LANGUAGE, ((Program) content).getLanguage());
069            
070            // Delete program on remote application
071            CallWSHelper.callWS("_odf-sync/delete-program", params, _logger);
072        }
073        
074    }
075    
076    @Override
077    public List<FunctionArgument> getArguments()
078    {
079        return ListUtils.EMPTY_LIST;
080    }
081    
082    @Override
083    public I18nizableText getDescription(Map<String, String> args)
084    {
085        return new I18nizableText("plugin.odf", "PLUGINS_ODF_DELETE_REMOTE_PROGRAM_FUNCTION_DESCRIPTION");
086    }
087}