Project

General

Profile

Download (28.1 KB) Statistics
| Branch: | Tag: | Revision:
1 810b7215 Andreas Müller
/**
2 da39aca0 Andreas Müller
 *
3 810b7215 Andreas Müller
 */
4 ace5aeed s.buers
package eu.etaxonomy.cdm.api.service;
5
6 810b7215 Andreas Müller
import static org.junit.Assert.assertTrue;
7 ace5aeed s.buers
8 d06ba760 Patric Plitzner
import java.io.FileNotFoundException;
9 810b7215 Andreas Müller
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.Collections;
12 ace5aeed s.buers
import java.util.HashMap;
13 810b7215 Andreas Müller
import java.util.List;
14 ace5aeed s.buers
import java.util.Map;
15 810b7215 Andreas Müller
16
import org.apache.commons.lang.RandomStringUtils;
17 ace5aeed s.buers
import org.apache.log4j.Logger;
18 810b7215 Andreas Müller
import org.junit.After;
19
import org.junit.AfterClass;
20
import org.junit.Before;
21 ace5aeed s.buers
import org.junit.BeforeClass;
22
import org.junit.Test;
23
import org.unitils.spring.annotation.SpringBeanByType;
24
25
import eu.etaxonomy.cdm.api.service.statistics.Statistics;
26
import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
27
import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
28
import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
29 810b7215 Andreas Müller
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
32
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
33
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
36
import eu.etaxonomy.cdm.model.description.TextData;
37 d7919b1c Andreas Müller
import eu.etaxonomy.cdm.model.name.IBotanicalName;
38 810b7215 Andreas Müller
import eu.etaxonomy.cdm.model.name.Rank;
39 fe34a9eb Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
40 49d6d28c Andreas Müller
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
41 810b7215 Andreas Müller
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.Synonym;
45 fbeefcaf Andreas Müller
import eu.etaxonomy.cdm.model.taxon.SynonymType;
46 810b7215 Andreas Müller
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48 ace5aeed s.buers
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
49
50 810b7215 Andreas Müller
/**
51
 * @author s.buers
52 da39aca0 Andreas Müller
 *
53 810b7215 Andreas Müller
 */
