% ADDCONNECTION adds a new managed connection to the repository manager.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% description: addconnection adds a new managed connection to the
%              repository manager.
%              this method still adds a new connection. it doesn't open by
%              default the connection and for this reason it is not
%              necessary to define a password yet.
%
% call:        conn = rm.addconnection(pl);
%              conn = rm.addconnection('hostname');
%              conn = rm.addconnection('hostname', 'database');
%              conn = rm.addconnection('hostname', 'database', 'username');
%              conn = rm.addconnection('hostname', 'database', 'username', 'password');
%
% <a href="matlab:web(ltpdarepositorymanager.getinfo('addconnection').tohtml, '-helpbrowser')">parameter sets</a>
%
% version:    $Id: addConnection.m,v 1.6 2011/03/28 12:45:44 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = addConnection(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(utils.const.msg.MNAME , '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');
  autoconnect = pl.find('connect');
  
  % 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 = mpipeline.repository.RepositoryConnection([]);
  conn.setHostname(hostname);
  conn.setDatabase(database);
  conn.setUsername(username);
  conn.setPassword(password);
  
  if ~isempty(password) && autoconnect
    conn.openconnection();
  end
  
  rm.manager.addConnection(conn);
  if ~isempty(rm.gui)
    rm.gui.reloadConnectionTable();
  end
  
  if nargout == 1
    varargout{1} = conn;
  end
  
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: psd.m,v 1.53 2009/12/17 08:04:43 mauro 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);
  
  % auto connect
  p = param({'connect', '''true'' or ''false'' if the repository manager should open the connection.'}, paramValue.FALSE_TRUE);
  pl.append(p);
  
end

