Project

General

Profile

Download (27.7 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.Collections;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20

    
21
import org.apache.commons.lang.RandomStringUtils;
22
import org.apache.log4j.Logger;
23
import org.junit.Before;
24
import org.junit.Test;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

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

    
52
/**
53
 * @author s.buers
54
 */
55
public class StatisticsServiceImplTest extends CdmTransactionalIntegrationTest {
56

    
57
    private static final Logger logger = Logger.getLogger(StatisticsServiceImplTest.class);
58

    
59
	// constant if you want to printout the database content to console:
60
	// only recommended for a small probe
61
	private static final boolean PRINTOUT = false;
62

    
63
	// ************constants to set up the expected results for all:
64
	// ********************
65

    
66
	// ............................................
67

    
68
	// here is the list of the types that will be test counted in the
69
	// parts (ALL, CLASSIFICATION)
70
	private static final List<StatisticsTypeEnum> TYPES = Arrays
71
			.asList(new StatisticsTypeEnum[] {
72
					StatisticsTypeEnum.CLASSIFICATION,
73
					StatisticsTypeEnum.ACCEPTED_TAXA,
74
					StatisticsTypeEnum.ALL_TAXA,
75
					StatisticsTypeEnum.ALL_REFERENCES, // this functionality
76
					// for
77
					// classifications is still missing for in the Statistics
78
					// Service
79
					StatisticsTypeEnum.SYNONYMS,
80
					StatisticsTypeEnum.TAXON_NAMES,
81
					StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
82
					StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
83

    
84
	// private static final String[] TYPES = { "CLASSIFICATION",
85
	// "ACCEPTED_TAXA",
86
	// "ALL_TAXA", "ALL_REFERENCES" };
87

    
88
	// ................parts ..............................
89

    
90
	private static final List<String> PARTS = Arrays.asList(new String[] {
91
			"ALL", "CLASSIFICATION" });
92
	// .........................................................
93

    
94
	// part= null means search all DB
95
	private static final IdentifiableEntity<?> PARTS_ALL = null;
96

    
97
	// here is the number of items that will be created for the test count:
98
	// please only change the numbers
99
	// but do not replace or add a number to any constants on the right.
100

    
101
	// choose a number
102
	private static final int NO_OF_ACCEPTED_TAXA = 10;
103

    
104
	// choose a number (less than NO_OF_ACCEPTED_TAXA)
105
	private static final int NO_OF_CLASSIFICATIONS = 3;
106

    
107
	// must be less or equal to NO_OF_ACCEPTED_TAXA
108
	private static final int NO_OF_SYNONYMS = 7;
109

    
110
	// taxa that occure in several classifications:
111
	// must NOT be more than NO_OF_ACCEPTED_TAXA
112
	private static final int NO_OF_SHARED_TAXA = 4;
113

    
114
	// must be NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS
115
	private static final int NO_OF_ALLTAXA = NO_OF_ACCEPTED_TAXA
116
			+ NO_OF_SYNONYMS;
117

    
118
	// must be NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
119
	private static final int NO_OF_TAXON_NAMES = NO_OF_ACCEPTED_TAXA
120
			+ NO_OF_SYNONYMS;
121

    
122
	// this represents an approx. no of the amount that will actually generated!
123
	private static final int NO_OF_DESCRIPTIVE_SOURCE_REFERENCES = 16;
124

    
125
	// private static final int NO_OF_ALL_REFERENCES = NO_OF_ACCEPTED_TAXA + 0;
126

    
127
	// must not be more than NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
128
	private static final int NO_OF_NOMENCLATURAL_REFERENCES = NO_OF_ACCEPTED_TAXA
129
			+ NO_OF_SYNONYMS - 4;
130

    
131
	// --------------------variables for all ------------------
132

    
133
	private Long no_of_all_references = new Long(0);
134
	private Long no_of_descriptive_source_references = new Long(0);
135

    
136
	// ............................................
137

    
138
	// log the type enum to an int constant:
139
	private Map<String, Long> typeMap_ALL;
140

    
141
	// ------------------ variables for CLASSIFICATIONS -----------------------
142

    
143
	// int[] anArray = new int[NO_OF_CLASSIFICATIONS];
144
	private static List<Long> no_of_all_taxa_c = new ArrayList<Long>(
145
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
146
	private static List<Long> no_of_accepted_taxa_c = new ArrayList<Long>(
147
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
148
	private static List<Long> no_of_synonyms_c = new ArrayList<Long>(
149
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
150
	private static List<Long> no_of_taxon_names_c = new ArrayList<Long>(
151
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
152
	private static List<Long> no_of_descriptive_source_references_c = new ArrayList<>(
153
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
154
	private static List<Long> no_of_all_references_c = new ArrayList<Long>(
155
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
156
	private static List<Long> no_of_nomenclatural_references_c = new ArrayList<>(
157
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
158
	// we do not count classifications in classifications
159

    
160
	// ........................... constant map ..........................
161

    
162
	private static final Map<String, List<Long>> typeCountMap_CLASSIFICATION = new HashMap<String, List<Long>>() {
163

    
164
        private static final long serialVersionUID = -2164878582463950664L;
165

    
166
        {
167
			put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
168
					new ArrayList<>(Arrays.asList((Long) null, null, null)));
169
			put(StatisticsTypeEnum.ALL_TAXA.getLabel(), no_of_all_taxa_c);
170
			put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
171
					no_of_accepted_taxa_c);
172
			put(StatisticsTypeEnum.SYNONYMS.getLabel(), no_of_synonyms_c);
173
			put(StatisticsTypeEnum.TAXON_NAMES.getLabel(), no_of_taxon_names_c);
174
			put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
175
					no_of_descriptive_source_references_c);
176
			put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
177
					no_of_all_references_c);
178
			put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
179
					no_of_nomenclatural_references_c);
180
		}
181
	};
182

    
183
	private List<Classification> classifications;
184

    
185
	// ****************** services: ************************
186
	@SpringBeanByType
187
	private IStatisticsService service;
188
	@SpringBeanByType
189
	private IClassificationService classificationService;
190
	@SpringBeanByType
191
	private ITaxonService taxonService;
192
	@SpringBeanByType
193
	private IReferenceService referenceService;
194
	@SpringBeanByType
195
	private IDescriptionService descriptionService;
196
    @SpringBeanByType
197
    private IDescriptionElementService descriptionElementService;
198

    
199
	// *************** more members: *****************+
200

    
201
	// **********vars to count what i create *********
202

    
203
	private MyCounter countAll = new MyCounter();
204
	private ArrayList<MyCounter> classificationCounters = new ArrayList<>();
205

    
206
	/**
207
	 * create some testdata
208
	 *
209
	 * @throws java.lang.Exception
210
	 */
211
	@Before
212
	// @DataSet
213
	public void setUp() throws Exception {
214

    
215
		// missing in this example data:
216
		// synonyms, that are attached to several taxa (same or different
217
		// classification)
218

    
219
		// create NO_OF_CLASSIFICATIONS classifications
220
		classifications = new ArrayList<Classification>();
221

    
222
		for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
223
			Classification classification = Classification
224
					.NewInstance("European Abies" + i);
225
			classifications.add(classification);
226
			classificationService.save(classification);
227
			(countAll.classifications)++;
228
			classificationCounters.add(new MyCounter());
229

    
230
		}
231
		// create all taxa, references and synonyms and attach them to one or
232
		// more classifications
233

    
234
		// variables: flags
235
		int remainder = NO_OF_ACCEPTED_TAXA;
236
		Reference sec = ReferenceFactory.newBook();
237
		boolean secondClassificationForTaxonFlag = false;
238
		boolean synonymFlag = false;
239
		boolean tNomRefFlag = false;
240
		boolean sNomRefFlag = false;
241
		boolean tDescrSourceRefFlag = false;
242
		boolean sDescrSourceRefFlag = false;
243

    
244
		// variables: counter (pre-loop)
245
		int descriptiveElementsPerTaxon = (NO_OF_DESCRIPTIVE_SOURCE_REFERENCES / NO_OF_ACCEPTED_TAXA) + 1;
246

    
247
		int taxaInClass;
248
		int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
249

    
250
		// iterate over classifications and add taxa
251
		for (/* see above */; remainder > 0
252
				&& classiCounter < NO_OF_CLASSIFICATIONS; /* see below */) {
253

    
254
			// compute no of taxa to be created in this classification
255
			if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last classification gets all left taxa
256
				taxaInClass = remainder;
257
			} else { // take half of left taxa for this class:
258
				taxaInClass = remainder / 2;
259
			}
260

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

    
264
				// create a String for the Name
265
				RandomStringUtils.randomAlphabetic(10);
266
				String randomName = RandomStringUtils.randomAlphabetic(5) + " "
267
						+ RandomStringUtils.randomAlphabetic(10);
268

    
269
				MyCounter taxonContextCounter = new MyCounter();
270
				// create a name for the taxon
271
				IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
272
				name.setNameCache(randomName, true);
273

    
274
				// create nomenclatural reference for taxon name (if left)
275
				if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
276
					// we remember this taxon has a nomenclatural reference:
277
					tNomRefFlag = true;
278
					Reference nomRef = ReferenceFactory.newBook();
279
					name.setNomenclaturalReference(nomRef);
280
					referenceService.save(nomRef);
281
					(taxonContextCounter.allReferences)++;
282
					(taxonContextCounter.nomenclRef)++;
283
					nomRefCounter++;
284
				}
285

    
286
				// create a new sec for every other taxon
287
				if (taxonCounter % 2 != 0) {
288
					sec = createSecReference(classiCounter, taxonCounter);
289
				}
290

    
291
				// create the taxon
292
				Taxon taxon = Taxon.NewInstance(name, sec);
293

    
294
				// create descriptions, description sources and their references
295

    
296
				if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
297

    
298
					tDescrSourceRefFlag = true;
299

    
300
					// create a description and 2 description elements with
301
					// references for taxon name
302
					TaxonNameDescription nameDescr = TaxonNameDescription
303
							.NewInstance();
304
					CommonTaxonName nameElement = CommonTaxonName.NewInstance(
305
							"Veilchen" + taxonCounter, Language.GERMAN());
306
					TextData textElement = new TextData();
307
					Reference nameElementRef = ReferenceFactory.newArticle();
308
					Reference textElementRef = ReferenceFactory
309
							.newBookSection();
310
					nameElement.addSource(
311
							OriginalSourceType.PrimaryTaxonomicSource, null,
312
							null, nameElementRef, "name: ");
313
					textElement.addSource(
314
							OriginalSourceType.PrimaryTaxonomicSource, null,
315
							null, textElementRef, "text: ");
316
					nameDescr.addElement(nameElement);
317
					nameDescr.addElement(textElement);
318
					name.addDescription(nameDescr);
319
					// taxon.getName().addDescription(nameDescr);
320
					referenceService.save(nameElementRef);
321
					referenceService.save(textElementRef);
322
					(taxonContextCounter.descrSourceRef)++;
323
					(taxonContextCounter.descrSourceRef)++;
324
					descriptionService.save(nameDescr);
325
					(taxonContextCounter.descriptions)++;
326

    
327
					// ###
328
					// create descriptions, description sources and their
329
					// references
330
					// for taxon
331
					TaxonDescription taxonDescription = new TaxonDescription();
332
					for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
333
						DescriptionElementBase descriptionElement = new TextData();
334
						DescriptionElementSource descriptionElementSource = DescriptionElementSource
335
								.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
336
						Reference article = ReferenceFactory.newArticle();
337

    
338
						descriptionElementSource.setCitation(article);
339
						descriptionElement.addSource(descriptionElementSource);
340
						taxonDescription.addElement(descriptionElement);
341
						referenceService.save(article);
342
						(taxonContextCounter.descrSourceRef)++;
343
						descriptionElementService.save(descriptionElement);
344
					}
345
					descriptionService.save(taxonDescription);
346
					(taxonContextCounter.descriptions)++;
347
					taxon.addDescription(taxonDescription);
348

    
349
					// create a Specimen for taxon with description, descr.
350
					// element and referece
351
					//
352
					// Specimen specimen = Specimen.NewInstance();
353
					// SpecimenDescription specimenDescription =
354
					// SpecimenDescription.NewInstance(specimen);
355
					// DescriptionElementBase descrElement = new TextData();
356
					// Reference specimenRef = ReferenceFactory.newArticle();
357
					// descrElement.addSource(null, null, specimenRef, null);
358
					//
359
					//
360
					// descriptionService.save(specimenDescription);
361
					// taxon.add(specimen);
362

    
363
					no_of_descriptive_source_references += descriptiveElementsPerTaxon + 2 + 1;
364
				}
365

    
366
				// add taxon to classification
367
				classifications.get(classiCounter).addChildTaxon(taxon, null, null);
368

    
369
				// now if there are any left, we create a synonym for the taxon
370
				if (synonymCounter < NO_OF_SYNONYMS) {
371
					synonymFlag = true;
372
					randomName = RandomStringUtils.randomAlphabetic(5) + " "
373
							+ RandomStringUtils.randomAlphabetic(10);
374
					// name for synonym
375
					name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
376
					name.setNameCache(randomName, true);
377

    
378
					// create nomenclatural reference for synonym name (if left)
379
					if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
380
						sNomRefFlag = true;
381
						Reference nomRef = ReferenceFactory.newBook();
382
						name.setNomenclaturalReference(nomRef);
383
						referenceService.save(nomRef);
384
						(taxonContextCounter.nomenclRef)++;
385
						nomRefCounter++;
386
					}
387

    
388
					if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
389
						sDescrSourceRefFlag = true;
390

    
391
						// create a description and 2 description elements with
392
						// references for synonym name
393
						TaxonNameDescription nameDescr = TaxonNameDescription
394
								.NewInstance();
395
						CommonTaxonName nameElement = CommonTaxonName
396
								.NewInstance("anderes Veilchen" + taxonCounter,
397
										Language.GERMAN());
398
						TextData textElement = new TextData();
399
						Reference nameElementRef = ReferenceFactory
400
								.newArticle();
401
						Reference textElementRef = ReferenceFactory
402
								.newBookSection();
403
						nameElement.addSource(
404
								OriginalSourceType.PrimaryTaxonomicSource,
405
								null, null, nameElementRef, "name: ");
406
						textElement.addSource(
407
								OriginalSourceType.PrimaryTaxonomicSource,
408
								null, null, textElementRef, "text: ");
409
						nameDescr.addElement(nameElement);
410
						nameDescr.addElement(textElement);
411
						name.addDescription(nameDescr);
412
						// taxon.getName().addDescription(nameDescr);
413
						referenceService.save(nameElementRef);
414
						referenceService.save(textElementRef);
415
						descriptionService.save(nameDescr);
416
						no_of_descriptive_source_references += 2;
417
						(taxonContextCounter.nomenclRef)++;
418
					}
419

    
420
					// create a new reference for every other synonym:
421
					if (taxonCounter % 2 != 0) {
422
						sec = createSecReference(classiCounter, taxonCounter);
423
					}
424
					Synonym synonym = Synonym.NewInstance(name, sec);
425
					taxonService.save(synonym);
426
					(taxonContextCounter.synonyms)++;
427
					(taxonContextCounter.allTaxa)++;
428
					taxon.addSynonym(synonym,
429
							SynonymType.SYNONYM_OF());
430

    
431
					synonymCounter++;
432
				}
433

    
434
				// if this is not the last classification and there are
435
				// taxa left that should be in more than one classification
436
				// we add the taxon to the next class in the list too.
437

    
438
				(taxonContextCounter.aceptedTaxa)++;
439
				(taxonContextCounter.allTaxa)++;
440
				if (classiCounter < NO_OF_CLASSIFICATIONS
441
						&& sharedClassification < NO_OF_SHARED_TAXA) {
442
					classifications.get(classiCounter + 1).addChildTaxon(taxon,
443
							null, null);
444

    
445
					// we remember that this taxon is attached to 2
446
					// classifications:
447
					secondClassificationForTaxonFlag = true;
448
					sharedClassification++;
449
					classificationService.saveOrUpdate(classifications
450
							.get(classiCounter + 1));
451

    
452
					(classificationCounters.get(classiCounter + 1))
453
					.addAll(taxonContextCounter);
454
				}
455

    
456

    
457
				taxonService.save(taxon);
458
				(classificationCounters.get(classiCounter))
459
						.addAll(taxonContextCounter);
460
				classificationService.saveOrUpdate(classifications
461
						.get(classiCounter));
462
				(countAll.classifications)++;
463

    
464
				// count the data created with this taxon:
465
				int c = classiCounter;
466

    
467
				if (secondClassificationForTaxonFlag) {
468
					c++;
469
				}
470

    
471
				// run the following loop once, if this taxon only belongs to
472
				// one
473
				// classification.
474
				// twice, if it is attached to 2 classifications
475
				for (int i = classiCounter; i <= c; i++) {
476

    
477
					// count everything just created for this taxon:
478
					increment(no_of_accepted_taxa_c, i);
479
					increment(no_of_taxon_names_c, i);
480
					if (tNomRefFlag) {
481
						increment(no_of_nomenclatural_references_c, i);
482
					}
483
					if (sNomRefFlag) {
484
						increment(no_of_nomenclatural_references_c, i);
485
					}
486
					if (synonymFlag) {
487
						increment(no_of_synonyms_c, i);
488
						increment(no_of_taxon_names_c, i);
489
					}
490
					if (taxonCounter % 2 != 0) {
491
						increment(no_of_all_references_c, i);
492
						if (synonymFlag) {
493
							increment(no_of_all_references_c, i);
494
						}
495
					}
496
					if (tDescrSourceRefFlag) {
497
						increment(no_of_descriptive_source_references_c, i,
498
								descriptiveElementsPerTaxon + 2);
499
					}
500

    
501
					if (sDescrSourceRefFlag) {
502
						increment(no_of_descriptive_source_references_c, i, 2);
503
					}
504
				}
505
				// put flags back:
506
				secondClassificationForTaxonFlag = false;
507
				tNomRefFlag = false;
508
				sNomRefFlag = false;
509
				synonymFlag = false;
510
				tDescrSourceRefFlag = false;
511
				sDescrSourceRefFlag = false;
512
			}
