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.ArrayList;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.hibernate.Query;
|
18
|
import org.hibernate.Session;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
21
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
22
|
import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
|
23
|
import eu.etaxonomy.cdm.model.common.Marker;
|
24
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
25
|
import eu.etaxonomy.cdm.persistence.dao.common.IAnnotatableDao;
|
26
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
27
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
28
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
29
|
|
30
|
/**
|
31
|
* @author n.hoffmann
|
32
|
* @created 24.09.2008
|
33
|
*/
|
34
|
public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity>
|
35
|
extends VersionableDaoBase<T>
|
36
|
implements IAnnotatableDao<T> {
|
37
|
|
38
|
@SuppressWarnings("unused")
|
39
|
private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class);
|
40
|
|
41
|
/**
|
42
|
* @param type
|
43
|
*/
|
44
|
public AnnotatableDaoImpl(Class<T> type) {
|
45
|
super(type);
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public int countAnnotations(T annotatableEntity, MarkerType status) {
|
50
|
checkNotInPriorView("AnnotatableDaoImpl.countAnnotations(T annotatableEntity, MarkerType status)");
|
51
|
Query query = null;
|
52
|
|
53
|
String className = annotatableEntity.getClass().getName();
|
54
|
if(status == null) {
|
55
|
//AND annoEnt.class = :class" does not work for some reason
|
56
|
query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id" );
|
57
|
} else {
|
58
|
query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker "
|
59
|
+ " WHERE annoEnt.id = :id AND marker.markerType = :status");
|
60
|
query.setParameter("status", status);
|
61
|
}
|
62
|
|
63
|
query.setParameter("id", annotatableEntity.getId());
|
64
|
|
65
|
|
66
|
return ((Long)query.uniqueResult()).intValue();
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public List<Annotation> getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
71
|
checkNotInPriorView("AnnotatableDaoImpl.getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber)");
|
72
|
Query query = null;
|
73
|
|
74
|
StringBuffer orderString = new StringBuffer();
|
75
|
|
76
|
if(orderHints != null && !orderHints.isEmpty()) {
|
77
|
orderString.append(" ORDER BY");
|
78
|
for(OrderHint orderHint : orderHints) {
|
79
|
orderString.append(" annotation." + orderHint.getPropertyName() + " ");
|
80
|
|
81
|
if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
|
82
|
orderString.append("ASC");
|
83
|
} else {
|
84
|
orderString.append("DESC");
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
String className = annotatableEntity.getClass().getName();
|
90
|
if(status == null) {
|
91
|
//AND annoEnt.class = :class does not work for some reason
|
92
|
query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id " + orderString.toString());
|
93
|
} else {
|
94
|
query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker " +
|
95
|
" WHERE annoEnt.id = :id AND marker.markerType = :status" + orderString.toString());
|
96
|
query.setParameter("status",status);
|
97
|
}
|
98
|
|
99
|
query.setParameter("id",annotatableEntity.getId());
|
100
|
|
101
|
|
102
|
if(pageSize != null) {
|
103
|
query.setMaxResults(pageSize);
|
104
|
if(pageNumber != null) {
|
105
|
query.setFirstResult(pageNumber * pageSize);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
List<Annotation> results = query.list();
|
110
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
111
|
return results;
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public int countMarkers(T annotatableEntity, Boolean technical) {
|
116
|
checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical");
|
117
|
Query query = null;
|
118
|
|
119
|
String className = annotatableEntity.getClass().getName();
|
120
|
if(technical == null) {
|
121
|
query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id ");
|
122
|
} else {
|
123
|
query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
|
124
|
+ " WHERE annoEnt.id = :id AND type.isTechnical = :technical");
|
125
|
query.setParameter("technical", technical);
|
126
|
}
|
127
|
|
128
|
query.setParameter("id",annotatableEntity.getId());
|
129
|
|
130
|
return ((Long)query.uniqueResult()).intValue();
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public List<Marker> getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
135
|
checkNotInPriorView("AnnotatableDaoImpl.getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
136
|
Query query = null;
|
137
|
|
138
|
StringBuffer orderString = new StringBuffer();
|
139
|
|
140
|
if(orderHints != null && !orderHints.isEmpty()) {
|
141
|
orderString.append(" ORDER BY");
|
142
|
for(OrderHint orderHint : orderHints) {
|
143
|
orderString.append(" marker." + orderHint.getPropertyName() + " ");
|
144
|
|
145
|
if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
|
146
|
orderString.append("ASC");
|
147
|
} else {
|
148
|
orderString.append("DESC");
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
|
153
|
String className = annotatableEntity.getClass().getName();
|
154
|
if(technical == null) {
|
155
|
query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id" + orderString.toString());
|
156
|
} else {
|
157
|
query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
|
158
|
+ " WHERE annoEnt.id = :id AND type.isTechnical = :technical" + orderString.toString());
|
159
|
query.setParameter("technical",technical);
|
160
|
}
|
161
|
|
162
|
query.setParameter("id",annotatableEntity.getId());
|
163
|
|
164
|
if(pageSize != null) {
|
165
|
query.setMaxResults(pageSize);
|
166
|
if(pageNumber != null) {
|
167
|
query.setFirstResult(pageNumber * pageSize);
|
168
|
}
|
169
|
}
|
170
|
|
171
|
List<Marker> results = query.list();
|
172
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
173
|
return results;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public int countMarkers(Class<? extends T> clazz, Boolean technical) {
|
178
|
checkNotInPriorView("AnnotatableDaoImpl.countMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
179
|
Query query = null;
|
180
|
String className = clazz == null ? type.getName() : clazz.getName();
|
181
|
if(technical == null) {
|
182
|
query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type");
|
183
|
} else {
|
184
|
query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
|
185
|
+ " WHERE type.technical = :technical");
|
186
|
query.setParameter("technical", technical);
|
187
|
}
|
188
|
|
189
|
// if(clazz == null) {
|
190
|
// query.setParameter("class", type.getName());
|
191
|
// } else {
|
192
|
// query.setParameter("class", clazz.getName());
|
193
|
// }
|
194
|
|
195
|
return ((Long)query.uniqueResult()).intValue();
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
200
|
checkNotInPriorView("AnnotatableDaoImpl.groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
201
|
Query query = null;
|
202
|
String className = clazz == null ? type.getName() : clazz.getName();
|
203
|
if(technical == null) {
|
204
|
query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type GROUP BY type ORDER BY type.titleCache ASC");
|
205
|
} else {
|
206
|
query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
|
207
|
+ " WHERE type.technical = :technical GROUP BY type ORDER BY type.titleCache ASC");
|
208
|
query.setParameter("technical", technical);
|
209
|
}
|
210
|
|
211
|
// if(clazz == null) {
|
212
|
// query.setParameter("class", type.getName());
|
213
|
// } else {
|
214
|
// query.setParameter("class", clazz.getName());
|
215
|
// }
|
216
|
|
217
|
if(pageSize != null) {
|
218
|
query.setMaxResults(pageSize);
|
219
|
if(pageNumber != null) {
|
220
|
query.setFirstResult(pageNumber * pageSize);
|
221
|
}
|
222
|
}
|
223
|
|
224
|
List<Object[]> result = query.list();
|
225
|
|
226
|
if(propertyPaths != null && !propertyPaths.isEmpty()) {
|
227
|
for(Object[] objects : result) {
|
228
|
defaultBeanInitializer.initialize(objects[0], propertyPaths);
|
229
|
}
|
230
|
}
|
231
|
|
232
|
return result;
|
233
|
}
|
234
|
|
235
|
@Override
|
236
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache(Integer limit, String pattern){
|
237
|
return getUuidAndTitleCache(type, limit, pattern);
|
238
|
}
|
239
|
|
240
|
|
241
|
@Override
|
242
|
public <S extends T> List<UuidAndTitleCache<S>> getUuidAndTitleCache(Class<S> clazz, Integer limit, String pattern){
|
243
|
Session session = getSession();
|
244
|
Query query = null;
|
245
|
if (pattern != null){
|
246
|
query = session.createQuery("select uuid, id, titleCache from " + clazz.getSimpleName() +" where titleCache like :pattern");
|
247
|
pattern = pattern.replace("*", "%");
|
248
|
pattern = pattern.replace("?", "_");
|
249
|
pattern = pattern + "%";
|
250
|
query.setParameter("pattern", pattern);
|
251
|
} else {
|
252
|
query = session.createQuery("select uuid, id, titleCache from " + clazz.getSimpleName() );
|
253
|
}
|
254
|
if (limit != null){
|
255
|
query.setMaxResults(limit);
|
256
|
}
|
257
|
return getUuidAndTitleCache(query);
|
258
|
}
|
259
|
|
260
|
|
261
|
@Override
|
262
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache(){
|
263
|
return getUuidAndTitleCache(type, null, null);
|
264
|
}
|
265
|
|
266
|
protected <E extends IAnnotatableEntity> List<UuidAndTitleCache<E>> getUuidAndAbbrevTitleCache(Query query){
|
267
|
List<UuidAndTitleCache<E>> list = new ArrayList<UuidAndTitleCache<E>>();
|
268
|
|
269
|
List<Object[]> result = query.list();
|
270
|
|
271
|
for(Object[] object : result){
|
272
|
list.add(new UuidAndTitleCache<E>((UUID) object[0],(Integer) object[1], (String) object[3], (String) object[2]));
|
273
|
}
|
274
|
return list;
|
275
|
}
|
276
|
|
277
|
protected <E extends IAnnotatableEntity> List<UuidAndTitleCache<E>> getUuidAndTitleCache(Query query){
|
278
|
List<UuidAndTitleCache<E>> list = new ArrayList<UuidAndTitleCache<E>>();
|
279
|
|
280
|
List<Object[]> result = query.list();
|
281
|
|
282
|
for(Object[] object : result){
|
283
|
list.add(new UuidAndTitleCache<E>((UUID) object[0],(Integer) object[1], (String) object[2]));
|
284
|
}
|
285
|
return list;
|
286
|
}
|
287
|
|
288
|
}
|