0001 function varargout = ltpda_obj_submit(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
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
0064
0065
0066 conn = sinfo.conn;
0067 if isempty(conn)
0068 error('### No valid database connect supplied. Aborting submit process.');
0069 end
0070
0071
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
0079 p = provenance;
0080
0081
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
0090
0091 disp('*** Submitting objects to repository...');
0092
0093 try
0094
0095
0096 db_lock(conn);
0097 disp(' ** database locked for submission...');
0098
0099
0100 t = time();
0101 tdate = t.time_str;
0102
0103 ids = [];
0104 for o=1:length(objs)
0105
0106 obj = objs(o);
0107
0108
0109
0110
0111 if isfield(struct(obj), 'name')
0112 name = obj.name;
0113 else
0114 name = '';
0115 end
0116
0117
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
0129 if isfield(struct(obj), 'created')
0130 created = obj.created.time_str;
0131 else
0132 created = '';
0133 end
0134
0135
0136 if isfield(obj, 'tag')
0137 obj = tag(obj, conn);
0138 end
0139
0140
0141 x = xml(obj);
0142 otxt = xmlwrite(x.docNode);
0143
0144
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
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
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
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
0193 ids = [ids objid];
0194 end
0195
0196
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
0208 db_unlock(conn);
0209
0210 rethrow(lasterror)
0211 end
0212
0213
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
0267 function s = csv(x)
0268
0269 s = sprintf('%g,', x);
0270 s = s(1:end-1);
0271
0272 end
0273
0274