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