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