1
|
package eu.etaxonomy.cdm.api.service;
|
2
|
|
3
|
import static org.junit.Assert.assertTrue;
|
4
|
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Arrays;
|
7
|
import java.util.List;
|
8
|
|
9
|
import org.apache.commons.lang.RandomStringUtils;
|
10
|
import org.junit.Before;
|
11
|
import org.junit.Test;
|
12
|
import org.unitils.dbunit.annotation.DataSet;
|
13
|
import org.unitils.spring.annotation.SpringBeanByType;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
|
16
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
|
17
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
|
18
|
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
|
19
|
import eu.etaxonomy.cdm.model.common.Language;
|
20
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
21
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
24
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
25
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
26
|
import eu.etaxonomy.cdm.model.description.TextData;
|
27
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
28
|
import eu.etaxonomy.cdm.model.name.Rank;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
31
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
33
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
36
|
|
37
|
public class StatisticsServiceImplTest2 extends CdmTransactionalIntegrationTest {
|
38
|
|
39
|
// ************constants to set up the expected results for all:
|
40
|
// ********************
|
41
|
|
42
|
// ............................................
|
43
|
|
44
|
// here is the list of the types that will be test counted in the
|
45
|
// parts (ALL, CLASSIFICATION)
|
46
|
private static final List<StatisticsTypeEnum> TYPES = Arrays
|
47
|
.asList(new StatisticsTypeEnum[] {
|
48
|
StatisticsTypeEnum.CLASSIFICATION,
|
49
|
StatisticsTypeEnum.ACCEPTED_TAXA,
|
50
|
StatisticsTypeEnum.ALL_TAXA,
|
51
|
StatisticsTypeEnum.ALL_REFERENCES, // this functionality
|
52
|
// for
|
53
|
// classifications is still missing for in the Statistics
|
54
|
// Service
|
55
|
StatisticsTypeEnum.SYNONYMS,
|
56
|
StatisticsTypeEnum.TAXON_NAMES,
|
57
|
StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
|
58
|
StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
|
59
|
|
60
|
// private static final String[] TYPES = { "CLASSIFICATION",
|
61
|
// "ACCEPTED_TAXA",
|
62
|
// "ALL_TAXA", "ALL_REFERENCES" };
|
63
|
|
64
|
// ................parts ..............................
|
65
|
|
66
|
private static final List<String> PARTS = Arrays.asList(new String[] {
|
67
|
"ALL", "CLASSIFICATION" });
|
68
|
// .........................................................
|
69
|
|
70
|
private ArrayList<Classification> classifications;
|
71
|
|
72
|
// ****************** services: ************************
|
73
|
@SpringBeanByType
|
74
|
private IStatisticsService service;
|
75
|
@SpringBeanByType
|
76
|
private IClassificationService classificationService;
|
77
|
@SpringBeanByType
|
78
|
private ITaxonService taxonService;
|
79
|
@SpringBeanByType
|
80
|
private IReferenceService referenceService;
|
81
|
@SpringBeanByType
|
82
|
private IDescriptionService descriptionService;
|
83
|
|
84
|
// ............................................
|
85
|
|
86
|
@Before
|
87
|
// @DataSet
|
88
|
public void setUp() throws Exception {
|
89
|
// createTestData(3, 10, 7, 16, 4);
|
90
|
// OutputStream out= new ByteArrayOutputStream();
|
91
|
// printDataSet(out);
|
92
|
// System.out.println(out.toString());
|
93
|
}
|
94
|
|
95
|
@Test
|
96
|
@DataSet
|
97
|
public void testGetCountStatistics() {
|
98
|
|
99
|
// create configurator needed to call
|
100
|
// StatisticsService.getCountStatistics()
|
101
|
List<StatisticsConfigurator> configuratorList = createConfiguratorList(
|
102
|
(String[]) PARTS.toArray(), TYPES);
|
103
|
|
104
|
// run method of StatisticsService
|
105
|
List<Statistics> statisticsList = service
|
106
|
.getCountStatistics(configuratorList);
|
107
|
|
108
|
// print out result
|
109
|
logger.info("statistics service result: ");
|
110
|
for (Statistics statistics : statisticsList) {
|
111
|
logger.info(statistics.getCountMap().toString());
|
112
|
}
|
113
|
|
114
|
assertTrue(true);
|
115
|
}
|
116
|
|
117
|
private List<StatisticsConfigurator> createConfiguratorList(String[] part,
|
118
|
List<StatisticsTypeEnum> types) {
|
119
|
|
120
|
ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<StatisticsConfigurator>();
|
121
|
|
122
|
// 1. get types for configurators:
|
123
|
// in our case all the configurators will have the same types
|
124
|
// so we calculate the types once and save them in a helperConfigurator
|
125
|
StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
|
126
|
|
127
|
if (types != null) {
|
128
|
for (StatisticsTypeEnum type : types) {
|
129
|
helperConfigurator.addType(type);
|
130
|
}
|
131
|
} else {
|
132
|
for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
|
133
|
helperConfigurator.addType(enumValue);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
// 2. determine the entities and put each of them in a configurator:
|
138
|
|
139
|
// if no part was given:
|
140
|
if (part == null) {
|
141
|
helperConfigurator.addFilter(null); // part= null means search all
|
142
|
// DB
|
143
|
configuratorList.add(helperConfigurator);
|
144
|
}
|
145
|
// else parse list of parts and create configurator for each:
|
146
|
else {
|
147
|
for (String string : part) {
|
148
|
if (string.equals(StatisticsPartEnum.ALL.toString())) {
|
149
|
helperConfigurator.addFilter(null);
|
150
|
configuratorList.add(helperConfigurator);
|
151
|
} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
|
152
|
.toString())) {
|
153
|
List<Classification> classificationsList = classificationService
|
154
|
.listClassifications(null, 0, null, null);
|
155
|
for (Classification classification : classificationsList) {
|
156
|
|
157
|
StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
|
158
|
newConfigurator.setType(helperConfigurator.getType());
|
159
|
newConfigurator.getFilter().addAll(
|
160
|
helperConfigurator.getFilter());
|
161
|
newConfigurator.addFilter(classification);
|
162
|
configuratorList.add(newConfigurator);
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
return configuratorList;
|
170
|
}
|
171
|
|
172
|
public void createTestData(int noOfClassifications, int noOfAcceptedTaxa,
|
173
|
int noOfSynonyms, int noOfDescrSrcReferences, int sharedTaxa)
|
174
|
throws Exception {
|
175
|
|
176
|
// create more parameters:
|
177
|
int noOfNomRefs = noOfAcceptedTaxa + noOfSynonyms - 4;
|
178
|
|
179
|
// missing in this example data:
|
180
|
// synonyms, that are attached to several taxa (same or different
|
181
|
// classification)
|
182
|
|
183
|
// --------------------variables for counting produced elements
|
184
|
// ------------------
|
185
|
|
186
|
int no_of_all_references = 0;
|
187
|
int descrSrcReferencesCounter = 0;
|
188
|
|
189
|
// create noOfClassifications classifications
|
190
|
classifications = new ArrayList<Classification>();
|
191
|
|
192
|
for (int i = 1; i <= noOfClassifications; i++) {
|
193
|
Classification classification = Classification
|
194
|
.NewInstance("European Abies" + i);
|
195
|
classifications.add(classification);
|
196
|
classificationService.save(classification);
|
197
|
|
198
|
}
|
199
|
// create all taxa, references and synonyms and attach them to one or
|
200
|
// more classifications
|
201
|
|
202
|
// variables: flags
|
203
|
int remainder = noOfAcceptedTaxa;
|
204
|
Reference sec = ReferenceFactory.newBook();
|
205
|
boolean secondClassificationForTaxonFlag = false;
|
206
|
boolean synonymFlag = false;
|
207
|
boolean tNomRefFlag = false;
|
208
|
boolean sNomRefFlag = false;
|
209
|
boolean tDescrSourceRefFlag = false;
|
210
|
boolean sDescrSourceRefFlag = false;
|
211
|
|
212
|
// variables: counter (pre-loop)
|
213
|
int descriptiveElementsPerTaxon = (noOfDescrSrcReferences / noOfAcceptedTaxa) + 1;
|
214
|
|
215
|
int taxaInClass;
|
216
|
int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
|
217
|
|
218
|
// iterate over classifications and add taxa
|
219
|
for (/* see above */; remainder > 0
|
220
|
&& classiCounter < noOfClassifications; /* see below */) {
|
221
|
|
222
|
// compute no of taxa to be created in this classification
|
223
|
if (classiCounter >= noOfClassifications - 1) { // last
|
224
|
// classification
|
225
|
// gets all left
|
226
|
// taxa
|
227
|
taxaInClass = remainder;
|
228
|
} else { // take half of left taxa for this class:
|
229
|
taxaInClass = remainder / 2;
|
230
|
}
|
231
|
|
232
|
// iterate over amount of taxa meant to be in this classification
|
233
|
for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
|
234
|
|
235
|
// create a String for the Name
|
236
|
RandomStringUtils.randomAlphabetic(10);
|
237
|
String randomName = RandomStringUtils.randomAlphabetic(5) + " "
|
238
|
+ RandomStringUtils.randomAlphabetic(10);
|
239
|
|
240
|
// create a name for the taxon
|
241
|
BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
|
242
|
name.setNameCache(randomName, true);
|
243
|
|
244
|
// create nomenclatural reference for taxon name (if left)
|
245
|
if (nomRefCounter < noOfNomRefs) {
|
246
|
// we remember this taxon has a nomenclatural reference:
|
247
|
tNomRefFlag = true;
|
248
|
Reference nomRef = ReferenceFactory.newBook();
|
249
|
name.setNomenclaturalReference(nomRef);
|
250
|
referenceService.save(nomRef);
|
251
|
nomRefCounter++;
|
252
|
}
|
253
|
|
254
|
// create a new sec for every other taxon
|
255
|
if (taxonCounter % 2 != 0) {
|
256
|
sec = createSecReference(classiCounter, taxonCounter);
|
257
|
}
|
258
|
|
259
|
// create the taxon
|
260
|
Taxon taxon = Taxon.NewInstance(name, sec);
|
261
|
|
262
|
// create descriptions, description sources and their references
|
263
|
|
264
|
if (descrSrcReferencesCounter < noOfDescrSrcReferences) {
|
265
|
|
266
|
tDescrSourceRefFlag = true;
|
267
|
|
268
|
// create a description and 2 description elements with
|
269
|
// references for taxon name
|
270
|
TaxonNameDescription nameDescr = TaxonNameDescription
|
271
|
.NewInstance();
|
272
|
CommonTaxonName nameElement = CommonTaxonName.NewInstance(
|
273
|
"Veilchen" + taxonCounter, Language.GERMAN());
|
274
|
TextData textElement = new TextData();
|
275
|
Reference nameElementRef = ReferenceFactory.newArticle();
|
276
|
Reference textElementRef = ReferenceFactory
|
277
|
.newBookSection();
|
278
|
nameElement.addSource(
|
279
|
OriginalSourceType.PrimaryTaxonomicSource, null,
|
280
|
null, nameElementRef, "name: ");
|
281
|
textElement.addSource(
|
282
|
OriginalSourceType.PrimaryTaxonomicSource, null,
|
283
|
null, textElementRef, "text: ");
|
284
|
nameDescr.addElement(nameElement);
|
285
|
nameDescr.addElement(textElement);
|
286
|
name.addDescription(nameDescr);
|
287
|
// taxon.getName().addDescription(nameDescr);
|
288
|
referenceService.save(nameElementRef);
|
289
|
referenceService.save(textElementRef);
|
290
|
descriptionService.save(nameDescr);
|
291
|
System.out.println("Descriptive Src Ref for TaxonName: "+nameElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
|
292
|
System.out.println("Descriptive Src Ref for TaxonName: "+textElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
|
293
|
|
294
|
// ###
|
295
|
// create descriptions, description sources and their
|
296
|
// references
|
297
|
// for taxon
|
298
|
TaxonDescription taxonDescription = new TaxonDescription();
|
299
|
for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
|
300
|
DescriptionElementBase descriptionElement = new TextData();
|
301
|
DescriptionElementSource descriptionElementSource = DescriptionElementSource
|
302
|
.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
|
303
|
Reference article = ReferenceFactory.newArticle();
|
304
|
|
305
|
descriptionElementSource.setCitation(article);
|
306
|
descriptionElement.addSource(descriptionElementSource);
|
307
|
taxonDescription.addElement(descriptionElement);
|
308
|
referenceService.save(article);
|
309
|
descriptionService
|
310
|
.saveDescriptionElement(descriptionElement);
|
311
|
System.out.println("Descriptive Src Ref for Taxon: "+article.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
|
312
|
|
313
|
}
|
314
|
descriptionService.save(taxonDescription);
|
315
|
taxon.addDescription(taxonDescription);
|
316
|
|
317
|
//TODO create Sspecimen connected to taxon via TaxonDescription->DescriptionElement=IndividualAssoziation->setAssociatedSpecimenOrObservation(SpecimenOrObservationBase)
|
318
|
// TODO and NameBase->SpecimenTypeDesignation->
|
319
|
// DerrivedUnit???
|
320
|
|
321
|
// create a Specimen for taxon with description, descr.
|
322
|
// element and referece
|
323
|
//
|
324
|
// SpecimenOrObservationBase specimen = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
|
325
|
// SpecimenDescription specimenDescription =
|
326
|
// SpecimenDescription.NewInstance(specimen);
|
327
|
// DescriptionElementBase descrElement = new TextData();
|
328
|
// Reference specimenRef = ReferenceFactory.newArticle();
|
329
|
//// descrElement.add;
|
330
|
//
|
331
|
//
|
332
|
// descriptionService.save(specimenDescription);
|
333
|
// taxon.addSource(
|
334
|
// OriginalSourceType.PrimaryTaxonomicSource,
|
335
|
// null, null, nameElementRef, " ");
|
336
|
// taxon.add(specimen);
|
337
|
|
338
|
descrSrcReferencesCounter += descriptiveElementsPerTaxon + 2 + 1;
|
339
|
|
340
|
}
|
341
|
|
342
|
// add taxon to classification
|
343
|
classifications.get(classiCounter).addChildTaxon(taxon, null,
|
344
|
null);
|
345
|
|
346
|
// now if there are any left, we create a synonym for the taxon
|
347
|
if (synonymCounter < noOfSynonyms) {
|
348
|
synonymFlag = true;
|
349
|
randomName = RandomStringUtils.randomAlphabetic(5) + " "
|
350
|
+ RandomStringUtils.randomAlphabetic(10);
|
351
|
// name for synonym
|
352
|
name = BotanicalName.NewInstance(Rank.SPECIES());
|
353
|
name.setNameCache(randomName, true);
|
354
|
|
355
|
// create nomenclatural reference for synonym name (if left)
|
356
|
if (nomRefCounter < noOfNomRefs) {
|
357
|
sNomRefFlag = true;
|
358
|
Reference nomRef = ReferenceFactory.newBook();
|
359
|
name.setNomenclaturalReference(nomRef);
|
360
|
referenceService.save(nomRef);
|
361
|
nomRefCounter++;
|
362
|
}
|
363
|
|
364
|
if (descrSrcReferencesCounter < noOfDescrSrcReferences) {
|
365
|
sDescrSourceRefFlag = true;
|
366
|
|
367
|
// create a description and 2 description elements with
|
368
|
// references for synonym name
|
369
|
TaxonNameDescription nameDescr = TaxonNameDescription
|
370
|
.NewInstance();
|
371
|
CommonTaxonName nameElement = CommonTaxonName
|
372
|
.NewInstance("anderes Veilchen" + taxonCounter,
|
373
|
Language.GERMAN());
|
374
|
TextData textElement = new TextData();
|
375
|
Reference nameElementRef = ReferenceFactory
|
376
|
.newArticle();
|
377
|
Reference textElementRef = ReferenceFactory
|
378
|
.newBookSection();
|
379
|
nameElement.addSource(
|
380
|
OriginalSourceType.PrimaryTaxonomicSource,
|
381
|
null, null, nameElementRef, "name: ");
|
382
|
textElement.addSource(
|
383
|
OriginalSourceType.PrimaryTaxonomicSource,
|
384
|
null, null, textElementRef, "text: ");
|
385
|
nameDescr.addElement(nameElement);
|
386
|
nameDescr.addElement(textElement);
|
387
|
name.addDescription(nameDescr);
|
388
|
// taxon.getName().addDescription(nameDescr);
|
389
|
referenceService.save(nameElementRef);
|
390
|
referenceService.save(textElementRef);
|
391
|
descriptionService.save(nameDescr);
|
392
|
descrSrcReferencesCounter += 2;
|
393
|
System.out.println("Descriptive Src Ref for Synonym: "+nameElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
|
394
|
System.out.println("Descriptive Src Ref for Synonym: "+textElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
|
395
|
}
|
396
|
|
397
|
// create a new reference for every other synonym:
|
398
|
if (taxonCounter % 2 != 0) {
|
399
|
sec = createSecReference(classiCounter, taxonCounter);
|
400
|
}
|
401
|
Synonym synonym = Synonym.NewInstance(name, sec);
|
402
|
taxonService.save(synonym);
|
403
|
taxon.addSynonym(synonym,
|
404
|
SynonymRelationshipType.SYNONYM_OF());
|
405
|
|
406
|
synonymCounter++;
|
407
|
}
|
408
|
|
409
|
// if this is not the last classification and there are
|
410
|
// taxa left that should be in more than one classification
|
411
|
// we add the taxon to the next class in the list too.
|
412
|
|
413
|
if (classiCounter < noOfClassifications
|
414
|
&& sharedClassification < sharedTaxa) {
|
415
|
classifications.get(classiCounter + 1).addChildTaxon(taxon,
|
416
|
null, null);
|
417
|
|
418
|
// we remember that this taxon is attached to 2
|
419
|
// classifications:
|
420
|
secondClassificationForTaxonFlag = true;
|
421
|
sharedClassification++;
|
422
|
classificationService.saveOrUpdate(classifications
|
423
|
.get(classiCounter + 1));
|
424
|
|
425
|
}
|
426
|
|
427
|
taxonService.save(taxon);
|
428
|
classificationService.saveOrUpdate(classifications
|
429
|
.get(classiCounter));
|
430
|
|
431
|
// count the data created with this taxon:
|
432
|
int c = classiCounter;
|
433
|
|
434
|
if (secondClassificationForTaxonFlag) {
|
435
|
c++;
|
436
|
}
|
437
|
|
438
|
// put flags back:
|
439
|
secondClassificationForTaxonFlag = false;
|
440
|
tNomRefFlag = false;
|
441
|
sNomRefFlag = false;
|
442
|
synonymFlag = false;
|
443
|
tDescrSourceRefFlag = false;
|
444
|
sDescrSourceRefFlag = false;
|
445
|
}
|
446
|
|
447
|
// modify variables (post-loop)
|
448
|
classiCounter++;
|
449
|
remainder -= taxaInClass;
|
450
|
|
451
|
}
|
452
|
|
453
|
commit();
|
454
|
|
455
|
writeDbUnitDataSetFile(new String[] { "TAXONBASE", "TAXONNAMEBASE",
|
456
|
"TAXONRELATIONSHIP", "SYNONYMRELATIONSHIP", "REFERENCE",
|
457
|
"DESCRIPTIONELEMENTBASE",
|
458
|
"DESCRIPTIONELEMENTBASE_ORIGINALSOURCEBASE",
|
459
|
"ORIGINALSOURCEBASE", "DESCRIPTIONBASE", "REFERENCE_ORIGINALSOURCEBASE","LANGUAGESTRING",
|
460
|
"CLASSIFICATION", "TAXONNODE",
|
461
|
"HIBERNATE_SEQUENCES" });
|
462
|
|
463
|
// "AGENTBASE","HOMOTYPICALGROUP","LANGUAGESTRING",
|
464
|
// "DESCRIPTIONELEMENTBASE_LANGUAGESTRING", "HIBERNATE_SEQUENCES"
|
465
|
}
|
466
|
|
467
|
/**
|
468
|
* create and count a new sec Reference
|
469
|
*
|
470
|
* @param classiCounter
|
471
|
* @param taxonCounter
|
472
|
* @return
|
473
|
*/
|
474
|
private Reference createSecReference(int classiCounter, int taxonCounter) {
|
475
|
Reference sec;
|
476
|
sec = ReferenceFactory.newBook();
|
477
|
sec.setTitle("book " + classiCounter + "." + taxonCounter);
|
478
|
referenceService.save(sec);
|
479
|
return sec;
|
480
|
}
|
481
|
|
482
|
}
|