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.IOException;
019import java.io.InputStream;
020import java.util.Collection;
021import java.util.Set;
022import java.util.stream.Stream;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.util.log.SLF4JLoggerAdapter;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.odf.CallWSHelper;
030import org.ametys.odf.catalog.CatalogsManager;
031import org.ametys.odf.program.Program;
032import org.ametys.runtime.config.Config;
033
034/**
035 * Processor to send the CDM-fr file
036 */
037public class SendCDMfrProcessor extends AbstractCDMfrProcessor
038{
039    /** The component role. */
040    public static final String ROLE = SendCDMfrProcessor.class.getName();
041    
042    /** The catalogs manager */
043    protected CatalogsManager _catalogsManager;
044
045    private String _catalogMode;
046    private Collection<String> _selectedCatalogs;
047    
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
053    }
054    
055    @Override
056    public void initialize() throws Exception
057    {
058        super.initialize();
059        _catalogMode = Config.getInstance().getValue("odf.publish.catalog.mode", true, "ALL");
060        if (_catalogMode.equals("SELECT"))
061        {
062            _selectedCatalogs = org.ametys.core.util.StringUtils.stringToCollection(Config.getInstance().getValue("odf.publish.catalogs"));
063        }
064    }
065    
066    @Override
067    protected String getConfigActiveParam()
068    {
069        return "odf.publish.server";
070    }
071    
072    @Override
073    protected Stream<Program> filterPrograms(Stream<Program> programs)
074    {
075        switch (_catalogMode)
076        {
077            case "ALL":
078                // Do not filter programs
079                return programs;
080            case "DEFAULT":
081                // Filter programs on default catalog
082                String defaultCatalogName = _catalogsManager.getDefaultCatalogName();
083                return programs.filter(p -> p.getCatalog().equals(defaultCatalogName));
084            case "SELECT":
085                // Filter programs on selected catalogs
086                return programs.filter(p -> _selectedCatalogs.contains(p.getCatalog()));
087            default:
088                // Should not happen
089                throw new RuntimeException("The parameter 'odf.publish.catalog.mode' has not a valid value.");
090        }
091    }
092    
093    @Override
094    protected boolean isCDMfrForAmetys()
095    {
096        return true;
097    }
098    
099    @Override
100    protected void processProgram(Program program, InputStream cdmfrContent) throws IOException
101    {
102        // Send CDM-fr to portails for the given program
103        Set<String> failedUrl = CallWSHelper.callWS("_odf-sync/upload-cdm", cdmfrContent, new SLF4JLoggerAdapter(getLogger()));
104        if (failedUrl == null || failedUrl.size() > 0)
105        {
106            getLogger().error("The program {} can't be synchronized with portals {}", program.getId(), StringUtils.defaultString(StringUtils.join(failedUrl, ", ")));
107        }
108    }
109    
110    @Override
111    protected String getErrorMessage()
112    {
113        return "Unable to publish CDM-fr on distant server";
114    }
115}