/*
 *************************************************************************
 * 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.report.designer.data.ui.util;

import java.util.ArrayList;
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 ManifestExplorer sm_dtpExplorer = 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";
	    
	/**
	 * Singleton getter method.
	 * @return	singleton instance of the class.
	 */
	public static DtpManifestExplorer getInstance()
	{
	    if( sm_instance == null )
	        sm_instance = new DtpManifestExplorer();
	    if( sm_dtpExplorer == null )
	        sm_dtpExplorer = ManifestExplorer.getInstance();
		return sm_instance;
	}
	
	private DtpManifestExplorer()
	{
	}
	
	/**
	 * 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 = 
	        sm_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 sm_dtpExplorer.getExtensionManifest( extensionId, 
	            									BIRT_ODA_EXT_POINT );
	}

	/**
	 * Returns an array of DTP ODA extension configuration information  
	 * found in the plugin manifest file.  Returns 
	 * an empty array if there are no data source extensions found.
	 * Invalid data source extension definitions are ignored.
	 * Includes extensions that implement either the org.eclipse.birt.data.oda.dataSource
	 * or org.eclipse.datatools.connectivity.oda.dataSource extension points.
	 * @return	a DTP ODA <code>ExtensionManifest</code> array containing 
	 * 			the definition of all valid ODA data source extensions.
	 */
	public ExtensionManifest[] getExtensionManifests()
	{
		ExtensionManifest[] dtpManifests =
		    sm_dtpExplorer.getExtensionManifests( DTP_ODA_EXT_POINT );
		ExtensionManifest[] birtManifests =
		    sm_dtpExplorer.getExtensionManifests( BIRT_ODA_EXT_POINT );
		
		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, 
		// excluding the dtp-oda adapter from the returned array		
		ArrayList mergedList = new ArrayList( dtpCount + birtCount );
        final String adapterId = "org.eclipse.birt.data.oda.adapter.dtp";
        
        for( int i = 0; i < dtpCount; i++ )
        {
            if ( ! dtpManifests[i].getNamespace().equals( adapterId ) )
                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 ] );
	}

}
