001/*
002 *  Copyright 2015 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.contentstree.ui;
017
018import java.util.HashMap;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.plugins.contentstree.TreeConfiguration;
031import org.ametys.plugins.contentstree.TreeConfigurationContentType;
032import org.ametys.plugins.contentstree.TreeConfigurationElements;
033import org.ametys.plugins.contentstree.TreeExtensionPoint;
034import org.ametys.core.ui.StaticClientSideElement;
035
036/**
037 * This class act as a StaticClientSideElement but do handle a new tag "tree-config" that lead to a file containing the tree configuration
038 */
039public class OpenTreeControllerClientSideElement extends StaticClientSideElement
040{
041    /** The tree configuration EP */
042    protected TreeExtensionPoint _treeExtensionPoint;
043
044    /** During startup will contains the configured tree config id. Null once initialized */
045    protected String _treeConfigId;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        
052        _treeExtensionPoint = (TreeExtensionPoint) smanager.lookup(TreeExtensionPoint.ROLE);
053    }
054    
055    @Override
056    public void configure(Configuration configuration) throws ConfigurationException
057    {
058        super.configure(configuration);
059        
060        _treeConfigId = configuration.getChild("tree-config").getValue();
061    }
062    
063    /**
064     * Lazy configuration of the component
065     */
066    protected void _lazyConfigure()
067    {
068        if (_treeConfigId != null)
069        {
070            TreeConfiguration treeConfiguration = _treeExtensionPoint.getExtension(_treeConfigId);
071            
072            _script.getParameters().put("opentool-id", treeConfiguration.getUIToolRole());
073            
074            Map<String, Object> openToolParams = new HashMap<>();
075            openToolParams.put("treeId", _treeConfigId);
076            _script.getParameters().put("opentool-params", openToolParams); 
077
078            
079            Set<String> busMessageTypesRegExp = new HashSet<>();
080            Set<String> contentTypeIdsRegExp = new HashSet<>();
081            
082            for (TreeConfigurationElements element : treeConfiguration.getElements())
083            {
084                for (TreeConfigurationContentType contentTypeInfo : element.getContentTypesConfiguration())
085                {
086                    if (contentTypeInfo.canBeRoot())
087                    {
088                        busMessageTypesRegExp.add("^" + contentTypeInfo.getMessageBusType() + "$");
089                        
090                        for (String id : contentTypeInfo.getContentTypesIds())
091                        {
092                            contentTypeIdsRegExp.add("^" + id + "$");
093                        }
094                    }
095                }
096            }
097            
098            _script.getParameters().put("selection-target-id", StringUtils.join(busMessageTypesRegExp, "|"));
099            
100            Map<String, Object> selectionTargetParameter = new HashMap<>();
101            selectionTargetParameter.put("name", "^types$");
102            selectionTargetParameter.put("value", StringUtils.join(contentTypeIdsRegExp, "|"));
103            
104            _script.getParameters().put("selection-target-parameter", selectionTargetParameter);
105
106            _treeConfigId = null;
107        }
108    }
109    
110    @Override
111    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
112    {
113        _lazyConfigure();
114        
115        return super.getScripts(ignoreRights, contextParameters);
116    }
117}