Project

General

Profile

Download (7.21 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Service;
15
import org.springframework.transaction.TransactionStatus;
16
import org.springframework.transaction.annotation.Transactional;
17

    
18
import eu.etaxonomy.cdm.model.description.TaxonDescription;
19
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
21
import eu.etaxonomy.cdm.model.taxon.Synonym;
22
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
23
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
27
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
28
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
29
import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
30

    
31
import java.util.Collection;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.UUID;
35

    
36

    
37
@Service
38
@Transactional(readOnly = true)
39
public class TaxonServiceImpl extends ServiceBase<TaxonBase> implements ITaxonService {
40
	static Logger logger = Logger.getLogger(TaxonServiceImpl.class);
41
	
42
	private ITaxonDao taxonDao;
43
	
44
	@Autowired
45
	protected void setDao(ITaxonDao dao) {
46
		this.dao = dao;
47
		this.taxonDao = dao;
48
	}
49
	
50

    
51
	public TaxonBase getTaxonByUuid(UUID uuid) {
52
		return super.getCdmObjectByUuid(uuid); 
53
	}
54

    
55
	@Transactional(readOnly = false)
56
	public UUID saveTaxon(TaxonBase taxon) {
57
		return super.saveCdmObject(taxon);
58
	}
59

    
60
	//@Transactional(readOnly = false)
61
	public UUID saveTaxon(TaxonBase taxon, TransactionStatus txStatus) {
62
		
63
		return super.saveCdmObject(taxon);
64
	}
65
	
66
	
67
	@Transactional(readOnly = false)
68
	public Map<UUID, TaxonBase> saveTaxonAll(Collection<TaxonBase> taxonCollection){
69
		return saveCdmObjectAll(taxonCollection);
70
	}
71

    
72
	
73

    
74
	@Transactional(readOnly = false)
75
	public UUID removeTaxon(TaxonBase taxon) {
76
		return super.removeCdmObject(taxon);
77
	}
78

    
79
	public List<TaxonBase> searchTaxaByName(String name, ReferenceBase sec) {
80
		return taxonDao.getTaxaByName(name, sec);
81
	}
82

    
83
	/* (non-Javadoc)
84
	 * @see eu.etaxonomy.cdm.api.service.ITaxonService#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase)
85
	 */
86
	public List<Taxon> getRootTaxa(ReferenceBase sec){
87
		return getRootTaxa(sec, CdmFetch.FETCH_CHILDTAXA(), true);
88
	}
89

    
90
	
91
	
92
	/* (non-Javadoc)
93
	 * @see eu.etaxonomy.cdm.api.service.ITaxonService#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase, boolean)
94
	 */
95
	public List<Taxon> getRootTaxa(ReferenceBase sec, CdmFetch cdmFetch, boolean onlyWithChildren) {
96
		if (cdmFetch == null){
97
			cdmFetch = CdmFetch.NO_FETCH();
98
		}
99
		return taxonDao.getRootTaxa(sec, cdmFetch, onlyWithChildren);
100
	}
101

    
102
	/* (non-Javadoc)
103
	 * @see eu.etaxonomy.cdm.api.service.ITaxonService#makeTaxonSynonym(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Taxon)
104
	 */
105
	@Transactional(readOnly = false)
106
	public Synonym makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon, SynonymRelationshipType synonymType, ReferenceBase citation, String citationMicroReference) {
107
		if (oldTaxon == null || newAcceptedTaxon == null || oldTaxon.getName() == null){
108
			return null;
109
		}
110
		
111
		// Move oldTaxon to newTaxon
112
		TaxonNameBase synonymName = oldTaxon.getName();
113
		if (synonymType == null){
114
			if (synonymName.isHomotypic(newAcceptedTaxon.getName())){
115
				synonymType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
116
			}else{
117
				//TODO synonymType 
118
				synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
119
			}
120
		}
121
		SynonymRelationship synRel = newAcceptedTaxon.addSynonymName(synonymName, synonymType, citation, citationMicroReference);
122
		
123
		//Move Synonym Relations to new Taxon
124
		for(SynonymRelationship synRelation : oldTaxon.getSynonymRelations()){
125
			//TODO citation and microcitation
126
			newAcceptedTaxon.addSynonym(synRelation.getSynonym(), synRelation.getType(), null, null);
127
		}
128

    
129
		//Move Taxon RelationShips to new Taxon
130
		for(TaxonRelationship taxonRelation : oldTaxon.getTaxonRelations()){
131
			//CHILDREN
132
			if (taxonRelation.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
133
				if (taxonRelation.getFromTaxon() == oldTaxon){
134
					oldTaxon.removeTaxonRelation(taxonRelation);
135
				}else if(taxonRelation.getToTaxon() == oldTaxon){
136
					newAcceptedTaxon.addTaxonomicChild(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
137
					oldTaxon.removeTaxonRelation(taxonRelation);
138
				}else{
139
					logger.warn("Taxon is not part of its own Taxonrelationship");
140
				}
141
			}
142
			//MISAPPLIED NAMES
143
			if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
144
				if (taxonRelation.getFromTaxon() == oldTaxon){
145
					newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
146
					oldTaxon.removeTaxonRelation(taxonRelation);
147
				}else if(taxonRelation.getToTaxon() == oldTaxon){
148
					newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
149
					oldTaxon.removeTaxonRelation(taxonRelation);
150
				}else{
151
					logger.warn("Taxon is not part of its own Taxonrelationship");
152
				}
153
			}
154
			//Concept Relationships
155
			//FIXME implement
156
//			if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIEDNAMEFOR())){
157
//				if (taxonRelation.getFromTaxon() == oldTaxon){
158
//					newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
159
//					oldTaxon.removeTaxonRelation(taxonRelation);
160
//				}else if(taxonRelation.getToTaxon() == oldTaxon){
161
//					newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
162
//					oldTaxon.removeTaxonRelation(taxonRelation);
163
//				}else{
164
//					logger.warn("Taxon is not part of its own Taxonrelationship");
165
//				}
166
//			}
167
		}
168
		
169
		//Move Descriptions to new Taxon
170
		for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
171
			newAcceptedTaxon.addDescription(taxDescription);
172
		}
173
		//delete old Taxon
174
		this.dao.saveOrUpdate(newAcceptedTaxon);
175
//		FIXME implement
176
		this.dao.delete(oldTaxon);
177
		
178
		//return
179
		this.dao.flush();
180
		return synRel.getSynonym();
181
	}
182

    
183

    
184
	public void generateTitleCache() {
185
		generateTitleCache(true);
186
	}
187
	//TODO
188
	public void generateTitleCache(boolean forceProtected) {
189
		logger.warn("generateTitleCache not yet fully implemented!");
190
//		for (TaxonBase tb : taxonDao.getAllTaxa(null,null)){
191
//			logger.warn("Old taxon title: " + tb.getTitleCache());
192
//			if (forceProtected || !tb.isProtectedTitleCache() ){
193
//				tb.setTitleCache(tb.generateTitle(), false);
194
//				taxonDao.update(tb);
195
//				logger.warn("New title: " + tb.getTitleCache());
196
//			}
197
//		}
198
		
199
	}
200
}
(19-19/20)