Project

General

Profile

Download (3.79 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.lang.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 sublassing 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
 */
33
public class NameMustFollowCodeValidator implements
34
        ConstraintValidator<NameMustFollowCode, TaxonName> {
35

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

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

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

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

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

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

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

    
(9-9/17)