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.model.properties;
017
018import java.util.List;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022
023import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
024import org.ametys.plugins.repository.data.holder.impl.DataHolderHelper;
025import org.ametys.runtime.model.ElementDefinition;
026import org.ametys.runtime.model.Enumerator;
027
028/**
029 * Property referencing an {@link ElementDefinition}
030 * @param <T> Type of the element value
031 * @param <X> Type of ametys object supported by this property
032 */
033public class ElementReferencingProperty<T, X extends ModelAwareDataAwareAmetysObject> extends AbstractElementsReferencingProperty<T, X>
034{
035    /** The path to the referenced element */
036    protected String _reference;
037    
038    @Override
039    protected void configureReferences(Configuration configuration) throws ConfigurationException
040    {
041        _reference = configuration.getAttribute("path");
042    }
043    
044    @Override
045    public List<String> getReferences()
046    {
047        return List.of(_reference);
048    }
049    
050    @Override
051    public Enumerator<T> getEnumerator()
052    {
053        Enumerator<T> enumerator = super .getEnumerator();
054        if (enumerator == null)
055        {
056            enumerator = _getReferenceDefinition(_reference).getEnumerator();
057        }
058        
059        return enumerator;
060    }
061    
062    public Object getValue(X ametysObject)
063    {
064        return ametysObject.getValue(_reference, true);
065    }
066    
067    @Override
068    public boolean isMultiple()
069    {
070        return DataHolderHelper.isMultiple(getModel(), _reference);
071    }
072    
073    @Override
074    protected String getTypeId()
075    {
076        return _getReferenceDefinition(_reference).getType().getId();
077    }
078}