package %packagename;

import java.util.*;
import javax.resource.*;
import javax.resource.spi.*;
import com.unisys.os2200.dms.*;


public class %classname {

    /// DMS-RA Configuration parameters

    private static final String CONNECTION_TYPE = "%conntype";
    private static final String SERVER_NAME = "%sname";
    private static final String SERVER_PORT = "%port";
    

    private static final String SERVER_USER = "%userid";
    private static final String SERVER_PASSWORD = "%password";
    private static final String LOCALE = "%locale";
    private static final String CHARACTER_SET = "%charset";
	private static final String ACCESS_TYPE = "%access";
	private static final String RECOVERY_OPTION = "%recoveryoption";
	private static final String MODE = "%mode";
	
	private static final String mDBName = "%dbname";
	
    
    private DMSConnectionFactory connectionFactory = null;
    private DMSConnection connection = null;
    private LocalTransaction transaction = null;

     /**
     * Class constructor
     */
	public %classname(){
		super();
	}
	/**
	 * Called to close a DMS database connection
	 */
    protected void closeConnection() {
        if ( this.connection instanceof DMSConnection) {
            
            try {
                this.connection.close();
            }
            catch (DMSException e) {
            	e.printStackTrace();
            }
            finally {           
           		 this.connection = null;
		    }   
		}
    }
    
    /**
     * Called to open a DMS connection, based on the static final variables
     * defined at the beginning of this class.
     * 
     * @throws DMSException
     */
    protected void initialize() throws DMSException, ResourceException {
 
        Map configProperties = new HashMap();
        
        configProperties.put(new Integer(DMSResourceAdapter.DMS_PORT_NUMBER), SERVER_PORT);
        configProperties.put(new Integer(DMSResourceAdapter.DMS_SERVER_NAME), SERVER_NAME);
        configProperties.put(new Integer(DMSResourceAdapter.DMS_CONNECTION_TYPE), CONNECTION_TYPE);

        configProperties.put(new Integer(DMSResourceAdapter.DMS_USER_NAME), SERVER_USER); 
        configProperties.put(new Integer(DMSResourceAdapter.DMS_PASSWORD), SERVER_PASSWORD); 
        
        configProperties.put(new Integer(DMSResourceAdapter.DMS_ACCESS_TYPE), ACCESS_TYPE); 
		configProperties.put(new Integer(DMSResourceAdapter.DMS_RECOVERY_OPTION), RECOVERY_OPTION);
		configProperties.put(new Integer(DMSResourceAdapter.DMS_LOCALE), LOCALE);  
        configProperties.put(new Integer(DMSResourceAdapter.DMS_CHARACTER_SET), CHARACTER_SET);          
        configProperties.put(new Integer(DMSResourceAdapter.DMS_MODE), MODE);
        %changefileline
        
        
        connectionFactory = DMSResourceAdapter.createConnectionFactory(configProperties);
        this.connection = connectionFactory.getConnection(SERVER_USER, SERVER_PASSWORD);
        this.transaction = this.connection.getLocalTransaction();
        this.transaction.begin();
    }
    /**
     * Called to get a DMS database manager with requested parameters
     * 
     * @param databaseName - name of database to access
     * @param configuration - DMSDatabaseManager configuration parameters
     * @return a DMSDatabaseManager instance
     * @throws DMSException - thrown if an error is detected while accessing the database
     * @throws ResourceException - thrown if a transaction-related error is detected
     */
    public DMSDatabaseManager getDatabaseManger(String databaseName, Map<Integer, Object> configuration)
            throws DMSException, ResourceException {
        initialize();
    	
		
		DMSDatabaseManager dbMgr = 
		        connection.getDatabaseManager(databaseName, configuration);
		
		return dbMgr;    	

    }
    
        /**
     * Called to end the transaction and close the DMS database connection.
     * 
     * @param rollback - indicates if a transaction rollback or commit is performed
     */
    void close(boolean rollback) {

        if (this.transaction instanceof LocalTransaction) {

            try {
                if (rollback) {
                    this.transaction.rollback();
                }
                else {
                    this.transaction.commit();
                }
            }
            catch (ResourceException e) {
                e.printStackTrace();
            }
            finally {
                this.transaction = null;
                closeConnection();
            }
        }
    }
    
    
    public static Map getDMSDBConfiguration(){
    	Map config = new HashMap();

		%invokeline
		
		%impartline
		
		config.put(new Integer(DMSConnection.DMS_FETCH_WITH_LOCK),
		           new Integer(DMSConnection.%fetchlockval));
		
		config.put(new Integer(DMSConnection.DMS_QUEUING),
		           new Integer(DMSConnection.%queueval));  
		
		return config; 
    }
    
    
    /**
     *  A static method to demonstrate using this class to invoke the data base
     *  
     *  @param dbName - String name of the data base.  If  null or blank,
     *       the name will be one from the wizard.
     */
    public static void sampleCall(String dbName){
    
    	%classname databaseAccess = new %classname();
    
    	Map configuration = getDMSDBConfiguration();
    	
    	// if a database name is not given on the call
    	// 	take the name from the generated code 
    	String databaseName = dbName;
    	if ((databaseName == null) || (databaseName.trim().equals("")))
    		databaseName = mDBName;

    	try {

            DMSDatabaseManager dbMgr = databaseAccess.getDatabaseManger(databaseName,configuration);
            dbMgr.close();
        }
        catch (DMSException e) {
            e.printStackTrace();
        }
        catch (ResourceException e) {
            e.printStackTrace();
        }

        databaseAccess.close(true);
    }
	
}
