Project

General

Profile

Download (23.4 KB) Statistics
| Branch: | Tag: | Revision:
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.description;
11

    
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18
import org.hibernate.Criteria;
19
import org.hibernate.Query;
20
import org.hibernate.criterion.ProjectionList;
21
import org.hibernate.criterion.Projections;
22
import org.hibernate.criterion.Restrictions;
23
import org.hibernate.envers.query.AuditEntity;
24
import org.hibernate.envers.query.AuditQuery;
25
import org.springframework.beans.factory.annotation.Qualifier;
26
import org.springframework.stereotype.Repository;
27

    
28
import eu.etaxonomy.cdm.model.common.LSID;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.description.DescriptionBase;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
34
import eu.etaxonomy.cdm.model.description.Scope;
35
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
36
import eu.etaxonomy.cdm.model.description.TaxonDescription;
37
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
38
import eu.etaxonomy.cdm.model.location.NamedArea;
39
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.view.AuditEvent;
42
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
43
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
44
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
45
import eu.etaxonomy.cdm.persistence.query.MatchMode;
46
import eu.etaxonomy.cdm.persistence.query.OrderHint;
47

    
48
@Repository 
49
@Qualifier("descriptionDaoImpl")
50
public class DescriptionDaoImpl extends IdentifiableDaoBase<DescriptionBase> implements IDescriptionDao{
51

    
52
	@SuppressWarnings("unused")
53
	private static final Logger logger = Logger.getLogger(DescriptionDaoImpl.class);
54

    
55
	public DescriptionDaoImpl() {
56
		super(DescriptionBase.class); 
57
		indexedClasses = new Class[3];
58
		indexedClasses[0] = TaxonDescription.class;
59
		indexedClasses[1] = TaxonNameDescription.class;
60
		indexedClasses[2] = SpecimenDescription.class;
61
	}
62

    
63
	public int countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status) {
64
		checkNotInPriorView("DescriptionDaoImpl.countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status)");
65
		Query query = null;
66
		
67
		if(status == null) {
68
			query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area where area in (:namedAreas)");
69
		} else {
70
			query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area  join element.status status where area in (:namedAreas) and status = :status");
71
			query.setParameter("status", status);
72
		}
73
		query.setParameterList("namedAreas", namedAreas);
74
		
75
		return ((Long)query.uniqueResult()).intValue();
76
	}
77

    
78
	public int countDescriptionElements(DescriptionBase description, Set<Feature> features, Class<? extends DescriptionElementBase> clazz) {
79
		AuditEvent auditEvent = getAuditEventFromContext();
80
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
81
			Criteria criteria = null;
82
			if(clazz == null) {
83
		        criteria = getSession().createCriteria(DescriptionElementBase.class);
84
			} else {
85
			    criteria = getSession().createCriteria(clazz);	
86
			}	
87
			
88
		    if(description != null) {
89
		        criteria.add(Restrictions.eq("inDescription", description));
90
		    }
91
		
92
		    if(features != null && !features.isEmpty()) {
93
			    criteria.add(Restrictions.in("feature", features));
94
		    }
95
		
96
		    criteria.setProjection(Projections.rowCount());
97
		
98
		    return (Integer)criteria.uniqueResult();
99
		} else {
100
			if(features != null && !features.isEmpty()) {
101
				Integer count = 0;
102
			    for(Feature f : features) {
103
			        AuditQuery query = null;
104
			        if(clazz == null) {
105
			        	query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
106
			        } else {
107
			        	query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
108
			        }
109
			    
110
			        if(description != null) {
111
			    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
112
			        }
113
			     
114
			        query.add(AuditEntity.relatedId("feature").eq(f.getId()));
115
			        query.addProjection(AuditEntity.id().count("id"));
116
			        count += ((Long)query.getSingleResult()).intValue();
117
			    }
118
			    
119
			    return count;
120
			} else {
121
				AuditQuery query = null;
122
		        if(clazz == null) {
123
		        	query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
124
		        } else {
125
		        	query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
126
		        }
127
			    
128
		        if(description != null) {
129
		    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
130
		        }
131
		        query.addProjection(AuditEntity.id().count("id"));
132
		        return ((Long)query.getSingleResult()).intValue();
133
			}
134
		}
135
	}
