Home > classes > @ao > submit.m

submit

PURPOSE ^

SUBMIT AO(s) to the MySQL server.

SYNOPSIS ^

function [AOids, Cid] = submit(varargin)

DESCRIPTION ^

 SUBMIT AO(s) to the MySQL server.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: SUBMIT AO(s) to the MySQL server.
              The input AOs will be submitted to the MySQL server configured in
              ltpda_startup.m. If multiple AOs are submitted together,
              a corresponding AO Collection entry will be made.

 CALL:        submit(a1, 'some comment')
              submit(a1,a2, 'some comment')
              submit([a1 a2], a3, 'some comment')

 VERSION:     $Id: submit.m,v 1.15 2007/08/31 17:40:08 hewitson Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = submit('Params')

 HISTORY: 09-05-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [AOids, Cid] = submit(varargin)
0002 % SUBMIT AO(s) to the MySQL server.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: SUBMIT AO(s) to the MySQL server.
0007 %              The input AOs will be submitted to the MySQL server configured in
0008 %              ltpda_startup.m. If multiple AOs are submitted together,
0009 %              a corresponding AO Collection entry will be made.
0010 %
0011 % CALL:        submit(a1, 'some comment')
0012 %              submit(a1,a2, 'some comment')
0013 %              submit([a1 a2], a3, 'some comment')
0014 %
0015 % VERSION:     $Id: submit.m,v 1.15 2007/08/31 17:40:08 hewitson Exp $
0016 %
0017 % The following call returns a parameter list object that contains the
0018 % default parameter values:
0019 %
0020 % >> pl = submit('Params')
0021 %
0022 % HISTORY: 09-05-07 M Hewitson
0023 %             Creation
0024 %
0025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0026 
0027 % Check if this is a call for parameters
0028 if nargin == 2
0029   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0030     in = char(varargin{2});
0031     if strcmp(in, 'Params')
0032       AOids = getDefaultPL();
0033       return
0034     end
0035   end
0036 end
0037 
0038 %--------------------------------------------------------------------------
0039 % collect all AOs from input
0040 as      = [];
0041 comment = [];
0042 for j=1:nargin
0043   a = varargin{j};
0044   if isa(a, 'ao')
0045     as = [as a];
0046   end
0047   if ischar(a)
0048     comment = varargin{j};
0049   end
0050 end
0051 na = length(as);
0052 if na < 1
0053   error('### Incorrect inputs: please supply at least one AO to submit.');
0054 end
0055 if isempty(comment) || length(comment)<10
0056   error('### Incorrect inputs: please supply a comment of at least 10 characters in length.');
0057 end
0058 disp(sprintf('** Submitting %d AOs.', na));
0059 
0060 %--------------------------------------------------------------------------
0061 % open mysql connection
0062 
0063 conn = mysql_connect();
0064 
0065 %--------------------------------------------------------------------------
0066 % Now process each AO
0067 
0068 AOids = [];
0069 Cid   = -1;
0070 
0071 for j=1:na
0072 
0073   a = as(j);
0074   % Tag this AO
0075   a = tag(a, conn);
0076   
0077   disp(sprintf('** Submitting AO: %06d/%s', a.tag, a.name));
0078 
0079   % Get UserID from Username
0080   [userid, dbuser] = mysql_getUserID(conn);
0081   disp(sprintf('** submitting as user %s/%d', dbuser, userid));
0082 
0083   % Collect together all info for this entry
0084   name      = a.name;
0085   data      = a.data;
0086   dinfo     = whos('data');
0087   dtype     = dinfo.class;
0088   dname     = data.name;
0089   submitted = ltpda_reformat_date(datestr(now, 31));
0090   prov      = a.provenance;
0091 %   created   = ltpda_reformat_date(datestr(get(prov, 'created'), 31));
0092   created   = format(get(prov, 'created'), 'mm-dd-yyyy HH:MM:SS');
0093   ip        = get(prov, 'ip');
0094   hostname  = get(prov, 'hostname');
0095   os        = get(prov, 'os');
0096   filename  = sprintf('AO%06d.xml', a.tag);
0097 
0098   % convert ao to text
0099   x = xml(a); % first to DOM
0100   atxt = xmlwrite(x.docNode); % then to string
0101 
0102   % Insert object into
0103   
0104   % Insert into objmeta table
0105   minsert(conn, 'objmeta',...
0106            'name', name,...
0107            'dtype', dtype,...
0108            'dname', dname,...
0109            'ndata', len(a),...
0110            'UserID', userid,...
0111            'Comment1', comment,...
0112            'Creation', created,...
0113            'IP', ip,...
0114            'Hostname', hostname,...
0115            'OS', os,...
0116            'Filename', filename,...
0117            'AO', atxt);
0118     
0119   
0120 %   colnames = {'AO'};
0121 %   exdata   = {atxt};
0122 %   insert(conn, 'aofiles', colnames, exdata);
0123   
0124 %   q = sprintf('INSERT INTO aofiles(AO) VALUES(''%s'');', atxt);
0125 %   curs = exec(conn, q);
0126 %   disp(curs.Message)
0127 %   close(curs);
0128 
0129 %   minsert(conn, 'aofiles', 'AO', atxt);
0130   
0131   % Update transactions table
0132   colnames = {'aoid','UserId','Tdate','Direction'};
0133   exdata   = {a.tag,userid,submitted,'in'};
0134   try
0135     fastinsert(conn, 'transactions', colnames, exdata);
0136   catch
0137     warning('### error updating transaction table for submission of AO %s/%d', a.name, a.tag);
0138   end
0139 
0140   % collect submitted IDs
0141   AOids = [AOids a.tag];
0142 
0143 end
0144 
0145 % Update collections table.
0146 % colnames = {'num','aoids'};
0147 % exdata   = {length(AOids),csv(AOids)};
0148 if ~isempty(AOids)
0149   try
0150 %     fastinsert(conn, 'collections', colnames, exdata);
0151     minsert(conn, 'collections', 'num', length(AOids), 'aoids', csv(AOids));
0152     % get assigned collection ID
0153     curs = exec(conn, 'select max(id) from collections');
0154     curs = fetch(curs);
0155     Cid  = curs.Data{1};
0156     close(curs);
0157   catch
0158     warning('### error submitting AO collection.');
0159   end
0160 end
0161 
0162 % Cid = [];
0163 
0164 %--------------------------------------------------------------------------
0165 % close MySQL connection
0166 close(conn);
0167 
0168 %--------------------------------------------------------------------------
0169 % make comma separated list of numbers
0170 function s = csv(x)
0171 
0172 s = sprintf('%g,', x);
0173 s = s(1:end-1);
0174 
0175 %--------------------------------------------------------------------------
0176 % Get default params
0177 function plo = getDefaultPL()
0178 
0179 disp('* creating default plist...');
0180 plo = plist();
0181 disp('* done.');
0182 
0183 
0184 
0185 %--------------------------------------------------------------------------
0186 % A faster insert than fastinsert (at least for this case)
0187 function message = minsert(conn, table, varargin)
0188 
0189 % MINSERT inserts values into a single row of a table using JDBC driver
0190 % specified by the input connection.
0191 %
0192 % Usage: message = minsert(conn, table, 'field1', value1, 'field2',
0193 %                               value2,...)
0194 %
0195 % M Hewitson
0196 %
0197 % 31-07-07
0198 %
0199 % $Id: submit.m,v 1.15 2007/08/31 17:40:08 hewitson Exp $
0200 %
0201 
0202 %   q = sprintf('INSERT INTO aofiles(AO) VALUES(''%s'');', atxt);
0203 %   curs = exec(conn, q);
0204 %   disp(curs.Message)
0205 %   close(curs);
0206 
0207 q = sprintf('INSERT INTO %s(',  table);
0208 for j=1:2:length(varargin)
0209   q = [q varargin{j} ','];
0210 end
0211 q = q(1:end-1);
0212 
0213 q = [q ') VALUES('];
0214 
0215 for j=2:2:length(varargin)
0216   val = varargin{j};
0217   if ischar(val)
0218     q = [q '''' val '''' ','];
0219   elseif isnumeric(val)
0220     q = [q mat2str(val) ','];
0221   else
0222     error('unknown data type');
0223   end
0224   
0225 end
0226 
0227 q = q(1:end-1);
0228 q = [q ');'];
0229 
0230 curs = exec(conn, q);
0231 message = curs.Message;
0232 disp(message);
0233 close(curs);
0234 
0235 % END

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003