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.runtime.model;
017
018import java.util.List;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.commons.lang3.tuple.Pair;
023
024import org.ametys.runtime.model.type.ElementType;
025import org.ametys.runtime.parameter.Validator;
026import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
027
028/**
029 * Interface of the definition of a single model item (parameter, attribute)
030 * @param <T> Type of the element value
031 */
032public interface ElementDefinition<T> extends ModelItem
033{
034    /** config type for default values */
035    public static final String CONFIG_DEFAULT_VALUE_TYPE = "config";
036    
037    @Override
038    public ElementType<T> getType();
039    
040    /**
041     * Retrieves the enumerator.
042     * @return the enumerator or <code>null</code> if none is defined.
043     */
044    public Enumerator<T> getEnumerator();
045    
046    /**
047     * Set the enumerator.
048     * @param enumerator the enumerator.
049     */
050    public void setEnumerator(Enumerator<T> enumerator);
051    
052    /**
053     * Retrieves the custom enumerator's class name
054     * @return the custom enumerator's class name
055     */
056    public String getCustomEnumerator();
057    
058    /**
059     * Set the custom enumerator's class name
060     * @param customEnumerator the custom enumerator's class name
061     */
062    public void setCustomEnumerator(String customEnumerator);
063    
064    /**
065     * Retrieves the custom enumerator's configuration
066     * @return the custom enumerator's configuration
067     */
068    public Configuration getEnumeratorConfiguration();
069    
070    /**
071     * Set the custom enumerator's configuration
072     * @param enumeratorConfiguration the custom enumerator's configuration
073     */
074    public void setEnumeratorConfiguration(Configuration enumeratorConfiguration);
075    
076    /**
077     * Get the enumerator to use when rendering this element as a criterion
078     * @param configuration The enumerator configuration.
079     * @param enumeratorManager ComponentManager for the criterion's {@link Enumerator}
080     * @return The enumerator or null if the element'critérion is not enumerated.
081     * @throws ConfigurationException If an error occurs while initializing the enumerator
082     */
083    public default Enumerator getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
084    {
085        return getEnumerator();
086    }
087    
088    /**
089     * Retrieves the validator.
090     * @return the validator or <code>null</code> if none is defined.
091     */
092    public Validator getValidator();
093    
094    /**
095     * Set the validator.
096     * @param validator the validator.
097     */
098    public void setValidator(Validator validator);
099    
100    /**
101     * Retrieves the custom validator's class name
102     * @return the custom validator's class name
103     */
104    public String getCustomValidator();
105    
106    /**
107     * Set the custom validator's class name
108     * @param customValidator the custom validator's class name
109     */
110    public void setCustomValidator(String customValidator);
111    
112    /**
113     * Retrieves the custom validator's configuraiton
114     * @return the custom validator's configuration
115     */
116    public Configuration getValidatorConfiguration();
117    
118    /**
119     * Set the custom validator's configuration
120     * @param validatorConfiguration the custom validator's configuration
121     */
122    public void setValidatorConfiguration(Configuration validatorConfiguration);
123    
124    /**
125     * Retrieves the default value, as an object corresponding to the definition's type and cardinality
126     * Retrieves <code>null</code> if no default value is defined for this definition
127     * @param <X> The type of the default value
128     * @return the default value.
129     */
130    public <X> X getDefaultValue();
131    
132    /**
133     * Set the parsed default values.
134     * If the definition is not multiple, the list should contain only one element
135     * A parsed default value is described by its type and an object depending on the type 
136     * @param parsedDefaultValues the parsed default values.
137     */
138    public void setParsedDefaultValues(List<Pair<String, Object>> parsedDefaultValues);
139    
140    /**
141     * Set a default value to the definition
142     * The default value is single, classic default value
143     * @param defaultValue the default value to set
144     */
145    public void setDefaultValue(T defaultValue);
146    
147    /**
148     * Retrieves the parsed default values
149     * @return the parsed default values
150     */
151    public List<Pair<String, Object>> _getParsedDefaultValues();
152    
153    /**
154     * Test if the element is multiple.
155     * @return <code>true</code> if the metadata is multiple.
156     */
157    public boolean isMultiple();
158    
159    /**
160     * Set the element multiple status.
161     * @param isMultiple the element multiple status.
162     */
163    public void setMultiple(boolean isMultiple);
164    
165    /**
166     * Determines if the definition is editable, i.e. if it can be part of a view used for edition
167     * @return <code>true</code> if the definition is editable, <code>false</code> otherwise
168     */
169    public default boolean isEditable()
170    {
171        // A definition is modifable by default
172        return true;
173    }
174}