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.core.datasource.dbtype;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.core.util.I18nUtils;
030import org.ametys.plugins.core.impl.datasource.StaticSQLDatabaseType;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * Manager handling the handled SQL database types
035 */
036public class SQLDatabaseTypeManager implements Component, Serviceable
037{
038    /** Avalon Role */
039    public static final String ROLE = SQLDatabaseTypeManager.class.getName();
040    
041    /** The extension point for SQL database types */
042    private SQLDatabaseTypeExtensionPoint _sqlDatabaseTypeExtensionPoint;
043    
044    /** Component containing i18n utility methods */
045    private I18nUtils _i18nUtils;
046    
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        _sqlDatabaseTypeExtensionPoint = (SQLDatabaseTypeExtensionPoint) serviceManager.lookup(SQLDatabaseTypeExtensionPoint.ROLE);
051        _i18nUtils = (I18nUtils) serviceManager.lookup(I18nUtils.ROLE);
052    }
053    
054    /**
055     * Retrieve the available sql drivers and the associated templates
056     * @return the available drivers in JSON 
057     */
058    @Callable
059    public Map<String, Object> getSQLDatabaseTypes()
060    {
061        Map<String, Object> result = new HashMap<> ();
062        List<Map<String, Object>> databaseTypes = new ArrayList<>();
063        
064        Map<Object, I18nizableText> sqlDatabaseTypes = _sqlDatabaseTypeExtensionPoint.getSQLDatabaseTypes();
065        for (Object extensionId : sqlDatabaseTypes.keySet())
066        {
067            Map<String, Object> databaseType = new HashMap<> ();
068            StaticSQLDatabaseType staticSQLDatabaseType = (StaticSQLDatabaseType) _sqlDatabaseTypeExtensionPoint.getExtension((String) extensionId);
069            
070            databaseType.put("label", _i18nUtils.translate(staticSQLDatabaseType.getLabel()));
071            databaseType.put("value", extensionId);
072            databaseType.put("template", staticSQLDatabaseType.getTemplate());
073            
074            databaseTypes.add(databaseType);
075        }
076        
077        result.put("databaseTypes", databaseTypes);
078        return result;
079    }
080    
081    /**
082     * Get the class not found message corresponding to a driver
083     * @param driver the driver
084     * @return the message to use in case the class of the driver was not found
085     */
086    public I18nizableText getClassNotFoundMessage(String driver)
087    {
088        Map<Object, I18nizableText> sqlDatabaseTypes = _sqlDatabaseTypeExtensionPoint.getSQLDatabaseTypes();
089        for (Object extensionId : sqlDatabaseTypes.keySet())
090        {
091            StaticSQLDatabaseType staticSQLDatabaseType = (StaticSQLDatabaseType) _sqlDatabaseTypeExtensionPoint.getExtension((String) extensionId);
092            if (staticSQLDatabaseType.getDriver().equals(driver))
093            {
094                return staticSQLDatabaseType.getDriverNotFoundMessage();
095            }
096        }
097        
098        return null;
099    }
100}