001/*
002 *  Copyright 2024 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.forms.question.autofill;
017
018import org.apache.avalon.framework.configuration.Configurable;
019import org.apache.avalon.framework.configuration.Configuration;
020import org.apache.avalon.framework.configuration.ConfigurationException;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang.StringUtils;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.UserManager;
031import org.ametys.plugins.forms.repository.FormQuestion;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.plugin.component.AbstractLogEnabled;
034import org.ametys.runtime.plugin.component.PluginAware;
035
036/**
037 * Static class for creating {@link AutofillSource} from xml configuration
038 */
039public abstract class AbstractStaticAutoFillSource extends AbstractLogEnabled implements AutofillSource, Serviceable, Configurable, PluginAware
040{
041    /** Forms */
042    protected String _pluginName;
043    /** Id of computing type */
044    protected String _id;
045    /** Label of computing type */
046    protected I18nizableText _label;
047    /** Description of computing type */
048    protected I18nizableText _description;
049
050    /** The autofill source extension point */
051    protected AutofillSourceExtensionPoint _autoFillSourceExtensionPoint;
052    /** The current user provider */
053    protected CurrentUserProvider _currentUserProvider;
054    /** The users manager */
055    protected UserManager _userManager;
056    
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        _autoFillSourceExtensionPoint = (AutofillSourceExtensionPoint) manager.lookup(AutofillSourceExtensionPoint.ROLE);
060        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
061        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
062    }
063    
064    public void setPluginInfo(String pluginName, String featureName, String id)
065    {
066        _pluginName = pluginName;
067    }
068
069    public void configure(Configuration configuration) throws ConfigurationException
070    {
071        _id = configuration.getAttribute("id");
072        
073        Configuration childLabel = configuration.getChild("label");
074        _label = I18nizableText.getI18nizableTextValue(childLabel, "plugin." + _pluginName, childLabel.getValue());
075        
076        Configuration childDesc = configuration.getChild("description");
077        _description = I18nizableText.getI18nizableTextValue(childDesc, "plugin." + _pluginName, childDesc.getValue());
078    }
079
080    public String getId()
081    {
082        return _id;
083    }
084
085    public I18nizableText getLabel()
086    {
087        return _label;
088    }
089    
090    public I18nizableText getDescription()
091    {
092        return _description;
093    }
094    
095    public void saxAdditionalInfos(ContentHandler contentHandler, FormQuestion question) throws SAXException
096    {
097        String autofillValue = getAutofillValue(question);
098        if (StringUtils.isNotBlank(autofillValue))
099        {
100            XMLUtils.createElement(contentHandler, "default-value", autofillValue);
101        }
102    }
103
104}