1
|
/**
|
2
|
* Copyright (C) 2008 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*/
|
6
|
|
7
|
package eu.etaxonomy.cdm.app.util;
|
8
|
|
9
|
import java.util.List;
|
10
|
import java.util.UUID;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.springframework.transaction.TransactionStatus;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
16
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
17
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
18
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
19
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
20
|
import eu.etaxonomy.cdm.model.name.IBotanicalName;
|
21
|
import eu.etaxonomy.cdm.model.name.Rank;
|
22
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
23
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
24
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
25
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
26
|
|
27
|
/**
|
28
|
* @author a.babadshanjan
|
29
|
* @created 27.10.2008
|
30
|
*/
|
31
|
public class TestTransaction {
|
32
|
|
33
|
private static final String dbName = "cdm_test_jaxb";
|
34
|
private static final int MAX_ENTRIES = 20;
|
35
|
|
36
|
private static final ICdmDataSource db = TestDatabase.CDM_DB(dbName);
|
37
|
private static final Logger logger = Logger.getLogger(TestTransaction.class);
|
38
|
|
39
|
|
40
|
/** Modifies disjunct objects within two transactions of one application context.
|
41
|
* Flow:
|
42
|
* Start transaction #1. Modify and save taxon #1.
|
43
|
* Start transaction #2. Modify taxon #2.
|
44
|
* Commit transaction #1.
|
45
|
* Save taxon #2.
|
46
|
* Commit transaction #2.
|
47
|
*
|
48
|
* It is possible to commit transaction #2 before committing transaction #1
|
49
|
* but it is not possible to modify data after transaction #2 has been committed
|
50
|
* (LazyInitializationException). However, it is possible to save data after
|
51
|
* transaction #2 has been committed.
|
52
|
*/
|
53
|
private void modifyDisjunctObjects() {
|
54
|
|
55
|
CdmApplicationController appCtr = null;
|
56
|
logger.info("Test modifying disjunct objects");
|
57
|
|
58
|
try {
|
59
|
appCtr = CdmApplicationController.NewInstance(db, DbSchemaValidation.VALIDATE, true);
|
60
|
|
61
|
} catch (Exception e) {
|
62
|
logger.error("Error creating application controller");
|
63
|
e.printStackTrace();
|
64
|
System.exit(1);
|
65
|
}
|
66
|
|
67
|
IBotanicalName name1, name2;
|
68
|
Rank rankSpecies = Rank.SPECIES();
|
69
|
Taxon taxon1, taxon2, child1, child2;
|
70
|
|
71
|
try {
|
72
|
/* ************** Start Transaction #1 ******************************** */
|
73
|
|
74
|
TransactionStatus txStatOne = appCtr.startTransaction();
|
75
|
|
76
|
List<? extends AgentBase> agents = appCtr.getAgentService().list(null, MAX_ENTRIES, 0, null, null);
|
77
|
//List<TeamOrPersonBase> agents = appCtr.getAgentService().getAllAgents(MAX_ENTRIES, 0);
|
78
|
TeamOrPersonBase author = (TeamOrPersonBase) agents.get(0);
|
79
|
List<Reference> references = appCtr.getReferenceService().list(null, MAX_ENTRIES, 0, null, null);
|
80
|
Reference sec = references.get(0);
|
81
|
List<Taxon> taxa = appCtr.getTaxonService().list(Taxon.class, MAX_ENTRIES, 0, null, null);
|
82
|
|
83
|
name1 =
|
84
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "Hyoseris", null, "lucida", null, author, null, "1", null);
|
85
|
// Calendula L.
|
86
|
taxon1 = taxa.get(0);
|
87
|
child1 = Taxon.NewInstance(name1, sec);
|
88
|
// taxon1.addTaxonomicChild(child1, sec, "D#t1-c1");
|
89
|
appCtr.getTaxonService().saveOrUpdate(taxon1);
|
90
|
|
91
|
|
92
|
/* ************** Start Transaction #2 ******************************** */
|
93
|
|
94
|
TransactionStatus txStatTwo = appCtr.startTransaction();
|
95
|
|
96
|
name2 =
|
97
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "Hyoseris", null, "scabra", null, author, null, "2", null);
|
98
|
// Sonchus L.
|
99
|
taxon2 = taxa.get(1);
|
100
|
child2 = Taxon.NewInstance(name2, sec);
|
101
|
// taxon2.addTaxonomicChild(child2, sec, "D#t2-c2");
|
102
|
|
103
|
/* ************** Commit Transaction #1 ******************************** */
|
104
|
|
105
|
appCtr.commitTransaction(txStatOne);
|
106
|
|
107
|
UUID t2uuid = appCtr.getTaxonService().saveOrUpdate(taxon2);
|
108
|
|
109
|
/* ************** Commit Transaction #2 ******************************** */
|
110
|
|
111
|
appCtr.commitTransaction(txStatTwo);
|
112
|
|
113
|
appCtr.close();
|
114
|
logger.info("End test modifying disjunct objects");
|
115
|
|
116
|
} catch (Exception e) {
|
117
|
logger.error("Error");
|
118
|
e.printStackTrace();
|
119
|
}
|
120
|
}
|
121
|
|
122
|
|
123
|
/** Modifies shared objects within two transactions of one application context.
|
124
|
* Flow:
|
125
|
* Start transaction #1. Modify and save taxon #1.
|
126
|
* Start transaction #2. Modify taxon #1.
|
127
|
* Commit transaction #1.
|
128
|
* Save taxon #1.
|
129
|
* Commit transaction #2.
|
130
|
*/
|
131
|
private void modifySharedObjects() {
|
132
|
|
133
|
CdmApplicationController appCtr = null;
|
134
|
logger.info("Test modifying shared objects");
|
135
|
|
136
|
try {
|
137
|
appCtr = CdmApplicationController.NewInstance(db, DbSchemaValidation.VALIDATE, true);
|
138
|
|
139
|
} catch (Exception e) {
|
140
|
logger.error("Error creating application controller");
|
141
|
e.printStackTrace();
|
142
|
System.exit(1);
|
143
|
}
|
144
|
|
145
|
IBotanicalName name1, name2;
|
146
|
Rank rankSpecies = Rank.SPECIES();
|
147
|
Taxon taxon1, taxon2, child1, child2;
|
148
|
|
149
|
try {
|
150
|
/* ************** Start Transaction #1 ******************************** */
|
151
|
|
152
|
TransactionStatus txStatOne = appCtr.startTransaction();
|
153
|
|
154
|
List<TeamOrPersonBase> agents = appCtr.getAgentService().list(TeamOrPersonBase.class, MAX_ENTRIES, 0, null, null);
|
155
|
//List<TeamOrPersonBase> agents = appCtr.getAgentService().getAllAgents(MAX_ENTRIES, 0);
|
156
|
TeamOrPersonBase<?> author = agents.get(0);
|
157
|
List<Reference> references = appCtr.getReferenceService().list(null, MAX_ENTRIES, 0, null, null);
|
158
|
Reference sec = references.get(0);
|
159
|
List<Taxon> taxa = appCtr.getTaxonService().list(Taxon.class, MAX_ENTRIES, 0, null, null);
|
160
|
|
161
|
name1 =
|
162
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "Launaea", null, "child1", null, author, null, "1", null);
|
163
|
// Cichorium intybus L.
|
164
|
taxon1 = taxa.get(5);
|
165
|
child1 = Taxon.NewInstance(name1, sec);
|
166
|
// taxon1.addTaxonomicChild(child1, sec, "S#t1-c1");
|
167
|
appCtr.getTaxonService().saveOrUpdate(taxon1);
|
168
|
|
169
|
|
170
|
/* ************** Start Transaction #2 ******************************** */
|
171
|
|
172
|
TransactionStatus txStatTwo = appCtr.startTransaction();
|
173
|
|
174
|
name2 =
|
175
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "Reichardia", null, "child2", null, author, null, "2", null);
|
176
|
// Cichorium intybus L.
|
177
|
taxon2 = taxa.get(5);
|
178
|
child2 = Taxon.NewInstance(name2, sec);
|
179
|
// taxon2.addTaxonomicChild(child2, sec, "S#t1-c2");
|
180
|
|
181
|
/* ************** Commit Transaction #1 ******************************** */
|
182
|
|
183
|
appCtr.commitTransaction(txStatOne);
|
184
|
|
185
|
UUID t2uuid = appCtr.getTaxonService().saveOrUpdate(taxon2);
|
186
|
|
187
|
/* ************** Commit Transaction #2 ******************************** */
|
188
|
|
189
|
appCtr.commitTransaction(txStatTwo);
|
190
|
|
191
|
appCtr.close();
|
192
|
logger.info("End test modifying shared objects");
|
193
|
|
194
|
} catch (Exception e) {
|
195
|
logger.error("Error");
|
196
|
e.printStackTrace();
|
197
|
}
|
198
|
}
|
199
|
|
200
|
|
201
|
private void checkTransactionFacets() {
|
202
|
|
203
|
CdmApplicationController appCtr = null;
|
204
|
logger.info("Test checking transaction facets");
|
205
|
|
206
|
try {
|
207
|
appCtr = CdmApplicationController.NewInstance(db, DbSchemaValidation.VALIDATE, true);
|
208
|
|
209
|
} catch (Exception e) {
|
210
|
logger.error("Error creating application controller");
|
211
|
e.printStackTrace();
|
212
|
System.exit(1);
|
213
|
}
|
214
|
|
215
|
try {
|
216
|
/* ************** Start Transaction #1 ******************************** */
|
217
|
|
218
|
TransactionStatus txStatOne = appCtr.startTransaction();
|
219
|
appCtr.commitTransaction(txStatOne);
|
220
|
// set CdmApplicationController = debug in log4j.properties to see the transaction properties
|
221
|
appCtr.close();
|
222
|
logger.info("End test ask session for objects");
|
223
|
|
224
|
} catch (Exception e) {
|
225
|
logger.error("Error");
|
226
|
e.printStackTrace();
|
227
|
}
|
228
|
}
|
229
|
|
230
|
private void askSessionForObjects() {
|
231
|
|
232
|
CdmApplicationController appCtr = null;
|
233
|
logger.info("Test asking session for objects");
|
234
|
|
235
|
try {
|
236
|
appCtr = CdmApplicationController.NewInstance(db, DbSchemaValidation.VALIDATE, true);
|
237
|
|
238
|
} catch (Exception e) {
|
239
|
logger.error("Error creating application controller");
|
240
|
e.printStackTrace();
|
241
|
System.exit(1);
|
242
|
}
|
243
|
|
244
|
IBotanicalName name1, name1_;
|
245
|
Rank rankSpecies = Rank.SPECIES();
|
246
|
Taxon taxon1;
|
247
|
TaxonBase taxon1_;
|
248
|
UUID t1uuid;
|
249
|
|
250
|
try {
|
251
|
/* ************** Start Transaction #1 ******************************** */
|
252
|
|
253
|
TransactionStatus txStatOne = appCtr.startTransaction();
|
254
|
|
255
|
List<TeamOrPersonBase> agents = appCtr.getAgentService().list(TeamOrPersonBase.class, MAX_ENTRIES, 0, null, null);
|
256
|
TeamOrPersonBase author = agents.get(0);
|
257
|
List<Reference> references = appCtr.getReferenceService().list(null, MAX_ENTRIES, 0, null, null);
|
258
|
Reference sec = references.get(0);
|
259
|
|
260
|
name1 =
|
261
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "NewTaxon1", null, "taxon1", null, author, null, "1", null);
|
262
|
taxon1 = Taxon.NewInstance(name1, sec);
|
263
|
t1uuid = appCtr.getTaxonService().saveOrUpdate(taxon1);
|
264
|
//t1uuid = appCtr.getTaxonService().saveTaxon(taxon1, txStatOne);
|
265
|
|
266
|
/* ************** Start Transaction #2 ******************************** */
|
267
|
|
268
|
TransactionStatus txStatTwo = appCtr.startTransaction();
|
269
|
|
270
|
// ask whether object taxon1 is known
|
271
|
//getSession().
|
272
|
|
273
|
name1_ =
|
274
|
TaxonNameFactory.NewBotanicalInstance(rankSpecies, "NewTaxon1_", null, "taxon1_", null, author, null, "1_", null);
|
275
|
taxon1_ = appCtr.getTaxonService().find(t1uuid);
|
276
|
|
277
|
/* ************** Commit Transaction #1 ******************************** */
|
278
|
|
279
|
appCtr.commitTransaction(txStatOne);
|
280
|
|
281
|
//UUID t2uuid = appCtr.getTaxonService().saveTaxon(taxon2);
|
282
|
|
283
|
/* ************** Commit Transaction #2 ******************************** */
|
284
|
|
285
|
appCtr.commitTransaction(txStatTwo);
|
286
|
|
287
|
appCtr.close();
|
288
|
logger.info("End test ask session for objects");
|
289
|
|
290
|
} catch (Exception e) {
|
291
|
logger.error("Error");
|
292
|
e.printStackTrace();
|
293
|
}
|
294
|
}
|
295
|
|
296
|
|
297
|
private void test() {
|
298
|
|
299
|
/* Init DB */
|
300
|
// initDb(ICdmDataSource db, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
|
301
|
CdmApplicationController appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.CREATE, false);
|
302
|
|
303
|
/* Load test data into DB */
|
304
|
// TestDatabase.loadTestData(dbName, appCtrInit);
|
305
|
|
306
|
// checkTransactionFacets();
|
307
|
// modifyDisjunctObjects();
|
308
|
// modifySharedObjects();
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
* @param args
|
313
|
*/
|
314
|
public static void main(String[] args) {
|
315
|
TestTransaction ta = new TestTransaction();
|
316
|
ta.test();
|
317
|
}
|
318
|
}
|