136

    
137
	public int countDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText, Set<Feature> features) {
138
		checkNotInPriorView("DescriptionDaoImpl.countDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText, Set<Feature> features)");
139
		Criteria inner = null;
140
		
141
		if(clazz == null) {
142
			inner = getSession().createCriteria(type);
143
		} else {
144
			inner = getSession().createCriteria(clazz);
145
		}
146
		
147
		Criteria elementsCriteria = inner.createCriteria("descriptionElements");
148
		if(hasText != null) {
149
			if(hasText) {
150
				elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
151
			} else {
152
				elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
153
			}
154
		}
155
		
156
		if(hasImages != null) {
157
			if(hasImages) {
158
				elementsCriteria.add(Restrictions.isNotEmpty("media"));
159
			} else {
160
				elementsCriteria.add(Restrictions.isEmpty("media"));
161
			}
162
		}
163
		
164
		if(features != null && !features.isEmpty()) {
165
			elementsCriteria.add(Restrictions.in("feature", features));
166
		}
167
		
168
		inner.setProjection(Projections.countDistinct("id"));
169

    
170
		return (Integer) inner.uniqueResult();
171
	}
172

    
173
	public int countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes) {
174
		AuditEvent auditEvent = getAuditEventFromContext();
175
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
176
			Criteria criteria = getSession().createCriteria(TaxonDescription.class);
177

    
178
			if(taxon != null) {
179
				criteria.add(Restrictions.eq("taxon", taxon));
180
			}
181

    
182
			if(scopes != null && !scopes.isEmpty()) {
183
				Set<Integer> scopeIds = new HashSet<Integer>();
184
				for(Scope s : scopes) {
185
					scopeIds.add(s.getId());
186
				}
187
				criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
188
			}
189

    
190
			if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
191
				Set<Integer> geoScopeIds = new HashSet<Integer>();
192
				for(NamedArea n : geographicalScopes) {
193
					geoScopeIds.add(n.getId());
194
				}
195
				criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
196
			}
197

    
198
			criteria.setProjection(Projections.rowCount());
199

    
200
			return (Integer)criteria.uniqueResult();
201
		} else {
202
			if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty())) {
203
				AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
204
				if(taxon != null) {
205
				    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
206
				}
207
				
208
				query.addProjection(AuditEntity.id().count("id"));
209
				
210
				return ((Long)query.getSingleResult()).intValue();
211
			} else {
212
				throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
213
			}
214
		}
215
	}
216

    
217
	public List<DescriptionElementBase> getDescriptionElements(DescriptionBase description, Set<Feature> features,Class<? extends DescriptionElementBase> clazz, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
218

    
219
		AuditEvent auditEvent = getAuditEventFromContext();
220
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
221
            Criteria criteria = null;
222
            if(clazz == null) {
223
            	criteria = getSession().createCriteria(DescriptionElementBase.class);
224
            } else {
225
            	criteria = getSession().createCriteria(clazz);
226
            }
227
		
228
            if(description != null) {
229
		        criteria.add(Restrictions.eq("inDescription", description));
230
		    }
231
		
232
		    if(features != null && !features.isEmpty()) {
233
			    criteria.add(Restrictions.in("feature", features));
234
		    }
235
		
236
		    if(pageSize != null) {
237
			    criteria.setMaxResults(pageSize);
238
		        if(pageNumber != null) {
239
		    	    criteria.setFirstResult(pageNumber * pageSize);
240
		        }
241
		    }
242
		    
243
		    List<DescriptionElementBase> results = (List<DescriptionElementBase>)criteria.list();
244
		    
245
		    defaultBeanInitializer.initializeAll(results, propertyPaths);
246
		
247
	    	return results; 
248
		} else {
249
			List<DescriptionElementBase> result = new ArrayList<DescriptionElementBase>();
250
			if(features != null && !features.isEmpty()) {
251
				
252
			    for(Feature f : features) {
253
			    	AuditQuery query = null;
254
			    	if(clazz == null) {
255
			            query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
256
			    	} else {
257
			    		query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
258
			    	}
259
			    	
260
			        if(description != null) {
261
			    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
262
			        }
263
			     
264
			        query.add(AuditEntity.relatedId("feature").eq(f.getId()));
265
			        result.addAll((List<DescriptionElementBase>)query.getResultList());
266
			    }
267
			} else {
268
				AuditQuery query = null;
269
		    	if(clazz == null) {
270
		            query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
271
		    	} else {
272
		    		query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
273
		    	}
274
			    
275
		        if(description != null) {
276
		    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
277
		        }
278
		        
279
		        result = query.getResultList();
280
			}
281
			
282
			defaultBeanInitializer.initializeAll(result, propertyPaths);
283
			
284
			return result;
285
		}
