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.unitsofmeasurement.indriya;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.measure.Unit;
22  
23  import org.hibernate.SessionFactory;
24  import org.jadira.usertype.spi.shared.AbstractParameterizedUserType;
25  import org.jadira.usertype.spi.shared.ValidTypesConfigured;
26  import org.jadira.usertype.spi.utils.reflection.ClassLoaderUtils;
27  import org.jadira.usertype.unitsofmeasurement.indriya.columnmapper.StringColumnUnitMapper;
28  
29  /**
30   * Persists a Unit using its assigned Symbol. Out of the box, the supported units are those defined in the generally used System of Units.
31   * If you want to add additional units, you must specify them using the parameter 'validTypes', where they can be specified as a comma separated
32   * list of class names.
33   */
34  public class PersistentUnit extends AbstractParameterizedUserType<Unit<?>, String, StringColumnUnitMapper> {
35  
36  	private static final long serialVersionUID = -2015829087239519037L;
37  	
38  	@Override
39  	public void applyConfiguration(SessionFactory sessionFactory) {
40  		super.applyConfiguration(sessionFactory);
41  		doApplyConfiguration();
42      }
43      
44  	private <Z> void doApplyConfiguration() {
45  
46  //		if (ValidTypesConfigured.class.isAssignableFrom(this.getClass())) {
47  //				
48  //			ValidTypesConfigured<Unit<?>> next = (ValidTypesConfigured<Unit<?>>)this;			
49  //			performValidTypesConfiguration(next);
50  //		}
51  //		if (ValidTypesConfigured.class.isAssignableFrom(getColumnMapper().getClass())) {			
52  		ValidTypesConfigured<Unit<?>> next = (ValidTypesConfigured<Unit<?>>)getColumnMapper();
53  		performValidTypesConfiguration(next);
54  //		}
55  	}
56  	
57  	private void performValidTypesConfiguration(ValidTypesConfigured<Unit<?>> next) {
58  		
59          String validTypesString = null;
60          if (getParameterValues() != null) {
61          	validTypesString = getParameterValues().getProperty("validTypes");
62          }
63  		if (validTypesString != null) {
64  			String[] validTypes = validTypesString.split(",");
65  			List<Class<Unit<?>>> units = new ArrayList<>();
66  			for (String nextType : validTypes) {
67  				
68  				
69  				try {
70  					@SuppressWarnings("unchecked")
71  					Class<Unit<?>> nextClass = (Class<Unit<?>>)(ClassLoaderUtils.classForName(nextType));
72  					units.add(nextClass);
73  				} catch (ClassNotFoundException e) {
74  					throw new IllegalStateException("Cannot find specified class " + nextType, e);
75  				}
76  				
77  			}
78  			next.setValidTypes(units);
79  		}
80  	}
81  }