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