1 | |
package org.paneris.bibliomania; |
2 | |
|
3 | |
import java.util.Hashtable; |
4 | |
import java.util.Locale; |
5 | |
|
6 | |
import org.melati.poem.Database; |
7 | |
import org.melati.poem.DefinitionSource; |
8 | |
import org.melati.poem.PoemException; |
9 | |
import org.melati.poem.PoemThread; |
10 | |
import org.paneris.bibliomania.generated.CurrencyTableBase; |
11 | |
|
12 | |
public class CurrencyTable<T extends Currency> extends CurrencyTableBase<Currency> { |
13 | |
|
14 | |
public CurrencyTable( |
15 | |
Database database, |
16 | |
String name, |
17 | |
DefinitionSource definitionSource) |
18 | |
throws PoemException { |
19 | 2 | super(database, name, definitionSource); |
20 | 2 | } |
21 | |
|
22 | |
public Currency ensure(String name, double conversionrate, Locale locale) { |
23 | 2 | Currency c = (Currency)newPersistent(); |
24 | 2 | c.setName(name); |
25 | 2 | c.setConversionrate(conversionrate); |
26 | 2 | c.setLocale(locale.toString()); |
27 | 2 | return (Currency)getNameColumn().ensure(c); |
28 | |
} |
29 | |
|
30 | |
public Currency getCurrency(Locale locale) { |
31 | 0 | return getCurrency(locale.toString()); |
32 | |
} |
33 | |
|
34 | 2 | private Hashtable cache = null; |
35 | 2 | private long cacheSerial = 0L; |
36 | 1 | private static final Object nullEntry = new Object(); |
37 | |
|
38 | |
public Currency getCurrency(String name) { |
39 | 0 | if (cache == null || cacheSerial != serial(PoemThread.transaction())) |
40 | 0 | cache = new Hashtable(); |
41 | 0 | Object value = cache.get(name); |
42 | 0 | if (value == nullEntry) |
43 | 0 | return null; |
44 | 0 | else if (value != null) |
45 | 0 | return (Currency)value; |
46 | |
else { |
47 | 0 | Currency prop = (Currency)getLocaleColumn().firstWhereEq(name); |
48 | 0 | cache.put(name, prop == null ? nullEntry : prop); |
49 | 0 | return prop; |
50 | |
} |
51 | |
} |
52 | |
|
53 | |
} |