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
0080 p = provenance;
0081
0082
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
0091
0092 disp('*** Submitting objects to repository...');
0093
0094 try
0095
0096
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
0104
0105
0106
0107
0108 t = time();
0109 tdate = t.time_str;
0110
0111 ids = [];
0112 for o=1:length(objs)
0113
0114 obj = objs{o};
0115
0116
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
0123
0124
0125 if isfield(obj, 'name')
0126 name = obj.name;
0127 else
0128 name = '';
0129 end
0130
0131
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
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
0159 if isfield(obj, 'version')
0160 objver = obj.version;
0161 else
0162 objver = '';
0163 end
0164
0165
0166 x = xml(obj);
0167 otxt = (xmlwrite(x.docNode));
0168
0169
0170 md5hash = ltpda_hash(otxt, 'MD5');
0171
0172
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
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
0201
0202
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
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
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
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 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
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
0307 function s = csv(x)
0308
0309 s = sprintf('%g,', x);
0310 s = s(1:end-1);
0311
0312 end
0313
0314