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