View Javadoc
1   /*
2    *  Copyright 2010, 2011, 2012 Christopher Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Base class for money types that do not map a currency column using a configured currency instead.
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  }