comparison m-toolbox/classes/@ltpda_uo/retrieve.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children 1e91f84a4be8 bc767aaa99a8
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % RETRIEVE retrieves a collection of objects from an LTPDA repository.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: This static method retrieves a collection of objects from an
5 % LTPDA repository.
6 %
7 % CALL: objs = retrieve(conn, obj_id_1, obj_id_2)
8 % [o1,o2] = retrieve(conn, obj_id_1, obj_id_2)
9 % [o1,o2] = retrieve(conn, 'binary', obj_id_1, obj_id_2)
10 % objs = retrieve(conn, 'Collection', coll_id)
11 % objs = retrieve(conn, 'binary', 'Collection', coll_id)
12 %
13 % INPUTS:
14 % conn - database connection object
15 % obj_id_N - an object ID
16 % coll_id - a collection ID
17 % 'binary' - to retrieve a binary representation of the object
18 % (if stored)
19 %
20 % OUTPUTS:
21 % objs - the retrieved object(s) as a cell array.*
22 % o1,o2,...,oN - returns the first N objects
23 %
24 %
25 % If more than one object is retrieved and only one output is specified
26 % then the output is a cell array of objects.
27 %
28 % If only a single object is requested, it is returned as an object,
29 % not packed in a cell array.
30 %
31 % VERSION: $Id: retrieve.m,v 1.28 2011/07/01 14:38:57 ingo Exp $
32 %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 function varargout = retrieve(varargin)
36
37 % Check if this is a call for parameters
38 if utils.helper.isinfocall(varargin{:})
39 varargout{1} = getInfo(varargin{3});
40 return
41 end
42
43 import utils.const.*
44 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
45
46 if nargin == 0
47 help(mfilename);
48 error('### Incorrect inputs');
49 end
50
51 objs = [];
52 conn = varargin{1};
53 if ~isa(conn, 'mpipeline.repository.RepositoryConnection')
54 error('### the first argument should be a mpipeline.repository.RepositoryConnection connection object.');
55 end
56
57 % Unlock the connection only if the connection was not locked
58 unlockConnection = ~conn.isLocked();
59
60 rm = LTPDARepositoryManager;
61
62 try
63
64 if ~conn.isConnected
65 if isempty(conn.openConnection());
66 return
67 end
68 end
69
70 conn.setLocked(true);
71
72 % reload connection table if we have a GUI
73 if ~isempty(rm.gui)
74 rm.gui.reloadConnectionTable();
75 end
76
77 % Get username and user id
78 username = char(conn.getUsername);
79 userid = utils.jmysql.getUserID(conn, username);
80
81 if ~isempty(userid)
82 utils.helper.msg(msg.PROC1, 'got user id %d for user: %s', userid, username);
83 if userid < 1 || isnan(userid) || strcmp(userid, 'No Data') || ischar(userid)
84 error('### Unknown username.');
85 end
86 else
87 error('### Could not determine user id. Can not proceed.');
88 end
89
90 binary = false;
91 if nargin >= 3 && ischar(varargin{2}) && strcmpi(varargin{2}, 'binary')
92 %%% retrieve(conn, 'binary', obj_id_1, obj_id_2)
93 %%% retrieve(conn, 'binary', 'Collection', coll_id)
94 binary = true;
95 if nargin == 4 && ischar(varargin{3}) && strcmpi(varargin{3}, 'Collection') && isnumeric(varargin{4}) && numel(varargin{4}) == 1
96 cid = varargin{4};
97 % Get a list of object IDs from the collection ID
98 ids = mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid);
99 elseif nargin >= 3 && isnumeric([varargin{3:end}])
100 ids = [varargin{3:end}];
101 else
102 help(mfilename)
103 error('### Incorrect usage');
104 end
105
106 elseif nargin == 3 && ischar(varargin{2}) && strcmpi(varargin{2}, 'Collection') && isnumeric(varargin{3}) && numel(varargin{3}) == 1
107 %%% retrieve(conn, 'Collection', coll_id)
108 cid = varargin{3};
109 % Get a list of object IDs from the collection ID
110 ids = mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid);
111
112 elseif nargin >= 2 && isnumeric([varargin{2:end}])
113 %%% retrieve(conn, obj_id_1, obj_id_2)
114 ids = [varargin{2:end}];
115
116 else
117 help(mfilename)
118 error('### Incorrect usage');
119 end
120
121 utils.helper.msg(msg.PROC1, ['retrieving objects ' utils.xml.mat2str(ids)]);
122
123 v = ver('LTPDA');
124 for j=1:length(ids)
125
126 % It is only possible to download the object if the object in the
127 % database was submitted with the same or lower LTPDA version as the
128 % current version.
129 q = sprintf('SELECT version FROM objmeta WHERE obj_id=%d', ids(j));
130 try
131 vDb = utils.jmysql.dbquery(conn, q);
132 catch ME
133 disp(ME);
134 end
135 if utils.helper.ver2num(v.Version) < utils.helper.ver2num(vDb{1})
136 error('### The object with the ID %d was submitted with a higher LTPDA version than you use %s. Please update your LTPDA version.', ids(j), vDb{1});
137 end
138
139 % Get object
140 if binary
141 % Retrieve the bytes
142 q = sprintf('select mat from bobjs where obj_id="%d"', ids(j));
143 results = conn.query(q);
144 while results.next
145 dd = results.getObject(1);
146 end
147
148 if strcmp(dd, 'No Data') || isempty(dd)
149 error('Failed to get binary data for object %d', ids(j));
150 end
151 % Write bytes out to a temp MAT file
152 fname = [tempname '.mat'];
153 fd = fopen(fname, 'w+');
154 fwrite(fd, dd, 'int8');
155 fclose(fd);
156 % Load the MAT data to a structure
157 obj = load(fname);
158 % Delete temp file
159 delete(fname);
160 % Get the struct out
161 obj = obj.objs;
162 % Get the object class
163 scl = char(mpipeline.repository.MySQLUtils.getObjectTypeForID(conn, ids(j)));
164
165 % Check if the retrieved object is a struct
166 if isstruct(obj)
167 % Call the constructor with this struct
168 fcn_name = [scl '.update_struct'];
169 obj = feval(fcn_name, obj, obj.tbxver);
170 obj = feval(scl, obj);
171 end
172 % Add tyo object array
173 objs = [objs {obj}];
174 else
175 xdoc = utils.jmysql.getXdoc(conn, ids(j));
176 if ~isempty(xdoc)
177 obj = utils.xml.xmlread(xdoc);
178 if j==1
179 objs = {obj};
180 else
181 objs = [objs {obj}];
182 end
183
184 % make transaction entry
185 t = time();
186 tdate = t.format('yyyy-mm-dd HH:MM:SS');
187 try
188 message = utils.jmysql.insert(conn, ...
189 'transactions',...
190 'obj_id', ids(j),...
191 'user_id', userid,...
192 'transdate', tdate,...
193 'direction', 'out'...
194 );
195
196 utils.helper.msg(msg.PROC1, 'updated transactions table');
197 catch
198 error('### Failed to make entry in transactions table');
199 end
200 else
201 warning('!!! Error retrieving object: %d', ids(j));
202 end % End empty Xdoc
203 end % End binary
204 end % End id loop
205
206 catch ME
207 fprintf(2, [ME.message, '\n\n']);
208 utils.helper.msg(msg.PROC1, '### Retrieve error.');
209 rethrow(ME)
210 end
211
212 if unlockConnection
213 conn.setLocked(false);
214 end
215
216 % reset Timer
217 LTPDARepositoryManager.resetTimer(rm.timerClearPass, conn);
218 LTPDARepositoryManager.resetTimer(rm.timerDisconnect, conn);
219
220 % Set outputs
221 if nargout == 1
222 if length(objs) == 1
223 varargout{1} = objs{1};
224 else
225 varargout{1} = objs;
226 end
227 else
228 for k=1:nargout
229 varargout{k} = objs{k};
230 end
231 end
232 end
233
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 % Local Functions %
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 %
240 function ii = getInfo(varargin)
241 if nargin == 1 && strcmpi(varargin{1}, 'None')
242 sets = {};
243 pl = [];
244 else
245 sets = {'Default'};
246 pl = getDefaultPlist;
247 end
248 % Build info object
249 ii = minfo(mfilename, 'ltpda_uo', 'ltpda', utils.const.categories.internal, '$Id: retrieve.m,v 1.28 2011/07/01 14:38:57 ingo Exp $', sets, pl);
250 ii.setModifier(false);
251 end
252
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %
255 function plout = getDefaultPlist()
256 persistent pl;
257 if exist('pl', 'var')==0 || isempty(pl)
258 pl = buildplist();
259 end
260 plout = pl;
261 end
262
263 function plo = buildplist()
264 plo = plist();
265
266 p = param({'conn', 'A database object'}, paramValue.EMPTY_DOUBLE);
267 plo.append(p);
268
269 p = param({'ids', 'IDs which should be collected'}, paramValue.EMPTY_DOUBLE);
270 plo.append(p);
271 end
272
273
274