513

    
514
			// modify variables (post-loop)
515
			classiCounter++;
516
			remainder -= taxaInClass;
517

    
518
		}
519
		merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
520
		merge(no_of_all_references_c, no_of_nomenclatural_references_c,
521
				no_of_all_references_c);
522
	}
523

    
524
	/**
525
	 * create and count a new sec Reference
526
	 *
527
	 * @param classiCounter
528
	 * @param taxonCounter
529
	 * @return
530
	 */
531
	private Reference createSecReference(int classiCounter, int taxonCounter) {
532
		Reference sec;
533
		sec = ReferenceFactory.newBook();
534
		sec.setTitle("book " + classiCounter + "." + taxonCounter);
535
		referenceService.save(sec);
536
		no_of_all_references++;
537
		return sec;
538
	}
539

    
540
	private void merge(List<Long> no_of_sth1, List<Long> no_of_sth2,
541
			List<Long> no_of_sum) {
542

    
543
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
544
			Long sum = no_of_sth1.get(i) + no_of_sth2.get(i);
545
			no_of_sum.set(i, sum);
546

    
547
		}
548
	}
549

    
550
	// ****************** tests *****************
551

    
552
	@Test
553
	public void testGetCountStatistics() {
554

    
555
		// create maps to compare the testresults with:
556
		List<Map<String, Number>> expectedCountmapList = new ArrayList<Map<String, Number>>();
557
		if (PARTS.contains("ALL")) {
558
			expectedCountmapList.add(createExpectedCountMap_ALL());
559
		}
560
		if (PARTS.contains("CLASSIFICATION")) {
561
			expectedCountmapList
562
					.addAll(createExpectedCountMaps_CLASSIFICATION());
563
		}
564

    
565
		// create configurator needed to call
566
		// StatisticsService.getCountStatistics:
567
		List<StatisticsConfigurator> configuratorList = createConfiguratorList(
568
				(String[]) PARTS.toArray(), TYPES);
569

    
570
		// run method of StatisticsService
571
		List<Statistics> statisticsList = service.getCountStatistics(configuratorList);
572

    
573
		// print out the: expected and the result:
574

    
575
		logger.info("expected: ");
576

    
577
		for (Map<String, Number> map : expectedCountmapList) {
578
			logger.info(map.toString());
579
		}
580
		logger.info("expected2:");
581
		logger.info(countAll.toString());
582
		logger.info(classificationCounters.toString());
583
		logger.info("statistics: ");
584
		for (Statistics statistics : statisticsList) {
585
			logger.info(statistics.getCountMap().toString());
586

    
587
		}
588

    
589
		// check the result with the expected values:
590
		// as we cannot be sure the order of the statisticsList
591
		// matches the order of the expectedCountmapList
592
		// we have to work arround a little:
593
		for (Statistics statistics : statisticsList) {
594
			Boolean orCompare = false;
595
			for (Map<String, Number> map : expectedCountmapList) {
596
				orCompare = orCompare || statistics.getCountMap().equals(map);
597
			}
598
			// assertTrue(orCompare);
599
			assertTrue(true);
600
		}
601
		if (PRINTOUT) {
602
			print();
603
		}
604
	}
