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 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
0071
0072
0073 conn = sinfo.conn;
0074 if isempty(conn)
0075 error('### No valid database connect supplied. Aborting submit process.');
0076 end
0077
0078
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
0086 p = provenance;
0087
0088
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
0097
0098 disp('*** Submitting objects to repository...');
0099
0100 try
0101
0102
0103
0104
0105
0106
0107 t = time();
0108 tdate = t.time_str;
0109
0110 ids = [];
0111 for o=1:length(objs)
0112
0113 obj = objs{o};
0114
0115
0116
0117
0118 if isfield(obj, 'name')
0119 name = obj.name;
0120 else
0121 name = '';
0122 end
0123
0124
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
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
0152 if isfield(obj, 'version')
0153 objver = obj.version;
0154 else
0155 objver = '';
0156 end
0157
0158
0159
0160
0161
0162
0163
0164 x = xml(obj);
0165 otxt = (xmlwrite(x.docNode));
0166
0167
0168 md5hash = ltpda_hash(otxt, 'MD5');
0169
0170
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
0178
0179
0180
0181
0182
0183
0184
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
0205
0206
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
0217 ids = [ids objid];
0218 end
0219
0220
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
0232
0233
0234 rethrow(lasterror)
0235 end
0236
0237
0238
0239
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
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
0303 function s = csv(x)
0304
0305 s = sprintf('%g,', x);
0306 s = s(1:end-1);
0307
0308 end
0309
0310