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
|
//TODO Top1 and ORDER BY treeIndex length and remove for() loop below
|
117
|
String hql = "FROM CdmPreference pref "
|
118
|
+ " WHERE pref.key.predicate = :predicate "
|
119
|
+ " AND pref.key.subject IN :subject "
|
120
|
;
|
121
|
Query query = getSession().createQuery(hql);
|
122
|
query.setParameter("predicate", predicate);
|
123
|
query.setParameterList("subject", filterStrings);
|
124
|
@SuppressWarnings("unchecked")
|
125
|
List<CdmPreference> allPreferences = query.list();
|
126
|
CdmPreference result = null;
|
127
|
for (CdmPreference pref : allPreferences){
|
128
|
//FIXME this is problematic
|
129
|
if (result == null || result.getSubjectString().length() < pref.getSubjectString().length()){
|
130
|
result = pref;
|
131
|
}
|
132
|
}
|
133
|
return result;
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public long count(){
|
138
|
Criteria crit = getSession().createCriteria(CdmPreference.class);
|
139
|
crit.setProjection(Projections.rowCount());
|
140
|
return (Long)crit.uniqueResult();
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
public List<CdmPreference> list(){
|
145
|
Criteria crit = getSession().createCriteria(CdmPreference.class);
|
146
|
@SuppressWarnings("unchecked")
|
147
|
List<CdmPreference> result = crit.list();
|
148
|
return result;
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
public void afterPropertiesSet() throws Exception {
|
153
|
CdmPreferenceLookup.instance().setIPreferenceDao(this);
|
154
|
|
155
|
}
|
156
|
|
157
|
}
|