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.13 2007/11/13 13:53:47 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.13 2007/11/13 13:53:47 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 
0079 % Machine details
0080 p = provenance;
0081 
0082 % comment mapping
0083 comm1 = sinfo.experiment_title;
0084 comm2 = sinfo.experiment_description;
0085 comm3 = sinfo.reference_ids;
0086 comm4 = sinfo.additional_comments;
0087 comm5 = sinfo.additional_authors;
0088 comm6 = '';
0089 
0090 %% Process each object and collect id numbers
0091 
0092 disp('*** Submitting objects to repository...');
0093 
0094 try
0095 
0096   % Look-up user id
0097   userid = mysql_getUserID(conn, sinfo.username);
0098   disp(sprintf(' ** got user id %d for user: %s', userid, sinfo.username));
0099   if userid < 1 || isnan(userid)
0100     error('### Unknown username.');
0101   end
0102 
0103   %%%%%%%%%%% Lock database
0104   %   db_lock(conn);
0105   %   disp(' ** database locked for submission...');
0106 
0107   % The date for the transaction table
0108   t     = time();
0109   tdate = t.time_str;
0110 
0111   ids = [];
0112   for o=1:length(objs)
0113     % this object
0114     obj = objs{o};
0115 
0116     % Check this is a user object that can be submitted
0117     if isa(obj, 'ao') || isa(obj, 'miir') || isa(obj, 'mfir') ||...
0118         isa(obj, 'pzmodel') || isa(obj, 'time') || isa(obj, 'timespan') ||...
0119         isa(obj, 'specwin') || isa(obj, 'plist')
0120 
0121 
0122       %%%%%%%%%%% Some setup
0123 
0124       % Object name
0125       if isfield(obj, 'name')
0126         name = obj.name;
0127       else
0128         name = '';
0129       end
0130 
0131       % Data object (if present)
0132       if isfield(obj, 'data') && ~isempty(obj.data)
0133         dname = obj.data.name;
0134         dtype = class(obj.data);
0135         ndata = length(get_xy_values(obj.data));
0136       else
0137         dtype = '';
0138         dname = '';
0139         ndata = 0;
0140       end
0141 
0142       % Creation time
0143       if isfield(obj, 'created')
0144         if isa(obj.created, 'time')
0145           created = obj.created.time_str;
0146         elseif ischar(obj.created)
0147           created = obj.created;
0148         else
0149           error('### Unknown format for created field.');
0150         end
0151       else
0152         created = '0000-00-00 00:00:00';
0153       end
0154       if isempty(created)
0155         created = '0000-00-00 00:00:00';
0156       end
0157 
0158       % Version
0159       if isfield(obj, 'version')
0160         objver = obj.version;
0161       else
0162         objver = '';
0163       end
0164 
0165       %%%%%%%%%%% Convert object to XML
0166       x = xml(obj);               % first to DOM
0167       otxt = (xmlwrite(x.docNode)); % then to string
0168 
0169       %%%%%%%%%%% Create MD5 hash of object
0170       md5hash = ltpda_hash(otxt, 'MD5');
0171 
0172       %%%%%%%%%%% Submit object to objs table
0173 
0174       curs = exec(conn, ['insert into objs(xml,hash) values(''' otxt ''', ''' char(md5hash) ''' );']);
0175       close(curs);
0176 
0177       objid = getObjID(conn, md5hash);
0178       disp(sprintf('  + submitted object %s with id %d', class(obj), objid));
0179 
0180       %%%%%%%%%%% Make objmeta entry
0181 
0182       message = ltpda_insert(conn, 'objmeta',...
0183         'obj_id', objid,...
0184         'obj_type', class(obj),...
0185         'name', name,...
0186         'created', created,...
0187         'version', objver,...
0188         'ip', p.ip,...
0189         'hostname', p.hostname,...
0190         'os', p.os,...
0191         'submitted', datestr(now, 31),...
0192         'comment1', comm1,...
0193         'comment2', comm2,...
0194         'comment3', comm3,...
0195         'comment4', comm4,...
0196         'comment5', comm5,...
0197         'comment6', comm6...
0198         );
0199       disp(sprintf('  + made meta-data entry'));
0200       %       'filename', sprintf('OBJ%06d.xml', objid)...
0201 
0202       %%%%%%%%%%% Update transactions table
0203 
0204       message = ltpda_insert(conn, 'transactions',...
0205         'obj_id', objid,...
0206         'user_id', userid,...
0207         'transdate', tdate,...
0208         'direction', 'in'...
0209         );
0210       disp(sprintf('  + updated transactions table'));
0211 
0212       % Collect this ID
0213       ids = [ids objid];
0214     else
0215       warning('!! Object is not one of the User classes. Not submitting.');
0216       disp(obj);
0217     end
0218   end
0219 
0220   %% Now make collection entry
0221   if ~isempty(ids)
0222     message = ltpda_insert(conn, 'collections',...
0223       'nobjs', length(ids),...
0224       'obj_ids', csv(ids)...
0225       );
0226     disp(sprintf(' ** made collection entry'));
0227   end
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 obj_ids="%s"', csv(objids)));
0265 curs = fetch(curs);
0266 Cid  = curs.Data{1};
0267 close(curs);
0268 
0269 if strcmp(Cid, 'No Data')
0270   Cid = [];
0271 end
0272 
0273 end
0274 
0275 %--------------------------------------------------------------------------
0276 %
0277 function db_lock(conn)
0278 
0279 % lock all tables
0280 q = ['lock tables '...
0281   'ao write,'...
0282   'collections write,'...
0283   'fsdata write,'...
0284   'mfir write,'...
0285   'miir write,'...
0286   'objmeta write,'...
0287   'objs write,' ...
0288   'transactions write,'...
0289   'tsdata write,'...
0290   'users write'];
0291 curs = exec(conn, q);
0292 
0293 end
0294 
0295 
0296 %--------------------------------------------------------------------------
0297 %
0298 function db_unlock(conn)
0299 
0300 q = 'UNLOCK TABLES';
0301 curs = exec(conn, q);
0302 
0303 end
0304 
0305 %--------------------------------------------------------------------------
0306 % make comma separated list of numbers
0307 function s = csv(x)
0308 
0309 s = sprintf('%g,', x);
0310 s = s(1:end-1);
0311 
0312 end
0313 
0314 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003