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.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   * Base class for money types that do not map a currency column using a configured currency instead.
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  }