001/*
002 *  Copyright 2016 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.odf.oai;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.configuration.Configurable;
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
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.plugins.repository.AmetysObjectResolver;
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * Abstract implementation of a OAI-PMH set with a configurable name and description
031 */
032public abstract class AbstractOaiSet implements OaiSet, Configurable, Component, Serviceable
033{
034    /** The Ametys object resolver */
035    protected AmetysObjectResolver _resolver;
036    
037    private I18nizableText _name;
038    private I18nizableText _description;
039    
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
043    }
044    
045    @Override
046    public void configure(Configuration configuration) throws ConfigurationException
047    {
048        _name = _getI18nizableConfiguration(configuration, "name", "");
049        _description = _getI18nizableConfiguration(configuration, "description", null);
050    }
051    
052    private I18nizableText _getI18nizableConfiguration(Configuration configuration, String configurationName, String defaultValue)
053    {
054        boolean isValueI18n = configuration.getChild(configurationName).getAttributeAsBoolean("i18n", false);
055        String value =  configuration.getChild(configurationName).getValue(defaultValue);
056
057        if (value == null)
058        {
059            return null;
060        }
061        else if (isValueI18n)
062        {
063            String valueI18nPlugin = configuration.getChild(configurationName).getAttribute("plugin", "odf");
064            return new I18nizableText("plugin." + valueI18nPlugin, value);
065        }
066        else
067        {
068            return new I18nizableText(value);
069        }
070    }
071
072    public I18nizableText getName()
073    {
074        return _name;
075    }
076
077    public I18nizableText getDescription()
078    {
079        return _description;
080    }
081}