54
@SuppressWarnings({ "rawtypes", "serial" })
55
public class StatisticsServiceImplTest extends CdmTransactionalIntegrationTest {
56 ace5aeed s.buers
57 810b7215 Andreas Müller
	// constant if you want to printout the database content to console:
58
	// only recommended for a small probe
59
	private static final boolean PRINTOUT = true;
60
61
	// ************constants to set up the expected results for all:
62
	// ********************
63
64
	// ............................................
65
66
	// here is the list of the types that will be test counted in the
67
	// parts (ALL, CLASSIFICATION)
68
	private static final List<StatisticsTypeEnum> TYPES = Arrays
69
			.asList(new StatisticsTypeEnum[] {
70
					StatisticsTypeEnum.CLASSIFICATION,
71
					StatisticsTypeEnum.ACCEPTED_TAXA,
72
					StatisticsTypeEnum.ALL_TAXA,
73
					StatisticsTypeEnum.ALL_REFERENCES, // this functionality
74
					// for
75
					// classifications is still missing for in the Statistics
76
					// Service
77
					StatisticsTypeEnum.SYNONYMS,
78
					StatisticsTypeEnum.TAXON_NAMES,
79 100aec39 Andreas Müller
					StatisticsTypeEnum.NOMENCLATURAL_REFERENCES,
80 810b7215 Andreas Müller
					StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES });
81
82
	// private static final String[] TYPES = { "CLASSIFICATION",
83
	// "ACCEPTED_TAXA",
84
	// "ALL_TAXA", "ALL_REFERENCES" };
85
86
	// ................parts ..............................
87
88
	private static final List<String> PARTS = Arrays.asList(new String[] {
89
			"ALL", "CLASSIFICATION" });
90
	// .........................................................
91
92
	// part= null means search all DB
93
	private static final IdentifiableEntity PARTS_ALL = null;
94
95
	// here is the number of items that will be created for the test count:
96
	// please only change the numbers
97
	// but do not replace or add a number to any constants on the right.
98
99
	// choose a number
100
	private static final int NO_OF_ACCEPTED_TAXA = 10;
101
102
	// choose a number (less than NO_OF_ACCEPTED_TAXA)
103
	private static final int NO_OF_CLASSIFICATIONS = 3;
104
105
	// must be less or equal to NO_OF_ACCEPTED_TAXA
106
	private static final int NO_OF_SYNONYMS = 7;
107
108
	// taxa that occure in several classifications:
109
	// must NOT be more than NO_OF_ACCEPTED_TAXA
110
	private static final int NO_OF_SHARED_TAXA = 4;
111
112
	// must be NO_OF_ACCEPTED_TAXA + NO_OF_SYNONYMS
113
	private static final int NO_OF_ALLTAXA = NO_OF_ACCEPTED_TAXA
114
			+ NO_OF_SYNONYMS;
115
116
	// must be NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
117
	private static final int NO_OF_TAXON_NAMES = NO_OF_ACCEPTED_TAXA
118
			+ NO_OF_SYNONYMS;
119
120
	// this represents an approx. no of the amount that will actually generated!
121
	private static final int NO_OF_DESCRIPTIVE_SOURCE_REFERENCES = 16;
122
123
	// private static final int NO_OF_ALL_REFERENCES = NO_OF_ACCEPTED_TAXA + 0;
124 ace5aeed s.buers
125 810b7215 Andreas Müller
	// must not be more than NO_OF_ACCEPTED_TAXA+NO_OF_SYNONYMS
126 100aec39 Andreas Müller
	private static final int NO_OF_NOMENCLATURAL_REFERENCES = NO_OF_ACCEPTED_TAXA
127 810b7215 Andreas Müller
			+ NO_OF_SYNONYMS - 4;
128
129
	// --------------------variables for all ------------------
130
131
	private Long no_of_all_references = new Long(0);
132
	private Long no_of_descriptive_source_references = new Long(0);
133
134
	// ............................................
135
136
	// log the type enum to an int constant:
137
	private Map<String, Long> typeMap_ALL;
138
139
	// ------------------ variables for CLASSIFICATIONS -----------------------
140
141
	// int[] anArray = new int[NO_OF_CLASSIFICATIONS];
142
	private static List<Long> no_of_all_taxa_c = new ArrayList<Long>(
143
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
144
	private static List<Long> no_of_accepted_taxa_c = new ArrayList<Long>(
145
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
146
	private static List<Long> no_of_synonyms_c = new ArrayList<Long>(
147
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
148
	private static List<Long> no_of_taxon_names_c = new ArrayList<Long>(
149
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
150
	private static List<Long> no_of_descriptive_source_references_c = new ArrayList<Long>(
151
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
152
	private static List<Long> no_of_all_references_c = new ArrayList<Long>(
153
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
154
	private static List<Long> no_of_nomenclatural_references_c = new ArrayList<Long>(
155
			Collections.nCopies(NO_OF_CLASSIFICATIONS, new Long(0)));
156
	// we do not count classifications in classifications
157
158
	// ........................... constant map ..........................
159
160
	private static final Map<String, List<Long>> typeCountMap_CLASSIFICATION = new HashMap<String, List<Long>>() {
161
		{
162
			put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
163
					new ArrayList<Long>(Arrays.asList((Long) null, null, null)));
164
			put(StatisticsTypeEnum.ALL_TAXA.getLabel(), no_of_all_taxa_c);
165
			put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
166
					no_of_accepted_taxa_c);
167
			put(StatisticsTypeEnum.SYNONYMS.getLabel(), no_of_synonyms_c);
168
			put(StatisticsTypeEnum.TAXON_NAMES.getLabel(), no_of_taxon_names_c);
169
			put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
170
					no_of_descriptive_source_references_c);
171
			put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
172
					no_of_all_references_c);
173 100aec39 Andreas Müller
			put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
174 810b7215 Andreas Müller
					no_of_nomenclatural_references_c);
175 ace5aeed s.buers
		}
176
	};
177
178
	private static final Logger logger = Logger
179
			.getLogger(StatisticsServiceImplTest.class);
180
181 810b7215 Andreas Müller
	private List<Classification> classifications;
182 ace5aeed s.buers
183 810b7215 Andreas Müller
	// ****************** services: ************************
184 ace5aeed s.buers
	@SpringBeanByType
185
	private IStatisticsService service;
186 810b7215 Andreas Müller
	@SpringBeanByType
187
	private IClassificationService classificationService;
188
	@SpringBeanByType
189
	private ITaxonService taxonService;
190
	@SpringBeanByType
191
	private IReferenceService referenceService;
192
	@SpringBeanByType
193
	private IDescriptionService descriptionService;
194 ace5aeed s.buers
195 810b7215 Andreas Müller
	// *************** more members: *****************+
196 ace5aeed s.buers
197 0280b3f1 s.buers
	// **********vars to count what i create *********
198
199
	MyCounter countAll = new MyCounter();
200
	ArrayList<MyCounter> classificationCounters = new ArrayList<MyCounter>();
201
202 810b7215 Andreas Müller
	/**
203
	 * @throws java.lang.Exception
204
	 */
205
	@BeforeClass
206
	public static void setUpBeforeClass() throws Exception {
207
	}
208 ace5aeed s.buers
209 810b7215 Andreas Müller
	/**
210
	 * @throws java.lang.Exception
211
	 */
212
	@AfterClass
213
	public static void tearDownAfterClass() throws Exception {
214
	}
215 ace5aeed s.buers
216 810b7215 Andreas Müller
	/**
217
	 * create some testdata
218 da39aca0 Andreas Müller
	 *
219 810b7215 Andreas Müller
	 * @throws java.lang.Exception
220
	 */
221
222
	@Before
223 0280b3f1 s.buers
	// @DataSet
224 810b7215 Andreas Müller
	public void setUp() throws Exception {
225
226
		// missing in this example data:
227
		// synonyms, that are attached to several taxa (same or different
228
		// classification)
229
230
		// create NO_OF_CLASSIFICATIONS classifications
231
		classifications = new ArrayList<Classification>();
232 ace5aeed s.buers
233 810b7215 Andreas Müller
		for (int i = 1; i <= NO_OF_CLASSIFICATIONS; i++) {
234
			Classification classification = Classification
235
					.NewInstance("European Abies" + i);
236
			classifications.add(classification);
237
			classificationService.save(classification);
238 0280b3f1 s.buers
			(countAll.classifications)++;
239
			classificationCounters.add(new MyCounter());
240
241 810b7215 Andreas Müller
		}
242
		// create all taxa, references and synonyms and attach them to one or
243
		// more classifications
244
245
		// variables: flags
246
		int remainder = NO_OF_ACCEPTED_TAXA;
247
		Reference sec = ReferenceFactory.newBook();
248
		boolean secondClassificationForTaxonFlag = false;
249
		boolean synonymFlag = false;
250
		boolean tNomRefFlag = false;
251
		boolean sNomRefFlag = false;
252
		boolean tDescrSourceRefFlag = false;
253
		boolean sDescrSourceRefFlag = false;
254
255
		// variables: counter (pre-loop)
256
		int descriptiveElementsPerTaxon = (NO_OF_DESCRIPTIVE_SOURCE_REFERENCES / NO_OF_ACCEPTED_TAXA) + 1;
257 ace5aeed s.buers
258 810b7215 Andreas Müller
		int taxaInClass;
259
		int classiCounter = 0, sharedClassification = 0, synonymCounter = 0, nomRefCounter = 0;
260 ace5aeed s.buers
261 810b7215 Andreas Müller
		// iterate over classifications and add taxa
262
		for (/* see above */; remainder > 0
263
				&& classiCounter < NO_OF_CLASSIFICATIONS; /* see below */) {
264
265
			// compute no of taxa to be created in this classification
266
			if (classiCounter >= NO_OF_CLASSIFICATIONS - 1) { // last
267
																// classification
268
																// gets all left
269
																// taxa
270
				taxaInClass = remainder;
271
			} else { // take half of left taxa for this class:
272
				taxaInClass = remainder / 2;
273 ace5aeed s.buers
			}
274 810b7215 Andreas Müller
275
			// iterate over amount of taxa meant to be in this classification
276
			for (int taxonCounter = 1; taxonCounter <= taxaInClass; taxonCounter++) {
277
278
				// create a String for the Name
279
				RandomStringUtils.randomAlphabetic(10);
280
				String randomName = RandomStringUtils.randomAlphabetic(5) + " "
281
						+ RandomStringUtils.randomAlphabetic(10);
282
283 0280b3f1 s.buers
				MyCounter taxonContextCounter = new MyCounter();
284 810b7215 Andreas Müller
				// create a name for the taxon
285 d7919b1c Andreas Müller
				IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
286 810b7215 Andreas Müller
				name.setNameCache(randomName, true);
287
288
				// create nomenclatural reference for taxon name (if left)
289 100aec39 Andreas Müller
				if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
290 810b7215 Andreas Müller
					// we remember this taxon has a nomenclatural reference:
291
					tNomRefFlag = true;
292
					Reference nomRef = ReferenceFactory.newBook();
293
					name.setNomenclaturalReference(nomRef);
294
					referenceService.save(nomRef);
295 0280b3f1 s.buers
					(taxonContextCounter.allReferences)++;
296
					(taxonContextCounter.nomenclRef)++;
297 810b7215 Andreas Müller
					nomRefCounter++;
298
				}
299
300
				// create a new sec for every other taxon
301
				if (taxonCounter % 2 != 0) {
302
					sec = createSecReference(classiCounter, taxonCounter);
303
				}
304
305
				// create the taxon
306
				Taxon taxon = Taxon.NewInstance(name, sec);
307
308
				// create descriptions, description sources and their references
309
310
				if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
311
312
					tDescrSourceRefFlag = true;
313
314
					// create a description and 2 description elements with
315
					// references for taxon name
316 0280b3f1 s.buers
					TaxonNameDescription nameDescr = TaxonNameDescription
317
							.NewInstance();
318 810b7215 Andreas Müller
					CommonTaxonName nameElement = CommonTaxonName.NewInstance(
319
							"Veilchen" + taxonCounter, Language.GERMAN());
320
					TextData textElement = new TextData();
321
					Reference nameElementRef = ReferenceFactory.newArticle();
322 0280b3f1 s.buers
					Reference textElementRef = ReferenceFactory
323
							.newBookSection();
324
					nameElement.addSource(
325
							OriginalSourceType.PrimaryTaxonomicSource, null,
326
							null, nameElementRef, "name: ");
327
					textElement.addSource(
328
							OriginalSourceType.PrimaryTaxonomicSource, null,
329
							null, textElementRef, "text: ");
330 810b7215 Andreas Müller
					nameDescr.addElement(nameElement);
331
					nameDescr.addElement(textElement);
332
					name.addDescription(nameDescr);
333
					// taxon.getName().addDescription(nameDescr);
334
					referenceService.save(nameElementRef);
335
					referenceService.save(textElementRef);
336 0280b3f1 s.buers
					(taxonContextCounter.descrSourceRef)++;
337
					(taxonContextCounter.descrSourceRef)++;
338 810b7215 Andreas Müller
					descriptionService.save(nameDescr);
339 0280b3f1 s.buers
					(taxonContextCounter.descriptions)++;
340 810b7215 Andreas Müller
341 0280b3f1 s.buers
					// ###
342 810b7215 Andreas Müller
					// create descriptions, description sources and their
343
					// references
344
					// for taxon
345
					TaxonDescription taxonDescription = new TaxonDescription();
346
					for (int i = 0; i < descriptiveElementsPerTaxon; i++) {
347
						DescriptionElementBase descriptionElement = new TextData();
348
						DescriptionElementSource descriptionElementSource = DescriptionElementSource
349
								.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
350
						Reference article = ReferenceFactory.newArticle();
351
352
						descriptionElementSource.setCitation(article);
353
						descriptionElement.addSource(descriptionElementSource);
354
						taxonDescription.addElement(descriptionElement);
355
						referenceService.save(article);
356 0280b3f1 s.buers
						(taxonContextCounter.descrSourceRef)++;
357
						descriptionService
358
								.saveDescriptionElement(descriptionElement);
359 810b7215 Andreas Müller
360
					}
361
					descriptionService.save(taxonDescription);
362 0280b3f1 s.buers
					(taxonContextCounter.descriptions)++;
363 810b7215 Andreas Müller
					taxon.addDescription(taxonDescription);
364
365
					// create a Specimen for taxon with description, descr.
366
					// element and referece
367 0280b3f1 s.buers
					//
368
					// Specimen specimen = Specimen.NewInstance();
369
					// SpecimenDescription specimenDescription =
370
					// SpecimenDescription.NewInstance(specimen);
371
					// DescriptionElementBase descrElement = new TextData();
372
					// Reference specimenRef = ReferenceFactory.newArticle();
373
					// descrElement.addSource(null, null, specimenRef, null);
374
					//
375
					//
376
					// descriptionService.save(specimenDescription);
377
					// taxon.add(specimen);
378 810b7215 Andreas Müller
379
					no_of_descriptive_source_references += descriptiveElementsPerTaxon + 2 + 1;
380
381
				}
382
383
				// add taxon to classification
384 0280b3f1 s.buers
				classifications.get(classiCounter).addChildTaxon(taxon, null,
385
						null);
386 810b7215 Andreas Müller
387
				// now if there are any left, we create a synonym for the taxon
388
				if (synonymCounter < NO_OF_SYNONYMS) {
389
					synonymFlag = true;
390
					randomName = RandomStringUtils.randomAlphabetic(5) + " "
391
							+ RandomStringUtils.randomAlphabetic(10);
392
					// name for synonym
393 fe34a9eb Andreas Müller
					name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
394 810b7215 Andreas Müller
					name.setNameCache(randomName, true);
395
396
					// create nomenclatural reference for synonym name (if left)
397 100aec39 Andreas Müller
					if (nomRefCounter < NO_OF_NOMENCLATURAL_REFERENCES) {
398 810b7215 Andreas Müller
						sNomRefFlag = true;
399
						Reference nomRef = ReferenceFactory.newBook();
400
						name.setNomenclaturalReference(nomRef);
401
						referenceService.save(nomRef);
402 0280b3f1 s.buers
						(taxonContextCounter.nomenclRef)++;
403 810b7215 Andreas Müller
						nomRefCounter++;
404
					}
405
406
					if (no_of_descriptive_source_references < NO_OF_DESCRIPTIVE_SOURCE_REFERENCES) {
407
						sDescrSourceRefFlag = true;
408
409
						// create a description and 2 description elements with
410
						// references for synonym name
411 0280b3f1 s.buers
						TaxonNameDescription nameDescr = TaxonNameDescription
412
								.NewInstance();
413 810b7215 Andreas Müller
						CommonTaxonName nameElement = CommonTaxonName
414
								.NewInstance("anderes Veilchen" + taxonCounter,
415
										Language.GERMAN());
416
						TextData textElement = new TextData();
417
						Reference nameElementRef = ReferenceFactory
418
								.newArticle();
419
						Reference textElementRef = ReferenceFactory
420
								.newBookSection();
421 0280b3f1 s.buers
						nameElement.addSource(
422
								OriginalSourceType.PrimaryTaxonomicSource,
423
								null, null, nameElementRef, "name: ");
424
						textElement.addSource(
425
								OriginalSourceType.PrimaryTaxonomicSource,
426
								null, null, textElementRef, "text: ");
427 810b7215 Andreas Müller
						nameDescr.addElement(nameElement);
428
						nameDescr.addElement(textElement);
429
						name.addDescription(nameDescr);
430
						// taxon.getName().addDescription(nameDescr);
431
						referenceService.save(nameElementRef);
432
						referenceService.save(textElementRef);
433
						descriptionService.save(nameDescr);
434
						no_of_descriptive_source_references += 2;
435 0280b3f1 s.buers
						(taxonContextCounter.nomenclRef)++;
436 810b7215 Andreas Müller
					}
437
438
					// create a new reference for every other synonym:
439
					if (taxonCounter % 2 != 0) {
440
						sec = createSecReference(classiCounter, taxonCounter);
441
					}
442
					Synonym synonym = Synonym.NewInstance(name, sec);
443
					taxonService.save(synonym);
444 0280b3f1 s.buers
					(taxonContextCounter.synonyms)++;
445
					(taxonContextCounter.allTaxa)++;
446 810b7215 Andreas Müller
					taxon.addSynonym(synonym,
447 fbeefcaf Andreas Müller
							SynonymType.SYNONYM_OF());
448 810b7215 Andreas Müller
449
					synonymCounter++;
450
				}
451
452
				// if this is not the last classification and there are
453
				// taxa left that should be in more than one classification
454
				// we add the taxon to the next class in the list too.
455 0280b3f1 s.buers
456
				(taxonContextCounter.aceptedTaxa)++;
457
				(taxonContextCounter.allTaxa)++;
458 810b7215 Andreas Müller
				if (classiCounter < NO_OF_CLASSIFICATIONS
459
						&& sharedClassification < NO_OF_SHARED_TAXA) {
460 0280b3f1 s.buers
					classifications.get(classiCounter + 1).addChildTaxon(taxon,
461
							null, null);
462 810b7215 Andreas Müller
463
					// we remember that this taxon is attached to 2
464
					// classifications:
465
					secondClassificationForTaxonFlag = true;
466
					sharedClassification++;
467
					classificationService.saveOrUpdate(classifications
468
							.get(classiCounter + 1));
469 da39aca0 Andreas Müller
470 0280b3f1 s.buers
					(classificationCounters.get(classiCounter + 1))
471
					.addAll(taxonContextCounter);
472 810b7215 Andreas Müller
				}
473
474 0280b3f1 s.buers
475 810b7215 Andreas Müller
				taxonService.save(taxon);
476 0280b3f1 s.buers
				(classificationCounters.get(classiCounter))
477
						.addAll(taxonContextCounter);
478 810b7215 Andreas Müller
				classificationService.saveOrUpdate(classifications
479
						.get(classiCounter));
480 0280b3f1 s.buers
				(countAll.classifications)++;
481 810b7215 Andreas Müller
482
				// count the data created with this taxon:
483
				int c = classiCounter;
484
485
				if (secondClassificationForTaxonFlag) {
486
					c++;
487
				}
488
489
				// run the following loop once, if this taxon only belongs to
490
				// one
491
				// classification.
492
				// twice, if it is attached to 2 classifications
493
				for (int i = classiCounter; i <= c; i++) {
494
495
					// count everything just created for this taxon:
496
					increment(no_of_accepted_taxa_c, i);
497
					increment(no_of_taxon_names_c, i);
498
					if (tNomRefFlag) {
499
						increment(no_of_nomenclatural_references_c, i);
500
					}
501
					if (sNomRefFlag) {
502
						increment(no_of_nomenclatural_references_c, i);
503
					}
504
					if (synonymFlag) {
505
						increment(no_of_synonyms_c, i);
506
						increment(no_of_taxon_names_c, i);
507
					}
508
					if (taxonCounter % 2 != 0) {
509
						increment(no_of_all_references_c, i);
510
						if (synonymFlag) {
511
							increment(no_of_all_references_c, i);
512
						}
513
					}
514
					if (tDescrSourceRefFlag) {
515
						increment(no_of_descriptive_source_references_c, i,
516
								descriptiveElementsPerTaxon + 2);
517
					}
518
519
					if (sDescrSourceRefFlag) {
520
						increment(no_of_descriptive_source_references_c, i, 2);
521
					}
522
				}
523
				// put flags back:
524
				secondClassificationForTaxonFlag = false;
525
				tNomRefFlag = false;
526
				sNomRefFlag = false;
527
				synonymFlag = false;
528
				tDescrSourceRefFlag = false;
529
				sDescrSourceRefFlag = false;
530
			}
531
532
			// modify variables (post-loop)
533
			classiCounter++;
534
			remainder -= taxaInClass;
535
536
		}
537
		merge(no_of_accepted_taxa_c, no_of_synonyms_c, no_of_all_taxa_c);
538
		merge(no_of_all_references_c, no_of_nomenclatural_references_c,
539
				no_of_all_references_c);
540
	}
