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.web.skin;
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 ImportSkinActionExtensionPoint} to create a button for import skin. 
032 */
033public class ImportSkinClientSideElement extends SimpleMenu
034{
035    private ImportSkinActionExtensionPoint _importSkinActionEP;
036
037    private boolean _importMenuItemsInitialized;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _importSkinActionEP = (ImportSkinActionExtensionPoint) smanager.lookup(ImportSkinActionExtensionPoint.ROLE);
044    }
045    
046    private void _resolveImportMenuItems ()
047    {
048        if (!_importMenuItemsInitialized)
049        {
050            List<UnresolvedItem> unresolvedImportMenuItems = new ArrayList<>();
051            
052            Set<String> importActionIds = _importSkinActionEP.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 = _importSkinActionEP.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 = _importSkinActionEP.getExtensionsIds();
086        
087        if (importActionIds.size() == 1)
088        {
089            for (String id : importActionIds)
090            {
091                ClientSideElement importAction = _importSkinActionEP.getExtension(id);
092                return importAction.getScripts(contextParameters);
093            }
094        }
095        else
096        {
097            try
098            {
099                _resolveImportMenuItems();
100            }
101            catch (Exception e)
102            {
103                throw new IllegalStateException("Unable to lookup client side element local components", e);
104            }
105        }
106        
107        
108        List<ScriptFile> scriptFiles = new ArrayList<>();
109        List<ScriptFile> cssFiles = new ArrayList<>();
110        Map<String, Object> parameters = new HashMap<>();
111        for (Script script : super.getScripts(ignoreRights, contextParameters))
112        {
113            scriptFiles.addAll(script.getScriptFiles());        
114            cssFiles.addAll(script.getCSSFiles());
115            parameters.putAll(script.getParameters());
116        }
117        
118        if (importActionIds.size() == 0)
119        {
120            // Non button
121            return scripts;
122        }
123        
124        for (String id : importActionIds)
125        {
126            ClientSideElement importAction = _importSkinActionEP.getExtension(id);
127            for (Script script : importAction.getScripts(ignoreRights, contextParameters))
128            {
129                scriptFiles.addAll(script.getScriptFiles());
130                cssFiles.addAll(script.getCSSFiles());
131                parameters.putAll(script.getParameters());
132            }
133        }
134        
135        scripts.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
136        return scripts;
137    }
138    
139}