001: package com.completex.objective.components.persistency.type;
002:
003: import com.completex.objective.util.TypeUtil;
004: import com.completex.objective.components.OdalRuntimeException;
005:
006: import java.sql.SQLException;
007: import java.sql.Clob;
008: import java.math.BigDecimal;
009: import java.math.BigInteger;
010:
011: /**
012: * @author Gennady Krizhevsky
013: */
014: public class TypeConverterImpl implements TypeConverter {
015:
016: private int booleanFormat = TypeUtil.Y_N;
017:
018: public int getBooleanFormat() {
019: return booleanFormat;
020: }
021:
022: public void setBooleanFormat(int booleanFormat) {
023: this .booleanFormat = booleanFormat;
024: }
025:
026: public long objectToLongPrimitive(Object value) {
027: if (value == null) {
028: return 0;
029: }
030: if (value instanceof Number) {
031: return TypeUtil.Number2long((Number) value);
032: } else if (value instanceof String) {
033: return TypeUtil.S2L((String) value).longValue();
034: } else {
035: throw new IllegalArgumentException("Cannot convert value "
036: + value + " to long");
037: }
038: }
039:
040: public int objectToIntPrimitive(Object value) {
041: return (int) objectToLongPrimitive(value);
042: }
043:
044: public short objectToShortPrimitive(Object value) {
045: return (short) objectToLongPrimitive(value);
046: }
047:
048: public double objectToDoublePrimitive(Object value) {
049: if (value == null) {
050: return 0;
051: }
052: if (value instanceof Number) {
053: return TypeUtil.Number2double((Number) value);
054: } else if (value instanceof String) {
055: return TypeUtil.S2BigDecimal((String) value).doubleValue();
056: } else {
057: throw new IllegalArgumentException("Cannot convert value "
058: + value + " to long");
059: }
060: }
061:
062: public float objectToFloatPrimitive(Object value) {
063: return (float) objectToDoublePrimitive(value);
064: }
065:
066: public boolean objectToBooleanPrimitive(Object value) {
067: if (value instanceof String) {
068: return TypeUtil.S2b((String) value, TypeUtil.Y_N);
069: } else {
070: throw new IllegalArgumentException("Cannot convert value "
071: + value + " to boolean");
072: }
073: }
074:
075: public Long objectToLong(Object value) {
076: if (value == null) {
077: return null;
078: }
079: return new Long(objectToLongPrimitive(value));
080: }
081:
082: public Integer objectToInt(Object value) {
083: if (value == null) {
084: return null;
085: }
086: return new Integer(objectToIntPrimitive(value));
087: }
088:
089: public Short objectToShort(Object value) {
090: if (value == null) {
091: return null;
092: }
093: return new Short(objectToShortPrimitive(value));
094: }
095:
096: public BigDecimal objectToBigDecimal(Object value) {
097: if (value == null) {
098: return null;
099: }
100:
101: if (value instanceof String) {
102: return TypeUtil.S2BigDecimal((String) value);
103: } else if (value instanceof Number) {
104: return TypeUtil.number2BigDecimal((Number) value);
105: } else {
106: throw new OdalRuntimeException("Cannot convert value "
107: + value + " to BigDecimal");
108: }
109: }
110:
111: public BigInteger objectToBigInteger(Object value) {
112: if (value == null) {
113: return null;
114: }
115:
116: if (value instanceof String) {
117: return TypeUtil.S2BigDecimal((String) value).toBigInteger();
118: } else if (value instanceof Number) {
119: return TypeUtil.number2BigDecimal((Number) value)
120: .toBigInteger();
121: } else {
122: throw new OdalRuntimeException("Cannot convert value "
123: + value + " to BigDecimal");
124: }
125: }
126:
127: public Double objectToDouble(Object value) {
128: if (value == null) {
129: return null;
130: }
131: return new Double(objectToDoublePrimitive(value));
132: }
133:
134: public Float objectToFloat(Object value) {
135: if (value == null) {
136: return null;
137: }
138: return new Float(objectToFloatPrimitive(value));
139: }
140:
141: public byte[] objectToByteArray(Object value) {
142: if (value == null) {
143: return null;
144: }
145: try {
146: if (value instanceof byte[]) {
147: return (byte[]) value;
148: } else {
149: return (byte[]) new ByteArrayTypeHandler()
150: .transformRead(value);
151: }
152: } catch (SQLException e) {
153: throw new OdalRuntimeException(e);
154: }
155: }
156:
157: public Boolean objectToBoolean(Object value) {
158: if (value instanceof Boolean) {
159: return (Boolean) value;
160: } else if (value instanceof String) {
161: return TypeUtil.S2B((String) value, TypeUtil.Y_N);
162: } else {
163: throw new IllegalArgumentException("Cannot convert value "
164: + value + " to boolean");
165: }
166: }
167:
168: public Object convertBeanValueToPoValue(Object beanValue,
169: Class beanClass, Class poClass) {
170: return beanValue;
171: }
172:
173: public Object convertPoValueToBeanValue(Object poValue,
174: Class beanClass, Class poClass) {
175: if (poValue == null) {
176: if (beanClass.isPrimitive()) {
177: poValue = ridOfNullsForPrimitives(beanClass, poValue);
178: } else {
179: return null;
180: }
181: }
182:
183: assertBeanClassNotNull(beanClass);
184:
185: beanClass = toNonPrimitiveClass(beanClass);
186:
187: return convert(beanClass, poValue);
188: }
189:
190: protected Object convert(Class beanClass, Object poValue) {
191: if (Number.class.isAssignableFrom(beanClass)) {
192: return convertToNumber(beanClass, poValue);
193: } else if (String.class.isAssignableFrom(beanClass)) {
194: return convertToString(beanClass, poValue);
195: } else if (Character.class.isAssignableFrom(beanClass)) {
196: return convertToString(beanClass, poValue);
197: } else if (Boolean.class.isAssignableFrom(beanClass)) {
198: return convertToBoolean(beanClass, poValue);
199: } else if (byte[].class.isAssignableFrom(beanClass)) {
200: return convertToByteArray(beanClass, poValue);
201: } else {
202: return convertToOther(beanClass, poValue);
203: }
204: }
205:
206: private Object convertToByteArray(Class beanClass, Object poValue) {
207: return objectToByteArray(poValue);
208: }
209:
210: private void assertBeanClassNotNull(Class beanClass) {
211: if (beanClass == null) {
212: throw new IllegalArgumentException("Bean class is not set");
213: }
214: }
215:
216: protected Object ridOfNullsForPrimitives(Class beanClass,
217: Object poValue) {
218: if (beanClass == boolean.class) {
219: poValue = Boolean.FALSE;
220: } else if (beanClass == char.class) {
221: } else if (beanClass == void.class) {
222: } else {
223: poValue = new BigDecimal("0");
224: }
225: return poValue;
226: }
227:
228: private Object convertToOther(Class beanClass, Object poValue) {
229: return poValue;
230: }
231:
232: private Boolean convertToBoolean(Class beanClass, Object poValue) {
233: return objectToBoolean(poValue);
234: }
235:
236: private String convertToString(Class beanClass, Object poValue) {
237: if (poValue instanceof Clob) {
238: ClobStringHandler handler = new ClobStringHandler();
239: try {
240: return (String) handler.transformRead(poValue);
241: } catch (SQLException e) {
242: throw new OdalRuntimeException(
243: "Cannot convert clob value to string: " + e);
244: }
245: } else {
246: return poValue.toString();
247: }
248:
249: }
250:
251: protected Object convertToNumber(Class beanClass, Object poValue) {
252: if (beanClass == Double.class) {
253: return objectToDouble(poValue);
254: } else if (beanClass == Float.class) {
255: return objectToFloat(poValue);
256: } else if (beanClass == Long.class) {
257: return objectToLong(poValue);
258: } else if (beanClass == Integer.class
259: || beanClass == Byte.class) {
260: return objectToInt(poValue);
261: } else if (beanClass == Short.class) {
262: return objectToShort(poValue);
263: } else if (beanClass == BigDecimal.class) {
264: return objectToBigDecimal(poValue);
265: } else if (beanClass == BigInteger.class) {
266: return objectToBigInteger(poValue);
267: } else {
268: throw new OdalRuntimeException("Cannot convert value "
269: + poValue + " to " + beanClass);
270: }
271: }
272:
273: protected Class toNonPrimitiveClass(Class primitive) {
274: if (primitive == null || !primitive.isPrimitive()) {
275: return primitive;
276: } else if (boolean.class == primitive) {
277: return Boolean.class;
278: } else if (char.class == primitive) {
279: return Character.class;
280: } else if (byte.class == primitive) {
281: return Byte.class;
282: } else if (byte.class == primitive) {
283: return Byte.class;
284: } else if (short.class == primitive) {
285: return Short.class;
286: } else if (int.class == primitive) {
287: return Integer.class;
288: } else if (long.class == primitive) {
289: return Long.class;
290: } else if (double.class == primitive) {
291: return Double.class;
292: } else if (float.class == primitive) {
293: return Float.class;
294: } else if (void.class == primitive) {
295: return Void.class;
296: } else {
297: throw new IllegalArgumentException(
298: "Unsupporter primitive class " + primitive);
299: }
300: }
301:
302: }
|