Project

General

Profile

Download (4.27 KB) Statistics
| Branch: | Tag: | Revision:
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.apache.log4j.Logger;
24
import org.hibernate.Criteria;
25
import org.hibernate.HibernateException;
26
import org.hibernate.Session;
27
import org.hibernate.SessionFactory;
28
import org.springframework.beans.factory.annotation.Autowired;
29
import org.springframework.transaction.PlatformTransactionManager;
30

    
31
import eu.etaxonomy.cdm.persistence.query.OrderHint;
32

    
33
public abstract class DaoBase {
34

    
35
    final static Logger logger = Logger.getLogger(DaoBase.class);
36

    
37
    @Autowired
38
    private SessionFactory factory;
39

    
40
    @Autowired
41
    private PlatformTransactionManager transactionManager;
42

    
43
    public void setSessionFactory(SessionFactory sessionFactory) {
44
        this.factory = sessionFactory;
45
    }
46
    public SessionFactory getSessionFactory() {
47
        return factory;
48
    }
49
    protected Session getSession(){
50
        Session session ;
51
        try {
52

    
53
            session = factory.getCurrentSession();
54
        } catch (HibernateException e) {
55
            logger.error("Opening new session in turn of a HibernateException", e);
56
            session = factory.openSession();
57
        }
58
        return session;
59
    }
60

    
61
    public void flush(){
62
        getSession().flush();
63
    }
64

    
65
    private class OrderHintComparator implements Comparator<OrderHint> {
66

    
67
        @Override
68
        public int compare(OrderHint o1, OrderHint o2) {
69
            return o1.getPropertyName().compareTo(o2.getPropertyName());
70
        }
71

    
72
    }
73

    
74
    protected void addOrder(Criteria criteria, List<OrderHint> orderHints) {
75

    
76
        if(orderHints != null){
77
            Collections.sort(orderHints, new OrderHintComparator());
78

    
79
            Map<String,Criteria> criteriaMap = new HashMap<>();
80
            for(OrderHint orderHint : orderHints){
81
                orderHint.add(criteria, criteriaMap);
82
            }
83
        }
84
    }
85

    
86
    /**
87
     * Null save method which compiles a order by clause from the given list of OrderHints
88
     *
89
     * @param orderHints can be NULL
90
     * @return a StringBuffer holding the hql orderby clause
91
     */
92
    protected StringBuffer orderByClause(List<OrderHint> orderHints, String aliasName) {
93

    
94
        StringBuffer orderString = new StringBuffer();
95

    
96
        StringBuffer aliasPrefix = new StringBuffer();
97
        aliasPrefix.append(" ");
98
        if(aliasName != null && !aliasName.isEmpty()){
99
            aliasPrefix.append(aliasName).append(".");
100
        }
101

    
102
        if(orderHints != null && !orderHints.isEmpty()) {
103
            orderString.append(" order by");
104
            for(OrderHint orderHint : orderHints) {
105
                orderString.append(aliasPrefix).append(orderHint.toHql());
106
            }
107
        }
108
        return orderString;
109
    }
110

    
111

    
112
    /**
113
     * Splits a set of e.g. query parameters into a list of sets with size <code>splitSize</code>.
114
     * Only the last set may be smaller if the collection's size is not an exact multiple of split size.
115
     * @param collection the collection to split
116
     * @param splitSize the split size
117
     * @return a list of collections
118
     */
119
    protected <T extends Object> List<Collection<T>> splitCollection(Set<T> collection, int splitSize) {
120
        if (splitSize < 1){
121
            throw new IllegalArgumentException("Split size must not be positive integer");
122
        }
123
        List<Collection<T>> result = new ArrayList<>();
124
        Iterator<T> it = collection.iterator();
125
        int i = 0;
126
        Set<T> nextCollection = new HashSet<>();
127
        while (it.hasNext()){
128
            nextCollection.add(it.next());
129
            i++;
130
            if (i == splitSize){
131
                result.add(nextCollection);
132
                nextCollection = new HashSet<>();
133
                i = 0;
134
            }
135
        }
136
        if (!nextCollection.isEmpty()){
137
            result.add(nextCollection);
138
        }
139
        return result;
140
    }
141

    
142
}
(5-5/24)