Project

General

Profile

Download (3.85 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.Arrays;
13
import java.util.Collection;
14
import java.util.Collections;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
22

    
23
/**
24
 *
25
 * @author a.kohlbecker
26
 * @since Apr 26, 2019
27
 *
28
 */
29
public class TypeDesignationStatusFilter {
30

    
31
    private Set<TypeDesignationStatusBase> status = new HashSet<>();
32
    private List<Language> languages;
33
    private boolean ignoreCase = true;
34

    
35
    public static final TypeDesignationStatusFilter NULL_ELEMENT = new TypeDesignationStatusFilter(null, Arrays.asList(Language.DEFAULT()), true);
36

    
37
    public TypeDesignationStatusFilter(TypeDesignationStatusBase status, List<Language> language, boolean ignoreCase){
38
        this.languages = language;
39
        this.status.add(status);
40
        this.ignoreCase = ignoreCase;
41

    
42
        if(language == null){
43
            language = Arrays.asList(Language.DEFAULT());
44
        }
45
    }
46

    
47
    public void addStatus(TypeDesignationStatusBase status){
48
        String representation = getLabel();
49
        if(equalByLabel(status)){
50
            this.status.add(status);
51
        } else {
52
            throw new RuntimeException("You must not add a status with different label");
53
        }
54
    }
55

    
56
    public boolean equalByLabel(TypeDesignationStatusBase status){
57

    
58
        String thisRepresentation = getLabel();
59
        String otherRepresentation = status.getPreferredRepresentation(languages).getLabel();
60
        if(ignoreCase){
61
            thisRepresentation = thisRepresentation.toLowerCase();
62
            otherRepresentation = otherRepresentation.toLowerCase();
63
        }
64
        return thisRepresentation.equals(otherRepresentation);
65
    }
66

    
67
    /**
68
     * The localized label of the status terms represented by this filter term
69
     *
70
     * @return
71
     */
72
    public String getLabel() {
73
        if(null == this.status.iterator().next()){
74
            return "- none -";
75
        }
76
        return this.status.iterator().next().getPreferredRepresentation(languages).getLabel();
77
    }
78

    
79
    public String getKey() {
80
        if(ignoreCase){
81
            return getLabel().toLowerCase();
82
        } else {
83
            return getLabel();
84
        }
85
    }
86

    
87
    public Collection<UUID> getUuids() {
88
        List<UUID> uuids = new ArrayList<>();
89
        for(TypeDesignationStatusBase sb : status){
90
            uuids.add(sb.getUuid());
91
        }
92
        return Collections.unmodifiableList(uuids);
93
    }
94

    
95
    @Override
96
    public String toString(){
97
        return getLabel();
98
    }
99

    
100
    /**
101
     * @return
102
     */
103
    private boolean isEmpty() {
104
        return this.status.isEmpty();
105
    }
106

    
107
    private Set<TypeDesignationStatusBase> status() {
108
        return status;
109
    }
110

    
111
    public static Set<TypeDesignationStatusBase> toTypeDesignationStatus(Set<TypeDesignationStatusFilter> filterTerms){
112
        Set<TypeDesignationStatusBase> statusSet = new HashSet<>();
113
        for(TypeDesignationStatusFilter f : filterTerms){
114
            if(f == null){
115
                statusSet.add(null);
116
            } else {
117
                statusSet.addAll(f.status);
118
            }
119
        }
120
        return statusSet;
121
    }
122

    
123
    public static Set<UUID> toTypeDesignationStatusUuids(Set<TypeDesignationStatusFilter> filterTerms){
124
        Set<UUID> uuids = new HashSet<>();
125
        for(TypeDesignationStatusFilter f : filterTerms){
126
            if(f != null){
127
                uuids.addAll(f.getUuids());
128
            } else {
129
                uuids.add(null);
130
            }
131
        }
132
        return uuids;
133
    }
134

    
135
}
(36-36/36)