1 |
33852ad0
|
Andreas Müller
|
/**
|
2 |
|
|
* Copyright (C) 2018 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.api.service.dto;
|
10 |
|
|
|
11 |
|
|
import java.util.ArrayList;
|
12 |
|
|
import java.util.List;
|
13 |
1e1d5695
|
Andreas Müller
|
import java.util.Set;
|
14 |
33852ad0
|
Andreas Müller
|
import java.util.UUID;
|
15 |
3d73cbc2
|
Andreas Kohlbecker
|
import java.util.stream.Collectors;
|
16 |
33852ad0
|
Andreas Müller
|
|
17 |
|
|
import org.joda.time.DateTime;
|
18 |
|
|
|
19 |
|
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
20 |
|
|
import eu.etaxonomy.cdm.format.taxon.TaxonRelationshipFormatter;
|
21 |
|
|
import eu.etaxonomy.cdm.model.common.Language;
|
22 |
|
|
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
|
23 |
|
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
24 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
25 |
|
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
26 |
1c39686b
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.term.Representation;
|
27 |
1e1d5695
|
Andreas Müller
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
28 |
33852ad0
|
Andreas Müller
|
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
|
29 |
|
|
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
|
30 |
|
|
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* DTO to transfer a list of taxon relationships for a given taxon.
|
34 |
|
|
*
|
35 |
|
|
* @author a.mueller
|
36 |
|
|
* @since 15.08.2018
|
37 |
|
|
*/
|
38 |
|
|
public class TaxonRelationshipsDTO {
|
39 |
|
|
|
40 |
d7909683
|
Andreas Müller
|
private static final String SENSU_SEPARATOR = ", ";
|
41 |
|
|
|
42 |
b7f100e5
|
Andreas Kohlbecker
|
public class TaxonRelationDTO{
|
43 |
33852ad0
|
Andreas Müller
|
|
44 |
|
|
private boolean doubtful = false;
|
45 |
|
|
private boolean misapplication = false;
|
46 |
|
|
private boolean synonym = false;
|
47 |
|
|
private Direction direction;
|
48 |
|
|
private UUID taxonUuid;
|
49 |
|
|
private String cache;
|
50 |
|
|
private List<TaggedText> taggedText;
|
51 |
|
|
//TODO maybe this will be changed in future
|
52 |
1e1d5695
|
Andreas Müller
|
private TermDto type;
|
53 |
78614c1a
|
Andreas Müller
|
private UUID typeUuid;
|
54 |
b7f100e5
|
Andreas Kohlbecker
|
private SourceDTO sec;
|
55 |
|
|
private SourceDTO relSec;
|
56 |
3d73cbc2
|
Andreas Kohlbecker
|
private Set<UUID> classificationsUUIDs;
|
57 |
33852ad0
|
Andreas Müller
|
|
58 |
|
|
|
59 |
b7f100e5
|
Andreas Kohlbecker
|
public TaxonRelationDTO(TaxonRelationship relation, Direction direction, List<Language> languages) {
|
60 |
33852ad0
|
Andreas Müller
|
Taxon relatedTaxon = direction == Direction.relatedTo? relation.getToTaxon()
|
61 |
|
|
: relation.getFromTaxon();
|
62 |
|
|
this.taxonUuid = relatedTaxon.getUuid();
|
63 |
3d73cbc2
|
Andreas Kohlbecker
|
|
64 |
|
|
classificationsUUIDs = relatedTaxon.getTaxonNodes().stream().map(tn -> tn.getClassification().getUuid()).collect(Collectors.toSet());
|
65 |
33852ad0
|
Andreas Müller
|
this.doubtful = relation.isDoubtful();
|
66 |
|
|
this.direction = direction;
|
67 |
1e1d5695
|
Andreas Müller
|
TaxonRelationshipType relType = relation.getType();
|
68 |
|
|
|
69 |
|
|
if (relType != null){
|
70 |
1ffd1c27
|
Andreas Müller
|
this.misapplication = relType.isMisappliedName();
|
71 |
1e1d5695
|
Andreas Müller
|
this.synonym = relType.isAnySynonym();
|
72 |
|
|
this.typeUuid = relType.getUuid();
|
73 |
|
|
// TODO there must be a better DTO which also includes
|
74 |
|
|
Set<Representation> representations = direction.isDirect() ? relType.getRepresentations() : relType.getInverseRepresentations();
|
75 |
1cac1886
|
Patrick Plitzner
|
TermDto termDto = TermDto.fromTerm(relType, representations);
|
76 |
1e1d5695
|
Andreas Müller
|
this.type = termDto;
|
77 |
d7909683
|
Andreas Müller
|
// TODO localize
|
78 |
|
|
// termDto.localize(representation_L10n);
|
79 |
33852ad0
|
Andreas Müller
|
}
|
80 |
b53c4d75
|
Andreas Müller
|
List<TaggedText> tags = TaxonRelationshipFormatter.INSTANCE().getTaggedText(
|
81 |
33852ad0
|
Andreas Müller
|
relation, direction == Direction.relatedFrom, languages);
|
82 |
|
|
this.taggedText = tags;
|
83 |
|
|
this.setCache(TaggedCacheHelper.createString(tags));
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
public UUID getTaxonUuid() {
|
88 |
|
|
return taxonUuid;
|
89 |
|
|
}
|
90 |
|
|
public void setTaxonUuid(UUID taxonUuid) {
|
91 |
|
|
this.taxonUuid = taxonUuid;
|
92 |
|
|
}
|
93 |
|
|
public boolean isDoubtful() {
|
94 |
|
|
return doubtful;
|
95 |
|
|
}
|
96 |
|
|
public void setDoubtful(boolean doubtful) {
|
97 |
|
|
this.doubtful = doubtful;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
public Direction getDirection() {
|
101 |
|
|
return direction;
|
102 |
|
|
}
|
103 |
|
|
public void setDirection(Direction direction) {
|
104 |
|
|
this.direction = direction;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
@Override
|
110 |
|
|
public String toString(){
|
111 |
|
|
return taxonUuid == null? super.toString() : taxonUuid.toString();
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
public String getCache() {
|
115 |
|
|
return cache;
|
116 |
|
|
}
|
117 |
|
|
public void setCache(String cache) {
|
118 |
|
|
this.cache = cache;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public List<TaggedText> getTaggedText() {
|
122 |
|
|
return taggedText;
|
123 |
|
|
}
|
124 |
|
|
// public void setTaggedText(List<TaggedText> taggedText) {
|
125 |
|
|
// this.taggedText = taggedText;
|
126 |
|
|
// }
|
127 |
|
|
|
128 |
|
|
public boolean isMisapplication() {
|
129 |
|
|
return misapplication;
|
130 |
|
|
}
|
131 |
|
|
public void setMisapplication(boolean misapplication) {
|
132 |
|
|
this.misapplication = misapplication;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
public boolean isSynonym() {
|
136 |
|
|
return synonym;
|
137 |
|
|
}
|
138 |
|
|
public void setSynonym(boolean synonym) {
|
139 |
|
|
this.synonym = synonym;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
1e1d5695
|
Andreas Müller
|
public TermDto getType() {
|
143 |
33852ad0
|
Andreas Müller
|
return type;
|
144 |
|
|
}
|
145 |
1e1d5695
|
Andreas Müller
|
public void setType(TermDto type) {
|
146 |
33852ad0
|
Andreas Müller
|
this.type = type;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
1e1d5695
|
Andreas Müller
|
|
150 |
|
|
/**
|
151 |
|
|
* @return the typeUuid
|
152 |
|
|
*/
|
153 |
|
|
public UUID getTypeUuid() {
|
154 |
|
|
return typeUuid;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* @param typeUuid the typeUuid to set
|
160 |
|
|
*/
|
161 |
|
|
public void setTypeUuid(UUID typeUuid) {
|
162 |
|
|
this.typeUuid = typeUuid;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
3d73cbc2
|
Andreas Kohlbecker
|
|
166 |
|
|
public Set<UUID> getClassificationsUUIDs() {
|
167 |
|
|
return classificationsUUIDs;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
33852ad0
|
Andreas Müller
|
}
|
171 |
|
|
|
172 |
b7f100e5
|
Andreas Kohlbecker
|
private List<TaxonRelationDTO> relations = new ArrayList<>();
|
173 |
33852ad0
|
Andreas Müller
|
|
174 |
|
|
private List<List<TaggedText>> misapplications = new ArrayList<>();
|
175 |
|
|
|
176 |
|
|
private DateTime date = DateTime.now();
|
177 |
|
|
|
178 |
|
|
//** ******************* CONSTRUCTOR **************************/
|
179 |
|
|
|
180 |
|
|
public TaxonRelationshipsDTO() {}
|
181 |
|
|
|
182 |
|
|
// public TaxonRelationshipsDTO(UUID taxonUuid) {
|
183 |
|
|
// IncludedTaxon originalTaxon = new TaxonRelationshipsDTO(taxonUuid, false);
|
184 |
|
|
// includedTaxa.add(originalTaxon);
|
185 |
|
|
// }
|
186 |
|
|
|
187 |
|
|
// ************************** GETTER / SETTER ***********************/
|
188 |
|
|
|
189 |
b7f100e5
|
Andreas Kohlbecker
|
public List<TaxonRelationDTO> getRelations() {
|
190 |
33852ad0
|
Andreas Müller
|
return relations;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
b7f100e5
|
Andreas Kohlbecker
|
public void setIncludedTaxa(List<TaxonRelationDTO> relations) {
|
194 |
33852ad0
|
Andreas Müller
|
this.relations = relations;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
b7f100e5
|
Andreas Kohlbecker
|
public void addRelation(TaxonRelationDTO relation){
|
198 |
33852ad0
|
Andreas Müller
|
relations.add(relation);
|
199 |
|
|
}
|
200 |
|
|
|
201 |
b7f100e5
|
Andreas Kohlbecker
|
public TaxonRelationDTO addRelation(TaxonRelationship relation, Direction direction, List<Language> languages) {
|
202 |
|
|
TaxonRelationDTO newRelation = new TaxonRelationDTO(relation, direction, languages);
|
203 |
33852ad0
|
Andreas Müller
|
relations.add(newRelation);
|
204 |
d7909683
|
Andreas Müller
|
return newRelation;
|
205 |
33852ad0
|
Andreas Müller
|
}
|
206 |
|
|
|
207 |
|
|
public void createMisapplicationString() {
|
208 |
|
|
List<List<TaggedText>> result = new ArrayList<>();
|
209 |
|
|
|
210 |
b7f100e5
|
Andreas Kohlbecker
|
for (TaxonRelationDTO relation: relations){
|
211 |
33852ad0
|
Andreas Müller
|
if (relation.isMisapplication()){
|
212 |
|
|
List<TaggedText> tags = relation.getTaggedText();
|
213 |
|
|
|
214 |
4e6e568a
|
Andreas Müller
|
boolean isDuplicate = false;
|
215 |
33852ad0
|
Andreas Müller
|
for (List<TaggedText> existing: result){
|
216 |
|
|
isDuplicate = mergeIfDuplicate(existing, tags);
|
217 |
|
|
if (isDuplicate){
|
218 |
|
|
break;
|
219 |
|
|
}
|
220 |
|
|
}
|
221 |
|
|
if (!isDuplicate){
|
222 |
|
|
List<TaggedText> newTags = new ArrayList<>(tags.size());
|
223 |
|
|
newTags.addAll(tags);
|
224 |
|
|
result.add(newTags);
|
225 |
|
|
}
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
this.setMisapplications(result);
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
/**
|
232 |
|
|
* Checks if all tags are equal, except for the sensuReference tags
|
233 |
|
|
* @param existing
|
234 |
|
|
* @param tags
|
235 |
|
|
* @return
|
236 |
|
|
*/
|
237 |
|
|
private boolean mergeIfDuplicate(List<TaggedText> first, List<TaggedText> second) {
|
238 |
|
|
int i = 0;
|
239 |
|
|
int j = 0;
|
240 |
|
|
int sensuEndInFirst = -1;
|
241 |
|
|
int sensuStartInSecond = -1;
|
242 |
973082b3
|
Andreas Müller
|
int senusEndInSecond = -1;
|
243 |
33852ad0
|
Andreas Müller
|
|
244 |
973082b3
|
Andreas Müller
|
boolean sensuHandled = false;
|
245 |
|
|
boolean isDuplicate = true;
|
246 |
|
|
while (isDuplicate && i < first.size() && j< second.size()){
|
247 |
33852ad0
|
Andreas Müller
|
if (tagEqualsMisapplied(first.get(i), second.get(i))){
|
248 |
4e6e568a
|
Andreas Müller
|
i++;j++;
|
249 |
973082b3
|
Andreas Müller
|
}else if (!sensuHandled){
|
250 |
33852ad0
|
Andreas Müller
|
while (i < first.size() && tagIsSensu(first.get(i))){
|
251 |
|
|
i++;
|
252 |
973082b3
|
Andreas Müller
|
sensuEndInFirst = i;
|
253 |
33852ad0
|
Andreas Müller
|
}
|
254 |
|
|
sensuStartInSecond = j;
|
255 |
|
|
while (j< second.size() && tagIsSensu(second.get(j))){
|
256 |
|
|
j++;
|
257 |
973082b3
|
Andreas Müller
|
senusEndInSecond = j;
|
258 |
33852ad0
|
Andreas Müller
|
}
|
259 |
973082b3
|
Andreas Müller
|
sensuHandled = true;
|
260 |
|
|
}else{
|
261 |
|
|
isDuplicate = false;
|
262 |
4e6e568a
|
Andreas Müller
|
i++;j++; //not really necessary
|
263 |
33852ad0
|
Andreas Müller
|
}
|
264 |
4e6e568a
|
Andreas Müller
|
|
265 |
33852ad0
|
Andreas Müller
|
}
|
266 |
4e6e568a
|
Andreas Müller
|
isDuplicate = isDuplicate && i == first.size() && j == second.size();
|
267 |
33852ad0
|
Andreas Müller
|
if (isDuplicate && sensuEndInFirst > -1 && sensuStartInSecond > -1){
|
268 |
973082b3
|
Andreas Müller
|
first.addAll(sensuEndInFirst, second.subList(sensuStartInSecond, senusEndInSecond));
|
269 |
d7909683
|
Andreas Müller
|
first.add(sensuEndInFirst, TaggedText.NewSeparatorInstance(SENSU_SEPARATOR));
|
270 |
33852ad0
|
Andreas Müller
|
return true;
|
271 |
|
|
}else{
|
272 |
|
|
return false;
|
273 |
|
|
}
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
private boolean tagIsSensu(TaggedText tag) {
|
277 |
8a17aa3e
|
Andreas Müller
|
if (tag.getType() == TagEnum.secReference ||
|
278 |
|
|
tag.getType() == TagEnum.secMicroReference ||
|
279 |
82eb3374
|
Andreas Müller
|
isSensuSeparator(tag)){
|
280 |
|
|
return true;
|
281 |
|
|
}
|
282 |
|
|
return false;
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
private boolean isSensuSeparator(TaggedText tag) {
|
286 |
|
|
if (SENSU_SEPARATOR.equals(tag.getText())
|
287 |
|
|
&& tag.getType() == TagEnum.separator) {
|
288 |
33852ad0
|
Andreas Müller
|
return true;
|
289 |
|
|
}
|
290 |
|
|
return false;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
private boolean tagEqualsMisapplied(TaggedText x, TaggedText y) {
|
294 |
|
|
if (CdmUtils.nullSafeEqual(x.getText(),y.getText())
|
295 |
4e6e568a
|
Andreas Müller
|
&& x.getType().equals(y.getType())
|
296 |
|
|
&& CdmUtils.nullSafeEqual(x.getEntityReference(),y.getEntityReference())){
|
297 |
33852ad0
|
Andreas Müller
|
return true;
|
298 |
|
|
}else{
|
299 |
|
|
return false;
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
public DateTime getDate() {
|
304 |
|
|
return date;
|
305 |
|
|
}
|
306 |
|
|
public void setDate(DateTime date) {
|
307 |
|
|
this.date = date;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
public int getSize(){
|
311 |
|
|
return relations.size();
|
312 |
|
|
}
|
313 |
|
|
//
|
314 |
|
|
// public boolean contains(UUID taxonUuid) {
|
315 |
b7f100e5
|
Andreas Kohlbecker
|
// for (TaxonRelationDTO relation: relations){
|
316 |
33852ad0
|
Andreas Müller
|
// if (taxon.taxonUuid.equals(taxonUuid)){
|
317 |
|
|
// return true;
|
318 |
|
|
// }
|
319 |
|
|
// }
|
320 |
|
|
// return false;
|
321 |
|
|
// }
|
322 |
|
|
|
323 |
|
|
@Override
|
324 |
|
|
public String toString(){
|
325 |
|
|
String result = "";
|
326 |
b7f100e5
|
Andreas Kohlbecker
|
for (TaxonRelationDTO relation : relations){
|
327 |
33852ad0
|
Andreas Müller
|
result += relation.toString() + ",";
|
328 |
|
|
}
|
329 |
|
|
if (result.length() > 0){
|
330 |
|
|
result = result.substring(0, result.length() - 1);
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
result = "[" + result + "]";
|
334 |
|
|
return result;
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
public List<List<TaggedText>> getMisapplications() {
|
338 |
|
|
return misapplications;
|
339 |
|
|
}
|
340 |
|
|
public void setMisapplications(List<List<TaggedText>> misapplications) {
|
341 |
|
|
this.misapplications = misapplications;
|
342 |
|
|
}
|
343 |
|
|
}
|