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.*;
import javax.naming.NamingException;
import javax.ejb.FinderException;

/**
 *  Description of the Class
 *
 *@author    Diana Abbas <dabbas @ umail ucsb edu >
 */
public class EJBClient implements ClientInterface
{
	Context        ctx;
	CDEntryHome    home;
	TrackHome      trackHome;


	/**
	 *  Constructor for the EJBClient object
	 *
	 *@exception  Exception  Description of Exception
	 *@since                 empty
	 */
	public EJBClient()
	{
                try{
                        ctx = getContext();
                        home = ( CDEntryHome ) PortableRemoteObject.narrow( ctx.lookup( "CDEntryBean" ), CDEntryHome.class );
                        trackHome = ( TrackHome ) PortableRemoteObject.narrow( ctx.lookup( "TrackBean" ), TrackHome.class );
                } catch (Exception e) {
                        e.printStackTrace();
                }

  }


	/**
	 *  Gets the File attribute of the EJBClient object
	 *
	 *@param  discid         disc id to look up in database
	 *@return                The File in the format requested by the client
	 *@exception  Exception  Description of Exception
	 *@since                 empty
	 */
	public String getFile( String discid )
	{
                String file = null;
                try{


                        Collection collection = home.findByCDID( discid );
                        CDEntryRemote albumBean = ( CDEntryRemote ) collection.iterator().next();

                        file = "DISCID=" + albumBean.getDiscId() + "\n" +
                                        "DTITLE=" + albumBean.getTitle() + "\n" +
                                        "DYEAR=" + albumBean.getYear() + "\n" +
                                        "DGENRE=" + albumBean.getGenre() + "\n";

                        Iterator tracksIter = trackHome.findByCDID( discid ).iterator();


                        while ( tracksIter.hasNext() )
                        {
                                TrackRemote  trackBean  = ( TrackRemote ) tracksIter.next();

                                file += "TTITLE" + trackBean.getTrackNumber() + "=" + trackBean.getTrackName() + "\n";

                        }

                        file += "EXTD=" + albumBean.getExtendedData() + "\n" + "PLAYORDER=" + "\n";
                } catch (Exception e) {
					file = null;
                }


		return file;
	}


	/**
	 *  Gets the Context attribute of the CDDBLookup class
	 *
	 *@return                                   The Context value
	 *@exception  javax.naming.NamingException  Pinpoints where the operation
	 *      failed
	 *@since                                    empty
	 */
	public static Context getContext() throws javax.naming.NamingException
	{
		Properties  env           = new Properties();
		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 ) );
	}

        public static void main( String[] args )
        {
          try
          {
            EJBClient blah = new EJBClient();
            System.out.println( blah.getFile( "000e0c14" ) );
          }
          catch ( Exception ex )
          {
            ex.printStackTrace();
          }
        }


}
