% SUBMITDIALOG a submission dialog for submitting objects an LTPDA repository.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SUBMITDIALOG a submission dialog for submitting objects an
%              LTPDA repository.
%
% CALL:        sinfo = utils.jmysql.submitDialog();
%              sinfo = utils.jmysql.submitDialog(conn);
%              sinfo = utils.jmysql.submitDialog(conn,sinfo);
%
% INPUTS:
%              conn  - a connection object like that returned from
%                      utils.jmysql.connect.
%              sinfo - an sinfo structure to pre-populate the entry fields
%
% OUTPUTS: 
%              sinfo - an sinfo structure suitable to pass to submit
% 
% NOTE: the sinfo object contains a valid and connected database connection
% object. It is up to the caller to close this connection when it is no
% longer required.
% 
% NOTE: if an input connection is provided, this is not closed by this
% process.
%
% VERSION:     $Id: submitDialog.m,v 1.4 2009/10/02 09:19:50 hewitson Exp $
%
% HISTORY:     24-05-2007 M Hewitson
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function sinfo = submitDialog(varargin)
  
  if nargin > 0 && ~isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')
    error('### The first argument should be empty or a RepositoryConnection');
  end
  
  isinfo = [];
  
  if nargin == 2 && isstruct(varargin{2})
    isinfo = varargin{2};
  end
  
  % Inputs
  conn = [];
  if nargin > 0
    conn  = varargin{1};
  end

  % Check the connection
  if ~isempty(conn)
    closeConnection = false;
    if ~conn.isConnected
      conn.openConnection
    end
    if ~conn.isConnected
      error('### Failed to connect to repository.');
    end
  else
    closeConnection = true;
  end
  
  % Get host list
  prefs = getappdata(0, 'LTPDApreferences');
  hosts = java.util.ArrayList();
  servers = prefs.repository.servers;
  for kk=1:numel(servers)
    hosts.add(servers{kk});
  end
  
  jisinfo = [];
  if ~isempty(isinfo) 
    
    jisinfo = mpipeline.repository.SubmissionInfo([]);
    jisinfo.setExperimentTitle(isinfo.experiment_title);
    jisinfo.setExperimentDescription(isinfo.experiment_description);
    jisinfo.setAnalysisDescription(isinfo.analysis_description);
    jisinfo.setQuantity(isinfo.quantity);
    jisinfo.setKeywords(isinfo.keywords);
    jisinfo.setReferenceIDs(isinfo.reference_ids);
    jisinfo.setAdditionalComments(isinfo.additional_comments);
    jisinfo.setAdditionalAuthors(isinfo.additional_authors);
    
  end
  
  % Open submit dialog
  warning('off', 'MATLAB:JavaEDTAutoDelegation');
  srd = mpipeline.repository.SubmitInfoDialog([], jisinfo, hosts, conn);
  srd.setVisible(true)
  warning('on', 'MATLAB:JavaEDTAutoDelegation');
  
  sinfo = [];
  if ~srd.isCancelled
    
    jsinfo = srd.getSubmissionInfo;
    sinfo.experiment_title       = char(jsinfo.getExperimentTitle);
    sinfo.experiment_description = char(jsinfo.getExperimentDescription);
    sinfo.analysis_description   = char(jsinfo.getAnalysisDescription);
    sinfo.quantity               = char(jsinfo.getQuantity);
    sinfo.keywords               = char(jsinfo.getKeywords);
    sinfo.reference_ids          = char(jsinfo.getReferenceIDs);
    sinfo.additional_comments    = char(jsinfo.getAdditionalComments);
    sinfo.additional_authors     = char(jsinfo.getAdditionalAuthors);
    
    jconn = srd.getRepoConnection;    
    sinfo.conn = jconn; %utils.jmysql.jconnTodbconn(jconn);    
    if closeConnection
      jconn.closeConnection;
    end
  end
  
end

