1
|
/**
|
2
|
* Copyright (C) 2007 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.dao.hibernate.common;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collection;
|
14
|
import java.util.Collections;
|
15
|
import java.util.Comparator;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.Iterator;
|
19
|
import java.util.List;
|
20
|
import java.util.Map;
|
21
|
import java.util.Set;
|
22
|
|
23
|
import org.hibernate.Criteria;
|
24
|
import org.hibernate.HibernateException;
|
25
|
import org.hibernate.Session;
|
26
|
import org.hibernate.SessionFactory;
|
27
|
import org.springframework.beans.factory.annotation.Autowired;
|
28
|
|
29
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
30
|
|
31
|
public abstract class DaoBase {
|
32
|
|
33
|
@Autowired
|
34
|
private SessionFactory factory;
|
35
|
|
36
|
public void setSessionFactory(SessionFactory sessionFactory) {
|
37
|
this.factory = sessionFactory;
|
38
|
}
|
39
|
public SessionFactory getSessionFactory() {
|
40
|
return factory;
|
41
|
}
|
42
|
protected Session getSession(){
|
43
|
Session session ;
|
44
|
try {
|
45
|
session = factory.getCurrentSession();
|
46
|
} catch (HibernateException e) {
|
47
|
session = factory.openSession();
|
48
|
}
|
49
|
return session;
|
50
|
}
|
51
|
|
52
|
public void flush(){
|
53
|
getSession().flush();
|
54
|
}
|
55
|
|
56
|
private class OrderHintComparator implements Comparator<OrderHint> {
|
57
|
|
58
|
@Override
|
59
|
public int compare(OrderHint o1, OrderHint o2) {
|
60
|
return o1.getPropertyName().compareTo(o2.getPropertyName());
|
61
|
}
|
62
|
|
63
|
}
|
64
|
|
65
|
protected void addOrder(Criteria criteria, List<OrderHint> orderHints) {
|
66
|
|
67
|
if(orderHints != null){
|
68
|
Collections.sort(orderHints, new OrderHintComparator());
|
69
|
|
70
|
Map<String,Criteria> criteriaMap = new HashMap<>();
|
71
|
for(OrderHint orderHint : orderHints){
|
72
|
orderHint.add(criteria, criteriaMap);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Null save method which compiles a order by clause from the given list of OrderHints
|
79
|
*
|
80
|
* @param orderHints can be NULL
|
81
|
* @return a StringBuffer holding the hql orderby clause
|
82
|
*/
|
83
|
protected StringBuffer orderByClause(List<OrderHint> orderHints, String aliasName) {
|
84
|
|
85
|
StringBuffer orderString = new StringBuffer();
|
86
|
|
87
|
StringBuffer aliasPrefix = new StringBuffer();
|
88
|
aliasPrefix.append(" ");
|
89
|
if(aliasName != null && !aliasName.isEmpty()){
|
90
|
aliasPrefix.append(aliasName).append(".");
|
91
|
}
|
92
|
|
93
|
if(orderHints != null && !orderHints.isEmpty()) {
|
94
|
orderString.append(" order by");
|
95
|
for(OrderHint orderHint : orderHints) {
|
96
|
orderString.append(aliasPrefix).append(orderHint.toHql());
|
97
|
}
|
98
|
}
|
99
|
return orderString;
|
100
|
}
|
101
|
|
102
|
|
103
|
/**
|
104
|
* Splits a set of e.g. query parameters into a list of sets with size <code>splitSize</code>.
|
105
|
* Only the last set may be smaller if the collection's size is not an exact multiple of split size.
|
106
|
* @param collection the collection to split
|
107
|
* @param splitSize the split size
|
108
|
* @return a list of collections
|
109
|
*/
|
110
|
protected <T extends Object> List<Collection<T>> splitCollection(Set<T> collection, int splitSize) {
|
111
|
if (splitSize < 1){
|
112
|
throw new IllegalArgumentException("Split size must not be positive integer");
|
113
|
}
|
114
|
List<Collection<T>> result = new ArrayList<>();
|
115
|
Iterator<T> it = collection.iterator();
|
116
|
int i = 0;
|
117
|
Set<T> nextCollection = new HashSet<>();
|
118
|
while (it.hasNext()){
|
119
|
nextCollection.add(it.next());
|
120
|
i++;
|
121
|
if (i == splitSize){
|
122
|
result.add(nextCollection);
|
123
|
nextCollection = new HashSet<>();
|
124
|
i = 0;
|
125
|
}
|
126
|
}
|
127
|
if (!nextCollection.isEmpty()){
|
128
|
result.add(nextCollection);
|
129
|
}
|
130
|
return result;
|
131
|
}
|
132
|
|
133
|
}
|