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.odf.content.code;
017
018import java.util.Optional;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.model.properties.AbstractIndexableStaticProperty;
024import org.ametys.cms.model.properties.Property;
025import org.ametys.cms.repository.Content;
026import org.ametys.odf.ProgramItem;
027import org.ametys.runtime.config.Config;
028import org.ametys.runtime.model.type.ModelItemTypeConstants;
029
030/**
031 * {@link Property} for the computed display code of a {@link ProgramItem}.
032 */
033public class DisplayCodeProperty extends AbstractIndexableStaticProperty<String, String, Content>
034{
035    /** The property name for display code */
036    public static final String PROPERTY_NAME = "displayCode";
037    
038    private static final String __CONFIG_PARAM_DISPLAY_CODE_PROVIDER = "odf.display.code.provider";
039    
040    private DisplayCodeProviderExtensionPoint _displayCodeEP;
041
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _displayCodeEP = (DisplayCodeProviderExtensionPoint) smanager.lookup(DisplayCodeProviderExtensionPoint.ROLE);
047    }
048    
049    public Object getValue(Content content)
050    {
051        String dcpId = Config.getInstance().getValue(__CONFIG_PARAM_DISPLAY_CODE_PROVIDER, true, AmetysDisplayCodeProvider.ID);
052        
053        DisplayCodeProvider dcProvider = _displayCodeEP.getExtension(dcpId);
054        if (dcProvider.supports(content))
055        {
056            Optional<String> displayCode = dcProvider.getDisplayCode(content);
057            if (displayCode.isPresent())
058            {
059                return displayCode.get();
060            }
061        }
062        
063        // Return value of default provider
064        DisplayCodeProvider defaultProvider = _displayCodeEP.getExtension(AmetysDisplayCodeProvider.ID);
065        return defaultProvider.getDisplayCode(content).orElse(null);
066    }
067
068    @Override
069    protected String getTypeId()
070    {
071        return ModelItemTypeConstants.STRING_TYPE_ID;
072    }
073}