view m-toolbox/classes/@LTPDARepositoryManager/copyObjects.m @ 2:18e956c96a1b database-connection-manager

Add LTPDADatabaseConnectionManager implementation. Matlab code
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Sun, 04 Dec 2011 21:23:09 +0100
parents f0afece42f48
children
line wrap: on
line source

% COPYOBJECTS This function copies objects from one repository to another.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% description: This static function copies objects from one repository to another.
%
% call:        LTPDARepositoryManager.copyObjects(pl);
%              LTPDARepositoryManager.copyObjects([1 2 3],
%                                                 'hostname1',
%                                                 'database1',
%                                                 'username1',
%                                                 'hostname2',
%                                                 'database2',
%                                                 'username2');
%
% <a href="matlab:web(LTPDARepositoryManager.getInfo('LTPDARepositoryManager.copyObjects').tohtml, '-helpbrowser')">Parameters Description</a>
%
% version:    $Id: copyObjects.m,v 1.5 2011/04/08 08:56:35 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = copyObjects(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');
  hostname1 = pl.find('hostname1');
  database1 = pl.find('database1');
  username1 = pl.find('username1');
  hostname2 = pl.find('hostname2');
  database2 = pl.find('database2');
  username2 = pl.find('username2');
  
  % Check through 'rest'
  if numel(rest) > 0
    ids = rest{1};
  end
  if numel(rest) > 1
    hostname1 = rest{2};
  end
  if numel(rest) > 2
    database1 = rest{3};
  end
  if numel(rest) > 3
    username1 = rest{4};
  end
  if numel(rest) > 4
    hostname2 = rest{5};
  end
  if numel(rest) > 5
    database2 = rest{6};
  end
  if numel(rest) > 6
    username2 = rest{7};
  end
  
  % Some plausibility checks
  if isempty(ids)
    error('### This method needs at least one object id which you want to copy.');
  end
  
  % Get the repository manager - there should only be one!
  rm = LTPDARepositoryManager();
  
  % Get connection
  conn1 = rm.findConnections(hostname1, database1, username1);
  if numel(conn1) == 0
    conn1 = rm.newConnection(hostname1, database1, username1);
  elseif numel(conn1) > 1
    conn1 = rm.manager.selectConnection([]);
  end
  
  if isempty(conn1)
    error('### It is necessary to create or select a connection.');
  end
  
  % open connection
  conn1.openConnection();
  if ~conn1.isConnected()
    error('### Can not open the connection.');
  end
  
  try
    
    objs = ltpda_uo.retrieve(conn1, ids);
    if ~iscell(objs)
      objs = {objs};
    end
    sinfo = LTPDARepositoryManager.getSinfo(ids, ...
      char(conn1.getHostname), char(conn1.getDatabase), char(conn1.getUsername));
    
    pl = plist('hostname', hostname2, 'database', database2, 'username', username2, 'no dialog', true);
    for oo = 1:numel(objs)
      obj = objs{oo};
      obj.submit(sinfo(oo), pl);
    end
  catch Exception
    disp(Exception.message);
    error('### Copying failed.');
  end
  
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: copyObjects.m,v 1.5 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 want to copy.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  % hostname1
  p = param({'hostname1', 'The hostname of the ''old'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % database1
  p = param({'database1', 'The database of the ''old'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % username1
  p = param({'username1', 'The username for the ''old'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % hostname2
  p = param({'hostname2', 'The hostname of the ''new'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % database2
  p = param({'database2', 'The database of the ''new'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % username2
  p = param({'username2', 'The username for the ''new'' repository.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
end