1 | |
package org.paneris.jammyjoes.model; |
2 | |
|
3 | |
import java.sql.ResultSet; |
4 | |
import java.util.Hashtable; |
5 | |
import java.util.Locale; |
6 | |
|
7 | |
import org.melati.poem.Database; |
8 | |
import org.melati.poem.DefinitionSource; |
9 | |
import org.melati.poem.PoemException; |
10 | |
import org.melati.poem.PoemThread; |
11 | |
import org.paneris.jammyjoes.model.generated.ShopCurrencyTableBase; |
12 | |
|
13 | |
public class ShopCurrencyTable extends ShopCurrencyTableBase { |
14 | |
|
15 | |
public ShopCurrencyTable( |
16 | |
Database database, String name, |
17 | |
DefinitionSource definitionSource) throws PoemException { |
18 | 0 | super(database, name, definitionSource); |
19 | 0 | } |
20 | |
|
21 | |
private ShopCurrency UK,US,Euro; |
22 | |
|
23 | |
public synchronized void unifyWithDB(ResultSet colDescs) |
24 | |
throws PoemException { |
25 | 0 | super.unifyWithDB(colDescs); |
26 | 0 | UK = ensure("UK Pound Sterling", 1d, Locale.UK); |
27 | 0 | US = ensure("US Dollar", 1.473, Locale.US); |
28 | 0 | Euro = ensure("Euro", 1.606, Locale.US); |
29 | 0 | } |
30 | |
|
31 | |
public ShopCurrency ensure(String name, double conversionrate, Locale locale) { |
32 | 0 | ShopCurrency c = (ShopCurrency)newPersistent(); |
33 | 0 | c.setName(name); |
34 | 0 | c.setConversionrate(conversionrate); |
35 | 0 | c.setLocale(locale.toString()); |
36 | 0 | return (ShopCurrency)getNameColumn().ensure(c); |
37 | |
} |
38 | |
|
39 | |
public ShopCurrency getCurrency(Locale locale) { |
40 | 0 | return getCurrency(locale.toString()); |
41 | |
} |
42 | |
|
43 | 0 | private Hashtable cache = null; |
44 | 0 | private long cacheSerial = 0L; |
45 | 0 | private static final Object nullEntry = new Object(); |
46 | |
|
47 | |
public ShopCurrency getCurrency(String name) { |
48 | 0 | if (cache == null || cacheSerial != serial(PoemThread.transaction())) |
49 | 0 | cache = new Hashtable(); |
50 | 0 | Object value = cache.get(name); |
51 | 0 | if (value == nullEntry) |
52 | 0 | return null; |
53 | 0 | else if (value != null) |
54 | 0 | return (ShopCurrency)value; |
55 | |
else { |
56 | 0 | ShopCurrency prop = |
57 | |
(ShopCurrency)getLocaleColumn().firstWhereEq(name); |
58 | 0 | if (prop == null) { |
59 | 0 | cache.put(name, nullEntry); |
60 | 0 | return null; |
61 | |
} |
62 | |
else { |
63 | 0 | cache.put(name, prop); |
64 | 0 | return prop; |
65 | |
} |
66 | |
} |
67 | |
} |
68 | |
} |