286
	}
287

    
288
	public List<TaxonDescription> getTaxonDescriptions(Taxon taxon,	Set<Scope> scopes, Set<NamedArea> geographicalScopes,Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
289
		AuditEvent auditEvent = getAuditEventFromContext();
290
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
291
			Criteria criteria = getSession().createCriteria(TaxonDescription.class);
292

    
293
			if(taxon != null) {
294
				criteria.add(Restrictions.eq("taxon", taxon));
295
			}
296

    
297
			if(scopes != null && !scopes.isEmpty()) {
298
				Set<Integer> scopeIds = new HashSet<Integer>();
299
				for(Scope s : scopes) {
300
					scopeIds.add(s.getId());
301
				}
302
				criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
303
			}
304

    
305
			if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
306
				Set<Integer> geoScopeIds = new HashSet<Integer>();
307
				for(NamedArea n : geographicalScopes) {
308
					geoScopeIds.add(n.getId());
309
				}
310
				criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
311
			}
312

    
313
			if(pageSize != null) {
314
				criteria.setMaxResults(pageSize);
315
				if(pageNumber != null) {
316
					criteria.setFirstResult(pageNumber * pageSize);
317
				}
318
			}
319

    
320
			List<TaxonDescription> results = (List<TaxonDescription>)criteria.list();
321

    
322
			defaultBeanInitializer.initializeAll(results, propertyPaths);
323

    
324
			return results;
325
		} else {
326
			if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty())) {
327
				AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
328
				if(taxon != null) {
329
				    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
330
				}
331
				
332
				if(pageSize != null) {
333
			        query.setMaxResults(pageSize);
334
			        if(pageNumber != null) {
335
			            query.setFirstResult(pageNumber * pageSize);
336
			        } else {
337
			    	    query.setFirstResult(0);
338
			        }
339
			    }
340
				
341
				List<TaxonDescription> results = (List<TaxonDescription>)query.getResultList();
342
				defaultBeanInitializer.initializeAll(results, propertyPaths);
343
				return results;
344
			} else {
345
				throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
346
			}
347
		}
348
	}
349
	
350
    public List<TaxonNameDescription> getTaxonNameDescriptions(TaxonNameBase name, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
351
        AuditEvent auditEvent = getAuditEventFromContext();
352
	    if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
353
	        Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
354
	  
355
	      if(name != null) {
356
		      criteria.add(Restrictions.eq("taxonName", name));
357
	      }
358
	  
359
	      if(pageSize != null) {
360
			  criteria.setMaxResults(pageSize);
361
		      if(pageNumber != null) {
362
		    	  criteria.setFirstResult(pageNumber * pageSize);
363
		      }
364
	      }
365
	  
366
	      List<TaxonNameDescription> results = (List<TaxonNameDescription>)criteria.list();
367
	      
368
	      defaultBeanInitializer.initializeAll(results, propertyPaths);
369
	      
370
	      return results; 
371
	    } else {
372
	    	AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
373
	    	
374
	    	if(name != null) {
375
			    query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
376
		    }
377
	    	
378
	    	if(pageSize != null) {
379
				  query.setMaxResults(pageSize);
380
			      if(pageNumber != null) {
381
			    	  query.setFirstResult(pageNumber * pageSize);
382
			      }
383
		    }
384
	    	
385
	    	List<TaxonNameDescription> results = (List<TaxonNameDescription>)query.getResultList();
386
	    	
387
	    	defaultBeanInitializer.initializeAll(results, propertyPaths);
388
	    	
389
	    	return results;
390
	    }
391
	  
392
    }
393
	
394
	public int countTaxonNameDescriptions(TaxonNameBase name) {
395
		AuditEvent auditEvent = getAuditEventFromContext();
396
	    if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
397
		    Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
398
		  
399
		    if(name != null) {
400
			    criteria.add(Restrictions.eq("taxonName", name));
401
		    }
402
		  
403
		    criteria.setProjection(Projections.rowCount());
404
		  
405
		    return (Integer)criteria.uniqueResult();
406
	    } else {
407
            AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
408
	    	
409
	    	if(name != null) {
410
			    query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
411
		    }
412
	    	
413
	    	query.addProjection(AuditEntity.id().count("id"));
414
	    	return ((Long)query.getSingleResult()).intValue();
415
	    }
