Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2018 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.persistence.dto;
11

    
12
import java.io.Serializable;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import eu.etaxonomy.cdm.common.URI;
19
import eu.etaxonomy.cdm.model.common.Language;
20
import eu.etaxonomy.cdm.model.term.Representation;
21
import eu.etaxonomy.cdm.model.term.TermType;
22

    
23
/**
24
 * @author pplitzner
25
 * @date 05.11.2018
26
 */
27
public class AbstractTermDto implements Serializable, Comparable<AbstractTermDto> {
28

    
29
    private static final long serialVersionUID = -7160319884811828125L;
30

    
31
    private UUID uuid;
32
    private URI uri;
33
    private TermType termType;
34
    private Set<Representation> representations = new HashSet<>();
35
    private String representation_L10n = null;
36
    private String representation_L10n_abbreviatedLabel = null;
37
    private String representation_L10n_text = null;
38
    private String titleCache;
39

    
40
    protected AbstractTermDto(){}
41

    
42
    public AbstractTermDto(UUID uuid, Set<Representation> representations, String titleCache) {
43
        this.uuid = uuid;
44
        this.titleCache = titleCache;
45

    
46
        for(Representation rep: representations){
47
            this.representations.add(rep.clone());
48
        }
49
    }
50

    
51
    /**
52
     *
53
     * @param representation_L10n a blank instance of ITermRepresentation_L10n
54
     *   created by the  default constructor
55
     */
56
    public void localize(ITermRepresentation_L10n representation_L10n) {
57

    
58
        representation_L10n.localize(representations);
59
        if (representation_L10n.getLabel() != null) {
60
            setRepresentation_L10n(representation_L10n.getLabel());
61
        }
62
        if (representation_L10n.getAbbreviatedLabel() != null) {
63
            setRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
64
        }
65
        if (representation_L10n.getText() != null) {
66
            setRepresentation_L10n_text(representation_L10n.getText());
67
        }
68
    }
69

    
70
    public URI getUri() {
71
        return uri;
72
    }
73

    
74
    public void setUri(URI uri) {
75
        this.uri = uri;
76
    }
77

    
78
    public TermType getTermType() {
79
        return termType;
80
    }
81

    
82
    public void setTermType(TermType termType) {
83
        this.termType = termType;
84
    }
85

    
86
    public String getRepresentation_L10n() {
87
        return representation_L10n;
88
    }
89

    
90
    public void setRepresentation_L10n(String representation_L10n) {
91
        this.representation_L10n = representation_L10n;
92
    }
93

    
94
    public void addRepresentation(Representation representation) {
95
        representations.add(representation);
96
    }
97

    
98
    public String getRepresentation_L10n_abbreviatedLabel() {
99
        return representation_L10n_abbreviatedLabel;
100
    }
101

    
102
    public void setRepresentation_L10n_abbreviatedLabel(String representation_L10n_abbreviatedLabel) {
103
        this.representation_L10n_abbreviatedLabel = representation_L10n_abbreviatedLabel;
104
    }
105

    
106
    public String getRepresentation_L10n_text() {
107
        return representation_L10n_text;
108
    }
109

    
110
    public void setRepresentation_L10n_text(String representation_L10n_text) {
111
        this.representation_L10n_text = representation_L10n_text;
112
    }
113

    
114
    public UUID getUuid() {
115
        return uuid;
116
    }
117

    
118
    public Set<Representation> getRepresentations() {
119
        return representations;
120
    }
121
    public Representation getPreferredRepresentation(Language lang){
122

    
123
        Language language = lang;
124
        if(lang != null){
125
            language = Language.DEFAULT();
126
        }
127
        for (Representation rep: representations){
128
            if(rep != null && rep.getLanguage().equals(language)){
129
                return rep;
130
            }
131
        }
132
        language = Language.DEFAULT();
133
        for (Representation rep: representations){
134
            if(rep != null && rep.getLanguage().equals(language)){
135
                return rep;
136
            }
137
        }
138

    
139
        Iterator<Representation> it = getRepresentations().iterator();
140
        if(it.hasNext()){
141
            return getRepresentations().iterator().next();
142
        }
143

    
144
        return null;
145
    }
146

    
147
    public Representation getRepresentation(Language lang) {
148
        for (Representation repr : representations){
149
            Language reprLanguage = repr.getLanguage();
150
            if (reprLanguage != null && reprLanguage.equals(lang)){
151
                return repr;
152
            }
153
        }
154
        return null;
155
    }
156

    
157
    public String getTitleCache() {
158
        return titleCache;
159
    }
160

    
161
    public void setTitleCache(String titleCache) {
162
        this.titleCache = titleCache;
163
    }
164

    
165
    @Override
166
    public int hashCode() {
167
        final int prime = 31;
168
        int result = 1;
169
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
170
        return result;
171
    }
172

    
173
    @Override
174
    public boolean equals(Object obj) {
175
        if (this == obj) {
176
            return true;
177
        }
178
        if (obj == null) {
179
            return false;
180
        }
181
        if (getClass() != obj.getClass()) {
182
            return false;
183
        }
184
        AbstractTermDto other = (AbstractTermDto) obj;
185
        if (uuid == null) {
186
            if (other.uuid != null) {
187
                return false;
188
            }
189
        } else if (!uuid.equals(other.uuid)) {
190
            return false;
191
        }
192
        return true;
193
    }
194

    
195
    @Override
196
    public int compareTo(AbstractTermDto o) {
197
        if(o == null){
198
            return 1;
199
        }
200
        if(o.getRepresentation_L10n()!=null){
201
            if(representation_L10n==null){
202
                return -1;
203
            }
204
            else{
205
                return representation_L10n.toLowerCase().compareTo(o.getRepresentation_L10n().toLowerCase());
206
            }
207
        }
208
        else if(representation_L10n!=null){
209
            return 1;
210
        }
211
        return uuid.compareTo(o.getUuid());
212
    }
213
}
(1-1/26)