


function retrieveObjectsFromDialog(varargin)

  if ~isa(varargin{1}, 'mpipeline.repository.RepositoryRetrieveDialog')
    error('### The first input must be a mpipeline.repository.RepositoryRetrieveDialog');
  end
  if ~isa(varargin{2}, 'mpipeline.repository.RepositoryConnection')
    error('### The first input must be a mpipeline.repository.RepositoryConnection');
  end
    
  qb   = varargin{1};
  conn = varargin{2};
  
  % Get object IDs
  obj_ids = qb.getObjectIDs;
  col_ids = qb.getCollectionIDs;
  
  if isempty(obj_ids) && isempty(col_ids)
    utils.helper.errorDlg('Please enter either a object ID or a collection ID.');
    return
  end
  
  % object prefix
  obj_prefix = char(qb.getObjectPrefix);
  
  % append object type?
  appendObjectType = qb.appendObjectType;
  
  % binary retrieval?
  binaryRetrieval = qb.useBinaryRetrieval;
  
  % file extension
  fileext = qb.getSaveFileExtension;
  
  [objs, obj_names] = retrieveObjects(conn, obj_ids, col_ids, binaryRetrieval, obj_prefix, appendObjectType);
  
  if qb.isSaveObjects
    save_objects(objs, obj_names, fileext);
  else
    import_objects(objs, obj_names);
  end
  
end

function save_objects(objs, obj_names, fileext)
  for j=1:length(objs)
    if isvarname(obj_names{j})
      save(objs{j}, [obj_names{j} char(fileext)]);
    else
      utils.helper.errorDlg('Can not save the object(s) because you used a not valid prefix name.');
    end
  end
end

function import_objects(objs, obj_names)
  for j=1:length(objs)
    if isvarname(obj_names{j})
      assignin('base', obj_names{j}, objs{j});
    else
      utils.helper.errorDlg('Can not import the object(s) because you used a not valid prefix name.');
    end
  end
end

function [objs, obj_names] = retrieveObjects(conn, ids, cids, retrieveBinary, prefix, appendObj)
  
  
  %---------------------------------------------------------------
  % Retrieve these ids
  objs = {};
  obj_names = {};
  for j=1:length(ids)
    disp(sprintf('+ retrieving object %d', ids(j)));
    
    % determine object type
    tt = char(mpipeline.repository.MySQLUtils.getObjectTypeForID(conn, ids(j)));
    
    if isempty(tt) || strcmp(tt, 'No Data')
      utils.helper.errorDlg('Object type is unknown. Does this object really exist?');
      return
    end
    
    objname = sprintf('%s%03d', prefix, ids(j));
    if appendObj
      objname = [objname '_' tt];
    end
    obj_names = [obj_names {objname}];
    
    % Retrieve object
    hostname = char(conn.getHostname);
    db = char(conn.getDatabase);
    % add history
    pl = plist('hostname', hostname, 'database', db, 'ID', ids(j), 'conn', conn);
    if retrieveBinary
      pl.append('Binary', 'yes');
      disp(sprintf('*** performing binary retrieval.'));
    end
    obj = eval(sprintf('%s(pl);', tt));
    
    objs = [objs {obj}];
  end
  
  %---------------------------------------------------------------
  % Retrieve these Collections
  for k=1:length(cids)
    
    % get Ids from Cid
    ids = mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cids(k));
    if isempty(ids)
      error('### This collection doesn''t seem to exist.');
    end
    
    for j=1:length(ids)
      disp(sprintf('+ retrieving collection %d : %d', cids(k), ids(j)));
      tt = char(mpipeline.repository.MySQLUtils.getObjectTypeForID(conn, ids(j)));
      if ismember(tt, utils.helper.ltpda_userclasses)
        % Retrieve object
        if isa(conn, 'database')
          ipbits = regexp(conn.URL, '([0-9]+)', 'match');
          ip = [ipbits{1} '.' ipbits{2} '.' ipbits{3} '.' ipbits{4}];
          db = regexp(conn.URL, '/', 'split');
          db = db{end};
        else
          ip = conn.getHostname;
          db = conn.getDatabase;
        end
        % add history
        pl = plist('hostname', ip, 'database', db, 'ID', ids(j), 'conn', conn);
        obj = eval(sprintf('%s(pl);', tt));
        
        objname = sprintf('%sC%03d_%03d', prefix, cids(k), ids(j));
        if appendObj
          objname = [objname '_' tt];
        end
        obj_names = [obj_names {objname}];
        objs = [objs {obj}];
      else
        warning('!!! Objects of type %s are no longer considered user objects and can not be retrieved.', tt);
      end
    end
  end
  
end

