package %packagename;

import java.util.*;
import com.unisys.bis.*;

public class %classname {

    /*
     * BIS-RA Configuration parameters
     */
    private static final String BIS_SITE = "%site";
    private static final String SERVER_NAME = "%sname";
    private static final String SERVER_PORT = "%port";
    
    /*
     * BIS Login information 
     */    
    private static final String SERVER_USER = "%userid";
    private static final String SERVER_DEPARTMENT = "%dept";
    private static final String SERVER_PASSWORD = "%password";
    
    private static final String LOCALE = "%locale";
    private static final String CHARACTER_SET = "%charset";
    private static final int BLOCK_SIZE = %blocksize;
    
    private BISConnection connection = null;

    /**
     * Class constructor
     */
	public %classname(){
		super();
	}
	/**
	 * Called to close a BIS database connection
	 */
    protected void close() {
        if ( this.connection instanceof BISConnection) {
            
            try {
                this.connection.close();
            }
            catch (BISException e) {
            	e.printStackTrace();
            }
            finally {
           	   this.connection = null;
       		}   
       	}
    }
    
    /**
     * Called to open a BIS database connection, based on the static final variables defined
     * at the beginning of this class.
     * 
     * @throws BISException - if a BIS database error is detected
     */
    protected void initialize() throws BISException {

        Map configProperties = new HashMap();
        
        configProperties.put(new Integer(BISResourceAdapter.BIS_PORT_NUMBER), SERVER_PORT);
        configProperties.put(new Integer(BISResourceAdapter.BIS_SERVER_NAME), SERVER_NAME);
        configProperties.put(new Integer(BISResourceAdapter.BIS_SITE), BIS_SITE);
        configProperties.put(new Integer(BISResourceAdapter.BIS_USER_NAME), SERVER_USER); 
        configProperties.put(new Integer(BISResourceAdapter.BIS_DEPARTMENT), SERVER_DEPARTMENT); 
        configProperties.put(new Integer(BISResourceAdapter.BIS_PASSWORD), SERVER_PASSWORD); 
        configProperties.put(new Integer(BISResourceAdapter.BIS_LOCALE), LOCALE);  
        configProperties.put(new Integer(BISResourceAdapter.BIS_CHARACTER_SET), CHARACTER_SET);          
        configProperties.put(new Integer(BISResourceAdapter.BIS_BLOCK_SIZE), new Integer(BLOCK_SIZE));
        
        BISConnectionFactory connectionFactory = 
                                 BISResourceAdapter.createConnectionFactory(configProperties);
        this.connection = connectionFactory.getConnection();

    }
            
  
    /**
     * Called to get a BISDataset instance for the specified BIS dataset name
     * 
     * @param datasetName - a BIS dataset name
     * @return
     *     a BISDataset instance
     * @throws BISException - if a BIS database error is detected
     */
    public BISDataset getDataset(String datasetName) throws BISException {
        BISDataset dataset = this.connection.getDataset(datasetName);
        
        return dataset;
    }
    
    /**
     *  Sample code to demonstrate steps in retrieving
     *    a data set from a BIS data base.
     *    
     *    @param datasetName String name of the data set to retrieve
     */
    public static void sampleCall(String datasetName) {
        %classname databaseAccess = new %classname();  
        
        try {
            databaseAccess.initialize();
            
            BISDataset dataset = databaseAccess.getDataset(datasetName);
            
            dataset.close();
        }
        catch (BISException e) {
            e.printStackTrace();
        }
        finally {
            databaseAccess.close();
        }
    }
}


