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
|
package eu.etaxonomy.cdm.remote.dto.assembler;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collections;
|
13
|
import java.util.Enumeration;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Locale;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
25
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
26
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
28
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
33
|
import eu.etaxonomy.cdm.remote.dto.FeatureTreeTO;
|
34
|
import eu.etaxonomy.cdm.remote.dto.HomotypicTaxonGroupSTO;
|
35
|
import eu.etaxonomy.cdm.remote.dto.SynonymRelationshipTO;
|
36
|
import eu.etaxonomy.cdm.remote.dto.TaxonRelationshipTO;
|
37
|
import eu.etaxonomy.cdm.remote.dto.TaxonSTO;
|
38
|
import eu.etaxonomy.cdm.remote.dto.TaxonTO;
|
39
|
import eu.etaxonomy.cdm.remote.dto.TreeNode;
|
40
|
|
41
|
@Component
|
42
|
public class TaxonAssembler extends AssemblerBase<TaxonSTO, TaxonTO, TaxonBase>{
|
43
|
|
44
|
@Autowired
|
45
|
private NameAssembler nameAssembler;
|
46
|
@Autowired
|
47
|
private ReferenceAssembler refAssembler;
|
48
|
@Autowired
|
49
|
private LocalisedTermAssembler termAssembler;
|
50
|
@Autowired
|
51
|
private SpecimenTypeDesignationAssembler specimenTypeDesignationAssembler;
|
52
|
@Autowired
|
53
|
private DescriptionAssembler descriptionAssembler;
|
54
|
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.remote.dto.assembler.AssemblerBase#getSTO(eu.etaxonomy.cdm.model.common.CdmBase, java.util.Enumeration)
|
58
|
*/
|
59
|
public TaxonSTO getSTO(TaxonBase tb, Enumeration<Locale> locales){
|
60
|
TaxonSTO t = null;
|
61
|
if (tb!=null){
|
62
|
t = new TaxonSTO();
|
63
|
setVersionableEntity(tb, t);
|
64
|
t.setName(nameAssembler.getSTO(tb.getName(), locales));
|
65
|
t.setSecUuid(tb.getSec().getUuid().toString());
|
66
|
t.setAccepted((tb instanceof Taxon));
|
67
|
//TODO: add more mappings
|
68
|
}
|
69
|
return t;
|
70
|
}
|
71
|
|
72
|
/* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.cdm.remote.dto.assembler.AssemblerBase#getTO(eu.etaxonomy.cdm.model.common.CdmBase, java.util.Enumeration)
|
74
|
*/
|
75
|
@Override
|
76
|
@Deprecated //use getTO(TaxonBase taxonBase, FeatureTree featureTree, Enumeration<Locale> locales) instead
|
77
|
public TaxonTO getTO(TaxonBase taxonBase, Enumeration<Locale> locales){
|
78
|
FeatureTree featureTree = null;
|
79
|
return getTO(taxonBase, featureTree, locales);
|
80
|
}
|
81
|
|
82
|
/* (non-Javadoc)
|
83
|
* @see eu.etaxonomy.cdm.remote.dto.assembler.AssemblerBase#getTO(eu.etaxonomy.cdm.model.common.CdmBase, java.util.Enumeration)
|
84
|
*/
|
85
|
public TaxonTO getTO(TaxonBase taxonBase, FeatureTree featureTree, Enumeration<Locale> locales){
|
86
|
TaxonTO taxonTO = null;
|
87
|
if(taxonBase!=null){
|
88
|
taxonTO = new TaxonTO();
|
89
|
setVersionableEntity(taxonBase, taxonTO);
|
90
|
taxonTO.setName(nameAssembler.getSTO(taxonBase.getName(), locales));
|
91
|
taxonTO.setSec(refAssembler.getTO(taxonBase.getSec(), locales));
|
92
|
taxonTO.setAccepted((taxonBase instanceof Taxon));
|
93
|
if(taxonBase instanceof Taxon){
|
94
|
|
95
|
Taxon taxon = (Taxon) taxonBase;
|
96
|
// -- homotypic & heterotypic synonyms
|
97
|
List<Synonym> syns = taxon.getHomotypicSynonymsByHomotypicGroup();
|
98
|
List<Synonym> synList = new ArrayList<Synonym>();
|
99
|
for(Synonym synonym : syns) {
|
100
|
//FIXME remove skip-test hack if "missing synonym type"-bug is fixed
|
101
|
if(true || synonym.getRelationType(taxon).equals(SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF())){
|
102
|
synList.add(synonym);
|
103
|
}
|
104
|
}
|
105
|
taxonTO.setTypeDesignations(specimenTypeDesignationAssembler.getSTOs(taxon.getHomotypicGroup().getTypeDesignations(), locales));
|
106
|
taxonTO.setHomotypicSynonyms(getSynonymRelationshipTOs(synList, taxon, locales));
|
107
|
List<HomotypicalGroup> heterotypicGroups = taxon.getHeterotypicSynonymyGroups();
|
108
|
List<HomotypicTaxonGroupSTO> heterotypicSynonymyGroups = new ArrayList<HomotypicTaxonGroupSTO>(heterotypicGroups.size());
|
109
|
for (HomotypicalGroup homotypicalGroup : heterotypicGroups) {
|
110
|
heterotypicSynonymyGroups.add(getHomotypicTaxonGroupSTO(homotypicalGroup, taxon, locales));
|
111
|
}
|
112
|
taxonTO.setHeterotypicSynonymyGroups(heterotypicSynonymyGroups);
|
113
|
|
114
|
// -- taxon relations (MISAPPLIED_NAME_FOR + INVALID_DESIGNATION)
|
115
|
Set<TaxonRelationshipType> matchTypes = new HashSet<TaxonRelationshipType>();
|
116
|
matchTypes.add(TaxonRelationshipType.MISAPPLIED_NAME_FOR());
|
117
|
matchTypes.add(TaxonRelationshipType.INVALID_DESIGNATION_FOR());
|
118
|
taxonTO.setTaxonRelations(getTaxonRelationshipTOs(taxon.getTaxonRelations(), taxon, matchTypes, locales));
|
119
|
|
120
|
// -- descriptive data
|
121
|
/*if (featureTree == null){
|
122
|
taxonTO.setDescriptions(descriptionAssembler.getTOs(taxon.getDescriptions(), locales));
|
123
|
}else{*/
|
124
|
FeatureTreeTO featureTreeTO = descriptionAssembler.getTO(featureTree, taxon.getDescriptions(), locales);
|
125
|
taxonTO.setFeatureTree(featureTreeTO);
|
126
|
//}
|
127
|
//TODO: add more mappings
|
128
|
}
|
129
|
}
|
130
|
return taxonTO;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* @param homotypicalGroup
|
135
|
* @param taxon
|
136
|
* @param locales
|
137
|
* @return
|
138
|
*/
|
139
|
private HomotypicTaxonGroupSTO getHomotypicTaxonGroupSTO(HomotypicalGroup homotypicalGroup, Taxon taxon,Enumeration<Locale> locales) {
|
140
|
HomotypicTaxonGroupSTO homotypicTaxonGroupSTO = new HomotypicTaxonGroupSTO();
|
141
|
homotypicTaxonGroupSTO.setUuid(homotypicalGroup.getUuid().toString());
|
142
|
|
143
|
for(TaxonBase tb : homotypicalGroup.getSynonymsInGroup(taxon.getSec())){
|
144
|
homotypicTaxonGroupSTO.getTaxa().add(getSTO(tb, locales));
|
145
|
}
|
146
|
homotypicTaxonGroupSTO.getTypeDesignations().addAll(specimenTypeDesignationAssembler.getSTOs(homotypicalGroup.getTypeDesignations(), locales));
|
147
|
return homotypicTaxonGroupSTO;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @param synonyms
|
152
|
* @param taxon
|
153
|
* @param locales
|
154
|
* @return
|
155
|
*/
|
156
|
public List<SynonymRelationshipTO> getSynonymRelationshipTOs(List<Synonym> synonyms, Taxon taxon, Enumeration<Locale> locales){
|
157
|
List<SynonymRelationshipTO> synonymRelationshipTOs = new ArrayList<SynonymRelationshipTO>(synonyms.size());
|
158
|
for (Synonym synonym : synonyms) {
|
159
|
synonymRelationshipTOs.add(getSynonymRelationshipTO(synonym, taxon, locales));
|
160
|
}
|
161
|
return synonymRelationshipTOs;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* @param synonyms
|
166
|
* @param taxon
|
167
|
* @param locales
|
168
|
* @return
|
169
|
*/
|
170
|
public List<TaxonRelationshipTO> getTaxonRelationshipTOs(Set<TaxonRelationship> taxonRelationships, Taxon taxon, Set<TaxonRelationshipType> matchTypes, Enumeration<Locale> locales){
|
171
|
List<TaxonRelationshipTO> taxonRelationshipTOs = new ArrayList<TaxonRelationshipTO>(taxonRelationships.size());
|
172
|
for (TaxonRelationship taxonRelationship : taxonRelationships) {
|
173
|
boolean myType = false;
|
174
|
for (TaxonRelationshipType matchType : matchTypes) {
|
175
|
myType = myType || matchType.equals(taxonRelationship.getType());
|
176
|
}
|
177
|
if(myType){
|
178
|
taxonRelationshipTOs.add(getTaxonRelationshipTO(taxonRelationship, taxon, locales));
|
179
|
}
|
180
|
}
|
181
|
return taxonRelationshipTOs;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* @param syn
|
186
|
* @param t
|
187
|
* @param locales
|
188
|
* @return
|
189
|
*/
|
190
|
public TaxonRelationshipTO getTaxonRelationshipTO(TaxonRelationship taxrel, Taxon t, Enumeration<Locale> locales){
|
191
|
TaxonRelationshipTO to = new TaxonRelationshipTO();
|
192
|
if(taxrel != null){
|
193
|
to.setType(termAssembler.getSTO(taxrel.getType(), locales));
|
194
|
to.setTaxon(getSTO(taxrel.getFromTaxon(), locales));
|
195
|
}
|
196
|
return to;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* @param syn
|
201
|
* @param t
|
202
|
* @param locales
|
203
|
* @return
|
204
|
*/
|
205
|
public SynonymRelationshipTO getSynonymRelationshipTO(Synonym synonym, Taxon taxon, Enumeration<Locale> locales){
|
206
|
SynonymRelationshipTO synonymRelationshipTO = new SynonymRelationshipTO();
|
207
|
if(synonym != null){
|
208
|
synonymRelationshipTO.setSynoynm(getSTO(synonym, locales));
|
209
|
|
210
|
for(TermBase relationType : synonym.getRelationType(taxon)){
|
211
|
synonymRelationshipTO.addType(termAssembler.getSTO(relationType, locales));
|
212
|
}
|
213
|
}
|
214
|
return synonymRelationshipTO;
|
215
|
}
|
216
|
|
217
|
|
218
|
/**
|
219
|
* @param t
|
220
|
* @return
|
221
|
*/
|
222
|
public TreeNode getTreeNode(Taxon t){
|
223
|
TreeNode tn = null;
|
224
|
if(t!=null){
|
225
|
tn = new TreeNode();
|
226
|
setVersionableEntity(t, tn);
|
227
|
tn.setFullname(t.getName().getTitleCache());
|
228
|
tn.setHasChildren(t.getTaxonomicChildrenCount());
|
229
|
tn.setTaggedName(nameAssembler.getTaggedName(t.getName()));
|
230
|
tn.setSecUuid(t.getSec().getUuid().toString());
|
231
|
}
|
232
|
return tn;
|
233
|
}
|
234
|
public List<TreeNode> getTreeNodeList(Taxon[] taxa){
|
235
|
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
|
236
|
for (Taxon t : taxa){
|
237
|
result.add(this.getTreeNode(t));
|
238
|
}
|
239
|
return result;
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* @param taxa
|
244
|
* @return
|
245
|
*/
|
246
|
public List<TreeNode> getTreeNodeListSortedByName(Iterable<Taxon> taxa){
|
247
|
Map<String, TreeNode> nameMap = new HashMap<String, TreeNode>();
|
248
|
for (Taxon t : taxa){
|
249
|
nameMap.put(t.getTitleCache()+" "+t.getUuid().toString(), this.getTreeNode(t));
|
250
|
}
|
251
|
ArrayList<TreeNode> treeNodeList = new ArrayList<TreeNode>();
|
252
|
ArrayList<String> keys = new ArrayList<String>(nameMap.keySet());
|
253
|
Collections.sort(keys);
|
254
|
for (String name : keys){
|
255
|
treeNodeList.add(nameMap.get(name));
|
256
|
}
|
257
|
return treeNodeList;
|
258
|
}
|
259
|
|
260
|
}
|