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.configuration.Configurable; 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028 029import org.ametys.runtime.i18n.I18nizableText; 030import org.ametys.runtime.model.Enumerator; 031 032/** 033 * Enumerator for available and selectable {@link Returnable}s 034 */ 035public class ReturnableEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable, Configurable 036{ 037 /** The extension point for {@link Returnable}s */ 038 protected ReturnableExtensionPoint _returnableEP; 039 040 /** True to exclude private returnable */ 041 protected boolean _excludePrivate; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 _returnableEP = (ReturnableExtensionPoint) manager.lookup(ReturnableExtensionPoint.ROLE); 047 } 048 049 public void configure(Configuration configuration) throws ConfigurationException 050 { 051 Configuration privateConf = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("excludePrivate"); 052 _excludePrivate = privateConf.getValueAsBoolean(false); 053 } 054 055 @Override 056 public I18nizableText getEntry(String value) throws Exception 057 { 058 return Optional.ofNullable(_returnableEP.getExtension(value)) 059 .map(Returnable::getLabel) 060 .orElse(null/* return null if not found */); 061 } 062 063 @Override 064 public Map<String, I18nizableText> getTypedEntries() throws Exception 065 { 066 return _returnableEP.getExtensionsIds() 067 .stream() 068 .map(_returnableEP::getExtension) 069 .filter(this::_filterPrivate) 070 .collect(Collectors.toMap(Returnable::getId, Returnable::getLabel)); 071 } 072 073 private boolean _filterPrivate(Returnable returnable) 074 { 075 return !_excludePrivate || !returnable.isPrivate(); 076 } 077 078 @Override 079 public Map<Object, I18nizableText> getEntries() throws Exception 080 { 081 return getTypedEntries().entrySet() 082 .stream() 083 .collect(Collectors.toMap(e -> (Object) e.getKey(), Map.Entry::getValue)); 084 } 085 086 @Override 087 public Map<String, Object> getConfiguration() 088 { 089 return Enumerator.super.getConfiguration(); 090 } 091}