view m-toolbox/classes/@workspaceBrowser/cb_query.m @ 44:409a22968d5e default

Add unit tests
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Tue, 06 Dec 2011 18:42:11 +0100
parents f0afece42f48
children 317b5f447f3e
line wrap: on
line source

% CB_QUERY queries an LTPDA repository
%
% CALL: workspaceBrowser.cb_query
%
% M Hewitson 13-11-08
%
% $Id: cb_query.m,v 1.12 2010/07/23 15:09:45 ingo Exp $
%
function cb_query(varargin)
  
  wb = getappdata(0, 'WorkspaceBrowser');
  
  % Get a connection from the repository manager
  rm = LTPDARepositoryManager;
  conn = rm.manager.selectConnection([]);
  
  if ~isempty(conn)
    % Update
    if ~isempty(rm.gui)
      awtinvoke(rm.gui, 'reloadConnectionTable()');
    end
    % Get submission info from the user
    rqd = awtcreate('mpipeline.repository.RepositoryQueryDialog', 'Lmpipeline.main.MainWindow;ZLmpipeline.repository.RepositoryConnection;Z', [], false, conn, false);
    awtinvoke(rqd, 'setVisible', 'true');
    
    hExec = handle(rqd.getExecuteBtn(),'callbackproperties');
    hExec.ActionPerformedCallback = @cb_executeQuery;
    
    hGuiCloses1 = handle(rqd, 'callbackproperties');
    hGuiCloses1.WindowClosedCallback = @cb_guiClosed1;
    
  end
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function cb_guiClosed1(varargin)
    % Delete callback handles
    if ishandle(hExec)
      delete(hExec)
    end
    if ishandle(hGuiCloses1)
      delete(hGuiCloses1)
    end
    
    % Delete all variables
    wb   = [];
    rm   = [];
    conn = [];
    rqd  = [];
  end
  
  function cb_executeQuery(varargin)
    
    jQueryTextField = awtinvoke(rqd, 'getQueryTxtField()');
    jQuery = awtinvoke(jQueryTextField, 'getText()');
    
    if ~(awtinvoke(conn, 'isConnected()'))
      awtinvoke(conn, 'openConnection()');
    end
    
    if (awtinvoke(conn, 'isConnected()'))
      
      % lock connection
      awtinvoke(conn, 'setLocked(Z)', true);
      jStmt = awtinvoke(conn, 'createStatement()');
      
      try
        
        jResult = awtinvoke(jStmt, 'executeQuery(Ljava.lang.String;)', jQuery);
        
        % pass to query results table
        qrt = awtcreate('mpipeline.repository.QueryResultsTableDialog', 'Lmpipeline.main.MainWindow;ZLjava.sql.ResultSet;Ljava.lang.String;Z', [], false, jResult, jQuery, false);
        awtinvoke(qrt, 'setUsedConn(Lmpipeline.repository.RepositoryConnection;)', conn);
        awtinvoke(qrt, 'setVisible(Z)', 'true');
        
        hRetrieve = handle(qrt.getCreateConstructors,'callbackproperties');
        hRetrieve.ActionPerformedCallback = @cb_retrieveObjectsFromTable;
        
        hGuiCloses2 = handle(qrt, 'callbackproperties');
        hGuiCloses2.WindowClosedCallback = @cb_guiClosed2;
        
      catch err
        fprintf(2, [err.message, '\n']);
      end
      awtinvoke(conn, 'setLocked(Z)', false);
      
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function cb_guiClosed2(varargin)
      % Delete callback handles
      if ishandle(hRetrieve)
        delete(hRetrieve)
      end
      if ishandle(hGuiCloses2)
        delete(hGuiCloses2)
      end
      
      % Delete all variables
      jQueryTextField = [];
      jQuery          = [];
      jStmt           = [];
      jResult         = [];
      qrt             = [];
    end
    
    function cb_retrieveObjectsFromTable(varargin)
      
      try
        jResultTable = awtinvoke(qrt, 'getResultsTable()');
        jColNames    = awtinvoke(qrt, 'getColNames()');
        jObj_id_col  = awtinvoke(jColNames, 'indexOf(Ljava.lang.Object;)', 'obj_id');
        
        if (jObj_id_col == -1)
          msg = 'The results must contain the field ''obj_id'' in order to create constructor blocks.';
          utils.helper.errorDlg(msg, 'Fields error');
          jResultTable = [];
          jColNames    = [];
          jObj_id_col  = [];
          error(msg);
        end
        
        numberStr = '';
        jRows = double(awtinvoke(jResultTable, 'getSelectedRows()'));
        
        jModel = [];
        if numel(jRows) >= 1
          jModel = awtinvoke(jResultTable, 'getModel()');
          numberStr = num2str(awtinvoke(jModel, 'getValueAt(II), ', jRows(1), jObj_id_col));
        end
        
        for kk = 2:numel(jRows)
          jModel = awtinvoke(jResultTable, 'getModel()');
          
          objIDstr = num2str( awtinvoke(jModel, 'getValueAt(II)', jRows(kk), jObj_id_col));
          numberStr = [numberStr, ' ', objIDstr];
        end
        
        rrd = awtcreate('mpipeline.repository.RepositoryRetrieveDialog', 'Ljava.awt.Frame;Z', [], true);
        
        jObjIDTextField = awtinvoke(rrd, 'getObjectIDsTextField()');
        awtinvoke(jObjIDTextField, 'setText(Ljava.lang.String;)', numberStr);
        awtinvoke(rrd, 'setVisible(Z)', true);
        
        hGuiCloses3 = handle(rrd, 'callbackproperties');
        hGuiCloses3.WindowClosedCallback = @cb_guiClosed3;
        
      catch err
        fprintf(2, [err.message, '\n']);
      end
      
      function cb_guiClosed3(varargin)
        % Delete callback handles
        if ishandle(hGuiCloses3)
          delete(hGuiCloses3)
        end
        
        if ~rrd.isCancelled
          workspaceBrowser.retrieveObjectsFromDialog(rrd, conn);
        end
        
        % Delete all variables
        jResultTable    = [];
        jColNames       = [];
        jObj_id_col     = [];
        jRows           = [];
        jModel          = [];
        rrd             = [];
        jObjIDTextField = [];
      end
      
    end % cb_retrieveObjectsFromTable
    
  end % cb_executeQuery
  
end % cb_query