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.Comparator;
019import java.util.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.ametys.runtime.plugin.ExtensionPoint;
024import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
025
026/**
027 * {@link ExtensionPoint} handling the steps of the Admin welcome feature.
028 */
029public class WelcomeStepExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<WelcomeStep>
030{
031    /** Avalon Role */
032    public static final String ROLE = WelcomeStepExtensionPoint.class.getName();
033    
034    /**
035     * Get all the steps (ordered).
036     * @return The ordered list of the steps
037     */
038    public List<WelcomeStep> getSteps()
039    {
040        return getExtensionsIds().stream()
041                                 .map(this::getExtension) // the set of WelcomeStep
042                                 .sorted(Comparator.comparing(WelcomeStep::getOrder)) // ordered by their order
043                                 .collect(Collectors.toList());
044    }
045    
046    /**
047     * Get all the steps (ordered) as maps.
048     * @return The ordered list of the steps, as maps
049     */
050    public List<Map<String, Object>> getStepsAsMap()
051    {
052        return getExtensionsIds().stream()
053                                 .map(this::getExtension) // the set of WelcomeStep
054                                 .sorted(Comparator.comparing(WelcomeStep::getOrder)) // ordered by their order
055                                 .map(WelcomeStep::toMap) // given to the output as maps
056                                 .collect(Collectors.toList());
057    }
058    
059    /**
060     * Gets the index of the current step, i.e. the first step which is not performed yet.
061     * If all steps are performed, return -1
062     * @return The index of the current step.
063     */
064    public int getCurrentStep()
065    {
066        getLogger().debug("Getting current step..");
067        
068        long t = System.currentTimeMillis();
069
070        List<WelcomeStep> orderedSteps = getSteps();
071        for (int i = 0; i < orderedSteps.size(); i++)
072        {
073            long t0 = System.currentTimeMillis();
074            
075            if (!orderedSteps.get(i).isPerformed())
076            {
077                long t1 = System.currentTimeMillis();
078                getLogger().debug("Step " + i + " tested in " + (t1 - t0) + "ms");
079                getLogger().info("Current step is " + i + ". Tested in " + (t1 - t) + "ms");
080                return i;
081            }
082            
083            long t1 = System.currentTimeMillis();
084            getLogger().debug("Step " + i + " tested in " + (t1 - t0) + "ms");
085        }
086        
087        long t1 = System.currentTimeMillis();
088        getLogger().info("Current step is -1. Tested in " + (t1 - t) + "ms");
089        return -1;
090    }
091}