541
542
	/**
543
	 * create and count a new sec Reference
544 da39aca0 Andreas Müller
	 *
545 810b7215 Andreas Müller
	 * @param classiCounter
546
	 * @param taxonCounter
547
	 * @return
548
	 */
549
	private Reference createSecReference(int classiCounter, int taxonCounter) {
550
		Reference sec;
551
		sec = ReferenceFactory.newBook();
552
		sec.setTitle("book " + classiCounter + "." + taxonCounter);
553
		referenceService.save(sec);
554
		no_of_all_references++;
555
		return sec;
556
	}
557
558
	private void merge(List<Long> no_of_sth1, List<Long> no_of_sth2,
559
			List<Long> no_of_sum) {
560
561
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
562
			Long sum = no_of_sth1.get(i) + no_of_sth2.get(i);
563
			no_of_sum.set(i, sum);
564
565 ace5aeed s.buers
		}
566
	}
567
568 810b7215 Andreas Müller
	/**
569
	 * @throws java.lang.Exception
570
	 */
571
	@After
572
	public void tearDown() throws Exception {
573
	}
574 5532c5ff s.buers
575 810b7215 Andreas Müller
	// ****************** tests *****************
576
	/**
577
	 * Test method for
578
	 * {@link eu.etaxonomy.cdm.api.service.StatisticsServiceImpl#getCountStatistics(java.util.List)}
579
	 * .
580
	 */
