comparison m-toolbox/classes/@collection/fromRepository.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children b11e88004fca
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.8 2010/10/29 16:09:11 ingo Exp $
13 %
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 function coll = fromRepository(coll, pli)
16
17 VERSION = '$Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $';
18
19 % get object info
20 ii = collection.getInfo('collection', 'From Repository');
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);
30 else
31 pl = pli;
32 end
33 plh = copy(pl, 1);
34
35 % 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');
41 cids = find(pl, 'cid');
42 bin = find(pl, 'binary');
43
44 collFound = false;
45
46 %%% check if using binary or not: 'yes'/'no' or true/false or
47 %%% 'true'/'false'
48 bin = utils.prog.yes2true(bin);
49
50 % do we have a connection?
51 closeConn = 0;
52 if isempty(conn)
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)
67 for kk=1:numel(cids)
68 cid = cids(kk);
69 if isa(conn, 'database')
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
77 end
78
79 % Get each ID
80 Nids = length(ids);
81 collObjs = {};
82
83 for kk=1:Nids
84
85 %---- copy the input plist because each object should get an other plist
86 plh = copy(pl, 1);
87
88 %---- This id
89 id = ids(kk);
90 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id);
91
92 try
93 %---- call database constructor
94 if bin
95 try
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
102 robj = ltpda_uo.retrieve(conn, id);
103 end
104
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
113 if plh.isparam('conn')
114 plh.remove('conn');
115 end
116
117 %---- Set only the ID of the current object to the plist
118 plh.pset('ID', id);
119
120 %---- Add history
121 robj.addHistoryWoChangingUUID(robj.getInfo(class(robj), 'From Repository'), plh, [], robj.hist);
122
123 %---- Add to output array
124 if isa(robj, 'collection')
125 if numel(ids) == 1
126 coll = robj;
127 return
128 end
129
130 if numel(robj.objs)>0
131 collFound = true;
132 collObjs = [collObjs; robj.objs(:)];
133 end
134 else
135 collObjs = [collObjs; {robj}];
136 end
137 catch Exception
138 if closeConn
139 close(conn);
140 end
141 throw(Exception)
142 end
143
144 end
145
146 % close connection
147 if closeConn
148 if isa(conn, 'database')
149 close(conn);
150 else
151 conn.closeConnection;
152 end
153 end
154
155 if collFound
156 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 ' ...
158 'objects is retained.']));
159 end
160 if isempty(collObjs)
161 warning('!!! No objects were retrieved from the ids %s and/or collection id %s', mat2str(ids), mat2str(cid));
162 end
163
164 % Define collection-plist for the history
165 plh.pset('id', pli.find('id'));
166 plh.pset('cid', pli.find('cid'));
167
168 % Set properties from the plist
169 coll.setProperties(pl);
170 coll.objs = collObjs;
171 coll.addHistory(ii, plh, [], []);
172
173 end
174