view m-toolbox/classes/@ltpda_uo/fromRepository.m @ 22:b11e88004fca database-connection-manager

Update collection.fromRepository
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents 69e3d49b4b0c
children 056f8e1e995e
line wrap: on
line source

% 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, pl, 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]);
    
  % Get parameters
  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);

  % database connection
  conn = LTPDADatabaseConnectionManager().connect(pl);
  
  if ~isempty(cids)
    for kk=1:numel(cids)
      cid = cids(kk);
      % get the ids from the cid
      ids = [ids utils.repository.getCollectionIDs(conn, cid)];
    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);
    
    try
      %---- call database constructor
      if bin
        obj = ltpda_uo.retrieve(conn, 'binary', id);
      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 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];
      
    catch ex
      % close connection if we own it
      if isempty(find(pl, 'conn'))
        conn.close();
      end
      throw(ex)
    end
      
  end
  
  % close connection if we own it
  if isempty(find(pl, 'conn'))
    conn.close();
  end
  
end