Opportunity to omit term loading during DB initialization.
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / TaxonServiceImpl.java
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.common.RelationshipBase;
19 import eu.etaxonomy.cdm.model.description.TaxonDescription;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
22 import eu.etaxonomy.cdm.model.taxon.Synonym;
23 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
24 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
28 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
29 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
30 import eu.etaxonomy.cdm.persistence.fetch.CdmFetch;
31
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36
37
38 @Service
39 @Transactional(readOnly = true)
40 public class TaxonServiceImpl extends ServiceBase<TaxonBase> implements ITaxonService {
41 static Logger logger = Logger.getLogger(TaxonServiceImpl.class);
42
43 private ITaxonDao taxonDao;
44
45 @Autowired
46 protected void setDao(ITaxonDao dao) {
47 this.dao = dao;
48 this.taxonDao = dao;
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, ? extends TaxonBase> saveTaxonAll(Collection<? extends TaxonBase> taxonCollection){
69 return saveCdmObjectAll(taxonCollection);
70 }
71
72 @Transactional(readOnly = false)
73 public UUID removeTaxon(TaxonBase taxon) {
74 return super.removeCdmObject(taxon);
75 }
76
77 public List<TaxonBase> searchTaxaByName(String name, ReferenceBase sec) {
78 return taxonDao.getTaxaByName(name, sec);
79 }
80
81 public List<TaxonBase> getAllTaxa(int limit, int start){
82 return taxonDao.list(limit, start);
83 }
84
85 public List<Synonym> getAllSynonyms(int limit, int start) {
86 return taxonDao.getAllSynonyms(limit, start);
87 }
88
89 /* (non-Javadoc)
90 * @see eu.etaxonomy.cdm.api.service.ITaxonService#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase)
91 */
92 public List<Taxon> getRootTaxa(ReferenceBase sec){
93 return getRootTaxa(sec, CdmFetch.FETCH_CHILDTAXA(), true);
94 }
95
96 /* (non-Javadoc)
97 * @see eu.etaxonomy.cdm.api.service.ITaxonService#getRootTaxa(eu.etaxonomy.cdm.model.reference.ReferenceBase, boolean)
98 */
99 public List<Taxon> getRootTaxa(ReferenceBase sec, CdmFetch cdmFetch, boolean onlyWithChildren) {
100 if (cdmFetch == null){
101 cdmFetch = CdmFetch.NO_FETCH();
102 }
103 return taxonDao.getRootTaxa(sec, cdmFetch, onlyWithChildren);
104 }
105
106 public List<RelationshipBase> getAllRelationships(int limit, int start){
107 return taxonDao.getAllRelationships(limit, start);
108 }
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.cdm.api.service.ITaxonService#makeTaxonSynonym(eu.etaxonomy.cdm.model.taxon.Taxon, eu.etaxonomy.cdm.model.taxon.Taxon)
112 */
113 @Transactional(readOnly = false)
114 public Synonym makeTaxonSynonym(Taxon oldTaxon, Taxon newAcceptedTaxon, SynonymRelationshipType synonymType, ReferenceBase citation, String citationMicroReference) {
115 if (oldTaxon == null || newAcceptedTaxon == null || oldTaxon.getName() == null){
116 return null;
117 }
118
119 // Move oldTaxon to newTaxon
120 TaxonNameBase synonymName = oldTaxon.getName();
121 if (synonymType == null){
122 if (synonymName.isHomotypic(newAcceptedTaxon.getName())){
123 synonymType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
124 }else{
125 //TODO synonymType
126 synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
127 }
128 }
129 SynonymRelationship synRel = newAcceptedTaxon.addSynonymName(synonymName, synonymType, citation, citationMicroReference);
130
131 //Move Synonym Relations to new Taxon
132 for(SynonymRelationship synRelation : oldTaxon.getSynonymRelations()){
133 //TODO citation and microcitation
134 newAcceptedTaxon.addSynonym(synRelation.getSynonym(), synRelation.getType(), null, null);
135 }
136
137 //Move Taxon RelationShips to new Taxon
138 for(TaxonRelationship taxonRelation : oldTaxon.getTaxonRelations()){
139 //CHILDREN
140 if (taxonRelation.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
141 if (taxonRelation.getFromTaxon() == oldTaxon){
142 oldTaxon.removeTaxonRelation(taxonRelation);
143 }else if(taxonRelation.getToTaxon() == oldTaxon){
144 newAcceptedTaxon.addTaxonomicChild(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
145 oldTaxon.removeTaxonRelation(taxonRelation);
146 }else{
147 logger.warn("Taxon is not part of its own Taxonrelationship");
148 }
149 }
150 //MISAPPLIED NAMES
151 if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
152 if (taxonRelation.getFromTaxon() == oldTaxon){
153 newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
154 oldTaxon.removeTaxonRelation(taxonRelation);
155 }else if(taxonRelation.getToTaxon() == oldTaxon){
156 newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
157 oldTaxon.removeTaxonRelation(taxonRelation);
158 }else{
159 logger.warn("Taxon is not part of its own Taxonrelationship");
160 }
161 }
162 //Concept Relationships
163 //FIXME implement
164 // if (taxonRelation.getType().equals(TaxonRelationshipType.MISAPPLIEDNAMEFOR())){
165 // if (taxonRelation.getFromTaxon() == oldTaxon){
166 // newAcceptedTaxon.addMisappliedName(taxonRelation.getToTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
167 // oldTaxon.removeTaxonRelation(taxonRelation);
168 // }else if(taxonRelation.getToTaxon() == oldTaxon){
169 // newAcceptedTaxon.addMisappliedName(taxonRelation.getFromTaxon(), taxonRelation.getCitation(), taxonRelation.getCitationMicroReference());
170 // oldTaxon.removeTaxonRelation(taxonRelation);
171 // }else{
172 // logger.warn("Taxon is not part of its own Taxonrelationship");
173 // }
174 // }
175 }
176
177 //Move Descriptions to new Taxon
178 for(TaxonDescription taxDescription : oldTaxon.getDescriptions()){
179 newAcceptedTaxon.addDescription(taxDescription);
180 }
181 //delete old Taxon
182 this.dao.saveOrUpdate(newAcceptedTaxon);
183 // FIXME implement
184 this.dao.delete(oldTaxon);
185
186 //return
187 this.dao.flush();
188 return synRel.getSynonym();
189 }
190
191
192 public void generateTitleCache() {
193 generateTitleCache(true);
194 }
195 //TODO
196 public void generateTitleCache(boolean forceProtected) {
197 logger.warn("generateTitleCache not yet fully implemented!");
198 // for (TaxonBase tb : taxonDao.getAllTaxa(null,null)){
199 // logger.warn("Old taxon title: " + tb.getTitleCache());
200 // if (forceProtected || !tb.isProtectedTitleCache() ){
201 // tb.setTitleCache(tb.generateTitle(), false);
202 // taxonDao.update(tb);
203 // logger.warn("New title: " + tb.getTitleCache());
204 // }
205 // }
206
207 }
208 }