06fa0679af5c09bad2a8617b3889f6b2f1fff6ea
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / hibernate / name / TaxonNameDaoHibernateImpl.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.name;
11
12 import java.util.List;
13
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;
21
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;
35
36 /**
37 * @author a.mueller
38 *
39 */
40 @Repository
41 @Qualifier("taxonNameDaoHibernateImpl")
42 public class TaxonNameDaoHibernateImpl
43 extends IdentifiableDaoBase<TaxonNameBase> implements ITaxonNameDao {
44
45 @SuppressWarnings("unused")
46 private static final Logger logger = Logger.getLogger(TaxonNameDaoHibernateImpl.class);
47
48 public TaxonNameDaoHibernateImpl() {
49 super(TaxonNameBase.class);
50 }
51
52 public int countHybridNames(BotanicalName name, HybridRelationshipType type) {
53 Query query = null;
54 if(type == null) {
55 query = getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name");
56 } else {
57 query = getSession().createQuery("select count(relation) from HybridRelationship relation where relation.relatedFrom = :name and relation.type = :type");
58 query.setParameter("type", type);
59 }
60 query.setParameter("name",name);
61 return ((Long)query.uniqueResult()).intValue();
62 }
63
64 public int countNames(String queryString) {
65
66 Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
67
68 if (queryString != null) {
69 criteria.add(Restrictions.ilike("nameCache", queryString));
70 }
71 criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));
72
73 return (Integer)criteria.uniqueResult();
74 }
75
76 public int countNames(String genusOrUninomial, String infraGenericEpithet, String specificEpithet, String infraSpecificEpithet, Rank rank) {
77 Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
78
79 /**
80 * Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
81 * We need to convert nulls to NullRestrictions for now
82 */
83 if(genusOrUninomial != null) {
84 criteria.add(Restrictions.eq("genusOrUninomial",genusOrUninomial));
85 } else {
86 criteria.add(Restrictions.isNull("genusOrUninomial"));
87 }
88
89 if(infraGenericEpithet != null) {
90 criteria.add(Restrictions.eq("infraGenericEpithet", infraGenericEpithet));
91 } else {
92 criteria.add(Restrictions.isNull("infraGenericEpithet"));
93 }
94
95 if(specificEpithet != null) {
96 criteria.add(Restrictions.eq("specificEpithet", specificEpithet));
97 } else {
98 criteria.add(Restrictions.isNull("specificEpithet"));
99 }
100
101 if(infraSpecificEpithet != null) {
102 criteria.add(Restrictions.eq("infraSpecificEpithet",infraSpecificEpithet));
103 } else {
104 criteria.add(Restrictions.isNull("infraSpecificEpithet"));
105 }
106 criteria.add(Restrictions.eq("rank", rank));
107
108 criteria.setProjection(Projections.rowCount());
109 return (Integer)criteria.uniqueResult();
110 }
111
112 public int countRelatedNames(TaxonNameBase name, NameRelationshipType type) {
113 Query query = null;
114 if(type == null) {
115 query = getSession().createQuery("select count(relation) from NameRelationship relation where relation.relatedFrom = :name");
116 } else {
117 query = getSession().createQuery("select count(relation) from NameRelationship relation where relation.relatedFrom = :name and relation.type = :type");
118 query.setParameter("type", type);
119 }
120 query.setParameter("name",name);
121 return ((Long)query.uniqueResult()).intValue();
122 }
123
124 public int countTypeDesignations(TaxonNameBase name, TypeDesignationStatus status) {
125 Query query = null;
126 if(status == null) {
127 query = getSession().createQuery("select count(designation) from TypeDesignationBase designation join designation.typifiedNames name where name = :name");
128 } else {
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);
131 }
132 query.setParameter("name",name);
133 return ((Long)query.uniqueResult()).intValue();
134 }
135
136 public List<HybridRelationship> getHybridNames(BotanicalName name, HybridRelationshipType type, Integer pageSize, Integer pageNumber) {
137 Query query = null;
138 if(type == null) {
139 query = getSession().createQuery("select relation from HybridRelationship relation where relation.relatedFrom = :name");
140 } else {
141 query = getSession().createQuery("select relation from HybridRelationship relation where relation.relatedFrom = :name and relation.type = :type");
142 query.setParameter("type", type);
143 }
144 query.setParameter("name",name);
145
146 if(pageSize != null) {
147 query.setMaxResults(pageSize);
148 if(pageNumber != null) {
149 query.setFirstResult(pageNumber * pageSize);
150 } else {
151 query.setFirstResult(0);
152 }
153 }
154 return (List<HybridRelationship>)query.list();
155 }
156
157 public List<NameRelationship> getRelatedNames(TaxonNameBase name, NameRelationshipType type, Integer pageSize, Integer pageNumber) {
158 Query query = null;
159 if(type == null) {
160 query = getSession().createQuery("select relation from NameRelationship relation where relation.relatedFrom = :name");
161 } else {
162 query = getSession().createQuery("select relation from NameRelationship relation where relation.relatedFrom = :name and relation.type = :type");
163 query.setParameter("type", type);
164 }
165 query.setParameter("name",name);
166
167 if(pageSize != null) {
168 query.setMaxResults(pageSize);
169 if(pageNumber != null) {
170 query.setFirstResult(pageNumber * pageSize);
171 } else {
172 query.setFirstResult(0);
173 }
174 }
175 return (List<NameRelationship>)query.list();
176 }
177
178 public List<TypeDesignationBase> getTypeDesignations(TaxonNameBase name, TypeDesignationStatus status, Integer pageSize, Integer pageNumber) {
179 Query query = null;
180 if(status == null) {
181 query = getSession().createQuery("select designation from TypeDesignationBase designation join designation.typifiedNames name where name = :name");
182 } else {
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);
185 }
186 query.setParameter("name",name);
187
188 if(pageSize != null) {
189 query.setMaxResults(pageSize);
190 if(pageNumber != null) {
191 query.setFirstResult(pageNumber * pageSize);
192 } else {
193 query.setFirstResult(0);
194 }
195 }
196 return (List<TypeDesignationBase>)query.list();
197 }
198
199
200 public List<TaxonNameBase<?,?>> searchNames(String queryString, Integer pageSize, Integer pageNumber) {
201
202 Criteria criteria = getSession().createCriteria(TaxonNameBase.class);
203
204 if (queryString != null) {
205 criteria.add(Restrictions.ilike("nameCache", queryString));
206 }
207 if(pageSize != null) {
208 criteria.setMaxResults(pageSize);
209 if(pageNumber != null) {
210 criteria.setFirstResult(pageNumber * pageSize);
211 } else {
212 criteria.setFirstResult(0);
213 }
214 }
215 List<TaxonNameBase<?,?>> results = criteria.list();
216 return results;
217 }
218
219
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);
222
223 /**
224 * Given HHH-2951 - "Restrictions.eq when passed null, should create a NullRestriction"
225 * We need to convert nulls to NullRestrictions for now
226 */
227 if(genusOrUninomial != null) {
228 criteria.add(Restrictions.eq("genusOrUninomial",genusOrUninomial));
229 } else {
230 criteria.add(Restrictions.isNull("genusOrUninomial"));
231 }
232
233 if(infraGenericEpithet != null) {
234 criteria.add(Restrictions.eq("infraGenericEpithet", infraGenericEpithet));
235 } else {
236 criteria.add(Restrictions.isNull("infraGenericEpithet"));
237 }
238
239 if(specificEpithet != null) {
240 criteria.add(Restrictions.eq("specificEpithet", specificEpithet));
241 } else {
242 criteria.add(Restrictions.isNull("specificEpithet"));
243 }
244
245 if(infraSpecificEpithet != null) {
246 criteria.add(Restrictions.eq("infraSpecificEpithet",infraSpecificEpithet));
247 } else {
248 criteria.add(Restrictions.isNull("infraSpecificEpithet"));
249 }
250 criteria.add(Restrictions.eq("rank", rank));
251
252 if(pageSize != null) {
253 criteria.setMaxResults(pageSize);
254 if(pageNumber != null) {
255 criteria.setFirstResult(pageNumber * pageSize);
256 } else {
257 criteria.setFirstResult(0);
258 }
259 }
260
261 return (List<TaxonNameBase>)criteria.list();
262 }
263
264 }