Project

General

Profile

Download (3.81 KB) Statistics
| Branch: | Tag: | Revision:
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.List;
13
import java.util.Map;
14

    
15
import org.hibernate.Criteria;
16
import org.hibernate.criterion.Order;
17
import org.hibernate.envers.query.AuditEntity;
18
import org.hibernate.envers.query.AuditQuery;
19
import org.hibernate.search.FullTextQuery;
20

    
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22
import eu.etaxonomy.cdm.model.common.ICdmBase;
23
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
24

    
25
public class OrderHint {
26

    
27
	public enum SortOrder {
28

    
29
		/**
30
		 * items are sorted in increasing
31
		 * order.
32
		 */
33
		ASCENDING, 
34
		/**
35
		 * items are sorted in decreasing
36
		 * order.
37
		 */
38
		DESCENDING
39
	}
40
	
41
	private String propertyName;
42
	
43
	private SortOrder sortOrder;
44

    
45
	public OrderHint(String fieldName, SortOrder sortOrder) {
46
		super();
47
		this.propertyName = fieldName;
48
		this.sortOrder = sortOrder;
49
	}
50

    
51
	/**
52
	 * The property of a bean
53
	 * @return
54
	 */
55
	public String getPropertyName() {
56
		return propertyName;
57
	}
58

    
59
	/**
60
	 * possible sort orders are {@link SortOrder.ASCENDING} or {@link SortOrder.DESCENDING} 
61
	 * @return
62
	 */
63
	public SortOrder getSortOrder() {
64
		return sortOrder;
65
	}
66
	
67
	public boolean isAscending(){
68
		return sortOrder.equals(SortOrder.ASCENDING);
69
	}
70
	
71
	public void add(Criteria criteria, Map<String, Criteria> criteriaMap) {
72
		if(getPropertyName().indexOf(".") != -1) {
73
			/**
74
			 * Here we have to work a bit of magic as currently hibernate will
75
			 * throw an error if we attempt to join the same association twice.
76
			 * 
77
			 * http://opensource.atlassian.com/projects/hibernate/browse/HHH-879
78
			 */
79
			Order order;
80
			
81
			String[] assocObjs = getPropertyName().split("\\.");
82
			String path = "";
83
			Criteria c = criteria;
84
			for(int i = 0; i < assocObjs.length - 1; i++) {
85
				path = path + assocObjs[i];
86
				if(criteriaMap.get(path) == null) {
87
					c = c.createCriteria(assocObjs[i]);
88
					criteriaMap.put(path, c);
89
				} else {
90
					c = criteriaMap.get(path);
91
				}		               
92
                path = path + '.';
93
            }
94
			String propname = assocObjs[assocObjs.length - 1];
95
			if(isAscending()){
96
				c.addOrder(Order.asc(propname));					
97
			} else {
98
				c.addOrder(Order.desc(propname));
99
			}
100
		} else {
101
			if(isAscending()){
102
				criteria.addOrder(Order.asc(getPropertyName()));					
103
			} else {
104
				criteria.addOrder(Order.desc(getPropertyName()));
105
			}
106
		}
107
	}
108
	
109
	public void add(AuditQuery query) {
110
	
111
		if(getPropertyName().indexOf('.', 0) >= 0){
112
			throw new OperationNotSupportedInPriorViewException("Sorting by related properties is not supported in the history view");
113
		} else {
114
			if(isAscending()){
115
				query.addOrder(AuditEntity.property(getPropertyName()).asc());					
116
			} else {
117
				query.addOrder(AuditEntity.property(getPropertyName()).desc());
118
			}
119
		}
120
	}
121

    
122
	@Override
123
	public boolean equals(Object obj) {
124
		if (obj == this){
125
			return true;
126
		}
127
		if (obj == null){
128
			return false;
129
		}
130
		if (!OrderHint.class.isAssignableFrom(obj.getClass())){
131
			return false;
132
		}
133
		OrderHint orderHint= (OrderHint)obj;
134
		boolean propertyNameEqual = orderHint.getPropertyName().equals(this.getPropertyName());
135
		boolean sortOrderEqual = orderHint.getSortOrder().equals(this.getSortOrder());
136
		if (! propertyNameEqual || !sortOrderEqual){
137
				return false;
138
		}
139
		return true;
140
	}
141
	
142
	@Override
143
	public int hashCode() {
144
		   int hashCode = 7;
145
		   hashCode = 29 * hashCode + this.getPropertyName().hashCode() * this.getSortOrder().hashCode();
146
		   return hashCode;
147
	}
148
}
(5-5/6)