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