% FINDCONNECTIONS finds a managed connection.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: FINDCONNECTIONS finds a managed connection.
%
% CALL:        conns = rm.findConnections(pl);
%              conns = rm.findConnections('hostname');
%              conns = rm.findConnections('hostname', 'database');
%              conns = rm.findConnections('hostname', 'database', 'username');
%
% findConneciton finds all connections which match the given input fields.
% If no matching connections are found then the method returns an emtpy
% array. To search for all 'hostnames' for a given 'username', enter an
% empty string. For example:
%
% 1) Find all connections for user 'bob'
%
%     pl = plist('username', 'bob');
%     conns = rm.findConnections(pl);
%
%  or
%
%     conns = rm.findConnections('', '', 'bob');
%
% 2) Find all connection to 'localhost' for database 'foo'
%
%    pl    = plist('hostname', 'localhost', 'database', 'foo')
%    conns = rm.findConnections(pl);
%
%  or
%
%    conns = rm.findConnections('localhost', 'foo');
%
% 3) Find all connections to 'localhost' for user 'bob'
%
%    conns = rm.findConnections('localhost', '', 'bob');
%
% <a href="matlab:web(LTPDARepositoryManager.getInfo('findConnections').tohtml, '-helpbrowser')">Parameters Description</a>
%
% VERSION:    $Id: findConnections.m,v 1.7 2011/04/08 08:56:35 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = findConnections(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');
  
  % 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
  
  % make sure that username are strings because JAVA interprets an empty
  % array [] as a null-pointer.
  if isempty(hostname), hostname = ''; end
  if isempty(database), database = ''; end
  if isempty(username), username = ''; end
  
  conns = rm.manager.findConnections(hostname, database, username);
  
  mconns = [];
  for i=1:conns.size
    mconns = [mconns conns.get(i-1)];
  end
  
%   if isempty(mconns)
%     mconns = rm.newConnection(hostname, database, username);
%   end
%   
  utils.helper.msg(utils.const.msg.PROC1, 'Matched %d managed connections', numel(mconns));
  
  if nargout > 0
    varargout{1} = mconns;
  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: findConnections.m,v 1.7 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);
  
end
% END
