Project

General

Profile

Download (3.88 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
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.utility;
10

    
11
import java.util.HashSet;
12
import java.util.Optional;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import eu.etaxonomy.cdm.model.name.Rank;
17
import eu.etaxonomy.cdm.model.name.TaxonName;
18
import eu.etaxonomy.cdm.persistence.dto.TaxonNameParts;
19

    
20
/**
21
 * The {@link TaxonNamePartsFilter} defines the rank and fixed name parts for the name part search. See as an example
22
 * {@link eu.etaxonomy.cdm.api.service.INameService#findTaxonNameParts(TaxonNamePartsFilter, String, Integer, Integer, java.util.List)}
23
 * <p>
24
 * For example: In case the <code>rank</code> is "genus", the <code>genusOrUninomial</code> will be used as search parameter which needs to match exactly.
25
 * The <code>namePartQueryString</code> will be used to do a wildcard search on the specificEpithet.
26
 * <p>
27
 * For name part lookup purposes the <code>TaxonNameParts</code> in the result list can be asked to return the relavant name part by
28
 * calling {@link TaxonNameParts#nameRankSpecificNamePart(TaxonName)}
29
 *
30
 * @author a.kohlbecker
31
 * @since Jun 12, 2018
32
 *
33
 */
34
public class TaxonNamePartsFilter extends TaxonNameParts {
35

    
36
    private Set<UUID> exludedNamesUuids = new HashSet<>();
37

    
38
    /**
39
     * @param taxonNameId
40
     * @param rank
41
     * @param genusOrUninomial
42
     * @param infraGenericEpithet
43
     * @param specificEpithet
44
     * @param infraSpecificEpithet
45
     */
46
    public TaxonNamePartsFilter(Rank rank, String genusOrUninomial, String infraGenericEpithet,
47
            String specificEpithet, String infraSpecificEpithet, Set<TaxonName> exludedNames) {
48
        super(null, null, rank, genusOrUninomial, infraGenericEpithet, specificEpithet, infraSpecificEpithet);
49
        this.setExludedNames(exludedNames);
50
    }
51

    
52
    public TaxonNamePartsFilter(){
53
        super();
54
    }
55

    
56
    public Optional<String> uninomialQueryString(String query){
57
         if(rank.isLower(Rank.GENUS())){
58
             return optionalForNonNull(genusOrUninomial);
59
        } else {
60
            return Optional.of(appendWildcard(query));
61
        }
62
    }
63

    
64
    public Optional<String> infraGenericEpithet(String query){
65
        if(rank.isInfraGeneric()){
66
            return Optional.of(appendWildcard(query));
67
        } else if(rank.isLower(Rank.GENUS())) {
68
            return optionalForNonNull(infraGenericEpithet);
69
        } else {
70
            // mask invalid data as null
71
            return null;
72
        }
73
    }
74

    
75
    public Optional<String> specificEpithet(String query){
76
        if(rank.isLower(Rank.SPECIES())){
77
            return optionalForNonNull(specificEpithet);
78
        } else if(rank.isSpecies()) {
79
            return Optional.of(appendWildcard(query));
80
        } else {
81
            return null;
82
        }
83
    }
84

    
85
    public Optional<String> infraspecificEpithet(String query){
86
        if(rank.isInfraSpecific()){
87
            return Optional.of(appendWildcard(query));
88
        } else {
89
            return null;
90
        }
91
    }
92

    
93
    private Optional<String> optionalForNonNull(String value){
94
        if(value != null){
95
            return Optional.of(value);
96
        } else {
97
            return null;
98
        }
99
    }
100

    
101
    private String appendWildcard(String query){
102
        if(query == null){
103
            query = "*";
104
        } else if(!query.endsWith("*")){
105
            return query + "*";
106
        }
107
        return query;
108

    
109
    }
110

    
111
    /**
112
     * @return the exludedNames
113
     */
114
    public Set<UUID> getExludedNamesUuids() {
115
        return exludedNamesUuids;
116
    }
117

    
118
    /**
119
     * @param exludedNames the exludedNames to set
120
     */
121
    public void setExludedNames(Set<TaxonName> exludedNames) {
122
        for(TaxonName name : exludedNames){
123
            exludedNamesUuids.add(name.getUuid());
124
        }
125
    }
126
}
(11-11/12)