001/*
002 *  Copyright 2022 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.cms.content.properties;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.cms.model.properties.AbstractElementsReferencingProperty;
026import org.ametys.cms.model.properties.Property;
027import org.ametys.cms.repository.Content;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.model.Enumerator;
030
031/**
032 * {@link Property} referencing multiple values in one property.
033 * @param <T> Type of the element value
034 */
035public abstract class AbstractMultiValuesProperty<T> extends AbstractElementsReferencingProperty<T, Content>
036{
037    /** The attribute paths. */
038    protected List<String> _references;
039    
040    @Override
041    protected void configureReferences(Configuration configuration) throws ConfigurationException
042    {
043        _references = new ArrayList<>();
044        for (Configuration attributeConf : configuration.getChildren("attribute"))
045        {
046            _references.add(attributeConf.getAttribute("path"));
047        }
048        
049        if (_references.isEmpty())
050        {
051            throw new ConfigurationException("The property '" + getName() + "' has to reference at least one attribute", configuration);
052        }
053    }
054    
055    @Override
056    public List<String> getReferences()
057    {
058        return _references;
059    }
060    
061    @Override
062    public boolean isMultiple()
063    {
064        return true;
065    }
066    
067    @Override
068    public Enumerator<T> getEnumerator()
069    {
070        // There is no sense to retrieve the enumerator of the first referenced attribute -> it could lead to errors
071        return null;
072    }
073    
074    @Override
075    public String getCriterionWidget()
076    {
077        // There is no sense to retrieve the enumerator of the first referenced attribute -> it could lead to errors
078        return null;
079    }
080    
081    @Override
082    public Map<String, I18nizableText> getCriterionWidgetParameters(Configuration configuration)
083    {
084        // There is no sense to retrieve the enumerator of the first referenced attribute -> it could lead to errors
085        return null;
086    }
087}