001/*
002 *  Copyright 2016 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.core.datasource;
017
018import java.util.HashMap;
019import java.util.HashSet;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.Set;
023
024import org.ametys.core.datasource.DataSourceConsumer.TypeOfUse;
025import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
026
027/**
028 * This class is in charge to load and initialize the various {@link DataSourceConsumer} 
029 */
030public class DataSourceConsumerExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<DataSourceConsumer>
031{
032    /** Avalon Role */
033    public static final String ROLE = DataSourceConsumerExtensionPoint.class.getName();
034
035    /**
036     * Determines if a data source is in use by a {@link DataSourceConsumer}
037     * @param id The id of the data source to check
038     * @return true if the data source is used by at least one of the {@link DataSourceConsumer}
039     */
040    public TypeOfUse isInUse(String id)
041    {
042        Set<TypeOfUse> typesOfUse = new HashSet<>();
043        
044        for (String extensionId : getExtensionsIds())
045        {
046            DataSourceConsumer dataSourceClient = getExtension(extensionId);
047            typesOfUse.add(dataSourceClient.isInUse(id));
048        }
049        
050        return TypeOfUse.merge(typesOfUse);
051    }
052    
053    /**
054     * Retrieve the ids of the used data sources
055     * @return the set of ids of data source used by all of the {@link DataSourceConsumer}
056     */
057    public Map<String, TypeOfUse> getUsedDataSourceIds()
058    {
059        Set<String> extensionsIds = getExtensionsIds();
060        Map<String, TypeOfUse> usedDataSourceIds = new HashMap<>();
061        
062        for (String extensionId : extensionsIds)
063        {
064            DataSourceConsumer dataSourceClient = getExtension(extensionId);
065            for (Entry<String, TypeOfUse> usedDataSource : dataSourceClient.getUsedDataSourceIds().entrySet())
066            {
067                if (usedDataSourceIds.containsKey(usedDataSource.getKey()))
068                {
069                    usedDataSourceIds.put(usedDataSource.getKey(), TypeOfUse.merge(usedDataSourceIds.get(usedDataSource.getKey()), usedDataSource.getValue()));
070                }
071                else
072                {
073                    usedDataSourceIds.put(usedDataSource.getKey(), usedDataSource.getValue());
074                }
075            }
076        }
077        
078        return usedDataSourceIds;
079    }
080}