1
|
/**
|
2
|
* Copyright (C) 2009 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
|
|
10
|
package eu.etaxonomy.cdm.persistence.query;
|
11
|
|
12
|
|
13
|
import org.hibernate.Criteria;
|
14
|
import org.hibernate.Hibernate;
|
15
|
import org.hibernate.criterion.CriteriaQuery;
|
16
|
import org.hibernate.criterion.Order;
|
17
|
import org.hibernate.criterion.ProjectionList;
|
18
|
import org.hibernate.criterion.Projections;
|
19
|
import org.hibernate.type.Type;
|
20
|
|
21
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
22
|
|
23
|
public class GroupByDate extends Grouping {
|
24
|
|
25
|
private Resolution resolution;
|
26
|
|
27
|
public GroupByDate(String propertyPath,String name, SortOrder order,Resolution resolution) {
|
28
|
super(propertyPath, name, null, order);
|
29
|
this.resolution = resolution;
|
30
|
}
|
31
|
|
32
|
@Override
|
33
|
public void addProjection(ProjectionList projectionList) {
|
34
|
if(resolution.equals(Resolution.YEAR)) {
|
35
|
StringBuffer selectSqlString = getYearSelect();
|
36
|
StringBuffer projectSqlString = getYearProjection();
|
37
|
projectionList.add(Projections.sqlGroupProjection(selectSqlString.toString(), projectSqlString.toString(), new String[] {"year"}, new Type[] { Hibernate.INTEGER }),name);
|
38
|
} else if(resolution.equals(Resolution.MONTH)) {
|
39
|
StringBuffer selectSqlString = getYearMonthSelect();
|
40
|
StringBuffer projectSqlString = getYearMonthProjection();
|
41
|
projectionList.add(Projections.sqlGroupProjection(selectSqlString.toString(), projectSqlString.toString(), new String[] {"year","month"}, new Type[] { Hibernate.INTEGER, Hibernate.INTEGER }),name);
|
42
|
} else {
|
43
|
StringBuffer selectSqlString = getYearMonthDaySelect();
|
44
|
StringBuffer projectSqlString = getYearMonthDayProjection();
|
45
|
projectionList.add(Projections.sqlGroupProjection(selectSqlString.toString(), projectSqlString.toString(), new String[] {"year","month", "day"}, new Type[] { Hibernate.INTEGER, Hibernate.INTEGER, Hibernate.INTEGER }),name);
|
46
|
}
|
47
|
}
|
48
|
|
49
|
public void addOrder(Criteria criteria) {
|
50
|
if(getOrder() != null) {
|
51
|
if(getOrder().equals(SortOrder.ASCENDING)) {
|
52
|
if(resolution.equals(Resolution.YEAR)) {
|
53
|
criteria.addOrder(asc(this.getPropertyName(),"year"));
|
54
|
} else if(resolution.equals(Resolution.MONTH)) {
|
55
|
criteria.addOrder(asc(this.getPropertyName(),"year"));
|
56
|
criteria.addOrder(asc(this.getPropertyName(),"month"));
|
57
|
} else {
|
58
|
criteria.addOrder(asc(this.getPropertyName(),"year"));
|
59
|
criteria.addOrder(asc(this.getPropertyName(),"month"));
|
60
|
criteria.addOrder(asc(this.getPropertyName(),"day"));
|
61
|
}
|
62
|
} else {
|
63
|
if(resolution.equals(Resolution.YEAR)) {
|
64
|
criteria.addOrder(desc(this.getPropertyName(),"year"));
|
65
|
} else if(resolution.equals(Resolution.MONTH)) {
|
66
|
criteria.addOrder(desc(this.getPropertyName(),"year"));
|
67
|
criteria.addOrder(desc(this.getPropertyName(),"month"));
|
68
|
} else {
|
69
|
criteria.addOrder(desc(this.getPropertyName(),"year"));
|
70
|
criteria.addOrder(desc(this.getPropertyName(),"month"));
|
71
|
criteria.addOrder(desc(this.getPropertyName(),"month"));
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
|
77
|
//"year({alias}.property) as year"
|
78
|
private StringBuffer getYearSelect() {
|
79
|
StringBuffer stringBuffer = new StringBuffer();
|
80
|
stringBuffer.append("year({alias}.");
|
81
|
stringBuffer.append(getPropertyName());
|
82
|
stringBuffer.append(") as year");
|
83
|
return stringBuffer;
|
84
|
}
|
85
|
|
86
|
private StringBuffer getYearMonthSelect() {
|
87
|
StringBuffer stringBuffer = getYearSelect();
|
88
|
stringBuffer.append(", month({alias}.");
|
89
|
stringBuffer.append(getPropertyName());
|
90
|
stringBuffer.append(") as month");
|
91
|
return stringBuffer;
|
92
|
}
|
93
|
|
94
|
private StringBuffer getYearMonthDaySelect() {
|
95
|
StringBuffer stringBuffer = getYearProjection();
|
96
|
stringBuffer.append(", day(");
|
97
|
if(getAssociatedObj() != null) {
|
98
|
stringBuffer.append(getAssociatedObjectAlias());
|
99
|
stringBuffer.append(".");
|
100
|
}
|
101
|
stringBuffer.append(getPropertyName());
|
102
|
stringBuffer.append(") as day");
|
103
|
return stringBuffer;
|
104
|
}
|
105
|
|
106
|
//"year({alias}.property) as year"
|
107
|
private StringBuffer getYearProjection() {
|
108
|
StringBuffer stringBuffer = new StringBuffer();
|
109
|
stringBuffer.append("year({alias}.");
|
110
|
stringBuffer.append(getPropertyName());
|
111
|
stringBuffer.append(")");
|
112
|
return stringBuffer;
|
113
|
}
|
114
|
|
115
|
private StringBuffer getYearMonthProjection() {
|
116
|
StringBuffer stringBuffer = getYearProjection();
|
117
|
stringBuffer.append(", month({alias}.");
|
118
|
stringBuffer.append(getPropertyName());
|
119
|
stringBuffer.append(")");
|
120
|
return stringBuffer;
|
121
|
}
|
122
|
|
123
|
private StringBuffer getYearMonthDayProjection() {
|
124
|
StringBuffer stringBuffer = getYearProjection();
|
125
|
stringBuffer.append(", day(");
|
126
|
if(getAssociatedObj() != null) {
|
127
|
stringBuffer.append(getAssociatedObjectAlias());
|
128
|
stringBuffer.append(".");
|
129
|
}
|
130
|
stringBuffer.append(getPropertyName());
|
131
|
stringBuffer.append(")");
|
132
|
return stringBuffer;
|
133
|
}
|
134
|
|
135
|
public enum Resolution {
|
136
|
DAY,
|
137
|
MONTH,
|
138
|
YEAR;
|
139
|
}
|
140
|
|
141
|
public Order asc(String propertyName, String function) {
|
142
|
return new GroupByDateOrder(propertyName,function, true);
|
143
|
}
|
144
|
|
145
|
public Order desc(String propertyName, String function) {
|
146
|
return new GroupByDateOrder(propertyName,function, false);
|
147
|
}
|
148
|
|
149
|
public class GroupByDateOrder extends Order {
|
150
|
String function;
|
151
|
String propertyName;
|
152
|
boolean ascending;
|
153
|
|
154
|
protected GroupByDateOrder(String propertyName, String function, boolean ascending) {
|
155
|
super(propertyName,ascending);
|
156
|
this.propertyName = propertyName;
|
157
|
this.ascending = ascending;
|
158
|
this.function = function;
|
159
|
}
|
160
|
|
161
|
@Override
|
162
|
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) {
|
163
|
StringBuffer stringBuffer = new StringBuffer();
|
164
|
stringBuffer.append(function);
|
165
|
stringBuffer.append("(this_.");
|
166
|
stringBuffer.append(propertyName);
|
167
|
stringBuffer.append(")");
|
168
|
|
169
|
if(ascending) {
|
170
|
stringBuffer.append(" asc");
|
171
|
} else {
|
172
|
stringBuffer.append(" desc");
|
173
|
}
|
174
|
|
175
|
return stringBuffer.toString();
|
176
|
}
|
177
|
|
178
|
|
179
|
|
180
|
}
|
181
|
}
|