<commit_before>/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.data.engine.odaconsumer;

import java.io.IOException;
import java.net.URL;
import java.sql.Types;
import java.util.logging.Level;

import org.eclipse.birt.core.framework.IBundle;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.datatools.connectivity.oda.consumer.helper.OdaDriver;
import org.eclipse.datatools.connectivity.oda.IDriver;
import org.eclipse.datatools.connectivity.oda.LogConfiguration;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.util.manifest.ManifestExplorer;
import org.eclipse.datatools.connectivity.oda.util.manifest.DataSetType;
import org.eclipse.datatools.connectivity.oda.util.manifest.DataTypeMapping;
import org.eclipse.datatools.connectivity.oda.util.manifest.ExtensionManifest;
import org.eclipse.datatools.connectivity.oda.util.manifest.TraceLogging;

/**
 * Each <code>Driver</code> maintains the state of a driver in the drivers 
 * home directory.  
 */
class Driver
{
	private String m_dataSourceDriverId;
	private ExtensionManifest m_driverConfig;
	private ExtensionManifest m_adapterConfig;
    private IDriver m_driverHelper;

    private static final String BIRT_DATASOURCE_EXTENSION_POINT = "org.eclipse.birt.data.oda.dataSource";
    private static final String ODA_ADAPTER_PLUGIN_ID = "org.eclipse.birt.data.oda.adapter.dtp";
    
    // trace logging variables
	private static final String sm_className = Driver.class.getName();
	private static final String sm_loggerName = ConnectionManager.sm_packageName;
	private static LogHelper sm_logger = LogHelper.getInstance( sm_loggerName );

	Driver( String dataSourceElementId )
	{
		final String methodName = "Driver";		
		sm_logger.entering( sm_className, methodName, dataSourceElementId );

		m_dataSourceDriverId = dataSourceElementId;

		sm_logger.exiting( sm_className, methodName, this );
	}

	/**
	 * Returns the manifest that should be passed to 
	 * the ODA consumer helper to handle, 
	 * either a DTP ODA driver or the DTP-to-BIRT adapter.
	 */ 
	ExtensionManifest getExtensionConfig() throws DataException
	{
		if ( m_adapterConfig != null )
	        return m_adapterConfig;

		// get DTP ODA driver manifest, or
		// do lazy initialization of member variable(s)
		ExtensionManifest driverManifest = getDriverExtensionConfig();
	    assert( driverManifest != null );	// otherwise, DataException should have been thrown
		
	    if ( m_adapterConfig != null )	// check if adapter is now initialized
	        return m_adapterConfig;

	    return driverManifest;	// manifest of a DTP ODA driver
	}
	
	/**
	 * Returns the manifest of a DTP ODA driver, or that of
	 * a BIRT ODA driver.
	 * @throws DataException
	 */	 
	ExtensionManifest getDriverExtensionConfig() throws DataException
	{
		if ( m_driverConfig != null )
	        return m_driverConfig;
		
		// do lazy initialization;
	    // find the driver extension config and initializes member variables
	    findDataSourceExtensionConfig();
	    
	    assert( m_driverConfig != null );	// otherwise, DataException should have been thrown
	    return m_driverConfig;
	}

	// gets the consumer manager helper for this driver
	IDriver getDriverHelper() throws DataException
	{	
		final String methodName = "getDriverHelper";
		if( m_driverHelper != null )
            return m_driverHelper;
        
		try
		{		
            m_driverHelper = new OdaDriver( getExtensionConfig() );
            return m_driverHelper;
		}
		catch( OdaException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot get ODA data source driver helper.", ex );
			throw new DataException( ResourceConstants.INIT_ENTRY_CANNOT_BE_FOUND, ex, 
                                     new Object[] { m_dataSourceDriverId } );
		}
		catch( UnsupportedOperationException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot get ODA data source driver factory.", ex );
			throw new DataException( ResourceConstants.INIT_ENTRY_CANNOT_BE_FOUND, ex, 
                                     new Object[] { m_dataSourceDriverId } );
		}
	}

