001/*
002 *  Copyright 2016 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.administration.welcome;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.HashMap;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025import java.util.stream.Collectors;
026
027import org.apache.avalon.framework.configuration.Configurable;
028import org.apache.avalon.framework.configuration.Configuration;
029import org.apache.avalon.framework.configuration.ConfigurationException;
030
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.plugin.component.PluginAware;
033
034/**
035 * Absract {@link WelcomeStep} which is {@link Configurable}
036 */
037public abstract class AbstractWelcomeStep implements WelcomeStep, Configurable, PluginAware
038{
039    /** The name of the plugin that has declared this component */
040    protected String _pluginName;
041    /** The order of the step */
042    protected int _order;
043    /** The HTML title of the step */
044    protected I18nizableText _title;
045    /** The HTML description of the step */
046    protected I18nizableText _description;
047    /** The JS actions */
048    protected String[] _actions;
049    /** The images */
050    protected String[] _images;
051    /** The JS target ids to listen for messages */
052    protected Map<String, Set<String>> _targetIds;
053    
054    @Override
055    public void setPluginInfo(String pluginName, String featureName, String id)
056    {
057        _pluginName = pluginName;
058    }
059
060    @Override
061    public void configure(Configuration configuration) throws ConfigurationException
062    {
063        _order = configuration.getChild("order").getValueAsInteger();
064        _title = I18nizableText.parseI18nizableText(configuration.getChild("title"), "plugin." + _pluginName);
065        _description = I18nizableText.parseI18nizableText(configuration.getChild("description"), "plugin." + _pluginName);
066        
067        Configuration[] actions = configuration.getChildren("action");
068        List<String> actionsList = new ArrayList<>(Arrays.asList(actions)).stream().map(conf -> conf.getValue("Ext.emptyFn()")).collect(Collectors.toList());
069        _actions = actionsList.toArray(new String[actionsList.size()]);
070        
071        Configuration[] images = configuration.getChildren("image");
072        List<String> imagesList = new ArrayList<>(Arrays.asList(images)).stream().map(conf -> conf.getValue("")).collect(Collectors.toList());
073        _images = imagesList.toArray(new String[imagesList.size()]);
074        
075        _targetIds = _getTargetIds(configuration);
076    }
077    
078    @Override
079    public I18nizableText getTitle()
080    {
081        return _title;
082    }
083    
084    @Override
085    public I18nizableText getDescription()
086    {
087        return _description;
088    }
089    
090    @Override
091    public String[] getActions()
092    {
093        return _actions;
094    }
095    
096    @Override
097    public String[] getImages()
098    {
099        return _images;
100    }
101    
102    @Override
103    public Map<String, Set<String>> getListenedTargetIds()
104    {
105        return _targetIds;
106    }
107    
108    @Override
109    public int getOrder()
110    {
111        return _order;
112    }
113    
114    private Map<String, Set<String>> _getTargetIds(Configuration configuration) throws ConfigurationException
115    {
116        Map<String, Set<String>> targetIds = new HashMap<>();
117        
118        Configuration[] confsOnMessage = configuration.getChildren("onMessage");
119        for (Configuration conf : confsOnMessage)
120        {
121            String messageType = conf.getAttribute("type");
122            if (targetIds.containsKey(messageType))
123            {
124                targetIds.get(messageType).add(conf.getValue());
125            }
126            else
127            {
128                Set<String> set = new HashSet<>();
129                set.add(conf.getValue());
130                targetIds.put(messageType, set);
131            }
132        }
133        
134        return targetIds;
135    }
136}