001/*
002 *  Copyright 2017 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.workspaces.project.modules;
017
018import java.util.LinkedHashSet;
019import java.util.List;
020import java.util.Set;
021import java.util.TreeSet;
022import java.util.stream.Collectors;
023
024import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
025
026/**
027 * Extension point for Workspace Modules Managers
028 */
029public class WorkspaceModuleExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<WorkspaceModule>
030{
031    /** Avalon Role */
032    public static final String ROLE = WorkspaceModuleExtensionPoint.class.getName();
033    
034    private Set<String> _sortedIds;
035
036    /**
037     * Get a workspace module 
038     * @param moduleId The id of the module
039     * @param <M> The module type
040     * @return The workspace module
041     */
042    @SuppressWarnings("unchecked")
043    public <M extends WorkspaceModule> M getModule(String moduleId)
044    {
045        return (M) getExtension(moduleId);
046    }
047    
048    /**
049     * Get the list of available modules
050     * @return The modules
051     */
052    public List<WorkspaceModule> getModules()
053    {
054        return getExtensionsIds().stream().map(id -> getExtension(id)).collect(Collectors.toList());
055    }
056    
057    /**
058     * Get a workspace module by its name
059     * @param moduleName The module name
060     * @return The workspace module or null if not found
061     */
062    public WorkspaceModule getModuleByName(String moduleName)
063    {
064        List<WorkspaceModule> modules = getModules();
065        for (WorkspaceModule workspaceModule : modules)
066        {
067            if (workspaceModule.getModuleName().equals(moduleName))
068            {
069                return workspaceModule;
070            }
071        }
072        
073        return null;
074    }
075    
076    @Override
077    public void initializeExtensions() throws Exception
078    {
079        super.initializeExtensions();
080        
081        Set<WorkspaceModule> sortedModules = new TreeSet<>();
082        sortedModules.addAll(getModules());
083        _sortedIds = sortedModules.stream().map(WorkspaceModule::getId).collect(Collectors.toCollection(LinkedHashSet::new));
084    }
085    
086    @Override
087    public Set<String> getExtensionsIds()
088    {
089        if (_sortedIds != null)
090        {
091            return _sortedIds;
092        }
093        else
094        {
095            return super.getExtensionsIds();
096        }
097    }
098}