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