	// gets the specific native-to-oda type mapping for the specified data set type 
	// in this driver
	int getTypeMapping( String dataSetType, int nativeType ) throws DataException
	{
	    final String methodName = "getTypeMapping";
		DataSetType dsType = null;
		
		try
		{
			dsType = getDriverExtensionConfig().getDataSetType( dataSetType );
		}
		catch( OdaException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot find data set element.", ex );
			throw new DataException( ResourceConstants.INVALID_DATA_SET_TYPE, ex );
		}

		DataTypeMapping mapping = dsType.getDataTypeMapping( (short) nativeType );
		
		// no mapping found in data source extension configuration, return a default type
		if( mapping == null )	
			return Types.NULL;
		
		String odaType = mapping.getOdaScalarDataType();
		
		if( odaType.equalsIgnoreCase( "Date" ) )
			return Types.DATE;
		else if( odaType.equalsIgnoreCase( "Decimal" ) )
			return Types.DECIMAL;
		else if( odaType.equalsIgnoreCase( "Double" ) )
			return Types.DOUBLE;
		else if( odaType.equalsIgnoreCase( "Integer" ) )
			return Types.INTEGER;
		else if( odaType.equalsIgnoreCase( "String" ) )
			return Types.CHAR;
		else if( odaType.equalsIgnoreCase( "Time" ) )<commit_msg>[132084] Inconsistent manifest data type for an ODA driver's native data type code<commit_after>/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.data.engine.odaconsumer;

import java.io.IOException;
import java.net.URL;
import java.sql.Types;
import java.util.logging.Level;

import org.eclipse.birt.core.framework.IBundle;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.datatools.connectivity.oda.consumer.helper.OdaDriver;
import org.eclipse.datatools.connectivity.oda.IDriver;
import org.eclipse.datatools.connectivity.oda.LogConfiguration;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.util.manifest.ManifestExplorer;
import org.eclipse.datatools.connectivity.oda.util.manifest.DataSetType;
import org.eclipse.datatools.connectivity.oda.util.manifest.DataTypeMapping;
import org.eclipse.datatools.connectivity.oda.util.manifest.ExtensionManifest;
import org.eclipse.datatools.connectivity.oda.util.manifest.TraceLogging;

/**
 * Each <code>Driver</code> maintains the state of a driver in the drivers 
 * home directory.  
 */
class Driver
{
	private String m_dataSourceDriverId;
	private ExtensionManifest m_driverConfig;
	private ExtensionManifest m_adapterConfig;
    private IDriver m_driverHelper;

    private static final String BIRT_DATASOURCE_EXTENSION_POINT = "org.eclipse.birt.data.oda.dataSource";
    private static final String ODA_ADAPTER_PLUGIN_ID = "org.eclipse.birt.data.oda.adapter.dtp";
    
    // trace logging variables
	private static final String sm_className = Driver.class.getName();
	private static final String sm_loggerName = ConnectionManager.sm_packageName;
	private static LogHelper sm_logger = LogHelper.getInstance( sm_loggerName );

	Driver( String dataSourceElementId )
	{
		final String methodName = "Driver";		
		sm_logger.entering( sm_className, methodName, dataSourceElementId );

		m_dataSourceDriverId = dataSourceElementId;

		sm_logger.exiting( sm_className, methodName, this );
	}

	/**
	 * Returns the manifest that should be passed to 
	 * the ODA consumer helper to handle, 
	 * either a DTP ODA driver or the DTP-to-BIRT adapter.
	 */ 
	ExtensionManifest getExtensionConfig() throws DataException
	{
		if ( m_adapterConfig != null )
	        return m_adapterConfig;

		// get DTP ODA driver manifest, or
		// do lazy initialization of member variable(s)
		ExtensionManifest driverManifest = getDriverExtensionConfig();
	    assert( driverManifest != null );	// otherwise, DataException should have been thrown
		
	    if ( m_adapterConfig != null )	// check if adapter is now initialized
	        return m_adapterConfig;

	    return driverManifest;	// manifest of a DTP ODA driver
	}
	
	/**
	 * Returns the manifest of a DTP ODA driver, or that of
	 * a BIRT ODA driver.
	 * @throws DataException
	 */	 
	ExtensionManifest getDriverExtensionConfig() throws DataException
	{
		if ( m_driverConfig != null )
	        return m_driverConfig;
		
		// do lazy initialization;
	    // find the driver extension config and initializes member variables
	    findDataSourceExtensionConfig();
	    
	    assert( m_driverConfig != null );	// otherwise, DataException should have been thrown
	    return m_driverConfig;
	}

	// gets the consumer manager helper for this driver
	IDriver getDriverHelper() throws DataException
	{	
		final String methodName = "getDriverHelper";
		if( m_driverHelper != null )
            return m_driverHelper;
        
		try
		{		
            m_driverHelper = new OdaDriver( getExtensionConfig() );
            return m_driverHelper;
		}
		catch( OdaException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot get ODA data source driver helper.", ex );
			throw new DataException( ResourceConstants.INIT_ENTRY_CANNOT_BE_FOUND, ex, 
                                     new Object[] { m_dataSourceDriverId } );
		}
		catch( UnsupportedOperationException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot get ODA data source driver factory.", ex );
			throw new DataException( ResourceConstants.INIT_ENTRY_CANNOT_BE_FOUND, ex, 
                                     new Object[] { m_dataSourceDriverId } );
		}
	}

	// gets the specific native-to-oda type mapping for the specified data set type 
	// in this driver
	int getTypeMapping( String dataSetType, int nativeType ) throws DataException
	{
	    final String methodName = "getTypeMapping";
		DataSetType dsType = null;
		
		try
		{
			dsType = getDriverExtensionConfig().getDataSetType( dataSetType );
		}
		catch( OdaException ex )
		{
			sm_logger.logp( Level.SEVERE, sm_className, methodName,
					"Cannot find data set element.", ex );
			throw new DataException( ResourceConstants.INVALID_DATA_SET_TYPE, ex );
		}

		DataTypeMapping mapping = dsType.getDataTypeMapping( nativeType );
		
		// no mapping found in data source extension configuration, return a default type
		if( mapping == null )	
			return Types.NULL;
		
		String odaType = mapping.getOdaScalarDataType();
		
		if( odaType.equalsIgnoreCase( "Date" ) )
			return Types.DATE;
		else if( odaType.equalsIgnoreCase( "Decimal" ) )
			return Types.DECIMAL;
		else if( odaType.equalsIgnoreCase( "Double" ) )
			return Types.DOUBLE;
		else if( odaType.equalsIgnoreCase( "Integer" ) )
			return Types.INTEGER;
		else if( odaType.equalsIgnoreCase( "String" ) )
			return Types.CHAR;
		else if( odaType.equalsIgnoreCase( "Time" ) )
{'fraction_non_alphanumeric': 0.10703969629446232, 'fraction_numerical': 0.0028059750763390276, 'mean_word_length': 3.8823529411764706, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657477', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3407, 'n_words': 949}