Project

General

Profile

Download (6.38 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.api.service;
12

    
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.HashSet;
17
import java.util.List;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.stereotype.Service;
24
import org.springframework.transaction.annotation.Propagation;
25
import org.springframework.transaction.annotation.Transactional;
26

    
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.ITaxonNodeComparator;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
34
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
38
import eu.etaxonomy.cdm.persistence.dao.BeanInitializer;
39
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
40

    
41
/**
42
 * @author n.hoffmann
43
 * @created Apr 9, 2010
44
 * @version 1.0
45
 */
46
@Service
47
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
48
public class TaxonNodeServiceImpl extends AnnotatableServiceBase<TaxonNode, ITaxonNodeDao> implements ITaxonNodeService{
49
	private static final Logger logger = Logger
50
			.getLogger(TaxonNodeServiceImpl.class);
51

    
52
	@Autowired
53
	private BeanInitializer defaultBeanInitializer;
54
	
55
	private Comparator<? super TaxonNode> taxonNodeComparator;
56
	@Autowired
57
	public void setTaxonNodeComparator(ITaxonNodeComparator<? super TaxonNode> taxonNodeComparator){
58
		this.taxonNodeComparator = (Comparator<? super TaxonNode>) taxonNodeComparator;
59
	}
60

    
61
	public TaxonNode getTaxonNodeByUuid(UUID uuid) {
62
		return dao.findByUuid(uuid);
63
	}
64
	
65
	public List<TaxonNode> loadChildNodesOfTaxonNode(TaxonNode taxonNode,
66
			List<String> propertyPaths) {
67
		taxonNode = dao.load(taxonNode.getUuid());
68
		List<TaxonNode> childNodes = new ArrayList<TaxonNode>(taxonNode.getChildNodes());
69
		Collections.sort(childNodes, taxonNodeComparator);
70
		defaultBeanInitializer.initializeAll(childNodes, propertyPaths);
71
		return childNodes;
72
	}
73
	
74
	/* (non-Javadoc)
75
	 * @see eu.etaxonomy.cdm.api.service.ServiceBase#setDao(eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao)
76
	 */
77
	@Override
78
	@Autowired
79
	protected void setDao(ITaxonNodeDao dao) {
80
		this.dao = dao;
81
	}
82
	
83
	/* (non-Javadoc)
84
	 * @see eu.etaxonomy.cdm.api.service.ITaxonService#makeTaxonSynonym(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Taxon)
85
	 */
86
	@Transactional(readOnly = false)
87
	public Synonym makeTaxonNodeASynonymOfAnotherTaxonNode(TaxonNode oldTaxonNode, TaxonNode newAcceptedTaxonNode, SynonymRelationshipType synonymRelationshipType, Reference citation, String citationMicroReference) {
88

    
89
		// TODO at the moment this method only moves synonym-, concept relations and descriptions to the new accepted taxon
90
		// in a future version we also want to move cdm data like annotations, marker, so., but we will need a policy for that
91
		if (oldTaxonNode == null || newAcceptedTaxonNode == null || oldTaxonNode.getTaxon().getName() == null){
92
			throw new IllegalArgumentException("A mandatory parameter was null.");
93
		}
94
		
95
		if(oldTaxonNode.equals(newAcceptedTaxonNode)){
96
			throw new IllegalArgumentException("Taxon can not be made synonym of its own.");
97
		}
98
		
99
		Taxon oldTaxon = (Taxon) HibernateProxyHelper.deproxy(oldTaxonNode.getTaxon());
100
		Taxon newAcceptedTaxon = (Taxon) HibernateProxyHelper.deproxy(newAcceptedTaxonNode.getTaxon());
101
		
102
		// Move oldTaxon to newTaxon
103
		TaxonNameBase<?,?> synonymName = oldTaxon.getName();
104
		if (synonymRelationshipType == null){
105
			if (synonymName.isHomotypic(newAcceptedTaxon.getName())){
106
				synonymRelationshipType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
107
			}else{
108
				synonymRelationshipType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
109
			}
110
		}
111
		SynonymRelationship synonmyRelationship = newAcceptedTaxon.addSynonymName(synonymName, synonymRelationshipType, citation, citationMicroReference);
112
		
113
		//Move Synonym Relations to new Taxon
114
		for(SynonymRelationship synRelation : oldTaxon.getSynonymRelations()){
115
			newAcceptedTaxon.addSynonym(synRelation.getSynonym(), synRelation.getType(), 
116
					synRelation.getCitation(), synRelation.getCitationMicroReference());
117
		}
118

    
119
		
120
		// CHILD NODES
121
		if(oldTaxonNode.getChildNodes() != null && oldTaxonNode.getChildNodes().size() != 0){
122
			for(TaxonNode childNode : oldTaxonNode.getChildNodes()){
123
				newAcceptedTaxonNode.addChildNode(childNode, childNode.getReference(), childNode.getMicroReference(), childNode.getSynonymToBeUsed());
124
			}
125
		}
126
		
127
		//Move Taxon RelationShips to new Taxon
128
		Set<TaxonRelationship> obsoleteTaxonRelationships = new HashSet<TaxonRelationship>();
129
		for(TaxonRelationship taxonRelationship : oldTaxon.getTaxonRelations()){
130
			Taxon fromTaxon = (Taxon) HibernateProxyHelper.deproxy(taxonRelationship.getFromTaxon());
131
			Taxon toTaxon = (Taxon) HibernateProxyHelper.deproxy(taxonRelationship.getToTaxon());
132
			if (fromTaxon == oldTaxon){
133
				newAcceptedTaxon.addTaxonRelation(taxonRelationship.getToTaxon(), taxonRelationship.getType(), 
134
						taxonRelationship.getCitation(), taxonRelationship.getCitationMicroReference());
135
				
136
			}else if(toTaxon == oldTaxon){
137
				taxonRelationship.getFromTaxon().addTaxonRelation(newAcceptedTaxon, taxonRelationship.getType(), 
138
						taxonRelationship.getCitation(), taxonRelationship.getCitationMicroReference());
139

    
140
			}else{
141
				logger.warn("Taxon is not part of its own Taxonrelationship");
142
			}
143
			// Remove old relationships
144
			taxonRelationship.setToTaxon(null);
145
			taxonRelationship.setFromTaxon(null);
146
		}
147
		
148
		//Move descriptions to new taxon
149
		for(TaxonDescription description : oldTaxon.getDescriptions()){
150
			description.setTitleCache("Description copied from former accepted taxon: " + oldTaxon.getTitleCache() + "(Old title: " + description.getTitleCache()  + ")");
151
			newAcceptedTaxon.addDescription(description);
152
		}
153
				
154
		oldTaxonNode.delete();
155
		
156
		return synonmyRelationship.getSynonym();
157
	}
158
}
(70-70/76)