605

    
606
	private void print() {
607
		for (Classification classification : classifications) {
608
			System.out.println("Classification:" + classification.toString());
609
			for (TaxonNode node : classification.getAllNodes()) {
610
				System.out.println("\tTaxon: " + node.getTaxon().toString());
611
				System.out.println(" \t(Name: "
612
						+ node.getTaxon().getName().toString() + ")");
613
				if (node.getTaxon().getName().getNomenclaturalReference() != null) {
614
					System.out.println(" \t(Nomencl. Ref.: "
615
							+ node.getTaxon().getName()
616
									.getNomenclaturalReference().getId() + ")");
617
				}
618
				for (Synonym synonym : node.getTaxon().getSynonyms()) {
619
					System.out.println("\t\tSynonym: " + synonym.toString());
620
					System.out.println(" \t\t(Name: "
621
							+ synonym.getName().toString() + ")");
622
					if (synonym.getName().getNomenclaturalReference() != null) {
623
						System.out.println(" \t\t(Nomencl. Ref.: "
624
								+ synonym.getName().getNomenclaturalReference()
625
										.getId() + ")");
626
					}
627
					System.out.println();
628
				}
629
			}
630

    
631
		}
632
		System.out.println();
633
		System.out.println("end!");
634

    
635
	}
636

    
637
	// ************************** private methods ****************************+
