001/*
002 *  Copyright 2019 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.plugins.odfsync.cdmfr;
017
018import java.io.InputStream;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.ProcessingException;
028import org.slf4j.Logger;
029
030import org.ametys.cms.repository.ModifiableDefaultContent;
031import org.ametys.plugins.odfsync.cdmfr.actions.UploadCDMFrAction;
032import org.ametys.plugins.odfsync.cdmfr.components.ImportCDMFrComponent;
033import org.ametys.plugins.odfsync.cdmfr.components.impl.RemoteImportCDMFrComponent;
034
035/**
036 * Class to import remote CDMFr contents
037 */
038public class RemoteCDMFrSynchronizableContentsCollection extends AbstractCDMFrSynchronizableContentsCollection
039{
040    /** The name of the param to get the shared with type */
041    public static final String PARAM_SHARED_WITH_TYPE = "shared.subprogram.type";
042    
043    /** The name of the param to get the role of the CDM-fr importer */
044    public static final String PARAM_CDMFR_IMPORTER_ROLE = "remote.implementation";
045    
046    /** The name of the param to get the catalog */
047    public static final String PARAM_CDMFR_CATALOG = "remote.catalog";
048    
049    /** The name of the param to validate after import */
050    public static final String PARAM_CDMFR_VALIDATE_AFTER_IMPORT = "remote.validate.after.import";
051    
052    /** The service manager */
053    protected ServiceManager _manager;
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        super.service(manager);
059        _manager = manager;
060    }
061    
062    @Override
063    protected void configureStaticParams(Configuration configuration) throws ConfigurationException
064    {
065        super.configureStaticParams(configuration);
066        try
067        {
068            _importCDMFrComponent = (ImportCDMFrComponent) _manager.lookup(getImportCDMFrRole());
069        }
070        catch (ServiceException e)
071        {
072            throw new ConfigurationException("An error occurred getting the CDM-fr importer", e);
073        }
074    }
075
076    @Override
077    @SuppressWarnings("unchecked")
078    public List<ModifiableDefaultContent> importContent(String idValue, Map<String, Object> additionalParameters, Logger logger) throws Exception
079    {
080        if (additionalParameters.containsKey(UploadCDMFrAction.CDMFR_IMPUT_STREAM_KEY_NAME))
081        {
082            InputStream is = (InputStream) additionalParameters.get(UploadCDMFrAction.CDMFR_IMPUT_STREAM_KEY_NAME);
083            try
084            {
085                Map<String, Object> parameters = new HashMap<>();
086                parameters.put(PARAM_SHARED_WITH_TYPE, getSharedWithType());
087                parameters.put(PARAM_CDMFR_CATALOG, getCatalog());
088                parameters.put(PARAM_CDMFR_VALIDATE_AFTER_IMPORT, validateAfterImport());
089                Map<String, Object> resultMap = _importCDMFrComponent.handleInputStream(is, parameters, logger);
090                
091                Map<String, Integer> importedContents = (Map<String, Integer>) resultMap.get("importedContents");
092                for (String contentId : importedContents.keySet())
093                {
094                    ModifiableDefaultContent content = _resolver.resolveById(contentId);
095                    updateSCCProperty(content);
096                    content.saveChanges();
097                }
098                
099                return (List<ModifiableDefaultContent>) resultMap.get("importedPrograms");
100            }
101            catch (ProcessingException e)
102            {
103                logger.error("An error occurred. Can't import remote program.", e);
104            }
105        }
106        else
107        {
108            logger.error("An error occurred because no input stream has been sent");
109        }
110        
111        return null;
112    }
113
114    /**
115     * Get the defined way to detect shared program
116     * @return The defined way to detect shared program
117     */
118    protected String getSharedWithType()
119    {
120        return (String) getParameterValues().get(PARAM_SHARED_WITH_TYPE);
121    }
122    
123    /**
124     * Get the role of the CDM-fr importer
125     * @return the role of the CDM-fr importer
126     */
127    protected String getImportCDMFrRole()
128    {
129        return (String) getParameterValues().getOrDefault(PARAM_CDMFR_IMPORTER_ROLE, RemoteImportCDMFrComponent.ROLE);
130    }
131    
132    /**
133     * Get the catalog
134     * @return the catalog
135     */
136    protected String getCatalog()
137    {
138        return (String) getParameterValues().get(PARAM_CDMFR_CATALOG);
139    }
140}