001/*
002 *  Copyright 2025 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.disableconditions;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.ametys.runtime.model.DefinitionContext;
022import org.ametys.runtime.model.Model;
023import org.ametys.runtime.model.ModelItem;
024
025/**
026 * Interface for a disable condition
027 */
028public interface DisableCondition
029{
030    /** The pefix to use for external conditions' identifier */
031    public static final String EXTERNAL_CONDITION_ID_PREFIX = "__external_";
032    
033    /**
034     * The available operators
035     */
036    public enum OPERATOR
037    {
038        /** Equals */
039        EQ,
040        /** Non equals */
041        NEQ,
042        /** Greater than */
043        GT,
044        /** Greater or equals */
045        GEQ,
046        /** Less or equals */ 
047        LEQ,
048        /** Less than */
049        LT
050    }
051    
052    /**
053     * Get the id
054     * @return the condition identifier
055     */
056    public String getId();
057    
058    /**
059     * Get the name
060     * @return the condition name
061     */
062    public String getName();
063
064    /**
065     * Get the operator
066     * @return The comparison operator
067     */
068    public OPERATOR getOperator();
069
070    /**
071     * Get the value
072     * @return The value to compare to
073     */
074    public String getValue();
075    
076    /**
077     * Check if the current disable condition is external
078     * An external condition can not be computed only from the object's values
079     * @param context the definition context
080     * @return <code>true</code> if the disable condition is external, <code>false</code> otherwise
081     */
082    public default boolean isExternal(DefinitionContext context)
083    {
084        return false;
085    }
086    
087    /**
088     * Used to do more initialization, checks, ... needing model items of distant objects
089     * Called by {@link Model} parsing when the model items of all models have been initialized.
090     * @param model the model containing the definition
091     * @param definition the model item defining this disable condition
092     * @throws Exception if an error occurs or if an additional check fails.
093     */
094    public void init(Model model, ModelItem definition) throws Exception;
095    
096    /**
097     * Evaluate the current {@link DisableCondition} against the given values
098     * @param definition the definition of the evaluated data
099     * @param dataPath the path of the evaluated data. Needed to get the value to compare as condition ids are relative to this one
100     * @param oldDataPath the old path of the evaluated data. Needed to get stored value if the data has been moved
101     * @param values values to check conditions on
102     * @param object the object holding the data to evaluate and the condition value
103     * @param contextualParameters the contextual parameters
104     * @param <T> Type of object holding the data to evaluate
105     * @return <code>true</code> if the disable condition is <code>true</code>, <code>false</code> otherwise
106     */
107    public <T> boolean evaluate(ModelItem definition, String dataPath, Optional<String> oldDataPath, Map<String, Object> values, Optional<T> object, Map<String, Object> contextualParameters);
108}