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