1
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.statistics;
|
2
|
|
3
|
import org.hibernate.Criteria;
|
4
|
import org.hibernate.Query;
|
5
|
import org.hibernate.criterion.Projections;
|
6
|
import org.hibernate.criterion.Restrictions;
|
7
|
import org.springframework.stereotype.Repository;
|
8
|
|
9
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
10
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
11
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
12
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
13
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
14
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
|
15
|
import eu.etaxonomy.cdm.persistence.dao.statistics.IStatisticsDao;
|
16
|
|
17
|
@Repository
|
18
|
public class StatisticsDaoHibernateImpl extends DaoBase implements
|
19
|
IStatisticsDao {
|
20
|
|
21
|
/**
|
22
|
* @return be aware that the returned long might be null
|
23
|
*/
|
24
|
@Override
|
25
|
public Long countNomenclaturalReferences() {
|
26
|
return countNomenclaturalReferences("");
|
27
|
}
|
28
|
|
29
|
private Long countNomenclaturalReferences(String where) {
|
30
|
|
31
|
Query query = getSession().createQuery(
|
32
|
"select count(distinct nomenclaturalReference) from TaxonNameBase"
|
33
|
+ where);
|
34
|
return (Long) query.uniqueResult();
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
public Long countNomenclaturalReferences(Class clazz) {
|
39
|
return countNomenclaturalReferences("where" + clazz.getSimpleName());
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* @return be aware that the returned long might be null
|
44
|
*/
|
45
|
|
46
|
public Long countDescriptiveSourceReferences() {
|
47
|
Query query;
|
48
|
Long count = new Long(0);
|
49
|
|
50
|
// count sources from Descriptions:
|
51
|
query = getSession().createQuery(
|
52
|
"select count(distinct d.descriptionSources) from DescriptionBase as d "
|
53
|
+ "join d.descriptionSources");
|
54
|
|
55
|
query = getSession()
|
56
|
.createQuery(
|
57
|
"select count(distinct s.citation) from DescriptionElementBase as d join d.sources as s");
|
58
|
return (Long) query.uniqueResult();
|
59
|
}
|
60
|
|
61
|
// TODO remove this method:
|
62
|
@Override
|
63
|
public void tryArround() {
|
64
|
Criteria criteria = getSession().createCriteria(Synonym.class);
|
65
|
// Query query =
|
66
|
// System.out.println("query: "+((Query) criteria).getQueryString());
|
67
|
// System.out.println(getSession().createCriteria(Synonym.class);
|
68
|
criteria.list();
|
69
|
// criteria.
|
70
|
// System.out.println(criteria.toString());
|
71
|
System.out.println("");
|
72
|
|
73
|
}
|
74
|
|
75
|
@Override
|
76
|
public Long countTaxaInClassification(Class<? extends TaxonBase> clazz,
|
77
|
Classification classification) {
|
78
|
|
79
|
if (clazz.equals(TaxonBase.class)) {
|
80
|
|
81
|
return countTaxaInClassification(Taxon.class, classification)
|
82
|
+ countTaxaInClassification(Synonym.class, classification);
|
83
|
}
|
84
|
|
85
|
if (clazz.equals(Taxon.class)) {
|
86
|
Criteria criteria = getSession().createCriteria(TaxonNode.class);
|
87
|
criteria = getSession().createCriteria(TaxonNode.class);
|
88
|
criteria.add(Restrictions.eq("classification", classification));
|
89
|
criteria.setProjection(Projections.rowCount());
|
90
|
return Long.valueOf((Integer) criteria.uniqueResult());
|
91
|
// Long counter = Long.valueOf((Integer) criteria.uniqueResult());
|
92
|
// return counter;
|
93
|
}
|
94
|
|
95
|
else if (clazz.equals(Synonym.class)) {
|
96
|
// criteria= getSession().createCriteria(TaxonNode.class);
|
97
|
|
98
|
Query query = getSession().createQuery(
|
99
|
"select count(distinct sr.relatedFrom.uuid) from TaxonNode tn "
|
100
|
+ "join tn.taxon.synonymRelations as sr "
|
101
|
+ "where tn.classification=:classification");
|
102
|
query.setParameter("classification", classification);
|
103
|
Long counter = (Long) query.uniqueResult();
|
104
|
return counter;
|
105
|
// return (Long) query.uniqueResult();
|
106
|
}
|
107
|
// this should never happen:
|
108
|
return null;
|
109
|
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public Long countTaxonNames(Classification classification) {
|
114
|
// TODO: merge the 2 queries below - you can't just add them because
|
115
|
// there might be doubles
|
116
|
Query query;
|
117
|
// this is only the taxon names:
|
118
|
// query = getSession().createQuery(
|
119
|
// "select count(distinct tn.taxon.name) from TaxonNode tn "
|
120
|
// + "where tn.classification=:classification");
|
121
|
|
122
|
// this is only the synonym names:
|
123
|
query = getSession().createQuery(
|
124
|
"select count(distinct s.name) from TaxonNode tn "
|
125
|
+ "join tn.taxon.synonymRelations sr "
|
126
|
+ "join sr.relatedFrom s "
|
127
|
+ "where tn.classification=:classification");
|
128
|
|
129
|
// everything???:
|
130
|
query = getSession()
|
131
|
.createQuery(
|
132
|
"select count(distinct s2) from TaxonNode tn " +
|
133
|
"join tn.taxon.synonymRelations sr join sr.relatedFrom.name s1 " +
|
134
|
"join tn.taxon.name s2 " +
|
135
|
"where tn.classification=:classification");
|
136
|
|
137
|
query.setParameter("classification", classification);
|
138
|
return (Long) query.uniqueResult();
|
139
|
}
|
140
|
}
|