3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.taxon
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.Query
;
19 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
20 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
21 import org
.springframework
.stereotype
.Repository
;
23 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
27 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.IClassificationDao
;
29 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonNodeDao
;
37 @Qualifier("classificationDaoHibernateImpl")
38 public class ClassificationDaoHibernateImpl
extends IdentifiableDaoBase
<Classification
>
39 implements IClassificationDao
{
40 @SuppressWarnings("unused")
41 private static final Logger logger
= Logger
.getLogger(ClassificationDaoHibernateImpl
.class);
44 private ITaxonNodeDao taxonNodeDao
;
46 public ClassificationDaoHibernateImpl() {
47 super(Classification
.class);
48 indexedClasses
= new Class
[1];
49 indexedClasses
[0] = Classification
.class;
53 @SuppressWarnings("unchecked")
54 public List
<TaxonNode
> listRankSpecificRootNodes(Classification classification
, Rank rank
,
55 Integer limit
, Integer start
, List
<String
> propertyPaths
){
57 Query query
= prepareRankSpecificRootNodes(classification
, rank
, false);
60 query
.setMaxResults(limit
);
62 query
.setFirstResult(start
);
66 List
<TaxonNode
> results
= query
.list();
67 defaultBeanInitializer
.initializeAll(results
, propertyPaths
);
73 public long countRankSpecificRootNodes(Classification classification
, Rank rank
) {
75 Query query
= prepareRankSpecificRootNodes(classification
, rank
, true);
76 return (Long
)query
.uniqueResult();
80 * @param classification
84 private Query
prepareRankSpecificRootNodes(Classification classification
, Rank rank
, boolean doCount
) {
87 String whereClassification
= "";
88 if (classification
!= null){
89 whereClassification
= " AND tn.classification = :classification ";
92 String selectWhat
= doCount ?
"count(distinct tn)" : "distinct tn";
95 String hql
= "SELECT " + selectWhat
+ " FROM TaxonNode tn LEFT JOIN tn.childNodes as tnc" +
96 " WHERE tn.parent.parent = null " +
98 query
= getSession().createQuery(hql
);
100 String hql
= "SELECT " + selectWhat
+ " FROM TaxonNode tn LEFT JOIN tn.childNodes as tnc" +
102 " (tn.taxon.name.rank = :rank" +
103 " OR (tn.taxon.name.rank.orderIndex > :rankOrderIndex AND tn.parent.parent = null)" +
104 " OR (tn.taxon.name.rank.orderIndex < :rankOrderIndex AND tnc.taxon.name.rank.orderIndex > :rankOrderIndex)" +
107 query
= getSession().createQuery(hql
);
108 query
.setParameter("rank", rank
);
109 query
.setParameter("rankOrderIndex", rank
.getOrderIndex());
112 if (classification
!= null){
113 query
.setParameter("classification", classification
);
119 public List
<TaxonNode
> listChildrenOf(Taxon taxon
, Classification classification
, Integer pageSize
, Integer pageIndex
, List
<String
> propertyPaths
){
120 Query query
= prepareListChildrenOf(taxon
, classification
, false);
122 setPagingParameter(query
, pageSize
, pageIndex
);
124 @SuppressWarnings("unchecked")
125 List
<TaxonNode
> result
= query
.list();
126 //check if array is "empty" (not containing null objects)
127 if(!result
.isEmpty() && result
.iterator().next()==null){
128 return java
.util
.Collections
.emptyList();
130 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
135 public Long
countChildrenOf(Taxon taxon
, Classification classification
){
136 Query query
= prepareListChildrenOf(taxon
, classification
, true);
137 Long count
= (Long
) query
.uniqueResult();
141 private Query
prepareListChildrenOf(Taxon taxon
, Classification classification
, boolean doCount
){
143 String selectWhat
= doCount ?
"count(cn)" : "cn";
145 String hql
= "select " + selectWhat
+ " from TaxonNode as tn left join tn.classification as c left join tn.taxon as t left join tn.childNodes as cn "
146 + "where t = :taxon and c = :classification";
148 Query query
= getSession().createQuery(hql
);
149 query
.setParameter("taxon", taxon
);
150 query
.setParameter("classification", classification
);
156 public UUID
delete(Classification persistentObject
){
157 //delete all childnodes, then delete the tree
159 List
<TaxonNode
> nodes
= persistentObject
.getChildNodes();
160 List
<TaxonNode
> nodesTmp
= new ArrayList
<TaxonNode
>(nodes
);
161 // Iterator<TaxonNode> nodesIterator = nodes.iterator();
164 for(TaxonNode node
: nodesTmp
){
166 persistentObject
.deleteChildNode(node
, true);
167 taxonNodeDao
.delete(node
, true);
172 TaxonNode rootNode
= persistentObject
.getRootNode();
173 persistentObject
.removeRootNode();
174 taxonNodeDao
.delete(rootNode
);
175 super.delete(persistentObject
);
177 return persistentObject
.getUuid();