Project

General

Profile

Download (6.32 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.Synonym;
32
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
33
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
37
import eu.etaxonomy.cdm.persistence.dao.BeanInitializer;
38
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
39

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

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

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

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

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

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