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