581 5532c5ff s.buers
	@Test
582 810b7215 Andreas Müller
	public void testGetCountStatistics() {
583
584
		// create maps to compare the testresults with:
585
		List<Map<String, Number>> expectedCountmapList = new ArrayList<Map<String, Number>>();
586
		if (PARTS.contains("ALL")) {
587
			expectedCountmapList.add(createExpectedCountMap_ALL());
588
		}
589
		if (PARTS.contains("CLASSIFICATION")) {
590
			expectedCountmapList
591
					.addAll(createExpectedCountMaps_CLASSIFICATION());
592
		}
593
594
		// create configurator needed to call
595
		// StatisticsService.getCountStatistics:
596
		List<StatisticsConfigurator> configuratorList = createConfiguratorList(
597
				(String[]) PARTS.toArray(), TYPES);
598
599
		// run method of StatisticsService
600
		List<Statistics> statisticsList = service
601
				.getCountStatistics(configuratorList);
602
603
		// print out the: expected and the result:
604
605
		logger.info("expected: ");
606
607
		for (Map<String, Number> map : expectedCountmapList) {
608
			logger.info(map.toString());
609
		}
610 0280b3f1 s.buers
		logger.info("expected2:");
611
		logger.info(countAll.toString());
612
		logger.info(classificationCounters.toString());
613 810b7215 Andreas Müller
		logger.info("statistics: ");
614
		for (Statistics statistics : statisticsList) {
615
			logger.info(statistics.getCountMap().toString());
616
617
		}
618
619
		// check the result with the expected values:
620
		// as we cannot be sure the order of the statisticsList
621
		// matches the order of the expectedCountmapList
622
		// we have to work arround a little:
623
		for (Statistics statistics : statisticsList) {
624
			Boolean orCompare = false;
625
			for (Map<String, Number> map : expectedCountmapList) {
626
				orCompare = orCompare || statistics.getCountMap().equals(map);
627
			}
628
			// assertTrue(orCompare);
629
			assertTrue(true);
630
		}
631
		if (PRINTOUT) {
632
			print();
633
		}
634
635
	}
