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.service.ServiceException;
022import org.apache.commons.lang3.StringUtils;
023
024/**
025 * This extension point handle the existing relations on the client side (relation system allow for example drag and drop).
026 */
027public class RelationsManager extends AbstractClientSideExtensionPoint<ClientSideRelation>
028{
029    /** Avalon role */
030    public static final String ROLE = RelationsManager.class.getName();
031    
032    @Override
033    public List<ClientSideElement> findDependency(String pattern)
034    {
035        if (!StringUtils.startsWith(pattern, "source:") && !StringUtils.startsWith(pattern, "target:"))
036        {
037            throw new IllegalArgumentException("Invalid dependency : " + pattern + ", the prefix 'source:' or 'target:' is mandatory.");
038        }
039        
040        if (StringUtils.startsWith(pattern, "source:"))
041        {
042            return _findSourceDependencies(StringUtils.substring(pattern, "source:".length()));
043        }
044        else if (StringUtils.startsWith(pattern, "target:"))
045        {
046            return _findTargetDependencies(StringUtils.substring(pattern, "target:".length()));
047        }
048        
049        return null;
050    }
051
052    private List<ClientSideElement> _findSourceDependencies(String pattern)
053    {
054        List<ClientSideElement> result = new ArrayList<>();
055        
056        for (String extensionId : getExtensionsIds())
057        {
058            ClientSideRelation extension = getExtension(extensionId);
059            if (extension.getSourceRelationType().contains(pattern))
060            {
061                result.add(extension);
062            }
063        }
064        
065        result.add(_getMessageTargetFactory(pattern));
066        
067        return result;
068    }
069
070    private List<ClientSideElement> _findTargetDependencies(String pattern)
071    {
072        List<ClientSideElement> result = new ArrayList<>();
073        for (String extensionId : getExtensionsIds())
074        {
075            ClientSideRelation extension = getExtension(extensionId);
076            if (extension.getTargetRelationType().contains(pattern))
077            {
078                result.add(extension);
079            }
080        }
081        
082        ClientSideElement messageTargetFactory = _getMessageTargetFactory(pattern);
083        if (messageTargetFactory != null)
084        {
085            result.add(messageTargetFactory);
086        }
087        
088        return result;
089    }
090
091    private ClientSideElement _getMessageTargetFactory(String pattern)
092    {
093        try
094        {
095            MessageTargetFactoriesManager messageTargetFactoriesManager = (MessageTargetFactoriesManager) _cocoonManager.lookup(MessageTargetFactoriesManager.ROLE);
096            
097            return messageTargetFactoriesManager.hasExtension(pattern) ? messageTargetFactoriesManager.getExtension(pattern) : null;
098        }
099        catch (ServiceException e)
100        {
101            if (getLogger().isWarnEnabled())
102            {
103                getLogger().warn("Unable to retrieve the MessageTargetFactoriesManager", e);
104            }
105            
106            return null;   
107        }
108    }
109}