% GETSINFO gets the submission information of an object in the repository.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: GETSINFO gets the submission information of an object in the
%              repository.
%
% CALL:        sinfo = LTPDARepositoryManager.getSinfo(pl);
%              sinfo = LTPDARepositoryManager.getSinfo(ids);
%              sinfo = LTPDARepositoryManager.getSinfo(ids, 'hostname');
%              sinfo = LTPDARepositoryManager.getSinfo(ids, 'hostname', 'database');
%              sinfo = LTPDARepositoryManager.getSinfo(ids, 'hostname', 'database', 'username');
%
% 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('LTPDARepositoryManager.getSinfo').tohtml, '-helpbrowser')">Parameters Description</a>
%
% VERSION:    $Id: getSinfo.m,v 1.4 2011/04/08 08:56:35 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = getSinfo(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);
  
  % Collect all plists
  [pl, invars, rest] = utils.helper.collect_objects(varargin(:), 'plist');

  pl = combine(pl, getDefaultPlist);
  ids      = pl.find('ids');
  hostname = pl.find('hostname');
  database = pl.find('database');
  username = pl.find('username');

  % Check through 'rest'  
  if numel(rest) > 0
    ids = rest{1};
  end
  if numel(rest) > 1
    hostname = rest{2};
  end
  if numel(rest) > 2
    database = rest{3};
  end
  if numel(rest) > 3
    username = rest{4};
  end
  
  % Some plausibility checks
  if isempty(ids)
    error('### This method needs at least one object id.');
  end
  if nargout == 0
    error('### This method can not be used as a modifier method. Please give one output');
  end
  
  % Get complete experiment information for each input id
  sinfo = [];
  for ii=1:length(ids)
    query = ['select name,experiment_title,experiment_desc,analysis_desc,quantity, '...
      'additional_authors,additional_comments,keywords,reference_ids FROM objmeta ' ...
      sprintf('where objmeta.obj_id=%d',ids(ii))];
    
    infoall = LTPDARepositoryManager.executeQuery(query, hostname, database, username);
    if ~isempty(infoall)
      s.name                    = infoall{1};
      s.experiment_title        = infoall{2};
      s.experiment_description  = infoall{3};
      s.analysis_description    = infoall{4};
      s.quantity                = infoall{5};
      s.additional_authors      = infoall{6};
      s.additional_comments     = infoall{7};
      s.keywords                = infoall{8};
      s.reference_ids           = infoall{9};
      sinfo = [sinfo s];
    else
      warning('!!! Doesn''t find any submission information for the object id %d', ids(ii));
    end
  end
  
  % Set output
  varargout{1} = sinfo;
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: Returns the method-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.helper, '$Id: getSinfo.m,v 1.4 2011/04/08 08:56:35 hewitson Exp $', sets, pl);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Returns the default PLIST
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pl = getDefaultPlist()
  
  % Initialise plist
  pl = plist();
  
  % ids
  p = param({'ids', 'Object identifications of which you need the submission information.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  % 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

