001/*
002 *  Copyright 2020 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.repository.data.external;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
023import org.ametys.runtime.model.ModelItem;
024import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
025
026/**
027 * Extension point for {@link ExternalizableDataProvider}s.
028 */
029public class ExternalizableDataProviderExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<ExternalizableDataProvider>
030{
031    /** Avalon Role */
032    public static final String ROLE = ExternalizableDataProviderExtensionPoint.class.getName();
033    
034    /**
035     * Get the path of data that can be valued externally or locally by a {@link ExternalizableDataProvider}
036     * @param dataHolder The externalizable data holder
037     * @return The paths of data that can be valued externally or locally
038     */
039    public Set<String> getExternalizableDataPaths(ModelAwareDataHolder dataHolder)
040    {
041        Set<String> dataPaths = new HashSet<>();
042        
043        for (String id : getExtensionsIds())
044        {
045            ExternalizableDataProvider provider = getExtension(id);
046            
047            for (String dataPath : provider.getExternalizableDataPaths(dataHolder))
048            {
049                if (dataPaths.contains(dataPath))
050                {
051                    throw new UnsupportedOperationException("The data of path " + dataPath + " is already handled by another externalizable data provider");
052                }
053                dataPaths.add(dataPath);
054            }
055        }
056        return dataPaths;
057    }
058
059    /**
060     * Checks if the data of given model item is externalizable
061     * @param dataHolder The externalizable data holder
062     * @param modelItem the model item
063     * @return <code>true</code> if the data of given model item is externalizable, <code>false</code> otherwise
064     */
065    public boolean isDataExternalizable(ModelAwareDataHolder dataHolder, ModelItem modelItem)
066    {
067        return isDataExternalizable(dataHolder, modelItem, Map.of());
068    }
069    
070    /**
071     * Checks if the data of given model item is externalizable in the given context
072     * @param dataHolder The externalizable data holder
073     * @param modelItem the model item
074     * @param context the context that can be used to determine if the data is externalizable
075     * @return <code>true</code> if the data of given model item is externalizable, <code>false</code> otherwise
076     */
077    public boolean isDataExternalizable(ModelAwareDataHolder dataHolder, ModelItem modelItem, Map<String, Object> context)
078    {
079        for (String id : getExtensionsIds())
080        {
081            ExternalizableDataProvider provider = getExtension(id);
082            if (provider.isDataExternalizable(dataHolder, modelItem, context))
083            {
084                return true;
085            }
086        }
087        
088        // no provider set this data as externalizable
089        return false;
090    }
091}