/*
 * ClientFactory.java
 *
 * Created on March 11, 2005, 9:31 PM
 */

package thegroup.projectcddb.database;

/**
 *
 * @author Alan Savage
 */

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;

/**
 * Factory class for ClientInterfaces.
 */
public final class ClientFactory {
        
        /**
         * returns a ClientInterface depending on the setting of the config.cfg file
         * The settings are the following.
         * DATABASE=FileReader
         * DATABASE=MySQL
         * 
         * The instantiation of ClientInterface will reflect these settings.
         * @return a freedb ClientInterface
         */
        public static final ClientInterface getClientInterface() {
                FileReader fileReader = null;
                File file = null;
                try {
                        
                        file = new File("config.cfg");
                        fileReader = new FileReader(file);
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
                
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String config = null;
                try {
                        config = bufferedReader.readLine();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                
                
                if(config.equals("DATABASE=FileReader")) {
                        return new FileOpener();
                } else if(config.equals("DATABASE=MySQL") ) {
                        return new EJBClient();
                } else {
                        System.err.println("Error in config file.");
                        return null;
                }
        }
        
        
        
        
}
