Mercurial > hg > ltpda
comparison m-toolbox/classes/@ltpda_uo/fromRepository.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children | 69e3d49b4b0c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % Retrieve a ltpda_uo from a repository | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % FUNCTION: fromRepository | |
5 % | |
6 % DESCRIPTION: Retrieve a ltpda_uo from a repository | |
7 % | |
8 % CALL: obj = fromRepository(pl) | |
9 % | |
10 % PARAMETER: pl: Parameter list object | |
11 % | |
12 % VERSION: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $ | |
13 % | |
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
15 function [objs, plhout, ii] = fromRepository(obj, pli, ii) | |
16 | |
17 VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $'; | |
18 | |
19 requested_class = class(obj); | |
20 | |
21 % Set the method version string in the minfo object | |
22 ii.setMversion([VERSION '-->' ii.mversion]); | |
23 | |
24 % Check if the user supplied a DB connection | |
25 conn = find(pli, 'conn'); | |
26 | |
27 % Add default values if necessary | |
28 if isempty(conn) | |
29 pl = combine(pli, ii.plists.pset('HOSTNAME', '')); | |
30 else | |
31 pl = pli; | |
32 end | |
33 | |
34 % Get parameters | |
35 hostname = find(pl, 'hostname'); | |
36 database = find(pl, 'database'); | |
37 user = find(pl, 'username'); | |
38 passwd = find(pl, 'password'); | |
39 ids = find(pl, 'id'); | |
40 cids = find(pl, 'cid'); | |
41 bin = find(pl, 'binary'); | |
42 | |
43 % Make sure that 'ids' or 'cids' are empty arrays if they are empty. | |
44 % It might be that the GUI return an empty string. | |
45 if isempty(ids) | |
46 ids = []; | |
47 end | |
48 if isempty(cids) | |
49 cids = []; | |
50 end | |
51 | |
52 % Check if some ID is defined | |
53 if isempty(ids) && isempty(cids) | |
54 error('### Please define at least one object ID or connection ID'); | |
55 end | |
56 | |
57 %%% check if using binary or not: 'yes'/'no' or true/false or | |
58 %%% 'true'/'false' | |
59 bin = utils.prog.yes2true(bin); | |
60 | |
61 % do we have a connection? | |
62 closeConn = 0; | |
63 if isempty(conn) | |
64 closeConn = 1; | |
65 % Connect to repository | |
66 conn = utils.jmysql.connect(hostname, database, user, passwd); | |
67 end | |
68 if ~isa(conn, 'database') && ~isa(conn, 'mpipeline.repository.RepositoryConnection') | |
69 error('### connection to the database failed.'); | |
70 end | |
71 | |
72 if ~isempty(cids) | |
73 for kk=1:numel(cids) | |
74 cid = cids(kk); | |
75 if isa(conn, 'database') | |
76 % get the ids from the cid | |
77 ids = [ids utils.mysql.getObjIds(conn,cid)]; | |
78 else | |
79 c_ids = double(mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid)); | |
80 ids = [ids c_ids.']; | |
81 end | |
82 end | |
83 end | |
84 | |
85 % Get each ID | |
86 Nids = length(ids); | |
87 objs = []; | |
88 plhout = []; | |
89 | |
90 for kk=1:Nids | |
91 | |
92 %---- copy the input plist because each object should get an other plist | |
93 plh = copy(pl, 1); | |
94 | |
95 %---- This id | |
96 id = ids(kk); | |
97 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); | |
98 | |
99 %---- call database constructor | |
100 if bin | |
101 try | |
102 obj = ltpda_uo.retrieve(conn, 'binary', id); | |
103 | |
104 catch | |
105 | |
106 if ~conn.isConnected() | |
107 error('### There is something wrong with the connection.') | |
108 end | |
109 | |
110 warning('!!! Unable to do binary retrieval for object %d; trying to retrieve XML instead.', id); | |
111 obj = ltpda_uo.retrieve(conn, id); | |
112 end | |
113 else | |
114 obj = ltpda_uo.retrieve(conn, id); | |
115 end | |
116 | |
117 if ~strcmp(class(obj), requested_class) | |
118 error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj)); | |
119 end | |
120 | |
121 % - remove connection object from plist first | |
122 if isempty(hostname) || isempty(database) | |
123 if isa(conn, 'database') | |
124 txt = textscan(conn.URL, '%s', 'Delimiter', '/', 'CollectOutput', 1); | |
125 plc = plist('hostname', txt{1}{3}, 'database', txt{1}{4}); | |
126 else | |
127 plc = plist('hostname', char(conn.getHostname), 'database', char(conn.getDatabase)); | |
128 end | |
129 plh = combine(pli, plc); | |
130 end | |
131 | |
132 %---- remove the connection from the history plist | |
133 if plh.isparam('conn') | |
134 plh.remove('conn'); | |
135 end | |
136 | |
137 %---- Set only the ID of the current object to the plist | |
138 plh.pset('ID', id); | |
139 | |
140 %---- Add history-plist to output array | |
141 plhout = [plhout plh]; | |
142 | |
143 %---- Add to output array | |
144 objs = [objs obj]; | |
145 | |
146 end | |
147 | |
148 % close connection | |
149 if closeConn | |
150 if isa(conn, 'database') | |
151 close(conn); | |
152 else | |
153 conn.closeConnection; | |
154 end | |
155 end | |
156 | |
157 end | |
158 |