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 synchronized 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            }
074            
075            _importMenuItemsInitialized = true;
076        }
077    }
078    
079    @Override
080    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
081    {
082        List<Script> scripts = new ArrayList<>();
083        
084        Set<String> importActionIds = _importSkinActionEP.getExtensionsIds();
085        
086        if (importActionIds.size() == 1)
087        {
088            for (String id : importActionIds)
089            {
090                ClientSideElement importAction = _importSkinActionEP.getExtension(id);
091                return importAction.getScripts(contextParameters);
092            }
093        }
094        else
095        {
096            try
097            {
098                _resolveImportMenuItems();
099            }
100            catch (Exception e)
101            {
102                throw new IllegalStateException("Unable to lookup client side element local components", e);
103            }
104        }
105        
106        
107        List<ScriptFile> scriptFiles = new ArrayList<>();
108        List<ScriptFile> cssFiles = new ArrayList<>();
109        Map<String, Object> parameters = new HashMap<>();
110        for (Script script : super.getScripts(ignoreRights, contextParameters))
111        {
112            scriptFiles.addAll(script.getScriptFiles());        
113            cssFiles.addAll(script.getCSSFiles());
114            parameters.putAll(script.getParameters());
115        }
116        
117        if (importActionIds.size() == 0)
118        {
119            // Non button
120            return scripts;
121        }
122        
123        for (String id : importActionIds)
124        {
125            ClientSideElement importAction = _importSkinActionEP.getExtension(id);
126            for (Script script : importAction.getScripts(ignoreRights, contextParameters))
127            {
128                scriptFiles.addAll(script.getScriptFiles());
129                cssFiles.addAll(script.getCSSFiles());
130                parameters.putAll(script.getParameters());
131            }
132        }
133        
134        scripts.add(new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters));
135        return scripts;
136    }
137    
138}