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.ElementRefProperty;
025import org.ametys.cms.model.properties.Property;
026import org.ametys.cms.repository.Content;
027
028/**
029 * {@link Property} referencing multiple values in one property.
030 * @param <T> Type of the element value
031 */
032public abstract class AbstractMultiValuesProperty<T> extends ElementRefProperty<T, Content>
033{
034    /** The attribute paths. */
035    protected List<String> _attributePaths;
036    
037    @Override
038    protected void configureReferencedElementPath(Configuration configuration) throws ConfigurationException
039    {
040        _attributePaths = new ArrayList<>();
041        for (Configuration attributeConf : configuration.getChildren("attribute"))
042        {
043            _attributePaths.add(attributeConf.getAttribute("path"));
044        }
045        
046        if (!_attributePaths.isEmpty())
047        {
048            setElementPath(_attributePaths.get(0));
049        }
050        else
051        {
052            throw new ConfigurationException("The property '" + getName() + "' has to reference at least one attribute", configuration);
053            
054        }
055    }
056    
057    @Override
058    public boolean isMultiple()
059    {
060        return true;
061    }
062}