Mercurial > hg > ltpda
annotate m-toolbox/classes/@ltpda_uo/fromRepository.m @ 51:9d5c88356247 database-connection-manager
Make unit tests database connection parameters configurable
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 07 Dec 2011 17:24:37 +0100 |
parents | 056f8e1e995e |
children |
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.4 2010/03/16 19:16:20 ingo Exp $ | |
13 % | |
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
15 function [objs, plhout, ii] = fromRepository(obj, pl, ii) |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
16 |
0 | 17 VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $'; |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
18 |
0 | 19 requested_class = class(obj); |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
20 |
0 | 21 % Set the method version string in the minfo object |
22 ii.setMversion([VERSION '-->' ii.mversion]); | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
23 |
0 | 24 % Get parameters |
25 ids = find(pl, 'id'); | |
26 cids = find(pl, 'cid'); | |
27 bin = find(pl, 'binary'); | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
28 |
0 | 29 % Make sure that 'ids' or 'cids' are empty arrays if they are empty. |
30 % It might be that the GUI return an empty string. | |
31 if isempty(ids) | |
32 ids = []; | |
33 end | |
34 if isempty(cids) | |
35 cids = []; | |
36 end | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
37 |
0 | 38 % Check if some ID is defined |
39 if isempty(ids) && isempty(cids) | |
40 error('### Please define at least one object ID or connection ID'); | |
41 end | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
42 |
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
43 % check if using binary download |
0 | 44 bin = utils.prog.yes2true(bin); |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
45 |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
46 % database connection |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
47 conn = LTPDADatabaseConnectionManager().connect(pl); |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
48 |
0 | 49 if ~isempty(cids) |
50 for kk=1:numel(cids) | |
51 cid = cids(kk); | |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
52 % get the ids from the cid |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
53 ids = [ids utils.repository.getCollectionIDs(conn, cid)]; |
0 | 54 end |
55 end | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
56 |
0 | 57 % Get each ID |
58 Nids = length(ids); | |
59 objs = []; | |
60 plhout = []; | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
61 |
0 | 62 for kk=1:Nids |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
63 |
0 | 64 %---- copy the input plist because each object should get an other plist |
65 plh = copy(pl, 1); | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
66 |
0 | 67 %---- This id |
68 id = ids(kk); | |
69 utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
70 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
71 try |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
72 %---- call database constructor |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
73 if bin |
0 | 74 obj = ltpda_uo.retrieve(conn, 'binary', id); |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
75 else |
0 | 76 obj = ltpda_uo.retrieve(conn, id); |
77 end | |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
78 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
79 if ~strcmp(class(obj), requested_class) |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
80 error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj)); |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
81 end |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
82 |
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
83 %---- Set connection parameters in the plist |
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
84 utils.repository.adjustPlist(conn, plh); |
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
85 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
86 %---- Set only the ID of the current object to the plist |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
87 plh.pset('id', id); |
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
88 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
89 %---- Add history-plist to output array |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
90 plhout = [plhout plh]; |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
91 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
92 %---- Add to output array |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
93 objs = [objs obj]; |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
94 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
95 catch ex |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
96 % close connection if we own it |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
97 if isempty(find(pl, 'conn')) |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
98 conn.close(); |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
99 end |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
100 throw(ex) |
0 | 101 end |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
102 |
0 | 103 end |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
104 |
19
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
105 % close connection if we own it |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
106 if isempty(find(pl, 'conn')) |
69e3d49b4b0c
Update ltpda_uo.fromRepository
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
0
diff
changeset
|
107 conn.close(); |
0 | 108 end |
24
056f8e1e995e
Properly record history in fromRepository constructors
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
19
diff
changeset
|
109 |
0 | 110 end |
111 |