Project

General

Profile

Download (5.96 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
        if (representations != null){
47
            for(Representation rep: representations){
48
                this.representations.add(rep.clone());
49
            }
50
        }
51
    }
52

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

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

    
72
    public URI getUri() {
73
        return uri;
74
    }
75

    
76
    public void setUri(URI uri) {
77
        this.uri = uri;
78
    }
79

    
80
    public TermType getTermType() {
81
        return termType;
82
    }
83

    
84
    public void setTermType(TermType termType) {
85
        this.termType = termType;
86
    }
87

    
88
    public String getRepresentation_L10n() {
89
        return representation_L10n;
90
    }
91

    
92
    public void setRepresentation_L10n(String representation_L10n) {
93
        this.representation_L10n = representation_L10n;
94
    }
95

    
96
    public void addRepresentation(Representation representation) {
97
        representations.add(representation);
98
    }
99

    
100
    public String getRepresentation_L10n_abbreviatedLabel() {
101
        return representation_L10n_abbreviatedLabel;
102
    }
103

    
104
    public void setRepresentation_L10n_abbreviatedLabel(String representation_L10n_abbreviatedLabel) {
105
        this.representation_L10n_abbreviatedLabel = representation_L10n_abbreviatedLabel;
106
    }
107

    
108
    public String getRepresentation_L10n_text() {
109
        return representation_L10n_text;
110
    }
111

    
112
    public void setRepresentation_L10n_text(String representation_L10n_text) {
113
        this.representation_L10n_text = representation_L10n_text;
114
    }
115

    
116

    
117
    public UUID getUuid() {
118
        return uuid;
119
    }
120

    
121
    public Set<Representation> getRepresentations() {
122
        return representations;
123
    }
124
    public Representation getPreferredRepresentation(Language lang){
125

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

    
142
        Iterator<Representation> it = getRepresentations().iterator();
143
        if(it.hasNext()){
144
            return getRepresentations().iterator().next();
145
        }
146

    
147
        return null;
148
    }
149

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

    
160

    
161
    public String getTitleCache() {
162
        return titleCache;
163
    }
164

    
165
    public void setTitleCache(String titleCache) {
166
        this.titleCache = titleCache;
167
    }
168

    
169
    @Override
170
    public int hashCode() {
171
        final int prime = 31;
172
        int result = 1;
173
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
174
        return result;
175
    }
176

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

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