416
	}
417

    
418
	/**
419
	 * Should use a DetachedCriteria & subquery, but HHH-158 prevents this, for now.
420
	 * 
421
	 * e.g. DetachedCriteria inner = DestachedCriteria.forClass(type);
422
	 * 
423
	 * outer.add(Subqueries.propertyIn("id", inner));
424
	 */
425
	public List<DescriptionBase> listDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
426
		checkNotInPriorView("DescriptionDaoImpl.listDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber)");
427
		Criteria inner = null;
428
		
429
		if(clazz == null) {
430
			inner = getSession().createCriteria(type);
431
		} else {
432
			inner = getSession().createCriteria(clazz);
433
		}
434
		
435
		Criteria elementsCriteria = inner.createCriteria("descriptionElements");
436
		if(hasText != null) {
437
			if(hasText) {
438
				elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
439
			} else {
440
				elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
441
			}
442
		}
443
		
444
		if(hasImages != null) {
445
			if(hasImages) {
446
				elementsCriteria.add(Restrictions.isNotEmpty("media"));
447
			} else {
448
				elementsCriteria.add(Restrictions.isEmpty("media"));
449
			}
450
		}
451
		
452
		if(features != null && !features.isEmpty()) {
453
			elementsCriteria.add(Restrictions.in("feature", features));
454
		}
455
		
456
		inner.setProjection(Projections.distinct(Projections.id()));
457
		
458
		List<Object> intermediateResult = (List<Object>)inner.list();
459
		
460
		if(intermediateResult.isEmpty()) {
461
			return new ArrayList<DescriptionBase>();
462
		}
463
		
464
		Integer[] resultIds = new Integer[intermediateResult.size()];
465
		for(int i = 0; i < resultIds.length; i++) {	
466
				resultIds[i] = (Integer)intermediateResult.get(i);
467
		}
468
		
469
		Criteria outer = null;
470
		
471
		if(clazz == null) {
472
			outer = getSession().createCriteria(type);
473
		} else {
474
			outer = getSession().createCriteria(clazz);
475
		}
476
		
477
		outer.add(Restrictions.in("id", resultIds));
478
		addOrder(outer, orderHints);
479
		
480
		if(pageSize != null) {
481
			outer.setMaxResults(pageSize);
482
		    if(pageNumber != null) {
483
		    	outer.setFirstResult(pageNumber * pageSize);
484
		    }
485
		}
486
		
487
		List<DescriptionBase> results = (List<DescriptionBase>)outer.list();
488
		defaultBeanInitializer.initializeAll(results, propertyPaths);
489
		return results;
490
	}
491

    
492
	public List<TaxonDescription> searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
493
		checkNotInPriorView("DescriptionDaoImpl.searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
494
        
495
        Criteria criteria = getSession().createCriteria(TaxonDescription.class);
496
        Criteria elements = criteria.createCriteria("descriptionElements", "descriptionElement", Criteria.LEFT_JOIN);
497
		elements.add(Restrictions.in("area", namedAreas.toArray()));
498
		
499
		if(status != null) {
500
			elements.add(Restrictions.eq("status", status));
501
		}
502
		
503
		ProjectionList projectionList = Projections.projectionList().add(Projections.id());
504
		
505
		if(orderHints != null && !orderHints.isEmpty()) {
506
		    for(OrderHint orderHint : orderHints) {
507
			    projectionList = projectionList.add(Projections.property(orderHint.getPropertyName()));
508
		    }
509
		}
510
		
511
		criteria.setProjection(Projections.distinct(projectionList));	
512
		
513
		if(pageSize != null) {
514
			criteria.setMaxResults(pageSize);
515
		    if(pageNumber != null) {
516
		    	criteria.setFirstResult(pageNumber * pageSize);
517
		    }
518
		}
519
		
520
		addOrder(criteria,orderHints);
521
		
522
		List<Object> intermediateResult = (List<Object>)criteria.list();
523
		
524
		if(intermediateResult.isEmpty())
525
			return new ArrayList<TaxonDescription>();
526
		
527
		Integer[] resultIds = new Integer[intermediateResult.size()];
528
		for(int i = 0; i < resultIds.length; i++) {
529
			if(orderHints == null || orderHints.isEmpty()) {
530
				resultIds[i] = (Integer)intermediateResult.get(i);
531
			} else {
532
			  resultIds[i] = (Integer)((Object[])intermediateResult.get(i))[0];
533
			}
534
		}
535
		
536
		criteria = getSession().createCriteria(TaxonDescription.class);
537
		criteria.add(Restrictions.in("id", resultIds));
538
		addOrder(criteria,orderHints);
539
		
540
		List<TaxonDescription> results = (List<TaxonDescription>)criteria.list();
541
		defaultBeanInitializer.initializeAll(results, propertyPaths);
542
		return results;
543
	}
