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