001/*
002 *  Copyright 2012 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.io.File;
019import java.io.FileOutputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.avalon.framework.activity.Initializable;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.commons.collections.ListUtils;
031import org.apache.commons.io.FileUtils;
032import org.apache.commons.io.IOUtils;
033import org.apache.excalibur.source.Source;
034import org.apache.excalibur.source.SourceResolver;
035
036import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
037import org.ametys.odf.program.Program;
038import org.ametys.plugins.workflow.EnhancedFunction;
039import org.ametys.runtime.config.Config;
040import org.ametys.runtime.i18n.I18nizableText;
041import org.ametys.runtime.util.AmetysHomeHelper;
042
043import com.opensymphony.module.propertyset.PropertySet;
044import com.opensymphony.workflow.WorkflowException;
045
046/**
047 * Generate and put CDM-fr file in an output folder
048 *
049 */
050public class DepositCDMFRFunction extends AbstractContentWorkflowComponent implements EnhancedFunction, Initializable
051{
052    private File _outputFolder;
053    private boolean _isActive;
054    private SourceResolver _sourceResolver;
055    
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        super.service(manager);
060        _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
061    }
062    
063    @Override
064    public void initialize() throws Exception
065    {
066        _isActive = Config.getInstance().getValue("odf.publish.cdm-fr.output.folder");
067        
068        if (_isActive)
069        {
070            _outputFolder = new File(AmetysHomeHelper.getAmetysHomeData(), "/odf/cdmfr");
071            FileUtils.forceMkdir(_outputFolder);
072        }
073    }
074
075    @Override
076    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
077    {
078        if (!_isActive || _outputFolder == null)
079        {
080            // Do nothing
081            return;
082        }
083        
084        // Retrieve current content
085        Program program = (Program) getContent(transientVars);
086        
087        try
088        {
089            // Generate CDM-FR file
090            Source cdmfrSource = _sourceResolver.resolveURI("cocoon://_plugins/odf/export-cdmfr.xml?id=" + program.getId() + "&" + ExportCDMfrHelper.REQUEST_PARAM_VALID_LABEL + "=true");
091
092            // Save file
093            try (InputStream is = cdmfrSource.getInputStream())
094            {
095                String filename = program.getCDMId() + ".xml";
096                
097                // Delete existing file
098                File file = new File(_outputFolder, filename);
099                if (file.exists())
100                {
101                    file.delete();
102                }
103             
104                try (OutputStream os = new FileOutputStream(file))
105                {
106                    IOUtils.copy(is, os);
107                }
108            }
109        }
110        catch (IOException e)
111        {
112            addWorkflowWarning(transientVars, new I18nizableText("plugin.odf", "PLUGINS_ODF_PUBLISH_PROGRAM_CDMFR_ERROR", Collections.singletonList(program.getTitle())));
113            _logger.error("Unable to generate and copy the CDM-fr file", e);
114        }
115    }
116    
117    @Override
118    public List<FunctionArgument> getArguments()
119    {
120        return ListUtils.EMPTY_LIST;
121    }
122
123    @Override
124    public I18nizableText getDescription(Map<String, String> args)
125    {
126        return new I18nizableText("plugin.odf", "PLUGINS_ODF_DEPOSIT_CDMFR_FUNCTION_DESCRIPTION");
127    }
128}