Project

General

Profile

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

    
11
import static org.junit.Assert.assertTrue;
12

    
13
import java.io.FileNotFoundException;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.List;
17

    
18
import org.apache.commons.lang.RandomStringUtils;
19
import org.junit.Before;
20
import org.junit.Test;
21
import org.unitils.dbunit.annotation.DataSet;
22
import org.unitils.spring.annotation.SpringBeanByType;
23

    
24
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
25
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
26
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
27
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
34
import eu.etaxonomy.cdm.model.description.TextData;
35
import eu.etaxonomy.cdm.model.name.IBotanicalName;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
38
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.taxon.Classification;
42
import eu.etaxonomy.cdm.model.taxon.Synonym;
43
import eu.etaxonomy.cdm.model.taxon.SynonymType;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
46

    
47
public class StatisticsServiceImplTest2 extends CdmTransactionalIntegrationTest {
48

    
49
	// ************constants to set up the expected results for all:
50
	// ********************
51

    
52
	// ............................................
53

    
54
	// here is the list of the types that will be test counted in the
55
	// parts (ALL, CLASSIFICATION)
56
	private static final List<StatisticsTypeEnum> TYPES = Arrays
57
			.asList(new StatisticsTypeEnum[] {
58
					StatisticsTypeEnum.CLASSIFICATION,
59
					StatisticsTypeEnum.ACCEPTED_TAXA,
60
					StatisticsTypeEnum.ALL_TAXA,
61
					StatisticsTypeEnum.ALL_REFERENCES, // this functionality
62
					// for
63
					// classifications is still missing for in the Statistics
64
					// Service
65
					StatisticsTypeEnum.SYNONYMS,
66
					StatisticsTypeEnum.TAXON_NAMES,
67
					StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
68
					StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
69

    
70
	// private static final String[] TYPES = { "CLASSIFICATION",
71
	// "ACCEPTED_TAXA",
72
	// "ALL_TAXA", "ALL_REFERENCES" };
73

    
74
	// ................parts ..............................
75

    
76
	private static final List<String> PARTS = Arrays.asList(new String[] {
77
			"ALL", "CLASSIFICATION" });
78
	// .........................................................
79

    
80
	private ArrayList<Classification> classifications;
81

    
82
	// ****************** services: ************************
83
	@SpringBeanByType
84
	private IStatisticsService service;
85
	@SpringBeanByType
86
	private IClassificationService classificationService;
87
	@SpringBeanByType
88
	private ITaxonService taxonService;
89
	@SpringBeanByType
90
	private IReferenceService referenceService;
91
	@SpringBeanByType
92
	private IDescriptionService descriptionService;
93

    
94
	// ............................................
95

    
96
	@Before
97
	// @DataSet
98
	public void setUp() throws Exception {
99
//		 createTestData(3, 10, 7, 16, 4);
100
		// OutputStream out= new ByteArrayOutputStream();
101
		// printDataSet(out);
102
		// System.out.println(out.toString());
103
	}
104

    
105
	@Test
106
	@DataSet
107
	public void testGetCountStatistics() {
108

    
109
		// create configurator needed to call
110
		// StatisticsService.getCountStatistics()
111
		List<StatisticsConfigurator> configuratorList = createConfiguratorList(
112
				(String[]) PARTS.toArray(), TYPES);
113

    
114
		// run method of StatisticsService
115
		List<Statistics> statisticsList = service
116
				.getCountStatistics(configuratorList);
117

    
118
		// print out result
119
		logger.info("statistics service result: ");
120
		for (Statistics statistics : statisticsList) {
121
			logger.info(statistics.getCountMap().toString());
122
		}
123

    
124
		assertTrue(true);
125
	}
126

    
127
	private List<StatisticsConfigurator> createConfiguratorList(String[] part,
128
			List<StatisticsTypeEnum> types) {
129

    
130
		ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<>();
131

    
132
		// 1. get types for configurators:
133
		// in our case all the configurators will have the same types
134
		// so we calculate the types once and save them in a helperConfigurator
135
		StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
136

    
137
		if (types != null) {
138
			for (StatisticsTypeEnum type : types) {
139
				helperConfigurator.addType(type);
140
			}
141
		} else {
142
			for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
143
				helperConfigurator.addType(enumValue);
144
			}
145
		}
146

    
147
		// 2. determine the entities and put each of them in a configurator:
148

    
149
		// if no part was given:
150
		if (part == null) {
151
			helperConfigurator.addFilter(null); // part= null means search all
152
												// DB
153
			configuratorList.add(helperConfigurator);
154
		}
155
		// else parse list of parts and create configurator for each:
156
		else {
157
			for (String string : part) {
158
				if (string.equals(StatisticsPartEnum.ALL.toString())) {
159
					helperConfigurator.addFilter(null);
160
					configuratorList.add(helperConfigurator);
161
				} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
162
						.toString())) {
163
					List<Classification> classificationsList = classificationService
164
							.listClassifications(null, 0, null, null);
165
					for (Classification classification : classificationsList) {
166
						StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
167
						newConfigurator.setType(helperConfigurator.getType());
168
						newConfigurator.getFilter().addAll(
169
								helperConfigurator.getFilter());
170
						newConfigurator.addFilter(classification);
171
						configuratorList.add(newConfigurator);
172
					}
173
				}
174
			}
175
		}
176

    
177
		return configuratorList;
178
	}