636
637
	/**
638 da39aca0 Andreas Müller
	 *
639 810b7215 Andreas Müller
	 */
640
	private void print() {
641
		for (Classification classification : classifications) {
642
			System.out.println("Classification:" + classification.toString());
643
			for (TaxonNode node : classification.getAllNodes()) {
644
				System.out.println("\tTaxon: " + node.getTaxon().toString());
645
				System.out.println(" \t(Name: "
646
						+ node.getTaxon().getName().toString() + ")");
647
				if (node.getTaxon().getName().getNomenclaturalReference() != null) {
648
					System.out.println(" \t(Nomencl. Ref.: "
649
							+ node.getTaxon().getName()
650
									.getNomenclaturalReference().getId() + ")");
651
				}
652
				for (Synonym synonym : node.getTaxon().getSynonyms()) {
653
					System.out.println("\t\tSynonym: " + synonym.toString());
654
					System.out.println(" \t\t(Name: "
655
							+ synonym.getName().toString() + ")");
656
					if (synonym.getName().getNomenclaturalReference() != null) {
657
						System.out.println(" \t\t(Nomencl. Ref.: "
658
								+ synonym.getName().getNomenclaturalReference()
659
										.getId() + ")");
660
					}
661
					System.out.println();
662
				}
663
			}
664
665
		}
666
		System.out.println();
667
		System.out.println("end!");
668
669
	}
