/*
 *************************************************************************
 * 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.oda.util.manifest;

import java.util.ArrayList;
import java.util.Properties;

import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.util.manifest.ExtensionManifest;
import org.eclipse.datatools.connectivity.oda.util.manifest.ManifestExplorer;

/**
 * The DTP Manifest Explorer is the entry point to explore and access
 * the DTP ODA manifest of all the ODA plug-ins extensions that implement the 
 * <code>org.eclipse.birt.data.oda.dataSource</code> or
 * <code>org.eclipse.datatools.connectivity.oda.dataSource</code> extension point.
 * The <code>DtpManifestExplorer</code> singleton instance can be retrieved 
 * using the <code>getInstance()</code> method.
 * <br>
 * This provides a migration path to BIRT components to adopt
 * the DTP ODA manifest package, and covers backward compatibilty
 * to support both BIRT and DTP ODA extensions.
 */

public class DtpManifestExplorer
{
	private static DtpManifestExplorer sm_instance = null;

	private static final String DTP_ODA_EXT_POINT = 
	    "org.eclipse.datatools.connectivity.oda.dataSource";
	private static final String BIRT_ODA_EXT_POINT = 
	    "org.eclipse.birt.data.oda.dataSource";
	  
    private ManifestExplorer m_dtpExplorer;
    
	/**
	 * Singleton getter method.
	 * @return	singleton instance of the class.
	 */
	public static DtpManifestExplorer getInstance()
	{
	    if( sm_instance == null )
	        sm_instance = new DtpManifestExplorer();
		return sm_instance;
	}
    
    /**
     * Singleton instance release method.
     */
    public static void releaseInstance()
    {
        sm_instance = null;
    }
	
	private DtpManifestExplorer()
	{
        m_dtpExplorer = ManifestExplorer.getInstance();
	}
	
	/**
	 * Returns a collection of identifiers of 
	 * all ODA data source extensions (both BIRT and DTP).
	 * The extension's data source element ID and display name
	 * are stored as the key and value in the returned Properties instance.
	 * Returns an empty <code>Properties</code> if there are 
	 * no data source extensions found.
	 * @return	a <code>Properties</code> containing the id
	 * 			and display name of all data source extensions.
	 */
	public Properties getDataSourceIdentifiers()
	{
	    org.eclipse.birt.data.oda.util.manifest.ManifestExplorer birtExplorer =
	        org.eclipse.birt.data.oda.util.manifest.ManifestExplorer.getInstance();
	    return birtExplorer.getDataSourceIdentifiers();
	}

	/**
	 * Returns the DTP ODA extension configuration information found 
	 * in the plugin manifest file
	 * of the specified data source extension.
	 * Includes extension that implements either the org.eclipse.birt.data.oda.dataSource
	 * or org.eclipse.datatools.connectivity.oda.dataSource extension points.
	 * @param extensionId	the unique id of the data source element
	 * 						in a data source extension.
	 * @return	the DTP ODA extension manifest information.
	 * @throws IllegalArgumentException	if no ODA driver plugin supports the data source extension ID.
	 * @throws OdaException	if the extension manifest is invalid.
	 */
	public ExtensionManifest getExtensionManifest( String extensionId ) 
		throws OdaException
	{
	    ExtensionManifest manifest = 
            m_dtpExplorer.getExtensionManifest( extensionId,
	                							 DTP_ODA_EXT_POINT );
	    if( manifest != null )	// found valid manifest for given extensionId
	        return manifest;
	    
	    // try look under the birt oda extension point
	    return m_dtpExplorer.getExtensionManifest( extensionId, 
	            									BIRT_ODA_EXT_POINT );
	}

	/**
	 * Returns an array of ODA extension configuration information  
	 * found in the plugin manifest file.  
     * Includes extensions that implement either the org.eclipse.birt.data.oda.dataSource,
     * or the org.eclipse.datatools.connectivity.oda.dataSource extension points with
     * at least one valid dataSet element defined.
	 * Returns an empty array if there are no data source extensions found.
	 * Invalid data source extension definitions are ignored.
	 * @return	a DTP ODA <code>ExtensionManifest</code> array containing 
	 * 			the definition of all valid ODA data source extensions.
	 */
	public ExtensionManifest[] getExtensionManifests()
	{
		ExtensionManifest[] dtpManifests =
            m_dtpExplorer.getExtensionManifests( DTP_ODA_EXT_POINT, false );
		ExtensionManifest[] birtManifests =
            m_dtpExplorer.getExtensionManifests( BIRT_ODA_EXT_POINT, false );
		
		int dtpCount = dtpManifests.length;
		int birtCount = birtManifests.length;
		
		if( dtpCount == 0 && birtCount > 0 )	// dtpManifests is empty
		    return birtManifests;
		if( dtpCount == 0 && birtCount == 0 )	// both are empty
		    return dtpManifests;	// return an empty array
				
		// merge the birt and dtp extensions into a single array		
		ArrayList mergedList = new ArrayList( dtpCount + birtCount );
        
        for( int i = 0; i < dtpCount; i++ )
        {
            mergedList.add( dtpManifests[i] );
        }

        for( int i = 0; i < birtCount; i++ )
		{
		    mergedList.add( birtManifests[i] );
		}
        
		int numOfValidExtensions = mergedList.size();
		return (ExtensionManifest[]) 
			mergedList.toArray( new ExtensionManifest[ numOfValidExtensions ] );
	}

}
