Home > m > mysql > ltpda_obj_submit.m

ltpda_obj_submit

PURPOSE ^

LTPDA_OBJ_SUBMIT submits the given collection of objects to an LTPDA Repository.

SYNOPSIS ^

function varargout = ltpda_obj_submit(varargin)

DESCRIPTION ^

 LTPDA_OBJ_SUBMIT submits the given collection of objects to an LTPDA Repository.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: Submits the given collection of objects to an LTPDA Repository.
              The input object(s) will be submitted to the LTPDA Repository
              configured in ltpda_startup.m. If multiple objects are submitted
              together, a corresponding collection entry will be made.

              This is meant to be called by class/submit methods.

 CALL:        [ids, cids] = ltpda_obj_submit(o1, sinfo)
              [ids, cids] = ltpda_obj_submit(o1,o2, sinfo)
              [ids, cids] = ltpda_obj_submit([o1 o2], o3, {o4, o5}, sinfo)

 INPUTS:
            o1,o2,... - input objects to be submitted
            sinfo     - a structure containing the following fields:

                 'conn'                   - database connection object
                 'username'               - the username for the server
                 'experiment_title'       - a title for the submission
                 'experiment_description' - a description of this submission
                 'reference_ids'          - a string containing any
                                            reference object id numbers
                 'additional_comments'    - any additional comments
                 'additional_authors'     - any additional author names

 VERSION:     $Id: ltpda_obj_submit.m,v 1.24 2008/02/24 19:24:31 hewitson Exp $


 HISTORY: 09-05-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = ltpda_obj_submit(varargin)