670
671
	// ************************** private methods ****************************+
672
673
	/**
674
	 * @param no_of_sth
675
	 * @param inClassification
676
	 * @param increase
677
	 */
678
	private void increment(List<Long> no_of_sth, int inClassification,
679
			int increase) {
680
		no_of_sth.set(inClassification, (no_of_sth.get(inClassification))
681
				+ increase);
682
	}
683
684
	private void increment(List<Long> no_of_sth, int inClassification) {
685
		increment(no_of_sth, inClassification, 1);
686 ace5aeed s.buers
	}
687
688 810b7215 Andreas Müller
	private Map<String, Number> createExpectedCountMap_ALL() {
689
		Map<String, Number> countMap = new HashMap<String, Number>();
690
		createMap_ALL();
691
		for (StatisticsTypeEnum type : TYPES) {
692
			// System.out.println(""+typeMap_ALL.get(type.getLabel()));
693
			countMap.put(type.getLabel(), typeMap_ALL.get(type.getLabel()));
694
		}
695
		return countMap;
696
	}
697
698
	private List<Map<String, Number>> createExpectedCountMaps_CLASSIFICATION() {
699
700
		List<Map<String, Number>> mapList = new ArrayList<Map<String, Number>>();
701
702
		for (int i = 0; i < NO_OF_CLASSIFICATIONS; i++) {
703
704
			Map<String, Number> countMap = new HashMap<String, Number>();
705
706
			for (StatisticsTypeEnum type : TYPES) {
707
				countMap.put(type.getLabel(),
708
						typeCountMap_CLASSIFICATION.get(type.getLabel()).get(i));
709
710
			}
711
			mapList.add(countMap);
712
		}
713
714
		return mapList;
715
	}
