% NEWCONNECTION makes a new managed repository connection.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: NEWCONNECTION makes a new managed repository connection.
%
% CALL:        conn = rm.newConnection(pl);
%              conn = rm.newConnection('hostname');
%              conn = rm.newConnection('hostname', 'database');
%              conn = rm.newConnection('hostname', 'database', 'username');
%              conn = rm.newConnection('hostname', 'database', 'username', 'password');
%
% If all required connection fields are input, the connection will be
% silently created and added to the manager. Otherwise, a connection dialog
% will be presented and the resulting connection added to the manager.
%
% <a href="matlab:web(LTPDARepositoryManager.getInfo('newConnection').tohtml, '-helpbrowser')">Parameters Description</a>
%
% VERSION:    $Id: newConnection.m,v 1.10 2011/04/08 08:56:35 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = newConnection(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
  
  % Get the repository manager - there should only be one!
  [rm, invars, rest] = utils.helper.collect_objects(varargin(:), 'LTPDARepositoryManager');
  
  % Collect all plists
  [pl, invars, rest] = utils.helper.collect_objects(rest(:), 'plist');
  
  pl = combine(pl, getDefaultPlist);
  hostname = pl.find('hostname');
  database = pl.find('database');
  username = pl.find('username');
  password = pl.find('password');
  
  % Check through 'rest'
  if numel(rest) > 0
    hostname = rest{1};
  end
  if numel(rest) > 1
    database = rest{2};
  end
  if numel(rest) > 2
    username = rest{3};
  end
  if numel(rest) > 3
    password = rest{4};
  end
  
  conn = rm.manager.findExactConnection(hostname, database, username);
  
  if ~isempty(conn)
    if ~isempty(password) && isempty(char(conn.getPassword))
      conn.setPassword(password);
    end
  else
    conn = rm.basic_newConnection(hostname, database, username, password);
  end
  
  % my be it is necessary to update the GUI table
  if ~isempty(rm.gui)
    rm.gui.reloadConnectionTable();
  end
  if ~isempty(rm.selector)
    rm.selector.reloadConnectionTable();
  end
  varargout{1} = conn;
  
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'LTPDARepositoryManager', 'ltpda', utils.const.categories.gui, '$Id: newConnection.m,v 1.10 2011/04/08 08:56:35 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  
  % Initialise plist
  pl = plist();
  
  % hostname
  p = param({'hostname', 'The hostname of the repository to connect to.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % database
  p = param({'database', 'The database on the repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % username
  p = param({'username', 'The username to connect with.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % password
  p = param({'password', 'The password to connect with. Leave this empty to be prompted on connection.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
end

