9d9a1b67ae1d9ff73bf3255405f3e77f228bba84
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / test / function / TestService.java
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.SortedSet;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.junit.Assert;
19 import org.junit.Ignore;
20 import org.springframework.transaction.TransactionStatus;
21
22 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
23 import eu.etaxonomy.cdm.api.service.DeleteResult;
24 import eu.etaxonomy.cdm.api.service.IDescriptionService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.database.DbSchemaValidation;
27 import eu.etaxonomy.cdm.model.agent.Person;
28 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.description.Feature;
32 import eu.etaxonomy.cdm.model.name.IBotanicalName;
33 import eu.etaxonomy.cdm.model.name.INonViralName;
34 import eu.etaxonomy.cdm.model.name.IZoologicalName;
35 import eu.etaxonomy.cdm.model.name.Rank;
36 import eu.etaxonomy.cdm.model.name.TaxonName;
37 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
38 import eu.etaxonomy.cdm.model.reference.Reference;
39 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
40 import eu.etaxonomy.cdm.model.taxon.Synonym;
41 import eu.etaxonomy.cdm.model.taxon.SynonymType;
42 import eu.etaxonomy.cdm.model.taxon.Taxon;
43
44
45
46 /**
47 * <h2>NOTE</h2>
48 * This is a test for sole development purposes, it is not
49 * touched by mvn test since it is not matching the "\/**\/*Test" pattern,
50 * but it should be annotate with @Ignore when running the project a s junit suite in eclipse
51 *
52 *
53 */
54 @Ignore
55 public class TestService {
56 static Logger logger = Logger.getLogger(TestService.class);
57
58 private static final UUID TEST_TAXON_UUID = UUID.fromString("b3084573-343d-4279-ba92-4ab01bb47db5");
59 private static CdmApplicationController appCtr;
60
61 public void testAppController() {
62 logger.info("Create name objects...");
63 INonViralName nvn = TaxonNameFactory.NewNonViralInstance(Rank.SPECIES());
64
65 IBotanicalName bn = TaxonNameFactory.NewBotanicalInstance(Rank.SUBSPECIES());
66 IZoologicalName zn = TaxonNameFactory.NewZoologicalInstance(Rank.FAMILY());
67
68 logger.info("Create reference objects...");
69
70 Reference sec = ReferenceFactory.newJournal();
71 sec.setTitleCache("TestJournal", true);
72
73 logger.info("Create taxon objects...");
74 Taxon childTaxon = Taxon.NewInstance(nvn, sec);
75 Synonym syn = Synonym.NewInstance(bn, sec);
76 childTaxon.addSynonym(syn, SynonymType.SYNONYM_OF());
77 TransactionStatus txStatus = appCtr.startTransaction();
78 appCtr.getTaxonService().save(childTaxon);
79 appCtr.commitTransaction(txStatus);
80
81
82 Taxon parentTaxon = Taxon.NewInstance(zn, sec);
83 parentTaxon.setUuid(TEST_TAXON_UUID);
84 // parentTaxon.addTaxonomicChild(childTaxon, sec, null);
85
86
87 // test
88 nvn.setGenusOrUninomial("Nonvirala");
89 bn.setGenusOrUninomial("Abies");
90
91 logger.info("Create new Author agent...");
92 Person team= Person.NewInstance();
93 team.setTitleCache("AuthorAgent1", true);
94 nvn.setCombinationAuthorship(team);
95
96 logger.info("Save objects ...");
97 appCtr.getTaxonService().save(parentTaxon);
98
99 // load Name list
100 logger.info("Load existing names from db...");
101 List<TaxonName> tnList = appCtr.getNameService().list(null,1000, 0,null,null);
102 for (TaxonName tn2: tnList){
103 logger.info("Title: "+ tn2.getTitleCache() + " UUID: " + tn2.getUuid()+";");
104 }
105 }
106
107 public void testDeleteTaxa(){
108 ITaxonService taxonService = appCtr.getTaxonService();
109 TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
110 Reference ref = ReferenceFactory.newJournal();
111 Taxon taxon1 = Taxon.NewInstance(taxonName, ref);
112 Taxon taxon2 = Taxon.NewInstance(taxonName, null);
113 logger.info("Save taxon ...");
114 UUID uuidTaxon1 = taxonService.save(taxon1).getUuid();
115 logger.info(" UUID: " + uuidTaxon1);
116 UUID uuidTaxon2 = taxonService.save(taxon2).getUuid();
117 logger.info(" UUID: " + uuidTaxon2);
118 logger.info("Remove taxon ...");
119 UUID uuid = null;
120
121 DeleteResult result = taxonService.deleteTaxon(taxon1.getUuid(), null, null);
122
123 if(!result.isOk()){
124 Assert.fail();
125 }
126 logger.info(" UUID: " + uuid);
127 }
128
129
130 // public void testVocabularyLists(){
131 // TermVocabulary<NomenclaturalStatusType> voc = appCtr.getNameService().getStatusTypeVocabulary();
132 // Set<NomenclaturalStatusType> set = voc.getTermsOrderedByLabels(Language.DEFAULT());
133 // for (Object obj : set.toArray()){
134 // NomenclaturalStatusType nomStatusType = (NomenclaturalStatusType)obj;
135 // System.out.println(nomStatusType.getLabel());
136 // }
137 // TermVocabulary<NameRelationshipType> nameRelVoc = appCtr.getNameService().getNameRelationshipTypeVocabulary();
138 // Set<NameRelationshipType> nameRelSet = nameRelVoc.getTermsOrderedByLabels(Language.DEFAULT());
139 // for (Object obj : nameRelSet.toArray()){
140 // NameRelationshipType naemRelType = (NameRelationshipType)obj;
141 // System.out.println(naemRelType.getLabel());
142 // }
143 // System.out.println("=========== NAME LIST =================");
144 // List<TaxonName> nameList = appCtr.getNameService().getNamesByName("Abies%");
145 // System.out.println("Size" + nameList.size());
146 // for (TaxonName name : nameList){
147 // System.out.println("ABEIS: " + name.getTitleCache());
148 // }
149 // }
150
151 public void testDeleteRelationship(){
152 ITaxonService taxonService = appCtr.getTaxonService();
153 TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
154 Reference ref = ReferenceFactory.newJournal();
155 Taxon parent = Taxon.NewInstance(taxonName, ref);
156 Taxon child = Taxon.NewInstance(taxonName, null);
157 // parent.addTaxonomicChild(child, null, null);
158
159 logger.info("Save taxon ...");
160 UUID uuidTaxon1 = taxonService.save(parent).getUuid();
161 logger.info(" UUID: " + uuidTaxon1);
162 UUID uuidTaxon2 = taxonService.save(child).getUuid();
163 logger.info(" UUID: " + uuidTaxon2);
164
165
166 // Set<TaxonRelationship> set = parent.getRelationsToThisTaxon();
167 // for (TaxonRelationship rel : set){
168 // if (rel.getType().equals(ConceptRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
169 // parent.removeTaxonRelation(rel);
170 // }
171 // }
172
173 }
174
175 public void testTransientRank(){
176 ITaxonService taxonService = appCtr.getTaxonService();
177 TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(transientRank);
178 Reference ref = ReferenceFactory.newJournal();
179 Taxon taxon = Taxon.NewInstance(taxonName, ref);
180
181 logger.info("Save taxon ...");
182 UUID uuidTaxon1 = taxonService.save(taxon).getUuid();
183 logger.info(" UUID: " + uuidTaxon1);
184
185 }
186
187 public void testFeature(){
188 TransactionStatus tx = appCtr.startTransaction();
189 Language lang = Language.DEFAULT();
190 IDescriptionService descriptionService = appCtr.getDescriptionService();
191 TermVocabulary<Feature> voc = descriptionService.getDefaultFeatureVocabulary();
192 SortedSet<Feature> terms = voc.getTermsOrderedByLabels(lang);
193 for (DefinedTermBase term : terms){
194 logger.warn(term.getRepresentation(lang));
195 }
196 appCtr.commitTransaction(tx);
197 }
198
199
200 public void regenerateTaxonTitleCache(){
201 ITaxonService taxonService = appCtr.getTaxonService();
202 taxonService.updateCaches();
203 }
204
205 private void test(){
206 System.out.println("Start ...");
207 // testAppController();
208 //testRootTaxa();
209 //testTermApi();
210 //testDeleteTaxa();
211 //testDeleteRelationship();
212 //regenerateTaxonTitleCache();
213 //testVocabularyLists();
214 //testTransientRank();
215 testFeature();
216 System.out.println("\nEnd ...");
217 }
218
219 private static Rank transientRank = Rank.SPECIES();
220
221 private void init(){
222 try {
223 DbSchemaValidation dbSchemaValidation = DbSchemaValidation.CREATE;
224 //appCtr = CdmApplicationController.NewInstance(CdmPersistentDataSource.NewInstance("defaultMySql") , dbSchemaValidation);
225 appCtr = CdmApplicationController.NewInstance(dbSchemaValidation);
226
227
228 TaxonName name = TaxonNameFactory.NewNonViralInstance(null);
229 name.setTitleCache("Abies alba", true);
230
231 TaxonName name2 = TaxonNameFactory.NewNonViralInstance(null);
232 name2.setTitleCache("Abies beta", true);
233
234 //appCtr.getNameService().saveTaxonName(name);
235 //appCtr.getNameService().saveTaxonName(name2);
236
237 //appCtr = CdmApplicationController.NewInstance(CdmPersistentDataSource.NewInstance("rel1_1"));
238 //appCtr = new CdmApplicationController(HBM2DDL.CREATE);
239 } catch (Exception e) {
240 // TODO Auto-generated catch block
241 e.printStackTrace();
242 }
243 }
244
245 /**
246 * @param args
247 */
248 public static void main(String[] args) {
249 TestService sc = new TestService();
250 sc.init();
251 sc.test();
252 appCtr.close();
253 }
254
255 }