Project

General

Profile

Download (4.9 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.common.CdmUtils;
23
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
24
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
25
import eu.etaxonomy.cdm.model.metadata.IPreferencePredicate;
26
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
27
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
28
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29
import eu.etaxonomy.cdm.persistence.dao.common.IPreferenceDao;
30

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

    
38
    private static final String TAXON_NODE_FILTER_START = PreferenceSubject.ROOT + "TaxonNode[";
39

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

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

    
49
	}
50

    
51
	@Override
52
	public void set(CdmPreference preference){
53
		CdmPreference pref = get(preference.getKey());
54
		//maybe
55
		//TODO maybe there is better way to allow updates without allowing to write CdmPref.value
56
		if (pref != null){
57
			getSession().delete(pref);
58
		}
59
		IPreferencePredicate<?> predicate = PreferencePredicate.getByKey(preference.getPredicate());
60
		if (predicate == null ||
61
		        !preference.isAllowOverride() ||
62
		        !CdmUtils.nullSafeEqual(predicate.getDefaultValue().toString(), preference.getValue())){
63
		    //do not save is value is default value with allow override
64
		    getSession().save(preference);
65
		}
66

    
67
		//old
68
//		if (pref == null){
69
//			getSessionFactory().openStatelessSession().insert(preference);
70
//		}else{
71
//			getSessionFactory().openStatelessSession().update(preference);
72
//		}
73
	}
74

    
75
    @Override
76
    public List<CdmPreference> list(IPreferencePredicate<?> predicate){
77

    
78
        String hql = "FROM CdmPreference pref "
79
                + " WHERE pref.key.predicate = :predicate "
80
                ;
81
        Query query = getSession().createQuery(hql);
82
        query.setParameter("predicate", predicate.getKey());
83
        @SuppressWarnings("unchecked")
84
        List<CdmPreference> allPreferences = query.list();
85
        return allPreferences;
86
    }
87

    
88
	@Override
89
	public CdmPreference find(TaxonNode taxonNode, String predicate){
90
	    String treeIndex = taxonNode.treeIndex();
91
	    String[] splits = treeIndex == null ? new String[]{}: treeIndex.split("#");
92
	    List<String> filterStrings = new ArrayList<>();
93
	    filterStrings.add(PreferenceSubject.ROOT);
94
	    String rootSplit = "";
95
	    for (String split : splits){
96
	        if (! "".equals(split)) {
97
	            rootSplit += "#" + split;
98
	            filterStrings.add(TAXON_NODE_FILTER_START + rootSplit + "#]");
99
	        }
100
	    }
101

    
102

    
103
	    //TODO Top1 and ORDER BY treeIndex length and remove for() loop below
104
	    String hql = "FROM CdmPreference pref "
105
	            + " WHERE pref.key.predicate = :predicate "
106
	            + "    AND pref.key.subject IN :subject "
107
	            ;
108
	    Query query = getSession().createQuery(hql);
109
	    query.setParameter("predicate", predicate);
110
	    query.setParameterList("subject", filterStrings);
111
        @SuppressWarnings("unchecked")
112
        List<CdmPreference> allPreferences = query.list();
113
        CdmPreference result = null;
114
        for (CdmPreference pref : allPreferences){
115
            //FIXME this is problematci
116
            if (result == null || result.getSubjectString().length() < pref.getSubjectString().length()){
117
                result = pref;
118
            }
119
        }
120
        return result;
121
	}
122

    
123

    
124
	@Override
125
	public long count(){
126
		Criteria crit = getSession().createCriteria(CdmPreference.class);
127
        crit.setProjection(Projections.rowCount());
128
        return (Long)crit.uniqueResult();
129
	}
130

    
131
    @Override
132
    public List<CdmPreference> list(){
133
        Criteria crit = getSession().createCriteria(CdmPreference.class);
134
        @SuppressWarnings("unchecked")
135
        List<CdmPreference> result = crit.list();
136
        return result;
137
    }
138

    
139
    /**
140
     * {@inheritDoc}
141
     */
142
    @Override
143
    public void afterPropertiesSet() throws Exception {
144
        CdmPreferenceLookup.instance().setIPreferenceDao(this);
145

    
146
    }
147

    
148
}
(19-19/25)