Revision 0ecfd682
Added by Andreas Müller almost 8 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/AnnotatableDaoImpl.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
... | ... | |
25 | 25 |
/** |
26 | 26 |
* @author n.hoffmann |
27 | 27 |
* @created 24.09.2008 |
28 |
* @version 1.0 |
|
29 | 28 |
*/ |
30 | 29 |
public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity> extends VersionableDaoBase<T> implements IAnnotatableDao<T> { |
31 | 30 |
@SuppressWarnings("unused") |
32 | 31 |
private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class); |
33 |
|
|
32 |
|
|
34 | 33 |
/** |
35 | 34 |
* @param type |
36 | 35 |
*/ |
37 | 36 |
public AnnotatableDaoImpl(Class<T> type) { |
38 | 37 |
super(type); |
39 | 38 |
} |
40 |
|
|
41 |
public int countAnnotations(T annotatableEntity, MarkerType status) { |
|
39 |
|
|
40 |
@Override |
|
41 |
public int countAnnotations(T annotatableEntity, MarkerType status) { |
|
42 | 42 |
checkNotInPriorView("AnnotatableDaoImpl.countAnnotations(T annotatableEntity, MarkerType status)"); |
43 | 43 |
Query query = null; |
44 |
|
|
45 |
if(status == null) { |
|
46 |
query = getSession().createQuery("select count(annotation) from Annotation annotation where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class"); |
|
47 |
} else { |
|
48 |
query = getSession().createQuery("select count(annotation) from Annotation annotation join annotation.markers marker where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class and marker.markerType = :status"); |
|
49 |
query.setParameter("status",status); |
|
50 |
} |
|
51 |
|
|
52 |
query.setParameter("id",annotatableEntity.getId()); |
|
53 |
query.setParameter("class", annotatableEntity.getClass().getName()); |
|
54 |
|
|
44 |
|
|
45 |
String className = annotatableEntity.getClass().getName(); |
|
46 |
if(status == null) { |
|
47 |
//AND annoEnt.class = :class" does not work for some reason |
|
48 |
query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id" ); |
|
49 |
} else { |
|
50 |
query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker " |
|
51 |
+ " WHERE annoEnt.id = :id AND marker.markerType = :status"); |
|
52 |
query.setParameter("status", status); |
|
53 |
} |
|
54 |
|
|
55 |
query.setParameter("id", annotatableEntity.getId()); |
|
56 |
|
|
57 |
|
|
55 | 58 |
return ((Long)query.uniqueResult()).intValue(); |
56 | 59 |
} |
57 |
|
|
58 |
public List<Annotation> getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) { |
|
60 |
|
|
61 |
@Override |
|
62 |
public List<Annotation> getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) { |
|
59 | 63 |
checkNotInPriorView("AnnotatableDaoImpl.getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber)"); |
60 | 64 |
Query query = null; |
61 |
|
|
65 |
|
|
62 | 66 |
StringBuffer orderString = new StringBuffer(); |
63 |
|
|
67 |
|
|
64 | 68 |
if(orderHints != null && !orderHints.isEmpty()) { |
65 |
orderString.append(" order by");
|
|
69 |
orderString.append(" ORDER BY");
|
|
66 | 70 |
for(OrderHint orderHint : orderHints) { |
67 | 71 |
orderString.append(" annotation." + orderHint.getPropertyName() + " "); |
68 |
|
|
72 |
|
|
69 | 73 |
if(orderHint.getSortOrder() == SortOrder.ASCENDING) { |
70 |
orderString.append("asc");
|
|
74 |
orderString.append("ASC");
|
|
71 | 75 |
} else { |
72 |
orderString.append("desc");
|
|
76 |
orderString.append("DESC");
|
|
73 | 77 |
} |
74 | 78 |
} |
75 | 79 |
} |
76 |
|
|
77 |
|
|
78 |
if(status == null) { |
|
79 |
query = getSession().createQuery("select annotation from Annotation annotation where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class" + orderString.toString()); |
|
80 |
} else { |
|
81 |
query = getSession().createQuery("select annotation from Annotation annotation join annotation.markers marker where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class and marker.markerType = :status" + orderString.toString()); |
|
82 |
query.setParameter("status",status); |
|
83 |
} |
|
84 |
|
|
85 |
query.setParameter("id",annotatableEntity.getId()); |
|
86 |
query.setParameter("class", annotatableEntity.getClass().getName()); |
|
87 |
|
|
80 |
|
|
81 |
String className = annotatableEntity.getClass().getName(); |
|
82 |
if(status == null) { |
|
83 |
//AND annoEnt.class = :class does not work for some reason |
|
84 |
query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id " + orderString.toString()); |
|
85 |
} else { |
|
86 |
query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker " + |
|
87 |
" WHERE annoEnt.id = :id AND marker.markerType = :status" + orderString.toString()); |
|
88 |
query.setParameter("status",status); |
|
89 |
} |
|
90 |
|
|
91 |
query.setParameter("id",annotatableEntity.getId()); |
|
92 |
|
|
93 |
|
|
88 | 94 |
if(pageSize != null) { |
89 | 95 |
query.setMaxResults(pageSize); |
90 | 96 |
if(pageNumber != null) { |
91 | 97 |
query.setFirstResult(pageNumber * pageSize); |
92 | 98 |
} |
93 | 99 |
} |
94 |
|
|
95 |
List<Annotation> results = (List<Annotation>)query.list();
|
|
100 |
|
|
101 |
List<Annotation> results = query.list(); |
|
96 | 102 |
defaultBeanInitializer.initializeAll(results, propertyPaths); |
97 | 103 |
return results; |
98 | 104 |
} |
99 |
|
|
100 |
public int countMarkers(T annotatableEntity, Boolean technical) { |
|
105 |
|
|
106 |
@Override |
|
107 |
public int countMarkers(T annotatableEntity, Boolean technical) { |
|
101 | 108 |
checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical"); |
102 | 109 |
Query query = null; |
103 |
|
|
110 |
|
|
111 |
String className = annotatableEntity.getClass().getName(); |
|
104 | 112 |
if(technical == null) { |
105 |
query = getSession().createQuery("select count(marker) from Marker marker where marker.markedObj.id = :id and marker.markedObj.class = :class");
|
|
113 |
query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id ");
|
|
106 | 114 |
} else { |
107 |
query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.id = :id and marker.markedObj.class = :class and type.isTechnical = :technical"); |
|
108 |
query.setParameter("technical",technical); |
|
115 |
query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type " |
|
116 |
+ " WHERE annoEnt.id = :id AND type.isTechnical = :technical"); |
|
117 |
query.setParameter("technical", technical); |
|
109 | 118 |
} |
110 |
|
|
119 |
|
|
111 | 120 |
query.setParameter("id",annotatableEntity.getId()); |
112 |
query.setParameter("class", annotatableEntity.getClass().getName()); |
|
113 |
|
|
121 |
|
|
114 | 122 |
return ((Long)query.uniqueResult()).intValue(); |
115 | 123 |
} |
116 |
|
|
124 |
|
|
125 |
@Override |
|
117 | 126 |
public List<Marker> getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) { |
118 | 127 |
checkNotInPriorView("AnnotatableDaoImpl.getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)"); |
119 | 128 |
Query query = null; |
120 |
|
|
129 |
|
|
121 | 130 |
StringBuffer orderString = new StringBuffer(); |
122 |
|
|
131 |
|
|
123 | 132 |
if(orderHints != null && !orderHints.isEmpty()) { |
124 |
orderString.append(" order by");
|
|
133 |
orderString.append(" ORDER BY");
|
|
125 | 134 |
for(OrderHint orderHint : orderHints) { |
126 | 135 |
orderString.append(" marker." + orderHint.getPropertyName() + " "); |
127 |
|
|
136 |
|
|
128 | 137 |
if(orderHint.getSortOrder() == SortOrder.ASCENDING) { |
129 |
orderString.append("asc");
|
|
138 |
orderString.append("ASC");
|
|
130 | 139 |
} else { |
131 |
orderString.append("desc");
|
|
140 |
orderString.append("DESC");
|
|
132 | 141 |
} |
133 | 142 |
} |
134 | 143 |
} |
135 |
|
|
136 |
|
|
144 |
|
|
145 |
String className = annotatableEntity.getClass().getName(); |
|
137 | 146 |
if(technical == null) { |
138 |
query = getSession().createQuery("select marker from Marker marker where marker.markedObj.id = :id and marker.markedObj.class = :class" + orderString.toString());
|
|
147 |
query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id" + orderString.toString());
|
|
139 | 148 |
} else { |
140 |
query = getSession().createQuery("select marker from Marker marker join marker.markerType type where marker.markedObj.id = :id and marker.markedObj.class = :class and type.isTechnical = :technical" + orderString.toString()); |
|
149 |
query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type " |
|
150 |
+ " WHERE annoEnt.id = :id AND type.isTechnical = :technical" + orderString.toString()); |
|
141 | 151 |
query.setParameter("technical",technical); |
142 | 152 |
} |
143 |
|
|
153 |
|
|
144 | 154 |
query.setParameter("id",annotatableEntity.getId()); |
145 |
query.setParameter("class", annotatableEntity.getClass().getName()); |
|
146 |
|
|
155 |
|
|
147 | 156 |
if(pageSize != null) { |
148 | 157 |
query.setMaxResults(pageSize); |
149 | 158 |
if(pageNumber != null) { |
150 | 159 |
query.setFirstResult(pageNumber * pageSize); |
151 | 160 |
} |
152 | 161 |
} |
153 |
|
|
154 |
List<Marker> results = (List<Marker>)query.list();
|
|
162 |
|
|
163 |
List<Marker> results = query.list(); |
|
155 | 164 |
defaultBeanInitializer.initializeAll(results, propertyPaths); |
156 | 165 |
return results; |
157 | 166 |
} |
158 |
|
|
167 |
|
|
168 |
@Override |
|
159 | 169 |
public int countMarkers(Class<? extends T> clazz, Boolean technical) { |
160 | 170 |
checkNotInPriorView("AnnotatableDaoImpl.countMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)"); |
161 | 171 |
Query query = null; |
172 |
String className = clazz == null ? type.getName() : clazz.getName(); |
|
162 | 173 |
if(technical == null) { |
163 |
query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class"); |
|
164 |
} else { |
|
165 |
query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class and type.technical = :technical"); |
|
166 |
query.setParameter("technical",technical); |
|
167 |
} |
|
168 |
|
|
169 |
if(clazz == null) { |
|
170 |
query.setParameter("class", type.getName()); |
|
174 |
query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type"); |
|
171 | 175 |
} else { |
172 |
query.setParameter("class", clazz.getName()); |
|
176 |
query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type " |
|
177 |
+ " WHERE type.technical = :technical"); |
|
178 |
query.setParameter("technical", technical); |
|
173 | 179 |
} |
174 |
|
|
180 |
|
|
181 |
// if(clazz == null) { |
|
182 |
// query.setParameter("class", type.getName()); |
|
183 |
// } else { |
|
184 |
// query.setParameter("class", clazz.getName()); |
|
185 |
// } |
|
186 |
|
|
175 | 187 |
return ((Long)query.uniqueResult()).intValue(); |
176 | 188 |
} |
177 |
|
|
178 |
public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
189 |
|
|
190 |
@Override |
|
191 |
public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
179 | 192 |
checkNotInPriorView("AnnotatableDaoImpl.groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)"); |
180 | 193 |
Query query = null; |
181 |
if(technical == null) { |
|
182 |
query = getSession().createQuery("select type, count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class group by type order by type.titleCache asc"); |
|
183 |
} else { |
|
184 |
query = getSession().createQuery("select type, count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class and type.technical = :technical group by type order by type.titleCache asc"); |
|
185 |
query.setParameter("technical",technical); |
|
186 |
} |
|
187 |
|
|
188 |
if(clazz == null) { |
|
189 |
query.setParameter("class", type.getName()); |
|
194 |
String className = clazz == null ? type.getName() : clazz.getName(); |
|
195 |
if(technical == null) { |
|
196 |
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"); |
|
190 | 197 |
} else { |
191 |
query.setParameter("class", clazz.getName()); |
|
198 |
query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type " |
|
199 |
+ " WHERE type.technical = :technical GROUP BY type ORDER BY type.titleCache ASC"); |
|
200 |
query.setParameter("technical", technical); |
|
192 | 201 |
} |
193 |
|
|
202 |
|
|
203 |
// if(clazz == null) { |
|
204 |
// query.setParameter("class", type.getName()); |
|
205 |
// } else { |
|
206 |
// query.setParameter("class", clazz.getName()); |
|
207 |
// } |
|
208 |
|
|
194 | 209 |
if(pageSize != null) { |
195 | 210 |
query.setMaxResults(pageSize); |
196 | 211 |
if(pageNumber != null) { |
197 | 212 |
query.setFirstResult(pageNumber * pageSize); |
198 | 213 |
} |
199 | 214 |
} |
200 |
|
|
201 |
List<Object[]> result = (List<Object[]>)query.list();
|
|
202 |
|
|
215 |
|
|
216 |
List<Object[]> result = query.list(); |
|
217 |
|
|
203 | 218 |
if(propertyPaths != null && !propertyPaths.isEmpty()) { |
204 | 219 |
for(Object[] objects : result) { |
205 | 220 |
defaultBeanInitializer.initialize(objects[0], propertyPaths); |
206 | 221 |
} |
207 | 222 |
} |
208 |
|
|
223 |
|
|
209 | 224 |
return result; |
210 | 225 |
} |
211 |
|
|
226 |
|
|
212 | 227 |
} |
Also available in: Unified diff
Remove bidirectionality for supplemental data #5743