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.restriction;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.odf.EducationalPathHelper;
029import org.ametys.plugins.repository.model.RepeaterDefinition;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.runtime.model.Enumerator;
032import org.ametys.runtime.model.ModelItem;
033import org.ametys.runtime.plugin.component.DeferredServiceable;
034
035/**
036 * List the educational paths attributes
037 */
038public class ByEducationalPathAttributesEnumerator implements Enumerator<String>, Configurable, DeferredServiceable
039{
040    /** The contenttype to consider */
041    protected String _contentType;
042    /** The content type extension point */
043    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
044    /** The educational path helper */
045    protected EducationalPathHelper _educationalPathHelper;
046
047    public void configure(Configuration configuration) throws ConfigurationException
048    {
049        _contentType = configuration.getChild("content-type").getValue();
050        if (_contentTypeExtensionPoint != null // Avoid exception in safe mode
051                && !_contentTypeExtensionPoint.hasExtension(_contentType))
052        {
053            throw new ConfigurationException("The content type '" + _contentType + "' is not registered in the content type extension point.");
054        }
055    }
056    
057    public void deferredService(ServiceManager manager) throws ServiceException
058    {
059        if (manager.hasService(ContentTypeExtensionPoint.ROLE)) // Safe mode compatible
060        {
061            _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
062            _educationalPathHelper = (EducationalPathHelper) manager.lookup(EducationalPathHelper.ROLE);
063        }
064    }
065    
066    public Map<String, I18nizableText> getEntries() throws Exception
067    {
068        Map<String, I18nizableText> entries = new LinkedHashMap<>();
069        
070        if (_contentTypeExtensionPoint != null)
071        {
072            for (ModelItem modelItem : _contentTypeExtensionPoint.getExtension(_contentType).getModelItems())
073            {
074                if (modelItem instanceof RepeaterDefinition repeaterDef)
075                {
076                    if (_educationalPathHelper.isRepeaterWithEducationalPath(repeaterDef))
077                    {
078                        entries.put(repeaterDef.getName(), repeaterDef.getLabel());
079                    }
080                }
081            }
082        }
083        
084        return entries;
085    }
086
087}