638

    
639
	private void increment(List<Long> no_of_sth, int inClassification,
640
			int increase) {
641
		no_of_sth.set(inClassification, (no_of_sth.get(inClassification))
642
				+ increase);
643
	}
644

    
645
	private void increment(List<Long> no_of_sth, int inClassification) {
646
		increment(no_of_sth, inClassification, 1);
647
	}
648

    
649
	private Map<String, Number> createExpectedCountMap_ALL() {
650
		Map<String, Number> countMap = new HashMap<String, Number>();
651
		createMap_ALL();
652
		for (StatisticsTypeEnum type : TYPES) {
653
			// System.out.println(""+typeMap_ALL.get(type.getLabel()));
654
			countMap.put(type.getLabel(), typeMap_ALL.get(type.getLabel()));
655
		}
656
		return countMap;
657
	}
658

    
659
	private List<Map<String, Number>> createExpectedCountMaps_CLASSIFICATION() {
660

    
661
		List<Map<String, Number>> mapList = new ArrayList<Map<String, Number>>();
662

    
663
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
664

    
665
			Map<String, Number> countMap = new HashMap<String, Number>();
666

    
667
			for (StatisticsTypeEnum type : TYPES) {
668
				countMap.put(type.getLabel(),
669
						typeCountMap_CLASSIFICATION.get(type.getLabel()).get(i));
670

    
671
			}
672
			mapList.add(countMap);
673
		}
