001/*
002 *  Copyright 2017 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.odfweb;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.contenttype.MetadataDefinition;
026import org.ametys.odf.program.ProgramFactory;
027import org.ametys.plugins.odfweb.repository.OdfPageHandler;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.parameter.Enumerator;
030
031/**
032 * Enumerator for the eligible metadata for ODF page level
033 *
034 */
035public class EligibleMetadataForLevelEnumerator implements Enumerator, Serviceable
036{
037    /** The ODF root page handler. */
038    protected OdfPageHandler _odfRootHandler;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        _odfRootHandler = (OdfPageHandler) serviceManager.lookup(OdfPageHandler.ROLE);
044    }
045    
046    @Override
047    public I18nizableText getEntry(String value) throws Exception
048    {
049        Map<String, MetadataDefinition> eligibleMetadataForLevel = _odfRootHandler.getEnumeratedMetadata(ProgramFactory.PROGRAM_CONTENT_TYPE, true);
050        
051        if (eligibleMetadataForLevel.containsKey(value))
052        {
053            return eligibleMetadataForLevel.get(value).getLabel();
054        }
055        
056        return null;
057    }
058
059    @Override
060    public Map<Object, I18nizableText> getEntries() throws Exception
061    {
062        Map<String, MetadataDefinition> eligibleMetadataForLevel = _odfRootHandler.getEnumeratedMetadata(ProgramFactory.PROGRAM_CONTENT_TYPE, true);
063        
064        Map<Object, I18nizableText> entries = new LinkedHashMap<>();
065        
066        for (String path : eligibleMetadataForLevel.keySet())
067        {
068            MetadataDefinition metadataDefinition = eligibleMetadataForLevel.get(path);
069            
070            entries.put(path, metadataDefinition.getLabel());
071        }
072        
073        return entries;
074    }
075
076}