Mercurial > hg > ltpda
annotate 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 |
rev | line source |
---|---|
0 | 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
15 function coll = fromRepository(coll, pl) |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
16 |
0 | 17 VERSION = '$Id: fromRepository.m,v 1.8 2010/10/29 16:09:11 ingo Exp $'; |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
18 |
0 | 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]); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
23 |
0 | 24 plh = copy(pl, 1); |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
25 |
0 | 26 % Get parameters |
27 ids = find(pl, 'id'); | |
28 cids = find(pl, 'cid'); | |
29 bin = find(pl, 'binary'); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
30 |
0 | 31 collFound = false; |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
32 |
0 | 33 %%% check if using binary or not: 'yes'/'no' or true/false or |
34 %%% 'true'/'false' | |
35 bin = utils.prog.yes2true(bin); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
36 |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
37 % database connection |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
38 conn = LTPDADatabaseConnectionManager().connect(pl); |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
39 |
0 | 40 if ~isempty(cids) |
41 for kk=1:numel(cids) | |
42 cid = cids(kk); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
43 ids = utils.repository.getCollectionIDs(conn, cid); |
0 | 44 end |
45 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
46 |
0 | 47 % Get each ID |
48 Nids = length(ids); | |
49 collObjs = {}; | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
50 |
0 | 51 for kk=1:Nids |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
52 |
0 | 53 %---- copy the input plist because each object should get an other plist |
54 plh = copy(pl, 1); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
55 |
0 | 56 %---- This id |
57 id = ids(kk); | |
58 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
59 |
0 | 60 try |
61 %---- call database constructor | |
62 if bin | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
63 robj = ltpda_uo.retrieve(conn, 'binary', id); |
0 | 64 else |
65 robj = ltpda_uo.retrieve(conn, id); | |
66 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
67 |
0 | 68 %---- remove the connection from the history plist |
69 if plh.isparam('conn') | |
70 plh.remove('conn'); | |
71 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
72 |
0 | 73 %---- Set only the ID of the current object to the plist |
74 plh.pset('ID', id); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
75 |
0 | 76 %---- Add history |
77 robj.addHistoryWoChangingUUID(robj.getInfo(class(robj), 'From Repository'), plh, [], robj.hist); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
78 |
0 | 79 %---- Add to output array |
80 if isa(robj, 'collection') | |
81 if numel(ids) == 1 | |
82 coll = robj; | |
83 return | |
84 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
85 |
0 | 86 if numel(robj.objs)>0 |
87 collFound = true; | |
88 collObjs = [collObjs; robj.objs(:)]; | |
89 end | |
90 else | |
91 collObjs = [collObjs; {robj}]; | |
92 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
93 |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
94 catch ex |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
95 % close connection if we own it |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
96 if isempty(find(pl, 'conn')) |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
97 conn.close(); |
0 | 98 end |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
99 throw(ex) |
0 | 100 end |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
101 |
0 | 102 end |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
103 |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
104 % close connection if we own it |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
105 if isempty(find(pl, 'conn')) |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
106 conn.close(); |
0 | 107 end |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
108 |
0 | 109 if collFound |
110 warning(sprintf(['!!! The output collection contains the contents of at least one retrieved collection.' ... | |
111 '\nThe history of those collection objects is discarded. The internal objects of those ' ... | |
112 'objects is retained.'])); | |
113 end | |
114 if isempty(collObjs) | |
115 warning('!!! No objects were retrieved from the ids %s and/or collection id %s', mat2str(ids), mat2str(cid)); | |
116 end | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
117 |
0 | 118 % Define collection-plist for the history |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
119 plh.pset('id', pl.find('id')); |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
120 plh.pset('cid', pl.find('cid')); |
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
121 |
0 | 122 % Set properties from the plist |
123 coll.setProperties(pl); | |
124 coll.objs = collObjs; | |
125 coll.addHistory(ii, plh, [], []); | |
22
b11e88004fca
Update collection.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
126 |
0 | 127 end |