1
|
package eu.dnetlib.data.search.utils.vocabulary;
|
2
|
|
3
|
import eu.dnetlib.data.search.transform.config.Configuration;
|
4
|
import org.apache.log4j.Logger;
|
5
|
|
6
|
import java.util.*;
|
7
|
|
8
|
public class VocabularyManagerImpl implements VocabularyManager {
|
9
|
|
10
|
private Configuration config = null;
|
11
|
|
12
|
//key-> vocabulary name, value-> vocabulary loader
|
13
|
private Map<String, VocabularyLoader> vocabularyLoaderMap = new HashMap<String, VocabularyLoader>();
|
14
|
|
15
|
//key-> vocabulary name, value-> vocabulary item
|
16
|
//this vocabulary class contains the fields needed to load the domain.Vocab object
|
17
|
private Map<String, Vocabulary> vocabularyMap = new HashMap<String, Vocabulary>();
|
18
|
|
19
|
//key-> vocabulary name, value-> vocabulary object per locale
|
20
|
private Map<String, Map<Locale, eu.dnetlib.domain.enabling.Vocabulary>> pathMap = new HashMap<String, Map<Locale, eu.dnetlib.domain.enabling.Vocabulary>>();
|
21
|
|
22
|
private List<String> indexVocabularies = new ArrayList<String>();
|
23
|
|
24
|
private ISVocabularyLoader isVocabularyLoader;
|
25
|
private IndexVocabularyLoader indexVocabularyLoader;
|
26
|
private LocalVocabularyLoader localVocabularyLoader;
|
27
|
|
28
|
private static Logger logger = Logger.getLogger(VocabularyManagerImpl.class);
|
29
|
|
30
|
public void init() {
|
31
|
for(String vocabularyName: config.getIsVocabularyMap().keySet()) {
|
32
|
vocabularyLoaderMap.put(vocabularyName, isVocabularyLoader);
|
33
|
vocabularyMap.put(vocabularyName, config.getIsVocabularyMap().get(vocabularyName));
|
34
|
}
|
35
|
|
36
|
for(String vocabularyName: config.getIndexVocabularyMap().keySet()) {
|
37
|
vocabularyLoaderMap.put(vocabularyName, indexVocabularyLoader);
|
38
|
vocabularyMap.put(vocabularyName, config.getIndexVocabularyMap().get(vocabularyName));
|
39
|
|
40
|
indexVocabularies.add(vocabularyName);
|
41
|
}
|
42
|
|
43
|
for(String vocabularyName: config.getLocalVocabularyMap().keySet()) {
|
44
|
vocabularyLoaderMap.put(vocabularyName, localVocabularyLoader);
|
45
|
vocabularyMap.put(vocabularyName, config.getLocalVocabularyMap().get(vocabularyName));
|
46
|
}
|
47
|
|
48
|
}
|
49
|
|
50
|
public String translate(String name, String l, String encoding, String default_value){
|
51
|
Locale locale = new Locale(l);
|
52
|
//logger.debug("Translating... " + name);
|
53
|
eu.dnetlib.domain.enabling.Vocabulary vocabulary = getVocabulary(name, locale);
|
54
|
|
55
|
if(vocabulary == null) {
|
56
|
return (default_value.length() == 0)? encoding: default_value;
|
57
|
}
|
58
|
|
59
|
String term = vocabulary.getEnglishName(encoding);
|
60
|
|
61
|
if(term != null && !term.trim().isEmpty()) {
|
62
|
return term;
|
63
|
}
|
64
|
|
65
|
vocabulary = getVocabulary(name, config.getDefaultLocale());
|
66
|
|
67
|
if(vocabulary == null) {
|
68
|
return (default_value.length() == 0)? encoding: default_value;
|
69
|
}
|
70
|
term = vocabulary.getEnglishName(encoding);
|
71
|
|
72
|
if(term != null && !term.trim().isEmpty()) {
|
73
|
return term;
|
74
|
}
|
75
|
|
76
|
return (default_value.length() == 0)? encoding: default_value;
|
77
|
}
|
78
|
|
79
|
public eu.dnetlib.domain.enabling.Vocabulary getVocabulary(String vocabularyName, Locale locale) {
|
80
|
eu.dnetlib.domain.enabling.Vocabulary myVocabulary = null;
|
81
|
|
82
|
//if vocabulary loader does not exist, return null
|
83
|
VocabularyLoader vocabularyLoader = vocabularyLoaderMap.get(vocabularyName);
|
84
|
if (vocabularyLoader == null) {
|
85
|
return null;
|
86
|
}
|
87
|
|
88
|
//if locale is not in the configuration file, change it with the default
|
89
|
if (!config.getLocales().contains(locale)) {
|
90
|
locale = config.getDefaultLocale();
|
91
|
}
|
92
|
|
93
|
//if it is an index vocabulary use the default locale only!
|
94
|
if (indexVocabularies.contains(vocabularyName)) {
|
95
|
locale = config.getDefaultLocale();
|
96
|
}
|
97
|
|
98
|
//if vocabulary does not exist, return null
|
99
|
Vocabulary vocabulary = vocabularyMap.get(vocabularyName);
|
100
|
if (vocabulary == null) {
|
101
|
return null;
|
102
|
}
|
103
|
|
104
|
Map<Locale, eu.dnetlib.domain.enabling.Vocabulary> localePathMap = pathMap.get(vocabularyName);
|
105
|
|
106
|
try {
|
107
|
if (localePathMap == null) { //first time to load the vocabulary
|
108
|
localePathMap = new HashMap<Locale, eu.dnetlib.domain.enabling.Vocabulary>();
|
109
|
myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
|
110
|
localePathMap.put(locale, myVocabulary);
|
111
|
pathMap.put(vocabularyName, localePathMap);
|
112
|
|
113
|
} else {
|
114
|
myVocabulary = localePathMap.get(locale);
|
115
|
|
116
|
if (myVocabulary == null) { //first time to load the vocabulary for this locale
|
117
|
myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
|
118
|
localePathMap.put(locale, myVocabulary);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
} catch (Exception e) {
|
123
|
logger.error("Cannot load vocabulary " + vocabularyName + " with locale " + locale, e);
|
124
|
}
|
125
|
|
126
|
return myVocabulary;
|
127
|
}
|
128
|
|
129
|
public eu.dnetlib.domain.enabling.Vocabulary getVocabulary(String vocabularyName, Locale locale, boolean loadanyway) {
|
130
|
eu.dnetlib.domain.enabling.Vocabulary myVocabulary = null;
|
131
|
|
132
|
//if vocabulary loader does not exist, return null
|
133
|
VocabularyLoader vocabularyLoader = vocabularyLoaderMap.get(vocabularyName);
|
134
|
if (vocabularyLoader == null) {
|
135
|
return null;
|
136
|
}
|
137
|
|
138
|
//if locale is not in the configuration file, change it with the default
|
139
|
if (!config.getLocales().contains(locale)) {
|
140
|
locale = config.getDefaultLocale();
|
141
|
}
|
142
|
|
143
|
//if it is an index vocabulary use the default locale only!
|
144
|
if (indexVocabularies.contains(vocabularyName)) {
|
145
|
locale = config.getDefaultLocale();
|
146
|
}
|
147
|
|
148
|
//if vocabulary does not exist, return null
|
149
|
Vocabulary vocabulary = vocabularyMap.get(vocabularyName);
|
150
|
//logger.debug("vocabulary map " + vocabularyMap.keySet());
|
151
|
//logger.debug("loading vocabulary " + vocabularyName);
|
152
|
//logger.debug("vocabulary " + vocabulary);
|
153
|
|
154
|
if (vocabulary == null) {
|
155
|
return null;
|
156
|
}
|
157
|
|
158
|
Map<Locale, eu.dnetlib.domain.enabling.Vocabulary> localePathMap = pathMap.get(vocabularyName);
|
159
|
|
160
|
try {
|
161
|
if (localePathMap == null) { //first time to load the vocabulary
|
162
|
localePathMap = new HashMap<Locale, eu.dnetlib.domain.enabling.Vocabulary>();
|
163
|
myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
|
164
|
localePathMap.put(locale, myVocabulary);
|
165
|
pathMap.put(vocabularyName, localePathMap);
|
166
|
|
167
|
} else {
|
168
|
myVocabulary = localePathMap.get(locale);
|
169
|
|
170
|
if (myVocabulary == null || loadanyway) { //first time to load the vocabulary for this locale, or load anyway
|
171
|
myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
|
172
|
localePathMap.put(locale, myVocabulary);
|
173
|
}
|
174
|
}
|
175
|
|
176
|
} catch (Exception e) {
|
177
|
logger.error("Cannot load vocabulary " + vocabularyName + " with locale " + locale, e);
|
178
|
}
|
179
|
|
180
|
return myVocabulary;
|
181
|
}
|
182
|
|
183
|
public void setConfig(Configuration config) {
|
184
|
this.config = config;
|
185
|
}
|
186
|
|
187
|
public void setIsVocabularyLoader(ISVocabularyLoader isVocabularyLoader) {
|
188
|
this.isVocabularyLoader = isVocabularyLoader;
|
189
|
}
|
190
|
|
191
|
public void setIndexVocabularyLoader(IndexVocabularyLoader indexVocabularyLoader) {
|
192
|
this.indexVocabularyLoader = indexVocabularyLoader;
|
193
|
}
|
194
|
|
195
|
public void setLocalVocabularyLoader(LocalVocabularyLoader localVocabularyLoader) {
|
196
|
this.localVocabularyLoader = localVocabularyLoader;
|
197
|
}
|
198
|
|
199
|
public Map<String, Vocabulary> getVocabulariesMap(){
|
200
|
return this.vocabularyMap;
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public Set<String> getVocabularyNames() {
|
205
|
return this.vocabularyMap.keySet();
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public List<Locale> getLocales(){
|
210
|
return this.config.getLocales();
|
211
|
}
|
212
|
|
213
|
}
|