View Javadoc

1   /*
2    * Joey and its relative products are published under the terms
3    * of the Apache Software License.
4    */
5   /*
6    * Created on 2004/02/27
7    */
8   package org.asyrinx.joey.om.hibernate;
9   
10  import java.math.BigInteger;
11  import java.util.ArrayList;
12  import java.util.Collection;
13  import java.util.HashSet;
14  import java.util.Iterator;
15  import java.util.Map;
16  import java.util.Set;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.asyrinx.brownie.core.lang.ArrayUtils;
22  import org.asyrinx.brownie.core.lang.UnsupportedClassRuntimeException;
23  
24  /***
25   * @author akima
26   */
27  public abstract class AbstractHibernateTranslator {
28  
29  	/***
30  	 * 
31  	 */
32  	public AbstractHibernateTranslator(
33  		Map columnToProperty,
34  		Map tableToClass) {
35  		super();
36  		this.columnToProperty = columnToProperty;
37  		this.tableToClass = tableToClass;
38  	}
39  
40  	/***
41  	 * 
42  	 */
43  	public AbstractHibernateTranslator(
44  		Map columnToProperty,
45  		Map tableToClass,
46  		boolean usePropertyInCondition) {
47  		super();
48  		this.columnToProperty = columnToProperty;
49  		this.tableToClass = tableToClass;
50  		this.usePropertyInCondition = usePropertyInCondition;
51  	}
52  
53  	private boolean usePropertyInCondition = false;
54  	protected final Map columnToProperty;
55  	protected final Map tableToClass;
56  
57  	protected String toClass(String tableName) {
58  		final String result = String.valueOf(tableToClass.get(tableName));
59  		return StringUtils.isEmpty(result) ? tableName : result;
60  	}
61  
62  	protected String toFieldName(String columnName) {
63  		if (this.isUsePropertyInCondition()) {
64  			final String result =
65  				String.valueOf(columnToProperty.get(columnName));
66  			return StringUtils.isEmpty(result) ? columnName : result;
67  		} else {
68  			final int idx = columnName.indexOf('.');
69  			return (idx > -1) ? columnName.substring(idx + 1) : columnName;
70  		}
71  	}
72  
73  	protected final Log log = LogFactory.getLog(this.getClass());
74  
75  	/***
76  	 * @return
77  	 */
78  	public boolean isUsePropertyInCondition() {
79  		return usePropertyInCondition;
80  	}
81  
82  	/***
83  	 * @param b
84  	 */
85  	public void setUsePropertyInCondition(boolean b) {
86  		usePropertyInCondition = b;
87  	}
88  
89  	protected final Object toNumber(Object value, Class numberClass) {
90  		if (value == null)
91  			return null;
92  		if (value instanceof Number)
93  			return (Number) value;
94  		else if (value.getClass().isArray()) {
95  			if (value.getClass().getComponentType().isPrimitive())
96  				value = ArrayUtils.toObjectArray(value);
97  			if (Number
98  				.class
99  				.isAssignableFrom(value.getClass().getComponentType())) {
100 				return value;
101 			} else {
102 				final Object[] objects = (Object[]) value;
103 				final Object[] result = new Object[objects.length];
104 				for (int i = 0; i < objects.length; i++) {
105 					result[i] = toNumber(objects[i], numberClass);
106 				}
107 				return result;
108 			}
109 		} else if (value instanceof Collection) {
110 			final Collection result;
111 			if (value instanceof Set)
112 				result = new HashSet();
113 			else
114 				result = new ArrayList();
115 			final Iterator iterator = ((Collection) value).iterator();
116 			while (iterator.hasNext()) {
117 				result.add(toNumber(iterator.next(), numberClass));
118 			}
119 			return result;
120 		} else if (value instanceof String) {
121 			final String strValue = (String) value;
122 			if (StringUtils.isEmpty(strValue))
123 				return null;
124 			if (numberClass == Byte.class)
125 				return new Byte(strValue);
126 			else if (numberClass == Short.class)
127 				return new Short(strValue);
128 			else if (numberClass == Integer.class)
129 				return new Integer(strValue);
130 			else if (numberClass == Long.class)
131 				return new Long(strValue);
132 			else if (numberClass == BigInteger.class)
133 				return new BigInteger(strValue);
134 			else if (numberClass == Float.class)
135 				return new Float(strValue);
136 			else if (numberClass == Double.class)
137 				return new Double(strValue);
138 			else
139 				return new Long(strValue);
140 		} else {
141 			throw new UnsupportedClassRuntimeException(
142 				"cannot convert to Number: " + value.getClass().getName());
143 		}
144 	}
145 
146 }