0002 
0003 % LTPDA_OBJ_SUBMIT submits the given collection of objects to an LTPDA Repository.
0004 %
0005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0006 %
0007 % DESCRIPTION: Submits the given collection of objects to an LTPDA Repository.
0008 %              The input object(s) will be submitted to the LTPDA Repository
0009 %              configured in ltpda_startup.m. If multiple objects are submitted
0010 %              together, a corresponding collection entry will be made.
0011 %
0012 %              This is meant to be called by class/submit methods.
0013 %
0014 % CALL:        [ids, cids] = ltpda_obj_submit(o1, sinfo)
0015 %              [ids, cids] = ltpda_obj_submit(o1,o2, sinfo)
0016 %              [ids, cids] = ltpda_obj_submit([o1 o2], o3, {o4, o5}, sinfo)
0017 %
0018 % INPUTS:
0019 %            o1,o2,... - input objects to be submitted
0020 %            sinfo     - a structure containing the following fields:
0021 %
0022 %                 'conn'                   - database connection object
0023 %                 'username'               - the username for the server
0024 %                 'experiment_title'       - a title for the submission
0025 %                 'experiment_description' - a description of this submission
0026 %                 'reference_ids'          - a string containing any
0027 %                                            reference object id numbers
0028 %                 'additional_comments'    - any additional comments
0029 %                 'additional_authors'     - any additional author names
0030 %
0031 % VERSION:     $Id: ltpda_obj_submit.m,v 1.24 2008/02/24 19:24:31 hewitson Exp $
0032 %
0033 %
0034 % HISTORY: 09-05-07 M Hewitson
0035 %             Creation
0036 %
0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0038 
0039 %% Process inputs
0040 
0041 objs  = {};
0042 sinfo = [];
0043 for j=1:nargin
0044   if  ltpda_isobject(varargin{j})
0045     ins = varargin{j};
0046     if iscell(ins)
0047       for k=1:length(ins)
0048         objs = [objs {ins{k}}];
0049       end
0050     else
0051       for k=1:length(ins)
0052         objs = [objs {ins(k)}];
0053       end
0054     end
0055   elseif isstruct(varargin{j})
0056     sinfo = varargin{j};
0057   else
0058     warning(sprintf('!!! input argument %d will be ignored.', j));
0059   end
0060 end
0061 
0062 if isempty(objs)
0063   error('### Please input at least one LTPDA object.');
0064 end
0065 if isempty(sinfo)
0066   error('### Please provide an input submission structure.');
0067 end
0068 
0069 
0070 %% Some setup
0071 
0072 % Check the comments
0073 check_sinfo(sinfo);
0074 
0075 % make copy of connection object
0076 conn = sinfo.conn;
0077 if isempty(conn)
0078   error('### No valid database connect supplied. Aborting submit process.');
0079 end
0080 
0081 % Machine details
0082 p = provenance;
0083 
0084 %% Process each object and collect id numbers
0085 
0086 disp('*** Submitting objects to repository...');
0087 
0088 try
0089 
0090   % Look-up user id
0091   userid = mysql_getUserID(conn);
0092   disp(sprintf(' ** got user id %d for user: %s', userid, conn.Username))
0093   if userid < 1 || isnan(userid)  || strcmp(userid, 'No Data') || ischar(userid)
0094     error('### Unknown username.');
0095   end
0096 
0097   % The date for the transaction table
0098   t     = time();
0099   tdate = t.time_str;
0100 
0101   ids = [];
0102   for o=1:length(objs)
0103     % this object
0104     obj = objs{o};
0105 
0106     % Check this is a user object that can be submitted
0107     if ltpda_isuserobject(obj)
0108 
0109       % Object name
0110       if isfield(obj, 'name')
0111         name = obj.name;
0112       else
0113         name = '';
0114       end
0115 
0116       % Data object (if present)
0117       if isfield(obj, 'data') && ~isempty(obj.data)
0118         dname = obj.data.name;
0119         dtype = class(obj.data);
0120         ndata = length(obj.data.y);
0121       else
0122         dtype = '';
0123         dname = '';
0124         ndata = 0;
0125       end
0126 
0127       % Creation time
0128       if isfield(obj, 'created')
0129         if isa(obj.created, 'time')
0130           created = obj.created.time_str;
0131         elseif ischar(obj.created)
0132           created = obj.created;
0133         else
0134           error('### Unknown format for created field.');
0135         end
0136       else
0137         created = '1970-01-01 00:00:00';
0138       end
0139       if isempty(created)
0140         created = '1970-01-01 00:00:00';
0141       end
0142 
0143       % Version
0144       if isfield(obj, 'version')
0145         objver = obj.version;
0146       else
0147         objver = '';
0148       end
0149 
0150       %%%%%%%%%%% Convert object to XML
0151       % make pointer to xml document
0152       xml = com.mathworks.xml.XMLUtils.createDocument('ltpda_object');
0153       % extract parent node
0154       parent = xml.getDocumentElement;
0155       % write obj into xml
0156       ltpda_xmlwrite(obj, xml, parent, '');    % Save the XML document.
0157       otxt = xmlwrite(xml); % then to string
0158 
0159       %%%%%%%%%%% Create MD5 hash of object
0160       md5hash = ltpda_hash(otxt, 'MD5');
0161 
0162       %%%%%%%%%%% Submit object to objs table
0163 
0164       try
0165         disp(sprintf('  + Uploading XML data...\f'));
0166         curs = exec(conn, ['insert into objs(xml,hash) values(''' otxt ''', ''' char(md5hash) ''' );']);
0167         close(curs);
0168         disp(sprintf('\bdone.'));
0169       catch
0170         disp(sprintf('### Server returned: %s', curs.Message))
0171         error('### Submission error');
0172       end
0173       
0174       try
0175         objid = getObjID(conn, md5hash);
0176         disp(sprintf('  + submitted object %s with id %d', class(obj), objid));
0177       catch
0178         error('### Failed to get ID of submitted object.');
0179       end
0180       
0181       %%%%%%%%%%% Make objmeta entry
0182 
0183       % For now reference IDs are stored in a string
0184       if ischar(sinfo.reference_ids)
0185         refIds = sinfo.reference_ids;
0186       else
0187         refIds = csv(sinfo.reference_ids);
0188       end
0189       
0190       try
0191         message = ltpda_insert(conn, 'objmeta',...
0192           'obj_id', objid,...
0193           'obj_type', class(obj),...
0194           'name', name,...
0195           'created', created,...
0196           'version', objver,...
0197           'ip', p.ip,...
0198           'hostname', p.hostname,...
0199           'os', p.os,...
0200           'submitted', datestr(now, 31),...
0201           'experiment_title', sinfo.experiment_title,...
0202           'experiment_desc', sinfo.experiment_description,...
0203           'reference_ids', refIds,...
0204           'additional_comments', sinfo.additional_comments,...
0205           'additional_authors', sinfo.additional_authors,...
0206           'keywords', sinfo.keywords, ...
0207           'quantity', sinfo.quantity, ...
0208           'analysis_desc', sinfo.analysis_description...
0209           );
0210 
0211         disp(sprintf('  + made meta-data entry'));
0212       catch
0213         error('### failed to make meta-data entry');
0214       end
0215       
0216       %%%%%%%%%%% Update object tables
0217       eid = update_object_tables(conn, obj, objid);      
0218       %%%%%%%%%%% Update transactions table
0219     
0220       try
0221         message = ltpda_insert(conn, 'transactions',...
0222           'obj_id', objid,...
0223           'user_id', userid,...
0224           'transdate', tdate,...
0225           'direction', 'in'...
0226           );
0227         disp(sprintf('  + updated transactions table'));
0228       catch
0229         error('### Failed to update transactions table');
0230       end
0231       
0232       % Collect this ID
0233       ids = [ids objid];
0234     else
0235       warning('!! Object is not one of the User classes. Not submitting.');
0236       disp(obj);
0237     end
0238   end
0239 
0240   %% Now make collection entry
0241   if ~isempty(ids)
0242     try
0243       message = ltpda_insert(conn, 'collections',...
0244         'nobjs', length(ids),...
0245         'obj_ids', csv(ids)...
0246         );
0247       disp(sprintf(' ** made collection entry'));
0248     catch
0249       error('### Failed to make an entry in the collections table');
0250     end
0251   end
0252 catch
0253   disp('### Submission error - cleaning up.');
0254   rethrow(lasterror)
0255 end
0256 
0257 % Pass back outptus
0258 varargout{1} = ids;
0259 varargout{2} = getCollectionID(conn, ids);
0260 
0261 disp('*** submission complete.');
0262 
0263 end
0264 
0265 
0266 %--------------------------------------------------------------------------
0267 %
0268 %  Update entries for various object tables
0269 %
0270 % eid = update_object_tables(conn, obj)
0271 % eid = update_object_tables(conn, obj, objid)
0272 %
0273 function  eid = update_object_tables(varargin)
0274 
0275   if nargin < 2
0276     error('### Incorrect usage.');
0277   end
0278   conn = varargin{1};
0279   obj  = varargin{2};
0280   if nargin == 3
0281     objid = varargin{3};
0282   else
0283     objid = [];
0284   end
0285 
0286   % Class of object
0287   cl = class(obj);
0288   disp(sprintf('  + making meta data entry for %s', cl));
0289   eid = [];
0290 
0291   switch cl
0292     %------------------ AO
0293     case 'ao'
0294       
0295       %-- First we need to insert the data object info
0296       did = update_object_tables(conn, obj.data);
0297 
0298       % Now insert the AO info
0299       try
0300         message = ltpda_insert(conn, 'ao',...
0301           'obj_id', objid,...
0302           'data_type', class(obj.data),...
0303           'data_id', did, ...
0304           'description', obj.description, ...
0305           'mfilename', obj.mfilename, ...
0306           'mdlfilename', obj.mdlfilename...
0307           );
0308         disp(sprintf('  + made meta-data entry for %s', cl));
0309         % get the entry id
0310         eid = getObjID(conn, '');
0311       catch
0312         error('### failed to make meta-data entry for %s table', cl);
0313       end
0314       
0315     %------------------ CSDATA
0316     case 'cdata'
0317       % Make the meta-data entry
0318       try
0319         message = ltpda_insert(conn, 'cdata',...
0320           'xunits', obj.xunits,...
0321           'yunits', obj.yunits ...
0322           );
0323         disp(sprintf('  + made meta-data entry for %s', cl));        
0324         % get the entry id
0325         eid = getObjID(conn, '');        
0326       catch
0327         error('### failed to make meta-data entry for %s table', cl);
0328       end
0329     %------------------ FSDATA
0330     case 'fsdata'
0331       % Make the meta-data entry
0332       try
0333         message = ltpda_insert(conn, 'fsdata',...
0334           'xunits', obj.xunits,...
0335           'yunits', obj.yunits,...
0336           'fs', obj.fs ...
0337           );
0338         disp(sprintf('  + made meta-data entry for %s', cl));        
0339         % get the entry id
0340         eid = getObjID(conn, '');        
0341       catch
0342         error('### failed to make meta-data entry for %s table', cl);
0343       end
0344     %------------------ MFIR
0345     case 'mfir'
0346       % Make the meta-data entry
0347       try
0348         message = ltpda_insert(conn, 'mfir',...
0349           'obj_id', objid,...
0350           'in_file', obj.infile,...
0351           'fs', obj.fs ...
0352           );
0353         disp(sprintf('  + made meta-data entry for %s', cl));        
0354         % get the entry id
0355         eid = getObjID(conn, '');        
0356       catch
0357         error('### failed to make meta-data entry for %s table', cl);
0358       end
0359     %------------------ MIIR
0360     case 'miir'
0361       % Make the meta-data entry
0362       try
0363         message = ltpda_insert(conn, 'miir',...
0364           'obj_id', objid,...
0365           'in_file', obj.infile,...
0366           'fs', obj.fs ...
0367           );
0368         disp(sprintf('  + made meta-data entry for %s', cl));        
0369         % get the entry id
0370         eid = getObjID(conn, '');        
0371       catch
0372         error('### failed to make meta-data entry for %s table', cl);
0373       end
0374     %------------------ TSDATA
0375     case 'tsdata'      
0376       % Make the meta-data entry
0377       try
0378         message = ltpda_insert(conn, 'tsdata',...
0379           'xunits', obj.xunits,...
0380           'yunits', obj.yunits,...
0381           'fs', obj.fs,...
0382           'nsecs', obj.nsecs,...
0383           't0', obj.t0.time_str ...
0384           );
0385         disp(sprintf('  + made meta-data entry for %s', cl));        
0386         % get the entry id
0387         eid = getObjID(conn, '');        
0388       catch
0389         error('### failed to make meta-data entry for %s table', cl);
0390       end      
0391     %------------------ XYDATA
0392     case 'xydata'
0393       % Make the meta-data entry
0394       try
0395         message = ltpda_insert(conn, 'xydata',...
0396           'xunits', obj.xunits,...
0397           'yunits', obj.yunits ...
0398           );
0399         disp(sprintf('  + made meta-data entry for %s', cl));        
0400         % get the entry id
0401         eid = getObjID(conn, '');        
0402       catch
0403         error('### failed to make meta-data entry for %s table', cl);
0404       end
0405     otherwise
0406       disp(sprintf('   # no special table for objects of type %s', cl));
0407   end
0408   
0409   if ~isempty(eid)
0410     disp(sprintf('  + made meta-data entry in %s table with id %d', cl, eid));
0411   end
0412 end
0413 
0414 %--------------------------------------------------------------------------
0415 %
0416 function id = getObjID(conn, hash)
0417 
0418 try
0419   q = sprintf('select last_insert_id()');
0420   curs = exec(conn, q);
0421   curs = fetch(curs);
0422   id  = curs.Data{1};
0423   close(curs);
0424 catch
0425   error('### Failed to get object ID. Server returned %s', curs.Message);
0426 end
0427 
0428 end
0429 
0430 %--------------------------------------------------------------------------
0431 %
0432 function Cid = getCollectionID(conn, objids)
0433 
0434 try
0435   curs = exec(conn, sprintf('select id from collections where obj_ids="%s"', csv(objids)));
0436   curs = fetch(curs);
0437   Cid  = curs.Data{1};
0438   close(curs);
0439 catch
0440   error('### Failed to get Collection ID. Server returned %s', curs.Message);
0441 end
0442 
0443 if strcmp(Cid, 'No Data')
0444   Cid = [];
0445 end
0446 
0447 end
0448 
0449 %--------------------------------------------------------------------------
0450 % make comma separated list of numbers
0451 function s = csv(x)
0452 
0453 s = sprintf('%g,', x);
0454 s = s(1:end-1);
0455 
0456 end
0457 
0458 %--------------------------------------------------------------------------
0459 % check sinfo structure
0460 %
0461 function check_sinfo(sinfo)
0462 
0463 
0464 % --- check fieldnames
0465 manfields = {'conn', 'experiment_title', 'experiment_description', 'analysis_description'};
0466 if ~isstruct(sinfo)
0467   sinfo_description();
0468 end
0469 
0470 % get input fieldnames
0471 fnames = fieldnames(sinfo);
0472 
0473 % check these
0474 for jj=1:length(manfields)
0475   if ~ismember(fnames, manfields{jj})
0476     for kk=1:length(manfields)
0477       disp(sprintf('Required Field: %s', manfields{kk}));
0478     end
0479     error('The sinfo structure should contain at least the above fields.');
0480   end
0481 end
0482 
0483 % Check other fields exist
0484 if ~isfield(sinfo, 'quantity')
0485   sinfo.quantity = '';
0486 end
0487 if ~isfield(sinfo, 'keywords')
0488   sinfo.keywords = '';
0489 end
0490 if ~isfield(sinfo, 'reference_ids')
0491   sinfo.reference_ids = '';
0492 end
0493 if ~isfield(sinfo, 'additional_comments')
0494   sinfo.additional_comments = '';
0495 end
0496 if ~isfield(sinfo, 'additional_authors')
0497   sinfo.additional_authors = '';
0498 end
0499 
0500 %---- Additional checks
0501 if length(sinfo.experiment_title) < 5
0502   error('### Experiment title is mandatory and should be at least 5 characters long.');
0503 end
0504 if length(sinfo.experiment_description) < 10
0505   error('### Experiment description is mandatory and should be more that 10 characters long.');
0506 end
0507 if length(sinfo.analysis_description) < 10
0508   error('### Analysis description is mandatory and should be more that 10 characters long.');
0509 end
0510 
0511 disp('*** sinfo structure is valid.');
0512 
0513 end
0514 
0515 % Throw description of sinfo to terminal
0516 function sinfo_description
0517 disp('The sinfo structure should look like:');
0518 disp('');
0519 disp('  sinfo.conn                   - a database connection object');
0520 disp('  sinfo.experiment_title       - the experiment title       (Mandatory, >5 characters)');
0521 disp('  sinfo.experiment_description - the experiment description (Mandatory, >10 characters)');
0522 disp('  sinfo.analysis_description   - the analysis description   (Mandatory, >10 characters)');
0523 disp('  sinfo.quantity               - the physical quantity represented by the data');
0524 disp('  sinfo.keywords               - a comma-delimeted list of keywords');
0525 disp('  sinfo.reference_ids          - IDs of other applicable LTPDA objects in the repository');
0526 disp('  sinfo.additional_comments    - any additional comments');
0527 disp('  sinfo.additional_authors     - any additional authors');
0528 disp('');
0529 error('### Incorrect specification for sinfo.')
0530 end
0531 
0532 % END

Generated on Fri 07-Mar-2008 15:46:43 by m2html © 2003