view m-toolbox/classes/@ltpda_uo/fromRepository.m @ 46:ca0b8d4dcdb6 database-connection-manager

Fix
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 19:07:27 +0100
parents 056f8e1e995e
children
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 download
  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

      %---- Set connection parameters in the plist
      utils.repository.adjustPlist(conn, plh);

      %---- 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