544
	
545
	public List<CommonTaxonName> searchDescriptionByCommonName(String queryString, MatchMode matchMode, Integer pageSize, Integer pageNumber) {
546
		
547
		Criteria crit = getSession().createCriteria(CommonTaxonName.class); 
548
		if (matchMode == MatchMode.EXACT) { 
549
			crit.add(Restrictions.eq("name", matchMode.queryStringFrom(queryString))); 
550
		} else { 
551
			crit.add(Restrictions.ilike("name", matchMode.queryStringFrom(queryString))); 
552
		} 
553

    
554
		if(pageSize != null) {
555
			crit.setMaxResults(pageSize); 
556
			if(pageNumber != null) {
557
				crit.setFirstResult(pageNumber * pageSize);
558
			}
559
		}
560
		List<CommonTaxonName> results = (List<CommonTaxonName>)crit.list();
561
		return results;
562
	}
563

    
564
	public Integer countDescriptionByCommonName(String queryString, MatchMode matchMode) {
565
		//TODO inprove performance
566
		List<CommonTaxonName> results =  searchDescriptionByCommonName(queryString, matchMode, null, null);
567
		return results.size();
568
	}
569
	
570
	@Override
571
	public DescriptionBase find(LSID lsid) {
572
		DescriptionBase descriptionBase = super.find(lsid);
573
		if(descriptionBase != null) {
574
			List<String> propertyPaths = new ArrayList<String>();
575
			propertyPaths.add("createdBy");
576
			propertyPaths.add("updatedBy");
577
			propertyPaths.add("taxon");
578
			propertyPaths.add("taxonName");
579
			propertyPaths.add("descriptionElements");
580
			propertyPaths.add("descriptionElements.createdBy");
581
			propertyPaths.add("descriptionElements.updatedBy");
582
			propertyPaths.add("descriptionElements.feature");
583
			propertyPaths.add("descriptionElements.multilanguageText");
584
			propertyPaths.add("descriptionElements.multilanguageText.language");
585
			propertyPaths.add("descriptionElements.area");
586
			propertyPaths.add("descriptionElements.status");
587
			propertyPaths.add("descriptionElements.modifyingText");
588
			propertyPaths.add("descriptionElementsmodifyingText.language");
589
			propertyPaths.add("descriptionElements.modifiers");
590
			
591
			defaultBeanInitializer.initialize(descriptionBase, propertyPaths);
592
		}
593
		return descriptionBase;
594
	}
595

    
596
	public List<DescriptionElementBase> getDescriptionElementForTaxon(
597
			Taxon taxon, Set<Feature> features,
598
			Class<? extends DescriptionElementBase> type, Integer pageSize,
599
			Integer pageNumber, List<String> propertyPaths) {
600
		List<DescriptionElementBase> result = new ArrayList<DescriptionElementBase>();
601
			
602
		 Criteria criteria = null;
603
         if(type == null) {
604
         	criteria = getSession().createCriteria(DescriptionElementBase.class);
605
         } else {
606
         	criteria = getSession().createCriteria(type);
607
         }
608
		
609
         if(taxon != null) {
610
		        criteria.add(Restrictions.eq("inDescription.taxon", taxon));
611
		    }
612
		
613
		    if(features != null && !features.isEmpty()) {
614
			    criteria.add(Restrictions.in("feature", features));
615
		    }
616
		
617
		    if(pageSize != null) {
618
			    criteria.setMaxResults(pageSize);
619
		        if(pageNumber != null) {
620
		    	    criteria.setFirstResult(pageNumber * pageSize);
621
		        }
622
		    }
623
		    
624
		    List<DescriptionElementBase> results = (List<DescriptionElementBase>)criteria.list();
625
		    
626
		    defaultBeanInitializer.initializeAll(results, propertyPaths);
627
		
628
	    	return results; 
629
	}
630
}
(1-1/8)