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
|
import java.util.Map;
|
13
|
|
14
|
import org.hibernate.Criteria;
|
15
|
import org.hibernate.criterion.Order;
|
16
|
import org.hibernate.envers.query.AuditEntity;
|
17
|
import org.hibernate.envers.query.AuditQuery;
|
18
|
|
19
|
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
|
20
|
|
21
|
public class OrderHint {
|
22
|
|
23
|
public enum SortOrder {
|
24
|
|
25
|
/**
|
26
|
* items are sorted in increasing
|
27
|
* order.
|
28
|
*/
|
29
|
ASCENDING("asc"),
|
30
|
/**
|
31
|
* items are sorted in decreasing
|
32
|
* order.
|
33
|
*/
|
34
|
DESCENDING("desc");
|
35
|
|
36
|
private String hql;
|
37
|
|
38
|
private SortOrder(String hqlStr){
|
39
|
hql = hqlStr;
|
40
|
}
|
41
|
|
42
|
public String toHql(){
|
43
|
return hql;
|
44
|
}
|
45
|
}
|
46
|
|
47
|
private String propertyName;
|
48
|
|
49
|
private SortOrder sortOrder;
|
50
|
|
51
|
public OrderHint(String fieldName, SortOrder sortOrder) {
|
52
|
super();
|
53
|
this.propertyName = fieldName;
|
54
|
this.sortOrder = sortOrder;
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* The property of a bean
|
59
|
* @return
|
60
|
*/
|
61
|
public String getPropertyName() {
|
62
|
return propertyName;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* possible sort orders are {@link SortOrder.ASCENDING} or {@link SortOrder.DESCENDING}
|
67
|
* @return
|
68
|
*/
|
69
|
public SortOrder getSortOrder() {
|
70
|
return sortOrder;
|
71
|
}
|
72
|
|
73
|
public boolean isAscending(){
|
74
|
return sortOrder.equals(SortOrder.ASCENDING);
|
75
|
}
|
76
|
|
77
|
public void add(Criteria criteria, Map<String, Criteria> criteriaMap) {
|
78
|
if(getPropertyName().indexOf(".") != -1) {
|
79
|
/**
|
80
|
* Here we have to work a bit of magic as currently hibernate will
|
81
|
* throw an error if we attempt to join the same association twice.
|
82
|
*
|
83
|
* http://opensource.atlassian.com/projects/hibernate/browse/HHH-879
|
84
|
*/
|
85
|
Order order;
|
86
|
|
87
|
String[] assocObjs = getPropertyName().split("\\.");
|
88
|
String path = "";
|
89
|
Criteria c = criteria;
|
90
|
for(int i = 0; i < assocObjs.length - 1; i++) {
|
91
|
path = path + assocObjs[i];
|
92
|
if(criteriaMap.get(path) == null) {
|
93
|
c = c.createCriteria(assocObjs[i]);
|
94
|
criteriaMap.put(path, c);
|
95
|
} else {
|
96
|
c = criteriaMap.get(path);
|
97
|
}
|
98
|
path = path + '.';
|
99
|
}
|
100
|
String propname = assocObjs[assocObjs.length - 1];
|
101
|
if(isAscending()){
|
102
|
c.addOrder(Order.asc(propname));
|
103
|
} else {
|
104
|
c.addOrder(Order.desc(propname));
|
105
|
}
|
106
|
} else {
|
107
|
if(isAscending()){
|
108
|
criteria.addOrder(Order.asc(getPropertyName()));
|
109
|
} else {
|
110
|
criteria.addOrder(Order.desc(getPropertyName()));
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
|
115
|
public void add(AuditQuery query) {
|
116
|
|
117
|
if(getPropertyName().indexOf('.', 0) >= 0){
|
118
|
throw new OperationNotSupportedInPriorViewException("Sorting by related properties is not supported in the history view");
|
119
|
} else {
|
120
|
if(isAscending()){
|
121
|
query.addOrder(AuditEntity.property(getPropertyName()).asc());
|
122
|
} else {
|
123
|
query.addOrder(AuditEntity.property(getPropertyName()).desc());
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public boolean equals(Object obj) {
|
130
|
if (obj == this){
|
131
|
return true;
|
132
|
}
|
133
|
if (obj == null){
|
134
|
return false;
|
135
|
}
|
136
|
if (!OrderHint.class.isAssignableFrom(obj.getClass())){
|
137
|
return false;
|
138
|
}
|
139
|
OrderHint orderHint= (OrderHint)obj;
|
140
|
boolean propertyNameEqual = orderHint.getPropertyName().equals(this.getPropertyName());
|
141
|
boolean sortOrderEqual = orderHint.getSortOrder().equals(this.getSortOrder());
|
142
|
if (! propertyNameEqual || !sortOrderEqual){
|
143
|
return false;
|
144
|
}
|
145
|
return true;
|
146
|
}
|
147
|
|
148
|
@Override
|
149
|
public int hashCode() {
|
150
|
int hashCode = 7;
|
151
|
hashCode = 29 * hashCode + this.getPropertyName().hashCode() * this.getSortOrder().hashCode();
|
152
|
return hashCode;
|
153
|
}
|
154
|
}
|