001/*
002 *  Copyright 2015 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.core.impl.datasource;
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.commons.lang3.StringUtils;
022
023import org.ametys.core.datasource.dbtype.SQLDatabaseType;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.runtime.plugin.component.AbstractLogEnabled;
026import org.ametys.runtime.plugin.component.PluginAware;
027
028/**
029 * Default implementation for a {@link SQLDatabaseType}.
030 */
031public class StaticSQLDatabaseType extends AbstractLogEnabled implements Configurable, SQLDatabaseType, PluginAware
032{
033    /** The id of the database type */
034    protected String _id;
035    
036    /** The label of the database type */
037    protected I18nizableText _label;
038
039    /** The driver of the database type */
040    protected String _driver;
041    
042    /** The url template for this database type */
043    protected String _template;
044    
045    /** The plugin's name */
046    protected String _pluginName;
047    
048    /** The SQL query to validate the connection is still alive */
049    protected String _validationQuery;
050    
051    /** The driver not found message */
052    protected I18nizableText _driverNotFoundMessage;
053
054    /** The template to escape the table name. Must contain {tableName} */
055    protected String _languageEscapeTableNameTemplate;
056    /** The template to limit the query to a limit/offset. Must contain {query} {limit} and {offset}*/
057    protected String _languageLimitQueryTemplate;
058    
059    public void setPluginInfo(String pluginName, String featureName, String id)
060    {
061        _pluginName = pluginName;
062        _id = id;
063    }
064    
065    public void configure(Configuration configuration) throws ConfigurationException
066    {
067        if (getLogger().isDebugEnabled())
068        {
069            getLogger().debug("Configuring database type with id '" + _id  + "'");
070        }
071        
072        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), "plugin." + _pluginName);
073        _driver = configuration.getChild("driver").getValue();
074        _driverNotFoundMessage = I18nizableText.parseI18nizableText(configuration.getChild("driver-not-found-message"), "plugin." + _pluginName); 
075        _template = configuration.getChild("template").getValue("");
076        _validationQuery = configuration.getChild("validation-query").getValue("SELECT 1");
077        _languageEscapeTableNameTemplate = configuration.getChild("language").getChild("escape-table-name").getValue("{tableName}");
078        _languageLimitQueryTemplate = configuration.getChild("language").getChild("limit-query").getValue("{query} LIMIT {limit} OFFSET {offset}");
079    }
080    
081    public String getId()
082    {
083        return _id;
084    }
085    
086    public I18nizableText getLabel() 
087    {
088        return _label;
089    }
090    
091    public String getDriver() 
092    {
093        return _driver;
094    }
095    
096    public String getTemplate()
097    {
098        return _template;
099    }
100    
101    public String getValidationQuery()
102    {
103        return _validationQuery;
104    }
105    
106    public I18nizableText getDriverNotFoundMessage()
107    {
108        return _driverNotFoundMessage;
109    }
110    
111    public String languageEscapeTableName(String tableNameToEscape)
112    {
113        return StringUtils.replace(_languageEscapeTableNameTemplate, "{tableName}", tableNameToEscape);
114    }
115    
116    public String languageLimitQuery(String queryToLimit, String limit, String offset)
117    {
118        return StringUtils.replace(StringUtils.replace(StringUtils.replace(_languageLimitQueryTemplate, "{query}", queryToLimit), "{limit}", limit), "{offset}", offset);
119    }
120}