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.runtime.i18n.I18nizableText;
032import org.ametys.runtime.model.Enumerator;
033import org.ametys.runtime.model.View;
034
035/**
036 * Metadata set enumerator.
037 */
038public class ContentViewEnumerator extends AbstractLogEnabled implements Enumerator<String>, 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    public void configure(Configuration configuration) throws ConfigurationException
048    {
049        _contentTypeId = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("content-type").getValue();
050    }
051    
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
055    }
056    
057    public Map<String, I18nizableText> getTypedEntries() throws Exception
058    {
059        Map<String, I18nizableText> entries = new LinkedHashMap<>();
060        
061        ContentType cType = _cTypeEP.getExtension(_contentTypeId);
062        if (cType != null)
063        {
064            for (String viewName : cType.getViewNames(false))
065            {
066                View view = cType.getView(viewName);
067                
068                entries.put(viewName, view.getLabel());
069            }
070        }
071        
072        return entries;
073    }
074    
075    @Override
076    public I18nizableText getEntry(String value) throws Exception
077    {
078        I18nizableText entry = null;
079        
080        ContentType cType = _cTypeEP.getExtension(_contentTypeId);
081        if (cType != null)
082        {
083            View view = cType.getView(value);
084            if (view != null)
085            {
086                entry = view.getLabel();
087            }
088        }
089        
090        return entry;
091    }
092
093}