2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
7 package eu
.etaxonomy
.cdm
.app
.util
;
10 import java
.util
.UUID
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.springframework
.transaction
.TransactionStatus
;
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
;
27 * @author a.babadshanjan
30 public class TestTransaction
{
32 private static final String dbName
= "cdm_test_jaxb";
33 private static final int MAX_ENTRIES
= 20;
35 private static final ICdmDataSource db
= TestDatabase
.CDM_DB(dbName
);
36 private static final Logger logger
= Logger
.getLogger(TestTransaction
.class);
39 /** Modifies disjunct objects within two transactions of one application context.
41 * Start transaction #1. Modify and save taxon #1.
42 * Start transaction #2. Modify taxon #2.
43 * Commit transaction #1.
45 * Commit transaction #2.
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.
52 private void modifyDisjunctObjects() {
54 CdmApplicationController appCtr
= null;
55 logger
.info("Test modifying disjunct objects");
58 appCtr
= CdmApplicationController
.NewInstance(db
, DbSchemaValidation
.VALIDATE
, true);
60 } catch (Exception e
) {
61 logger
.error("Error creating application controller");
66 BotanicalName name1
, name2
;
67 Rank rankSpecies
= Rank
.SPECIES();
68 Taxon taxon1
, taxon2
, child1
, child2
;
71 /* ************** Start Transaction #1 ******************************** */
73 TransactionStatus txStatOne
= appCtr
.startTransaction();
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);
83 BotanicalName
.NewInstance(rankSpecies
, "Hyoseris", null, "lucida", null, author
, null, "1", null);
86 child1
= Taxon
.NewInstance(name1
, sec
);
87 taxon1
.addTaxonomicChild(child1
, sec
, "D#t1-c1");
88 appCtr
.getTaxonService().saveOrUpdate(taxon1
);
91 /* ************** Start Transaction #2 ******************************** */
93 TransactionStatus txStatTwo
= appCtr
.startTransaction();
96 BotanicalName
.NewInstance(rankSpecies
, "Hyoseris", null, "scabra", null, author
, null, "2", null);
99 child2
= Taxon
.NewInstance(name2
, sec
);
100 taxon2
.addTaxonomicChild(child2
, sec
, "D#t2-c2");
102 /* ************** Commit Transaction #1 ******************************** */
104 appCtr
.commitTransaction(txStatOne
);
106 UUID t2uuid
= appCtr
.getTaxonService().saveOrUpdate(taxon2
);
108 /* ************** Commit Transaction #2 ******************************** */
110 appCtr
.commitTransaction(txStatTwo
);
113 logger
.info("End test modifying disjunct objects");
115 } catch (Exception e
) {
116 logger
.error("Error");
122 /** Modifies shared objects within two transactions of one application context.
124 * Start transaction #1. Modify and save taxon #1.
125 * Start transaction #2. Modify taxon #1.
126 * Commit transaction #1.
128 * Commit transaction #2.
130 private void modifySharedObjects() {
132 CdmApplicationController appCtr
= null;
133 logger
.info("Test modifying shared objects");
136 appCtr
= CdmApplicationController
.NewInstance(db
, DbSchemaValidation
.VALIDATE
, true);
138 } catch (Exception e
) {
139 logger
.error("Error creating application controller");
144 BotanicalName name1
, name2
;
145 Rank rankSpecies
= Rank
.SPECIES();
146 Taxon taxon1
, taxon2
, child1
, child2
;
149 /* ************** Start Transaction #1 ******************************** */
151 TransactionStatus txStatOne
= appCtr
.startTransaction();
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);
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
);
169 /* ************** Start Transaction #2 ******************************** */
171 TransactionStatus txStatTwo
= appCtr
.startTransaction();
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");
180 /* ************** Commit Transaction #1 ******************************** */
182 appCtr
.commitTransaction(txStatOne
);
184 UUID t2uuid
= appCtr
.getTaxonService().saveOrUpdate(taxon2
);
186 /* ************** Commit Transaction #2 ******************************** */
188 appCtr
.commitTransaction(txStatTwo
);
191 logger
.info("End test modifying shared objects");
193 } catch (Exception e
) {
194 logger
.error("Error");
200 private void checkTransactionFacets() {
202 CdmApplicationController appCtr
= null;
203 logger
.info("Test checking transaction facets");
206 appCtr
= CdmApplicationController
.NewInstance(db
, DbSchemaValidation
.VALIDATE
, true);
208 } catch (Exception e
) {
209 logger
.error("Error creating application controller");
215 /* ************** Start Transaction #1 ******************************** */
217 TransactionStatus txStatOne
= appCtr
.startTransaction();
218 appCtr
.commitTransaction(txStatOne
);
219 // set CdmApplicationController = debug in log4j.properties to see the transaction properties
221 logger
.info("End test ask session for objects");
223 } catch (Exception e
) {
224 logger
.error("Error");
229 private void askSessionForObjects() {
231 CdmApplicationController appCtr
= null;
232 logger
.info("Test asking session for objects");
235 appCtr
= CdmApplicationController
.NewInstance(db
, DbSchemaValidation
.VALIDATE
, true);
237 } catch (Exception e
) {
238 logger
.error("Error creating application controller");
243 BotanicalName name1
, name1_
;
244 Rank rankSpecies
= Rank
.SPECIES();
250 /* ************** Start Transaction #1 ******************************** */
252 TransactionStatus txStatOne
= appCtr
.startTransaction();
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);
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);
267 /* ************** Start Transaction #2 ******************************** */
269 TransactionStatus txStatTwo
= appCtr
.startTransaction();
271 // ask whether object taxon1 is known
275 BotanicalName
.NewInstance(rankSpecies
, "NewTaxon1_", null, "taxon1_", null, author
, null, "1_", null);
276 taxon1_
= appCtr
.getTaxonService().find(t1uuid
);
278 /* ************** Commit Transaction #1 ******************************** */
280 appCtr
.commitTransaction(txStatOne
);
282 //UUID t2uuid = appCtr.getTaxonService().saveTaxon(taxon2);
284 /* ************** Commit Transaction #2 ******************************** */
286 appCtr
.commitTransaction(txStatTwo
);
289 logger
.info("End test ask session for objects");
291 } catch (Exception e
) {
292 logger
.error("Error");
298 private void test() {
301 // initDb(ICdmDataSource db, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
302 CdmApplicationController appCtrInit
= TestDatabase
.initDb(db
, DbSchemaValidation
.CREATE
, false);
304 /* Load test data into DB */
305 // TestDatabase.loadTestData(dbName, appCtrInit);
307 // checkTransactionFacets();
308 // modifyDisjunctObjects();
309 // modifySharedObjects();
315 public static void main(String
[] args
) {
316 TestTransaction ta
= new TestTransaction();