dbcc3ec5c5689b161847132dac009871db17ebe3
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
;
12 import java
.util
.List
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.hibernate
.Query
;
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
;
29 public abstract class AnnotatableDaoImpl
<T
extends AnnotatableEntity
>
30 extends VersionableDaoBase
<T
>
31 implements IAnnotatableDao
<T
> {
33 @SuppressWarnings("unused")
34 private static Logger logger
= Logger
.getLogger(AnnotatableDaoImpl
.class);
39 public AnnotatableDaoImpl(Class
<T
> type
) {
44 public int countAnnotations(T annotatableEntity
, MarkerType status
) {
45 checkNotInPriorView("AnnotatableDaoImpl.countAnnotations(T annotatableEntity, MarkerType status)");
48 String className
= annotatableEntity
.getClass().getName();
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" );
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
);
58 query
.setParameter("id", annotatableEntity
.getId());
61 return ((Long
)query
.uniqueResult()).intValue();
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)");
69 StringBuffer orderString
= new StringBuffer();
71 if(orderHints
!= null && !orderHints
.isEmpty()) {
72 orderString
.append(" ORDER BY");
73 for(OrderHint orderHint
: orderHints
) {
74 orderString
.append(" annotation." + orderHint
.getPropertyName() + " ");
76 if(orderHint
.getSortOrder() == SortOrder
.ASCENDING
) {
77 orderString
.append("ASC");
79 orderString
.append("DESC");
84 String className
= annotatableEntity
.getClass().getName();
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());
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
);
94 query
.setParameter("id",annotatableEntity
.getId());
97 if(pageSize
!= null) {
98 query
.setMaxResults(pageSize
);
99 if(pageNumber
!= null) {
100 query
.setFirstResult(pageNumber
* pageSize
);
104 List
<Annotation
> results
= query
.list();
105 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
110 public int countMarkers(T annotatableEntity
, Boolean technical
) {
111 checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical");
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 ");
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
);
123 query
.setParameter("id",annotatableEntity
.getId());
125 return ((Long
)query
.uniqueResult()).intValue();
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)");
133 StringBuffer orderString
= new StringBuffer();
135 if(orderHints
!= null && !orderHints
.isEmpty()) {
136 orderString
.append(" ORDER BY");
137 for(OrderHint orderHint
: orderHints
) {
138 orderString
.append(" marker." + orderHint
.getPropertyName() + " ");
140 if(orderHint
.getSortOrder() == SortOrder
.ASCENDING
) {
141 orderString
.append("ASC");
143 orderString
.append("DESC");
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());
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
);
157 query
.setParameter("id",annotatableEntity
.getId());
159 if(pageSize
!= null) {
160 query
.setMaxResults(pageSize
);
161 if(pageNumber
!= null) {
162 query
.setFirstResult(pageNumber
* pageSize
);
166 List
<Marker
> results
= query
.list();
167 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
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)");
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");
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
);
184 // if(clazz == null) {
185 // query.setParameter("class", type.getName());
187 // query.setParameter("class", clazz.getName());
190 return ((Long
)query
.uniqueResult()).intValue();
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)");
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");
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
);
206 // if(clazz == null) {
207 // query.setParameter("class", type.getName());
209 // query.setParameter("class", clazz.getName());
212 if(pageSize
!= null) {
213 query
.setMaxResults(pageSize
);
214 if(pageNumber
!= null) {
215 query
.setFirstResult(pageNumber
* pageSize
);
219 List
<Object
[]> result
= query
.list();
221 if(propertyPaths
!= null && !propertyPaths
.isEmpty()) {
222 for(Object
[] objects
: result
) {
223 defaultBeanInitializer
.initialize(objects
[0], propertyPaths
);