1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.usertype.moneyandcurrency.joda;
17
18 import java.util.Properties;
19
20 import org.hibernate.SessionFactory;
21 import org.hibernate.usertype.ParameterizedType;
22 import org.jadira.usertype.moneyandcurrency.joda.util.CurrencyUnitConfigured;
23 import org.jadira.usertype.spi.shared.AbstractSingleColumnUserType;
24 import org.jadira.usertype.spi.shared.ColumnMapper;
25 import org.jadira.usertype.spi.shared.ConfigurationHelper;
26 import org.jadira.usertype.spi.shared.IntegratorConfiguredType;
27 import org.jadira.usertype.spi.utils.lang.ConcurrentHashMapBackedProperties;
28 import org.joda.money.CurrencyUnit;
29
30
31
32
33 public abstract class AbstractSingleColumnMoneyUserType<T, J, C extends ColumnMapper<T, J>> extends AbstractSingleColumnUserType<T, J, C> implements ParameterizedType, IntegratorConfiguredType {
34
35 private static final long serialVersionUID = 8244061728586173961L;
36
37 private Properties parameterValues;
38
39 private CurrencyUnit currencyUnit;
40
41 @Override
42 public void setParameterValues(Properties parameters) {
43 this.parameterValues = new ConcurrentHashMapBackedProperties(parameters);
44 }
45
46 protected Properties getParameterValues() {
47 return parameterValues;
48 }
49
50 @Override
51 public void applyConfiguration(SessionFactory sessionFactory) {
52
53 CurrencyUnitConfigured columnMapper = (CurrencyUnitConfigured) getColumnMapper();
54 if (currencyUnit == null) {
55
56 String currencyString = null;
57 if (parameterValues != null) {
58 currencyString = parameterValues.getProperty("currencyCode");
59 }
60 if (currencyString == null) {
61 currencyString = ConfigurationHelper.getProperty("currencyCode");
62 }
63 if (currencyString != null) {
64
65 currencyUnit = CurrencyUnit.of(currencyString);
66 } else {
67 throw new IllegalStateException(getClass().getSimpleName() + " requires currencyCode to be defined as a parameter, or the jadira.usertype.currencyCode Hibernate property to be defined");
68 }
69 }
70 columnMapper.setCurrencyUnit(currencyUnit);
71 }
72 }