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