package thegroup.projectcddb.database;

import java.rmi.RemoteException;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;
import java.util.*;

/**
 *  Connects to the database and looks up the file according to the primary key or disc id.
 *
 *@author    Diana Abbas <dabbas @ umail ucsb edu >
 *@author    Kyle Dorman <kyle_dorman @ umail ucsb edu>
 */
public class CDDBLookup
{
	/**
	 *  Constructor for the CDDBLookup object
	 *
	 */
	public CDDBLookup() { }


	/**
	 *  Gets the Context attribute of the CDDBLookup class
	 *
	 *@return                                   The Context value
	 *@exception  javax.naming.NamingException  Pinpoints where the operation failed
	 */
	public static Context getContext() throws javax.naming.NamingException
	{
		Properties  env           = new Properties();
//The JNDI properties you set depend
//on which server you are using.
		String      jndiProvider  = "org.openejb.client.RemoteInitialContextFactory";

		env.put( "java.naming.factory.initial", jndiProvider );
		env.put( "java.naming.provider.url", "169.231.19.126:4201" );
		env.put( "java.naming.security.principal", "fakeuser" );
		env.put( "java.naming.security.credentials", "fakepass" );
		return ( ( Context ) new InitialContext( env ) );
	}


	/**
	 *  The main program for the CDDBLookup class
	 *
	
	 *@exception  Exception  Conditions that a reasonable application may want to catch

	 */
	public static void main( String[] args ) throws Exception
	{

		Context      ctx        = getContext();
		CDEntryHome  home       = ( CDEntryHome ) PortableRemoteObject.narrow( ctx.lookup( "CDEntryBean" ), CDEntryHome.class );
		TrackHome    trackHome  = ( TrackHome ) PortableRemoteObject.narrow( ctx.lookup( "TrackBean" ), TrackHome.class );

		if ( args[0].equals( "id" ) )
		{
			CDEntryRemote  album_create  = home.findByPrimaryKey( new Integer( args[1] ) );

			System.out.println( "!!find a CDEntry using the primary key!!" );
			System.out.println( "[EJB] Found Bean" );
			System.out.println( "[EJB] Primary Key: " + album_create.getId() );
			System.out.println( "[EJB] Discid: " + album_create.getDiscId() );
			System.out.println( "[EJB] Genre: " + album_create.getGenre() );
			System.out.println( "[EJB] Title: " + album_create.getTitle() );
			System.out.println( "[EJB] Year: " + album_create.getYear() );
			System.out.println( "[EJB] Extended: " + album_create.getExtendedData() );
		}

		if ( args[0].equals( "discid" ) )
		{
			String         discIdTest    = args[1];

			Collection     collection    = home.findByCDID( discIdTest );

			CDEntryRemote  album_create  = ( CDEntryRemote ) collection.iterator().next();

			System.out.println( "!!find a CDEntry using the discid!!" );
			System.out.println( "[EJB] Found Bean" );
			System.out.println( "[EJB] Primary Key: " + album_create.getId() );
			System.out.println( "[EJB] Discid: " + album_create.getDiscId() );
			System.out.println( "[EJB] Genre: " + album_create.getGenre() );
			System.out.println( "[EJB] Title: " + album_create.getTitle() );
			System.out.println( "[EJB] Year: " + album_create.getYear() );
			System.out.println( "[EJB] Extended: " + album_create.getExtendedData() );
			album_create = null;

			Collection     tracks        = trackHome.findByCDID( discIdTest );
			Iterator       tracksIter    = tracks.iterator();

			System.out.println( "!!find all the tracks associated with a discid!!" );
			while ( tracksIter.hasNext() )
			{
				TrackRemote  trackBean  = ( TrackRemote ) tracksIter.next();

				System.out.println( "[EJB]===TRACK====================" );
				System.out.println( "[EJB] Track for DiscID: " + trackBean.getDiscId() );
				System.out.println( "[EJB] Track Number: " + trackBean.getTrackNumber() );
				System.out.println( "[EJB] Track Name:" + trackBean.getTrackName() );

			}
		}
	}
}
