Project

General

Profile

Download (2.71 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.Collections;
13
import java.util.Comparator;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

    
18
import org.hibernate.Criteria;
19
import org.hibernate.HibernateException;
20
import org.hibernate.Session;
21
import org.hibernate.SessionFactory;
22
import org.springframework.beans.factory.annotation.Autowired;
23

    
24
import eu.etaxonomy.cdm.persistence.query.OrderHint;
25

    
26
public abstract class DaoBase {
27

    
28
    @Autowired
29
    private SessionFactory factory;
30

    
31
    public void setSessionFactory(SessionFactory sessionFactory) {
32
        this.factory = sessionFactory;
33
    }
34
    public SessionFactory getSessionFactory() {
35
        return factory;
36
    }
37
    protected Session getSession(){
38
        Session session ;
39
        try {
40
            session = factory.getCurrentSession();
41
        } catch (HibernateException e) {
42
            session = factory.openSession();
43
        }
44
        return session;
45
    }
46

    
47
    public void flush(){
48
        getSession().flush();
49
    }
50

    
51
    private class OrderHintComparator implements Comparator<OrderHint> {
52

    
53
        @Override
54
        public int compare(OrderHint o1, OrderHint o2) {
55
            return o1.getPropertyName().compareTo(o2.getPropertyName());
56
        }
57

    
58
    }
59

    
60
    protected void addOrder(Criteria criteria, List<OrderHint> orderHints) {
61

    
62
        if(orderHints != null){
63
            Collections.sort(orderHints, new OrderHintComparator());
64

    
65
            Map<String,Criteria> criteriaMap = new HashMap<>();
66
            for(OrderHint orderHint : orderHints){
67
                orderHint.add(criteria, criteriaMap);
68
            }
69
        }
70
    }
71

    
72
    /**
73
     * Null save method which compiles a order by clause from the given list of OrderHints
74
     *
75
     * @param orderHints can be NULL
76
     * @return a StringBuffer holding the hql orderby clause
77
     */
78
    protected StringBuffer orderByClause(List<OrderHint> orderHints, String aliasName) {
79

    
80
        StringBuffer orderString = new StringBuffer();
81

    
82
        StringBuffer aliasPrefix = new StringBuffer();
83
        aliasPrefix.append(" ");
84
        if(aliasName != null && !aliasName.isEmpty()){
85
            aliasPrefix.append(aliasName).append(".");
86
        }
87

    
88
        if(orderHints != null && !orderHints.isEmpty()) {
89
            orderString.append(" order by");
90
            for(OrderHint orderHint : orderHints) {
91
                orderString.append(aliasPrefix).append(orderHint.toHql());
92
            }
93
        }
94
        return orderString;
95
    }
96

    
97
}
(5-5/24)