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: 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, {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: submit.m,v 1.14 2007/08/16 06:55:25 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 % make copy of connection object
0073 conn = sinfo.conn;
0074 if isempty(conn)
0075   error('### No valid database connect supplied. Aborting submit process.');
0076 end
0077 
0078 % Look-up user id
0079 userid = mysql_getUserID(conn, sinfo.username);
0080 disp(sprintf(' ** got user id %d for user: %s', userid, sinfo.username));
0081 if userid < 1 || isnan(userid)
0082   error('### Unknown username.');
0083 end
0084 
0085 % Machine details
0086 p = provenance;
0087 
0088 % comment mapping
0089 comm1 = sinfo.experiment_title;
0090 comm2 = sinfo.experiment_description;
0091 comm3 = sinfo.reference_ids;
0092 comm4 = sinfo.additional_comments;
0093 comm5 = sinfo.additional_authors;
0094 comm6 = '';
0095 
0096 %% Process each object and collect id numbers
0097 
0098 disp('*** Submitting objects to repository...');
0099 
0100 try
0101 
0102   %%%%%%%%%%% Lock database
0103 %   db_lock(conn);
0104 %   disp(' ** database locked for submission...');
0105 
0106   % The date for the transaction table
0107   t     = time();
0108   tdate = t.time_str;
0109 
0110   ids = [];
0111   for o=1:length(objs)
0112     % this object
0113     obj = objs{o};
0114     
0115     %%%%%%%%%%% Some setup
0116 
0117     % Object name
0118     if isfield(obj, 'name')
0119       name = obj.name;
0120     else
0121       name = '';
0122     end
0123 
0124     % Data object (if present)
0125     if isfield(obj, 'data') && ~isempty(obj.data)
0126       dname = obj.data.name;
0127       dtype = class(obj.data);
0128       ndata = length(get_xy_values(obj.data));
0129     else
0130       dtype = '';
0131       dname = '';
0132       ndata = 0;
0133     end
0134 
0135     % Creation time
0136     if isfield(obj, 'created')
0137       if isa(obj.created, 'time')
0138         created = obj.created.time_str;
0139       elseif ischar(obj.created)
0140         created = obj.created;
0141       else
0142         error('### Unknown format for created field.');
0143       end
0144     else
0145       created = '0000-00-00 00:00:00';
0146     end
0147     if isempty(created)
0148       created = '0000-00-00 00:00:00';
0149     end
0150     
0151     % Version
0152     if isfield(obj, 'version')
0153       objver = obj.version;
0154     else
0155       objver = '';
0156     end
0157 
0158 %     %%%%%%%%%%% Tag the object (if possible)
0159 %     if isfield(obj, 'tag')
0160 %       obj = tag(obj, conn);
0161 %     end
0162 
0163     %%%%%%%%%%% Convert object to XML
0164     x = xml(obj);               % first to DOM
0165     otxt = (xmlwrite(x.docNode)); % then to string
0166 
0167     %%%%%%%%%%% Create MD5 hash of object
0168     md5hash = ltpda_hash(otxt, 'MD5');
0169     
0170     %%%%%%%%%%% Submit object to objs table
0171 
0172     curs = exec(conn, ['insert into objs(xml,hash) values(''' otxt ''', ''' char(md5hash) ''' );']);
0173     close(curs);
0174     
0175     objid = getObjID(conn, md5hash);
0176     disp(sprintf('  + submitted object with id %d', objid));
0177 %     % check this against tag if present
0178 %     if isfield(obj, 'tag')
0179 %       if obj.tag ~= objid
0180 %         error('### Object ID doesn''t match object tag: this should never happen.');
0181 %       end
0182 %     end
0183     
0184     %%%%%%%%%%% Make objmeta entry
0185 
0186     message = ltpda_insert(conn, 'objmeta',...
0187       'obj_id', objid,...
0188       'obj_type', class(obj),...
0189       'name', name,...
0190       'created', created,...
0191       'version', objver,...
0192       'ip', p.ip,...
0193       'hostname', p.hostname,...
0194       'os', p.os,...
0195       'submitted', datestr(now, 31),...
0196       'comment1', comm1,...
0197       'comment2', comm2,...
0198       'comment3', comm3,...
0199       'comment4', comm4,...
0200       'comment5', comm5,...
0201       'comment6', comm6...
0202       );
0203     disp(sprintf('  + made meta-data entry'));
0204 %       'filename', sprintf('OBJ%06d.xml', objid)...
0205 
0206     %%%%%%%%%%% Update transactions table
0207 
0208     message = ltpda_insert(conn, 'transactions',...
0209       'objid', objid,...
0210       'userid', userid,...
0211       'transdate', tdate,...
0212       'direction', 'in'...
0213       );
0214     disp(sprintf('  + updated transactions table'));
0215 
0216     % Collect this ID
0217     ids = [ids objid];
0218   end
0219 
0220   %% Now make collection entry
0221 
0222   message = ltpda_insert(conn, 'collections',...
0223     'nobjs', length(ids),...
0224     'objids', csv(ids)...
0225     );
0226   disp(sprintf(' ** made collection entry'));
0227 
0228 catch
0229   disp('### Submission error - cleaning up.');
0230   
0231 %   % unlock database
0232 %   db_unlock(conn);
0233   
0234   rethrow(lasterror)
0235 end
0236 
0237 %% unlock database
0238 % disp(sprintf(' ** unlocking tables.'));
0239 % db_unlock(conn);
0240 
0241 varargout{1} = ids;
0242 varargout{2} = getCollectionID(conn, ids);
0243 
0244 disp('*** submission complete.');
0245 
0246 end
0247 
0248 %--------------------------------------------------------------------------
0249 %
0250 function id = getObjID(conn, hash)
0251 
0252 q = sprintf('select last_insert_id()');
0253 curs = exec(conn, q);
0254 curs = fetch(curs);
0255 id  = curs.Data{1};
0256 close(curs);
0257 
0258 end
0259 
0260 %--------------------------------------------------------------------------
0261 %
0262 function Cid = getCollectionID(conn, objids)
0263 
0264 curs = exec(conn, sprintf('select id from collections where objids="%s"', csv(objids)));
0265 curs = fetch(curs);
0266 Cid  = curs.Data{1};
0267 close(curs);
0268 
0269 end
0270 
0271 %--------------------------------------------------------------------------
0272 %
0273 function db_lock(conn)
0274 
0275 % lock all tables
0276 q = ['lock tables '...
0277      'ao write,'...
0278      'collections write,'...
0279      'fsdata write,'...
0280      'mfir write,'...
0281      'miir write,'...
0282      'objmeta write,'...
0283      'objs write,' ...
0284      'transactions write,'...
0285      'tsdata write,'...
0286      'users write'];
0287 curs = exec(conn, q);
0288 
0289 end
0290 
0291 
0292 %--------------------------------------------------------------------------
0293 %
0294 function db_unlock(conn)
0295 
0296 q = 'UNLOCK TABLES';
0297 curs = exec(conn, q);
0298 
0299 end
0300 
0301 %--------------------------------------------------------------------------
0302 % make comma separated list of numbers
0303 function s = csv(x)
0304 
0305 s = sprintf('%g,', x);
0306 s = s(1:end-1);
0307 
0308 end
0309 
0310 % END

Generated on Fri 02-Nov-2007 19:39:27 by m2html © 2003