45c315fdaf8faabf7c3d29e8886cad74293e229e
[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.MarkerType;
20 import eu.etaxonomy.cdm.persistence.dao.common.IAnnotatableDao;
21
22 /**
23 * @author n.hoffmann
24 * @created 24.09.2008
25 * @version 1.0
26 */
27 public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity> extends CdmEntityDaoBase<T> implements IAnnotatableDao<T> {
28 private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class);
29
30 /**
31 * @param type
32 */
33 public AnnotatableDaoImpl(Class<T> type) {
34 super(type);
35 }
36 public int countAnnotations(T annotatableEntity, MarkerType status) {
37 Query query = null;
38
39 if(status == null) {
40 query = getSession().createQuery("select count(annotation) from Annotation annotation where annotation.annotatedObj = :annotatableEntity");
41 } else {
42 query = getSession().createQuery("select count(annotation) from Annotation annotation join annotation.markers marker where annotation.annotatedObj = :annotatableEntity and marker.markerType = :status");
43 query.setParameter("status",status);
44 }
45
46 query.setParameter("annotatableEntity",annotatableEntity);
47
48 return ((Long)query.uniqueResult()).intValue();
49 }
50 public List<Annotation> getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber) {
51 Query query = null;
52
53 if(status == null) {
54 query = getSession().createQuery("select annotation from Annotation annotation where annotation.annotatedObj = :annotatableEntity");
55 } else {
56 query = getSession().createQuery("select annotation from Annotation annotation join annotation.markers marker where annotation.annotatedObj = :annotatableEntity and marker.markerType = :status");
57 query.setParameter("status",status);
58 }
59
60 query.setParameter("annotatableEntity",annotatableEntity);
61
62 if(pageSize != null) {
63 query.setMaxResults(pageSize);
64 if(pageNumber != null) {
65 query.setFirstResult(pageNumber * pageSize);
66 }
67 }
68
69 return (List<Annotation>)query.list();
70 }
71
72 }