2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.query
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
16 import org
.hibernate
.Criteria
;
17 import org
.hibernate
.criterion
.Order
;
18 import org
.hibernate
.envers
.query
.AuditEntity
;
19 import org
.hibernate
.envers
.query
.AuditQuery
;
21 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
23 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.OperationNotSupportedInPriorViewException
;
25 public class OrderHint
{
27 public enum SortOrder
{
30 * items are sorted in increasing
35 * items are sorted in decreasing
42 private SortOrder(String hqlStr
){
46 public String
toHql(){
51 private final String propertyName
;
53 private final SortOrder sortOrder
;
55 public static final List
<OrderHint
> ORDER_BY_ID
= Arrays
.asList(new OrderHint
[]{new OrderHint("id", SortOrder
.ASCENDING
)});
57 public static final List
<OrderHint
> ORDER_BY_TITLE_CACHE
= Arrays
.asList(new OrderHint
[]{new OrderHint("titleCache", SortOrder
.ASCENDING
)});
61 * @return "by titleCache" for all IdentifiableEntitys otherwise "by id"
63 public static List
<OrderHint
> defaultOrderHintsFor(Class
<?
extends CdmBase
> clazz
) {
64 if (clazz
.isAssignableFrom(IdentifiableEntity
.class)) {
65 return ORDER_BY_TITLE_CACHE
;
71 public OrderHint(String fieldName
, SortOrder sortOrder
) {
73 this.propertyName
= fieldName
;
74 this.sortOrder
= sortOrder
;
78 * The property of a bean
81 public String
getPropertyName() {
86 * possible sort orders are {@link SortOrder.ASCENDING} or {@link SortOrder.DESCENDING}
89 public SortOrder
getSortOrder() {
93 public boolean isAscending(){
94 return sortOrder
.equals(SortOrder
.ASCENDING
);
103 public void add(Criteria criteria
, Map
<String
, Criteria
> criteriaMap
) {
104 if(getPropertyName().indexOf(".") != -1) {
106 * Here we have to work a bit of magic as currently hibernate will
107 * throw an error if we attempt to join the same association twice.
109 * http://opensource.atlassian.com/projects/hibernate/browse/HHH-879
113 String
[] assocObjs
= getPropertyName().split("\\.");
115 Criteria c
= criteria
;
116 for(int i
= 0; i
< assocObjs
.length
- 1; i
++) {
117 path
= path
+ assocObjs
[i
];
118 if(criteriaMap
.get(path
) == null) {
119 c
= c
.createCriteria(assocObjs
[i
]);
120 criteriaMap
.put(path
, c
);
122 c
= criteriaMap
.get(path
);
126 String propname
= assocObjs
[assocObjs
.length
- 1];
128 c
.addOrder(Order
.asc(propname
));
130 c
.addOrder(Order
.desc(propname
));
134 criteria
.addOrder(Order
.asc(getPropertyName()));
136 criteria
.addOrder(Order
.desc(getPropertyName()));
142 * FIXME document this
146 public void add(AuditQuery query
) {
148 if(getPropertyName().indexOf('.', 0) >= 0){
149 throw new OperationNotSupportedInPriorViewException("Sorting by related properties is not supported in the history view");
152 query
.addOrder(AuditEntity
.property(getPropertyName()).asc());
154 query
.addOrder(AuditEntity
.property(getPropertyName()).desc());
160 * Returns a hql order by clause element which can directly be used in hql queries.
162 * e.g.: "titleCache ASC"
164 * @return an hql order by clause element
166 public String
toHql(){
167 return propertyName
+ " " + sortOrder
.toHql();
171 public boolean equals(Object obj
) {
178 if (!OrderHint
.class.isAssignableFrom(obj
.getClass())){
181 OrderHint orderHint
= (OrderHint
)obj
;
182 boolean propertyNameEqual
= orderHint
.getPropertyName().equals(this.getPropertyName());
183 boolean sortOrderEqual
= orderHint
.getSortOrder().equals(this.getSortOrder());
184 if (! propertyNameEqual
|| !sortOrderEqual
){
191 public int hashCode() {
193 hashCode
= 29 * hashCode
+ this.getPropertyName().hashCode() * this.getSortOrder().hashCode();