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.content;
017
018import java.util.UUID;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022
023import org.ametys.cms.data.holder.DataHolderStaticRelativeDisableConditions;
024import org.ametys.runtime.model.disableconditions.DisableCondition;
025import org.ametys.runtime.model.disableconditions.DisableConditions;
026
027/**
028 * {@link DisableConditions} for contents
029 */
030public class ContentStaticRelativeDisableConditions extends DataHolderStaticRelativeDisableConditions
031{
032    @Override
033    protected String configureDisableCondition(Configuration conditionConfiguration) throws ConfigurationException
034    {
035        String className = conditionConfiguration.getAttribute("class", null);
036        if (className != null)
037        {
038            final String conditionRole = className + "$" + UUID.randomUUID().toString();
039            try
040            {
041                @SuppressWarnings("unchecked")
042                Class<? extends DisableCondition> conditionClass = (Class<DisableCondition>) Class.forName(className);
043                addDisableConditionComponent(conditionRole, conditionClass, conditionConfiguration);
044                return conditionRole;
045            }
046            catch (ClassNotFoundException e)
047            {
048                throw new ConfigurationException("Unable to configure a condition with class name '" + className + "'. This class has not been found", conditionConfiguration, e);
049            }
050        }
051        else
052        {
053            try
054            {
055                return super.configureDisableCondition(conditionConfiguration);
056            }
057            catch (ConfigurationException e)
058            {
059                throw new ConfigurationException("Unable to configure a condition with no specified class name nor id of relative item", conditionConfiguration);
060            }
061        }
062    }
063}