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.Map;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.solr.common.SolrInputDocument;
023
024import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
025import org.ametys.cms.data.type.indexing.IndexableDataContext;
026import org.ametys.plugins.repository.data.holder.impl.DataHolderHelper;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.ElementDefinition;
029import org.ametys.runtime.model.Enumerator;
030import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
031
032/**
033 * Property referencing an {@link ElementDefinition}
034 * @param <T> Type of the element value
035 * @param <X> type of ametys object supported by this property
036 */
037public class ElementRefProperty<T, X extends ModelAwareDataAwareAmetysObject> extends AbstractProperty<T, X>
038{
039    private String _elementPath;
040    
041    @Override
042    public void configure(Configuration configuration) throws ConfigurationException
043    {
044        super.configure(configuration);
045        configureReferencedElementPath(configuration);
046    }
047    
048    @Override
049    protected I18nizableText _parseI18nizableText(Configuration config, String pluginName, String name) throws ConfigurationException
050    {
051        // Do no use name as default value, to use the referenced element's one if the the concerned text is not filled
052        return I18nizableText.parseI18nizableText(config.getChild(name), "plugin." + pluginName, (I18nizableText) null);
053    }
054    
055    /**
056     * Configures the path of the referenced element
057     * @param configuration the property configuration
058     * @throws ConfigurationException if an error occurred
059     */
060    protected void configureReferencedElementPath(Configuration configuration) throws ConfigurationException
061    {
062        _elementPath = configuration.getAttribute("path");
063    }
064    
065    @Override
066    public String getName()
067    {
068        String name = super.getName();
069        if (name == null)
070        {
071            // If no name has been configured, use the referenced element's one
072            name = getElementDefinition().getName();
073        }
074        
075        return name;
076    }
077    
078    @Override
079    public I18nizableText getLabel()
080    {
081        I18nizableText label = super.getLabel();
082        if (label == null)
083        {
084            label = getElementDefinition().getLabel();
085        }
086        
087        return label;
088    }
089    
090    @Override
091    public I18nizableText getDescription()
092    {
093        I18nizableText description = super.getDescription();
094        if (description == null)
095        {
096            description = getElementDefinition().getDescription();
097        }
098        
099        return description;
100    }
101    
102    @Override
103    public Enumerator<T> getEnumerator()
104    {
105        Enumerator<T> enumerator = super .getEnumerator();
106        if (enumerator == null)
107        {
108            enumerator = getElementDefinition().getEnumerator();
109        }
110        
111        return enumerator;
112    }
113    
114    /**
115     * Retrieves the referenced element definition
116     * @return the referenced element definition
117     */
118    @SuppressWarnings("unchecked")
119    public ElementDefinition<T> getElementDefinition()
120    {
121        return (ElementDefinition<T>) getModel().getModelItem(getElementPath());
122    }
123    
124    public Object getValue(X ametysObject)
125    {
126        return ametysObject.getValue(getElementPath(), true);
127    }
128    
129    public void indexValue(SolrInputDocument document, X ametysObject, IndexableDataContext context)
130    {
131        super.indexValue(document, ametysObject, context.cloneContext().withModelItem(getElementDefinition()));
132    }
133    
134    @Override
135    public boolean isMultiple()
136    {
137        return DataHolderHelper.isMultiple(getModel(), getElementPath());
138    }
139    
140    /**
141     * Retrieves the path of the referenced element definition
142     * @return the path of the referenced element definition
143     */
144    public String getElementPath()
145    {
146        return _elementPath;
147    }
148    
149    /**
150     * Set the path of the referenced element definition
151     * @param elementPath the path to set
152     */
153    public void setElementPath(String elementPath)
154    {
155        _elementPath = elementPath;
156    }
157    
158    @Override
159    protected String _getTypeId()
160    {
161        return getElementDefinition().getType().getId();
162    }
163    
164    public Enumerator getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
165    {
166        return getElementDefinition().getCriterionEnumerator(configuration, enumeratorManager);
167    }
168    
169    public String getCriterionWidget()
170    {
171        return getElementDefinition().getCriterionWidget();
172    }
173    
174    public Map<String, I18nizableText> getCriterionWidgetParameters(Configuration configuration)
175    {
176        return getElementDefinition().getCriterionWidgetParameters(configuration);
177    }
178}