001/*
002 *  Copyright 2011 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.plugins.blog;
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.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.cms.contenttype.ContentType;
030import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
031import org.ametys.cms.contenttype.MetadataSet;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.parameter.Enumerator;
034
035/**
036 * Metadata set enumerator.
037 */
038public class MetadataSetEnumerator extends AbstractLogEnabled implements Enumerator, Serviceable, Configurable
039{
040    
041    /** Content type extension point. */
042    protected ContentTypeExtensionPoint _cTypeEP;
043    
044    /** The configured content type. */
045    protected String _contentTypeId;
046    
047    @Override
048    public void configure(Configuration configuration) throws ConfigurationException
049    {
050        _contentTypeId = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("content-type").getValue();
051    }
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
057    }
058    
059    @Override
060    public Map<Object, I18nizableText> getEntries() throws Exception
061    {
062        Map<Object, I18nizableText> entries = new LinkedHashMap<>();
063        
064        ContentType cType = _cTypeEP.getExtension(_contentTypeId);
065        if (cType != null)
066        {
067            for (String metadataSetName : cType.getViewMetadataSetNames(false))
068            {
069                MetadataSet metadataSet = cType.getMetadataSetForView(metadataSetName);
070                
071                entries.put(metadataSetName, metadataSet.getLabel());
072            }
073        }
074        
075        return entries;
076    }
077    
078    @Override
079    public I18nizableText getEntry(String value) throws Exception
080    {
081        I18nizableText entry = null;
082        
083        ContentType cType = _cTypeEP.getExtension(_contentTypeId);
084        if (cType != null)
085        {
086            MetadataSet metadataSet = cType.getMetadataSetForView(value);
087            if (metadataSet != null)
088            {
089                entry = metadataSet.getLabel();
090            }
091        }
092        
093        return entry;
094    }
095
096}