179

    
180
	public void createTestDataSet(int noOfClassifications, int noOfAcceptedTaxa,
181
			int noOfSynonyms, int noOfDescrSrcReferences, int sharedTaxa)
182
			throws Exception {
183

    
184
		// create more parameters:
185
		int noOfNomRefs = noOfAcceptedTaxa + noOfSynonyms - 4;
186

    
187
		// missing in this example data:
188
		// synonyms, that are attached to several taxa (same or different
189
		// classification)
190

    
191
		// --------------------variables for counting produced elements
192
		// ------------------
193

    
194
		int descrSrcReferencesCounter = 0;
195

    
196
		// create noOfClassifications classifications
197
		classifications = new ArrayList<Classification>();
198

    
199
		for (int i = 1; i <= noOfClassifications; i++) {
200
			Classification classification = Classification
201
					.NewInstance("European Abies" + i);
202
			classifications.add(classification);
203
			classificationService.save(classification);
204

    
205
		}
206
		// create all taxa, references and synonyms and attach them to one or
207
		// more classifications
208

    
209
		// variables: flags
210
		int remainder = noOfAcceptedTaxa;
211
		Reference sec = ReferenceFactory.newBook();
212
		boolean secondClassificationForTaxonFlag = false;
213
		boolean synonymFlag = false;
214
		boolean tNomRefFlag = false;
215
		boolean sNomRefFlag = false;
216
		boolean tDescrSourceRefFlag = false;
217
		boolean sDescrSourceRefFlag = false;
218

    
219
		// variables: counter (pre-loop)
220
		int descriptiveElementsPerTaxon = (noOfDescrSrcReferences / noOfAcceptedTaxa) + 1;
221

    
222
		int taxaInClass;
223
		int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
224

    
225
		// iterate over classifications and add taxa
226
		for (/* see above */; remainder > 0
227
				&& classiCounter < noOfClassifications; /* see below */) {
228

    
229
			// compute no of taxa to be created in this classification
230
			if (classiCounter >= noOfClassifications - 1) { // last classification
231
															// gets all left taxa
232
				taxaInClass = remainder;
233
			} else { // take half of left taxa for this class:
234
				taxaInClass = remainder / 2;
235
			}
236

    
237
			// iterate over amount of taxa meant to be in this classification
238
			for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
239

    
240
				// create a String for the Name
241
				RandomStringUtils.randomAlphabetic(10);
242
				String randomName = RandomStringUtils.randomAlphabetic(5) + " "
243
						+ RandomStringUtils.randomAlphabetic(10);
244

    
245
				// create a name for the taxon
246
				IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
247
				name.setNameCache(randomName, true);
248

    
249
				// create nomenclatural reference for taxon name (if left)
250
				if (nomRefCounter < noOfNomRefs) {
251
					// we remember this taxon has a nomenclatural reference:
252
					tNomRefFlag = true;
253
					Reference nomRef = ReferenceFactory.newBook();
254
					name.setNomenclaturalReference(nomRef);
255
					referenceService.save(nomRef);
256
					nomRefCounter++;
257
				}
258

    
259
				// create a new sec for every other taxon
260
				if (taxonCounter % 2 != 0) {
261
					sec = createSecReference(classiCounter, taxonCounter);
262
				}
263

    
264
				// create the taxon
265
				Taxon taxon = Taxon.NewInstance(name, sec);
266

    
267
				// create descriptions, description sources and their references
268

    
269
				if (descrSrcReferencesCounter < noOfDescrSrcReferences) {
270

    
271
					tDescrSourceRefFlag = true;
272

    
273
					// create a description and 2 description elements with
274
					// references for taxon name
275
					TaxonNameDescription nameDescr = TaxonNameDescription
276
							.NewInstance();
277
					CommonTaxonName nameElement = CommonTaxonName.NewInstance(
278
							"Veilchen" + taxonCounter, Language.GERMAN());
279
					TextData textElement = new TextData();
280
					Reference nameElementRef = ReferenceFactory.newArticle();
281
					Reference textElementRef = ReferenceFactory
282
							.newBookSection();
283
					nameElement.addSource(
284
							OriginalSourceType.PrimaryTaxonomicSource, null,
285
							null, nameElementRef, "name: ");
286
					textElement.addSource(
287
							OriginalSourceType.PrimaryTaxonomicSource, null,
288
							null, textElementRef, "text: ");
289
					nameDescr.addElement(nameElement);
290
					nameDescr.addElement(textElement);
291
					name.addDescription(nameDescr);
292
					// taxon.getName().addDescription(nameDescr);
293
					referenceService.save(nameElementRef);
294
					referenceService.save(textElementRef);
295
					descriptionService.save(nameDescr);
296
//					System.out.println("Descriptive Src Ref for TaxonName: "+nameElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
297
//					System.out.println("Descriptive Src Ref for TaxonName: "+textElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
298

    
299
					// ###
300
					// create descriptions, description sources and their
301
					// references
302
					// for taxon
303
					TaxonDescription taxonDescription = new TaxonDescription();
304
					for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
305
						DescriptionElementBase descriptionElement = new TextData();
306
						DescriptionElementSource descriptionElementSource = DescriptionElementSource
307
								.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
308
						Reference article = ReferenceFactory.newArticle();
309

    
310
						descriptionElementSource.setCitation(article);
311
						descriptionElement.addSource(descriptionElementSource);
312
						taxonDescription.addElement(descriptionElement);
313
						referenceService.save(article);
314
						descriptionService
315
								.saveDescriptionElement(descriptionElement);
316
						System.out.println("Descriptive Src Ref for Taxon: "+article.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
317

    
318
					}
319
					descriptionService.save(taxonDescription);
320
					taxon.addDescription(taxonDescription);
321

    
322
					//TODO create Sspecimen connected to taxon via TaxonDescription->DescriptionElement=IndividualAssoziation->setAssociatedSpecimenOrObservation(SpecimenOrObservationBase)
323
					// TODO and NameBase->SpecimenTypeDesignation->
324
					// DerrivedUnit???
325

    
326
					// create a Specimen for taxon with description, descr.
327
					// element and referece
328
					//
329
//					 SpecimenOrObservationBase specimen = DerivedUnit.NewInstance(SpecimenOrObservationType.Fossil);
330
//					 SpecimenDescription specimenDescription =
331
//					 SpecimenDescription.NewInstance(specimen);
332
//					 DescriptionElementBase descrElement = new TextData();
333
//					 Reference specimenRef = ReferenceFactory.newArticle();
334
////					 descrElement.add;
335
//
336
//
337
//					 descriptionService.save(specimenDescription);
338
//					 taxon.addSource(
339
//								OriginalSourceType.PrimaryTaxonomicSource,
340
//								null, null, nameElementRef, " ");
341
//					 taxon.add(specimen);
342

    
343
					descrSrcReferencesCounter += descriptiveElementsPerTaxon + 2 + 1;
344

    
345
				}
346

    
347
				// add taxon to classification
348
				classifications.get(classiCounter).addChildTaxon(taxon, null,
349
						null);
350

    
351
				// now if there are any left, we create a synonym for the taxon
352
				if (synonymCounter < noOfSynonyms) {
353
					synonymFlag = true;
354
					randomName = RandomStringUtils.randomAlphabetic(5) + " "
355
							+ RandomStringUtils.randomAlphabetic(10);
356
					// name for synonym
357
					name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
358
					name.setNameCache(randomName, true);
359

    
360
					// create nomenclatural reference for synonym name (if left)
361
					if (nomRefCounter < noOfNomRefs) {
362
						sNomRefFlag = true;
363
						Reference nomRef = ReferenceFactory.newBook();
364
						name.setNomenclaturalReference(nomRef);
365
						referenceService.save(nomRef);
366
						nomRefCounter++;
367
					}
368

    
369
					if (descrSrcReferencesCounter < noOfDescrSrcReferences) {
370
						sDescrSourceRefFlag = true;
371

    
372
						// create a description and 2 description elements with
373
						// references for synonym name
374
						TaxonNameDescription nameDescr = TaxonNameDescription
375
								.NewInstance();
376
						CommonTaxonName nameElement = CommonTaxonName
377
								.NewInstance("anderes Veilchen" + taxonCounter,
378
										Language.GERMAN());
379
						TextData textElement = new TextData();
380
						Reference nameElementRef = ReferenceFactory
381
								.newArticle();
382
						Reference textElementRef = ReferenceFactory
383
								.newBookSection();
384
						nameElement.addSource(
385
								OriginalSourceType.PrimaryTaxonomicSource,
386
								null, null, nameElementRef, "name: ");
387
						textElement.addSource(
388
								OriginalSourceType.PrimaryTaxonomicSource,
389
								null, null, textElementRef, "text: ");
390
						nameDescr.addElement(nameElement);
391
						nameDescr.addElement(textElement);
392
						name.addDescription(nameDescr);
393
						// taxon.getName().addDescription(nameDescr);
394
						referenceService.save(nameElementRef);
395
						referenceService.save(textElementRef);
396
						descriptionService.save(nameDescr);
397
						descrSrcReferencesCounter += 2;
398
						System.out.println("Descriptive Src Ref for Synonym: "+nameElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
399
						System.out.println("Descriptive Src Ref for Synonym: "+textElementRef.getId()+" Taxon: "+taxon.getId()+" name: "+taxon.getTitleCache());
400
					}
401

    
402
					// create a new reference for every other synonym:
403
					if (taxonCounter % 2 != 0) {
404
						sec = createSecReference(classiCounter, taxonCounter);
405
					}
406
					Synonym synonym = Synonym.NewInstance(name, sec);
407
					taxonService.save(synonym);
408
					taxon.addSynonym(synonym,
409
							SynonymType.SYNONYM_OF());
410

    
411
					synonymCounter++;
412
				}
413

    
414
				// if this is not the last classification and there are
415
				// taxa left that should be in more than one classification
416
				// we add the taxon to the next class in the list too.
417

    
418
				if (classiCounter < noOfClassifications
419
						&& sharedClassification < sharedTaxa) {
420
					classifications.get(classiCounter + 1).addChildTaxon(taxon,
421
							null, null);
422

    
423
					// we remember that this taxon is attached to 2
424
					// classifications:
425
					secondClassificationForTaxonFlag = true;
426
					sharedClassification++;
427
					classificationService.saveOrUpdate(classifications
428
							.get(classiCounter + 1));
429

    
430
				}
431

    
432
				taxonService.save(taxon);
433
				classificationService.saveOrUpdate(classifications
434
						.get(classiCounter));
435

    
436
				// count the data created with this taxon:
437
				int c = classiCounter;
438

    
439
				if (secondClassificationForTaxonFlag) {
440
					c++;
441
				}
442

    
443
				// put flags back:
444
				secondClassificationForTaxonFlag = false;
445
				tNomRefFlag = false;
446
				sNomRefFlag = false;
447
				synonymFlag = false;
448
				tDescrSourceRefFlag = false;
449
				sDescrSourceRefFlag = false;
450
			}
451

    
452
			// modify variables (post-loop)
453
			classiCounter++;
454
			remainder -= taxaInClass;
455

    
456
		}
457

    
458
		commit();
459

    
460
		writeDbUnitDataSetFile(new String[] { "TAXONBASE", "TAXONNAME",
461
				"TAXONRELATIONSHIP", "REFERENCE",
462
				"DESCRIPTIONELEMENTBASE",
463
				"DESCRIPTIONELEMENTBASE_ORIGINALSOURCEBASE",
464
				"ORIGINALSOURCEBASE", "DESCRIPTIONBASE", "REFERENCE_ORIGINALSOURCEBASE","LANGUAGESTRING",
465
				"CLASSIFICATION",  "TAXONNODE",
466
				"HIBERNATE_SEQUENCES" });
467

    
468
		// "AGENTBASE","HOMOTYPICALGROUP","LANGUAGESTRING",
469
		// "DESCRIPTIONELEMENTBASE_LANGUAGESTRING", "HIBERNATE_SEQUENCES"
470
	}
471

    
472
	/**
473
	 * create and count a new sec Reference
474
	 */
475
	private Reference createSecReference(int classiCounter, int taxonCounter) {
476
		Reference sec;
477
		sec = ReferenceFactory.newBook();
478
		sec.setTitle("book " + classiCounter + "." + taxonCounter);
479
		referenceService.save(sec);
480
		return sec;
481
	}
482

    
483
    @Override
484
    public void createTestDataSet() throws FileNotFoundException {}
485

    
486
}
(27-27/38)