% CB_SUBMIT callback executed when the user clicks on the 'Submit' button
%
% M Hewitson
%
% $Id: cb_submit.m,v 1.5 2009/05/11 17:09:17 nicola Exp $
%
function cb_submit(varargin)


  myh     = varargin{1};
  mainfig = varargin{end};
  status  = findobj(mainfig.handle, 'Tag', 'RepoguiStatusTxt');
  submitButton = findobj('Tag', 'repoguiSubmitButton');

  % get object list
  objlist = findobj(mainfig.handle, 'Tag', 'RepoguiObjs2SubmitList');
  objstr  = get(objlist, 'String');

  % Build sinfo structure
  sinfo.conn                   = mainfig.connection;
  sinfo.experiment_title       = get(findobj(mainfig.handle, 'Tag', 'RepoguiExperimentTitleEdit'), 'String');
  sinfo.experiment_description = get(findobj(mainfig.handle, 'Tag', 'RepoguiExperimentDescriptionEdit'), 'String');
  sinfo.analysis_description   = get(findobj(mainfig.handle, 'Tag', 'RepoguiAnalysisDescriptionEdit'), 'String');
  sinfo.quantity               = get(findobj(mainfig.handle, 'Tag', 'RepoguiQuantityEdit'), 'String');
  sinfo.keywords               = get(findobj(mainfig.handle, 'Tag', 'RepoguiKeywordsEdit'), 'String');
  sinfo.reference_ids          = get(findobj(mainfig.handle, 'Tag', 'RepoguiReferenceIDsEdit'), 'String');
  sinfo.additional_comments    = get(findobj(mainfig.handle, 'Tag', 'RepoguiAdditonalCommentsEdit'), 'String');
  sinfo.additional_authors     = get(findobj(mainfig.handle, 'Tag', 'RepoguiAdditionalAuthorsEdit'), 'String');

  if ~isa(sinfo.conn, 'database')
    error('### Please connect to a database before trying to submit.');
  end

  objs = [];
  objname = '';
  
  if isempty(objstr)
     error('### There are no objects selected to be submitted.');
  end
  
  % Remove all incorrect lines
  jj = 1;
  while jj<=numel(objstr)
     try
        cmd = sprintf('obj = evalin(''base'', ''%s'');', objstr{jj});
        eval(cmd);
        objs = [objs ; obj];
        jj = jj+1;
     catch
        warning(['### The submission ignored the input on line ' num2str(jj) ', ''' objstr{jj} ''', because there''s no such variable, or it has a different class with respect to the first object selected.' ]) %#ok<WNTAG>
        objstr(jj) = [];
     end
  end
  
  % Put together the objects to submit
  objs = [];
  for j=1:numel(objstr)
     if ~isempty(objname), objname = [objname ', ']; end
     cmd = sprintf('obj = evalin(''base'', ''%s'');', objstr{j});
     eval(cmd);
     objname = [objname objstr{j}];
     objs = [objs ; obj];
  end
  
  % Submit objects
  try
    set(submitButton,'String','Submitting...')
    set(status,'String','++ Submitting object(s)','ForeGroundcolor','r')
    drawnow
    [ids, cid] = submit(objs, sinfo);
    if isempty(ids)
      warning('!!! Failed to submit object: %s', objname);
      set(status,'String',sprintf('!!! Failed to submit object: %s', objname),'ForeGroundcolor','r');
    else
      disp(sprintf('++ Submitted object(s) %s (ref id = %s, collection id = %d)', objname, mat2str(ids), cid));
      set(status,'String',sprintf('++ Submitted object(s) %s (ref id = %s, collection id = %d)', objname, mat2str(ids), cid),'ForeGroundcolor','b');
    end
    set(submitButton,'String','Submit')
  catch
    set(status,'String',sprintf('!!! Failed to submit object: %s', objname));
    set(submitButton,'String','Submit')
    warning('!!! Failed to submit object: %s', objname);
    rethrow(lasterror)
  end
  
end
