001/*
002 *  Copyright 2024 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;
023
024import org.apache.commons.io.FileUtils;
025import org.apache.commons.io.IOUtils;
026
027import org.ametys.odf.program.Program;
028import org.ametys.runtime.util.AmetysHomeHelper;
029
030/**
031 * Processor to deposit the CDM-fr file
032 */
033public class DepositCDMfrProcessor extends AbstractCDMfrProcessor
034{
035    /** The component role. */
036    public static final String ROLE = DepositCDMfrProcessor.class.getName();
037    
038    private File _outputFolder;
039    
040    @Override
041    public void initialize() throws Exception
042    {
043        super.initialize();
044        if (isActive())
045        {
046            _outputFolder = new File(AmetysHomeHelper.getAmetysHomeData(), "/odf/cdmfr");
047            FileUtils.forceMkdir(_outputFolder);
048        }
049    }
050    @Override
051    protected String getConfigActiveParam()
052    {
053        return "odf.publish.cdm-fr.output.folder";
054    }
055    
056    @Override
057    protected void processProgram(Program program, InputStream cdmfrContent) throws IOException
058    {
059        String filename = program.getCDMId() + ".xml";
060        
061        // Delete existing file
062        File file = new File(_outputFolder, program.getCatalog() + File.separator + filename);
063        FileUtils.forceMkdirParent(file);
064        if (file.exists())
065        {
066            file.delete();
067        }
068
069        // Deposit CDM-fr for the given programs
070        try (OutputStream os = new FileOutputStream(file))
071        {
072            IOUtils.copy(cdmfrContent, os);
073        }
074    }
075    
076    @Override
077    protected boolean isCDMfrForAmetys()
078    {
079        return false;
080    }
081    
082    @Override
083    protected String getErrorMessage()
084    {
085        return "Unable to generate and copy the CDM-fr file";
086    }
087}