- show infraspecific taxa in Palmweb theme only if there are any
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / hibernate / taxon / ClassificationDaoHibernateImpl.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.persistence.dao.hibernate.taxon;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.UUID;
16
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;
22
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;
30
31 /**
32 * @author a.mueller
33 * @created 16.06.2009
34 * @version 1.0
35 */
36 @Repository
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);
42
43 @Autowired
44 private ITaxonNodeDao taxonNodeDao;
45
46 public ClassificationDaoHibernateImpl() {
47 super(Classification.class);
48 indexedClasses = new Class[1];
49 indexedClasses[0] = Classification.class;
50 }
51
52 @Override
53 @SuppressWarnings("unchecked")
54 public List<TaxonNode> listRankSpecificRootNodes(Classification classification, Rank rank,
55 Integer limit, Integer start, List<String> propertyPaths){
56
57 Query query = prepareRankSpecificRootNodes(classification, rank, false);
58
59 if(limit != null) {
60 query.setMaxResults(limit);
61 if(start != null) {
62 query.setFirstResult(start);
63 }
64 }
65
66 List<TaxonNode> results = query.list();
67 defaultBeanInitializer.initializeAll(results, propertyPaths);
68 return results;
69
70 }
71
72 @Override
73 public long countRankSpecificRootNodes(Classification classification, Rank rank) {
74
75 Query query = prepareRankSpecificRootNodes(classification, rank, true);
76 return (Long)query.uniqueResult();
77 }
78
79 /**
80 * @param classification
81 * @param rank
82 * @return
83 */
84 private Query prepareRankSpecificRootNodes(Classification classification, Rank rank, boolean doCount) {
85 Query query;
86
87 String whereClassification = "";
88 if (classification != null){
89 whereClassification = " AND tn.classification = :classification ";
90 }
91
92 String selectWhat = doCount ? "count(distinct tn)" : "distinct tn";
93
94 if(rank == null){
95 String hql = "SELECT " + selectWhat + " FROM TaxonNode tn LEFT JOIN tn.childNodes as tnc" +
96 " WHERE tn.parent.parent = null " +
97 whereClassification;
98 query = getSession().createQuery(hql);
99 } else {
100 String hql = "SELECT " + selectWhat + " FROM TaxonNode tn LEFT JOIN tn.childNodes as tnc" +
101 " WHERE " +
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)" +
105 " )" +
106 whereClassification;
107 query = getSession().createQuery(hql);
108 query.setParameter("rank", rank);
109 query.setParameter("rankOrderIndex", rank.getOrderIndex());
110 }
111
112 if (classification != null){
113 query.setParameter("classification", classification);
114 }
115 return query;
116 }
117
118 @Override
119 public List<TaxonNode> listChildrenOf(Taxon taxon, Classification classification, Integer pageSize, Integer pageIndex, List<String> propertyPaths){
120 Query query = prepareListChildrenOf(taxon, classification, false);
121
122 setPagingParameter(query, pageSize, pageIndex);
123
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();
129 }
130 defaultBeanInitializer.initializeAll(result, propertyPaths);
131 return result;
132 }
133
134 @Override
135 public Long countChildrenOf(Taxon taxon, Classification classification){
136 Query query = prepareListChildrenOf(taxon, classification, true);
137 Long count = (Long) query.uniqueResult();
138 return count;
139 }
140
141 private Query prepareListChildrenOf(Taxon taxon, Classification classification, boolean doCount){
142
143 String selectWhat = doCount ? "count(cn)" : "cn";
144
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";
147
148 Query query = getSession().createQuery(hql);
149 query.setParameter("taxon", taxon);
150 query.setParameter("classification", classification);
151
152 return query;
153 }
154
155 @Override
156 public UUID delete(Classification persistentObject){
157 //delete all childnodes, then delete the tree
158
159 List<TaxonNode> nodes = persistentObject.getChildNodes();
160 List<TaxonNode> nodesTmp = new ArrayList<TaxonNode>(nodes);
161 // Iterator<TaxonNode> nodesIterator = nodes.iterator();
162
163
164 for(TaxonNode node : nodesTmp){
165
166 persistentObject.deleteChildNode(node, true);
167 taxonNodeDao.delete(node, true);
168
169
170 }
171
172 TaxonNode rootNode = persistentObject.getRootNode();
173 persistentObject.removeRootNode();
174 taxonNodeDao.delete(rootNode);
175 super.delete(persistentObject);
176
177 return persistentObject.getUuid();
178 }
179
180
181
182 }