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