001/*
002 *  Copyright 2018 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.frontoffice.search.metamodel;
017
018import java.util.Map;
019import java.util.Optional;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.model.Enumerator;
028
029/**
030 * Enumerator for available and selectable {@link Returnable}s 
031 */
032public class ReturnableEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable
033{
034    /** The extension point for {@link Returnable}s */
035    protected ReturnableExtensionPoint _returnableEP;
036    
037    @Override
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _returnableEP = (ReturnableExtensionPoint) manager.lookup(ReturnableExtensionPoint.ROLE);
041    }
042
043    @Override
044    public I18nizableText getEntry(String value) throws Exception
045    {
046        return Optional.ofNullable(_returnableEP.getExtension(value))
047                .map(Returnable::getLabel)
048                .orElse(null/* return null if not found */);
049    }
050
051    @Override
052    public Map<String, I18nizableText> getTypedEntries() throws Exception
053    {
054        return _returnableEP.getExtensionsIds()
055                .stream()
056                .map(_returnableEP::getExtension)
057                .collect(Collectors.toMap(Returnable::getId, Returnable::getLabel));
058    }
059    
060    @Override
061    public Map<Object, I18nizableText> getEntries() throws Exception
062    {
063        return getTypedEntries().entrySet()
064                .stream()
065                .collect(Collectors.toMap(e -> (Object) e.getKey(), Map.Entry::getValue));
066    }
067    
068    @Override
069    public Map<String, Object> getConfiguration()
070    {
071        return Enumerator.super.getConfiguration();
072    }
073}