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