001/*
002 *  Copyright 2017 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.scc;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.slf4j.Logger;
025
026import org.ametys.cms.repository.ModifiableDefaultContent;
027import org.ametys.plugins.contentio.synchronize.impl.LDAPSynchronizableContentsCollection;
028import org.ametys.runtime.config.Config;
029
030/**
031 * Class to import ODF persons. Import a person only in one language.
032 *
033 */
034public class PersonSynchronizableContentsCollection extends LDAPSynchronizableContentsCollection
035{
036    /** Default language configured for ODF */
037    protected String _odfLang;
038
039    @Override
040    protected void configureDataSource(Configuration configuration) throws ConfigurationException
041    {
042        super.configureDataSource(configuration);
043        _odfLang = Config.getInstance().getValue("odf.programs.lang");
044    }
045    
046    @Override
047    protected List<ModifiableDefaultContent> _importOrSynchronizeContent(String idValue, Map<String, List<Object>> remoteValues, boolean forceImport, Logger logger)
048    {
049        return _importOrSynchronizeContent(idValue, _odfLang, remoteValues, forceImport, logger);
050    }
051
052    @Override
053    public List<ModifiableDefaultContent> importContent(String idValue, Map<String, Object> importParams, Logger logger) throws Exception
054    {
055        List<ModifiableDefaultContent> createdContents = new ArrayList<>();
056        
057        Map<String, Object> parameters = putIdParameter(idValue);
058        Map<String, Map<String, List<Object>>> results = getTransformedRemoteValues(parameters, logger);
059        if (!results.isEmpty())
060        {
061            try
062            {
063                createdContents.add(_importContent(idValue, importParams, _odfLang, results.get(idValue), logger));
064            }
065            catch (Exception e)
066            {
067                _nbError++;
068                logger.error("An error occurred while importing or synchronizing content", e);
069            }
070        }
071        
072        return createdContents;
073    }
074}