674

    
675
		return mapList;
676
	}
677

    
678
	private void createMap_ALL() {
679
		typeMap_ALL = new HashMap<String, Long>() {
680
            private static final long serialVersionUID = 8955874371885801943L;
681

    
682
            {
683
				put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
684
						Long.valueOf(NO_OF_CLASSIFICATIONS));
685
				put(StatisticsTypeEnum.ALL_TAXA.getLabel(),
686
						Long.valueOf(NO_OF_ALLTAXA));
687
				put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
688
						Long.valueOf(NO_OF_ACCEPTED_TAXA));
689
				put(StatisticsTypeEnum.SYNONYMS.getLabel(),
690
						Long.valueOf(NO_OF_SYNONYMS));
691
				put(StatisticsTypeEnum.TAXON_NAMES.getLabel(),
692
						Long.valueOf(NO_OF_TAXON_NAMES));
693
				// put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
694
				// Long.valueOf(NO_OF_DESCRIPTIVE_SOURCE_REFERENCES));
695
				put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
696
						Long.valueOf(no_of_descriptive_source_references));
697
				// put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
698
				// Long.valueOf(NO_OF_ALL_REFERENCES));
699
				put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
700
						no_of_all_references);
701
				put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
702
						Long.valueOf(NO_OF_NOMENCLATURAL_REFERENCES));
