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
= (List
)appCtr
.getTaxonService().list(Taxon
.class, MAX_ENTRIES
, 0, null, null);
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
<TeamOrPersonBase
> agents
= appCtr
.getAgentService().list(TeamOrPersonBase
.class, MAX_ENTRIES
, 0, null, null);
154 //List<TeamOrPersonBase> agents = appCtr.getAgentService().getAllAgents(MAX_ENTRIES, 0);
155 TeamOrPersonBase
<?
> author
= 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().list(Taxon
.class, MAX_ENTRIES
, 0, null, null);
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
<TeamOrPersonBase
> agents
= appCtr
.getAgentService().list(TeamOrPersonBase
.class, MAX_ENTRIES
, 0, null, null);
255 TeamOrPersonBase author
= agents
.get(0);
256 List
<Reference
> references
= appCtr
.getReferenceService().list(null, MAX_ENTRIES
, 0, null, null);
257 Reference
<?
> sec
= references
.get(0);
260 BotanicalName
.NewInstance(rankSpecies
, "NewTaxon1", null, "taxon1", null, author
, null, "1", null);
261 taxon1
= Taxon
.NewInstance(name1
, sec
);
262 t1uuid
= appCtr
.getTaxonService().saveOrUpdate(taxon1
);
263 //t1uuid = appCtr.getTaxonService().saveTaxon(taxon1, txStatOne);
265 /* ************** Start Transaction #2 ******************************** */
267 TransactionStatus txStatTwo
= appCtr
.startTransaction();
269 // ask whether object taxon1 is known
273 BotanicalName
.NewInstance(rankSpecies
, "NewTaxon1_", null, "taxon1_", null, author
, null, "1_", null);
274 taxon1_
= appCtr
.getTaxonService().find(t1uuid
);
276 /* ************** Commit Transaction #1 ******************************** */
278 appCtr
.commitTransaction(txStatOne
);
280 //UUID t2uuid = appCtr.getTaxonService().saveTaxon(taxon2);
282 /* ************** Commit Transaction #2 ******************************** */
284 appCtr
.commitTransaction(txStatTwo
);
287 logger
.info("End test ask session for objects");
289 } catch (Exception e
) {
290 logger
.error("Error");
296 private void test() {
299 // initDb(ICdmDataSource db, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
300 CdmApplicationController appCtrInit
= TestDatabase
.initDb(db
, DbSchemaValidation
.CREATE
, false);
302 /* Load test data into DB */
303 // TestDatabase.loadTestData(dbName, appCtrInit);
305 // checkTransactionFacets();
306 // modifyDisjunctObjects();
307 // modifySharedObjects();
313 public static void main(String
[] args
) {
314 TestTransaction ta
= new TestTransaction();