comparison m-toolbox/classes/@collection/fromRepository.m @ 22:b11e88004fca database-connection-manager

Update collection.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
21:8be9deffe989 22:b11e88004fca
10 % PARAMETER: pl: Parameter list object 10 % PARAMETER: pl: Parameter list object
11 % 11 %
12 % VERSION: $Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $ 12 % VERSION: $Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $
13 % 13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 function coll = fromRepository(coll, pli) 15 function coll = fromRepository(coll, pl)
16 16
17 VERSION = '$Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $'; 17 VERSION = '$Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $';
18 18
19 % get object info 19 % get object info
20 ii = collection.getInfo('collection', 'From Repository'); 20 ii = collection.getInfo('collection', 'From Repository');
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);
30 else
31 pl = pli;
32 end
33 plh = copy(pl, 1); 24 plh = copy(pl, 1);
34 25
35 % Get parameters 26 % Get parameters
36 hostname = find(pl, 'hostname');
37 database = find(pl, 'database');
38 user = find(pl, 'user');
39 passwd = find(pl, 'passwd');
40 ids = find(pl, 'id'); 27 ids = find(pl, 'id');
41 cids = find(pl, 'cid'); 28 cids = find(pl, 'cid');
42 bin = find(pl, 'binary'); 29 bin = find(pl, 'binary');
43 30
44 collFound = false; 31 collFound = false;
45 32
46 %%% check if using binary or not: 'yes'/'no' or true/false or 33 %%% check if using binary or not: 'yes'/'no' or true/false or
47 %%% 'true'/'false' 34 %%% 'true'/'false'
48 bin = utils.prog.yes2true(bin); 35 bin = utils.prog.yes2true(bin);
49 36
50 % do we have a connection? 37 % database connection
51 closeConn = 0; 38 conn = LTPDADatabaseConnectionManager().connect(pl);
52 if isempty(conn) 39
53 closeConn = 1;
54 % Connect to repository
55 % Connect to repository
56 if ~isempty(user) && ~isempty(passwd)
57 conn = utils.jmysql.connect(hostname, database, user, passwd);
58 else
59 conn = utils.jmysql.connect(hostname, database);
60 end
61 end
62 if ~isa(conn, 'database') && ~isa(conn, 'mpipeline.repository.RepositoryConnection')
63 error('### connection to the database failed.');
64 end
65
66 if ~isempty(cids) 40 if ~isempty(cids)
67 for kk=1:numel(cids) 41 for kk=1:numel(cids)
68 cid = cids(kk); 42 cid = cids(kk);
69 if isa(conn, 'database') 43 ids = utils.repository.getCollectionIDs(conn, cid);
70 % get the ids from the cid
71 ids = [ids utils.mysql.getObjIds(conn,cid)];
72 else
73 c_ids = mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid);
74 ids = [ids c_ids.'];
75 end
76 end 44 end
77 end 45 end
78 46
79 % Get each ID 47 % Get each ID
80 Nids = length(ids); 48 Nids = length(ids);
81 collObjs = {}; 49 collObjs = {};
82 50
83 for kk=1:Nids 51 for kk=1:Nids
84 52
85 %---- copy the input plist because each object should get an other plist 53 %---- copy the input plist because each object should get an other plist
86 plh = copy(pl, 1); 54 plh = copy(pl, 1);
87 55
88 %---- This id 56 %---- This id
89 id = ids(kk); 57 id = ids(kk);
90 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); 58 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id);
91 59
92 try 60 try
93 %---- call database constructor 61 %---- call database constructor
94 if bin 62 if bin
95 try 63 robj = ltpda_uo.retrieve(conn, 'binary', id);
96 robj = ltpda_uo.retrieve(conn, 'binary', id);
97 catch
98 warning('!!! Unable to do binary retrieval for object %d; trying to retrieve XML instead.', id);
99 robj = ltpda_uo.retrieve(conn, id);
100 end
101 else 64 else
102 robj = ltpda_uo.retrieve(conn, id); 65 robj = ltpda_uo.retrieve(conn, id);
103 end 66 end
104 67
105 % - remove connection object from plist first
106 if isempty(hostname) || isempty(database)
107 txt = textscan(conn.URL, '%s', 'Delimiter', '/', 'CollectOutput', 1);
108 plc = plist('hostname', txt{1}{3}, 'database', txt{1}{4});
109 plh = combine(pli, plc);
110 end
111
112 %---- remove the connection from the history plist 68 %---- remove the connection from the history plist
113 if plh.isparam('conn') 69 if plh.isparam('conn')
114 plh.remove('conn'); 70 plh.remove('conn');
115 end 71 end
116 72
117 %---- Set only the ID of the current object to the plist 73 %---- Set only the ID of the current object to the plist
118 plh.pset('ID', id); 74 plh.pset('ID', id);
119 75
120 %---- Add history 76 %---- Add history
121 robj.addHistoryWoChangingUUID(robj.getInfo(class(robj), 'From Repository'), plh, [], robj.hist); 77 robj.addHistoryWoChangingUUID(robj.getInfo(class(robj), 'From Repository'), plh, [], robj.hist);
122 78
123 %---- Add to output array 79 %---- Add to output array
124 if isa(robj, 'collection') 80 if isa(robj, 'collection')
125 if numel(ids) == 1 81 if numel(ids) == 1
126 coll = robj; 82 coll = robj;
127 return 83 return
128 end 84 end
129 85
130 if numel(robj.objs)>0 86 if numel(robj.objs)>0
131 collFound = true; 87 collFound = true;
132 collObjs = [collObjs; robj.objs(:)]; 88 collObjs = [collObjs; robj.objs(:)];
133 end 89 end
134 else 90 else
135 collObjs = [collObjs; {robj}]; 91 collObjs = [collObjs; {robj}];
136 end 92 end
137 catch Exception 93
138 if closeConn 94 catch ex
139 close(conn); 95 % close connection if we own it
96 if isempty(find(pl, 'conn'))
97 conn.close();
140 end 98 end
141 throw(Exception) 99 throw(ex)
142 end 100 end
143 101
144 end 102 end
145 103
146 % close connection 104 % close connection if we own it
147 if closeConn 105 if isempty(find(pl, 'conn'))
148 if isa(conn, 'database') 106 conn.close();
149 close(conn);
150 else
151 conn.closeConnection;
152 end
153 end 107 end
154 108
155 if collFound 109 if collFound
156 warning(sprintf(['!!! The output collection contains the contents of at least one retrieved collection.' ... 110 warning(sprintf(['!!! The output collection contains the contents of at least one retrieved collection.' ...
157 '\nThe history of those collection objects is discarded. The internal objects of those ' ... 111 '\nThe history of those collection objects is discarded. The internal objects of those ' ...
158 'objects is retained.'])); 112 'objects is retained.']));
159 end 113 end
160 if isempty(collObjs) 114 if isempty(collObjs)
161 warning('!!! No objects were retrieved from the ids %s and/or collection id %s', mat2str(ids), mat2str(cid)); 115 warning('!!! No objects were retrieved from the ids %s and/or collection id %s', mat2str(ids), mat2str(cid));
162 end 116 end
163 117
164 % Define collection-plist for the history 118 % Define collection-plist for the history
165 plh.pset('id', pli.find('id')); 119 plh.pset('id', pl.find('id'));
166 plh.pset('cid', pli.find('cid')); 120 plh.pset('cid', pl.find('cid'));
167 121
168 % Set properties from the plist 122 % Set properties from the plist
169 coll.setProperties(pl); 123 coll.setProperties(pl);
170 coll.objs = collObjs; 124 coll.objs = collObjs;
171 coll.addHistory(ii, plh, [], []); 125 coll.addHistory(ii, plh, [], []);
172 126
173 end 127 end
174