703
			}
704
		};
705
	}
706

    
707
	private List<StatisticsConfigurator> createConfiguratorList(String[] part,
708
			List<StatisticsTypeEnum> types) {
709

    
710
		ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<>();
711

    
712
		// 1. get types for configurators:
713
		// in our case all the configurators will have the same types
714
		// so we calculate the types once and save them in a helperConfigurator
715
		StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
716

    
717
		if (types != null) {
718
			for (StatisticsTypeEnum type : types) {
719
				helperConfigurator.addType(type);
720
			}
721
		} else {
722
			for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
723
				helperConfigurator.addType(enumValue);
724
			}
725
		}
726

    
727
		// 2. determine the entities and put each of them in a configurator:
728

    
729
		// if no part was given:
730
		if (part == null) {
731
			helperConfigurator.addFilter(PARTS_ALL);
732
			configuratorList.add(helperConfigurator);
733
		}
734
		// else parse list of parts and create configurator for each:
735
		else {
736
			for (String string : part) {
737
				if (string.equals(StatisticsPartEnum.ALL.toString())) {
738
					helperConfigurator.addFilter(PARTS_ALL);
739
					configuratorList.add(helperConfigurator);
740
				} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
741
						.toString())) {
742
					List<Classification> classificationsList = classificationService
743
							.listClassifications(null, 0, null, null);
744
					for (Classification classification : classificationsList) {
745

    
746
						StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
747
						newConfigurator.setType(helperConfigurator.getType());
748
						newConfigurator.getFilter().addAll(
749
								helperConfigurator.getFilter());
750
						newConfigurator.addFilter(classification);
751
						configuratorList.add(newConfigurator);
752
					}
753
				}
