ecc84525e085f1a9ac481b9ef24586e455cf1ab6
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / StatisticsServiceImpl.java
1 /**
2 * Copyright (C) 2007 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 java.util.ArrayList;
12 import java.util.List;
13
14 import org.apache.logging.log4j.LogManager;
15 import org.apache.logging.log4j.Logger;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Service;
18 import org.springframework.transaction.annotation.Transactional;
19
20 import eu.etaxonomy.cdm.api.service.statistics.Statistics;
21 import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
22 import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
23 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24 import eu.etaxonomy.cdm.model.description.DescriptionBase;
25 import eu.etaxonomy.cdm.model.name.TaxonName;
26 import eu.etaxonomy.cdm.model.taxon.Classification;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
31 import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
32 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
33 import eu.etaxonomy.cdm.persistence.dao.statistics.IStatisticsDao;
34 import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
35 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
36
37 /**
38 * @author s.buers Service to provide statistic data of the database elements
39 */
40 @Service
41 @Transactional
42 public class StatisticsServiceImpl implements IStatisticsService {
43
44 private static final Logger logger = LogManager.getLogger(StatisticsServiceImpl.class);
45
46 // this does not make sense, we just count nothing if no type is given.
47 // the one who calls this service should check that there are types given!
48 // private static final StatisticsTypeEnum DEFAULT_TYPE=StatisticsTypeEnum.ALL_TAXA;
49 //TODO create a list with all types.
50
51 // this constant can also be used by the ones that use the service
52
53 private static final IdentifiableEntity<?> ALL_DB = null;
54
55 @Override
56 @Transactional
57 public IdentifiableEntity<?> getFilterALL_DB(){
58 return ALL_DB;
59 }
60
61 private ArrayList<Statistics> statisticsList;
62
63 @Autowired
64 private ITaxonDao taxonDao;
65
66 @Autowired
67 private ITaxonNameDao taxonNameDao;
68
69 @Autowired
70 private IClassificationDao classificationDao;
71
72 @Autowired
73 private IReferenceDao referenceDao;
74
75 @Autowired
76 private IStatisticsDao statisticsDao;
77
78 @Autowired
79 private IDescriptionDao descriptionDao;
80
81 /**
82 * counts all the elements referenced in the configurator from the part of
83 * the database referenced in the configurator
84 *
85 * @param configurators
86 * @return be aware that a Statistics.countMap might contain "null"
87 * {@link Number} values, if the count failed (, if the value is "0"
88 * the count succeeded in counting zero elements.)
89 */
90 @Override
91 @Transactional
92 public List<Statistics> getCountStatistics(
93 List<StatisticsConfigurator> configurators) {
94
95 statisticsList = new ArrayList<>();
96
97 for (StatisticsConfigurator statisticsConfigurator : configurators) {
98 // create a Statistics element for each configurator
99 countStatisticsPart(statisticsConfigurator);
100 }
101 return this.statisticsList;
102 }
103
104 @Transactional
105 private void countStatisticsPart(StatisticsConfigurator configurator) {
106 // get last element of configurator.filter (the node that is the root
107 // for the count):
108 IdentifiableEntity<?> filter = configurator.getFilter().get(
109 (configurator.getFilter().size()) - 1);
110 if (filter == getFilterALL_DB()) {
111 countAll(configurator);
112 } else { // check for classtype classification
113 countPart(configurator, filter);
114 }
115
116 }
117
118 private void countAll(StatisticsConfigurator configurator) {
119 Statistics statistics = new Statistics(configurator);
120
121 for (StatisticsTypeEnum type : configurator.getType()) {
122 Long counter = null;
123 switch (type) {
124
125 case ALL_TAXA:
126 counter = taxonDao.count(TaxonBase.class);
127 break;
128 case SYNONYMS:
129 counter = taxonDao.count(Synonym.class);
130 break;
131 case ACCEPTED_TAXA:
132 counter = taxonDao.count(Taxon.class);
133 break;
134 case ALL_REFERENCES:
135 counter = referenceDao.count(eu.etaxonomy.cdm.model.reference.Reference.class);
136 counter -=statisticsDao.countNomenclaturalReferences();
137 break;
138
139 case NOMENCLATURAL_REFERENCES:
140
141 counter = statisticsDao.countNomenclaturalReferences();
142 break;
143
144 case CLASSIFICATION:
145 counter = classificationDao.count(Classification.class);
146
147 break;
148
149 case TAXON_NAMES:
150 counter = taxonNameDao.count(TaxonName.class);
151 break;
152
153 case DESCRIPTIVE_SOURCE_REFERENCES:
154
155 counter = statisticsDao.countDescriptiveSourceReferences();
156
157 break;
158 case DESCRIPTIONS:
159
160 counter = descriptionDao.count(DescriptionBase.class);
161
162 break;
163 }
164
165 statistics.addCount(type, counter);
166 }
167 statisticsList.add(statistics);
168 }
169
170 @Transactional
171 private void countPart(StatisticsConfigurator configurator,
172 IdentifiableEntity filter) {
173 // TODO maybe remove redundant parameter filter
174 Statistics statistics = new Statistics(configurator);
175
176 Long counter = null;
177
178 if (filter instanceof Classification) {
179
180 for (StatisticsTypeEnum type : configurator.getType()) {
181
182 switch (type) {
183 case CLASSIFICATION:
184 logger.info("there should not be any classification "
185 + "nested in an other classification");
186 // so we set counter to 1, as a classification itself is one classification
187 counter = new Long(1);
188 break;
189 case ACCEPTED_TAXA:
190 counter = statisticsDao.countTaxaInClassification(
191 Taxon.class, (Classification) filter);
192 break;
193
194 case ALL_TAXA:
195 counter = statisticsDao.countTaxaInClassification(
196 TaxonBase.class, (Classification) filter);
197 break;
198 case SYNONYMS:
199 counter = statisticsDao.countTaxaInClassification(
200 Synonym.class, (Classification) filter);
201 break;
202 case TAXON_NAMES:
203 counter = statisticsDao
204 .countTaxonNames((Classification) filter);
205 break;
206 case ALL_REFERENCES:
207 // counter = statisticsDao.countReferencesInClassification((Classification) filter);
208 counter = statisticsDao.countReferencesInClassificationWithUuids((Classification) filter);
209 counter+=statisticsDao
210 .countDescriptive(true, (Classification) filter);
211 break;
212 case DESCRIPTIVE_SOURCE_REFERENCES:
213 counter = statisticsDao
214 .countDescriptive(true, (Classification) filter);
215 break;
216 case DESCRIPTIONS:
217 counter = statisticsDao
218 .countDescriptive(false, (Classification) filter);
219 break;
220 case NOMENCLATURAL_REFERENCES:
221 counter = statisticsDao
222 .countNomenclaturalReferences((Classification) filter);
223 break;
224
225 }
226
227 statistics.addCount(type, counter);
228 }
229 } else if(filter instanceof Taxon) {
230 //TODO get all taxa of the tree:
231 do{
232 filter.getUuid();
233 statisticsDao.getTaxonTree(filter);
234 }while(true);
235 }else {
236 // we just return null as count for the statistics
237 // element, if the filter is neither classification nor null.
238 }
239
240 statisticsList.add(statistics);
241 }
242 }