Hibernate 4 migration. All test running except for SDDImport (Out of memory)
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / l10n / LocaleContext.java
1 package eu.etaxonomy.cdm.remote.l10n;
2
3 import java.util.Enumeration;
4 import java.util.Hashtable;
5 import java.util.List;
6 import java.util.Locale;
7 import java.util.Vector;
8
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Component;
11
12 import eu.etaxonomy.cdm.api.service.ITermService;
13 import eu.etaxonomy.cdm.model.common.Language;
14
15 @Component
16 public class LocaleContext {
17
18 protected static ThreadLocal<Vector<Locale>> localesHolder = new ThreadLocal<Vector<Locale>>(){
19
20 @Override
21 protected Vector<Locale> initialValue() {
22 return new Vector<Locale>();
23 }
24
25 };
26
27 protected static Hashtable<String, List<Language>> languageMap = new Hashtable<String, List<Language>>();
28
29 private static ITermService termService;
30
31 @Autowired
32 public void setTermService(ITermService termService) {
33 this.termService = termService;
34 }
35
36 public static void setLocales(Enumeration<Locale> locales) {
37 Vector<Locale> l = new Vector<Locale>();
38 while(locales.hasMoreElements()){
39 l.add(locales.nextElement());
40 }
41 LocaleContext.localesHolder.set(l);
42 mapToLanguages(l);
43 }
44
45 public static Enumeration<Locale> getLocales(){
46 return localesHolder.get().elements();
47 }
48
49 /**
50 *
51 * @return
52 */
53 public static List<Language> getLanguages(){
54 String localesKey = composeLocalesKey(getLocales());
55 return languageMap.get(localesKey);
56 }
57
58 private static void mapToLanguages(Vector<Locale> locales) {
59
60 String localesKey = composeLocalesKey(locales.elements());
61
62 if(!languageMap.containsKey(localesKey)){
63 List<Language> languages = termService.getLanguagesByLocale(locales.elements());
64 languageMap.put(localesKey, languages);
65 }
66 }
67
68 private static String composeLocalesKey(Enumeration<Locale> locales) {
69 String localesKey = "";
70 while(locales.hasMoreElements()){
71 localesKey += locales.nextElement() + ",";
72 }
73 return localesKey;
74 }
75
76
77
78 }