% JCONNTODBCONN convert a java db connection to a MATLAB database
% connection.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: JCONNTODBCONN convert a java db connection to a MATLAB
%              database Toolbox connection.
%
% CALL:        dbconn = utils.jmysql.jconnTodbconn(jconn);
%
% INPUTS:
%              jconn - a connection object like that returned from
%                     utils.jmysql.connect.
% 
% OUTPUTS:     dbconn - a database toolbox connecton
%
%
% VERSION:     $Id: jconnTodbconn.m,v 1.1 2009/07/28 06:46:09 hewitson Exp $
%
% HISTORY:     24-05-2007 M Hewitson
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function conn = jconnTodbconn(jconn)
  
  if nargin ~= 1 || ~isa(jconn, 'mpipeline.repository.RepositoryConnection')
    error('### The first argument should be a RepositoryConnection');
  end

  host = char(jconn.getHostname);
  db   = char(jconn.getDatabase);
  user = char(jconn.getUsername);
  pw   = char(jconn.getPassword);
  
  dbdriver = getappdata(0, 'mysql_driver');
  dburl    = sprintf('jdbc:mysql://%s/%s',host,db);

  disp(sprintf('** Connecting to %s as %s...', host, user))

  conn     = database(db,user,pw,dbdriver,dburl);
  disp('** Connection status:')
  disp(ping(conn))
  
  
end

