1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.model.common;
|
11
|
|
12
|
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import javax.persistence.Column;
|
19
|
import javax.persistence.Entity;
|
20
|
import javax.xml.bind.annotation.XmlAccessType;
|
21
|
import javax.xml.bind.annotation.XmlAccessorType;
|
22
|
import javax.xml.bind.annotation.XmlAttribute;
|
23
|
import javax.xml.bind.annotation.XmlRootElement;
|
24
|
import javax.xml.bind.annotation.XmlType;
|
25
|
|
26
|
import org.apache.log4j.Logger;
|
27
|
import org.hibernate.envers.Audited;
|
28
|
|
29
|
import au.com.bytecode.opencsv.CSVWriter;
|
30
|
|
31
|
/**
|
32
|
* list of languages according to current internet best practices as given by IANA
|
33
|
* or ISO codes. http://www.ietf.org/rfc/rfc4646.txt
|
34
|
* http://www.loc.gov/standards/iso639-2/php/English_list.php
|
35
|
* @author m.doering
|
36
|
* @version 1.0
|
37
|
* @created 08-Nov-2007 13:06:31
|
38
|
*/
|
39
|
@XmlAccessorType(XmlAccessType.FIELD)
|
40
|
@XmlType(name = "Language")
|
41
|
@XmlRootElement(name = "Language")
|
42
|
@Entity
|
43
|
@Audited
|
44
|
public class Language extends DefinedTermBase<Language> {
|
45
|
private static final long serialVersionUID = -5030610079904074217L;
|
46
|
private static final Logger logger = Logger.getLogger(Language.class);
|
47
|
|
48
|
private static final UUID uuidChinese = UUID.fromString("a9fc2782-5b2a-466f-b9c3-64d9ca6614c4");
|
49
|
public static final UUID uuidEnglish = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
|
50
|
private static final UUID uuidSpanish = UUID.fromString("511d8125-f5e6-445d-aee2-6327375238be");
|
51
|
private static final UUID uuidHindi = UUID.fromString("0a1d9d1d-135d-4575-b172-669b51673c39");
|
52
|
private static final UUID uuidArabic = UUID.fromString("4d3ec2eb-536f-4aab-81c5-34e37a3edbba");
|
53
|
private static final UUID uuidRussian = UUID.fromString("64ea9354-cbf8-40de-9f6e-387d24896f50");
|
54
|
private static final UUID uuidPortuguese = UUID.fromString("c2c08339-2405-4d7d-bd25-cbe01fb7ce09");
|
55
|
private static final UUID uuidJapanese = UUID.fromString("6778c7fb-c195-4dc1-ae3f-164201314e51");
|
56
|
private static final UUID uuidGerman = UUID.fromString("d1131746-e58b-4e80-a865-f5182c9c3073");
|
57
|
private static final UUID uuidFrench = UUID.fromString("7759a1d8-a5ea-454a-8c93-1dcfaae8cc21");
|
58
|
private static final UUID uuidItalian = UUID.fromString("fecbf0c7-fea9-465b-8a16-950517c5c0c4");
|
59
|
private static final UUID uuidDutch = UUID.fromString("9965d79a-acf9-4921-a2c0-863b8c16c056");
|
60
|
private static final UUID uuidPolish = UUID.fromString("3fdca387-f1b0-4ec1-808f-1bc3dc482194");
|
61
|
private static final UUID uuidLatin = UUID.fromString("160a5b6c-87f5-4422-9bda-78cd404c179e");
|
62
|
private static Language ENGLISH;
|
63
|
private static Language LATIN;
|
64
|
private static Language POLISH;
|
65
|
private static Language DUTCH;
|
66
|
private static Language ITALIAN;
|
67
|
private static Language FRENCH;
|
68
|
private static Language GERMAN;
|
69
|
private static Language JAPANESE;
|
70
|
private static Language PORTUGUESE;
|
71
|
private static Language RUSSIAN;
|
72
|
private static Language ARABIC;
|
73
|
private static Language HINDI;
|
74
|
private static Language SPANISH;
|
75
|
private static Language CHINESE;
|
76
|
|
77
|
public static Language NewInstance(){
|
78
|
return new Language();
|
79
|
}
|
80
|
|
81
|
public static Language NewInstance(UUID uuid){
|
82
|
return new Language(uuid);
|
83
|
}
|
84
|
|
85
|
@XmlAttribute(name = "iso639_1")
|
86
|
//TODO create userDefinedType ?
|
87
|
@Column(length=2)
|
88
|
private String iso639_1;
|
89
|
|
90
|
@XmlAttribute(name = "iso639_2")
|
91
|
//TODO create userDefinedType ?
|
92
|
@Column(length=3)
|
93
|
private String iso639_2;
|
94
|
|
95
|
public Language() {
|
96
|
super();
|
97
|
}
|
98
|
public Language(UUID uuid) {
|
99
|
super();
|
100
|
this.setUuid(uuid);
|
101
|
}
|
102
|
public Language(String iso639_1, String iso639_2, String englishLabel, String frenchLabel) throws Exception {
|
103
|
super();
|
104
|
if(iso639_1 != null && iso639_1.length() > 2){
|
105
|
logger.warn("iso639_1 too long: "+iso639_1.toString());
|
106
|
}
|
107
|
if(iso639_1 != null && iso639_2.length() > 3){
|
108
|
logger.warn("iso639_2 too long: "+iso639_2.toString());
|
109
|
}
|
110
|
this.iso639_1=iso639_1;
|
111
|
this.iso639_2=iso639_2;
|
112
|
String textEnglish = englishLabel;
|
113
|
String textFrench = englishLabel;
|
114
|
String label = iso639_2;
|
115
|
String labelAbbrev = null;
|
116
|
this.addRepresentation(new Representation(textEnglish, label, labelAbbrev, Language.ENGLISH()));
|
117
|
this.addRepresentation(new Representation(textFrench, label, labelAbbrev, Language.FRENCH()));
|
118
|
}
|
119
|
public Language(String text, String label, String labelAbbrev, Language lang) {
|
120
|
super();
|
121
|
this.addRepresentation(new Representation(text,label,labelAbbrev, lang));
|
122
|
}
|
123
|
public Language(String label, String text, String labelAbbrev) {
|
124
|
this(label,text,labelAbbrev, DEFAULT());
|
125
|
}
|
126
|
|
127
|
public static final Language DEFAULT(){
|
128
|
return ENGLISH;
|
129
|
}
|
130
|
|
131
|
public static final Language CHINESE(){
|
132
|
return CHINESE;
|
133
|
}
|
134
|
|
135
|
public static final Language ENGLISH(){
|
136
|
return ENGLISH;
|
137
|
}
|
138
|
|
139
|
public static final Language SPANISH(){
|
140
|
return SPANISH;
|
141
|
}
|
142
|
|
143
|
public static final Language HINDI(){
|
144
|
return HINDI;
|
145
|
}
|
146
|
|
147
|
public static final Language ARABIC(){
|
148
|
return ARABIC;
|
149
|
}
|
150
|
|
151
|
public static final Language RUSSIAN(){
|
152
|
return RUSSIAN;
|
153
|
}
|
154
|
|
155
|
public static final Language PORTUGUESE(){
|
156
|
return PORTUGUESE;
|
157
|
}
|
158
|
|
159
|
public static final Language JAPANESE(){
|
160
|
return JAPANESE;
|
161
|
}
|
162
|
|
163
|
public static final Language GERMAN(){
|
164
|
return GERMAN;
|
165
|
}
|
166
|
|
167
|
public static final Language FRENCH(){
|
168
|
return FRENCH;
|
169
|
}
|
170
|
|
171
|
public static final Language ITALIAN(){
|
172
|
return ITALIAN;
|
173
|
}
|
174
|
|
175
|
public static final Language DUTCH(){
|
176
|
return DUTCH;
|
177
|
}
|
178
|
|
179
|
public static final Language POLISH(){
|
180
|
return POLISH;
|
181
|
}
|
182
|
|
183
|
public static final Language LATIN(){
|
184
|
return LATIN;
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Get the according iso639-1 alpha-2 language code
|
189
|
* http://www.loc.gov/standards/iso639-2/
|
190
|
*
|
191
|
* @return the iso639 alpha-2 language code or null if not available
|
192
|
*/
|
193
|
public String getIso639_1() {
|
194
|
return iso639_1;
|
195
|
}
|
196
|
|
197
|
public void setIso639_1(String iso639_1) {
|
198
|
iso639_1 = iso639_1.trim();
|
199
|
if(iso639_1.length() > 2){
|
200
|
logger.warn("Iso639-1: "+iso639_1+" too long");
|
201
|
}
|
202
|
this.iso639_1 = iso639_1;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Get the according iso639-2 alpha-3 language code
|
207
|
* http://www.loc.gov/standards/iso639-2/
|
208
|
*
|
209
|
* @return the iso639 alpha-3 language code or null if not available
|
210
|
*/
|
211
|
public String getIso639_2() {
|
212
|
return iso639_2;
|
213
|
}
|
214
|
|
215
|
public void setIso639_2(String iso639_2) {
|
216
|
iso639_2 = iso639_2.trim();
|
217
|
if(iso639_2.length() > 3 ){
|
218
|
logger.warn("Iso639-2: "+iso639_2+" too long");
|
219
|
}
|
220
|
this.iso639_2 = iso639_2;
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public Language readCsvLine(Class<Language> termClass, List<String> csvLine, Map<UUID,DefinedTermBase> terms) {
|
225
|
try {
|
226
|
Language newInstance = Language.class.newInstance();
|
227
|
if ( UUID.fromString(csvLine.get(0).toString()).equals(Language.uuidEnglish)){
|
228
|
DefinedTermBase.readCsvLine(newInstance, csvLine, newInstance);
|
229
|
}else{
|
230
|
DefinedTermBase.readCsvLine(newInstance,csvLine,(Language)terms.get(Language.uuidEnglish));
|
231
|
}
|
232
|
|
233
|
newInstance.setIso639_1(csvLine.get(5).trim());
|
234
|
newInstance.setIso639_2(csvLine.get(4).trim());
|
235
|
//TODO could replace with generic validation
|
236
|
if(iso639_1 != null && iso639_1.length() > 2){
|
237
|
logger.warn("Iso639-1: "+ newInstance.getIso639_1() +" from "+csvLine.get(3)+" ,"+csvLine.get(2)+" too long");
|
238
|
}
|
239
|
if(iso639_2 != null && iso639_2.length() > 3 ){
|
240
|
logger.warn("Iso639-2: "+newInstance.getIso639_2()+" from "+csvLine.get(3)+" ,"+csvLine.get(2)+" too long");
|
241
|
}
|
242
|
|
243
|
return newInstance;
|
244
|
} catch (Exception e) {
|
245
|
logger.error(e);
|
246
|
return null;
|
247
|
}
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public void writeCsvLine(CSVWriter writer, Language language) {
|
252
|
String [] line = new String[6];
|
253
|
line[0] = language.getUuid().toString();
|
254
|
line[1] = language.getUri();
|
255
|
line[2] = language.getLabel(Language.ENGLISH());
|
256
|
line[3] = language.getDescription(Language.ENGLISH());
|
257
|
line[4] = language.getIso639_1();
|
258
|
line[5] = language.getIso639_2();
|
259
|
writer.writeNext(line);
|
260
|
}
|
261
|
|
262
|
/* (non-Javadoc)
|
263
|
* @see eu.etaxonomy.cdm.model.common.TermBase#toString()
|
264
|
*/
|
265
|
@Override
|
266
|
public String toString() {
|
267
|
if (this.getLabel() != null){
|
268
|
return this.getLabel();
|
269
|
}else{
|
270
|
return super.toString();
|
271
|
}
|
272
|
}
|
273
|
|
274
|
@Override
|
275
|
protected void setDefaultTerms(TermVocabulary<Language> termVocabulary) {
|
276
|
Language.ARABIC = termVocabulary.findTermByUuid(Language.uuidArabic);
|
277
|
Language.CHINESE = termVocabulary.findTermByUuid(Language.uuidChinese);
|
278
|
Language.DUTCH = termVocabulary.findTermByUuid(Language.uuidDutch);
|
279
|
Language.ENGLISH = termVocabulary.findTermByUuid(Language.uuidEnglish);
|
280
|
Language.FRENCH = termVocabulary.findTermByUuid(Language.uuidFrench);
|
281
|
Language.GERMAN = termVocabulary.findTermByUuid(Language.uuidGerman);
|
282
|
Language.HINDI = termVocabulary.findTermByUuid(Language.uuidHindi);
|
283
|
Language.ITALIAN = termVocabulary.findTermByUuid(Language.uuidItalian);
|
284
|
Language.JAPANESE = termVocabulary.findTermByUuid(Language.uuidJapanese);
|
285
|
Language.LATIN = termVocabulary.findTermByUuid(Language.uuidLatin);
|
286
|
Language.POLISH = termVocabulary.findTermByUuid(Language.uuidPolish);
|
287
|
Language.PORTUGUESE = termVocabulary.findTermByUuid(Language.uuidPortuguese);
|
288
|
Language.RUSSIAN = termVocabulary.findTermByUuid(Language.uuidRussian);
|
289
|
Language.SPANISH = termVocabulary.findTermByUuid(Language.uuidSpanish);
|
290
|
addLanguageForVocabularyRepresentation(termVocabulary);
|
291
|
}
|
292
|
|
293
|
private void addLanguageForVocabularyRepresentation(TermVocabulary<Language> termVocabulary){
|
294
|
for (Representation repr : termVocabulary.getRepresentations()){
|
295
|
Language lang = repr.getLanguage();
|
296
|
if (lang == null){
|
297
|
repr.setLanguage(Language.DEFAULT());
|
298
|
}
|
299
|
}
|
300
|
}
|
301
|
|
302
|
|
303
|
|
304
|
}
|