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.cms.data.holder;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022import java.util.UUID;
023
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.cms.search.model.SystemProperty;
027import org.ametys.plugins.repository.data.holder.DataHolder;
028import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
029import org.ametys.runtime.model.DefinitionContext;
030import org.ametys.runtime.model.Model;
031import org.ametys.runtime.model.ModelHelper;
032import org.ametys.runtime.model.ModelItem;
033import org.ametys.runtime.model.ModelItemAccessor;
034import org.ametys.runtime.model.disableconditions.AbstractRelativeDisableCondition;
035import org.ametys.runtime.model.disableconditions.DisableCondition;
036import org.ametys.runtime.model.exception.BadItemTypeException;
037import org.ametys.runtime.model.exception.UndefinedItemPathException;
038
039/**
040 * {@link DisableCondition} for model aware {@link DataHolder}
041 */
042public class DataHolderRelativeDisableCondition extends AbstractRelativeDisableCondition
043{
044    /** The data holder relative disable condition helper */
045    protected DataHolderRelativeDisableConditionsHelper _disableConditionsHelper;
046    
047    /**
048     * Creates a data holder disable condition
049     * @param name The condition name
050     * @param operator comparison operator of the condition ('eq'...)
051     * @param value value to compare to
052     * @param disableConditionsHelper the data holder relative disable condition helper
053     */
054    public DataHolderRelativeDisableCondition(String name, OPERATOR operator, String value, DataHolderRelativeDisableConditionsHelper disableConditionsHelper)
055    {
056        _id = UUID.randomUUID().toString();
057        _name = name;
058        _operator = operator;
059        _value = value;
060        _disableConditionsHelper = disableConditionsHelper;
061    }
062    
063    @Override
064    protected List<ModelItem> getAllModelItemsInPath(Model model, ModelItem definition) throws UndefinedItemPathException, IllegalArgumentException
065    {
066        String conditionPath = ModelHelper.getDisableConditionAbsolutePath(this, definition.getPath());
067        SystemProperty systemProperty = _disableConditionsHelper.getSystemProperty(conditionPath);
068        return systemProperty != null
069                ? _disableConditionsHelper.getAllModelItemsInPathToSystemProperty(this, conditionPath, model, definition, systemProperty)
070                : super.getAllModelItemsInPath(model, definition);
071    }
072
073    @Override
074    protected void checkModelItemPathSegment(Model model, ModelItem definition, ModelItem segmentModelItem, int segmentIndex, int conditionPathSize) throws ConfigurationException
075    {
076        super.checkModelItemPathSegment(model, definition, segmentModelItem, segmentIndex, conditionPathSize);
077        _disableConditionsHelper.checkModelItemPathSegment(this, model, definition, segmentModelItem, segmentIndex, conditionPathSize);
078    }
079    
080    @Override
081    protected <T> RelativeDefinitionAndValue _getConditionDefinitionAndValue(ModelItem definition, String dataPath, Optional<String> oldDataPath, Map<String, Object> values, Optional<T> dataHolder, Map<String, Object> contextualParameters)
082    {
083        dataHolder.filter(ModelAwareDataHolder.class::isInstance)
084                  .map(ModelAwareDataHolder.class::cast)
085                  .ifPresent(dH -> contextualParameters.putAll(_disableConditionsHelper.getAdditionalContextualParameters(this, oldDataPath, dH)));
086
087        return super._getConditionDefinitionAndValue(definition, dataPath, oldDataPath, values, dataHolder, contextualParameters);
088    }
089    
090    @Override
091    protected <T> Collection<? extends ModelItemAccessor> getModelItemAccessors(ModelItem definition, Optional<T> object)
092    {
093        return _disableConditionsHelper.getModelItemAccessors(object)
094                                       .orElseGet(() -> super.getModelItemAccessors(definition, object));
095    }
096    
097    @Override
098    protected ModelItem getModelItem(Collection<? extends ModelItemAccessor> modelItemAccessors, String conditionDataPath, Map<String, Object> contextualParameters) throws UndefinedItemPathException
099    {
100        SystemProperty systemProperty = _disableConditionsHelper.getSystemProperty(conditionDataPath);
101        return systemProperty != null
102                ? systemProperty
103                : super.getModelItem(modelItemAccessors, conditionDataPath, contextualParameters);
104    }
105    
106    /**
107     * {@inheritDoc}
108     * @throws BadItemTypeException if a value does not correspond to the model of given object
109     */
110    @Override
111    protected boolean containsRelativeValue(Collection<? extends ModelItemAccessor> modelItemAccessors, String conditionDataPath, Map<String, Object> values, Map<String, Object> contextualParameters) throws BadItemTypeException
112    {
113        return _disableConditionsHelper.containsValue(modelItemAccessors, conditionDataPath, values, contextualParameters);
114    }
115    
116    /**
117     * {@inheritDoc}
118     * @throws BadItemTypeException if a value does not correspond to the model of given object
119     */
120    @Override
121    protected Object getRelativeValueFromMap(Collection<? extends ModelItemAccessor> modelItemAccessors, String conditionDataPath, Map<String, Object> values, Map<String, Object> contextualParameters) throws BadItemTypeException
122    {
123        return _disableConditionsHelper.getValueFromMap(modelItemAccessors, conditionDataPath, values, contextualParameters);
124    }
125
126    @Override
127    protected <T> Object getStoredRelativeValue(String conditionDataPath, Optional<String> oldConditionDataPath, T dataHolder, Map<String, Object> contextualParameters)
128    {
129        String dataPath = oldConditionDataPath.orElse(conditionDataPath);
130        return _disableConditionsHelper.doesEvaluateConditionForNewRepeaterEntry((ModelAwareDataHolder) dataHolder, dataPath)
131            ? null // The condition is external, so the relative item will not be valued
132            : ((ModelAwareDataHolder) dataHolder).getValue(dataPath, true);
133    }
134    
135    public boolean isExternal(DefinitionContext context)
136    {
137        return _disableConditionsHelper.isExternal(this, context);
138    }
139}