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(nullOrToString(predicate.getDefaultValue()), 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
|
/**
|
76
|
* Return null if obj is null, obj.toString otherwise
|
77
|
* @param defaultValue
|
78
|
* @return
|
79
|
*/
|
80
|
private Object nullOrToString(Object obj) {
|
81
|
return obj == null? null: obj.toString();
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public List<CdmPreference> list(IPreferencePredicate<?> predicate){
|
86
|
|
87
|
String hql = "FROM CdmPreference pref "
|
88
|
+ " WHERE pref.key.predicate = :predicate "
|
89
|
;
|
90
|
Query query = getSession().createQuery(hql);
|
91
|
query.setParameter("predicate", predicate.getKey());
|
92
|
@SuppressWarnings("unchecked")
|
93
|
List<CdmPreference> allPreferences = query.list();
|
94
|
return allPreferences;
|
95
|
}
|
96
|
|
97
|
@Override
|
98
|
public CdmPreference find(TaxonNode taxonNode, String predicate){
|
99
|
String treeIndex = taxonNode.treeIndex();
|
100
|
String[] splits = treeIndex == null ? new String[]{}: treeIndex.split("#");
|
101
|
List<String> filterStrings = new ArrayList<>();
|
102
|
filterStrings.add(PreferenceSubject.ROOT);
|
103
|
String rootSplit = "";
|
104
|
for (String split : splits){
|
105
|
if (! "".equals(split)) {
|
106
|
rootSplit += "#" + split;
|
107
|
filterStrings.add(TAXON_NODE_FILTER_START + rootSplit + "#]");
|
108
|
}
|
109
|
}
|
110
|
|
111
|
|
112
|
//TODO Top1 and ORDER BY treeIndex length and remove for() loop below
|
113
|
String hql = "FROM CdmPreference pref "
|
114
|
+ " WHERE pref.key.predicate = :predicate "
|
115
|
+ " AND pref.key.subject IN :subject "
|
116
|
;
|
117
|
Query query = getSession().createQuery(hql);
|
118
|
query.setParameter("predicate", predicate);
|
119
|
query.setParameterList("subject", filterStrings);
|
120
|
@SuppressWarnings("unchecked")
|
121
|
List<CdmPreference> allPreferences = query.list();
|
122
|
CdmPreference result = null;
|
123
|
for (CdmPreference pref : allPreferences){
|
124
|
//FIXME this is problematci
|
125
|
if (result == null || result.getSubjectString().length() < pref.getSubjectString().length()){
|
126
|
result = pref;
|
127
|
}
|
128
|
}
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
|
133
|
@Override
|
134
|
public long count(){
|
135
|
Criteria crit = getSession().createCriteria(CdmPreference.class);
|
136
|
crit.setProjection(Projections.rowCount());
|
137
|
return (Long)crit.uniqueResult();
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public List<CdmPreference> list(){
|
142
|
Criteria crit = getSession().createCriteria(CdmPreference.class);
|
143
|
@SuppressWarnings("unchecked")
|
144
|
List<CdmPreference> result = crit.list();
|
145
|
return result;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* {@inheritDoc}
|
150
|
*/
|
151
|
@Override
|
152
|
public void afterPropertiesSet() throws Exception {
|
153
|
CdmPreferenceLookup.instance().setIPreferenceDao(this);
|
154
|
|
155
|
}
|
156
|
|
157
|
}
|