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

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

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

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

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

    
145
        return null;
146
    }
147

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

    
158

    
159
    public String getTitleCache() {
160
        return titleCache;
161
    }
162

    
163
    public void setTitleCache(String titleCache) {
164
        this.titleCache = titleCache;
165
    }
166

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

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

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