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