Project

General

Profile

Download (3.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.Collection;
12

    
13
import javax.validation.ConstraintValidator;
14
import javax.validation.ConstraintValidatorContext;
15

    
16
import org.apache.commons.lang3.StringUtils;
17

    
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.model.name.TaxonName;
20
import eu.etaxonomy.cdm.validation.annotation.NameMustFollowCode;
21

    
22
/**
23
 * Validator for name parts. Required since {@link TaxonName} has
24
 * no subclasses anymore. This validator checks if the names follow
25
 * the old subclassing rules.
26
 * <BR><BR>
27
 * https://dev.e-taxonomy.eu/redmine/issues/6363
28
 *
29
 * @author a.mueller
30
 * @since 11.03.2017
31
 */
32
public class NameMustFollowCodeValidator implements
33
        ConstraintValidator<NameMustFollowCode, TaxonName> {
34

    
35
    @Override
36
    public void initialize(NameMustFollowCode nameMustFollowTheirCode) { }
37

    
38
    @Override
39
    public boolean isValid(TaxonName name, ConstraintValidatorContext constraintContext) {
40
        name = CdmBase.deproxy(name);
41
        boolean valid = true;
42

    
43
        //CultivarPlantName
44
        if (! (name.isCultivar())){
45
            if (name.getCultivarEpithet() != null){
46
                valid = false;
47
            }
48
            if (name.getCultivarGroupEpithet() != null){
49
                valid = false;
50
            }
51
        }
52
        //BacterialName
53
        if (! (name.isBacterial())){
54
            if (isNotNull(name.getSubGenusAuthorship(), name.getNameApprobation())){
55
                valid = false;
56
            }
57
        }
58
        //BacterialName
59
        if (! (name.isViral())){
60
            if (name.getAcronym() != null){
61
                valid = false;
62
            }
63
        }
64
        //ZoologicalName
65
        if (! (name.isZoological())){
66
            if (isNotNull(name.getBreed(), name.getOriginalPublicationYear()
67
                    , name.getPublicationYear())){
68
                valid = false;
69
            }
70
        }
71
        //NonViralName
72
        if (! (name.isNonViral())){
73
            if (    isNotNull(name.getGenusOrUninomial(), name.getSpecificEpithet()
74
                        , name.getInfraGenericEpithet(), name.getInfraSpecificEpithet() )
75
                    || isNotEmpty(name.getNameRelations() , name.getHybridParentRelations()
76
                        , name.getHybridChildRelations())
77
                    || isNotFalse(name.hasAuthors(), name.isHybrid()
78
                        , name.isProtectedAuthorshipCache(), name.isProtectedNameCache())
79
                    || isNotBlank(name.getNameCache(), name.getAuthorshipCache())
80
                    ){
81
                valid = false;
82
            }
83
        }
84
        return valid;
85
    }
86

    
87
    private boolean isNotFalse(boolean ... shouldBeFalse) {
88
        for (boolean bool : shouldBeFalse){
89
            if (bool){
90
                return true;
91
            }
92
        }
93
        return false;
94
    }
95

    
96
    private boolean isNotEmpty(Collection<?> ... shouldBeEmpty) {
97
        for (Collection<?> coll : shouldBeEmpty){
98
            if (!coll.isEmpty()){
99
                return true;
100
            }
101
        }
102
        return false;
103
    }
104

    
105
    /**
106
     * @param nameCache
107
     * @param authorshipCache
108
     * @return
109
     */
110
    private boolean isNotBlank(String ... shouldBeBlank) {
111
        for (String str : shouldBeBlank){
112
            if (StringUtils.isNotBlank(str)){
113
                return true;
114
            }
115
        }
116
        return false;
117
    }
118

    
119
    /**
120
     * @param subGenusAuthorship
121
     * @param nameApprobation
122
     * @return
123
     */
124
    private boolean isNotNull(Object ... shouldBeNullObjects) {
125
        for (Object obj : shouldBeNullObjects){
126
            if (obj != null){
127
                return true;
128
            }
129
        }
130
        return false;
131
    }
132
}
133

    
(11-11/20)