Project

General

Profile

Download (9.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/* just for testing */
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/ 
10

    
11
package eu.etaxonomy.cdm.test.function;
12

    
13
import java.util.List;
14
import java.util.Set;
15
import java.util.SortedSet;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
22
import eu.etaxonomy.cdm.api.service.IDescriptionService;
23
import eu.etaxonomy.cdm.api.service.ITaxonService;
24
import eu.etaxonomy.cdm.api.service.ITermService;
25
import eu.etaxonomy.cdm.database.DbSchemaValidation;
26
import eu.etaxonomy.cdm.model.agent.Person;
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
33
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
34
import eu.etaxonomy.cdm.model.name.NonViralName;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
37
import eu.etaxonomy.cdm.model.name.ZoologicalName;
38
import eu.etaxonomy.cdm.model.reference.IJournal;
39
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.taxon.Synonym;
42
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
43
import eu.etaxonomy.cdm.model.taxon.Taxon;
44
import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
45

    
46

    
47

    
48

    
49
public class TestService {
50
	static Logger logger = Logger.getLogger(TestService.class);
51
	
52
	private static final UUID TEST_TAXON_UUID = UUID.fromString("b3084573-343d-4279-ba92-4ab01bb47db5");
53
	private static CdmApplicationController appCtr;
54
	ReferenceFactory refFactory = ReferenceFactory.newInstance();
55
	
56
	public void testAppController() {
57
		logger.info("Create name objects...");
58
		NonViralName<?> nvn = NonViralName.NewInstance(Rank.SPECIES());
59
 		
60
		BotanicalName bn = BotanicalName.NewInstance(Rank.SUBSPECIES());
61
		ZoologicalName zn = ZoologicalName.NewInstance(Rank.FAMILY());
62
		
63
		logger.info("Create reference objects...");
64
		
65
		ReferenceBase sec = refFactory.newJournal();
66
		sec.setTitleCache("TestJournal", true);
67
		
68
		logger.info("Create taxon objects...");
69
		Taxon childTaxon = Taxon.NewInstance(nvn, sec);
70
		Synonym syn = Synonym.NewInstance(bn, sec);
71
		childTaxon.addSynonym(syn, SynonymRelationshipType.SYNONYM_OF());
72
 		TransactionStatus txStatus = appCtr.startTransaction();
73
		appCtr.getTaxonService().save(childTaxon);
74
		appCtr.commitTransaction(txStatus);
75
 		
76
 		
77
 		Taxon parentTaxon = Taxon.NewInstance(zn, sec);
78
		parentTaxon.setUuid(TEST_TAXON_UUID);
79
		parentTaxon.addTaxonomicChild(childTaxon, sec, null);
80
		
81
		
82
		// test 
83
		nvn.setGenusOrUninomial("Nonvirala");
84
		bn.setGenusOrUninomial("Abies");
85
		
86
		logger.info("Create new Author agent...");
87
		Person team= Person.NewInstance();
88
		team.setTitleCache("AuthorAgent1", true);
89
		nvn.setCombinationAuthorTeam(team);
90
		
91
		logger.info("Save objects ...");
92
 		appCtr.getTaxonService().save(parentTaxon);
93
		
94
		// load Name list 
95
		logger.info("Load existing names from db...");
96
		List<TaxonNameBase> tnList = appCtr.getNameService().list(null,1000, 0,null,null);
97
		for (TaxonNameBase tn2: tnList){
98
			logger.info("Title: "+ tn2.getTitleCache() + " UUID: " + tn2.getUuid()+";");
99
		}
100
	}
101

    
102
	public void testRootTaxa(){
103
		// load Name list 
104
		logger.info("Load existing names from db...");
105
		List<TaxonNameBase> tnList = appCtr.getNameService().list(null,1000, 0,null,null);
106
		for (TaxonNameBase tn2: tnList){
107
			logger.info("Title: "+ tn2.getTitleCache() + " UUID: " + tn2.getUuid()+";");
108
		}
109
		
110
		// load Root taxa 
111
		logger.info("Load taxon from db...");
112
		List<Taxon> taxa = appCtr.getTaxonService().getRootTaxa(null, CdmFetch.NO_FETCH(), false);
113
		for (Taxon rt: taxa){
114
			logger.info("Root taxon: "+ rt.toString());
115
			for (Taxon child: rt){
116
				logger.info("Child: "+ child.toString());
117
				logger.info("  Child.higherTaxon: "+ child.getTaxonomicParent().toString());
118
				for (Synonym synonym: child.getSynonyms()){
119
					logger.info("  Child synonyms: "+ synonym.toString());
120
				}
121
			}
122
		}
123
	}
124

    
125
	public void testTermApi(){
126
		ITermService ts = (ITermService)appCtr.getTermService();
127
		//DefinedTermBase dt = ts.getTermByUri("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
128
		//logger.warn(dt.toString());
129
		//TODO: fix ts.listTerms(0,100)
130
//		List<DefinedTermBase> dts = ts.listTerms(0,100); 
131
//		int i = 0;
132
//		for (DefinedTermBase d: dts){
133
//			i++;
134
//			if (i > 10) break;
135
//			logger.info(d.toString());
136
//		}
137
	}
138
	
139
	public void testDeleteTaxa(){
140
		ITaxonService taxonService = (ITaxonService)appCtr.getTaxonService();
141
		TaxonNameBase<?,?> taxonName = BotanicalName.NewInstance(Rank.SPECIES());
142
		ReferenceBase ref = refFactory.newJournal();
143
		Taxon taxon1 = Taxon.NewInstance(taxonName, ref);
144
		Taxon taxon2 = Taxon.NewInstance(taxonName, null);
145
		logger.info("Save taxon ...");
146
		UUID uuidTaxon1 = taxonService.save(taxon1);
147
		logger.info("  UUID: " + uuidTaxon1);
148
		UUID uuidTaxon2 = taxonService.save(taxon2);
149
		logger.info("  UUID: " + uuidTaxon2);
150
		logger.info("Remove taxon ...");
151
		UUID uuid = taxonService.delete(taxon1);
152
		logger.info("  UUID: " + uuid);
153
	}
154
	
155
	
156
	public void testVocabularyLists(){
157
		TermVocabulary<NomenclaturalStatusType> voc = appCtr.getNameService().getStatusTypeVocabulary();
158
		Set<NomenclaturalStatusType> set = voc.getTermsOrderedByLabels(Language.DEFAULT());
159
		for (Object obj : set.toArray()){
160
			NomenclaturalStatusType nomStatusType = (NomenclaturalStatusType)obj;
161
			System.out.println(nomStatusType.getLabel());
162
		}
163
		TermVocabulary<NameRelationshipType> nameRelVoc = appCtr.getNameService().getNameRelationshipTypeVocabulary();
164
		Set<NameRelationshipType> nameRelSet = nameRelVoc.getTermsOrderedByLabels(Language.DEFAULT());
165
		for (Object obj : nameRelSet.toArray()){
166
			NameRelationshipType naemRelType = (NameRelationshipType)obj;
167
			System.out.println(naemRelType.getLabel());
168
		}
169
		System.out.println("=========== NAME LIST =================");
170
		List<TaxonNameBase> nameList = appCtr.getNameService().getNamesByName("Abies%");
171
		System.out.println("Size" + nameList.size());
172
		for (TaxonNameBase name : nameList){
173
			System.out.println("ABEIS: " + name.getTitleCache());
174
		}	
175
	}
176

    
177
	public void testDeleteRelationship(){
178
		ITaxonService taxonService = (ITaxonService)appCtr.getTaxonService();
179
		TaxonNameBase<?,?> taxonName = BotanicalName.NewInstance(Rank.SPECIES());
180
		ReferenceBase ref = refFactory.newJournal();
181
		Taxon parent = Taxon.NewInstance(taxonName, ref);
182
		Taxon child = Taxon.NewInstance(taxonName, null);
183
		parent.addTaxonomicChild(child, null, null);
184
		
185
		logger.info("Save taxon ...");
186
		UUID uuidTaxon1 = taxonService.save(parent);
187
		logger.info("  UUID: " + uuidTaxon1);
188
		UUID uuidTaxon2 = taxonService.save(child);
189
		logger.info("  UUID: " + uuidTaxon2);
190
		
191
		
192
//		Set<TaxonRelationship> set = parent.getRelationsToThisTaxon();
193
//		for (TaxonRelationship rel : set){
194
//			if (rel.getType().equals(ConceptRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
195
//				parent.removeTaxonRelation(rel);
196
//			}
197
//		}
198
		
199
	}
200

    
201
	public void testTransientRank(){
202
		ITaxonService taxonService = (ITaxonService)appCtr.getTaxonService();
203
		TaxonNameBase<?,?> taxonName = BotanicalName.NewInstance(transientRank);
204
		ReferenceBase ref =  refFactory.newJournal();
205
		Taxon taxon = Taxon.NewInstance(taxonName, ref);
206
		
207
		logger.info("Save taxon ...");
208
		UUID uuidTaxon1 = taxonService.save(taxon);
209
		logger.info("  UUID: " + uuidTaxon1);
210

    
211
	}
212

    
213
	public void testFeature(){
214
		TransactionStatus tx = appCtr.startTransaction();
215
		Language lang = Language.DEFAULT();
216
		IDescriptionService descriptionService = appCtr.getDescriptionService();
217
		TermVocabulary<Feature> voc = descriptionService.getDefaultFeatureVocabulary();
218
		SortedSet<Feature> terms = voc.getTermsOrderedByLabels(lang);
219
		for (DefinedTermBase term : terms){
220
			logger.warn(term.getRepresentation(lang));
221
		}
222
		appCtr.commitTransaction(tx);
223
	}
224
	
225
	
226
	public void regenerateTaxonTitleCache(){
227
		ITaxonService taxonService = (ITaxonService)appCtr.getTaxonService();
228
		taxonService.updateTitleCache();
229
	}
230
	
231
	private void test(){
232
		System.out.println("Start ...");
233
//    	testAppController();
234
		//testRootTaxa();
235
		//testTermApi();
236
		//testDeleteTaxa();
237
		//testDeleteRelationship();
238
		//regenerateTaxonTitleCache();
239
//		testVocabularyLists();
240
//		testTransientRank();
241
		testFeature();
242
		System.out.println("\nEnd ...");
243
	}
244
	
245
	private static Rank transientRank = Rank.SPECIES();
246
	
247
	private void init(){
248
		try {
249
			DbSchemaValidation dbSchemaValidation = DbSchemaValidation.CREATE;
250
			//appCtr = CdmApplicationController.NewInstance(CdmPersistentDataSource.NewInstance("defaultMySql") , dbSchemaValidation);
251
			appCtr = CdmApplicationController.NewInstance(dbSchemaValidation);
252
			
253
			
254
			TaxonNameBase<?,?> name = NonViralName.NewInstance(null);
255
			name.setTitleCache("Abies alba", true);
256

    
257
			TaxonNameBase<?,?> name2 = NonViralName.NewInstance(null);
258
			name2.setTitleCache("Abies beta", true);
259
			
260
			//appCtr.getNameService().saveTaxonName(name);
261
			//appCtr.getNameService().saveTaxonName(name2);
262
			
263
			//appCtr = CdmApplicationController.NewInstance(CdmPersistentDataSource.NewInstance("rel1_1"));
264
			//appCtr = new CdmApplicationController(HBM2DDL.CREATE);
265
		} catch (Exception e) {
266
			// TODO Auto-generated catch block
267
			e.printStackTrace();
268
		}
269
	}
270

    
271
	/**
272
	 * @param args
273
	 */
274
	public static void  main(String[] args) {
275
		TestService sc = new TestService();
276
		sc.init();
277
    	sc.test();
278
		appCtr.close();
279
	}
280

    
281
}
(8-8/10)