Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Revision:
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().list(Taxon.class, MAX_ENTRIES, 0, null, null);
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<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);
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<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);
258

    
259
			name1 =
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);
264

    
265
			/* ************** Start Transaction #2 ******************************** */
266

    
267
			TransactionStatus txStatTwo = appCtr.startTransaction();
268

    
269
			// ask whether object taxon1 is known
270
			//getSession().
271

    
272
			name1_ =
273
				BotanicalName.NewInstance(rankSpecies, "NewTaxon1_", null, "taxon1_", null, author, null, "1_", null);
274
	    	taxon1_ = appCtr.getTaxonService().find(t1uuid);
275

    
276
			/* ************** Commit Transaction #1 ******************************** */
277

    
278
	    	appCtr.commitTransaction(txStatOne);
279

    
280
			//UUID t2uuid = appCtr.getTaxonService().saveTaxon(taxon2);
281

    
282
			/* ************** Commit Transaction #2 ******************************** */
283

    
284
	    	appCtr.commitTransaction(txStatTwo);
285

    
286
	    	appCtr.close();
287
			logger.info("End test ask session for objects");
288

    
289
		} catch (Exception e) {
290
    		logger.error("Error");
291
    		e.printStackTrace();
292
		}
293
	}
294

    
295

    
296
	private void test() {
297

    
298
    	/* Init DB */
299
		// initDb(ICdmDataSource db, DbSchemaValidation dbSchemaValidation, boolean omitTermLoading)
300
		CdmApplicationController appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.CREATE, false);
301

    
302
		/* Load test data into DB */
303
//    	TestDatabase.loadTestData(dbName, appCtrInit);
304

    
305
//		checkTransactionFacets();
306
//		modifyDisjunctObjects();
307
//		modifySharedObjects();
308
	}
309

    
310
	/**
311
	 * @param args
312
	 */
313
	public static void  main(String[] args) {
314
		TestTransaction ta = new TestTransaction();
315
    	ta.test();
316
	}
317
}
(2-2/2)