Project

General

Profile

Download (5 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.dao.hibernate.common;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.hibernate.Criteria;
16
import org.hibernate.Query;
17
import org.hibernate.Session;
18
import org.hibernate.criterion.Projections;
19
import org.springframework.beans.factory.InitializingBean;
20
import org.springframework.stereotype.Repository;
21

    
22
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
23
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
24
import eu.etaxonomy.cdm.model.metadata.IPreferencePredicate;
25
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.cdm.persistence.dao.common.IPreferenceDao;
28

    
29
/**
30
 * @author a.mueller
31
 * @since 2013-09-09
32
 */
33
@Repository
34
public class PreferenceDaoImpl extends DaoBase implements IPreferenceDao, InitializingBean  {
35

    
36
    private static final String TAXON_NODE_FILTER_START = PreferenceSubject.ROOT + "TaxonNode[";
37

    
38
	@Override
39
	public CdmPreference get(PrefKey key){
40
		Session session = getSession();
41
		return session.get(CdmPreference.class, key);
42

    
43
		//old
44
//		StatelessSession session = getSessionFactory().openStatelessSession();
45
//		return (CdmPreference) session.get(CdmPreference.class, key);
46

    
47
	}
48

    
49
	@Override
50
	public void set(CdmPreference preference){
51
		CdmPreference pref = get(preference.getKey());
52
		//maybe
53
		//TODO maybe there is better way to allow updates without allowing to write CdmPref.value
54
		if (pref != null){
55
			getSession().delete(pref);
56
		}
57
//		IPreferencePredicate<?> predicate = PreferencePredicate.getByKey(preference.getPredicate());
58
//		if (predicate == null ||
59
//		        !preference.isAllowOverride()){
60
		    getSession().save(preference);
61

    
62
		//old
63
//		if (pref == null){
64
//			getSessionFactory().openStatelessSession().insert(preference);
65
//		}else{
66
//			getSessionFactory().openStatelessSession().update(preference);
67
//		}
68
	}
69

    
70
	@Override
71
    public void remove(PrefKey key){
72
        CdmPreference pref = get(key);
73
        if (pref != null){
74
            getSession().delete(pref);
75
        }
76

    
77
    }
78

    
79
    /**
80
     * Return null if obj is null, obj.toString otherwise
81
     * @param defaultValue
82
     * @return
83
     */
84
    private Object nullOrToString(Object obj) {
85
        return obj == null? null: obj.toString();
86
    }
87

    
88
    @Override
89
    public List<CdmPreference> list(IPreferencePredicate<?> predicate){
90

    
91
        String hql = "FROM CdmPreference pref "
92
                + " WHERE pref.key.predicate = :predicate "
93
                ;
94
        Query query = getSession().createQuery(hql);
95
        query.setParameter("predicate", predicate.getKey());
96
        @SuppressWarnings("unchecked")
97
        List<CdmPreference> allPreferences = query.list();
98
        return allPreferences;
99
    }
100

    
101
	@Override
102
	public CdmPreference find(TaxonNode taxonNode, String predicate){
103

    
104
	    String treeIndex = taxonNode.treeIndex();
105
	    String[] splits = treeIndex == null ? new String[]{}: treeIndex.split("#");
106
	    List<String> filterStrings = new ArrayList<>();
107
	    filterStrings.add(PreferenceSubject.ROOT);
108
	    String rootSplit = "";
109
	    for (String split : splits){
110
	        if (! "".equals(split)) {
111
	            rootSplit += "#" + split;
112
	            filterStrings.add(TAXON_NODE_FILTER_START + rootSplit + "#]");
113
	        }
114
	    }
115

    
116

    
117
	    //TODO Top1 and ORDER BY treeIndex length and remove for() loop below
118
	    String hql = "FROM CdmPreference pref "
119
	            + " WHERE pref.key.predicate = :predicate "
120
	            + "    AND pref.key.subject IN :subject "
121
	            ;
122
	    Query query = getSession().createQuery(hql);
123
	    query.setParameter("predicate", predicate);
124
	    query.setParameterList("subject", filterStrings);
125
        @SuppressWarnings("unchecked")
126
        List<CdmPreference> allPreferences = query.list();
127
        CdmPreference result = null;
128
        for (CdmPreference pref : allPreferences){
129
            //FIXME this is problematic
130
            if (result == null || result.getSubjectString().length() < pref.getSubjectString().length()){
131
                result = pref;
132
            }
133
        }
134
        return result;
135
	}
136

    
137
	@Override
138
	public long count(){
139
		Criteria crit = getSession().createCriteria(CdmPreference.class);
140
        crit.setProjection(Projections.rowCount());
141
        return (Long)crit.uniqueResult();
142
	}
143

    
144
    @Override
145
    public List<CdmPreference> list(){
146
        Criteria crit = getSession().createCriteria(CdmPreference.class);
147
        @SuppressWarnings("unchecked")
148
        List<CdmPreference> result = crit.list();
149
        return result;
150
    }
151

    
152
    @Override
153
    public void afterPropertiesSet() throws Exception {
154
        CdmPreferenceLookup.instance().setIPreferenceDao(this);
155

    
156
    }
157

    
158
}
(14-14/19)