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