1
|
/**
|
2
|
* Copyright (C) 2009 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.validation.constraint;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.List;
|
13
|
import java.util.Objects;
|
14
|
import java.util.Set;
|
15
|
import java.util.stream.Collectors;
|
16
|
|
17
|
import javax.validation.ConstraintValidator;
|
18
|
import javax.validation.ConstraintValidatorContext;
|
19
|
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.INameService;
|
23
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
24
|
import eu.etaxonomy.cdm.validation.annotation.NoDuplicateNames;
|
25
|
|
26
|
public class NoDuplicateNamesValidator implements
|
27
|
ConstraintValidator<NoDuplicateNames,TaxonName> {
|
28
|
|
29
|
private static Set<String> includeProperties;
|
30
|
|
31
|
static {
|
32
|
includeProperties = new HashSet<>();
|
33
|
includeProperties.add("genusOrUninomial");
|
34
|
includeProperties.add("infraGenericEpithet");
|
35
|
includeProperties.add("specificEpithet");
|
36
|
includeProperties.add("infraSpecificEpithet");
|
37
|
includeProperties.add("rank");
|
38
|
// includeProperties.add("nomenclaturalSource.citation"); //handled in method now since #6581
|
39
|
// includeProperties.add("nomenclaturalSource.citationMicroReference"); //handled in method now since #6581
|
40
|
includeProperties.add("basionymAuthorship");
|
41
|
includeProperties.add("exBasionymAuthorship");
|
42
|
includeProperties.add("combinationAuthorship");
|
43
|
includeProperties.add("exCombinationAuthorship");
|
44
|
}
|
45
|
|
46
|
private INameService nameService;
|
47
|
|
48
|
@Autowired
|
49
|
public void setNameService(INameService nameService) {
|
50
|
this.nameService = nameService;
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public void initialize(NoDuplicateNames noDuplicateNames) { }
|
55
|
|
56
|
@Override
|
57
|
public boolean isValid(TaxonName name, ConstraintValidatorContext constraintContext) {
|
58
|
if(name == null) {
|
59
|
return true;
|
60
|
} else {
|
61
|
List<TaxonName> matchingNames = nameService.list(name, includeProperties, null, null, null, null);
|
62
|
matchingNames = matchingNames.stream().filter(existing->
|
63
|
Objects.equals(existing.getNomenclaturalReference(),name.getNomenclaturalReference())
|
64
|
&& Objects.equals(existing.getNomenclaturalMicroReference(), name.getNomenclaturalMicroReference())
|
65
|
&& !existing.equals(name)
|
66
|
).collect(Collectors.toList());
|
67
|
|
68
|
if(matchingNames.size() > 0) {
|
69
|
if(matchingNames.size() == 1 && matchingNames.get(0).equals(name)) {
|
70
|
return true;
|
71
|
} else {
|
72
|
return false;
|
73
|
}
|
74
|
} else {
|
75
|
return true;
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
}
|