merge trunk to cdm-3.3 branch
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / StatisticsServiceImpl.java
1 package eu.etaxonomy.cdm.api.service;
2
3 import java.util.Arrays;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.UUID;
8
9 import org.apache.log4j.Logger;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Service;
12 import org.springframework.transaction.annotation.Transactional;
13
14 import eu.etaxonomy.cdm.api.service.statistics.Statistics;
15 import eu.etaxonomy.cdm.api.service.statistics.StatisticsConfigurator;
16 import eu.etaxonomy.cdm.api.service.statistics.StatisticsPartEnum;
17 import eu.etaxonomy.cdm.api.service.statistics.StatisticsTypeEnum;
18 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
19 import eu.etaxonomy.cdm.model.description.DescriptionBase;
20 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
22 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
23 import eu.etaxonomy.cdm.model.description.TaxonDescription;
24 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
25 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.persistence.dao.description.IDescriptionDao;
30 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
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.taxon.IClassificationDao;
34 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
35
36 /**
37 *
38 * @author s.buers Service to provide statistic data of the database elements
39 */
40
41 @Service
42 @Transactional
43 public class StatisticsServiceImpl implements IStatisticsService {
44
45 private static final Logger logger = Logger
46 .getLogger(StatisticsServiceImpl.class);
47
48 private static final List<String> DESCRIPTION_SOURCE_REF_STRATEGIE = Arrays
49 .asList(new String[] { "sources.citation" });
50 // "descriptionSources", "citation"
51
52 private static final List<String> DESCR_ELEMENT_REF_STRATEGIE = Arrays
53 .asList(new String[] { "sources.citation", });;
54
55 private StatisticsConfigurator configurator;
56
57 private Statistics statistics;
58
59 @Autowired
60 private ITaxonDao taxonDao;
61
62 @Autowired
63 private ITaxonNameDao taxonNameDao;
64
65 @Autowired
66 private IClassificationDao classificationDao;
67
68 @Autowired
69 private IReferenceDao referenceDao;
70
71 @Autowired
72 private IDescriptionDao descriptionDao;
73
74 @Autowired
75 private IDescriptionElementDao descrElementDao;
76
77 /**
78 * counts all the elements referenced in the configurator from the part of
79 * the database referenced in the configurator
80 */
81 @Override
82 @Transactional
83 public Statistics getCountStatistics(StatisticsConfigurator configurator) {
84 this.configurator = configurator;
85 this.statistics = new Statistics(configurator);
86 // TODO use "about" parameter of Statistics element
87 calculateParts();
88 return this.statistics;
89 // return new Statistics(null);
90 }
91
92 private void calculateParts() {
93 for (StatisticsPartEnum part : configurator.getPart()) {
94 switch (part) {
95 case ALL:
96 countAll();
97 break;
98
99 case CLASSIFICATION:
100 // TODO
101 break;
102 }
103 }
104
105 }
106
107 @Transactional
108 private void countAll() {
109
110 for (StatisticsTypeEnum type : configurator.getType()) {
111 Integer number = 0;
112 switch (type) {
113
114 case ALL_TAXA:
115 number += taxonDao.count(Taxon.class);
116 case SYNONYMS:
117 number += taxonDao.count(Synonym.class);
118 break;
119 case ACCEPTED_TAXA:
120 number += taxonDao.count(Taxon.class);
121 break;
122 case ALL_REFERENCES:
123 number += referenceDao
124 .count(eu.etaxonomy.cdm.model.reference.Reference.class);
125 break;
126
127 case NOMECLATURAL_REFERENCES:
128 number += (referenceDao.getAllNomenclaturalReferences()).size();
129 break;
130
131 case CLASSIFICATION:
132 number += classificationDao.count(Classification.class);
133
134 break;
135
136 case TAXON_NAMES:
137 number += taxonNameDao.count(TaxonNameBase.class);
138 break;
139
140 // case DESCRIPTIVE_SOURCE_REFERENCES:
141 // TODO create statistics DAO
142 // number += getDescriptiveSourceReferences();
143 // break;
144 }
145 statistics.addCount(type, number);
146 }
147
148 }
149
150 private Integer getDescriptiveSourceReferences() {
151 // int counter = 0;
152
153 // count references from each description:
154 // TODO test this function or write dao and delete it
155
156 // // we need the set to get off the doubles:
157
158 /*
159 * TODO >>> better performance and more reliabale deduplication with
160 * Set<UUID> referenceUuids = new HashSet<UUID>();
161 */
162 Set<UUID> referenceUuids = new HashSet<UUID>();
163 Set<eu.etaxonomy.cdm.model.reference.Reference<?>> references = new HashSet<eu.etaxonomy.cdm.model.reference.Reference<?>>();
164 // TODO second param 0?:
165
166 /*
167 * TODO >>>> it should not be necessary to use init stratgies >>>>
168 * listDescriptions(null, null, null, null, null, null, null, null);
169 * would list all descriptions
170 */
171 List<DescriptionBase> descriptions = descriptionDao.listDescriptions(
172 TaxonDescription.class, null, null, null, null, null, null,
173 DESCRIPTION_SOURCE_REF_STRATEGIE);
174 descriptions.addAll(descriptionDao.listDescriptions(
175 TaxonNameDescription.class, null, null, null, null, null, null,
176 DESCRIPTION_SOURCE_REF_STRATEGIE));
177 descriptions.addAll(descriptionDao.listDescriptions(
178 SpecimenDescription.class, null, null, null, null, null, null,
179 DESCRIPTION_SOURCE_REF_STRATEGIE));
180 // list(null, 0);
181 for (DescriptionBase<?> description : descriptions) {
182
183 // get all sources of the description
184 Set<IdentifiableSource> sources = description.getSources();
185 for (IdentifiableSource source : sources) {
186 if (source.getCitation() != null)
187
188 references.add(source.getCitation());
189 }
190
191 /*
192 * TODO >>>> get all description elements from the description
193 *
194 * e.g: for (DescriptionElementBase element :
195 * description.getElements()) { for (DescriptionElementSource source
196 * : element.getSources()) {
197 *
198 * } }
199 */
200 }
201
202 // this part still provokes an error:
203 // count references from each description element:
204 List<DescriptionElementBase> descrElements = descrElementDao.list(null,
205 0, null, DESCR_ELEMENT_REF_STRATEGIE);
206 for (DescriptionElementBase descriptionElement : descrElements) {
207 Set<DescriptionElementSource> elementSources = descriptionElement
208 .getSources();
209 for (DescriptionElementSource source : elementSources) {
210 if (source.getCitation() != null)
211 references.add(source.getCitation());
212 }
213 }
214
215 return references.size();
216 }
217
218 }