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.ui;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023
024/**
025 * Default implementation of the ClientSideRelation that expects the configuration to define the supported relations.
026 */
027public class StaticClientSideRelation extends StaticClientSideElement implements ClientSideRelation
028{
029    List<String> _sourceRelationType;
030    List<String> _targetRelationType;
031
032    @Override
033    public void configure(Configuration configuration) throws ConfigurationException
034    {
035        _sourceRelationType = new ArrayList<>();
036        _targetRelationType = new ArrayList<>();
037        List<String> messageTargetDependencies = new ArrayList<>();
038        
039        if (configuration.getChild("relations", false) != null)
040        {
041            for (Configuration relationConfiguration : configuration.getChild("relations").getChildren())
042            {
043                String relationType = relationConfiguration.getName();
044                String relation = relationConfiguration.getValue();
045                
046                if ("source".equals(relationType))
047                {
048                    _sourceRelationType.add(relation);
049                    messageTargetDependencies.add(relation);
050                }
051                else if ("target".equals(relationType))
052                {
053                    _targetRelationType.add(relation);
054                    messageTargetDependencies.add(relation);
055                }
056                else
057                {
058                    throw new ConfigurationException("Unsupported relation type for StaticClientSideRelation : " + relationType, configuration);
059                }
060            }
061        }
062        
063        super.configure(configuration);
064        
065        for (String messageTargetFactory : messageTargetDependencies)
066        {
067            _addMessageTargetDependency(messageTargetFactory);
068        }
069    }
070    
071    private void _addMessageTargetDependency(String messageTargetFactory)
072    {
073        if (_dependencies.containsKey(MessageTargetFactoriesManager.ROLE))
074        {
075            List<String> extensions = _dependencies.get(MessageTargetFactoriesManager.ROLE);
076            extensions.add(messageTargetFactory);
077        }
078        else
079        {
080            List<String> extensions = new ArrayList<>();
081            extensions.add(messageTargetFactory);
082            _dependencies.put(MessageTargetFactoriesManager.ROLE, extensions);
083        }
084    }
085
086    @Override
087    public List<String> getSourceRelationType()
088    {
089        return _sourceRelationType;
090    }
091
092    @Override
093    public List<String> getTargetRelationType()
094    {
095        return _targetRelationType;
096    }
097}