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 */
033public abstract class AbstractMultiValuesProperty<T> extends AbstractElementsReferencingProperty<T, Content>
034{
035    /** The attribute paths. */
036    protected List<String> _references;
037    
038    @Override
039    protected void configureReferences(Configuration configuration) throws ConfigurationException
040    {
041        _references = new ArrayList<>();
042        for (Configuration attributeConf : configuration.getChildren("attribute"))
043        {
044            _references.add(attributeConf.getAttribute("path"));
045        }
046        
047        if (_references.isEmpty())
048        {
049            throw new ConfigurationException("The property '" + getName() + "' has to reference at least one attribute", configuration);
050        }
051    }
052    
053    @Override
054    public List<String> getReferences()
055    {
056        return _references;
057    }
058    
059    @Override
060    public boolean isMultiple()
061    {
062        return true;
063    }
064    
065    @Override
066    public Enumerator<T> getEnumerator()
067    {
068        // There is no sense to retrieve the enumerator of the first referenced attribute -> it could lead to errors
069        return null;
070    }
071}