% Retrieve a ltpda_uo from a repository
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromRepository
%
% DESCRIPTION: Retrieve a ltpda_uo from a repository
%
% CALL:        obj = fromRepository(pl)
%
% PARAMETER:   pl: Parameter list object
%
% VERSION:     $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [objs, plhout, ii] = fromRepository(obj, pli, ii)
  
  VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $';
  
  requested_class = class(obj);
  
  % Set the method version string in the minfo object
  ii.setMversion([VERSION '-->' ii.mversion]);
  
  % Check if the user supplied a DB connection
  conn = find(pli, 'conn');
  
  % Add default values if necessary
  if isempty(conn)
    pl = combine(pli, ii.plists.pset('HOSTNAME', ''));
  else
    pl = pli;
  end
  
  % Get parameters
  hostname = find(pl, 'hostname');
  database = find(pl, 'database');
  user     = find(pl, 'username');
  passwd   = find(pl, 'password');
  ids      = find(pl, 'id');
  cids     = find(pl, 'cid');
  bin      = find(pl, 'binary');
  
  % Make sure that 'ids' or 'cids' are empty arrays if they are empty.
  % It might be that the GUI return an empty string.
  if isempty(ids)
    ids = [];
  end
  if isempty(cids)
    cids = [];
  end
  
  % Check if some ID is defined
  if isempty(ids) && isempty(cids)
    error('### Please define at least one object ID or connection ID');
  end
  
  %%% check if using binary or not: 'yes'/'no' or true/false or
  %%% 'true'/'false'
  bin = utils.prog.yes2true(bin);
  
  % do we have a connection?
  closeConn = 0;
  if isempty(conn)
    closeConn = 1;
    % Connect to repository
    conn = utils.jmysql.connect(hostname, database, user, passwd);
  end
  if ~isa(conn, 'database') && ~isa(conn, 'mpipeline.repository.RepositoryConnection')
    error('### connection to the database failed.');
  end
  
  if ~isempty(cids)
    for kk=1:numel(cids)
      cid = cids(kk);
      if isa(conn, 'database')
        % get the ids from the cid
        ids = [ids utils.mysql.getObjIds(conn,cid)];
      else
        c_ids = double(mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid));
        ids = [ids c_ids.'];
      end
    end
  end
  
  % Get each ID
  Nids = length(ids);
  objs = [];
  plhout = [];
  
  for kk=1:Nids
    
    %---- copy the input plist because each object should get an other plist
    plh = copy(pl, 1);
    
    %---- This id
    id = ids(kk);
    utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id);
    
    %---- call database constructor
    if bin
      try
        obj = ltpda_uo.retrieve(conn, 'binary', id);
        
      catch
        
        if ~conn.isConnected()
          error('### There is something wrong with the connection.')
        end
        
        warning('!!! Unable to do binary retrieval for object %d; trying to retrieve XML instead.', id);
        obj = ltpda_uo.retrieve(conn, id);
      end
    else
      obj = ltpda_uo.retrieve(conn, id);
    end
    
    if ~strcmp(class(obj), requested_class)
      error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj));
    end
    
    %  - remove connection object from plist first
    if isempty(hostname) || isempty(database)
      if isa(conn, 'database')
        txt = textscan(conn.URL, '%s', 'Delimiter', '/', 'CollectOutput', 1);
        plc = plist('hostname', txt{1}{3}, 'database', txt{1}{4});
      else
        plc = plist('hostname', char(conn.getHostname), 'database', char(conn.getDatabase));
      end
      plh = combine(pli, plc);
    end
    
    %---- remove the connection from the history plist
    if plh.isparam('conn')
      plh.remove('conn');
    end
    
    %---- Set only the ID of the current object to the plist
    plh.pset('ID', id);
    
    %---- Add history-plist to output array
    plhout = [plhout plh];
    
    %---- Add to output array
    objs = [objs obj];
    
  end
  
  % close connection
  if closeConn
    if isa(conn, 'database')
      close(conn);
    else
      conn.closeConnection;
    end
  end
  
end

