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, 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

 *NOTE: The input objects must be all of the same class.
 
 VERSION:     $Id: submit.m,v 1.14 2007/08/16 06:55:25 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, 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 % *NOTE: The input objects must be all of the same class.
0032 %
0033 % VERSION:     $Id: submit.m,v 1.14 2007/08/16 06:55:25 hewitson Exp $
0034 %
0035 %
0036 % HISTORY: 09-05-07 M Hewitson
0037 %             Creation
0038 %
0039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0040 
0041 %% Process inputs
0042 
0043 objs  = [];
0044 sinfo = [];
0045 for j=1:nargin
0046   if  ltpda_isobject(varargin{j})    
0047     objs = [objs varargin{j}];    
0048   elseif isstruct(varargin{j})
0049     sinfo = varargin{j};
0050   else
0051     warning(sprintf('!!! input argument %d will be ignored.', j));
0052   end
0053 end
0054 
0055 if isempty(objs)
0056   error('### Please input at least one LTPDA object.');
0057 end
0058 if isempty(sinfo)
0059   error('### Please provide an input submission structure.'); 
0060 end
0061 
0062 
0063 %% Some setup
0064 
0065 % make copy of connection object
0066 conn = sinfo.conn;
0067 if isempty(conn)
0068   error('### No valid database connect supplied. Aborting submit process.');
0069 end
0070 
0071 % Look-up user id
0072 userid = mysql_getUserID(conn, sinfo.username);
0073 disp(sprintf(' ** got user id %d for user: %s', userid, sinfo.username));
0074 if userid < 1 || isnan(userid)
0075   error('### Unknown username.');
0076 end
0077 
0078 % Machine details
0079 p = provenance;
0080 
0081 % comment mapping
0082 comm1 = sinfo.experiment_title;
0083 comm2 = sinfo.experiment_description;
0084 comm3 = sinfo.reference_ids;
0085 comm4 = sinfo.additional_comments;
0086 comm5 = sinfo.additional_authors;
0087 comm6 = '';
0088 
0089 %% Process each object and collect id numbers
0090 
0091 disp('*** Submitting objects to repository...');
0092 
0093 try
0094 
0095   %%%%%%%%%%% Lock database
0096   db_lock(conn);
0097   disp(' ** database locked for submission...');
0098 
0099   % The date for the transaction table
0100   t     = time();
0101   tdate = t.time_str;
0102 
0103   ids = [];
0104   for o=1:length(objs)
0105     % this object
0106     obj = objs(o);
0107 
0108     %%%%%%%%%%% Some setup
0109 
0110     % Object name
0111     if isfield(struct(obj), 'name')
0112       name = obj.name;
0113     else
0114       name = '';
0115     end
0116 
0117     % Data object (if present)
0118     if isfield(struct(obj), 'data')
0119       dname = obj.data.name;
0120       dtype = class(obj.data);
0121       ndata = length(get_xy_axis(obj.data));
0122     else
0123       dtype = '';
0124       dname = '';
0125       ndata = 0;
0126     end
0127 
0128     % Creation time
0129     if isfield(struct(obj), 'created')
0130       created = obj.created.time_str;
0131     else
0132       created = '';
0133     end
0134 
0135     %%%%%%%%%%% Tag the object (if possible)
0136     if isfield(obj, 'tag')
0137       obj = tag(obj, conn);
0138     end
0139 
0140     %%%%%%%%%%% Convert object to XML
0141     x = xml(obj);               % first to DOM
0142     otxt = xmlwrite(x.docNode); % then to string
0143 
0144     %%%%%%%%%%% Submit object to objs table
0145 
0146     message = ltpda_insert(conn, 'objs',...
0147       'xml', otxt...
0148       );
0149 
0150     objid = getObjID(conn);
0151     disp(sprintf('  + submitted object with id %d', objid));
0152     % check this against tag if present
0153     if isfield(obj, 'tag')
0154       if obj.tag ~= objid
0155         error('### Object ID doesn''t match object tag: this should never happen.');
0156       end
0157     end
0158     
0159     %%%%%%%%%%% Make objmeta entry
0160 
0161     message = ltpda_insert(conn, 'objmeta',...
0162       'objid', objid,...
0163       'objtype', class(obj),...
0164       'name', name,...
0165       'datatype', dtype,...
0166       'dataname', dname,...
0167       'datan', ndata,...
0168       'creation', created,...
0169       'ip', p.ip,...
0170       'hostname', p.hostname,...
0171       'os', p.os,...
0172       'comment1', comm1,...
0173       'comment2', comm2,...
0174       'comment3', comm3,...
0175       'comment4', comm4,...
0176       'comment5', comm5,...
0177       'comment6', comm6,...
0178       'filename', sprintf('OBJ%06d.xml', objid)...
0179       );
0180     disp(sprintf('  + made meta-data entry'));
0181 
0182     %%%%%%%%%%% Update transactions table
0183 
0184     message = ltpda_insert(conn, 'transactions',...
0185       'objid', objid,...
0186       'userid', userid,...
0187       'transdate', tdate,...
0188       'direction', 'in'...
0189       );
0190     disp(sprintf('  + updated transactions table'));
0191 
0192     % Collect this ID
0193     ids = [ids objid];
0194   end
0195 
0196   %% Now make collection entry
0197 
0198   message = ltpda_insert(conn, 'collections',...
0199     'nobjs', length(ids),...
0200     'objids', csv(ids)...
0201     );
0202   disp(sprintf(' ** made collection entry'));
0203 
0204 catch
0205   disp('### Submission error - cleaning up.');
0206   
0207   % unlock database
0208   db_unlock(conn);
0209   
0210   rethrow(lasterror)
0211 end
0212 
0213 %% unlock database
0214 disp(sprintf(' ** unlocking tables.'));
0215 db_unlock(conn);
0216 
0217 varargout{1} = ids;
0218 varargout{2} = getCollectionID(conn);
0219 
0220 disp('*** submission complete.');
0221 
0222 end
0223 
0224 %--------------------------------------------------------------------------
0225 %
0226 function id = getObjID(conn)
0227 
0228 curs = exec(conn, 'select max(id) from objs');
0229 curs = fetch(curs);
0230 id  = curs.Data{1};
0231 close(curs);
0232 
0233 end
0234 
0235 %--------------------------------------------------------------------------
0236 %
0237 function Cid = getCollectionID(conn)
0238 
0239 curs = exec(conn, 'select max(id) from collections');
0240 curs = fetch(curs);
0241 Cid  = curs.Data{1};
0242 close(curs);
0243 
0244 end
0245 
0246 %--------------------------------------------------------------------------
0247 %
0248 function db_lock(conn)
0249 
0250 q = 'FLUSH TABLES WITH WRITE LOCK';
0251 curs = exec(conn, q);
0252 
0253 end
0254 
0255 
0256 %--------------------------------------------------------------------------
0257 %
0258 function db_unlock(conn)
0259 
0260 q = 'UNLOCK TABLES';
0261 curs = exec(conn, q);
0262 
0263 end
0264 
0265 %--------------------------------------------------------------------------
0266 % make comma separated list of numbers
0267 function s = csv(x)
0268 
0269 s = sprintf('%g,', x);
0270 s = s(1:end-1);
0271 
0272 end
0273 
0274 % END

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003