View Javadoc

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      super(database, name, definitionSource);
19    }
20  
21    private ShopCurrency UK,US,Euro;
22  
23    public synchronized void unifyWithDB(ResultSet colDescs)
24        throws PoemException {
25      super.unifyWithDB(colDescs);
26      UK = ensure("UK Pound Sterling", 1d, Locale.UK);
27      US = ensure("US Dollar", 1.473, Locale.US);
28      Euro = ensure("Euro", 1.606, Locale.US);
29    }
30  
31    public ShopCurrency ensure(String name, double conversionrate, Locale locale) {
32      ShopCurrency c = (ShopCurrency)newPersistent();
33      c.setName(name);
34      c.setConversionrate(conversionrate);
35      c.setLocale(locale.toString());
36      return (ShopCurrency)getNameColumn().ensure(c);
37    }
38    
39    public ShopCurrency getCurrency(Locale locale) {
40      return getCurrency(locale.toString());
41    }
42     
43    private Hashtable cache = null;
44    private long cacheSerial = 0L;
45    private static final Object nullEntry = new Object();
46  
47    public ShopCurrency getCurrency(String name) {
48      if (cache == null || cacheSerial != serial(PoemThread.transaction()))
49        cache = new Hashtable();
50      Object value = cache.get(name);
51      if (value == nullEntry)
52        return null;
53      else if (value != null)
54        return (ShopCurrency)value;
55      else {
56        ShopCurrency prop =
57            (ShopCurrency)getLocaleColumn().firstWhereEq(name);
58        if (prop == null) {
59  	cache.put(name, nullEntry);
60  	return null;
61        }
62        else {
63  	cache.put(name, prop);
64  	return prop;
65        }
66      }
67    }
68  }