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.odf.program.ProgramFactory;
026import org.ametys.plugins.odfweb.repository.OdfPageHandler;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.Enumerator;
029import org.ametys.runtime.model.ModelItem;
030
031/**
032 * Enumerator for the eligible metadata for ODF page level
033 *
034 */
035public class EligibleMetadataForLevelEnumerator implements Enumerator<String>, 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, ModelItem> eligibleAttributesForLevel = _odfRootHandler.getEnumeratedAttributes(ProgramFactory.PROGRAM_CONTENT_TYPE, true);
050        
051        if (eligibleAttributesForLevel.containsKey(value))
052        {
053            return eligibleAttributesForLevel.get(value).getLabel();
054        }
055        
056        return null;
057    }
058
059    @Override
060    public Map<String, I18nizableText> getTypedEntries() throws Exception
061    {
062        Map<String, ModelItem> eligibleAttributesForLevel = _odfRootHandler.getEnumeratedAttributes(ProgramFactory.PROGRAM_CONTENT_TYPE, true);
063        
064        Map<String, I18nizableText> entries = new LinkedHashMap<>();
065        
066        for (String path : eligibleAttributesForLevel.keySet())
067        {
068            entries.put(path, eligibleAttributesForLevel.get(path).getLabel());
069        }
070        
071        return entries;
072    }
073}