Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
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.math.BigInteger;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19
import org.hibernate.Criteria;
20
import org.hibernate.Query;
21
import org.hibernate.criterion.Projections;
22
import org.hibernate.criterion.Restrictions;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Qualifier;
25
import org.springframework.stereotype.Repository;
26

    
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.taxon.Classification;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
32
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
33
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao;
34
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
35
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
36

    
37
/**
38
 * @author a.mueller
39
 * @created 16.06.2009
40
 */
41
@Repository
42
@Qualifier("taxonNodeDaoHibernateImpl")
43
public class TaxonNodeDaoHibernateImpl extends AnnotatableDaoImpl<TaxonNode>
44
		implements ITaxonNodeDao {
45
	@SuppressWarnings("unused")
46
	private static final Logger logger = Logger.getLogger(TaxonNodeDaoHibernateImpl.class);
47

    
48
	@Autowired
49
	private ITaxonDao taxonDao;
50
	@Autowired
51
	private IClassificationDao classificationDao;
52

    
53
	public TaxonNodeDaoHibernateImpl() {
54
		super(TaxonNode.class);
55
	}
56
	@Override
57
	public UUID delete(TaxonNode persistentObject, boolean deleteChildren){
58
		Taxon taxon = persistentObject.getTaxon();
59
		taxon = HibernateProxyHelper.deproxy(taxon, Taxon.class);
60

    
61
		/*Session session = this.getSession();
62
		Query query = session.createQuery("from TaxonNode t where t.taxon = :taxon");
63
		query.setParameter("taxon", taxon);
64
		List result = query.list();*/
65
		if (taxon != null){
66
			Set<TaxonNode> nodes = taxon.getTaxonNodes();
67

    
68
			if (nodes.size()==1){
69

    
70
				TaxonNode node = nodes.iterator().next();
71
				node = HibernateProxyHelper.deproxy(node, TaxonNode.class);
72

    
73
				taxon.removeTaxonNode(node, deleteChildren);
74
				taxonDao.delete(taxon);
75
			}
76
		}
77
		//persistentObject.delete();
78

    
79
		super.delete(persistentObject);
80

    
81

    
82

    
83
		//taxon = (Taxon)taxonDao.findByUuid(taxon.getUuid());
84
		return persistentObject.getUuid();
85
	}
86

    
87
	@Override
88
	public List<TaxonNode> getTaxonOfAcceptedTaxaByClassification(Classification classification, Integer start, Integer end) {
89
		int classificationId = classification.getId();
90
		String limit = "";
91
		if(start !=null && end != null){
92
		    limit = "LIMIT "+start+"," +end;
93
		}
94
		//FIXME write test
95
        String queryString = "SELECT DISTINCT nodes.*,taxa.titleCache FROM TaxonNode AS nodes LEFT JOIN TaxonBase AS taxa ON nodes.taxon_id = taxa.id WHERE taxa.DTYPE = 'Taxon' AND nodes.classification_id = " + classificationId + " ORDER BY taxa.titleCache " + limit;
96
        List<TaxonNode> result  = getSession().createSQLQuery(queryString).addEntity(TaxonNode.class).list();
97

    
98
       return result;
99

    
100

    
101
	}
102
    /* (non-Javadoc)
103
     * @see eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao#countTaxonOfAcceptedTaxaByClassification(eu.etaxonomy.cdm.model.taxon.Classification)
104
     */
105
    @Override
106
    public int countTaxonOfAcceptedTaxaByClassification(Classification classification){
107
        int classificationId = classification.getId();
108
        //FIXME write test
109
        String queryString = "SELECT DISTINCT COUNT('nodes.*') FROM TaxonNode AS nodes LEFT JOIN TaxonBase AS taxa ON nodes.taxon_id = taxa.id WHERE taxa.DTYPE = 'Taxon' AND nodes.classification_id = " + classificationId;
110
         List<BigInteger> result = getSession().createSQLQuery(queryString).list();
111
         return result.get(0).intValue ();
112
    }
113

    
114
    @Override
115
    public List<TaxonNode> listChildrenOf(TaxonNode node, Integer pageSize, Integer pageIndex, List<String> propertyPaths, boolean recursive){
116
    	if (recursive == true){
117
    		Criteria crit = getSession().createCriteria(TaxonNode.class);
118
    		crit.add( Restrictions.like("treeIndex", node.treeIndex()+ "%") );
119
    		if(pageSize != null) {
120
                crit.setMaxResults(pageSize);
121
                if(pageIndex != null) {
122
                    crit.setFirstResult(pageIndex * pageSize);
123
                } else {
124
                    crit.setFirstResult(0);
125
                }
126
            }
127
    		List<TaxonNode> results = crit.list();
128
    		results.remove(node);
129
    		defaultBeanInitializer.initializeAll(results, propertyPaths);
130
    		return results;
131
    	}else{
132
    		return classificationDao.listChildrenOf(node.getTaxon(), node.getClassification(), pageSize, pageIndex, propertyPaths);
133
    	}
134

    
135
    }
136

    
137
    @Override
138
	public Long countChildrenOf(TaxonNode node, Classification classification,
139
			boolean recursive) {
140

    
141
		if (recursive == true){
142
			Criteria crit = getSession().createCriteria(TaxonNode.class);
143
    		crit.add( Restrictions.like("treeIndex", node.treeIndex()+ "%") );
144
    		crit.setProjection(Projections.rowCount());
145
    		return ((Integer)crit.uniqueResult().hashCode()).longValue();
146
		}else{
147
			return classificationDao.countChildrenOf(node.getTaxon(), classification);
148
		}
149
	}
150
    /**
151
     * {@inheritDoc}
152
     */
153
    @Override
154
    public List<TaxonNodeAgentRelation> listTaxonNodeAgentRelations(UUID taxonUuid, UUID classificationUuid,
155
            UUID agentUuid, UUID rankUuid, UUID relTypeUuid, Integer start, Integer limit, List<String> propertyPaths) {
156

    
157

    
158
        StringBuilder hql = prepareListTaxonNodeAgentRelations(taxonUuid, classificationUuid, agentUuid, rankUuid, relTypeUuid, false);
159

    
160
        Query query =  getSession().createQuery(hql.toString());
161

    
162
        if(limit != null) {
163
            query.setMaxResults(limit);
164
            if(start != null) {
165
                query.setFirstResult(start);
166
            }
167
        }
168

    
169
        setParamsForListTaxonNodeAgentRelations(taxonUuid, classificationUuid, agentUuid, rankUuid, relTypeUuid, query);
170

    
171
        List<TaxonNodeAgentRelation> records = query.list();
172

    
173
        if(propertyPaths != null) {
174
            defaultBeanInitializer.initializeAll(records, propertyPaths);
175
        }
176
        return records;
177
    }
178

    
179
    /**
180
     * {@inheritDoc}
181
     */
182
    @Override
183
    public long countTaxonNodeAgentRelations(UUID taxonUuid, UUID classificationUuid, UUID agentUuid, UUID rankUuid, UUID relTypeUuid) {
184

    
185
        StringBuilder hql = prepareListTaxonNodeAgentRelations(taxonUuid, classificationUuid, agentUuid, rankUuid, relTypeUuid, true);
186
        Query query =  getSession().createQuery(hql.toString());
187

    
188
        setParamsForListTaxonNodeAgentRelations(taxonUuid, classificationUuid, agentUuid, rankUuid, relTypeUuid, query);
189

    
190
        Long count = Long.parseLong(query.uniqueResult().toString());
191

    
192
        return count;
193
    }
194
    /**
195
     * @param taxonUuid
196
     * @param classificationUuid
197
     * @param agentUuid
198
     * @param relTypeUuid TODO
199
     * @param doCount TODO
200
     * @param rankId
201
     *     limit to taxa having this rank, only applies if <code>taxonUuid = null</code>
202
     * @return
203
     */
204
    private StringBuilder prepareListTaxonNodeAgentRelations(UUID taxonUuid, UUID classificationUuid, UUID agentUuid, UUID rankUuid, UUID relTypeUuid, boolean doCount) {
205

    
206
        StringBuilder hql = new StringBuilder();
207

    
208
        String join_fetch_mode = doCount ? "join" : "join fetch";
209

    
210
        if(doCount) {
211
            hql.append("select count(tnar)");
212
        } else {
213
            hql.append("select tnar");
214
        }
215

    
216
        hql.append(" from TaxonNodeAgentRelation as tnar ");
217
        if(taxonUuid != null) {
218
            // taxonUuid is search filter, do not fetch it
219
            hql.append(" join tnar.taxonNode as tn join tn.taxon as t ");
220
        } else {
221
            hql.append(join_fetch_mode).append(" tnar.taxonNode as tn ").append(join_fetch_mode).append(" tn.taxon as t ");
222
            if(rankUuid != null) {
223
                hql.append(" join t.name as n ");
224
            }
225
        }
226
        hql.append(" join tn.classification as c ");
227
        if(agentUuid != null) {
228
            // agentUuid is search filter, do not fetch it
229
//            hql.append(" join tnar.agent as a ");
230
            hql.append(join_fetch_mode).append(" tnar.agent as a ");
231
        } else {
232
            hql.append(join_fetch_mode).append(" tnar.agent as a ");
233
        }
234

    
235
        hql.append(" where 1 = 1 ");
236

    
237
        if(relTypeUuid != null) {
238
            hql.append(" and tnar.type.uuid = :relTypeUuid ");
239
        }
240

    
241
        if(taxonUuid != null) {
242
            hql.append(" and t.uuid = :taxonUuid ");
243
        } else {
244
            if(rankUuid != null) {
245
                hql.append(" and n.rank.uuid = :rankUuid ");
246
            }
247
        }
248
        if(classificationUuid != null) {
249
            hql.append(" and c.uuid = :classificationUuid ");
250
        }
251
        if(agentUuid != null) {
252
            hql.append(" and a.uuid = :agentUuid ");
253
        }
254

    
255
        hql.append(" order by a.titleCache");
256
        return hql;
257
    }
258
    /**
259
     * @param taxonUuid
260
     * @param classificationUuid
261
     * @param agentUuid
262
     * @param relTypeUuid TODO
263
     * @param query
264
     * @param rankId TODO
265
     */
266
    private void setParamsForListTaxonNodeAgentRelations(UUID taxonUuid, UUID classificationUuid, UUID agentUuid,
267
            UUID rankUuid, UUID relTypeUuid, Query query) {
268

    
269
        if(taxonUuid != null) {
270
            query.setParameter("taxonUuid", taxonUuid);
271
        } else {
272
            if(rankUuid != null) {
273
                query.setParameter("rankUuid", rankUuid);
274
            }
275
        }
276
        if(classificationUuid != null) {
277
            query.setParameter("classificationUuid", classificationUuid);
278
        }
279
        if(agentUuid != null) {
280
            query.setParameter("agentUuid", agentUuid);
281
        }
282
        if(relTypeUuid != null) {
283
            query.setParameter("relTypeUuid", relTypeUuid);
284
        }
285
    }
286

    
287
}
(4-4/4)