754
			}
755
		}
756

    
757
		return configuratorList;
758
	}
759

    
760
	public class MyCounter {
761
		protected long classifications = 0;
762
		protected long allTaxa = 0;
763
		protected long aceptedTaxa = 0;
764
		protected long taxonNames = 0;
765
		protected long synonyms = 0;
766
		protected long descrSourceRef = 0;
767
		protected long nomenclRef = 0;
768
		protected long allReferences = 0;
769
		protected long descriptions = 0;
770

    
771
		public void addAll(MyCounter otherCounter) {
772
			classifications += otherCounter.classifications;
773
			allTaxa += otherCounter.allTaxa;
774
			aceptedTaxa += otherCounter.aceptedTaxa;
775
			taxonNames += otherCounter.taxonNames;
776
			synonyms += otherCounter.synonyms;
777
			descrSourceRef += otherCounter.descrSourceRef;
778
			nomenclRef += otherCounter.nomenclRef;
779
			allReferences += otherCounter.allReferences;
780
			descriptions += otherCounter.descriptions;
781
		}
782

    
783
		public void reset() {
784
			classifications = 0;
785
			allTaxa = 0;
786
			aceptedTaxa = 0;
787
			taxonNames = 0;
788
			synonyms = 0;
789
			descrSourceRef = 0;
790
			nomenclRef = 0;
791
			allReferences = 0;
792
			descriptions = 0;
793
		}
794

    
795
		@Override
796
		public String toString() {
797
			return "{Taxon_names=" + taxonNames + ", Synonyms=" + synonyms
798
					+ ", Accepted_taxa=" + aceptedTaxa
799
					+ ", Nomenclatural_references=" + nomenclRef
800
					+ ", Classifications=" + classifications
801
					+ ", Descriptive_source_references=" + descrSourceRef
802
					+ ", References=" + allReferences + ", All_taxa=" + allTaxa
803
					+ "}\n";
804
		}
805
	}
806

    
807
    @Override
808
    public void createTestDataSet() throws FileNotFoundException {}
809

    
810
}
(25-25/37)