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