Project

General

Profile

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
		if(logger.isDebugEnabled()) {
49
			for (Map.Entry<String, Vocabulary> vocabularyEntry : vocabularyMap.entrySet()) {
50
				logger.debug("vocabulary names " + vocabularyEntry.getValue().getName());
51
			}
52
		}
53
	}
54
	
55
	public String translate(String name, String l, String encoding, String default_value){
56
		Locale locale = new Locale(l);
57
        //logger.debug("Translating... " + name);
58
		eu.dnetlib.domain.enabling.Vocabulary vocabulary = getVocabulary(name, locale);
59
		
60
		if(vocabulary == null) {	
61
			return (default_value.length() == 0)? encoding: default_value;
62
		}
63
		
64
		String term = vocabulary.getEnglishName(encoding);
65
		if(term != null && term != "") {
66
			return term;
67
		}
68
		
69
		vocabulary = getVocabulary(name, config.getDefaultLocale());
70
		
71
		if(vocabulary == null) {
72
			return (default_value.length() == 0)? encoding: default_value;
73
		}
74
		term = vocabulary.getEnglishName(encoding);
75

    
76
		if(term != null && term != ""){
77
			return term;
78
		}
79
			
80
		return (default_value.length() == 0)? encoding: default_value;
81
	}
82
	
83
	public eu.dnetlib.domain.enabling.Vocabulary getVocabulary(String vocabularyName, Locale locale) {
84
		eu.dnetlib.domain.enabling.Vocabulary myVocabulary = null;
85
		
86
		//if vocabulary loader does not exist, return null
87
		VocabularyLoader vocabularyLoader = vocabularyLoaderMap.get(vocabularyName);
88
		if (vocabularyLoader == null) 
89
			return null;
90
		
91
		//if locale is not in the configuration file, change it with the default
92
		if (!config.getLocales().contains(locale))
93
			locale = config.getDefaultLocale();
94
		
95
		//if it is an index vocabulary use the default locale only!
96
		if (indexVocabularies.contains(vocabularyName))
97
			locale = config.getDefaultLocale();
98
		
99
		//if vocabulary does not exist, return null
100
		Vocabulary vocabulary = vocabularyMap.get(vocabularyName);
101
		if (vocabulary == null)
102
			return null;
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
		//if locale is not in the configuration file, change it with the default
138
		if (!config.getLocales().contains(locale))
139
			locale = config.getDefaultLocale();
140
		
141
		//if it is an index vocabulary use the default locale only!
142
		if (indexVocabularies.contains(vocabularyName))
143
			locale = config.getDefaultLocale();
144
		
145
		//if vocabulary does not exist, return null
146
		Vocabulary vocabulary = vocabularyMap.get(vocabularyName);
147
        //logger.debug("vocabulary map " + vocabularyMap.keySet());
148
        //logger.debug("loading vocabulary " + vocabularyName);
149
        //logger.debug("vocabulary " + vocabulary);
150

    
151
		if (vocabulary == null)
152
			return null;
153
		
154
		Map<Locale, eu.dnetlib.domain.enabling.Vocabulary> localePathMap = pathMap.get(vocabularyName);
155
		
156
		try {
157
			if (localePathMap == null) { //first time to load the vocabulary
158
				localePathMap = new HashMap<Locale, eu.dnetlib.domain.enabling.Vocabulary>();				
159
				myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
160
				localePathMap.put(locale, myVocabulary);
161
				pathMap.put(vocabularyName, localePathMap);
162
			
163
			} else {
164
				myVocabulary = localePathMap.get(locale);
165
				
166
				if (myVocabulary == null || loadanyway) { //first time to load the vocabulary for this locale, or load anyway
167
					myVocabulary = vocabularyLoader.loadVocabulary(vocabulary, locale);
168
					localePathMap.put(locale, myVocabulary);
169
				}
170
			}
171
			
172
		} catch (Exception e) {
173
			logger.error("Cannot load vocabulary " + vocabularyName + " with locale " + locale, e);
174
		}
175

    
176
		return myVocabulary;
177
	}
178

    
179
	public void setConfig(Configuration config) {
180
		this.config = config;
181
	}
182

    
183
	public void setIsVocabularyLoader(ISVocabularyLoader isVocabularyLoader) {
184
		this.isVocabularyLoader = isVocabularyLoader;
185
	}
186

    
187
	public void setIndexVocabularyLoader(IndexVocabularyLoader indexVocabularyLoader) {
188
		this.indexVocabularyLoader = indexVocabularyLoader;
189
	}
190

    
191
	public void setLocalVocabularyLoader(LocalVocabularyLoader localVocabularyLoader) {
192
		this.localVocabularyLoader = localVocabularyLoader;
193
	}
194

    
195
	public Map<String, Vocabulary> getVocabulariesMap(){
196
		return this.vocabularyMap;
197
	}
198
	
199
	@Override
200
	public Set<String> getVocabularyNames() {
201
		return this.vocabularyMap.keySet();
202
	}
203

    
204
	@Override
205
	public List<Locale> getLocales(){
206
		return this.config.getLocales();
207
	}
208
	
209
}
(14-14/15)