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.skinfactory.model;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.core.ui.ClientSideElement;
028import org.ametys.core.ui.SimpleMenu;
029
030/**
031 * This implementation looks at existing {@link ImportModelActionExtensionPoint} to create a button for import skin. 
032 */
033public class ImportModelClientSideElement extends SimpleMenu
034{
035    private ImportModelActionExtensionPoint _importModelActionEP;
036
037    private boolean _importMenuItemsInitialized;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _importModelActionEP = (ImportModelActionExtensionPoint) smanager.lookup(ImportModelActionExtensionPoint.ROLE);
044    }
045    
046    private void _resolveImportMenuItems ()
047    {
048        if (!_importMenuItemsInitialized)
049        {
050            List<UnresolvedItem> unresolvedImportMenuItems = new ArrayList<>();
051            
052            Set<String> importActionIds = _importModelActionEP.getExtensionsIds();
053            
054            if (importActionIds.size() > 1)
055            {
056                for (String id : importActionIds)
057                {
058                    unresolvedImportMenuItems.add(new UnresolvedItem(id, false, false));
059                }
060            }
061            
062            for (UnresolvedItem unresolvedItem : unresolvedImportMenuItems)
063            {
064                String id = unresolvedItem.getId();
065                ClientSideElement element = _importModelActionEP.getExtension(id);
066                
067                if (unresolvedItem.isPrimary())
068                {
069                    _primaryMenuItem = element;
070                }
071                
072                _menuItems.add(element);
073                _referencedClientSideElement.add(element);
074            }
075            
076            _importMenuItemsInitialized = true;
077        }
078    }
079    
080    @Override
081    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
082    {
083        List<Script> scripts = new ArrayList<>();
084        
085        Set<String> importActionIds = _importModelActionEP.getExtensionsIds();
086        
087        if (importActionIds.size() == 0)
088        {
089            // Non button
090            return scripts;
091        }
092        else if (importActionIds.size() == 1)
093        {
094            for (String id : importActionIds)
095            {
096                ClientSideElement importAction = _importModelActionEP.getExtension(id);
097                return importAction.getScripts(ignoreRights, contextParameters);
098            }
099        }
100        
101        _resolveImportMenuItems();
102        
103        List<ScriptFile> scriptFiles = new ArrayList<>();
104        List<ScriptFile> cssFiles = new ArrayList<>();
105        Map<String, Object> parameters = new HashMap<>();
106        
107        for (Script script : super.getScripts(ignoreRights, contextParameters))
108        {
109            scriptFiles.addAll(script.getScriptFiles());
110            cssFiles.addAll(script.getCSSFiles());
111            parameters.putAll(script.getParameters());
112        }
113        
114        for (String id : importActionIds)
115        {
116            ClientSideElement importAction = _importModelActionEP.getExtension(id);
117            for (Script script : importAction.getScripts(ignoreRights, contextParameters))
118            {
119                scriptFiles.addAll(script.getScriptFiles());
120                cssFiles.addAll(script.getCSSFiles());
121                parameters.putAll(script.getParameters());
122            }
123        }
124        
125        scripts.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
126        return scripts;
127    }
128    
129}