view m-toolbox/classes/@repogui/cb_importBtn.m @ 31:a26669b59d7e database-connection-manager

Update LTPDAworkbench
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

% CB_IMPORTBTN callback executed when the user clicks on the import button.
%
% M Hewitson
%
% $Id: cb_importBtn.m,v 1.4 2009/09/11 18:23:36 ingo Exp $
%
function cb_importBtn(varargin)


  % Get connection
  mainfig  = varargin{end};
  conn     = mainfig.connection;
  if isempty(conn) || ~isa(conn, 'database')
    utils.helper.errorDlg('Please connect to a database first', 'No connection found');
    return
  end

  % Get variable name specs
  h = findobj(mainfig.handle, 'Tag', 'objPrefixTxt');
  prefix = get(h, 'String');
  if isempty(prefix)
    prefix = 'obj';
    warning('! Using default prefix (obj)');
  end
  if ~strcmp(prefix,genvarname(prefix))
     prefix = 'obj';
     warning('Warning: ! The given prefix will be ignored, being not a valid variable name: default ''obj'' will be used instead')
  end
  h = findobj(mainfig.handle, 'Tag', 'appendObjTypeChk');
  appendObj = get(h, 'Value');
  h = findobj(mainfig.handle, 'Tag', 'retrieveBinaryChk');
  retrieveBinary = get(h, 'Value');

  % Get IDs from text box
  [ids, cids] = getIds(mainfig);


  %---------------------------------------------------------------
  % Retrieve these ids
  objs = [];
  for j=1:length(ids)
    disp(sprintf('+ retrieving object %d', ids(j)));

    % determine object type
    tt = utils.mysql.getObjType(conn, ids(j));
    objname = sprintf('%s%03d', prefix, ids(j));
        
    if ~isempty(tt) && ~strcmp(tt, 'No Data')
      if appendObj
        objname = [objname '_' tt];
      end
    else
      error('!!! Object type is unknown. Does this object really exist?');
    end

    % Retrieve object
    a = regexp(conn.URL, '//(\S+)/', 'tokens');
    db = regexp(conn.URL, '/', 'split');
    db = db{end};
    % add history
    pl = plist('hostname', a{1}, '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));
        
    assignin('base', objname, obj);
    disp(sprintf('** Retrieve object %d to workspace [%s]', ids(j), objname));

    if j==1
      objs = {obj};
    else
      objs = [objs {obj}];
    end
  end

  %---------------------------------------------------------------
  % Retrieve these Collections
  for k=1:length(cids)

    % get Ids from Cid
    ids = utils.mysql.getObjIds(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 = utils.mysql.getObjType(conn, ids(j));
      if ismember(tt, utils.helper.ltpda_userclasses)
        objname = sprintf('%sC%03d_%03d', prefix, cids(k), ids(j));
        if appendObj
          objname = [objname '_' tt];
        end
        % Retrieve object
        ipbits = regexp(conn.URL, '([0-9]+)', 'match');
        ip = [ipbits{1} '.' ipbits{2} '.' ipbits{3} '.' ipbits{4}];
        db = regexp(conn.URL, '/', 'split');
        db = db{end};
        % add history
        pl = plist('hostname', ip, 'database', db, 'ID', ids(j), 'conn', conn);
        obj = eval(sprintf('%s(pl);', tt));

        assignin('base', objname, obj);
        disp(sprintf('** Retrieve object %d to workspace [%s]', ids(j), objname));
        if j==1
          objs = {obj};
        else
          objs = [objs {obj}];
        end
      else
        warning('!!! Objects of type %s are no longer considered user objects and can not be retrieved.', tt);
      end
    end
  end

  disp(sprintf('*** Retrieved %d objects.', length(objs)));
  status  = findobj(mainfig.handle, 'Tag', 'RepoguiStatusTxt');
  set(status, 'String', sprintf('*** Retrieved %d objects.', length(objs)),'ForeGroundcolor','b');


end


function [ids, cids] = getIds(mainfig)

  ids  = [];
  cids = [];

  th = findobj(mainfig.handle, 'Tag', 'retrieveIDsTxt');
  idStr = get(th, 'String');
  cs = cellstr(idStr);

  for j=1:length(cs)
    disp('---------')
    ls = cs{j};
    [s,r] = strtok(ls);
    if ~isempty(s)
      if s(1) == 'c'
        s = s(2:end);
        cids = [cids round(str2num(s))];
      else
        ids = [ids round(str2num(s))];
      end
    end
    while ~isempty(r)
      [s,r] = strtok(r);
      if ~isempty(s)
        if s(1) == 'c'
          s = s(2:end);
          cids = [cids round(str2num(s))];
        else
          ids = [ids round(str2num(s))];
        end
      end
    end
  end

end