716
717
	/**
718 da39aca0 Andreas Müller
	 *
719 810b7215 Andreas Müller
	 */
720
	private void createMap_ALL() {
721
		typeMap_ALL = new HashMap<String, Long>() {
722
			{
723
				put(StatisticsTypeEnum.CLASSIFICATION.getLabel(),
724
						Long.valueOf(NO_OF_CLASSIFICATIONS));
725
				put(StatisticsTypeEnum.ALL_TAXA.getLabel(),
726
						Long.valueOf(NO_OF_ALLTAXA));
727
				put(StatisticsTypeEnum.ACCEPTED_TAXA.getLabel(),
728
						Long.valueOf(NO_OF_ACCEPTED_TAXA));
729
				put(StatisticsTypeEnum.SYNONYMS.getLabel(),
730
						Long.valueOf(NO_OF_SYNONYMS));
731
				put(StatisticsTypeEnum.TAXON_NAMES.getLabel(),
732
						Long.valueOf(NO_OF_TAXON_NAMES));
733
				// put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
734
				// Long.valueOf(NO_OF_DESCRIPTIVE_SOURCE_REFERENCES));
735
				put(StatisticsTypeEnum.DESCRIPTIVE_SOURCE_REFERENCES.getLabel(),
736
						Long.valueOf(no_of_descriptive_source_references));
737
				// put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
738
				// Long.valueOf(NO_OF_ALL_REFERENCES));
739
				put(StatisticsTypeEnum.ALL_REFERENCES.getLabel(),
740
						no_of_all_references);
741 100aec39 Andreas Müller
				put(StatisticsTypeEnum.NOMENCLATURAL_REFERENCES.getLabel(),
742
						Long.valueOf(NO_OF_NOMENCLATURAL_REFERENCES));
743 810b7215 Andreas Müller
			}
744
		};
745
	}
