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 as a client to the database and finds the file wanted according to primary key or disk ID
 *
 *@author    Diana Abbas <dabbas @ umail ucsb edu >
 *@author    Kyle Dorman <kyle_dorman @ umail ucsb edu>
 */
public class CDDBClient
{
	/**
	 *  Constructor for the CDDBClient object
	 *
	 *
	 */
	public CDDBClient() { }


	/**
	 *  The main program for the CDDBClient class
	 *
	 *
	 */
	public static void main( String[] args )
			 throws Exception
	{
		CDDBClient     cddbclient    = new CDDBClient();
		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" );

		Context        ctx           = new InitialContext( env );

		Object         obj           = ctx.lookup( "CDEntryBean" );

		obj = PortableRemoteObject.narrow( obj, CDEntryHome.class );

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

		CDEntryRemote  album_create  = home.findByPrimaryKey( new Integer( 13 ) );

		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() );

		String         discIdTest    = album_create.getDiscId();

		album_create = null;

		Collection     collection    = home.findByCDID( discIdTest );

		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() );

		}
	}
}
