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
.name
;
12 import java
.util
.List
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.hibernate
.Criteria
;
16 import org
.hibernate
.Query
;
17 import org
.hibernate
.criterion
.Projections
;
18 import org
.hibernate
.criterion
.Restrictions
;
19 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
20 import org
.springframework
.stereotype
.Repository
;
22 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
23 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationship
;
24 import eu
.etaxonomy
.cdm
.model
.name
.HybridRelationshipType
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
27 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatus
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.name
.ITaxonNameDao
;
41 @Qualifier("taxonNameDaoHibernateImpl")
42 public class TaxonNameDaoHibernateImpl
43 extends IdentifiableDaoBase
<TaxonNameBase
> implements ITaxonNameDao
{
45 @SuppressWarnings("unused")
46 private static final Logger logger
= Logger
.getLogger(TaxonNameDaoHibernateImpl
.class);
48 public TaxonNameDaoHibernateImpl() {
49 super(TaxonNameBase
.class);
52 public int countHybridNames(BotanicalName name
, HybridRelationshipType type
) {
55 query
= getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name");
57 query
= getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name and relation.type = :type");
58 query
.setParameter("type", type
);
60 query
.setParameter("name",name
);
61 return ((Long
)query
.uniqueResult()).intValue();
64 public int countNames(String queryString
) {
66 Criteria criteria
= getSession().createCriteria(TaxonNameBase
.class);
68 if (queryString
!= null) {
69 criteria
.add(Restrictions
.ilike("nameCache", queryString
));
71 criteria
.setProjection(Projections
.projectionList().add(Projections
.rowCount()));
73 return (Integer
)criteria
.uniqueResult();
76 public int countNames(String genusOrUninomial
, String infraGenericEpithet
, String specificEpithet
, String infraSpecificEpithet
, Rank rank
) {
77 Criteria criteria
= getSession().createCriteria(TaxonNameBase
.class);
80 * Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
81 * We need to convert nulls to NullRestrictions for now
83 if(genusOrUninomial
!= null) {
84 criteria
.add(Restrictions
.eq("genusOrUninomial",genusOrUninomial
));
86 criteria
.add(Restrictions
.isNull("genusOrUninomial"));
89 if(infraGenericEpithet
!= null) {
90 criteria
.add(Restrictions
.eq("infraGenericEpithet", infraGenericEpithet
));
92 criteria
.add(Restrictions
.isNull("infraGenericEpithet"));
95 if(specificEpithet
!= null) {
96 criteria
.add(Restrictions
.eq("specificEpithet", specificEpithet
));
98 criteria
.add(Restrictions
.isNull("specificEpithet"));
101 if(infraSpecificEpithet
!= null) {
102 criteria
.add(Restrictions
.eq("infraSpecificEpithet",infraSpecificEpithet
));
104 criteria
.add(Restrictions
.isNull("infraSpecificEpithet"));
106 criteria
.add(Restrictions
.eq("rank", rank
));
108 criteria
.setProjection(Projections
.rowCount());
109 return (Integer
)criteria
.uniqueResult();
112 public int countRelatedNames(TaxonNameBase name
, NameRelationshipType type
) {
115 query
= getSession().createQuery("select count(relation) from NameRelationship relation where relation.relatedFrom = :name");
117 query
= getSession().createQuery("select count(relation) from NameRelationship relation where relation.relatedFrom = :name and relation.type = :type");
118 query
.setParameter("type", type
);
120 query
.setParameter("name",name
);
121 return ((Long
)query
.uniqueResult()).intValue();
124 public int countTypeDesignations(TaxonNameBase name
, TypeDesignationStatus status
) {
127 query
= getSession().createQuery("select count(designation) from TypeDesignationBase designation join designation.typifiedNames name where name = :name");
129 query
= getSession().createQuery("select count(designation) from TypeDesignationBase designation join designation.typifiedNames name where name = :name and designation.typeStatus = :status");
130 query
.setParameter("status", status
);
132 query
.setParameter("name",name
);
133 return ((Long
)query
.uniqueResult()).intValue();
136 public List
<HybridRelationship
> getHybridNames(BotanicalName name
, HybridRelationshipType type
, Integer pageSize
, Integer pageNumber
) {
139 query
= getSession().createQuery("select relation from HybridRelationship relation where relation.relatedFrom = :name");
141 query
= getSession().createQuery("select relation from HybridRelationship relation where relation.relatedFrom = :name and relation.type = :type");
142 query
.setParameter("type", type
);
144 query
.setParameter("name",name
);
146 if(pageSize
!= null) {
147 query
.setMaxResults(pageSize
);
148 if(pageNumber
!= null) {
149 query
.setFirstResult(pageNumber
* pageSize
);
151 query
.setFirstResult(0);
154 return (List
<HybridRelationship
>)query
.list();
157 public List
<NameRelationship
> getRelatedNames(TaxonNameBase name
, NameRelationshipType type
, Integer pageSize
, Integer pageNumber
) {
160 query
= getSession().createQuery("select relation from NameRelationship relation where relation.relatedFrom = :name");
162 query
= getSession().createQuery("select relation from NameRelationship relation where relation.relatedFrom = :name and relation.type = :type");
163 query
.setParameter("type", type
);
165 query
.setParameter("name",name
);
167 if(pageSize
!= null) {
168 query
.setMaxResults(pageSize
);
169 if(pageNumber
!= null) {
170 query
.setFirstResult(pageNumber
* pageSize
);
172 query
.setFirstResult(0);
175 return (List
<NameRelationship
>)query
.list();
178 public List
<TypeDesignationBase
> getTypeDesignations(TaxonNameBase name
, TypeDesignationStatus status
, Integer pageSize
, Integer pageNumber
) {
181 query
= getSession().createQuery("select designation from TypeDesignationBase designation join designation.typifiedNames name where name = :name");
183 query
= getSession().createQuery("select designation from TypeDesignationBase designation join designation.typifiedNames name where name = :name and designation.typeStatus = :status");
184 query
.setParameter("status", status
);
186 query
.setParameter("name",name
);
188 if(pageSize
!= null) {
189 query
.setMaxResults(pageSize
);
190 if(pageNumber
!= null) {
191 query
.setFirstResult(pageNumber
* pageSize
);
193 query
.setFirstResult(0);
196 return (List
<TypeDesignationBase
>)query
.list();
200 public List
<TaxonNameBase
<?
,?
>> searchNames(String queryString
, Integer pageSize
, Integer pageNumber
) {
202 Criteria criteria
= getSession().createCriteria(TaxonNameBase
.class);
204 if (queryString
!= null) {
205 criteria
.add(Restrictions
.ilike("nameCache", queryString
));
207 if(pageSize
!= null) {
208 criteria
.setMaxResults(pageSize
);
209 if(pageNumber
!= null) {
210 criteria
.setFirstResult(pageNumber
* pageSize
);
212 criteria
.setFirstResult(0);
215 List
<TaxonNameBase
<?
,?
>> results
= criteria
.list();
220 public List
<TaxonNameBase
> searchNames(String genusOrUninomial
,String infraGenericEpithet
, String specificEpithet
, String infraSpecificEpithet
, Rank rank
, Integer pageSize
,Integer pageNumber
) {
221 Criteria criteria
= getSession().createCriteria(TaxonNameBase
.class);
224 * Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
225 * We need to convert nulls to NullRestrictions for now
227 if(genusOrUninomial
!= null) {
228 criteria
.add(Restrictions
.eq("genusOrUninomial",genusOrUninomial
));
230 criteria
.add(Restrictions
.isNull("genusOrUninomial"));
233 if(infraGenericEpithet
!= null) {
234 criteria
.add(Restrictions
.eq("infraGenericEpithet", infraGenericEpithet
));
236 criteria
.add(Restrictions
.isNull("infraGenericEpithet"));
239 if(specificEpithet
!= null) {
240 criteria
.add(Restrictions
.eq("specificEpithet", specificEpithet
));
242 criteria
.add(Restrictions
.isNull("specificEpithet"));
245 if(infraSpecificEpithet
!= null) {
246 criteria
.add(Restrictions
.eq("infraSpecificEpithet",infraSpecificEpithet
));
248 criteria
.add(Restrictions
.isNull("infraSpecificEpithet"));
250 criteria
.add(Restrictions
.eq("rank", rank
));
252 if(pageSize
!= null) {
253 criteria
.setMaxResults(pageSize
);
254 if(pageNumber
!= null) {
255 criteria
.setFirstResult(pageNumber
* pageSize
);
257 criteria
.setFirstResult(0);
261 return (List
<TaxonNameBase
>)criteria
.list();