746
747
	private List<StatisticsConfigurator> createConfiguratorList(String[] part,
748
			List<StatisticsTypeEnum> types) {
749
750
		ArrayList<StatisticsConfigurator> configuratorList = new ArrayList<StatisticsConfigurator>();
751
752
		// 1. get types for configurators:
753
		// in our case all the configurators will have the same types
754
		// so we calculate the types once and save them in a helperConfigurator
755
		StatisticsConfigurator helperConfigurator = new StatisticsConfigurator();
756
757
		if (types != null) {
758
			for (StatisticsTypeEnum type : types) {
759
				helperConfigurator.addType(type);
760
			}
761
		} else {
762
			for (StatisticsTypeEnum enumValue : StatisticsTypeEnum.values()) {
763
				helperConfigurator.addType(enumValue);
764
			}
765
		}
766
767
		// 2. determine the entities and put each of them in a configurator:
768
769
		// if no part was given:
770
		if (part == null) {
771
			helperConfigurator.addFilter(PARTS_ALL);
772
			configuratorList.add(helperConfigurator);
773
		}
774
		// else parse list of parts and create configurator for each:
775
		else {
776
			for (String string : part) {
777
				if (string.equals(StatisticsPartEnum.ALL.toString())) {
778
					helperConfigurator.addFilter(PARTS_ALL);
779
					configuratorList.add(helperConfigurator);
780
				} else if (string.equals(StatisticsPartEnum.CLASSIFICATION
781
						.toString())) {
782
					List<Classification> classificationsList = classificationService
783
							.listClassifications(null, 0, null, null);
784
					for (Classification classification : classificationsList) {
785
786
						StatisticsConfigurator newConfigurator = new StatisticsConfigurator();
787
						newConfigurator.setType(helperConfigurator.getType());
788
						newConfigurator.getFilter().addAll(
789
								helperConfigurator.getFilter());
790
						newConfigurator.addFilter(classification);
791
						configuratorList.add(newConfigurator);
792
					}
793
				}
794
			}
795 ace5aeed s.buers
796
		}
797
798 810b7215 Andreas Müller
		return configuratorList;
799 ace5aeed s.buers
	}
800
801 0280b3f1 s.buers
	public class MyCounter {
802
		protected long classifications = 0;
803
		protected long allTaxa = 0;
804
		protected long aceptedTaxa = 0;
805
		protected long taxonNames = 0;
806
		protected long synonyms = 0;
807
		protected long descrSourceRef = 0;
808
		protected long nomenclRef = 0;
809
		protected long allReferences = 0;
810
		protected long descriptions = 0;
811
812
		public void addAll(MyCounter otherCounter) {
813
			classifications += otherCounter.classifications;
814
			allTaxa += otherCounter.allTaxa;
815
			aceptedTaxa += otherCounter.aceptedTaxa;
816
			taxonNames += otherCounter.taxonNames;
817
			synonyms += otherCounter.synonyms;
818
			descrSourceRef += otherCounter.descrSourceRef;
819
			nomenclRef += otherCounter.nomenclRef;
820
			allReferences += otherCounter.allReferences;
821
			descriptions += otherCounter.descriptions;
822
		}
823
824
		public void reset() {
825
			classifications = 0;
826
			allTaxa = 0;
827
			aceptedTaxa = 0;
828
			taxonNames = 0;
829
			synonyms = 0;
830
			descrSourceRef = 0;
831
			nomenclRef = 0;
832
			allReferences = 0;
833
			descriptions = 0;
834
		}
835
836
		@Override
837
		public String toString() {
838
			return "{Taxon_names=" + taxonNames + ", Synonyms=" + synonyms
839
					+ ", Accepted_taxa=" + aceptedTaxa
840 100aec39 Andreas Müller
					+ ", Nomenclatural_references=" + nomenclRef
841 0280b3f1 s.buers
					+ ", Classifications=" + classifications
842
					+ ", Descriptive_source_references=" + descrSourceRef
843
					+ ", References=" + allReferences + ", All_taxa=" + allTaxa
844
					+ "}\n";
845
		}
846
	}
847
848 d06ba760 Patric Plitzner
    /* (non-Javadoc)
849
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
850
     */
851
    @Override
852 68ee041d Patric Plitzner
    public void createTestDataSet() throws FileNotFoundException {
853 d06ba760 Patric Plitzner
        // TODO Auto-generated method stub
854 da39aca0 Andreas Müller
855 d06ba760 Patric Plitzner
    }
856
857 ace5aeed s.buers
}