Mercurial > hg > ltpda
comparison m-toolbox/classes/@ltpda_uo/fromRepository.m @ 19:69e3d49b4b0c database-connection-manager
Update ltpda_uo.fromRepository
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children | 056f8e1e995e |
comparison
equal
deleted
inserted
replaced
18:947e2ff4b1b9 | 19:69e3d49b4b0c |
---|---|
10 % PARAMETER: pl: Parameter list object | 10 % PARAMETER: pl: Parameter list object |
11 % | 11 % |
12 % VERSION: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $ | 12 % VERSION: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $ |
13 % | 13 % |
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
15 function [objs, plhout, ii] = fromRepository(obj, pli, ii) | 15 function [objs, plhout, ii] = fromRepository(obj, pl, ii) |
16 | 16 |
17 VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $'; | 17 VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $'; |
18 | 18 |
19 requested_class = class(obj); | 19 requested_class = class(obj); |
20 | 20 |
21 % Set the method version string in the minfo object | 21 % Set the method version string in the minfo object |
22 ii.setMversion([VERSION '-->' ii.mversion]); | 22 ii.setMversion([VERSION '-->' ii.mversion]); |
23 | 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 | 24 % 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'); | 25 ids = find(pl, 'id'); |
40 cids = find(pl, 'cid'); | 26 cids = find(pl, 'cid'); |
41 bin = find(pl, 'binary'); | 27 bin = find(pl, 'binary'); |
42 | 28 |
43 % Make sure that 'ids' or 'cids' are empty arrays if they are empty. | 29 % Make sure that 'ids' or 'cids' are empty arrays if they are empty. |
55 end | 41 end |
56 | 42 |
57 %%% check if using binary or not: 'yes'/'no' or true/false or | 43 %%% check if using binary or not: 'yes'/'no' or true/false or |
58 %%% 'true'/'false' | 44 %%% 'true'/'false' |
59 bin = utils.prog.yes2true(bin); | 45 bin = utils.prog.yes2true(bin); |
60 | 46 |
61 % do we have a connection? | 47 % database connection |
62 closeConn = 0; | 48 conn = LTPDADatabaseConnectionManager().connect(pl); |
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 | 49 |
72 if ~isempty(cids) | 50 if ~isempty(cids) |
73 for kk=1:numel(cids) | 51 for kk=1:numel(cids) |
74 cid = cids(kk); | 52 cid = cids(kk); |
75 if isa(conn, 'database') | 53 % get the ids from the cid |
76 % get the ids from the cid | 54 ids = [ids utils.repository.getCollectionIDs(conn, 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 | 55 end |
83 end | 56 end |
84 | 57 |
85 % Get each ID | 58 % Get each ID |
86 Nids = length(ids); | 59 Nids = length(ids); |
94 | 67 |
95 %---- This id | 68 %---- This id |
96 id = ids(kk); | 69 id = ids(kk); |
97 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); | 70 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); |
98 | 71 |
99 %---- call database constructor | 72 try |
100 if bin | 73 %---- call database constructor |
101 try | 74 if bin |
102 obj = ltpda_uo.retrieve(conn, 'binary', id); | 75 obj = ltpda_uo.retrieve(conn, 'binary', id); |
103 | 76 else |
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); | 77 obj = ltpda_uo.retrieve(conn, id); |
112 end | 78 end |
113 else | 79 |
114 obj = ltpda_uo.retrieve(conn, id); | 80 if ~strcmp(class(obj), requested_class) |
81 error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj)); | |
82 end | |
83 | |
84 %---- remove the connection from the history plist | |
85 if plh.isparam('conn') | |
86 plh.remove('conn'); | |
87 end | |
88 | |
89 %---- Set only the ID of the current object to the plist | |
90 plh.pset('ID', id); | |
91 | |
92 %---- Add history-plist to output array | |
93 plhout = [plhout plh]; | |
94 | |
95 %---- Add to output array | |
96 objs = [objs obj]; | |
97 | |
98 catch ex | |
99 % close connection if we own it | |
100 if isempty(find(pl, 'conn')) | |
101 conn.close(); | |
102 end | |
103 throw(ex) | |
115 end | 104 end |
116 | 105 |
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 | 106 end |
147 | 107 |
148 % close connection | 108 % close connection if we own it |
149 if closeConn | 109 if isempty(find(pl, 'conn')) |
150 if isa(conn, 'database') | 110 conn.close(); |
151 close(conn); | |
152 else | |
153 conn.closeConnection; | |
154 end | |
155 end | 111 end |
156 | 112 |
157 end | 113 end |
158 | 114 |